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