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