xref: /linux/drivers/net/ethernet/amazon/ena/ena_ethtool.c (revision bd628c1bed7902ec1f24ba0fe70758949146abbe)
1 /*
2  * Copyright 2015 Amazon.com, Inc. or its affiliates.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/pci.h>
34 
35 #include "ena_netdev.h"
36 
37 struct ena_stats {
38 	char name[ETH_GSTRING_LEN];
39 	int stat_offset;
40 };
41 
42 #define ENA_STAT_ENA_COM_ENTRY(stat) { \
43 	.name = #stat, \
44 	.stat_offset = offsetof(struct ena_com_stats_admin, stat) \
45 }
46 
47 #define ENA_STAT_ENTRY(stat, stat_type) { \
48 	.name = #stat, \
49 	.stat_offset = offsetof(struct ena_stats_##stat_type, stat) \
50 }
51 
52 #define ENA_STAT_RX_ENTRY(stat) \
53 	ENA_STAT_ENTRY(stat, rx)
54 
55 #define ENA_STAT_TX_ENTRY(stat) \
56 	ENA_STAT_ENTRY(stat, tx)
57 
58 #define ENA_STAT_GLOBAL_ENTRY(stat) \
59 	ENA_STAT_ENTRY(stat, dev)
60 
61 static const struct ena_stats ena_stats_global_strings[] = {
62 	ENA_STAT_GLOBAL_ENTRY(tx_timeout),
63 	ENA_STAT_GLOBAL_ENTRY(suspend),
64 	ENA_STAT_GLOBAL_ENTRY(resume),
65 	ENA_STAT_GLOBAL_ENTRY(wd_expired),
66 	ENA_STAT_GLOBAL_ENTRY(interface_up),
67 	ENA_STAT_GLOBAL_ENTRY(interface_down),
68 	ENA_STAT_GLOBAL_ENTRY(admin_q_pause),
69 };
70 
71 static const struct ena_stats ena_stats_tx_strings[] = {
72 	ENA_STAT_TX_ENTRY(cnt),
73 	ENA_STAT_TX_ENTRY(bytes),
74 	ENA_STAT_TX_ENTRY(queue_stop),
75 	ENA_STAT_TX_ENTRY(queue_wakeup),
76 	ENA_STAT_TX_ENTRY(dma_mapping_err),
77 	ENA_STAT_TX_ENTRY(linearize),
78 	ENA_STAT_TX_ENTRY(linearize_failed),
79 	ENA_STAT_TX_ENTRY(napi_comp),
80 	ENA_STAT_TX_ENTRY(tx_poll),
81 	ENA_STAT_TX_ENTRY(doorbells),
82 	ENA_STAT_TX_ENTRY(prepare_ctx_err),
83 	ENA_STAT_TX_ENTRY(bad_req_id),
84 	ENA_STAT_TX_ENTRY(llq_buffer_copy),
85 	ENA_STAT_TX_ENTRY(missed_tx),
86 };
87 
88 static const struct ena_stats ena_stats_rx_strings[] = {
89 	ENA_STAT_RX_ENTRY(cnt),
90 	ENA_STAT_RX_ENTRY(bytes),
91 	ENA_STAT_RX_ENTRY(refil_partial),
92 	ENA_STAT_RX_ENTRY(bad_csum),
93 	ENA_STAT_RX_ENTRY(page_alloc_fail),
94 	ENA_STAT_RX_ENTRY(skb_alloc_fail),
95 	ENA_STAT_RX_ENTRY(dma_mapping_err),
96 	ENA_STAT_RX_ENTRY(bad_desc_num),
97 	ENA_STAT_RX_ENTRY(rx_copybreak_pkt),
98 	ENA_STAT_RX_ENTRY(bad_req_id),
99 	ENA_STAT_RX_ENTRY(empty_rx_ring),
100 	ENA_STAT_RX_ENTRY(csum_unchecked),
101 };
102 
103 static const struct ena_stats ena_stats_ena_com_strings[] = {
104 	ENA_STAT_ENA_COM_ENTRY(aborted_cmd),
105 	ENA_STAT_ENA_COM_ENTRY(submitted_cmd),
106 	ENA_STAT_ENA_COM_ENTRY(completed_cmd),
107 	ENA_STAT_ENA_COM_ENTRY(out_of_space),
108 	ENA_STAT_ENA_COM_ENTRY(no_completion),
109 };
110 
111 #define ENA_STATS_ARRAY_GLOBAL	ARRAY_SIZE(ena_stats_global_strings)
112 #define ENA_STATS_ARRAY_TX	ARRAY_SIZE(ena_stats_tx_strings)
113 #define ENA_STATS_ARRAY_RX	ARRAY_SIZE(ena_stats_rx_strings)
114 #define ENA_STATS_ARRAY_ENA_COM	ARRAY_SIZE(ena_stats_ena_com_strings)
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_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 *)((uintptr_t)&ring->tx_stats +
143 				(uintptr_t)ena_stats->stat_offset);
144 
145 			ena_safe_update_stat(ptr, (*data)++, &ring->syncp);
146 		}
147 
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 *)((uintptr_t)&ring->rx_stats +
155 				(uintptr_t)ena_stats->stat_offset);
156 
157 			ena_safe_update_stat(ptr, (*data)++, &ring->syncp);
158 		}
159 	}
160 }
161 
162 static void ena_dev_admin_queue_stats(struct ena_adapter *adapter, u64 **data)
163 {
164 	const struct ena_stats *ena_stats;
165 	u32 *ptr;
166 	int i;
167 
168 	for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
169 		ena_stats = &ena_stats_ena_com_strings[i];
170 
171 		ptr = (u32 *)((uintptr_t)&adapter->ena_dev->admin_queue.stats +
172 			(uintptr_t)ena_stats->stat_offset);
173 
174 		*(*data)++ = *ptr;
175 	}
176 }
177 
178 static void ena_get_ethtool_stats(struct net_device *netdev,
179 				  struct ethtool_stats *stats,
180 				  u64 *data)
181 {
182 	struct ena_adapter *adapter = netdev_priv(netdev);
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 *)((uintptr_t)&adapter->dev_stats +
191 			(uintptr_t)ena_stats->stat_offset);
192 
193 		ena_safe_update_stat(ptr, data++, &adapter->syncp);
194 	}
195 
196 	ena_queue_stats(adapter, &data);
197 	ena_dev_admin_queue_stats(adapter, &data);
198 }
199 
200 int ena_get_sset_count(struct net_device *netdev, int sset)
201 {
202 	struct ena_adapter *adapter = netdev_priv(netdev);
203 
204 	if (sset != ETH_SS_STATS)
205 		return -EOPNOTSUPP;
206 
207 	return  adapter->num_queues * (ENA_STATS_ARRAY_TX + ENA_STATS_ARRAY_RX)
208 		+ ENA_STATS_ARRAY_GLOBAL + ENA_STATS_ARRAY_ENA_COM;
209 }
210 
211 static void ena_queue_strings(struct ena_adapter *adapter, u8 **data)
212 {
213 	const struct ena_stats *ena_stats;
214 	int i, j;
215 
216 	for (i = 0; i < adapter->num_queues; i++) {
217 		/* Tx stats */
218 		for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
219 			ena_stats = &ena_stats_tx_strings[j];
220 
221 			snprintf(*data, ETH_GSTRING_LEN,
222 				 "queue_%u_tx_%s", i, ena_stats->name);
223 			 (*data) += ETH_GSTRING_LEN;
224 		}
225 		/* Rx stats */
226 		for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
227 			ena_stats = &ena_stats_rx_strings[j];
228 
229 			snprintf(*data, ETH_GSTRING_LEN,
230 				 "queue_%u_rx_%s", i, ena_stats->name);
231 			(*data) += ETH_GSTRING_LEN;
232 		}
233 	}
234 }
235 
236 static void ena_com_dev_strings(u8 **data)
237 {
238 	const struct ena_stats *ena_stats;
239 	int i;
240 
241 	for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
242 		ena_stats = &ena_stats_ena_com_strings[i];
243 
244 		snprintf(*data, ETH_GSTRING_LEN,
245 			 "ena_admin_q_%s", ena_stats->name);
246 		(*data) += ETH_GSTRING_LEN;
247 	}
248 }
249 
250 static void ena_get_strings(struct net_device *netdev, u32 sset, u8 *data)
251 {
252 	struct ena_adapter *adapter = netdev_priv(netdev);
253 	const struct ena_stats *ena_stats;
254 	int i;
255 
256 	if (sset != ETH_SS_STATS)
257 		return;
258 
259 	for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
260 		ena_stats = &ena_stats_global_strings[i];
261 
262 		memcpy(data, ena_stats->name, ETH_GSTRING_LEN);
263 		data += ETH_GSTRING_LEN;
264 	}
265 
266 	ena_queue_strings(adapter, &data);
267 	ena_com_dev_strings(&data);
268 }
269 
270 static int ena_get_link_ksettings(struct net_device *netdev,
271 				  struct ethtool_link_ksettings *link_ksettings)
272 {
273 	struct ena_adapter *adapter = netdev_priv(netdev);
274 	struct ena_com_dev *ena_dev = adapter->ena_dev;
275 	struct ena_admin_get_feature_link_desc *link;
276 	struct ena_admin_get_feat_resp feat_resp;
277 	int rc;
278 
279 	rc = ena_com_get_link_params(ena_dev, &feat_resp);
280 	if (rc)
281 		return rc;
282 
283 	link = &feat_resp.u.link;
284 	link_ksettings->base.speed = link->speed;
285 
286 	if (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) {
287 		ethtool_link_ksettings_add_link_mode(link_ksettings,
288 						     supported, Autoneg);
289 		ethtool_link_ksettings_add_link_mode(link_ksettings,
290 						     supported, Autoneg);
291 	}
292 
293 	link_ksettings->base.autoneg =
294 		(link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) ?
295 		AUTONEG_ENABLE : AUTONEG_DISABLE;
296 
297 	link_ksettings->base.duplex = DUPLEX_FULL;
298 
299 	return 0;
300 }
301 
302 static int ena_get_coalesce(struct net_device *net_dev,
303 			    struct ethtool_coalesce *coalesce)
304 {
305 	struct ena_adapter *adapter = netdev_priv(net_dev);
306 	struct ena_com_dev *ena_dev = adapter->ena_dev;
307 	struct ena_intr_moder_entry intr_moder_entry;
308 
309 	if (!ena_com_interrupt_moderation_supported(ena_dev)) {
310 		/* the devie doesn't support interrupt moderation */
311 		return -EOPNOTSUPP;
312 	}
313 	coalesce->tx_coalesce_usecs =
314 		ena_com_get_nonadaptive_moderation_interval_tx(ena_dev) /
315 			ena_dev->intr_delay_resolution;
316 	if (!ena_com_get_adaptive_moderation_enabled(ena_dev)) {
317 		coalesce->rx_coalesce_usecs =
318 			ena_com_get_nonadaptive_moderation_interval_rx(ena_dev)
319 			/ ena_dev->intr_delay_resolution;
320 	} else {
321 		ena_com_get_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_LOWEST, &intr_moder_entry);
322 		coalesce->rx_coalesce_usecs_low = intr_moder_entry.intr_moder_interval;
323 		coalesce->rx_max_coalesced_frames_low = intr_moder_entry.pkts_per_interval;
324 
325 		ena_com_get_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_MID, &intr_moder_entry);
326 		coalesce->rx_coalesce_usecs = intr_moder_entry.intr_moder_interval;
327 		coalesce->rx_max_coalesced_frames = intr_moder_entry.pkts_per_interval;
328 
329 		ena_com_get_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_HIGHEST, &intr_moder_entry);
330 		coalesce->rx_coalesce_usecs_high = intr_moder_entry.intr_moder_interval;
331 		coalesce->rx_max_coalesced_frames_high = intr_moder_entry.pkts_per_interval;
332 	}
333 	coalesce->use_adaptive_rx_coalesce =
334 		ena_com_get_adaptive_moderation_enabled(ena_dev);
335 
336 	return 0;
337 }
338 
339 static void ena_update_tx_rings_intr_moderation(struct ena_adapter *adapter)
340 {
341 	unsigned int val;
342 	int i;
343 
344 	val = ena_com_get_nonadaptive_moderation_interval_tx(adapter->ena_dev);
345 
346 	for (i = 0; i < adapter->num_queues; i++)
347 		adapter->tx_ring[i].smoothed_interval = val;
348 }
349 
350 static int ena_set_coalesce(struct net_device *net_dev,
351 			    struct ethtool_coalesce *coalesce)
352 {
353 	struct ena_adapter *adapter = netdev_priv(net_dev);
354 	struct ena_com_dev *ena_dev = adapter->ena_dev;
355 	struct ena_intr_moder_entry intr_moder_entry;
356 	int rc;
357 
358 	if (!ena_com_interrupt_moderation_supported(ena_dev)) {
359 		/* the devie doesn't support interrupt moderation */
360 		return -EOPNOTSUPP;
361 	}
362 
363 	if (coalesce->rx_coalesce_usecs_irq ||
364 	    coalesce->rx_max_coalesced_frames_irq ||
365 	    coalesce->tx_coalesce_usecs_irq ||
366 	    coalesce->tx_max_coalesced_frames ||
367 	    coalesce->tx_max_coalesced_frames_irq ||
368 	    coalesce->stats_block_coalesce_usecs ||
369 	    coalesce->use_adaptive_tx_coalesce ||
370 	    coalesce->pkt_rate_low ||
371 	    coalesce->tx_coalesce_usecs_low ||
372 	    coalesce->tx_max_coalesced_frames_low ||
373 	    coalesce->pkt_rate_high ||
374 	    coalesce->tx_coalesce_usecs_high ||
375 	    coalesce->tx_max_coalesced_frames_high ||
376 	    coalesce->rate_sample_interval)
377 		return -EINVAL;
378 
379 	rc = ena_com_update_nonadaptive_moderation_interval_tx(ena_dev,
380 							       coalesce->tx_coalesce_usecs);
381 	if (rc)
382 		return rc;
383 
384 	ena_update_tx_rings_intr_moderation(adapter);
385 
386 	if (ena_com_get_adaptive_moderation_enabled(ena_dev)) {
387 		if (!coalesce->use_adaptive_rx_coalesce) {
388 			ena_com_disable_adaptive_moderation(ena_dev);
389 			rc = ena_com_update_nonadaptive_moderation_interval_rx(ena_dev,
390 									       coalesce->rx_coalesce_usecs);
391 			return rc;
392 		}
393 	} else { /* was in non-adaptive mode */
394 		if (coalesce->use_adaptive_rx_coalesce) {
395 			ena_com_enable_adaptive_moderation(ena_dev);
396 		} else {
397 			rc = ena_com_update_nonadaptive_moderation_interval_rx(ena_dev,
398 									       coalesce->rx_coalesce_usecs);
399 			return rc;
400 		}
401 	}
402 
403 	intr_moder_entry.intr_moder_interval = coalesce->rx_coalesce_usecs_low;
404 	intr_moder_entry.pkts_per_interval = coalesce->rx_max_coalesced_frames_low;
405 	intr_moder_entry.bytes_per_interval = ENA_INTR_BYTE_COUNT_NOT_SUPPORTED;
406 	ena_com_init_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_LOWEST, &intr_moder_entry);
407 
408 	intr_moder_entry.intr_moder_interval = coalesce->rx_coalesce_usecs;
409 	intr_moder_entry.pkts_per_interval = coalesce->rx_max_coalesced_frames;
410 	intr_moder_entry.bytes_per_interval = ENA_INTR_BYTE_COUNT_NOT_SUPPORTED;
411 	ena_com_init_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_MID, &intr_moder_entry);
412 
413 	intr_moder_entry.intr_moder_interval = coalesce->rx_coalesce_usecs_high;
414 	intr_moder_entry.pkts_per_interval = coalesce->rx_max_coalesced_frames_high;
415 	intr_moder_entry.bytes_per_interval = ENA_INTR_BYTE_COUNT_NOT_SUPPORTED;
416 	ena_com_init_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_HIGHEST, &intr_moder_entry);
417 
418 	return 0;
419 }
420 
421 static u32 ena_get_msglevel(struct net_device *netdev)
422 {
423 	struct ena_adapter *adapter = netdev_priv(netdev);
424 
425 	return adapter->msg_enable;
426 }
427 
428 static void ena_set_msglevel(struct net_device *netdev, u32 value)
429 {
430 	struct ena_adapter *adapter = netdev_priv(netdev);
431 
432 	adapter->msg_enable = value;
433 }
434 
435 static void ena_get_drvinfo(struct net_device *dev,
436 			    struct ethtool_drvinfo *info)
437 {
438 	struct ena_adapter *adapter = netdev_priv(dev);
439 
440 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
441 	strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
442 	strlcpy(info->bus_info, pci_name(adapter->pdev),
443 		sizeof(info->bus_info));
444 }
445 
446 static void ena_get_ringparam(struct net_device *netdev,
447 			      struct ethtool_ringparam *ring)
448 {
449 	struct ena_adapter *adapter = netdev_priv(netdev);
450 	struct ena_ring *tx_ring = &adapter->tx_ring[0];
451 	struct ena_ring *rx_ring = &adapter->rx_ring[0];
452 
453 	ring->rx_max_pending = rx_ring->ring_size;
454 	ring->tx_max_pending = tx_ring->ring_size;
455 	ring->rx_pending = rx_ring->ring_size;
456 	ring->tx_pending = tx_ring->ring_size;
457 }
458 
459 static u32 ena_flow_hash_to_flow_type(u16 hash_fields)
460 {
461 	u32 data = 0;
462 
463 	if (hash_fields & ENA_ADMIN_RSS_L2_DA)
464 		data |= RXH_L2DA;
465 
466 	if (hash_fields & ENA_ADMIN_RSS_L3_DA)
467 		data |= RXH_IP_DST;
468 
469 	if (hash_fields & ENA_ADMIN_RSS_L3_SA)
470 		data |= RXH_IP_SRC;
471 
472 	if (hash_fields & ENA_ADMIN_RSS_L4_DP)
473 		data |= RXH_L4_B_2_3;
474 
475 	if (hash_fields & ENA_ADMIN_RSS_L4_SP)
476 		data |= RXH_L4_B_0_1;
477 
478 	return data;
479 }
480 
481 static u16 ena_flow_data_to_flow_hash(u32 hash_fields)
482 {
483 	u16 data = 0;
484 
485 	if (hash_fields & RXH_L2DA)
486 		data |= ENA_ADMIN_RSS_L2_DA;
487 
488 	if (hash_fields & RXH_IP_DST)
489 		data |= ENA_ADMIN_RSS_L3_DA;
490 
491 	if (hash_fields & RXH_IP_SRC)
492 		data |= ENA_ADMIN_RSS_L3_SA;
493 
494 	if (hash_fields & RXH_L4_B_2_3)
495 		data |= ENA_ADMIN_RSS_L4_DP;
496 
497 	if (hash_fields & RXH_L4_B_0_1)
498 		data |= ENA_ADMIN_RSS_L4_SP;
499 
500 	return data;
501 }
502 
503 static int ena_get_rss_hash(struct ena_com_dev *ena_dev,
504 			    struct ethtool_rxnfc *cmd)
505 {
506 	enum ena_admin_flow_hash_proto proto;
507 	u16 hash_fields;
508 	int rc;
509 
510 	cmd->data = 0;
511 
512 	switch (cmd->flow_type) {
513 	case TCP_V4_FLOW:
514 		proto = ENA_ADMIN_RSS_TCP4;
515 		break;
516 	case UDP_V4_FLOW:
517 		proto = ENA_ADMIN_RSS_UDP4;
518 		break;
519 	case TCP_V6_FLOW:
520 		proto = ENA_ADMIN_RSS_TCP6;
521 		break;
522 	case UDP_V6_FLOW:
523 		proto = ENA_ADMIN_RSS_UDP6;
524 		break;
525 	case IPV4_FLOW:
526 		proto = ENA_ADMIN_RSS_IP4;
527 		break;
528 	case IPV6_FLOW:
529 		proto = ENA_ADMIN_RSS_IP6;
530 		break;
531 	case ETHER_FLOW:
532 		proto = ENA_ADMIN_RSS_NOT_IP;
533 		break;
534 	case AH_V4_FLOW:
535 	case ESP_V4_FLOW:
536 	case AH_V6_FLOW:
537 	case ESP_V6_FLOW:
538 	case SCTP_V4_FLOW:
539 	case AH_ESP_V4_FLOW:
540 		return -EOPNOTSUPP;
541 	default:
542 		return -EINVAL;
543 	}
544 
545 	rc = ena_com_get_hash_ctrl(ena_dev, proto, &hash_fields);
546 	if (rc)
547 		return rc;
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;
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;
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 = adapter->num_queues;
749 	channels->max_tx = adapter->num_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_kcalloc(&adapter->pdev->dev,
844 				   ETH_GSTRING_LEN, strings_num,
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_kcalloc(&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