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"
51 struct mlx5e_rq_param {
52 u32 rqc[MLX5_ST_SZ_DW(rqc)];
53 struct mlx5_wq_param wq;
54 struct mlx5e_rq_frags_info frags_info;
57 struct mlx5e_sq_param {
58 u32 sqc[MLX5_ST_SZ_DW(sqc)];
59 struct mlx5_wq_param wq;
62 struct mlx5e_cq_param {
63 u32 cqc[MLX5_ST_SZ_DW(cqc)];
64 struct mlx5_wq_param wq;
69 struct mlx5e_channel_param {
70 struct mlx5e_rq_param rq;
71 struct mlx5e_sq_param sq;
72 struct mlx5e_sq_param xdp_sq;
73 struct mlx5e_sq_param icosq;
74 struct mlx5e_cq_param rx_cq;
75 struct mlx5e_cq_param tx_cq;
76 struct mlx5e_cq_param icosq_cq;
79 bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
81 bool striding_rq_umr = MLX5_CAP_GEN(mdev, striding_rq) &&
82 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
83 MLX5_CAP_ETH(mdev, reg_umr_sq);
84 u16 max_wqe_sz_cap = MLX5_CAP_GEN(mdev, max_wqe_sz_sq);
85 bool inline_umr = MLX5E_UMR_WQE_INLINE_SZ <= max_wqe_sz_cap;
90 mlx5_core_warn(mdev, "Cannot support Striding RQ: UMR WQE size (%d) exceeds maximum supported (%d).\n",
91 (int)MLX5E_UMR_WQE_INLINE_SZ, max_wqe_sz_cap);
97 static u32 mlx5e_rx_get_linear_frag_sz(struct mlx5e_params *params)
99 if (!params->xdp_prog) {
100 u16 hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
101 u16 rq_headroom = MLX5_RX_HEADROOM + NET_IP_ALIGN;
103 return MLX5_SKB_FRAG_SZ(rq_headroom + hw_mtu);
109 static u8 mlx5e_mpwqe_log_pkts_per_wqe(struct mlx5e_params *params)
111 u32 linear_frag_sz = mlx5e_rx_get_linear_frag_sz(params);
113 return MLX5_MPWRQ_LOG_WQE_SZ - order_base_2(linear_frag_sz);
116 static bool mlx5e_rx_is_linear_skb(struct mlx5_core_dev *mdev,
117 struct mlx5e_params *params)
119 u32 frag_sz = mlx5e_rx_get_linear_frag_sz(params);
121 return !params->lro_en && frag_sz <= PAGE_SIZE;
124 static bool mlx5e_rx_mpwqe_is_linear_skb(struct mlx5_core_dev *mdev,
125 struct mlx5e_params *params)
127 u32 frag_sz = mlx5e_rx_get_linear_frag_sz(params);
128 s8 signed_log_num_strides_param;
131 if (!mlx5e_rx_is_linear_skb(mdev, params))
134 if (MLX5_CAP_GEN(mdev, ext_stride_num_range))
137 log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ - order_base_2(frag_sz);
138 signed_log_num_strides_param =
139 (s8)log_num_strides - MLX5_MPWQE_LOG_NUM_STRIDES_BASE;
141 return signed_log_num_strides_param >= 0;
144 static u8 mlx5e_mpwqe_get_log_rq_size(struct mlx5e_params *params)
146 if (params->log_rq_mtu_frames <
147 mlx5e_mpwqe_log_pkts_per_wqe(params) + MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW)
148 return MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW;
150 return params->log_rq_mtu_frames - mlx5e_mpwqe_log_pkts_per_wqe(params);
153 static u8 mlx5e_mpwqe_get_log_stride_size(struct mlx5_core_dev *mdev,
154 struct mlx5e_params *params)
156 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, params))
157 return order_base_2(mlx5e_rx_get_linear_frag_sz(params));
159 return MLX5E_MPWQE_STRIDE_SZ(mdev,
160 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
163 static u8 mlx5e_mpwqe_get_log_num_strides(struct mlx5_core_dev *mdev,
164 struct mlx5e_params *params)
166 return MLX5_MPWRQ_LOG_WQE_SZ -
167 mlx5e_mpwqe_get_log_stride_size(mdev, params);
170 static u16 mlx5e_get_rq_headroom(struct mlx5_core_dev *mdev,
171 struct mlx5e_params *params)
173 u16 linear_rq_headroom = params->xdp_prog ?
174 XDP_PACKET_HEADROOM : MLX5_RX_HEADROOM;
177 linear_rq_headroom += NET_IP_ALIGN;
179 is_linear_skb = (params->rq_wq_type == MLX5_WQ_TYPE_CYCLIC) ?
180 mlx5e_rx_is_linear_skb(mdev, params) :
181 mlx5e_rx_mpwqe_is_linear_skb(mdev, params);
183 return is_linear_skb ? linear_rq_headroom : 0;
186 void mlx5e_init_rq_type_params(struct mlx5_core_dev *mdev,
187 struct mlx5e_params *params)
189 params->lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
190 params->log_rq_mtu_frames = is_kdump_kernel() ?
191 MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE :
192 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
194 mlx5_core_info(mdev, "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
195 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
196 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ ?
197 BIT(mlx5e_mpwqe_get_log_rq_size(params)) :
198 BIT(params->log_rq_mtu_frames),
199 BIT(mlx5e_mpwqe_get_log_stride_size(mdev, params)),
200 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
203 bool mlx5e_striding_rq_possible(struct mlx5_core_dev *mdev,
204 struct mlx5e_params *params)
206 return mlx5e_check_fragmented_striding_rq_cap(mdev) &&
207 !MLX5_IPSEC_DEV(mdev) &&
208 !(params->xdp_prog && !mlx5e_rx_mpwqe_is_linear_skb(mdev, params));
211 void mlx5e_set_rq_type(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
213 params->rq_wq_type = mlx5e_striding_rq_possible(mdev, params) &&
214 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ) ?
215 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
219 static void mlx5e_update_carrier(struct mlx5e_priv *priv)
221 struct mlx5_core_dev *mdev = priv->mdev;
224 port_state = mlx5_query_vport_state(mdev,
225 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT,
228 if (port_state == VPORT_STATE_UP) {
229 netdev_info(priv->netdev, "Link up\n");
230 netif_carrier_on(priv->netdev);
232 netdev_info(priv->netdev, "Link down\n");
233 netif_carrier_off(priv->netdev);
237 static void mlx5e_update_carrier_work(struct work_struct *work)
239 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
240 update_carrier_work);
242 mutex_lock(&priv->state_lock);
243 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
244 if (priv->profile->update_carrier)
245 priv->profile->update_carrier(priv);
246 mutex_unlock(&priv->state_lock);
249 void mlx5e_update_stats(struct mlx5e_priv *priv)
253 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
254 if (mlx5e_stats_grps[i].update_stats)
255 mlx5e_stats_grps[i].update_stats(priv);
258 static void mlx5e_update_ndo_stats(struct mlx5e_priv *priv)
262 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
263 if (mlx5e_stats_grps[i].update_stats_mask &
264 MLX5E_NDO_UPDATE_STATS)
265 mlx5e_stats_grps[i].update_stats(priv);
268 void mlx5e_update_stats_work(struct work_struct *work)
270 struct delayed_work *dwork = to_delayed_work(work);
271 struct mlx5e_priv *priv = container_of(dwork, struct mlx5e_priv,
273 mutex_lock(&priv->state_lock);
274 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
275 priv->profile->update_stats(priv);
276 queue_delayed_work(priv->wq, dwork,
277 msecs_to_jiffies(MLX5E_UPDATE_STATS_INTERVAL));
279 mutex_unlock(&priv->state_lock);
282 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
283 enum mlx5_dev_event event, unsigned long param)
285 struct mlx5e_priv *priv = vpriv;
287 if (!test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state))
291 case MLX5_DEV_EVENT_PORT_UP:
292 case MLX5_DEV_EVENT_PORT_DOWN:
293 queue_work(priv->wq, &priv->update_carrier_work);
300 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
302 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
305 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
307 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
308 synchronize_irq(pci_irq_vector(priv->mdev->pdev, MLX5_EQ_VEC_ASYNC));
311 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq,
312 struct mlx5e_icosq *sq,
313 struct mlx5e_umr_wqe *wqe)
315 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
316 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
317 u8 ds_cnt = DIV_ROUND_UP(MLX5E_UMR_WQE_INLINE_SZ, MLX5_SEND_WQE_DS);
319 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
321 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
322 cseg->imm = rq->mkey_be;
324 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN | MLX5_UMR_INLINE;
325 ucseg->xlt_octowords =
326 cpu_to_be16(MLX5_MTT_OCTW(MLX5_MPWRQ_PAGES_PER_WQE));
327 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
330 static u32 mlx5e_rqwq_get_size(struct mlx5e_rq *rq)
332 switch (rq->wq_type) {
333 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
334 return mlx5_wq_ll_get_size(&rq->mpwqe.wq);
336 return mlx5_wq_cyc_get_size(&rq->wqe.wq);
340 static u32 mlx5e_rqwq_get_cur_sz(struct mlx5e_rq *rq)
342 switch (rq->wq_type) {
343 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
344 return rq->mpwqe.wq.cur_sz;
346 return rq->wqe.wq.cur_sz;
350 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq,
351 struct mlx5e_channel *c)
353 int wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
355 rq->mpwqe.info = kzalloc_node(wq_sz * sizeof(*rq->mpwqe.info),
356 GFP_KERNEL, cpu_to_node(c->cpu));
360 mlx5e_build_umr_wqe(rq, &c->icosq, &rq->mpwqe.umr_wqe);
365 static int mlx5e_create_umr_mkey(struct mlx5_core_dev *mdev,
366 u64 npages, u8 page_shift,
367 struct mlx5_core_mkey *umr_mkey)
369 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
374 in = kvzalloc(inlen, GFP_KERNEL);
378 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
380 MLX5_SET(mkc, mkc, free, 1);
381 MLX5_SET(mkc, mkc, umr_en, 1);
382 MLX5_SET(mkc, mkc, lw, 1);
383 MLX5_SET(mkc, mkc, lr, 1);
384 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT);
386 MLX5_SET(mkc, mkc, qpn, 0xffffff);
387 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.pdn);
388 MLX5_SET64(mkc, mkc, len, npages << page_shift);
389 MLX5_SET(mkc, mkc, translations_octword_size,
390 MLX5_MTT_OCTW(npages));
391 MLX5_SET(mkc, mkc, log_page_size, page_shift);
393 err = mlx5_core_create_mkey(mdev, umr_mkey, in, inlen);
399 static int mlx5e_create_rq_umr_mkey(struct mlx5_core_dev *mdev, struct mlx5e_rq *rq)
401 u64 num_mtts = MLX5E_REQUIRED_MTTS(mlx5_wq_ll_get_size(&rq->mpwqe.wq));
403 return mlx5e_create_umr_mkey(mdev, num_mtts, PAGE_SHIFT, &rq->umr_mkey);
406 static inline u64 mlx5e_get_mpwqe_offset(struct mlx5e_rq *rq, u16 wqe_ix)
408 return (wqe_ix << MLX5E_LOG_ALIGNED_MPWQE_PPW) << PAGE_SHIFT;
411 static void mlx5e_init_frags_partition(struct mlx5e_rq *rq)
413 struct mlx5e_wqe_frag_info next_frag, *prev;
416 next_frag.di = &rq->wqe.di[0];
417 next_frag.offset = 0;
420 for (i = 0; i < mlx5_wq_cyc_get_size(&rq->wqe.wq); i++) {
421 struct mlx5e_rq_frag_info *frag_info = &rq->wqe.info.arr[0];
422 struct mlx5e_wqe_frag_info *frag =
423 &rq->wqe.frags[i << rq->wqe.info.log_num_frags];
426 for (f = 0; f < rq->wqe.info.num_frags; f++, frag++) {
427 if (next_frag.offset + frag_info[f].frag_stride > PAGE_SIZE) {
429 next_frag.offset = 0;
431 prev->last_in_page = true;
436 next_frag.offset += frag_info[f].frag_stride;
442 prev->last_in_page = true;
445 static int mlx5e_init_di_list(struct mlx5e_rq *rq,
446 struct mlx5e_params *params,
449 int len = wq_sz << rq->wqe.info.log_num_frags;
451 rq->wqe.di = kvzalloc_node(len * sizeof(*rq->wqe.di),
452 GFP_KERNEL, cpu_to_node(cpu));
456 mlx5e_init_frags_partition(rq);
461 static void mlx5e_free_di_list(struct mlx5e_rq *rq)
466 static int mlx5e_alloc_rq(struct mlx5e_channel *c,
467 struct mlx5e_params *params,
468 struct mlx5e_rq_param *rqp,
471 struct page_pool_params pp_params = { 0 };
472 struct mlx5_core_dev *mdev = c->mdev;
473 void *rqc = rqp->rqc;
474 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
480 rqp->wq.db_numa_node = cpu_to_node(c->cpu);
482 rq->wq_type = params->rq_wq_type;
484 rq->netdev = c->netdev;
485 rq->tstamp = c->tstamp;
486 rq->clock = &mdev->clock;
490 rq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
491 rq->stats = &c->priv->channel_stats[c->ix].rq;
493 rq->xdp_prog = params->xdp_prog ? bpf_prog_inc(params->xdp_prog) : NULL;
494 if (IS_ERR(rq->xdp_prog)) {
495 err = PTR_ERR(rq->xdp_prog);
497 goto err_rq_wq_destroy;
500 err = xdp_rxq_info_reg(&rq->xdp_rxq, rq->netdev, rq->ix);
502 goto err_rq_wq_destroy;
504 rq->buff.map_dir = rq->xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
505 rq->buff.headroom = mlx5e_get_rq_headroom(mdev, params);
506 pool_size = 1 << params->log_rq_mtu_frames;
508 switch (rq->wq_type) {
509 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
510 err = mlx5_wq_ll_create(mdev, &rqp->wq, rqc_wq, &rq->mpwqe.wq,
515 rq->mpwqe.wq.db = &rq->mpwqe.wq.db[MLX5_RCV_DBR];
517 wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
519 pool_size = MLX5_MPWRQ_PAGES_PER_WQE << mlx5e_mpwqe_get_log_rq_size(params);
521 rq->post_wqes = mlx5e_post_rx_mpwqes;
522 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
524 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe_mpwqe;
525 #ifdef CONFIG_MLX5_EN_IPSEC
526 if (MLX5_IPSEC_DEV(mdev)) {
528 netdev_err(c->netdev, "MPWQE RQ with IPSec offload not supported\n");
529 goto err_rq_wq_destroy;
532 if (!rq->handle_rx_cqe) {
534 netdev_err(c->netdev, "RX handler of MPWQE RQ is not set, err %d\n", err);
535 goto err_rq_wq_destroy;
538 rq->mpwqe.skb_from_cqe_mpwrq =
539 mlx5e_rx_mpwqe_is_linear_skb(mdev, params) ?
540 mlx5e_skb_from_cqe_mpwrq_linear :
541 mlx5e_skb_from_cqe_mpwrq_nonlinear;
542 rq->mpwqe.log_stride_sz = mlx5e_mpwqe_get_log_stride_size(mdev, params);
543 rq->mpwqe.num_strides = BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params));
545 err = mlx5e_create_rq_umr_mkey(mdev, rq);
547 goto err_rq_wq_destroy;
548 rq->mkey_be = cpu_to_be32(rq->umr_mkey.key);
550 err = mlx5e_rq_alloc_mpwqe_info(rq, c);
554 default: /* MLX5_WQ_TYPE_CYCLIC */
555 err = mlx5_wq_cyc_create(mdev, &rqp->wq, rqc_wq, &rq->wqe.wq,
560 rq->wqe.wq.db = &rq->wqe.wq.db[MLX5_RCV_DBR];
562 wq_sz = mlx5_wq_cyc_get_size(&rq->wqe.wq);
564 rq->wqe.info = rqp->frags_info;
566 kvzalloc_node((wq_sz << rq->wqe.info.log_num_frags) *
567 sizeof(*rq->wqe.frags),
568 GFP_KERNEL, cpu_to_node(c->cpu));
572 err = mlx5e_init_di_list(rq, params, wq_sz, c->cpu);
575 rq->post_wqes = mlx5e_post_rx_wqes;
576 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
578 #ifdef CONFIG_MLX5_EN_IPSEC
580 rq->handle_rx_cqe = mlx5e_ipsec_handle_rx_cqe;
583 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe;
584 if (!rq->handle_rx_cqe) {
586 netdev_err(c->netdev, "RX handler of RQ is not set, err %d\n", err);
590 rq->wqe.skb_from_cqe = mlx5e_rx_is_linear_skb(mdev, params) ?
591 mlx5e_skb_from_cqe_linear :
592 mlx5e_skb_from_cqe_nonlinear;
593 rq->mkey_be = c->mkey_be;
596 /* Create a page_pool and register it with rxq */
598 pp_params.flags = 0; /* No-internal DMA mapping in page_pool */
599 pp_params.pool_size = pool_size;
600 pp_params.nid = cpu_to_node(c->cpu);
601 pp_params.dev = c->pdev;
602 pp_params.dma_dir = rq->buff.map_dir;
604 /* page_pool can be used even when there is no rq->xdp_prog,
605 * given page_pool does not handle DMA mapping there is no
606 * required state to clear. And page_pool gracefully handle
609 rq->page_pool = page_pool_create(&pp_params);
610 if (IS_ERR(rq->page_pool)) {
611 err = PTR_ERR(rq->page_pool);
612 rq->page_pool = NULL;
615 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
616 MEM_TYPE_PAGE_POOL, rq->page_pool);
620 for (i = 0; i < wq_sz; i++) {
621 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
622 struct mlx5e_rx_wqe_ll *wqe =
623 mlx5_wq_ll_get_wqe(&rq->mpwqe.wq, i);
625 rq->mpwqe.num_strides << rq->mpwqe.log_stride_sz;
626 u64 dma_offset = mlx5e_get_mpwqe_offset(rq, i);
628 wqe->data[0].addr = cpu_to_be64(dma_offset + rq->buff.headroom);
629 wqe->data[0].byte_count = cpu_to_be32(byte_count);
630 wqe->data[0].lkey = rq->mkey_be;
632 struct mlx5e_rx_wqe_cyc *wqe =
633 mlx5_wq_cyc_get_wqe(&rq->wqe.wq, i);
636 for (f = 0; f < rq->wqe.info.num_frags; f++) {
637 u32 frag_size = rq->wqe.info.arr[f].frag_size |
638 MLX5_HW_START_PADDING;
640 wqe->data[f].byte_count = cpu_to_be32(frag_size);
641 wqe->data[f].lkey = rq->mkey_be;
643 /* check if num_frags is not a pow of two */
644 if (rq->wqe.info.num_frags < (1 << rq->wqe.info.log_num_frags)) {
645 wqe->data[f].byte_count = 0;
646 wqe->data[f].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
647 wqe->data[f].addr = 0;
652 INIT_WORK(&rq->dim.work, mlx5e_rx_dim_work);
654 switch (params->rx_cq_moderation.cq_period_mode) {
655 case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
656 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE;
658 case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
660 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
663 rq->page_cache.head = 0;
664 rq->page_cache.tail = 0;
669 switch (rq->wq_type) {
670 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
671 kfree(rq->mpwqe.info);
672 mlx5_core_destroy_mkey(mdev, &rq->umr_mkey);
674 default: /* MLX5_WQ_TYPE_CYCLIC */
675 kvfree(rq->wqe.frags);
676 mlx5e_free_di_list(rq);
681 bpf_prog_put(rq->xdp_prog);
682 xdp_rxq_info_unreg(&rq->xdp_rxq);
684 page_pool_destroy(rq->page_pool);
685 mlx5_wq_destroy(&rq->wq_ctrl);
690 static void mlx5e_free_rq(struct mlx5e_rq *rq)
695 bpf_prog_put(rq->xdp_prog);
697 xdp_rxq_info_unreg(&rq->xdp_rxq);
699 page_pool_destroy(rq->page_pool);
701 switch (rq->wq_type) {
702 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
703 kfree(rq->mpwqe.info);
704 mlx5_core_destroy_mkey(rq->mdev, &rq->umr_mkey);
706 default: /* MLX5_WQ_TYPE_CYCLIC */
707 kvfree(rq->wqe.frags);
708 mlx5e_free_di_list(rq);
711 for (i = rq->page_cache.head; i != rq->page_cache.tail;
712 i = (i + 1) & (MLX5E_CACHE_SIZE - 1)) {
713 struct mlx5e_dma_info *dma_info = &rq->page_cache.page_cache[i];
715 mlx5e_page_release(rq, dma_info, false);
717 mlx5_wq_destroy(&rq->wq_ctrl);
720 static int mlx5e_create_rq(struct mlx5e_rq *rq,
721 struct mlx5e_rq_param *param)
723 struct mlx5_core_dev *mdev = rq->mdev;
731 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
732 sizeof(u64) * rq->wq_ctrl.buf.npages;
733 in = kvzalloc(inlen, GFP_KERNEL);
737 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
738 wq = MLX5_ADDR_OF(rqc, rqc, wq);
740 memcpy(rqc, param->rqc, sizeof(param->rqc));
742 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
743 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
744 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
745 MLX5_ADAPTER_PAGE_SHIFT);
746 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
748 mlx5_fill_page_frag_array(&rq->wq_ctrl.buf,
749 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
751 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
758 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
761 struct mlx5_core_dev *mdev = rq->mdev;
768 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
769 in = kvzalloc(inlen, GFP_KERNEL);
773 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
775 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
776 MLX5_SET(rqc, rqc, state, next_state);
778 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
785 static int mlx5e_modify_rq_scatter_fcs(struct mlx5e_rq *rq, bool enable)
787 struct mlx5e_channel *c = rq->channel;
788 struct mlx5e_priv *priv = c->priv;
789 struct mlx5_core_dev *mdev = priv->mdev;
796 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
797 in = kvzalloc(inlen, GFP_KERNEL);
801 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
803 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
804 MLX5_SET64(modify_rq_in, in, modify_bitmask,
805 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_SCATTER_FCS);
806 MLX5_SET(rqc, rqc, scatter_fcs, enable);
807 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
809 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
816 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
818 struct mlx5e_channel *c = rq->channel;
819 struct mlx5_core_dev *mdev = c->mdev;
825 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
826 in = kvzalloc(inlen, GFP_KERNEL);
830 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
832 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
833 MLX5_SET64(modify_rq_in, in, modify_bitmask,
834 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
835 MLX5_SET(rqc, rqc, vsd, vsd);
836 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
838 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
845 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
847 mlx5_core_destroy_rq(rq->mdev, rq->rqn);
850 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq, int wait_time)
852 unsigned long exp_time = jiffies + msecs_to_jiffies(wait_time);
853 struct mlx5e_channel *c = rq->channel;
855 u16 min_wqes = mlx5_min_rx_wqes(rq->wq_type, mlx5e_rqwq_get_size(rq));
858 if (mlx5e_rqwq_get_cur_sz(rq) >= min_wqes)
862 } while (time_before(jiffies, exp_time));
864 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",
865 c->ix, rq->rqn, mlx5e_rqwq_get_cur_sz(rq), min_wqes);
870 static void mlx5e_free_rx_descs(struct mlx5e_rq *rq)
875 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
876 struct mlx5_wq_ll *wq = &rq->mpwqe.wq;
878 /* UMR WQE (if in progress) is always at wq->head */
879 if (rq->mpwqe.umr_in_progress)
880 mlx5e_free_rx_mpwqe(rq, &rq->mpwqe.info[wq->head]);
882 while (!mlx5_wq_ll_is_empty(wq)) {
883 struct mlx5e_rx_wqe_ll *wqe;
885 wqe_ix_be = *wq->tail_next;
886 wqe_ix = be16_to_cpu(wqe_ix_be);
887 wqe = mlx5_wq_ll_get_wqe(wq, wqe_ix);
888 rq->dealloc_wqe(rq, wqe_ix);
889 mlx5_wq_ll_pop(wq, wqe_ix_be,
890 &wqe->next.next_wqe_index);
893 struct mlx5_wq_cyc *wq = &rq->wqe.wq;
895 while (!mlx5_wq_cyc_is_empty(wq)) {
896 wqe_ix = mlx5_wq_cyc_get_tail(wq);
897 rq->dealloc_wqe(rq, wqe_ix);
904 static int mlx5e_open_rq(struct mlx5e_channel *c,
905 struct mlx5e_params *params,
906 struct mlx5e_rq_param *param,
911 err = mlx5e_alloc_rq(c, params, param, rq);
915 err = mlx5e_create_rq(rq, param);
919 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
923 if (params->rx_dim_enabled)
924 __set_bit(MLX5E_RQ_STATE_AM, &c->rq.state);
929 mlx5e_destroy_rq(rq);
936 static void mlx5e_activate_rq(struct mlx5e_rq *rq)
938 struct mlx5e_icosq *sq = &rq->channel->icosq;
939 struct mlx5_wq_cyc *wq = &sq->wq;
940 struct mlx5e_tx_wqe *nopwqe;
942 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
944 set_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
945 sq->db.ico_wqe[pi].opcode = MLX5_OPCODE_NOP;
946 nopwqe = mlx5e_post_nop(wq, sq->sqn, &sq->pc);
947 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &nopwqe->ctrl);
950 static void mlx5e_deactivate_rq(struct mlx5e_rq *rq)
952 clear_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
953 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
956 static void mlx5e_close_rq(struct mlx5e_rq *rq)
958 cancel_work_sync(&rq->dim.work);
959 mlx5e_destroy_rq(rq);
960 mlx5e_free_rx_descs(rq);
964 static void mlx5e_free_xdpsq_db(struct mlx5e_xdpsq *sq)
969 static int mlx5e_alloc_xdpsq_db(struct mlx5e_xdpsq *sq, int numa)
971 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
973 sq->db.di = kzalloc_node(sizeof(*sq->db.di) * wq_sz,
976 mlx5e_free_xdpsq_db(sq);
983 static int mlx5e_alloc_xdpsq(struct mlx5e_channel *c,
984 struct mlx5e_params *params,
985 struct mlx5e_sq_param *param,
986 struct mlx5e_xdpsq *sq)
988 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
989 struct mlx5_core_dev *mdev = c->mdev;
990 struct mlx5_wq_cyc *wq = &sq->wq;
994 sq->mkey_be = c->mkey_be;
996 sq->uar_map = mdev->mlx5e_res.bfreg.map;
997 sq->min_inline_mode = params->tx_min_inline_mode;
999 param->wq.db_numa_node = cpu_to_node(c->cpu);
1000 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1003 wq->db = &wq->db[MLX5_SND_DBR];
1005 err = mlx5e_alloc_xdpsq_db(sq, cpu_to_node(c->cpu));
1007 goto err_sq_wq_destroy;
1012 mlx5_wq_destroy(&sq->wq_ctrl);
1017 static void mlx5e_free_xdpsq(struct mlx5e_xdpsq *sq)
1019 mlx5e_free_xdpsq_db(sq);
1020 mlx5_wq_destroy(&sq->wq_ctrl);
1023 static void mlx5e_free_icosq_db(struct mlx5e_icosq *sq)
1025 kfree(sq->db.ico_wqe);
1028 static int mlx5e_alloc_icosq_db(struct mlx5e_icosq *sq, int numa)
1030 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1032 sq->db.ico_wqe = kzalloc_node(sizeof(*sq->db.ico_wqe) * wq_sz,
1034 if (!sq->db.ico_wqe)
1040 static int mlx5e_alloc_icosq(struct mlx5e_channel *c,
1041 struct mlx5e_sq_param *param,
1042 struct mlx5e_icosq *sq)
1044 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1045 struct mlx5_core_dev *mdev = c->mdev;
1046 struct mlx5_wq_cyc *wq = &sq->wq;
1050 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1052 param->wq.db_numa_node = cpu_to_node(c->cpu);
1053 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1056 wq->db = &wq->db[MLX5_SND_DBR];
1058 err = mlx5e_alloc_icosq_db(sq, cpu_to_node(c->cpu));
1060 goto err_sq_wq_destroy;
1065 mlx5_wq_destroy(&sq->wq_ctrl);
1070 static void mlx5e_free_icosq(struct mlx5e_icosq *sq)
1072 mlx5e_free_icosq_db(sq);
1073 mlx5_wq_destroy(&sq->wq_ctrl);
1076 static void mlx5e_free_txqsq_db(struct mlx5e_txqsq *sq)
1078 kfree(sq->db.wqe_info);
1079 kfree(sq->db.dma_fifo);
1082 static int mlx5e_alloc_txqsq_db(struct mlx5e_txqsq *sq, int numa)
1084 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1085 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
1087 sq->db.dma_fifo = kzalloc_node(df_sz * sizeof(*sq->db.dma_fifo),
1089 sq->db.wqe_info = kzalloc_node(wq_sz * sizeof(*sq->db.wqe_info),
1091 if (!sq->db.dma_fifo || !sq->db.wqe_info) {
1092 mlx5e_free_txqsq_db(sq);
1096 sq->dma_fifo_mask = df_sz - 1;
1101 static void mlx5e_sq_recover(struct work_struct *work);
1102 static int mlx5e_alloc_txqsq(struct mlx5e_channel *c,
1104 struct mlx5e_params *params,
1105 struct mlx5e_sq_param *param,
1106 struct mlx5e_txqsq *sq,
1109 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1110 struct mlx5_core_dev *mdev = c->mdev;
1111 struct mlx5_wq_cyc *wq = &sq->wq;
1115 sq->tstamp = c->tstamp;
1116 sq->clock = &mdev->clock;
1117 sq->mkey_be = c->mkey_be;
1119 sq->txq_ix = txq_ix;
1120 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1121 sq->min_inline_mode = params->tx_min_inline_mode;
1122 sq->stats = &c->priv->channel_stats[c->ix].sq[tc];
1123 INIT_WORK(&sq->recover.recover_work, mlx5e_sq_recover);
1124 if (MLX5_IPSEC_DEV(c->priv->mdev))
1125 set_bit(MLX5E_SQ_STATE_IPSEC, &sq->state);
1126 if (mlx5_accel_is_tls_device(c->priv->mdev))
1127 set_bit(MLX5E_SQ_STATE_TLS, &sq->state);
1129 param->wq.db_numa_node = cpu_to_node(c->cpu);
1130 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1133 wq->db = &wq->db[MLX5_SND_DBR];
1135 err = mlx5e_alloc_txqsq_db(sq, cpu_to_node(c->cpu));
1137 goto err_sq_wq_destroy;
1139 INIT_WORK(&sq->dim.work, mlx5e_tx_dim_work);
1140 sq->dim.mode = params->tx_cq_moderation.cq_period_mode;
1145 mlx5_wq_destroy(&sq->wq_ctrl);
1150 static void mlx5e_free_txqsq(struct mlx5e_txqsq *sq)
1152 mlx5e_free_txqsq_db(sq);
1153 mlx5_wq_destroy(&sq->wq_ctrl);
1156 struct mlx5e_create_sq_param {
1157 struct mlx5_wq_ctrl *wq_ctrl;
1164 static int mlx5e_create_sq(struct mlx5_core_dev *mdev,
1165 struct mlx5e_sq_param *param,
1166 struct mlx5e_create_sq_param *csp,
1175 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1176 sizeof(u64) * csp->wq_ctrl->buf.npages;
1177 in = kvzalloc(inlen, GFP_KERNEL);
1181 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1182 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1184 memcpy(sqc, param->sqc, sizeof(param->sqc));
1185 MLX5_SET(sqc, sqc, tis_lst_sz, csp->tis_lst_sz);
1186 MLX5_SET(sqc, sqc, tis_num_0, csp->tisn);
1187 MLX5_SET(sqc, sqc, cqn, csp->cqn);
1189 if (MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5_CAP_INLINE_MODE_VPORT_CONTEXT)
1190 MLX5_SET(sqc, sqc, min_wqe_inline_mode, csp->min_inline_mode);
1192 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1193 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1195 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1196 MLX5_SET(wq, wq, uar_page, mdev->mlx5e_res.bfreg.index);
1197 MLX5_SET(wq, wq, log_wq_pg_sz, csp->wq_ctrl->buf.page_shift -
1198 MLX5_ADAPTER_PAGE_SHIFT);
1199 MLX5_SET64(wq, wq, dbr_addr, csp->wq_ctrl->db.dma);
1201 mlx5_fill_page_frag_array(&csp->wq_ctrl->buf,
1202 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
1204 err = mlx5_core_create_sq(mdev, in, inlen, sqn);
1211 struct mlx5e_modify_sq_param {
1218 static int mlx5e_modify_sq(struct mlx5_core_dev *mdev, u32 sqn,
1219 struct mlx5e_modify_sq_param *p)
1226 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1227 in = kvzalloc(inlen, GFP_KERNEL);
1231 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1233 MLX5_SET(modify_sq_in, in, sq_state, p->curr_state);
1234 MLX5_SET(sqc, sqc, state, p->next_state);
1235 if (p->rl_update && p->next_state == MLX5_SQC_STATE_RDY) {
1236 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
1237 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, p->rl_index);
1240 err = mlx5_core_modify_sq(mdev, sqn, in, inlen);
1247 static void mlx5e_destroy_sq(struct mlx5_core_dev *mdev, u32 sqn)
1249 mlx5_core_destroy_sq(mdev, sqn);
1252 static int mlx5e_create_sq_rdy(struct mlx5_core_dev *mdev,
1253 struct mlx5e_sq_param *param,
1254 struct mlx5e_create_sq_param *csp,
1257 struct mlx5e_modify_sq_param msp = {0};
1260 err = mlx5e_create_sq(mdev, param, csp, sqn);
1264 msp.curr_state = MLX5_SQC_STATE_RST;
1265 msp.next_state = MLX5_SQC_STATE_RDY;
1266 err = mlx5e_modify_sq(mdev, *sqn, &msp);
1268 mlx5e_destroy_sq(mdev, *sqn);
1273 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1274 struct mlx5e_txqsq *sq, u32 rate);
1276 static int mlx5e_open_txqsq(struct mlx5e_channel *c,
1279 struct mlx5e_params *params,
1280 struct mlx5e_sq_param *param,
1281 struct mlx5e_txqsq *sq,
1284 struct mlx5e_create_sq_param csp = {};
1288 err = mlx5e_alloc_txqsq(c, txq_ix, params, param, sq, tc);
1294 csp.cqn = sq->cq.mcq.cqn;
1295 csp.wq_ctrl = &sq->wq_ctrl;
1296 csp.min_inline_mode = sq->min_inline_mode;
1297 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1299 goto err_free_txqsq;
1301 tx_rate = c->priv->tx_rates[sq->txq_ix];
1303 mlx5e_set_sq_maxrate(c->netdev, sq, tx_rate);
1305 if (params->tx_dim_enabled)
1306 sq->state |= BIT(MLX5E_SQ_STATE_AM);
1311 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1312 mlx5e_free_txqsq(sq);
1317 static void mlx5e_reset_txqsq_cc_pc(struct mlx5e_txqsq *sq)
1319 WARN_ONCE(sq->cc != sq->pc,
1320 "SQ 0x%x: cc (0x%x) != pc (0x%x)\n",
1321 sq->sqn, sq->cc, sq->pc);
1323 sq->dma_fifo_cc = 0;
1327 static void mlx5e_activate_txqsq(struct mlx5e_txqsq *sq)
1329 sq->txq = netdev_get_tx_queue(sq->channel->netdev, sq->txq_ix);
1330 clear_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state);
1331 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1332 netdev_tx_reset_queue(sq->txq);
1333 netif_tx_start_queue(sq->txq);
1336 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
1338 __netif_tx_lock_bh(txq);
1339 netif_tx_stop_queue(txq);
1340 __netif_tx_unlock_bh(txq);
1343 static void mlx5e_deactivate_txqsq(struct mlx5e_txqsq *sq)
1345 struct mlx5e_channel *c = sq->channel;
1346 struct mlx5_wq_cyc *wq = &sq->wq;
1348 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1349 /* prevent netif_tx_wake_queue */
1350 napi_synchronize(&c->napi);
1352 netif_tx_disable_queue(sq->txq);
1354 /* last doorbell out, godspeed .. */
1355 if (mlx5e_wqc_has_room_for(wq, sq->cc, sq->pc, 1)) {
1356 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
1357 struct mlx5e_tx_wqe *nop;
1359 sq->db.wqe_info[pi].skb = NULL;
1360 nop = mlx5e_post_nop(wq, sq->sqn, &sq->pc);
1361 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &nop->ctrl);
1365 static void mlx5e_close_txqsq(struct mlx5e_txqsq *sq)
1367 struct mlx5e_channel *c = sq->channel;
1368 struct mlx5_core_dev *mdev = c->mdev;
1369 struct mlx5_rate_limit rl = {0};
1371 mlx5e_destroy_sq(mdev, sq->sqn);
1372 if (sq->rate_limit) {
1373 rl.rate = sq->rate_limit;
1374 mlx5_rl_remove_rate(mdev, &rl);
1376 mlx5e_free_txqsq_descs(sq);
1377 mlx5e_free_txqsq(sq);
1380 static int mlx5e_wait_for_sq_flush(struct mlx5e_txqsq *sq)
1382 unsigned long exp_time = jiffies + msecs_to_jiffies(2000);
1384 while (time_before(jiffies, exp_time)) {
1385 if (sq->cc == sq->pc)
1391 netdev_err(sq->channel->netdev,
1392 "Wait for SQ 0x%x flush timeout (sq cc = 0x%x, sq pc = 0x%x)\n",
1393 sq->sqn, sq->cc, sq->pc);
1398 static int mlx5e_sq_to_ready(struct mlx5e_txqsq *sq, int curr_state)
1400 struct mlx5_core_dev *mdev = sq->channel->mdev;
1401 struct net_device *dev = sq->channel->netdev;
1402 struct mlx5e_modify_sq_param msp = {0};
1405 msp.curr_state = curr_state;
1406 msp.next_state = MLX5_SQC_STATE_RST;
1408 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1410 netdev_err(dev, "Failed to move sq 0x%x to reset\n", sq->sqn);
1414 memset(&msp, 0, sizeof(msp));
1415 msp.curr_state = MLX5_SQC_STATE_RST;
1416 msp.next_state = MLX5_SQC_STATE_RDY;
1418 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1420 netdev_err(dev, "Failed to move sq 0x%x to ready\n", sq->sqn);
1427 static void mlx5e_sq_recover(struct work_struct *work)
1429 struct mlx5e_txqsq_recover *recover =
1430 container_of(work, struct mlx5e_txqsq_recover,
1432 struct mlx5e_txqsq *sq = container_of(recover, struct mlx5e_txqsq,
1434 struct mlx5_core_dev *mdev = sq->channel->mdev;
1435 struct net_device *dev = sq->channel->netdev;
1439 err = mlx5_core_query_sq_state(mdev, sq->sqn, &state);
1441 netdev_err(dev, "Failed to query SQ 0x%x state. err = %d\n",
1446 if (state != MLX5_RQC_STATE_ERR) {
1447 netdev_err(dev, "SQ 0x%x not in ERROR state\n", sq->sqn);
1451 netif_tx_disable_queue(sq->txq);
1453 if (mlx5e_wait_for_sq_flush(sq))
1456 /* If the interval between two consecutive recovers per SQ is too
1457 * short, don't recover to avoid infinite loop of ERR_CQE -> recover.
1458 * If we reached this state, there is probably a bug that needs to be
1459 * fixed. let's keep the queue close and let tx timeout cleanup.
1461 if (jiffies_to_msecs(jiffies - recover->last_recover) <
1462 MLX5E_SQ_RECOVER_MIN_INTERVAL) {
1463 netdev_err(dev, "Recover SQ 0x%x canceled, too many error CQEs\n",
1468 /* At this point, no new packets will arrive from the stack as TXQ is
1469 * marked with QUEUE_STATE_DRV_XOFF. In addition, NAPI cleared all
1470 * pending WQEs. SQ can safely reset the SQ.
1472 if (mlx5e_sq_to_ready(sq, state))
1475 mlx5e_reset_txqsq_cc_pc(sq);
1476 sq->stats->recover++;
1477 recover->last_recover = jiffies;
1478 mlx5e_activate_txqsq(sq);
1481 static int mlx5e_open_icosq(struct mlx5e_channel *c,
1482 struct mlx5e_params *params,
1483 struct mlx5e_sq_param *param,
1484 struct mlx5e_icosq *sq)
1486 struct mlx5e_create_sq_param csp = {};
1489 err = mlx5e_alloc_icosq(c, param, sq);
1493 csp.cqn = sq->cq.mcq.cqn;
1494 csp.wq_ctrl = &sq->wq_ctrl;
1495 csp.min_inline_mode = params->tx_min_inline_mode;
1496 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1497 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1499 goto err_free_icosq;
1504 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1505 mlx5e_free_icosq(sq);
1510 static void mlx5e_close_icosq(struct mlx5e_icosq *sq)
1512 struct mlx5e_channel *c = sq->channel;
1514 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1515 napi_synchronize(&c->napi);
1517 mlx5e_destroy_sq(c->mdev, sq->sqn);
1518 mlx5e_free_icosq(sq);
1521 static int mlx5e_open_xdpsq(struct mlx5e_channel *c,
1522 struct mlx5e_params *params,
1523 struct mlx5e_sq_param *param,
1524 struct mlx5e_xdpsq *sq)
1526 unsigned int ds_cnt = MLX5E_XDP_TX_DS_COUNT;
1527 struct mlx5e_create_sq_param csp = {};
1528 unsigned int inline_hdr_sz = 0;
1532 err = mlx5e_alloc_xdpsq(c, params, param, sq);
1537 csp.tisn = c->priv->tisn[0]; /* tc = 0 */
1538 csp.cqn = sq->cq.mcq.cqn;
1539 csp.wq_ctrl = &sq->wq_ctrl;
1540 csp.min_inline_mode = sq->min_inline_mode;
1541 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1542 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1544 goto err_free_xdpsq;
1546 if (sq->min_inline_mode != MLX5_INLINE_MODE_NONE) {
1547 inline_hdr_sz = MLX5E_XDP_MIN_INLINE;
1551 /* Pre initialize fixed WQE fields */
1552 for (i = 0; i < mlx5_wq_cyc_get_size(&sq->wq); i++) {
1553 struct mlx5e_tx_wqe *wqe = mlx5_wq_cyc_get_wqe(&sq->wq, i);
1554 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
1555 struct mlx5_wqe_eth_seg *eseg = &wqe->eth;
1556 struct mlx5_wqe_data_seg *dseg;
1558 cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
1559 eseg->inline_hdr.sz = cpu_to_be16(inline_hdr_sz);
1561 dseg = (struct mlx5_wqe_data_seg *)cseg + (ds_cnt - 1);
1562 dseg->lkey = sq->mkey_be;
1568 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1569 mlx5e_free_xdpsq(sq);
1574 static void mlx5e_close_xdpsq(struct mlx5e_xdpsq *sq)
1576 struct mlx5e_channel *c = sq->channel;
1578 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1579 napi_synchronize(&c->napi);
1581 mlx5e_destroy_sq(c->mdev, sq->sqn);
1582 mlx5e_free_xdpsq_descs(sq);
1583 mlx5e_free_xdpsq(sq);
1586 static int mlx5e_alloc_cq_common(struct mlx5_core_dev *mdev,
1587 struct mlx5e_cq_param *param,
1588 struct mlx5e_cq *cq)
1590 struct mlx5_core_cq *mcq = &cq->mcq;
1596 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1601 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1604 mcq->set_ci_db = cq->wq_ctrl.db.db;
1605 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1606 *mcq->set_ci_db = 0;
1608 mcq->vector = param->eq_ix;
1609 mcq->comp = mlx5e_completion_event;
1610 mcq->event = mlx5e_cq_error_event;
1613 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1614 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1624 static int mlx5e_alloc_cq(struct mlx5e_channel *c,
1625 struct mlx5e_cq_param *param,
1626 struct mlx5e_cq *cq)
1628 struct mlx5_core_dev *mdev = c->priv->mdev;
1631 param->wq.buf_numa_node = cpu_to_node(c->cpu);
1632 param->wq.db_numa_node = cpu_to_node(c->cpu);
1633 param->eq_ix = c->ix;
1635 err = mlx5e_alloc_cq_common(mdev, param, cq);
1637 cq->napi = &c->napi;
1643 static void mlx5e_free_cq(struct mlx5e_cq *cq)
1645 mlx5_wq_destroy(&cq->wq_ctrl);
1648 static int mlx5e_create_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
1650 struct mlx5_core_dev *mdev = cq->mdev;
1651 struct mlx5_core_cq *mcq = &cq->mcq;
1656 unsigned int irqn_not_used;
1660 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1661 sizeof(u64) * cq->wq_ctrl.buf.npages;
1662 in = kvzalloc(inlen, GFP_KERNEL);
1666 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1668 memcpy(cqc, param->cqc, sizeof(param->cqc));
1670 mlx5_fill_page_frag_array(&cq->wq_ctrl.buf,
1671 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
1673 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
1675 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
1676 MLX5_SET(cqc, cqc, c_eqn, eqn);
1677 MLX5_SET(cqc, cqc, uar_page, mdev->priv.uar->index);
1678 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
1679 MLX5_ADAPTER_PAGE_SHIFT);
1680 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1682 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
1694 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
1696 mlx5_core_destroy_cq(cq->mdev, &cq->mcq);
1699 static int mlx5e_open_cq(struct mlx5e_channel *c,
1700 struct net_dim_cq_moder moder,
1701 struct mlx5e_cq_param *param,
1702 struct mlx5e_cq *cq)
1704 struct mlx5_core_dev *mdev = c->mdev;
1707 err = mlx5e_alloc_cq(c, param, cq);
1711 err = mlx5e_create_cq(cq, param);
1715 if (MLX5_CAP_GEN(mdev, cq_moderation))
1716 mlx5_core_modify_cq_moderation(mdev, &cq->mcq, moder.usec, moder.pkts);
1725 static void mlx5e_close_cq(struct mlx5e_cq *cq)
1727 mlx5e_destroy_cq(cq);
1731 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
1733 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
1736 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1737 struct mlx5e_params *params,
1738 struct mlx5e_channel_param *cparam)
1743 for (tc = 0; tc < c->num_tc; tc++) {
1744 err = mlx5e_open_cq(c, params->tx_cq_moderation,
1745 &cparam->tx_cq, &c->sq[tc].cq);
1747 goto err_close_tx_cqs;
1753 for (tc--; tc >= 0; tc--)
1754 mlx5e_close_cq(&c->sq[tc].cq);
1759 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1763 for (tc = 0; tc < c->num_tc; tc++)
1764 mlx5e_close_cq(&c->sq[tc].cq);
1767 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1768 struct mlx5e_params *params,
1769 struct mlx5e_channel_param *cparam)
1771 struct mlx5e_priv *priv = c->priv;
1772 int err, tc, max_nch = priv->profile->max_nch(priv->mdev);
1774 for (tc = 0; tc < params->num_tc; tc++) {
1775 int txq_ix = c->ix + tc * max_nch;
1777 err = mlx5e_open_txqsq(c, c->priv->tisn[tc], txq_ix,
1778 params, &cparam->sq, &c->sq[tc], tc);
1786 for (tc--; tc >= 0; tc--)
1787 mlx5e_close_txqsq(&c->sq[tc]);
1792 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1796 for (tc = 0; tc < c->num_tc; tc++)
1797 mlx5e_close_txqsq(&c->sq[tc]);
1800 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1801 struct mlx5e_txqsq *sq, u32 rate)
1803 struct mlx5e_priv *priv = netdev_priv(dev);
1804 struct mlx5_core_dev *mdev = priv->mdev;
1805 struct mlx5e_modify_sq_param msp = {0};
1806 struct mlx5_rate_limit rl = {0};
1810 if (rate == sq->rate_limit)
1814 if (sq->rate_limit) {
1815 rl.rate = sq->rate_limit;
1816 /* remove current rl index to free space to next ones */
1817 mlx5_rl_remove_rate(mdev, &rl);
1824 err = mlx5_rl_add_rate(mdev, &rl_index, &rl);
1826 netdev_err(dev, "Failed configuring rate %u: %d\n",
1832 msp.curr_state = MLX5_SQC_STATE_RDY;
1833 msp.next_state = MLX5_SQC_STATE_RDY;
1834 msp.rl_index = rl_index;
1835 msp.rl_update = true;
1836 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1838 netdev_err(dev, "Failed configuring rate %u: %d\n",
1840 /* remove the rate from the table */
1842 mlx5_rl_remove_rate(mdev, &rl);
1846 sq->rate_limit = rate;
1850 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1852 struct mlx5e_priv *priv = netdev_priv(dev);
1853 struct mlx5_core_dev *mdev = priv->mdev;
1854 struct mlx5e_txqsq *sq = priv->txq2sq[index];
1857 if (!mlx5_rl_is_supported(mdev)) {
1858 netdev_err(dev, "Rate limiting is not supported on this device\n");
1862 /* rate is given in Mb/sec, HW config is in Kb/sec */
1865 /* Check whether rate in valid range, 0 is always valid */
1866 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1867 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1871 mutex_lock(&priv->state_lock);
1872 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1873 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1875 priv->tx_rates[index] = rate;
1876 mutex_unlock(&priv->state_lock);
1881 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1882 struct mlx5e_params *params,
1883 struct mlx5e_channel_param *cparam,
1884 struct mlx5e_channel **cp)
1886 struct net_dim_cq_moder icocq_moder = {0, 0};
1887 struct net_device *netdev = priv->netdev;
1888 int cpu = mlx5e_get_cpu(priv, ix);
1889 struct mlx5e_channel *c;
1894 c = kzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1899 c->mdev = priv->mdev;
1900 c->tstamp = &priv->tstamp;
1903 c->pdev = &priv->mdev->pdev->dev;
1904 c->netdev = priv->netdev;
1905 c->mkey_be = cpu_to_be32(priv->mdev->mlx5e_res.mkey.key);
1906 c->num_tc = params->num_tc;
1907 c->xdp = !!params->xdp_prog;
1908 c->stats = &priv->channel_stats[ix].ch;
1910 mlx5_vector2eqn(priv->mdev, ix, &eqn, &irq);
1911 c->irq_desc = irq_to_desc(irq);
1913 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1915 err = mlx5e_open_cq(c, icocq_moder, &cparam->icosq_cq, &c->icosq.cq);
1919 err = mlx5e_open_tx_cqs(c, params, cparam);
1921 goto err_close_icosq_cq;
1923 err = mlx5e_open_cq(c, params->rx_cq_moderation, &cparam->rx_cq, &c->rq.cq);
1925 goto err_close_tx_cqs;
1927 /* XDP SQ CQ params are same as normal TXQ sq CQ params */
1928 err = c->xdp ? mlx5e_open_cq(c, params->tx_cq_moderation,
1929 &cparam->tx_cq, &c->rq.xdpsq.cq) : 0;
1931 goto err_close_rx_cq;
1933 napi_enable(&c->napi);
1935 err = mlx5e_open_icosq(c, params, &cparam->icosq, &c->icosq);
1937 goto err_disable_napi;
1939 err = mlx5e_open_sqs(c, params, cparam);
1941 goto err_close_icosq;
1943 err = c->xdp ? mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, &c->rq.xdpsq) : 0;
1947 err = mlx5e_open_rq(c, params, &cparam->rq, &c->rq);
1949 goto err_close_xdp_sq;
1956 mlx5e_close_xdpsq(&c->rq.xdpsq);
1962 mlx5e_close_icosq(&c->icosq);
1965 napi_disable(&c->napi);
1967 mlx5e_close_cq(&c->rq.xdpsq.cq);
1970 mlx5e_close_cq(&c->rq.cq);
1973 mlx5e_close_tx_cqs(c);
1976 mlx5e_close_cq(&c->icosq.cq);
1979 netif_napi_del(&c->napi);
1985 static void mlx5e_activate_channel(struct mlx5e_channel *c)
1989 for (tc = 0; tc < c->num_tc; tc++)
1990 mlx5e_activate_txqsq(&c->sq[tc]);
1991 mlx5e_activate_rq(&c->rq);
1992 netif_set_xps_queue(c->netdev, get_cpu_mask(c->cpu), c->ix);
1995 static void mlx5e_deactivate_channel(struct mlx5e_channel *c)
1999 mlx5e_deactivate_rq(&c->rq);
2000 for (tc = 0; tc < c->num_tc; tc++)
2001 mlx5e_deactivate_txqsq(&c->sq[tc]);
2004 static void mlx5e_close_channel(struct mlx5e_channel *c)
2006 mlx5e_close_rq(&c->rq);
2008 mlx5e_close_xdpsq(&c->rq.xdpsq);
2010 mlx5e_close_icosq(&c->icosq);
2011 napi_disable(&c->napi);
2013 mlx5e_close_cq(&c->rq.xdpsq.cq);
2014 mlx5e_close_cq(&c->rq.cq);
2015 mlx5e_close_tx_cqs(c);
2016 mlx5e_close_cq(&c->icosq.cq);
2017 netif_napi_del(&c->napi);
2022 #define DEFAULT_FRAG_SIZE (2048)
2024 static void mlx5e_build_rq_frags_info(struct mlx5_core_dev *mdev,
2025 struct mlx5e_params *params,
2026 struct mlx5e_rq_frags_info *info)
2028 u32 byte_count = MLX5E_SW2HW_MTU(params, params->sw_mtu);
2029 int frag_size_max = DEFAULT_FRAG_SIZE;
2033 #ifdef CONFIG_MLX5_EN_IPSEC
2034 if (MLX5_IPSEC_DEV(mdev))
2035 byte_count += MLX5E_METADATA_ETHER_LEN;
2038 if (mlx5e_rx_is_linear_skb(mdev, params)) {
2041 frag_stride = mlx5e_rx_get_linear_frag_sz(params);
2042 frag_stride = roundup_pow_of_two(frag_stride);
2044 info->arr[0].frag_size = byte_count;
2045 info->arr[0].frag_stride = frag_stride;
2046 info->num_frags = 1;
2047 info->wqe_bulk = PAGE_SIZE / frag_stride;
2051 if (byte_count > PAGE_SIZE +
2052 (MLX5E_MAX_RX_FRAGS - 1) * frag_size_max)
2053 frag_size_max = PAGE_SIZE;
2056 while (buf_size < byte_count) {
2057 int frag_size = byte_count - buf_size;
2059 if (i < MLX5E_MAX_RX_FRAGS - 1)
2060 frag_size = min(frag_size, frag_size_max);
2062 info->arr[i].frag_size = frag_size;
2063 info->arr[i].frag_stride = roundup_pow_of_two(frag_size);
2065 buf_size += frag_size;
2068 info->num_frags = i;
2069 /* number of different wqes sharing a page */
2070 info->wqe_bulk = 1 + (info->num_frags % 2);
2073 info->wqe_bulk = max_t(u8, info->wqe_bulk, 8);
2074 info->log_num_frags = order_base_2(info->num_frags);
2077 static inline u8 mlx5e_get_rqwq_log_stride(u8 wq_type, int ndsegs)
2079 int sz = sizeof(struct mlx5_wqe_data_seg) * ndsegs;
2082 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2083 sz += sizeof(struct mlx5e_rx_wqe_ll);
2085 default: /* MLX5_WQ_TYPE_CYCLIC */
2086 sz += sizeof(struct mlx5e_rx_wqe_cyc);
2089 return order_base_2(sz);
2092 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
2093 struct mlx5e_params *params,
2094 struct mlx5e_rq_param *param)
2096 struct mlx5_core_dev *mdev = priv->mdev;
2097 void *rqc = param->rqc;
2098 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2101 switch (params->rq_wq_type) {
2102 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2103 MLX5_SET(wq, wq, log_wqe_num_of_strides,
2104 mlx5e_mpwqe_get_log_num_strides(mdev, params) -
2105 MLX5_MPWQE_LOG_NUM_STRIDES_BASE);
2106 MLX5_SET(wq, wq, log_wqe_stride_size,
2107 mlx5e_mpwqe_get_log_stride_size(mdev, params) -
2108 MLX5_MPWQE_LOG_STRIDE_SZ_BASE);
2109 MLX5_SET(wq, wq, log_wq_sz, mlx5e_mpwqe_get_log_rq_size(params));
2111 default: /* MLX5_WQ_TYPE_CYCLIC */
2112 MLX5_SET(wq, wq, log_wq_sz, params->log_rq_mtu_frames);
2113 mlx5e_build_rq_frags_info(mdev, params, ¶m->frags_info);
2114 ndsegs = param->frags_info.num_frags;
2117 MLX5_SET(wq, wq, wq_type, params->rq_wq_type);
2118 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
2119 MLX5_SET(wq, wq, log_wq_stride,
2120 mlx5e_get_rqwq_log_stride(params->rq_wq_type, ndsegs));
2121 MLX5_SET(wq, wq, pd, mdev->mlx5e_res.pdn);
2122 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
2123 MLX5_SET(rqc, rqc, vsd, params->vlan_strip_disable);
2124 MLX5_SET(rqc, rqc, scatter_fcs, params->scatter_fcs_en);
2126 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
2129 static void mlx5e_build_drop_rq_param(struct mlx5e_priv *priv,
2130 struct mlx5e_rq_param *param)
2132 struct mlx5_core_dev *mdev = priv->mdev;
2133 void *rqc = param->rqc;
2134 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2136 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
2137 MLX5_SET(wq, wq, log_wq_stride,
2138 mlx5e_get_rqwq_log_stride(MLX5_WQ_TYPE_CYCLIC, 1));
2139 MLX5_SET(rqc, rqc, counter_set_id, priv->drop_rq_q_counter);
2141 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
2144 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
2145 struct mlx5e_sq_param *param)
2147 void *sqc = param->sqc;
2148 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2150 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
2151 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
2153 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
2156 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
2157 struct mlx5e_params *params,
2158 struct mlx5e_sq_param *param)
2160 void *sqc = param->sqc;
2161 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2163 mlx5e_build_sq_param_common(priv, param);
2164 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2165 MLX5_SET(sqc, sqc, allow_swp, !!MLX5_IPSEC_DEV(priv->mdev));
2168 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
2169 struct mlx5e_cq_param *param)
2171 void *cqc = param->cqc;
2173 MLX5_SET(cqc, cqc, uar_page, priv->mdev->priv.uar->index);
2176 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
2177 struct mlx5e_params *params,
2178 struct mlx5e_cq_param *param)
2180 struct mlx5_core_dev *mdev = priv->mdev;
2181 void *cqc = param->cqc;
2184 switch (params->rq_wq_type) {
2185 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2186 log_cq_size = mlx5e_mpwqe_get_log_rq_size(params) +
2187 mlx5e_mpwqe_get_log_num_strides(mdev, params);
2189 default: /* MLX5_WQ_TYPE_CYCLIC */
2190 log_cq_size = params->log_rq_mtu_frames;
2193 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
2194 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
2195 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
2196 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
2199 mlx5e_build_common_cq_param(priv, param);
2200 param->cq_period_mode = params->rx_cq_moderation.cq_period_mode;
2203 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
2204 struct mlx5e_params *params,
2205 struct mlx5e_cq_param *param)
2207 void *cqc = param->cqc;
2209 MLX5_SET(cqc, cqc, log_cq_size, params->log_sq_size);
2211 mlx5e_build_common_cq_param(priv, param);
2212 param->cq_period_mode = params->tx_cq_moderation.cq_period_mode;
2215 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
2217 struct mlx5e_cq_param *param)
2219 void *cqc = param->cqc;
2221 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
2223 mlx5e_build_common_cq_param(priv, param);
2225 param->cq_period_mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2228 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
2230 struct mlx5e_sq_param *param)
2232 void *sqc = param->sqc;
2233 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2235 mlx5e_build_sq_param_common(priv, param);
2237 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
2238 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
2241 static void mlx5e_build_xdpsq_param(struct mlx5e_priv *priv,
2242 struct mlx5e_params *params,
2243 struct mlx5e_sq_param *param)
2245 void *sqc = param->sqc;
2246 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2248 mlx5e_build_sq_param_common(priv, param);
2249 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2252 static void mlx5e_build_channel_param(struct mlx5e_priv *priv,
2253 struct mlx5e_params *params,
2254 struct mlx5e_channel_param *cparam)
2256 u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
2258 mlx5e_build_rq_param(priv, params, &cparam->rq);
2259 mlx5e_build_sq_param(priv, params, &cparam->sq);
2260 mlx5e_build_xdpsq_param(priv, params, &cparam->xdp_sq);
2261 mlx5e_build_icosq_param(priv, icosq_log_wq_sz, &cparam->icosq);
2262 mlx5e_build_rx_cq_param(priv, params, &cparam->rx_cq);
2263 mlx5e_build_tx_cq_param(priv, params, &cparam->tx_cq);
2264 mlx5e_build_ico_cq_param(priv, icosq_log_wq_sz, &cparam->icosq_cq);
2267 int mlx5e_open_channels(struct mlx5e_priv *priv,
2268 struct mlx5e_channels *chs)
2270 struct mlx5e_channel_param *cparam;
2274 chs->num = chs->params.num_channels;
2276 chs->c = kcalloc(chs->num, sizeof(struct mlx5e_channel *), GFP_KERNEL);
2277 cparam = kzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
2278 if (!chs->c || !cparam)
2281 mlx5e_build_channel_param(priv, &chs->params, cparam);
2282 for (i = 0; i < chs->num; i++) {
2283 err = mlx5e_open_channel(priv, i, &chs->params, cparam, &chs->c[i]);
2285 goto err_close_channels;
2292 for (i--; i >= 0; i--)
2293 mlx5e_close_channel(chs->c[i]);
2302 static void mlx5e_activate_channels(struct mlx5e_channels *chs)
2306 for (i = 0; i < chs->num; i++)
2307 mlx5e_activate_channel(chs->c[i]);
2310 static int mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels *chs)
2315 for (i = 0; i < chs->num; i++)
2316 err |= mlx5e_wait_for_min_rx_wqes(&chs->c[i]->rq,
2319 return err ? -ETIMEDOUT : 0;
2322 static void mlx5e_deactivate_channels(struct mlx5e_channels *chs)
2326 for (i = 0; i < chs->num; i++)
2327 mlx5e_deactivate_channel(chs->c[i]);
2330 void mlx5e_close_channels(struct mlx5e_channels *chs)
2334 for (i = 0; i < chs->num; i++)
2335 mlx5e_close_channel(chs->c[i]);
2342 mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, struct mlx5e_rqt *rqt)
2344 struct mlx5_core_dev *mdev = priv->mdev;
2351 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
2352 in = kvzalloc(inlen, GFP_KERNEL);
2356 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
2358 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2359 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
2361 for (i = 0; i < sz; i++)
2362 MLX5_SET(rqtc, rqtc, rq_num[i], priv->drop_rq.rqn);
2364 err = mlx5_core_create_rqt(mdev, in, inlen, &rqt->rqtn);
2366 rqt->enabled = true;
2372 void mlx5e_destroy_rqt(struct mlx5e_priv *priv, struct mlx5e_rqt *rqt)
2374 rqt->enabled = false;
2375 mlx5_core_destroy_rqt(priv->mdev, rqt->rqtn);
2378 int mlx5e_create_indirect_rqt(struct mlx5e_priv *priv)
2380 struct mlx5e_rqt *rqt = &priv->indir_rqt;
2383 err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, rqt);
2385 mlx5_core_warn(priv->mdev, "create indirect rqts failed, %d\n", err);
2389 int mlx5e_create_direct_rqts(struct mlx5e_priv *priv)
2391 struct mlx5e_rqt *rqt;
2395 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2396 rqt = &priv->direct_tir[ix].rqt;
2397 err = mlx5e_create_rqt(priv, 1 /*size */, rqt);
2399 goto err_destroy_rqts;
2405 mlx5_core_warn(priv->mdev, "create direct rqts failed, %d\n", err);
2406 for (ix--; ix >= 0; ix--)
2407 mlx5e_destroy_rqt(priv, &priv->direct_tir[ix].rqt);
2412 void mlx5e_destroy_direct_rqts(struct mlx5e_priv *priv)
2416 for (i = 0; i < priv->profile->max_nch(priv->mdev); i++)
2417 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
2420 static int mlx5e_rx_hash_fn(int hfunc)
2422 return (hfunc == ETH_RSS_HASH_TOP) ?
2423 MLX5_RX_HASH_FN_TOEPLITZ :
2424 MLX5_RX_HASH_FN_INVERTED_XOR8;
2427 int mlx5e_bits_invert(unsigned long a, int size)
2432 for (i = 0; i < size; i++)
2433 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
2438 static void mlx5e_fill_rqt_rqns(struct mlx5e_priv *priv, int sz,
2439 struct mlx5e_redirect_rqt_param rrp, void *rqtc)
2443 for (i = 0; i < sz; i++) {
2449 if (rrp.rss.hfunc == ETH_RSS_HASH_XOR)
2450 ix = mlx5e_bits_invert(i, ilog2(sz));
2452 ix = priv->channels.params.indirection_rqt[ix];
2453 rqn = rrp.rss.channels->c[ix]->rq.rqn;
2457 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
2461 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz,
2462 struct mlx5e_redirect_rqt_param rrp)
2464 struct mlx5_core_dev *mdev = priv->mdev;
2470 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
2471 in = kvzalloc(inlen, GFP_KERNEL);
2475 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
2477 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2478 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
2479 mlx5e_fill_rqt_rqns(priv, sz, rrp, rqtc);
2480 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
2486 static u32 mlx5e_get_direct_rqn(struct mlx5e_priv *priv, int ix,
2487 struct mlx5e_redirect_rqt_param rrp)
2492 if (ix >= rrp.rss.channels->num)
2493 return priv->drop_rq.rqn;
2495 return rrp.rss.channels->c[ix]->rq.rqn;
2498 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv,
2499 struct mlx5e_redirect_rqt_param rrp)
2504 if (priv->indir_rqt.enabled) {
2506 rqtn = priv->indir_rqt.rqtn;
2507 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, rrp);
2510 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2511 struct mlx5e_redirect_rqt_param direct_rrp = {
2514 .rqn = mlx5e_get_direct_rqn(priv, ix, rrp)
2518 /* Direct RQ Tables */
2519 if (!priv->direct_tir[ix].rqt.enabled)
2522 rqtn = priv->direct_tir[ix].rqt.rqtn;
2523 mlx5e_redirect_rqt(priv, rqtn, 1, direct_rrp);
2527 static void mlx5e_redirect_rqts_to_channels(struct mlx5e_priv *priv,
2528 struct mlx5e_channels *chs)
2530 struct mlx5e_redirect_rqt_param rrp = {
2535 .hfunc = chs->params.rss_hfunc,
2540 mlx5e_redirect_rqts(priv, rrp);
2543 static void mlx5e_redirect_rqts_to_drop(struct mlx5e_priv *priv)
2545 struct mlx5e_redirect_rqt_param drop_rrp = {
2548 .rqn = priv->drop_rq.rqn,
2552 mlx5e_redirect_rqts(priv, drop_rrp);
2555 static void mlx5e_build_tir_ctx_lro(struct mlx5e_params *params, void *tirc)
2557 if (!params->lro_en)
2560 #define ROUGH_MAX_L2_L3_HDR_SZ 256
2562 MLX5_SET(tirc, tirc, lro_enable_mask,
2563 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2564 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
2565 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
2566 (params->lro_wqe_sz - ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
2567 MLX5_SET(tirc, tirc, lro_timeout_period_usecs, params->lro_timeout);
2570 void mlx5e_build_indir_tir_ctx_hash(struct mlx5e_params *params,
2571 enum mlx5e_traffic_types tt,
2572 void *tirc, bool inner)
2574 void *hfso = inner ? MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner) :
2575 MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2577 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
2578 MLX5_HASH_FIELD_SEL_DST_IP)
2580 #define MLX5_HASH_IP_L4PORTS (MLX5_HASH_FIELD_SEL_SRC_IP |\
2581 MLX5_HASH_FIELD_SEL_DST_IP |\
2582 MLX5_HASH_FIELD_SEL_L4_SPORT |\
2583 MLX5_HASH_FIELD_SEL_L4_DPORT)
2585 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
2586 MLX5_HASH_FIELD_SEL_DST_IP |\
2587 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2589 MLX5_SET(tirc, tirc, rx_hash_fn, mlx5e_rx_hash_fn(params->rss_hfunc));
2590 if (params->rss_hfunc == ETH_RSS_HASH_TOP) {
2591 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
2592 rx_hash_toeplitz_key);
2593 size_t len = MLX5_FLD_SZ_BYTES(tirc,
2594 rx_hash_toeplitz_key);
2596 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
2597 memcpy(rss_key, params->toeplitz_hash_key, len);
2601 case MLX5E_TT_IPV4_TCP:
2602 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2603 MLX5_L3_PROT_TYPE_IPV4);
2604 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2605 MLX5_L4_PROT_TYPE_TCP);
2606 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2607 MLX5_HASH_IP_L4PORTS);
2610 case MLX5E_TT_IPV6_TCP:
2611 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2612 MLX5_L3_PROT_TYPE_IPV6);
2613 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2614 MLX5_L4_PROT_TYPE_TCP);
2615 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2616 MLX5_HASH_IP_L4PORTS);
2619 case MLX5E_TT_IPV4_UDP:
2620 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2621 MLX5_L3_PROT_TYPE_IPV4);
2622 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2623 MLX5_L4_PROT_TYPE_UDP);
2624 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2625 MLX5_HASH_IP_L4PORTS);
2628 case MLX5E_TT_IPV6_UDP:
2629 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2630 MLX5_L3_PROT_TYPE_IPV6);
2631 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2632 MLX5_L4_PROT_TYPE_UDP);
2633 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2634 MLX5_HASH_IP_L4PORTS);
2637 case MLX5E_TT_IPV4_IPSEC_AH:
2638 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2639 MLX5_L3_PROT_TYPE_IPV4);
2640 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2641 MLX5_HASH_IP_IPSEC_SPI);
2644 case MLX5E_TT_IPV6_IPSEC_AH:
2645 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2646 MLX5_L3_PROT_TYPE_IPV6);
2647 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2648 MLX5_HASH_IP_IPSEC_SPI);
2651 case MLX5E_TT_IPV4_IPSEC_ESP:
2652 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2653 MLX5_L3_PROT_TYPE_IPV4);
2654 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2655 MLX5_HASH_IP_IPSEC_SPI);
2658 case MLX5E_TT_IPV6_IPSEC_ESP:
2659 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2660 MLX5_L3_PROT_TYPE_IPV6);
2661 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2662 MLX5_HASH_IP_IPSEC_SPI);
2666 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2667 MLX5_L3_PROT_TYPE_IPV4);
2668 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2673 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2674 MLX5_L3_PROT_TYPE_IPV6);
2675 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2679 WARN_ONCE(true, "%s: bad traffic type!\n", __func__);
2683 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
2685 struct mlx5_core_dev *mdev = priv->mdev;
2694 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
2695 in = kvzalloc(inlen, GFP_KERNEL);
2699 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
2700 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2702 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2704 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2705 err = mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in,
2711 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2712 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
2724 static void mlx5e_build_inner_indir_tir_ctx(struct mlx5e_priv *priv,
2725 enum mlx5e_traffic_types tt,
2728 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2730 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2732 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2733 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
2734 MLX5_SET(tirc, tirc, tunneled_offload_en, 0x1);
2736 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, true);
2739 static int mlx5e_set_mtu(struct mlx5_core_dev *mdev,
2740 struct mlx5e_params *params, u16 mtu)
2742 u16 hw_mtu = MLX5E_SW2HW_MTU(params, mtu);
2745 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
2749 /* Update vport context MTU */
2750 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
2754 static void mlx5e_query_mtu(struct mlx5_core_dev *mdev,
2755 struct mlx5e_params *params, u16 *mtu)
2760 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
2761 if (err || !hw_mtu) /* fallback to port oper mtu */
2762 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
2764 *mtu = MLX5E_HW2SW_MTU(params, hw_mtu);
2767 static int mlx5e_set_dev_port_mtu(struct mlx5e_priv *priv)
2769 struct mlx5e_params *params = &priv->channels.params;
2770 struct net_device *netdev = priv->netdev;
2771 struct mlx5_core_dev *mdev = priv->mdev;
2775 err = mlx5e_set_mtu(mdev, params, params->sw_mtu);
2779 mlx5e_query_mtu(mdev, params, &mtu);
2780 if (mtu != params->sw_mtu)
2781 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
2782 __func__, mtu, params->sw_mtu);
2784 params->sw_mtu = mtu;
2788 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
2790 struct mlx5e_priv *priv = netdev_priv(netdev);
2791 int nch = priv->channels.params.num_channels;
2792 int ntc = priv->channels.params.num_tc;
2795 netdev_reset_tc(netdev);
2800 netdev_set_num_tc(netdev, ntc);
2802 /* Map netdev TCs to offset 0
2803 * We have our own UP to TXQ mapping for QoS
2805 for (tc = 0; tc < ntc; tc++)
2806 netdev_set_tc_queue(netdev, tc, nch, 0);
2809 static void mlx5e_build_tc2txq_maps(struct mlx5e_priv *priv)
2811 int max_nch = priv->profile->max_nch(priv->mdev);
2814 for (i = 0; i < max_nch; i++)
2815 for (tc = 0; tc < priv->profile->max_tc; tc++)
2816 priv->channel_tc2txq[i][tc] = i + tc * max_nch;
2819 static void mlx5e_build_tx2sq_maps(struct mlx5e_priv *priv)
2821 struct mlx5e_channel *c;
2822 struct mlx5e_txqsq *sq;
2825 for (i = 0; i < priv->channels.num; i++) {
2826 c = priv->channels.c[i];
2827 for (tc = 0; tc < c->num_tc; tc++) {
2829 priv->txq2sq[sq->txq_ix] = sq;
2834 void mlx5e_activate_priv_channels(struct mlx5e_priv *priv)
2836 int num_txqs = priv->channels.num * priv->channels.params.num_tc;
2837 struct net_device *netdev = priv->netdev;
2839 mlx5e_netdev_set_tcs(netdev);
2840 netif_set_real_num_tx_queues(netdev, num_txqs);
2841 netif_set_real_num_rx_queues(netdev, priv->channels.num);
2843 mlx5e_build_tx2sq_maps(priv);
2844 mlx5e_activate_channels(&priv->channels);
2845 netif_tx_start_all_queues(priv->netdev);
2847 if (MLX5_VPORT_MANAGER(priv->mdev))
2848 mlx5e_add_sqs_fwd_rules(priv);
2850 mlx5e_wait_channels_min_rx_wqes(&priv->channels);
2851 mlx5e_redirect_rqts_to_channels(priv, &priv->channels);
2854 void mlx5e_deactivate_priv_channels(struct mlx5e_priv *priv)
2856 mlx5e_redirect_rqts_to_drop(priv);
2858 if (MLX5_VPORT_MANAGER(priv->mdev))
2859 mlx5e_remove_sqs_fwd_rules(priv);
2861 /* FIXME: This is a W/A only for tx timeout watch dog false alarm when
2862 * polling for inactive tx queues.
2864 netif_tx_stop_all_queues(priv->netdev);
2865 netif_tx_disable(priv->netdev);
2866 mlx5e_deactivate_channels(&priv->channels);
2869 void mlx5e_switch_priv_channels(struct mlx5e_priv *priv,
2870 struct mlx5e_channels *new_chs,
2871 mlx5e_fp_hw_modify hw_modify)
2873 struct net_device *netdev = priv->netdev;
2876 new_num_txqs = new_chs->num * new_chs->params.num_tc;
2878 carrier_ok = netif_carrier_ok(netdev);
2879 netif_carrier_off(netdev);
2881 if (new_num_txqs < netdev->real_num_tx_queues)
2882 netif_set_real_num_tx_queues(netdev, new_num_txqs);
2884 mlx5e_deactivate_priv_channels(priv);
2885 mlx5e_close_channels(&priv->channels);
2887 priv->channels = *new_chs;
2889 /* New channels are ready to roll, modify HW settings if needed */
2893 mlx5e_refresh_tirs(priv, false);
2894 mlx5e_activate_priv_channels(priv);
2896 /* return carrier back if needed */
2898 netif_carrier_on(netdev);
2901 void mlx5e_timestamp_init(struct mlx5e_priv *priv)
2903 priv->tstamp.tx_type = HWTSTAMP_TX_OFF;
2904 priv->tstamp.rx_filter = HWTSTAMP_FILTER_NONE;
2907 int mlx5e_open_locked(struct net_device *netdev)
2909 struct mlx5e_priv *priv = netdev_priv(netdev);
2912 set_bit(MLX5E_STATE_OPENED, &priv->state);
2914 err = mlx5e_open_channels(priv, &priv->channels);
2916 goto err_clear_state_opened_flag;
2918 mlx5e_refresh_tirs(priv, false);
2919 mlx5e_activate_priv_channels(priv);
2920 if (priv->profile->update_carrier)
2921 priv->profile->update_carrier(priv);
2923 if (priv->profile->update_stats)
2924 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
2928 err_clear_state_opened_flag:
2929 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2933 int mlx5e_open(struct net_device *netdev)
2935 struct mlx5e_priv *priv = netdev_priv(netdev);
2938 mutex_lock(&priv->state_lock);
2939 err = mlx5e_open_locked(netdev);
2941 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_UP);
2942 mutex_unlock(&priv->state_lock);
2944 if (mlx5e_vxlan_allowed(priv->mdev))
2945 udp_tunnel_get_rx_info(netdev);
2950 int mlx5e_close_locked(struct net_device *netdev)
2952 struct mlx5e_priv *priv = netdev_priv(netdev);
2954 /* May already be CLOSED in case a previous configuration operation
2955 * (e.g RX/TX queue size change) that involves close&open failed.
2957 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2960 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2962 netif_carrier_off(priv->netdev);
2963 mlx5e_deactivate_priv_channels(priv);
2964 mlx5e_close_channels(&priv->channels);
2969 int mlx5e_close(struct net_device *netdev)
2971 struct mlx5e_priv *priv = netdev_priv(netdev);
2974 if (!netif_device_present(netdev))
2977 mutex_lock(&priv->state_lock);
2978 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_DOWN);
2979 err = mlx5e_close_locked(netdev);
2980 mutex_unlock(&priv->state_lock);
2985 static int mlx5e_alloc_drop_rq(struct mlx5_core_dev *mdev,
2986 struct mlx5e_rq *rq,
2987 struct mlx5e_rq_param *param)
2989 void *rqc = param->rqc;
2990 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
2993 param->wq.db_numa_node = param->wq.buf_numa_node;
2995 err = mlx5_wq_cyc_create(mdev, ¶m->wq, rqc_wq, &rq->wqe.wq,
3000 /* Mark as unused given "Drop-RQ" packets never reach XDP */
3001 xdp_rxq_info_unused(&rq->xdp_rxq);
3008 static int mlx5e_alloc_drop_cq(struct mlx5_core_dev *mdev,
3009 struct mlx5e_cq *cq,
3010 struct mlx5e_cq_param *param)
3012 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
3013 param->wq.db_numa_node = dev_to_node(&mdev->pdev->dev);
3015 return mlx5e_alloc_cq_common(mdev, param, cq);
3018 static int mlx5e_open_drop_rq(struct mlx5e_priv *priv,
3019 struct mlx5e_rq *drop_rq)
3021 struct mlx5_core_dev *mdev = priv->mdev;
3022 struct mlx5e_cq_param cq_param = {};
3023 struct mlx5e_rq_param rq_param = {};
3024 struct mlx5e_cq *cq = &drop_rq->cq;
3027 mlx5e_build_drop_rq_param(priv, &rq_param);
3029 err = mlx5e_alloc_drop_cq(mdev, cq, &cq_param);
3033 err = mlx5e_create_cq(cq, &cq_param);
3037 err = mlx5e_alloc_drop_rq(mdev, drop_rq, &rq_param);
3039 goto err_destroy_cq;
3041 err = mlx5e_create_rq(drop_rq, &rq_param);
3045 err = mlx5e_modify_rq_state(drop_rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
3047 mlx5_core_warn(priv->mdev, "modify_rq_state failed, rx_if_down_packets won't be counted %d\n", err);
3052 mlx5e_free_rq(drop_rq);
3055 mlx5e_destroy_cq(cq);
3063 static void mlx5e_close_drop_rq(struct mlx5e_rq *drop_rq)
3065 mlx5e_destroy_rq(drop_rq);
3066 mlx5e_free_rq(drop_rq);
3067 mlx5e_destroy_cq(&drop_rq->cq);
3068 mlx5e_free_cq(&drop_rq->cq);
3071 int mlx5e_create_tis(struct mlx5_core_dev *mdev, int tc,
3072 u32 underlay_qpn, u32 *tisn)
3074 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
3075 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
3077 MLX5_SET(tisc, tisc, prio, tc << 1);
3078 MLX5_SET(tisc, tisc, underlay_qpn, underlay_qpn);
3079 MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.td.tdn);
3081 if (mlx5_lag_is_lacp_owner(mdev))
3082 MLX5_SET(tisc, tisc, strict_lag_tx_port_affinity, 1);
3084 return mlx5_core_create_tis(mdev, in, sizeof(in), tisn);
3087 void mlx5e_destroy_tis(struct mlx5_core_dev *mdev, u32 tisn)
3089 mlx5_core_destroy_tis(mdev, tisn);
3092 int mlx5e_create_tises(struct mlx5e_priv *priv)
3097 for (tc = 0; tc < priv->profile->max_tc; tc++) {
3098 err = mlx5e_create_tis(priv->mdev, tc, 0, &priv->tisn[tc]);
3100 goto err_close_tises;
3106 for (tc--; tc >= 0; tc--)
3107 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
3112 void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
3116 for (tc = 0; tc < priv->profile->max_tc; tc++)
3117 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
3120 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv,
3121 enum mlx5e_traffic_types tt,
3124 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
3126 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
3128 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
3129 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
3130 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, false);
3133 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 rqtn, u32 *tirc)
3135 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
3137 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
3139 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
3140 MLX5_SET(tirc, tirc, indirect_table, rqtn);
3141 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
3144 int mlx5e_create_indirect_tirs(struct mlx5e_priv *priv)
3146 struct mlx5e_tir *tir;
3154 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3155 in = kvzalloc(inlen, GFP_KERNEL);
3159 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
3160 memset(in, 0, inlen);
3161 tir = &priv->indir_tir[tt];
3162 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3163 mlx5e_build_indir_tir_ctx(priv, tt, tirc);
3164 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3166 mlx5_core_warn(priv->mdev, "create indirect tirs failed, %d\n", err);
3167 goto err_destroy_inner_tirs;
3171 if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
3174 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
3175 memset(in, 0, inlen);
3176 tir = &priv->inner_indir_tir[i];
3177 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3178 mlx5e_build_inner_indir_tir_ctx(priv, i, tirc);
3179 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3181 mlx5_core_warn(priv->mdev, "create inner indirect tirs failed, %d\n", err);
3182 goto err_destroy_inner_tirs;
3191 err_destroy_inner_tirs:
3192 for (i--; i >= 0; i--)
3193 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3195 for (tt--; tt >= 0; tt--)
3196 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[tt]);
3203 int mlx5e_create_direct_tirs(struct mlx5e_priv *priv)
3205 int nch = priv->profile->max_nch(priv->mdev);
3206 struct mlx5e_tir *tir;
3213 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3214 in = kvzalloc(inlen, GFP_KERNEL);
3218 for (ix = 0; ix < nch; ix++) {
3219 memset(in, 0, inlen);
3220 tir = &priv->direct_tir[ix];
3221 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3222 mlx5e_build_direct_tir_ctx(priv, priv->direct_tir[ix].rqt.rqtn, tirc);
3223 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3225 goto err_destroy_ch_tirs;
3232 err_destroy_ch_tirs:
3233 mlx5_core_warn(priv->mdev, "create direct tirs failed, %d\n", err);
3234 for (ix--; ix >= 0; ix--)
3235 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[ix]);
3242 void mlx5e_destroy_indirect_tirs(struct mlx5e_priv *priv)
3246 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3247 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[i]);
3249 if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
3252 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3253 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3256 void mlx5e_destroy_direct_tirs(struct mlx5e_priv *priv)
3258 int nch = priv->profile->max_nch(priv->mdev);
3261 for (i = 0; i < nch; i++)
3262 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[i]);
3265 static int mlx5e_modify_channels_scatter_fcs(struct mlx5e_channels *chs, bool enable)
3270 for (i = 0; i < chs->num; i++) {
3271 err = mlx5e_modify_rq_scatter_fcs(&chs->c[i]->rq, enable);
3279 static int mlx5e_modify_channels_vsd(struct mlx5e_channels *chs, bool vsd)
3284 for (i = 0; i < chs->num; i++) {
3285 err = mlx5e_modify_rq_vsd(&chs->c[i]->rq, vsd);
3293 static int mlx5e_setup_tc_mqprio(struct net_device *netdev,
3294 struct tc_mqprio_qopt *mqprio)
3296 struct mlx5e_priv *priv = netdev_priv(netdev);
3297 struct mlx5e_channels new_channels = {};
3298 u8 tc = mqprio->num_tc;
3301 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
3303 if (tc && tc != MLX5E_MAX_NUM_TC)
3306 mutex_lock(&priv->state_lock);
3308 new_channels.params = priv->channels.params;
3309 new_channels.params.num_tc = tc ? tc : 1;
3311 if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
3312 priv->channels.params = new_channels.params;
3316 err = mlx5e_open_channels(priv, &new_channels);
3320 priv->max_opened_tc = max_t(u8, priv->max_opened_tc,
3321 new_channels.params.num_tc);
3322 mlx5e_switch_priv_channels(priv, &new_channels, NULL);
3324 mutex_unlock(&priv->state_lock);
3328 #ifdef CONFIG_MLX5_ESWITCH
3329 static int mlx5e_setup_tc_cls_flower(struct mlx5e_priv *priv,
3330 struct tc_cls_flower_offload *cls_flower,
3333 switch (cls_flower->command) {
3334 case TC_CLSFLOWER_REPLACE:
3335 return mlx5e_configure_flower(priv, cls_flower, flags);
3336 case TC_CLSFLOWER_DESTROY:
3337 return mlx5e_delete_flower(priv, cls_flower, flags);
3338 case TC_CLSFLOWER_STATS:
3339 return mlx5e_stats_flower(priv, cls_flower, flags);
3345 static int mlx5e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
3348 struct mlx5e_priv *priv = cb_priv;
3350 if (!tc_cls_can_offload_and_chain0(priv->netdev, type_data))
3354 case TC_SETUP_CLSFLOWER:
3355 return mlx5e_setup_tc_cls_flower(priv, type_data, MLX5E_TC_INGRESS);
3361 static int mlx5e_setup_tc_block(struct net_device *dev,
3362 struct tc_block_offload *f)
3364 struct mlx5e_priv *priv = netdev_priv(dev);
3366 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
3369 switch (f->command) {
3371 return tcf_block_cb_register(f->block, mlx5e_setup_tc_block_cb,
3373 case TC_BLOCK_UNBIND:
3374 tcf_block_cb_unregister(f->block, mlx5e_setup_tc_block_cb,
3383 static int mlx5e_setup_tc(struct net_device *dev, enum tc_setup_type type,
3387 #ifdef CONFIG_MLX5_ESWITCH
3388 case TC_SETUP_BLOCK:
3389 return mlx5e_setup_tc_block(dev, type_data);
3391 case TC_SETUP_QDISC_MQPRIO:
3392 return mlx5e_setup_tc_mqprio(dev, type_data);
3399 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
3401 struct mlx5e_priv *priv = netdev_priv(dev);
3402 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
3403 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
3404 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
3406 if (mlx5e_is_uplink_rep(priv)) {
3407 stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
3408 stats->rx_bytes = PPORT_802_3_GET(pstats, a_octets_received_ok);
3409 stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
3410 stats->tx_bytes = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
3412 mlx5e_grp_sw_update_stats(priv);
3413 stats->rx_packets = sstats->rx_packets;
3414 stats->rx_bytes = sstats->rx_bytes;
3415 stats->tx_packets = sstats->tx_packets;
3416 stats->tx_bytes = sstats->tx_bytes;
3417 stats->tx_dropped = sstats->tx_queue_dropped;
3420 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
3422 stats->rx_length_errors =
3423 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
3424 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
3425 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
3426 stats->rx_crc_errors =
3427 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
3428 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
3429 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
3430 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
3431 stats->rx_frame_errors;
3432 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
3434 /* vport multicast also counts packets that are dropped due to steering
3435 * or rx out of buffer
3438 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
3441 static void mlx5e_set_rx_mode(struct net_device *dev)
3443 struct mlx5e_priv *priv = netdev_priv(dev);
3445 queue_work(priv->wq, &priv->set_rx_mode_work);
3448 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
3450 struct mlx5e_priv *priv = netdev_priv(netdev);
3451 struct sockaddr *saddr = addr;
3453 if (!is_valid_ether_addr(saddr->sa_data))
3454 return -EADDRNOTAVAIL;
3456 netif_addr_lock_bh(netdev);
3457 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
3458 netif_addr_unlock_bh(netdev);
3460 queue_work(priv->wq, &priv->set_rx_mode_work);
3465 #define MLX5E_SET_FEATURE(features, feature, enable) \
3468 *features |= feature; \
3470 *features &= ~feature; \
3473 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
3475 static int set_feature_lro(struct net_device *netdev, bool enable)
3477 struct mlx5e_priv *priv = netdev_priv(netdev);
3478 struct mlx5_core_dev *mdev = priv->mdev;
3479 struct mlx5e_channels new_channels = {};
3480 struct mlx5e_params *old_params;
3484 mutex_lock(&priv->state_lock);
3486 old_params = &priv->channels.params;
3487 if (enable && !MLX5E_GET_PFLAG(old_params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
3488 netdev_warn(netdev, "can't set LRO with legacy RQ\n");
3493 reset = test_bit(MLX5E_STATE_OPENED, &priv->state);
3495 new_channels.params = *old_params;
3496 new_channels.params.lro_en = enable;
3498 if (old_params->rq_wq_type != MLX5_WQ_TYPE_CYCLIC) {
3499 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, old_params) ==
3500 mlx5e_rx_mpwqe_is_linear_skb(mdev, &new_channels.params))
3505 *old_params = new_channels.params;
3506 err = mlx5e_modify_tirs_lro(priv);
3510 err = mlx5e_open_channels(priv, &new_channels);
3514 mlx5e_switch_priv_channels(priv, &new_channels, mlx5e_modify_tirs_lro);
3516 mutex_unlock(&priv->state_lock);
3520 static int set_feature_cvlan_filter(struct net_device *netdev, bool enable)
3522 struct mlx5e_priv *priv = netdev_priv(netdev);
3525 mlx5e_enable_cvlan_filter(priv);
3527 mlx5e_disable_cvlan_filter(priv);
3532 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
3534 struct mlx5e_priv *priv = netdev_priv(netdev);
3536 if (!enable && mlx5e_tc_num_filters(priv)) {
3538 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
3545 static int set_feature_rx_all(struct net_device *netdev, bool enable)
3547 struct mlx5e_priv *priv = netdev_priv(netdev);
3548 struct mlx5_core_dev *mdev = priv->mdev;
3550 return mlx5_set_port_fcs(mdev, !enable);
3553 static int set_feature_rx_fcs(struct net_device *netdev, bool enable)
3555 struct mlx5e_priv *priv = netdev_priv(netdev);
3558 mutex_lock(&priv->state_lock);
3560 priv->channels.params.scatter_fcs_en = enable;
3561 err = mlx5e_modify_channels_scatter_fcs(&priv->channels, enable);
3563 priv->channels.params.scatter_fcs_en = !enable;
3565 mutex_unlock(&priv->state_lock);
3570 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
3572 struct mlx5e_priv *priv = netdev_priv(netdev);
3575 mutex_lock(&priv->state_lock);
3577 priv->channels.params.vlan_strip_disable = !enable;
3578 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3581 err = mlx5e_modify_channels_vsd(&priv->channels, !enable);
3583 priv->channels.params.vlan_strip_disable = enable;
3586 mutex_unlock(&priv->state_lock);
3591 #ifdef CONFIG_RFS_ACCEL
3592 static int set_feature_arfs(struct net_device *netdev, bool enable)
3594 struct mlx5e_priv *priv = netdev_priv(netdev);
3598 err = mlx5e_arfs_enable(priv);
3600 err = mlx5e_arfs_disable(priv);
3606 static int mlx5e_handle_feature(struct net_device *netdev,
3607 netdev_features_t *features,
3608 netdev_features_t wanted_features,
3609 netdev_features_t feature,
3610 mlx5e_feature_handler feature_handler)
3612 netdev_features_t changes = wanted_features ^ netdev->features;
3613 bool enable = !!(wanted_features & feature);
3616 if (!(changes & feature))
3619 err = feature_handler(netdev, enable);
3621 netdev_err(netdev, "%s feature %pNF failed, err %d\n",
3622 enable ? "Enable" : "Disable", &feature, err);
3626 MLX5E_SET_FEATURE(features, feature, enable);
3630 static int mlx5e_set_features(struct net_device *netdev,
3631 netdev_features_t features)
3633 netdev_features_t oper_features = netdev->features;
3636 #define MLX5E_HANDLE_FEATURE(feature, handler) \
3637 mlx5e_handle_feature(netdev, &oper_features, features, feature, handler)
3639 err |= MLX5E_HANDLE_FEATURE(NETIF_F_LRO, set_feature_lro);
3640 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_FILTER,
3641 set_feature_cvlan_filter);
3642 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_TC, set_feature_tc_num_filters);
3643 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXALL, set_feature_rx_all);
3644 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXFCS, set_feature_rx_fcs);
3645 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_RX, set_feature_rx_vlan);
3646 #ifdef CONFIG_RFS_ACCEL
3647 err |= MLX5E_HANDLE_FEATURE(NETIF_F_NTUPLE, set_feature_arfs);
3651 netdev->features = oper_features;
3658 static netdev_features_t mlx5e_fix_features(struct net_device *netdev,
3659 netdev_features_t features)
3661 struct mlx5e_priv *priv = netdev_priv(netdev);
3662 struct mlx5e_params *params;
3664 mutex_lock(&priv->state_lock);
3665 params = &priv->channels.params;
3666 if (!bitmap_empty(priv->fs.vlan.active_svlans, VLAN_N_VID)) {
3667 /* HW strips the outer C-tag header, this is a problem
3668 * for S-tag traffic.
3670 features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3671 if (!params->vlan_strip_disable)
3672 netdev_warn(netdev, "Dropping C-tag vlan stripping offload due to S-tag vlan\n");
3674 if (!MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
3675 features &= ~NETIF_F_LRO;
3677 netdev_warn(netdev, "Disabling LRO, not supported in legacy RQ\n");
3680 mutex_unlock(&priv->state_lock);
3685 int mlx5e_change_mtu(struct net_device *netdev, int new_mtu,
3686 change_hw_mtu_cb set_mtu_cb)
3688 struct mlx5e_priv *priv = netdev_priv(netdev);
3689 struct mlx5e_channels new_channels = {};
3690 struct mlx5e_params *params;
3694 mutex_lock(&priv->state_lock);
3696 params = &priv->channels.params;
3698 reset = !params->lro_en;
3699 reset = reset && test_bit(MLX5E_STATE_OPENED, &priv->state);
3701 new_channels.params = *params;
3702 new_channels.params.sw_mtu = new_mtu;
3704 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
3705 u8 ppw_old = mlx5e_mpwqe_log_pkts_per_wqe(params);
3706 u8 ppw_new = mlx5e_mpwqe_log_pkts_per_wqe(&new_channels.params);
3708 reset = reset && (ppw_old != ppw_new);
3712 params->sw_mtu = new_mtu;
3714 netdev->mtu = params->sw_mtu;
3718 err = mlx5e_open_channels(priv, &new_channels);
3722 mlx5e_switch_priv_channels(priv, &new_channels, set_mtu_cb);
3723 netdev->mtu = new_channels.params.sw_mtu;
3726 mutex_unlock(&priv->state_lock);
3730 static int mlx5e_change_nic_mtu(struct net_device *netdev, int new_mtu)
3732 return mlx5e_change_mtu(netdev, new_mtu, mlx5e_set_dev_port_mtu);
3735 int mlx5e_hwstamp_set(struct mlx5e_priv *priv, struct ifreq *ifr)
3737 struct hwtstamp_config config;
3740 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
3743 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
3746 /* TX HW timestamp */
3747 switch (config.tx_type) {
3748 case HWTSTAMP_TX_OFF:
3749 case HWTSTAMP_TX_ON:
3755 mutex_lock(&priv->state_lock);
3756 /* RX HW timestamp */
3757 switch (config.rx_filter) {
3758 case HWTSTAMP_FILTER_NONE:
3759 /* Reset CQE compression to Admin default */
3760 mlx5e_modify_rx_cqe_compression_locked(priv, priv->channels.params.rx_cqe_compress_def);
3762 case HWTSTAMP_FILTER_ALL:
3763 case HWTSTAMP_FILTER_SOME:
3764 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3765 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3766 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3767 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3768 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3769 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3770 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3771 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3772 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3773 case HWTSTAMP_FILTER_PTP_V2_EVENT:
3774 case HWTSTAMP_FILTER_PTP_V2_SYNC:
3775 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3776 case HWTSTAMP_FILTER_NTP_ALL:
3777 /* Disable CQE compression */
3778 netdev_warn(priv->netdev, "Disabling cqe compression");
3779 err = mlx5e_modify_rx_cqe_compression_locked(priv, false);
3781 netdev_err(priv->netdev, "Failed disabling cqe compression err=%d\n", err);
3782 mutex_unlock(&priv->state_lock);
3785 config.rx_filter = HWTSTAMP_FILTER_ALL;
3788 mutex_unlock(&priv->state_lock);
3792 memcpy(&priv->tstamp, &config, sizeof(config));
3793 mutex_unlock(&priv->state_lock);
3795 return copy_to_user(ifr->ifr_data, &config,
3796 sizeof(config)) ? -EFAULT : 0;
3799 int mlx5e_hwstamp_get(struct mlx5e_priv *priv, struct ifreq *ifr)
3801 struct hwtstamp_config *cfg = &priv->tstamp;
3803 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
3806 return copy_to_user(ifr->ifr_data, cfg, sizeof(*cfg)) ? -EFAULT : 0;
3809 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3811 struct mlx5e_priv *priv = netdev_priv(dev);
3815 return mlx5e_hwstamp_set(priv, ifr);
3817 return mlx5e_hwstamp_get(priv, ifr);
3823 #ifdef CONFIG_MLX5_ESWITCH
3824 static int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
3826 struct mlx5e_priv *priv = netdev_priv(dev);
3827 struct mlx5_core_dev *mdev = priv->mdev;
3829 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
3832 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
3835 struct mlx5e_priv *priv = netdev_priv(dev);
3836 struct mlx5_core_dev *mdev = priv->mdev;
3838 if (vlan_proto != htons(ETH_P_8021Q))
3839 return -EPROTONOSUPPORT;
3841 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
3845 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
3847 struct mlx5e_priv *priv = netdev_priv(dev);
3848 struct mlx5_core_dev *mdev = priv->mdev;
3850 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
3853 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
3855 struct mlx5e_priv *priv = netdev_priv(dev);
3856 struct mlx5_core_dev *mdev = priv->mdev;
3858 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
3861 static int mlx5e_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
3864 struct mlx5e_priv *priv = netdev_priv(dev);
3865 struct mlx5_core_dev *mdev = priv->mdev;
3867 return mlx5_eswitch_set_vport_rate(mdev->priv.eswitch, vf + 1,
3868 max_tx_rate, min_tx_rate);
3871 static int mlx5_vport_link2ifla(u8 esw_link)
3874 case MLX5_ESW_VPORT_ADMIN_STATE_DOWN:
3875 return IFLA_VF_LINK_STATE_DISABLE;
3876 case MLX5_ESW_VPORT_ADMIN_STATE_UP:
3877 return IFLA_VF_LINK_STATE_ENABLE;
3879 return IFLA_VF_LINK_STATE_AUTO;
3882 static int mlx5_ifla_link2vport(u8 ifla_link)
3884 switch (ifla_link) {
3885 case IFLA_VF_LINK_STATE_DISABLE:
3886 return MLX5_ESW_VPORT_ADMIN_STATE_DOWN;
3887 case IFLA_VF_LINK_STATE_ENABLE:
3888 return MLX5_ESW_VPORT_ADMIN_STATE_UP;
3890 return MLX5_ESW_VPORT_ADMIN_STATE_AUTO;
3893 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
3896 struct mlx5e_priv *priv = netdev_priv(dev);
3897 struct mlx5_core_dev *mdev = priv->mdev;
3899 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
3900 mlx5_ifla_link2vport(link_state));
3903 static int mlx5e_get_vf_config(struct net_device *dev,
3904 int vf, struct ifla_vf_info *ivi)
3906 struct mlx5e_priv *priv = netdev_priv(dev);
3907 struct mlx5_core_dev *mdev = priv->mdev;
3910 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
3913 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
3917 static int mlx5e_get_vf_stats(struct net_device *dev,
3918 int vf, struct ifla_vf_stats *vf_stats)
3920 struct mlx5e_priv *priv = netdev_priv(dev);
3921 struct mlx5_core_dev *mdev = priv->mdev;
3923 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
3928 static void mlx5e_add_vxlan_port(struct net_device *netdev,
3929 struct udp_tunnel_info *ti)
3931 struct mlx5e_priv *priv = netdev_priv(netdev);
3933 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3936 if (!mlx5e_vxlan_allowed(priv->mdev))
3939 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 1);
3942 static void mlx5e_del_vxlan_port(struct net_device *netdev,
3943 struct udp_tunnel_info *ti)
3945 struct mlx5e_priv *priv = netdev_priv(netdev);
3947 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3950 if (!mlx5e_vxlan_allowed(priv->mdev))
3953 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 0);
3956 static netdev_features_t mlx5e_tunnel_features_check(struct mlx5e_priv *priv,
3957 struct sk_buff *skb,
3958 netdev_features_t features)
3960 unsigned int offset = 0;
3961 struct udphdr *udph;
3965 switch (vlan_get_protocol(skb)) {
3966 case htons(ETH_P_IP):
3967 proto = ip_hdr(skb)->protocol;
3969 case htons(ETH_P_IPV6):
3970 proto = ipv6_find_hdr(skb, &offset, -1, NULL, NULL);
3980 udph = udp_hdr(skb);
3981 port = be16_to_cpu(udph->dest);
3983 /* Verify if UDP port is being offloaded by HW */
3984 if (mlx5e_vxlan_lookup_port(priv, port))
3989 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
3990 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3993 static netdev_features_t mlx5e_features_check(struct sk_buff *skb,
3994 struct net_device *netdev,
3995 netdev_features_t features)
3997 struct mlx5e_priv *priv = netdev_priv(netdev);
3999 features = vlan_features_check(skb, features);
4000 features = vxlan_features_check(skb, features);
4002 #ifdef CONFIG_MLX5_EN_IPSEC
4003 if (mlx5e_ipsec_feature_check(skb, netdev, features))
4007 /* Validate if the tunneled packet is being offloaded by HW */
4008 if (skb->encapsulation &&
4009 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
4010 return mlx5e_tunnel_features_check(priv, skb, features);
4015 static bool mlx5e_tx_timeout_eq_recover(struct net_device *dev,
4016 struct mlx5e_txqsq *sq)
4018 struct mlx5_eq *eq = sq->cq.mcq.eq;
4021 netdev_err(dev, "EQ 0x%x: Cons = 0x%x, irqn = 0x%x\n",
4022 eq->eqn, eq->cons_index, eq->irqn);
4024 eqe_count = mlx5_eq_poll_irq_disabled(eq);
4028 netdev_err(dev, "Recover %d eqes on EQ 0x%x\n", eqe_count, eq->eqn);
4029 sq->channel->stats->eq_rearm++;
4033 static void mlx5e_tx_timeout_work(struct work_struct *work)
4035 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
4037 struct net_device *dev = priv->netdev;
4038 bool reopen_channels = false;
4042 mutex_lock(&priv->state_lock);
4044 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
4047 for (i = 0; i < priv->channels.num * priv->channels.params.num_tc; i++) {
4048 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, i);
4049 struct mlx5e_txqsq *sq = priv->txq2sq[i];
4051 if (!netif_xmit_stopped(dev_queue))
4055 "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",
4056 i, sq->sqn, sq->cq.mcq.cqn, sq->cc, sq->pc,
4057 jiffies_to_usecs(jiffies - dev_queue->trans_start));
4059 /* If we recover a lost interrupt, most likely TX timeout will
4060 * be resolved, skip reopening channels
4062 if (!mlx5e_tx_timeout_eq_recover(dev, sq)) {
4063 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
4064 reopen_channels = true;
4068 if (!reopen_channels)
4071 mlx5e_close_locked(dev);
4072 err = mlx5e_open_locked(dev);
4074 netdev_err(priv->netdev,
4075 "mlx5e_open_locked failed recovering from a tx_timeout, err(%d).\n",
4079 mutex_unlock(&priv->state_lock);
4083 static void mlx5e_tx_timeout(struct net_device *dev)
4085 struct mlx5e_priv *priv = netdev_priv(dev);
4087 netdev_err(dev, "TX timeout detected\n");
4088 queue_work(priv->wq, &priv->tx_timeout_work);
4091 static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog)
4093 struct mlx5e_priv *priv = netdev_priv(netdev);
4094 struct bpf_prog *old_prog;
4096 bool reset, was_opened;
4099 mutex_lock(&priv->state_lock);
4101 if ((netdev->features & NETIF_F_LRO) && prog) {
4102 netdev_warn(netdev, "can't set XDP while LRO is on, disable LRO first\n");
4107 if ((netdev->features & NETIF_F_HW_ESP) && prog) {
4108 netdev_warn(netdev, "can't set XDP with IPSec offload\n");
4113 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
4114 /* no need for full reset when exchanging programs */
4115 reset = (!priv->channels.params.xdp_prog || !prog);
4117 if (was_opened && reset)
4118 mlx5e_close_locked(netdev);
4119 if (was_opened && !reset) {
4120 /* num_channels is invariant here, so we can take the
4121 * batched reference right upfront.
4123 prog = bpf_prog_add(prog, priv->channels.num);
4125 err = PTR_ERR(prog);
4130 /* exchange programs, extra prog reference we got from caller
4131 * as long as we don't fail from this point onwards.
4133 old_prog = xchg(&priv->channels.params.xdp_prog, prog);
4135 bpf_prog_put(old_prog);
4137 if (reset) /* change RQ type according to priv->xdp_prog */
4138 mlx5e_set_rq_type(priv->mdev, &priv->channels.params);
4140 if (was_opened && reset)
4141 mlx5e_open_locked(netdev);
4143 if (!test_bit(MLX5E_STATE_OPENED, &priv->state) || reset)
4146 /* exchanging programs w/o reset, we update ref counts on behalf
4147 * of the channels RQs here.
4149 for (i = 0; i < priv->channels.num; i++) {
4150 struct mlx5e_channel *c = priv->channels.c[i];
4152 clear_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
4153 napi_synchronize(&c->napi);
4154 /* prevent mlx5e_poll_rx_cq from accessing rq->xdp_prog */
4156 old_prog = xchg(&c->rq.xdp_prog, prog);
4158 set_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
4159 /* napi_schedule in case we have missed anything */
4160 napi_schedule(&c->napi);
4163 bpf_prog_put(old_prog);
4167 mutex_unlock(&priv->state_lock);
4171 static u32 mlx5e_xdp_query(struct net_device *dev)
4173 struct mlx5e_priv *priv = netdev_priv(dev);
4174 const struct bpf_prog *xdp_prog;
4177 mutex_lock(&priv->state_lock);
4178 xdp_prog = priv->channels.params.xdp_prog;
4180 prog_id = xdp_prog->aux->id;
4181 mutex_unlock(&priv->state_lock);
4186 static int mlx5e_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4188 switch (xdp->command) {
4189 case XDP_SETUP_PROG:
4190 return mlx5e_xdp_set(dev, xdp->prog);
4191 case XDP_QUERY_PROG:
4192 xdp->prog_id = mlx5e_xdp_query(dev);
4193 xdp->prog_attached = !!xdp->prog_id;
4200 #ifdef CONFIG_NET_POLL_CONTROLLER
4201 /* Fake "interrupt" called by netpoll (eg netconsole) to send skbs without
4202 * reenabling interrupts.
4204 static void mlx5e_netpoll(struct net_device *dev)
4206 struct mlx5e_priv *priv = netdev_priv(dev);
4207 struct mlx5e_channels *chs = &priv->channels;
4211 for (i = 0; i < chs->num; i++)
4212 napi_schedule(&chs->c[i]->napi);
4216 static const struct net_device_ops mlx5e_netdev_ops = {
4217 .ndo_open = mlx5e_open,
4218 .ndo_stop = mlx5e_close,
4219 .ndo_start_xmit = mlx5e_xmit,
4220 .ndo_setup_tc = mlx5e_setup_tc,
4221 .ndo_select_queue = mlx5e_select_queue,
4222 .ndo_get_stats64 = mlx5e_get_stats,
4223 .ndo_set_rx_mode = mlx5e_set_rx_mode,
4224 .ndo_set_mac_address = mlx5e_set_mac,
4225 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
4226 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
4227 .ndo_set_features = mlx5e_set_features,
4228 .ndo_fix_features = mlx5e_fix_features,
4229 .ndo_change_mtu = mlx5e_change_nic_mtu,
4230 .ndo_do_ioctl = mlx5e_ioctl,
4231 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
4232 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
4233 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
4234 .ndo_features_check = mlx5e_features_check,
4235 #ifdef CONFIG_RFS_ACCEL
4236 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
4238 .ndo_tx_timeout = mlx5e_tx_timeout,
4239 .ndo_bpf = mlx5e_xdp,
4240 #ifdef CONFIG_NET_POLL_CONTROLLER
4241 .ndo_poll_controller = mlx5e_netpoll,
4243 #ifdef CONFIG_MLX5_ESWITCH
4244 /* SRIOV E-Switch NDOs */
4245 .ndo_set_vf_mac = mlx5e_set_vf_mac,
4246 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
4247 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
4248 .ndo_set_vf_trust = mlx5e_set_vf_trust,
4249 .ndo_set_vf_rate = mlx5e_set_vf_rate,
4250 .ndo_get_vf_config = mlx5e_get_vf_config,
4251 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
4252 .ndo_get_vf_stats = mlx5e_get_vf_stats,
4253 .ndo_has_offload_stats = mlx5e_has_offload_stats,
4254 .ndo_get_offload_stats = mlx5e_get_offload_stats,
4258 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
4260 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
4262 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
4263 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
4264 !MLX5_CAP_ETH(mdev, csum_cap) ||
4265 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
4266 !MLX5_CAP_ETH(mdev, vlan_cap) ||
4267 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
4268 MLX5_CAP_FLOWTABLE(mdev,
4269 flow_table_properties_nic_receive.max_ft_level)
4271 mlx5_core_warn(mdev,
4272 "Not creating net device, some required device capabilities are missing\n");
4275 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
4276 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
4277 if (!MLX5_CAP_GEN(mdev, cq_moderation))
4278 mlx5_core_warn(mdev, "CQ moderation is not supported\n");
4283 void mlx5e_build_default_indir_rqt(u32 *indirection_rqt, int len,
4288 for (i = 0; i < len; i++)
4289 indirection_rqt[i] = i % num_channels;
4292 static bool slow_pci_heuristic(struct mlx5_core_dev *mdev)
4297 mlx5e_port_max_linkspeed(mdev, &link_speed);
4298 pci_bw = pcie_bandwidth_available(mdev->pdev, NULL, NULL, NULL);
4299 mlx5_core_dbg_once(mdev, "Max link speed = %d, PCI BW = %d\n",
4300 link_speed, pci_bw);
4302 #define MLX5E_SLOW_PCI_RATIO (2)
4304 return link_speed && pci_bw &&
4305 link_speed > MLX5E_SLOW_PCI_RATIO * pci_bw;
4308 static struct net_dim_cq_moder mlx5e_get_def_tx_moderation(u8 cq_period_mode)
4310 struct net_dim_cq_moder moder;
4312 moder.cq_period_mode = cq_period_mode;
4313 moder.pkts = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
4314 moder.usec = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
4315 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4316 moder.usec = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC_FROM_CQE;
4321 static struct net_dim_cq_moder mlx5e_get_def_rx_moderation(u8 cq_period_mode)
4323 struct net_dim_cq_moder moder;
4325 moder.cq_period_mode = cq_period_mode;
4326 moder.pkts = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
4327 moder.usec = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
4328 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4329 moder.usec = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
4334 static u8 mlx5_to_net_dim_cq_period_mode(u8 cq_period_mode)
4336 return cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE ?
4337 NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE :
4338 NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
4341 void mlx5e_set_tx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4343 if (params->tx_dim_enabled) {
4344 u8 dim_period_mode = mlx5_to_net_dim_cq_period_mode(cq_period_mode);
4346 params->tx_cq_moderation = net_dim_get_def_tx_moderation(dim_period_mode);
4348 params->tx_cq_moderation = mlx5e_get_def_tx_moderation(cq_period_mode);
4351 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_TX_CQE_BASED_MODER,
4352 params->tx_cq_moderation.cq_period_mode ==
4353 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4356 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4358 if (params->rx_dim_enabled) {
4359 u8 dim_period_mode = mlx5_to_net_dim_cq_period_mode(cq_period_mode);
4361 params->rx_cq_moderation = net_dim_get_def_rx_moderation(dim_period_mode);
4363 params->rx_cq_moderation = mlx5e_get_def_rx_moderation(cq_period_mode);
4366 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_BASED_MODER,
4367 params->rx_cq_moderation.cq_period_mode ==
4368 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4371 static u32 mlx5e_choose_lro_timeout(struct mlx5_core_dev *mdev, u32 wanted_timeout)
4375 /* The supported periods are organized in ascending order */
4376 for (i = 0; i < MLX5E_LRO_TIMEOUT_ARR_SIZE - 1; i++)
4377 if (MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]) >= wanted_timeout)
4380 return MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]);
4383 void mlx5e_build_nic_params(struct mlx5_core_dev *mdev,
4384 struct mlx5e_params *params,
4385 u16 max_channels, u16 mtu)
4387 u8 rx_cq_period_mode;
4389 params->sw_mtu = mtu;
4390 params->hard_mtu = MLX5E_ETH_HARD_MTU;
4391 params->num_channels = max_channels;
4395 params->log_sq_size = is_kdump_kernel() ?
4396 MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE :
4397 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
4399 /* set CQE compression */
4400 params->rx_cqe_compress_def = false;
4401 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
4402 MLX5_CAP_GEN(mdev, vport_group_manager))
4403 params->rx_cqe_compress_def = slow_pci_heuristic(mdev);
4405 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS, params->rx_cqe_compress_def);
4408 /* Prefer Striding RQ, unless any of the following holds:
4409 * - Striding RQ configuration is not possible/supported.
4410 * - Slow PCI heuristic.
4411 * - Legacy RQ would use linear SKB while Striding RQ would use non-linear.
4413 if (!slow_pci_heuristic(mdev) &&
4414 mlx5e_striding_rq_possible(mdev, params) &&
4415 (mlx5e_rx_mpwqe_is_linear_skb(mdev, params) ||
4416 !mlx5e_rx_is_linear_skb(mdev, params)))
4417 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ, true);
4418 mlx5e_set_rq_type(mdev, params);
4419 mlx5e_init_rq_type_params(mdev, params);
4423 /* TODO: && MLX5_CAP_ETH(mdev, lro_cap) */
4424 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
4425 if (!mlx5e_rx_mpwqe_is_linear_skb(mdev, params))
4426 params->lro_en = !slow_pci_heuristic(mdev);
4427 params->lro_timeout = mlx5e_choose_lro_timeout(mdev, MLX5E_DEFAULT_LRO_TIMEOUT);
4429 /* CQ moderation params */
4430 rx_cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
4431 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
4432 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
4433 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4434 params->tx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4435 mlx5e_set_rx_cq_mode_params(params, rx_cq_period_mode);
4436 mlx5e_set_tx_cq_mode_params(params, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
4439 params->tx_min_inline_mode = mlx5e_params_calculate_tx_min_inline(mdev);
4442 params->rss_hfunc = ETH_RSS_HASH_XOR;
4443 netdev_rss_key_fill(params->toeplitz_hash_key, sizeof(params->toeplitz_hash_key));
4444 mlx5e_build_default_indir_rqt(params->indirection_rqt,
4445 MLX5E_INDIR_RQT_SIZE, max_channels);
4448 static void mlx5e_build_nic_netdev_priv(struct mlx5_core_dev *mdev,
4449 struct net_device *netdev,
4450 const struct mlx5e_profile *profile,
4453 struct mlx5e_priv *priv = netdev_priv(netdev);
4456 priv->netdev = netdev;
4457 priv->profile = profile;
4458 priv->ppriv = ppriv;
4459 priv->msglevel = MLX5E_MSG_LEVEL;
4460 priv->max_opened_tc = 1;
4462 mlx5e_build_nic_params(mdev, &priv->channels.params,
4463 profile->max_nch(mdev), netdev->mtu);
4465 mutex_init(&priv->state_lock);
4467 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
4468 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
4469 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
4470 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
4472 mlx5e_timestamp_init(priv);
4475 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
4477 struct mlx5e_priv *priv = netdev_priv(netdev);
4479 mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
4480 if (is_zero_ether_addr(netdev->dev_addr) &&
4481 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
4482 eth_hw_addr_random(netdev);
4483 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
4487 #if IS_ENABLED(CONFIG_MLX5_ESWITCH)
4488 static const struct switchdev_ops mlx5e_switchdev_ops = {
4489 .switchdev_port_attr_get = mlx5e_attr_get,
4493 static void mlx5e_build_nic_netdev(struct net_device *netdev)
4495 struct mlx5e_priv *priv = netdev_priv(netdev);
4496 struct mlx5_core_dev *mdev = priv->mdev;
4500 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
4502 netdev->netdev_ops = &mlx5e_netdev_ops;
4504 #ifdef CONFIG_MLX5_CORE_EN_DCB
4505 if (MLX5_CAP_GEN(mdev, vport_group_manager) && MLX5_CAP_GEN(mdev, qos))
4506 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
4509 netdev->watchdog_timeo = 15 * HZ;
4511 netdev->ethtool_ops = &mlx5e_ethtool_ops;
4513 netdev->vlan_features |= NETIF_F_SG;
4514 netdev->vlan_features |= NETIF_F_IP_CSUM;
4515 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
4516 netdev->vlan_features |= NETIF_F_GRO;
4517 netdev->vlan_features |= NETIF_F_TSO;
4518 netdev->vlan_features |= NETIF_F_TSO6;
4519 netdev->vlan_features |= NETIF_F_RXCSUM;
4520 netdev->vlan_features |= NETIF_F_RXHASH;
4522 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_TX;
4523 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_RX;
4525 if (!!MLX5_CAP_ETH(mdev, lro_cap) &&
4526 mlx5e_check_fragmented_striding_rq_cap(mdev))
4527 netdev->vlan_features |= NETIF_F_LRO;
4529 netdev->hw_features = netdev->vlan_features;
4530 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4531 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4532 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4533 netdev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
4535 if (mlx5e_vxlan_allowed(mdev) || MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4536 netdev->hw_features |= NETIF_F_GSO_PARTIAL;
4537 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
4538 netdev->hw_enc_features |= NETIF_F_IPV6_CSUM;
4539 netdev->hw_enc_features |= NETIF_F_TSO;
4540 netdev->hw_enc_features |= NETIF_F_TSO6;
4541 netdev->hw_enc_features |= NETIF_F_GSO_PARTIAL;
4544 if (mlx5e_vxlan_allowed(mdev)) {
4545 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
4546 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4547 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
4548 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4549 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
4552 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4553 netdev->hw_features |= NETIF_F_GSO_GRE |
4554 NETIF_F_GSO_GRE_CSUM;
4555 netdev->hw_enc_features |= NETIF_F_GSO_GRE |
4556 NETIF_F_GSO_GRE_CSUM;
4557 netdev->gso_partial_features |= NETIF_F_GSO_GRE |
4558 NETIF_F_GSO_GRE_CSUM;
4561 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
4564 netdev->hw_features |= NETIF_F_RXALL;
4566 if (MLX5_CAP_ETH(mdev, scatter_fcs))
4567 netdev->hw_features |= NETIF_F_RXFCS;
4569 netdev->features = netdev->hw_features;
4570 if (!priv->channels.params.lro_en)
4571 netdev->features &= ~NETIF_F_LRO;
4574 netdev->features &= ~NETIF_F_RXALL;
4576 if (!priv->channels.params.scatter_fcs_en)
4577 netdev->features &= ~NETIF_F_RXFCS;
4579 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
4580 if (FT_CAP(flow_modify_en) &&
4581 FT_CAP(modify_root) &&
4582 FT_CAP(identified_miss_table_mode) &&
4583 FT_CAP(flow_table_modify)) {
4584 netdev->hw_features |= NETIF_F_HW_TC;
4585 #ifdef CONFIG_RFS_ACCEL
4586 netdev->hw_features |= NETIF_F_NTUPLE;
4590 netdev->features |= NETIF_F_HIGHDMA;
4591 netdev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
4593 netdev->priv_flags |= IFF_UNICAST_FLT;
4595 mlx5e_set_netdev_dev_addr(netdev);
4597 #if IS_ENABLED(CONFIG_MLX5_ESWITCH)
4598 if (MLX5_VPORT_MANAGER(mdev))
4599 netdev->switchdev_ops = &mlx5e_switchdev_ops;
4602 mlx5e_ipsec_build_netdev(priv);
4603 mlx5e_tls_build_netdev(priv);
4606 static void mlx5e_create_q_counters(struct mlx5e_priv *priv)
4608 struct mlx5_core_dev *mdev = priv->mdev;
4611 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
4613 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
4614 priv->q_counter = 0;
4617 err = mlx5_core_alloc_q_counter(mdev, &priv->drop_rq_q_counter);
4619 mlx5_core_warn(mdev, "alloc drop RQ counter failed, %d\n", err);
4620 priv->drop_rq_q_counter = 0;
4624 static void mlx5e_destroy_q_counters(struct mlx5e_priv *priv)
4626 if (priv->q_counter)
4627 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
4629 if (priv->drop_rq_q_counter)
4630 mlx5_core_dealloc_q_counter(priv->mdev, priv->drop_rq_q_counter);
4633 static void mlx5e_nic_init(struct mlx5_core_dev *mdev,
4634 struct net_device *netdev,
4635 const struct mlx5e_profile *profile,
4638 struct mlx5e_priv *priv = netdev_priv(netdev);
4641 mlx5e_build_nic_netdev_priv(mdev, netdev, profile, ppriv);
4642 err = mlx5e_ipsec_init(priv);
4644 mlx5_core_err(mdev, "IPSec initialization failed, %d\n", err);
4645 err = mlx5e_tls_init(priv);
4647 mlx5_core_err(mdev, "TLS initialization failed, %d\n", err);
4648 mlx5e_build_nic_netdev(netdev);
4649 mlx5e_build_tc2txq_maps(priv);
4650 mlx5e_vxlan_init(priv);
4653 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
4655 mlx5e_tls_cleanup(priv);
4656 mlx5e_ipsec_cleanup(priv);
4657 mlx5e_vxlan_cleanup(priv);
4660 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv)
4662 struct mlx5_core_dev *mdev = priv->mdev;
4665 err = mlx5e_create_indirect_rqt(priv);
4669 err = mlx5e_create_direct_rqts(priv);
4671 goto err_destroy_indirect_rqts;
4673 err = mlx5e_create_indirect_tirs(priv);
4675 goto err_destroy_direct_rqts;
4677 err = mlx5e_create_direct_tirs(priv);
4679 goto err_destroy_indirect_tirs;
4681 err = mlx5e_create_flow_steering(priv);
4683 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
4684 goto err_destroy_direct_tirs;
4687 err = mlx5e_tc_nic_init(priv);
4689 goto err_destroy_flow_steering;
4693 err_destroy_flow_steering:
4694 mlx5e_destroy_flow_steering(priv);
4695 err_destroy_direct_tirs:
4696 mlx5e_destroy_direct_tirs(priv);
4697 err_destroy_indirect_tirs:
4698 mlx5e_destroy_indirect_tirs(priv);
4699 err_destroy_direct_rqts:
4700 mlx5e_destroy_direct_rqts(priv);
4701 err_destroy_indirect_rqts:
4702 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
4706 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv)
4708 mlx5e_tc_nic_cleanup(priv);
4709 mlx5e_destroy_flow_steering(priv);
4710 mlx5e_destroy_direct_tirs(priv);
4711 mlx5e_destroy_indirect_tirs(priv);
4712 mlx5e_destroy_direct_rqts(priv);
4713 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
4716 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv)
4720 err = mlx5e_create_tises(priv);
4722 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
4726 #ifdef CONFIG_MLX5_CORE_EN_DCB
4727 mlx5e_dcbnl_initialize(priv);
4732 static void mlx5e_nic_enable(struct mlx5e_priv *priv)
4734 struct net_device *netdev = priv->netdev;
4735 struct mlx5_core_dev *mdev = priv->mdev;
4738 mlx5e_init_l2_addr(priv);
4740 /* Marking the link as currently not needed by the Driver */
4741 if (!netif_running(netdev))
4742 mlx5_set_port_admin_status(mdev, MLX5_PORT_DOWN);
4744 /* MTU range: 68 - hw-specific max */
4745 netdev->min_mtu = ETH_MIN_MTU;
4746 mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
4747 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
4748 mlx5e_set_dev_port_mtu(priv);
4750 mlx5_lag_add(mdev, netdev);
4752 mlx5e_enable_async_events(priv);
4754 if (MLX5_VPORT_MANAGER(priv->mdev))
4755 mlx5e_register_vport_reps(priv);
4757 if (netdev->reg_state != NETREG_REGISTERED)
4759 #ifdef CONFIG_MLX5_CORE_EN_DCB
4760 mlx5e_dcbnl_init_app(priv);
4763 queue_work(priv->wq, &priv->set_rx_mode_work);
4766 if (netif_running(netdev))
4768 netif_device_attach(netdev);
4772 static void mlx5e_nic_disable(struct mlx5e_priv *priv)
4774 struct mlx5_core_dev *mdev = priv->mdev;
4776 #ifdef CONFIG_MLX5_CORE_EN_DCB
4777 if (priv->netdev->reg_state == NETREG_REGISTERED)
4778 mlx5e_dcbnl_delete_app(priv);
4782 if (netif_running(priv->netdev))
4783 mlx5e_close(priv->netdev);
4784 netif_device_detach(priv->netdev);
4787 queue_work(priv->wq, &priv->set_rx_mode_work);
4789 if (MLX5_VPORT_MANAGER(priv->mdev))
4790 mlx5e_unregister_vport_reps(priv);
4792 mlx5e_disable_async_events(priv);
4793 mlx5_lag_remove(mdev);
4796 static const struct mlx5e_profile mlx5e_nic_profile = {
4797 .init = mlx5e_nic_init,
4798 .cleanup = mlx5e_nic_cleanup,
4799 .init_rx = mlx5e_init_nic_rx,
4800 .cleanup_rx = mlx5e_cleanup_nic_rx,
4801 .init_tx = mlx5e_init_nic_tx,
4802 .cleanup_tx = mlx5e_cleanup_nic_tx,
4803 .enable = mlx5e_nic_enable,
4804 .disable = mlx5e_nic_disable,
4805 .update_stats = mlx5e_update_ndo_stats,
4806 .max_nch = mlx5e_get_max_num_channels,
4807 .update_carrier = mlx5e_update_carrier,
4808 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe,
4809 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
4810 .max_tc = MLX5E_MAX_NUM_TC,
4813 /* mlx5e generic netdev management API (move to en_common.c) */
4815 struct net_device *mlx5e_create_netdev(struct mlx5_core_dev *mdev,
4816 const struct mlx5e_profile *profile,
4819 int nch = profile->max_nch(mdev);
4820 struct net_device *netdev;
4821 struct mlx5e_priv *priv;
4823 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
4824 nch * profile->max_tc,
4827 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
4831 #ifdef CONFIG_RFS_ACCEL
4832 netdev->rx_cpu_rmap = mdev->rmap;
4835 profile->init(mdev, netdev, profile, ppriv);
4837 netif_carrier_off(netdev);
4839 priv = netdev_priv(netdev);
4841 priv->wq = create_singlethread_workqueue("mlx5e");
4843 goto err_cleanup_nic;
4848 if (profile->cleanup)
4849 profile->cleanup(priv);
4850 free_netdev(netdev);
4855 int mlx5e_attach_netdev(struct mlx5e_priv *priv)
4857 struct mlx5_core_dev *mdev = priv->mdev;
4858 const struct mlx5e_profile *profile;
4861 profile = priv->profile;
4862 clear_bit(MLX5E_STATE_DESTROYING, &priv->state);
4864 err = profile->init_tx(priv);
4868 mlx5e_create_q_counters(priv);
4870 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
4872 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
4873 goto err_destroy_q_counters;
4876 err = profile->init_rx(priv);
4878 goto err_close_drop_rq;
4880 if (profile->enable)
4881 profile->enable(priv);
4886 mlx5e_close_drop_rq(&priv->drop_rq);
4888 err_destroy_q_counters:
4889 mlx5e_destroy_q_counters(priv);
4890 profile->cleanup_tx(priv);
4896 void mlx5e_detach_netdev(struct mlx5e_priv *priv)
4898 const struct mlx5e_profile *profile = priv->profile;
4900 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
4902 if (profile->disable)
4903 profile->disable(priv);
4904 flush_workqueue(priv->wq);
4906 profile->cleanup_rx(priv);
4907 mlx5e_close_drop_rq(&priv->drop_rq);
4908 mlx5e_destroy_q_counters(priv);
4909 profile->cleanup_tx(priv);
4910 cancel_delayed_work_sync(&priv->update_stats_work);
4913 void mlx5e_destroy_netdev(struct mlx5e_priv *priv)
4915 const struct mlx5e_profile *profile = priv->profile;
4916 struct net_device *netdev = priv->netdev;
4918 destroy_workqueue(priv->wq);
4919 if (profile->cleanup)
4920 profile->cleanup(priv);
4921 free_netdev(netdev);
4924 /* mlx5e_attach and mlx5e_detach scope should be only creating/destroying
4925 * hardware contexts and to connect it to the current netdev.
4927 static int mlx5e_attach(struct mlx5_core_dev *mdev, void *vpriv)
4929 struct mlx5e_priv *priv = vpriv;
4930 struct net_device *netdev = priv->netdev;
4933 if (netif_device_present(netdev))
4936 err = mlx5e_create_mdev_resources(mdev);
4940 err = mlx5e_attach_netdev(priv);
4942 mlx5e_destroy_mdev_resources(mdev);
4949 static void mlx5e_detach(struct mlx5_core_dev *mdev, void *vpriv)
4951 struct mlx5e_priv *priv = vpriv;
4952 struct net_device *netdev = priv->netdev;
4954 if (!netif_device_present(netdev))
4957 mlx5e_detach_netdev(priv);
4958 mlx5e_destroy_mdev_resources(mdev);
4961 static void *mlx5e_add(struct mlx5_core_dev *mdev)
4963 struct net_device *netdev;
4968 err = mlx5e_check_required_hca_cap(mdev);
4972 #ifdef CONFIG_MLX5_ESWITCH
4973 if (MLX5_VPORT_MANAGER(mdev)) {
4974 rpriv = mlx5e_alloc_nic_rep_priv(mdev);
4976 mlx5_core_warn(mdev, "Failed to alloc NIC rep priv data\n");
4982 netdev = mlx5e_create_netdev(mdev, &mlx5e_nic_profile, rpriv);
4984 mlx5_core_err(mdev, "mlx5e_create_netdev failed\n");
4985 goto err_free_rpriv;
4988 priv = netdev_priv(netdev);
4990 err = mlx5e_attach(mdev, priv);
4992 mlx5_core_err(mdev, "mlx5e_attach failed, %d\n", err);
4993 goto err_destroy_netdev;
4996 err = register_netdev(netdev);
4998 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
5002 #ifdef CONFIG_MLX5_CORE_EN_DCB
5003 mlx5e_dcbnl_init_app(priv);
5008 mlx5e_detach(mdev, priv);
5010 mlx5e_destroy_netdev(priv);
5016 static void mlx5e_remove(struct mlx5_core_dev *mdev, void *vpriv)
5018 struct mlx5e_priv *priv = vpriv;
5019 void *ppriv = priv->ppriv;
5021 #ifdef CONFIG_MLX5_CORE_EN_DCB
5022 mlx5e_dcbnl_delete_app(priv);
5024 unregister_netdev(priv->netdev);
5025 mlx5e_detach(mdev, vpriv);
5026 mlx5e_destroy_netdev(priv);
5030 static void *mlx5e_get_netdev(void *vpriv)
5032 struct mlx5e_priv *priv = vpriv;
5034 return priv->netdev;
5037 static struct mlx5_interface mlx5e_interface = {
5039 .remove = mlx5e_remove,
5040 .attach = mlx5e_attach,
5041 .detach = mlx5e_detach,
5042 .event = mlx5e_async_event,
5043 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
5044 .get_dev = mlx5e_get_netdev,
5047 void mlx5e_init(void)
5049 mlx5e_ipsec_build_inverse_table();
5050 mlx5e_build_ptys2ethtool_map();
5051 mlx5_register_interface(&mlx5e_interface);
5054 void mlx5e_cleanup(void)
5056 mlx5_unregister_interface(&mlx5e_interface);