2 * Copyright (c) 2015-2016, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <net/tc_act/tc_gact.h>
34 #include <net/pkt_cls.h>
35 #include <linux/mlx5/fs.h>
36 #include <net/vxlan.h>
37 #include <linux/bpf.h>
38 #include <net/page_pool.h>
43 #include "en_accel/ipsec.h"
44 #include "en_accel/ipsec_rxtx.h"
45 #include "en_accel/tls.h"
46 #include "accel/ipsec.h"
47 #include "accel/tls.h"
48 #include "lib/vxlan.h"
49 #include "lib/clock.h"
53 #include "en/monitor_stats.h"
55 struct mlx5e_rq_param {
56 u32 rqc[MLX5_ST_SZ_DW(rqc)];
57 struct mlx5_wq_param wq;
58 struct mlx5e_rq_frags_info frags_info;
61 struct mlx5e_sq_param {
62 u32 sqc[MLX5_ST_SZ_DW(sqc)];
63 struct mlx5_wq_param wq;
67 struct mlx5e_cq_param {
68 u32 cqc[MLX5_ST_SZ_DW(cqc)];
69 struct mlx5_wq_param wq;
74 struct mlx5e_channel_param {
75 struct mlx5e_rq_param rq;
76 struct mlx5e_sq_param sq;
77 struct mlx5e_sq_param xdp_sq;
78 struct mlx5e_sq_param icosq;
79 struct mlx5e_cq_param rx_cq;
80 struct mlx5e_cq_param tx_cq;
81 struct mlx5e_cq_param icosq_cq;
84 bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
86 bool striding_rq_umr = MLX5_CAP_GEN(mdev, striding_rq) &&
87 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
88 MLX5_CAP_ETH(mdev, reg_umr_sq);
89 u16 max_wqe_sz_cap = MLX5_CAP_GEN(mdev, max_wqe_sz_sq);
90 bool inline_umr = MLX5E_UMR_WQE_INLINE_SZ <= max_wqe_sz_cap;
95 mlx5_core_warn(mdev, "Cannot support Striding RQ: UMR WQE size (%d) exceeds maximum supported (%d).\n",
96 (int)MLX5E_UMR_WQE_INLINE_SZ, max_wqe_sz_cap);
102 static u32 mlx5e_rx_get_linear_frag_sz(struct mlx5e_params *params)
104 u16 hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
105 u16 linear_rq_headroom = params->xdp_prog ?
106 XDP_PACKET_HEADROOM : MLX5_RX_HEADROOM;
109 linear_rq_headroom += NET_IP_ALIGN;
111 frag_sz = MLX5_SKB_FRAG_SZ(linear_rq_headroom + hw_mtu);
113 if (params->xdp_prog && frag_sz < PAGE_SIZE)
119 static u8 mlx5e_mpwqe_log_pkts_per_wqe(struct mlx5e_params *params)
121 u32 linear_frag_sz = mlx5e_rx_get_linear_frag_sz(params);
123 return MLX5_MPWRQ_LOG_WQE_SZ - order_base_2(linear_frag_sz);
126 static bool mlx5e_rx_is_linear_skb(struct mlx5_core_dev *mdev,
127 struct mlx5e_params *params)
129 u32 frag_sz = mlx5e_rx_get_linear_frag_sz(params);
131 return !params->lro_en && frag_sz <= PAGE_SIZE;
134 #define MLX5_MAX_MPWQE_LOG_WQE_STRIDE_SZ ((BIT(__mlx5_bit_sz(wq, log_wqe_stride_size)) - 1) + \
135 MLX5_MPWQE_LOG_STRIDE_SZ_BASE)
136 static bool mlx5e_rx_mpwqe_is_linear_skb(struct mlx5_core_dev *mdev,
137 struct mlx5e_params *params)
139 u32 frag_sz = mlx5e_rx_get_linear_frag_sz(params);
140 s8 signed_log_num_strides_param;
143 if (!mlx5e_rx_is_linear_skb(mdev, params))
146 if (order_base_2(frag_sz) > MLX5_MAX_MPWQE_LOG_WQE_STRIDE_SZ)
149 if (MLX5_CAP_GEN(mdev, ext_stride_num_range))
152 log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ - order_base_2(frag_sz);
153 signed_log_num_strides_param =
154 (s8)log_num_strides - MLX5_MPWQE_LOG_NUM_STRIDES_BASE;
156 return signed_log_num_strides_param >= 0;
159 static u8 mlx5e_mpwqe_get_log_rq_size(struct mlx5e_params *params)
161 if (params->log_rq_mtu_frames <
162 mlx5e_mpwqe_log_pkts_per_wqe(params) + MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW)
163 return MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW;
165 return params->log_rq_mtu_frames - mlx5e_mpwqe_log_pkts_per_wqe(params);
168 static u8 mlx5e_mpwqe_get_log_stride_size(struct mlx5_core_dev *mdev,
169 struct mlx5e_params *params)
171 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, params))
172 return order_base_2(mlx5e_rx_get_linear_frag_sz(params));
174 return MLX5E_MPWQE_STRIDE_SZ(mdev,
175 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
178 static u8 mlx5e_mpwqe_get_log_num_strides(struct mlx5_core_dev *mdev,
179 struct mlx5e_params *params)
181 return MLX5_MPWRQ_LOG_WQE_SZ -
182 mlx5e_mpwqe_get_log_stride_size(mdev, params);
185 static u16 mlx5e_get_rq_headroom(struct mlx5_core_dev *mdev,
186 struct mlx5e_params *params)
188 u16 linear_rq_headroom = params->xdp_prog ?
189 XDP_PACKET_HEADROOM : MLX5_RX_HEADROOM;
192 linear_rq_headroom += NET_IP_ALIGN;
194 is_linear_skb = (params->rq_wq_type == MLX5_WQ_TYPE_CYCLIC) ?
195 mlx5e_rx_is_linear_skb(mdev, params) :
196 mlx5e_rx_mpwqe_is_linear_skb(mdev, params);
198 return is_linear_skb ? linear_rq_headroom : 0;
201 void mlx5e_init_rq_type_params(struct mlx5_core_dev *mdev,
202 struct mlx5e_params *params)
204 params->lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
205 params->log_rq_mtu_frames = is_kdump_kernel() ?
206 MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE :
207 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
209 mlx5_core_info(mdev, "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
210 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
211 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ ?
212 BIT(mlx5e_mpwqe_get_log_rq_size(params)) :
213 BIT(params->log_rq_mtu_frames),
214 BIT(mlx5e_mpwqe_get_log_stride_size(mdev, params)),
215 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
218 bool mlx5e_striding_rq_possible(struct mlx5_core_dev *mdev,
219 struct mlx5e_params *params)
221 return mlx5e_check_fragmented_striding_rq_cap(mdev) &&
222 !MLX5_IPSEC_DEV(mdev) &&
223 !(params->xdp_prog && !mlx5e_rx_mpwqe_is_linear_skb(mdev, params));
226 void mlx5e_set_rq_type(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
228 params->rq_wq_type = mlx5e_striding_rq_possible(mdev, params) &&
229 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ) ?
230 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
234 void mlx5e_update_carrier(struct mlx5e_priv *priv)
236 struct mlx5_core_dev *mdev = priv->mdev;
239 port_state = mlx5_query_vport_state(mdev,
240 MLX5_VPORT_STATE_OP_MOD_VNIC_VPORT,
243 if (port_state == VPORT_STATE_UP) {
244 netdev_info(priv->netdev, "Link up\n");
245 netif_carrier_on(priv->netdev);
247 netdev_info(priv->netdev, "Link down\n");
248 netif_carrier_off(priv->netdev);
252 static void mlx5e_update_carrier_work(struct work_struct *work)
254 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
255 update_carrier_work);
257 mutex_lock(&priv->state_lock);
258 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
259 if (priv->profile->update_carrier)
260 priv->profile->update_carrier(priv);
261 mutex_unlock(&priv->state_lock);
264 void mlx5e_update_stats(struct mlx5e_priv *priv)
268 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
269 if (mlx5e_stats_grps[i].update_stats)
270 mlx5e_stats_grps[i].update_stats(priv);
273 void mlx5e_update_ndo_stats(struct mlx5e_priv *priv)
277 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
278 if (mlx5e_stats_grps[i].update_stats_mask &
279 MLX5E_NDO_UPDATE_STATS)
280 mlx5e_stats_grps[i].update_stats(priv);
283 static void mlx5e_update_stats_work(struct work_struct *work)
285 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
288 mutex_lock(&priv->state_lock);
289 priv->profile->update_stats(priv);
290 mutex_unlock(&priv->state_lock);
293 void mlx5e_queue_update_stats(struct mlx5e_priv *priv)
295 if (!priv->profile->update_stats)
298 if (unlikely(test_bit(MLX5E_STATE_DESTROYING, &priv->state)))
301 queue_work(priv->wq, &priv->update_stats_work);
304 static int async_event(struct notifier_block *nb, unsigned long event, void *data)
306 struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb);
307 struct mlx5_eqe *eqe = data;
309 if (event != MLX5_EVENT_TYPE_PORT_CHANGE)
312 switch (eqe->sub_type) {
313 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
314 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
315 queue_work(priv->wq, &priv->update_carrier_work);
324 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
326 priv->events_nb.notifier_call = async_event;
327 mlx5_notifier_register(priv->mdev, &priv->events_nb);
330 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
332 mlx5_notifier_unregister(priv->mdev, &priv->events_nb);
335 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq,
336 struct mlx5e_icosq *sq,
337 struct mlx5e_umr_wqe *wqe)
339 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
340 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
341 u8 ds_cnt = DIV_ROUND_UP(MLX5E_UMR_WQE_INLINE_SZ, MLX5_SEND_WQE_DS);
343 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
345 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
346 cseg->imm = rq->mkey_be;
348 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN | MLX5_UMR_INLINE;
349 ucseg->xlt_octowords =
350 cpu_to_be16(MLX5_MTT_OCTW(MLX5_MPWRQ_PAGES_PER_WQE));
351 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
354 static u32 mlx5e_rqwq_get_size(struct mlx5e_rq *rq)
356 switch (rq->wq_type) {
357 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
358 return mlx5_wq_ll_get_size(&rq->mpwqe.wq);
360 return mlx5_wq_cyc_get_size(&rq->wqe.wq);
364 static u32 mlx5e_rqwq_get_cur_sz(struct mlx5e_rq *rq)
366 switch (rq->wq_type) {
367 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
368 return rq->mpwqe.wq.cur_sz;
370 return rq->wqe.wq.cur_sz;
374 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq,
375 struct mlx5e_channel *c)
377 int wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
379 rq->mpwqe.info = kvzalloc_node(array_size(wq_sz,
380 sizeof(*rq->mpwqe.info)),
381 GFP_KERNEL, cpu_to_node(c->cpu));
385 mlx5e_build_umr_wqe(rq, &c->icosq, &rq->mpwqe.umr_wqe);
390 static int mlx5e_create_umr_mkey(struct mlx5_core_dev *mdev,
391 u64 npages, u8 page_shift,
392 struct mlx5_core_mkey *umr_mkey)
394 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
399 in = kvzalloc(inlen, GFP_KERNEL);
403 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
405 MLX5_SET(mkc, mkc, free, 1);
406 MLX5_SET(mkc, mkc, umr_en, 1);
407 MLX5_SET(mkc, mkc, lw, 1);
408 MLX5_SET(mkc, mkc, lr, 1);
409 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT);
411 MLX5_SET(mkc, mkc, qpn, 0xffffff);
412 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.pdn);
413 MLX5_SET64(mkc, mkc, len, npages << page_shift);
414 MLX5_SET(mkc, mkc, translations_octword_size,
415 MLX5_MTT_OCTW(npages));
416 MLX5_SET(mkc, mkc, log_page_size, page_shift);
418 err = mlx5_core_create_mkey(mdev, umr_mkey, in, inlen);
424 static int mlx5e_create_rq_umr_mkey(struct mlx5_core_dev *mdev, struct mlx5e_rq *rq)
426 u64 num_mtts = MLX5E_REQUIRED_MTTS(mlx5_wq_ll_get_size(&rq->mpwqe.wq));
428 return mlx5e_create_umr_mkey(mdev, num_mtts, PAGE_SHIFT, &rq->umr_mkey);
431 static inline u64 mlx5e_get_mpwqe_offset(struct mlx5e_rq *rq, u16 wqe_ix)
433 return (wqe_ix << MLX5E_LOG_ALIGNED_MPWQE_PPW) << PAGE_SHIFT;
436 static void mlx5e_init_frags_partition(struct mlx5e_rq *rq)
438 struct mlx5e_wqe_frag_info next_frag, *prev;
441 next_frag.di = &rq->wqe.di[0];
442 next_frag.offset = 0;
445 for (i = 0; i < mlx5_wq_cyc_get_size(&rq->wqe.wq); i++) {
446 struct mlx5e_rq_frag_info *frag_info = &rq->wqe.info.arr[0];
447 struct mlx5e_wqe_frag_info *frag =
448 &rq->wqe.frags[i << rq->wqe.info.log_num_frags];
451 for (f = 0; f < rq->wqe.info.num_frags; f++, frag++) {
452 if (next_frag.offset + frag_info[f].frag_stride > PAGE_SIZE) {
454 next_frag.offset = 0;
456 prev->last_in_page = true;
461 next_frag.offset += frag_info[f].frag_stride;
467 prev->last_in_page = true;
470 static int mlx5e_init_di_list(struct mlx5e_rq *rq,
471 struct mlx5e_params *params,
474 int len = wq_sz << rq->wqe.info.log_num_frags;
476 rq->wqe.di = kvzalloc_node(array_size(len, sizeof(*rq->wqe.di)),
477 GFP_KERNEL, cpu_to_node(cpu));
481 mlx5e_init_frags_partition(rq);
486 static void mlx5e_free_di_list(struct mlx5e_rq *rq)
491 static int mlx5e_alloc_rq(struct mlx5e_channel *c,
492 struct mlx5e_params *params,
493 struct mlx5e_rq_param *rqp,
496 struct page_pool_params pp_params = { 0 };
497 struct mlx5_core_dev *mdev = c->mdev;
498 void *rqc = rqp->rqc;
499 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
505 rqp->wq.db_numa_node = cpu_to_node(c->cpu);
507 rq->wq_type = params->rq_wq_type;
509 rq->netdev = c->netdev;
510 rq->tstamp = c->tstamp;
511 rq->clock = &mdev->clock;
515 rq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
516 rq->stats = &c->priv->channel_stats[c->ix].rq;
518 rq->xdp_prog = params->xdp_prog ? bpf_prog_inc(params->xdp_prog) : NULL;
519 if (IS_ERR(rq->xdp_prog)) {
520 err = PTR_ERR(rq->xdp_prog);
522 goto err_rq_wq_destroy;
525 err = xdp_rxq_info_reg(&rq->xdp_rxq, rq->netdev, rq->ix);
527 goto err_rq_wq_destroy;
529 rq->buff.map_dir = rq->xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
530 rq->buff.headroom = mlx5e_get_rq_headroom(mdev, params);
531 pool_size = 1 << params->log_rq_mtu_frames;
533 switch (rq->wq_type) {
534 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
535 err = mlx5_wq_ll_create(mdev, &rqp->wq, rqc_wq, &rq->mpwqe.wq,
540 rq->mpwqe.wq.db = &rq->mpwqe.wq.db[MLX5_RCV_DBR];
542 wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
544 pool_size = MLX5_MPWRQ_PAGES_PER_WQE << mlx5e_mpwqe_get_log_rq_size(params);
546 rq->post_wqes = mlx5e_post_rx_mpwqes;
547 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
549 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe_mpwqe;
550 #ifdef CONFIG_MLX5_EN_IPSEC
551 if (MLX5_IPSEC_DEV(mdev)) {
553 netdev_err(c->netdev, "MPWQE RQ with IPSec offload not supported\n");
554 goto err_rq_wq_destroy;
557 if (!rq->handle_rx_cqe) {
559 netdev_err(c->netdev, "RX handler of MPWQE RQ is not set, err %d\n", err);
560 goto err_rq_wq_destroy;
563 rq->mpwqe.skb_from_cqe_mpwrq =
564 mlx5e_rx_mpwqe_is_linear_skb(mdev, params) ?
565 mlx5e_skb_from_cqe_mpwrq_linear :
566 mlx5e_skb_from_cqe_mpwrq_nonlinear;
567 rq->mpwqe.log_stride_sz = mlx5e_mpwqe_get_log_stride_size(mdev, params);
568 rq->mpwqe.num_strides = BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params));
570 err = mlx5e_create_rq_umr_mkey(mdev, rq);
572 goto err_rq_wq_destroy;
573 rq->mkey_be = cpu_to_be32(rq->umr_mkey.key);
575 err = mlx5e_rq_alloc_mpwqe_info(rq, c);
579 default: /* MLX5_WQ_TYPE_CYCLIC */
580 err = mlx5_wq_cyc_create(mdev, &rqp->wq, rqc_wq, &rq->wqe.wq,
585 rq->wqe.wq.db = &rq->wqe.wq.db[MLX5_RCV_DBR];
587 wq_sz = mlx5_wq_cyc_get_size(&rq->wqe.wq);
589 rq->wqe.info = rqp->frags_info;
591 kvzalloc_node(array_size(sizeof(*rq->wqe.frags),
592 (wq_sz << rq->wqe.info.log_num_frags)),
593 GFP_KERNEL, cpu_to_node(c->cpu));
594 if (!rq->wqe.frags) {
599 err = mlx5e_init_di_list(rq, params, wq_sz, c->cpu);
602 rq->post_wqes = mlx5e_post_rx_wqes;
603 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
605 #ifdef CONFIG_MLX5_EN_IPSEC
607 rq->handle_rx_cqe = mlx5e_ipsec_handle_rx_cqe;
610 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe;
611 if (!rq->handle_rx_cqe) {
613 netdev_err(c->netdev, "RX handler of RQ is not set, err %d\n", err);
617 rq->wqe.skb_from_cqe = mlx5e_rx_is_linear_skb(mdev, params) ?
618 mlx5e_skb_from_cqe_linear :
619 mlx5e_skb_from_cqe_nonlinear;
620 rq->mkey_be = c->mkey_be;
623 /* Create a page_pool and register it with rxq */
625 pp_params.flags = 0; /* No-internal DMA mapping in page_pool */
626 pp_params.pool_size = pool_size;
627 pp_params.nid = cpu_to_node(c->cpu);
628 pp_params.dev = c->pdev;
629 pp_params.dma_dir = rq->buff.map_dir;
631 /* page_pool can be used even when there is no rq->xdp_prog,
632 * given page_pool does not handle DMA mapping there is no
633 * required state to clear. And page_pool gracefully handle
636 rq->page_pool = page_pool_create(&pp_params);
637 if (IS_ERR(rq->page_pool)) {
638 err = PTR_ERR(rq->page_pool);
639 rq->page_pool = NULL;
642 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
643 MEM_TYPE_PAGE_POOL, rq->page_pool);
647 for (i = 0; i < wq_sz; i++) {
648 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
649 struct mlx5e_rx_wqe_ll *wqe =
650 mlx5_wq_ll_get_wqe(&rq->mpwqe.wq, i);
652 rq->mpwqe.num_strides << rq->mpwqe.log_stride_sz;
653 u64 dma_offset = mlx5e_get_mpwqe_offset(rq, i);
655 wqe->data[0].addr = cpu_to_be64(dma_offset + rq->buff.headroom);
656 wqe->data[0].byte_count = cpu_to_be32(byte_count);
657 wqe->data[0].lkey = rq->mkey_be;
659 struct mlx5e_rx_wqe_cyc *wqe =
660 mlx5_wq_cyc_get_wqe(&rq->wqe.wq, i);
663 for (f = 0; f < rq->wqe.info.num_frags; f++) {
664 u32 frag_size = rq->wqe.info.arr[f].frag_size |
665 MLX5_HW_START_PADDING;
667 wqe->data[f].byte_count = cpu_to_be32(frag_size);
668 wqe->data[f].lkey = rq->mkey_be;
670 /* check if num_frags is not a pow of two */
671 if (rq->wqe.info.num_frags < (1 << rq->wqe.info.log_num_frags)) {
672 wqe->data[f].byte_count = 0;
673 wqe->data[f].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
674 wqe->data[f].addr = 0;
679 INIT_WORK(&rq->dim.work, mlx5e_rx_dim_work);
681 switch (params->rx_cq_moderation.cq_period_mode) {
682 case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
683 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE;
685 case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
687 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
690 rq->page_cache.head = 0;
691 rq->page_cache.tail = 0;
696 switch (rq->wq_type) {
697 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
698 kvfree(rq->mpwqe.info);
699 mlx5_core_destroy_mkey(mdev, &rq->umr_mkey);
701 default: /* MLX5_WQ_TYPE_CYCLIC */
702 kvfree(rq->wqe.frags);
703 mlx5e_free_di_list(rq);
708 bpf_prog_put(rq->xdp_prog);
709 xdp_rxq_info_unreg(&rq->xdp_rxq);
711 page_pool_destroy(rq->page_pool);
712 mlx5_wq_destroy(&rq->wq_ctrl);
717 static void mlx5e_free_rq(struct mlx5e_rq *rq)
722 bpf_prog_put(rq->xdp_prog);
724 xdp_rxq_info_unreg(&rq->xdp_rxq);
726 page_pool_destroy(rq->page_pool);
728 switch (rq->wq_type) {
729 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
730 kvfree(rq->mpwqe.info);
731 mlx5_core_destroy_mkey(rq->mdev, &rq->umr_mkey);
733 default: /* MLX5_WQ_TYPE_CYCLIC */
734 kvfree(rq->wqe.frags);
735 mlx5e_free_di_list(rq);
738 for (i = rq->page_cache.head; i != rq->page_cache.tail;
739 i = (i + 1) & (MLX5E_CACHE_SIZE - 1)) {
740 struct mlx5e_dma_info *dma_info = &rq->page_cache.page_cache[i];
742 mlx5e_page_release(rq, dma_info, false);
744 mlx5_wq_destroy(&rq->wq_ctrl);
747 static int mlx5e_create_rq(struct mlx5e_rq *rq,
748 struct mlx5e_rq_param *param)
750 struct mlx5_core_dev *mdev = rq->mdev;
758 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
759 sizeof(u64) * rq->wq_ctrl.buf.npages;
760 in = kvzalloc(inlen, GFP_KERNEL);
764 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
765 wq = MLX5_ADDR_OF(rqc, rqc, wq);
767 memcpy(rqc, param->rqc, sizeof(param->rqc));
769 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
770 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
771 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
772 MLX5_ADAPTER_PAGE_SHIFT);
773 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
775 mlx5_fill_page_frag_array(&rq->wq_ctrl.buf,
776 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
778 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
785 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
788 struct mlx5_core_dev *mdev = rq->mdev;
795 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
796 in = kvzalloc(inlen, GFP_KERNEL);
800 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
802 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
803 MLX5_SET(rqc, rqc, state, next_state);
805 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
812 static int mlx5e_modify_rq_scatter_fcs(struct mlx5e_rq *rq, bool enable)
814 struct mlx5e_channel *c = rq->channel;
815 struct mlx5e_priv *priv = c->priv;
816 struct mlx5_core_dev *mdev = priv->mdev;
823 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
824 in = kvzalloc(inlen, GFP_KERNEL);
828 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
830 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
831 MLX5_SET64(modify_rq_in, in, modify_bitmask,
832 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_SCATTER_FCS);
833 MLX5_SET(rqc, rqc, scatter_fcs, enable);
834 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
836 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
843 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
845 struct mlx5e_channel *c = rq->channel;
846 struct mlx5_core_dev *mdev = c->mdev;
852 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
853 in = kvzalloc(inlen, GFP_KERNEL);
857 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
859 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
860 MLX5_SET64(modify_rq_in, in, modify_bitmask,
861 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
862 MLX5_SET(rqc, rqc, vsd, vsd);
863 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
865 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
872 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
874 mlx5_core_destroy_rq(rq->mdev, rq->rqn);
877 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq, int wait_time)
879 unsigned long exp_time = jiffies + msecs_to_jiffies(wait_time);
880 struct mlx5e_channel *c = rq->channel;
882 u16 min_wqes = mlx5_min_rx_wqes(rq->wq_type, mlx5e_rqwq_get_size(rq));
885 if (mlx5e_rqwq_get_cur_sz(rq) >= min_wqes)
889 } while (time_before(jiffies, exp_time));
891 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",
892 c->ix, rq->rqn, mlx5e_rqwq_get_cur_sz(rq), min_wqes);
897 static void mlx5e_free_rx_descs(struct mlx5e_rq *rq)
902 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
903 struct mlx5_wq_ll *wq = &rq->mpwqe.wq;
905 /* UMR WQE (if in progress) is always at wq->head */
906 if (rq->mpwqe.umr_in_progress)
907 rq->dealloc_wqe(rq, wq->head);
909 while (!mlx5_wq_ll_is_empty(wq)) {
910 struct mlx5e_rx_wqe_ll *wqe;
912 wqe_ix_be = *wq->tail_next;
913 wqe_ix = be16_to_cpu(wqe_ix_be);
914 wqe = mlx5_wq_ll_get_wqe(wq, wqe_ix);
915 rq->dealloc_wqe(rq, wqe_ix);
916 mlx5_wq_ll_pop(wq, wqe_ix_be,
917 &wqe->next.next_wqe_index);
920 struct mlx5_wq_cyc *wq = &rq->wqe.wq;
922 while (!mlx5_wq_cyc_is_empty(wq)) {
923 wqe_ix = mlx5_wq_cyc_get_tail(wq);
924 rq->dealloc_wqe(rq, wqe_ix);
931 static int mlx5e_open_rq(struct mlx5e_channel *c,
932 struct mlx5e_params *params,
933 struct mlx5e_rq_param *param,
938 err = mlx5e_alloc_rq(c, params, param, rq);
942 err = mlx5e_create_rq(rq, param);
946 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
950 if (params->rx_dim_enabled)
951 __set_bit(MLX5E_RQ_STATE_AM, &c->rq.state);
953 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_NO_CSUM_COMPLETE))
954 __set_bit(MLX5E_RQ_STATE_NO_CSUM_COMPLETE, &c->rq.state);
959 mlx5e_destroy_rq(rq);
966 static void mlx5e_activate_rq(struct mlx5e_rq *rq)
968 struct mlx5e_icosq *sq = &rq->channel->icosq;
969 struct mlx5_wq_cyc *wq = &sq->wq;
970 struct mlx5e_tx_wqe *nopwqe;
972 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
974 set_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
975 sq->db.ico_wqe[pi].opcode = MLX5_OPCODE_NOP;
976 nopwqe = mlx5e_post_nop(wq, sq->sqn, &sq->pc);
977 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &nopwqe->ctrl);
980 static void mlx5e_deactivate_rq(struct mlx5e_rq *rq)
982 clear_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
983 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
986 static void mlx5e_close_rq(struct mlx5e_rq *rq)
988 cancel_work_sync(&rq->dim.work);
989 mlx5e_destroy_rq(rq);
990 mlx5e_free_rx_descs(rq);
994 static void mlx5e_free_xdpsq_db(struct mlx5e_xdpsq *sq)
996 kvfree(sq->db.xdpi_fifo.xi);
997 kvfree(sq->db.wqe_info);
1000 static int mlx5e_alloc_xdpsq_fifo(struct mlx5e_xdpsq *sq, int numa)
1002 struct mlx5e_xdp_info_fifo *xdpi_fifo = &sq->db.xdpi_fifo;
1003 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1004 int dsegs_per_wq = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
1006 xdpi_fifo->xi = kvzalloc_node(sizeof(*xdpi_fifo->xi) * dsegs_per_wq,
1011 xdpi_fifo->pc = &sq->xdpi_fifo_pc;
1012 xdpi_fifo->cc = &sq->xdpi_fifo_cc;
1013 xdpi_fifo->mask = dsegs_per_wq - 1;
1018 static int mlx5e_alloc_xdpsq_db(struct mlx5e_xdpsq *sq, int numa)
1020 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1023 sq->db.wqe_info = kvzalloc_node(sizeof(*sq->db.wqe_info) * wq_sz,
1025 if (!sq->db.wqe_info)
1028 err = mlx5e_alloc_xdpsq_fifo(sq, numa);
1030 mlx5e_free_xdpsq_db(sq);
1037 static int mlx5e_alloc_xdpsq(struct mlx5e_channel *c,
1038 struct mlx5e_params *params,
1039 struct mlx5e_sq_param *param,
1040 struct mlx5e_xdpsq *sq,
1043 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1044 struct mlx5_core_dev *mdev = c->mdev;
1045 struct mlx5_wq_cyc *wq = &sq->wq;
1049 sq->mkey_be = c->mkey_be;
1051 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1052 sq->min_inline_mode = params->tx_min_inline_mode;
1053 sq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
1054 sq->stats = is_redirect ?
1055 &c->priv->channel_stats[c->ix].xdpsq :
1056 &c->priv->channel_stats[c->ix].rq_xdpsq;
1058 param->wq.db_numa_node = cpu_to_node(c->cpu);
1059 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1062 wq->db = &wq->db[MLX5_SND_DBR];
1064 err = mlx5e_alloc_xdpsq_db(sq, cpu_to_node(c->cpu));
1066 goto err_sq_wq_destroy;
1071 mlx5_wq_destroy(&sq->wq_ctrl);
1076 static void mlx5e_free_xdpsq(struct mlx5e_xdpsq *sq)
1078 mlx5e_free_xdpsq_db(sq);
1079 mlx5_wq_destroy(&sq->wq_ctrl);
1082 static void mlx5e_free_icosq_db(struct mlx5e_icosq *sq)
1084 kvfree(sq->db.ico_wqe);
1087 static int mlx5e_alloc_icosq_db(struct mlx5e_icosq *sq, int numa)
1089 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1091 sq->db.ico_wqe = kvzalloc_node(array_size(wq_sz,
1092 sizeof(*sq->db.ico_wqe)),
1094 if (!sq->db.ico_wqe)
1100 static int mlx5e_alloc_icosq(struct mlx5e_channel *c,
1101 struct mlx5e_sq_param *param,
1102 struct mlx5e_icosq *sq)
1104 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1105 struct mlx5_core_dev *mdev = c->mdev;
1106 struct mlx5_wq_cyc *wq = &sq->wq;
1110 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1112 param->wq.db_numa_node = cpu_to_node(c->cpu);
1113 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1116 wq->db = &wq->db[MLX5_SND_DBR];
1118 err = mlx5e_alloc_icosq_db(sq, cpu_to_node(c->cpu));
1120 goto err_sq_wq_destroy;
1125 mlx5_wq_destroy(&sq->wq_ctrl);
1130 static void mlx5e_free_icosq(struct mlx5e_icosq *sq)
1132 mlx5e_free_icosq_db(sq);
1133 mlx5_wq_destroy(&sq->wq_ctrl);
1136 static void mlx5e_free_txqsq_db(struct mlx5e_txqsq *sq)
1138 kvfree(sq->db.wqe_info);
1139 kvfree(sq->db.dma_fifo);
1142 static int mlx5e_alloc_txqsq_db(struct mlx5e_txqsq *sq, int numa)
1144 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1145 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
1147 sq->db.dma_fifo = kvzalloc_node(array_size(df_sz,
1148 sizeof(*sq->db.dma_fifo)),
1150 sq->db.wqe_info = kvzalloc_node(array_size(wq_sz,
1151 sizeof(*sq->db.wqe_info)),
1153 if (!sq->db.dma_fifo || !sq->db.wqe_info) {
1154 mlx5e_free_txqsq_db(sq);
1158 sq->dma_fifo_mask = df_sz - 1;
1163 static void mlx5e_sq_recover(struct work_struct *work);
1164 static int mlx5e_alloc_txqsq(struct mlx5e_channel *c,
1166 struct mlx5e_params *params,
1167 struct mlx5e_sq_param *param,
1168 struct mlx5e_txqsq *sq,
1171 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1172 struct mlx5_core_dev *mdev = c->mdev;
1173 struct mlx5_wq_cyc *wq = &sq->wq;
1177 sq->tstamp = c->tstamp;
1178 sq->clock = &mdev->clock;
1179 sq->mkey_be = c->mkey_be;
1181 sq->txq_ix = txq_ix;
1182 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1183 sq->min_inline_mode = params->tx_min_inline_mode;
1184 sq->stats = &c->priv->channel_stats[c->ix].sq[tc];
1185 INIT_WORK(&sq->recover.recover_work, mlx5e_sq_recover);
1186 if (MLX5_IPSEC_DEV(c->priv->mdev))
1187 set_bit(MLX5E_SQ_STATE_IPSEC, &sq->state);
1188 if (mlx5_accel_is_tls_device(c->priv->mdev))
1189 set_bit(MLX5E_SQ_STATE_TLS, &sq->state);
1191 param->wq.db_numa_node = cpu_to_node(c->cpu);
1192 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1195 wq->db = &wq->db[MLX5_SND_DBR];
1197 err = mlx5e_alloc_txqsq_db(sq, cpu_to_node(c->cpu));
1199 goto err_sq_wq_destroy;
1201 INIT_WORK(&sq->dim.work, mlx5e_tx_dim_work);
1202 sq->dim.mode = params->tx_cq_moderation.cq_period_mode;
1207 mlx5_wq_destroy(&sq->wq_ctrl);
1212 static void mlx5e_free_txqsq(struct mlx5e_txqsq *sq)
1214 mlx5e_free_txqsq_db(sq);
1215 mlx5_wq_destroy(&sq->wq_ctrl);
1218 struct mlx5e_create_sq_param {
1219 struct mlx5_wq_ctrl *wq_ctrl;
1226 static int mlx5e_create_sq(struct mlx5_core_dev *mdev,
1227 struct mlx5e_sq_param *param,
1228 struct mlx5e_create_sq_param *csp,
1237 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1238 sizeof(u64) * csp->wq_ctrl->buf.npages;
1239 in = kvzalloc(inlen, GFP_KERNEL);
1243 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1244 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1246 memcpy(sqc, param->sqc, sizeof(param->sqc));
1247 MLX5_SET(sqc, sqc, tis_lst_sz, csp->tis_lst_sz);
1248 MLX5_SET(sqc, sqc, tis_num_0, csp->tisn);
1249 MLX5_SET(sqc, sqc, cqn, csp->cqn);
1251 if (MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5_CAP_INLINE_MODE_VPORT_CONTEXT)
1252 MLX5_SET(sqc, sqc, min_wqe_inline_mode, csp->min_inline_mode);
1254 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1255 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1257 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1258 MLX5_SET(wq, wq, uar_page, mdev->mlx5e_res.bfreg.index);
1259 MLX5_SET(wq, wq, log_wq_pg_sz, csp->wq_ctrl->buf.page_shift -
1260 MLX5_ADAPTER_PAGE_SHIFT);
1261 MLX5_SET64(wq, wq, dbr_addr, csp->wq_ctrl->db.dma);
1263 mlx5_fill_page_frag_array(&csp->wq_ctrl->buf,
1264 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
1266 err = mlx5_core_create_sq(mdev, in, inlen, sqn);
1273 struct mlx5e_modify_sq_param {
1280 static int mlx5e_modify_sq(struct mlx5_core_dev *mdev, u32 sqn,
1281 struct mlx5e_modify_sq_param *p)
1288 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1289 in = kvzalloc(inlen, GFP_KERNEL);
1293 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1295 MLX5_SET(modify_sq_in, in, sq_state, p->curr_state);
1296 MLX5_SET(sqc, sqc, state, p->next_state);
1297 if (p->rl_update && p->next_state == MLX5_SQC_STATE_RDY) {
1298 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
1299 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, p->rl_index);
1302 err = mlx5_core_modify_sq(mdev, sqn, in, inlen);
1309 static void mlx5e_destroy_sq(struct mlx5_core_dev *mdev, u32 sqn)
1311 mlx5_core_destroy_sq(mdev, sqn);
1314 static int mlx5e_create_sq_rdy(struct mlx5_core_dev *mdev,
1315 struct mlx5e_sq_param *param,
1316 struct mlx5e_create_sq_param *csp,
1319 struct mlx5e_modify_sq_param msp = {0};
1322 err = mlx5e_create_sq(mdev, param, csp, sqn);
1326 msp.curr_state = MLX5_SQC_STATE_RST;
1327 msp.next_state = MLX5_SQC_STATE_RDY;
1328 err = mlx5e_modify_sq(mdev, *sqn, &msp);
1330 mlx5e_destroy_sq(mdev, *sqn);
1335 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1336 struct mlx5e_txqsq *sq, u32 rate);
1338 static int mlx5e_open_txqsq(struct mlx5e_channel *c,
1341 struct mlx5e_params *params,
1342 struct mlx5e_sq_param *param,
1343 struct mlx5e_txqsq *sq,
1346 struct mlx5e_create_sq_param csp = {};
1350 err = mlx5e_alloc_txqsq(c, txq_ix, params, param, sq, tc);
1356 csp.cqn = sq->cq.mcq.cqn;
1357 csp.wq_ctrl = &sq->wq_ctrl;
1358 csp.min_inline_mode = sq->min_inline_mode;
1359 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1361 goto err_free_txqsq;
1363 tx_rate = c->priv->tx_rates[sq->txq_ix];
1365 mlx5e_set_sq_maxrate(c->netdev, sq, tx_rate);
1367 if (params->tx_dim_enabled)
1368 sq->state |= BIT(MLX5E_SQ_STATE_AM);
1373 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1374 mlx5e_free_txqsq(sq);
1379 static void mlx5e_reset_txqsq_cc_pc(struct mlx5e_txqsq *sq)
1381 WARN_ONCE(sq->cc != sq->pc,
1382 "SQ 0x%x: cc (0x%x) != pc (0x%x)\n",
1383 sq->sqn, sq->cc, sq->pc);
1385 sq->dma_fifo_cc = 0;
1389 static void mlx5e_activate_txqsq(struct mlx5e_txqsq *sq)
1391 sq->txq = netdev_get_tx_queue(sq->channel->netdev, sq->txq_ix);
1392 clear_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state);
1393 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1394 netdev_tx_reset_queue(sq->txq);
1395 netif_tx_start_queue(sq->txq);
1398 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
1400 __netif_tx_lock_bh(txq);
1401 netif_tx_stop_queue(txq);
1402 __netif_tx_unlock_bh(txq);
1405 static void mlx5e_deactivate_txqsq(struct mlx5e_txqsq *sq)
1407 struct mlx5e_channel *c = sq->channel;
1408 struct mlx5_wq_cyc *wq = &sq->wq;
1410 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1411 /* prevent netif_tx_wake_queue */
1412 napi_synchronize(&c->napi);
1414 netif_tx_disable_queue(sq->txq);
1416 /* last doorbell out, godspeed .. */
1417 if (mlx5e_wqc_has_room_for(wq, sq->cc, sq->pc, 1)) {
1418 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
1419 struct mlx5e_tx_wqe *nop;
1421 sq->db.wqe_info[pi].skb = NULL;
1422 nop = mlx5e_post_nop(wq, sq->sqn, &sq->pc);
1423 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &nop->ctrl);
1427 static void mlx5e_close_txqsq(struct mlx5e_txqsq *sq)
1429 struct mlx5e_channel *c = sq->channel;
1430 struct mlx5_core_dev *mdev = c->mdev;
1431 struct mlx5_rate_limit rl = {0};
1433 cancel_work_sync(&sq->dim.work);
1434 mlx5e_destroy_sq(mdev, sq->sqn);
1435 if (sq->rate_limit) {
1436 rl.rate = sq->rate_limit;
1437 mlx5_rl_remove_rate(mdev, &rl);
1439 mlx5e_free_txqsq_descs(sq);
1440 mlx5e_free_txqsq(sq);
1443 static int mlx5e_wait_for_sq_flush(struct mlx5e_txqsq *sq)
1445 unsigned long exp_time = jiffies + msecs_to_jiffies(2000);
1447 while (time_before(jiffies, exp_time)) {
1448 if (sq->cc == sq->pc)
1454 netdev_err(sq->channel->netdev,
1455 "Wait for SQ 0x%x flush timeout (sq cc = 0x%x, sq pc = 0x%x)\n",
1456 sq->sqn, sq->cc, sq->pc);
1461 static int mlx5e_sq_to_ready(struct mlx5e_txqsq *sq, int curr_state)
1463 struct mlx5_core_dev *mdev = sq->channel->mdev;
1464 struct net_device *dev = sq->channel->netdev;
1465 struct mlx5e_modify_sq_param msp = {0};
1468 msp.curr_state = curr_state;
1469 msp.next_state = MLX5_SQC_STATE_RST;
1471 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1473 netdev_err(dev, "Failed to move sq 0x%x to reset\n", sq->sqn);
1477 memset(&msp, 0, sizeof(msp));
1478 msp.curr_state = MLX5_SQC_STATE_RST;
1479 msp.next_state = MLX5_SQC_STATE_RDY;
1481 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1483 netdev_err(dev, "Failed to move sq 0x%x to ready\n", sq->sqn);
1490 static void mlx5e_sq_recover(struct work_struct *work)
1492 struct mlx5e_txqsq_recover *recover =
1493 container_of(work, struct mlx5e_txqsq_recover,
1495 struct mlx5e_txqsq *sq = container_of(recover, struct mlx5e_txqsq,
1497 struct mlx5_core_dev *mdev = sq->channel->mdev;
1498 struct net_device *dev = sq->channel->netdev;
1502 err = mlx5_core_query_sq_state(mdev, sq->sqn, &state);
1504 netdev_err(dev, "Failed to query SQ 0x%x state. err = %d\n",
1509 if (state != MLX5_RQC_STATE_ERR) {
1510 netdev_err(dev, "SQ 0x%x not in ERROR state\n", sq->sqn);
1514 netif_tx_disable_queue(sq->txq);
1516 if (mlx5e_wait_for_sq_flush(sq))
1519 /* If the interval between two consecutive recovers per SQ is too
1520 * short, don't recover to avoid infinite loop of ERR_CQE -> recover.
1521 * If we reached this state, there is probably a bug that needs to be
1522 * fixed. let's keep the queue close and let tx timeout cleanup.
1524 if (jiffies_to_msecs(jiffies - recover->last_recover) <
1525 MLX5E_SQ_RECOVER_MIN_INTERVAL) {
1526 netdev_err(dev, "Recover SQ 0x%x canceled, too many error CQEs\n",
1531 /* At this point, no new packets will arrive from the stack as TXQ is
1532 * marked with QUEUE_STATE_DRV_XOFF. In addition, NAPI cleared all
1533 * pending WQEs. SQ can safely reset the SQ.
1535 if (mlx5e_sq_to_ready(sq, state))
1538 mlx5e_reset_txqsq_cc_pc(sq);
1539 sq->stats->recover++;
1540 recover->last_recover = jiffies;
1541 mlx5e_activate_txqsq(sq);
1544 static int mlx5e_open_icosq(struct mlx5e_channel *c,
1545 struct mlx5e_params *params,
1546 struct mlx5e_sq_param *param,
1547 struct mlx5e_icosq *sq)
1549 struct mlx5e_create_sq_param csp = {};
1552 err = mlx5e_alloc_icosq(c, param, sq);
1556 csp.cqn = sq->cq.mcq.cqn;
1557 csp.wq_ctrl = &sq->wq_ctrl;
1558 csp.min_inline_mode = params->tx_min_inline_mode;
1559 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1560 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1562 goto err_free_icosq;
1567 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1568 mlx5e_free_icosq(sq);
1573 static void mlx5e_close_icosq(struct mlx5e_icosq *sq)
1575 struct mlx5e_channel *c = sq->channel;
1577 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1578 napi_synchronize(&c->napi);
1580 mlx5e_destroy_sq(c->mdev, sq->sqn);
1581 mlx5e_free_icosq(sq);
1584 static int mlx5e_open_xdpsq(struct mlx5e_channel *c,
1585 struct mlx5e_params *params,
1586 struct mlx5e_sq_param *param,
1587 struct mlx5e_xdpsq *sq,
1590 struct mlx5e_create_sq_param csp = {};
1593 err = mlx5e_alloc_xdpsq(c, params, param, sq, is_redirect);
1598 csp.tisn = c->priv->tisn[0]; /* tc = 0 */
1599 csp.cqn = sq->cq.mcq.cqn;
1600 csp.wq_ctrl = &sq->wq_ctrl;
1601 csp.min_inline_mode = sq->min_inline_mode;
1602 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1603 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1605 goto err_free_xdpsq;
1607 mlx5e_set_xmit_fp(sq, param->is_mpw);
1609 if (!param->is_mpw) {
1610 unsigned int ds_cnt = MLX5E_XDP_TX_DS_COUNT;
1611 unsigned int inline_hdr_sz = 0;
1614 if (sq->min_inline_mode != MLX5_INLINE_MODE_NONE) {
1615 inline_hdr_sz = MLX5E_XDP_MIN_INLINE;
1619 /* Pre initialize fixed WQE fields */
1620 for (i = 0; i < mlx5_wq_cyc_get_size(&sq->wq); i++) {
1621 struct mlx5e_xdp_wqe_info *wi = &sq->db.wqe_info[i];
1622 struct mlx5e_tx_wqe *wqe = mlx5_wq_cyc_get_wqe(&sq->wq, i);
1623 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
1624 struct mlx5_wqe_eth_seg *eseg = &wqe->eth;
1625 struct mlx5_wqe_data_seg *dseg;
1627 cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
1628 eseg->inline_hdr.sz = cpu_to_be16(inline_hdr_sz);
1630 dseg = (struct mlx5_wqe_data_seg *)cseg + (ds_cnt - 1);
1631 dseg->lkey = sq->mkey_be;
1641 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1642 mlx5e_free_xdpsq(sq);
1647 static void mlx5e_close_xdpsq(struct mlx5e_xdpsq *sq, struct mlx5e_rq *rq)
1649 struct mlx5e_channel *c = sq->channel;
1651 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1652 napi_synchronize(&c->napi);
1654 mlx5e_destroy_sq(c->mdev, sq->sqn);
1655 mlx5e_free_xdpsq_descs(sq, rq);
1656 mlx5e_free_xdpsq(sq);
1659 static int mlx5e_alloc_cq_common(struct mlx5_core_dev *mdev,
1660 struct mlx5e_cq_param *param,
1661 struct mlx5e_cq *cq)
1663 struct mlx5_core_cq *mcq = &cq->mcq;
1669 err = mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1673 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1679 mcq->set_ci_db = cq->wq_ctrl.db.db;
1680 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1681 *mcq->set_ci_db = 0;
1683 mcq->vector = param->eq_ix;
1684 mcq->comp = mlx5e_completion_event;
1685 mcq->event = mlx5e_cq_error_event;
1688 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1689 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1699 static int mlx5e_alloc_cq(struct mlx5e_channel *c,
1700 struct mlx5e_cq_param *param,
1701 struct mlx5e_cq *cq)
1703 struct mlx5_core_dev *mdev = c->priv->mdev;
1706 param->wq.buf_numa_node = cpu_to_node(c->cpu);
1707 param->wq.db_numa_node = cpu_to_node(c->cpu);
1708 param->eq_ix = c->ix;
1710 err = mlx5e_alloc_cq_common(mdev, param, cq);
1712 cq->napi = &c->napi;
1718 static void mlx5e_free_cq(struct mlx5e_cq *cq)
1720 mlx5_wq_destroy(&cq->wq_ctrl);
1723 static int mlx5e_create_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
1725 struct mlx5_core_dev *mdev = cq->mdev;
1726 struct mlx5_core_cq *mcq = &cq->mcq;
1731 unsigned int irqn_not_used;
1735 err = mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
1739 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1740 sizeof(u64) * cq->wq_ctrl.buf.npages;
1741 in = kvzalloc(inlen, GFP_KERNEL);
1745 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1747 memcpy(cqc, param->cqc, sizeof(param->cqc));
1749 mlx5_fill_page_frag_array(&cq->wq_ctrl.buf,
1750 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
1752 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
1753 MLX5_SET(cqc, cqc, c_eqn, eqn);
1754 MLX5_SET(cqc, cqc, uar_page, mdev->priv.uar->index);
1755 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
1756 MLX5_ADAPTER_PAGE_SHIFT);
1757 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1759 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
1771 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
1773 mlx5_core_destroy_cq(cq->mdev, &cq->mcq);
1776 static int mlx5e_open_cq(struct mlx5e_channel *c,
1777 struct net_dim_cq_moder moder,
1778 struct mlx5e_cq_param *param,
1779 struct mlx5e_cq *cq)
1781 struct mlx5_core_dev *mdev = c->mdev;
1784 err = mlx5e_alloc_cq(c, param, cq);
1788 err = mlx5e_create_cq(cq, param);
1792 if (MLX5_CAP_GEN(mdev, cq_moderation))
1793 mlx5_core_modify_cq_moderation(mdev, &cq->mcq, moder.usec, moder.pkts);
1802 static void mlx5e_close_cq(struct mlx5e_cq *cq)
1804 mlx5e_destroy_cq(cq);
1808 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1809 struct mlx5e_params *params,
1810 struct mlx5e_channel_param *cparam)
1815 for (tc = 0; tc < c->num_tc; tc++) {
1816 err = mlx5e_open_cq(c, params->tx_cq_moderation,
1817 &cparam->tx_cq, &c->sq[tc].cq);
1819 goto err_close_tx_cqs;
1825 for (tc--; tc >= 0; tc--)
1826 mlx5e_close_cq(&c->sq[tc].cq);
1831 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1835 for (tc = 0; tc < c->num_tc; tc++)
1836 mlx5e_close_cq(&c->sq[tc].cq);
1839 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1840 struct mlx5e_params *params,
1841 struct mlx5e_channel_param *cparam)
1843 struct mlx5e_priv *priv = c->priv;
1844 int err, tc, max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
1846 for (tc = 0; tc < params->num_tc; tc++) {
1847 int txq_ix = c->ix + tc * max_nch;
1849 err = mlx5e_open_txqsq(c, c->priv->tisn[tc], txq_ix,
1850 params, &cparam->sq, &c->sq[tc], tc);
1858 for (tc--; tc >= 0; tc--)
1859 mlx5e_close_txqsq(&c->sq[tc]);
1864 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1868 for (tc = 0; tc < c->num_tc; tc++)
1869 mlx5e_close_txqsq(&c->sq[tc]);
1872 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1873 struct mlx5e_txqsq *sq, u32 rate)
1875 struct mlx5e_priv *priv = netdev_priv(dev);
1876 struct mlx5_core_dev *mdev = priv->mdev;
1877 struct mlx5e_modify_sq_param msp = {0};
1878 struct mlx5_rate_limit rl = {0};
1882 if (rate == sq->rate_limit)
1886 if (sq->rate_limit) {
1887 rl.rate = sq->rate_limit;
1888 /* remove current rl index to free space to next ones */
1889 mlx5_rl_remove_rate(mdev, &rl);
1896 err = mlx5_rl_add_rate(mdev, &rl_index, &rl);
1898 netdev_err(dev, "Failed configuring rate %u: %d\n",
1904 msp.curr_state = MLX5_SQC_STATE_RDY;
1905 msp.next_state = MLX5_SQC_STATE_RDY;
1906 msp.rl_index = rl_index;
1907 msp.rl_update = true;
1908 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1910 netdev_err(dev, "Failed configuring rate %u: %d\n",
1912 /* remove the rate from the table */
1914 mlx5_rl_remove_rate(mdev, &rl);
1918 sq->rate_limit = rate;
1922 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1924 struct mlx5e_priv *priv = netdev_priv(dev);
1925 struct mlx5_core_dev *mdev = priv->mdev;
1926 struct mlx5e_txqsq *sq = priv->txq2sq[index];
1929 if (!mlx5_rl_is_supported(mdev)) {
1930 netdev_err(dev, "Rate limiting is not supported on this device\n");
1934 /* rate is given in Mb/sec, HW config is in Kb/sec */
1937 /* Check whether rate in valid range, 0 is always valid */
1938 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1939 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1943 mutex_lock(&priv->state_lock);
1944 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1945 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1947 priv->tx_rates[index] = rate;
1948 mutex_unlock(&priv->state_lock);
1953 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1954 struct mlx5e_params *params,
1955 struct mlx5e_channel_param *cparam,
1956 struct mlx5e_channel **cp)
1958 int cpu = cpumask_first(mlx5_comp_irq_get_affinity_mask(priv->mdev, ix));
1959 struct net_dim_cq_moder icocq_moder = {0, 0};
1960 struct net_device *netdev = priv->netdev;
1961 struct mlx5e_channel *c;
1966 err = mlx5_vector2eqn(priv->mdev, ix, &eqn, &irq);
1970 c = kvzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1975 c->mdev = priv->mdev;
1976 c->tstamp = &priv->tstamp;
1979 c->pdev = &priv->mdev->pdev->dev;
1980 c->netdev = priv->netdev;
1981 c->mkey_be = cpu_to_be32(priv->mdev->mlx5e_res.mkey.key);
1982 c->num_tc = params->num_tc;
1983 c->xdp = !!params->xdp_prog;
1984 c->stats = &priv->channel_stats[ix].ch;
1986 c->irq_desc = irq_to_desc(irq);
1988 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1990 err = mlx5e_open_cq(c, icocq_moder, &cparam->icosq_cq, &c->icosq.cq);
1994 err = mlx5e_open_tx_cqs(c, params, cparam);
1996 goto err_close_icosq_cq;
1998 err = mlx5e_open_cq(c, params->tx_cq_moderation, &cparam->tx_cq, &c->xdpsq.cq);
2000 goto err_close_tx_cqs;
2002 err = mlx5e_open_cq(c, params->rx_cq_moderation, &cparam->rx_cq, &c->rq.cq);
2004 goto err_close_xdp_tx_cqs;
2006 /* XDP SQ CQ params are same as normal TXQ sq CQ params */
2007 err = c->xdp ? mlx5e_open_cq(c, params->tx_cq_moderation,
2008 &cparam->tx_cq, &c->rq.xdpsq.cq) : 0;
2010 goto err_close_rx_cq;
2012 napi_enable(&c->napi);
2014 err = mlx5e_open_icosq(c, params, &cparam->icosq, &c->icosq);
2016 goto err_disable_napi;
2018 err = mlx5e_open_sqs(c, params, cparam);
2020 goto err_close_icosq;
2022 err = c->xdp ? mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, &c->rq.xdpsq, false) : 0;
2026 err = mlx5e_open_rq(c, params, &cparam->rq, &c->rq);
2028 goto err_close_xdp_sq;
2030 err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, &c->xdpsq, true);
2039 mlx5e_close_rq(&c->rq);
2043 mlx5e_close_xdpsq(&c->rq.xdpsq, &c->rq);
2049 mlx5e_close_icosq(&c->icosq);
2052 napi_disable(&c->napi);
2054 mlx5e_close_cq(&c->rq.xdpsq.cq);
2057 mlx5e_close_cq(&c->rq.cq);
2059 err_close_xdp_tx_cqs:
2060 mlx5e_close_cq(&c->xdpsq.cq);
2063 mlx5e_close_tx_cqs(c);
2066 mlx5e_close_cq(&c->icosq.cq);
2069 netif_napi_del(&c->napi);
2075 static void mlx5e_activate_channel(struct mlx5e_channel *c)
2079 for (tc = 0; tc < c->num_tc; tc++)
2080 mlx5e_activate_txqsq(&c->sq[tc]);
2081 mlx5e_activate_rq(&c->rq);
2082 netif_set_xps_queue(c->netdev, get_cpu_mask(c->cpu), c->ix);
2085 static void mlx5e_deactivate_channel(struct mlx5e_channel *c)
2089 mlx5e_deactivate_rq(&c->rq);
2090 for (tc = 0; tc < c->num_tc; tc++)
2091 mlx5e_deactivate_txqsq(&c->sq[tc]);
2094 static void mlx5e_close_channel(struct mlx5e_channel *c)
2096 mlx5e_close_xdpsq(&c->xdpsq, NULL);
2097 mlx5e_close_rq(&c->rq);
2099 mlx5e_close_xdpsq(&c->rq.xdpsq, &c->rq);
2101 mlx5e_close_icosq(&c->icosq);
2102 napi_disable(&c->napi);
2104 mlx5e_close_cq(&c->rq.xdpsq.cq);
2105 mlx5e_close_cq(&c->rq.cq);
2106 mlx5e_close_cq(&c->xdpsq.cq);
2107 mlx5e_close_tx_cqs(c);
2108 mlx5e_close_cq(&c->icosq.cq);
2109 netif_napi_del(&c->napi);
2114 #define DEFAULT_FRAG_SIZE (2048)
2116 static void mlx5e_build_rq_frags_info(struct mlx5_core_dev *mdev,
2117 struct mlx5e_params *params,
2118 struct mlx5e_rq_frags_info *info)
2120 u32 byte_count = MLX5E_SW2HW_MTU(params, params->sw_mtu);
2121 int frag_size_max = DEFAULT_FRAG_SIZE;
2125 #ifdef CONFIG_MLX5_EN_IPSEC
2126 if (MLX5_IPSEC_DEV(mdev))
2127 byte_count += MLX5E_METADATA_ETHER_LEN;
2130 if (mlx5e_rx_is_linear_skb(mdev, params)) {
2133 frag_stride = mlx5e_rx_get_linear_frag_sz(params);
2134 frag_stride = roundup_pow_of_two(frag_stride);
2136 info->arr[0].frag_size = byte_count;
2137 info->arr[0].frag_stride = frag_stride;
2138 info->num_frags = 1;
2139 info->wqe_bulk = PAGE_SIZE / frag_stride;
2143 if (byte_count > PAGE_SIZE +
2144 (MLX5E_MAX_RX_FRAGS - 1) * frag_size_max)
2145 frag_size_max = PAGE_SIZE;
2148 while (buf_size < byte_count) {
2149 int frag_size = byte_count - buf_size;
2151 if (i < MLX5E_MAX_RX_FRAGS - 1)
2152 frag_size = min(frag_size, frag_size_max);
2154 info->arr[i].frag_size = frag_size;
2155 info->arr[i].frag_stride = roundup_pow_of_two(frag_size);
2157 buf_size += frag_size;
2160 info->num_frags = i;
2161 /* number of different wqes sharing a page */
2162 info->wqe_bulk = 1 + (info->num_frags % 2);
2165 info->wqe_bulk = max_t(u8, info->wqe_bulk, 8);
2166 info->log_num_frags = order_base_2(info->num_frags);
2169 static inline u8 mlx5e_get_rqwq_log_stride(u8 wq_type, int ndsegs)
2171 int sz = sizeof(struct mlx5_wqe_data_seg) * ndsegs;
2174 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2175 sz += sizeof(struct mlx5e_rx_wqe_ll);
2177 default: /* MLX5_WQ_TYPE_CYCLIC */
2178 sz += sizeof(struct mlx5e_rx_wqe_cyc);
2181 return order_base_2(sz);
2184 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
2185 struct mlx5e_params *params,
2186 struct mlx5e_rq_param *param)
2188 struct mlx5_core_dev *mdev = priv->mdev;
2189 void *rqc = param->rqc;
2190 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2193 switch (params->rq_wq_type) {
2194 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2195 MLX5_SET(wq, wq, log_wqe_num_of_strides,
2196 mlx5e_mpwqe_get_log_num_strides(mdev, params) -
2197 MLX5_MPWQE_LOG_NUM_STRIDES_BASE);
2198 MLX5_SET(wq, wq, log_wqe_stride_size,
2199 mlx5e_mpwqe_get_log_stride_size(mdev, params) -
2200 MLX5_MPWQE_LOG_STRIDE_SZ_BASE);
2201 MLX5_SET(wq, wq, log_wq_sz, mlx5e_mpwqe_get_log_rq_size(params));
2203 default: /* MLX5_WQ_TYPE_CYCLIC */
2204 MLX5_SET(wq, wq, log_wq_sz, params->log_rq_mtu_frames);
2205 mlx5e_build_rq_frags_info(mdev, params, ¶m->frags_info);
2206 ndsegs = param->frags_info.num_frags;
2209 MLX5_SET(wq, wq, wq_type, params->rq_wq_type);
2210 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
2211 MLX5_SET(wq, wq, log_wq_stride,
2212 mlx5e_get_rqwq_log_stride(params->rq_wq_type, ndsegs));
2213 MLX5_SET(wq, wq, pd, mdev->mlx5e_res.pdn);
2214 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
2215 MLX5_SET(rqc, rqc, vsd, params->vlan_strip_disable);
2216 MLX5_SET(rqc, rqc, scatter_fcs, params->scatter_fcs_en);
2218 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
2221 static void mlx5e_build_drop_rq_param(struct mlx5e_priv *priv,
2222 struct mlx5e_rq_param *param)
2224 struct mlx5_core_dev *mdev = priv->mdev;
2225 void *rqc = param->rqc;
2226 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2228 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
2229 MLX5_SET(wq, wq, log_wq_stride,
2230 mlx5e_get_rqwq_log_stride(MLX5_WQ_TYPE_CYCLIC, 1));
2231 MLX5_SET(rqc, rqc, counter_set_id, priv->drop_rq_q_counter);
2233 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
2236 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
2237 struct mlx5e_sq_param *param)
2239 void *sqc = param->sqc;
2240 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2242 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
2243 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
2245 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
2248 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
2249 struct mlx5e_params *params,
2250 struct mlx5e_sq_param *param)
2252 void *sqc = param->sqc;
2253 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2255 mlx5e_build_sq_param_common(priv, param);
2256 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2257 MLX5_SET(sqc, sqc, allow_swp, !!MLX5_IPSEC_DEV(priv->mdev));
2260 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
2261 struct mlx5e_cq_param *param)
2263 void *cqc = param->cqc;
2265 MLX5_SET(cqc, cqc, uar_page, priv->mdev->priv.uar->index);
2266 if (MLX5_CAP_GEN(priv->mdev, cqe_128_always) && cache_line_size() >= 128)
2267 MLX5_SET(cqc, cqc, cqe_sz, CQE_STRIDE_128_PAD);
2270 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
2271 struct mlx5e_params *params,
2272 struct mlx5e_cq_param *param)
2274 struct mlx5_core_dev *mdev = priv->mdev;
2275 void *cqc = param->cqc;
2278 switch (params->rq_wq_type) {
2279 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2280 log_cq_size = mlx5e_mpwqe_get_log_rq_size(params) +
2281 mlx5e_mpwqe_get_log_num_strides(mdev, params);
2283 default: /* MLX5_WQ_TYPE_CYCLIC */
2284 log_cq_size = params->log_rq_mtu_frames;
2287 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
2288 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
2289 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
2290 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
2293 mlx5e_build_common_cq_param(priv, param);
2294 param->cq_period_mode = params->rx_cq_moderation.cq_period_mode;
2297 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
2298 struct mlx5e_params *params,
2299 struct mlx5e_cq_param *param)
2301 void *cqc = param->cqc;
2303 MLX5_SET(cqc, cqc, log_cq_size, params->log_sq_size);
2305 mlx5e_build_common_cq_param(priv, param);
2306 param->cq_period_mode = params->tx_cq_moderation.cq_period_mode;
2309 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
2311 struct mlx5e_cq_param *param)
2313 void *cqc = param->cqc;
2315 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
2317 mlx5e_build_common_cq_param(priv, param);
2319 param->cq_period_mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2322 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
2324 struct mlx5e_sq_param *param)
2326 void *sqc = param->sqc;
2327 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2329 mlx5e_build_sq_param_common(priv, param);
2331 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
2332 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
2335 static void mlx5e_build_xdpsq_param(struct mlx5e_priv *priv,
2336 struct mlx5e_params *params,
2337 struct mlx5e_sq_param *param)
2339 void *sqc = param->sqc;
2340 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2342 mlx5e_build_sq_param_common(priv, param);
2343 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2344 param->is_mpw = MLX5E_GET_PFLAG(params, MLX5E_PFLAG_XDP_TX_MPWQE);
2347 static void mlx5e_build_channel_param(struct mlx5e_priv *priv,
2348 struct mlx5e_params *params,
2349 struct mlx5e_channel_param *cparam)
2351 u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
2353 mlx5e_build_rq_param(priv, params, &cparam->rq);
2354 mlx5e_build_sq_param(priv, params, &cparam->sq);
2355 mlx5e_build_xdpsq_param(priv, params, &cparam->xdp_sq);
2356 mlx5e_build_icosq_param(priv, icosq_log_wq_sz, &cparam->icosq);
2357 mlx5e_build_rx_cq_param(priv, params, &cparam->rx_cq);
2358 mlx5e_build_tx_cq_param(priv, params, &cparam->tx_cq);
2359 mlx5e_build_ico_cq_param(priv, icosq_log_wq_sz, &cparam->icosq_cq);
2362 int mlx5e_open_channels(struct mlx5e_priv *priv,
2363 struct mlx5e_channels *chs)
2365 struct mlx5e_channel_param *cparam;
2369 chs->num = chs->params.num_channels;
2371 chs->c = kcalloc(chs->num, sizeof(struct mlx5e_channel *), GFP_KERNEL);
2372 cparam = kvzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
2373 if (!chs->c || !cparam)
2376 mlx5e_build_channel_param(priv, &chs->params, cparam);
2377 for (i = 0; i < chs->num; i++) {
2378 err = mlx5e_open_channel(priv, i, &chs->params, cparam, &chs->c[i]);
2380 goto err_close_channels;
2387 for (i--; i >= 0; i--)
2388 mlx5e_close_channel(chs->c[i]);
2397 static void mlx5e_activate_channels(struct mlx5e_channels *chs)
2401 for (i = 0; i < chs->num; i++)
2402 mlx5e_activate_channel(chs->c[i]);
2405 static int mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels *chs)
2410 for (i = 0; i < chs->num; i++)
2411 err |= mlx5e_wait_for_min_rx_wqes(&chs->c[i]->rq,
2414 return err ? -ETIMEDOUT : 0;
2417 static void mlx5e_deactivate_channels(struct mlx5e_channels *chs)
2421 for (i = 0; i < chs->num; i++)
2422 mlx5e_deactivate_channel(chs->c[i]);
2425 void mlx5e_close_channels(struct mlx5e_channels *chs)
2429 for (i = 0; i < chs->num; i++)
2430 mlx5e_close_channel(chs->c[i]);
2437 mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, struct mlx5e_rqt *rqt)
2439 struct mlx5_core_dev *mdev = priv->mdev;
2446 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
2447 in = kvzalloc(inlen, GFP_KERNEL);
2451 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
2453 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2454 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
2456 for (i = 0; i < sz; i++)
2457 MLX5_SET(rqtc, rqtc, rq_num[i], priv->drop_rq.rqn);
2459 err = mlx5_core_create_rqt(mdev, in, inlen, &rqt->rqtn);
2461 rqt->enabled = true;
2467 void mlx5e_destroy_rqt(struct mlx5e_priv *priv, struct mlx5e_rqt *rqt)
2469 rqt->enabled = false;
2470 mlx5_core_destroy_rqt(priv->mdev, rqt->rqtn);
2473 int mlx5e_create_indirect_rqt(struct mlx5e_priv *priv)
2475 struct mlx5e_rqt *rqt = &priv->indir_rqt;
2478 err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, rqt);
2480 mlx5_core_warn(priv->mdev, "create indirect rqts failed, %d\n", err);
2484 int mlx5e_create_direct_rqts(struct mlx5e_priv *priv)
2486 struct mlx5e_rqt *rqt;
2490 for (ix = 0; ix < mlx5e_get_netdev_max_channels(priv->netdev); ix++) {
2491 rqt = &priv->direct_tir[ix].rqt;
2492 err = mlx5e_create_rqt(priv, 1 /*size */, rqt);
2494 goto err_destroy_rqts;
2500 mlx5_core_warn(priv->mdev, "create direct rqts failed, %d\n", err);
2501 for (ix--; ix >= 0; ix--)
2502 mlx5e_destroy_rqt(priv, &priv->direct_tir[ix].rqt);
2507 void mlx5e_destroy_direct_rqts(struct mlx5e_priv *priv)
2511 for (i = 0; i < mlx5e_get_netdev_max_channels(priv->netdev); i++)
2512 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
2515 static int mlx5e_rx_hash_fn(int hfunc)
2517 return (hfunc == ETH_RSS_HASH_TOP) ?
2518 MLX5_RX_HASH_FN_TOEPLITZ :
2519 MLX5_RX_HASH_FN_INVERTED_XOR8;
2522 int mlx5e_bits_invert(unsigned long a, int size)
2527 for (i = 0; i < size; i++)
2528 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
2533 static void mlx5e_fill_rqt_rqns(struct mlx5e_priv *priv, int sz,
2534 struct mlx5e_redirect_rqt_param rrp, void *rqtc)
2538 for (i = 0; i < sz; i++) {
2544 if (rrp.rss.hfunc == ETH_RSS_HASH_XOR)
2545 ix = mlx5e_bits_invert(i, ilog2(sz));
2547 ix = priv->rss_params.indirection_rqt[ix];
2548 rqn = rrp.rss.channels->c[ix]->rq.rqn;
2552 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
2556 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz,
2557 struct mlx5e_redirect_rqt_param rrp)
2559 struct mlx5_core_dev *mdev = priv->mdev;
2565 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
2566 in = kvzalloc(inlen, GFP_KERNEL);
2570 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
2572 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2573 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
2574 mlx5e_fill_rqt_rqns(priv, sz, rrp, rqtc);
2575 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
2581 static u32 mlx5e_get_direct_rqn(struct mlx5e_priv *priv, int ix,
2582 struct mlx5e_redirect_rqt_param rrp)
2587 if (ix >= rrp.rss.channels->num)
2588 return priv->drop_rq.rqn;
2590 return rrp.rss.channels->c[ix]->rq.rqn;
2593 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv,
2594 struct mlx5e_redirect_rqt_param rrp)
2599 if (priv->indir_rqt.enabled) {
2601 rqtn = priv->indir_rqt.rqtn;
2602 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, rrp);
2605 for (ix = 0; ix < mlx5e_get_netdev_max_channels(priv->netdev); ix++) {
2606 struct mlx5e_redirect_rqt_param direct_rrp = {
2609 .rqn = mlx5e_get_direct_rqn(priv, ix, rrp)
2613 /* Direct RQ Tables */
2614 if (!priv->direct_tir[ix].rqt.enabled)
2617 rqtn = priv->direct_tir[ix].rqt.rqtn;
2618 mlx5e_redirect_rqt(priv, rqtn, 1, direct_rrp);
2622 static void mlx5e_redirect_rqts_to_channels(struct mlx5e_priv *priv,
2623 struct mlx5e_channels *chs)
2625 struct mlx5e_redirect_rqt_param rrp = {
2630 .hfunc = priv->rss_params.hfunc,
2635 mlx5e_redirect_rqts(priv, rrp);
2638 static void mlx5e_redirect_rqts_to_drop(struct mlx5e_priv *priv)
2640 struct mlx5e_redirect_rqt_param drop_rrp = {
2643 .rqn = priv->drop_rq.rqn,
2647 mlx5e_redirect_rqts(priv, drop_rrp);
2650 static const struct mlx5e_tirc_config tirc_default_config[MLX5E_NUM_INDIR_TIRS] = {
2651 [MLX5E_TT_IPV4_TCP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2652 .l4_prot_type = MLX5_L4_PROT_TYPE_TCP,
2653 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2655 [MLX5E_TT_IPV6_TCP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2656 .l4_prot_type = MLX5_L4_PROT_TYPE_TCP,
2657 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2659 [MLX5E_TT_IPV4_UDP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2660 .l4_prot_type = MLX5_L4_PROT_TYPE_UDP,
2661 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2663 [MLX5E_TT_IPV6_UDP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2664 .l4_prot_type = MLX5_L4_PROT_TYPE_UDP,
2665 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2667 [MLX5E_TT_IPV4_IPSEC_AH] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2669 .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2671 [MLX5E_TT_IPV6_IPSEC_AH] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2673 .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2675 [MLX5E_TT_IPV4_IPSEC_ESP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2677 .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2679 [MLX5E_TT_IPV6_IPSEC_ESP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2681 .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2683 [MLX5E_TT_IPV4] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2685 .rx_hash_fields = MLX5_HASH_IP,
2687 [MLX5E_TT_IPV6] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2689 .rx_hash_fields = MLX5_HASH_IP,
2693 struct mlx5e_tirc_config mlx5e_tirc_get_default_config(enum mlx5e_traffic_types tt)
2695 return tirc_default_config[tt];
2698 static void mlx5e_build_tir_ctx_lro(struct mlx5e_params *params, void *tirc)
2700 if (!params->lro_en)
2703 #define ROUGH_MAX_L2_L3_HDR_SZ 256
2705 MLX5_SET(tirc, tirc, lro_enable_mask,
2706 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2707 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
2708 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
2709 (params->lro_wqe_sz - ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
2710 MLX5_SET(tirc, tirc, lro_timeout_period_usecs, params->lro_timeout);
2713 void mlx5e_build_indir_tir_ctx_hash(struct mlx5e_rss_params *rss_params,
2714 const struct mlx5e_tirc_config *ttconfig,
2715 void *tirc, bool inner)
2717 void *hfso = inner ? MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner) :
2718 MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2720 MLX5_SET(tirc, tirc, rx_hash_fn, mlx5e_rx_hash_fn(rss_params->hfunc));
2721 if (rss_params->hfunc == ETH_RSS_HASH_TOP) {
2722 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
2723 rx_hash_toeplitz_key);
2724 size_t len = MLX5_FLD_SZ_BYTES(tirc,
2725 rx_hash_toeplitz_key);
2727 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
2728 memcpy(rss_key, rss_params->toeplitz_hash_key, len);
2730 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2731 ttconfig->l3_prot_type);
2732 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2733 ttconfig->l4_prot_type);
2734 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2735 ttconfig->rx_hash_fields);
2738 static void mlx5e_update_rx_hash_fields(struct mlx5e_tirc_config *ttconfig,
2739 enum mlx5e_traffic_types tt,
2742 *ttconfig = tirc_default_config[tt];
2743 ttconfig->rx_hash_fields = rx_hash_fields;
2746 void mlx5e_modify_tirs_hash(struct mlx5e_priv *priv, void *in, int inlen)
2748 void *tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2749 struct mlx5e_rss_params *rss = &priv->rss_params;
2750 struct mlx5_core_dev *mdev = priv->mdev;
2751 int ctxlen = MLX5_ST_SZ_BYTES(tirc);
2752 struct mlx5e_tirc_config ttconfig;
2755 MLX5_SET(modify_tir_in, in, bitmask.hash, 1);
2757 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2758 memset(tirc, 0, ctxlen);
2759 mlx5e_update_rx_hash_fields(&ttconfig, tt,
2760 rss->rx_hash_fields[tt]);
2761 mlx5e_build_indir_tir_ctx_hash(rss, &ttconfig, tirc, false);
2762 mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in, inlen);
2765 if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
2768 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2769 memset(tirc, 0, ctxlen);
2770 mlx5e_update_rx_hash_fields(&ttconfig, tt,
2771 rss->rx_hash_fields[tt]);
2772 mlx5e_build_indir_tir_ctx_hash(rss, &ttconfig, tirc, true);
2773 mlx5_core_modify_tir(mdev, priv->inner_indir_tir[tt].tirn, in,
2778 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
2780 struct mlx5_core_dev *mdev = priv->mdev;
2789 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
2790 in = kvzalloc(inlen, GFP_KERNEL);
2794 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
2795 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2797 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2799 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2800 err = mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in,
2806 for (ix = 0; ix < mlx5e_get_netdev_max_channels(priv->netdev); ix++) {
2807 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
2819 static void mlx5e_build_inner_indir_tir_ctx(struct mlx5e_priv *priv,
2820 enum mlx5e_traffic_types tt,
2823 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2825 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2827 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2828 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
2829 MLX5_SET(tirc, tirc, tunneled_offload_en, 0x1);
2831 mlx5e_build_indir_tir_ctx_hash(&priv->rss_params,
2832 &tirc_default_config[tt], tirc, true);
2835 static int mlx5e_set_mtu(struct mlx5_core_dev *mdev,
2836 struct mlx5e_params *params, u16 mtu)
2838 u16 hw_mtu = MLX5E_SW2HW_MTU(params, mtu);
2841 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
2845 /* Update vport context MTU */
2846 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
2850 static void mlx5e_query_mtu(struct mlx5_core_dev *mdev,
2851 struct mlx5e_params *params, u16 *mtu)
2856 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
2857 if (err || !hw_mtu) /* fallback to port oper mtu */
2858 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
2860 *mtu = MLX5E_HW2SW_MTU(params, hw_mtu);
2863 int mlx5e_set_dev_port_mtu(struct mlx5e_priv *priv)
2865 struct mlx5e_params *params = &priv->channels.params;
2866 struct net_device *netdev = priv->netdev;
2867 struct mlx5_core_dev *mdev = priv->mdev;
2871 err = mlx5e_set_mtu(mdev, params, params->sw_mtu);
2875 mlx5e_query_mtu(mdev, params, &mtu);
2876 if (mtu != params->sw_mtu)
2877 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
2878 __func__, mtu, params->sw_mtu);
2880 params->sw_mtu = mtu;
2884 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
2886 struct mlx5e_priv *priv = netdev_priv(netdev);
2887 int nch = priv->channels.params.num_channels;
2888 int ntc = priv->channels.params.num_tc;
2891 netdev_reset_tc(netdev);
2896 netdev_set_num_tc(netdev, ntc);
2898 /* Map netdev TCs to offset 0
2899 * We have our own UP to TXQ mapping for QoS
2901 for (tc = 0; tc < ntc; tc++)
2902 netdev_set_tc_queue(netdev, tc, nch, 0);
2905 static void mlx5e_build_tc2txq_maps(struct mlx5e_priv *priv)
2907 int max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
2910 for (i = 0; i < max_nch; i++)
2911 for (tc = 0; tc < priv->profile->max_tc; tc++)
2912 priv->channel_tc2txq[i][tc] = i + tc * max_nch;
2915 static void mlx5e_build_tx2sq_maps(struct mlx5e_priv *priv)
2917 struct mlx5e_channel *c;
2918 struct mlx5e_txqsq *sq;
2921 for (i = 0; i < priv->channels.num; i++) {
2922 c = priv->channels.c[i];
2923 for (tc = 0; tc < c->num_tc; tc++) {
2925 priv->txq2sq[sq->txq_ix] = sq;
2930 void mlx5e_activate_priv_channels(struct mlx5e_priv *priv)
2932 int num_txqs = priv->channels.num * priv->channels.params.num_tc;
2933 struct net_device *netdev = priv->netdev;
2935 mlx5e_netdev_set_tcs(netdev);
2936 netif_set_real_num_tx_queues(netdev, num_txqs);
2937 netif_set_real_num_rx_queues(netdev, priv->channels.num);
2939 mlx5e_build_tx2sq_maps(priv);
2940 mlx5e_activate_channels(&priv->channels);
2941 netif_tx_start_all_queues(priv->netdev);
2943 if (mlx5e_is_vport_rep(priv))
2944 mlx5e_add_sqs_fwd_rules(priv);
2946 mlx5e_wait_channels_min_rx_wqes(&priv->channels);
2947 mlx5e_redirect_rqts_to_channels(priv, &priv->channels);
2950 void mlx5e_deactivate_priv_channels(struct mlx5e_priv *priv)
2952 mlx5e_redirect_rqts_to_drop(priv);
2954 if (mlx5e_is_vport_rep(priv))
2955 mlx5e_remove_sqs_fwd_rules(priv);
2957 /* FIXME: This is a W/A only for tx timeout watch dog false alarm when
2958 * polling for inactive tx queues.
2960 netif_tx_stop_all_queues(priv->netdev);
2961 netif_tx_disable(priv->netdev);
2962 mlx5e_deactivate_channels(&priv->channels);
2965 void mlx5e_switch_priv_channels(struct mlx5e_priv *priv,
2966 struct mlx5e_channels *new_chs,
2967 mlx5e_fp_hw_modify hw_modify)
2969 struct net_device *netdev = priv->netdev;
2972 new_num_txqs = new_chs->num * new_chs->params.num_tc;
2974 carrier_ok = netif_carrier_ok(netdev);
2975 netif_carrier_off(netdev);
2977 if (new_num_txqs < netdev->real_num_tx_queues)
2978 netif_set_real_num_tx_queues(netdev, new_num_txqs);
2980 mlx5e_deactivate_priv_channels(priv);
2981 mlx5e_close_channels(&priv->channels);
2983 priv->channels = *new_chs;
2985 /* New channels are ready to roll, modify HW settings if needed */
2989 mlx5e_refresh_tirs(priv, false);
2990 mlx5e_activate_priv_channels(priv);
2992 /* return carrier back if needed */
2994 netif_carrier_on(netdev);
2997 void mlx5e_timestamp_init(struct mlx5e_priv *priv)
2999 priv->tstamp.tx_type = HWTSTAMP_TX_OFF;
3000 priv->tstamp.rx_filter = HWTSTAMP_FILTER_NONE;
3003 int mlx5e_open_locked(struct net_device *netdev)
3005 struct mlx5e_priv *priv = netdev_priv(netdev);
3008 set_bit(MLX5E_STATE_OPENED, &priv->state);
3010 err = mlx5e_open_channels(priv, &priv->channels);
3012 goto err_clear_state_opened_flag;
3014 mlx5e_refresh_tirs(priv, false);
3015 mlx5e_activate_priv_channels(priv);
3016 if (priv->profile->update_carrier)
3017 priv->profile->update_carrier(priv);
3019 mlx5e_queue_update_stats(priv);
3022 err_clear_state_opened_flag:
3023 clear_bit(MLX5E_STATE_OPENED, &priv->state);
3027 int mlx5e_open(struct net_device *netdev)
3029 struct mlx5e_priv *priv = netdev_priv(netdev);
3032 mutex_lock(&priv->state_lock);
3033 err = mlx5e_open_locked(netdev);
3035 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_UP);
3036 mutex_unlock(&priv->state_lock);
3038 if (mlx5_vxlan_allowed(priv->mdev->vxlan))
3039 udp_tunnel_get_rx_info(netdev);
3044 int mlx5e_close_locked(struct net_device *netdev)
3046 struct mlx5e_priv *priv = netdev_priv(netdev);
3048 /* May already be CLOSED in case a previous configuration operation
3049 * (e.g RX/TX queue size change) that involves close&open failed.
3051 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3054 clear_bit(MLX5E_STATE_OPENED, &priv->state);
3056 netif_carrier_off(priv->netdev);
3057 mlx5e_deactivate_priv_channels(priv);
3058 mlx5e_close_channels(&priv->channels);
3063 int mlx5e_close(struct net_device *netdev)
3065 struct mlx5e_priv *priv = netdev_priv(netdev);
3068 if (!netif_device_present(netdev))
3071 mutex_lock(&priv->state_lock);
3072 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_DOWN);
3073 err = mlx5e_close_locked(netdev);
3074 mutex_unlock(&priv->state_lock);
3079 static int mlx5e_alloc_drop_rq(struct mlx5_core_dev *mdev,
3080 struct mlx5e_rq *rq,
3081 struct mlx5e_rq_param *param)
3083 void *rqc = param->rqc;
3084 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
3087 param->wq.db_numa_node = param->wq.buf_numa_node;
3089 err = mlx5_wq_cyc_create(mdev, ¶m->wq, rqc_wq, &rq->wqe.wq,
3094 /* Mark as unused given "Drop-RQ" packets never reach XDP */
3095 xdp_rxq_info_unused(&rq->xdp_rxq);
3102 static int mlx5e_alloc_drop_cq(struct mlx5_core_dev *mdev,
3103 struct mlx5e_cq *cq,
3104 struct mlx5e_cq_param *param)
3106 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
3107 param->wq.db_numa_node = dev_to_node(&mdev->pdev->dev);
3109 return mlx5e_alloc_cq_common(mdev, param, cq);
3112 int mlx5e_open_drop_rq(struct mlx5e_priv *priv,
3113 struct mlx5e_rq *drop_rq)
3115 struct mlx5_core_dev *mdev = priv->mdev;
3116 struct mlx5e_cq_param cq_param = {};
3117 struct mlx5e_rq_param rq_param = {};
3118 struct mlx5e_cq *cq = &drop_rq->cq;
3121 mlx5e_build_drop_rq_param(priv, &rq_param);
3123 err = mlx5e_alloc_drop_cq(mdev, cq, &cq_param);
3127 err = mlx5e_create_cq(cq, &cq_param);
3131 err = mlx5e_alloc_drop_rq(mdev, drop_rq, &rq_param);
3133 goto err_destroy_cq;
3135 err = mlx5e_create_rq(drop_rq, &rq_param);
3139 err = mlx5e_modify_rq_state(drop_rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
3141 mlx5_core_warn(priv->mdev, "modify_rq_state failed, rx_if_down_packets won't be counted %d\n", err);
3146 mlx5e_free_rq(drop_rq);
3149 mlx5e_destroy_cq(cq);
3157 void mlx5e_close_drop_rq(struct mlx5e_rq *drop_rq)
3159 mlx5e_destroy_rq(drop_rq);
3160 mlx5e_free_rq(drop_rq);
3161 mlx5e_destroy_cq(&drop_rq->cq);
3162 mlx5e_free_cq(&drop_rq->cq);
3165 int mlx5e_create_tis(struct mlx5_core_dev *mdev, int tc,
3166 u32 underlay_qpn, u32 *tisn)
3168 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
3169 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
3171 MLX5_SET(tisc, tisc, prio, tc << 1);
3172 MLX5_SET(tisc, tisc, underlay_qpn, underlay_qpn);
3173 MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.td.tdn);
3175 if (mlx5_lag_is_lacp_owner(mdev))
3176 MLX5_SET(tisc, tisc, strict_lag_tx_port_affinity, 1);
3178 return mlx5_core_create_tis(mdev, in, sizeof(in), tisn);
3181 void mlx5e_destroy_tis(struct mlx5_core_dev *mdev, u32 tisn)
3183 mlx5_core_destroy_tis(mdev, tisn);
3186 int mlx5e_create_tises(struct mlx5e_priv *priv)
3191 for (tc = 0; tc < priv->profile->max_tc; tc++) {
3192 err = mlx5e_create_tis(priv->mdev, tc, 0, &priv->tisn[tc]);
3194 goto err_close_tises;
3200 for (tc--; tc >= 0; tc--)
3201 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
3206 static void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
3210 for (tc = 0; tc < priv->profile->max_tc; tc++)
3211 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
3214 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv,
3215 enum mlx5e_traffic_types tt,
3218 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
3220 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
3222 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
3223 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
3225 mlx5e_build_indir_tir_ctx_hash(&priv->rss_params,
3226 &tirc_default_config[tt], tirc, false);
3229 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 rqtn, u32 *tirc)
3231 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
3233 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
3235 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
3236 MLX5_SET(tirc, tirc, indirect_table, rqtn);
3237 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
3240 int mlx5e_create_indirect_tirs(struct mlx5e_priv *priv, bool inner_ttc)
3242 struct mlx5e_tir *tir;
3250 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3251 in = kvzalloc(inlen, GFP_KERNEL);
3255 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
3256 memset(in, 0, inlen);
3257 tir = &priv->indir_tir[tt];
3258 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3259 mlx5e_build_indir_tir_ctx(priv, tt, tirc);
3260 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3262 mlx5_core_warn(priv->mdev, "create indirect tirs failed, %d\n", err);
3263 goto err_destroy_inner_tirs;
3267 if (!inner_ttc || !mlx5e_tunnel_inner_ft_supported(priv->mdev))
3270 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
3271 memset(in, 0, inlen);
3272 tir = &priv->inner_indir_tir[i];
3273 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3274 mlx5e_build_inner_indir_tir_ctx(priv, i, tirc);
3275 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3277 mlx5_core_warn(priv->mdev, "create inner indirect tirs failed, %d\n", err);
3278 goto err_destroy_inner_tirs;
3287 err_destroy_inner_tirs:
3288 for (i--; i >= 0; i--)
3289 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3291 for (tt--; tt >= 0; tt--)
3292 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[tt]);
3299 int mlx5e_create_direct_tirs(struct mlx5e_priv *priv)
3301 int nch = mlx5e_get_netdev_max_channels(priv->netdev);
3302 struct mlx5e_tir *tir;
3309 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3310 in = kvzalloc(inlen, GFP_KERNEL);
3314 for (ix = 0; ix < nch; ix++) {
3315 memset(in, 0, inlen);
3316 tir = &priv->direct_tir[ix];
3317 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3318 mlx5e_build_direct_tir_ctx(priv, priv->direct_tir[ix].rqt.rqtn, tirc);
3319 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3321 goto err_destroy_ch_tirs;
3328 err_destroy_ch_tirs:
3329 mlx5_core_warn(priv->mdev, "create direct tirs failed, %d\n", err);
3330 for (ix--; ix >= 0; ix--)
3331 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[ix]);
3338 void mlx5e_destroy_indirect_tirs(struct mlx5e_priv *priv, bool inner_ttc)
3342 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3343 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[i]);
3345 if (!inner_ttc || !mlx5e_tunnel_inner_ft_supported(priv->mdev))
3348 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3349 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3352 void mlx5e_destroy_direct_tirs(struct mlx5e_priv *priv)
3354 int nch = mlx5e_get_netdev_max_channels(priv->netdev);
3357 for (i = 0; i < nch; i++)
3358 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[i]);
3361 static int mlx5e_modify_channels_scatter_fcs(struct mlx5e_channels *chs, bool enable)
3366 for (i = 0; i < chs->num; i++) {
3367 err = mlx5e_modify_rq_scatter_fcs(&chs->c[i]->rq, enable);
3375 static int mlx5e_modify_channels_vsd(struct mlx5e_channels *chs, bool vsd)
3380 for (i = 0; i < chs->num; i++) {
3381 err = mlx5e_modify_rq_vsd(&chs->c[i]->rq, vsd);
3389 static int mlx5e_setup_tc_mqprio(struct net_device *netdev,
3390 struct tc_mqprio_qopt *mqprio)
3392 struct mlx5e_priv *priv = netdev_priv(netdev);
3393 struct mlx5e_channels new_channels = {};
3394 u8 tc = mqprio->num_tc;
3397 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
3399 if (tc && tc != MLX5E_MAX_NUM_TC)
3402 mutex_lock(&priv->state_lock);
3404 new_channels.params = priv->channels.params;
3405 new_channels.params.num_tc = tc ? tc : 1;
3407 if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
3408 priv->channels.params = new_channels.params;
3412 err = mlx5e_open_channels(priv, &new_channels);
3416 priv->max_opened_tc = max_t(u8, priv->max_opened_tc,
3417 new_channels.params.num_tc);
3418 mlx5e_switch_priv_channels(priv, &new_channels, NULL);
3420 mutex_unlock(&priv->state_lock);
3424 #ifdef CONFIG_MLX5_ESWITCH
3425 static int mlx5e_setup_tc_cls_flower(struct mlx5e_priv *priv,
3426 struct tc_cls_flower_offload *cls_flower,
3429 switch (cls_flower->command) {
3430 case TC_CLSFLOWER_REPLACE:
3431 return mlx5e_configure_flower(priv->netdev, priv, cls_flower,
3433 case TC_CLSFLOWER_DESTROY:
3434 return mlx5e_delete_flower(priv->netdev, priv, cls_flower,
3436 case TC_CLSFLOWER_STATS:
3437 return mlx5e_stats_flower(priv->netdev, priv, cls_flower,
3444 static int mlx5e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
3447 struct mlx5e_priv *priv = cb_priv;
3450 case TC_SETUP_CLSFLOWER:
3451 return mlx5e_setup_tc_cls_flower(priv, type_data, MLX5E_TC_INGRESS |
3452 MLX5E_TC_NIC_OFFLOAD);
3458 static int mlx5e_setup_tc_block(struct net_device *dev,
3459 struct tc_block_offload *f)
3461 struct mlx5e_priv *priv = netdev_priv(dev);
3463 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
3466 switch (f->command) {
3468 return tcf_block_cb_register(f->block, mlx5e_setup_tc_block_cb,
3469 priv, priv, f->extack);
3470 case TC_BLOCK_UNBIND:
3471 tcf_block_cb_unregister(f->block, mlx5e_setup_tc_block_cb,
3480 static int mlx5e_setup_tc(struct net_device *dev, enum tc_setup_type type,
3484 #ifdef CONFIG_MLX5_ESWITCH
3485 case TC_SETUP_BLOCK:
3486 return mlx5e_setup_tc_block(dev, type_data);
3488 case TC_SETUP_QDISC_MQPRIO:
3489 return mlx5e_setup_tc_mqprio(dev, type_data);
3496 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
3498 struct mlx5e_priv *priv = netdev_priv(dev);
3499 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
3500 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
3501 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
3503 if (!mlx5e_monitor_counter_supported(priv)) {
3504 /* update HW stats in background for next time */
3505 mlx5e_queue_update_stats(priv);
3508 if (mlx5e_is_uplink_rep(priv)) {
3509 stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
3510 stats->rx_bytes = PPORT_802_3_GET(pstats, a_octets_received_ok);
3511 stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
3512 stats->tx_bytes = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
3514 mlx5e_grp_sw_update_stats(priv);
3515 stats->rx_packets = sstats->rx_packets;
3516 stats->rx_bytes = sstats->rx_bytes;
3517 stats->tx_packets = sstats->tx_packets;
3518 stats->tx_bytes = sstats->tx_bytes;
3519 stats->tx_dropped = sstats->tx_queue_dropped;
3522 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
3524 stats->rx_length_errors =
3525 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
3526 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
3527 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
3528 stats->rx_crc_errors =
3529 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
3530 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
3531 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
3532 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
3533 stats->rx_frame_errors;
3534 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
3536 /* vport multicast also counts packets that are dropped due to steering
3537 * or rx out of buffer
3540 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
3543 static void mlx5e_set_rx_mode(struct net_device *dev)
3545 struct mlx5e_priv *priv = netdev_priv(dev);
3547 queue_work(priv->wq, &priv->set_rx_mode_work);
3550 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
3552 struct mlx5e_priv *priv = netdev_priv(netdev);
3553 struct sockaddr *saddr = addr;
3555 if (!is_valid_ether_addr(saddr->sa_data))
3556 return -EADDRNOTAVAIL;
3558 netif_addr_lock_bh(netdev);
3559 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
3560 netif_addr_unlock_bh(netdev);
3562 queue_work(priv->wq, &priv->set_rx_mode_work);
3567 #define MLX5E_SET_FEATURE(features, feature, enable) \
3570 *features |= feature; \
3572 *features &= ~feature; \
3575 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
3577 static int set_feature_lro(struct net_device *netdev, bool enable)
3579 struct mlx5e_priv *priv = netdev_priv(netdev);
3580 struct mlx5_core_dev *mdev = priv->mdev;
3581 struct mlx5e_channels new_channels = {};
3582 struct mlx5e_params *old_params;
3586 mutex_lock(&priv->state_lock);
3588 old_params = &priv->channels.params;
3589 if (enable && !MLX5E_GET_PFLAG(old_params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
3590 netdev_warn(netdev, "can't set LRO with legacy RQ\n");
3595 reset = test_bit(MLX5E_STATE_OPENED, &priv->state);
3597 new_channels.params = *old_params;
3598 new_channels.params.lro_en = enable;
3600 if (old_params->rq_wq_type != MLX5_WQ_TYPE_CYCLIC) {
3601 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, old_params) ==
3602 mlx5e_rx_mpwqe_is_linear_skb(mdev, &new_channels.params))
3607 *old_params = new_channels.params;
3608 err = mlx5e_modify_tirs_lro(priv);
3612 err = mlx5e_open_channels(priv, &new_channels);
3616 mlx5e_switch_priv_channels(priv, &new_channels, mlx5e_modify_tirs_lro);
3618 mutex_unlock(&priv->state_lock);
3622 static int set_feature_cvlan_filter(struct net_device *netdev, bool enable)
3624 struct mlx5e_priv *priv = netdev_priv(netdev);
3627 mlx5e_enable_cvlan_filter(priv);
3629 mlx5e_disable_cvlan_filter(priv);
3634 #ifdef CONFIG_MLX5_ESWITCH
3635 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
3637 struct mlx5e_priv *priv = netdev_priv(netdev);
3639 if (!enable && mlx5e_tc_num_filters(priv, MLX5E_TC_NIC_OFFLOAD)) {
3641 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
3649 static int set_feature_rx_all(struct net_device *netdev, bool enable)
3651 struct mlx5e_priv *priv = netdev_priv(netdev);
3652 struct mlx5_core_dev *mdev = priv->mdev;
3654 return mlx5_set_port_fcs(mdev, !enable);
3657 static int set_feature_rx_fcs(struct net_device *netdev, bool enable)
3659 struct mlx5e_priv *priv = netdev_priv(netdev);
3662 mutex_lock(&priv->state_lock);
3664 priv->channels.params.scatter_fcs_en = enable;
3665 err = mlx5e_modify_channels_scatter_fcs(&priv->channels, enable);
3667 priv->channels.params.scatter_fcs_en = !enable;
3669 mutex_unlock(&priv->state_lock);
3674 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
3676 struct mlx5e_priv *priv = netdev_priv(netdev);
3679 mutex_lock(&priv->state_lock);
3681 priv->channels.params.vlan_strip_disable = !enable;
3682 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3685 err = mlx5e_modify_channels_vsd(&priv->channels, !enable);
3687 priv->channels.params.vlan_strip_disable = enable;
3690 mutex_unlock(&priv->state_lock);
3695 #ifdef CONFIG_MLX5_EN_ARFS
3696 static int set_feature_arfs(struct net_device *netdev, bool enable)
3698 struct mlx5e_priv *priv = netdev_priv(netdev);
3702 err = mlx5e_arfs_enable(priv);
3704 err = mlx5e_arfs_disable(priv);
3710 static int mlx5e_handle_feature(struct net_device *netdev,
3711 netdev_features_t *features,
3712 netdev_features_t wanted_features,
3713 netdev_features_t feature,
3714 mlx5e_feature_handler feature_handler)
3716 netdev_features_t changes = wanted_features ^ netdev->features;
3717 bool enable = !!(wanted_features & feature);
3720 if (!(changes & feature))
3723 err = feature_handler(netdev, enable);
3725 netdev_err(netdev, "%s feature %pNF failed, err %d\n",
3726 enable ? "Enable" : "Disable", &feature, err);
3730 MLX5E_SET_FEATURE(features, feature, enable);
3734 static int mlx5e_set_features(struct net_device *netdev,
3735 netdev_features_t features)
3737 netdev_features_t oper_features = netdev->features;
3740 #define MLX5E_HANDLE_FEATURE(feature, handler) \
3741 mlx5e_handle_feature(netdev, &oper_features, features, feature, handler)
3743 err |= MLX5E_HANDLE_FEATURE(NETIF_F_LRO, set_feature_lro);
3744 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_FILTER,
3745 set_feature_cvlan_filter);
3746 #ifdef CONFIG_MLX5_ESWITCH
3747 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_TC, set_feature_tc_num_filters);
3749 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXALL, set_feature_rx_all);
3750 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXFCS, set_feature_rx_fcs);
3751 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_RX, set_feature_rx_vlan);
3752 #ifdef CONFIG_MLX5_EN_ARFS
3753 err |= MLX5E_HANDLE_FEATURE(NETIF_F_NTUPLE, set_feature_arfs);
3757 netdev->features = oper_features;
3764 static netdev_features_t mlx5e_fix_features(struct net_device *netdev,
3765 netdev_features_t features)
3767 struct mlx5e_priv *priv = netdev_priv(netdev);
3768 struct mlx5e_params *params;
3770 mutex_lock(&priv->state_lock);
3771 params = &priv->channels.params;
3772 if (!bitmap_empty(priv->fs.vlan.active_svlans, VLAN_N_VID)) {
3773 /* HW strips the outer C-tag header, this is a problem
3774 * for S-tag traffic.
3776 features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3777 if (!params->vlan_strip_disable)
3778 netdev_warn(netdev, "Dropping C-tag vlan stripping offload due to S-tag vlan\n");
3780 if (!MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
3781 features &= ~NETIF_F_LRO;
3783 netdev_warn(netdev, "Disabling LRO, not supported in legacy RQ\n");
3786 mutex_unlock(&priv->state_lock);
3791 int mlx5e_change_mtu(struct net_device *netdev, int new_mtu,
3792 change_hw_mtu_cb set_mtu_cb)
3794 struct mlx5e_priv *priv = netdev_priv(netdev);
3795 struct mlx5e_channels new_channels = {};
3796 struct mlx5e_params *params;
3800 mutex_lock(&priv->state_lock);
3802 params = &priv->channels.params;
3804 reset = !params->lro_en;
3805 reset = reset && test_bit(MLX5E_STATE_OPENED, &priv->state);
3807 new_channels.params = *params;
3808 new_channels.params.sw_mtu = new_mtu;
3810 if (params->xdp_prog &&
3811 !mlx5e_rx_is_linear_skb(priv->mdev, &new_channels.params)) {
3812 netdev_err(netdev, "MTU(%d) > %d is not allowed while XDP enabled\n",
3813 new_mtu, MLX5E_XDP_MAX_MTU);
3818 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
3819 bool is_linear = mlx5e_rx_mpwqe_is_linear_skb(priv->mdev, &new_channels.params);
3820 u8 ppw_old = mlx5e_mpwqe_log_pkts_per_wqe(params);
3821 u8 ppw_new = mlx5e_mpwqe_log_pkts_per_wqe(&new_channels.params);
3823 reset = reset && (is_linear || (ppw_old != ppw_new));
3827 params->sw_mtu = new_mtu;
3830 netdev->mtu = params->sw_mtu;
3834 err = mlx5e_open_channels(priv, &new_channels);
3838 mlx5e_switch_priv_channels(priv, &new_channels, set_mtu_cb);
3839 netdev->mtu = new_channels.params.sw_mtu;
3842 mutex_unlock(&priv->state_lock);
3846 static int mlx5e_change_nic_mtu(struct net_device *netdev, int new_mtu)
3848 return mlx5e_change_mtu(netdev, new_mtu, mlx5e_set_dev_port_mtu);
3851 int mlx5e_hwstamp_set(struct mlx5e_priv *priv, struct ifreq *ifr)
3853 struct hwtstamp_config config;
3856 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz) ||
3857 (mlx5_clock_get_ptp_index(priv->mdev) == -1))
3860 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
3863 /* TX HW timestamp */
3864 switch (config.tx_type) {
3865 case HWTSTAMP_TX_OFF:
3866 case HWTSTAMP_TX_ON:
3872 mutex_lock(&priv->state_lock);
3873 /* RX HW timestamp */
3874 switch (config.rx_filter) {
3875 case HWTSTAMP_FILTER_NONE:
3876 /* Reset CQE compression to Admin default */
3877 mlx5e_modify_rx_cqe_compression_locked(priv, priv->channels.params.rx_cqe_compress_def);
3879 case HWTSTAMP_FILTER_ALL:
3880 case HWTSTAMP_FILTER_SOME:
3881 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3882 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3883 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3884 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3885 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3886 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3887 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3888 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3889 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3890 case HWTSTAMP_FILTER_PTP_V2_EVENT:
3891 case HWTSTAMP_FILTER_PTP_V2_SYNC:
3892 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3893 case HWTSTAMP_FILTER_NTP_ALL:
3894 /* Disable CQE compression */
3895 netdev_warn(priv->netdev, "Disabling cqe compression");
3896 err = mlx5e_modify_rx_cqe_compression_locked(priv, false);
3898 netdev_err(priv->netdev, "Failed disabling cqe compression err=%d\n", err);
3899 mutex_unlock(&priv->state_lock);
3902 config.rx_filter = HWTSTAMP_FILTER_ALL;
3905 mutex_unlock(&priv->state_lock);
3909 memcpy(&priv->tstamp, &config, sizeof(config));
3910 mutex_unlock(&priv->state_lock);
3912 return copy_to_user(ifr->ifr_data, &config,
3913 sizeof(config)) ? -EFAULT : 0;
3916 int mlx5e_hwstamp_get(struct mlx5e_priv *priv, struct ifreq *ifr)
3918 struct hwtstamp_config *cfg = &priv->tstamp;
3920 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
3923 return copy_to_user(ifr->ifr_data, cfg, sizeof(*cfg)) ? -EFAULT : 0;
3926 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3928 struct mlx5e_priv *priv = netdev_priv(dev);
3932 return mlx5e_hwstamp_set(priv, ifr);
3934 return mlx5e_hwstamp_get(priv, ifr);
3940 #ifdef CONFIG_MLX5_ESWITCH
3941 int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
3943 struct mlx5e_priv *priv = netdev_priv(dev);
3944 struct mlx5_core_dev *mdev = priv->mdev;
3946 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
3949 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
3952 struct mlx5e_priv *priv = netdev_priv(dev);
3953 struct mlx5_core_dev *mdev = priv->mdev;
3955 if (vlan_proto != htons(ETH_P_8021Q))
3956 return -EPROTONOSUPPORT;
3958 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
3962 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
3964 struct mlx5e_priv *priv = netdev_priv(dev);
3965 struct mlx5_core_dev *mdev = priv->mdev;
3967 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
3970 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
3972 struct mlx5e_priv *priv = netdev_priv(dev);
3973 struct mlx5_core_dev *mdev = priv->mdev;
3975 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
3978 int mlx5e_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
3981 struct mlx5e_priv *priv = netdev_priv(dev);
3982 struct mlx5_core_dev *mdev = priv->mdev;
3984 return mlx5_eswitch_set_vport_rate(mdev->priv.eswitch, vf + 1,
3985 max_tx_rate, min_tx_rate);
3988 static int mlx5_vport_link2ifla(u8 esw_link)
3991 case MLX5_VPORT_ADMIN_STATE_DOWN:
3992 return IFLA_VF_LINK_STATE_DISABLE;
3993 case MLX5_VPORT_ADMIN_STATE_UP:
3994 return IFLA_VF_LINK_STATE_ENABLE;
3996 return IFLA_VF_LINK_STATE_AUTO;
3999 static int mlx5_ifla_link2vport(u8 ifla_link)
4001 switch (ifla_link) {
4002 case IFLA_VF_LINK_STATE_DISABLE:
4003 return MLX5_VPORT_ADMIN_STATE_DOWN;
4004 case IFLA_VF_LINK_STATE_ENABLE:
4005 return MLX5_VPORT_ADMIN_STATE_UP;
4007 return MLX5_VPORT_ADMIN_STATE_AUTO;
4010 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
4013 struct mlx5e_priv *priv = netdev_priv(dev);
4014 struct mlx5_core_dev *mdev = priv->mdev;
4016 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
4017 mlx5_ifla_link2vport(link_state));
4020 int mlx5e_get_vf_config(struct net_device *dev,
4021 int vf, struct ifla_vf_info *ivi)
4023 struct mlx5e_priv *priv = netdev_priv(dev);
4024 struct mlx5_core_dev *mdev = priv->mdev;
4027 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
4030 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
4034 int mlx5e_get_vf_stats(struct net_device *dev,
4035 int vf, struct ifla_vf_stats *vf_stats)
4037 struct mlx5e_priv *priv = netdev_priv(dev);
4038 struct mlx5_core_dev *mdev = priv->mdev;
4040 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
4045 struct mlx5e_vxlan_work {
4046 struct work_struct work;
4047 struct mlx5e_priv *priv;
4051 static void mlx5e_vxlan_add_work(struct work_struct *work)
4053 struct mlx5e_vxlan_work *vxlan_work =
4054 container_of(work, struct mlx5e_vxlan_work, work);
4055 struct mlx5e_priv *priv = vxlan_work->priv;
4056 u16 port = vxlan_work->port;
4058 mutex_lock(&priv->state_lock);
4059 mlx5_vxlan_add_port(priv->mdev->vxlan, port);
4060 mutex_unlock(&priv->state_lock);
4065 static void mlx5e_vxlan_del_work(struct work_struct *work)
4067 struct mlx5e_vxlan_work *vxlan_work =
4068 container_of(work, struct mlx5e_vxlan_work, work);
4069 struct mlx5e_priv *priv = vxlan_work->priv;
4070 u16 port = vxlan_work->port;
4072 mutex_lock(&priv->state_lock);
4073 mlx5_vxlan_del_port(priv->mdev->vxlan, port);
4074 mutex_unlock(&priv->state_lock);
4078 static void mlx5e_vxlan_queue_work(struct mlx5e_priv *priv, u16 port, int add)
4080 struct mlx5e_vxlan_work *vxlan_work;
4082 vxlan_work = kmalloc(sizeof(*vxlan_work), GFP_ATOMIC);
4087 INIT_WORK(&vxlan_work->work, mlx5e_vxlan_add_work);
4089 INIT_WORK(&vxlan_work->work, mlx5e_vxlan_del_work);
4091 vxlan_work->priv = priv;
4092 vxlan_work->port = port;
4093 queue_work(priv->wq, &vxlan_work->work);
4096 void mlx5e_add_vxlan_port(struct net_device *netdev, struct udp_tunnel_info *ti)
4098 struct mlx5e_priv *priv = netdev_priv(netdev);
4100 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
4103 if (!mlx5_vxlan_allowed(priv->mdev->vxlan))
4106 mlx5e_vxlan_queue_work(priv, be16_to_cpu(ti->port), 1);
4109 void mlx5e_del_vxlan_port(struct net_device *netdev, struct udp_tunnel_info *ti)
4111 struct mlx5e_priv *priv = netdev_priv(netdev);
4113 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
4116 if (!mlx5_vxlan_allowed(priv->mdev->vxlan))
4119 mlx5e_vxlan_queue_work(priv, be16_to_cpu(ti->port), 0);
4122 static netdev_features_t mlx5e_tunnel_features_check(struct mlx5e_priv *priv,
4123 struct sk_buff *skb,
4124 netdev_features_t features)
4126 unsigned int offset = 0;
4127 struct udphdr *udph;
4131 switch (vlan_get_protocol(skb)) {
4132 case htons(ETH_P_IP):
4133 proto = ip_hdr(skb)->protocol;
4135 case htons(ETH_P_IPV6):
4136 proto = ipv6_find_hdr(skb, &offset, -1, NULL, NULL);
4146 udph = udp_hdr(skb);
4147 port = be16_to_cpu(udph->dest);
4149 /* Verify if UDP port is being offloaded by HW */
4150 if (mlx5_vxlan_lookup_port(priv->mdev->vxlan, port))
4155 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
4156 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4159 netdev_features_t mlx5e_features_check(struct sk_buff *skb,
4160 struct net_device *netdev,
4161 netdev_features_t features)
4163 struct mlx5e_priv *priv = netdev_priv(netdev);
4165 features = vlan_features_check(skb, features);
4166 features = vxlan_features_check(skb, features);
4168 #ifdef CONFIG_MLX5_EN_IPSEC
4169 if (mlx5e_ipsec_feature_check(skb, netdev, features))
4173 /* Validate if the tunneled packet is being offloaded by HW */
4174 if (skb->encapsulation &&
4175 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
4176 return mlx5e_tunnel_features_check(priv, skb, features);
4181 static bool mlx5e_tx_timeout_eq_recover(struct net_device *dev,
4182 struct mlx5e_txqsq *sq)
4184 struct mlx5_eq_comp *eq = sq->cq.mcq.eq;
4187 netdev_err(dev, "EQ 0x%x: Cons = 0x%x, irqn = 0x%x\n",
4188 eq->core.eqn, eq->core.cons_index, eq->core.irqn);
4190 eqe_count = mlx5_eq_poll_irq_disabled(eq);
4194 netdev_err(dev, "Recover %d eqes on EQ 0x%x\n", eqe_count, eq->core.eqn);
4195 sq->channel->stats->eq_rearm++;
4199 static void mlx5e_tx_timeout_work(struct work_struct *work)
4201 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
4203 struct net_device *dev = priv->netdev;
4204 bool reopen_channels = false;
4208 mutex_lock(&priv->state_lock);
4210 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
4213 for (i = 0; i < priv->channels.num * priv->channels.params.num_tc; i++) {
4214 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, i);
4215 struct mlx5e_txqsq *sq = priv->txq2sq[i];
4217 if (!netif_xmit_stopped(dev_queue))
4221 "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",
4222 i, sq->sqn, sq->cq.mcq.cqn, sq->cc, sq->pc,
4223 jiffies_to_usecs(jiffies - dev_queue->trans_start));
4225 /* If we recover a lost interrupt, most likely TX timeout will
4226 * be resolved, skip reopening channels
4228 if (!mlx5e_tx_timeout_eq_recover(dev, sq)) {
4229 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
4230 reopen_channels = true;
4234 if (!reopen_channels)
4237 mlx5e_close_locked(dev);
4238 err = mlx5e_open_locked(dev);
4240 netdev_err(priv->netdev,
4241 "mlx5e_open_locked failed recovering from a tx_timeout, err(%d).\n",
4245 mutex_unlock(&priv->state_lock);
4249 static void mlx5e_tx_timeout(struct net_device *dev)
4251 struct mlx5e_priv *priv = netdev_priv(dev);
4253 netdev_err(dev, "TX timeout detected\n");
4254 queue_work(priv->wq, &priv->tx_timeout_work);
4257 static int mlx5e_xdp_allowed(struct mlx5e_priv *priv, struct bpf_prog *prog)
4259 struct net_device *netdev = priv->netdev;
4260 struct mlx5e_channels new_channels = {};
4262 if (priv->channels.params.lro_en) {
4263 netdev_warn(netdev, "can't set XDP while LRO is on, disable LRO first\n");
4267 if (MLX5_IPSEC_DEV(priv->mdev)) {
4268 netdev_warn(netdev, "can't set XDP with IPSec offload\n");
4272 new_channels.params = priv->channels.params;
4273 new_channels.params.xdp_prog = prog;
4275 if (!mlx5e_rx_is_linear_skb(priv->mdev, &new_channels.params)) {
4276 netdev_warn(netdev, "XDP is not allowed with MTU(%d) > %d\n",
4277 new_channels.params.sw_mtu, MLX5E_XDP_MAX_MTU);
4284 static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog)
4286 struct mlx5e_priv *priv = netdev_priv(netdev);
4287 struct bpf_prog *old_prog;
4288 bool reset, was_opened;
4292 mutex_lock(&priv->state_lock);
4295 err = mlx5e_xdp_allowed(priv, prog);
4300 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
4301 /* no need for full reset when exchanging programs */
4302 reset = (!priv->channels.params.xdp_prog || !prog);
4304 if (was_opened && reset)
4305 mlx5e_close_locked(netdev);
4306 if (was_opened && !reset) {
4307 /* num_channels is invariant here, so we can take the
4308 * batched reference right upfront.
4310 prog = bpf_prog_add(prog, priv->channels.num);
4312 err = PTR_ERR(prog);
4317 /* exchange programs, extra prog reference we got from caller
4318 * as long as we don't fail from this point onwards.
4320 old_prog = xchg(&priv->channels.params.xdp_prog, prog);
4322 bpf_prog_put(old_prog);
4324 if (reset) /* change RQ type according to priv->xdp_prog */
4325 mlx5e_set_rq_type(priv->mdev, &priv->channels.params);
4327 if (was_opened && reset)
4328 mlx5e_open_locked(netdev);
4330 if (!test_bit(MLX5E_STATE_OPENED, &priv->state) || reset)
4333 /* exchanging programs w/o reset, we update ref counts on behalf
4334 * of the channels RQs here.
4336 for (i = 0; i < priv->channels.num; i++) {
4337 struct mlx5e_channel *c = priv->channels.c[i];
4339 clear_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
4340 napi_synchronize(&c->napi);
4341 /* prevent mlx5e_poll_rx_cq from accessing rq->xdp_prog */
4343 old_prog = xchg(&c->rq.xdp_prog, prog);
4345 set_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
4346 /* napi_schedule in case we have missed anything */
4347 napi_schedule(&c->napi);
4350 bpf_prog_put(old_prog);
4354 mutex_unlock(&priv->state_lock);
4358 static u32 mlx5e_xdp_query(struct net_device *dev)
4360 struct mlx5e_priv *priv = netdev_priv(dev);
4361 const struct bpf_prog *xdp_prog;
4364 mutex_lock(&priv->state_lock);
4365 xdp_prog = priv->channels.params.xdp_prog;
4367 prog_id = xdp_prog->aux->id;
4368 mutex_unlock(&priv->state_lock);
4373 static int mlx5e_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4375 switch (xdp->command) {
4376 case XDP_SETUP_PROG:
4377 return mlx5e_xdp_set(dev, xdp->prog);
4378 case XDP_QUERY_PROG:
4379 xdp->prog_id = mlx5e_xdp_query(dev);
4386 const struct net_device_ops mlx5e_netdev_ops = {
4387 .ndo_open = mlx5e_open,
4388 .ndo_stop = mlx5e_close,
4389 .ndo_start_xmit = mlx5e_xmit,
4390 .ndo_setup_tc = mlx5e_setup_tc,
4391 .ndo_select_queue = mlx5e_select_queue,
4392 .ndo_get_stats64 = mlx5e_get_stats,
4393 .ndo_set_rx_mode = mlx5e_set_rx_mode,
4394 .ndo_set_mac_address = mlx5e_set_mac,
4395 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
4396 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
4397 .ndo_set_features = mlx5e_set_features,
4398 .ndo_fix_features = mlx5e_fix_features,
4399 .ndo_change_mtu = mlx5e_change_nic_mtu,
4400 .ndo_do_ioctl = mlx5e_ioctl,
4401 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
4402 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
4403 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
4404 .ndo_features_check = mlx5e_features_check,
4405 .ndo_tx_timeout = mlx5e_tx_timeout,
4406 .ndo_bpf = mlx5e_xdp,
4407 .ndo_xdp_xmit = mlx5e_xdp_xmit,
4408 #ifdef CONFIG_MLX5_EN_ARFS
4409 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
4411 #ifdef CONFIG_MLX5_ESWITCH
4412 /* SRIOV E-Switch NDOs */
4413 .ndo_set_vf_mac = mlx5e_set_vf_mac,
4414 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
4415 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
4416 .ndo_set_vf_trust = mlx5e_set_vf_trust,
4417 .ndo_set_vf_rate = mlx5e_set_vf_rate,
4418 .ndo_get_vf_config = mlx5e_get_vf_config,
4419 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
4420 .ndo_get_vf_stats = mlx5e_get_vf_stats,
4424 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
4426 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
4428 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
4429 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
4430 !MLX5_CAP_ETH(mdev, csum_cap) ||
4431 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
4432 !MLX5_CAP_ETH(mdev, vlan_cap) ||
4433 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
4434 MLX5_CAP_FLOWTABLE(mdev,
4435 flow_table_properties_nic_receive.max_ft_level)
4437 mlx5_core_warn(mdev,
4438 "Not creating net device, some required device capabilities are missing\n");
4441 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
4442 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
4443 if (!MLX5_CAP_GEN(mdev, cq_moderation))
4444 mlx5_core_warn(mdev, "CQ moderation is not supported\n");
4449 void mlx5e_build_default_indir_rqt(u32 *indirection_rqt, int len,
4454 for (i = 0; i < len; i++)
4455 indirection_rqt[i] = i % num_channels;
4458 static bool slow_pci_heuristic(struct mlx5_core_dev *mdev)
4463 mlx5e_port_max_linkspeed(mdev, &link_speed);
4464 pci_bw = pcie_bandwidth_available(mdev->pdev, NULL, NULL, NULL);
4465 mlx5_core_dbg_once(mdev, "Max link speed = %d, PCI BW = %d\n",
4466 link_speed, pci_bw);
4468 #define MLX5E_SLOW_PCI_RATIO (2)
4470 return link_speed && pci_bw &&
4471 link_speed > MLX5E_SLOW_PCI_RATIO * pci_bw;
4474 static struct net_dim_cq_moder mlx5e_get_def_tx_moderation(u8 cq_period_mode)
4476 struct net_dim_cq_moder moder;
4478 moder.cq_period_mode = cq_period_mode;
4479 moder.pkts = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
4480 moder.usec = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
4481 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4482 moder.usec = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC_FROM_CQE;
4487 static struct net_dim_cq_moder mlx5e_get_def_rx_moderation(u8 cq_period_mode)
4489 struct net_dim_cq_moder moder;
4491 moder.cq_period_mode = cq_period_mode;
4492 moder.pkts = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
4493 moder.usec = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
4494 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4495 moder.usec = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
4500 static u8 mlx5_to_net_dim_cq_period_mode(u8 cq_period_mode)
4502 return cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE ?
4503 NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE :
4504 NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
4507 void mlx5e_set_tx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4509 if (params->tx_dim_enabled) {
4510 u8 dim_period_mode = mlx5_to_net_dim_cq_period_mode(cq_period_mode);
4512 params->tx_cq_moderation = net_dim_get_def_tx_moderation(dim_period_mode);
4514 params->tx_cq_moderation = mlx5e_get_def_tx_moderation(cq_period_mode);
4517 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_TX_CQE_BASED_MODER,
4518 params->tx_cq_moderation.cq_period_mode ==
4519 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4522 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4524 if (params->rx_dim_enabled) {
4525 u8 dim_period_mode = mlx5_to_net_dim_cq_period_mode(cq_period_mode);
4527 params->rx_cq_moderation = net_dim_get_def_rx_moderation(dim_period_mode);
4529 params->rx_cq_moderation = mlx5e_get_def_rx_moderation(cq_period_mode);
4532 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_BASED_MODER,
4533 params->rx_cq_moderation.cq_period_mode ==
4534 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4537 static u32 mlx5e_choose_lro_timeout(struct mlx5_core_dev *mdev, u32 wanted_timeout)
4541 /* The supported periods are organized in ascending order */
4542 for (i = 0; i < MLX5E_LRO_TIMEOUT_ARR_SIZE - 1; i++)
4543 if (MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]) >= wanted_timeout)
4546 return MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]);
4549 void mlx5e_build_rq_params(struct mlx5_core_dev *mdev,
4550 struct mlx5e_params *params)
4552 /* Prefer Striding RQ, unless any of the following holds:
4553 * - Striding RQ configuration is not possible/supported.
4554 * - Slow PCI heuristic.
4555 * - Legacy RQ would use linear SKB while Striding RQ would use non-linear.
4557 if (!slow_pci_heuristic(mdev) &&
4558 mlx5e_striding_rq_possible(mdev, params) &&
4559 (mlx5e_rx_mpwqe_is_linear_skb(mdev, params) ||
4560 !mlx5e_rx_is_linear_skb(mdev, params)))
4561 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ, true);
4562 mlx5e_set_rq_type(mdev, params);
4563 mlx5e_init_rq_type_params(mdev, params);
4566 void mlx5e_build_rss_params(struct mlx5e_rss_params *rss_params,
4569 enum mlx5e_traffic_types tt;
4571 rss_params->hfunc = ETH_RSS_HASH_XOR;
4572 netdev_rss_key_fill(rss_params->toeplitz_hash_key,
4573 sizeof(rss_params->toeplitz_hash_key));
4574 mlx5e_build_default_indir_rqt(rss_params->indirection_rqt,
4575 MLX5E_INDIR_RQT_SIZE, num_channels);
4576 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
4577 rss_params->rx_hash_fields[tt] =
4578 tirc_default_config[tt].rx_hash_fields;
4581 void mlx5e_build_nic_params(struct mlx5_core_dev *mdev,
4582 struct mlx5e_rss_params *rss_params,
4583 struct mlx5e_params *params,
4584 u16 max_channels, u16 mtu)
4586 u8 rx_cq_period_mode;
4588 params->sw_mtu = mtu;
4589 params->hard_mtu = MLX5E_ETH_HARD_MTU;
4590 params->num_channels = max_channels;
4594 params->log_sq_size = is_kdump_kernel() ?
4595 MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE :
4596 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
4599 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_XDP_TX_MPWQE,
4600 MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe));
4602 /* set CQE compression */
4603 params->rx_cqe_compress_def = false;
4604 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
4605 MLX5_CAP_GEN(mdev, vport_group_manager))
4606 params->rx_cqe_compress_def = slow_pci_heuristic(mdev);
4608 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS, params->rx_cqe_compress_def);
4609 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_NO_CSUM_COMPLETE, false);
4612 mlx5e_build_rq_params(mdev, params);
4616 /* TODO: && MLX5_CAP_ETH(mdev, lro_cap) */
4617 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
4618 if (!mlx5e_rx_mpwqe_is_linear_skb(mdev, params))
4619 params->lro_en = !slow_pci_heuristic(mdev);
4620 params->lro_timeout = mlx5e_choose_lro_timeout(mdev, MLX5E_DEFAULT_LRO_TIMEOUT);
4622 /* CQ moderation params */
4623 rx_cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
4624 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
4625 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
4626 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4627 params->tx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4628 mlx5e_set_rx_cq_mode_params(params, rx_cq_period_mode);
4629 mlx5e_set_tx_cq_mode_params(params, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
4632 params->tx_min_inline_mode = mlx5e_params_calculate_tx_min_inline(mdev);
4635 mlx5e_build_rss_params(rss_params, params->num_channels);
4638 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
4640 struct mlx5e_priv *priv = netdev_priv(netdev);
4642 mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
4643 if (is_zero_ether_addr(netdev->dev_addr) &&
4644 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
4645 eth_hw_addr_random(netdev);
4646 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
4650 static void mlx5e_build_nic_netdev(struct net_device *netdev)
4652 struct mlx5e_priv *priv = netdev_priv(netdev);
4653 struct mlx5_core_dev *mdev = priv->mdev;
4657 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
4659 netdev->netdev_ops = &mlx5e_netdev_ops;
4661 #ifdef CONFIG_MLX5_CORE_EN_DCB
4662 if (MLX5_CAP_GEN(mdev, vport_group_manager) && MLX5_CAP_GEN(mdev, qos))
4663 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
4666 netdev->watchdog_timeo = 15 * HZ;
4668 netdev->ethtool_ops = &mlx5e_ethtool_ops;
4670 netdev->vlan_features |= NETIF_F_SG;
4671 netdev->vlan_features |= NETIF_F_IP_CSUM;
4672 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
4673 netdev->vlan_features |= NETIF_F_GRO;
4674 netdev->vlan_features |= NETIF_F_TSO;
4675 netdev->vlan_features |= NETIF_F_TSO6;
4676 netdev->vlan_features |= NETIF_F_RXCSUM;
4677 netdev->vlan_features |= NETIF_F_RXHASH;
4679 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_TX;
4680 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_RX;
4682 if (!!MLX5_CAP_ETH(mdev, lro_cap) &&
4683 mlx5e_check_fragmented_striding_rq_cap(mdev))
4684 netdev->vlan_features |= NETIF_F_LRO;
4686 netdev->hw_features = netdev->vlan_features;
4687 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4688 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4689 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4690 netdev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
4692 if (mlx5_vxlan_allowed(mdev->vxlan) || MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4693 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
4694 netdev->hw_enc_features |= NETIF_F_IPV6_CSUM;
4695 netdev->hw_enc_features |= NETIF_F_TSO;
4696 netdev->hw_enc_features |= NETIF_F_TSO6;
4697 netdev->hw_enc_features |= NETIF_F_GSO_PARTIAL;
4700 if (mlx5_vxlan_allowed(mdev->vxlan)) {
4701 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
4702 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4703 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
4704 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4705 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
4708 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4709 netdev->hw_features |= NETIF_F_GSO_GRE |
4710 NETIF_F_GSO_GRE_CSUM;
4711 netdev->hw_enc_features |= NETIF_F_GSO_GRE |
4712 NETIF_F_GSO_GRE_CSUM;
4713 netdev->gso_partial_features |= NETIF_F_GSO_GRE |
4714 NETIF_F_GSO_GRE_CSUM;
4717 netdev->hw_features |= NETIF_F_GSO_PARTIAL;
4718 netdev->gso_partial_features |= NETIF_F_GSO_UDP_L4;
4719 netdev->hw_features |= NETIF_F_GSO_UDP_L4;
4720 netdev->features |= NETIF_F_GSO_UDP_L4;
4722 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
4725 netdev->hw_features |= NETIF_F_RXALL;
4727 if (MLX5_CAP_ETH(mdev, scatter_fcs))
4728 netdev->hw_features |= NETIF_F_RXFCS;
4730 netdev->features = netdev->hw_features;
4731 if (!priv->channels.params.lro_en)
4732 netdev->features &= ~NETIF_F_LRO;
4735 netdev->features &= ~NETIF_F_RXALL;
4737 if (!priv->channels.params.scatter_fcs_en)
4738 netdev->features &= ~NETIF_F_RXFCS;
4740 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
4741 if (FT_CAP(flow_modify_en) &&
4742 FT_CAP(modify_root) &&
4743 FT_CAP(identified_miss_table_mode) &&
4744 FT_CAP(flow_table_modify)) {
4745 #ifdef CONFIG_MLX5_ESWITCH
4746 netdev->hw_features |= NETIF_F_HW_TC;
4748 #ifdef CONFIG_MLX5_EN_ARFS
4749 netdev->hw_features |= NETIF_F_NTUPLE;
4753 netdev->features |= NETIF_F_HIGHDMA;
4754 netdev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
4756 netdev->priv_flags |= IFF_UNICAST_FLT;
4758 mlx5e_set_netdev_dev_addr(netdev);
4759 mlx5e_ipsec_build_netdev(priv);
4760 mlx5e_tls_build_netdev(priv);
4763 void mlx5e_create_q_counters(struct mlx5e_priv *priv)
4765 struct mlx5_core_dev *mdev = priv->mdev;
4768 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
4770 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
4771 priv->q_counter = 0;
4774 err = mlx5_core_alloc_q_counter(mdev, &priv->drop_rq_q_counter);
4776 mlx5_core_warn(mdev, "alloc drop RQ counter failed, %d\n", err);
4777 priv->drop_rq_q_counter = 0;
4781 void mlx5e_destroy_q_counters(struct mlx5e_priv *priv)
4783 if (priv->q_counter)
4784 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
4786 if (priv->drop_rq_q_counter)
4787 mlx5_core_dealloc_q_counter(priv->mdev, priv->drop_rq_q_counter);
4790 static int mlx5e_nic_init(struct mlx5_core_dev *mdev,
4791 struct net_device *netdev,
4792 const struct mlx5e_profile *profile,
4795 struct mlx5e_priv *priv = netdev_priv(netdev);
4796 struct mlx5e_rss_params *rss = &priv->rss_params;
4799 err = mlx5e_netdev_init(netdev, priv, mdev, profile, ppriv);
4803 mlx5e_build_nic_params(mdev, rss, &priv->channels.params,
4804 mlx5e_get_netdev_max_channels(netdev),
4807 mlx5e_timestamp_init(priv);
4809 err = mlx5e_ipsec_init(priv);
4811 mlx5_core_err(mdev, "IPSec initialization failed, %d\n", err);
4812 err = mlx5e_tls_init(priv);
4814 mlx5_core_err(mdev, "TLS initialization failed, %d\n", err);
4815 mlx5e_build_nic_netdev(netdev);
4816 mlx5e_build_tc2txq_maps(priv);
4821 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
4823 mlx5e_tls_cleanup(priv);
4824 mlx5e_ipsec_cleanup(priv);
4825 mlx5e_netdev_cleanup(priv->netdev, priv);
4828 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv)
4830 struct mlx5_core_dev *mdev = priv->mdev;
4833 mlx5e_create_q_counters(priv);
4835 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
4837 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
4838 goto err_destroy_q_counters;
4841 err = mlx5e_create_indirect_rqt(priv);
4843 goto err_close_drop_rq;
4845 err = mlx5e_create_direct_rqts(priv);
4847 goto err_destroy_indirect_rqts;
4849 err = mlx5e_create_indirect_tirs(priv, true);
4851 goto err_destroy_direct_rqts;
4853 err = mlx5e_create_direct_tirs(priv);
4855 goto err_destroy_indirect_tirs;
4857 err = mlx5e_create_flow_steering(priv);
4859 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
4860 goto err_destroy_direct_tirs;
4863 err = mlx5e_tc_nic_init(priv);
4865 goto err_destroy_flow_steering;
4869 err_destroy_flow_steering:
4870 mlx5e_destroy_flow_steering(priv);
4871 err_destroy_direct_tirs:
4872 mlx5e_destroy_direct_tirs(priv);
4873 err_destroy_indirect_tirs:
4874 mlx5e_destroy_indirect_tirs(priv, true);
4875 err_destroy_direct_rqts:
4876 mlx5e_destroy_direct_rqts(priv);
4877 err_destroy_indirect_rqts:
4878 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
4880 mlx5e_close_drop_rq(&priv->drop_rq);
4881 err_destroy_q_counters:
4882 mlx5e_destroy_q_counters(priv);
4886 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv)
4888 mlx5e_tc_nic_cleanup(priv);
4889 mlx5e_destroy_flow_steering(priv);
4890 mlx5e_destroy_direct_tirs(priv);
4891 mlx5e_destroy_indirect_tirs(priv, true);
4892 mlx5e_destroy_direct_rqts(priv);
4893 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
4894 mlx5e_close_drop_rq(&priv->drop_rq);
4895 mlx5e_destroy_q_counters(priv);
4898 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv)
4902 err = mlx5e_create_tises(priv);
4904 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
4908 #ifdef CONFIG_MLX5_CORE_EN_DCB
4909 mlx5e_dcbnl_initialize(priv);
4914 static void mlx5e_nic_enable(struct mlx5e_priv *priv)
4916 struct net_device *netdev = priv->netdev;
4917 struct mlx5_core_dev *mdev = priv->mdev;
4920 mlx5e_init_l2_addr(priv);
4922 /* Marking the link as currently not needed by the Driver */
4923 if (!netif_running(netdev))
4924 mlx5_set_port_admin_status(mdev, MLX5_PORT_DOWN);
4926 /* MTU range: 68 - hw-specific max */
4927 netdev->min_mtu = ETH_MIN_MTU;
4928 mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
4929 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
4930 mlx5e_set_dev_port_mtu(priv);
4932 mlx5_lag_add(mdev, netdev);
4934 mlx5e_enable_async_events(priv);
4935 if (mlx5e_monitor_counter_supported(priv))
4936 mlx5e_monitor_counter_init(priv);
4938 if (netdev->reg_state != NETREG_REGISTERED)
4940 #ifdef CONFIG_MLX5_CORE_EN_DCB
4941 mlx5e_dcbnl_init_app(priv);
4944 queue_work(priv->wq, &priv->set_rx_mode_work);
4947 if (netif_running(netdev))
4949 netif_device_attach(netdev);
4953 static void mlx5e_nic_disable(struct mlx5e_priv *priv)
4955 struct mlx5_core_dev *mdev = priv->mdev;
4957 #ifdef CONFIG_MLX5_CORE_EN_DCB
4958 if (priv->netdev->reg_state == NETREG_REGISTERED)
4959 mlx5e_dcbnl_delete_app(priv);
4963 if (netif_running(priv->netdev))
4964 mlx5e_close(priv->netdev);
4965 netif_device_detach(priv->netdev);
4968 queue_work(priv->wq, &priv->set_rx_mode_work);
4970 if (mlx5e_monitor_counter_supported(priv))
4971 mlx5e_monitor_counter_cleanup(priv);
4973 mlx5e_disable_async_events(priv);
4974 mlx5_lag_remove(mdev);
4977 static const struct mlx5e_profile mlx5e_nic_profile = {
4978 .init = mlx5e_nic_init,
4979 .cleanup = mlx5e_nic_cleanup,
4980 .init_rx = mlx5e_init_nic_rx,
4981 .cleanup_rx = mlx5e_cleanup_nic_rx,
4982 .init_tx = mlx5e_init_nic_tx,
4983 .cleanup_tx = mlx5e_cleanup_nic_tx,
4984 .enable = mlx5e_nic_enable,
4985 .disable = mlx5e_nic_disable,
4986 .update_stats = mlx5e_update_ndo_stats,
4987 .update_carrier = mlx5e_update_carrier,
4988 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe,
4989 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
4990 .max_tc = MLX5E_MAX_NUM_TC,
4993 /* mlx5e generic netdev management API (move to en_common.c) */
4995 /* mlx5e_netdev_init/cleanup must be called from profile->init/cleanup callbacks */
4996 int mlx5e_netdev_init(struct net_device *netdev,
4997 struct mlx5e_priv *priv,
4998 struct mlx5_core_dev *mdev,
4999 const struct mlx5e_profile *profile,
5004 priv->netdev = netdev;
5005 priv->profile = profile;
5006 priv->ppriv = ppriv;
5007 priv->msglevel = MLX5E_MSG_LEVEL;
5008 priv->max_opened_tc = 1;
5010 mutex_init(&priv->state_lock);
5011 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
5012 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
5013 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
5014 INIT_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
5016 priv->wq = create_singlethread_workqueue("mlx5e");
5021 netif_carrier_off(netdev);
5023 #ifdef CONFIG_MLX5_EN_ARFS
5024 netdev->rx_cpu_rmap = mlx5_eq_table_get_rmap(mdev);
5030 void mlx5e_netdev_cleanup(struct net_device *netdev, struct mlx5e_priv *priv)
5032 destroy_workqueue(priv->wq);
5035 struct net_device *mlx5e_create_netdev(struct mlx5_core_dev *mdev,
5036 const struct mlx5e_profile *profile,
5040 struct net_device *netdev;
5043 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
5044 nch * profile->max_tc,
5047 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
5051 err = profile->init(mdev, netdev, profile, ppriv);
5053 mlx5_core_err(mdev, "failed to init mlx5e profile %d\n", err);
5054 goto err_free_netdev;
5060 free_netdev(netdev);
5065 int mlx5e_attach_netdev(struct mlx5e_priv *priv)
5067 const struct mlx5e_profile *profile;
5071 profile = priv->profile;
5072 clear_bit(MLX5E_STATE_DESTROYING, &priv->state);
5074 /* max number of channels may have changed */
5075 max_nch = mlx5e_get_max_num_channels(priv->mdev);
5076 if (priv->channels.params.num_channels > max_nch) {
5077 mlx5_core_warn(priv->mdev, "MLX5E: Reducing number of channels to %d\n", max_nch);
5078 priv->channels.params.num_channels = max_nch;
5079 mlx5e_build_default_indir_rqt(priv->rss_params.indirection_rqt,
5080 MLX5E_INDIR_RQT_SIZE, max_nch);
5083 err = profile->init_tx(priv);
5087 err = profile->init_rx(priv);
5089 goto err_cleanup_tx;
5091 if (profile->enable)
5092 profile->enable(priv);
5097 profile->cleanup_tx(priv);
5103 void mlx5e_detach_netdev(struct mlx5e_priv *priv)
5105 const struct mlx5e_profile *profile = priv->profile;
5107 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
5109 if (profile->disable)
5110 profile->disable(priv);
5111 flush_workqueue(priv->wq);
5113 profile->cleanup_rx(priv);
5114 profile->cleanup_tx(priv);
5115 cancel_work_sync(&priv->update_stats_work);
5118 void mlx5e_destroy_netdev(struct mlx5e_priv *priv)
5120 const struct mlx5e_profile *profile = priv->profile;
5121 struct net_device *netdev = priv->netdev;
5123 if (profile->cleanup)
5124 profile->cleanup(priv);
5125 free_netdev(netdev);
5128 /* mlx5e_attach and mlx5e_detach scope should be only creating/destroying
5129 * hardware contexts and to connect it to the current netdev.
5131 static int mlx5e_attach(struct mlx5_core_dev *mdev, void *vpriv)
5133 struct mlx5e_priv *priv = vpriv;
5134 struct net_device *netdev = priv->netdev;
5137 if (netif_device_present(netdev))
5140 err = mlx5e_create_mdev_resources(mdev);
5144 err = mlx5e_attach_netdev(priv);
5146 mlx5e_destroy_mdev_resources(mdev);
5153 static void mlx5e_detach(struct mlx5_core_dev *mdev, void *vpriv)
5155 struct mlx5e_priv *priv = vpriv;
5156 struct net_device *netdev = priv->netdev;
5158 if (!netif_device_present(netdev))
5161 mlx5e_detach_netdev(priv);
5162 mlx5e_destroy_mdev_resources(mdev);
5165 static void *mlx5e_add(struct mlx5_core_dev *mdev)
5167 struct net_device *netdev;
5172 err = mlx5e_check_required_hca_cap(mdev);
5176 #ifdef CONFIG_MLX5_ESWITCH
5177 if (MLX5_ESWITCH_MANAGER(mdev) &&
5178 mlx5_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
5179 mlx5e_rep_register_vport_reps(mdev);
5184 nch = mlx5e_get_max_num_channels(mdev);
5185 netdev = mlx5e_create_netdev(mdev, &mlx5e_nic_profile, nch, NULL);
5187 mlx5_core_err(mdev, "mlx5e_create_netdev failed\n");
5191 priv = netdev_priv(netdev);
5193 err = mlx5e_attach(mdev, priv);
5195 mlx5_core_err(mdev, "mlx5e_attach failed, %d\n", err);
5196 goto err_destroy_netdev;
5199 err = register_netdev(netdev);
5201 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
5205 #ifdef CONFIG_MLX5_CORE_EN_DCB
5206 mlx5e_dcbnl_init_app(priv);
5211 mlx5e_detach(mdev, priv);
5213 mlx5e_destroy_netdev(priv);
5217 static void mlx5e_remove(struct mlx5_core_dev *mdev, void *vpriv)
5219 struct mlx5e_priv *priv;
5221 #ifdef CONFIG_MLX5_ESWITCH
5222 if (MLX5_ESWITCH_MANAGER(mdev) && vpriv == mdev) {
5223 mlx5e_rep_unregister_vport_reps(mdev);
5228 #ifdef CONFIG_MLX5_CORE_EN_DCB
5229 mlx5e_dcbnl_delete_app(priv);
5231 unregister_netdev(priv->netdev);
5232 mlx5e_detach(mdev, vpriv);
5233 mlx5e_destroy_netdev(priv);
5236 static struct mlx5_interface mlx5e_interface = {
5238 .remove = mlx5e_remove,
5239 .attach = mlx5e_attach,
5240 .detach = mlx5e_detach,
5241 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
5244 void mlx5e_init(void)
5246 mlx5e_ipsec_build_inverse_table();
5247 mlx5e_build_ptys2ethtool_map();
5248 mlx5_register_interface(&mlx5e_interface);
5251 void mlx5e_cleanup(void)
5253 mlx5_unregister_interface(&mlx5e_interface);