GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / net / ethernet / amazon / ena / ena_ethtool.c
1 /*
2  * Copyright 2015 Amazon.com, Inc. or its affiliates.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <linux/pci.h>
34
35 #include "ena_netdev.h"
36
37 struct ena_stats {
38         char name[ETH_GSTRING_LEN];
39         int stat_offset;
40 };
41
42 #define ENA_STAT_ENA_COM_ENTRY(stat) { \
43         .name = #stat, \
44         .stat_offset = offsetof(struct ena_com_stats_admin, stat) \
45 }
46
47 #define ENA_STAT_ENTRY(stat, stat_type) { \
48         .name = #stat, \
49         .stat_offset = offsetof(struct ena_stats_##stat_type, stat) \
50 }
51
52 #define ENA_STAT_RX_ENTRY(stat) \
53         ENA_STAT_ENTRY(stat, rx)
54
55 #define ENA_STAT_TX_ENTRY(stat) \
56         ENA_STAT_ENTRY(stat, tx)
57
58 #define ENA_STAT_GLOBAL_ENTRY(stat) \
59         ENA_STAT_ENTRY(stat, dev)
60
61 static const struct ena_stats ena_stats_global_strings[] = {
62         ENA_STAT_GLOBAL_ENTRY(tx_timeout),
63         ENA_STAT_GLOBAL_ENTRY(io_suspend),
64         ENA_STAT_GLOBAL_ENTRY(io_resume),
65         ENA_STAT_GLOBAL_ENTRY(wd_expired),
66         ENA_STAT_GLOBAL_ENTRY(interface_up),
67         ENA_STAT_GLOBAL_ENTRY(interface_down),
68         ENA_STAT_GLOBAL_ENTRY(admin_q_pause),
69 };
70
71 static const struct ena_stats ena_stats_tx_strings[] = {
72         ENA_STAT_TX_ENTRY(cnt),
73         ENA_STAT_TX_ENTRY(bytes),
74         ENA_STAT_TX_ENTRY(queue_stop),
75         ENA_STAT_TX_ENTRY(queue_wakeup),
76         ENA_STAT_TX_ENTRY(dma_mapping_err),
77         ENA_STAT_TX_ENTRY(linearize),
78         ENA_STAT_TX_ENTRY(linearize_failed),
79         ENA_STAT_TX_ENTRY(napi_comp),
80         ENA_STAT_TX_ENTRY(tx_poll),
81         ENA_STAT_TX_ENTRY(doorbells),
82         ENA_STAT_TX_ENTRY(prepare_ctx_err),
83         ENA_STAT_TX_ENTRY(bad_req_id),
84 };
85
86 static const struct ena_stats ena_stats_rx_strings[] = {
87         ENA_STAT_RX_ENTRY(cnt),
88         ENA_STAT_RX_ENTRY(bytes),
89         ENA_STAT_RX_ENTRY(refil_partial),
90         ENA_STAT_RX_ENTRY(bad_csum),
91         ENA_STAT_RX_ENTRY(page_alloc_fail),
92         ENA_STAT_RX_ENTRY(skb_alloc_fail),
93         ENA_STAT_RX_ENTRY(dma_mapping_err),
94         ENA_STAT_RX_ENTRY(bad_desc_num),
95         ENA_STAT_RX_ENTRY(rx_copybreak_pkt),
96         ENA_STAT_RX_ENTRY(bad_req_id),
97         ENA_STAT_RX_ENTRY(empty_rx_ring),
98 };
99
100 static const struct ena_stats ena_stats_ena_com_strings[] = {
101         ENA_STAT_ENA_COM_ENTRY(aborted_cmd),
102         ENA_STAT_ENA_COM_ENTRY(submitted_cmd),
103         ENA_STAT_ENA_COM_ENTRY(completed_cmd),
104         ENA_STAT_ENA_COM_ENTRY(out_of_space),
105         ENA_STAT_ENA_COM_ENTRY(no_completion),
106 };
107
108 #define ENA_STATS_ARRAY_GLOBAL  ARRAY_SIZE(ena_stats_global_strings)
109 #define ENA_STATS_ARRAY_TX      ARRAY_SIZE(ena_stats_tx_strings)
110 #define ENA_STATS_ARRAY_RX      ARRAY_SIZE(ena_stats_rx_strings)
111 #define ENA_STATS_ARRAY_ENA_COM ARRAY_SIZE(ena_stats_ena_com_strings)
112
113 static void ena_safe_update_stat(u64 *src, u64 *dst,
114                                  struct u64_stats_sync *syncp)
115 {
116         unsigned int start;
117
118         do {
119                 start = u64_stats_fetch_begin_irq(syncp);
120                 *(dst) = *src;
121         } while (u64_stats_fetch_retry_irq(syncp, start));
122 }
123
124 static void ena_queue_stats(struct ena_adapter *adapter, u64 **data)
125 {
126         const struct ena_stats *ena_stats;
127         struct ena_ring *ring;
128
129         u64 *ptr;
130         int i, j;
131
132         for (i = 0; i < adapter->num_queues; i++) {
133                 /* Tx stats */
134                 ring = &adapter->tx_ring[i];
135
136                 for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
137                         ena_stats = &ena_stats_tx_strings[j];
138
139                         ptr = (u64 *)((uintptr_t)&ring->tx_stats +
140                                 (uintptr_t)ena_stats->stat_offset);
141
142                         ena_safe_update_stat(ptr, (*data)++, &ring->syncp);
143                 }
144
145                 /* Rx stats */
146                 ring = &adapter->rx_ring[i];
147
148                 for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
149                         ena_stats = &ena_stats_rx_strings[j];
150
151                         ptr = (u64 *)((uintptr_t)&ring->rx_stats +
152                                 (uintptr_t)ena_stats->stat_offset);
153
154                         ena_safe_update_stat(ptr, (*data)++, &ring->syncp);
155                 }
156         }
157 }
158
159 static void ena_dev_admin_queue_stats(struct ena_adapter *adapter, u64 **data)
160 {
161         const struct ena_stats *ena_stats;
162         u32 *ptr;
163         int i;
164
165         for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
166                 ena_stats = &ena_stats_ena_com_strings[i];
167
168                 ptr = (u32 *)((uintptr_t)&adapter->ena_dev->admin_queue.stats +
169                         (uintptr_t)ena_stats->stat_offset);
170
171                 *(*data)++ = *ptr;
172         }
173 }
174
175 static void ena_get_ethtool_stats(struct net_device *netdev,
176                                   struct ethtool_stats *stats,
177                                   u64 *data)
178 {
179         struct ena_adapter *adapter = netdev_priv(netdev);
180         const struct ena_stats *ena_stats;
181         u64 *ptr;
182         int i;
183
184         for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
185                 ena_stats = &ena_stats_global_strings[i];
186
187                 ptr = (u64 *)((uintptr_t)&adapter->dev_stats +
188                         (uintptr_t)ena_stats->stat_offset);
189
190                 ena_safe_update_stat(ptr, data++, &adapter->syncp);
191         }
192
193         ena_queue_stats(adapter, &data);
194         ena_dev_admin_queue_stats(adapter, &data);
195 }
196
197 int ena_get_sset_count(struct net_device *netdev, int sset)
198 {
199         struct ena_adapter *adapter = netdev_priv(netdev);
200
201         if (sset != ETH_SS_STATS)
202                 return -EOPNOTSUPP;
203
204         return  adapter->num_queues * (ENA_STATS_ARRAY_TX + ENA_STATS_ARRAY_RX)
205                 + ENA_STATS_ARRAY_GLOBAL + ENA_STATS_ARRAY_ENA_COM;
206 }
207
208 static void ena_queue_strings(struct ena_adapter *adapter, u8 **data)
209 {
210         const struct ena_stats *ena_stats;
211         int i, j;
212
213         for (i = 0; i < adapter->num_queues; i++) {
214                 /* Tx stats */
215                 for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
216                         ena_stats = &ena_stats_tx_strings[j];
217
218                         snprintf(*data, ETH_GSTRING_LEN,
219                                  "queue_%u_tx_%s", i, ena_stats->name);
220                          (*data) += ETH_GSTRING_LEN;
221                 }
222                 /* Rx stats */
223                 for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
224                         ena_stats = &ena_stats_rx_strings[j];
225
226                         snprintf(*data, ETH_GSTRING_LEN,
227                                  "queue_%u_rx_%s", i, ena_stats->name);
228                         (*data) += ETH_GSTRING_LEN;
229                 }
230         }
231 }
232
233 static void ena_com_dev_strings(u8 **data)
234 {
235         const struct ena_stats *ena_stats;
236         int i;
237
238         for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
239                 ena_stats = &ena_stats_ena_com_strings[i];
240
241                 snprintf(*data, ETH_GSTRING_LEN,
242                          "ena_admin_q_%s", ena_stats->name);
243                 (*data) += ETH_GSTRING_LEN;
244         }
245 }
246
247 static void ena_get_strings(struct net_device *netdev, u32 sset, u8 *data)
248 {
249         struct ena_adapter *adapter = netdev_priv(netdev);
250         const struct ena_stats *ena_stats;
251         int i;
252
253         if (sset != ETH_SS_STATS)
254                 return;
255
256         for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
257                 ena_stats = &ena_stats_global_strings[i];
258
259                 memcpy(data, ena_stats->name, ETH_GSTRING_LEN);
260                 data += ETH_GSTRING_LEN;
261         }
262
263         ena_queue_strings(adapter, &data);
264         ena_com_dev_strings(&data);
265 }
266
267 static int ena_get_link_ksettings(struct net_device *netdev,
268                                   struct ethtool_link_ksettings *link_ksettings)
269 {
270         struct ena_adapter *adapter = netdev_priv(netdev);
271         struct ena_com_dev *ena_dev = adapter->ena_dev;
272         struct ena_admin_get_feature_link_desc *link;
273         struct ena_admin_get_feat_resp feat_resp;
274         int rc;
275
276         rc = ena_com_get_link_params(ena_dev, &feat_resp);
277         if (rc)
278                 return rc;
279
280         link = &feat_resp.u.link;
281         link_ksettings->base.speed = link->speed;
282
283         if (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) {
284                 ethtool_link_ksettings_add_link_mode(link_ksettings,
285                                                      supported, Autoneg);
286                 ethtool_link_ksettings_add_link_mode(link_ksettings,
287                                                      supported, Autoneg);
288         }
289
290         link_ksettings->base.autoneg =
291                 (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) ?
292                 AUTONEG_ENABLE : AUTONEG_DISABLE;
293
294         link_ksettings->base.duplex = DUPLEX_FULL;
295
296         return 0;
297 }
298
299 static int ena_get_coalesce(struct net_device *net_dev,
300                             struct ethtool_coalesce *coalesce)
301 {
302         struct ena_adapter *adapter = netdev_priv(net_dev);
303         struct ena_com_dev *ena_dev = adapter->ena_dev;
304         struct ena_intr_moder_entry intr_moder_entry;
305
306         if (!ena_com_interrupt_moderation_supported(ena_dev)) {
307                 /* the devie doesn't support interrupt moderation */
308                 return -EOPNOTSUPP;
309         }
310         coalesce->tx_coalesce_usecs =
311                 ena_com_get_nonadaptive_moderation_interval_tx(ena_dev) /
312                         ena_dev->intr_delay_resolution;
313         if (!ena_com_get_adaptive_moderation_enabled(ena_dev)) {
314                 coalesce->rx_coalesce_usecs =
315                         ena_com_get_nonadaptive_moderation_interval_rx(ena_dev)
316                         / ena_dev->intr_delay_resolution;
317         } else {
318                 ena_com_get_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_LOWEST, &intr_moder_entry);
319                 coalesce->rx_coalesce_usecs_low = intr_moder_entry.intr_moder_interval;
320                 coalesce->rx_max_coalesced_frames_low = intr_moder_entry.pkts_per_interval;
321
322                 ena_com_get_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_MID, &intr_moder_entry);
323                 coalesce->rx_coalesce_usecs = intr_moder_entry.intr_moder_interval;
324                 coalesce->rx_max_coalesced_frames = intr_moder_entry.pkts_per_interval;
325
326                 ena_com_get_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_HIGHEST, &intr_moder_entry);
327                 coalesce->rx_coalesce_usecs_high = intr_moder_entry.intr_moder_interval;
328                 coalesce->rx_max_coalesced_frames_high = intr_moder_entry.pkts_per_interval;
329         }
330         coalesce->use_adaptive_rx_coalesce =
331                 ena_com_get_adaptive_moderation_enabled(ena_dev);
332
333         return 0;
334 }
335
336 static void ena_update_tx_rings_intr_moderation(struct ena_adapter *adapter)
337 {
338         unsigned int val;
339         int i;
340
341         val = ena_com_get_nonadaptive_moderation_interval_tx(adapter->ena_dev);
342
343         for (i = 0; i < adapter->num_queues; i++)
344                 adapter->tx_ring[i].smoothed_interval = val;
345 }
346
347 static int ena_set_coalesce(struct net_device *net_dev,
348                             struct ethtool_coalesce *coalesce)
349 {
350         struct ena_adapter *adapter = netdev_priv(net_dev);
351         struct ena_com_dev *ena_dev = adapter->ena_dev;
352         struct ena_intr_moder_entry intr_moder_entry;
353         int rc;
354
355         if (!ena_com_interrupt_moderation_supported(ena_dev)) {
356                 /* the devie doesn't support interrupt moderation */
357                 return -EOPNOTSUPP;
358         }
359
360         if (coalesce->rx_coalesce_usecs_irq ||
361             coalesce->rx_max_coalesced_frames_irq ||
362             coalesce->tx_coalesce_usecs_irq ||
363             coalesce->tx_max_coalesced_frames ||
364             coalesce->tx_max_coalesced_frames_irq ||
365             coalesce->stats_block_coalesce_usecs ||
366             coalesce->use_adaptive_tx_coalesce ||
367             coalesce->pkt_rate_low ||
368             coalesce->tx_coalesce_usecs_low ||
369             coalesce->tx_max_coalesced_frames_low ||
370             coalesce->pkt_rate_high ||
371             coalesce->tx_coalesce_usecs_high ||
372             coalesce->tx_max_coalesced_frames_high ||
373             coalesce->rate_sample_interval)
374                 return -EINVAL;
375
376         rc = ena_com_update_nonadaptive_moderation_interval_tx(ena_dev,
377                                                                coalesce->tx_coalesce_usecs);
378         if (rc)
379                 return rc;
380
381         ena_update_tx_rings_intr_moderation(adapter);
382
383         if (ena_com_get_adaptive_moderation_enabled(ena_dev)) {
384                 if (!coalesce->use_adaptive_rx_coalesce) {
385                         ena_com_disable_adaptive_moderation(ena_dev);
386                         rc = ena_com_update_nonadaptive_moderation_interval_rx(ena_dev,
387                                                                                coalesce->rx_coalesce_usecs);
388                         return rc;
389                 }
390         } else { /* was in non-adaptive mode */
391                 if (coalesce->use_adaptive_rx_coalesce) {
392                         ena_com_enable_adaptive_moderation(ena_dev);
393                 } else {
394                         rc = ena_com_update_nonadaptive_moderation_interval_rx(ena_dev,
395                                                                                coalesce->rx_coalesce_usecs);
396                         return rc;
397                 }
398         }
399
400         intr_moder_entry.intr_moder_interval = coalesce->rx_coalesce_usecs_low;
401         intr_moder_entry.pkts_per_interval = coalesce->rx_max_coalesced_frames_low;
402         intr_moder_entry.bytes_per_interval = ENA_INTR_BYTE_COUNT_NOT_SUPPORTED;
403         ena_com_init_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_LOWEST, &intr_moder_entry);
404
405         intr_moder_entry.intr_moder_interval = coalesce->rx_coalesce_usecs;
406         intr_moder_entry.pkts_per_interval = coalesce->rx_max_coalesced_frames;
407         intr_moder_entry.bytes_per_interval = ENA_INTR_BYTE_COUNT_NOT_SUPPORTED;
408         ena_com_init_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_MID, &intr_moder_entry);
409
410         intr_moder_entry.intr_moder_interval = coalesce->rx_coalesce_usecs_high;
411         intr_moder_entry.pkts_per_interval = coalesce->rx_max_coalesced_frames_high;
412         intr_moder_entry.bytes_per_interval = ENA_INTR_BYTE_COUNT_NOT_SUPPORTED;
413         ena_com_init_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_HIGHEST, &intr_moder_entry);
414
415         return 0;
416 }
417
418 static u32 ena_get_msglevel(struct net_device *netdev)
419 {
420         struct ena_adapter *adapter = netdev_priv(netdev);
421
422         return adapter->msg_enable;
423 }
424
425 static void ena_set_msglevel(struct net_device *netdev, u32 value)
426 {
427         struct ena_adapter *adapter = netdev_priv(netdev);
428
429         adapter->msg_enable = value;
430 }
431
432 static void ena_get_drvinfo(struct net_device *dev,
433                             struct ethtool_drvinfo *info)
434 {
435         struct ena_adapter *adapter = netdev_priv(dev);
436
437         strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
438         strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
439         strlcpy(info->bus_info, pci_name(adapter->pdev),
440                 sizeof(info->bus_info));
441 }
442
443 static void ena_get_ringparam(struct net_device *netdev,
444                               struct ethtool_ringparam *ring)
445 {
446         struct ena_adapter *adapter = netdev_priv(netdev);
447         struct ena_ring *tx_ring = &adapter->tx_ring[0];
448         struct ena_ring *rx_ring = &adapter->rx_ring[0];
449
450         ring->rx_max_pending = rx_ring->ring_size;
451         ring->tx_max_pending = tx_ring->ring_size;
452         ring->rx_pending = rx_ring->ring_size;
453         ring->tx_pending = tx_ring->ring_size;
454 }
455
456 static u32 ena_flow_hash_to_flow_type(u16 hash_fields)
457 {
458         u32 data = 0;
459
460         if (hash_fields & ENA_ADMIN_RSS_L2_DA)
461                 data |= RXH_L2DA;
462
463         if (hash_fields & ENA_ADMIN_RSS_L3_DA)
464                 data |= RXH_IP_DST;
465
466         if (hash_fields & ENA_ADMIN_RSS_L3_SA)
467                 data |= RXH_IP_SRC;
468
469         if (hash_fields & ENA_ADMIN_RSS_L4_DP)
470                 data |= RXH_L4_B_2_3;
471
472         if (hash_fields & ENA_ADMIN_RSS_L4_SP)
473                 data |= RXH_L4_B_0_1;
474
475         return data;
476 }
477
478 static u16 ena_flow_data_to_flow_hash(u32 hash_fields)
479 {
480         u16 data = 0;
481
482         if (hash_fields & RXH_L2DA)
483                 data |= ENA_ADMIN_RSS_L2_DA;
484
485         if (hash_fields & RXH_IP_DST)
486                 data |= ENA_ADMIN_RSS_L3_DA;
487
488         if (hash_fields & RXH_IP_SRC)
489                 data |= ENA_ADMIN_RSS_L3_SA;
490
491         if (hash_fields & RXH_L4_B_2_3)
492                 data |= ENA_ADMIN_RSS_L4_DP;
493
494         if (hash_fields & RXH_L4_B_0_1)
495                 data |= ENA_ADMIN_RSS_L4_SP;
496
497         return data;
498 }
499
500 static int ena_get_rss_hash(struct ena_com_dev *ena_dev,
501                             struct ethtool_rxnfc *cmd)
502 {
503         enum ena_admin_flow_hash_proto proto;
504         u16 hash_fields;
505         int rc;
506
507         cmd->data = 0;
508
509         switch (cmd->flow_type) {
510         case TCP_V4_FLOW:
511                 proto = ENA_ADMIN_RSS_TCP4;
512                 break;
513         case UDP_V4_FLOW:
514                 proto = ENA_ADMIN_RSS_UDP4;
515                 break;
516         case TCP_V6_FLOW:
517                 proto = ENA_ADMIN_RSS_TCP6;
518                 break;
519         case UDP_V6_FLOW:
520                 proto = ENA_ADMIN_RSS_UDP6;
521                 break;
522         case IPV4_FLOW:
523                 proto = ENA_ADMIN_RSS_IP4;
524                 break;
525         case IPV6_FLOW:
526                 proto = ENA_ADMIN_RSS_IP6;
527                 break;
528         case ETHER_FLOW:
529                 proto = ENA_ADMIN_RSS_NOT_IP;
530                 break;
531         case AH_V4_FLOW:
532         case ESP_V4_FLOW:
533         case AH_V6_FLOW:
534         case ESP_V6_FLOW:
535         case SCTP_V4_FLOW:
536         case AH_ESP_V4_FLOW:
537                 return -EOPNOTSUPP;
538         default:
539                 return -EINVAL;
540         }
541
542         rc = ena_com_get_hash_ctrl(ena_dev, proto, &hash_fields);
543         if (rc)
544                 return rc;
545
546         cmd->data = ena_flow_hash_to_flow_type(hash_fields);
547
548         return 0;
549 }
550
551 static int ena_set_rss_hash(struct ena_com_dev *ena_dev,
552                             struct ethtool_rxnfc *cmd)
553 {
554         enum ena_admin_flow_hash_proto proto;
555         u16 hash_fields;
556
557         switch (cmd->flow_type) {
558         case TCP_V4_FLOW:
559                 proto = ENA_ADMIN_RSS_TCP4;
560                 break;
561         case UDP_V4_FLOW:
562                 proto = ENA_ADMIN_RSS_UDP4;
563                 break;
564         case TCP_V6_FLOW:
565                 proto = ENA_ADMIN_RSS_TCP6;
566                 break;
567         case UDP_V6_FLOW:
568                 proto = ENA_ADMIN_RSS_UDP6;
569                 break;
570         case IPV4_FLOW:
571                 proto = ENA_ADMIN_RSS_IP4;
572                 break;
573         case IPV6_FLOW:
574                 proto = ENA_ADMIN_RSS_IP6;
575                 break;
576         case ETHER_FLOW:
577                 proto = ENA_ADMIN_RSS_NOT_IP;
578                 break;
579         case AH_V4_FLOW:
580         case ESP_V4_FLOW:
581         case AH_V6_FLOW:
582         case ESP_V6_FLOW:
583         case SCTP_V4_FLOW:
584         case AH_ESP_V4_FLOW:
585                 return -EOPNOTSUPP;
586         default:
587                 return -EINVAL;
588         }
589
590         hash_fields = ena_flow_data_to_flow_hash(cmd->data);
591
592         return ena_com_fill_hash_ctrl(ena_dev, proto, hash_fields);
593 }
594
595 static int ena_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info)
596 {
597         struct ena_adapter *adapter = netdev_priv(netdev);
598         int rc = 0;
599
600         switch (info->cmd) {
601         case ETHTOOL_SRXFH:
602                 rc = ena_set_rss_hash(adapter->ena_dev, info);
603                 break;
604         case ETHTOOL_SRXCLSRLDEL:
605         case ETHTOOL_SRXCLSRLINS:
606         default:
607                 netif_err(adapter, drv, netdev,
608                           "Command parameter %d is not supported\n", info->cmd);
609                 rc = -EOPNOTSUPP;
610         }
611
612         return rc;
613 }
614
615 static int ena_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
616                          u32 *rules)
617 {
618         struct ena_adapter *adapter = netdev_priv(netdev);
619         int rc = 0;
620
621         switch (info->cmd) {
622         case ETHTOOL_GRXRINGS:
623                 info->data = adapter->num_queues;
624                 rc = 0;
625                 break;
626         case ETHTOOL_GRXFH:
627                 rc = ena_get_rss_hash(adapter->ena_dev, info);
628                 break;
629         case ETHTOOL_GRXCLSRLCNT:
630         case ETHTOOL_GRXCLSRULE:
631         case ETHTOOL_GRXCLSRLALL:
632         default:
633                 netif_err(adapter, drv, netdev,
634                           "Command parameter %d is not supported\n", info->cmd);
635                 rc = -EOPNOTSUPP;
636         }
637
638         return rc;
639 }
640
641 static u32 ena_get_rxfh_indir_size(struct net_device *netdev)
642 {
643         return ENA_RX_RSS_TABLE_SIZE;
644 }
645
646 static u32 ena_get_rxfh_key_size(struct net_device *netdev)
647 {
648         return ENA_HASH_KEY_SIZE;
649 }
650
651 static int ena_indirection_table_get(struct ena_adapter *adapter, u32 *indir)
652 {
653         struct ena_com_dev *ena_dev = adapter->ena_dev;
654         int i, rc;
655
656         if (!indir)
657                 return 0;
658
659         rc = ena_com_indirect_table_get(ena_dev, indir);
660         if (rc)
661                 return rc;
662
663         /* Our internal representation of the indices is: even indices
664          * for Tx and uneven indices for Rx. We need to convert the Rx
665          * indices to be consecutive
666          */
667         for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++)
668                 indir[i] = ENA_IO_RXQ_IDX_TO_COMBINED_IDX(indir[i]);
669
670         return rc;
671 }
672
673 static int ena_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
674                         u8 *hfunc)
675 {
676         struct ena_adapter *adapter = netdev_priv(netdev);
677         enum ena_admin_hash_functions ena_func;
678         u8 func;
679         int rc;
680
681         rc = ena_indirection_table_get(adapter, indir);
682         if (rc)
683                 return rc;
684
685         /* We call this function in order to check if the device
686          * supports getting/setting the hash function.
687          */
688         rc = ena_com_get_hash_function(adapter->ena_dev, &ena_func, key);
689
690         if (rc) {
691                 if (rc == -EOPNOTSUPP) {
692                         key = NULL;
693                         hfunc = NULL;
694                         rc = 0;
695                 }
696
697                 return rc;
698         }
699
700         if (rc)
701                 return rc;
702
703         switch (ena_func) {
704         case ENA_ADMIN_TOEPLITZ:
705                 func = ETH_RSS_HASH_TOP;
706                 break;
707         case ENA_ADMIN_CRC32:
708                 func = ETH_RSS_HASH_CRC32;
709                 break;
710         default:
711                 netif_err(adapter, drv, netdev,
712                           "Command parameter is not supported\n");
713                 return -EOPNOTSUPP;
714         }
715
716         if (hfunc)
717                 *hfunc = func;
718
719         return rc;
720 }
721
722 static int ena_set_rxfh(struct net_device *netdev, const u32 *indir,
723                         const u8 *key, const u8 hfunc)
724 {
725         struct ena_adapter *adapter = netdev_priv(netdev);
726         struct ena_com_dev *ena_dev = adapter->ena_dev;
727         enum ena_admin_hash_functions func;
728         int rc, i;
729
730         if (indir) {
731                 for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
732                         rc = ena_com_indirect_table_fill_entry(ena_dev,
733                                                                i,
734                                                                ENA_IO_RXQ_IDX(indir[i]));
735                         if (unlikely(rc)) {
736                                 netif_err(adapter, drv, netdev,
737                                           "Cannot fill indirect table (index is too large)\n");
738                                 return rc;
739                         }
740                 }
741
742                 rc = ena_com_indirect_table_set(ena_dev);
743                 if (rc) {
744                         netif_err(adapter, drv, netdev,
745                                   "Cannot set indirect table\n");
746                         return rc == -EPERM ? -EOPNOTSUPP : rc;
747                 }
748         }
749
750         switch (hfunc) {
751         case ETH_RSS_HASH_NO_CHANGE:
752                 func = ena_com_get_current_hash_function(ena_dev);
753                 break;
754         case ETH_RSS_HASH_TOP:
755                 func = ENA_ADMIN_TOEPLITZ;
756                 break;
757         case ETH_RSS_HASH_CRC32:
758                 func = ENA_ADMIN_CRC32;
759                 break;
760         default:
761                 netif_err(adapter, drv, netdev, "Unsupported hfunc %d\n",
762                           hfunc);
763                 return -EOPNOTSUPP;
764         }
765
766         if (key) {
767                 rc = ena_com_fill_hash_function(ena_dev, func, key,
768                                                 ENA_HASH_KEY_SIZE,
769                                                 0xFFFFFFFF);
770                 if (unlikely(rc)) {
771                         netif_err(adapter, drv, netdev, "Cannot fill key\n");
772                         return rc == -EPERM ? -EOPNOTSUPP : rc;
773                 }
774         }
775
776         return 0;
777 }
778
779 static void ena_get_channels(struct net_device *netdev,
780                              struct ethtool_channels *channels)
781 {
782         struct ena_adapter *adapter = netdev_priv(netdev);
783
784         channels->max_rx = adapter->num_queues;
785         channels->max_tx = adapter->num_queues;
786         channels->max_other = 0;
787         channels->max_combined = 0;
788         channels->rx_count = adapter->num_queues;
789         channels->tx_count = adapter->num_queues;
790         channels->other_count = 0;
791         channels->combined_count = 0;
792 }
793
794 static int ena_get_tunable(struct net_device *netdev,
795                            const struct ethtool_tunable *tuna, void *data)
796 {
797         struct ena_adapter *adapter = netdev_priv(netdev);
798         int ret = 0;
799
800         switch (tuna->id) {
801         case ETHTOOL_RX_COPYBREAK:
802                 *(u32 *)data = adapter->rx_copybreak;
803                 break;
804         default:
805                 ret = -EINVAL;
806                 break;
807         }
808
809         return ret;
810 }
811
812 static int ena_set_tunable(struct net_device *netdev,
813                            const struct ethtool_tunable *tuna,
814                            const void *data)
815 {
816         struct ena_adapter *adapter = netdev_priv(netdev);
817         int ret = 0;
818         u32 len;
819
820         switch (tuna->id) {
821         case ETHTOOL_RX_COPYBREAK:
822                 len = *(u32 *)data;
823                 if (len > adapter->netdev->mtu) {
824                         ret = -EINVAL;
825                         break;
826                 }
827                 adapter->rx_copybreak = len;
828                 break;
829         default:
830                 ret = -EINVAL;
831                 break;
832         }
833
834         return ret;
835 }
836
837 static const struct ethtool_ops ena_ethtool_ops = {
838         .get_link_ksettings     = ena_get_link_ksettings,
839         .get_drvinfo            = ena_get_drvinfo,
840         .get_msglevel           = ena_get_msglevel,
841         .set_msglevel           = ena_set_msglevel,
842         .get_link               = ethtool_op_get_link,
843         .get_coalesce           = ena_get_coalesce,
844         .set_coalesce           = ena_set_coalesce,
845         .get_ringparam          = ena_get_ringparam,
846         .get_sset_count         = ena_get_sset_count,
847         .get_strings            = ena_get_strings,
848         .get_ethtool_stats      = ena_get_ethtool_stats,
849         .get_rxnfc              = ena_get_rxnfc,
850         .set_rxnfc              = ena_set_rxnfc,
851         .get_rxfh_indir_size    = ena_get_rxfh_indir_size,
852         .get_rxfh_key_size      = ena_get_rxfh_key_size,
853         .get_rxfh               = ena_get_rxfh,
854         .set_rxfh               = ena_set_rxfh,
855         .get_channels           = ena_get_channels,
856         .get_tunable            = ena_get_tunable,
857         .set_tunable            = ena_set_tunable,
858         .get_ts_info            = ethtool_op_get_ts_info,
859 };
860
861 void ena_set_ethtool_ops(struct net_device *netdev)
862 {
863         netdev->ethtool_ops = &ena_ethtool_ops;
864 }
865
866 static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
867 {
868         struct net_device *netdev = adapter->netdev;
869         u8 *strings_buf;
870         u64 *data_buf;
871         int strings_num;
872         int i, rc;
873
874         strings_num = ena_get_sset_count(netdev, ETH_SS_STATS);
875         if (strings_num <= 0) {
876                 netif_err(adapter, drv, netdev, "Can't get stats num\n");
877                 return;
878         }
879
880         strings_buf = devm_kzalloc(&adapter->pdev->dev,
881                                    strings_num * ETH_GSTRING_LEN,
882                                    GFP_ATOMIC);
883         if (!strings_buf) {
884                 netif_err(adapter, drv, netdev,
885                           "failed to alloc strings_buf\n");
886                 return;
887         }
888
889         data_buf = devm_kzalloc(&adapter->pdev->dev,
890                                 strings_num * sizeof(u64),
891                                 GFP_ATOMIC);
892         if (!data_buf) {
893                 netif_err(adapter, drv, netdev,
894                           "failed to allocate data buf\n");
895                 devm_kfree(&adapter->pdev->dev, strings_buf);
896                 return;
897         }
898
899         ena_get_strings(netdev, ETH_SS_STATS, strings_buf);
900         ena_get_ethtool_stats(netdev, NULL, data_buf);
901
902         /* If there is a buffer, dump stats, otherwise print them to dmesg */
903         if (buf)
904                 for (i = 0; i < strings_num; i++) {
905                         rc = snprintf(buf, ETH_GSTRING_LEN + sizeof(u64),
906                                       "%s %llu\n",
907                                       strings_buf + i * ETH_GSTRING_LEN,
908                                       data_buf[i]);
909                         buf += rc;
910                 }
911         else
912                 for (i = 0; i < strings_num; i++)
913                         netif_err(adapter, drv, netdev, "%s: %llu\n",
914                                   strings_buf + i * ETH_GSTRING_LEN,
915                                   data_buf[i]);
916
917         devm_kfree(&adapter->pdev->dev, strings_buf);
918         devm_kfree(&adapter->pdev->dev, data_buf);
919 }
920
921 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf)
922 {
923         if (!buf)
924                 return;
925
926         ena_dump_stats_ex(adapter, buf);
927 }
928
929 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter)
930 {
931         ena_dump_stats_ex(adapter, NULL);
932 }