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