2 * Copyright (c) 2015-2016, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <net/tc_act/tc_gact.h>
34 #include <net/pkt_cls.h>
35 #include <linux/mlx5/fs.h>
36 #include <net/vxlan.h>
37 #include <linux/bpf.h>
38 #include <net/page_pool.h>
43 #include "en_accel/ipsec.h"
44 #include "en_accel/ipsec_rxtx.h"
45 #include "en_accel/tls.h"
46 #include "accel/ipsec.h"
47 #include "accel/tls.h"
51 struct mlx5e_rq_param {
52 u32 rqc[MLX5_ST_SZ_DW(rqc)];
53 struct mlx5_wq_param wq;
56 struct mlx5e_sq_param {
57 u32 sqc[MLX5_ST_SZ_DW(sqc)];
58 struct mlx5_wq_param wq;
61 struct mlx5e_cq_param {
62 u32 cqc[MLX5_ST_SZ_DW(cqc)];
63 struct mlx5_wq_param wq;
68 struct mlx5e_channel_param {
69 struct mlx5e_rq_param rq;
70 struct mlx5e_sq_param sq;
71 struct mlx5e_sq_param xdp_sq;
72 struct mlx5e_sq_param icosq;
73 struct mlx5e_cq_param rx_cq;
74 struct mlx5e_cq_param tx_cq;
75 struct mlx5e_cq_param icosq_cq;
78 bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
80 bool striding_rq_umr = MLX5_CAP_GEN(mdev, striding_rq) &&
81 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
82 MLX5_CAP_ETH(mdev, reg_umr_sq);
83 u16 max_wqe_sz_cap = MLX5_CAP_GEN(mdev, max_wqe_sz_sq);
84 bool inline_umr = MLX5E_UMR_WQE_INLINE_SZ <= max_wqe_sz_cap;
89 mlx5_core_warn(mdev, "Cannot support Striding RQ: UMR WQE size (%d) exceeds maximum supported (%d).\n",
90 (int)MLX5E_UMR_WQE_INLINE_SZ, max_wqe_sz_cap);
96 static u32 mlx5e_mpwqe_get_linear_frag_sz(struct mlx5e_params *params)
98 if (!params->xdp_prog) {
99 u16 hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
100 u16 rq_headroom = MLX5_RX_HEADROOM + NET_IP_ALIGN;
102 return MLX5_SKB_FRAG_SZ(rq_headroom + hw_mtu);
108 static u8 mlx5e_mpwqe_log_pkts_per_wqe(struct mlx5e_params *params)
110 u32 linear_frag_sz = mlx5e_mpwqe_get_linear_frag_sz(params);
112 return MLX5_MPWRQ_LOG_WQE_SZ - order_base_2(linear_frag_sz);
115 static bool mlx5e_rx_mpwqe_is_linear_skb(struct mlx5_core_dev *mdev,
116 struct mlx5e_params *params)
118 u32 frag_sz = mlx5e_mpwqe_get_linear_frag_sz(params);
119 s8 signed_log_num_strides_param;
122 if (params->lro_en || frag_sz > PAGE_SIZE)
125 if (MLX5_CAP_GEN(mdev, ext_stride_num_range))
128 log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ - order_base_2(frag_sz);
129 signed_log_num_strides_param =
130 (s8)log_num_strides - MLX5_MPWQE_LOG_NUM_STRIDES_BASE;
132 return signed_log_num_strides_param >= 0;
135 static u8 mlx5e_mpwqe_get_log_rq_size(struct mlx5e_params *params)
137 if (params->log_rq_mtu_frames <
138 mlx5e_mpwqe_log_pkts_per_wqe(params) + MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW)
139 return MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW;
141 return params->log_rq_mtu_frames - mlx5e_mpwqe_log_pkts_per_wqe(params);
144 static u8 mlx5e_mpwqe_get_log_stride_size(struct mlx5_core_dev *mdev,
145 struct mlx5e_params *params)
147 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, params))
148 return order_base_2(mlx5e_mpwqe_get_linear_frag_sz(params));
150 return MLX5E_MPWQE_STRIDE_SZ(mdev,
151 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
154 static u8 mlx5e_mpwqe_get_log_num_strides(struct mlx5_core_dev *mdev,
155 struct mlx5e_params *params)
157 return MLX5_MPWRQ_LOG_WQE_SZ -
158 mlx5e_mpwqe_get_log_stride_size(mdev, params);
161 static u16 mlx5e_get_rq_headroom(struct mlx5_core_dev *mdev,
162 struct mlx5e_params *params)
164 u16 linear_rq_headroom = params->xdp_prog ?
165 XDP_PACKET_HEADROOM : MLX5_RX_HEADROOM;
167 linear_rq_headroom += NET_IP_ALIGN;
169 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST)
170 return linear_rq_headroom;
172 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, params))
173 return linear_rq_headroom;
178 void mlx5e_init_rq_type_params(struct mlx5_core_dev *mdev,
179 struct mlx5e_params *params)
181 params->lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
182 params->log_rq_mtu_frames = is_kdump_kernel() ?
183 MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE :
184 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
185 switch (params->rq_wq_type) {
186 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
188 default: /* MLX5_WQ_TYPE_LINKED_LIST */
189 /* Extra room needed for build_skb */
190 params->lro_wqe_sz -= mlx5e_get_rq_headroom(mdev, params) +
191 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
194 mlx5_core_info(mdev, "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
195 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
196 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ ?
197 BIT(mlx5e_mpwqe_get_log_rq_size(params)) :
198 BIT(params->log_rq_mtu_frames),
199 BIT(mlx5e_mpwqe_get_log_stride_size(mdev, params)),
200 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
203 bool mlx5e_striding_rq_possible(struct mlx5_core_dev *mdev,
204 struct mlx5e_params *params)
206 return mlx5e_check_fragmented_striding_rq_cap(mdev) &&
207 !MLX5_IPSEC_DEV(mdev) &&
208 !(params->xdp_prog && !mlx5e_rx_mpwqe_is_linear_skb(mdev, params));
211 void mlx5e_set_rq_type(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
213 params->rq_wq_type = mlx5e_striding_rq_possible(mdev, params) &&
214 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ) ?
215 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
216 MLX5_WQ_TYPE_LINKED_LIST;
219 static void mlx5e_update_carrier(struct mlx5e_priv *priv)
221 struct mlx5_core_dev *mdev = priv->mdev;
224 port_state = mlx5_query_vport_state(mdev,
225 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT,
228 if (port_state == VPORT_STATE_UP) {
229 netdev_info(priv->netdev, "Link up\n");
230 netif_carrier_on(priv->netdev);
232 netdev_info(priv->netdev, "Link down\n");
233 netif_carrier_off(priv->netdev);
237 static void mlx5e_update_carrier_work(struct work_struct *work)
239 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
240 update_carrier_work);
242 mutex_lock(&priv->state_lock);
243 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
244 if (priv->profile->update_carrier)
245 priv->profile->update_carrier(priv);
246 mutex_unlock(&priv->state_lock);
249 void mlx5e_update_stats(struct mlx5e_priv *priv)
253 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
254 if (mlx5e_stats_grps[i].update_stats)
255 mlx5e_stats_grps[i].update_stats(priv);
258 static void mlx5e_update_ndo_stats(struct mlx5e_priv *priv)
262 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
263 if (mlx5e_stats_grps[i].update_stats_mask &
264 MLX5E_NDO_UPDATE_STATS)
265 mlx5e_stats_grps[i].update_stats(priv);
268 void mlx5e_update_stats_work(struct work_struct *work)
270 struct delayed_work *dwork = to_delayed_work(work);
271 struct mlx5e_priv *priv = container_of(dwork, struct mlx5e_priv,
273 mutex_lock(&priv->state_lock);
274 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
275 priv->profile->update_stats(priv);
276 queue_delayed_work(priv->wq, dwork,
277 msecs_to_jiffies(MLX5E_UPDATE_STATS_INTERVAL));
279 mutex_unlock(&priv->state_lock);
282 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
283 enum mlx5_dev_event event, unsigned long param)
285 struct mlx5e_priv *priv = vpriv;
287 if (!test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state))
291 case MLX5_DEV_EVENT_PORT_UP:
292 case MLX5_DEV_EVENT_PORT_DOWN:
293 queue_work(priv->wq, &priv->update_carrier_work);
300 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
302 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
305 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
307 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
308 synchronize_irq(pci_irq_vector(priv->mdev->pdev, MLX5_EQ_VEC_ASYNC));
311 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq,
312 struct mlx5e_icosq *sq,
313 struct mlx5e_umr_wqe *wqe)
315 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
316 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
317 u8 ds_cnt = DIV_ROUND_UP(MLX5E_UMR_WQE_INLINE_SZ, MLX5_SEND_WQE_DS);
319 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
321 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
322 cseg->imm = rq->mkey_be;
324 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN | MLX5_UMR_INLINE;
325 ucseg->xlt_octowords =
326 cpu_to_be16(MLX5_MTT_OCTW(MLX5_MPWRQ_PAGES_PER_WQE));
327 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
330 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq,
331 struct mlx5e_channel *c)
333 int wq_sz = mlx5_wq_ll_get_size(&rq->wq);
335 rq->mpwqe.info = kzalloc_node(wq_sz * sizeof(*rq->mpwqe.info),
336 GFP_KERNEL, cpu_to_node(c->cpu));
340 mlx5e_build_umr_wqe(rq, &c->icosq, &rq->mpwqe.umr_wqe);
345 static int mlx5e_create_umr_mkey(struct mlx5_core_dev *mdev,
346 u64 npages, u8 page_shift,
347 struct mlx5_core_mkey *umr_mkey)
349 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
354 in = kvzalloc(inlen, GFP_KERNEL);
358 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
360 MLX5_SET(mkc, mkc, free, 1);
361 MLX5_SET(mkc, mkc, umr_en, 1);
362 MLX5_SET(mkc, mkc, lw, 1);
363 MLX5_SET(mkc, mkc, lr, 1);
364 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT);
366 MLX5_SET(mkc, mkc, qpn, 0xffffff);
367 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.pdn);
368 MLX5_SET64(mkc, mkc, len, npages << page_shift);
369 MLX5_SET(mkc, mkc, translations_octword_size,
370 MLX5_MTT_OCTW(npages));
371 MLX5_SET(mkc, mkc, log_page_size, page_shift);
373 err = mlx5_core_create_mkey(mdev, umr_mkey, in, inlen);
379 static int mlx5e_create_rq_umr_mkey(struct mlx5_core_dev *mdev, struct mlx5e_rq *rq)
381 u64 num_mtts = MLX5E_REQUIRED_MTTS(mlx5_wq_ll_get_size(&rq->wq));
383 return mlx5e_create_umr_mkey(mdev, num_mtts, PAGE_SHIFT, &rq->umr_mkey);
386 static inline u64 mlx5e_get_mpwqe_offset(struct mlx5e_rq *rq, u16 wqe_ix)
388 return (wqe_ix << MLX5E_LOG_ALIGNED_MPWQE_PPW) << PAGE_SHIFT;
391 static int mlx5e_alloc_rq(struct mlx5e_channel *c,
392 struct mlx5e_params *params,
393 struct mlx5e_rq_param *rqp,
396 struct page_pool_params pp_params = { 0 };
397 struct mlx5_core_dev *mdev = c->mdev;
398 void *rqc = rqp->rqc;
399 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
400 u32 byte_count, pool_size;
406 rqp->wq.db_numa_node = cpu_to_node(c->cpu);
408 err = mlx5_wq_ll_create(mdev, &rqp->wq, rqc_wq, &rq->wq,
413 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
415 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
417 rq->wq_type = params->rq_wq_type;
419 rq->netdev = c->netdev;
420 rq->tstamp = c->tstamp;
421 rq->clock = &mdev->clock;
425 rq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
426 rq->stats = &c->priv->channel_stats[c->ix].rq;
428 rq->xdp_prog = params->xdp_prog ? bpf_prog_inc(params->xdp_prog) : NULL;
429 if (IS_ERR(rq->xdp_prog)) {
430 err = PTR_ERR(rq->xdp_prog);
432 goto err_rq_wq_destroy;
435 err = xdp_rxq_info_reg(&rq->xdp_rxq, rq->netdev, rq->ix);
437 goto err_rq_wq_destroy;
439 rq->buff.map_dir = rq->xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
440 rq->buff.headroom = mlx5e_get_rq_headroom(mdev, params);
441 pool_size = 1 << params->log_rq_mtu_frames;
443 switch (rq->wq_type) {
444 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
446 pool_size = MLX5_MPWRQ_PAGES_PER_WQE << mlx5e_mpwqe_get_log_rq_size(params);
447 rq->post_wqes = mlx5e_post_rx_mpwqes;
448 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
450 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe_mpwqe;
451 #ifdef CONFIG_MLX5_EN_IPSEC
452 if (MLX5_IPSEC_DEV(mdev)) {
454 netdev_err(c->netdev, "MPWQE RQ with IPSec offload not supported\n");
455 goto err_rq_wq_destroy;
458 if (!rq->handle_rx_cqe) {
460 netdev_err(c->netdev, "RX handler of MPWQE RQ is not set, err %d\n", err);
461 goto err_rq_wq_destroy;
464 rq->mpwqe.skb_from_cqe_mpwrq =
465 mlx5e_rx_mpwqe_is_linear_skb(mdev, params) ?
466 mlx5e_skb_from_cqe_mpwrq_linear :
467 mlx5e_skb_from_cqe_mpwrq_nonlinear;
468 rq->mpwqe.log_stride_sz = mlx5e_mpwqe_get_log_stride_size(mdev, params);
469 rq->mpwqe.num_strides = BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params));
471 byte_count = rq->mpwqe.num_strides << rq->mpwqe.log_stride_sz;
473 err = mlx5e_create_rq_umr_mkey(mdev, rq);
475 goto err_rq_wq_destroy;
476 rq->mkey_be = cpu_to_be32(rq->umr_mkey.key);
478 err = mlx5e_rq_alloc_mpwqe_info(rq, c);
480 goto err_destroy_umr_mkey;
482 default: /* MLX5_WQ_TYPE_LINKED_LIST */
484 kzalloc_node(wq_sz * sizeof(*rq->wqe.frag_info),
485 GFP_KERNEL, cpu_to_node(c->cpu));
486 if (!rq->wqe.frag_info) {
488 goto err_rq_wq_destroy;
490 rq->post_wqes = mlx5e_post_rx_wqes;
491 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
493 #ifdef CONFIG_MLX5_EN_IPSEC
495 rq->handle_rx_cqe = mlx5e_ipsec_handle_rx_cqe;
498 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe;
499 if (!rq->handle_rx_cqe) {
500 kfree(rq->wqe.frag_info);
502 netdev_err(c->netdev, "RX handler of RQ is not set, err %d\n", err);
503 goto err_rq_wq_destroy;
506 byte_count = params->lro_en ?
508 MLX5E_SW2HW_MTU(params, params->sw_mtu);
509 #ifdef CONFIG_MLX5_EN_IPSEC
510 if (MLX5_IPSEC_DEV(mdev))
511 byte_count += MLX5E_METADATA_ETHER_LEN;
513 rq->wqe.page_reuse = !params->xdp_prog && !params->lro_en;
515 /* calc the required page order */
516 rq->wqe.frag_sz = MLX5_SKB_FRAG_SZ(rq->buff.headroom + byte_count);
517 npages = DIV_ROUND_UP(rq->wqe.frag_sz, PAGE_SIZE);
518 rq->buff.page_order = order_base_2(npages);
520 byte_count |= MLX5_HW_START_PADDING;
521 rq->mkey_be = c->mkey_be;
524 /* Create a page_pool and register it with rxq */
525 pp_params.order = rq->buff.page_order;
526 pp_params.flags = 0; /* No-internal DMA mapping in page_pool */
527 pp_params.pool_size = pool_size;
528 pp_params.nid = cpu_to_node(c->cpu);
529 pp_params.dev = c->pdev;
530 pp_params.dma_dir = rq->buff.map_dir;
532 /* page_pool can be used even when there is no rq->xdp_prog,
533 * given page_pool does not handle DMA mapping there is no
534 * required state to clear. And page_pool gracefully handle
537 rq->page_pool = page_pool_create(&pp_params);
538 if (IS_ERR(rq->page_pool)) {
539 if (rq->wq_type != MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
540 kfree(rq->wqe.frag_info);
541 err = PTR_ERR(rq->page_pool);
542 rq->page_pool = NULL;
543 goto err_rq_wq_destroy;
545 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
546 MEM_TYPE_PAGE_POOL, rq->page_pool);
548 goto err_rq_wq_destroy;
550 for (i = 0; i < wq_sz; i++) {
551 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
553 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
554 u64 dma_offset = mlx5e_get_mpwqe_offset(rq, i);
556 wqe->data.addr = cpu_to_be64(dma_offset + rq->buff.headroom);
559 wqe->data.byte_count = cpu_to_be32(byte_count);
560 wqe->data.lkey = rq->mkey_be;
563 INIT_WORK(&rq->dim.work, mlx5e_rx_dim_work);
565 switch (params->rx_cq_moderation.cq_period_mode) {
566 case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
567 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE;
569 case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
571 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
574 rq->page_cache.head = 0;
575 rq->page_cache.tail = 0;
579 err_destroy_umr_mkey:
580 mlx5_core_destroy_mkey(mdev, &rq->umr_mkey);
584 bpf_prog_put(rq->xdp_prog);
585 xdp_rxq_info_unreg(&rq->xdp_rxq);
587 page_pool_destroy(rq->page_pool);
588 mlx5_wq_destroy(&rq->wq_ctrl);
593 static void mlx5e_free_rq(struct mlx5e_rq *rq)
598 bpf_prog_put(rq->xdp_prog);
600 xdp_rxq_info_unreg(&rq->xdp_rxq);
602 page_pool_destroy(rq->page_pool);
604 switch (rq->wq_type) {
605 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
606 kfree(rq->mpwqe.info);
607 mlx5_core_destroy_mkey(rq->mdev, &rq->umr_mkey);
609 default: /* MLX5_WQ_TYPE_LINKED_LIST */
610 kfree(rq->wqe.frag_info);
613 for (i = rq->page_cache.head; i != rq->page_cache.tail;
614 i = (i + 1) & (MLX5E_CACHE_SIZE - 1)) {
615 struct mlx5e_dma_info *dma_info = &rq->page_cache.page_cache[i];
617 mlx5e_page_release(rq, dma_info, false);
619 mlx5_wq_destroy(&rq->wq_ctrl);
622 static int mlx5e_create_rq(struct mlx5e_rq *rq,
623 struct mlx5e_rq_param *param)
625 struct mlx5_core_dev *mdev = rq->mdev;
633 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
634 sizeof(u64) * rq->wq_ctrl.buf.npages;
635 in = kvzalloc(inlen, GFP_KERNEL);
639 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
640 wq = MLX5_ADDR_OF(rqc, rqc, wq);
642 memcpy(rqc, param->rqc, sizeof(param->rqc));
644 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
645 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
646 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
647 MLX5_ADAPTER_PAGE_SHIFT);
648 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
650 mlx5_fill_page_frag_array(&rq->wq_ctrl.buf,
651 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
653 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
660 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
663 struct mlx5_core_dev *mdev = rq->mdev;
670 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
671 in = kvzalloc(inlen, GFP_KERNEL);
675 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
677 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
678 MLX5_SET(rqc, rqc, state, next_state);
680 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
687 static int mlx5e_modify_rq_scatter_fcs(struct mlx5e_rq *rq, bool enable)
689 struct mlx5e_channel *c = rq->channel;
690 struct mlx5e_priv *priv = c->priv;
691 struct mlx5_core_dev *mdev = priv->mdev;
698 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
699 in = kvzalloc(inlen, GFP_KERNEL);
703 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
705 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
706 MLX5_SET64(modify_rq_in, in, modify_bitmask,
707 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_SCATTER_FCS);
708 MLX5_SET(rqc, rqc, scatter_fcs, enable);
709 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
711 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
718 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
720 struct mlx5e_channel *c = rq->channel;
721 struct mlx5_core_dev *mdev = c->mdev;
727 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
728 in = kvzalloc(inlen, GFP_KERNEL);
732 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
734 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
735 MLX5_SET64(modify_rq_in, in, modify_bitmask,
736 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
737 MLX5_SET(rqc, rqc, vsd, vsd);
738 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
740 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
747 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
749 mlx5_core_destroy_rq(rq->mdev, rq->rqn);
752 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq, int wait_time)
754 unsigned long exp_time = jiffies + msecs_to_jiffies(wait_time);
755 struct mlx5e_channel *c = rq->channel;
757 struct mlx5_wq_ll *wq = &rq->wq;
758 u16 min_wqes = mlx5_min_rx_wqes(rq->wq_type, mlx5_wq_ll_get_size(wq));
761 if (wq->cur_sz >= min_wqes)
765 } while (time_before(jiffies, exp_time));
767 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",
768 c->ix, rq->rqn, wq->cur_sz, min_wqes);
773 static void mlx5e_free_rx_descs(struct mlx5e_rq *rq)
775 struct mlx5_wq_ll *wq = &rq->wq;
776 struct mlx5e_rx_wqe *wqe;
780 /* UMR WQE (if in progress) is always at wq->head */
781 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ &&
782 rq->mpwqe.umr_in_progress)
783 mlx5e_free_rx_mpwqe(rq, &rq->mpwqe.info[wq->head]);
785 while (!mlx5_wq_ll_is_empty(wq)) {
786 wqe_ix_be = *wq->tail_next;
787 wqe_ix = be16_to_cpu(wqe_ix_be);
788 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_ix);
789 rq->dealloc_wqe(rq, wqe_ix);
790 mlx5_wq_ll_pop(&rq->wq, wqe_ix_be,
791 &wqe->next.next_wqe_index);
794 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST && rq->wqe.page_reuse) {
795 /* Clean outstanding pages on handled WQEs that decided to do page-reuse,
796 * but yet to be re-posted.
798 int wq_sz = mlx5_wq_ll_get_size(&rq->wq);
800 for (wqe_ix = 0; wqe_ix < wq_sz; wqe_ix++)
801 rq->dealloc_wqe(rq, wqe_ix);
805 static int mlx5e_open_rq(struct mlx5e_channel *c,
806 struct mlx5e_params *params,
807 struct mlx5e_rq_param *param,
812 err = mlx5e_alloc_rq(c, params, param, rq);
816 err = mlx5e_create_rq(rq, param);
820 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
824 if (params->rx_dim_enabled)
825 __set_bit(MLX5E_RQ_STATE_AM, &c->rq.state);
830 mlx5e_destroy_rq(rq);
837 static void mlx5e_activate_rq(struct mlx5e_rq *rq)
839 struct mlx5e_icosq *sq = &rq->channel->icosq;
840 struct mlx5_wq_cyc *wq = &sq->wq;
841 struct mlx5e_tx_wqe *nopwqe;
843 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
845 set_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
846 sq->db.ico_wqe[pi].opcode = MLX5_OPCODE_NOP;
847 nopwqe = mlx5e_post_nop(wq, sq->sqn, &sq->pc);
848 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &nopwqe->ctrl);
851 static void mlx5e_deactivate_rq(struct mlx5e_rq *rq)
853 clear_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
854 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
857 static void mlx5e_close_rq(struct mlx5e_rq *rq)
859 cancel_work_sync(&rq->dim.work);
860 mlx5e_destroy_rq(rq);
861 mlx5e_free_rx_descs(rq);
865 static void mlx5e_free_xdpsq_db(struct mlx5e_xdpsq *sq)
870 static int mlx5e_alloc_xdpsq_db(struct mlx5e_xdpsq *sq, int numa)
872 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
874 sq->db.di = kzalloc_node(sizeof(*sq->db.di) * wq_sz,
877 mlx5e_free_xdpsq_db(sq);
884 static int mlx5e_alloc_xdpsq(struct mlx5e_channel *c,
885 struct mlx5e_params *params,
886 struct mlx5e_sq_param *param,
887 struct mlx5e_xdpsq *sq)
889 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
890 struct mlx5_core_dev *mdev = c->mdev;
891 struct mlx5_wq_cyc *wq = &sq->wq;
895 sq->mkey_be = c->mkey_be;
897 sq->uar_map = mdev->mlx5e_res.bfreg.map;
898 sq->min_inline_mode = params->tx_min_inline_mode;
900 param->wq.db_numa_node = cpu_to_node(c->cpu);
901 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
904 wq->db = &wq->db[MLX5_SND_DBR];
906 err = mlx5e_alloc_xdpsq_db(sq, cpu_to_node(c->cpu));
908 goto err_sq_wq_destroy;
913 mlx5_wq_destroy(&sq->wq_ctrl);
918 static void mlx5e_free_xdpsq(struct mlx5e_xdpsq *sq)
920 mlx5e_free_xdpsq_db(sq);
921 mlx5_wq_destroy(&sq->wq_ctrl);
924 static void mlx5e_free_icosq_db(struct mlx5e_icosq *sq)
926 kfree(sq->db.ico_wqe);
929 static int mlx5e_alloc_icosq_db(struct mlx5e_icosq *sq, int numa)
931 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
933 sq->db.ico_wqe = kzalloc_node(sizeof(*sq->db.ico_wqe) * wq_sz,
941 static int mlx5e_alloc_icosq(struct mlx5e_channel *c,
942 struct mlx5e_sq_param *param,
943 struct mlx5e_icosq *sq)
945 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
946 struct mlx5_core_dev *mdev = c->mdev;
947 struct mlx5_wq_cyc *wq = &sq->wq;
951 sq->uar_map = mdev->mlx5e_res.bfreg.map;
953 param->wq.db_numa_node = cpu_to_node(c->cpu);
954 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
957 wq->db = &wq->db[MLX5_SND_DBR];
959 err = mlx5e_alloc_icosq_db(sq, cpu_to_node(c->cpu));
961 goto err_sq_wq_destroy;
966 mlx5_wq_destroy(&sq->wq_ctrl);
971 static void mlx5e_free_icosq(struct mlx5e_icosq *sq)
973 mlx5e_free_icosq_db(sq);
974 mlx5_wq_destroy(&sq->wq_ctrl);
977 static void mlx5e_free_txqsq_db(struct mlx5e_txqsq *sq)
979 kfree(sq->db.wqe_info);
980 kfree(sq->db.dma_fifo);
983 static int mlx5e_alloc_txqsq_db(struct mlx5e_txqsq *sq, int numa)
985 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
986 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
988 sq->db.dma_fifo = kzalloc_node(df_sz * sizeof(*sq->db.dma_fifo),
990 sq->db.wqe_info = kzalloc_node(wq_sz * sizeof(*sq->db.wqe_info),
992 if (!sq->db.dma_fifo || !sq->db.wqe_info) {
993 mlx5e_free_txqsq_db(sq);
997 sq->dma_fifo_mask = df_sz - 1;
1002 static void mlx5e_sq_recover(struct work_struct *work);
1003 static int mlx5e_alloc_txqsq(struct mlx5e_channel *c,
1005 struct mlx5e_params *params,
1006 struct mlx5e_sq_param *param,
1007 struct mlx5e_txqsq *sq,
1010 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1011 struct mlx5_core_dev *mdev = c->mdev;
1012 struct mlx5_wq_cyc *wq = &sq->wq;
1016 sq->tstamp = c->tstamp;
1017 sq->clock = &mdev->clock;
1018 sq->mkey_be = c->mkey_be;
1020 sq->txq_ix = txq_ix;
1021 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1022 sq->min_inline_mode = params->tx_min_inline_mode;
1023 sq->stats = &c->priv->channel_stats[c->ix].sq[tc];
1024 INIT_WORK(&sq->recover.recover_work, mlx5e_sq_recover);
1025 if (MLX5_IPSEC_DEV(c->priv->mdev))
1026 set_bit(MLX5E_SQ_STATE_IPSEC, &sq->state);
1027 if (mlx5_accel_is_tls_device(c->priv->mdev))
1028 set_bit(MLX5E_SQ_STATE_TLS, &sq->state);
1030 param->wq.db_numa_node = cpu_to_node(c->cpu);
1031 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1034 wq->db = &wq->db[MLX5_SND_DBR];
1036 err = mlx5e_alloc_txqsq_db(sq, cpu_to_node(c->cpu));
1038 goto err_sq_wq_destroy;
1040 INIT_WORK(&sq->dim.work, mlx5e_tx_dim_work);
1041 sq->dim.mode = params->tx_cq_moderation.cq_period_mode;
1046 mlx5_wq_destroy(&sq->wq_ctrl);
1051 static void mlx5e_free_txqsq(struct mlx5e_txqsq *sq)
1053 mlx5e_free_txqsq_db(sq);
1054 mlx5_wq_destroy(&sq->wq_ctrl);
1057 struct mlx5e_create_sq_param {
1058 struct mlx5_wq_ctrl *wq_ctrl;
1065 static int mlx5e_create_sq(struct mlx5_core_dev *mdev,
1066 struct mlx5e_sq_param *param,
1067 struct mlx5e_create_sq_param *csp,
1076 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1077 sizeof(u64) * csp->wq_ctrl->buf.npages;
1078 in = kvzalloc(inlen, GFP_KERNEL);
1082 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1083 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1085 memcpy(sqc, param->sqc, sizeof(param->sqc));
1086 MLX5_SET(sqc, sqc, tis_lst_sz, csp->tis_lst_sz);
1087 MLX5_SET(sqc, sqc, tis_num_0, csp->tisn);
1088 MLX5_SET(sqc, sqc, cqn, csp->cqn);
1090 if (MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5_CAP_INLINE_MODE_VPORT_CONTEXT)
1091 MLX5_SET(sqc, sqc, min_wqe_inline_mode, csp->min_inline_mode);
1093 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1094 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1096 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1097 MLX5_SET(wq, wq, uar_page, mdev->mlx5e_res.bfreg.index);
1098 MLX5_SET(wq, wq, log_wq_pg_sz, csp->wq_ctrl->buf.page_shift -
1099 MLX5_ADAPTER_PAGE_SHIFT);
1100 MLX5_SET64(wq, wq, dbr_addr, csp->wq_ctrl->db.dma);
1102 mlx5_fill_page_frag_array(&csp->wq_ctrl->buf,
1103 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
1105 err = mlx5_core_create_sq(mdev, in, inlen, sqn);
1112 struct mlx5e_modify_sq_param {
1119 static int mlx5e_modify_sq(struct mlx5_core_dev *mdev, u32 sqn,
1120 struct mlx5e_modify_sq_param *p)
1127 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1128 in = kvzalloc(inlen, GFP_KERNEL);
1132 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1134 MLX5_SET(modify_sq_in, in, sq_state, p->curr_state);
1135 MLX5_SET(sqc, sqc, state, p->next_state);
1136 if (p->rl_update && p->next_state == MLX5_SQC_STATE_RDY) {
1137 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
1138 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, p->rl_index);
1141 err = mlx5_core_modify_sq(mdev, sqn, in, inlen);
1148 static void mlx5e_destroy_sq(struct mlx5_core_dev *mdev, u32 sqn)
1150 mlx5_core_destroy_sq(mdev, sqn);
1153 static int mlx5e_create_sq_rdy(struct mlx5_core_dev *mdev,
1154 struct mlx5e_sq_param *param,
1155 struct mlx5e_create_sq_param *csp,
1158 struct mlx5e_modify_sq_param msp = {0};
1161 err = mlx5e_create_sq(mdev, param, csp, sqn);
1165 msp.curr_state = MLX5_SQC_STATE_RST;
1166 msp.next_state = MLX5_SQC_STATE_RDY;
1167 err = mlx5e_modify_sq(mdev, *sqn, &msp);
1169 mlx5e_destroy_sq(mdev, *sqn);
1174 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1175 struct mlx5e_txqsq *sq, u32 rate);
1177 static int mlx5e_open_txqsq(struct mlx5e_channel *c,
1180 struct mlx5e_params *params,
1181 struct mlx5e_sq_param *param,
1182 struct mlx5e_txqsq *sq,
1185 struct mlx5e_create_sq_param csp = {};
1189 err = mlx5e_alloc_txqsq(c, txq_ix, params, param, sq, tc);
1195 csp.cqn = sq->cq.mcq.cqn;
1196 csp.wq_ctrl = &sq->wq_ctrl;
1197 csp.min_inline_mode = sq->min_inline_mode;
1198 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1200 goto err_free_txqsq;
1202 tx_rate = c->priv->tx_rates[sq->txq_ix];
1204 mlx5e_set_sq_maxrate(c->netdev, sq, tx_rate);
1206 if (params->tx_dim_enabled)
1207 sq->state |= BIT(MLX5E_SQ_STATE_AM);
1212 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1213 mlx5e_free_txqsq(sq);
1218 static void mlx5e_reset_txqsq_cc_pc(struct mlx5e_txqsq *sq)
1220 WARN_ONCE(sq->cc != sq->pc,
1221 "SQ 0x%x: cc (0x%x) != pc (0x%x)\n",
1222 sq->sqn, sq->cc, sq->pc);
1224 sq->dma_fifo_cc = 0;
1228 static void mlx5e_activate_txqsq(struct mlx5e_txqsq *sq)
1230 sq->txq = netdev_get_tx_queue(sq->channel->netdev, sq->txq_ix);
1231 clear_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state);
1232 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1233 netdev_tx_reset_queue(sq->txq);
1234 netif_tx_start_queue(sq->txq);
1237 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
1239 __netif_tx_lock_bh(txq);
1240 netif_tx_stop_queue(txq);
1241 __netif_tx_unlock_bh(txq);
1244 static void mlx5e_deactivate_txqsq(struct mlx5e_txqsq *sq)
1246 struct mlx5e_channel *c = sq->channel;
1247 struct mlx5_wq_cyc *wq = &sq->wq;
1249 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1250 /* prevent netif_tx_wake_queue */
1251 napi_synchronize(&c->napi);
1253 netif_tx_disable_queue(sq->txq);
1255 /* last doorbell out, godspeed .. */
1256 if (mlx5e_wqc_has_room_for(wq, sq->cc, sq->pc, 1)) {
1257 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
1258 struct mlx5e_tx_wqe *nop;
1260 sq->db.wqe_info[pi].skb = NULL;
1261 nop = mlx5e_post_nop(wq, sq->sqn, &sq->pc);
1262 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &nop->ctrl);
1266 static void mlx5e_close_txqsq(struct mlx5e_txqsq *sq)
1268 struct mlx5e_channel *c = sq->channel;
1269 struct mlx5_core_dev *mdev = c->mdev;
1270 struct mlx5_rate_limit rl = {0};
1272 mlx5e_destroy_sq(mdev, sq->sqn);
1273 if (sq->rate_limit) {
1274 rl.rate = sq->rate_limit;
1275 mlx5_rl_remove_rate(mdev, &rl);
1277 mlx5e_free_txqsq_descs(sq);
1278 mlx5e_free_txqsq(sq);
1281 static int mlx5e_wait_for_sq_flush(struct mlx5e_txqsq *sq)
1283 unsigned long exp_time = jiffies + msecs_to_jiffies(2000);
1285 while (time_before(jiffies, exp_time)) {
1286 if (sq->cc == sq->pc)
1292 netdev_err(sq->channel->netdev,
1293 "Wait for SQ 0x%x flush timeout (sq cc = 0x%x, sq pc = 0x%x)\n",
1294 sq->sqn, sq->cc, sq->pc);
1299 static int mlx5e_sq_to_ready(struct mlx5e_txqsq *sq, int curr_state)
1301 struct mlx5_core_dev *mdev = sq->channel->mdev;
1302 struct net_device *dev = sq->channel->netdev;
1303 struct mlx5e_modify_sq_param msp = {0};
1306 msp.curr_state = curr_state;
1307 msp.next_state = MLX5_SQC_STATE_RST;
1309 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1311 netdev_err(dev, "Failed to move sq 0x%x to reset\n", sq->sqn);
1315 memset(&msp, 0, sizeof(msp));
1316 msp.curr_state = MLX5_SQC_STATE_RST;
1317 msp.next_state = MLX5_SQC_STATE_RDY;
1319 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1321 netdev_err(dev, "Failed to move sq 0x%x to ready\n", sq->sqn);
1328 static void mlx5e_sq_recover(struct work_struct *work)
1330 struct mlx5e_txqsq_recover *recover =
1331 container_of(work, struct mlx5e_txqsq_recover,
1333 struct mlx5e_txqsq *sq = container_of(recover, struct mlx5e_txqsq,
1335 struct mlx5_core_dev *mdev = sq->channel->mdev;
1336 struct net_device *dev = sq->channel->netdev;
1340 err = mlx5_core_query_sq_state(mdev, sq->sqn, &state);
1342 netdev_err(dev, "Failed to query SQ 0x%x state. err = %d\n",
1347 if (state != MLX5_RQC_STATE_ERR) {
1348 netdev_err(dev, "SQ 0x%x not in ERROR state\n", sq->sqn);
1352 netif_tx_disable_queue(sq->txq);
1354 if (mlx5e_wait_for_sq_flush(sq))
1357 /* If the interval between two consecutive recovers per SQ is too
1358 * short, don't recover to avoid infinite loop of ERR_CQE -> recover.
1359 * If we reached this state, there is probably a bug that needs to be
1360 * fixed. let's keep the queue close and let tx timeout cleanup.
1362 if (jiffies_to_msecs(jiffies - recover->last_recover) <
1363 MLX5E_SQ_RECOVER_MIN_INTERVAL) {
1364 netdev_err(dev, "Recover SQ 0x%x canceled, too many error CQEs\n",
1369 /* At this point, no new packets will arrive from the stack as TXQ is
1370 * marked with QUEUE_STATE_DRV_XOFF. In addition, NAPI cleared all
1371 * pending WQEs. SQ can safely reset the SQ.
1373 if (mlx5e_sq_to_ready(sq, state))
1376 mlx5e_reset_txqsq_cc_pc(sq);
1377 sq->stats->recover++;
1378 recover->last_recover = jiffies;
1379 mlx5e_activate_txqsq(sq);
1382 static int mlx5e_open_icosq(struct mlx5e_channel *c,
1383 struct mlx5e_params *params,
1384 struct mlx5e_sq_param *param,
1385 struct mlx5e_icosq *sq)
1387 struct mlx5e_create_sq_param csp = {};
1390 err = mlx5e_alloc_icosq(c, param, sq);
1394 csp.cqn = sq->cq.mcq.cqn;
1395 csp.wq_ctrl = &sq->wq_ctrl;
1396 csp.min_inline_mode = params->tx_min_inline_mode;
1397 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1398 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1400 goto err_free_icosq;
1405 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1406 mlx5e_free_icosq(sq);
1411 static void mlx5e_close_icosq(struct mlx5e_icosq *sq)
1413 struct mlx5e_channel *c = sq->channel;
1415 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1416 napi_synchronize(&c->napi);
1418 mlx5e_destroy_sq(c->mdev, sq->sqn);
1419 mlx5e_free_icosq(sq);
1422 static int mlx5e_open_xdpsq(struct mlx5e_channel *c,
1423 struct mlx5e_params *params,
1424 struct mlx5e_sq_param *param,
1425 struct mlx5e_xdpsq *sq)
1427 unsigned int ds_cnt = MLX5E_XDP_TX_DS_COUNT;
1428 struct mlx5e_create_sq_param csp = {};
1429 unsigned int inline_hdr_sz = 0;
1433 err = mlx5e_alloc_xdpsq(c, params, param, sq);
1438 csp.tisn = c->priv->tisn[0]; /* tc = 0 */
1439 csp.cqn = sq->cq.mcq.cqn;
1440 csp.wq_ctrl = &sq->wq_ctrl;
1441 csp.min_inline_mode = sq->min_inline_mode;
1442 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1443 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1445 goto err_free_xdpsq;
1447 if (sq->min_inline_mode != MLX5_INLINE_MODE_NONE) {
1448 inline_hdr_sz = MLX5E_XDP_MIN_INLINE;
1452 /* Pre initialize fixed WQE fields */
1453 for (i = 0; i < mlx5_wq_cyc_get_size(&sq->wq); i++) {
1454 struct mlx5e_tx_wqe *wqe = mlx5_wq_cyc_get_wqe(&sq->wq, i);
1455 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
1456 struct mlx5_wqe_eth_seg *eseg = &wqe->eth;
1457 struct mlx5_wqe_data_seg *dseg;
1459 cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
1460 eseg->inline_hdr.sz = cpu_to_be16(inline_hdr_sz);
1462 dseg = (struct mlx5_wqe_data_seg *)cseg + (ds_cnt - 1);
1463 dseg->lkey = sq->mkey_be;
1469 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1470 mlx5e_free_xdpsq(sq);
1475 static void mlx5e_close_xdpsq(struct mlx5e_xdpsq *sq)
1477 struct mlx5e_channel *c = sq->channel;
1479 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1480 napi_synchronize(&c->napi);
1482 mlx5e_destroy_sq(c->mdev, sq->sqn);
1483 mlx5e_free_xdpsq_descs(sq);
1484 mlx5e_free_xdpsq(sq);
1487 static int mlx5e_alloc_cq_common(struct mlx5_core_dev *mdev,
1488 struct mlx5e_cq_param *param,
1489 struct mlx5e_cq *cq)
1491 struct mlx5_core_cq *mcq = &cq->mcq;
1497 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1502 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1505 mcq->set_ci_db = cq->wq_ctrl.db.db;
1506 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1507 *mcq->set_ci_db = 0;
1509 mcq->vector = param->eq_ix;
1510 mcq->comp = mlx5e_completion_event;
1511 mcq->event = mlx5e_cq_error_event;
1514 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1515 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1525 static int mlx5e_alloc_cq(struct mlx5e_channel *c,
1526 struct mlx5e_cq_param *param,
1527 struct mlx5e_cq *cq)
1529 struct mlx5_core_dev *mdev = c->priv->mdev;
1532 param->wq.buf_numa_node = cpu_to_node(c->cpu);
1533 param->wq.db_numa_node = cpu_to_node(c->cpu);
1534 param->eq_ix = c->ix;
1536 err = mlx5e_alloc_cq_common(mdev, param, cq);
1538 cq->napi = &c->napi;
1544 static void mlx5e_free_cq(struct mlx5e_cq *cq)
1546 mlx5_wq_destroy(&cq->wq_ctrl);
1549 static int mlx5e_create_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
1551 struct mlx5_core_dev *mdev = cq->mdev;
1552 struct mlx5_core_cq *mcq = &cq->mcq;
1557 unsigned int irqn_not_used;
1561 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1562 sizeof(u64) * cq->wq_ctrl.buf.npages;
1563 in = kvzalloc(inlen, GFP_KERNEL);
1567 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1569 memcpy(cqc, param->cqc, sizeof(param->cqc));
1571 mlx5_fill_page_frag_array(&cq->wq_ctrl.buf,
1572 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
1574 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
1576 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
1577 MLX5_SET(cqc, cqc, c_eqn, eqn);
1578 MLX5_SET(cqc, cqc, uar_page, mdev->priv.uar->index);
1579 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
1580 MLX5_ADAPTER_PAGE_SHIFT);
1581 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1583 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
1595 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
1597 mlx5_core_destroy_cq(cq->mdev, &cq->mcq);
1600 static int mlx5e_open_cq(struct mlx5e_channel *c,
1601 struct net_dim_cq_moder moder,
1602 struct mlx5e_cq_param *param,
1603 struct mlx5e_cq *cq)
1605 struct mlx5_core_dev *mdev = c->mdev;
1608 err = mlx5e_alloc_cq(c, param, cq);
1612 err = mlx5e_create_cq(cq, param);
1616 if (MLX5_CAP_GEN(mdev, cq_moderation))
1617 mlx5_core_modify_cq_moderation(mdev, &cq->mcq, moder.usec, moder.pkts);
1626 static void mlx5e_close_cq(struct mlx5e_cq *cq)
1628 mlx5e_destroy_cq(cq);
1632 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
1634 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
1637 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1638 struct mlx5e_params *params,
1639 struct mlx5e_channel_param *cparam)
1644 for (tc = 0; tc < c->num_tc; tc++) {
1645 err = mlx5e_open_cq(c, params->tx_cq_moderation,
1646 &cparam->tx_cq, &c->sq[tc].cq);
1648 goto err_close_tx_cqs;
1654 for (tc--; tc >= 0; tc--)
1655 mlx5e_close_cq(&c->sq[tc].cq);
1660 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1664 for (tc = 0; tc < c->num_tc; tc++)
1665 mlx5e_close_cq(&c->sq[tc].cq);
1668 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1669 struct mlx5e_params *params,
1670 struct mlx5e_channel_param *cparam)
1672 struct mlx5e_priv *priv = c->priv;
1673 int err, tc, max_nch = priv->profile->max_nch(priv->mdev);
1675 for (tc = 0; tc < params->num_tc; tc++) {
1676 int txq_ix = c->ix + tc * max_nch;
1678 err = mlx5e_open_txqsq(c, c->priv->tisn[tc], txq_ix,
1679 params, &cparam->sq, &c->sq[tc], tc);
1687 for (tc--; tc >= 0; tc--)
1688 mlx5e_close_txqsq(&c->sq[tc]);
1693 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1697 for (tc = 0; tc < c->num_tc; tc++)
1698 mlx5e_close_txqsq(&c->sq[tc]);
1701 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1702 struct mlx5e_txqsq *sq, u32 rate)
1704 struct mlx5e_priv *priv = netdev_priv(dev);
1705 struct mlx5_core_dev *mdev = priv->mdev;
1706 struct mlx5e_modify_sq_param msp = {0};
1707 struct mlx5_rate_limit rl = {0};
1711 if (rate == sq->rate_limit)
1715 if (sq->rate_limit) {
1716 rl.rate = sq->rate_limit;
1717 /* remove current rl index to free space to next ones */
1718 mlx5_rl_remove_rate(mdev, &rl);
1725 err = mlx5_rl_add_rate(mdev, &rl_index, &rl);
1727 netdev_err(dev, "Failed configuring rate %u: %d\n",
1733 msp.curr_state = MLX5_SQC_STATE_RDY;
1734 msp.next_state = MLX5_SQC_STATE_RDY;
1735 msp.rl_index = rl_index;
1736 msp.rl_update = true;
1737 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1739 netdev_err(dev, "Failed configuring rate %u: %d\n",
1741 /* remove the rate from the table */
1743 mlx5_rl_remove_rate(mdev, &rl);
1747 sq->rate_limit = rate;
1751 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1753 struct mlx5e_priv *priv = netdev_priv(dev);
1754 struct mlx5_core_dev *mdev = priv->mdev;
1755 struct mlx5e_txqsq *sq = priv->txq2sq[index];
1758 if (!mlx5_rl_is_supported(mdev)) {
1759 netdev_err(dev, "Rate limiting is not supported on this device\n");
1763 /* rate is given in Mb/sec, HW config is in Kb/sec */
1766 /* Check whether rate in valid range, 0 is always valid */
1767 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1768 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1772 mutex_lock(&priv->state_lock);
1773 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1774 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1776 priv->tx_rates[index] = rate;
1777 mutex_unlock(&priv->state_lock);
1782 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1783 struct mlx5e_params *params,
1784 struct mlx5e_channel_param *cparam,
1785 struct mlx5e_channel **cp)
1787 struct net_dim_cq_moder icocq_moder = {0, 0};
1788 struct net_device *netdev = priv->netdev;
1789 int cpu = mlx5e_get_cpu(priv, ix);
1790 struct mlx5e_channel *c;
1795 c = kzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1800 c->mdev = priv->mdev;
1801 c->tstamp = &priv->tstamp;
1804 c->pdev = &priv->mdev->pdev->dev;
1805 c->netdev = priv->netdev;
1806 c->mkey_be = cpu_to_be32(priv->mdev->mlx5e_res.mkey.key);
1807 c->num_tc = params->num_tc;
1808 c->xdp = !!params->xdp_prog;
1809 c->stats = &priv->channel_stats[ix].ch;
1811 mlx5_vector2eqn(priv->mdev, ix, &eqn, &irq);
1812 c->irq_desc = irq_to_desc(irq);
1814 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1816 err = mlx5e_open_cq(c, icocq_moder, &cparam->icosq_cq, &c->icosq.cq);
1820 err = mlx5e_open_tx_cqs(c, params, cparam);
1822 goto err_close_icosq_cq;
1824 err = mlx5e_open_cq(c, params->rx_cq_moderation, &cparam->rx_cq, &c->rq.cq);
1826 goto err_close_tx_cqs;
1828 /* XDP SQ CQ params are same as normal TXQ sq CQ params */
1829 err = c->xdp ? mlx5e_open_cq(c, params->tx_cq_moderation,
1830 &cparam->tx_cq, &c->rq.xdpsq.cq) : 0;
1832 goto err_close_rx_cq;
1834 napi_enable(&c->napi);
1836 err = mlx5e_open_icosq(c, params, &cparam->icosq, &c->icosq);
1838 goto err_disable_napi;
1840 err = mlx5e_open_sqs(c, params, cparam);
1842 goto err_close_icosq;
1844 err = c->xdp ? mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, &c->rq.xdpsq) : 0;
1848 err = mlx5e_open_rq(c, params, &cparam->rq, &c->rq);
1850 goto err_close_xdp_sq;
1857 mlx5e_close_xdpsq(&c->rq.xdpsq);
1863 mlx5e_close_icosq(&c->icosq);
1866 napi_disable(&c->napi);
1868 mlx5e_close_cq(&c->rq.xdpsq.cq);
1871 mlx5e_close_cq(&c->rq.cq);
1874 mlx5e_close_tx_cqs(c);
1877 mlx5e_close_cq(&c->icosq.cq);
1880 netif_napi_del(&c->napi);
1886 static void mlx5e_activate_channel(struct mlx5e_channel *c)
1890 for (tc = 0; tc < c->num_tc; tc++)
1891 mlx5e_activate_txqsq(&c->sq[tc]);
1892 mlx5e_activate_rq(&c->rq);
1893 netif_set_xps_queue(c->netdev, get_cpu_mask(c->cpu), c->ix);
1896 static void mlx5e_deactivate_channel(struct mlx5e_channel *c)
1900 mlx5e_deactivate_rq(&c->rq);
1901 for (tc = 0; tc < c->num_tc; tc++)
1902 mlx5e_deactivate_txqsq(&c->sq[tc]);
1905 static void mlx5e_close_channel(struct mlx5e_channel *c)
1907 mlx5e_close_rq(&c->rq);
1909 mlx5e_close_xdpsq(&c->rq.xdpsq);
1911 mlx5e_close_icosq(&c->icosq);
1912 napi_disable(&c->napi);
1914 mlx5e_close_cq(&c->rq.xdpsq.cq);
1915 mlx5e_close_cq(&c->rq.cq);
1916 mlx5e_close_tx_cqs(c);
1917 mlx5e_close_cq(&c->icosq.cq);
1918 netif_napi_del(&c->napi);
1923 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
1924 struct mlx5e_params *params,
1925 struct mlx5e_rq_param *param)
1927 struct mlx5_core_dev *mdev = priv->mdev;
1928 void *rqc = param->rqc;
1929 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1931 switch (params->rq_wq_type) {
1932 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1933 MLX5_SET(wq, wq, log_wqe_num_of_strides,
1934 mlx5e_mpwqe_get_log_num_strides(mdev, params) -
1935 MLX5_MPWQE_LOG_NUM_STRIDES_BASE);
1936 MLX5_SET(wq, wq, log_wqe_stride_size,
1937 mlx5e_mpwqe_get_log_stride_size(mdev, params) -
1938 MLX5_MPWQE_LOG_STRIDE_SZ_BASE);
1939 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
1940 MLX5_SET(wq, wq, log_wq_sz, mlx5e_mpwqe_get_log_rq_size(params));
1942 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1943 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1944 MLX5_SET(wq, wq, log_wq_sz, params->log_rq_mtu_frames);
1947 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1948 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1949 MLX5_SET(wq, wq, pd, mdev->mlx5e_res.pdn);
1950 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
1951 MLX5_SET(rqc, rqc, vsd, params->vlan_strip_disable);
1952 MLX5_SET(rqc, rqc, scatter_fcs, params->scatter_fcs_en);
1954 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
1957 static void mlx5e_build_drop_rq_param(struct mlx5e_priv *priv,
1958 struct mlx5e_rq_param *param)
1960 struct mlx5_core_dev *mdev = priv->mdev;
1961 void *rqc = param->rqc;
1962 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1964 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1965 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1966 MLX5_SET(rqc, rqc, counter_set_id, priv->drop_rq_q_counter);
1968 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
1971 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
1972 struct mlx5e_sq_param *param)
1974 void *sqc = param->sqc;
1975 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1977 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1978 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
1980 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1983 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
1984 struct mlx5e_params *params,
1985 struct mlx5e_sq_param *param)
1987 void *sqc = param->sqc;
1988 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1990 mlx5e_build_sq_param_common(priv, param);
1991 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
1992 MLX5_SET(sqc, sqc, allow_swp, !!MLX5_IPSEC_DEV(priv->mdev));
1995 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1996 struct mlx5e_cq_param *param)
1998 void *cqc = param->cqc;
2000 MLX5_SET(cqc, cqc, uar_page, priv->mdev->priv.uar->index);
2003 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
2004 struct mlx5e_params *params,
2005 struct mlx5e_cq_param *param)
2007 struct mlx5_core_dev *mdev = priv->mdev;
2008 void *cqc = param->cqc;
2011 switch (params->rq_wq_type) {
2012 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2013 log_cq_size = mlx5e_mpwqe_get_log_rq_size(params) +
2014 mlx5e_mpwqe_get_log_num_strides(mdev, params);
2016 default: /* MLX5_WQ_TYPE_LINKED_LIST */
2017 log_cq_size = params->log_rq_mtu_frames;
2020 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
2021 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
2022 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
2023 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
2026 mlx5e_build_common_cq_param(priv, param);
2027 param->cq_period_mode = params->rx_cq_moderation.cq_period_mode;
2030 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
2031 struct mlx5e_params *params,
2032 struct mlx5e_cq_param *param)
2034 void *cqc = param->cqc;
2036 MLX5_SET(cqc, cqc, log_cq_size, params->log_sq_size);
2038 mlx5e_build_common_cq_param(priv, param);
2039 param->cq_period_mode = params->tx_cq_moderation.cq_period_mode;
2042 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
2044 struct mlx5e_cq_param *param)
2046 void *cqc = param->cqc;
2048 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
2050 mlx5e_build_common_cq_param(priv, param);
2052 param->cq_period_mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2055 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
2057 struct mlx5e_sq_param *param)
2059 void *sqc = param->sqc;
2060 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2062 mlx5e_build_sq_param_common(priv, param);
2064 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
2065 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
2068 static void mlx5e_build_xdpsq_param(struct mlx5e_priv *priv,
2069 struct mlx5e_params *params,
2070 struct mlx5e_sq_param *param)
2072 void *sqc = param->sqc;
2073 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2075 mlx5e_build_sq_param_common(priv, param);
2076 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2079 static void mlx5e_build_channel_param(struct mlx5e_priv *priv,
2080 struct mlx5e_params *params,
2081 struct mlx5e_channel_param *cparam)
2083 u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
2085 mlx5e_build_rq_param(priv, params, &cparam->rq);
2086 mlx5e_build_sq_param(priv, params, &cparam->sq);
2087 mlx5e_build_xdpsq_param(priv, params, &cparam->xdp_sq);
2088 mlx5e_build_icosq_param(priv, icosq_log_wq_sz, &cparam->icosq);
2089 mlx5e_build_rx_cq_param(priv, params, &cparam->rx_cq);
2090 mlx5e_build_tx_cq_param(priv, params, &cparam->tx_cq);
2091 mlx5e_build_ico_cq_param(priv, icosq_log_wq_sz, &cparam->icosq_cq);
2094 int mlx5e_open_channels(struct mlx5e_priv *priv,
2095 struct mlx5e_channels *chs)
2097 struct mlx5e_channel_param *cparam;
2101 chs->num = chs->params.num_channels;
2103 chs->c = kcalloc(chs->num, sizeof(struct mlx5e_channel *), GFP_KERNEL);
2104 cparam = kzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
2105 if (!chs->c || !cparam)
2108 mlx5e_build_channel_param(priv, &chs->params, cparam);
2109 for (i = 0; i < chs->num; i++) {
2110 err = mlx5e_open_channel(priv, i, &chs->params, cparam, &chs->c[i]);
2112 goto err_close_channels;
2119 for (i--; i >= 0; i--)
2120 mlx5e_close_channel(chs->c[i]);
2129 static void mlx5e_activate_channels(struct mlx5e_channels *chs)
2133 for (i = 0; i < chs->num; i++)
2134 mlx5e_activate_channel(chs->c[i]);
2137 static int mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels *chs)
2142 for (i = 0; i < chs->num; i++)
2143 err |= mlx5e_wait_for_min_rx_wqes(&chs->c[i]->rq,
2146 return err ? -ETIMEDOUT : 0;
2149 static void mlx5e_deactivate_channels(struct mlx5e_channels *chs)
2153 for (i = 0; i < chs->num; i++)
2154 mlx5e_deactivate_channel(chs->c[i]);
2157 void mlx5e_close_channels(struct mlx5e_channels *chs)
2161 for (i = 0; i < chs->num; i++)
2162 mlx5e_close_channel(chs->c[i]);
2169 mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, struct mlx5e_rqt *rqt)
2171 struct mlx5_core_dev *mdev = priv->mdev;
2178 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
2179 in = kvzalloc(inlen, GFP_KERNEL);
2183 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
2185 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2186 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
2188 for (i = 0; i < sz; i++)
2189 MLX5_SET(rqtc, rqtc, rq_num[i], priv->drop_rq.rqn);
2191 err = mlx5_core_create_rqt(mdev, in, inlen, &rqt->rqtn);
2193 rqt->enabled = true;
2199 void mlx5e_destroy_rqt(struct mlx5e_priv *priv, struct mlx5e_rqt *rqt)
2201 rqt->enabled = false;
2202 mlx5_core_destroy_rqt(priv->mdev, rqt->rqtn);
2205 int mlx5e_create_indirect_rqt(struct mlx5e_priv *priv)
2207 struct mlx5e_rqt *rqt = &priv->indir_rqt;
2210 err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, rqt);
2212 mlx5_core_warn(priv->mdev, "create indirect rqts failed, %d\n", err);
2216 int mlx5e_create_direct_rqts(struct mlx5e_priv *priv)
2218 struct mlx5e_rqt *rqt;
2222 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2223 rqt = &priv->direct_tir[ix].rqt;
2224 err = mlx5e_create_rqt(priv, 1 /*size */, rqt);
2226 goto err_destroy_rqts;
2232 mlx5_core_warn(priv->mdev, "create direct rqts failed, %d\n", err);
2233 for (ix--; ix >= 0; ix--)
2234 mlx5e_destroy_rqt(priv, &priv->direct_tir[ix].rqt);
2239 void mlx5e_destroy_direct_rqts(struct mlx5e_priv *priv)
2243 for (i = 0; i < priv->profile->max_nch(priv->mdev); i++)
2244 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
2247 static int mlx5e_rx_hash_fn(int hfunc)
2249 return (hfunc == ETH_RSS_HASH_TOP) ?
2250 MLX5_RX_HASH_FN_TOEPLITZ :
2251 MLX5_RX_HASH_FN_INVERTED_XOR8;
2254 int mlx5e_bits_invert(unsigned long a, int size)
2259 for (i = 0; i < size; i++)
2260 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
2265 static void mlx5e_fill_rqt_rqns(struct mlx5e_priv *priv, int sz,
2266 struct mlx5e_redirect_rqt_param rrp, void *rqtc)
2270 for (i = 0; i < sz; i++) {
2276 if (rrp.rss.hfunc == ETH_RSS_HASH_XOR)
2277 ix = mlx5e_bits_invert(i, ilog2(sz));
2279 ix = priv->channels.params.indirection_rqt[ix];
2280 rqn = rrp.rss.channels->c[ix]->rq.rqn;
2284 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
2288 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz,
2289 struct mlx5e_redirect_rqt_param rrp)
2291 struct mlx5_core_dev *mdev = priv->mdev;
2297 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
2298 in = kvzalloc(inlen, GFP_KERNEL);
2302 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
2304 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2305 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
2306 mlx5e_fill_rqt_rqns(priv, sz, rrp, rqtc);
2307 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
2313 static u32 mlx5e_get_direct_rqn(struct mlx5e_priv *priv, int ix,
2314 struct mlx5e_redirect_rqt_param rrp)
2319 if (ix >= rrp.rss.channels->num)
2320 return priv->drop_rq.rqn;
2322 return rrp.rss.channels->c[ix]->rq.rqn;
2325 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv,
2326 struct mlx5e_redirect_rqt_param rrp)
2331 if (priv->indir_rqt.enabled) {
2333 rqtn = priv->indir_rqt.rqtn;
2334 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, rrp);
2337 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2338 struct mlx5e_redirect_rqt_param direct_rrp = {
2341 .rqn = mlx5e_get_direct_rqn(priv, ix, rrp)
2345 /* Direct RQ Tables */
2346 if (!priv->direct_tir[ix].rqt.enabled)
2349 rqtn = priv->direct_tir[ix].rqt.rqtn;
2350 mlx5e_redirect_rqt(priv, rqtn, 1, direct_rrp);
2354 static void mlx5e_redirect_rqts_to_channels(struct mlx5e_priv *priv,
2355 struct mlx5e_channels *chs)
2357 struct mlx5e_redirect_rqt_param rrp = {
2362 .hfunc = chs->params.rss_hfunc,
2367 mlx5e_redirect_rqts(priv, rrp);
2370 static void mlx5e_redirect_rqts_to_drop(struct mlx5e_priv *priv)
2372 struct mlx5e_redirect_rqt_param drop_rrp = {
2375 .rqn = priv->drop_rq.rqn,
2379 mlx5e_redirect_rqts(priv, drop_rrp);
2382 static void mlx5e_build_tir_ctx_lro(struct mlx5e_params *params, void *tirc)
2384 if (!params->lro_en)
2387 #define ROUGH_MAX_L2_L3_HDR_SZ 256
2389 MLX5_SET(tirc, tirc, lro_enable_mask,
2390 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2391 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
2392 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
2393 (params->lro_wqe_sz - ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
2394 MLX5_SET(tirc, tirc, lro_timeout_period_usecs, params->lro_timeout);
2397 void mlx5e_build_indir_tir_ctx_hash(struct mlx5e_params *params,
2398 enum mlx5e_traffic_types tt,
2399 void *tirc, bool inner)
2401 void *hfso = inner ? MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner) :
2402 MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2404 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
2405 MLX5_HASH_FIELD_SEL_DST_IP)
2407 #define MLX5_HASH_IP_L4PORTS (MLX5_HASH_FIELD_SEL_SRC_IP |\
2408 MLX5_HASH_FIELD_SEL_DST_IP |\
2409 MLX5_HASH_FIELD_SEL_L4_SPORT |\
2410 MLX5_HASH_FIELD_SEL_L4_DPORT)
2412 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
2413 MLX5_HASH_FIELD_SEL_DST_IP |\
2414 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2416 MLX5_SET(tirc, tirc, rx_hash_fn, mlx5e_rx_hash_fn(params->rss_hfunc));
2417 if (params->rss_hfunc == ETH_RSS_HASH_TOP) {
2418 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
2419 rx_hash_toeplitz_key);
2420 size_t len = MLX5_FLD_SZ_BYTES(tirc,
2421 rx_hash_toeplitz_key);
2423 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
2424 memcpy(rss_key, params->toeplitz_hash_key, len);
2428 case MLX5E_TT_IPV4_TCP:
2429 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2430 MLX5_L3_PROT_TYPE_IPV4);
2431 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2432 MLX5_L4_PROT_TYPE_TCP);
2433 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2434 MLX5_HASH_IP_L4PORTS);
2437 case MLX5E_TT_IPV6_TCP:
2438 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2439 MLX5_L3_PROT_TYPE_IPV6);
2440 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2441 MLX5_L4_PROT_TYPE_TCP);
2442 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2443 MLX5_HASH_IP_L4PORTS);
2446 case MLX5E_TT_IPV4_UDP:
2447 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2448 MLX5_L3_PROT_TYPE_IPV4);
2449 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2450 MLX5_L4_PROT_TYPE_UDP);
2451 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2452 MLX5_HASH_IP_L4PORTS);
2455 case MLX5E_TT_IPV6_UDP:
2456 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2457 MLX5_L3_PROT_TYPE_IPV6);
2458 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2459 MLX5_L4_PROT_TYPE_UDP);
2460 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2461 MLX5_HASH_IP_L4PORTS);
2464 case MLX5E_TT_IPV4_IPSEC_AH:
2465 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2466 MLX5_L3_PROT_TYPE_IPV4);
2467 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2468 MLX5_HASH_IP_IPSEC_SPI);
2471 case MLX5E_TT_IPV6_IPSEC_AH:
2472 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2473 MLX5_L3_PROT_TYPE_IPV6);
2474 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2475 MLX5_HASH_IP_IPSEC_SPI);
2478 case MLX5E_TT_IPV4_IPSEC_ESP:
2479 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2480 MLX5_L3_PROT_TYPE_IPV4);
2481 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2482 MLX5_HASH_IP_IPSEC_SPI);
2485 case MLX5E_TT_IPV6_IPSEC_ESP:
2486 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2487 MLX5_L3_PROT_TYPE_IPV6);
2488 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2489 MLX5_HASH_IP_IPSEC_SPI);
2493 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2494 MLX5_L3_PROT_TYPE_IPV4);
2495 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2500 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2501 MLX5_L3_PROT_TYPE_IPV6);
2502 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2506 WARN_ONCE(true, "%s: bad traffic type!\n", __func__);
2510 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
2512 struct mlx5_core_dev *mdev = priv->mdev;
2521 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
2522 in = kvzalloc(inlen, GFP_KERNEL);
2526 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
2527 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2529 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2531 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2532 err = mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in,
2538 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2539 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
2551 static void mlx5e_build_inner_indir_tir_ctx(struct mlx5e_priv *priv,
2552 enum mlx5e_traffic_types tt,
2555 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2557 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2559 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2560 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
2561 MLX5_SET(tirc, tirc, tunneled_offload_en, 0x1);
2563 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, true);
2566 static int mlx5e_set_mtu(struct mlx5_core_dev *mdev,
2567 struct mlx5e_params *params, u16 mtu)
2569 u16 hw_mtu = MLX5E_SW2HW_MTU(params, mtu);
2572 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
2576 /* Update vport context MTU */
2577 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
2581 static void mlx5e_query_mtu(struct mlx5_core_dev *mdev,
2582 struct mlx5e_params *params, u16 *mtu)
2587 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
2588 if (err || !hw_mtu) /* fallback to port oper mtu */
2589 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
2591 *mtu = MLX5E_HW2SW_MTU(params, hw_mtu);
2594 static int mlx5e_set_dev_port_mtu(struct mlx5e_priv *priv)
2596 struct mlx5e_params *params = &priv->channels.params;
2597 struct net_device *netdev = priv->netdev;
2598 struct mlx5_core_dev *mdev = priv->mdev;
2602 err = mlx5e_set_mtu(mdev, params, params->sw_mtu);
2606 mlx5e_query_mtu(mdev, params, &mtu);
2607 if (mtu != params->sw_mtu)
2608 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
2609 __func__, mtu, params->sw_mtu);
2611 params->sw_mtu = mtu;
2615 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
2617 struct mlx5e_priv *priv = netdev_priv(netdev);
2618 int nch = priv->channels.params.num_channels;
2619 int ntc = priv->channels.params.num_tc;
2622 netdev_reset_tc(netdev);
2627 netdev_set_num_tc(netdev, ntc);
2629 /* Map netdev TCs to offset 0
2630 * We have our own UP to TXQ mapping for QoS
2632 for (tc = 0; tc < ntc; tc++)
2633 netdev_set_tc_queue(netdev, tc, nch, 0);
2636 static void mlx5e_build_tc2txq_maps(struct mlx5e_priv *priv)
2638 int max_nch = priv->profile->max_nch(priv->mdev);
2641 for (i = 0; i < max_nch; i++)
2642 for (tc = 0; tc < priv->profile->max_tc; tc++)
2643 priv->channel_tc2txq[i][tc] = i + tc * max_nch;
2646 static void mlx5e_build_tx2sq_maps(struct mlx5e_priv *priv)
2648 struct mlx5e_channel *c;
2649 struct mlx5e_txqsq *sq;
2652 for (i = 0; i < priv->channels.num; i++) {
2653 c = priv->channels.c[i];
2654 for (tc = 0; tc < c->num_tc; tc++) {
2656 priv->txq2sq[sq->txq_ix] = sq;
2661 void mlx5e_activate_priv_channels(struct mlx5e_priv *priv)
2663 int num_txqs = priv->channels.num * priv->channels.params.num_tc;
2664 struct net_device *netdev = priv->netdev;
2666 mlx5e_netdev_set_tcs(netdev);
2667 netif_set_real_num_tx_queues(netdev, num_txqs);
2668 netif_set_real_num_rx_queues(netdev, priv->channels.num);
2670 mlx5e_build_tx2sq_maps(priv);
2671 mlx5e_activate_channels(&priv->channels);
2672 write_lock(&priv->stats_lock);
2673 priv->channels_active = true;
2674 write_unlock(&priv->stats_lock);
2675 netif_tx_start_all_queues(priv->netdev);
2677 if (MLX5_VPORT_MANAGER(priv->mdev))
2678 mlx5e_add_sqs_fwd_rules(priv);
2680 mlx5e_wait_channels_min_rx_wqes(&priv->channels);
2681 mlx5e_redirect_rqts_to_channels(priv, &priv->channels);
2684 void mlx5e_deactivate_priv_channels(struct mlx5e_priv *priv)
2686 mlx5e_redirect_rqts_to_drop(priv);
2688 if (MLX5_VPORT_MANAGER(priv->mdev))
2689 mlx5e_remove_sqs_fwd_rules(priv);
2691 /* FIXME: This is a W/A only for tx timeout watch dog false alarm when
2692 * polling for inactive tx queues.
2694 netif_tx_stop_all_queues(priv->netdev);
2695 netif_tx_disable(priv->netdev);
2696 write_lock(&priv->stats_lock);
2697 priv->channels_active = false;
2698 write_unlock(&priv->stats_lock);
2699 mlx5e_deactivate_channels(&priv->channels);
2702 void mlx5e_switch_priv_channels(struct mlx5e_priv *priv,
2703 struct mlx5e_channels *new_chs,
2704 mlx5e_fp_hw_modify hw_modify)
2706 struct net_device *netdev = priv->netdev;
2709 new_num_txqs = new_chs->num * new_chs->params.num_tc;
2711 carrier_ok = netif_carrier_ok(netdev);
2712 netif_carrier_off(netdev);
2714 if (new_num_txqs < netdev->real_num_tx_queues)
2715 netif_set_real_num_tx_queues(netdev, new_num_txqs);
2717 mlx5e_deactivate_priv_channels(priv);
2718 mlx5e_close_channels(&priv->channels);
2720 priv->channels = *new_chs;
2722 /* New channels are ready to roll, modify HW settings if needed */
2726 mlx5e_refresh_tirs(priv, false);
2727 mlx5e_activate_priv_channels(priv);
2729 /* return carrier back if needed */
2731 netif_carrier_on(netdev);
2734 void mlx5e_timestamp_init(struct mlx5e_priv *priv)
2736 priv->tstamp.tx_type = HWTSTAMP_TX_OFF;
2737 priv->tstamp.rx_filter = HWTSTAMP_FILTER_NONE;
2740 int mlx5e_open_locked(struct net_device *netdev)
2742 struct mlx5e_priv *priv = netdev_priv(netdev);
2745 set_bit(MLX5E_STATE_OPENED, &priv->state);
2747 err = mlx5e_open_channels(priv, &priv->channels);
2749 goto err_clear_state_opened_flag;
2751 mlx5e_refresh_tirs(priv, false);
2752 mlx5e_activate_priv_channels(priv);
2753 if (priv->profile->update_carrier)
2754 priv->profile->update_carrier(priv);
2756 if (priv->profile->update_stats)
2757 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
2761 err_clear_state_opened_flag:
2762 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2766 int mlx5e_open(struct net_device *netdev)
2768 struct mlx5e_priv *priv = netdev_priv(netdev);
2771 mutex_lock(&priv->state_lock);
2772 err = mlx5e_open_locked(netdev);
2774 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_UP);
2775 mutex_unlock(&priv->state_lock);
2777 if (mlx5e_vxlan_allowed(priv->mdev))
2778 udp_tunnel_get_rx_info(netdev);
2783 int mlx5e_close_locked(struct net_device *netdev)
2785 struct mlx5e_priv *priv = netdev_priv(netdev);
2787 /* May already be CLOSED in case a previous configuration operation
2788 * (e.g RX/TX queue size change) that involves close&open failed.
2790 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2793 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2795 netif_carrier_off(priv->netdev);
2796 mlx5e_deactivate_priv_channels(priv);
2797 mlx5e_close_channels(&priv->channels);
2802 int mlx5e_close(struct net_device *netdev)
2804 struct mlx5e_priv *priv = netdev_priv(netdev);
2807 if (!netif_device_present(netdev))
2810 mutex_lock(&priv->state_lock);
2811 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_DOWN);
2812 err = mlx5e_close_locked(netdev);
2813 mutex_unlock(&priv->state_lock);
2818 static int mlx5e_alloc_drop_rq(struct mlx5_core_dev *mdev,
2819 struct mlx5e_rq *rq,
2820 struct mlx5e_rq_param *param)
2822 void *rqc = param->rqc;
2823 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
2826 param->wq.db_numa_node = param->wq.buf_numa_node;
2828 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
2833 /* Mark as unused given "Drop-RQ" packets never reach XDP */
2834 xdp_rxq_info_unused(&rq->xdp_rxq);
2841 static int mlx5e_alloc_drop_cq(struct mlx5_core_dev *mdev,
2842 struct mlx5e_cq *cq,
2843 struct mlx5e_cq_param *param)
2845 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
2846 param->wq.db_numa_node = dev_to_node(&mdev->pdev->dev);
2848 return mlx5e_alloc_cq_common(mdev, param, cq);
2851 static int mlx5e_open_drop_rq(struct mlx5e_priv *priv,
2852 struct mlx5e_rq *drop_rq)
2854 struct mlx5_core_dev *mdev = priv->mdev;
2855 struct mlx5e_cq_param cq_param = {};
2856 struct mlx5e_rq_param rq_param = {};
2857 struct mlx5e_cq *cq = &drop_rq->cq;
2860 mlx5e_build_drop_rq_param(priv, &rq_param);
2862 err = mlx5e_alloc_drop_cq(mdev, cq, &cq_param);
2866 err = mlx5e_create_cq(cq, &cq_param);
2870 err = mlx5e_alloc_drop_rq(mdev, drop_rq, &rq_param);
2872 goto err_destroy_cq;
2874 err = mlx5e_create_rq(drop_rq, &rq_param);
2878 err = mlx5e_modify_rq_state(drop_rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
2880 mlx5_core_warn(priv->mdev, "modify_rq_state failed, rx_if_down_packets won't be counted %d\n", err);
2885 mlx5e_free_rq(drop_rq);
2888 mlx5e_destroy_cq(cq);
2896 static void mlx5e_close_drop_rq(struct mlx5e_rq *drop_rq)
2898 mlx5e_destroy_rq(drop_rq);
2899 mlx5e_free_rq(drop_rq);
2900 mlx5e_destroy_cq(&drop_rq->cq);
2901 mlx5e_free_cq(&drop_rq->cq);
2904 int mlx5e_create_tis(struct mlx5_core_dev *mdev, int tc,
2905 u32 underlay_qpn, u32 *tisn)
2907 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
2908 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
2910 MLX5_SET(tisc, tisc, prio, tc << 1);
2911 MLX5_SET(tisc, tisc, underlay_qpn, underlay_qpn);
2912 MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.td.tdn);
2914 if (mlx5_lag_is_lacp_owner(mdev))
2915 MLX5_SET(tisc, tisc, strict_lag_tx_port_affinity, 1);
2917 return mlx5_core_create_tis(mdev, in, sizeof(in), tisn);
2920 void mlx5e_destroy_tis(struct mlx5_core_dev *mdev, u32 tisn)
2922 mlx5_core_destroy_tis(mdev, tisn);
2925 int mlx5e_create_tises(struct mlx5e_priv *priv)
2930 for (tc = 0; tc < priv->profile->max_tc; tc++) {
2931 err = mlx5e_create_tis(priv->mdev, tc, 0, &priv->tisn[tc]);
2933 goto err_close_tises;
2939 for (tc--; tc >= 0; tc--)
2940 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
2945 void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
2949 for (tc = 0; tc < priv->profile->max_tc; tc++)
2950 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
2953 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv,
2954 enum mlx5e_traffic_types tt,
2957 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2959 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2961 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2962 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
2963 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, false);
2966 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 rqtn, u32 *tirc)
2968 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2970 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2972 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2973 MLX5_SET(tirc, tirc, indirect_table, rqtn);
2974 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
2977 int mlx5e_create_indirect_tirs(struct mlx5e_priv *priv)
2979 struct mlx5e_tir *tir;
2987 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2988 in = kvzalloc(inlen, GFP_KERNEL);
2992 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2993 memset(in, 0, inlen);
2994 tir = &priv->indir_tir[tt];
2995 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2996 mlx5e_build_indir_tir_ctx(priv, tt, tirc);
2997 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
2999 mlx5_core_warn(priv->mdev, "create indirect tirs failed, %d\n", err);
3000 goto err_destroy_inner_tirs;
3004 if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
3007 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
3008 memset(in, 0, inlen);
3009 tir = &priv->inner_indir_tir[i];
3010 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3011 mlx5e_build_inner_indir_tir_ctx(priv, i, tirc);
3012 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3014 mlx5_core_warn(priv->mdev, "create inner indirect tirs failed, %d\n", err);
3015 goto err_destroy_inner_tirs;
3024 err_destroy_inner_tirs:
3025 for (i--; i >= 0; i--)
3026 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3028 for (tt--; tt >= 0; tt--)
3029 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[tt]);
3036 int mlx5e_create_direct_tirs(struct mlx5e_priv *priv)
3038 int nch = priv->profile->max_nch(priv->mdev);
3039 struct mlx5e_tir *tir;
3046 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3047 in = kvzalloc(inlen, GFP_KERNEL);
3051 for (ix = 0; ix < nch; ix++) {
3052 memset(in, 0, inlen);
3053 tir = &priv->direct_tir[ix];
3054 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3055 mlx5e_build_direct_tir_ctx(priv, priv->direct_tir[ix].rqt.rqtn, tirc);
3056 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3058 goto err_destroy_ch_tirs;
3065 err_destroy_ch_tirs:
3066 mlx5_core_warn(priv->mdev, "create direct tirs failed, %d\n", err);
3067 for (ix--; ix >= 0; ix--)
3068 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[ix]);
3075 void mlx5e_destroy_indirect_tirs(struct mlx5e_priv *priv)
3079 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3080 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[i]);
3082 if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
3085 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3086 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3089 void mlx5e_destroy_direct_tirs(struct mlx5e_priv *priv)
3091 int nch = priv->profile->max_nch(priv->mdev);
3094 for (i = 0; i < nch; i++)
3095 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[i]);
3098 static int mlx5e_modify_channels_scatt