2 * Copyright (c) 2015-2016, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <net/tc_act/tc_gact.h>
34 #include <net/pkt_cls.h>
35 #include <linux/mlx5/fs.h>
36 #include <net/vxlan.h>
37 #include <linux/bpf.h>
38 #include <net/page_pool.h>
43 #include "en_accel/ipsec.h"
44 #include "en_accel/ipsec_rxtx.h"
45 #include "accel/ipsec.h"
48 struct mlx5e_rq_param {
49 u32 rqc[MLX5_ST_SZ_DW(rqc)];
50 struct mlx5_wq_param wq;
53 struct mlx5e_sq_param {
54 u32 sqc[MLX5_ST_SZ_DW(sqc)];
55 struct mlx5_wq_param wq;
58 struct mlx5e_cq_param {
59 u32 cqc[MLX5_ST_SZ_DW(cqc)];
60 struct mlx5_wq_param wq;
65 struct mlx5e_channel_param {
66 struct mlx5e_rq_param rq;
67 struct mlx5e_sq_param sq;
68 struct mlx5e_sq_param xdp_sq;
69 struct mlx5e_sq_param icosq;
70 struct mlx5e_cq_param rx_cq;
71 struct mlx5e_cq_param tx_cq;
72 struct mlx5e_cq_param icosq_cq;
75 bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
77 bool striding_rq_umr = MLX5_CAP_GEN(mdev, striding_rq) &&
78 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
79 MLX5_CAP_ETH(mdev, reg_umr_sq);
80 u16 max_wqe_sz_cap = MLX5_CAP_GEN(mdev, max_wqe_sz_sq);
81 bool inline_umr = MLX5E_UMR_WQE_INLINE_SZ <= max_wqe_sz_cap;
86 mlx5_core_warn(mdev, "Cannot support Striding RQ: UMR WQE size (%d) exceeds maximum supported (%d).\n",
87 (int)MLX5E_UMR_WQE_INLINE_SZ, max_wqe_sz_cap);
93 static u32 mlx5e_mpwqe_get_linear_frag_sz(struct mlx5e_params *params)
95 if (!params->xdp_prog) {
96 u16 hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
97 u16 rq_headroom = MLX5_RX_HEADROOM + NET_IP_ALIGN;
99 return MLX5_SKB_FRAG_SZ(rq_headroom + hw_mtu);
105 static u8 mlx5e_mpwqe_log_pkts_per_wqe(struct mlx5e_params *params)
107 u32 linear_frag_sz = mlx5e_mpwqe_get_linear_frag_sz(params);
109 return MLX5_MPWRQ_LOG_WQE_SZ - order_base_2(linear_frag_sz);
112 static bool mlx5e_rx_mpwqe_is_linear_skb(struct mlx5_core_dev *mdev,
113 struct mlx5e_params *params)
115 u32 frag_sz = mlx5e_mpwqe_get_linear_frag_sz(params);
116 s8 signed_log_num_strides_param;
119 if (params->lro_en || frag_sz > PAGE_SIZE)
122 if (MLX5_CAP_GEN(mdev, ext_stride_num_range))
125 log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ - order_base_2(frag_sz);
126 signed_log_num_strides_param =
127 (s8)log_num_strides - MLX5_MPWQE_LOG_NUM_STRIDES_BASE;
129 return signed_log_num_strides_param >= 0;
132 static u8 mlx5e_mpwqe_get_log_rq_size(struct mlx5e_params *params)
134 if (params->log_rq_mtu_frames <
135 mlx5e_mpwqe_log_pkts_per_wqe(params) + MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW)
136 return MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW;
138 return params->log_rq_mtu_frames - mlx5e_mpwqe_log_pkts_per_wqe(params);
141 static u8 mlx5e_mpwqe_get_log_stride_size(struct mlx5_core_dev *mdev,
142 struct mlx5e_params *params)
144 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, params))
145 return order_base_2(mlx5e_mpwqe_get_linear_frag_sz(params));
147 return MLX5E_MPWQE_STRIDE_SZ(mdev,
148 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
151 static u8 mlx5e_mpwqe_get_log_num_strides(struct mlx5_core_dev *mdev,
152 struct mlx5e_params *params)
154 return MLX5_MPWRQ_LOG_WQE_SZ -
155 mlx5e_mpwqe_get_log_stride_size(mdev, params);
158 static u16 mlx5e_get_rq_headroom(struct mlx5_core_dev *mdev,
159 struct mlx5e_params *params)
161 u16 linear_rq_headroom = params->xdp_prog ?
162 XDP_PACKET_HEADROOM : MLX5_RX_HEADROOM;
164 linear_rq_headroom += NET_IP_ALIGN;
166 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST)
167 return linear_rq_headroom;
169 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, params))
170 return linear_rq_headroom;
175 void mlx5e_init_rq_type_params(struct mlx5_core_dev *mdev,
176 struct mlx5e_params *params)
178 params->lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
179 params->log_rq_mtu_frames = is_kdump_kernel() ?
180 MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE :
181 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
182 switch (params->rq_wq_type) {
183 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
185 default: /* MLX5_WQ_TYPE_LINKED_LIST */
186 /* Extra room needed for build_skb */
187 params->lro_wqe_sz -= mlx5e_get_rq_headroom(mdev, params) +
188 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
191 mlx5_core_info(mdev, "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
192 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
193 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ ?
194 BIT(mlx5e_mpwqe_get_log_rq_size(params)) :
195 BIT(params->log_rq_mtu_frames),
196 BIT(mlx5e_mpwqe_get_log_stride_size(mdev, params)),
197 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
200 bool mlx5e_striding_rq_possible(struct mlx5_core_dev *mdev,
201 struct mlx5e_params *params)
203 return mlx5e_check_fragmented_striding_rq_cap(mdev) &&
204 !MLX5_IPSEC_DEV(mdev) &&
205 !(params->xdp_prog && !mlx5e_rx_mpwqe_is_linear_skb(mdev, params));
208 void mlx5e_set_rq_type(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
210 params->rq_wq_type = mlx5e_striding_rq_possible(mdev, params) &&
211 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ) ?
212 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
213 MLX5_WQ_TYPE_LINKED_LIST;
216 static void mlx5e_update_carrier(struct mlx5e_priv *priv)
218 struct mlx5_core_dev *mdev = priv->mdev;
221 port_state = mlx5_query_vport_state(mdev,
222 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT,
225 if (port_state == VPORT_STATE_UP) {
226 netdev_info(priv->netdev, "Link up\n");
227 netif_carrier_on(priv->netdev);
229 netdev_info(priv->netdev, "Link down\n");
230 netif_carrier_off(priv->netdev);
234 static void mlx5e_update_carrier_work(struct work_struct *work)
236 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
237 update_carrier_work);
239 mutex_lock(&priv->state_lock);
240 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
241 if (priv->profile->update_carrier)
242 priv->profile->update_carrier(priv);
243 mutex_unlock(&priv->state_lock);
246 void mlx5e_update_stats(struct mlx5e_priv *priv)
250 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
251 if (mlx5e_stats_grps[i].update_stats)
252 mlx5e_stats_grps[i].update_stats(priv);
255 static void mlx5e_update_ndo_stats(struct mlx5e_priv *priv)
259 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
260 if (mlx5e_stats_grps[i].update_stats_mask &
261 MLX5E_NDO_UPDATE_STATS)
262 mlx5e_stats_grps[i].update_stats(priv);
265 void mlx5e_update_stats_work(struct work_struct *work)
267 struct delayed_work *dwork = to_delayed_work(work);
268 struct mlx5e_priv *priv = container_of(dwork, struct mlx5e_priv,
270 mutex_lock(&priv->state_lock);
271 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
272 priv->profile->update_stats(priv);
273 queue_delayed_work(priv->wq, dwork,
274 msecs_to_jiffies(MLX5E_UPDATE_STATS_INTERVAL));
276 mutex_unlock(&priv->state_lock);
279 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
280 enum mlx5_dev_event event, unsigned long param)
282 struct mlx5e_priv *priv = vpriv;
284 if (!test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state))
288 case MLX5_DEV_EVENT_PORT_UP:
289 case MLX5_DEV_EVENT_PORT_DOWN:
290 queue_work(priv->wq, &priv->update_carrier_work);
297 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
299 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
302 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
304 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
305 synchronize_irq(pci_irq_vector(priv->mdev->pdev, MLX5_EQ_VEC_ASYNC));
308 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq,
309 struct mlx5e_icosq *sq,
310 struct mlx5e_umr_wqe *wqe)
312 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
313 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
314 u8 ds_cnt = DIV_ROUND_UP(MLX5E_UMR_WQE_INLINE_SZ, MLX5_SEND_WQE_DS);
316 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
318 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
319 cseg->imm = rq->mkey_be;
321 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN | MLX5_UMR_INLINE;
322 ucseg->xlt_octowords =
323 cpu_to_be16(MLX5_MTT_OCTW(MLX5_MPWRQ_PAGES_PER_WQE));
324 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
327 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq,
328 struct mlx5e_channel *c)
330 int wq_sz = mlx5_wq_ll_get_size(&rq->wq);
332 rq->mpwqe.info = kzalloc_node(wq_sz * sizeof(*rq->mpwqe.info),
333 GFP_KERNEL, cpu_to_node(c->cpu));
337 mlx5e_build_umr_wqe(rq, &c->icosq, &rq->mpwqe.umr_wqe);
342 static int mlx5e_create_umr_mkey(struct mlx5_core_dev *mdev,
343 u64 npages, u8 page_shift,
344 struct mlx5_core_mkey *umr_mkey)
346 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
351 in = kvzalloc(inlen, GFP_KERNEL);
355 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
357 MLX5_SET(mkc, mkc, free, 1);
358 MLX5_SET(mkc, mkc, umr_en, 1);
359 MLX5_SET(mkc, mkc, lw, 1);
360 MLX5_SET(mkc, mkc, lr, 1);
361 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT);
363 MLX5_SET(mkc, mkc, qpn, 0xffffff);
364 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.pdn);
365 MLX5_SET64(mkc, mkc, len, npages << page_shift);
366 MLX5_SET(mkc, mkc, translations_octword_size,
367 MLX5_MTT_OCTW(npages));
368 MLX5_SET(mkc, mkc, log_page_size, page_shift);
370 err = mlx5_core_create_mkey(mdev, umr_mkey, in, inlen);
376 static int mlx5e_create_rq_umr_mkey(struct mlx5_core_dev *mdev, struct mlx5e_rq *rq)
378 u64 num_mtts = MLX5E_REQUIRED_MTTS(mlx5_wq_ll_get_size(&rq->wq));
380 return mlx5e_create_umr_mkey(mdev, num_mtts, PAGE_SHIFT, &rq->umr_mkey);
383 static inline u64 mlx5e_get_mpwqe_offset(struct mlx5e_rq *rq, u16 wqe_ix)
385 return (wqe_ix << MLX5E_LOG_ALIGNED_MPWQE_PPW) << PAGE_SHIFT;
388 static int mlx5e_alloc_rq(struct mlx5e_channel *c,
389 struct mlx5e_params *params,
390 struct mlx5e_rq_param *rqp,
393 struct page_pool_params pp_params = { 0 };
394 struct mlx5_core_dev *mdev = c->mdev;
395 void *rqc = rqp->rqc;
396 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
397 u32 byte_count, pool_size;
403 rqp->wq.db_numa_node = cpu_to_node(c->cpu);
405 err = mlx5_wq_ll_create(mdev, &rqp->wq, rqc_wq, &rq->wq,
410 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
412 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
414 rq->wq_type = params->rq_wq_type;
416 rq->netdev = c->netdev;
417 rq->tstamp = c->tstamp;
418 rq->clock = &mdev->clock;
422 rq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
424 rq->xdp_prog = params->xdp_prog ? bpf_prog_inc(params->xdp_prog) : NULL;
425 if (IS_ERR(rq->xdp_prog)) {
426 err = PTR_ERR(rq->xdp_prog);
428 goto err_rq_wq_destroy;
431 err = xdp_rxq_info_reg(&rq->xdp_rxq, rq->netdev, rq->ix);
433 goto err_rq_wq_destroy;
435 rq->buff.map_dir = rq->xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
436 rq->buff.headroom = mlx5e_get_rq_headroom(mdev, params);
437 pool_size = 1 << params->log_rq_mtu_frames;
439 switch (rq->wq_type) {
440 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
442 pool_size = MLX5_MPWRQ_PAGES_PER_WQE << mlx5e_mpwqe_get_log_rq_size(params);
443 rq->post_wqes = mlx5e_post_rx_mpwqes;
444 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
446 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe_mpwqe;
447 #ifdef CONFIG_MLX5_EN_IPSEC
448 if (MLX5_IPSEC_DEV(mdev)) {
450 netdev_err(c->netdev, "MPWQE RQ with IPSec offload not supported\n");
451 goto err_rq_wq_destroy;
454 if (!rq->handle_rx_cqe) {
456 netdev_err(c->netdev, "RX handler of MPWQE RQ is not set, err %d\n", err);
457 goto err_rq_wq_destroy;
460 rq->mpwqe.skb_from_cqe_mpwrq =
461 mlx5e_rx_mpwqe_is_linear_skb(mdev, params) ?
462 mlx5e_skb_from_cqe_mpwrq_linear :
463 mlx5e_skb_from_cqe_mpwrq_nonlinear;
464 rq->mpwqe.log_stride_sz = mlx5e_mpwqe_get_log_stride_size(mdev, params);
465 rq->mpwqe.num_strides = BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params));
467 byte_count = rq->mpwqe.num_strides << rq->mpwqe.log_stride_sz;
469 err = mlx5e_create_rq_umr_mkey(mdev, rq);
471 goto err_rq_wq_destroy;
472 rq->mkey_be = cpu_to_be32(rq->umr_mkey.key);
474 err = mlx5e_rq_alloc_mpwqe_info(rq, c);
476 goto err_destroy_umr_mkey;
478 default: /* MLX5_WQ_TYPE_LINKED_LIST */
480 kzalloc_node(wq_sz * sizeof(*rq->wqe.frag_info),
481 GFP_KERNEL, cpu_to_node(c->cpu));
482 if (!rq->wqe.frag_info) {
484 goto err_rq_wq_destroy;
486 rq->post_wqes = mlx5e_post_rx_wqes;
487 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
489 #ifdef CONFIG_MLX5_EN_IPSEC
491 rq->handle_rx_cqe = mlx5e_ipsec_handle_rx_cqe;
494 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe;
495 if (!rq->handle_rx_cqe) {
496 kfree(rq->wqe.frag_info);
498 netdev_err(c->netdev, "RX handler of RQ is not set, err %d\n", err);
499 goto err_rq_wq_destroy;
502 byte_count = params->lro_en ?
504 MLX5E_SW2HW_MTU(params, params->sw_mtu);
505 #ifdef CONFIG_MLX5_EN_IPSEC
506 if (MLX5_IPSEC_DEV(mdev))
507 byte_count += MLX5E_METADATA_ETHER_LEN;
509 rq->wqe.page_reuse = !params->xdp_prog && !params->lro_en;
511 /* calc the required page order */
512 rq->wqe.frag_sz = MLX5_SKB_FRAG_SZ(rq->buff.headroom + byte_count);
513 npages = DIV_ROUND_UP(rq->wqe.frag_sz, PAGE_SIZE);
514 rq->buff.page_order = order_base_2(npages);
516 byte_count |= MLX5_HW_START_PADDING;
517 rq->mkey_be = c->mkey_be;
520 /* Create a page_pool and register it with rxq */
521 pp_params.order = rq->buff.page_order;
522 pp_params.flags = 0; /* No-internal DMA mapping in page_pool */
523 pp_params.pool_size = pool_size;
524 pp_params.nid = cpu_to_node(c->cpu);
525 pp_params.dev = c->pdev;
526 pp_params.dma_dir = rq->buff.map_dir;
528 /* page_pool can be used even when there is no rq->xdp_prog,
529 * given page_pool does not handle DMA mapping there is no
530 * required state to clear. And page_pool gracefully handle
533 rq->page_pool = page_pool_create(&pp_params);
534 if (IS_ERR(rq->page_pool)) {
535 if (rq->wq_type != MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
536 kfree(rq->wqe.frag_info);
537 err = PTR_ERR(rq->page_pool);
538 rq->page_pool = NULL;
539 goto err_rq_wq_destroy;
541 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
542 MEM_TYPE_PAGE_POOL, rq->page_pool);
544 goto err_rq_wq_destroy;
546 for (i = 0; i < wq_sz; i++) {
547 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
549 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
550 u64 dma_offset = mlx5e_get_mpwqe_offset(rq, i);
552 wqe->data.addr = cpu_to_be64(dma_offset + rq->buff.headroom);
555 wqe->data.byte_count = cpu_to_be32(byte_count);
556 wqe->data.lkey = rq->mkey_be;
559 INIT_WORK(&rq->dim.work, mlx5e_rx_dim_work);
561 switch (params->rx_cq_moderation.cq_period_mode) {
562 case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
563 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE;
565 case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
567 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
570 rq->page_cache.head = 0;
571 rq->page_cache.tail = 0;
575 err_destroy_umr_mkey:
576 mlx5_core_destroy_mkey(mdev, &rq->umr_mkey);
580 bpf_prog_put(rq->xdp_prog);
581 xdp_rxq_info_unreg(&rq->xdp_rxq);
583 page_pool_destroy(rq->page_pool);
584 mlx5_wq_destroy(&rq->wq_ctrl);
589 static void mlx5e_free_rq(struct mlx5e_rq *rq)
594 bpf_prog_put(rq->xdp_prog);
596 xdp_rxq_info_unreg(&rq->xdp_rxq);
598 page_pool_destroy(rq->page_pool);
600 switch (rq->wq_type) {
601 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
602 kfree(rq->mpwqe.info);
603 mlx5_core_destroy_mkey(rq->mdev, &rq->umr_mkey);
605 default: /* MLX5_WQ_TYPE_LINKED_LIST */
606 kfree(rq->wqe.frag_info);
609 for (i = rq->page_cache.head; i != rq->page_cache.tail;
610 i = (i + 1) & (MLX5E_CACHE_SIZE - 1)) {
611 struct mlx5e_dma_info *dma_info = &rq->page_cache.page_cache[i];
613 mlx5e_page_release(rq, dma_info, false);
615 mlx5_wq_destroy(&rq->wq_ctrl);
618 static int mlx5e_create_rq(struct mlx5e_rq *rq,
619 struct mlx5e_rq_param *param)
621 struct mlx5_core_dev *mdev = rq->mdev;
629 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
630 sizeof(u64) * rq->wq_ctrl.buf.npages;
631 in = kvzalloc(inlen, GFP_KERNEL);
635 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
636 wq = MLX5_ADDR_OF(rqc, rqc, wq);
638 memcpy(rqc, param->rqc, sizeof(param->rqc));
640 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
641 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
642 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
643 MLX5_ADAPTER_PAGE_SHIFT);
644 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
646 mlx5_fill_page_array(&rq->wq_ctrl.buf,
647 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
649 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
656 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
659 struct mlx5_core_dev *mdev = rq->mdev;
666 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
667 in = kvzalloc(inlen, GFP_KERNEL);
671 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
673 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
674 MLX5_SET(rqc, rqc, state, next_state);
676 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
683 static int mlx5e_modify_rq_scatter_fcs(struct mlx5e_rq *rq, bool enable)
685 struct mlx5e_channel *c = rq->channel;
686 struct mlx5e_priv *priv = c->priv;
687 struct mlx5_core_dev *mdev = priv->mdev;
694 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
695 in = kvzalloc(inlen, GFP_KERNEL);
699 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
701 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
702 MLX5_SET64(modify_rq_in, in, modify_bitmask,
703 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_SCATTER_FCS);
704 MLX5_SET(rqc, rqc, scatter_fcs, enable);
705 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
707 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
714 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
716 struct mlx5e_channel *c = rq->channel;
717 struct mlx5_core_dev *mdev = c->mdev;
723 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
724 in = kvzalloc(inlen, GFP_KERNEL);
728 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
730 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
731 MLX5_SET64(modify_rq_in, in, modify_bitmask,
732 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
733 MLX5_SET(rqc, rqc, vsd, vsd);
734 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
736 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
743 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
745 mlx5_core_destroy_rq(rq->mdev, rq->rqn);
748 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
750 unsigned long exp_time = jiffies + msecs_to_jiffies(20000);
751 struct mlx5e_channel *c = rq->channel;
753 struct mlx5_wq_ll *wq = &rq->wq;
754 u16 min_wqes = mlx5_min_rx_wqes(rq->wq_type, mlx5_wq_ll_get_size(wq));
756 while (time_before(jiffies, exp_time)) {
757 if (wq->cur_sz >= min_wqes)
763 netdev_warn(c->netdev, "Failed to get min RX wqes on RQN[0x%x] wq cur_sz(%d) min_rx_wqes(%d)\n",
764 rq->rqn, wq->cur_sz, min_wqes);
768 static void mlx5e_free_rx_descs(struct mlx5e_rq *rq)
770 struct mlx5_wq_ll *wq = &rq->wq;
771 struct mlx5e_rx_wqe *wqe;
775 /* UMR WQE (if in progress) is always at wq->head */
776 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ &&
777 rq->mpwqe.umr_in_progress)
778 mlx5e_free_rx_mpwqe(rq, &rq->mpwqe.info[wq->head]);
780 while (!mlx5_wq_ll_is_empty(wq)) {
781 wqe_ix_be = *wq->tail_next;
782 wqe_ix = be16_to_cpu(wqe_ix_be);
783 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_ix);
784 rq->dealloc_wqe(rq, wqe_ix);
785 mlx5_wq_ll_pop(&rq->wq, wqe_ix_be,
786 &wqe->next.next_wqe_index);
789 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST && rq->wqe.page_reuse) {
790 /* Clean outstanding pages on handled WQEs that decided to do page-reuse,
791 * but yet to be re-posted.
793 int wq_sz = mlx5_wq_ll_get_size(&rq->wq);
795 for (wqe_ix = 0; wqe_ix < wq_sz; wqe_ix++)
796 rq->dealloc_wqe(rq, wqe_ix);
800 static int mlx5e_open_rq(struct mlx5e_channel *c,
801 struct mlx5e_params *params,
802 struct mlx5e_rq_param *param,
807 err = mlx5e_alloc_rq(c, params, param, rq);
811 err = mlx5e_create_rq(rq, param);
815 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
819 if (params->rx_dim_enabled)
820 c->rq.state |= BIT(MLX5E_RQ_STATE_AM);
825 mlx5e_destroy_rq(rq);
832 static void mlx5e_activate_rq(struct mlx5e_rq *rq)
834 struct mlx5e_icosq *sq = &rq->channel->icosq;
835 u16 pi = sq->pc & sq->wq.sz_m1;
836 struct mlx5e_tx_wqe *nopwqe;
838 set_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
839 sq->db.ico_wqe[pi].opcode = MLX5_OPCODE_NOP;
840 nopwqe = mlx5e_post_nop(&sq->wq, sq->sqn, &sq->pc);
841 mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, &nopwqe->ctrl);
844 static void mlx5e_deactivate_rq(struct mlx5e_rq *rq)
846 clear_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
847 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
850 static void mlx5e_close_rq(struct mlx5e_rq *rq)
852 cancel_work_sync(&rq->dim.work);
853 mlx5e_destroy_rq(rq);
854 mlx5e_free_rx_descs(rq);
858 static void mlx5e_free_xdpsq_db(struct mlx5e_xdpsq *sq)
863 static int mlx5e_alloc_xdpsq_db(struct mlx5e_xdpsq *sq, int numa)
865 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
867 sq->db.di = kzalloc_node(sizeof(*sq->db.di) * wq_sz,
870 mlx5e_free_xdpsq_db(sq);
877 static int mlx5e_alloc_xdpsq(struct mlx5e_channel *c,
878 struct mlx5e_params *params,
879 struct mlx5e_sq_param *param,
880 struct mlx5e_xdpsq *sq)
882 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
883 struct mlx5_core_dev *mdev = c->mdev;
887 sq->mkey_be = c->mkey_be;
889 sq->uar_map = mdev->mlx5e_res.bfreg.map;
890 sq->min_inline_mode = params->tx_min_inline_mode;
892 param->wq.db_numa_node = cpu_to_node(c->cpu);
893 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq, &sq->wq_ctrl);
896 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
898 err = mlx5e_alloc_xdpsq_db(sq, cpu_to_node(c->cpu));
900 goto err_sq_wq_destroy;
905 mlx5_wq_destroy(&sq->wq_ctrl);
910 static void mlx5e_free_xdpsq(struct mlx5e_xdpsq *sq)
912 mlx5e_free_xdpsq_db(sq);
913 mlx5_wq_destroy(&sq->wq_ctrl);
916 static void mlx5e_free_icosq_db(struct mlx5e_icosq *sq)
918 kfree(sq->db.ico_wqe);
921 static int mlx5e_alloc_icosq_db(struct mlx5e_icosq *sq, int numa)
923 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
925 sq->db.ico_wqe = kzalloc_node(sizeof(*sq->db.ico_wqe) * wq_sz,
933 static int mlx5e_alloc_icosq(struct mlx5e_channel *c,
934 struct mlx5e_sq_param *param,
935 struct mlx5e_icosq *sq)
937 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
938 struct mlx5_core_dev *mdev = c->mdev;
942 sq->uar_map = mdev->mlx5e_res.bfreg.map;
944 param->wq.db_numa_node = cpu_to_node(c->cpu);
945 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq, &sq->wq_ctrl);
948 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
950 err = mlx5e_alloc_icosq_db(sq, cpu_to_node(c->cpu));
952 goto err_sq_wq_destroy;
954 sq->edge = (sq->wq.sz_m1 + 1) - MLX5E_ICOSQ_MAX_WQEBBS;
959 mlx5_wq_destroy(&sq->wq_ctrl);
964 static void mlx5e_free_icosq(struct mlx5e_icosq *sq)
966 mlx5e_free_icosq_db(sq);
967 mlx5_wq_destroy(&sq->wq_ctrl);
970 static void mlx5e_free_txqsq_db(struct mlx5e_txqsq *sq)
972 kfree(sq->db.wqe_info);
973 kfree(sq->db.dma_fifo);
976 static int mlx5e_alloc_txqsq_db(struct mlx5e_txqsq *sq, int numa)
978 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
979 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
981 sq->db.dma_fifo = kzalloc_node(df_sz * sizeof(*sq->db.dma_fifo),
983 sq->db.wqe_info = kzalloc_node(wq_sz * sizeof(*sq->db.wqe_info),
985 if (!sq->db.dma_fifo || !sq->db.wqe_info) {
986 mlx5e_free_txqsq_db(sq);
990 sq->dma_fifo_mask = df_sz - 1;
995 static void mlx5e_sq_recover(struct work_struct *work);
996 static int mlx5e_alloc_txqsq(struct mlx5e_channel *c,
998 struct mlx5e_params *params,
999 struct mlx5e_sq_param *param,
1000 struct mlx5e_txqsq *sq)
1002 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1003 struct mlx5_core_dev *mdev = c->mdev;
1007 sq->tstamp = c->tstamp;
1008 sq->clock = &mdev->clock;
1009 sq->mkey_be = c->mkey_be;
1011 sq->txq_ix = txq_ix;
1012 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1013 sq->min_inline_mode = params->tx_min_inline_mode;
1014 INIT_WORK(&sq->recover.recover_work, mlx5e_sq_recover);
1015 if (MLX5_IPSEC_DEV(c->priv->mdev))
1016 set_bit(MLX5E_SQ_STATE_IPSEC, &sq->state);
1018 param->wq.db_numa_node = cpu_to_node(c->cpu);
1019 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq, &sq->wq_ctrl);
1022 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
1024 err = mlx5e_alloc_txqsq_db(sq, cpu_to_node(c->cpu));
1026 goto err_sq_wq_destroy;
1028 sq->edge = (sq->wq.sz_m1 + 1) - MLX5_SEND_WQE_MAX_WQEBBS;
1033 mlx5_wq_destroy(&sq->wq_ctrl);
1038 static void mlx5e_free_txqsq(struct mlx5e_txqsq *sq)
1040 mlx5e_free_txqsq_db(sq);
1041 mlx5_wq_destroy(&sq->wq_ctrl);
1044 struct mlx5e_create_sq_param {
1045 struct mlx5_wq_ctrl *wq_ctrl;
1052 static int mlx5e_create_sq(struct mlx5_core_dev *mdev,
1053 struct mlx5e_sq_param *param,
1054 struct mlx5e_create_sq_param *csp,
1063 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1064 sizeof(u64) * csp->wq_ctrl->buf.npages;
1065 in = kvzalloc(inlen, GFP_KERNEL);
1069 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1070 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1072 memcpy(sqc, param->sqc, sizeof(param->sqc));
1073 MLX5_SET(sqc, sqc, tis_lst_sz, csp->tis_lst_sz);
1074 MLX5_SET(sqc, sqc, tis_num_0, csp->tisn);
1075 MLX5_SET(sqc, sqc, cqn, csp->cqn);
1077 if (MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5_CAP_INLINE_MODE_VPORT_CONTEXT)
1078 MLX5_SET(sqc, sqc, min_wqe_inline_mode, csp->min_inline_mode);
1080 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1081 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1083 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1084 MLX5_SET(wq, wq, uar_page, mdev->mlx5e_res.bfreg.index);
1085 MLX5_SET(wq, wq, log_wq_pg_sz, csp->wq_ctrl->buf.page_shift -
1086 MLX5_ADAPTER_PAGE_SHIFT);
1087 MLX5_SET64(wq, wq, dbr_addr, csp->wq_ctrl->db.dma);
1089 mlx5_fill_page_array(&csp->wq_ctrl->buf, (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
1091 err = mlx5_core_create_sq(mdev, in, inlen, sqn);
1098 struct mlx5e_modify_sq_param {
1105 static int mlx5e_modify_sq(struct mlx5_core_dev *mdev, u32 sqn,
1106 struct mlx5e_modify_sq_param *p)
1113 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1114 in = kvzalloc(inlen, GFP_KERNEL);
1118 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1120 MLX5_SET(modify_sq_in, in, sq_state, p->curr_state);
1121 MLX5_SET(sqc, sqc, state, p->next_state);
1122 if (p->rl_update && p->next_state == MLX5_SQC_STATE_RDY) {
1123 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
1124 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, p->rl_index);
1127 err = mlx5_core_modify_sq(mdev, sqn, in, inlen);
1134 static void mlx5e_destroy_sq(struct mlx5_core_dev *mdev, u32 sqn)
1136 mlx5_core_destroy_sq(mdev, sqn);
1139 static int mlx5e_create_sq_rdy(struct mlx5_core_dev *mdev,
1140 struct mlx5e_sq_param *param,
1141 struct mlx5e_create_sq_param *csp,
1144 struct mlx5e_modify_sq_param msp = {0};
1147 err = mlx5e_create_sq(mdev, param, csp, sqn);
1151 msp.curr_state = MLX5_SQC_STATE_RST;
1152 msp.next_state = MLX5_SQC_STATE_RDY;
1153 err = mlx5e_modify_sq(mdev, *sqn, &msp);
1155 mlx5e_destroy_sq(mdev, *sqn);
1160 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1161 struct mlx5e_txqsq *sq, u32 rate);
1163 static int mlx5e_open_txqsq(struct mlx5e_channel *c,
1166 struct mlx5e_params *params,
1167 struct mlx5e_sq_param *param,
1168 struct mlx5e_txqsq *sq)
1170 struct mlx5e_create_sq_param csp = {};
1174 err = mlx5e_alloc_txqsq(c, txq_ix, params, param, sq);
1180 csp.cqn = sq->cq.mcq.cqn;
1181 csp.wq_ctrl = &sq->wq_ctrl;
1182 csp.min_inline_mode = sq->min_inline_mode;
1183 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1185 goto err_free_txqsq;
1187 tx_rate = c->priv->tx_rates[sq->txq_ix];
1189 mlx5e_set_sq_maxrate(c->netdev, sq, tx_rate);
1194 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1195 mlx5e_free_txqsq(sq);
1200 static void mlx5e_reset_txqsq_cc_pc(struct mlx5e_txqsq *sq)
1202 WARN_ONCE(sq->cc != sq->pc,
1203 "SQ 0x%x: cc (0x%x) != pc (0x%x)\n",
1204 sq->sqn, sq->cc, sq->pc);
1206 sq->dma_fifo_cc = 0;
1210 static void mlx5e_activate_txqsq(struct mlx5e_txqsq *sq)
1212 sq->txq = netdev_get_tx_queue(sq->channel->netdev, sq->txq_ix);
1213 clear_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state);
1214 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1215 netdev_tx_reset_queue(sq->txq);
1216 netif_tx_start_queue(sq->txq);
1219 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
1221 __netif_tx_lock_bh(txq);
1222 netif_tx_stop_queue(txq);
1223 __netif_tx_unlock_bh(txq);
1226 static void mlx5e_deactivate_txqsq(struct mlx5e_txqsq *sq)
1228 struct mlx5e_channel *c = sq->channel;
1230 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1231 /* prevent netif_tx_wake_queue */
1232 napi_synchronize(&c->napi);
1234 netif_tx_disable_queue(sq->txq);
1236 /* last doorbell out, godspeed .. */
1237 if (mlx5e_wqc_has_room_for(&sq->wq, sq->cc, sq->pc, 1)) {
1238 struct mlx5e_tx_wqe *nop;
1240 sq->db.wqe_info[(sq->pc & sq->wq.sz_m1)].skb = NULL;
1241 nop = mlx5e_post_nop(&sq->wq, sq->sqn, &sq->pc);
1242 mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, &nop->ctrl);
1246 static void mlx5e_close_txqsq(struct mlx5e_txqsq *sq)
1248 struct mlx5e_channel *c = sq->channel;
1249 struct mlx5_core_dev *mdev = c->mdev;
1250 struct mlx5_rate_limit rl = {0};
1252 mlx5e_destroy_sq(mdev, sq->sqn);
1253 if (sq->rate_limit) {
1254 rl.rate = sq->rate_limit;
1255 mlx5_rl_remove_rate(mdev, &rl);
1257 mlx5e_free_txqsq_descs(sq);
1258 mlx5e_free_txqsq(sq);
1261 static int mlx5e_wait_for_sq_flush(struct mlx5e_txqsq *sq)
1263 unsigned long exp_time = jiffies + msecs_to_jiffies(2000);
1265 while (time_before(jiffies, exp_time)) {
1266 if (sq->cc == sq->pc)
1272 netdev_err(sq->channel->netdev,
1273 "Wait for SQ 0x%x flush timeout (sq cc = 0x%x, sq pc = 0x%x)\n",
1274 sq->sqn, sq->cc, sq->pc);
1279 static int mlx5e_sq_to_ready(struct mlx5e_txqsq *sq, int curr_state)
1281 struct mlx5_core_dev *mdev = sq->channel->mdev;
1282 struct net_device *dev = sq->channel->netdev;
1283 struct mlx5e_modify_sq_param msp = {0};
1286 msp.curr_state = curr_state;
1287 msp.next_state = MLX5_SQC_STATE_RST;
1289 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1291 netdev_err(dev, "Failed to move sq 0x%x to reset\n", sq->sqn);
1295 memset(&msp, 0, sizeof(msp));
1296 msp.curr_state = MLX5_SQC_STATE_RST;
1297 msp.next_state = MLX5_SQC_STATE_RDY;
1299 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1301 netdev_err(dev, "Failed to move sq 0x%x to ready\n", sq->sqn);
1308 static void mlx5e_sq_recover(struct work_struct *work)
1310 struct mlx5e_txqsq_recover *recover =
1311 container_of(work, struct mlx5e_txqsq_recover,
1313 struct mlx5e_txqsq *sq = container_of(recover, struct mlx5e_txqsq,
1315 struct mlx5_core_dev *mdev = sq->channel->mdev;
1316 struct net_device *dev = sq->channel->netdev;
1320 err = mlx5_core_query_sq_state(mdev, sq->sqn, &state);
1322 netdev_err(dev, "Failed to query SQ 0x%x state. err = %d\n",
1327 if (state != MLX5_RQC_STATE_ERR) {
1328 netdev_err(dev, "SQ 0x%x not in ERROR state\n", sq->sqn);
1332 netif_tx_disable_queue(sq->txq);
1334 if (mlx5e_wait_for_sq_flush(sq))
1337 /* If the interval between two consecutive recovers per SQ is too
1338 * short, don't recover to avoid infinite loop of ERR_CQE -> recover.
1339 * If we reached this state, there is probably a bug that needs to be
1340 * fixed. let's keep the queue close and let tx timeout cleanup.
1342 if (jiffies_to_msecs(jiffies - recover->last_recover) <
1343 MLX5E_SQ_RECOVER_MIN_INTERVAL) {
1344 netdev_err(dev, "Recover SQ 0x%x canceled, too many error CQEs\n",
1349 /* At this point, no new packets will arrive from the stack as TXQ is
1350 * marked with QUEUE_STATE_DRV_XOFF. In addition, NAPI cleared all
1351 * pending WQEs. SQ can safely reset the SQ.
1353 if (mlx5e_sq_to_ready(sq, state))
1356 mlx5e_reset_txqsq_cc_pc(sq);
1357 sq->stats.recover++;
1358 recover->last_recover = jiffies;
1359 mlx5e_activate_txqsq(sq);
1362 static int mlx5e_open_icosq(struct mlx5e_channel *c,
1363 struct mlx5e_params *params,
1364 struct mlx5e_sq_param *param,
1365 struct mlx5e_icosq *sq)
1367 struct mlx5e_create_sq_param csp = {};
1370 err = mlx5e_alloc_icosq(c, param, sq);
1374 csp.cqn = sq->cq.mcq.cqn;
1375 csp.wq_ctrl = &sq->wq_ctrl;
1376 csp.min_inline_mode = params->tx_min_inline_mode;
1377 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1378 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1380 goto err_free_icosq;
1385 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1386 mlx5e_free_icosq(sq);
1391 static void mlx5e_close_icosq(struct mlx5e_icosq *sq)
1393 struct mlx5e_channel *c = sq->channel;
1395 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1396 napi_synchronize(&c->napi);
1398 mlx5e_destroy_sq(c->mdev, sq->sqn);
1399 mlx5e_free_icosq(sq);
1402 static int mlx5e_open_xdpsq(struct mlx5e_channel *c,
1403 struct mlx5e_params *params,
1404 struct mlx5e_sq_param *param,
1405 struct mlx5e_xdpsq *sq)
1407 unsigned int ds_cnt = MLX5E_XDP_TX_DS_COUNT;
1408 struct mlx5e_create_sq_param csp = {};
1409 unsigned int inline_hdr_sz = 0;
1413 err = mlx5e_alloc_xdpsq(c, params, param, sq);
1418 csp.tisn = c->priv->tisn[0]; /* tc = 0 */
1419 csp.cqn = sq->cq.mcq.cqn;
1420 csp.wq_ctrl = &sq->wq_ctrl;
1421 csp.min_inline_mode = sq->min_inline_mode;
1422 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1423 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1425 goto err_free_xdpsq;
1427 if (sq->min_inline_mode != MLX5_INLINE_MODE_NONE) {
1428 inline_hdr_sz = MLX5E_XDP_MIN_INLINE;
1432 /* Pre initialize fixed WQE fields */
1433 for (i = 0; i < mlx5_wq_cyc_get_size(&sq->wq); i++) {
1434 struct mlx5e_tx_wqe *wqe = mlx5_wq_cyc_get_wqe(&sq->wq, i);
1435 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
1436 struct mlx5_wqe_eth_seg *eseg = &wqe->eth;
1437 struct mlx5_wqe_data_seg *dseg;
1439 cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
1440 eseg->inline_hdr.sz = cpu_to_be16(inline_hdr_sz);
1442 dseg = (struct mlx5_wqe_data_seg *)cseg + (ds_cnt - 1);
1443 dseg->lkey = sq->mkey_be;
1449 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1450 mlx5e_free_xdpsq(sq);
1455 static void mlx5e_close_xdpsq(struct mlx5e_xdpsq *sq)
1457 struct mlx5e_channel *c = sq->channel;
1459 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1460 napi_synchronize(&c->napi);
1462 mlx5e_destroy_sq(c->mdev, sq->sqn);
1463 mlx5e_free_xdpsq_descs(sq);
1464 mlx5e_free_xdpsq(sq);
1467 static int mlx5e_alloc_cq_common(struct mlx5_core_dev *mdev,
1468 struct mlx5e_cq_param *param,
1469 struct mlx5e_cq *cq)
1471 struct mlx5_core_cq *mcq = &cq->mcq;
1477 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1482 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1485 mcq->set_ci_db = cq->wq_ctrl.db.db;
1486 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1487 *mcq->set_ci_db = 0;
1489 mcq->vector = param->eq_ix;
1490 mcq->comp = mlx5e_completion_event;
1491 mcq->event = mlx5e_cq_error_event;
1494 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1495 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1505 static int mlx5e_alloc_cq(struct mlx5e_channel *c,
1506 struct mlx5e_cq_param *param,
1507 struct mlx5e_cq *cq)
1509 struct mlx5_core_dev *mdev = c->priv->mdev;
1512 param->wq.buf_numa_node = cpu_to_node(c->cpu);
1513 param->wq.db_numa_node = cpu_to_node(c->cpu);
1514 param->eq_ix = c->ix;
1516 err = mlx5e_alloc_cq_common(mdev, param, cq);
1518 cq->napi = &c->napi;
1524 static void mlx5e_free_cq(struct mlx5e_cq *cq)
1526 mlx5_cqwq_destroy(&cq->wq_ctrl);
1529 static int mlx5e_create_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
1531 struct mlx5_core_dev *mdev = cq->mdev;
1532 struct mlx5_core_cq *mcq = &cq->mcq;
1537 unsigned int irqn_not_used;
1541 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1542 sizeof(u64) * cq->wq_ctrl.frag_buf.npages;
1543 in = kvzalloc(inlen, GFP_KERNEL);
1547 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1549 memcpy(cqc, param->cqc, sizeof(param->cqc));
1551 mlx5_fill_page_frag_array(&cq->wq_ctrl.frag_buf,
1552 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
1554 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
1556 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
1557 MLX5_SET(cqc, cqc, c_eqn, eqn);
1558 MLX5_SET(cqc, cqc, uar_page, mdev->priv.uar->index);
1559 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.frag_buf.page_shift -
1560 MLX5_ADAPTER_PAGE_SHIFT);
1561 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1563 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
1575 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
1577 mlx5_core_destroy_cq(cq->mdev, &cq->mcq);
1580 static int mlx5e_open_cq(struct mlx5e_channel *c,
1581 struct net_dim_cq_moder moder,
1582 struct mlx5e_cq_param *param,
1583 struct mlx5e_cq *cq)
1585 struct mlx5_core_dev *mdev = c->mdev;
1588 err = mlx5e_alloc_cq(c, param, cq);
1592 err = mlx5e_create_cq(cq, param);
1596 if (MLX5_CAP_GEN(mdev, cq_moderation))
1597 mlx5_core_modify_cq_moderation(mdev, &cq->mcq, moder.usec, moder.pkts);
1606 static void mlx5e_close_cq(struct mlx5e_cq *cq)
1608 mlx5e_destroy_cq(cq);
1612 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
1614 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
1617 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1618 struct mlx5e_params *params,
1619 struct mlx5e_channel_param *cparam)
1624 for (tc = 0; tc < c->num_tc; tc++) {
1625 err = mlx5e_open_cq(c, params->tx_cq_moderation,
1626 &cparam->tx_cq, &c->sq[tc].cq);
1628 goto err_close_tx_cqs;
1634 for (tc--; tc >= 0; tc--)
1635 mlx5e_close_cq(&c->sq[tc].cq);
1640 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1644 for (tc = 0; tc < c->num_tc; tc++)
1645 mlx5e_close_cq(&c->sq[tc].cq);
1648 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1649 struct mlx5e_params *params,
1650 struct mlx5e_channel_param *cparam)
1655 for (tc = 0; tc < params->num_tc; tc++) {
1656 int txq_ix = c->ix + tc * params->num_channels;
1658 err = mlx5e_open_txqsq(c, c->priv->tisn[tc], txq_ix,
1659 params, &cparam->sq, &c->sq[tc]);
1667 for (tc--; tc >= 0; tc--)
1668 mlx5e_close_txqsq(&c->sq[tc]);
1673 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1677 for (tc = 0; tc < c->num_tc; tc++)
1678 mlx5e_close_txqsq(&c->sq[tc]);
1681 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1682 struct mlx5e_txqsq *sq, u32 rate)
1684 struct mlx5e_priv *priv = netdev_priv(dev);
1685 struct mlx5_core_dev *mdev = priv->mdev;
1686 struct mlx5e_modify_sq_param msp = {0};
1687 struct mlx5_rate_limit rl = {0};
1691 if (rate == sq->rate_limit)
1695 if (sq->rate_limit) {
1696 rl.rate = sq->rate_limit;
1697 /* remove current rl index to free space to next ones */
1698 mlx5_rl_remove_rate(mdev, &rl);
1705 err = mlx5_rl_add_rate(mdev, &rl_index, &rl);
1707 netdev_err(dev, "Failed configuring rate %u: %d\n",
1713 msp.curr_state = MLX5_SQC_STATE_RDY;
1714 msp.next_state = MLX5_SQC_STATE_RDY;
1715 msp.rl_index = rl_index;
1716 msp.rl_update = true;
1717 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1719 netdev_err(dev, "Failed configuring rate %u: %d\n",
1721 /* remove the rate from the table */
1723 mlx5_rl_remove_rate(mdev, &rl);
1727 sq->rate_limit = rate;
1731 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1733 struct mlx5e_priv *priv = netdev_priv(dev);
1734 struct mlx5_core_dev *mdev = priv->mdev;
1735 struct mlx5e_txqsq *sq = priv->txq2sq[index];
1738 if (!mlx5_rl_is_supported(mdev)) {
1739 netdev_err(dev, "Rate limiting is not supported on this device\n");
1743 /* rate is given in Mb/sec, HW config is in Kb/sec */
1746 /* Check whether rate in valid range, 0 is always valid */
1747 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1748 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1752 mutex_lock(&priv->state_lock);
1753 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1754 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1756 priv->tx_rates[index] = rate;
1757 mutex_unlock(&priv->state_lock);
1762 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1763 struct mlx5e_params *params,
1764 struct mlx5e_channel_param *cparam,
1765 struct mlx5e_channel **cp)
1767 struct net_dim_cq_moder icocq_moder = {0, 0};
1768 struct net_device *netdev = priv->netdev;
1769 int cpu = mlx5e_get_cpu(priv, ix);
1770 struct mlx5e_channel *c;
1775 c = kzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1780 c->mdev = priv->mdev;
1781 c->tstamp = &priv->tstamp;
1784 c->pdev = &priv->mdev->pdev->dev;
1785 c->netdev = priv->netdev;
1786 c->mkey_be = cpu_to_be32(priv->mdev->mlx5e_res.mkey.key);
1787 c->num_tc = params->num_tc;
1788 c->xdp = !!params->xdp_prog;
1790 mlx5_vector2eqn(priv->mdev, ix, &eqn, &irq);
1791 c->irq_desc = irq_to_desc(irq);
1793 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1795 err = mlx5e_open_cq(c, icocq_moder, &cparam->icosq_cq, &c->icosq.cq);
1799 err = mlx5e_open_tx_cqs(c, params, cparam);
1801 goto err_close_icosq_cq;
1803 err = mlx5e_open_cq(c, params->rx_cq_moderation, &cparam->rx_cq, &c->rq.cq);
1805 goto err_close_tx_cqs;
1807 /* XDP SQ CQ params are same as normal TXQ sq CQ params */
1808 err = c->xdp ? mlx5e_open_cq(c, params->tx_cq_moderation,
1809 &cparam->tx_cq, &c->rq.xdpsq.cq) : 0;
1811 goto err_close_rx_cq;
1813 napi_enable(&c->napi);
1815 err = mlx5e_open_icosq(c, params, &cparam->icosq, &c->icosq);
1817 goto err_disable_napi;
1819 err = mlx5e_open_sqs(c, params, cparam);
1821 goto err_close_icosq;
1823 err = c->xdp ? mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, &c->rq.xdpsq) : 0;
1827 err = mlx5e_open_rq(c, params, &cparam->rq, &c->rq);
1829 goto err_close_xdp_sq;
1836 mlx5e_close_xdpsq(&c->rq.xdpsq);
1842 mlx5e_close_icosq(&c->icosq);
1845 napi_disable(&c->napi);
1847 mlx5e_close_cq(&c->rq.xdpsq.cq);
1850 mlx5e_close_cq(&c->rq.cq);
1853 mlx5e_close_tx_cqs(c);
1856 mlx5e_close_cq(&c->icosq.cq);
1859 netif_napi_del(&c->napi);
1865 static void mlx5e_activate_channel(struct mlx5e_channel *c)
1869 for (tc = 0; tc < c->num_tc; tc++)
1870 mlx5e_activate_txqsq(&c->sq[tc]);
1871 mlx5e_activate_rq(&c->rq);
1872 netif_set_xps_queue(c->netdev, get_cpu_mask(c->cpu), c->ix);
1875 static void mlx5e_deactivate_channel(struct mlx5e_channel *c)
1879 mlx5e_deactivate_rq(&c->rq);
1880 for (tc = 0; tc < c->num_tc; tc++)
1881 mlx5e_deactivate_txqsq(&c->sq[tc]);
1884 static void mlx5e_close_channel(struct mlx5e_channel *c)
1886 mlx5e_close_rq(&c->rq);
1888 mlx5e_close_xdpsq(&c->rq.xdpsq);
1890 mlx5e_close_icosq(&c->icosq);
1891 napi_disable(&c->napi);
1893 mlx5e_close_cq(&c->rq.xdpsq.cq);
1894 mlx5e_close_cq(&c->rq.cq);
1895 mlx5e_close_tx_cqs(c);
1896 mlx5e_close_cq(&c->icosq.cq);
1897 netif_napi_del(&c->napi);
1902 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
1903 struct mlx5e_params *params,
1904 struct mlx5e_rq_param *param)
1906 struct mlx5_core_dev *mdev = priv->mdev;
1907 void *rqc = param->rqc;
1908 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1910 switch (params->rq_wq_type) {
1911 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1912 MLX5_SET(wq, wq, log_wqe_num_of_strides,
1913 mlx5e_mpwqe_get_log_num_strides(mdev, params) -
1914 MLX5_MPWQE_LOG_NUM_STRIDES_BASE);
1915 MLX5_SET(wq, wq, log_wqe_stride_size,
1916 mlx5e_mpwqe_get_log_stride_size(mdev, params) -
1917 MLX5_MPWQE_LOG_STRIDE_SZ_BASE);
1918 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
1919 MLX5_SET(wq, wq, log_wq_sz, mlx5e_mpwqe_get_log_rq_size(params));
1921 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1922 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1923 MLX5_SET(wq, wq, log_wq_sz, params->log_rq_mtu_frames);
1926 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1927 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1928 MLX5_SET(wq, wq, pd, mdev->mlx5e_res.pdn);
1929 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
1930 MLX5_SET(rqc, rqc, vsd, params->vlan_strip_disable);
1931 MLX5_SET(rqc, rqc, scatter_fcs, params->scatter_fcs_en);
1933 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
1934 param->wq.linear = 1;
1937 static void mlx5e_build_drop_rq_param(struct mlx5e_priv *priv,
1938 struct mlx5e_rq_param *param)
1940 struct mlx5_core_dev *mdev = priv->mdev;
1941 void *rqc = param->rqc;
1942 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1944 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1945 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1946 MLX5_SET(rqc, rqc, counter_set_id, priv->drop_rq_q_counter);
1948 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
1951 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
1952 struct mlx5e_sq_param *param)
1954 void *sqc = param->sqc;
1955 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1957 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1958 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
1960 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1963 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
1964 struct mlx5e_params *params,
1965 struct mlx5e_sq_param *param)
1967 void *sqc = param->sqc;
1968 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1970 mlx5e_build_sq_param_common(priv, param);
1971 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
1972 MLX5_SET(sqc, sqc, allow_swp, !!MLX5_IPSEC_DEV(priv->mdev));
1975 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1976 struct mlx5e_cq_param *param)
1978 void *cqc = param->cqc;
1980 MLX5_SET(cqc, cqc, uar_page, priv->mdev->priv.uar->index);
1983 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1984 struct mlx5e_params *params,
1985 struct mlx5e_cq_param *param)
1987 struct mlx5_core_dev *mdev = priv->mdev;
1988 void *cqc = param->cqc;
1991 switch (params->rq_wq_type) {
1992 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1993 log_cq_size = mlx5e_mpwqe_get_log_rq_size(params) +
1994 mlx5e_mpwqe_get_log_num_strides(mdev, params);
1996 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1997 log_cq_size = params->log_rq_mtu_frames;
2000 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
2001 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
2002 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
2003 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
2006 mlx5e_build_common_cq_param(priv, param);
2007 param->cq_period_mode = params->rx_cq_moderation.cq_period_mode;
2010 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
2011 struct mlx5e_params *params,
2012 struct mlx5e_cq_param *param)
2014 void *cqc = param->cqc;
2016 MLX5_SET(cqc, cqc, log_cq_size, params->log_sq_size);
2018 mlx5e_build_common_cq_param(priv, param);
2019 param->cq_period_mode = params->tx_cq_moderation.cq_period_mode;
2022 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
2024 struct mlx5e_cq_param *param)
2026 void *cqc = param->cqc;
2028 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
2030 mlx5e_build_common_cq_param(priv, param);
2032 param->cq_period_mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2035 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
2037 struct mlx5e_sq_param *param)
2039 void *sqc = param->sqc;
2040 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2042 mlx5e_build_sq_param_common(priv, param);
2044 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
2045 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
2048 static void mlx5e_build_xdpsq_param(struct mlx5e_priv *priv,
2049 struct mlx5e_params *params,
2050 struct mlx5e_sq_param *param)
2052 void *sqc = param->sqc;
2053 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2055 mlx5e_build_sq_param_common(priv, param);
2056 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2059 static void mlx5e_build_channel_param(struct mlx5e_priv *priv,
2060 struct mlx5e_params *params,
2061 struct mlx5e_channel_param *cparam)
2063 u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
2065 mlx5e_build_rq_param(priv, params, &cparam->rq);
2066 mlx5e_build_sq_param(priv, params, &cparam->sq);
2067 mlx5e_build_xdpsq_param(priv, params, &cparam->xdp_sq);
2068 mlx5e_build_icosq_param(priv, icosq_log_wq_sz, &cparam->icosq);
2069 mlx5e_build_rx_cq_param(priv, params, &cparam->rx_cq);
2070 mlx5e_build_tx_cq_param(priv, params, &cparam->tx_cq);
2071 mlx5e_build_ico_cq_param(priv, icosq_log_wq_sz, &cparam->icosq_cq);
2074 int mlx5e_open_channels(struct mlx5e_priv *priv,
2075 struct mlx5e_channels *chs)
2077 struct mlx5e_channel_param *cparam;
2081 chs->num = chs->params.num_channels;
2083 chs->c = kcalloc(chs->num, sizeof(struct mlx5e_channel *), GFP_KERNEL);
2084 cparam = kzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
2085 if (!chs->c || !cparam)
2088 mlx5e_build_channel_param(priv, &chs->params, cparam);
2089 for (i = 0; i < chs->num; i++) {
2090 err = mlx5e_open_channel(priv, i, &chs->params, cparam, &chs->c[i]);
2092 goto err_close_channels;
2099 for (i--; i >= 0; i--)
2100 mlx5e_close_channel(chs->c[i]);
2109 static void mlx5e_activate_channels(struct mlx5e_channels *chs)
2113 for (i = 0; i < chs->num; i++)
2114 mlx5e_activate_channel(chs->c[i]);
2117 static int mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels *chs)
2122 for (i = 0; i < chs->num; i++) {
2123 err = mlx5e_wait_for_min_rx_wqes(&chs->c[i]->rq);
2131 static void mlx5e_deactivate_channels(struct mlx5e_channels *chs)
2135 for (i = 0; i < chs->num; i++)
2136 mlx5e_deactivate_channel(chs->c[i]);
2139 void mlx5e_close_channels(struct mlx5e_channels *chs)
2143 for (i = 0; i < chs->num; i++)
2144 mlx5e_close_channel(chs->c[i]);
2151 mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, struct mlx5e_rqt *rqt)
2153 struct mlx5_core_dev *mdev = priv->mdev;
2160 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
2161 in = kvzalloc(inlen, GFP_KERNEL);
2165 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
2167 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2168 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
2170 for (i = 0; i < sz; i++)
2171 MLX5_SET(rqtc, rqtc, rq_num[i], priv->drop_rq.rqn);
2173 err = mlx5_core_create_rqt(mdev, in, inlen, &rqt->rqtn);
2175 rqt->enabled = true;
2181 void mlx5e_destroy_rqt(struct mlx5e_priv *priv, struct mlx5e_rqt *rqt)
2183 rqt->enabled = false;
2184 mlx5_core_destroy_rqt(priv->mdev, rqt->rqtn);
2187 int mlx5e_create_indirect_rqt(struct mlx5e_priv *priv)
2189 struct mlx5e_rqt *rqt = &priv->indir_rqt;
2192 err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, rqt);
2194 mlx5_core_warn(priv->mdev, "create indirect rqts failed, %d\n", err);
2198 int mlx5e_create_direct_rqts(struct mlx5e_priv *priv)
2200 struct mlx5e_rqt *rqt;
2204 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2205 rqt = &priv->direct_tir[ix].rqt;
2206 err = mlx5e_create_rqt(priv, 1 /*size */, rqt);
2208 goto err_destroy_rqts;
2214 mlx5_core_warn(priv->mdev, "create direct rqts failed, %d\n", err);
2215 for (ix--; ix >= 0; ix--)
2216 mlx5e_destroy_rqt(priv, &priv->direct_tir[ix].rqt);
2221 void mlx5e_destroy_direct_rqts(struct mlx5e_priv *priv)
2225 for (i = 0; i < priv->profile->max_nch(priv->mdev); i++)
2226 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
2229 static int mlx5e_rx_hash_fn(int hfunc)
2231 return (hfunc == ETH_RSS_HASH_TOP) ?
2232 MLX5_RX_HASH_FN_TOEPLITZ :
2233 MLX5_RX_HASH_FN_INVERTED_XOR8;
2236 int mlx5e_bits_invert(unsigned long a, int size)
2241 for (i = 0; i < size; i++)
2242 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
2247 static void mlx5e_fill_rqt_rqns(struct mlx5e_priv *priv, int sz,
2248 struct mlx5e_redirect_rqt_param rrp, void *rqtc)
2252 for (i = 0; i < sz; i++) {
2258 if (rrp.rss.hfunc == ETH_RSS_HASH_XOR)
2259 ix = mlx5e_bits_invert(i, ilog2(sz));
2261 ix = priv->channels.params.indirection_rqt[ix];
2262 rqn = rrp.rss.channels->c[ix]->rq.rqn;
2266 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
2270 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz,
2271 struct mlx5e_redirect_rqt_param rrp)
2273 struct mlx5_core_dev *mdev = priv->mdev;
2279 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
2280 in = kvzalloc(inlen, GFP_KERNEL);
2284 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
2286 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2287 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
2288 mlx5e_fill_rqt_rqns(priv, sz, rrp, rqtc);
2289 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
2295 static u32 mlx5e_get_direct_rqn(struct mlx5e_priv *priv, int ix,
2296 struct mlx5e_redirect_rqt_param rrp)
2301 if (ix >= rrp.rss.channels->num)
2302 return priv->drop_rq.rqn;
2304 return rrp.rss.channels->c[ix]->rq.rqn;
2307 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv,
2308 struct mlx5e_redirect_rqt_param rrp)
2313 if (priv->indir_rqt.enabled) {
2315 rqtn = priv->indir_rqt.rqtn;
2316 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, rrp);
2319 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2320 struct mlx5e_redirect_rqt_param direct_rrp = {
2323 .rqn = mlx5e_get_direct_rqn(priv, ix, rrp)
2327 /* Direct RQ Tables */
2328 if (!priv->direct_tir[ix].rqt.enabled)
2331 rqtn = priv->direct_tir[ix].rqt.rqtn;
2332 mlx5e_redirect_rqt(priv, rqtn, 1, direct_rrp);
2336 static void mlx5e_redirect_rqts_to_channels(struct mlx5e_priv *priv,
2337 struct mlx5e_channels *chs)
2339 struct mlx5e_redirect_rqt_param rrp = {
2344 .hfunc = chs->params.rss_hfunc,
2349 mlx5e_redirect_rqts(priv, rrp);
2352 static void mlx5e_redirect_rqts_to_drop(struct mlx5e_priv *priv)
2354 struct mlx5e_redirect_rqt_param drop_rrp = {
2357 .rqn = priv->drop_rq.rqn,
2361 mlx5e_redirect_rqts(priv, drop_rrp);
2364 static void mlx5e_build_tir_ctx_lro(struct mlx5e_params *params, void *tirc)
2366 if (!params->lro_en)
2369 #define ROUGH_MAX_L2_L3_HDR_SZ 256
2371 MLX5_SET(tirc, tirc, lro_enable_mask,
2372 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2373 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
2374 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
2375 (params->lro_wqe_sz - ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
2376 MLX5_SET(tirc, tirc, lro_timeout_period_usecs, params->lro_timeout);
2379 void mlx5e_build_indir_tir_ctx_hash(struct mlx5e_params *params,
2380 enum mlx5e_traffic_types tt,
2381 void *tirc, bool inner)
2383 void *hfso = inner ? MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner) :
2384 MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2386 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
2387 MLX5_HASH_FIELD_SEL_DST_IP)
2389 #define MLX5_HASH_IP_L4PORTS (MLX5_HASH_FIELD_SEL_SRC_IP |\
2390 MLX5_HASH_FIELD_SEL_DST_IP |\
2391 MLX5_HASH_FIELD_SEL_L4_SPORT |\
2392 MLX5_HASH_FIELD_SEL_L4_DPORT)
2394 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
2395 MLX5_HASH_FIELD_SEL_DST_IP |\
2396 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2398 MLX5_SET(tirc, tirc, rx_hash_fn, mlx5e_rx_hash_fn(params->rss_hfunc));
2399 if (params->rss_hfunc == ETH_RSS_HASH_TOP) {
2400 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
2401 rx_hash_toeplitz_key);
2402 size_t len = MLX5_FLD_SZ_BYTES(tirc,
2403 rx_hash_toeplitz_key);
2405 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
2406 memcpy(rss_key, params->toeplitz_hash_key, len);
2410 case MLX5E_TT_IPV4_TCP:
2411 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2412 MLX5_L3_PROT_TYPE_IPV4);
2413 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2414 MLX5_L4_PROT_TYPE_TCP);
2415 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2416 MLX5_HASH_IP_L4PORTS);
2419 case MLX5E_TT_IPV6_TCP:
2420 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2421 MLX5_L3_PROT_TYPE_IPV6);
2422 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2423 MLX5_L4_PROT_TYPE_TCP);
2424 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2425 MLX5_HASH_IP_L4PORTS);
2428 case MLX5E_TT_IPV4_UDP:
2429 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2430 MLX5_L3_PROT_TYPE_IPV4);
2431 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2432 MLX5_L4_PROT_TYPE_UDP);
2433 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2434 MLX5_HASH_IP_L4PORTS);
2437 case MLX5E_TT_IPV6_UDP:
2438 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2439 MLX5_L3_PROT_TYPE_IPV6);
2440 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2441 MLX5_L4_PROT_TYPE_UDP);
2442 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2443 MLX5_HASH_IP_L4PORTS);
2446 case MLX5E_TT_IPV4_IPSEC_AH:
2447 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2448 MLX5_L3_PROT_TYPE_IPV4);
2449 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2450 MLX5_HASH_IP_IPSEC_SPI);
2453 case MLX5E_TT_IPV6_IPSEC_AH:
2454 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2455 MLX5_L3_PROT_TYPE_IPV6);
2456 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2457 MLX5_HASH_IP_IPSEC_SPI);
2460 case MLX5E_TT_IPV4_IPSEC_ESP:
2461 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2462 MLX5_L3_PROT_TYPE_IPV4);
2463 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2464 MLX5_HASH_IP_IPSEC_SPI);
2467 case MLX5E_TT_IPV6_IPSEC_ESP:
2468 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2469 MLX5_L3_PROT_TYPE_IPV6);
2470 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2471 MLX5_HASH_IP_IPSEC_SPI);
2475 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2476 MLX5_L3_PROT_TYPE_IPV4);
2477 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2482 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2483 MLX5_L3_PROT_TYPE_IPV6);
2484 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2488 WARN_ONCE(true, "%s: bad traffic type!\n", __func__);
2492 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
2494 struct mlx5_core_dev *mdev = priv->mdev;
2503 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
2504 in = kvzalloc(inlen, GFP_KERNEL);
2508 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
2509 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2511 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2513 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2514 err = mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in,
2520 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
2521 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
2533 static void mlx5e_build_inner_indir_tir_ctx(struct mlx5e_priv *priv,
2534 enum mlx5e_traffic_types tt,
2537 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2539 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2541 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2542 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
2543 MLX5_SET(tirc, tirc, tunneled_offload_en, 0x1);
2545 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, true);
2548 static int mlx5e_set_mtu(struct mlx5_core_dev *mdev,
2549 struct mlx5e_params *params, u16 mtu)
2551 u16 hw_mtu = MLX5E_SW2HW_MTU(params, mtu);
2554 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
2558 /* Update vport context MTU */
2559 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
2563 static void mlx5e_query_mtu(struct mlx5_core_dev *mdev,
2564 struct mlx5e_params *params, u16 *mtu)
2569 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
2570 if (err || !hw_mtu) /* fallback to port oper mtu */
2571 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
2573 *mtu = MLX5E_HW2SW_MTU(params, hw_mtu);
2576 static int mlx5e_set_dev_port_mtu(struct mlx5e_priv *priv)
2578 struct mlx5e_params *params = &priv->channels.params;
2579 struct net_device *netdev = priv->netdev;
2580 struct mlx5_core_dev *mdev = priv->mdev;
2584 err = mlx5e_set_mtu(mdev, params, params->sw_mtu);
2588 mlx5e_query_mtu(mdev, params, &mtu);
2589 if (mtu != params->sw_mtu)
2590 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
2591 __func__, mtu, params->sw_mtu);
2593 params->sw_mtu = mtu;
2597 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
2599 struct mlx5e_priv *priv = netdev_priv(netdev);
2600 int nch = priv->channels.params.num_channels;
2601 int ntc = priv->channels.params.num_tc;
2604 netdev_reset_tc(netdev);
2609 netdev_set_num_tc(netdev, ntc);
2611 /* Map netdev TCs to offset 0
2612 * We have our own UP to TXQ mapping for QoS
2614 for (tc = 0; tc < ntc; tc++)
2615 netdev_set_tc_queue(netdev, tc, nch, 0);
2618 static void mlx5e_build_channels_tx_maps(struct mlx5e_priv *priv)
2620 struct mlx5e_channel *c;
2621 struct mlx5e_txqsq *sq;
2624 for (i = 0; i < priv->channels.num; i++)
2625 for (tc = 0; tc < priv->profile->max_tc; tc++)
2626 priv->channel_tc2txq[i][tc] = i + tc * priv->channels.num;
2628 for (i = 0; i < priv->channels.num; i++) {
2629 c = priv->channels.c[i];
2630 for (tc = 0; tc < c->num_tc; tc++) {
2632 priv->txq2sq[sq->txq_ix] = sq;
2637 void mlx5e_activate_priv_channels(struct mlx5e_priv *priv)
2639 int num_txqs = priv->channels.num * priv->channels.params.num_tc;
2640 struct net_device *netdev = priv->netdev;
2642 mlx5e_netdev_set_tcs(netdev);
2643 netif_set_real_num_tx_queues(netdev, num_txqs);
2644 netif_set_real_num_rx_queues(netdev, priv->channels.num);
2646 mlx5e_build_channels_tx_maps(priv);
2647 mlx5e_activate_channels(&priv->channels);
2648 netif_tx_start_all_queues(priv->netdev);
2650 if (MLX5_VPORT_MANAGER(priv->mdev))
2651 mlx5e_add_sqs_fwd_rules(priv);
2653 mlx5e_wait_channels_min_rx_wqes(&priv->channels);
2654 mlx5e_redirect_rqts_to_channels(priv, &priv->channels);
2657 void mlx5e_deactivate_priv_channels(struct mlx5e_priv *priv)
2659 mlx5e_redirect_rqts_to_drop(priv);
2661 if (MLX5_VPORT_MANAGER(priv->mdev))
2662 mlx5e_remove_sqs_fwd_rules(priv);
2664 /* FIXME: This is a W/A only for tx timeout watch dog false alarm when
2665 * polling for inactive tx queues.
2667 netif_tx_stop_all_queues(priv->netdev);
2668 netif_tx_disable(priv->netdev);
2669 mlx5e_deactivate_channels(&priv->channels);
2672 void mlx5e_switch_priv_channels(struct mlx5e_priv *priv,
2673 struct mlx5e_channels *new_chs,
2674 mlx5e_fp_hw_modify hw_modify)
2676 struct net_device *netdev = priv->netdev;
2679 new_num_txqs = new_chs->num * new_chs->params.num_tc;
2681 carrier_ok = netif_carrier_ok(netdev);
2682 netif_carrier_off(netdev);
2684 if (new_num_txqs < netdev->real_num_tx_queues)
2685 netif_set_real_num_tx_queues(netdev, new_num_txqs);
2687 mlx5e_deactivate_priv_channels(priv);
2688 mlx5e_close_channels(&priv->channels);
2690 priv->channels = *new_chs;
2692 /* New channels are ready to roll, modify HW settings if needed */
2696 mlx5e_refresh_tirs(priv, false);
2697 mlx5e_activate_priv_channels(priv);
2699 /* return carrier back if needed */
2701 netif_carrier_on(netdev);
2704 void mlx5e_timestamp_init(struct mlx5e_priv *priv)
2706 priv->tstamp.tx_type = HWTSTAMP_TX_OFF;
2707 priv->tstamp.rx_filter = HWTSTAMP_FILTER_NONE;
2710 int mlx5e_open_locked(struct net_device *netdev)
2712 struct mlx5e_priv *priv = netdev_priv(netdev);
2715 set_bit(MLX5E_STATE_OPENED, &priv->state);
2717 err = mlx5e_open_channels(priv, &priv->channels);
2719 goto err_clear_state_opened_flag;
2721 mlx5e_refresh_tirs(priv, false);
2722 mlx5e_activate_priv_channels(priv);
2723 if (priv->profile->update_carrier)
2724 priv->profile->update_carrier(priv);
2726 if (priv->profile->update_stats)
2727 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
2731 err_clear_state_opened_flag:
2732 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2736 int mlx5e_open(struct net_device *netdev)
2738 struct mlx5e_priv *priv = netdev_priv(netdev);
2741 mutex_lock(&priv->state_lock);
2742 err = mlx5e_open_locked(netdev);
2744 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_UP);
2745 mutex_unlock(&priv->state_lock);
2747 if (mlx5e_vxlan_allowed(priv->mdev))
2748 udp_tunnel_get_rx_info(netdev);
2753 int mlx5e_close_locked(struct net_device *netdev)
2755 struct mlx5e_priv *priv = netdev_priv(netdev);
2757 /* May already be CLOSED in case a previous configuration operation
2758 * (e.g RX/TX queue size change) that involves close&open failed.
2760 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2763 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2765 netif_carrier_off(priv->netdev);
2766 mlx5e_deactivate_priv_channels(priv);
2767 mlx5e_close_channels(&priv->channels);
2772 int mlx5e_close(struct net_device *netdev)
2774 struct mlx5e_priv *priv = netdev_priv(netdev);
2777 if (!netif_device_present(netdev))
2780 mutex_lock(&priv->state_lock);
2781 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_DOWN);
2782 err = mlx5e_close_locked(netdev);
2783 mutex_unlock(&priv->state_lock);
2788 static int mlx5e_alloc_drop_rq(struct mlx5_core_dev *mdev,
2789 struct mlx5e_rq *rq,
2790 struct mlx5e_rq_param *param)
2792 void *rqc = param->rqc;
2793 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
2796 param->wq.db_numa_node = param->wq.buf_numa_node;
2798 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
2803 /* Mark as unused given "Drop-RQ" packets never reach XDP */
2804 xdp_rxq_info_unused(&rq->xdp_rxq);
2811 static int mlx5e_alloc_drop_cq(struct mlx5_core_dev *mdev,
2812 struct mlx5e_cq *cq,
2813 struct mlx5e_cq_param *param)
2815 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
2816 param->wq.db_numa_node = dev_to_node(&mdev->pdev->dev);
2818 return mlx5e_alloc_cq_common(mdev, param, cq);
2821 static int mlx5e_open_drop_rq(struct mlx5e_priv *priv,
2822 struct mlx5e_rq *drop_rq)
2824 struct mlx5_core_dev *mdev = priv->mdev;
2825 struct mlx5e_cq_param cq_param = {};
2826 struct mlx5e_rq_param rq_param = {};
2827 struct mlx5e_cq *cq = &drop_rq->cq;
2830 mlx5e_build_drop_rq_param(priv, &rq_param);
2832 err = mlx5e_alloc_drop_cq(mdev, cq, &cq_param);
2836 err = mlx5e_create_cq(cq, &cq_param);
2840 err = mlx5e_alloc_drop_rq(mdev, drop_rq, &rq_param);
2842 goto err_destroy_cq;
2844 err = mlx5e_create_rq(drop_rq, &rq_param);
2848 err = mlx5e_modify_rq_state(drop_rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
2850 mlx5_core_warn(priv->mdev, "modify_rq_state failed, rx_if_down_packets won't be counted %d\n", err);
2855 mlx5e_free_rq(drop_rq);
2858 mlx5e_destroy_cq(cq);
2866 static void mlx5e_close_drop_rq(struct mlx5e_rq *drop_rq)
2868 mlx5e_destroy_rq(drop_rq);
2869 mlx5e_free_rq(drop_rq);
2870 mlx5e_destroy_cq(&drop_rq->cq);
2871 mlx5e_free_cq(&drop_rq->cq);
2874 int mlx5e_create_tis(struct mlx5_core_dev *mdev, int tc,
2875 u32 underlay_qpn, u32 *tisn)
2877 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
2878 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
2880 MLX5_SET(tisc, tisc, prio, tc << 1);
2881 MLX5_SET(tisc, tisc, underlay_qpn, underlay_qpn);
2882 MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.td.tdn);
2884 if (mlx5_lag_is_lacp_owner(mdev))
2885 MLX5_SET(tisc, tisc, strict_lag_tx_port_affinity, 1);
2887 return mlx5_core_create_tis(mdev, in, sizeof(in), tisn);
2890 void mlx5e_destroy_tis(struct mlx5_core_dev *mdev, u32 tisn)
2892 mlx5_core_destroy_tis(mdev, tisn);
2895 int mlx5e_create_tises(struct mlx5e_priv *priv)
2900 for (tc = 0; tc < priv->profile->max_tc; tc++) {
2901 err = mlx5e_create_tis(priv->mdev, tc, 0, &priv->tisn[tc]);
2903 goto err_close_tises;
2909 for (tc--; tc >= 0; tc--)
2910 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
2915 void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
2919 for (tc = 0; tc < priv->profile->max_tc; tc++)
2920 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
2923 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv,
2924 enum mlx5e_traffic_types tt,
2927 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2929 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2931 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2932 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
2933 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, false);
2936 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 rqtn, u32 *tirc)
2938 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2940 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2942 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2943 MLX5_SET(tirc, tirc, indirect_table, rqtn);
2944 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
2947 int mlx5e_create_indirect_tirs(struct mlx5e_priv *priv)
2949 struct mlx5e_tir *tir;
2957 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2958 in = kvzalloc(inlen, GFP_KERNEL);
2962 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2963 memset(in, 0, inlen);
2964 tir = &priv->indir_tir[tt];
2965 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2966 mlx5e_build_indir_tir_ctx(priv, tt, tirc);
2967 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
2969 mlx5_core_warn(priv->mdev, "create indirect tirs failed, %d\n", err);
2970 goto err_destroy_inner_tirs;
2974 if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
2977 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
2978 memset(in, 0, inlen);
2979 tir = &priv->inner_indir_tir[i];
2980 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2981 mlx5e_build_inner_indir_tir_ctx(priv, i, tirc);
2982 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
2984 mlx5_core_warn(priv->mdev, "create inner indirect tirs failed, %d\n", err);
2985 goto err_destroy_inner_tirs;
2994 err_destroy_inner_tirs:
2995 for (i--; i >= 0; i--)
2996 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
2998 for (tt--; tt >= 0; tt--)
2999 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[tt]);
3006 int mlx5e_create_direct_tirs(struct mlx5e_priv *priv)
3008 int nch = priv->profile->max_nch(priv->mdev);
3009 struct mlx5e_tir *tir;
3016 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3017 in = kvzalloc(inlen, GFP_KERNEL);
3021 for (ix = 0; ix < nch; ix++) {
3022 memset(in, 0, inlen);
3023 tir = &priv->direct_tir[ix];
3024 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3025 mlx5e_build_direct_tir_ctx(priv, priv->direct_tir[ix].rqt.rqtn, tirc);
3026 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3028 goto err_destroy_ch_tirs;
3035 err_destroy_ch_tirs:
3036 mlx5_core_warn(priv->mdev, "create direct tirs failed, %d\n", err);
3037 for (ix--; ix >= 0; ix--)
3038 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[ix]);
3045 void mlx5e_destroy_indirect_tirs(struct mlx5e_priv *priv)
3049 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3050 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[i]);
3052 if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
3055 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3056 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3059 void mlx5e_destroy_direct_tirs(struct mlx5e_priv *priv)
3061 int nch = priv->profile->max_nch(priv->mdev);
3064 for (i = 0; i < nch; i++)
3065 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[i]);
3068 static int mlx5e_modify_channels_scatter_fcs(struct mlx5e_channels *chs, bool enable)
3073 for (i = 0; i < chs->num; i++) {
3074 err = mlx5e_modify_rq_scatter_fcs(&chs->c[i]->rq, enable);
3082 static int mlx5e_modify_channels_vsd(struct mlx5e_channels *chs, bool vsd)
3087 for (i = 0; i < chs->num; i++) {
3088 err = mlx5e_modify_rq_vsd(&chs->c[i]->rq, vsd);
3096 static int mlx5e_setup_tc_mqprio(struct net_device *netdev,
3097 struct tc_mqprio_qopt *mqprio)
3099 struct mlx5e_priv *priv = netdev_priv(netdev);
3100 struct mlx5e_channels new_channels = {};
3101 u8 tc = mqprio->num_tc;
3104 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
3106 if (tc && tc != MLX5E_MAX_NUM_TC)
3109 mutex_lock(&priv->state_lock);
3111 new_channels.params = priv->channels.params;
3112 new_channels.params.num_tc = tc ? tc : 1;
3114 if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
3115 priv->channels.params = new_channels.params;
3119 err = mlx5e_open_channels(priv, &new_channels);
3123 mlx5e_switch_priv_channels(priv, &new_channels, NULL);
3125 mutex_unlock(&priv->state_lock);
3129 #ifdef CONFIG_MLX5_ESWITCH
3130 static int mlx5e_setup_tc_cls_flower(struct mlx5e_priv *priv,
3131 struct tc_cls_flower_offload *cls_flower)
3133 switch (cls_flower->command) {
3134 case TC_CLSFLOWER_REPLACE:
3135 return mlx5e_configure_flower(priv, cls_flower);
3136 case TC_CLSFLOWER_DESTROY:
3137 return mlx5e_delete_flower(priv, cls_flower);
3138 case TC_CLSFLOWER_STATS:
3139 return mlx5e_stats_flower(priv, cls_flower);
3145 int mlx5e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
3148 struct mlx5e_priv *priv = cb_priv;
3150 if (!tc_cls_can_offload_and_chain0(priv->netdev, type_data))
3154 case TC_SETUP_CLSFLOWER:
3155 return mlx5e_setup_tc_cls_flower(priv, type_data);
3161 static int mlx5e_setup_tc_block(struct net_device *dev,
3162 struct tc_block_offload *f)
3164 struct mlx5e_priv *priv = netdev_priv(dev);
3166 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
3169 switch (f->command) {
3171 return tcf_block_cb_register(f->block, mlx5e_setup_tc_block_cb,
3173 case TC_BLOCK_UNBIND:
3174 tcf_block_cb_unregister(f->block, mlx5e_setup_tc_block_cb,
3183 static int mlx5e_setup_tc(struct net_device *dev, enum tc_setup_type type,
3187 #ifdef CONFIG_MLX5_ESWITCH
3188 case TC_SETUP_BLOCK:
3189 return mlx5e_setup_tc_block(dev, type_data);
3191 case TC_SETUP_QDISC_MQPRIO:
3192 return mlx5e_setup_tc_mqprio(dev, type_data);
3199 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
3201 struct mlx5e_priv *priv = netdev_priv(dev);
3202 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
3203 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
3204 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
3206 if (mlx5e_is_uplink_rep(priv)) {
3207 stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
3208 stats->rx_bytes = PPORT_802_3_GET(pstats, a_octets_received_ok);
3209 stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
3210 stats->tx_bytes = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
3212 stats->rx_packets = sstats->rx_packets;
3213 stats->rx_bytes = sstats->rx_bytes;
3214 stats->tx_packets = sstats->tx_packets;
3215 stats->tx_bytes = sstats->tx_bytes;
3216 stats->tx_dropped = sstats->tx_queue_dropped;
3219 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
3221 stats->rx_length_errors =
3222 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
3223 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
3224 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
3225 stats->rx_crc_errors =
3226 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
3227 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
3228 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
3229 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
3230 stats->rx_frame_errors;
3231 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
3233 /* vport multicast also counts packets that are dropped due to steering
3234 * or rx out of buffer
3237 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
3240 static void mlx5e_set_rx_mode(struct net_device *dev)
3242 struct mlx5e_priv *priv = netdev_priv(dev);
3244 queue_work(priv->wq, &priv->set_rx_mode_work);
3247 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
3249 struct mlx5e_priv *priv = netdev_priv(netdev);
3250 struct sockaddr *saddr = addr;
3252 if (!is_valid_ether_addr(saddr->sa_data))
3253 return -EADDRNOTAVAIL;
3255 netif_addr_lock_bh(netdev);
3256 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
3257 netif_addr_unlock_bh(netdev);
3259 queue_work(priv->wq, &priv->set_rx_mode_work);
3264 #define MLX5E_SET_FEATURE(features, feature, enable) \
3267 *features |= feature; \
3269 *features &= ~feature; \
3272 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
3274 static int set_feature_lro(struct net_device *netdev, bool enable)
3276 struct mlx5e_priv *priv = netdev_priv(netdev);
3277 struct mlx5_core_dev *mdev = priv->mdev;
3278 struct mlx5e_channels new_channels = {};
3279 struct mlx5e_params *old_params;
3283 mutex_lock(&priv->state_lock);
3285 old_params = &priv->channels.params;
3286 reset = test_bit(MLX5E_STATE_OPENED, &priv->state);
3288 new_channels.params = *old_params;
3289 new_channels.params.lro_en = enable;
3291 if (old_params->rq_wq_type != MLX5_WQ_TYPE_LINKED_LIST) {
3292 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, old_params) ==
3293 mlx5e_rx_mpwqe_is_linear_skb(mdev, &new_channels.params))
3298 *old_params = new_channels.params;
3299 err = mlx5e_modify_tirs_lro(priv);
3303 err = mlx5e_open_channels(priv, &new_channels);
3307 mlx5e_switch_priv_channels(priv, &new_channels, mlx5e_modify_tirs_lro);
3309 mutex_unlock(&priv->state_lock);
3313 static int set_feature_cvlan_filter(struct net_device *netdev, bool enable)
3315 struct mlx5e_priv *priv = netdev_priv(netdev);
3318 mlx5e_enable_cvlan_filter(priv);
3320 mlx5e_disable_cvlan_filter(priv);
3325 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
3327 struct mlx5e_priv *priv = netdev_priv(netdev);
3329 if (!enable && mlx5e_tc_num_filters(priv)) {
3331 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
3338 static int set_feature_rx_all(struct net_device *netdev, bool enable)
3340 struct mlx5e_priv *priv = netdev_priv(netdev);
3341 struct mlx5_core_dev *mdev = priv->mdev;
3343 return mlx5_set_port_fcs(mdev, !enable);
3346 static int set_feature_rx_fcs(struct net_device *netdev, bool enable)
3348 struct mlx5e_priv *priv = netdev_priv(netdev);
3351 mutex_lock(&priv->state_lock);
3353 priv->channels.params.scatter_fcs_en = enable;
3354 err = mlx5e_modify_channels_scatter_fcs(&priv->channels, enable);
3356 priv->channels.params.scatter_fcs_en = !enable;
3358 mutex_unlock(&priv->state_lock);
3363 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
3365 struct mlx5e_priv *priv = netdev_priv(netdev);
3368 mutex_lock(&priv->state_lock);
3370 priv->channels.params.vlan_strip_disable = !enable;
3371 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3374 err = mlx5e_modify_channels_vsd(&priv->channels, !enable);
3376 priv->channels.params.vlan_strip_disable = enable;
3379 mutex_unlock(&priv->state_lock);
3384 #ifdef CONFIG_RFS_ACCEL
3385 static int set_feature_arfs(struct net_device *netdev, bool enable)
3387 struct mlx5e_priv *priv = netdev_priv(netdev);
3391 err = mlx5e_arfs_enable(priv);
3393 err = mlx5e_arfs_disable(priv);
3399 static int mlx5e_handle_feature(struct net_device *netdev,
3400 netdev_features_t *features,
3401 netdev_features_t wanted_features,
3402 netdev_features_t feature,
3403 mlx5e_feature_handler feature_handler)
3405 netdev_features_t changes = wanted_features ^ netdev->features;
3406 bool enable = !!(wanted_features & feature);
3409 if (!(changes & feature))
3412 err = feature_handler(netdev, enable);
3414 netdev_err(netdev, "%s feature %pNF failed, err %d\n",
3415 enable ? "Enable" : "Disable", &feature, err);
3419 MLX5E_SET_FEATURE(features, feature, enable);
3423 static int mlx5e_set_features(struct net_device *netdev,
3424 netdev_features_t features)
3426 netdev_features_t oper_features = netdev->features;
3429 #define MLX5E_HANDLE_FEATURE(feature, handler) \
3430 mlx5e_handle_feature(netdev, &oper_features, features, feature, handler)
3432 err |= MLX5E_HANDLE_FEATURE(NETIF_F_LRO, set_feature_lro);
3433 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_FILTER,
3434 set_feature_cvlan_filter);
3435 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_TC, set_feature_tc_num_filters);
3436 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXALL, set_feature_rx_all);
3437 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXFCS, set_feature_rx_fcs);
3438 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_RX, set_feature_rx_vlan);
3439 #ifdef CONFIG_RFS_ACCEL
3440 err |= MLX5E_HANDLE_FEATURE(NETIF_F_NTUPLE, set_feature_arfs);
3444 netdev->features = oper_features;
3451 static netdev_features_t mlx5e_fix_features(struct net_device *netdev,
3452 netdev_features_t features)
3454 struct mlx5e_priv *priv = netdev_priv(netdev);
3456 mutex_lock(&priv->state_lock);
3457 if (!bitmap_empty(priv->fs.vlan.active_svlans, VLAN_N_VID)) {
3458 /* HW strips the outer C-tag header, this is a problem
3459 * for S-tag traffic.
3461 features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3462 if (!priv->channels.params.vlan_strip_disable)
3463 netdev_warn(netdev, "Dropping C-tag vlan stripping offload due to S-tag vlan\n");
3465 mutex_unlock(&priv->state_lock);
3470 static int mlx5e_change_mtu(struct net_device *netdev, int new_mtu)
3472 struct mlx5e_priv *priv = netdev_priv(netdev);
3473 struct mlx5e_channels new_channels = {};
3474 struct mlx5e_params *params;
3478 mutex_lock(&priv->state_lock);
3480 params = &priv->channels.params;
3482 reset = !params->lro_en;
3483 reset = reset && test_bit(MLX5E_STATE_OPENED, &priv->state);
3485 new_channels.params = *params;
3486 new_channels.params.sw_mtu = new_mtu;
3488 if (params->rq_wq_type != MLX5_WQ_TYPE_LINKED_LIST) {
3489 u8 ppw_old = mlx5e_mpwqe_log_pkts_per_wqe(params);
3490 u8 ppw_new = mlx5e_mpwqe_log_pkts_per_wqe(&new_channels.params);
3492 reset = reset && (ppw_old != ppw_new);
3496 params->sw_mtu = new_mtu;
3497 mlx5e_set_dev_port_mtu(priv);
3498 netdev->mtu = params->sw_mtu;
3502 err = mlx5e_open_channels(priv, &new_channels);
3506 mlx5e_switch_priv_channels(priv, &new_channels, mlx5e_set_dev_port_mtu);
3507 netdev->mtu = new_channels.params.sw_mtu;
3510 mutex_unlock(&priv->state_lock);
3514 int mlx5e_hwstamp_set(struct mlx5e_priv *priv, struct ifreq *ifr)
3516 struct hwtstamp_config config;
3519 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
3522 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
3525 /* TX HW timestamp */
3526 switch (config.tx_type) {
3527 case HWTSTAMP_TX_OFF:
3528 case HWTSTAMP_TX_ON:
3534 mutex_lock(&priv->state_lock);
3535 /* RX HW timestamp */
3536 switch (config.rx_filter) {
3537 case HWTSTAMP_FILTER_NONE:
3538 /* Reset CQE compression to Admin default */
3539 mlx5e_modify_rx_cqe_compression_locked(priv, priv->channels.params.rx_cqe_compress_def);
3541 case HWTSTAMP_FILTER_ALL:
3542 case HWTSTAMP_FILTER_SOME:
3543 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3544 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3545 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3546 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3547 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3548 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3549 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3550 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3551 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3552 case HWTSTAMP_FILTER_PTP_V2_EVENT:
3553 case HWTSTAMP_FILTER_PTP_V2_SYNC:
3554 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3555 case HWTSTAMP_FILTER_NTP_ALL:
3556 /* Disable CQE compression */
3557 netdev_warn(priv->netdev, "Disabling cqe compression");
3558 err = mlx5e_modify_rx_cqe_compression_locked(priv, false);
3560 netdev_err(priv->netdev, "Failed disabling cqe compression err=%d\n", err);
3561 mutex_unlock(&priv->state_lock);
3564 config.rx_filter = HWTSTAMP_FILTER_ALL;
3567 mutex_unlock(&priv->state_lock);
3571 memcpy(&priv->tstamp, &config, sizeof(config));
3572 mutex_unlock(&priv->state_lock);
3574 return copy_to_user(ifr->ifr_data, &config,
3575 sizeof(config)) ? -EFAULT : 0;
3578 int mlx5e_hwstamp_get(struct mlx5e_priv *priv, struct ifreq *ifr)
3580 struct hwtstamp_config *cfg = &priv->tstamp;
3582 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
3585 return copy_to_user(ifr->ifr_data, cfg, sizeof(*cfg)) ? -EFAULT : 0;
3588 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3590 struct mlx5e_priv *priv = netdev_priv(dev);
3594 return mlx5e_hwstamp_set(priv, ifr);
3596 return mlx5e_hwstamp_get(priv, ifr);
3602 #ifdef CONFIG_MLX5_ESWITCH
3603 static int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
3605 struct mlx5e_priv *priv = netdev_priv(dev);
3606 struct mlx5_core_dev *mdev = priv->mdev;
3608 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
3611 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
3614 struct mlx5e_priv *priv = netdev_priv(dev);
3615 struct mlx5_core_dev *mdev = priv->mdev;
3617 if (vlan_proto != htons(ETH_P_8021Q))
3618 return -EPROTONOSUPPORT;
3620 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
3624 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
3626 struct mlx5e_priv *priv = netdev_priv(dev);
3627 struct mlx5_core_dev *mdev = priv->mdev;
3629 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
3632 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
3634 struct mlx5e_priv *priv = netdev_priv(dev);
3635 struct mlx5_core_dev *mdev = priv->mdev;
3637 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
3640 static int mlx5e_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
3643 struct mlx5e_priv *priv = netdev_priv(dev);
3644 struct mlx5_core_dev *mdev = priv->mdev;
3646 return mlx5_eswitch_set_vport_rate(mdev->priv.eswitch, vf + 1,
3647 max_tx_rate, min_tx_rate);
3650 static int mlx5_vport_link2ifla(u8 esw_link)
3653 case MLX5_ESW_VPORT_ADMIN_STATE_DOWN:
3654 return IFLA_VF_LINK_STATE_DISABLE;
3655 case MLX5_ESW_VPORT_ADMIN_STATE_UP:
3656 return IFLA_VF_LINK_STATE_ENABLE;
3658 return IFLA_VF_LINK_STATE_AUTO;
3661 static int mlx5_ifla_link2vport(u8 ifla_link)
3663 switch (ifla_link) {
3664 case IFLA_VF_LINK_STATE_DISABLE:
3665 return MLX5_ESW_VPORT_ADMIN_STATE_DOWN;
3666 case IFLA_VF_LINK_STATE_ENABLE:
3667 return MLX5_ESW_VPORT_ADMIN_STATE_UP;
3669 return MLX5_ESW_VPORT_ADMIN_STATE_AUTO;
3672 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
3675 struct mlx5e_priv *priv = netdev_priv(dev);
3676 struct mlx5_core_dev *mdev = priv->mdev;
3678 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
3679 mlx5_ifla_link2vport(link_state));
3682 static int mlx5e_get_vf_config(struct net_device *dev,
3683 int vf, struct ifla_vf_info *ivi)
3685 struct mlx5e_priv *priv = netdev_priv(dev);
3686 struct mlx5_core_dev *mdev = priv->mdev;
3689 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
3692 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
3696 static int mlx5e_get_vf_stats(struct net_device *dev,
3697 int vf, struct ifla_vf_stats *vf_stats)
3699 struct mlx5e_priv *priv = netdev_priv(dev);
3700 struct mlx5_core_dev *mdev = priv->mdev;
3702 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
3707 static void mlx5e_add_vxlan_port(struct net_device *netdev,
3708 struct udp_tunnel_info *ti)
3710 struct mlx5e_priv *priv = netdev_priv(netdev);
3712 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3715 if (!mlx5e_vxlan_allowed(priv->mdev))
3718 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 1);
3721 static void mlx5e_del_vxlan_port(struct net_device *netdev,
3722 struct udp_tunnel_info *ti)
3724 struct mlx5e_priv *priv = netdev_priv(netdev);
3726 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3729 if (!mlx5e_vxlan_allowed(priv->mdev))
3732 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 0);
3735 static netdev_features_t mlx5e_tunnel_features_check(struct mlx5e_priv *priv,
3736 struct sk_buff *skb,
3737 netdev_features_t features)
3739 unsigned int offset = 0;
3740 struct udphdr *udph;
3744 switch (vlan_get_protocol(skb)) {
3745 case htons(ETH_P_IP):
3746 proto = ip_hdr(skb)->protocol;
3748 case htons(ETH_P_IPV6):
3749 proto = ipv6_find_hdr(skb, &offset, -1, NULL, NULL);
3759 udph = udp_hdr(skb);
3760 port = be16_to_cpu(udph->dest);
3762 /* Verify if UDP port is being offloaded by HW */
3763 if (mlx5e_vxlan_lookup_port(priv, port))
3768 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
3769 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3772 static netdev_features_t mlx5e_features_check(struct sk_buff *skb,
3773 struct net_device *netdev,
3774 netdev_features_t features)
3776 struct mlx5e_priv *priv = netdev_priv(netdev);
3778 features = vlan_features_check(skb, features);
3779 features = vxlan_features_check(skb, features);
3781 #ifdef CONFIG_MLX5_EN_IPSEC
3782 if (mlx5e_ipsec_feature_check(skb, netdev, features))
3786 /* Validate if the tunneled packet is being offloaded by HW */
3787 if (skb->encapsulation &&
3788 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
3789 return mlx5e_tunnel_features_check(priv, skb, features);
3794 static bool mlx5e_tx_timeout_eq_recover(struct net_device *dev,
3795 struct mlx5e_txqsq *sq)
3797 struct mlx5_eq *eq = sq->cq.mcq.eq;
3800 netdev_err(dev, "EQ 0x%x: Cons = 0x%x, irqn = 0x%x\n",
3801 eq->eqn, eq->cons_index, eq->irqn);
3803 eqe_count = mlx5_eq_poll_irq_disabled(eq);
3807 netdev_err(dev, "Recover %d eqes on EQ 0x%x\n", eqe_count, eq->eqn);
3808 sq->channel->stats.eq_rearm++;
3812 static void mlx5e_tx_timeout_work(struct work_struct *work)
3814 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
3816 struct net_device *dev = priv->netdev;
3817 bool reopen_channels = false;
3821 mutex_lock(&priv->state_lock);
3823 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3826 for (i = 0; i < priv->channels.num * priv->channels.params.num_tc; i++) {
3827 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, i);
3828 struct mlx5e_txqsq *sq = priv->txq2sq[i];
3830 if (!netif_xmit_stopped(dev_queue))
3834 "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",
3835 i, sq->sqn, sq->cq.mcq.cqn, sq->cc, sq->pc,
3836 jiffies_to_usecs(jiffies - dev_queue->trans_start));
3838 /* If we recover a lost interrupt, most likely TX timeout will
3839 * be resolved, skip reopening channels
3841 if (!mlx5e_tx_timeout_eq_recover(dev, sq)) {
3842 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
3843 reopen_channels = true;
3847 if (!reopen_channels)
3850 mlx5e_close_locked(dev);
3851 err = mlx5e_open_locked(dev);
3853 netdev_err(priv->netdev,
3854 "mlx5e_open_locked failed recovering from a tx_timeout, err(%d).\n",
3858 mutex_unlock(&priv->state_lock);
3862 static void mlx5e_tx_timeout(struct net_device *dev)
3864 struct mlx5e_priv *priv = netdev_priv(dev);
3866 netdev_err(dev, "TX timeout detected\n");
3867 queue_work(priv->wq, &priv->tx_timeout_work);
3870 static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog)
3872 struct mlx5e_priv *priv = netdev_priv(netdev);
3873 struct bpf_prog *old_prog;
3875 bool reset, was_opened;
3878 mutex_lock(&priv->state_lock);
3880 if ((netdev->features & NETIF_F_LRO) && prog) {
3881 netdev_warn(netdev, "can't set XDP while LRO is on, disable LRO first\n");
3886 if ((netdev->features & NETIF_F_HW_ESP) && prog) {
3887 netdev_warn(netdev, "can't set XDP with IPSec offload\n");
3892 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
3893 /* no need for full reset when exchanging programs */
3894 reset = (!priv->channels.params.xdp_prog || !prog);
3896 if (was_opened && reset)
3897 mlx5e_close_locked(netdev);
3898 if (was_opened && !reset) {
3899 /* num_channels is invariant here, so we can take the
3900 * batched reference right upfront.
3902 prog = bpf_prog_add(prog, priv->channels.num);
3904 err = PTR_ERR(prog);
3909 /* exchange programs, extra prog reference we got from caller
3910 * as long as we don't fail from this point onwards.
3912 old_prog = xchg(&priv->channels.params.xdp_prog, prog);
3914 bpf_prog_put(old_prog);
3916 if (reset) /* change RQ type according to priv->xdp_prog */
3917 mlx5e_set_rq_type(priv->mdev, &priv->channels.params);
3919 if (was_opened && reset)
3920 mlx5e_open_locked(netdev);
3922 if (!test_bit(MLX5E_STATE_OPENED, &priv->state) || reset)
3925 /* exchanging programs w/o reset, we update ref counts on behalf
3926 * of the channels RQs here.
3928 for (i = 0; i < priv->channels.num; i++) {
3929 struct mlx5e_channel *c = priv->channels.c[i];
3931 clear_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
3932 napi_synchronize(&c->napi);
3933 /* prevent mlx5e_poll_rx_cq from accessing rq->xdp_prog */
3935 old_prog = xchg(&c->rq.xdp_prog, prog);
3937 set_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
3938 /* napi_schedule in case we have missed anything */
3939 napi_schedule(&c->napi);
3942 bpf_prog_put(old_prog);
3946 mutex_unlock(&priv->state_lock);
3950 static u32 mlx5e_xdp_query(struct net_device *dev)
3952 struct mlx5e_priv *priv = netdev_priv(dev);
3953 const struct bpf_prog *xdp_prog;
3956 mutex_lock(&priv->state_lock);
3957 xdp_prog = priv->channels.params.xdp_prog;
3959 prog_id = xdp_prog->aux->id;
3960 mutex_unlock(&priv->state_lock);
3965 static int mlx5e_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3967 switch (xdp->command) {
3968 case XDP_SETUP_PROG:
3969 return mlx5e_xdp_set(dev, xdp->prog);
3970 case XDP_QUERY_PROG:
3971 xdp->prog_id = mlx5e_xdp_query(dev);
3972 xdp->prog_attached = !!xdp->prog_id;
3979 #ifdef CONFIG_NET_POLL_CONTROLLER
3980 /* Fake "interrupt" called by netpoll (eg netconsole) to send skbs without
3981 * reenabling interrupts.
3983 static void mlx5e_netpoll(struct net_device *dev)
3985 struct mlx5e_priv *priv = netdev_priv(dev);
3986 struct mlx5e_channels *chs = &priv->channels;
3990 for (i = 0; i < chs->num; i++)
3991 napi_schedule(&chs->c[i]->napi);
3995 static const struct net_device_ops mlx5e_netdev_ops = {
3996 .ndo_open = mlx5e_open,
3997 .ndo_stop = mlx5e_close,
3998 .ndo_start_xmit = mlx5e_xmit,
3999 .ndo_setup_tc = mlx5e_setup_tc,
4000 .ndo_select_queue = mlx5e_select_queue,
4001 .ndo_get_stats64 = mlx5e_get_stats,
4002 .ndo_set_rx_mode = mlx5e_set_rx_mode,
4003 .ndo_set_mac_address = mlx5e_set_mac,
4004 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
4005 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
4006 .ndo_set_features = mlx5e_set_features,
4007 .ndo_fix_features = mlx5e_fix_features,
4008 .ndo_change_mtu = mlx5e_change_mtu,
4009 .ndo_do_ioctl = mlx5e_ioctl,
4010 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
4011 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
4012 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
4013 .ndo_features_check = mlx5e_features_check,
4014 #ifdef CONFIG_RFS_ACCEL
4015 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
4017 .ndo_tx_timeout = mlx5e_tx_timeout,
4018 .ndo_bpf = mlx5e_xdp,
4019 #ifdef CONFIG_NET_POLL_CONTROLLER
4020 .ndo_poll_controller = mlx5e_netpoll,
4022 #ifdef CONFIG_MLX5_ESWITCH
4023 /* SRIOV E-Switch NDOs */
4024 .ndo_set_vf_mac = mlx5e_set_vf_mac,
4025 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
4026 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
4027 .ndo_set_vf_trust = mlx5e_set_vf_trust,
4028 .ndo_set_vf_rate = mlx5e_set_vf_rate,
4029 .ndo_get_vf_config = mlx5e_get_vf_config,
4030 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
4031 .ndo_get_vf_stats = mlx5e_get_vf_stats,
4032 .ndo_has_offload_stats = mlx5e_has_offload_stats,
4033 .ndo_get_offload_stats = mlx5e_get_offload_stats,
4037 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
4039 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
4041 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
4042 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
4043 !MLX5_CAP_ETH(mdev, csum_cap) ||
4044 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
4045 !MLX5_CAP_ETH(mdev, vlan_cap) ||
4046 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
4047 MLX5_CAP_FLOWTABLE(mdev,
4048 flow_table_properties_nic_receive.max_ft_level)
4050 mlx5_core_warn(mdev,
4051 "Not creating net device, some required device capabilities are missing\n");
4054 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
4055 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
4056 if (!MLX5_CAP_GEN(mdev, cq_moderation))
4057 mlx5_core_warn(mdev, "CQ moderation is not supported\n");
4062 void mlx5e_build_default_indir_rqt(u32 *indirection_rqt, int len,
4067 for (i = 0; i < len; i++)
4068 indirection_rqt[i] = i % num_channels;
4071 static bool slow_pci_heuristic(struct mlx5_core_dev *mdev)
4076 mlx5e_get_max_linkspeed(mdev, &link_speed);
4077 pci_bw = pcie_bandwidth_available(mdev->pdev, NULL, NULL, NULL);
4078 mlx5_core_dbg_once(mdev, "Max link speed = %d, PCI BW = %d\n",
4079 link_speed, pci_bw);
4081 #define MLX5E_SLOW_PCI_RATIO (2)
4083 return link_speed && pci_bw &&
4084 link_speed > MLX5E_SLOW_PCI_RATIO * pci_bw;
4087 void mlx5e_set_tx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4089 params->tx_cq_moderation.cq_period_mode = cq_period_mode;
4091 params->tx_cq_moderation.pkts =
4092 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
4093 params->tx_cq_moderation.usec =
4094 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
4096 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4097 params->tx_cq_moderation.usec =
4098 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC_FROM_CQE;
4100 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_TX_CQE_BASED_MODER,
4101 params->tx_cq_moderation.cq_period_mode ==
4102 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4105 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4107 params->rx_cq_moderation.cq_period_mode = cq_period_mode;
4109 params->rx_cq_moderation.pkts =
4110 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
4111 params->rx_cq_moderation.usec =
4112 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
4114 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4115 params->rx_cq_moderation.usec =
4116 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
4118 if (params->rx_dim_enabled) {
4119 switch (cq_period_mode) {
4120 case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
4121 params->rx_cq_moderation =
4122 net_dim_get_def_profile(NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE);
4124 case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
4126 params->rx_cq_moderation =
4127 net_dim_get_def_profile(NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE);
4131 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_BASED_MODER,
4132 params->rx_cq_moderation.cq_period_mode ==
4133 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4136 static u32 mlx5e_choose_lro_timeout(struct mlx5_core_dev *mdev, u32 wanted_timeout)
4140 /* The supported periods are organized in ascending order */
4141 for (i = 0; i < MLX5E_LRO_TIMEOUT_ARR_SIZE - 1; i++)
4142 if (MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]) >= wanted_timeout)
4145 return MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]);
4148 void mlx5e_build_nic_params(struct mlx5_core_dev *mdev,
4149 struct mlx5e_params *params,
4150 u16 max_channels, u16 mtu)
4152 u8 rx_cq_period_mode;
4154 params->sw_mtu = mtu;
4155 params->hard_mtu = MLX5E_ETH_HARD_MTU;
4156 params->num_channels = max_channels;
4160 params->log_sq_size = is_kdump_kernel() ?
4161 MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE :
4162 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
4164 /* set CQE compression */
4165 params->rx_cqe_compress_def = false;
4166 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
4167 MLX5_CAP_GEN(mdev, vport_group_manager))
4168 params->rx_cqe_compress_def = slow_pci_heuristic(mdev);
4170 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS, params->rx_cqe_compress_def);
4173 if (mlx5e_striding_rq_possible(mdev, params))
4174 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ,
4175 !slow_pci_heuristic(mdev));
4176 mlx5e_set_rq_type(mdev, params);
4177 mlx5e_init_rq_type_params(mdev, params);
4181 /* TODO: && MLX5_CAP_ETH(mdev, lro_cap) */
4182 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
4183 if (!mlx5e_rx_mpwqe_is_linear_skb(mdev, params))
4184 params->lro_en = !slow_pci_heuristic(mdev);
4185 params->lro_timeout = mlx5e_choose_lro_timeout(mdev, MLX5E_DEFAULT_LRO_TIMEOUT);
4187 /* CQ moderation params */
4188 rx_cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
4189 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
4190 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
4191 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4192 mlx5e_set_rx_cq_mode_params(params, rx_cq_period_mode);
4193 mlx5e_set_tx_cq_mode_params(params, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
4196 params->tx_min_inline_mode = mlx5e_params_calculate_tx_min_inline(mdev);
4199 params->rss_hfunc = ETH_RSS_HASH_XOR;
4200 netdev_rss_key_fill(params->toeplitz_hash_key, sizeof(params->toeplitz_hash_key));
4201 mlx5e_build_default_indir_rqt(params->indirection_rqt,
4202 MLX5E_INDIR_RQT_SIZE, max_channels);
4205 static void mlx5e_build_nic_netdev_priv(struct mlx5_core_dev *mdev,
4206 struct net_device *netdev,
4207 const struct mlx5e_profile *profile,
4210 struct mlx5e_priv *priv = netdev_priv(netdev);
4213 priv->netdev = netdev;
4214 priv->profile = profile;
4215 priv->ppriv = ppriv;
4216 priv->msglevel = MLX5E_MSG_LEVEL;
4218 mlx5e_build_nic_params(mdev, &priv->channels.params,
4219 profile->max_nch(mdev), netdev->mtu);
4221 mutex_init(&priv->state_lock);
4223 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
4224 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
4225 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
4226 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
4228 mlx5e_timestamp_init(priv);
4231 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
4233 struct mlx5e_priv *priv = netdev_priv(netdev);
4235 mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
4236 if (is_zero_ether_addr(netdev->dev_addr) &&
4237 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
4238 eth_hw_addr_random(netdev);
4239 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
4243 #if IS_ENABLED(CONFIG_MLX5_ESWITCH)
4244 static const struct switchdev_ops mlx5e_switchdev_ops = {
4245 .switchdev_port_attr_get = mlx5e_attr_get,
4249 static void mlx5e_build_nic_netdev(struct net_device *netdev)
4251 struct mlx5e_priv *priv = netdev_priv(netdev);
4252 struct mlx5_core_dev *mdev = priv->mdev;
4256 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
4258 netdev->netdev_ops = &mlx5e_netdev_ops;
4260 #ifdef CONFIG_MLX5_CORE_EN_DCB
4261 if (MLX5_CAP_GEN(mdev, vport_group_manager) && MLX5_CAP_GEN(mdev, qos))
4262 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
4265 netdev->watchdog_timeo = 15 * HZ;
4267 netdev->ethtool_ops = &mlx5e_ethtool_ops;
4269 netdev->vlan_features |= NETIF_F_SG;
4270 netdev->vlan_features |= NETIF_F_IP_CSUM;
4271 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
4272 netdev->vlan_features |= NETIF_F_GRO;
4273 netdev->vlan_features |= NETIF_F_TSO;
4274 netdev->vlan_features |= NETIF_F_TSO6;
4275 netdev->vlan_features |= NETIF_F_RXCSUM;
4276 netdev->vlan_features |= NETIF_F_RXHASH;
4278 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_TX;
4279 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_RX;
4281 if (!!MLX5_CAP_ETH(mdev, lro_cap))
4282 netdev->vlan_features |= NETIF_F_LRO;
4284 netdev->hw_features = netdev->vlan_features;
4285 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4286 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4287 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4288 netdev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
4290 if (mlx5e_vxlan_allowed(mdev) || MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4291 netdev->hw_features |= NETIF_F_GSO_PARTIAL;
4292 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
4293 netdev->hw_enc_features |= NETIF_F_IPV6_CSUM;
4294 netdev->hw_enc_features |= NETIF_F_TSO;
4295 netdev->hw_enc_features |= NETIF_F_TSO6;
4296 netdev->hw_enc_features |= NETIF_F_GSO_PARTIAL;
4299 if (mlx5e_vxlan_allowed(mdev)) {
4300 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
4301 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4302 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
4303 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4304 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
4307 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4308 netdev->hw_features |= NETIF_F_GSO_GRE |
4309 NETIF_F_GSO_GRE_CSUM;
4310 netdev->hw_enc_features |= NETIF_F_GSO_GRE |
4311 NETIF_F_GSO_GRE_CSUM;
4312 netdev->gso_partial_features |= NETIF_F_GSO_GRE |
4313 NETIF_F_GSO_GRE_CSUM;
4316 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
4319 netdev->hw_features |= NETIF_F_RXALL;
4321 if (MLX5_CAP_ETH(mdev, scatter_fcs))
4322 netdev->hw_features |= NETIF_F_RXFCS;
4324 netdev->features = netdev->hw_features;
4325 if (!priv->channels.params.lro_en)
4326 netdev->features &= ~NETIF_F_LRO;
4329 netdev->features &= ~NETIF_F_RXALL;
4331 if (!priv->channels.params.scatter_fcs_en)
4332 netdev->features &= ~NETIF_F_RXFCS;
4334 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
4335 if (FT_CAP(flow_modify_en) &&
4336 FT_CAP(modify_root) &&
4337 FT_CAP(identified_miss_table_mode) &&
4338 FT_CAP(flow_table_modify)) {
4339 netdev->hw_features |= NETIF_F_HW_TC;
4340 #ifdef CONFIG_RFS_ACCEL
4341 netdev->hw_features |= NETIF_F_NTUPLE;
4345 netdev->features |= NETIF_F_HIGHDMA;
4346 netdev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
4348 netdev->priv_flags |= IFF_UNICAST_FLT;
4350 mlx5e_set_netdev_dev_addr(netdev);
4352 #if IS_ENABLED(CONFIG_MLX5_ESWITCH)
4353 if (MLX5_VPORT_MANAGER(mdev))
4354 netdev->switchdev_ops = &mlx5e_switchdev_ops;
4357 mlx5e_ipsec_build_netdev(priv);
4360 static void mlx5e_create_q_counters(struct mlx5e_priv *priv)
4362 struct mlx5_core_dev *mdev = priv->mdev;
4365 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
4367 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
4368 priv->q_counter = 0;
4371 err = mlx5_core_alloc_q_counter(mdev, &priv->drop_rq_q_counter);
4373 mlx5_core_warn(mdev, "alloc drop RQ counter failed, %d\n", err);
4374 priv->drop_rq_q_counter = 0;
4378 static void mlx5e_destroy_q_counters(struct mlx5e_priv *priv)
4380 if (priv->q_counter)
4381 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
4383 if (priv->drop_rq_q_counter)
4384 mlx5_core_dealloc_q_counter(priv->mdev, priv->drop_rq_q_counter);
4387 static void mlx5e_nic_init(struct mlx5_core_dev *mdev,
4388 struct net_device *netdev,
4389 const struct mlx5e_profile *profile,
4392 struct mlx5e_priv *priv = netdev_priv(netdev);
4395 mlx5e_build_nic_netdev_priv(mdev, netdev, profile, ppriv);
4396 err = mlx5e_ipsec_init(priv);
4398 mlx5_core_err(mdev, "IPSec initialization failed, %d\n", err);
4399 mlx5e_build_nic_netdev(netdev);
4400 mlx5e_vxlan_init(priv);
4403 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
4405 mlx5e_ipsec_cleanup(priv);
4406 mlx5e_vxlan_cleanup(priv);
4409 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv)
4411 struct mlx5_core_dev *mdev = priv->mdev;
4414 err = mlx5e_create_indirect_rqt(priv);
4418 err = mlx5e_create_direct_rqts(priv);
4420 goto err_destroy_indirect_rqts;
4422 err = mlx5e_create_indirect_tirs(priv);
4424 goto err_destroy_direct_rqts;
4426 err = mlx5e_create_direct_tirs(priv);
4428 goto err_destroy_indirect_tirs;
4430 err = mlx5e_create_flow_steering(priv);
4432 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
4433 goto err_destroy_direct_tirs;
4436 err = mlx5e_tc_init(priv);
4438 goto err_destroy_flow_steering;
4442 err_destroy_flow_steering:
4443 mlx5e_destroy_flow_steering(priv);
4444 err_destroy_direct_tirs:
4445 mlx5e_destroy_direct_tirs(priv);
4446 err_destroy_indirect_tirs:
4447 mlx5e_destroy_indirect_tirs(priv);
4448 err_destroy_direct_rqts:
4449 mlx5e_destroy_direct_rqts(priv);
4450 err_destroy_indirect_rqts:
4451 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
4455 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv)
4457 mlx5e_tc_cleanup(priv);
4458 mlx5e_destroy_flow_steering(priv);
4459 mlx5e_destroy_direct_tirs(priv);
4460 mlx5e_destroy_indirect_tirs(priv);
4461 mlx5e_destroy_direct_rqts(priv);
4462 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
4465 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv)
4469 err = mlx5e_create_tises(priv);
4471 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
4475 #ifdef CONFIG_MLX5_CORE_EN_DCB
4476 mlx5e_dcbnl_initialize(priv);
4481 static void mlx5e_nic_enable(struct mlx5e_priv *priv)
4483 struct net_device *netdev = priv->netdev;
4484 struct mlx5_core_dev *mdev = priv->mdev;
4487 mlx5e_init_l2_addr(priv);
4489 /* Marking the link as currently not needed by the Driver */
4490 if (!netif_running(netdev))
4491 mlx5_set_port_admin_status(mdev, MLX5_PORT_DOWN);
4493 /* MTU range: 68 - hw-specific max */
4494 netdev->min_mtu = ETH_MIN_MTU;
4495 mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
4496 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
4497 mlx5e_set_dev_port_mtu(priv);
4499 mlx5_lag_add(mdev, netdev);
4501 mlx5e_enable_async_events(priv);
4503 if (MLX5_VPORT_MANAGER(priv->mdev))
4504 mlx5e_register_vport_reps(priv);
4506 if (netdev->reg_state != NETREG_REGISTERED)
4508 #ifdef CONFIG_MLX5_CORE_EN_DCB
4509 mlx5e_dcbnl_init_app(priv);
4512 queue_work(priv->wq, &priv->set_rx_mode_work);
4515 if (netif_running(netdev))
4517 netif_device_attach(netdev);
4521 static void mlx5e_nic_disable(struct mlx5e_priv *priv)
4523 struct mlx5_core_dev *mdev = priv->mdev;
4525 #ifdef CONFIG_MLX5_CORE_EN_DCB
4526 if (priv->netdev->reg_state == NETREG_REGISTERED)
4527 mlx5e_dcbnl_delete_app(priv);
4531 if (netif_running(priv->netdev))
4532 mlx5e_close(priv->netdev);
4533 netif_device_detach(priv->netdev);
4536 queue_work(priv->wq, &priv->set_rx_mode_work);
4538 if (MLX5_VPORT_MANAGER(priv->mdev))
4539 mlx5e_unregister_vport_reps(priv);
4541 mlx5e_disable_async_events(priv);
4542 mlx5_lag_remove(mdev);
4545 static const struct mlx5e_profile mlx5e_nic_profile = {
4546 .init = mlx5e_nic_init,
4547 .cleanup = mlx5e_nic_cleanup,
4548 .init_rx = mlx5e_init_nic_rx,
4549 .cleanup_rx = mlx5e_cleanup_nic_rx,
4550 .init_tx = mlx5e_init_nic_tx,
4551 .cleanup_tx = mlx5e_cleanup_nic_tx,
4552 .enable = mlx5e_nic_enable,
4553 .disable = mlx5e_nic_disable,
4554 .update_stats = mlx5e_update_ndo_stats,
4555 .max_nch = mlx5e_get_max_num_channels,
4556 .update_carrier = mlx5e_update_carrier,
4557 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe,
4558 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
4559 .max_tc = MLX5E_MAX_NUM_TC,
4562 /* mlx5e generic netdev management API (move to en_common.c) */
4564 struct net_device *mlx5e_create_netdev(struct mlx5_core_dev *mdev,
4565 const struct mlx5e_profile *profile,
4568 int nch = profile->max_nch(mdev);
4569 struct net_device *netdev;
4570 struct mlx5e_priv *priv;
4572 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
4573 nch * profile->max_tc,
4576 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
4580 #ifdef CONFIG_RFS_ACCEL
4581 netdev->rx_cpu_rmap = mdev->rmap;
4584 profile->init(mdev, netdev, profile, ppriv);
4586 netif_carrier_off(netdev);
4588 priv = netdev_priv(netdev);
4590 priv->wq = create_singlethread_workqueue("mlx5e");
4592 goto err_cleanup_nic;
4597 if (profile->cleanup)
4598 profile->cleanup(priv);
4599 free_netdev(netdev);
4604 int mlx5e_attach_netdev(struct mlx5e_priv *priv)
4606 struct mlx5_core_dev *mdev = priv->mdev;
4607 const struct mlx5e_profile *profile;
4610 profile = priv->profile;
4611 clear_bit(MLX5E_STATE_DESTROYING, &priv->state);
4613 err = profile->init_tx(priv);
4617 mlx5e_create_q_counters(priv);
4619 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
4621 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
4622 goto err_destroy_q_counters;
4625 err = profile->init_rx(priv);
4627 goto err_close_drop_rq;
4629 if (profile->enable)
4630 profile->enable(priv);
4635 mlx5e_close_drop_rq(&priv->drop_rq);
4637 err_destroy_q_counters:
4638 mlx5e_destroy_q_counters(priv);
4639 profile->cleanup_tx(priv);
4645 void mlx5e_detach_netdev(struct mlx5e_priv *priv)
4647 const struct mlx5e_profile *profile = priv->profile;
4649 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
4651 if (profile->disable)
4652 profile->disable(priv);
4653 flush_workqueue(priv->wq);
4655 profile->cleanup_rx(priv);
4656 mlx5e_close_drop_rq(&priv->drop_rq);
4657 mlx5e_destroy_q_counters(priv);
4658 profile->cleanup_tx(priv);
4659 cancel_delayed_work_sync(&priv->update_stats_work);
4662 void mlx5e_destroy_netdev(struct mlx5e_priv *priv)
4664 const struct mlx5e_profile *profile = priv->profile;
4665 struct net_device *netdev = priv->netdev;
4667 destroy_workqueue(priv->wq);
4668 if (profile->cleanup)
4669 profile->cleanup(priv);
4670 free_netdev(netdev);
4673 /* mlx5e_attach and mlx5e_detach scope should be only creating/destroying
4674 * hardware contexts and to connect it to the current netdev.
4676 static int mlx5e_attach(struct mlx5_core_dev *mdev, void *vpriv)
4678 struct mlx5e_priv *priv = vpriv;
4679 struct net_device *netdev = priv->netdev;
4682 if (netif_device_present(netdev))
4685 err = mlx5e_create_mdev_resources(mdev);
4689 err = mlx5e_attach_netdev(priv);
4691 mlx5e_destroy_mdev_resources(mdev);
4698 static void mlx5e_detach(struct mlx5_core_dev *mdev, void *vpriv)
4700 struct mlx5e_priv *priv = vpriv;
4701 struct net_device *netdev = priv->netdev;
4703 if (!netif_device_present(netdev))
4706 mlx5e_detach_netdev(priv);
4707 mlx5e_destroy_mdev_resources(mdev);
4710 static void *mlx5e_add(struct mlx5_core_dev *mdev)
4712 struct net_device *netdev;
4717 err = mlx5e_check_required_hca_cap(mdev);
4721 #ifdef CONFIG_MLX5_ESWITCH
4722 if (MLX5_VPORT_MANAGER(mdev)) {
4723 rpriv = mlx5e_alloc_nic_rep_priv(mdev);
4725 mlx5_core_warn(mdev, "Failed to alloc NIC rep priv data\n");
4731 netdev = mlx5e_create_netdev(mdev, &mlx5e_nic_profile, rpriv);
4733 mlx5_core_err(mdev, "mlx5e_create_netdev failed\n");
4734 goto err_free_rpriv;
4737 priv = netdev_priv(netdev);
4739 err = mlx5e_attach(mdev, priv);
4741 mlx5_core_err(mdev, "mlx5e_attach failed, %d\n", err);
4742 goto err_destroy_netdev;
4745 err = register_netdev(netdev);
4747 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
4751 #ifdef CONFIG_MLX5_CORE_EN_DCB
4752 mlx5e_dcbnl_init_app(priv);
4757 mlx5e_detach(mdev, priv);
4759 mlx5e_destroy_netdev(priv);
4765 static void mlx5e_remove(struct mlx5_core_dev *mdev, void *vpriv)
4767 struct mlx5e_priv *priv = vpriv;
4768 void *ppriv = priv->ppriv;
4770 #ifdef CONFIG_MLX5_CORE_EN_DCB
4771 mlx5e_dcbnl_delete_app(priv);
4773 unregister_netdev(priv->netdev);
4774 mlx5e_detach(mdev, vpriv);
4775 mlx5e_destroy_netdev(priv);
4779 static void *mlx5e_get_netdev(void *vpriv)
4781 struct mlx5e_priv *priv = vpriv;
4783 return priv->netdev;
4786 static struct mlx5_interface mlx5e_interface = {
4788 .remove = mlx5e_remove,
4789 .attach = mlx5e_attach,
4790 .detach = mlx5e_detach,
4791 .event = mlx5e_async_event,
4792 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
4793 .get_dev = mlx5e_get_netdev,
4796 void mlx5e_init(void)
4798 mlx5e_ipsec_build_inverse_table();
4799 mlx5e_build_ptys2ethtool_map();
4800 mlx5_register_interface(&mlx5e_interface);
4803 void mlx5e_cleanup(void)
4805 mlx5_unregister_interface(&mlx5e_interface);