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