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