mlx5_en_ethtool.c revision 300282
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: head/sys/dev/mlx5/mlx5_en/mlx5_en_ethtool.c 300282 2016-05-20 07:07:27Z 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;
95290650Shselasky	int was_opened;
96290650Shselasky	int error;
97290650Shselasky
98290650Shselasky	PRIV_LOCK(priv);
99290650Shselasky	value = priv->params_ethtool.arg[arg2];
100292837Shselasky	if (req != NULL) {
101292837Shselasky		error = sysctl_handle_64(oidp, &value, 0, req);
102292837Shselasky		if (error || req->newptr == NULL ||
103292837Shselasky		    value == priv->params_ethtool.arg[arg2])
104292837Shselasky			goto done;
105290650Shselasky
106292837Shselasky		/* assign new value */
107292837Shselasky		priv->params_ethtool.arg[arg2] = value;
108292837Shselasky	} else {
109292837Shselasky		error = 0;
110292837Shselasky	}
111290650Shselasky	/* check if device is gone */
112290650Shselasky	if (priv->gone) {
113290650Shselasky		error = ENXIO;
114290650Shselasky		goto done;
115290650Shselasky	}
116300282Shselasky	was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
117290650Shselasky
118300282Shselasky	switch (MLX5_PARAM_OFFSET(arg[arg2])) {
119300282Shselasky	case MLX5_PARAM_OFFSET(rx_coalesce_usecs):
120300282Shselasky		/* import RX coal time */
121300282Shselasky		if (priv->params_ethtool.rx_coalesce_usecs < 1)
122300282Shselasky			priv->params_ethtool.rx_coalesce_usecs = 0;
123300282Shselasky		else if (priv->params_ethtool.rx_coalesce_usecs >
124300282Shselasky		    MLX5E_FLD_MAX(cqc, cq_period)) {
125300282Shselasky			priv->params_ethtool.rx_coalesce_usecs =
126300282Shselasky			    MLX5E_FLD_MAX(cqc, cq_period);
127300282Shselasky		}
128300282Shselasky		priv->params.rx_cq_moderation_usec =
129300282Shselasky		    priv->params_ethtool.rx_coalesce_usecs;
130292949Shselasky
131300282Shselasky		/* check to avoid down and up the network interface */
132300282Shselasky		if (was_opened)
133300282Shselasky			error = mlx5e_refresh_channel_params(priv);
134300282Shselasky		break;
135292949Shselasky
136300282Shselasky	case MLX5_PARAM_OFFSET(rx_coalesce_pkts):
137300282Shselasky		/* import RX coal pkts */
138300282Shselasky		if (priv->params_ethtool.rx_coalesce_pkts < 1)
139300282Shselasky			priv->params_ethtool.rx_coalesce_pkts = 0;
140300282Shselasky		else if (priv->params_ethtool.rx_coalesce_pkts >
141300282Shselasky		    MLX5E_FLD_MAX(cqc, cq_max_count)) {
142300282Shselasky			priv->params_ethtool.rx_coalesce_pkts =
143300282Shselasky			    MLX5E_FLD_MAX(cqc, cq_max_count);
144300282Shselasky		}
145300282Shselasky		priv->params.rx_cq_moderation_pkts =
146300282Shselasky		    priv->params_ethtool.rx_coalesce_pkts;
147292949Shselasky
148300282Shselasky		/* check to avoid down and up the network interface */
149300282Shselasky		if (was_opened)
150300282Shselasky			error = mlx5e_refresh_channel_params(priv);
151300282Shselasky		break;
152292949Shselasky
153300282Shselasky	case MLX5_PARAM_OFFSET(tx_coalesce_usecs):
154300282Shselasky		/* import TX coal time */
155300282Shselasky		if (priv->params_ethtool.tx_coalesce_usecs < 1)
156300282Shselasky			priv->params_ethtool.tx_coalesce_usecs = 0;
157300282Shselasky		else if (priv->params_ethtool.tx_coalesce_usecs >
158300282Shselasky		    MLX5E_FLD_MAX(cqc, cq_period)) {
159300282Shselasky			priv->params_ethtool.tx_coalesce_usecs =
160300282Shselasky			    MLX5E_FLD_MAX(cqc, cq_period);
161300282Shselasky		}
162300282Shselasky		priv->params.tx_cq_moderation_usec =
163300282Shselasky		    priv->params_ethtool.tx_coalesce_usecs;
164300282Shselasky
165300282Shselasky		/* check to avoid down and up the network interface */
166300282Shselasky		if (was_opened)
167292949Shselasky			error = mlx5e_refresh_channel_params(priv);
168300282Shselasky		break;
169300282Shselasky
170300282Shselasky	case MLX5_PARAM_OFFSET(tx_coalesce_pkts):
171300282Shselasky		/* import TX coal pkts */
172300282Shselasky		if (priv->params_ethtool.tx_coalesce_pkts < 1)
173300282Shselasky			priv->params_ethtool.tx_coalesce_pkts = 0;
174300282Shselasky		else if (priv->params_ethtool.tx_coalesce_pkts >
175300282Shselasky		    MLX5E_FLD_MAX(cqc, cq_max_count)) {
176300282Shselasky			priv->params_ethtool.tx_coalesce_pkts =
177300282Shselasky			    MLX5E_FLD_MAX(cqc, cq_max_count);
178292949Shselasky		}
179300282Shselasky		priv->params.tx_cq_moderation_pkts =
180300282Shselasky		    priv->params_ethtool.tx_coalesce_pkts;
181300282Shselasky
182300282Shselasky		/* check to avoid down and up the network interface */
183300282Shselasky		if (was_opened)
184300282Shselasky			error = mlx5e_refresh_channel_params(priv);
185300282Shselasky		break;
186300282Shselasky
187300282Shselasky	case MLX5_PARAM_OFFSET(tx_queue_size):
188300282Shselasky		/* network interface must be down */
189300282Shselasky		if (was_opened)
190300282Shselasky			mlx5e_close_locked(priv->ifp);
191300282Shselasky
192300282Shselasky		/* import TX queue size */
193300282Shselasky		if (priv->params_ethtool.tx_queue_size <
194300282Shselasky		    (1 << MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE)) {
195300282Shselasky			priv->params_ethtool.tx_queue_size =
196300282Shselasky			    (1 << MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE);
197300282Shselasky		} else if (priv->params_ethtool.tx_queue_size >
198300282Shselasky		    priv->params_ethtool.tx_queue_size_max) {
199300282Shselasky			priv->params_ethtool.tx_queue_size =
200300282Shselasky			    priv->params_ethtool.tx_queue_size_max;
201300282Shselasky		}
202300282Shselasky		/* store actual TX queue size */
203300282Shselasky		priv->params.log_sq_size =
204300282Shselasky		    order_base_2(priv->params_ethtool.tx_queue_size);
205290650Shselasky		priv->params_ethtool.tx_queue_size =
206300282Shselasky		    1 << priv->params.log_sq_size;
207290650Shselasky
208300282Shselasky		/* verify TX completion factor */
209300282Shselasky		mlx5e_ethtool_sync_tx_completion_fact(priv);
210300282Shselasky
211300282Shselasky		/* restart network interface, if any */
212300282Shselasky		if (was_opened)
213300282Shselasky			mlx5e_open_locked(priv->ifp);
214300282Shselasky		break;
215300282Shselasky
216300282Shselasky	case MLX5_PARAM_OFFSET(rx_queue_size):
217300282Shselasky		/* network interface must be down */
218300282Shselasky		if (was_opened)
219300282Shselasky			mlx5e_close_locked(priv->ifp);
220300282Shselasky
221300282Shselasky		/* import RX queue size */
222300282Shselasky		if (priv->params_ethtool.rx_queue_size <
223300282Shselasky		    (1 << MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE)) {
224300282Shselasky			priv->params_ethtool.rx_queue_size =
225300282Shselasky			    (1 << MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE);
226300282Shselasky		} else if (priv->params_ethtool.rx_queue_size >
227300282Shselasky		    priv->params_ethtool.rx_queue_size_max) {
228300282Shselasky			priv->params_ethtool.rx_queue_size =
229300282Shselasky			    priv->params_ethtool.rx_queue_size_max;
230300282Shselasky		}
231300282Shselasky		/* store actual RX queue size */
232300282Shselasky		priv->params.log_rq_size =
233300282Shselasky		    order_base_2(priv->params_ethtool.rx_queue_size);
234290650Shselasky		priv->params_ethtool.rx_queue_size =
235300282Shselasky		    1 << priv->params.log_rq_size;
236290650Shselasky
237300282Shselasky		/* update least number of RX WQEs */
238300282Shselasky		priv->params.min_rx_wqes = min(
239300282Shselasky		    priv->params_ethtool.rx_queue_size - 1,
240300282Shselasky		    MLX5E_PARAMS_DEFAULT_MIN_RX_WQES);
241290650Shselasky
242300282Shselasky		/* restart network interface, if any */
243300282Shselasky		if (was_opened)
244300282Shselasky			mlx5e_open_locked(priv->ifp);
245300282Shselasky		break;
246290650Shselasky
247300282Shselasky	case MLX5_PARAM_OFFSET(channels):
248300282Shselasky		/* network interface must be down */
249300282Shselasky		if (was_opened)
250300282Shselasky			mlx5e_close_locked(priv->ifp);
251290650Shselasky
252300282Shselasky		/* import number of channels */
253300282Shselasky		if (priv->params_ethtool.channels < 1)
254300282Shselasky			priv->params_ethtool.channels = 1;
255300282Shselasky		else if (priv->params_ethtool.channels >
256300282Shselasky		    (u64) priv->mdev->priv.eq_table.num_comp_vectors) {
257300282Shselasky			priv->params_ethtool.channels =
258300282Shselasky			    (u64) priv->mdev->priv.eq_table.num_comp_vectors;
259300282Shselasky		}
260300282Shselasky		priv->params.num_channels = priv->params_ethtool.channels;
261291932Shselasky
262300282Shselasky		/* restart network interface, if any */
263300282Shselasky		if (was_opened)
264300282Shselasky			mlx5e_open_locked(priv->ifp);
265300282Shselasky		break;
266300282Shselasky
267300282Shselasky	case MLX5_PARAM_OFFSET(rx_coalesce_mode):
268300282Shselasky		/* network interface must be down */
269300282Shselasky		if (was_opened)
270300282Shselasky			mlx5e_close_locked(priv->ifp);
271300282Shselasky
272300282Shselasky		/* import RX coalesce mode */
273300282Shselasky		if (priv->params_ethtool.rx_coalesce_mode != 0)
274300282Shselasky			priv->params_ethtool.rx_coalesce_mode = 1;
275300282Shselasky		priv->params.rx_cq_moderation_mode =
276300282Shselasky		    priv->params_ethtool.rx_coalesce_mode;
277300282Shselasky
278300282Shselasky		/* restart network interface, if any */
279300282Shselasky		if (was_opened)
280300282Shselasky			mlx5e_open_locked(priv->ifp);
281300282Shselasky		break;
282300282Shselasky
283300282Shselasky	case MLX5_PARAM_OFFSET(tx_coalesce_mode):
284300282Shselasky		/* network interface must be down */
285300282Shselasky		if (was_opened)
286300282Shselasky			mlx5e_close_locked(priv->ifp);
287300282Shselasky
288300282Shselasky		/* import TX coalesce mode */
289300282Shselasky		if (priv->params_ethtool.tx_coalesce_mode != 0)
290300282Shselasky			priv->params_ethtool.tx_coalesce_mode = 1;
291300282Shselasky		priv->params.tx_cq_moderation_mode =
292300282Shselasky		    priv->params_ethtool.tx_coalesce_mode;
293300282Shselasky
294300282Shselasky		/* restart network interface, if any */
295300282Shselasky		if (was_opened)
296300282Shselasky			mlx5e_open_locked(priv->ifp);
297300282Shselasky		break;
298300282Shselasky
299300282Shselasky	case MLX5_PARAM_OFFSET(hw_lro):
300300282Shselasky		/* network interface must be down */
301300282Shselasky		if (was_opened)
302300282Shselasky			mlx5e_close_locked(priv->ifp);
303300282Shselasky
304300282Shselasky		/* import HW LRO mode */
305300282Shselasky		if (priv->params_ethtool.hw_lro != 0) {
306300282Shselasky			if ((priv->ifp->if_capenable & IFCAP_LRO) &&
307300282Shselasky			    MLX5_CAP_ETH(priv->mdev, lro_cap)) {
308300282Shselasky				priv->params.hw_lro_en = 1;
309300282Shselasky				priv->params_ethtool.hw_lro = 1;
310300282Shselasky			} else {
311300282Shselasky				priv->params.hw_lro_en = 0;
312300282Shselasky				priv->params_ethtool.hw_lro = 0;
313300282Shselasky				error = EINVAL;
314300282Shselasky
315300282Shselasky				if_printf(priv->ifp, "Can't enable HW LRO: "
316300282Shselasky				    "The HW or SW LRO feature is disabled\n");
317300282Shselasky			}
318294319Shselasky		} else {
319294319Shselasky			priv->params.hw_lro_en = 0;
320291068Shselasky		}
321300282Shselasky		/* restart network interface, if any */
322300282Shselasky		if (was_opened)
323300282Shselasky			mlx5e_open_locked(priv->ifp);
324300282Shselasky		break;
325290650Shselasky
326300282Shselasky	case MLX5_PARAM_OFFSET(cqe_zipping):
327300282Shselasky		/* network interface must be down */
328300282Shselasky		if (was_opened)
329300282Shselasky			mlx5e_close_locked(priv->ifp);
330300282Shselasky
331300282Shselasky		/* import CQE zipping mode */
332292838Shselasky		if (priv->params_ethtool.cqe_zipping &&
333292838Shselasky		    MLX5_CAP_GEN(priv->mdev, cqe_compression)) {
334292838Shselasky			priv->params.cqe_zipping_en = true;
335292838Shselasky			priv->params_ethtool.cqe_zipping = 1;
336292838Shselasky		} else {
337292838Shselasky			priv->params.cqe_zipping_en = false;
338292838Shselasky			priv->params_ethtool.cqe_zipping = 0;
339292838Shselasky		}
340300282Shselasky		/* restart network interface, if any */
341300282Shselasky		if (was_opened)
342300282Shselasky			mlx5e_open_locked(priv->ifp);
343300282Shselasky		break;
344300277Shselasky
345300282Shselasky	case MLX5_PARAM_OFFSET(tx_completion_fact):
346300282Shselasky		/* network interface must be down */
347300282Shselasky		if (was_opened)
348300282Shselasky			mlx5e_close_locked(priv->ifp);
349300282Shselasky
350300277Shselasky		/* verify parameter */
351300277Shselasky		mlx5e_ethtool_sync_tx_completion_fact(priv);
352300282Shselasky
353300282Shselasky		/* restart network interface, if any */
354300282Shselasky		if (was_opened)
355300282Shselasky			mlx5e_open_locked(priv->ifp);
356300282Shselasky		break;
357300282Shselasky
358300282Shselasky	default:
359300282Shselasky		break;
360300277Shselasky	}
361290650Shselaskydone:
362290650Shselasky	PRIV_UNLOCK(priv);
363290650Shselasky	return (error);
364290650Shselasky}
365290650Shselasky
366290650Shselasky/*
367290650Shselasky * Read the first three bytes of the eeprom in order to get the needed info
368290650Shselasky * for the whole reading.
369290650Shselasky * Byte 0 - Identifier byte
370290650Shselasky * Byte 1 - Revision byte
371290650Shselasky * Byte 2 - Status byte
372290650Shselasky */
373290650Shselaskystatic int
374290650Shselaskymlx5e_get_eeprom_info(struct mlx5e_priv *priv, struct mlx5e_eeprom *eeprom)
375290650Shselasky{
376290650Shselasky	struct mlx5_core_dev *dev = priv->mdev;
377290650Shselasky	u32 data = 0;
378290650Shselasky	int size_read = 0;
379290650Shselasky	int ret;
380290650Shselasky
381290650Shselasky	ret = mlx5_query_module_num(dev, &eeprom->module_num);
382290650Shselasky	if (ret) {
383290650Shselasky		if_printf(priv->ifp, "%s:%d: Failed query module error=%d\n",
384290650Shselasky		    __func__, __LINE__, ret);
385290650Shselasky		return (ret);
386290650Shselasky	}
387290650Shselasky
388290650Shselasky	/* Read the first three bytes to get Identifier, Revision and Status */
389290650Shselasky	ret = mlx5_query_eeprom(dev, eeprom->i2c_addr, eeprom->page_num,
390290650Shselasky	    eeprom->device_addr, MLX5E_EEPROM_INFO_BYTES, eeprom->module_num, &data,
391290650Shselasky	    &size_read);
392290650Shselasky	if (ret) {
393290650Shselasky		if_printf(priv->ifp, "%s:%d: Failed query eeprom module error=0x%x\n",
394290650Shselasky		    __func__, __LINE__, ret);
395290650Shselasky		return (ret);
396290650Shselasky	}
397290650Shselasky
398290650Shselasky	switch (data & MLX5_EEPROM_IDENTIFIER_BYTE_MASK) {
399290650Shselasky	case SFF_8024_ID_QSFP:
400290650Shselasky		eeprom->type = MLX5E_ETH_MODULE_SFF_8436;
401290650Shselasky		eeprom->len = MLX5E_ETH_MODULE_SFF_8436_LEN;
402290650Shselasky		break;
403290650Shselasky	case SFF_8024_ID_QSFPPLUS:
404290650Shselasky	case SFF_8024_ID_QSFP28:
405290650Shselasky		if ((data & MLX5_EEPROM_IDENTIFIER_BYTE_MASK) == SFF_8024_ID_QSFP28 ||
406291070Shselasky		    ((data & MLX5_EEPROM_REVISION_ID_BYTE_MASK) >> 8) >= 0x3) {
407290650Shselasky			eeprom->type = MLX5E_ETH_MODULE_SFF_8636;
408290650Shselasky			eeprom->len = MLX5E_ETH_MODULE_SFF_8636_LEN;
409290650Shselasky		} else {
410290650Shselasky			eeprom->type = MLX5E_ETH_MODULE_SFF_8436;
411290650Shselasky			eeprom->len = MLX5E_ETH_MODULE_SFF_8436_LEN;
412290650Shselasky		}
413290650Shselasky		if ((data & MLX5_EEPROM_PAGE_3_VALID_BIT_MASK) == 0)
414290650Shselasky			eeprom->page_valid = 1;
415290650Shselasky		break;
416290650Shselasky	case SFF_8024_ID_SFP:
417290650Shselasky		eeprom->type = MLX5E_ETH_MODULE_SFF_8472;
418290650Shselasky		eeprom->len = MLX5E_ETH_MODULE_SFF_8472_LEN;
419290650Shselasky		break;
420290650Shselasky	default:
421291067Shselasky		if_printf(priv->ifp, "%s:%d: Not recognized cable type = 0x%x(%s)\n",
422291067Shselasky		    __func__, __LINE__, data & MLX5_EEPROM_IDENTIFIER_BYTE_MASK,
423291067Shselasky		    sff_8024_id[data & MLX5_EEPROM_IDENTIFIER_BYTE_MASK]);
424290650Shselasky		return (EINVAL);
425290650Shselasky	}
426290650Shselasky	return (0);
427290650Shselasky}
428290650Shselasky
429290650Shselasky/* Read both low and high pages of the eeprom */
430290650Shselaskystatic int
431290650Shselaskymlx5e_get_eeprom(struct mlx5e_priv *priv, struct mlx5e_eeprom *ee)
432290650Shselasky{
433290650Shselasky	struct mlx5_core_dev *dev = priv->mdev;
434290650Shselasky	int size_read = 0;
435290650Shselasky	int ret;
436290650Shselasky
437290650Shselasky	if (ee->len == 0)
438290650Shselasky		return (EINVAL);
439290650Shselasky
440290650Shselasky	/* Read low page of the eeprom */
441290650Shselasky	while (ee->device_addr < ee->len) {
442290650Shselasky		ret = mlx5_query_eeprom(dev, ee->i2c_addr, ee->page_num, ee->device_addr,
443290650Shselasky		    ee->len - ee->device_addr, ee->module_num,
444291070Shselasky		    ee->data + (ee->device_addr / 4), &size_read);
445290650Shselasky		if (ret) {
446290650Shselasky			if_printf(priv->ifp, "%s:%d: Failed reading eeprom, "
447290650Shselasky			    "error = 0x%02x\n", __func__, __LINE__, ret);
448290650Shselasky			return (ret);
449290650Shselasky		}
450290650Shselasky		ee->device_addr += size_read;
451290650Shselasky	}
452290650Shselasky
453290650Shselasky	/* Read high page of the eeprom */
454290650Shselasky	if (ee->page_valid) {
455290650Shselasky		ee->device_addr = MLX5E_EEPROM_HIGH_PAGE_OFFSET;
456290650Shselasky		ee->page_num = MLX5E_EEPROM_HIGH_PAGE;
457290650Shselasky		size_read = 0;
458290650Shselasky		while (ee->device_addr < MLX5E_EEPROM_PAGE_LENGTH) {
459290650Shselasky			ret = mlx5_query_eeprom(dev, ee->i2c_addr, ee->page_num,
460290650Shselasky			    ee->device_addr, MLX5E_EEPROM_PAGE_LENGTH - ee->device_addr,
461291070Shselasky			    ee->module_num, ee->data + (ee->len / 4) +
462291070Shselasky			    ((ee->device_addr - MLX5E_EEPROM_HIGH_PAGE_OFFSET) / 4),
463290650Shselasky			    &size_read);
464290650Shselasky			if (ret) {
465290650Shselasky				if_printf(priv->ifp, "%s:%d: Failed reading eeprom, "
466290650Shselasky				    "error = 0x%02x\n", __func__, __LINE__, ret);
467290650Shselasky				return (ret);
468290650Shselasky			}
469290650Shselasky			ee->device_addr += size_read;
470290650Shselasky		}
471290650Shselasky	}
472290650Shselasky	return (0);
473290650Shselasky}
474290650Shselasky
475290650Shselaskystatic void
476290650Shselaskymlx5e_print_eeprom(struct mlx5e_eeprom *eeprom)
477290650Shselasky{
478292835Shselasky	int row;
479292835Shselasky	int index_in_row;
480292835Shselasky	int byte_to_write = 0;
481292835Shselasky	int line_length = 16;
482290650Shselasky
483290650Shselasky	printf("\nOffset\t\tValues\n");
484292835Shselasky	printf("------\t\t------");
485292835Shselasky	while (byte_to_write < eeprom->len) {
486292835Shselasky		printf("\n0x%04X\t\t", byte_to_write);
487292835Shselasky		for (index_in_row = 0; index_in_row < line_length; index_in_row++) {
488292835Shselasky			printf("%02X ", ((u8 *)eeprom->data)[byte_to_write]);
489292835Shselasky			byte_to_write++;
490290650Shselasky		}
491290650Shselasky	}
492290650Shselasky
493290650Shselasky	if (eeprom->page_valid) {
494290650Shselasky		row = MLX5E_EEPROM_HIGH_PAGE_OFFSET;
495292835Shselasky		printf("\n\nUpper Page 0x03\n");
496290650Shselasky		printf("\nOffset\t\tValues\n");
497292835Shselasky		printf("------\t\t------");
498290650Shselasky		while (row < MLX5E_EEPROM_PAGE_LENGTH) {
499292835Shselasky			printf("\n0x%04X\t\t", row);
500292835Shselasky			for (index_in_row = 0; index_in_row < line_length; index_in_row++) {
501292835Shselasky				printf("%02X ", ((u8 *)eeprom->data)[byte_to_write]);
502292835Shselasky				byte_to_write++;
503290650Shselasky				row++;
504290650Shselasky			}
505290650Shselasky		}
506290650Shselasky	}
507290650Shselasky}
508290650Shselasky
509290650Shselasky/*
510290650Shselasky * Read cable EEPROM module information by first inspecting the first
511290650Shselasky * three bytes to get the initial information for a whole reading.
512290650Shselasky * Information will be printed to dmesg.
513290650Shselasky */
514290650Shselaskystatic int
515290650Shselaskymlx5e_read_eeprom(SYSCTL_HANDLER_ARGS)
516290650Shselasky{
517290650Shselasky	struct mlx5e_priv *priv = arg1;
518290650Shselasky	struct mlx5e_eeprom eeprom;
519290650Shselasky	int error;
520290650Shselasky	int result = 0;
521290650Shselasky
522290650Shselasky	PRIV_LOCK(priv);
523290650Shselasky	error = sysctl_handle_int(oidp, &result, 0, req);
524290650Shselasky	if (error || !req->newptr)
525290650Shselasky		goto done;
526290650Shselasky
527290650Shselasky	/* Check if device is gone */
528290650Shselasky	if (priv->gone) {
529290650Shselasky		error = ENXIO;
530290650Shselasky		goto done;
531290650Shselasky	}
532290650Shselasky
533290650Shselasky	if (result == 1) {
534290650Shselasky		eeprom.i2c_addr = MLX5E_I2C_ADDR_LOW;
535290650Shselasky		eeprom.device_addr = 0;
536290650Shselasky		eeprom.page_num = MLX5E_EEPROM_LOW_PAGE;
537290650Shselasky		eeprom.page_valid = 0;
538290650Shselasky
539290650Shselasky		/* Read three first bytes to get important info */
540290650Shselasky		error = mlx5e_get_eeprom_info(priv, &eeprom);
541290650Shselasky		if (error) {
542290650Shselasky			if_printf(priv->ifp, "%s:%d: Failed reading eeprom's "
543290650Shselasky			    "initial information\n", __func__, __LINE__);
544290650Shselasky			error = 0;
545290650Shselasky			goto done;
546290650Shselasky		}
547291070Shselasky		/*
548291070Shselasky		 * Allocate needed length buffer and additional space for
549291070Shselasky		 * page 0x03
550291070Shselasky		 */
551290650Shselasky		eeprom.data = malloc(eeprom.len + MLX5E_EEPROM_PAGE_LENGTH,
552290650Shselasky		    M_MLX5EN, M_WAITOK | M_ZERO);
553290650Shselasky
554290650Shselasky		/* Read the whole eeprom information */
555290650Shselasky		error = mlx5e_get_eeprom(priv, &eeprom);
556290650Shselasky		if (error) {
557290650Shselasky			if_printf(priv->ifp, "%s:%d: Failed reading eeprom\n",
558290650Shselasky			    __func__, __LINE__);
559290650Shselasky			error = 0;
560291070Shselasky			/*
561291070Shselasky			 * Continue printing partial information in case of
562291070Shselasky			 * an error
563291070Shselasky			 */
564290650Shselasky		}
565290650Shselasky		mlx5e_print_eeprom(&eeprom);
566290650Shselasky		free(eeprom.data, M_MLX5EN);
567290650Shselasky	}
568290650Shselaskydone:
569290650Shselasky	PRIV_UNLOCK(priv);
570290650Shselasky	return (error);
571290650Shselasky}
572290650Shselasky
573290650Shselaskystatic const char *mlx5e_params_desc[] = {
574290650Shselasky	MLX5E_PARAMS(MLX5E_STATS_DESC)
575290650Shselasky};
576290650Shselasky
577290650Shselaskystatic const char *mlx5e_port_stats_debug_desc[] = {
578290650Shselasky	MLX5E_PORT_STATS_DEBUG(MLX5E_STATS_DESC)
579290650Shselasky};
580290650Shselasky
581290650Shselaskystatic int
582290650Shselaskymlx5e_ethtool_debug_stats(SYSCTL_HANDLER_ARGS)
583290650Shselasky{
584290650Shselasky	struct mlx5e_priv *priv = arg1;
585290650Shselasky	int error;
586290650Shselasky	int sys_debug;
587290650Shselasky
588290650Shselasky	sys_debug = priv->sysctl_debug;
589290650Shselasky	error = sysctl_handle_int(oidp, &priv->sysctl_debug, 0, req);
590290650Shselasky	if (error || !req->newptr)
591290650Shselasky		return (error);
592290650Shselasky	priv->sysctl_debug = !!priv->sysctl_debug;
593290650Shselasky	if (sys_debug == priv->sysctl_debug)
594290650Shselasky		return (error);
595290650Shselasky	if (priv->sysctl_debug)
596290650Shselasky		mlx5e_create_stats(&priv->stats.port_stats_debug.ctx,
597290650Shselasky		    SYSCTL_CHILDREN(priv->sysctl_ifnet), "debug_stats",
598290650Shselasky		    mlx5e_port_stats_debug_desc, MLX5E_PORT_STATS_DEBUG_NUM,
599290650Shselasky		    priv->stats.port_stats_debug.arg);
600290650Shselasky	else
601290650Shselasky		sysctl_ctx_free(&priv->stats.port_stats_debug.ctx);
602290650Shselasky	return (error);
603290650Shselasky}
604290650Shselasky
605290650Shselaskyvoid
606290650Shselaskymlx5e_create_ethtool(struct mlx5e_priv *priv)
607290650Shselasky{
608290650Shselasky	struct sysctl_oid *node;
609290650Shselasky	const char *pnameunit;
610290650Shselasky	unsigned x;
611290650Shselasky
612290650Shselasky	/* set some defaults */
613290650Shselasky	priv->params_ethtool.tx_queue_size_max = 1 << MLX5E_PARAMS_MAXIMUM_LOG_SQ_SIZE;
614290650Shselasky	priv->params_ethtool.rx_queue_size_max = 1 << MLX5E_PARAMS_MAXIMUM_LOG_RQ_SIZE;
615290650Shselasky	priv->params_ethtool.tx_queue_size = 1 << priv->params.log_sq_size;
616290650Shselasky	priv->params_ethtool.rx_queue_size = 1 << priv->params.log_rq_size;
617290650Shselasky	priv->params_ethtool.channels = priv->params.num_channels;
618290650Shselasky	priv->params_ethtool.coalesce_pkts_max = MLX5E_FLD_MAX(cqc, cq_max_count);
619290650Shselasky	priv->params_ethtool.coalesce_usecs_max = MLX5E_FLD_MAX(cqc, cq_period);
620290650Shselasky	priv->params_ethtool.rx_coalesce_mode = priv->params.rx_cq_moderation_mode;
621290650Shselasky	priv->params_ethtool.rx_coalesce_usecs = priv->params.rx_cq_moderation_usec;
622290650Shselasky	priv->params_ethtool.rx_coalesce_pkts = priv->params.rx_cq_moderation_pkts;
623291932Shselasky	priv->params_ethtool.tx_coalesce_mode = priv->params.tx_cq_moderation_mode;
624290650Shselasky	priv->params_ethtool.tx_coalesce_usecs = priv->params.tx_cq_moderation_usec;
625290650Shselasky	priv->params_ethtool.tx_coalesce_pkts = priv->params.tx_cq_moderation_pkts;
626290650Shselasky	priv->params_ethtool.hw_lro = priv->params.hw_lro_en;
627292838Shselasky	priv->params_ethtool.cqe_zipping = priv->params.cqe_zipping_en;
628300277Shselasky	mlx5e_ethtool_sync_tx_completion_fact(priv);
629290650Shselasky
630290650Shselasky	/* create root node */
631290650Shselasky	node = SYSCTL_ADD_NODE(&priv->sysctl_ctx,
632290650Shselasky	    SYSCTL_CHILDREN(priv->sysctl_ifnet), OID_AUTO,
633290650Shselasky	    "conf", CTLFLAG_RW, NULL, "Configuration");
634290650Shselasky	if (node == NULL)
635290650Shselasky		return;
636290650Shselasky	for (x = 0; x != MLX5E_PARAMS_NUM; x++) {
637290650Shselasky		/* check for read-only parameter */
638290650Shselasky		if (strstr(mlx5e_params_desc[2 * x], "_max") != NULL) {
639290650Shselasky			SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO,
640290650Shselasky			    mlx5e_params_desc[2 * x], CTLTYPE_U64 | CTLFLAG_RD |
641290650Shselasky			    CTLFLAG_MPSAFE, priv, x, &mlx5e_ethtool_handler, "QU",
642290650Shselasky			    mlx5e_params_desc[2 * x + 1]);
643290650Shselasky		} else {
644292837Shselasky#if (__FreeBSD_version < 1100000)
645292837Shselasky			char path[64];
646292837Shselasky#endif
647292837Shselasky			/*
648292837Shselasky			 * NOTE: In FreeBSD-11 and newer the
649292837Shselasky			 * CTLFLAG_RWTUN flag will take care of
650292837Shselasky			 * loading default sysctl value from the
651292837Shselasky			 * kernel environment, if any:
652292837Shselasky			 */
653290650Shselasky			SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO,
654290650Shselasky			    mlx5e_params_desc[2 * x], CTLTYPE_U64 | CTLFLAG_RWTUN |
655290650Shselasky			    CTLFLAG_MPSAFE, priv, x, &mlx5e_ethtool_handler, "QU",
656290650Shselasky			    mlx5e_params_desc[2 * x + 1]);
657292837Shselasky
658292837Shselasky#if (__FreeBSD_version < 1100000)
659292837Shselasky			/* compute path for sysctl */
660292837Shselasky			snprintf(path, sizeof(path), "dev.mce.%d.conf.%s",
661292837Shselasky			    device_get_unit(priv->mdev->pdev->dev.bsddev),
662292837Shselasky			    mlx5e_params_desc[2 * x]);
663292837Shselasky
664292837Shselasky			/* try to fetch tunable, if any */
665292837Shselasky			if (TUNABLE_QUAD_FETCH(path, &priv->params_ethtool.arg[x]))
666292837Shselasky				mlx5e_ethtool_handler(NULL, priv, x, NULL);
667292837Shselasky#endif
668290650Shselasky		}
669290650Shselasky	}
670290650Shselasky
671290650Shselasky	SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO,
672290650Shselasky	    "debug_stats", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, priv,
673290650Shselasky	    0, &mlx5e_ethtool_debug_stats, "I", "Extended debug statistics");
674290650Shselasky
675290650Shselasky	pnameunit = device_get_nameunit(priv->mdev->pdev->dev.bsddev);
676290650Shselasky
677290650Shselasky	SYSCTL_ADD_STRING(&priv->sysctl_ctx, SYSCTL_CHILDREN(node),
678290650Shselasky	    OID_AUTO, "device_name", CTLFLAG_RD,
679290650Shselasky	    __DECONST(void *, pnameunit), 0,
680290650Shselasky	    "PCI device name");
681290650Shselasky
682290650Shselasky	/* EEPROM support */
683290650Shselasky	SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO, "eeprom_info",
684290650Shselasky	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, priv, 0,
685290650Shselasky	    mlx5e_read_eeprom, "I", "EEPROM information");
686290650Shselasky}
687