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 static bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
76 return MLX5_CAP_GEN(mdev, striding_rq) &&
77 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
78 MLX5_CAP_ETH(mdev, reg_umr_sq);
81 void mlx5e_init_rq_type_params(struct mlx5_core_dev *mdev,
82 struct mlx5e_params *params, u8 rq_type)
84 params->rq_wq_type = rq_type;
85 params->lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
86 switch (params->rq_wq_type) {
87 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
88 params->log_rq_size = is_kdump_kernel() ?
89 MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW :
90 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE_MPW;
91 params->mpwqe_log_stride_sz = MLX5E_MPWQE_STRIDE_SZ(mdev,
92 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
93 params->mpwqe_log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ -
94 params->mpwqe_log_stride_sz;
96 default: /* MLX5_WQ_TYPE_LINKED_LIST */
97 params->log_rq_size = is_kdump_kernel() ?
98 MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE :
99 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
100 params->rq_headroom = params->xdp_prog ?
101 XDP_PACKET_HEADROOM : MLX5_RX_HEADROOM;
102 params->rq_headroom += NET_IP_ALIGN;
104 /* Extra room needed for build_skb */
105 params->lro_wqe_sz -= params->rq_headroom +
106 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
109 mlx5_core_info(mdev, "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
110 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
111 BIT(params->log_rq_size),
112 BIT(params->mpwqe_log_stride_sz),
113 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
116 static void mlx5e_set_rq_params(struct mlx5_core_dev *mdev,
117 struct mlx5e_params *params)
119 u8 rq_type = mlx5e_check_fragmented_striding_rq_cap(mdev) &&
120 !params->xdp_prog && !MLX5_IPSEC_DEV(mdev) ?
121 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
122 MLX5_WQ_TYPE_LINKED_LIST;
123 mlx5e_init_rq_type_params(mdev, params, rq_type);
126 static void mlx5e_update_carrier(struct mlx5e_priv *priv)
128 struct mlx5_core_dev *mdev = priv->mdev;
131 port_state = mlx5_query_vport_state(mdev,
132 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT,
135 if (port_state == VPORT_STATE_UP) {
136 netdev_info(priv->netdev, "Link up\n");
137 netif_carrier_on(priv->netdev);
139 netdev_info(priv->netdev, "Link down\n");
140 netif_carrier_off(priv->netdev);
144 static void mlx5e_update_carrier_work(struct work_struct *work)
146 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
147 update_carrier_work);
149 mutex_lock(&priv->state_lock);
150 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
151 if (priv->profile->update_carrier)
152 priv->profile->update_carrier(priv);
153 mutex_unlock(&priv->state_lock);
156 static void mlx5e_tx_timeout_work(struct work_struct *work)
158 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
163 mutex_lock(&priv->state_lock);
164 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
166 mlx5e_close_locked(priv->netdev);
167 err = mlx5e_open_locked(priv->netdev);
169 netdev_err(priv->netdev, "mlx5e_open_locked failed recovering from a tx_timeout, err(%d).\n",
172 mutex_unlock(&priv->state_lock);
176 void mlx5e_update_stats(struct mlx5e_priv *priv)
180 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
181 if (mlx5e_stats_grps[i].update_stats)
182 mlx5e_stats_grps[i].update_stats(priv);
185 static void mlx5e_update_ndo_stats(struct mlx5e_priv *priv)
189 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
190 if (mlx5e_stats_grps[i].update_stats_mask &
191 MLX5E_NDO_UPDATE_STATS)
192 mlx5e_stats_grps[i].update_stats(priv);
195 void mlx5e_update_stats_work(struct work_struct *work)
197 struct delayed_work *dwork = to_delayed_work(work);
198 struct mlx5e_priv *priv = container_of(dwork, struct mlx5e_priv,
200 mutex_lock(&priv->state_lock);
201 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
202 priv->profile->update_stats(priv);
203 queue_delayed_work(priv->wq, dwork,
204 msecs_to_jiffies(MLX5E_UPDATE_STATS_INTERVAL));
206 mutex_unlock(&priv->state_lock);
209 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
210 enum mlx5_dev_event event, unsigned long param)
212 struct mlx5e_priv *priv = vpriv;
214 if (!test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state))
218 case MLX5_DEV_EVENT_PORT_UP:
219 case MLX5_DEV_EVENT_PORT_DOWN:
220 queue_work(priv->wq, &priv->update_carrier_work);
227 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
229 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
232 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
234 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
235 synchronize_irq(pci_irq_vector(priv->mdev->pdev, MLX5_EQ_VEC_ASYNC));
238 static inline int mlx5e_get_wqe_mtt_sz(void)
240 /* UMR copies MTTs in units of MLX5_UMR_MTT_ALIGNMENT bytes.
241 * To avoid copying garbage after the mtt array, we allocate
244 return ALIGN(MLX5_MPWRQ_PAGES_PER_WQE * sizeof(__be64),
245 MLX5_UMR_MTT_ALIGNMENT);
248 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq,
249 struct mlx5e_icosq *sq,
250 struct mlx5e_umr_wqe *wqe,
253 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
254 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
255 struct mlx5_wqe_data_seg *dseg = &wqe->data;
256 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[ix];
257 u8 ds_cnt = DIV_ROUND_UP(sizeof(*wqe), MLX5_SEND_WQE_DS);
258 u32 umr_wqe_mtt_offset = mlx5e_get_wqe_mtt_offset(rq, ix);
260 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
262 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
263 cseg->imm = rq->mkey_be;
265 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN;
266 ucseg->xlt_octowords =
267 cpu_to_be16(MLX5_MTT_OCTW(MLX5_MPWRQ_PAGES_PER_WQE));
268 ucseg->bsf_octowords =
269 cpu_to_be16(MLX5_MTT_OCTW(umr_wqe_mtt_offset));
270 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
272 dseg->lkey = sq->mkey_be;
273 dseg->addr = cpu_to_be64(wi->umr.mtt_addr);
276 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq,
277 struct mlx5e_channel *c)
279 int wq_sz = mlx5_wq_ll_get_size(&rq->wq);
280 int mtt_sz = mlx5e_get_wqe_mtt_sz();
281 int mtt_alloc = mtt_sz + MLX5_UMR_ALIGN - 1;
284 rq->mpwqe.info = kzalloc_node(wq_sz * sizeof(*rq->mpwqe.info),
285 GFP_KERNEL, cpu_to_node(c->cpu));
289 /* We allocate more than mtt_sz as we will align the pointer */
290 rq->mpwqe.mtt_no_align = kzalloc_node(mtt_alloc * wq_sz, GFP_KERNEL,
291 cpu_to_node(c->cpu));
292 if (unlikely(!rq->mpwqe.mtt_no_align))
293 goto err_free_wqe_info;
295 for (i = 0; i < wq_sz; i++) {
296 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[i];
298 wi->umr.mtt = PTR_ALIGN(rq->mpwqe.mtt_no_align + i * mtt_alloc,
300 wi->umr.mtt_addr = dma_map_single(c->pdev, wi->umr.mtt, mtt_sz,
302 if (unlikely(dma_mapping_error(c->pdev, wi->umr.mtt_addr)))
305 mlx5e_build_umr_wqe(rq, &c->icosq, &wi->umr.wqe, i);
312 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[i];
314 dma_unmap_single(c->pdev, wi->umr.mtt_addr, mtt_sz,
317 kfree(rq->mpwqe.mtt_no_align);
319 kfree(rq->mpwqe.info);
325 static void mlx5e_rq_free_mpwqe_info(struct mlx5e_rq *rq)
327 int wq_sz = mlx5_wq_ll_get_size(&rq->wq);
328 int mtt_sz = mlx5e_get_wqe_mtt_sz();
331 for (i = 0; i < wq_sz; i++) {
332 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[i];
334 dma_unmap_single(rq->pdev, wi->umr.mtt_addr, mtt_sz,
337 kfree(rq->mpwqe.mtt_no_align);
338 kfree(rq->mpwqe.info);
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 if (!MLX5E_VALID_NUM_MTTS(npages))
353 in = kvzalloc(inlen, GFP_KERNEL);
357 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
359 MLX5_SET(mkc, mkc, free, 1);
360 MLX5_SET(mkc, mkc, umr_en, 1);
361 MLX5_SET(mkc, mkc, lw, 1);
362 MLX5_SET(mkc, mkc, lr, 1);
363 MLX5_SET(mkc, mkc, access_mode, MLX5_MKC_ACCESS_MODE_MTT);
365 MLX5_SET(mkc, mkc, qpn, 0xffffff);
366 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.pdn);
367 MLX5_SET64(mkc, mkc, len, npages << page_shift);
368 MLX5_SET(mkc, mkc, translations_octword_size,
369 MLX5_MTT_OCTW(npages));
370 MLX5_SET(mkc, mkc, log_page_size, page_shift);
372 err = mlx5_core_create_mkey(mdev, umr_mkey, in, inlen);
378 static int mlx5e_create_rq_umr_mkey(struct mlx5_core_dev *mdev, struct mlx5e_rq *rq)
380 u64 num_mtts = MLX5E_REQUIRED_MTTS(mlx5_wq_ll_get_size(&rq->wq));
382 return mlx5e_create_umr_mkey(mdev, num_mtts, PAGE_SHIFT, &rq->umr_mkey);
385 static int mlx5e_alloc_rq(struct mlx5e_channel *c,
386 struct mlx5e_params *params,
387 struct mlx5e_rq_param *rqp,
390 struct mlx5_core_dev *mdev = c->mdev;
391 void *rqc = rqp->rqc;
392 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
399 rqp->wq.db_numa_node = cpu_to_node(c->cpu);
401 err = mlx5_wq_ll_create(mdev, &rqp->wq, rqc_wq, &rq->wq,
406 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
408 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
410 rq->wq_type = params->rq_wq_type;
412 rq->netdev = c->netdev;
413 rq->tstamp = c->tstamp;
414 rq->clock = &mdev->clock;
419 rq->xdp_prog = params->xdp_prog ? bpf_prog_inc(params->xdp_prog) : NULL;
420 if (IS_ERR(rq->xdp_prog)) {
421 err = PTR_ERR(rq->xdp_prog);
423 goto err_rq_wq_destroy;
426 err = xdp_rxq_info_reg(&rq->xdp_rxq, rq->netdev, rq->ix);
428 goto err_rq_wq_destroy;
430 rq->buff.map_dir = rq->xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
431 rq->buff.headroom = params->rq_headroom;
433 switch (rq->wq_type) {
434 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
436 rq->post_wqes = mlx5e_post_rx_mpwqes;
437 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
439 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe_mpwqe;
440 #ifdef CONFIG_MLX5_EN_IPSEC
441 if (MLX5_IPSEC_DEV(mdev)) {
443 netdev_err(c->netdev, "MPWQE RQ with IPSec offload not supported\n");
444 goto err_rq_wq_destroy;
447 if (!rq->handle_rx_cqe) {
449 netdev_err(c->netdev, "RX handler of MPWQE RQ is not set, err %d\n", err);
450 goto err_rq_wq_destroy;
453 rq->mpwqe.log_stride_sz = params->mpwqe_log_stride_sz;
454 rq->mpwqe.num_strides = BIT(params->mpwqe_log_num_strides);
456 byte_count = rq->mpwqe.num_strides << rq->mpwqe.log_stride_sz;
458 err = mlx5e_create_rq_umr_mkey(mdev, rq);
460 goto err_rq_wq_destroy;
461 rq->mkey_be = cpu_to_be32(rq->umr_mkey.key);
463 err = mlx5e_rq_alloc_mpwqe_info(rq, c);
465 goto err_destroy_umr_mkey;
467 default: /* MLX5_WQ_TYPE_LINKED_LIST */
469 kzalloc_node(wq_sz * sizeof(*rq->wqe.frag_info),
470 GFP_KERNEL, cpu_to_node(c->cpu));
471 if (!rq->wqe.frag_info) {
473 goto err_rq_wq_destroy;
475 rq->post_wqes = mlx5e_post_rx_wqes;
476 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
478 #ifdef CONFIG_MLX5_EN_IPSEC
480 rq->handle_rx_cqe = mlx5e_ipsec_handle_rx_cqe;
483 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe;
484 if (!rq->handle_rx_cqe) {
485 kfree(rq->wqe.frag_info);
487 netdev_err(c->netdev, "RX handler of RQ is not set, err %d\n", err);
488 goto err_rq_wq_destroy;
491 byte_count = params->lro_en ?
493 MLX5E_SW2HW_MTU(c->priv, c->netdev->mtu);
494 #ifdef CONFIG_MLX5_EN_IPSEC
495 if (MLX5_IPSEC_DEV(mdev))
496 byte_count += MLX5E_METADATA_ETHER_LEN;
498 rq->wqe.page_reuse = !params->xdp_prog && !params->lro_en;
500 /* calc the required page order */
501 rq->wqe.frag_sz = MLX5_SKB_FRAG_SZ(rq->buff.headroom + byte_count);
502 npages = DIV_ROUND_UP(rq->wqe.frag_sz, PAGE_SIZE);
503 rq->buff.page_order = order_base_2(npages);
505 byte_count |= MLX5_HW_START_PADDING;
506 rq->mkey_be = c->mkey_be;
509 for (i = 0; i < wq_sz; i++) {
510 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
512 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
513 u64 dma_offset = (u64)mlx5e_get_wqe_mtt_offset(rq, i) << PAGE_SHIFT;
515 wqe->data.addr = cpu_to_be64(dma_offset);
518 wqe->data.byte_count = cpu_to_be32(byte_count);
519 wqe->data.lkey = rq->mkey_be;
522 INIT_WORK(&rq->dim.work, mlx5e_rx_dim_work);
524 switch (params->rx_cq_moderation.cq_period_mode) {
525 case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
526 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE;
528 case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
530 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
533 rq->page_cache.head = 0;
534 rq->page_cache.tail = 0;
538 err_destroy_umr_mkey:
539 mlx5_core_destroy_mkey(mdev, &rq->umr_mkey);
543 bpf_prog_put(rq->xdp_prog);
544 xdp_rxq_info_unreg(&rq->xdp_rxq);
545 mlx5_wq_destroy(&rq->wq_ctrl);
550 static void mlx5e_free_rq(struct mlx5e_rq *rq)
555 bpf_prog_put(rq->xdp_prog);
557 xdp_rxq_info_unreg(&rq->xdp_rxq);
559 switch (rq->wq_type) {
560 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
561 mlx5e_rq_free_mpwqe_info(rq);
562 mlx5_core_destroy_mkey(rq->mdev, &rq->umr_mkey);
564 default: /* MLX5_WQ_TYPE_LINKED_LIST */
565 kfree(rq->wqe.frag_info);
568 for (i = rq->page_cache.head; i != rq->page_cache.tail;
569 i = (i + 1) & (MLX5E_CACHE_SIZE - 1)) {
570 struct mlx5e_dma_info *dma_info = &rq->page_cache.page_cache[i];
572 mlx5e_page_release(rq, dma_info, false);
574 mlx5_wq_destroy(&rq->wq_ctrl);
577 static int mlx5e_create_rq(struct mlx5e_rq *rq,
578 struct mlx5e_rq_param *param)
580 struct mlx5_core_dev *mdev = rq->mdev;
588 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
589 sizeof(u64) * rq->wq_ctrl.buf.npages;
590 in = kvzalloc(inlen, GFP_KERNEL);
594 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
595 wq = MLX5_ADDR_OF(rqc, rqc, wq);
597 memcpy(rqc, param->rqc, sizeof(param->rqc));
599 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
600 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
601 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
602 MLX5_ADAPTER_PAGE_SHIFT);
603 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
605 mlx5_fill_page_array(&rq->wq_ctrl.buf,
606 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
608 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
615 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
618 struct mlx5e_channel *c = rq->channel;
619 struct mlx5_core_dev *mdev = c->mdev;
626 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
627 in = kvzalloc(inlen, GFP_KERNEL);
631 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
633 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
634 MLX5_SET(rqc, rqc, state, next_state);
636 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
643 static int mlx5e_modify_rq_scatter_fcs(struct mlx5e_rq *rq, bool enable)
645 struct mlx5e_channel *c = rq->channel;
646 struct mlx5e_priv *priv = c->priv;
647 struct mlx5_core_dev *mdev = priv->mdev;
654 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
655 in = kvzalloc(inlen, GFP_KERNEL);
659 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
661 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
662 MLX5_SET64(modify_rq_in, in, modify_bitmask,
663 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_SCATTER_FCS);
664 MLX5_SET(rqc, rqc, scatter_fcs, enable);
665 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
667 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
674 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
676 struct mlx5e_channel *c = rq->channel;
677 struct mlx5_core_dev *mdev = c->mdev;
683 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
684 in = kvzalloc(inlen, GFP_KERNEL);
688 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
690 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
691 MLX5_SET64(modify_rq_in, in, modify_bitmask,
692 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
693 MLX5_SET(rqc, rqc, vsd, vsd);
694 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
696 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
703 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
705 mlx5_core_destroy_rq(rq->mdev, rq->rqn);
708 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
710 unsigned long exp_time = jiffies + msecs_to_jiffies(20000);
711 struct mlx5e_channel *c = rq->channel;
713 struct mlx5_wq_ll *wq = &rq->wq;
714 u16 min_wqes = mlx5_min_rx_wqes(rq->wq_type, mlx5_wq_ll_get_size(wq));
716 while (time_before(jiffies, exp_time)) {
717 if (wq->cur_sz >= min_wqes)
723 netdev_warn(c->netdev, "Failed to get min RX wqes on RQN[0x%x] wq cur_sz(%d) min_rx_wqes(%d)\n",
724 rq->rqn, wq->cur_sz, min_wqes);
728 static void mlx5e_free_rx_descs(struct mlx5e_rq *rq)
730 struct mlx5_wq_ll *wq = &rq->wq;
731 struct mlx5e_rx_wqe *wqe;
735 /* UMR WQE (if in progress) is always at wq->head */
736 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ &&
737 rq->mpwqe.umr_in_progress)
738 mlx5e_free_rx_mpwqe(rq, &rq->mpwqe.info[wq->head]);
740 while (!mlx5_wq_ll_is_empty(wq)) {
741 wqe_ix_be = *wq->tail_next;
742 wqe_ix = be16_to_cpu(wqe_ix_be);
743 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_ix);
744 rq->dealloc_wqe(rq, wqe_ix);
745 mlx5_wq_ll_pop(&rq->wq, wqe_ix_be,
746 &wqe->next.next_wqe_index);
749 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST && rq->wqe.page_reuse) {
750 /* Clean outstanding pages on handled WQEs that decided to do page-reuse,
751 * but yet to be re-posted.
753 int wq_sz = mlx5_wq_ll_get_size(&rq->wq);
755 for (wqe_ix = 0; wqe_ix < wq_sz; wqe_ix++)
756 rq->dealloc_wqe(rq, wqe_ix);
760 static int mlx5e_open_rq(struct mlx5e_channel *c,
761 struct mlx5e_params *params,
762 struct mlx5e_rq_param *param,
767 err = mlx5e_alloc_rq(c, params, param, rq);
771 err = mlx5e_create_rq(rq, param);
775 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
779 if (params->rx_dim_enabled)
780 c->rq.state |= BIT(MLX5E_RQ_STATE_AM);
785 mlx5e_destroy_rq(rq);
792 static void mlx5e_activate_rq(struct mlx5e_rq *rq)
794 struct mlx5e_icosq *sq = &rq->channel->icosq;
795 u16 pi = sq->pc & sq->wq.sz_m1;
796 struct mlx5e_tx_wqe *nopwqe;
798 set_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
799 sq->db.ico_wqe[pi].opcode = MLX5_OPCODE_NOP;
800 nopwqe = mlx5e_post_nop(&sq->wq, sq->sqn, &sq->pc);
801 mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, &nopwqe->ctrl);
804 static void mlx5e_deactivate_rq(struct mlx5e_rq *rq)
806 clear_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
807 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
810 static void mlx5e_close_rq(struct mlx5e_rq *rq)
812 cancel_work_sync(&rq->dim.work);
813 mlx5e_destroy_rq(rq);
814 mlx5e_free_rx_descs(rq);
818 static void mlx5e_free_xdpsq_db(struct mlx5e_xdpsq *sq)
823 static int mlx5e_alloc_xdpsq_db(struct mlx5e_xdpsq *sq, int numa)
825 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
827 sq->db.di = kzalloc_node(sizeof(*sq->db.di) * wq_sz,
830 mlx5e_free_xdpsq_db(sq);
837 static int mlx5e_alloc_xdpsq(struct mlx5e_channel *c,
838 struct mlx5e_params *params,
839 struct mlx5e_sq_param *param,
840 struct mlx5e_xdpsq *sq)
842 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
843 struct mlx5_core_dev *mdev = c->mdev;
847 sq->mkey_be = c->mkey_be;
849 sq->uar_map = mdev->mlx5e_res.bfreg.map;
850 sq->min_inline_mode = params->tx_min_inline_mode;
852 param->wq.db_numa_node = cpu_to_node(c->cpu);
853 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq, &sq->wq_ctrl);
856 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
858 err = mlx5e_alloc_xdpsq_db(sq, cpu_to_node(c->cpu));
860 goto err_sq_wq_destroy;
865 mlx5_wq_destroy(&sq->wq_ctrl);
870 static void mlx5e_free_xdpsq(struct mlx5e_xdpsq *sq)
872 mlx5e_free_xdpsq_db(sq);
873 mlx5_wq_destroy(&sq->wq_ctrl);
876 static void mlx5e_free_icosq_db(struct mlx5e_icosq *sq)
878 kfree(sq->db.ico_wqe);
881 static int mlx5e_alloc_icosq_db(struct mlx5e_icosq *sq, int numa)
883 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
885 sq->db.ico_wqe = kzalloc_node(sizeof(*sq->db.ico_wqe) * wq_sz,
893 static int mlx5e_alloc_icosq(struct mlx5e_channel *c,
894 struct mlx5e_sq_param *param,
895 struct mlx5e_icosq *sq)
897 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
898 struct mlx5_core_dev *mdev = c->mdev;
901 sq->mkey_be = c->mkey_be;
903 sq->uar_map = mdev->mlx5e_res.bfreg.map;
905 param->wq.db_numa_node = cpu_to_node(c->cpu);
906 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq, &sq->wq_ctrl);
909 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
911 err = mlx5e_alloc_icosq_db(sq, cpu_to_node(c->cpu));
913 goto err_sq_wq_destroy;
915 sq->edge = (sq->wq.sz_m1 + 1) - MLX5E_ICOSQ_MAX_WQEBBS;
920 mlx5_wq_destroy(&sq->wq_ctrl);
925 static void mlx5e_free_icosq(struct mlx5e_icosq *sq)
927 mlx5e_free_icosq_db(sq);
928 mlx5_wq_destroy(&sq->wq_ctrl);
931 static void mlx5e_free_txqsq_db(struct mlx5e_txqsq *sq)
933 kfree(sq->db.wqe_info);
934 kfree(sq->db.dma_fifo);
937 static int mlx5e_alloc_txqsq_db(struct mlx5e_txqsq *sq, int numa)
939 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
940 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
942 sq->db.dma_fifo = kzalloc_node(df_sz * sizeof(*sq->db.dma_fifo),
944 sq->db.wqe_info = kzalloc_node(wq_sz * sizeof(*sq->db.wqe_info),
946 if (!sq->db.dma_fifo || !sq->db.wqe_info) {
947 mlx5e_free_txqsq_db(sq);
951 sq->dma_fifo_mask = df_sz - 1;
956 static int mlx5e_alloc_txqsq(struct mlx5e_channel *c,
958 struct mlx5e_params *params,
959 struct mlx5e_sq_param *param,
960 struct mlx5e_txqsq *sq)
962 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
963 struct mlx5_core_dev *mdev = c->mdev;
967 sq->tstamp = c->tstamp;
968 sq->clock = &mdev->clock;
969 sq->mkey_be = c->mkey_be;
972 sq->uar_map = mdev->mlx5e_res.bfreg.map;
973 sq->max_inline = params->tx_max_inline;
974 sq->min_inline_mode = params->tx_min_inline_mode;
975 if (MLX5_IPSEC_DEV(c->priv->mdev))
976 set_bit(MLX5E_SQ_STATE_IPSEC, &sq->state);
978 param->wq.db_numa_node = cpu_to_node(c->cpu);
979 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq, &sq->wq_ctrl);
982 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
984 err = mlx5e_alloc_txqsq_db(sq, cpu_to_node(c->cpu));
986 goto err_sq_wq_destroy;
988 sq->edge = (sq->wq.sz_m1 + 1) - MLX5_SEND_WQE_MAX_WQEBBS;
993 mlx5_wq_destroy(&sq->wq_ctrl);
998 static void mlx5e_free_txqsq(struct mlx5e_txqsq *sq)
1000 mlx5e_free_txqsq_db(sq);
1001 mlx5_wq_destroy(&sq->wq_ctrl);
1004 struct mlx5e_create_sq_param {
1005 struct mlx5_wq_ctrl *wq_ctrl;
1012 static int mlx5e_create_sq(struct mlx5_core_dev *mdev,
1013 struct mlx5e_sq_param *param,
1014 struct mlx5e_create_sq_param *csp,
1023 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1024 sizeof(u64) * csp->wq_ctrl->buf.npages;
1025 in = kvzalloc(inlen, GFP_KERNEL);
1029 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1030 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1032 memcpy(sqc, param->sqc, sizeof(param->sqc));
1033 MLX5_SET(sqc, sqc, tis_lst_sz, csp->tis_lst_sz);
1034 MLX5_SET(sqc, sqc, tis_num_0, csp->tisn);
1035 MLX5_SET(sqc, sqc, cqn, csp->cqn);
1037 if (MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5_CAP_INLINE_MODE_VPORT_CONTEXT)
1038 MLX5_SET(sqc, sqc, min_wqe_inline_mode, csp->min_inline_mode);
1040 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1042 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1043 MLX5_SET(wq, wq, uar_page, mdev->mlx5e_res.bfreg.index);
1044 MLX5_SET(wq, wq, log_wq_pg_sz, csp->wq_ctrl->buf.page_shift -
1045 MLX5_ADAPTER_PAGE_SHIFT);
1046 MLX5_SET64(wq, wq, dbr_addr, csp->wq_ctrl->db.dma);
1048 mlx5_fill_page_array(&csp->wq_ctrl->buf, (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
1050 err = mlx5_core_create_sq(mdev, in, inlen, sqn);
1057 struct mlx5e_modify_sq_param {
1064 static int mlx5e_modify_sq(struct mlx5_core_dev *mdev, u32 sqn,
1065 struct mlx5e_modify_sq_param *p)
1072 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1073 in = kvzalloc(inlen, GFP_KERNEL);
1077 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1079 MLX5_SET(modify_sq_in, in, sq_state, p->curr_state);
1080 MLX5_SET(sqc, sqc, state, p->next_state);
1081 if (p->rl_update && p->next_state == MLX5_SQC_STATE_RDY) {
1082 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
1083 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, p->rl_index);
1086 err = mlx5_core_modify_sq(mdev, sqn, in, inlen);
1093 static void mlx5e_destroy_sq(struct mlx5_core_dev *mdev, u32 sqn)
1095 mlx5_core_destroy_sq(mdev, sqn);
1098 static int mlx5e_create_sq_rdy(struct mlx5_core_dev *mdev,
1099 struct mlx5e_sq_param *param,
1100 struct mlx5e_create_sq_param *csp,
1103 struct mlx5e_modify_sq_param msp = {0};
1106 err = mlx5e_create_sq(mdev, param, csp, sqn);
1110 msp.curr_state = MLX5_SQC_STATE_RST;
1111 msp.next_state = MLX5_SQC_STATE_RDY;
1112 err = mlx5e_modify_sq(mdev, *sqn, &msp);
1114 mlx5e_destroy_sq(mdev, *sqn);
1119 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1120 struct mlx5e_txqsq *sq, u32 rate);
1122 static int mlx5e_open_txqsq(struct mlx5e_channel *c,
1125 struct mlx5e_params *params,
1126 struct mlx5e_sq_param *param,
1127 struct mlx5e_txqsq *sq)
1129 struct mlx5e_create_sq_param csp = {};
1133 err = mlx5e_alloc_txqsq(c, txq_ix, params, param, sq);
1139 csp.cqn = sq->cq.mcq.cqn;
1140 csp.wq_ctrl = &sq->wq_ctrl;
1141 csp.min_inline_mode = sq->min_inline_mode;
1142 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1144 goto err_free_txqsq;
1146 tx_rate = c->priv->tx_rates[sq->txq_ix];
1148 mlx5e_set_sq_maxrate(c->netdev, sq, tx_rate);
1153 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1154 mlx5e_free_txqsq(sq);
1159 static void mlx5e_activate_txqsq(struct mlx5e_txqsq *sq)
1161 sq->txq = netdev_get_tx_queue(sq->channel->netdev, sq->txq_ix);
1162 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1163 netdev_tx_reset_queue(sq->txq);
1164 netif_tx_start_queue(sq->txq);
1167 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
1169 __netif_tx_lock_bh(txq);
1170 netif_tx_stop_queue(txq);
1171 __netif_tx_unlock_bh(txq);
1174 static void mlx5e_deactivate_txqsq(struct mlx5e_txqsq *sq)
1176 struct mlx5e_channel *c = sq->channel;
1178 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1179 /* prevent netif_tx_wake_queue */
1180 napi_synchronize(&c->napi);
1182 netif_tx_disable_queue(sq->txq);
1184 /* last doorbell out, godspeed .. */
1185 if (mlx5e_wqc_has_room_for(&sq->wq, sq->cc, sq->pc, 1)) {
1186 struct mlx5e_tx_wqe *nop;
1188 sq->db.wqe_info[(sq->pc & sq->wq.sz_m1)].skb = NULL;
1189 nop = mlx5e_post_nop(&sq->wq, sq->sqn, &sq->pc);
1190 mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, &nop->ctrl);
1194 static void mlx5e_close_txqsq(struct mlx5e_txqsq *sq)
1196 struct mlx5e_channel *c = sq->channel;
1197 struct mlx5_core_dev *mdev = c->mdev;
1199 mlx5e_destroy_sq(mdev, sq->sqn);
1201 mlx5_rl_remove_rate(mdev, sq->rate_limit);
1202 mlx5e_free_txqsq_descs(sq);
1203 mlx5e_free_txqsq(sq);
1206 static int mlx5e_open_icosq(struct mlx5e_channel *c,
1207 struct mlx5e_params *params,
1208 struct mlx5e_sq_param *param,
1209 struct mlx5e_icosq *sq)
1211 struct mlx5e_create_sq_param csp = {};
1214 err = mlx5e_alloc_icosq(c, param, sq);
1218 csp.cqn = sq->cq.mcq.cqn;
1219 csp.wq_ctrl = &sq->wq_ctrl;
1220 csp.min_inline_mode = params->tx_min_inline_mode;
1221 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1222 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1224 goto err_free_icosq;
1229 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1230 mlx5e_free_icosq(sq);
1235 static void mlx5e_close_icosq(struct mlx5e_icosq *sq)
1237 struct mlx5e_channel *c = sq->channel;
1239 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1240 napi_synchronize(&c->napi);
1242 mlx5e_destroy_sq(c->mdev, sq->sqn);
1243 mlx5e_free_icosq(sq);
1246 static int mlx5e_open_xdpsq(struct mlx5e_channel *c,
1247 struct mlx5e_params *params,
1248 struct mlx5e_sq_param *param,
1249 struct mlx5e_xdpsq *sq)
1251 unsigned int ds_cnt = MLX5E_XDP_TX_DS_COUNT;
1252 struct mlx5e_create_sq_param csp = {};
1253 unsigned int inline_hdr_sz = 0;
1257 err = mlx5e_alloc_xdpsq(c, params, param, sq);
1262 csp.tisn = c->priv->tisn[0]; /* tc = 0 */
1263 csp.cqn = sq->cq.mcq.cqn;
1264 csp.wq_ctrl = &sq->wq_ctrl;
1265 csp.min_inline_mode = sq->min_inline_mode;
1266 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1267 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1269 goto err_free_xdpsq;
1271 if (sq->min_inline_mode != MLX5_INLINE_MODE_NONE) {
1272 inline_hdr_sz = MLX5E_XDP_MIN_INLINE;
1276 /* Pre initialize fixed WQE fields */
1277 for (i = 0; i < mlx5_wq_cyc_get_size(&sq->wq); i++) {
1278 struct mlx5e_tx_wqe *wqe = mlx5_wq_cyc_get_wqe(&sq->wq, i);
1279 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
1280 struct mlx5_wqe_eth_seg *eseg = &wqe->eth;
1281 struct mlx5_wqe_data_seg *dseg;
1283 cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
1284 eseg->inline_hdr.sz = cpu_to_be16(inline_hdr_sz);
1286 dseg = (struct mlx5_wqe_data_seg *)cseg + (ds_cnt - 1);
1287 dseg->lkey = sq->mkey_be;
1293 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1294 mlx5e_free_xdpsq(sq);
1299 static void mlx5e_close_xdpsq(struct mlx5e_xdpsq *sq)
1301 struct mlx5e_channel *c = sq->channel;
1303 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1304 napi_synchronize(&c->napi);
1306 mlx5e_destroy_sq(c->mdev, sq->sqn);
1307 mlx5e_free_xdpsq_descs(sq);
1308 mlx5e_free_xdpsq(sq);
1311 static int mlx5e_alloc_cq_common(struct mlx5_core_dev *mdev,
1312 struct mlx5e_cq_param *param,
1313 struct mlx5e_cq *cq)
1315 struct mlx5_core_cq *mcq = &cq->mcq;
1321 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1326 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1329 mcq->set_ci_db = cq->wq_ctrl.db.db;
1330 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1331 *mcq->set_ci_db = 0;
1333 mcq->vector = param->eq_ix;
1334 mcq->comp = mlx5e_completion_event;
1335 mcq->event = mlx5e_cq_error_event;
1338 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1339 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1349 static int mlx5e_alloc_cq(struct mlx5e_channel *c,
1350 struct mlx5e_cq_param *param,
1351 struct mlx5e_cq *cq)
1353 struct mlx5_core_dev *mdev = c->priv->mdev;
1356 param->wq.buf_numa_node = cpu_to_node(c->cpu);
1357 param->wq.db_numa_node = cpu_to_node(c->cpu);
1358 param->eq_ix = c->ix;
1360 err = mlx5e_alloc_cq_common(mdev, param, cq);
1362 cq->napi = &c->napi;
1368 static void mlx5e_free_cq(struct mlx5e_cq *cq)
1370 mlx5_cqwq_destroy(&cq->wq_ctrl);
1373 static int mlx5e_create_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
1375 struct mlx5_core_dev *mdev = cq->mdev;
1376 struct mlx5_core_cq *mcq = &cq->mcq;
1381 unsigned int irqn_not_used;
1385 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1386 sizeof(u64) * cq->wq_ctrl.frag_buf.npages;
1387 in = kvzalloc(inlen, GFP_KERNEL);
1391 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1393 memcpy(cqc, param->cqc, sizeof(param->cqc));
1395 mlx5_fill_page_frag_array(&cq->wq_ctrl.frag_buf,
1396 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
1398 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
1400 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
1401 MLX5_SET(cqc, cqc, c_eqn, eqn);
1402 MLX5_SET(cqc, cqc, uar_page, mdev->priv.uar->index);
1403 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.frag_buf.page_shift -
1404 MLX5_ADAPTER_PAGE_SHIFT);
1405 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1407 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
1419 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
1421 mlx5_core_destroy_cq(cq->mdev, &cq->mcq);
1424 static int mlx5e_open_cq(struct mlx5e_channel *c,
1425 struct net_dim_cq_moder moder,
1426 struct mlx5e_cq_param *param,
1427 struct mlx5e_cq *cq)
1429 struct mlx5_core_dev *mdev = c->mdev;
1432 err = mlx5e_alloc_cq(c, param, cq);
1436 err = mlx5e_create_cq(cq, param);
1440 if (MLX5_CAP_GEN(mdev, cq_moderation))
1441 mlx5_core_modify_cq_moderation(mdev, &cq->mcq, moder.usec, moder.pkts);
1450 static void mlx5e_close_cq(struct mlx5e_cq *cq)
1452 mlx5e_destroy_cq(cq);
1456 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
1458 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
1461 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1462 struct mlx5e_params *params,
1463 struct mlx5e_channel_param *cparam)
1468 for (tc = 0; tc < c->num_tc; tc++) {
1469 err = mlx5e_open_cq(c, params->tx_cq_moderation,
1470 &cparam->tx_cq, &c->sq[tc].cq);
1472 goto err_close_tx_cqs;
1478 for (tc--; tc >= 0; tc--)
1479 mlx5e_close_cq(&c->sq[tc].cq);
1484 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1488 for (tc = 0; tc < c->num_tc; tc++)
1489 mlx5e_close_cq(&c->sq[tc].cq);
1492 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1493 struct mlx5e_params *params,
1494 struct mlx5e_channel_param *cparam)
1499 for (tc = 0; tc < params->num_tc; tc++) {
1500 int txq_ix = c->ix + tc * params->num_channels;
1502 err = mlx5e_open_txqsq(c, c->priv->tisn[tc], txq_ix,
1503 params, &cparam->sq, &c->sq[tc]);
1511 for (tc--; tc >= 0; tc--)
1512 mlx5e_close_txqsq(&c->sq[tc]);
1517 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1521 for (tc = 0; tc < c->num_tc; tc++)
1522 mlx5e_close_txqsq(&c->sq[tc]);
1525 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1526 struct mlx5e_txqsq *sq, u32 rate)
1528 struct mlx5e_priv *priv = netdev_priv(dev);
1529 struct mlx5_core_dev *mdev = priv->mdev;
1530 struct mlx5e_modify_sq_param msp = {0};
1534 if (rate == sq->rate_limit)
1539 /* remove current rl index to free space to next ones */
1540 mlx5_rl_remove_rate(mdev, sq->rate_limit);
1545 err = mlx5_rl_add_rate(mdev, rate, &rl_index);
1547 netdev_err(dev, "Failed configuring rate %u: %d\n",
1553 msp.curr_state = MLX5_SQC_STATE_RDY;
1554 msp.next_state = MLX5_SQC_STATE_RDY;
1555 msp.rl_index = rl_index;
1556 msp.rl_update = true;
1557 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1559 netdev_err(dev, "Failed configuring rate %u: %d\n",
1561 /* remove the rate from the table */
1563 mlx5_rl_remove_rate(mdev, rate);
1567 sq->rate_limit = rate;
1571 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1573 struct mlx5e_priv *priv = netdev_priv(dev);
1574 struct mlx5_core_dev *mdev = priv->mdev;
1575 struct mlx5e_txqsq *sq = priv->txq2sq[index];
1578 if (!mlx5_rl_is_supported(mdev)) {
1579 netdev_err(dev, "Rate limiting is not supported on this device\n");
1583 /* rate is given in Mb/sec, HW config is in Kb/sec */
1586 /* Check whether rate in valid range, 0 is always valid */
1587 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1588 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1592 mutex_lock(&priv->state_lock);
1593 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1594 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1596 priv->tx_rates[index] = rate;
1597 mutex_unlock(&priv->state_lock);
1602 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1603 struct mlx5e_params *params,
1604 struct mlx5e_channel_param *cparam,
1605 struct mlx5e_channel **cp)
1607 struct net_dim_cq_moder icocq_moder = {0, 0};
1608 struct net_device *netdev = priv->netdev;
1609 int cpu = mlx5e_get_cpu(priv, ix);
1610 struct mlx5e_channel *c;
1615 c = kzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1620 c->mdev = priv->mdev;
1621 c->tstamp = &priv->tstamp;
1624 c->pdev = &priv->mdev->pdev->dev;
1625 c->netdev = priv->netdev;
1626 c->mkey_be = cpu_to_be32(priv->mdev->mlx5e_res.mkey.key);
1627 c->num_tc = params->num_tc;
1628 c->xdp = !!params->xdp_prog;
1630 mlx5_vector2eqn(priv->mdev, ix, &eqn, &irq);
1631 c->irq_desc = irq_to_desc(irq);
1633 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1635 err = mlx5e_open_cq(c, icocq_moder, &cparam->icosq_cq, &c->icosq.cq);
1639 err = mlx5e_open_tx_cqs(c, params, cparam);
1641 goto err_close_icosq_cq;
1643 err = mlx5e_open_cq(c, params->rx_cq_moderation, &cparam->rx_cq, &c->rq.cq);
1645 goto err_close_tx_cqs;
1647 /* XDP SQ CQ params are same as normal TXQ sq CQ params */
1648 err = c->xdp ? mlx5e_open_cq(c, params->tx_cq_moderation,
1649 &cparam->tx_cq, &c->rq.xdpsq.cq) : 0;
1651 goto err_close_rx_cq;
1653 napi_enable(&c->napi);
1655 err = mlx5e_open_icosq(c, params, &cparam->icosq, &c->icosq);
1657 goto err_disable_napi;
1659 err = mlx5e_open_sqs(c, params, cparam);
1661 goto err_close_icosq;
1663 err = c->xdp ? mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, &c->rq.xdpsq) : 0;
1667 err = mlx5e_open_rq(c, params, &cparam->rq, &c->rq);
1669 goto err_close_xdp_sq;
1676 mlx5e_close_xdpsq(&c->rq.xdpsq);
1682 mlx5e_close_icosq(&c->icosq);
1685 napi_disable(&c->napi);
1687 mlx5e_close_cq(&c->rq.xdpsq.cq);
1690 mlx5e_close_cq(&c->rq.cq);
1693 mlx5e_close_tx_cqs(c);
1696 mlx5e_close_cq(&c->icosq.cq);
1699 netif_napi_del(&c->napi);
1705 static void mlx5e_activate_channel(struct mlx5e_channel *c)
1709 for (tc = 0; tc < c->num_tc; tc++)
1710 mlx5e_activate_txqsq(&c->sq[tc]);
1711 mlx5e_activate_rq(&c->rq);
1712 netif_set_xps_queue(c->netdev, get_cpu_mask(c->cpu), c->ix);
1715 static void mlx5e_deactivate_channel(struct mlx5e_channel *c)
1719 mlx5e_deactivate_rq(&c->rq);
1720 for (tc = 0; tc < c->num_tc; tc++)
1721 mlx5e_deactivate_txqsq(&c->sq[tc]);
1724 static void mlx5e_close_channel(struct mlx5e_channel *c)
1726 mlx5e_close_rq(&c->rq);
1728 mlx5e_close_xdpsq(&c->rq.xdpsq);
1730 mlx5e_close_icosq(&c->icosq);
1731 napi_disable(&c->napi);
1733 mlx5e_close_cq(&c->rq.xdpsq.cq);
1734 mlx5e_close_cq(&c->rq.cq);
1735 mlx5e_close_tx_cqs(c);
1736 mlx5e_close_cq(&c->icosq.cq);
1737 netif_napi_del(&c->napi);
1742 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
1743 struct mlx5e_params *params,
1744 struct mlx5e_rq_param *param)
1746 void *rqc = param->rqc;
1747 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1749 switch (params->rq_wq_type) {
1750 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1751 MLX5_SET(wq, wq, log_wqe_num_of_strides, params->mpwqe_log_num_strides - 9);
1752 MLX5_SET(wq, wq, log_wqe_stride_size, params->mpwqe_log_stride_sz - 6);
1753 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
1755 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1756 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1759 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1760 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1761 MLX5_SET(wq, wq, log_wq_sz, params->log_rq_size);
1762 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
1763 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
1764 MLX5_SET(rqc, rqc, vsd, params->vlan_strip_disable);
1765 MLX5_SET(rqc, rqc, scatter_fcs, params->scatter_fcs_en);
1767 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1768 param->wq.linear = 1;
1771 static void mlx5e_build_drop_rq_param(struct mlx5e_rq_param *param)
1773 void *rqc = param->rqc;
1774 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1776 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1777 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1780 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
1781 struct mlx5e_sq_param *param)
1783 void *sqc = param->sqc;
1784 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1786 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1787 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
1789 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1792 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
1793 struct mlx5e_params *params,
1794 struct mlx5e_sq_param *param)
1796 void *sqc = param->sqc;
1797 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1799 mlx5e_build_sq_param_common(priv, param);
1800 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
1801 MLX5_SET(sqc, sqc, allow_swp, !!MLX5_IPSEC_DEV(priv->mdev));
1804 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1805 struct mlx5e_cq_param *param)
1807 void *cqc = param->cqc;
1809 MLX5_SET(cqc, cqc, uar_page, priv->mdev->priv.uar->index);
1812 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1813 struct mlx5e_params *params,
1814 struct mlx5e_cq_param *param)
1816 void *cqc = param->cqc;
1819 switch (params->rq_wq_type) {
1820 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1821 log_cq_size = params->log_rq_size + params->mpwqe_log_num_strides;
1823 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1824 log_cq_size = params->log_rq_size;
1827 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
1828 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
1829 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
1830 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
1833 mlx5e_build_common_cq_param(priv, param);
1834 param->cq_period_mode = params->rx_cq_moderation.cq_period_mode;
1837 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
1838 struct mlx5e_params *params,
1839 struct mlx5e_cq_param *param)
1841 void *cqc = param->cqc;
1843 MLX5_SET(cqc, cqc, log_cq_size, params->log_sq_size);
1845 mlx5e_build_common_cq_param(priv, param);
1846 param->cq_period_mode = params->tx_cq_moderation.cq_period_mode;
1849 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
1851 struct mlx5e_cq_param *param)
1853 void *cqc = param->cqc;
1855 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
1857 mlx5e_build_common_cq_param(priv, param);
1859 param->cq_period_mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1862 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
1864 struct mlx5e_sq_param *param)
1866 void *sqc = param->sqc;
1867 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1869 mlx5e_build_sq_param_common(priv, param);
1871 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
1872 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
1875 static void mlx5e_build_xdpsq_param(struct mlx5e_priv *priv,
1876 struct mlx5e_params *params,
1877 struct mlx5e_sq_param *param)
1879 void *sqc = param->sqc;
1880 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1882 mlx5e_build_sq_param_common(priv, param);
1883 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
1886 static void mlx5e_build_channel_param(struct mlx5e_priv *priv,
1887 struct mlx5e_params *params,
1888 struct mlx5e_channel_param *cparam)
1890 u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
1892 mlx5e_build_rq_param(priv, params, &cparam->rq);
1893 mlx5e_build_sq_param(priv, params, &cparam->sq);
1894 mlx5e_build_xdpsq_param(priv, params, &cparam->xdp_sq);
1895 mlx5e_build_icosq_param(priv, icosq_log_wq_sz, &cparam->icosq);
1896 mlx5e_build_rx_cq_param(priv, params, &cparam->rx_cq);
1897 mlx5e_build_tx_cq_param(priv, params, &cparam->tx_cq);
1898 mlx5e_build_ico_cq_param(priv, icosq_log_wq_sz, &cparam->icosq_cq);
1901 int mlx5e_open_channels(struct mlx5e_priv *priv,
1902 struct mlx5e_channels *chs)
1904 struct mlx5e_channel_param *cparam;
1908 chs->num = chs->params.num_channels;
1910 chs->c = kcalloc(chs->num, sizeof(struct mlx5e_channel *), GFP_KERNEL);
1911 cparam = kzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
1912 if (!chs->c || !cparam)
1915 mlx5e_build_channel_param(priv, &chs->params, cparam);
1916 for (i = 0; i < chs->num; i++) {
1917 err = mlx5e_open_channel(priv, i, &chs->params, cparam, &chs->c[i]);
1919 goto err_close_channels;
1926 for (i--; i >= 0; i--)
1927 mlx5e_close_channel(chs->c[i]);
1936 static void mlx5e_activate_channels(struct mlx5e_channels *chs)
1940 for (i = 0; i < chs->num; i++)
1941 mlx5e_activate_channel(chs->c[i]);
1944 static int mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels *chs)
1949 for (i = 0; i < chs->num; i++) {
1950 err = mlx5e_wait_for_min_rx_wqes(&chs->c[i]->rq);
1958 static void mlx5e_deactivate_channels(struct mlx5e_channels *chs)
1962 for (i = 0; i < chs->num; i++)
1963 mlx5e_deactivate_channel(chs->c[i]);
1966 void mlx5e_close_channels(struct mlx5e_channels *chs)
1970 for (i = 0; i < chs->num; i++)
1971 mlx5e_close_channel(chs->c[i]);
1978 mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, struct mlx5e_rqt *rqt)
1980 struct mlx5_core_dev *mdev = priv->mdev;
1987 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
1988 in = kvzalloc(inlen, GFP_KERNEL);
1992 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
1994 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1995 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
1997 for (i = 0; i < sz; i++)
1998 MLX5_SET(rqtc, rqtc, rq_num[i], priv->drop_rq.rqn);
2000 err = mlx5_core_create_rqt(mdev, in, inlen, &rqt->rqtn);
2002 rqt->enabled = true;
2008 void mlx5e_destroy_rqt(struct mlx5e_priv *priv, struct mlx5e_rqt *rqt)
2010 rqt->enabled = false;
2011 mlx5_core_destroy_rqt(priv->mdev, rqt->rqtn);
2014 int mlx5e_create_indirect_rqt(struct mlx5e_priv *priv)
2016 struct mlx5e_rqt *rqt = &priv->indir_rqt;
2019 err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, rqt);
2021 mlx5_core_warn(priv->mdev, "create indirect rqts failed, %d\n", err);
2025 int mlx5e_create_direct_rqts(struct mlx5e_priv *priv)
2027 struct mlx5e_rqt *rqt;
2031 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2032 rqt = &priv->direct_tir[ix].rqt;
2033 err = mlx5e_create_rqt(priv, 1 /*size */, rqt);
2035 goto err_destroy_rqts;
2041 mlx5_core_warn(priv->mdev, "create direct rqts failed, %d\n", err);
2042 for (ix--; ix >= 0; ix--)
2043 mlx5e_destroy_rqt(priv, &priv->direct_tir[ix].rqt);
2048 void mlx5e_destroy_direct_rqts(struct mlx5e_priv *priv)
2052 for (i = 0; i < priv->profile->max_nch(priv->mdev); i++)
2053 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
2056 static int mlx5e_rx_hash_fn(int hfunc)
2058 return (hfunc == ETH_RSS_HASH_TOP) ?
2059 MLX5_RX_HASH_FN_TOEPLITZ :
2060 MLX5_RX_HASH_FN_INVERTED_XOR8;
2063 int mlx5e_bits_invert(unsigned long a, int size)
2068 for (i = 0; i < size; i++)
2069 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
2074 static void mlx5e_fill_rqt_rqns(struct mlx5e_priv *priv, int sz,
2075 struct mlx5e_redirect_rqt_param rrp, void *rqtc)
2079 for (i = 0; i < sz; i++) {
2085 if (rrp.rss.hfunc == ETH_RSS_HASH_XOR)
2086 ix = mlx5e_bits_invert(i, ilog2(sz));
2088 ix = priv->channels.params.indirection_rqt[ix];
2089 rqn = rrp.rss.channels->c[ix]->rq.rqn;
2093 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
2097 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz,
2098 struct mlx5e_redirect_rqt_param rrp)
2100 struct mlx5_core_dev *mdev = priv->mdev;
2106 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
2107 in = kvzalloc(inlen, GFP_KERNEL);
2111 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
2113 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2114 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
2115 mlx5e_fill_rqt_rqns(priv, sz, rrp, rqtc);
2116 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
2122 static u32 mlx5e_get_direct_rqn(struct mlx5e_priv *priv, int ix,
2123 struct mlx5e_redirect_rqt_param rrp)
2128 if (ix >= rrp.rss.channels->num)
2129 return priv->drop_rq.rqn;
2131 return rrp.rss.channels->c[ix]->rq.rqn;
2134 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv,
2135 struct mlx5e_redirect_rqt_param rrp)
2140 if (priv->indir_rqt.enabled) {
2142 rqtn = priv->indir_rqt.rqtn;
2143 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, rrp);
2146 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2147 struct mlx5e_redirect_rqt_param direct_rrp = {
2150 .rqn = mlx5e_get_direct_rqn(priv, ix, rrp)
2154 /* Direct RQ Tables */
2155 if (!priv->direct_tir[ix].rqt.enabled)
2158 rqtn = priv->direct_tir[ix].rqt.rqtn;
2159 mlx5e_redirect_rqt(priv, rqtn, 1, direct_rrp);
2163 static void mlx5e_redirect_rqts_to_channels(struct mlx5e_priv *priv,
2164 struct mlx5e_channels *chs)
2166 struct mlx5e_redirect_rqt_param rrp = {
2171 .hfunc = chs->params.rss_hfunc,
2176 mlx5e_redirect_rqts(priv, rrp);
2179 static void mlx5e_redirect_rqts_to_drop(struct mlx5e_priv *priv)
2181 struct mlx5e_redirect_rqt_param drop_rrp = {
2184 .rqn = priv->drop_rq.rqn,
2188 mlx5e_redirect_rqts(priv, drop_rrp);
2191 static void mlx5e_build_tir_ctx_lro(struct mlx5e_params *params, void *tirc)
2193 if (!params->lro_en)
2196 #define ROUGH_MAX_L2_L3_HDR_SZ 256
2198 MLX5_SET(tirc, tirc, lro_enable_mask,
2199 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2200 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
2201 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
2202 (params->lro_wqe_sz - ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
2203 MLX5_SET(tirc, tirc, lro_timeout_period_usecs, params->lro_timeout);
2206 void mlx5e_build_indir_tir_ctx_hash(struct mlx5e_params *params,
2207 enum mlx5e_traffic_types tt,
2208 void *tirc, bool inner)
2210 void *hfso = inner ? MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner) :
2211 MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2213 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
2214 MLX5_HASH_FIELD_SEL_DST_IP)
2216 #define MLX5_HASH_IP_L4PORTS (MLX5_HASH_FIELD_SEL_SRC_IP |\
2217 MLX5_HASH_FIELD_SEL_DST_IP |\
2218 MLX5_HASH_FIELD_SEL_L4_SPORT |\
2219 MLX5_HASH_FIELD_SEL_L4_DPORT)
2221 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
2222 MLX5_HASH_FIELD_SEL_DST_IP |\
2223 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2225 MLX5_SET(tirc, tirc, rx_hash_fn, mlx5e_rx_hash_fn(params->rss_hfunc));
2226 if (params->rss_hfunc == ETH_RSS_HASH_TOP) {
2227 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
2228 rx_hash_toeplitz_key);
2229 size_t len = MLX5_FLD_SZ_BYTES(tirc,
2230 rx_hash_toeplitz_key);
2232 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
2233 memcpy(rss_key, params->toeplitz_hash_key, len);
2237 case MLX5E_TT_IPV4_TCP:
2238 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2239 MLX5_L3_PROT_TYPE_IPV4);
2240 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2241 MLX5_L4_PROT_TYPE_TCP);
2242 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2243 MLX5_HASH_IP_L4PORTS);
2246 case MLX5E_TT_IPV6_TCP:
2247 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2248 MLX5_L3_PROT_TYPE_IPV6);
2249 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2250 MLX5_L4_PROT_TYPE_TCP);
2251 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2252 MLX5_HASH_IP_L4PORTS);
2255 case MLX5E_TT_IPV4_UDP:
2256 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2257 MLX5_L3_PROT_TYPE_IPV4);
2258 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2259 MLX5_L4_PROT_TYPE_UDP);
2260 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2261 MLX5_HASH_IP_L4PORTS);
2264 case MLX5E_TT_IPV6_UDP:
2265 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2266 MLX5_L3_PROT_TYPE_IPV6);
2267 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2268 MLX5_L4_PROT_TYPE_UDP);
2269 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2270 MLX5_HASH_IP_L4PORTS);
2273 case MLX5E_TT_IPV4_IPSEC_AH:
2274 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2275 MLX5_L3_PROT_TYPE_IPV4);
2276 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2277 MLX5_HASH_IP_IPSEC_SPI);
2280 case MLX5E_TT_IPV6_IPSEC_AH:
2281 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2282 MLX5_L3_PROT_TYPE_IPV6);
2283 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2284 MLX5_HASH_IP_IPSEC_SPI);
2287 case MLX5E_TT_IPV4_IPSEC_ESP:
2288 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2289 MLX5_L3_PROT_TYPE_IPV4);
2290 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2291 MLX5_HASH_IP_IPSEC_SPI);
2294 case MLX5E_TT_IPV6_IPSEC_ESP:
2295 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2296 MLX5_L3_PROT_TYPE_IPV6);
2297 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2298 MLX5_HASH_IP_IPSEC_SPI);
2302 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2303 MLX5_L3_PROT_TYPE_IPV4);
2304 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2309 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2310 MLX5_L3_PROT_TYPE_IPV6);
2311 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2315 WARN_ONCE(true, "%s: bad traffic type!\n", __func__);
2319 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
2321 struct mlx5_core_dev *mdev = priv->mdev;
2330 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
2331 in = kvzalloc(inlen, GFP_KERNEL);
2335 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
2336 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2338 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2340 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2341 err = mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in,
2347 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2348 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
2360 static void mlx5e_build_inner_indir_tir_ctx(struct mlx5e_priv *priv,
2361 enum mlx5e_traffic_types tt,
2364 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2366 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2368 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2369 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
2370 MLX5_SET(tirc, tirc, tunneled_offload_en, 0x1);
2372 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, true);
2375 static int mlx5e_set_mtu(struct mlx5e_priv *priv, u16 mtu)
2377 struct mlx5_core_dev *mdev = priv->mdev;
2378 u16 hw_mtu = MLX5E_SW2HW_MTU(priv, mtu);
2381 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
2385 /* Update vport context MTU */
2386 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
2390 static void mlx5e_query_mtu(struct mlx5e_priv *priv, u16 *mtu)
2392 struct mlx5_core_dev *mdev = priv->mdev;
2396 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
2397 if (err || !hw_mtu) /* fallback to port oper mtu */
2398 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
2400 *mtu = MLX5E_HW2SW_MTU(priv, hw_mtu);
2403 static int mlx5e_set_dev_port_mtu(struct mlx5e_priv *priv)
2405 struct net_device *netdev = priv->netdev;
2409 err = mlx5e_set_mtu(priv, netdev->mtu);
2413 mlx5e_query_mtu(priv, &mtu);
2414 if (mtu != netdev->mtu)
2415 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
2416 __func__, mtu, netdev->mtu);
2422 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
2424 struct mlx5e_priv *priv = netdev_priv(netdev);
2425 int nch = priv->channels.params.num_channels;
2426 int ntc = priv->channels.params.num_tc;
2429 netdev_reset_tc(netdev);
2434 netdev_set_num_tc(netdev, ntc);
2436 /* Map netdev TCs to offset 0
2437 * We have our own UP to TXQ mapping for QoS
2439 for (tc = 0; tc < ntc; tc++)
2440 netdev_set_tc_queue(netdev, tc, nch, 0);
2443 static void mlx5e_build_channels_tx_maps(struct mlx5e_priv *priv)
2445 struct mlx5e_channel *c;
2446 struct mlx5e_txqsq *sq;
2449 for (i = 0; i < priv->channels.num; i++)
2450 for (tc = 0; tc < priv->profile->max_tc; tc++)
2451 priv->channel_tc2txq[i][tc] = i + tc * priv->channels.num;
2453 for (i = 0; i < priv->channels.num; i++) {
2454 c = priv->channels.c[i];
2455 for (tc = 0; tc < c->num_tc; tc++) {
2457 priv->txq2sq[sq->txq_ix] = sq;
2462 void mlx5e_activate_priv_channels(struct mlx5e_priv *priv)
2464 int num_txqs = priv->channels.num * priv->channels.params.num_tc;
2465 struct net_device *netdev = priv->netdev;
2467 mlx5e_netdev_set_tcs(netdev);
2468 netif_set_real_num_tx_queues(netdev, num_txqs);
2469 netif_set_real_num_rx_queues(netdev, priv->channels.num);
2471 mlx5e_build_channels_tx_maps(priv);
2472 mlx5e_activate_channels(&priv->channels);
2473 netif_tx_start_all_queues(priv->netdev);
2475 if (MLX5_VPORT_MANAGER(priv->mdev))
2476 mlx5e_add_sqs_fwd_rules(priv);
2478 mlx5e_wait_channels_min_rx_wqes(&priv->channels);
2479 mlx5e_redirect_rqts_to_channels(priv, &priv->channels);
2482 void mlx5e_deactivate_priv_channels(struct mlx5e_priv *priv)
2484 mlx5e_redirect_rqts_to_drop(priv);
2486 if (MLX5_VPORT_MANAGER(priv->mdev))
2487 mlx5e_remove_sqs_fwd_rules(priv);
2489 /* FIXME: This is a W/A only for tx timeout watch dog false alarm when
2490 * polling for inactive tx queues.
2492 netif_tx_stop_all_queues(priv->netdev);
2493 netif_tx_disable(priv->netdev);
2494 mlx5e_deactivate_channels(&priv->channels);
2497 void mlx5e_switch_priv_channels(struct mlx5e_priv *priv,
2498 struct mlx5e_channels *new_chs,
2499 mlx5e_fp_hw_modify hw_modify)
2501 struct net_device *netdev = priv->netdev;
2504 new_num_txqs = new_chs->num * new_chs->params.num_tc;
2506 carrier_ok = netif_carrier_ok(netdev);
2507 netif_carrier_off(netdev);
2509 if (new_num_txqs < netdev->real_num_tx_queues)
2510 netif_set_real_num_tx_queues(netdev, new_num_txqs);
2512 mlx5e_deactivate_priv_channels(priv);
2513 mlx5e_close_channels(&priv->channels);
2515 priv->channels = *new_chs;
2517 /* New channels are ready to roll, modify HW settings if needed */
2521 mlx5e_refresh_tirs(priv, false);
2522 mlx5e_activate_priv_channels(priv);
2524 /* return carrier back if needed */
2526 netif_carrier_on(netdev);
2529 void mlx5e_timestamp_init(struct mlx5e_priv *priv)
2531 priv->tstamp.tx_type = HWTSTAMP_TX_OFF;
2532 priv->tstamp.rx_filter = HWTSTAMP_FILTER_NONE;
2535 int mlx5e_open_locked(struct net_device *netdev)
2537 struct mlx5e_priv *priv = netdev_priv(netdev);
2540 set_bit(MLX5E_STATE_OPENED, &priv->state);
2542 err = mlx5e_open_channels(priv, &priv->channels);
2544 goto err_clear_state_opened_flag;
2546 mlx5e_refresh_tirs(priv, false);
2547 mlx5e_activate_priv_channels(priv);
2548 if (priv->profile->update_carrier)
2549 priv->profile->update_carrier(priv);
2551 if (priv->profile->update_stats)
2552 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
2556 err_clear_state_opened_flag:
2557 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2561 int mlx5e_open(struct net_device *netdev)
2563 struct mlx5e_priv *priv = netdev_priv(netdev);
2566 mutex_lock(&priv->state_lock);
2567 err = mlx5e_open_locked(netdev);
2569 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_UP);
2570 mutex_unlock(&priv->state_lock);
2575 int mlx5e_close_locked(struct net_device *netdev)
2577 struct mlx5e_priv *priv = netdev_priv(netdev);
2579 /* May already be CLOSED in case a previous configuration operation
2580 * (e.g RX/TX queue size change) that involves close&open failed.
2582 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2585 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2587 netif_carrier_off(priv->netdev);
2588 mlx5e_deactivate_priv_channels(priv);
2589 mlx5e_close_channels(&priv->channels);
2594 int mlx5e_close(struct net_device *netdev)
2596 struct mlx5e_priv *priv = netdev_priv(netdev);
2599 if (!netif_device_present(netdev))
2602 mutex_lock(&priv->state_lock);
2603 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_DOWN);
2604 err = mlx5e_close_locked(netdev);
2605 mutex_unlock(&priv->state_lock);
2610 static int mlx5e_alloc_drop_rq(struct mlx5_core_dev *mdev,
2611 struct mlx5e_rq *rq,
2612 struct mlx5e_rq_param *param)
2614 void *rqc = param->rqc;
2615 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
2618 param->wq.db_numa_node = param->wq.buf_numa_node;
2620 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
2625 /* Mark as unused given "Drop-RQ" packets never reach XDP */
2626 xdp_rxq_info_unused(&rq->xdp_rxq);
2633 static int mlx5e_alloc_drop_cq(struct mlx5_core_dev *mdev,
2634 struct mlx5e_cq *cq,
2635 struct mlx5e_cq_param *param)
2637 return mlx5e_alloc_cq_common(mdev, param, cq);
2640 static int mlx5e_open_drop_rq(struct mlx5_core_dev *mdev,
2641 struct mlx5e_rq *drop_rq)
2643 struct mlx5e_cq_param cq_param = {};
2644 struct mlx5e_rq_param rq_param = {};
2645 struct mlx5e_cq *cq = &drop_rq->cq;
2648 mlx5e_build_drop_rq_param(&rq_param);
2650 err = mlx5e_alloc_drop_cq(mdev, cq, &cq_param);
2654 err = mlx5e_create_cq(cq, &cq_param);
2658 err = mlx5e_alloc_drop_rq(mdev, drop_rq, &rq_param);
2660 goto err_destroy_cq;
2662 err = mlx5e_create_rq(drop_rq, &rq_param);
2669 mlx5e_free_rq(drop_rq);
2672 mlx5e_destroy_cq(cq);
2680 static void mlx5e_close_drop_rq(struct mlx5e_rq *drop_rq)
2682 mlx5e_destroy_rq(drop_rq);
2683 mlx5e_free_rq(drop_rq);
2684 mlx5e_destroy_cq(&drop_rq->cq);
2685 mlx5e_free_cq(&drop_rq->cq);
2688 int mlx5e_create_tis(struct mlx5_core_dev *mdev, int tc,
2689 u32 underlay_qpn, u32 *tisn)
2691 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
2692 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
2694 MLX5_SET(tisc, tisc, prio, tc << 1);
2695 MLX5_SET(tisc, tisc, underlay_qpn, underlay_qpn);
2696 MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.td.tdn);
2698 if (mlx5_lag_is_lacp_owner(mdev))
2699 MLX5_SET(tisc, tisc, strict_lag_tx_port_affinity, 1);
2701 return mlx5_core_create_tis(mdev, in, sizeof(in), tisn);
2704 void mlx5e_destroy_tis(struct mlx5_core_dev *mdev, u32 tisn)
2706 mlx5_core_destroy_tis(mdev, tisn);
2709 int mlx5e_create_tises(struct mlx5e_priv *priv)
2714 for (tc = 0; tc < priv->profile->max_tc; tc++) {
2715 err = mlx5e_create_tis(priv->mdev, tc, 0, &priv->tisn[tc]);
2717 goto err_close_tises;
2723 for (tc--; tc >= 0; tc--)
2724 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
2729 void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
2733 for (tc = 0; tc < priv->profile->max_tc; tc++)
2734 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
2737 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv,
2738 enum mlx5e_traffic_types tt,
2741 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2743 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2745 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2746 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
2747 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, false);
2750 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 rqtn, u32 *tirc)
2752 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2754 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2756 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2757 MLX5_SET(tirc, tirc, indirect_table, rqtn);
2758 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
2761 int mlx5e_create_indirect_tirs(struct mlx5e_priv *priv)
2763 struct mlx5e_tir *tir;
2771 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2772 in = kvzalloc(inlen, GFP_KERNEL);
2776 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2777 memset(in, 0, inlen);
2778 tir = &priv->indir_tir[tt];
2779 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2780 mlx5e_build_indir_tir_ctx(priv, tt, tirc);
2781 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
2783 mlx5_core_warn(priv->mdev, "create indirect tirs failed, %d\n", err);
2784 goto err_destroy_inner_tirs;
2788 if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
2791 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
2792 memset(in, 0, inlen);
2793 tir = &priv->inner_indir_tir[i];
2794 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2795 mlx5e_build_inner_indir_tir_ctx(priv, i, tirc);
2796 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
2798 mlx5_core_warn(priv->mdev, "create inner indirect tirs failed, %d\n", err);
2799 goto err_destroy_inner_tirs;
2808 err_destroy_inner_tirs:
2809 for (i--; i >= 0; i--)
2810 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
2812 for (tt--; tt >= 0; tt--)
2813 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[tt]);
2820 int mlx5e_create_direct_tirs(struct mlx5e_priv *priv)
2822 int nch = priv->profile->max_nch(priv->mdev);
2823 struct mlx5e_tir *tir;
2830 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2831 in = kvzalloc(inlen, GFP_KERNEL);
2835 for (ix = 0; ix < nch; ix++) {
2836 memset(in, 0, inlen);
2837 tir = &priv->direct_tir[ix];
2838 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2839 mlx5e_build_direct_tir_ctx(priv, priv->direct_tir[ix].rqt.rqtn, tirc);
2840 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
2842 goto err_destroy_ch_tirs;
2849 err_destroy_ch_tirs:
2850 mlx5_core_warn(priv->mdev, "create direct tirs failed, %d\n", err);
2851 for (ix--; ix >= 0; ix--)
2852 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[ix]);
2859 void mlx5e_destroy_indirect_tirs(struct mlx5e_priv *priv)
2863 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
2864 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[i]);
2866 if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
2869 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
2870 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
2873 void mlx5e_destroy_direct_tirs(struct mlx5e_priv *priv)
2875 int nch = priv->profile->max_nch(priv->mdev);
2878 for (i = 0; i < nch; i++)
2879 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[i]);
2882 static int mlx5e_modify_channels_scatter_fcs(struct mlx5e_channels *chs, bool enable)
2887 for (i = 0; i < chs->num; i++) {
2888 err = mlx5e_modify_rq_scatter_fcs(&chs->c[i]->rq, enable);
2896 static int mlx5e_modify_channels_vsd(struct mlx5e_channels *chs, bool vsd)
2901 for (i = 0; i < chs->num; i++) {
2902 err = mlx5e_modify_rq_vsd(&chs->c[i]->rq, vsd);
2910 static int mlx5e_setup_tc_mqprio(struct net_device *netdev,
2911 struct tc_mqprio_qopt *mqprio)
2913 struct mlx5e_priv *priv = netdev_priv(netdev);
2914 struct mlx5e_channels new_channels = {};
2915 u8 tc = mqprio->num_tc;
2918 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2920 if (tc && tc != MLX5E_MAX_NUM_TC)
2923 mutex_lock(&priv->state_lock);
2925 new_channels.params = priv->channels.params;
2926 new_channels.params.num_tc = tc ? tc : 1;
2928 if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
2929 priv->channels.params = new_channels.params;
2933 err = mlx5e_open_channels(priv, &new_channels);
2937 mlx5e_switch_priv_channels(priv, &new_channels, NULL);
2939 mutex_unlock(&priv->state_lock);
2943 #ifdef CONFIG_MLX5_ESWITCH
2944 static int mlx5e_setup_tc_cls_flower(struct mlx5e_priv *priv,
2945 struct tc_cls_flower_offload *cls_flower)
2947 switch (cls_flower->command) {
2948 case TC_CLSFLOWER_REPLACE:
2949 return mlx5e_configure_flower(priv, cls_flower);
2950 case TC_CLSFLOWER_DESTROY:
2951 return mlx5e_delete_flower(priv, cls_flower);
2952 case TC_CLSFLOWER_STATS:
2953 return mlx5e_stats_flower(priv, cls_flower);
2959 int mlx5e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
2962 struct mlx5e_priv *priv = cb_priv;
2964 if (!tc_cls_can_offload_and_chain0(priv->netdev, type_data))
2968 case TC_SETUP_CLSFLOWER:
2969 return mlx5e_setup_tc_cls_flower(priv, type_data);
2975 static int mlx5e_setup_tc_block(struct net_device *dev,
2976 struct tc_block_offload *f)
2978 struct mlx5e_priv *priv = netdev_priv(dev);
2980 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
2983 switch (f->command) {
2985 return tcf_block_cb_register(f->block, mlx5e_setup_tc_block_cb,
2987 case TC_BLOCK_UNBIND:
2988 tcf_block_cb_unregister(f->block, mlx5e_setup_tc_block_cb,
2997 int mlx5e_setup_tc(struct net_device *dev, enum tc_setup_type type,
3001 #ifdef CONFIG_MLX5_ESWITCH
3002 case TC_SETUP_BLOCK:
3003 return mlx5e_setup_tc_block(dev, type_data);
3005 case TC_SETUP_QDISC_MQPRIO:
3006 return mlx5e_setup_tc_mqprio(dev, type_data);
3013 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
3015 struct mlx5e_priv *priv = netdev_priv(dev);
3016 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
3017 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
3018 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
3020 if (mlx5e_is_uplink_rep(priv)) {
3021 stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
3022 stats->rx_bytes = PPORT_802_3_GET(pstats, a_octets_received_ok);
3023 stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
3024 stats->tx_bytes = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
3026 stats->rx_packets = sstats->rx_packets;
3027 stats->rx_bytes = sstats->rx_bytes;
3028 stats->tx_packets = sstats->tx_packets;
3029 stats->tx_bytes = sstats->tx_bytes;
3030 stats->tx_dropped = sstats->tx_queue_dropped;
3033 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
3035 stats->rx_length_errors =
3036 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
3037 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
3038 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
3039 stats->rx_crc_errors =
3040 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
3041 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
3042 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
3043 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
3044 stats->rx_frame_errors;
3045 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
3047 /* vport multicast also counts packets that are dropped due to steering
3048 * or rx out of buffer
3051 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
3054 static void mlx5e_set_rx_mode(struct net_device *dev)
3056 struct mlx5e_priv *priv = netdev_priv(dev);
3058 queue_work(priv->wq, &priv->set_rx_mode_work);
3061 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
3063 struct mlx5e_priv *priv = netdev_priv(netdev);
3064 struct sockaddr *saddr = addr;
3066 if (!is_valid_ether_addr(saddr->sa_data))
3067 return -EADDRNOTAVAIL;
3069 netif_addr_lock_bh(netdev);
3070 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
3071 netif_addr_unlock_bh(netdev);
3073 queue_work(priv->wq, &priv->set_rx_mode_work);
3078 #define MLX5E_SET_FEATURE(features, feature, enable) \
3081 *features |= feature; \
3083 *features &= ~feature; \
3086 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
3088 static int set_feature_lro(struct net_device *netdev, bool enable)
3090 struct mlx5e_priv *priv = netdev_priv(netdev);
3091 struct mlx5e_channels new_channels = {};
3095 mutex_lock(&priv->state_lock);
3097 reset = (priv->channels.params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST);
3098 reset = reset && test_bit(MLX5E_STATE_OPENED, &priv->state);
3100 new_channels.params = priv->channels.params;
3101 new_channels.params.lro_en = enable;
3104 priv->channels.params = new_channels.params;
3105 err = mlx5e_modify_tirs_lro(priv);
3109 err = mlx5e_open_channels(priv, &new_channels);
3113 mlx5e_switch_priv_channels(priv, &new_channels, mlx5e_modify_tirs_lro);
3115 mutex_unlock(&priv->state_lock);
3119 static int set_feature_cvlan_filter(struct net_device *netdev, bool enable)
3121 struct mlx5e_priv *priv = netdev_priv(netdev);
3124 mlx5e_enable_cvlan_filter(priv);
3126 mlx5e_disable_cvlan_filter(priv);
3131 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
3133 struct mlx5e_priv *priv = netdev_priv(netdev);
3135 if (!enable && mlx5e_tc_num_filters(priv)) {
3137 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
3144 static int set_feature_rx_all(struct net_device *netdev, bool enable)
3146 struct mlx5e_priv *priv = netdev_priv(netdev);
3147 struct mlx5_core_dev *mdev = priv->mdev;
3149 return mlx5_set_port_fcs(mdev, !enable);
3152 static int set_feature_rx_fcs(struct net_device *netdev, bool enable)
3154 struct mlx5e_priv *priv = netdev_priv(netdev);
3157 mutex_lock(&priv->state_lock);
3159 priv->channels.params.scatter_fcs_en = enable;
3160 err = mlx5e_modify_channels_scatter_fcs(&priv->channels, enable);
3162 priv->channels.params.scatter_fcs_en = !enable;
3164 mutex_unlock(&priv->state_lock);
3169 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
3171 struct mlx5e_priv *priv = netdev_priv(netdev);
3174 mutex_lock(&priv->state_lock);
3176 priv->channels.params.vlan_strip_disable = !enable;
3177 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3180 err = mlx5e_modify_channels_vsd(&priv->channels, !enable);
3182 priv->channels.params.vlan_strip_disable = enable;
3185 mutex_unlock(&priv->state_lock);
3190 #ifdef CONFIG_RFS_ACCEL
3191 static int set_feature_arfs(struct net_device *netdev, bool enable)
3193 struct mlx5e_priv *priv = netdev_priv(netdev);
3197 err = mlx5e_arfs_enable(priv);
3199 err = mlx5e_arfs_disable(priv);
3205 static int mlx5e_handle_feature(struct net_device *netdev,
3206 netdev_features_t *features,
3207 netdev_features_t wanted_features,
3208 netdev_features_t feature,
3209 mlx5e_feature_handler feature_handler)
3211 netdev_features_t changes = wanted_features ^ netdev->features;
3212 bool enable = !!(wanted_features & feature);
3215 if (!(changes & feature))
3218 err = feature_handler(netdev, enable);
3220 netdev_err(netdev, "%s feature %pNF failed, err %d\n",
3221 enable ? "Enable" : "Disable", &feature, err);
3225 MLX5E_SET_FEATURE(features, feature, enable);
3229 static int mlx5e_set_features(struct net_device *netdev,
3230 netdev_features_t features)
3232 netdev_features_t oper_features = netdev->features;
3235 err = mlx5e_handle_feature(netdev, &oper_features, features,
3236 NETIF_F_LRO, set_feature_lro);
3237 err |= mlx5e_handle_feature(netdev, &oper_features, features,
3238 NETIF_F_HW_VLAN_CTAG_FILTER,
3239 set_feature_cvlan_filter);
3240 err |= mlx5e_handle_feature(netdev, &oper_features, features,
3241 NETIF_F_HW_TC, set_feature_tc_num_filters);
3242 err |= mlx5e_handle_feature(netdev, &oper_features, features,
3243 NETIF_F_RXALL, set_feature_rx_all);
3244 err |= mlx5e_handle_feature(netdev, &oper_features, features,
3245 NETIF_F_RXFCS, set_feature_rx_fcs);
3246 err |= mlx5e_handle_feature(netdev, &oper_features, features,
3247 NETIF_F_HW_VLAN_CTAG_RX, set_feature_rx_vlan);
3248 #ifdef CONFIG_RFS_ACCEL
3249 err |= mlx5e_handle_feature(netdev, &oper_features, features,
3250 NETIF_F_NTUPLE, set_feature_arfs);
3254 netdev->features = oper_features;
3261 static netdev_features_t mlx5e_fix_features(struct net_device *netdev,
3262 netdev_features_t features)
3264 struct mlx5e_priv *priv = netdev_priv(netdev);
3266 mutex_lock(&priv->state_lock);
3267 if (!bitmap_empty(priv->fs.vlan.active_svlans, VLAN_N_VID)) {
3268 /* HW strips the outer C-tag header, this is a problem
3269 * for S-tag traffic.
3271 features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3272 if (!priv->channels.params.vlan_strip_disable)
3273 netdev_warn(netdev, "Dropping C-tag vlan stripping offload due to S-tag vlan\n");
3275 mutex_unlock(&priv->state_lock);
3280 static int mlx5e_change_mtu(struct net_device *netdev, int new_mtu)
3282 struct mlx5e_priv *priv = netdev_priv(netdev);
3283 struct mlx5e_channels new_channels = {};
3288 mutex_lock(&priv->state_lock);
3290 reset = !priv->channels.params.lro_en &&
3291 (priv->channels.params.rq_wq_type !=
3292 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
3294 reset = reset && test_bit(MLX5E_STATE_OPENED, &priv->state);
3296 curr_mtu = netdev->mtu;
3297 netdev->mtu = new_mtu;
3300 mlx5e_set_dev_port_mtu(priv);
3304 new_channels.params = priv->channels.params;
3305 err = mlx5e_open_channels(priv, &new_channels);
3307 netdev->mtu = curr_mtu;
3311 mlx5e_switch_priv_channels(priv, &new_channels, mlx5e_set_dev_port_mtu);
3314 mutex_unlock(&priv->state_lock);
3318 int mlx5e_hwstamp_set(struct mlx5e_priv *priv, struct ifreq *ifr)
3320 struct hwtstamp_config config;
3323 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
3326 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
3329 /* TX HW timestamp */
3330 switch (config.tx_type) {
3331 case HWTSTAMP_TX_OFF:
3332 case HWTSTAMP_TX_ON:
3338 mutex_lock(&priv->state_lock);
3339 /* RX HW timestamp */
3340 switch (config.rx_filter) {
3341 case HWTSTAMP_FILTER_NONE:
3342 /* Reset CQE compression to Admin default */
3343 mlx5e_modify_rx_cqe_compression_locked(priv, priv->channels.params.rx_cqe_compress_def);
3345 case HWTSTAMP_FILTER_ALL:
3346 case HWTSTAMP_FILTER_SOME:
3347 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3348 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3349 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3350 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3351 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3352 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3353 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3354 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3355 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3356 case HWTSTAMP_FILTER_PTP_V2_EVENT:
3357 case HWTSTAMP_FILTER_PTP_V2_SYNC:
3358 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3359 case HWTSTAMP_FILTER_NTP_ALL:
3360 /* Disable CQE compression */
3361 netdev_warn(priv->netdev, "Disabling cqe compression");
3362 err = mlx5e_modify_rx_cqe_compression_locked(priv, false);
3364 netdev_err(priv->netdev, "Failed disabling cqe compression err=%d\n", err);
3365 mutex_unlock(&priv->state_lock);
3368 config.rx_filter = HWTSTAMP_FILTER_ALL;
3371 mutex_unlock(&priv->state_lock);
3375 memcpy(&priv->tstamp, &config, sizeof(config));
3376 mutex_unlock(&priv->state_lock);
3378 return copy_to_user(ifr->ifr_data, &config,
3379 sizeof(config)) ? -EFAULT : 0;
3382 int mlx5e_hwstamp_get(struct mlx5e_priv *priv, struct ifreq *ifr)
3384 struct hwtstamp_config *cfg = &priv->tstamp;
3386 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
3389 return copy_to_user(ifr->ifr_data, cfg, sizeof(*cfg)) ? -EFAULT : 0;
3392 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3394 struct mlx5e_priv *priv = netdev_priv(dev);
3398 return mlx5e_hwstamp_set(priv, ifr);
3400 return mlx5e_hwstamp_get(priv, ifr);
3406 #ifdef CONFIG_MLX5_ESWITCH
3407 static int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
3409 struct mlx5e_priv *priv = netdev_priv(dev);
3410 struct mlx5_core_dev *mdev = priv->mdev;
3412 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
3415 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
3418 struct mlx5e_priv *priv = netdev_priv(dev);
3419 struct mlx5_core_dev *mdev = priv->mdev;
3421 if (vlan_proto != htons(ETH_P_8021Q))
3422 return -EPROTONOSUPPORT;
3424 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
3428 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
3430 struct mlx5e_priv *priv = netdev_priv(dev);
3431 struct mlx5_core_dev *mdev = priv->mdev;
3433 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
3436 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
3438 struct mlx5e_priv *priv = netdev_priv(dev);
3439 struct mlx5_core_dev *mdev = priv->mdev;
3441 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
3444 static int mlx5e_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
3447 struct mlx5e_priv *priv = netdev_priv(dev);
3448 struct mlx5_core_dev *mdev = priv->mdev;
3450 return mlx5_eswitch_set_vport_rate(mdev->priv.eswitch, vf + 1,
3451 max_tx_rate, min_tx_rate);
3454 static int mlx5_vport_link2ifla(u8 esw_link)
3457 case MLX5_ESW_VPORT_ADMIN_STATE_DOWN:
3458 return IFLA_VF_LINK_STATE_DISABLE;
3459 case MLX5_ESW_VPORT_ADMIN_STATE_UP:
3460 return IFLA_VF_LINK_STATE_ENABLE;
3462 return IFLA_VF_LINK_STATE_AUTO;
3465 static int mlx5_ifla_link2vport(u8 ifla_link)
3467 switch (ifla_link) {
3468 case IFLA_VF_LINK_STATE_DISABLE:
3469 return MLX5_ESW_VPORT_ADMIN_STATE_DOWN;
3470 case IFLA_VF_LINK_STATE_ENABLE:
3471 return MLX5_ESW_VPORT_ADMIN_STATE_UP;
3473 return MLX5_ESW_VPORT_ADMIN_STATE_AUTO;
3476 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
3479 struct mlx5e_priv *priv = netdev_priv(dev);
3480 struct mlx5_core_dev *mdev = priv->mdev;
3482 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
3483 mlx5_ifla_link2vport(link_state));
3486 static int mlx5e_get_vf_config(struct net_device *dev,
3487 int vf, struct ifla_vf_info *ivi)
3489 struct mlx5e_priv *priv = netdev_priv(dev);
3490 struct mlx5_core_dev *mdev = priv->mdev;
3493 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
3496 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
3500 static int mlx5e_get_vf_stats(struct net_device *dev,
3501 int vf, struct ifla_vf_stats *vf_stats)
3503 struct mlx5e_priv *priv = netdev_priv(dev);
3504 struct mlx5_core_dev *mdev = priv->mdev;
3506 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
3511 static void mlx5e_add_vxlan_port(struct net_device *netdev,
3512 struct udp_tunnel_info *ti)
3514 struct mlx5e_priv *priv = netdev_priv(netdev);
3516 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3519 if (!mlx5e_vxlan_allowed(priv->mdev))
3522 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 1);
3525 static void mlx5e_del_vxlan_port(struct net_device *netdev,
3526 struct udp_tunnel_info *ti)
3528 struct mlx5e_priv *priv = netdev_priv(netdev);
3530 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3533 if (!mlx5e_vxlan_allowed(priv->mdev))
3536 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 0);
3539 static netdev_features_t mlx5e_tunnel_features_check(struct mlx5e_priv *priv,
3540 struct sk_buff *skb,
3541 netdev_features_t features)
3543 unsigned int offset = 0;
3544 struct udphdr *udph;
3548 switch (vlan_get_protocol(skb)) {
3549 case htons(ETH_P_IP):
3550 proto = ip_hdr(skb)->protocol;
3552 case htons(ETH_P_IPV6):
3553 proto = ipv6_find_hdr(skb, &offset, -1, NULL, NULL);
3563 udph = udp_hdr(skb);
3564 port = be16_to_cpu(udph->dest);
3566 /* Verify if UDP port is being offloaded by HW */
3567 if (mlx5e_vxlan_lookup_port(priv, port))
3572 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
3573 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3576 static netdev_features_t mlx5e_features_check(struct sk_buff *skb,
3577 struct net_device *netdev,
3578 netdev_features_t features)
3580 struct mlx5e_priv *priv = netdev_priv(netdev);
3582 features = vlan_features_check(skb, features);
3583 features = vxlan_features_check(skb, features);
3585 #ifdef CONFIG_MLX5_EN_IPSEC
3586 if (mlx5e_ipsec_feature_check(skb, netdev, features))
3590 /* Validate if the tunneled packet is being offloaded by HW */
3591 if (skb->encapsulation &&
3592 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
3593 return mlx5e_tunnel_features_check(priv, skb, features);
3598 static bool mlx5e_tx_timeout_eq_recover(struct net_device *dev,
3599 struct mlx5e_txqsq *sq)
3601 struct mlx5e_priv *priv = netdev_priv(dev);
3602 struct mlx5_core_dev *mdev = priv->mdev;
3603 int irqn_not_used, eqn;
3607 if (mlx5_vector2eqn(mdev, sq->cq.mcq.vector, &eqn, &irqn_not_used))
3610 eq = mlx5_eqn2eq(mdev, eqn);
3614 netdev_err(dev, "EQ 0x%x: Cons = 0x%x, irqn = 0x%x\n",
3615 eqn, eq->cons_index, eq->irqn);
3617 eqe_count = mlx5_eq_poll_irq_disabled(eq);
3621 netdev_err(dev, "Recover %d eqes on EQ 0x%x\n", eqe_count, eq->eqn);
3622 sq->channel->stats.eq_rearm++;
3626 static void mlx5e_tx_timeout(struct net_device *dev)
3628 struct mlx5e_priv *priv = netdev_priv(dev);
3629 bool reopen_channels = false;
3632 netdev_err(dev, "TX timeout detected\n");
3634 for (i = 0; i < priv->channels.num * priv->channels.params.num_tc; i++) {
3635 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, i);
3636 struct mlx5e_txqsq *sq = priv->txq2sq[i];
3638 if (!netif_xmit_stopped(dev_queue))
3640 netdev_err(dev, "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",
3641 i, sq->sqn, sq->cq.mcq.cqn, sq->cc, sq->pc,
3642 jiffies_to_usecs(jiffies - dev_queue->trans_start));
3644 /* If we recover a lost interrupt, most likely TX timeout will
3645 * be resolved, skip reopening channels
3647 if (!mlx5e_tx_timeout_eq_recover(dev, sq)) {
3648 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
3649 reopen_channels = true;
3653 if (reopen_channels && test_bit(MLX5E_STATE_OPENED, &priv->state))
3654 schedule_work(&priv->tx_timeout_work);
3657 static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog)
3659 struct mlx5e_priv *priv = netdev_priv(netdev);
3660 struct bpf_prog *old_prog;
3662 bool reset, was_opened;
3665 mutex_lock(&priv->state_lock);
3667 if ((netdev->features & NETIF_F_LRO) && prog) {
3668 netdev_warn(netdev, "can't set XDP while LRO is on, disable LRO first\n");
3673 if ((netdev->features & NETIF_F_HW_ESP) && prog) {
3674 netdev_warn(netdev, "can't set XDP with IPSec offload\n");
3679 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
3680 /* no need for full reset when exchanging programs */
3681 reset = (!priv->channels.params.xdp_prog || !prog);
3683 if (was_opened && reset)
3684 mlx5e_close_locked(netdev);
3685 if (was_opened && !reset) {
3686 /* num_channels is invariant here, so we can take the
3687 * batched reference right upfront.
3689 prog = bpf_prog_add(prog, priv->channels.num);
3691 err = PTR_ERR(prog);
3696 /* exchange programs, extra prog reference we got from caller
3697 * as long as we don't fail from this point onwards.
3699 old_prog = xchg(&priv->channels.params.xdp_prog, prog);
3701 bpf_prog_put(old_prog);
3703 if (reset) /* change RQ type according to priv->xdp_prog */
3704 mlx5e_set_rq_params(priv->mdev, &priv->channels.params);
3706 if (was_opened && reset)
3707 mlx5e_open_locked(netdev);
3709 if (!test_bit(MLX5E_STATE_OPENED, &priv->state) || reset)
3712 /* exchanging programs w/o reset, we update ref counts on behalf
3713 * of the channels RQs here.
3715 for (i = 0; i < priv->channels.num; i++) {
3716 struct mlx5e_channel *c = priv->channels.c[i];
3718 clear_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
3719 napi_synchronize(&c->napi);
3720 /* prevent mlx5e_poll_rx_cq from accessing rq->xdp_prog */
3722 old_prog = xchg(&c->rq.xdp_prog, prog);
3724 set_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
3725 /* napi_schedule in case we have missed anything */
3726 napi_schedule(&c->napi);
3729 bpf_prog_put(old_prog);
3733 mutex_unlock(&priv->state_lock);
3737 static u32 mlx5e_xdp_query(struct net_device *dev)
3739 struct mlx5e_priv *priv = netdev_priv(dev);
3740 const struct bpf_prog *xdp_prog;
3743 mutex_lock(&priv->state_lock);
3744 xdp_prog = priv->channels.params.xdp_prog;
3746 prog_id = xdp_prog->aux->id;
3747 mutex_unlock(&priv->state_lock);
3752 static int mlx5e_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3754 switch (xdp->command) {
3755 case XDP_SETUP_PROG:
3756 return mlx5e_xdp_set(dev, xdp->prog);
3757 case XDP_QUERY_PROG:
3758 xdp->prog_id = mlx5e_xdp_query(dev);
3759 xdp->prog_attached = !!xdp->prog_id;
3766 #ifdef CONFIG_NET_POLL_CONTROLLER
3767 /* Fake "interrupt" called by netpoll (eg netconsole) to send skbs without
3768 * reenabling interrupts.
3770 static void mlx5e_netpoll(struct net_device *dev)
3772 struct mlx5e_priv *priv = netdev_priv(dev);
3773 struct mlx5e_channels *chs = &priv->channels;
3777 for (i = 0; i < chs->num; i++)
3778 napi_schedule(&chs->c[i]->napi);
3782 static const struct net_device_ops mlx5e_netdev_ops = {
3783 .ndo_open = mlx5e_open,
3784 .ndo_stop = mlx5e_close,
3785 .ndo_start_xmit = mlx5e_xmit,
3786 .ndo_setup_tc = mlx5e_setup_tc,
3787 .ndo_select_queue = mlx5e_select_queue,
3788 .ndo_get_stats64 = mlx5e_get_stats,
3789 .ndo_set_rx_mode = mlx5e_set_rx_mode,
3790 .ndo_set_mac_address = mlx5e_set_mac,
3791 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
3792 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
3793 .ndo_set_features = mlx5e_set_features,
3794 .ndo_fix_features = mlx5e_fix_features,
3795 .ndo_change_mtu = mlx5e_change_mtu,
3796 .ndo_do_ioctl = mlx5e_ioctl,
3797 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
3798 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
3799 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
3800 .ndo_features_check = mlx5e_features_check,
3801 #ifdef CONFIG_RFS_ACCEL
3802 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
3804 .ndo_tx_timeout = mlx5e_tx_timeout,
3805 .ndo_bpf = mlx5e_xdp,
3806 #ifdef CONFIG_NET_POLL_CONTROLLER
3807 .ndo_poll_controller = mlx5e_netpoll,
3809 #ifdef CONFIG_MLX5_ESWITCH
3810 /* SRIOV E-Switch NDOs */
3811 .ndo_set_vf_mac = mlx5e_set_vf_mac,
3812 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
3813 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
3814 .ndo_set_vf_trust = mlx5e_set_vf_trust,
3815 .ndo_set_vf_rate = mlx5e_set_vf_rate,
3816 .ndo_get_vf_config = mlx5e_get_vf_config,
3817 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
3818 .ndo_get_vf_stats = mlx5e_get_vf_stats,
3819 .ndo_has_offload_stats = mlx5e_has_offload_stats,
3820 .ndo_get_offload_stats = mlx5e_get_offload_stats,
3824 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
3826 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
3828 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
3829 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
3830 !MLX5_CAP_ETH(mdev, csum_cap) ||
3831 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
3832 !MLX5_CAP_ETH(mdev, vlan_cap) ||
3833 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
3834 MLX5_CAP_FLOWTABLE(mdev,
3835 flow_table_properties_nic_receive.max_ft_level)
3837 mlx5_core_warn(mdev,
3838 "Not creating net device, some required device capabilities are missing\n");
3841 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
3842 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
3843 if (!MLX5_CAP_GEN(mdev, cq_moderation))
3844 mlx5_core_warn(mdev, "CQ moderation is not supported\n");
3849 u16 mlx5e_get_max_inline_cap(struct mlx5_core_dev *mdev)
3851 int bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
3853 return bf_buf_size -
3854 sizeof(struct mlx5e_tx_wqe) +
3855 2 /*sizeof(mlx5e_tx_wqe.inline_hdr_start)*/;
3858 void mlx5e_build_default_indir_rqt(u32 *indirection_rqt, int len,
3863 for (i = 0; i < len; i++)
3864 indirection_rqt[i] = i % num_channels;
3867 static int mlx5e_get_pci_bw(struct mlx5_core_dev *mdev, u32 *pci_bw)
3869 enum pcie_link_width width;
3870 enum pci_bus_speed speed;
3873 err = pcie_get_minimum_link(mdev->pdev, &speed, &width);
3877 if (speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
3881 case PCIE_SPEED_2_5GT:
3882 *pci_bw = 2500 * width;
3884 case PCIE_SPEED_5_0GT:
3885 *pci_bw = 5000 * width;
3887 case PCIE_SPEED_8_0GT:
3888 *pci_bw = 8000 * width;
3897 static bool cqe_compress_heuristic(u32 link_speed, u32 pci_bw)
3899 return (link_speed && pci_bw &&
3900 (pci_bw < 40000) && (pci_bw < link_speed));
3903 static bool hw_lro_heuristic(u32 link_speed, u32 pci_bw)
3905 return !(link_speed && pci_bw &&
3906 (pci_bw <= 16000) && (pci_bw < link_speed));
3909 void mlx5e_set_tx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
3911 params->tx_cq_moderation.cq_period_mode = cq_period_mode;
3913 params->tx_cq_moderation.pkts =
3914 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
3915 params->tx_cq_moderation.usec =
3916 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
3918 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
3919 params->tx_cq_moderation.usec =
3920 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC_FROM_CQE;
3922 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_TX_CQE_BASED_MODER,
3923 params->tx_cq_moderation.cq_period_mode ==
3924 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
3927 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
3929 params->rx_cq_moderation.cq_period_mode = cq_period_mode;
3931 params->rx_cq_moderation.pkts =
3932 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
3933 params->rx_cq_moderation.usec =
3934 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
3936 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
3937 params->rx_cq_moderation.usec =
3938 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
3940 if (params->rx_dim_enabled) {
3941 switch (cq_period_mode) {
3942 case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
3943 params->rx_cq_moderation =
3944 net_dim_get_def_profile(NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE);
3946 case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
3948 params->rx_cq_moderation =
3949 net_dim_get_def_profile(NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE);
3953 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_BASED_MODER,
3954 params->rx_cq_moderation.cq_period_mode ==
3955 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
3958 u32 mlx5e_choose_lro_timeout(struct mlx5_core_dev *mdev, u32 wanted_timeout)
3962 /* The supported periods are organized in ascending order */
3963 for (i = 0; i < MLX5E_LRO_TIMEOUT_ARR_SIZE - 1; i++)
3964 if (MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]) >= wanted_timeout)
3967 return MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]);
3970 void mlx5e_build_nic_params(struct mlx5_core_dev *mdev,
3971 struct mlx5e_params *params,
3974 u8 cq_period_mode = 0;
3978 params->num_channels = max_channels;
3981 mlx5e_get_max_linkspeed(mdev, &link_speed);
3982 mlx5e_get_pci_bw(mdev, &pci_bw);
3983 mlx5_core_dbg(mdev, "Max link speed = %d, PCI BW = %d\n",
3984 link_speed, pci_bw);
3987 params->log_sq_size = is_kdump_kernel() ?
3988 MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE :
3989 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
3991 /* set CQE compression */
3992 params->rx_cqe_compress_def = false;
3993 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
3994 MLX5_CAP_GEN(mdev, vport_group_manager))
3995 params->rx_cqe_compress_def = cqe_compress_heuristic(link_speed, pci_bw);
3997 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS, params->rx_cqe_compress_def);
4000 mlx5e_set_rq_params(mdev, params);
4004 /* TODO: && MLX5_CAP_ETH(mdev, lro_cap) */
4005 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
4006 params->lro_en = hw_lro_heuristic(link_speed, pci_bw);
4007 params->lro_timeout = mlx5e_choose_lro_timeout(mdev, MLX5E_DEFAULT_LRO_TIMEOUT);
4009 /* CQ moderation params */
4010 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
4011 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
4012 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
4013 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4014 mlx5e_set_rx_cq_mode_params(params, cq_period_mode);
4015 mlx5e_set_tx_cq_mode_params(params, cq_period_mode);
4018 params->tx_max_inline = mlx5e_get_max_inline_cap(mdev);
4019 params->tx_min_inline_mode = mlx5e_params_calculate_tx_min_inline(mdev);
4022 params->rss_hfunc = ETH_RSS_HASH_XOR;
4023 netdev_rss_key_fill(params->toeplitz_hash_key, sizeof(params->toeplitz_hash_key));
4024 mlx5e_build_default_indir_rqt(params->indirection_rqt,
4025 MLX5E_INDIR_RQT_SIZE, max_channels);
4028 static void mlx5e_build_nic_netdev_priv(struct mlx5_core_dev *mdev,
4029 struct net_device *netdev,
4030 const struct mlx5e_profile *profile,
4033 struct mlx5e_priv *priv = netdev_priv(netdev);
4036 priv->netdev = netdev;
4037 priv->profile = profile;
4038 priv->ppriv = ppriv;
4039 priv->msglevel = MLX5E_MSG_LEVEL;
4040 priv->hard_mtu = MLX5E_ETH_HARD_MTU;
4042 mlx5e_build_nic_params(mdev, &priv->channels.params, profile->max_nch(mdev));
4044 mutex_init(&priv->state_lock);
4046 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
4047 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
4048 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
4049 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
4051 mlx5e_timestamp_init(priv);
4054 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
4056 struct mlx5e_priv *priv = netdev_priv(netdev);
4058 mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
4059 if (is_zero_ether_addr(netdev->dev_addr) &&
4060 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
4061 eth_hw_addr_random(netdev);
4062 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
4066 #if IS_ENABLED(CONFIG_NET_SWITCHDEV) && IS_ENABLED(CONFIG_MLX5_ESWITCH)
4067 static const struct switchdev_ops mlx5e_switchdev_ops = {
4068 .switchdev_port_attr_get = mlx5e_attr_get,
4072 static void mlx5e_build_nic_netdev(struct net_device *netdev)
4074 struct mlx5e_priv *priv = netdev_priv(netdev);
4075 struct mlx5_core_dev *mdev = priv->mdev;
4079 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
4081 netdev->netdev_ops = &mlx5e_netdev_ops;
4083 #ifdef CONFIG_MLX5_CORE_EN_DCB
4084 if (MLX5_CAP_GEN(mdev, vport_group_manager) && MLX5_CAP_GEN(mdev, qos))
4085 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
4088 netdev->watchdog_timeo = 15 * HZ;
4090 netdev->ethtool_ops = &mlx5e_ethtool_ops;
4092 netdev->vlan_features |= NETIF_F_SG;
4093 netdev->vlan_features |= NETIF_F_IP_CSUM;
4094 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
4095 netdev->vlan_features |= NETIF_F_GRO;
4096 netdev->vlan_features |= NETIF_F_TSO;
4097 netdev->vlan_features |= NETIF_F_TSO6;
4098 netdev->vlan_features |= NETIF_F_RXCSUM;
4099 netdev->vlan_features |= NETIF_F_RXHASH;
4101 if (!!MLX5_CAP_ETH(mdev, lro_cap))
4102 netdev->vlan_features |= NETIF_F_LRO;
4104 netdev->hw_features = netdev->vlan_features;
4105 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4106 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4107 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4108 netdev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
4110 if (mlx5e_vxlan_allowed(mdev) || MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4111 netdev->hw_features |= NETIF_F_GSO_PARTIAL;
4112 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
4113 netdev->hw_enc_features |= NETIF_F_IPV6_CSUM;
4114 netdev->hw_enc_features |= NETIF_F_TSO;
4115 netdev->hw_enc_features |= NETIF_F_TSO6;
4116 netdev->hw_enc_features |= NETIF_F_GSO_PARTIAL;
4119 if (mlx5e_vxlan_allowed(mdev)) {
4120 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
4121 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4122 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
4123 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4124 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
4127 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4128 netdev->hw_features |= NETIF_F_GSO_GRE |
4129 NETIF_F_GSO_GRE_CSUM;
4130 netdev->hw_enc_features |= NETIF_F_GSO_GRE |
4131 NETIF_F_GSO_GRE_CSUM;
4132 netdev->gso_partial_features |= NETIF_F_GSO_GRE |
4133 NETIF_F_GSO_GRE_CSUM;
4136 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
4139 netdev->hw_features |= NETIF_F_RXALL;
4141 if (MLX5_CAP_ETH(mdev, scatter_fcs))
4142 netdev->hw_features |= NETIF_F_RXFCS;
4144 netdev->features = netdev->hw_features;
4145 if (!priv->channels.params.lro_en)
4146 netdev->features &= ~NETIF_F_LRO;
4149 netdev->features &= ~NETIF_F_RXALL;
4151 if (!priv->channels.params.scatter_fcs_en)
4152 netdev->features &= ~NETIF_F_RXFCS;
4154 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
4155 if (FT_CAP(flow_modify_en) &&
4156 FT_CAP(modify_root) &&
4157 FT_CAP(identified_miss_table_mode) &&
4158 FT_CAP(flow_table_modify)) {
4159 netdev->hw_features |= NETIF_F_HW_TC;
4160 #ifdef CONFIG_RFS_ACCEL
4161 netdev->hw_features |= NETIF_F_NTUPLE;
4165 netdev->features |= NETIF_F_HIGHDMA;
4166 netdev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
4168 netdev->priv_flags |= IFF_UNICAST_FLT;
4170 mlx5e_set_netdev_dev_addr(netdev);
4172 #if IS_ENABLED(CONFIG_NET_SWITCHDEV) && IS_ENABLED(CONFIG_MLX5_ESWITCH)
4173 if (MLX5_VPORT_MANAGER(mdev))
4174 netdev->switchdev_ops = &mlx5e_switchdev_ops;
4177 mlx5e_ipsec_build_netdev(priv);
4180 static void mlx5e_create_q_counter(struct mlx5e_priv *priv)
4182 struct mlx5_core_dev *mdev = priv->mdev;
4185 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
4187 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
4188 priv->q_counter = 0;
4192 static void mlx5e_destroy_q_counter(struct mlx5e_priv *priv)
4194 if (!priv->q_counter)
4197 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
4200 static void mlx5e_nic_init(struct mlx5_core_dev *mdev,
4201 struct net_device *netdev,
4202 const struct mlx5e_profile *profile,
4205 struct mlx5e_priv *priv = netdev_priv(netdev);
4208 mlx5e_build_nic_netdev_priv(mdev, netdev, profile, ppriv);
4209 err = mlx5e_ipsec_init(priv);
4211 mlx5_core_err(mdev, "IPSec initialization failed, %d\n", err);
4212 mlx5e_build_nic_netdev(netdev);
4213 mlx5e_vxlan_init(priv);
4216 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
4218 mlx5e_ipsec_cleanup(priv);
4219 mlx5e_vxlan_cleanup(priv);
4222 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv)
4224 struct mlx5_core_dev *mdev = priv->mdev;
4227 err = mlx5e_create_indirect_rqt(priv);
4231 err = mlx5e_create_direct_rqts(priv);
4233 goto err_destroy_indirect_rqts;
4235 err = mlx5e_create_indirect_tirs(priv);
4237 goto err_destroy_direct_rqts;
4239 err = mlx5e_create_direct_tirs(priv);
4241 goto err_destroy_indirect_tirs;
4243 err = mlx5e_create_flow_steering(priv);
4245 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
4246 goto err_destroy_direct_tirs;
4249 err = mlx5e_tc_init(priv);
4251 goto err_destroy_flow_steering;
4255 err_destroy_flow_steering:
4256 mlx5e_destroy_flow_steering(priv);
4257 err_destroy_direct_tirs:
4258 mlx5e_destroy_direct_tirs(priv);
4259 err_destroy_indirect_tirs:
4260 mlx5e_destroy_indirect_tirs(priv);
4261 err_destroy_direct_rqts:
4262 mlx5e_destroy_direct_rqts(priv);
4263 err_destroy_indirect_rqts:
4264 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
4268 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv)
4270 mlx5e_tc_cleanup(priv);
4271 mlx5e_destroy_flow_steering(priv);
4272 mlx5e_destroy_direct_tirs(priv);
4273 mlx5e_destroy_indirect_tirs(priv);
4274 mlx5e_destroy_direct_rqts(priv);
4275 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
4278 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv)
4282 err = mlx5e_create_tises(priv);
4284 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
4288 #ifdef CONFIG_MLX5_CORE_EN_DCB
4289 mlx5e_dcbnl_initialize(priv);
4294 static void mlx5e_nic_enable(struct mlx5e_priv *priv)
4296 struct net_device *netdev = priv->netdev;
4297 struct mlx5_core_dev *mdev = priv->mdev;
4300 mlx5e_init_l2_addr(priv);
4302 /* Marking the link as currently not needed by the Driver */
4303 if (!netif_running(netdev))
4304 mlx5_set_port_admin_status(mdev, MLX5_PORT_DOWN);
4306 /* MTU range: 68 - hw-specific max */
4307 netdev->min_mtu = ETH_MIN_MTU;
4308 mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
4309 netdev->max_mtu = MLX5E_HW2SW_MTU(priv, max_mtu);
4310 mlx5e_set_dev_port_mtu(priv);
4312 mlx5_lag_add(mdev, netdev);
4314 mlx5e_enable_async_events(priv);
4316 if (MLX5_VPORT_MANAGER(priv->mdev))
4317 mlx5e_register_vport_reps(priv);
4319 if (netdev->reg_state != NETREG_REGISTERED)
4321 #ifdef CONFIG_MLX5_CORE_EN_DCB
4322 mlx5e_dcbnl_init_app(priv);
4324 /* Device already registered: sync netdev system state */
4325 if (mlx5e_vxlan_allowed(mdev)) {
4327 udp_tunnel_get_rx_info(netdev);
4331 queue_work(priv->wq, &priv->set_rx_mode_work);
4334 if (netif_running(netdev))
4336 netif_device_attach(netdev);
4340 static void mlx5e_nic_disable(struct mlx5e_priv *priv)
4342 struct mlx5_core_dev *mdev = priv->mdev;
4344 #ifdef CONFIG_MLX5_CORE_EN_DCB
4345 if (priv->netdev->reg_state == NETREG_REGISTERED)
4346 mlx5e_dcbnl_delete_app(priv);
4350 if (netif_running(priv->netdev))
4351 mlx5e_close(priv->netdev);
4352 netif_device_detach(priv->netdev);
4355 queue_work(priv->wq, &priv->set_rx_mode_work);
4357 if (MLX5_VPORT_MANAGER(priv->mdev))
4358 mlx5e_unregister_vport_reps(priv);
4360 mlx5e_disable_async_events(priv);
4361 mlx5_lag_remove(mdev);
4364 static const struct mlx5e_profile mlx5e_nic_profile = {
4365 .init = mlx5e_nic_init,
4366 .cleanup = mlx5e_nic_cleanup,
4367 .init_rx = mlx5e_init_nic_rx,
4368 .cleanup_rx = mlx5e_cleanup_nic_rx,
4369 .init_tx = mlx5e_init_nic_tx,
4370 .cleanup_tx = mlx5e_cleanup_nic_tx,
4371 .enable = mlx5e_nic_enable,
4372 .disable = mlx5e_nic_disable,
4373 .update_stats = mlx5e_update_ndo_stats,
4374 .max_nch = mlx5e_get_max_num_channels,
4375 .update_carrier = mlx5e_update_carrier,
4376 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe,
4377 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
4378 .max_tc = MLX5E_MAX_NUM_TC,
4381 /* mlx5e generic netdev management API (move to en_common.c) */
4383 struct net_device *mlx5e_create_netdev(struct mlx5_core_dev *mdev,
4384 const struct mlx5e_profile *profile,
4387 int nch = profile->max_nch(mdev);
4388 struct net_device *netdev;
4389 struct mlx5e_priv *priv;
4391 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
4392 nch * profile->max_tc,
4395 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
4399 #ifdef CONFIG_RFS_ACCEL
4400 netdev->rx_cpu_rmap = mdev->rmap;
4403 profile->init(mdev, netdev, profile, ppriv);
4405 netif_carrier_off(netdev);
4407 priv = netdev_priv(netdev);
4409 priv->wq = create_singlethread_workqueue("mlx5e");
4411 goto err_cleanup_nic;
4416 if (profile->cleanup)
4417 profile->cleanup(priv);
4418 free_netdev(netdev);
4423 int mlx5e_attach_netdev(struct mlx5e_priv *priv)
4425 struct mlx5_core_dev *mdev = priv->mdev;
4426 const struct mlx5e_profile *profile;
4429 profile = priv->profile;
4430 clear_bit(MLX5E_STATE_DESTROYING, &priv->state);
4432 err = profile->init_tx(priv);
4436 err = mlx5e_open_drop_rq(mdev, &priv->drop_rq);
4438 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
4439 goto err_cleanup_tx;
4442 err = profile->init_rx(priv);
4444 goto err_close_drop_rq;
4446 mlx5e_create_q_counter(priv);
4448 if (profile->enable)
4449 profile->enable(priv);
4454 mlx5e_close_drop_rq(&priv->drop_rq);
4457 profile->cleanup_tx(priv);
4463 void mlx5e_detach_netdev(struct mlx5e_priv *priv)
4465 const struct mlx5e_profile *profile = priv->profile;
4467 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
4469 if (profile->disable)
4470 profile->disable(priv);
4471 flush_workqueue(priv->wq);
4473 mlx5e_destroy_q_counter(priv);
4474 profile->cleanup_rx(priv);
4475 mlx5e_close_drop_rq(&priv->drop_rq);
4476 profile->cleanup_tx(priv);
4477 cancel_delayed_work_sync(&priv->update_stats_work);
4480 void mlx5e_destroy_netdev(struct mlx5e_priv *priv)
4482 const struct mlx5e_profile *profile = priv->profile;
4483 struct net_device *netdev = priv->netdev;
4485 destroy_workqueue(priv->wq);
4486 if (profile->cleanup)
4487 profile->cleanup(priv);
4488 free_netdev(netdev);
4491 /* mlx5e_attach and mlx5e_detach scope should be only creating/destroying
4492 * hardware contexts and to connect it to the current netdev.
4494 static int mlx5e_attach(struct mlx5_core_dev *mdev, void *vpriv)
4496 struct mlx5e_priv *priv = vpriv;
4497 struct net_device *netdev = priv->netdev;
4500 if (netif_device_present(netdev))
4503 err = mlx5e_create_mdev_resources(mdev);
4507 err = mlx5e_attach_netdev(priv);
4509 mlx5e_destroy_mdev_resources(mdev);
4516 static void mlx5e_detach(struct mlx5_core_dev *mdev, void *vpriv)
4518 struct mlx5e_priv *priv = vpriv;
4519 struct net_device *netdev = priv->netdev;
4521 if (!netif_device_present(netdev))
4524 mlx5e_detach_netdev(priv);
4525 mlx5e_destroy_mdev_resources(mdev);
4528 static void *mlx5e_add(struct mlx5_core_dev *mdev)
4530 struct net_device *netdev;
4535 err = mlx5e_check_required_hca_cap(mdev);
4539 #ifdef CONFIG_MLX5_ESWITCH
4540 if (MLX5_VPORT_MANAGER(mdev)) {
4541 rpriv = mlx5e_alloc_nic_rep_priv(mdev);
4543 mlx5_core_warn(mdev, "Failed to alloc NIC rep priv data\n");
4549 netdev = mlx5e_create_netdev(mdev, &mlx5e_nic_profile, rpriv);
4551 mlx5_core_err(mdev, "mlx5e_create_netdev failed\n");
4552 goto err_free_rpriv;
4555 priv = netdev_priv(netdev);
4557 err = mlx5e_attach(mdev, priv);
4559 mlx5_core_err(mdev, "mlx5e_attach failed, %d\n", err);
4560 goto err_destroy_netdev;
4563 err = register_netdev(netdev);
4565 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
4569 #ifdef CONFIG_MLX5_CORE_EN_DCB
4570 mlx5e_dcbnl_init_app(priv);
4575 mlx5e_detach(mdev, priv);
4577 mlx5e_destroy_netdev(priv);
4583 static void mlx5e_remove(struct mlx5_core_dev *mdev, void *vpriv)
4585 struct mlx5e_priv *priv = vpriv;
4586 void *ppriv = priv->ppriv;
4588 #ifdef CONFIG_MLX5_CORE_EN_DCB
4589 mlx5e_dcbnl_delete_app(priv);
4591 unregister_netdev(priv->netdev);
4592 mlx5e_detach(mdev, vpriv);
4593 mlx5e_destroy_netdev(priv);
4597 static void *mlx5e_get_netdev(void *vpriv)
4599 struct mlx5e_priv *priv = vpriv;
4601 return priv->netdev;
4604 static struct mlx5_interface mlx5e_interface = {
4606 .remove = mlx5e_remove,
4607 .attach = mlx5e_attach,
4608 .detach = mlx5e_detach,
4609 .event = mlx5e_async_event,
4610 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
4611 .get_dev = mlx5e_get_netdev,
4614 void mlx5e_init(void)
4616 mlx5e_ipsec_build_inverse_table();
4617 mlx5e_build_ptys2ethtool_map();
4618 mlx5_register_interface(&mlx5e_interface);
4621 void mlx5e_cleanup(void)
4623 mlx5_unregister_interface(&mlx5e_interface);