mlx5_en_ethtool.c revision 321995
1290650Shselasky/*-
2290650Shselasky * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
3290650Shselasky *
4290650Shselasky * Redistribution and use in source and binary forms, with or without
5290650Shselasky * modification, are permitted provided that the following conditions
6290650Shselasky * are met:
7290650Shselasky * 1. Redistributions of source code must retain the above copyright
8290650Shselasky *    notice, this list of conditions and the following disclaimer.
9290650Shselasky * 2. Redistributions in binary form must reproduce the above copyright
10290650Shselasky *    notice, this list of conditions and the following disclaimer in the
11290650Shselasky *    documentation and/or other materials provided with the distribution.
12290650Shselasky *
13290650Shselasky * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14290650Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15290650Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16290650Shselasky * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17290650Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18290650Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19290650Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20290650Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21290650Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22290650Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23290650Shselasky * SUCH DAMAGE.
24290650Shselasky *
25290650Shselasky * $FreeBSD: stable/11/sys/dev/mlx5/mlx5_en/mlx5_en_ethtool.c 321995 2017-08-03 13:55:39Z hselasky $
26290650Shselasky */
27290650Shselasky
28290650Shselasky#include "en.h"
29290650Shselasky#include <net/sff8472.h>
30290650Shselasky
31290650Shselaskyvoid
32290650Shselaskymlx5e_create_stats(struct sysctl_ctx_list *ctx,
33290650Shselasky    struct sysctl_oid_list *parent, const char *buffer,
34290650Shselasky    const char **desc, unsigned num, u64 * arg)
35290650Shselasky{
36290650Shselasky	struct sysctl_oid *node;
37290650Shselasky	unsigned x;
38290650Shselasky
39290650Shselasky	sysctl_ctx_init(ctx);
40290650Shselasky
41290650Shselasky	node = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO,
42290650Shselasky	    buffer, CTLFLAG_RD, NULL, "Statistics");
43290650Shselasky	if (node == NULL)
44290650Shselasky		return;
45290650Shselasky	for (x = 0; x != num; x++) {
46290650Shselasky		SYSCTL_ADD_UQUAD(ctx, SYSCTL_CHILDREN(node), OID_AUTO,
47290650Shselasky		    desc[2 * x], CTLFLAG_RD, arg + x, desc[2 * x + 1]);
48290650Shselasky	}
49290650Shselasky}
50290650Shselasky
51300277Shselaskystatic void
52300277Shselaskymlx5e_ethtool_sync_tx_completion_fact(struct mlx5e_priv *priv)
53300277Shselasky{
54300277Shselasky	/*
55300277Shselasky	 * Limit the maximum distance between completion events to
56300277Shselasky	 * half of the currently set TX queue size.
57300277Shselasky	 *
58300277Shselasky	 * The maximum number of queue entries a single IP packet can
59300277Shselasky	 * consume is given by MLX5_SEND_WQE_MAX_WQEBBS.
60300277Shselasky	 *
61300277Shselasky	 * The worst case max value is then given as below:
62300277Shselasky	 */
63300277Shselasky	uint64_t max = priv->params_ethtool.tx_queue_size /
64300277Shselasky	    (2 * MLX5_SEND_WQE_MAX_WQEBBS);
65300277Shselasky
66300277Shselasky	/*
67300277Shselasky	 * Update the maximum completion factor value in case the
68300277Shselasky	 * tx_queue_size field changed. Ensure we don't overflow
69300277Shselasky	 * 16-bits.
70300277Shselasky	 */
71300277Shselasky	if (max < 1)
72300277Shselasky		max = 1;
73300277Shselasky	else if (max > 65535)
74300277Shselasky		max = 65535;
75300277Shselasky	priv->params_ethtool.tx_completion_fact_max = max;
76300277Shselasky
77300277Shselasky	/*
78300277Shselasky	 * Verify that the current TX completion factor is within the
79300277Shselasky	 * given limits:
80300277Shselasky	 */
81300277Shselasky	if (priv->params_ethtool.tx_completion_fact < 1)
82300277Shselasky		priv->params_ethtool.tx_completion_fact = 1;
83300277Shselasky	else if (priv->params_ethtool.tx_completion_fact > max)
84300277Shselasky		priv->params_ethtool.tx_completion_fact = max;
85300277Shselasky}
86300277Shselasky
87300282Shselasky#define	MLX5_PARAM_OFFSET(n)				\
88300282Shselasky    __offsetof(struct mlx5e_priv, params_ethtool.n)
89300282Shselasky
90290650Shselaskystatic int
91290650Shselaskymlx5e_ethtool_handler(SYSCTL_HANDLER_ARGS)
92290650Shselasky{
93290650Shselasky	struct mlx5e_priv *priv = arg1;
94290650Shselasky	uint64_t value;
95321995Shselasky	int mode_modify;
96290650Shselasky	int was_opened;
97290650Shselasky	int error;
98290650Shselasky
99290650Shselasky	PRIV_LOCK(priv);
100290650Shselasky	value = priv->params_ethtool.arg[arg2];
101292837Shselasky	if (req != NULL) {
102292837Shselasky		error = sysctl_handle_64(oidp, &value, 0, req);
103292837Shselasky		if (error || req->newptr == NULL ||
104292837Shselasky		    value == priv->params_ethtool.arg[arg2])
105292837Shselasky			goto done;
106290650Shselasky
107292837Shselasky		/* assign new value */
108292837Shselasky		priv->params_ethtool.arg[arg2] = value;
109292837Shselasky	} else {
110292837Shselasky		error = 0;
111292837Shselasky	}
112290650Shselasky	/* check if device is gone */
113290650Shselasky	if (priv->gone) {
114290650Shselasky		error = ENXIO;
115290650Shselasky		goto done;
116290650Shselasky	}
117300282Shselasky	was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
118321995Shselasky	mode_modify = MLX5_CAP_GEN(priv->mdev, cq_period_mode_modify);
119290650Shselasky
120300282Shselasky	switch (MLX5_PARAM_OFFSET(arg[arg2])) {
121300282Shselasky	case MLX5_PARAM_OFFSET(rx_coalesce_usecs):
122300282Shselasky		/* import RX coal time */
123300282Shselasky		if (priv->params_ethtool.rx_coalesce_usecs < 1)
124300282Shselasky			priv->params_ethtool.rx_coalesce_usecs = 0;
125300282Shselasky		else if (priv->params_ethtool.rx_coalesce_usecs >
126300282Shselasky		    MLX5E_FLD_MAX(cqc, cq_period)) {
127300282Shselasky			priv->params_ethtool.rx_coalesce_usecs =
128300282Shselasky			    MLX5E_FLD_MAX(cqc, cq_period);
129300282Shselasky		}
130300282Shselasky		priv->params.rx_cq_moderation_usec =
131300282Shselasky		    priv->params_ethtool.rx_coalesce_usecs;
132292949Shselasky
133300282Shselasky		/* check to avoid down and up the network interface */
134300282Shselasky		if (was_opened)
135300282Shselasky			error = mlx5e_refresh_channel_params(priv);
136300282Shselasky		break;
137292949Shselasky
138300282Shselasky	case MLX5_PARAM_OFFSET(rx_coalesce_pkts):
139300282Shselasky		/* import RX coal pkts */
140300282Shselasky		if (priv->params_ethtool.rx_coalesce_pkts < 1)
141300282Shselasky			priv->params_ethtool.rx_coalesce_pkts = 0;
142300282Shselasky		else if (priv->params_ethtool.rx_coalesce_pkts >
143300282Shselasky		    MLX5E_FLD_MAX(cqc, cq_max_count)) {
144300282Shselasky			priv->params_ethtool.rx_coalesce_pkts =
145300282Shselasky			    MLX5E_FLD_MAX(cqc, cq_max_count);
146300282Shselasky		}
147300282Shselasky		priv->params.rx_cq_moderation_pkts =
148300282Shselasky		    priv->params_ethtool.rx_coalesce_pkts;
149292949Shselasky
150300282Shselasky		/* check to avoid down and up the network interface */
151300282Shselasky		if (was_opened)
152300282Shselasky			error = mlx5e_refresh_channel_params(priv);
153300282Shselasky		break;
154292949Shselasky
155300282Shselasky	case MLX5_PARAM_OFFSET(tx_coalesce_usecs):
156300282Shselasky		/* import TX coal time */
157300282Shselasky		if (priv->params_ethtool.tx_coalesce_usecs < 1)
158300282Shselasky			priv->params_ethtool.tx_coalesce_usecs = 0;
159300282Shselasky		else if (priv->params_ethtool.tx_coalesce_usecs >
160300282Shselasky		    MLX5E_FLD_MAX(cqc, cq_period)) {
161300282Shselasky			priv->params_ethtool.tx_coalesce_usecs =
162300282Shselasky			    MLX5E_FLD_MAX(cqc, cq_period);
163300282Shselasky		}
164300282Shselasky		priv->params.tx_cq_moderation_usec =
165300282Shselasky		    priv->params_ethtool.tx_coalesce_usecs;
166300282Shselasky
167300282Shselasky		/* check to avoid down and up the network interface */
168300282Shselasky		if (was_opened)
169292949Shselasky			error = mlx5e_refresh_channel_params(priv);
170300282Shselasky		break;
171300282Shselasky
172300282Shselasky	case MLX5_PARAM_OFFSET(tx_coalesce_pkts):
173300282Shselasky		/* import TX coal pkts */
174300282Shselasky		if (priv->params_ethtool.tx_coalesce_pkts < 1)
175300282Shselasky			priv->params_ethtool.tx_coalesce_pkts = 0;
176300282Shselasky		else if (priv->params_ethtool.tx_coalesce_pkts >
177300282Shselasky		    MLX5E_FLD_MAX(cqc, cq_max_count)) {
178300282Shselasky			priv->params_ethtool.tx_coalesce_pkts =
179300282Shselasky			    MLX5E_FLD_MAX(cqc, cq_max_count);
180292949Shselasky		}
181300282Shselasky		priv->params.tx_cq_moderation_pkts =
182300282Shselasky		    priv->params_ethtool.tx_coalesce_pkts;
183300282Shselasky
184300282Shselasky		/* check to avoid down and up the network interface */
185300282Shselasky		if (was_opened)
186300282Shselasky			error = mlx5e_refresh_channel_params(priv);
187300282Shselasky		break;
188300282Shselasky
189300282Shselasky	case MLX5_PARAM_OFFSET(tx_queue_size):
190300282Shselasky		/* network interface must be down */
191300282Shselasky		if (was_opened)
192300282Shselasky			mlx5e_close_locked(priv->ifp);
193300282Shselasky
194300282Shselasky		/* import TX queue size */
195300282Shselasky		if (priv->params_ethtool.tx_queue_size <
196300282Shselasky		    (1 << MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE)) {
197300282Shselasky			priv->params_ethtool.tx_queue_size =
198300282Shselasky			    (1 << MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE);
199300282Shselasky		} else if (priv->params_ethtool.tx_queue_size >
200300282Shselasky		    priv->params_ethtool.tx_queue_size_max) {
201300282Shselasky			priv->params_ethtool.tx_queue_size =
202300282Shselasky			    priv->params_ethtool.tx_queue_size_max;
203300282Shselasky		}
204300282Shselasky		/* store actual TX queue size */
205300282Shselasky		priv->params.log_sq_size =
206300282Shselasky		    order_base_2(priv->params_ethtool.tx_queue_size);
207290650Shselasky		priv->params_ethtool.tx_queue_size =
208300282Shselasky		    1 << priv->params.log_sq_size;
209290650Shselasky
210300282Shselasky		/* verify TX completion factor */
211300282Shselasky		mlx5e_ethtool_sync_tx_completion_fact(priv);
212300282Shselasky
213300282Shselasky		/* restart network interface, if any */
214300282Shselasky		if (was_opened)
215300282Shselasky			mlx5e_open_locked(priv->ifp);
216300282Shselasky		break;
217300282Shselasky
218300282Shselasky	case MLX5_PARAM_OFFSET(rx_queue_size):
219300282Shselasky		/* network interface must be down */
220300282Shselasky		if (was_opened)
221300282Shselasky			mlx5e_close_locked(priv->ifp);
222300282Shselasky
223300282Shselasky		/* import RX queue size */
224300282Shselasky		if (priv->params_ethtool.rx_queue_size <
225300282Shselasky		    (1 << MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE)) {
226300282Shselasky			priv->params_ethtool.rx_queue_size =
227300282Shselasky			    (1 << MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE);
228300282Shselasky		} else if (priv->params_ethtool.rx_queue_size >
229300282Shselasky		    priv->params_ethtool.rx_queue_size_max) {
230300282Shselasky			priv->params_ethtool.rx_queue_size =
231300282Shselasky			    priv->params_ethtool.rx_queue_size_max;
232300282Shselasky		}
233300282Shselasky		/* store actual RX queue size */
234300282Shselasky		priv->params.log_rq_size =
235300282Shselasky		    order_base_2(priv->params_ethtool.rx_queue_size);
236290650Shselasky		priv->params_ethtool.rx_queue_size =
237300282Shselasky		    1 << priv->params.log_rq_size;
238290650Shselasky
239300282Shselasky		/* update least number of RX WQEs */
240300282Shselasky		priv->params.min_rx_wqes = min(
241300282Shselasky		    priv->params_ethtool.rx_queue_size - 1,
242300282Shselasky		    MLX5E_PARAMS_DEFAULT_MIN_RX_WQES);
243290650Shselasky
244300282Shselasky		/* restart network interface, if any */
245300282Shselasky		if (was_opened)
246300282Shselasky			mlx5e_open_locked(priv->ifp);
247300282Shselasky		break;
248290650Shselasky
249300282Shselasky	case MLX5_PARAM_OFFSET(channels):
250300282Shselasky		/* network interface must be down */
251300282Shselasky		if (was_opened)
252300282Shselasky			mlx5e_close_locked(priv->ifp);
253290650Shselasky
254300282Shselasky		/* import number of channels */
255300282Shselasky		if (priv->params_ethtool.channels < 1)
256300282Shselasky			priv->params_ethtool.channels = 1;
257300282Shselasky		else if (priv->params_ethtool.channels >
258300282Shselasky		    (u64) priv->mdev->priv.eq_table.num_comp_vectors) {
259300282Shselasky			priv->params_ethtool.channels =
260300282Shselasky			    (u64) priv->mdev->priv.eq_table.num_comp_vectors;
261300282Shselasky		}
262300282Shselasky		priv->params.num_channels = priv->params_ethtool.channels;
263291932Shselasky
264300282Shselasky		/* restart network interface, if any */
265300282Shselasky		if (was_opened)
266300282Shselasky			mlx5e_open_locked(priv->ifp);
267300282Shselasky		break;
268300282Shselasky
269300282Shselasky	case MLX5_PARAM_OFFSET(rx_coalesce_mode):
270300282Shselasky		/* network interface must be down */
271321995Shselasky		if (was_opened != 0 && mode_modify == 0)
272300282Shselasky			mlx5e_close_locked(priv->ifp);
273300282Shselasky
274300282Shselasky		/* import RX coalesce mode */
275300282Shselasky		if (priv->params_ethtool.rx_coalesce_mode != 0)
276300282Shselasky			priv->params_ethtool.rx_coalesce_mode = 1;
277300282Shselasky		priv->params.rx_cq_moderation_mode =
278300282Shselasky		    priv->params_ethtool.rx_coalesce_mode;
279300282Shselasky
280300282Shselasky		/* restart network interface, if any */
281321995Shselasky		if (was_opened != 0) {
282321995Shselasky			if (mode_modify == 0)
283321995Shselasky				mlx5e_open_locked(priv->ifp);
284321995Shselasky			else
285321995Shselasky				error = mlx5e_refresh_channel_params(priv);
286321995Shselasky		}
287300282Shselasky		break;
288300282Shselasky
289300282Shselasky	case MLX5_PARAM_OFFSET(tx_coalesce_mode):
290300282Shselasky		/* network interface must be down */
291321995Shselasky		if (was_opened != 0 && mode_modify == 0)
292300282Shselasky			mlx5e_close_locked(priv->ifp);
293300282Shselasky
294300282Shselasky		/* import TX coalesce mode */
295300282Shselasky		if (priv->params_ethtool.tx_coalesce_mode != 0)
296300282Shselasky			priv->params_ethtool.tx_coalesce_mode = 1;
297300282Shselasky		priv->params.tx_cq_moderation_mode =
298300282Shselasky		    priv->params_ethtool.tx_coalesce_mode;
299300282Shselasky
300300282Shselasky		/* restart network interface, if any */
301321995Shselasky		if (was_opened != 0) {
302321995Shselasky			if (mode_modify == 0)
303321995Shselasky				mlx5e_open_locked(priv->ifp);
304321995Shselasky			else
305321995Shselasky				error = mlx5e_refresh_channel_params(priv);
306321995Shselasky		}
307300282Shselasky		break;
308300282Shselasky
309300282Shselasky	case MLX5_PARAM_OFFSET(hw_lro):
310300282Shselasky		/* network interface must be down */
311300282Shselasky		if (was_opened)
312300282Shselasky			mlx5e_close_locked(priv->ifp);
313300282Shselasky
314300282Shselasky		/* import HW LRO mode */
315300282Shselasky		if (priv->params_ethtool.hw_lro != 0) {
316300282Shselasky			if ((priv->ifp->if_capenable & IFCAP_LRO) &&
317300282Shselasky			    MLX5_CAP_ETH(priv->mdev, lro_cap)) {
318300282Shselasky				priv->params.hw_lro_en = 1;
319300282Shselasky				priv->params_ethtool.hw_lro = 1;
320300282Shselasky			} else {
321300282Shselasky				priv->params.hw_lro_en = 0;
322300282Shselasky				priv->params_ethtool.hw_lro = 0;
323300282Shselasky				error = EINVAL;
324300282Shselasky
325300282Shselasky				if_printf(priv->ifp, "Can't enable HW LRO: "
326300282Shselasky				    "The HW or SW LRO feature is disabled\n");
327300282Shselasky			}
328294319Shselasky		} else {
329294319Shselasky			priv->params.hw_lro_en = 0;
330291068Shselasky		}
331300282Shselasky		/* restart network interface, if any */
332300282Shselasky		if (was_opened)
333300282Shselasky			mlx5e_open_locked(priv->ifp);
334300282Shselasky		break;
335290650Shselasky
336300282Shselasky	case MLX5_PARAM_OFFSET(cqe_zipping):
337300282Shselasky		/* network interface must be down */
338300282Shselasky		if (was_opened)
339300282Shselasky			mlx5e_close_locked(priv->ifp);
340300282Shselasky
341300282Shselasky		/* import CQE zipping mode */
342292838Shselasky		if (priv->params_ethtool.cqe_zipping &&
343292838Shselasky		    MLX5_CAP_GEN(priv->mdev, cqe_compression)) {
344292838Shselasky			priv->params.cqe_zipping_en = true;
345292838Shselasky			priv->params_ethtool.cqe_zipping = 1;
346292838Shselasky		} else {
347292838Shselasky			priv->params.cqe_zipping_en = false;
348292838Shselasky			priv->params_ethtool.cqe_zipping = 0;
349292838Shselasky		}
350300282Shselasky		/* restart network interface, if any */
351300282Shselasky		if (was_opened)
352300282Shselasky			mlx5e_open_locked(priv->ifp);
353300282Shselasky		break;
354300277Shselasky
355300282Shselasky	case MLX5_PARAM_OFFSET(tx_completion_fact):
356300282Shselasky		/* network interface must be down */
357300282Shselasky		if (was_opened)
358300282Shselasky			mlx5e_close_locked(priv->ifp);
359300282Shselasky
360300277Shselasky		/* verify parameter */
361300277Shselasky		mlx5e_ethtool_sync_tx_completion_fact(priv);
362300282Shselasky
363300282Shselasky		/* restart network interface, if any */
364300282Shselasky		if (was_opened)
365300282Shselasky			mlx5e_open_locked(priv->ifp);
366300282Shselasky		break;
367300282Shselasky
368300282Shselasky	default:
369300282Shselasky		break;
370300277Shselasky	}
371290650Shselaskydone:
372290650Shselasky	PRIV_UNLOCK(priv);
373290650Shselasky	return (error);
374290650Shselasky}
375290650Shselasky
376290650Shselasky/*
377290650Shselasky * Read the first three bytes of the eeprom in order to get the needed info
378290650Shselasky * for the whole reading.
379290650Shselasky * Byte 0 - Identifier byte
380290650Shselasky * Byte 1 - Revision byte
381290650Shselasky * Byte 2 - Status byte
382290650Shselasky */
383290650Shselaskystatic int
384290650Shselaskymlx5e_get_eeprom_info(struct mlx5e_priv *priv, struct mlx5e_eeprom *eeprom)
385290650Shselasky{
386290650Shselasky	struct mlx5_core_dev *dev = priv->mdev;
387290650Shselasky	u32 data = 0;
388290650Shselasky	int size_read = 0;
389290650Shselasky	int ret;
390290650Shselasky
391290650Shselasky	ret = mlx5_query_module_num(dev, &eeprom->module_num);
392290650Shselasky	if (ret) {
393290650Shselasky		if_printf(priv->ifp, "%s:%d: Failed query module error=%d\n",
394290650Shselasky		    __func__, __LINE__, ret);
395290650Shselasky		return (ret);
396290650Shselasky	}
397290650Shselasky
398290650Shselasky	/* Read the first three bytes to get Identifier, Revision and Status */
399290650Shselasky	ret = mlx5_query_eeprom(dev, eeprom->i2c_addr, eeprom->page_num,
400290650Shselasky	    eeprom->device_addr, MLX5E_EEPROM_INFO_BYTES, eeprom->module_num, &data,
401290650Shselasky	    &size_read);
402290650Shselasky	if (ret) {
403290650Shselasky		if_printf(priv->ifp, "%s:%d: Failed query eeprom module error=0x%x\n",
404290650Shselasky		    __func__, __LINE__, ret);
405290650Shselasky		return (ret);
406290650Shselasky	}
407290650Shselasky
408290650Shselasky	switch (data & MLX5_EEPROM_IDENTIFIER_BYTE_MASK) {
409290650Shselasky	case SFF_8024_ID_QSFP:
410290650Shselasky		eeprom->type = MLX5E_ETH_MODULE_SFF_8436;
411290650Shselasky		eeprom->len = MLX5E_ETH_MODULE_SFF_8436_LEN;
412290650Shselasky		break;
413290650Shselasky	case SFF_8024_ID_QSFPPLUS:
414290650Shselasky	case SFF_8024_ID_QSFP28:
415290650Shselasky		if ((data & MLX5_EEPROM_IDENTIFIER_BYTE_MASK) == SFF_8024_ID_QSFP28 ||
416291070Shselasky		    ((data & MLX5_EEPROM_REVISION_ID_BYTE_MASK) >> 8) >= 0x3) {
417290650Shselasky			eeprom->type = MLX5E_ETH_MODULE_SFF_8636;
418290650Shselasky			eeprom->len = MLX5E_ETH_MODULE_SFF_8636_LEN;
419290650Shselasky		} else {
420290650Shselasky			eeprom->type = MLX5E_ETH_MODULE_SFF_8436;
421290650Shselasky			eeprom->len = MLX5E_ETH_MODULE_SFF_8436_LEN;
422290650Shselasky		}
423290650Shselasky		if ((data & MLX5_EEPROM_PAGE_3_VALID_BIT_MASK) == 0)
424290650Shselasky			eeprom->page_valid = 1;
425290650Shselasky		break;
426290650Shselasky	case SFF_8024_ID_SFP:
427290650Shselasky		eeprom->type = MLX5E_ETH_MODULE_SFF_8472;
428290650Shselasky		eeprom->len = MLX5E_ETH_MODULE_SFF_8472_LEN;
429290650Shselasky		break;
430290650Shselasky	default:
431291067Shselasky		if_printf(priv->ifp, "%s:%d: Not recognized cable type = 0x%x(%s)\n",
432291067Shselasky		    __func__, __LINE__, data & MLX5_EEPROM_IDENTIFIER_BYTE_MASK,
433291067Shselasky		    sff_8024_id[data & MLX5_EEPROM_IDENTIFIER_BYTE_MASK]);
434290650Shselasky		return (EINVAL);
435290650Shselasky	}
436290650Shselasky	return (0);
437290650Shselasky}
438290650Shselasky
439290650Shselasky/* Read both low and high pages of the eeprom */
440290650Shselaskystatic int
441290650Shselaskymlx5e_get_eeprom(struct mlx5e_priv *priv, struct mlx5e_eeprom *ee)
442290650Shselasky{
443290650Shselasky	struct mlx5_core_dev *dev = priv->mdev;
444290650Shselasky	int size_read = 0;
445290650Shselasky	int ret;
446290650Shselasky
447290650Shselasky	if (ee->len == 0)
448290650Shselasky		return (EINVAL);
449290650Shselasky
450290650Shselasky	/* Read low page of the eeprom */
451290650Shselasky	while (ee->device_addr < ee->len) {
452290650Shselasky		ret = mlx5_query_eeprom(dev, ee->i2c_addr, ee->page_num, ee->device_addr,
453290650Shselasky		    ee->len - ee->device_addr, ee->module_num,
454291070Shselasky		    ee->data + (ee->device_addr / 4), &size_read);
455290650Shselasky		if (ret) {
456290650Shselasky			if_printf(priv->ifp, "%s:%d: Failed reading eeprom, "
457290650Shselasky			    "error = 0x%02x\n", __func__, __LINE__, ret);
458290650Shselasky			return (ret);
459290650Shselasky		}
460290650Shselasky		ee->device_addr += size_read;
461290650Shselasky	}
462290650Shselasky
463290650Shselasky	/* Read high page of the eeprom */
464290650Shselasky	if (ee->page_valid) {
465290650Shselasky		ee->device_addr = MLX5E_EEPROM_HIGH_PAGE_OFFSET;
466290650Shselasky		ee->page_num = MLX5E_EEPROM_HIGH_PAGE;
467290650Shselasky		size_read = 0;
468290650Shselasky		while (ee->device_addr < MLX5E_EEPROM_PAGE_LENGTH) {
469290650Shselasky			ret = mlx5_query_eeprom(dev, ee->i2c_addr, ee->page_num,
470290650Shselasky			    ee->device_addr, MLX5E_EEPROM_PAGE_LENGTH - ee->device_addr,
471291070Shselasky			    ee->module_num, ee->data + (ee->len / 4) +
472291070Shselasky			    ((ee->device_addr - MLX5E_EEPROM_HIGH_PAGE_OFFSET) / 4),
473290650Shselasky			    &size_read);
474290650Shselasky			if (ret) {
475290650Shselasky				if_printf(priv->ifp, "%s:%d: Failed reading eeprom, "
476290650Shselasky				    "error = 0x%02x\n", __func__, __LINE__, ret);
477290650Shselasky				return (ret);
478290650Shselasky			}
479290650Shselasky			ee->device_addr += size_read;
480290650Shselasky		}
481290650Shselasky	}
482290650Shselasky	return (0);
483290650Shselasky}
484290650Shselasky
485290650Shselaskystatic void
486290650Shselaskymlx5e_print_eeprom(struct mlx5e_eeprom *eeprom)
487290650Shselasky{
488292835Shselasky	int row;
489292835Shselasky	int index_in_row;
490292835Shselasky	int byte_to_write = 0;
491292835Shselasky	int line_length = 16;
492290650Shselasky
493290650Shselasky	printf("\nOffset\t\tValues\n");
494292835Shselasky	printf("------\t\t------");
495292835Shselasky	while (byte_to_write < eeprom->len) {
496292835Shselasky		printf("\n0x%04X\t\t", byte_to_write);
497292835Shselasky		for (index_in_row = 0; index_in_row < line_length; index_in_row++) {
498292835Shselasky			printf("%02X ", ((u8 *)eeprom->data)[byte_to_write]);
499292835Shselasky			byte_to_write++;
500290650Shselasky		}
501290650Shselasky	}
502290650Shselasky
503290650Shselasky	if (eeprom->page_valid) {
504290650Shselasky		row = MLX5E_EEPROM_HIGH_PAGE_OFFSET;
505292835Shselasky		printf("\n\nUpper Page 0x03\n");
506290650Shselasky		printf("\nOffset\t\tValues\n");
507292835Shselasky		printf("------\t\t------");
508290650Shselasky		while (row < MLX5E_EEPROM_PAGE_LENGTH) {
509292835Shselasky			printf("\n0x%04X\t\t", row);
510292835Shselasky			for (index_in_row = 0; index_in_row < line_length; index_in_row++) {
511292835Shselasky				printf("%02X ", ((u8 *)eeprom->data)[byte_to_write]);
512292835Shselasky				byte_to_write++;
513290650Shselasky				row++;
514290650Shselasky			}
515290650Shselasky		}
516290650Shselasky	}
517290650Shselasky}
518290650Shselasky
519290650Shselasky/*
520290650Shselasky * Read cable EEPROM module information by first inspecting the first
521290650Shselasky * three bytes to get the initial information for a whole reading.
522290650Shselasky * Information will be printed to dmesg.
523290650Shselasky */
524290650Shselaskystatic int
525290650Shselaskymlx5e_read_eeprom(SYSCTL_HANDLER_ARGS)
526290650Shselasky{
527290650Shselasky	struct mlx5e_priv *priv = arg1;
528290650Shselasky	struct mlx5e_eeprom eeprom;
529290650Shselasky	int error;
530290650Shselasky	int result = 0;
531290650Shselasky
532290650Shselasky	PRIV_LOCK(priv);
533290650Shselasky	error = sysctl_handle_int(oidp, &result, 0, req);
534290650Shselasky	if (error || !req->newptr)
535290650Shselasky		goto done;
536290650Shselasky
537290650Shselasky	/* Check if device is gone */
538290650Shselasky	if (priv->gone) {
539290650Shselasky		error = ENXIO;
540290650Shselasky		goto done;
541290650Shselasky	}
542290650Shselasky
543290650Shselasky	if (result == 1) {
544290650Shselasky		eeprom.i2c_addr = MLX5E_I2C_ADDR_LOW;
545290650Shselasky		eeprom.device_addr = 0;
546290650Shselasky		eeprom.page_num = MLX5E_EEPROM_LOW_PAGE;
547290650Shselasky		eeprom.page_valid = 0;
548290650Shselasky
549290650Shselasky		/* Read three first bytes to get important info */
550290650Shselasky		error = mlx5e_get_eeprom_info(priv, &eeprom);
551290650Shselasky		if (error) {
552290650Shselasky			if_printf(priv->ifp, "%s:%d: Failed reading eeprom's "
553290650Shselasky			    "initial information\n", __func__, __LINE__);
554290650Shselasky			error = 0;
555290650Shselasky			goto done;
556290650Shselasky		}
557291070Shselasky		/*
558291070Shselasky		 * Allocate needed length buffer and additional space for
559291070Shselasky		 * page 0x03
560291070Shselasky		 */
561290650Shselasky		eeprom.data = malloc(eeprom.len + MLX5E_EEPROM_PAGE_LENGTH,
562290650Shselasky		    M_MLX5EN, M_WAITOK | M_ZERO);
563290650Shselasky
564290650Shselasky		/* Read the whole eeprom information */
565290650Shselasky		error = mlx5e_get_eeprom(priv, &eeprom);
566290650Shselasky		if (error) {
567290650Shselasky			if_printf(priv->ifp, "%s:%d: Failed reading eeprom\n",
568290650Shselasky			    __func__, __LINE__);
569290650Shselasky			error = 0;
570291070Shselasky			/*
571291070Shselasky			 * Continue printing partial information in case of
572291070Shselasky			 * an error
573291070Shselasky			 */
574290650Shselasky		}
575290650Shselasky		mlx5e_print_eeprom(&eeprom);
576290650Shselasky		free(eeprom.data, M_MLX5EN);
577290650Shselasky	}
578290650Shselaskydone:
579290650Shselasky	PRIV_UNLOCK(priv);
580290650Shselasky	return (error);
581290650Shselasky}
582290650Shselasky
583290650Shselaskystatic const char *mlx5e_params_desc[] = {
584290650Shselasky	MLX5E_PARAMS(MLX5E_STATS_DESC)
585290650Shselasky};
586290650Shselasky
587290650Shselaskystatic const char *mlx5e_port_stats_debug_desc[] = {
588290650Shselasky	MLX5E_PORT_STATS_DEBUG(MLX5E_STATS_DESC)
589290650Shselasky};
590290650Shselasky
591290650Shselaskystatic int
592290650Shselaskymlx5e_ethtool_debug_stats(SYSCTL_HANDLER_ARGS)
593290650Shselasky{
594290650Shselasky	struct mlx5e_priv *priv = arg1;
595290650Shselasky	int error;
596290650Shselasky	int sys_debug;
597290650Shselasky
598290650Shselasky	sys_debug = priv->sysctl_debug;
599290650Shselasky	error = sysctl_handle_int(oidp, &priv->sysctl_debug, 0, req);
600290650Shselasky	if (error || !req->newptr)
601290650Shselasky		return (error);
602290650Shselasky	priv->sysctl_debug = !!priv->sysctl_debug;
603290650Shselasky	if (sys_debug == priv->sysctl_debug)
604290650Shselasky		return (error);
605290650Shselasky	if (priv->sysctl_debug)
606290650Shselasky		mlx5e_create_stats(&priv->stats.port_stats_debug.ctx,
607290650Shselasky		    SYSCTL_CHILDREN(priv->sysctl_ifnet), "debug_stats",
608290650Shselasky		    mlx5e_port_stats_debug_desc, MLX5E_PORT_STATS_DEBUG_NUM,
609290650Shselasky		    priv->stats.port_stats_debug.arg);
610290650Shselasky	else
611290650Shselasky		sysctl_ctx_free(&priv->stats.port_stats_debug.ctx);
612290650Shselasky	return (error);
613290650Shselasky}
614290650Shselasky
615290650Shselaskyvoid
616290650Shselaskymlx5e_create_ethtool(struct mlx5e_priv *priv)
617290650Shselasky{
618290650Shselasky	struct sysctl_oid *node;
619290650Shselasky	const char *pnameunit;
620290650Shselasky	unsigned x;
621290650Shselasky
622290650Shselasky	/* set some defaults */
623290650Shselasky	priv->params_ethtool.tx_queue_size_max = 1 << MLX5E_PARAMS_MAXIMUM_LOG_SQ_SIZE;
624290650Shselasky	priv->params_ethtool.rx_queue_size_max = 1 << MLX5E_PARAMS_MAXIMUM_LOG_RQ_SIZE;
625290650Shselasky	priv->params_ethtool.tx_queue_size = 1 << priv->params.log_sq_size;
626290650Shselasky	priv->params_ethtool.rx_queue_size = 1 << priv->params.log_rq_size;
627290650Shselasky	priv->params_ethtool.channels = priv->params.num_channels;
628290650Shselasky	priv->params_ethtool.coalesce_pkts_max = MLX5E_FLD_MAX(cqc, cq_max_count);
629290650Shselasky	priv->params_ethtool.coalesce_usecs_max = MLX5E_FLD_MAX(cqc, cq_period);
630290650Shselasky	priv->params_ethtool.rx_coalesce_mode = priv->params.rx_cq_moderation_mode;
631290650Shselasky	priv->params_ethtool.rx_coalesce_usecs = priv->params.rx_cq_moderation_usec;
632290650Shselasky	priv->params_ethtool.rx_coalesce_pkts = priv->params.rx_cq_moderation_pkts;
633291932Shselasky	priv->params_ethtool.tx_coalesce_mode = priv->params.tx_cq_moderation_mode;
634290650Shselasky	priv->params_ethtool.tx_coalesce_usecs = priv->params.tx_cq_moderation_usec;
635290650Shselasky	priv->params_ethtool.tx_coalesce_pkts = priv->params.tx_cq_moderation_pkts;
636290650Shselasky	priv->params_ethtool.hw_lro = priv->params.hw_lro_en;
637292838Shselasky	priv->params_ethtool.cqe_zipping = priv->params.cqe_zipping_en;
638300277Shselasky	mlx5e_ethtool_sync_tx_completion_fact(priv);
639290650Shselasky
640290650Shselasky	/* create root node */
641290650Shselasky	node = SYSCTL_ADD_NODE(&priv->sysctl_ctx,
642290650Shselasky	    SYSCTL_CHILDREN(priv->sysctl_ifnet), OID_AUTO,
643290650Shselasky	    "conf", CTLFLAG_RW, NULL, "Configuration");
644290650Shselasky	if (node == NULL)
645290650Shselasky		return;
646290650Shselasky	for (x = 0; x != MLX5E_PARAMS_NUM; x++) {
647290650Shselasky		/* check for read-only parameter */
648290650Shselasky		if (strstr(mlx5e_params_desc[2 * x], "_max") != NULL) {
649290650Shselasky			SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO,
650290650Shselasky			    mlx5e_params_desc[2 * x], CTLTYPE_U64 | CTLFLAG_RD |
651290650Shselasky			    CTLFLAG_MPSAFE, priv, x, &mlx5e_ethtool_handler, "QU",
652290650Shselasky			    mlx5e_params_desc[2 * x + 1]);
653290650Shselasky		} else {
654292837Shselasky#if (__FreeBSD_version < 1100000)
655292837Shselasky			char path[64];
656292837Shselasky#endif
657292837Shselasky			/*
658292837Shselasky			 * NOTE: In FreeBSD-11 and newer the
659292837Shselasky			 * CTLFLAG_RWTUN flag will take care of
660292837Shselasky			 * loading default sysctl value from the
661292837Shselasky			 * kernel environment, if any:
662292837Shselasky			 */
663290650Shselasky			SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO,
664290650Shselasky			    mlx5e_params_desc[2 * x], CTLTYPE_U64 | CTLFLAG_RWTUN |
665290650Shselasky			    CTLFLAG_MPSAFE, priv, x, &mlx5e_ethtool_handler, "QU",
666290650Shselasky			    mlx5e_params_desc[2 * x + 1]);
667292837Shselasky
668292837Shselasky#if (__FreeBSD_version < 1100000)
669292837Shselasky			/* compute path for sysctl */
670292837Shselasky			snprintf(path, sizeof(path), "dev.mce.%d.conf.%s",
671292837Shselasky			    device_get_unit(priv->mdev->pdev->dev.bsddev),
672292837Shselasky			    mlx5e_params_desc[2 * x]);
673292837Shselasky
674292837Shselasky			/* try to fetch tunable, if any */
675292837Shselasky			if (TUNABLE_QUAD_FETCH(path, &priv->params_ethtool.arg[x]))
676292837Shselasky				mlx5e_ethtool_handler(NULL, priv, x, NULL);
677292837Shselasky#endif
678290650Shselasky		}
679290650Shselasky	}
680290650Shselasky
681290650Shselasky	SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO,
682290650Shselasky	    "debug_stats", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, priv,
683290650Shselasky	    0, &mlx5e_ethtool_debug_stats, "I", "Extended debug statistics");
684290650Shselasky
685290650Shselasky	pnameunit = device_get_nameunit(priv->mdev->pdev->dev.bsddev);
686290650Shselasky
687290650Shselasky	SYSCTL_ADD_STRING(&priv->sysctl_ctx, SYSCTL_CHILDREN(node),
688290650Shselasky	    OID_AUTO, "device_name", CTLFLAG_RD,
689290650Shselasky	    __DECONST(void *, pnameunit), 0,
690290650Shselasky	    "PCI device name");
691290650Shselasky
692290650Shselasky	/* EEPROM support */
693290650Shselasky	SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO, "eeprom_info",
694290650Shselasky	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, priv, 0,
695290650Shselasky	    mlx5e_read_eeprom, "I", "EEPROM information");
696290650Shselasky}
697