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