xref: /freebsd/sys/dev/mlx5/mlx5_en/mlx5_en_main.c (revision f0a0420d)
1 /*-
2  * Copyright (c) 2015-2021 Mellanox Technologies. All rights reserved.
3  * Copyright (c) 2022 NVIDIA corporation & affiliates.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include "opt_kern_tls.h"
28 #include "opt_rss.h"
29 #include "opt_ratelimit.h"
30 
31 #include <dev/mlx5/mlx5_en/en.h>
32 
33 #include <sys/eventhandler.h>
34 #include <sys/sockio.h>
35 #include <machine/atomic.h>
36 
37 #include <net/debugnet.h>
38 
39 static int mlx5e_get_wqe_sz(struct mlx5e_priv *priv, u32 *wqe_sz, u32 *nsegs);
40 static if_snd_tag_query_t mlx5e_ul_snd_tag_query;
41 static if_snd_tag_free_t mlx5e_ul_snd_tag_free;
42 
43 struct mlx5e_channel_param {
44 	struct mlx5e_rq_param rq;
45 	struct mlx5e_sq_param sq;
46 	struct mlx5e_cq_param rx_cq;
47 	struct mlx5e_cq_param tx_cq;
48 };
49 
50 struct media {
51 	u32	subtype;
52 	u64	baudrate;
53 };
54 
55 static const struct media mlx5e_mode_table[MLX5E_LINK_SPEEDS_NUMBER] =
56 {
57 	[MLX5E_1000BASE_CX_SGMII] = {
58 		.subtype = IFM_1000_CX_SGMII,
59 		.baudrate = IF_Mbps(1000ULL),
60 	},
61 	[MLX5E_1000BASE_KX] = {
62 		.subtype = IFM_1000_KX,
63 		.baudrate = IF_Mbps(1000ULL),
64 	},
65 	[MLX5E_10GBASE_CX4] = {
66 		.subtype = IFM_10G_CX4,
67 		.baudrate = IF_Gbps(10ULL),
68 	},
69 	[MLX5E_10GBASE_KX4] = {
70 		.subtype = IFM_10G_KX4,
71 		.baudrate = IF_Gbps(10ULL),
72 	},
73 	[MLX5E_10GBASE_KR] = {
74 		.subtype = IFM_10G_KR,
75 		.baudrate = IF_Gbps(10ULL),
76 	},
77 	[MLX5E_20GBASE_KR2] = {
78 		.subtype = IFM_20G_KR2,
79 		.baudrate = IF_Gbps(20ULL),
80 	},
81 	[MLX5E_40GBASE_CR4] = {
82 		.subtype = IFM_40G_CR4,
83 		.baudrate = IF_Gbps(40ULL),
84 	},
85 	[MLX5E_40GBASE_KR4] = {
86 		.subtype = IFM_40G_KR4,
87 		.baudrate = IF_Gbps(40ULL),
88 	},
89 	[MLX5E_56GBASE_R4] = {
90 		.subtype = IFM_56G_R4,
91 		.baudrate = IF_Gbps(56ULL),
92 	},
93 	[MLX5E_10GBASE_CR] = {
94 		.subtype = IFM_10G_CR1,
95 		.baudrate = IF_Gbps(10ULL),
96 	},
97 	[MLX5E_10GBASE_SR] = {
98 		.subtype = IFM_10G_SR,
99 		.baudrate = IF_Gbps(10ULL),
100 	},
101 	[MLX5E_10GBASE_ER_LR] = {
102 		.subtype = IFM_10G_ER,
103 		.baudrate = IF_Gbps(10ULL),
104 	},
105 	[MLX5E_40GBASE_SR4] = {
106 		.subtype = IFM_40G_SR4,
107 		.baudrate = IF_Gbps(40ULL),
108 	},
109 	[MLX5E_40GBASE_LR4_ER4] = {
110 		.subtype = IFM_40G_LR4,
111 		.baudrate = IF_Gbps(40ULL),
112 	},
113 	[MLX5E_100GBASE_CR4] = {
114 		.subtype = IFM_100G_CR4,
115 		.baudrate = IF_Gbps(100ULL),
116 	},
117 	[MLX5E_100GBASE_SR4] = {
118 		.subtype = IFM_100G_SR4,
119 		.baudrate = IF_Gbps(100ULL),
120 	},
121 	[MLX5E_100GBASE_KR4] = {
122 		.subtype = IFM_100G_KR4,
123 		.baudrate = IF_Gbps(100ULL),
124 	},
125 	[MLX5E_100GBASE_LR4] = {
126 		.subtype = IFM_100G_LR4,
127 		.baudrate = IF_Gbps(100ULL),
128 	},
129 	[MLX5E_100BASE_TX] = {
130 		.subtype = IFM_100_TX,
131 		.baudrate = IF_Mbps(100ULL),
132 	},
133 	[MLX5E_1000BASE_T] = {
134 		.subtype = IFM_1000_T,
135 		.baudrate = IF_Mbps(1000ULL),
136 	},
137 	[MLX5E_10GBASE_T] = {
138 		.subtype = IFM_10G_T,
139 		.baudrate = IF_Gbps(10ULL),
140 	},
141 	[MLX5E_25GBASE_CR] = {
142 		.subtype = IFM_25G_CR,
143 		.baudrate = IF_Gbps(25ULL),
144 	},
145 	[MLX5E_25GBASE_KR] = {
146 		.subtype = IFM_25G_KR,
147 		.baudrate = IF_Gbps(25ULL),
148 	},
149 	[MLX5E_25GBASE_SR] = {
150 		.subtype = IFM_25G_SR,
151 		.baudrate = IF_Gbps(25ULL),
152 	},
153 	[MLX5E_50GBASE_CR2] = {
154 		.subtype = IFM_50G_CR2,
155 		.baudrate = IF_Gbps(50ULL),
156 	},
157 	[MLX5E_50GBASE_KR2] = {
158 		.subtype = IFM_50G_KR2,
159 		.baudrate = IF_Gbps(50ULL),
160 	},
161 	[MLX5E_50GBASE_KR4] = {
162 		.subtype = IFM_50G_KR4,
163 		.baudrate = IF_Gbps(50ULL),
164 	},
165 };
166 
167 static const struct media mlx5e_ext_mode_table[MLX5E_EXT_LINK_SPEEDS_NUMBER][MLX5E_CABLE_TYPE_NUMBER] =
168 {
169 	/**/
170 	[MLX5E_SGMII_100M][MLX5E_CABLE_TYPE_UNKNOWN] = {
171 		.subtype = IFM_100_SGMII,
172 		.baudrate = IF_Mbps(100),
173 	},
174 
175 	/**/
176 	[MLX5E_1000BASE_X_SGMII][MLX5E_CABLE_TYPE_UNKNOWN] = {
177 		.subtype = IFM_1000_CX,
178 		.baudrate = IF_Mbps(1000),
179 	},
180 	[MLX5E_1000BASE_X_SGMII][MLX5E_CABLE_TYPE_OPTICAL_MODULE] = {
181 		.subtype = IFM_1000_SX,
182 		.baudrate = IF_Mbps(1000),
183 	},
184 
185 	/**/
186 	[MLX5E_5GBASE_R][MLX5E_CABLE_TYPE_UNKNOWN] = {
187 		.subtype = IFM_5000_KR,
188 		.baudrate = IF_Mbps(5000),
189 	},
190 	[MLX5E_5GBASE_R][MLX5E_CABLE_TYPE_TWISTED_PAIR] = {
191 		.subtype = IFM_5000_T,
192 		.baudrate = IF_Mbps(5000),
193 	},
194 
195 	/**/
196 	[MLX5E_10GBASE_XFI_XAUI_1][MLX5E_CABLE_TYPE_UNKNOWN] = {
197 		.subtype = IFM_10G_KR,
198 		.baudrate = IF_Gbps(10ULL),
199 	},
200 	[MLX5E_10GBASE_XFI_XAUI_1][MLX5E_CABLE_TYPE_PASSIVE_COPPER] = {
201 		.subtype = IFM_10G_CR1,
202 		.baudrate = IF_Gbps(10ULL),
203 	},
204 	[MLX5E_10GBASE_XFI_XAUI_1][MLX5E_CABLE_TYPE_OPTICAL_MODULE] = {
205 		.subtype = IFM_10G_SR,
206 		.baudrate = IF_Gbps(10ULL),
207 	},
208 
209 	/**/
210 	[MLX5E_40GBASE_XLAUI_4_XLPPI_4][MLX5E_CABLE_TYPE_UNKNOWN] = {
211 		.subtype = IFM_40G_KR4,
212 		.baudrate = IF_Gbps(40ULL),
213 	},
214 	[MLX5E_40GBASE_XLAUI_4_XLPPI_4][MLX5E_CABLE_TYPE_PASSIVE_COPPER] = {
215 		.subtype = IFM_40G_CR4,
216 		.baudrate = IF_Gbps(40ULL),
217 	},
218 	[MLX5E_40GBASE_XLAUI_4_XLPPI_4][MLX5E_CABLE_TYPE_OPTICAL_MODULE] = {
219 		.subtype = IFM_40G_SR4,
220 		.baudrate = IF_Gbps(40ULL),
221 	},
222 
223 	/**/
224 	[MLX5E_25GAUI_1_25GBASE_CR_KR][MLX5E_CABLE_TYPE_UNKNOWN] = {
225 		.subtype = IFM_25G_KR,
226 		.baudrate = IF_Gbps(25ULL),
227 	},
228 	[MLX5E_25GAUI_1_25GBASE_CR_KR][MLX5E_CABLE_TYPE_PASSIVE_COPPER] = {
229 		.subtype = IFM_25G_CR,
230 		.baudrate = IF_Gbps(25ULL),
231 	},
232 	[MLX5E_25GAUI_1_25GBASE_CR_KR][MLX5E_CABLE_TYPE_OPTICAL_MODULE] = {
233 		.subtype = IFM_25G_SR,
234 		.baudrate = IF_Gbps(25ULL),
235 	},
236 	[MLX5E_25GAUI_1_25GBASE_CR_KR][MLX5E_CABLE_TYPE_TWISTED_PAIR] = {
237 		.subtype = IFM_25G_T,
238 		.baudrate = IF_Gbps(25ULL),
239 	},
240 
241 	/**/
242 	[MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2][MLX5E_CABLE_TYPE_UNKNOWN] = {
243 		.subtype = IFM_50G_KR2,
244 		.baudrate = IF_Gbps(50ULL),
245 	},
246 	[MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2][MLX5E_CABLE_TYPE_PASSIVE_COPPER] = {
247 		.subtype = IFM_50G_CR2,
248 		.baudrate = IF_Gbps(50ULL),
249 	},
250 	[MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2][MLX5E_CABLE_TYPE_OPTICAL_MODULE] = {
251 		.subtype = IFM_50G_SR2,
252 		.baudrate = IF_Gbps(50ULL),
253 	},
254 
255 	/**/
256 	[MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR][MLX5E_CABLE_TYPE_UNKNOWN] = {
257 		.subtype = IFM_50G_KR_PAM4,
258 		.baudrate = IF_Gbps(50ULL),
259 	},
260 	[MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR][MLX5E_CABLE_TYPE_PASSIVE_COPPER] = {
261 		.subtype = IFM_50G_CP,
262 		.baudrate = IF_Gbps(50ULL),
263 	},
264 	[MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR][MLX5E_CABLE_TYPE_OPTICAL_MODULE] = {
265 		.subtype = IFM_50G_SR,
266 		.baudrate = IF_Gbps(50ULL),
267 	},
268 
269 	/**/
270 	[MLX5E_CAUI_4_100GBASE_CR4_KR4][MLX5E_CABLE_TYPE_UNKNOWN] = {
271 		.subtype = IFM_100G_KR4,
272 		.baudrate = IF_Gbps(100ULL),
273 	},
274 	[MLX5E_CAUI_4_100GBASE_CR4_KR4][MLX5E_CABLE_TYPE_PASSIVE_COPPER] = {
275 		.subtype = IFM_100G_CR4,
276 		.baudrate = IF_Gbps(100ULL),
277 	},
278 	[MLX5E_CAUI_4_100GBASE_CR4_KR4][MLX5E_CABLE_TYPE_OPTICAL_MODULE] = {
279 		.subtype = IFM_100G_SR4,
280 		.baudrate = IF_Gbps(100ULL),
281 	},
282 
283 	/**/
284 	[MLX5E_100GAUI_1_100GBASE_CR_KR][MLX5E_CABLE_TYPE_UNKNOWN] = {
285 		.subtype = IFM_100G_KR_PAM4,
286 		.baudrate = IF_Gbps(100ULL),
287 	},
288 	[MLX5E_100GAUI_1_100GBASE_CR_KR][MLX5E_CABLE_TYPE_PASSIVE_COPPER] = {
289 		.subtype = IFM_100G_CR_PAM4,
290 		.baudrate = IF_Gbps(100ULL),
291 	},
292 	[MLX5E_100GAUI_1_100GBASE_CR_KR][MLX5E_CABLE_TYPE_OPTICAL_MODULE] = {
293 		.subtype = IFM_100G_SR2,	/* XXX */
294 		.baudrate = IF_Gbps(100ULL),
295 	},
296 
297 	/**/
298 	[MLX5E_100GAUI_2_100GBASE_CR2_KR2][MLX5E_CABLE_TYPE_UNKNOWN] = {
299 		.subtype = IFM_100G_KR4,
300 		.baudrate = IF_Gbps(100ULL),
301 	},
302 	[MLX5E_100GAUI_2_100GBASE_CR2_KR2][MLX5E_CABLE_TYPE_PASSIVE_COPPER] = {
303 		.subtype = IFM_100G_CP2,
304 		.baudrate = IF_Gbps(100ULL),
305 	},
306 	[MLX5E_100GAUI_2_100GBASE_CR2_KR2][MLX5E_CABLE_TYPE_OPTICAL_MODULE] = {
307 		.subtype = IFM_100G_SR2,
308 		.baudrate = IF_Gbps(100ULL),
309 	},
310 
311 	/**/
312 	[MLX5E_200GAUI_2_200GBASE_CR2_KR2][MLX5E_CABLE_TYPE_UNKNOWN] = {
313 		.subtype = IFM_200G_KR4_PAM4,	/* XXX */
314 		.baudrate = IF_Gbps(200ULL),
315 	},
316 	[MLX5E_200GAUI_2_200GBASE_CR2_KR2][MLX5E_CABLE_TYPE_PASSIVE_COPPER] = {
317 		.subtype = IFM_200G_CR4_PAM4,	/* XXX */
318 		.baudrate = IF_Gbps(200ULL),
319 	},
320 	[MLX5E_200GAUI_2_200GBASE_CR2_KR2][MLX5E_CABLE_TYPE_OPTICAL_MODULE] = {
321 		.subtype = IFM_200G_SR4,	/* XXX */
322 		.baudrate = IF_Gbps(200ULL),
323 	},
324 
325 	/**/
326 	[MLX5E_200GAUI_4_200GBASE_CR4_KR4][MLX5E_CABLE_TYPE_UNKNOWN] = {
327 		.subtype = IFM_200G_KR4_PAM4,
328 		.baudrate = IF_Gbps(200ULL),
329 	},
330 	[MLX5E_200GAUI_4_200GBASE_CR4_KR4][MLX5E_CABLE_TYPE_PASSIVE_COPPER] = {
331 		.subtype = IFM_200G_CR4_PAM4,
332 		.baudrate = IF_Gbps(200ULL),
333 	},
334 	[MLX5E_200GAUI_4_200GBASE_CR4_KR4][MLX5E_CABLE_TYPE_OPTICAL_MODULE] = {
335 		.subtype = IFM_200G_SR4,
336 		.baudrate = IF_Gbps(200ULL),
337 	},
338 
339 	/**/
340 	[MLX5E_400GAUI_8][MLX5E_CABLE_TYPE_UNKNOWN] = {
341 		.subtype = IFM_400G_LR8,	/* XXX */
342 		.baudrate = IF_Gbps(400ULL),
343 	},
344 
345 	/**/
346 	[MLX5E_400GAUI_4_400GBASE_CR4_KR4][MLX5E_CABLE_TYPE_UNKNOWN] = {
347 		.subtype = IFM_400G_LR8,	/* XXX */
348 		.baudrate = IF_Gbps(400ULL),
349 	},
350 };
351 
352 static const struct if_snd_tag_sw mlx5e_ul_snd_tag_sw = {
353 	.snd_tag_query = mlx5e_ul_snd_tag_query,
354 	.snd_tag_free = mlx5e_ul_snd_tag_free,
355 	.type = IF_SND_TAG_TYPE_UNLIMITED
356 };
357 
358 DEBUGNET_DEFINE(mlx5_en);
359 
360 MALLOC_DEFINE(M_MLX5EN, "MLX5EN", "MLX5 Ethernet");
361 
362 static void
mlx5e_update_carrier(struct mlx5e_priv * priv)363 mlx5e_update_carrier(struct mlx5e_priv *priv)
364 {
365 	struct mlx5_core_dev *mdev = priv->mdev;
366 	u32 out[MLX5_ST_SZ_DW(ptys_reg)];
367 	u32 eth_proto_oper;
368 	int error;
369 	u8 i;
370 	u8 cable_type;
371 	u8 port_state;
372 	u8 is_er_type;
373 	bool ext;
374 	struct media media_entry = {};
375 
376 	port_state = mlx5_query_vport_state(mdev,
377 	    MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT, 0);
378 
379 	if (port_state == VPORT_STATE_UP) {
380 		priv->media_status_last |= IFM_ACTIVE;
381 	} else {
382 		priv->media_status_last &= ~IFM_ACTIVE;
383 		priv->media_active_last = IFM_ETHER;
384 		if_link_state_change(priv->ifp, LINK_STATE_DOWN);
385 		return;
386 	}
387 
388 	error = mlx5_query_port_ptys(mdev, out, sizeof(out),
389 	    MLX5_PTYS_EN, 1);
390 	if (error) {
391 		priv->media_active_last = IFM_ETHER;
392 		if_setbaudrate(priv->ifp, 1);
393 		mlx5_en_err(priv->ifp, "query port ptys failed: 0x%x\n",
394 		    error);
395 		return;
396 	}
397 
398 	ext = MLX5_CAP_PCAM_FEATURE(mdev, ptys_extended_ethernet);
399 	eth_proto_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext,
400 	    eth_proto_oper);
401 
402 	i = ilog2(eth_proto_oper);
403 
404 	if (ext) {
405 		error = mlx5_query_pddr_cable_type(mdev, 1, &cable_type);
406 		if (error != 0) {
407 			/* use fallback entry */
408 			media_entry = mlx5e_ext_mode_table[i][MLX5E_CABLE_TYPE_UNKNOWN];
409 
410 			mlx5_en_err(priv->ifp,
411 			    "query port pddr failed: %d\n", error);
412 		} else {
413 			media_entry = mlx5e_ext_mode_table[i][cable_type];
414 
415 			/* check if we should use fallback entry */
416 			if (media_entry.subtype == 0)
417 				media_entry = mlx5e_ext_mode_table[i][MLX5E_CABLE_TYPE_UNKNOWN];
418 		}
419 	} else {
420 		media_entry = mlx5e_mode_table[i];
421 	}
422 
423 	if (media_entry.subtype == 0) {
424 		mlx5_en_err(priv->ifp,
425 		    "Could not find operational media subtype\n");
426 		return;
427 	}
428 
429 	switch (media_entry.subtype) {
430 	case IFM_10G_ER:
431 		error = mlx5_query_pddr_range_info(mdev, 1, &is_er_type);
432 		if (error != 0) {
433 			mlx5_en_err(priv->ifp,
434 			    "query port pddr failed: %d\n", error);
435 		}
436 		if (error != 0 || is_er_type == 0)
437 			media_entry.subtype = IFM_10G_LR;
438 		break;
439 	case IFM_40G_LR4:
440 		error = mlx5_query_pddr_range_info(mdev, 1, &is_er_type);
441 		if (error != 0) {
442 			mlx5_en_err(priv->ifp,
443 			    "query port pddr failed: %d\n", error);
444 		}
445 		if (error == 0 && is_er_type != 0)
446 			media_entry.subtype = IFM_40G_ER4;
447 		break;
448 	}
449 	priv->media_active_last = media_entry.subtype | IFM_ETHER | IFM_FDX;
450 	if_setbaudrate(priv->ifp, media_entry.baudrate);
451 
452 	if_link_state_change(priv->ifp, LINK_STATE_UP);
453 }
454 
455 static void
mlx5e_media_status(if_t dev,struct ifmediareq * ifmr)456 mlx5e_media_status(if_t dev, struct ifmediareq *ifmr)
457 {
458 	struct mlx5e_priv *priv = if_getsoftc(dev);
459 
460 	ifmr->ifm_status = priv->media_status_last;
461 	ifmr->ifm_current = ifmr->ifm_active = priv->media_active_last |
462 	    (priv->params.rx_pauseframe_control ? IFM_ETH_RXPAUSE : 0) |
463 	    (priv->params.tx_pauseframe_control ? IFM_ETH_TXPAUSE : 0);
464 
465 }
466 
467 static u32
mlx5e_find_link_mode(u32 subtype,bool ext)468 mlx5e_find_link_mode(u32 subtype, bool ext)
469 {
470 	u32 link_mode = 0;
471 
472 	switch (subtype) {
473 	case 0:
474 		goto done;
475 	case IFM_10G_LR:
476 		subtype = IFM_10G_ER;
477 		break;
478 	case IFM_40G_ER4:
479 		subtype = IFM_40G_LR4;
480 		break;
481 	default:
482 		break;
483 	}
484 
485 	if (ext) {
486 		for (unsigned i = 0; i != MLX5E_EXT_LINK_SPEEDS_NUMBER; i++) {
487 			for (unsigned j = 0; j != MLX5E_CABLE_TYPE_NUMBER; j++) {
488 				if (mlx5e_ext_mode_table[i][j].subtype == subtype)
489 					link_mode |= MLX5E_PROT_MASK(i);
490 			}
491 		}
492 	} else {
493 		for (unsigned i = 0; i != MLX5E_LINK_SPEEDS_NUMBER; i++) {
494 			if (mlx5e_mode_table[i].subtype == subtype)
495 				link_mode |= MLX5E_PROT_MASK(i);
496 		}
497 	}
498 done:
499 	return (link_mode);
500 }
501 
502 static int
mlx5e_set_port_pause_and_pfc(struct mlx5e_priv * priv)503 mlx5e_set_port_pause_and_pfc(struct mlx5e_priv *priv)
504 {
505 	return (mlx5_set_port_pause_and_pfc(priv->mdev, 1,
506 	    priv->params.rx_pauseframe_control,
507 	    priv->params.tx_pauseframe_control,
508 	    priv->params.rx_priority_flow_control,
509 	    priv->params.tx_priority_flow_control));
510 }
511 
512 static int
mlx5e_set_port_pfc(struct mlx5e_priv * priv)513 mlx5e_set_port_pfc(struct mlx5e_priv *priv)
514 {
515 	int error;
516 
517 	if (priv->gone != 0) {
518 		error = -ENXIO;
519 	} else if (priv->params.rx_pauseframe_control ||
520 	    priv->params.tx_pauseframe_control) {
521 		mlx5_en_err(priv->ifp,
522 		    "Global pauseframes must be disabled before enabling PFC.\n");
523 		error = -EINVAL;
524 	} else {
525 		error = mlx5e_set_port_pause_and_pfc(priv);
526 	}
527 	return (error);
528 }
529 
530 static int
mlx5e_media_change(if_t dev)531 mlx5e_media_change(if_t dev)
532 {
533 	struct mlx5e_priv *priv = if_getsoftc(dev);
534 	struct mlx5_core_dev *mdev = priv->mdev;
535 	u32 eth_proto_cap;
536 	u32 link_mode;
537 	u32 out[MLX5_ST_SZ_DW(ptys_reg)];
538 	int was_opened;
539 	int locked;
540 	int error;
541 	bool ext;
542 
543 	locked = PRIV_LOCKED(priv);
544 	if (!locked)
545 		PRIV_LOCK(priv);
546 
547 	if (IFM_TYPE(priv->media.ifm_media) != IFM_ETHER) {
548 		error = EINVAL;
549 		goto done;
550 	}
551 
552 	error = mlx5_query_port_ptys(mdev, out, sizeof(out),
553 	    MLX5_PTYS_EN, 1);
554 	if (error != 0) {
555 		mlx5_en_err(dev, "Query port media capability failed\n");
556 		goto done;
557 	}
558 
559 	ext = MLX5_CAP_PCAM_FEATURE(mdev, ptys_extended_ethernet);
560 	link_mode = mlx5e_find_link_mode(IFM_SUBTYPE(priv->media.ifm_media), ext);
561 
562 	/* query supported capabilities */
563 	eth_proto_cap = MLX5_GET_ETH_PROTO(ptys_reg, out, ext,
564 	    eth_proto_capability);
565 
566 	/* check for autoselect */
567 	if (IFM_SUBTYPE(priv->media.ifm_media) == IFM_AUTO) {
568 		link_mode = eth_proto_cap;
569 		if (link_mode == 0) {
570 			mlx5_en_err(dev, "Port media capability is zero\n");
571 			error = EINVAL;
572 			goto done;
573 		}
574 	} else {
575 		link_mode = link_mode & eth_proto_cap;
576 		if (link_mode == 0) {
577 			mlx5_en_err(dev, "Not supported link mode requested\n");
578 			error = EINVAL;
579 			goto done;
580 		}
581 	}
582 	if (priv->media.ifm_media & (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE)) {
583 		/* check if PFC is enabled */
584 		if (priv->params.rx_priority_flow_control ||
585 		    priv->params.tx_priority_flow_control) {
586 			mlx5_en_err(dev, "PFC must be disabled before enabling global pauseframes.\n");
587 			error = EINVAL;
588 			goto done;
589 		}
590 	}
591 	/* update pauseframe control bits */
592 	priv->params.rx_pauseframe_control =
593 	    (priv->media.ifm_media & IFM_ETH_RXPAUSE) ? 1 : 0;
594 	priv->params.tx_pauseframe_control =
595 	    (priv->media.ifm_media & IFM_ETH_TXPAUSE) ? 1 : 0;
596 
597 	/* check if device is opened */
598 	was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
599 
600 	/* reconfigure the hardware */
601 	mlx5_set_port_status(mdev, MLX5_PORT_DOWN);
602 	mlx5_set_port_proto(mdev, link_mode, MLX5_PTYS_EN, ext);
603 	error = -mlx5e_set_port_pause_and_pfc(priv);
604 	if (was_opened)
605 		mlx5_set_port_status(mdev, MLX5_PORT_UP);
606 
607 done:
608 	if (!locked)
609 		PRIV_UNLOCK(priv);
610 	return (error);
611 }
612 
613 static void
mlx5e_update_carrier_work(struct work_struct * work)614 mlx5e_update_carrier_work(struct work_struct *work)
615 {
616 	struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
617 	    update_carrier_work);
618 
619 	PRIV_LOCK(priv);
620 	if (test_bit(MLX5E_STATE_OPENED, &priv->state))
621 		mlx5e_update_carrier(priv);
622 	PRIV_UNLOCK(priv);
623 }
624 
625 #define	MLX5E_PCIE_PERF_GET_64(a,b,c,d,e,f)    \
626 	s_debug->c = MLX5_GET64(mpcnt_reg, out, counter_set.f.c);
627 
628 #define	MLX5E_PCIE_PERF_GET_32(a,b,c,d,e,f)    \
629 	s_debug->c = MLX5_GET(mpcnt_reg, out, counter_set.f.c);
630 
631 static void
mlx5e_update_pcie_counters(struct mlx5e_priv * priv)632 mlx5e_update_pcie_counters(struct mlx5e_priv *priv)
633 {
634 	struct mlx5_core_dev *mdev = priv->mdev;
635 	struct mlx5e_port_stats_debug *s_debug = &priv->stats.port_stats_debug;
636 	const unsigned sz = MLX5_ST_SZ_BYTES(mpcnt_reg);
637 	void *out;
638 	void *in;
639 	int err;
640 
641 	/* allocate firmware request structures */
642 	in = mlx5_vzalloc(sz);
643 	out = mlx5_vzalloc(sz);
644 	if (in == NULL || out == NULL)
645 		goto free_out;
646 
647 	MLX5_SET(mpcnt_reg, in, grp, MLX5_PCIE_PERFORMANCE_COUNTERS_GROUP);
648 	err = mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_MPCNT, 0, 0);
649 	if (err != 0)
650 		goto free_out;
651 
652 	MLX5E_PCIE_PERFORMANCE_COUNTERS_64(MLX5E_PCIE_PERF_GET_64)
653 	MLX5E_PCIE_PERFORMANCE_COUNTERS_32(MLX5E_PCIE_PERF_GET_32)
654 
655 	MLX5_SET(mpcnt_reg, in, grp, MLX5_PCIE_TIMERS_AND_STATES_COUNTERS_GROUP);
656 	err = mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_MPCNT, 0, 0);
657 	if (err != 0)
658 		goto free_out;
659 
660 	MLX5E_PCIE_TIMERS_AND_STATES_COUNTERS_32(MLX5E_PCIE_PERF_GET_32)
661 
662 	MLX5_SET(mpcnt_reg, in, grp, MLX5_PCIE_LANE_COUNTERS_GROUP);
663 	err = mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_MPCNT, 0, 0);
664 	if (err != 0)
665 		goto free_out;
666 
667 	MLX5E_PCIE_LANE_COUNTERS_32(MLX5E_PCIE_PERF_GET_32)
668 
669 free_out:
670 	/* free firmware request structures */
671 	kvfree(in);
672 	kvfree(out);
673 }
674 
675 /*
676  * This function reads the physical port counters from the firmware
677  * using a pre-defined layout defined by various MLX5E_PPORT_XXX()
678  * macros. The output is converted from big-endian 64-bit values into
679  * host endian ones and stored in the "priv->stats.pport" structure.
680  */
681 static void
mlx5e_update_pport_counters(struct mlx5e_priv * priv)682 mlx5e_update_pport_counters(struct mlx5e_priv *priv)
683 {
684 	struct mlx5_core_dev *mdev = priv->mdev;
685 	struct mlx5e_pport_stats *s = &priv->stats.pport;
686 	struct mlx5e_port_stats_debug *s_debug = &priv->stats.port_stats_debug;
687 	u32 *in;
688 	u32 *out;
689 	const u64 *ptr;
690 	unsigned sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
691 	unsigned x;
692 	unsigned y;
693 	unsigned z;
694 
695 	/* allocate firmware request structures */
696 	in = mlx5_vzalloc(sz);
697 	out = mlx5_vzalloc(sz);
698 	if (in == NULL || out == NULL)
699 		goto free_out;
700 
701 	/*
702 	 * Get pointer to the 64-bit counter set which is located at a
703 	 * fixed offset in the output firmware request structure:
704 	 */
705 	ptr = (const uint64_t *)MLX5_ADDR_OF(ppcnt_reg, out, counter_set);
706 
707 	MLX5_SET(ppcnt_reg, in, local_port, 1);
708 
709 	/* read IEEE802_3 counter group using predefined counter layout */
710 	MLX5_SET(ppcnt_reg, in, grp, MLX5_IEEE_802_3_COUNTERS_GROUP);
711 	mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
712 	for (x = 0, y = MLX5E_PPORT_PER_PRIO_STATS_NUM;
713 	     x != MLX5E_PPORT_IEEE802_3_STATS_NUM; x++, y++)
714 		s->arg[y] = be64toh(ptr[x]);
715 
716 	/* read RFC2819 counter group using predefined counter layout */
717 	MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2819_COUNTERS_GROUP);
718 	mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
719 	for (x = 0; x != MLX5E_PPORT_RFC2819_STATS_NUM; x++, y++)
720 		s->arg[y] = be64toh(ptr[x]);
721 
722 	for (y = 0; x != MLX5E_PPORT_RFC2819_STATS_NUM +
723 	    MLX5E_PPORT_RFC2819_STATS_DEBUG_NUM; x++, y++)
724 		s_debug->arg[y] = be64toh(ptr[x]);
725 
726 	/* read RFC2863 counter group using predefined counter layout */
727 	MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2863_COUNTERS_GROUP);
728 	mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
729 	for (x = 0; x != MLX5E_PPORT_RFC2863_STATS_DEBUG_NUM; x++, y++)
730 		s_debug->arg[y] = be64toh(ptr[x]);
731 
732 	/* read physical layer stats counter group using predefined counter layout */
733 	MLX5_SET(ppcnt_reg, in, grp, MLX5_PHYSICAL_LAYER_COUNTERS_GROUP);
734 	mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
735 	for (x = 0; x != MLX5E_PPORT_PHYSICAL_LAYER_STATS_DEBUG_NUM; x++, y++)
736 		s_debug->arg[y] = be64toh(ptr[x]);
737 
738 	/* read Extended Ethernet counter group using predefined counter layout */
739 	MLX5_SET(ppcnt_reg, in, grp, MLX5_ETHERNET_EXTENDED_COUNTERS_GROUP);
740 	mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
741 	for (x = 0; x != MLX5E_PPORT_ETHERNET_EXTENDED_STATS_DEBUG_NUM; x++, y++)
742 		s_debug->arg[y] = be64toh(ptr[x]);
743 
744 	/* read Extended Statistical Group */
745 	if (MLX5_CAP_GEN(mdev, pcam_reg) &&
746 	    MLX5_CAP_PCAM_FEATURE(mdev, ppcnt_statistical_group) &&
747 	    MLX5_CAP_PCAM_FEATURE(mdev, per_lane_error_counters)) {
748 		/* read Extended Statistical counter group using predefined counter layout */
749 		MLX5_SET(ppcnt_reg, in, grp, MLX5_PHYSICAL_LAYER_STATISTICAL_GROUP);
750 		mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
751 
752 		for (x = 0; x != MLX5E_PPORT_STATISTICAL_DEBUG_NUM; x++, y++)
753 			s_debug->arg[y] = be64toh(ptr[x]);
754 	}
755 
756 	/* read PCIE counters */
757 	mlx5e_update_pcie_counters(priv);
758 
759 	/* read per-priority counters */
760 	MLX5_SET(ppcnt_reg, in, grp, MLX5_PER_PRIORITY_COUNTERS_GROUP);
761 
762 	/* iterate all the priorities */
763 	for (y = z = 0; z != MLX5E_PPORT_PER_PRIO_STATS_NUM_PRIO; z++) {
764 		MLX5_SET(ppcnt_reg, in, prio_tc, z);
765 		mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
766 
767 		/* read per priority stats counter group using predefined counter layout */
768 		for (x = 0; x != (MLX5E_PPORT_PER_PRIO_STATS_NUM /
769 		    MLX5E_PPORT_PER_PRIO_STATS_NUM_PRIO); x++, y++)
770 			s->arg[y] = be64toh(ptr[x]);
771 	}
772 
773 free_out:
774 	/* free firmware request structures */
775 	kvfree(in);
776 	kvfree(out);
777 }
778 
779 static void
mlx5e_grp_vnic_env_update_stats(struct mlx5e_priv * priv)780 mlx5e_grp_vnic_env_update_stats(struct mlx5e_priv *priv)
781 {
782 	u32 out[MLX5_ST_SZ_DW(query_vnic_env_out)] = {};
783 	u32 in[MLX5_ST_SZ_DW(query_vnic_env_in)] = {};
784 
785 	if (!MLX5_CAP_GEN(priv->mdev, nic_receive_steering_discard))
786 		return;
787 
788 	MLX5_SET(query_vnic_env_in, in, opcode,
789 	    MLX5_CMD_OP_QUERY_VNIC_ENV);
790 	MLX5_SET(query_vnic_env_in, in, op_mod, 0);
791 	MLX5_SET(query_vnic_env_in, in, other_vport, 0);
792 
793 	if (mlx5_cmd_exec(priv->mdev, in, sizeof(in), out, sizeof(out)) != 0)
794 		return;
795 
796 	priv->stats.vport.rx_steer_missed_packets =
797 	    MLX5_GET64(query_vnic_env_out, out,
798 	    vport_env.nic_receive_steering_discard);
799 }
800 
801 /*
802  * This function is called regularly to collect all statistics
803  * counters from the firmware. The values can be viewed through the
804  * sysctl interface. Execution is serialized using the priv's global
805  * configuration lock.
806  */
807 static void
mlx5e_update_stats_locked(struct mlx5e_priv * priv)808 mlx5e_update_stats_locked(struct mlx5e_priv *priv)
809 {
810 	struct mlx5_core_dev *mdev = priv->mdev;
811 	struct mlx5e_vport_stats *s = &priv->stats.vport;
812 	struct mlx5e_sq_stats *sq_stats;
813 	u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)];
814 	u32 *out;
815 	int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
816 	u64 tso_packets = 0;
817 	u64 tso_bytes = 0;
818 	u64 tx_queue_dropped = 0;
819 	u64 tx_defragged = 0;
820 	u64 tx_offload_none = 0;
821 	u64 lro_packets = 0;
822 	u64 lro_bytes = 0;
823 	u64 sw_lro_queued = 0;
824 	u64 sw_lro_flushed = 0;
825 	u64 rx_csum_none = 0;
826 	u64 rx_wqe_err = 0;
827 	u64 rx_packets = 0;
828 	u64 rx_bytes = 0;
829 	u64 rx_decrypted_error = 0;
830 	u64 rx_decrypted_ok = 0;
831 	u32 rx_out_of_buffer = 0;
832 	int error;
833 	int i;
834 	int j;
835 
836 	out = mlx5_vzalloc(outlen);
837 	if (out == NULL)
838 		goto free_out;
839 
840 	/* Collect firts the SW counters and then HW for consistency */
841 	for (i = 0; i < priv->params.num_channels; i++) {
842 		struct mlx5e_channel *pch = priv->channel + i;
843 		struct mlx5e_rq *rq = &pch->rq;
844 		struct mlx5e_rq_stats *rq_stats = &pch->rq.stats;
845 
846 		/* collect stats from LRO */
847 		rq_stats->sw_lro_queued = rq->lro.lro_queued;
848 		rq_stats->sw_lro_flushed = rq->lro.lro_flushed;
849 		sw_lro_queued += rq_stats->sw_lro_queued;
850 		sw_lro_flushed += rq_stats->sw_lro_flushed;
851 		lro_packets += rq_stats->lro_packets;
852 		lro_bytes += rq_stats->lro_bytes;
853 		rx_csum_none += rq_stats->csum_none;
854 		rx_wqe_err += rq_stats->wqe_err;
855 		rx_packets += rq_stats->packets;
856 		rx_bytes += rq_stats->bytes;
857 		rx_decrypted_error += rq_stats->decrypted_error_packets;
858 		rx_decrypted_ok += rq_stats->decrypted_ok_packets;
859 
860 		for (j = 0; j < priv->num_tc; j++) {
861 			sq_stats = &pch->sq[j].stats;
862 
863 			tso_packets += sq_stats->tso_packets;
864 			tso_bytes += sq_stats->tso_bytes;
865 			tx_queue_dropped += sq_stats->dropped;
866 			tx_queue_dropped += sq_stats->enobuf;
867 			tx_defragged += sq_stats->defragged;
868 			tx_offload_none += sq_stats->csum_offload_none;
869 		}
870 	}
871 
872 #ifdef RATELIMIT
873 	/* Collect statistics from all rate-limit queues */
874 	for (j = 0; j < priv->rl.param.tx_worker_threads_def; j++) {
875 		struct mlx5e_rl_worker *rlw = priv->rl.workers + j;
876 
877 		for (i = 0; i < priv->rl.param.tx_channels_per_worker_def; i++) {
878 			struct mlx5e_rl_channel *channel = rlw->channels + i;
879 			struct mlx5e_sq *sq = channel->sq;
880 
881 			if (sq == NULL)
882 				continue;
883 
884 			sq_stats = &sq->stats;
885 
886 			tso_packets += sq_stats->tso_packets;
887 			tso_bytes += sq_stats->tso_bytes;
888 			tx_queue_dropped += sq_stats->dropped;
889 			tx_queue_dropped += sq_stats->enobuf;
890 			tx_defragged += sq_stats->defragged;
891 			tx_offload_none += sq_stats->csum_offload_none;
892 		}
893 	}
894 #endif
895 
896 	/* update counters */
897 	s->tso_packets = tso_packets;
898 	s->tso_bytes = tso_bytes;
899 	s->tx_queue_dropped = tx_queue_dropped;
900 	s->tx_defragged = tx_defragged;
901 	s->lro_packets = lro_packets;
902 	s->lro_bytes = lro_bytes;
903 	s->sw_lro_queued = sw_lro_queued;
904 	s->sw_lro_flushed = sw_lro_flushed;
905 	s->rx_csum_none = rx_csum_none;
906 	s->rx_wqe_err = rx_wqe_err;
907 	s->rx_packets = rx_packets;
908 	s->rx_bytes = rx_bytes;
909 	s->rx_decrypted_error_packets = rx_decrypted_error;
910 	s->rx_decrypted_ok_packets = rx_decrypted_ok;
911 
912 	mlx5e_grp_vnic_env_update_stats(priv);
913 
914 	/* HW counters */
915 	memset(in, 0, sizeof(in));
916 
917 	MLX5_SET(query_vport_counter_in, in, opcode,
918 	    MLX5_CMD_OP_QUERY_VPORT_COUNTER);
919 	MLX5_SET(query_vport_counter_in, in, op_mod, 0);
920 	MLX5_SET(query_vport_counter_in, in, other_vport, 0);
921 
922 	memset(out, 0, outlen);
923 
924 	/* get number of out-of-buffer drops first */
925 	if (test_bit(MLX5E_STATE_OPENED, &priv->state) != 0 &&
926 	    mlx5_vport_query_out_of_rx_buffer(mdev, priv->counter_set_id,
927 	    &rx_out_of_buffer) == 0) {
928 		s->rx_out_of_buffer = rx_out_of_buffer;
929 	}
930 
931 	/* get port statistics */
932 	if (mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen) == 0) {
933 #define	MLX5_GET_CTR(out, x) \
934 	MLX5_GET64(query_vport_counter_out, out, x)
935 
936 		s->rx_error_packets =
937 		    MLX5_GET_CTR(out, received_errors.packets);
938 		s->rx_error_bytes =
939 		    MLX5_GET_CTR(out, received_errors.octets);
940 		s->tx_error_packets =
941 		    MLX5_GET_CTR(out, transmit_errors.packets);
942 		s->tx_error_bytes =
943 		    MLX5_GET_CTR(out, transmit_errors.octets);
944 
945 		s->rx_unicast_packets =
946 		    MLX5_GET_CTR(out, received_eth_unicast.packets);
947 		s->rx_unicast_bytes =
948 		    MLX5_GET_CTR(out, received_eth_unicast.octets);
949 		s->tx_unicast_packets =
950 		    MLX5_GET_CTR(out, transmitted_eth_unicast.packets);
951 		s->tx_unicast_bytes =
952 		    MLX5_GET_CTR(out, transmitted_eth_unicast.octets);
953 
954 		s->rx_multicast_packets =
955 		    MLX5_GET_CTR(out, received_eth_multicast.packets);
956 		s->rx_multicast_bytes =
957 		    MLX5_GET_CTR(out, received_eth_multicast.octets);
958 		s->tx_multicast_packets =
959 		    MLX5_GET_CTR(out, transmitted_eth_multicast.packets);
960 		s->tx_multicast_bytes =
961 		    MLX5_GET_CTR(out, transmitted_eth_multicast.octets);
962 
963 		s->rx_broadcast_packets =
964 		    MLX5_GET_CTR(out, received_eth_broadcast.packets);
965 		s->rx_broadcast_bytes =
966 		    MLX5_GET_CTR(out, received_eth_broadcast.octets);
967 		s->tx_broadcast_packets =
968 		    MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
969 		s->tx_broadcast_bytes =
970 		    MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
971 
972 		s->tx_packets = s->tx_unicast_packets +
973 		    s->tx_multicast_packets + s->tx_broadcast_packets;
974 		s->tx_bytes = s->tx_unicast_bytes + s->tx_multicast_bytes +
975 		    s->tx_broadcast_bytes;
976 
977 		/* Update calculated offload counters */
978 		s->tx_csum_offload = s->tx_packets - tx_offload_none;
979 		s->rx_csum_good = s->rx_packets - s->rx_csum_none;
980 	}
981 
982 	/* Get physical port counters */
983 	mlx5e_update_pport_counters(priv);
984 
985 	s->tx_jumbo_packets =
986 	    priv->stats.port_stats_debug.tx_stat_p1519to2047octets +
987 	    priv->stats.port_stats_debug.tx_stat_p2048to4095octets +
988 	    priv->stats.port_stats_debug.tx_stat_p4096to8191octets +
989 	    priv->stats.port_stats_debug.tx_stat_p8192to10239octets;
990 
991 free_out:
992 	kvfree(out);
993 
994 	/* Update diagnostics, if any */
995 	if (priv->params_ethtool.diag_pci_enable ||
996 	    priv->params_ethtool.diag_general_enable) {
997 		error = mlx5_core_get_diagnostics_full(mdev,
998 		    priv->params_ethtool.diag_pci_enable ? &priv->params_pci : NULL,
999 		    priv->params_ethtool.diag_general_enable ? &priv->params_general : NULL);
1000 		if (error != 0)
1001 			mlx5_en_err(priv->ifp,
1002 			    "Failed reading diagnostics: %d\n", error);
1003 	}
1004 
1005 	/* Update FEC, if any */
1006 	error = mlx5e_fec_update(priv);
1007 	if (error != 0 && error != EOPNOTSUPP) {
1008 		mlx5_en_err(priv->ifp,
1009 		    "Updating FEC failed: %d\n", error);
1010 	}
1011 
1012 	/* Update temperature, if any */
1013 	if (priv->params_ethtool.hw_num_temp != 0) {
1014 		error = mlx5e_hw_temperature_update(priv);
1015 		if (error != 0 && error != EOPNOTSUPP) {
1016 			mlx5_en_err(priv->ifp,
1017 			    "Updating temperature failed: %d\n", error);
1018 		}
1019 	}
1020 }
1021 
1022 static void
mlx5e_update_stats_work(struct work_struct * work)1023 mlx5e_update_stats_work(struct work_struct *work)
1024 {
1025 	struct mlx5e_priv *priv;
1026 
1027 	priv = container_of(work, struct mlx5e_priv, update_stats_work);
1028 	PRIV_LOCK(priv);
1029 	if (test_bit(MLX5E_STATE_OPENED, &priv->state) != 0 &&
1030 	    !test_bit(MLX5_INTERFACE_STATE_TEARDOWN, &priv->mdev->intf_state))
1031 		mlx5e_update_stats_locked(priv);
1032 	PRIV_UNLOCK(priv);
1033 }
1034 
1035 static void
mlx5e_update_stats(void * arg)1036 mlx5e_update_stats(void *arg)
1037 {
1038 	struct mlx5e_priv *priv = arg;
1039 
1040 	queue_work(priv->wq, &priv->update_stats_work);
1041 
1042 	callout_reset(&priv->watchdog, hz / 4, &mlx5e_update_stats, priv);
1043 }
1044 
1045 static void
mlx5e_async_event_sub(struct mlx5e_priv * priv,enum mlx5_dev_event event)1046 mlx5e_async_event_sub(struct mlx5e_priv *priv,
1047     enum mlx5_dev_event event)
1048 {
1049 	switch (event) {
1050 	case MLX5_DEV_EVENT_PORT_UP:
1051 	case MLX5_DEV_EVENT_PORT_DOWN:
1052 		queue_work(priv->wq, &priv->update_carrier_work);
1053 		break;
1054 
1055 	default:
1056 		break;
1057 	}
1058 }
1059 
1060 static void
mlx5e_async_event(struct mlx5_core_dev * mdev,void * vpriv,enum mlx5_dev_event event,unsigned long param)1061 mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
1062     enum mlx5_dev_event event, unsigned long param)
1063 {
1064 	struct mlx5e_priv *priv = vpriv;
1065 
1066 	mtx_lock(&priv->async_events_mtx);
1067 	if (test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state))
1068 		mlx5e_async_event_sub(priv, event);
1069 	mtx_unlock(&priv->async_events_mtx);
1070 }
1071 
1072 static void
mlx5e_enable_async_events(struct mlx5e_priv * priv)1073 mlx5e_enable_async_events(struct mlx5e_priv *priv)
1074 {
1075 	set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
1076 }
1077 
1078 static void
mlx5e_disable_async_events(struct mlx5e_priv * priv)1079 mlx5e_disable_async_events(struct mlx5e_priv *priv)
1080 {
1081 	mtx_lock(&priv->async_events_mtx);
1082 	clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
1083 	mtx_unlock(&priv->async_events_mtx);
1084 }
1085 
1086 static void mlx5e_calibration_callout(void *arg);
1087 static int mlx5e_calibration_duration = 20;
1088 static int mlx5e_fast_calibration = 1;
1089 static int mlx5e_normal_calibration = 30;
1090 
1091 static SYSCTL_NODE(_hw_mlx5, OID_AUTO, calibr, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1092     "MLX5 timestamp calibration parameters");
1093 
1094 SYSCTL_INT(_hw_mlx5_calibr, OID_AUTO, duration, CTLFLAG_RWTUN,
1095     &mlx5e_calibration_duration, 0,
1096     "Duration of initial calibration");
1097 SYSCTL_INT(_hw_mlx5_calibr, OID_AUTO, fast, CTLFLAG_RWTUN,
1098     &mlx5e_fast_calibration, 0,
1099     "Recalibration interval during initial calibration");
1100 SYSCTL_INT(_hw_mlx5_calibr, OID_AUTO, normal, CTLFLAG_RWTUN,
1101     &mlx5e_normal_calibration, 0,
1102     "Recalibration interval during normal operations");
1103 
1104 /*
1105  * Ignites the calibration process.
1106  */
1107 static void
mlx5e_reset_calibration_callout(struct mlx5e_priv * priv)1108 mlx5e_reset_calibration_callout(struct mlx5e_priv *priv)
1109 {
1110 
1111 	if (priv->clbr_done == 0)
1112 		mlx5e_calibration_callout(priv);
1113 	else
1114 		callout_reset_sbt_curcpu(&priv->tstmp_clbr, (priv->clbr_done <
1115 		    mlx5e_calibration_duration ? mlx5e_fast_calibration :
1116 		    mlx5e_normal_calibration) * SBT_1S, 0,
1117 		    mlx5e_calibration_callout, priv, C_DIRECT_EXEC);
1118 }
1119 
1120 static uint64_t
mlx5e_timespec2usec(const struct timespec * ts)1121 mlx5e_timespec2usec(const struct timespec *ts)
1122 {
1123 
1124 	return ((uint64_t)ts->tv_sec * 1000000000 + ts->tv_nsec);
1125 }
1126 
1127 static uint64_t
mlx5e_hw_clock(struct mlx5e_priv * priv)1128 mlx5e_hw_clock(struct mlx5e_priv *priv)
1129 {
1130 	struct mlx5_init_seg *iseg;
1131 	uint32_t hw_h, hw_h1, hw_l;
1132 
1133 	iseg = priv->mdev->iseg;
1134 	do {
1135 		hw_h = ioread32be(&iseg->internal_timer_h);
1136 		hw_l = ioread32be(&iseg->internal_timer_l);
1137 		hw_h1 = ioread32be(&iseg->internal_timer_h);
1138 	} while (hw_h1 != hw_h);
1139 	return (((uint64_t)hw_h << 32) | hw_l);
1140 }
1141 
1142 /*
1143  * The calibration callout, it runs either in the context of the
1144  * thread which enables calibration, or in callout.  It takes the
1145  * snapshot of system and adapter clocks, then advances the pointers to
1146  * the calibration point to allow rx path to read the consistent data
1147  * lockless.
1148  */
1149 static void
mlx5e_calibration_callout(void * arg)1150 mlx5e_calibration_callout(void *arg)
1151 {
1152 	struct mlx5e_priv *priv;
1153 	struct mlx5e_clbr_point *next, *curr;
1154 	struct timespec ts;
1155 	int clbr_curr_next;
1156 
1157 	priv = arg;
1158 	curr = &priv->clbr_points[priv->clbr_curr];
1159 	clbr_curr_next = priv->clbr_curr + 1;
1160 	if (clbr_curr_next >= nitems(priv->clbr_points))
1161 		clbr_curr_next = 0;
1162 	next = &priv->clbr_points[clbr_curr_next];
1163 
1164 	next->base_prev = curr->base_curr;
1165 	next->clbr_hw_prev = curr->clbr_hw_curr;
1166 
1167 	next->clbr_hw_curr = mlx5e_hw_clock(priv);
1168 	if (((next->clbr_hw_curr - curr->clbr_hw_curr) >> MLX5E_TSTMP_PREC) ==
1169 	    0) {
1170 		if (priv->clbr_done != 0) {
1171 			mlx5_en_err(priv->ifp,
1172 			    "HW failed tstmp frozen %#jx %#jx, disabling\n",
1173 			     next->clbr_hw_curr, curr->clbr_hw_prev);
1174 			priv->clbr_done = 0;
1175 		}
1176 		atomic_store_rel_int(&curr->clbr_gen, 0);
1177 		return;
1178 	}
1179 
1180 	nanouptime(&ts);
1181 	next->base_curr = mlx5e_timespec2usec(&ts);
1182 
1183 	curr->clbr_gen = 0;
1184 	atomic_thread_fence_rel();
1185 	priv->clbr_curr = clbr_curr_next;
1186 	atomic_store_rel_int(&next->clbr_gen, ++(priv->clbr_gen));
1187 
1188 	if (priv->clbr_done < mlx5e_calibration_duration)
1189 		priv->clbr_done++;
1190 	mlx5e_reset_calibration_callout(priv);
1191 }
1192 
1193 static const char *mlx5e_rq_stats_desc[] = {
1194 	MLX5E_RQ_STATS(MLX5E_STATS_DESC)
1195 };
1196 
1197 static int
mlx5e_create_rq(struct mlx5e_channel * c,struct mlx5e_rq_param * param,struct mlx5e_rq * rq)1198 mlx5e_create_rq(struct mlx5e_channel *c,
1199     struct mlx5e_rq_param *param,
1200     struct mlx5e_rq *rq)
1201 {
1202 	struct mlx5e_priv *priv = c->priv;
1203 	struct mlx5_core_dev *mdev = priv->mdev;
1204 	char buffer[16];
1205 	void *rqc = param->rqc;
1206 	void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
1207 	int wq_sz;
1208 	int err;
1209 	int i;
1210 	u32 nsegs, wqe_sz;
1211 
1212 	err = mlx5e_get_wqe_sz(priv, &wqe_sz, &nsegs);
1213 	if (err != 0)
1214 		goto done;
1215 
1216 	/* Create DMA descriptor TAG */
1217 	if ((err = -bus_dma_tag_create(
1218 	    bus_get_dma_tag(mdev->pdev->dev.bsddev),
1219 	    1,				/* any alignment */
1220 	    0,				/* no boundary */
1221 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1222 	    BUS_SPACE_MAXADDR,		/* highaddr */
1223 	    NULL, NULL,			/* filter, filterarg */
1224 	    nsegs * MLX5E_MAX_RX_BYTES,	/* maxsize */
1225 	    nsegs,			/* nsegments */
1226 	    nsegs * MLX5E_MAX_RX_BYTES,	/* maxsegsize */
1227 	    0,				/* flags */
1228 	    NULL, NULL,			/* lockfunc, lockfuncarg */
1229 	    &rq->dma_tag)))
1230 		goto done;
1231 
1232 	err = mlx5_wq_ll_create(mdev, &param->wq, rqc_wq, &rq->wq,
1233 	    &rq->wq_ctrl);
1234 	if (err)
1235 		goto err_free_dma_tag;
1236 
1237 	rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
1238 
1239 	err = mlx5e_get_wqe_sz(priv, &rq->wqe_sz, &rq->nsegs);
1240 	if (err != 0)
1241 		goto err_rq_wq_destroy;
1242 
1243 	wq_sz = mlx5_wq_ll_get_size(&rq->wq);
1244 
1245 	err = -tcp_lro_init_args(&rq->lro, priv->ifp, TCP_LRO_ENTRIES, wq_sz);
1246 	if (err)
1247 		goto err_rq_wq_destroy;
1248 
1249 	rq->mbuf = malloc_domainset(wq_sz * sizeof(rq->mbuf[0]), M_MLX5EN,
1250 	    mlx5_dev_domainset(mdev), M_WAITOK | M_ZERO);
1251 	for (i = 0; i != wq_sz; i++) {
1252 		struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
1253 		int j;
1254 
1255 		err = -bus_dmamap_create(rq->dma_tag, 0, &rq->mbuf[i].dma_map);
1256 		if (err != 0) {
1257 			while (i--)
1258 				bus_dmamap_destroy(rq->dma_tag, rq->mbuf[i].dma_map);
1259 			goto err_rq_mbuf_free;
1260 		}
1261 
1262 		/* set value for constant fields */
1263 		for (j = 0; j < rq->nsegs; j++)
1264 			wqe->data[j].lkey = cpu_to_be32(priv->mr.key);
1265 	}
1266 
1267 	INIT_WORK(&rq->dim.work, mlx5e_dim_work);
1268 	if (priv->params.rx_cq_moderation_mode < 2) {
1269 		rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_DISABLED;
1270 	} else {
1271 		void *cqc = container_of(param,
1272 		    struct mlx5e_channel_param, rq)->rx_cq.cqc;
1273 
1274 		switch (MLX5_GET(cqc, cqc, cq_period_mode)) {
1275 		case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
1276 			rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1277 			break;
1278 		case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
1279 			rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE;
1280 			break;
1281 		default:
1282 			rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_DISABLED;
1283 			break;
1284 		}
1285 	}
1286 
1287 	rq->ifp = priv->ifp;
1288 	rq->channel = c;
1289 	rq->ix = c->ix;
1290 
1291 	snprintf(buffer, sizeof(buffer), "rxstat%d", c->ix);
1292 	mlx5e_create_stats(&rq->stats.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
1293 	    buffer, mlx5e_rq_stats_desc, MLX5E_RQ_STATS_NUM,
1294 	    rq->stats.arg);
1295 	return (0);
1296 
1297 err_rq_mbuf_free:
1298 	free(rq->mbuf, M_MLX5EN);
1299 	tcp_lro_free(&rq->lro);
1300 err_rq_wq_destroy:
1301 	mlx5_wq_destroy(&rq->wq_ctrl);
1302 err_free_dma_tag:
1303 	bus_dma_tag_destroy(rq->dma_tag);
1304 done:
1305 	return (err);
1306 }
1307 
1308 static void
mlx5e_destroy_rq(struct mlx5e_rq * rq)1309 mlx5e_destroy_rq(struct mlx5e_rq *rq)
1310 {
1311 	int wq_sz;
1312 	int i;
1313 
1314 	/* destroy all sysctl nodes */
1315 	sysctl_ctx_free(&rq->stats.ctx);
1316 
1317 	/* free leftover LRO packets, if any */
1318 	tcp_lro_free(&rq->lro);
1319 
1320 	wq_sz = mlx5_wq_ll_get_size(&rq->wq);
1321 	for (i = 0; i != wq_sz; i++) {
1322 		if (rq->mbuf[i].mbuf != NULL) {
1323 			bus_dmamap_unload(rq->dma_tag, rq->mbuf[i].dma_map);
1324 			m_freem(rq->mbuf[i].mbuf);
1325 		}
1326 		bus_dmamap_destroy(rq->dma_tag, rq->mbuf[i].dma_map);
1327 	}
1328 	free(rq->mbuf, M_MLX5EN);
1329 	mlx5_wq_destroy(&rq->wq_ctrl);
1330 	bus_dma_tag_destroy(rq->dma_tag);
1331 }
1332 
1333 static int
mlx5e_enable_rq(struct mlx5e_rq * rq,struct mlx5e_rq_param * param)1334 mlx5e_enable_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param)
1335 {
1336 	struct mlx5e_channel *c = rq->channel;
1337 	struct mlx5e_priv *priv = c->priv;
1338 	struct mlx5_core_dev *mdev = priv->mdev;
1339 	void *in;
1340 	void *rqc;
1341 	void *wq;
1342 	int inlen;
1343 	int err;
1344 	u8 ts_format;
1345 
1346 	inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
1347 	    sizeof(u64) * rq->wq_ctrl.buf.npages;
1348 	in = mlx5_vzalloc(inlen);
1349 	if (in == NULL)
1350 		return (-ENOMEM);
1351 
1352 	ts_format = mlx5_get_rq_default_ts(mdev);
1353 	rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
1354 	wq = MLX5_ADDR_OF(rqc, rqc, wq);
1355 
1356 	memcpy(rqc, param->rqc, sizeof(param->rqc));
1357 
1358 	MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1359 	MLX5_SET(rqc, rqc, ts_format, ts_format);
1360 	MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1361 	if (priv->counter_set_id >= 0)
1362 		MLX5_SET(rqc, rqc, counter_set_id, priv->counter_set_id);
1363 	MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
1364 	    MLX5_ADAPTER_PAGE_SHIFT);
1365 	MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
1366 
1367 	mlx5_fill_page_array(&rq->wq_ctrl.buf,
1368 	    (__be64 *) MLX5_ADDR_OF(wq, wq, pas));
1369 
1370 	err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
1371 
1372 	kvfree(in);
1373 
1374 	return (err);
1375 }
1376 
1377 static int
mlx5e_modify_rq(struct mlx5e_rq * rq,int curr_state,int next_state)1378 mlx5e_modify_rq(struct mlx5e_rq *rq, int curr_state, int next_state)
1379 {
1380 	struct mlx5e_channel *c = rq->channel;
1381 	struct mlx5e_priv *priv = c->priv;
1382 	struct mlx5_core_dev *mdev = priv->mdev;
1383 
1384 	void *in;
1385 	void *rqc;
1386 	int inlen;
1387 	int err;
1388 
1389 	inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
1390 	in = mlx5_vzalloc(inlen);
1391 	if (in == NULL)
1392 		return (-ENOMEM);
1393 
1394 	rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
1395 
1396 	MLX5_SET(modify_rq_in, in, rqn, rq->rqn);
1397 	MLX5_SET(modify_rq_in, in, rq_state, curr_state);
1398 	MLX5_SET(rqc, rqc, state, next_state);
1399 
1400 	err = mlx5_core_modify_rq(mdev, in, inlen);
1401 
1402 	kvfree(in);
1403 
1404 	return (err);
1405 }
1406 
1407 static void
mlx5e_disable_rq(struct mlx5e_rq * rq)1408 mlx5e_disable_rq(struct mlx5e_rq *rq)
1409 {
1410 	struct mlx5e_channel *c = rq->channel;
1411 	struct mlx5e_priv *priv = c->priv;
1412 	struct mlx5_core_dev *mdev = priv->mdev;
1413 
1414 	mlx5_core_destroy_rq(mdev, rq->rqn);
1415 }
1416 
1417 static int
mlx5e_open_rq(struct mlx5e_channel * c,struct mlx5e_rq_param * param,struct mlx5e_rq * rq)1418 mlx5e_open_rq(struct mlx5e_channel *c,
1419     struct mlx5e_rq_param *param,
1420     struct mlx5e_rq *rq)
1421 {
1422 	int err;
1423 
1424 	err = mlx5e_create_rq(c, param, rq);
1425 	if (err)
1426 		return (err);
1427 
1428 	/* set CQN in RQ parameters */
1429 	MLX5_SET(rqc, param->rqc, cqn, c->rq.cq.mcq.cqn);
1430 
1431 	err = mlx5e_enable_rq(rq, param);
1432 	if (err)
1433 		goto err_destroy_rq;
1434 
1435 	err = mlx5e_modify_rq(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
1436 	if (err)
1437 		goto err_disable_rq;
1438 
1439 	c->rq.enabled = 1;
1440 
1441 	return (0);
1442 
1443 err_disable_rq:
1444 	mlx5e_disable_rq(rq);
1445 err_destroy_rq:
1446 	mlx5e_destroy_rq(rq);
1447 
1448 	return (err);
1449 }
1450 
1451 static void
mlx5e_close_rq(struct mlx5e_rq * rq)1452 mlx5e_close_rq(struct mlx5e_rq *rq)
1453 {
1454 	mtx_lock(&rq->mtx);
1455 	rq->enabled = 0;
1456 	callout_stop(&rq->watchdog);
1457 	mtx_unlock(&rq->mtx);
1458 
1459 	mlx5e_modify_rq(rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
1460 }
1461 
1462 static void
mlx5e_close_rq_wait(struct mlx5e_rq * rq)1463 mlx5e_close_rq_wait(struct mlx5e_rq *rq)
1464 {
1465 
1466 	mlx5e_disable_rq(rq);
1467 	mlx5e_close_cq(&rq->cq);
1468 	cancel_work_sync(&rq->dim.work);
1469 	mlx5e_destroy_rq(rq);
1470 }
1471 
1472 /*
1473  * What is a drop RQ and why is it needed?
1474  *
1475  * The RSS indirection table, also called the RQT, selects the
1476  * destination RQ based on the receive queue number, RQN. The RQT is
1477  * frequently referred to by flow steering rules to distribute traffic
1478  * among multiple RQs. The problem is that the RQs cannot be destroyed
1479  * before the RQT referring them is destroyed too. Further, TLS RX
1480  * rules may still be referring to the RQT even if the link went
1481  * down. Because there is no magic RQN for dropping packets, we create
1482  * a dummy RQ, also called drop RQ, which sole purpose is to drop all
1483  * received packets. When the link goes down this RQN is filled in all
1484  * RQT entries, of the main RQT, so the real RQs which are about to be
1485  * destroyed can be released and the TLS RX rules can be sustained.
1486  */
1487 static void
mlx5e_open_drop_rq_comp(struct mlx5_core_cq * mcq __unused,struct mlx5_eqe * eqe __unused)1488 mlx5e_open_drop_rq_comp(struct mlx5_core_cq *mcq __unused, struct mlx5_eqe *eqe __unused)
1489 {
1490 }
1491 
1492 static int
mlx5e_open_drop_rq(struct mlx5e_priv * priv,struct mlx5e_rq * drop_rq)1493 mlx5e_open_drop_rq(struct mlx5e_priv *priv,
1494     struct mlx5e_rq *drop_rq)
1495 {
1496 	struct mlx5e_cq_param param_cq = {};
1497 	struct mlx5e_rq_param param_rq = {};
1498 	void *rqc_wq = MLX5_ADDR_OF(rqc, param_rq.rqc, wq);
1499 	int err;
1500 
1501 	/* set channel pointer */
1502 	drop_rq->channel = priv->channel;
1503 
1504 	/* set basic CQ parameters needed */
1505 	MLX5_SET(cqc, param_cq.cqc, log_cq_size, 0);
1506 	MLX5_SET(cqc, param_cq.cqc, uar_page, priv->mdev->priv.uar->index);
1507 
1508 	/* open receive completion queue */
1509 	err = mlx5e_open_cq(priv, &param_cq, &drop_rq->cq,
1510 	    &mlx5e_open_drop_rq_comp, 0);
1511 	if (err)
1512 		goto err_done;
1513 
1514 	/* set basic WQ parameters needed */
1515 	MLX5_SET(wq, rqc_wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1516 	MLX5_SET(wq, rqc_wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1517 	MLX5_SET(wq, rqc_wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe) + sizeof(struct mlx5_wqe_data_seg)));
1518 	MLX5_SET(wq, rqc_wq, log_wq_sz, 0);
1519 	MLX5_SET(wq, rqc_wq, pd, priv->pdn);
1520 
1521 	param_rq.wq.linear = 1;
1522 
1523 	err = mlx5_wq_ll_create(priv->mdev, &param_rq.wq, rqc_wq, &drop_rq->wq,
1524 	    &drop_rq->wq_ctrl);
1525 	if (err)
1526 		goto err_close_cq;
1527 
1528 	/* set CQN in RQ parameters */
1529 	MLX5_SET(rqc, param_rq.rqc, cqn, drop_rq->cq.mcq.cqn);
1530 
1531 	err = mlx5e_enable_rq(drop_rq, &param_rq);
1532 	if (err)
1533 		goto err_wq_destroy;
1534 
1535 	err = mlx5e_modify_rq(drop_rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
1536 	if (err)
1537 		goto err_disable_rq;
1538 
1539 	return (err);
1540 
1541 err_disable_rq:
1542 	mlx5e_disable_rq(drop_rq);
1543 err_wq_destroy:
1544 	mlx5_wq_destroy(&drop_rq->wq_ctrl);
1545 err_close_cq:
1546 	mlx5e_close_cq(&drop_rq->cq);
1547 err_done:
1548 	return (err);
1549 }
1550 
1551 static void
mlx5e_close_drop_rq(struct mlx5e_rq * drop_rq)1552 mlx5e_close_drop_rq(struct mlx5e_rq *drop_rq)
1553 {
1554 	mlx5e_modify_rq(drop_rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
1555 	mlx5e_disable_rq(drop_rq);
1556 	mlx5_wq_destroy(&drop_rq->wq_ctrl);
1557 	mlx5e_close_cq(&drop_rq->cq);
1558 }
1559 
1560 void
mlx5e_free_sq_db(struct mlx5e_sq * sq)1561 mlx5e_free_sq_db(struct mlx5e_sq *sq)
1562 {
1563 	int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1564 	int x;
1565 
1566 	for (x = 0; x != wq_sz; x++) {
1567 		if (sq->mbuf[x].mbuf != NULL) {
1568 			bus_dmamap_unload(sq->dma_tag, sq->mbuf[x].dma_map);
1569 			m_freem(sq->mbuf[x].mbuf);
1570 		}
1571 		if (sq->mbuf[x].mst != NULL) {
1572 			m_snd_tag_rele(sq->mbuf[x].mst);
1573 			sq->mbuf[x].mst = NULL;
1574 		}
1575 		bus_dmamap_destroy(sq->dma_tag, sq->mbuf[x].dma_map);
1576 	}
1577 	free(sq->mbuf, M_MLX5EN);
1578 }
1579 
1580 int
mlx5e_alloc_sq_db(struct mlx5e_sq * sq)1581 mlx5e_alloc_sq_db(struct mlx5e_sq *sq)
1582 {
1583 	int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1584 	int err;
1585 	int x;
1586 
1587 	sq->mbuf = malloc_domainset(wq_sz * sizeof(sq->mbuf[0]), M_MLX5EN,
1588 	    mlx5_dev_domainset(sq->priv->mdev), M_WAITOK | M_ZERO);
1589 
1590 	/* Create DMA descriptor MAPs */
1591 	for (x = 0; x != wq_sz; x++) {
1592 		err = -bus_dmamap_create(sq->dma_tag, 0, &sq->mbuf[x].dma_map);
1593 		if (err != 0) {
1594 			while (x--)
1595 				bus_dmamap_destroy(sq->dma_tag, sq->mbuf[x].dma_map);
1596 			free(sq->mbuf, M_MLX5EN);
1597 			return (err);
1598 		}
1599 	}
1600 	return (0);
1601 }
1602 
1603 static const char *mlx5e_sq_stats_desc[] = {
1604 	MLX5E_SQ_STATS(MLX5E_STATS_DESC)
1605 };
1606 
1607 void
mlx5e_update_sq_inline(struct mlx5e_sq * sq)1608 mlx5e_update_sq_inline(struct mlx5e_sq *sq)
1609 {
1610 	sq->max_inline = sq->priv->params.tx_max_inline;
1611 	sq->min_inline_mode = sq->priv->params.tx_min_inline_mode;
1612 
1613 	/*
1614 	 * Check if trust state is DSCP or if inline mode is NONE which
1615 	 * indicates CX-5 or newer hardware.
1616 	 */
1617 	if (sq->priv->params_ethtool.trust_state != MLX5_QPTS_TRUST_PCP ||
1618 	    sq->min_inline_mode == MLX5_INLINE_MODE_NONE) {
1619 		if (MLX5_CAP_ETH(sq->priv->mdev, wqe_vlan_insert))
1620 			sq->min_insert_caps = MLX5E_INSERT_VLAN | MLX5E_INSERT_NON_VLAN;
1621 		else
1622 			sq->min_insert_caps = MLX5E_INSERT_NON_VLAN;
1623 	} else {
1624 		sq->min_insert_caps = 0;
1625 	}
1626 }
1627 
1628 static void
mlx5e_refresh_sq_inline_sub(struct mlx5e_priv * priv,struct mlx5e_channel * c)1629 mlx5e_refresh_sq_inline_sub(struct mlx5e_priv *priv, struct mlx5e_channel *c)
1630 {
1631 	int i;
1632 
1633 	for (i = 0; i != priv->num_tc; i++) {
1634 		mtx_lock(&c->sq[i].lock);
1635 		mlx5e_update_sq_inline(&c->sq[i]);
1636 		mtx_unlock(&c->sq[i].lock);
1637 	}
1638 }
1639 
1640 void
mlx5e_refresh_sq_inline(struct mlx5e_priv * priv)1641 mlx5e_refresh_sq_inline(struct mlx5e_priv *priv)
1642 {
1643 	int i;
1644 
1645 	/* check if channels are closed */
1646 	if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
1647 		return;
1648 
1649 	for (i = 0; i < priv->params.num_channels; i++)
1650 		mlx5e_refresh_sq_inline_sub(priv, &priv->channel[i]);
1651 }
1652 
1653 static int
mlx5e_create_sq(struct mlx5e_channel * c,int tc,struct mlx5e_sq_param * param,struct mlx5e_sq * sq)1654 mlx5e_create_sq(struct mlx5e_channel *c,
1655     int tc,
1656     struct mlx5e_sq_param *param,
1657     struct mlx5e_sq *sq)
1658 {
1659 	struct mlx5e_priv *priv = c->priv;
1660 	struct mlx5_core_dev *mdev = priv->mdev;
1661 	char buffer[16];
1662 	void *sqc = param->sqc;
1663 	void *sqc_wq = MLX5_ADDR_OF(sqc, sqc, wq);
1664 	int err;
1665 
1666 	/* Create DMA descriptor TAG */
1667 	if ((err = -bus_dma_tag_create(
1668 	    bus_get_dma_tag(mdev->pdev->dev.bsddev),
1669 	    1,				/* any alignment */
1670 	    0,				/* no boundary */
1671 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1672 	    BUS_SPACE_MAXADDR,		/* highaddr */
1673 	    NULL, NULL,			/* filter, filterarg */
1674 	    MLX5E_MAX_TX_PAYLOAD_SIZE,	/* maxsize */
1675 	    MLX5E_MAX_TX_MBUF_FRAGS,	/* nsegments */
1676 	    MLX5E_MAX_TX_MBUF_SIZE,	/* maxsegsize */
1677 	    0,				/* flags */
1678 	    NULL, NULL,			/* lockfunc, lockfuncarg */
1679 	    &sq->dma_tag)))
1680 		goto done;
1681 
1682 	sq->mkey_be = cpu_to_be32(priv->mr.key);
1683 	sq->ifp = priv->ifp;
1684 	sq->priv = priv;
1685 	sq->tc = tc;
1686 
1687 	err = mlx5_wq_cyc_create(mdev, &param->wq, sqc_wq, &sq->wq,
1688 	    &sq->wq_ctrl);
1689 	if (err)
1690 		goto err_free_dma_tag;
1691 
1692 	sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
1693 
1694 	err = mlx5e_alloc_sq_db(sq);
1695 	if (err)
1696 		goto err_sq_wq_destroy;
1697 
1698 	mlx5e_update_sq_inline(sq);
1699 
1700 	snprintf(buffer, sizeof(buffer), "txstat%dtc%d", c->ix, tc);
1701 	mlx5e_create_stats(&sq->stats.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
1702 	    buffer, mlx5e_sq_stats_desc, MLX5E_SQ_STATS_NUM,
1703 	    sq->stats.arg);
1704 
1705 	return (0);
1706 
1707 err_sq_wq_destroy:
1708 	mlx5_wq_destroy(&sq->wq_ctrl);
1709 
1710 err_free_dma_tag:
1711 	bus_dma_tag_destroy(sq->dma_tag);
1712 done:
1713 	return (err);
1714 }
1715 
1716 static void
mlx5e_destroy_sq(struct mlx5e_sq * sq)1717 mlx5e_destroy_sq(struct mlx5e_sq *sq)
1718 {
1719 	/* destroy all sysctl nodes */
1720 	sysctl_ctx_free(&sq->stats.ctx);
1721 
1722 	mlx5e_free_sq_db(sq);
1723 	mlx5_wq_destroy(&sq->wq_ctrl);
1724 	bus_dma_tag_destroy(sq->dma_tag);
1725 }
1726 
1727 int
mlx5e_enable_sq(struct mlx5e_sq * sq,struct mlx5e_sq_param * param,const struct mlx5_sq_bfreg * bfreg,int tis_num)1728 mlx5e_enable_sq(struct mlx5e_sq *sq, struct mlx5e_sq_param *param,
1729     const struct mlx5_sq_bfreg *bfreg, int tis_num)
1730 {
1731 	void *in;
1732 	void *sqc;
1733 	void *wq;
1734 	int inlen;
1735 	int err;
1736 	u8 ts_format;
1737 
1738 	inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1739 	    sizeof(u64) * sq->wq_ctrl.buf.npages;
1740 	in = mlx5_vzalloc(inlen);
1741 	if (in == NULL)
1742 		return (-ENOMEM);
1743 
1744 	sq->uar_map = bfreg->map;
1745 
1746 	ts_format = mlx5_get_sq_default_ts(sq->priv->mdev);
1747 	sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1748 	wq = MLX5_ADDR_OF(sqc, sqc, wq);
1749 
1750 	memcpy(sqc, param->sqc, sizeof(param->sqc));
1751 
1752 	MLX5_SET(sqc, sqc, tis_num_0, tis_num);
1753 	MLX5_SET(sqc, sqc, cqn, sq->cq.mcq.cqn);
1754 	MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1755 	MLX5_SET(sqc, sqc, ts_format, ts_format);
1756 	MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1757 	MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1758 	MLX5_SET(sqc, sqc, allow_swp, 1);
1759 
1760 	MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1761 	MLX5_SET(wq, wq, uar_page, bfreg->index);
1762 	MLX5_SET(wq, wq, log_wq_pg_sz, sq->wq_ctrl.buf.page_shift -
1763 	    MLX5_ADAPTER_PAGE_SHIFT);
1764 	MLX5_SET64(wq, wq, dbr_addr, sq->wq_ctrl.db.dma);
1765 
1766 	mlx5_fill_page_array(&sq->wq_ctrl.buf,
1767 	    (__be64 *) MLX5_ADDR_OF(wq, wq, pas));
1768 
1769 	err = mlx5_core_create_sq(sq->priv->mdev, in, inlen, &sq->sqn);
1770 
1771 	kvfree(in);
1772 
1773 	return (err);
1774 }
1775 
1776 int
mlx5e_modify_sq(struct mlx5e_sq * sq,int curr_state,int next_state)1777 mlx5e_modify_sq(struct mlx5e_sq *sq, int curr_state, int next_state)
1778 {
1779 	void *in;
1780 	void *sqc;
1781 	int inlen;
1782 	int err;
1783 
1784 	inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1785 	in = mlx5_vzalloc(inlen);
1786 	if (in == NULL)
1787 		return (-ENOMEM);
1788 
1789 	sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1790 
1791 	MLX5_SET(modify_sq_in, in, sqn, sq->sqn);
1792 	MLX5_SET(modify_sq_in, in, sq_state, curr_state);
1793 	MLX5_SET(sqc, sqc, state, next_state);
1794 
1795 	err = mlx5_core_modify_sq(sq->priv->mdev, in, inlen);
1796 
1797 	kvfree(in);
1798 
1799 	return (err);
1800 }
1801 
1802 void
mlx5e_disable_sq(struct mlx5e_sq * sq)1803 mlx5e_disable_sq(struct mlx5e_sq *sq)
1804 {
1805 
1806 	mlx5_core_destroy_sq(sq->priv->mdev, sq->sqn);
1807 }
1808 
1809 static int
mlx5e_open_sq(struct mlx5e_channel * c,int tc,struct mlx5e_sq_param * param,struct mlx5e_sq * sq)1810 mlx5e_open_sq(struct mlx5e_channel *c,
1811     int tc,
1812     struct mlx5e_sq_param *param,
1813     struct mlx5e_sq *sq)
1814 {
1815 	int err;
1816 
1817 	sq->cev_factor = c->priv->params_ethtool.tx_completion_fact;
1818 
1819 	/* ensure the TX completion event factor is not zero */
1820 	if (sq->cev_factor == 0)
1821 		sq->cev_factor = 1;
1822 
1823 	err = mlx5e_create_sq(c, tc, param, sq);
1824 	if (err)
1825 		return (err);
1826 
1827 	err = mlx5e_enable_sq(sq, param, &c->bfreg, c->priv->tisn[tc]);
1828 	if (err)
1829 		goto err_destroy_sq;
1830 
1831 	err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY);
1832 	if (err)
1833 		goto err_disable_sq;
1834 
1835 	WRITE_ONCE(sq->running, 1);
1836 
1837 	return (0);
1838 
1839 err_disable_sq:
1840 	mlx5e_disable_sq(sq);
1841 err_destroy_sq:
1842 	mlx5e_destroy_sq(sq);
1843 
1844 	return (err);
1845 }
1846 
1847 static void
mlx5e_sq_send_nops_locked(struct mlx5e_sq * sq,int can_sleep)1848 mlx5e_sq_send_nops_locked(struct mlx5e_sq *sq, int can_sleep)
1849 {
1850 	/* fill up remainder with NOPs */
1851 	while (sq->cev_counter != 0) {
1852 		while (!mlx5e_sq_has_room_for(sq, 1)) {
1853 			if (can_sleep != 0) {
1854 				mtx_unlock(&sq->lock);
1855 				msleep(4);
1856 				mtx_lock(&sq->lock);
1857 			} else {
1858 				goto done;
1859 			}
1860 		}
1861 		/* send a single NOP */
1862 		mlx5e_send_nop(sq, 1);
1863 		atomic_thread_fence_rel();
1864 	}
1865 done:
1866 	mlx5e_tx_notify_hw(sq, false);
1867 }
1868 
1869 void
mlx5e_sq_cev_timeout(void * arg)1870 mlx5e_sq_cev_timeout(void *arg)
1871 {
1872 	struct mlx5e_sq *sq = arg;
1873 
1874 	mtx_assert(&sq->lock, MA_OWNED);
1875 
1876 	/* check next state */
1877 	switch (sq->cev_next_state) {
1878 	case MLX5E_CEV_STATE_SEND_NOPS:
1879 		/* fill TX ring with NOPs, if any */
1880 		mlx5e_sq_send_nops_locked(sq, 0);
1881 
1882 		/* check if completed */
1883 		if (sq->cev_counter == 0) {
1884 			sq->cev_next_state = MLX5E_CEV_STATE_INITIAL;
1885 			return;
1886 		}
1887 		break;
1888 	default:
1889 		/* send NOPs on next timeout */
1890 		sq->cev_next_state = MLX5E_CEV_STATE_SEND_NOPS;
1891 		break;
1892 	}
1893 
1894 	/* restart timer */
1895 	callout_reset_curcpu(&sq->cev_callout, hz, mlx5e_sq_cev_timeout, sq);
1896 }
1897 
1898 void
mlx5e_drain_sq(struct mlx5e_sq * sq)1899 mlx5e_drain_sq(struct mlx5e_sq *sq)
1900 {
1901 	int error;
1902 	struct mlx5_core_dev *mdev= sq->priv->mdev;
1903 
1904 	/*
1905 	 * Check if already stopped.
1906 	 *
1907 	 * NOTE: Serialization of this function is managed by the
1908 	 * caller ensuring the priv's state lock is locked or in case
1909 	 * of rate limit support, a single thread manages drain and
1910 	 * resume of SQs. The "running" variable can therefore safely
1911 	 * be read without any locks.
1912 	 */
1913 	if (READ_ONCE(sq->running) == 0)
1914 		return;
1915 
1916 	/* don't put more packets into the SQ */
1917 	WRITE_ONCE(sq->running, 0);
1918 
1919 	/* serialize access to DMA rings */
1920 	mtx_lock(&sq->lock);
1921 
1922 	/* teardown event factor timer, if any */
1923 	sq->cev_next_state = MLX5E_CEV_STATE_HOLD_NOPS;
1924 	callout_stop(&sq->cev_callout);
1925 
1926 	/* send dummy NOPs in order to flush the transmit ring */
1927 	mlx5e_sq_send_nops_locked(sq, 1);
1928 	mtx_unlock(&sq->lock);
1929 
1930 	/* wait till SQ is empty or link is down */
1931 	mtx_lock(&sq->lock);
1932 	while (sq->cc != sq->pc &&
1933 	    (sq->priv->media_status_last & IFM_ACTIVE) != 0 &&
1934 	    mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR &&
1935 	    pci_channel_offline(mdev->pdev) == 0) {
1936 		mtx_unlock(&sq->lock);
1937 		msleep(1);
1938 		sq->cq.mcq.comp(&sq->cq.mcq, NULL);
1939 		mtx_lock(&sq->lock);
1940 	}
1941 	mtx_unlock(&sq->lock);
1942 
1943 	/* error out remaining requests */
1944 	error = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY, MLX5_SQC_STATE_ERR);
1945 	if (error != 0) {
1946 		mlx5_en_err(sq->ifp,
1947 		    "mlx5e_modify_sq() from RDY to ERR failed: %d\n", error);
1948 	}
1949 
1950 	/* wait till SQ is empty */
1951 	mtx_lock(&sq->lock);
1952 	while (sq->cc != sq->pc &&
1953 	       mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR &&
1954 	       pci_channel_offline(mdev->pdev) == 0) {
1955 		mtx_unlock(&sq->lock);
1956 		msleep(1);
1957 		sq->cq.mcq.comp(&sq->cq.mcq, NULL);
1958 		mtx_lock(&sq->lock);
1959 	}
1960 	mtx_unlock(&sq->lock);
1961 }
1962 
1963 static void
mlx5e_close_sq_wait(struct mlx5e_sq * sq)1964 mlx5e_close_sq_wait(struct mlx5e_sq *sq)
1965 {
1966 
1967 	mlx5e_drain_sq(sq);
1968 	mlx5e_disable_sq(sq);
1969 	mlx5e_destroy_sq(sq);
1970 }
1971 
1972 static int
mlx5e_create_cq(struct mlx5e_priv * priv,struct mlx5e_cq_param * param,struct mlx5e_cq * cq,mlx5e_cq_comp_t * comp,int eq_ix)1973 mlx5e_create_cq(struct mlx5e_priv *priv,
1974     struct mlx5e_cq_param *param,
1975     struct mlx5e_cq *cq,
1976     mlx5e_cq_comp_t *comp,
1977     int eq_ix)
1978 {
1979 	struct mlx5_core_dev *mdev = priv->mdev;
1980 	struct mlx5_core_cq *mcq = &cq->mcq;
1981 	int eqn_not_used;
1982 	int irqn;
1983 	int err;
1984 	u32 i;
1985 
1986 	err = mlx5_vector2eqn(mdev, eq_ix, &eqn_not_used, &irqn);
1987 	if (err)
1988 		return (err);
1989 
1990 	err = mlx5_cqwq_create(mdev, &param->wq, param->cqc, &cq->wq,
1991 	    &cq->wq_ctrl);
1992 	if (err)
1993 		return (err);
1994 
1995 	mcq->cqe_sz = 64;
1996 	mcq->set_ci_db = cq->wq_ctrl.db.db;
1997 	mcq->arm_db = cq->wq_ctrl.db.db + 1;
1998 	*mcq->set_ci_db = 0;
1999 	*mcq->arm_db = 0;
2000 	mcq->vector = eq_ix;
2001 	mcq->comp = comp;
2002 	mcq->event = mlx5e_cq_error_event;
2003 	mcq->irqn = irqn;
2004 
2005 	for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
2006 		struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
2007 
2008 		cqe->op_own = 0xf1;
2009 	}
2010 
2011 	cq->priv = priv;
2012 
2013 	return (0);
2014 }
2015 
2016 static void
mlx5e_destroy_cq(struct mlx5e_cq * cq)2017 mlx5e_destroy_cq(struct mlx5e_cq *cq)
2018 {
2019 	mlx5_wq_destroy(&cq->wq_ctrl);
2020 }
2021 
2022 static int
mlx5e_enable_cq(struct mlx5e_cq * cq,struct mlx5e_cq_param * param,int eq_ix)2023 mlx5e_enable_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param, int eq_ix)
2024 {
2025 	struct mlx5_core_cq *mcq = &cq->mcq;
2026 	u32 out[MLX5_ST_SZ_DW(create_cq_out)];
2027 	void *in;
2028 	void *cqc;
2029 	int inlen;
2030 	int irqn_not_used;
2031 	int eqn;
2032 	int err;
2033 
2034 	inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
2035 	    sizeof(u64) * cq->wq_ctrl.buf.npages;
2036 	in = mlx5_vzalloc(inlen);
2037 	if (in == NULL)
2038 		return (-ENOMEM);
2039 
2040 	cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
2041 
2042 	memcpy(cqc, param->cqc, sizeof(param->cqc));
2043 
2044 	mlx5_fill_page_array(&cq->wq_ctrl.buf,
2045 	    (__be64 *) MLX5_ADDR_OF(create_cq_in, in, pas));
2046 
2047 	mlx5_vector2eqn(cq->priv->mdev, eq_ix, &eqn, &irqn_not_used);
2048 
2049 	MLX5_SET(cqc, cqc, c_eqn, eqn);
2050 	MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
2051 	    MLX5_ADAPTER_PAGE_SHIFT);
2052 	MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
2053 
2054 	err = mlx5_core_create_cq(cq->priv->mdev, mcq, in, inlen, out, sizeof(out));
2055 
2056 	kvfree(in);
2057 
2058 	if (err)
2059 		return (err);
2060 
2061 	mlx5e_cq_arm(cq, MLX5_GET_DOORBELL_LOCK(&cq->priv->doorbell_lock));
2062 
2063 	return (0);
2064 }
2065 
2066 static void
mlx5e_disable_cq(struct mlx5e_cq * cq)2067 mlx5e_disable_cq(struct mlx5e_cq *cq)
2068 {
2069 
2070 	mlx5_core_destroy_cq(cq->priv->mdev, &cq->mcq);
2071 }
2072 
2073 int
mlx5e_open_cq(struct mlx5e_priv * priv,struct mlx5e_cq_param * param,struct mlx5e_cq * cq,mlx5e_cq_comp_t * comp,int eq_ix)2074 mlx5e_open_cq(struct mlx5e_priv *priv,
2075     struct mlx5e_cq_param *param,
2076     struct mlx5e_cq *cq,
2077     mlx5e_cq_comp_t *comp,
2078     int eq_ix)
2079 {
2080 	int err;
2081 
2082 	err = mlx5e_create_cq(priv, param, cq, comp, eq_ix);
2083 	if (err)
2084 		return (err);
2085 
2086 	err = mlx5e_enable_cq(cq, param, eq_ix);
2087 	if (err)
2088 		goto err_destroy_cq;
2089 
2090 	return (0);
2091 
2092 err_destroy_cq:
2093 	mlx5e_destroy_cq(cq);
2094 
2095 	return (err);
2096 }
2097 
2098 void
mlx5e_close_cq(struct mlx5e_cq * cq)2099 mlx5e_close_cq(struct mlx5e_cq *cq)
2100 {
2101 	mlx5e_disable_cq(cq);
2102 	mlx5e_destroy_cq(cq);
2103 }
2104 
2105 static int
mlx5e_open_tx_cqs(struct mlx5e_channel * c,struct mlx5e_channel_param * cparam)2106 mlx5e_open_tx_cqs(struct mlx5e_channel *c,
2107     struct mlx5e_channel_param *cparam)
2108 {
2109 	int err;
2110 	int tc;
2111 
2112 	for (tc = 0; tc < c->priv->num_tc; tc++) {
2113 		/* open completion queue */
2114 		err = mlx5e_open_cq(c->priv, &cparam->tx_cq, &c->sq[tc].cq,
2115 		    &mlx5e_tx_cq_comp, c->ix);
2116 		if (err)
2117 			goto err_close_tx_cqs;
2118 	}
2119 	return (0);
2120 
2121 err_close_tx_cqs:
2122 	for (tc--; tc >= 0; tc--)
2123 		mlx5e_close_cq(&c->sq[tc].cq);
2124 
2125 	return (err);
2126 }
2127 
2128 static void
mlx5e_close_tx_cqs(struct mlx5e_channel * c)2129 mlx5e_close_tx_cqs(struct mlx5e_channel *c)
2130 {
2131 	int tc;
2132 
2133 	for (tc = 0; tc < c->priv->num_tc; tc++)
2134 		mlx5e_close_cq(&c->sq[tc].cq);
2135 }
2136 
2137 static int
mlx5e_open_sqs(struct mlx5e_channel * c,struct mlx5e_channel_param * cparam)2138 mlx5e_open_sqs(struct mlx5e_channel *c,
2139     struct mlx5e_channel_param *cparam)
2140 {
2141 	int err;
2142 	int tc;
2143 
2144 	for (tc = 0; tc < c->priv->num_tc; tc++) {
2145 		err = mlx5e_open_sq(c, tc, &cparam->sq, &c->sq[tc]);
2146 		if (err)
2147 			goto err_close_sqs;
2148 	}
2149 
2150 	return (0);
2151 
2152 err_close_sqs:
2153 	for (tc--; tc >= 0; tc--)
2154 		mlx5e_close_sq_wait(&c->sq[tc]);
2155 
2156 	return (err);
2157 }
2158 
2159 static void
mlx5e_close_sqs_wait(struct mlx5e_channel * c)2160 mlx5e_close_sqs_wait(struct mlx5e_channel *c)
2161 {
2162 	int tc;
2163 
2164 	for (tc = 0; tc < c->priv->num_tc; tc++)
2165 		mlx5e_close_sq_wait(&c->sq[tc]);
2166 }
2167 
2168 static void
mlx5e_chan_static_init(struct mlx5e_priv * priv,struct mlx5e_channel * c,int ix)2169 mlx5e_chan_static_init(struct mlx5e_priv *priv, struct mlx5e_channel *c, int ix)
2170 {
2171 	int tc;
2172 
2173 	/* setup priv and channel number */
2174 	c->priv = priv;
2175 	c->ix = ix;
2176 
2177 	/* setup send tag */
2178 	m_snd_tag_init(&c->tag, c->priv->ifp, &mlx5e_ul_snd_tag_sw);
2179 
2180 	init_completion(&c->completion);
2181 
2182 	mtx_init(&c->rq.mtx, "mlx5rx", MTX_NETWORK_LOCK, MTX_DEF);
2183 
2184 	callout_init_mtx(&c->rq.watchdog, &c->rq.mtx, 0);
2185 
2186 	for (tc = 0; tc != MLX5E_MAX_TX_NUM_TC; tc++) {
2187 		struct mlx5e_sq *sq = c->sq + tc;
2188 
2189 		mtx_init(&sq->lock, "mlx5tx",
2190 		    MTX_NETWORK_LOCK " TX", MTX_DEF);
2191 		mtx_init(&sq->comp_lock, "mlx5comp",
2192 		    MTX_NETWORK_LOCK " TX", MTX_DEF);
2193 
2194 		callout_init_mtx(&sq->cev_callout, &sq->lock, 0);
2195 	}
2196 
2197 	mlx5e_iq_static_init(&c->iq);
2198 }
2199 
2200 static void
mlx5e_chan_wait_for_completion(struct mlx5e_channel * c)2201 mlx5e_chan_wait_for_completion(struct mlx5e_channel *c)
2202 {
2203 
2204 	m_snd_tag_rele(&c->tag);
2205 	wait_for_completion(&c->completion);
2206 }
2207 
2208 static void
mlx5e_priv_wait_for_completion(struct mlx5e_priv * priv,const uint32_t channels)2209 mlx5e_priv_wait_for_completion(struct mlx5e_priv *priv, const uint32_t channels)
2210 {
2211 	uint32_t x;
2212 
2213 	for (x = 0; x != channels; x++)
2214 		mlx5e_chan_wait_for_completion(&priv->channel[x]);
2215 }
2216 
2217 static void
mlx5e_chan_static_destroy(struct mlx5e_channel * c)2218 mlx5e_chan_static_destroy(struct mlx5e_channel *c)
2219 {
2220 	int tc;
2221 
2222 	callout_drain(&c->rq.watchdog);
2223 
2224 	mtx_destroy(&c->rq.mtx);
2225 
2226 	for (tc = 0; tc != MLX5E_MAX_TX_NUM_TC; tc++) {
2227 		callout_drain(&c->sq[tc].cev_callout);
2228 		mtx_destroy(&c->sq[tc].lock);
2229 		mtx_destroy(&c->sq[tc].comp_lock);
2230 	}
2231 
2232 	mlx5e_iq_static_destroy(&c->iq);
2233 }
2234 
2235 static int
mlx5e_open_channel(struct mlx5e_priv * priv,struct mlx5e_channel_param * cparam,struct mlx5e_channel * c)2236 mlx5e_open_channel(struct mlx5e_priv *priv,
2237     struct mlx5e_channel_param *cparam,
2238     struct mlx5e_channel *c)
2239 {
2240 	struct epoch_tracker et;
2241 	int i, err;
2242 
2243 	/* zero non-persistent data */
2244 	MLX5E_ZERO(&c->rq, mlx5e_rq_zero_start);
2245 	for (i = 0; i != priv->num_tc; i++)
2246 		MLX5E_ZERO(&c->sq[i], mlx5e_sq_zero_start);
2247 	MLX5E_ZERO(&c->iq, mlx5e_iq_zero_start);
2248 
2249 	/* open transmit completion queue */
2250 	err = mlx5e_open_tx_cqs(c, cparam);
2251 	if (err)
2252 		goto err_free;
2253 
2254 	/* open receive completion queue */
2255 	err = mlx5e_open_cq(c->priv, &cparam->rx_cq, &c->rq.cq,
2256 	    &mlx5e_rx_cq_comp, c->ix);
2257 	if (err)
2258 		goto err_close_tx_cqs;
2259 
2260 	err = mlx5e_open_sqs(c, cparam);
2261 	if (err)
2262 		goto err_close_rx_cq;
2263 
2264 	err = mlx5e_iq_open(c, &cparam->sq, &cparam->tx_cq, &c->iq);
2265 	if (err)
2266 		goto err_close_sqs;
2267 
2268 	err = mlx5e_open_rq(c, &cparam->rq, &c->rq);
2269 	if (err)
2270 		goto err_close_iq;
2271 
2272 	/* poll receive queue initially */
2273 	NET_EPOCH_ENTER(et);
2274 	c->rq.cq.mcq.comp(&c->rq.cq.mcq, NULL);
2275 	NET_EPOCH_EXIT(et);
2276 
2277 	return (0);
2278 
2279 err_close_iq:
2280 	mlx5e_iq_close(&c->iq);
2281 
2282 err_close_sqs:
2283 	mlx5e_close_sqs_wait(c);
2284 
2285 err_close_rx_cq:
2286 	mlx5e_close_cq(&c->rq.cq);
2287 
2288 err_close_tx_cqs:
2289 	mlx5e_close_tx_cqs(c);
2290 
2291 err_free:
2292 	return (err);
2293 }
2294 
2295 static void
mlx5e_close_channel(struct mlx5e_channel * c)2296 mlx5e_close_channel(struct mlx5e_channel *c)
2297 {
2298 	mlx5e_close_rq(&c->rq);
2299 }
2300 
2301 static void
mlx5e_close_channel_wait(struct mlx5e_channel * c)2302 mlx5e_close_channel_wait(struct mlx5e_channel *c)
2303 {
2304 	mlx5e_close_rq_wait(&c->rq);
2305 	mlx5e_iq_close(&c->iq);
2306 	mlx5e_close_sqs_wait(c);
2307 	mlx5e_close_tx_cqs(c);
2308 }
2309 
2310 static int
mlx5e_get_wqe_sz(struct mlx5e_priv * priv,u32 * wqe_sz,u32 * nsegs)2311 mlx5e_get_wqe_sz(struct mlx5e_priv *priv, u32 *wqe_sz, u32 *nsegs)
2312 {
2313 	u32 r, n;
2314 
2315 	r = priv->params.hw_lro_en ? priv->params.lro_wqe_sz :
2316 	    MLX5E_SW2MB_MTU(if_getmtu(priv->ifp));
2317 	if (r > MJUM16BYTES)
2318 		return (-ENOMEM);
2319 
2320 	if (r > MJUM9BYTES)
2321 		r = MJUM16BYTES;
2322 	else if (r > MJUMPAGESIZE)
2323 		r = MJUM9BYTES;
2324 	else if (r > MCLBYTES)
2325 		r = MJUMPAGESIZE;
2326 	else
2327 		r = MCLBYTES;
2328 
2329 	/*
2330 	 * n + 1 must be a power of two, because stride size must be.
2331 	 * Stride size is 16 * (n + 1), as the first segment is
2332 	 * control.
2333 	 */
2334 	n = (1 << fls(howmany(r, MLX5E_MAX_RX_BYTES))) - 1;
2335 	if (n > MLX5E_MAX_BUSDMA_RX_SEGS)
2336 		return (-ENOMEM);
2337 
2338 	*wqe_sz = r;
2339 	*nsegs = n;
2340 	return (0);
2341 }
2342 
2343 static void
mlx5e_build_rq_param(struct mlx5e_priv * priv,struct mlx5e_rq_param * param)2344 mlx5e_build_rq_param(struct mlx5e_priv *priv,
2345     struct mlx5e_rq_param *param)
2346 {
2347 	void *rqc = param->rqc;
2348 	void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2349 	u32 wqe_sz, nsegs;
2350 
2351 	mlx5e_get_wqe_sz(priv, &wqe_sz, &nsegs);
2352 	MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
2353 	MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
2354 	MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe) +
2355 	    nsegs * sizeof(struct mlx5_wqe_data_seg)));
2356 	MLX5_SET(wq, wq, log_wq_sz, priv->params.log_rq_size);
2357 	MLX5_SET(wq, wq, pd, priv->pdn);
2358 
2359 	param->wq.linear = 1;
2360 }
2361 
2362 static void
mlx5e_build_sq_param(struct mlx5e_priv * priv,struct mlx5e_sq_param * param)2363 mlx5e_build_sq_param(struct mlx5e_priv *priv,
2364     struct mlx5e_sq_param *param)
2365 {
2366 	void *sqc = param->sqc;
2367 	void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2368 
2369 	MLX5_SET(wq, wq, log_wq_sz, priv->params.log_sq_size);
2370 	MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
2371 	MLX5_SET(wq, wq, pd, priv->pdn);
2372 
2373 	param->wq.linear = 1;
2374 }
2375 
2376 static void
mlx5e_build_common_cq_param(struct mlx5e_priv * priv,struct mlx5e_cq_param * param)2377 mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
2378     struct mlx5e_cq_param *param)
2379 {
2380 	void *cqc = param->cqc;
2381 
2382 	MLX5_SET(cqc, cqc, uar_page, priv->mdev->priv.uar->index);
2383 }
2384 
2385 static void
mlx5e_get_default_profile(struct mlx5e_priv * priv,int mode,struct net_dim_cq_moder * ptr)2386 mlx5e_get_default_profile(struct mlx5e_priv *priv, int mode, struct net_dim_cq_moder *ptr)
2387 {
2388 
2389 	*ptr = net_dim_get_profile(mode, MLX5E_DIM_DEFAULT_PROFILE);
2390 
2391 	/* apply LRO restrictions */
2392 	if (priv->params.hw_lro_en &&
2393 	    ptr->pkts > MLX5E_DIM_MAX_RX_CQ_MODERATION_PKTS_WITH_LRO) {
2394 		ptr->pkts = MLX5E_DIM_MAX_RX_CQ_MODERATION_PKTS_WITH_LRO;
2395 	}
2396 }
2397 
2398 static void
mlx5e_build_rx_cq_param(struct mlx5e_priv * priv,struct mlx5e_cq_param * param)2399 mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
2400     struct mlx5e_cq_param *param)
2401 {
2402 	struct net_dim_cq_moder curr;
2403 	void *cqc = param->cqc;
2404 
2405 	/*
2406 	 * We use MLX5_CQE_FORMAT_HASH because the RX hash mini CQE
2407 	 * format is more beneficial for FreeBSD use case.
2408 	 *
2409 	 * Adding support for MLX5_CQE_FORMAT_CSUM will require changes
2410 	 * in mlx5e_decompress_cqe.
2411 	 */
2412 	if (priv->params.cqe_zipping_en) {
2413 		MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_HASH);
2414 		MLX5_SET(cqc, cqc, cqe_compression_en, 1);
2415 	}
2416 
2417 	MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_rq_size);
2418 
2419 	switch (priv->params.rx_cq_moderation_mode) {
2420 	case 0:
2421 		MLX5_SET(cqc, cqc, cq_period, priv->params.rx_cq_moderation_usec);
2422 		MLX5_SET(cqc, cqc, cq_max_count, priv->params.rx_cq_moderation_pkts);
2423 		MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
2424 		break;
2425 	case 1:
2426 		MLX5_SET(cqc, cqc, cq_period, priv->params.rx_cq_moderation_usec);
2427 		MLX5_SET(cqc, cqc, cq_max_count, priv->params.rx_cq_moderation_pkts);
2428 		if (MLX5_CAP_GEN(priv->mdev, cq_period_start_from_cqe))
2429 			MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
2430 		else
2431 			MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
2432 		break;
2433 	case 2:
2434 		mlx5e_get_default_profile(priv, NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE, &curr);
2435 		MLX5_SET(cqc, cqc, cq_period, curr.usec);
2436 		MLX5_SET(cqc, cqc, cq_max_count, curr.pkts);
2437 		MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
2438 		break;
2439 	case 3:
2440 		mlx5e_get_default_profile(priv, NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE, &curr);
2441 		MLX5_SET(cqc, cqc, cq_period, curr.usec);
2442 		MLX5_SET(cqc, cqc, cq_max_count, curr.pkts);
2443 		if (MLX5_CAP_GEN(priv->mdev, cq_period_start_from_cqe))
2444 			MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
2445 		else
2446 			MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
2447 		break;
2448 	default:
2449 		break;
2450 	}
2451 
2452 	mlx5e_dim_build_cq_param(priv, param);
2453 
2454 	mlx5e_build_common_cq_param(priv, param);
2455 }
2456 
2457 static void
mlx5e_build_tx_cq_param(struct mlx5e_priv * priv,struct mlx5e_cq_param * param)2458 mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
2459     struct mlx5e_cq_param *param)
2460 {
2461 	void *cqc = param->cqc;
2462 
2463 	MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_sq_size);
2464 	MLX5_SET(cqc, cqc, cq_period, priv->params.tx_cq_moderation_usec);
2465 	MLX5_SET(cqc, cqc, cq_max_count, priv->params.tx_cq_moderation_pkts);
2466 
2467 	switch (priv->params.tx_cq_moderation_mode) {
2468 	case 0:
2469 		MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
2470 		break;
2471 	default:
2472 		if (MLX5_CAP_GEN(priv->mdev, cq_period_start_from_cqe))
2473 			MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
2474 		else
2475 			MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
2476 		break;
2477 	}
2478 
2479 	mlx5e_build_common_cq_param(priv, param);
2480 }
2481 
2482 static void
mlx5e_build_channel_param(struct mlx5e_priv * priv,struct mlx5e_channel_param * cparam)2483 mlx5e_build_channel_param(struct mlx5e_priv *priv,
2484     struct mlx5e_channel_param *cparam)
2485 {
2486 	memset(cparam, 0, sizeof(*cparam));
2487 
2488 	mlx5e_build_rq_param(priv, &cparam->rq);
2489 	mlx5e_build_sq_param(priv, &cparam->sq);
2490 	mlx5e_build_rx_cq_param(priv, &cparam->rx_cq);
2491 	mlx5e_build_tx_cq_param(priv, &cparam->tx_cq);
2492 }
2493 
2494 static int
mlx5e_open_channels(struct mlx5e_priv * priv)2495 mlx5e_open_channels(struct mlx5e_priv *priv)
2496 {
2497 	struct mlx5e_channel_param *cparam;
2498 	int err;
2499 	int i;
2500 
2501 	cparam = malloc(sizeof(*cparam), M_MLX5EN, M_WAITOK);
2502 
2503 	mlx5e_build_channel_param(priv, cparam);
2504 	for (i = 0; i < priv->params.num_channels; i++) {
2505 		err = mlx5e_open_channel(priv, cparam, &priv->channel[i]);
2506 		if (err)
2507 			goto err_close_channels;
2508 
2509 		/* Bind interrupt vectors, if any. */
2510 		if (priv->params_ethtool.irq_cpu_base > -1) {
2511 			cpuset_t cpuset;
2512 			int cpu;
2513 			int irq;
2514 			int eqn;
2515 			int nirq;
2516 
2517 			err = mlx5_vector2eqn(priv->mdev, i,
2518 			    &eqn, &nirq);
2519 
2520 			/* error here is non-fatal */
2521 			if (err != 0)
2522 				continue;
2523 
2524 			irq = priv->mdev->priv.msix_arr[nirq].vector;
2525 			cpu = (unsigned)(priv->params_ethtool.irq_cpu_base +
2526 			    i * priv->params_ethtool.irq_cpu_stride) % (unsigned)mp_ncpus;
2527 
2528 			CPU_ZERO(&cpuset);
2529 			CPU_SET(cpu, &cpuset);
2530 			intr_setaffinity(irq, CPU_WHICH_INTRHANDLER, &cpuset);
2531 		}
2532 	}
2533 	free(cparam, M_MLX5EN);
2534 	return (0);
2535 
2536 err_close_channels:
2537 	while (i--) {
2538 		mlx5e_close_channel(&priv->channel[i]);
2539 		mlx5e_close_channel_wait(&priv->channel[i]);
2540 	}
2541 	free(cparam, M_MLX5EN);
2542 	return (err);
2543 }
2544 
2545 static void
mlx5e_close_channels(struct mlx5e_priv * priv)2546 mlx5e_close_channels(struct mlx5e_priv *priv)
2547 {
2548 	int i;
2549 
2550 	for (i = 0; i < priv->params.num_channels; i++)
2551 		mlx5e_close_channel(&priv->channel[i]);
2552 	for (i = 0; i < priv->params.num_channels; i++)
2553 		mlx5e_close_channel_wait(&priv->channel[i]);
2554 }
2555 
2556 static int
mlx5e_refresh_sq_params(struct mlx5e_priv * priv,struct mlx5e_sq * sq)2557 mlx5e_refresh_sq_params(struct mlx5e_priv *priv, struct mlx5e_sq *sq)
2558 {
2559 
2560 	if (MLX5_CAP_GEN(priv->mdev, cq_period_mode_modify)) {
2561 		uint8_t cq_mode;
2562 
2563 		switch (priv->params.tx_cq_moderation_mode) {
2564 		case 0:
2565 		case 2:
2566 			cq_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
2567 			break;
2568 		default:
2569 			cq_mode = MLX5_CQ_PERIOD_MODE_START_FROM_CQE;
2570 			break;
2571 		}
2572 
2573 		return (mlx5_core_modify_cq_moderation_mode(priv->mdev, &sq->cq.mcq,
2574 		    priv->params.tx_cq_moderation_usec,
2575 		    priv->params.tx_cq_moderation_pkts,
2576 		    cq_mode));
2577 	}
2578 
2579 	return (mlx5_core_modify_cq_moderation(priv->mdev, &sq->cq.mcq,
2580 	    priv->params.tx_cq_moderation_usec,
2581 	    priv->params.tx_cq_moderation_pkts));
2582 }
2583 
2584 static int
mlx5e_refresh_rq_params(struct mlx5e_priv * priv,struct mlx5e_rq * rq)2585 mlx5e_refresh_rq_params(struct mlx5e_priv *priv, struct mlx5e_rq *rq)
2586 {
2587 
2588 	if (MLX5_CAP_GEN(priv->mdev, cq_period_mode_modify)) {
2589 		uint8_t cq_mode;
2590 		uint8_t dim_mode;
2591 		int retval;
2592 
2593 		switch (priv->params.rx_cq_moderation_mode) {
2594 		case 0:
2595 		case 2:
2596 			cq_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
2597 			dim_mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2598 			break;
2599 		default:
2600 			cq_mode = MLX5_CQ_PERIOD_MODE_START_FROM_CQE;
2601 			dim_mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE;
2602 			break;
2603 		}
2604 
2605 		/* tear down dynamic interrupt moderation */
2606 		mtx_lock(&rq->mtx);
2607 		rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_DISABLED;
2608 		mtx_unlock(&rq->mtx);
2609 
2610 		/* wait for dynamic interrupt moderation work task, if any */
2611 		cancel_work_sync(&rq->dim.work);
2612 
2613 		if (priv->params.rx_cq_moderation_mode >= 2) {
2614 			struct net_dim_cq_moder curr;
2615 
2616 			mlx5e_get_default_profile(priv, dim_mode, &curr);
2617 
2618 			retval = mlx5_core_modify_cq_moderation_mode(priv->mdev, &rq->cq.mcq,
2619 			    curr.usec, curr.pkts, cq_mode);
2620 
2621 			/* set dynamic interrupt moderation mode and zero defaults */
2622 			mtx_lock(&rq->mtx);
2623 			rq->dim.mode = dim_mode;
2624 			rq->dim.state = 0;
2625 			rq->dim.profile_ix = MLX5E_DIM_DEFAULT_PROFILE;
2626 			mtx_unlock(&rq->mtx);
2627 		} else {
2628 			retval = mlx5_core_modify_cq_moderation_mode(priv->mdev, &rq->cq.mcq,
2629 			    priv->params.rx_cq_moderation_usec,
2630 			    priv->params.rx_cq_moderation_pkts,
2631 			    cq_mode);
2632 		}
2633 		return (retval);
2634 	}
2635 
2636 	return (mlx5_core_modify_cq_moderation(priv->mdev, &rq->cq.mcq,
2637 	    priv->params.rx_cq_moderation_usec,
2638 	    priv->params.rx_cq_moderation_pkts));
2639 }
2640 
2641 static int
mlx5e_refresh_channel_params_sub(struct mlx5e_priv * priv,struct mlx5e_channel * c)2642 mlx5e_refresh_channel_params_sub(struct mlx5e_priv *priv, struct mlx5e_channel *c)
2643 {
2644 	int err;
2645 	int i;
2646 
2647 	err = mlx5e_refresh_rq_params(priv, &c->rq);
2648 	if (err)
2649 		goto done;
2650 
2651 	for (i = 0; i != priv->num_tc; i++) {
2652 		err = mlx5e_refresh_sq_params(priv, &c->sq[i]);
2653 		if (err)
2654 			goto done;
2655 	}
2656 done:
2657 	return (err);
2658 }
2659 
2660 int
mlx5e_refresh_channel_params(struct mlx5e_priv * priv)2661 mlx5e_refresh_channel_params(struct mlx5e_priv *priv)
2662 {
2663 	int i;
2664 
2665 	/* check if channels are closed */
2666 	if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
2667 		return (EINVAL);
2668 
2669 	for (i = 0; i < priv->params.num_channels; i++) {
2670 		int err;
2671 
2672 		err = mlx5e_refresh_channel_params_sub(priv, &priv->channel[i]);
2673 		if (err)
2674 			return (err);
2675 	}
2676 	return (0);
2677 }
2678 
2679 static int
mlx5e_open_tis(struct mlx5e_priv * priv,int tc)2680 mlx5e_open_tis(struct mlx5e_priv *priv, int tc)
2681 {
2682 	struct mlx5_core_dev *mdev = priv->mdev;
2683 	u32 in[MLX5_ST_SZ_DW(create_tis_in)];
2684 	void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
2685 
2686 	memset(in, 0, sizeof(in));
2687 
2688 	MLX5_SET(tisc, tisc, prio, tc);
2689 	MLX5_SET(tisc, tisc, transport_domain, priv->tdn);
2690 
2691 	return (mlx5_core_create_tis(mdev, in, sizeof(in), &priv->tisn[tc]));
2692 }
2693 
2694 static void
mlx5e_close_tis(struct mlx5e_priv * priv,int tc)2695 mlx5e_close_tis(struct mlx5e_priv *priv, int tc)
2696 {
2697 	mlx5_core_destroy_tis(priv->mdev, priv->tisn[tc], 0);
2698 }
2699 
2700 static int
mlx5e_open_tises(struct mlx5e_priv * priv)2701 mlx5e_open_tises(struct mlx5e_priv *priv)
2702 {
2703 	int num_tc = priv->num_tc;
2704 	int err;
2705 	int tc;
2706 
2707 	for (tc = 0; tc < num_tc; tc++) {
2708 		err = mlx5e_open_tis(priv, tc);
2709 		if (err)
2710 			goto err_close_tises;
2711 	}
2712 
2713 	return (0);
2714 
2715 err_close_tises:
2716 	for (tc--; tc >= 0; tc--)
2717 		mlx5e_close_tis(priv, tc);
2718 
2719 	return (err);
2720 }
2721 
2722 static void
mlx5e_close_tises(struct mlx5e_priv * priv)2723 mlx5e_close_tises(struct mlx5e_priv *priv)
2724 {
2725 	int num_tc = priv->num_tc;
2726 	int tc;
2727 
2728 	for (tc = 0; tc < num_tc; tc++)
2729 		mlx5e_close_tis(priv, tc);
2730 }
2731 
2732 static int
mlx5e_open_default_rqt(struct mlx5e_priv * priv,u32 * prqtn,int sz)2733 mlx5e_open_default_rqt(struct mlx5e_priv *priv, u32 *prqtn, int sz)
2734 {
2735 	u32 *in;
2736 	void *rqtc;
2737 	int inlen;
2738 	int err;
2739 	int i;
2740 
2741 	inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
2742 	in = mlx5_vzalloc(inlen);
2743 	if (in == NULL)
2744 		return (-ENOMEM);
2745 	rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
2746 
2747 	MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2748 	MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
2749 
2750 	for (i = 0; i != sz; i++)
2751 		MLX5_SET(rqtc, rqtc, rq_num[i], priv->drop_rq.rqn);
2752 
2753 	err = mlx5_core_create_rqt(priv->mdev, in, inlen, prqtn);
2754 	kvfree(in);
2755 
2756 	return (err);
2757 }
2758 
2759 static int
mlx5e_open_rqts(struct mlx5e_priv * priv)2760 mlx5e_open_rqts(struct mlx5e_priv *priv)
2761 {
2762 	int err;
2763 	int i;
2764 
2765 	err = mlx5e_open_default_rqt(priv, &priv->rqtn,
2766 	    1 << priv->params.rx_hash_log_tbl_sz);
2767 	if (err)
2768 		goto err_default;
2769 
2770 	for (i = 0; i != priv->mdev->priv.eq_table.num_comp_vectors; i++) {
2771 		err = mlx5e_open_default_rqt(priv, &priv->channel[i].rqtn, 1);
2772 		if (err)
2773 			goto err_channel;
2774 	}
2775 	return (0);
2776 
2777 err_channel:
2778 	while (i--)
2779 		mlx5_core_destroy_rqt(priv->mdev, priv->channel[i].rqtn, 0);
2780 
2781 	mlx5_core_destroy_rqt(priv->mdev, priv->rqtn, 0);
2782 
2783 err_default:
2784 	return (err);
2785 }
2786 
2787 static void
mlx5e_close_rqts(struct mlx5e_priv * priv)2788 mlx5e_close_rqts(struct mlx5e_priv *priv)
2789 {
2790 	int i;
2791 
2792 	for (i = 0; i != priv->mdev->priv.eq_table.num_comp_vectors; i++)
2793 		mlx5_core_destroy_rqt(priv->mdev, priv->channel[i].rqtn, 0);
2794 
2795 	mlx5_core_destroy_rqt(priv->mdev, priv->rqtn, 0);
2796 }
2797 
2798 static int
mlx5e_activate_rqt(struct mlx5e_priv * priv)2799 mlx5e_activate_rqt(struct mlx5e_priv *priv)
2800 {
2801 	u32 *in;
2802 	void *rqtc;
2803 	int inlen;
2804 	int err;
2805 	int sz;
2806 	int i;
2807 
2808 	sz = 1 << priv->params.rx_hash_log_tbl_sz;
2809 
2810 	inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
2811 	in = mlx5_vzalloc(inlen);
2812 	if (in == NULL)
2813 		return (-ENOMEM);
2814 
2815 	rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
2816 
2817 	MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2818 	MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
2819 
2820 	for (i = 0; i != sz; i++) {
2821 		int ix;
2822 #ifdef RSS
2823 		ix = rss_get_indirection_to_bucket(i);
2824 #else
2825 		ix = i;
2826 #endif
2827 		/* ensure we don't overflow */
2828 		ix %= priv->params.num_channels;
2829 
2830 		/* apply receive side scaling stride, if any */
2831 		ix -= ix % (int)priv->params.channels_rsss;
2832 
2833 		MLX5_SET(rqtc, rqtc, rq_num[i], priv->channel[ix].rq.rqn);
2834 	}
2835 
2836 	err = mlx5_core_modify_rqt(priv->mdev, priv->rqtn, in, inlen);
2837 	if (err)
2838 		goto err_modify;
2839 
2840 	inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32);
2841 
2842 	MLX5_SET(rqtc, rqtc, rqt_actual_size, 1);
2843 
2844 	for (i = 0; i != priv->mdev->priv.eq_table.num_comp_vectors; i++) {
2845 		int ix;
2846 #ifdef RSS
2847 		ix = rss_get_indirection_to_bucket(i);
2848 #else
2849 		ix = i;
2850 #endif
2851 		/* ensure we don't overflow */
2852 		ix %= priv->params.num_channels;
2853 
2854 		/* apply receive side scaling stride, if any */
2855 		ix -= ix % (int)priv->params.channels_rsss;
2856 
2857 		MLX5_SET(rqtc, rqtc, rq_num[0], priv->channel[ix].rq.rqn);
2858 
2859 		err = mlx5_core_modify_rqt(priv->mdev, priv->channel[i].rqtn, in, inlen);
2860 		if (err)
2861 			goto err_modify;
2862 	}
2863 
2864 err_modify:
2865 	kvfree(in);
2866 	return (err);
2867 }
2868 
2869 static int
mlx5e_deactivate_rqt(struct mlx5e_priv * priv)2870 mlx5e_deactivate_rqt(struct mlx5e_priv *priv)
2871 {
2872 	u32 *in;
2873 	void *rqtc;
2874 	int inlen;
2875 	int err;
2876 	int sz;
2877 	int i;
2878 
2879 	sz = 1 << priv->params.rx_hash_log_tbl_sz;
2880 
2881 	inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
2882 	in = mlx5_vzalloc(inlen);
2883 	if (in == NULL)
2884 		return (-ENOMEM);
2885 
2886 	rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
2887 
2888 	MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2889 	MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
2890 
2891 	for (i = 0; i != sz; i++)
2892 		MLX5_SET(rqtc, rqtc, rq_num[i], priv->drop_rq.rqn);
2893 
2894 	err = mlx5_core_modify_rqt(priv->mdev, priv->rqtn, in, inlen);
2895 	if (err)
2896 		goto err_modify;
2897 
2898 	inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32);
2899 
2900 	MLX5_SET(rqtc, rqtc, rqt_actual_size, 1);
2901 
2902 	for (i = 0; i != priv->mdev->priv.eq_table.num_comp_vectors; i++) {
2903 		MLX5_SET(rqtc, rqtc, rq_num[0], priv->drop_rq.rqn);
2904 
2905 		err = mlx5_core_modify_rqt(priv->mdev, priv->channel[i].rqtn, in, inlen);
2906 		if (err)
2907 			goto err_modify;
2908 	}
2909 
2910 err_modify:
2911 	kvfree(in);
2912 	return (err);
2913 }
2914 
2915 #define	MLX5E_RSS_KEY_SIZE (10 * 4)	/* bytes */
2916 
2917 static void
mlx5e_get_rss_key(void * key_ptr)2918 mlx5e_get_rss_key(void *key_ptr)
2919 {
2920 #ifdef RSS
2921 	rss_getkey(key_ptr);
2922 #else
2923 	static const u32 rsskey[] = {
2924 	    cpu_to_be32(0xD181C62C),
2925 	    cpu_to_be32(0xF7F4DB5B),
2926 	    cpu_to_be32(0x1983A2FC),
2927 	    cpu_to_be32(0x943E1ADB),
2928 	    cpu_to_be32(0xD9389E6B),
2929 	    cpu_to_be32(0xD1039C2C),
2930 	    cpu_to_be32(0xA74499AD),
2931 	    cpu_to_be32(0x593D56D9),
2932 	    cpu_to_be32(0xF3253C06),
2933 	    cpu_to_be32(0x2ADC1FFC),
2934 	};
2935 	CTASSERT(sizeof(rsskey) == MLX5E_RSS_KEY_SIZE);
2936 	memcpy(key_ptr, rsskey, MLX5E_RSS_KEY_SIZE);
2937 #endif
2938 }
2939 
2940 static void
mlx5e_build_tir_ctx(struct mlx5e_priv * priv,u32 * tirc,int tt,bool inner_vxlan)2941 mlx5e_build_tir_ctx(struct mlx5e_priv *priv, u32 * tirc, int tt, bool inner_vxlan)
2942 {
2943 	void *hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2944 	void *hfsi = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
2945 	void *hfs = inner_vxlan ? hfsi : hfso;
2946 	__be32 *hkey;
2947 
2948 	MLX5_SET(tirc, tirc, transport_domain, priv->tdn);
2949 
2950 #define	ROUGH_MAX_L2_L3_HDR_SZ 256
2951 
2952 #define	MLX5_HASH_IP     (MLX5_HASH_FIELD_SEL_SRC_IP   |\
2953 			  MLX5_HASH_FIELD_SEL_DST_IP)
2954 
2955 #define	MLX5_HASH_ALL    (MLX5_HASH_FIELD_SEL_SRC_IP   |\
2956 			  MLX5_HASH_FIELD_SEL_DST_IP   |\
2957 			  MLX5_HASH_FIELD_SEL_L4_SPORT |\
2958 			  MLX5_HASH_FIELD_SEL_L4_DPORT)
2959 
2960 #define	MLX5_HASH_IP_IPSEC_SPI	(MLX5_HASH_FIELD_SEL_SRC_IP   |\
2961 				 MLX5_HASH_FIELD_SEL_DST_IP   |\
2962 				 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2963 
2964 	if (priv->params.hw_lro_en) {
2965 		MLX5_SET(tirc, tirc, lro_enable_mask,
2966 		    MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2967 		    MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
2968 		MLX5_SET(tirc, tirc, lro_max_msg_sz,
2969 		    (priv->params.lro_wqe_sz -
2970 		    ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
2971 		/* TODO: add the option to choose timer value dynamically */
2972 		MLX5_SET(tirc, tirc, lro_timeout_period_usecs,
2973 		    MLX5_CAP_ETH(priv->mdev,
2974 		    lro_timer_supported_periods[2]));
2975 	}
2976 
2977 	if (inner_vxlan)
2978 		MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
2979 
2980 	/*
2981 	 * All packets must go through the indirection table, RQT,
2982 	 * because it is not possible to modify the RQN of the TIR
2983 	 * for direct dispatchment after it is created, typically
2984 	 * when the link goes up and down.
2985 	 */
2986 	MLX5_SET(tirc, tirc, disp_type,
2987 	    MLX5_TIRC_DISP_TYPE_INDIRECT);
2988 	MLX5_SET(tirc, tirc, indirect_table,
2989 	    priv->rqtn);
2990 	MLX5_SET(tirc, tirc, rx_hash_fn,
2991 		 MLX5_TIRC_RX_HASH_FN_HASH_TOEPLITZ);
2992 	hkey = (__be32 *) MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
2993 
2994 	CTASSERT(MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key) >=
2995 		 MLX5E_RSS_KEY_SIZE);
2996 #ifdef RSS
2997 	/*
2998 	 * The FreeBSD RSS implementation does currently not
2999 	 * support symmetric Toeplitz hashes:
3000 	 */
3001 	MLX5_SET(tirc, tirc, rx_hash_symmetric, 0);
3002 #else
3003 	MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
3004 #endif
3005 	mlx5e_get_rss_key(hkey);
3006 
3007 	switch (tt) {
3008 	case MLX5E_TT_IPV4_TCP:
3009 		MLX5_SET(rx_hash_field_select, hfs, l3_prot_type,
3010 		    MLX5_L3_PROT_TYPE_IPV4);
3011 		MLX5_SET(rx_hash_field_select, hfs, l4_prot_type,
3012 		    MLX5_L4_PROT_TYPE_TCP);
3013 #ifdef RSS
3014 		if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4)) {
3015 			MLX5_SET(rx_hash_field_select, hfs, selected_fields,
3016 			    MLX5_HASH_IP);
3017 		} else
3018 #endif
3019 		MLX5_SET(rx_hash_field_select, hfs, selected_fields,
3020 		    MLX5_HASH_ALL);
3021 		break;
3022 
3023 	case MLX5E_TT_IPV6_TCP:
3024 		MLX5_SET(rx_hash_field_select, hfs, l3_prot_type,
3025 		    MLX5_L3_PROT_TYPE_IPV6);
3026 		MLX5_SET(rx_hash_field_select, hfs, l4_prot_type,
3027 		    MLX5_L4_PROT_TYPE_TCP);
3028 #ifdef RSS
3029 		if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6)) {
3030 			MLX5_SET(rx_hash_field_select, hfs, selected_fields,
3031 			    MLX5_HASH_IP);
3032 		} else
3033 #endif
3034 		MLX5_SET(rx_hash_field_select, hfs, selected_fields,
3035 		    MLX5_HASH_ALL);
3036 		break;
3037 
3038 	case MLX5E_TT_IPV4_UDP:
3039 		MLX5_SET(rx_hash_field_select, hfs, l3_prot_type,
3040 		    MLX5_L3_PROT_TYPE_IPV4);
3041 		MLX5_SET(rx_hash_field_select, hfs, l4_prot_type,
3042 		    MLX5_L4_PROT_TYPE_UDP);
3043 #ifdef RSS
3044 		if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4)) {
3045 			MLX5_SET(rx_hash_field_select, hfs, selected_fields,
3046 			    MLX5_HASH_IP);
3047 		} else
3048 #endif
3049 		MLX5_SET(rx_hash_field_select, hfs, selected_fields,
3050 		    MLX5_HASH_ALL);
3051 		break;
3052 
3053 	case MLX5E_TT_IPV6_UDP:
3054 		MLX5_SET(rx_hash_field_select, hfs, l3_prot_type,
3055 		    MLX5_L3_PROT_TYPE_IPV6);
3056 		MLX5_SET(rx_hash_field_select, hfs, l4_prot_type,
3057 		    MLX5_L4_PROT_TYPE_UDP);
3058 #ifdef RSS
3059 		if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6)) {
3060 			MLX5_SET(rx_hash_field_select, hfs, selected_fields,
3061 			    MLX5_HASH_IP);
3062 		} else
3063 #endif
3064 		MLX5_SET(rx_hash_field_select, hfs, selected_fields,
3065 		    MLX5_HASH_ALL);
3066 		break;
3067 
3068 	case MLX5E_TT_IPV4_IPSEC_AH:
3069 		MLX5_SET(rx_hash_field_select, hfs, l3_prot_type,
3070 		    MLX5_L3_PROT_TYPE_IPV4);
3071 		MLX5_SET(rx_hash_field_select, hfs, selected_fields,
3072 		    MLX5_HASH_IP_IPSEC_SPI);
3073 		break;
3074 
3075 	case MLX5E_TT_IPV6_IPSEC_AH:
3076 		MLX5_SET(rx_hash_field_select, hfs, l3_prot_type,
3077 		    MLX5_L3_PROT_TYPE_IPV6);
3078 		MLX5_SET(rx_hash_field_select, hfs, selected_fields,
3079 		    MLX5_HASH_IP_IPSEC_SPI);
3080 		break;
3081 
3082 	case MLX5E_TT_IPV4_IPSEC_ESP:
3083 		MLX5_SET(rx_hash_field_select, hfs, l3_prot_type,
3084 		    MLX5_L3_PROT_TYPE_IPV4);
3085 		MLX5_SET(rx_hash_field_select, hfs, selected_fields,
3086 		    MLX5_HASH_IP_IPSEC_SPI);
3087 		break;
3088 
3089 	case MLX5E_TT_IPV6_IPSEC_ESP:
3090 		MLX5_SET(rx_hash_field_select, hfs, l3_prot_type,
3091 		    MLX5_L3_PROT_TYPE_IPV6);
3092 		MLX5_SET(rx_hash_field_select, hfs, selected_fields,
3093 		    MLX5_HASH_IP_IPSEC_SPI);
3094 		break;
3095 
3096 	case MLX5E_TT_IPV4:
3097 		MLX5_SET(rx_hash_field_select, hfs, l3_prot_type,
3098 		    MLX5_L3_PROT_TYPE_IPV4);
3099 		MLX5_SET(rx_hash_field_select, hfs, selected_fields,
3100 		    MLX5_HASH_IP);
3101 		break;
3102 
3103 	case MLX5E_TT_IPV6:
3104 		MLX5_SET(rx_hash_field_select, hfs, l3_prot_type,
3105 		    MLX5_L3_PROT_TYPE_IPV6);
3106 		MLX5_SET(rx_hash_field_select, hfs, selected_fields,
3107 		    MLX5_HASH_IP);
3108 		break;
3109 
3110 	default:
3111 		break;
3112 	}
3113 }
3114 
3115 static int
mlx5e_open_tir(struct mlx5e_priv * priv,int tt,bool inner_vxlan)3116 mlx5e_open_tir(struct mlx5e_priv *priv, int tt, bool inner_vxlan)
3117 {
3118 	struct mlx5_core_dev *mdev = priv->mdev;
3119 	u32 *in;
3120 	void *tirc;
3121 	int inlen;
3122 	int err;
3123 
3124 	inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3125 	in = mlx5_vzalloc(inlen);
3126 	if (in == NULL)
3127 		return (-ENOMEM);
3128 	tirc = MLX5_ADDR_OF(create_tir_in, in, tir_context);
3129 
3130 	mlx5e_build_tir_ctx(priv, tirc, tt, inner_vxlan);
3131 
3132 	err = mlx5_core_create_tir(mdev, in, inlen, inner_vxlan ?
3133 	    &priv->tirn_inner_vxlan[tt] : &priv->tirn[tt]);
3134 
3135 	kvfree(in);
3136 
3137 	return (err);
3138 }
3139 
3140 static void
mlx5e_close_tir(struct mlx5e_priv * priv,int tt,bool inner_vxlan)3141 mlx5e_close_tir(struct mlx5e_priv *priv, int tt, bool inner_vxlan)
3142 {
3143 	mlx5_core_destroy_tir(priv->mdev, inner_vxlan ?
3144 	    priv->tirn_inner_vxlan[tt] : priv->tirn[tt], 0);
3145 }
3146 
3147 static int
mlx5e_open_tirs(struct mlx5e_priv * priv)3148 mlx5e_open_tirs(struct mlx5e_priv *priv)
3149 {
3150 	int err;
3151 	int i;
3152 
3153 	for (i = 0; i != 2 * MLX5E_NUM_TT; i++) {
3154 		err = mlx5e_open_tir(priv, i / 2, (i % 2) ? true : false);
3155 		if (err)
3156 			goto err_close_tirs;
3157 	}
3158 
3159 	return (0);
3160 
3161 err_close_tirs:
3162 	for (i--; i >= 0; i--)
3163 		mlx5e_close_tir(priv, i / 2, (i % 2) ? true : false);
3164 
3165 	return (err);
3166 }
3167 
3168 static void
mlx5e_close_tirs(struct mlx5e_priv * priv)3169 mlx5e_close_tirs(struct mlx5e_priv *priv)
3170 {
3171 	int i;
3172 
3173 	for (i = 0; i != 2 * MLX5E_NUM_TT; i++)
3174 		mlx5e_close_tir(priv, i / 2, (i % 2) ? true : false);
3175 }
3176 
3177 /*
3178  * SW MTU does not include headers,
3179  * HW MTU includes all headers and checksums.
3180  */
3181 static int
mlx5e_set_dev_port_mtu(if_t ifp,int sw_mtu)3182 mlx5e_set_dev_port_mtu(if_t ifp, int sw_mtu)
3183 {
3184 	struct mlx5e_priv *priv = if_getsoftc(ifp);
3185 	struct mlx5_core_dev *mdev = priv->mdev;
3186 	int hw_mtu;
3187 	int err;
3188 
3189 	hw_mtu = MLX5E_SW2HW_MTU(sw_mtu);
3190 
3191 	err = mlx5_set_port_mtu(mdev, hw_mtu);
3192 	if (err) {
3193 		mlx5_en_err(ifp, "mlx5_set_port_mtu failed setting %d, err=%d\n",
3194 		    sw_mtu, err);
3195 		return (err);
3196 	}
3197 
3198 	/* Update vport context MTU */
3199 	err = mlx5_set_vport_mtu(mdev, hw_mtu);
3200 	if (err) {
3201 		mlx5_en_err(ifp,
3202 		    "Failed updating vport context with MTU size, err=%d\n",
3203 		    err);
3204 	}
3205 
3206 	if_setmtu(ifp, sw_mtu);
3207 
3208 	err = mlx5_query_vport_mtu(mdev, &hw_mtu);
3209 	if (err || !hw_mtu) {
3210 		/* fallback to port oper mtu */
3211 		err = mlx5_query_port_oper_mtu(mdev, &hw_mtu);
3212 	}
3213 	if (err) {
3214 		mlx5_en_err(ifp,
3215 		    "Query port MTU, after setting new MTU value, failed\n");
3216 		return (err);
3217 	} else if (MLX5E_HW2SW_MTU(hw_mtu) < sw_mtu) {
3218 		err = -E2BIG,
3219 		mlx5_en_err(ifp,
3220 		    "Port MTU %d is smaller than ifp mtu %d\n",
3221 		    hw_mtu, sw_mtu);
3222 	} else if (MLX5E_HW2SW_MTU(hw_mtu) > sw_mtu) {
3223 		err = -EINVAL;
3224                 mlx5_en_err(ifp,
3225 		    "Port MTU %d is bigger than ifp mtu %d\n",
3226 		    hw_mtu, sw_mtu);
3227 	}
3228 	priv->params_ethtool.hw_mtu = hw_mtu;
3229 
3230 	/* compute MSB */
3231 	while (hw_mtu & (hw_mtu - 1))
3232 		hw_mtu &= (hw_mtu - 1);
3233 	priv->params_ethtool.hw_mtu_msb = hw_mtu;
3234 
3235 	return (err);
3236 }
3237 
3238 int
mlx5e_open_locked(if_t ifp)3239 mlx5e_open_locked(if_t ifp)
3240 {
3241 	struct mlx5e_priv *priv = if_getsoftc(ifp);
3242 	int err;
3243 	u16 set_id;
3244 
3245 	/* check if already opened */
3246 	if (test_bit(MLX5E_STATE_OPENED, &priv->state) != 0)
3247 		return (0);
3248 
3249 #ifdef RSS
3250 	if (rss_getnumbuckets() > priv->params.num_channels) {
3251 		mlx5_en_info(ifp,
3252 		    "NOTE: There are more RSS buckets(%u) than channels(%u) available\n",
3253 		    rss_getnumbuckets(), priv->params.num_channels);
3254 	}
3255 #endif
3256 	err = mlx5e_open_tises(priv);
3257 	if (err) {
3258 		mlx5_en_err(ifp, "mlx5e_open_tises failed, %d\n", err);
3259 		return (err);
3260 	}
3261 	err = mlx5_vport_alloc_q_counter(priv->mdev,
3262 	    MLX5_INTERFACE_PROTOCOL_ETH, &set_id);
3263 	if (err) {
3264 		mlx5_en_err(priv->ifp,
3265 		    "mlx5_vport_alloc_q_counter failed: %d\n", err);
3266 		goto err_close_tises;
3267 	}
3268 	/* store counter set ID */
3269 	priv->counter_set_id = set_id;
3270 
3271 	err = mlx5e_open_channels(priv);
3272 	if (err) {
3273 		mlx5_en_err(ifp,
3274 		    "mlx5e_open_channels failed, %d\n", err);
3275 		goto err_dalloc_q_counter;
3276 	}
3277 	err = mlx5e_activate_rqt(priv);
3278 	if (err) {
3279 		mlx5_en_err(ifp, "mlx5e_activate_rqt failed, %d\n", err);
3280 		goto err_close_channels;
3281 	}
3282 
3283 	set_bit(MLX5E_STATE_OPENED, &priv->state);
3284 
3285 	mlx5e_update_carrier(priv);
3286 
3287 	return (0);
3288 
3289 err_close_channels:
3290 	mlx5e_close_channels(priv);
3291 
3292 err_dalloc_q_counter:
3293 	mlx5_vport_dealloc_q_counter(priv->mdev,
3294 	    MLX5_INTERFACE_PROTOCOL_ETH, priv->counter_set_id);
3295 
3296 err_close_tises:
3297 	mlx5e_close_tises(priv);
3298 
3299 	return (err);
3300 }
3301 
3302 static void
mlx5e_open(void * arg)3303 mlx5e_open(void *arg)
3304 {
3305 	struct mlx5e_priv *priv = arg;
3306 
3307 	PRIV_LOCK(priv);
3308 	if (mlx5_set_port_status(priv->mdev, MLX5_PORT_UP))
3309 		mlx5_en_err(priv->ifp,
3310 		    "Setting port status to up failed\n");
3311 
3312 	mlx5e_open_locked(priv->ifp);
3313 	if_setdrvflagbits(priv->ifp, IFF_DRV_RUNNING, 0);
3314 	PRIV_UNLOCK(priv);
3315 }
3316 
3317 int
mlx5e_close_locked(if_t ifp)3318 mlx5e_close_locked(if_t ifp)
3319 {
3320 	struct mlx5e_priv *priv = if_getsoftc(ifp);
3321 
3322 	/* check if already closed */
3323 	if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
3324 		return (0);
3325 
3326 	clear_bit(MLX5E_STATE_OPENED, &priv->state);
3327 
3328 	if_link_state_change(priv->ifp, LINK_STATE_DOWN);
3329 
3330 	mlx5e_deactivate_rqt(priv);
3331 	mlx5e_close_channels(priv);
3332 	mlx5_vport_dealloc_q_counter(priv->mdev,
3333 	    MLX5_INTERFACE_PROTOCOL_ETH, priv->counter_set_id);
3334 	mlx5e_close_tises(priv);
3335 
3336 	return (0);
3337 }
3338 
3339 static uint64_t
mlx5e_get_counter(if_t ifp,ift_counter cnt)3340 mlx5e_get_counter(if_t ifp, ift_counter cnt)
3341 {
3342 	struct mlx5e_priv *priv = if_getsoftc(ifp);
3343 	u64 retval;
3344 
3345 	/* PRIV_LOCK(priv); XXX not allowed */
3346 	switch (cnt) {
3347 	case IFCOUNTER_IPACKETS:
3348 		retval = priv->stats.vport.rx_packets;
3349 		break;
3350 	case IFCOUNTER_IERRORS:
3351 		retval = priv->stats.pport.in_range_len_errors +
3352 		    priv->stats.pport.out_of_range_len +
3353 		    priv->stats.pport.too_long_errors +
3354 		    priv->stats.pport.check_seq_err +
3355 		    priv->stats.pport.alignment_err;
3356 		break;
3357 	case IFCOUNTER_IQDROPS:
3358 		retval = priv->stats.vport.rx_out_of_buffer;
3359 		break;
3360 	case IFCOUNTER_OPACKETS:
3361 		retval = priv->stats.vport.tx_packets;
3362 		break;
3363 	case IFCOUNTER_OERRORS:
3364 		retval = priv->stats.port_stats_debug.out_discards;
3365 		break;
3366 	case IFCOUNTER_IBYTES:
3367 		retval = priv->stats.vport.rx_bytes;
3368 		break;
3369 	case IFCOUNTER_OBYTES:
3370 		retval = priv->stats.vport.tx_bytes;
3371 		break;
3372 	case IFCOUNTER_IMCASTS:
3373 		retval = priv->stats.vport.rx_multicast_packets;
3374 		break;
3375 	case IFCOUNTER_OMCASTS:
3376 		retval = priv->stats.vport.tx_multicast_packets;
3377 		break;
3378 	case IFCOUNTER_OQDROPS:
3379 		retval = priv->stats.vport.tx_queue_dropped;
3380 		break;
3381 	case IFCOUNTER_COLLISIONS:
3382 		retval = priv->stats.pport.collisions;
3383 		break;
3384 	default:
3385 		retval = if_get_counter_default(ifp, cnt);
3386 		break;
3387 	}
3388 	/* PRIV_UNLOCK(priv); XXX not allowed */
3389 	return (retval);
3390 }
3391 
3392 static void
mlx5e_set_rx_mode(if_t ifp)3393 mlx5e_set_rx_mode(if_t ifp)
3394 {
3395 	struct mlx5e_priv *priv = if_getsoftc(ifp);
3396 
3397 	queue_work(priv->wq, &priv->set_rx_mode_work);
3398 }
3399 
3400 static int
mlx5e_ioctl(if_t ifp,u_long command,caddr_t data)3401 mlx5e_ioctl(if_t ifp, u_long command, caddr_t data)
3402 {
3403 	struct mlx5e_priv *priv;
3404 	struct ifreq *ifr;
3405 	struct ifdownreason *ifdr;
3406 	struct ifi2creq i2c;
3407 	struct ifrsskey *ifrk;
3408 	struct ifrsshash *ifrh;
3409 	struct siocsifcapnv_driver_data *drv_ioctl_data, drv_ioctl_data_d;
3410 	int error = 0;
3411 	int mask;
3412 	int size_read = 0;
3413 	int module_status;
3414 	int module_num;
3415 	int max_mtu;
3416 	uint8_t read_addr;
3417 
3418 	priv = if_getsoftc(ifp);
3419 
3420 	/* check if detaching */
3421 	if (priv == NULL || priv->gone != 0)
3422 		return (ENXIO);
3423 
3424 	switch (command) {
3425 	case SIOCSIFMTU:
3426 		ifr = (struct ifreq *)data;
3427 
3428 		PRIV_LOCK(priv);
3429 		mlx5_query_port_max_mtu(priv->mdev, &max_mtu);
3430 
3431 		if (ifr->ifr_mtu >= MLX5E_MTU_MIN &&
3432 		    ifr->ifr_mtu <= MIN(MLX5E_MTU_MAX, max_mtu)) {
3433 			int was_opened;
3434 
3435 			was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
3436 			if (was_opened)
3437 				mlx5e_close_locked(ifp);
3438 
3439 			/* set new MTU */
3440 			mlx5e_set_dev_port_mtu(ifp, ifr->ifr_mtu);
3441 
3442 			if (was_opened)
3443 				mlx5e_open_locked(ifp);
3444 		} else {
3445 			error = EINVAL;
3446 			mlx5_en_err(ifp,
3447 			    "Invalid MTU value. Min val: %d, Max val: %d\n",
3448 			    MLX5E_MTU_MIN, MIN(MLX5E_MTU_MAX, max_mtu));
3449 		}
3450 		PRIV_UNLOCK(priv);
3451 		break;
3452 	case SIOCSIFFLAGS:
3453 		if ((if_getflags(ifp) & IFF_UP) &&
3454 		    (if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
3455 			mlx5e_set_rx_mode(ifp);
3456 			break;
3457 		}
3458 		PRIV_LOCK(priv);
3459 		if (if_getflags(ifp) & IFF_UP) {
3460 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
3461 				if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
3462 					mlx5e_open_locked(ifp);
3463 				if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
3464 				mlx5_set_port_status(priv->mdev, MLX5_PORT_UP);
3465 			}
3466 		} else {
3467 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3468 				mlx5_set_port_status(priv->mdev,
3469 				    MLX5_PORT_DOWN);
3470 				if (test_bit(MLX5E_STATE_OPENED, &priv->state) != 0)
3471 					mlx5e_close_locked(ifp);
3472 				mlx5e_update_carrier(priv);
3473 				if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
3474 			}
3475 		}
3476 		PRIV_UNLOCK(priv);
3477 		break;
3478 	case SIOCADDMULTI:
3479 	case SIOCDELMULTI:
3480 		mlx5e_set_rx_mode(ifp);
3481 		break;
3482 	case SIOCSIFMEDIA:
3483 	case SIOCGIFMEDIA:
3484 	case SIOCGIFXMEDIA:
3485 		ifr = (struct ifreq *)data;
3486 		error = ifmedia_ioctl(ifp, ifr, &priv->media, command);
3487 		break;
3488 	case SIOCGIFCAPNV:
3489 		error = 0;
3490 		break;
3491 	case SIOCSIFCAP:
3492 		ifr = (struct ifreq *)data;
3493 		drv_ioctl_data = &drv_ioctl_data_d;
3494 		drv_ioctl_data->reqcap = ifr->ifr_reqcap;
3495 		PRIV_LOCK(priv);
3496 		drv_ioctl_data->reqcap2 = if_getcapenable2(ifp);
3497 		drv_ioctl_data->nvcap = NULL;
3498 		goto siocsifcap_driver;
3499 	case SIOCSIFCAPNV:
3500 		drv_ioctl_data = (struct siocsifcapnv_driver_data *)data;
3501 		PRIV_LOCK(priv);
3502 siocsifcap_driver:
3503 		mask = drv_ioctl_data->reqcap ^ if_getcapenable(ifp);
3504 
3505 		if (mask & IFCAP_TXCSUM) {
3506 			if_togglecapenable(ifp, IFCAP_TXCSUM);
3507 			if_togglehwassist(ifp, (CSUM_TCP | CSUM_UDP | CSUM_IP));
3508 
3509 			if (IFCAP_TSO4 & if_getcapenable(ifp) &&
3510 			    !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
3511 				mask &= ~IFCAP_TSO4;
3512 				if_setcapenablebit(ifp, 0, IFCAP_TSO4);
3513 				if_sethwassistbits(ifp, 0, CSUM_IP_TSO);
3514 				mlx5_en_err(ifp,
3515 				    "tso4 disabled due to -txcsum.\n");
3516 			}
3517 		}
3518 		if (mask & IFCAP_TXCSUM_IPV6) {
3519 			if_togglecapenable(ifp, IFCAP_TXCSUM_IPV6);
3520 			if_togglehwassist(ifp, (CSUM_UDP_IPV6 | CSUM_TCP_IPV6));
3521 
3522 			if (IFCAP_TSO6 & if_getcapenable(ifp) &&
3523 			    !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
3524 				mask &= ~IFCAP_TSO6;
3525 				if_setcapenablebit(ifp, 0, IFCAP_TSO6);
3526 				if_sethwassistbits(ifp, 0, CSUM_IP6_TSO);
3527 				mlx5_en_err(ifp,
3528 				    "tso6 disabled due to -txcsum6.\n");
3529 			}
3530 		}
3531 		if (mask & IFCAP_MEXTPG)
3532 			if_togglecapenable(ifp, IFCAP_MEXTPG);
3533 		if (mask & IFCAP_TXTLS4)
3534 			if_togglecapenable(ifp, IFCAP_TXTLS4);
3535 		if (mask & IFCAP_TXTLS6)
3536 			if_togglecapenable(ifp, IFCAP_TXTLS6);
3537 #ifdef RATELIMIT
3538 		if (mask & IFCAP_TXTLS_RTLMT)
3539 			if_togglecapenable(ifp, IFCAP_TXTLS_RTLMT);
3540 #endif
3541 		if (mask & IFCAP_RXCSUM)
3542 			if_togglecapenable(ifp, IFCAP_RXCSUM);
3543 		if (mask & IFCAP_RXCSUM_IPV6)
3544 			if_togglecapenable(ifp, IFCAP_RXCSUM_IPV6);
3545 		if (mask & IFCAP_TSO4) {
3546 			if (!(IFCAP_TSO4 & if_getcapenable(ifp)) &&
3547 			    !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
3548 				mlx5_en_err(ifp, "enable txcsum first.\n");
3549 				error = EAGAIN;
3550 				goto out;
3551 			}
3552 			if_togglecapenable(ifp, IFCAP_TSO4);
3553 			if_togglehwassist(ifp, CSUM_IP_TSO);
3554 		}
3555 		if (mask & IFCAP_TSO6) {
3556 			if (!(IFCAP_TSO6 & if_getcapenable(ifp)) &&
3557 			    !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
3558 				mlx5_en_err(ifp, "enable txcsum6 first.\n");
3559 				error = EAGAIN;
3560 				goto out;
3561 			}
3562 			if_togglecapenable(ifp, IFCAP_TSO6);
3563 			if_togglehwassist(ifp, CSUM_IP6_TSO);
3564 		}
3565 		if (mask & IFCAP_VLAN_HWTSO)
3566 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
3567 		if (mask & IFCAP_VLAN_HWFILTER) {
3568 			if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER)
3569 				mlx5e_disable_vlan_filter(priv);
3570 			else
3571 				mlx5e_enable_vlan_filter(priv);
3572 
3573 			if_togglecapenable(ifp, IFCAP_VLAN_HWFILTER);
3574 		}
3575 		if (mask & IFCAP_VLAN_HWTAGGING)
3576 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
3577 		if (mask & IFCAP_WOL_MAGIC)
3578 			if_togglecapenable(ifp, IFCAP_WOL_MAGIC);
3579 		if (mask & IFCAP_VXLAN_HWCSUM) {
3580 			const bool was_enabled =
3581 			    (if_getcapenable(ifp) & IFCAP_VXLAN_HWCSUM) != 0;
3582 			if (was_enabled)
3583 				mlx5e_del_all_vxlan_rules(priv);
3584 			if_togglecapenable(ifp, IFCAP_VXLAN_HWCSUM);
3585 			if_togglehwassist(ifp, CSUM_INNER_IP | CSUM_INNER_IP_UDP |
3586 			    CSUM_INNER_IP_TCP | CSUM_INNER_IP6_UDP |
3587 			    CSUM_INNER_IP6_TCP);
3588 			if (!was_enabled) {
3589 				int err = mlx5e_add_all_vxlan_rules(priv);
3590 				if (err != 0) {
3591 					mlx5_en_err(ifp,
3592 					    "mlx5e_add_all_vxlan_rules() failed, %d (ignored)\n", err);
3593 				}
3594 			}
3595 		}
3596 		if (mask & IFCAP_VXLAN_HWTSO) {
3597 			if_togglecapenable(ifp, IFCAP_VXLAN_HWTSO);
3598 			if_togglehwassist(ifp, CSUM_INNER_IP_TSO |
3599 			    CSUM_INNER_IP6_TSO);
3600 		}
3601 
3602 		VLAN_CAPABILITIES(ifp);
3603 		/* turn off LRO means also turn of HW LRO - if it's on */
3604 		if (mask & IFCAP_LRO) {
3605 			int was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
3606 			bool need_restart = false;
3607 
3608 			if_togglecapenable(ifp, IFCAP_LRO);
3609 
3610 			/* figure out if updating HW LRO is needed */
3611 			if (!(if_getcapenable(ifp) & IFCAP_LRO)) {
3612 				if (priv->params.hw_lro_en) {
3613 					priv->params.hw_lro_en = false;
3614 					need_restart = true;
3615 				}
3616 			} else {
3617 				if (priv->params.hw_lro_en == false &&
3618 				    priv->params_ethtool.hw_lro != 0) {
3619 					priv->params.hw_lro_en = true;
3620 					need_restart = true;
3621 				}
3622 			}
3623 			if (was_opened && need_restart) {
3624 				mlx5e_close_locked(ifp);
3625 				mlx5e_open_locked(ifp);
3626 			}
3627 		}
3628 		if (mask & IFCAP_HWRXTSTMP) {
3629 			if_togglecapenable(ifp, IFCAP_HWRXTSTMP);
3630 			if (if_getcapenable(ifp) & IFCAP_HWRXTSTMP) {
3631 				if (priv->clbr_done == 0)
3632 					mlx5e_reset_calibration_callout(priv);
3633 			} else {
3634 				callout_drain(&priv->tstmp_clbr);
3635 				priv->clbr_done = 0;
3636 			}
3637 		}
3638 		mask = drv_ioctl_data->reqcap2 ^ if_getcapenable2(ifp);
3639 		if ((mask & IFCAP2_BIT(IFCAP2_RXTLS4)) != 0)
3640 			if_togglecapenable2(ifp, IFCAP2_BIT(IFCAP2_RXTLS4));
3641 		if ((mask & IFCAP2_BIT(IFCAP2_RXTLS6)) != 0)
3642 			if_togglecapenable2(ifp, IFCAP2_BIT(IFCAP2_RXTLS6));
3643 out:
3644 		PRIV_UNLOCK(priv);
3645 		break;
3646 
3647 	case SIOCGI2C:
3648 		ifr = (struct ifreq *)data;
3649 
3650 		/*
3651 		 * Copy from the user-space address ifr_data to the
3652 		 * kernel-space address i2c
3653 		 */
3654 		error = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
3655 		if (error)
3656 			break;
3657 
3658 		if (i2c.len > sizeof(i2c.data)) {
3659 			error = EINVAL;
3660 			break;
3661 		}
3662 
3663 		PRIV_LOCK(priv);
3664 		/* Get module_num which is required for the query_eeprom */
3665 		error = mlx5_query_module_num(priv->mdev, &module_num);
3666 		if (error) {
3667 			mlx5_en_err(ifp,
3668 			    "Query module num failed, eeprom reading is not supported\n");
3669 			error = EINVAL;
3670 			goto err_i2c;
3671 		}
3672 		/* Check if module is present before doing an access */
3673 		module_status = mlx5_query_module_status(priv->mdev, module_num);
3674 		if (module_status != MLX5_MODULE_STATUS_PLUGGED_ENABLED) {
3675 			mlx5_en_err(ifp,
3676 			    "Query module %d status: not plugged (%d), eeprom reading is not supported\n",
3677 			    module_num, module_status);
3678 			error = EINVAL;
3679 			goto err_i2c;
3680 		}
3681 		/*
3682 		 * Currently 0XA0 and 0xA2 are the only addresses permitted.
3683 		 * The internal conversion is as follows:
3684 		 */
3685 		if (i2c.dev_addr == 0xA0)
3686 			read_addr = MLX5_I2C_ADDR_LOW;
3687 		else if (i2c.dev_addr == 0xA2)
3688 			read_addr = MLX5_I2C_ADDR_HIGH;
3689 		else {
3690 			mlx5_en_err(ifp,
3691 			    "Query eeprom failed, Invalid Address: %X\n",
3692 			    i2c.dev_addr);
3693 			error = EINVAL;
3694 			goto err_i2c;
3695 		}
3696 		error = mlx5_query_eeprom(priv->mdev,
3697 		    read_addr, MLX5_EEPROM_LOW_PAGE,
3698 		    (uint32_t)i2c.offset, (uint32_t)i2c.len, module_num,
3699 		    (uint32_t *)i2c.data, &size_read);
3700 		if (error) {
3701 			mlx5_en_err(ifp,
3702 			    "Query eeprom failed, eeprom reading is not supported\n");
3703 			error = EINVAL;
3704 			goto err_i2c;
3705 		}
3706 
3707 		if (i2c.len > MLX5_EEPROM_MAX_BYTES) {
3708 			error = mlx5_query_eeprom(priv->mdev,
3709 			    read_addr, MLX5_EEPROM_LOW_PAGE,
3710 			    (uint32_t)(i2c.offset + size_read),
3711 			    (uint32_t)(i2c.len - size_read), module_num,
3712 			    (uint32_t *)(i2c.data + size_read), &size_read);
3713 		}
3714 		if (error) {
3715 			mlx5_en_err(ifp,
3716 			    "Query eeprom failed, eeprom reading is not supported\n");
3717 			error = EINVAL;
3718 			goto err_i2c;
3719 		}
3720 
3721 		error = copyout(&i2c, ifr_data_get_ptr(ifr), sizeof(i2c));
3722 err_i2c:
3723 		PRIV_UNLOCK(priv);
3724 		break;
3725 	case SIOCGIFDOWNREASON:
3726 		ifdr = (struct ifdownreason *)data;
3727 		bzero(ifdr->ifdr_msg, sizeof(ifdr->ifdr_msg));
3728 		PRIV_LOCK(priv);
3729 		error = -mlx5_query_pddr_troubleshooting_info(priv->mdev, NULL,
3730 		    ifdr->ifdr_msg, sizeof(ifdr->ifdr_msg));
3731 		PRIV_UNLOCK(priv);
3732 		if (error == 0)
3733 			ifdr->ifdr_reason = IFDR_REASON_MSG;
3734 		break;
3735 
3736 	case SIOCGIFRSSKEY:
3737 		ifrk = (struct ifrsskey *)data;
3738 		ifrk->ifrk_func = RSS_FUNC_TOEPLITZ;
3739 		ifrk->ifrk_keylen = MLX5E_RSS_KEY_SIZE;
3740 		CTASSERT(sizeof(ifrk->ifrk_key) >= MLX5E_RSS_KEY_SIZE);
3741 		mlx5e_get_rss_key(ifrk->ifrk_key);
3742 		break;
3743 
3744 	case SIOCGIFRSSHASH:
3745 		ifrh = (struct ifrsshash *)data;
3746 		ifrh->ifrh_func = RSS_FUNC_TOEPLITZ;
3747 		ifrh->ifrh_types =
3748 		    RSS_TYPE_IPV4 |
3749 		    RSS_TYPE_TCP_IPV4 |
3750 		    RSS_TYPE_UDP_IPV4 |
3751 		    RSS_TYPE_IPV6 |
3752 		    RSS_TYPE_TCP_IPV6 |
3753 		    RSS_TYPE_UDP_IPV6;
3754 		break;
3755 
3756 	default:
3757 		error = ether_ioctl(ifp, command, data);
3758 		break;
3759 	}
3760 	return (error);
3761 }
3762 
3763 static int
mlx5e_check_required_hca_cap(struct mlx5_core_dev * mdev)3764 mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
3765 {
3766 	/*
3767 	 * TODO: uncoment once FW really sets all these bits if
3768 	 * (!mdev->caps.eth.rss_ind_tbl_cap || !mdev->caps.eth.csum_cap ||
3769 	 * !mdev->caps.eth.max_lso_cap || !mdev->caps.eth.vlan_cap ||
3770 	 * !(mdev->caps.gen.flags & MLX5_DEV_CAP_FLAG_SCQE_BRK_MOD)) return
3771 	 * -ENOTSUPP;
3772 	 */
3773 
3774 	/* TODO: add more must-to-have features */
3775 
3776 	if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
3777 		return (-ENODEV);
3778 
3779 	return (0);
3780 }
3781 
3782 static u16
mlx5e_get_max_inline_cap(struct mlx5_core_dev * mdev)3783 mlx5e_get_max_inline_cap(struct mlx5_core_dev *mdev)
3784 {
3785 	const int min_size = ETHER_VLAN_ENCAP_LEN + ETHER_HDR_LEN;
3786 	const int max_size = MLX5E_MAX_TX_INLINE;
3787 	const int bf_buf_size =
3788 	    ((1U << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2U) -
3789 	    (sizeof(struct mlx5e_tx_wqe) - 2);
3790 
3791 	/* verify against driver limits */
3792 	if (bf_buf_size > max_size)
3793 		return (max_size);
3794 	else if (bf_buf_size < min_size)
3795 		return (min_size);
3796 	else
3797 		return (bf_buf_size);
3798 }
3799 
3800 static int
mlx5e_build_ifp_priv(struct mlx5_core_dev * mdev,struct mlx5e_priv * priv,int num_comp_vectors)3801 mlx5e_build_ifp_priv(struct mlx5_core_dev *mdev,
3802     struct mlx5e_priv *priv,
3803     int num_comp_vectors)
3804 {
3805 	int err;
3806 
3807 	/*
3808 	 * TODO: Consider link speed for setting "log_sq_size",
3809 	 * "log_rq_size" and "cq_moderation_xxx":
3810 	 */
3811 	priv->params.log_sq_size =
3812 	    MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
3813 	priv->params.log_rq_size =
3814 	    MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
3815 	priv->params.rx_cq_moderation_usec =
3816 	    MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
3817 	    MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE :
3818 	    MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
3819 	priv->params.rx_cq_moderation_mode =
3820 	    MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ? 1 : 0;
3821 	priv->params.rx_cq_moderation_pkts =
3822 	    MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
3823 	priv->params.tx_cq_moderation_usec =
3824 	    MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
3825 	priv->params.tx_cq_moderation_pkts =
3826 	    MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
3827 	priv->params.rx_hash_log_tbl_sz =
3828 	    (order_base_2(num_comp_vectors) >
3829 	    MLX5E_PARAMS_DEFAULT_RX_HASH_LOG_TBL_SZ) ?
3830 	    order_base_2(num_comp_vectors) :
3831 	    MLX5E_PARAMS_DEFAULT_RX_HASH_LOG_TBL_SZ;
3832 	priv->params.num_tc = 1;
3833 	priv->params.default_vlan_prio = 0;
3834 	priv->counter_set_id = -1;
3835 	priv->params.tx_max_inline = mlx5e_get_max_inline_cap(mdev);
3836 
3837 	err = mlx5_query_min_inline(mdev, &priv->params.tx_min_inline_mode);
3838 	if (err)
3839 		return (err);
3840 
3841 	/*
3842 	 * hw lro is currently defaulted to off. when it won't anymore we
3843 	 * will consider the HW capability: "!!MLX5_CAP_ETH(mdev, lro_cap)"
3844 	 */
3845 	priv->params.hw_lro_en = false;
3846 	priv->params.lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
3847 
3848 	/*
3849 	 * CQE zipping is off, because the per-packet 32-bit Toeplitz hash
3850 	 * is then not supported. The 32-bit Toeplitz hash is needed to
3851 	 * correctly demultiplex incoming traffic into the expected
3852 	 * network queues.
3853 	 */
3854 	priv->params.cqe_zipping_en = false;
3855 
3856 	priv->mdev = mdev;
3857 	priv->params.num_channels = num_comp_vectors;
3858 	priv->params.channels_rsss = 1;
3859 	priv->order_base_2_num_channels = order_base_2(num_comp_vectors);
3860 	priv->queue_mapping_channel_mask =
3861 	    roundup_pow_of_two(num_comp_vectors) - 1;
3862 	priv->num_tc = priv->params.num_tc;
3863 	priv->default_vlan_prio = priv->params.default_vlan_prio;
3864 
3865 	INIT_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
3866 	INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
3867 	INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
3868 
3869 	return (0);
3870 }
3871 
3872 static void
mlx5e_mkey_set_relaxed_ordering(struct mlx5_core_dev * mdev,void * mkc)3873 mlx5e_mkey_set_relaxed_ordering(struct mlx5_core_dev *mdev, void *mkc)
3874 {
3875 	bool ro_pci_enable =
3876 	    pci_get_relaxed_ordering_enabled(mdev->pdev->dev.bsddev);
3877 	bool ro_write = MLX5_CAP_GEN(mdev, relaxed_ordering_write);
3878 	bool ro_read = MLX5_CAP_GEN(mdev, relaxed_ordering_read);
3879 
3880 	MLX5_SET(mkc, mkc, relaxed_ordering_read, ro_pci_enable && ro_read);
3881 	MLX5_SET(mkc, mkc, relaxed_ordering_write, ro_pci_enable && ro_write);
3882 }
3883 
3884 static int
mlx5e_create_mkey(struct mlx5e_priv * priv,u32 pdn,struct mlx5_core_mkey * mkey)3885 mlx5e_create_mkey(struct mlx5e_priv *priv, u32 pdn,
3886 		  struct mlx5_core_mkey *mkey)
3887 {
3888 	if_t ifp = priv->ifp;
3889 	struct mlx5_core_dev *mdev = priv->mdev;
3890 	int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
3891 	void *mkc;
3892 	u32 *in;
3893 	int err;
3894 
3895 	in = mlx5_vzalloc(inlen);
3896 	if (in == NULL) {
3897 		mlx5_en_err(ifp, "failed to allocate inbox\n");
3898 		return (-ENOMEM);
3899 	}
3900 
3901 	mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
3902 	MLX5_SET(mkc, mkc, access_mode, MLX5_ACCESS_MODE_PA);
3903 	MLX5_SET(mkc, mkc, umr_en, 1);	/* used by HW TLS */
3904 	MLX5_SET(mkc, mkc, lw, 1);
3905 	MLX5_SET(mkc, mkc, lr, 1);
3906 	mlx5e_mkey_set_relaxed_ordering(mdev, mkc);
3907 	MLX5_SET(mkc, mkc, pd, pdn);
3908 	MLX5_SET(mkc, mkc, length64, 1);
3909 	MLX5_SET(mkc, mkc, qpn, 0xffffff);
3910 
3911 	err = mlx5_core_create_mkey(mdev, mkey, in, inlen);
3912 	if (err)
3913 		mlx5_en_err(ifp, "mlx5_core_create_mkey failed, %d\n",
3914 		    err);
3915 
3916 	kvfree(in);
3917 	return (err);
3918 }
3919 
3920 static const char *mlx5e_vport_stats_desc[] = {
3921 	MLX5E_VPORT_STATS(MLX5E_STATS_DESC)
3922 };
3923 
3924 static const char *mlx5e_pport_stats_desc[] = {
3925 	MLX5E_PPORT_STATS(MLX5E_STATS_DESC)
3926 };
3927 
3928 static int
mlx5e_priv_static_init(struct mlx5e_priv * priv,struct mlx5_core_dev * mdev,const uint32_t channels)3929 mlx5e_priv_static_init(struct mlx5e_priv *priv, struct mlx5_core_dev *mdev,
3930     const uint32_t channels)
3931 {
3932 	uint32_t x;
3933 	int err;
3934 
3935 	mtx_init(&priv->async_events_mtx, "mlx5async", MTX_NETWORK_LOCK, MTX_DEF);
3936 	sx_init(&priv->state_lock, "mlx5state");
3937 	callout_init_mtx(&priv->watchdog, &priv->async_events_mtx, 0);
3938 	MLX5_INIT_DOORBELL_LOCK(&priv->doorbell_lock);
3939 	for (x = 0; x != channels; x++)
3940 		mlx5e_chan_static_init(priv, &priv->channel[x], x);
3941 
3942 	for (x = 0; x != channels; x++) {
3943 		err = mlx5_alloc_bfreg(mdev, &priv->channel[x].bfreg, false, false);
3944 		if (err)
3945 			goto err_alloc_bfreg;
3946 	}
3947 	return (0);
3948 
3949 err_alloc_bfreg:
3950 	while (x--)
3951 		mlx5_free_bfreg(mdev, &priv->channel[x].bfreg);
3952 
3953 	for (x = 0; x != channels; x++)
3954 		mlx5e_chan_static_destroy(&priv->channel[x]);
3955 	callout_drain(&priv->watchdog);
3956 	mtx_destroy(&priv->async_events_mtx);
3957 	sx_destroy(&priv->state_lock);
3958 	return (err);
3959 }
3960 
3961 static void
mlx5e_priv_static_destroy(struct mlx5e_priv * priv,struct mlx5_core_dev * mdev,const uint32_t channels)3962 mlx5e_priv_static_destroy(struct mlx5e_priv *priv, struct mlx5_core_dev *mdev,
3963     const uint32_t channels)
3964 {
3965 	uint32_t x;
3966 
3967 	for (x = 0; x != channels; x++)
3968 		mlx5_free_bfreg(mdev, &priv->channel[x].bfreg);
3969 	for (x = 0; x != channels; x++)
3970 		mlx5e_chan_static_destroy(&priv->channel[x]);
3971 	callout_drain(&priv->watchdog);
3972 	mtx_destroy(&priv->async_events_mtx);
3973 	sx_destroy(&priv->state_lock);
3974 }
3975 
3976 static int
sysctl_firmware(SYSCTL_HANDLER_ARGS)3977 sysctl_firmware(SYSCTL_HANDLER_ARGS)
3978 {
3979 	/*
3980 	 * %d.%d%.d the string format.
3981 	 * fw_rev_{maj,min,sub} return u16, 2^16 = 65536.
3982 	 * We need at most 5 chars to store that.
3983 	 * It also has: two "." and NULL at the end, which means we need 18
3984 	 * (5*3 + 3) chars at most.
3985 	 */
3986 	char fw[18];
3987 	struct mlx5e_priv *priv = arg1;
3988 	int error;
3989 
3990 	snprintf(fw, sizeof(fw), "%d.%d.%d", fw_rev_maj(priv->mdev), fw_rev_min(priv->mdev),
3991 	    fw_rev_sub(priv->mdev));
3992 	error = sysctl_handle_string(oidp, fw, sizeof(fw), req);
3993 	return (error);
3994 }
3995 
3996 static void
mlx5e_disable_tx_dma(struct mlx5e_channel * ch)3997 mlx5e_disable_tx_dma(struct mlx5e_channel *ch)
3998 {
3999 	int i;
4000 
4001 	for (i = 0; i < ch->priv->num_tc; i++)
4002 		mlx5e_drain_sq(&ch->sq[i]);
4003 }
4004 
4005 static void
mlx5e_reset_sq_doorbell_record(struct mlx5e_sq * sq)4006 mlx5e_reset_sq_doorbell_record(struct mlx5e_sq *sq)
4007 {
4008 
4009 	sq->doorbell.d32[0] = cpu_to_be32(MLX5_OPCODE_NOP);
4010 	sq->doorbell.d32[1] = cpu_to_be32(sq->sqn << 8);
4011 	mlx5e_tx_notify_hw(sq, true);
4012 }
4013 
4014 void
mlx5e_resume_sq(struct mlx5e_sq * sq)4015 mlx5e_resume_sq(struct mlx5e_sq *sq)
4016 {
4017 	int err;
4018 
4019 	/* check if already enabled */
4020 	if (READ_ONCE(sq->running) != 0)
4021 		return;
4022 
4023 	err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_ERR,
4024 	    MLX5_SQC_STATE_RST);
4025 	if (err != 0) {
4026 		mlx5_en_err(sq->ifp,
4027 		    "mlx5e_modify_sq() from ERR to RST failed: %d\n", err);
4028 	}
4029 
4030 	sq->cc = 0;
4031 	sq->pc = 0;
4032 
4033 	/* reset doorbell prior to moving from RST to RDY */
4034 	mlx5e_reset_sq_doorbell_record(sq);
4035 
4036 	err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST,
4037 	    MLX5_SQC_STATE_RDY);
4038 	if (err != 0) {
4039 		mlx5_en_err(sq->ifp,
4040 		    "mlx5e_modify_sq() from RST to RDY failed: %d\n", err);
4041 	}
4042 
4043 	sq->cev_next_state = MLX5E_CEV_STATE_INITIAL;
4044 	WRITE_ONCE(sq->running, 1);
4045 }
4046 
4047 static void
mlx5e_enable_tx_dma(struct mlx5e_channel * ch)4048 mlx5e_enable_tx_dma(struct mlx5e_channel *ch)
4049 {
4050         int i;
4051 
4052 	for (i = 0; i < ch->priv->num_tc; i++)
4053 		mlx5e_resume_sq(&ch->sq[i]);
4054 }
4055 
4056 static void
mlx5e_disable_rx_dma(struct mlx5e_channel * ch)4057 mlx5e_disable_rx_dma(struct mlx5e_channel *ch)
4058 {
4059 	struct mlx5e_rq *rq = &ch->rq;
4060 	struct epoch_tracker et;
4061 	int err;
4062 
4063 	mtx_lock(&rq->mtx);
4064 	rq->enabled = 0;
4065 	callout_stop(&rq->watchdog);
4066 	mtx_unlock(&rq->mtx);
4067 
4068 	err = mlx5e_modify_rq(rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
4069 	if (err != 0) {
4070 		mlx5_en_err(rq->ifp,
4071 		    "mlx5e_modify_rq() from RDY to RST failed: %d\n", err);
4072 	}
4073 
4074 	while (!mlx5_wq_ll_is_empty(&rq->wq)) {
4075 		msleep(1);
4076 		NET_EPOCH_ENTER(et);
4077 		rq->cq.mcq.comp(&rq->cq.mcq, NULL);
4078 		NET_EPOCH_EXIT(et);
4079 	}
4080 
4081 	/*
4082 	 * Transitioning into RST state will allow the FW to track less ERR state queues,
4083 	 * thus reducing the recv queue flushing time
4084 	 */
4085 	err = mlx5e_modify_rq(rq, MLX5_RQC_STATE_ERR, MLX5_RQC_STATE_RST);
4086 	if (err != 0) {
4087 		mlx5_en_err(rq->ifp,
4088 		    "mlx5e_modify_rq() from ERR to RST failed: %d\n", err);
4089 	}
4090 }
4091 
4092 static void
mlx5e_enable_rx_dma(struct mlx5e_channel * ch)4093 mlx5e_enable_rx_dma(struct mlx5e_channel *ch)
4094 {
4095 	struct mlx5e_rq *rq = &ch->rq;
4096 	struct epoch_tracker et;
4097 	int err;
4098 
4099 	rq->wq.wqe_ctr = 0;
4100 	mlx5_wq_ll_update_db_record(&rq->wq);
4101 	err = mlx5e_modify_rq(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
4102 	if (err != 0) {
4103 		mlx5_en_err(rq->ifp,
4104 		    "mlx5e_modify_rq() from RST to RDY failed: %d\n", err);
4105         }
4106 
4107 	rq->enabled = 1;
4108 
4109 	NET_EPOCH_ENTER(et);
4110 	rq->cq.mcq.comp(&rq->cq.mcq, NULL);
4111 	NET_EPOCH_EXIT(et);
4112 }
4113 
4114 void
mlx5e_modify_tx_dma(struct mlx5e_priv * priv,uint8_t value)4115 mlx5e_modify_tx_dma(struct mlx5e_priv *priv, uint8_t value)
4116 {
4117 	int i;
4118 
4119 	if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
4120 		return;
4121 
4122 	for (i = 0; i < priv->params.num_channels; i++) {
4123 		if (value)
4124 			mlx5e_disable_tx_dma(&priv->channel[i]);
4125 		else
4126 			mlx5e_enable_tx_dma(&priv->channel[i]);
4127 	}
4128 }
4129 
4130 void
mlx5e_modify_rx_dma(struct mlx5e_priv * priv,uint8_t value)4131 mlx5e_modify_rx_dma(struct mlx5e_priv *priv, uint8_t value)
4132 {
4133 	int i;
4134 
4135 	if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
4136 		return;
4137 
4138 	for (i = 0; i < priv->params.num_channels; i++) {
4139 		if (value)
4140 			mlx5e_disable_rx_dma(&priv->channel[i]);
4141 		else
4142 			mlx5e_enable_rx_dma(&priv->channel[i]);
4143 	}
4144 }
4145 
4146 static void
mlx5e_add_hw_stats(struct mlx5e_priv * priv)4147 mlx5e_add_hw_stats(struct mlx5e_priv *priv)
4148 {
4149 	SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_hw),
4150 	    OID_AUTO, "fw_version", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
4151 	    priv, 0, sysctl_firmware, "A", "HCA firmware version");
4152 
4153 	SYSCTL_ADD_STRING(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_hw),
4154 	    OID_AUTO, "board_id", CTLFLAG_RD, priv->mdev->board_id, 0,
4155 	    "Board ID");
4156 }
4157 
4158 static int
mlx5e_sysctl_tx_priority_flow_control(SYSCTL_HANDLER_ARGS)4159 mlx5e_sysctl_tx_priority_flow_control(SYSCTL_HANDLER_ARGS)
4160 {
4161 	struct mlx5e_priv *priv = arg1;
4162 	uint8_t temp[MLX5E_MAX_PRIORITY];
4163 	uint32_t tx_pfc;
4164 	int err;
4165 	int i;
4166 
4167 	PRIV_LOCK(priv);
4168 
4169 	tx_pfc = priv->params.tx_priority_flow_control;
4170 
4171 	for (i = 0; i != MLX5E_MAX_PRIORITY; i++)
4172 		temp[i] = (tx_pfc >> i) & 1;
4173 
4174 	err = SYSCTL_OUT(req, temp, MLX5E_MAX_PRIORITY);
4175 	if (err || !req->newptr)
4176 		goto done;
4177 	err = SYSCTL_IN(req, temp, MLX5E_MAX_PRIORITY);
4178 	if (err)
4179 		goto done;
4180 
4181 	priv->params.tx_priority_flow_control = 0;
4182 
4183 	/* range check input value */
4184 	for (i = 0; i != MLX5E_MAX_PRIORITY; i++) {
4185 		if (temp[i] > 1) {
4186 			err = ERANGE;
4187 			goto done;
4188 		}
4189 		priv->params.tx_priority_flow_control |= (temp[i] << i);
4190 	}
4191 
4192 	/* check if update is required */
4193 	if (tx_pfc != priv->params.tx_priority_flow_control)
4194 		err = -mlx5e_set_port_pfc(priv);
4195 done:
4196 	if (err != 0)
4197 		priv->params.tx_priority_flow_control= tx_pfc;
4198 	PRIV_UNLOCK(priv);
4199 
4200 	return (err);
4201 }
4202 
4203 static int
mlx5e_sysctl_rx_priority_flow_control(SYSCTL_HANDLER_ARGS)4204 mlx5e_sysctl_rx_priority_flow_control(SYSCTL_HANDLER_ARGS)
4205 {
4206 	struct mlx5e_priv *priv = arg1;
4207 	uint8_t temp[MLX5E_MAX_PRIORITY];
4208 	uint32_t rx_pfc;
4209 	int err;
4210 	int i;
4211 
4212 	PRIV_LOCK(priv);
4213 
4214 	rx_pfc = priv->params.rx_priority_flow_control;
4215 
4216 	for (i = 0; i != MLX5E_MAX_PRIORITY; i++)
4217 		temp[i] = (rx_pfc >> i) & 1;
4218 
4219 	err = SYSCTL_OUT(req, temp, MLX5E_MAX_PRIORITY);
4220 	if (err || !req->newptr)
4221 		goto done;
4222 	err = SYSCTL_IN(req, temp, MLX5E_MAX_PRIORITY);
4223 	if (err)
4224 		goto done;
4225 
4226 	priv->params.rx_priority_flow_control = 0;
4227 
4228 	/* range check input value */
4229 	for (i = 0; i != MLX5E_MAX_PRIORITY; i++) {
4230 		if (temp[i] > 1) {
4231 			err = ERANGE;
4232 			goto done;
4233 		}
4234 		priv->params.rx_priority_flow_control |= (temp[i] << i);
4235 	}
4236 
4237 	/* check if update is required */
4238 	if (rx_pfc != priv->params.rx_priority_flow_control) {
4239 		err = -mlx5e_set_port_pfc(priv);
4240 		if (err == 0 && priv->sw_is_port_buf_owner)
4241 			err = mlx5e_update_buf_lossy(priv);
4242 	}
4243 done:
4244 	if (err != 0)
4245 		priv->params.rx_priority_flow_control= rx_pfc;
4246 	PRIV_UNLOCK(priv);
4247 
4248 	return (err);
4249 }
4250 
4251 static void
mlx5e_setup_pauseframes(struct mlx5e_priv * priv)4252 mlx5e_setup_pauseframes(struct mlx5e_priv *priv)
4253 {
4254 	int error;
4255 
4256 	/* enable pauseframes by default */
4257 	priv->params.tx_pauseframe_control = 1;
4258 	priv->params.rx_pauseframe_control = 1;
4259 
4260 	/* disable ports flow control, PFC, by default */
4261 	priv->params.tx_priority_flow_control = 0;
4262 	priv->params.rx_priority_flow_control = 0;
4263 
4264 	/* register pauseframe SYSCTLs */
4265 	SYSCTL_ADD_INT(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
4266 	    OID_AUTO, "tx_pauseframe_control", CTLFLAG_RDTUN,
4267 	    &priv->params.tx_pauseframe_control, 0,
4268 	    "Set to enable TX pause frames. Clear to disable.");
4269 
4270 	SYSCTL_ADD_INT(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
4271 	    OID_AUTO, "rx_pauseframe_control", CTLFLAG_RDTUN,
4272 	    &priv->params.rx_pauseframe_control, 0,
4273 	    "Set to enable RX pause frames. Clear to disable.");
4274 
4275 	/* register priority flow control, PFC, SYSCTLs */
4276 	SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
4277 	    OID_AUTO, "tx_priority_flow_control", CTLTYPE_U8 | CTLFLAG_RWTUN |
4278 	    CTLFLAG_MPSAFE, priv, 0, &mlx5e_sysctl_tx_priority_flow_control, "CU",
4279 	    "Set to enable TX ports flow control frames for priorities 0..7. Clear to disable.");
4280 
4281 	SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
4282 	    OID_AUTO, "rx_priority_flow_control", CTLTYPE_U8 | CTLFLAG_RWTUN |
4283 	    CTLFLAG_MPSAFE, priv, 0, &mlx5e_sysctl_rx_priority_flow_control, "CU",
4284 	    "Set to enable RX ports flow control frames for priorities 0..7. Clear to disable.");
4285 
4286 	PRIV_LOCK(priv);
4287 
4288 	/* range check */
4289 	priv->params.tx_pauseframe_control =
4290 	    priv->params.tx_pauseframe_control ? 1 : 0;
4291 	priv->params.rx_pauseframe_control =
4292 	    priv->params.rx_pauseframe_control ? 1 : 0;
4293 
4294 	/* update firmware */
4295 	error = mlx5e_set_port_pause_and_pfc(priv);
4296 	if (error == -EINVAL) {
4297 		mlx5_en_err(priv->ifp,
4298 		    "Global pauseframes must be disabled before enabling PFC.\n");
4299 		priv->params.rx_priority_flow_control = 0;
4300 		priv->params.tx_priority_flow_control = 0;
4301 
4302 		/* update firmware */
4303 		(void) mlx5e_set_port_pause_and_pfc(priv);
4304 	}
4305 	PRIV_UNLOCK(priv);
4306 }
4307 
4308 static int
mlx5e_ul_snd_tag_alloc(if_t ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** ppmt)4309 mlx5e_ul_snd_tag_alloc(if_t ifp,
4310     union if_snd_tag_alloc_params *params,
4311     struct m_snd_tag **ppmt)
4312 {
4313 	struct mlx5e_priv *priv;
4314 	struct mlx5e_channel *pch;
4315 
4316 	priv = if_getsoftc(ifp);
4317 
4318 	if (unlikely(priv->gone || params->hdr.flowtype == M_HASHTYPE_NONE)) {
4319 		return (EOPNOTSUPP);
4320 	} else {
4321 		/* keep this code synced with mlx5e_select_queue() */
4322 		u32 ch = priv->params.num_channels;
4323 #ifdef RSS
4324 		u32 temp;
4325 
4326 		if (rss_hash2bucket(params->hdr.flowid,
4327 		    params->hdr.flowtype, &temp) == 0)
4328 			ch = temp % ch;
4329 		else
4330 #endif
4331 			ch = (params->hdr.flowid % 128) % ch;
4332 
4333 		/*
4334 		 * NOTE: The channels array is only freed at detach
4335 		 * and it safe to return a pointer to the send tag
4336 		 * inside the channels structure as long as we
4337 		 * reference the priv.
4338 		 */
4339 		pch = priv->channel + ch;
4340 
4341 		/* check if send queue is not running */
4342 		if (unlikely(pch->sq[0].running == 0))
4343 			return (ENXIO);
4344 		m_snd_tag_ref(&pch->tag);
4345 		*ppmt = &pch->tag;
4346 		return (0);
4347 	}
4348 }
4349 
4350 static int
mlx5e_ul_snd_tag_query(struct m_snd_tag * pmt,union if_snd_tag_query_params * params)4351 mlx5e_ul_snd_tag_query(struct m_snd_tag *pmt, union if_snd_tag_query_params *params)
4352 {
4353 	struct mlx5e_channel *pch =
4354 	    container_of(pmt, struct mlx5e_channel, tag);
4355 
4356 	params->unlimited.max_rate = -1ULL;
4357 	params->unlimited.queue_level = mlx5e_sq_queue_level(&pch->sq[0]);
4358 	return (0);
4359 }
4360 
4361 static void
mlx5e_ul_snd_tag_free(struct m_snd_tag * pmt)4362 mlx5e_ul_snd_tag_free(struct m_snd_tag *pmt)
4363 {
4364 	struct mlx5e_channel *pch =
4365 	    container_of(pmt, struct mlx5e_channel, tag);
4366 
4367 	complete(&pch->completion);
4368 }
4369 
4370 static int
mlx5e_snd_tag_alloc(if_t ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** ppmt)4371 mlx5e_snd_tag_alloc(if_t ifp,
4372     union if_snd_tag_alloc_params *params,
4373     struct m_snd_tag **ppmt)
4374 {
4375 
4376 	switch (params->hdr.type) {
4377 #ifdef RATELIMIT
4378 	case IF_SND_TAG_TYPE_RATE_LIMIT:
4379 		return (mlx5e_rl_snd_tag_alloc(ifp, params, ppmt));
4380 #ifdef KERN_TLS
4381 	case IF_SND_TAG_TYPE_TLS_RATE_LIMIT:
4382 		return (mlx5e_tls_snd_tag_alloc(ifp, params, ppmt));
4383 #endif
4384 #endif
4385 	case IF_SND_TAG_TYPE_UNLIMITED:
4386 		return (mlx5e_ul_snd_tag_alloc(ifp, params, ppmt));
4387 #ifdef KERN_TLS
4388 	case IF_SND_TAG_TYPE_TLS:
4389 		return (mlx5e_tls_snd_tag_alloc(ifp, params, ppmt));
4390 	case IF_SND_TAG_TYPE_TLS_RX:
4391 		return (mlx5e_tls_rx_snd_tag_alloc(ifp, params, ppmt));
4392 #endif
4393 	default:
4394 		return (EOPNOTSUPP);
4395 	}
4396 }
4397 
4398 #ifdef RATELIMIT
4399 #define NUM_HDWR_RATES_MLX 13
4400 static const uint64_t adapter_rates_mlx[NUM_HDWR_RATES_MLX] = {
4401 	135375,			/* 1,083,000 */
4402 	180500,			/* 1,444,000 */
4403 	270750,			/* 2,166,000 */
4404 	361000,			/* 2,888,000 */
4405 	541500,			/* 4,332,000 */
4406 	721875,			/* 5,775,000 */
4407 	1082875,		/* 8,663,000 */
4408 	1443875,		/* 11,551,000 */
4409 	2165750,		/* 17,326,000 */
4410 	2887750,		/* 23,102,000 */
4411 	4331625,		/* 34,653,000 */
4412 	5775500,		/* 46,204,000 */
4413 	8663125			/* 69,305,000 */
4414 };
4415 
4416 static void
mlx5e_ratelimit_query(if_t ifp __unused,struct if_ratelimit_query_results * q)4417 mlx5e_ratelimit_query(if_t ifp __unused, struct if_ratelimit_query_results *q)
4418 {
4419 	/*
4420 	 * This function needs updating by the driver maintainer!
4421 	 * For the MLX card there are currently (ConectX-4?) 13
4422 	 * pre-set rates and others i.e. ConnectX-5, 6, 7??
4423 	 *
4424 	 * This will change based on later adapters
4425 	 * and this code should be updated to look at ifp
4426 	 * and figure out the specific adapter type
4427 	 * settings i.e. how many rates as well
4428 	 * as if they are fixed (as is shown here) or
4429 	 * if they are dynamic (example chelsio t4). Also if there
4430 	 * is a maximum number of flows that the adapter
4431 	 * can handle that too needs to be updated in
4432 	 * the max_flows field.
4433 	 */
4434 	q->rate_table = adapter_rates_mlx;
4435 	q->flags = RT_IS_FIXED_TABLE;
4436 	q->max_flows = 0;	/* mlx has no limit */
4437 	q->number_of_rates = NUM_HDWR_RATES_MLX;
4438 	q->min_segment_burst = 1;
4439 }
4440 #endif
4441 
4442 static void
mlx5e_ifm_add(struct mlx5e_priv * priv,int type)4443 mlx5e_ifm_add(struct mlx5e_priv *priv, int type)
4444 {
4445 	ifmedia_add(&priv->media, type | IFM_ETHER, 0, NULL);
4446 	ifmedia_add(&priv->media, type | IFM_ETHER |
4447 	    IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE, 0, NULL);
4448 	ifmedia_add(&priv->media, type | IFM_ETHER | IFM_ETH_RXPAUSE, 0, NULL);
4449 	ifmedia_add(&priv->media, type | IFM_ETHER | IFM_ETH_TXPAUSE, 0, NULL);
4450 	ifmedia_add(&priv->media, type | IFM_ETHER | IFM_FDX, 0, NULL);
4451 	ifmedia_add(&priv->media, type | IFM_ETHER | IFM_FDX |
4452 	    IFM_ETH_RXPAUSE, 0, NULL);
4453 	ifmedia_add(&priv->media, type | IFM_ETHER | IFM_FDX |
4454 	    IFM_ETH_TXPAUSE, 0, NULL);
4455 	ifmedia_add(&priv->media, type | IFM_ETHER | IFM_FDX |
4456 	    IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE, 0, NULL);
4457 }
4458 
4459 static void *
mlx5e_create_ifp(struct mlx5_core_dev * mdev)4460 mlx5e_create_ifp(struct mlx5_core_dev *mdev)
4461 {
4462 	if_t ifp;
4463 	struct mlx5e_priv *priv;
4464 	u8 dev_addr[ETHER_ADDR_LEN] __aligned(4);
4465 	struct sysctl_oid_list *child;
4466 	int ncv = mdev->priv.eq_table.num_comp_vectors;
4467 	char unit[16];
4468 	struct pfil_head_args pa;
4469 	int err;
4470 	u32 eth_proto_cap;
4471 	u32 out[MLX5_ST_SZ_DW(ptys_reg)];
4472 	bool ext;
4473 	struct media media_entry = {};
4474 
4475 	if (mlx5e_check_required_hca_cap(mdev)) {
4476 		mlx5_core_dbg(mdev, "mlx5e_check_required_hca_cap() failed\n");
4477 		return (NULL);
4478 	}
4479 
4480 	/*
4481 	 * Try to allocate the priv and make room for worst-case
4482 	 * number of channel structures:
4483 	 */
4484 	priv = malloc_domainset(sizeof(*priv) +
4485 	    (sizeof(priv->channel[0]) * mdev->priv.eq_table.num_comp_vectors),
4486 	    M_MLX5EN, mlx5_dev_domainset(mdev), M_WAITOK | M_ZERO);
4487 
4488 	ifp = priv->ifp = if_alloc_dev(IFT_ETHER, mdev->pdev->dev.bsddev);
4489 	if (ifp == NULL) {
4490 		mlx5_core_err(mdev, "if_alloc() failed\n");
4491 		goto err_free_priv;
4492 	}
4493 	/* setup all static fields */
4494 	if (mlx5e_priv_static_init(priv, mdev, mdev->priv.eq_table.num_comp_vectors)) {
4495 		mlx5_core_err(mdev, "mlx5e_priv_static_init() failed\n");
4496 		goto err_free_ifp;
4497 	}
4498 
4499 	if_setsoftc(ifp, priv);
4500 	if_initname(ifp, "mce", device_get_unit(mdev->pdev->dev.bsddev));
4501 	if_setmtu(ifp, ETHERMTU);
4502 	if_setinitfn(ifp, mlx5e_open);
4503 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
4504 	if_setioctlfn(ifp, mlx5e_ioctl);
4505 	if_settransmitfn(ifp, mlx5e_xmit);
4506 	if_setqflushfn(ifp, if_qflush);
4507 	if_setgetcounterfn(ifp, mlx5e_get_counter);
4508 	if_setsendqlen(ifp, ifqmaxlen);
4509 	/*
4510          * Set driver features
4511          */
4512 	if_setcapabilities(ifp, IFCAP_NV);
4513 	if_setcapabilitiesbit(ifp, IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6, 0);
4514 	if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING, 0);
4515 	if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWFILTER, 0);
4516 	if_setcapabilitiesbit(ifp, IFCAP_LINKSTATE | IFCAP_JUMBO_MTU, 0);
4517 	if_setcapabilitiesbit(ifp, IFCAP_LRO, 0);
4518 	if_setcapabilitiesbit(ifp, IFCAP_TSO | IFCAP_VLAN_HWTSO, 0);
4519 	if_setcapabilitiesbit(ifp, IFCAP_HWSTATS | IFCAP_HWRXTSTMP, 0);
4520 	if_setcapabilitiesbit(ifp, IFCAP_MEXTPG, 0);
4521 	if_setcapabilitiesbit(ifp, IFCAP_TXTLS4 | IFCAP_TXTLS6, 0);
4522 #ifdef RATELIMIT
4523 	if_setcapabilitiesbit(ifp, IFCAP_TXRTLMT | IFCAP_TXTLS_RTLMT, 0);
4524 #endif
4525 	if_setcapabilitiesbit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
4526 	if_setcapabilities2bit(ifp, IFCAP2_BIT(IFCAP2_RXTLS4) |
4527 	    IFCAP2_BIT(IFCAP2_RXTLS6), 0);
4528 	if_setsndtagallocfn(ifp, mlx5e_snd_tag_alloc);
4529 #ifdef RATELIMIT
4530 	if_setratelimitqueryfn(ifp, mlx5e_ratelimit_query);
4531 #endif
4532 	/* set TSO limits so that we don't have to drop TX packets */
4533 	if_sethwtsomax(ifp, MLX5E_MAX_TX_PAYLOAD_SIZE - (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
4534 	if_sethwtsomaxsegcount(ifp, MLX5E_MAX_TX_MBUF_FRAGS - 1 /* hdr */);
4535 	if_sethwtsomaxsegsize(ifp, MLX5E_MAX_TX_MBUF_SIZE);
4536 
4537 	if_setcapenable(ifp, if_getcapabilities(ifp));
4538 	if_setcapenable2(ifp, if_getcapabilities2(ifp));
4539 	if_sethwassist(ifp, 0);
4540 	if (if_getcapenable(ifp) & IFCAP_TSO)
4541 		if_sethwassistbits(ifp, CSUM_TSO, 0);
4542 	if (if_getcapenable(ifp) & IFCAP_TXCSUM)
4543 		if_sethwassistbits(ifp, (CSUM_TCP | CSUM_UDP | CSUM_IP), 0);
4544 	if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6)
4545 		if_sethwassistbits(ifp, (CSUM_UDP_IPV6 | CSUM_TCP_IPV6), 0);
4546 	if (if_getcapabilities(ifp) & IFCAP_VXLAN_HWCSUM)
4547 		if_sethwassistbits(ifp, CSUM_INNER_IP6_UDP | CSUM_INNER_IP6_TCP |
4548 		    CSUM_INNER_IP | CSUM_INNER_IP_UDP | CSUM_INNER_IP_TCP |
4549 		    CSUM_ENCAP_VXLAN, 0);
4550 	if (if_getcapabilities(ifp) & IFCAP_VXLAN_HWTSO)
4551 		if_sethwassistbits(ifp, CSUM_INNER_IP6_TSO | CSUM_INNER_IP_TSO, 0);
4552 
4553 	/* ifnet sysctl tree */
4554 	sysctl_ctx_init(&priv->sysctl_ctx);
4555 	priv->sysctl_ifnet = SYSCTL_ADD_NODE(&priv->sysctl_ctx, SYSCTL_STATIC_CHILDREN(_dev),
4556 	    OID_AUTO, if_getdname(ifp), CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
4557 	    "MLX5 ethernet - interface name");
4558 	if (priv->sysctl_ifnet == NULL) {
4559 		mlx5_core_err(mdev, "SYSCTL_ADD_NODE() failed\n");
4560 		goto err_free_sysctl;
4561 	}
4562 	snprintf(unit, sizeof(unit), "%d", if_getdunit(ifp));
4563 	priv->sysctl_ifnet = SYSCTL_ADD_NODE(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
4564 	    OID_AUTO, unit, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
4565 	    "MLX5 ethernet - interface unit");
4566 	if (priv->sysctl_ifnet == NULL) {
4567 		mlx5_core_err(mdev, "SYSCTL_ADD_NODE() failed\n");
4568 		goto err_free_sysctl;
4569 	}
4570 
4571 	/* HW sysctl tree */
4572 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(mdev->pdev->dev.bsddev));
4573 	priv->sysctl_hw = SYSCTL_ADD_NODE(&priv->sysctl_ctx, child,
4574 	    OID_AUTO, "hw", CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
4575 	    "MLX5 ethernet dev hw");
4576 	if (priv->sysctl_hw == NULL) {
4577 		mlx5_core_err(mdev, "SYSCTL_ADD_NODE() failed\n");
4578 		goto err_free_sysctl;
4579 	}
4580 
4581 	err = mlx5e_build_ifp_priv(mdev, priv, ncv);
4582 	if (err) {
4583 		mlx5_core_err(mdev, "mlx5e_build_ifp_priv() failed (%d)\n", err);
4584 		goto err_free_sysctl;
4585 	}
4586 
4587 	/* reuse mlx5core's watchdog workqueue */
4588 	priv->wq = mdev->priv.health.wq_watchdog;
4589 
4590 	err = mlx5_core_alloc_pd(mdev, &priv->pdn, 0);
4591 	if (err) {
4592 		mlx5_en_err(ifp, "mlx5_core_alloc_pd failed, %d\n", err);
4593 		goto err_free_wq;
4594 	}
4595 	err = mlx5_alloc_transport_domain(mdev, &priv->tdn, 0);
4596 	if (err) {
4597 		mlx5_en_err(ifp,
4598 		    "mlx5_alloc_transport_domain failed, %d\n", err);
4599 		goto err_dealloc_pd;
4600 	}
4601 	err = mlx5e_create_mkey(priv, priv->pdn, &priv->mr);
4602 	if (err) {
4603 		mlx5_en_err(ifp, "mlx5e_create_mkey failed, %d\n", err);
4604 		goto err_dealloc_transport_domain;
4605 	}
4606 	mlx5_query_nic_vport_mac_address(priv->mdev, 0, dev_addr);
4607 
4608 	/* check if we should generate a random MAC address */
4609 	if (MLX5_CAP_GEN(priv->mdev, vport_group_manager) == 0 &&
4610 	    is_zero_ether_addr(dev_addr)) {
4611 		random_ether_addr(dev_addr);
4612 		mlx5_en_err(ifp, "Assigned random MAC address\n");
4613 	}
4614 
4615 	err = mlx5e_rl_init(priv);
4616 	if (err) {
4617 		mlx5_en_err(ifp, "mlx5e_rl_init failed, %d\n", err);
4618 		goto err_create_mkey;
4619 	}
4620 
4621 	err = mlx5e_tls_init(priv);
4622 	if (err) {
4623 		if_printf(ifp, "%s: mlx5e_tls_init failed\n", __func__);
4624 		goto err_rl_init;
4625 	}
4626 
4627 	err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
4628 	if (err) {
4629 		if_printf(ifp, "%s: mlx5e_open_drop_rq failed (%d)\n", __func__, err);
4630 		goto err_tls_init;
4631 	}
4632 
4633 	err = mlx5e_open_rqts(priv);
4634 	if (err) {
4635 		if_printf(ifp, "%s: mlx5e_open_rqts failed (%d)\n", __func__, err);
4636 		goto err_open_drop_rq;
4637 	}
4638 
4639 	err = mlx5e_open_tirs(priv);
4640 	if (err) {
4641 		mlx5_en_err(ifp, "mlx5e_open_tirs() failed, %d\n", err);
4642 		goto err_open_rqts;
4643 	}
4644 
4645 	err = mlx5e_open_flow_tables(priv);
4646 	if (err) {
4647 		if_printf(ifp, "%s: mlx5e_open_flow_tables failed (%d)\n", __func__, err);
4648 		goto err_open_tirs;
4649 	}
4650 
4651 	err = mlx5e_tls_rx_init(priv);
4652 	if (err) {
4653 		if_printf(ifp, "%s: mlx5e_tls_rx_init() failed, %d\n", __func__, err);
4654 		goto err_open_flow_tables;
4655 	}
4656 
4657 	/* set default MTU */
4658 	mlx5e_set_dev_port_mtu(ifp, if_getmtu(ifp));
4659 
4660 	/* Set default media status */
4661 	priv->media_status_last = IFM_AVALID;
4662 	priv->media_active_last = IFM_ETHER | IFM_AUTO | IFM_FDX;
4663 
4664 	/* setup default pauseframes configuration */
4665 	mlx5e_setup_pauseframes(priv);
4666 
4667 	/* Setup supported medias */
4668 	if (!mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN, 1)) {
4669 		ext = MLX5_CAP_PCAM_FEATURE(mdev,
4670 		    ptys_extended_ethernet);
4671 		eth_proto_cap = MLX5_GET_ETH_PROTO(ptys_reg, out, ext,
4672 		    eth_proto_capability);
4673 	} else {
4674 		ext = false;
4675 		eth_proto_cap = 0;
4676 		mlx5_en_err(ifp, "Query port media capability failed, %d\n", err);
4677 	}
4678 
4679 	ifmedia_init(&priv->media, IFM_IMASK,
4680 	    mlx5e_media_change, mlx5e_media_status);
4681 
4682 	if (ext) {
4683 		for (unsigned i = 0; i != MLX5E_EXT_LINK_SPEEDS_NUMBER; i++) {
4684 			/* check if hardware has the right capability */
4685 			if (MLX5E_PROT_MASK(i) & ~eth_proto_cap)
4686 				continue;
4687 			for (unsigned j = 0; j != MLX5E_CABLE_TYPE_NUMBER; j++) {
4688 				media_entry = mlx5e_ext_mode_table[i][j];
4689 				if (media_entry.subtype == 0)
4690 					continue;
4691 				/* check if this subtype was already added */
4692 				for (unsigned k = 0; k != i; k++) {
4693 					/* check if hardware has the right capability */
4694 					if (MLX5E_PROT_MASK(k) & ~eth_proto_cap)
4695 						continue;
4696 					for (unsigned m = 0; m != MLX5E_CABLE_TYPE_NUMBER; m++) {
4697 						if (media_entry.subtype == mlx5e_ext_mode_table[k][m].subtype)
4698 							goto skip_ext_media;
4699 					}
4700 				}
4701 				mlx5e_ifm_add(priv, media_entry.subtype);
4702 			skip_ext_media:;
4703 			}
4704 		}
4705 	} else {
4706 		for (unsigned i = 0; i != MLX5E_LINK_SPEEDS_NUMBER; i++) {
4707 			media_entry = mlx5e_mode_table[i];
4708 			if (media_entry.subtype == 0)
4709 				continue;
4710 			if (MLX5E_PROT_MASK(i) & ~eth_proto_cap)
4711 				continue;
4712 			/* check if this subtype was already added */
4713 			for (unsigned k = 0; k != i; k++) {
4714 				if (media_entry.subtype == mlx5e_mode_table[k].subtype)
4715 					goto skip_media;
4716 			}
4717 			mlx5e_ifm_add(priv, media_entry.subtype);
4718 
4719 			/* NOTE: 10G ER and LR shares the same entry */
4720 			if (media_entry.subtype == IFM_10G_ER)
4721 				mlx5e_ifm_add(priv, IFM_10G_LR);
4722 		skip_media:;
4723 		}
4724 	}
4725 
4726 	mlx5e_ifm_add(priv, IFM_AUTO);
4727 
4728 	/* Set autoselect by default */
4729 	ifmedia_set(&priv->media, IFM_ETHER | IFM_AUTO | IFM_FDX |
4730 	    IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
4731 
4732 	DEBUGNET_SET(ifp, mlx5_en);
4733 
4734 	ether_ifattach(ifp, dev_addr);
4735 
4736 	/* Register for VLAN events */
4737 	priv->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
4738 	    mlx5e_vlan_rx_add_vid, priv, EVENTHANDLER_PRI_FIRST);
4739 	priv->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
4740 	    mlx5e_vlan_rx_kill_vid, priv, EVENTHANDLER_PRI_FIRST);
4741 
4742 	/* Register for VxLAN events */
4743 	priv->vxlan_start = EVENTHANDLER_REGISTER(vxlan_start,
4744 	    mlx5e_vxlan_start, priv, EVENTHANDLER_PRI_ANY);
4745 	priv->vxlan_stop = EVENTHANDLER_REGISTER(vxlan_stop,
4746 	    mlx5e_vxlan_stop, priv, EVENTHANDLER_PRI_ANY);
4747 
4748 	/* Link is down by default */
4749 	if_link_state_change(ifp, LINK_STATE_DOWN);
4750 
4751 	mlx5e_enable_async_events(priv);
4752 
4753 	mlx5e_add_hw_stats(priv);
4754 
4755 	mlx5e_create_stats(&priv->stats.vport.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
4756 	    "vstats", mlx5e_vport_stats_desc, MLX5E_VPORT_STATS_NUM,
4757 	    priv->stats.vport.arg);
4758 
4759 	mlx5e_create_stats(&priv->stats.pport.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
4760 	    "pstats", mlx5e_pport_stats_desc, MLX5E_PPORT_STATS_NUM,
4761 	    priv->stats.pport.arg);
4762 
4763 	mlx5e_create_ethtool(priv);
4764 
4765 	mtx_lock(&priv->async_events_mtx);
4766 	mlx5e_update_stats(priv);
4767 	mtx_unlock(&priv->async_events_mtx);
4768 
4769 	SYSCTL_ADD_INT(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
4770 	    OID_AUTO, "rx_clbr_done", CTLFLAG_RD,
4771 	    &priv->clbr_done, 0,
4772 	    "RX timestamps calibration state");
4773 	callout_init(&priv->tstmp_clbr, 1);
4774 	/* Pull out the frequency of the clock in hz */
4775 	priv->cclk = (uint64_t)MLX5_CAP_GEN(mdev, device_frequency_khz) * 1000ULL;
4776 	mlx5e_reset_calibration_callout(priv);
4777 
4778 	pa.pa_version = PFIL_VERSION;
4779 	pa.pa_flags = PFIL_IN;
4780 	pa.pa_type = PFIL_TYPE_ETHERNET;
4781 	pa.pa_headname = if_name(ifp);
4782 	priv->pfil = pfil_head_register(&pa);
4783 
4784 	PRIV_LOCK(priv);
4785 	err = mlx5e_open_flow_rules(priv);
4786 	if (err) {
4787 		mlx5_en_err(ifp,
4788 		    "mlx5e_open_flow_rules() failed, %d (ignored)\n", err);
4789 	}
4790 	PRIV_UNLOCK(priv);
4791 
4792 	return (priv);
4793 
4794 err_open_flow_tables:
4795 	mlx5e_close_flow_tables(priv);
4796 
4797 err_open_tirs:
4798 	mlx5e_close_tirs(priv);
4799 
4800 err_open_rqts:
4801 	mlx5e_close_rqts(priv);
4802 
4803 err_open_drop_rq:
4804 	mlx5e_close_drop_rq(&priv->drop_rq);
4805 
4806 err_tls_init:
4807 	mlx5e_tls_cleanup(priv);
4808 
4809 err_rl_init:
4810 	mlx5e_rl_cleanup(priv);
4811 
4812 err_create_mkey:
4813 	mlx5_core_destroy_mkey(priv->mdev, &priv->mr);
4814 
4815 err_dealloc_transport_domain:
4816 	mlx5_dealloc_transport_domain(mdev, priv->tdn, 0);
4817 
4818 err_dealloc_pd:
4819 	mlx5_core_dealloc_pd(mdev, priv->pdn, 0);
4820 
4821 err_free_wq:
4822 	flush_workqueue(priv->wq);
4823 
4824 err_free_sysctl:
4825 	sysctl_ctx_free(&priv->sysctl_ctx);
4826 	if (priv->sysctl_debug)
4827 		sysctl_ctx_free(&priv->stats.port_stats_debug.ctx);
4828 	mlx5e_priv_static_destroy(priv, mdev, mdev->priv.eq_table.num_comp_vectors);
4829 
4830 err_free_ifp:
4831 	if_free(ifp);
4832 
4833 err_free_priv:
4834 	free(priv, M_MLX5EN);
4835 	return (NULL);
4836 }
4837 
4838 static void
mlx5e_destroy_ifp(struct mlx5_core_dev * mdev,void * vpriv)4839 mlx5e_destroy_ifp(struct mlx5_core_dev *mdev, void *vpriv)
4840 {
4841 	struct mlx5e_priv *priv = vpriv;
4842 	if_t ifp = priv->ifp;
4843 
4844 	/* don't allow more IOCTLs */
4845 	priv->gone = 1;
4846 
4847 	/* XXX wait a bit to allow IOCTL handlers to complete */
4848 	pause("W", hz);
4849 
4850 #ifdef RATELIMIT
4851 	/*
4852 	 * The kernel can have reference(s) via the m_snd_tag's into
4853 	 * the ratelimit channels, and these must go away before
4854 	 * detaching:
4855 	 */
4856 	while (READ_ONCE(priv->rl.stats.tx_active_connections) != 0) {
4857 		mlx5_en_err(priv->ifp,
4858 		    "Waiting for all ratelimit connections to terminate\n");
4859 		pause("W", hz);
4860 	}
4861 #endif
4862 
4863 #ifdef KERN_TLS
4864 	/* wait for all TLS tags to get freed */
4865 	while (priv->tls.init != 0 &&
4866 	    uma_zone_get_cur(priv->tls.zone) != 0)  {
4867 		mlx5_en_err(priv->ifp,
4868 		    "Waiting for all TLS connections to terminate\n");
4869 		pause("W", hz);
4870 	}
4871 
4872 	/* wait for all TLS RX tags to get freed */
4873 	while (priv->tls_rx.init != 0 &&
4874 	    uma_zone_get_cur(priv->tls_rx.zone) != 0)  {
4875 		mlx5_en_err(priv->ifp,
4876 		    "Waiting for all TLS RX connections to terminate\n");
4877 		pause("W", hz);
4878 	}
4879 #endif
4880 	/* wait for all unlimited send tags to complete */
4881 	mlx5e_priv_wait_for_completion(priv, mdev->priv.eq_table.num_comp_vectors);
4882 
4883 	/* stop watchdog timer */
4884 	callout_drain(&priv->watchdog);
4885 
4886 	callout_drain(&priv->tstmp_clbr);
4887 
4888 	if (priv->vlan_attach != NULL)
4889 		EVENTHANDLER_DEREGISTER(vlan_config, priv->vlan_attach);
4890 	if (priv->vlan_detach != NULL)
4891 		EVENTHANDLER_DEREGISTER(vlan_unconfig, priv->vlan_detach);
4892 	if (priv->vxlan_start != NULL)
4893 		EVENTHANDLER_DEREGISTER(vxlan_start, priv->vxlan_start);
4894 	if (priv->vxlan_stop != NULL)
4895 		EVENTHANDLER_DEREGISTER(vxlan_stop, priv->vxlan_stop);
4896 
4897 	/* make sure device gets closed */
4898 	PRIV_LOCK(priv);
4899 	mlx5e_close_locked(ifp);
4900 	mlx5e_close_flow_rules(priv);
4901 	PRIV_UNLOCK(priv);
4902 
4903 	/* deregister pfil */
4904 	if (priv->pfil != NULL) {
4905 		pfil_head_unregister(priv->pfil);
4906 		priv->pfil = NULL;
4907 	}
4908 
4909 	/* unregister device */
4910 	ifmedia_removeall(&priv->media);
4911 	ether_ifdetach(ifp);
4912 
4913 	mlx5e_tls_rx_cleanup(priv);
4914 	mlx5e_close_flow_tables(priv);
4915 	mlx5e_close_tirs(priv);
4916 	mlx5e_close_rqts(priv);
4917 	mlx5e_close_drop_rq(&priv->drop_rq);
4918 	mlx5e_tls_cleanup(priv);
4919 	mlx5e_rl_cleanup(priv);
4920 
4921 	/* destroy all remaining sysctl nodes */
4922 	sysctl_ctx_free(&priv->stats.vport.ctx);
4923 	sysctl_ctx_free(&priv->stats.pport.ctx);
4924 	if (priv->sysctl_debug)
4925 		sysctl_ctx_free(&priv->stats.port_stats_debug.ctx);
4926 	sysctl_ctx_free(&priv->sysctl_ctx);
4927 
4928 	mlx5_core_destroy_mkey(priv->mdev, &priv->mr);
4929 	mlx5_dealloc_transport_domain(priv->mdev, priv->tdn, 0);
4930 	mlx5_core_dealloc_pd(priv->mdev, priv->pdn, 0);
4931 	mlx5e_disable_async_events(priv);
4932 	flush_workqueue(priv->wq);
4933 	mlx5e_priv_static_destroy(priv, mdev, mdev->priv.eq_table.num_comp_vectors);
4934 	if_free(ifp);
4935 	free(priv, M_MLX5EN);
4936 }
4937 
4938 #ifdef DEBUGNET
4939 static void
mlx5_en_debugnet_init(if_t dev,int * nrxr,int * ncl,int * clsize)4940 mlx5_en_debugnet_init(if_t dev, int *nrxr, int *ncl, int *clsize)
4941 {
4942 	struct mlx5e_priv *priv = if_getsoftc(dev);
4943 
4944 	PRIV_LOCK(priv);
4945 	*nrxr = priv->params.num_channels;
4946 	*ncl = DEBUGNET_MAX_IN_FLIGHT;
4947 	*clsize = MLX5E_MAX_RX_BYTES;
4948 	PRIV_UNLOCK(priv);
4949 }
4950 
4951 static void
mlx5_en_debugnet_event(if_t dev,enum debugnet_ev event)4952 mlx5_en_debugnet_event(if_t dev, enum debugnet_ev event)
4953 {
4954 }
4955 
4956 static int
mlx5_en_debugnet_transmit(if_t dev,struct mbuf * m)4957 mlx5_en_debugnet_transmit(if_t dev, struct mbuf *m)
4958 {
4959 	struct mlx5e_priv *priv = if_getsoftc(dev);
4960 	struct mlx5e_sq *sq;
4961 	int err;
4962 
4963 	if ((if_getdrvflags(dev) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
4964 	    IFF_DRV_RUNNING || (priv->media_status_last & IFM_ACTIVE) == 0)
4965 		return (ENOENT);
4966 
4967 	sq = &priv->channel[0].sq[0];
4968 
4969 	if (sq->running == 0) {
4970 		m_freem(m);
4971 		return (ENOENT);
4972 	}
4973 
4974 	if (mlx5e_sq_xmit(sq, &m) != 0) {
4975 		m_freem(m);
4976 		err = ENOBUFS;
4977 	} else {
4978 		err = 0;
4979 	}
4980 
4981 	mlx5e_tx_notify_hw(sq, true);
4982 
4983 	return (err);
4984 }
4985 
4986 static int
mlx5_en_debugnet_poll(if_t dev,int count)4987 mlx5_en_debugnet_poll(if_t dev, int count)
4988 {
4989 	struct mlx5e_priv *priv = if_getsoftc(dev);
4990 
4991 	if ((if_getdrvflags(dev) & IFF_DRV_RUNNING) == 0 ||
4992 	    (priv->media_status_last & IFM_ACTIVE) == 0)
4993 		return (ENOENT);
4994 
4995 	mlx5_poll_interrupts(priv->mdev);
4996 
4997 	return (0);
4998 }
4999 #endif /* DEBUGNET */
5000 
5001 static void *
mlx5e_get_ifp(void * vpriv)5002 mlx5e_get_ifp(void *vpriv)
5003 {
5004 	struct mlx5e_priv *priv = vpriv;
5005 
5006 	return (priv->ifp);
5007 }
5008 
5009 static struct mlx5_interface mlx5e_interface = {
5010 	.add = mlx5e_create_ifp,
5011 	.remove = mlx5e_destroy_ifp,
5012 	.event = mlx5e_async_event,
5013 	.protocol = MLX5_INTERFACE_PROTOCOL_ETH,
5014 	.get_dev = mlx5e_get_ifp,
5015 };
5016 
5017 void
mlx5e_init(void)5018 mlx5e_init(void)
5019 {
5020 	mlx5_register_interface(&mlx5e_interface);
5021 }
5022 
5023 void
mlx5e_cleanup(void)5024 mlx5e_cleanup(void)
5025 {
5026 	mlx5_unregister_interface(&mlx5e_interface);
5027 }
5028 
5029 module_init_order(mlx5e_init, SI_ORDER_SIXTH);
5030 module_exit_order(mlx5e_cleanup, SI_ORDER_SIXTH);
5031 
5032 MODULE_DEPEND(mlx5en, linuxkpi, 1, 1, 1);
5033 MODULE_DEPEND(mlx5en, mlx5, 1, 1, 1);
5034 MODULE_VERSION(mlx5en, 1);
5035