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