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