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 #include "ena_xdp.h"
11 
12 struct ena_stats {
13 	char name[ETH_GSTRING_LEN];
14 	int stat_offset;
15 };
16 
17 #define ENA_STAT_ENA_COM_ENTRY(stat) { \
18 	.name = #stat, \
19 	.stat_offset = offsetof(struct ena_com_stats_admin, stat) / sizeof(u64) \
20 }
21 
22 #define ENA_STAT_ENTRY(stat, stat_type) { \
23 	.name = #stat, \
24 	.stat_offset = offsetof(struct ena_stats_##stat_type, stat) / sizeof(u64) \
25 }
26 
27 #define ENA_STAT_HW_ENTRY(stat, stat_type) { \
28 	.name = #stat, \
29 	.stat_offset = offsetof(struct ena_admin_##stat_type, stat) / sizeof(u64) \
30 }
31 
32 #define ENA_STAT_RX_ENTRY(stat) \
33 	ENA_STAT_ENTRY(stat, rx)
34 
35 #define ENA_STAT_TX_ENTRY(stat) \
36 	ENA_STAT_ENTRY(stat, tx)
37 
38 #define ENA_STAT_GLOBAL_ENTRY(stat) \
39 	ENA_STAT_ENTRY(stat, dev)
40 
41 #define ENA_STAT_ENI_ENTRY(stat) \
42 	ENA_STAT_HW_ENTRY(stat, eni_stats)
43 
44 static const struct ena_stats ena_stats_global_strings[] = {
45 	ENA_STAT_GLOBAL_ENTRY(tx_timeout),
46 	ENA_STAT_GLOBAL_ENTRY(suspend),
47 	ENA_STAT_GLOBAL_ENTRY(resume),
48 	ENA_STAT_GLOBAL_ENTRY(wd_expired),
49 	ENA_STAT_GLOBAL_ENTRY(interface_up),
50 	ENA_STAT_GLOBAL_ENTRY(interface_down),
51 	ENA_STAT_GLOBAL_ENTRY(admin_q_pause),
52 };
53 
54 static const struct ena_stats ena_stats_eni_strings[] = {
55 	ENA_STAT_ENI_ENTRY(bw_in_allowance_exceeded),
56 	ENA_STAT_ENI_ENTRY(bw_out_allowance_exceeded),
57 	ENA_STAT_ENI_ENTRY(pps_allowance_exceeded),
58 	ENA_STAT_ENI_ENTRY(conntrack_allowance_exceeded),
59 	ENA_STAT_ENI_ENTRY(linklocal_allowance_exceeded),
60 };
61 
62 static const struct ena_stats ena_stats_tx_strings[] = {
63 	ENA_STAT_TX_ENTRY(cnt),
64 	ENA_STAT_TX_ENTRY(bytes),
65 	ENA_STAT_TX_ENTRY(queue_stop),
66 	ENA_STAT_TX_ENTRY(queue_wakeup),
67 	ENA_STAT_TX_ENTRY(dma_mapping_err),
68 	ENA_STAT_TX_ENTRY(linearize),
69 	ENA_STAT_TX_ENTRY(linearize_failed),
70 	ENA_STAT_TX_ENTRY(napi_comp),
71 	ENA_STAT_TX_ENTRY(tx_poll),
72 	ENA_STAT_TX_ENTRY(doorbells),
73 	ENA_STAT_TX_ENTRY(prepare_ctx_err),
74 	ENA_STAT_TX_ENTRY(bad_req_id),
75 	ENA_STAT_TX_ENTRY(llq_buffer_copy),
76 	ENA_STAT_TX_ENTRY(missed_tx),
77 	ENA_STAT_TX_ENTRY(unmask_interrupt),
78 };
79 
80 static const struct ena_stats ena_stats_rx_strings[] = {
81 	ENA_STAT_RX_ENTRY(cnt),
82 	ENA_STAT_RX_ENTRY(bytes),
83 	ENA_STAT_RX_ENTRY(rx_copybreak_pkt),
84 	ENA_STAT_RX_ENTRY(csum_good),
85 	ENA_STAT_RX_ENTRY(refil_partial),
86 	ENA_STAT_RX_ENTRY(csum_bad),
87 	ENA_STAT_RX_ENTRY(page_alloc_fail),
88 	ENA_STAT_RX_ENTRY(skb_alloc_fail),
89 	ENA_STAT_RX_ENTRY(dma_mapping_err),
90 	ENA_STAT_RX_ENTRY(bad_desc_num),
91 	ENA_STAT_RX_ENTRY(bad_req_id),
92 	ENA_STAT_RX_ENTRY(empty_rx_ring),
93 	ENA_STAT_RX_ENTRY(csum_unchecked),
94 	ENA_STAT_RX_ENTRY(xdp_aborted),
95 	ENA_STAT_RX_ENTRY(xdp_drop),
96 	ENA_STAT_RX_ENTRY(xdp_pass),
97 	ENA_STAT_RX_ENTRY(xdp_tx),
98 	ENA_STAT_RX_ENTRY(xdp_invalid),
99 	ENA_STAT_RX_ENTRY(xdp_redirect),
100 };
101 
102 static const struct ena_stats ena_stats_ena_com_strings[] = {
103 	ENA_STAT_ENA_COM_ENTRY(aborted_cmd),
104 	ENA_STAT_ENA_COM_ENTRY(submitted_cmd),
105 	ENA_STAT_ENA_COM_ENTRY(completed_cmd),
106 	ENA_STAT_ENA_COM_ENTRY(out_of_space),
107 	ENA_STAT_ENA_COM_ENTRY(no_completion),
108 };
109 
110 #define ENA_STATS_ARRAY_GLOBAL		ARRAY_SIZE(ena_stats_global_strings)
111 #define ENA_STATS_ARRAY_TX		ARRAY_SIZE(ena_stats_tx_strings)
112 #define ENA_STATS_ARRAY_RX		ARRAY_SIZE(ena_stats_rx_strings)
113 #define ENA_STATS_ARRAY_ENA_COM		ARRAY_SIZE(ena_stats_ena_com_strings)
114 #define ENA_STATS_ARRAY_ENI(adapter)	ARRAY_SIZE(ena_stats_eni_strings)
115 
ena_safe_update_stat(u64 * src,u64 * dst,struct u64_stats_sync * syncp)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(syncp);
123 		*(dst) = *src;
124 	} while (u64_stats_fetch_retry(syncp, start));
125 }
126 
ena_queue_stats(struct ena_adapter * adapter,u64 ** data)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 
ena_dev_admin_queue_stats(struct ena_adapter * adapter,u64 ** data)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 
ena_get_stats(struct ena_adapter * adapter,u64 * data,bool eni_stats_needed)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 
ena_get_ethtool_stats(struct net_device * netdev,struct ethtool_stats * stats,u64 * data)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 	struct ena_com_dev *dev = adapter->ena_dev;
217 
218 	ena_get_stats(adapter, data, ena_com_get_cap(dev, ENA_ADMIN_ENI_STATS));
219 }
220 
ena_get_sw_stats_count(struct ena_adapter * adapter)221 static int ena_get_sw_stats_count(struct ena_adapter *adapter)
222 {
223 	return adapter->num_io_queues * (ENA_STATS_ARRAY_TX + ENA_STATS_ARRAY_RX)
224 		+ adapter->xdp_num_queues * ENA_STATS_ARRAY_TX
225 		+ ENA_STATS_ARRAY_GLOBAL + ENA_STATS_ARRAY_ENA_COM;
226 }
227 
ena_get_hw_stats_count(struct ena_adapter * adapter)228 static int ena_get_hw_stats_count(struct ena_adapter *adapter)
229 {
230 	bool supported = ena_com_get_cap(adapter->ena_dev, ENA_ADMIN_ENI_STATS);
231 
232 	return ENA_STATS_ARRAY_ENI(adapter) * supported;
233 }
234 
ena_get_sset_count(struct net_device * netdev,int sset)235 int ena_get_sset_count(struct net_device *netdev, int sset)
236 {
237 	struct ena_adapter *adapter = netdev_priv(netdev);
238 
239 	switch (sset) {
240 	case ETH_SS_STATS:
241 		return ena_get_sw_stats_count(adapter) +
242 		       ena_get_hw_stats_count(adapter);
243 	}
244 
245 	return -EOPNOTSUPP;
246 }
247 
ena_queue_strings(struct ena_adapter * adapter,u8 ** data)248 static void ena_queue_strings(struct ena_adapter *adapter, u8 **data)
249 {
250 	const struct ena_stats *ena_stats;
251 	bool is_xdp;
252 	int i, j;
253 
254 	for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) {
255 		is_xdp = ENA_IS_XDP_INDEX(adapter, i);
256 		/* Tx stats */
257 		for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
258 			ena_stats = &ena_stats_tx_strings[j];
259 
260 			ethtool_sprintf(data,
261 					"queue_%u_%s_%s", i,
262 					is_xdp ? "xdp_tx" : "tx",
263 					ena_stats->name);
264 		}
265 
266 		if (!is_xdp) {
267 			/* RX stats, in XDP there isn't a RX queue
268 			 * counterpart
269 			 */
270 			for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
271 				ena_stats = &ena_stats_rx_strings[j];
272 
273 				ethtool_sprintf(data,
274 						"queue_%u_rx_%s", i,
275 						ena_stats->name);
276 			}
277 		}
278 	}
279 }
280 
ena_com_dev_strings(u8 ** data)281 static void ena_com_dev_strings(u8 **data)
282 {
283 	const struct ena_stats *ena_stats;
284 	int i;
285 
286 	for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
287 		ena_stats = &ena_stats_ena_com_strings[i];
288 
289 		ethtool_sprintf(data,
290 				"ena_admin_q_%s", ena_stats->name);
291 	}
292 }
293 
ena_get_strings(struct ena_adapter * adapter,u8 * data,bool eni_stats_needed)294 static void ena_get_strings(struct ena_adapter *adapter,
295 			    u8 *data,
296 			    bool eni_stats_needed)
297 {
298 	const struct ena_stats *ena_stats;
299 	int i;
300 
301 	for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
302 		ena_stats = &ena_stats_global_strings[i];
303 		ethtool_sprintf(&data, ena_stats->name);
304 	}
305 
306 	if (eni_stats_needed) {
307 		for (i = 0; i < ENA_STATS_ARRAY_ENI(adapter); i++) {
308 			ena_stats = &ena_stats_eni_strings[i];
309 			ethtool_sprintf(&data, ena_stats->name);
310 		}
311 	}
312 
313 	ena_queue_strings(adapter, &data);
314 	ena_com_dev_strings(&data);
315 }
316 
ena_get_ethtool_strings(struct net_device * netdev,u32 sset,u8 * data)317 static void ena_get_ethtool_strings(struct net_device *netdev,
318 				    u32 sset,
319 				    u8 *data)
320 {
321 	struct ena_adapter *adapter = netdev_priv(netdev);
322 	struct ena_com_dev *dev = adapter->ena_dev;
323 
324 	switch (sset) {
325 	case ETH_SS_STATS:
326 		ena_get_strings(adapter, data, ena_com_get_cap(dev, ENA_ADMIN_ENI_STATS));
327 		break;
328 	}
329 }
330 
ena_get_link_ksettings(struct net_device * netdev,struct ethtool_link_ksettings * link_ksettings)331 static int ena_get_link_ksettings(struct net_device *netdev,
332 				  struct ethtool_link_ksettings *link_ksettings)
333 {
334 	struct ena_adapter *adapter = netdev_priv(netdev);
335 	struct ena_com_dev *ena_dev = adapter->ena_dev;
336 	struct ena_admin_get_feature_link_desc *link;
337 	struct ena_admin_get_feat_resp feat_resp;
338 	int rc;
339 
340 	rc = ena_com_get_link_params(ena_dev, &feat_resp);
341 	if (rc)
342 		return rc;
343 
344 	link = &feat_resp.u.link;
345 	link_ksettings->base.speed = link->speed;
346 
347 	if (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) {
348 		ethtool_link_ksettings_add_link_mode(link_ksettings,
349 						     supported, Autoneg);
350 		ethtool_link_ksettings_add_link_mode(link_ksettings,
351 						     supported, Autoneg);
352 	}
353 
354 	link_ksettings->base.autoneg =
355 		(link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) ?
356 		AUTONEG_ENABLE : AUTONEG_DISABLE;
357 
358 	link_ksettings->base.duplex = DUPLEX_FULL;
359 
360 	return 0;
361 }
362 
ena_get_coalesce(struct net_device * net_dev,struct ethtool_coalesce * coalesce,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)363 static int ena_get_coalesce(struct net_device *net_dev,
364 			    struct ethtool_coalesce *coalesce,
365 			    struct kernel_ethtool_coalesce *kernel_coal,
366 			    struct netlink_ext_ack *extack)
367 {
368 	struct ena_adapter *adapter = netdev_priv(net_dev);
369 	struct ena_com_dev *ena_dev = adapter->ena_dev;
370 
371 	if (!ena_com_interrupt_moderation_supported(ena_dev))
372 		return -EOPNOTSUPP;
373 
374 	coalesce->tx_coalesce_usecs =
375 		ena_com_get_nonadaptive_moderation_interval_tx(ena_dev) *
376 			ena_dev->intr_delay_resolution;
377 
378 	coalesce->rx_coalesce_usecs =
379 		ena_com_get_nonadaptive_moderation_interval_rx(ena_dev)
380 		* ena_dev->intr_delay_resolution;
381 
382 	coalesce->use_adaptive_rx_coalesce =
383 		ena_com_get_adaptive_moderation_enabled(ena_dev);
384 
385 	return 0;
386 }
387 
ena_update_tx_rings_nonadaptive_intr_moderation(struct ena_adapter * adapter)388 static void ena_update_tx_rings_nonadaptive_intr_moderation(struct ena_adapter *adapter)
389 {
390 	unsigned int val;
391 	int i;
392 
393 	val = ena_com_get_nonadaptive_moderation_interval_tx(adapter->ena_dev);
394 
395 	for (i = 0; i < adapter->num_io_queues; i++)
396 		adapter->tx_ring[i].smoothed_interval = val;
397 }
398 
ena_update_rx_rings_nonadaptive_intr_moderation(struct ena_adapter * adapter)399 static void ena_update_rx_rings_nonadaptive_intr_moderation(struct ena_adapter *adapter)
400 {
401 	unsigned int val;
402 	int i;
403 
404 	val = ena_com_get_nonadaptive_moderation_interval_rx(adapter->ena_dev);
405 
406 	for (i = 0; i < adapter->num_io_queues; i++)
407 		adapter->rx_ring[i].smoothed_interval = val;
408 }
409 
ena_set_coalesce(struct net_device * net_dev,struct ethtool_coalesce * coalesce,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)410 static int ena_set_coalesce(struct net_device *net_dev,
411 			    struct ethtool_coalesce *coalesce,
412 			    struct kernel_ethtool_coalesce *kernel_coal,
413 			    struct netlink_ext_ack *extack)
414 {
415 	struct ena_adapter *adapter = netdev_priv(net_dev);
416 	struct ena_com_dev *ena_dev = adapter->ena_dev;
417 	int rc;
418 
419 	if (!ena_com_interrupt_moderation_supported(ena_dev))
420 		return -EOPNOTSUPP;
421 
422 	rc = ena_com_update_nonadaptive_moderation_interval_tx(ena_dev,
423 							       coalesce->tx_coalesce_usecs);
424 	if (rc)
425 		return rc;
426 
427 	ena_update_tx_rings_nonadaptive_intr_moderation(adapter);
428 
429 	rc = ena_com_update_nonadaptive_moderation_interval_rx(ena_dev,
430 							       coalesce->rx_coalesce_usecs);
431 	if (rc)
432 		return rc;
433 
434 	ena_update_rx_rings_nonadaptive_intr_moderation(adapter);
435 
436 	if (coalesce->use_adaptive_rx_coalesce &&
437 	    !ena_com_get_adaptive_moderation_enabled(ena_dev))
438 		ena_com_enable_adaptive_moderation(ena_dev);
439 
440 	if (!coalesce->use_adaptive_rx_coalesce &&
441 	    ena_com_get_adaptive_moderation_enabled(ena_dev))
442 		ena_com_disable_adaptive_moderation(ena_dev);
443 
444 	return 0;
445 }
446 
ena_get_msglevel(struct net_device * netdev)447 static u32 ena_get_msglevel(struct net_device *netdev)
448 {
449 	struct ena_adapter *adapter = netdev_priv(netdev);
450 
451 	return adapter->msg_enable;
452 }
453 
ena_set_msglevel(struct net_device * netdev,u32 value)454 static void ena_set_msglevel(struct net_device *netdev, u32 value)
455 {
456 	struct ena_adapter *adapter = netdev_priv(netdev);
457 
458 	adapter->msg_enable = value;
459 }
460 
ena_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)461 static void ena_get_drvinfo(struct net_device *dev,
462 			    struct ethtool_drvinfo *info)
463 {
464 	struct ena_adapter *adapter = netdev_priv(dev);
465 
466 	strscpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
467 	strscpy(info->bus_info, pci_name(adapter->pdev),
468 		sizeof(info->bus_info));
469 }
470 
ena_get_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)471 static void ena_get_ringparam(struct net_device *netdev,
472 			      struct ethtool_ringparam *ring,
473 			      struct kernel_ethtool_ringparam *kernel_ring,
474 			      struct netlink_ext_ack *extack)
475 {
476 	struct ena_adapter *adapter = netdev_priv(netdev);
477 
478 	ring->tx_max_pending = adapter->max_tx_ring_size;
479 	ring->rx_max_pending = adapter->max_rx_ring_size;
480 	if (adapter->ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
481 		bool large_llq_supported = adapter->large_llq_header_supported;
482 
483 		kernel_ring->tx_push = true;
484 		kernel_ring->tx_push_buf_len = adapter->ena_dev->tx_max_header_size;
485 		if (large_llq_supported)
486 			kernel_ring->tx_push_buf_max_len = ENA_LLQ_LARGE_HEADER;
487 		else
488 			kernel_ring->tx_push_buf_max_len = ENA_LLQ_HEADER;
489 	} else {
490 		kernel_ring->tx_push = false;
491 		kernel_ring->tx_push_buf_max_len = 0;
492 		kernel_ring->tx_push_buf_len = 0;
493 	}
494 
495 	ring->tx_pending = adapter->tx_ring[0].ring_size;
496 	ring->rx_pending = adapter->rx_ring[0].ring_size;
497 }
498 
ena_set_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)499 static int ena_set_ringparam(struct net_device *netdev,
500 			     struct ethtool_ringparam *ring,
501 			     struct kernel_ethtool_ringparam *kernel_ring,
502 			     struct netlink_ext_ack *extack)
503 {
504 	struct ena_adapter *adapter = netdev_priv(netdev);
505 	u32 new_tx_size, new_rx_size, new_tx_push_buf_len;
506 	bool changed = false;
507 
508 	new_tx_size = ring->tx_pending < ENA_MIN_RING_SIZE ?
509 			ENA_MIN_RING_SIZE : ring->tx_pending;
510 	new_tx_size = rounddown_pow_of_two(new_tx_size);
511 
512 	new_rx_size = ring->rx_pending < ENA_MIN_RING_SIZE ?
513 			ENA_MIN_RING_SIZE : ring->rx_pending;
514 	new_rx_size = rounddown_pow_of_two(new_rx_size);
515 
516 	changed |= new_tx_size != adapter->requested_tx_ring_size ||
517 		   new_rx_size != adapter->requested_rx_ring_size;
518 
519 	/* This value is ignored if LLQ is not supported */
520 	new_tx_push_buf_len = adapter->ena_dev->tx_max_header_size;
521 
522 	if ((adapter->ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) !=
523 	    kernel_ring->tx_push) {
524 		NL_SET_ERR_MSG_MOD(extack, "Push mode state cannot be modified");
525 		return -EINVAL;
526 	}
527 
528 	/* Validate that the push buffer is supported on the underlying device */
529 	if (kernel_ring->tx_push_buf_len) {
530 		enum ena_admin_placement_policy_type placement;
531 
532 		new_tx_push_buf_len = kernel_ring->tx_push_buf_len;
533 
534 		placement = adapter->ena_dev->tx_mem_queue_type;
535 		if (placement == ENA_ADMIN_PLACEMENT_POLICY_HOST)
536 			return -EOPNOTSUPP;
537 
538 		if (new_tx_push_buf_len != ENA_LLQ_HEADER &&
539 		    new_tx_push_buf_len != ENA_LLQ_LARGE_HEADER) {
540 			bool large_llq_sup = adapter->large_llq_header_supported;
541 			char large_llq_size_str[40];
542 
543 			snprintf(large_llq_size_str, 40, ", %lu", ENA_LLQ_LARGE_HEADER);
544 
545 			NL_SET_ERR_MSG_FMT_MOD(extack,
546 					       "Supported tx push buff values: [%lu%s]",
547 					       ENA_LLQ_HEADER,
548 					       large_llq_sup ? large_llq_size_str : "");
549 
550 			return -EINVAL;
551 		}
552 
553 		changed |= new_tx_push_buf_len != adapter->ena_dev->tx_max_header_size;
554 	}
555 
556 	if (!changed)
557 		return 0;
558 
559 	return ena_update_queue_params(adapter, new_tx_size, new_rx_size,
560 				       new_tx_push_buf_len);
561 }
562 
ena_flow_hash_to_flow_type(u16 hash_fields)563 static u32 ena_flow_hash_to_flow_type(u16 hash_fields)
564 {
565 	u32 data = 0;
566 
567 	if (hash_fields & ENA_ADMIN_RSS_L2_DA)
568 		data |= RXH_L2DA;
569 
570 	if (hash_fields & ENA_ADMIN_RSS_L3_DA)
571 		data |= RXH_IP_DST;
572 
573 	if (hash_fields & ENA_ADMIN_RSS_L3_SA)
574 		data |= RXH_IP_SRC;
575 
576 	if (hash_fields & ENA_ADMIN_RSS_L4_DP)
577 		data |= RXH_L4_B_2_3;
578 
579 	if (hash_fields & ENA_ADMIN_RSS_L4_SP)
580 		data |= RXH_L4_B_0_1;
581 
582 	return data;
583 }
584 
ena_flow_data_to_flow_hash(u32 hash_fields)585 static u16 ena_flow_data_to_flow_hash(u32 hash_fields)
586 {
587 	u16 data = 0;
588 
589 	if (hash_fields & RXH_L2DA)
590 		data |= ENA_ADMIN_RSS_L2_DA;
591 
592 	if (hash_fields & RXH_IP_DST)
593 		data |= ENA_ADMIN_RSS_L3_DA;
594 
595 	if (hash_fields & RXH_IP_SRC)
596 		data |= ENA_ADMIN_RSS_L3_SA;
597 
598 	if (hash_fields & RXH_L4_B_2_3)
599 		data |= ENA_ADMIN_RSS_L4_DP;
600 
601 	if (hash_fields & RXH_L4_B_0_1)
602 		data |= ENA_ADMIN_RSS_L4_SP;
603 
604 	return data;
605 }
606 
ena_get_rss_hash(struct ena_com_dev * ena_dev,struct ethtool_rxnfc * cmd)607 static int ena_get_rss_hash(struct ena_com_dev *ena_dev,
608 			    struct ethtool_rxnfc *cmd)
609 {
610 	enum ena_admin_flow_hash_proto proto;
611 	u16 hash_fields;
612 	int rc;
613 
614 	cmd->data = 0;
615 
616 	switch (cmd->flow_type) {
617 	case TCP_V4_FLOW:
618 		proto = ENA_ADMIN_RSS_TCP4;
619 		break;
620 	case UDP_V4_FLOW:
621 		proto = ENA_ADMIN_RSS_UDP4;
622 		break;
623 	case TCP_V6_FLOW:
624 		proto = ENA_ADMIN_RSS_TCP6;
625 		break;
626 	case UDP_V6_FLOW:
627 		proto = ENA_ADMIN_RSS_UDP6;
628 		break;
629 	case IPV4_FLOW:
630 		proto = ENA_ADMIN_RSS_IP4;
631 		break;
632 	case IPV6_FLOW:
633 		proto = ENA_ADMIN_RSS_IP6;
634 		break;
635 	case ETHER_FLOW:
636 		proto = ENA_ADMIN_RSS_NOT_IP;
637 		break;
638 	case AH_V4_FLOW:
639 	case ESP_V4_FLOW:
640 	case AH_V6_FLOW:
641 	case ESP_V6_FLOW:
642 	case SCTP_V4_FLOW:
643 	case AH_ESP_V4_FLOW:
644 		return -EOPNOTSUPP;
645 	default:
646 		return -EINVAL;
647 	}
648 
649 	rc = ena_com_get_hash_ctrl(ena_dev, proto, &hash_fields);
650 	if (rc)
651 		return rc;
652 
653 	cmd->data = ena_flow_hash_to_flow_type(hash_fields);
654 
655 	return 0;
656 }
657 
ena_set_rss_hash(struct ena_com_dev * ena_dev,struct ethtool_rxnfc * cmd)658 static int ena_set_rss_hash(struct ena_com_dev *ena_dev,
659 			    struct ethtool_rxnfc *cmd)
660 {
661 	enum ena_admin_flow_hash_proto proto;
662 	u16 hash_fields;
663 
664 	switch (cmd->flow_type) {
665 	case TCP_V4_FLOW:
666 		proto = ENA_ADMIN_RSS_TCP4;
667 		break;
668 	case UDP_V4_FLOW:
669 		proto = ENA_ADMIN_RSS_UDP4;
670 		break;
671 	case TCP_V6_FLOW:
672 		proto = ENA_ADMIN_RSS_TCP6;
673 		break;
674 	case UDP_V6_FLOW:
675 		proto = ENA_ADMIN_RSS_UDP6;
676 		break;
677 	case IPV4_FLOW:
678 		proto = ENA_ADMIN_RSS_IP4;
679 		break;
680 	case IPV6_FLOW:
681 		proto = ENA_ADMIN_RSS_IP6;
682 		break;
683 	case ETHER_FLOW:
684 		proto = ENA_ADMIN_RSS_NOT_IP;
685 		break;
686 	case AH_V4_FLOW:
687 	case ESP_V4_FLOW:
688 	case AH_V6_FLOW:
689 	case ESP_V6_FLOW:
690 	case SCTP_V4_FLOW:
691 	case AH_ESP_V4_FLOW:
692 		return -EOPNOTSUPP;
693 	default:
694 		return -EINVAL;
695 	}
696 
697 	hash_fields = ena_flow_data_to_flow_hash(cmd->data);
698 
699 	return ena_com_fill_hash_ctrl(ena_dev, proto, hash_fields);
700 }
701 
ena_set_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * info)702 static int ena_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info)
703 {
704 	struct ena_adapter *adapter = netdev_priv(netdev);
705 	int rc = 0;
706 
707 	switch (info->cmd) {
708 	case ETHTOOL_SRXFH:
709 		rc = ena_set_rss_hash(adapter->ena_dev, info);
710 		break;
711 	case ETHTOOL_SRXCLSRLDEL:
712 	case ETHTOOL_SRXCLSRLINS:
713 	default:
714 		netif_err(adapter, drv, netdev,
715 			  "Command parameter %d is not supported\n", info->cmd);
716 		rc = -EOPNOTSUPP;
717 	}
718 
719 	return rc;
720 }
721 
ena_get_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * info,u32 * rules)722 static int ena_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
723 			 u32 *rules)
724 {
725 	struct ena_adapter *adapter = netdev_priv(netdev);
726 	int rc = 0;
727 
728 	switch (info->cmd) {
729 	case ETHTOOL_GRXRINGS:
730 		info->data = adapter->num_io_queues;
731 		rc = 0;
732 		break;
733 	case ETHTOOL_GRXFH:
734 		rc = ena_get_rss_hash(adapter->ena_dev, info);
735 		break;
736 	case ETHTOOL_GRXCLSRLCNT:
737 	case ETHTOOL_GRXCLSRULE:
738 	case ETHTOOL_GRXCLSRLALL:
739 	default:
740 		netif_err(adapter, drv, netdev,
741 			  "Command parameter %d is not supported\n", info->cmd);
742 		rc = -EOPNOTSUPP;
743 	}
744 
745 	return rc;
746 }
747 
ena_get_rxfh_indir_size(struct net_device * netdev)748 static u32 ena_get_rxfh_indir_size(struct net_device *netdev)
749 {
750 	return ENA_RX_RSS_TABLE_SIZE;
751 }
752 
ena_get_rxfh_key_size(struct net_device * netdev)753 static u32 ena_get_rxfh_key_size(struct net_device *netdev)
754 {
755 	return ENA_HASH_KEY_SIZE;
756 }
757 
ena_indirection_table_set(struct ena_adapter * adapter,const u32 * indir)758 static int ena_indirection_table_set(struct ena_adapter *adapter,
759 				     const u32 *indir)
760 {
761 	struct ena_com_dev *ena_dev = adapter->ena_dev;
762 	int i, rc;
763 
764 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
765 		rc = ena_com_indirect_table_fill_entry(ena_dev,
766 						       i,
767 						       ENA_IO_RXQ_IDX(indir[i]));
768 		if (unlikely(rc)) {
769 			netif_err(adapter, drv, adapter->netdev,
770 				  "Cannot fill indirect table (index is too large)\n");
771 			return rc;
772 		}
773 	}
774 
775 	rc = ena_com_indirect_table_set(ena_dev);
776 	if (rc) {
777 		netif_err(adapter, drv, adapter->netdev,
778 			  "Cannot set indirect table\n");
779 		return rc == -EPERM ? -EOPNOTSUPP : rc;
780 	}
781 	return rc;
782 }
783 
ena_indirection_table_get(struct ena_adapter * adapter,u32 * indir)784 static int ena_indirection_table_get(struct ena_adapter *adapter, u32 *indir)
785 {
786 	struct ena_com_dev *ena_dev = adapter->ena_dev;
787 	int i, rc;
788 
789 	if (!indir)
790 		return 0;
791 
792 	rc = ena_com_indirect_table_get(ena_dev, indir);
793 	if (rc)
794 		return rc;
795 
796 	/* Our internal representation of the indices is: even indices
797 	 * for Tx and uneven indices for Rx. We need to convert the Rx
798 	 * indices to be consecutive
799 	 */
800 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++)
801 		indir[i] = ENA_IO_RXQ_IDX_TO_COMBINED_IDX(indir[i]);
802 
803 	return rc;
804 }
805 
ena_get_rxfh(struct net_device * netdev,u32 * indir,u8 * key,u8 * hfunc)806 static int ena_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
807 			u8 *hfunc)
808 {
809 	struct ena_adapter *adapter = netdev_priv(netdev);
810 	enum ena_admin_hash_functions ena_func;
811 	u8 func;
812 	int rc;
813 
814 	rc = ena_indirection_table_get(adapter, indir);
815 	if (rc)
816 		return rc;
817 
818 	/* We call this function in order to check if the device
819 	 * supports getting/setting the hash function.
820 	 */
821 	rc = ena_com_get_hash_function(adapter->ena_dev, &ena_func);
822 	if (rc) {
823 		if (rc == -EOPNOTSUPP)
824 			rc = 0;
825 
826 		return rc;
827 	}
828 
829 	rc = ena_com_get_hash_key(adapter->ena_dev, key);
830 	if (rc)
831 		return rc;
832 
833 	switch (ena_func) {
834 	case ENA_ADMIN_TOEPLITZ:
835 		func = ETH_RSS_HASH_TOP;
836 		break;
837 	case ENA_ADMIN_CRC32:
838 		func = ETH_RSS_HASH_CRC32;
839 		break;
840 	default:
841 		netif_err(adapter, drv, netdev,
842 			  "Command parameter is not supported\n");
843 		return -EOPNOTSUPP;
844 	}
845 
846 	if (hfunc)
847 		*hfunc = func;
848 
849 	return 0;
850 }
851 
ena_set_rxfh(struct net_device * netdev,const u32 * indir,const u8 * key,const u8 hfunc)852 static int ena_set_rxfh(struct net_device *netdev, const u32 *indir,
853 			const u8 *key, const u8 hfunc)
854 {
855 	struct ena_adapter *adapter = netdev_priv(netdev);
856 	struct ena_com_dev *ena_dev = adapter->ena_dev;
857 	enum ena_admin_hash_functions func = 0;
858 	int rc;
859 
860 	if (indir) {
861 		rc = ena_indirection_table_set(adapter, indir);
862 		if (rc)
863 			return rc;
864 	}
865 
866 	switch (hfunc) {
867 	case ETH_RSS_HASH_NO_CHANGE:
868 		func = ena_com_get_current_hash_function(ena_dev);
869 		break;
870 	case ETH_RSS_HASH_TOP:
871 		func = ENA_ADMIN_TOEPLITZ;
872 		break;
873 	case ETH_RSS_HASH_CRC32:
874 		func = ENA_ADMIN_CRC32;
875 		break;
876 	default:
877 		netif_err(adapter, drv, netdev, "Unsupported hfunc %d\n",
878 			  hfunc);
879 		return -EOPNOTSUPP;
880 	}
881 
882 	if (key || func) {
883 		rc = ena_com_fill_hash_function(ena_dev, func, key,
884 						ENA_HASH_KEY_SIZE,
885 						0xFFFFFFFF);
886 		if (unlikely(rc)) {
887 			netif_err(adapter, drv, netdev, "Cannot fill key\n");
888 			return rc == -EPERM ? -EOPNOTSUPP : rc;
889 		}
890 	}
891 
892 	return 0;
893 }
894 
ena_get_channels(struct net_device * netdev,struct ethtool_channels * channels)895 static void ena_get_channels(struct net_device *netdev,
896 			     struct ethtool_channels *channels)
897 {
898 	struct ena_adapter *adapter = netdev_priv(netdev);
899 
900 	channels->max_combined = adapter->max_num_io_queues;
901 	channels->combined_count = adapter->num_io_queues;
902 }
903 
ena_set_channels(struct net_device * netdev,struct ethtool_channels * channels)904 static int ena_set_channels(struct net_device *netdev,
905 			    struct ethtool_channels *channels)
906 {
907 	struct ena_adapter *adapter = netdev_priv(netdev);
908 	u32 count = channels->combined_count;
909 	/* The check for max value is already done in ethtool */
910 	if (count < ENA_MIN_NUM_IO_QUEUES)
911 		return -EINVAL;
912 
913 	if (!ena_xdp_legal_queue_count(adapter, count)) {
914 		if (ena_xdp_present(adapter))
915 			return -EINVAL;
916 
917 		xdp_clear_features_flag(netdev);
918 	} else {
919 		xdp_set_features_flag(netdev,
920 				      NETDEV_XDP_ACT_BASIC |
921 				      NETDEV_XDP_ACT_REDIRECT);
922 	}
923 
924 	return ena_update_queue_count(adapter, count);
925 }
926 
ena_get_tunable(struct net_device * netdev,const struct ethtool_tunable * tuna,void * data)927 static int ena_get_tunable(struct net_device *netdev,
928 			   const struct ethtool_tunable *tuna, void *data)
929 {
930 	struct ena_adapter *adapter = netdev_priv(netdev);
931 	int ret = 0;
932 
933 	switch (tuna->id) {
934 	case ETHTOOL_RX_COPYBREAK:
935 		*(u32 *)data = adapter->rx_copybreak;
936 		break;
937 	default:
938 		ret = -EINVAL;
939 		break;
940 	}
941 
942 	return ret;
943 }
944 
ena_set_tunable(struct net_device * netdev,const struct ethtool_tunable * tuna,const void * data)945 static int ena_set_tunable(struct net_device *netdev,
946 			   const struct ethtool_tunable *tuna,
947 			   const void *data)
948 {
949 	struct ena_adapter *adapter = netdev_priv(netdev);
950 	int ret = 0;
951 	u32 len;
952 
953 	switch (tuna->id) {
954 	case ETHTOOL_RX_COPYBREAK:
955 		len = *(u32 *)data;
956 		ret = ena_set_rx_copybreak(adapter, len);
957 		break;
958 	default:
959 		ret = -EINVAL;
960 		break;
961 	}
962 
963 	return ret;
964 }
965 
966 static const struct ethtool_ops ena_ethtool_ops = {
967 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
968 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
969 	.supported_ring_params	= ETHTOOL_RING_USE_TX_PUSH_BUF_LEN |
970 				  ETHTOOL_RING_USE_TX_PUSH,
971 	.get_link_ksettings	= ena_get_link_ksettings,
972 	.get_drvinfo		= ena_get_drvinfo,
973 	.get_msglevel		= ena_get_msglevel,
974 	.set_msglevel		= ena_set_msglevel,
975 	.get_link		= ethtool_op_get_link,
976 	.get_coalesce		= ena_get_coalesce,
977 	.set_coalesce		= ena_set_coalesce,
978 	.get_ringparam		= ena_get_ringparam,
979 	.set_ringparam		= ena_set_ringparam,
980 	.get_sset_count         = ena_get_sset_count,
981 	.get_strings		= ena_get_ethtool_strings,
982 	.get_ethtool_stats      = ena_get_ethtool_stats,
983 	.get_rxnfc		= ena_get_rxnfc,
984 	.set_rxnfc		= ena_set_rxnfc,
985 	.get_rxfh_indir_size    = ena_get_rxfh_indir_size,
986 	.get_rxfh_key_size	= ena_get_rxfh_key_size,
987 	.get_rxfh		= ena_get_rxfh,
988 	.set_rxfh		= ena_set_rxfh,
989 	.get_channels		= ena_get_channels,
990 	.set_channels		= ena_set_channels,
991 	.get_tunable		= ena_get_tunable,
992 	.set_tunable		= ena_set_tunable,
993 	.get_ts_info            = ethtool_op_get_ts_info,
994 };
995 
ena_set_ethtool_ops(struct net_device * netdev)996 void ena_set_ethtool_ops(struct net_device *netdev)
997 {
998 	netdev->ethtool_ops = &ena_ethtool_ops;
999 }
1000 
ena_dump_stats_ex(struct ena_adapter * adapter,u8 * buf)1001 static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
1002 {
1003 	struct net_device *netdev = adapter->netdev;
1004 	u8 *strings_buf;
1005 	u64 *data_buf;
1006 	int strings_num;
1007 	int i, rc;
1008 
1009 	strings_num = ena_get_sw_stats_count(adapter);
1010 	if (strings_num <= 0) {
1011 		netif_err(adapter, drv, netdev, "Can't get stats num\n");
1012 		return;
1013 	}
1014 
1015 	strings_buf = devm_kcalloc(&adapter->pdev->dev,
1016 				   ETH_GSTRING_LEN, strings_num,
1017 				   GFP_ATOMIC);
1018 	if (!strings_buf) {
1019 		netif_err(adapter, drv, netdev,
1020 			  "Failed to allocate strings_buf\n");
1021 		return;
1022 	}
1023 
1024 	data_buf = devm_kcalloc(&adapter->pdev->dev,
1025 				strings_num, sizeof(u64),
1026 				GFP_ATOMIC);
1027 	if (!data_buf) {
1028 		netif_err(adapter, drv, netdev,
1029 			  "Failed to allocate data buf\n");
1030 		devm_kfree(&adapter->pdev->dev, strings_buf);
1031 		return;
1032 	}
1033 
1034 	ena_get_strings(adapter, strings_buf, false);
1035 	ena_get_stats(adapter, data_buf, false);
1036 
1037 	/* If there is a buffer, dump stats, otherwise print them to dmesg */
1038 	if (buf)
1039 		for (i = 0; i < strings_num; i++) {
1040 			rc = snprintf(buf, ETH_GSTRING_LEN + sizeof(u64),
1041 				      "%s %llu\n",
1042 				      strings_buf + i * ETH_GSTRING_LEN,
1043 				      data_buf[i]);
1044 			buf += rc;
1045 		}
1046 	else
1047 		for (i = 0; i < strings_num; i++)
1048 			netif_err(adapter, drv, netdev, "%s: %llu\n",
1049 				  strings_buf + i * ETH_GSTRING_LEN,
1050 				  data_buf[i]);
1051 
1052 	devm_kfree(&adapter->pdev->dev, strings_buf);
1053 	devm_kfree(&adapter->pdev->dev, data_buf);
1054 }
1055 
ena_dump_stats_to_buf(struct ena_adapter * adapter,u8 * buf)1056 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf)
1057 {
1058 	if (!buf)
1059 		return;
1060 
1061 	ena_dump_stats_ex(adapter, buf);
1062 }
1063 
ena_dump_stats_to_dmesg(struct ena_adapter * adapter)1064 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter)
1065 {
1066 	ena_dump_stats_ex(adapter, NULL);
1067 }
1068