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>
42 #include "en_accel/ipsec.h"
43 #include "en_accel/ipsec_rxtx.h"
44 #include "accel/ipsec.h"
47 struct mlx5e_rq_param {
48 u32 rqc[MLX5_ST_SZ_DW(rqc)];
49 struct mlx5_wq_param wq;
52 struct mlx5e_sq_param {
53 u32 sqc[MLX5_ST_SZ_DW(sqc)];
54 struct mlx5_wq_param wq;
57 struct mlx5e_cq_param {
58 u32 cqc[MLX5_ST_SZ_DW(cqc)];
59 struct mlx5_wq_param wq;
64 struct mlx5e_channel_param {
65 struct mlx5e_rq_param rq;
66 struct mlx5e_sq_param sq;
67 struct mlx5e_sq_param xdp_sq;
68 struct mlx5e_sq_param icosq;
69 struct mlx5e_cq_param rx_cq;
70 struct mlx5e_cq_param tx_cq;
71 struct mlx5e_cq_param icosq_cq;
74 bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
76 bool striding_rq_umr = MLX5_CAP_GEN(mdev, striding_rq) &&
77 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
78 MLX5_CAP_ETH(mdev, reg_umr_sq);
79 u16 max_wqe_sz_cap = MLX5_CAP_GEN(mdev, max_wqe_sz_sq);
80 bool inline_umr = MLX5E_UMR_WQE_INLINE_SZ <= max_wqe_sz_cap;
85 mlx5_core_warn(mdev, "Cannot support Striding RQ: UMR WQE size (%d) exceeds maximum supported (%d).\n",
86 (int)MLX5E_UMR_WQE_INLINE_SZ, max_wqe_sz_cap);
92 static u32 mlx5e_mpwqe_get_linear_frag_sz(struct mlx5e_params *params)
94 if (!params->xdp_prog) {
95 u16 hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
96 u16 rq_headroom = MLX5_RX_HEADROOM + NET_IP_ALIGN;
98 return MLX5_SKB_FRAG_SZ(rq_headroom + hw_mtu);
104 static u8 mlx5e_mpwqe_log_pkts_per_wqe(struct mlx5e_params *params)
106 u32 linear_frag_sz = mlx5e_mpwqe_get_linear_frag_sz(params);
108 return MLX5_MPWRQ_LOG_WQE_SZ - order_base_2(linear_frag_sz);
111 static bool mlx5e_rx_mpwqe_is_linear_skb(struct mlx5_core_dev *mdev,
112 struct mlx5e_params *params)
114 u32 frag_sz = mlx5e_mpwqe_get_linear_frag_sz(params);
115 s8 signed_log_num_strides_param;
118 if (params->lro_en || frag_sz > PAGE_SIZE)
121 if (MLX5_CAP_GEN(mdev, ext_stride_num_range))
124 log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ - order_base_2(frag_sz);
125 signed_log_num_strides_param =
126 (s8)log_num_strides - MLX5_MPWQE_LOG_NUM_STRIDES_BASE;
128 return signed_log_num_strides_param >= 0;
131 static u8 mlx5e_mpwqe_get_log_rq_size(struct mlx5e_params *params)
133 if (params->log_rq_mtu_frames <
134 mlx5e_mpwqe_log_pkts_per_wqe(params) + MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW)
135 return MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW;
137 return params->log_rq_mtu_frames - mlx5e_mpwqe_log_pkts_per_wqe(params);
140 static u8 mlx5e_mpwqe_get_log_stride_size(struct mlx5_core_dev *mdev,
141 struct mlx5e_params *params)
143 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, params))
144 return order_base_2(mlx5e_mpwqe_get_linear_frag_sz(params));
146 return MLX5E_MPWQE_STRIDE_SZ(mdev,
147 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
150 static u8 mlx5e_mpwqe_get_log_num_strides(struct mlx5_core_dev *mdev,
151 struct mlx5e_params *params)
153 return MLX5_MPWRQ_LOG_WQE_SZ -
154 mlx5e_mpwqe_get_log_stride_size(mdev, params);
157 static u16 mlx5e_get_rq_headroom(struct mlx5_core_dev *mdev,
158 struct mlx5e_params *params)
160 u16 linear_rq_headroom = params->xdp_prog ?
161 XDP_PACKET_HEADROOM : MLX5_RX_HEADROOM;
163 linear_rq_headroom += NET_IP_ALIGN;
165 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST)
166 return linear_rq_headroom;
168 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, params))
169 return linear_rq_headroom;
174 void mlx5e_init_rq_type_params(struct mlx5_core_dev *mdev,
175 struct mlx5e_params *params)
177 params->lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
178 params->log_rq_mtu_frames = is_kdump_kernel() ?
179 MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE :
180 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
181 switch (params->rq_wq_type) {
182 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
184 default: /* MLX5_WQ_TYPE_LINKED_LIST */
185 /* Extra room needed for build_skb */
186 params->lro_wqe_sz -= mlx5e_get_rq_headroom(mdev, params) +
187 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
190 mlx5_core_info(mdev, "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
191 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
192 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ ?
193 BIT(mlx5e_mpwqe_get_log_rq_size(params)) :
194 BIT(params->log_rq_mtu_frames),
195 BIT(mlx5e_mpwqe_get_log_stride_size(mdev, params)),
196 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
199 bool mlx5e_striding_rq_possible(struct mlx5_core_dev *mdev,
200 struct mlx5e_params *params)
202 return mlx5e_check_fragmented_striding_rq_cap(mdev) &&
203 !MLX5_IPSEC_DEV(mdev) &&
204 !(params->xdp_prog && !mlx5e_rx_mpwqe_is_linear_skb(mdev, params));
207 void mlx5e_set_rq_type(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
209 params->rq_wq_type = mlx5e_striding_rq_possible(mdev, params) &&
210 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ) ?
211 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
212 MLX5_WQ_TYPE_LINKED_LIST;
215 static void mlx5e_update_carrier(struct mlx5e_priv *priv)
217 struct mlx5_core_dev *mdev = priv->mdev;
220 port_state = mlx5_query_vport_state(mdev,
221 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT,
224 if (port_state == VPORT_STATE_UP) {
225 netdev_info(priv->netdev, "Link up\n");
226 netif_carrier_on(priv->netdev);
228 netdev_info(priv->netdev, "Link down\n");
229 netif_carrier_off(priv->netdev);
233 static void mlx5e_update_carrier_work(struct work_struct *work)
235 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
236 update_carrier_work);
238 mutex_lock(&priv->state_lock);
239 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
240 if (priv->profile->update_carrier)
241 priv->profile->update_carrier(priv);
242 mutex_unlock(&priv->state_lock);
245 void mlx5e_update_stats(struct mlx5e_priv *priv)
249 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
250 if (mlx5e_stats_grps[i].update_stats)
251 mlx5e_stats_grps[i].update_stats(priv);
254 static void mlx5e_update_ndo_stats(struct mlx5e_priv *priv)
258 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
259 if (mlx5e_stats_grps[i].update_stats_mask &
260 MLX5E_NDO_UPDATE_STATS)
261 mlx5e_stats_grps[i].update_stats(priv);
264 void mlx5e_update_stats_work(struct work_struct *work)
266 struct delayed_work *dwork = to_delayed_work(work);
267 struct mlx5e_priv *priv = container_of(dwork, struct mlx5e_priv,
269 mutex_lock(&priv->state_lock);
270 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
271 priv->profile->update_stats(priv);
272 queue_delayed_work(priv->wq, dwork,
273 msecs_to_jiffies(MLX5E_UPDATE_STATS_INTERVAL));
275 mutex_unlock(&priv->state_lock);
278 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
279 enum mlx5_dev_event event, unsigned long param)
281 struct mlx5e_priv *priv = vpriv;
283 if (!test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state))
287 case MLX5_DEV_EVENT_PORT_UP:
288 case MLX5_DEV_EVENT_PORT_DOWN:
289 queue_work(priv->wq, &priv->update_carrier_work);
296 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
298 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
301 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
303 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
304 synchronize_irq(pci_irq_vector(priv->mdev->pdev, MLX5_EQ_VEC_ASYNC));
307 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq,
308 struct mlx5e_icosq *sq,
309 struct mlx5e_umr_wqe *wqe)
311 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
312 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
313 u8 ds_cnt = DIV_ROUND_UP(MLX5E_UMR_WQE_INLINE_SZ, MLX5_SEND_WQE_DS);
315 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
317 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
318 cseg->imm = rq->mkey_be;
320 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN | MLX5_UMR_INLINE;
321 ucseg->xlt_octowords =
322 cpu_to_be16(MLX5_MTT_OCTW(MLX5_MPWRQ_PAGES_PER_WQE));
323 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
326 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq,
327 struct mlx5e_channel *c)
329 int wq_sz = mlx5_wq_ll_get_size(&rq->wq);
331 rq->mpwqe.info = kzalloc_node(wq_sz * sizeof(*rq->mpwqe.info),
332 GFP_KERNEL, cpu_to_node(c->cpu));
336 mlx5e_build_umr_wqe(rq, &c->icosq, &rq->mpwqe.umr_wqe);
341 static int mlx5e_create_umr_mkey(struct mlx5_core_dev *mdev,
342 u64 npages, u8 page_shift,
343 struct mlx5_core_mkey *umr_mkey)
345 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
350 in = kvzalloc(inlen, GFP_KERNEL);
354 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
356 MLX5_SET(mkc, mkc, free, 1);
357 MLX5_SET(mkc, mkc, umr_en, 1);
358 MLX5_SET(mkc, mkc, lw, 1);
359 MLX5_SET(mkc, mkc, lr, 1);
360 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT);
362 MLX5_SET(mkc, mkc, qpn, 0xffffff);
363 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.pdn);
364 MLX5_SET64(mkc, mkc, len, npages << page_shift);
365 MLX5_SET(mkc, mkc, translations_octword_size,
366 MLX5_MTT_OCTW(npages));
367 MLX5_SET(mkc, mkc, log_page_size, page_shift);
369 err = mlx5_core_create_mkey(mdev, umr_mkey, in, inlen);
375 static int mlx5e_create_rq_umr_mkey(struct mlx5_core_dev *mdev, struct mlx5e_rq *rq)
377 u64 num_mtts = MLX5E_REQUIRED_MTTS(mlx5_wq_ll_get_size(&rq->wq));
379 return mlx5e_create_umr_mkey(mdev, num_mtts, PAGE_SHIFT, &rq->umr_mkey);
382 static inline u64 mlx5e_get_mpwqe_offset(struct mlx5e_rq *rq, u16 wqe_ix)
384 return (wqe_ix << MLX5E_LOG_ALIGNED_MPWQE_PPW) << PAGE_SHIFT;
387 static int mlx5e_alloc_rq(struct mlx5e_channel *c,
388 struct mlx5e_params *params,
389 struct mlx5e_rq_param *rqp,
392 struct mlx5_core_dev *mdev = c->mdev;
393 void *rqc = rqp->rqc;
394 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
401 rqp->wq.db_numa_node = cpu_to_node(c->cpu);
403 err = mlx5_wq_ll_create(mdev, &rqp->wq, rqc_wq, &rq->wq,
408 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
410 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
412 rq->wq_type = params->rq_wq_type;
414 rq->netdev = c->netdev;
415 rq->tstamp = c->tstamp;
416 rq->clock = &mdev->clock;
420 rq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
422 rq->xdp_prog = params->xdp_prog ? bpf_prog_inc(params->xdp_prog) : NULL;
423 if (IS_ERR(rq->xdp_prog)) {
424 err = PTR_ERR(rq->xdp_prog);
426 goto err_rq_wq_destroy;
429 err = xdp_rxq_info_reg(&rq->xdp_rxq, rq->netdev, rq->ix);
431 goto err_rq_wq_destroy;
433 rq->buff.map_dir = rq->xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
434 rq->buff.headroom = mlx5e_get_rq_headroom(mdev, params);
436 switch (rq->wq_type) {
437 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
438 rq->post_wqes = mlx5e_post_rx_mpwqes;
439 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
441 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe_mpwqe;
442 #ifdef CONFIG_MLX5_EN_IPSEC
443 if (MLX5_IPSEC_DEV(mdev)) {
445 netdev_err(c->netdev, "MPWQE RQ with IPSec offload not supported\n");
446 goto err_rq_wq_destroy;
449 if (!rq->handle_rx_cqe) {
451 netdev_err(c->netdev, "RX handler of MPWQE RQ is not set, err %d\n", err);
452 goto err_rq_wq_destroy;
455 rq->mpwqe.skb_from_cqe_mpwrq =
456 mlx5e_rx_mpwqe_is_linear_skb(mdev, params) ?
457 mlx5e_skb_from_cqe_mpwrq_linear :
458 mlx5e_skb_from_cqe_mpwrq_nonlinear;
459 rq->mpwqe.log_stride_sz = mlx5e_mpwqe_get_log_stride_size(mdev, params);
460 rq->mpwqe.num_strides = BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params));
462 byte_count = rq->mpwqe.num_strides << rq->mpwqe.log_stride_sz;
464 err = mlx5e_create_rq_umr_mkey(mdev, rq);
466 goto err_rq_wq_destroy;
467 rq->mkey_be = cpu_to_be32(rq->umr_mkey.key);
469 err = mlx5e_rq_alloc_mpwqe_info(rq, c);
471 goto err_destroy_umr_mkey;
473 default: /* MLX5_WQ_TYPE_LINKED_LIST */
475 kzalloc_node(wq_sz * sizeof(*rq->wqe.frag_info),
476 GFP_KERNEL, cpu_to_node(c->cpu));
477 if (!rq->wqe.frag_info) {
479 goto err_rq_wq_destroy;
481 rq->post_wqes = mlx5e_post_rx_wqes;
482 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
484 #ifdef CONFIG_MLX5_EN_IPSEC
486 rq->handle_rx_cqe = mlx5e_ipsec_handle_rx_cqe;
489 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe;
490 if (!rq->handle_rx_cqe) {
491 kfree(rq->wqe.frag_info);
493 netdev_err(c->netdev, "RX handler of RQ is not set, err %d\n", err);
494 goto err_rq_wq_destroy;
497 byte_count = params->lro_en ?
499 MLX5E_SW2HW_MTU(params, params->sw_mtu);
500 #ifdef CONFIG_MLX5_EN_IPSEC
501 if (MLX5_IPSEC_DEV(mdev))
502 byte_count += MLX5E_METADATA_ETHER_LEN;
504 rq->wqe.page_reuse = !params->xdp_prog && !params->lro_en;
506 /* calc the required page order */
507 rq->wqe.frag_sz = MLX5_SKB_FRAG_SZ(rq->buff.headroom + byte_count);
508 npages = DIV_ROUND_UP(rq->wqe.frag_sz, PAGE_SIZE);
509 rq->buff.page_order = order_base_2(npages);
511 byte_count |= MLX5_HW_START_PADDING;
512 rq->mkey_be = c->mkey_be;
515 /* This must only be activate for order-0 pages */
517 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
518 MEM_TYPE_PAGE_ORDER0, NULL);
520 goto err_rq_wq_destroy;
523 for (i = 0; i < wq_sz; i++) {
524 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
526 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
527 u64 dma_offset = mlx5e_get_mpwqe_offset(rq, i);
529 wqe->data.addr = cpu_to_be64(dma_offset + rq->buff.headroom);
532 wqe->data.byte_count = cpu_to_be32(byte_count);
533 wqe->data.lkey = rq->mkey_be;
536 INIT_WORK(&rq->dim.work, mlx5e_rx_dim_work);
538 switch (params->rx_cq_moderation.cq_period_mode) {
539 case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
540 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE;
542 case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
544 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
547 rq->page_cache.head = 0;
548 rq->page_cache.tail = 0;
552 err_destroy_umr_mkey:
553 mlx5_core_destroy_mkey(mdev, &rq->umr_mkey);
557 bpf_prog_put(rq->xdp_prog);
558 xdp_rxq_info_unreg(&rq->xdp_rxq);
559 mlx5_wq_destroy(&rq->wq_ctrl);
564 static void mlx5e_free_rq(struct mlx5e_rq *rq)
569 bpf_prog_put(rq->xdp_prog);
571 xdp_rxq_info_unreg(&rq->xdp_rxq);
573 switch (rq->wq_type) {
574 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
575 kfree(rq->mpwqe.info);
576 mlx5_core_destroy_mkey(rq->mdev, &rq->umr_mkey);
578 default: /* MLX5_WQ_TYPE_LINKED_LIST */
579 kfree(rq->wqe.frag_info);
582 for (i = rq->page_cache.head; i != rq->page_cache.tail;
583 i = (i + 1) & (MLX5E_CACHE_SIZE - 1)) {
584 struct mlx5e_dma_info *dma_info = &rq->page_cache.page_cache[i];
586 mlx5e_page_release(rq, dma_info, false);
588 mlx5_wq_destroy(&rq->wq_ctrl);
591 static int mlx5e_create_rq(struct mlx5e_rq *rq,
592 struct mlx5e_rq_param *param)
594 struct mlx5_core_dev *mdev = rq->mdev;
602 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
603 sizeof(u64) * rq->wq_ctrl.buf.npages;
604 in = kvzalloc(inlen, GFP_KERNEL);
608 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
609 wq = MLX5_ADDR_OF(rqc, rqc, wq);
611 memcpy(rqc, param->rqc, sizeof(param->rqc));
613 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
614 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
615 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
616 MLX5_ADAPTER_PAGE_SHIFT);
617 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
619 mlx5_fill_page_array(&rq->wq_ctrl.buf,
620 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
622 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
629 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
632 struct mlx5_core_dev *mdev = rq->mdev;
639 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
640 in = kvzalloc(inlen, GFP_KERNEL);
644 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
646 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
647 MLX5_SET(rqc, rqc, state, next_state);
649 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
656 static int mlx5e_modify_rq_scatter_fcs(struct mlx5e_rq *rq, bool enable)
658 struct mlx5e_channel *c = rq->channel;
659 struct mlx5e_priv *priv = c->priv;
660 struct mlx5_core_dev *mdev = priv->mdev;
667 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
668 in = kvzalloc(inlen, GFP_KERNEL);
672 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
674 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
675 MLX5_SET64(modify_rq_in, in, modify_bitmask,
676 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_SCATTER_FCS);
677 MLX5_SET(rqc, rqc, scatter_fcs, enable);
678 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
680 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
687 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
689 struct mlx5e_channel *c = rq->channel;
690 struct mlx5_core_dev *mdev = c->mdev;
696 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
697 in = kvzalloc(inlen, GFP_KERNEL);
701 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
703 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
704 MLX5_SET64(modify_rq_in, in, modify_bitmask,
705 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
706 MLX5_SET(rqc, rqc, vsd, vsd);
707 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
709 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
716 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
718 mlx5_core_destroy_rq(rq->mdev, rq->rqn);
721 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
723 unsigned long exp_time = jiffies + msecs_to_jiffies(20000);
724 struct mlx5e_channel *c = rq->channel;
726 struct mlx5_wq_ll *wq = &rq->wq;
727 u16 min_wqes = mlx5_min_rx_wqes(rq->wq_type, mlx5_wq_ll_get_size(wq));
729 while (time_before(jiffies, exp_time)) {
730 if (wq->cur_sz >= min_wqes)
736 netdev_warn(c->netdev, "Failed to get min RX wqes on RQN[0x%x] wq cur_sz(%d) min_rx_wqes(%d)\n",
737 rq->rqn, wq->cur_sz, min_wqes);
741 static void mlx5e_free_rx_descs(struct mlx5e_rq *rq)
743 struct mlx5_wq_ll *wq = &rq->wq;
744 struct mlx5e_rx_wqe *wqe;
748 /* UMR WQE (if in progress) is always at wq->head */
749 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ &&
750 rq->mpwqe.umr_in_progress)
751 mlx5e_free_rx_mpwqe(rq, &rq->mpwqe.info[wq->head]);
753 while (!mlx5_wq_ll_is_empty(wq)) {
754 wqe_ix_be = *wq->tail_next;
755 wqe_ix = be16_to_cpu(wqe_ix_be);
756 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_ix);
757 rq->dealloc_wqe(rq, wqe_ix);
758 mlx5_wq_ll_pop(&rq->wq, wqe_ix_be,
759 &wqe->next.next_wqe_index);
762 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST && rq->wqe.page_reuse) {
763 /* Clean outstanding pages on handled WQEs that decided to do page-reuse,
764 * but yet to be re-posted.
766 int wq_sz = mlx5_wq_ll_get_size(&rq->wq);
768 for (wqe_ix = 0; wqe_ix < wq_sz; wqe_ix++)
769 rq->dealloc_wqe(rq, wqe_ix);
773 static int mlx5e_open_rq(struct mlx5e_channel *c,
774 struct mlx5e_params *params,
775 struct mlx5e_rq_param *param,
780 err = mlx5e_alloc_rq(c, params, param, rq);
784 err = mlx5e_create_rq(rq, param);
788 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
792 if (params->rx_dim_enabled)
793 c->rq.state |= BIT(MLX5E_RQ_STATE_AM);
798 mlx5e_destroy_rq(rq);
805 static void mlx5e_activate_rq(struct mlx5e_rq *rq)
807 struct mlx5e_icosq *sq = &rq->channel->icosq;
808 u16 pi = sq->pc & sq->wq.sz_m1;
809 struct mlx5e_tx_wqe *nopwqe;
811 set_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
812 sq->db.ico_wqe[pi].opcode = MLX5_OPCODE_NOP;
813 nopwqe = mlx5e_post_nop(&sq->wq, sq->sqn, &sq->pc);
814 mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, &nopwqe->ctrl);
817 static void mlx5e_deactivate_rq(struct mlx5e_rq *rq)
819 clear_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
820 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
823 static void mlx5e_close_rq(struct mlx5e_rq *rq)
825 cancel_work_sync(&rq->dim.work);
826 mlx5e_destroy_rq(rq);
827 mlx5e_free_rx_descs(rq);
831 static void mlx5e_free_xdpsq_db(struct mlx5e_xdpsq *sq)
836 static int mlx5e_alloc_xdpsq_db(struct mlx5e_xdpsq *sq, int numa)
838 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
840 sq->db.di = kzalloc_node(sizeof(*sq->db.di) * wq_sz,
843 mlx5e_free_xdpsq_db(sq);
850 static int mlx5e_alloc_xdpsq(struct mlx5e_channel *c,
851 struct mlx5e_params *params,
852 struct mlx5e_sq_param *param,
853 struct mlx5e_xdpsq *sq)
855 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
856 struct mlx5_core_dev *mdev = c->mdev;
860 sq->mkey_be = c->mkey_be;
862 sq->uar_map = mdev->mlx5e_res.bfreg.map;
863 sq->min_inline_mode = params->tx_min_inline_mode;
865 param->wq.db_numa_node = cpu_to_node(c->cpu);
866 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq, &sq->wq_ctrl);
869 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
871 err = mlx5e_alloc_xdpsq_db(sq, cpu_to_node(c->cpu));
873 goto err_sq_wq_destroy;
878 mlx5_wq_destroy(&sq->wq_ctrl);
883 static void mlx5e_free_xdpsq(struct mlx5e_xdpsq *sq)
885 mlx5e_free_xdpsq_db(sq);
886 mlx5_wq_destroy(&sq->wq_ctrl);
889 static void mlx5e_free_icosq_db(struct mlx5e_icosq *sq)
891 kfree(sq->db.ico_wqe);
894 static int mlx5e_alloc_icosq_db(struct mlx5e_icosq *sq, int numa)
896 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
898 sq->db.ico_wqe = kzalloc_node(sizeof(*sq->db.ico_wqe) * wq_sz,
906 static int mlx5e_alloc_icosq(struct mlx5e_channel *c,
907 struct mlx5e_sq_param *param,
908 struct mlx5e_icosq *sq)
910 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
911 struct mlx5_core_dev *mdev = c->mdev;
915 sq->uar_map = mdev->mlx5e_res.bfreg.map;
917 param->wq.db_numa_node = cpu_to_node(c->cpu);
918 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq, &sq->wq_ctrl);
921 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
923 err = mlx5e_alloc_icosq_db(sq, cpu_to_node(c->cpu));
925 goto err_sq_wq_destroy;
927 sq->edge = (sq->wq.sz_m1 + 1) - MLX5E_ICOSQ_MAX_WQEBBS;
932 mlx5_wq_destroy(&sq->wq_ctrl);
937 static void mlx5e_free_icosq(struct mlx5e_icosq *sq)
939 mlx5e_free_icosq_db(sq);
940 mlx5_wq_destroy(&sq->wq_ctrl);
943 static void mlx5e_free_txqsq_db(struct mlx5e_txqsq *sq)
945 kfree(sq->db.wqe_info);
946 kfree(sq->db.dma_fifo);
949 static int mlx5e_alloc_txqsq_db(struct mlx5e_txqsq *sq, int numa)
951 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
952 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
954 sq->db.dma_fifo = kzalloc_node(df_sz * sizeof(*sq->db.dma_fifo),
956 sq->db.wqe_info = kzalloc_node(wq_sz * sizeof(*sq->db.wqe_info),
958 if (!sq->db.dma_fifo || !sq->db.wqe_info) {
959 mlx5e_free_txqsq_db(sq);
963 sq->dma_fifo_mask = df_sz - 1;
968 static void mlx5e_sq_recover(struct work_struct *work);
969 static int mlx5e_alloc_txqsq(struct mlx5e_channel *c,
971 struct mlx5e_params *params,
972 struct mlx5e_sq_param *param,
973 struct mlx5e_txqsq *sq)
975 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
976 struct mlx5_core_dev *mdev = c->mdev;
980 sq->tstamp = c->tstamp;
981 sq->clock = &mdev->clock;
982 sq->mkey_be = c->mkey_be;
985 sq->uar_map = mdev->mlx5e_res.bfreg.map;
986 sq->min_inline_mode = params->tx_min_inline_mode;
987 INIT_WORK(&sq->recover.recover_work, mlx5e_sq_recover);
988 if (MLX5_IPSEC_DEV(c->priv->mdev))
989 set_bit(MLX5E_SQ_STATE_IPSEC, &sq->state);
991 param->wq.db_numa_node = cpu_to_node(c->cpu);
992 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq, &sq->wq_ctrl);
995 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
997 err = mlx5e_alloc_txqsq_db(sq, cpu_to_node(c->cpu));
999 goto err_sq_wq_destroy;
1001 sq->edge = (sq->wq.sz_m1 + 1) - MLX5_SEND_WQE_MAX_WQEBBS;
1006 mlx5_wq_destroy(&sq->wq_ctrl);
1011 static void mlx5e_free_txqsq(struct mlx5e_txqsq *sq)
1013 mlx5e_free_txqsq_db(sq);
1014 mlx5_wq_destroy(&sq->wq_ctrl);
1017 struct mlx5e_create_sq_param {
1018 struct mlx5_wq_ctrl *wq_ctrl;
1025 static int mlx5e_create_sq(struct mlx5_core_dev *mdev,
1026 struct mlx5e_sq_param *param,
1027 struct mlx5e_create_sq_param *csp,
1036 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1037 sizeof(u64) * csp->wq_ctrl->buf.npages;
1038 in = kvzalloc(inlen, GFP_KERNEL);
1042 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1043 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1045 memcpy(sqc, param->sqc, sizeof(param->sqc));
1046 MLX5_SET(sqc, sqc, tis_lst_sz, csp->tis_lst_sz);
1047 MLX5_SET(sqc, sqc, tis_num_0, csp->tisn);
1048 MLX5_SET(sqc, sqc, cqn, csp->cqn);
1050 if (MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5_CAP_INLINE_MODE_VPORT_CONTEXT)
1051 MLX5_SET(sqc, sqc, min_wqe_inline_mode, csp->min_inline_mode);
1053 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1054 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1056 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1057 MLX5_SET(wq, wq, uar_page, mdev->mlx5e_res.bfreg.index);
1058 MLX5_SET(wq, wq, log_wq_pg_sz, csp->wq_ctrl->buf.page_shift -
1059 MLX5_ADAPTER_PAGE_SHIFT);
1060 MLX5_SET64(wq, wq, dbr_addr, csp->wq_ctrl->db.dma);
1062 mlx5_fill_page_array(&csp->wq_ctrl->buf, (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
1064 err = mlx5_core_create_sq(mdev, in, inlen, sqn);
1071 struct mlx5e_modify_sq_param {
1078 static int mlx5e_modify_sq(struct mlx5_core_dev *mdev, u32 sqn,
1079 struct mlx5e_modify_sq_param *p)
1086 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1087 in = kvzalloc(inlen, GFP_KERNEL);
1091 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1093 MLX5_SET(modify_sq_in, in, sq_state, p->curr_state);
1094 MLX5_SET(sqc, sqc, state, p->next_state);
1095 if (p->rl_update && p->next_state == MLX5_SQC_STATE_RDY) {
1096 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
1097 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, p->rl_index);
1100 err = mlx5_core_modify_sq(mdev, sqn, in, inlen);
1107 static void mlx5e_destroy_sq(struct mlx5_core_dev *mdev, u32 sqn)
1109 mlx5_core_destroy_sq(mdev, sqn);
1112 static int mlx5e_create_sq_rdy(struct mlx5_core_dev *mdev,
1113 struct mlx5e_sq_param *param,
1114 struct mlx5e_create_sq_param *csp,
1117 struct mlx5e_modify_sq_param msp = {0};
1120 err = mlx5e_create_sq(mdev, param, csp, sqn);
1124 msp.curr_state = MLX5_SQC_STATE_RST;
1125 msp.next_state = MLX5_SQC_STATE_RDY;
1126 err = mlx5e_modify_sq(mdev, *sqn, &msp);
1128 mlx5e_destroy_sq(mdev, *sqn);
1133 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1134 struct mlx5e_txqsq *sq, u32 rate);
1136 static int mlx5e_open_txqsq(struct mlx5e_channel *c,
1139 struct mlx5e_params *params,
1140 struct mlx5e_sq_param *param,
1141 struct mlx5e_txqsq *sq)
1143 struct mlx5e_create_sq_param csp = {};
1147 err = mlx5e_alloc_txqsq(c, txq_ix, params, param, sq);
1153 csp.cqn = sq->cq.mcq.cqn;
1154 csp.wq_ctrl = &sq->wq_ctrl;
1155 csp.min_inline_mode = sq->min_inline_mode;
1156 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1158 goto err_free_txqsq;
1160 tx_rate = c->priv->tx_rates[sq->txq_ix];
1162 mlx5e_set_sq_maxrate(c->netdev, sq, tx_rate);
1167 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1168 mlx5e_free_txqsq(sq);
1173 static void mlx5e_reset_txqsq_cc_pc(struct mlx5e_txqsq *sq)
1175 WARN_ONCE(sq->cc != sq->pc,
1176 "SQ 0x%x: cc (0x%x) != pc (0x%x)\n",
1177 sq->sqn, sq->cc, sq->pc);
1179 sq->dma_fifo_cc = 0;
1183 static void mlx5e_activate_txqsq(struct mlx5e_txqsq *sq)
1185 sq->txq = netdev_get_tx_queue(sq->channel->netdev, sq->txq_ix);
1186 clear_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state);
1187 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1188 netdev_tx_reset_queue(sq->txq);
1189 netif_tx_start_queue(sq->txq);
1192 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
1194 __netif_tx_lock_bh(txq);
1195 netif_tx_stop_queue(txq);
1196 __netif_tx_unlock_bh(txq);
1199 static void mlx5e_deactivate_txqsq(struct mlx5e_txqsq *sq)
1201 struct mlx5e_channel *c = sq->channel;
1203 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1204 /* prevent netif_tx_wake_queue */
1205 napi_synchronize(&c->napi);
1207 netif_tx_disable_queue(sq->txq);
1209 /* last doorbell out, godspeed .. */
1210 if (mlx5e_wqc_has_room_for(&sq->wq, sq->cc, sq->pc, 1)) {
1211 struct mlx5e_tx_wqe *nop;
1213 sq->db.wqe_info[(sq->pc & sq->wq.sz_m1)].skb = NULL;
1214 nop = mlx5e_post_nop(&sq->wq, sq->sqn, &sq->pc);
1215 mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, &nop->ctrl);
1219 static void mlx5e_close_txqsq(struct mlx5e_txqsq *sq)
1221 struct mlx5e_channel *c = sq->channel;
1222 struct mlx5_core_dev *mdev = c->mdev;
1223 struct mlx5_rate_limit rl = {0};
1225 mlx5e_destroy_sq(mdev, sq->sqn);
1226 if (sq->rate_limit) {
1227 rl.rate = sq->rate_limit;
1228 mlx5_rl_remove_rate(mdev, &rl);
1230 mlx5e_free_txqsq_descs(sq);
1231 mlx5e_free_txqsq(sq);
1234 static int mlx5e_wait_for_sq_flush(struct mlx5e_txqsq *sq)
1236 unsigned long exp_time = jiffies + msecs_to_jiffies(2000);
1238 while (time_before(jiffies, exp_time)) {
1239 if (sq->cc == sq->pc)
1245 netdev_err(sq->channel->netdev,
1246 "Wait for SQ 0x%x flush timeout (sq cc = 0x%x, sq pc = 0x%x)\n",
1247 sq->sqn, sq->cc, sq->pc);
1252 static int mlx5e_sq_to_ready(struct mlx5e_txqsq *sq, int curr_state)
1254 struct mlx5_core_dev *mdev = sq->channel->mdev;
1255 struct net_device *dev = sq->channel->netdev;
1256 struct mlx5e_modify_sq_param msp = {0};
1259 msp.curr_state = curr_state;
1260 msp.next_state = MLX5_SQC_STATE_RST;
1262 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1264 netdev_err(dev, "Failed to move sq 0x%x to reset\n", sq->sqn);
1268 memset(&msp, 0, sizeof(msp));
1269 msp.curr_state = MLX5_SQC_STATE_RST;
1270 msp.next_state = MLX5_SQC_STATE_RDY;
1272 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1274 netdev_err(dev, "Failed to move sq 0x%x to ready\n", sq->sqn);
1281 static void mlx5e_sq_recover(struct work_struct *work)
1283 struct mlx5e_txqsq_recover *recover =
1284 container_of(work, struct mlx5e_txqsq_recover,
1286 struct mlx5e_txqsq *sq = container_of(recover, struct mlx5e_txqsq,
1288 struct mlx5_core_dev *mdev = sq->channel->mdev;
1289 struct net_device *dev = sq->channel->netdev;
1293 err = mlx5_core_query_sq_state(mdev, sq->sqn, &state);
1295 netdev_err(dev, "Failed to query SQ 0x%x state. err = %d\n",
1300 if (state != MLX5_RQC_STATE_ERR) {
1301 netdev_err(dev, "SQ 0x%x not in ERROR state\n", sq->sqn);
1305 netif_tx_disable_queue(sq->txq);
1307 if (mlx5e_wait_for_sq_flush(sq))
1310 /* If the interval between two consecutive recovers per SQ is too
1311 * short, don't recover to avoid infinite loop of ERR_CQE -> recover.
1312 * If we reached this state, there is probably a bug that needs to be
1313 * fixed. let's keep the queue close and let tx timeout cleanup.
1315 if (jiffies_to_msecs(jiffies - recover->last_recover) <
1316 MLX5E_SQ_RECOVER_MIN_INTERVAL) {
1317 netdev_err(dev, "Recover SQ 0x%x canceled, too many error CQEs\n",
1322 /* At this point, no new packets will arrive from the stack as TXQ is
1323 * marked with QUEUE_STATE_DRV_XOFF. In addition, NAPI cleared all
1324 * pending WQEs. SQ can safely reset the SQ.
1326 if (mlx5e_sq_to_ready(sq, state))
1329 mlx5e_reset_txqsq_cc_pc(sq);
1330 sq->stats.recover++;
1331 recover->last_recover = jiffies;
1332 mlx5e_activate_txqsq(sq);
1335 static int mlx5e_open_icosq(struct mlx5e_channel *c,
1336 struct mlx5e_params *params,
1337 struct mlx5e_sq_param *param,
1338 struct mlx5e_icosq *sq)
1340 struct mlx5e_create_sq_param csp = {};
1343 err = mlx5e_alloc_icosq(c, param, sq);
1347 csp.cqn = sq->cq.mcq.cqn;
1348 csp.wq_ctrl = &sq->wq_ctrl;
1349 csp.min_inline_mode = params->tx_min_inline_mode;
1350 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1351 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1353 goto err_free_icosq;
1358 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1359 mlx5e_free_icosq(sq);
1364 static void mlx5e_close_icosq(struct mlx5e_icosq *sq)
1366 struct mlx5e_channel *c = sq->channel;
1368 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1369 napi_synchronize(&c->napi);
1371 mlx5e_destroy_sq(c->mdev, sq->sqn);
1372 mlx5e_free_icosq(sq);
1375 static int mlx5e_open_xdpsq(struct mlx5e_channel *c,
1376 struct mlx5e_params *params,
1377 struct mlx5e_sq_param *param,
1378 struct mlx5e_xdpsq *sq)
1380 unsigned int ds_cnt = MLX5E_XDP_TX_DS_COUNT;
1381 struct mlx5e_create_sq_param csp = {};
1382 unsigned int inline_hdr_sz = 0;
1386 err = mlx5e_alloc_xdpsq(c, params, param, sq);
1391 csp.tisn = c->priv->tisn[0]; /* tc = 0 */
1392 csp.cqn = sq->cq.mcq.cqn;
1393 csp.wq_ctrl = &sq->wq_ctrl;
1394 csp.min_inline_mode = sq->min_inline_mode;
1395 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1396 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1398 goto err_free_xdpsq;
1400 if (sq->min_inline_mode != MLX5_INLINE_MODE_NONE) {
1401 inline_hdr_sz = MLX5E_XDP_MIN_INLINE;
1405 /* Pre initialize fixed WQE fields */
1406 for (i = 0; i < mlx5_wq_cyc_get_size(&sq->wq); i++) {
1407 struct mlx5e_tx_wqe *wqe = mlx5_wq_cyc_get_wqe(&sq->wq, i);
1408 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
1409 struct mlx5_wqe_eth_seg *eseg = &wqe->eth;
1410 struct mlx5_wqe_data_seg *dseg;
1412 cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
1413 eseg->inline_hdr.sz = cpu_to_be16(inline_hdr_sz);
1415 dseg = (struct mlx5_wqe_data_seg *)cseg + (ds_cnt - 1);
1416 dseg->lkey = sq->mkey_be;
1422 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1423 mlx5e_free_xdpsq(sq);
1428 static void mlx5e_close_xdpsq(struct mlx5e_xdpsq *sq)
1430 struct mlx5e_channel *c = sq->channel;
1432 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1433 napi_synchronize(&c->napi);
1435 mlx5e_destroy_sq(c->mdev, sq->sqn);
1436 mlx5e_free_xdpsq_descs(sq);
1437 mlx5e_free_xdpsq(sq);
1440 static int mlx5e_alloc_cq_common(struct mlx5_core_dev *mdev,
1441 struct mlx5e_cq_param *param,
1442 struct mlx5e_cq *cq)
1444 struct mlx5_core_cq *mcq = &cq->mcq;
1450 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1455 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1458 mcq->set_ci_db = cq->wq_ctrl.db.db;
1459 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1460 *mcq->set_ci_db = 0;
1462 mcq->vector = param->eq_ix;
1463 mcq->comp = mlx5e_completion_event;
1464 mcq->event = mlx5e_cq_error_event;
1467 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1468 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1478 static int mlx5e_alloc_cq(struct mlx5e_channel *c,
1479 struct mlx5e_cq_param *param,
1480 struct mlx5e_cq *cq)
1482 struct mlx5_core_dev *mdev = c->priv->mdev;
1485 param->wq.buf_numa_node = cpu_to_node(c->cpu);
1486 param->wq.db_numa_node = cpu_to_node(c->cpu);
1487 param->eq_ix = c->ix;
1489 err = mlx5e_alloc_cq_common(mdev, param, cq);
1491 cq->napi = &c->napi;
1497 static void mlx5e_free_cq(struct mlx5e_cq *cq)
1499 mlx5_cqwq_destroy(&cq->wq_ctrl);
1502 static int mlx5e_create_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
1504 struct mlx5_core_dev *mdev = cq->mdev;
1505 struct mlx5_core_cq *mcq = &cq->mcq;
1510 unsigned int irqn_not_used;
1514 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1515 sizeof(u64) * cq->wq_ctrl.frag_buf.npages;
1516 in = kvzalloc(inlen, GFP_KERNEL);
1520 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1522 memcpy(cqc, param->cqc, sizeof(param->cqc));
1524 mlx5_fill_page_frag_array(&cq->wq_ctrl.frag_buf,
1525 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
1527 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
1529 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
1530 MLX5_SET(cqc, cqc, c_eqn, eqn);
1531 MLX5_SET(cqc, cqc, uar_page, mdev->priv.uar->index);
1532 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.frag_buf.page_shift -
1533 MLX5_ADAPTER_PAGE_SHIFT);
1534 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1536 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
1548 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
1550 mlx5_core_destroy_cq(cq->mdev, &cq->mcq);
1553 static int mlx5e_open_cq(struct mlx5e_channel *c,
1554 struct net_dim_cq_moder moder,
1555 struct mlx5e_cq_param *param,
1556 struct mlx5e_cq *cq)
1558 struct mlx5_core_dev *mdev = c->mdev;
1561 err = mlx5e_alloc_cq(c, param, cq);
1565 err = mlx5e_create_cq(cq, param);
1569 if (MLX5_CAP_GEN(mdev, cq_moderation))
1570 mlx5_core_modify_cq_moderation(mdev, &cq->mcq, moder.usec, moder.pkts);
1579 static void mlx5e_close_cq(struct mlx5e_cq *cq)
1581 mlx5e_destroy_cq(cq);
1585 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
1587 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
1590 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1591 struct mlx5e_params *params,
1592 struct mlx5e_channel_param *cparam)
1597 for (tc = 0; tc < c->num_tc; tc++) {
1598 err = mlx5e_open_cq(c, params->tx_cq_moderation,
1599 &cparam->tx_cq, &c->sq[tc].cq);
1601 goto err_close_tx_cqs;
1607 for (tc--; tc >= 0; tc--)
1608 mlx5e_close_cq(&c->sq[tc].cq);
1613 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1617 for (tc = 0; tc < c->num_tc; tc++)
1618 mlx5e_close_cq(&c->sq[tc].cq);
1621 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1622 struct mlx5e_params *params,
1623 struct mlx5e_channel_param *cparam)
1628 for (tc = 0; tc < params->num_tc; tc++) {
1629 int txq_ix = c->ix + tc * params->num_channels;
1631 err = mlx5e_open_txqsq(c, c->priv->tisn[tc], txq_ix,
1632 params, &cparam->sq, &c->sq[tc]);
1640 for (tc--; tc >= 0; tc--)
1641 mlx5e_close_txqsq(&c->sq[tc]);
1646 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1650 for (tc = 0; tc < c->num_tc; tc++)
1651 mlx5e_close_txqsq(&c->sq[tc]);
1654 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1655 struct mlx5e_txqsq *sq, u32 rate)
1657 struct mlx5e_priv *priv = netdev_priv(dev);
1658 struct mlx5_core_dev *mdev = priv->mdev;
1659 struct mlx5e_modify_sq_param msp = {0};
1660 struct mlx5_rate_limit rl = {0};
1664 if (rate == sq->rate_limit)
1668 if (sq->rate_limit) {
1669 rl.rate = sq->rate_limit;
1670 /* remove current rl index to free space to next ones */
1671 mlx5_rl_remove_rate(mdev, &rl);
1678 err = mlx5_rl_add_rate(mdev, &rl_index, &rl);
1680 netdev_err(dev, "Failed configuring rate %u: %d\n",
1686 msp.curr_state = MLX5_SQC_STATE_RDY;
1687 msp.next_state = MLX5_SQC_STATE_RDY;
1688 msp.rl_index = rl_index;
1689 msp.rl_update = true;
1690 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1692 netdev_err(dev, "Failed configuring rate %u: %d\n",
1694 /* remove the rate from the table */
1696 mlx5_rl_remove_rate(mdev, &rl);
1700 sq->rate_limit = rate;
1704 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1706 struct mlx5e_priv *priv = netdev_priv(dev);
1707 struct mlx5_core_dev *mdev = priv->mdev;
1708 struct mlx5e_txqsq *sq = priv->txq2sq[index];
1711 if (!mlx5_rl_is_supported(mdev)) {
1712 netdev_err(dev, "Rate limiting is not supported on this device\n");
1716 /* rate is given in Mb/sec, HW config is in Kb/sec */
1719 /* Check whether rate in valid range, 0 is always valid */
1720 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1721 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1725 mutex_lock(&priv->state_lock);
1726 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1727 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1729 priv->tx_rates[index] = rate;
1730 mutex_unlock(&priv->state_lock);
1735 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1736 struct mlx5e_params *params,
1737 struct mlx5e_channel_param *cparam,
1738 struct mlx5e_channel **cp)
1740 struct net_dim_cq_moder icocq_moder = {0, 0};
1741 struct net_device *netdev = priv->netdev;
1742 int cpu = mlx5e_get_cpu(priv, ix);
1743 struct mlx5e_channel *c;
1748 c = kzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1753 c->mdev = priv->mdev;
1754 c->tstamp = &priv->tstamp;
1757 c->pdev = &priv->mdev->pdev->dev;
1758 c->netdev = priv->netdev;
1759 c->mkey_be = cpu_to_be32(priv->mdev->mlx5e_res.mkey.key);
1760 c->num_tc = params->num_tc;
1761 c->xdp = !!params->xdp_prog;
1763 mlx5_vector2eqn(priv->mdev, ix, &eqn, &irq);
1764 c->irq_desc = irq_to_desc(irq);
1766 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1768 err = mlx5e_open_cq(c, icocq_moder, &cparam->icosq_cq, &c->icosq.cq);
1772 err = mlx5e_open_tx_cqs(c, params, cparam);
1774 goto err_close_icosq_cq;
1776 err = mlx5e_open_cq(c, params->rx_cq_moderation, &cparam->rx_cq, &c->rq.cq);
1778 goto err_close_tx_cqs;
1780 /* XDP SQ CQ params are same as normal TXQ sq CQ params */
1781 err = c->xdp ? mlx5e_open_cq(c, params->tx_cq_moderation,
1782 &cparam->tx_cq, &c->rq.xdpsq.cq) : 0;
1784 goto err_close_rx_cq;
1786 napi_enable(&c->napi);
1788 err = mlx5e_open_icosq(c, params, &cparam->icosq, &c->icosq);
1790 goto err_disable_napi;
1792 err = mlx5e_open_sqs(c, params, cparam);
1794 goto err_close_icosq;
1796 err = c->xdp ? mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, &c->rq.xdpsq) : 0;
1800 err = mlx5e_open_rq(c, params, &cparam->rq, &c->rq);
1802 goto err_close_xdp_sq;
1809 mlx5e_close_xdpsq(&c->rq.xdpsq);
1815 mlx5e_close_icosq(&c->icosq);
1818 napi_disable(&c->napi);
1820 mlx5e_close_cq(&c->rq.xdpsq.cq);
1823 mlx5e_close_cq(&c->rq.cq);
1826 mlx5e_close_tx_cqs(c);
1829 mlx5e_close_cq(&c->icosq.cq);
1832 netif_napi_del(&c->napi);
1838 static void mlx5e_activate_channel(struct mlx5e_channel *c)
1842 for (tc = 0; tc < c->num_tc; tc++)
1843 mlx5e_activate_txqsq(&c->sq[tc]);
1844 mlx5e_activate_rq(&c->rq);
1845 netif_set_xps_queue(c->netdev, get_cpu_mask(c->cpu), c->ix);
1848 static void mlx5e_deactivate_channel(struct mlx5e_channel *c)
1852 mlx5e_deactivate_rq(&c->rq);
1853 for (tc = 0; tc < c->num_tc; tc++)
1854 mlx5e_deactivate_txqsq(&c->sq[tc]);
1857 static void mlx5e_close_channel(struct mlx5e_channel *c)
1859 mlx5e_close_rq(&c->rq);
1861 mlx5e_close_xdpsq(&c->rq.xdpsq);
1863 mlx5e_close_icosq(&c->icosq);
1864 napi_disable(&c->napi);
1866 mlx5e_close_cq(&c->rq.xdpsq.cq);
1867 mlx5e_close_cq(&c->rq.cq);
1868 mlx5e_close_tx_cqs(c);
1869 mlx5e_close_cq(&c->icosq.cq);
1870 netif_napi_del(&c->napi);
1875 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
1876 struct mlx5e_params *params,
1877 struct mlx5e_rq_param *param)
1879 struct mlx5_core_dev *mdev = priv->mdev;
1880 void *rqc = param->rqc;
1881 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1883 switch (params->rq_wq_type) {
1884 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1885 MLX5_SET(wq, wq, log_wqe_num_of_strides,
1886 mlx5e_mpwqe_get_log_num_strides(mdev, params) -
1887 MLX5_MPWQE_LOG_NUM_STRIDES_BASE);
1888 MLX5_SET(wq, wq, log_wqe_stride_size,
1889 mlx5e_mpwqe_get_log_stride_size(mdev, params) -
1890 MLX5_MPWQE_LOG_STRIDE_SZ_BASE);
1891 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
1892 MLX5_SET(wq, wq, log_wq_sz, mlx5e_mpwqe_get_log_rq_size(params));
1894 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1895 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1896 MLX5_SET(wq, wq, log_wq_sz, params->log_rq_mtu_frames);
1899 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1900 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1901 MLX5_SET(wq, wq, pd, mdev->mlx5e_res.pdn);
1902 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
1903 MLX5_SET(rqc, rqc, vsd, params->vlan_strip_disable);
1904 MLX5_SET(rqc, rqc, scatter_fcs, params->scatter_fcs_en);
1906 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
1907 param->wq.linear = 1;
1910 static void mlx5e_build_drop_rq_param(struct mlx5e_priv *priv,
1911 struct mlx5e_rq_param *param)
1913 struct mlx5_core_dev *mdev = priv->mdev;
1914 void *rqc = param->rqc;
1915 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1917 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1918 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1919 MLX5_SET(rqc, rqc, counter_set_id, priv->drop_rq_q_counter);
1921 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
1924 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
1925 struct mlx5e_sq_param *param)
1927 void *sqc = param->sqc;
1928 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1930 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1931 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
1933 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1936 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
1937 struct mlx5e_params *params,
1938 struct mlx5e_sq_param *param)
1940 void *sqc = param->sqc;
1941 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1943 mlx5e_build_sq_param_common(priv, param);
1944 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
1945 MLX5_SET(sqc, sqc, allow_swp, !!MLX5_IPSEC_DEV(priv->mdev));
1948 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1949 struct mlx5e_cq_param *param)
1951 void *cqc = param->cqc;
1953 MLX5_SET(cqc, cqc, uar_page, priv->mdev->priv.uar->index);
1956 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1957 struct mlx5e_params *params,
1958 struct mlx5e_cq_param *param)
1960 struct mlx5_core_dev *mdev = priv->mdev;
1961 void *cqc = param->cqc;
1964 switch (params->rq_wq_type) {
1965 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1966 log_cq_size = mlx5e_mpwqe_get_log_rq_size(params) +
1967 mlx5e_mpwqe_get_log_num_strides(mdev, params);
1969 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1970 log_cq_size = params->log_rq_mtu_frames;
1973 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
1974 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
1975 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
1976 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
1979 mlx5e_build_common_cq_param(priv, param);
1980 param->cq_period_mode = params->rx_cq_moderation.cq_period_mode;
1983 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
1984 struct mlx5e_params *params,
1985 struct mlx5e_cq_param *param)
1987 void *cqc = param->cqc;
1989 MLX5_SET(cqc, cqc, log_cq_size, params->log_sq_size);
1991 mlx5e_build_common_cq_param(priv, param);
1992 param->cq_period_mode = params->tx_cq_moderation.cq_period_mode;
1995 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
1997 struct mlx5e_cq_param *param)
1999 void *cqc = param->cqc;
2001 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
2003 mlx5e_build_common_cq_param(priv, param);
2005 param->cq_period_mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2008 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
2010 struct mlx5e_sq_param *param)
2012 void *sqc = param->sqc;
2013 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2015 mlx5e_build_sq_param_common(priv, param);
2017 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
2018 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
2021 static void mlx5e_build_xdpsq_param(struct mlx5e_priv *priv,
2022 struct mlx5e_params *params,
2023 struct mlx5e_sq_param *param)
2025 void *sqc = param->sqc;
2026 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2028 mlx5e_build_sq_param_common(priv, param);
2029 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2032 static void mlx5e_build_channel_param(struct mlx5e_priv *priv,
2033 struct mlx5e_params *params,
2034 struct mlx5e_channel_param *cparam)
2036 u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
2038 mlx5e_build_rq_param(priv, params, &cparam->rq);
2039 mlx5e_build_sq_param(priv, params, &cparam->sq);
2040 mlx5e_build_xdpsq_param(priv, params, &cparam->xdp_sq);
2041 mlx5e_build_icosq_param(priv, icosq_log_wq_sz, &cparam->icosq);
2042 mlx5e_build_rx_cq_param(priv, params, &cparam->rx_cq);
2043 mlx5e_build_tx_cq_param(priv, params, &cparam->tx_cq);
2044 mlx5e_build_ico_cq_param(priv, icosq_log_wq_sz, &cparam->icosq_cq);
2047 int mlx5e_open_channels(struct mlx5e_priv *priv,
2048 struct mlx5e_channels *chs)
2050 struct mlx5e_channel_param *cparam;
2054 chs->num = chs->params.num_channels;
2056 chs->c = kcalloc(chs->num, sizeof(struct mlx5e_channel *), GFP_KERNEL);
2057 cparam = kzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
2058 if (!chs->c || !cparam)
2061 mlx5e_build_channel_param(priv, &chs->params, cparam);
2062 for (i = 0; i < chs->num; i++) {
2063 err = mlx5e_open_channel(priv, i, &chs->params, cparam, &chs->c[i]);
2065 goto err_close_channels;
2072 for (i--; i >= 0; i--)
2073 mlx5e_close_channel(chs->c[i]);
2082 static void mlx5e_activate_channels(struct mlx5e_channels *chs)
2086 for (i = 0; i < chs->num; i++)
2087 mlx5e_activate_channel(chs->c[i]);
2090 static int mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels *chs)
2095 for (i = 0; i < chs->num; i++) {
2096 err = mlx5e_wait_for_min_rx_wqes(&chs->c[i]->rq);
2104 static void mlx5e_deactivate_channels(struct mlx5e_channels *chs)
2108 for (i = 0; i < chs->num; i++)
2109 mlx5e_deactivate_channel(chs->c[i]);
2112 void mlx5e_close_channels(struct mlx5e_channels *chs)
2116 for (i = 0; i < chs->num; i++)
2117 mlx5e_close_channel(chs->c[i]);
2124 mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, struct mlx5e_rqt *rqt)
2126 struct mlx5_core_dev *mdev = priv->mdev;
2133 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
2134 in = kvzalloc(inlen, GFP_KERNEL);
2138 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
2140 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2141 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
2143 for (i = 0; i < sz; i++)
2144 MLX5_SET(rqtc, rqtc, rq_num[i], priv->drop_rq.rqn);
2146 err = mlx5_core_create_rqt(mdev, in, inlen, &rqt->rqtn);
2148 rqt->enabled = true;
2154 void mlx5e_destroy_rqt(struct mlx5e_priv *priv, struct mlx5e_rqt *rqt)
2156 rqt->enabled = false;
2157 mlx5_core_destroy_rqt(priv->mdev, rqt->rqtn);
2160 int mlx5e_create_indirect_rqt(struct mlx5e_priv *priv)
2162 struct mlx5e_rqt *rqt = &priv->indir_rqt;
2165 err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, rqt);
2167 mlx5_core_warn(priv->mdev, "create indirect rqts failed, %d\n", err);
2171 int mlx5e_create_direct_rqts(struct mlx5e_priv *priv)
2173 struct mlx5e_rqt *rqt;
2177 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2178 rqt = &priv->direct_tir[ix].rqt;
2179 err = mlx5e_create_rqt(priv, 1 /*size */, rqt);
2181 goto err_destroy_rqts;
2187 mlx5_core_warn(priv->mdev, "create direct rqts failed, %d\n", err);
2188 for (ix--; ix >= 0; ix--)
2189 mlx5e_destroy_rqt(priv, &priv->direct_tir[ix].rqt);
2194 void mlx5e_destroy_direct_rqts(struct mlx5e_priv *priv)
2198 for (i = 0; i < priv->profile->max_nch(priv->mdev); i++)
2199 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
2202 static int mlx5e_rx_hash_fn(int hfunc)
2204 return (hfunc == ETH_RSS_HASH_TOP) ?
2205 MLX5_RX_HASH_FN_TOEPLITZ :
2206 MLX5_RX_HASH_FN_INVERTED_XOR8;
2209 int mlx5e_bits_invert(unsigned long a, int size)
2214 for (i = 0; i < size; i++)
2215 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
2220 static void mlx5e_fill_rqt_rqns(struct mlx5e_priv *priv, int sz,
2221 struct mlx5e_redirect_rqt_param rrp, void *rqtc)
2225 for (i = 0; i < sz; i++) {
2231 if (rrp.rss.hfunc == ETH_RSS_HASH_XOR)
2232 ix = mlx5e_bits_invert(i, ilog2(sz));
2234 ix = priv->channels.params.indirection_rqt[ix];
2235 rqn = rrp.rss.channels->c[ix]->rq.rqn;
2239 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
2243 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz,
2244 struct mlx5e_redirect_rqt_param rrp)
2246 struct mlx5_core_dev *mdev = priv->mdev;
2252 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
2253 in = kvzalloc(inlen, GFP_KERNEL);
2257 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
2259 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2260 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
2261 mlx5e_fill_rqt_rqns(priv, sz, rrp, rqtc);
2262 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
2268 static u32 mlx5e_get_direct_rqn(struct mlx5e_priv *priv, int ix,
2269 struct mlx5e_redirect_rqt_param rrp)
2274 if (ix >= rrp.rss.channels->num)
2275 return priv->drop_rq.rqn;
2277 return rrp.rss.channels->c[ix]->rq.rqn;
2280 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv,
2281 struct mlx5e_redirect_rqt_param rrp)
2286 if (priv->indir_rqt.enabled) {
2288 rqtn = priv->indir_rqt.rqtn;
2289 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, rrp);
2292 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2293 struct mlx5e_redirect_rqt_param direct_rrp = {
2296 .rqn = mlx5e_get_direct_rqn(priv, ix, rrp)
2300 /* Direct RQ Tables */
2301 if (!priv->direct_tir[ix].rqt.enabled)
2304 rqtn = priv->direct_tir[ix].rqt.rqtn;
2305 mlx5e_redirect_rqt(priv, rqtn, 1, direct_rrp);
2309 static void mlx5e_redirect_rqts_to_channels(struct mlx5e_priv *priv,
2310 struct mlx5e_channels *chs)
2312 struct mlx5e_redirect_rqt_param rrp = {
2317 .hfunc = chs->params.rss_hfunc,
2322 mlx5e_redirect_rqts(priv, rrp);
2325 static void mlx5e_redirect_rqts_to_drop(struct mlx5e_priv *priv)
2327 struct mlx5e_redirect_rqt_param drop_rrp = {
2330 .rqn = priv->drop_rq.rqn,
2334 mlx5e_redirect_rqts(priv, drop_rrp);
2337 static void mlx5e_build_tir_ctx_lro(struct mlx5e_params *params, void *tirc)
2339 if (!params->lro_en)
2342 #define ROUGH_MAX_L2_L3_HDR_SZ 256
2344 MLX5_SET(tirc, tirc, lro_enable_mask,
2345 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2346 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
2347 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
2348 (params->lro_wqe_sz - ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
2349 MLX5_SET(tirc, tirc, lro_timeout_period_usecs, params->lro_timeout);
2352 void mlx5e_build_indir_tir_ctx_hash(struct mlx5e_params *params,
2353 enum mlx5e_traffic_types tt,
2354 void *tirc, bool inner)
2356 void *hfso = inner ? MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner) :
2357 MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2359 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
2360 MLX5_HASH_FIELD_SEL_DST_IP)
2362 #define MLX5_HASH_IP_L4PORTS (MLX5_HASH_FIELD_SEL_SRC_IP |\
2363 MLX5_HASH_FIELD_SEL_DST_IP |\
2364 MLX5_HASH_FIELD_SEL_L4_SPORT |\
2365 MLX5_HASH_FIELD_SEL_L4_DPORT)
2367 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
2368 MLX5_HASH_FIELD_SEL_DST_IP |\
2369 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2371 MLX5_SET(tirc, tirc, rx_hash_fn, mlx5e_rx_hash_fn(params->rss_hfunc));
2372 if (params->rss_hfunc == ETH_RSS_HASH_TOP) {
2373 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
2374 rx_hash_toeplitz_key);
2375 size_t len = MLX5_FLD_SZ_BYTES(tirc,
2376 rx_hash_toeplitz_key);
2378 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
2379 memcpy(rss_key, params->toeplitz_hash_key, len);
2383 case MLX5E_TT_IPV4_TCP:
2384 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2385 MLX5_L3_PROT_TYPE_IPV4);
2386 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2387 MLX5_L4_PROT_TYPE_TCP);
2388 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2389 MLX5_HASH_IP_L4PORTS);
2392 case MLX5E_TT_IPV6_TCP:
2393 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2394 MLX5_L3_PROT_TYPE_IPV6);
2395 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2396 MLX5_L4_PROT_TYPE_TCP);
2397 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2398 MLX5_HASH_IP_L4PORTS);
2401 case MLX5E_TT_IPV4_UDP:
2402 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2403 MLX5_L3_PROT_TYPE_IPV4);
2404 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2405 MLX5_L4_PROT_TYPE_UDP);
2406 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2407 MLX5_HASH_IP_L4PORTS);
2410 case MLX5E_TT_IPV6_UDP:
2411 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2412 MLX5_L3_PROT_TYPE_IPV6);
2413 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2414 MLX5_L4_PROT_TYPE_UDP);
2415 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2416 MLX5_HASH_IP_L4PORTS);
2419 case MLX5E_TT_IPV4_IPSEC_AH:
2420 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2421 MLX5_L3_PROT_TYPE_IPV4);
2422 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2423 MLX5_HASH_IP_IPSEC_SPI);
2426 case MLX5E_TT_IPV6_IPSEC_AH:
2427 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2428 MLX5_L3_PROT_TYPE_IPV6);
2429 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2430 MLX5_HASH_IP_IPSEC_SPI);
2433 case MLX5E_TT_IPV4_IPSEC_ESP:
2434 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2435 MLX5_L3_PROT_TYPE_IPV4);
2436 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2437 MLX5_HASH_IP_IPSEC_SPI);
2440 case MLX5E_TT_IPV6_IPSEC_ESP:
2441 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2442 MLX5_L3_PROT_TYPE_IPV6);
2443 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2444 MLX5_HASH_IP_IPSEC_SPI);
2448 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2449 MLX5_L3_PROT_TYPE_IPV4);
2450 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2455 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2456 MLX5_L3_PROT_TYPE_IPV6);
2457 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2461 WARN_ONCE(true, "%s: bad traffic type!\n", __func__);
2465 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
2467 struct mlx5_core_dev *mdev = priv->mdev;
2476 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
2477 in = kvzalloc(inlen, GFP_KERNEL);
2481 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
2482 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2484 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2486 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2487 err = mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in,
2493 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2494 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
2506 static void mlx5e_build_inner_indir_tir_ctx(struct mlx5e_priv *priv,
2507 enum mlx5e_traffic_types tt,
2510 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2512 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2514 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2515 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
2516 MLX5_SET(tirc, tirc, tunneled_offload_en, 0x1);
2518 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, true);
2521 static int mlx5e_set_mtu(struct mlx5_core_dev *mdev,
2522 struct mlx5e_params *params, u16 mtu)
2524 u16 hw_mtu = MLX5E_SW2HW_MTU(params, mtu);
2527 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
2531 /* Update vport context MTU */
2532 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
2536 static void mlx5e_query_mtu(struct mlx5_core_dev *mdev,
2537 struct mlx5e_params *params, u16 *mtu)
2542 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
2543 if (err || !hw_mtu) /* fallback to port oper mtu */
2544 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
2546 *mtu = MLX5E_HW2SW_MTU(params, hw_mtu);
2549 static int mlx5e_set_dev_port_mtu(struct mlx5e_priv *priv)
2551 struct mlx5e_params *params = &priv->channels.params;
2552 struct net_device *netdev = priv->netdev;
2553 struct mlx5_core_dev *mdev = priv->mdev;
2557 err = mlx5e_set_mtu(mdev, params, params->sw_mtu);
2561 mlx5e_query_mtu(mdev, params, &mtu);
2562 if (mtu != params->sw_mtu)
2563 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
2564 __func__, mtu, params->sw_mtu);
2566 params->sw_mtu = mtu;
2570 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
2572 struct mlx5e_priv *priv = netdev_priv(netdev);
2573 int nch = priv->channels.params.num_channels;
2574 int ntc = priv->channels.params.num_tc;
2577 netdev_reset_tc(netdev);
2582 netdev_set_num_tc(netdev, ntc);
2584 /* Map netdev TCs to offset 0
2585 * We have our own UP to TXQ mapping for QoS
2587 for (tc = 0; tc < ntc; tc++)
2588 netdev_set_tc_queue(netdev, tc, nch, 0);
2591 static void mlx5e_build_channels_tx_maps(struct mlx5e_priv *priv)
2593 struct mlx5e_channel *c;
2594 struct mlx5e_txqsq *sq;
2597 for (i = 0; i < priv->channels.num; i++)
2598 for (tc = 0; tc < priv->profile->max_tc; tc++)
2599 priv->channel_tc2txq[i][tc] = i + tc * priv->channels.num;
2601 for (i = 0; i < priv->channels.num; i++) {
2602 c = priv->channels.c[i];
2603 for (tc = 0; tc < c->num_tc; tc++) {
2605 priv->txq2sq[sq->txq_ix] = sq;
2610 void mlx5e_activate_priv_channels(struct mlx5e_priv *priv)
2612 int num_txqs = priv->channels.num * priv->channels.params.num_tc;
2613 struct net_device *netdev = priv->netdev;
2615 mlx5e_netdev_set_tcs(netdev);
2616 netif_set_real_num_tx_queues(netdev, num_txqs);
2617 netif_set_real_num_rx_queues(netdev, priv->channels.num);
2619 mlx5e_build_channels_tx_maps(priv);
2620 mlx5e_activate_channels(&priv->channels);
2621 netif_tx_start_all_queues(priv->netdev);
2623 if (MLX5_VPORT_MANAGER(priv->mdev))
2624 mlx5e_add_sqs_fwd_rules(priv);
2626 mlx5e_wait_channels_min_rx_wqes(&priv->channels);
2627 mlx5e_redirect_rqts_to_channels(priv, &priv->channels);
2630 void mlx5e_deactivate_priv_channels(struct mlx5e_priv *priv)
2632 mlx5e_redirect_rqts_to_drop(priv);
2634 if (MLX5_VPORT_MANAGER(priv->mdev))
2635 mlx5e_remove_sqs_fwd_rules(priv);
2637 /* FIXME: This is a W/A only for tx timeout watch dog false alarm when
2638 * polling for inactive tx queues.
2640 netif_tx_stop_all_queues(priv->netdev);
2641 netif_tx_disable(priv->netdev);
2642 mlx5e_deactivate_channels(&priv->channels);
2645 void mlx5e_switch_priv_channels(struct mlx5e_priv *priv,
2646 struct mlx5e_channels *new_chs,
2647 mlx5e_fp_hw_modify hw_modify)
2649 struct net_device *netdev = priv->netdev;
2652 new_num_txqs = new_chs->num * new_chs->params.num_tc;
2654 carrier_ok = netif_carrier_ok(netdev);
2655 netif_carrier_off(netdev);
2657 if (new_num_txqs < netdev->real_num_tx_queues)
2658 netif_set_real_num_tx_queues(netdev, new_num_txqs);
2660 mlx5e_deactivate_priv_channels(priv);
2661 mlx5e_close_channels(&priv->channels);
2663 priv->channels = *new_chs;
2665 /* New channels are ready to roll, modify HW settings if needed */
2669 mlx5e_refresh_tirs(priv, false);
2670 mlx5e_activate_priv_channels(priv);
2672 /* return carrier back if needed */
2674 netif_carrier_on(netdev);
2677 void mlx5e_timestamp_init(struct mlx5e_priv *priv)
2679 priv->tstamp.tx_type = HWTSTAMP_TX_OFF;
2680 priv->tstamp.rx_filter = HWTSTAMP_FILTER_NONE;
2683 int mlx5e_open_locked(struct net_device *netdev)
2685 struct mlx5e_priv *priv = netdev_priv(netdev);
2688 set_bit(MLX5E_STATE_OPENED, &priv->state);
2690 err = mlx5e_open_channels(priv, &priv->channels);
2692 goto err_clear_state_opened_flag;
2694 mlx5e_refresh_tirs(priv, false);
2695 mlx5e_activate_priv_channels(priv);
2696 if (priv->profile->update_carrier)
2697 priv->profile->update_carrier(priv);
2699 if (priv->profile->update_stats)
2700 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
2704 err_clear_state_opened_flag:
2705 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2709 int mlx5e_open(struct net_device *netdev)
2711 struct mlx5e_priv *priv = netdev_priv(netdev);
2714 mutex_lock(&priv->state_lock);
2715 err = mlx5e_open_locked(netdev);
2717 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_UP);
2718 mutex_unlock(&priv->state_lock);
2720 if (mlx5e_vxlan_allowed(priv->mdev))
2721 udp_tunnel_get_rx_info(netdev);
2726 int mlx5e_close_locked(struct net_device *netdev)
2728 struct mlx5e_priv *priv = netdev_priv(netdev);
2730 /* May already be CLOSED in case a previous configuration operation
2731 * (e.g RX/TX queue size change) that involves close&open failed.
2733 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2736 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2738 netif_carrier_off(priv->netdev);
2739 mlx5e_deactivate_priv_channels(priv);
2740 mlx5e_close_channels(&priv->channels);
2745 int mlx5e_close(struct net_device *netdev)
2747 struct mlx5e_priv *priv = netdev_priv(netdev);
2750 if (!netif_device_present(netdev))
2753 mutex_lock(&priv->state_lock);
2754 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_DOWN);
2755 err = mlx5e_close_locked(netdev);
2756 mutex_unlock(&priv->state_lock);
2761 static int mlx5e_alloc_drop_rq(struct mlx5_core_dev *mdev,
2762 struct mlx5e_rq *rq,
2763 struct mlx5e_rq_param *param)
2765 void *rqc = param->rqc;
2766 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
2769 param->wq.db_numa_node = param->wq.buf_numa_node;
2771 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
2776 /* Mark as unused given "Drop-RQ" packets never reach XDP */
2777 xdp_rxq_info_unused(&rq->xdp_rxq);
2784 static int mlx5e_alloc_drop_cq(struct mlx5_core_dev *mdev,
2785 struct mlx5e_cq *cq,
2786 struct mlx5e_cq_param *param)
2788 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
2789 param->wq.db_numa_node = dev_to_node(&mdev->pdev->dev);
2791 return mlx5e_alloc_cq_common(mdev, param, cq);
2794 static int mlx5e_open_drop_rq(struct mlx5e_priv *priv,
2795 struct mlx5e_rq *drop_rq)
2797 struct mlx5_core_dev *mdev = priv->mdev;
2798 struct mlx5e_cq_param cq_param = {};
2799 struct mlx5e_rq_param rq_param = {};
2800 struct mlx5e_cq *cq = &drop_rq->cq;
2803 mlx5e_build_drop_rq_param(priv, &rq_param);
2805 err = mlx5e_alloc_drop_cq(mdev, cq, &cq_param);
2809 err = mlx5e_create_cq(cq, &cq_param);
2813 err = mlx5e_alloc_drop_rq(mdev, drop_rq, &rq_param);
2815 goto err_destroy_cq;
2817 err = mlx5e_create_rq(drop_rq, &rq_param);
2821 err = mlx5e_modify_rq_state(drop_rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
2823 mlx5_core_warn(priv->mdev, "modify_rq_state failed, rx_if_down_packets won't be counted %d\n", err);
2828 mlx5e_free_rq(drop_rq);
2831 mlx5e_destroy_cq(cq);
2839 static void mlx5e_close_drop_rq(struct mlx5e_rq *drop_rq)
2841 mlx5e_destroy_rq(drop_rq);
2842 mlx5e_free_rq(drop_rq);
2843 mlx5e_destroy_cq(&drop_rq->cq);
2844 mlx5e_free_cq(&drop_rq->cq);
2847 int mlx5e_create_tis(struct mlx5_core_dev *mdev, int tc,
2848 u32 underlay_qpn, u32 *tisn)
2850 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
2851 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
2853 MLX5_SET(tisc, tisc, prio, tc << 1);
2854 MLX5_SET(tisc, tisc, underlay_qpn, underlay_qpn);
2855 MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.td.tdn);
2857 if (mlx5_lag_is_lacp_owner(mdev))
2858 MLX5_SET(tisc, tisc, strict_lag_tx_port_affinity, 1);
2860 return mlx5_core_create_tis(mdev, in, sizeof(in), tisn);
2863 void mlx5e_destroy_tis(struct mlx5_core_dev *mdev, u32 tisn)
2865 mlx5_core_destroy_tis(mdev, tisn);
2868 int mlx5e_create_tises(struct mlx5e_priv *priv)
2873 for (tc = 0; tc < priv->profile->max_tc; tc++) {
2874 err = mlx5e_create_tis(priv->mdev, tc, 0, &priv->tisn[tc]);
2876 goto err_close_tises;
2882 for (tc--; tc >= 0; tc--)
2883 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
2888 void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
2892 for (tc = 0; tc < priv->profile->max_tc; tc++)
2893 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
2896 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv,
2897 enum mlx5e_traffic_types tt,
2900 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2902 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2904 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2905 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
2906 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, false);
2909 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 rqtn, u32 *tirc)
2911 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2913 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2915 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2916 MLX5_SET(tirc, tirc, indirect_table, rqtn);
2917 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
2920 int mlx5e_create_indirect_tirs(struct mlx5e_priv *priv)
2922 struct mlx5e_tir *tir;
2930 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2931 in = kvzalloc(inlen, GFP_KERNEL);
2935 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2936 memset(in, 0, inlen);
2937 tir = &priv->indir_tir[tt];
2938 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2939 mlx5e_build_indir_tir_ctx(priv, tt, tirc);
2940 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
2942 mlx5_core_warn(priv->mdev, "create indirect tirs failed, %d\n", err);
2943 goto err_destroy_inner_tirs;
2947 if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
2950 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
2951 memset(in, 0, inlen);
2952 tir = &priv->inner_indir_tir[i];
2953 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2954 mlx5e_build_inner_indir_tir_ctx(priv, i, tirc);
2955 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
2957 mlx5_core_warn(priv->mdev, "create inner indirect tirs failed, %d\n", err);
2958 goto err_destroy_inner_tirs;
2967 err_destroy_inner_tirs:
2968 for (i--; i >= 0; i--)
2969 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
2971 for (tt--; tt >= 0; tt--)
2972 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[tt]);
2979 int mlx5e_create_direct_tirs(struct mlx5e_priv *priv)
2981 int nch = priv->profile->max_nch(priv->mdev);
2982 struct mlx5e_tir *tir;
2989 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2990 in = kvzalloc(inlen, GFP_KERNEL);
2994 for (ix = 0; ix < nch; ix++) {
2995 memset(in, 0, inlen);
2996 tir = &priv->direct_tir[ix];
2997 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2998 mlx5e_build_direct_tir_ctx(priv, priv->direct_tir[ix].rqt.rqtn, tirc);
2999 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3001 goto err_destroy_ch_tirs;
3008 err_destroy_ch_tirs:
3009 mlx5_core_warn(priv->mdev, "create direct tirs failed, %d\n", err);
3010 for (ix--; ix >= 0; ix--)
3011 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[ix]);
3018 void mlx5e_destroy_indirect_tirs(struct mlx5e_priv *priv)
3022 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3023 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[i]);
3025 if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
3028 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3029 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3032 void mlx5e_destroy_direct_tirs(struct mlx5e_priv *priv)
3034 int nch = priv->profile->max_nch(priv->mdev);
3037 for (i = 0; i < nch; i++)
3038 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[i]);
3041 static int mlx5e_modify_channels_scatter_fcs(struct mlx5e_channels *chs, bool enable)
3046 for (i = 0; i < chs->num; i++) {
3047 err = mlx5e_modify_rq_scatter_fcs(&chs->c[i]->rq, enable);
3055 static int mlx5e_modify_channels_vsd(struct mlx5e_channels *chs, bool vsd)
3060 for (i = 0; i < chs->num; i++) {
3061 err = mlx5e_modify_rq_vsd(&chs->c[i]->rq, vsd);
3069 static int mlx5e_setup_tc_mqprio(struct net_device *netdev,
3070 struct tc_mqprio_qopt *mqprio)
3072 struct mlx5e_priv *priv = netdev_priv(netdev);
3073 struct mlx5e_channels new_channels = {};
3074 u8 tc = mqprio->num_tc;
3077 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
3079 if (tc && tc != MLX5E_MAX_NUM_TC)
3082 mutex_lock(&priv->state_lock);
3084 new_channels.params = priv->channels.params;
3085 new_channels.params.num_tc = tc ? tc : 1;
3087 if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
3088 priv->channels.params = new_channels.params;
3092 err = mlx5e_open_channels(priv, &new_channels);
3096 mlx5e_switch_priv_channels(priv, &new_channels, NULL);
3098 mutex_unlock(&priv->state_lock);
3102 #ifdef CONFIG_MLX5_ESWITCH
3103 static int mlx5e_setup_tc_cls_flower(struct mlx5e_priv *priv,
3104 struct tc_cls_flower_offload *cls_flower)
3106 switch (cls_flower->command) {
3107 case TC_CLSFLOWER_REPLACE:
3108 return mlx5e_configure_flower(priv, cls_flower);
3109 case TC_CLSFLOWER_DESTROY:
3110 return mlx5e_delete_flower(priv, cls_flower);
3111 case TC_CLSFLOWER_STATS:
3112 return mlx5e_stats_flower(priv, cls_flower);
3118 int mlx5e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
3121 struct mlx5e_priv *priv = cb_priv;
3123 if (!tc_cls_can_offload_and_chain0(priv->netdev, type_data))
3127 case TC_SETUP_CLSFLOWER:
3128 return mlx5e_setup_tc_cls_flower(priv, type_data);
3134 static int mlx5e_setup_tc_block(struct net_device *dev,
3135 struct tc_block_offload *f)
3137 struct mlx5e_priv *priv = netdev_priv(dev);
3139 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
3142 switch (f->command) {
3144 return tcf_block_cb_register(f->block, mlx5e_setup_tc_block_cb,
3146 case TC_BLOCK_UNBIND:
3147 tcf_block_cb_unregister(f->block, mlx5e_setup_tc_block_cb,
3156 static int mlx5e_setup_tc(struct net_device *dev, enum tc_setup_type type,
3160 #ifdef CONFIG_MLX5_ESWITCH
3161 case TC_SETUP_BLOCK:
3162 return mlx5e_setup_tc_block(dev, type_data);
3164 case TC_SETUP_QDISC_MQPRIO:
3165 return mlx5e_setup_tc_mqprio(dev, type_data);
3172 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
3174 struct mlx5e_priv *priv = netdev_priv(dev);
3175 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
3176 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
3177 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
3179 if (mlx5e_is_uplink_rep(priv)) {
3180 stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
3181 stats->rx_bytes = PPORT_802_3_GET(pstats, a_octets_received_ok);
3182 stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
3183 stats->tx_bytes = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
3185 stats->rx_packets = sstats->rx_packets;
3186 stats->rx_bytes = sstats->rx_bytes;
3187 stats->tx_packets = sstats->tx_packets;
3188 stats->tx_bytes = sstats->tx_bytes;
3189 stats->tx_dropped = sstats->tx_queue_dropped;
3192 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
3194 stats->rx_length_errors =
3195 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
3196 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
3197 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
3198 stats->rx_crc_errors =
3199 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
3200 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
3201 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
3202 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
3203 stats->rx_frame_errors;
3204 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
3206 /* vport multicast also counts packets that are dropped due to steering
3207 * or rx out of buffer
3210 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
3213 static void mlx5e_set_rx_mode(struct net_device *dev)
3215 struct mlx5e_priv *priv = netdev_priv(dev);
3217 queue_work(priv->wq, &priv->set_rx_mode_work);
3220 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
3222 struct mlx5e_priv *priv = netdev_priv(netdev);
3223 struct sockaddr *saddr = addr;
3225 if (!is_valid_ether_addr(saddr->sa_data))
3226 return -EADDRNOTAVAIL;
3228 netif_addr_lock_bh(netdev);
3229 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
3230 netif_addr_unlock_bh(netdev);
3232 queue_work(priv->wq, &priv->set_rx_mode_work);
3237 #define MLX5E_SET_FEATURE(features, feature, enable) \
3240 *features |= feature; \
3242 *features &= ~feature; \
3245 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
3247 static int set_feature_lro(struct net_device *netdev, bool enable)
3249 struct mlx5e_priv *priv = netdev_priv(netdev);
3250 struct mlx5_core_dev *mdev = priv->mdev;
3251 struct mlx5e_channels new_channels = {};
3252 struct mlx5e_params *old_params;
3256 mutex_lock(&priv->state_lock);
3258 old_params = &priv->channels.params;
3259 reset = test_bit(MLX5E_STATE_OPENED, &priv->state);
3261 new_channels.params = *old_params;
3262 new_channels.params.lro_en = enable;
3264 if (old_params->rq_wq_type != MLX5_WQ_TYPE_LINKED_LIST) {
3265 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, old_params) ==
3266 mlx5e_rx_mpwqe_is_linear_skb(mdev, &new_channels.params))
3271 *old_params = new_channels.params;
3272 err = mlx5e_modify_tirs_lro(priv);
3276 err = mlx5e_open_channels(priv, &new_channels);
3280 mlx5e_switch_priv_channels(priv, &new_channels, mlx5e_modify_tirs_lro);
3282 mutex_unlock(&priv->state_lock);
3286 static int set_feature_cvlan_filter(struct net_device *netdev, bool enable)
3288 struct mlx5e_priv *priv = netdev_priv(netdev);
3291 mlx5e_enable_cvlan_filter(priv);
3293 mlx5e_disable_cvlan_filter(priv);
3298 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
3300 struct mlx5e_priv *priv = netdev_priv(netdev);
3302 if (!enable && mlx5e_tc_num_filters(priv)) {
3304 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
3311 static int set_feature_rx_all(struct net_device *netdev, bool enable)
3313 struct mlx5e_priv *priv = netdev_priv(netdev);
3314 struct mlx5_core_dev *mdev = priv->mdev;
3316 return mlx5_set_port_fcs(mdev, !enable);
3319 static int set_feature_rx_fcs(struct net_device *netdev, bool enable)
3321 struct mlx5e_priv *priv = netdev_priv(netdev);
3324 mutex_lock(&priv->state_lock);
3326 priv->channels.params.scatter_fcs_en = enable;
3327 err = mlx5e_modify_channels_scatter_fcs(&priv->channels, enable);
3329 priv->channels.params.scatter_fcs_en = !enable;
3331 mutex_unlock(&priv->state_lock);
3336 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
3338 struct mlx5e_priv *priv = netdev_priv(netdev);
3341 mutex_lock(&priv->state_lock);
3343 priv->channels.params.vlan_strip_disable = !enable;
3344 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3347 err = mlx5e_modify_channels_vsd(&priv->channels, !enable);
3349 priv->channels.params.vlan_strip_disable = enable;
3352 mutex_unlock(&priv->state_lock);
3357 #ifdef CONFIG_RFS_ACCEL
3358 static int set_feature_arfs(struct net_device *netdev, bool enable)
3360 struct mlx5e_priv *priv = netdev_priv(netdev);
3364 err = mlx5e_arfs_enable(priv);
3366 err = mlx5e_arfs_disable(priv);
3372 static int mlx5e_handle_feature(struct net_device *netdev,
3373 netdev_features_t *features,
3374 netdev_features_t wanted_features,
3375 netdev_features_t feature,
3376 mlx5e_feature_handler feature_handler)
3378 netdev_features_t changes = wanted_features ^ netdev->features;
3379 bool enable = !!(wanted_features & feature);
3382 if (!(changes & feature))
3385 err = feature_handler(netdev, enable);
3387 netdev_err(netdev, "%s feature %pNF failed, err %d\n",
3388 enable ? "Enable" : "Disable", &feature, err);
3392 MLX5E_SET_FEATURE(features, feature, enable);
3396 static int mlx5e_set_features(struct net_device *netdev,
3397 netdev_features_t features)
3399 netdev_features_t oper_features = netdev->features;
3402 #define MLX5E_HANDLE_FEATURE(feature, handler) \
3403 mlx5e_handle_feature(netdev, &oper_features, features, feature, handler)
3405 err |= MLX5E_HANDLE_FEATURE(NETIF_F_LRO, set_feature_lro);
3406 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_FILTER,
3407 set_feature_cvlan_filter);
3408 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_TC, set_feature_tc_num_filters);
3409 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXALL, set_feature_rx_all);
3410 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXFCS, set_feature_rx_fcs);
3411 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_RX, set_feature_rx_vlan);
3412 #ifdef CONFIG_RFS_ACCEL
3413 err |= MLX5E_HANDLE_FEATURE(NETIF_F_NTUPLE, set_feature_arfs);
3417 netdev->features = oper_features;
3424 static netdev_features_t mlx5e_fix_features(struct net_device *netdev,
3425 netdev_features_t features)
3427 struct mlx5e_priv *priv = netdev_priv(netdev);
3429 mutex_lock(&priv->state_lock);
3430 if (!bitmap_empty(priv->fs.vlan.active_svlans, VLAN_N_VID)) {
3431 /* HW strips the outer C-tag header, this is a problem
3432 * for S-tag traffic.
3434 features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3435 if (!priv->channels.params.vlan_strip_disable)
3436 netdev_warn(netdev, "Dropping C-tag vlan stripping offload due to S-tag vlan\n");
3438 mutex_unlock(&priv->state_lock);
3443 static int mlx5e_change_mtu(struct net_device *netdev, int new_mtu)
3445 struct mlx5e_priv *priv = netdev_priv(netdev);
3446 struct mlx5e_channels new_channels = {};
3447 struct mlx5e_params *params;
3451 mutex_lock(&priv->state_lock);
3453 params = &priv->channels.params;
3455 reset = !params->lro_en;
3456 reset = reset && test_bit(MLX5E_STATE_OPENED, &priv->state);
3458 new_channels.params = *params;
3459 new_channels.params.sw_mtu = new_mtu;
3461 if (params->rq_wq_type != MLX5_WQ_TYPE_LINKED_LIST) {
3462 u8 ppw_old = mlx5e_mpwqe_log_pkts_per_wqe(params);
3463 u8 ppw_new = mlx5e_mpwqe_log_pkts_per_wqe(&new_channels.params);
3465 reset = reset && (ppw_old != ppw_new);
3469 params->sw_mtu = new_mtu;
3470 mlx5e_set_dev_port_mtu(priv);
3471 netdev->mtu = params->sw_mtu;
3475 err = mlx5e_open_channels(priv, &new_channels);
3479 mlx5e_switch_priv_channels(priv, &new_channels, mlx5e_set_dev_port_mtu);
3480 netdev->mtu = new_channels.params.sw_mtu;
3483 mutex_unlock(&priv->state_lock);
3487 int mlx5e_hwstamp_set(struct mlx5e_priv *priv, struct ifreq *ifr)
3489 struct hwtstamp_config config;
3492 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
3495 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
3498 /* TX HW timestamp */
3499 switch (config.tx_type) {
3500 case HWTSTAMP_TX_OFF:
3501 case HWTSTAMP_TX_ON:
3507 mutex_lock(&priv->state_lock);
3508 /* RX HW timestamp */
3509 switch (config.rx_filter) {
3510 case HWTSTAMP_FILTER_NONE:
3511 /* Reset CQE compression to Admin default */
3512 mlx5e_modify_rx_cqe_compression_locked(priv, priv->channels.params.rx_cqe_compress_def);
3514 case HWTSTAMP_FILTER_ALL:
3515 case HWTSTAMP_FILTER_SOME:
3516 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3517 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3518 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3519 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3520 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3521 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3522 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3523 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3524 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3525 case HWTSTAMP_FILTER_PTP_V2_EVENT:
3526 case HWTSTAMP_FILTER_PTP_V2_SYNC:
3527 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3528 case HWTSTAMP_FILTER_NTP_ALL:
3529 /* Disable CQE compression */
3530 netdev_warn(priv->netdev, "Disabling cqe compression");
3531 err = mlx5e_modify_rx_cqe_compression_locked(priv, false);
3533 netdev_err(priv->netdev, "Failed disabling cqe compression err=%d\n", err);
3534 mutex_unlock(&priv->state_lock);
3537 config.rx_filter = HWTSTAMP_FILTER_ALL;
3540 mutex_unlock(&priv->state_lock);
3544 memcpy(&priv->tstamp, &config, sizeof(config));
3545 mutex_unlock(&priv->state_lock);
3547 return copy_to_user(ifr->ifr_data, &config,
3548 sizeof(config)) ? -EFAULT : 0;
3551 int mlx5e_hwstamp_get(struct mlx5e_priv *priv, struct ifreq *ifr)
3553 struct hwtstamp_config *cfg = &priv->tstamp;
3555 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
3558 return copy_to_user(ifr->ifr_data, cfg, sizeof(*cfg)) ? -EFAULT : 0;
3561 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3563 struct mlx5e_priv *priv = netdev_priv(dev);
3567 return mlx5e_hwstamp_set(priv, ifr);
3569 return mlx5e_hwstamp_get(priv, ifr);
3575 #ifdef CONFIG_MLX5_ESWITCH
3576 static int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
3578 struct mlx5e_priv *priv = netdev_priv(dev);
3579 struct mlx5_core_dev *mdev = priv->mdev;
3581 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
3584 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
3587 struct mlx5e_priv *priv = netdev_priv(dev);
3588 struct mlx5_core_dev *mdev = priv->mdev;
3590 if (vlan_proto != htons(ETH_P_8021Q))
3591 return -EPROTONOSUPPORT;
3593 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
3597 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
3599 struct mlx5e_priv *priv = netdev_priv(dev);
3600 struct mlx5_core_dev *mdev = priv->mdev;
3602 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
3605 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
3607 struct mlx5e_priv *priv = netdev_priv(dev);
3608 struct mlx5_core_dev *mdev = priv->mdev;
3610 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
3613 static int mlx5e_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
3616 struct mlx5e_priv *priv = netdev_priv(dev);
3617 struct mlx5_core_dev *mdev = priv->mdev;
3619 return mlx5_eswitch_set_vport_rate(mdev->priv.eswitch, vf + 1,
3620 max_tx_rate, min_tx_rate);
3623 static int mlx5_vport_link2ifla(u8 esw_link)
3626 case MLX5_ESW_VPORT_ADMIN_STATE_DOWN:
3627 return IFLA_VF_LINK_STATE_DISABLE;
3628 case MLX5_ESW_VPORT_ADMIN_STATE_UP:
3629 return IFLA_VF_LINK_STATE_ENABLE;
3631 return IFLA_VF_LINK_STATE_AUTO;
3634 static int mlx5_ifla_link2vport(u8 ifla_link)
3636 switch (ifla_link) {
3637 case IFLA_VF_LINK_STATE_DISABLE:
3638 return MLX5_ESW_VPORT_ADMIN_STATE_DOWN;
3639 case IFLA_VF_LINK_STATE_ENABLE:
3640 return MLX5_ESW_VPORT_ADMIN_STATE_UP;
3642 return MLX5_ESW_VPORT_ADMIN_STATE_AUTO;
3645 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
3648 struct mlx5e_priv *priv = netdev_priv(dev);
3649 struct mlx5_core_dev *mdev = priv->mdev;
3651 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
3652 mlx5_ifla_link2vport(link_state));
3655 static int mlx5e_get_vf_config(struct net_device *dev,
3656 int vf, struct ifla_vf_info *ivi)
3658 struct mlx5e_priv *priv = netdev_priv(dev);
3659 struct mlx5_core_dev *mdev = priv->mdev;
3662 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
3665 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
3669 static int mlx5e_get_vf_stats(struct net_device *dev,
3670 int vf, struct ifla_vf_stats *vf_stats)
3672 struct mlx5e_priv *priv = netdev_priv(dev);
3673 struct mlx5_core_dev *mdev = priv->mdev;
3675 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
3680 static void mlx5e_add_vxlan_port(struct net_device *netdev,
3681 struct udp_tunnel_info *ti)
3683 struct mlx5e_priv *priv = netdev_priv(netdev);
3685 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3688 if (!mlx5e_vxlan_allowed(priv->mdev))
3691 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 1);
3694 static void mlx5e_del_vxlan_port(struct net_device *netdev,
3695 struct udp_tunnel_info *ti)
3697 struct mlx5e_priv *priv = netdev_priv(netdev);
3699 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3702 if (!mlx5e_vxlan_allowed(priv->mdev))
3705 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 0);
3708 static netdev_features_t mlx5e_tunnel_features_check(struct mlx5e_priv *priv,
3709 struct sk_buff *skb,
3710 netdev_features_t features)
3712 unsigned int offset = 0;
3713 struct udphdr *udph;
3717 switch (vlan_get_protocol(skb)) {
3718 case htons(ETH_P_IP):
3719 proto = ip_hdr(skb)->protocol;
3721 case htons(ETH_P_IPV6):
3722 proto = ipv6_find_hdr(skb, &offset, -1, NULL, NULL);
3732 udph = udp_hdr(skb);
3733 port = be16_to_cpu(udph->dest);
3735 /* Verify if UDP port is being offloaded by HW */
3736 if (mlx5e_vxlan_lookup_port(priv, port))
3741 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
3742 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3745 static netdev_features_t mlx5e_features_check(struct sk_buff *skb,
3746 struct net_device *netdev,
3747 netdev_features_t features)
3749 struct mlx5e_priv *priv = netdev_priv(netdev);
3751 features = vlan_features_check(skb, features);
3752 features = vxlan_features_check(skb, features);
3754 #ifdef CONFIG_MLX5_EN_IPSEC
3755 if (mlx5e_ipsec_feature_check(skb, netdev, features))
3759 /* Validate if the tunneled packet is being offloaded by HW */
3760 if (skb->encapsulation &&
3761 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
3762 return mlx5e_tunnel_features_check(priv, skb, features);
3767 static bool mlx5e_tx_timeout_eq_recover(struct net_device *dev,
3768 struct mlx5e_txqsq *sq)
3770 struct mlx5_eq *eq = sq->cq.mcq.eq;
3773 netdev_err(dev, "EQ 0x%x: Cons = 0x%x, irqn = 0x%x\n",
3774 eq->eqn, eq->cons_index, eq->irqn);
3776 eqe_count = mlx5_eq_poll_irq_disabled(eq);
3780 netdev_err(dev, "Recover %d eqes on EQ 0x%x\n", eqe_count, eq->eqn);
3781 sq->channel->stats.eq_rearm++;
3785 static void mlx5e_tx_timeout_work(struct work_struct *work)
3787 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
3789 struct net_device *dev = priv->netdev;
3790 bool reopen_channels = false;
3794 mutex_lock(&priv->state_lock);
3796 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3799 for (i = 0; i < priv->channels.num * priv->channels.params.num_tc; i++) {
3800 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, i);
3801 struct mlx5e_txqsq *sq = priv->txq2sq[i];
3803 if (!netif_xmit_stopped(dev_queue))
3807 "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",
3808 i, sq->sqn, sq->cq.mcq.cqn, sq->cc, sq->pc,
3809 jiffies_to_usecs(jiffies - dev_queue->trans_start));
3811 /* If we recover a lost interrupt, most likely TX timeout will
3812 * be resolved, skip reopening channels
3814 if (!mlx5e_tx_timeout_eq_recover(dev, sq)) {
3815 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
3816 reopen_channels = true;
3820 if (!reopen_channels)
3823 mlx5e_close_locked(dev);
3824 err = mlx5e_open_locked(dev);
3826 netdev_err(priv->netdev,
3827 "mlx5e_open_locked failed recovering from a tx_timeout, err(%d).\n",
3831 mutex_unlock(&priv->state_lock);
3835 static void mlx5e_tx_timeout(struct net_device *dev)
3837 struct mlx5e_priv *priv = netdev_priv(dev);
3839 netdev_err(dev, "TX timeout detected\n");
3840 queue_work(priv->wq, &priv->tx_timeout_work);
3843 static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog)
3845 struct mlx5e_priv *priv = netdev_priv(netdev);
3846 struct bpf_prog *old_prog;
3848 bool reset, was_opened;
3851 mutex_lock(&priv->state_lock);
3853 if ((netdev->features & NETIF_F_LRO) && prog) {
3854 netdev_warn(netdev, "can't set XDP while LRO is on, disable LRO first\n");
3859 if ((netdev->features & NETIF_F_HW_ESP) && prog) {
3860 netdev_warn(netdev, "can't set XDP with IPSec offload\n");
3865 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
3866 /* no need for full reset when exchanging programs */
3867 reset = (!priv->channels.params.xdp_prog || !prog);
3869 if (was_opened && reset)
3870 mlx5e_close_locked(netdev);
3871 if (was_opened && !reset) {
3872 /* num_channels is invariant here, so we can take the
3873 * batched reference right upfront.
3875 prog = bpf_prog_add(prog, priv->channels.num);
3877 err = PTR_ERR(prog);
3882 /* exchange programs, extra prog reference we got from caller
3883 * as long as we don't fail from this point onwards.
3885 old_prog = xchg(&priv->channels.params.xdp_prog, prog);
3887 bpf_prog_put(old_prog);
3889 if (reset) /* change RQ type according to priv->xdp_prog */
3890 mlx5e_set_rq_type(priv->mdev, &priv->channels.params);
3892 if (was_opened && reset)
3893 mlx5e_open_locked(netdev);
3895 if (!test_bit(MLX5E_STATE_OPENED, &priv->state) || reset)
3898 /* exchanging programs w/o reset, we update ref counts on behalf
3899 * of the channels RQs here.
3901 for (i = 0; i < priv->channels.num; i++) {
3902 struct mlx5e_channel *c = priv->channels.c[i];
3904 clear_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
3905 napi_synchronize(&c->napi);
3906 /* prevent mlx5e_poll_rx_cq from accessing rq->xdp_prog */
3908 old_prog = xchg(&c->rq.xdp_prog, prog);
3910 set_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
3911 /* napi_schedule in case we have missed anything */
3912 napi_schedule(&c->napi);
3915 bpf_prog_put(old_prog);
3919 mutex_unlock(&priv->state_lock);
3923 static u32 mlx5e_xdp_query(struct net_device *dev)
3925 struct mlx5e_priv *priv = netdev_priv(dev);
3926 const struct bpf_prog *xdp_prog;
3929 mutex_lock(&priv->state_lock);
3930 xdp_prog = priv->channels.params.xdp_prog;
3932 prog_id = xdp_prog->aux->id;
3933 mutex_unlock(&priv->state_lock);
3938 static int mlx5e_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3940 switch (xdp->command) {
3941 case XDP_SETUP_PROG:
3942 return mlx5e_xdp_set(dev, xdp->prog);
3943 case XDP_QUERY_PROG:
3944 xdp->prog_id = mlx5e_xdp_query(dev);
3945 xdp->prog_attached = !!xdp->prog_id;
3952 #ifdef CONFIG_NET_POLL_CONTROLLER
3953 /* Fake "interrupt" called by netpoll (eg netconsole) to send skbs without
3954 * reenabling interrupts.
3956 static void mlx5e_netpoll(struct net_device *dev)
3958 struct mlx5e_priv *priv = netdev_priv(dev);
3959 struct mlx5e_channels *chs = &priv->channels;
3963 for (i = 0; i < chs->num; i++)
3964 napi_schedule(&chs->c[i]->napi);
3968 static const struct net_device_ops mlx5e_netdev_ops = {
3969 .ndo_open = mlx5e_open,
3970 .ndo_stop = mlx5e_close,
3971 .ndo_start_xmit = mlx5e_xmit,
3972 .ndo_setup_tc = mlx5e_setup_tc,
3973 .ndo_select_queue = mlx5e_select_queue,
3974 .ndo_get_stats64 = mlx5e_get_stats,
3975 .ndo_set_rx_mode = mlx5e_set_rx_mode,
3976 .ndo_set_mac_address = mlx5e_set_mac,
3977 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
3978 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
3979 .ndo_set_features = mlx5e_set_features,
3980 .ndo_fix_features = mlx5e_fix_features,
3981 .ndo_change_mtu = mlx5e_change_mtu,
3982 .ndo_do_ioctl = mlx5e_ioctl,
3983 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
3984 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
3985 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
3986 .ndo_features_check = mlx5e_features_check,
3987 #ifdef CONFIG_RFS_ACCEL
3988 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
3990 .ndo_tx_timeout = mlx5e_tx_timeout,
3991 .ndo_bpf = mlx5e_xdp,
3992 #ifdef CONFIG_NET_POLL_CONTROLLER
3993 .ndo_poll_controller = mlx5e_netpoll,
3995 #ifdef CONFIG_MLX5_ESWITCH
3996 /* SRIOV E-Switch NDOs */
3997 .ndo_set_vf_mac = mlx5e_set_vf_mac,
3998 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
3999 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
4000 .ndo_set_vf_trust = mlx5e_set_vf_trust,
4001 .ndo_set_vf_rate = mlx5e_set_vf_rate,
4002 .ndo_get_vf_config = mlx5e_get_vf_config,
4003 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
4004 .ndo_get_vf_stats = mlx5e_get_vf_stats,
4005 .ndo_has_offload_stats = mlx5e_has_offload_stats,
4006 .ndo_get_offload_stats = mlx5e_get_offload_stats,
4010 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
4012 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
4014 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
4015 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
4016 !MLX5_CAP_ETH(mdev, csum_cap) ||
4017 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
4018 !MLX5_CAP_ETH(mdev, vlan_cap) ||
4019 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
4020 MLX5_CAP_FLOWTABLE(mdev,
4021 flow_table_properties_nic_receive.max_ft_level)
4023 mlx5_core_warn(mdev,
4024 "Not creating net device, some required device capabilities are missing\n");
4027 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
4028 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
4029 if (!MLX5_CAP_GEN(mdev, cq_moderation))
4030 mlx5_core_warn(mdev, "CQ moderation is not supported\n");
4035 void mlx5e_build_default_indir_rqt(u32 *indirection_rqt, int len,
4040 for (i = 0; i < len; i++)
4041 indirection_rqt[i] = i % num_channels;
4044 static bool slow_pci_heuristic(struct mlx5_core_dev *mdev)
4049 mlx5e_get_max_linkspeed(mdev, &link_speed);
4050 pci_bw = pcie_bandwidth_available(mdev->pdev, NULL, NULL, NULL);
4051 mlx5_core_dbg_once(mdev, "Max link speed = %d, PCI BW = %d\n",
4052 link_speed, pci_bw);
4054 #define MLX5E_SLOW_PCI_RATIO (2)
4056 return link_speed && pci_bw &&
4057 link_speed > MLX5E_SLOW_PCI_RATIO * pci_bw;
4060 void mlx5e_set_tx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4062 params->tx_cq_moderation.cq_period_mode = cq_period_mode;
4064 params->tx_cq_moderation.pkts =
4065 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
4066 params->tx_cq_moderation.usec =
4067 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
4069 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4070 params->tx_cq_moderation.usec =
4071 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC_FROM_CQE;
4073 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_TX_CQE_BASED_MODER,
4074 params->tx_cq_moderation.cq_period_mode ==
4075 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4078 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4080 params->rx_cq_moderation.cq_period_mode = cq_period_mode;
4082 params->rx_cq_moderation.pkts =
4083 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
4084 params->rx_cq_moderation.usec =
4085 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
4087 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4088 params->rx_cq_moderation.usec =
4089 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
4091 if (params->rx_dim_enabled) {
4092 switch (cq_period_mode) {
4093 case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
4094 params->rx_cq_moderation =
4095 net_dim_get_def_profile(NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE);
4097 case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
4099 params->rx_cq_moderation =
4100 net_dim_get_def_profile(NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE);
4104 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_BASED_MODER,
4105 params->rx_cq_moderation.cq_period_mode ==
4106 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4109 static u32 mlx5e_choose_lro_timeout(struct mlx5_core_dev *mdev, u32 wanted_timeout)
4113 /* The supported periods are organized in ascending order */
4114 for (i = 0; i < MLX5E_LRO_TIMEOUT_ARR_SIZE - 1; i++)
4115 if (MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]) >= wanted_timeout)
4118 return MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]);
4121 void mlx5e_build_nic_params(struct mlx5_core_dev *mdev,
4122 struct mlx5e_params *params,
4123 u16 max_channels, u16 mtu)
4125 u8 rx_cq_period_mode;
4127 params->sw_mtu = mtu;
4128 params->hard_mtu = MLX5E_ETH_HARD_MTU;
4129 params->num_channels = max_channels;
4133 params->log_sq_size = is_kdump_kernel() ?
4134 MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE :
4135 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
4137 /* set CQE compression */
4138 params->rx_cqe_compress_def = false;
4139 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
4140 MLX5_CAP_GEN(mdev, vport_group_manager))
4141 params->rx_cqe_compress_def = slow_pci_heuristic(mdev);
4143 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS, params->rx_cqe_compress_def);
4146 if (mlx5e_striding_rq_possible(mdev, params))
4147 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ,
4148 !slow_pci_heuristic(mdev));
4149 mlx5e_set_rq_type(mdev, params);
4150 mlx5e_init_rq_type_params(mdev, params);
4154 /* TODO: && MLX5_CAP_ETH(mdev, lro_cap) */
4155 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
4156 if (!mlx5e_rx_mpwqe_is_linear_skb(mdev, params))
4157 params->lro_en = !slow_pci_heuristic(mdev);
4158 params->lro_timeout = mlx5e_choose_lro_timeout(mdev, MLX5E_DEFAULT_LRO_TIMEOUT);
4160 /* CQ moderation params */
4161 rx_cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
4162 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
4163 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
4164 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4165 mlx5e_set_rx_cq_mode_params(params, rx_cq_period_mode);
4166 mlx5e_set_tx_cq_mode_params(params, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
4169 params->tx_min_inline_mode = mlx5e_params_calculate_tx_min_inline(mdev);
4172 params->rss_hfunc = ETH_RSS_HASH_XOR;
4173 netdev_rss_key_fill(params->toeplitz_hash_key, sizeof(params->toeplitz_hash_key));
4174 mlx5e_build_default_indir_rqt(params->indirection_rqt,
4175 MLX5E_INDIR_RQT_SIZE, max_channels);
4178 static void mlx5e_build_nic_netdev_priv(struct mlx5_core_dev *mdev,
4179 struct net_device *netdev,
4180 const struct mlx5e_profile *profile,
4183 struct mlx5e_priv *priv = netdev_priv(netdev);
4186 priv->netdev = netdev;
4187 priv->profile = profile;
4188 priv->ppriv = ppriv;
4189 priv->msglevel = MLX5E_MSG_LEVEL;
4191 mlx5e_build_nic_params(mdev, &priv->channels.params,
4192 profile->max_nch(mdev), netdev->mtu);
4194 mutex_init(&priv->state_lock);
4196 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
4197 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
4198 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
4199 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
4201 mlx5e_timestamp_init(priv);
4204 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
4206 struct mlx5e_priv *priv = netdev_priv(netdev);
4208 mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
4209 if (is_zero_ether_addr(netdev->dev_addr) &&
4210 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
4211 eth_hw_addr_random(netdev);
4212 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
4216 #if IS_ENABLED(CONFIG_MLX5_ESWITCH)
4217 static const struct switchdev_ops mlx5e_switchdev_ops = {
4218 .switchdev_port_attr_get = mlx5e_attr_get,
4222 static void mlx5e_build_nic_netdev(struct net_device *netdev)
4224 struct mlx5e_priv *priv = netdev_priv(netdev);
4225 struct mlx5_core_dev *mdev = priv->mdev;
4229 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
4231 netdev->netdev_ops = &mlx5e_netdev_ops;
4233 #ifdef CONFIG_MLX5_CORE_EN_DCB
4234 if (MLX5_CAP_GEN(mdev, vport_group_manager) && MLX5_CAP_GEN(mdev, qos))
4235 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
4238 netdev->watchdog_timeo = 15 * HZ;
4240 netdev->ethtool_ops = &mlx5e_ethtool_ops;
4242 netdev->vlan_features |= NETIF_F_SG;
4243 netdev->vlan_features |= NETIF_F_IP_CSUM;
4244 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
4245 netdev->vlan_features |= NETIF_F_GRO;
4246 netdev->vlan_features |= NETIF_F_TSO;
4247 netdev->vlan_features |= NETIF_F_TSO6;
4248 netdev->vlan_features |= NETIF_F_RXCSUM;
4249 netdev->vlan_features |= NETIF_F_RXHASH;
4251 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_TX;
4252 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_RX;
4254 if (!!MLX5_CAP_ETH(mdev, lro_cap))
4255 netdev->vlan_features |= NETIF_F_LRO;
4257 netdev->hw_features = netdev->vlan_features;
4258 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4259 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4260 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4261 netdev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
4263 if (mlx5e_vxlan_allowed(mdev) || MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4264 netdev->hw_features |= NETIF_F_GSO_PARTIAL;
4265 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
4266 netdev->hw_enc_features |= NETIF_F_IPV6_CSUM;
4267 netdev->hw_enc_features |= NETIF_F_TSO;
4268 netdev->hw_enc_features |= NETIF_F_TSO6;
4269 netdev->hw_enc_features |= NETIF_F_GSO_PARTIAL;
4272 if (mlx5e_vxlan_allowed(mdev)) {
4273 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
4274 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4275 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
4276 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4277 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
4280 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4281 netdev->hw_features |= NETIF_F_GSO_GRE |
4282 NETIF_F_GSO_GRE_CSUM;
4283 netdev->hw_enc_features |= NETIF_F_GSO_GRE |
4284 NETIF_F_GSO_GRE_CSUM;
4285 netdev->gso_partial_features |= NETIF_F_GSO_GRE |
4286 NETIF_F_GSO_GRE_CSUM;
4289 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
4292 netdev->hw_features |= NETIF_F_RXALL;
4294 if (MLX5_CAP_ETH(mdev, scatter_fcs))
4295 netdev->hw_features |= NETIF_F_RXFCS;
4297 netdev->features = netdev->hw_features;
4298 if (!priv->channels.params.lro_en)
4299 netdev->features &= ~NETIF_F_LRO;
4302 netdev->features &= ~NETIF_F_RXALL;
4304 if (!priv->channels.params.scatter_fcs_en)
4305 netdev->features &= ~NETIF_F_RXFCS;
4307 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
4308 if (FT_CAP(flow_modify_en) &&
4309 FT_CAP(modify_root) &&
4310 FT_CAP(identified_miss_table_mode) &&
4311 FT_CAP(flow_table_modify)) {
4312 netdev->hw_features |= NETIF_F_HW_TC;
4313 #ifdef CONFIG_RFS_ACCEL
4314 netdev->hw_features |= NETIF_F_NTUPLE;
4318 netdev->features |= NETIF_F_HIGHDMA;
4319 netdev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
4321 netdev->priv_flags |= IFF_UNICAST_FLT;
4323 mlx5e_set_netdev_dev_addr(netdev);
4325 #if IS_ENABLED(CONFIG_MLX5_ESWITCH)
4326 if (MLX5_VPORT_MANAGER(mdev))
4327 netdev->switchdev_ops = &mlx5e_switchdev_ops;
4330 mlx5e_ipsec_build_netdev(priv);
4333 static void mlx5e_create_q_counters(struct mlx5e_priv *priv)
4335 struct mlx5_core_dev *mdev = priv->mdev;
4338 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
4340 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
4341 priv->q_counter = 0;
4344 err = mlx5_core_alloc_q_counter(mdev, &priv->drop_rq_q_counter);
4346 mlx5_core_warn(mdev, "alloc drop RQ counter failed, %d\n", err);
4347 priv->drop_rq_q_counter = 0;
4351 static void mlx5e_destroy_q_counters(struct mlx5e_priv *priv)
4353 if (priv->q_counter)
4354 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
4356 if (priv->drop_rq_q_counter)
4357 mlx5_core_dealloc_q_counter(priv->mdev, priv->drop_rq_q_counter);
4360 static void mlx5e_nic_init(struct mlx5_core_dev *mdev,
4361 struct net_device *netdev,
4362 const struct mlx5e_profile *profile,
4365 struct mlx5e_priv *priv = netdev_priv(netdev);
4368 mlx5e_build_nic_netdev_priv(mdev, netdev, profile, ppriv);
4369 err = mlx5e_ipsec_init(priv);
4371 mlx5_core_err(mdev, "IPSec initialization failed, %d\n", err);
4372 mlx5e_build_nic_netdev(netdev);
4373 mlx5e_vxlan_init(priv);
4376 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
4378 mlx5e_ipsec_cleanup(priv);
4379 mlx5e_vxlan_cleanup(priv);
4382 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv)
4384 struct mlx5_core_dev *mdev = priv->mdev;
4387 err = mlx5e_create_indirect_rqt(priv);
4391 err = mlx5e_create_direct_rqts(priv);
4393 goto err_destroy_indirect_rqts;
4395 err = mlx5e_create_indirect_tirs(priv);
4397 goto err_destroy_direct_rqts;
4399 err = mlx5e_create_direct_tirs(priv);
4401 goto err_destroy_indirect_tirs;
4403 err = mlx5e_create_flow_steering(priv);
4405 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
4406 goto err_destroy_direct_tirs;
4409 err = mlx5e_tc_init(priv);
4411 goto err_destroy_flow_steering;
4415 err_destroy_flow_steering:
4416 mlx5e_destroy_flow_steering(priv);
4417 err_destroy_direct_tirs:
4418 mlx5e_destroy_direct_tirs(priv);
4419 err_destroy_indirect_tirs:
4420 mlx5e_destroy_indirect_tirs(priv);
4421 err_destroy_direct_rqts:
4422 mlx5e_destroy_direct_rqts(priv);
4423 err_destroy_indirect_rqts:
4424 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
4428 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv)
4430 mlx5e_tc_cleanup(priv);
4431 mlx5e_destroy_flow_steering(priv);
4432 mlx5e_destroy_direct_tirs(priv);
4433 mlx5e_destroy_indirect_tirs(priv);
4434 mlx5e_destroy_direct_rqts(priv);
4435 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
4438 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv)
4442 err = mlx5e_create_tises(priv);
4444 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
4448 #ifdef CONFIG_MLX5_CORE_EN_DCB
4449 mlx5e_dcbnl_initialize(priv);
4454 static void mlx5e_nic_enable(struct mlx5e_priv *priv)
4456 struct net_device *netdev = priv->netdev;
4457 struct mlx5_core_dev *mdev = priv->mdev;
4460 mlx5e_init_l2_addr(priv);
4462 /* Marking the link as currently not needed by the Driver */
4463 if (!netif_running(netdev))
4464 mlx5_set_port_admin_status(mdev, MLX5_PORT_DOWN);
4466 /* MTU range: 68 - hw-specific max */
4467 netdev->min_mtu = ETH_MIN_MTU;
4468 mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
4469 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
4470 mlx5e_set_dev_port_mtu(priv);
4472 mlx5_lag_add(mdev, netdev);
4474 mlx5e_enable_async_events(priv);
4476 if (MLX5_VPORT_MANAGER(priv->mdev))
4477 mlx5e_register_vport_reps(priv);
4479 if (netdev->reg_state != NETREG_REGISTERED)
4481 #ifdef CONFIG_MLX5_CORE_EN_DCB
4482 mlx5e_dcbnl_init_app(priv);
4485 queue_work(priv->wq, &priv->set_rx_mode_work);
4488 if (netif_running(netdev))
4490 netif_device_attach(netdev);
4494 static void mlx5e_nic_disable(struct mlx5e_priv *priv)
4496 struct mlx5_core_dev *mdev = priv->mdev;
4498 #ifdef CONFIG_MLX5_CORE_EN_DCB
4499 if (priv->netdev->reg_state == NETREG_REGISTERED)
4500 mlx5e_dcbnl_delete_app(priv);
4504 if (netif_running(priv->netdev))
4505 mlx5e_close(priv->netdev);
4506 netif_device_detach(priv->netdev);
4509 queue_work(priv->wq, &priv->set_rx_mode_work);
4511 if (MLX5_VPORT_MANAGER(priv->mdev))
4512 mlx5e_unregister_vport_reps(priv);
4514 mlx5e_disable_async_events(priv);
4515 mlx5_lag_remove(mdev);
4518 static const struct mlx5e_profile mlx5e_nic_profile = {
4519 .init = mlx5e_nic_init,
4520 .cleanup = mlx5e_nic_cleanup,
4521 .init_rx = mlx5e_init_nic_rx,
4522 .cleanup_rx = mlx5e_cleanup_nic_rx,
4523 .init_tx = mlx5e_init_nic_tx,
4524 .cleanup_tx = mlx5e_cleanup_nic_tx,
4525 .enable = mlx5e_nic_enable,
4526 .disable = mlx5e_nic_disable,
4527 .update_stats = mlx5e_update_ndo_stats,
4528 .max_nch = mlx5e_get_max_num_channels,
4529 .update_carrier = mlx5e_update_carrier,
4530 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe,
4531 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
4532 .max_tc = MLX5E_MAX_NUM_TC,
4535 /* mlx5e generic netdev management API (move to en_common.c) */
4537 struct net_device *mlx5e_create_netdev(struct mlx5_core_dev *mdev,
4538 const struct mlx5e_profile *profile,
4541 int nch = profile->max_nch(mdev);
4542 struct net_device *netdev;
4543 struct mlx5e_priv *priv;
4545 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
4546 nch * profile->max_tc,
4549 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
4553 #ifdef CONFIG_RFS_ACCEL
4554 netdev->rx_cpu_rmap = mdev->rmap;
4557 profile->init(mdev, netdev, profile, ppriv);
4559 netif_carrier_off(netdev);
4561 priv = netdev_priv(netdev);
4563 priv->wq = create_singlethread_workqueue("mlx5e");
4565 goto err_cleanup_nic;
4570 if (profile->cleanup)
4571 profile->cleanup(priv);
4572 free_netdev(netdev);
4577 int mlx5e_attach_netdev(struct mlx5e_priv *priv)
4579 struct mlx5_core_dev *mdev = priv->mdev;
4580 const struct mlx5e_profile *profile;
4583 profile = priv->profile;
4584 clear_bit(MLX5E_STATE_DESTROYING, &priv->state);
4586 err = profile->init_tx(priv);
4590 mlx5e_create_q_counters(priv);
4592 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
4594 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
4595 goto err_destroy_q_counters;
4598 err = profile->init_rx(priv);
4600 goto err_close_drop_rq;
4602 if (profile->enable)
4603 profile->enable(priv);
4608 mlx5e_close_drop_rq(&priv->drop_rq);
4610 err_destroy_q_counters:
4611 mlx5e_destroy_q_counters(priv);
4612 profile->cleanup_tx(priv);
4618 void mlx5e_detach_netdev(struct mlx5e_priv *priv)
4620 const struct mlx5e_profile *profile = priv->profile;
4622 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
4624 if (profile->disable)
4625 profile->disable(priv);
4626 flush_workqueue(priv->wq);
4628 profile->cleanup_rx(priv);
4629 mlx5e_close_drop_rq(&priv->drop_rq);
4630 mlx5e_destroy_q_counters(priv);
4631 profile->cleanup_tx(priv);
4632 cancel_delayed_work_sync(&priv->update_stats_work);
4635 void mlx5e_destroy_netdev(struct mlx5e_priv *priv)
4637 const struct mlx5e_profile *profile = priv->profile;
4638 struct net_device *netdev = priv->netdev;
4640 destroy_workqueue(priv->wq);
4641 if (profile->cleanup)
4642 profile->cleanup(priv);
4643 free_netdev(netdev);
4646 /* mlx5e_attach and mlx5e_detach scope should be only creating/destroying
4647 * hardware contexts and to connect it to the current netdev.
4649 static int mlx5e_attach(struct mlx5_core_dev *mdev, void *vpriv)
4651 struct mlx5e_priv *priv = vpriv;
4652 struct net_device *netdev = priv->netdev;
4655 if (netif_device_present(netdev))
4658 err = mlx5e_create_mdev_resources(mdev);
4662 err = mlx5e_attach_netdev(priv);
4664 mlx5e_destroy_mdev_resources(mdev);
4671 static void mlx5e_detach(struct mlx5_core_dev *mdev, void *vpriv)
4673 struct mlx5e_priv *priv = vpriv;
4674 struct net_device *netdev = priv->netdev;
4676 if (!netif_device_present(netdev))
4679 mlx5e_detach_netdev(priv);
4680 mlx5e_destroy_mdev_resources(mdev);
4683 static void *mlx5e_add(struct mlx5_core_dev *mdev)
4685 struct net_device *netdev;
4690 err = mlx5e_check_required_hca_cap(mdev);
4694 #ifdef CONFIG_MLX5_ESWITCH
4695 if (MLX5_VPORT_MANAGER(mdev)) {
4696 rpriv = mlx5e_alloc_nic_rep_priv(mdev);
4698 mlx5_core_warn(mdev, "Failed to alloc NIC rep priv data\n");
4704 netdev = mlx5e_create_netdev(mdev, &mlx5e_nic_profile, rpriv);
4706 mlx5_core_err(mdev, "mlx5e_create_netdev failed\n");
4707 goto err_free_rpriv;
4710 priv = netdev_priv(netdev);
4712 err = mlx5e_attach(mdev, priv);
4714 mlx5_core_err(mdev, "mlx5e_attach failed, %d\n", err);
4715 goto err_destroy_netdev;
4718 err = register_netdev(netdev);
4720 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
4724 #ifdef CONFIG_MLX5_CORE_EN_DCB
4725 mlx5e_dcbnl_init_app(priv);
4730 mlx5e_detach(mdev, priv);
4732 mlx5e_destroy_netdev(priv);
4738 static void mlx5e_remove(struct mlx5_core_dev *mdev, void *vpriv)
4740 struct mlx5e_priv *priv = vpriv;
4741 void *ppriv = priv->ppriv;
4743 #ifdef CONFIG_MLX5_CORE_EN_DCB
4744 mlx5e_dcbnl_delete_app(priv);
4746 unregister_netdev(priv->netdev);
4747 mlx5e_detach(mdev, vpriv);
4748 mlx5e_destroy_netdev(priv);
4752 static void *mlx5e_get_netdev(void *vpriv)
4754 struct mlx5e_priv *priv = vpriv;
4756 return priv->netdev;
4759 static struct mlx5_interface mlx5e_interface = {
4761 .remove = mlx5e_remove,
4762 .attach = mlx5e_attach,
4763 .detach = mlx5e_detach,
4764 .event = mlx5e_async_event,
4765 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
4766 .get_dev = mlx5e_get_netdev,
4769 void mlx5e_init(void)
4771 mlx5e_ipsec_build_inverse_table();
4772 mlx5e_build_ptys2ethtool_map();
4773 mlx5_register_interface(&mlx5e_interface);
4776 void mlx5e_cleanup(void)
4778 mlx5_unregister_interface(&mlx5e_interface);