xref: /linux/drivers/net/ethernet/amazon/ena/ena_ethtool.c (revision 13091aa30535b719e269f20a7bc34002bf5afae5)
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(rx_copybreak_pkt),
92 	ENA_STAT_RX_ENTRY(csum_good),
93 	ENA_STAT_RX_ENTRY(refil_partial),
94 	ENA_STAT_RX_ENTRY(bad_csum),
95 	ENA_STAT_RX_ENTRY(page_alloc_fail),
96 	ENA_STAT_RX_ENTRY(skb_alloc_fail),
97 	ENA_STAT_RX_ENTRY(dma_mapping_err),
98 	ENA_STAT_RX_ENTRY(bad_desc_num),
99 	ENA_STAT_RX_ENTRY(bad_req_id),
100 	ENA_STAT_RX_ENTRY(empty_rx_ring),
101 	ENA_STAT_RX_ENTRY(csum_unchecked),
102 };
103 
104 static const struct ena_stats ena_stats_ena_com_strings[] = {
105 	ENA_STAT_ENA_COM_ENTRY(aborted_cmd),
106 	ENA_STAT_ENA_COM_ENTRY(submitted_cmd),
107 	ENA_STAT_ENA_COM_ENTRY(completed_cmd),
108 	ENA_STAT_ENA_COM_ENTRY(out_of_space),
109 	ENA_STAT_ENA_COM_ENTRY(no_completion),
110 };
111 
112 #define ENA_STATS_ARRAY_GLOBAL	ARRAY_SIZE(ena_stats_global_strings)
113 #define ENA_STATS_ARRAY_TX	ARRAY_SIZE(ena_stats_tx_strings)
114 #define ENA_STATS_ARRAY_RX	ARRAY_SIZE(ena_stats_rx_strings)
115 #define ENA_STATS_ARRAY_ENA_COM	ARRAY_SIZE(ena_stats_ena_com_strings)
116 
117 static void ena_safe_update_stat(u64 *src, u64 *dst,
118 				 struct u64_stats_sync *syncp)
119 {
120 	unsigned int start;
121 
122 	do {
123 		start = u64_stats_fetch_begin_irq(syncp);
124 		*(dst) = *src;
125 	} while (u64_stats_fetch_retry_irq(syncp, start));
126 }
127 
128 static void ena_queue_stats(struct ena_adapter *adapter, u64 **data)
129 {
130 	const struct ena_stats *ena_stats;
131 	struct ena_ring *ring;
132 
133 	u64 *ptr;
134 	int i, j;
135 
136 	for (i = 0; i < adapter->num_queues; i++) {
137 		/* Tx stats */
138 		ring = &adapter->tx_ring[i];
139 
140 		for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
141 			ena_stats = &ena_stats_tx_strings[j];
142 
143 			ptr = (u64 *)((uintptr_t)&ring->tx_stats +
144 				(uintptr_t)ena_stats->stat_offset);
145 
146 			ena_safe_update_stat(ptr, (*data)++, &ring->syncp);
147 		}
148 
149 		/* Rx stats */
150 		ring = &adapter->rx_ring[i];
151 
152 		for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
153 			ena_stats = &ena_stats_rx_strings[j];
154 
155 			ptr = (u64 *)((uintptr_t)&ring->rx_stats +
156 				(uintptr_t)ena_stats->stat_offset);
157 
158 			ena_safe_update_stat(ptr, (*data)++, &ring->syncp);
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 	u32 *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 = (u32 *)((uintptr_t)&adapter->ena_dev->admin_queue.stats +
173 			(uintptr_t)ena_stats->stat_offset);
174 
175 		*(*data)++ = *ptr;
176 	}
177 }
178 
179 static void ena_get_ethtool_stats(struct net_device *netdev,
180 				  struct ethtool_stats *stats,
181 				  u64 *data)
182 {
183 	struct ena_adapter *adapter = netdev_priv(netdev);
184 	const struct ena_stats *ena_stats;
185 	u64 *ptr;
186 	int i;
187 
188 	for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
189 		ena_stats = &ena_stats_global_strings[i];
190 
191 		ptr = (u64 *)((uintptr_t)&adapter->dev_stats +
192 			(uintptr_t)ena_stats->stat_offset);
193 
194 		ena_safe_update_stat(ptr, data++, &adapter->syncp);
195 	}
196 
197 	ena_queue_stats(adapter, &data);
198 	ena_dev_admin_queue_stats(adapter, &data);
199 }
200 
201 static int get_stats_sset_count(struct ena_adapter *adapter)
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 int ena_get_sset_count(struct net_device *netdev, int sset)
208 {
209 	struct ena_adapter *adapter = netdev_priv(netdev);
210 
211 	switch (sset) {
212 	case ETH_SS_STATS:
213 		return get_stats_sset_count(adapter);
214 	case ETH_SS_PRIV_FLAGS:
215 		return adapter->ena_extra_properties_count;
216 	default:
217 		return -EOPNOTSUPP;
218 	}
219 }
220 
221 static void ena_queue_strings(struct ena_adapter *adapter, u8 **data)
222 {
223 	const struct ena_stats *ena_stats;
224 	int i, j;
225 
226 	for (i = 0; i < adapter->num_queues; i++) {
227 		/* Tx stats */
228 		for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
229 			ena_stats = &ena_stats_tx_strings[j];
230 
231 			snprintf(*data, ETH_GSTRING_LEN,
232 				 "queue_%u_tx_%s", i, ena_stats->name);
233 			 (*data) += ETH_GSTRING_LEN;
234 		}
235 		/* Rx stats */
236 		for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
237 			ena_stats = &ena_stats_rx_strings[j];
238 
239 			snprintf(*data, ETH_GSTRING_LEN,
240 				 "queue_%u_rx_%s", i, ena_stats->name);
241 			(*data) += ETH_GSTRING_LEN;
242 		}
243 	}
244 }
245 
246 static void ena_com_dev_strings(u8 **data)
247 {
248 	const struct ena_stats *ena_stats;
249 	int i;
250 
251 	for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
252 		ena_stats = &ena_stats_ena_com_strings[i];
253 
254 		snprintf(*data, ETH_GSTRING_LEN,
255 			 "ena_admin_q_%s", ena_stats->name);
256 		(*data) += ETH_GSTRING_LEN;
257 	}
258 }
259 
260 static void get_stats_strings(struct ena_adapter *adapter, u8 *data)
261 {
262 	const struct ena_stats *ena_stats;
263 	int i;
264 
265 	for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
266 		ena_stats = &ena_stats_global_strings[i];
267 		memcpy(data, ena_stats->name, ETH_GSTRING_LEN);
268 		data += ETH_GSTRING_LEN;
269 	}
270 	ena_queue_strings(adapter, &data);
271 	ena_com_dev_strings(&data);
272 }
273 
274 static void get_private_flags_strings(struct ena_adapter *adapter, u8 *data)
275 {
276 	struct ena_com_dev *ena_dev = adapter->ena_dev;
277 	u8 *strings = ena_dev->extra_properties_strings.virt_addr;
278 	int i;
279 
280 	if (unlikely(!strings)) {
281 		adapter->ena_extra_properties_count = 0;
282 		return;
283 	}
284 
285 	for (i = 0; i < adapter->ena_extra_properties_count; i++) {
286 		strlcpy(data, strings + ENA_ADMIN_EXTRA_PROPERTIES_STRING_LEN * i,
287 			ETH_GSTRING_LEN);
288 		data += ETH_GSTRING_LEN;
289 	}
290 }
291 
292 static void ena_get_strings(struct net_device *netdev, u32 sset, u8 *data)
293 {
294 	struct ena_adapter *adapter = netdev_priv(netdev);
295 
296 	switch (sset) {
297 	case ETH_SS_STATS:
298 		get_stats_strings(adapter, data);
299 		break;
300 	case ETH_SS_PRIV_FLAGS:
301 		get_private_flags_strings(adapter, data);
302 		break;
303 	default:
304 		break;
305 	}
306 }
307 
308 static int ena_get_link_ksettings(struct net_device *netdev,
309 				  struct ethtool_link_ksettings *link_ksettings)
310 {
311 	struct ena_adapter *adapter = netdev_priv(netdev);
312 	struct ena_com_dev *ena_dev = adapter->ena_dev;
313 	struct ena_admin_get_feature_link_desc *link;
314 	struct ena_admin_get_feat_resp feat_resp;
315 	int rc;
316 
317 	rc = ena_com_get_link_params(ena_dev, &feat_resp);
318 	if (rc)
319 		return rc;
320 
321 	link = &feat_resp.u.link;
322 	link_ksettings->base.speed = link->speed;
323 
324 	if (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) {
325 		ethtool_link_ksettings_add_link_mode(link_ksettings,
326 						     supported, Autoneg);
327 		ethtool_link_ksettings_add_link_mode(link_ksettings,
328 						     supported, Autoneg);
329 	}
330 
331 	link_ksettings->base.autoneg =
332 		(link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) ?
333 		AUTONEG_ENABLE : AUTONEG_DISABLE;
334 
335 	link_ksettings->base.duplex = DUPLEX_FULL;
336 
337 	return 0;
338 }
339 
340 static int ena_get_coalesce(struct net_device *net_dev,
341 			    struct ethtool_coalesce *coalesce)
342 {
343 	struct ena_adapter *adapter = netdev_priv(net_dev);
344 	struct ena_com_dev *ena_dev = adapter->ena_dev;
345 	struct ena_intr_moder_entry intr_moder_entry;
346 
347 	if (!ena_com_interrupt_moderation_supported(ena_dev)) {
348 		/* the devie doesn't support interrupt moderation */
349 		return -EOPNOTSUPP;
350 	}
351 	coalesce->tx_coalesce_usecs =
352 		ena_com_get_nonadaptive_moderation_interval_tx(ena_dev) /
353 			ena_dev->intr_delay_resolution;
354 	if (!ena_com_get_adaptive_moderation_enabled(ena_dev)) {
355 		coalesce->rx_coalesce_usecs =
356 			ena_com_get_nonadaptive_moderation_interval_rx(ena_dev)
357 			/ ena_dev->intr_delay_resolution;
358 	} else {
359 		ena_com_get_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_LOWEST, &intr_moder_entry);
360 		coalesce->rx_coalesce_usecs_low = intr_moder_entry.intr_moder_interval;
361 		coalesce->rx_max_coalesced_frames_low = intr_moder_entry.pkts_per_interval;
362 
363 		ena_com_get_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_MID, &intr_moder_entry);
364 		coalesce->rx_coalesce_usecs = intr_moder_entry.intr_moder_interval;
365 		coalesce->rx_max_coalesced_frames = intr_moder_entry.pkts_per_interval;
366 
367 		ena_com_get_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_HIGHEST, &intr_moder_entry);
368 		coalesce->rx_coalesce_usecs_high = intr_moder_entry.intr_moder_interval;
369 		coalesce->rx_max_coalesced_frames_high = intr_moder_entry.pkts_per_interval;
370 	}
371 	coalesce->use_adaptive_rx_coalesce =
372 		ena_com_get_adaptive_moderation_enabled(ena_dev);
373 
374 	return 0;
375 }
376 
377 static void ena_update_tx_rings_intr_moderation(struct ena_adapter *adapter)
378 {
379 	unsigned int val;
380 	int i;
381 
382 	val = ena_com_get_nonadaptive_moderation_interval_tx(adapter->ena_dev);
383 
384 	for (i = 0; i < adapter->num_queues; i++)
385 		adapter->tx_ring[i].smoothed_interval = val;
386 }
387 
388 static int ena_set_coalesce(struct net_device *net_dev,
389 			    struct ethtool_coalesce *coalesce)
390 {
391 	struct ena_adapter *adapter = netdev_priv(net_dev);
392 	struct ena_com_dev *ena_dev = adapter->ena_dev;
393 	struct ena_intr_moder_entry intr_moder_entry;
394 	int rc;
395 
396 	if (!ena_com_interrupt_moderation_supported(ena_dev)) {
397 		/* the devie doesn't support interrupt moderation */
398 		return -EOPNOTSUPP;
399 	}
400 
401 	if (coalesce->rx_coalesce_usecs_irq ||
402 	    coalesce->rx_max_coalesced_frames_irq ||
403 	    coalesce->tx_coalesce_usecs_irq ||
404 	    coalesce->tx_max_coalesced_frames ||
405 	    coalesce->tx_max_coalesced_frames_irq ||
406 	    coalesce->stats_block_coalesce_usecs ||
407 	    coalesce->use_adaptive_tx_coalesce ||
408 	    coalesce->pkt_rate_low ||
409 	    coalesce->tx_coalesce_usecs_low ||
410 	    coalesce->tx_max_coalesced_frames_low ||
411 	    coalesce->pkt_rate_high ||
412 	    coalesce->tx_coalesce_usecs_high ||
413 	    coalesce->tx_max_coalesced_frames_high ||
414 	    coalesce->rate_sample_interval)
415 		return -EINVAL;
416 
417 	rc = ena_com_update_nonadaptive_moderation_interval_tx(ena_dev,
418 							       coalesce->tx_coalesce_usecs);
419 	if (rc)
420 		return rc;
421 
422 	ena_update_tx_rings_intr_moderation(adapter);
423 
424 	if (ena_com_get_adaptive_moderation_enabled(ena_dev)) {
425 		if (!coalesce->use_adaptive_rx_coalesce) {
426 			ena_com_disable_adaptive_moderation(ena_dev);
427 			rc = ena_com_update_nonadaptive_moderation_interval_rx(ena_dev,
428 									       coalesce->rx_coalesce_usecs);
429 			return rc;
430 		}
431 	} else { /* was in non-adaptive mode */
432 		if (coalesce->use_adaptive_rx_coalesce) {
433 			ena_com_enable_adaptive_moderation(ena_dev);
434 		} else {
435 			rc = ena_com_update_nonadaptive_moderation_interval_rx(ena_dev,
436 									       coalesce->rx_coalesce_usecs);
437 			return rc;
438 		}
439 	}
440 
441 	intr_moder_entry.intr_moder_interval = coalesce->rx_coalesce_usecs_low;
442 	intr_moder_entry.pkts_per_interval = coalesce->rx_max_coalesced_frames_low;
443 	intr_moder_entry.bytes_per_interval = ENA_INTR_BYTE_COUNT_NOT_SUPPORTED;
444 	ena_com_init_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_LOWEST, &intr_moder_entry);
445 
446 	intr_moder_entry.intr_moder_interval = coalesce->rx_coalesce_usecs;
447 	intr_moder_entry.pkts_per_interval = coalesce->rx_max_coalesced_frames;
448 	intr_moder_entry.bytes_per_interval = ENA_INTR_BYTE_COUNT_NOT_SUPPORTED;
449 	ena_com_init_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_MID, &intr_moder_entry);
450 
451 	intr_moder_entry.intr_moder_interval = coalesce->rx_coalesce_usecs_high;
452 	intr_moder_entry.pkts_per_interval = coalesce->rx_max_coalesced_frames_high;
453 	intr_moder_entry.bytes_per_interval = ENA_INTR_BYTE_COUNT_NOT_SUPPORTED;
454 	ena_com_init_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_HIGHEST, &intr_moder_entry);
455 
456 	return 0;
457 }
458 
459 static u32 ena_get_msglevel(struct net_device *netdev)
460 {
461 	struct ena_adapter *adapter = netdev_priv(netdev);
462 
463 	return adapter->msg_enable;
464 }
465 
466 static void ena_set_msglevel(struct net_device *netdev, u32 value)
467 {
468 	struct ena_adapter *adapter = netdev_priv(netdev);
469 
470 	adapter->msg_enable = value;
471 }
472 
473 static void ena_get_drvinfo(struct net_device *dev,
474 			    struct ethtool_drvinfo *info)
475 {
476 	struct ena_adapter *adapter = netdev_priv(dev);
477 
478 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
479 	strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
480 	strlcpy(info->bus_info, pci_name(adapter->pdev),
481 		sizeof(info->bus_info));
482 	info->n_priv_flags = adapter->ena_extra_properties_count;
483 }
484 
485 static void ena_get_ringparam(struct net_device *netdev,
486 			      struct ethtool_ringparam *ring)
487 {
488 	struct ena_adapter *adapter = netdev_priv(netdev);
489 
490 	ring->tx_max_pending = adapter->max_tx_ring_size;
491 	ring->rx_max_pending = adapter->max_rx_ring_size;
492 	ring->tx_pending = adapter->tx_ring[0].ring_size;
493 	ring->rx_pending = adapter->rx_ring[0].ring_size;
494 }
495 
496 static int ena_set_ringparam(struct net_device *netdev,
497 			     struct ethtool_ringparam *ring)
498 {
499 	struct ena_adapter *adapter = netdev_priv(netdev);
500 	u32 new_tx_size, new_rx_size;
501 
502 	new_tx_size = ring->tx_pending < ENA_MIN_RING_SIZE ?
503 			ENA_MIN_RING_SIZE : ring->tx_pending;
504 	new_tx_size = rounddown_pow_of_two(new_tx_size);
505 
506 	new_rx_size = ring->rx_pending < ENA_MIN_RING_SIZE ?
507 			ENA_MIN_RING_SIZE : ring->rx_pending;
508 	new_rx_size = rounddown_pow_of_two(new_rx_size);
509 
510 	if (new_tx_size == adapter->requested_tx_ring_size &&
511 	    new_rx_size == adapter->requested_rx_ring_size)
512 		return 0;
513 
514 	return ena_update_queue_sizes(adapter, new_tx_size, new_rx_size);
515 }
516 
517 static u32 ena_flow_hash_to_flow_type(u16 hash_fields)
518 {
519 	u32 data = 0;
520 
521 	if (hash_fields & ENA_ADMIN_RSS_L2_DA)
522 		data |= RXH_L2DA;
523 
524 	if (hash_fields & ENA_ADMIN_RSS_L3_DA)
525 		data |= RXH_IP_DST;
526 
527 	if (hash_fields & ENA_ADMIN_RSS_L3_SA)
528 		data |= RXH_IP_SRC;
529 
530 	if (hash_fields & ENA_ADMIN_RSS_L4_DP)
531 		data |= RXH_L4_B_2_3;
532 
533 	if (hash_fields & ENA_ADMIN_RSS_L4_SP)
534 		data |= RXH_L4_B_0_1;
535 
536 	return data;
537 }
538 
539 static u16 ena_flow_data_to_flow_hash(u32 hash_fields)
540 {
541 	u16 data = 0;
542 
543 	if (hash_fields & RXH_L2DA)
544 		data |= ENA_ADMIN_RSS_L2_DA;
545 
546 	if (hash_fields & RXH_IP_DST)
547 		data |= ENA_ADMIN_RSS_L3_DA;
548 
549 	if (hash_fields & RXH_IP_SRC)
550 		data |= ENA_ADMIN_RSS_L3_SA;
551 
552 	if (hash_fields & RXH_L4_B_2_3)
553 		data |= ENA_ADMIN_RSS_L4_DP;
554 
555 	if (hash_fields & RXH_L4_B_0_1)
556 		data |= ENA_ADMIN_RSS_L4_SP;
557 
558 	return data;
559 }
560 
561 static int ena_get_rss_hash(struct ena_com_dev *ena_dev,
562 			    struct ethtool_rxnfc *cmd)
563 {
564 	enum ena_admin_flow_hash_proto proto;
565 	u16 hash_fields;
566 	int rc;
567 
568 	cmd->data = 0;
569 
570 	switch (cmd->flow_type) {
571 	case TCP_V4_FLOW:
572 		proto = ENA_ADMIN_RSS_TCP4;
573 		break;
574 	case UDP_V4_FLOW:
575 		proto = ENA_ADMIN_RSS_UDP4;
576 		break;
577 	case TCP_V6_FLOW:
578 		proto = ENA_ADMIN_RSS_TCP6;
579 		break;
580 	case UDP_V6_FLOW:
581 		proto = ENA_ADMIN_RSS_UDP6;
582 		break;
583 	case IPV4_FLOW:
584 		proto = ENA_ADMIN_RSS_IP4;
585 		break;
586 	case IPV6_FLOW:
587 		proto = ENA_ADMIN_RSS_IP6;
588 		break;
589 	case ETHER_FLOW:
590 		proto = ENA_ADMIN_RSS_NOT_IP;
591 		break;
592 	case AH_V4_FLOW:
593 	case ESP_V4_FLOW:
594 	case AH_V6_FLOW:
595 	case ESP_V6_FLOW:
596 	case SCTP_V4_FLOW:
597 	case AH_ESP_V4_FLOW:
598 		return -EOPNOTSUPP;
599 	default:
600 		return -EINVAL;
601 	}
602 
603 	rc = ena_com_get_hash_ctrl(ena_dev, proto, &hash_fields);
604 	if (rc)
605 		return rc;
606 
607 	cmd->data = ena_flow_hash_to_flow_type(hash_fields);
608 
609 	return 0;
610 }
611 
612 static int ena_set_rss_hash(struct ena_com_dev *ena_dev,
613 			    struct ethtool_rxnfc *cmd)
614 {
615 	enum ena_admin_flow_hash_proto proto;
616 	u16 hash_fields;
617 
618 	switch (cmd->flow_type) {
619 	case TCP_V4_FLOW:
620 		proto = ENA_ADMIN_RSS_TCP4;
621 		break;
622 	case UDP_V4_FLOW:
623 		proto = ENA_ADMIN_RSS_UDP4;
624 		break;
625 	case TCP_V6_FLOW:
626 		proto = ENA_ADMIN_RSS_TCP6;
627 		break;
628 	case UDP_V6_FLOW:
629 		proto = ENA_ADMIN_RSS_UDP6;
630 		break;
631 	case IPV4_FLOW:
632 		proto = ENA_ADMIN_RSS_IP4;
633 		break;
634 	case IPV6_FLOW:
635 		proto = ENA_ADMIN_RSS_IP6;
636 		break;
637 	case ETHER_FLOW:
638 		proto = ENA_ADMIN_RSS_NOT_IP;
639 		break;
640 	case AH_V4_FLOW:
641 	case ESP_V4_FLOW:
642 	case AH_V6_FLOW:
643 	case ESP_V6_FLOW:
644 	case SCTP_V4_FLOW:
645 	case AH_ESP_V4_FLOW:
646 		return -EOPNOTSUPP;
647 	default:
648 		return -EINVAL;
649 	}
650 
651 	hash_fields = ena_flow_data_to_flow_hash(cmd->data);
652 
653 	return ena_com_fill_hash_ctrl(ena_dev, proto, hash_fields);
654 }
655 
656 static int ena_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info)
657 {
658 	struct ena_adapter *adapter = netdev_priv(netdev);
659 	int rc = 0;
660 
661 	switch (info->cmd) {
662 	case ETHTOOL_SRXFH:
663 		rc = ena_set_rss_hash(adapter->ena_dev, info);
664 		break;
665 	case ETHTOOL_SRXCLSRLDEL:
666 	case ETHTOOL_SRXCLSRLINS:
667 	default:
668 		netif_err(adapter, drv, netdev,
669 			  "Command parameter %d is not supported\n", info->cmd);
670 		rc = -EOPNOTSUPP;
671 	}
672 
673 	return rc;
674 }
675 
676 static int ena_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
677 			 u32 *rules)
678 {
679 	struct ena_adapter *adapter = netdev_priv(netdev);
680 	int rc = 0;
681 
682 	switch (info->cmd) {
683 	case ETHTOOL_GRXRINGS:
684 		info->data = adapter->num_queues;
685 		rc = 0;
686 		break;
687 	case ETHTOOL_GRXFH:
688 		rc = ena_get_rss_hash(adapter->ena_dev, info);
689 		break;
690 	case ETHTOOL_GRXCLSRLCNT:
691 	case ETHTOOL_GRXCLSRULE:
692 	case ETHTOOL_GRXCLSRLALL:
693 	default:
694 		netif_err(adapter, drv, netdev,
695 			  "Command parameter %d is not supported\n", info->cmd);
696 		rc = -EOPNOTSUPP;
697 	}
698 
699 	return rc;
700 }
701 
702 static u32 ena_get_rxfh_indir_size(struct net_device *netdev)
703 {
704 	return ENA_RX_RSS_TABLE_SIZE;
705 }
706 
707 static u32 ena_get_rxfh_key_size(struct net_device *netdev)
708 {
709 	return ENA_HASH_KEY_SIZE;
710 }
711 
712 static int ena_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
713 			u8 *hfunc)
714 {
715 	struct ena_adapter *adapter = netdev_priv(netdev);
716 	enum ena_admin_hash_functions ena_func;
717 	u8 func;
718 	int rc;
719 
720 	rc = ena_com_indirect_table_get(adapter->ena_dev, indir);
721 	if (rc)
722 		return rc;
723 
724 	rc = ena_com_get_hash_function(adapter->ena_dev, &ena_func, key);
725 	if (rc)
726 		return rc;
727 
728 	switch (ena_func) {
729 	case ENA_ADMIN_TOEPLITZ:
730 		func = ETH_RSS_HASH_TOP;
731 		break;
732 	case ENA_ADMIN_CRC32:
733 		func = ETH_RSS_HASH_XOR;
734 		break;
735 	default:
736 		netif_err(adapter, drv, netdev,
737 			  "Command parameter is not supported\n");
738 		return -EOPNOTSUPP;
739 	}
740 
741 	if (hfunc)
742 		*hfunc = func;
743 
744 	return rc;
745 }
746 
747 static int ena_set_rxfh(struct net_device *netdev, const u32 *indir,
748 			const u8 *key, const u8 hfunc)
749 {
750 	struct ena_adapter *adapter = netdev_priv(netdev);
751 	struct ena_com_dev *ena_dev = adapter->ena_dev;
752 	enum ena_admin_hash_functions func;
753 	int rc, i;
754 
755 	if (indir) {
756 		for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
757 			rc = ena_com_indirect_table_fill_entry(ena_dev,
758 							       i,
759 							       ENA_IO_RXQ_IDX(indir[i]));
760 			if (unlikely(rc)) {
761 				netif_err(adapter, drv, netdev,
762 					  "Cannot fill indirect table (index is too large)\n");
763 				return rc;
764 			}
765 		}
766 
767 		rc = ena_com_indirect_table_set(ena_dev);
768 		if (rc) {
769 			netif_err(adapter, drv, netdev,
770 				  "Cannot set indirect table\n");
771 			return rc == -EPERM ? -EOPNOTSUPP : rc;
772 		}
773 	}
774 
775 	switch (hfunc) {
776 	case ETH_RSS_HASH_TOP:
777 		func = ENA_ADMIN_TOEPLITZ;
778 		break;
779 	case ETH_RSS_HASH_XOR:
780 		func = ENA_ADMIN_CRC32;
781 		break;
782 	default:
783 		netif_err(adapter, drv, netdev, "Unsupported hfunc %d\n",
784 			  hfunc);
785 		return -EOPNOTSUPP;
786 	}
787 
788 	if (key) {
789 		rc = ena_com_fill_hash_function(ena_dev, func, key,
790 						ENA_HASH_KEY_SIZE,
791 						0xFFFFFFFF);
792 		if (unlikely(rc)) {
793 			netif_err(adapter, drv, netdev, "Cannot fill key\n");
794 			return rc == -EPERM ? -EOPNOTSUPP : rc;
795 		}
796 	}
797 
798 	return 0;
799 }
800 
801 static void ena_get_channels(struct net_device *netdev,
802 			     struct ethtool_channels *channels)
803 {
804 	struct ena_adapter *adapter = netdev_priv(netdev);
805 
806 	channels->max_rx = adapter->num_queues;
807 	channels->max_tx = adapter->num_queues;
808 	channels->max_other = 0;
809 	channels->max_combined = 0;
810 	channels->rx_count = adapter->num_queues;
811 	channels->tx_count = adapter->num_queues;
812 	channels->other_count = 0;
813 	channels->combined_count = 0;
814 }
815 
816 static int ena_get_tunable(struct net_device *netdev,
817 			   const struct ethtool_tunable *tuna, void *data)
818 {
819 	struct ena_adapter *adapter = netdev_priv(netdev);
820 	int ret = 0;
821 
822 	switch (tuna->id) {
823 	case ETHTOOL_RX_COPYBREAK:
824 		*(u32 *)data = adapter->rx_copybreak;
825 		break;
826 	default:
827 		ret = -EINVAL;
828 		break;
829 	}
830 
831 	return ret;
832 }
833 
834 static int ena_set_tunable(struct net_device *netdev,
835 			   const struct ethtool_tunable *tuna,
836 			   const void *data)
837 {
838 	struct ena_adapter *adapter = netdev_priv(netdev);
839 	int ret = 0;
840 	u32 len;
841 
842 	switch (tuna->id) {
843 	case ETHTOOL_RX_COPYBREAK:
844 		len = *(u32 *)data;
845 		if (len > adapter->netdev->mtu) {
846 			ret = -EINVAL;
847 			break;
848 		}
849 		adapter->rx_copybreak = len;
850 		break;
851 	default:
852 		ret = -EINVAL;
853 		break;
854 	}
855 
856 	return ret;
857 }
858 
859 static u32 ena_get_priv_flags(struct net_device *netdev)
860 {
861 	struct ena_adapter *adapter = netdev_priv(netdev);
862 	struct ena_com_dev *ena_dev = adapter->ena_dev;
863 	struct ena_admin_get_feat_resp get_resp;
864 	u32 rc;
865 
866 	rc = ena_com_get_extra_properties_flags(ena_dev, &get_resp);
867 	if (!rc)
868 		return get_resp.u.extra_properties_flags.flags;
869 
870 	return 0;
871 }
872 
873 static const struct ethtool_ops ena_ethtool_ops = {
874 	.get_link_ksettings	= ena_get_link_ksettings,
875 	.get_drvinfo		= ena_get_drvinfo,
876 	.get_msglevel		= ena_get_msglevel,
877 	.set_msglevel		= ena_set_msglevel,
878 	.get_link		= ethtool_op_get_link,
879 	.get_coalesce		= ena_get_coalesce,
880 	.set_coalesce		= ena_set_coalesce,
881 	.get_ringparam		= ena_get_ringparam,
882 	.set_ringparam		= ena_set_ringparam,
883 	.get_sset_count         = ena_get_sset_count,
884 	.get_strings		= ena_get_strings,
885 	.get_ethtool_stats      = ena_get_ethtool_stats,
886 	.get_rxnfc		= ena_get_rxnfc,
887 	.set_rxnfc		= ena_set_rxnfc,
888 	.get_rxfh_indir_size    = ena_get_rxfh_indir_size,
889 	.get_rxfh_key_size	= ena_get_rxfh_key_size,
890 	.get_rxfh		= ena_get_rxfh,
891 	.set_rxfh		= ena_set_rxfh,
892 	.get_channels		= ena_get_channels,
893 	.get_tunable		= ena_get_tunable,
894 	.set_tunable		= ena_set_tunable,
895 	.get_priv_flags		= ena_get_priv_flags,
896 };
897 
898 void ena_set_ethtool_ops(struct net_device *netdev)
899 {
900 	netdev->ethtool_ops = &ena_ethtool_ops;
901 }
902 
903 static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
904 {
905 	struct net_device *netdev = adapter->netdev;
906 	u8 *strings_buf;
907 	u64 *data_buf;
908 	int strings_num;
909 	int i, rc;
910 
911 	strings_num = ena_get_sset_count(netdev, ETH_SS_STATS);
912 	if (strings_num <= 0) {
913 		netif_err(adapter, drv, netdev, "Can't get stats num\n");
914 		return;
915 	}
916 
917 	strings_buf = devm_kcalloc(&adapter->pdev->dev,
918 				   ETH_GSTRING_LEN, strings_num,
919 				   GFP_ATOMIC);
920 	if (!strings_buf) {
921 		netif_err(adapter, drv, netdev,
922 			  "failed to alloc strings_buf\n");
923 		return;
924 	}
925 
926 	data_buf = devm_kcalloc(&adapter->pdev->dev,
927 				strings_num, sizeof(u64),
928 				GFP_ATOMIC);
929 	if (!data_buf) {
930 		netif_err(adapter, drv, netdev,
931 			  "failed to allocate data buf\n");
932 		devm_kfree(&adapter->pdev->dev, strings_buf);
933 		return;
934 	}
935 
936 	ena_get_strings(netdev, ETH_SS_STATS, strings_buf);
937 	ena_get_ethtool_stats(netdev, NULL, data_buf);
938 
939 	/* If there is a buffer, dump stats, otherwise print them to dmesg */
940 	if (buf)
941 		for (i = 0; i < strings_num; i++) {
942 			rc = snprintf(buf, ETH_GSTRING_LEN + sizeof(u64),
943 				      "%s %llu\n",
944 				      strings_buf + i * ETH_GSTRING_LEN,
945 				      data_buf[i]);
946 			buf += rc;
947 		}
948 	else
949 		for (i = 0; i < strings_num; i++)
950 			netif_err(adapter, drv, netdev, "%s: %llu\n",
951 				  strings_buf + i * ETH_GSTRING_LEN,
952 				  data_buf[i]);
953 
954 	devm_kfree(&adapter->pdev->dev, strings_buf);
955 	devm_kfree(&adapter->pdev->dev, data_buf);
956 }
957 
958 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf)
959 {
960 	if (!buf)
961 		return;
962 
963 	ena_dump_stats_ex(adapter, buf);
964 }
965 
966 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter)
967 {
968 	ena_dump_stats_ex(adapter, NULL);
969 }
970