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