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