xref: /linux/drivers/net/ethernet/mellanox/mlx5/core/en_main.c (revision c2c2ccfd4ba72718266a56f3ecc34c989cb5b7a0)
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->arm_db    = 0;
2223 	mcq->vector     = param->eq_ix;
2224 	mcq->comp       = mlx5e_completion_event;
2225 	mcq->event      = mlx5e_cq_error_event;
2226 
2227 	for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
2228 		struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
2229 
2230 		cqe->op_own = 0xf1;
2231 		cqe->validity_iteration_count = 0xff;
2232 	}
2233 
2234 	cq->mdev = mdev;
2235 	cq->netdev = netdev;
2236 	cq->workqueue = workqueue;
2237 	cq->uar = uar;
2238 
2239 	return 0;
2240 }
2241 
mlx5e_alloc_cq(struct mlx5_core_dev * mdev,struct mlx5e_cq_param * param,struct mlx5e_create_cq_param * ccp,struct mlx5e_cq * cq)2242 static int mlx5e_alloc_cq(struct mlx5_core_dev *mdev,
2243 			  struct mlx5e_cq_param *param,
2244 			  struct mlx5e_create_cq_param *ccp,
2245 			  struct mlx5e_cq *cq)
2246 {
2247 	int err;
2248 
2249 	param->wq.buf_numa_node = ccp->node;
2250 	param->wq.db_numa_node  = ccp->node;
2251 	param->eq_ix            = ccp->ix;
2252 
2253 	err = mlx5e_alloc_cq_common(mdev, ccp->netdev, ccp->wq,
2254 				    ccp->uar, param, cq);
2255 
2256 	cq->napi     = ccp->napi;
2257 	cq->ch_stats = ccp->ch_stats;
2258 
2259 	return err;
2260 }
2261 
mlx5e_free_cq(struct mlx5e_cq * cq)2262 static void mlx5e_free_cq(struct mlx5e_cq *cq)
2263 {
2264 	mlx5_wq_destroy(&cq->wq_ctrl);
2265 }
2266 
mlx5e_create_cq(struct mlx5e_cq * cq,struct mlx5e_cq_param * param)2267 static int mlx5e_create_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
2268 {
2269 	u32 out[MLX5_ST_SZ_DW(create_cq_out)];
2270 	struct mlx5_core_dev *mdev = cq->mdev;
2271 	struct mlx5_core_cq *mcq = &cq->mcq;
2272 
2273 	void *in;
2274 	void *cqc;
2275 	int inlen;
2276 	int eqn;
2277 	int err;
2278 
2279 	err = mlx5_comp_eqn_get(mdev, param->eq_ix, &eqn);
2280 	if (err)
2281 		return err;
2282 
2283 	inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
2284 		sizeof(u64) * cq->wq_ctrl.buf.npages;
2285 	in = kvzalloc(inlen, GFP_KERNEL);
2286 	if (!in)
2287 		return -ENOMEM;
2288 
2289 	cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
2290 
2291 	memcpy(cqc, param->cqc, sizeof(param->cqc));
2292 
2293 	mlx5_fill_page_frag_array(&cq->wq_ctrl.buf,
2294 				  (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
2295 
2296 	MLX5_SET(cqc, cqc, cq_period_mode, mlx5e_cq_period_mode(param->cq_period_mode));
2297 
2298 	MLX5_SET(cqc,   cqc, c_eqn_or_apu_element, eqn);
2299 	MLX5_SET(cqc,   cqc, uar_page,      cq->uar->index);
2300 	MLX5_SET(cqc,   cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
2301 					    MLX5_ADAPTER_PAGE_SHIFT);
2302 	MLX5_SET64(cqc, cqc, dbr_addr,      cq->wq_ctrl.db.dma);
2303 
2304 	err = mlx5_core_create_cq(mdev, mcq, in, inlen, out, sizeof(out));
2305 
2306 	kvfree(in);
2307 
2308 	if (err)
2309 		return err;
2310 
2311 	mlx5e_cq_arm(cq);
2312 
2313 	return 0;
2314 }
2315 
mlx5e_destroy_cq(struct mlx5e_cq * cq)2316 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
2317 {
2318 	mlx5_core_destroy_cq(cq->mdev, &cq->mcq);
2319 }
2320 
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)2321 int mlx5e_open_cq(struct mlx5_core_dev *mdev, struct dim_cq_moder moder,
2322 		  struct mlx5e_cq_param *param, struct mlx5e_create_cq_param *ccp,
2323 		  struct mlx5e_cq *cq)
2324 {
2325 	int err;
2326 
2327 	err = mlx5e_alloc_cq(mdev, param, ccp, cq);
2328 	if (err)
2329 		return err;
2330 
2331 	err = mlx5e_create_cq(cq, param);
2332 	if (err)
2333 		goto err_free_cq;
2334 
2335 	if (MLX5_CAP_GEN(mdev, cq_moderation) &&
2336 	    MLX5_CAP_GEN(mdev, cq_period_mode_modify))
2337 		mlx5e_modify_cq_moderation(mdev, &cq->mcq, moder.usec, moder.pkts,
2338 					   mlx5e_cq_period_mode(moder.cq_period_mode));
2339 	return 0;
2340 
2341 err_free_cq:
2342 	mlx5e_free_cq(cq);
2343 
2344 	return err;
2345 }
2346 
mlx5e_close_cq(struct mlx5e_cq * cq)2347 void mlx5e_close_cq(struct mlx5e_cq *cq)
2348 {
2349 	mlx5e_destroy_cq(cq);
2350 	mlx5e_free_cq(cq);
2351 }
2352 
mlx5e_modify_cq_period_mode(struct mlx5_core_dev * dev,struct mlx5_core_cq * cq,u8 cq_period_mode)2353 int mlx5e_modify_cq_period_mode(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
2354 				u8 cq_period_mode)
2355 {
2356 	u32 in[MLX5_ST_SZ_DW(modify_cq_in)] = {};
2357 	void *cqc;
2358 
2359 	MLX5_SET(modify_cq_in, in, cqn, cq->cqn);
2360 	cqc = MLX5_ADDR_OF(modify_cq_in, in, cq_context);
2361 	MLX5_SET(cqc, cqc, cq_period_mode, mlx5e_cq_period_mode(cq_period_mode));
2362 	MLX5_SET(modify_cq_in, in,
2363 		 modify_field_select_resize_field_select.modify_field_select.modify_field_select,
2364 		 MLX5_CQ_MODIFY_PERIOD_MODE);
2365 
2366 	return mlx5_core_modify_cq(dev, cq, in, sizeof(in));
2367 }
2368 
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)2369 int mlx5e_modify_cq_moderation(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
2370 			       u16 cq_period, u16 cq_max_count, u8 cq_period_mode)
2371 {
2372 	u32 in[MLX5_ST_SZ_DW(modify_cq_in)] = {};
2373 	void *cqc;
2374 
2375 	MLX5_SET(modify_cq_in, in, cqn, cq->cqn);
2376 	cqc = MLX5_ADDR_OF(modify_cq_in, in, cq_context);
2377 	MLX5_SET(cqc, cqc, cq_period, cq_period);
2378 	MLX5_SET(cqc, cqc, cq_max_count, cq_max_count);
2379 	MLX5_SET(cqc, cqc, cq_period_mode, cq_period_mode);
2380 	MLX5_SET(modify_cq_in, in,
2381 		 modify_field_select_resize_field_select.modify_field_select.modify_field_select,
2382 		 MLX5_CQ_MODIFY_PERIOD | MLX5_CQ_MODIFY_COUNT | MLX5_CQ_MODIFY_PERIOD_MODE);
2383 
2384 	return mlx5_core_modify_cq(dev, cq, in, sizeof(in));
2385 }
2386 
mlx5e_open_tx_cqs(struct mlx5e_channel * c,struct mlx5e_params * params,struct mlx5e_create_cq_param * ccp,struct mlx5e_channel_param * cparam)2387 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
2388 			     struct mlx5e_params *params,
2389 			     struct mlx5e_create_cq_param *ccp,
2390 			     struct mlx5e_channel_param *cparam)
2391 {
2392 	int err;
2393 	int tc;
2394 
2395 	for (tc = 0; tc < c->num_tc; tc++) {
2396 		err = mlx5e_open_cq(c->mdev, params->tx_cq_moderation, &cparam->txq_sq.cqp,
2397 				    ccp, &c->sq[tc].cq);
2398 		if (err)
2399 			goto err_close_tx_cqs;
2400 	}
2401 
2402 	return 0;
2403 
2404 err_close_tx_cqs:
2405 	for (tc--; tc >= 0; tc--)
2406 		mlx5e_close_cq(&c->sq[tc].cq);
2407 
2408 	return err;
2409 }
2410 
mlx5e_close_tx_cqs(struct mlx5e_channel * c)2411 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
2412 {
2413 	int tc;
2414 
2415 	for (tc = 0; tc < c->num_tc; tc++)
2416 		mlx5e_close_cq(&c->sq[tc].cq);
2417 }
2418 
mlx5e_mqprio_txq_to_tc(struct netdev_tc_txq * tc_to_txq,unsigned int txq)2419 static int mlx5e_mqprio_txq_to_tc(struct netdev_tc_txq *tc_to_txq, unsigned int txq)
2420 {
2421 	int tc;
2422 
2423 	for (tc = 0; tc < TC_MAX_QUEUE; tc++)
2424 		if (txq - tc_to_txq[tc].offset < tc_to_txq[tc].count)
2425 			return tc;
2426 
2427 	WARN(1, "Unexpected TCs configuration. No match found for txq %u", txq);
2428 	return -ENOENT;
2429 }
2430 
mlx5e_txq_get_qos_node_hw_id(struct mlx5e_params * params,int txq_ix,u32 * hw_id)2431 static int mlx5e_txq_get_qos_node_hw_id(struct mlx5e_params *params, int txq_ix,
2432 					u32 *hw_id)
2433 {
2434 	int tc;
2435 
2436 	if (params->mqprio.mode != TC_MQPRIO_MODE_CHANNEL) {
2437 		*hw_id = 0;
2438 		return 0;
2439 	}
2440 
2441 	tc = mlx5e_mqprio_txq_to_tc(params->mqprio.tc_to_txq, txq_ix);
2442 	if (tc < 0)
2443 		return tc;
2444 
2445 	if (tc >= params->mqprio.num_tc) {
2446 		WARN(1, "Unexpected TCs configuration. tc %d is out of range of %u",
2447 		     tc, params->mqprio.num_tc);
2448 		return -EINVAL;
2449 	}
2450 
2451 	*hw_id = params->mqprio.channel.hw_id[tc];
2452 	return 0;
2453 }
2454 
mlx5e_open_sqs(struct mlx5e_channel * c,struct mlx5e_params * params,struct mlx5e_channel_param * cparam)2455 static int mlx5e_open_sqs(struct mlx5e_channel *c,
2456 			  struct mlx5e_params *params,
2457 			  struct mlx5e_channel_param *cparam)
2458 {
2459 	int err, tc;
2460 
2461 	for (tc = 0; tc < mlx5e_get_dcb_num_tc(params); tc++) {
2462 		int txq_ix = c->ix + tc * params->num_channels;
2463 		u32 qos_queue_group_id;
2464 		u32 tisn;
2465 
2466 		tisn = mlx5e_profile_get_tisn(c->mdev, c->priv, c->priv->profile,
2467 					      c->lag_port, tc);
2468 		err = mlx5e_txq_get_qos_node_hw_id(params, txq_ix, &qos_queue_group_id);
2469 		if (err)
2470 			goto err_close_sqs;
2471 
2472 		err = mlx5e_open_txqsq(c, tisn, txq_ix,
2473 				       params, &cparam->txq_sq, &c->sq[tc], tc,
2474 				       qos_queue_group_id,
2475 				       &c->priv->channel_stats[c->ix]->sq[tc]);
2476 		if (err)
2477 			goto err_close_sqs;
2478 	}
2479 
2480 	return 0;
2481 
2482 err_close_sqs:
2483 	for (tc--; tc >= 0; tc--)
2484 		mlx5e_close_txqsq(&c->sq[tc]);
2485 
2486 	return err;
2487 }
2488 
mlx5e_close_sqs(struct mlx5e_channel * c)2489 static void mlx5e_close_sqs(struct mlx5e_channel *c)
2490 {
2491 	int tc;
2492 
2493 	for (tc = 0; tc < c->num_tc; tc++)
2494 		mlx5e_close_txqsq(&c->sq[tc]);
2495 }
2496 
mlx5e_set_sq_maxrate(struct net_device * dev,struct mlx5e_txqsq * sq,u32 rate)2497 static int mlx5e_set_sq_maxrate(struct net_device *dev,
2498 				struct mlx5e_txqsq *sq, u32 rate)
2499 {
2500 	struct mlx5e_priv *priv = netdev_priv(dev);
2501 	struct mlx5_core_dev *mdev = priv->mdev;
2502 	struct mlx5e_modify_sq_param msp = {0};
2503 	struct mlx5_rate_limit rl = {0};
2504 	u16 rl_index = 0;
2505 	int err;
2506 
2507 	if (rate == sq->rate_limit)
2508 		/* nothing to do */
2509 		return 0;
2510 
2511 	if (sq->rate_limit) {
2512 		rl.rate = sq->rate_limit;
2513 		/* remove current rl index to free space to next ones */
2514 		mlx5_rl_remove_rate(mdev, &rl);
2515 	}
2516 
2517 	sq->rate_limit = 0;
2518 
2519 	if (rate) {
2520 		rl.rate = rate;
2521 		err = mlx5_rl_add_rate(mdev, &rl_index, &rl);
2522 		if (err) {
2523 			netdev_err(dev, "Failed configuring rate %u: %d\n",
2524 				   rate, err);
2525 			return err;
2526 		}
2527 	}
2528 
2529 	msp.curr_state = MLX5_SQC_STATE_RDY;
2530 	msp.next_state = MLX5_SQC_STATE_RDY;
2531 	msp.rl_index   = rl_index;
2532 	msp.rl_update  = true;
2533 	err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
2534 	if (err) {
2535 		netdev_err(dev, "Failed configuring rate %u: %d\n",
2536 			   rate, err);
2537 		/* remove the rate from the table */
2538 		if (rate)
2539 			mlx5_rl_remove_rate(mdev, &rl);
2540 		return err;
2541 	}
2542 
2543 	sq->rate_limit = rate;
2544 	return 0;
2545 }
2546 
mlx5e_set_tx_maxrate(struct net_device * dev,int index,u32 rate)2547 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
2548 {
2549 	struct mlx5e_priv *priv = netdev_priv(dev);
2550 	struct mlx5_core_dev *mdev = priv->mdev;
2551 	struct mlx5e_txqsq *sq = priv->txq2sq[index];
2552 	int err = 0;
2553 
2554 	if (!mlx5_rl_is_supported(mdev)) {
2555 		netdev_err(dev, "Rate limiting is not supported on this device\n");
2556 		return -EINVAL;
2557 	}
2558 
2559 	/* rate is given in Mb/sec, HW config is in Kb/sec */
2560 	rate = rate << 10;
2561 
2562 	/* Check whether rate in valid range, 0 is always valid */
2563 	if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
2564 		netdev_err(dev, "TX rate %u, is not in range\n", rate);
2565 		return -ERANGE;
2566 	}
2567 
2568 	mutex_lock(&priv->state_lock);
2569 	if (test_bit(MLX5E_STATE_OPENED, &priv->state))
2570 		err = mlx5e_set_sq_maxrate(dev, sq, rate);
2571 	if (!err)
2572 		priv->tx_rates[index] = rate;
2573 	mutex_unlock(&priv->state_lock);
2574 
2575 	return err;
2576 }
2577 
mlx5e_open_rxq_rq(struct mlx5e_channel * c,struct mlx5e_params * params,struct mlx5e_rq_param * rq_params)2578 static int mlx5e_open_rxq_rq(struct mlx5e_channel *c, struct mlx5e_params *params,
2579 			     struct mlx5e_rq_param *rq_params)
2580 {
2581 	u16 q_counter = c->priv->q_counter[c->sd_ix];
2582 	int err;
2583 
2584 	err = mlx5e_init_rxq_rq(c, params, rq_params->xdp_frag_size, &c->rq);
2585 	if (err)
2586 		return err;
2587 
2588 	return mlx5e_open_rq(params, rq_params, NULL, cpu_to_node(c->cpu), q_counter, &c->rq);
2589 }
2590 
mlx5e_open_queues(struct mlx5e_channel * c,struct mlx5e_params * params,struct mlx5e_channel_param * cparam)2591 static int mlx5e_open_queues(struct mlx5e_channel *c,
2592 			     struct mlx5e_params *params,
2593 			     struct mlx5e_channel_param *cparam)
2594 {
2595 	const struct net_device_ops *netdev_ops = c->netdev->netdev_ops;
2596 	struct dim_cq_moder icocq_moder = {0, 0};
2597 	struct mlx5e_create_cq_param ccp;
2598 	int err;
2599 
2600 	mlx5e_build_create_cq_param(&ccp, c);
2601 
2602 	err = mlx5e_open_cq(c->mdev, icocq_moder, &cparam->async_icosq.cqp, &ccp,
2603 			    &c->async_icosq.cq);
2604 	if (err)
2605 		return err;
2606 
2607 	err = mlx5e_open_cq(c->mdev, icocq_moder, &cparam->icosq.cqp, &ccp,
2608 			    &c->icosq.cq);
2609 	if (err)
2610 		goto err_close_async_icosq_cq;
2611 
2612 	err = mlx5e_open_tx_cqs(c, params, &ccp, cparam);
2613 	if (err)
2614 		goto err_close_icosq_cq;
2615 
2616 	if (netdev_ops->ndo_xdp_xmit) {
2617 		c->xdpsq = mlx5e_open_xdpredirect_sq(c, params, cparam, &ccp);
2618 		if (IS_ERR(c->xdpsq)) {
2619 			err = PTR_ERR(c->xdpsq);
2620 			goto err_close_tx_cqs;
2621 		}
2622 	}
2623 
2624 	err = mlx5e_open_cq(c->mdev, params->rx_cq_moderation, &cparam->rq.cqp, &ccp,
2625 			    &c->rq.cq);
2626 	if (err)
2627 		goto err_close_xdpredirect_sq;
2628 
2629 	err = c->xdp ? mlx5e_open_cq(c->mdev, params->tx_cq_moderation, &cparam->xdp_sq.cqp,
2630 				     &ccp, &c->rq_xdpsq.cq) : 0;
2631 	if (err)
2632 		goto err_close_rx_cq;
2633 
2634 	spin_lock_init(&c->async_icosq_lock);
2635 
2636 	err = mlx5e_open_icosq(c, params, &cparam->async_icosq, &c->async_icosq,
2637 			       mlx5e_async_icosq_err_cqe_work);
2638 	if (err)
2639 		goto err_close_rq_xdpsq_cq;
2640 
2641 	mutex_init(&c->icosq_recovery_lock);
2642 
2643 	err = mlx5e_open_icosq(c, params, &cparam->icosq, &c->icosq,
2644 			       mlx5e_icosq_err_cqe_work);
2645 	if (err)
2646 		goto err_close_async_icosq;
2647 
2648 	err = mlx5e_open_sqs(c, params, cparam);
2649 	if (err)
2650 		goto err_close_icosq;
2651 
2652 	err = mlx5e_open_rxq_rq(c, params, &cparam->rq);
2653 	if (err)
2654 		goto err_close_sqs;
2655 
2656 	if (c->xdp) {
2657 		err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, NULL,
2658 				       &c->rq_xdpsq, false);
2659 		if (err)
2660 			goto err_close_rq;
2661 	}
2662 
2663 	return 0;
2664 
2665 err_close_rq:
2666 	mlx5e_close_rq(&c->rq);
2667 
2668 err_close_sqs:
2669 	mlx5e_close_sqs(c);
2670 
2671 err_close_icosq:
2672 	mlx5e_close_icosq(&c->icosq);
2673 
2674 err_close_async_icosq:
2675 	mlx5e_close_icosq(&c->async_icosq);
2676 
2677 err_close_rq_xdpsq_cq:
2678 	if (c->xdp)
2679 		mlx5e_close_cq(&c->rq_xdpsq.cq);
2680 
2681 err_close_rx_cq:
2682 	mlx5e_close_cq(&c->rq.cq);
2683 
2684 err_close_xdpredirect_sq:
2685 	if (c->xdpsq)
2686 		mlx5e_close_xdpredirect_sq(c->xdpsq);
2687 
2688 err_close_tx_cqs:
2689 	mlx5e_close_tx_cqs(c);
2690 
2691 err_close_icosq_cq:
2692 	mlx5e_close_cq(&c->icosq.cq);
2693 
2694 err_close_async_icosq_cq:
2695 	mlx5e_close_cq(&c->async_icosq.cq);
2696 
2697 	return err;
2698 }
2699 
mlx5e_close_queues(struct mlx5e_channel * c)2700 static void mlx5e_close_queues(struct mlx5e_channel *c)
2701 {
2702 	if (c->xdp)
2703 		mlx5e_close_xdpsq(&c->rq_xdpsq);
2704 	/* The same ICOSQ is used for UMRs for both RQ and XSKRQ. */
2705 	cancel_work_sync(&c->icosq.recover_work);
2706 	mlx5e_close_rq(&c->rq);
2707 	mlx5e_close_sqs(c);
2708 	mlx5e_close_icosq(&c->icosq);
2709 	mutex_destroy(&c->icosq_recovery_lock);
2710 	mlx5e_close_icosq(&c->async_icosq);
2711 	if (c->xdp)
2712 		mlx5e_close_cq(&c->rq_xdpsq.cq);
2713 	mlx5e_close_cq(&c->rq.cq);
2714 	if (c->xdpsq)
2715 		mlx5e_close_xdpredirect_sq(c->xdpsq);
2716 	mlx5e_close_tx_cqs(c);
2717 	mlx5e_close_cq(&c->icosq.cq);
2718 	mlx5e_close_cq(&c->async_icosq.cq);
2719 }
2720 
mlx5e_enumerate_lag_port(struct mlx5_core_dev * mdev,int ix)2721 static u8 mlx5e_enumerate_lag_port(struct mlx5_core_dev *mdev, int ix)
2722 {
2723 	u16 port_aff_bias = mlx5_core_is_pf(mdev) ? 0 : MLX5_CAP_GEN(mdev, vhca_id);
2724 
2725 	return (ix + port_aff_bias) % mlx5e_get_num_lag_ports(mdev);
2726 }
2727 
mlx5e_channel_stats_alloc(struct mlx5e_priv * priv,int ix,int cpu)2728 static int mlx5e_channel_stats_alloc(struct mlx5e_priv *priv, int ix, int cpu)
2729 {
2730 	if (ix > priv->stats_nch)  {
2731 		netdev_warn(priv->netdev, "Unexpected channel stats index %d > %d\n", ix,
2732 			    priv->stats_nch);
2733 		return -EINVAL;
2734 	}
2735 
2736 	if (priv->channel_stats[ix])
2737 		return 0;
2738 
2739 	/* Asymmetric dynamic memory allocation.
2740 	 * Freed in mlx5e_priv_arrays_free, not on channel closure.
2741 	 */
2742 	netdev_dbg(priv->netdev, "Creating channel stats %d\n", ix);
2743 	priv->channel_stats[ix] = kvzalloc_node(sizeof(**priv->channel_stats),
2744 						GFP_KERNEL, cpu_to_node(cpu));
2745 	if (!priv->channel_stats[ix])
2746 		return -ENOMEM;
2747 	priv->stats_nch++;
2748 
2749 	return 0;
2750 }
2751 
mlx5e_trigger_napi_icosq(struct mlx5e_channel * c)2752 void mlx5e_trigger_napi_icosq(struct mlx5e_channel *c)
2753 {
2754 	spin_lock_bh(&c->async_icosq_lock);
2755 	mlx5e_trigger_irq(&c->async_icosq);
2756 	spin_unlock_bh(&c->async_icosq_lock);
2757 }
2758 
mlx5e_trigger_napi_sched(struct napi_struct * napi)2759 void mlx5e_trigger_napi_sched(struct napi_struct *napi)
2760 {
2761 	local_bh_disable();
2762 	napi_schedule(napi);
2763 	local_bh_enable();
2764 }
2765 
mlx5e_channel_pick_doorbell(struct mlx5e_channel * c)2766 static void mlx5e_channel_pick_doorbell(struct mlx5e_channel *c)
2767 {
2768 	struct mlx5e_hw_objs *hw_objs = &c->mdev->mlx5e_res.hw_objs;
2769 
2770 	/* No dedicated Ethernet doorbells, use the global one. */
2771 	if (hw_objs->num_bfregs == 0) {
2772 		c->bfreg = &c->mdev->priv.bfreg;
2773 		return;
2774 	}
2775 
2776 	/* Round-robin between doorbells. */
2777 	c->bfreg = hw_objs->bfregs + c->vec_ix % hw_objs->num_bfregs;
2778 }
2779 
mlx5e_open_channel(struct mlx5e_priv * priv,int ix,struct mlx5e_params * params,struct xsk_buff_pool * xsk_pool,struct mlx5e_channel ** cp)2780 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
2781 			      struct mlx5e_params *params,
2782 			      struct xsk_buff_pool *xsk_pool,
2783 			      struct mlx5e_channel **cp)
2784 {
2785 	struct net_device *netdev = priv->netdev;
2786 	struct mlx5e_channel_param *cparam;
2787 	struct mlx5_core_dev *mdev;
2788 	struct mlx5e_xsk_param xsk;
2789 	struct mlx5e_channel *c;
2790 	unsigned int irq;
2791 	int vec_ix;
2792 	int cpu;
2793 	int err;
2794 
2795 	mdev = mlx5_sd_ch_ix_get_dev(priv->mdev, ix);
2796 	vec_ix = mlx5_sd_ch_ix_get_vec_ix(mdev, ix);
2797 	cpu = mlx5_comp_vector_get_cpu(mdev, vec_ix);
2798 
2799 	err = mlx5_comp_irqn_get(mdev, vec_ix, &irq);
2800 	if (err)
2801 		return err;
2802 
2803 	err = mlx5e_channel_stats_alloc(priv, ix, cpu);
2804 	if (err)
2805 		return err;
2806 
2807 	c = kvzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
2808 	cparam = kvzalloc(sizeof(*cparam), GFP_KERNEL);
2809 	if (!c || !cparam) {
2810 		err = -ENOMEM;
2811 		goto err_free;
2812 	}
2813 
2814 	err = mlx5e_build_channel_param(mdev, params, cparam);
2815 	if (err)
2816 		goto err_free;
2817 
2818 	c->priv     = priv;
2819 	c->mdev     = mdev;
2820 	c->tstamp   = &priv->tstamp;
2821 	c->ix       = ix;
2822 	c->vec_ix   = vec_ix;
2823 	c->sd_ix    = mlx5_sd_ch_ix_get_dev_ix(mdev, ix);
2824 	c->cpu      = cpu;
2825 	c->pdev     = mlx5_core_dma_dev(mdev);
2826 	c->netdev   = priv->netdev;
2827 	c->mkey_be  = cpu_to_be32(mdev->mlx5e_res.hw_objs.mkey);
2828 	c->num_tc   = mlx5e_get_dcb_num_tc(params);
2829 	c->xdp      = !!params->xdp_prog;
2830 	c->stats    = &priv->channel_stats[ix]->ch;
2831 	c->aff_mask = irq_get_effective_affinity_mask(irq);
2832 	c->lag_port = mlx5e_enumerate_lag_port(mdev, ix);
2833 
2834 	mlx5e_channel_pick_doorbell(c);
2835 
2836 	netif_napi_add_config_locked(netdev, &c->napi, mlx5e_napi_poll, ix);
2837 	netif_napi_set_irq_locked(&c->napi, irq);
2838 
2839 	err = mlx5e_open_queues(c, params, cparam);
2840 	if (unlikely(err))
2841 		goto err_napi_del;
2842 
2843 	if (xsk_pool) {
2844 		mlx5e_build_xsk_param(xsk_pool, &xsk);
2845 		err = mlx5e_open_xsk(priv, params, &xsk, xsk_pool, c);
2846 		if (unlikely(err))
2847 			goto err_close_queues;
2848 	}
2849 
2850 	*cp = c;
2851 
2852 	kvfree(cparam);
2853 	return 0;
2854 
2855 err_close_queues:
2856 	mlx5e_close_queues(c);
2857 
2858 err_napi_del:
2859 	netif_napi_del_locked(&c->napi);
2860 
2861 err_free:
2862 	kvfree(cparam);
2863 	kvfree(c);
2864 
2865 	return err;
2866 }
2867 
mlx5e_activate_channel(struct mlx5e_channel * c)2868 static void mlx5e_activate_channel(struct mlx5e_channel *c)
2869 {
2870 	int tc;
2871 
2872 	napi_enable_locked(&c->napi);
2873 
2874 	for (tc = 0; tc < c->num_tc; tc++)
2875 		mlx5e_activate_txqsq(&c->sq[tc]);
2876 	mlx5e_activate_icosq(&c->icosq);
2877 	mlx5e_activate_icosq(&c->async_icosq);
2878 
2879 	if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
2880 		mlx5e_activate_xsk(c);
2881 	else
2882 		mlx5e_activate_rq(&c->rq);
2883 
2884 	netif_queue_set_napi(c->netdev, c->ix, NETDEV_QUEUE_TYPE_RX, &c->napi);
2885 }
2886 
mlx5e_deactivate_channel(struct mlx5e_channel * c)2887 static void mlx5e_deactivate_channel(struct mlx5e_channel *c)
2888 {
2889 	int tc;
2890 
2891 	netif_queue_set_napi(c->netdev, c->ix, NETDEV_QUEUE_TYPE_RX, NULL);
2892 
2893 	if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
2894 		mlx5e_deactivate_xsk(c);
2895 	else
2896 		mlx5e_deactivate_rq(&c->rq);
2897 
2898 	mlx5e_deactivate_icosq(&c->async_icosq);
2899 	mlx5e_deactivate_icosq(&c->icosq);
2900 	for (tc = 0; tc < c->num_tc; tc++)
2901 		mlx5e_deactivate_txqsq(&c->sq[tc]);
2902 	mlx5e_qos_deactivate_queues(c);
2903 
2904 	napi_disable_locked(&c->napi);
2905 }
2906 
mlx5e_close_channel(struct mlx5e_channel * c)2907 static void mlx5e_close_channel(struct mlx5e_channel *c)
2908 {
2909 	if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
2910 		mlx5e_close_xsk(c);
2911 	mlx5e_close_queues(c);
2912 	mlx5e_qos_close_queues(c);
2913 	netif_napi_del_locked(&c->napi);
2914 
2915 	kvfree(c);
2916 }
2917 
mlx5e_open_channels(struct mlx5e_priv * priv,struct mlx5e_channels * chs)2918 int mlx5e_open_channels(struct mlx5e_priv *priv,
2919 			struct mlx5e_channels *chs)
2920 {
2921 	int err = -ENOMEM;
2922 	int i;
2923 
2924 	chs->num = chs->params.num_channels;
2925 
2926 	chs->c = kcalloc(chs->num, sizeof(struct mlx5e_channel *), GFP_KERNEL);
2927 	if (!chs->c)
2928 		goto err_out;
2929 
2930 	for (i = 0; i < chs->num; i++) {
2931 		struct xsk_buff_pool *xsk_pool = NULL;
2932 
2933 		if (chs->params.xdp_prog)
2934 			xsk_pool = mlx5e_xsk_get_pool(&chs->params, chs->params.xsk, i);
2935 
2936 		err = mlx5e_open_channel(priv, i, &chs->params, xsk_pool, &chs->c[i]);
2937 		if (err)
2938 			goto err_close_channels;
2939 	}
2940 
2941 	if (MLX5E_GET_PFLAG(&chs->params, MLX5E_PFLAG_TX_PORT_TS) || chs->params.ptp_rx) {
2942 		err = mlx5e_ptp_open(priv, &chs->params, chs->c[0]->lag_port, &chs->ptp);
2943 		if (err)
2944 			goto err_close_channels;
2945 	}
2946 
2947 	if (priv->htb) {
2948 		err = mlx5e_qos_open_queues(priv, chs);
2949 		if (err)
2950 			goto err_close_ptp;
2951 	}
2952 
2953 	mlx5e_health_channels_update(priv);
2954 	return 0;
2955 
2956 err_close_ptp:
2957 	if (chs->ptp)
2958 		mlx5e_ptp_close(chs->ptp);
2959 
2960 err_close_channels:
2961 	for (i--; i >= 0; i--)
2962 		mlx5e_close_channel(chs->c[i]);
2963 
2964 	kfree(chs->c);
2965 err_out:
2966 	chs->num = 0;
2967 	return err;
2968 }
2969 
mlx5e_activate_channels(struct mlx5e_priv * priv,struct mlx5e_channels * chs)2970 static void mlx5e_activate_channels(struct mlx5e_priv *priv, struct mlx5e_channels *chs)
2971 {
2972 	int i;
2973 
2974 	for (i = 0; i < chs->num; i++)
2975 		mlx5e_activate_channel(chs->c[i]);
2976 
2977 	if (priv->htb)
2978 		mlx5e_qos_activate_queues(priv);
2979 
2980 	for (i = 0; i < chs->num; i++)
2981 		mlx5e_trigger_napi_icosq(chs->c[i]);
2982 
2983 	if (chs->ptp)
2984 		mlx5e_ptp_activate_channel(chs->ptp);
2985 }
2986 
mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels * chs)2987 static int mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels *chs)
2988 {
2989 	int err = 0;
2990 	int i;
2991 
2992 	for (i = 0; i < chs->num; i++) {
2993 		int timeout = err ? 0 : MLX5E_RQ_WQES_TIMEOUT;
2994 		struct mlx5e_channel *c = chs->c[i];
2995 
2996 		if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
2997 			continue;
2998 
2999 		err |= mlx5e_wait_for_min_rx_wqes(&c->rq, timeout);
3000 
3001 		/* Don't wait on the XSK RQ, because the newer xdpsock sample
3002 		 * doesn't provide any Fill Ring entries at the setup stage.
3003 		 */
3004 	}
3005 
3006 	return err ? -ETIMEDOUT : 0;
3007 }
3008 
mlx5e_deactivate_channels(struct mlx5e_channels * chs)3009 static void mlx5e_deactivate_channels(struct mlx5e_channels *chs)
3010 {
3011 	int i;
3012 
3013 	if (chs->ptp)
3014 		mlx5e_ptp_deactivate_channel(chs->ptp);
3015 
3016 	for (i = 0; i < chs->num; i++)
3017 		mlx5e_deactivate_channel(chs->c[i]);
3018 }
3019 
mlx5e_close_channels(struct mlx5e_channels * chs)3020 void mlx5e_close_channels(struct mlx5e_channels *chs)
3021 {
3022 	int i;
3023 
3024 	ASSERT_RTNL();
3025 	if (chs->ptp) {
3026 		mlx5e_ptp_close(chs->ptp);
3027 		chs->ptp = NULL;
3028 	}
3029 	for (i = 0; i < chs->num; i++)
3030 		mlx5e_close_channel(chs->c[i]);
3031 
3032 	kfree(chs->c);
3033 	chs->num = 0;
3034 }
3035 
mlx5e_modify_tirs_packet_merge(struct mlx5e_priv * priv)3036 static int mlx5e_modify_tirs_packet_merge(struct mlx5e_priv *priv)
3037 {
3038 	struct mlx5e_rx_res *res = priv->rx_res;
3039 
3040 	return mlx5e_rx_res_packet_merge_set_param(res, &priv->channels.params.packet_merge);
3041 }
3042 
3043 static MLX5E_DEFINE_PREACTIVATE_WRAPPER_CTX(mlx5e_modify_tirs_packet_merge);
3044 
mlx5e_set_mtu(struct mlx5_core_dev * mdev,struct mlx5e_params * params,u16 mtu)3045 static int mlx5e_set_mtu(struct mlx5_core_dev *mdev,
3046 			 struct mlx5e_params *params, u16 mtu)
3047 {
3048 	u16 hw_mtu = MLX5E_SW2HW_MTU(params, mtu);
3049 	int err;
3050 
3051 	err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
3052 	if (err)
3053 		return err;
3054 
3055 	/* Update vport context MTU */
3056 	mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
3057 	return 0;
3058 }
3059 
mlx5e_query_mtu(struct mlx5_core_dev * mdev,struct mlx5e_params * params,u16 * mtu)3060 static void mlx5e_query_mtu(struct mlx5_core_dev *mdev,
3061 			    struct mlx5e_params *params, u16 *mtu)
3062 {
3063 	u16 hw_mtu = 0;
3064 	int err;
3065 
3066 	err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
3067 	if (err || !hw_mtu) /* fallback to port oper mtu */
3068 		mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
3069 
3070 	*mtu = MLX5E_HW2SW_MTU(params, hw_mtu);
3071 }
3072 
mlx5e_set_dev_port_mtu(struct mlx5e_priv * priv)3073 int mlx5e_set_dev_port_mtu(struct mlx5e_priv *priv)
3074 {
3075 	struct mlx5e_params *params = &priv->channels.params;
3076 	struct net_device *netdev = priv->netdev;
3077 	struct mlx5_core_dev *mdev = priv->mdev;
3078 	u16 mtu;
3079 	int err;
3080 
3081 	err = mlx5e_set_mtu(mdev, params, params->sw_mtu);
3082 	if (err)
3083 		return err;
3084 
3085 	mlx5e_query_mtu(mdev, params, &mtu);
3086 	if (mtu != params->sw_mtu)
3087 		netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
3088 			    __func__, mtu, params->sw_mtu);
3089 
3090 	params->sw_mtu = mtu;
3091 	return 0;
3092 }
3093 
3094 MLX5E_DEFINE_PREACTIVATE_WRAPPER_CTX(mlx5e_set_dev_port_mtu);
3095 
mlx5e_set_netdev_mtu_boundaries(struct mlx5e_priv * priv)3096 void mlx5e_set_netdev_mtu_boundaries(struct mlx5e_priv *priv)
3097 {
3098 	struct mlx5e_params *params = &priv->channels.params;
3099 	struct net_device *netdev   = priv->netdev;
3100 	struct mlx5_core_dev *mdev  = priv->mdev;
3101 	u16 max_mtu;
3102 
3103 	/* MTU range: 68 - hw-specific max */
3104 	netdev->min_mtu = ETH_MIN_MTU;
3105 
3106 	mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
3107 	netdev->max_mtu = min_t(unsigned int, MLX5E_HW2SW_MTU(params, max_mtu),
3108 				ETH_MAX_MTU);
3109 }
3110 
mlx5e_netdev_set_tcs(struct net_device * netdev,u16 nch,u8 ntc,struct netdev_tc_txq * tc_to_txq)3111 static int mlx5e_netdev_set_tcs(struct net_device *netdev, u16 nch, u8 ntc,
3112 				struct netdev_tc_txq *tc_to_txq)
3113 {
3114 	int tc, err;
3115 
3116 	netdev_reset_tc(netdev);
3117 
3118 	if (ntc == 1)
3119 		return 0;
3120 
3121 	err = netdev_set_num_tc(netdev, ntc);
3122 	if (err) {
3123 		netdev_WARN(netdev, "netdev_set_num_tc failed (%d), ntc = %d\n", err, ntc);
3124 		return err;
3125 	}
3126 
3127 	for (tc = 0; tc < ntc; tc++) {
3128 		u16 count, offset;
3129 
3130 		count = tc_to_txq[tc].count;
3131 		offset = tc_to_txq[tc].offset;
3132 		netdev_set_tc_queue(netdev, tc, count, offset);
3133 	}
3134 
3135 	return 0;
3136 }
3137 
mlx5e_update_tx_netdev_queues(struct mlx5e_priv * priv)3138 int mlx5e_update_tx_netdev_queues(struct mlx5e_priv *priv)
3139 {
3140 	int nch, ntc, num_txqs, err;
3141 	int qos_queues = 0;
3142 
3143 	if (priv->htb)
3144 		qos_queues = mlx5e_htb_cur_leaf_nodes(priv->htb);
3145 
3146 	nch = priv->channels.params.num_channels;
3147 	ntc = mlx5e_get_dcb_num_tc(&priv->channels.params);
3148 	num_txqs = nch * ntc + qos_queues;
3149 	if (MLX5E_GET_PFLAG(&priv->channels.params, MLX5E_PFLAG_TX_PORT_TS))
3150 		num_txqs += ntc;
3151 
3152 	netdev_dbg(priv->netdev, "Setting num_txqs %d\n", num_txqs);
3153 	err = netif_set_real_num_tx_queues(priv->netdev, num_txqs);
3154 	if (err)
3155 		netdev_warn(priv->netdev, "netif_set_real_num_tx_queues failed, %d\n", err);
3156 
3157 	return err;
3158 }
3159 
mlx5e_set_default_xps_cpumasks(struct mlx5e_priv * priv,struct mlx5e_params * params)3160 static void mlx5e_set_default_xps_cpumasks(struct mlx5e_priv *priv,
3161 					   struct mlx5e_params *params)
3162 {
3163 	int ix;
3164 
3165 	for (ix = 0; ix < params->num_channels; ix++) {
3166 		int num_comp_vectors, irq, vec_ix;
3167 		struct mlx5_core_dev *mdev;
3168 
3169 		mdev = mlx5_sd_ch_ix_get_dev(priv->mdev, ix);
3170 		num_comp_vectors = mlx5_comp_vectors_max(mdev);
3171 		cpumask_clear(priv->scratchpad.cpumask);
3172 		vec_ix = mlx5_sd_ch_ix_get_vec_ix(mdev, ix);
3173 
3174 		for (irq = vec_ix; irq < num_comp_vectors; irq += params->num_channels) {
3175 			int cpu = mlx5_comp_vector_get_cpu(mdev, irq);
3176 
3177 			cpumask_set_cpu(cpu, priv->scratchpad.cpumask);
3178 		}
3179 
3180 		netif_set_xps_queue(priv->netdev, priv->scratchpad.cpumask, ix);
3181 	}
3182 }
3183 
mlx5e_update_tc_and_tx_queues(struct mlx5e_priv * priv)3184 static int mlx5e_update_tc_and_tx_queues(struct mlx5e_priv *priv)
3185 {
3186 	struct netdev_tc_txq old_tc_to_txq[TC_MAX_QUEUE], *tc_to_txq;
3187 	struct net_device *netdev = priv->netdev;
3188 	int old_num_txqs, old_ntc;
3189 	int nch, ntc;
3190 	int err;
3191 	int i;
3192 
3193 	old_num_txqs = netdev->real_num_tx_queues;
3194 	old_ntc = netdev->num_tc ? : 1;
3195 	for (i = 0; i < ARRAY_SIZE(old_tc_to_txq); i++)
3196 		old_tc_to_txq[i] = netdev->tc_to_txq[i];
3197 
3198 	nch = priv->channels.params.num_channels;
3199 	ntc = priv->channels.params.mqprio.num_tc;
3200 	tc_to_txq = priv->channels.params.mqprio.tc_to_txq;
3201 
3202 	err = mlx5e_netdev_set_tcs(netdev, nch, ntc, tc_to_txq);
3203 	if (err)
3204 		goto err_out;
3205 	err = mlx5e_update_tx_netdev_queues(priv);
3206 	if (err)
3207 		goto err_tcs;
3208 	mlx5e_set_default_xps_cpumasks(priv, &priv->channels.params);
3209 
3210 	return 0;
3211 
3212 err_tcs:
3213 	WARN_ON_ONCE(mlx5e_netdev_set_tcs(netdev, old_num_txqs / old_ntc, old_ntc,
3214 					  old_tc_to_txq));
3215 err_out:
3216 	return err;
3217 }
3218 
3219 MLX5E_DEFINE_PREACTIVATE_WRAPPER_CTX(mlx5e_update_tc_and_tx_queues);
3220 
mlx5e_num_channels_changed(struct mlx5e_priv * priv)3221 static int mlx5e_num_channels_changed(struct mlx5e_priv *priv)
3222 {
3223 	u16 count = priv->channels.params.num_channels;
3224 	struct net_device *netdev = priv->netdev;
3225 	int old_num_rxqs;
3226 	int err;
3227 
3228 	old_num_rxqs = netdev->real_num_rx_queues;
3229 	err = netif_set_real_num_rx_queues(netdev, count);
3230 	if (err) {
3231 		netdev_warn(netdev, "%s: netif_set_real_num_rx_queues failed, %d\n",
3232 			    __func__, err);
3233 		return err;
3234 	}
3235 	err = mlx5e_update_tc_and_tx_queues(priv);
3236 	if (err) {
3237 		/* mlx5e_update_tc_and_tx_queues can fail if channels or TCs number increases.
3238 		 * Since channel number changed, it increased. That means, the call to
3239 		 * netif_set_real_num_rx_queues below should not fail, because it
3240 		 * decreases the number of RX queues.
3241 		 */
3242 		WARN_ON_ONCE(netif_set_real_num_rx_queues(netdev, old_num_rxqs));
3243 		return err;
3244 	}
3245 
3246 	/* This function may be called on attach, before priv->rx_res is created. */
3247 	if (priv->rx_res) {
3248 		mlx5e_rx_res_rss_update_num_channels(priv->rx_res, count);
3249 
3250 		if (!netif_is_rxfh_configured(priv->netdev))
3251 			mlx5e_rx_res_rss_set_indir_uniform(priv->rx_res, count);
3252 	}
3253 
3254 	return 0;
3255 }
3256 
3257 MLX5E_DEFINE_PREACTIVATE_WRAPPER_CTX(mlx5e_num_channels_changed);
3258 
mlx5e_build_txq_maps(struct mlx5e_priv * priv)3259 static void mlx5e_build_txq_maps(struct mlx5e_priv *priv)
3260 {
3261 	int i, ch, tc, num_tc;
3262 
3263 	ch = priv->channels.num;
3264 	num_tc = mlx5e_get_dcb_num_tc(&priv->channels.params);
3265 
3266 	for (i = 0; i < ch; i++) {
3267 		for (tc = 0; tc < num_tc; tc++) {
3268 			struct mlx5e_channel *c = priv->channels.c[i];
3269 			struct mlx5e_txqsq *sq = &c->sq[tc];
3270 
3271 			priv->txq2sq[sq->txq_ix] = sq;
3272 			priv->txq2sq_stats[sq->txq_ix] = sq->stats;
3273 		}
3274 	}
3275 
3276 	if (!priv->channels.ptp)
3277 		goto out;
3278 
3279 	if (!test_bit(MLX5E_PTP_STATE_TX, priv->channels.ptp->state))
3280 		goto out;
3281 
3282 	for (tc = 0; tc < num_tc; tc++) {
3283 		struct mlx5e_ptp *c = priv->channels.ptp;
3284 		struct mlx5e_txqsq *sq = &c->ptpsq[tc].txqsq;
3285 
3286 		priv->txq2sq[sq->txq_ix] = sq;
3287 		priv->txq2sq_stats[sq->txq_ix] = sq->stats;
3288 	}
3289 
3290 out:
3291 	/* Make the change to txq2sq visible before the queue is started.
3292 	 * As mlx5e_xmit runs under a spinlock, there is an implicit ACQUIRE,
3293 	 * which pairs with this barrier.
3294 	 */
3295 	smp_wmb();
3296 }
3297 
mlx5e_activate_priv_channels(struct mlx5e_priv * priv)3298 void mlx5e_activate_priv_channels(struct mlx5e_priv *priv)
3299 {
3300 	mlx5e_build_txq_maps(priv);
3301 	mlx5e_activate_channels(priv, &priv->channels);
3302 	mlx5e_xdp_tx_enable(priv);
3303 
3304 	/* dev_watchdog() wants all TX queues to be started when the carrier is
3305 	 * OK, including the ones in range real_num_tx_queues..num_tx_queues-1.
3306 	 * Make it happy to avoid TX timeout false alarms.
3307 	 */
3308 	netif_tx_start_all_queues(priv->netdev);
3309 
3310 	if (mlx5e_is_vport_rep(priv))
3311 		mlx5e_rep_activate_channels(priv);
3312 
3313 	set_bit(MLX5E_STATE_CHANNELS_ACTIVE, &priv->state);
3314 
3315 	mlx5e_wait_channels_min_rx_wqes(&priv->channels);
3316 
3317 	if (priv->rx_res)
3318 		mlx5e_rx_res_channels_activate(priv->rx_res, &priv->channels);
3319 }
3320 
mlx5e_cancel_tx_timeout_work(struct mlx5e_priv * priv)3321 static void mlx5e_cancel_tx_timeout_work(struct mlx5e_priv *priv)
3322 {
3323 	WARN_ON_ONCE(test_bit(MLX5E_STATE_CHANNELS_ACTIVE, &priv->state));
3324 	if (current_work() != &priv->tx_timeout_work)
3325 		cancel_work_sync(&priv->tx_timeout_work);
3326 }
3327 
mlx5e_deactivate_priv_channels(struct mlx5e_priv * priv)3328 void mlx5e_deactivate_priv_channels(struct mlx5e_priv *priv)
3329 {
3330 	if (priv->rx_res)
3331 		mlx5e_rx_res_channels_deactivate(priv->rx_res);
3332 
3333 	clear_bit(MLX5E_STATE_CHANNELS_ACTIVE, &priv->state);
3334 	mlx5e_cancel_tx_timeout_work(priv);
3335 
3336 	if (mlx5e_is_vport_rep(priv))
3337 		mlx5e_rep_deactivate_channels(priv);
3338 
3339 	/* The results of ndo_select_queue are unreliable, while netdev config
3340 	 * is being changed (real_num_tx_queues, num_tc). Stop all queues to
3341 	 * prevent ndo_start_xmit from being called, so that it can assume that
3342 	 * the selected queue is always valid.
3343 	 */
3344 	netif_tx_disable(priv->netdev);
3345 
3346 	mlx5e_xdp_tx_disable(priv);
3347 	mlx5e_deactivate_channels(&priv->channels);
3348 }
3349 
mlx5e_switch_priv_params(struct mlx5e_priv * priv,struct mlx5e_params * new_params,mlx5e_fp_preactivate preactivate,void * context)3350 static int mlx5e_switch_priv_params(struct mlx5e_priv *priv,
3351 				    struct mlx5e_params *new_params,
3352 				    mlx5e_fp_preactivate preactivate,
3353 				    void *context)
3354 {
3355 	struct mlx5e_params old_params;
3356 
3357 	old_params = priv->channels.params;
3358 	priv->channels.params = *new_params;
3359 
3360 	if (preactivate) {
3361 		int err;
3362 
3363 		err = preactivate(priv, context);
3364 		if (err) {
3365 			priv->channels.params = old_params;
3366 			return err;
3367 		}
3368 	}
3369 
3370 	return 0;
3371 }
3372 
mlx5e_switch_priv_channels(struct mlx5e_priv * priv,struct mlx5e_channels * new_chs,mlx5e_fp_preactivate preactivate,void * context)3373 static int mlx5e_switch_priv_channels(struct mlx5e_priv *priv,
3374 				      struct mlx5e_channels *new_chs,
3375 				      mlx5e_fp_preactivate preactivate,
3376 				      void *context)
3377 {
3378 	struct net_device *netdev = priv->netdev;
3379 	struct mlx5e_channels old_chs;
3380 	int carrier_ok;
3381 	int err = 0;
3382 
3383 	carrier_ok = netif_carrier_ok(netdev);
3384 	netif_carrier_off(netdev);
3385 
3386 	mlx5e_deactivate_priv_channels(priv);
3387 
3388 	old_chs = priv->channels;
3389 	priv->channels = *new_chs;
3390 
3391 	/* New channels are ready to roll, call the preactivate hook if needed
3392 	 * to modify HW settings or update kernel parameters.
3393 	 */
3394 	if (preactivate) {
3395 		err = preactivate(priv, context);
3396 		if (err) {
3397 			priv->channels = old_chs;
3398 			goto out;
3399 		}
3400 	}
3401 
3402 	mlx5e_close_channels(&old_chs);
3403 	priv->profile->update_rx(priv);
3404 
3405 	mlx5e_selq_apply(&priv->selq);
3406 out:
3407 	mlx5e_activate_priv_channels(priv);
3408 
3409 	/* return carrier back if needed */
3410 	if (carrier_ok)
3411 		netif_carrier_on(netdev);
3412 
3413 	return err;
3414 }
3415 
mlx5e_safe_switch_params(struct mlx5e_priv * priv,struct mlx5e_params * params,mlx5e_fp_preactivate preactivate,void * context,bool reset)3416 int mlx5e_safe_switch_params(struct mlx5e_priv *priv,
3417 			     struct mlx5e_params *params,
3418 			     mlx5e_fp_preactivate preactivate,
3419 			     void *context, bool reset)
3420 {
3421 	struct mlx5e_channels *new_chs;
3422 	int err;
3423 
3424 	reset &= test_bit(MLX5E_STATE_OPENED, &priv->state);
3425 	if (!reset)
3426 		return mlx5e_switch_priv_params(priv, params, preactivate, context);
3427 
3428 	new_chs = kzalloc(sizeof(*new_chs), GFP_KERNEL);
3429 	if (!new_chs)
3430 		return -ENOMEM;
3431 	new_chs->params = *params;
3432 
3433 	mlx5e_selq_prepare_params(&priv->selq, &new_chs->params);
3434 
3435 	err = mlx5e_open_channels(priv, new_chs);
3436 	if (err)
3437 		goto err_cancel_selq;
3438 
3439 	err = mlx5e_switch_priv_channels(priv, new_chs, preactivate, context);
3440 	if (err)
3441 		goto err_close;
3442 
3443 	kfree(new_chs);
3444 	return 0;
3445 
3446 err_close:
3447 	mlx5e_close_channels(new_chs);
3448 
3449 err_cancel_selq:
3450 	mlx5e_selq_cancel(&priv->selq);
3451 	kfree(new_chs);
3452 	return err;
3453 }
3454 
mlx5e_safe_reopen_channels(struct mlx5e_priv * priv)3455 int mlx5e_safe_reopen_channels(struct mlx5e_priv *priv)
3456 {
3457 	return mlx5e_safe_switch_params(priv, &priv->channels.params, NULL, NULL, true);
3458 }
3459 
mlx5e_timestamp_init(struct mlx5e_priv * priv)3460 void mlx5e_timestamp_init(struct mlx5e_priv *priv)
3461 {
3462 	priv->tstamp.tx_type   = HWTSTAMP_TX_OFF;
3463 	priv->tstamp.rx_filter = HWTSTAMP_FILTER_NONE;
3464 }
3465 
mlx5e_modify_admin_state(struct mlx5_core_dev * mdev,enum mlx5_port_status state)3466 static void mlx5e_modify_admin_state(struct mlx5_core_dev *mdev,
3467 				     enum mlx5_port_status state)
3468 {
3469 	struct mlx5_eswitch *esw = mdev->priv.eswitch;
3470 	int vport_admin_state;
3471 
3472 	mlx5_set_port_admin_status(mdev, state);
3473 
3474 	if (mlx5_eswitch_mode(mdev) == MLX5_ESWITCH_OFFLOADS ||
3475 	    !MLX5_CAP_GEN(mdev, uplink_follow))
3476 		return;
3477 
3478 	if (state == MLX5_PORT_UP)
3479 		vport_admin_state = MLX5_VPORT_ADMIN_STATE_AUTO;
3480 	else
3481 		vport_admin_state = MLX5_VPORT_ADMIN_STATE_DOWN;
3482 
3483 	mlx5_eswitch_set_vport_state(esw, MLX5_VPORT_UPLINK, vport_admin_state);
3484 }
3485 
mlx5e_open_locked(struct net_device * netdev)3486 int mlx5e_open_locked(struct net_device *netdev)
3487 {
3488 	struct mlx5e_priv *priv = netdev_priv(netdev);
3489 	int err;
3490 
3491 	mlx5e_selq_prepare_params(&priv->selq, &priv->channels.params);
3492 
3493 	set_bit(MLX5E_STATE_OPENED, &priv->state);
3494 
3495 	err = mlx5e_open_channels(priv, &priv->channels);
3496 	if (err)
3497 		goto err_clear_state_opened_flag;
3498 
3499 	err = priv->profile->update_rx(priv);
3500 	if (err)
3501 		goto err_close_channels;
3502 
3503 	mlx5e_selq_apply(&priv->selq);
3504 	mlx5e_activate_priv_channels(priv);
3505 	mlx5e_apply_traps(priv, true);
3506 	if (priv->profile->update_carrier)
3507 		priv->profile->update_carrier(priv);
3508 
3509 	mlx5e_queue_update_stats(priv);
3510 	return 0;
3511 
3512 err_close_channels:
3513 	mlx5e_close_channels(&priv->channels);
3514 err_clear_state_opened_flag:
3515 	clear_bit(MLX5E_STATE_OPENED, &priv->state);
3516 	mlx5e_selq_cancel(&priv->selq);
3517 	return err;
3518 }
3519 
mlx5e_open(struct net_device * netdev)3520 int mlx5e_open(struct net_device *netdev)
3521 {
3522 	struct mlx5e_priv *priv = netdev_priv(netdev);
3523 	int err;
3524 
3525 	mutex_lock(&priv->state_lock);
3526 	err = mlx5e_open_locked(netdev);
3527 	if (!err)
3528 		mlx5e_modify_admin_state(priv->mdev, MLX5_PORT_UP);
3529 	mutex_unlock(&priv->state_lock);
3530 
3531 	return err;
3532 }
3533 
mlx5e_close_locked(struct net_device * netdev)3534 int mlx5e_close_locked(struct net_device *netdev)
3535 {
3536 	struct mlx5e_priv *priv = netdev_priv(netdev);
3537 
3538 	/* May already be CLOSED in case a previous configuration operation
3539 	 * (e.g RX/TX queue size change) that involves close&open failed.
3540 	 */
3541 	if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3542 		return 0;
3543 
3544 	mlx5e_apply_traps(priv, false);
3545 	clear_bit(MLX5E_STATE_OPENED, &priv->state);
3546 
3547 	netif_carrier_off(priv->netdev);
3548 	mlx5e_deactivate_priv_channels(priv);
3549 	mlx5e_close_channels(&priv->channels);
3550 
3551 	return 0;
3552 }
3553 
mlx5e_close(struct net_device * netdev)3554 int mlx5e_close(struct net_device *netdev)
3555 {
3556 	struct mlx5e_priv *priv = netdev_priv(netdev);
3557 	int err;
3558 
3559 	if (!netif_device_present(netdev))
3560 		return -ENODEV;
3561 
3562 	mutex_lock(&priv->state_lock);
3563 	mlx5e_modify_admin_state(priv->mdev, MLX5_PORT_DOWN);
3564 	err = mlx5e_close_locked(netdev);
3565 	mutex_unlock(&priv->state_lock);
3566 
3567 	return err;
3568 }
3569 
mlx5e_free_drop_rq(struct mlx5e_rq * rq)3570 static void mlx5e_free_drop_rq(struct mlx5e_rq *rq)
3571 {
3572 	mlx5_wq_destroy(&rq->wq_ctrl);
3573 }
3574 
mlx5e_alloc_drop_rq(struct mlx5_core_dev * mdev,struct mlx5e_rq * rq,struct mlx5e_rq_param * param)3575 static int mlx5e_alloc_drop_rq(struct mlx5_core_dev *mdev,
3576 			       struct mlx5e_rq *rq,
3577 			       struct mlx5e_rq_param *param)
3578 {
3579 	void *rqc = param->rqc;
3580 	void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
3581 	int err;
3582 
3583 	param->wq.db_numa_node = param->wq.buf_numa_node;
3584 
3585 	err = mlx5_wq_cyc_create(mdev, &param->wq, rqc_wq, &rq->wqe.wq,
3586 				 &rq->wq_ctrl);
3587 	if (err)
3588 		return err;
3589 
3590 	/* Mark as unused given "Drop-RQ" packets never reach XDP */
3591 	xdp_rxq_info_unused(&rq->xdp_rxq);
3592 
3593 	rq->mdev = mdev;
3594 
3595 	return 0;
3596 }
3597 
mlx5e_alloc_drop_cq(struct mlx5e_priv * priv,struct mlx5e_cq * cq,struct mlx5e_cq_param * param)3598 static int mlx5e_alloc_drop_cq(struct mlx5e_priv *priv,
3599 			       struct mlx5e_cq *cq,
3600 			       struct mlx5e_cq_param *param)
3601 {
3602 	struct mlx5_core_dev *mdev = priv->mdev;
3603 
3604 	param->wq.buf_numa_node = dev_to_node(mlx5_core_dma_dev(mdev));
3605 	param->wq.db_numa_node  = dev_to_node(mlx5_core_dma_dev(mdev));
3606 
3607 	return mlx5e_alloc_cq_common(priv->mdev, priv->netdev, priv->wq,
3608 				     mdev->priv.bfreg.up, param, cq);
3609 }
3610 
mlx5e_open_drop_rq(struct mlx5e_priv * priv,struct mlx5e_rq * drop_rq)3611 int mlx5e_open_drop_rq(struct mlx5e_priv *priv,
3612 		       struct mlx5e_rq *drop_rq)
3613 {
3614 	struct mlx5_core_dev *mdev = priv->mdev;
3615 	struct mlx5e_cq_param cq_param = {};
3616 	struct mlx5e_rq_param rq_param = {};
3617 	struct mlx5e_cq *cq = &drop_rq->cq;
3618 	int err;
3619 
3620 	mlx5e_build_drop_rq_param(mdev, &rq_param);
3621 
3622 	err = mlx5e_alloc_drop_cq(priv, cq, &cq_param);
3623 	if (err)
3624 		return err;
3625 
3626 	err = mlx5e_create_cq(cq, &cq_param);
3627 	if (err)
3628 		goto err_free_cq;
3629 
3630 	err = mlx5e_alloc_drop_rq(mdev, drop_rq, &rq_param);
3631 	if (err)
3632 		goto err_destroy_cq;
3633 
3634 	err = mlx5e_create_rq(drop_rq, &rq_param, priv->drop_rq_q_counter);
3635 	if (err)
3636 		goto err_free_rq;
3637 
3638 	err = mlx5e_modify_rq_state(drop_rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
3639 	if (err)
3640 		mlx5_core_warn(priv->mdev, "modify_rq_state failed, rx_if_down_packets won't be counted %d\n", err);
3641 
3642 	return 0;
3643 
3644 err_free_rq:
3645 	mlx5e_free_drop_rq(drop_rq);
3646 
3647 err_destroy_cq:
3648 	mlx5e_destroy_cq(cq);
3649 
3650 err_free_cq:
3651 	mlx5e_free_cq(cq);
3652 
3653 	return err;
3654 }
3655 
mlx5e_close_drop_rq(struct mlx5e_rq * drop_rq)3656 void mlx5e_close_drop_rq(struct mlx5e_rq *drop_rq)
3657 {
3658 	mlx5e_destroy_rq(drop_rq);
3659 	mlx5e_free_drop_rq(drop_rq);
3660 	mlx5e_destroy_cq(&drop_rq->cq);
3661 	mlx5e_free_cq(&drop_rq->cq);
3662 }
3663 
mlx5e_cleanup_nic_tx(struct mlx5e_priv * priv)3664 static void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
3665 {
3666 	if (priv->mqprio_rl) {
3667 		mlx5e_mqprio_rl_cleanup(priv->mqprio_rl);
3668 		mlx5e_mqprio_rl_free(priv->mqprio_rl);
3669 		priv->mqprio_rl = NULL;
3670 	}
3671 	mlx5e_accel_cleanup_tx(priv);
3672 }
3673 
mlx5e_modify_channels_vsd(struct mlx5e_channels * chs,bool vsd)3674 static int mlx5e_modify_channels_vsd(struct mlx5e_channels *chs, bool vsd)
3675 {
3676 	int err;
3677 	int i;
3678 
3679 	for (i = 0; i < chs->num; i++) {
3680 		err = mlx5e_modify_rq_vsd(&chs->c[i]->rq, vsd);
3681 		if (err)
3682 			return err;
3683 	}
3684 	if (chs->ptp && test_bit(MLX5E_PTP_STATE_RX, chs->ptp->state))
3685 		return mlx5e_modify_rq_vsd(&chs->ptp->rq, vsd);
3686 
3687 	return 0;
3688 }
3689 
mlx5e_mqprio_build_default_tc_to_txq(struct netdev_tc_txq * tc_to_txq,int ntc,int nch)3690 static void mlx5e_mqprio_build_default_tc_to_txq(struct netdev_tc_txq *tc_to_txq,
3691 						 int ntc, int nch)
3692 {
3693 	int tc;
3694 
3695 	memset(tc_to_txq, 0, sizeof(*tc_to_txq) * TC_MAX_QUEUE);
3696 
3697 	/* Map netdev TCs to offset 0.
3698 	 * We have our own UP to TXQ mapping for DCB mode of QoS
3699 	 */
3700 	for (tc = 0; tc < ntc; tc++) {
3701 		tc_to_txq[tc] = (struct netdev_tc_txq) {
3702 			.count = nch,
3703 			.offset = 0,
3704 		};
3705 	}
3706 }
3707 
mlx5e_mqprio_build_tc_to_txq(struct netdev_tc_txq * tc_to_txq,struct tc_mqprio_qopt * qopt)3708 static void mlx5e_mqprio_build_tc_to_txq(struct netdev_tc_txq *tc_to_txq,
3709 					 struct tc_mqprio_qopt *qopt)
3710 {
3711 	int tc;
3712 
3713 	for (tc = 0; tc < TC_MAX_QUEUE; tc++) {
3714 		tc_to_txq[tc] = (struct netdev_tc_txq) {
3715 			.count = qopt->count[tc],
3716 			.offset = qopt->offset[tc],
3717 		};
3718 	}
3719 }
3720 
mlx5e_params_mqprio_dcb_set(struct mlx5e_params * params,u8 num_tc)3721 static void mlx5e_params_mqprio_dcb_set(struct mlx5e_params *params, u8 num_tc)
3722 {
3723 	params->mqprio.mode = TC_MQPRIO_MODE_DCB;
3724 	params->mqprio.num_tc = num_tc;
3725 	mlx5e_mqprio_build_default_tc_to_txq(params->mqprio.tc_to_txq, num_tc,
3726 					     params->num_channels);
3727 }
3728 
mlx5e_mqprio_rl_update_params(struct mlx5e_params * params,struct mlx5e_mqprio_rl * rl)3729 static void mlx5e_mqprio_rl_update_params(struct mlx5e_params *params,
3730 					  struct mlx5e_mqprio_rl *rl)
3731 {
3732 	int tc;
3733 
3734 	for (tc = 0; tc < TC_MAX_QUEUE; tc++) {
3735 		u32 hw_id = 0;
3736 
3737 		if (rl)
3738 			mlx5e_mqprio_rl_get_node_hw_id(rl, tc, &hw_id);
3739 		params->mqprio.channel.hw_id[tc] = hw_id;
3740 	}
3741 }
3742 
mlx5e_params_mqprio_channel_set(struct mlx5e_params * params,struct tc_mqprio_qopt_offload * mqprio,struct mlx5e_mqprio_rl * rl)3743 static void mlx5e_params_mqprio_channel_set(struct mlx5e_params *params,
3744 					    struct tc_mqprio_qopt_offload *mqprio,
3745 					    struct mlx5e_mqprio_rl *rl)
3746 {
3747 	int tc;
3748 
3749 	params->mqprio.mode = TC_MQPRIO_MODE_CHANNEL;
3750 	params->mqprio.num_tc = mqprio->qopt.num_tc;
3751 
3752 	for (tc = 0; tc < TC_MAX_QUEUE; tc++)
3753 		params->mqprio.channel.max_rate[tc] = mqprio->max_rate[tc];
3754 
3755 	mlx5e_mqprio_rl_update_params(params, rl);
3756 	mlx5e_mqprio_build_tc_to_txq(params->mqprio.tc_to_txq, &mqprio->qopt);
3757 }
3758 
mlx5e_params_mqprio_reset(struct mlx5e_params * params)3759 static void mlx5e_params_mqprio_reset(struct mlx5e_params *params)
3760 {
3761 	mlx5e_params_mqprio_dcb_set(params, 1);
3762 }
3763 
mlx5e_setup_tc_mqprio_dcb(struct mlx5e_priv * priv,struct tc_mqprio_qopt * mqprio)3764 static int mlx5e_setup_tc_mqprio_dcb(struct mlx5e_priv *priv,
3765 				     struct tc_mqprio_qopt *mqprio)
3766 {
3767 	struct mlx5e_params new_params;
3768 	u8 tc = mqprio->num_tc;
3769 	int err;
3770 
3771 	mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
3772 
3773 	if (tc && tc != MLX5_MAX_NUM_TC)
3774 		return -EINVAL;
3775 
3776 	new_params = priv->channels.params;
3777 	mlx5e_params_mqprio_dcb_set(&new_params, tc ? tc : 1);
3778 
3779 	err = mlx5e_safe_switch_params(priv, &new_params,
3780 				       mlx5e_update_tc_and_tx_queues_ctx, NULL, true);
3781 
3782 	if (!err && priv->mqprio_rl) {
3783 		mlx5e_mqprio_rl_cleanup(priv->mqprio_rl);
3784 		mlx5e_mqprio_rl_free(priv->mqprio_rl);
3785 		priv->mqprio_rl = NULL;
3786 	}
3787 
3788 	priv->max_opened_tc = max_t(u8, priv->max_opened_tc,
3789 				    mlx5e_get_dcb_num_tc(&priv->channels.params));
3790 	return err;
3791 }
3792 
mlx5e_mqprio_channel_validate(struct mlx5e_priv * priv,struct tc_mqprio_qopt_offload * mqprio)3793 static int mlx5e_mqprio_channel_validate(struct mlx5e_priv *priv,
3794 					 struct tc_mqprio_qopt_offload *mqprio)
3795 {
3796 	struct net_device *netdev = priv->netdev;
3797 	struct mlx5e_ptp *ptp_channel;
3798 	int agg_count = 0;
3799 	int i;
3800 
3801 	ptp_channel = priv->channels.ptp;
3802 	if (ptp_channel && test_bit(MLX5E_PTP_STATE_TX, ptp_channel->state)) {
3803 		netdev_err(netdev,
3804 			   "Cannot activate MQPRIO mode channel since it conflicts with TX port TS\n");
3805 		return -EINVAL;
3806 	}
3807 
3808 	if (mqprio->qopt.offset[0] != 0 || mqprio->qopt.num_tc < 1 ||
3809 	    mqprio->qopt.num_tc > MLX5E_MAX_NUM_MQPRIO_CH_TC)
3810 		return -EINVAL;
3811 
3812 	for (i = 0; i < mqprio->qopt.num_tc; i++) {
3813 		if (!mqprio->qopt.count[i]) {
3814 			netdev_err(netdev, "Zero size for queue-group (%d) is not supported\n", i);
3815 			return -EINVAL;
3816 		}
3817 		if (mqprio->min_rate[i]) {
3818 			netdev_err(netdev, "Min tx rate is not supported\n");
3819 			return -EINVAL;
3820 		}
3821 
3822 		if (mqprio->max_rate[i]) {
3823 			int err;
3824 
3825 			err = mlx5e_qos_bytes_rate_check(priv->mdev, mqprio->max_rate[i]);
3826 			if (err)
3827 				return err;
3828 		}
3829 
3830 		if (mqprio->qopt.offset[i] != agg_count) {
3831 			netdev_err(netdev, "Discontinuous queues config is not supported\n");
3832 			return -EINVAL;
3833 		}
3834 		agg_count += mqprio->qopt.count[i];
3835 	}
3836 
3837 	if (priv->channels.params.num_channels != agg_count) {
3838 		netdev_err(netdev, "Num of queues (%d) does not match available (%d)\n",
3839 			   agg_count, priv->channels.params.num_channels);
3840 		return -EINVAL;
3841 	}
3842 
3843 	return 0;
3844 }
3845 
mlx5e_mqprio_rate_limit(u8 num_tc,u64 max_rate[])3846 static bool mlx5e_mqprio_rate_limit(u8 num_tc, u64 max_rate[])
3847 {
3848 	int tc;
3849 
3850 	for (tc = 0; tc < num_tc; tc++)
3851 		if (max_rate[tc])
3852 			return true;
3853 	return false;
3854 }
3855 
mlx5e_mqprio_rl_create(struct mlx5_core_dev * mdev,u8 num_tc,u64 max_rate[])3856 static struct mlx5e_mqprio_rl *mlx5e_mqprio_rl_create(struct mlx5_core_dev *mdev,
3857 						      u8 num_tc, u64 max_rate[])
3858 {
3859 	struct mlx5e_mqprio_rl *rl;
3860 	int err;
3861 
3862 	if (!mlx5e_mqprio_rate_limit(num_tc, max_rate))
3863 		return NULL;
3864 
3865 	rl = mlx5e_mqprio_rl_alloc();
3866 	if (!rl)
3867 		return ERR_PTR(-ENOMEM);
3868 
3869 	err = mlx5e_mqprio_rl_init(rl, mdev, num_tc, max_rate);
3870 	if (err) {
3871 		mlx5e_mqprio_rl_free(rl);
3872 		return ERR_PTR(err);
3873 	}
3874 
3875 	return rl;
3876 }
3877 
mlx5e_setup_tc_mqprio_channel(struct mlx5e_priv * priv,struct tc_mqprio_qopt_offload * mqprio)3878 static int mlx5e_setup_tc_mqprio_channel(struct mlx5e_priv *priv,
3879 					 struct tc_mqprio_qopt_offload *mqprio)
3880 {
3881 	struct mlx5e_params new_params;
3882 	struct mlx5e_mqprio_rl *rl;
3883 	int err;
3884 
3885 	err = mlx5e_mqprio_channel_validate(priv, mqprio);
3886 	if (err)
3887 		return err;
3888 
3889 	rl = mlx5e_mqprio_rl_create(priv->mdev, mqprio->qopt.num_tc, mqprio->max_rate);
3890 	if (IS_ERR(rl))
3891 		return PTR_ERR(rl);
3892 
3893 	new_params = priv->channels.params;
3894 	mlx5e_params_mqprio_channel_set(&new_params, mqprio, rl);
3895 
3896 	err = mlx5e_safe_switch_params(priv, &new_params,
3897 				       mlx5e_update_tc_and_tx_queues_ctx, NULL, true);
3898 	if (err) {
3899 		if (rl) {
3900 			mlx5e_mqprio_rl_cleanup(rl);
3901 			mlx5e_mqprio_rl_free(rl);
3902 		}
3903 		return err;
3904 	}
3905 
3906 	if (priv->mqprio_rl) {
3907 		mlx5e_mqprio_rl_cleanup(priv->mqprio_rl);
3908 		mlx5e_mqprio_rl_free(priv->mqprio_rl);
3909 	}
3910 	priv->mqprio_rl = rl;
3911 
3912 	return 0;
3913 }
3914 
mlx5e_setup_tc_mqprio(struct mlx5e_priv * priv,struct tc_mqprio_qopt_offload * mqprio)3915 static int mlx5e_setup_tc_mqprio(struct mlx5e_priv *priv,
3916 				 struct tc_mqprio_qopt_offload *mqprio)
3917 {
3918 	/* MQPRIO is another toplevel qdisc that can't be attached
3919 	 * simultaneously with the offloaded HTB.
3920 	 */
3921 	if (mlx5e_selq_is_htb_enabled(&priv->selq)) {
3922 		NL_SET_ERR_MSG_MOD(mqprio->extack,
3923 				   "MQPRIO cannot be configured when HTB offload is enabled.");
3924 		return -EOPNOTSUPP;
3925 	}
3926 
3927 	switch (mqprio->mode) {
3928 	case TC_MQPRIO_MODE_DCB:
3929 		return mlx5e_setup_tc_mqprio_dcb(priv, &mqprio->qopt);
3930 	case TC_MQPRIO_MODE_CHANNEL:
3931 		return mlx5e_setup_tc_mqprio_channel(priv, mqprio);
3932 	default:
3933 		return -EOPNOTSUPP;
3934 	}
3935 }
3936 
3937 static LIST_HEAD(mlx5e_block_cb_list);
3938 
mlx5e_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)3939 static int mlx5e_setup_tc(struct net_device *dev, enum tc_setup_type type,
3940 			  void *type_data)
3941 {
3942 	struct mlx5e_priv *priv = netdev_priv(dev);
3943 	bool tc_unbind = false;
3944 	int err;
3945 
3946 	if (type == TC_SETUP_BLOCK &&
3947 	    ((struct flow_block_offload *)type_data)->command == FLOW_BLOCK_UNBIND)
3948 		tc_unbind = true;
3949 
3950 	if (!netif_device_present(dev) && !tc_unbind)
3951 		return -ENODEV;
3952 
3953 	switch (type) {
3954 	case TC_SETUP_BLOCK: {
3955 		struct flow_block_offload *f = type_data;
3956 
3957 		f->unlocked_driver_cb = true;
3958 		return flow_block_cb_setup_simple(type_data,
3959 						  &mlx5e_block_cb_list,
3960 						  mlx5e_setup_tc_block_cb,
3961 						  priv, priv, true);
3962 	}
3963 	case TC_SETUP_QDISC_MQPRIO:
3964 		mutex_lock(&priv->state_lock);
3965 		err = mlx5e_setup_tc_mqprio(priv, type_data);
3966 		mutex_unlock(&priv->state_lock);
3967 		return err;
3968 	case TC_SETUP_QDISC_HTB:
3969 		mutex_lock(&priv->state_lock);
3970 		err = mlx5e_htb_setup_tc(priv, type_data);
3971 		mutex_unlock(&priv->state_lock);
3972 		return err;
3973 	default:
3974 		return -EOPNOTSUPP;
3975 	}
3976 }
3977 
mlx5e_fold_sw_stats64(struct mlx5e_priv * priv,struct rtnl_link_stats64 * s)3978 void mlx5e_fold_sw_stats64(struct mlx5e_priv *priv, struct rtnl_link_stats64 *s)
3979 {
3980 	int i;
3981 
3982 	for (i = 0; i < priv->stats_nch; i++) {
3983 		struct mlx5e_channel_stats *channel_stats = priv->channel_stats[i];
3984 		struct mlx5e_rq_stats *xskrq_stats = &channel_stats->xskrq;
3985 		struct mlx5e_rq_stats *rq_stats = &channel_stats->rq;
3986 		int j;
3987 
3988 		s->rx_packets   += rq_stats->packets + xskrq_stats->packets;
3989 		s->rx_bytes     += rq_stats->bytes + xskrq_stats->bytes;
3990 		s->multicast    += rq_stats->mcast_packets + xskrq_stats->mcast_packets;
3991 
3992 		for (j = 0; j < priv->max_opened_tc; j++) {
3993 			struct mlx5e_sq_stats *sq_stats = &channel_stats->sq[j];
3994 
3995 			s->tx_packets    += sq_stats->packets;
3996 			s->tx_bytes      += sq_stats->bytes;
3997 			s->tx_dropped    += sq_stats->dropped;
3998 		}
3999 	}
4000 	if (priv->tx_ptp_opened) {
4001 		for (i = 0; i < priv->max_opened_tc; i++) {
4002 			struct mlx5e_sq_stats *sq_stats = &priv->ptp_stats.sq[i];
4003 
4004 			s->tx_packets    += sq_stats->packets;
4005 			s->tx_bytes      += sq_stats->bytes;
4006 			s->tx_dropped    += sq_stats->dropped;
4007 		}
4008 	}
4009 	if (priv->rx_ptp_opened) {
4010 		struct mlx5e_rq_stats *rq_stats = &priv->ptp_stats.rq;
4011 
4012 		s->rx_packets   += rq_stats->packets;
4013 		s->rx_bytes     += rq_stats->bytes;
4014 		s->multicast    += rq_stats->mcast_packets;
4015 	}
4016 }
4017 
4018 void
mlx5e_get_stats(struct net_device * dev,struct rtnl_link_stats64 * stats)4019 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
4020 {
4021 	struct mlx5e_priv *priv = netdev_priv(dev);
4022 	struct mlx5e_pport_stats *pstats = &priv->stats.pport;
4023 
4024 	if (!netif_device_present(dev))
4025 		return;
4026 
4027 	/* In switchdev mode, monitor counters doesn't monitor
4028 	 * rx/tx stats of 802_3. The update stats mechanism
4029 	 * should keep the 802_3 layout counters updated
4030 	 */
4031 	if (!mlx5e_monitor_counter_supported(priv) ||
4032 	    mlx5e_is_uplink_rep(priv)) {
4033 		/* update HW stats in background for next time */
4034 		mlx5e_queue_update_stats(priv);
4035 	}
4036 
4037 	if (mlx5e_is_uplink_rep(priv)) {
4038 		struct mlx5e_vport_stats *vstats = &priv->stats.vport;
4039 
4040 		stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
4041 		stats->rx_bytes   = PPORT_802_3_GET(pstats, a_octets_received_ok);
4042 		stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
4043 		stats->tx_bytes   = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
4044 
4045 		/* vport multicast also counts packets that are dropped due to steering
4046 		 * or rx out of buffer
4047 		 */
4048 		stats->multicast = VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
4049 	} else {
4050 		mlx5e_fold_sw_stats64(priv, stats);
4051 	}
4052 
4053 	stats->rx_missed_errors = priv->stats.qcnt.rx_out_of_buffer;
4054 	stats->rx_dropped = PPORT_2863_GET(pstats, if_in_discards);
4055 
4056 	stats->rx_length_errors =
4057 		PPORT_802_3_GET(pstats, a_in_range_length_errors) +
4058 		PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
4059 		PPORT_802_3_GET(pstats, a_frame_too_long_errors) +
4060 		VNIC_ENV_GET(&priv->stats.vnic, eth_wqe_too_small);
4061 	stats->rx_crc_errors =
4062 		PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
4063 	stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
4064 	stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
4065 	stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
4066 			   stats->rx_frame_errors;
4067 	stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
4068 }
4069 
mlx5e_nic_set_rx_mode(struct mlx5e_priv * priv)4070 static void mlx5e_nic_set_rx_mode(struct mlx5e_priv *priv)
4071 {
4072 	if (mlx5e_is_uplink_rep(priv))
4073 		return; /* no rx mode for uplink rep */
4074 
4075 	queue_work(priv->wq, &priv->set_rx_mode_work);
4076 }
4077 
mlx5e_set_rx_mode(struct net_device * dev)4078 static void mlx5e_set_rx_mode(struct net_device *dev)
4079 {
4080 	struct mlx5e_priv *priv = netdev_priv(dev);
4081 
4082 	mlx5e_nic_set_rx_mode(priv);
4083 }
4084 
mlx5e_set_mac(struct net_device * netdev,void * addr)4085 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
4086 {
4087 	struct mlx5e_priv *priv = netdev_priv(netdev);
4088 	struct sockaddr *saddr = addr;
4089 
4090 	if (!is_valid_ether_addr(saddr->sa_data))
4091 		return -EADDRNOTAVAIL;
4092 
4093 	netif_addr_lock_bh(netdev);
4094 	eth_hw_addr_set(netdev, saddr->sa_data);
4095 	netif_addr_unlock_bh(netdev);
4096 
4097 	mlx5e_nic_set_rx_mode(priv);
4098 
4099 	return 0;
4100 }
4101 
4102 #define MLX5E_SET_FEATURE(features, feature, enable)	\
4103 	do {						\
4104 		if (enable)				\
4105 			*features |= feature;		\
4106 		else					\
4107 			*features &= ~feature;		\
4108 	} while (0)
4109 
4110 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
4111 
set_feature_lro(struct net_device * netdev,bool enable)4112 static int set_feature_lro(struct net_device *netdev, bool enable)
4113 {
4114 	struct mlx5e_priv *priv = netdev_priv(netdev);
4115 	struct mlx5_core_dev *mdev = priv->mdev;
4116 	struct mlx5e_params *cur_params;
4117 	struct mlx5e_params new_params;
4118 	bool reset = true;
4119 	int err = 0;
4120 
4121 	mutex_lock(&priv->state_lock);
4122 
4123 	cur_params = &priv->channels.params;
4124 	new_params = *cur_params;
4125 
4126 	if (enable)
4127 		new_params.packet_merge.type = MLX5E_PACKET_MERGE_LRO;
4128 	else if (new_params.packet_merge.type == MLX5E_PACKET_MERGE_LRO)
4129 		new_params.packet_merge.type = MLX5E_PACKET_MERGE_NONE;
4130 	else
4131 		goto out;
4132 
4133 	if (!(cur_params->packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO &&
4134 	      new_params.packet_merge.type == MLX5E_PACKET_MERGE_LRO)) {
4135 		if (cur_params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
4136 			if (mlx5e_rx_mpwqe_is_linear_skb(mdev, cur_params, NULL) ==
4137 			    mlx5e_rx_mpwqe_is_linear_skb(mdev, &new_params, NULL))
4138 				reset = false;
4139 		}
4140 	}
4141 
4142 	err = mlx5e_safe_switch_params(priv, &new_params,
4143 				       mlx5e_modify_tirs_packet_merge_ctx, NULL, reset);
4144 out:
4145 	mutex_unlock(&priv->state_lock);
4146 	return err;
4147 }
4148 
set_feature_hw_gro(struct net_device * netdev,bool enable)4149 static int set_feature_hw_gro(struct net_device *netdev, bool enable)
4150 {
4151 	struct mlx5e_priv *priv = netdev_priv(netdev);
4152 	struct mlx5e_params new_params;
4153 	bool reset = true;
4154 	int err = 0;
4155 
4156 	mutex_lock(&priv->state_lock);
4157 	new_params = priv->channels.params;
4158 
4159 	if (enable) {
4160 		new_params.packet_merge.type = MLX5E_PACKET_MERGE_SHAMPO;
4161 	} else if (new_params.packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO) {
4162 		new_params.packet_merge.type = MLX5E_PACKET_MERGE_NONE;
4163 	} else {
4164 		goto out;
4165 	}
4166 
4167 	err = mlx5e_safe_switch_params(priv, &new_params, NULL, NULL, reset);
4168 out:
4169 	mutex_unlock(&priv->state_lock);
4170 	return err;
4171 }
4172 
set_feature_cvlan_filter(struct net_device * netdev,bool enable)4173 static int set_feature_cvlan_filter(struct net_device *netdev, bool enable)
4174 {
4175 	struct mlx5e_priv *priv = netdev_priv(netdev);
4176 
4177 	if (enable)
4178 		mlx5e_enable_cvlan_filter(priv->fs,
4179 					  !!(priv->netdev->flags & IFF_PROMISC));
4180 	else
4181 		mlx5e_disable_cvlan_filter(priv->fs,
4182 					   !!(priv->netdev->flags & IFF_PROMISC));
4183 
4184 	return 0;
4185 }
4186 
set_feature_hw_tc(struct net_device * netdev,bool enable)4187 static int set_feature_hw_tc(struct net_device *netdev, bool enable)
4188 {
4189 	struct mlx5e_priv *priv = netdev_priv(netdev);
4190 	int err = 0;
4191 
4192 #if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
4193 	int tc_flag = mlx5e_is_uplink_rep(priv) ? MLX5_TC_FLAG(ESW_OFFLOAD) :
4194 						  MLX5_TC_FLAG(NIC_OFFLOAD);
4195 	if (!enable && mlx5e_tc_num_filters(priv, tc_flag)) {
4196 		netdev_err(netdev,
4197 			   "Active offloaded tc filters, can't turn hw_tc_offload off\n");
4198 		return -EINVAL;
4199 	}
4200 #endif
4201 
4202 	mutex_lock(&priv->state_lock);
4203 	if (!enable && mlx5e_selq_is_htb_enabled(&priv->selq)) {
4204 		netdev_err(netdev, "Active HTB offload, can't turn hw_tc_offload off\n");
4205 		err = -EINVAL;
4206 	}
4207 	mutex_unlock(&priv->state_lock);
4208 
4209 	return err;
4210 }
4211 
set_feature_rx_all(struct net_device * netdev,bool enable)4212 static int set_feature_rx_all(struct net_device *netdev, bool enable)
4213 {
4214 	struct mlx5e_priv *priv = netdev_priv(netdev);
4215 	struct mlx5_core_dev *mdev = priv->mdev;
4216 
4217 	return mlx5_set_port_fcs(mdev, !enable);
4218 }
4219 
mlx5e_get_def_rx_moderation(u8 cq_period_mode)4220 static struct dim_cq_moder mlx5e_get_def_rx_moderation(u8 cq_period_mode)
4221 {
4222 	return (struct dim_cq_moder) {
4223 		.cq_period_mode = cq_period_mode,
4224 		.pkts = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS,
4225 		.usec = cq_period_mode == DIM_CQ_PERIOD_MODE_START_FROM_CQE ?
4226 				MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE :
4227 				MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC,
4228 	};
4229 }
4230 
mlx5e_reset_rx_moderation(struct dim_cq_moder * cq_moder,u8 cq_period_mode,bool dim_enabled)4231 bool mlx5e_reset_rx_moderation(struct dim_cq_moder *cq_moder, u8 cq_period_mode,
4232 			       bool dim_enabled)
4233 {
4234 	bool reset_needed = cq_moder->cq_period_mode != cq_period_mode;
4235 
4236 	if (dim_enabled)
4237 		*cq_moder = net_dim_get_def_rx_moderation(cq_period_mode);
4238 	else
4239 		*cq_moder = mlx5e_get_def_rx_moderation(cq_period_mode);
4240 
4241 	return reset_needed;
4242 }
4243 
mlx5e_reset_rx_channels_moderation(struct mlx5e_channels * chs,u8 cq_period_mode,bool dim_enabled,bool keep_dim_state)4244 bool mlx5e_reset_rx_channels_moderation(struct mlx5e_channels *chs, u8 cq_period_mode,
4245 					bool dim_enabled, bool keep_dim_state)
4246 {
4247 	bool reset = false;
4248 	int i;
4249 
4250 	for (i = 0; i < chs->num; i++) {
4251 		if (keep_dim_state)
4252 			dim_enabled = !!chs->c[i]->rq.dim;
4253 
4254 		reset |= mlx5e_reset_rx_moderation(&chs->c[i]->rx_cq_moder,
4255 						   cq_period_mode, dim_enabled);
4256 	}
4257 
4258 	return reset;
4259 }
4260 
mlx5e_set_rx_port_ts(struct mlx5_core_dev * mdev,bool enable)4261 static int mlx5e_set_rx_port_ts(struct mlx5_core_dev *mdev, bool enable)
4262 {
4263 	u32 in[MLX5_ST_SZ_DW(pcmr_reg)] = {};
4264 	bool supported, curr_state;
4265 	int err;
4266 
4267 	if (!MLX5_CAP_GEN(mdev, ports_check))
4268 		return 0;
4269 
4270 	err = mlx5_query_ports_check(mdev, in, sizeof(in));
4271 	if (err)
4272 		return err;
4273 
4274 	supported = MLX5_GET(pcmr_reg, in, rx_ts_over_crc_cap);
4275 	curr_state = MLX5_GET(pcmr_reg, in, rx_ts_over_crc);
4276 
4277 	if (!supported || enable == curr_state)
4278 		return 0;
4279 
4280 	MLX5_SET(pcmr_reg, in, local_port, 1);
4281 	MLX5_SET(pcmr_reg, in, rx_ts_over_crc, enable);
4282 
4283 	return mlx5_set_ports_check(mdev, in, sizeof(in));
4284 }
4285 
mlx5e_set_rx_port_ts_wrap(struct mlx5e_priv * priv,void * ctx)4286 static int mlx5e_set_rx_port_ts_wrap(struct mlx5e_priv *priv, void *ctx)
4287 {
4288 	struct mlx5_core_dev *mdev = priv->mdev;
4289 	bool enable = *(bool *)ctx;
4290 
4291 	return mlx5e_set_rx_port_ts(mdev, enable);
4292 }
4293 
set_feature_rx_fcs(struct net_device * netdev,bool enable)4294 static int set_feature_rx_fcs(struct net_device *netdev, bool enable)
4295 {
4296 	struct mlx5e_priv *priv = netdev_priv(netdev);
4297 	struct mlx5e_channels *chs = &priv->channels;
4298 	struct mlx5e_params new_params;
4299 	int err;
4300 	bool rx_ts_over_crc = !enable;
4301 
4302 	mutex_lock(&priv->state_lock);
4303 
4304 	new_params = chs->params;
4305 	new_params.scatter_fcs_en = enable;
4306 	err = mlx5e_safe_switch_params(priv, &new_params, mlx5e_set_rx_port_ts_wrap,
4307 				       &rx_ts_over_crc, true);
4308 	mutex_unlock(&priv->state_lock);
4309 	return err;
4310 }
4311 
set_feature_rx_vlan(struct net_device * netdev,bool enable)4312 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
4313 {
4314 	struct mlx5e_priv *priv = netdev_priv(netdev);
4315 	int err = 0;
4316 
4317 	mutex_lock(&priv->state_lock);
4318 
4319 	mlx5e_fs_set_vlan_strip_disable(priv->fs, !enable);
4320 	priv->channels.params.vlan_strip_disable = !enable;
4321 
4322 	if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
4323 		goto unlock;
4324 
4325 	err = mlx5e_modify_channels_vsd(&priv->channels, !enable);
4326 	if (err) {
4327 		mlx5e_fs_set_vlan_strip_disable(priv->fs, enable);
4328 		priv->channels.params.vlan_strip_disable = enable;
4329 	}
4330 unlock:
4331 	mutex_unlock(&priv->state_lock);
4332 
4333 	return err;
4334 }
4335 
mlx5e_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)4336 int mlx5e_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
4337 {
4338 	struct mlx5e_priv *priv = netdev_priv(dev);
4339 	struct mlx5e_flow_steering *fs = priv->fs;
4340 
4341 	if (mlx5e_is_uplink_rep(priv))
4342 		return 0; /* no vlan table for uplink rep */
4343 
4344 	return mlx5e_fs_vlan_rx_add_vid(fs, dev, proto, vid);
4345 }
4346 
mlx5e_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)4347 int mlx5e_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
4348 {
4349 	struct mlx5e_priv *priv = netdev_priv(dev);
4350 	struct mlx5e_flow_steering *fs = priv->fs;
4351 
4352 	if (mlx5e_is_uplink_rep(priv))
4353 		return 0; /* no vlan table for uplink rep */
4354 
4355 	return mlx5e_fs_vlan_rx_kill_vid(fs, dev, proto, vid);
4356 }
4357 
4358 #ifdef CONFIG_MLX5_EN_ARFS
set_feature_arfs(struct net_device * netdev,bool enable)4359 static int set_feature_arfs(struct net_device *netdev, bool enable)
4360 {
4361 	struct mlx5e_priv *priv = netdev_priv(netdev);
4362 	int err;
4363 
4364 	if (enable)
4365 		err = mlx5e_arfs_enable(priv->fs);
4366 	else
4367 		err = mlx5e_arfs_disable(priv->fs);
4368 
4369 	return err;
4370 }
4371 #endif
4372 
mlx5e_handle_feature(struct net_device * netdev,netdev_features_t * features,netdev_features_t feature,mlx5e_feature_handler feature_handler)4373 static int mlx5e_handle_feature(struct net_device *netdev,
4374 				netdev_features_t *features,
4375 				netdev_features_t feature,
4376 				mlx5e_feature_handler feature_handler)
4377 {
4378 	netdev_features_t changes = *features ^ netdev->features;
4379 	bool enable = !!(*features & feature);
4380 	int err;
4381 
4382 	if (!(changes & feature))
4383 		return 0;
4384 
4385 	err = feature_handler(netdev, enable);
4386 	if (err) {
4387 		MLX5E_SET_FEATURE(features, feature, !enable);
4388 		netdev_err(netdev, "%s feature %pNF failed, err %d\n",
4389 			   enable ? "Enable" : "Disable", &feature, err);
4390 		return err;
4391 	}
4392 
4393 	return 0;
4394 }
4395 
mlx5e_set_xdp_feature(struct net_device * netdev)4396 void mlx5e_set_xdp_feature(struct net_device *netdev)
4397 {
4398 	struct mlx5e_priv *priv = netdev_priv(netdev);
4399 	struct mlx5e_params *params = &priv->channels.params;
4400 	xdp_features_t val;
4401 
4402 	if (!netdev->netdev_ops->ndo_bpf ||
4403 	    params->packet_merge.type != MLX5E_PACKET_MERGE_NONE) {
4404 		xdp_set_features_flag_locked(netdev, 0);
4405 		return;
4406 	}
4407 
4408 	val = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
4409 	      NETDEV_XDP_ACT_XSK_ZEROCOPY |
4410 	      NETDEV_XDP_ACT_RX_SG |
4411 	      NETDEV_XDP_ACT_NDO_XMIT |
4412 	      NETDEV_XDP_ACT_NDO_XMIT_SG;
4413 	xdp_set_features_flag_locked(netdev, val);
4414 }
4415 
mlx5e_set_features(struct net_device * netdev,netdev_features_t features)4416 int mlx5e_set_features(struct net_device *netdev, netdev_features_t features)
4417 {
4418 	netdev_features_t oper_features = features;
4419 	int err = 0;
4420 
4421 #define MLX5E_HANDLE_FEATURE(feature, handler) \
4422 	mlx5e_handle_feature(netdev, &oper_features, feature, handler)
4423 
4424 	if (features & (NETIF_F_GRO_HW | NETIF_F_LRO)) {
4425 		err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXFCS, set_feature_rx_fcs);
4426 		err |= MLX5E_HANDLE_FEATURE(NETIF_F_LRO, set_feature_lro);
4427 		err |= MLX5E_HANDLE_FEATURE(NETIF_F_GRO_HW, set_feature_hw_gro);
4428 	} else {
4429 		err |= MLX5E_HANDLE_FEATURE(NETIF_F_LRO, set_feature_lro);
4430 		err |= MLX5E_HANDLE_FEATURE(NETIF_F_GRO_HW, set_feature_hw_gro);
4431 		err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXFCS, set_feature_rx_fcs);
4432 	}
4433 	err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_FILTER,
4434 				    set_feature_cvlan_filter);
4435 	err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_TC, set_feature_hw_tc);
4436 	err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXALL, set_feature_rx_all);
4437 	err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_RX, set_feature_rx_vlan);
4438 #ifdef CONFIG_MLX5_EN_ARFS
4439 	err |= MLX5E_HANDLE_FEATURE(NETIF_F_NTUPLE, set_feature_arfs);
4440 #endif
4441 	err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_TLS_RX, mlx5e_ktls_set_feature_rx);
4442 
4443 	if (err) {
4444 		netdev->features = oper_features;
4445 		return -EINVAL;
4446 	}
4447 
4448 	/* update XDP supported features */
4449 	mlx5e_set_xdp_feature(netdev);
4450 
4451 	return 0;
4452 }
4453 
mlx5e_fix_uplink_rep_features(struct net_device * netdev,netdev_features_t features)4454 static netdev_features_t mlx5e_fix_uplink_rep_features(struct net_device *netdev,
4455 						       netdev_features_t features)
4456 {
4457 	features &= ~NETIF_F_HW_TLS_RX;
4458 	if (netdev->features & NETIF_F_HW_TLS_RX)
4459 		netdev_warn(netdev, "Disabling hw_tls_rx, not supported in switchdev mode\n");
4460 
4461 	features &= ~NETIF_F_HW_TLS_TX;
4462 	if (netdev->features & NETIF_F_HW_TLS_TX)
4463 		netdev_warn(netdev, "Disabling hw_tls_tx, not supported in switchdev mode\n");
4464 
4465 	features &= ~NETIF_F_NTUPLE;
4466 	if (netdev->features & NETIF_F_NTUPLE)
4467 		netdev_warn(netdev, "Disabling ntuple, not supported in switchdev mode\n");
4468 
4469 	features &= ~NETIF_F_GRO_HW;
4470 	if (netdev->features & NETIF_F_GRO_HW)
4471 		netdev_warn(netdev, "Disabling HW_GRO, not supported in switchdev mode\n");
4472 
4473 	features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
4474 	if (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
4475 		netdev_warn(netdev, "Disabling HW_VLAN CTAG FILTERING, not supported in switchdev mode\n");
4476 
4477 	features &= ~NETIF_F_HW_MACSEC;
4478 	if (netdev->features & NETIF_F_HW_MACSEC)
4479 		netdev_warn(netdev, "Disabling HW MACsec offload, not supported in switchdev mode\n");
4480 
4481 	return features;
4482 }
4483 
mlx5e_fix_features(struct net_device * netdev,netdev_features_t features)4484 static netdev_features_t mlx5e_fix_features(struct net_device *netdev,
4485 					    netdev_features_t features)
4486 {
4487 	struct netdev_config *cfg = netdev->cfg_pending;
4488 	struct mlx5e_priv *priv = netdev_priv(netdev);
4489 	struct mlx5e_vlan_table *vlan;
4490 	struct mlx5e_params *params;
4491 
4492 	if (!netif_device_present(netdev))
4493 		return features;
4494 
4495 	vlan = mlx5e_fs_get_vlan(priv->fs);
4496 	mutex_lock(&priv->state_lock);
4497 	params = &priv->channels.params;
4498 	if (!vlan ||
4499 	    !bitmap_empty(mlx5e_vlan_get_active_svlans(vlan), VLAN_N_VID)) {
4500 		/* HW strips the outer C-tag header, this is a problem
4501 		 * for S-tag traffic.
4502 		 */
4503 		features &= ~NETIF_F_HW_VLAN_CTAG_RX;
4504 		if (!params->vlan_strip_disable)
4505 			netdev_warn(netdev, "Dropping C-tag vlan stripping offload due to S-tag vlan\n");
4506 	}
4507 
4508 	if (!MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
4509 		if (features & NETIF_F_LRO) {
4510 			netdev_warn(netdev, "Disabling LRO, not supported in legacy RQ\n");
4511 			features &= ~NETIF_F_LRO;
4512 		}
4513 		if (features & NETIF_F_GRO_HW) {
4514 			netdev_warn(netdev, "Disabling HW-GRO, not supported in legacy RQ\n");
4515 			features &= ~NETIF_F_GRO_HW;
4516 		}
4517 	}
4518 
4519 	if (params->xdp_prog) {
4520 		if (features & NETIF_F_LRO) {
4521 			netdev_warn(netdev, "LRO is incompatible with XDP\n");
4522 			features &= ~NETIF_F_LRO;
4523 		}
4524 		if (features & NETIF_F_GRO_HW) {
4525 			netdev_warn(netdev, "HW GRO is incompatible with XDP\n");
4526 			features &= ~NETIF_F_GRO_HW;
4527 		}
4528 	}
4529 
4530 	if (priv->xsk.refcnt) {
4531 		if (features & NETIF_F_LRO) {
4532 			netdev_warn(netdev, "LRO is incompatible with AF_XDP (%u XSKs are active)\n",
4533 				    priv->xsk.refcnt);
4534 			features &= ~NETIF_F_LRO;
4535 		}
4536 		if (features & NETIF_F_GRO_HW) {
4537 			netdev_warn(netdev, "HW GRO is incompatible with AF_XDP (%u XSKs are active)\n",
4538 				    priv->xsk.refcnt);
4539 			features &= ~NETIF_F_GRO_HW;
4540 		}
4541 	}
4542 
4543 	if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
4544 		features &= ~NETIF_F_RXHASH;
4545 		if (netdev->features & NETIF_F_RXHASH)
4546 			netdev_warn(netdev, "Disabling rxhash, not supported when CQE compress is active\n");
4547 
4548 		if (features & NETIF_F_GRO_HW) {
4549 			netdev_warn(netdev, "Disabling HW-GRO, not supported when CQE compress is active\n");
4550 			features &= ~NETIF_F_GRO_HW;
4551 		}
4552 	}
4553 
4554 	/* The header-data split ring param requires HW GRO to stay enabled. */
4555 	if (cfg && cfg->hds_config == ETHTOOL_TCP_DATA_SPLIT_ENABLED &&
4556 	    !(features & NETIF_F_GRO_HW)) {
4557 		netdev_warn(netdev, "Keeping HW-GRO enabled, TCP header-data split depends on it\n");
4558 		features |= NETIF_F_GRO_HW;
4559 	}
4560 
4561 	if (mlx5e_is_uplink_rep(priv)) {
4562 		features = mlx5e_fix_uplink_rep_features(netdev, features);
4563 		netdev->netns_immutable = true;
4564 	} else {
4565 		netdev->netns_immutable = false;
4566 	}
4567 
4568 	mutex_unlock(&priv->state_lock);
4569 
4570 	return features;
4571 }
4572 
mlx5e_xsk_validate_mtu(struct net_device * netdev,struct mlx5e_channels * chs,struct mlx5e_params * new_params,struct mlx5_core_dev * mdev)4573 static bool mlx5e_xsk_validate_mtu(struct net_device *netdev,
4574 				   struct mlx5e_channels *chs,
4575 				   struct mlx5e_params *new_params,
4576 				   struct mlx5_core_dev *mdev)
4577 {
4578 	u16 ix;
4579 
4580 	for (ix = 0; ix < chs->params.num_channels; ix++) {
4581 		struct xsk_buff_pool *xsk_pool =
4582 			mlx5e_xsk_get_pool(&chs->params, chs->params.xsk, ix);
4583 		struct mlx5e_xsk_param xsk;
4584 		int max_xdp_mtu;
4585 
4586 		if (!xsk_pool)
4587 			continue;
4588 
4589 		mlx5e_build_xsk_param(xsk_pool, &xsk);
4590 		max_xdp_mtu = mlx5e_xdp_max_mtu(new_params, &xsk);
4591 
4592 		/* Validate XSK params and XDP MTU in advance */
4593 		if (!mlx5e_validate_xsk_param(new_params, &xsk, mdev) ||
4594 		    new_params->sw_mtu > max_xdp_mtu) {
4595 			u32 hr = mlx5e_get_linear_rq_headroom(new_params, &xsk);
4596 			int max_mtu_frame, max_mtu_page, max_mtu;
4597 
4598 			/* Two criteria must be met:
4599 			 * 1. HW MTU + all headrooms <= XSK frame size.
4600 			 * 2. Size of SKBs allocated on XDP_PASS <= PAGE_SIZE.
4601 			 */
4602 			max_mtu_frame = MLX5E_HW2SW_MTU(new_params, xsk.chunk_size - hr);
4603 			max_mtu_page = MLX5E_HW2SW_MTU(new_params, SKB_MAX_HEAD(0));
4604 			max_mtu = min3(max_mtu_frame, max_mtu_page, max_xdp_mtu);
4605 
4606 			netdev_err(netdev, "MTU %d is too big for an XSK running on channel %u or its redirection XDP program. Try MTU <= %d\n",
4607 				   new_params->sw_mtu, ix, max_mtu);
4608 			return false;
4609 		}
4610 	}
4611 
4612 	return true;
4613 }
4614 
mlx5e_params_validate_xdp(struct net_device * netdev,struct mlx5_core_dev * mdev,struct mlx5e_params * params)4615 static bool mlx5e_params_validate_xdp(struct net_device *netdev,
4616 				      struct mlx5_core_dev *mdev,
4617 				      struct mlx5e_params *params)
4618 {
4619 	bool is_linear;
4620 
4621 	/* No XSK params: AF_XDP can't be enabled yet at the point of setting
4622 	 * the XDP program.
4623 	 */
4624 	is_linear = params->rq_wq_type == MLX5_WQ_TYPE_CYCLIC ?
4625 		mlx5e_rx_is_linear_skb(mdev, params, NULL) :
4626 		mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL);
4627 
4628 	if (!is_linear) {
4629 		if (!params->xdp_prog->aux->xdp_has_frags) {
4630 			netdev_warn(netdev, "MTU(%d) > %d, too big for an XDP program not aware of multi buffer\n",
4631 				    params->sw_mtu,
4632 				    mlx5e_xdp_max_mtu(params, NULL));
4633 			return false;
4634 		}
4635 		if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ &&
4636 		    !mlx5e_verify_params_rx_mpwqe_strides(mdev, params, NULL)) {
4637 			netdev_warn(netdev, "XDP is not allowed with striding RQ and MTU(%d) > %d\n",
4638 				    params->sw_mtu,
4639 				    mlx5e_xdp_max_mtu(params, NULL));
4640 			return false;
4641 		}
4642 	}
4643 
4644 	return true;
4645 }
4646 
mlx5e_change_mtu(struct net_device * netdev,int new_mtu,mlx5e_fp_preactivate preactivate)4647 int mlx5e_change_mtu(struct net_device *netdev, int new_mtu,
4648 		     mlx5e_fp_preactivate preactivate)
4649 {
4650 	struct mlx5e_priv *priv = netdev_priv(netdev);
4651 	struct mlx5e_params new_params;
4652 	struct mlx5e_params *params;
4653 	bool reset = true;
4654 	int err = 0;
4655 
4656 	mutex_lock(&priv->state_lock);
4657 
4658 	params = &priv->channels.params;
4659 
4660 	new_params = *params;
4661 	new_params.sw_mtu = new_mtu;
4662 	err = mlx5e_validate_params(priv->mdev, &new_params);
4663 	if (err)
4664 		goto out;
4665 
4666 	if (new_params.xdp_prog && !mlx5e_params_validate_xdp(netdev, priv->mdev,
4667 							      &new_params)) {
4668 		err = -EINVAL;
4669 		goto out;
4670 	}
4671 
4672 	if (priv->xsk.refcnt &&
4673 	    !mlx5e_xsk_validate_mtu(netdev, &priv->channels,
4674 				    &new_params, priv->mdev)) {
4675 		err = -EINVAL;
4676 		goto out;
4677 	}
4678 
4679 	if (params->packet_merge.type == MLX5E_PACKET_MERGE_LRO)
4680 		reset = false;
4681 
4682 	if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ &&
4683 	    params->packet_merge.type != MLX5E_PACKET_MERGE_SHAMPO) {
4684 		bool is_linear_old = mlx5e_rx_mpwqe_is_linear_skb(priv->mdev, params, NULL);
4685 		bool is_linear_new = mlx5e_rx_mpwqe_is_linear_skb(priv->mdev,
4686 								  &new_params, NULL);
4687 		u8 sz_old = mlx5e_mpwqe_get_log_rq_size(priv->mdev, params, NULL);
4688 		u8 sz_new = mlx5e_mpwqe_get_log_rq_size(priv->mdev, &new_params, NULL);
4689 
4690 		/* Always reset in linear mode - hw_mtu is used in data path.
4691 		 * Check that the mode was non-linear and didn't change.
4692 		 * If XSK is active, XSK RQs are linear.
4693 		 * Reset if the RQ size changed, even if it's non-linear.
4694 		 */
4695 		if (!is_linear_old && !is_linear_new && !priv->xsk.refcnt &&
4696 		    sz_old == sz_new)
4697 			reset = false;
4698 	}
4699 
4700 	err = mlx5e_safe_switch_params(priv, &new_params, preactivate, NULL, reset);
4701 
4702 out:
4703 	WRITE_ONCE(netdev->mtu, params->sw_mtu);
4704 	mutex_unlock(&priv->state_lock);
4705 
4706 	if (!err)
4707 		netdev_update_features(netdev);
4708 
4709 	return err;
4710 }
4711 
mlx5e_change_nic_mtu(struct net_device * netdev,int new_mtu)4712 static int mlx5e_change_nic_mtu(struct net_device *netdev, int new_mtu)
4713 {
4714 	return mlx5e_change_mtu(netdev, new_mtu, mlx5e_set_dev_port_mtu_ctx);
4715 }
4716 
mlx5e_ptp_rx_manage_fs_ctx(struct mlx5e_priv * priv,void * ctx)4717 int mlx5e_ptp_rx_manage_fs_ctx(struct mlx5e_priv *priv, void *ctx)
4718 {
4719 	bool set  = *(bool *)ctx;
4720 
4721 	return mlx5e_ptp_rx_manage_fs(priv, set);
4722 }
4723 
mlx5e_hwstamp_config_no_ptp_rx(struct mlx5e_priv * priv,bool rx_filter)4724 static int mlx5e_hwstamp_config_no_ptp_rx(struct mlx5e_priv *priv, bool rx_filter)
4725 {
4726 	bool rx_cqe_compress_def = priv->channels.params.rx_cqe_compress_def;
4727 	int err;
4728 
4729 	if (!rx_filter)
4730 		/* Reset CQE compression to Admin default */
4731 		return mlx5e_modify_rx_cqe_compression_locked(priv, rx_cqe_compress_def, false);
4732 
4733 	if (!MLX5E_GET_PFLAG(&priv->channels.params, MLX5E_PFLAG_RX_CQE_COMPRESS))
4734 		return 0;
4735 
4736 	/* Disable CQE compression */
4737 	netdev_warn(priv->netdev, "Disabling RX cqe compression\n");
4738 	err = mlx5e_modify_rx_cqe_compression_locked(priv, false, true);
4739 	if (err)
4740 		netdev_err(priv->netdev, "Failed disabling cqe compression err=%d\n", err);
4741 
4742 	return err;
4743 }
4744 
mlx5e_hwstamp_config_ptp_rx(struct mlx5e_priv * priv,bool ptp_rx)4745 static int mlx5e_hwstamp_config_ptp_rx(struct mlx5e_priv *priv, bool ptp_rx)
4746 {
4747 	struct mlx5e_params new_params;
4748 
4749 	if (ptp_rx == priv->channels.params.ptp_rx)
4750 		return 0;
4751 
4752 	new_params = priv->channels.params;
4753 	new_params.ptp_rx = ptp_rx;
4754 	return mlx5e_safe_switch_params(priv, &new_params, mlx5e_ptp_rx_manage_fs_ctx,
4755 					&new_params.ptp_rx, true);
4756 }
4757 
mlx5e_hwstamp_set(struct mlx5e_priv * priv,struct ifreq * ifr)4758 int mlx5e_hwstamp_set(struct mlx5e_priv *priv, struct ifreq *ifr)
4759 {
4760 	struct hwtstamp_config config;
4761 	bool rx_cqe_compress_def;
4762 	bool ptp_rx;
4763 	int err;
4764 
4765 	if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz) ||
4766 	    (mlx5_clock_get_ptp_index(priv->mdev) == -1))
4767 		return -EOPNOTSUPP;
4768 
4769 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
4770 		return -EFAULT;
4771 
4772 	/* TX HW timestamp */
4773 	switch (config.tx_type) {
4774 	case HWTSTAMP_TX_OFF:
4775 	case HWTSTAMP_TX_ON:
4776 		break;
4777 	default:
4778 		return -ERANGE;
4779 	}
4780 
4781 	mutex_lock(&priv->state_lock);
4782 	rx_cqe_compress_def = priv->channels.params.rx_cqe_compress_def;
4783 
4784 	/* RX HW timestamp */
4785 	switch (config.rx_filter) {
4786 	case HWTSTAMP_FILTER_NONE:
4787 		ptp_rx = false;
4788 		break;
4789 	case HWTSTAMP_FILTER_ALL:
4790 	case HWTSTAMP_FILTER_SOME:
4791 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
4792 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
4793 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
4794 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
4795 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
4796 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
4797 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
4798 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
4799 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
4800 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
4801 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
4802 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
4803 	case HWTSTAMP_FILTER_NTP_ALL:
4804 		config.rx_filter = HWTSTAMP_FILTER_ALL;
4805 		/* ptp_rx is set if both HW TS is set and CQE
4806 		 * compression is set
4807 		 */
4808 		ptp_rx = rx_cqe_compress_def;
4809 		break;
4810 	default:
4811 		err = -ERANGE;
4812 		goto err_unlock;
4813 	}
4814 
4815 	if (!mlx5e_profile_feature_cap(priv->profile, PTP_RX))
4816 		err = mlx5e_hwstamp_config_no_ptp_rx(priv,
4817 						     config.rx_filter != HWTSTAMP_FILTER_NONE);
4818 	else
4819 		err = mlx5e_hwstamp_config_ptp_rx(priv, ptp_rx);
4820 	if (err)
4821 		goto err_unlock;
4822 
4823 	memcpy(&priv->tstamp, &config, sizeof(config));
4824 	mutex_unlock(&priv->state_lock);
4825 
4826 	/* might need to fix some features */
4827 	netdev_update_features(priv->netdev);
4828 
4829 	return copy_to_user(ifr->ifr_data, &config,
4830 			    sizeof(config)) ? -EFAULT : 0;
4831 err_unlock:
4832 	mutex_unlock(&priv->state_lock);
4833 	return err;
4834 }
4835 
mlx5e_hwstamp_get(struct mlx5e_priv * priv,struct ifreq * ifr)4836 int mlx5e_hwstamp_get(struct mlx5e_priv *priv, struct ifreq *ifr)
4837 {
4838 	struct hwtstamp_config *cfg = &priv->tstamp;
4839 
4840 	if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
4841 		return -EOPNOTSUPP;
4842 
4843 	return copy_to_user(ifr->ifr_data, cfg, sizeof(*cfg)) ? -EFAULT : 0;
4844 }
4845 
mlx5e_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)4846 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4847 {
4848 	struct mlx5e_priv *priv = netdev_priv(dev);
4849 
4850 	switch (cmd) {
4851 	case SIOCSHWTSTAMP:
4852 		return mlx5e_hwstamp_set(priv, ifr);
4853 	case SIOCGHWTSTAMP:
4854 		return mlx5e_hwstamp_get(priv, ifr);
4855 	default:
4856 		return -EOPNOTSUPP;
4857 	}
4858 }
4859 
4860 #ifdef CONFIG_MLX5_ESWITCH
mlx5e_set_vf_mac(struct net_device * dev,int vf,u8 * mac)4861 int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
4862 {
4863 	struct mlx5e_priv *priv = netdev_priv(dev);
4864 	struct mlx5_core_dev *mdev = priv->mdev;
4865 
4866 	return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
4867 }
4868 
mlx5e_set_vf_vlan(struct net_device * dev,int vf,u16 vlan,u8 qos,__be16 vlan_proto)4869 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
4870 			     __be16 vlan_proto)
4871 {
4872 	struct mlx5e_priv *priv = netdev_priv(dev);
4873 	struct mlx5_core_dev *mdev = priv->mdev;
4874 
4875 	if (vlan_proto != htons(ETH_P_8021Q))
4876 		return -EPROTONOSUPPORT;
4877 
4878 	return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
4879 					   vlan, qos);
4880 }
4881 
mlx5e_set_vf_spoofchk(struct net_device * dev,int vf,bool setting)4882 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
4883 {
4884 	struct mlx5e_priv *priv = netdev_priv(dev);
4885 	struct mlx5_core_dev *mdev = priv->mdev;
4886 
4887 	return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
4888 }
4889 
mlx5e_set_vf_trust(struct net_device * dev,int vf,bool setting)4890 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
4891 {
4892 	struct mlx5e_priv *priv = netdev_priv(dev);
4893 	struct mlx5_core_dev *mdev = priv->mdev;
4894 
4895 	return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
4896 }
4897 
mlx5e_set_vf_rate(struct net_device * dev,int vf,int min_tx_rate,int max_tx_rate)4898 int mlx5e_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
4899 		      int max_tx_rate)
4900 {
4901 	struct mlx5e_priv *priv = netdev_priv(dev);
4902 	struct mlx5_core_dev *mdev = priv->mdev;
4903 
4904 	return mlx5_eswitch_set_vport_rate(mdev->priv.eswitch, vf + 1,
4905 					   max_tx_rate, min_tx_rate);
4906 }
4907 
mlx5_vport_link2ifla(u8 esw_link)4908 static int mlx5_vport_link2ifla(u8 esw_link)
4909 {
4910 	switch (esw_link) {
4911 	case MLX5_VPORT_ADMIN_STATE_DOWN:
4912 		return IFLA_VF_LINK_STATE_DISABLE;
4913 	case MLX5_VPORT_ADMIN_STATE_UP:
4914 		return IFLA_VF_LINK_STATE_ENABLE;
4915 	}
4916 	return IFLA_VF_LINK_STATE_AUTO;
4917 }
4918 
mlx5_ifla_link2vport(u8 ifla_link)4919 static int mlx5_ifla_link2vport(u8 ifla_link)
4920 {
4921 	switch (ifla_link) {
4922 	case IFLA_VF_LINK_STATE_DISABLE:
4923 		return MLX5_VPORT_ADMIN_STATE_DOWN;
4924 	case IFLA_VF_LINK_STATE_ENABLE:
4925 		return MLX5_VPORT_ADMIN_STATE_UP;
4926 	}
4927 	return MLX5_VPORT_ADMIN_STATE_AUTO;
4928 }
4929 
mlx5e_set_vf_link_state(struct net_device * dev,int vf,int link_state)4930 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
4931 				   int link_state)
4932 {
4933 	struct mlx5e_priv *priv = netdev_priv(dev);
4934 	struct mlx5_core_dev *mdev = priv->mdev;
4935 
4936 	if (mlx5e_is_uplink_rep(priv))
4937 		return -EOPNOTSUPP;
4938 
4939 	return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
4940 					    mlx5_ifla_link2vport(link_state));
4941 }
4942 
mlx5e_get_vf_config(struct net_device * dev,int vf,struct ifla_vf_info * ivi)4943 int mlx5e_get_vf_config(struct net_device *dev,
4944 			int vf, struct ifla_vf_info *ivi)
4945 {
4946 	struct mlx5e_priv *priv = netdev_priv(dev);
4947 	struct mlx5_core_dev *mdev = priv->mdev;
4948 	int err;
4949 
4950 	if (!netif_device_present(dev))
4951 		return -EOPNOTSUPP;
4952 
4953 	err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
4954 	if (err)
4955 		return err;
4956 	ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
4957 	return 0;
4958 }
4959 
mlx5e_get_vf_stats(struct net_device * dev,int vf,struct ifla_vf_stats * vf_stats)4960 int mlx5e_get_vf_stats(struct net_device *dev,
4961 		       int vf, struct ifla_vf_stats *vf_stats)
4962 {
4963 	struct mlx5e_priv *priv = netdev_priv(dev);
4964 	struct mlx5_core_dev *mdev = priv->mdev;
4965 
4966 	return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
4967 					    vf_stats);
4968 }
4969 
4970 static bool
mlx5e_has_offload_stats(const struct net_device * dev,int attr_id)4971 mlx5e_has_offload_stats(const struct net_device *dev, int attr_id)
4972 {
4973 	struct mlx5e_priv *priv = netdev_priv(dev);
4974 
4975 	if (!netif_device_present(dev))
4976 		return false;
4977 
4978 	if (!mlx5e_is_uplink_rep(priv))
4979 		return false;
4980 
4981 	return mlx5e_rep_has_offload_stats(dev, attr_id);
4982 }
4983 
4984 static int
mlx5e_get_offload_stats(int attr_id,const struct net_device * dev,void * sp)4985 mlx5e_get_offload_stats(int attr_id, const struct net_device *dev,
4986 			void *sp)
4987 {
4988 	struct mlx5e_priv *priv = netdev_priv(dev);
4989 
4990 	if (!mlx5e_is_uplink_rep(priv))
4991 		return -EOPNOTSUPP;
4992 
4993 	return mlx5e_rep_get_offload_stats(attr_id, dev, sp);
4994 }
4995 #endif
4996 
mlx5e_tunnel_proto_supported_tx(struct mlx5_core_dev * mdev,u8 proto_type)4997 static bool mlx5e_tunnel_proto_supported_tx(struct mlx5_core_dev *mdev, u8 proto_type)
4998 {
4999 	switch (proto_type) {
5000 	case IPPROTO_GRE:
5001 		return MLX5_CAP_ETH(mdev, tunnel_stateless_gre);
5002 	case IPPROTO_IPIP:
5003 	case IPPROTO_IPV6:
5004 		return (MLX5_CAP_ETH(mdev, tunnel_stateless_ip_over_ip) ||
5005 			MLX5_CAP_ETH(mdev, tunnel_stateless_ip_over_ip_tx));
5006 	default:
5007 		return false;
5008 	}
5009 }
5010 
mlx5e_gre_tunnel_inner_proto_offload_supported(struct mlx5_core_dev * mdev,struct sk_buff * skb)5011 static bool mlx5e_gre_tunnel_inner_proto_offload_supported(struct mlx5_core_dev *mdev,
5012 							   struct sk_buff *skb)
5013 {
5014 	switch (skb->inner_protocol) {
5015 	case htons(ETH_P_IP):
5016 	case htons(ETH_P_IPV6):
5017 	case htons(ETH_P_TEB):
5018 		return true;
5019 	case htons(ETH_P_MPLS_UC):
5020 	case htons(ETH_P_MPLS_MC):
5021 		return MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_gre);
5022 	}
5023 	return false;
5024 }
5025 
mlx5e_tunnel_features_check(struct mlx5e_priv * priv,struct sk_buff * skb,netdev_features_t features)5026 static netdev_features_t mlx5e_tunnel_features_check(struct mlx5e_priv *priv,
5027 						     struct sk_buff *skb,
5028 						     netdev_features_t features)
5029 {
5030 	unsigned int offset = 0;
5031 	struct udphdr *udph;
5032 	u8 proto;
5033 	u16 port;
5034 
5035 	switch (vlan_get_protocol(skb)) {
5036 	case htons(ETH_P_IP):
5037 		proto = ip_hdr(skb)->protocol;
5038 		break;
5039 	case htons(ETH_P_IPV6):
5040 		proto = ipv6_find_hdr(skb, &offset, -1, NULL, NULL);
5041 		break;
5042 	default:
5043 		goto out;
5044 	}
5045 
5046 	switch (proto) {
5047 	case IPPROTO_GRE:
5048 		if (mlx5e_gre_tunnel_inner_proto_offload_supported(priv->mdev, skb))
5049 			return features;
5050 		break;
5051 	case IPPROTO_IPIP:
5052 	case IPPROTO_IPV6:
5053 		if (mlx5e_tunnel_proto_supported_tx(priv->mdev, IPPROTO_IPIP))
5054 			return features;
5055 		break;
5056 	case IPPROTO_UDP:
5057 		udph = udp_hdr(skb);
5058 		port = be16_to_cpu(udph->dest);
5059 
5060 		/* Verify if UDP port is being offloaded by HW */
5061 		if (mlx5_vxlan_lookup_port(priv->mdev->vxlan, port))
5062 			return vxlan_features_check(skb, features);
5063 
5064 #if IS_ENABLED(CONFIG_GENEVE)
5065 		/* Support Geneve offload for default UDP port */
5066 		if (port == GENEVE_UDP_PORT && mlx5_geneve_tx_allowed(priv->mdev))
5067 			return features;
5068 #endif
5069 		break;
5070 #ifdef CONFIG_MLX5_EN_IPSEC
5071 	case IPPROTO_ESP:
5072 		return mlx5e_ipsec_feature_check(skb, features);
5073 #endif
5074 	}
5075 
5076 out:
5077 	/* Disable CSUM and GSO if skb cannot be offloaded by HW */
5078 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
5079 }
5080 
mlx5e_features_check(struct sk_buff * skb,struct net_device * netdev,netdev_features_t features)5081 netdev_features_t mlx5e_features_check(struct sk_buff *skb,
5082 				       struct net_device *netdev,
5083 				       netdev_features_t features)
5084 {
5085 	struct mlx5e_priv *priv = netdev_priv(netdev);
5086 
5087 	features = vlan_features_check(skb, features);
5088 
5089 	/* Validate if the tunneled packet is being offloaded by HW */
5090 	if (skb->encapsulation &&
5091 	    (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
5092 		return mlx5e_tunnel_features_check(priv, skb, features);
5093 
5094 	return features;
5095 }
5096 
mlx5e_tx_timeout_work(struct work_struct * work)5097 static void mlx5e_tx_timeout_work(struct work_struct *work)
5098 {
5099 	struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
5100 					       tx_timeout_work);
5101 	struct net_device *netdev = priv->netdev;
5102 	int i;
5103 
5104 	/* Recovering the TX queues implies re-enabling NAPI, which requires
5105 	 * the netdev instance lock.
5106 	 * However, channel closing flows have to wait for this work to finish
5107 	 * while holding the same lock. So either get the lock or find that
5108 	 * channels are being closed for other reason and this work is not
5109 	 * relevant anymore.
5110 	 */
5111 	while (!netdev_trylock(netdev)) {
5112 		if (!test_bit(MLX5E_STATE_CHANNELS_ACTIVE, &priv->state))
5113 			return;
5114 		msleep(20);
5115 	}
5116 
5117 	for (i = 0; i < netdev->real_num_tx_queues; i++) {
5118 		struct netdev_queue *dev_queue =
5119 			netdev_get_tx_queue(netdev, i);
5120 		struct mlx5e_txqsq *sq = priv->txq2sq[i];
5121 
5122 		if (!netif_xmit_stopped(dev_queue))
5123 			continue;
5124 
5125 		if (mlx5e_reporter_tx_timeout(sq))
5126 		/* break if tried to reopened channels */
5127 			break;
5128 	}
5129 
5130 	netdev_unlock(netdev);
5131 }
5132 
mlx5e_tx_timeout(struct net_device * dev,unsigned int txqueue)5133 static void mlx5e_tx_timeout(struct net_device *dev, unsigned int txqueue)
5134 {
5135 	struct mlx5e_priv *priv = netdev_priv(dev);
5136 
5137 	netdev_err(dev, "TX timeout detected\n");
5138 	queue_work(priv->wq, &priv->tx_timeout_work);
5139 }
5140 
mlx5e_xdp_allowed(struct net_device * netdev,struct mlx5_core_dev * mdev,struct mlx5e_params * params)5141 static int mlx5e_xdp_allowed(struct net_device *netdev, struct mlx5_core_dev *mdev,
5142 			     struct mlx5e_params *params)
5143 {
5144 	if (params->packet_merge.type != MLX5E_PACKET_MERGE_NONE) {
5145 		netdev_warn(netdev, "can't set XDP while HW-GRO/LRO is on, disable them first\n");
5146 		return -EINVAL;
5147 	}
5148 
5149 	if (!mlx5e_params_validate_xdp(netdev, mdev, params))
5150 		return -EINVAL;
5151 
5152 	return 0;
5153 }
5154 
mlx5e_rq_replace_xdp_prog(struct mlx5e_rq * rq,struct bpf_prog * prog)5155 static void mlx5e_rq_replace_xdp_prog(struct mlx5e_rq *rq, struct bpf_prog *prog)
5156 {
5157 	struct bpf_prog *old_prog;
5158 
5159 	old_prog = rcu_replace_pointer(rq->xdp_prog, prog,
5160 				       lockdep_is_held(&rq->priv->state_lock));
5161 	if (old_prog)
5162 		bpf_prog_put(old_prog);
5163 }
5164 
mlx5e_xdp_set(struct net_device * netdev,struct bpf_prog * prog)5165 static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog)
5166 {
5167 	struct mlx5e_priv *priv = netdev_priv(netdev);
5168 	struct mlx5e_params new_params;
5169 	struct bpf_prog *old_prog;
5170 	int err = 0;
5171 	bool reset;
5172 	int i;
5173 
5174 	mutex_lock(&priv->state_lock);
5175 
5176 	new_params = priv->channels.params;
5177 	new_params.xdp_prog = prog;
5178 
5179 	if (prog) {
5180 		err = mlx5e_xdp_allowed(netdev, priv->mdev, &new_params);
5181 		if (err)
5182 			goto unlock;
5183 	}
5184 
5185 	/* no need for full reset when exchanging programs */
5186 	reset = (!priv->channels.params.xdp_prog || !prog);
5187 
5188 	old_prog = priv->channels.params.xdp_prog;
5189 
5190 	err = mlx5e_safe_switch_params(priv, &new_params, NULL, NULL, reset);
5191 	if (err)
5192 		goto unlock;
5193 
5194 	if (old_prog)
5195 		bpf_prog_put(old_prog);
5196 
5197 	if (!test_bit(MLX5E_STATE_OPENED, &priv->state) || reset)
5198 		goto unlock;
5199 
5200 	/* exchanging programs w/o reset, we update ref counts on behalf
5201 	 * of the channels RQs here.
5202 	 */
5203 	bpf_prog_add(prog, priv->channels.num);
5204 	for (i = 0; i < priv->channels.num; i++) {
5205 		struct mlx5e_channel *c = priv->channels.c[i];
5206 
5207 		mlx5e_rq_replace_xdp_prog(&c->rq, prog);
5208 		if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state)) {
5209 			bpf_prog_inc(prog);
5210 			mlx5e_rq_replace_xdp_prog(&c->xskrq, prog);
5211 		}
5212 	}
5213 
5214 unlock:
5215 	mutex_unlock(&priv->state_lock);
5216 
5217 	/* Need to fix some features. */
5218 	if (!err)
5219 		netdev_update_features(netdev);
5220 
5221 	return err;
5222 }
5223 
mlx5e_xdp(struct net_device * dev,struct netdev_bpf * xdp)5224 static int mlx5e_xdp(struct net_device *dev, struct netdev_bpf *xdp)
5225 {
5226 	switch (xdp->command) {
5227 	case XDP_SETUP_PROG:
5228 		return mlx5e_xdp_set(dev, xdp->prog);
5229 	case XDP_SETUP_XSK_POOL:
5230 		return mlx5e_xsk_setup_pool(dev, xdp->xsk.pool,
5231 					    xdp->xsk.queue_id);
5232 	default:
5233 		return -EINVAL;
5234 	}
5235 }
5236 
5237 #ifdef CONFIG_MLX5_ESWITCH
mlx5e_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 filter_mask,int nlflags)5238 static int mlx5e_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
5239 				struct net_device *dev, u32 filter_mask,
5240 				int nlflags)
5241 {
5242 	struct mlx5e_priv *priv = netdev_priv(dev);
5243 	struct mlx5_core_dev *mdev = priv->mdev;
5244 	u8 mode, setting;
5245 
5246 	if (mlx5_eswitch_get_vepa(mdev->priv.eswitch, &setting))
5247 		return -EOPNOTSUPP;
5248 	mode = setting ? BRIDGE_MODE_VEPA : BRIDGE_MODE_VEB;
5249 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev,
5250 				       mode,
5251 				       0, 0, nlflags, filter_mask, NULL);
5252 }
5253 
mlx5e_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)5254 static int mlx5e_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
5255 				u16 flags, struct netlink_ext_ack *extack)
5256 {
5257 	struct mlx5e_priv *priv = netdev_priv(dev);
5258 	struct mlx5_core_dev *mdev = priv->mdev;
5259 	struct nlattr *attr, *br_spec;
5260 	u16 mode = BRIDGE_MODE_UNDEF;
5261 	u8 setting;
5262 	int rem;
5263 
5264 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5265 	if (!br_spec)
5266 		return -EINVAL;
5267 
5268 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
5269 		mode = nla_get_u16(attr);
5270 		if (mode > BRIDGE_MODE_VEPA)
5271 			return -EINVAL;
5272 
5273 		break;
5274 	}
5275 
5276 	if (mode == BRIDGE_MODE_UNDEF)
5277 		return -EINVAL;
5278 
5279 	setting = (mode == BRIDGE_MODE_VEPA) ?  1 : 0;
5280 	return mlx5_eswitch_set_vepa(mdev->priv.eswitch, setting);
5281 }
5282 #endif
5283 
5284 const struct net_device_ops mlx5e_netdev_ops = {
5285 	.ndo_open                = mlx5e_open,
5286 	.ndo_stop                = mlx5e_close,
5287 	.ndo_start_xmit          = mlx5e_xmit,
5288 	.ndo_setup_tc            = mlx5e_setup_tc,
5289 	.ndo_select_queue        = mlx5e_select_queue,
5290 	.ndo_get_stats64         = mlx5e_get_stats,
5291 	.ndo_set_rx_mode         = mlx5e_set_rx_mode,
5292 	.ndo_set_mac_address     = mlx5e_set_mac,
5293 	.ndo_vlan_rx_add_vid     = mlx5e_vlan_rx_add_vid,
5294 	.ndo_vlan_rx_kill_vid    = mlx5e_vlan_rx_kill_vid,
5295 	.ndo_set_features        = mlx5e_set_features,
5296 	.ndo_fix_features        = mlx5e_fix_features,
5297 	.ndo_change_mtu          = mlx5e_change_nic_mtu,
5298 	.ndo_eth_ioctl            = mlx5e_ioctl,
5299 	.ndo_set_tx_maxrate      = mlx5e_set_tx_maxrate,
5300 	.ndo_features_check      = mlx5e_features_check,
5301 	.ndo_tx_timeout          = mlx5e_tx_timeout,
5302 	.ndo_bpf		 = mlx5e_xdp,
5303 	.ndo_xdp_xmit            = mlx5e_xdp_xmit,
5304 	.ndo_xsk_wakeup          = mlx5e_xsk_wakeup,
5305 #ifdef CONFIG_MLX5_EN_ARFS
5306 	.ndo_rx_flow_steer	 = mlx5e_rx_flow_steer,
5307 #endif
5308 #ifdef CONFIG_MLX5_ESWITCH
5309 	.ndo_bridge_setlink      = mlx5e_bridge_setlink,
5310 	.ndo_bridge_getlink      = mlx5e_bridge_getlink,
5311 
5312 	/* SRIOV E-Switch NDOs */
5313 	.ndo_set_vf_mac          = mlx5e_set_vf_mac,
5314 	.ndo_set_vf_vlan         = mlx5e_set_vf_vlan,
5315 	.ndo_set_vf_spoofchk     = mlx5e_set_vf_spoofchk,
5316 	.ndo_set_vf_trust        = mlx5e_set_vf_trust,
5317 	.ndo_set_vf_rate         = mlx5e_set_vf_rate,
5318 	.ndo_get_vf_config       = mlx5e_get_vf_config,
5319 	.ndo_set_vf_link_state   = mlx5e_set_vf_link_state,
5320 	.ndo_get_vf_stats        = mlx5e_get_vf_stats,
5321 	.ndo_has_offload_stats   = mlx5e_has_offload_stats,
5322 	.ndo_get_offload_stats   = mlx5e_get_offload_stats,
5323 #endif
5324 };
5325 
mlx5e_build_nic_params(struct mlx5e_priv * priv,struct mlx5e_xsk * xsk,u16 mtu)5326 void mlx5e_build_nic_params(struct mlx5e_priv *priv, struct mlx5e_xsk *xsk, u16 mtu)
5327 {
5328 	struct mlx5e_params *params = &priv->channels.params;
5329 	struct mlx5_core_dev *mdev = priv->mdev;
5330 
5331 	params->sw_mtu = mtu;
5332 	params->hard_mtu = MLX5E_ETH_HARD_MTU;
5333 	params->num_channels = min_t(unsigned int, MLX5E_MAX_NUM_CHANNELS / 2,
5334 				     priv->max_nch);
5335 	mlx5e_params_mqprio_reset(params);
5336 
5337 	/* SQ */
5338 	params->log_sq_size = is_kdump_kernel() ?
5339 		MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE :
5340 		MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
5341 	MLX5E_SET_PFLAG(params, MLX5E_PFLAG_SKB_TX_MPWQE, mlx5e_tx_mpwqe_supported(mdev));
5342 
5343 	/* XDP SQ */
5344 	MLX5E_SET_PFLAG(params, MLX5E_PFLAG_XDP_TX_MPWQE, mlx5e_tx_mpwqe_supported(mdev));
5345 
5346 	/* set CQE compression */
5347 	params->rx_cqe_compress_def = false;
5348 	if (MLX5_CAP_GEN(mdev, cqe_compression) &&
5349 	    MLX5_CAP_GEN(mdev, vport_group_manager))
5350 		params->rx_cqe_compress_def = slow_pci_heuristic(mdev);
5351 
5352 	MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS, params->rx_cqe_compress_def);
5353 	MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_NO_CSUM_COMPLETE, false);
5354 
5355 	/* RQ */
5356 	mlx5e_build_rq_params(mdev, params);
5357 
5358 	params->terminate_lkey_be = mlx5_core_get_terminate_scatter_list_mkey(mdev);
5359 
5360 	params->packet_merge.timeout = mlx5e_choose_lro_timeout(mdev, MLX5E_DEFAULT_LRO_TIMEOUT);
5361 
5362 	/* CQ moderation params */
5363 	params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation) &&
5364 				 MLX5_CAP_GEN(mdev, cq_period_mode_modify);
5365 	params->tx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation) &&
5366 				 MLX5_CAP_GEN(mdev, cq_period_mode_modify);
5367 	params->rx_moder_use_cqe_mode = !!MLX5_CAP_GEN(mdev, cq_period_start_from_cqe);
5368 	params->tx_moder_use_cqe_mode = false;
5369 	mlx5e_reset_rx_moderation(&params->rx_cq_moderation, params->rx_moder_use_cqe_mode,
5370 				  params->rx_dim_enabled);
5371 	mlx5e_reset_tx_moderation(&params->tx_cq_moderation, params->tx_moder_use_cqe_mode,
5372 				  params->tx_dim_enabled);
5373 
5374 	/* TX inline */
5375 	mlx5_query_min_inline(mdev, &params->tx_min_inline_mode);
5376 
5377 	/* AF_XDP */
5378 	params->xsk = xsk;
5379 
5380 	/* Do not update netdev->features directly in here
5381 	 * on mlx5e_attach_netdev() we will call mlx5e_update_features()
5382 	 * To update netdev->features please modify mlx5e_fix_features()
5383 	 */
5384 }
5385 
mlx5e_set_netdev_dev_addr(struct net_device * netdev)5386 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
5387 {
5388 	struct mlx5e_priv *priv = netdev_priv(netdev);
5389 	u8 addr[ETH_ALEN];
5390 
5391 	mlx5_query_mac_address(priv->mdev, addr);
5392 	if (is_zero_ether_addr(addr) &&
5393 	    !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
5394 		eth_hw_addr_random(netdev);
5395 		mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
5396 		return;
5397 	}
5398 
5399 	eth_hw_addr_set(netdev, addr);
5400 }
5401 
mlx5e_vxlan_set_port(struct net_device * netdev,unsigned int table,unsigned int entry,struct udp_tunnel_info * ti)5402 static int mlx5e_vxlan_set_port(struct net_device *netdev, unsigned int table,
5403 				unsigned int entry, struct udp_tunnel_info *ti)
5404 {
5405 	struct mlx5e_priv *priv = netdev_priv(netdev);
5406 
5407 	return mlx5_vxlan_add_port(priv->mdev->vxlan, ntohs(ti->port));
5408 }
5409 
mlx5e_vxlan_unset_port(struct net_device * netdev,unsigned int table,unsigned int entry,struct udp_tunnel_info * ti)5410 static int mlx5e_vxlan_unset_port(struct net_device *netdev, unsigned int table,
5411 				  unsigned int entry, struct udp_tunnel_info *ti)
5412 {
5413 	struct mlx5e_priv *priv = netdev_priv(netdev);
5414 
5415 	return mlx5_vxlan_del_port(priv->mdev->vxlan, ntohs(ti->port));
5416 }
5417 
mlx5e_vxlan_set_netdev_info(struct mlx5e_priv * priv)5418 void mlx5e_vxlan_set_netdev_info(struct mlx5e_priv *priv)
5419 {
5420 	if (!mlx5_vxlan_allowed(priv->mdev->vxlan))
5421 		return;
5422 
5423 	priv->nic_info.set_port = mlx5e_vxlan_set_port;
5424 	priv->nic_info.unset_port = mlx5e_vxlan_unset_port;
5425 	priv->nic_info.flags = UDP_TUNNEL_NIC_INFO_STATIC_IANA_VXLAN;
5426 	priv->nic_info.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN;
5427 	/* Don't count the space hard-coded to the IANA port */
5428 	priv->nic_info.tables[0].n_entries =
5429 		mlx5_vxlan_max_udp_ports(priv->mdev) - 1;
5430 
5431 	priv->netdev->udp_tunnel_nic_info = &priv->nic_info;
5432 }
5433 
mlx5e_tunnel_any_tx_proto_supported(struct mlx5_core_dev * mdev)5434 static bool mlx5e_tunnel_any_tx_proto_supported(struct mlx5_core_dev *mdev)
5435 {
5436 	int tt;
5437 
5438 	for (tt = 0; tt < MLX5_NUM_TUNNEL_TT; tt++) {
5439 		if (mlx5e_tunnel_proto_supported_tx(mdev, mlx5_get_proto_by_tunnel_type(tt)))
5440 			return true;
5441 	}
5442 	return (mlx5_vxlan_allowed(mdev->vxlan) || mlx5_geneve_tx_allowed(mdev));
5443 }
5444 
mlx5e_get_queue_stats_rx(struct net_device * dev,int i,struct netdev_queue_stats_rx * stats)5445 static void mlx5e_get_queue_stats_rx(struct net_device *dev, int i,
5446 				     struct netdev_queue_stats_rx *stats)
5447 {
5448 	struct mlx5e_priv *priv = netdev_priv(dev);
5449 	struct mlx5e_channel_stats *channel_stats;
5450 	struct mlx5e_rq_stats *xskrq_stats;
5451 	struct mlx5e_rq_stats *rq_stats;
5452 
5453 	if (mlx5e_is_uplink_rep(priv) || !priv->stats_nch)
5454 		return;
5455 
5456 	channel_stats = priv->channel_stats[i];
5457 	xskrq_stats = &channel_stats->xskrq;
5458 	rq_stats = &channel_stats->rq;
5459 
5460 	stats->packets = rq_stats->packets + xskrq_stats->packets;
5461 	stats->bytes = rq_stats->bytes + xskrq_stats->bytes;
5462 	stats->alloc_fail = rq_stats->buff_alloc_err +
5463 			    xskrq_stats->buff_alloc_err;
5464 }
5465 
mlx5e_get_queue_stats_tx(struct net_device * dev,int i,struct netdev_queue_stats_tx * stats)5466 static void mlx5e_get_queue_stats_tx(struct net_device *dev, int i,
5467 				     struct netdev_queue_stats_tx *stats)
5468 {
5469 	struct mlx5e_priv *priv = netdev_priv(dev);
5470 	struct mlx5e_sq_stats *sq_stats;
5471 
5472 	if (!priv->stats_nch)
5473 		return;
5474 
5475 	/* no special case needed for ptp htb etc since txq2sq_stats is kept up
5476 	 * to date for active sq_stats, otherwise get_base_stats takes care of
5477 	 * inactive sqs.
5478 	 */
5479 	sq_stats = priv->txq2sq_stats[i];
5480 	stats->packets = sq_stats->packets;
5481 	stats->bytes = sq_stats->bytes;
5482 }
5483 
mlx5e_get_base_stats(struct net_device * dev,struct netdev_queue_stats_rx * rx,struct netdev_queue_stats_tx * tx)5484 static void mlx5e_get_base_stats(struct net_device *dev,
5485 				 struct netdev_queue_stats_rx *rx,
5486 				 struct netdev_queue_stats_tx *tx)
5487 {
5488 	struct mlx5e_priv *priv = netdev_priv(dev);
5489 	struct mlx5e_ptp *ptp_channel;
5490 	int i, tc;
5491 
5492 	if (!mlx5e_is_uplink_rep(priv)) {
5493 		rx->packets = 0;
5494 		rx->bytes = 0;
5495 		rx->alloc_fail = 0;
5496 
5497 		for (i = priv->channels.params.num_channels; i < priv->stats_nch; i++) {
5498 			struct netdev_queue_stats_rx rx_i = {0};
5499 
5500 			mlx5e_get_queue_stats_rx(dev, i, &rx_i);
5501 
5502 			rx->packets += rx_i.packets;
5503 			rx->bytes += rx_i.bytes;
5504 			rx->alloc_fail += rx_i.alloc_fail;
5505 		}
5506 
5507 		/* always report PTP RX stats from base as there is no
5508 		 * corresponding channel to report them under in
5509 		 * mlx5e_get_queue_stats_rx.
5510 		 */
5511 		if (priv->rx_ptp_opened) {
5512 			struct mlx5e_rq_stats *rq_stats = &priv->ptp_stats.rq;
5513 
5514 			rx->packets += rq_stats->packets;
5515 			rx->bytes += rq_stats->bytes;
5516 		}
5517 	}
5518 
5519 	tx->packets = 0;
5520 	tx->bytes = 0;
5521 
5522 	for (i = 0; i < priv->stats_nch; i++) {
5523 		struct mlx5e_channel_stats *channel_stats = priv->channel_stats[i];
5524 
5525 		/* handle two cases:
5526 		 *
5527 		 *  1. channels which are active. In this case,
5528 		 *     report only deactivated TCs on these channels.
5529 		 *
5530 		 *  2. channels which were deactivated
5531 		 *     (i > priv->channels.params.num_channels)
5532 		 *     must have all of their TCs [0 .. priv->max_opened_tc)
5533 		 *     examined because deactivated channels will not be in the
5534 		 *     range of [0..real_num_tx_queues) and will not have their
5535 		 *     stats reported by mlx5e_get_queue_stats_tx.
5536 		 */
5537 		if (i < priv->channels.params.num_channels)
5538 			tc = mlx5e_get_dcb_num_tc(&priv->channels.params);
5539 		else
5540 			tc = 0;
5541 
5542 		for (; tc < priv->max_opened_tc; tc++) {
5543 			struct mlx5e_sq_stats *sq_stats = &channel_stats->sq[tc];
5544 
5545 			tx->packets += sq_stats->packets;
5546 			tx->bytes += sq_stats->bytes;
5547 		}
5548 	}
5549 
5550 	/* if PTP TX was opened at some point and has since either:
5551 	 *    -  been shutdown and set to NULL, or
5552 	 *    -  simply disabled (bit unset)
5553 	 *
5554 	 * report stats directly from the ptp_stats structures as these queues
5555 	 * are now unavailable and there is no txq index to retrieve these
5556 	 * stats via calls to mlx5e_get_queue_stats_tx.
5557 	 */
5558 	ptp_channel = priv->channels.ptp;
5559 	if (priv->tx_ptp_opened && (!ptp_channel || !test_bit(MLX5E_PTP_STATE_TX, ptp_channel->state))) {
5560 		for (tc = 0; tc < priv->max_opened_tc; tc++) {
5561 			struct mlx5e_sq_stats *sq_stats = &priv->ptp_stats.sq[tc];
5562 
5563 			tx->packets += sq_stats->packets;
5564 			tx->bytes   += sq_stats->bytes;
5565 		}
5566 	}
5567 }
5568 
5569 static const struct netdev_stat_ops mlx5e_stat_ops = {
5570 	.get_queue_stats_rx  = mlx5e_get_queue_stats_rx,
5571 	.get_queue_stats_tx  = mlx5e_get_queue_stats_tx,
5572 	.get_base_stats      = mlx5e_get_base_stats,
5573 };
5574 
5575 struct mlx5_qmgmt_data {
5576 	struct mlx5e_channel *c;
5577 	struct mlx5e_channel_param cparam;
5578 };
5579 
mlx5e_queue_mem_alloc(struct net_device * dev,void * newq,int queue_index)5580 static int mlx5e_queue_mem_alloc(struct net_device *dev, void *newq,
5581 				 int queue_index)
5582 {
5583 	struct mlx5_qmgmt_data *new = (struct mlx5_qmgmt_data *)newq;
5584 	struct mlx5e_priv *priv = netdev_priv(dev);
5585 	struct mlx5e_channels *chs = &priv->channels;
5586 	struct mlx5e_params params = chs->params;
5587 	struct mlx5_core_dev *mdev;
5588 	int err;
5589 
5590 	mutex_lock(&priv->state_lock);
5591 	if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
5592 		err = -ENODEV;
5593 		goto unlock;
5594 	}
5595 
5596 	if (queue_index >= chs->num) {
5597 		err = -ERANGE;
5598 		goto unlock;
5599 	}
5600 
5601 	if (MLX5E_GET_PFLAG(&chs->params, MLX5E_PFLAG_TX_PORT_TS) ||
5602 	    chs->params.ptp_rx   ||
5603 	    chs->params.xdp_prog ||
5604 	    priv->htb) {
5605 		netdev_err(priv->netdev,
5606 			   "Cloning channels with Port/rx PTP, XDP or HTB is not supported\n");
5607 		err = -EOPNOTSUPP;
5608 		goto unlock;
5609 	}
5610 
5611 	mdev = mlx5_sd_ch_ix_get_dev(priv->mdev, queue_index);
5612 	err = mlx5e_build_channel_param(mdev, &params, &new->cparam);
5613 	if (err)
5614 		goto unlock;
5615 
5616 	err = mlx5e_open_channel(priv, queue_index, &params, NULL, &new->c);
5617 unlock:
5618 	mutex_unlock(&priv->state_lock);
5619 	return err;
5620 }
5621 
mlx5e_queue_mem_free(struct net_device * dev,void * mem)5622 static void mlx5e_queue_mem_free(struct net_device *dev, void *mem)
5623 {
5624 	struct mlx5_qmgmt_data *data = (struct mlx5_qmgmt_data *)mem;
5625 
5626 	/* not supposed to happen since mlx5e_queue_start never fails
5627 	 * but this is how this should be implemented just in case
5628 	 */
5629 	if (data->c)
5630 		mlx5e_close_channel(data->c);
5631 }
5632 
mlx5e_queue_stop(struct net_device * dev,void * oldq,int queue_index)5633 static int mlx5e_queue_stop(struct net_device *dev, void *oldq, int queue_index)
5634 {
5635 	/* In mlx5 a txq cannot be simply stopped in isolation, only restarted.
5636 	 * mlx5e_queue_start does not fail, we stop the old queue there.
5637 	 * TODO: Improve this.
5638 	 */
5639 	return 0;
5640 }
5641 
mlx5e_queue_start(struct net_device * dev,void * newq,int queue_index)5642 static int mlx5e_queue_start(struct net_device *dev, void *newq,
5643 			     int queue_index)
5644 {
5645 	struct mlx5_qmgmt_data *new = (struct mlx5_qmgmt_data *)newq;
5646 	struct mlx5e_priv *priv = netdev_priv(dev);
5647 	struct mlx5e_channel *old;
5648 
5649 	mutex_lock(&priv->state_lock);
5650 
5651 	/* stop and close the old */
5652 	old = priv->channels.c[queue_index];
5653 	mlx5e_deactivate_priv_channels(priv);
5654 	/* close old before activating new, to avoid napi conflict */
5655 	mlx5e_close_channel(old);
5656 
5657 	/* start the new */
5658 	priv->channels.c[queue_index] = new->c;
5659 	mlx5e_activate_priv_channels(priv);
5660 	mutex_unlock(&priv->state_lock);
5661 	return 0;
5662 }
5663 
mlx5e_queue_get_dma_dev(struct net_device * dev,int queue_index)5664 static struct device *mlx5e_queue_get_dma_dev(struct net_device *dev,
5665 					      int queue_index)
5666 {
5667 	struct mlx5e_priv *priv = netdev_priv(dev);
5668 	struct mlx5e_channels *channels;
5669 	struct device *pdev = NULL;
5670 	struct mlx5e_channel *ch;
5671 
5672 	channels = &priv->channels;
5673 
5674 	mutex_lock(&priv->state_lock);
5675 
5676 	if (queue_index >= channels->num)
5677 		goto out;
5678 
5679 	ch = channels->c[queue_index];
5680 	pdev = ch->pdev;
5681 out:
5682 	mutex_unlock(&priv->state_lock);
5683 
5684 	return pdev;
5685 }
5686 
5687 static const struct netdev_queue_mgmt_ops mlx5e_queue_mgmt_ops = {
5688 	.ndo_queue_mem_size	=	sizeof(struct mlx5_qmgmt_data),
5689 	.ndo_queue_mem_alloc	=	mlx5e_queue_mem_alloc,
5690 	.ndo_queue_mem_free	=	mlx5e_queue_mem_free,
5691 	.ndo_queue_start	=	mlx5e_queue_start,
5692 	.ndo_queue_stop		=	mlx5e_queue_stop,
5693 	.ndo_queue_get_dma_dev	=	mlx5e_queue_get_dma_dev,
5694 };
5695 
mlx5e_build_nic_netdev(struct net_device * netdev)5696 static void mlx5e_build_nic_netdev(struct net_device *netdev)
5697 {
5698 	struct mlx5e_priv *priv = netdev_priv(netdev);
5699 	struct mlx5_core_dev *mdev = priv->mdev;
5700 	bool fcs_supported;
5701 	bool fcs_enabled;
5702 
5703 	SET_NETDEV_DEV(netdev, mdev->device);
5704 
5705 	netdev->netdev_ops = &mlx5e_netdev_ops;
5706 	netdev->queue_mgmt_ops = &mlx5e_queue_mgmt_ops;
5707 	netdev->xdp_metadata_ops = &mlx5e_xdp_metadata_ops;
5708 	netdev->xsk_tx_metadata_ops = &mlx5e_xsk_tx_metadata_ops;
5709 	netdev->request_ops_lock = true;
5710 	netdev_lockdep_set_classes(netdev);
5711 
5712 	mlx5e_dcbnl_build_netdev(netdev);
5713 
5714 	netdev->watchdog_timeo    = 15 * HZ;
5715 
5716 	netdev->stat_ops	  = &mlx5e_stat_ops;
5717 	netdev->ethtool_ops	  = &mlx5e_ethtool_ops;
5718 
5719 	netdev->vlan_features    |= NETIF_F_SG;
5720 	netdev->vlan_features    |= NETIF_F_HW_CSUM;
5721 	netdev->vlan_features    |= NETIF_F_HW_MACSEC;
5722 	netdev->vlan_features    |= NETIF_F_GRO;
5723 	netdev->vlan_features    |= NETIF_F_TSO;
5724 	netdev->vlan_features    |= NETIF_F_TSO6;
5725 	netdev->vlan_features    |= NETIF_F_RXCSUM;
5726 	netdev->vlan_features    |= NETIF_F_RXHASH;
5727 	netdev->vlan_features    |= NETIF_F_GSO_PARTIAL;
5728 
5729 	netdev->mpls_features    |= NETIF_F_SG;
5730 	netdev->mpls_features    |= NETIF_F_HW_CSUM;
5731 	netdev->mpls_features    |= NETIF_F_TSO;
5732 	netdev->mpls_features    |= NETIF_F_TSO6;
5733 
5734 	netdev->hw_enc_features  |= NETIF_F_HW_VLAN_CTAG_TX;
5735 	netdev->hw_enc_features  |= NETIF_F_HW_VLAN_CTAG_RX;
5736 
5737 	/* Tunneled LRO is not supported in the driver, and the same RQs are
5738 	 * shared between inner and outer TIRs, so the driver can't disable LRO
5739 	 * for inner TIRs while having it enabled for outer TIRs. Due to this,
5740 	 * block LRO altogether if the firmware declares tunneled LRO support.
5741 	 */
5742 	if (!!MLX5_CAP_ETH(mdev, lro_cap) &&
5743 	    !MLX5_CAP_ETH(mdev, tunnel_lro_vxlan) &&
5744 	    !MLX5_CAP_ETH(mdev, tunnel_lro_gre) &&
5745 	    mlx5e_check_fragmented_striding_rq_cap(mdev, PAGE_SHIFT,
5746 						   MLX5E_MPWRQ_UMR_MODE_ALIGNED))
5747 		netdev->vlan_features    |= NETIF_F_LRO;
5748 
5749 	if (mlx5e_hw_gro_supported(mdev) &&
5750 	    mlx5e_check_fragmented_striding_rq_cap(mdev, PAGE_SHIFT,
5751 						   MLX5E_MPWRQ_UMR_MODE_ALIGNED))
5752 		netdev->vlan_features |= NETIF_F_GRO_HW;
5753 
5754 	netdev->hw_features       = netdev->vlan_features;
5755 	netdev->hw_features      |= NETIF_F_HW_VLAN_CTAG_TX;
5756 	netdev->hw_features      |= NETIF_F_HW_VLAN_CTAG_RX;
5757 	netdev->hw_features      |= NETIF_F_HW_VLAN_CTAG_FILTER;
5758 	netdev->hw_features      |= NETIF_F_HW_VLAN_STAG_TX;
5759 
5760 	if (mlx5e_tunnel_any_tx_proto_supported(mdev)) {
5761 		netdev->hw_enc_features |= NETIF_F_HW_CSUM;
5762 		netdev->hw_enc_features |= NETIF_F_TSO;
5763 		netdev->hw_enc_features |= NETIF_F_TSO6;
5764 		netdev->hw_enc_features |= NETIF_F_GSO_PARTIAL;
5765 	}
5766 
5767 	if (mlx5_vxlan_allowed(mdev->vxlan) || mlx5_geneve_tx_allowed(mdev)) {
5768 		netdev->hw_features     |= NETIF_F_GSO_UDP_TUNNEL |
5769 					   NETIF_F_GSO_UDP_TUNNEL_CSUM;
5770 		netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
5771 					   NETIF_F_GSO_UDP_TUNNEL_CSUM;
5772 		netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
5773 		netdev->vlan_features |= NETIF_F_GSO_UDP_TUNNEL |
5774 					 NETIF_F_GSO_UDP_TUNNEL_CSUM;
5775 	}
5776 
5777 	if (mlx5e_tunnel_proto_supported_tx(mdev, IPPROTO_GRE)) {
5778 		netdev->hw_features     |= NETIF_F_GSO_GRE |
5779 					   NETIF_F_GSO_GRE_CSUM;
5780 		netdev->hw_enc_features |= NETIF_F_GSO_GRE |
5781 					   NETIF_F_GSO_GRE_CSUM;
5782 		netdev->gso_partial_features |= NETIF_F_GSO_GRE |
5783 						NETIF_F_GSO_GRE_CSUM;
5784 	}
5785 
5786 	if (mlx5e_tunnel_proto_supported_tx(mdev, IPPROTO_IPIP)) {
5787 		netdev->hw_features |= NETIF_F_GSO_IPXIP4 |
5788 				       NETIF_F_GSO_IPXIP6;
5789 		netdev->hw_enc_features |= NETIF_F_GSO_IPXIP4 |
5790 					   NETIF_F_GSO_IPXIP6;
5791 		netdev->gso_partial_features |= NETIF_F_GSO_IPXIP4 |
5792 						NETIF_F_GSO_IPXIP6;
5793 	}
5794 
5795 	netdev->gso_partial_features             |= NETIF_F_GSO_UDP_L4;
5796 	netdev->hw_features                      |= NETIF_F_GSO_UDP_L4;
5797 
5798 	mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
5799 
5800 	if (fcs_supported)
5801 		netdev->hw_features |= NETIF_F_RXALL;
5802 
5803 	if (MLX5_CAP_ETH(mdev, scatter_fcs))
5804 		netdev->hw_features |= NETIF_F_RXFCS;
5805 
5806 	if (mlx5_qos_is_supported(mdev))
5807 		netdev->hw_features |= NETIF_F_HW_TC;
5808 
5809 	netdev->features          = netdev->hw_features;
5810 
5811 	/* Defaults */
5812 	if (fcs_enabled)
5813 		netdev->features  &= ~NETIF_F_RXALL;
5814 	netdev->features  &= ~NETIF_F_LRO;
5815 	netdev->features  &= ~NETIF_F_GRO_HW;
5816 	netdev->features  &= ~NETIF_F_RXFCS;
5817 
5818 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
5819 	if (FT_CAP(flow_modify_en) &&
5820 	    FT_CAP(modify_root) &&
5821 	    FT_CAP(identified_miss_table_mode) &&
5822 	    FT_CAP(flow_table_modify)) {
5823 #if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
5824 		netdev->hw_features      |= NETIF_F_HW_TC;
5825 #endif
5826 #if IS_ENABLED(CONFIG_MLX5_EN_ARFS)
5827 		netdev->hw_features	 |= NETIF_F_NTUPLE;
5828 #elif IS_ENABLED(CONFIG_MLX5_EN_RXNFC)
5829 		netdev->features	 |= NETIF_F_NTUPLE;
5830 #endif
5831 	}
5832 
5833 	netdev->features         |= NETIF_F_HIGHDMA;
5834 	netdev->features         |= NETIF_F_HW_VLAN_STAG_FILTER;
5835 
5836 	netdev->priv_flags       |= IFF_UNICAST_FLT;
5837 
5838 	netdev->netmem_tx = true;
5839 
5840 	netif_set_tso_max_size(netdev, GSO_MAX_SIZE);
5841 	mlx5e_set_xdp_feature(netdev);
5842 	mlx5e_set_netdev_dev_addr(netdev);
5843 	mlx5e_macsec_build_netdev(priv);
5844 	mlx5e_ipsec_build_netdev(priv);
5845 	mlx5e_ktls_build_netdev(priv);
5846 }
5847 
mlx5e_create_q_counters(struct mlx5e_priv * priv)5848 void mlx5e_create_q_counters(struct mlx5e_priv *priv)
5849 {
5850 	u32 out[MLX5_ST_SZ_DW(alloc_q_counter_out)] = {};
5851 	u32 in[MLX5_ST_SZ_DW(alloc_q_counter_in)] = {};
5852 	struct mlx5_core_dev *mdev = priv->mdev;
5853 	struct mlx5_core_dev *pos;
5854 	int err, i;
5855 
5856 	MLX5_SET(alloc_q_counter_in, in, opcode, MLX5_CMD_OP_ALLOC_Q_COUNTER);
5857 
5858 	mlx5_sd_for_each_dev(i, mdev, pos) {
5859 		err = mlx5_cmd_exec_inout(pos, alloc_q_counter, in, out);
5860 		if (!err)
5861 			priv->q_counter[i] =
5862 				MLX5_GET(alloc_q_counter_out, out, counter_set_id);
5863 	}
5864 
5865 	err = mlx5_cmd_exec_inout(mdev, alloc_q_counter, in, out);
5866 	if (!err)
5867 		priv->drop_rq_q_counter =
5868 			MLX5_GET(alloc_q_counter_out, out, counter_set_id);
5869 }
5870 
mlx5e_destroy_q_counters(struct mlx5e_priv * priv)5871 void mlx5e_destroy_q_counters(struct mlx5e_priv *priv)
5872 {
5873 	u32 in[MLX5_ST_SZ_DW(dealloc_q_counter_in)] = {};
5874 	struct mlx5_core_dev *pos;
5875 	int i;
5876 
5877 	MLX5_SET(dealloc_q_counter_in, in, opcode,
5878 		 MLX5_CMD_OP_DEALLOC_Q_COUNTER);
5879 	mlx5_sd_for_each_dev(i, priv->mdev, pos) {
5880 		if (priv->q_counter[i]) {
5881 			MLX5_SET(dealloc_q_counter_in, in, counter_set_id,
5882 				 priv->q_counter[i]);
5883 			mlx5_cmd_exec_in(pos, dealloc_q_counter, in);
5884 		}
5885 	}
5886 
5887 	if (priv->drop_rq_q_counter) {
5888 		MLX5_SET(dealloc_q_counter_in, in, counter_set_id,
5889 			 priv->drop_rq_q_counter);
5890 		mlx5_cmd_exec_in(priv->mdev, dealloc_q_counter, in);
5891 	}
5892 }
5893 
mlx5e_nic_init(struct mlx5_core_dev * mdev,struct net_device * netdev)5894 static int mlx5e_nic_init(struct mlx5_core_dev *mdev,
5895 			  struct net_device *netdev)
5896 {
5897 	const bool take_rtnl = netdev->reg_state == NETREG_REGISTERED;
5898 	struct mlx5e_priv *priv = netdev_priv(netdev);
5899 	struct mlx5e_flow_steering *fs;
5900 	int err;
5901 
5902 	mlx5e_build_nic_params(priv, &priv->xsk, netdev->mtu);
5903 	mlx5e_vxlan_set_netdev_info(priv);
5904 
5905 	mlx5e_timestamp_init(priv);
5906 
5907 	priv->dfs_root = debugfs_create_dir("nic",
5908 					    mlx5_debugfs_get_dev_root(mdev));
5909 
5910 	fs = mlx5e_fs_init(priv->profile, mdev,
5911 			   !test_bit(MLX5E_STATE_DESTROYING, &priv->state),
5912 			   priv->dfs_root);
5913 	if (!fs) {
5914 		err = -ENOMEM;
5915 		mlx5_core_err(mdev, "FS initialization failed, %d\n", err);
5916 		debugfs_remove_recursive(priv->dfs_root);
5917 		return err;
5918 	}
5919 	priv->fs = fs;
5920 
5921 	err = mlx5e_psp_init(priv);
5922 	if (err)
5923 		mlx5_core_err(mdev, "PSP initialization failed, %d\n", err);
5924 
5925 	err = mlx5e_ktls_init(priv);
5926 	if (err)
5927 		mlx5_core_err(mdev, "TLS initialization failed, %d\n", err);
5928 
5929 	mlx5e_health_create_reporters(priv);
5930 
5931 	/* If netdev is already registered (e.g. move from uplink to nic profile),
5932 	 * RTNL lock must be held before triggering netdev notifiers.
5933 	 */
5934 	if (take_rtnl)
5935 		rtnl_lock();
5936 
5937 	mlx5e_psp_register(priv);
5938 	/* update XDP supported features */
5939 	mlx5e_set_xdp_feature(netdev);
5940 
5941 	if (take_rtnl)
5942 		rtnl_unlock();
5943 
5944 	return 0;
5945 }
5946 
mlx5e_nic_cleanup(struct mlx5e_priv * priv)5947 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
5948 {
5949 	mlx5e_health_destroy_reporters(priv);
5950 	mlx5e_psp_unregister(priv);
5951 	mlx5e_ktls_cleanup(priv);
5952 	mlx5e_psp_cleanup(priv);
5953 	mlx5e_fs_cleanup(priv->fs);
5954 	debugfs_remove_recursive(priv->dfs_root);
5955 	priv->fs = NULL;
5956 }
5957 
mlx5e_init_nic_rx(struct mlx5e_priv * priv)5958 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv)
5959 {
5960 	struct mlx5_core_dev *mdev = priv->mdev;
5961 	enum mlx5e_rx_res_features features;
5962 	int err;
5963 
5964 	mlx5e_create_q_counters(priv);
5965 
5966 	err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
5967 	if (err) {
5968 		mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
5969 		goto err_destroy_q_counters;
5970 	}
5971 
5972 	features = MLX5E_RX_RES_FEATURE_PTP;
5973 	if (mlx5_tunnel_inner_ft_supported(mdev))
5974 		features |= MLX5E_RX_RES_FEATURE_INNER_FT;
5975 	if (mlx5_get_sd(priv->mdev))
5976 		features |= MLX5E_RX_RES_FEATURE_MULTI_VHCA;
5977 
5978 	priv->rx_res = mlx5e_rx_res_create(priv->mdev, features, priv->max_nch, priv->drop_rq.rqn,
5979 					   &priv->channels.params.packet_merge,
5980 					   priv->channels.params.num_channels);
5981 	if (IS_ERR(priv->rx_res)) {
5982 		err = PTR_ERR(priv->rx_res);
5983 		priv->rx_res = NULL;
5984 		mlx5_core_err(mdev, "create rx resources failed, %d\n", err);
5985 		goto err_close_drop_rq;
5986 	}
5987 
5988 	err = mlx5e_create_flow_steering(priv->fs, priv->rx_res, priv->profile,
5989 					 priv->netdev);
5990 	if (err) {
5991 		mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
5992 		goto err_destroy_rx_res;
5993 	}
5994 
5995 	err = mlx5e_tc_nic_init(priv);
5996 	if (err)
5997 		goto err_destroy_flow_steering;
5998 
5999 	err = mlx5e_accel_init_rx(priv);
6000 	if (err)
6001 		goto err_tc_nic_cleanup;
6002 
6003 #ifdef CONFIG_MLX5_EN_ARFS
6004 	priv->netdev->rx_cpu_rmap =  mlx5_eq_table_get_rmap(priv->mdev);
6005 #endif
6006 
6007 	return 0;
6008 
6009 err_tc_nic_cleanup:
6010 	mlx5e_tc_nic_cleanup(priv);
6011 err_destroy_flow_steering:
6012 	mlx5e_destroy_flow_steering(priv->fs, mlx5e_fs_has_arfs(priv->netdev),
6013 				    priv->profile);
6014 err_destroy_rx_res:
6015 	mlx5e_rx_res_destroy(priv->rx_res);
6016 	priv->rx_res = NULL;
6017 err_close_drop_rq:
6018 	mlx5e_close_drop_rq(&priv->drop_rq);
6019 err_destroy_q_counters:
6020 	mlx5e_destroy_q_counters(priv);
6021 	return err;
6022 }
6023 
mlx5e_cleanup_nic_rx(struct mlx5e_priv * priv)6024 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv)
6025 {
6026 	mlx5e_accel_cleanup_rx(priv);
6027 	mlx5e_tc_nic_cleanup(priv);
6028 	mlx5e_destroy_flow_steering(priv->fs, mlx5e_fs_has_arfs(priv->netdev),
6029 				    priv->profile);
6030 	mlx5e_rx_res_destroy(priv->rx_res);
6031 	priv->rx_res = NULL;
6032 	mlx5e_close_drop_rq(&priv->drop_rq);
6033 	mlx5e_destroy_q_counters(priv);
6034 }
6035 
mlx5e_set_mqprio_rl(struct mlx5e_priv * priv)6036 static void mlx5e_set_mqprio_rl(struct mlx5e_priv *priv)
6037 {
6038 	struct mlx5e_params *params;
6039 	struct mlx5e_mqprio_rl *rl;
6040 
6041 	params = &priv->channels.params;
6042 	if (params->mqprio.mode != TC_MQPRIO_MODE_CHANNEL)
6043 		return;
6044 
6045 	rl = mlx5e_mqprio_rl_create(priv->mdev, params->mqprio.num_tc,
6046 				    params->mqprio.channel.max_rate);
6047 	if (IS_ERR(rl))
6048 		rl = NULL;
6049 	priv->mqprio_rl = rl;
6050 	mlx5e_mqprio_rl_update_params(params, rl);
6051 }
6052 
mlx5e_init_nic_tx(struct mlx5e_priv * priv)6053 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv)
6054 {
6055 	int err;
6056 
6057 	err = mlx5e_accel_init_tx(priv);
6058 	if (err)
6059 		return err;
6060 
6061 	mlx5e_set_mqprio_rl(priv);
6062 	mlx5e_dcbnl_initialize(priv);
6063 	return 0;
6064 }
6065 
mlx5e_nic_enable(struct mlx5e_priv * priv)6066 static void mlx5e_nic_enable(struct mlx5e_priv *priv)
6067 {
6068 	struct net_device *netdev = priv->netdev;
6069 	struct mlx5_core_dev *mdev = priv->mdev;
6070 	int err;
6071 
6072 	mlx5e_fs_init_l2_addr(priv->fs, netdev);
6073 	mlx5e_ipsec_init(priv);
6074 
6075 	err = mlx5e_macsec_init(priv);
6076 	if (err)
6077 		mlx5_core_err(mdev, "MACsec initialization failed, %d\n", err);
6078 
6079 	/* Marking the link as currently not needed by the Driver */
6080 	if (!netif_running(netdev))
6081 		mlx5e_modify_admin_state(mdev, MLX5_PORT_DOWN);
6082 
6083 	mlx5e_set_netdev_mtu_boundaries(priv);
6084 	mlx5e_set_dev_port_mtu(priv);
6085 
6086 	mlx5_lag_add_netdev(mdev, netdev);
6087 
6088 	mlx5e_enable_async_events(priv);
6089 	mlx5e_enable_blocking_events(priv);
6090 	if (mlx5e_monitor_counter_supported(priv))
6091 		mlx5e_monitor_counter_init(priv);
6092 
6093 	mlx5e_pcie_cong_event_init(priv);
6094 	mlx5e_hv_vhca_stats_create(priv);
6095 	if (netdev->reg_state != NETREG_REGISTERED)
6096 		return;
6097 	mlx5e_dcbnl_init_app(priv);
6098 
6099 	mlx5e_nic_set_rx_mode(priv);
6100 
6101 	rtnl_lock();
6102 	netdev_lock(netdev);
6103 	if (netif_running(netdev))
6104 		mlx5e_open(netdev);
6105 	udp_tunnel_nic_reset_ntf(priv->netdev);
6106 	netdev_unlock(netdev);
6107 	netif_device_attach(netdev);
6108 	rtnl_unlock();
6109 }
6110 
mlx5e_nic_disable(struct mlx5e_priv * priv)6111 static void mlx5e_nic_disable(struct mlx5e_priv *priv)
6112 {
6113 	struct mlx5_core_dev *mdev = priv->mdev;
6114 
6115 	if (priv->netdev->reg_state == NETREG_REGISTERED)
6116 		mlx5e_dcbnl_delete_app(priv);
6117 
6118 	rtnl_lock();
6119 	netdev_lock(priv->netdev);
6120 	if (netif_running(priv->netdev))
6121 		mlx5e_close(priv->netdev);
6122 	netif_device_detach(priv->netdev);
6123 	if (priv->en_trap) {
6124 		mlx5e_deactivate_trap(priv);
6125 		mlx5e_close_trap(priv->en_trap);
6126 		priv->en_trap = NULL;
6127 	}
6128 	netdev_unlock(priv->netdev);
6129 	rtnl_unlock();
6130 
6131 	mlx5e_nic_set_rx_mode(priv);
6132 
6133 	mlx5e_pcie_cong_event_cleanup(priv);
6134 	mlx5e_hv_vhca_stats_destroy(priv);
6135 	if (mlx5e_monitor_counter_supported(priv))
6136 		mlx5e_monitor_counter_cleanup(priv);
6137 
6138 	mlx5e_ipsec_disable_events(priv);
6139 	mlx5e_disable_blocking_events(priv);
6140 	mlx5e_disable_async_events(priv);
6141 	mlx5_lag_remove_netdev(mdev, priv->netdev);
6142 	mlx5_vxlan_reset_to_default(mdev->vxlan);
6143 	mlx5e_macsec_cleanup(priv);
6144 	mlx5e_ipsec_cleanup(priv);
6145 }
6146 
mlx5e_update_nic_rx(struct mlx5e_priv * priv)6147 static int mlx5e_update_nic_rx(struct mlx5e_priv *priv)
6148 {
6149 	return mlx5e_refresh_tirs(priv, false, false);
6150 }
6151 
6152 static const struct mlx5e_profile mlx5e_nic_profile = {
6153 	.init		   = mlx5e_nic_init,
6154 	.cleanup	   = mlx5e_nic_cleanup,
6155 	.init_rx	   = mlx5e_init_nic_rx,
6156 	.cleanup_rx	   = mlx5e_cleanup_nic_rx,
6157 	.init_tx	   = mlx5e_init_nic_tx,
6158 	.cleanup_tx	   = mlx5e_cleanup_nic_tx,
6159 	.enable		   = mlx5e_nic_enable,
6160 	.disable	   = mlx5e_nic_disable,
6161 	.update_rx	   = mlx5e_update_nic_rx,
6162 	.update_stats	   = mlx5e_stats_update_ndo_stats,
6163 	.update_carrier	   = mlx5e_update_carrier,
6164 	.rx_handlers       = &mlx5e_rx_handlers_nic,
6165 	.max_tc		   = MLX5_MAX_NUM_TC,
6166 	.stats_grps	   = mlx5e_nic_stats_grps,
6167 	.stats_grps_num	   = mlx5e_nic_stats_grps_num,
6168 	.features          = BIT(MLX5E_PROFILE_FEATURE_PTP_RX) |
6169 		BIT(MLX5E_PROFILE_FEATURE_PTP_TX) |
6170 		BIT(MLX5E_PROFILE_FEATURE_QOS_HTB) |
6171 		BIT(MLX5E_PROFILE_FEATURE_FS_VLAN) |
6172 		BIT(MLX5E_PROFILE_FEATURE_FS_TC),
6173 };
6174 
mlx5e_profile_max_num_channels(struct mlx5_core_dev * mdev,const struct mlx5e_profile * profile)6175 static int mlx5e_profile_max_num_channels(struct mlx5_core_dev *mdev,
6176 					  const struct mlx5e_profile *profile)
6177 {
6178 	int nch;
6179 
6180 	nch = mlx5e_get_max_num_channels(mdev);
6181 
6182 	if (profile->max_nch_limit)
6183 		nch = min_t(int, nch, profile->max_nch_limit(mdev));
6184 	return nch;
6185 }
6186 
6187 static unsigned int
mlx5e_calc_max_nch(struct mlx5_core_dev * mdev,struct net_device * netdev,const struct mlx5e_profile * profile)6188 mlx5e_calc_max_nch(struct mlx5_core_dev *mdev, struct net_device *netdev,
6189 		   const struct mlx5e_profile *profile)
6190 
6191 {
6192 	unsigned int max_nch, tmp;
6193 
6194 	/* core resources */
6195 	max_nch = mlx5e_profile_max_num_channels(mdev, profile);
6196 
6197 	/* netdev rx queues */
6198 	max_nch = min_t(unsigned int, max_nch, netdev->num_rx_queues);
6199 
6200 	/* netdev tx queues */
6201 	tmp = netdev->num_tx_queues;
6202 	if (mlx5_qos_is_supported(mdev))
6203 		tmp -= mlx5e_qos_max_leaf_nodes(mdev);
6204 	if (MLX5_CAP_GEN(mdev, ts_cqe_to_dest_cqn))
6205 		tmp -= profile->max_tc;
6206 	tmp = tmp / profile->max_tc;
6207 	max_nch = min_t(unsigned int, max_nch, tmp);
6208 
6209 	return max_nch;
6210 }
6211 
mlx5e_get_pf_num_tirs(struct mlx5_core_dev * mdev)6212 int mlx5e_get_pf_num_tirs(struct mlx5_core_dev *mdev)
6213 {
6214 	/* Indirect TIRS: 2 sets of TTCs (inner + outer steering)
6215 	 * and 1 set of direct TIRS
6216 	 */
6217 	return 2 * MLX5E_NUM_INDIR_TIRS
6218 		+ mlx5e_profile_max_num_channels(mdev, &mlx5e_nic_profile);
6219 }
6220 
mlx5e_set_rx_mode_work(struct work_struct * work)6221 void mlx5e_set_rx_mode_work(struct work_struct *work)
6222 {
6223 	struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
6224 					       set_rx_mode_work);
6225 
6226 	return mlx5e_fs_set_rx_mode_work(priv->fs, priv->netdev);
6227 }
6228 
6229 /* 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)6230 int mlx5e_priv_init(struct mlx5e_priv *priv,
6231 		    const struct mlx5e_profile *profile,
6232 		    struct net_device *netdev,
6233 		    struct mlx5_core_dev *mdev)
6234 {
6235 	int nch, num_txqs, node;
6236 	int err;
6237 
6238 	num_txqs = netdev->num_tx_queues;
6239 	nch = mlx5e_calc_max_nch(mdev, netdev, profile);
6240 	node = dev_to_node(mlx5_core_dma_dev(mdev));
6241 
6242 	/* priv init */
6243 	priv->mdev        = mdev;
6244 	priv->netdev      = netdev;
6245 	priv->max_nch     = nch;
6246 	priv->max_opened_tc = 1;
6247 
6248 	if (!alloc_cpumask_var(&priv->scratchpad.cpumask, GFP_KERNEL))
6249 		return -ENOMEM;
6250 
6251 	mutex_init(&priv->state_lock);
6252 
6253 	err = mlx5e_selq_init(&priv->selq, &priv->state_lock);
6254 	if (err)
6255 		goto err_free_cpumask;
6256 
6257 	INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
6258 	INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
6259 	INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
6260 	INIT_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
6261 
6262 	priv->wq = create_singlethread_workqueue("mlx5e");
6263 	if (!priv->wq)
6264 		goto err_free_selq;
6265 
6266 	priv->txq2sq = kcalloc_node(num_txqs, sizeof(*priv->txq2sq), GFP_KERNEL, node);
6267 	if (!priv->txq2sq)
6268 		goto err_destroy_workqueue;
6269 
6270 	priv->txq2sq_stats = kcalloc_node(num_txqs, sizeof(*priv->txq2sq_stats), GFP_KERNEL, node);
6271 	if (!priv->txq2sq_stats)
6272 		goto err_free_txq2sq;
6273 
6274 	priv->tx_rates = kcalloc_node(num_txqs, sizeof(*priv->tx_rates), GFP_KERNEL, node);
6275 	if (!priv->tx_rates)
6276 		goto err_free_txq2sq_stats;
6277 
6278 	priv->channel_stats =
6279 		kcalloc_node(nch, sizeof(*priv->channel_stats), GFP_KERNEL, node);
6280 	if (!priv->channel_stats)
6281 		goto err_free_tx_rates;
6282 
6283 	priv->fec_ranges = kcalloc(ETHTOOL_FEC_HIST_MAX,
6284 				   sizeof(*priv->fec_ranges), GFP_KERNEL);
6285 	if (!priv->fec_ranges)
6286 		goto err_free_channel_stats;
6287 
6288 	return 0;
6289 
6290 err_free_channel_stats:
6291 	kfree(priv->channel_stats);
6292 err_free_tx_rates:
6293 	kfree(priv->tx_rates);
6294 err_free_txq2sq_stats:
6295 	kfree(priv->txq2sq_stats);
6296 err_free_txq2sq:
6297 	kfree(priv->txq2sq);
6298 err_destroy_workqueue:
6299 	destroy_workqueue(priv->wq);
6300 err_free_selq:
6301 	mlx5e_selq_cleanup(&priv->selq);
6302 err_free_cpumask:
6303 	free_cpumask_var(priv->scratchpad.cpumask);
6304 	return -ENOMEM;
6305 }
6306 
mlx5e_priv_cleanup(struct mlx5e_priv * priv)6307 void mlx5e_priv_cleanup(struct mlx5e_priv *priv)
6308 {
6309 	int i;
6310 
6311 	/* bail if change profile failed and also rollback failed */
6312 	if (!priv->mdev)
6313 		return;
6314 
6315 	kfree(priv->fec_ranges);
6316 	for (i = 0; i < priv->stats_nch; i++)
6317 		kvfree(priv->channel_stats[i]);
6318 	kfree(priv->channel_stats);
6319 	kfree(priv->tx_rates);
6320 	kfree(priv->txq2sq_stats);
6321 	kfree(priv->txq2sq);
6322 	destroy_workqueue(priv->wq);
6323 	mlx5e_selq_cleanup(&priv->selq);
6324 	free_cpumask_var(priv->scratchpad.cpumask);
6325 
6326 	for (i = 0; i < priv->htb_max_qos_sqs; i++)
6327 		kfree(priv->htb_qos_sq_stats[i]);
6328 	kvfree(priv->htb_qos_sq_stats);
6329 
6330 	if (priv->mqprio_rl) {
6331 		mlx5e_mqprio_rl_cleanup(priv->mqprio_rl);
6332 		mlx5e_mqprio_rl_free(priv->mqprio_rl);
6333 	}
6334 
6335 	memset(priv, 0, sizeof(*priv));
6336 }
6337 
mlx5e_get_max_num_txqs(struct mlx5_core_dev * mdev,const struct mlx5e_profile * profile)6338 static unsigned int mlx5e_get_max_num_txqs(struct mlx5_core_dev *mdev,
6339 					   const struct mlx5e_profile *profile)
6340 {
6341 	unsigned int nch, ptp_txqs, qos_txqs;
6342 
6343 	nch = mlx5e_profile_max_num_channels(mdev, profile);
6344 
6345 	ptp_txqs = MLX5_CAP_GEN(mdev, ts_cqe_to_dest_cqn) &&
6346 		mlx5e_profile_feature_cap(profile, PTP_TX) ?
6347 		profile->max_tc : 0;
6348 
6349 	qos_txqs = mlx5_qos_is_supported(mdev) &&
6350 		mlx5e_profile_feature_cap(profile, QOS_HTB) ?
6351 		mlx5e_qos_max_leaf_nodes(mdev) : 0;
6352 
6353 	return nch * profile->max_tc + ptp_txqs + qos_txqs;
6354 }
6355 
mlx5e_get_max_num_rxqs(struct mlx5_core_dev * mdev,const struct mlx5e_profile * profile)6356 static unsigned int mlx5e_get_max_num_rxqs(struct mlx5_core_dev *mdev,
6357 					   const struct mlx5e_profile *profile)
6358 {
6359 	return mlx5e_profile_max_num_channels(mdev, profile);
6360 }
6361 
6362 struct net_device *
mlx5e_create_netdev(struct mlx5_core_dev * mdev,const struct mlx5e_profile * profile)6363 mlx5e_create_netdev(struct mlx5_core_dev *mdev, const struct mlx5e_profile *profile)
6364 {
6365 	struct net_device *netdev;
6366 	unsigned int txqs, rxqs;
6367 	int err;
6368 
6369 	txqs = mlx5e_get_max_num_txqs(mdev, profile);
6370 	rxqs = mlx5e_get_max_num_rxqs(mdev, profile);
6371 
6372 	netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv), txqs, rxqs);
6373 	if (!netdev) {
6374 		mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
6375 		return NULL;
6376 	}
6377 
6378 	err = mlx5e_priv_init(netdev_priv(netdev), profile, netdev, mdev);
6379 	if (err) {
6380 		mlx5_core_err(mdev, "mlx5e_priv_init failed, err=%d\n", err);
6381 		goto err_free_netdev;
6382 	}
6383 
6384 	netif_carrier_off(netdev);
6385 	netif_tx_disable(netdev);
6386 	dev_net_set(netdev, mlx5_core_net(mdev));
6387 
6388 	return netdev;
6389 
6390 err_free_netdev:
6391 	free_netdev(netdev);
6392 
6393 	return NULL;
6394 }
6395 
mlx5e_update_features(struct net_device * netdev)6396 static void mlx5e_update_features(struct net_device *netdev)
6397 {
6398 	if (netdev->reg_state != NETREG_REGISTERED)
6399 		return; /* features will be updated on netdev registration */
6400 
6401 	rtnl_lock();
6402 	netdev_lock(netdev);
6403 	netdev_update_features(netdev);
6404 	netdev_unlock(netdev);
6405 	rtnl_unlock();
6406 }
6407 
mlx5e_reset_channels(struct net_device * netdev)6408 static void mlx5e_reset_channels(struct net_device *netdev)
6409 {
6410 	netdev_reset_tc(netdev);
6411 }
6412 
mlx5e_attach_netdev(struct mlx5e_priv * priv)6413 int mlx5e_attach_netdev(struct mlx5e_priv *priv)
6414 {
6415 	const bool need_lock = priv->netdev->reg_state == NETREG_REGISTERED;
6416 	const struct mlx5e_profile *profile = priv->profile;
6417 	int max_nch;
6418 	int err;
6419 
6420 	clear_bit(MLX5E_STATE_DESTROYING, &priv->state);
6421 	if (priv->fs)
6422 		mlx5e_fs_set_state_destroy(priv->fs,
6423 					   !test_bit(MLX5E_STATE_DESTROYING, &priv->state));
6424 
6425 	/* Validate the max_wqe_size_sq capability. */
6426 	if (WARN_ON_ONCE(mlx5e_get_max_sq_wqebbs(priv->mdev) < MLX5E_MAX_TX_WQEBBS)) {
6427 		mlx5_core_warn(priv->mdev, "MLX5E: Max SQ WQEBBs firmware capability: %u, needed %u\n",
6428 			       mlx5e_get_max_sq_wqebbs(priv->mdev), (unsigned int)MLX5E_MAX_TX_WQEBBS);
6429 		return -EIO;
6430 	}
6431 
6432 	/* max number of channels may have changed */
6433 	max_nch = mlx5e_calc_max_nch(priv->mdev, priv->netdev, profile);
6434 	if (priv->channels.params.num_channels > max_nch) {
6435 		mlx5_core_warn(priv->mdev, "MLX5E: Reducing number of channels to %d\n", max_nch);
6436 		/* Reducing the number of channels - RXFH has to be reset, and
6437 		 * mlx5e_num_channels_changed below will build the RQT.
6438 		 */
6439 		priv->netdev->priv_flags &= ~IFF_RXFH_CONFIGURED;
6440 		priv->channels.params.num_channels = max_nch;
6441 		if (priv->channels.params.mqprio.mode == TC_MQPRIO_MODE_CHANNEL) {
6442 			mlx5_core_warn(priv->mdev, "MLX5E: Disabling MQPRIO channel mode\n");
6443 			mlx5e_params_mqprio_reset(&priv->channels.params);
6444 		}
6445 	}
6446 	if (max_nch != priv->max_nch) {
6447 		mlx5_core_warn(priv->mdev,
6448 			       "MLX5E: Updating max number of channels from %u to %u\n",
6449 			       priv->max_nch, max_nch);
6450 		priv->max_nch = max_nch;
6451 	}
6452 
6453 	/* 1. Set the real number of queues in the kernel the first time.
6454 	 * 2. Set our default XPS cpumask.
6455 	 * 3. Build the RQT.
6456 	 *
6457 	 * Locking is required by netif_set_real_num_*_queues in case the
6458 	 * netdev has been registered by this point (if this function was called
6459 	 * in the reload or resume flow).
6460 	 */
6461 	if (need_lock) {
6462 		rtnl_lock();
6463 		netdev_lock(priv->netdev);
6464 	}
6465 	err = mlx5e_num_channels_changed(priv);
6466 	if (need_lock) {
6467 		netdev_unlock(priv->netdev);
6468 		rtnl_unlock();
6469 	}
6470 	if (err)
6471 		goto out;
6472 
6473 	err = profile->init_tx(priv);
6474 	if (err)
6475 		goto out;
6476 
6477 	err = profile->init_rx(priv);
6478 	if (err)
6479 		goto err_cleanup_tx;
6480 
6481 	if (profile->enable)
6482 		profile->enable(priv);
6483 
6484 	mlx5e_update_features(priv->netdev);
6485 
6486 	return 0;
6487 
6488 err_cleanup_tx:
6489 	profile->cleanup_tx(priv);
6490 
6491 out:
6492 	mlx5e_reset_channels(priv->netdev);
6493 	set_bit(MLX5E_STATE_DESTROYING, &priv->state);
6494 	if (priv->fs)
6495 		mlx5e_fs_set_state_destroy(priv->fs,
6496 					   !test_bit(MLX5E_STATE_DESTROYING, &priv->state));
6497 	cancel_work_sync(&priv->update_stats_work);
6498 	return err;
6499 }
6500 
mlx5e_detach_netdev(struct mlx5e_priv * priv)6501 void mlx5e_detach_netdev(struct mlx5e_priv *priv)
6502 {
6503 	const struct mlx5e_profile *profile = priv->profile;
6504 
6505 	set_bit(MLX5E_STATE_DESTROYING, &priv->state);
6506 	if (priv->fs)
6507 		mlx5e_fs_set_state_destroy(priv->fs,
6508 					   !test_bit(MLX5E_STATE_DESTROYING, &priv->state));
6509 
6510 	if (profile->disable)
6511 		profile->disable(priv);
6512 	flush_workqueue(priv->wq);
6513 
6514 	profile->cleanup_rx(priv);
6515 	profile->cleanup_tx(priv);
6516 	mlx5e_reset_channels(priv->netdev);
6517 	cancel_work_sync(&priv->update_stats_work);
6518 }
6519 
6520 static int
mlx5e_netdev_init_profile(struct net_device * netdev,struct mlx5_core_dev * mdev,const struct mlx5e_profile * new_profile,void * new_ppriv)6521 mlx5e_netdev_init_profile(struct net_device *netdev, struct mlx5_core_dev *mdev,
6522 			  const struct mlx5e_profile *new_profile, void *new_ppriv)
6523 {
6524 	struct mlx5e_priv *priv = netdev_priv(netdev);
6525 	int err;
6526 
6527 	err = mlx5e_priv_init(priv, new_profile, netdev, mdev);
6528 	if (err) {
6529 		mlx5_core_err(mdev, "mlx5e_priv_init failed, err=%d\n", err);
6530 		return err;
6531 	}
6532 	netif_carrier_off(netdev);
6533 	priv->profile = new_profile;
6534 	priv->ppriv = new_ppriv;
6535 	err = new_profile->init(priv->mdev, priv->netdev);
6536 	if (err)
6537 		goto priv_cleanup;
6538 
6539 	return 0;
6540 
6541 priv_cleanup:
6542 	mlx5e_priv_cleanup(priv);
6543 	return err;
6544 }
6545 
6546 static int
mlx5e_netdev_attach_profile(struct net_device * netdev,struct mlx5_core_dev * mdev,const struct mlx5e_profile * new_profile,void * new_ppriv)6547 mlx5e_netdev_attach_profile(struct net_device *netdev, struct mlx5_core_dev *mdev,
6548 			    const struct mlx5e_profile *new_profile, void *new_ppriv)
6549 {
6550 	struct mlx5e_priv *priv = netdev_priv(netdev);
6551 	int err;
6552 
6553 	err = mlx5e_netdev_init_profile(netdev, mdev, new_profile, new_ppriv);
6554 	if (err)
6555 		return err;
6556 
6557 	err = mlx5e_attach_netdev(priv);
6558 	if (err)
6559 		goto profile_cleanup;
6560 	return err;
6561 
6562 profile_cleanup:
6563 	new_profile->cleanup(priv);
6564 	mlx5e_priv_cleanup(priv);
6565 	return err;
6566 }
6567 
mlx5e_netdev_change_profile(struct mlx5e_priv * priv,const struct mlx5e_profile * new_profile,void * new_ppriv)6568 int mlx5e_netdev_change_profile(struct mlx5e_priv *priv,
6569 				const struct mlx5e_profile *new_profile, void *new_ppriv)
6570 {
6571 	const struct mlx5e_profile *orig_profile = priv->profile;
6572 	struct net_device *netdev = priv->netdev;
6573 	struct mlx5_core_dev *mdev = priv->mdev;
6574 	void *orig_ppriv = priv->ppriv;
6575 	int err, rollback_err;
6576 
6577 	/* cleanup old profile */
6578 	mlx5e_detach_netdev(priv);
6579 	priv->profile->cleanup(priv);
6580 	mlx5e_priv_cleanup(priv);
6581 
6582 	if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6583 		mlx5e_netdev_init_profile(netdev, mdev, new_profile, new_ppriv);
6584 		set_bit(MLX5E_STATE_DESTROYING, &priv->state);
6585 		return -EIO;
6586 	}
6587 
6588 	err = mlx5e_netdev_attach_profile(netdev, mdev, new_profile, new_ppriv);
6589 	if (err) { /* roll back to original profile */
6590 		netdev_warn(netdev, "%s: new profile init failed, %d\n", __func__, err);
6591 		goto rollback;
6592 	}
6593 
6594 	return 0;
6595 
6596 rollback:
6597 	rollback_err = mlx5e_netdev_attach_profile(netdev, mdev, orig_profile, orig_ppriv);
6598 	if (rollback_err)
6599 		netdev_err(netdev, "%s: failed to rollback to orig profile, %d\n",
6600 			   __func__, rollback_err);
6601 	return err;
6602 }
6603 
mlx5e_netdev_attach_nic_profile(struct mlx5e_priv * priv)6604 void mlx5e_netdev_attach_nic_profile(struct mlx5e_priv *priv)
6605 {
6606 	mlx5e_netdev_change_profile(priv, &mlx5e_nic_profile, NULL);
6607 }
6608 
mlx5e_destroy_netdev(struct mlx5e_priv * priv)6609 void mlx5e_destroy_netdev(struct mlx5e_priv *priv)
6610 {
6611 	struct net_device *netdev = priv->netdev;
6612 
6613 	mlx5e_priv_cleanup(priv);
6614 	free_netdev(netdev);
6615 }
6616 
_mlx5e_resume(struct auxiliary_device * adev)6617 static int _mlx5e_resume(struct auxiliary_device *adev)
6618 {
6619 	struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
6620 	struct mlx5e_dev *mlx5e_dev = auxiliary_get_drvdata(adev);
6621 	struct mlx5e_priv *priv = mlx5e_dev->priv;
6622 	struct net_device *netdev = priv->netdev;
6623 	struct mlx5_core_dev *mdev = edev->mdev;
6624 	struct mlx5_core_dev *pos, *to;
6625 	int err, i;
6626 
6627 	if (netif_device_present(netdev))
6628 		return 0;
6629 
6630 	mlx5_sd_for_each_dev(i, mdev, pos) {
6631 		err = mlx5e_create_mdev_resources(pos, true);
6632 		if (err)
6633 			goto err_destroy_mdev_res;
6634 	}
6635 
6636 	err = mlx5e_attach_netdev(priv);
6637 	if (err)
6638 		goto err_destroy_mdev_res;
6639 
6640 	return 0;
6641 
6642 err_destroy_mdev_res:
6643 	to = pos;
6644 	mlx5_sd_for_each_dev_to(i, mdev, to, pos)
6645 		mlx5e_destroy_mdev_resources(pos);
6646 	return err;
6647 }
6648 
mlx5e_resume(struct auxiliary_device * adev)6649 static int mlx5e_resume(struct auxiliary_device *adev)
6650 {
6651 	struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
6652 	struct mlx5_core_dev *mdev = edev->mdev;
6653 	struct auxiliary_device *actual_adev;
6654 	int err;
6655 
6656 	err = mlx5_sd_init(mdev);
6657 	if (err)
6658 		return err;
6659 
6660 	actual_adev = mlx5_sd_get_adev(mdev, adev, edev->idx);
6661 	if (actual_adev)
6662 		return _mlx5e_resume(actual_adev);
6663 	return 0;
6664 }
6665 
_mlx5e_suspend(struct auxiliary_device * adev,bool pre_netdev_reg)6666 static int _mlx5e_suspend(struct auxiliary_device *adev, bool pre_netdev_reg)
6667 {
6668 	struct mlx5e_dev *mlx5e_dev = auxiliary_get_drvdata(adev);
6669 	struct mlx5e_priv *priv = mlx5e_dev->priv;
6670 	struct net_device *netdev = priv->netdev;
6671 	struct mlx5_core_dev *mdev = priv->mdev;
6672 	struct mlx5_core_dev *pos;
6673 	int i;
6674 
6675 	if (!pre_netdev_reg && !netif_device_present(netdev)) {
6676 		if (test_bit(MLX5E_STATE_DESTROYING, &priv->state))
6677 			mlx5_sd_for_each_dev(i, mdev, pos)
6678 				mlx5e_destroy_mdev_resources(pos);
6679 		return -ENODEV;
6680 	}
6681 
6682 	mlx5e_detach_netdev(priv);
6683 	mlx5_sd_for_each_dev(i, mdev, pos)
6684 		mlx5e_destroy_mdev_resources(pos);
6685 
6686 	return 0;
6687 }
6688 
mlx5e_suspend(struct auxiliary_device * adev,pm_message_t state)6689 static int mlx5e_suspend(struct auxiliary_device *adev, pm_message_t state)
6690 {
6691 	struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
6692 	struct mlx5_core_dev *mdev = edev->mdev;
6693 	struct auxiliary_device *actual_adev;
6694 	int err = 0;
6695 
6696 	actual_adev = mlx5_sd_get_adev(mdev, adev, edev->idx);
6697 	if (actual_adev)
6698 		err = _mlx5e_suspend(actual_adev, false);
6699 
6700 	mlx5_sd_cleanup(mdev);
6701 	return err;
6702 }
6703 
_mlx5e_probe(struct auxiliary_device * adev)6704 static int _mlx5e_probe(struct auxiliary_device *adev)
6705 {
6706 	struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
6707 	const struct mlx5e_profile *profile = &mlx5e_nic_profile;
6708 	struct mlx5_core_dev *mdev = edev->mdev;
6709 	struct mlx5e_dev *mlx5e_dev;
6710 	struct net_device *netdev;
6711 	struct mlx5e_priv *priv;
6712 	int err;
6713 
6714 	mlx5e_dev = mlx5e_create_devlink(&adev->dev, mdev);
6715 	if (IS_ERR(mlx5e_dev))
6716 		return PTR_ERR(mlx5e_dev);
6717 	auxiliary_set_drvdata(adev, mlx5e_dev);
6718 
6719 	err = mlx5e_devlink_port_register(mlx5e_dev, mdev);
6720 	if (err) {
6721 		mlx5_core_err(mdev, "mlx5e_devlink_port_register failed, %d\n", err);
6722 		goto err_devlink_unregister;
6723 	}
6724 
6725 	netdev = mlx5e_create_netdev(mdev, profile);
6726 	if (!netdev) {
6727 		mlx5_core_err(mdev, "mlx5e_create_netdev failed\n");
6728 		err = -ENOMEM;
6729 		goto err_devlink_port_unregister;
6730 	}
6731 	SET_NETDEV_DEVLINK_PORT(netdev, &mlx5e_dev->dl_port);
6732 
6733 	mlx5e_build_nic_netdev(netdev);
6734 
6735 	priv = netdev_priv(netdev);
6736 	mlx5e_dev->priv = priv;
6737 
6738 	priv->profile = profile;
6739 	priv->ppriv = NULL;
6740 
6741 	err = profile->init(mdev, netdev);
6742 	if (err) {
6743 		mlx5_core_err(mdev, "mlx5e_nic_profile init failed, %d\n", err);
6744 		goto err_destroy_netdev;
6745 	}
6746 
6747 	err = _mlx5e_resume(adev);
6748 	if (err) {
6749 		mlx5_core_err(mdev, "_mlx5e_resume failed, %d\n", err);
6750 		goto err_profile_cleanup;
6751 	}
6752 
6753 	err = register_netdev(netdev);
6754 	if (err) {
6755 		mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
6756 		goto err_resume;
6757 	}
6758 
6759 	mlx5e_dcbnl_init_app(priv);
6760 	mlx5_core_uplink_netdev_set(mdev, netdev);
6761 	mlx5e_params_print_info(mdev, &priv->channels.params);
6762 	return 0;
6763 
6764 err_resume:
6765 	_mlx5e_suspend(adev, true);
6766 err_profile_cleanup:
6767 	profile->cleanup(priv);
6768 err_destroy_netdev:
6769 	mlx5e_destroy_netdev(priv);
6770 err_devlink_port_unregister:
6771 	mlx5e_devlink_port_unregister(mlx5e_dev);
6772 err_devlink_unregister:
6773 	mlx5e_destroy_devlink(mlx5e_dev);
6774 	return err;
6775 }
6776 
mlx5e_probe(struct auxiliary_device * adev,const struct auxiliary_device_id * id)6777 static int mlx5e_probe(struct auxiliary_device *adev,
6778 		       const struct auxiliary_device_id *id)
6779 {
6780 	struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
6781 	struct mlx5_core_dev *mdev = edev->mdev;
6782 	struct auxiliary_device *actual_adev;
6783 	int err;
6784 
6785 	err = mlx5_sd_init(mdev);
6786 	if (err)
6787 		return err;
6788 
6789 	actual_adev = mlx5_sd_get_adev(mdev, adev, edev->idx);
6790 	if (actual_adev)
6791 		return _mlx5e_probe(actual_adev);
6792 	return 0;
6793 }
6794 
_mlx5e_remove(struct auxiliary_device * adev)6795 static void _mlx5e_remove(struct auxiliary_device *adev)
6796 {
6797 	struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
6798 	struct mlx5e_dev *mlx5e_dev = auxiliary_get_drvdata(adev);
6799 	struct mlx5e_priv *priv = mlx5e_dev->priv;
6800 	struct mlx5_core_dev *mdev = edev->mdev;
6801 
6802 	mlx5_core_uplink_netdev_set(mdev, NULL);
6803 	mlx5e_dcbnl_delete_app(priv);
6804 	/* When unload driver, the netdev is in registered state
6805 	 * if it's from legacy mode. If from switchdev mode, it
6806 	 * is already unregistered before changing to NIC profile.
6807 	 */
6808 	if (priv->netdev->reg_state == NETREG_REGISTERED) {
6809 		mlx5e_psp_unregister(priv);
6810 		unregister_netdev(priv->netdev);
6811 		_mlx5e_suspend(adev, false);
6812 	} else {
6813 		struct mlx5_core_dev *pos;
6814 		int i;
6815 
6816 		if (test_bit(MLX5E_STATE_DESTROYING, &priv->state))
6817 			mlx5_sd_for_each_dev(i, mdev, pos)
6818 				mlx5e_destroy_mdev_resources(pos);
6819 		else
6820 			_mlx5e_suspend(adev, true);
6821 	}
6822 	/* Avoid cleanup if profile rollback failed. */
6823 	if (priv->profile)
6824 		priv->profile->cleanup(priv);
6825 	mlx5e_destroy_netdev(priv);
6826 	mlx5e_devlink_port_unregister(mlx5e_dev);
6827 	mlx5e_destroy_devlink(mlx5e_dev);
6828 }
6829 
mlx5e_remove(struct auxiliary_device * adev)6830 static void mlx5e_remove(struct auxiliary_device *adev)
6831 {
6832 	struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
6833 	struct mlx5_core_dev *mdev = edev->mdev;
6834 	struct auxiliary_device *actual_adev;
6835 
6836 	actual_adev = mlx5_sd_get_adev(mdev, adev, edev->idx);
6837 	if (actual_adev)
6838 		_mlx5e_remove(actual_adev);
6839 
6840 	mlx5_sd_cleanup(mdev);
6841 }
6842 
6843 static const struct auxiliary_device_id mlx5e_id_table[] = {
6844 	{ .name = MLX5_ADEV_NAME ".eth", },
6845 	{},
6846 };
6847 
6848 MODULE_DEVICE_TABLE(auxiliary, mlx5e_id_table);
6849 
6850 static struct auxiliary_driver mlx5e_driver = {
6851 	.name = "eth",
6852 	.probe = mlx5e_probe,
6853 	.remove = mlx5e_remove,
6854 	.suspend = mlx5e_suspend,
6855 	.resume = mlx5e_resume,
6856 	.id_table = mlx5e_id_table,
6857 };
6858 
mlx5e_init(void)6859 int mlx5e_init(void)
6860 {
6861 	int ret;
6862 
6863 	mlx5e_build_ptys2ethtool_map();
6864 	ret = auxiliary_driver_register(&mlx5e_driver);
6865 	if (ret)
6866 		return ret;
6867 
6868 	ret = mlx5e_rep_init();
6869 	if (ret)
6870 		auxiliary_driver_unregister(&mlx5e_driver);
6871 	return ret;
6872 }
6873 
mlx5e_cleanup(void)6874 void mlx5e_cleanup(void)
6875 {
6876 	mlx5e_rep_cleanup();
6877 	auxiliary_driver_unregister(&mlx5e_driver);
6878 }
6879