linux/dim: Rename externally exposed macros
[muen/linux.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_main.c
1 /*
2  * Copyright (c) 2015-2016, Mellanox Technologies. All rights reserved.
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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
30  * SOFTWARE.
31  */
32
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 <net/geneve.h>
38 #include <linux/bpf.h>
39 #include <linux/if_bridge.h>
40 #include <net/page_pool.h>
41 #include "eswitch.h"
42 #include "en.h"
43 #include "en_tc.h"
44 #include "en_rep.h"
45 #include "en_accel/ipsec.h"
46 #include "en_accel/ipsec_rxtx.h"
47 #include "en_accel/en_accel.h"
48 #include "en_accel/tls.h"
49 #include "accel/ipsec.h"
50 #include "accel/tls.h"
51 #include "lib/vxlan.h"
52 #include "lib/clock.h"
53 #include "en/port.h"
54 #include "en/xdp.h"
55 #include "lib/eq.h"
56 #include "en/monitor_stats.h"
57 #include "en/reporter.h"
58 #include "en/params.h"
59
60 struct mlx5e_rq_param {
61         u32                     rqc[MLX5_ST_SZ_DW(rqc)];
62         struct mlx5_wq_param    wq;
63         struct mlx5e_rq_frags_info frags_info;
64 };
65
66 struct mlx5e_sq_param {
67         u32                        sqc[MLX5_ST_SZ_DW(sqc)];
68         struct mlx5_wq_param       wq;
69         bool                       is_mpw;
70 };
71
72 struct mlx5e_cq_param {
73         u32                        cqc[MLX5_ST_SZ_DW(cqc)];
74         struct mlx5_wq_param       wq;
75         u16                        eq_ix;
76         u8                         cq_period_mode;
77 };
78
79 struct mlx5e_channel_param {
80         struct mlx5e_rq_param      rq;
81         struct mlx5e_sq_param      sq;
82         struct mlx5e_sq_param      xdp_sq;
83         struct mlx5e_sq_param      icosq;
84         struct mlx5e_cq_param      rx_cq;
85         struct mlx5e_cq_param      tx_cq;
86         struct mlx5e_cq_param      icosq_cq;
87 };
88
89 bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
90 {
91         bool striding_rq_umr = MLX5_CAP_GEN(mdev, striding_rq) &&
92                 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
93                 MLX5_CAP_ETH(mdev, reg_umr_sq);
94         u16 max_wqe_sz_cap = MLX5_CAP_GEN(mdev, max_wqe_sz_sq);
95         bool inline_umr = MLX5E_UMR_WQE_INLINE_SZ <= max_wqe_sz_cap;
96
97         if (!striding_rq_umr)
98                 return false;
99         if (!inline_umr) {
100                 mlx5_core_warn(mdev, "Cannot support Striding RQ: UMR WQE size (%d) exceeds maximum supported (%d).\n",
101                                (int)MLX5E_UMR_WQE_INLINE_SZ, max_wqe_sz_cap);
102                 return false;
103         }
104         return true;
105 }
106
107 void mlx5e_init_rq_type_params(struct mlx5_core_dev *mdev,
108                                struct mlx5e_params *params)
109 {
110         params->log_rq_mtu_frames = is_kdump_kernel() ?
111                 MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE :
112                 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
113
114         mlx5_core_info(mdev, "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
115                        params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
116                        params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ ?
117                        BIT(mlx5e_mpwqe_get_log_rq_size(params)) :
118                        BIT(params->log_rq_mtu_frames),
119                        BIT(mlx5e_mpwqe_get_log_stride_size(mdev, params)),
120                        MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
121 }
122
123 bool mlx5e_striding_rq_possible(struct mlx5_core_dev *mdev,
124                                 struct mlx5e_params *params)
125 {
126         return mlx5e_check_fragmented_striding_rq_cap(mdev) &&
127                 !MLX5_IPSEC_DEV(mdev) &&
128                 !(params->xdp_prog && !mlx5e_rx_mpwqe_is_linear_skb(mdev, params));
129 }
130
131 void mlx5e_set_rq_type(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
132 {
133         params->rq_wq_type = mlx5e_striding_rq_possible(mdev, params) &&
134                 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ) ?
135                 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
136                 MLX5_WQ_TYPE_CYCLIC;
137 }
138
139 void mlx5e_update_carrier(struct mlx5e_priv *priv)
140 {
141         struct mlx5_core_dev *mdev = priv->mdev;
142         u8 port_state;
143
144         port_state = mlx5_query_vport_state(mdev,
145                                             MLX5_VPORT_STATE_OP_MOD_VNIC_VPORT,
146                                             0);
147
148         if (port_state == VPORT_STATE_UP) {
149                 netdev_info(priv->netdev, "Link up\n");
150                 netif_carrier_on(priv->netdev);
151         } else {
152                 netdev_info(priv->netdev, "Link down\n");
153                 netif_carrier_off(priv->netdev);
154         }
155 }
156
157 static void mlx5e_update_carrier_work(struct work_struct *work)
158 {
159         struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
160                                                update_carrier_work);
161
162         mutex_lock(&priv->state_lock);
163         if (test_bit(MLX5E_STATE_OPENED, &priv->state))
164                 if (priv->profile->update_carrier)
165                         priv->profile->update_carrier(priv);
166         mutex_unlock(&priv->state_lock);
167 }
168
169 void mlx5e_update_stats(struct mlx5e_priv *priv)
170 {
171         int i;
172
173         for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
174                 if (mlx5e_stats_grps[i].update_stats)
175                         mlx5e_stats_grps[i].update_stats(priv);
176 }
177
178 void mlx5e_update_ndo_stats(struct mlx5e_priv *priv)
179 {
180         int i;
181
182         for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
183                 if (mlx5e_stats_grps[i].update_stats_mask &
184                     MLX5E_NDO_UPDATE_STATS)
185                         mlx5e_stats_grps[i].update_stats(priv);
186 }
187
188 static void mlx5e_update_stats_work(struct work_struct *work)
189 {
190         struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
191                                                update_stats_work);
192
193         mutex_lock(&priv->state_lock);
194         priv->profile->update_stats(priv);
195         mutex_unlock(&priv->state_lock);
196 }
197
198 void mlx5e_queue_update_stats(struct mlx5e_priv *priv)
199 {
200         if (!priv->profile->update_stats)
201                 return;
202
203         if (unlikely(test_bit(MLX5E_STATE_DESTROYING, &priv->state)))
204                 return;
205
206         queue_work(priv->wq, &priv->update_stats_work);
207 }
208
209 static int async_event(struct notifier_block *nb, unsigned long event, void *data)
210 {
211         struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb);
212         struct mlx5_eqe   *eqe = data;
213
214         if (event != MLX5_EVENT_TYPE_PORT_CHANGE)
215                 return NOTIFY_DONE;
216
217         switch (eqe->sub_type) {
218         case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
219         case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
220                 queue_work(priv->wq, &priv->update_carrier_work);
221                 break;
222         default:
223                 return NOTIFY_DONE;
224         }
225
226         return NOTIFY_OK;
227 }
228
229 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
230 {
231         priv->events_nb.notifier_call = async_event;
232         mlx5_notifier_register(priv->mdev, &priv->events_nb);
233 }
234
235 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
236 {
237         mlx5_notifier_unregister(priv->mdev, &priv->events_nb);
238 }
239
240 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq,
241                                        struct mlx5e_icosq *sq,
242                                        struct mlx5e_umr_wqe *wqe)
243 {
244         struct mlx5_wqe_ctrl_seg      *cseg = &wqe->ctrl;
245         struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
246         u8 ds_cnt = DIV_ROUND_UP(MLX5E_UMR_WQE_INLINE_SZ, MLX5_SEND_WQE_DS);
247
248         cseg->qpn_ds    = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
249                                       ds_cnt);
250         cseg->fm_ce_se  = MLX5_WQE_CTRL_CQ_UPDATE;
251         cseg->imm       = rq->mkey_be;
252
253         ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN | MLX5_UMR_INLINE;
254         ucseg->xlt_octowords =
255                 cpu_to_be16(MLX5_MTT_OCTW(MLX5_MPWRQ_PAGES_PER_WQE));
256         ucseg->mkey_mask     = cpu_to_be64(MLX5_MKEY_MASK_FREE);
257 }
258
259 static u32 mlx5e_rqwq_get_size(struct mlx5e_rq *rq)
260 {
261         switch (rq->wq_type) {
262         case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
263                 return mlx5_wq_ll_get_size(&rq->mpwqe.wq);
264         default:
265                 return mlx5_wq_cyc_get_size(&rq->wqe.wq);
266         }
267 }
268
269 static u32 mlx5e_rqwq_get_cur_sz(struct mlx5e_rq *rq)
270 {
271         switch (rq->wq_type) {
272         case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
273                 return rq->mpwqe.wq.cur_sz;
274         default:
275                 return rq->wqe.wq.cur_sz;
276         }
277 }
278
279 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq,
280                                      struct mlx5e_channel *c)
281 {
282         int wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
283
284         rq->mpwqe.info = kvzalloc_node(array_size(wq_sz,
285                                                   sizeof(*rq->mpwqe.info)),
286                                        GFP_KERNEL, cpu_to_node(c->cpu));
287         if (!rq->mpwqe.info)
288                 return -ENOMEM;
289
290         mlx5e_build_umr_wqe(rq, &c->icosq, &rq->mpwqe.umr_wqe);
291
292         return 0;
293 }
294
295 static int mlx5e_create_umr_mkey(struct mlx5_core_dev *mdev,
296                                  u64 npages, u8 page_shift,
297                                  struct mlx5_core_mkey *umr_mkey)
298 {
299         int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
300         void *mkc;
301         u32 *in;
302         int err;
303
304         in = kvzalloc(inlen, GFP_KERNEL);
305         if (!in)
306                 return -ENOMEM;
307
308         mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
309
310         MLX5_SET(mkc, mkc, free, 1);
311         MLX5_SET(mkc, mkc, umr_en, 1);
312         MLX5_SET(mkc, mkc, lw, 1);
313         MLX5_SET(mkc, mkc, lr, 1);
314         MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT);
315
316         MLX5_SET(mkc, mkc, qpn, 0xffffff);
317         MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.pdn);
318         MLX5_SET64(mkc, mkc, len, npages << page_shift);
319         MLX5_SET(mkc, mkc, translations_octword_size,
320                  MLX5_MTT_OCTW(npages));
321         MLX5_SET(mkc, mkc, log_page_size, page_shift);
322
323         err = mlx5_core_create_mkey(mdev, umr_mkey, in, inlen);
324
325         kvfree(in);
326         return err;
327 }
328
329 static int mlx5e_create_rq_umr_mkey(struct mlx5_core_dev *mdev, struct mlx5e_rq *rq)
330 {
331         u64 num_mtts = MLX5E_REQUIRED_MTTS(mlx5_wq_ll_get_size(&rq->mpwqe.wq));
332
333         return mlx5e_create_umr_mkey(mdev, num_mtts, PAGE_SHIFT, &rq->umr_mkey);
334 }
335
336 static inline u64 mlx5e_get_mpwqe_offset(struct mlx5e_rq *rq, u16 wqe_ix)
337 {
338         return (wqe_ix << MLX5E_LOG_ALIGNED_MPWQE_PPW) << PAGE_SHIFT;
339 }
340
341 static void mlx5e_init_frags_partition(struct mlx5e_rq *rq)
342 {
343         struct mlx5e_wqe_frag_info next_frag, *prev;
344         int i;
345
346         next_frag.di = &rq->wqe.di[0];
347         next_frag.offset = 0;
348         prev = NULL;
349
350         for (i = 0; i < mlx5_wq_cyc_get_size(&rq->wqe.wq); i++) {
351                 struct mlx5e_rq_frag_info *frag_info = &rq->wqe.info.arr[0];
352                 struct mlx5e_wqe_frag_info *frag =
353                         &rq->wqe.frags[i << rq->wqe.info.log_num_frags];
354                 int f;
355
356                 for (f = 0; f < rq->wqe.info.num_frags; f++, frag++) {
357                         if (next_frag.offset + frag_info[f].frag_stride > PAGE_SIZE) {
358                                 next_frag.di++;
359                                 next_frag.offset = 0;
360                                 if (prev)
361                                         prev->last_in_page = true;
362                         }
363                         *frag = next_frag;
364
365                         /* prepare next */
366                         next_frag.offset += frag_info[f].frag_stride;
367                         prev = frag;
368                 }
369         }
370
371         if (prev)
372                 prev->last_in_page = true;
373 }
374
375 static int mlx5e_init_di_list(struct mlx5e_rq *rq,
376                               int wq_sz, int cpu)
377 {
378         int len = wq_sz << rq->wqe.info.log_num_frags;
379
380         rq->wqe.di = kvzalloc_node(array_size(len, sizeof(*rq->wqe.di)),
381                                    GFP_KERNEL, cpu_to_node(cpu));
382         if (!rq->wqe.di)
383                 return -ENOMEM;
384
385         mlx5e_init_frags_partition(rq);
386
387         return 0;
388 }
389
390 static void mlx5e_free_di_list(struct mlx5e_rq *rq)
391 {
392         kvfree(rq->wqe.di);
393 }
394
395 static int mlx5e_alloc_rq(struct mlx5e_channel *c,
396                           struct mlx5e_params *params,
397                           struct mlx5e_rq_param *rqp,
398                           struct mlx5e_rq *rq)
399 {
400         struct page_pool_params pp_params = { 0 };
401         struct mlx5_core_dev *mdev = c->mdev;
402         void *rqc = rqp->rqc;
403         void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
404         u32 pool_size;
405         int wq_sz;
406         int err;
407         int i;
408
409         rqp->wq.db_numa_node = cpu_to_node(c->cpu);
410
411         rq->wq_type = params->rq_wq_type;
412         rq->pdev    = c->pdev;
413         rq->netdev  = c->netdev;
414         rq->tstamp  = c->tstamp;
415         rq->clock   = &mdev->clock;
416         rq->channel = c;
417         rq->ix      = c->ix;
418         rq->mdev    = mdev;
419         rq->hw_mtu  = MLX5E_SW2HW_MTU(params, params->sw_mtu);
420         rq->stats   = &c->priv->channel_stats[c->ix].rq;
421
422         rq->xdp_prog = params->xdp_prog ? bpf_prog_inc(params->xdp_prog) : NULL;
423         if (IS_ERR(rq->xdp_prog)) {
424                 err = PTR_ERR(rq->xdp_prog);
425                 rq->xdp_prog = NULL;
426                 goto err_rq_wq_destroy;
427         }
428
429         err = xdp_rxq_info_reg(&rq->xdp_rxq, rq->netdev, rq->ix);
430         if (err < 0)
431                 goto err_rq_wq_destroy;
432
433         rq->buff.map_dir = rq->xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
434         rq->buff.headroom = mlx5e_get_rq_headroom(mdev, params);
435         pool_size = 1 << params->log_rq_mtu_frames;
436
437         switch (rq->wq_type) {
438         case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
439                 err = mlx5_wq_ll_create(mdev, &rqp->wq, rqc_wq, &rq->mpwqe.wq,
440                                         &rq->wq_ctrl);
441                 if (err)
442                         return err;
443
444                 rq->mpwqe.wq.db = &rq->mpwqe.wq.db[MLX5_RCV_DBR];
445
446                 wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
447
448                 pool_size = MLX5_MPWRQ_PAGES_PER_WQE << mlx5e_mpwqe_get_log_rq_size(params);
449
450                 rq->post_wqes = mlx5e_post_rx_mpwqes;
451                 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
452
453                 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe_mpwqe;
454 #ifdef CONFIG_MLX5_EN_IPSEC
455                 if (MLX5_IPSEC_DEV(mdev)) {
456                         err = -EINVAL;
457                         netdev_err(c->netdev, "MPWQE RQ with IPSec offload not supported\n");
458                         goto err_rq_wq_destroy;
459                 }
460 #endif
461                 if (!rq->handle_rx_cqe) {
462                         err = -EINVAL;
463                         netdev_err(c->netdev, "RX handler of MPWQE RQ is not set, err %d\n", err);
464                         goto err_rq_wq_destroy;
465                 }
466
467                 rq->mpwqe.skb_from_cqe_mpwrq =
468                         mlx5e_rx_mpwqe_is_linear_skb(mdev, params) ?
469                         mlx5e_skb_from_cqe_mpwrq_linear :
470                         mlx5e_skb_from_cqe_mpwrq_nonlinear;
471                 rq->mpwqe.log_stride_sz = mlx5e_mpwqe_get_log_stride_size(mdev, params);
472                 rq->mpwqe.num_strides = BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params));
473
474                 err = mlx5e_create_rq_umr_mkey(mdev, rq);
475                 if (err)
476                         goto err_rq_wq_destroy;
477                 rq->mkey_be = cpu_to_be32(rq->umr_mkey.key);
478
479                 err = mlx5e_rq_alloc_mpwqe_info(rq, c);
480                 if (err)
481                         goto err_free;
482                 break;
483         default: /* MLX5_WQ_TYPE_CYCLIC */
484                 err = mlx5_wq_cyc_create(mdev, &rqp->wq, rqc_wq, &rq->wqe.wq,
485                                          &rq->wq_ctrl);
486                 if (err)
487                         return err;
488
489                 rq->wqe.wq.db = &rq->wqe.wq.db[MLX5_RCV_DBR];
490
491                 wq_sz = mlx5_wq_cyc_get_size(&rq->wqe.wq);
492
493                 rq->wqe.info = rqp->frags_info;
494                 rq->wqe.frags =
495                         kvzalloc_node(array_size(sizeof(*rq->wqe.frags),
496                                         (wq_sz << rq->wqe.info.log_num_frags)),
497                                       GFP_KERNEL, cpu_to_node(c->cpu));
498                 if (!rq->wqe.frags) {
499                         err = -ENOMEM;
500                         goto err_free;
501                 }
502
503                 err = mlx5e_init_di_list(rq, wq_sz, c->cpu);
504                 if (err)
505                         goto err_free;
506                 rq->post_wqes = mlx5e_post_rx_wqes;
507                 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
508
509 #ifdef CONFIG_MLX5_EN_IPSEC
510                 if (c->priv->ipsec)
511                         rq->handle_rx_cqe = mlx5e_ipsec_handle_rx_cqe;
512                 else
513 #endif
514                         rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe;
515                 if (!rq->handle_rx_cqe) {
516                         err = -EINVAL;
517                         netdev_err(c->netdev, "RX handler of RQ is not set, err %d\n", err);
518                         goto err_free;
519                 }
520
521                 rq->wqe.skb_from_cqe = mlx5e_rx_is_linear_skb(params) ?
522                         mlx5e_skb_from_cqe_linear :
523                         mlx5e_skb_from_cqe_nonlinear;
524                 rq->mkey_be = c->mkey_be;
525         }
526
527         /* Create a page_pool and register it with rxq */
528         pp_params.order     = 0;
529         pp_params.flags     = 0; /* No-internal DMA mapping in page_pool */
530         pp_params.pool_size = pool_size;
531         pp_params.nid       = cpu_to_node(c->cpu);
532         pp_params.dev       = c->pdev;
533         pp_params.dma_dir   = rq->buff.map_dir;
534
535         /* page_pool can be used even when there is no rq->xdp_prog,
536          * given page_pool does not handle DMA mapping there is no
537          * required state to clear. And page_pool gracefully handle
538          * elevated refcnt.
539          */
540         rq->page_pool = page_pool_create(&pp_params);
541         if (IS_ERR(rq->page_pool)) {
542                 err = PTR_ERR(rq->page_pool);
543                 rq->page_pool = NULL;
544                 goto err_free;
545         }
546         err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
547                                          MEM_TYPE_PAGE_POOL, rq->page_pool);
548         if (err)
549                 goto err_free;
550
551         for (i = 0; i < wq_sz; i++) {
552                 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
553                         struct mlx5e_rx_wqe_ll *wqe =
554                                 mlx5_wq_ll_get_wqe(&rq->mpwqe.wq, i);
555                         u32 byte_count =
556                                 rq->mpwqe.num_strides << rq->mpwqe.log_stride_sz;
557                         u64 dma_offset = mlx5e_get_mpwqe_offset(rq, i);
558
559                         wqe->data[0].addr = cpu_to_be64(dma_offset + rq->buff.headroom);
560                         wqe->data[0].byte_count = cpu_to_be32(byte_count);
561                         wqe->data[0].lkey = rq->mkey_be;
562                 } else {
563                         struct mlx5e_rx_wqe_cyc *wqe =
564                                 mlx5_wq_cyc_get_wqe(&rq->wqe.wq, i);
565                         int f;
566
567                         for (f = 0; f < rq->wqe.info.num_frags; f++) {
568                                 u32 frag_size = rq->wqe.info.arr[f].frag_size |
569                                         MLX5_HW_START_PADDING;
570
571                                 wqe->data[f].byte_count = cpu_to_be32(frag_size);
572                                 wqe->data[f].lkey = rq->mkey_be;
573                         }
574                         /* check if num_frags is not a pow of two */
575                         if (rq->wqe.info.num_frags < (1 << rq->wqe.info.log_num_frags)) {
576                                 wqe->data[f].byte_count = 0;
577                                 wqe->data[f].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
578                                 wqe->data[f].addr = 0;
579                         }
580                 }
581         }
582
583         INIT_WORK(&rq->dim.work, mlx5e_rx_dim_work);
584
585         switch (params->rx_cq_moderation.cq_period_mode) {
586         case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
587                 rq->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_CQE;
588                 break;
589         case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
590         default:
591                 rq->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
592         }
593
594         rq->page_cache.head = 0;
595         rq->page_cache.tail = 0;
596
597         return 0;
598
599 err_free:
600         switch (rq->wq_type) {
601         case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
602                 kvfree(rq->mpwqe.info);
603                 mlx5_core_destroy_mkey(mdev, &rq->umr_mkey);
604                 break;
605         default: /* MLX5_WQ_TYPE_CYCLIC */
606                 kvfree(rq->wqe.frags);
607                 mlx5e_free_di_list(rq);
608         }
609
610 err_rq_wq_destroy:
611         if (rq->xdp_prog)
612                 bpf_prog_put(rq->xdp_prog);
613         xdp_rxq_info_unreg(&rq->xdp_rxq);
614         if (rq->page_pool)
615                 page_pool_destroy(rq->page_pool);
616         mlx5_wq_destroy(&rq->wq_ctrl);
617
618         return err;
619 }
620
621 static void mlx5e_free_rq(struct mlx5e_rq *rq)
622 {
623         int i;
624
625         if (rq->xdp_prog)
626                 bpf_prog_put(rq->xdp_prog);
627
628         xdp_rxq_info_unreg(&rq->xdp_rxq);
629         if (rq->page_pool)
630                 page_pool_destroy(rq->page_pool);
631
632         switch (rq->wq_type) {
633         case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
634                 kvfree(rq->mpwqe.info);
635                 mlx5_core_destroy_mkey(rq->mdev, &rq->umr_mkey);
636                 break;
637         default: /* MLX5_WQ_TYPE_CYCLIC */
638                 kvfree(rq->wqe.frags);
639                 mlx5e_free_di_list(rq);
640         }
641
642         for (i = rq->page_cache.head; i != rq->page_cache.tail;
643              i = (i + 1) & (MLX5E_CACHE_SIZE - 1)) {
644                 struct mlx5e_dma_info *dma_info = &rq->page_cache.page_cache[i];
645
646                 mlx5e_page_release(rq, dma_info, false);
647         }
648         mlx5_wq_destroy(&rq->wq_ctrl);
649 }
650
651 static int mlx5e_create_rq(struct mlx5e_rq *rq,
652                            struct mlx5e_rq_param *param)
653 {
654         struct mlx5_core_dev *mdev = rq->mdev;
655
656         void *in;
657         void *rqc;
658         void *wq;
659         int inlen;
660         int err;
661
662         inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
663                 sizeof(u64) * rq->wq_ctrl.buf.npages;
664         in = kvzalloc(inlen, GFP_KERNEL);
665         if (!in)
666                 return -ENOMEM;
667
668         rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
669         wq  = MLX5_ADDR_OF(rqc, rqc, wq);
670
671         memcpy(rqc, param->rqc, sizeof(param->rqc));
672
673         MLX5_SET(rqc,  rqc, cqn,                rq->cq.mcq.cqn);
674         MLX5_SET(rqc,  rqc, state,              MLX5_RQC_STATE_RST);
675         MLX5_SET(wq,   wq,  log_wq_pg_sz,       rq->wq_ctrl.buf.page_shift -
676                                                 MLX5_ADAPTER_PAGE_SHIFT);
677         MLX5_SET64(wq, wq,  dbr_addr,           rq->wq_ctrl.db.dma);
678
679         mlx5_fill_page_frag_array(&rq->wq_ctrl.buf,
680                                   (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
681
682         err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
683
684         kvfree(in);
685
686         return err;
687 }
688
689 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
690                                  int next_state)
691 {
692         struct mlx5_core_dev *mdev = rq->mdev;
693
694         void *in;
695         void *rqc;
696         int inlen;
697         int err;
698
699         inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
700         in = kvzalloc(inlen, GFP_KERNEL);
701         if (!in)
702                 return -ENOMEM;
703
704         rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
705
706         MLX5_SET(modify_rq_in, in, rq_state, curr_state);
707         MLX5_SET(rqc, rqc, state, next_state);
708
709         err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
710
711         kvfree(in);
712
713         return err;
714 }
715
716 static int mlx5e_modify_rq_scatter_fcs(struct mlx5e_rq *rq, bool enable)
717 {
718         struct mlx5e_channel *c = rq->channel;
719         struct mlx5e_priv *priv = c->priv;
720         struct mlx5_core_dev *mdev = priv->mdev;
721
722         void *in;
723         void *rqc;
724         int inlen;
725         int err;
726
727         inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
728         in = kvzalloc(inlen, GFP_KERNEL);
729         if (!in)
730                 return -ENOMEM;
731
732         rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
733
734         MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
735         MLX5_SET64(modify_rq_in, in, modify_bitmask,
736                    MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_SCATTER_FCS);
737         MLX5_SET(rqc, rqc, scatter_fcs, enable);
738         MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
739
740         err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
741
742         kvfree(in);
743
744         return err;
745 }
746
747 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
748 {
749         struct mlx5e_channel *c = rq->channel;
750         struct mlx5_core_dev *mdev = c->mdev;
751         void *in;
752         void *rqc;
753         int inlen;
754         int err;
755
756         inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
757         in = kvzalloc(inlen, GFP_KERNEL);
758         if (!in)
759                 return -ENOMEM;
760
761         rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
762
763         MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
764         MLX5_SET64(modify_rq_in, in, modify_bitmask,
765                    MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
766         MLX5_SET(rqc, rqc, vsd, vsd);
767         MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
768
769         err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
770
771         kvfree(in);
772
773         return err;
774 }
775
776 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
777 {
778         mlx5_core_destroy_rq(rq->mdev, rq->rqn);
779 }
780
781 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq, int wait_time)
782 {
783         unsigned long exp_time = jiffies + msecs_to_jiffies(wait_time);
784         struct mlx5e_channel *c = rq->channel;
785
786         u16 min_wqes = mlx5_min_rx_wqes(rq->wq_type, mlx5e_rqwq_get_size(rq));
787
788         do {
789                 if (mlx5e_rqwq_get_cur_sz(rq) >= min_wqes)
790                         return 0;
791
792                 msleep(20);
793         } while (time_before(jiffies, exp_time));
794
795         netdev_warn(c->netdev, "Failed to get min RX wqes on Channel[%d] RQN[0x%x] wq cur_sz(%d) min_rx_wqes(%d)\n",
796                     c->ix, rq->rqn, mlx5e_rqwq_get_cur_sz(rq), min_wqes);
797
798         return -ETIMEDOUT;
799 }
800
801 static void mlx5e_free_rx_descs(struct mlx5e_rq *rq)
802 {
803         __be16 wqe_ix_be;
804         u16 wqe_ix;
805
806         if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
807                 struct mlx5_wq_ll *wq = &rq->mpwqe.wq;
808                 u16 head = wq->head;
809                 int i;
810
811                 /* Outstanding UMR WQEs (in progress) start at wq->head */
812                 for (i = 0; i < rq->mpwqe.umr_in_progress; i++) {
813                         rq->dealloc_wqe(rq, head);
814                         head = mlx5_wq_ll_get_wqe_next_ix(wq, head);
815                 }
816
817                 while (!mlx5_wq_ll_is_empty(wq)) {
818                         struct mlx5e_rx_wqe_ll *wqe;
819
820                         wqe_ix_be = *wq->tail_next;
821                         wqe_ix    = be16_to_cpu(wqe_ix_be);
822                         wqe       = mlx5_wq_ll_get_wqe(wq, wqe_ix);
823                         rq->dealloc_wqe(rq, wqe_ix);
824                         mlx5_wq_ll_pop(wq, wqe_ix_be,
825                                        &wqe->next.next_wqe_index);
826                 }
827         } else {
828                 struct mlx5_wq_cyc *wq = &rq->wqe.wq;
829
830                 while (!mlx5_wq_cyc_is_empty(wq)) {
831                         wqe_ix = mlx5_wq_cyc_get_tail(wq);
832                         rq->dealloc_wqe(rq, wqe_ix);
833                         mlx5_wq_cyc_pop(wq);
834                 }
835         }
836
837 }
838
839 static int mlx5e_open_rq(struct mlx5e_channel *c,
840                          struct mlx5e_params *params,
841                          struct mlx5e_rq_param *param,
842                          struct mlx5e_rq *rq)
843 {
844         int err;
845
846         err = mlx5e_alloc_rq(c, params, param, rq);
847         if (err)
848                 return err;
849
850         err = mlx5e_create_rq(rq, param);
851         if (err)
852                 goto err_free_rq;
853
854         err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
855         if (err)
856                 goto err_destroy_rq;
857
858         if (params->rx_dim_enabled)
859                 __set_bit(MLX5E_RQ_STATE_AM, &c->rq.state);
860
861         /* We disable csum_complete when XDP is enabled since
862          * XDP programs might manipulate packets which will render
863          * skb->checksum incorrect.
864          */
865         if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_NO_CSUM_COMPLETE) || c->xdp)
866                 __set_bit(MLX5E_RQ_STATE_NO_CSUM_COMPLETE, &c->rq.state);
867
868         return 0;
869
870 err_destroy_rq:
871         mlx5e_destroy_rq(rq);
872 err_free_rq:
873         mlx5e_free_rq(rq);
874
875         return err;
876 }
877
878 static void mlx5e_activate_rq(struct mlx5e_rq *rq)
879 {
880         set_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
881         mlx5e_trigger_irq(&rq->channel->icosq);
882 }
883
884 static void mlx5e_deactivate_rq(struct mlx5e_rq *rq)
885 {
886         clear_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
887         napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
888 }
889
890 static void mlx5e_close_rq(struct mlx5e_rq *rq)
891 {
892         cancel_work_sync(&rq->dim.work);
893         mlx5e_destroy_rq(rq);
894         mlx5e_free_rx_descs(rq);
895         mlx5e_free_rq(rq);
896 }
897
898 static void mlx5e_free_xdpsq_db(struct mlx5e_xdpsq *sq)
899 {
900         kvfree(sq->db.xdpi_fifo.xi);
901         kvfree(sq->db.wqe_info);
902 }
903
904 static int mlx5e_alloc_xdpsq_fifo(struct mlx5e_xdpsq *sq, int numa)
905 {
906         struct mlx5e_xdp_info_fifo *xdpi_fifo = &sq->db.xdpi_fifo;
907         int wq_sz        = mlx5_wq_cyc_get_size(&sq->wq);
908         int dsegs_per_wq = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
909
910         xdpi_fifo->xi = kvzalloc_node(sizeof(*xdpi_fifo->xi) * dsegs_per_wq,
911                                       GFP_KERNEL, numa);
912         if (!xdpi_fifo->xi)
913                 return -ENOMEM;
914
915         xdpi_fifo->pc   = &sq->xdpi_fifo_pc;
916         xdpi_fifo->cc   = &sq->xdpi_fifo_cc;
917         xdpi_fifo->mask = dsegs_per_wq - 1;
918
919         return 0;
920 }
921
922 static int mlx5e_alloc_xdpsq_db(struct mlx5e_xdpsq *sq, int numa)
923 {
924         int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
925         int err;
926
927         sq->db.wqe_info = kvzalloc_node(sizeof(*sq->db.wqe_info) * wq_sz,
928                                         GFP_KERNEL, numa);
929         if (!sq->db.wqe_info)
930                 return -ENOMEM;
931
932         err = mlx5e_alloc_xdpsq_fifo(sq, numa);
933         if (err) {
934                 mlx5e_free_xdpsq_db(sq);
935                 return err;
936         }
937
938         return 0;
939 }
940
941 static int mlx5e_alloc_xdpsq(struct mlx5e_channel *c,
942                              struct mlx5e_params *params,
943                              struct mlx5e_sq_param *param,
944                              struct mlx5e_xdpsq *sq,
945                              bool is_redirect)
946 {
947         void *sqc_wq               = MLX5_ADDR_OF(sqc, param->sqc, wq);
948         struct mlx5_core_dev *mdev = c->mdev;
949         struct mlx5_wq_cyc *wq = &sq->wq;
950         int err;
951
952         sq->pdev      = c->pdev;
953         sq->mkey_be   = c->mkey_be;
954         sq->channel   = c;
955         sq->uar_map   = mdev->mlx5e_res.bfreg.map;
956         sq->min_inline_mode = params->tx_min_inline_mode;
957         sq->hw_mtu    = MLX5E_SW2HW_MTU(params, params->sw_mtu);
958         sq->stats     = is_redirect ?
959                 &c->priv->channel_stats[c->ix].xdpsq :
960                 &c->priv->channel_stats[c->ix].rq_xdpsq;
961
962         param->wq.db_numa_node = cpu_to_node(c->cpu);
963         err = mlx5_wq_cyc_create(mdev, &param->wq, sqc_wq, wq, &sq->wq_ctrl);
964         if (err)
965                 return err;
966         wq->db = &wq->db[MLX5_SND_DBR];
967
968         err = mlx5e_alloc_xdpsq_db(sq, cpu_to_node(c->cpu));
969         if (err)
970                 goto err_sq_wq_destroy;
971
972         return 0;
973
974 err_sq_wq_destroy:
975         mlx5_wq_destroy(&sq->wq_ctrl);
976
977         return err;
978 }
979
980 static void mlx5e_free_xdpsq(struct mlx5e_xdpsq *sq)
981 {
982         mlx5e_free_xdpsq_db(sq);
983         mlx5_wq_destroy(&sq->wq_ctrl);
984 }
985
986 static void mlx5e_free_icosq_db(struct mlx5e_icosq *sq)
987 {
988         kvfree(sq->db.ico_wqe);
989 }
990
991 static int mlx5e_alloc_icosq_db(struct mlx5e_icosq *sq, int numa)
992 {
993         int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
994
995         sq->db.ico_wqe = kvzalloc_node(array_size(wq_sz,
996                                                   sizeof(*sq->db.ico_wqe)),
997                                        GFP_KERNEL, numa);
998         if (!sq->db.ico_wqe)
999                 return -ENOMEM;
1000
1001         return 0;
1002 }
1003
1004 static int mlx5e_alloc_icosq(struct mlx5e_channel *c,
1005                              struct mlx5e_sq_param *param,
1006                              struct mlx5e_icosq *sq)
1007 {
1008         void *sqc_wq               = MLX5_ADDR_OF(sqc, param->sqc, wq);
1009         struct mlx5_core_dev *mdev = c->mdev;
1010         struct mlx5_wq_cyc *wq = &sq->wq;
1011         int err;
1012
1013         sq->channel   = c;
1014         sq->uar_map   = mdev->mlx5e_res.bfreg.map;
1015
1016         param->wq.db_numa_node = cpu_to_node(c->cpu);
1017         err = mlx5_wq_cyc_create(mdev, &param->wq, sqc_wq, wq, &sq->wq_ctrl);
1018         if (err)
1019                 return err;
1020         wq->db = &wq->db[MLX5_SND_DBR];
1021
1022         err = mlx5e_alloc_icosq_db(sq, cpu_to_node(c->cpu));
1023         if (err)
1024                 goto err_sq_wq_destroy;
1025
1026         return 0;
1027
1028 err_sq_wq_destroy:
1029         mlx5_wq_destroy(&sq->wq_ctrl);
1030
1031         return err;
1032 }
1033
1034 static void mlx5e_free_icosq(struct mlx5e_icosq *sq)
1035 {
1036         mlx5e_free_icosq_db(sq);
1037         mlx5_wq_destroy(&sq->wq_ctrl);
1038 }
1039
1040 static void mlx5e_free_txqsq_db(struct mlx5e_txqsq *sq)
1041 {
1042         kvfree(sq->db.wqe_info);
1043         kvfree(sq->db.dma_fifo);
1044 }
1045
1046 static int mlx5e_alloc_txqsq_db(struct mlx5e_txqsq *sq, int numa)
1047 {
1048         int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1049         int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
1050
1051         sq->db.dma_fifo = kvzalloc_node(array_size(df_sz,
1052                                                    sizeof(*sq->db.dma_fifo)),
1053                                         GFP_KERNEL, numa);
1054         sq->db.wqe_info = kvzalloc_node(array_size(wq_sz,
1055                                                    sizeof(*sq->db.wqe_info)),
1056                                         GFP_KERNEL, numa);
1057         if (!sq->db.dma_fifo || !sq->db.wqe_info) {
1058                 mlx5e_free_txqsq_db(sq);
1059                 return -ENOMEM;
1060         }
1061
1062         sq->dma_fifo_mask = df_sz - 1;
1063
1064         return 0;
1065 }
1066
1067 static void mlx5e_tx_err_cqe_work(struct work_struct *recover_work);
1068 static int mlx5e_alloc_txqsq(struct mlx5e_channel *c,
1069                              int txq_ix,
1070                              struct mlx5e_params *params,
1071                              struct mlx5e_sq_param *param,
1072                              struct mlx5e_txqsq *sq,
1073                              int tc)
1074 {
1075         void *sqc_wq               = MLX5_ADDR_OF(sqc, param->sqc, wq);
1076         struct mlx5_core_dev *mdev = c->mdev;
1077         struct mlx5_wq_cyc *wq = &sq->wq;
1078         int err;
1079
1080         sq->pdev      = c->pdev;
1081         sq->tstamp    = c->tstamp;
1082         sq->clock     = &mdev->clock;
1083         sq->mkey_be   = c->mkey_be;
1084         sq->channel   = c;
1085         sq->txq_ix    = txq_ix;
1086         sq->uar_map   = mdev->mlx5e_res.bfreg.map;
1087         sq->min_inline_mode = params->tx_min_inline_mode;
1088         sq->stats     = &c->priv->channel_stats[c->ix].sq[tc];
1089         INIT_WORK(&sq->recover_work, mlx5e_tx_err_cqe_work);
1090         if (MLX5_IPSEC_DEV(c->priv->mdev))
1091                 set_bit(MLX5E_SQ_STATE_IPSEC, &sq->state);
1092         if (mlx5_accel_is_tls_device(c->priv->mdev))
1093                 set_bit(MLX5E_SQ_STATE_TLS, &sq->state);
1094
1095         param->wq.db_numa_node = cpu_to_node(c->cpu);
1096         err = mlx5_wq_cyc_create(mdev, &param->wq, sqc_wq, wq, &sq->wq_ctrl);
1097         if (err)
1098                 return err;
1099         wq->db    = &wq->db[MLX5_SND_DBR];
1100
1101         err = mlx5e_alloc_txqsq_db(sq, cpu_to_node(c->cpu));
1102         if (err)
1103                 goto err_sq_wq_destroy;
1104
1105         INIT_WORK(&sq->dim.work, mlx5e_tx_dim_work);
1106         sq->dim.mode = params->tx_cq_moderation.cq_period_mode;
1107
1108         return 0;
1109
1110 err_sq_wq_destroy:
1111         mlx5_wq_destroy(&sq->wq_ctrl);
1112
1113         return err;
1114 }
1115
1116 static void mlx5e_free_txqsq(struct mlx5e_txqsq *sq)
1117 {
1118         mlx5e_free_txqsq_db(sq);
1119         mlx5_wq_destroy(&sq->wq_ctrl);
1120 }
1121
1122 struct mlx5e_create_sq_param {
1123         struct mlx5_wq_ctrl        *wq_ctrl;
1124         u32                         cqn;
1125         u32                         tisn;
1126         u8                          tis_lst_sz;
1127         u8                          min_inline_mode;
1128 };
1129
1130 static int mlx5e_create_sq(struct mlx5_core_dev *mdev,
1131                            struct mlx5e_sq_param *param,
1132                            struct mlx5e_create_sq_param *csp,
1133                            u32 *sqn)
1134 {
1135         void *in;
1136         void *sqc;
1137         void *wq;
1138         int inlen;
1139         int err;
1140
1141         inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1142                 sizeof(u64) * csp->wq_ctrl->buf.npages;
1143         in = kvzalloc(inlen, GFP_KERNEL);
1144         if (!in)
1145                 return -ENOMEM;
1146
1147         sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1148         wq = MLX5_ADDR_OF(sqc, sqc, wq);
1149
1150         memcpy(sqc, param->sqc, sizeof(param->sqc));
1151         MLX5_SET(sqc,  sqc, tis_lst_sz, csp->tis_lst_sz);
1152         MLX5_SET(sqc,  sqc, tis_num_0, csp->tisn);
1153         MLX5_SET(sqc,  sqc, cqn, csp->cqn);
1154
1155         if (MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5_CAP_INLINE_MODE_VPORT_CONTEXT)
1156                 MLX5_SET(sqc,  sqc, min_wqe_inline_mode, csp->min_inline_mode);
1157
1158         MLX5_SET(sqc,  sqc, state, MLX5_SQC_STATE_RST);
1159         MLX5_SET(sqc,  sqc, flush_in_error_en, 1);
1160
1161         MLX5_SET(wq,   wq, wq_type,       MLX5_WQ_TYPE_CYCLIC);
1162         MLX5_SET(wq,   wq, uar_page,      mdev->mlx5e_res.bfreg.index);
1163         MLX5_SET(wq,   wq, log_wq_pg_sz,  csp->wq_ctrl->buf.page_shift -
1164                                           MLX5_ADAPTER_PAGE_SHIFT);
1165         MLX5_SET64(wq, wq, dbr_addr,      csp->wq_ctrl->db.dma);
1166
1167         mlx5_fill_page_frag_array(&csp->wq_ctrl->buf,
1168                                   (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
1169
1170         err = mlx5_core_create_sq(mdev, in, inlen, sqn);
1171
1172         kvfree(in);
1173
1174         return err;
1175 }
1176
1177 int mlx5e_modify_sq(struct mlx5_core_dev *mdev, u32 sqn,
1178                     struct mlx5e_modify_sq_param *p)
1179 {
1180         void *in;
1181         void *sqc;
1182         int inlen;
1183         int err;
1184
1185         inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1186         in = kvzalloc(inlen, GFP_KERNEL);
1187         if (!in)
1188                 return -ENOMEM;
1189
1190         sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1191
1192         MLX5_SET(modify_sq_in, in, sq_state, p->curr_state);
1193         MLX5_SET(sqc, sqc, state, p->next_state);
1194         if (p->rl_update && p->next_state == MLX5_SQC_STATE_RDY) {
1195                 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
1196                 MLX5_SET(sqc,  sqc, packet_pacing_rate_limit_index, p->rl_index);
1197         }
1198
1199         err = mlx5_core_modify_sq(mdev, sqn, in, inlen);
1200
1201         kvfree(in);
1202
1203         return err;
1204 }
1205
1206 static void mlx5e_destroy_sq(struct mlx5_core_dev *mdev, u32 sqn)
1207 {
1208         mlx5_core_destroy_sq(mdev, sqn);
1209 }
1210
1211 static int mlx5e_create_sq_rdy(struct mlx5_core_dev *mdev,
1212                                struct mlx5e_sq_param *param,
1213                                struct mlx5e_create_sq_param *csp,
1214                                u32 *sqn)
1215 {
1216         struct mlx5e_modify_sq_param msp = {0};
1217         int err;
1218
1219         err = mlx5e_create_sq(mdev, param, csp, sqn);
1220         if (err)
1221                 return err;
1222
1223         msp.curr_state = MLX5_SQC_STATE_RST;
1224         msp.next_state = MLX5_SQC_STATE_RDY;
1225         err = mlx5e_modify_sq(mdev, *sqn, &msp);
1226         if (err)
1227                 mlx5e_destroy_sq(mdev, *sqn);
1228
1229         return err;
1230 }
1231
1232 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1233                                 struct mlx5e_txqsq *sq, u32 rate);
1234
1235 static int mlx5e_open_txqsq(struct mlx5e_channel *c,
1236                             u32 tisn,
1237                             int txq_ix,
1238                             struct mlx5e_params *params,
1239                             struct mlx5e_sq_param *param,
1240                             struct mlx5e_txqsq *sq,
1241                             int tc)
1242 {
1243         struct mlx5e_create_sq_param csp = {};
1244         u32 tx_rate;
1245         int err;
1246
1247         err = mlx5e_alloc_txqsq(c, txq_ix, params, param, sq, tc);
1248         if (err)
1249                 return err;
1250
1251         csp.tisn            = tisn;
1252         csp.tis_lst_sz      = 1;
1253         csp.cqn             = sq->cq.mcq.cqn;
1254         csp.wq_ctrl         = &sq->wq_ctrl;
1255         csp.min_inline_mode = sq->min_inline_mode;
1256         err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1257         if (err)
1258                 goto err_free_txqsq;
1259
1260         tx_rate = c->priv->tx_rates[sq->txq_ix];
1261         if (tx_rate)
1262                 mlx5e_set_sq_maxrate(c->netdev, sq, tx_rate);
1263
1264         if (params->tx_dim_enabled)
1265                 sq->state |= BIT(MLX5E_SQ_STATE_AM);
1266
1267         return 0;
1268
1269 err_free_txqsq:
1270         clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1271         mlx5e_free_txqsq(sq);
1272
1273         return err;
1274 }
1275
1276 void mlx5e_activate_txqsq(struct mlx5e_txqsq *sq)
1277 {
1278         sq->txq = netdev_get_tx_queue(sq->channel->netdev, sq->txq_ix);
1279         clear_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state);
1280         set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1281         netdev_tx_reset_queue(sq->txq);
1282         netif_tx_start_queue(sq->txq);
1283 }
1284
1285 void mlx5e_tx_disable_queue(struct netdev_queue *txq)
1286 {
1287         __netif_tx_lock_bh(txq);
1288         netif_tx_stop_queue(txq);
1289         __netif_tx_unlock_bh(txq);
1290 }
1291
1292 static void mlx5e_deactivate_txqsq(struct mlx5e_txqsq *sq)
1293 {
1294         struct mlx5e_channel *c = sq->channel;
1295         struct mlx5_wq_cyc *wq = &sq->wq;
1296
1297         clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1298         /* prevent netif_tx_wake_queue */
1299         napi_synchronize(&c->napi);
1300
1301         mlx5e_tx_disable_queue(sq->txq);
1302
1303         /* last doorbell out, godspeed .. */
1304         if (mlx5e_wqc_has_room_for(wq, sq->cc, sq->pc, 1)) {
1305                 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
1306                 struct mlx5e_tx_wqe *nop;
1307
1308                 sq->db.wqe_info[pi].skb = NULL;
1309                 nop = mlx5e_post_nop(wq, sq->sqn, &sq->pc);
1310                 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &nop->ctrl);
1311         }
1312 }
1313
1314 static void mlx5e_close_txqsq(struct mlx5e_txqsq *sq)
1315 {
1316         struct mlx5e_channel *c = sq->channel;
1317         struct mlx5_core_dev *mdev = c->mdev;
1318         struct mlx5_rate_limit rl = {0};
1319
1320         cancel_work_sync(&sq->dim.work);
1321         cancel_work_sync(&sq->recover_work);
1322         mlx5e_destroy_sq(mdev, sq->sqn);
1323         if (sq->rate_limit) {
1324                 rl.rate = sq->rate_limit;
1325                 mlx5_rl_remove_rate(mdev, &rl);
1326         }
1327         mlx5e_free_txqsq_descs(sq);
1328         mlx5e_free_txqsq(sq);
1329 }
1330
1331 static void mlx5e_tx_err_cqe_work(struct work_struct *recover_work)
1332 {
1333         struct mlx5e_txqsq *sq = container_of(recover_work, struct mlx5e_txqsq,
1334                                               recover_work);
1335
1336         mlx5e_tx_reporter_err_cqe(sq);
1337 }
1338
1339 static int mlx5e_open_icosq(struct mlx5e_channel *c,
1340                             struct mlx5e_params *params,
1341                             struct mlx5e_sq_param *param,
1342                             struct mlx5e_icosq *sq)
1343 {
1344         struct mlx5e_create_sq_param csp = {};
1345         int err;
1346
1347         err = mlx5e_alloc_icosq(c, param, sq);
1348         if (err)
1349                 return err;
1350
1351         csp.cqn             = sq->cq.mcq.cqn;
1352         csp.wq_ctrl         = &sq->wq_ctrl;
1353         csp.min_inline_mode = params->tx_min_inline_mode;
1354         set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1355         err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1356         if (err)
1357                 goto err_free_icosq;
1358
1359         return 0;
1360
1361 err_free_icosq:
1362         clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1363         mlx5e_free_icosq(sq);
1364
1365         return err;
1366 }
1367
1368 static void mlx5e_close_icosq(struct mlx5e_icosq *sq)
1369 {
1370         struct mlx5e_channel *c = sq->channel;
1371
1372         clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1373         napi_synchronize(&c->napi);
1374
1375         mlx5e_destroy_sq(c->mdev, sq->sqn);
1376         mlx5e_free_icosq(sq);
1377 }
1378
1379 static int mlx5e_open_xdpsq(struct mlx5e_channel *c,
1380                             struct mlx5e_params *params,
1381                             struct mlx5e_sq_param *param,
1382                             struct mlx5e_xdpsq *sq,
1383                             bool is_redirect)
1384 {
1385         struct mlx5e_create_sq_param csp = {};
1386         int err;
1387
1388         err = mlx5e_alloc_xdpsq(c, params, param, sq, is_redirect);
1389         if (err)
1390                 return err;
1391
1392         csp.tis_lst_sz      = 1;
1393         csp.tisn            = c->priv->tisn[0]; /* tc = 0 */
1394         csp.cqn             = sq->cq.mcq.cqn;
1395         csp.wq_ctrl         = &sq->wq_ctrl;
1396         csp.min_inline_mode = sq->min_inline_mode;
1397         set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1398         err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1399         if (err)
1400                 goto err_free_xdpsq;
1401
1402         mlx5e_set_xmit_fp(sq, param->is_mpw);
1403
1404         if (!param->is_mpw) {
1405                 unsigned int ds_cnt = MLX5E_XDP_TX_DS_COUNT;
1406                 unsigned int inline_hdr_sz = 0;
1407                 int i;
1408
1409                 if (sq->min_inline_mode != MLX5_INLINE_MODE_NONE) {
1410                         inline_hdr_sz = MLX5E_XDP_MIN_INLINE;
1411                         ds_cnt++;
1412                 }
1413
1414                 /* Pre initialize fixed WQE fields */
1415                 for (i = 0; i < mlx5_wq_cyc_get_size(&sq->wq); i++) {
1416                         struct mlx5e_xdp_wqe_info *wi  = &sq->db.wqe_info[i];
1417                         struct mlx5e_tx_wqe      *wqe  = mlx5_wq_cyc_get_wqe(&sq->wq, i);
1418                         struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
1419                         struct mlx5_wqe_eth_seg  *eseg = &wqe->eth;
1420                         struct mlx5_wqe_data_seg *dseg;
1421
1422                         cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
1423                         eseg->inline_hdr.sz = cpu_to_be16(inline_hdr_sz);
1424
1425                         dseg = (struct mlx5_wqe_data_seg *)cseg + (ds_cnt - 1);
1426                         dseg->lkey = sq->mkey_be;
1427
1428                         wi->num_wqebbs = 1;
1429                         wi->num_pkts   = 1;
1430                 }
1431         }
1432
1433         return 0;
1434
1435 err_free_xdpsq:
1436         clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1437         mlx5e_free_xdpsq(sq);
1438
1439         return err;
1440 }
1441
1442 static void mlx5e_close_xdpsq(struct mlx5e_xdpsq *sq, struct mlx5e_rq *rq)
1443 {
1444         struct mlx5e_channel *c = sq->channel;
1445
1446         clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1447         napi_synchronize(&c->napi);
1448
1449         mlx5e_destroy_sq(c->mdev, sq->sqn);
1450         mlx5e_free_xdpsq_descs(sq, rq);
1451         mlx5e_free_xdpsq(sq);
1452 }
1453
1454 static int mlx5e_alloc_cq_common(struct mlx5_core_dev *mdev,
1455                                  struct mlx5e_cq_param *param,
1456                                  struct mlx5e_cq *cq)
1457 {
1458         struct mlx5_core_cq *mcq = &cq->mcq;
1459         int eqn_not_used;
1460         unsigned int irqn;
1461         int err;
1462         u32 i;
1463
1464         err = mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1465         if (err)
1466                 return err;
1467
1468         err = mlx5_cqwq_create(mdev, &param->wq, param->cqc, &cq->wq,
1469                                &cq->wq_ctrl);
1470         if (err)
1471                 return err;
1472
1473         mcq->cqe_sz     = 64;
1474         mcq->set_ci_db  = cq->wq_ctrl.db.db;
1475         mcq->arm_db     = cq->wq_ctrl.db.db + 1;
1476         *mcq->set_ci_db = 0;
1477         *mcq->arm_db    = 0;
1478         mcq->vector     = param->eq_ix;
1479         mcq->comp       = mlx5e_completion_event;
1480         mcq->event      = mlx5e_cq_error_event;
1481         mcq->irqn       = irqn;
1482
1483         for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1484                 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1485
1486                 cqe->op_own = 0xf1;
1487         }
1488
1489         cq->mdev = mdev;
1490
1491         return 0;
1492 }
1493
1494 static int mlx5e_alloc_cq(struct mlx5e_channel *c,
1495                           struct mlx5e_cq_param *param,
1496                           struct mlx5e_cq *cq)
1497 {
1498         struct mlx5_core_dev *mdev = c->priv->mdev;
1499         int err;
1500
1501         param->wq.buf_numa_node = cpu_to_node(c->cpu);
1502         param->wq.db_numa_node  = cpu_to_node(c->cpu);
1503         param->eq_ix   = c->ix;
1504
1505         err = mlx5e_alloc_cq_common(mdev, param, cq);
1506
1507         cq->napi    = &c->napi;
1508         cq->channel = c;
1509
1510         return err;
1511 }
1512
1513 static void mlx5e_free_cq(struct mlx5e_cq *cq)
1514 {
1515         mlx5_wq_destroy(&cq->wq_ctrl);
1516 }
1517
1518 static int mlx5e_create_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
1519 {
1520         struct mlx5_core_dev *mdev = cq->mdev;
1521         struct mlx5_core_cq *mcq = &cq->mcq;
1522
1523         void *in;
1524         void *cqc;
1525         int inlen;
1526         unsigned int irqn_not_used;
1527         int eqn;
1528         int err;
1529
1530         err = mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
1531         if (err)
1532                 return err;
1533
1534         inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1535                 sizeof(u64) * cq->wq_ctrl.buf.npages;
1536         in = kvzalloc(inlen, GFP_KERNEL);
1537         if (!in)
1538                 return -ENOMEM;
1539
1540         cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1541
1542         memcpy(cqc, param->cqc, sizeof(param->cqc));
1543
1544         mlx5_fill_page_frag_array(&cq->wq_ctrl.buf,
1545                                   (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
1546
1547         MLX5_SET(cqc,   cqc, cq_period_mode, param->cq_period_mode);
1548         MLX5_SET(cqc,   cqc, c_eqn,         eqn);
1549         MLX5_SET(cqc,   cqc, uar_page,      mdev->priv.uar->index);
1550         MLX5_SET(cqc,   cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
1551                                             MLX5_ADAPTER_PAGE_SHIFT);
1552         MLX5_SET64(cqc, cqc, dbr_addr,      cq->wq_ctrl.db.dma);
1553
1554         err = mlx5_core_create_cq(mdev, mcq, in, inlen);
1555
1556         kvfree(in);
1557
1558         if (err)
1559                 return err;
1560
1561         mlx5e_cq_arm(cq);
1562
1563         return 0;
1564 }
1565
1566 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
1567 {
1568         mlx5_core_destroy_cq(cq->mdev, &cq->mcq);
1569 }
1570
1571 static int mlx5e_open_cq(struct mlx5e_channel *c,
1572                          struct net_dim_cq_moder moder,
1573                          struct mlx5e_cq_param *param,
1574                          struct mlx5e_cq *cq)
1575 {
1576         struct mlx5_core_dev *mdev = c->mdev;
1577         int err;
1578
1579         err = mlx5e_alloc_cq(c, param, cq);
1580         if (err)
1581                 return err;
1582
1583         err = mlx5e_create_cq(cq, param);
1584         if (err)
1585                 goto err_free_cq;
1586
1587         if (MLX5_CAP_GEN(mdev, cq_moderation))
1588                 mlx5_core_modify_cq_moderation(mdev, &cq->mcq, moder.usec, moder.pkts);
1589         return 0;
1590
1591 err_free_cq:
1592         mlx5e_free_cq(cq);
1593
1594         return err;
1595 }
1596
1597 static void mlx5e_close_cq(struct mlx5e_cq *cq)
1598 {
1599         mlx5e_destroy_cq(cq);
1600         mlx5e_free_cq(cq);
1601 }
1602
1603 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1604                              struct mlx5e_params *params,
1605                              struct mlx5e_channel_param *cparam)
1606 {
1607         int err;
1608         int tc;
1609
1610         for (tc = 0; tc < c->num_tc; tc++) {
1611                 err = mlx5e_open_cq(c, params->tx_cq_moderation,
1612                                     &cparam->tx_cq, &c->sq[tc].cq);
1613                 if (err)
1614                         goto err_close_tx_cqs;
1615         }
1616
1617         return 0;
1618
1619 err_close_tx_cqs:
1620         for (tc--; tc >= 0; tc--)
1621                 mlx5e_close_cq(&c->sq[tc].cq);
1622
1623         return err;
1624 }
1625
1626 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1627 {
1628         int tc;
1629
1630         for (tc = 0; tc < c->num_tc; tc++)
1631                 mlx5e_close_cq(&c->sq[tc].cq);
1632 }
1633
1634 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1635                           struct mlx5e_params *params,
1636                           struct mlx5e_channel_param *cparam)
1637 {
1638         struct mlx5e_priv *priv = c->priv;
1639         int err, tc, max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
1640
1641         for (tc = 0; tc < params->num_tc; tc++) {
1642                 int txq_ix = c->ix + tc * max_nch;
1643
1644                 err = mlx5e_open_txqsq(c, c->priv->tisn[tc], txq_ix,
1645                                        params, &cparam->sq, &c->sq[tc], tc);
1646                 if (err)
1647                         goto err_close_sqs;
1648         }
1649
1650         return 0;
1651
1652 err_close_sqs:
1653         for (tc--; tc >= 0; tc--)
1654                 mlx5e_close_txqsq(&c->sq[tc]);
1655
1656         return err;
1657 }
1658
1659 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1660 {
1661         int tc;
1662
1663         for (tc = 0; tc < c->num_tc; tc++)
1664                 mlx5e_close_txqsq(&c->sq[tc]);
1665 }
1666
1667 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1668                                 struct mlx5e_txqsq *sq, u32 rate)
1669 {
1670         struct mlx5e_priv *priv = netdev_priv(dev);
1671         struct mlx5_core_dev *mdev = priv->mdev;
1672         struct mlx5e_modify_sq_param msp = {0};
1673         struct mlx5_rate_limit rl = {0};
1674         u16 rl_index = 0;
1675         int err;
1676
1677         if (rate == sq->rate_limit)
1678                 /* nothing to do */
1679                 return 0;
1680
1681         if (sq->rate_limit) {
1682                 rl.rate = sq->rate_limit;
1683                 /* remove current rl index to free space to next ones */
1684                 mlx5_rl_remove_rate(mdev, &rl);
1685         }
1686
1687         sq->rate_limit = 0;
1688
1689         if (rate) {
1690                 rl.rate = rate;
1691                 err = mlx5_rl_add_rate(mdev, &rl_index, &rl);
1692                 if (err) {
1693                         netdev_err(dev, "Failed configuring rate %u: %d\n",
1694                                    rate, err);
1695                         return err;
1696                 }
1697         }
1698
1699         msp.curr_state = MLX5_SQC_STATE_RDY;
1700         msp.next_state = MLX5_SQC_STATE_RDY;
1701         msp.rl_index   = rl_index;
1702         msp.rl_update  = true;
1703         err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1704         if (err) {
1705                 netdev_err(dev, "Failed configuring rate %u: %d\n",
1706                            rate, err);
1707                 /* remove the rate from the table */
1708                 if (rate)
1709                         mlx5_rl_remove_rate(mdev, &rl);
1710                 return err;
1711         }
1712
1713         sq->rate_limit = rate;
1714         return 0;
1715 }
1716
1717 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1718 {
1719         struct mlx5e_priv *priv = netdev_priv(dev);
1720         struct mlx5_core_dev *mdev = priv->mdev;
1721         struct mlx5e_txqsq *sq = priv->txq2sq[index];
1722         int err = 0;
1723
1724         if (!mlx5_rl_is_supported(mdev)) {
1725                 netdev_err(dev, "Rate limiting is not supported on this device\n");
1726                 return -EINVAL;
1727         }
1728
1729         /* rate is given in Mb/sec, HW config is in Kb/sec */
1730         rate = rate << 10;
1731
1732         /* Check whether rate in valid range, 0 is always valid */
1733         if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1734                 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1735                 return -ERANGE;
1736         }
1737
1738         mutex_lock(&priv->state_lock);
1739         if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1740                 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1741         if (!err)
1742                 priv->tx_rates[index] = rate;
1743         mutex_unlock(&priv->state_lock);
1744
1745         return err;
1746 }
1747
1748 static int mlx5e_alloc_xps_cpumask(struct mlx5e_channel *c,
1749                                    struct mlx5e_params *params)
1750 {
1751         int num_comp_vectors = mlx5_comp_vectors_count(c->mdev);
1752         int irq;
1753
1754         if (!zalloc_cpumask_var(&c->xps_cpumask, GFP_KERNEL))
1755                 return -ENOMEM;
1756
1757         for (irq = c->ix; irq < num_comp_vectors; irq += params->num_channels) {
1758                 int cpu = cpumask_first(mlx5_comp_irq_get_affinity_mask(c->mdev, irq));
1759
1760                 cpumask_set_cpu(cpu, c->xps_cpumask);
1761         }
1762
1763         return 0;
1764 }
1765
1766 static void mlx5e_free_xps_cpumask(struct mlx5e_channel *c)
1767 {
1768         free_cpumask_var(c->xps_cpumask);
1769 }
1770
1771 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1772                               struct mlx5e_params *params,
1773                               struct mlx5e_channel_param *cparam,
1774                               struct mlx5e_channel **cp)
1775 {
1776         int cpu = cpumask_first(mlx5_comp_irq_get_affinity_mask(priv->mdev, ix));
1777         struct net_dim_cq_moder icocq_moder = {0, 0};
1778         struct net_device *netdev = priv->netdev;
1779         struct mlx5e_channel *c;
1780         unsigned int irq;
1781         int err;
1782         int eqn;
1783
1784         err = mlx5_vector2eqn(priv->mdev, ix, &eqn, &irq);
1785         if (err)
1786                 return err;
1787
1788         c = kvzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1789         if (!c)
1790                 return -ENOMEM;
1791
1792         c->priv     = priv;
1793         c->mdev     = priv->mdev;
1794         c->tstamp   = &priv->tstamp;
1795         c->ix       = ix;
1796         c->cpu      = cpu;
1797         c->pdev     = priv->mdev->device;
1798         c->netdev   = priv->netdev;
1799         c->mkey_be  = cpu_to_be32(priv->mdev->mlx5e_res.mkey.key);
1800         c->num_tc   = params->num_tc;
1801         c->xdp      = !!params->xdp_prog;
1802         c->stats    = &priv->channel_stats[ix].ch;
1803         c->irq_desc = irq_to_desc(irq);
1804
1805         err = mlx5e_alloc_xps_cpumask(c, params);
1806         if (err)
1807                 goto err_free_channel;
1808
1809         netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1810
1811         err = mlx5e_open_cq(c, icocq_moder, &cparam->icosq_cq, &c->icosq.cq);
1812         if (err)
1813                 goto err_napi_del;
1814
1815         err = mlx5e_open_tx_cqs(c, params, cparam);
1816         if (err)
1817                 goto err_close_icosq_cq;
1818
1819         err = mlx5e_open_cq(c, params->tx_cq_moderation, &cparam->tx_cq, &c->xdpsq.cq);
1820         if (err)
1821                 goto err_close_tx_cqs;
1822
1823         err = mlx5e_open_cq(c, params->rx_cq_moderation, &cparam->rx_cq, &c->rq.cq);
1824         if (err)
1825                 goto err_close_xdp_tx_cqs;
1826
1827         /* XDP SQ CQ params are same as normal TXQ sq CQ params */
1828         err = c->xdp ? mlx5e_open_cq(c, params->tx_cq_moderation,
1829                                      &cparam->tx_cq, &c->rq.xdpsq.cq) : 0;
1830         if (err)
1831                 goto err_close_rx_cq;
1832
1833         napi_enable(&c->napi);
1834
1835         err = mlx5e_open_icosq(c, params, &cparam->icosq, &c->icosq);
1836         if (err)
1837                 goto err_disable_napi;
1838
1839         err = mlx5e_open_sqs(c, params, cparam);
1840         if (err)
1841                 goto err_close_icosq;
1842
1843         err = c->xdp ? mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, &c->rq.xdpsq, false) : 0;
1844         if (err)
1845                 goto err_close_sqs;
1846
1847         err = mlx5e_open_rq(c, params, &cparam->rq, &c->rq);
1848         if (err)
1849                 goto err_close_xdp_sq;
1850
1851         err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, &c->xdpsq, true);
1852         if (err)
1853                 goto err_close_rq;
1854
1855         *cp = c;
1856
1857         return 0;
1858
1859 err_close_rq:
1860         mlx5e_close_rq(&c->rq);
1861
1862 err_close_xdp_sq:
1863         if (c->xdp)
1864                 mlx5e_close_xdpsq(&c->rq.xdpsq, &c->rq);
1865
1866 err_close_sqs:
1867         mlx5e_close_sqs(c);
1868
1869 err_close_icosq:
1870         mlx5e_close_icosq(&c->icosq);
1871
1872 err_disable_napi:
1873         napi_disable(&c->napi);
1874         if (c->xdp)
1875                 mlx5e_close_cq(&c->rq.xdpsq.cq);
1876
1877 err_close_rx_cq:
1878         mlx5e_close_cq(&c->rq.cq);
1879
1880 err_close_xdp_tx_cqs:
1881         mlx5e_close_cq(&c->xdpsq.cq);
1882
1883 err_close_tx_cqs:
1884         mlx5e_close_tx_cqs(c);
1885
1886 err_close_icosq_cq:
1887         mlx5e_close_cq(&c->icosq.cq);
1888
1889 err_napi_del:
1890         netif_napi_del(&c->napi);
1891         mlx5e_free_xps_cpumask(c);
1892
1893 err_free_channel:
1894         kvfree(c);
1895
1896         return err;
1897 }
1898
1899 static void mlx5e_activate_channel(struct mlx5e_channel *c)
1900 {
1901         int tc;
1902
1903         for (tc = 0; tc < c->num_tc; tc++)
1904                 mlx5e_activate_txqsq(&c->sq[tc]);
1905         mlx5e_activate_rq(&c->rq);
1906         netif_set_xps_queue(c->netdev, c->xps_cpumask, c->ix);
1907 }
1908
1909 static void mlx5e_deactivate_channel(struct mlx5e_channel *c)
1910 {
1911         int tc;
1912
1913         mlx5e_deactivate_rq(&c->rq);
1914         for (tc = 0; tc < c->num_tc; tc++)
1915                 mlx5e_deactivate_txqsq(&c->sq[tc]);
1916 }
1917
1918 static void mlx5e_close_channel(struct mlx5e_channel *c)
1919 {
1920         mlx5e_close_xdpsq(&c->xdpsq, NULL);
1921         mlx5e_close_rq(&c->rq);
1922         if (c->xdp)
1923                 mlx5e_close_xdpsq(&c->rq.xdpsq, &c->rq);
1924         mlx5e_close_sqs(c);
1925         mlx5e_close_icosq(&c->icosq);
1926         napi_disable(&c->napi);
1927         if (c->xdp)
1928                 mlx5e_close_cq(&c->rq.xdpsq.cq);
1929         mlx5e_close_cq(&c->rq.cq);
1930         mlx5e_close_cq(&c->xdpsq.cq);
1931         mlx5e_close_tx_cqs(c);
1932         mlx5e_close_cq(&c->icosq.cq);
1933         netif_napi_del(&c->napi);
1934         mlx5e_free_xps_cpumask(c);
1935
1936         kvfree(c);
1937 }
1938
1939 #define DEFAULT_FRAG_SIZE (2048)
1940
1941 static void mlx5e_build_rq_frags_info(struct mlx5_core_dev *mdev,
1942                                       struct mlx5e_params *params,
1943                                       struct mlx5e_rq_frags_info *info)
1944 {
1945         u32 byte_count = MLX5E_SW2HW_MTU(params, params->sw_mtu);
1946         int frag_size_max = DEFAULT_FRAG_SIZE;
1947         u32 buf_size = 0;
1948         int i;
1949
1950 #ifdef CONFIG_MLX5_EN_IPSEC
1951         if (MLX5_IPSEC_DEV(mdev))
1952                 byte_count += MLX5E_METADATA_ETHER_LEN;
1953 #endif
1954
1955         if (mlx5e_rx_is_linear_skb(params)) {
1956                 int frag_stride;
1957
1958                 frag_stride = mlx5e_rx_get_linear_frag_sz(params);
1959                 frag_stride = roundup_pow_of_two(frag_stride);
1960
1961                 info->arr[0].frag_size = byte_count;
1962                 info->arr[0].frag_stride = frag_stride;
1963                 info->num_frags = 1;
1964                 info->wqe_bulk = PAGE_SIZE / frag_stride;
1965                 goto out;
1966         }
1967
1968         if (byte_count > PAGE_SIZE +
1969             (MLX5E_MAX_RX_FRAGS - 1) * frag_size_max)
1970                 frag_size_max = PAGE_SIZE;
1971
1972         i = 0;
1973         while (buf_size < byte_count) {
1974                 int frag_size = byte_count - buf_size;
1975
1976                 if (i < MLX5E_MAX_RX_FRAGS - 1)
1977                         frag_size = min(frag_size, frag_size_max);
1978
1979                 info->arr[i].frag_size = frag_size;
1980                 info->arr[i].frag_stride = roundup_pow_of_two(frag_size);
1981
1982                 buf_size += frag_size;
1983                 i++;
1984         }
1985         info->num_frags = i;
1986         /* number of different wqes sharing a page */
1987         info->wqe_bulk = 1 + (info->num_frags % 2);
1988
1989 out:
1990         info->wqe_bulk = max_t(u8, info->wqe_bulk, 8);
1991         info->log_num_frags = order_base_2(info->num_frags);
1992 }
1993
1994 static inline u8 mlx5e_get_rqwq_log_stride(u8 wq_type, int ndsegs)
1995 {
1996         int sz = sizeof(struct mlx5_wqe_data_seg) * ndsegs;
1997
1998         switch (wq_type) {
1999         case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2000                 sz += sizeof(struct mlx5e_rx_wqe_ll);
2001                 break;
2002         default: /* MLX5_WQ_TYPE_CYCLIC */
2003                 sz += sizeof(struct mlx5e_rx_wqe_cyc);
2004         }
2005
2006         return order_base_2(sz);
2007 }
2008
2009 static u8 mlx5e_get_rq_log_wq_sz(void *rqc)
2010 {
2011         void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2012
2013         return MLX5_GET(wq, wq, log_wq_sz);
2014 }
2015
2016 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
2017                                  struct mlx5e_params *params,
2018                                  struct mlx5e_rq_param *param)
2019 {
2020         struct mlx5_core_dev *mdev = priv->mdev;
2021         void *rqc = param->rqc;
2022         void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2023         int ndsegs = 1;
2024
2025         switch (params->rq_wq_type) {
2026         case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2027                 MLX5_SET(wq, wq, log_wqe_num_of_strides,
2028                          mlx5e_mpwqe_get_log_num_strides(mdev, params) -
2029                          MLX5_MPWQE_LOG_NUM_STRIDES_BASE);
2030                 MLX5_SET(wq, wq, log_wqe_stride_size,
2031                          mlx5e_mpwqe_get_log_stride_size(mdev, params) -
2032                          MLX5_MPWQE_LOG_STRIDE_SZ_BASE);
2033                 MLX5_SET(wq, wq, log_wq_sz, mlx5e_mpwqe_get_log_rq_size(params));
2034                 break;
2035         default: /* MLX5_WQ_TYPE_CYCLIC */
2036                 MLX5_SET(wq, wq, log_wq_sz, params->log_rq_mtu_frames);
2037                 mlx5e_build_rq_frags_info(mdev, params, &param->frags_info);
2038                 ndsegs = param->frags_info.num_frags;
2039         }
2040
2041         MLX5_SET(wq, wq, wq_type,          params->rq_wq_type);
2042         MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
2043         MLX5_SET(wq, wq, log_wq_stride,
2044                  mlx5e_get_rqwq_log_stride(params->rq_wq_type, ndsegs));
2045         MLX5_SET(wq, wq, pd,               mdev->mlx5e_res.pdn);
2046         MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
2047         MLX5_SET(rqc, rqc, vsd,            params->vlan_strip_disable);
2048         MLX5_SET(rqc, rqc, scatter_fcs,    params->scatter_fcs_en);
2049
2050         param->wq.buf_numa_node = dev_to_node(mdev->device);
2051 }
2052
2053 static void mlx5e_build_drop_rq_param(struct mlx5e_priv *priv,
2054                                       struct mlx5e_rq_param *param)
2055 {
2056         struct mlx5_core_dev *mdev = priv->mdev;
2057         void *rqc = param->rqc;
2058         void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2059
2060         MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
2061         MLX5_SET(wq, wq, log_wq_stride,
2062                  mlx5e_get_rqwq_log_stride(MLX5_WQ_TYPE_CYCLIC, 1));
2063         MLX5_SET(rqc, rqc, counter_set_id, priv->drop_rq_q_counter);
2064
2065         param->wq.buf_numa_node = dev_to_node(mdev->device);
2066 }
2067
2068 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
2069                                         struct mlx5e_sq_param *param)
2070 {
2071         void *sqc = param->sqc;
2072         void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2073
2074         MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
2075         MLX5_SET(wq, wq, pd,            priv->mdev->mlx5e_res.pdn);
2076
2077         param->wq.buf_numa_node = dev_to_node(priv->mdev->device);
2078 }
2079
2080 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
2081                                  struct mlx5e_params *params,
2082                                  struct mlx5e_sq_param *param)
2083 {
2084         void *sqc = param->sqc;
2085         void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2086         bool allow_swp;
2087
2088         allow_swp = mlx5_geneve_tx_allowed(priv->mdev) ||
2089                     !!MLX5_IPSEC_DEV(priv->mdev);
2090         mlx5e_build_sq_param_common(priv, param);
2091         MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2092         MLX5_SET(sqc, sqc, allow_swp, allow_swp);
2093 }
2094
2095 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
2096                                         struct mlx5e_cq_param *param)
2097 {
2098         void *cqc = param->cqc;
2099
2100         MLX5_SET(cqc, cqc, uar_page, priv->mdev->priv.uar->index);
2101         if (MLX5_CAP_GEN(priv->mdev, cqe_128_always) && cache_line_size() >= 128)
2102                 MLX5_SET(cqc, cqc, cqe_sz, CQE_STRIDE_128_PAD);
2103 }
2104
2105 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
2106                                     struct mlx5e_params *params,
2107                                     struct mlx5e_cq_param *param)
2108 {
2109         struct mlx5_core_dev *mdev = priv->mdev;
2110         void *cqc = param->cqc;
2111         u8 log_cq_size;
2112
2113         switch (params->rq_wq_type) {
2114         case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2115                 log_cq_size = mlx5e_mpwqe_get_log_rq_size(params) +
2116                         mlx5e_mpwqe_get_log_num_strides(mdev, params);
2117                 break;
2118         default: /* MLX5_WQ_TYPE_CYCLIC */
2119                 log_cq_size = params->log_rq_mtu_frames;
2120         }
2121
2122         MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
2123         if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
2124                 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
2125                 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
2126         }
2127
2128         mlx5e_build_common_cq_param(priv, param);
2129         param->cq_period_mode = params->rx_cq_moderation.cq_period_mode;
2130 }
2131
2132 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
2133                                     struct mlx5e_params *params,
2134                                     struct mlx5e_cq_param *param)
2135 {
2136         void *cqc = param->cqc;
2137
2138         MLX5_SET(cqc, cqc, log_cq_size, params->log_sq_size);
2139
2140         mlx5e_build_common_cq_param(priv, param);
2141         param->cq_period_mode = params->tx_cq_moderation.cq_period_mode;
2142 }
2143
2144 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
2145                                      u8 log_wq_size,
2146                                      struct mlx5e_cq_param *param)
2147 {
2148         void *cqc = param->cqc;
2149
2150         MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
2151
2152         mlx5e_build_common_cq_param(priv, param);
2153
2154         param->cq_period_mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2155 }
2156
2157 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
2158                                     u8 log_wq_size,
2159                                     struct mlx5e_sq_param *param)
2160 {
2161         void *sqc = param->sqc;
2162         void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2163
2164         mlx5e_build_sq_param_common(priv, param);
2165
2166         MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
2167         MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
2168 }
2169
2170 static void mlx5e_build_xdpsq_param(struct mlx5e_priv *priv,
2171                                     struct mlx5e_params *params,
2172                                     struct mlx5e_sq_param *param)
2173 {
2174         void *sqc = param->sqc;
2175         void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2176
2177         mlx5e_build_sq_param_common(priv, param);
2178         MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2179         param->is_mpw = MLX5E_GET_PFLAG(params, MLX5E_PFLAG_XDP_TX_MPWQE);
2180 }
2181
2182 static u8 mlx5e_build_icosq_log_wq_sz(struct mlx5e_params *params,
2183                                       struct mlx5e_rq_param *rqp)
2184 {
2185         switch (params->rq_wq_type) {
2186         case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2187                 return order_base_2(MLX5E_UMR_WQEBBS) +
2188                         mlx5e_get_rq_log_wq_sz(rqp->rqc);
2189         default: /* MLX5_WQ_TYPE_CYCLIC */
2190                 return MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
2191         }
2192 }
2193
2194 static void mlx5e_build_channel_param(struct mlx5e_priv *priv,
2195                                       struct mlx5e_params *params,
2196                                       struct mlx5e_channel_param *cparam)
2197 {
2198         u8 icosq_log_wq_sz;
2199
2200         mlx5e_build_rq_param(priv, params, &cparam->rq);
2201
2202         icosq_log_wq_sz = mlx5e_build_icosq_log_wq_sz(params, &cparam->rq);
2203
2204         mlx5e_build_sq_param(priv, params, &cparam->sq);
2205         mlx5e_build_xdpsq_param(priv, params, &cparam->xdp_sq);
2206         mlx5e_build_icosq_param(priv, icosq_log_wq_sz, &cparam->icosq);
2207         mlx5e_build_rx_cq_param(priv, params, &cparam->rx_cq);
2208         mlx5e_build_tx_cq_param(priv, params, &cparam->tx_cq);
2209         mlx5e_build_ico_cq_param(priv, icosq_log_wq_sz, &cparam->icosq_cq);
2210 }
2211
2212 int mlx5e_open_channels(struct mlx5e_priv *priv,
2213                         struct mlx5e_channels *chs)
2214 {
2215         struct mlx5e_channel_param *cparam;
2216         int err = -ENOMEM;
2217         int i;
2218
2219         chs->num = chs->params.num_channels;
2220
2221         chs->c = kcalloc(chs->num, sizeof(struct mlx5e_channel *), GFP_KERNEL);
2222         cparam = kvzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
2223         if (!chs->c || !cparam)
2224                 goto err_free;
2225
2226         mlx5e_build_channel_param(priv, &chs->params, cparam);
2227         for (i = 0; i < chs->num; i++) {
2228                 err = mlx5e_open_channel(priv, i, &chs->params, cparam, &chs->c[i]);
2229                 if (err)
2230                         goto err_close_channels;
2231         }
2232
2233         if (!IS_ERR_OR_NULL(priv->tx_reporter))
2234                 devlink_health_reporter_state_update(priv->tx_reporter,
2235                                                      DEVLINK_HEALTH_REPORTER_STATE_HEALTHY);
2236
2237         kvfree(cparam);
2238         return 0;
2239
2240 err_close_channels:
2241         for (i--; i >= 0; i--)
2242                 mlx5e_close_channel(chs->c[i]);
2243
2244 err_free:
2245         kfree(chs->c);
2246         kvfree(cparam);
2247         chs->num = 0;
2248         return err;
2249 }
2250
2251 static void mlx5e_activate_channels(struct mlx5e_channels *chs)
2252 {
2253         int i;
2254
2255         for (i = 0; i < chs->num; i++)
2256                 mlx5e_activate_channel(chs->c[i]);
2257 }
2258
2259 #define MLX5E_RQ_WQES_TIMEOUT 20000 /* msecs */
2260
2261 static int mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels *chs)
2262 {
2263         int err = 0;
2264         int i;
2265
2266         for (i = 0; i < chs->num; i++) {
2267                 int timeout = err ? 0 : MLX5E_RQ_WQES_TIMEOUT;
2268
2269                 err |= mlx5e_wait_for_min_rx_wqes(&chs->c[i]->rq, timeout);
2270         }
2271
2272         return err ? -ETIMEDOUT : 0;
2273 }
2274
2275 static void mlx5e_deactivate_channels(struct mlx5e_channels *chs)
2276 {
2277         int i;
2278
2279         for (i = 0; i < chs->num; i++)
2280                 mlx5e_deactivate_channel(chs->c[i]);
2281 }
2282
2283 void mlx5e_close_channels(struct mlx5e_channels *chs)
2284 {
2285         int i;
2286
2287         for (i = 0; i < chs->num; i++)
2288                 mlx5e_close_channel(chs->c[i]);
2289
2290         kfree(chs->c);
2291         chs->num = 0;
2292 }
2293
2294 static int
2295 mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, struct mlx5e_rqt *rqt)
2296 {
2297         struct mlx5_core_dev *mdev = priv->mdev;
2298         void *rqtc;
2299         int inlen;
2300         int err;
2301         u32 *in;
2302         int i;
2303
2304         inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
2305         in = kvzalloc(inlen, GFP_KERNEL);
2306         if (!in)
2307                 return -ENOMEM;
2308
2309         rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
2310
2311         MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2312         MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
2313
2314         for (i = 0; i < sz; i++)
2315                 MLX5_SET(rqtc, rqtc, rq_num[i], priv->drop_rq.rqn);
2316
2317         err = mlx5_core_create_rqt(mdev, in, inlen, &rqt->rqtn);
2318         if (!err)
2319                 rqt->enabled = true;
2320
2321         kvfree(in);
2322         return err;
2323 }
2324
2325 void mlx5e_destroy_rqt(struct mlx5e_priv *priv, struct mlx5e_rqt *rqt)
2326 {
2327         rqt->enabled = false;
2328         mlx5_core_destroy_rqt(priv->mdev, rqt->rqtn);
2329 }
2330
2331 int mlx5e_create_indirect_rqt(struct mlx5e_priv *priv)
2332 {
2333         struct mlx5e_rqt *rqt = &priv->indir_rqt;
2334         int err;
2335
2336         err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, rqt);
2337         if (err)
2338                 mlx5_core_warn(priv->mdev, "create indirect rqts failed, %d\n", err);
2339         return err;
2340 }
2341
2342 int mlx5e_create_direct_rqts(struct mlx5e_priv *priv)
2343 {
2344         struct mlx5e_rqt *rqt;
2345         int err;
2346         int ix;
2347
2348         for (ix = 0; ix < mlx5e_get_netdev_max_channels(priv->netdev); ix++) {
2349                 rqt = &priv->direct_tir[ix].rqt;
2350                 err = mlx5e_create_rqt(priv, 1 /*size */, rqt);
2351                 if (err)
2352                         goto err_destroy_rqts;
2353         }
2354
2355         return 0;
2356
2357 err_destroy_rqts:
2358         mlx5_core_warn(priv->mdev, "create direct rqts failed, %d\n", err);
2359         for (ix--; ix >= 0; ix--)
2360                 mlx5e_destroy_rqt(priv, &priv->direct_tir[ix].rqt);
2361
2362         return err;
2363 }
2364
2365 void mlx5e_destroy_direct_rqts(struct mlx5e_priv *priv)
2366 {
2367         int i;
2368
2369         for (i = 0; i < mlx5e_get_netdev_max_channels(priv->netdev); i++)
2370                 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
2371 }
2372
2373 static int mlx5e_rx_hash_fn(int hfunc)
2374 {
2375         return (hfunc == ETH_RSS_HASH_TOP) ?
2376                MLX5_RX_HASH_FN_TOEPLITZ :
2377                MLX5_RX_HASH_FN_INVERTED_XOR8;
2378 }
2379
2380 int mlx5e_bits_invert(unsigned long a, int size)
2381 {
2382         int inv = 0;
2383         int i;
2384
2385         for (i = 0; i < size; i++)
2386                 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
2387
2388         return inv;
2389 }
2390
2391 static void mlx5e_fill_rqt_rqns(struct mlx5e_priv *priv, int sz,
2392                                 struct mlx5e_redirect_rqt_param rrp, void *rqtc)
2393 {
2394         int i;
2395
2396         for (i = 0; i < sz; i++) {
2397                 u32 rqn;
2398
2399                 if (rrp.is_rss) {
2400                         int ix = i;
2401
2402                         if (rrp.rss.hfunc == ETH_RSS_HASH_XOR)
2403                                 ix = mlx5e_bits_invert(i, ilog2(sz));
2404
2405                         ix = priv->rss_params.indirection_rqt[ix];
2406                         rqn = rrp.rss.channels->c[ix]->rq.rqn;
2407                 } else {
2408                         rqn = rrp.rqn;
2409                 }
2410                 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
2411         }
2412 }
2413
2414 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz,
2415                        struct mlx5e_redirect_rqt_param rrp)
2416 {
2417         struct mlx5_core_dev *mdev = priv->mdev;
2418         void *rqtc;
2419         int inlen;
2420         u32 *in;
2421         int err;
2422
2423         inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
2424         in = kvzalloc(inlen, GFP_KERNEL);
2425         if (!in)
2426                 return -ENOMEM;
2427
2428         rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
2429
2430         MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2431         MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
2432         mlx5e_fill_rqt_rqns(priv, sz, rrp, rqtc);
2433         err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
2434
2435         kvfree(in);
2436         return err;
2437 }
2438
2439 static u32 mlx5e_get_direct_rqn(struct mlx5e_priv *priv, int ix,
2440                                 struct mlx5e_redirect_rqt_param rrp)
2441 {
2442         if (!rrp.is_rss)
2443                 return rrp.rqn;
2444
2445         if (ix >= rrp.rss.channels->num)
2446                 return priv->drop_rq.rqn;
2447
2448         return rrp.rss.channels->c[ix]->rq.rqn;
2449 }
2450
2451 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv,
2452                                 struct mlx5e_redirect_rqt_param rrp)
2453 {
2454         u32 rqtn;
2455         int ix;
2456
2457         if (priv->indir_rqt.enabled) {
2458                 /* RSS RQ table */
2459                 rqtn = priv->indir_rqt.rqtn;
2460                 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, rrp);
2461         }
2462
2463         for (ix = 0; ix < mlx5e_get_netdev_max_channels(priv->netdev); ix++) {
2464                 struct mlx5e_redirect_rqt_param direct_rrp = {
2465                         .is_rss = false,
2466                         {
2467                                 .rqn    = mlx5e_get_direct_rqn(priv, ix, rrp)
2468                         },
2469                 };
2470
2471                 /* Direct RQ Tables */
2472                 if (!priv->direct_tir[ix].rqt.enabled)
2473                         continue;
2474
2475                 rqtn = priv->direct_tir[ix].rqt.rqtn;
2476                 mlx5e_redirect_rqt(priv, rqtn, 1, direct_rrp);
2477         }
2478 }
2479
2480 static void mlx5e_redirect_rqts_to_channels(struct mlx5e_priv *priv,
2481                                             struct mlx5e_channels *chs)
2482 {
2483         struct mlx5e_redirect_rqt_param rrp = {
2484                 .is_rss        = true,
2485                 {
2486                         .rss = {
2487                                 .channels  = chs,
2488                                 .hfunc     = priv->rss_params.hfunc,
2489                         }
2490                 },
2491         };
2492
2493         mlx5e_redirect_rqts(priv, rrp);
2494 }
2495
2496 static void mlx5e_redirect_rqts_to_drop(struct mlx5e_priv *priv)
2497 {
2498         struct mlx5e_redirect_rqt_param drop_rrp = {
2499                 .is_rss = false,
2500                 {
2501                         .rqn = priv->drop_rq.rqn,
2502                 },
2503         };
2504
2505         mlx5e_redirect_rqts(priv, drop_rrp);
2506 }
2507
2508 static const struct mlx5e_tirc_config tirc_default_config[MLX5E_NUM_INDIR_TIRS] = {
2509         [MLX5E_TT_IPV4_TCP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2510                                 .l4_prot_type = MLX5_L4_PROT_TYPE_TCP,
2511                                 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2512         },
2513         [MLX5E_TT_IPV6_TCP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2514                                 .l4_prot_type = MLX5_L4_PROT_TYPE_TCP,
2515                                 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2516         },
2517         [MLX5E_TT_IPV4_UDP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2518                                 .l4_prot_type = MLX5_L4_PROT_TYPE_UDP,
2519                                 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2520         },
2521         [MLX5E_TT_IPV6_UDP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2522                                 .l4_prot_type = MLX5_L4_PROT_TYPE_UDP,
2523                                 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2524         },
2525         [MLX5E_TT_IPV4_IPSEC_AH] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2526                                      .l4_prot_type = 0,
2527                                      .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2528         },
2529         [MLX5E_TT_IPV6_IPSEC_AH] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2530                                      .l4_prot_type = 0,
2531                                      .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2532         },
2533         [MLX5E_TT_IPV4_IPSEC_ESP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2534                                       .l4_prot_type = 0,
2535                                       .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2536         },
2537         [MLX5E_TT_IPV6_IPSEC_ESP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2538                                       .l4_prot_type = 0,
2539                                       .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2540         },
2541         [MLX5E_TT_IPV4] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2542                             .l4_prot_type = 0,
2543                             .rx_hash_fields = MLX5_HASH_IP,
2544         },
2545         [MLX5E_TT_IPV6] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2546                             .l4_prot_type = 0,
2547                             .rx_hash_fields = MLX5_HASH_IP,
2548         },
2549 };
2550
2551 struct mlx5e_tirc_config mlx5e_tirc_get_default_config(enum mlx5e_traffic_types tt)
2552 {
2553         return tirc_default_config[tt];
2554 }
2555
2556 static void mlx5e_build_tir_ctx_lro(struct mlx5e_params *params, void *tirc)
2557 {
2558         if (!params->lro_en)
2559                 return;
2560
2561 #define ROUGH_MAX_L2_L3_HDR_SZ 256
2562
2563         MLX5_SET(tirc, tirc, lro_enable_mask,
2564                  MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2565                  MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
2566         MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
2567                  (MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ - ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
2568         MLX5_SET(tirc, tirc, lro_timeout_period_usecs, params->lro_timeout);
2569 }
2570
2571 void mlx5e_build_indir_tir_ctx_hash(struct mlx5e_rss_params *rss_params,
2572                                     const struct mlx5e_tirc_config *ttconfig,
2573                                     void *tirc, bool inner)
2574 {
2575         void *hfso = inner ? MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner) :
2576                              MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2577
2578         MLX5_SET(tirc, tirc, rx_hash_fn, mlx5e_rx_hash_fn(rss_params->hfunc));
2579         if (rss_params->hfunc == ETH_RSS_HASH_TOP) {
2580                 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
2581                                              rx_hash_toeplitz_key);
2582                 size_t len = MLX5_FLD_SZ_BYTES(tirc,
2583                                                rx_hash_toeplitz_key);
2584
2585                 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
2586                 memcpy(rss_key, rss_params->toeplitz_hash_key, len);
2587         }
2588         MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2589                  ttconfig->l3_prot_type);
2590         MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2591                  ttconfig->l4_prot_type);
2592         MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2593                  ttconfig->rx_hash_fields);
2594 }
2595
2596 static void mlx5e_update_rx_hash_fields(struct mlx5e_tirc_config *ttconfig,
2597                                         enum mlx5e_traffic_types tt,
2598                                         u32 rx_hash_fields)
2599 {
2600         *ttconfig                = tirc_default_config[tt];
2601         ttconfig->rx_hash_fields = rx_hash_fields;
2602 }
2603
2604 void mlx5e_modify_tirs_hash(struct mlx5e_priv *priv, void *in, int inlen)
2605 {
2606         void *tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2607         struct mlx5e_rss_params *rss = &priv->rss_params;
2608         struct mlx5_core_dev *mdev = priv->mdev;
2609         int ctxlen = MLX5_ST_SZ_BYTES(tirc);
2610         struct mlx5e_tirc_config ttconfig;
2611         int tt;
2612
2613         MLX5_SET(modify_tir_in, in, bitmask.hash, 1);
2614
2615         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2616                 memset(tirc, 0, ctxlen);
2617                 mlx5e_update_rx_hash_fields(&ttconfig, tt,
2618                                             rss->rx_hash_fields[tt]);
2619                 mlx5e_build_indir_tir_ctx_hash(rss, &ttconfig, tirc, false);
2620                 mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in, inlen);
2621         }
2622
2623         if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
2624                 return;
2625
2626         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2627                 memset(tirc, 0, ctxlen);
2628                 mlx5e_update_rx_hash_fields(&ttconfig, tt,
2629                                             rss->rx_hash_fields[tt]);
2630                 mlx5e_build_indir_tir_ctx_hash(rss, &ttconfig, tirc, true);
2631                 mlx5_core_modify_tir(mdev, priv->inner_indir_tir[tt].tirn, in,
2632                                      inlen);
2633         }
2634 }
2635
2636 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
2637 {
2638         struct mlx5_core_dev *mdev = priv->mdev;
2639
2640         void *in;
2641         void *tirc;
2642         int inlen;
2643         int err;
2644         int tt;
2645         int ix;
2646
2647         inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
2648         in = kvzalloc(inlen, GFP_KERNEL);
2649         if (!in)
2650                 return -ENOMEM;
2651
2652         MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
2653         tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2654
2655         mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2656
2657         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2658                 err = mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in,
2659                                            inlen);
2660                 if (err)
2661                         goto free_in;
2662         }
2663
2664         for (ix = 0; ix < mlx5e_get_netdev_max_channels(priv->netdev); ix++) {
2665                 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
2666                                            in, inlen);
2667                 if (err)
2668                         goto free_in;
2669         }
2670
2671 free_in:
2672         kvfree(in);
2673
2674         return err;
2675 }
2676
2677 static int mlx5e_set_mtu(struct mlx5_core_dev *mdev,
2678                          struct mlx5e_params *params, u16 mtu)
2679 {
2680         u16 hw_mtu = MLX5E_SW2HW_MTU(params, mtu);
2681         int err;
2682
2683         err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
2684         if (err)
2685                 return err;
2686
2687         /* Update vport context MTU */
2688         mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
2689         return 0;
2690 }
2691
2692 static void mlx5e_query_mtu(struct mlx5_core_dev *mdev,
2693                             struct mlx5e_params *params, u16 *mtu)
2694 {
2695         u16 hw_mtu = 0;
2696         int err;
2697
2698         err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
2699         if (err || !hw_mtu) /* fallback to port oper mtu */
2700                 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
2701
2702         *mtu = MLX5E_HW2SW_MTU(params, hw_mtu);
2703 }
2704
2705 int mlx5e_set_dev_port_mtu(struct mlx5e_priv *priv)
2706 {
2707         struct mlx5e_params *params = &priv->channels.params;
2708         struct net_device *netdev = priv->netdev;
2709         struct mlx5_core_dev *mdev = priv->mdev;
2710         u16 mtu;
2711         int err;
2712
2713         err = mlx5e_set_mtu(mdev, params, params->sw_mtu);
2714         if (err)
2715                 return err;
2716
2717         mlx5e_query_mtu(mdev, params, &mtu);
2718         if (mtu != params->sw_mtu)
2719                 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
2720                             __func__, mtu, params->sw_mtu);
2721
2722         params->sw_mtu = mtu;
2723         return 0;
2724 }
2725
2726 void mlx5e_set_netdev_mtu_boundaries(struct mlx5e_priv *priv)
2727 {
2728         struct mlx5e_params *params = &priv->channels.params;
2729         struct net_device *netdev   = priv->netdev;
2730         struct mlx5_core_dev *mdev  = priv->mdev;
2731         u16 max_mtu;
2732
2733         /* MTU range: 68 - hw-specific max */
2734         netdev->min_mtu = ETH_MIN_MTU;
2735
2736         mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
2737         netdev->max_mtu = min_t(unsigned int, MLX5E_HW2SW_MTU(params, max_mtu),
2738                                 ETH_MAX_MTU);
2739 }
2740
2741 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
2742 {
2743         struct mlx5e_priv *priv = netdev_priv(netdev);
2744         int nch = priv->channels.params.num_channels;
2745         int ntc = priv->channels.params.num_tc;
2746         int tc;
2747
2748         netdev_reset_tc(netdev);
2749
2750         if (ntc == 1)
2751                 return;
2752
2753         netdev_set_num_tc(netdev, ntc);
2754
2755         /* Map netdev TCs to offset 0
2756          * We have our own UP to TXQ mapping for QoS
2757          */
2758         for (tc = 0; tc < ntc; tc++)
2759                 netdev_set_tc_queue(netdev, tc, nch, 0);
2760 }
2761
2762 static void mlx5e_build_tc2txq_maps(struct mlx5e_priv *priv)
2763 {
2764         int max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
2765         int i, tc;
2766
2767         for (i = 0; i < max_nch; i++)
2768                 for (tc = 0; tc < priv->profile->max_tc; tc++)
2769                         priv->channel_tc2txq[i][tc] = i + tc * max_nch;
2770 }
2771
2772 static void mlx5e_build_tx2sq_maps(struct mlx5e_priv *priv)
2773 {
2774         struct mlx5e_channel *c;
2775         struct mlx5e_txqsq *sq;
2776         int i, tc;
2777
2778         for (i = 0; i < priv->channels.num; i++) {
2779                 c = priv->channels.c[i];
2780                 for (tc = 0; tc < c->num_tc; tc++) {
2781                         sq = &c->sq[tc];
2782                         priv->txq2sq[sq->txq_ix] = sq;
2783                 }
2784         }
2785 }
2786
2787 void mlx5e_activate_priv_channels(struct mlx5e_priv *priv)
2788 {
2789         int num_txqs = priv->channels.num * priv->channels.params.num_tc;
2790         struct net_device *netdev = priv->netdev;
2791
2792         mlx5e_netdev_set_tcs(netdev);
2793         netif_set_real_num_tx_queues(netdev, num_txqs);
2794         netif_set_real_num_rx_queues(netdev, priv->channels.num);
2795
2796         mlx5e_build_tx2sq_maps(priv);
2797         mlx5e_activate_channels(&priv->channels);
2798         mlx5e_xdp_tx_enable(priv);
2799         netif_tx_start_all_queues(priv->netdev);
2800
2801         if (mlx5e_is_vport_rep(priv))
2802                 mlx5e_add_sqs_fwd_rules(priv);
2803
2804         mlx5e_wait_channels_min_rx_wqes(&priv->channels);
2805         mlx5e_redirect_rqts_to_channels(priv, &priv->channels);
2806 }
2807
2808 void mlx5e_deactivate_priv_channels(struct mlx5e_priv *priv)
2809 {
2810         mlx5e_redirect_rqts_to_drop(priv);
2811
2812         if (mlx5e_is_vport_rep(priv))
2813                 mlx5e_remove_sqs_fwd_rules(priv);
2814
2815         /* FIXME: This is a W/A only for tx timeout watch dog false alarm when
2816          * polling for inactive tx queues.
2817          */
2818         netif_tx_stop_all_queues(priv->netdev);
2819         netif_tx_disable(priv->netdev);
2820         mlx5e_xdp_tx_disable(priv);
2821         mlx5e_deactivate_channels(&priv->channels);
2822 }
2823
2824 static void mlx5e_switch_priv_channels(struct mlx5e_priv *priv,
2825                                        struct mlx5e_channels *new_chs,
2826                                        mlx5e_fp_hw_modify hw_modify)
2827 {
2828         struct net_device *netdev = priv->netdev;
2829         int new_num_txqs;
2830         int carrier_ok;
2831
2832         new_num_txqs = new_chs->num * new_chs->params.num_tc;
2833
2834         carrier_ok = netif_carrier_ok(netdev);
2835         netif_carrier_off(netdev);
2836
2837         if (new_num_txqs < netdev->real_num_tx_queues)
2838                 netif_set_real_num_tx_queues(netdev, new_num_txqs);
2839
2840         mlx5e_deactivate_priv_channels(priv);
2841         mlx5e_close_channels(&priv->channels);
2842
2843         priv->channels = *new_chs;
2844
2845         /* New channels are ready to roll, modify HW settings if needed */
2846         if (hw_modify)
2847                 hw_modify(priv);
2848
2849         mlx5e_refresh_tirs(priv, false);
2850         mlx5e_activate_priv_channels(priv);
2851
2852         /* return carrier back if needed */
2853         if (carrier_ok)
2854                 netif_carrier_on(netdev);
2855 }
2856
2857 int mlx5e_safe_switch_channels(struct mlx5e_priv *priv,
2858                                struct mlx5e_channels *new_chs,
2859                                mlx5e_fp_hw_modify hw_modify)
2860 {
2861         int err;
2862
2863         err = mlx5e_open_channels(priv, new_chs);
2864         if (err)
2865                 return err;
2866
2867         mlx5e_switch_priv_channels(priv, new_chs, hw_modify);
2868         return 0;
2869 }
2870
2871 int mlx5e_safe_reopen_channels(struct mlx5e_priv *priv)
2872 {
2873         struct mlx5e_channels new_channels = {};
2874
2875         new_channels.params = priv->channels.params;
2876         return mlx5e_safe_switch_channels(priv, &new_channels, NULL);
2877 }
2878
2879 void mlx5e_timestamp_init(struct mlx5e_priv *priv)
2880 {
2881         priv->tstamp.tx_type   = HWTSTAMP_TX_OFF;
2882         priv->tstamp.rx_filter = HWTSTAMP_FILTER_NONE;
2883 }
2884
2885 int mlx5e_open_locked(struct net_device *netdev)
2886 {
2887         struct mlx5e_priv *priv = netdev_priv(netdev);
2888         int err;
2889
2890         set_bit(MLX5E_STATE_OPENED, &priv->state);
2891
2892         err = mlx5e_open_channels(priv, &priv->channels);
2893         if (err)
2894                 goto err_clear_state_opened_flag;
2895
2896         mlx5e_refresh_tirs(priv, false);
2897         mlx5e_activate_priv_channels(priv);
2898         if (priv->profile->update_carrier)
2899                 priv->profile->update_carrier(priv);
2900
2901         mlx5e_queue_update_stats(priv);
2902         return 0;
2903
2904 err_clear_state_opened_flag:
2905         clear_bit(MLX5E_STATE_OPENED, &priv->state);
2906         return err;
2907 }
2908
2909 int mlx5e_open(struct net_device *netdev)
2910 {
2911         struct mlx5e_priv *priv = netdev_priv(netdev);
2912         int err;
2913
2914         mutex_lock(&priv->state_lock);
2915         err = mlx5e_open_locked(netdev);
2916         if (!err)
2917                 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_UP);
2918         mutex_unlock(&priv->state_lock);
2919
2920         if (mlx5_vxlan_allowed(priv->mdev->vxlan))
2921                 udp_tunnel_get_rx_info(netdev);
2922
2923         return err;
2924 }
2925
2926 int mlx5e_close_locked(struct net_device *netdev)
2927 {
2928         struct mlx5e_priv *priv = netdev_priv(netdev);
2929
2930         /* May already be CLOSED in case a previous configuration operation
2931          * (e.g RX/TX queue size change) that involves close&open failed.
2932          */
2933         if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2934                 return 0;
2935
2936         clear_bit(MLX5E_STATE_OPENED, &priv->state);
2937
2938         netif_carrier_off(priv->netdev);
2939         mlx5e_deactivate_priv_channels(priv);
2940         mlx5e_close_channels(&priv->channels);
2941
2942         return 0;
2943 }
2944
2945 int mlx5e_close(struct net_device *netdev)
2946 {
2947         struct mlx5e_priv *priv = netdev_priv(netdev);
2948         int err;
2949
2950         if (!netif_device_present(netdev))
2951                 return -ENODEV;
2952
2953         mutex_lock(&priv->state_lock);
2954         mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_DOWN);
2955         err = mlx5e_close_locked(netdev);
2956         mutex_unlock(&priv->state_lock);
2957
2958         return err;
2959 }
2960
2961 static int mlx5e_alloc_drop_rq(struct mlx5_core_dev *mdev,
2962                                struct mlx5e_rq *rq,
2963                                struct mlx5e_rq_param *param)
2964 {
2965         void *rqc = param->rqc;
2966         void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
2967         int err;
2968
2969         param->wq.db_numa_node = param->wq.buf_numa_node;
2970
2971         err = mlx5_wq_cyc_create(mdev, &param->wq, rqc_wq, &rq->wqe.wq,
2972                                  &rq->wq_ctrl);
2973         if (err)
2974                 return err;
2975
2976         /* Mark as unused given "Drop-RQ" packets never reach XDP */
2977         xdp_rxq_info_unused(&rq->xdp_rxq);
2978
2979         rq->mdev = mdev;
2980
2981         return 0;
2982 }
2983
2984 static int mlx5e_alloc_drop_cq(struct mlx5_core_dev *mdev,
2985                                struct mlx5e_cq *cq,
2986                                struct mlx5e_cq_param *param)
2987 {
2988         param->wq.buf_numa_node = dev_to_node(mdev->device);
2989         param->wq.db_numa_node  = dev_to_node(mdev->device);
2990
2991         return mlx5e_alloc_cq_common(mdev, param, cq);
2992 }
2993
2994 int mlx5e_open_drop_rq(struct mlx5e_priv *priv,
2995                        struct mlx5e_rq *drop_rq)
2996 {
2997         struct mlx5_core_dev *mdev = priv->mdev;
2998         struct mlx5e_cq_param cq_param = {};
2999         struct mlx5e_rq_param rq_param = {};
3000         struct mlx5e_cq *cq = &drop_rq->cq;
3001         int err;
3002
3003         mlx5e_build_drop_rq_param(priv, &rq_param);
3004
3005         err = mlx5e_alloc_drop_cq(mdev, cq, &cq_param);
3006         if (err)
3007                 return err;
3008
3009         err = mlx5e_create_cq(cq, &cq_param);
3010         if (err)
3011                 goto err_free_cq;
3012
3013         err = mlx5e_alloc_drop_rq(mdev, drop_rq, &rq_param);
3014         if (err)
3015                 goto err_destroy_cq;
3016
3017         err = mlx5e_create_rq(drop_rq, &rq_param);
3018         if (err)
3019                 goto err_free_rq;
3020
3021         err = mlx5e_modify_rq_state(drop_rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
3022         if (err)
3023                 mlx5_core_warn(priv->mdev, "modify_rq_state failed, rx_if_down_packets won't be counted %d\n", err);
3024
3025         return 0;
3026
3027 err_free_rq:
3028         mlx5e_free_rq(drop_rq);
3029
3030 err_destroy_cq:
3031         mlx5e_destroy_cq(cq);
3032
3033 err_free_cq:
3034         mlx5e_free_cq(cq);
3035
3036         return err;
3037 }
3038
3039 void mlx5e_close_drop_rq(struct mlx5e_rq *drop_rq)
3040 {
3041         mlx5e_destroy_rq(drop_rq);
3042         mlx5e_free_rq(drop_rq);
3043         mlx5e_destroy_cq(&drop_rq->cq);
3044         mlx5e_free_cq(&drop_rq->cq);
3045 }
3046
3047 int mlx5e_create_tis(struct mlx5_core_dev *mdev, int tc,
3048                      u32 underlay_qpn, u32 *tisn)
3049 {
3050         u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
3051         void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
3052
3053         MLX5_SET(tisc, tisc, prio, tc << 1);
3054         MLX5_SET(tisc, tisc, underlay_qpn, underlay_qpn);
3055         MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.td.tdn);
3056
3057         if (mlx5_lag_is_lacp_owner(mdev))
3058                 MLX5_SET(tisc, tisc, strict_lag_tx_port_affinity, 1);
3059
3060         return mlx5_core_create_tis(mdev, in, sizeof(in), tisn);
3061 }
3062
3063 void mlx5e_destroy_tis(struct mlx5_core_dev *mdev, u32 tisn)
3064 {
3065         mlx5_core_destroy_tis(mdev, tisn);
3066 }
3067
3068 int mlx5e_create_tises(struct mlx5e_priv *priv)
3069 {
3070         int err;
3071         int tc;
3072
3073         for (tc = 0; tc < priv->profile->max_tc; tc++) {
3074                 err = mlx5e_create_tis(priv->mdev, tc, 0, &priv->tisn[tc]);
3075                 if (err)
3076                         goto err_close_tises;
3077         }
3078
3079         return 0;
3080
3081 err_close_tises:
3082         for (tc--; tc >= 0; tc--)
3083                 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
3084
3085         return err;
3086 }
3087
3088 static void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
3089 {
3090         int tc;
3091
3092         mlx5e_tx_reporter_destroy(priv);
3093         for (tc = 0; tc < priv->profile->max_tc; tc++)
3094                 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
3095 }
3096
3097 static void mlx5e_build_indir_tir_ctx_common(struct mlx5e_priv *priv,
3098                                              u32 rqtn, u32 *tirc)
3099 {
3100         MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
3101         MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
3102         MLX5_SET(tirc, tirc, indirect_table, rqtn);
3103         MLX5_SET(tirc, tirc, tunneled_offload_en,
3104                  priv->channels.params.tunneled_offload_en);
3105
3106         mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
3107 }
3108
3109 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv,
3110                                       enum mlx5e_traffic_types tt,
3111                                       u32 *tirc)
3112 {
3113         mlx5e_build_indir_tir_ctx_common(priv, priv->indir_rqt.rqtn, tirc);
3114         mlx5e_build_indir_tir_ctx_hash(&priv->rss_params,
3115                                        &tirc_default_config[tt], tirc, false);
3116 }
3117
3118 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 rqtn, u32 *tirc)
3119 {
3120         mlx5e_build_indir_tir_ctx_common(priv, rqtn, tirc);
3121         MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
3122 }
3123
3124 static void mlx5e_build_inner_indir_tir_ctx(struct mlx5e_priv *priv,
3125                                             enum mlx5e_traffic_types tt,
3126                                             u32 *tirc)
3127 {
3128         mlx5e_build_indir_tir_ctx_common(priv, priv->indir_rqt.rqtn, tirc);
3129         mlx5e_build_indir_tir_ctx_hash(&priv->rss_params,
3130                                        &tirc_default_config[tt], tirc, true);
3131 }
3132
3133 int mlx5e_create_indirect_tirs(struct mlx5e_priv *priv, bool inner_ttc)
3134 {
3135         struct mlx5e_tir *tir;
3136         void *tirc;
3137         int inlen;
3138         int i = 0;
3139         int err;
3140         u32 *in;
3141         int tt;
3142
3143         inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3144         in = kvzalloc(inlen, GFP_KERNEL);
3145         if (!in)
3146                 return -ENOMEM;
3147
3148         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
3149                 memset(in, 0, inlen);
3150                 tir = &priv->indir_tir[tt];
3151                 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3152                 mlx5e_build_indir_tir_ctx(priv, tt, tirc);
3153                 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3154                 if (err) {
3155                         mlx5_core_warn(priv->mdev, "create indirect tirs failed, %d\n", err);
3156                         goto err_destroy_inner_tirs;
3157                 }
3158         }
3159
3160         if (!inner_ttc || !mlx5e_tunnel_inner_ft_supported(priv->mdev))
3161                 goto out;
3162
3163         for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
3164                 memset(in, 0, inlen);
3165                 tir = &priv->inner_indir_tir[i];
3166                 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3167                 mlx5e_build_inner_indir_tir_ctx(priv, i, tirc);
3168                 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3169                 if (err) {
3170                         mlx5_core_warn(priv->mdev, "create inner indirect tirs failed, %d\n", err);
3171                         goto err_destroy_inner_tirs;
3172                 }
3173         }
3174
3175 out:
3176         kvfree(in);
3177
3178         return 0;
3179
3180 err_destroy_inner_tirs:
3181         for (i--; i >= 0; i--)
3182                 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3183
3184         for (tt--; tt >= 0; tt--)
3185                 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[tt]);
3186
3187         kvfree(in);
3188
3189         return err;
3190 }
3191
3192 int mlx5e_create_direct_tirs(struct mlx5e_priv *priv)
3193 {
3194         int nch = mlx5e_get_netdev_max_channels(priv->netdev);
3195         struct mlx5e_tir *tir;
3196         void *tirc;
3197         int inlen;
3198         int err;
3199         u32 *in;
3200         int ix;
3201
3202         inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3203         in = kvzalloc(inlen, GFP_KERNEL);
3204         if (!in)
3205                 return -ENOMEM;
3206
3207         for (ix = 0; ix < nch; ix++) {
3208                 memset(in, 0, inlen);
3209                 tir = &priv->direct_tir[ix];
3210                 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3211                 mlx5e_build_direct_tir_ctx(priv, priv->direct_tir[ix].rqt.rqtn, tirc);
3212                 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3213                 if (err)
3214                         goto err_destroy_ch_tirs;
3215         }
3216
3217         kvfree(in);
3218
3219         return 0;
3220
3221 err_destroy_ch_tirs:
3222         mlx5_core_warn(priv->mdev, "create direct tirs failed, %d\n", err);
3223         for (ix--; ix >= 0; ix--)
3224                 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[ix]);
3225
3226         kvfree(in);
3227
3228         return err;
3229 }
3230
3231 void mlx5e_destroy_indirect_tirs(struct mlx5e_priv *priv, bool inner_ttc)
3232 {
3233         int i;
3234
3235         for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3236                 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[i]);
3237
3238         if (!inner_ttc || !mlx5e_tunnel_inner_ft_supported(priv->mdev))
3239                 return;
3240
3241         for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3242                 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3243 }
3244
3245 void mlx5e_destroy_direct_tirs(struct mlx5e_priv *priv)
3246 {
3247         int nch = mlx5e_get_netdev_max_channels(priv->netdev);
3248         int i;
3249
3250         for (i = 0; i < nch; i++)
3251                 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[i]);
3252 }
3253
3254 static int mlx5e_modify_channels_scatter_fcs(struct mlx5e_channels *chs, bool enable)
3255 {
3256         int err = 0;
3257         int i;
3258
3259         for (i = 0; i < chs->num; i++) {
3260                 err = mlx5e_modify_rq_scatter_fcs(&chs->c[i]->rq, enable);
3261                 if (err)
3262                         return err;
3263         }
3264
3265         return 0;
3266 }
3267
3268 static int mlx5e_modify_channels_vsd(struct mlx5e_channels *chs, bool vsd)
3269 {
3270         int err = 0;
3271         int i;
3272
3273         for (i = 0; i < chs->num; i++) {
3274                 err = mlx5e_modify_rq_vsd(&chs->c[i]->rq, vsd);
3275                 if (err)
3276                         return err;
3277         }
3278
3279         return 0;
3280 }
3281
3282 static int mlx5e_setup_tc_mqprio(struct net_device *netdev,
3283                                  struct tc_mqprio_qopt *mqprio)
3284 {
3285         struct mlx5e_priv *priv = netdev_priv(netdev);
3286         struct mlx5e_channels new_channels = {};
3287         u8 tc = mqprio->num_tc;
3288         int err = 0;
3289
3290         mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
3291
3292         if (tc && tc != MLX5E_MAX_NUM_TC)
3293                 return -EINVAL;
3294
3295         mutex_lock(&priv->state_lock);
3296
3297         new_channels.params = priv->channels.params;
3298         new_channels.params.num_tc = tc ? tc : 1;
3299
3300         if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
3301                 priv->channels.params = new_channels.params;
3302                 goto out;
3303         }
3304
3305         err = mlx5e_safe_switch_channels(priv, &new_channels, NULL);
3306         if (err)
3307                 goto out;
3308
3309         priv->max_opened_tc = max_t(u8, priv->max_opened_tc,
3310                                     new_channels.params.num_tc);
3311 out:
3312         mutex_unlock(&priv->state_lock);
3313         return err;
3314 }
3315
3316 #ifdef CONFIG_MLX5_ESWITCH
3317 static int mlx5e_setup_tc_cls_flower(struct mlx5e_priv *priv,
3318                                      struct tc_cls_flower_offload *cls_flower,
3319                                      int flags)
3320 {
3321         switch (cls_flower->command) {
3322         case TC_CLSFLOWER_REPLACE:
3323                 return mlx5e_configure_flower(priv->netdev, priv, cls_flower,
3324                                               flags);
3325         case TC_CLSFLOWER_DESTROY:
3326                 return mlx5e_delete_flower(priv->netdev, priv, cls_flower,
3327                                            flags);
3328         case TC_CLSFLOWER_STATS:
3329                 return mlx5e_stats_flower(priv->netdev, priv, cls_flower,
3330                                           flags);
3331         default:
3332                 return -EOPNOTSUPP;
3333         }
3334 }
3335
3336 static int mlx5e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
3337                                    void *cb_priv)
3338 {
3339         struct mlx5e_priv *priv = cb_priv;
3340
3341         switch (type) {
3342         case TC_SETUP_CLSFLOWER:
3343                 return mlx5e_setup_tc_cls_flower(priv, type_data, MLX5E_TC_INGRESS |
3344                                                  MLX5E_TC_NIC_OFFLOAD);
3345         default:
3346                 return -EOPNOTSUPP;
3347         }
3348 }
3349
3350 static int mlx5e_setup_tc_block(struct net_device *dev,
3351                                 struct tc_block_offload *f)
3352 {
3353         struct mlx5e_priv *priv = netdev_priv(dev);
3354
3355         if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
3356                 return -EOPNOTSUPP;
3357
3358         switch (f->command) {
3359         case TC_BLOCK_BIND:
3360                 return tcf_block_cb_register(f->block, mlx5e_setup_tc_block_cb,
3361                                              priv, priv, f->extack);
3362         case TC_BLOCK_UNBIND:
3363                 tcf_block_cb_unregister(f->block, mlx5e_setup_tc_block_cb,
3364                                         priv);
3365                 return 0;
3366         default:
3367                 return -EOPNOTSUPP;
3368         }
3369 }
3370 #endif
3371
3372 static int mlx5e_setup_tc(struct net_device *dev, enum tc_setup_type type,
3373                           void *type_data)
3374 {
3375         switch (type) {
3376 #ifdef CONFIG_MLX5_ESWITCH
3377         case TC_SETUP_BLOCK:
3378                 return mlx5e_setup_tc_block(dev, type_data);
3379 #endif
3380         case TC_SETUP_QDISC_MQPRIO:
3381                 return mlx5e_setup_tc_mqprio(dev, type_data);
3382         default:
3383                 return -EOPNOTSUPP;
3384         }
3385 }
3386
3387 void mlx5e_fold_sw_stats64(struct mlx5e_priv *priv, struct rtnl_link_stats64 *s)
3388 {
3389         int i;
3390
3391         for (i = 0; i < mlx5e_get_netdev_max_channels(priv->netdev); i++) {
3392                 struct mlx5e_channel_stats *channel_stats = &priv->channel_stats[i];
3393                 struct mlx5e_rq_stats *rq_stats = &channel_stats->rq;
3394                 int j;
3395
3396                 s->rx_packets   += rq_stats->packets;
3397                 s->rx_bytes     += rq_stats->bytes;
3398
3399                 for (j = 0; j < priv->max_opened_tc; j++) {
3400                         struct mlx5e_sq_stats *sq_stats = &channel_stats->sq[j];
3401
3402                         s->tx_packets    += sq_stats->packets;
3403                         s->tx_bytes      += sq_stats->bytes;
3404                         s->tx_dropped    += sq_stats->dropped;
3405                 }
3406         }
3407 }
3408
3409 void
3410 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
3411 {
3412         struct mlx5e_priv *priv = netdev_priv(dev);
3413         struct mlx5e_vport_stats *vstats = &priv->stats.vport;
3414         struct mlx5e_pport_stats *pstats = &priv->stats.pport;
3415
3416         if (!mlx5e_monitor_counter_supported(priv)) {
3417                 /* update HW stats in background for next time */
3418                 mlx5e_queue_update_stats(priv);
3419         }
3420
3421         if (mlx5e_is_uplink_rep(priv)) {
3422                 stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
3423                 stats->rx_bytes   = PPORT_802_3_GET(pstats, a_octets_received_ok);
3424                 stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
3425                 stats->tx_bytes   = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
3426         } else {
3427                 mlx5e_fold_sw_stats64(priv, stats);
3428         }
3429
3430         stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
3431
3432         stats->rx_length_errors =
3433                 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
3434                 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
3435                 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
3436         stats->rx_crc_errors =
3437                 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
3438         stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
3439         stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
3440         stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
3441                            stats->rx_frame_errors;
3442         stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
3443
3444         /* vport multicast also counts packets that are dropped due to steering
3445          * or rx out of buffer
3446          */
3447         stats->multicast =
3448                 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
3449 }
3450
3451 static void mlx5e_set_rx_mode(struct net_device *dev)
3452 {
3453         struct mlx5e_priv *priv = netdev_priv(dev);
3454
3455         queue_work(priv->wq, &priv->set_rx_mode_work);
3456 }
3457
3458 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
3459 {
3460         struct mlx5e_priv *priv = netdev_priv(netdev);
3461         struct sockaddr *saddr = addr;
3462
3463         if (!is_valid_ether_addr(saddr->sa_data))
3464                 return -EADDRNOTAVAIL;
3465
3466         netif_addr_lock_bh(netdev);
3467         ether_addr_copy(netdev->dev_addr, saddr->sa_data);
3468         netif_addr_unlock_bh(netdev);
3469
3470         queue_work(priv->wq, &priv->set_rx_mode_work);
3471
3472         return 0;
3473 }
3474
3475 #define MLX5E_SET_FEATURE(features, feature, enable)    \
3476         do {                                            \
3477                 if (enable)                             \
3478                         *features |= feature;           \
3479                 else                                    \
3480                         *features &= ~feature;          \
3481         } while (0)
3482
3483 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
3484
3485 static int set_feature_lro(struct net_device *netdev, bool enable)
3486 {
3487         struct mlx5e_priv *priv = netdev_priv(netdev);
3488         struct mlx5_core_dev *mdev = priv->mdev;
3489         struct mlx5e_channels new_channels = {};
3490         struct mlx5e_params *old_params;
3491         int err = 0;
3492         bool reset;
3493
3494         mutex_lock(&priv->state_lock);
3495
3496         old_params = &priv->channels.params;
3497         if (enable && !MLX5E_GET_PFLAG(old_params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
3498                 netdev_warn(netdev, "can't set LRO with legacy RQ\n");
3499                 err = -EINVAL;
3500                 goto out;
3501         }
3502
3503         reset = test_bit(MLX5E_STATE_OPENED, &priv->state);
3504
3505         new_channels.params = *old_params;
3506         new_channels.params.lro_en = enable;
3507
3508         if (old_params->rq_wq_type != MLX5_WQ_TYPE_CYCLIC) {
3509                 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, old_params) ==
3510                     mlx5e_rx_mpwqe_is_linear_skb(mdev, &new_channels.params))
3511                         reset = false;
3512         }
3513
3514         if (!reset) {
3515                 *old_params = new_channels.params;
3516                 err = mlx5e_modify_tirs_lro(priv);
3517                 goto out;
3518         }
3519
3520         err = mlx5e_safe_switch_channels(priv, &new_channels, mlx5e_modify_tirs_lro);
3521 out:
3522         mutex_unlock(&priv->state_lock);
3523         return err;
3524 }
3525
3526 static int set_feature_cvlan_filter(struct net_device *netdev, bool enable)
3527 {
3528         struct mlx5e_priv *priv = netdev_priv(netdev);
3529
3530         if (enable)
3531                 mlx5e_enable_cvlan_filter(priv);
3532         else
3533                 mlx5e_disable_cvlan_filter(priv);
3534
3535         return 0;
3536 }
3537
3538 #ifdef CONFIG_MLX5_ESWITCH
3539 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
3540 {
3541         struct mlx5e_priv *priv = netdev_priv(netdev);
3542