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