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"
52 struct mlx5e_rq_param {
53 u32 rqc[MLX5_ST_SZ_DW(rqc)];
54 struct mlx5_wq_param wq;
55 struct mlx5e_rq_frags_info frags_info;
58 struct mlx5e_sq_param {
59 u32 sqc[MLX5_ST_SZ_DW(sqc)];
60 struct mlx5_wq_param wq;
63 struct mlx5e_cq_param {
64 u32 cqc[MLX5_ST_SZ_DW(cqc)];
65 struct mlx5_wq_param wq;
70 struct mlx5e_channel_param {
71 struct mlx5e_rq_param rq;
72 struct mlx5e_sq_param sq;
73 struct mlx5e_sq_param xdp_sq;
74 struct mlx5e_sq_param icosq;
75 struct mlx5e_cq_param rx_cq;
76 struct mlx5e_cq_param tx_cq;
77 struct mlx5e_cq_param icosq_cq;
80 bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
82 bool striding_rq_umr = MLX5_CAP_GEN(mdev, striding_rq) &&
83 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
84 MLX5_CAP_ETH(mdev, reg_umr_sq);
85 u16 max_wqe_sz_cap = MLX5_CAP_GEN(mdev, max_wqe_sz_sq);
86 bool inline_umr = MLX5E_UMR_WQE_INLINE_SZ <= max_wqe_sz_cap;
91 mlx5_core_warn(mdev, "Cannot support Striding RQ: UMR WQE size (%d) exceeds maximum supported (%d).\n",
92 (int)MLX5E_UMR_WQE_INLINE_SZ, max_wqe_sz_cap);
98 static u32 mlx5e_rx_get_linear_frag_sz(struct mlx5e_params *params)
100 u16 hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
101 u16 linear_rq_headroom = params->xdp_prog ?
102 XDP_PACKET_HEADROOM : MLX5_RX_HEADROOM;
105 linear_rq_headroom += NET_IP_ALIGN;
107 frag_sz = MLX5_SKB_FRAG_SZ(linear_rq_headroom + hw_mtu);
109 if (params->xdp_prog && frag_sz < PAGE_SIZE)
115 static u8 mlx5e_mpwqe_log_pkts_per_wqe(struct mlx5e_params *params)
117 u32 linear_frag_sz = mlx5e_rx_get_linear_frag_sz(params);
119 return MLX5_MPWRQ_LOG_WQE_SZ - order_base_2(linear_frag_sz);
122 static bool mlx5e_rx_is_linear_skb(struct mlx5_core_dev *mdev,
123 struct mlx5e_params *params)
125 u32 frag_sz = mlx5e_rx_get_linear_frag_sz(params);
127 return !params->lro_en && frag_sz <= PAGE_SIZE;
130 static bool mlx5e_rx_mpwqe_is_linear_skb(struct mlx5_core_dev *mdev,
131 struct mlx5e_params *params)
133 u32 frag_sz = mlx5e_rx_get_linear_frag_sz(params);
134 s8 signed_log_num_strides_param;
137 if (!mlx5e_rx_is_linear_skb(mdev, params))
140 if (MLX5_CAP_GEN(mdev, ext_stride_num_range))
143 log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ - order_base_2(frag_sz);
144 signed_log_num_strides_param =
145 (s8)log_num_strides - MLX5_MPWQE_LOG_NUM_STRIDES_BASE;
147 return signed_log_num_strides_param >= 0;
150 static u8 mlx5e_mpwqe_get_log_rq_size(struct mlx5e_params *params)
152 if (params->log_rq_mtu_frames <
153 mlx5e_mpwqe_log_pkts_per_wqe(params) + MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW)
154 return MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW;
156 return params->log_rq_mtu_frames - mlx5e_mpwqe_log_pkts_per_wqe(params);
159 static u8 mlx5e_mpwqe_get_log_stride_size(struct mlx5_core_dev *mdev,
160 struct mlx5e_params *params)
162 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, params))
163 return order_base_2(mlx5e_rx_get_linear_frag_sz(params));
165 return MLX5E_MPWQE_STRIDE_SZ(mdev,
166 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
169 static u8 mlx5e_mpwqe_get_log_num_strides(struct mlx5_core_dev *mdev,
170 struct mlx5e_params *params)
172 return MLX5_MPWRQ_LOG_WQE_SZ -
173 mlx5e_mpwqe_get_log_stride_size(mdev, params);
176 static u16 mlx5e_get_rq_headroom(struct mlx5_core_dev *mdev,
177 struct mlx5e_params *params)
179 u16 linear_rq_headroom = params->xdp_prog ?
180 XDP_PACKET_HEADROOM : MLX5_RX_HEADROOM;
183 linear_rq_headroom += NET_IP_ALIGN;
185 is_linear_skb = (params->rq_wq_type == MLX5_WQ_TYPE_CYCLIC) ?
186 mlx5e_rx_is_linear_skb(mdev, params) :
187 mlx5e_rx_mpwqe_is_linear_skb(mdev, params);
189 return is_linear_skb ? linear_rq_headroom : 0;
192 void mlx5e_init_rq_type_params(struct mlx5_core_dev *mdev,
193 struct mlx5e_params *params)
195 params->lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
196 params->log_rq_mtu_frames = is_kdump_kernel() ?
197 MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE :
198 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
200 mlx5_core_info(mdev, "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
201 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
202 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ ?
203 BIT(mlx5e_mpwqe_get_log_rq_size(params)) :
204 BIT(params->log_rq_mtu_frames),
205 BIT(mlx5e_mpwqe_get_log_stride_size(mdev, params)),
206 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
209 bool mlx5e_striding_rq_possible(struct mlx5_core_dev *mdev,
210 struct mlx5e_params *params)
212 return mlx5e_check_fragmented_striding_rq_cap(mdev) &&
213 !MLX5_IPSEC_DEV(mdev) &&
214 !(params->xdp_prog && !mlx5e_rx_mpwqe_is_linear_skb(mdev, params));
217 void mlx5e_set_rq_type(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
219 params->rq_wq_type = mlx5e_striding_rq_possible(mdev, params) &&
220 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ) ?
221 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
225 static void mlx5e_update_carrier(struct mlx5e_priv *priv)
227 struct mlx5_core_dev *mdev = priv->mdev;
230 port_state = mlx5_query_vport_state(mdev,
231 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT,
234 if (port_state == VPORT_STATE_UP) {
235 netdev_info(priv->netdev, "Link up\n");
236 netif_carrier_on(priv->netdev);
238 netdev_info(priv->netdev, "Link down\n");
239 netif_carrier_off(priv->netdev);
243 static void mlx5e_update_carrier_work(struct work_struct *work)
245 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
246 update_carrier_work);
248 mutex_lock(&priv->state_lock);
249 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
250 if (priv->profile->update_carrier)
251 priv->profile->update_carrier(priv);
252 mutex_unlock(&priv->state_lock);
255 void mlx5e_update_stats(struct mlx5e_priv *priv)
259 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
260 if (mlx5e_stats_grps[i].update_stats)
261 mlx5e_stats_grps[i].update_stats(priv);
264 static void mlx5e_update_ndo_stats(struct mlx5e_priv *priv)
268 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
269 if (mlx5e_stats_grps[i].update_stats_mask &
270 MLX5E_NDO_UPDATE_STATS)
271 mlx5e_stats_grps[i].update_stats(priv);
274 void mlx5e_update_stats_work(struct work_struct *work)
276 struct delayed_work *dwork = to_delayed_work(work);
277 struct mlx5e_priv *priv = container_of(dwork, struct mlx5e_priv,
280 mutex_lock(&priv->state_lock);
281 priv->profile->update_stats(priv);
282 mutex_unlock(&priv->state_lock);
285 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
286 enum mlx5_dev_event event, unsigned long param)
288 struct mlx5e_priv *priv = vpriv;
290 if (!test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state))
294 case MLX5_DEV_EVENT_PORT_UP:
295 case MLX5_DEV_EVENT_PORT_DOWN:
296 queue_work(priv->wq, &priv->update_carrier_work);
303 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
305 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
308 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
310 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
311 synchronize_irq(pci_irq_vector(priv->mdev->pdev, MLX5_EQ_VEC_ASYNC));
314 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq,
315 struct mlx5e_icosq *sq,
316 struct mlx5e_umr_wqe *wqe)
318 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
319 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
320 u8 ds_cnt = DIV_ROUND_UP(MLX5E_UMR_WQE_INLINE_SZ, MLX5_SEND_WQE_DS);
322 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
324 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
325 cseg->imm = rq->mkey_be;
327 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN | MLX5_UMR_INLINE;
328 ucseg->xlt_octowords =
329 cpu_to_be16(MLX5_MTT_OCTW(MLX5_MPWRQ_PAGES_PER_WQE));
330 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
333 static u32 mlx5e_rqwq_get_size(struct mlx5e_rq *rq)
335 switch (rq->wq_type) {
336 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
337 return mlx5_wq_ll_get_size(&rq->mpwqe.wq);
339 return mlx5_wq_cyc_get_size(&rq->wqe.wq);
343 static u32 mlx5e_rqwq_get_cur_sz(struct mlx5e_rq *rq)
345 switch (rq->wq_type) {
346 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
347 return rq->mpwqe.wq.cur_sz;
349 return rq->wqe.wq.cur_sz;
353 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq,
354 struct mlx5e_channel *c)
356 int wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
358 rq->mpwqe.info = kvzalloc_node(array_size(wq_sz,
359 sizeof(*rq->mpwqe.info)),
360 GFP_KERNEL, cpu_to_node(c->cpu));
364 mlx5e_build_umr_wqe(rq, &c->icosq, &rq->mpwqe.umr_wqe);
369 static int mlx5e_create_umr_mkey(struct mlx5_core_dev *mdev,
370 u64 npages, u8 page_shift,
371 struct mlx5_core_mkey *umr_mkey)
373 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
378 in = kvzalloc(inlen, GFP_KERNEL);
382 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
384 MLX5_SET(mkc, mkc, free, 1);
385 MLX5_SET(mkc, mkc, umr_en, 1);
386 MLX5_SET(mkc, mkc, lw, 1);
387 MLX5_SET(mkc, mkc, lr, 1);
388 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT);
390 MLX5_SET(mkc, mkc, qpn, 0xffffff);
391 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.pdn);
392 MLX5_SET64(mkc, mkc, len, npages << page_shift);
393 MLX5_SET(mkc, mkc, translations_octword_size,
394 MLX5_MTT_OCTW(npages));
395 MLX5_SET(mkc, mkc, log_page_size, page_shift);
397 err = mlx5_core_create_mkey(mdev, umr_mkey, in, inlen);
403 static int mlx5e_create_rq_umr_mkey(struct mlx5_core_dev *mdev, struct mlx5e_rq *rq)
405 u64 num_mtts = MLX5E_REQUIRED_MTTS(mlx5_wq_ll_get_size(&rq->mpwqe.wq));
407 return mlx5e_create_umr_mkey(mdev, num_mtts, PAGE_SHIFT, &rq->umr_mkey);
410 static inline u64 mlx5e_get_mpwqe_offset(struct mlx5e_rq *rq, u16 wqe_ix)
412 return (wqe_ix << MLX5E_LOG_ALIGNED_MPWQE_PPW) << PAGE_SHIFT;
415 static void mlx5e_init_frags_partition(struct mlx5e_rq *rq)
417 struct mlx5e_wqe_frag_info next_frag, *prev;
420 next_frag.di = &rq->wqe.di[0];
421 next_frag.offset = 0;
424 for (i = 0; i < mlx5_wq_cyc_get_size(&rq->wqe.wq); i++) {
425 struct mlx5e_rq_frag_info *frag_info = &rq->wqe.info.arr[0];
426 struct mlx5e_wqe_frag_info *frag =
427 &rq->wqe.frags[i << rq->wqe.info.log_num_frags];
430 for (f = 0; f < rq->wqe.info.num_frags; f++, frag++) {
431 if (next_frag.offset + frag_info[f].frag_stride > PAGE_SIZE) {
433 next_frag.offset = 0;
435 prev->last_in_page = true;
440 next_frag.offset += frag_info[f].frag_stride;
446 prev->last_in_page = true;
449 static int mlx5e_init_di_list(struct mlx5e_rq *rq,
450 struct mlx5e_params *params,
453 int len = wq_sz << rq->wqe.info.log_num_frags;
455 rq->wqe.di = kvzalloc_node(array_size(len, sizeof(*rq->wqe.di)),
456 GFP_KERNEL, cpu_to_node(cpu));
460 mlx5e_init_frags_partition(rq);
465 static void mlx5e_free_di_list(struct mlx5e_rq *rq)
470 static int mlx5e_alloc_rq(struct mlx5e_channel *c,
471 struct mlx5e_params *params,
472 struct mlx5e_rq_param *rqp,
475 struct page_pool_params pp_params = { 0 };
476 struct mlx5_core_dev *mdev = c->mdev;
477 void *rqc = rqp->rqc;
478 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
484 rqp->wq.db_numa_node = cpu_to_node(c->cpu);
486 rq->wq_type = params->rq_wq_type;
488 rq->netdev = c->netdev;
489 rq->tstamp = c->tstamp;
490 rq->clock = &mdev->clock;
494 rq->stats = &c->priv->channel_stats[c->ix].rq;
496 rq->xdp_prog = params->xdp_prog ? bpf_prog_inc(params->xdp_prog) : NULL;
497 if (IS_ERR(rq->xdp_prog)) {
498 err = PTR_ERR(rq->xdp_prog);
500 goto err_rq_wq_destroy;
503 err = xdp_rxq_info_reg(&rq->xdp_rxq, rq->netdev, rq->ix);
505 goto err_rq_wq_destroy;
507 rq->buff.map_dir = rq->xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
508 rq->buff.headroom = mlx5e_get_rq_headroom(mdev, params);
509 pool_size = 1 << params->log_rq_mtu_frames;
511 switch (rq->wq_type) {
512 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
513 err = mlx5_wq_ll_create(mdev, &rqp->wq, rqc_wq, &rq->mpwqe.wq,
518 rq->mpwqe.wq.db = &rq->mpwqe.wq.db[MLX5_RCV_DBR];
520 wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
522 pool_size = MLX5_MPWRQ_PAGES_PER_WQE << mlx5e_mpwqe_get_log_rq_size(params);
524 rq->post_wqes = mlx5e_post_rx_mpwqes;
525 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
527 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe_mpwqe;
528 #ifdef CONFIG_MLX5_EN_IPSEC
529 if (MLX5_IPSEC_DEV(mdev)) {
531 netdev_err(c->netdev, "MPWQE RQ with IPSec offload not supported\n");
532 goto err_rq_wq_destroy;
535 if (!rq->handle_rx_cqe) {
537 netdev_err(c->netdev, "RX handler of MPWQE RQ is not set, err %d\n", err);
538 goto err_rq_wq_destroy;
541 rq->mpwqe.skb_from_cqe_mpwrq =
542 mlx5e_rx_mpwqe_is_linear_skb(mdev, params) ?
543 mlx5e_skb_from_cqe_mpwrq_linear :
544 mlx5e_skb_from_cqe_mpwrq_nonlinear;
545 rq->mpwqe.log_stride_sz = mlx5e_mpwqe_get_log_stride_size(mdev, params);
546 rq->mpwqe.num_strides = BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params));
548 err = mlx5e_create_rq_umr_mkey(mdev, rq);
550 goto err_rq_wq_destroy;
551 rq->mkey_be = cpu_to_be32(rq->umr_mkey.key);
553 err = mlx5e_rq_alloc_mpwqe_info(rq, c);
557 default: /* MLX5_WQ_TYPE_CYCLIC */
558 err = mlx5_wq_cyc_create(mdev, &rqp->wq, rqc_wq, &rq->wqe.wq,
563 rq->wqe.wq.db = &rq->wqe.wq.db[MLX5_RCV_DBR];
565 wq_sz = mlx5_wq_cyc_get_size(&rq->wqe.wq);
567 rq->wqe.info = rqp->frags_info;
569 kvzalloc_node(array_size(sizeof(*rq->wqe.frags),
570 (wq_sz << rq->wqe.info.log_num_frags)),
571 GFP_KERNEL, cpu_to_node(c->cpu));
572 if (!rq->wqe.frags) {
577 err = mlx5e_init_di_list(rq, params, wq_sz, c->cpu);
580 rq->post_wqes = mlx5e_post_rx_wqes;
581 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
583 #ifdef CONFIG_MLX5_EN_IPSEC
585 rq->handle_rx_cqe = mlx5e_ipsec_handle_rx_cqe;
588 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe;
589 if (!rq->handle_rx_cqe) {
591 netdev_err(c->netdev, "RX handler of RQ is not set, err %d\n", err);
595 rq->wqe.skb_from_cqe = mlx5e_rx_is_linear_skb(mdev, params) ?
596 mlx5e_skb_from_cqe_linear :
597 mlx5e_skb_from_cqe_nonlinear;
598 rq->mkey_be = c->mkey_be;
601 /* Create a page_pool and register it with rxq */
603 pp_params.flags = 0; /* No-internal DMA mapping in page_pool */
604 pp_params.pool_size = pool_size;
605 pp_params.nid = cpu_to_node(c->cpu);
606 pp_params.dev = c->pdev;
607 pp_params.dma_dir = rq->buff.map_dir;
609 /* page_pool can be used even when there is no rq->xdp_prog,
610 * given page_pool does not handle DMA mapping there is no
611 * required state to clear. And page_pool gracefully handle
614 rq->page_pool = page_pool_create(&pp_params);
615 if (IS_ERR(rq->page_pool)) {
616 err = PTR_ERR(rq->page_pool);
617 rq->page_pool = NULL;
620 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
621 MEM_TYPE_PAGE_POOL, rq->page_pool);
625 for (i = 0; i < wq_sz; i++) {
626 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
627 struct mlx5e_rx_wqe_ll *wqe =
628 mlx5_wq_ll_get_wqe(&rq->mpwqe.wq, i);
630 rq->mpwqe.num_strides << rq->mpwqe.log_stride_sz;
631 u64 dma_offset = mlx5e_get_mpwqe_offset(rq, i);
633 wqe->data[0].addr = cpu_to_be64(dma_offset + rq->buff.headroom);
634 wqe->data[0].byte_count = cpu_to_be32(byte_count);
635 wqe->data[0].lkey = rq->mkey_be;
637 struct mlx5e_rx_wqe_cyc *wqe =
638 mlx5_wq_cyc_get_wqe(&rq->wqe.wq, i);
641 for (f = 0; f < rq->wqe.info.num_frags; f++) {
642 u32 frag_size = rq->wqe.info.arr[f].frag_size |
643 MLX5_HW_START_PADDING;
645 wqe->data[f].byte_count = cpu_to_be32(frag_size);
646 wqe->data[f].lkey = rq->mkey_be;
648 /* check if num_frags is not a pow of two */
649 if (rq->wqe.info.num_frags < (1 << rq->wqe.info.log_num_frags)) {
650 wqe->data[f].byte_count = 0;
651 wqe->data[f].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
652 wqe->data[f].addr = 0;
657 INIT_WORK(&rq->dim.work, mlx5e_rx_dim_work);
659 switch (params->rx_cq_moderation.cq_period_mode) {
660 case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
661 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE;
663 case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
665 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
668 rq->page_cache.head = 0;
669 rq->page_cache.tail = 0;
674 switch (rq->wq_type) {
675 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
676 kvfree(rq->mpwqe.info);
677 mlx5_core_destroy_mkey(mdev, &rq->umr_mkey);
679 default: /* MLX5_WQ_TYPE_CYCLIC */
680 kvfree(rq->wqe.frags);
681 mlx5e_free_di_list(rq);
686 bpf_prog_put(rq->xdp_prog);
687 xdp_rxq_info_unreg(&rq->xdp_rxq);
689 page_pool_destroy(rq->page_pool);
690 mlx5_wq_destroy(&rq->wq_ctrl);
695 static void mlx5e_free_rq(struct mlx5e_rq *rq)
700 bpf_prog_put(rq->xdp_prog);
702 xdp_rxq_info_unreg(&rq->xdp_rxq);
704 page_pool_destroy(rq->page_pool);
706 switch (rq->wq_type) {
707 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
708 kvfree(rq->mpwqe.info);
709 mlx5_core_destroy_mkey(rq->mdev, &rq->umr_mkey);
711 default: /* MLX5_WQ_TYPE_CYCLIC */
712 kvfree(rq->wqe.frags);
713 mlx5e_free_di_list(rq);
716 for (i = rq->page_cache.head; i != rq->page_cache.tail;
717 i = (i + 1) & (MLX5E_CACHE_SIZE - 1)) {
718 struct mlx5e_dma_info *dma_info = &rq->page_cache.page_cache[i];
720 mlx5e_page_release(rq, dma_info, false);
722 mlx5_wq_destroy(&rq->wq_ctrl);
725 static int mlx5e_create_rq(struct mlx5e_rq *rq,
726 struct mlx5e_rq_param *param)
728 struct mlx5_core_dev *mdev = rq->mdev;
736 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
737 sizeof(u64) * rq->wq_ctrl.buf.npages;
738 in = kvzalloc(inlen, GFP_KERNEL);
742 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
743 wq = MLX5_ADDR_OF(rqc, rqc, wq);
745 memcpy(rqc, param->rqc, sizeof(param->rqc));
747 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
748 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
749 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
750 MLX5_ADAPTER_PAGE_SHIFT);
751 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
753 mlx5_fill_page_frag_array(&rq->wq_ctrl.buf,
754 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
756 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
763 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
766 struct mlx5_core_dev *mdev = rq->mdev;
773 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
774 in = kvzalloc(inlen, GFP_KERNEL);
778 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
780 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
781 MLX5_SET(rqc, rqc, state, next_state);
783 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
790 static int mlx5e_modify_rq_scatter_fcs(struct mlx5e_rq *rq, bool enable)
792 struct mlx5e_channel *c = rq->channel;
793 struct mlx5e_priv *priv = c->priv;
794 struct mlx5_core_dev *mdev = priv->mdev;
801 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
802 in = kvzalloc(inlen, GFP_KERNEL);
806 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
808 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
809 MLX5_SET64(modify_rq_in, in, modify_bitmask,
810 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_SCATTER_FCS);
811 MLX5_SET(rqc, rqc, scatter_fcs, enable);
812 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
814 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
821 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
823 struct mlx5e_channel *c = rq->channel;
824 struct mlx5_core_dev *mdev = c->mdev;
830 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
831 in = kvzalloc(inlen, GFP_KERNEL);
835 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
837 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
838 MLX5_SET64(modify_rq_in, in, modify_bitmask,
839 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
840 MLX5_SET(rqc, rqc, vsd, vsd);
841 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
843 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
850 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
852 mlx5_core_destroy_rq(rq->mdev, rq->rqn);
855 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq, int wait_time)
857 unsigned long exp_time = jiffies + msecs_to_jiffies(wait_time);
858 struct mlx5e_channel *c = rq->channel;
860 u16 min_wqes = mlx5_min_rx_wqes(rq->wq_type, mlx5e_rqwq_get_size(rq));
863 if (mlx5e_rqwq_get_cur_sz(rq) >= min_wqes)
867 } while (time_before(jiffies, exp_time));
869 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",
870 c->ix, rq->rqn, mlx5e_rqwq_get_cur_sz(rq), min_wqes);
875 static void mlx5e_free_rx_descs(struct mlx5e_rq *rq)
880 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
881 struct mlx5_wq_ll *wq = &rq->mpwqe.wq;
883 /* UMR WQE (if in progress) is always at wq->head */
884 if (rq->mpwqe.umr_in_progress)
885 rq->dealloc_wqe(rq, wq->head);
887 while (!mlx5_wq_ll_is_empty(wq)) {
888 struct mlx5e_rx_wqe_ll *wqe;
890 wqe_ix_be = *wq->tail_next;
891 wqe_ix = be16_to_cpu(wqe_ix_be);
892 wqe = mlx5_wq_ll_get_wqe(wq, wqe_ix);
893 rq->dealloc_wqe(rq, wqe_ix);
894 mlx5_wq_ll_pop(wq, wqe_ix_be,
895 &wqe->next.next_wqe_index);
898 struct mlx5_wq_cyc *wq = &rq->wqe.wq;
900 while (!mlx5_wq_cyc_is_empty(wq)) {
901 wqe_ix = mlx5_wq_cyc_get_tail(wq);
902 rq->dealloc_wqe(rq, wqe_ix);
909 static int mlx5e_open_rq(struct mlx5e_channel *c,
910 struct mlx5e_params *params,
911 struct mlx5e_rq_param *param,
916 err = mlx5e_alloc_rq(c, params, param, rq);
920 err = mlx5e_create_rq(rq, param);
924 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
928 if (params->rx_dim_enabled)
929 __set_bit(MLX5E_RQ_STATE_AM, &c->rq.state);
934 mlx5e_destroy_rq(rq);
941 static void mlx5e_activate_rq(struct mlx5e_rq *rq)
943 struct mlx5e_icosq *sq = &rq->channel->icosq;
944 struct mlx5_wq_cyc *wq = &sq->wq;
945 struct mlx5e_tx_wqe *nopwqe;
947 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
949 set_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
950 sq->db.ico_wqe[pi].opcode = MLX5_OPCODE_NOP;
951 nopwqe = mlx5e_post_nop(wq, sq->sqn, &sq->pc);
952 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &nopwqe->ctrl);
955 static void mlx5e_deactivate_rq(struct mlx5e_rq *rq)
957 clear_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
958 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
961 static void mlx5e_close_rq(struct mlx5e_rq *rq)
963 cancel_work_sync(&rq->dim.work);
964 mlx5e_destroy_rq(rq);
965 mlx5e_free_rx_descs(rq);
969 static void mlx5e_free_xdpsq_db(struct mlx5e_xdpsq *sq)
974 static int mlx5e_alloc_xdpsq_db(struct mlx5e_xdpsq *sq, int numa)
976 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
978 sq->db.xdpi = kvzalloc_node(array_size(wq_sz, sizeof(*sq->db.xdpi)),
981 mlx5e_free_xdpsq_db(sq);
988 static int mlx5e_alloc_xdpsq(struct mlx5e_channel *c,
989 struct mlx5e_params *params,
990 struct mlx5e_sq_param *param,
991 struct mlx5e_xdpsq *sq)
993 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
994 struct mlx5_core_dev *mdev = c->mdev;
995 struct mlx5_wq_cyc *wq = &sq->wq;
999 sq->mkey_be = c->mkey_be;
1001 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1002 sq->min_inline_mode = params->tx_min_inline_mode;
1003 sq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
1005 param->wq.db_numa_node = cpu_to_node(c->cpu);
1006 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1009 wq->db = &wq->db[MLX5_SND_DBR];
1011 err = mlx5e_alloc_xdpsq_db(sq, cpu_to_node(c->cpu));
1013 goto err_sq_wq_destroy;
1018 mlx5_wq_destroy(&sq->wq_ctrl);
1023 static void mlx5e_free_xdpsq(struct mlx5e_xdpsq *sq)
1025 mlx5e_free_xdpsq_db(sq);
1026 mlx5_wq_destroy(&sq->wq_ctrl);
1029 static void mlx5e_free_icosq_db(struct mlx5e_icosq *sq)
1031 kvfree(sq->db.ico_wqe);
1034 static int mlx5e_alloc_icosq_db(struct mlx5e_icosq *sq, int numa)
1036 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1038 sq->db.ico_wqe = kvzalloc_node(array_size(wq_sz,
1039 sizeof(*sq->db.ico_wqe)),
1041 if (!sq->db.ico_wqe)
1047 static int mlx5e_alloc_icosq(struct mlx5e_channel *c,
1048 struct mlx5e_sq_param *param,
1049 struct mlx5e_icosq *sq)
1051 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1052 struct mlx5_core_dev *mdev = c->mdev;
1053 struct mlx5_wq_cyc *wq = &sq->wq;
1057 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1059 param->wq.db_numa_node = cpu_to_node(c->cpu);
1060 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1063 wq->db = &wq->db[MLX5_SND_DBR];
1065 err = mlx5e_alloc_icosq_db(sq, cpu_to_node(c->cpu));
1067 goto err_sq_wq_destroy;
1072 mlx5_wq_destroy(&sq->wq_ctrl);
1077 static void mlx5e_free_icosq(struct mlx5e_icosq *sq)
1079 mlx5e_free_icosq_db(sq);
1080 mlx5_wq_destroy(&sq->wq_ctrl);
1083 static void mlx5e_free_txqsq_db(struct mlx5e_txqsq *sq)
1085 kvfree(sq->db.wqe_info);
1086 kvfree(sq->db.dma_fifo);
1089 static int mlx5e_alloc_txqsq_db(struct mlx5e_txqsq *sq, int numa)
1091 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1092 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
1094 sq->db.dma_fifo = kvzalloc_node(array_size(df_sz,
1095 sizeof(*sq->db.dma_fifo)),
1097 sq->db.wqe_info = kvzalloc_node(array_size(wq_sz,
1098 sizeof(*sq->db.wqe_info)),
1100 if (!sq->db.dma_fifo || !sq->db.wqe_info) {
1101 mlx5e_free_txqsq_db(sq);
1105 sq->dma_fifo_mask = df_sz - 1;
1110 static void mlx5e_sq_recover(struct work_struct *work);
1111 static int mlx5e_alloc_txqsq(struct mlx5e_channel *c,
1113 struct mlx5e_params *params,
1114 struct mlx5e_sq_param *param,
1115 struct mlx5e_txqsq *sq,
1118 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1119 struct mlx5_core_dev *mdev = c->mdev;
1120 struct mlx5_wq_cyc *wq = &sq->wq;
1124 sq->tstamp = c->tstamp;
1125 sq->clock = &mdev->clock;
1126 sq->mkey_be = c->mkey_be;
1128 sq->txq_ix = txq_ix;
1129 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1130 sq->min_inline_mode = params->tx_min_inline_mode;
1131 sq->stats = &c->priv->channel_stats[c->ix].sq[tc];
1132 INIT_WORK(&sq->recover.recover_work, mlx5e_sq_recover);
1133 if (MLX5_IPSEC_DEV(c->priv->mdev))
1134 set_bit(MLX5E_SQ_STATE_IPSEC, &sq->state);
1135 if (mlx5_accel_is_tls_device(c->priv->mdev))
1136 set_bit(MLX5E_SQ_STATE_TLS, &sq->state);
1138 param->wq.db_numa_node = cpu_to_node(c->cpu);
1139 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1142 wq->db = &wq->db[MLX5_SND_DBR];
1144 err = mlx5e_alloc_txqsq_db(sq, cpu_to_node(c->cpu));
1146 goto err_sq_wq_destroy;
1148 INIT_WORK(&sq->dim.work, mlx5e_tx_dim_work);
1149 sq->dim.mode = params->tx_cq_moderation.cq_period_mode;
1154 mlx5_wq_destroy(&sq->wq_ctrl);
1159 static void mlx5e_free_txqsq(struct mlx5e_txqsq *sq)
1161 mlx5e_free_txqsq_db(sq);
1162 mlx5_wq_destroy(&sq->wq_ctrl);
1165 struct mlx5e_create_sq_param {
1166 struct mlx5_wq_ctrl *wq_ctrl;
1173 static int mlx5e_create_sq(struct mlx5_core_dev *mdev,
1174 struct mlx5e_sq_param *param,
1175 struct mlx5e_create_sq_param *csp,
1184 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1185 sizeof(u64) * csp->wq_ctrl->buf.npages;
1186 in = kvzalloc(inlen, GFP_KERNEL);
1190 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1191 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1193 memcpy(sqc, param->sqc, sizeof(param->sqc));
1194 MLX5_SET(sqc, sqc, tis_lst_sz, csp->tis_lst_sz);
1195 MLX5_SET(sqc, sqc, tis_num_0, csp->tisn);
1196 MLX5_SET(sqc, sqc, cqn, csp->cqn);
1198 if (MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5_CAP_INLINE_MODE_VPORT_CONTEXT)
1199 MLX5_SET(sqc, sqc, min_wqe_inline_mode, csp->min_inline_mode);
1201 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1202 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1204 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1205 MLX5_SET(wq, wq, uar_page, mdev->mlx5e_res.bfreg.index);
1206 MLX5_SET(wq, wq, log_wq_pg_sz, csp->wq_ctrl->buf.page_shift -
1207 MLX5_ADAPTER_PAGE_SHIFT);
1208 MLX5_SET64(wq, wq, dbr_addr, csp->wq_ctrl->db.dma);
1210 mlx5_fill_page_frag_array(&csp->wq_ctrl->buf,
1211 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
1213 err = mlx5_core_create_sq(mdev, in, inlen, sqn);
1220 struct mlx5e_modify_sq_param {
1227 static int mlx5e_modify_sq(struct mlx5_core_dev *mdev, u32 sqn,
1228 struct mlx5e_modify_sq_param *p)
1235 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1236 in = kvzalloc(inlen, GFP_KERNEL);
1240 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1242 MLX5_SET(modify_sq_in, in, sq_state, p->curr_state);
1243 MLX5_SET(sqc, sqc, state, p->next_state);
1244 if (p->rl_update && p->next_state == MLX5_SQC_STATE_RDY) {
1245 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
1246 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, p->rl_index);
1249 err = mlx5_core_modify_sq(mdev, sqn, in, inlen);
1256 static void mlx5e_destroy_sq(struct mlx5_core_dev *mdev, u32 sqn)
1258 mlx5_core_destroy_sq(mdev, sqn);
1261 static int mlx5e_create_sq_rdy(struct mlx5_core_dev *mdev,
1262 struct mlx5e_sq_param *param,
1263 struct mlx5e_create_sq_param *csp,
1266 struct mlx5e_modify_sq_param msp = {0};
1269 err = mlx5e_create_sq(mdev, param, csp, sqn);
1273 msp.curr_state = MLX5_SQC_STATE_RST;
1274 msp.next_state = MLX5_SQC_STATE_RDY;
1275 err = mlx5e_modify_sq(mdev, *sqn, &msp);
1277 mlx5e_destroy_sq(mdev, *sqn);
1282 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1283 struct mlx5e_txqsq *sq, u32 rate);
1285 static int mlx5e_open_txqsq(struct mlx5e_channel *c,
1288 struct mlx5e_params *params,
1289 struct mlx5e_sq_param *param,
1290 struct mlx5e_txqsq *sq,
1293 struct mlx5e_create_sq_param csp = {};
1297 err = mlx5e_alloc_txqsq(c, txq_ix, params, param, sq, tc);
1303 csp.cqn = sq->cq.mcq.cqn;
1304 csp.wq_ctrl = &sq->wq_ctrl;
1305 csp.min_inline_mode = sq->min_inline_mode;
1306 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1308 goto err_free_txqsq;
1310 tx_rate = c->priv->tx_rates[sq->txq_ix];
1312 mlx5e_set_sq_maxrate(c->netdev, sq, tx_rate);
1314 if (params->tx_dim_enabled)
1315 sq->state |= BIT(MLX5E_SQ_STATE_AM);
1320 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1321 mlx5e_free_txqsq(sq);
1326 static void mlx5e_reset_txqsq_cc_pc(struct mlx5e_txqsq *sq)
1328 WARN_ONCE(sq->cc != sq->pc,
1329 "SQ 0x%x: cc (0x%x) != pc (0x%x)\n",
1330 sq->sqn, sq->cc, sq->pc);
1332 sq->dma_fifo_cc = 0;
1336 static void mlx5e_activate_txqsq(struct mlx5e_txqsq *sq)
1338 sq->txq = netdev_get_tx_queue(sq->channel->netdev, sq->txq_ix);
1339 clear_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state);
1340 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1341 netdev_tx_reset_queue(sq->txq);
1342 netif_tx_start_queue(sq->txq);
1345 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
1347 __netif_tx_lock_bh(txq);
1348 netif_tx_stop_queue(txq);
1349 __netif_tx_unlock_bh(txq);
1352 static void mlx5e_deactivate_txqsq(struct mlx5e_txqsq *sq)
1354 struct mlx5e_channel *c = sq->channel;
1355 struct mlx5_wq_cyc *wq = &sq->wq;
1357 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1358 /* prevent netif_tx_wake_queue */
1359 napi_synchronize(&c->napi);
1361 netif_tx_disable_queue(sq->txq);
1363 /* last doorbell out, godspeed .. */
1364 if (mlx5e_wqc_has_room_for(wq, sq->cc, sq->pc, 1)) {
1365 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
1366 struct mlx5e_tx_wqe *nop;
1368 sq->db.wqe_info[pi].skb = NULL;
1369 nop = mlx5e_post_nop(wq, sq->sqn, &sq->pc);
1370 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &nop->ctrl);
1374 static void mlx5e_close_txqsq(struct mlx5e_txqsq *sq)
1376 struct mlx5e_channel *c = sq->channel;
1377 struct mlx5_core_dev *mdev = c->mdev;
1378 struct mlx5_rate_limit rl = {0};
1380 mlx5e_destroy_sq(mdev, sq->sqn);
1381 if (sq->rate_limit) {
1382 rl.rate = sq->rate_limit;
1383 mlx5_rl_remove_rate(mdev, &rl);
1385 mlx5e_free_txqsq_descs(sq);
1386 mlx5e_free_txqsq(sq);
1389 static int mlx5e_wait_for_sq_flush(struct mlx5e_txqsq *sq)
1391 unsigned long exp_time = jiffies + msecs_to_jiffies(2000);
1393 while (time_before(jiffies, exp_time)) {
1394 if (sq->cc == sq->pc)
1400 netdev_err(sq->channel->netdev,
1401 "Wait for SQ 0x%x flush timeout (sq cc = 0x%x, sq pc = 0x%x)\n",
1402 sq->sqn, sq->cc, sq->pc);
1407 static int mlx5e_sq_to_ready(struct mlx5e_txqsq *sq, int curr_state)
1409 struct mlx5_core_dev *mdev = sq->channel->mdev;
1410 struct net_device *dev = sq->channel->netdev;
1411 struct mlx5e_modify_sq_param msp = {0};
1414 msp.curr_state = curr_state;
1415 msp.next_state = MLX5_SQC_STATE_RST;
1417 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1419 netdev_err(dev, "Failed to move sq 0x%x to reset\n", sq->sqn);
1423 memset(&msp, 0, sizeof(msp));
1424 msp.curr_state = MLX5_SQC_STATE_RST;
1425 msp.next_state = MLX5_SQC_STATE_RDY;
1427 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1429 netdev_err(dev, "Failed to move sq 0x%x to ready\n", sq->sqn);
1436 static void mlx5e_sq_recover(struct work_struct *work)
1438 struct mlx5e_txqsq_recover *recover =
1439 container_of(work, struct mlx5e_txqsq_recover,
1441 struct mlx5e_txqsq *sq = container_of(recover, struct mlx5e_txqsq,
1443 struct mlx5_core_dev *mdev = sq->channel->mdev;
1444 struct net_device *dev = sq->channel->netdev;
1448 err = mlx5_core_query_sq_state(mdev, sq->sqn, &state);
1450 netdev_err(dev, "Failed to query SQ 0x%x state. err = %d\n",
1455 if (state != MLX5_RQC_STATE_ERR) {
1456 netdev_err(dev, "SQ 0x%x not in ERROR state\n", sq->sqn);
1460 netif_tx_disable_queue(sq->txq);
1462 if (mlx5e_wait_for_sq_flush(sq))
1465 /* If the interval between two consecutive recovers per SQ is too
1466 * short, don't recover to avoid infinite loop of ERR_CQE -> recover.
1467 * If we reached this state, there is probably a bug that needs to be
1468 * fixed. let's keep the queue close and let tx timeout cleanup.
1470 if (jiffies_to_msecs(jiffies - recover->last_recover) <
1471 MLX5E_SQ_RECOVER_MIN_INTERVAL) {
1472 netdev_err(dev, "Recover SQ 0x%x canceled, too many error CQEs\n",
1477 /* At this point, no new packets will arrive from the stack as TXQ is
1478 * marked with QUEUE_STATE_DRV_XOFF. In addition, NAPI cleared all
1479 * pending WQEs. SQ can safely reset the SQ.
1481 if (mlx5e_sq_to_ready(sq, state))
1484 mlx5e_reset_txqsq_cc_pc(sq);
1485 sq->stats->recover++;
1486 recover->last_recover = jiffies;
1487 mlx5e_activate_txqsq(sq);
1490 static int mlx5e_open_icosq(struct mlx5e_channel *c,
1491 struct mlx5e_params *params,
1492 struct mlx5e_sq_param *param,
1493 struct mlx5e_icosq *sq)
1495 struct mlx5e_create_sq_param csp = {};
1498 err = mlx5e_alloc_icosq(c, param, sq);
1502 csp.cqn = sq->cq.mcq.cqn;
1503 csp.wq_ctrl = &sq->wq_ctrl;
1504 csp.min_inline_mode = params->tx_min_inline_mode;
1505 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1506 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1508 goto err_free_icosq;
1513 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1514 mlx5e_free_icosq(sq);
1519 static void mlx5e_close_icosq(struct mlx5e_icosq *sq)
1521 struct mlx5e_channel *c = sq->channel;
1523 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1524 napi_synchronize(&c->napi);
1526 mlx5e_destroy_sq(c->mdev, sq->sqn);
1527 mlx5e_free_icosq(sq);
1530 static int mlx5e_open_xdpsq(struct mlx5e_channel *c,
1531 struct mlx5e_params *params,
1532 struct mlx5e_sq_param *param,
1533 struct mlx5e_xdpsq *sq)
1535 unsigned int ds_cnt = MLX5E_XDP_TX_DS_COUNT;
1536 struct mlx5e_create_sq_param csp = {};
1537 unsigned int inline_hdr_sz = 0;
1541 err = mlx5e_alloc_xdpsq(c, params, param, sq);
1546 csp.tisn = c->priv->tisn[0]; /* tc = 0 */
1547 csp.cqn = sq->cq.mcq.cqn;
1548 csp.wq_ctrl = &sq->wq_ctrl;
1549 csp.min_inline_mode = sq->min_inline_mode;
1550 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1551 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1553 goto err_free_xdpsq;
1555 if (sq->min_inline_mode != MLX5_INLINE_MODE_NONE) {
1556 inline_hdr_sz = MLX5E_XDP_MIN_INLINE;
1560 /* Pre initialize fixed WQE fields */
1561 for (i = 0; i < mlx5_wq_cyc_get_size(&sq->wq); i++) {
1562 struct mlx5e_tx_wqe *wqe = mlx5_wq_cyc_get_wqe(&sq->wq, i);
1563 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
1564 struct mlx5_wqe_eth_seg *eseg = &wqe->eth;
1565 struct mlx5_wqe_data_seg *dseg;
1567 cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
1568 eseg->inline_hdr.sz = cpu_to_be16(inline_hdr_sz);
1570 dseg = (struct mlx5_wqe_data_seg *)cseg + (ds_cnt - 1);
1571 dseg->lkey = sq->mkey_be;
1577 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1578 mlx5e_free_xdpsq(sq);
1583 static void mlx5e_close_xdpsq(struct mlx5e_xdpsq *sq)
1585 struct mlx5e_channel *c = sq->channel;
1587 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1588 napi_synchronize(&c->napi);
1590 mlx5e_destroy_sq(c->mdev, sq->sqn);
1591 mlx5e_free_xdpsq_descs(sq);
1592 mlx5e_free_xdpsq(sq);
1595 static int mlx5e_alloc_cq_common(struct mlx5_core_dev *mdev,
1596 struct mlx5e_cq_param *param,
1597 struct mlx5e_cq *cq)
1599 struct mlx5_core_cq *mcq = &cq->mcq;
1605 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1610 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1613 mcq->set_ci_db = cq->wq_ctrl.db.db;
1614 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1615 *mcq->set_ci_db = 0;
1617 mcq->vector = param->eq_ix;
1618 mcq->comp = mlx5e_completion_event;
1619 mcq->event = mlx5e_cq_error_event;
1622 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1623 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1633 static int mlx5e_alloc_cq(struct mlx5e_channel *c,
1634 struct mlx5e_cq_param *param,
1635 struct mlx5e_cq *cq)
1637 struct mlx5_core_dev *mdev = c->priv->mdev;
1640 param->wq.buf_numa_node = cpu_to_node(c->cpu);
1641 param->wq.db_numa_node = cpu_to_node(c->cpu);
1642 param->eq_ix = c->ix;
1644 err = mlx5e_alloc_cq_common(mdev, param, cq);
1646 cq->napi = &c->napi;
1652 static void mlx5e_free_cq(struct mlx5e_cq *cq)
1654 mlx5_wq_destroy(&cq->wq_ctrl);
1657 static int mlx5e_create_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
1659 struct mlx5_core_dev *mdev = cq->mdev;
1660 struct mlx5_core_cq *mcq = &cq->mcq;
1665 unsigned int irqn_not_used;
1669 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1670 sizeof(u64) * cq->wq_ctrl.buf.npages;
1671 in = kvzalloc(inlen, GFP_KERNEL);
1675 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1677 memcpy(cqc, param->cqc, sizeof(param->cqc));
1679 mlx5_fill_page_frag_array(&cq->wq_ctrl.buf,
1680 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
1682 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
1684 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
1685 MLX5_SET(cqc, cqc, c_eqn, eqn);
1686 MLX5_SET(cqc, cqc, uar_page, mdev->priv.uar->index);
1687 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
1688 MLX5_ADAPTER_PAGE_SHIFT);
1689 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1691 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
1703 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
1705 mlx5_core_destroy_cq(cq->mdev, &cq->mcq);
1708 static int mlx5e_open_cq(struct mlx5e_channel *c,
1709 struct net_dim_cq_moder moder,
1710 struct mlx5e_cq_param *param,
1711 struct mlx5e_cq *cq)
1713 struct mlx5_core_dev *mdev = c->mdev;
1716 err = mlx5e_alloc_cq(c, param, cq);
1720 err = mlx5e_create_cq(cq, param);
1724 if (MLX5_CAP_GEN(mdev, cq_moderation))
1725 mlx5_core_modify_cq_moderation(mdev, &cq->mcq, moder.usec, moder.pkts);
1734 static void mlx5e_close_cq(struct mlx5e_cq *cq)
1736 mlx5e_destroy_cq(cq);
1740 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
1742 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
1745 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1746 struct mlx5e_params *params,
1747 struct mlx5e_channel_param *cparam)
1752 for (tc = 0; tc < c->num_tc; tc++) {
1753 err = mlx5e_open_cq(c, params->tx_cq_moderation,
1754 &cparam->tx_cq, &c->sq[tc].cq);
1756 goto err_close_tx_cqs;
1762 for (tc--; tc >= 0; tc--)
1763 mlx5e_close_cq(&c->sq[tc].cq);
1768 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1772 for (tc = 0; tc < c->num_tc; tc++)
1773 mlx5e_close_cq(&c->sq[tc].cq);
1776 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1777 struct mlx5e_params *params,
1778 struct mlx5e_channel_param *cparam)
1780 struct mlx5e_priv *priv = c->priv;
1781 int err, tc, max_nch = priv->profile->max_nch(priv->mdev);
1783 for (tc = 0; tc < params->num_tc; tc++) {
1784 int txq_ix = c->ix + tc * max_nch;
1786 err = mlx5e_open_txqsq(c, c->priv->tisn[tc], txq_ix,
1787 params, &cparam->sq, &c->sq[tc], tc);
1795 for (tc--; tc >= 0; tc--)
1796 mlx5e_close_txqsq(&c->sq[tc]);
1801 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1805 for (tc = 0; tc < c->num_tc; tc++)
1806 mlx5e_close_txqsq(&c->sq[tc]);
1809 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1810 struct mlx5e_txqsq *sq, u32 rate)
1812 struct mlx5e_priv *priv = netdev_priv(dev);
1813 struct mlx5_core_dev *mdev = priv->mdev;
1814 struct mlx5e_modify_sq_param msp = {0};
1815 struct mlx5_rate_limit rl = {0};
1819 if (rate == sq->rate_limit)
1823 if (sq->rate_limit) {
1824 rl.rate = sq->rate_limit;
1825 /* remove current rl index to free space to next ones */
1826 mlx5_rl_remove_rate(mdev, &rl);
1833 err = mlx5_rl_add_rate(mdev, &rl_index, &rl);
1835 netdev_err(dev, "Failed configuring rate %u: %d\n",
1841 msp.curr_state = MLX5_SQC_STATE_RDY;
1842 msp.next_state = MLX5_SQC_STATE_RDY;
1843 msp.rl_index = rl_index;
1844 msp.rl_update = true;
1845 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1847 netdev_err(dev, "Failed configuring rate %u: %d\n",
1849 /* remove the rate from the table */
1851 mlx5_rl_remove_rate(mdev, &rl);
1855 sq->rate_limit = rate;
1859 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1861 struct mlx5e_priv *priv = netdev_priv(dev);
1862 struct mlx5_core_dev *mdev = priv->mdev;
1863 struct mlx5e_txqsq *sq = priv->txq2sq[index];
1866 if (!mlx5_rl_is_supported(mdev)) {
1867 netdev_err(dev, "Rate limiting is not supported on this device\n");
1871 /* rate is given in Mb/sec, HW config is in Kb/sec */
1874 /* Check whether rate in valid range, 0 is always valid */
1875 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1876 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1880 mutex_lock(&priv->state_lock);
1881 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1882 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1884 priv->tx_rates[index] = rate;
1885 mutex_unlock(&priv->state_lock);
1890 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1891 struct mlx5e_params *params,
1892 struct mlx5e_channel_param *cparam,
1893 struct mlx5e_channel **cp)
1895 struct net_dim_cq_moder icocq_moder = {0, 0};
1896 struct net_device *netdev = priv->netdev;
1897 int cpu = mlx5e_get_cpu(priv, ix);
1898 struct mlx5e_channel *c;
1903 c = kvzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1908 c->mdev = priv->mdev;
1909 c->tstamp = &priv->tstamp;
1912 c->pdev = &priv->mdev->pdev->dev;
1913 c->netdev = priv->netdev;
1914 c->mkey_be = cpu_to_be32(priv->mdev->mlx5e_res.mkey.key);
1915 c->num_tc = params->num_tc;
1916 c->xdp = !!params->xdp_prog;
1917 c->stats = &priv->channel_stats[ix].ch;
1919 mlx5_vector2eqn(priv->mdev, ix, &eqn, &irq);
1920 c->irq_desc = irq_to_desc(irq);
1922 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1924 err = mlx5e_open_cq(c, icocq_moder, &cparam->icosq_cq, &c->icosq.cq);
1928 err = mlx5e_open_tx_cqs(c, params, cparam);
1930 goto err_close_icosq_cq;
1932 err = mlx5e_open_cq(c, params->rx_cq_moderation, &cparam->rx_cq, &c->rq.cq);
1934 goto err_close_tx_cqs;
1936 /* XDP SQ CQ params are same as normal TXQ sq CQ params */
1937 err = c->xdp ? mlx5e_open_cq(c, params->tx_cq_moderation,
1938 &cparam->tx_cq, &c->rq.xdpsq.cq) : 0;
1940 goto err_close_rx_cq;
1942 napi_enable(&c->napi);
1944 err = mlx5e_open_icosq(c, params, &cparam->icosq, &c->icosq);
1946 goto err_disable_napi;
1948 err = mlx5e_open_sqs(c, params, cparam);
1950 goto err_close_icosq;
1952 err = c->xdp ? mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, &c->rq.xdpsq) : 0;
1956 err = mlx5e_open_rq(c, params, &cparam->rq, &c->rq);
1958 goto err_close_xdp_sq;
1965 mlx5e_close_xdpsq(&c->rq.xdpsq);
1971 mlx5e_close_icosq(&c->icosq);
1974 napi_disable(&c->napi);
1976 mlx5e_close_cq(&c->rq.xdpsq.cq);
1979 mlx5e_close_cq(&c->rq.cq);
1982 mlx5e_close_tx_cqs(c);
1985 mlx5e_close_cq(&c->icosq.cq);
1988 netif_napi_del(&c->napi);
1994 static void mlx5e_activate_channel(struct mlx5e_channel *c)
1998 for (tc = 0; tc < c->num_tc; tc++)
1999 mlx5e_activate_txqsq(&c->sq[tc]);
2000 mlx5e_activate_rq(&c->rq);
2001 netif_set_xps_queue(c->netdev, get_cpu_mask(c->cpu), c->ix);
2004 static void mlx5e_deactivate_channel(struct mlx5e_channel *c)
2008 mlx5e_deactivate_rq(&c->rq);
2009 for (tc = 0; tc < c->num_tc; tc++)
2010 mlx5e_deactivate_txqsq(&c->sq[tc]);
2013 static void mlx5e_close_channel(struct mlx5e_channel *c)
2015 mlx5e_close_rq(&c->rq);
2017 mlx5e_close_xdpsq(&c->rq.xdpsq);
2019 mlx5e_close_icosq(&c->icosq);
2020 napi_disable(&c->napi);
2022 mlx5e_close_cq(&c->rq.xdpsq.cq);
2023 mlx5e_close_cq(&c->rq.cq);
2024 mlx5e_close_tx_cqs(c);
2025 mlx5e_close_cq(&c->icosq.cq);
2026 netif_napi_del(&c->napi);
2031 #define DEFAULT_FRAG_SIZE (2048)
2033 static void mlx5e_build_rq_frags_info(struct mlx5_core_dev *mdev,
2034 struct mlx5e_params *params,
2035 struct mlx5e_rq_frags_info *info)
2037 u32 byte_count = MLX5E_SW2HW_MTU(params, params->sw_mtu);
2038 int frag_size_max = DEFAULT_FRAG_SIZE;
2042 #ifdef CONFIG_MLX5_EN_IPSEC
2043 if (MLX5_IPSEC_DEV(mdev))
2044 byte_count += MLX5E_METADATA_ETHER_LEN;
2047 if (mlx5e_rx_is_linear_skb(mdev, params)) {
2050 frag_stride = mlx5e_rx_get_linear_frag_sz(params);
2051 frag_stride = roundup_pow_of_two(frag_stride);
2053 info->arr[0].frag_size = byte_count;
2054 info->arr[0].frag_stride = frag_stride;
2055 info->num_frags = 1;
2056 info->wqe_bulk = PAGE_SIZE / frag_stride;
2060 if (byte_count > PAGE_SIZE +
2061 (MLX5E_MAX_RX_FRAGS - 1) * frag_size_max)
2062 frag_size_max = PAGE_SIZE;
2065 while (buf_size < byte_count) {
2066 int frag_size = byte_count - buf_size;
2068 if (i < MLX5E_MAX_RX_FRAGS - 1)
2069 frag_size = min(frag_size, frag_size_max);
2071 info->arr[i].frag_size = frag_size;
2072 info->arr[i].frag_stride = roundup_pow_of_two(frag_size);
2074 buf_size += frag_size;
2077 info->num_frags = i;
2078 /* number of different wqes sharing a page */
2079 info->wqe_bulk = 1 + (info->num_frags % 2);
2082 info->wqe_bulk = max_t(u8, info->wqe_bulk, 8);
2083 info->log_num_frags = order_base_2(info->num_frags);
2086 static inline u8 mlx5e_get_rqwq_log_stride(u8 wq_type, int ndsegs)
2088 int sz = sizeof(struct mlx5_wqe_data_seg) * ndsegs;
2091 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2092 sz += sizeof(struct mlx5e_rx_wqe_ll);
2094 default: /* MLX5_WQ_TYPE_CYCLIC */
2095 sz += sizeof(struct mlx5e_rx_wqe_cyc);
2098 return order_base_2(sz);
2101 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
2102 struct mlx5e_params *params,
2103 struct mlx5e_rq_param *param)
2105 struct mlx5_core_dev *mdev = priv->mdev;
2106 void *rqc = param->rqc;
2107 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2110 switch (params->rq_wq_type) {
2111 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2112 MLX5_SET(wq, wq, log_wqe_num_of_strides,
2113 mlx5e_mpwqe_get_log_num_strides(mdev, params) -
2114 MLX5_MPWQE_LOG_NUM_STRIDES_BASE);
2115 MLX5_SET(wq, wq, log_wqe_stride_size,
2116 mlx5e_mpwqe_get_log_stride_size(mdev, params) -
2117 MLX5_MPWQE_LOG_STRIDE_SZ_BASE);
2118 MLX5_SET(wq, wq, log_wq_sz, mlx5e_mpwqe_get_log_rq_size(params));
2120 default: /* MLX5_WQ_TYPE_CYCLIC */
2121 MLX5_SET(wq, wq, log_wq_sz, params->log_rq_mtu_frames);
2122 mlx5e_build_rq_frags_info(mdev, params, ¶m->frags_info);
2123 ndsegs = param->frags_info.num_frags;
2126 MLX5_SET(wq, wq, wq_type, params->rq_wq_type);
2127 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
2128 MLX5_SET(wq, wq, log_wq_stride,
2129 mlx5e_get_rqwq_log_stride(params->rq_wq_type, ndsegs));
2130 MLX5_SET(wq, wq, pd, mdev->mlx5e_res.pdn);
2131 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
2132 MLX5_SET(rqc, rqc, vsd, params->vlan_strip_disable);
2133 MLX5_SET(rqc, rqc, scatter_fcs, params->scatter_fcs_en);
2135 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
2138 static void mlx5e_build_drop_rq_param(struct mlx5e_priv *priv,
2139 struct mlx5e_rq_param *param)
2141 struct mlx5_core_dev *mdev = priv->mdev;
2142 void *rqc = param->rqc;
2143 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2145 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
2146 MLX5_SET(wq, wq, log_wq_stride,
2147 mlx5e_get_rqwq_log_stride(MLX5_WQ_TYPE_CYCLIC, 1));
2148 MLX5_SET(rqc, rqc, counter_set_id, priv->drop_rq_q_counter);
2150 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
2153 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
2154 struct mlx5e_sq_param *param)
2156 void *sqc = param->sqc;
2157 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2159 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
2160 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
2162 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
2165 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
2166 struct mlx5e_params *params,
2167 struct mlx5e_sq_param *param)
2169 void *sqc = param->sqc;
2170 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2172 mlx5e_build_sq_param_common(priv, param);
2173 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2174 MLX5_SET(sqc, sqc, allow_swp, !!MLX5_IPSEC_DEV(priv->mdev));
2177 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
2178 struct mlx5e_cq_param *param)
2180 void *cqc = param->cqc;
2182 MLX5_SET(cqc, cqc, uar_page, priv->mdev->priv.uar->index);
2185 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
2186 struct mlx5e_params *params,
2187 struct mlx5e_cq_param *param)
2189 struct mlx5_core_dev *mdev = priv->mdev;
2190 void *cqc = param->cqc;
2193 switch (params->rq_wq_type) {
2194 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2195 log_cq_size = mlx5e_mpwqe_get_log_rq_size(params) +
2196 mlx5e_mpwqe_get_log_num_strides(mdev, params);
2198 default: /* MLX5_WQ_TYPE_CYCLIC */
2199 log_cq_size = params->log_rq_mtu_frames;
2202 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
2203 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
2204 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
2205 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
2208 mlx5e_build_common_cq_param(priv, param);
2209 param->cq_period_mode = params->rx_cq_moderation.cq_period_mode;
2212 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
2213 struct mlx5e_params *params,
2214 struct mlx5e_cq_param *param)
2216 void *cqc = param->cqc;
2218 MLX5_SET(cqc, cqc, log_cq_size, params->log_sq_size);
2220 mlx5e_build_common_cq_param(priv, param);
2221 param->cq_period_mode = params->tx_cq_moderation.cq_period_mode;
2224 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
2226 struct mlx5e_cq_param *param)
2228 void *cqc = param->cqc;
2230 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
2232 mlx5e_build_common_cq_param(priv, param);
2234 param->cq_period_mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2237 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
2239 struct mlx5e_sq_param *param)
2241 void *sqc = param->sqc;
2242 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2244 mlx5e_build_sq_param_common(priv, param);
2246 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
2247 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
2250 static void mlx5e_build_xdpsq_param(struct mlx5e_priv *priv,
2251 struct mlx5e_params *params,
2252 struct mlx5e_sq_param *param)
2254 void *sqc = param->sqc;
2255 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2257 mlx5e_build_sq_param_common(priv, param);
2258 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2261 static void mlx5e_build_channel_param(struct mlx5e_priv *priv,
2262 struct mlx5e_params *params,
2263 struct mlx5e_channel_param *cparam)
2265 u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
2267 mlx5e_build_rq_param(priv, params, &cparam->rq);
2268 mlx5e_build_sq_param(priv, params, &cparam->sq);
2269 mlx5e_build_xdpsq_param(priv, params, &cparam->xdp_sq);
2270 mlx5e_build_icosq_param(priv, icosq_log_wq_sz, &cparam->icosq);
2271 mlx5e_build_rx_cq_param(priv, params, &cparam->rx_cq);
2272 mlx5e_build_tx_cq_param(priv, params, &cparam->tx_cq);
2273 mlx5e_build_ico_cq_param(priv, icosq_log_wq_sz, &cparam->icosq_cq);
2276 int mlx5e_open_channels(struct mlx5e_priv *priv,
2277 struct mlx5e_channels *chs)
2279 struct mlx5e_channel_param *cparam;
2283 chs->num = chs->params.num_channels;
2285 chs->c = kcalloc(chs->num, sizeof(struct mlx5e_channel *), GFP_KERNEL);
2286 cparam = kvzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
2287 if (!chs->c || !cparam)
2290 mlx5e_build_channel_param(priv, &chs->params, cparam);
2291 for (i = 0; i < chs->num; i++) {
2292 err = mlx5e_open_channel(priv, i, &chs->params, cparam, &chs->c[i]);
2294 goto err_close_channels;
2301 for (i--; i >= 0; i--)
2302 mlx5e_close_channel(chs->c[i]);
2311 static void mlx5e_activate_channels(struct mlx5e_channels *chs)
2315 for (i = 0; i < chs->num; i++)
2316 mlx5e_activate_channel(chs->c[i]);
2319 static int mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels *chs)
2324 for (i = 0; i < chs->num; i++)
2325 err |= mlx5e_wait_for_min_rx_wqes(&chs->c[i]->rq,
2328 return err ? -ETIMEDOUT : 0;
2331 static void mlx5e_deactivate_channels(struct mlx5e_channels *chs)
2335 for (i = 0; i < chs->num; i++)
2336 mlx5e_deactivate_channel(chs->c[i]);
2339 void mlx5e_close_channels(struct mlx5e_channels *chs)
2343 for (i = 0; i < chs->num; i++)
2344 mlx5e_close_channel(chs->c[i]);
2351 mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, struct mlx5e_rqt *rqt)
2353 struct mlx5_core_dev *mdev = priv->mdev;
2360 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
2361 in = kvzalloc(inlen, GFP_KERNEL);
2365 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
2367 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2368 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
2370 for (i = 0; i < sz; i++)
2371 MLX5_SET(rqtc, rqtc, rq_num[i], priv->drop_rq.rqn);
2373 err = mlx5_core_create_rqt(mdev, in, inlen, &rqt->rqtn);
2375 rqt->enabled = true;
2381 void mlx5e_destroy_rqt(struct mlx5e_priv *priv, struct mlx5e_rqt *rqt)
2383 rqt->enabled = false;
2384 mlx5_core_destroy_rqt(priv->mdev, rqt->rqtn);
2387 int mlx5e_create_indirect_rqt(struct mlx5e_priv *priv)
2389 struct mlx5e_rqt *rqt = &priv->indir_rqt;
2392 err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, rqt);
2394 mlx5_core_warn(priv->mdev, "create indirect rqts failed, %d\n", err);
2398 int mlx5e_create_direct_rqts(struct mlx5e_priv *priv)
2400 struct mlx5e_rqt *rqt;
2404 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2405 rqt = &priv->direct_tir[ix].rqt;
2406 err = mlx5e_create_rqt(priv, 1 /*size */, rqt);
2408 goto err_destroy_rqts;
2414 mlx5_core_warn(priv->mdev, "create direct rqts failed, %d\n", err);
2415 for (ix--; ix >= 0; ix--)
2416 mlx5e_destroy_rqt(priv, &priv->direct_tir[ix].rqt);
2421 void mlx5e_destroy_direct_rqts(struct mlx5e_priv *priv)
2425 for (i = 0; i < priv->profile->max_nch(priv->mdev); i++)
2426 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
2429 static int mlx5e_rx_hash_fn(int hfunc)
2431 return (hfunc == ETH_RSS_HASH_TOP) ?
2432 MLX5_RX_HASH_FN_TOEPLITZ :
2433 MLX5_RX_HASH_FN_INVERTED_XOR8;
2436 int mlx5e_bits_invert(unsigned long a, int size)
2441 for (i = 0; i < size; i++)
2442 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
2447 static void mlx5e_fill_rqt_rqns(struct mlx5e_priv *priv, int sz,
2448 struct mlx5e_redirect_rqt_param rrp, void *rqtc)
2452 for (i = 0; i < sz; i++) {
2458 if (rrp.rss.hfunc == ETH_RSS_HASH_XOR)
2459 ix = mlx5e_bits_invert(i, ilog2(sz));
2461 ix = priv->channels.params.indirection_rqt[ix];
2462 rqn = rrp.rss.channels->c[ix]->rq.rqn;
2466 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
2470 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz,
2471 struct mlx5e_redirect_rqt_param rrp)
2473 struct mlx5_core_dev *mdev = priv->mdev;
2479 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
2480 in = kvzalloc(inlen, GFP_KERNEL);
2484 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
2486 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2487 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
2488 mlx5e_fill_rqt_rqns(priv, sz, rrp, rqtc);
2489 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
2495 static u32 mlx5e_get_direct_rqn(struct mlx5e_priv *priv, int ix,
2496 struct mlx5e_redirect_rqt_param rrp)
2501 if (ix >= rrp.rss.channels->num)
2502 return priv->drop_rq.rqn;
2504 return rrp.rss.channels->c[ix]->rq.rqn;
2507 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv,
2508 struct mlx5e_redirect_rqt_param rrp)
2513 if (priv->indir_rqt.enabled) {
2515 rqtn = priv->indir_rqt.rqtn;
2516 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, rrp);
2519 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2520 struct mlx5e_redirect_rqt_param direct_rrp = {
2523 .rqn = mlx5e_get_direct_rqn(priv, ix, rrp)
2527 /* Direct RQ Tables */
2528 if (!priv->direct_tir[ix].rqt.enabled)
2531 rqtn = priv->direct_tir[ix].rqt.rqtn;
2532 mlx5e_redirect_rqt(priv, rqtn, 1, direct_rrp);
2536 static void mlx5e_redirect_rqts_to_channels(struct mlx5e_priv *priv,
2537 struct mlx5e_channels *chs)
2539 struct mlx5e_redirect_rqt_param rrp = {
2544 .hfunc = chs->params.rss_hfunc,
2549 mlx5e_redirect_rqts(priv, rrp);
2552 static void mlx5e_redirect_rqts_to_drop(struct mlx5e_priv *priv)
2554 struct mlx5e_redirect_rqt_param drop_rrp = {
2557 .rqn = priv->drop_rq.rqn,
2561 mlx5e_redirect_rqts(priv, drop_rrp);
2564 static void mlx5e_build_tir_ctx_lro(struct mlx5e_params *params, void *tirc)
2566 if (!params->lro_en)
2569 #define ROUGH_MAX_L2_L3_HDR_SZ 256
2571 MLX5_SET(tirc, tirc, lro_enable_mask,
2572 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2573 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
2574 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
2575 (params->lro_wqe_sz - ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
2576 MLX5_SET(tirc, tirc, lro_timeout_period_usecs, params->lro_timeout);
2579 void mlx5e_build_indir_tir_ctx_hash(struct mlx5e_params *params,
2580 enum mlx5e_traffic_types tt,
2581 void *tirc, bool inner)
2583 void *hfso = inner ? MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner) :
2584 MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2586 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
2587 MLX5_HASH_FIELD_SEL_DST_IP)
2589 #define MLX5_HASH_IP_L4PORTS (MLX5_HASH_FIELD_SEL_SRC_IP |\
2590 MLX5_HASH_FIELD_SEL_DST_IP |\
2591 MLX5_HASH_FIELD_SEL_L4_SPORT |\
2592 MLX5_HASH_FIELD_SEL_L4_DPORT)
2594 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
2595 MLX5_HASH_FIELD_SEL_DST_IP |\
2596 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2598 MLX5_SET(tirc, tirc, rx_hash_fn, mlx5e_rx_hash_fn(params->rss_hfunc));
2599 if (params->rss_hfunc == ETH_RSS_HASH_TOP) {
2600 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
2601 rx_hash_toeplitz_key);
2602 size_t len = MLX5_FLD_SZ_BYTES(tirc,
2603 rx_hash_toeplitz_key);
2605 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
2606 memcpy(rss_key, params->toeplitz_hash_key, len);
2610 case MLX5E_TT_IPV4_TCP:
2611 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2612 MLX5_L3_PROT_TYPE_IPV4);
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_IPV6_TCP:
2620 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2621 MLX5_L3_PROT_TYPE_IPV6);
2622 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2623 MLX5_L4_PROT_TYPE_TCP);
2624 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2625 MLX5_HASH_IP_L4PORTS);
2628 case MLX5E_TT_IPV4_UDP:
2629 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2630 MLX5_L3_PROT_TYPE_IPV4);
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_IPV6_UDP:
2638 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2639 MLX5_L3_PROT_TYPE_IPV6);
2640 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2641 MLX5_L4_PROT_TYPE_UDP);
2642 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2643 MLX5_HASH_IP_L4PORTS);
2646 case MLX5E_TT_IPV4_IPSEC_AH:
2647 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2648 MLX5_L3_PROT_TYPE_IPV4);
2649 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2650 MLX5_HASH_IP_IPSEC_SPI);
2653 case MLX5E_TT_IPV6_IPSEC_AH:
2654 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2655 MLX5_L3_PROT_TYPE_IPV6);
2656 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2657 MLX5_HASH_IP_IPSEC_SPI);
2660 case MLX5E_TT_IPV4_IPSEC_ESP:
2661 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2662 MLX5_L3_PROT_TYPE_IPV4);
2663 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2664 MLX5_HASH_IP_IPSEC_SPI);
2667 case MLX5E_TT_IPV6_IPSEC_ESP:
2668 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2669 MLX5_L3_PROT_TYPE_IPV6);
2670 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2671 MLX5_HASH_IP_IPSEC_SPI);
2675 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2676 MLX5_L3_PROT_TYPE_IPV4);
2677 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2682 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2683 MLX5_L3_PROT_TYPE_IPV6);
2684 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2688 WARN_ONCE(true, "%s: bad traffic type!\n", __func__);
2692 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
2694 struct mlx5_core_dev *mdev = priv->mdev;
2703 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
2704 in = kvzalloc(inlen, GFP_KERNEL);
2708 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
2709 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2711 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2713 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2714 err = mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in,
2720 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2721 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
2733 static void mlx5e_build_inner_indir_tir_ctx(struct mlx5e_priv *priv,
2734 enum mlx5e_traffic_types tt,
2737 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2739 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2741 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2742 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
2743 MLX5_SET(tirc, tirc, tunneled_offload_en, 0x1);
2745 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, true);
2748 static int mlx5e_set_mtu(struct mlx5_core_dev *mdev,
2749 struct mlx5e_params *params, u16 mtu)
2751 u16 hw_mtu = MLX5E_SW2HW_MTU(params, mtu);
2754 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
2758 /* Update vport context MTU */
2759 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
2763 static void mlx5e_query_mtu(struct mlx5_core_dev *mdev,
2764 struct mlx5e_params *params, u16 *mtu)
2769 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
2770 if (err || !hw_mtu) /* fallback to port oper mtu */
2771 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
2773 *mtu = MLX5E_HW2SW_MTU(params, hw_mtu);
2776 static int mlx5e_set_dev_port_mtu(struct mlx5e_priv *priv)
2778 struct mlx5e_params *params = &priv->channels.params;
2779 struct net_device *netdev = priv->netdev;
2780 struct mlx5_core_dev *mdev = priv->mdev;
2784 err = mlx5e_set_mtu(mdev, params, params->sw_mtu);
2788 mlx5e_query_mtu(mdev, params, &mtu);
2789 if (mtu != params->sw_mtu)
2790 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
2791 __func__, mtu, params->sw_mtu);
2793 params->sw_mtu = mtu;
2797 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
2799 struct mlx5e_priv *priv = netdev_priv(netdev);
2800 int nch = priv->channels.params.num_channels;
2801 int ntc = priv->channels.params.num_tc;
2804 netdev_reset_tc(netdev);
2809 netdev_set_num_tc(netdev, ntc);
2811 /* Map netdev TCs to offset 0
2812 * We have our own UP to TXQ mapping for QoS
2814 for (tc = 0; tc < ntc; tc++)
2815 netdev_set_tc_queue(netdev, tc, nch, 0);
2818 static void mlx5e_build_tc2txq_maps(struct mlx5e_priv *priv)
2820 int max_nch = priv->profile->max_nch(priv->mdev);
2823 for (i = 0; i < max_nch; i++)
2824 for (tc = 0; tc < priv->profile->max_tc; tc++)
2825 priv->channel_tc2txq[i][tc] = i + tc * max_nch;
2828 static void mlx5e_build_tx2sq_maps(struct mlx5e_priv *priv)
2830 struct mlx5e_channel *c;
2831 struct mlx5e_txqsq *sq;
2834 for (i = 0; i < priv->channels.num; i++) {
2835 c = priv->channels.c[i];
2836 for (tc = 0; tc < c->num_tc; tc++) {
2838 priv->txq2sq[sq->txq_ix] = sq;
2843 void mlx5e_activate_priv_channels(struct mlx5e_priv *priv)
2845 int num_txqs = priv->channels.num * priv->channels.params.num_tc;
2846 struct net_device *netdev = priv->netdev;
2848 mlx5e_netdev_set_tcs(netdev);
2849 netif_set_real_num_tx_queues(netdev, num_txqs);
2850 netif_set_real_num_rx_queues(netdev, priv->channels.num);
2852 mlx5e_build_tx2sq_maps(priv);
2853 mlx5e_activate_channels(&priv->channels);
2854 netif_tx_start_all_queues(priv->netdev);
2856 if (MLX5_ESWITCH_MANAGER(priv->mdev))
2857 mlx5e_add_sqs_fwd_rules(priv);
2859 mlx5e_wait_channels_min_rx_wqes(&priv->channels);
2860 mlx5e_redirect_rqts_to_channels(priv, &priv->channels);
2863 void mlx5e_deactivate_priv_channels(struct mlx5e_priv *priv)
2865 mlx5e_redirect_rqts_to_drop(priv);
2867 if (MLX5_ESWITCH_MANAGER(priv->mdev))
2868 mlx5e_remove_sqs_fwd_rules(priv);
2870 /* FIXME: This is a W/A only for tx timeout watch dog false alarm when
2871 * polling for inactive tx queues.
2873 netif_tx_stop_all_queues(priv->netdev);
2874 netif_tx_disable(priv->netdev);
2875 mlx5e_deactivate_channels(&priv->channels);
2878 void mlx5e_switch_priv_channels(struct mlx5e_priv *priv,
2879 struct mlx5e_channels *new_chs,
2880 mlx5e_fp_hw_modify hw_modify)
2882 struct net_device *netdev = priv->netdev;
2885 new_num_txqs = new_chs->num * new_chs->params.num_tc;
2887 carrier_ok = netif_carrier_ok(netdev);
2888 netif_carrier_off(netdev);
2890 if (new_num_txqs < netdev->real_num_tx_queues)
2891 netif_set_real_num_tx_queues(netdev, new_num_txqs);
2893 mlx5e_deactivate_priv_channels(priv);
2894 mlx5e_close_channels(&priv->channels);
2896 priv->channels = *new_chs;
2898 /* New channels are ready to roll, modify HW settings if needed */
2902 mlx5e_refresh_tirs(priv, false);
2903 mlx5e_activate_priv_channels(priv);
2905 /* return carrier back if needed */
2907 netif_carrier_on(netdev);
2910 void mlx5e_timestamp_init(struct mlx5e_priv *priv)
2912 priv->tstamp.tx_type = HWTSTAMP_TX_OFF;
2913 priv->tstamp.rx_filter = HWTSTAMP_FILTER_NONE;
2916 int mlx5e_open_locked(struct net_device *netdev)
2918 struct mlx5e_priv *priv = netdev_priv(netdev);
2921 set_bit(MLX5E_STATE_OPENED, &priv->state);
2923 err = mlx5e_open_channels(priv, &priv->channels);
2925 goto err_clear_state_opened_flag;
2927 mlx5e_refresh_tirs(priv, false);
2928 mlx5e_activate_priv_channels(priv);
2929 if (priv->profile->update_carrier)
2930 priv->profile->update_carrier(priv);
2932 if (priv->profile->update_stats)
2933 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
2937 err_clear_state_opened_flag:
2938 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2942 int mlx5e_open(struct net_device *netdev)
2944 struct mlx5e_priv *priv = netdev_priv(netdev);
2947 mutex_lock(&priv->state_lock);
2948 err = mlx5e_open_locked(netdev);
2950 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_UP);
2951 mutex_unlock(&priv->state_lock);
2953 if (mlx5e_vxlan_allowed(priv->mdev))
2954 udp_tunnel_get_rx_info(netdev);
2959 int mlx5e_close_locked(struct net_device *netdev)
2961 struct mlx5e_priv *priv = netdev_priv(netdev);
2963 /* May already be CLOSED in case a previous configuration operation
2964 * (e.g RX/TX queue size change) that involves close&open failed.
2966 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2969 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2971 netif_carrier_off(priv->netdev);
2972 mlx5e_deactivate_priv_channels(priv);
2973 mlx5e_close_channels(&priv->channels);
2978 int mlx5e_close(struct net_device *netdev)
2980 struct mlx5e_priv *priv = netdev_priv(netdev);
2983 if (!netif_device_present(netdev))
2986 mutex_lock(&priv->state_lock);
2987 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_DOWN);
2988 err = mlx5e_close_locked(netdev);
2989 mutex_unlock(&priv->state_lock);
2994 static int mlx5e_alloc_drop_rq(struct mlx5_core_dev *mdev,
2995 struct mlx5e_rq *rq,
2996 struct mlx5e_rq_param *param)
2998 void *rqc = param->rqc;
2999 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
3002 param->wq.db_numa_node = param->wq.buf_numa_node;
3004 err = mlx5_wq_cyc_create(mdev, ¶m->wq, rqc_wq, &rq->wqe.wq,
3009 /* Mark as unused given "Drop-RQ" packets never reach XDP */
3010 xdp_rxq_info_unused(&rq->xdp_rxq);
3017 static int mlx5e_alloc_drop_cq(struct mlx5_core_dev *mdev,
3018 struct mlx5e_cq *cq,
3019 struct mlx5e_cq_param *param)
3021 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
3022 param->wq.db_numa_node = dev_to_node(&mdev->pdev->dev);
3024 return mlx5e_alloc_cq_common(mdev, param, cq);
3027 static int mlx5e_open_drop_rq(struct mlx5e_priv *priv,
3028 struct mlx5e_rq *drop_rq)
3030 struct mlx5_core_dev *mdev = priv->mdev;
3031 struct mlx5e_cq_param cq_param = {};
3032 struct mlx5e_rq_param rq_param = {};
3033 struct mlx5e_cq *cq = &drop_rq->cq;
3036 mlx5e_build_drop_rq_param(priv, &rq_param);
3038 err = mlx5e_alloc_drop_cq(mdev, cq, &cq_param);
3042 err = mlx5e_create_cq(cq, &cq_param);
3046 err = mlx5e_alloc_drop_rq(mdev, drop_rq, &rq_param);
3048 goto err_destroy_cq;
3050 err = mlx5e_create_rq(drop_rq, &rq_param);
3054 err = mlx5e_modify_rq_state(drop_rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
3056 mlx5_core_warn(priv->mdev, "modify_rq_state failed, rx_if_down_packets won't be counted %d\n", err);
3061 mlx5e_free_rq(drop_rq);
3064 mlx5e_destroy_cq(cq);
3072 static void mlx5e_close_drop_rq(struct mlx5e_rq *drop_rq)
3074 mlx5e_destroy_rq(drop_rq);
3075 mlx5e_free_rq(drop_rq);
3076 mlx5e_destroy_cq(&drop_rq->cq);
3077 mlx5e_free_cq(&drop_rq->cq);
3080 int mlx5e_create_tis(struct mlx5_core_dev *mdev, int tc,
3081 u32 underlay_qpn, u32 *tisn)
3083 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
3084 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
3086 MLX5_SET(tisc, tisc, prio, tc << 1);
3087 MLX5_SET(tisc, tisc, underlay_qpn, underlay_qpn);
3088 MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.td.tdn);
3090 if (mlx5_lag_is_lacp_owner(mdev))
3091 MLX5_SET(tisc, tisc, strict_lag_tx_port_affinity, 1);
3093 return mlx5_core_create_tis(mdev, in, sizeof(in), tisn);
3096 void mlx5e_destroy_tis(struct mlx5_core_dev *mdev, u32 tisn)
3098 mlx5_core_destroy_tis(mdev, tisn);
3101 int mlx5e_create_tises(struct mlx5e_priv *priv)
3106 for (tc = 0; tc < priv->profile->max_tc; tc++) {
3107 err = mlx5e_create_tis(priv->mdev, tc, 0, &priv->tisn[tc]);
3109 goto err_close_tises;
3115 for (tc--; tc >= 0; tc--)
3116 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
3121 void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
3125 for (tc = 0; tc < priv->profile->max_tc; tc++)
3126 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
3129 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv,
3130 enum mlx5e_traffic_types tt,
3133 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
3135 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
3137 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
3138 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
3139 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, false);
3142 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 rqtn, u32 *tirc)
3144 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
3146 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
3148 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
3149 MLX5_SET(tirc, tirc, indirect_table, rqtn);
3150 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
3153 int mlx5e_create_indirect_tirs(struct mlx5e_priv *priv)
3155 struct mlx5e_tir *tir;
3163 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3164 in = kvzalloc(inlen, GFP_KERNEL);
3168 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
3169 memset(in, 0, inlen);
3170 tir = &priv->indir_tir[tt];
3171 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3172 mlx5e_build_indir_tir_ctx(priv, tt, tirc);
3173 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3175 mlx5_core_warn(priv->mdev, "create indirect tirs failed, %d\n", err);
3176 goto err_destroy_inner_tirs;
3180 if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
3183 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
3184 memset(in, 0, inlen);
3185 tir = &priv->inner_indir_tir[i];
3186 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3187 mlx5e_build_inner_indir_tir_ctx(priv, i, tirc);
3188 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3190 mlx5_core_warn(priv->mdev, "create inner indirect tirs failed, %d\n", err);
3191 goto err_destroy_inner_tirs;
3200 err_destroy_inner_tirs:
3201 for (i--; i >= 0; i--)
3202 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3204 for (tt--; tt >= 0; tt--)
3205 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[tt]);
3212 int mlx5e_create_direct_tirs(struct mlx5e_priv *priv)
3214 int nch = priv->profile->max_nch(priv->mdev);
3215 struct mlx5e_tir *tir;
3222 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3223 in = kvzalloc(inlen, GFP_KERNEL);
3227 for (ix = 0; ix < nch; ix++) {
3228 memset(in, 0, inlen);
3229 tir = &priv->direct_tir[ix];
3230 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3231 mlx5e_build_direct_tir_ctx(priv, priv->direct_tir[ix].rqt.rqtn, tirc);
3232 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3234 goto err_destroy_ch_tirs;
3241 err_destroy_ch_tirs:
3242 mlx5_core_warn(priv->mdev, "create direct tirs failed, %d\n", err);
3243 for (ix--; ix >= 0; ix--)
3244 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[ix]);
3251 void mlx5e_destroy_indirect_tirs(struct mlx5e_priv *priv)
3255 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3256 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[i]);
3258 if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
3261 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3262 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3265 void mlx5e_destroy_direct_tirs(struct mlx5e_priv *priv)
3267 int nch = priv->profile->max_nch(priv->mdev);
3270 for (i = 0; i < nch; i++)
3271 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[i]);
3274 static int mlx5e_modify_channels_scatter_fcs(struct mlx5e_channels *chs, bool enable)
3279 for (i = 0; i < chs->num; i++) {
3280 err = mlx5e_modify_rq_scatter_fcs(&chs->c[i]->rq, enable);
3288 static int mlx5e_modify_channels_vsd(struct mlx5e_channels *chs, bool vsd)
3293 for (i = 0; i < chs->num; i++) {
3294 err = mlx5e_modify_rq_vsd(&chs->c[i]->rq, vsd);
3302 static int mlx5e_setup_tc_mqprio(struct net_device *netdev,
3303 struct tc_mqprio_qopt *mqprio)
3305 struct mlx5e_priv *priv = netdev_priv(netdev);
3306 struct mlx5e_channels new_channels = {};
3307 u8 tc = mqprio->num_tc;
3310 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
3312 if (tc && tc != MLX5E_MAX_NUM_TC)
3315 mutex_lock(&priv->state_lock);
3317 new_channels.params = priv->channels.params;
3318 new_channels.params.num_tc = tc ? tc : 1;
3320 if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
3321 priv->channels.params = new_channels.params;
3325 err = mlx5e_open_channels(priv, &new_channels);
3329 priv->max_opened_tc = max_t(u8, priv->max_opened_tc,
3330 new_channels.params.num_tc);
3331 mlx5e_switch_priv_channels(priv, &new_channels, NULL);
3333 mutex_unlock(&priv->state_lock);
3337 #ifdef CONFIG_MLX5_ESWITCH
3338 static int mlx5e_setup_tc_cls_flower(struct mlx5e_priv *priv,
3339 struct tc_cls_flower_offload *cls_flower,
3342 switch (cls_flower->command) {
3343 case TC_CLSFLOWER_REPLACE:
3344 return mlx5e_configure_flower(priv, cls_flower, flags);
3345 case TC_CLSFLOWER_DESTROY:
3346 return mlx5e_delete_flower(priv, cls_flower, flags);
3347 case TC_CLSFLOWER_STATS:
3348 return mlx5e_stats_flower(priv, cls_flower, flags);
3354 static int mlx5e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
3357 struct mlx5e_priv *priv = cb_priv;
3359 if (!tc_cls_can_offload_and_chain0(priv->netdev, type_data))
3363 case TC_SETUP_CLSFLOWER:
3364 return mlx5e_setup_tc_cls_flower(priv, type_data, MLX5E_TC_INGRESS);
3370 static int mlx5e_setup_tc_block(struct net_device *dev,
3371 struct tc_block_offload *f)
3373 struct mlx5e_priv *priv = netdev_priv(dev);
3375 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
3378 switch (f->command) {
3380 return tcf_block_cb_register(f->block, mlx5e_setup_tc_block_cb,
3381 priv, priv, f->extack);
3382 case TC_BLOCK_UNBIND:
3383 tcf_block_cb_unregister(f->block, mlx5e_setup_tc_block_cb,
3392 static int mlx5e_setup_tc(struct net_device *dev, enum tc_setup_type type,
3396 #ifdef CONFIG_MLX5_ESWITCH
3397 case TC_SETUP_BLOCK:
3398 return mlx5e_setup_tc_block(dev, type_data);
3400 case TC_SETUP_QDISC_MQPRIO:
3401 return mlx5e_setup_tc_mqprio(dev, type_data);
3408 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
3410 struct mlx5e_priv *priv = netdev_priv(dev);
3411 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
3412 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
3413 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
3415 /* update HW stats in background for next time */
3416 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
3418 if (mlx5e_is_uplink_rep(priv)) {
3419 stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
3420 stats->rx_bytes = PPORT_802_3_GET(pstats, a_octets_received_ok);
3421 stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
3422 stats->tx_bytes = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
3424 mlx5e_grp_sw_update_stats(priv);
3425 stats->rx_packets = sstats->rx_packets;
3426 stats->rx_bytes = sstats->rx_bytes;
3427 stats->tx_packets = sstats->tx_packets;
3428 stats->tx_bytes = sstats->tx_bytes;
3429 stats->tx_dropped = sstats->tx_queue_dropped;
3432 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
3434 stats->rx_length_errors =
3435 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
3436 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
3437 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
3438 stats->rx_crc_errors =
3439 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
3440 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
3441 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
3442 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
3443 stats->rx_frame_errors;
3444 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
3446 /* vport multicast also counts packets that are dropped due to steering
3447 * or rx out of buffer
3450 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
3453 static void mlx5e_set_rx_mode(struct net_device *dev)
3455 struct mlx5e_priv *priv = netdev_priv(dev);
3457 queue_work(priv->wq, &priv->set_rx_mode_work);
3460 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
3462 struct mlx5e_priv *priv = netdev_priv(netdev);
3463 struct sockaddr *saddr = addr;
3465 if (!is_valid_ether_addr(saddr->sa_data))
3466 return -EADDRNOTAVAIL;
3468 netif_addr_lock_bh(netdev);
3469 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
3470 netif_addr_unlock_bh(netdev);
3472 queue_work(priv->wq, &priv->set_rx_mode_work);
3477 #define MLX5E_SET_FEATURE(features, feature, enable) \
3480 *features |= feature; \
3482 *features &= ~feature; \
3485 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
3487 static int set_feature_lro(struct net_device *netdev, bool enable)
3489 struct mlx5e_priv *priv = netdev_priv(netdev);
3490 struct mlx5_core_dev *mdev = priv->mdev;
3491 struct mlx5e_channels new_channels = {};
3492 struct mlx5e_params *old_params;
3496 mutex_lock(&priv->state_lock);
3498 old_params = &priv->channels.params;
3499 if (enable && !MLX5E_GET_PFLAG(old_params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
3500 netdev_warn(netdev, "can't set LRO with legacy RQ\n");
3505 reset = test_bit(MLX5E_STATE_OPENED, &priv->state);
3507 new_channels.params = *old_params;
3508 new_channels.params.lro_en = enable;
3510 if (old_params->rq_wq_type != MLX5_WQ_TYPE_CYCLIC) {
3511 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, old_params) ==
3512 mlx5e_rx_mpwqe_is_linear_skb(mdev, &new_channels.params))
3517 *old_params = new_channels.params;
3518 err = mlx5e_modify_tirs_lro(priv);
3522 err = mlx5e_open_channels(priv, &new_channels);
3526 mlx5e_switch_priv_channels(priv, &new_channels, mlx5e_modify_tirs_lro);
3528 mutex_unlock(&priv->state_lock);
3532 static int set_feature_cvlan_filter(struct net_device *netdev, bool enable)
3534 struct mlx5e_priv *priv = netdev_priv(netdev);
3537 mlx5e_enable_cvlan_filter(priv);
3539 mlx5e_disable_cvlan_filter(priv);
3544 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
3546 struct mlx5e_priv *priv = netdev_priv(netdev);
3548 if (!enable && mlx5e_tc_num_filters(priv)) {
3550 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
3557 static int set_feature_rx_all(struct net_device *netdev, bool enable)
3559 struct mlx5e_priv *priv = netdev_priv(netdev);
3560 struct mlx5_core_dev *mdev = priv->mdev;
3562 return mlx5_set_port_fcs(mdev, !enable);
3565 static int set_feature_rx_fcs(struct net_device *netdev, bool enable)
3567 struct mlx5e_priv *priv = netdev_priv(netdev);
3570 mutex_lock(&priv->state_lock);
3572 priv->channels.params.scatter_fcs_en = enable;
3573 err = mlx5e_modify_channels_scatter_fcs(&priv->channels, enable);
3575 priv->channels.params.scatter_fcs_en = !enable;
3577 mutex_unlock(&priv->state_lock);
3582 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
3584 struct mlx5e_priv *priv = netdev_priv(netdev);
3587 mutex_lock(&priv->state_lock);
3589 priv->channels.params.vlan_strip_disable = !enable;
3590 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3593 err = mlx5e_modify_channels_vsd(&priv->channels, !enable);
3595 priv->channels.params.vlan_strip_disable = enable;
3598 mutex_unlock(&priv->state_lock);
3603 #ifdef CONFIG_RFS_ACCEL
3604 static int set_feature_arfs(struct net_device *netdev, bool enable)
3606 struct mlx5e_priv *priv = netdev_priv(netdev);
3610 err = mlx5e_arfs_enable(priv);
3612 err = mlx5e_arfs_disable(priv);
3618 static int mlx5e_handle_feature(struct net_device *netdev,
3619 netdev_features_t *features,
3620 netdev_features_t wanted_features,
3621 netdev_features_t feature,
3622 mlx5e_feature_handler feature_handler)
3624 netdev_features_t changes = wanted_features ^ netdev->features;
3625 bool enable = !!(wanted_features & feature);
3628 if (!(changes & feature))
3631 err = feature_handler(netdev, enable);
3633 netdev_err(netdev, "%s feature %pNF failed, err %d\n",
3634 enable ? "Enable" : "Disable", &feature, err);
3638 MLX5E_SET_FEATURE(features, feature, enable);
3642 static int mlx5e_set_features(struct net_device *netdev,
3643 netdev_features_t features)
3645 netdev_features_t oper_features = netdev->features;
3648 #define MLX5E_HANDLE_FEATURE(feature, handler) \
3649 mlx5e_handle_feature(netdev, &oper_features, features, feature, handler)
3651 err |= MLX5E_HANDLE_FEATURE(NETIF_F_LRO, set_feature_lro);
3652 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_FILTER,
3653 set_feature_cvlan_filter);
3654 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_TC, set_feature_tc_num_filters);
3655 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXALL, set_feature_rx_all);
3656 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXFCS, set_feature_rx_fcs);
3657 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_RX, set_feature_rx_vlan);
3658 #ifdef CONFIG_RFS_ACCEL
3659 err |= MLX5E_HANDLE_FEATURE(NETIF_F_NTUPLE, set_feature_arfs);
3663 netdev->features = oper_features;
3670 static netdev_features_t mlx5e_fix_features(struct net_device *netdev,
3671 netdev_features_t features)
3673 struct mlx5e_priv *priv = netdev_priv(netdev);
3674 struct mlx5e_params *params;
3676 mutex_lock(&priv->state_lock);
3677 params = &priv->channels.params;
3678 if (!bitmap_empty(priv->fs.vlan.active_svlans, VLAN_N_VID)) {
3679 /* HW strips the outer C-tag header, this is a problem
3680 * for S-tag traffic.
3682 features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3683 if (!params->vlan_strip_disable)
3684 netdev_warn(netdev, "Dropping C-tag vlan stripping offload due to S-tag vlan\n");
3686 if (!MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
3687 features &= ~NETIF_F_LRO;
3689 netdev_warn(netdev, "Disabling LRO, not supported in legacy RQ\n");
3692 mutex_unlock(&priv->state_lock);
3697 int mlx5e_change_mtu(struct net_device *netdev, int new_mtu,
3698 change_hw_mtu_cb set_mtu_cb)
3700 struct mlx5e_priv *priv = netdev_priv(netdev);
3701 struct mlx5e_channels new_channels = {};
3702 struct mlx5e_params *params;
3706 mutex_lock(&priv->state_lock);
3708 params = &priv->channels.params;
3710 reset = !params->lro_en;
3711 reset = reset && test_bit(MLX5E_STATE_OPENED, &priv->state);
3713 new_channels.params = *params;
3714 new_channels.params.sw_mtu = new_mtu;
3716 if (params->xdp_prog &&
3717 !mlx5e_rx_is_linear_skb(priv->mdev, &new_channels.params)) {
3718 netdev_err(netdev, "MTU(%d) > %d is not allowed while XDP enabled\n",
3719 new_mtu, MLX5E_XDP_MAX_MTU);
3724 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
3725 u8 ppw_old = mlx5e_mpwqe_log_pkts_per_wqe(params);
3726 u8 ppw_new = mlx5e_mpwqe_log_pkts_per_wqe(&new_channels.params);
3728 reset = reset && (ppw_old != ppw_new);
3732 params->sw_mtu = new_mtu;
3734 netdev->mtu = params->sw_mtu;
3738 err = mlx5e_open_channels(priv, &new_channels);
3742 mlx5e_switch_priv_channels(priv, &new_channels, set_mtu_cb);
3743 netdev->mtu = new_channels.params.sw_mtu;
3746 mutex_unlock(&priv->state_lock);
3750 static int mlx5e_change_nic_mtu(struct net_device *netdev, int new_mtu)
3752 return mlx5e_change_mtu(netdev, new_mtu, mlx5e_set_dev_port_mtu);
3755 int mlx5e_hwstamp_set(struct mlx5e_priv *priv, struct ifreq *ifr)
3757 struct hwtstamp_config config;
3760 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
3763 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
3766 /* TX HW timestamp */
3767 switch (config.tx_type) {
3768 case HWTSTAMP_TX_OFF:
3769 case HWTSTAMP_TX_ON:
3775 mutex_lock(&priv->state_lock);
3776 /* RX HW timestamp */
3777 switch (config.rx_filter) {
3778 case HWTSTAMP_FILTER_NONE:
3779 /* Reset CQE compression to Admin default */
3780 mlx5e_modify_rx_cqe_compression_locked(priv, priv->channels.params.rx_cqe_compress_def);
3782 case HWTSTAMP_FILTER_ALL:
3783 case HWTSTAMP_FILTER_SOME:
3784 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3785 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3786 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3787 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3788 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3789 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3790 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3791 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3792 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3793 case HWTSTAMP_FILTER_PTP_V2_EVENT:
3794 case HWTSTAMP_FILTER_PTP_V2_SYNC:
3795 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3796 case HWTSTAMP_FILTER_NTP_ALL:
3797 /* Disable CQE compression */
3798 netdev_warn(priv->netdev, "Disabling cqe compression");
3799 err = mlx5e_modify_rx_cqe_compression_locked(priv, false);
3801 netdev_err(priv->netdev, "Failed disabling cqe compression err=%d\n", err);
3802 mutex_unlock(&priv->state_lock);
3805 config.rx_filter = HWTSTAMP_FILTER_ALL;
3808 mutex_unlock(&priv->state_lock);
3812 memcpy(&priv->tstamp, &config, sizeof(config));
3813 mutex_unlock(&priv->state_lock);
3815 return copy_to_user(ifr->ifr_data, &config,
3816 sizeof(config)) ? -EFAULT : 0;
3819 int mlx5e_hwstamp_get(struct mlx5e_priv *priv, struct ifreq *ifr)
3821 struct hwtstamp_config *cfg = &priv->tstamp;
3823 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
3826 return copy_to_user(ifr->ifr_data, cfg, sizeof(*cfg)) ? -EFAULT : 0;
3829 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3831 struct mlx5e_priv *priv = netdev_priv(dev);
3835 return mlx5e_hwstamp_set(priv, ifr);
3837 return mlx5e_hwstamp_get(priv, ifr);
3843 #ifdef CONFIG_MLX5_ESWITCH
3844 static int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
3846 struct mlx5e_priv *priv = netdev_priv(dev);
3847 struct mlx5_core_dev *mdev = priv->mdev;
3849 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
3852 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
3855 struct mlx5e_priv *priv = netdev_priv(dev);
3856 struct mlx5_core_dev *mdev = priv->mdev;
3858 if (vlan_proto != htons(ETH_P_8021Q))
3859 return -EPROTONOSUPPORT;
3861 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
3865 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
3867 struct mlx5e_priv *priv = netdev_priv(dev);
3868 struct mlx5_core_dev *mdev = priv->mdev;
3870 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
3873 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
3875 struct mlx5e_priv *priv = netdev_priv(dev);
3876 struct mlx5_core_dev *mdev = priv->mdev;
3878 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
3881 static int mlx5e_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
3884 struct mlx5e_priv *priv = netdev_priv(dev);
3885 struct mlx5_core_dev *mdev = priv->mdev;
3887 return mlx5_eswitch_set_vport_rate(mdev->priv.eswitch, vf + 1,
3888 max_tx_rate, min_tx_rate);
3891 static int mlx5_vport_link2ifla(u8 esw_link)
3894 case MLX5_ESW_VPORT_ADMIN_STATE_DOWN:
3895 return IFLA_VF_LINK_STATE_DISABLE;
3896 case MLX5_ESW_VPORT_ADMIN_STATE_UP:
3897 return IFLA_VF_LINK_STATE_ENABLE;
3899 return IFLA_VF_LINK_STATE_AUTO;
3902 static int mlx5_ifla_link2vport(u8 ifla_link)
3904 switch (ifla_link) {
3905 case IFLA_VF_LINK_STATE_DISABLE:
3906 return MLX5_ESW_VPORT_ADMIN_STATE_DOWN;
3907 case IFLA_VF_LINK_STATE_ENABLE:
3908 return MLX5_ESW_VPORT_ADMIN_STATE_UP;
3910 return MLX5_ESW_VPORT_ADMIN_STATE_AUTO;
3913 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
3916 struct mlx5e_priv *priv = netdev_priv(dev);
3917 struct mlx5_core_dev *mdev = priv->mdev;
3919 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
3920 mlx5_ifla_link2vport(link_state));
3923 static int mlx5e_get_vf_config(struct net_device *dev,
3924 int vf, struct ifla_vf_info *ivi)
3926 struct mlx5e_priv *priv = netdev_priv(dev);
3927 struct mlx5_core_dev *mdev = priv->mdev;
3930 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
3933 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
3937 static int mlx5e_get_vf_stats(struct net_device *dev,
3938 int vf, struct ifla_vf_stats *vf_stats)
3940 struct mlx5e_priv *priv = netdev_priv(dev);
3941 struct mlx5_core_dev *mdev = priv->mdev;
3943 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
3948 static void mlx5e_add_vxlan_port(struct net_device *netdev,
3949 struct udp_tunnel_info *ti)
3951 struct mlx5e_priv *priv = netdev_priv(netdev);
3953 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3956 if (!mlx5e_vxlan_allowed(priv->mdev))
3959 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 1);
3962 static void mlx5e_del_vxlan_port(struct net_device *netdev,
3963 struct udp_tunnel_info *ti)
3965 struct mlx5e_priv *priv = netdev_priv(netdev);
3967 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3970 if (!mlx5e_vxlan_allowed(priv->mdev))
3973 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 0);
3976 static netdev_features_t mlx5e_tunnel_features_check(struct mlx5e_priv *priv,
3977 struct sk_buff *skb,
3978 netdev_features_t features)
3980 unsigned int offset = 0;
3981 struct udphdr *udph;
3985 switch (vlan_get_protocol(skb)) {
3986 case htons(ETH_P_IP):
3987 proto = ip_hdr(skb)->protocol;
3989 case htons(ETH_P_IPV6):
3990 proto = ipv6_find_hdr(skb, &offset, -1, NULL, NULL);
4000 udph = udp_hdr(skb);
4001 port = be16_to_cpu(udph->dest);
4003 /* Verify if UDP port is being offloaded by HW */
4004 if (mlx5e_vxlan_lookup_port(priv, port))
4009 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
4010 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4013 static netdev_features_t mlx5e_features_check(struct sk_buff *skb,
4014 struct net_device *netdev,
4015 netdev_features_t features)
4017 struct mlx5e_priv *priv = netdev_priv(netdev);
4019 features = vlan_features_check(skb, features);
4020 features = vxlan_features_check(skb, features);
4022 #ifdef CONFIG_MLX5_EN_IPSEC
4023 if (mlx5e_ipsec_feature_check(skb, netdev, features))
4027 /* Validate if the tunneled packet is being offloaded by HW */
4028 if (skb->encapsulation &&
4029 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
4030 return mlx5e_tunnel_features_check(priv, skb, features);
4035 static bool mlx5e_tx_timeout_eq_recover(struct net_device *dev,
4036 struct mlx5e_txqsq *sq)
4038 struct mlx5_eq *eq = sq->cq.mcq.eq;
4041 netdev_err(dev, "EQ 0x%x: Cons = 0x%x, irqn = 0x%x\n",
4042 eq->eqn, eq->cons_index, eq->irqn);
4044 eqe_count = mlx5_eq_poll_irq_disabled(eq);
4048 netdev_err(dev, "Recover %d eqes on EQ 0x%x\n", eqe_count, eq->eqn);
4049 sq->channel->stats->eq_rearm++;
4053 static void mlx5e_tx_timeout_work(struct work_struct *work)
4055 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
4057 struct net_device *dev = priv->netdev;
4058 bool reopen_channels = false;
4062 mutex_lock(&priv->state_lock);
4064 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
4067 for (i = 0; i < priv->channels.num * priv->channels.params.num_tc; i++) {
4068 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, i);
4069 struct mlx5e_txqsq *sq = priv->txq2sq[i];
4071 if (!netif_xmit_stopped(dev_queue))
4075 "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",
4076 i, sq->sqn, sq->cq.mcq.cqn, sq->cc, sq->pc,
4077 jiffies_to_usecs(jiffies - dev_queue->trans_start));
4079 /* If we recover a lost interrupt, most likely TX timeout will
4080 * be resolved, skip reopening channels
4082 if (!mlx5e_tx_timeout_eq_recover(dev, sq)) {
4083 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
4084 reopen_channels = true;
4088 if (!reopen_channels)
4091 mlx5e_close_locked(dev);
4092 err = mlx5e_open_locked(dev);
4094 netdev_err(priv->netdev,
4095 "mlx5e_open_locked failed recovering from a tx_timeout, err(%d).\n",
4099 mutex_unlock(&priv->state_lock);
4103 static void mlx5e_tx_timeout(struct net_device *dev)
4105 struct mlx5e_priv *priv = netdev_priv(dev);
4107 netdev_err(dev, "TX timeout detected\n");
4108 queue_work(priv->wq, &priv->tx_timeout_work);
4111 static int mlx5e_xdp_allowed(struct mlx5e_priv *priv, struct bpf_prog *prog)
4113 struct net_device *netdev = priv->netdev;
4114 struct mlx5e_channels new_channels = {};
4116 if (priv->channels.params.lro_en) {
4117 netdev_warn(netdev, "can't set XDP while LRO is on, disable LRO first\n");
4121 if (MLX5_IPSEC_DEV(priv->mdev)) {
4122 netdev_warn(netdev, "can't set XDP with IPSec offload\n");
4126 new_channels.params = priv->channels.params;
4127 new_channels.params.xdp_prog = prog;
4129 if (!mlx5e_rx_is_linear_skb(priv->mdev, &new_channels.params)) {
4130 netdev_warn(netdev, "XDP is not allowed with MTU(%d) > %d\n",
4131 new_channels.params.sw_mtu, MLX5E_XDP_MAX_MTU);
4138 static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog)
4140 struct mlx5e_priv *priv = netdev_priv(netdev);
4141 struct bpf_prog *old_prog;
4142 bool reset, was_opened;
4146 mutex_lock(&priv->state_lock);
4149 err = mlx5e_xdp_allowed(priv, prog);
4154 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
4155 /* no need for full reset when exchanging programs */
4156 reset = (!priv->channels.params.xdp_prog || !prog);
4158 if (was_opened && reset)
4159 mlx5e_close_locked(netdev);
4160 if (was_opened && !reset) {
4161 /* num_channels is invariant here, so we can take the
4162 * batched reference right upfront.
4164 prog = bpf_prog_add(prog, priv->channels.num);
4166 err = PTR_ERR(prog);
4171 /* exchange programs, extra prog reference we got from caller
4172 * as long as we don't fail from this point onwards.
4174 old_prog = xchg(&priv->channels.params.xdp_prog, prog);
4176 bpf_prog_put(old_prog);
4178 if (reset) /* change RQ type according to priv->xdp_prog */
4179 mlx5e_set_rq_type(priv->mdev, &priv->channels.params);
4181 if (was_opened && reset)
4182 mlx5e_open_locked(netdev);
4184 if (!test_bit(MLX5E_STATE_OPENED, &priv->state) || reset)
4187 /* exchanging programs w/o reset, we update ref counts on behalf
4188 * of the channels RQs here.
4190 for (i = 0; i < priv->channels.num; i++) {
4191 struct mlx5e_channel *c = priv->channels.c[i];
4193 clear_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
4194 napi_synchronize(&c->napi);
4195 /* prevent mlx5e_poll_rx_cq from accessing rq->xdp_prog */
4197 old_prog = xchg(&c->rq.xdp_prog, prog);
4199 set_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
4200 /* napi_schedule in case we have missed anything */
4201 napi_schedule(&c->napi);
4204 bpf_prog_put(old_prog);
4208 mutex_unlock(&priv->state_lock);
4212 static u32 mlx5e_xdp_query(struct net_device *dev)
4214 struct mlx5e_priv *priv = netdev_priv(dev);
4215 const struct bpf_prog *xdp_prog;
4218 mutex_lock(&priv->state_lock);
4219 xdp_prog = priv->channels.params.xdp_prog;
4221 prog_id = xdp_prog->aux->id;
4222 mutex_unlock(&priv->state_lock);
4227 static int mlx5e_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4229 switch (xdp->command) {
4230 case XDP_SETUP_PROG:
4231 return mlx5e_xdp_set(dev, xdp->prog);
4232 case XDP_QUERY_PROG:
4233 xdp->prog_id = mlx5e_xdp_query(dev);
4240 #ifdef CONFIG_NET_POLL_CONTROLLER
4241 /* Fake "interrupt" called by netpoll (eg netconsole) to send skbs without
4242 * reenabling interrupts.
4244 static void mlx5e_netpoll(struct net_device *dev)
4246 struct mlx5e_priv *priv = netdev_priv(dev);
4247 struct mlx5e_channels *chs = &priv->channels;
4251 for (i = 0; i < chs->num; i++)
4252 napi_schedule(&chs->c[i]->napi);
4256 static const struct net_device_ops mlx5e_netdev_ops = {
4257 .ndo_open = mlx5e_open,
4258 .ndo_stop = mlx5e_close,
4259 .ndo_start_xmit = mlx5e_xmit,
4260 .ndo_setup_tc = mlx5e_setup_tc,
4261 .ndo_select_queue = mlx5e_select_queue,
4262 .ndo_get_stats64 = mlx5e_get_stats,
4263 .ndo_set_rx_mode = mlx5e_set_rx_mode,
4264 .ndo_set_mac_address = mlx5e_set_mac,
4265 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
4266 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
4267 .ndo_set_features = mlx5e_set_features,
4268 .ndo_fix_features = mlx5e_fix_features,
4269 .ndo_change_mtu = mlx5e_change_nic_mtu,
4270 .ndo_do_ioctl = mlx5e_ioctl,
4271 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
4272 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
4273 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
4274 .ndo_features_check = mlx5e_features_check,
4275 #ifdef CONFIG_RFS_ACCEL
4276 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
4278 .ndo_tx_timeout = mlx5e_tx_timeout,
4279 .ndo_bpf = mlx5e_xdp,
4280 #ifdef CONFIG_NET_POLL_CONTROLLER
4281 .ndo_poll_controller = mlx5e_netpoll,
4283 #ifdef CONFIG_MLX5_ESWITCH
4284 /* SRIOV E-Switch NDOs */
4285 .ndo_set_vf_mac = mlx5e_set_vf_mac,
4286 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
4287 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
4288 .ndo_set_vf_trust = mlx5e_set_vf_trust,
4289 .ndo_set_vf_rate = mlx5e_set_vf_rate,
4290 .ndo_get_vf_config = mlx5e_get_vf_config,
4291 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
4292 .ndo_get_vf_stats = mlx5e_get_vf_stats,
4293 .ndo_has_offload_stats = mlx5e_has_offload_stats,
4294 .ndo_get_offload_stats = mlx5e_get_offload_stats,
4298 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
4300 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
4302 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
4303 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
4304 !MLX5_CAP_ETH(mdev, csum_cap) ||
4305 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
4306 !MLX5_CAP_ETH(mdev, vlan_cap) ||
4307 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
4308 MLX5_CAP_FLOWTABLE(mdev,
4309 flow_table_properties_nic_receive.max_ft_level)
4311 mlx5_core_warn(mdev,
4312 "Not creating net device, some required device capabilities are missing\n");
4315 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
4316 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
4317 if (!MLX5_CAP_GEN(mdev, cq_moderation))
4318 mlx5_core_warn(mdev, "CQ moderation is not supported\n");
4323 void mlx5e_build_default_indir_rqt(u32 *indirection_rqt, int len,
4328 for (i = 0; i < len; i++)
4329 indirection_rqt[i] = i % num_channels;
4332 static bool slow_pci_heuristic(struct mlx5_core_dev *mdev)
4337 mlx5e_port_max_linkspeed(mdev, &link_speed);
4338 pci_bw = pcie_bandwidth_available(mdev->pdev, NULL, NULL, NULL);
4339 mlx5_core_dbg_once(mdev, "Max link speed = %d, PCI BW = %d\n",
4340 link_speed, pci_bw);
4342 #define MLX5E_SLOW_PCI_RATIO (2)
4344 return link_speed && pci_bw &&
4345 link_speed > MLX5E_SLOW_PCI_RATIO * pci_bw;
4348 static struct net_dim_cq_moder mlx5e_get_def_tx_moderation(u8 cq_period_mode)
4350 struct net_dim_cq_moder moder;
4352 moder.cq_period_mode = cq_period_mode;
4353 moder.pkts = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
4354 moder.usec = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
4355 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4356 moder.usec = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC_FROM_CQE;
4361 static struct net_dim_cq_moder mlx5e_get_def_rx_moderation(u8 cq_period_mode)
4363 struct net_dim_cq_moder moder;
4365 moder.cq_period_mode = cq_period_mode;
4366 moder.pkts = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
4367 moder.usec = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
4368 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4369 moder.usec = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
4374 static u8 mlx5_to_net_dim_cq_period_mode(u8 cq_period_mode)
4376 return cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE ?
4377 NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE :
4378 NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
4381 void mlx5e_set_tx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4383 if (params->tx_dim_enabled) {
4384 u8 dim_period_mode = mlx5_to_net_dim_cq_period_mode(cq_period_mode);
4386 params->tx_cq_moderation = net_dim_get_def_tx_moderation(dim_period_mode);
4388 params->tx_cq_moderation = mlx5e_get_def_tx_moderation(cq_period_mode);
4391 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_TX_CQE_BASED_MODER,
4392 params->tx_cq_moderation.cq_period_mode ==
4393 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4396 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4398 if (params->rx_dim_enabled) {
4399 u8 dim_period_mode = mlx5_to_net_dim_cq_period_mode(cq_period_mode);
4401 params->rx_cq_moderation = net_dim_get_def_rx_moderation(dim_period_mode);
4403 params->rx_cq_moderation = mlx5e_get_def_rx_moderation(cq_period_mode);
4406 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_BASED_MODER,
4407 params->rx_cq_moderation.cq_period_mode ==
4408 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4411 static u32 mlx5e_choose_lro_timeout(struct mlx5_core_dev *mdev, u32 wanted_timeout)
4415 /* The supported periods are organized in ascending order */
4416 for (i = 0; i < MLX5E_LRO_TIMEOUT_ARR_SIZE - 1; i++)
4417 if (MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]) >= wanted_timeout)
4420 return MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]);
4423 void mlx5e_build_nic_params(struct mlx5_core_dev *mdev,
4424 struct mlx5e_params *params,
4425 u16 max_channels, u16 mtu)
4427 u8 rx_cq_period_mode;
4429 params->sw_mtu = mtu;
4430 params->hard_mtu = MLX5E_ETH_HARD_MTU;
4431 params->num_channels = max_channels;
4435 params->log_sq_size = is_kdump_kernel() ?
4436 MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE :
4437 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
4439 /* set CQE compression */
4440 params->rx_cqe_compress_def = false;
4441 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
4442 MLX5_CAP_GEN(mdev, vport_group_manager))
4443 params->rx_cqe_compress_def = slow_pci_heuristic(mdev);
4445 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS, params->rx_cqe_compress_def);
4448 /* Prefer Striding RQ, unless any of the following holds:
4449 * - Striding RQ configuration is not possible/supported.
4450 * - Slow PCI heuristic.
4451 * - Legacy RQ would use linear SKB while Striding RQ would use non-linear.
4453 if (!slow_pci_heuristic(mdev) &&
4454 mlx5e_striding_rq_possible(mdev, params) &&
4455 (mlx5e_rx_mpwqe_is_linear_skb(mdev, params) ||
4456 !mlx5e_rx_is_linear_skb(mdev, params)))
4457 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ, true);
4458 mlx5e_set_rq_type(mdev, params);
4459 mlx5e_init_rq_type_params(mdev, params);
4463 /* TODO: && MLX5_CAP_ETH(mdev, lro_cap) */
4464 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
4465 if (!mlx5e_rx_mpwqe_is_linear_skb(mdev, params))
4466 params->lro_en = !slow_pci_heuristic(mdev);
4467 params->lro_timeout = mlx5e_choose_lro_timeout(mdev, MLX5E_DEFAULT_LRO_TIMEOUT);
4469 /* CQ moderation params */
4470 rx_cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
4471 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
4472 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
4473 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4474 params->tx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4475 mlx5e_set_rx_cq_mode_params(params, rx_cq_period_mode);
4476 mlx5e_set_tx_cq_mode_params(params, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
4479 params->tx_min_inline_mode = mlx5e_params_calculate_tx_min_inline(mdev);
4482 params->rss_hfunc = ETH_RSS_HASH_XOR;
4483 netdev_rss_key_fill(params->toeplitz_hash_key, sizeof(params->toeplitz_hash_key));
4484 mlx5e_build_default_indir_rqt(params->indirection_rqt,
4485 MLX5E_INDIR_RQT_SIZE, max_channels);
4488 static void mlx5e_build_nic_netdev_priv(struct mlx5_core_dev *mdev,
4489 struct net_device *netdev,
4490 const struct mlx5e_profile *profile,
4493 struct mlx5e_priv *priv = netdev_priv(netdev);
4496 priv->netdev = netdev;
4497 priv->profile = profile;
4498 priv->ppriv = ppriv;
4499 priv->msglevel = MLX5E_MSG_LEVEL;
4500 priv->max_opened_tc = 1;
4502 mlx5e_build_nic_params(mdev, &priv->channels.params,
4503 profile->max_nch(mdev), netdev->mtu);
4505 mutex_init(&priv->state_lock);
4507 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
4508 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
4509 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
4510 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
4512 mlx5e_timestamp_init(priv);
4515 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
4517 struct mlx5e_priv *priv = netdev_priv(netdev);
4519 mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
4520 if (is_zero_ether_addr(netdev->dev_addr) &&
4521 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
4522 eth_hw_addr_random(netdev);
4523 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
4527 #if IS_ENABLED(CONFIG_MLX5_ESWITCH)
4528 static const struct switchdev_ops mlx5e_switchdev_ops = {
4529 .switchdev_port_attr_get = mlx5e_attr_get,
4533 static void mlx5e_build_nic_netdev(struct net_device *netdev)
4535 struct mlx5e_priv *priv = netdev_priv(netdev);
4536 struct mlx5_core_dev *mdev = priv->mdev;
4540 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
4542 netdev->netdev_ops = &mlx5e_netdev_ops;
4544 #ifdef CONFIG_MLX5_CORE_EN_DCB
4545 if (MLX5_CAP_GEN(mdev, vport_group_manager) && MLX5_CAP_GEN(mdev, qos))
4546 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
4549 netdev->watchdog_timeo = 15 * HZ;
4551 netdev->ethtool_ops = &mlx5e_ethtool_ops;
4553 netdev->vlan_features |= NETIF_F_SG;
4554 netdev->vlan_features |= NETIF_F_IP_CSUM;
4555 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
4556 netdev->vlan_features |= NETIF_F_GRO;
4557 netdev->vlan_features |= NETIF_F_TSO;
4558 netdev->vlan_features |= NETIF_F_TSO6;
4559 netdev->vlan_features |= NETIF_F_RXCSUM;
4560 netdev->vlan_features |= NETIF_F_RXHASH;
4562 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_TX;
4563 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_RX;
4565 if (!!MLX5_CAP_ETH(mdev, lro_cap) &&
4566 mlx5e_check_fragmented_striding_rq_cap(mdev))
4567 netdev->vlan_features |= NETIF_F_LRO;
4569 netdev->hw_features = netdev->vlan_features;
4570 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4571 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4572 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4573 netdev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
4575 if (mlx5e_vxlan_allowed(mdev) || MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4576 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
4577 netdev->hw_enc_features |= NETIF_F_IPV6_CSUM;
4578 netdev->hw_enc_features |= NETIF_F_TSO;
4579 netdev->hw_enc_features |= NETIF_F_TSO6;
4580 netdev->hw_enc_features |= NETIF_F_GSO_PARTIAL;
4583 if (mlx5e_vxlan_allowed(mdev)) {
4584 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
4585 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4586 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
4587 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4588 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
4591 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4592 netdev->hw_features |= NETIF_F_GSO_GRE |
4593 NETIF_F_GSO_GRE_CSUM;
4594 netdev->hw_enc_features |= NETIF_F_GSO_GRE |
4595 NETIF_F_GSO_GRE_CSUM;
4596 netdev->gso_partial_features |= NETIF_F_GSO_GRE |
4597 NETIF_F_GSO_GRE_CSUM;
4600 netdev->hw_features |= NETIF_F_GSO_PARTIAL;
4601 netdev->gso_partial_features |= NETIF_F_GSO_UDP_L4;
4602 netdev->hw_features |= NETIF_F_GSO_UDP_L4;
4603 netdev->features |= NETIF_F_GSO_UDP_L4;
4605 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
4608 netdev->hw_features |= NETIF_F_RXALL;
4610 if (MLX5_CAP_ETH(mdev, scatter_fcs))
4611 netdev->hw_features |= NETIF_F_RXFCS;
4613 netdev->features = netdev->hw_features;
4614 if (!priv->channels.params.lro_en)
4615 netdev->features &= ~NETIF_F_LRO;
4618 netdev->features &= ~NETIF_F_RXALL;
4620 if (!priv->channels.params.scatter_fcs_en)
4621 netdev->features &= ~NETIF_F_RXFCS;
4623 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
4624 if (FT_CAP(flow_modify_en) &&
4625 FT_CAP(modify_root) &&
4626 FT_CAP(identified_miss_table_mode) &&
4627 FT_CAP(flow_table_modify)) {
4628 netdev->hw_features |= NETIF_F_HW_TC;
4629 #ifdef CONFIG_RFS_ACCEL
4630 netdev->hw_features |= NETIF_F_NTUPLE;
4634 netdev->features |= NETIF_F_HIGHDMA;
4635 netdev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
4637 netdev->priv_flags |= IFF_UNICAST_FLT;
4639 mlx5e_set_netdev_dev_addr(netdev);
4641 #if IS_ENABLED(CONFIG_MLX5_ESWITCH)
4642 if (MLX5_ESWITCH_MANAGER(mdev))
4643 netdev->switchdev_ops = &mlx5e_switchdev_ops;
4646 mlx5e_ipsec_build_netdev(priv);
4647 mlx5e_tls_build_netdev(priv);
4650 static void mlx5e_create_q_counters(struct mlx5e_priv *priv)
4652 struct mlx5_core_dev *mdev = priv->mdev;
4655 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
4657 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
4658 priv->q_counter = 0;
4661 err = mlx5_core_alloc_q_counter(mdev, &priv->drop_rq_q_counter);
4663 mlx5_core_warn(mdev, "alloc drop RQ counter failed, %d\n", err);
4664 priv->drop_rq_q_counter = 0;
4668 static void mlx5e_destroy_q_counters(struct mlx5e_priv *priv)
4670 if (priv->q_counter)
4671 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
4673 if (priv->drop_rq_q_counter)
4674 mlx5_core_dealloc_q_counter(priv->mdev, priv->drop_rq_q_counter);
4677 static void mlx5e_nic_init(struct mlx5_core_dev *mdev,
4678 struct net_device *netdev,
4679 const struct mlx5e_profile *profile,
4682 struct mlx5e_priv *priv = netdev_priv(netdev);
4685 mlx5e_build_nic_netdev_priv(mdev, netdev, profile, ppriv);
4686 err = mlx5e_ipsec_init(priv);
4688 mlx5_core_err(mdev, "IPSec initialization failed, %d\n", err);
4689 err = mlx5e_tls_init(priv);
4691 mlx5_core_err(mdev, "TLS initialization failed, %d\n", err);
4692 mlx5e_build_nic_netdev(netdev);
4693 mlx5e_build_tc2txq_maps(priv);
4694 mlx5e_vxlan_init(priv);
4697 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
4699 mlx5e_tls_cleanup(priv);
4700 mlx5e_ipsec_cleanup(priv);
4701 mlx5e_vxlan_cleanup(priv);
4704 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv)
4706 struct mlx5_core_dev *mdev = priv->mdev;
4709 err = mlx5e_create_indirect_rqt(priv);
4713 err = mlx5e_create_direct_rqts(priv);
4715 goto err_destroy_indirect_rqts;
4717 err = mlx5e_create_indirect_tirs(priv);
4719 goto err_destroy_direct_rqts;
4721 err = mlx5e_create_direct_tirs(priv);
4723 goto err_destroy_indirect_tirs;
4725 err = mlx5e_create_flow_steering(priv);
4727 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
4728 goto err_destroy_direct_tirs;
4731 err = mlx5e_tc_nic_init(priv);
4733 goto err_destroy_flow_steering;
4737 err_destroy_flow_steering:
4738 mlx5e_destroy_flow_steering(priv);
4739 err_destroy_direct_tirs:
4740 mlx5e_destroy_direct_tirs(priv);
4741 err_destroy_indirect_tirs:
4742 mlx5e_destroy_indirect_tirs(priv);
4743 err_destroy_direct_rqts:
4744 mlx5e_destroy_direct_rqts(priv);
4745 err_destroy_indirect_rqts:
4746 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
4750 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv)
4752 mlx5e_tc_nic_cleanup(priv);
4753 mlx5e_destroy_flow_steering(priv);
4754 mlx5e_destroy_direct_tirs(priv);
4755 mlx5e_destroy_indirect_tirs(priv);
4756 mlx5e_destroy_direct_rqts(priv);
4757 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
4760 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv)
4764 err = mlx5e_create_tises(priv);
4766 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
4770 #ifdef CONFIG_MLX5_CORE_EN_DCB
4771 mlx5e_dcbnl_initialize(priv);
4776 static void mlx5e_nic_enable(struct mlx5e_priv *priv)
4778 struct net_device *netdev = priv->netdev;
4779 struct mlx5_core_dev *mdev = priv->mdev;
4782 mlx5e_init_l2_addr(priv);
4784 /* Marking the link as currently not needed by the Driver */
4785 if (!netif_running(netdev))
4786 mlx5_set_port_admin_status(mdev, MLX5_PORT_DOWN);
4788 /* MTU range: 68 - hw-specific max */
4789 netdev->min_mtu = ETH_MIN_MTU;
4790 mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
4791 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
4792 mlx5e_set_dev_port_mtu(priv);
4794 mlx5_lag_add(mdev, netdev);
4796 mlx5e_enable_async_events(priv);
4798 if (MLX5_ESWITCH_MANAGER(priv->mdev))
4799 mlx5e_register_vport_reps(priv);
4801 if (netdev->reg_state != NETREG_REGISTERED)
4803 #ifdef CONFIG_MLX5_CORE_EN_DCB
4804 mlx5e_dcbnl_init_app(priv);
4807 queue_work(priv->wq, &priv->set_rx_mode_work);
4810 if (netif_running(netdev))
4812 netif_device_attach(netdev);
4816 static void mlx5e_nic_disable(struct mlx5e_priv *priv)
4818 struct mlx5_core_dev *mdev = priv->mdev;
4820 #ifdef CONFIG_MLX5_CORE_EN_DCB
4821 if (priv->netdev->reg_state == NETREG_REGISTERED)
4822 mlx5e_dcbnl_delete_app(priv);
4826 if (netif_running(priv->netdev))
4827 mlx5e_close(priv->netdev);
4828 netif_device_detach(priv->netdev);
4831 queue_work(priv->wq, &priv->set_rx_mode_work);
4833 if (MLX5_ESWITCH_MANAGER(priv->mdev))
4834 mlx5e_unregister_vport_reps(priv);
4836 mlx5e_disable_async_events(priv);
4837 mlx5_lag_remove(mdev);
4840 static const struct mlx5e_profile mlx5e_nic_profile = {
4841 .init = mlx5e_nic_init,
4842 .cleanup = mlx5e_nic_cleanup,
4843 .init_rx = mlx5e_init_nic_rx,
4844 .cleanup_rx = mlx5e_cleanup_nic_rx,
4845 .init_tx = mlx5e_init_nic_tx,
4846 .cleanup_tx = mlx5e_cleanup_nic_tx,
4847 .enable = mlx5e_nic_enable,
4848 .disable = mlx5e_nic_disable,
4849 .update_stats = mlx5e_update_ndo_stats,
4850 .max_nch = mlx5e_get_max_num_channels,
4851 .update_carrier = mlx5e_update_carrier,
4852 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe,
4853 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
4854 .max_tc = MLX5E_MAX_NUM_TC,
4857 /* mlx5e generic netdev management API (move to en_common.c) */
4859 struct net_device *mlx5e_create_netdev(struct mlx5_core_dev *mdev,
4860 const struct mlx5e_profile *profile,
4863 int nch = profile->max_nch(mdev);
4864 struct net_device *netdev;
4865 struct mlx5e_priv *priv;
4867 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
4868 nch * profile->max_tc,
4871 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
4875 #ifdef CONFIG_RFS_ACCEL
4876 netdev->rx_cpu_rmap = mdev->rmap;
4879 profile->init(mdev, netdev, profile, ppriv);
4881 netif_carrier_off(netdev);
4883 priv = netdev_priv(netdev);
4885 priv->wq = create_singlethread_workqueue("mlx5e");
4887 goto err_cleanup_nic;
4892 if (profile->cleanup)
4893 profile->cleanup(priv);
4894 free_netdev(netdev);
4899 int mlx5e_attach_netdev(struct mlx5e_priv *priv)
4901 struct mlx5_core_dev *mdev = priv->mdev;
4902 const struct mlx5e_profile *profile;
4905 profile = priv->profile;
4906 clear_bit(MLX5E_STATE_DESTROYING, &priv->state);
4908 err = profile->init_tx(priv);
4912 mlx5e_create_q_counters(priv);
4914 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
4916 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
4917 goto err_destroy_q_counters;
4920 err = profile->init_rx(priv);
4922 goto err_close_drop_rq;
4924 if (profile->enable)
4925 profile->enable(priv);
4930 mlx5e_close_drop_rq(&priv->drop_rq);
4932 err_destroy_q_counters:
4933 mlx5e_destroy_q_counters(priv);
4934 profile->cleanup_tx(priv);
4940 void mlx5e_detach_netdev(struct mlx5e_priv *priv)
4942 const struct mlx5e_profile *profile = priv->profile;
4944 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
4946 if (profile->disable)
4947 profile->disable(priv);
4948 flush_workqueue(priv->wq);
4950 profile->cleanup_rx(priv);
4951 mlx5e_close_drop_rq(&priv->drop_rq);
4952 mlx5e_destroy_q_counters(priv);
4953 profile->cleanup_tx(priv);
4954 cancel_delayed_work_sync(&priv->update_stats_work);
4957 void mlx5e_destroy_netdev(struct mlx5e_priv *priv)
4959 const struct mlx5e_profile *profile = priv->profile;
4960 struct net_device *netdev = priv->netdev;
4962 destroy_workqueue(priv->wq);
4963 if (profile->cleanup)
4964 profile->cleanup(priv);
4965 free_netdev(netdev);
4968 /* mlx5e_attach and mlx5e_detach scope should be only creating/destroying
4969 * hardware contexts and to connect it to the current netdev.
4971 static int mlx5e_attach(struct mlx5_core_dev *mdev, void *vpriv)
4973 struct mlx5e_priv *priv = vpriv;
4974 struct net_device *netdev = priv->netdev;
4977 if (netif_device_present(netdev))
4980 err = mlx5e_create_mdev_resources(mdev);
4984 err = mlx5e_attach_netdev(priv);
4986 mlx5e_destroy_mdev_resources(mdev);
4993 static void mlx5e_detach(struct mlx5_core_dev *mdev, void *vpriv)
4995 struct mlx5e_priv *priv = vpriv;
4996 struct net_device *netdev = priv->netdev;
4998 if (!netif_device_present(netdev))
5001 mlx5e_detach_netdev(priv);
5002 mlx5e_destroy_mdev_resources(mdev);
5005 static void *mlx5e_add(struct mlx5_core_dev *mdev)
5007 struct net_device *netdev;
5012 err = mlx5e_check_required_hca_cap(mdev);
5016 #ifdef CONFIG_MLX5_ESWITCH
5017 if (MLX5_ESWITCH_MANAGER(mdev)) {
5018 rpriv = mlx5e_alloc_nic_rep_priv(mdev);
5020 mlx5_core_warn(mdev, "Failed to alloc NIC rep priv data\n");
5026 netdev = mlx5e_create_netdev(mdev, &mlx5e_nic_profile, rpriv);
5028 mlx5_core_err(mdev, "mlx5e_create_netdev failed\n");
5029 goto err_free_rpriv;
5032 priv = netdev_priv(netdev);
5034 err = mlx5e_attach(mdev, priv);
5036 mlx5_core_err(mdev, "mlx5e_attach failed, %d\n", err);
5037 goto err_destroy_netdev;
5040 err = register_netdev(netdev);
5042 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
5046 #ifdef CONFIG_MLX5_CORE_EN_DCB
5047 mlx5e_dcbnl_init_app(priv);
5052 mlx5e_detach(mdev, priv);
5054 mlx5e_destroy_netdev(priv);
5060 static void mlx5e_remove(struct mlx5_core_dev *mdev, void *vpriv)
5062 struct mlx5e_priv *priv = vpriv;
5063 void *ppriv = priv->ppriv;
5065 #ifdef CONFIG_MLX5_CORE_EN_DCB
5066 mlx5e_dcbnl_delete_app(priv);
5068 unregister_netdev(priv->netdev);
5069 mlx5e_detach(mdev, vpriv);
5070 mlx5e_destroy_netdev(priv);
5074 static void *mlx5e_get_netdev(void *vpriv)
5076 struct mlx5e_priv *priv = vpriv;
5078 return priv->netdev;
5081 static struct mlx5_interface mlx5e_interface = {
5083 .remove = mlx5e_remove,
5084 .attach = mlx5e_attach,
5085 .detach = mlx5e_detach,
5086 .event = mlx5e_async_event,
5087 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
5088 .get_dev = mlx5e_get_netdev,
5091 void mlx5e_init(void)
5093 mlx5e_ipsec_build_inverse_table();
5094 mlx5e_build_ptys2ethtool_map();
5095 mlx5_register_interface(&mlx5e_interface);
5098 void mlx5e_cleanup(void)
5100 mlx5_unregister_interface(&mlx5e_interface);