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