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