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