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