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