]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/net/ethernet/amazon/ena/ena_ethtool.c
net: ena: change return value for unsupported features unsupported return value
[mirror_ubuntu-zesty-kernel.git] / drivers / net / ethernet / amazon / ena / ena_ethtool.c
1 /*
2 * Copyright 2015 Amazon.com, Inc. or its affiliates.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33 #include <linux/pci.h>
34
35 #include "ena_netdev.h"
36
37 struct ena_stats {
38 char name[ETH_GSTRING_LEN];
39 int stat_offset;
40 };
41
42 #define ENA_STAT_ENA_COM_ENTRY(stat) { \
43 .name = #stat, \
44 .stat_offset = offsetof(struct ena_com_stats_admin, stat) \
45 }
46
47 #define ENA_STAT_ENTRY(stat, stat_type) { \
48 .name = #stat, \
49 .stat_offset = offsetof(struct ena_stats_##stat_type, stat) \
50 }
51
52 #define ENA_STAT_RX_ENTRY(stat) \
53 ENA_STAT_ENTRY(stat, rx)
54
55 #define ENA_STAT_TX_ENTRY(stat) \
56 ENA_STAT_ENTRY(stat, tx)
57
58 #define ENA_STAT_GLOBAL_ENTRY(stat) \
59 ENA_STAT_ENTRY(stat, dev)
60
61 static const struct ena_stats ena_stats_global_strings[] = {
62 ENA_STAT_GLOBAL_ENTRY(tx_timeout),
63 ENA_STAT_GLOBAL_ENTRY(io_suspend),
64 ENA_STAT_GLOBAL_ENTRY(io_resume),
65 ENA_STAT_GLOBAL_ENTRY(wd_expired),
66 ENA_STAT_GLOBAL_ENTRY(interface_up),
67 ENA_STAT_GLOBAL_ENTRY(interface_down),
68 ENA_STAT_GLOBAL_ENTRY(admin_q_pause),
69 };
70
71 static const struct ena_stats ena_stats_tx_strings[] = {
72 ENA_STAT_TX_ENTRY(cnt),
73 ENA_STAT_TX_ENTRY(bytes),
74 ENA_STAT_TX_ENTRY(queue_stop),
75 ENA_STAT_TX_ENTRY(queue_wakeup),
76 ENA_STAT_TX_ENTRY(dma_mapping_err),
77 ENA_STAT_TX_ENTRY(linearize),
78 ENA_STAT_TX_ENTRY(linearize_failed),
79 ENA_STAT_TX_ENTRY(napi_comp),
80 ENA_STAT_TX_ENTRY(tx_poll),
81 ENA_STAT_TX_ENTRY(doorbells),
82 ENA_STAT_TX_ENTRY(prepare_ctx_err),
83 ENA_STAT_TX_ENTRY(bad_req_id),
84 };
85
86 static const struct ena_stats ena_stats_rx_strings[] = {
87 ENA_STAT_RX_ENTRY(cnt),
88 ENA_STAT_RX_ENTRY(bytes),
89 ENA_STAT_RX_ENTRY(refil_partial),
90 ENA_STAT_RX_ENTRY(bad_csum),
91 ENA_STAT_RX_ENTRY(page_alloc_fail),
92 ENA_STAT_RX_ENTRY(skb_alloc_fail),
93 ENA_STAT_RX_ENTRY(dma_mapping_err),
94 ENA_STAT_RX_ENTRY(bad_desc_num),
95 ENA_STAT_RX_ENTRY(rx_copybreak_pkt),
96 ENA_STAT_RX_ENTRY(empty_rx_ring),
97 };
98
99 static const struct ena_stats ena_stats_ena_com_strings[] = {
100 ENA_STAT_ENA_COM_ENTRY(aborted_cmd),
101 ENA_STAT_ENA_COM_ENTRY(submitted_cmd),
102 ENA_STAT_ENA_COM_ENTRY(completed_cmd),
103 ENA_STAT_ENA_COM_ENTRY(out_of_space),
104 ENA_STAT_ENA_COM_ENTRY(no_completion),
105 };
106
107 #define ENA_STATS_ARRAY_GLOBAL ARRAY_SIZE(ena_stats_global_strings)
108 #define ENA_STATS_ARRAY_TX ARRAY_SIZE(ena_stats_tx_strings)
109 #define ENA_STATS_ARRAY_RX ARRAY_SIZE(ena_stats_rx_strings)
110 #define ENA_STATS_ARRAY_ENA_COM ARRAY_SIZE(ena_stats_ena_com_strings)
111
112 static void ena_safe_update_stat(u64 *src, u64 *dst,
113 struct u64_stats_sync *syncp)
114 {
115 unsigned int start;
116
117 do {
118 start = u64_stats_fetch_begin_irq(syncp);
119 *(dst) = *src;
120 } while (u64_stats_fetch_retry_irq(syncp, start));
121 }
122
123 static void ena_queue_stats(struct ena_adapter *adapter, u64 **data)
124 {
125 const struct ena_stats *ena_stats;
126 struct ena_ring *ring;
127
128 u64 *ptr;
129 int i, j;
130
131 for (i = 0; i < adapter->num_queues; i++) {
132 /* Tx stats */
133 ring = &adapter->tx_ring[i];
134
135 for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
136 ena_stats = &ena_stats_tx_strings[j];
137
138 ptr = (u64 *)((uintptr_t)&ring->tx_stats +
139 (uintptr_t)ena_stats->stat_offset);
140
141 ena_safe_update_stat(ptr, (*data)++, &ring->syncp);
142 }
143
144 /* Rx stats */
145 ring = &adapter->rx_ring[i];
146
147 for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
148 ena_stats = &ena_stats_rx_strings[j];
149
150 ptr = (u64 *)((uintptr_t)&ring->rx_stats +
151 (uintptr_t)ena_stats->stat_offset);
152
153 ena_safe_update_stat(ptr, (*data)++, &ring->syncp);
154 }
155 }
156 }
157
158 static void ena_dev_admin_queue_stats(struct ena_adapter *adapter, u64 **data)
159 {
160 const struct ena_stats *ena_stats;
161 u32 *ptr;
162 int i;
163
164 for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
165 ena_stats = &ena_stats_ena_com_strings[i];
166
167 ptr = (u32 *)((uintptr_t)&adapter->ena_dev->admin_queue.stats +
168 (uintptr_t)ena_stats->stat_offset);
169
170 *(*data)++ = *ptr;
171 }
172 }
173
174 static void ena_get_ethtool_stats(struct net_device *netdev,
175 struct ethtool_stats *stats,
176 u64 *data)
177 {
178 struct ena_adapter *adapter = netdev_priv(netdev);
179 const struct ena_stats *ena_stats;
180 u64 *ptr;
181 int i;
182
183 for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
184 ena_stats = &ena_stats_global_strings[i];
185
186 ptr = (u64 *)((uintptr_t)&adapter->dev_stats +
187 (uintptr_t)ena_stats->stat_offset);
188
189 ena_safe_update_stat(ptr, data++, &adapter->syncp);
190 }
191
192 ena_queue_stats(adapter, &data);
193 ena_dev_admin_queue_stats(adapter, &data);
194 }
195
196 int ena_get_sset_count(struct net_device *netdev, int sset)
197 {
198 struct ena_adapter *adapter = netdev_priv(netdev);
199
200 if (sset != ETH_SS_STATS)
201 return -EOPNOTSUPP;
202
203 return adapter->num_queues * (ENA_STATS_ARRAY_TX + ENA_STATS_ARRAY_RX)
204 + ENA_STATS_ARRAY_GLOBAL + ENA_STATS_ARRAY_ENA_COM;
205 }
206
207 static void ena_queue_strings(struct ena_adapter *adapter, u8 **data)
208 {
209 const struct ena_stats *ena_stats;
210 int i, j;
211
212 for (i = 0; i < adapter->num_queues; i++) {
213 /* Tx stats */
214 for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
215 ena_stats = &ena_stats_tx_strings[j];
216
217 snprintf(*data, ETH_GSTRING_LEN,
218 "queue_%u_tx_%s", i, ena_stats->name);
219 (*data) += ETH_GSTRING_LEN;
220 }
221 /* Rx stats */
222 for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
223 ena_stats = &ena_stats_rx_strings[j];
224
225 snprintf(*data, ETH_GSTRING_LEN,
226 "queue_%u_rx_%s", i, ena_stats->name);
227 (*data) += ETH_GSTRING_LEN;
228 }
229 }
230 }
231
232 static void ena_com_dev_strings(u8 **data)
233 {
234 const struct ena_stats *ena_stats;
235 int i;
236
237 for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
238 ena_stats = &ena_stats_ena_com_strings[i];
239
240 snprintf(*data, ETH_GSTRING_LEN,
241 "ena_admin_q_%s", ena_stats->name);
242 (*data) += ETH_GSTRING_LEN;
243 }
244 }
245
246 static void ena_get_strings(struct net_device *netdev, u32 sset, u8 *data)
247 {
248 struct ena_adapter *adapter = netdev_priv(netdev);
249 const struct ena_stats *ena_stats;
250 int i;
251
252 if (sset != ETH_SS_STATS)
253 return;
254
255 for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
256 ena_stats = &ena_stats_global_strings[i];
257
258 memcpy(data, ena_stats->name, ETH_GSTRING_LEN);
259 data += ETH_GSTRING_LEN;
260 }
261
262 ena_queue_strings(adapter, &data);
263 ena_com_dev_strings(&data);
264 }
265
266 static int ena_get_link_ksettings(struct net_device *netdev,
267 struct ethtool_link_ksettings *link_ksettings)
268 {
269 struct ena_adapter *adapter = netdev_priv(netdev);
270 struct ena_com_dev *ena_dev = adapter->ena_dev;
271 struct ena_admin_get_feature_link_desc *link;
272 struct ena_admin_get_feat_resp feat_resp;
273 int rc;
274
275 rc = ena_com_get_link_params(ena_dev, &feat_resp);
276 if (rc)
277 return rc;
278
279 link = &feat_resp.u.link;
280 link_ksettings->base.speed = link->speed;
281
282 if (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) {
283 ethtool_link_ksettings_add_link_mode(link_ksettings,
284 supported, Autoneg);
285 ethtool_link_ksettings_add_link_mode(link_ksettings,
286 supported, Autoneg);
287 }
288
289 link_ksettings->base.autoneg =
290 (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) ?
291 AUTONEG_ENABLE : AUTONEG_DISABLE;
292
293 link_ksettings->base.duplex = DUPLEX_FULL;
294
295 return 0;
296 }
297
298 static int ena_get_coalesce(struct net_device *net_dev,
299 struct ethtool_coalesce *coalesce)
300 {
301 struct ena_adapter *adapter = netdev_priv(net_dev);
302 struct ena_com_dev *ena_dev = adapter->ena_dev;
303 struct ena_intr_moder_entry intr_moder_entry;
304
305 if (!ena_com_interrupt_moderation_supported(ena_dev)) {
306 /* the devie doesn't support interrupt moderation */
307 return -EOPNOTSUPP;
308 }
309 coalesce->tx_coalesce_usecs =
310 ena_com_get_nonadaptive_moderation_interval_tx(ena_dev) /
311 ena_dev->intr_delay_resolution;
312 if (!ena_com_get_adaptive_moderation_enabled(ena_dev)) {
313 coalesce->rx_coalesce_usecs =
314 ena_com_get_nonadaptive_moderation_interval_rx(ena_dev)
315 / ena_dev->intr_delay_resolution;
316 } else {
317 ena_com_get_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_LOWEST, &intr_moder_entry);
318 coalesce->rx_coalesce_usecs_low = intr_moder_entry.intr_moder_interval;
319 coalesce->rx_max_coalesced_frames_low = intr_moder_entry.pkts_per_interval;
320
321 ena_com_get_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_MID, &intr_moder_entry);
322 coalesce->rx_coalesce_usecs = intr_moder_entry.intr_moder_interval;
323 coalesce->rx_max_coalesced_frames = intr_moder_entry.pkts_per_interval;
324
325 ena_com_get_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_HIGHEST, &intr_moder_entry);
326 coalesce->rx_coalesce_usecs_high = intr_moder_entry.intr_moder_interval;
327 coalesce->rx_max_coalesced_frames_high = intr_moder_entry.pkts_per_interval;
328 }
329 coalesce->use_adaptive_rx_coalesce =
330 ena_com_get_adaptive_moderation_enabled(ena_dev);
331
332 return 0;
333 }
334
335 static void ena_update_tx_rings_intr_moderation(struct ena_adapter *adapter)
336 {
337 unsigned int val;
338 int i;
339
340 val = ena_com_get_nonadaptive_moderation_interval_tx(adapter->ena_dev);
341
342 for (i = 0; i < adapter->num_queues; i++)
343 adapter->tx_ring[i].smoothed_interval = val;
344 }
345
346 static int ena_set_coalesce(struct net_device *net_dev,
347 struct ethtool_coalesce *coalesce)
348 {
349 struct ena_adapter *adapter = netdev_priv(net_dev);
350 struct ena_com_dev *ena_dev = adapter->ena_dev;
351 struct ena_intr_moder_entry intr_moder_entry;
352 int rc;
353
354 if (!ena_com_interrupt_moderation_supported(ena_dev)) {
355 /* the devie doesn't support interrupt moderation */
356 return -EOPNOTSUPP;
357 }
358
359 if (coalesce->rx_coalesce_usecs_irq ||
360 coalesce->rx_max_coalesced_frames_irq ||
361 coalesce->tx_coalesce_usecs_irq ||
362 coalesce->tx_max_coalesced_frames ||
363 coalesce->tx_max_coalesced_frames_irq ||
364 coalesce->stats_block_coalesce_usecs ||
365 coalesce->use_adaptive_tx_coalesce ||
366 coalesce->pkt_rate_low ||
367 coalesce->tx_coalesce_usecs_low ||
368 coalesce->tx_max_coalesced_frames_low ||
369 coalesce->pkt_rate_high ||
370 coalesce->tx_coalesce_usecs_high ||
371 coalesce->tx_max_coalesced_frames_high ||
372 coalesce->rate_sample_interval)
373 return -EINVAL;
374
375 rc = ena_com_update_nonadaptive_moderation_interval_tx(ena_dev,
376 coalesce->tx_coalesce_usecs);
377 if (rc)
378 return rc;
379
380 ena_update_tx_rings_intr_moderation(adapter);
381
382 if (ena_com_get_adaptive_moderation_enabled(ena_dev)) {
383 if (!coalesce->use_adaptive_rx_coalesce) {
384 ena_com_disable_adaptive_moderation(ena_dev);
385 rc = ena_com_update_nonadaptive_moderation_interval_rx(ena_dev,
386 coalesce->rx_coalesce_usecs);
387 return rc;
388 }
389 } else { /* was in non-adaptive mode */
390 if (coalesce->use_adaptive_rx_coalesce) {
391 ena_com_enable_adaptive_moderation(ena_dev);
392 } else {
393 rc = ena_com_update_nonadaptive_moderation_interval_rx(ena_dev,
394 coalesce->rx_coalesce_usecs);
395 return rc;
396 }
397 }
398
399 intr_moder_entry.intr_moder_interval = coalesce->rx_coalesce_usecs_low;
400 intr_moder_entry.pkts_per_interval = coalesce->rx_max_coalesced_frames_low;
401 intr_moder_entry.bytes_per_interval = ENA_INTR_BYTE_COUNT_NOT_SUPPORTED;
402 ena_com_init_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_LOWEST, &intr_moder_entry);
403
404 intr_moder_entry.intr_moder_interval = coalesce->rx_coalesce_usecs;
405 intr_moder_entry.pkts_per_interval = coalesce->rx_max_coalesced_frames;
406 intr_moder_entry.bytes_per_interval = ENA_INTR_BYTE_COUNT_NOT_SUPPORTED;
407 ena_com_init_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_MID, &intr_moder_entry);
408
409 intr_moder_entry.intr_moder_interval = coalesce->rx_coalesce_usecs_high;
410 intr_moder_entry.pkts_per_interval = coalesce->rx_max_coalesced_frames_high;
411 intr_moder_entry.bytes_per_interval = ENA_INTR_BYTE_COUNT_NOT_SUPPORTED;
412 ena_com_init_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_HIGHEST, &intr_moder_entry);
413
414 return 0;
415 }
416
417 static u32 ena_get_msglevel(struct net_device *netdev)
418 {
419 struct ena_adapter *adapter = netdev_priv(netdev);
420
421 return adapter->msg_enable;
422 }
423
424 static void ena_set_msglevel(struct net_device *netdev, u32 value)
425 {
426 struct ena_adapter *adapter = netdev_priv(netdev);
427
428 adapter->msg_enable = value;
429 }
430
431 static void ena_get_drvinfo(struct net_device *dev,
432 struct ethtool_drvinfo *info)
433 {
434 struct ena_adapter *adapter = netdev_priv(dev);
435
436 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
437 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
438 strlcpy(info->bus_info, pci_name(adapter->pdev),
439 sizeof(info->bus_info));
440 }
441
442 static void ena_get_ringparam(struct net_device *netdev,
443 struct ethtool_ringparam *ring)
444 {
445 struct ena_adapter *adapter = netdev_priv(netdev);
446 struct ena_ring *tx_ring = &adapter->tx_ring[0];
447 struct ena_ring *rx_ring = &adapter->rx_ring[0];
448
449 ring->rx_max_pending = rx_ring->ring_size;
450 ring->tx_max_pending = tx_ring->ring_size;
451 ring->rx_pending = rx_ring->ring_size;
452 ring->tx_pending = tx_ring->ring_size;
453 }
454
455 static u32 ena_flow_hash_to_flow_type(u16 hash_fields)
456 {
457 u32 data = 0;
458
459 if (hash_fields & ENA_ADMIN_RSS_L2_DA)
460 data |= RXH_L2DA;
461
462 if (hash_fields & ENA_ADMIN_RSS_L3_DA)
463 data |= RXH_IP_DST;
464
465 if (hash_fields & ENA_ADMIN_RSS_L3_SA)
466 data |= RXH_IP_SRC;
467
468 if (hash_fields & ENA_ADMIN_RSS_L4_DP)
469 data |= RXH_L4_B_2_3;
470
471 if (hash_fields & ENA_ADMIN_RSS_L4_SP)
472 data |= RXH_L4_B_0_1;
473
474 return data;
475 }
476
477 static u16 ena_flow_data_to_flow_hash(u32 hash_fields)
478 {
479 u16 data = 0;
480
481 if (hash_fields & RXH_L2DA)
482 data |= ENA_ADMIN_RSS_L2_DA;
483
484 if (hash_fields & RXH_IP_DST)
485 data |= ENA_ADMIN_RSS_L3_DA;
486
487 if (hash_fields & RXH_IP_SRC)
488 data |= ENA_ADMIN_RSS_L3_SA;
489
490 if (hash_fields & RXH_L4_B_2_3)
491 data |= ENA_ADMIN_RSS_L4_DP;
492
493 if (hash_fields & RXH_L4_B_0_1)
494 data |= ENA_ADMIN_RSS_L4_SP;
495
496 return data;
497 }
498
499 static int ena_get_rss_hash(struct ena_com_dev *ena_dev,
500 struct ethtool_rxnfc *cmd)
501 {
502 enum ena_admin_flow_hash_proto proto;
503 u16 hash_fields;
504 int rc;
505
506 cmd->data = 0;
507
508 switch (cmd->flow_type) {
509 case TCP_V4_FLOW:
510 proto = ENA_ADMIN_RSS_TCP4;
511 break;
512 case UDP_V4_FLOW:
513 proto = ENA_ADMIN_RSS_UDP4;
514 break;
515 case TCP_V6_FLOW:
516 proto = ENA_ADMIN_RSS_TCP6;
517 break;
518 case UDP_V6_FLOW:
519 proto = ENA_ADMIN_RSS_UDP6;
520 break;
521 case IPV4_FLOW:
522 proto = ENA_ADMIN_RSS_IP4;
523 break;
524 case IPV6_FLOW:
525 proto = ENA_ADMIN_RSS_IP6;
526 break;
527 case ETHER_FLOW:
528 proto = ENA_ADMIN_RSS_NOT_IP;
529 break;
530 case AH_V4_FLOW:
531 case ESP_V4_FLOW:
532 case AH_V6_FLOW:
533 case ESP_V6_FLOW:
534 case SCTP_V4_FLOW:
535 case AH_ESP_V4_FLOW:
536 return -EOPNOTSUPP;
537 default:
538 return -EINVAL;
539 }
540
541 rc = ena_com_get_hash_ctrl(ena_dev, proto, &hash_fields);
542 if (rc)
543 return rc;
544
545 cmd->data = ena_flow_hash_to_flow_type(hash_fields);
546
547 return 0;
548 }
549
550 static int ena_set_rss_hash(struct ena_com_dev *ena_dev,
551 struct ethtool_rxnfc *cmd)
552 {
553 enum ena_admin_flow_hash_proto proto;
554 u16 hash_fields;
555
556 switch (cmd->flow_type) {
557 case TCP_V4_FLOW:
558 proto = ENA_ADMIN_RSS_TCP4;
559 break;
560 case UDP_V4_FLOW:
561 proto = ENA_ADMIN_RSS_UDP4;
562 break;
563 case TCP_V6_FLOW:
564 proto = ENA_ADMIN_RSS_TCP6;
565 break;
566 case UDP_V6_FLOW:
567 proto = ENA_ADMIN_RSS_UDP6;
568 break;
569 case IPV4_FLOW:
570 proto = ENA_ADMIN_RSS_IP4;
571 break;
572 case IPV6_FLOW:
573 proto = ENA_ADMIN_RSS_IP6;
574 break;
575 case ETHER_FLOW:
576 proto = ENA_ADMIN_RSS_NOT_IP;
577 break;
578 case AH_V4_FLOW:
579 case ESP_V4_FLOW:
580 case AH_V6_FLOW:
581 case ESP_V6_FLOW:
582 case SCTP_V4_FLOW:
583 case AH_ESP_V4_FLOW:
584 return -EOPNOTSUPP;
585 default:
586 return -EINVAL;
587 }
588
589 hash_fields = ena_flow_data_to_flow_hash(cmd->data);
590
591 return ena_com_fill_hash_ctrl(ena_dev, proto, hash_fields);
592 }
593
594 static int ena_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info)
595 {
596 struct ena_adapter *adapter = netdev_priv(netdev);
597 int rc = 0;
598
599 switch (info->cmd) {
600 case ETHTOOL_SRXFH:
601 rc = ena_set_rss_hash(adapter->ena_dev, info);
602 break;
603 case ETHTOOL_SRXCLSRLDEL:
604 case ETHTOOL_SRXCLSRLINS:
605 default:
606 netif_err(adapter, drv, netdev,
607 "Command parameter %d is not supported\n", info->cmd);
608 rc = -EOPNOTSUPP;
609 }
610
611 return rc;
612 }
613
614 static int ena_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
615 u32 *rules)
616 {
617 struct ena_adapter *adapter = netdev_priv(netdev);
618 int rc = 0;
619
620 switch (info->cmd) {
621 case ETHTOOL_GRXRINGS:
622 info->data = adapter->num_queues;
623 rc = 0;
624 break;
625 case ETHTOOL_GRXFH:
626 rc = ena_get_rss_hash(adapter->ena_dev, info);
627 break;
628 case ETHTOOL_GRXCLSRLCNT:
629 case ETHTOOL_GRXCLSRULE:
630 case ETHTOOL_GRXCLSRLALL:
631 default:
632 netif_err(adapter, drv, netdev,
633 "Command parameter %d is not supported\n", info->cmd);
634 rc = -EOPNOTSUPP;
635 }
636
637 return rc;
638 }
639
640 static u32 ena_get_rxfh_indir_size(struct net_device *netdev)
641 {
642 return ENA_RX_RSS_TABLE_SIZE;
643 }
644
645 static u32 ena_get_rxfh_key_size(struct net_device *netdev)
646 {
647 return ENA_HASH_KEY_SIZE;
648 }
649
650 static int ena_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
651 u8 *hfunc)
652 {
653 struct ena_adapter *adapter = netdev_priv(netdev);
654 enum ena_admin_hash_functions ena_func;
655 u8 func;
656 int rc;
657
658 rc = ena_com_indirect_table_get(adapter->ena_dev, indir);
659 if (rc)
660 return rc;
661
662 rc = ena_com_get_hash_function(adapter->ena_dev, &ena_func, key);
663 if (rc)
664 return rc;
665
666 switch (ena_func) {
667 case ENA_ADMIN_TOEPLITZ:
668 func = ETH_RSS_HASH_TOP;
669 break;
670 case ENA_ADMIN_CRC32:
671 func = ETH_RSS_HASH_XOR;
672 break;
673 default:
674 netif_err(adapter, drv, netdev,
675 "Command parameter is not supported\n");
676 return -EOPNOTSUPP;
677 }
678
679 if (hfunc)
680 *hfunc = func;
681
682 return rc;
683 }
684
685 static int ena_set_rxfh(struct net_device *netdev, const u32 *indir,
686 const u8 *key, const u8 hfunc)
687 {
688 struct ena_adapter *adapter = netdev_priv(netdev);
689 struct ena_com_dev *ena_dev = adapter->ena_dev;
690 enum ena_admin_hash_functions func;
691 int rc, i;
692
693 if (indir) {
694 for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
695 rc = ena_com_indirect_table_fill_entry(ena_dev,
696 ENA_IO_RXQ_IDX(indir[i]),
697 i);
698 if (unlikely(rc)) {
699 netif_err(adapter, drv, netdev,
700 "Cannot fill indirect table (index is too large)\n");
701 return rc;
702 }
703 }
704
705 rc = ena_com_indirect_table_set(ena_dev);
706 if (rc) {
707 netif_err(adapter, drv, netdev,
708 "Cannot set indirect table\n");
709 return rc == -EPERM ? -EOPNOTSUPP : rc;
710 }
711 }
712
713 switch (hfunc) {
714 case ETH_RSS_HASH_TOP:
715 func = ENA_ADMIN_TOEPLITZ;
716 break;
717 case ETH_RSS_HASH_XOR:
718 func = ENA_ADMIN_CRC32;
719 break;
720 default:
721 netif_err(adapter, drv, netdev, "Unsupported hfunc %d\n",
722 hfunc);
723 return -EOPNOTSUPP;
724 }
725
726 if (key) {
727 rc = ena_com_fill_hash_function(ena_dev, func, key,
728 ENA_HASH_KEY_SIZE,
729 0xFFFFFFFF);
730 if (unlikely(rc)) {
731 netif_err(adapter, drv, netdev, "Cannot fill key\n");
732 return rc == -EPERM ? -EOPNOTSUPP : rc;
733 }
734 }
735
736 return 0;
737 }
738
739 static void ena_get_channels(struct net_device *netdev,
740 struct ethtool_channels *channels)
741 {
742 struct ena_adapter *adapter = netdev_priv(netdev);
743
744 channels->max_rx = ENA_MAX_NUM_IO_QUEUES;
745 channels->max_tx = ENA_MAX_NUM_IO_QUEUES;
746 channels->max_other = 0;
747 channels->max_combined = 0;
748 channels->rx_count = adapter->num_queues;
749 channels->tx_count = adapter->num_queues;
750 channels->other_count = 0;
751 channels->combined_count = 0;
752 }
753
754 static int ena_get_tunable(struct net_device *netdev,
755 const struct ethtool_tunable *tuna, void *data)
756 {
757 struct ena_adapter *adapter = netdev_priv(netdev);
758 int ret = 0;
759
760 switch (tuna->id) {
761 case ETHTOOL_RX_COPYBREAK:
762 *(u32 *)data = adapter->rx_copybreak;
763 break;
764 default:
765 ret = -EINVAL;
766 break;
767 }
768
769 return ret;
770 }
771
772 static int ena_set_tunable(struct net_device *netdev,
773 const struct ethtool_tunable *tuna,
774 const void *data)
775 {
776 struct ena_adapter *adapter = netdev_priv(netdev);
777 int ret = 0;
778 u32 len;
779
780 switch (tuna->id) {
781 case ETHTOOL_RX_COPYBREAK:
782 len = *(u32 *)data;
783 if (len > adapter->netdev->mtu) {
784 ret = -EINVAL;
785 break;
786 }
787 adapter->rx_copybreak = len;
788 break;
789 default:
790 ret = -EINVAL;
791 break;
792 }
793
794 return ret;
795 }
796
797 static const struct ethtool_ops ena_ethtool_ops = {
798 .get_link_ksettings = ena_get_link_ksettings,
799 .get_drvinfo = ena_get_drvinfo,
800 .get_msglevel = ena_get_msglevel,
801 .set_msglevel = ena_set_msglevel,
802 .get_link = ethtool_op_get_link,
803 .get_coalesce = ena_get_coalesce,
804 .set_coalesce = ena_set_coalesce,
805 .get_ringparam = ena_get_ringparam,
806 .get_sset_count = ena_get_sset_count,
807 .get_strings = ena_get_strings,
808 .get_ethtool_stats = ena_get_ethtool_stats,
809 .get_rxnfc = ena_get_rxnfc,
810 .set_rxnfc = ena_set_rxnfc,
811 .get_rxfh_indir_size = ena_get_rxfh_indir_size,
812 .get_rxfh_key_size = ena_get_rxfh_key_size,
813 .get_rxfh = ena_get_rxfh,
814 .set_rxfh = ena_set_rxfh,
815 .get_channels = ena_get_channels,
816 .get_tunable = ena_get_tunable,
817 .set_tunable = ena_set_tunable,
818 };
819
820 void ena_set_ethtool_ops(struct net_device *netdev)
821 {
822 netdev->ethtool_ops = &ena_ethtool_ops;
823 }
824
825 static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
826 {
827 struct net_device *netdev = adapter->netdev;
828 u8 *strings_buf;
829 u64 *data_buf;
830 int strings_num;
831 int i, rc;
832
833 strings_num = ena_get_sset_count(netdev, ETH_SS_STATS);
834 if (strings_num <= 0) {
835 netif_err(adapter, drv, netdev, "Can't get stats num\n");
836 return;
837 }
838
839 strings_buf = devm_kzalloc(&adapter->pdev->dev,
840 strings_num * ETH_GSTRING_LEN,
841 GFP_ATOMIC);
842 if (!strings_buf) {
843 netif_err(adapter, drv, netdev,
844 "failed to alloc strings_buf\n");
845 return;
846 }
847
848 data_buf = devm_kzalloc(&adapter->pdev->dev,
849 strings_num * sizeof(u64),
850 GFP_ATOMIC);
851 if (!data_buf) {
852 netif_err(adapter, drv, netdev,
853 "failed to allocate data buf\n");
854 devm_kfree(&adapter->pdev->dev, strings_buf);
855 return;
856 }
857
858 ena_get_strings(netdev, ETH_SS_STATS, strings_buf);
859 ena_get_ethtool_stats(netdev, NULL, data_buf);
860
861 /* If there is a buffer, dump stats, otherwise print them to dmesg */
862 if (buf)
863 for (i = 0; i < strings_num; i++) {
864 rc = snprintf(buf, ETH_GSTRING_LEN + sizeof(u64),
865 "%s %llu\n",
866 strings_buf + i * ETH_GSTRING_LEN,
867 data_buf[i]);
868 buf += rc;
869 }
870 else
871 for (i = 0; i < strings_num; i++)
872 netif_err(adapter, drv, netdev, "%s: %llu\n",
873 strings_buf + i * ETH_GSTRING_LEN,
874 data_buf[i]);
875
876 devm_kfree(&adapter->pdev->dev, strings_buf);
877 devm_kfree(&adapter->pdev->dev, data_buf);
878 }
879
880 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf)
881 {
882 if (!buf)
883 return;
884
885 ena_dump_stats_ex(adapter, buf);
886 }
887
888 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter)
889 {
890 ena_dump_stats_ex(adapter, NULL);
891 }