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