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