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 <linux/dim.h>
34 #include <net/tc_act/tc_gact.h>
35 #include <linux/mlx5/fs.h>
36 #include <net/vxlan.h>
37 #include <net/geneve.h>
38 #include <linux/bpf.h>
39 #include <linux/debugfs.h>
40 #include <linux/if_bridge.h>
41 #include <linux/filter.h>
42 #include <net/netdev_queues.h>
43 #include <net/page_pool/types.h>
44 #include <net/pkt_sched.h>
45 #include <net/xdp_sock_drv.h>
46 #include "eswitch.h"
47 #include "en.h"
48 #include "en/dim.h"
49 #include "en/txrx.h"
50 #include "en_tc.h"
51 #include "en_rep.h"
52 #include "en_accel/ipsec.h"
53 #include "en_accel/macsec.h"
54 #include "en_accel/en_accel.h"
55 #include "en_accel/ktls.h"
56 #include "lib/vxlan.h"
57 #include "lib/clock.h"
58 #include "en/port.h"
59 #include "en/xdp.h"
60 #include "lib/eq.h"
61 #include "en/monitor_stats.h"
62 #include "en/health.h"
63 #include "en/params.h"
64 #include "en/xsk/pool.h"
65 #include "en/xsk/setup.h"
66 #include "en/xsk/rx.h"
67 #include "en/xsk/tx.h"
68 #include "en/hv_vhca_stats.h"
69 #include "en/devlink.h"
70 #include "lib/mlx5.h"
71 #include "en/ptp.h"
72 #include "en/htb.h"
73 #include "qos.h"
74 #include "en/trap.h"
75 #include "lib/devcom.h"
76 #include "lib/sd.h"
77
mlx5e_hw_gro_supported(struct mlx5_core_dev * mdev)78 static bool mlx5e_hw_gro_supported(struct mlx5_core_dev *mdev)
79 {
80 if (!MLX5_CAP_GEN(mdev, shampo))
81 return false;
82
83 /* Our HW-GRO implementation relies on "KSM Mkey" for
84 * SHAMPO headers buffer mapping
85 */
86 if (!MLX5_CAP_GEN(mdev, fixed_buffer_size))
87 return false;
88
89 if (!MLX5_CAP_GEN_2(mdev, min_mkey_log_entity_size_fixed_buffer_valid))
90 return false;
91
92 if (MLX5_CAP_GEN_2(mdev, min_mkey_log_entity_size_fixed_buffer) >
93 MLX5E_SHAMPO_LOG_HEADER_ENTRY_SIZE)
94 return false;
95
96 return true;
97 }
98
mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev * mdev,u8 page_shift,enum mlx5e_mpwrq_umr_mode umr_mode)99 bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev, u8 page_shift,
100 enum mlx5e_mpwrq_umr_mode umr_mode)
101 {
102 u16 umr_wqebbs, max_wqebbs;
103 bool striding_rq_umr;
104
105 striding_rq_umr = MLX5_CAP_GEN(mdev, striding_rq) && MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
106 MLX5_CAP_ETH(mdev, reg_umr_sq);
107 if (!striding_rq_umr)
108 return false;
109
110 umr_wqebbs = mlx5e_mpwrq_umr_wqebbs(mdev, page_shift, umr_mode);
111 max_wqebbs = mlx5e_get_max_sq_aligned_wqebbs(mdev);
112 /* Sanity check; should never happen, because mlx5e_mpwrq_umr_wqebbs is
113 * calculated from mlx5e_get_max_sq_aligned_wqebbs.
114 */
115 if (WARN_ON(umr_wqebbs > max_wqebbs))
116 return false;
117
118 return true;
119 }
120
mlx5e_update_carrier(struct mlx5e_priv * priv)121 void mlx5e_update_carrier(struct mlx5e_priv *priv)
122 {
123 struct mlx5_core_dev *mdev = priv->mdev;
124 u8 port_state;
125 bool up;
126
127 port_state = mlx5_query_vport_state(mdev,
128 MLX5_VPORT_STATE_OP_MOD_VNIC_VPORT,
129 0);
130
131 up = port_state == VPORT_STATE_UP;
132 if (up == netif_carrier_ok(priv->netdev))
133 netif_carrier_event(priv->netdev);
134 if (up) {
135 netdev_info(priv->netdev, "Link up\n");
136 netif_carrier_on(priv->netdev);
137 } else {
138 netdev_info(priv->netdev, "Link down\n");
139 netif_carrier_off(priv->netdev);
140 }
141 }
142
mlx5e_update_carrier_work(struct work_struct * work)143 static void mlx5e_update_carrier_work(struct work_struct *work)
144 {
145 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
146 update_carrier_work);
147
148 mutex_lock(&priv->state_lock);
149 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
150 if (priv->profile->update_carrier)
151 priv->profile->update_carrier(priv);
152 mutex_unlock(&priv->state_lock);
153 }
154
mlx5e_update_stats_work(struct work_struct * work)155 static void mlx5e_update_stats_work(struct work_struct *work)
156 {
157 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
158 update_stats_work);
159
160 mutex_lock(&priv->state_lock);
161 priv->profile->update_stats(priv);
162 mutex_unlock(&priv->state_lock);
163 }
164
mlx5e_queue_update_stats(struct mlx5e_priv * priv)165 void mlx5e_queue_update_stats(struct mlx5e_priv *priv)
166 {
167 if (!priv->profile->update_stats)
168 return;
169
170 if (unlikely(test_bit(MLX5E_STATE_DESTROYING, &priv->state)))
171 return;
172
173 queue_work(priv->wq, &priv->update_stats_work);
174 }
175
async_event(struct notifier_block * nb,unsigned long event,void * data)176 static int async_event(struct notifier_block *nb, unsigned long event, void *data)
177 {
178 struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb);
179 struct mlx5_eqe *eqe = data;
180
181 if (event != MLX5_EVENT_TYPE_PORT_CHANGE)
182 return NOTIFY_DONE;
183
184 switch (eqe->sub_type) {
185 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
186 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
187 queue_work(priv->wq, &priv->update_carrier_work);
188 break;
189 default:
190 return NOTIFY_DONE;
191 }
192
193 return NOTIFY_OK;
194 }
195
mlx5e_enable_async_events(struct mlx5e_priv * priv)196 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
197 {
198 priv->events_nb.notifier_call = async_event;
199 mlx5_notifier_register(priv->mdev, &priv->events_nb);
200 }
201
mlx5e_disable_async_events(struct mlx5e_priv * priv)202 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
203 {
204 mlx5_notifier_unregister(priv->mdev, &priv->events_nb);
205 }
206
mlx5e_devcom_event_mpv(int event,void * my_data,void * event_data)207 static int mlx5e_devcom_event_mpv(int event, void *my_data, void *event_data)
208 {
209 struct mlx5e_priv *slave_priv = my_data;
210
211 switch (event) {
212 case MPV_DEVCOM_MASTER_UP:
213 mlx5_devcom_comp_set_ready(slave_priv->devcom, true);
214 break;
215 case MPV_DEVCOM_MASTER_DOWN:
216 /* no need for comp set ready false since we unregister after
217 * and it hurts cleanup flow.
218 */
219 break;
220 case MPV_DEVCOM_IPSEC_MASTER_UP:
221 case MPV_DEVCOM_IPSEC_MASTER_DOWN:
222 mlx5e_ipsec_handle_mpv_event(event, my_data, event_data);
223 break;
224 }
225
226 return 0;
227 }
228
mlx5e_devcom_init_mpv(struct mlx5e_priv * priv,u64 * data)229 static int mlx5e_devcom_init_mpv(struct mlx5e_priv *priv, u64 *data)
230 {
231 priv->devcom = mlx5_devcom_register_component(priv->mdev->priv.devc,
232 MLX5_DEVCOM_MPV,
233 *data,
234 mlx5e_devcom_event_mpv,
235 priv);
236 if (IS_ERR(priv->devcom))
237 return PTR_ERR(priv->devcom);
238
239 if (mlx5_core_is_mp_master(priv->mdev)) {
240 mlx5_devcom_send_event(priv->devcom, MPV_DEVCOM_MASTER_UP,
241 MPV_DEVCOM_MASTER_UP, priv);
242 mlx5e_ipsec_send_event(priv, MPV_DEVCOM_IPSEC_MASTER_UP);
243 }
244
245 return 0;
246 }
247
mlx5e_devcom_cleanup_mpv(struct mlx5e_priv * priv)248 static void mlx5e_devcom_cleanup_mpv(struct mlx5e_priv *priv)
249 {
250 if (IS_ERR_OR_NULL(priv->devcom))
251 return;
252
253 if (mlx5_core_is_mp_master(priv->mdev)) {
254 mlx5_devcom_send_event(priv->devcom, MPV_DEVCOM_MASTER_DOWN,
255 MPV_DEVCOM_MASTER_DOWN, priv);
256 mlx5e_ipsec_send_event(priv, MPV_DEVCOM_IPSEC_MASTER_DOWN);
257 }
258
259 mlx5_devcom_unregister_component(priv->devcom);
260 }
261
blocking_event(struct notifier_block * nb,unsigned long event,void * data)262 static int blocking_event(struct notifier_block *nb, unsigned long event, void *data)
263 {
264 struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, blocking_events_nb);
265 struct mlx5_devlink_trap_event_ctx *trap_event_ctx = data;
266 int err;
267
268 switch (event) {
269 case MLX5_DRIVER_EVENT_TYPE_TRAP:
270 err = mlx5e_handle_trap_event(priv, trap_event_ctx->trap);
271 if (err) {
272 trap_event_ctx->err = err;
273 return NOTIFY_BAD;
274 }
275 break;
276 case MLX5_DRIVER_EVENT_AFFILIATION_DONE:
277 if (mlx5e_devcom_init_mpv(priv, data))
278 return NOTIFY_BAD;
279 break;
280 case MLX5_DRIVER_EVENT_AFFILIATION_REMOVED:
281 mlx5e_devcom_cleanup_mpv(priv);
282 break;
283 default:
284 return NOTIFY_DONE;
285 }
286 return NOTIFY_OK;
287 }
288
mlx5e_enable_blocking_events(struct mlx5e_priv * priv)289 static void mlx5e_enable_blocking_events(struct mlx5e_priv *priv)
290 {
291 priv->blocking_events_nb.notifier_call = blocking_event;
292 mlx5_blocking_notifier_register(priv->mdev, &priv->blocking_events_nb);
293 }
294
mlx5e_disable_blocking_events(struct mlx5e_priv * priv)295 static void mlx5e_disable_blocking_events(struct mlx5e_priv *priv)
296 {
297 mlx5_blocking_notifier_unregister(priv->mdev, &priv->blocking_events_nb);
298 }
299
mlx5e_mpwrq_umr_octowords(u32 entries,enum mlx5e_mpwrq_umr_mode umr_mode)300 static u16 mlx5e_mpwrq_umr_octowords(u32 entries, enum mlx5e_mpwrq_umr_mode umr_mode)
301 {
302 u8 umr_entry_size = mlx5e_mpwrq_umr_entry_size(umr_mode);
303 u32 sz;
304
305 sz = ALIGN(entries * umr_entry_size, MLX5_UMR_FLEX_ALIGNMENT);
306
307 return sz / MLX5_OCTWORD;
308 }
309
mlx5e_build_umr_wqe(struct mlx5e_rq * rq,struct mlx5e_icosq * sq,struct mlx5e_umr_wqe * wqe)310 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq,
311 struct mlx5e_icosq *sq,
312 struct mlx5e_umr_wqe *wqe)
313 {
314 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
315 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
316 u16 octowords;
317 u8 ds_cnt;
318
319 ds_cnt = DIV_ROUND_UP(mlx5e_mpwrq_umr_wqe_sz(rq->mdev, rq->mpwqe.page_shift,
320 rq->mpwqe.umr_mode),
321 MLX5_SEND_WQE_DS);
322
323 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
324 ds_cnt);
325 cseg->umr_mkey = rq->mpwqe.umr_mkey_be;
326
327 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN | MLX5_UMR_INLINE;
328 octowords = mlx5e_mpwrq_umr_octowords(rq->mpwqe.pages_per_wqe, rq->mpwqe.umr_mode);
329 ucseg->xlt_octowords = cpu_to_be16(octowords);
330 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
331 }
332
mlx5e_rq_shampo_hd_alloc(struct mlx5e_rq * rq,int node)333 static int mlx5e_rq_shampo_hd_alloc(struct mlx5e_rq *rq, int node)
334 {
335 rq->mpwqe.shampo = kvzalloc_node(sizeof(*rq->mpwqe.shampo),
336 GFP_KERNEL, node);
337 if (!rq->mpwqe.shampo)
338 return -ENOMEM;
339 return 0;
340 }
341
mlx5e_rq_shampo_hd_free(struct mlx5e_rq * rq)342 static void mlx5e_rq_shampo_hd_free(struct mlx5e_rq *rq)
343 {
344 kvfree(rq->mpwqe.shampo);
345 }
346
mlx5e_rq_shampo_hd_info_alloc(struct mlx5e_rq * rq,int node)347 static int mlx5e_rq_shampo_hd_info_alloc(struct mlx5e_rq *rq, int node)
348 {
349 struct mlx5e_shampo_hd *shampo = rq->mpwqe.shampo;
350
351 shampo->bitmap = bitmap_zalloc_node(shampo->hd_per_wq, GFP_KERNEL,
352 node);
353 shampo->pages = kvzalloc_node(array_size(shampo->hd_per_wq,
354 sizeof(*shampo->pages)),
355 GFP_KERNEL, node);
356 if (!shampo->bitmap || !shampo->pages)
357 goto err_nomem;
358
359 return 0;
360
361 err_nomem:
362 kvfree(shampo->bitmap);
363 kvfree(shampo->pages);
364
365 return -ENOMEM;
366 }
367
mlx5e_rq_shampo_hd_info_free(struct mlx5e_rq * rq)368 static void mlx5e_rq_shampo_hd_info_free(struct mlx5e_rq *rq)
369 {
370 kvfree(rq->mpwqe.shampo->bitmap);
371 kvfree(rq->mpwqe.shampo->pages);
372 }
373
mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq * rq,int node)374 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq, int node)
375 {
376 int wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
377 size_t alloc_size;
378
379 alloc_size = array_size(wq_sz, struct_size(rq->mpwqe.info,
380 alloc_units.frag_pages,
381 rq->mpwqe.pages_per_wqe));
382
383 rq->mpwqe.info = kvzalloc_node(alloc_size, GFP_KERNEL, node);
384 if (!rq->mpwqe.info)
385 return -ENOMEM;
386
387 /* For deferred page release (release right before alloc), make sure
388 * that on first round release is not called.
389 */
390 for (int i = 0; i < wq_sz; i++) {
391 struct mlx5e_mpw_info *wi = mlx5e_get_mpw_info(rq, i);
392
393 bitmap_fill(wi->skip_release_bitmap, rq->mpwqe.pages_per_wqe);
394 }
395
396 mlx5e_build_umr_wqe(rq, rq->icosq, &rq->mpwqe.umr_wqe);
397
398 return 0;
399 }
400
401
mlx5e_mpwrq_access_mode(enum mlx5e_mpwrq_umr_mode umr_mode)402 static u8 mlx5e_mpwrq_access_mode(enum mlx5e_mpwrq_umr_mode umr_mode)
403 {
404 switch (umr_mode) {
405 case MLX5E_MPWRQ_UMR_MODE_ALIGNED:
406 return MLX5_MKC_ACCESS_MODE_MTT;
407 case MLX5E_MPWRQ_UMR_MODE_UNALIGNED:
408 return MLX5_MKC_ACCESS_MODE_KSM;
409 case MLX5E_MPWRQ_UMR_MODE_OVERSIZED:
410 return MLX5_MKC_ACCESS_MODE_KLMS;
411 case MLX5E_MPWRQ_UMR_MODE_TRIPLE:
412 return MLX5_MKC_ACCESS_MODE_KSM;
413 }
414 WARN_ONCE(1, "MPWRQ UMR mode %d is not known\n", umr_mode);
415 return 0;
416 }
417
mlx5e_create_umr_mkey(struct mlx5_core_dev * mdev,u32 npages,u8 page_shift,u32 * umr_mkey,dma_addr_t filler_addr,enum mlx5e_mpwrq_umr_mode umr_mode,u32 xsk_chunk_size)418 static int mlx5e_create_umr_mkey(struct mlx5_core_dev *mdev,
419 u32 npages, u8 page_shift, u32 *umr_mkey,
420 dma_addr_t filler_addr,
421 enum mlx5e_mpwrq_umr_mode umr_mode,
422 u32 xsk_chunk_size)
423 {
424 struct mlx5_mtt *mtt;
425 struct mlx5_ksm *ksm;
426 struct mlx5_klm *klm;
427 u32 octwords;
428 int inlen;
429 void *mkc;
430 u32 *in;
431 int err;
432 int i;
433
434 if ((umr_mode == MLX5E_MPWRQ_UMR_MODE_UNALIGNED ||
435 umr_mode == MLX5E_MPWRQ_UMR_MODE_TRIPLE) &&
436 !MLX5_CAP_GEN(mdev, fixed_buffer_size)) {
437 mlx5_core_warn(mdev, "Unaligned AF_XDP requires fixed_buffer_size capability\n");
438 return -EINVAL;
439 }
440
441 octwords = mlx5e_mpwrq_umr_octowords(npages, umr_mode);
442
443 inlen = MLX5_FLEXIBLE_INLEN(mdev, MLX5_ST_SZ_BYTES(create_mkey_in),
444 MLX5_OCTWORD, octwords);
445 if (inlen < 0)
446 return inlen;
447
448 in = kvzalloc(inlen, GFP_KERNEL);
449 if (!in)
450 return -ENOMEM;
451
452 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
453
454 MLX5_SET(mkc, mkc, free, 1);
455 MLX5_SET(mkc, mkc, umr_en, 1);
456 MLX5_SET(mkc, mkc, lw, 1);
457 MLX5_SET(mkc, mkc, lr, 1);
458 MLX5_SET(mkc, mkc, access_mode_1_0, mlx5e_mpwrq_access_mode(umr_mode));
459 mlx5e_mkey_set_relaxed_ordering(mdev, mkc);
460 MLX5_SET(mkc, mkc, qpn, 0xffffff);
461 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.hw_objs.pdn);
462 MLX5_SET64(mkc, mkc, len, npages << page_shift);
463 MLX5_SET(mkc, mkc, translations_octword_size, octwords);
464 if (umr_mode == MLX5E_MPWRQ_UMR_MODE_TRIPLE)
465 MLX5_SET(mkc, mkc, log_page_size, page_shift - 2);
466 else if (umr_mode != MLX5E_MPWRQ_UMR_MODE_OVERSIZED)
467 MLX5_SET(mkc, mkc, log_page_size, page_shift);
468 MLX5_SET(create_mkey_in, in, translations_octword_actual_size, octwords);
469
470 /* Initialize the mkey with all MTTs pointing to a default
471 * page (filler_addr). When the channels are activated, UMR
472 * WQEs will redirect the RX WQEs to the actual memory from
473 * the RQ's pool, while the gaps (wqe_overflow) remain mapped
474 * to the default page.
475 */
476 switch (umr_mode) {
477 case MLX5E_MPWRQ_UMR_MODE_OVERSIZED:
478 klm = MLX5_ADDR_OF(create_mkey_in, in, klm_pas_mtt);
479 for (i = 0; i < npages; i++) {
480 klm[i << 1] = (struct mlx5_klm) {
481 .va = cpu_to_be64(filler_addr),
482 .bcount = cpu_to_be32(xsk_chunk_size),
483 .key = cpu_to_be32(mdev->mlx5e_res.hw_objs.mkey),
484 };
485 klm[(i << 1) + 1] = (struct mlx5_klm) {
486 .va = cpu_to_be64(filler_addr),
487 .bcount = cpu_to_be32((1 << page_shift) - xsk_chunk_size),
488 .key = cpu_to_be32(mdev->mlx5e_res.hw_objs.mkey),
489 };
490 }
491 break;
492 case MLX5E_MPWRQ_UMR_MODE_UNALIGNED:
493 ksm = MLX5_ADDR_OF(create_mkey_in, in, klm_pas_mtt);
494 for (i = 0; i < npages; i++)
495 ksm[i] = (struct mlx5_ksm) {
496 .key = cpu_to_be32(mdev->mlx5e_res.hw_objs.mkey),
497 .va = cpu_to_be64(filler_addr),
498 };
499 break;
500 case MLX5E_MPWRQ_UMR_MODE_ALIGNED:
501 mtt = MLX5_ADDR_OF(create_mkey_in, in, klm_pas_mtt);
502 for (i = 0; i < npages; i++)
503 mtt[i] = (struct mlx5_mtt) {
504 .ptag = cpu_to_be64(filler_addr),
505 };
506 break;
507 case MLX5E_MPWRQ_UMR_MODE_TRIPLE:
508 ksm = MLX5_ADDR_OF(create_mkey_in, in, klm_pas_mtt);
509 for (i = 0; i < npages * 4; i++) {
510 ksm[i] = (struct mlx5_ksm) {
511 .key = cpu_to_be32(mdev->mlx5e_res.hw_objs.mkey),
512 .va = cpu_to_be64(filler_addr),
513 };
514 }
515 break;
516 }
517
518 err = mlx5_core_create_mkey(mdev, umr_mkey, in, inlen);
519
520 kvfree(in);
521 return err;
522 }
523
mlx5e_create_umr_ksm_mkey(struct mlx5_core_dev * mdev,u64 nentries,u8 log_entry_size,u32 * umr_mkey)524 static int mlx5e_create_umr_ksm_mkey(struct mlx5_core_dev *mdev,
525 u64 nentries, u8 log_entry_size,
526 u32 *umr_mkey)
527 {
528 int inlen;
529 void *mkc;
530 u32 *in;
531 int err;
532
533 inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
534
535 in = kvzalloc(inlen, GFP_KERNEL);
536 if (!in)
537 return -ENOMEM;
538
539 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
540
541 MLX5_SET(mkc, mkc, free, 1);
542 MLX5_SET(mkc, mkc, umr_en, 1);
543 MLX5_SET(mkc, mkc, lw, 1);
544 MLX5_SET(mkc, mkc, lr, 1);
545 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_KSM);
546 mlx5e_mkey_set_relaxed_ordering(mdev, mkc);
547 MLX5_SET(mkc, mkc, qpn, 0xffffff);
548 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.hw_objs.pdn);
549 MLX5_SET(mkc, mkc, translations_octword_size, nentries);
550 MLX5_SET(mkc, mkc, log_page_size, log_entry_size);
551 MLX5_SET64(mkc, mkc, len, nentries << log_entry_size);
552 err = mlx5_core_create_mkey(mdev, umr_mkey, in, inlen);
553
554 kvfree(in);
555 return err;
556 }
557
mlx5e_create_rq_umr_mkey(struct mlx5_core_dev * mdev,struct mlx5e_rq * rq)558 static int mlx5e_create_rq_umr_mkey(struct mlx5_core_dev *mdev, struct mlx5e_rq *rq)
559 {
560 u32 xsk_chunk_size = rq->xsk_pool ? rq->xsk_pool->chunk_size : 0;
561 u32 wq_size = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
562 u32 num_entries, max_num_entries;
563 u32 umr_mkey;
564 int err;
565
566 max_num_entries = mlx5e_mpwrq_max_num_entries(mdev, rq->mpwqe.umr_mode);
567
568 /* Shouldn't overflow, the result is at most MLX5E_MAX_RQ_NUM_MTTS. */
569 if (WARN_ON_ONCE(check_mul_overflow(wq_size, (u32)rq->mpwqe.mtts_per_wqe,
570 &num_entries) ||
571 num_entries > max_num_entries))
572 mlx5_core_err(mdev, "%s: multiplication overflow: %u * %u > %u\n",
573 __func__, wq_size, rq->mpwqe.mtts_per_wqe,
574 max_num_entries);
575
576 err = mlx5e_create_umr_mkey(mdev, num_entries, rq->mpwqe.page_shift,
577 &umr_mkey, rq->wqe_overflow.addr,
578 rq->mpwqe.umr_mode, xsk_chunk_size);
579 rq->mpwqe.umr_mkey_be = cpu_to_be32(umr_mkey);
580 return err;
581 }
582
mlx5e_create_rq_hd_umr_mkey(struct mlx5_core_dev * mdev,struct mlx5e_rq * rq)583 static int mlx5e_create_rq_hd_umr_mkey(struct mlx5_core_dev *mdev,
584 struct mlx5e_rq *rq)
585 {
586 u32 max_ksm_size = BIT(MLX5_CAP_GEN(mdev, log_max_klm_list_size));
587
588 if (max_ksm_size < rq->mpwqe.shampo->hd_per_wq) {
589 mlx5_core_err(mdev, "max ksm list size 0x%x is smaller than shampo header buffer list size 0x%x\n",
590 max_ksm_size, rq->mpwqe.shampo->hd_per_wq);
591 return -EINVAL;
592 }
593
594 return mlx5e_create_umr_ksm_mkey(mdev, rq->mpwqe.shampo->hd_per_wq,
595 MLX5E_SHAMPO_LOG_HEADER_ENTRY_SIZE,
596 &rq->mpwqe.shampo->mkey);
597 }
598
mlx5e_init_frags_partition(struct mlx5e_rq * rq)599 static void mlx5e_init_frags_partition(struct mlx5e_rq *rq)
600 {
601 struct mlx5e_wqe_frag_info next_frag = {};
602 struct mlx5e_wqe_frag_info *prev = NULL;
603 int i;
604
605 WARN_ON(rq->xsk_pool);
606
607 next_frag.frag_page = &rq->wqe.alloc_units->frag_pages[0];
608
609 /* Skip first release due to deferred release. */
610 next_frag.flags = BIT(MLX5E_WQE_FRAG_SKIP_RELEASE);
611
612 for (i = 0; i < mlx5_wq_cyc_get_size(&rq->wqe.wq); i++) {
613 struct mlx5e_rq_frag_info *frag_info = &rq->wqe.info.arr[0];
614 struct mlx5e_wqe_frag_info *frag =
615 &rq->wqe.frags[i << rq->wqe.info.log_num_frags];
616 int f;
617
618 for (f = 0; f < rq->wqe.info.num_frags; f++, frag++) {
619 if (next_frag.offset + frag_info[f].frag_stride > PAGE_SIZE) {
620 /* Pages are assigned at runtime. */
621 next_frag.frag_page++;
622 next_frag.offset = 0;
623 if (prev)
624 prev->flags |= BIT(MLX5E_WQE_FRAG_LAST_IN_PAGE);
625 }
626 *frag = next_frag;
627
628 /* prepare next */
629 next_frag.offset += frag_info[f].frag_stride;
630 prev = frag;
631 }
632 }
633
634 if (prev)
635 prev->flags |= BIT(MLX5E_WQE_FRAG_LAST_IN_PAGE);
636 }
637
mlx5e_init_xsk_buffs(struct mlx5e_rq * rq)638 static void mlx5e_init_xsk_buffs(struct mlx5e_rq *rq)
639 {
640 int i;
641
642 /* Assumptions used by XSK batched allocator. */
643 WARN_ON(rq->wqe.info.num_frags != 1);
644 WARN_ON(rq->wqe.info.log_num_frags != 0);
645 WARN_ON(rq->wqe.info.arr[0].frag_stride != PAGE_SIZE);
646
647 /* Considering the above assumptions a fragment maps to a single
648 * xsk_buff.
649 */
650 for (i = 0; i < mlx5_wq_cyc_get_size(&rq->wqe.wq); i++) {
651 rq->wqe.frags[i].xskp = &rq->wqe.alloc_units->xsk_buffs[i];
652
653 /* Skip first release due to deferred release as WQES are
654 * not allocated yet.
655 */
656 rq->wqe.frags[i].flags |= BIT(MLX5E_WQE_FRAG_SKIP_RELEASE);
657 }
658 }
659
mlx5e_init_wqe_alloc_info(struct mlx5e_rq * rq,int node)660 static int mlx5e_init_wqe_alloc_info(struct mlx5e_rq *rq, int node)
661 {
662 int wq_sz = mlx5_wq_cyc_get_size(&rq->wqe.wq);
663 int len = wq_sz << rq->wqe.info.log_num_frags;
664 struct mlx5e_wqe_frag_info *frags;
665 union mlx5e_alloc_units *aus;
666 int aus_sz;
667
668 if (rq->xsk_pool)
669 aus_sz = sizeof(*aus->xsk_buffs);
670 else
671 aus_sz = sizeof(*aus->frag_pages);
672
673 aus = kvzalloc_node(array_size(len, aus_sz), GFP_KERNEL, node);
674 if (!aus)
675 return -ENOMEM;
676
677 frags = kvzalloc_node(array_size(len, sizeof(*frags)), GFP_KERNEL, node);
678 if (!frags) {
679 kvfree(aus);
680 return -ENOMEM;
681 }
682
683 rq->wqe.alloc_units = aus;
684 rq->wqe.frags = frags;
685
686 if (rq->xsk_pool)
687 mlx5e_init_xsk_buffs(rq);
688 else
689 mlx5e_init_frags_partition(rq);
690
691 return 0;
692 }
693
mlx5e_free_wqe_alloc_info(struct mlx5e_rq * rq)694 static void mlx5e_free_wqe_alloc_info(struct mlx5e_rq *rq)
695 {
696 kvfree(rq->wqe.frags);
697 kvfree(rq->wqe.alloc_units);
698 }
699
mlx5e_rq_err_cqe_work(struct work_struct * recover_work)700 static void mlx5e_rq_err_cqe_work(struct work_struct *recover_work)
701 {
702 struct mlx5e_rq *rq = container_of(recover_work, struct mlx5e_rq, recover_work);
703
704 mlx5e_reporter_rq_cqe_err(rq);
705 }
706
mlx5e_alloc_mpwqe_rq_drop_page(struct mlx5e_rq * rq)707 static int mlx5e_alloc_mpwqe_rq_drop_page(struct mlx5e_rq *rq)
708 {
709 rq->wqe_overflow.page = alloc_page(GFP_KERNEL);
710 if (!rq->wqe_overflow.page)
711 return -ENOMEM;
712
713 rq->wqe_overflow.addr = dma_map_page(rq->pdev, rq->wqe_overflow.page, 0,
714 PAGE_SIZE, rq->buff.map_dir);
715 if (dma_mapping_error(rq->pdev, rq->wqe_overflow.addr)) {
716 __free_page(rq->wqe_overflow.page);
717 return -ENOMEM;
718 }
719 return 0;
720 }
721
mlx5e_free_mpwqe_rq_drop_page(struct mlx5e_rq * rq)722 static void mlx5e_free_mpwqe_rq_drop_page(struct mlx5e_rq *rq)
723 {
724 dma_unmap_page(rq->pdev, rq->wqe_overflow.addr, PAGE_SIZE,
725 rq->buff.map_dir);
726 __free_page(rq->wqe_overflow.page);
727 }
728
mlx5e_init_rxq_rq(struct mlx5e_channel * c,struct mlx5e_params * params,u32 xdp_frag_size,struct mlx5e_rq * rq)729 static int mlx5e_init_rxq_rq(struct mlx5e_channel *c, struct mlx5e_params *params,
730 u32 xdp_frag_size, struct mlx5e_rq *rq)
731 {
732 struct mlx5_core_dev *mdev = c->mdev;
733 int err;
734
735 rq->wq_type = params->rq_wq_type;
736 rq->pdev = c->pdev;
737 rq->netdev = c->netdev;
738 rq->priv = c->priv;
739 rq->tstamp = c->tstamp;
740 rq->clock = &mdev->clock;
741 rq->icosq = &c->icosq;
742 rq->ix = c->ix;
743 rq->channel = c;
744 rq->mdev = mdev;
745 rq->hw_mtu =
746 MLX5E_SW2HW_MTU(params, params->sw_mtu) - ETH_FCS_LEN * !params->scatter_fcs_en;
747 rq->xdpsq = &c->rq_xdpsq;
748 rq->stats = &c->priv->channel_stats[c->ix]->rq;
749 rq->ptp_cyc2time = mlx5_rq_ts_translator(mdev);
750 err = mlx5e_rq_set_handlers(rq, params, NULL);
751 if (err)
752 return err;
753
754 return __xdp_rxq_info_reg(&rq->xdp_rxq, rq->netdev, rq->ix, c->napi.napi_id,
755 xdp_frag_size);
756 }
757
mlx5_rq_shampo_alloc(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_rq_param * rqp,struct mlx5e_rq * rq,u32 * pool_size,int node)758 static int mlx5_rq_shampo_alloc(struct mlx5_core_dev *mdev,
759 struct mlx5e_params *params,
760 struct mlx5e_rq_param *rqp,
761 struct mlx5e_rq *rq,
762 u32 *pool_size,
763 int node)
764 {
765 int err;
766
767 if (!test_bit(MLX5E_RQ_STATE_SHAMPO, &rq->state))
768 return 0;
769 err = mlx5e_rq_shampo_hd_alloc(rq, node);
770 if (err)
771 goto out;
772 rq->mpwqe.shampo->hd_per_wq =
773 mlx5e_shampo_hd_per_wq(mdev, params, rqp);
774 err = mlx5e_create_rq_hd_umr_mkey(mdev, rq);
775 if (err)
776 goto err_shampo_hd;
777 err = mlx5e_rq_shampo_hd_info_alloc(rq, node);
778 if (err)
779 goto err_shampo_info;
780 rq->hw_gro_data = kvzalloc_node(sizeof(*rq->hw_gro_data), GFP_KERNEL, node);
781 if (!rq->hw_gro_data) {
782 err = -ENOMEM;
783 goto err_hw_gro_data;
784 }
785 rq->mpwqe.shampo->key =
786 cpu_to_be32(rq->mpwqe.shampo->mkey);
787 rq->mpwqe.shampo->hd_per_wqe =
788 mlx5e_shampo_hd_per_wqe(mdev, params, rqp);
789 rq->mpwqe.shampo->pages_per_wq =
790 rq->mpwqe.shampo->hd_per_wq / MLX5E_SHAMPO_WQ_HEADER_PER_PAGE;
791 *pool_size += rq->mpwqe.shampo->pages_per_wq;
792 return 0;
793
794 err_hw_gro_data:
795 mlx5e_rq_shampo_hd_info_free(rq);
796 err_shampo_info:
797 mlx5_core_destroy_mkey(mdev, rq->mpwqe.shampo->mkey);
798 err_shampo_hd:
799 mlx5e_rq_shampo_hd_free(rq);
800 out:
801 return err;
802 }
803
mlx5e_rq_free_shampo(struct mlx5e_rq * rq)804 static void mlx5e_rq_free_shampo(struct mlx5e_rq *rq)
805 {
806 if (!test_bit(MLX5E_RQ_STATE_SHAMPO, &rq->state))
807 return;
808
809 kvfree(rq->hw_gro_data);
810 mlx5e_rq_shampo_hd_info_free(rq);
811 mlx5_core_destroy_mkey(rq->mdev, rq->mpwqe.shampo->mkey);
812 mlx5e_rq_shampo_hd_free(rq);
813 }
814
mlx5e_alloc_rq(struct mlx5e_params * params,struct mlx5e_xsk_param * xsk,struct mlx5e_rq_param * rqp,int node,struct mlx5e_rq * rq)815 static int mlx5e_alloc_rq(struct mlx5e_params *params,
816 struct mlx5e_xsk_param *xsk,
817 struct mlx5e_rq_param *rqp,
818 int node, struct mlx5e_rq *rq)
819 {
820 struct mlx5_core_dev *mdev = rq->mdev;
821 void *rqc = rqp->rqc;
822 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
823 u32 pool_size;
824 int wq_sz;
825 int err;
826 int i;
827
828 rqp->wq.db_numa_node = node;
829 INIT_WORK(&rq->recover_work, mlx5e_rq_err_cqe_work);
830
831 if (params->xdp_prog)
832 bpf_prog_inc(params->xdp_prog);
833 RCU_INIT_POINTER(rq->xdp_prog, params->xdp_prog);
834
835 rq->buff.map_dir = params->xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
836 rq->buff.headroom = mlx5e_get_rq_headroom(mdev, params, xsk);
837 pool_size = 1 << params->log_rq_mtu_frames;
838
839 rq->mkey_be = cpu_to_be32(mdev->mlx5e_res.hw_objs.mkey);
840
841 switch (rq->wq_type) {
842 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
843 err = mlx5_wq_ll_create(mdev, &rqp->wq, rqc_wq, &rq->mpwqe.wq,
844 &rq->wq_ctrl);
845 if (err)
846 goto err_rq_xdp_prog;
847
848 err = mlx5e_alloc_mpwqe_rq_drop_page(rq);
849 if (err)
850 goto err_rq_wq_destroy;
851
852 rq->mpwqe.wq.db = &rq->mpwqe.wq.db[MLX5_RCV_DBR];
853
854 wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
855
856 rq->mpwqe.page_shift = mlx5e_mpwrq_page_shift(mdev, xsk);
857 rq->mpwqe.umr_mode = mlx5e_mpwrq_umr_mode(mdev, xsk);
858 rq->mpwqe.pages_per_wqe =
859 mlx5e_mpwrq_pages_per_wqe(mdev, rq->mpwqe.page_shift,
860 rq->mpwqe.umr_mode);
861 rq->mpwqe.umr_wqebbs =
862 mlx5e_mpwrq_umr_wqebbs(mdev, rq->mpwqe.page_shift,
863 rq->mpwqe.umr_mode);
864 rq->mpwqe.mtts_per_wqe =
865 mlx5e_mpwrq_mtts_per_wqe(mdev, rq->mpwqe.page_shift,
866 rq->mpwqe.umr_mode);
867
868 pool_size = rq->mpwqe.pages_per_wqe <<
869 mlx5e_mpwqe_get_log_rq_size(mdev, params, xsk);
870
871 if (!mlx5e_rx_mpwqe_is_linear_skb(mdev, params, xsk) && params->xdp_prog)
872 pool_size *= 2; /* additional page per packet for the linear part */
873
874 rq->mpwqe.log_stride_sz = mlx5e_mpwqe_get_log_stride_size(mdev, params, xsk);
875 rq->mpwqe.num_strides =
876 BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk));
877 rq->mpwqe.min_wqe_bulk = mlx5e_mpwqe_get_min_wqe_bulk(wq_sz);
878
879 rq->buff.frame0_sz = (1 << rq->mpwqe.log_stride_sz);
880
881 err = mlx5e_create_rq_umr_mkey(mdev, rq);
882 if (err)
883 goto err_rq_drop_page;
884
885 err = mlx5e_rq_alloc_mpwqe_info(rq, node);
886 if (err)
887 goto err_rq_mkey;
888
889 err = mlx5_rq_shampo_alloc(mdev, params, rqp, rq, &pool_size, node);
890 if (err)
891 goto err_free_mpwqe_info;
892
893 break;
894 default: /* MLX5_WQ_TYPE_CYCLIC */
895 err = mlx5_wq_cyc_create(mdev, &rqp->wq, rqc_wq, &rq->wqe.wq,
896 &rq->wq_ctrl);
897 if (err)
898 goto err_rq_xdp_prog;
899
900 rq->wqe.wq.db = &rq->wqe.wq.db[MLX5_RCV_DBR];
901
902 wq_sz = mlx5_wq_cyc_get_size(&rq->wqe.wq);
903
904 rq->wqe.info = rqp->frags_info;
905 rq->buff.frame0_sz = rq->wqe.info.arr[0].frag_stride;
906
907 err = mlx5e_init_wqe_alloc_info(rq, node);
908 if (err)
909 goto err_rq_wq_destroy;
910 }
911
912 if (xsk) {
913 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
914 MEM_TYPE_XSK_BUFF_POOL, NULL);
915 xsk_pool_set_rxq_info(rq->xsk_pool, &rq->xdp_rxq);
916 } else {
917 /* Create a page_pool and register it with rxq */
918 struct page_pool_params pp_params = { 0 };
919
920 pp_params.order = 0;
921 pp_params.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
922 pp_params.pool_size = pool_size;
923 pp_params.nid = node;
924 pp_params.dev = rq->pdev;
925 pp_params.napi = rq->cq.napi;
926 pp_params.netdev = rq->netdev;
927 pp_params.dma_dir = rq->buff.map_dir;
928 pp_params.max_len = PAGE_SIZE;
929
930 /* page_pool can be used even when there is no rq->xdp_prog,
931 * given page_pool does not handle DMA mapping there is no
932 * required state to clear. And page_pool gracefully handle
933 * elevated refcnt.
934 */
935 rq->page_pool = page_pool_create(&pp_params);
936 if (IS_ERR(rq->page_pool)) {
937 err = PTR_ERR(rq->page_pool);
938 rq->page_pool = NULL;
939 goto err_free_by_rq_type;
940 }
941 if (xdp_rxq_info_is_reg(&rq->xdp_rxq))
942 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
943 MEM_TYPE_PAGE_POOL, rq->page_pool);
944 }
945 if (err)
946 goto err_destroy_page_pool;
947
948 for (i = 0; i < wq_sz; i++) {
949 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
950 struct mlx5e_rx_wqe_ll *wqe =
951 mlx5_wq_ll_get_wqe(&rq->mpwqe.wq, i);
952 u32 byte_count =
953 rq->mpwqe.num_strides << rq->mpwqe.log_stride_sz;
954 u64 dma_offset = mul_u32_u32(i, rq->mpwqe.mtts_per_wqe) <<
955 rq->mpwqe.page_shift;
956 u16 headroom = test_bit(MLX5E_RQ_STATE_SHAMPO, &rq->state) ?
957 0 : rq->buff.headroom;
958
959 wqe->data[0].addr = cpu_to_be64(dma_offset + headroom);
960 wqe->data[0].byte_count = cpu_to_be32(byte_count);
961 wqe->data[0].lkey = rq->mpwqe.umr_mkey_be;
962 } else {
963 struct mlx5e_rx_wqe_cyc *wqe =
964 mlx5_wq_cyc_get_wqe(&rq->wqe.wq, i);
965 int f;
966
967 for (f = 0; f < rq->wqe.info.num_frags; f++) {
968 u32 frag_size = rq->wqe.info.arr[f].frag_size |
969 MLX5_HW_START_PADDING;
970
971 wqe->data[f].byte_count = cpu_to_be32(frag_size);
972 wqe->data[f].lkey = rq->mkey_be;
973 }
974 /* check if num_frags is not a pow of two */
975 if (rq->wqe.info.num_frags < (1 << rq->wqe.info.log_num_frags)) {
976 wqe->data[f].byte_count = 0;
977 wqe->data[f].lkey = params->terminate_lkey_be;
978 wqe->data[f].addr = 0;
979 }
980 }
981 }
982
983 return 0;
984
985 err_destroy_page_pool:
986 page_pool_destroy(rq->page_pool);
987 err_free_by_rq_type:
988 switch (rq->wq_type) {
989 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
990 mlx5e_rq_free_shampo(rq);
991 err_free_mpwqe_info:
992 kvfree(rq->mpwqe.info);
993 err_rq_mkey:
994 mlx5_core_destroy_mkey(mdev, be32_to_cpu(rq->mpwqe.umr_mkey_be));
995 err_rq_drop_page:
996 mlx5e_free_mpwqe_rq_drop_page(rq);
997 break;
998 default: /* MLX5_WQ_TYPE_CYCLIC */
999 mlx5e_free_wqe_alloc_info(rq);
1000 }
1001 err_rq_wq_destroy:
1002 mlx5_wq_destroy(&rq->wq_ctrl);
1003 err_rq_xdp_prog:
1004 if (params->xdp_prog)
1005 bpf_prog_put(params->xdp_prog);
1006
1007 return err;
1008 }
1009
mlx5e_free_rq(struct mlx5e_rq * rq)1010 static void mlx5e_free_rq(struct mlx5e_rq *rq)
1011 {
1012 kvfree(rq->dim);
1013 page_pool_destroy(rq->page_pool);
1014
1015 switch (rq->wq_type) {
1016 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1017 mlx5e_rq_free_shampo(rq);
1018 kvfree(rq->mpwqe.info);
1019 mlx5_core_destroy_mkey(rq->mdev, be32_to_cpu(rq->mpwqe.umr_mkey_be));
1020 mlx5e_free_mpwqe_rq_drop_page(rq);
1021 break;
1022 default: /* MLX5_WQ_TYPE_CYCLIC */
1023 mlx5e_free_wqe_alloc_info(rq);
1024 }
1025
1026 mlx5_wq_destroy(&rq->wq_ctrl);
1027
1028 if (xdp_rxq_info_is_reg(&rq->xdp_rxq)) {
1029 struct bpf_prog *old_prog;
1030
1031 old_prog = rcu_dereference_protected(rq->xdp_prog,
1032 lockdep_is_held(&rq->priv->state_lock));
1033 if (old_prog)
1034 bpf_prog_put(old_prog);
1035 }
1036 xdp_rxq_info_unreg(&rq->xdp_rxq);
1037 }
1038
mlx5e_create_rq(struct mlx5e_rq * rq,struct mlx5e_rq_param * param,u16 q_counter)1039 int mlx5e_create_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param, u16 q_counter)
1040 {
1041 struct mlx5_core_dev *mdev = rq->mdev;
1042 u8 ts_format;
1043 void *in;
1044 void *rqc;
1045 void *wq;
1046 int inlen;
1047 int err;
1048
1049 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
1050 sizeof(u64) * rq->wq_ctrl.buf.npages;
1051 in = kvzalloc(inlen, GFP_KERNEL);
1052 if (!in)
1053 return -ENOMEM;
1054
1055 ts_format = mlx5_is_real_time_rq(mdev) ?
1056 MLX5_TIMESTAMP_FORMAT_REAL_TIME :
1057 MLX5_TIMESTAMP_FORMAT_FREE_RUNNING;
1058 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
1059 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1060
1061 memcpy(rqc, param->rqc, sizeof(param->rqc));
1062
1063 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
1064 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1065 MLX5_SET(rqc, rqc, ts_format, ts_format);
1066 MLX5_SET(rqc, rqc, counter_set_id, q_counter);
1067 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
1068 MLX5_ADAPTER_PAGE_SHIFT);
1069 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
1070
1071 if (test_bit(MLX5E_RQ_STATE_SHAMPO, &rq->state)) {
1072 MLX5_SET(wq, wq, log_headers_buffer_entry_num,
1073 order_base_2(rq->mpwqe.shampo->hd_per_wq));
1074 MLX5_SET(wq, wq, headers_mkey, rq->mpwqe.shampo->mkey);
1075 }
1076
1077 mlx5_fill_page_frag_array(&rq->wq_ctrl.buf,
1078 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
1079
1080 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
1081
1082 kvfree(in);
1083
1084 return err;
1085 }
1086
mlx5e_modify_rq_state(struct mlx5e_rq * rq,int curr_state,int next_state)1087 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state, int next_state)
1088 {
1089 struct mlx5_core_dev *mdev = rq->mdev;
1090
1091 void *in;
1092 void *rqc;
1093 int inlen;
1094 int err;
1095
1096 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
1097 in = kvzalloc(inlen, GFP_KERNEL);
1098 if (!in)
1099 return -ENOMEM;
1100
1101 if (curr_state == MLX5_RQC_STATE_RST && next_state == MLX5_RQC_STATE_RDY)
1102 mlx5e_rqwq_reset(rq);
1103
1104 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
1105
1106 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
1107 MLX5_SET(rqc, rqc, state, next_state);
1108
1109 err = mlx5_core_modify_rq(mdev, rq->rqn, in);
1110
1111 kvfree(in);
1112
1113 return err;
1114 }
1115
mlx5e_flush_rq_cq(struct mlx5e_rq * rq)1116 static void mlx5e_flush_rq_cq(struct mlx5e_rq *rq)
1117 {
1118 struct mlx5_cqwq *cqwq = &rq->cq.wq;
1119 struct mlx5_cqe64 *cqe;
1120
1121 if (test_bit(MLX5E_RQ_STATE_MINI_CQE_ENHANCED, &rq->state)) {
1122 while ((cqe = mlx5_cqwq_get_cqe_enhanced_comp(cqwq)))
1123 mlx5_cqwq_pop(cqwq);
1124 } else {
1125 while ((cqe = mlx5_cqwq_get_cqe(cqwq)))
1126 mlx5_cqwq_pop(cqwq);
1127 }
1128
1129 mlx5_cqwq_update_db_record(cqwq);
1130 }
1131
mlx5e_flush_rq(struct mlx5e_rq * rq,int curr_state)1132 int mlx5e_flush_rq(struct mlx5e_rq *rq, int curr_state)
1133 {
1134 struct net_device *dev = rq->netdev;
1135 int err;
1136
1137 err = mlx5e_modify_rq_state(rq, curr_state, MLX5_RQC_STATE_RST);
1138 if (err) {
1139 netdev_err(dev, "Failed to move rq 0x%x to reset\n", rq->rqn);
1140 return err;
1141 }
1142
1143 mlx5e_free_rx_descs(rq);
1144 mlx5e_flush_rq_cq(rq);
1145
1146 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
1147 if (err) {
1148 netdev_err(dev, "Failed to move rq 0x%x to ready\n", rq->rqn);
1149 return err;
1150 }
1151
1152 return 0;
1153 }
1154
mlx5e_modify_rq_vsd(struct mlx5e_rq * rq,bool vsd)1155 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
1156 {
1157 struct mlx5_core_dev *mdev = rq->mdev;
1158 void *in;
1159 void *rqc;
1160 int inlen;
1161 int err;
1162
1163 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
1164 in = kvzalloc(inlen, GFP_KERNEL);
1165 if (!in)
1166 return -ENOMEM;
1167
1168 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
1169
1170 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
1171 MLX5_SET64(modify_rq_in, in, modify_bitmask,
1172 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
1173 MLX5_SET(rqc, rqc, vsd, vsd);
1174 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
1175
1176 err = mlx5_core_modify_rq(mdev, rq->rqn, in);
1177
1178 kvfree(in);
1179
1180 return err;
1181 }
1182
mlx5e_destroy_rq(struct mlx5e_rq * rq)1183 void mlx5e_destroy_rq(struct mlx5e_rq *rq)
1184 {
1185 mlx5_core_destroy_rq(rq->mdev, rq->rqn);
1186 }
1187
mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq * rq,int wait_time)1188 int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq, int wait_time)
1189 {
1190 unsigned long exp_time = jiffies + msecs_to_jiffies(wait_time);
1191
1192 u16 min_wqes = mlx5_min_rx_wqes(rq->wq_type, mlx5e_rqwq_get_size(rq));
1193
1194 do {
1195 if (mlx5e_rqwq_get_cur_sz(rq) >= min_wqes)
1196 return 0;
1197
1198 msleep(20);
1199 } while (time_before(jiffies, exp_time));
1200
1201 netdev_warn(rq->netdev, "Failed to get min RX wqes on Channel[%d] RQN[0x%x] wq cur_sz(%d) min_rx_wqes(%d)\n",
1202 rq->ix, rq->rqn, mlx5e_rqwq_get_cur_sz(rq), min_wqes);
1203
1204 mlx5e_reporter_rx_timeout(rq);
1205 return -ETIMEDOUT;
1206 }
1207
mlx5e_free_rx_missing_descs(struct mlx5e_rq * rq)1208 void mlx5e_free_rx_missing_descs(struct mlx5e_rq *rq)
1209 {
1210 struct mlx5_wq_ll *wq;
1211 u16 head;
1212 int i;
1213
1214 if (rq->wq_type != MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
1215 return;
1216
1217 wq = &rq->mpwqe.wq;
1218 head = wq->head;
1219
1220 /* Release WQEs that are in missing state: they have been
1221 * popped from the list after completion but were not freed
1222 * due to deferred release.
1223 * Also free the linked-list reserved entry, hence the "+ 1".
1224 */
1225 for (i = 0; i < mlx5_wq_ll_missing(wq) + 1; i++) {
1226 rq->dealloc_wqe(rq, head);
1227 head = mlx5_wq_ll_get_wqe_next_ix(wq, head);
1228 }
1229
1230 rq->mpwqe.actual_wq_head = wq->head;
1231 rq->mpwqe.umr_in_progress = 0;
1232 rq->mpwqe.umr_completed = 0;
1233
1234 if (test_bit(MLX5E_RQ_STATE_SHAMPO, &rq->state)) {
1235 struct mlx5e_shampo_hd *shampo = rq->mpwqe.shampo;
1236 u16 len;
1237
1238 len = (shampo->pi - shampo->ci) & shampo->hd_per_wq;
1239 mlx5e_shampo_fill_umr(rq, len);
1240 }
1241 }
1242
mlx5e_free_rx_descs(struct mlx5e_rq * rq)1243 void mlx5e_free_rx_descs(struct mlx5e_rq *rq)
1244 {
1245 __be16 wqe_ix_be;
1246 u16 wqe_ix;
1247
1248 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
1249 struct mlx5_wq_ll *wq = &rq->mpwqe.wq;
1250
1251 mlx5e_free_rx_missing_descs(rq);
1252
1253 while (!mlx5_wq_ll_is_empty(wq)) {
1254 struct mlx5e_rx_wqe_ll *wqe;
1255
1256 wqe_ix_be = *wq->tail_next;
1257 wqe_ix = be16_to_cpu(wqe_ix_be);
1258 wqe = mlx5_wq_ll_get_wqe(wq, wqe_ix);
1259 rq->dealloc_wqe(rq, wqe_ix);
1260 mlx5_wq_ll_pop(wq, wqe_ix_be,
1261 &wqe->next.next_wqe_index);
1262 }
1263
1264 if (test_bit(MLX5E_RQ_STATE_SHAMPO, &rq->state))
1265 mlx5e_shampo_dealloc_hd(rq);
1266 } else {
1267 struct mlx5_wq_cyc *wq = &rq->wqe.wq;
1268 u16 missing = mlx5_wq_cyc_missing(wq);
1269 u16 head = mlx5_wq_cyc_get_head(wq);
1270
1271 while (!mlx5_wq_cyc_is_empty(wq)) {
1272 wqe_ix = mlx5_wq_cyc_get_tail(wq);
1273 rq->dealloc_wqe(rq, wqe_ix);
1274 mlx5_wq_cyc_pop(wq);
1275 }
1276 /* Missing slots might also contain unreleased pages due to
1277 * deferred release.
1278 */
1279 while (missing--) {
1280 wqe_ix = mlx5_wq_cyc_ctr2ix(wq, head++);
1281 rq->dealloc_wqe(rq, wqe_ix);
1282 }
1283 }
1284
1285 }
1286
mlx5e_open_rq(struct mlx5e_params * params,struct mlx5e_rq_param * param,struct mlx5e_xsk_param * xsk,int node,u16 q_counter,struct mlx5e_rq * rq)1287 int mlx5e_open_rq(struct mlx5e_params *params, struct mlx5e_rq_param *param,
1288 struct mlx5e_xsk_param *xsk, int node, u16 q_counter,
1289 struct mlx5e_rq *rq)
1290 {
1291 struct mlx5_core_dev *mdev = rq->mdev;
1292 int err;
1293
1294 if (params->packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO)
1295 __set_bit(MLX5E_RQ_STATE_SHAMPO, &rq->state);
1296
1297 err = mlx5e_alloc_rq(params, xsk, param, node, rq);
1298 if (err)
1299 return err;
1300
1301 err = mlx5e_create_rq(rq, param, q_counter);
1302 if (err)
1303 goto err_free_rq;
1304
1305 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
1306 if (err)
1307 goto err_destroy_rq;
1308
1309 if (MLX5_CAP_ETH(mdev, cqe_checksum_full))
1310 __set_bit(MLX5E_RQ_STATE_CSUM_FULL, &rq->state);
1311
1312 if (rq->channel && !params->rx_dim_enabled) {
1313 rq->channel->rx_cq_moder = params->rx_cq_moderation;
1314 } else if (rq->channel) {
1315 u8 cq_period_mode;
1316
1317 cq_period_mode = params->rx_moder_use_cqe_mode ?
1318 DIM_CQ_PERIOD_MODE_START_FROM_CQE :
1319 DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1320 mlx5e_reset_rx_moderation(&rq->channel->rx_cq_moder, cq_period_mode,
1321 params->rx_dim_enabled);
1322
1323 err = mlx5e_dim_rx_change(rq, params->rx_dim_enabled);
1324 if (err)
1325 goto err_destroy_rq;
1326 }
1327
1328 /* We disable csum_complete when XDP is enabled since
1329 * XDP programs might manipulate packets which will render
1330 * skb->checksum incorrect.
1331 */
1332 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_NO_CSUM_COMPLETE) || params->xdp_prog)
1333 __set_bit(MLX5E_RQ_STATE_NO_CSUM_COMPLETE, &rq->state);
1334
1335 /* For CQE compression on striding RQ, use stride index provided by
1336 * HW if capability is supported.
1337 */
1338 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ) &&
1339 MLX5_CAP_GEN(mdev, mini_cqe_resp_stride_index))
1340 __set_bit(MLX5E_RQ_STATE_MINI_CQE_HW_STRIDX, &rq->state);
1341
1342 /* For enhanced CQE compression packet processing. decompress
1343 * session according to the enhanced layout.
1344 */
1345 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS) &&
1346 MLX5_CAP_GEN(mdev, enhanced_cqe_compression))
1347 __set_bit(MLX5E_RQ_STATE_MINI_CQE_ENHANCED, &rq->state);
1348
1349 return 0;
1350
1351 err_destroy_rq:
1352 mlx5e_destroy_rq(rq);
1353 err_free_rq:
1354 mlx5e_free_rq(rq);
1355
1356 return err;
1357 }
1358
mlx5e_activate_rq(struct mlx5e_rq * rq)1359 void mlx5e_activate_rq(struct mlx5e_rq *rq)
1360 {
1361 set_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
1362 }
1363
mlx5e_deactivate_rq(struct mlx5e_rq * rq)1364 void mlx5e_deactivate_rq(struct mlx5e_rq *rq)
1365 {
1366 clear_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
1367 synchronize_net(); /* Sync with NAPI to prevent mlx5e_post_rx_wqes. */
1368 }
1369
mlx5e_close_rq(struct mlx5e_rq * rq)1370 void mlx5e_close_rq(struct mlx5e_rq *rq)
1371 {
1372 if (rq->dim)
1373 cancel_work_sync(&rq->dim->work);
1374 cancel_work_sync(&rq->recover_work);
1375 mlx5e_destroy_rq(rq);
1376 mlx5e_free_rx_descs(rq);
1377 mlx5e_free_rq(rq);
1378 }
1379
mlx5e_profile_get_tisn(struct mlx5_core_dev * mdev,struct mlx5e_priv * priv,const struct mlx5e_profile * profile,u8 lag_port,u8 tc)1380 u32 mlx5e_profile_get_tisn(struct mlx5_core_dev *mdev,
1381 struct mlx5e_priv *priv,
1382 const struct mlx5e_profile *profile,
1383 u8 lag_port, u8 tc)
1384 {
1385 if (profile->get_tisn)
1386 return profile->get_tisn(mdev, priv, lag_port, tc);
1387
1388 return mdev->mlx5e_res.hw_objs.tisn[lag_port][tc];
1389 }
1390
mlx5e_free_xdpsq_db(struct mlx5e_xdpsq * sq)1391 static void mlx5e_free_xdpsq_db(struct mlx5e_xdpsq *sq)
1392 {
1393 kvfree(sq->db.xdpi_fifo.xi);
1394 kvfree(sq->db.wqe_info);
1395 }
1396
mlx5e_alloc_xdpsq_fifo(struct mlx5e_xdpsq * sq,int numa)1397 static int mlx5e_alloc_xdpsq_fifo(struct mlx5e_xdpsq *sq, int numa)
1398 {
1399 struct mlx5e_xdp_info_fifo *xdpi_fifo = &sq->db.xdpi_fifo;
1400 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1401 int entries;
1402 size_t size;
1403
1404 /* upper bound for maximum num of entries of all xmit_modes. */
1405 entries = roundup_pow_of_two(wq_sz * MLX5_SEND_WQEBB_NUM_DS *
1406 MLX5E_XDP_FIFO_ENTRIES2DS_MAX_RATIO);
1407
1408 size = array_size(sizeof(*xdpi_fifo->xi), entries);
1409 xdpi_fifo->xi = kvzalloc_node(size, GFP_KERNEL, numa);
1410 if (!xdpi_fifo->xi)
1411 return -ENOMEM;
1412
1413 xdpi_fifo->pc = &sq->xdpi_fifo_pc;
1414 xdpi_fifo->cc = &sq->xdpi_fifo_cc;
1415 xdpi_fifo->mask = entries - 1;
1416
1417 return 0;
1418 }
1419
mlx5e_alloc_xdpsq_db(struct mlx5e_xdpsq * sq,int numa)1420 static int mlx5e_alloc_xdpsq_db(struct mlx5e_xdpsq *sq, int numa)
1421 {
1422 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1423 size_t size;
1424 int err;
1425
1426 size = array_size(sizeof(*sq->db.wqe_info), wq_sz);
1427 sq->db.wqe_info = kvzalloc_node(size, GFP_KERNEL, numa);
1428 if (!sq->db.wqe_info)
1429 return -ENOMEM;
1430
1431 err = mlx5e_alloc_xdpsq_fifo(sq, numa);
1432 if (err) {
1433 mlx5e_free_xdpsq_db(sq);
1434 return err;
1435 }
1436
1437 return 0;
1438 }
1439
mlx5e_alloc_xdpsq(struct mlx5e_channel * c,struct mlx5e_params * params,struct xsk_buff_pool * xsk_pool,struct mlx5e_sq_param * param,struct mlx5e_xdpsq * sq,bool is_redirect)1440 static int mlx5e_alloc_xdpsq(struct mlx5e_channel *c,
1441 struct mlx5e_params *params,
1442 struct xsk_buff_pool *xsk_pool,
1443 struct mlx5e_sq_param *param,
1444 struct mlx5e_xdpsq *sq,
1445 bool is_redirect)
1446 {
1447 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1448 struct mlx5_core_dev *mdev = c->mdev;
1449 struct mlx5_wq_cyc *wq = &sq->wq;
1450 int err;
1451
1452 sq->pdev = c->pdev;
1453 sq->mkey_be = c->mkey_be;
1454 sq->channel = c;
1455 sq->uar_map = mdev->mlx5e_res.hw_objs.bfreg.map;
1456 sq->min_inline_mode = params->tx_min_inline_mode;
1457 sq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu) - ETH_FCS_LEN;
1458 sq->xsk_pool = xsk_pool;
1459
1460 sq->stats = sq->xsk_pool ?
1461 &c->priv->channel_stats[c->ix]->xsksq :
1462 is_redirect ?
1463 &c->priv->channel_stats[c->ix]->xdpsq :
1464 &c->priv->channel_stats[c->ix]->rq_xdpsq;
1465 sq->stop_room = param->is_mpw ? mlx5e_stop_room_for_mpwqe(mdev) :
1466 mlx5e_stop_room_for_max_wqe(mdev);
1467 sq->max_sq_mpw_wqebbs = mlx5e_get_max_sq_aligned_wqebbs(mdev);
1468
1469 param->wq.db_numa_node = cpu_to_node(c->cpu);
1470 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1471 if (err)
1472 return err;
1473 wq->db = &wq->db[MLX5_SND_DBR];
1474
1475 err = mlx5e_alloc_xdpsq_db(sq, cpu_to_node(c->cpu));
1476 if (err)
1477 goto err_sq_wq_destroy;
1478
1479 return 0;
1480
1481 err_sq_wq_destroy:
1482 mlx5_wq_destroy(&sq->wq_ctrl);
1483
1484 return err;
1485 }
1486
mlx5e_free_xdpsq(struct mlx5e_xdpsq * sq)1487 static void mlx5e_free_xdpsq(struct mlx5e_xdpsq *sq)
1488 {
1489 mlx5e_free_xdpsq_db(sq);
1490 mlx5_wq_destroy(&sq->wq_ctrl);
1491 }
1492
mlx5e_free_icosq_db(struct mlx5e_icosq * sq)1493 static void mlx5e_free_icosq_db(struct mlx5e_icosq *sq)
1494 {
1495 kvfree(sq->db.wqe_info);
1496 }
1497
mlx5e_alloc_icosq_db(struct mlx5e_icosq * sq,int numa)1498 static int mlx5e_alloc_icosq_db(struct mlx5e_icosq *sq, int numa)
1499 {
1500 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1501 size_t size;
1502
1503 size = array_size(wq_sz, sizeof(*sq->db.wqe_info));
1504 sq->db.wqe_info = kvzalloc_node(size, GFP_KERNEL, numa);
1505 if (!sq->db.wqe_info)
1506 return -ENOMEM;
1507
1508 return 0;
1509 }
1510
mlx5e_icosq_err_cqe_work(struct work_struct * recover_work)1511 static void mlx5e_icosq_err_cqe_work(struct work_struct *recover_work)
1512 {
1513 struct mlx5e_icosq *sq = container_of(recover_work, struct mlx5e_icosq,
1514 recover_work);
1515
1516 mlx5e_reporter_icosq_cqe_err(sq);
1517 }
1518
mlx5e_async_icosq_err_cqe_work(struct work_struct * recover_work)1519 static void mlx5e_async_icosq_err_cqe_work(struct work_struct *recover_work)
1520 {
1521 struct mlx5e_icosq *sq = container_of(recover_work, struct mlx5e_icosq,
1522 recover_work);
1523
1524 /* Not implemented yet. */
1525
1526 netdev_warn(sq->channel->netdev, "async_icosq recovery is not implemented\n");
1527 }
1528
mlx5e_alloc_icosq(struct mlx5e_channel * c,struct mlx5e_sq_param * param,struct mlx5e_icosq * sq,work_func_t recover_work_func)1529 static int mlx5e_alloc_icosq(struct mlx5e_channel *c,
1530 struct mlx5e_sq_param *param,
1531 struct mlx5e_icosq *sq,
1532 work_func_t recover_work_func)
1533 {
1534 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1535 struct mlx5_core_dev *mdev = c->mdev;
1536 struct mlx5_wq_cyc *wq = &sq->wq;
1537 int err;
1538
1539 sq->channel = c;
1540 sq->uar_map = mdev->mlx5e_res.hw_objs.bfreg.map;
1541 sq->reserved_room = param->stop_room;
1542
1543 param->wq.db_numa_node = cpu_to_node(c->cpu);
1544 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1545 if (err)
1546 return err;
1547 wq->db = &wq->db[MLX5_SND_DBR];
1548
1549 err = mlx5e_alloc_icosq_db(sq, cpu_to_node(c->cpu));
1550 if (err)
1551 goto err_sq_wq_destroy;
1552
1553 INIT_WORK(&sq->recover_work, recover_work_func);
1554
1555 return 0;
1556
1557 err_sq_wq_destroy:
1558 mlx5_wq_destroy(&sq->wq_ctrl);
1559
1560 return err;
1561 }
1562
mlx5e_free_icosq(struct mlx5e_icosq * sq)1563 static void mlx5e_free_icosq(struct mlx5e_icosq *sq)
1564 {
1565 mlx5e_free_icosq_db(sq);
1566 mlx5_wq_destroy(&sq->wq_ctrl);
1567 }
1568
mlx5e_free_txqsq_db(struct mlx5e_txqsq * sq)1569 void mlx5e_free_txqsq_db(struct mlx5e_txqsq *sq)
1570 {
1571 kvfree(sq->db.wqe_info);
1572 kvfree(sq->db.skb_fifo.fifo);
1573 kvfree(sq->db.dma_fifo);
1574 }
1575
mlx5e_alloc_txqsq_db(struct mlx5e_txqsq * sq,int numa)1576 int mlx5e_alloc_txqsq_db(struct mlx5e_txqsq *sq, int numa)
1577 {
1578 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1579 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
1580
1581 sq->db.dma_fifo = kvzalloc_node(array_size(df_sz,
1582 sizeof(*sq->db.dma_fifo)),
1583 GFP_KERNEL, numa);
1584 sq->db.skb_fifo.fifo = kvzalloc_node(array_size(df_sz,
1585 sizeof(*sq->db.skb_fifo.fifo)),
1586 GFP_KERNEL, numa);
1587 sq->db.wqe_info = kvzalloc_node(array_size(wq_sz,
1588 sizeof(*sq->db.wqe_info)),
1589 GFP_KERNEL, numa);
1590 if (!sq->db.dma_fifo || !sq->db.skb_fifo.fifo || !sq->db.wqe_info) {
1591 mlx5e_free_txqsq_db(sq);
1592 return -ENOMEM;
1593 }
1594
1595 sq->dma_fifo_mask = df_sz - 1;
1596
1597 sq->db.skb_fifo.pc = &sq->skb_fifo_pc;
1598 sq->db.skb_fifo.cc = &sq->skb_fifo_cc;
1599 sq->db.skb_fifo.mask = df_sz - 1;
1600
1601 return 0;
1602 }
1603
mlx5e_alloc_txqsq(struct mlx5e_channel * c,int txq_ix,struct mlx5e_params * params,struct mlx5e_sq_param * param,struct mlx5e_txqsq * sq,int tc)1604 static int mlx5e_alloc_txqsq(struct mlx5e_channel *c,
1605 int txq_ix,
1606 struct mlx5e_params *params,
1607 struct mlx5e_sq_param *param,
1608 struct mlx5e_txqsq *sq,
1609 int tc)
1610 {
1611 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1612 struct mlx5_core_dev *mdev = c->mdev;
1613 struct mlx5_wq_cyc *wq = &sq->wq;
1614 int err;
1615
1616 sq->pdev = c->pdev;
1617 sq->clock = &mdev->clock;
1618 sq->mkey_be = c->mkey_be;
1619 sq->netdev = c->netdev;
1620 sq->mdev = c->mdev;
1621 sq->channel = c;
1622 sq->priv = c->priv;
1623 sq->ch_ix = c->ix;
1624 sq->txq_ix = txq_ix;
1625 sq->uar_map = mdev->mlx5e_res.hw_objs.bfreg.map;
1626 sq->min_inline_mode = params->tx_min_inline_mode;
1627 sq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
1628 sq->max_sq_mpw_wqebbs = mlx5e_get_max_sq_aligned_wqebbs(mdev);
1629 INIT_WORK(&sq->recover_work, mlx5e_tx_err_cqe_work);
1630 if (!MLX5_CAP_ETH(mdev, wqe_vlan_insert))
1631 set_bit(MLX5E_SQ_STATE_VLAN_NEED_L2_INLINE, &sq->state);
1632 if (mlx5_ipsec_device_caps(c->priv->mdev))
1633 set_bit(MLX5E_SQ_STATE_IPSEC, &sq->state);
1634 if (param->is_mpw)
1635 set_bit(MLX5E_SQ_STATE_MPWQE, &sq->state);
1636 sq->stop_room = param->stop_room;
1637 sq->ptp_cyc2time = mlx5_sq_ts_translator(mdev);
1638
1639 param->wq.db_numa_node = cpu_to_node(c->cpu);
1640 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1641 if (err)
1642 return err;
1643 wq->db = &wq->db[MLX5_SND_DBR];
1644
1645 err = mlx5e_alloc_txqsq_db(sq, cpu_to_node(c->cpu));
1646 if (err)
1647 goto err_sq_wq_destroy;
1648
1649 return 0;
1650
1651 err_sq_wq_destroy:
1652 mlx5_wq_destroy(&sq->wq_ctrl);
1653
1654 return err;
1655 }
1656
mlx5e_free_txqsq(struct mlx5e_txqsq * sq)1657 void mlx5e_free_txqsq(struct mlx5e_txqsq *sq)
1658 {
1659 kvfree(sq->dim);
1660 mlx5e_free_txqsq_db(sq);
1661 mlx5_wq_destroy(&sq->wq_ctrl);
1662 }
1663
mlx5e_create_sq(struct mlx5_core_dev * mdev,struct mlx5e_sq_param * param,struct mlx5e_create_sq_param * csp,u32 * sqn)1664 static int mlx5e_create_sq(struct mlx5_core_dev *mdev,
1665 struct mlx5e_sq_param *param,
1666 struct mlx5e_create_sq_param *csp,
1667 u32 *sqn)
1668 {
1669 u8 ts_format;
1670 void *in;
1671 void *sqc;
1672 void *wq;
1673 int inlen;
1674 int err;
1675
1676 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1677 sizeof(u64) * csp->wq_ctrl->buf.npages;
1678 in = kvzalloc(inlen, GFP_KERNEL);
1679 if (!in)
1680 return -ENOMEM;
1681
1682 ts_format = mlx5_is_real_time_sq(mdev) ?
1683 MLX5_TIMESTAMP_FORMAT_REAL_TIME :
1684 MLX5_TIMESTAMP_FORMAT_FREE_RUNNING;
1685 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1686 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1687
1688 memcpy(sqc, param->sqc, sizeof(param->sqc));
1689 MLX5_SET(sqc, sqc, tis_lst_sz, csp->tis_lst_sz);
1690 MLX5_SET(sqc, sqc, tis_num_0, csp->tisn);
1691 MLX5_SET(sqc, sqc, cqn, csp->cqn);
1692 MLX5_SET(sqc, sqc, ts_cqe_to_dest_cqn, csp->ts_cqe_to_dest_cqn);
1693 MLX5_SET(sqc, sqc, ts_format, ts_format);
1694
1695
1696 if (MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5_CAP_INLINE_MODE_VPORT_CONTEXT)
1697 MLX5_SET(sqc, sqc, min_wqe_inline_mode, csp->min_inline_mode);
1698
1699 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1700 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1701
1702 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1703 MLX5_SET(wq, wq, uar_page, mdev->mlx5e_res.hw_objs.bfreg.index);
1704 MLX5_SET(wq, wq, log_wq_pg_sz, csp->wq_ctrl->buf.page_shift -
1705 MLX5_ADAPTER_PAGE_SHIFT);
1706 MLX5_SET64(wq, wq, dbr_addr, csp->wq_ctrl->db.dma);
1707
1708 mlx5_fill_page_frag_array(&csp->wq_ctrl->buf,
1709 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
1710
1711 err = mlx5_core_create_sq(mdev, in, inlen, sqn);
1712
1713 kvfree(in);
1714
1715 return err;
1716 }
1717
mlx5e_modify_sq(struct mlx5_core_dev * mdev,u32 sqn,struct mlx5e_modify_sq_param * p)1718 int mlx5e_modify_sq(struct mlx5_core_dev *mdev, u32 sqn,
1719 struct mlx5e_modify_sq_param *p)
1720 {
1721 u64 bitmask = 0;
1722 void *in;
1723 void *sqc;
1724 int inlen;
1725 int err;
1726
1727 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1728 in = kvzalloc(inlen, GFP_KERNEL);
1729 if (!in)
1730 return -ENOMEM;
1731
1732 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1733
1734 MLX5_SET(modify_sq_in, in, sq_state, p->curr_state);
1735 MLX5_SET(sqc, sqc, state, p->next_state);
1736 if (p->rl_update && p->next_state == MLX5_SQC_STATE_RDY) {
1737 bitmask |= 1;
1738 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, p->rl_index);
1739 }
1740 if (p->qos_update && p->next_state == MLX5_SQC_STATE_RDY) {
1741 bitmask |= 1 << 2;
1742 MLX5_SET(sqc, sqc, qos_queue_group_id, p->qos_queue_group_id);
1743 }
1744 MLX5_SET64(modify_sq_in, in, modify_bitmask, bitmask);
1745
1746 err = mlx5_core_modify_sq(mdev, sqn, in);
1747
1748 kvfree(in);
1749
1750 return err;
1751 }
1752
mlx5e_destroy_sq(struct mlx5_core_dev * mdev,u32 sqn)1753 static void mlx5e_destroy_sq(struct mlx5_core_dev *mdev, u32 sqn)
1754 {
1755 mlx5_core_destroy_sq(mdev, sqn);
1756 }
1757
mlx5e_create_sq_rdy(struct mlx5_core_dev * mdev,struct mlx5e_sq_param * param,struct mlx5e_create_sq_param * csp,u16 qos_queue_group_id,u32 * sqn)1758 int mlx5e_create_sq_rdy(struct mlx5_core_dev *mdev,
1759 struct mlx5e_sq_param *param,
1760 struct mlx5e_create_sq_param *csp,
1761 u16 qos_queue_group_id,
1762 u32 *sqn)
1763 {
1764 struct mlx5e_modify_sq_param msp = {0};
1765 int err;
1766
1767 err = mlx5e_create_sq(mdev, param, csp, sqn);
1768 if (err)
1769 return err;
1770
1771 msp.curr_state = MLX5_SQC_STATE_RST;
1772 msp.next_state = MLX5_SQC_STATE_RDY;
1773 if (qos_queue_group_id) {
1774 msp.qos_update = true;
1775 msp.qos_queue_group_id = qos_queue_group_id;
1776 }
1777 err = mlx5e_modify_sq(mdev, *sqn, &msp);
1778 if (err)
1779 mlx5e_destroy_sq(mdev, *sqn);
1780
1781 return err;
1782 }
1783
1784 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1785 struct mlx5e_txqsq *sq, u32 rate);
1786
mlx5e_open_txqsq(struct mlx5e_channel * c,u32 tisn,int txq_ix,struct mlx5e_params * params,struct mlx5e_sq_param * param,struct mlx5e_txqsq * sq,int tc,u16 qos_queue_group_id,struct mlx5e_sq_stats * sq_stats)1787 int mlx5e_open_txqsq(struct mlx5e_channel *c, u32 tisn, int txq_ix,
1788 struct mlx5e_params *params, struct mlx5e_sq_param *param,
1789 struct mlx5e_txqsq *sq, int tc, u16 qos_queue_group_id,
1790 struct mlx5e_sq_stats *sq_stats)
1791 {
1792 struct mlx5e_create_sq_param csp = {};
1793 u32 tx_rate;
1794 int err;
1795
1796 err = mlx5e_alloc_txqsq(c, txq_ix, params, param, sq, tc);
1797 if (err)
1798 return err;
1799
1800 sq->stats = sq_stats;
1801
1802 csp.tisn = tisn;
1803 csp.tis_lst_sz = 1;
1804 csp.cqn = sq->cq.mcq.cqn;
1805 csp.wq_ctrl = &sq->wq_ctrl;
1806 csp.min_inline_mode = sq->min_inline_mode;
1807 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, qos_queue_group_id, &sq->sqn);
1808 if (err)
1809 goto err_free_txqsq;
1810
1811 tx_rate = c->priv->tx_rates[sq->txq_ix];
1812 if (tx_rate)
1813 mlx5e_set_sq_maxrate(c->netdev, sq, tx_rate);
1814
1815 if (sq->channel && !params->tx_dim_enabled) {
1816 sq->channel->tx_cq_moder = params->tx_cq_moderation;
1817 } else if (sq->channel) {
1818 u8 cq_period_mode;
1819
1820 cq_period_mode = params->tx_moder_use_cqe_mode ?
1821 DIM_CQ_PERIOD_MODE_START_FROM_CQE :
1822 DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1823 mlx5e_reset_tx_moderation(&sq->channel->tx_cq_moder,
1824 cq_period_mode,
1825 params->tx_dim_enabled);
1826
1827 err = mlx5e_dim_tx_change(sq, params->tx_dim_enabled);
1828 if (err)
1829 goto err_destroy_sq;
1830 }
1831
1832 return 0;
1833
1834 err_destroy_sq:
1835 mlx5e_destroy_sq(c->mdev, sq->sqn);
1836 err_free_txqsq:
1837 mlx5e_free_txqsq(sq);
1838
1839 return err;
1840 }
1841
mlx5e_activate_txqsq(struct mlx5e_txqsq * sq)1842 void mlx5e_activate_txqsq(struct mlx5e_txqsq *sq)
1843 {
1844 sq->txq = netdev_get_tx_queue(sq->netdev, sq->txq_ix);
1845 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1846 netdev_tx_reset_queue(sq->txq);
1847 netif_tx_start_queue(sq->txq);
1848 netif_queue_set_napi(sq->netdev, sq->txq_ix, NETDEV_QUEUE_TYPE_TX, sq->cq.napi);
1849 }
1850
mlx5e_tx_disable_queue(struct netdev_queue * txq)1851 void mlx5e_tx_disable_queue(struct netdev_queue *txq)
1852 {
1853 __netif_tx_lock_bh(txq);
1854 netif_tx_stop_queue(txq);
1855 __netif_tx_unlock_bh(txq);
1856 }
1857
mlx5e_deactivate_txqsq(struct mlx5e_txqsq * sq)1858 void mlx5e_deactivate_txqsq(struct mlx5e_txqsq *sq)
1859 {
1860 struct mlx5_wq_cyc *wq = &sq->wq;
1861
1862 netif_queue_set_napi(sq->netdev, sq->txq_ix, NETDEV_QUEUE_TYPE_TX, NULL);
1863 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1864 synchronize_net(); /* Sync with NAPI to prevent netif_tx_wake_queue. */
1865
1866 mlx5e_tx_disable_queue(sq->txq);
1867
1868 /* last doorbell out, godspeed .. */
1869 if (mlx5e_wqc_has_room_for(wq, sq->cc, sq->pc, 1)) {
1870 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
1871 struct mlx5e_tx_wqe *nop;
1872
1873 sq->db.wqe_info[pi] = (struct mlx5e_tx_wqe_info) {
1874 .num_wqebbs = 1,
1875 };
1876
1877 nop = mlx5e_post_nop(wq, sq->sqn, &sq->pc);
1878 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &nop->ctrl);
1879 }
1880 }
1881
mlx5e_close_txqsq(struct mlx5e_txqsq * sq)1882 void mlx5e_close_txqsq(struct mlx5e_txqsq *sq)
1883 {
1884 struct mlx5_core_dev *mdev = sq->mdev;
1885 struct mlx5_rate_limit rl = {0};
1886
1887 if (sq->dim)
1888 cancel_work_sync(&sq->dim->work);
1889 cancel_work_sync(&sq->recover_work);
1890 mlx5e_destroy_sq(mdev, sq->sqn);
1891 if (sq->rate_limit) {
1892 rl.rate = sq->rate_limit;
1893 mlx5_rl_remove_rate(mdev, &rl);
1894 }
1895 mlx5e_free_txqsq_descs(sq);
1896 mlx5e_free_txqsq(sq);
1897 }
1898
mlx5e_tx_err_cqe_work(struct work_struct * recover_work)1899 void mlx5e_tx_err_cqe_work(struct work_struct *recover_work)
1900 {
1901 struct mlx5e_txqsq *sq = container_of(recover_work, struct mlx5e_txqsq,
1902 recover_work);
1903
1904 mlx5e_reporter_tx_err_cqe(sq);
1905 }
1906
mlx5e_get_def_tx_moderation(u8 cq_period_mode)1907 static struct dim_cq_moder mlx5e_get_def_tx_moderation(u8 cq_period_mode)
1908 {
1909 return (struct dim_cq_moder) {
1910 .cq_period_mode = cq_period_mode,
1911 .pkts = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS,
1912 .usec = cq_period_mode == DIM_CQ_PERIOD_MODE_START_FROM_CQE ?
1913 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC_FROM_CQE :
1914 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC,
1915 };
1916 }
1917
mlx5e_reset_tx_moderation(struct dim_cq_moder * cq_moder,u8 cq_period_mode,bool dim_enabled)1918 bool mlx5e_reset_tx_moderation(struct dim_cq_moder *cq_moder, u8 cq_period_mode,
1919 bool dim_enabled)
1920 {
1921 bool reset_needed = cq_moder->cq_period_mode != cq_period_mode;
1922
1923 if (dim_enabled)
1924 *cq_moder = net_dim_get_def_tx_moderation(cq_period_mode);
1925 else
1926 *cq_moder = mlx5e_get_def_tx_moderation(cq_period_mode);
1927
1928 return reset_needed;
1929 }
1930
mlx5e_reset_tx_channels_moderation(struct mlx5e_channels * chs,u8 cq_period_mode,bool dim_enabled,bool keep_dim_state)1931 bool mlx5e_reset_tx_channels_moderation(struct mlx5e_channels *chs, u8 cq_period_mode,
1932 bool dim_enabled, bool keep_dim_state)
1933 {
1934 bool reset = false;
1935 int i, tc;
1936
1937 for (i = 0; i < chs->num; i++) {
1938 for (tc = 0; tc < mlx5e_get_dcb_num_tc(&chs->params); tc++) {
1939 if (keep_dim_state)
1940 dim_enabled = !!chs->c[i]->sq[tc].dim;
1941
1942 reset |= mlx5e_reset_tx_moderation(&chs->c[i]->tx_cq_moder,
1943 cq_period_mode, dim_enabled);
1944 }
1945 }
1946
1947 return reset;
1948 }
1949
mlx5e_open_icosq(struct mlx5e_channel * c,struct mlx5e_params * params,struct mlx5e_sq_param * param,struct mlx5e_icosq * sq,work_func_t recover_work_func)1950 static int mlx5e_open_icosq(struct mlx5e_channel *c, struct mlx5e_params *params,
1951 struct mlx5e_sq_param *param, struct mlx5e_icosq *sq,
1952 work_func_t recover_work_func)
1953 {
1954 struct mlx5e_create_sq_param csp = {};
1955 int err;
1956
1957 err = mlx5e_alloc_icosq(c, param, sq, recover_work_func);
1958 if (err)
1959 return err;
1960
1961 csp.cqn = sq->cq.mcq.cqn;
1962 csp.wq_ctrl = &sq->wq_ctrl;
1963 csp.min_inline_mode = params->tx_min_inline_mode;
1964 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, 0, &sq->sqn);
1965 if (err)
1966 goto err_free_icosq;
1967
1968 if (param->is_tls) {
1969 sq->ktls_resync = mlx5e_ktls_rx_resync_create_resp_list();
1970 if (IS_ERR(sq->ktls_resync)) {
1971 err = PTR_ERR(sq->ktls_resync);
1972 goto err_destroy_icosq;
1973 }
1974 }
1975 return 0;
1976
1977 err_destroy_icosq:
1978 mlx5e_destroy_sq(c->mdev, sq->sqn);
1979 err_free_icosq:
1980 mlx5e_free_icosq(sq);
1981
1982 return err;
1983 }
1984
mlx5e_activate_icosq(struct mlx5e_icosq * icosq)1985 void mlx5e_activate_icosq(struct mlx5e_icosq *icosq)
1986 {
1987 set_bit(MLX5E_SQ_STATE_ENABLED, &icosq->state);
1988 }
1989
mlx5e_deactivate_icosq(struct mlx5e_icosq * icosq)1990 void mlx5e_deactivate_icosq(struct mlx5e_icosq *icosq)
1991 {
1992 clear_bit(MLX5E_SQ_STATE_ENABLED, &icosq->state);
1993 synchronize_net(); /* Sync with NAPI. */
1994 }
1995
mlx5e_close_icosq(struct mlx5e_icosq * sq)1996 static void mlx5e_close_icosq(struct mlx5e_icosq *sq)
1997 {
1998 struct mlx5e_channel *c = sq->channel;
1999
2000 if (sq->ktls_resync)
2001 mlx5e_ktls_rx_resync_destroy_resp_list(sq->ktls_resync);
2002 mlx5e_destroy_sq(c->mdev, sq->sqn);
2003 mlx5e_free_icosq_descs(sq);
2004 mlx5e_free_icosq(sq);
2005 }
2006
mlx5e_open_xdpsq(struct mlx5e_channel * c,struct mlx5e_params * params,struct mlx5e_sq_param * param,struct xsk_buff_pool * xsk_pool,struct mlx5e_xdpsq * sq,bool is_redirect)2007 int mlx5e_open_xdpsq(struct mlx5e_channel *c, struct mlx5e_params *params,
2008 struct mlx5e_sq_param *param, struct xsk_buff_pool *xsk_pool,
2009 struct mlx5e_xdpsq *sq, bool is_redirect)
2010 {
2011 struct mlx5e_create_sq_param csp = {};
2012 int err;
2013
2014 err = mlx5e_alloc_xdpsq(c, params, xsk_pool, param, sq, is_redirect);
2015 if (err)
2016 return err;
2017
2018 csp.tis_lst_sz = 1;
2019 csp.tisn = mlx5e_profile_get_tisn(c->mdev, c->priv, c->priv->profile,
2020 c->lag_port, 0); /* tc = 0 */
2021 csp.cqn = sq->cq.mcq.cqn;
2022 csp.wq_ctrl = &sq->wq_ctrl;
2023 csp.min_inline_mode = sq->min_inline_mode;
2024 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
2025
2026 if (param->is_xdp_mb)
2027 set_bit(MLX5E_SQ_STATE_XDP_MULTIBUF, &sq->state);
2028
2029 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, 0, &sq->sqn);
2030 if (err)
2031 goto err_free_xdpsq;
2032
2033 mlx5e_set_xmit_fp(sq, param->is_mpw);
2034
2035 if (!param->is_mpw && !test_bit(MLX5E_SQ_STATE_XDP_MULTIBUF, &sq->state)) {
2036 unsigned int ds_cnt = MLX5E_TX_WQE_EMPTY_DS_COUNT + 1;
2037 unsigned int inline_hdr_sz = 0;
2038 int i;
2039
2040 if (sq->min_inline_mode != MLX5_INLINE_MODE_NONE) {
2041 inline_hdr_sz = MLX5E_XDP_MIN_INLINE;
2042 ds_cnt++;
2043 }
2044
2045 /* Pre initialize fixed WQE fields */
2046 for (i = 0; i < mlx5_wq_cyc_get_size(&sq->wq); i++) {
2047 struct mlx5e_tx_wqe *wqe = mlx5_wq_cyc_get_wqe(&sq->wq, i);
2048 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
2049 struct mlx5_wqe_eth_seg *eseg = &wqe->eth;
2050
2051 sq->db.wqe_info[i] = (struct mlx5e_xdp_wqe_info) {
2052 .num_wqebbs = 1,
2053 .num_pkts = 1,
2054 };
2055
2056 cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
2057 eseg->inline_hdr.sz = cpu_to_be16(inline_hdr_sz);
2058 }
2059 }
2060
2061 return 0;
2062
2063 err_free_xdpsq:
2064 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
2065 mlx5e_free_xdpsq(sq);
2066
2067 return err;
2068 }
2069
mlx5e_close_xdpsq(struct mlx5e_xdpsq * sq)2070 void mlx5e_close_xdpsq(struct mlx5e_xdpsq *sq)
2071 {
2072 struct mlx5e_channel *c = sq->channel;
2073
2074 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
2075 synchronize_net(); /* Sync with NAPI. */
2076
2077 mlx5e_destroy_sq(c->mdev, sq->sqn);
2078 mlx5e_free_xdpsq_descs(sq);
2079 mlx5e_free_xdpsq(sq);
2080 }
2081
mlx5e_open_xdpredirect_sq(struct mlx5e_channel * c,struct mlx5e_params * params,struct mlx5e_channel_param * cparam,struct mlx5e_create_cq_param * ccp)2082 static struct mlx5e_xdpsq *mlx5e_open_xdpredirect_sq(struct mlx5e_channel *c,
2083 struct mlx5e_params *params,
2084 struct mlx5e_channel_param *cparam,
2085 struct mlx5e_create_cq_param *ccp)
2086 {
2087 struct mlx5e_xdpsq *xdpsq;
2088 int err;
2089
2090 xdpsq = kvzalloc_node(sizeof(*xdpsq), GFP_KERNEL, cpu_to_node(c->cpu));
2091 if (!xdpsq)
2092 return ERR_PTR(-ENOMEM);
2093
2094 err = mlx5e_open_cq(c->mdev, params->tx_cq_moderation,
2095 &cparam->xdp_sq.cqp, ccp, &xdpsq->cq);
2096 if (err)
2097 goto err_free_xdpsq;
2098
2099 err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, NULL, xdpsq, true);
2100 if (err)
2101 goto err_close_xdpsq_cq;
2102
2103 return xdpsq;
2104
2105 err_close_xdpsq_cq:
2106 mlx5e_close_cq(&xdpsq->cq);
2107 err_free_xdpsq:
2108 kvfree(xdpsq);
2109
2110 return ERR_PTR(err);
2111 }
2112
mlx5e_close_xdpredirect_sq(struct mlx5e_xdpsq * xdpsq)2113 static void mlx5e_close_xdpredirect_sq(struct mlx5e_xdpsq *xdpsq)
2114 {
2115 mlx5e_close_xdpsq(xdpsq);
2116 mlx5e_close_cq(&xdpsq->cq);
2117 kvfree(xdpsq);
2118 }
2119
mlx5e_alloc_cq_common(struct mlx5_core_dev * mdev,struct net_device * netdev,struct workqueue_struct * workqueue,struct mlx5e_cq_param * param,struct mlx5e_cq * cq)2120 static int mlx5e_alloc_cq_common(struct mlx5_core_dev *mdev,
2121 struct net_device *netdev,
2122 struct workqueue_struct *workqueue,
2123 struct mlx5e_cq_param *param,
2124 struct mlx5e_cq *cq)
2125 {
2126 struct mlx5_core_cq *mcq = &cq->mcq;
2127 int err;
2128 u32 i;
2129
2130 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
2131 &cq->wq_ctrl);
2132 if (err)
2133 return err;
2134
2135 mcq->cqe_sz = 64;
2136 mcq->set_ci_db = cq->wq_ctrl.db.db;
2137 mcq->arm_db = cq->wq_ctrl.db.db + 1;
2138 *mcq->set_ci_db = 0;
2139 *mcq->arm_db = 0;
2140 mcq->vector = param->eq_ix;
2141 mcq->comp = mlx5e_completion_event;
2142 mcq->event = mlx5e_cq_error_event;
2143
2144 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
2145 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
2146
2147 cqe->op_own = 0xf1;
2148 cqe->validity_iteration_count = 0xff;
2149 }
2150
2151 cq->mdev = mdev;
2152 cq->netdev = netdev;
2153 cq->workqueue = workqueue;
2154
2155 return 0;
2156 }
2157
mlx5e_alloc_cq(struct mlx5_core_dev * mdev,struct mlx5e_cq_param * param,struct mlx5e_create_cq_param * ccp,struct mlx5e_cq * cq)2158 static int mlx5e_alloc_cq(struct mlx5_core_dev *mdev,
2159 struct mlx5e_cq_param *param,
2160 struct mlx5e_create_cq_param *ccp,
2161 struct mlx5e_cq *cq)
2162 {
2163 int err;
2164
2165 param->wq.buf_numa_node = ccp->node;
2166 param->wq.db_numa_node = ccp->node;
2167 param->eq_ix = ccp->ix;
2168
2169 err = mlx5e_alloc_cq_common(mdev, ccp->netdev, ccp->wq, param, cq);
2170
2171 cq->napi = ccp->napi;
2172 cq->ch_stats = ccp->ch_stats;
2173
2174 return err;
2175 }
2176
mlx5e_free_cq(struct mlx5e_cq * cq)2177 static void mlx5e_free_cq(struct mlx5e_cq *cq)
2178 {
2179 mlx5_wq_destroy(&cq->wq_ctrl);
2180 }
2181
mlx5e_create_cq(struct mlx5e_cq * cq,struct mlx5e_cq_param * param)2182 static int mlx5e_create_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
2183 {
2184 u32 out[MLX5_ST_SZ_DW(create_cq_out)];
2185 struct mlx5_core_dev *mdev = cq->mdev;
2186 struct mlx5_core_cq *mcq = &cq->mcq;
2187
2188 void *in;
2189 void *cqc;
2190 int inlen;
2191 int eqn;
2192 int err;
2193
2194 err = mlx5_comp_eqn_get(mdev, param->eq_ix, &eqn);
2195 if (err)
2196 return err;
2197
2198 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
2199 sizeof(u64) * cq->wq_ctrl.buf.npages;
2200 in = kvzalloc(inlen, GFP_KERNEL);
2201 if (!in)
2202 return -ENOMEM;
2203
2204 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
2205
2206 memcpy(cqc, param->cqc, sizeof(param->cqc));
2207
2208 mlx5_fill_page_frag_array(&cq->wq_ctrl.buf,
2209 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
2210
2211 MLX5_SET(cqc, cqc, cq_period_mode, mlx5e_cq_period_mode(param->cq_period_mode));
2212
2213 MLX5_SET(cqc, cqc, c_eqn_or_apu_element, eqn);
2214 MLX5_SET(cqc, cqc, uar_page, mdev->priv.uar->index);
2215 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
2216 MLX5_ADAPTER_PAGE_SHIFT);
2217 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
2218
2219 err = mlx5_core_create_cq(mdev, mcq, in, inlen, out, sizeof(out));
2220
2221 kvfree(in);
2222
2223 if (err)
2224 return err;
2225
2226 mlx5e_cq_arm(cq);
2227
2228 return 0;
2229 }
2230
mlx5e_destroy_cq(struct mlx5e_cq * cq)2231 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
2232 {
2233 mlx5_core_destroy_cq(cq->mdev, &cq->mcq);
2234 }
2235
mlx5e_open_cq(struct mlx5_core_dev * mdev,struct dim_cq_moder moder,struct mlx5e_cq_param * param,struct mlx5e_create_cq_param * ccp,struct mlx5e_cq * cq)2236 int mlx5e_open_cq(struct mlx5_core_dev *mdev, struct dim_cq_moder moder,
2237 struct mlx5e_cq_param *param, struct mlx5e_create_cq_param *ccp,
2238 struct mlx5e_cq *cq)
2239 {
2240 int err;
2241
2242 err = mlx5e_alloc_cq(mdev, param, ccp, cq);
2243 if (err)
2244 return err;
2245
2246 err = mlx5e_create_cq(cq, param);
2247 if (err)
2248 goto err_free_cq;
2249
2250 if (MLX5_CAP_GEN(mdev, cq_moderation) &&
2251 MLX5_CAP_GEN(mdev, cq_period_mode_modify))
2252 mlx5e_modify_cq_moderation(mdev, &cq->mcq, moder.usec, moder.pkts,
2253 mlx5e_cq_period_mode(moder.cq_period_mode));
2254 return 0;
2255
2256 err_free_cq:
2257 mlx5e_free_cq(cq);
2258
2259 return err;
2260 }
2261
mlx5e_close_cq(struct mlx5e_cq * cq)2262 void mlx5e_close_cq(struct mlx5e_cq *cq)
2263 {
2264 mlx5e_destroy_cq(cq);
2265 mlx5e_free_cq(cq);
2266 }
2267
mlx5e_modify_cq_period_mode(struct mlx5_core_dev * dev,struct mlx5_core_cq * cq,u8 cq_period_mode)2268 int mlx5e_modify_cq_period_mode(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
2269 u8 cq_period_mode)
2270 {
2271 u32 in[MLX5_ST_SZ_DW(modify_cq_in)] = {};
2272 void *cqc;
2273
2274 MLX5_SET(modify_cq_in, in, cqn, cq->cqn);
2275 cqc = MLX5_ADDR_OF(modify_cq_in, in, cq_context);
2276 MLX5_SET(cqc, cqc, cq_period_mode, mlx5e_cq_period_mode(cq_period_mode));
2277 MLX5_SET(modify_cq_in, in,
2278 modify_field_select_resize_field_select.modify_field_select.modify_field_select,
2279 MLX5_CQ_MODIFY_PERIOD_MODE);
2280
2281 return mlx5_core_modify_cq(dev, cq, in, sizeof(in));
2282 }
2283
mlx5e_modify_cq_moderation(struct mlx5_core_dev * dev,struct mlx5_core_cq * cq,u16 cq_period,u16 cq_max_count,u8 cq_period_mode)2284 int mlx5e_modify_cq_moderation(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
2285 u16 cq_period, u16 cq_max_count, u8 cq_period_mode)
2286 {
2287 u32 in[MLX5_ST_SZ_DW(modify_cq_in)] = {};
2288 void *cqc;
2289
2290 MLX5_SET(modify_cq_in, in, cqn, cq->cqn);
2291 cqc = MLX5_ADDR_OF(modify_cq_in, in, cq_context);
2292 MLX5_SET(cqc, cqc, cq_period, cq_period);
2293 MLX5_SET(cqc, cqc, cq_max_count, cq_max_count);
2294 MLX5_SET(cqc, cqc, cq_period_mode, cq_period_mode);
2295 MLX5_SET(modify_cq_in, in,
2296 modify_field_select_resize_field_select.modify_field_select.modify_field_select,
2297 MLX5_CQ_MODIFY_PERIOD | MLX5_CQ_MODIFY_COUNT | MLX5_CQ_MODIFY_PERIOD_MODE);
2298
2299 return mlx5_core_modify_cq(dev, cq, in, sizeof(in));
2300 }
2301
mlx5e_open_tx_cqs(struct mlx5e_channel * c,struct mlx5e_params * params,struct mlx5e_create_cq_param * ccp,struct mlx5e_channel_param * cparam)2302 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
2303 struct mlx5e_params *params,
2304 struct mlx5e_create_cq_param *ccp,
2305 struct mlx5e_channel_param *cparam)
2306 {
2307 int err;
2308 int tc;
2309
2310 for (tc = 0; tc < c->num_tc; tc++) {
2311 err = mlx5e_open_cq(c->mdev, params->tx_cq_moderation, &cparam->txq_sq.cqp,
2312 ccp, &c->sq[tc].cq);
2313 if (err)
2314 goto err_close_tx_cqs;
2315 }
2316
2317 return 0;
2318
2319 err_close_tx_cqs:
2320 for (tc--; tc >= 0; tc--)
2321 mlx5e_close_cq(&c->sq[tc].cq);
2322
2323 return err;
2324 }
2325
mlx5e_close_tx_cqs(struct mlx5e_channel * c)2326 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
2327 {
2328 int tc;
2329
2330 for (tc = 0; tc < c->num_tc; tc++)
2331 mlx5e_close_cq(&c->sq[tc].cq);
2332 }
2333
mlx5e_mqprio_txq_to_tc(struct netdev_tc_txq * tc_to_txq,unsigned int txq)2334 static int mlx5e_mqprio_txq_to_tc(struct netdev_tc_txq *tc_to_txq, unsigned int txq)
2335 {
2336 int tc;
2337
2338 for (tc = 0; tc < TC_MAX_QUEUE; tc++)
2339 if (txq - tc_to_txq[tc].offset < tc_to_txq[tc].count)
2340 return tc;
2341
2342 WARN(1, "Unexpected TCs configuration. No match found for txq %u", txq);
2343 return -ENOENT;
2344 }
2345
mlx5e_txq_get_qos_node_hw_id(struct mlx5e_params * params,int txq_ix,u32 * hw_id)2346 static int mlx5e_txq_get_qos_node_hw_id(struct mlx5e_params *params, int txq_ix,
2347 u32 *hw_id)
2348 {
2349 int tc;
2350
2351 if (params->mqprio.mode != TC_MQPRIO_MODE_CHANNEL) {
2352 *hw_id = 0;
2353 return 0;
2354 }
2355
2356 tc = mlx5e_mqprio_txq_to_tc(params->mqprio.tc_to_txq, txq_ix);
2357 if (tc < 0)
2358 return tc;
2359
2360 if (tc >= params->mqprio.num_tc) {
2361 WARN(1, "Unexpected TCs configuration. tc %d is out of range of %u",
2362 tc, params->mqprio.num_tc);
2363 return -EINVAL;
2364 }
2365
2366 *hw_id = params->mqprio.channel.hw_id[tc];
2367 return 0;
2368 }
2369
mlx5e_open_sqs(struct mlx5e_channel * c,struct mlx5e_params * params,struct mlx5e_channel_param * cparam)2370 static int mlx5e_open_sqs(struct mlx5e_channel *c,
2371 struct mlx5e_params *params,
2372 struct mlx5e_channel_param *cparam)
2373 {
2374 int err, tc;
2375
2376 for (tc = 0; tc < mlx5e_get_dcb_num_tc(params); tc++) {
2377 int txq_ix = c->ix + tc * params->num_channels;
2378 u32 qos_queue_group_id;
2379 u32 tisn;
2380
2381 tisn = mlx5e_profile_get_tisn(c->mdev, c->priv, c->priv->profile,
2382 c->lag_port, tc);
2383 err = mlx5e_txq_get_qos_node_hw_id(params, txq_ix, &qos_queue_group_id);
2384 if (err)
2385 goto err_close_sqs;
2386
2387 err = mlx5e_open_txqsq(c, tisn, txq_ix,
2388 params, &cparam->txq_sq, &c->sq[tc], tc,
2389 qos_queue_group_id,
2390 &c->priv->channel_stats[c->ix]->sq[tc]);
2391 if (err)
2392 goto err_close_sqs;
2393 }
2394
2395 return 0;
2396
2397 err_close_sqs:
2398 for (tc--; tc >= 0; tc--)
2399 mlx5e_close_txqsq(&c->sq[tc]);
2400
2401 return err;
2402 }
2403
mlx5e_close_sqs(struct mlx5e_channel * c)2404 static void mlx5e_close_sqs(struct mlx5e_channel *c)
2405 {
2406 int tc;
2407
2408 for (tc = 0; tc < c->num_tc; tc++)
2409 mlx5e_close_txqsq(&c->sq[tc]);
2410 }
2411
mlx5e_set_sq_maxrate(struct net_device * dev,struct mlx5e_txqsq * sq,u32 rate)2412 static int mlx5e_set_sq_maxrate(struct net_device *dev,
2413 struct mlx5e_txqsq *sq, u32 rate)
2414 {
2415 struct mlx5e_priv *priv = netdev_priv(dev);
2416 struct mlx5_core_dev *mdev = priv->mdev;
2417 struct mlx5e_modify_sq_param msp = {0};
2418 struct mlx5_rate_limit rl = {0};
2419 u16 rl_index = 0;
2420 int err;
2421
2422 if (rate == sq->rate_limit)
2423 /* nothing to do */
2424 return 0;
2425
2426 if (sq->rate_limit) {
2427 rl.rate = sq->rate_limit;
2428 /* remove current rl index to free space to next ones */
2429 mlx5_rl_remove_rate(mdev, &rl);
2430 }
2431
2432 sq->rate_limit = 0;
2433
2434 if (rate) {
2435 rl.rate = rate;
2436 err = mlx5_rl_add_rate(mdev, &rl_index, &rl);
2437 if (err) {
2438 netdev_err(dev, "Failed configuring rate %u: %d\n",
2439 rate, err);
2440 return err;
2441 }
2442 }
2443
2444 msp.curr_state = MLX5_SQC_STATE_RDY;
2445 msp.next_state = MLX5_SQC_STATE_RDY;
2446 msp.rl_index = rl_index;
2447 msp.rl_update = true;
2448 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
2449 if (err) {
2450 netdev_err(dev, "Failed configuring rate %u: %d\n",
2451 rate, err);
2452 /* remove the rate from the table */
2453 if (rate)
2454 mlx5_rl_remove_rate(mdev, &rl);
2455 return err;
2456 }
2457
2458 sq->rate_limit = rate;
2459 return 0;
2460 }
2461
mlx5e_set_tx_maxrate(struct net_device * dev,int index,u32 rate)2462 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
2463 {
2464 struct mlx5e_priv *priv = netdev_priv(dev);
2465 struct mlx5_core_dev *mdev = priv->mdev;
2466 struct mlx5e_txqsq *sq = priv->txq2sq[index];
2467 int err = 0;
2468
2469 if (!mlx5_rl_is_supported(mdev)) {
2470 netdev_err(dev, "Rate limiting is not supported on this device\n");
2471 return -EINVAL;
2472 }
2473
2474 /* rate is given in Mb/sec, HW config is in Kb/sec */
2475 rate = rate << 10;
2476
2477 /* Check whether rate in valid range, 0 is always valid */
2478 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
2479 netdev_err(dev, "TX rate %u, is not in range\n", rate);
2480 return -ERANGE;
2481 }
2482
2483 mutex_lock(&priv->state_lock);
2484 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
2485 err = mlx5e_set_sq_maxrate(dev, sq, rate);
2486 if (!err)
2487 priv->tx_rates[index] = rate;
2488 mutex_unlock(&priv->state_lock);
2489
2490 return err;
2491 }
2492
mlx5e_open_rxq_rq(struct mlx5e_channel * c,struct mlx5e_params * params,struct mlx5e_rq_param * rq_params)2493 static int mlx5e_open_rxq_rq(struct mlx5e_channel *c, struct mlx5e_params *params,
2494 struct mlx5e_rq_param *rq_params)
2495 {
2496 u16 q_counter = c->priv->q_counter[c->sd_ix];
2497 int err;
2498
2499 err = mlx5e_init_rxq_rq(c, params, rq_params->xdp_frag_size, &c->rq);
2500 if (err)
2501 return err;
2502
2503 return mlx5e_open_rq(params, rq_params, NULL, cpu_to_node(c->cpu), q_counter, &c->rq);
2504 }
2505
mlx5e_open_queues(struct mlx5e_channel * c,struct mlx5e_params * params,struct mlx5e_channel_param * cparam)2506 static int mlx5e_open_queues(struct mlx5e_channel *c,
2507 struct mlx5e_params *params,
2508 struct mlx5e_channel_param *cparam)
2509 {
2510 const struct net_device_ops *netdev_ops = c->netdev->netdev_ops;
2511 struct dim_cq_moder icocq_moder = {0, 0};
2512 struct mlx5e_create_cq_param ccp;
2513 int err;
2514
2515 mlx5e_build_create_cq_param(&ccp, c);
2516
2517 err = mlx5e_open_cq(c->mdev, icocq_moder, &cparam->async_icosq.cqp, &ccp,
2518 &c->async_icosq.cq);
2519 if (err)
2520 return err;
2521
2522 err = mlx5e_open_cq(c->mdev, icocq_moder, &cparam->icosq.cqp, &ccp,
2523 &c->icosq.cq);
2524 if (err)
2525 goto err_close_async_icosq_cq;
2526
2527 err = mlx5e_open_tx_cqs(c, params, &ccp, cparam);
2528 if (err)
2529 goto err_close_icosq_cq;
2530
2531 if (netdev_ops->ndo_xdp_xmit) {
2532 c->xdpsq = mlx5e_open_xdpredirect_sq(c, params, cparam, &ccp);
2533 if (IS_ERR(c->xdpsq)) {
2534 err = PTR_ERR(c->xdpsq);
2535 goto err_close_tx_cqs;
2536 }
2537 }
2538
2539 err = mlx5e_open_cq(c->mdev, params->rx_cq_moderation, &cparam->rq.cqp, &ccp,
2540 &c->rq.cq);
2541 if (err)
2542 goto err_close_xdpredirect_sq;
2543
2544 err = c->xdp ? mlx5e_open_cq(c->mdev, params->tx_cq_moderation, &cparam->xdp_sq.cqp,
2545 &ccp, &c->rq_xdpsq.cq) : 0;
2546 if (err)
2547 goto err_close_rx_cq;
2548
2549 spin_lock_init(&c->async_icosq_lock);
2550
2551 err = mlx5e_open_icosq(c, params, &cparam->async_icosq, &c->async_icosq,
2552 mlx5e_async_icosq_err_cqe_work);
2553 if (err)
2554 goto err_close_rq_xdpsq_cq;
2555
2556 mutex_init(&c->icosq_recovery_lock);
2557
2558 err = mlx5e_open_icosq(c, params, &cparam->icosq, &c->icosq,
2559 mlx5e_icosq_err_cqe_work);
2560 if (err)
2561 goto err_close_async_icosq;
2562
2563 err = mlx5e_open_sqs(c, params, cparam);
2564 if (err)
2565 goto err_close_icosq;
2566
2567 err = mlx5e_open_rxq_rq(c, params, &cparam->rq);
2568 if (err)
2569 goto err_close_sqs;
2570
2571 if (c->xdp) {
2572 err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, NULL,
2573 &c->rq_xdpsq, false);
2574 if (err)
2575 goto err_close_rq;
2576 }
2577
2578 return 0;
2579
2580 err_close_rq:
2581 mlx5e_close_rq(&c->rq);
2582
2583 err_close_sqs:
2584 mlx5e_close_sqs(c);
2585
2586 err_close_icosq:
2587 mlx5e_close_icosq(&c->icosq);
2588
2589 err_close_async_icosq:
2590 mlx5e_close_icosq(&c->async_icosq);
2591
2592 err_close_rq_xdpsq_cq:
2593 if (c->xdp)
2594 mlx5e_close_cq(&c->rq_xdpsq.cq);
2595
2596 err_close_rx_cq:
2597 mlx5e_close_cq(&c->rq.cq);
2598
2599 err_close_xdpredirect_sq:
2600 if (c->xdpsq)
2601 mlx5e_close_xdpredirect_sq(c->xdpsq);
2602
2603 err_close_tx_cqs:
2604 mlx5e_close_tx_cqs(c);
2605
2606 err_close_icosq_cq:
2607 mlx5e_close_cq(&c->icosq.cq);
2608
2609 err_close_async_icosq_cq:
2610 mlx5e_close_cq(&c->async_icosq.cq);
2611
2612 return err;
2613 }
2614
mlx5e_close_queues(struct mlx5e_channel * c)2615 static void mlx5e_close_queues(struct mlx5e_channel *c)
2616 {
2617 if (c->xdp)
2618 mlx5e_close_xdpsq(&c->rq_xdpsq);
2619 /* The same ICOSQ is used for UMRs for both RQ and XSKRQ. */
2620 cancel_work_sync(&c->icosq.recover_work);
2621 mlx5e_close_rq(&c->rq);
2622 mlx5e_close_sqs(c);
2623 mlx5e_close_icosq(&c->icosq);
2624 mutex_destroy(&c->icosq_recovery_lock);
2625 mlx5e_close_icosq(&c->async_icosq);
2626 if (c->xdp)
2627 mlx5e_close_cq(&c->rq_xdpsq.cq);
2628 mlx5e_close_cq(&c->rq.cq);
2629 if (c->xdpsq)
2630 mlx5e_close_xdpredirect_sq(c->xdpsq);
2631 mlx5e_close_tx_cqs(c);
2632 mlx5e_close_cq(&c->icosq.cq);
2633 mlx5e_close_cq(&c->async_icosq.cq);
2634 }
2635
mlx5e_enumerate_lag_port(struct mlx5_core_dev * mdev,int ix)2636 static u8 mlx5e_enumerate_lag_port(struct mlx5_core_dev *mdev, int ix)
2637 {
2638 u16 port_aff_bias = mlx5_core_is_pf(mdev) ? 0 : MLX5_CAP_GEN(mdev, vhca_id);
2639
2640 return (ix + port_aff_bias) % mlx5e_get_num_lag_ports(mdev);
2641 }
2642
mlx5e_channel_stats_alloc(struct mlx5e_priv * priv,int ix,int cpu)2643 static int mlx5e_channel_stats_alloc(struct mlx5e_priv *priv, int ix, int cpu)
2644 {
2645 if (ix > priv->stats_nch) {
2646 netdev_warn(priv->netdev, "Unexpected channel stats index %d > %d\n", ix,
2647 priv->stats_nch);
2648 return -EINVAL;
2649 }
2650
2651 if (priv->channel_stats[ix])
2652 return 0;
2653
2654 /* Asymmetric dynamic memory allocation.
2655 * Freed in mlx5e_priv_arrays_free, not on channel closure.
2656 */
2657 netdev_dbg(priv->netdev, "Creating channel stats %d\n", ix);
2658 priv->channel_stats[ix] = kvzalloc_node(sizeof(**priv->channel_stats),
2659 GFP_KERNEL, cpu_to_node(cpu));
2660 if (!priv->channel_stats[ix])
2661 return -ENOMEM;
2662 priv->stats_nch++;
2663
2664 return 0;
2665 }
2666
mlx5e_trigger_napi_icosq(struct mlx5e_channel * c)2667 void mlx5e_trigger_napi_icosq(struct mlx5e_channel *c)
2668 {
2669 spin_lock_bh(&c->async_icosq_lock);
2670 mlx5e_trigger_irq(&c->async_icosq);
2671 spin_unlock_bh(&c->async_icosq_lock);
2672 }
2673
mlx5e_trigger_napi_sched(struct napi_struct * napi)2674 void mlx5e_trigger_napi_sched(struct napi_struct *napi)
2675 {
2676 local_bh_disable();
2677 napi_schedule(napi);
2678 local_bh_enable();
2679 }
2680
mlx5e_open_channel(struct mlx5e_priv * priv,int ix,struct mlx5e_params * params,struct xsk_buff_pool * xsk_pool,struct mlx5e_channel ** cp)2681 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
2682 struct mlx5e_params *params,
2683 struct xsk_buff_pool *xsk_pool,
2684 struct mlx5e_channel **cp)
2685 {
2686 struct net_device *netdev = priv->netdev;
2687 struct mlx5e_channel_param *cparam;
2688 struct mlx5_core_dev *mdev;
2689 struct mlx5e_xsk_param xsk;
2690 struct mlx5e_channel *c;
2691 unsigned int irq;
2692 int vec_ix;
2693 int cpu;
2694 int err;
2695
2696 mdev = mlx5_sd_ch_ix_get_dev(priv->mdev, ix);
2697 vec_ix = mlx5_sd_ch_ix_get_vec_ix(mdev, ix);
2698 cpu = mlx5_comp_vector_get_cpu(mdev, vec_ix);
2699
2700 err = mlx5_comp_irqn_get(mdev, vec_ix, &irq);
2701 if (err)
2702 return err;
2703
2704 err = mlx5e_channel_stats_alloc(priv, ix, cpu);
2705 if (err)
2706 return err;
2707
2708 c = kvzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
2709 cparam = kvzalloc(sizeof(*cparam), GFP_KERNEL);
2710 if (!c || !cparam) {
2711 err = -ENOMEM;
2712 goto err_free;
2713 }
2714
2715 err = mlx5e_build_channel_param(mdev, params, cparam);
2716 if (err)
2717 goto err_free;
2718
2719 c->priv = priv;
2720 c->mdev = mdev;
2721 c->tstamp = &priv->tstamp;
2722 c->ix = ix;
2723 c->vec_ix = vec_ix;
2724 c->sd_ix = mlx5_sd_ch_ix_get_dev_ix(mdev, ix);
2725 c->cpu = cpu;
2726 c->pdev = mlx5_core_dma_dev(mdev);
2727 c->netdev = priv->netdev;
2728 c->mkey_be = cpu_to_be32(mdev->mlx5e_res.hw_objs.mkey);
2729 c->num_tc = mlx5e_get_dcb_num_tc(params);
2730 c->xdp = !!params->xdp_prog;
2731 c->stats = &priv->channel_stats[ix]->ch;
2732 c->aff_mask = irq_get_effective_affinity_mask(irq);
2733 c->lag_port = mlx5e_enumerate_lag_port(mdev, ix);
2734
2735 netif_napi_add_config(netdev, &c->napi, mlx5e_napi_poll, ix);
2736 netif_napi_set_irq(&c->napi, irq);
2737
2738 err = mlx5e_open_queues(c, params, cparam);
2739 if (unlikely(err))
2740 goto err_napi_del;
2741
2742 if (xsk_pool) {
2743 mlx5e_build_xsk_param(xsk_pool, &xsk);
2744 err = mlx5e_open_xsk(priv, params, &xsk, xsk_pool, c);
2745 if (unlikely(err))
2746 goto err_close_queues;
2747 }
2748
2749 *cp = c;
2750
2751 kvfree(cparam);
2752 return 0;
2753
2754 err_close_queues:
2755 mlx5e_close_queues(c);
2756
2757 err_napi_del:
2758 netif_napi_del(&c->napi);
2759
2760 err_free:
2761 kvfree(cparam);
2762 kvfree(c);
2763
2764 return err;
2765 }
2766
mlx5e_activate_channel(struct mlx5e_channel * c)2767 static void mlx5e_activate_channel(struct mlx5e_channel *c)
2768 {
2769 int tc;
2770
2771 napi_enable(&c->napi);
2772
2773 for (tc = 0; tc < c->num_tc; tc++)
2774 mlx5e_activate_txqsq(&c->sq[tc]);
2775 mlx5e_activate_icosq(&c->icosq);
2776 mlx5e_activate_icosq(&c->async_icosq);
2777
2778 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
2779 mlx5e_activate_xsk(c);
2780 else
2781 mlx5e_activate_rq(&c->rq);
2782
2783 netif_queue_set_napi(c->netdev, c->ix, NETDEV_QUEUE_TYPE_RX, &c->napi);
2784 }
2785
mlx5e_deactivate_channel(struct mlx5e_channel * c)2786 static void mlx5e_deactivate_channel(struct mlx5e_channel *c)
2787 {
2788 int tc;
2789
2790 netif_queue_set_napi(c->netdev, c->ix, NETDEV_QUEUE_TYPE_RX, NULL);
2791
2792 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
2793 mlx5e_deactivate_xsk(c);
2794 else
2795 mlx5e_deactivate_rq(&c->rq);
2796
2797 mlx5e_deactivate_icosq(&c->async_icosq);
2798 mlx5e_deactivate_icosq(&c->icosq);
2799 for (tc = 0; tc < c->num_tc; tc++)
2800 mlx5e_deactivate_txqsq(&c->sq[tc]);
2801 mlx5e_qos_deactivate_queues(c);
2802
2803 napi_disable(&c->napi);
2804 }
2805
mlx5e_close_channel(struct mlx5e_channel * c)2806 static void mlx5e_close_channel(struct mlx5e_channel *c)
2807 {
2808 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
2809 mlx5e_close_xsk(c);
2810 mlx5e_close_queues(c);
2811 mlx5e_qos_close_queues(c);
2812 netif_napi_del(&c->napi);
2813
2814 kvfree(c);
2815 }
2816
mlx5e_open_channels(struct mlx5e_priv * priv,struct mlx5e_channels * chs)2817 int mlx5e_open_channels(struct mlx5e_priv *priv,
2818 struct mlx5e_channels *chs)
2819 {
2820 int err = -ENOMEM;
2821 int i;
2822
2823 chs->num = chs->params.num_channels;
2824
2825 chs->c = kcalloc(chs->num, sizeof(struct mlx5e_channel *), GFP_KERNEL);
2826 if (!chs->c)
2827 goto err_out;
2828
2829 for (i = 0; i < chs->num; i++) {
2830 struct xsk_buff_pool *xsk_pool = NULL;
2831
2832 if (chs->params.xdp_prog)
2833 xsk_pool = mlx5e_xsk_get_pool(&chs->params, chs->params.xsk, i);
2834
2835 err = mlx5e_open_channel(priv, i, &chs->params, xsk_pool, &chs->c[i]);
2836 if (err)
2837 goto err_close_channels;
2838 }
2839
2840 if (MLX5E_GET_PFLAG(&chs->params, MLX5E_PFLAG_TX_PORT_TS) || chs->params.ptp_rx) {
2841 err = mlx5e_ptp_open(priv, &chs->params, chs->c[0]->lag_port, &chs->ptp);
2842 if (err)
2843 goto err_close_channels;
2844 }
2845
2846 if (priv->htb) {
2847 err = mlx5e_qos_open_queues(priv, chs);
2848 if (err)
2849 goto err_close_ptp;
2850 }
2851
2852 mlx5e_health_channels_update(priv);
2853 return 0;
2854
2855 err_close_ptp:
2856 if (chs->ptp)
2857 mlx5e_ptp_close(chs->ptp);
2858
2859 err_close_channels:
2860 for (i--; i >= 0; i--)
2861 mlx5e_close_channel(chs->c[i]);
2862
2863 kfree(chs->c);
2864 err_out:
2865 chs->num = 0;
2866 return err;
2867 }
2868
mlx5e_activate_channels(struct mlx5e_priv * priv,struct mlx5e_channels * chs)2869 static void mlx5e_activate_channels(struct mlx5e_priv *priv, struct mlx5e_channels *chs)
2870 {
2871 int i;
2872
2873 for (i = 0; i < chs->num; i++)
2874 mlx5e_activate_channel(chs->c[i]);
2875
2876 if (priv->htb)
2877 mlx5e_qos_activate_queues(priv);
2878
2879 for (i = 0; i < chs->num; i++)
2880 mlx5e_trigger_napi_icosq(chs->c[i]);
2881
2882 if (chs->ptp)
2883 mlx5e_ptp_activate_channel(chs->ptp);
2884 }
2885
mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels * chs)2886 static int mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels *chs)
2887 {
2888 int err = 0;
2889 int i;
2890
2891 for (i = 0; i < chs->num; i++) {
2892 int timeout = err ? 0 : MLX5E_RQ_WQES_TIMEOUT;
2893 struct mlx5e_channel *c = chs->c[i];
2894
2895 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
2896 continue;
2897
2898 err |= mlx5e_wait_for_min_rx_wqes(&c->rq, timeout);
2899
2900 /* Don't wait on the XSK RQ, because the newer xdpsock sample
2901 * doesn't provide any Fill Ring entries at the setup stage.
2902 */
2903 }
2904
2905 return err ? -ETIMEDOUT : 0;
2906 }
2907
mlx5e_deactivate_channels(struct mlx5e_channels * chs)2908 static void mlx5e_deactivate_channels(struct mlx5e_channels *chs)
2909 {
2910 int i;
2911
2912 if (chs->ptp)
2913 mlx5e_ptp_deactivate_channel(chs->ptp);
2914
2915 for (i = 0; i < chs->num; i++)
2916 mlx5e_deactivate_channel(chs->c[i]);
2917 }
2918
mlx5e_close_channels(struct mlx5e_channels * chs)2919 void mlx5e_close_channels(struct mlx5e_channels *chs)
2920 {
2921 int i;
2922
2923 ASSERT_RTNL();
2924 if (chs->ptp) {
2925 mlx5e_ptp_close(chs->ptp);
2926 chs->ptp = NULL;
2927 }
2928 for (i = 0; i < chs->num; i++)
2929 mlx5e_close_channel(chs->c[i]);
2930
2931 kfree(chs->c);
2932 chs->num = 0;
2933 }
2934
mlx5e_modify_tirs_packet_merge(struct mlx5e_priv * priv)2935 static int mlx5e_modify_tirs_packet_merge(struct mlx5e_priv *priv)
2936 {
2937 struct mlx5e_rx_res *res = priv->rx_res;
2938
2939 return mlx5e_rx_res_packet_merge_set_param(res, &priv->channels.params.packet_merge);
2940 }
2941
2942 static MLX5E_DEFINE_PREACTIVATE_WRAPPER_CTX(mlx5e_modify_tirs_packet_merge);
2943
mlx5e_set_mtu(struct mlx5_core_dev * mdev,struct mlx5e_params * params,u16 mtu)2944 static int mlx5e_set_mtu(struct mlx5_core_dev *mdev,
2945 struct mlx5e_params *params, u16 mtu)
2946 {
2947 u16 hw_mtu = MLX5E_SW2HW_MTU(params, mtu);
2948 int err;
2949
2950 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
2951 if (err)
2952 return err;
2953
2954 /* Update vport context MTU */
2955 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
2956 return 0;
2957 }
2958
mlx5e_query_mtu(struct mlx5_core_dev * mdev,struct mlx5e_params * params,u16 * mtu)2959 static void mlx5e_query_mtu(struct mlx5_core_dev *mdev,
2960 struct mlx5e_params *params, u16 *mtu)
2961 {
2962 u16 hw_mtu = 0;
2963 int err;
2964
2965 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
2966 if (err || !hw_mtu) /* fallback to port oper mtu */
2967 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
2968
2969 *mtu = MLX5E_HW2SW_MTU(params, hw_mtu);
2970 }
2971
mlx5e_set_dev_port_mtu(struct mlx5e_priv * priv)2972 int mlx5e_set_dev_port_mtu(struct mlx5e_priv *priv)
2973 {
2974 struct mlx5e_params *params = &priv->channels.params;
2975 struct net_device *netdev = priv->netdev;
2976 struct mlx5_core_dev *mdev = priv->mdev;
2977 u16 mtu;
2978 int err;
2979
2980 err = mlx5e_set_mtu(mdev, params, params->sw_mtu);
2981 if (err)
2982 return err;
2983
2984 mlx5e_query_mtu(mdev, params, &mtu);
2985 if (mtu != params->sw_mtu)
2986 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
2987 __func__, mtu, params->sw_mtu);
2988
2989 params->sw_mtu = mtu;
2990 return 0;
2991 }
2992
2993 MLX5E_DEFINE_PREACTIVATE_WRAPPER_CTX(mlx5e_set_dev_port_mtu);
2994
mlx5e_set_netdev_mtu_boundaries(struct mlx5e_priv * priv)2995 void mlx5e_set_netdev_mtu_boundaries(struct mlx5e_priv *priv)
2996 {
2997 struct mlx5e_params *params = &priv->channels.params;
2998 struct net_device *netdev = priv->netdev;
2999 struct mlx5_core_dev *mdev = priv->mdev;
3000 u16 max_mtu;
3001
3002 /* MTU range: 68 - hw-specific max */
3003 netdev->min_mtu = ETH_MIN_MTU;
3004
3005 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
3006 netdev->max_mtu = min_t(unsigned int, MLX5E_HW2SW_MTU(params, max_mtu),
3007 ETH_MAX_MTU);
3008 }
3009
mlx5e_netdev_set_tcs(struct net_device * netdev,u16 nch,u8 ntc,struct netdev_tc_txq * tc_to_txq)3010 static int mlx5e_netdev_set_tcs(struct net_device *netdev, u16 nch, u8 ntc,
3011 struct netdev_tc_txq *tc_to_txq)
3012 {
3013 int tc, err;
3014
3015 netdev_reset_tc(netdev);
3016
3017 if (ntc == 1)
3018 return 0;
3019
3020 err = netdev_set_num_tc(netdev, ntc);
3021 if (err) {
3022 netdev_WARN(netdev, "netdev_set_num_tc failed (%d), ntc = %d\n", err, ntc);
3023 return err;
3024 }
3025
3026 for (tc = 0; tc < ntc; tc++) {
3027 u16 count, offset;
3028
3029 count = tc_to_txq[tc].count;
3030 offset = tc_to_txq[tc].offset;
3031 netdev_set_tc_queue(netdev, tc, count, offset);
3032 }
3033
3034 return 0;
3035 }
3036
mlx5e_update_tx_netdev_queues(struct mlx5e_priv * priv)3037 int mlx5e_update_tx_netdev_queues(struct mlx5e_priv *priv)
3038 {
3039 int nch, ntc, num_txqs, err;
3040 int qos_queues = 0;
3041
3042 if (priv->htb)
3043 qos_queues = mlx5e_htb_cur_leaf_nodes(priv->htb);
3044
3045 nch = priv->channels.params.num_channels;
3046 ntc = mlx5e_get_dcb_num_tc(&priv->channels.params);
3047 num_txqs = nch * ntc + qos_queues;
3048 if (MLX5E_GET_PFLAG(&priv->channels.params, MLX5E_PFLAG_TX_PORT_TS))
3049 num_txqs += ntc;
3050
3051 netdev_dbg(priv->netdev, "Setting num_txqs %d\n", num_txqs);
3052 err = netif_set_real_num_tx_queues(priv->netdev, num_txqs);
3053 if (err)
3054 netdev_warn(priv->netdev, "netif_set_real_num_tx_queues failed, %d\n", err);
3055
3056 return err;
3057 }
3058
mlx5e_set_default_xps_cpumasks(struct mlx5e_priv * priv,struct mlx5e_params * params)3059 static void mlx5e_set_default_xps_cpumasks(struct mlx5e_priv *priv,
3060 struct mlx5e_params *params)
3061 {
3062 int ix;
3063
3064 for (ix = 0; ix < params->num_channels; ix++) {
3065 int num_comp_vectors, irq, vec_ix;
3066 struct mlx5_core_dev *mdev;
3067
3068 mdev = mlx5_sd_ch_ix_get_dev(priv->mdev, ix);
3069 num_comp_vectors = mlx5_comp_vectors_max(mdev);
3070 cpumask_clear(priv->scratchpad.cpumask);
3071 vec_ix = mlx5_sd_ch_ix_get_vec_ix(mdev, ix);
3072
3073 for (irq = vec_ix; irq < num_comp_vectors; irq += params->num_channels) {
3074 int cpu = mlx5_comp_vector_get_cpu(mdev, irq);
3075
3076 cpumask_set_cpu(cpu, priv->scratchpad.cpumask);
3077 }
3078
3079 netif_set_xps_queue(priv->netdev, priv->scratchpad.cpumask, ix);
3080 }
3081 }
3082
mlx5e_update_tc_and_tx_queues(struct mlx5e_priv * priv)3083 static int mlx5e_update_tc_and_tx_queues(struct mlx5e_priv *priv)
3084 {
3085 struct netdev_tc_txq old_tc_to_txq[TC_MAX_QUEUE], *tc_to_txq;
3086 struct net_device *netdev = priv->netdev;
3087 int old_num_txqs, old_ntc;
3088 int nch, ntc;
3089 int err;
3090 int i;
3091
3092 old_num_txqs = netdev->real_num_tx_queues;
3093 old_ntc = netdev->num_tc ? : 1;
3094 for (i = 0; i < ARRAY_SIZE(old_tc_to_txq); i++)
3095 old_tc_to_txq[i] = netdev->tc_to_txq[i];
3096
3097 nch = priv->channels.params.num_channels;
3098 ntc = priv->channels.params.mqprio.num_tc;
3099 tc_to_txq = priv->channels.params.mqprio.tc_to_txq;
3100
3101 err = mlx5e_netdev_set_tcs(netdev, nch, ntc, tc_to_txq);
3102 if (err)
3103 goto err_out;
3104 err = mlx5e_update_tx_netdev_queues(priv);
3105 if (err)
3106 goto err_tcs;
3107 mlx5e_set_default_xps_cpumasks(priv, &priv->channels.params);
3108
3109 return 0;
3110
3111 err_tcs:
3112 WARN_ON_ONCE(mlx5e_netdev_set_tcs(netdev, old_num_txqs / old_ntc, old_ntc,
3113 old_tc_to_txq));
3114 err_out:
3115 return err;
3116 }
3117
3118 MLX5E_DEFINE_PREACTIVATE_WRAPPER_CTX(mlx5e_update_tc_and_tx_queues);
3119
mlx5e_num_channels_changed(struct mlx5e_priv * priv)3120 static int mlx5e_num_channels_changed(struct mlx5e_priv *priv)
3121 {
3122 u16 count = priv->channels.params.num_channels;
3123 struct net_device *netdev = priv->netdev;
3124 int old_num_rxqs;
3125 int err;
3126
3127 old_num_rxqs = netdev->real_num_rx_queues;
3128 err = netif_set_real_num_rx_queues(netdev, count);
3129 if (err) {
3130 netdev_warn(netdev, "%s: netif_set_real_num_rx_queues failed, %d\n",
3131 __func__, err);
3132 return err;
3133 }
3134 err = mlx5e_update_tc_and_tx_queues(priv);
3135 if (err) {
3136 /* mlx5e_update_tc_and_tx_queues can fail if channels or TCs number increases.
3137 * Since channel number changed, it increased. That means, the call to
3138 * netif_set_real_num_rx_queues below should not fail, because it
3139 * decreases the number of RX queues.
3140 */
3141 WARN_ON_ONCE(netif_set_real_num_rx_queues(netdev, old_num_rxqs));
3142 return err;
3143 }
3144
3145 /* This function may be called on attach, before priv->rx_res is created. */
3146 if (priv->rx_res) {
3147 mlx5e_rx_res_rss_update_num_channels(priv->rx_res, count);
3148
3149 if (!netif_is_rxfh_configured(priv->netdev))
3150 mlx5e_rx_res_rss_set_indir_uniform(priv->rx_res, count);
3151 }
3152
3153 return 0;
3154 }
3155
3156 MLX5E_DEFINE_PREACTIVATE_WRAPPER_CTX(mlx5e_num_channels_changed);
3157
mlx5e_build_txq_maps(struct mlx5e_priv * priv)3158 static void mlx5e_build_txq_maps(struct mlx5e_priv *priv)
3159 {
3160 int i, ch, tc, num_tc;
3161
3162 ch = priv->channels.num;
3163 num_tc = mlx5e_get_dcb_num_tc(&priv->channels.params);
3164
3165 for (i = 0; i < ch; i++) {
3166 for (tc = 0; tc < num_tc; tc++) {
3167 struct mlx5e_channel *c = priv->channels.c[i];
3168 struct mlx5e_txqsq *sq = &c->sq[tc];
3169
3170 priv->txq2sq[sq->txq_ix] = sq;
3171 priv->txq2sq_stats[sq->txq_ix] = sq->stats;
3172 }
3173 }
3174
3175 if (!priv->channels.ptp)
3176 goto out;
3177
3178 if (!test_bit(MLX5E_PTP_STATE_TX, priv->channels.ptp->state))
3179 goto out;
3180
3181 for (tc = 0; tc < num_tc; tc++) {
3182 struct mlx5e_ptp *c = priv->channels.ptp;
3183 struct mlx5e_txqsq *sq = &c->ptpsq[tc].txqsq;
3184
3185 priv->txq2sq[sq->txq_ix] = sq;
3186 priv->txq2sq_stats[sq->txq_ix] = sq->stats;
3187 }
3188
3189 out:
3190 /* Make the change to txq2sq visible before the queue is started.
3191 * As mlx5e_xmit runs under a spinlock, there is an implicit ACQUIRE,
3192 * which pairs with this barrier.
3193 */
3194 smp_wmb();
3195 }
3196
mlx5e_activate_priv_channels(struct mlx5e_priv * priv)3197 void mlx5e_activate_priv_channels(struct mlx5e_priv *priv)
3198 {
3199 mlx5e_build_txq_maps(priv);
3200 mlx5e_activate_channels(priv, &priv->channels);
3201 mlx5e_xdp_tx_enable(priv);
3202
3203 /* dev_watchdog() wants all TX queues to be started when the carrier is
3204 * OK, including the ones in range real_num_tx_queues..num_tx_queues-1.
3205 * Make it happy to avoid TX timeout false alarms.
3206 */
3207 netif_tx_start_all_queues(priv->netdev);
3208
3209 if (mlx5e_is_vport_rep(priv))
3210 mlx5e_rep_activate_channels(priv);
3211
3212 set_bit(MLX5E_STATE_CHANNELS_ACTIVE, &priv->state);
3213
3214 mlx5e_wait_channels_min_rx_wqes(&priv->channels);
3215
3216 if (priv->rx_res)
3217 mlx5e_rx_res_channels_activate(priv->rx_res, &priv->channels);
3218 }
3219
mlx5e_cancel_tx_timeout_work(struct mlx5e_priv * priv)3220 static void mlx5e_cancel_tx_timeout_work(struct mlx5e_priv *priv)
3221 {
3222 WARN_ON_ONCE(test_bit(MLX5E_STATE_CHANNELS_ACTIVE, &priv->state));
3223 if (current_work() != &priv->tx_timeout_work)
3224 cancel_work_sync(&priv->tx_timeout_work);
3225 }
3226
mlx5e_deactivate_priv_channels(struct mlx5e_priv * priv)3227 void mlx5e_deactivate_priv_channels(struct mlx5e_priv *priv)
3228 {
3229 if (priv->rx_res)
3230 mlx5e_rx_res_channels_deactivate(priv->rx_res);
3231
3232 clear_bit(MLX5E_STATE_CHANNELS_ACTIVE, &priv->state);
3233 mlx5e_cancel_tx_timeout_work(priv);
3234
3235 if (mlx5e_is_vport_rep(priv))
3236 mlx5e_rep_deactivate_channels(priv);
3237
3238 /* The results of ndo_select_queue are unreliable, while netdev config
3239 * is being changed (real_num_tx_queues, num_tc). Stop all queues to
3240 * prevent ndo_start_xmit from being called, so that it can assume that
3241 * the selected queue is always valid.
3242 */
3243 netif_tx_disable(priv->netdev);
3244
3245 mlx5e_xdp_tx_disable(priv);
3246 mlx5e_deactivate_channels(&priv->channels);
3247 }
3248
mlx5e_switch_priv_params(struct mlx5e_priv * priv,struct mlx5e_params * new_params,mlx5e_fp_preactivate preactivate,void * context)3249 static int mlx5e_switch_priv_params(struct mlx5e_priv *priv,
3250 struct mlx5e_params *new_params,
3251 mlx5e_fp_preactivate preactivate,
3252 void *context)
3253 {
3254 struct mlx5e_params old_params;
3255
3256 old_params = priv->channels.params;
3257 priv->channels.params = *new_params;
3258
3259 if (preactivate) {
3260 int err;
3261
3262 err = preactivate(priv, context);
3263 if (err) {
3264 priv->channels.params = old_params;
3265 return err;
3266 }
3267 }
3268
3269 return 0;
3270 }
3271
mlx5e_switch_priv_channels(struct mlx5e_priv * priv,struct mlx5e_channels * new_chs,mlx5e_fp_preactivate preactivate,void * context)3272 static int mlx5e_switch_priv_channels(struct mlx5e_priv *priv,
3273 struct mlx5e_channels *new_chs,
3274 mlx5e_fp_preactivate preactivate,
3275 void *context)
3276 {
3277 struct net_device *netdev = priv->netdev;
3278 struct mlx5e_channels old_chs;
3279 int carrier_ok;
3280 int err = 0;
3281
3282 carrier_ok = netif_carrier_ok(netdev);
3283 netif_carrier_off(netdev);
3284
3285 mlx5e_deactivate_priv_channels(priv);
3286
3287 old_chs = priv->channels;
3288 priv->channels = *new_chs;
3289
3290 /* New channels are ready to roll, call the preactivate hook if needed
3291 * to modify HW settings or update kernel parameters.
3292 */
3293 if (preactivate) {
3294 err = preactivate(priv, context);
3295 if (err) {
3296 priv->channels = old_chs;
3297 goto out;
3298 }
3299 }
3300
3301 mlx5e_close_channels(&old_chs);
3302 priv->profile->update_rx(priv);
3303
3304 mlx5e_selq_apply(&priv->selq);
3305 out:
3306 mlx5e_activate_priv_channels(priv);
3307
3308 /* return carrier back if needed */
3309 if (carrier_ok)
3310 netif_carrier_on(netdev);
3311
3312 return err;
3313 }
3314
mlx5e_safe_switch_params(struct mlx5e_priv * priv,struct mlx5e_params * params,mlx5e_fp_preactivate preactivate,void * context,bool reset)3315 int mlx5e_safe_switch_params(struct mlx5e_priv *priv,
3316 struct mlx5e_params *params,
3317 mlx5e_fp_preactivate preactivate,
3318 void *context, bool reset)
3319 {
3320 struct mlx5e_channels *new_chs;
3321 int err;
3322
3323 reset &= test_bit(MLX5E_STATE_OPENED, &priv->state);
3324 if (!reset)
3325 return mlx5e_switch_priv_params(priv, params, preactivate, context);
3326
3327 new_chs = kzalloc(sizeof(*new_chs), GFP_KERNEL);
3328 if (!new_chs)
3329 return -ENOMEM;
3330 new_chs->params = *params;
3331
3332 mlx5e_selq_prepare_params(&priv->selq, &new_chs->params);
3333
3334 err = mlx5e_open_channels(priv, new_chs);
3335 if (err)
3336 goto err_cancel_selq;
3337
3338 err = mlx5e_switch_priv_channels(priv, new_chs, preactivate, context);
3339 if (err)
3340 goto err_close;
3341
3342 kfree(new_chs);
3343 return 0;
3344
3345 err_close:
3346 mlx5e_close_channels(new_chs);
3347
3348 err_cancel_selq:
3349 mlx5e_selq_cancel(&priv->selq);
3350 kfree(new_chs);
3351 return err;
3352 }
3353
mlx5e_safe_reopen_channels(struct mlx5e_priv * priv)3354 int mlx5e_safe_reopen_channels(struct mlx5e_priv *priv)
3355 {
3356 return mlx5e_safe_switch_params(priv, &priv->channels.params, NULL, NULL, true);
3357 }
3358
mlx5e_timestamp_init(struct mlx5e_priv * priv)3359 void mlx5e_timestamp_init(struct mlx5e_priv *priv)
3360 {
3361 priv->tstamp.tx_type = HWTSTAMP_TX_OFF;
3362 priv->tstamp.rx_filter = HWTSTAMP_FILTER_NONE;
3363 }
3364
mlx5e_modify_admin_state(struct mlx5_core_dev * mdev,enum mlx5_port_status state)3365 static void mlx5e_modify_admin_state(struct mlx5_core_dev *mdev,
3366 enum mlx5_port_status state)
3367 {
3368 struct mlx5_eswitch *esw = mdev->priv.eswitch;
3369 int vport_admin_state;
3370
3371 mlx5_set_port_admin_status(mdev, state);
3372
3373 if (mlx5_eswitch_mode(mdev) == MLX5_ESWITCH_OFFLOADS ||
3374 !MLX5_CAP_GEN(mdev, uplink_follow))
3375 return;
3376
3377 if (state == MLX5_PORT_UP)
3378 vport_admin_state = MLX5_VPORT_ADMIN_STATE_AUTO;
3379 else
3380 vport_admin_state = MLX5_VPORT_ADMIN_STATE_DOWN;
3381
3382 mlx5_eswitch_set_vport_state(esw, MLX5_VPORT_UPLINK, vport_admin_state);
3383 }
3384
mlx5e_open_locked(struct net_device * netdev)3385 int mlx5e_open_locked(struct net_device *netdev)
3386 {
3387 struct mlx5e_priv *priv = netdev_priv(netdev);
3388 int err;
3389
3390 mlx5e_selq_prepare_params(&priv->selq, &priv->channels.params);
3391
3392 set_bit(MLX5E_STATE_OPENED, &priv->state);
3393
3394 err = mlx5e_open_channels(priv, &priv->channels);
3395 if (err)
3396 goto err_clear_state_opened_flag;
3397
3398 err = priv->profile->update_rx(priv);
3399 if (err)
3400 goto err_close_channels;
3401
3402 mlx5e_selq_apply(&priv->selq);
3403 mlx5e_activate_priv_channels(priv);
3404 mlx5e_apply_traps(priv, true);
3405 if (priv->profile->update_carrier)
3406 priv->profile->update_carrier(priv);
3407
3408 mlx5e_queue_update_stats(priv);
3409 return 0;
3410
3411 err_close_channels:
3412 mlx5e_close_channels(&priv->channels);
3413 err_clear_state_opened_flag:
3414 clear_bit(MLX5E_STATE_OPENED, &priv->state);
3415 mlx5e_selq_cancel(&priv->selq);
3416 return err;
3417 }
3418
mlx5e_open(struct net_device * netdev)3419 int mlx5e_open(struct net_device *netdev)
3420 {
3421 struct mlx5e_priv *priv = netdev_priv(netdev);
3422 int err;
3423
3424 mutex_lock(&priv->state_lock);
3425 err = mlx5e_open_locked(netdev);
3426 if (!err)
3427 mlx5e_modify_admin_state(priv->mdev, MLX5_PORT_UP);
3428 mutex_unlock(&priv->state_lock);
3429
3430 return err;
3431 }
3432
mlx5e_close_locked(struct net_device * netdev)3433 int mlx5e_close_locked(struct net_device *netdev)
3434 {
3435 struct mlx5e_priv *priv = netdev_priv(netdev);
3436
3437 /* May already be CLOSED in case a previous configuration operation
3438 * (e.g RX/TX queue size change) that involves close&open failed.
3439 */
3440 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3441 return 0;
3442
3443 mlx5e_apply_traps(priv, false);
3444 clear_bit(MLX5E_STATE_OPENED, &priv->state);
3445
3446 netif_carrier_off(priv->netdev);
3447 mlx5e_deactivate_priv_channels(priv);
3448 mlx5e_close_channels(&priv->channels);
3449
3450 return 0;
3451 }
3452
mlx5e_close(struct net_device * netdev)3453 int mlx5e_close(struct net_device *netdev)
3454 {
3455 struct mlx5e_priv *priv = netdev_priv(netdev);
3456 int err;
3457
3458 if (!netif_device_present(netdev))
3459 return -ENODEV;
3460
3461 mutex_lock(&priv->state_lock);
3462 mlx5e_modify_admin_state(priv->mdev, MLX5_PORT_DOWN);
3463 err = mlx5e_close_locked(netdev);
3464 mutex_unlock(&priv->state_lock);
3465
3466 return err;
3467 }
3468
mlx5e_free_drop_rq(struct mlx5e_rq * rq)3469 static void mlx5e_free_drop_rq(struct mlx5e_rq *rq)
3470 {
3471 mlx5_wq_destroy(&rq->wq_ctrl);
3472 }
3473
mlx5e_alloc_drop_rq(struct mlx5_core_dev * mdev,struct mlx5e_rq * rq,struct mlx5e_rq_param * param)3474 static int mlx5e_alloc_drop_rq(struct mlx5_core_dev *mdev,
3475 struct mlx5e_rq *rq,
3476 struct mlx5e_rq_param *param)
3477 {
3478 void *rqc = param->rqc;
3479 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
3480 int err;
3481
3482 param->wq.db_numa_node = param->wq.buf_numa_node;
3483
3484 err = mlx5_wq_cyc_create(mdev, ¶m->wq, rqc_wq, &rq->wqe.wq,
3485 &rq->wq_ctrl);
3486 if (err)
3487 return err;
3488
3489 /* Mark as unused given "Drop-RQ" packets never reach XDP */
3490 xdp_rxq_info_unused(&rq->xdp_rxq);
3491
3492 rq->mdev = mdev;
3493
3494 return 0;
3495 }
3496
mlx5e_alloc_drop_cq(struct mlx5e_priv * priv,struct mlx5e_cq * cq,struct mlx5e_cq_param * param)3497 static int mlx5e_alloc_drop_cq(struct mlx5e_priv *priv,
3498 struct mlx5e_cq *cq,
3499 struct mlx5e_cq_param *param)
3500 {
3501 struct mlx5_core_dev *mdev = priv->mdev;
3502
3503 param->wq.buf_numa_node = dev_to_node(mlx5_core_dma_dev(mdev));
3504 param->wq.db_numa_node = dev_to_node(mlx5_core_dma_dev(mdev));
3505
3506 return mlx5e_alloc_cq_common(priv->mdev, priv->netdev, priv->wq, param, cq);
3507 }
3508
mlx5e_open_drop_rq(struct mlx5e_priv * priv,struct mlx5e_rq * drop_rq)3509 int mlx5e_open_drop_rq(struct mlx5e_priv *priv,
3510 struct mlx5e_rq *drop_rq)
3511 {
3512 struct mlx5_core_dev *mdev = priv->mdev;
3513 struct mlx5e_cq_param cq_param = {};
3514 struct mlx5e_rq_param rq_param = {};
3515 struct mlx5e_cq *cq = &drop_rq->cq;
3516 int err;
3517
3518 mlx5e_build_drop_rq_param(mdev, &rq_param);
3519
3520 err = mlx5e_alloc_drop_cq(priv, cq, &cq_param);
3521 if (err)
3522 return err;
3523
3524 err = mlx5e_create_cq(cq, &cq_param);
3525 if (err)
3526 goto err_free_cq;
3527
3528 err = mlx5e_alloc_drop_rq(mdev, drop_rq, &rq_param);
3529 if (err)
3530 goto err_destroy_cq;
3531
3532 err = mlx5e_create_rq(drop_rq, &rq_param, priv->drop_rq_q_counter);
3533 if (err)
3534 goto err_free_rq;
3535
3536 err = mlx5e_modify_rq_state(drop_rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
3537 if (err)
3538 mlx5_core_warn(priv->mdev, "modify_rq_state failed, rx_if_down_packets won't be counted %d\n", err);
3539
3540 return 0;
3541
3542 err_free_rq:
3543 mlx5e_free_drop_rq(drop_rq);
3544
3545 err_destroy_cq:
3546 mlx5e_destroy_cq(cq);
3547
3548 err_free_cq:
3549 mlx5e_free_cq(cq);
3550
3551 return err;
3552 }
3553
mlx5e_close_drop_rq(struct mlx5e_rq * drop_rq)3554 void mlx5e_close_drop_rq(struct mlx5e_rq *drop_rq)
3555 {
3556 mlx5e_destroy_rq(drop_rq);
3557 mlx5e_free_drop_rq(drop_rq);
3558 mlx5e_destroy_cq(&drop_rq->cq);
3559 mlx5e_free_cq(&drop_rq->cq);
3560 }
3561
mlx5e_cleanup_nic_tx(struct mlx5e_priv * priv)3562 static void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
3563 {
3564 if (priv->mqprio_rl) {
3565 mlx5e_mqprio_rl_cleanup(priv->mqprio_rl);
3566 mlx5e_mqprio_rl_free(priv->mqprio_rl);
3567 priv->mqprio_rl = NULL;
3568 }
3569 mlx5e_accel_cleanup_tx(priv);
3570 }
3571
mlx5e_modify_channels_vsd(struct mlx5e_channels * chs,bool vsd)3572 static int mlx5e_modify_channels_vsd(struct mlx5e_channels *chs, bool vsd)
3573 {
3574 int err;
3575 int i;
3576
3577 for (i = 0; i < chs->num; i++) {
3578 err = mlx5e_modify_rq_vsd(&chs->c[i]->rq, vsd);
3579 if (err)
3580 return err;
3581 }
3582 if (chs->ptp && test_bit(MLX5E_PTP_STATE_RX, chs->ptp->state))
3583 return mlx5e_modify_rq_vsd(&chs->ptp->rq, vsd);
3584
3585 return 0;
3586 }
3587
mlx5e_mqprio_build_default_tc_to_txq(struct netdev_tc_txq * tc_to_txq,int ntc,int nch)3588 static void mlx5e_mqprio_build_default_tc_to_txq(struct netdev_tc_txq *tc_to_txq,
3589 int ntc, int nch)
3590 {
3591 int tc;
3592
3593 memset(tc_to_txq, 0, sizeof(*tc_to_txq) * TC_MAX_QUEUE);
3594
3595 /* Map netdev TCs to offset 0.
3596 * We have our own UP to TXQ mapping for DCB mode of QoS
3597 */
3598 for (tc = 0; tc < ntc; tc++) {
3599 tc_to_txq[tc] = (struct netdev_tc_txq) {
3600 .count = nch,
3601 .offset = 0,
3602 };
3603 }
3604 }
3605
mlx5e_mqprio_build_tc_to_txq(struct netdev_tc_txq * tc_to_txq,struct tc_mqprio_qopt * qopt)3606 static void mlx5e_mqprio_build_tc_to_txq(struct netdev_tc_txq *tc_to_txq,
3607 struct tc_mqprio_qopt *qopt)
3608 {
3609 int tc;
3610
3611 for (tc = 0; tc < TC_MAX_QUEUE; tc++) {
3612 tc_to_txq[tc] = (struct netdev_tc_txq) {
3613 .count = qopt->count[tc],
3614 .offset = qopt->offset[tc],
3615 };
3616 }
3617 }
3618
mlx5e_params_mqprio_dcb_set(struct mlx5e_params * params,u8 num_tc)3619 static void mlx5e_params_mqprio_dcb_set(struct mlx5e_params *params, u8 num_tc)
3620 {
3621 params->mqprio.mode = TC_MQPRIO_MODE_DCB;
3622 params->mqprio.num_tc = num_tc;
3623 mlx5e_mqprio_build_default_tc_to_txq(params->mqprio.tc_to_txq, num_tc,
3624 params->num_channels);
3625 }
3626
mlx5e_mqprio_rl_update_params(struct mlx5e_params * params,struct mlx5e_mqprio_rl * rl)3627 static void mlx5e_mqprio_rl_update_params(struct mlx5e_params *params,
3628 struct mlx5e_mqprio_rl *rl)
3629 {
3630 int tc;
3631
3632 for (tc = 0; tc < TC_MAX_QUEUE; tc++) {
3633 u32 hw_id = 0;
3634
3635 if (rl)
3636 mlx5e_mqprio_rl_get_node_hw_id(rl, tc, &hw_id);
3637 params->mqprio.channel.hw_id[tc] = hw_id;
3638 }
3639 }
3640
mlx5e_params_mqprio_channel_set(struct mlx5e_params * params,struct tc_mqprio_qopt_offload * mqprio,struct mlx5e_mqprio_rl * rl)3641 static void mlx5e_params_mqprio_channel_set(struct mlx5e_params *params,
3642 struct tc_mqprio_qopt_offload *mqprio,
3643 struct mlx5e_mqprio_rl *rl)
3644 {
3645 int tc;
3646
3647 params->mqprio.mode = TC_MQPRIO_MODE_CHANNEL;
3648 params->mqprio.num_tc = mqprio->qopt.num_tc;
3649
3650 for (tc = 0; tc < TC_MAX_QUEUE; tc++)
3651 params->mqprio.channel.max_rate[tc] = mqprio->max_rate[tc];
3652
3653 mlx5e_mqprio_rl_update_params(params, rl);
3654 mlx5e_mqprio_build_tc_to_txq(params->mqprio.tc_to_txq, &mqprio->qopt);
3655 }
3656
mlx5e_params_mqprio_reset(struct mlx5e_params * params)3657 static void mlx5e_params_mqprio_reset(struct mlx5e_params *params)
3658 {
3659 mlx5e_params_mqprio_dcb_set(params, 1);
3660 }
3661
mlx5e_setup_tc_mqprio_dcb(struct mlx5e_priv * priv,struct tc_mqprio_qopt * mqprio)3662 static int mlx5e_setup_tc_mqprio_dcb(struct mlx5e_priv *priv,
3663 struct tc_mqprio_qopt *mqprio)
3664 {
3665 struct mlx5e_params new_params;
3666 u8 tc = mqprio->num_tc;
3667 int err;
3668
3669 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
3670
3671 if (tc && tc != MLX5_MAX_NUM_TC)
3672 return -EINVAL;
3673
3674 new_params = priv->channels.params;
3675 mlx5e_params_mqprio_dcb_set(&new_params, tc ? tc : 1);
3676
3677 err = mlx5e_safe_switch_params(priv, &new_params,
3678 mlx5e_update_tc_and_tx_queues_ctx, NULL, true);
3679
3680 if (!err && priv->mqprio_rl) {
3681 mlx5e_mqprio_rl_cleanup(priv->mqprio_rl);
3682 mlx5e_mqprio_rl_free(priv->mqprio_rl);
3683 priv->mqprio_rl = NULL;
3684 }
3685
3686 priv->max_opened_tc = max_t(u8, priv->max_opened_tc,
3687 mlx5e_get_dcb_num_tc(&priv->channels.params));
3688 return err;
3689 }
3690
mlx5e_mqprio_channel_validate(struct mlx5e_priv * priv,struct tc_mqprio_qopt_offload * mqprio)3691 static int mlx5e_mqprio_channel_validate(struct mlx5e_priv *priv,
3692 struct tc_mqprio_qopt_offload *mqprio)
3693 {
3694 struct net_device *netdev = priv->netdev;
3695 struct mlx5e_ptp *ptp_channel;
3696 int agg_count = 0;
3697 int i;
3698
3699 ptp_channel = priv->channels.ptp;
3700 if (ptp_channel && test_bit(MLX5E_PTP_STATE_TX, ptp_channel->state)) {
3701 netdev_err(netdev,
3702 "Cannot activate MQPRIO mode channel since it conflicts with TX port TS\n");
3703 return -EINVAL;
3704 }
3705
3706 if (mqprio->qopt.offset[0] != 0 || mqprio->qopt.num_tc < 1 ||
3707 mqprio->qopt.num_tc > MLX5E_MAX_NUM_MQPRIO_CH_TC)
3708 return -EINVAL;
3709
3710 for (i = 0; i < mqprio->qopt.num_tc; i++) {
3711 if (!mqprio->qopt.count[i]) {
3712 netdev_err(netdev, "Zero size for queue-group (%d) is not supported\n", i);
3713 return -EINVAL;
3714 }
3715 if (mqprio->min_rate[i]) {
3716 netdev_err(netdev, "Min tx rate is not supported\n");
3717 return -EINVAL;
3718 }
3719
3720 if (mqprio->max_rate[i]) {
3721 int err;
3722
3723 err = mlx5e_qos_bytes_rate_check(priv->mdev, mqprio->max_rate[i]);
3724 if (err)
3725 return err;
3726 }
3727
3728 if (mqprio->qopt.offset[i] != agg_count) {
3729 netdev_err(netdev, "Discontinuous queues config is not supported\n");
3730 return -EINVAL;
3731 }
3732 agg_count += mqprio->qopt.count[i];
3733 }
3734
3735 if (priv->channels.params.num_channels != agg_count) {
3736 netdev_err(netdev, "Num of queues (%d) does not match available (%d)\n",
3737 agg_count, priv->channels.params.num_channels);
3738 return -EINVAL;
3739 }
3740
3741 return 0;
3742 }
3743
mlx5e_mqprio_rate_limit(u8 num_tc,u64 max_rate[])3744 static bool mlx5e_mqprio_rate_limit(u8 num_tc, u64 max_rate[])
3745 {
3746 int tc;
3747
3748 for (tc = 0; tc < num_tc; tc++)
3749 if (max_rate[tc])
3750 return true;
3751 return false;
3752 }
3753
mlx5e_mqprio_rl_create(struct mlx5_core_dev * mdev,u8 num_tc,u64 max_rate[])3754 static struct mlx5e_mqprio_rl *mlx5e_mqprio_rl_create(struct mlx5_core_dev *mdev,
3755 u8 num_tc, u64 max_rate[])
3756 {
3757 struct mlx5e_mqprio_rl *rl;
3758 int err;
3759
3760 if (!mlx5e_mqprio_rate_limit(num_tc, max_rate))
3761 return NULL;
3762
3763 rl = mlx5e_mqprio_rl_alloc();
3764 if (!rl)
3765 return ERR_PTR(-ENOMEM);
3766
3767 err = mlx5e_mqprio_rl_init(rl, mdev, num_tc, max_rate);
3768 if (err) {
3769 mlx5e_mqprio_rl_free(rl);
3770 return ERR_PTR(err);
3771 }
3772
3773 return rl;
3774 }
3775
mlx5e_setup_tc_mqprio_channel(struct mlx5e_priv * priv,struct tc_mqprio_qopt_offload * mqprio)3776 static int mlx5e_setup_tc_mqprio_channel(struct mlx5e_priv *priv,
3777 struct tc_mqprio_qopt_offload *mqprio)
3778 {
3779 struct mlx5e_params new_params;
3780 struct mlx5e_mqprio_rl *rl;
3781 int err;
3782
3783 err = mlx5e_mqprio_channel_validate(priv, mqprio);
3784 if (err)
3785 return err;
3786
3787 rl = mlx5e_mqprio_rl_create(priv->mdev, mqprio->qopt.num_tc, mqprio->max_rate);
3788 if (IS_ERR(rl))
3789 return PTR_ERR(rl);
3790
3791 new_params = priv->channels.params;
3792 mlx5e_params_mqprio_channel_set(&new_params, mqprio, rl);
3793
3794 err = mlx5e_safe_switch_params(priv, &new_params,
3795 mlx5e_update_tc_and_tx_queues_ctx, NULL, true);
3796 if (err) {
3797 if (rl) {
3798 mlx5e_mqprio_rl_cleanup(rl);
3799 mlx5e_mqprio_rl_free(rl);
3800 }
3801 return err;
3802 }
3803
3804 if (priv->mqprio_rl) {
3805 mlx5e_mqprio_rl_cleanup(priv->mqprio_rl);
3806 mlx5e_mqprio_rl_free(priv->mqprio_rl);
3807 }
3808 priv->mqprio_rl = rl;
3809
3810 return 0;
3811 }
3812
mlx5e_setup_tc_mqprio(struct mlx5e_priv * priv,struct tc_mqprio_qopt_offload * mqprio)3813 static int mlx5e_setup_tc_mqprio(struct mlx5e_priv *priv,
3814 struct tc_mqprio_qopt_offload *mqprio)
3815 {
3816 /* MQPRIO is another toplevel qdisc that can't be attached
3817 * simultaneously with the offloaded HTB.
3818 */
3819 if (WARN_ON(mlx5e_selq_is_htb_enabled(&priv->selq)))
3820 return -EINVAL;
3821
3822 switch (mqprio->mode) {
3823 case TC_MQPRIO_MODE_DCB:
3824 return mlx5e_setup_tc_mqprio_dcb(priv, &mqprio->qopt);
3825 case TC_MQPRIO_MODE_CHANNEL:
3826 return mlx5e_setup_tc_mqprio_channel(priv, mqprio);
3827 default:
3828 return -EOPNOTSUPP;
3829 }
3830 }
3831
3832 static LIST_HEAD(mlx5e_block_cb_list);
3833
mlx5e_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)3834 static int mlx5e_setup_tc(struct net_device *dev, enum tc_setup_type type,
3835 void *type_data)
3836 {
3837 struct mlx5e_priv *priv = netdev_priv(dev);
3838 bool tc_unbind = false;
3839 int err;
3840
3841 if (type == TC_SETUP_BLOCK &&
3842 ((struct flow_block_offload *)type_data)->command == FLOW_BLOCK_UNBIND)
3843 tc_unbind = true;
3844
3845 if (!netif_device_present(dev) && !tc_unbind)
3846 return -ENODEV;
3847
3848 switch (type) {
3849 case TC_SETUP_BLOCK: {
3850 struct flow_block_offload *f = type_data;
3851
3852 f->unlocked_driver_cb = true;
3853 return flow_block_cb_setup_simple(type_data,
3854 &mlx5e_block_cb_list,
3855 mlx5e_setup_tc_block_cb,
3856 priv, priv, true);
3857 }
3858 case TC_SETUP_QDISC_MQPRIO:
3859 mutex_lock(&priv->state_lock);
3860 err = mlx5e_setup_tc_mqprio(priv, type_data);
3861 mutex_unlock(&priv->state_lock);
3862 return err;
3863 case TC_SETUP_QDISC_HTB:
3864 mutex_lock(&priv->state_lock);
3865 err = mlx5e_htb_setup_tc(priv, type_data);
3866 mutex_unlock(&priv->state_lock);
3867 return err;
3868 default:
3869 return -EOPNOTSUPP;
3870 }
3871 }
3872
mlx5e_fold_sw_stats64(struct mlx5e_priv * priv,struct rtnl_link_stats64 * s)3873 void mlx5e_fold_sw_stats64(struct mlx5e_priv *priv, struct rtnl_link_stats64 *s)
3874 {
3875 int i;
3876
3877 for (i = 0; i < priv->stats_nch; i++) {
3878 struct mlx5e_channel_stats *channel_stats = priv->channel_stats[i];
3879 struct mlx5e_rq_stats *xskrq_stats = &channel_stats->xskrq;
3880 struct mlx5e_rq_stats *rq_stats = &channel_stats->rq;
3881 int j;
3882
3883 s->rx_packets += rq_stats->packets + xskrq_stats->packets;
3884 s->rx_bytes += rq_stats->bytes + xskrq_stats->bytes;
3885 s->multicast += rq_stats->mcast_packets + xskrq_stats->mcast_packets;
3886
3887 for (j = 0; j < priv->max_opened_tc; j++) {
3888 struct mlx5e_sq_stats *sq_stats = &channel_stats->sq[j];
3889
3890 s->tx_packets += sq_stats->packets;
3891 s->tx_bytes += sq_stats->bytes;
3892 s->tx_dropped += sq_stats->dropped;
3893 }
3894 }
3895 if (priv->tx_ptp_opened) {
3896 for (i = 0; i < priv->max_opened_tc; i++) {
3897 struct mlx5e_sq_stats *sq_stats = &priv->ptp_stats.sq[i];
3898
3899 s->tx_packets += sq_stats->packets;
3900 s->tx_bytes += sq_stats->bytes;
3901 s->tx_dropped += sq_stats->dropped;
3902 }
3903 }
3904 if (priv->rx_ptp_opened) {
3905 struct mlx5e_rq_stats *rq_stats = &priv->ptp_stats.rq;
3906
3907 s->rx_packets += rq_stats->packets;
3908 s->rx_bytes += rq_stats->bytes;
3909 s->multicast += rq_stats->mcast_packets;
3910 }
3911 }
3912
3913 void
mlx5e_get_stats(struct net_device * dev,struct rtnl_link_stats64 * stats)3914 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
3915 {
3916 struct mlx5e_priv *priv = netdev_priv(dev);
3917 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
3918
3919 if (!netif_device_present(dev))
3920 return;
3921
3922 /* In switchdev mode, monitor counters doesn't monitor
3923 * rx/tx stats of 802_3. The update stats mechanism
3924 * should keep the 802_3 layout counters updated
3925 */
3926 if (!mlx5e_monitor_counter_supported(priv) ||
3927 mlx5e_is_uplink_rep(priv)) {
3928 /* update HW stats in background for next time */
3929 mlx5e_queue_update_stats(priv);
3930 }
3931
3932 if (mlx5e_is_uplink_rep(priv)) {
3933 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
3934
3935 stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
3936 stats->rx_bytes = PPORT_802_3_GET(pstats, a_octets_received_ok);
3937 stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
3938 stats->tx_bytes = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
3939
3940 /* vport multicast also counts packets that are dropped due to steering
3941 * or rx out of buffer
3942 */
3943 stats->multicast = VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
3944 } else {
3945 mlx5e_fold_sw_stats64(priv, stats);
3946 }
3947
3948 stats->rx_missed_errors = priv->stats.qcnt.rx_out_of_buffer;
3949 stats->rx_dropped = PPORT_2863_GET(pstats, if_in_discards);
3950
3951 stats->rx_length_errors =
3952 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
3953 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
3954 PPORT_802_3_GET(pstats, a_frame_too_long_errors) +
3955 VNIC_ENV_GET(&priv->stats.vnic, eth_wqe_too_small);
3956 stats->rx_crc_errors =
3957 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
3958 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
3959 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
3960 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
3961 stats->rx_frame_errors;
3962 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
3963 }
3964
mlx5e_nic_set_rx_mode(struct mlx5e_priv * priv)3965 static void mlx5e_nic_set_rx_mode(struct mlx5e_priv *priv)
3966 {
3967 if (mlx5e_is_uplink_rep(priv))
3968 return; /* no rx mode for uplink rep */
3969
3970 queue_work(priv->wq, &priv->set_rx_mode_work);
3971 }
3972
mlx5e_set_rx_mode(struct net_device * dev)3973 static void mlx5e_set_rx_mode(struct net_device *dev)
3974 {
3975 struct mlx5e_priv *priv = netdev_priv(dev);
3976
3977 mlx5e_nic_set_rx_mode(priv);
3978 }
3979
mlx5e_set_mac(struct net_device * netdev,void * addr)3980 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
3981 {
3982 struct mlx5e_priv *priv = netdev_priv(netdev);
3983 struct sockaddr *saddr = addr;
3984
3985 if (!is_valid_ether_addr(saddr->sa_data))
3986 return -EADDRNOTAVAIL;
3987
3988 netif_addr_lock_bh(netdev);
3989 eth_hw_addr_set(netdev, saddr->sa_data);
3990 netif_addr_unlock_bh(netdev);
3991
3992 mlx5e_nic_set_rx_mode(priv);
3993
3994 return 0;
3995 }
3996
3997 #define MLX5E_SET_FEATURE(features, feature, enable) \
3998 do { \
3999 if (enable) \
4000 *features |= feature; \
4001 else \
4002 *features &= ~feature; \
4003 } while (0)
4004
4005 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
4006
set_feature_lro(struct net_device * netdev,bool enable)4007 static int set_feature_lro(struct net_device *netdev, bool enable)
4008 {
4009 struct mlx5e_priv *priv = netdev_priv(netdev);
4010 struct mlx5_core_dev *mdev = priv->mdev;
4011 struct mlx5e_params *cur_params;
4012 struct mlx5e_params new_params;
4013 bool reset = true;
4014 int err = 0;
4015
4016 mutex_lock(&priv->state_lock);
4017
4018 cur_params = &priv->channels.params;
4019 new_params = *cur_params;
4020
4021 if (enable)
4022 new_params.packet_merge.type = MLX5E_PACKET_MERGE_LRO;
4023 else if (new_params.packet_merge.type == MLX5E_PACKET_MERGE_LRO)
4024 new_params.packet_merge.type = MLX5E_PACKET_MERGE_NONE;
4025 else
4026 goto out;
4027
4028 if (!(cur_params->packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO &&
4029 new_params.packet_merge.type == MLX5E_PACKET_MERGE_LRO)) {
4030 if (cur_params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
4031 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, cur_params, NULL) ==
4032 mlx5e_rx_mpwqe_is_linear_skb(mdev, &new_params, NULL))
4033 reset = false;
4034 }
4035 }
4036
4037 err = mlx5e_safe_switch_params(priv, &new_params,
4038 mlx5e_modify_tirs_packet_merge_ctx, NULL, reset);
4039 out:
4040 mutex_unlock(&priv->state_lock);
4041 return err;
4042 }
4043
set_feature_hw_gro(struct net_device * netdev,bool enable)4044 static int set_feature_hw_gro(struct net_device *netdev, bool enable)
4045 {
4046 struct mlx5e_priv *priv = netdev_priv(netdev);
4047 struct mlx5e_params new_params;
4048 bool reset = true;
4049 int err = 0;
4050
4051 mutex_lock(&priv->state_lock);
4052 new_params = priv->channels.params;
4053
4054 if (enable) {
4055 new_params.packet_merge.type = MLX5E_PACKET_MERGE_SHAMPO;
4056 new_params.packet_merge.shampo.match_criteria_type =
4057 MLX5_RQC_SHAMPO_MATCH_CRITERIA_TYPE_EXTENDED;
4058 new_params.packet_merge.shampo.alignment_granularity =
4059 MLX5_RQC_SHAMPO_NO_MATCH_ALIGNMENT_GRANULARITY_STRIDE;
4060 } else if (new_params.packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO) {
4061 new_params.packet_merge.type = MLX5E_PACKET_MERGE_NONE;
4062 } else {
4063 goto out;
4064 }
4065
4066 err = mlx5e_safe_switch_params(priv, &new_params, NULL, NULL, reset);
4067 out:
4068 mutex_unlock(&priv->state_lock);
4069 return err;
4070 }
4071
set_feature_cvlan_filter(struct net_device * netdev,bool enable)4072 static int set_feature_cvlan_filter(struct net_device *netdev, bool enable)
4073 {
4074 struct mlx5e_priv *priv = netdev_priv(netdev);
4075
4076 if (enable)
4077 mlx5e_enable_cvlan_filter(priv->fs,
4078 !!(priv->netdev->flags & IFF_PROMISC));
4079 else
4080 mlx5e_disable_cvlan_filter(priv->fs,
4081 !!(priv->netdev->flags & IFF_PROMISC));
4082
4083 return 0;
4084 }
4085
set_feature_hw_tc(struct net_device * netdev,bool enable)4086 static int set_feature_hw_tc(struct net_device *netdev, bool enable)
4087 {
4088 struct mlx5e_priv *priv = netdev_priv(netdev);
4089 int err = 0;
4090
4091 #if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
4092 int tc_flag = mlx5e_is_uplink_rep(priv) ? MLX5_TC_FLAG(ESW_OFFLOAD) :
4093 MLX5_TC_FLAG(NIC_OFFLOAD);
4094 if (!enable && mlx5e_tc_num_filters(priv, tc_flag)) {
4095 netdev_err(netdev,
4096 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
4097 return -EINVAL;
4098 }
4099 #endif
4100
4101 mutex_lock(&priv->state_lock);
4102 if (!enable && mlx5e_selq_is_htb_enabled(&priv->selq)) {
4103 netdev_err(netdev, "Active HTB offload, can't turn hw_tc_offload off\n");
4104 err = -EINVAL;
4105 }
4106 mutex_unlock(&priv->state_lock);
4107
4108 return err;
4109 }
4110
set_feature_rx_all(struct net_device * netdev,bool enable)4111 static int set_feature_rx_all(struct net_device *netdev, bool enable)
4112 {
4113 struct mlx5e_priv *priv = netdev_priv(netdev);
4114 struct mlx5_core_dev *mdev = priv->mdev;
4115
4116 return mlx5_set_port_fcs(mdev, !enable);
4117 }
4118
mlx5e_get_def_rx_moderation(u8 cq_period_mode)4119 static struct dim_cq_moder mlx5e_get_def_rx_moderation(u8 cq_period_mode)
4120 {
4121 return (struct dim_cq_moder) {
4122 .cq_period_mode = cq_period_mode,
4123 .pkts = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS,
4124 .usec = cq_period_mode == DIM_CQ_PERIOD_MODE_START_FROM_CQE ?
4125 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE :
4126 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC,
4127 };
4128 }
4129
mlx5e_reset_rx_moderation(struct dim_cq_moder * cq_moder,u8 cq_period_mode,bool dim_enabled)4130 bool mlx5e_reset_rx_moderation(struct dim_cq_moder *cq_moder, u8 cq_period_mode,
4131 bool dim_enabled)
4132 {
4133 bool reset_needed = cq_moder->cq_period_mode != cq_period_mode;
4134
4135 if (dim_enabled)
4136 *cq_moder = net_dim_get_def_rx_moderation(cq_period_mode);
4137 else
4138 *cq_moder = mlx5e_get_def_rx_moderation(cq_period_mode);
4139
4140 return reset_needed;
4141 }
4142
mlx5e_reset_rx_channels_moderation(struct mlx5e_channels * chs,u8 cq_period_mode,bool dim_enabled,bool keep_dim_state)4143 bool mlx5e_reset_rx_channels_moderation(struct mlx5e_channels *chs, u8 cq_period_mode,
4144 bool dim_enabled, bool keep_dim_state)
4145 {
4146 bool reset = false;
4147 int i;
4148
4149 for (i = 0; i < chs->num; i++) {
4150 if (keep_dim_state)
4151 dim_enabled = !!chs->c[i]->rq.dim;
4152
4153 reset |= mlx5e_reset_rx_moderation(&chs->c[i]->rx_cq_moder,
4154 cq_period_mode, dim_enabled);
4155 }
4156
4157 return reset;
4158 }
4159
mlx5e_set_rx_port_ts(struct mlx5_core_dev * mdev,bool enable)4160 static int mlx5e_set_rx_port_ts(struct mlx5_core_dev *mdev, bool enable)
4161 {
4162 u32 in[MLX5_ST_SZ_DW(pcmr_reg)] = {};
4163 bool supported, curr_state;
4164 int err;
4165
4166 if (!MLX5_CAP_GEN(mdev, ports_check))
4167 return 0;
4168
4169 err = mlx5_query_ports_check(mdev, in, sizeof(in));
4170 if (err)
4171 return err;
4172
4173 supported = MLX5_GET(pcmr_reg, in, rx_ts_over_crc_cap);
4174 curr_state = MLX5_GET(pcmr_reg, in, rx_ts_over_crc);
4175
4176 if (!supported || enable == curr_state)
4177 return 0;
4178
4179 MLX5_SET(pcmr_reg, in, local_port, 1);
4180 MLX5_SET(pcmr_reg, in, rx_ts_over_crc, enable);
4181
4182 return mlx5_set_ports_check(mdev, in, sizeof(in));
4183 }
4184
mlx5e_set_rx_port_ts_wrap(struct mlx5e_priv * priv,void * ctx)4185 static int mlx5e_set_rx_port_ts_wrap(struct mlx5e_priv *priv, void *ctx)
4186 {
4187 struct mlx5_core_dev *mdev = priv->mdev;
4188 bool enable = *(bool *)ctx;
4189
4190 return mlx5e_set_rx_port_ts(mdev, enable);
4191 }
4192
set_feature_rx_fcs(struct net_device * netdev,bool enable)4193 static int set_feature_rx_fcs(struct net_device *netdev, bool enable)
4194 {
4195 struct mlx5e_priv *priv = netdev_priv(netdev);
4196 struct mlx5e_channels *chs = &priv->channels;
4197 struct mlx5e_params new_params;
4198 int err;
4199 bool rx_ts_over_crc = !enable;
4200
4201 mutex_lock(&priv->state_lock);
4202
4203 new_params = chs->params;
4204 new_params.scatter_fcs_en = enable;
4205 err = mlx5e_safe_switch_params(priv, &new_params, mlx5e_set_rx_port_ts_wrap,
4206 &rx_ts_over_crc, true);
4207 mutex_unlock(&priv->state_lock);
4208 return err;
4209 }
4210
set_feature_rx_vlan(struct net_device * netdev,bool enable)4211 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
4212 {
4213 struct mlx5e_priv *priv = netdev_priv(netdev);
4214 int err = 0;
4215
4216 mutex_lock(&priv->state_lock);
4217
4218 mlx5e_fs_set_vlan_strip_disable(priv->fs, !enable);
4219 priv->channels.params.vlan_strip_disable = !enable;
4220
4221 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
4222 goto unlock;
4223
4224 err = mlx5e_modify_channels_vsd(&priv->channels, !enable);
4225 if (err) {
4226 mlx5e_fs_set_vlan_strip_disable(priv->fs, enable);
4227 priv->channels.params.vlan_strip_disable = enable;
4228 }
4229 unlock:
4230 mutex_unlock(&priv->state_lock);
4231
4232 return err;
4233 }
4234
mlx5e_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)4235 int mlx5e_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
4236 {
4237 struct mlx5e_priv *priv = netdev_priv(dev);
4238 struct mlx5e_flow_steering *fs = priv->fs;
4239
4240 if (mlx5e_is_uplink_rep(priv))
4241 return 0; /* no vlan table for uplink rep */
4242
4243 return mlx5e_fs_vlan_rx_add_vid(fs, dev, proto, vid);
4244 }
4245
mlx5e_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)4246 int mlx5e_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
4247 {
4248 struct mlx5e_priv *priv = netdev_priv(dev);
4249 struct mlx5e_flow_steering *fs = priv->fs;
4250
4251 if (mlx5e_is_uplink_rep(priv))
4252 return 0; /* no vlan table for uplink rep */
4253
4254 return mlx5e_fs_vlan_rx_kill_vid(fs, dev, proto, vid);
4255 }
4256
4257 #ifdef CONFIG_MLX5_EN_ARFS
set_feature_arfs(struct net_device * netdev,bool enable)4258 static int set_feature_arfs(struct net_device *netdev, bool enable)
4259 {
4260 struct mlx5e_priv *priv = netdev_priv(netdev);
4261 int err;
4262
4263 if (enable)
4264 err = mlx5e_arfs_enable(priv->fs);
4265 else
4266 err = mlx5e_arfs_disable(priv->fs);
4267
4268 return err;
4269 }
4270 #endif
4271
mlx5e_handle_feature(struct net_device * netdev,netdev_features_t * features,netdev_features_t feature,mlx5e_feature_handler feature_handler)4272 static int mlx5e_handle_feature(struct net_device *netdev,
4273 netdev_features_t *features,
4274 netdev_features_t feature,
4275 mlx5e_feature_handler feature_handler)
4276 {
4277 netdev_features_t changes = *features ^ netdev->features;
4278 bool enable = !!(*features & feature);
4279 int err;
4280
4281 if (!(changes & feature))
4282 return 0;
4283
4284 err = feature_handler(netdev, enable);
4285 if (err) {
4286 MLX5E_SET_FEATURE(features, feature, !enable);
4287 netdev_err(netdev, "%s feature %pNF failed, err %d\n",
4288 enable ? "Enable" : "Disable", &feature, err);
4289 return err;
4290 }
4291
4292 return 0;
4293 }
4294
mlx5e_set_xdp_feature(struct net_device * netdev)4295 void mlx5e_set_xdp_feature(struct net_device *netdev)
4296 {
4297 struct mlx5e_priv *priv = netdev_priv(netdev);
4298 struct mlx5e_params *params = &priv->channels.params;
4299 xdp_features_t val;
4300
4301 if (!netdev->netdev_ops->ndo_bpf ||
4302 params->packet_merge.type != MLX5E_PACKET_MERGE_NONE) {
4303 xdp_clear_features_flag(netdev);
4304 return;
4305 }
4306
4307 val = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
4308 NETDEV_XDP_ACT_XSK_ZEROCOPY |
4309 NETDEV_XDP_ACT_RX_SG |
4310 NETDEV_XDP_ACT_NDO_XMIT |
4311 NETDEV_XDP_ACT_NDO_XMIT_SG;
4312 xdp_set_features_flag(netdev, val);
4313 }
4314
mlx5e_set_features(struct net_device * netdev,netdev_features_t features)4315 int mlx5e_set_features(struct net_device *netdev, netdev_features_t features)
4316 {
4317 netdev_features_t oper_features = features;
4318 int err = 0;
4319
4320 #define MLX5E_HANDLE_FEATURE(feature, handler) \
4321 mlx5e_handle_feature(netdev, &oper_features, feature, handler)
4322
4323 if (features & (NETIF_F_GRO_HW | NETIF_F_LRO)) {
4324 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXFCS, set_feature_rx_fcs);
4325 err |= MLX5E_HANDLE_FEATURE(NETIF_F_LRO, set_feature_lro);
4326 err |= MLX5E_HANDLE_FEATURE(NETIF_F_GRO_HW, set_feature_hw_gro);
4327 } else {
4328 err |= MLX5E_HANDLE_FEATURE(NETIF_F_LRO, set_feature_lro);
4329 err |= MLX5E_HANDLE_FEATURE(NETIF_F_GRO_HW, set_feature_hw_gro);
4330 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXFCS, set_feature_rx_fcs);
4331 }
4332 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_FILTER,
4333 set_feature_cvlan_filter);
4334 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_TC, set_feature_hw_tc);
4335 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXALL, set_feature_rx_all);
4336 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_RX, set_feature_rx_vlan);
4337 #ifdef CONFIG_MLX5_EN_ARFS
4338 err |= MLX5E_HANDLE_FEATURE(NETIF_F_NTUPLE, set_feature_arfs);
4339 #endif
4340 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_TLS_RX, mlx5e_ktls_set_feature_rx);
4341
4342 if (err) {
4343 netdev->features = oper_features;
4344 return -EINVAL;
4345 }
4346
4347 /* update XDP supported features */
4348 mlx5e_set_xdp_feature(netdev);
4349
4350 return 0;
4351 }
4352
mlx5e_fix_uplink_rep_features(struct net_device * netdev,netdev_features_t features)4353 static netdev_features_t mlx5e_fix_uplink_rep_features(struct net_device *netdev,
4354 netdev_features_t features)
4355 {
4356 features &= ~NETIF_F_HW_TLS_RX;
4357 if (netdev->features & NETIF_F_HW_TLS_RX)
4358 netdev_warn(netdev, "Disabling hw_tls_rx, not supported in switchdev mode\n");
4359
4360 features &= ~NETIF_F_HW_TLS_TX;
4361 if (netdev->features & NETIF_F_HW_TLS_TX)
4362 netdev_warn(netdev, "Disabling hw_tls_tx, not supported in switchdev mode\n");
4363
4364 features &= ~NETIF_F_NTUPLE;
4365 if (netdev->features & NETIF_F_NTUPLE)
4366 netdev_warn(netdev, "Disabling ntuple, not supported in switchdev mode\n");
4367
4368 features &= ~NETIF_F_GRO_HW;
4369 if (netdev->features & NETIF_F_GRO_HW)
4370 netdev_warn(netdev, "Disabling HW_GRO, not supported in switchdev mode\n");
4371
4372 features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
4373 if (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
4374 netdev_warn(netdev, "Disabling HW_VLAN CTAG FILTERING, not supported in switchdev mode\n");
4375
4376 return features;
4377 }
4378
mlx5e_fix_features(struct net_device * netdev,netdev_features_t features)4379 static netdev_features_t mlx5e_fix_features(struct net_device *netdev,
4380 netdev_features_t features)
4381 {
4382 struct mlx5e_priv *priv = netdev_priv(netdev);
4383 struct mlx5e_vlan_table *vlan;
4384 struct mlx5e_params *params;
4385
4386 if (!netif_device_present(netdev))
4387 return features;
4388
4389 vlan = mlx5e_fs_get_vlan(priv->fs);
4390 mutex_lock(&priv->state_lock);
4391 params = &priv->channels.params;
4392 if (!vlan ||
4393 !bitmap_empty(mlx5e_vlan_get_active_svlans(vlan), VLAN_N_VID)) {
4394 /* HW strips the outer C-tag header, this is a problem
4395 * for S-tag traffic.
4396 */
4397 features &= ~NETIF_F_HW_VLAN_CTAG_RX;
4398 if (!params->vlan_strip_disable)
4399 netdev_warn(netdev, "Dropping C-tag vlan stripping offload due to S-tag vlan\n");
4400 }
4401
4402 if (!MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
4403 if (features & NETIF_F_LRO) {
4404 netdev_warn(netdev, "Disabling LRO, not supported in legacy RQ\n");
4405 features &= ~NETIF_F_LRO;
4406 }
4407 if (features & NETIF_F_GRO_HW) {
4408 netdev_warn(netdev, "Disabling HW-GRO, not supported in legacy RQ\n");
4409 features &= ~NETIF_F_GRO_HW;
4410 }
4411 }
4412
4413 if (params->xdp_prog) {
4414 if (features & NETIF_F_LRO) {
4415 netdev_warn(netdev, "LRO is incompatible with XDP\n");
4416 features &= ~NETIF_F_LRO;
4417 }
4418 if (features & NETIF_F_GRO_HW) {
4419 netdev_warn(netdev, "HW GRO is incompatible with XDP\n");
4420 features &= ~NETIF_F_GRO_HW;
4421 }
4422 }
4423
4424 if (priv->xsk.refcnt) {
4425 if (features & NETIF_F_LRO) {
4426 netdev_warn(netdev, "LRO is incompatible with AF_XDP (%u XSKs are active)\n",
4427 priv->xsk.refcnt);
4428 features &= ~NETIF_F_LRO;
4429 }
4430 if (features & NETIF_F_GRO_HW) {
4431 netdev_warn(netdev, "HW GRO is incompatible with AF_XDP (%u XSKs are active)\n",
4432 priv->xsk.refcnt);
4433 features &= ~NETIF_F_GRO_HW;
4434 }
4435 }
4436
4437 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
4438 features &= ~NETIF_F_RXHASH;
4439 if (netdev->features & NETIF_F_RXHASH)
4440 netdev_warn(netdev, "Disabling rxhash, not supported when CQE compress is active\n");
4441
4442 if (features & NETIF_F_GRO_HW) {
4443 netdev_warn(netdev, "Disabling HW-GRO, not supported when CQE compress is active\n");
4444 features &= ~NETIF_F_GRO_HW;
4445 }
4446 }
4447
4448 if (mlx5e_is_uplink_rep(priv)) {
4449 features = mlx5e_fix_uplink_rep_features(netdev, features);
4450 netdev->netns_local = true;
4451 } else {
4452 netdev->netns_local = false;
4453 }
4454
4455 mutex_unlock(&priv->state_lock);
4456
4457 return features;
4458 }
4459
mlx5e_xsk_validate_mtu(struct net_device * netdev,struct mlx5e_channels * chs,struct mlx5e_params * new_params,struct mlx5_core_dev * mdev)4460 static bool mlx5e_xsk_validate_mtu(struct net_device *netdev,
4461 struct mlx5e_channels *chs,
4462 struct mlx5e_params *new_params,
4463 struct mlx5_core_dev *mdev)
4464 {
4465 u16 ix;
4466
4467 for (ix = 0; ix < chs->params.num_channels; ix++) {
4468 struct xsk_buff_pool *xsk_pool =
4469 mlx5e_xsk_get_pool(&chs->params, chs->params.xsk, ix);
4470 struct mlx5e_xsk_param xsk;
4471 int max_xdp_mtu;
4472
4473 if (!xsk_pool)
4474 continue;
4475
4476 mlx5e_build_xsk_param(xsk_pool, &xsk);
4477 max_xdp_mtu = mlx5e_xdp_max_mtu(new_params, &xsk);
4478
4479 /* Validate XSK params and XDP MTU in advance */
4480 if (!mlx5e_validate_xsk_param(new_params, &xsk, mdev) ||
4481 new_params->sw_mtu > max_xdp_mtu) {
4482 u32 hr = mlx5e_get_linear_rq_headroom(new_params, &xsk);
4483 int max_mtu_frame, max_mtu_page, max_mtu;
4484
4485 /* Two criteria must be met:
4486 * 1. HW MTU + all headrooms <= XSK frame size.
4487 * 2. Size of SKBs allocated on XDP_PASS <= PAGE_SIZE.
4488 */
4489 max_mtu_frame = MLX5E_HW2SW_MTU(new_params, xsk.chunk_size - hr);
4490 max_mtu_page = MLX5E_HW2SW_MTU(new_params, SKB_MAX_HEAD(0));
4491 max_mtu = min3(max_mtu_frame, max_mtu_page, max_xdp_mtu);
4492
4493 netdev_err(netdev, "MTU %d is too big for an XSK running on channel %u or its redirection XDP program. Try MTU <= %d\n",
4494 new_params->sw_mtu, ix, max_mtu);
4495 return false;
4496 }
4497 }
4498
4499 return true;
4500 }
4501
mlx5e_params_validate_xdp(struct net_device * netdev,struct mlx5_core_dev * mdev,struct mlx5e_params * params)4502 static bool mlx5e_params_validate_xdp(struct net_device *netdev,
4503 struct mlx5_core_dev *mdev,
4504 struct mlx5e_params *params)
4505 {
4506 bool is_linear;
4507
4508 /* No XSK params: AF_XDP can't be enabled yet at the point of setting
4509 * the XDP program.
4510 */
4511 is_linear = params->rq_wq_type == MLX5_WQ_TYPE_CYCLIC ?
4512 mlx5e_rx_is_linear_skb(mdev, params, NULL) :
4513 mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL);
4514
4515 if (!is_linear) {
4516 if (!params->xdp_prog->aux->xdp_has_frags) {
4517 netdev_warn(netdev, "MTU(%d) > %d, too big for an XDP program not aware of multi buffer\n",
4518 params->sw_mtu,
4519 mlx5e_xdp_max_mtu(params, NULL));
4520 return false;
4521 }
4522 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ &&
4523 !mlx5e_verify_params_rx_mpwqe_strides(mdev, params, NULL)) {
4524 netdev_warn(netdev, "XDP is not allowed with striding RQ and MTU(%d) > %d\n",
4525 params->sw_mtu,
4526 mlx5e_xdp_max_mtu(params, NULL));
4527 return false;
4528 }
4529 }
4530
4531 return true;
4532 }
4533
mlx5e_change_mtu(struct net_device * netdev,int new_mtu,mlx5e_fp_preactivate preactivate)4534 int mlx5e_change_mtu(struct net_device *netdev, int new_mtu,
4535 mlx5e_fp_preactivate preactivate)
4536 {
4537 struct mlx5e_priv *priv = netdev_priv(netdev);
4538 struct mlx5e_params new_params;
4539 struct mlx5e_params *params;
4540 bool reset = true;
4541 int err = 0;
4542
4543 mutex_lock(&priv->state_lock);
4544
4545 params = &priv->channels.params;
4546
4547 new_params = *params;
4548 new_params.sw_mtu = new_mtu;
4549 err = mlx5e_validate_params(priv->mdev, &new_params);
4550 if (err)
4551 goto out;
4552
4553 if (new_params.xdp_prog && !mlx5e_params_validate_xdp(netdev, priv->mdev,
4554 &new_params)) {
4555 err = -EINVAL;
4556 goto out;
4557 }
4558
4559 if (priv->xsk.refcnt &&
4560 !mlx5e_xsk_validate_mtu(netdev, &priv->channels,
4561 &new_params, priv->mdev)) {
4562 err = -EINVAL;
4563 goto out;
4564 }
4565
4566 if (params->packet_merge.type == MLX5E_PACKET_MERGE_LRO)
4567 reset = false;
4568
4569 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ &&
4570 params->packet_merge.type != MLX5E_PACKET_MERGE_SHAMPO) {
4571 bool is_linear_old = mlx5e_rx_mpwqe_is_linear_skb(priv->mdev, params, NULL);
4572 bool is_linear_new = mlx5e_rx_mpwqe_is_linear_skb(priv->mdev,
4573 &new_params, NULL);
4574 u8 sz_old = mlx5e_mpwqe_get_log_rq_size(priv->mdev, params, NULL);
4575 u8 sz_new = mlx5e_mpwqe_get_log_rq_size(priv->mdev, &new_params, NULL);
4576
4577 /* Always reset in linear mode - hw_mtu is used in data path.
4578 * Check that the mode was non-linear and didn't change.
4579 * If XSK is active, XSK RQs are linear.
4580 * Reset if the RQ size changed, even if it's non-linear.
4581 */
4582 if (!is_linear_old && !is_linear_new && !priv->xsk.refcnt &&
4583 sz_old == sz_new)
4584 reset = false;
4585 }
4586
4587 err = mlx5e_safe_switch_params(priv, &new_params, preactivate, NULL, reset);
4588
4589 out:
4590 WRITE_ONCE(netdev->mtu, params->sw_mtu);
4591 mutex_unlock(&priv->state_lock);
4592
4593 if (!err)
4594 netdev_update_features(netdev);
4595
4596 return err;
4597 }
4598
mlx5e_change_nic_mtu(struct net_device * netdev,int new_mtu)4599 static int mlx5e_change_nic_mtu(struct net_device *netdev, int new_mtu)
4600 {
4601 return mlx5e_change_mtu(netdev, new_mtu, mlx5e_set_dev_port_mtu_ctx);
4602 }
4603
mlx5e_ptp_rx_manage_fs_ctx(struct mlx5e_priv * priv,void * ctx)4604 int mlx5e_ptp_rx_manage_fs_ctx(struct mlx5e_priv *priv, void *ctx)
4605 {
4606 bool set = *(bool *)ctx;
4607
4608 return mlx5e_ptp_rx_manage_fs(priv, set);
4609 }
4610
mlx5e_hwstamp_config_no_ptp_rx(struct mlx5e_priv * priv,bool rx_filter)4611 static int mlx5e_hwstamp_config_no_ptp_rx(struct mlx5e_priv *priv, bool rx_filter)
4612 {
4613 bool rx_cqe_compress_def = priv->channels.params.rx_cqe_compress_def;
4614 int err;
4615
4616 if (!rx_filter)
4617 /* Reset CQE compression to Admin default */
4618 return mlx5e_modify_rx_cqe_compression_locked(priv, rx_cqe_compress_def, false);
4619
4620 if (!MLX5E_GET_PFLAG(&priv->channels.params, MLX5E_PFLAG_RX_CQE_COMPRESS))
4621 return 0;
4622
4623 /* Disable CQE compression */
4624 netdev_warn(priv->netdev, "Disabling RX cqe compression\n");
4625 err = mlx5e_modify_rx_cqe_compression_locked(priv, false, true);
4626 if (err)
4627 netdev_err(priv->netdev, "Failed disabling cqe compression err=%d\n", err);
4628
4629 return err;
4630 }
4631
mlx5e_hwstamp_config_ptp_rx(struct mlx5e_priv * priv,bool ptp_rx)4632 static int mlx5e_hwstamp_config_ptp_rx(struct mlx5e_priv *priv, bool ptp_rx)
4633 {
4634 struct mlx5e_params new_params;
4635
4636 if (ptp_rx == priv->channels.params.ptp_rx)
4637 return 0;
4638
4639 new_params = priv->channels.params;
4640 new_params.ptp_rx = ptp_rx;
4641 return mlx5e_safe_switch_params(priv, &new_params, mlx5e_ptp_rx_manage_fs_ctx,
4642 &new_params.ptp_rx, true);
4643 }
4644
mlx5e_hwstamp_set(struct mlx5e_priv * priv,struct ifreq * ifr)4645 int mlx5e_hwstamp_set(struct mlx5e_priv *priv, struct ifreq *ifr)
4646 {
4647 struct hwtstamp_config config;
4648 bool rx_cqe_compress_def;
4649 bool ptp_rx;
4650 int err;
4651
4652 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz) ||
4653 (mlx5_clock_get_ptp_index(priv->mdev) == -1))
4654 return -EOPNOTSUPP;
4655
4656 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
4657 return -EFAULT;
4658
4659 /* TX HW timestamp */
4660 switch (config.tx_type) {
4661 case HWTSTAMP_TX_OFF:
4662 case HWTSTAMP_TX_ON:
4663 break;
4664 default:
4665 return -ERANGE;
4666 }
4667
4668 mutex_lock(&priv->state_lock);
4669 rx_cqe_compress_def = priv->channels.params.rx_cqe_compress_def;
4670
4671 /* RX HW timestamp */
4672 switch (config.rx_filter) {
4673 case HWTSTAMP_FILTER_NONE:
4674 ptp_rx = false;
4675 break;
4676 case HWTSTAMP_FILTER_ALL:
4677 case HWTSTAMP_FILTER_SOME:
4678 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
4679 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
4680 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
4681 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
4682 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
4683 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
4684 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
4685 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
4686 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
4687 case HWTSTAMP_FILTER_PTP_V2_EVENT:
4688 case HWTSTAMP_FILTER_PTP_V2_SYNC:
4689 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
4690 case HWTSTAMP_FILTER_NTP_ALL:
4691 config.rx_filter = HWTSTAMP_FILTER_ALL;
4692 /* ptp_rx is set if both HW TS is set and CQE
4693 * compression is set
4694 */
4695 ptp_rx = rx_cqe_compress_def;
4696 break;
4697 default:
4698 err = -ERANGE;
4699 goto err_unlock;
4700 }
4701
4702 if (!mlx5e_profile_feature_cap(priv->profile, PTP_RX))
4703 err = mlx5e_hwstamp_config_no_ptp_rx(priv,
4704 config.rx_filter != HWTSTAMP_FILTER_NONE);
4705 else
4706 err = mlx5e_hwstamp_config_ptp_rx(priv, ptp_rx);
4707 if (err)
4708 goto err_unlock;
4709
4710 memcpy(&priv->tstamp, &config, sizeof(config));
4711 mutex_unlock(&priv->state_lock);
4712
4713 /* might need to fix some features */
4714 netdev_update_features(priv->netdev);
4715
4716 return copy_to_user(ifr->ifr_data, &config,
4717 sizeof(config)) ? -EFAULT : 0;
4718 err_unlock:
4719 mutex_unlock(&priv->state_lock);
4720 return err;
4721 }
4722
mlx5e_hwstamp_get(struct mlx5e_priv * priv,struct ifreq * ifr)4723 int mlx5e_hwstamp_get(struct mlx5e_priv *priv, struct ifreq *ifr)
4724 {
4725 struct hwtstamp_config *cfg = &priv->tstamp;
4726
4727 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
4728 return -EOPNOTSUPP;
4729
4730 return copy_to_user(ifr->ifr_data, cfg, sizeof(*cfg)) ? -EFAULT : 0;
4731 }
4732
mlx5e_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)4733 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4734 {
4735 struct mlx5e_priv *priv = netdev_priv(dev);
4736
4737 switch (cmd) {
4738 case SIOCSHWTSTAMP:
4739 return mlx5e_hwstamp_set(priv, ifr);
4740 case SIOCGHWTSTAMP:
4741 return mlx5e_hwstamp_get(priv, ifr);
4742 default:
4743 return -EOPNOTSUPP;
4744 }
4745 }
4746
4747 #ifdef CONFIG_MLX5_ESWITCH
mlx5e_set_vf_mac(struct net_device * dev,int vf,u8 * mac)4748 int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
4749 {
4750 struct mlx5e_priv *priv = netdev_priv(dev);
4751 struct mlx5_core_dev *mdev = priv->mdev;
4752
4753 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
4754 }
4755
mlx5e_set_vf_vlan(struct net_device * dev,int vf,u16 vlan,u8 qos,__be16 vlan_proto)4756 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
4757 __be16 vlan_proto)
4758 {
4759 struct mlx5e_priv *priv = netdev_priv(dev);
4760 struct mlx5_core_dev *mdev = priv->mdev;
4761
4762 if (vlan_proto != htons(ETH_P_8021Q))
4763 return -EPROTONOSUPPORT;
4764
4765 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
4766 vlan, qos);
4767 }
4768
mlx5e_set_vf_spoofchk(struct net_device * dev,int vf,bool setting)4769 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
4770 {
4771 struct mlx5e_priv *priv = netdev_priv(dev);
4772 struct mlx5_core_dev *mdev = priv->mdev;
4773
4774 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
4775 }
4776
mlx5e_set_vf_trust(struct net_device * dev,int vf,bool setting)4777 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
4778 {
4779 struct mlx5e_priv *priv = netdev_priv(dev);
4780 struct mlx5_core_dev *mdev = priv->mdev;
4781
4782 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
4783 }
4784
mlx5e_set_vf_rate(struct net_device * dev,int vf,int min_tx_rate,int max_tx_rate)4785 int mlx5e_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
4786 int max_tx_rate)
4787 {
4788 struct mlx5e_priv *priv = netdev_priv(dev);
4789 struct mlx5_core_dev *mdev = priv->mdev;
4790
4791 return mlx5_eswitch_set_vport_rate(mdev->priv.eswitch, vf + 1,
4792 max_tx_rate, min_tx_rate);
4793 }
4794
mlx5_vport_link2ifla(u8 esw_link)4795 static int mlx5_vport_link2ifla(u8 esw_link)
4796 {
4797 switch (esw_link) {
4798 case MLX5_VPORT_ADMIN_STATE_DOWN:
4799 return IFLA_VF_LINK_STATE_DISABLE;
4800 case MLX5_VPORT_ADMIN_STATE_UP:
4801 return IFLA_VF_LINK_STATE_ENABLE;
4802 }
4803 return IFLA_VF_LINK_STATE_AUTO;
4804 }
4805
mlx5_ifla_link2vport(u8 ifla_link)4806 static int mlx5_ifla_link2vport(u8 ifla_link)
4807 {
4808 switch (ifla_link) {
4809 case IFLA_VF_LINK_STATE_DISABLE:
4810 return MLX5_VPORT_ADMIN_STATE_DOWN;
4811 case IFLA_VF_LINK_STATE_ENABLE:
4812 return MLX5_VPORT_ADMIN_STATE_UP;
4813 }
4814 return MLX5_VPORT_ADMIN_STATE_AUTO;
4815 }
4816
mlx5e_set_vf_link_state(struct net_device * dev,int vf,int link_state)4817 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
4818 int link_state)
4819 {
4820 struct mlx5e_priv *priv = netdev_priv(dev);
4821 struct mlx5_core_dev *mdev = priv->mdev;
4822
4823 if (mlx5e_is_uplink_rep(priv))
4824 return -EOPNOTSUPP;
4825
4826 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
4827 mlx5_ifla_link2vport(link_state));
4828 }
4829
mlx5e_get_vf_config(struct net_device * dev,int vf,struct ifla_vf_info * ivi)4830 int mlx5e_get_vf_config(struct net_device *dev,
4831 int vf, struct ifla_vf_info *ivi)
4832 {
4833 struct mlx5e_priv *priv = netdev_priv(dev);
4834 struct mlx5_core_dev *mdev = priv->mdev;
4835 int err;
4836
4837 if (!netif_device_present(dev))
4838 return -EOPNOTSUPP;
4839
4840 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
4841 if (err)
4842 return err;
4843 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
4844 return 0;
4845 }
4846
mlx5e_get_vf_stats(struct net_device * dev,int vf,struct ifla_vf_stats * vf_stats)4847 int mlx5e_get_vf_stats(struct net_device *dev,
4848 int vf, struct ifla_vf_stats *vf_stats)
4849 {
4850 struct mlx5e_priv *priv = netdev_priv(dev);
4851 struct mlx5_core_dev *mdev = priv->mdev;
4852
4853 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
4854 vf_stats);
4855 }
4856
4857 static bool
mlx5e_has_offload_stats(const struct net_device * dev,int attr_id)4858 mlx5e_has_offload_stats(const struct net_device *dev, int attr_id)
4859 {
4860 struct mlx5e_priv *priv = netdev_priv(dev);
4861
4862 if (!netif_device_present(dev))
4863 return false;
4864
4865 if (!mlx5e_is_uplink_rep(priv))
4866 return false;
4867
4868 return mlx5e_rep_has_offload_stats(dev, attr_id);
4869 }
4870
4871 static int
mlx5e_get_offload_stats(int attr_id,const struct net_device * dev,void * sp)4872 mlx5e_get_offload_stats(int attr_id, const struct net_device *dev,
4873 void *sp)
4874 {
4875 struct mlx5e_priv *priv = netdev_priv(dev);
4876
4877 if (!mlx5e_is_uplink_rep(priv))
4878 return -EOPNOTSUPP;
4879
4880 return mlx5e_rep_get_offload_stats(attr_id, dev, sp);
4881 }
4882 #endif
4883
mlx5e_tunnel_proto_supported_tx(struct mlx5_core_dev * mdev,u8 proto_type)4884 static bool mlx5e_tunnel_proto_supported_tx(struct mlx5_core_dev *mdev, u8 proto_type)
4885 {
4886 switch (proto_type) {
4887 case IPPROTO_GRE:
4888 return MLX5_CAP_ETH(mdev, tunnel_stateless_gre);
4889 case IPPROTO_IPIP:
4890 case IPPROTO_IPV6:
4891 return (MLX5_CAP_ETH(mdev, tunnel_stateless_ip_over_ip) ||
4892 MLX5_CAP_ETH(mdev, tunnel_stateless_ip_over_ip_tx));
4893 default:
4894 return false;
4895 }
4896 }
4897
mlx5e_gre_tunnel_inner_proto_offload_supported(struct mlx5_core_dev * mdev,struct sk_buff * skb)4898 static bool mlx5e_gre_tunnel_inner_proto_offload_supported(struct mlx5_core_dev *mdev,
4899 struct sk_buff *skb)
4900 {
4901 switch (skb->inner_protocol) {
4902 case htons(ETH_P_IP):
4903 case htons(ETH_P_IPV6):
4904 case htons(ETH_P_TEB):
4905 return true;
4906 case htons(ETH_P_MPLS_UC):
4907 case htons(ETH_P_MPLS_MC):
4908 return MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_gre);
4909 }
4910 return false;
4911 }
4912
mlx5e_tunnel_features_check(struct mlx5e_priv * priv,struct sk_buff * skb,netdev_features_t features)4913 static netdev_features_t mlx5e_tunnel_features_check(struct mlx5e_priv *priv,
4914 struct sk_buff *skb,
4915 netdev_features_t features)
4916 {
4917 unsigned int offset = 0;
4918 struct udphdr *udph;
4919 u8 proto;
4920 u16 port;
4921
4922 switch (vlan_get_protocol(skb)) {
4923 case htons(ETH_P_IP):
4924 proto = ip_hdr(skb)->protocol;
4925 break;
4926 case htons(ETH_P_IPV6):
4927 proto = ipv6_find_hdr(skb, &offset, -1, NULL, NULL);
4928 break;
4929 default:
4930 goto out;
4931 }
4932
4933 switch (proto) {
4934 case IPPROTO_GRE:
4935 if (mlx5e_gre_tunnel_inner_proto_offload_supported(priv->mdev, skb))
4936 return features;
4937 break;
4938 case IPPROTO_IPIP:
4939 case IPPROTO_IPV6:
4940 if (mlx5e_tunnel_proto_supported_tx(priv->mdev, IPPROTO_IPIP))
4941 return features;
4942 break;
4943 case IPPROTO_UDP:
4944 udph = udp_hdr(skb);
4945 port = be16_to_cpu(udph->dest);
4946
4947 /* Verify if UDP port is being offloaded by HW */
4948 if (mlx5_vxlan_lookup_port(priv->mdev->vxlan, port))
4949 return vxlan_features_check(skb, features);
4950
4951 #if IS_ENABLED(CONFIG_GENEVE)
4952 /* Support Geneve offload for default UDP port */
4953 if (port == GENEVE_UDP_PORT && mlx5_geneve_tx_allowed(priv->mdev))
4954 return features;
4955 #endif
4956 break;
4957 #ifdef CONFIG_MLX5_EN_IPSEC
4958 case IPPROTO_ESP:
4959 return mlx5e_ipsec_feature_check(skb, features);
4960 #endif
4961 }
4962
4963 out:
4964 /* Disable CSUM and GSO if skb cannot be offloaded by HW */
4965 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4966 }
4967
mlx5e_features_check(struct sk_buff * skb,struct net_device * netdev,netdev_features_t features)4968 netdev_features_t mlx5e_features_check(struct sk_buff *skb,
4969 struct net_device *netdev,
4970 netdev_features_t features)
4971 {
4972 struct mlx5e_priv *priv = netdev_priv(netdev);
4973
4974 features = vlan_features_check(skb, features);
4975
4976 /* Validate if the tunneled packet is being offloaded by HW */
4977 if (skb->encapsulation &&
4978 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
4979 return mlx5e_tunnel_features_check(priv, skb, features);
4980
4981 return features;
4982 }
4983
mlx5e_tx_timeout_work(struct work_struct * work)4984 static void mlx5e_tx_timeout_work(struct work_struct *work)
4985 {
4986 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
4987 tx_timeout_work);
4988 struct net_device *netdev = priv->netdev;
4989 int i;
4990
4991 /* Take rtnl_lock to ensure no change in netdev->real_num_tx_queues
4992 * through this flow. However, channel closing flows have to wait for
4993 * this work to finish while holding rtnl lock too. So either get the
4994 * lock or find that channels are being closed for other reason and
4995 * this work is not relevant anymore.
4996 */
4997 while (!rtnl_trylock()) {
4998 if (!test_bit(MLX5E_STATE_CHANNELS_ACTIVE, &priv->state))
4999 return;
5000 msleep(20);
5001 }
5002
5003 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
5004 goto unlock;
5005
5006 for (i = 0; i < netdev->real_num_tx_queues; i++) {
5007 struct netdev_queue *dev_queue =
5008 netdev_get_tx_queue(netdev, i);
5009 struct mlx5e_txqsq *sq = priv->txq2sq[i];
5010
5011 if (!netif_xmit_stopped(dev_queue))
5012 continue;
5013
5014 if (mlx5e_reporter_tx_timeout(sq))
5015 /* break if tried to reopened channels */
5016 break;
5017 }
5018
5019 unlock:
5020 rtnl_unlock();
5021 }
5022
mlx5e_tx_timeout(struct net_device * dev,unsigned int txqueue)5023 static void mlx5e_tx_timeout(struct net_device *dev, unsigned int txqueue)
5024 {
5025 struct mlx5e_priv *priv = netdev_priv(dev);
5026
5027 netdev_err(dev, "TX timeout detected\n");
5028 queue_work(priv->wq, &priv->tx_timeout_work);
5029 }
5030
mlx5e_xdp_allowed(struct net_device * netdev,struct mlx5_core_dev * mdev,struct mlx5e_params * params)5031 static int mlx5e_xdp_allowed(struct net_device *netdev, struct mlx5_core_dev *mdev,
5032 struct mlx5e_params *params)
5033 {
5034 if (params->packet_merge.type != MLX5E_PACKET_MERGE_NONE) {
5035 netdev_warn(netdev, "can't set XDP while HW-GRO/LRO is on, disable them first\n");
5036 return -EINVAL;
5037 }
5038
5039 if (!mlx5e_params_validate_xdp(netdev, mdev, params))
5040 return -EINVAL;
5041
5042 return 0;
5043 }
5044
mlx5e_rq_replace_xdp_prog(struct mlx5e_rq * rq,struct bpf_prog * prog)5045 static void mlx5e_rq_replace_xdp_prog(struct mlx5e_rq *rq, struct bpf_prog *prog)
5046 {
5047 struct bpf_prog *old_prog;
5048
5049 old_prog = rcu_replace_pointer(rq->xdp_prog, prog,
5050 lockdep_is_held(&rq->priv->state_lock));
5051 if (old_prog)
5052 bpf_prog_put(old_prog);
5053 }
5054
mlx5e_xdp_set(struct net_device * netdev,struct bpf_prog * prog)5055 static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog)
5056 {
5057 struct mlx5e_priv *priv = netdev_priv(netdev);
5058 struct mlx5e_params new_params;
5059 struct bpf_prog *old_prog;
5060 int err = 0;
5061 bool reset;
5062 int i;
5063
5064 mutex_lock(&priv->state_lock);
5065
5066 new_params = priv->channels.params;
5067 new_params.xdp_prog = prog;
5068
5069 if (prog) {
5070 err = mlx5e_xdp_allowed(netdev, priv->mdev, &new_params);
5071 if (err)
5072 goto unlock;
5073 }
5074
5075 /* no need for full reset when exchanging programs */
5076 reset = (!priv->channels.params.xdp_prog || !prog);
5077
5078 old_prog = priv->channels.params.xdp_prog;
5079
5080 err = mlx5e_safe_switch_params(priv, &new_params, NULL, NULL, reset);
5081 if (err)
5082 goto unlock;
5083
5084 if (old_prog)
5085 bpf_prog_put(old_prog);
5086
5087 if (!test_bit(MLX5E_STATE_OPENED, &priv->state) || reset)
5088 goto unlock;
5089
5090 /* exchanging programs w/o reset, we update ref counts on behalf
5091 * of the channels RQs here.
5092 */
5093 bpf_prog_add(prog, priv->channels.num);
5094 for (i = 0; i < priv->channels.num; i++) {
5095 struct mlx5e_channel *c = priv->channels.c[i];
5096
5097 mlx5e_rq_replace_xdp_prog(&c->rq, prog);
5098 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state)) {
5099 bpf_prog_inc(prog);
5100 mlx5e_rq_replace_xdp_prog(&c->xskrq, prog);
5101 }
5102 }
5103
5104 unlock:
5105 mutex_unlock(&priv->state_lock);
5106
5107 /* Need to fix some features. */
5108 if (!err)
5109 netdev_update_features(netdev);
5110
5111 return err;
5112 }
5113
mlx5e_xdp(struct net_device * dev,struct netdev_bpf * xdp)5114 static int mlx5e_xdp(struct net_device *dev, struct netdev_bpf *xdp)
5115 {
5116 switch (xdp->command) {
5117 case XDP_SETUP_PROG:
5118 return mlx5e_xdp_set(dev, xdp->prog);
5119 case XDP_SETUP_XSK_POOL:
5120 return mlx5e_xsk_setup_pool(dev, xdp->xsk.pool,
5121 xdp->xsk.queue_id);
5122 default:
5123 return -EINVAL;
5124 }
5125 }
5126
5127 #ifdef CONFIG_MLX5_ESWITCH
mlx5e_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 filter_mask,int nlflags)5128 static int mlx5e_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
5129 struct net_device *dev, u32 filter_mask,
5130 int nlflags)
5131 {
5132 struct mlx5e_priv *priv = netdev_priv(dev);
5133 struct mlx5_core_dev *mdev = priv->mdev;
5134 u8 mode, setting;
5135
5136 if (mlx5_eswitch_get_vepa(mdev->priv.eswitch, &setting))
5137 return -EOPNOTSUPP;
5138 mode = setting ? BRIDGE_MODE_VEPA : BRIDGE_MODE_VEB;
5139 return ndo_dflt_bridge_getlink(skb, pid, seq, dev,
5140 mode,
5141 0, 0, nlflags, filter_mask, NULL);
5142 }
5143
mlx5e_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)5144 static int mlx5e_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
5145 u16 flags, struct netlink_ext_ack *extack)
5146 {
5147 struct mlx5e_priv *priv = netdev_priv(dev);
5148 struct mlx5_core_dev *mdev = priv->mdev;
5149 struct nlattr *attr, *br_spec;
5150 u16 mode = BRIDGE_MODE_UNDEF;
5151 u8 setting;
5152 int rem;
5153
5154 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5155 if (!br_spec)
5156 return -EINVAL;
5157
5158 nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
5159 mode = nla_get_u16(attr);
5160 if (mode > BRIDGE_MODE_VEPA)
5161 return -EINVAL;
5162
5163 break;
5164 }
5165
5166 if (mode == BRIDGE_MODE_UNDEF)
5167 return -EINVAL;
5168
5169 setting = (mode == BRIDGE_MODE_VEPA) ? 1 : 0;
5170 return mlx5_eswitch_set_vepa(mdev->priv.eswitch, setting);
5171 }
5172 #endif
5173
5174 const struct net_device_ops mlx5e_netdev_ops = {
5175 .ndo_open = mlx5e_open,
5176 .ndo_stop = mlx5e_close,
5177 .ndo_start_xmit = mlx5e_xmit,
5178 .ndo_setup_tc = mlx5e_setup_tc,
5179 .ndo_select_queue = mlx5e_select_queue,
5180 .ndo_get_stats64 = mlx5e_get_stats,
5181 .ndo_set_rx_mode = mlx5e_set_rx_mode,
5182 .ndo_set_mac_address = mlx5e_set_mac,
5183 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
5184 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
5185 .ndo_set_features = mlx5e_set_features,
5186 .ndo_fix_features = mlx5e_fix_features,
5187 .ndo_change_mtu = mlx5e_change_nic_mtu,
5188 .ndo_eth_ioctl = mlx5e_ioctl,
5189 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
5190 .ndo_features_check = mlx5e_features_check,
5191 .ndo_tx_timeout = mlx5e_tx_timeout,
5192 .ndo_bpf = mlx5e_xdp,
5193 .ndo_xdp_xmit = mlx5e_xdp_xmit,
5194 .ndo_xsk_wakeup = mlx5e_xsk_wakeup,
5195 #ifdef CONFIG_MLX5_EN_ARFS
5196 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
5197 #endif
5198 #ifdef CONFIG_MLX5_ESWITCH
5199 .ndo_bridge_setlink = mlx5e_bridge_setlink,
5200 .ndo_bridge_getlink = mlx5e_bridge_getlink,
5201
5202 /* SRIOV E-Switch NDOs */
5203 .ndo_set_vf_mac = mlx5e_set_vf_mac,
5204 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
5205 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
5206 .ndo_set_vf_trust = mlx5e_set_vf_trust,
5207 .ndo_set_vf_rate = mlx5e_set_vf_rate,
5208 .ndo_get_vf_config = mlx5e_get_vf_config,
5209 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
5210 .ndo_get_vf_stats = mlx5e_get_vf_stats,
5211 .ndo_has_offload_stats = mlx5e_has_offload_stats,
5212 .ndo_get_offload_stats = mlx5e_get_offload_stats,
5213 #endif
5214 };
5215
mlx5e_build_nic_params(struct mlx5e_priv * priv,struct mlx5e_xsk * xsk,u16 mtu)5216 void mlx5e_build_nic_params(struct mlx5e_priv *priv, struct mlx5e_xsk *xsk, u16 mtu)
5217 {
5218 struct mlx5e_params *params = &priv->channels.params;
5219 struct mlx5_core_dev *mdev = priv->mdev;
5220
5221 params->sw_mtu = mtu;
5222 params->hard_mtu = MLX5E_ETH_HARD_MTU;
5223 params->num_channels = min_t(unsigned int, MLX5E_MAX_NUM_CHANNELS / 2,
5224 priv->max_nch);
5225 mlx5e_params_mqprio_reset(params);
5226
5227 /* SQ */
5228 params->log_sq_size = is_kdump_kernel() ?
5229 MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE :
5230 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
5231 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_SKB_TX_MPWQE, mlx5e_tx_mpwqe_supported(mdev));
5232
5233 /* XDP SQ */
5234 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_XDP_TX_MPWQE, mlx5e_tx_mpwqe_supported(mdev));
5235
5236 /* set CQE compression */
5237 params->rx_cqe_compress_def = false;
5238 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
5239 MLX5_CAP_GEN(mdev, vport_group_manager))
5240 params->rx_cqe_compress_def = slow_pci_heuristic(mdev);
5241
5242 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS, params->rx_cqe_compress_def);
5243 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_NO_CSUM_COMPLETE, false);
5244
5245 /* RQ */
5246 mlx5e_build_rq_params(mdev, params);
5247
5248 params->terminate_lkey_be = mlx5_core_get_terminate_scatter_list_mkey(mdev);
5249
5250 params->packet_merge.timeout = mlx5e_choose_lro_timeout(mdev, MLX5E_DEFAULT_LRO_TIMEOUT);
5251
5252 /* CQ moderation params */
5253 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation) &&
5254 MLX5_CAP_GEN(mdev, cq_period_mode_modify);
5255 params->tx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation) &&
5256 MLX5_CAP_GEN(mdev, cq_period_mode_modify);
5257 params->rx_moder_use_cqe_mode = !!MLX5_CAP_GEN(mdev, cq_period_start_from_cqe);
5258 params->tx_moder_use_cqe_mode = false;
5259 mlx5e_reset_rx_moderation(¶ms->rx_cq_moderation, params->rx_moder_use_cqe_mode,
5260 params->rx_dim_enabled);
5261 mlx5e_reset_tx_moderation(¶ms->tx_cq_moderation, params->tx_moder_use_cqe_mode,
5262 params->tx_dim_enabled);
5263
5264 /* TX inline */
5265 mlx5_query_min_inline(mdev, ¶ms->tx_min_inline_mode);
5266
5267 /* AF_XDP */
5268 params->xsk = xsk;
5269
5270 /* Do not update netdev->features directly in here
5271 * on mlx5e_attach_netdev() we will call mlx5e_update_features()
5272 * To update netdev->features please modify mlx5e_fix_features()
5273 */
5274 }
5275
mlx5e_set_netdev_dev_addr(struct net_device * netdev)5276 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
5277 {
5278 struct mlx5e_priv *priv = netdev_priv(netdev);
5279 u8 addr[ETH_ALEN];
5280
5281 mlx5_query_mac_address(priv->mdev, addr);
5282 if (is_zero_ether_addr(addr) &&
5283 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
5284 eth_hw_addr_random(netdev);
5285 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
5286 return;
5287 }
5288
5289 eth_hw_addr_set(netdev, addr);
5290 }
5291
mlx5e_vxlan_set_port(struct net_device * netdev,unsigned int table,unsigned int entry,struct udp_tunnel_info * ti)5292 static int mlx5e_vxlan_set_port(struct net_device *netdev, unsigned int table,
5293 unsigned int entry, struct udp_tunnel_info *ti)
5294 {
5295 struct mlx5e_priv *priv = netdev_priv(netdev);
5296
5297 return mlx5_vxlan_add_port(priv->mdev->vxlan, ntohs(ti->port));
5298 }
5299
mlx5e_vxlan_unset_port(struct net_device * netdev,unsigned int table,unsigned int entry,struct udp_tunnel_info * ti)5300 static int mlx5e_vxlan_unset_port(struct net_device *netdev, unsigned int table,
5301 unsigned int entry, struct udp_tunnel_info *ti)
5302 {
5303 struct mlx5e_priv *priv = netdev_priv(netdev);
5304
5305 return mlx5_vxlan_del_port(priv->mdev->vxlan, ntohs(ti->port));
5306 }
5307
mlx5e_vxlan_set_netdev_info(struct mlx5e_priv * priv)5308 void mlx5e_vxlan_set_netdev_info(struct mlx5e_priv *priv)
5309 {
5310 if (!mlx5_vxlan_allowed(priv->mdev->vxlan))
5311 return;
5312
5313 priv->nic_info.set_port = mlx5e_vxlan_set_port;
5314 priv->nic_info.unset_port = mlx5e_vxlan_unset_port;
5315 priv->nic_info.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
5316 UDP_TUNNEL_NIC_INFO_STATIC_IANA_VXLAN;
5317 priv->nic_info.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN;
5318 /* Don't count the space hard-coded to the IANA port */
5319 priv->nic_info.tables[0].n_entries =
5320 mlx5_vxlan_max_udp_ports(priv->mdev) - 1;
5321
5322 priv->netdev->udp_tunnel_nic_info = &priv->nic_info;
5323 }
5324
mlx5e_tunnel_any_tx_proto_supported(struct mlx5_core_dev * mdev)5325 static bool mlx5e_tunnel_any_tx_proto_supported(struct mlx5_core_dev *mdev)
5326 {
5327 int tt;
5328
5329 for (tt = 0; tt < MLX5_NUM_TUNNEL_TT; tt++) {
5330 if (mlx5e_tunnel_proto_supported_tx(mdev, mlx5_get_proto_by_tunnel_type(tt)))
5331 return true;
5332 }
5333 return (mlx5_vxlan_allowed(mdev->vxlan) || mlx5_geneve_tx_allowed(mdev));
5334 }
5335
mlx5e_get_queue_stats_rx(struct net_device * dev,int i,struct netdev_queue_stats_rx * stats)5336 static void mlx5e_get_queue_stats_rx(struct net_device *dev, int i,
5337 struct netdev_queue_stats_rx *stats)
5338 {
5339 struct mlx5e_priv *priv = netdev_priv(dev);
5340 struct mlx5e_channel_stats *channel_stats;
5341 struct mlx5e_rq_stats *xskrq_stats;
5342 struct mlx5e_rq_stats *rq_stats;
5343
5344 ASSERT_RTNL();
5345 if (mlx5e_is_uplink_rep(priv) || !priv->stats_nch)
5346 return;
5347
5348 channel_stats = priv->channel_stats[i];
5349 xskrq_stats = &channel_stats->xskrq;
5350 rq_stats = &channel_stats->rq;
5351
5352 stats->packets = rq_stats->packets + xskrq_stats->packets;
5353 stats->bytes = rq_stats->bytes + xskrq_stats->bytes;
5354 stats->alloc_fail = rq_stats->buff_alloc_err +
5355 xskrq_stats->buff_alloc_err;
5356 }
5357
mlx5e_get_queue_stats_tx(struct net_device * dev,int i,struct netdev_queue_stats_tx * stats)5358 static void mlx5e_get_queue_stats_tx(struct net_device *dev, int i,
5359 struct netdev_queue_stats_tx *stats)
5360 {
5361 struct mlx5e_priv *priv = netdev_priv(dev);
5362 struct mlx5e_sq_stats *sq_stats;
5363
5364 ASSERT_RTNL();
5365 if (!priv->stats_nch)
5366 return;
5367
5368 /* no special case needed for ptp htb etc since txq2sq_stats is kept up
5369 * to date for active sq_stats, otherwise get_base_stats takes care of
5370 * inactive sqs.
5371 */
5372 sq_stats = priv->txq2sq_stats[i];
5373 stats->packets = sq_stats->packets;
5374 stats->bytes = sq_stats->bytes;
5375 }
5376
mlx5e_get_base_stats(struct net_device * dev,struct netdev_queue_stats_rx * rx,struct netdev_queue_stats_tx * tx)5377 static void mlx5e_get_base_stats(struct net_device *dev,
5378 struct netdev_queue_stats_rx *rx,
5379 struct netdev_queue_stats_tx *tx)
5380 {
5381 struct mlx5e_priv *priv = netdev_priv(dev);
5382 struct mlx5e_ptp *ptp_channel;
5383 int i, tc;
5384
5385 ASSERT_RTNL();
5386 if (!mlx5e_is_uplink_rep(priv)) {
5387 rx->packets = 0;
5388 rx->bytes = 0;
5389 rx->alloc_fail = 0;
5390
5391 for (i = priv->channels.params.num_channels; i < priv->stats_nch; i++) {
5392 struct netdev_queue_stats_rx rx_i = {0};
5393
5394 mlx5e_get_queue_stats_rx(dev, i, &rx_i);
5395
5396 rx->packets += rx_i.packets;
5397 rx->bytes += rx_i.bytes;
5398 rx->alloc_fail += rx_i.alloc_fail;
5399 }
5400
5401 /* always report PTP RX stats from base as there is no
5402 * corresponding channel to report them under in
5403 * mlx5e_get_queue_stats_rx.
5404 */
5405 if (priv->rx_ptp_opened) {
5406 struct mlx5e_rq_stats *rq_stats = &priv->ptp_stats.rq;
5407
5408 rx->packets += rq_stats->packets;
5409 rx->bytes += rq_stats->bytes;
5410 }
5411 }
5412
5413 tx->packets = 0;
5414 tx->bytes = 0;
5415
5416 for (i = 0; i < priv->stats_nch; i++) {
5417 struct mlx5e_channel_stats *channel_stats = priv->channel_stats[i];
5418
5419 /* handle two cases:
5420 *
5421 * 1. channels which are active. In this case,
5422 * report only deactivated TCs on these channels.
5423 *
5424 * 2. channels which were deactivated
5425 * (i > priv->channels.params.num_channels)
5426 * must have all of their TCs [0 .. priv->max_opened_tc)
5427 * examined because deactivated channels will not be in the
5428 * range of [0..real_num_tx_queues) and will not have their
5429 * stats reported by mlx5e_get_queue_stats_tx.
5430 */
5431 if (i < priv->channels.params.num_channels)
5432 tc = mlx5e_get_dcb_num_tc(&priv->channels.params);
5433 else
5434 tc = 0;
5435
5436 for (; tc < priv->max_opened_tc; tc++) {
5437 struct mlx5e_sq_stats *sq_stats = &channel_stats->sq[tc];
5438
5439 tx->packets += sq_stats->packets;
5440 tx->bytes += sq_stats->bytes;
5441 }
5442 }
5443
5444 /* if PTP TX was opened at some point and has since either:
5445 * - been shutdown and set to NULL, or
5446 * - simply disabled (bit unset)
5447 *
5448 * report stats directly from the ptp_stats structures as these queues
5449 * are now unavailable and there is no txq index to retrieve these
5450 * stats via calls to mlx5e_get_queue_stats_tx.
5451 */
5452 ptp_channel = priv->channels.ptp;
5453 if (priv->tx_ptp_opened && (!ptp_channel || !test_bit(MLX5E_PTP_STATE_TX, ptp_channel->state))) {
5454 for (tc = 0; tc < priv->max_opened_tc; tc++) {
5455 struct mlx5e_sq_stats *sq_stats = &priv->ptp_stats.sq[tc];
5456
5457 tx->packets += sq_stats->packets;
5458 tx->bytes += sq_stats->bytes;
5459 }
5460 }
5461 }
5462
5463 static const struct netdev_stat_ops mlx5e_stat_ops = {
5464 .get_queue_stats_rx = mlx5e_get_queue_stats_rx,
5465 .get_queue_stats_tx = mlx5e_get_queue_stats_tx,
5466 .get_base_stats = mlx5e_get_base_stats,
5467 };
5468
mlx5e_build_nic_netdev(struct net_device * netdev)5469 static void mlx5e_build_nic_netdev(struct net_device *netdev)
5470 {
5471 struct mlx5e_priv *priv = netdev_priv(netdev);
5472 struct mlx5_core_dev *mdev = priv->mdev;
5473 bool fcs_supported;
5474 bool fcs_enabled;
5475
5476 SET_NETDEV_DEV(netdev, mdev->device);
5477
5478 netdev->netdev_ops = &mlx5e_netdev_ops;
5479 netdev->xdp_metadata_ops = &mlx5e_xdp_metadata_ops;
5480 netdev->xsk_tx_metadata_ops = &mlx5e_xsk_tx_metadata_ops;
5481
5482 mlx5e_dcbnl_build_netdev(netdev);
5483
5484 netdev->watchdog_timeo = 15 * HZ;
5485
5486 netdev->stat_ops = &mlx5e_stat_ops;
5487 netdev->ethtool_ops = &mlx5e_ethtool_ops;
5488
5489 netdev->vlan_features |= NETIF_F_SG;
5490 netdev->vlan_features |= NETIF_F_HW_CSUM;
5491 netdev->vlan_features |= NETIF_F_HW_MACSEC;
5492 netdev->vlan_features |= NETIF_F_GRO;
5493 netdev->vlan_features |= NETIF_F_TSO;
5494 netdev->vlan_features |= NETIF_F_TSO6;
5495 netdev->vlan_features |= NETIF_F_RXCSUM;
5496 netdev->vlan_features |= NETIF_F_RXHASH;
5497 netdev->vlan_features |= NETIF_F_GSO_PARTIAL;
5498
5499 netdev->mpls_features |= NETIF_F_SG;
5500 netdev->mpls_features |= NETIF_F_HW_CSUM;
5501 netdev->mpls_features |= NETIF_F_TSO;
5502 netdev->mpls_features |= NETIF_F_TSO6;
5503
5504 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_TX;
5505 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_RX;
5506
5507 /* Tunneled LRO is not supported in the driver, and the same RQs are
5508 * shared between inner and outer TIRs, so the driver can't disable LRO
5509 * for inner TIRs while having it enabled for outer TIRs. Due to this,
5510 * block LRO altogether if the firmware declares tunneled LRO support.
5511 */
5512 if (!!MLX5_CAP_ETH(mdev, lro_cap) &&
5513 !MLX5_CAP_ETH(mdev, tunnel_lro_vxlan) &&
5514 !MLX5_CAP_ETH(mdev, tunnel_lro_gre) &&
5515 mlx5e_check_fragmented_striding_rq_cap(mdev, PAGE_SHIFT,
5516 MLX5E_MPWRQ_UMR_MODE_ALIGNED))
5517 netdev->vlan_features |= NETIF_F_LRO;
5518
5519 netdev->hw_features = netdev->vlan_features;
5520 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
5521 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
5522 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
5523 netdev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
5524
5525 if (mlx5e_hw_gro_supported(mdev) &&
5526 mlx5e_check_fragmented_striding_rq_cap(mdev, PAGE_SHIFT,
5527 MLX5E_MPWRQ_UMR_MODE_ALIGNED))
5528 netdev->hw_features |= NETIF_F_GRO_HW;
5529
5530 if (mlx5e_tunnel_any_tx_proto_supported(mdev)) {
5531 netdev->hw_enc_features |= NETIF_F_HW_CSUM;
5532 netdev->hw_enc_features |= NETIF_F_TSO;
5533 netdev->hw_enc_features |= NETIF_F_TSO6;
5534 netdev->hw_enc_features |= NETIF_F_GSO_PARTIAL;
5535 }
5536
5537 if (mlx5_vxlan_allowed(mdev->vxlan) || mlx5_geneve_tx_allowed(mdev)) {
5538 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
5539 NETIF_F_GSO_UDP_TUNNEL_CSUM;
5540 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
5541 NETIF_F_GSO_UDP_TUNNEL_CSUM;
5542 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
5543 netdev->vlan_features |= NETIF_F_GSO_UDP_TUNNEL |
5544 NETIF_F_GSO_UDP_TUNNEL_CSUM;
5545 }
5546
5547 if (mlx5e_tunnel_proto_supported_tx(mdev, IPPROTO_GRE)) {
5548 netdev->hw_features |= NETIF_F_GSO_GRE |
5549 NETIF_F_GSO_GRE_CSUM;
5550 netdev->hw_enc_features |= NETIF_F_GSO_GRE |
5551 NETIF_F_GSO_GRE_CSUM;
5552 netdev->gso_partial_features |= NETIF_F_GSO_GRE |
5553 NETIF_F_GSO_GRE_CSUM;
5554 }
5555
5556 if (mlx5e_tunnel_proto_supported_tx(mdev, IPPROTO_IPIP)) {
5557 netdev->hw_features |= NETIF_F_GSO_IPXIP4 |
5558 NETIF_F_GSO_IPXIP6;
5559 netdev->hw_enc_features |= NETIF_F_GSO_IPXIP4 |
5560 NETIF_F_GSO_IPXIP6;
5561 netdev->gso_partial_features |= NETIF_F_GSO_IPXIP4 |
5562 NETIF_F_GSO_IPXIP6;
5563 }
5564
5565 netdev->gso_partial_features |= NETIF_F_GSO_UDP_L4;
5566 netdev->hw_features |= NETIF_F_GSO_UDP_L4;
5567
5568 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
5569
5570 if (fcs_supported)
5571 netdev->hw_features |= NETIF_F_RXALL;
5572
5573 if (MLX5_CAP_ETH(mdev, scatter_fcs))
5574 netdev->hw_features |= NETIF_F_RXFCS;
5575
5576 if (mlx5_qos_is_supported(mdev))
5577 netdev->hw_features |= NETIF_F_HW_TC;
5578
5579 netdev->features = netdev->hw_features;
5580
5581 /* Defaults */
5582 if (fcs_enabled)
5583 netdev->features &= ~NETIF_F_RXALL;
5584 netdev->features &= ~NETIF_F_LRO;
5585 netdev->features &= ~NETIF_F_GRO_HW;
5586 netdev->features &= ~NETIF_F_RXFCS;
5587
5588 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
5589 if (FT_CAP(flow_modify_en) &&
5590 FT_CAP(modify_root) &&
5591 FT_CAP(identified_miss_table_mode) &&
5592 FT_CAP(flow_table_modify)) {
5593 #if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
5594 netdev->hw_features |= NETIF_F_HW_TC;
5595 #endif
5596 #if IS_ENABLED(CONFIG_MLX5_EN_ARFS)
5597 netdev->hw_features |= NETIF_F_NTUPLE;
5598 #elif IS_ENABLED(CONFIG_MLX5_EN_RXNFC)
5599 netdev->features |= NETIF_F_NTUPLE;
5600 #endif
5601 }
5602
5603 netdev->features |= NETIF_F_HIGHDMA;
5604 netdev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
5605
5606 netdev->priv_flags |= IFF_UNICAST_FLT;
5607
5608 netif_set_tso_max_size(netdev, GSO_MAX_SIZE);
5609 mlx5e_set_xdp_feature(netdev);
5610 mlx5e_set_netdev_dev_addr(netdev);
5611 mlx5e_macsec_build_netdev(priv);
5612 mlx5e_ipsec_build_netdev(priv);
5613 mlx5e_ktls_build_netdev(priv);
5614 }
5615
mlx5e_create_q_counters(struct mlx5e_priv * priv)5616 void mlx5e_create_q_counters(struct mlx5e_priv *priv)
5617 {
5618 u32 out[MLX5_ST_SZ_DW(alloc_q_counter_out)] = {};
5619 u32 in[MLX5_ST_SZ_DW(alloc_q_counter_in)] = {};
5620 struct mlx5_core_dev *mdev = priv->mdev;
5621 struct mlx5_core_dev *pos;
5622 int err, i;
5623
5624 MLX5_SET(alloc_q_counter_in, in, opcode, MLX5_CMD_OP_ALLOC_Q_COUNTER);
5625
5626 mlx5_sd_for_each_dev(i, mdev, pos) {
5627 err = mlx5_cmd_exec_inout(pos, alloc_q_counter, in, out);
5628 if (!err)
5629 priv->q_counter[i] =
5630 MLX5_GET(alloc_q_counter_out, out, counter_set_id);
5631 }
5632
5633 err = mlx5_cmd_exec_inout(mdev, alloc_q_counter, in, out);
5634 if (!err)
5635 priv->drop_rq_q_counter =
5636 MLX5_GET(alloc_q_counter_out, out, counter_set_id);
5637 }
5638
mlx5e_destroy_q_counters(struct mlx5e_priv * priv)5639 void mlx5e_destroy_q_counters(struct mlx5e_priv *priv)
5640 {
5641 u32 in[MLX5_ST_SZ_DW(dealloc_q_counter_in)] = {};
5642 struct mlx5_core_dev *pos;
5643 int i;
5644
5645 MLX5_SET(dealloc_q_counter_in, in, opcode,
5646 MLX5_CMD_OP_DEALLOC_Q_COUNTER);
5647 mlx5_sd_for_each_dev(i, priv->mdev, pos) {
5648 if (priv->q_counter[i]) {
5649 MLX5_SET(dealloc_q_counter_in, in, counter_set_id,
5650 priv->q_counter[i]);
5651 mlx5_cmd_exec_in(pos, dealloc_q_counter, in);
5652 }
5653 }
5654
5655 if (priv->drop_rq_q_counter) {
5656 MLX5_SET(dealloc_q_counter_in, in, counter_set_id,
5657 priv->drop_rq_q_counter);
5658 mlx5_cmd_exec_in(priv->mdev, dealloc_q_counter, in);
5659 }
5660 }
5661
mlx5e_nic_init(struct mlx5_core_dev * mdev,struct net_device * netdev)5662 static int mlx5e_nic_init(struct mlx5_core_dev *mdev,
5663 struct net_device *netdev)
5664 {
5665 const bool take_rtnl = netdev->reg_state == NETREG_REGISTERED;
5666 struct mlx5e_priv *priv = netdev_priv(netdev);
5667 struct mlx5e_flow_steering *fs;
5668 int err;
5669
5670 mlx5e_build_nic_params(priv, &priv->xsk, netdev->mtu);
5671 mlx5e_vxlan_set_netdev_info(priv);
5672
5673 mlx5e_timestamp_init(priv);
5674
5675 priv->dfs_root = debugfs_create_dir("nic",
5676 mlx5_debugfs_get_dev_root(mdev));
5677
5678 fs = mlx5e_fs_init(priv->profile, mdev,
5679 !test_bit(MLX5E_STATE_DESTROYING, &priv->state),
5680 priv->dfs_root);
5681 if (!fs) {
5682 err = -ENOMEM;
5683 mlx5_core_err(mdev, "FS initialization failed, %d\n", err);
5684 debugfs_remove_recursive(priv->dfs_root);
5685 return err;
5686 }
5687 priv->fs = fs;
5688
5689 err = mlx5e_ktls_init(priv);
5690 if (err)
5691 mlx5_core_err(mdev, "TLS initialization failed, %d\n", err);
5692
5693 mlx5e_health_create_reporters(priv);
5694
5695 /* If netdev is already registered (e.g. move from uplink to nic profile),
5696 * RTNL lock must be held before triggering netdev notifiers.
5697 */
5698 if (take_rtnl)
5699 rtnl_lock();
5700
5701 /* update XDP supported features */
5702 mlx5e_set_xdp_feature(netdev);
5703
5704 if (take_rtnl)
5705 rtnl_unlock();
5706
5707 return 0;
5708 }
5709
mlx5e_nic_cleanup(struct mlx5e_priv * priv)5710 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
5711 {
5712 mlx5e_health_destroy_reporters(priv);
5713 mlx5e_ktls_cleanup(priv);
5714 mlx5e_fs_cleanup(priv->fs);
5715 debugfs_remove_recursive(priv->dfs_root);
5716 priv->fs = NULL;
5717 }
5718
mlx5e_init_nic_rx(struct mlx5e_priv * priv)5719 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv)
5720 {
5721 struct mlx5_core_dev *mdev = priv->mdev;
5722 enum mlx5e_rx_res_features features;
5723 int err;
5724
5725 mlx5e_create_q_counters(priv);
5726
5727 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
5728 if (err) {
5729 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
5730 goto err_destroy_q_counters;
5731 }
5732
5733 features = MLX5E_RX_RES_FEATURE_PTP;
5734 if (mlx5_tunnel_inner_ft_supported(mdev))
5735 features |= MLX5E_RX_RES_FEATURE_INNER_FT;
5736 if (mlx5_get_sd(priv->mdev))
5737 features |= MLX5E_RX_RES_FEATURE_MULTI_VHCA;
5738
5739 priv->rx_res = mlx5e_rx_res_create(priv->mdev, features, priv->max_nch, priv->drop_rq.rqn,
5740 &priv->channels.params.packet_merge,
5741 priv->channels.params.num_channels);
5742 if (IS_ERR(priv->rx_res)) {
5743 err = PTR_ERR(priv->rx_res);
5744 priv->rx_res = NULL;
5745 mlx5_core_err(mdev, "create rx resources failed, %d\n", err);
5746 goto err_close_drop_rq;
5747 }
5748
5749 err = mlx5e_create_flow_steering(priv->fs, priv->rx_res, priv->profile,
5750 priv->netdev);
5751 if (err) {
5752 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
5753 goto err_destroy_rx_res;
5754 }
5755
5756 err = mlx5e_tc_nic_init(priv);
5757 if (err)
5758 goto err_destroy_flow_steering;
5759
5760 err = mlx5e_accel_init_rx(priv);
5761 if (err)
5762 goto err_tc_nic_cleanup;
5763
5764 #ifdef CONFIG_MLX5_EN_ARFS
5765 priv->netdev->rx_cpu_rmap = mlx5_eq_table_get_rmap(priv->mdev);
5766 #endif
5767
5768 return 0;
5769
5770 err_tc_nic_cleanup:
5771 mlx5e_tc_nic_cleanup(priv);
5772 err_destroy_flow_steering:
5773 mlx5e_destroy_flow_steering(priv->fs, mlx5e_fs_has_arfs(priv->netdev),
5774 priv->profile);
5775 err_destroy_rx_res:
5776 mlx5e_rx_res_destroy(priv->rx_res);
5777 priv->rx_res = NULL;
5778 err_close_drop_rq:
5779 mlx5e_close_drop_rq(&priv->drop_rq);
5780 err_destroy_q_counters:
5781 mlx5e_destroy_q_counters(priv);
5782 return err;
5783 }
5784
mlx5e_cleanup_nic_rx(struct mlx5e_priv * priv)5785 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv)
5786 {
5787 mlx5e_accel_cleanup_rx(priv);
5788 mlx5e_tc_nic_cleanup(priv);
5789 mlx5e_destroy_flow_steering(priv->fs, mlx5e_fs_has_arfs(priv->netdev),
5790 priv->profile);
5791 mlx5e_rx_res_destroy(priv->rx_res);
5792 priv->rx_res = NULL;
5793 mlx5e_close_drop_rq(&priv->drop_rq);
5794 mlx5e_destroy_q_counters(priv);
5795 }
5796
mlx5e_set_mqprio_rl(struct mlx5e_priv * priv)5797 static void mlx5e_set_mqprio_rl(struct mlx5e_priv *priv)
5798 {
5799 struct mlx5e_params *params;
5800 struct mlx5e_mqprio_rl *rl;
5801
5802 params = &priv->channels.params;
5803 if (params->mqprio.mode != TC_MQPRIO_MODE_CHANNEL)
5804 return;
5805
5806 rl = mlx5e_mqprio_rl_create(priv->mdev, params->mqprio.num_tc,
5807 params->mqprio.channel.max_rate);
5808 if (IS_ERR(rl))
5809 rl = NULL;
5810 priv->mqprio_rl = rl;
5811 mlx5e_mqprio_rl_update_params(params, rl);
5812 }
5813
mlx5e_init_nic_tx(struct mlx5e_priv * priv)5814 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv)
5815 {
5816 int err;
5817
5818 err = mlx5e_accel_init_tx(priv);
5819 if (err)
5820 return err;
5821
5822 mlx5e_set_mqprio_rl(priv);
5823 mlx5e_dcbnl_initialize(priv);
5824 return 0;
5825 }
5826
mlx5e_nic_enable(struct mlx5e_priv * priv)5827 static void mlx5e_nic_enable(struct mlx5e_priv *priv)
5828 {
5829 struct net_device *netdev = priv->netdev;
5830 struct mlx5_core_dev *mdev = priv->mdev;
5831 int err;
5832
5833 mlx5e_fs_init_l2_addr(priv->fs, netdev);
5834 mlx5e_ipsec_init(priv);
5835
5836 err = mlx5e_macsec_init(priv);
5837 if (err)
5838 mlx5_core_err(mdev, "MACsec initialization failed, %d\n", err);
5839
5840 /* Marking the link as currently not needed by the Driver */
5841 if (!netif_running(netdev))
5842 mlx5e_modify_admin_state(mdev, MLX5_PORT_DOWN);
5843
5844 mlx5e_set_netdev_mtu_boundaries(priv);
5845 mlx5e_set_dev_port_mtu(priv);
5846
5847 mlx5_lag_add_netdev(mdev, netdev);
5848
5849 mlx5e_enable_async_events(priv);
5850 mlx5e_enable_blocking_events(priv);
5851 if (mlx5e_monitor_counter_supported(priv))
5852 mlx5e_monitor_counter_init(priv);
5853
5854 mlx5e_hv_vhca_stats_create(priv);
5855 if (netdev->reg_state != NETREG_REGISTERED)
5856 return;
5857 mlx5e_dcbnl_init_app(priv);
5858
5859 mlx5e_nic_set_rx_mode(priv);
5860
5861 rtnl_lock();
5862 if (netif_running(netdev))
5863 mlx5e_open(netdev);
5864 udp_tunnel_nic_reset_ntf(priv->netdev);
5865 netif_device_attach(netdev);
5866 rtnl_unlock();
5867 }
5868
mlx5e_nic_disable(struct mlx5e_priv * priv)5869 static void mlx5e_nic_disable(struct mlx5e_priv *priv)
5870 {
5871 struct mlx5_core_dev *mdev = priv->mdev;
5872
5873 if (priv->netdev->reg_state == NETREG_REGISTERED)
5874 mlx5e_dcbnl_delete_app(priv);
5875
5876 rtnl_lock();
5877 if (netif_running(priv->netdev))
5878 mlx5e_close(priv->netdev);
5879 netif_device_detach(priv->netdev);
5880 rtnl_unlock();
5881
5882 mlx5e_nic_set_rx_mode(priv);
5883
5884 mlx5e_hv_vhca_stats_destroy(priv);
5885 if (mlx5e_monitor_counter_supported(priv))
5886 mlx5e_monitor_counter_cleanup(priv);
5887
5888 mlx5e_disable_blocking_events(priv);
5889 if (priv->en_trap) {
5890 mlx5e_deactivate_trap(priv);
5891 mlx5e_close_trap(priv->en_trap);
5892 priv->en_trap = NULL;
5893 }
5894 mlx5e_disable_async_events(priv);
5895 mlx5_lag_remove_netdev(mdev, priv->netdev);
5896 mlx5_vxlan_reset_to_default(mdev->vxlan);
5897 mlx5e_macsec_cleanup(priv);
5898 mlx5e_ipsec_cleanup(priv);
5899 }
5900
mlx5e_update_nic_rx(struct mlx5e_priv * priv)5901 static int mlx5e_update_nic_rx(struct mlx5e_priv *priv)
5902 {
5903 return mlx5e_refresh_tirs(priv, false, false);
5904 }
5905
5906 static const struct mlx5e_profile mlx5e_nic_profile = {
5907 .init = mlx5e_nic_init,
5908 .cleanup = mlx5e_nic_cleanup,
5909 .init_rx = mlx5e_init_nic_rx,
5910 .cleanup_rx = mlx5e_cleanup_nic_rx,
5911 .init_tx = mlx5e_init_nic_tx,
5912 .cleanup_tx = mlx5e_cleanup_nic_tx,
5913 .enable = mlx5e_nic_enable,
5914 .disable = mlx5e_nic_disable,
5915 .update_rx = mlx5e_update_nic_rx,
5916 .update_stats = mlx5e_stats_update_ndo_stats,
5917 .update_carrier = mlx5e_update_carrier,
5918 .rx_handlers = &mlx5e_rx_handlers_nic,
5919 .max_tc = MLX5_MAX_NUM_TC,
5920 .stats_grps = mlx5e_nic_stats_grps,
5921 .stats_grps_num = mlx5e_nic_stats_grps_num,
5922 .features = BIT(MLX5E_PROFILE_FEATURE_PTP_RX) |
5923 BIT(MLX5E_PROFILE_FEATURE_PTP_TX) |
5924 BIT(MLX5E_PROFILE_FEATURE_QOS_HTB) |
5925 BIT(MLX5E_PROFILE_FEATURE_FS_VLAN) |
5926 BIT(MLX5E_PROFILE_FEATURE_FS_TC),
5927 };
5928
mlx5e_profile_max_num_channels(struct mlx5_core_dev * mdev,const struct mlx5e_profile * profile)5929 static int mlx5e_profile_max_num_channels(struct mlx5_core_dev *mdev,
5930 const struct mlx5e_profile *profile)
5931 {
5932 int nch;
5933
5934 nch = mlx5e_get_max_num_channels(mdev);
5935
5936 if (profile->max_nch_limit)
5937 nch = min_t(int, nch, profile->max_nch_limit(mdev));
5938 return nch;
5939 }
5940
5941 static unsigned int
mlx5e_calc_max_nch(struct mlx5_core_dev * mdev,struct net_device * netdev,const struct mlx5e_profile * profile)5942 mlx5e_calc_max_nch(struct mlx5_core_dev *mdev, struct net_device *netdev,
5943 const struct mlx5e_profile *profile)
5944
5945 {
5946 unsigned int max_nch, tmp;
5947
5948 /* core resources */
5949 max_nch = mlx5e_profile_max_num_channels(mdev, profile);
5950
5951 /* netdev rx queues */
5952 max_nch = min_t(unsigned int, max_nch, netdev->num_rx_queues);
5953
5954 /* netdev tx queues */
5955 tmp = netdev->num_tx_queues;
5956 if (mlx5_qos_is_supported(mdev))
5957 tmp -= mlx5e_qos_max_leaf_nodes(mdev);
5958 if (MLX5_CAP_GEN(mdev, ts_cqe_to_dest_cqn))
5959 tmp -= profile->max_tc;
5960 tmp = tmp / profile->max_tc;
5961 max_nch = min_t(unsigned int, max_nch, tmp);
5962
5963 return max_nch;
5964 }
5965
mlx5e_get_pf_num_tirs(struct mlx5_core_dev * mdev)5966 int mlx5e_get_pf_num_tirs(struct mlx5_core_dev *mdev)
5967 {
5968 /* Indirect TIRS: 2 sets of TTCs (inner + outer steering)
5969 * and 1 set of direct TIRS
5970 */
5971 return 2 * MLX5E_NUM_INDIR_TIRS
5972 + mlx5e_profile_max_num_channels(mdev, &mlx5e_nic_profile);
5973 }
5974
mlx5e_set_rx_mode_work(struct work_struct * work)5975 void mlx5e_set_rx_mode_work(struct work_struct *work)
5976 {
5977 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
5978 set_rx_mode_work);
5979
5980 return mlx5e_fs_set_rx_mode_work(priv->fs, priv->netdev);
5981 }
5982
5983 /* mlx5e generic netdev management API (move to en_common.c) */
mlx5e_priv_init(struct mlx5e_priv * priv,const struct mlx5e_profile * profile,struct net_device * netdev,struct mlx5_core_dev * mdev)5984 int mlx5e_priv_init(struct mlx5e_priv *priv,
5985 const struct mlx5e_profile *profile,
5986 struct net_device *netdev,
5987 struct mlx5_core_dev *mdev)
5988 {
5989 int nch, num_txqs, node;
5990 int err;
5991
5992 num_txqs = netdev->num_tx_queues;
5993 nch = mlx5e_calc_max_nch(mdev, netdev, profile);
5994 node = dev_to_node(mlx5_core_dma_dev(mdev));
5995
5996 /* priv init */
5997 priv->mdev = mdev;
5998 priv->netdev = netdev;
5999 priv->max_nch = nch;
6000 priv->max_opened_tc = 1;
6001
6002 if (!alloc_cpumask_var(&priv->scratchpad.cpumask, GFP_KERNEL))
6003 return -ENOMEM;
6004
6005 mutex_init(&priv->state_lock);
6006
6007 err = mlx5e_selq_init(&priv->selq, &priv->state_lock);
6008 if (err)
6009 goto err_free_cpumask;
6010
6011 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
6012 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
6013 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
6014 INIT_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
6015
6016 priv->wq = create_singlethread_workqueue("mlx5e");
6017 if (!priv->wq)
6018 goto err_free_selq;
6019
6020 priv->txq2sq = kcalloc_node(num_txqs, sizeof(*priv->txq2sq), GFP_KERNEL, node);
6021 if (!priv->txq2sq)
6022 goto err_destroy_workqueue;
6023
6024 priv->txq2sq_stats = kcalloc_node(num_txqs, sizeof(*priv->txq2sq_stats), GFP_KERNEL, node);
6025 if (!priv->txq2sq_stats)
6026 goto err_free_txq2sq;
6027
6028 priv->tx_rates = kcalloc_node(num_txqs, sizeof(*priv->tx_rates), GFP_KERNEL, node);
6029 if (!priv->tx_rates)
6030 goto err_free_txq2sq_stats;
6031
6032 priv->channel_stats =
6033 kcalloc_node(nch, sizeof(*priv->channel_stats), GFP_KERNEL, node);
6034 if (!priv->channel_stats)
6035 goto err_free_tx_rates;
6036
6037 return 0;
6038
6039 err_free_tx_rates:
6040 kfree(priv->tx_rates);
6041 err_free_txq2sq_stats:
6042 kfree(priv->txq2sq_stats);
6043 err_free_txq2sq:
6044 kfree(priv->txq2sq);
6045 err_destroy_workqueue:
6046 destroy_workqueue(priv->wq);
6047 err_free_selq:
6048 mlx5e_selq_cleanup(&priv->selq);
6049 err_free_cpumask:
6050 free_cpumask_var(priv->scratchpad.cpumask);
6051 return -ENOMEM;
6052 }
6053
mlx5e_priv_cleanup(struct mlx5e_priv * priv)6054 void mlx5e_priv_cleanup(struct mlx5e_priv *priv)
6055 {
6056 int i;
6057
6058 /* bail if change profile failed and also rollback failed */
6059 if (!priv->mdev)
6060 return;
6061
6062 for (i = 0; i < priv->stats_nch; i++)
6063 kvfree(priv->channel_stats[i]);
6064 kfree(priv->channel_stats);
6065 kfree(priv->tx_rates);
6066 kfree(priv->txq2sq_stats);
6067 kfree(priv->txq2sq);
6068 destroy_workqueue(priv->wq);
6069 mlx5e_selq_cleanup(&priv->selq);
6070 free_cpumask_var(priv->scratchpad.cpumask);
6071
6072 for (i = 0; i < priv->htb_max_qos_sqs; i++)
6073 kfree(priv->htb_qos_sq_stats[i]);
6074 kvfree(priv->htb_qos_sq_stats);
6075
6076 if (priv->mqprio_rl) {
6077 mlx5e_mqprio_rl_cleanup(priv->mqprio_rl);
6078 mlx5e_mqprio_rl_free(priv->mqprio_rl);
6079 }
6080
6081 memset(priv, 0, sizeof(*priv));
6082 }
6083
mlx5e_get_max_num_txqs(struct mlx5_core_dev * mdev,const struct mlx5e_profile * profile)6084 static unsigned int mlx5e_get_max_num_txqs(struct mlx5_core_dev *mdev,
6085 const struct mlx5e_profile *profile)
6086 {
6087 unsigned int nch, ptp_txqs, qos_txqs;
6088
6089 nch = mlx5e_profile_max_num_channels(mdev, profile);
6090
6091 ptp_txqs = MLX5_CAP_GEN(mdev, ts_cqe_to_dest_cqn) &&
6092 mlx5e_profile_feature_cap(profile, PTP_TX) ?
6093 profile->max_tc : 0;
6094
6095 qos_txqs = mlx5_qos_is_supported(mdev) &&
6096 mlx5e_profile_feature_cap(profile, QOS_HTB) ?
6097 mlx5e_qos_max_leaf_nodes(mdev) : 0;
6098
6099 return nch * profile->max_tc + ptp_txqs + qos_txqs;
6100 }
6101
mlx5e_get_max_num_rxqs(struct mlx5_core_dev * mdev,const struct mlx5e_profile * profile)6102 static unsigned int mlx5e_get_max_num_rxqs(struct mlx5_core_dev *mdev,
6103 const struct mlx5e_profile *profile)
6104 {
6105 return mlx5e_profile_max_num_channels(mdev, profile);
6106 }
6107
6108 struct net_device *
mlx5e_create_netdev(struct mlx5_core_dev * mdev,const struct mlx5e_profile * profile)6109 mlx5e_create_netdev(struct mlx5_core_dev *mdev, const struct mlx5e_profile *profile)
6110 {
6111 struct net_device *netdev;
6112 unsigned int txqs, rxqs;
6113 int err;
6114
6115 txqs = mlx5e_get_max_num_txqs(mdev, profile);
6116 rxqs = mlx5e_get_max_num_rxqs(mdev, profile);
6117
6118 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv), txqs, rxqs);
6119 if (!netdev) {
6120 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
6121 return NULL;
6122 }
6123
6124 err = mlx5e_priv_init(netdev_priv(netdev), profile, netdev, mdev);
6125 if (err) {
6126 mlx5_core_err(mdev, "mlx5e_priv_init failed, err=%d\n", err);
6127 goto err_free_netdev;
6128 }
6129
6130 netif_carrier_off(netdev);
6131 netif_tx_disable(netdev);
6132 dev_net_set(netdev, mlx5_core_net(mdev));
6133
6134 return netdev;
6135
6136 err_free_netdev:
6137 free_netdev(netdev);
6138
6139 return NULL;
6140 }
6141
mlx5e_update_features(struct net_device * netdev)6142 static void mlx5e_update_features(struct net_device *netdev)
6143 {
6144 if (netdev->reg_state != NETREG_REGISTERED)
6145 return; /* features will be updated on netdev registration */
6146
6147 rtnl_lock();
6148 netdev_update_features(netdev);
6149 rtnl_unlock();
6150 }
6151
mlx5e_reset_channels(struct net_device * netdev)6152 static void mlx5e_reset_channels(struct net_device *netdev)
6153 {
6154 netdev_reset_tc(netdev);
6155 }
6156
mlx5e_attach_netdev(struct mlx5e_priv * priv)6157 int mlx5e_attach_netdev(struct mlx5e_priv *priv)
6158 {
6159 const bool take_rtnl = priv->netdev->reg_state == NETREG_REGISTERED;
6160 const struct mlx5e_profile *profile = priv->profile;
6161 int max_nch;
6162 int err;
6163
6164 clear_bit(MLX5E_STATE_DESTROYING, &priv->state);
6165 if (priv->fs)
6166 mlx5e_fs_set_state_destroy(priv->fs,
6167 !test_bit(MLX5E_STATE_DESTROYING, &priv->state));
6168
6169 /* Validate the max_wqe_size_sq capability. */
6170 if (WARN_ON_ONCE(mlx5e_get_max_sq_wqebbs(priv->mdev) < MLX5E_MAX_TX_WQEBBS)) {
6171 mlx5_core_warn(priv->mdev, "MLX5E: Max SQ WQEBBs firmware capability: %u, needed %u\n",
6172 mlx5e_get_max_sq_wqebbs(priv->mdev), (unsigned int)MLX5E_MAX_TX_WQEBBS);
6173 return -EIO;
6174 }
6175
6176 /* max number of channels may have changed */
6177 max_nch = mlx5e_calc_max_nch(priv->mdev, priv->netdev, profile);
6178 if (priv->channels.params.num_channels > max_nch) {
6179 mlx5_core_warn(priv->mdev, "MLX5E: Reducing number of channels to %d\n", max_nch);
6180 /* Reducing the number of channels - RXFH has to be reset, and
6181 * mlx5e_num_channels_changed below will build the RQT.
6182 */
6183 priv->netdev->priv_flags &= ~IFF_RXFH_CONFIGURED;
6184 priv->channels.params.num_channels = max_nch;
6185 if (priv->channels.params.mqprio.mode == TC_MQPRIO_MODE_CHANNEL) {
6186 mlx5_core_warn(priv->mdev, "MLX5E: Disabling MQPRIO channel mode\n");
6187 mlx5e_params_mqprio_reset(&priv->channels.params);
6188 }
6189 }
6190 if (max_nch != priv->max_nch) {
6191 mlx5_core_warn(priv->mdev,
6192 "MLX5E: Updating max number of channels from %u to %u\n",
6193 priv->max_nch, max_nch);
6194 priv->max_nch = max_nch;
6195 }
6196
6197 /* 1. Set the real number of queues in the kernel the first time.
6198 * 2. Set our default XPS cpumask.
6199 * 3. Build the RQT.
6200 *
6201 * rtnl_lock is required by netif_set_real_num_*_queues in case the
6202 * netdev has been registered by this point (if this function was called
6203 * in the reload or resume flow).
6204 */
6205 if (take_rtnl)
6206 rtnl_lock();
6207 err = mlx5e_num_channels_changed(priv);
6208 if (take_rtnl)
6209 rtnl_unlock();
6210 if (err)
6211 goto out;
6212
6213 err = profile->init_tx(priv);
6214 if (err)
6215 goto out;
6216
6217 err = profile->init_rx(priv);
6218 if (err)
6219 goto err_cleanup_tx;
6220
6221 if (profile->enable)
6222 profile->enable(priv);
6223
6224 mlx5e_update_features(priv->netdev);
6225
6226 return 0;
6227
6228 err_cleanup_tx:
6229 profile->cleanup_tx(priv);
6230
6231 out:
6232 mlx5e_reset_channels(priv->netdev);
6233 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
6234 if (priv->fs)
6235 mlx5e_fs_set_state_destroy(priv->fs,
6236 !test_bit(MLX5E_STATE_DESTROYING, &priv->state));
6237 cancel_work_sync(&priv->update_stats_work);
6238 return err;
6239 }
6240
mlx5e_detach_netdev(struct mlx5e_priv * priv)6241 void mlx5e_detach_netdev(struct mlx5e_priv *priv)
6242 {
6243 const struct mlx5e_profile *profile = priv->profile;
6244
6245 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
6246 if (priv->fs)
6247 mlx5e_fs_set_state_destroy(priv->fs,
6248 !test_bit(MLX5E_STATE_DESTROYING, &priv->state));
6249
6250 if (profile->disable)
6251 profile->disable(priv);
6252 flush_workqueue(priv->wq);
6253
6254 profile->cleanup_rx(priv);
6255 profile->cleanup_tx(priv);
6256 mlx5e_reset_channels(priv->netdev);
6257 cancel_work_sync(&priv->update_stats_work);
6258 }
6259
6260 static int
mlx5e_netdev_init_profile(struct net_device * netdev,struct mlx5_core_dev * mdev,const struct mlx5e_profile * new_profile,void * new_ppriv)6261 mlx5e_netdev_init_profile(struct net_device *netdev, struct mlx5_core_dev *mdev,
6262 const struct mlx5e_profile *new_profile, void *new_ppriv)
6263 {
6264 struct mlx5e_priv *priv = netdev_priv(netdev);
6265 int err;
6266
6267 err = mlx5e_priv_init(priv, new_profile, netdev, mdev);
6268 if (err) {
6269 mlx5_core_err(mdev, "mlx5e_priv_init failed, err=%d\n", err);
6270 return err;
6271 }
6272 netif_carrier_off(netdev);
6273 priv->profile = new_profile;
6274 priv->ppriv = new_ppriv;
6275 err = new_profile->init(priv->mdev, priv->netdev);
6276 if (err)
6277 goto priv_cleanup;
6278
6279 return 0;
6280
6281 priv_cleanup:
6282 mlx5e_priv_cleanup(priv);
6283 return err;
6284 }
6285
6286 static int
mlx5e_netdev_attach_profile(struct net_device * netdev,struct mlx5_core_dev * mdev,const struct mlx5e_profile * new_profile,void * new_ppriv)6287 mlx5e_netdev_attach_profile(struct net_device *netdev, struct mlx5_core_dev *mdev,
6288 const struct mlx5e_profile *new_profile, void *new_ppriv)
6289 {
6290 struct mlx5e_priv *priv = netdev_priv(netdev);
6291 int err;
6292
6293 err = mlx5e_netdev_init_profile(netdev, mdev, new_profile, new_ppriv);
6294 if (err)
6295 return err;
6296
6297 err = mlx5e_attach_netdev(priv);
6298 if (err)
6299 goto profile_cleanup;
6300 return err;
6301
6302 profile_cleanup:
6303 new_profile->cleanup(priv);
6304 mlx5e_priv_cleanup(priv);
6305 return err;
6306 }
6307
mlx5e_netdev_change_profile(struct mlx5e_priv * priv,const struct mlx5e_profile * new_profile,void * new_ppriv)6308 int mlx5e_netdev_change_profile(struct mlx5e_priv *priv,
6309 const struct mlx5e_profile *new_profile, void *new_ppriv)
6310 {
6311 const struct mlx5e_profile *orig_profile = priv->profile;
6312 struct net_device *netdev = priv->netdev;
6313 struct mlx5_core_dev *mdev = priv->mdev;
6314 void *orig_ppriv = priv->ppriv;
6315 int err, rollback_err;
6316
6317 /* cleanup old profile */
6318 mlx5e_detach_netdev(priv);
6319 priv->profile->cleanup(priv);
6320 mlx5e_priv_cleanup(priv);
6321
6322 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6323 mlx5e_netdev_init_profile(netdev, mdev, new_profile, new_ppriv);
6324 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
6325 return -EIO;
6326 }
6327
6328 err = mlx5e_netdev_attach_profile(netdev, mdev, new_profile, new_ppriv);
6329 if (err) { /* roll back to original profile */
6330 netdev_warn(netdev, "%s: new profile init failed, %d\n", __func__, err);
6331 goto rollback;
6332 }
6333
6334 return 0;
6335
6336 rollback:
6337 rollback_err = mlx5e_netdev_attach_profile(netdev, mdev, orig_profile, orig_ppriv);
6338 if (rollback_err)
6339 netdev_err(netdev, "%s: failed to rollback to orig profile, %d\n",
6340 __func__, rollback_err);
6341 return err;
6342 }
6343
mlx5e_netdev_attach_nic_profile(struct mlx5e_priv * priv)6344 void mlx5e_netdev_attach_nic_profile(struct mlx5e_priv *priv)
6345 {
6346 mlx5e_netdev_change_profile(priv, &mlx5e_nic_profile, NULL);
6347 }
6348
mlx5e_destroy_netdev(struct mlx5e_priv * priv)6349 void mlx5e_destroy_netdev(struct mlx5e_priv *priv)
6350 {
6351 struct net_device *netdev = priv->netdev;
6352
6353 mlx5e_priv_cleanup(priv);
6354 free_netdev(netdev);
6355 }
6356
_mlx5e_resume(struct auxiliary_device * adev)6357 static int _mlx5e_resume(struct auxiliary_device *adev)
6358 {
6359 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
6360 struct mlx5e_dev *mlx5e_dev = auxiliary_get_drvdata(adev);
6361 struct mlx5e_priv *priv = mlx5e_dev->priv;
6362 struct net_device *netdev = priv->netdev;
6363 struct mlx5_core_dev *mdev = edev->mdev;
6364 struct mlx5_core_dev *pos, *to;
6365 int err, i;
6366
6367 if (netif_device_present(netdev))
6368 return 0;
6369
6370 mlx5_sd_for_each_dev(i, mdev, pos) {
6371 err = mlx5e_create_mdev_resources(pos, true);
6372 if (err)
6373 goto err_destroy_mdev_res;
6374 }
6375
6376 err = mlx5e_attach_netdev(priv);
6377 if (err)
6378 goto err_destroy_mdev_res;
6379
6380 return 0;
6381
6382 err_destroy_mdev_res:
6383 to = pos;
6384 mlx5_sd_for_each_dev_to(i, mdev, to, pos)
6385 mlx5e_destroy_mdev_resources(pos);
6386 return err;
6387 }
6388
mlx5e_resume(struct auxiliary_device * adev)6389 static int mlx5e_resume(struct auxiliary_device *adev)
6390 {
6391 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
6392 struct mlx5_core_dev *mdev = edev->mdev;
6393 struct auxiliary_device *actual_adev;
6394 int err;
6395
6396 err = mlx5_sd_init(mdev);
6397 if (err)
6398 return err;
6399
6400 actual_adev = mlx5_sd_get_adev(mdev, adev, edev->idx);
6401 if (actual_adev)
6402 return _mlx5e_resume(actual_adev);
6403 return 0;
6404 }
6405
_mlx5e_suspend(struct auxiliary_device * adev,bool pre_netdev_reg)6406 static int _mlx5e_suspend(struct auxiliary_device *adev, bool pre_netdev_reg)
6407 {
6408 struct mlx5e_dev *mlx5e_dev = auxiliary_get_drvdata(adev);
6409 struct mlx5e_priv *priv = mlx5e_dev->priv;
6410 struct net_device *netdev = priv->netdev;
6411 struct mlx5_core_dev *mdev = priv->mdev;
6412 struct mlx5_core_dev *pos;
6413 int i;
6414
6415 if (!pre_netdev_reg && !netif_device_present(netdev)) {
6416 if (test_bit(MLX5E_STATE_DESTROYING, &priv->state))
6417 mlx5_sd_for_each_dev(i, mdev, pos)
6418 mlx5e_destroy_mdev_resources(pos);
6419 return -ENODEV;
6420 }
6421
6422 mlx5e_detach_netdev(priv);
6423 mlx5_sd_for_each_dev(i, mdev, pos)
6424 mlx5e_destroy_mdev_resources(pos);
6425
6426 return 0;
6427 }
6428
mlx5e_suspend(struct auxiliary_device * adev,pm_message_t state)6429 static int mlx5e_suspend(struct auxiliary_device *adev, pm_message_t state)
6430 {
6431 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
6432 struct mlx5_core_dev *mdev = edev->mdev;
6433 struct auxiliary_device *actual_adev;
6434 int err = 0;
6435
6436 actual_adev = mlx5_sd_get_adev(mdev, adev, edev->idx);
6437 if (actual_adev)
6438 err = _mlx5e_suspend(actual_adev, false);
6439
6440 mlx5_sd_cleanup(mdev);
6441 return err;
6442 }
6443
_mlx5e_probe(struct auxiliary_device * adev)6444 static int _mlx5e_probe(struct auxiliary_device *adev)
6445 {
6446 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
6447 const struct mlx5e_profile *profile = &mlx5e_nic_profile;
6448 struct mlx5_core_dev *mdev = edev->mdev;
6449 struct mlx5e_dev *mlx5e_dev;
6450 struct net_device *netdev;
6451 struct mlx5e_priv *priv;
6452 int err;
6453
6454 mlx5e_dev = mlx5e_create_devlink(&adev->dev, mdev);
6455 if (IS_ERR(mlx5e_dev))
6456 return PTR_ERR(mlx5e_dev);
6457 auxiliary_set_drvdata(adev, mlx5e_dev);
6458
6459 err = mlx5e_devlink_port_register(mlx5e_dev, mdev);
6460 if (err) {
6461 mlx5_core_err(mdev, "mlx5e_devlink_port_register failed, %d\n", err);
6462 goto err_devlink_unregister;
6463 }
6464
6465 netdev = mlx5e_create_netdev(mdev, profile);
6466 if (!netdev) {
6467 mlx5_core_err(mdev, "mlx5e_create_netdev failed\n");
6468 err = -ENOMEM;
6469 goto err_devlink_port_unregister;
6470 }
6471 SET_NETDEV_DEVLINK_PORT(netdev, &mlx5e_dev->dl_port);
6472
6473 mlx5e_build_nic_netdev(netdev);
6474
6475 priv = netdev_priv(netdev);
6476 mlx5e_dev->priv = priv;
6477
6478 priv->profile = profile;
6479 priv->ppriv = NULL;
6480
6481 err = profile->init(mdev, netdev);
6482 if (err) {
6483 mlx5_core_err(mdev, "mlx5e_nic_profile init failed, %d\n", err);
6484 goto err_destroy_netdev;
6485 }
6486
6487 err = _mlx5e_resume(adev);
6488 if (err) {
6489 mlx5_core_err(mdev, "_mlx5e_resume failed, %d\n", err);
6490 goto err_profile_cleanup;
6491 }
6492
6493 err = register_netdev(netdev);
6494 if (err) {
6495 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
6496 goto err_resume;
6497 }
6498
6499 mlx5e_dcbnl_init_app(priv);
6500 mlx5_core_uplink_netdev_set(mdev, netdev);
6501 mlx5e_params_print_info(mdev, &priv->channels.params);
6502 return 0;
6503
6504 err_resume:
6505 _mlx5e_suspend(adev, true);
6506 err_profile_cleanup:
6507 profile->cleanup(priv);
6508 err_destroy_netdev:
6509 mlx5e_destroy_netdev(priv);
6510 err_devlink_port_unregister:
6511 mlx5e_devlink_port_unregister(mlx5e_dev);
6512 err_devlink_unregister:
6513 mlx5e_destroy_devlink(mlx5e_dev);
6514 return err;
6515 }
6516
mlx5e_probe(struct auxiliary_device * adev,const struct auxiliary_device_id * id)6517 static int mlx5e_probe(struct auxiliary_device *adev,
6518 const struct auxiliary_device_id *id)
6519 {
6520 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
6521 struct mlx5_core_dev *mdev = edev->mdev;
6522 struct auxiliary_device *actual_adev;
6523 int err;
6524
6525 err = mlx5_sd_init(mdev);
6526 if (err)
6527 return err;
6528
6529 actual_adev = mlx5_sd_get_adev(mdev, adev, edev->idx);
6530 if (actual_adev)
6531 return _mlx5e_probe(actual_adev);
6532 return 0;
6533 }
6534
_mlx5e_remove(struct auxiliary_device * adev)6535 static void _mlx5e_remove(struct auxiliary_device *adev)
6536 {
6537 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
6538 struct mlx5e_dev *mlx5e_dev = auxiliary_get_drvdata(adev);
6539 struct mlx5e_priv *priv = mlx5e_dev->priv;
6540 struct mlx5_core_dev *mdev = edev->mdev;
6541
6542 mlx5_core_uplink_netdev_set(mdev, NULL);
6543 mlx5e_dcbnl_delete_app(priv);
6544 /* When unload driver, the netdev is in registered state
6545 * if it's from legacy mode. If from switchdev mode, it
6546 * is already unregistered before changing to NIC profile.
6547 */
6548 if (priv->netdev->reg_state == NETREG_REGISTERED) {
6549 unregister_netdev(priv->netdev);
6550 _mlx5e_suspend(adev, false);
6551 } else {
6552 struct mlx5_core_dev *pos;
6553 int i;
6554
6555 if (test_bit(MLX5E_STATE_DESTROYING, &priv->state))
6556 mlx5_sd_for_each_dev(i, mdev, pos)
6557 mlx5e_destroy_mdev_resources(pos);
6558 else
6559 _mlx5e_suspend(adev, true);
6560 }
6561 /* Avoid cleanup if profile rollback failed. */
6562 if (priv->profile)
6563 priv->profile->cleanup(priv);
6564 mlx5e_destroy_netdev(priv);
6565 mlx5e_devlink_port_unregister(mlx5e_dev);
6566 mlx5e_destroy_devlink(mlx5e_dev);
6567 }
6568
mlx5e_remove(struct auxiliary_device * adev)6569 static void mlx5e_remove(struct auxiliary_device *adev)
6570 {
6571 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
6572 struct mlx5_core_dev *mdev = edev->mdev;
6573 struct auxiliary_device *actual_adev;
6574
6575 actual_adev = mlx5_sd_get_adev(mdev, adev, edev->idx);
6576 if (actual_adev)
6577 _mlx5e_remove(actual_adev);
6578
6579 mlx5_sd_cleanup(mdev);
6580 }
6581
6582 static const struct auxiliary_device_id mlx5e_id_table[] = {
6583 { .name = MLX5_ADEV_NAME ".eth", },
6584 {},
6585 };
6586
6587 MODULE_DEVICE_TABLE(auxiliary, mlx5e_id_table);
6588
6589 static struct auxiliary_driver mlx5e_driver = {
6590 .name = "eth",
6591 .probe = mlx5e_probe,
6592 .remove = mlx5e_remove,
6593 .suspend = mlx5e_suspend,
6594 .resume = mlx5e_resume,
6595 .id_table = mlx5e_id_table,
6596 };
6597
mlx5e_init(void)6598 int mlx5e_init(void)
6599 {
6600 int ret;
6601
6602 mlx5e_build_ptys2ethtool_map();
6603 ret = auxiliary_driver_register(&mlx5e_driver);
6604 if (ret)
6605 return ret;
6606
6607 ret = mlx5e_rep_init();
6608 if (ret)
6609 auxiliary_driver_unregister(&mlx5e_driver);
6610 return ret;
6611 }
6612
mlx5e_cleanup(void)6613 void mlx5e_cleanup(void)
6614 {
6615 mlx5e_rep_cleanup();
6616 auxiliary_driver_unregister(&mlx5e_driver);
6617 }
6618