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