Merge remote-tracking branches 'regmap/topic/const' and 'regmap/topic/hwspinlock...
[linux-2.6-block.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_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
652                         u8 *hfunc)
653 {
654         struct ena_adapter *adapter = netdev_priv(netdev);
655         enum ena_admin_hash_functions ena_func;
656         u8 func;
657         int rc;
658
659         rc = ena_com_indirect_table_get(adapter->ena_dev, indir);
660         if (rc)
661                 return rc;
662
663         rc = ena_com_get_hash_function(adapter->ena_dev, &ena_func, key);
664         if (rc)
665                 return rc;
666
667         switch (ena_func) {
668         case ENA_ADMIN_TOEPLITZ:
669                 func = ETH_RSS_HASH_TOP;
670                 break;
671         case ENA_ADMIN_CRC32:
672                 func = ETH_RSS_HASH_XOR;
673                 break;
674         default:
675                 netif_err(adapter, drv, netdev,
676                           "Command parameter is not supported\n");
677                 return -EOPNOTSUPP;
678         }
679
680         if (hfunc)
681                 *hfunc = func;
682
683         return rc;
684 }
685
686 static int ena_set_rxfh(struct net_device *netdev, const u32 *indir,
687                         const u8 *key, const u8 hfunc)
688 {
689         struct ena_adapter *adapter = netdev_priv(netdev);
690         struct ena_com_dev *ena_dev = adapter->ena_dev;
691         enum ena_admin_hash_functions func;
692         int rc, i;
693
694         if (indir) {
695                 for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
696                         rc = ena_com_indirect_table_fill_entry(ena_dev,
697                                                                ENA_IO_RXQ_IDX(indir[i]),
698                                                                i);
699                         if (unlikely(rc)) {
700                                 netif_err(adapter, drv, netdev,
701                                           "Cannot fill indirect table (index is too large)\n");
702                                 return rc;
703                         }
704                 }
705
706                 rc = ena_com_indirect_table_set(ena_dev);
707                 if (rc) {
708                         netif_err(adapter, drv, netdev,
709                                   "Cannot set indirect table\n");
710                         return rc == -EPERM ? -EOPNOTSUPP : rc;
711                 }
712         }
713
714         switch (hfunc) {
715         case ETH_RSS_HASH_TOP:
716                 func = ENA_ADMIN_TOEPLITZ;
717                 break;
718         case ETH_RSS_HASH_XOR:
719                 func = ENA_ADMIN_CRC32;
720                 break;
721         default:
722                 netif_err(adapter, drv, netdev, "Unsupported hfunc %d\n",
723                           hfunc);
724                 return -EOPNOTSUPP;
725         }
726
727         if (key) {
728                 rc = ena_com_fill_hash_function(ena_dev, func, key,
729                                                 ENA_HASH_KEY_SIZE,
730                                                 0xFFFFFFFF);
731                 if (unlikely(rc)) {
732                         netif_err(adapter, drv, netdev, "Cannot fill key\n");
733                         return rc == -EPERM ? -EOPNOTSUPP : rc;
734                 }
735         }
736
737         return 0;
738 }
739
740 static void ena_get_channels(struct net_device *netdev,
741                              struct ethtool_channels *channels)
742 {
743         struct ena_adapter *adapter = netdev_priv(netdev);
744
745         channels->max_rx = adapter->num_queues;
746         channels->max_tx = adapter->num_queues;
747         channels->max_other = 0;
748         channels->max_combined = 0;
749         channels->rx_count = adapter->num_queues;
750         channels->tx_count = adapter->num_queues;
751         channels->other_count = 0;
752         channels->combined_count = 0;
753 }
754
755 static int ena_get_tunable(struct net_device *netdev,
756                            const struct ethtool_tunable *tuna, void *data)
757 {
758         struct ena_adapter *adapter = netdev_priv(netdev);
759         int ret = 0;
760
761         switch (tuna->id) {
762         case ETHTOOL_RX_COPYBREAK:
763                 *(u32 *)data = adapter->rx_copybreak;
764                 break;
765         default:
766                 ret = -EINVAL;
767                 break;
768         }
769
770         return ret;
771 }
772
773 static int ena_set_tunable(struct net_device *netdev,
774                            const struct ethtool_tunable *tuna,
775                            const void *data)
776 {
777         struct ena_adapter *adapter = netdev_priv(netdev);
778         int ret = 0;
779         u32 len;
780
781         switch (tuna->id) {
782         case ETHTOOL_RX_COPYBREAK:
783                 len = *(u32 *)data;
784                 if (len > adapter->netdev->mtu) {
785                         ret = -EINVAL;
786                         break;
787                 }
788                 adapter->rx_copybreak = len;
789                 break;
790         default:
791                 ret = -EINVAL;
792                 break;
793         }
794
795         return ret;
796 }
797
798 static const struct ethtool_ops ena_ethtool_ops = {
799         .get_link_ksettings     = ena_get_link_ksettings,
800         .get_drvinfo            = ena_get_drvinfo,
801         .get_msglevel           = ena_get_msglevel,
802         .set_msglevel           = ena_set_msglevel,
803         .get_link               = ethtool_op_get_link,
804         .get_coalesce           = ena_get_coalesce,
805         .set_coalesce           = ena_set_coalesce,
806         .get_ringparam          = ena_get_ringparam,
807         .get_sset_count         = ena_get_sset_count,
808         .get_strings            = ena_get_strings,
809         .get_ethtool_stats      = ena_get_ethtool_stats,
810         .get_rxnfc              = ena_get_rxnfc,
811         .set_rxnfc              = ena_set_rxnfc,
812         .get_rxfh_indir_size    = ena_get_rxfh_indir_size,
813         .get_rxfh_key_size      = ena_get_rxfh_key_size,
814         .get_rxfh               = ena_get_rxfh,
815         .set_rxfh               = ena_set_rxfh,
816         .get_channels           = ena_get_channels,
817         .get_tunable            = ena_get_tunable,
818         .set_tunable            = ena_set_tunable,
819 };
820
821 void ena_set_ethtool_ops(struct net_device *netdev)
822 {
823         netdev->ethtool_ops = &ena_ethtool_ops;
824 }
825
826 static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
827 {
828         struct net_device *netdev = adapter->netdev;
829         u8 *strings_buf;
830         u64 *data_buf;
831         int strings_num;
832         int i, rc;
833
834         strings_num = ena_get_sset_count(netdev, ETH_SS_STATS);
835         if (strings_num <= 0) {
836                 netif_err(adapter, drv, netdev, "Can't get stats num\n");
837                 return;
838         }
839
840         strings_buf = devm_kzalloc(&adapter->pdev->dev,
841                                    strings_num * ETH_GSTRING_LEN,
842                                    GFP_ATOMIC);
843         if (!strings_buf) {
844                 netif_err(adapter, drv, netdev,
845                           "failed to alloc strings_buf\n");
846                 return;
847         }
848
849         data_buf = devm_kzalloc(&adapter->pdev->dev,
850                                 strings_num * sizeof(u64),
851                                 GFP_ATOMIC);
852         if (!data_buf) {
853                 netif_err(adapter, drv, netdev,
854                           "failed to allocate data buf\n");
855                 devm_kfree(&adapter->pdev->dev, strings_buf);
856                 return;
857         }
858
859         ena_get_strings(netdev, ETH_SS_STATS, strings_buf);
860         ena_get_ethtool_stats(netdev, NULL, data_buf);
861
862         /* If there is a buffer, dump stats, otherwise print them to dmesg */
863         if (buf)
864                 for (i = 0; i < strings_num; i++) {
865                         rc = snprintf(buf, ETH_GSTRING_LEN + sizeof(u64),
866                                       "%s %llu\n",
867                                       strings_buf + i * ETH_GSTRING_LEN,
868                                       data_buf[i]);
869                         buf += rc;
870                 }
871         else
872                 for (i = 0; i < strings_num; i++)
873                         netif_err(adapter, drv, netdev, "%s: %llu\n",
874                                   strings_buf + i * ETH_GSTRING_LEN,
875                                   data_buf[i]);
876
877         devm_kfree(&adapter->pdev->dev, strings_buf);
878         devm_kfree(&adapter->pdev->dev, data_buf);
879 }
880
881 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf)
882 {
883         if (!buf)
884                 return;
885
886         ena_dump_stats_ex(adapter, buf);
887 }
888
889 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter)
890 {
891         ena_dump_stats_ex(adapter, NULL);
892 }