xref: /freebsd/sys/dev/mlx5/mlx5_en/mlx5_en_ethtool.c (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
1 /*-
2  * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 #include "en.h"
29 #include <net/sff8472.h>
30 
31 void
32 mlx5e_create_stats(struct sysctl_ctx_list *ctx,
33     struct sysctl_oid_list *parent, const char *buffer,
34     const char **desc, unsigned num, u64 * arg)
35 {
36 	struct sysctl_oid *node;
37 	unsigned x;
38 
39 	sysctl_ctx_init(ctx);
40 
41 	node = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO,
42 	    buffer, CTLFLAG_RD, NULL, "Statistics");
43 	if (node == NULL)
44 		return;
45 	for (x = 0; x != num; x++) {
46 		SYSCTL_ADD_UQUAD(ctx, SYSCTL_CHILDREN(node), OID_AUTO,
47 		    desc[2 * x], CTLFLAG_RD, arg + x, desc[2 * x + 1]);
48 	}
49 }
50 
51 static void
52 mlx5e_ethtool_sync_tx_completion_fact(struct mlx5e_priv *priv)
53 {
54 	/*
55 	 * Limit the maximum distance between completion events to
56 	 * half of the currently set TX queue size.
57 	 *
58 	 * The maximum number of queue entries a single IP packet can
59 	 * consume is given by MLX5_SEND_WQE_MAX_WQEBBS.
60 	 *
61 	 * The worst case max value is then given as below:
62 	 */
63 	uint64_t max = priv->params_ethtool.tx_queue_size /
64 	    (2 * MLX5_SEND_WQE_MAX_WQEBBS);
65 
66 	/*
67 	 * Update the maximum completion factor value in case the
68 	 * tx_queue_size field changed. Ensure we don't overflow
69 	 * 16-bits.
70 	 */
71 	if (max < 1)
72 		max = 1;
73 	else if (max > 65535)
74 		max = 65535;
75 	priv->params_ethtool.tx_completion_fact_max = max;
76 
77 	/*
78 	 * Verify that the current TX completion factor is within the
79 	 * given limits:
80 	 */
81 	if (priv->params_ethtool.tx_completion_fact < 1)
82 		priv->params_ethtool.tx_completion_fact = 1;
83 	else if (priv->params_ethtool.tx_completion_fact > max)
84 		priv->params_ethtool.tx_completion_fact = max;
85 }
86 
87 #define	MLX5_PARAM_OFFSET(n)				\
88     __offsetof(struct mlx5e_priv, params_ethtool.n)
89 
90 static int
91 mlx5e_ethtool_handler(SYSCTL_HANDLER_ARGS)
92 {
93 	struct mlx5e_priv *priv = arg1;
94 	uint64_t value;
95 	int was_opened;
96 	int error;
97 
98 	PRIV_LOCK(priv);
99 	value = priv->params_ethtool.arg[arg2];
100 	if (req != NULL) {
101 		error = sysctl_handle_64(oidp, &value, 0, req);
102 		if (error || req->newptr == NULL ||
103 		    value == priv->params_ethtool.arg[arg2])
104 			goto done;
105 
106 		/* assign new value */
107 		priv->params_ethtool.arg[arg2] = value;
108 	} else {
109 		error = 0;
110 	}
111 	/* check if device is gone */
112 	if (priv->gone) {
113 		error = ENXIO;
114 		goto done;
115 	}
116 	was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
117 
118 	switch (MLX5_PARAM_OFFSET(arg[arg2])) {
119 	case MLX5_PARAM_OFFSET(rx_coalesce_usecs):
120 		/* import RX coal time */
121 		if (priv->params_ethtool.rx_coalesce_usecs < 1)
122 			priv->params_ethtool.rx_coalesce_usecs = 0;
123 		else if (priv->params_ethtool.rx_coalesce_usecs >
124 		    MLX5E_FLD_MAX(cqc, cq_period)) {
125 			priv->params_ethtool.rx_coalesce_usecs =
126 			    MLX5E_FLD_MAX(cqc, cq_period);
127 		}
128 		priv->params.rx_cq_moderation_usec =
129 		    priv->params_ethtool.rx_coalesce_usecs;
130 
131 		/* check to avoid down and up the network interface */
132 		if (was_opened)
133 			error = mlx5e_refresh_channel_params(priv);
134 		break;
135 
136 	case MLX5_PARAM_OFFSET(rx_coalesce_pkts):
137 		/* import RX coal pkts */
138 		if (priv->params_ethtool.rx_coalesce_pkts < 1)
139 			priv->params_ethtool.rx_coalesce_pkts = 0;
140 		else if (priv->params_ethtool.rx_coalesce_pkts >
141 		    MLX5E_FLD_MAX(cqc, cq_max_count)) {
142 			priv->params_ethtool.rx_coalesce_pkts =
143 			    MLX5E_FLD_MAX(cqc, cq_max_count);
144 		}
145 		priv->params.rx_cq_moderation_pkts =
146 		    priv->params_ethtool.rx_coalesce_pkts;
147 
148 		/* check to avoid down and up the network interface */
149 		if (was_opened)
150 			error = mlx5e_refresh_channel_params(priv);
151 		break;
152 
153 	case MLX5_PARAM_OFFSET(tx_coalesce_usecs):
154 		/* import TX coal time */
155 		if (priv->params_ethtool.tx_coalesce_usecs < 1)
156 			priv->params_ethtool.tx_coalesce_usecs = 0;
157 		else if (priv->params_ethtool.tx_coalesce_usecs >
158 		    MLX5E_FLD_MAX(cqc, cq_period)) {
159 			priv->params_ethtool.tx_coalesce_usecs =
160 			    MLX5E_FLD_MAX(cqc, cq_period);
161 		}
162 		priv->params.tx_cq_moderation_usec =
163 		    priv->params_ethtool.tx_coalesce_usecs;
164 
165 		/* check to avoid down and up the network interface */
166 		if (was_opened)
167 			error = mlx5e_refresh_channel_params(priv);
168 		break;
169 
170 	case MLX5_PARAM_OFFSET(tx_coalesce_pkts):
171 		/* import TX coal pkts */
172 		if (priv->params_ethtool.tx_coalesce_pkts < 1)
173 			priv->params_ethtool.tx_coalesce_pkts = 0;
174 		else if (priv->params_ethtool.tx_coalesce_pkts >
175 		    MLX5E_FLD_MAX(cqc, cq_max_count)) {
176 			priv->params_ethtool.tx_coalesce_pkts =
177 			    MLX5E_FLD_MAX(cqc, cq_max_count);
178 		}
179 		priv->params.tx_cq_moderation_pkts =
180 		    priv->params_ethtool.tx_coalesce_pkts;
181 
182 		/* check to avoid down and up the network interface */
183 		if (was_opened)
184 			error = mlx5e_refresh_channel_params(priv);
185 		break;
186 
187 	case MLX5_PARAM_OFFSET(tx_queue_size):
188 		/* network interface must be down */
189 		if (was_opened)
190 			mlx5e_close_locked(priv->ifp);
191 
192 		/* import TX queue size */
193 		if (priv->params_ethtool.tx_queue_size <
194 		    (1 << MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE)) {
195 			priv->params_ethtool.tx_queue_size =
196 			    (1 << MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE);
197 		} else if (priv->params_ethtool.tx_queue_size >
198 		    priv->params_ethtool.tx_queue_size_max) {
199 			priv->params_ethtool.tx_queue_size =
200 			    priv->params_ethtool.tx_queue_size_max;
201 		}
202 		/* store actual TX queue size */
203 		priv->params.log_sq_size =
204 		    order_base_2(priv->params_ethtool.tx_queue_size);
205 		priv->params_ethtool.tx_queue_size =
206 		    1 << priv->params.log_sq_size;
207 
208 		/* verify TX completion factor */
209 		mlx5e_ethtool_sync_tx_completion_fact(priv);
210 
211 		/* restart network interface, if any */
212 		if (was_opened)
213 			mlx5e_open_locked(priv->ifp);
214 		break;
215 
216 	case MLX5_PARAM_OFFSET(rx_queue_size):
217 		/* network interface must be down */
218 		if (was_opened)
219 			mlx5e_close_locked(priv->ifp);
220 
221 		/* import RX queue size */
222 		if (priv->params_ethtool.rx_queue_size <
223 		    (1 << MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE)) {
224 			priv->params_ethtool.rx_queue_size =
225 			    (1 << MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE);
226 		} else if (priv->params_ethtool.rx_queue_size >
227 		    priv->params_ethtool.rx_queue_size_max) {
228 			priv->params_ethtool.rx_queue_size =
229 			    priv->params_ethtool.rx_queue_size_max;
230 		}
231 		/* store actual RX queue size */
232 		priv->params.log_rq_size =
233 		    order_base_2(priv->params_ethtool.rx_queue_size);
234 		priv->params_ethtool.rx_queue_size =
235 		    1 << priv->params.log_rq_size;
236 
237 		/* update least number of RX WQEs */
238 		priv->params.min_rx_wqes = min(
239 		    priv->params_ethtool.rx_queue_size - 1,
240 		    MLX5E_PARAMS_DEFAULT_MIN_RX_WQES);
241 
242 		/* restart network interface, if any */
243 		if (was_opened)
244 			mlx5e_open_locked(priv->ifp);
245 		break;
246 
247 	case MLX5_PARAM_OFFSET(channels):
248 		/* network interface must be down */
249 		if (was_opened)
250 			mlx5e_close_locked(priv->ifp);
251 
252 		/* import number of channels */
253 		if (priv->params_ethtool.channels < 1)
254 			priv->params_ethtool.channels = 1;
255 		else if (priv->params_ethtool.channels >
256 		    (u64) priv->mdev->priv.eq_table.num_comp_vectors) {
257 			priv->params_ethtool.channels =
258 			    (u64) priv->mdev->priv.eq_table.num_comp_vectors;
259 		}
260 		priv->params.num_channels = priv->params_ethtool.channels;
261 
262 		/* restart network interface, if any */
263 		if (was_opened)
264 			mlx5e_open_locked(priv->ifp);
265 		break;
266 
267 	case MLX5_PARAM_OFFSET(rx_coalesce_mode):
268 		/* network interface must be down */
269 		if (was_opened)
270 			mlx5e_close_locked(priv->ifp);
271 
272 		/* import RX coalesce mode */
273 		if (priv->params_ethtool.rx_coalesce_mode != 0)
274 			priv->params_ethtool.rx_coalesce_mode = 1;
275 		priv->params.rx_cq_moderation_mode =
276 		    priv->params_ethtool.rx_coalesce_mode;
277 
278 		/* restart network interface, if any */
279 		if (was_opened)
280 			mlx5e_open_locked(priv->ifp);
281 		break;
282 
283 	case MLX5_PARAM_OFFSET(tx_coalesce_mode):
284 		/* network interface must be down */
285 		if (was_opened)
286 			mlx5e_close_locked(priv->ifp);
287 
288 		/* import TX coalesce mode */
289 		if (priv->params_ethtool.tx_coalesce_mode != 0)
290 			priv->params_ethtool.tx_coalesce_mode = 1;
291 		priv->params.tx_cq_moderation_mode =
292 		    priv->params_ethtool.tx_coalesce_mode;
293 
294 		/* restart network interface, if any */
295 		if (was_opened)
296 			mlx5e_open_locked(priv->ifp);
297 		break;
298 
299 	case MLX5_PARAM_OFFSET(hw_lro):
300 		/* network interface must be down */
301 		if (was_opened)
302 			mlx5e_close_locked(priv->ifp);
303 
304 		/* import HW LRO mode */
305 		if (priv->params_ethtool.hw_lro != 0) {
306 			if ((priv->ifp->if_capenable & IFCAP_LRO) &&
307 			    MLX5_CAP_ETH(priv->mdev, lro_cap)) {
308 				priv->params.hw_lro_en = 1;
309 				priv->params_ethtool.hw_lro = 1;
310 			} else {
311 				priv->params.hw_lro_en = 0;
312 				priv->params_ethtool.hw_lro = 0;
313 				error = EINVAL;
314 
315 				if_printf(priv->ifp, "Can't enable HW LRO: "
316 				    "The HW or SW LRO feature is disabled\n");
317 			}
318 		} else {
319 			priv->params.hw_lro_en = 0;
320 		}
321 		/* restart network interface, if any */
322 		if (was_opened)
323 			mlx5e_open_locked(priv->ifp);
324 		break;
325 
326 	case MLX5_PARAM_OFFSET(cqe_zipping):
327 		/* network interface must be down */
328 		if (was_opened)
329 			mlx5e_close_locked(priv->ifp);
330 
331 		/* import CQE zipping mode */
332 		if (priv->params_ethtool.cqe_zipping &&
333 		    MLX5_CAP_GEN(priv->mdev, cqe_compression)) {
334 			priv->params.cqe_zipping_en = true;
335 			priv->params_ethtool.cqe_zipping = 1;
336 		} else {
337 			priv->params.cqe_zipping_en = false;
338 			priv->params_ethtool.cqe_zipping = 0;
339 		}
340 		/* restart network interface, if any */
341 		if (was_opened)
342 			mlx5e_open_locked(priv->ifp);
343 		break;
344 
345 	case MLX5_PARAM_OFFSET(tx_completion_fact):
346 		/* network interface must be down */
347 		if (was_opened)
348 			mlx5e_close_locked(priv->ifp);
349 
350 		/* verify parameter */
351 		mlx5e_ethtool_sync_tx_completion_fact(priv);
352 
353 		/* restart network interface, if any */
354 		if (was_opened)
355 			mlx5e_open_locked(priv->ifp);
356 		break;
357 
358 	default:
359 		break;
360 	}
361 done:
362 	PRIV_UNLOCK(priv);
363 	return (error);
364 }
365 
366 /*
367  * Read the first three bytes of the eeprom in order to get the needed info
368  * for the whole reading.
369  * Byte 0 - Identifier byte
370  * Byte 1 - Revision byte
371  * Byte 2 - Status byte
372  */
373 static int
374 mlx5e_get_eeprom_info(struct mlx5e_priv *priv, struct mlx5e_eeprom *eeprom)
375 {
376 	struct mlx5_core_dev *dev = priv->mdev;
377 	u32 data = 0;
378 	int size_read = 0;
379 	int ret;
380 
381 	ret = mlx5_query_module_num(dev, &eeprom->module_num);
382 	if (ret) {
383 		if_printf(priv->ifp, "%s:%d: Failed query module error=%d\n",
384 		    __func__, __LINE__, ret);
385 		return (ret);
386 	}
387 
388 	/* Read the first three bytes to get Identifier, Revision and Status */
389 	ret = mlx5_query_eeprom(dev, eeprom->i2c_addr, eeprom->page_num,
390 	    eeprom->device_addr, MLX5E_EEPROM_INFO_BYTES, eeprom->module_num, &data,
391 	    &size_read);
392 	if (ret) {
393 		if_printf(priv->ifp, "%s:%d: Failed query eeprom module error=0x%x\n",
394 		    __func__, __LINE__, ret);
395 		return (ret);
396 	}
397 
398 	switch (data & MLX5_EEPROM_IDENTIFIER_BYTE_MASK) {
399 	case SFF_8024_ID_QSFP:
400 		eeprom->type = MLX5E_ETH_MODULE_SFF_8436;
401 		eeprom->len = MLX5E_ETH_MODULE_SFF_8436_LEN;
402 		break;
403 	case SFF_8024_ID_QSFPPLUS:
404 	case SFF_8024_ID_QSFP28:
405 		if ((data & MLX5_EEPROM_IDENTIFIER_BYTE_MASK) == SFF_8024_ID_QSFP28 ||
406 		    ((data & MLX5_EEPROM_REVISION_ID_BYTE_MASK) >> 8) >= 0x3) {
407 			eeprom->type = MLX5E_ETH_MODULE_SFF_8636;
408 			eeprom->len = MLX5E_ETH_MODULE_SFF_8636_LEN;
409 		} else {
410 			eeprom->type = MLX5E_ETH_MODULE_SFF_8436;
411 			eeprom->len = MLX5E_ETH_MODULE_SFF_8436_LEN;
412 		}
413 		if ((data & MLX5_EEPROM_PAGE_3_VALID_BIT_MASK) == 0)
414 			eeprom->page_valid = 1;
415 		break;
416 	case SFF_8024_ID_SFP:
417 		eeprom->type = MLX5E_ETH_MODULE_SFF_8472;
418 		eeprom->len = MLX5E_ETH_MODULE_SFF_8472_LEN;
419 		break;
420 	default:
421 		if_printf(priv->ifp, "%s:%d: Not recognized cable type = 0x%x(%s)\n",
422 		    __func__, __LINE__, data & MLX5_EEPROM_IDENTIFIER_BYTE_MASK,
423 		    sff_8024_id[data & MLX5_EEPROM_IDENTIFIER_BYTE_MASK]);
424 		return (EINVAL);
425 	}
426 	return (0);
427 }
428 
429 /* Read both low and high pages of the eeprom */
430 static int
431 mlx5e_get_eeprom(struct mlx5e_priv *priv, struct mlx5e_eeprom *ee)
432 {
433 	struct mlx5_core_dev *dev = priv->mdev;
434 	int size_read = 0;
435 	int ret;
436 
437 	if (ee->len == 0)
438 		return (EINVAL);
439 
440 	/* Read low page of the eeprom */
441 	while (ee->device_addr < ee->len) {
442 		ret = mlx5_query_eeprom(dev, ee->i2c_addr, ee->page_num, ee->device_addr,
443 		    ee->len - ee->device_addr, ee->module_num,
444 		    ee->data + (ee->device_addr / 4), &size_read);
445 		if (ret) {
446 			if_printf(priv->ifp, "%s:%d: Failed reading eeprom, "
447 			    "error = 0x%02x\n", __func__, __LINE__, ret);
448 			return (ret);
449 		}
450 		ee->device_addr += size_read;
451 	}
452 
453 	/* Read high page of the eeprom */
454 	if (ee->page_valid) {
455 		ee->device_addr = MLX5E_EEPROM_HIGH_PAGE_OFFSET;
456 		ee->page_num = MLX5E_EEPROM_HIGH_PAGE;
457 		size_read = 0;
458 		while (ee->device_addr < MLX5E_EEPROM_PAGE_LENGTH) {
459 			ret = mlx5_query_eeprom(dev, ee->i2c_addr, ee->page_num,
460 			    ee->device_addr, MLX5E_EEPROM_PAGE_LENGTH - ee->device_addr,
461 			    ee->module_num, ee->data + (ee->len / 4) +
462 			    ((ee->device_addr - MLX5E_EEPROM_HIGH_PAGE_OFFSET) / 4),
463 			    &size_read);
464 			if (ret) {
465 				if_printf(priv->ifp, "%s:%d: Failed reading eeprom, "
466 				    "error = 0x%02x\n", __func__, __LINE__, ret);
467 				return (ret);
468 			}
469 			ee->device_addr += size_read;
470 		}
471 	}
472 	return (0);
473 }
474 
475 static void
476 mlx5e_print_eeprom(struct mlx5e_eeprom *eeprom)
477 {
478 	int row;
479 	int index_in_row;
480 	int byte_to_write = 0;
481 	int line_length = 16;
482 
483 	printf("\nOffset\t\tValues\n");
484 	printf("------\t\t------");
485 	while (byte_to_write < eeprom->len) {
486 		printf("\n0x%04X\t\t", byte_to_write);
487 		for (index_in_row = 0; index_in_row < line_length; index_in_row++) {
488 			printf("%02X ", ((u8 *)eeprom->data)[byte_to_write]);
489 			byte_to_write++;
490 		}
491 	}
492 
493 	if (eeprom->page_valid) {
494 		row = MLX5E_EEPROM_HIGH_PAGE_OFFSET;
495 		printf("\n\nUpper Page 0x03\n");
496 		printf("\nOffset\t\tValues\n");
497 		printf("------\t\t------");
498 		while (row < MLX5E_EEPROM_PAGE_LENGTH) {
499 			printf("\n0x%04X\t\t", row);
500 			for (index_in_row = 0; index_in_row < line_length; index_in_row++) {
501 				printf("%02X ", ((u8 *)eeprom->data)[byte_to_write]);
502 				byte_to_write++;
503 				row++;
504 			}
505 		}
506 	}
507 }
508 
509 /*
510  * Read cable EEPROM module information by first inspecting the first
511  * three bytes to get the initial information for a whole reading.
512  * Information will be printed to dmesg.
513  */
514 static int
515 mlx5e_read_eeprom(SYSCTL_HANDLER_ARGS)
516 {
517 	struct mlx5e_priv *priv = arg1;
518 	struct mlx5e_eeprom eeprom;
519 	int error;
520 	int result = 0;
521 
522 	PRIV_LOCK(priv);
523 	error = sysctl_handle_int(oidp, &result, 0, req);
524 	if (error || !req->newptr)
525 		goto done;
526 
527 	/* Check if device is gone */
528 	if (priv->gone) {
529 		error = ENXIO;
530 		goto done;
531 	}
532 
533 	if (result == 1) {
534 		eeprom.i2c_addr = MLX5E_I2C_ADDR_LOW;
535 		eeprom.device_addr = 0;
536 		eeprom.page_num = MLX5E_EEPROM_LOW_PAGE;
537 		eeprom.page_valid = 0;
538 
539 		/* Read three first bytes to get important info */
540 		error = mlx5e_get_eeprom_info(priv, &eeprom);
541 		if (error) {
542 			if_printf(priv->ifp, "%s:%d: Failed reading eeprom's "
543 			    "initial information\n", __func__, __LINE__);
544 			error = 0;
545 			goto done;
546 		}
547 		/*
548 		 * Allocate needed length buffer and additional space for
549 		 * page 0x03
550 		 */
551 		eeprom.data = malloc(eeprom.len + MLX5E_EEPROM_PAGE_LENGTH,
552 		    M_MLX5EN, M_WAITOK | M_ZERO);
553 
554 		/* Read the whole eeprom information */
555 		error = mlx5e_get_eeprom(priv, &eeprom);
556 		if (error) {
557 			if_printf(priv->ifp, "%s:%d: Failed reading eeprom\n",
558 			    __func__, __LINE__);
559 			error = 0;
560 			/*
561 			 * Continue printing partial information in case of
562 			 * an error
563 			 */
564 		}
565 		mlx5e_print_eeprom(&eeprom);
566 		free(eeprom.data, M_MLX5EN);
567 	}
568 done:
569 	PRIV_UNLOCK(priv);
570 	return (error);
571 }
572 
573 static const char *mlx5e_params_desc[] = {
574 	MLX5E_PARAMS(MLX5E_STATS_DESC)
575 };
576 
577 static const char *mlx5e_port_stats_debug_desc[] = {
578 	MLX5E_PORT_STATS_DEBUG(MLX5E_STATS_DESC)
579 };
580 
581 static int
582 mlx5e_ethtool_debug_stats(SYSCTL_HANDLER_ARGS)
583 {
584 	struct mlx5e_priv *priv = arg1;
585 	int error;
586 	int sys_debug;
587 
588 	sys_debug = priv->sysctl_debug;
589 	error = sysctl_handle_int(oidp, &priv->sysctl_debug, 0, req);
590 	if (error || !req->newptr)
591 		return (error);
592 	priv->sysctl_debug = !!priv->sysctl_debug;
593 	if (sys_debug == priv->sysctl_debug)
594 		return (error);
595 	if (priv->sysctl_debug)
596 		mlx5e_create_stats(&priv->stats.port_stats_debug.ctx,
597 		    SYSCTL_CHILDREN(priv->sysctl_ifnet), "debug_stats",
598 		    mlx5e_port_stats_debug_desc, MLX5E_PORT_STATS_DEBUG_NUM,
599 		    priv->stats.port_stats_debug.arg);
600 	else
601 		sysctl_ctx_free(&priv->stats.port_stats_debug.ctx);
602 	return (error);
603 }
604 
605 void
606 mlx5e_create_ethtool(struct mlx5e_priv *priv)
607 {
608 	struct sysctl_oid *node;
609 	const char *pnameunit;
610 	unsigned x;
611 
612 	/* set some defaults */
613 	priv->params_ethtool.tx_queue_size_max = 1 << MLX5E_PARAMS_MAXIMUM_LOG_SQ_SIZE;
614 	priv->params_ethtool.rx_queue_size_max = 1 << MLX5E_PARAMS_MAXIMUM_LOG_RQ_SIZE;
615 	priv->params_ethtool.tx_queue_size = 1 << priv->params.log_sq_size;
616 	priv->params_ethtool.rx_queue_size = 1 << priv->params.log_rq_size;
617 	priv->params_ethtool.channels = priv->params.num_channels;
618 	priv->params_ethtool.coalesce_pkts_max = MLX5E_FLD_MAX(cqc, cq_max_count);
619 	priv->params_ethtool.coalesce_usecs_max = MLX5E_FLD_MAX(cqc, cq_period);
620 	priv->params_ethtool.rx_coalesce_mode = priv->params.rx_cq_moderation_mode;
621 	priv->params_ethtool.rx_coalesce_usecs = priv->params.rx_cq_moderation_usec;
622 	priv->params_ethtool.rx_coalesce_pkts = priv->params.rx_cq_moderation_pkts;
623 	priv->params_ethtool.tx_coalesce_mode = priv->params.tx_cq_moderation_mode;
624 	priv->params_ethtool.tx_coalesce_usecs = priv->params.tx_cq_moderation_usec;
625 	priv->params_ethtool.tx_coalesce_pkts = priv->params.tx_cq_moderation_pkts;
626 	priv->params_ethtool.hw_lro = priv->params.hw_lro_en;
627 	priv->params_ethtool.cqe_zipping = priv->params.cqe_zipping_en;
628 	mlx5e_ethtool_sync_tx_completion_fact(priv);
629 
630 	/* create root node */
631 	node = SYSCTL_ADD_NODE(&priv->sysctl_ctx,
632 	    SYSCTL_CHILDREN(priv->sysctl_ifnet), OID_AUTO,
633 	    "conf", CTLFLAG_RW, NULL, "Configuration");
634 	if (node == NULL)
635 		return;
636 	for (x = 0; x != MLX5E_PARAMS_NUM; x++) {
637 		/* check for read-only parameter */
638 		if (strstr(mlx5e_params_desc[2 * x], "_max") != NULL) {
639 			SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO,
640 			    mlx5e_params_desc[2 * x], CTLTYPE_U64 | CTLFLAG_RD |
641 			    CTLFLAG_MPSAFE, priv, x, &mlx5e_ethtool_handler, "QU",
642 			    mlx5e_params_desc[2 * x + 1]);
643 		} else {
644 #if (__FreeBSD_version < 1100000)
645 			char path[64];
646 #endif
647 			/*
648 			 * NOTE: In FreeBSD-11 and newer the
649 			 * CTLFLAG_RWTUN flag will take care of
650 			 * loading default sysctl value from the
651 			 * kernel environment, if any:
652 			 */
653 			SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO,
654 			    mlx5e_params_desc[2 * x], CTLTYPE_U64 | CTLFLAG_RWTUN |
655 			    CTLFLAG_MPSAFE, priv, x, &mlx5e_ethtool_handler, "QU",
656 			    mlx5e_params_desc[2 * x + 1]);
657 
658 #if (__FreeBSD_version < 1100000)
659 			/* compute path for sysctl */
660 			snprintf(path, sizeof(path), "dev.mce.%d.conf.%s",
661 			    device_get_unit(priv->mdev->pdev->dev.bsddev),
662 			    mlx5e_params_desc[2 * x]);
663 
664 			/* try to fetch tunable, if any */
665 			if (TUNABLE_QUAD_FETCH(path, &priv->params_ethtool.arg[x]))
666 				mlx5e_ethtool_handler(NULL, priv, x, NULL);
667 #endif
668 		}
669 	}
670 
671 	SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO,
672 	    "debug_stats", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, priv,
673 	    0, &mlx5e_ethtool_debug_stats, "I", "Extended debug statistics");
674 
675 	pnameunit = device_get_nameunit(priv->mdev->pdev->dev.bsddev);
676 
677 	SYSCTL_ADD_STRING(&priv->sysctl_ctx, SYSCTL_CHILDREN(node),
678 	    OID_AUTO, "device_name", CTLFLAG_RD,
679 	    __DECONST(void *, pnameunit), 0,
680 	    "PCI device name");
681 
682 	/* EEPROM support */
683 	SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO, "eeprom_info",
684 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, priv, 0,
685 	    mlx5e_read_eeprom, "I", "EEPROM information");
686 }
687