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