xref: /openbmc/linux/drivers/net/ethernet/amazon/ena/ena_ethtool.c (revision c64d01b3ceba873aa8e8605598cec4a6bc6d1601)
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 	switch (sset) {
237 	case ETH_SS_STATS:
238 		return ena_get_sw_stats_count(adapter) +
239 		       ena_get_hw_stats_count(adapter);
240 	}
241 
242 	return -EOPNOTSUPP;
243 }
244 
245 static void ena_queue_strings(struct ena_adapter *adapter, u8 **data)
246 {
247 	const struct ena_stats *ena_stats;
248 	bool is_xdp;
249 	int i, j;
250 
251 	for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) {
252 		is_xdp = ENA_IS_XDP_INDEX(adapter, i);
253 		/* Tx stats */
254 		for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
255 			ena_stats = &ena_stats_tx_strings[j];
256 
257 			ethtool_sprintf(data,
258 					"queue_%u_%s_%s", i,
259 					is_xdp ? "xdp_tx" : "tx",
260 					ena_stats->name);
261 		}
262 
263 		if (!is_xdp) {
264 			/* RX stats, in XDP there isn't a RX queue
265 			 * counterpart
266 			 */
267 			for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
268 				ena_stats = &ena_stats_rx_strings[j];
269 
270 				ethtool_sprintf(data,
271 						"queue_%u_rx_%s", i,
272 						ena_stats->name);
273 			}
274 		}
275 	}
276 }
277 
278 static void ena_com_dev_strings(u8 **data)
279 {
280 	const struct ena_stats *ena_stats;
281 	int i;
282 
283 	for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
284 		ena_stats = &ena_stats_ena_com_strings[i];
285 
286 		ethtool_sprintf(data,
287 				"ena_admin_q_%s", ena_stats->name);
288 	}
289 }
290 
291 static void ena_get_strings(struct ena_adapter *adapter,
292 			    u8 *data,
293 			    bool eni_stats_needed)
294 {
295 	const struct ena_stats *ena_stats;
296 	int i;
297 
298 	for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
299 		ena_stats = &ena_stats_global_strings[i];
300 		ethtool_sprintf(&data, ena_stats->name);
301 	}
302 
303 	if (eni_stats_needed) {
304 		for (i = 0; i < ENA_STATS_ARRAY_ENI(adapter); i++) {
305 			ena_stats = &ena_stats_eni_strings[i];
306 			ethtool_sprintf(&data, ena_stats->name);
307 		}
308 	}
309 
310 	ena_queue_strings(adapter, &data);
311 	ena_com_dev_strings(&data);
312 }
313 
314 static void ena_get_ethtool_strings(struct net_device *netdev,
315 				    u32 sset,
316 				    u8 *data)
317 {
318 	struct ena_adapter *adapter = netdev_priv(netdev);
319 
320 	switch (sset) {
321 	case ETH_SS_STATS:
322 		ena_get_strings(adapter, data, adapter->eni_stats_supported);
323 		break;
324 	}
325 }
326 
327 static int ena_get_link_ksettings(struct net_device *netdev,
328 				  struct ethtool_link_ksettings *link_ksettings)
329 {
330 	struct ena_adapter *adapter = netdev_priv(netdev);
331 	struct ena_com_dev *ena_dev = adapter->ena_dev;
332 	struct ena_admin_get_feature_link_desc *link;
333 	struct ena_admin_get_feat_resp feat_resp;
334 	int rc;
335 
336 	rc = ena_com_get_link_params(ena_dev, &feat_resp);
337 	if (rc)
338 		return rc;
339 
340 	link = &feat_resp.u.link;
341 	link_ksettings->base.speed = link->speed;
342 
343 	if (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) {
344 		ethtool_link_ksettings_add_link_mode(link_ksettings,
345 						     supported, Autoneg);
346 		ethtool_link_ksettings_add_link_mode(link_ksettings,
347 						     supported, Autoneg);
348 	}
349 
350 	link_ksettings->base.autoneg =
351 		(link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) ?
352 		AUTONEG_ENABLE : AUTONEG_DISABLE;
353 
354 	link_ksettings->base.duplex = DUPLEX_FULL;
355 
356 	return 0;
357 }
358 
359 static int ena_get_coalesce(struct net_device *net_dev,
360 			    struct ethtool_coalesce *coalesce,
361 			    struct kernel_ethtool_coalesce *kernel_coal,
362 			    struct netlink_ext_ack *extack)
363 {
364 	struct ena_adapter *adapter = netdev_priv(net_dev);
365 	struct ena_com_dev *ena_dev = adapter->ena_dev;
366 
367 	if (!ena_com_interrupt_moderation_supported(ena_dev))
368 		return -EOPNOTSUPP;
369 
370 	coalesce->tx_coalesce_usecs =
371 		ena_com_get_nonadaptive_moderation_interval_tx(ena_dev) *
372 			ena_dev->intr_delay_resolution;
373 
374 	coalesce->rx_coalesce_usecs =
375 		ena_com_get_nonadaptive_moderation_interval_rx(ena_dev)
376 		* ena_dev->intr_delay_resolution;
377 
378 	coalesce->use_adaptive_rx_coalesce =
379 		ena_com_get_adaptive_moderation_enabled(ena_dev);
380 
381 	return 0;
382 }
383 
384 static void ena_update_tx_rings_nonadaptive_intr_moderation(struct ena_adapter *adapter)
385 {
386 	unsigned int val;
387 	int i;
388 
389 	val = ena_com_get_nonadaptive_moderation_interval_tx(adapter->ena_dev);
390 
391 	for (i = 0; i < adapter->num_io_queues; i++)
392 		adapter->tx_ring[i].smoothed_interval = val;
393 }
394 
395 static void ena_update_rx_rings_nonadaptive_intr_moderation(struct ena_adapter *adapter)
396 {
397 	unsigned int val;
398 	int i;
399 
400 	val = ena_com_get_nonadaptive_moderation_interval_rx(adapter->ena_dev);
401 
402 	for (i = 0; i < adapter->num_io_queues; i++)
403 		adapter->rx_ring[i].smoothed_interval = val;
404 }
405 
406 static int ena_set_coalesce(struct net_device *net_dev,
407 			    struct ethtool_coalesce *coalesce,
408 			    struct kernel_ethtool_coalesce *kernel_coal,
409 			    struct netlink_ext_ack *extack)
410 {
411 	struct ena_adapter *adapter = netdev_priv(net_dev);
412 	struct ena_com_dev *ena_dev = adapter->ena_dev;
413 	int rc;
414 
415 	if (!ena_com_interrupt_moderation_supported(ena_dev))
416 		return -EOPNOTSUPP;
417 
418 	rc = ena_com_update_nonadaptive_moderation_interval_tx(ena_dev,
419 							       coalesce->tx_coalesce_usecs);
420 	if (rc)
421 		return rc;
422 
423 	ena_update_tx_rings_nonadaptive_intr_moderation(adapter);
424 
425 	rc = ena_com_update_nonadaptive_moderation_interval_rx(ena_dev,
426 							       coalesce->rx_coalesce_usecs);
427 	if (rc)
428 		return rc;
429 
430 	ena_update_rx_rings_nonadaptive_intr_moderation(adapter);
431 
432 	if (coalesce->use_adaptive_rx_coalesce &&
433 	    !ena_com_get_adaptive_moderation_enabled(ena_dev))
434 		ena_com_enable_adaptive_moderation(ena_dev);
435 
436 	if (!coalesce->use_adaptive_rx_coalesce &&
437 	    ena_com_get_adaptive_moderation_enabled(ena_dev))
438 		ena_com_disable_adaptive_moderation(ena_dev);
439 
440 	return 0;
441 }
442 
443 static u32 ena_get_msglevel(struct net_device *netdev)
444 {
445 	struct ena_adapter *adapter = netdev_priv(netdev);
446 
447 	return adapter->msg_enable;
448 }
449 
450 static void ena_set_msglevel(struct net_device *netdev, u32 value)
451 {
452 	struct ena_adapter *adapter = netdev_priv(netdev);
453 
454 	adapter->msg_enable = value;
455 }
456 
457 static void ena_get_drvinfo(struct net_device *dev,
458 			    struct ethtool_drvinfo *info)
459 {
460 	struct ena_adapter *adapter = netdev_priv(dev);
461 
462 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
463 	strlcpy(info->bus_info, pci_name(adapter->pdev),
464 		sizeof(info->bus_info));
465 }
466 
467 static void ena_get_ringparam(struct net_device *netdev,
468 			      struct ethtool_ringparam *ring,
469 			      struct kernel_ethtool_ringparam *kernel_ring,
470 			      struct netlink_ext_ack *extack)
471 {
472 	struct ena_adapter *adapter = netdev_priv(netdev);
473 
474 	ring->tx_max_pending = adapter->max_tx_ring_size;
475 	ring->rx_max_pending = adapter->max_rx_ring_size;
476 	ring->tx_pending = adapter->tx_ring[0].ring_size;
477 	ring->rx_pending = adapter->rx_ring[0].ring_size;
478 }
479 
480 static int ena_set_ringparam(struct net_device *netdev,
481 			     struct ethtool_ringparam *ring,
482 			     struct kernel_ethtool_ringparam *kernel_ring,
483 			     struct netlink_ext_ack *extack)
484 {
485 	struct ena_adapter *adapter = netdev_priv(netdev);
486 	u32 new_tx_size, new_rx_size;
487 
488 	new_tx_size = ring->tx_pending < ENA_MIN_RING_SIZE ?
489 			ENA_MIN_RING_SIZE : ring->tx_pending;
490 	new_tx_size = rounddown_pow_of_two(new_tx_size);
491 
492 	new_rx_size = ring->rx_pending < ENA_MIN_RING_SIZE ?
493 			ENA_MIN_RING_SIZE : ring->rx_pending;
494 	new_rx_size = rounddown_pow_of_two(new_rx_size);
495 
496 	if (new_tx_size == adapter->requested_tx_ring_size &&
497 	    new_rx_size == adapter->requested_rx_ring_size)
498 		return 0;
499 
500 	return ena_update_queue_sizes(adapter, new_tx_size, new_rx_size);
501 }
502 
503 static u32 ena_flow_hash_to_flow_type(u16 hash_fields)
504 {
505 	u32 data = 0;
506 
507 	if (hash_fields & ENA_ADMIN_RSS_L2_DA)
508 		data |= RXH_L2DA;
509 
510 	if (hash_fields & ENA_ADMIN_RSS_L3_DA)
511 		data |= RXH_IP_DST;
512 
513 	if (hash_fields & ENA_ADMIN_RSS_L3_SA)
514 		data |= RXH_IP_SRC;
515 
516 	if (hash_fields & ENA_ADMIN_RSS_L4_DP)
517 		data |= RXH_L4_B_2_3;
518 
519 	if (hash_fields & ENA_ADMIN_RSS_L4_SP)
520 		data |= RXH_L4_B_0_1;
521 
522 	return data;
523 }
524 
525 static u16 ena_flow_data_to_flow_hash(u32 hash_fields)
526 {
527 	u16 data = 0;
528 
529 	if (hash_fields & RXH_L2DA)
530 		data |= ENA_ADMIN_RSS_L2_DA;
531 
532 	if (hash_fields & RXH_IP_DST)
533 		data |= ENA_ADMIN_RSS_L3_DA;
534 
535 	if (hash_fields & RXH_IP_SRC)
536 		data |= ENA_ADMIN_RSS_L3_SA;
537 
538 	if (hash_fields & RXH_L4_B_2_3)
539 		data |= ENA_ADMIN_RSS_L4_DP;
540 
541 	if (hash_fields & RXH_L4_B_0_1)
542 		data |= ENA_ADMIN_RSS_L4_SP;
543 
544 	return data;
545 }
546 
547 static int ena_get_rss_hash(struct ena_com_dev *ena_dev,
548 			    struct ethtool_rxnfc *cmd)
549 {
550 	enum ena_admin_flow_hash_proto proto;
551 	u16 hash_fields;
552 	int rc;
553 
554 	cmd->data = 0;
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 	rc = ena_com_get_hash_ctrl(ena_dev, proto, &hash_fields);
590 	if (rc)
591 		return rc;
592 
593 	cmd->data = ena_flow_hash_to_flow_type(hash_fields);
594 
595 	return 0;
596 }
597 
598 static int ena_set_rss_hash(struct ena_com_dev *ena_dev,
599 			    struct ethtool_rxnfc *cmd)
600 {
601 	enum ena_admin_flow_hash_proto proto;
602 	u16 hash_fields;
603 
604 	switch (cmd->flow_type) {
605 	case TCP_V4_FLOW:
606 		proto = ENA_ADMIN_RSS_TCP4;
607 		break;
608 	case UDP_V4_FLOW:
609 		proto = ENA_ADMIN_RSS_UDP4;
610 		break;
611 	case TCP_V6_FLOW:
612 		proto = ENA_ADMIN_RSS_TCP6;
613 		break;
614 	case UDP_V6_FLOW:
615 		proto = ENA_ADMIN_RSS_UDP6;
616 		break;
617 	case IPV4_FLOW:
618 		proto = ENA_ADMIN_RSS_IP4;
619 		break;
620 	case IPV6_FLOW:
621 		proto = ENA_ADMIN_RSS_IP6;
622 		break;
623 	case ETHER_FLOW:
624 		proto = ENA_ADMIN_RSS_NOT_IP;
625 		break;
626 	case AH_V4_FLOW:
627 	case ESP_V4_FLOW:
628 	case AH_V6_FLOW:
629 	case ESP_V6_FLOW:
630 	case SCTP_V4_FLOW:
631 	case AH_ESP_V4_FLOW:
632 		return -EOPNOTSUPP;
633 	default:
634 		return -EINVAL;
635 	}
636 
637 	hash_fields = ena_flow_data_to_flow_hash(cmd->data);
638 
639 	return ena_com_fill_hash_ctrl(ena_dev, proto, hash_fields);
640 }
641 
642 static int ena_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info)
643 {
644 	struct ena_adapter *adapter = netdev_priv(netdev);
645 	int rc = 0;
646 
647 	switch (info->cmd) {
648 	case ETHTOOL_SRXFH:
649 		rc = ena_set_rss_hash(adapter->ena_dev, info);
650 		break;
651 	case ETHTOOL_SRXCLSRLDEL:
652 	case ETHTOOL_SRXCLSRLINS:
653 	default:
654 		netif_err(adapter, drv, netdev,
655 			  "Command parameter %d is not supported\n", info->cmd);
656 		rc = -EOPNOTSUPP;
657 	}
658 
659 	return rc;
660 }
661 
662 static int ena_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
663 			 u32 *rules)
664 {
665 	struct ena_adapter *adapter = netdev_priv(netdev);
666 	int rc = 0;
667 
668 	switch (info->cmd) {
669 	case ETHTOOL_GRXRINGS:
670 		info->data = adapter->num_io_queues;
671 		rc = 0;
672 		break;
673 	case ETHTOOL_GRXFH:
674 		rc = ena_get_rss_hash(adapter->ena_dev, info);
675 		break;
676 	case ETHTOOL_GRXCLSRLCNT:
677 	case ETHTOOL_GRXCLSRULE:
678 	case ETHTOOL_GRXCLSRLALL:
679 	default:
680 		netif_err(adapter, drv, netdev,
681 			  "Command parameter %d is not supported\n", info->cmd);
682 		rc = -EOPNOTSUPP;
683 	}
684 
685 	return rc;
686 }
687 
688 static u32 ena_get_rxfh_indir_size(struct net_device *netdev)
689 {
690 	return ENA_RX_RSS_TABLE_SIZE;
691 }
692 
693 static u32 ena_get_rxfh_key_size(struct net_device *netdev)
694 {
695 	return ENA_HASH_KEY_SIZE;
696 }
697 
698 static int ena_indirection_table_set(struct ena_adapter *adapter,
699 				     const u32 *indir)
700 {
701 	struct ena_com_dev *ena_dev = adapter->ena_dev;
702 	int i, rc;
703 
704 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
705 		rc = ena_com_indirect_table_fill_entry(ena_dev,
706 						       i,
707 						       ENA_IO_RXQ_IDX(indir[i]));
708 		if (unlikely(rc)) {
709 			netif_err(adapter, drv, adapter->netdev,
710 				  "Cannot fill indirect table (index is too large)\n");
711 			return rc;
712 		}
713 	}
714 
715 	rc = ena_com_indirect_table_set(ena_dev);
716 	if (rc) {
717 		netif_err(adapter, drv, adapter->netdev,
718 			  "Cannot set indirect table\n");
719 		return rc == -EPERM ? -EOPNOTSUPP : rc;
720 	}
721 	return rc;
722 }
723 
724 static int ena_indirection_table_get(struct ena_adapter *adapter, u32 *indir)
725 {
726 	struct ena_com_dev *ena_dev = adapter->ena_dev;
727 	int i, rc;
728 
729 	if (!indir)
730 		return 0;
731 
732 	rc = ena_com_indirect_table_get(ena_dev, indir);
733 	if (rc)
734 		return rc;
735 
736 	/* Our internal representation of the indices is: even indices
737 	 * for Tx and uneven indices for Rx. We need to convert the Rx
738 	 * indices to be consecutive
739 	 */
740 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++)
741 		indir[i] = ENA_IO_RXQ_IDX_TO_COMBINED_IDX(indir[i]);
742 
743 	return rc;
744 }
745 
746 static int ena_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
747 			u8 *hfunc)
748 {
749 	struct ena_adapter *adapter = netdev_priv(netdev);
750 	enum ena_admin_hash_functions ena_func;
751 	u8 func;
752 	int rc;
753 
754 	rc = ena_indirection_table_get(adapter, indir);
755 	if (rc)
756 		return rc;
757 
758 	/* We call this function in order to check if the device
759 	 * supports getting/setting the hash function.
760 	 */
761 	rc = ena_com_get_hash_function(adapter->ena_dev, &ena_func);
762 	if (rc) {
763 		if (rc == -EOPNOTSUPP)
764 			rc = 0;
765 
766 		return rc;
767 	}
768 
769 	rc = ena_com_get_hash_key(adapter->ena_dev, key);
770 	if (rc)
771 		return rc;
772 
773 	switch (ena_func) {
774 	case ENA_ADMIN_TOEPLITZ:
775 		func = ETH_RSS_HASH_TOP;
776 		break;
777 	case ENA_ADMIN_CRC32:
778 		func = ETH_RSS_HASH_CRC32;
779 		break;
780 	default:
781 		netif_err(adapter, drv, netdev,
782 			  "Command parameter is not supported\n");
783 		return -EOPNOTSUPP;
784 	}
785 
786 	if (hfunc)
787 		*hfunc = func;
788 
789 	return 0;
790 }
791 
792 static int ena_set_rxfh(struct net_device *netdev, const u32 *indir,
793 			const u8 *key, const u8 hfunc)
794 {
795 	struct ena_adapter *adapter = netdev_priv(netdev);
796 	struct ena_com_dev *ena_dev = adapter->ena_dev;
797 	enum ena_admin_hash_functions func = 0;
798 	int rc;
799 
800 	if (indir) {
801 		rc = ena_indirection_table_set(adapter, indir);
802 		if (rc)
803 			return rc;
804 	}
805 
806 	switch (hfunc) {
807 	case ETH_RSS_HASH_NO_CHANGE:
808 		func = ena_com_get_current_hash_function(ena_dev);
809 		break;
810 	case ETH_RSS_HASH_TOP:
811 		func = ENA_ADMIN_TOEPLITZ;
812 		break;
813 	case ETH_RSS_HASH_CRC32:
814 		func = ENA_ADMIN_CRC32;
815 		break;
816 	default:
817 		netif_err(adapter, drv, netdev, "Unsupported hfunc %d\n",
818 			  hfunc);
819 		return -EOPNOTSUPP;
820 	}
821 
822 	if (key || func) {
823 		rc = ena_com_fill_hash_function(ena_dev, func, key,
824 						ENA_HASH_KEY_SIZE,
825 						0xFFFFFFFF);
826 		if (unlikely(rc)) {
827 			netif_err(adapter, drv, netdev, "Cannot fill key\n");
828 			return rc == -EPERM ? -EOPNOTSUPP : rc;
829 		}
830 	}
831 
832 	return 0;
833 }
834 
835 static void ena_get_channels(struct net_device *netdev,
836 			     struct ethtool_channels *channels)
837 {
838 	struct ena_adapter *adapter = netdev_priv(netdev);
839 
840 	channels->max_combined = adapter->max_num_io_queues;
841 	channels->combined_count = adapter->num_io_queues;
842 }
843 
844 static int ena_set_channels(struct net_device *netdev,
845 			    struct ethtool_channels *channels)
846 {
847 	struct ena_adapter *adapter = netdev_priv(netdev);
848 	u32 count = channels->combined_count;
849 	/* The check for max value is already done in ethtool */
850 	if (count < ENA_MIN_NUM_IO_QUEUES ||
851 	    (ena_xdp_present(adapter) &&
852 	    !ena_xdp_legal_queue_count(adapter, count)))
853 		return -EINVAL;
854 
855 	return ena_update_queue_count(adapter, count);
856 }
857 
858 static int ena_get_tunable(struct net_device *netdev,
859 			   const struct ethtool_tunable *tuna, void *data)
860 {
861 	struct ena_adapter *adapter = netdev_priv(netdev);
862 	int ret = 0;
863 
864 	switch (tuna->id) {
865 	case ETHTOOL_RX_COPYBREAK:
866 		*(u32 *)data = adapter->rx_copybreak;
867 		break;
868 	default:
869 		ret = -EINVAL;
870 		break;
871 	}
872 
873 	return ret;
874 }
875 
876 static int ena_set_tunable(struct net_device *netdev,
877 			   const struct ethtool_tunable *tuna,
878 			   const void *data)
879 {
880 	struct ena_adapter *adapter = netdev_priv(netdev);
881 	int ret = 0;
882 	u32 len;
883 
884 	switch (tuna->id) {
885 	case ETHTOOL_RX_COPYBREAK:
886 		len = *(u32 *)data;
887 		if (len > adapter->netdev->mtu) {
888 			ret = -EINVAL;
889 			break;
890 		}
891 		adapter->rx_copybreak = len;
892 		break;
893 	default:
894 		ret = -EINVAL;
895 		break;
896 	}
897 
898 	return ret;
899 }
900 
901 static const struct ethtool_ops ena_ethtool_ops = {
902 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
903 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
904 	.get_link_ksettings	= ena_get_link_ksettings,
905 	.get_drvinfo		= ena_get_drvinfo,
906 	.get_msglevel		= ena_get_msglevel,
907 	.set_msglevel		= ena_set_msglevel,
908 	.get_link		= ethtool_op_get_link,
909 	.get_coalesce		= ena_get_coalesce,
910 	.set_coalesce		= ena_set_coalesce,
911 	.get_ringparam		= ena_get_ringparam,
912 	.set_ringparam		= ena_set_ringparam,
913 	.get_sset_count         = ena_get_sset_count,
914 	.get_strings		= ena_get_ethtool_strings,
915 	.get_ethtool_stats      = ena_get_ethtool_stats,
916 	.get_rxnfc		= ena_get_rxnfc,
917 	.set_rxnfc		= ena_set_rxnfc,
918 	.get_rxfh_indir_size    = ena_get_rxfh_indir_size,
919 	.get_rxfh_key_size	= ena_get_rxfh_key_size,
920 	.get_rxfh		= ena_get_rxfh,
921 	.set_rxfh		= ena_set_rxfh,
922 	.get_channels		= ena_get_channels,
923 	.set_channels		= ena_set_channels,
924 	.get_tunable		= ena_get_tunable,
925 	.set_tunable		= ena_set_tunable,
926 	.get_ts_info            = ethtool_op_get_ts_info,
927 };
928 
929 void ena_set_ethtool_ops(struct net_device *netdev)
930 {
931 	netdev->ethtool_ops = &ena_ethtool_ops;
932 }
933 
934 static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
935 {
936 	struct net_device *netdev = adapter->netdev;
937 	u8 *strings_buf;
938 	u64 *data_buf;
939 	int strings_num;
940 	int i, rc;
941 
942 	strings_num = ena_get_sw_stats_count(adapter);
943 	if (strings_num <= 0) {
944 		netif_err(adapter, drv, netdev, "Can't get stats num\n");
945 		return;
946 	}
947 
948 	strings_buf = devm_kcalloc(&adapter->pdev->dev,
949 				   ETH_GSTRING_LEN, strings_num,
950 				   GFP_ATOMIC);
951 	if (!strings_buf) {
952 		netif_err(adapter, drv, netdev,
953 			  "Failed to allocate strings_buf\n");
954 		return;
955 	}
956 
957 	data_buf = devm_kcalloc(&adapter->pdev->dev,
958 				strings_num, sizeof(u64),
959 				GFP_ATOMIC);
960 	if (!data_buf) {
961 		netif_err(adapter, drv, netdev,
962 			  "Failed to allocate data buf\n");
963 		devm_kfree(&adapter->pdev->dev, strings_buf);
964 		return;
965 	}
966 
967 	ena_get_strings(adapter, strings_buf, false);
968 	ena_get_stats(adapter, data_buf, false);
969 
970 	/* If there is a buffer, dump stats, otherwise print them to dmesg */
971 	if (buf)
972 		for (i = 0; i < strings_num; i++) {
973 			rc = snprintf(buf, ETH_GSTRING_LEN + sizeof(u64),
974 				      "%s %llu\n",
975 				      strings_buf + i * ETH_GSTRING_LEN,
976 				      data_buf[i]);
977 			buf += rc;
978 		}
979 	else
980 		for (i = 0; i < strings_num; i++)
981 			netif_err(adapter, drv, netdev, "%s: %llu\n",
982 				  strings_buf + i * ETH_GSTRING_LEN,
983 				  data_buf[i]);
984 
985 	devm_kfree(&adapter->pdev->dev, strings_buf);
986 	devm_kfree(&adapter->pdev->dev, data_buf);
987 }
988 
989 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf)
990 {
991 	if (!buf)
992 		return;
993 
994 	ena_dump_stats_ex(adapter, buf);
995 }
996 
997 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter)
998 {
999 	ena_dump_stats_ex(adapter, NULL);
1000 }
1001