mlx5_en_ethtool.c revision 331568
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 331568 2018-03-26 19:59:00Z 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
355321999Shselasky	case MLX5_PARAM_OFFSET(tx_bufring_disable):
356321999Shselasky		/* rangecheck input value */
357321999Shselasky		priv->params_ethtool.tx_bufring_disable =
358321999Shselasky		    priv->params_ethtool.tx_bufring_disable ? 1 : 0;
359321999Shselasky
360321999Shselasky		/* reconfigure the sendqueues, if any */
361321999Shselasky		if (was_opened) {
362321999Shselasky			mlx5e_close_locked(priv->ifp);
363321999Shselasky			mlx5e_open_locked(priv->ifp);
364321999Shselasky		}
365321999Shselasky		break;
366321999Shselasky
367300282Shselasky	case MLX5_PARAM_OFFSET(tx_completion_fact):
368300282Shselasky		/* network interface must be down */
369300282Shselasky		if (was_opened)
370300282Shselasky			mlx5e_close_locked(priv->ifp);
371300282Shselasky
372300277Shselasky		/* verify parameter */
373300277Shselasky		mlx5e_ethtool_sync_tx_completion_fact(priv);
374300282Shselasky
375300282Shselasky		/* restart network interface, if any */
376300282Shselasky		if (was_opened)
377300282Shselasky			mlx5e_open_locked(priv->ifp);
378300282Shselasky		break;
379300282Shselasky
380331568Shselasky	case MLX5_PARAM_OFFSET(modify_tx_dma):
381331568Shselasky		/* check if network interface is opened */
382331568Shselasky		if (was_opened) {
383331568Shselasky			priv->params_ethtool.modify_tx_dma =
384331568Shselasky			    priv->params_ethtool.modify_tx_dma ? 1 : 0;
385331568Shselasky			/* modify tx according to value */
386331568Shselasky			mlx5e_modify_tx_dma(priv, value != 0);
387331568Shselasky		} else {
388331568Shselasky			/* if closed force enable tx */
389331568Shselasky			priv->params_ethtool.modify_tx_dma = 0;
390331568Shselasky		}
391331568Shselasky		break;
392331568Shselasky
393331568Shselasky	case MLX5_PARAM_OFFSET(modify_rx_dma):
394331568Shselasky		/* check if network interface is opened */
395331568Shselasky		if (was_opened) {
396331568Shselasky			priv->params_ethtool.modify_rx_dma =
397331568Shselasky			    priv->params_ethtool.modify_rx_dma ? 1 : 0;
398331568Shselasky			/* modify rx according to value */
399331568Shselasky			mlx5e_modify_rx_dma(priv, value != 0);
400331568Shselasky		} else {
401331568Shselasky			/* if closed force enable rx */
402331568Shselasky			priv->params_ethtool.modify_rx_dma = 0;
403331568Shselasky		}
404331568Shselasky		break;
405331568Shselasky
406322006Shselasky	case MLX5_PARAM_OFFSET(diag_pci_enable):
407322006Shselasky		priv->params_ethtool.diag_pci_enable =
408322006Shselasky		    priv->params_ethtool.diag_pci_enable ? 1 : 0;
409322006Shselasky
410322006Shselasky		error = -mlx5_core_set_diagnostics_full(priv->mdev,
411322006Shselasky		    priv->params_ethtool.diag_pci_enable,
412322006Shselasky		    priv->params_ethtool.diag_general_enable);
413322006Shselasky		break;
414322006Shselasky
415322006Shselasky	case MLX5_PARAM_OFFSET(diag_general_enable):
416322006Shselasky		priv->params_ethtool.diag_general_enable =
417322006Shselasky		    priv->params_ethtool.diag_general_enable ? 1 : 0;
418322006Shselasky
419322006Shselasky		error = -mlx5_core_set_diagnostics_full(priv->mdev,
420322006Shselasky		    priv->params_ethtool.diag_pci_enable,
421322006Shselasky		    priv->params_ethtool.diag_general_enable);
422322006Shselasky		break;
423322006Shselasky
424300282Shselasky	default:
425300282Shselasky		break;
426300277Shselasky	}
427290650Shselaskydone:
428290650Shselasky	PRIV_UNLOCK(priv);
429290650Shselasky	return (error);
430290650Shselasky}
431290650Shselasky
432290650Shselasky/*
433290650Shselasky * Read the first three bytes of the eeprom in order to get the needed info
434290650Shselasky * for the whole reading.
435290650Shselasky * Byte 0 - Identifier byte
436290650Shselasky * Byte 1 - Revision byte
437290650Shselasky * Byte 2 - Status byte
438290650Shselasky */
439290650Shselaskystatic int
440290650Shselaskymlx5e_get_eeprom_info(struct mlx5e_priv *priv, struct mlx5e_eeprom *eeprom)
441290650Shselasky{
442290650Shselasky	struct mlx5_core_dev *dev = priv->mdev;
443290650Shselasky	u32 data = 0;
444290650Shselasky	int size_read = 0;
445290650Shselasky	int ret;
446290650Shselasky
447290650Shselasky	ret = mlx5_query_module_num(dev, &eeprom->module_num);
448290650Shselasky	if (ret) {
449290650Shselasky		if_printf(priv->ifp, "%s:%d: Failed query module error=%d\n",
450290650Shselasky		    __func__, __LINE__, ret);
451290650Shselasky		return (ret);
452290650Shselasky	}
453290650Shselasky
454290650Shselasky	/* Read the first three bytes to get Identifier, Revision and Status */
455290650Shselasky	ret = mlx5_query_eeprom(dev, eeprom->i2c_addr, eeprom->page_num,
456290650Shselasky	    eeprom->device_addr, MLX5E_EEPROM_INFO_BYTES, eeprom->module_num, &data,
457290650Shselasky	    &size_read);
458290650Shselasky	if (ret) {
459290650Shselasky		if_printf(priv->ifp, "%s:%d: Failed query eeprom module error=0x%x\n",
460290650Shselasky		    __func__, __LINE__, ret);
461290650Shselasky		return (ret);
462290650Shselasky	}
463290650Shselasky
464290650Shselasky	switch (data & MLX5_EEPROM_IDENTIFIER_BYTE_MASK) {
465290650Shselasky	case SFF_8024_ID_QSFP:
466290650Shselasky		eeprom->type = MLX5E_ETH_MODULE_SFF_8436;
467290650Shselasky		eeprom->len = MLX5E_ETH_MODULE_SFF_8436_LEN;
468290650Shselasky		break;
469290650Shselasky	case SFF_8024_ID_QSFPPLUS:
470290650Shselasky	case SFF_8024_ID_QSFP28:
471290650Shselasky		if ((data & MLX5_EEPROM_IDENTIFIER_BYTE_MASK) == SFF_8024_ID_QSFP28 ||
472291070Shselasky		    ((data & MLX5_EEPROM_REVISION_ID_BYTE_MASK) >> 8) >= 0x3) {
473290650Shselasky			eeprom->type = MLX5E_ETH_MODULE_SFF_8636;
474290650Shselasky			eeprom->len = MLX5E_ETH_MODULE_SFF_8636_LEN;
475290650Shselasky		} else {
476290650Shselasky			eeprom->type = MLX5E_ETH_MODULE_SFF_8436;
477290650Shselasky			eeprom->len = MLX5E_ETH_MODULE_SFF_8436_LEN;
478290650Shselasky		}
479290650Shselasky		if ((data & MLX5_EEPROM_PAGE_3_VALID_BIT_MASK) == 0)
480290650Shselasky			eeprom->page_valid = 1;
481290650Shselasky		break;
482290650Shselasky	case SFF_8024_ID_SFP:
483290650Shselasky		eeprom->type = MLX5E_ETH_MODULE_SFF_8472;
484290650Shselasky		eeprom->len = MLX5E_ETH_MODULE_SFF_8472_LEN;
485290650Shselasky		break;
486290650Shselasky	default:
487291067Shselasky		if_printf(priv->ifp, "%s:%d: Not recognized cable type = 0x%x(%s)\n",
488291067Shselasky		    __func__, __LINE__, data & MLX5_EEPROM_IDENTIFIER_BYTE_MASK,
489291067Shselasky		    sff_8024_id[data & MLX5_EEPROM_IDENTIFIER_BYTE_MASK]);
490290650Shselasky		return (EINVAL);
491290650Shselasky	}
492290650Shselasky	return (0);
493290650Shselasky}
494290650Shselasky
495290650Shselasky/* Read both low and high pages of the eeprom */
496290650Shselaskystatic int
497290650Shselaskymlx5e_get_eeprom(struct mlx5e_priv *priv, struct mlx5e_eeprom *ee)
498290650Shselasky{
499290650Shselasky	struct mlx5_core_dev *dev = priv->mdev;
500290650Shselasky	int size_read = 0;
501290650Shselasky	int ret;
502290650Shselasky
503290650Shselasky	if (ee->len == 0)
504290650Shselasky		return (EINVAL);
505290650Shselasky
506290650Shselasky	/* Read low page of the eeprom */
507290650Shselasky	while (ee->device_addr < ee->len) {
508290650Shselasky		ret = mlx5_query_eeprom(dev, ee->i2c_addr, ee->page_num, ee->device_addr,
509290650Shselasky		    ee->len - ee->device_addr, ee->module_num,
510291070Shselasky		    ee->data + (ee->device_addr / 4), &size_read);
511290650Shselasky		if (ret) {
512290650Shselasky			if_printf(priv->ifp, "%s:%d: Failed reading eeprom, "
513290650Shselasky			    "error = 0x%02x\n", __func__, __LINE__, ret);
514290650Shselasky			return (ret);
515290650Shselasky		}
516290650Shselasky		ee->device_addr += size_read;
517290650Shselasky	}
518290650Shselasky
519290650Shselasky	/* Read high page of the eeprom */
520290650Shselasky	if (ee->page_valid) {
521290650Shselasky		ee->device_addr = MLX5E_EEPROM_HIGH_PAGE_OFFSET;
522290650Shselasky		ee->page_num = MLX5E_EEPROM_HIGH_PAGE;
523290650Shselasky		size_read = 0;
524290650Shselasky		while (ee->device_addr < MLX5E_EEPROM_PAGE_LENGTH) {
525290650Shselasky			ret = mlx5_query_eeprom(dev, ee->i2c_addr, ee->page_num,
526290650Shselasky			    ee->device_addr, MLX5E_EEPROM_PAGE_LENGTH - ee->device_addr,
527291070Shselasky			    ee->module_num, ee->data + (ee->len / 4) +
528291070Shselasky			    ((ee->device_addr - MLX5E_EEPROM_HIGH_PAGE_OFFSET) / 4),
529290650Shselasky			    &size_read);
530290650Shselasky			if (ret) {
531290650Shselasky				if_printf(priv->ifp, "%s:%d: Failed reading eeprom, "
532290650Shselasky				    "error = 0x%02x\n", __func__, __LINE__, ret);
533290650Shselasky				return (ret);
534290650Shselasky			}
535290650Shselasky			ee->device_addr += size_read;
536290650Shselasky		}
537290650Shselasky	}
538290650Shselasky	return (0);
539290650Shselasky}
540290650Shselasky
541290650Shselaskystatic void
542290650Shselaskymlx5e_print_eeprom(struct mlx5e_eeprom *eeprom)
543290650Shselasky{
544292835Shselasky	int row;
545292835Shselasky	int index_in_row;
546292835Shselasky	int byte_to_write = 0;
547292835Shselasky	int line_length = 16;
548290650Shselasky
549290650Shselasky	printf("\nOffset\t\tValues\n");
550292835Shselasky	printf("------\t\t------");
551292835Shselasky	while (byte_to_write < eeprom->len) {
552292835Shselasky		printf("\n0x%04X\t\t", byte_to_write);
553292835Shselasky		for (index_in_row = 0; index_in_row < line_length; index_in_row++) {
554292835Shselasky			printf("%02X ", ((u8 *)eeprom->data)[byte_to_write]);
555292835Shselasky			byte_to_write++;
556290650Shselasky		}
557290650Shselasky	}
558290650Shselasky
559290650Shselasky	if (eeprom->page_valid) {
560290650Shselasky		row = MLX5E_EEPROM_HIGH_PAGE_OFFSET;
561292835Shselasky		printf("\n\nUpper Page 0x03\n");
562290650Shselasky		printf("\nOffset\t\tValues\n");
563292835Shselasky		printf("------\t\t------");
564290650Shselasky		while (row < MLX5E_EEPROM_PAGE_LENGTH) {
565292835Shselasky			printf("\n0x%04X\t\t", row);
566292835Shselasky			for (index_in_row = 0; index_in_row < line_length; index_in_row++) {
567292835Shselasky				printf("%02X ", ((u8 *)eeprom->data)[byte_to_write]);
568292835Shselasky				byte_to_write++;
569290650Shselasky				row++;
570290650Shselasky			}
571290650Shselasky		}
572290650Shselasky	}
573290650Shselasky}
574290650Shselasky
575290650Shselasky/*
576290650Shselasky * Read cable EEPROM module information by first inspecting the first
577290650Shselasky * three bytes to get the initial information for a whole reading.
578290650Shselasky * Information will be printed to dmesg.
579290650Shselasky */
580290650Shselaskystatic int
581290650Shselaskymlx5e_read_eeprom(SYSCTL_HANDLER_ARGS)
582290650Shselasky{
583290650Shselasky	struct mlx5e_priv *priv = arg1;
584290650Shselasky	struct mlx5e_eeprom eeprom;
585290650Shselasky	int error;
586290650Shselasky	int result = 0;
587290650Shselasky
588290650Shselasky	PRIV_LOCK(priv);
589290650Shselasky	error = sysctl_handle_int(oidp, &result, 0, req);
590290650Shselasky	if (error || !req->newptr)
591290650Shselasky		goto done;
592290650Shselasky
593290650Shselasky	/* Check if device is gone */
594290650Shselasky	if (priv->gone) {
595290650Shselasky		error = ENXIO;
596290650Shselasky		goto done;
597290650Shselasky	}
598290650Shselasky
599290650Shselasky	if (result == 1) {
600290650Shselasky		eeprom.i2c_addr = MLX5E_I2C_ADDR_LOW;
601290650Shselasky		eeprom.device_addr = 0;
602290650Shselasky		eeprom.page_num = MLX5E_EEPROM_LOW_PAGE;
603290650Shselasky		eeprom.page_valid = 0;
604290650Shselasky
605290650Shselasky		/* Read three first bytes to get important info */
606290650Shselasky		error = mlx5e_get_eeprom_info(priv, &eeprom);
607290650Shselasky		if (error) {
608290650Shselasky			if_printf(priv->ifp, "%s:%d: Failed reading eeprom's "
609290650Shselasky			    "initial information\n", __func__, __LINE__);
610290650Shselasky			error = 0;
611290650Shselasky			goto done;
612290650Shselasky		}
613291070Shselasky		/*
614291070Shselasky		 * Allocate needed length buffer and additional space for
615291070Shselasky		 * page 0x03
616291070Shselasky		 */
617290650Shselasky		eeprom.data = malloc(eeprom.len + MLX5E_EEPROM_PAGE_LENGTH,
618290650Shselasky		    M_MLX5EN, M_WAITOK | M_ZERO);
619290650Shselasky
620290650Shselasky		/* Read the whole eeprom information */
621290650Shselasky		error = mlx5e_get_eeprom(priv, &eeprom);
622290650Shselasky		if (error) {
623290650Shselasky			if_printf(priv->ifp, "%s:%d: Failed reading eeprom\n",
624290650Shselasky			    __func__, __LINE__);
625290650Shselasky			error = 0;
626291070Shselasky			/*
627291070Shselasky			 * Continue printing partial information in case of
628291070Shselasky			 * an error
629291070Shselasky			 */
630290650Shselasky		}
631290650Shselasky		mlx5e_print_eeprom(&eeprom);
632290650Shselasky		free(eeprom.data, M_MLX5EN);
633290650Shselasky	}
634290650Shselaskydone:
635290650Shselasky	PRIV_UNLOCK(priv);
636290650Shselasky	return (error);
637290650Shselasky}
638290650Shselasky
639290650Shselaskystatic const char *mlx5e_params_desc[] = {
640290650Shselasky	MLX5E_PARAMS(MLX5E_STATS_DESC)
641290650Shselasky};
642290650Shselasky
643290650Shselaskystatic const char *mlx5e_port_stats_debug_desc[] = {
644290650Shselasky	MLX5E_PORT_STATS_DEBUG(MLX5E_STATS_DESC)
645290650Shselasky};
646290650Shselasky
647290650Shselaskystatic int
648290650Shselaskymlx5e_ethtool_debug_stats(SYSCTL_HANDLER_ARGS)
649290650Shselasky{
650290650Shselasky	struct mlx5e_priv *priv = arg1;
651290650Shselasky	int error;
652290650Shselasky	int sys_debug;
653290650Shselasky
654290650Shselasky	sys_debug = priv->sysctl_debug;
655290650Shselasky	error = sysctl_handle_int(oidp, &priv->sysctl_debug, 0, req);
656290650Shselasky	if (error || !req->newptr)
657290650Shselasky		return (error);
658290650Shselasky	priv->sysctl_debug = !!priv->sysctl_debug;
659290650Shselasky	if (sys_debug == priv->sysctl_debug)
660290650Shselasky		return (error);
661290650Shselasky	if (priv->sysctl_debug)
662290650Shselasky		mlx5e_create_stats(&priv->stats.port_stats_debug.ctx,
663290650Shselasky		    SYSCTL_CHILDREN(priv->sysctl_ifnet), "debug_stats",
664290650Shselasky		    mlx5e_port_stats_debug_desc, MLX5E_PORT_STATS_DEBUG_NUM,
665290650Shselasky		    priv->stats.port_stats_debug.arg);
666290650Shselasky	else
667290650Shselasky		sysctl_ctx_free(&priv->stats.port_stats_debug.ctx);
668290650Shselasky	return (error);
669290650Shselasky}
670290650Shselasky
671322006Shselaskystatic void
672322006Shselaskymlx5e_create_diagnostics(struct mlx5e_priv *priv)
673322006Shselasky{
674322006Shselasky	struct mlx5_core_diagnostics_entry entry;
675322006Shselasky	struct sysctl_ctx_list *ctx;
676322006Shselasky	struct sysctl_oid *node;
677322006Shselasky	int x;
678322006Shselasky
679322006Shselasky	/* sysctl context we are using */
680322006Shselasky	ctx = &priv->sysctl_ctx;
681322006Shselasky
682322006Shselasky	/* create root node */
683322006Shselasky	node = SYSCTL_ADD_NODE(ctx,
684322006Shselasky	    SYSCTL_CHILDREN(priv->sysctl_ifnet), OID_AUTO,
685322006Shselasky	    "diagnostics", CTLFLAG_RD, NULL, "Diagnostics");
686322006Shselasky	if (node == NULL)
687322006Shselasky		return;
688322006Shselasky
689322006Shselasky	/* create PCI diagnostics */
690322006Shselasky	for (x = 0; x != MLX5_CORE_PCI_DIAGNOSTICS_NUM; x++) {
691322006Shselasky		entry = mlx5_core_pci_diagnostics_table[x];
692322006Shselasky		if (mlx5_core_supports_diagnostics(priv->mdev, entry.counter_id) == 0)
693322006Shselasky			continue;
694322006Shselasky		SYSCTL_ADD_UQUAD(ctx, SYSCTL_CHILDREN(node), OID_AUTO,
695322006Shselasky		    entry.desc, CTLFLAG_RD, priv->params_pci.array + x,
696322006Shselasky		    "PCI diagnostics counter");
697322006Shselasky	}
698322006Shselasky
699322006Shselasky	/* create general diagnostics */
700322006Shselasky	for (x = 0; x != MLX5_CORE_GENERAL_DIAGNOSTICS_NUM; x++) {
701322006Shselasky		entry = mlx5_core_general_diagnostics_table[x];
702322006Shselasky		if (mlx5_core_supports_diagnostics(priv->mdev, entry.counter_id) == 0)
703322006Shselasky			continue;
704322006Shselasky		SYSCTL_ADD_UQUAD(ctx, SYSCTL_CHILDREN(node), OID_AUTO,
705322006Shselasky		    entry.desc, CTLFLAG_RD, priv->params_general.array + x,
706322006Shselasky		    "General diagnostics counter");
707322006Shselasky	}
708322006Shselasky}
709322006Shselasky
710290650Shselaskyvoid
711290650Shselaskymlx5e_create_ethtool(struct mlx5e_priv *priv)
712290650Shselasky{
713290650Shselasky	struct sysctl_oid *node;
714290650Shselasky	const char *pnameunit;
715290650Shselasky	unsigned x;
716290650Shselasky
717290650Shselasky	/* set some defaults */
718290650Shselasky	priv->params_ethtool.tx_queue_size_max = 1 << MLX5E_PARAMS_MAXIMUM_LOG_SQ_SIZE;
719290650Shselasky	priv->params_ethtool.rx_queue_size_max = 1 << MLX5E_PARAMS_MAXIMUM_LOG_RQ_SIZE;
720290650Shselasky	priv->params_ethtool.tx_queue_size = 1 << priv->params.log_sq_size;
721290650Shselasky	priv->params_ethtool.rx_queue_size = 1 << priv->params.log_rq_size;
722290650Shselasky	priv->params_ethtool.channels = priv->params.num_channels;
723290650Shselasky	priv->params_ethtool.coalesce_pkts_max = MLX5E_FLD_MAX(cqc, cq_max_count);
724290650Shselasky	priv->params_ethtool.coalesce_usecs_max = MLX5E_FLD_MAX(cqc, cq_period);
725290650Shselasky	priv->params_ethtool.rx_coalesce_mode = priv->params.rx_cq_moderation_mode;
726290650Shselasky	priv->params_ethtool.rx_coalesce_usecs = priv->params.rx_cq_moderation_usec;
727290650Shselasky	priv->params_ethtool.rx_coalesce_pkts = priv->params.rx_cq_moderation_pkts;
728291932Shselasky	priv->params_ethtool.tx_coalesce_mode = priv->params.tx_cq_moderation_mode;
729290650Shselasky	priv->params_ethtool.tx_coalesce_usecs = priv->params.tx_cq_moderation_usec;
730290650Shselasky	priv->params_ethtool.tx_coalesce_pkts = priv->params.tx_cq_moderation_pkts;
731290650Shselasky	priv->params_ethtool.hw_lro = priv->params.hw_lro_en;
732292838Shselasky	priv->params_ethtool.cqe_zipping = priv->params.cqe_zipping_en;
733300277Shselasky	mlx5e_ethtool_sync_tx_completion_fact(priv);
734290650Shselasky
735290650Shselasky	/* create root node */
736290650Shselasky	node = SYSCTL_ADD_NODE(&priv->sysctl_ctx,
737290650Shselasky	    SYSCTL_CHILDREN(priv->sysctl_ifnet), OID_AUTO,
738290650Shselasky	    "conf", CTLFLAG_RW, NULL, "Configuration");
739290650Shselasky	if (node == NULL)
740290650Shselasky		return;
741290650Shselasky	for (x = 0; x != MLX5E_PARAMS_NUM; x++) {
742290650Shselasky		/* check for read-only parameter */
743290650Shselasky		if (strstr(mlx5e_params_desc[2 * x], "_max") != NULL) {
744290650Shselasky			SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO,
745290650Shselasky			    mlx5e_params_desc[2 * x], CTLTYPE_U64 | CTLFLAG_RD |
746290650Shselasky			    CTLFLAG_MPSAFE, priv, x, &mlx5e_ethtool_handler, "QU",
747290650Shselasky			    mlx5e_params_desc[2 * x + 1]);
748290650Shselasky		} else {
749292837Shselasky#if (__FreeBSD_version < 1100000)
750292837Shselasky			char path[64];
751292837Shselasky#endif
752292837Shselasky			/*
753292837Shselasky			 * NOTE: In FreeBSD-11 and newer the
754292837Shselasky			 * CTLFLAG_RWTUN flag will take care of
755292837Shselasky			 * loading default sysctl value from the
756292837Shselasky			 * kernel environment, if any:
757292837Shselasky			 */
758290650Shselasky			SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO,
759290650Shselasky			    mlx5e_params_desc[2 * x], CTLTYPE_U64 | CTLFLAG_RWTUN |
760290650Shselasky			    CTLFLAG_MPSAFE, priv, x, &mlx5e_ethtool_handler, "QU",
761290650Shselasky			    mlx5e_params_desc[2 * x + 1]);
762292837Shselasky
763292837Shselasky#if (__FreeBSD_version < 1100000)
764292837Shselasky			/* compute path for sysctl */
765292837Shselasky			snprintf(path, sizeof(path), "dev.mce.%d.conf.%s",
766292837Shselasky			    device_get_unit(priv->mdev->pdev->dev.bsddev),
767292837Shselasky			    mlx5e_params_desc[2 * x]);
768292837Shselasky
769292837Shselasky			/* try to fetch tunable, if any */
770292837Shselasky			if (TUNABLE_QUAD_FETCH(path, &priv->params_ethtool.arg[x]))
771292837Shselasky				mlx5e_ethtool_handler(NULL, priv, x, NULL);
772292837Shselasky#endif
773290650Shselasky		}
774290650Shselasky	}
775290650Shselasky
776290650Shselasky	SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO,
777290650Shselasky	    "debug_stats", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, priv,
778290650Shselasky	    0, &mlx5e_ethtool_debug_stats, "I", "Extended debug statistics");
779290650Shselasky
780290650Shselasky	pnameunit = device_get_nameunit(priv->mdev->pdev->dev.bsddev);
781290650Shselasky
782290650Shselasky	SYSCTL_ADD_STRING(&priv->sysctl_ctx, SYSCTL_CHILDREN(node),
783290650Shselasky	    OID_AUTO, "device_name", CTLFLAG_RD,
784290650Shselasky	    __DECONST(void *, pnameunit), 0,
785290650Shselasky	    "PCI device name");
786290650Shselasky
787290650Shselasky	/* EEPROM support */
788290650Shselasky	SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO, "eeprom_info",
789290650Shselasky	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, priv, 0,
790290650Shselasky	    mlx5e_read_eeprom, "I", "EEPROM information");
791322006Shselasky
792322006Shselasky	/* Diagnostics support */
793322006Shselasky	mlx5e_create_diagnostics(priv);
794290650Shselasky}
795