xref: /linux/drivers/net/ethernet/amazon/ena/ena_ethtool.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1 /*
2  * Copyright 2015 Amazon.com, Inc. or its affiliates.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/pci.h>
34 
35 #include "ena_netdev.h"
36 
37 struct ena_stats {
38 	char name[ETH_GSTRING_LEN];
39 	int stat_offset;
40 };
41 
42 #define ENA_STAT_ENA_COM_ENTRY(stat) { \
43 	.name = #stat, \
44 	.stat_offset = offsetof(struct ena_com_stats_admin, stat) \
45 }
46 
47 #define ENA_STAT_ENTRY(stat, stat_type) { \
48 	.name = #stat, \
49 	.stat_offset = offsetof(struct ena_stats_##stat_type, stat) \
50 }
51 
52 #define ENA_STAT_RX_ENTRY(stat) \
53 	ENA_STAT_ENTRY(stat, rx)
54 
55 #define ENA_STAT_TX_ENTRY(stat) \
56 	ENA_STAT_ENTRY(stat, tx)
57 
58 #define ENA_STAT_GLOBAL_ENTRY(stat) \
59 	ENA_STAT_ENTRY(stat, dev)
60 
61 static const struct ena_stats ena_stats_global_strings[] = {
62 	ENA_STAT_GLOBAL_ENTRY(tx_timeout),
63 	ENA_STAT_GLOBAL_ENTRY(io_suspend),
64 	ENA_STAT_GLOBAL_ENTRY(io_resume),
65 	ENA_STAT_GLOBAL_ENTRY(wd_expired),
66 	ENA_STAT_GLOBAL_ENTRY(interface_up),
67 	ENA_STAT_GLOBAL_ENTRY(interface_down),
68 	ENA_STAT_GLOBAL_ENTRY(admin_q_pause),
69 };
70 
71 static const struct ena_stats ena_stats_tx_strings[] = {
72 	ENA_STAT_TX_ENTRY(cnt),
73 	ENA_STAT_TX_ENTRY(bytes),
74 	ENA_STAT_TX_ENTRY(queue_stop),
75 	ENA_STAT_TX_ENTRY(queue_wakeup),
76 	ENA_STAT_TX_ENTRY(dma_mapping_err),
77 	ENA_STAT_TX_ENTRY(linearize),
78 	ENA_STAT_TX_ENTRY(linearize_failed),
79 	ENA_STAT_TX_ENTRY(napi_comp),
80 	ENA_STAT_TX_ENTRY(tx_poll),
81 	ENA_STAT_TX_ENTRY(doorbells),
82 	ENA_STAT_TX_ENTRY(prepare_ctx_err),
83 	ENA_STAT_TX_ENTRY(missing_tx_comp),
84 	ENA_STAT_TX_ENTRY(bad_req_id),
85 };
86 
87 static const struct ena_stats ena_stats_rx_strings[] = {
88 	ENA_STAT_RX_ENTRY(cnt),
89 	ENA_STAT_RX_ENTRY(bytes),
90 	ENA_STAT_RX_ENTRY(refil_partial),
91 	ENA_STAT_RX_ENTRY(bad_csum),
92 	ENA_STAT_RX_ENTRY(page_alloc_fail),
93 	ENA_STAT_RX_ENTRY(skb_alloc_fail),
94 	ENA_STAT_RX_ENTRY(dma_mapping_err),
95 	ENA_STAT_RX_ENTRY(bad_desc_num),
96 	ENA_STAT_RX_ENTRY(rx_copybreak_pkt),
97 };
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 		/* If device don't have permission, return unsupported */
544 		if (rc == -EPERM)
545 			rc = -EOPNOTSUPP;
546 		return rc;
547 	}
548 
549 	cmd->data = ena_flow_hash_to_flow_type(hash_fields);
550 
551 	return 0;
552 }
553 
554 static int ena_set_rss_hash(struct ena_com_dev *ena_dev,
555 			    struct ethtool_rxnfc *cmd)
556 {
557 	enum ena_admin_flow_hash_proto proto;
558 	u16 hash_fields;
559 
560 	switch (cmd->flow_type) {
561 	case TCP_V4_FLOW:
562 		proto = ENA_ADMIN_RSS_TCP4;
563 		break;
564 	case UDP_V4_FLOW:
565 		proto = ENA_ADMIN_RSS_UDP4;
566 		break;
567 	case TCP_V6_FLOW:
568 		proto = ENA_ADMIN_RSS_TCP6;
569 		break;
570 	case UDP_V6_FLOW:
571 		proto = ENA_ADMIN_RSS_UDP6;
572 		break;
573 	case IPV4_FLOW:
574 		proto = ENA_ADMIN_RSS_IP4;
575 		break;
576 	case IPV6_FLOW:
577 		proto = ENA_ADMIN_RSS_IP6;
578 		break;
579 	case ETHER_FLOW:
580 		proto = ENA_ADMIN_RSS_NOT_IP;
581 		break;
582 	case AH_V4_FLOW:
583 	case ESP_V4_FLOW:
584 	case AH_V6_FLOW:
585 	case ESP_V6_FLOW:
586 	case SCTP_V4_FLOW:
587 	case AH_ESP_V4_FLOW:
588 		return -EOPNOTSUPP;
589 	default:
590 		return -EINVAL;
591 	}
592 
593 	hash_fields = ena_flow_data_to_flow_hash(cmd->data);
594 
595 	return ena_com_fill_hash_ctrl(ena_dev, proto, hash_fields);
596 }
597 
598 static int ena_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info)
599 {
600 	struct ena_adapter *adapter = netdev_priv(netdev);
601 	int rc = 0;
602 
603 	switch (info->cmd) {
604 	case ETHTOOL_SRXFH:
605 		rc = ena_set_rss_hash(adapter->ena_dev, info);
606 		break;
607 	case ETHTOOL_SRXCLSRLDEL:
608 	case ETHTOOL_SRXCLSRLINS:
609 	default:
610 		netif_err(adapter, drv, netdev,
611 			  "Command parameter %d is not supported\n", info->cmd);
612 		rc = -EOPNOTSUPP;
613 	}
614 
615 	return (rc == -EPERM) ? -EOPNOTSUPP : rc;
616 }
617 
618 static int ena_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
619 			 u32 *rules)
620 {
621 	struct ena_adapter *adapter = netdev_priv(netdev);
622 	int rc = 0;
623 
624 	switch (info->cmd) {
625 	case ETHTOOL_GRXRINGS:
626 		info->data = adapter->num_queues;
627 		rc = 0;
628 		break;
629 	case ETHTOOL_GRXFH:
630 		rc = ena_get_rss_hash(adapter->ena_dev, info);
631 		break;
632 	case ETHTOOL_GRXCLSRLCNT:
633 	case ETHTOOL_GRXCLSRULE:
634 	case ETHTOOL_GRXCLSRLALL:
635 	default:
636 		netif_err(adapter, drv, netdev,
637 			  "Command parameter %d is not supported\n", info->cmd);
638 		rc = -EOPNOTSUPP;
639 	}
640 
641 	return (rc == -EPERM) ? -EOPNOTSUPP : rc;
642 }
643 
644 static u32 ena_get_rxfh_indir_size(struct net_device *netdev)
645 {
646 	return ENA_RX_RSS_TABLE_SIZE;
647 }
648 
649 static u32 ena_get_rxfh_key_size(struct net_device *netdev)
650 {
651 	return ENA_HASH_KEY_SIZE;
652 }
653 
654 static int ena_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
655 			u8 *hfunc)
656 {
657 	struct ena_adapter *adapter = netdev_priv(netdev);
658 	enum ena_admin_hash_functions ena_func;
659 	u8 func;
660 	int rc;
661 
662 	rc = ena_com_indirect_table_get(adapter->ena_dev, indir);
663 	if (rc)
664 		return rc;
665 
666 	rc = ena_com_get_hash_function(adapter->ena_dev, &ena_func, key);
667 	if (rc)
668 		return rc;
669 
670 	switch (ena_func) {
671 	case ENA_ADMIN_TOEPLITZ:
672 		func = ETH_RSS_HASH_TOP;
673 		break;
674 	case ENA_ADMIN_CRC32:
675 		func = ETH_RSS_HASH_XOR;
676 		break;
677 	default:
678 		netif_err(adapter, drv, netdev,
679 			  "Command parameter is not supported\n");
680 		return -EOPNOTSUPP;
681 	}
682 
683 	if (hfunc)
684 		*hfunc = func;
685 
686 	return rc;
687 }
688 
689 static int ena_set_rxfh(struct net_device *netdev, const u32 *indir,
690 			const u8 *key, const u8 hfunc)
691 {
692 	struct ena_adapter *adapter = netdev_priv(netdev);
693 	struct ena_com_dev *ena_dev = adapter->ena_dev;
694 	enum ena_admin_hash_functions func;
695 	int rc, i;
696 
697 	if (indir) {
698 		for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
699 			rc = ena_com_indirect_table_fill_entry(ena_dev,
700 							       ENA_IO_RXQ_IDX(indir[i]),
701 							       i);
702 			if (unlikely(rc)) {
703 				netif_err(adapter, drv, netdev,
704 					  "Cannot fill indirect table (index is too large)\n");
705 				return rc;
706 			}
707 		}
708 
709 		rc = ena_com_indirect_table_set(ena_dev);
710 		if (rc) {
711 			netif_err(adapter, drv, netdev,
712 				  "Cannot set indirect table\n");
713 			return rc == -EPERM ? -EOPNOTSUPP : rc;
714 		}
715 	}
716 
717 	switch (hfunc) {
718 	case ETH_RSS_HASH_TOP:
719 		func = ENA_ADMIN_TOEPLITZ;
720 		break;
721 	case ETH_RSS_HASH_XOR:
722 		func = ENA_ADMIN_CRC32;
723 		break;
724 	default:
725 		netif_err(adapter, drv, netdev, "Unsupported hfunc %d\n",
726 			  hfunc);
727 		return -EOPNOTSUPP;
728 	}
729 
730 	if (key) {
731 		rc = ena_com_fill_hash_function(ena_dev, func, key,
732 						ENA_HASH_KEY_SIZE,
733 						0xFFFFFFFF);
734 		if (unlikely(rc)) {
735 			netif_err(adapter, drv, netdev, "Cannot fill key\n");
736 			return rc == -EPERM ? -EOPNOTSUPP : rc;
737 		}
738 	}
739 
740 	return 0;
741 }
742 
743 static void ena_get_channels(struct net_device *netdev,
744 			     struct ethtool_channels *channels)
745 {
746 	struct ena_adapter *adapter = netdev_priv(netdev);
747 
748 	channels->max_rx = ENA_MAX_NUM_IO_QUEUES;
749 	channels->max_tx = ENA_MAX_NUM_IO_QUEUES;
750 	channels->max_other = 0;
751 	channels->max_combined = 0;
752 	channels->rx_count = adapter->num_queues;
753 	channels->tx_count = adapter->num_queues;
754 	channels->other_count = 0;
755 	channels->combined_count = 0;
756 }
757 
758 static int ena_get_tunable(struct net_device *netdev,
759 			   const struct ethtool_tunable *tuna, void *data)
760 {
761 	struct ena_adapter *adapter = netdev_priv(netdev);
762 	int ret = 0;
763 
764 	switch (tuna->id) {
765 	case ETHTOOL_RX_COPYBREAK:
766 		*(u32 *)data = adapter->rx_copybreak;
767 		break;
768 	default:
769 		ret = -EINVAL;
770 		break;
771 	}
772 
773 	return ret;
774 }
775 
776 static int ena_set_tunable(struct net_device *netdev,
777 			   const struct ethtool_tunable *tuna,
778 			   const void *data)
779 {
780 	struct ena_adapter *adapter = netdev_priv(netdev);
781 	int ret = 0;
782 	u32 len;
783 
784 	switch (tuna->id) {
785 	case ETHTOOL_RX_COPYBREAK:
786 		len = *(u32 *)data;
787 		if (len > adapter->netdev->mtu) {
788 			ret = -EINVAL;
789 			break;
790 		}
791 		adapter->rx_copybreak = len;
792 		break;
793 	default:
794 		ret = -EINVAL;
795 		break;
796 	}
797 
798 	return ret;
799 }
800 
801 static const struct ethtool_ops ena_ethtool_ops = {
802 	.get_link_ksettings	= ena_get_link_ksettings,
803 	.get_drvinfo		= ena_get_drvinfo,
804 	.get_msglevel		= ena_get_msglevel,
805 	.set_msglevel		= ena_set_msglevel,
806 	.get_link		= ethtool_op_get_link,
807 	.get_coalesce		= ena_get_coalesce,
808 	.set_coalesce		= ena_set_coalesce,
809 	.get_ringparam		= ena_get_ringparam,
810 	.get_sset_count         = ena_get_sset_count,
811 	.get_strings		= ena_get_strings,
812 	.get_ethtool_stats      = ena_get_ethtool_stats,
813 	.get_rxnfc		= ena_get_rxnfc,
814 	.set_rxnfc		= ena_set_rxnfc,
815 	.get_rxfh_indir_size    = ena_get_rxfh_indir_size,
816 	.get_rxfh_key_size	= ena_get_rxfh_key_size,
817 	.get_rxfh		= ena_get_rxfh,
818 	.set_rxfh		= ena_set_rxfh,
819 	.get_channels		= ena_get_channels,
820 	.get_tunable		= ena_get_tunable,
821 	.set_tunable		= ena_set_tunable,
822 };
823 
824 void ena_set_ethtool_ops(struct net_device *netdev)
825 {
826 	netdev->ethtool_ops = &ena_ethtool_ops;
827 }
828 
829 static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
830 {
831 	struct net_device *netdev = adapter->netdev;
832 	u8 *strings_buf;
833 	u64 *data_buf;
834 	int strings_num;
835 	int i, rc;
836 
837 	strings_num = ena_get_sset_count(netdev, ETH_SS_STATS);
838 	if (strings_num <= 0) {
839 		netif_err(adapter, drv, netdev, "Can't get stats num\n");
840 		return;
841 	}
842 
843 	strings_buf = devm_kzalloc(&adapter->pdev->dev,
844 				   strings_num * ETH_GSTRING_LEN,
845 				   GFP_ATOMIC);
846 	if (!strings_buf) {
847 		netif_err(adapter, drv, netdev,
848 			  "failed to alloc strings_buf\n");
849 		return;
850 	}
851 
852 	data_buf = devm_kzalloc(&adapter->pdev->dev,
853 				strings_num * sizeof(u64),
854 				GFP_ATOMIC);
855 	if (!data_buf) {
856 		netif_err(adapter, drv, netdev,
857 			  "failed to allocate data buf\n");
858 		devm_kfree(&adapter->pdev->dev, strings_buf);
859 		return;
860 	}
861 
862 	ena_get_strings(netdev, ETH_SS_STATS, strings_buf);
863 	ena_get_ethtool_stats(netdev, NULL, data_buf);
864 
865 	/* If there is a buffer, dump stats, otherwise print them to dmesg */
866 	if (buf)
867 		for (i = 0; i < strings_num; i++) {
868 			rc = snprintf(buf, ETH_GSTRING_LEN + sizeof(u64),
869 				      "%s %llu\n",
870 				      strings_buf + i * ETH_GSTRING_LEN,
871 				      data_buf[i]);
872 			buf += rc;
873 		}
874 	else
875 		for (i = 0; i < strings_num; i++)
876 			netif_err(adapter, drv, netdev, "%s: %llu\n",
877 				  strings_buf + i * ETH_GSTRING_LEN,
878 				  data_buf[i]);
879 
880 	devm_kfree(&adapter->pdev->dev, strings_buf);
881 	devm_kfree(&adapter->pdev->dev, data_buf);
882 }
883 
884 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf)
885 {
886 	if (!buf)
887 		return;
888 
889 	ena_dump_stats_ex(adapter, buf);
890 }
891 
892 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter)
893 {
894 	ena_dump_stats_ex(adapter, NULL);
895 }
896