]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/amazon/ena/ena_ethtool.c
net: ena: fix theoretical Rx hang on low memory systems
[karo-tx-linux.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         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                 /* If device don't have permission, return unsupported */
545                 if (rc == -EPERM)
546                         rc = -EOPNOTSUPP;
547                 return rc;
548         }
549
550         cmd->data = ena_flow_hash_to_flow_type(hash_fields);
551
552         return 0;
553 }
554
555 static int ena_set_rss_hash(struct ena_com_dev *ena_dev,
556                             struct ethtool_rxnfc *cmd)
557 {
558         enum ena_admin_flow_hash_proto proto;
559         u16 hash_fields;
560
561         switch (cmd->flow_type) {
562         case TCP_V4_FLOW:
563                 proto = ENA_ADMIN_RSS_TCP4;
564                 break;
565         case UDP_V4_FLOW:
566                 proto = ENA_ADMIN_RSS_UDP4;
567                 break;
568         case TCP_V6_FLOW:
569                 proto = ENA_ADMIN_RSS_TCP6;
570                 break;
571         case UDP_V6_FLOW:
572                 proto = ENA_ADMIN_RSS_UDP6;
573                 break;
574         case IPV4_FLOW:
575                 proto = ENA_ADMIN_RSS_IP4;
576                 break;
577         case IPV6_FLOW:
578                 proto = ENA_ADMIN_RSS_IP6;
579                 break;
580         case ETHER_FLOW:
581                 proto = ENA_ADMIN_RSS_NOT_IP;
582                 break;
583         case AH_V4_FLOW:
584         case ESP_V4_FLOW:
585         case AH_V6_FLOW:
586         case ESP_V6_FLOW:
587         case SCTP_V4_FLOW:
588         case AH_ESP_V4_FLOW:
589                 return -EOPNOTSUPP;
590         default:
591                 return -EINVAL;
592         }
593
594         hash_fields = ena_flow_data_to_flow_hash(cmd->data);
595
596         return ena_com_fill_hash_ctrl(ena_dev, proto, hash_fields);
597 }
598
599 static int ena_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info)
600 {
601         struct ena_adapter *adapter = netdev_priv(netdev);
602         int rc = 0;
603
604         switch (info->cmd) {
605         case ETHTOOL_SRXFH:
606                 rc = ena_set_rss_hash(adapter->ena_dev, info);
607                 break;
608         case ETHTOOL_SRXCLSRLDEL:
609         case ETHTOOL_SRXCLSRLINS:
610         default:
611                 netif_err(adapter, drv, netdev,
612                           "Command parameter %d is not supported\n", info->cmd);
613                 rc = -EOPNOTSUPP;
614         }
615
616         return (rc == -EPERM) ? -EOPNOTSUPP : rc;
617 }
618
619 static int ena_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
620                          u32 *rules)
621 {
622         struct ena_adapter *adapter = netdev_priv(netdev);
623         int rc = 0;
624
625         switch (info->cmd) {
626         case ETHTOOL_GRXRINGS:
627                 info->data = adapter->num_queues;
628                 rc = 0;
629                 break;
630         case ETHTOOL_GRXFH:
631                 rc = ena_get_rss_hash(adapter->ena_dev, info);
632                 break;
633         case ETHTOOL_GRXCLSRLCNT:
634         case ETHTOOL_GRXCLSRULE:
635         case ETHTOOL_GRXCLSRLALL:
636         default:
637                 netif_err(adapter, drv, netdev,
638                           "Command parameter %d is not supported\n", info->cmd);
639                 rc = -EOPNOTSUPP;
640         }
641
642         return (rc == -EPERM) ? -EOPNOTSUPP : rc;
643 }
644
645 static u32 ena_get_rxfh_indir_size(struct net_device *netdev)
646 {
647         return ENA_RX_RSS_TABLE_SIZE;
648 }
649
650 static u32 ena_get_rxfh_key_size(struct net_device *netdev)
651 {
652         return ENA_HASH_KEY_SIZE;
653 }
654
655 static int ena_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
656                         u8 *hfunc)
657 {
658         struct ena_adapter *adapter = netdev_priv(netdev);
659         enum ena_admin_hash_functions ena_func;
660         u8 func;
661         int rc;
662
663         rc = ena_com_indirect_table_get(adapter->ena_dev, indir);
664         if (rc)
665                 return rc;
666
667         rc = ena_com_get_hash_function(adapter->ena_dev, &ena_func, key);
668         if (rc)
669                 return rc;
670
671         switch (ena_func) {
672         case ENA_ADMIN_TOEPLITZ:
673                 func = ETH_RSS_HASH_TOP;
674                 break;
675         case ENA_ADMIN_CRC32:
676                 func = ETH_RSS_HASH_XOR;
677                 break;
678         default:
679                 netif_err(adapter, drv, netdev,
680                           "Command parameter is not supported\n");
681                 return -EOPNOTSUPP;
682         }
683
684         if (hfunc)
685                 *hfunc = func;
686
687         return rc;
688 }
689
690 static int ena_set_rxfh(struct net_device *netdev, const u32 *indir,
691                         const u8 *key, const u8 hfunc)
692 {
693         struct ena_adapter *adapter = netdev_priv(netdev);
694         struct ena_com_dev *ena_dev = adapter->ena_dev;
695         enum ena_admin_hash_functions func;
696         int rc, i;
697
698         if (indir) {
699                 for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
700                         rc = ena_com_indirect_table_fill_entry(ena_dev,
701                                                                ENA_IO_RXQ_IDX(indir[i]),
702                                                                i);
703                         if (unlikely(rc)) {
704                                 netif_err(adapter, drv, netdev,
705                                           "Cannot fill indirect table (index is too large)\n");
706                                 return rc;
707                         }
708                 }
709
710                 rc = ena_com_indirect_table_set(ena_dev);
711                 if (rc) {
712                         netif_err(adapter, drv, netdev,
713                                   "Cannot set indirect table\n");
714                         return rc == -EPERM ? -EOPNOTSUPP : rc;
715                 }
716         }
717
718         switch (hfunc) {
719         case ETH_RSS_HASH_TOP:
720                 func = ENA_ADMIN_TOEPLITZ;
721                 break;
722         case ETH_RSS_HASH_XOR:
723                 func = ENA_ADMIN_CRC32;
724                 break;
725         default:
726                 netif_err(adapter, drv, netdev, "Unsupported hfunc %d\n",
727                           hfunc);
728                 return -EOPNOTSUPP;
729         }
730
731         if (key) {
732                 rc = ena_com_fill_hash_function(ena_dev, func, key,
733                                                 ENA_HASH_KEY_SIZE,
734                                                 0xFFFFFFFF);
735                 if (unlikely(rc)) {
736                         netif_err(adapter, drv, netdev, "Cannot fill key\n");
737                         return rc == -EPERM ? -EOPNOTSUPP : rc;
738                 }
739         }
740
741         return 0;
742 }
743
744 static void ena_get_channels(struct net_device *netdev,
745                              struct ethtool_channels *channels)
746 {
747         struct ena_adapter *adapter = netdev_priv(netdev);
748
749         channels->max_rx = ENA_MAX_NUM_IO_QUEUES;
750         channels->max_tx = ENA_MAX_NUM_IO_QUEUES;
751         channels->max_other = 0;
752         channels->max_combined = 0;
753         channels->rx_count = adapter->num_queues;
754         channels->tx_count = adapter->num_queues;
755         channels->other_count = 0;
756         channels->combined_count = 0;
757 }
758
759 static int ena_get_tunable(struct net_device *netdev,
760                            const struct ethtool_tunable *tuna, void *data)
761 {
762         struct ena_adapter *adapter = netdev_priv(netdev);
763         int ret = 0;
764
765         switch (tuna->id) {
766         case ETHTOOL_RX_COPYBREAK:
767                 *(u32 *)data = adapter->rx_copybreak;
768                 break;
769         default:
770                 ret = -EINVAL;
771                 break;
772         }
773
774         return ret;
775 }
776
777 static int ena_set_tunable(struct net_device *netdev,
778                            const struct ethtool_tunable *tuna,
779                            const void *data)
780 {
781         struct ena_adapter *adapter = netdev_priv(netdev);
782         int ret = 0;
783         u32 len;
784
785         switch (tuna->id) {
786         case ETHTOOL_RX_COPYBREAK:
787                 len = *(u32 *)data;
788                 if (len > adapter->netdev->mtu) {
789                         ret = -EINVAL;
790                         break;
791                 }
792                 adapter->rx_copybreak = len;
793                 break;
794         default:
795                 ret = -EINVAL;
796                 break;
797         }
798
799         return ret;
800 }
801
802 static const struct ethtool_ops ena_ethtool_ops = {
803         .get_link_ksettings     = ena_get_link_ksettings,
804         .get_drvinfo            = ena_get_drvinfo,
805         .get_msglevel           = ena_get_msglevel,
806         .set_msglevel           = ena_set_msglevel,
807         .get_link               = ethtool_op_get_link,
808         .get_coalesce           = ena_get_coalesce,
809         .set_coalesce           = ena_set_coalesce,
810         .get_ringparam          = ena_get_ringparam,
811         .get_sset_count         = ena_get_sset_count,
812         .get_strings            = ena_get_strings,
813         .get_ethtool_stats      = ena_get_ethtool_stats,
814         .get_rxnfc              = ena_get_rxnfc,
815         .set_rxnfc              = ena_set_rxnfc,
816         .get_rxfh_indir_size    = ena_get_rxfh_indir_size,
817         .get_rxfh_key_size      = ena_get_rxfh_key_size,
818         .get_rxfh               = ena_get_rxfh,
819         .set_rxfh               = ena_set_rxfh,
820         .get_channels           = ena_get_channels,
821         .get_tunable            = ena_get_tunable,
822         .set_tunable            = ena_set_tunable,
823 };
824
825 void ena_set_ethtool_ops(struct net_device *netdev)
826 {
827         netdev->ethtool_ops = &ena_ethtool_ops;
828 }
829
830 static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
831 {
832         struct net_device *netdev = adapter->netdev;
833         u8 *strings_buf;
834         u64 *data_buf;
835         int strings_num;
836         int i, rc;
837
838         strings_num = ena_get_sset_count(netdev, ETH_SS_STATS);
839         if (strings_num <= 0) {
840                 netif_err(adapter, drv, netdev, "Can't get stats num\n");
841                 return;
842         }
843
844         strings_buf = devm_kzalloc(&adapter->pdev->dev,
845                                    strings_num * ETH_GSTRING_LEN,
846                                    GFP_ATOMIC);
847         if (!strings_buf) {
848                 netif_err(adapter, drv, netdev,
849                           "failed to alloc strings_buf\n");
850                 return;
851         }
852
853         data_buf = devm_kzalloc(&adapter->pdev->dev,
854                                 strings_num * sizeof(u64),
855                                 GFP_ATOMIC);
856         if (!data_buf) {
857                 netif_err(adapter, drv, netdev,
858                           "failed to allocate data buf\n");
859                 devm_kfree(&adapter->pdev->dev, strings_buf);
860                 return;
861         }
862
863         ena_get_strings(netdev, ETH_SS_STATS, strings_buf);
864         ena_get_ethtool_stats(netdev, NULL, data_buf);
865
866         /* If there is a buffer, dump stats, otherwise print them to dmesg */
867         if (buf)
868                 for (i = 0; i < strings_num; i++) {
869                         rc = snprintf(buf, ETH_GSTRING_LEN + sizeof(u64),
870                                       "%s %llu\n",
871                                       strings_buf + i * ETH_GSTRING_LEN,
872                                       data_buf[i]);
873                         buf += rc;
874                 }
875         else
876                 for (i = 0; i < strings_num; i++)
877                         netif_err(adapter, drv, netdev, "%s: %llu\n",
878                                   strings_buf + i * ETH_GSTRING_LEN,
879                                   data_buf[i]);
880
881         devm_kfree(&adapter->pdev->dev, strings_buf);
882         devm_kfree(&adapter->pdev->dev, data_buf);
883 }
884
885 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf)
886 {
887         if (!buf)
888                 return;
889
890         ena_dump_stats_ex(adapter, buf);
891 }
892
893 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter)
894 {
895         ena_dump_stats_ex(adapter, NULL);
896 }