]> Git Repo - J-linux.git/blob - drivers/net/ethernet/amazon/ena/ena_ethtool.c
Merge tag 'trace-v5.13-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[J-linux.git] / drivers / net / ethernet / amazon / ena / ena_ethtool.c
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3  * Copyright 2015-2020 Amazon.com, Inc. or its affiliates. All rights reserved.
4  */
5
6 #include <linux/ethtool.h>
7 #include <linux/pci.h>
8
9 #include "ena_netdev.h"
10
11 struct ena_stats {
12         char name[ETH_GSTRING_LEN];
13         int stat_offset;
14 };
15
16 #define ENA_STAT_ENA_COM_ENTRY(stat) { \
17         .name = #stat, \
18         .stat_offset = offsetof(struct ena_com_stats_admin, stat) / sizeof(u64) \
19 }
20
21 #define ENA_STAT_ENTRY(stat, stat_type) { \
22         .name = #stat, \
23         .stat_offset = offsetof(struct ena_stats_##stat_type, stat) / sizeof(u64) \
24 }
25
26 #define ENA_STAT_HW_ENTRY(stat, stat_type) { \
27         .name = #stat, \
28         .stat_offset = offsetof(struct ena_admin_##stat_type, stat) / sizeof(u64) \
29 }
30
31 #define ENA_STAT_RX_ENTRY(stat) \
32         ENA_STAT_ENTRY(stat, rx)
33
34 #define ENA_STAT_TX_ENTRY(stat) \
35         ENA_STAT_ENTRY(stat, tx)
36
37 #define ENA_STAT_GLOBAL_ENTRY(stat) \
38         ENA_STAT_ENTRY(stat, dev)
39
40 #define ENA_STAT_ENI_ENTRY(stat) \
41         ENA_STAT_HW_ENTRY(stat, eni_stats)
42
43 static const struct ena_stats ena_stats_global_strings[] = {
44         ENA_STAT_GLOBAL_ENTRY(tx_timeout),
45         ENA_STAT_GLOBAL_ENTRY(suspend),
46         ENA_STAT_GLOBAL_ENTRY(resume),
47         ENA_STAT_GLOBAL_ENTRY(wd_expired),
48         ENA_STAT_GLOBAL_ENTRY(interface_up),
49         ENA_STAT_GLOBAL_ENTRY(interface_down),
50         ENA_STAT_GLOBAL_ENTRY(admin_q_pause),
51 };
52
53 static const struct ena_stats ena_stats_eni_strings[] = {
54         ENA_STAT_ENI_ENTRY(bw_in_allowance_exceeded),
55         ENA_STAT_ENI_ENTRY(bw_out_allowance_exceeded),
56         ENA_STAT_ENI_ENTRY(pps_allowance_exceeded),
57         ENA_STAT_ENI_ENTRY(conntrack_allowance_exceeded),
58         ENA_STAT_ENI_ENTRY(linklocal_allowance_exceeded),
59 };
60
61 static const struct ena_stats ena_stats_tx_strings[] = {
62         ENA_STAT_TX_ENTRY(cnt),
63         ENA_STAT_TX_ENTRY(bytes),
64         ENA_STAT_TX_ENTRY(queue_stop),
65         ENA_STAT_TX_ENTRY(queue_wakeup),
66         ENA_STAT_TX_ENTRY(dma_mapping_err),
67         ENA_STAT_TX_ENTRY(linearize),
68         ENA_STAT_TX_ENTRY(linearize_failed),
69         ENA_STAT_TX_ENTRY(napi_comp),
70         ENA_STAT_TX_ENTRY(tx_poll),
71         ENA_STAT_TX_ENTRY(doorbells),
72         ENA_STAT_TX_ENTRY(prepare_ctx_err),
73         ENA_STAT_TX_ENTRY(bad_req_id),
74         ENA_STAT_TX_ENTRY(llq_buffer_copy),
75         ENA_STAT_TX_ENTRY(missed_tx),
76         ENA_STAT_TX_ENTRY(unmask_interrupt),
77 };
78
79 static const struct ena_stats ena_stats_rx_strings[] = {
80         ENA_STAT_RX_ENTRY(cnt),
81         ENA_STAT_RX_ENTRY(bytes),
82         ENA_STAT_RX_ENTRY(rx_copybreak_pkt),
83         ENA_STAT_RX_ENTRY(csum_good),
84         ENA_STAT_RX_ENTRY(refil_partial),
85         ENA_STAT_RX_ENTRY(bad_csum),
86         ENA_STAT_RX_ENTRY(page_alloc_fail),
87         ENA_STAT_RX_ENTRY(skb_alloc_fail),
88         ENA_STAT_RX_ENTRY(dma_mapping_err),
89         ENA_STAT_RX_ENTRY(bad_desc_num),
90         ENA_STAT_RX_ENTRY(bad_req_id),
91         ENA_STAT_RX_ENTRY(empty_rx_ring),
92         ENA_STAT_RX_ENTRY(csum_unchecked),
93         ENA_STAT_RX_ENTRY(xdp_aborted),
94         ENA_STAT_RX_ENTRY(xdp_drop),
95         ENA_STAT_RX_ENTRY(xdp_pass),
96         ENA_STAT_RX_ENTRY(xdp_tx),
97         ENA_STAT_RX_ENTRY(xdp_invalid),
98         ENA_STAT_RX_ENTRY(xdp_redirect),
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 #define ENA_STATS_ARRAY_ENI(adapter)    \
114         (ARRAY_SIZE(ena_stats_eni_strings) * (adapter)->eni_stats_supported)
115
116 static void ena_safe_update_stat(u64 *src, u64 *dst,
117                                  struct u64_stats_sync *syncp)
118 {
119         unsigned int start;
120
121         do {
122                 start = u64_stats_fetch_begin_irq(syncp);
123                 *(dst) = *src;
124         } while (u64_stats_fetch_retry_irq(syncp, start));
125 }
126
127 static void ena_queue_stats(struct ena_adapter *adapter, u64 **data)
128 {
129         const struct ena_stats *ena_stats;
130         struct ena_ring *ring;
131
132         u64 *ptr;
133         int i, j;
134
135         for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) {
136                 /* Tx stats */
137                 ring = &adapter->tx_ring[i];
138
139                 for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
140                         ena_stats = &ena_stats_tx_strings[j];
141
142                         ptr = (u64 *)&ring->tx_stats + ena_stats->stat_offset;
143
144                         ena_safe_update_stat(ptr, (*data)++, &ring->syncp);
145                 }
146                 /* XDP TX queues don't have a RX queue counterpart */
147                 if (!ENA_IS_XDP_INDEX(adapter, i)) {
148                         /* Rx stats */
149                         ring = &adapter->rx_ring[i];
150
151                         for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
152                                 ena_stats = &ena_stats_rx_strings[j];
153
154                                 ptr = (u64 *)&ring->rx_stats +
155                                         ena_stats->stat_offset;
156
157                                 ena_safe_update_stat(ptr, (*data)++, &ring->syncp);
158                         }
159                 }
160         }
161 }
162
163 static void ena_dev_admin_queue_stats(struct ena_adapter *adapter, u64 **data)
164 {
165         const struct ena_stats *ena_stats;
166         u64 *ptr;
167         int i;
168
169         for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
170                 ena_stats = &ena_stats_ena_com_strings[i];
171
172                 ptr = (u64 *)&adapter->ena_dev->admin_queue.stats +
173                         ena_stats->stat_offset;
174
175                 *(*data)++ = *ptr;
176         }
177 }
178
179 static void ena_get_stats(struct ena_adapter *adapter,
180                           u64 *data,
181                           bool eni_stats_needed)
182 {
183         const struct ena_stats *ena_stats;
184         u64 *ptr;
185         int i;
186
187         for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
188                 ena_stats = &ena_stats_global_strings[i];
189
190                 ptr = (u64 *)&adapter->dev_stats + ena_stats->stat_offset;
191
192                 ena_safe_update_stat(ptr, data++, &adapter->syncp);
193         }
194
195         if (eni_stats_needed) {
196                 ena_update_hw_stats(adapter);
197                 for (i = 0; i < ENA_STATS_ARRAY_ENI(adapter); i++) {
198                         ena_stats = &ena_stats_eni_strings[i];
199
200                         ptr = (u64 *)&adapter->eni_stats +
201                                 ena_stats->stat_offset;
202
203                         ena_safe_update_stat(ptr, data++, &adapter->syncp);
204                 }
205         }
206
207         ena_queue_stats(adapter, &data);
208         ena_dev_admin_queue_stats(adapter, &data);
209 }
210
211 static void ena_get_ethtool_stats(struct net_device *netdev,
212                                   struct ethtool_stats *stats,
213                                   u64 *data)
214 {
215         struct ena_adapter *adapter = netdev_priv(netdev);
216
217         ena_get_stats(adapter, data, adapter->eni_stats_supported);
218 }
219
220 static int ena_get_sw_stats_count(struct ena_adapter *adapter)
221 {
222         return adapter->num_io_queues * (ENA_STATS_ARRAY_TX + ENA_STATS_ARRAY_RX)
223                 + adapter->xdp_num_queues * ENA_STATS_ARRAY_TX
224                 + ENA_STATS_ARRAY_GLOBAL + ENA_STATS_ARRAY_ENA_COM;
225 }
226
227 static int ena_get_hw_stats_count(struct ena_adapter *adapter)
228 {
229         return ENA_STATS_ARRAY_ENI(adapter);
230 }
231
232 int ena_get_sset_count(struct net_device *netdev, int sset)
233 {
234         struct ena_adapter *adapter = netdev_priv(netdev);
235
236         if (sset != ETH_SS_STATS)
237                 return -EOPNOTSUPP;
238
239         return ena_get_sw_stats_count(adapter) + ena_get_hw_stats_count(adapter);
240 }
241
242 static void ena_queue_strings(struct ena_adapter *adapter, u8 **data)
243 {
244         const struct ena_stats *ena_stats;
245         bool is_xdp;
246         int i, j;
247
248         for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) {
249                 is_xdp = ENA_IS_XDP_INDEX(adapter, i);
250                 /* Tx stats */
251                 for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
252                         ena_stats = &ena_stats_tx_strings[j];
253
254                         ethtool_sprintf(data,
255                                         "queue_%u_%s_%s", i,
256                                         is_xdp ? "xdp_tx" : "tx",
257                                         ena_stats->name);
258                 }
259
260                 if (!is_xdp) {
261                         /* RX stats, in XDP there isn't a RX queue
262                          * counterpart
263                          */
264                         for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
265                                 ena_stats = &ena_stats_rx_strings[j];
266
267                                 ethtool_sprintf(data,
268                                                 "queue_%u_rx_%s", i,
269                                                 ena_stats->name);
270                         }
271                 }
272         }
273 }
274
275 static void ena_com_dev_strings(u8 **data)
276 {
277         const struct ena_stats *ena_stats;
278         int i;
279
280         for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
281                 ena_stats = &ena_stats_ena_com_strings[i];
282
283                 ethtool_sprintf(data,
284                                 "ena_admin_q_%s", ena_stats->name);
285         }
286 }
287
288 static void ena_get_strings(struct ena_adapter *adapter,
289                             u8 *data,
290                             bool eni_stats_needed)
291 {
292         const struct ena_stats *ena_stats;
293         int i;
294
295         for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
296                 ena_stats = &ena_stats_global_strings[i];
297                 ethtool_sprintf(&data, ena_stats->name);
298         }
299
300         if (eni_stats_needed) {
301                 for (i = 0; i < ENA_STATS_ARRAY_ENI(adapter); i++) {
302                         ena_stats = &ena_stats_eni_strings[i];
303                         ethtool_sprintf(&data, ena_stats->name);
304                 }
305         }
306
307         ena_queue_strings(adapter, &data);
308         ena_com_dev_strings(&data);
309 }
310
311 static void ena_get_ethtool_strings(struct net_device *netdev,
312                                     u32 sset,
313                                     u8 *data)
314 {
315         struct ena_adapter *adapter = netdev_priv(netdev);
316
317         if (sset != ETH_SS_STATS)
318                 return;
319
320         ena_get_strings(adapter, data, adapter->eni_stats_supported);
321 }
322
323 static int ena_get_link_ksettings(struct net_device *netdev,
324                                   struct ethtool_link_ksettings *link_ksettings)
325 {
326         struct ena_adapter *adapter = netdev_priv(netdev);
327         struct ena_com_dev *ena_dev = adapter->ena_dev;
328         struct ena_admin_get_feature_link_desc *link;
329         struct ena_admin_get_feat_resp feat_resp;
330         int rc;
331
332         rc = ena_com_get_link_params(ena_dev, &feat_resp);
333         if (rc)
334                 return rc;
335
336         link = &feat_resp.u.link;
337         link_ksettings->base.speed = link->speed;
338
339         if (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) {
340                 ethtool_link_ksettings_add_link_mode(link_ksettings,
341                                                      supported, Autoneg);
342                 ethtool_link_ksettings_add_link_mode(link_ksettings,
343                                                      supported, Autoneg);
344         }
345
346         link_ksettings->base.autoneg =
347                 (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) ?
348                 AUTONEG_ENABLE : AUTONEG_DISABLE;
349
350         link_ksettings->base.duplex = DUPLEX_FULL;
351
352         return 0;
353 }
354
355 static int ena_get_coalesce(struct net_device *net_dev,
356                             struct ethtool_coalesce *coalesce)
357 {
358         struct ena_adapter *adapter = netdev_priv(net_dev);
359         struct ena_com_dev *ena_dev = adapter->ena_dev;
360
361         if (!ena_com_interrupt_moderation_supported(ena_dev))
362                 return -EOPNOTSUPP;
363
364         coalesce->tx_coalesce_usecs =
365                 ena_com_get_nonadaptive_moderation_interval_tx(ena_dev) *
366                         ena_dev->intr_delay_resolution;
367
368         coalesce->rx_coalesce_usecs =
369                 ena_com_get_nonadaptive_moderation_interval_rx(ena_dev)
370                 * ena_dev->intr_delay_resolution;
371
372         coalesce->use_adaptive_rx_coalesce =
373                 ena_com_get_adaptive_moderation_enabled(ena_dev);
374
375         return 0;
376 }
377
378 static void ena_update_tx_rings_nonadaptive_intr_moderation(struct ena_adapter *adapter)
379 {
380         unsigned int val;
381         int i;
382
383         val = ena_com_get_nonadaptive_moderation_interval_tx(adapter->ena_dev);
384
385         for (i = 0; i < adapter->num_io_queues; i++)
386                 adapter->tx_ring[i].smoothed_interval = val;
387 }
388
389 static void ena_update_rx_rings_nonadaptive_intr_moderation(struct ena_adapter *adapter)
390 {
391         unsigned int val;
392         int i;
393
394         val = ena_com_get_nonadaptive_moderation_interval_rx(adapter->ena_dev);
395
396         for (i = 0; i < adapter->num_io_queues; i++)
397                 adapter->rx_ring[i].smoothed_interval = val;
398 }
399
400 static int ena_set_coalesce(struct net_device *net_dev,
401                             struct ethtool_coalesce *coalesce)
402 {
403         struct ena_adapter *adapter = netdev_priv(net_dev);
404         struct ena_com_dev *ena_dev = adapter->ena_dev;
405         int rc;
406
407         if (!ena_com_interrupt_moderation_supported(ena_dev))
408                 return -EOPNOTSUPP;
409
410         rc = ena_com_update_nonadaptive_moderation_interval_tx(ena_dev,
411                                                                coalesce->tx_coalesce_usecs);
412         if (rc)
413                 return rc;
414
415         ena_update_tx_rings_nonadaptive_intr_moderation(adapter);
416
417         rc = ena_com_update_nonadaptive_moderation_interval_rx(ena_dev,
418                                                                coalesce->rx_coalesce_usecs);
419         if (rc)
420                 return rc;
421
422         ena_update_rx_rings_nonadaptive_intr_moderation(adapter);
423
424         if (coalesce->use_adaptive_rx_coalesce &&
425             !ena_com_get_adaptive_moderation_enabled(ena_dev))
426                 ena_com_enable_adaptive_moderation(ena_dev);
427
428         if (!coalesce->use_adaptive_rx_coalesce &&
429             ena_com_get_adaptive_moderation_enabled(ena_dev))
430                 ena_com_disable_adaptive_moderation(ena_dev);
431
432         return 0;
433 }
434
435 static u32 ena_get_msglevel(struct net_device *netdev)
436 {
437         struct ena_adapter *adapter = netdev_priv(netdev);
438
439         return adapter->msg_enable;
440 }
441
442 static void ena_set_msglevel(struct net_device *netdev, u32 value)
443 {
444         struct ena_adapter *adapter = netdev_priv(netdev);
445
446         adapter->msg_enable = value;
447 }
448
449 static void ena_get_drvinfo(struct net_device *dev,
450                             struct ethtool_drvinfo *info)
451 {
452         struct ena_adapter *adapter = netdev_priv(dev);
453
454         strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
455         strlcpy(info->bus_info, pci_name(adapter->pdev),
456                 sizeof(info->bus_info));
457 }
458
459 static void ena_get_ringparam(struct net_device *netdev,
460                               struct ethtool_ringparam *ring)
461 {
462         struct ena_adapter *adapter = netdev_priv(netdev);
463
464         ring->tx_max_pending = adapter->max_tx_ring_size;
465         ring->rx_max_pending = adapter->max_rx_ring_size;
466         ring->tx_pending = adapter->tx_ring[0].ring_size;
467         ring->rx_pending = adapter->rx_ring[0].ring_size;
468 }
469
470 static int ena_set_ringparam(struct net_device *netdev,
471                              struct ethtool_ringparam *ring)
472 {
473         struct ena_adapter *adapter = netdev_priv(netdev);
474         u32 new_tx_size, new_rx_size;
475
476         new_tx_size = ring->tx_pending < ENA_MIN_RING_SIZE ?
477                         ENA_MIN_RING_SIZE : ring->tx_pending;
478         new_tx_size = rounddown_pow_of_two(new_tx_size);
479
480         new_rx_size = ring->rx_pending < ENA_MIN_RING_SIZE ?
481                         ENA_MIN_RING_SIZE : ring->rx_pending;
482         new_rx_size = rounddown_pow_of_two(new_rx_size);
483
484         if (new_tx_size == adapter->requested_tx_ring_size &&
485             new_rx_size == adapter->requested_rx_ring_size)
486                 return 0;
487
488         return ena_update_queue_sizes(adapter, new_tx_size, new_rx_size);
489 }
490
491 static u32 ena_flow_hash_to_flow_type(u16 hash_fields)
492 {
493         u32 data = 0;
494
495         if (hash_fields & ENA_ADMIN_RSS_L2_DA)
496                 data |= RXH_L2DA;
497
498         if (hash_fields & ENA_ADMIN_RSS_L3_DA)
499                 data |= RXH_IP_DST;
500
501         if (hash_fields & ENA_ADMIN_RSS_L3_SA)
502                 data |= RXH_IP_SRC;
503
504         if (hash_fields & ENA_ADMIN_RSS_L4_DP)
505                 data |= RXH_L4_B_2_3;
506
507         if (hash_fields & ENA_ADMIN_RSS_L4_SP)
508                 data |= RXH_L4_B_0_1;
509
510         return data;
511 }
512
513 static u16 ena_flow_data_to_flow_hash(u32 hash_fields)
514 {
515         u16 data = 0;
516
517         if (hash_fields & RXH_L2DA)
518                 data |= ENA_ADMIN_RSS_L2_DA;
519
520         if (hash_fields & RXH_IP_DST)
521                 data |= ENA_ADMIN_RSS_L3_DA;
522
523         if (hash_fields & RXH_IP_SRC)
524                 data |= ENA_ADMIN_RSS_L3_SA;
525
526         if (hash_fields & RXH_L4_B_2_3)
527                 data |= ENA_ADMIN_RSS_L4_DP;
528
529         if (hash_fields & RXH_L4_B_0_1)
530                 data |= ENA_ADMIN_RSS_L4_SP;
531
532         return data;
533 }
534
535 static int ena_get_rss_hash(struct ena_com_dev *ena_dev,
536                             struct ethtool_rxnfc *cmd)
537 {
538         enum ena_admin_flow_hash_proto proto;
539         u16 hash_fields;
540         int rc;
541
542         cmd->data = 0;
543
544         switch (cmd->flow_type) {
545         case TCP_V4_FLOW:
546                 proto = ENA_ADMIN_RSS_TCP4;
547                 break;
548         case UDP_V4_FLOW:
549                 proto = ENA_ADMIN_RSS_UDP4;
550                 break;
551         case TCP_V6_FLOW:
552                 proto = ENA_ADMIN_RSS_TCP6;
553                 break;
554         case UDP_V6_FLOW:
555                 proto = ENA_ADMIN_RSS_UDP6;
556                 break;
557         case IPV4_FLOW:
558                 proto = ENA_ADMIN_RSS_IP4;
559                 break;
560         case IPV6_FLOW:
561                 proto = ENA_ADMIN_RSS_IP6;
562                 break;
563         case ETHER_FLOW:
564                 proto = ENA_ADMIN_RSS_NOT_IP;
565                 break;
566         case AH_V4_FLOW:
567         case ESP_V4_FLOW:
568         case AH_V6_FLOW:
569         case ESP_V6_FLOW:
570         case SCTP_V4_FLOW:
571         case AH_ESP_V4_FLOW:
572                 return -EOPNOTSUPP;
573         default:
574                 return -EINVAL;
575         }
576
577         rc = ena_com_get_hash_ctrl(ena_dev, proto, &hash_fields);
578         if (rc)
579                 return rc;
580
581         cmd->data = ena_flow_hash_to_flow_type(hash_fields);
582
583         return 0;
584 }
585
586 static int ena_set_rss_hash(struct ena_com_dev *ena_dev,
587                             struct ethtool_rxnfc *cmd)
588 {
589         enum ena_admin_flow_hash_proto proto;
590         u16 hash_fields;
591
592         switch (cmd->flow_type) {
593         case TCP_V4_FLOW:
594                 proto = ENA_ADMIN_RSS_TCP4;
595                 break;
596         case UDP_V4_FLOW:
597                 proto = ENA_ADMIN_RSS_UDP4;
598                 break;
599         case TCP_V6_FLOW:
600                 proto = ENA_ADMIN_RSS_TCP6;
601                 break;
602         case UDP_V6_FLOW:
603                 proto = ENA_ADMIN_RSS_UDP6;
604                 break;
605         case IPV4_FLOW:
606                 proto = ENA_ADMIN_RSS_IP4;
607                 break;
608         case IPV6_FLOW:
609                 proto = ENA_ADMIN_RSS_IP6;
610                 break;
611         case ETHER_FLOW:
612                 proto = ENA_ADMIN_RSS_NOT_IP;
613                 break;
614         case AH_V4_FLOW:
615         case ESP_V4_FLOW:
616         case AH_V6_FLOW:
617         case ESP_V6_FLOW:
618         case SCTP_V4_FLOW:
619         case AH_ESP_V4_FLOW:
620                 return -EOPNOTSUPP;
621         default:
622                 return -EINVAL;
623         }
624
625         hash_fields = ena_flow_data_to_flow_hash(cmd->data);
626
627         return ena_com_fill_hash_ctrl(ena_dev, proto, hash_fields);
628 }
629
630 static int ena_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info)
631 {
632         struct ena_adapter *adapter = netdev_priv(netdev);
633         int rc = 0;
634
635         switch (info->cmd) {
636         case ETHTOOL_SRXFH:
637                 rc = ena_set_rss_hash(adapter->ena_dev, info);
638                 break;
639         case ETHTOOL_SRXCLSRLDEL:
640         case ETHTOOL_SRXCLSRLINS:
641         default:
642                 netif_err(adapter, drv, netdev,
643                           "Command parameter %d is not supported\n", info->cmd);
644                 rc = -EOPNOTSUPP;
645         }
646
647         return rc;
648 }
649
650 static int ena_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
651                          u32 *rules)
652 {
653         struct ena_adapter *adapter = netdev_priv(netdev);
654         int rc = 0;
655
656         switch (info->cmd) {
657         case ETHTOOL_GRXRINGS:
658                 info->data = adapter->num_io_queues;
659                 rc = 0;
660                 break;
661         case ETHTOOL_GRXFH:
662                 rc = ena_get_rss_hash(adapter->ena_dev, info);
663                 break;
664         case ETHTOOL_GRXCLSRLCNT:
665         case ETHTOOL_GRXCLSRULE:
666         case ETHTOOL_GRXCLSRLALL:
667         default:
668                 netif_err(adapter, drv, netdev,
669                           "Command parameter %d is not supported\n", info->cmd);
670                 rc = -EOPNOTSUPP;
671         }
672
673         return rc;
674 }
675
676 static u32 ena_get_rxfh_indir_size(struct net_device *netdev)
677 {
678         return ENA_RX_RSS_TABLE_SIZE;
679 }
680
681 static u32 ena_get_rxfh_key_size(struct net_device *netdev)
682 {
683         return ENA_HASH_KEY_SIZE;
684 }
685
686 static int ena_indirection_table_set(struct ena_adapter *adapter,
687                                      const u32 *indir)
688 {
689         struct ena_com_dev *ena_dev = adapter->ena_dev;
690         int i, rc;
691
692         for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
693                 rc = ena_com_indirect_table_fill_entry(ena_dev,
694                                                        i,
695                                                        ENA_IO_RXQ_IDX(indir[i]));
696                 if (unlikely(rc)) {
697                         netif_err(adapter, drv, adapter->netdev,
698                                   "Cannot fill indirect table (index is too large)\n");
699                         return rc;
700                 }
701         }
702
703         rc = ena_com_indirect_table_set(ena_dev);
704         if (rc) {
705                 netif_err(adapter, drv, adapter->netdev,
706                           "Cannot set indirect table\n");
707                 return rc == -EPERM ? -EOPNOTSUPP : rc;
708         }
709         return rc;
710 }
711
712 static int ena_indirection_table_get(struct ena_adapter *adapter, u32 *indir)
713 {
714         struct ena_com_dev *ena_dev = adapter->ena_dev;
715         int i, rc;
716
717         if (!indir)
718                 return 0;
719
720         rc = ena_com_indirect_table_get(ena_dev, indir);
721         if (rc)
722                 return rc;
723
724         /* Our internal representation of the indices is: even indices
725          * for Tx and uneven indices for Rx. We need to convert the Rx
726          * indices to be consecutive
727          */
728         for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++)
729                 indir[i] = ENA_IO_RXQ_IDX_TO_COMBINED_IDX(indir[i]);
730
731         return rc;
732 }
733
734 static int ena_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
735                         u8 *hfunc)
736 {
737         struct ena_adapter *adapter = netdev_priv(netdev);
738         enum ena_admin_hash_functions ena_func;
739         u8 func;
740         int rc;
741
742         rc = ena_indirection_table_get(adapter, indir);
743         if (rc)
744                 return rc;
745
746         /* We call this function in order to check if the device
747          * supports getting/setting the hash function.
748          */
749         rc = ena_com_get_hash_function(adapter->ena_dev, &ena_func);
750         if (rc) {
751                 if (rc == -EOPNOTSUPP)
752                         rc = 0;
753
754                 return rc;
755         }
756
757         rc = ena_com_get_hash_key(adapter->ena_dev, key);
758         if (rc)
759                 return rc;
760
761         switch (ena_func) {
762         case ENA_ADMIN_TOEPLITZ:
763                 func = ETH_RSS_HASH_TOP;
764                 break;
765         case ENA_ADMIN_CRC32:
766                 func = ETH_RSS_HASH_CRC32;
767                 break;
768         default:
769                 netif_err(adapter, drv, netdev,
770                           "Command parameter is not supported\n");
771                 return -EOPNOTSUPP;
772         }
773
774         if (hfunc)
775                 *hfunc = func;
776
777         return 0;
778 }
779
780 static int ena_set_rxfh(struct net_device *netdev, const u32 *indir,
781                         const u8 *key, const u8 hfunc)
782 {
783         struct ena_adapter *adapter = netdev_priv(netdev);
784         struct ena_com_dev *ena_dev = adapter->ena_dev;
785         enum ena_admin_hash_functions func = 0;
786         int rc;
787
788         if (indir) {
789                 rc = ena_indirection_table_set(adapter, indir);
790                 if (rc)
791                         return rc;
792         }
793
794         switch (hfunc) {
795         case ETH_RSS_HASH_NO_CHANGE:
796                 func = ena_com_get_current_hash_function(ena_dev);
797                 break;
798         case ETH_RSS_HASH_TOP:
799                 func = ENA_ADMIN_TOEPLITZ;
800                 break;
801         case ETH_RSS_HASH_CRC32:
802                 func = ENA_ADMIN_CRC32;
803                 break;
804         default:
805                 netif_err(adapter, drv, netdev, "Unsupported hfunc %d\n",
806                           hfunc);
807                 return -EOPNOTSUPP;
808         }
809
810         if (key || func) {
811                 rc = ena_com_fill_hash_function(ena_dev, func, key,
812                                                 ENA_HASH_KEY_SIZE,
813                                                 0xFFFFFFFF);
814                 if (unlikely(rc)) {
815                         netif_err(adapter, drv, netdev, "Cannot fill key\n");
816                         return rc == -EPERM ? -EOPNOTSUPP : rc;
817                 }
818         }
819
820         return 0;
821 }
822
823 static void ena_get_channels(struct net_device *netdev,
824                              struct ethtool_channels *channels)
825 {
826         struct ena_adapter *adapter = netdev_priv(netdev);
827
828         channels->max_combined = adapter->max_num_io_queues;
829         channels->combined_count = adapter->num_io_queues;
830 }
831
832 static int ena_set_channels(struct net_device *netdev,
833                             struct ethtool_channels *channels)
834 {
835         struct ena_adapter *adapter = netdev_priv(netdev);
836         u32 count = channels->combined_count;
837         /* The check for max value is already done in ethtool */
838         if (count < ENA_MIN_NUM_IO_QUEUES ||
839             (ena_xdp_present(adapter) &&
840             !ena_xdp_legal_queue_count(adapter, count)))
841                 return -EINVAL;
842
843         return ena_update_queue_count(adapter, count);
844 }
845
846 static int ena_get_tunable(struct net_device *netdev,
847                            const struct ethtool_tunable *tuna, void *data)
848 {
849         struct ena_adapter *adapter = netdev_priv(netdev);
850         int ret = 0;
851
852         switch (tuna->id) {
853         case ETHTOOL_RX_COPYBREAK:
854                 *(u32 *)data = adapter->rx_copybreak;
855                 break;
856         default:
857                 ret = -EINVAL;
858                 break;
859         }
860
861         return ret;
862 }
863
864 static int ena_set_tunable(struct net_device *netdev,
865                            const struct ethtool_tunable *tuna,
866                            const void *data)
867 {
868         struct ena_adapter *adapter = netdev_priv(netdev);
869         int ret = 0;
870         u32 len;
871
872         switch (tuna->id) {
873         case ETHTOOL_RX_COPYBREAK:
874                 len = *(u32 *)data;
875                 if (len > adapter->netdev->mtu) {
876                         ret = -EINVAL;
877                         break;
878                 }
879                 adapter->rx_copybreak = len;
880                 break;
881         default:
882                 ret = -EINVAL;
883                 break;
884         }
885
886         return ret;
887 }
888
889 static const struct ethtool_ops ena_ethtool_ops = {
890         .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
891                                      ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
892         .get_link_ksettings     = ena_get_link_ksettings,
893         .get_drvinfo            = ena_get_drvinfo,
894         .get_msglevel           = ena_get_msglevel,
895         .set_msglevel           = ena_set_msglevel,
896         .get_link               = ethtool_op_get_link,
897         .get_coalesce           = ena_get_coalesce,
898         .set_coalesce           = ena_set_coalesce,
899         .get_ringparam          = ena_get_ringparam,
900         .set_ringparam          = ena_set_ringparam,
901         .get_sset_count         = ena_get_sset_count,
902         .get_strings            = ena_get_ethtool_strings,
903         .get_ethtool_stats      = ena_get_ethtool_stats,
904         .get_rxnfc              = ena_get_rxnfc,
905         .set_rxnfc              = ena_set_rxnfc,
906         .get_rxfh_indir_size    = ena_get_rxfh_indir_size,
907         .get_rxfh_key_size      = ena_get_rxfh_key_size,
908         .get_rxfh               = ena_get_rxfh,
909         .set_rxfh               = ena_set_rxfh,
910         .get_channels           = ena_get_channels,
911         .set_channels           = ena_set_channels,
912         .get_tunable            = ena_get_tunable,
913         .set_tunable            = ena_set_tunable,
914         .get_ts_info            = ethtool_op_get_ts_info,
915 };
916
917 void ena_set_ethtool_ops(struct net_device *netdev)
918 {
919         netdev->ethtool_ops = &ena_ethtool_ops;
920 }
921
922 static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
923 {
924         struct net_device *netdev = adapter->netdev;
925         u8 *strings_buf;
926         u64 *data_buf;
927         int strings_num;
928         int i, rc;
929
930         strings_num = ena_get_sw_stats_count(adapter);
931         if (strings_num <= 0) {
932                 netif_err(adapter, drv, netdev, "Can't get stats num\n");
933                 return;
934         }
935
936         strings_buf = devm_kcalloc(&adapter->pdev->dev,
937                                    ETH_GSTRING_LEN, strings_num,
938                                    GFP_ATOMIC);
939         if (!strings_buf) {
940                 netif_err(adapter, drv, netdev,
941                           "Failed to allocate strings_buf\n");
942                 return;
943         }
944
945         data_buf = devm_kcalloc(&adapter->pdev->dev,
946                                 strings_num, sizeof(u64),
947                                 GFP_ATOMIC);
948         if (!data_buf) {
949                 netif_err(adapter, drv, netdev,
950                           "Failed to allocate data buf\n");
951                 devm_kfree(&adapter->pdev->dev, strings_buf);
952                 return;
953         }
954
955         ena_get_strings(adapter, strings_buf, false);
956         ena_get_stats(adapter, data_buf, false);
957
958         /* If there is a buffer, dump stats, otherwise print them to dmesg */
959         if (buf)
960                 for (i = 0; i < strings_num; i++) {
961                         rc = snprintf(buf, ETH_GSTRING_LEN + sizeof(u64),
962                                       "%s %llu\n",
963                                       strings_buf + i * ETH_GSTRING_LEN,
964                                       data_buf[i]);
965                         buf += rc;
966                 }
967         else
968                 for (i = 0; i < strings_num; i++)
969                         netif_err(adapter, drv, netdev, "%s: %llu\n",
970                                   strings_buf + i * ETH_GSTRING_LEN,
971                                   data_buf[i]);
972
973         devm_kfree(&adapter->pdev->dev, strings_buf);
974         devm_kfree(&adapter->pdev->dev, data_buf);
975 }
976
977 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf)
978 {
979         if (!buf)
980                 return;
981
982         ena_dump_stats_ex(adapter, buf);
983 }
984
985 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter)
986 {
987         ena_dump_stats_ex(adapter, NULL);
988 }
This page took 0.087204 seconds and 4 git commands to generate.