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(syncp);
122 		*(dst) = *src;
123 	} while (u64_stats_fetch_retry(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 	if (adapter->ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
480 		bool large_llq_supported = adapter->large_llq_header_supported;
481 
482 		kernel_ring->tx_push = true;
483 		kernel_ring->tx_push_buf_len = adapter->ena_dev->tx_max_header_size;
484 		if (large_llq_supported)
485 			kernel_ring->tx_push_buf_max_len = ENA_LLQ_LARGE_HEADER;
486 		else
487 			kernel_ring->tx_push_buf_max_len = ENA_LLQ_HEADER;
488 	} else {
489 		kernel_ring->tx_push = false;
490 		kernel_ring->tx_push_buf_max_len = 0;
491 		kernel_ring->tx_push_buf_len = 0;
492 	}
493 
494 	ring->tx_pending = adapter->tx_ring[0].ring_size;
495 	ring->rx_pending = adapter->rx_ring[0].ring_size;
496 }
497 
ena_set_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)498 static int ena_set_ringparam(struct net_device *netdev,
499 			     struct ethtool_ringparam *ring,
500 			     struct kernel_ethtool_ringparam *kernel_ring,
501 			     struct netlink_ext_ack *extack)
502 {
503 	struct ena_adapter *adapter = netdev_priv(netdev);
504 	u32 new_tx_size, new_rx_size, new_tx_push_buf_len;
505 	bool changed = false;
506 
507 	new_tx_size = ring->tx_pending < ENA_MIN_RING_SIZE ?
508 			ENA_MIN_RING_SIZE : ring->tx_pending;
509 	new_tx_size = rounddown_pow_of_two(new_tx_size);
510 
511 	new_rx_size = ring->rx_pending < ENA_MIN_RING_SIZE ?
512 			ENA_MIN_RING_SIZE : ring->rx_pending;
513 	new_rx_size = rounddown_pow_of_two(new_rx_size);
514 
515 	changed |= new_tx_size != adapter->requested_tx_ring_size ||
516 		   new_rx_size != adapter->requested_rx_ring_size;
517 
518 	/* This value is ignored if LLQ is not supported */
519 	new_tx_push_buf_len = adapter->ena_dev->tx_max_header_size;
520 
521 	if ((adapter->ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) !=
522 	    kernel_ring->tx_push) {
523 		NL_SET_ERR_MSG_MOD(extack, "Push mode state cannot be modified");
524 		return -EINVAL;
525 	}
526 
527 	/* Validate that the push buffer is supported on the underlying device */
528 	if (kernel_ring->tx_push_buf_len) {
529 		enum ena_admin_placement_policy_type placement;
530 
531 		new_tx_push_buf_len = kernel_ring->tx_push_buf_len;
532 
533 		placement = adapter->ena_dev->tx_mem_queue_type;
534 		if (placement == ENA_ADMIN_PLACEMENT_POLICY_HOST)
535 			return -EOPNOTSUPP;
536 
537 		if (new_tx_push_buf_len != ENA_LLQ_HEADER &&
538 		    new_tx_push_buf_len != ENA_LLQ_LARGE_HEADER) {
539 			bool large_llq_sup = adapter->large_llq_header_supported;
540 			char large_llq_size_str[40];
541 
542 			snprintf(large_llq_size_str, 40, ", %lu", ENA_LLQ_LARGE_HEADER);
543 
544 			NL_SET_ERR_MSG_FMT_MOD(extack,
545 					       "Supported tx push buff values: [%lu%s]",
546 					       ENA_LLQ_HEADER,
547 					       large_llq_sup ? large_llq_size_str : "");
548 
549 			return -EINVAL;
550 		}
551 
552 		changed |= new_tx_push_buf_len != adapter->ena_dev->tx_max_header_size;
553 	}
554 
555 	if (!changed)
556 		return 0;
557 
558 	return ena_update_queue_params(adapter, new_tx_size, new_rx_size,
559 				       new_tx_push_buf_len);
560 }
561 
ena_flow_hash_to_flow_type(u16 hash_fields)562 static u32 ena_flow_hash_to_flow_type(u16 hash_fields)
563 {
564 	u32 data = 0;
565 
566 	if (hash_fields & ENA_ADMIN_RSS_L2_DA)
567 		data |= RXH_L2DA;
568 
569 	if (hash_fields & ENA_ADMIN_RSS_L3_DA)
570 		data |= RXH_IP_DST;
571 
572 	if (hash_fields & ENA_ADMIN_RSS_L3_SA)
573 		data |= RXH_IP_SRC;
574 
575 	if (hash_fields & ENA_ADMIN_RSS_L4_DP)
576 		data |= RXH_L4_B_2_3;
577 
578 	if (hash_fields & ENA_ADMIN_RSS_L4_SP)
579 		data |= RXH_L4_B_0_1;
580 
581 	return data;
582 }
583 
ena_flow_data_to_flow_hash(u32 hash_fields)584 static u16 ena_flow_data_to_flow_hash(u32 hash_fields)
585 {
586 	u16 data = 0;
587 
588 	if (hash_fields & RXH_L2DA)
589 		data |= ENA_ADMIN_RSS_L2_DA;
590 
591 	if (hash_fields & RXH_IP_DST)
592 		data |= ENA_ADMIN_RSS_L3_DA;
593 
594 	if (hash_fields & RXH_IP_SRC)
595 		data |= ENA_ADMIN_RSS_L3_SA;
596 
597 	if (hash_fields & RXH_L4_B_2_3)
598 		data |= ENA_ADMIN_RSS_L4_DP;
599 
600 	if (hash_fields & RXH_L4_B_0_1)
601 		data |= ENA_ADMIN_RSS_L4_SP;
602 
603 	return data;
604 }
605 
ena_get_rss_hash(struct ena_com_dev * ena_dev,struct ethtool_rxnfc * cmd)606 static int ena_get_rss_hash(struct ena_com_dev *ena_dev,
607 			    struct ethtool_rxnfc *cmd)
608 {
609 	enum ena_admin_flow_hash_proto proto;
610 	u16 hash_fields;
611 	int rc;
612 
613 	cmd->data = 0;
614 
615 	switch (cmd->flow_type) {
616 	case TCP_V4_FLOW:
617 		proto = ENA_ADMIN_RSS_TCP4;
618 		break;
619 	case UDP_V4_FLOW:
620 		proto = ENA_ADMIN_RSS_UDP4;
621 		break;
622 	case TCP_V6_FLOW:
623 		proto = ENA_ADMIN_RSS_TCP6;
624 		break;
625 	case UDP_V6_FLOW:
626 		proto = ENA_ADMIN_RSS_UDP6;
627 		break;
628 	case IPV4_FLOW:
629 		proto = ENA_ADMIN_RSS_IP4;
630 		break;
631 	case IPV6_FLOW:
632 		proto = ENA_ADMIN_RSS_IP6;
633 		break;
634 	case ETHER_FLOW:
635 		proto = ENA_ADMIN_RSS_NOT_IP;
636 		break;
637 	case AH_V4_FLOW:
638 	case ESP_V4_FLOW:
639 	case AH_V6_FLOW:
640 	case ESP_V6_FLOW:
641 	case SCTP_V4_FLOW:
642 	case AH_ESP_V4_FLOW:
643 		return -EOPNOTSUPP;
644 	default:
645 		return -EINVAL;
646 	}
647 
648 	rc = ena_com_get_hash_ctrl(ena_dev, proto, &hash_fields);
649 	if (rc)
650 		return rc;
651 
652 	cmd->data = ena_flow_hash_to_flow_type(hash_fields);
653 
654 	return 0;
655 }
656 
ena_set_rss_hash(struct ena_com_dev * ena_dev,struct ethtool_rxnfc * cmd)657 static int ena_set_rss_hash(struct ena_com_dev *ena_dev,
658 			    struct ethtool_rxnfc *cmd)
659 {
660 	enum ena_admin_flow_hash_proto proto;
661 	u16 hash_fields;
662 
663 	switch (cmd->flow_type) {
664 	case TCP_V4_FLOW:
665 		proto = ENA_ADMIN_RSS_TCP4;
666 		break;
667 	case UDP_V4_FLOW:
668 		proto = ENA_ADMIN_RSS_UDP4;
669 		break;
670 	case TCP_V6_FLOW:
671 		proto = ENA_ADMIN_RSS_TCP6;
672 		break;
673 	case UDP_V6_FLOW:
674 		proto = ENA_ADMIN_RSS_UDP6;
675 		break;
676 	case IPV4_FLOW:
677 		proto = ENA_ADMIN_RSS_IP4;
678 		break;
679 	case IPV6_FLOW:
680 		proto = ENA_ADMIN_RSS_IP6;
681 		break;
682 	case ETHER_FLOW:
683 		proto = ENA_ADMIN_RSS_NOT_IP;
684 		break;
685 	case AH_V4_FLOW:
686 	case ESP_V4_FLOW:
687 	case AH_V6_FLOW:
688 	case ESP_V6_FLOW:
689 	case SCTP_V4_FLOW:
690 	case AH_ESP_V4_FLOW:
691 		return -EOPNOTSUPP;
692 	default:
693 		return -EINVAL;
694 	}
695 
696 	hash_fields = ena_flow_data_to_flow_hash(cmd->data);
697 
698 	return ena_com_fill_hash_ctrl(ena_dev, proto, hash_fields);
699 }
700 
ena_set_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * info)701 static int ena_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info)
702 {
703 	struct ena_adapter *adapter = netdev_priv(netdev);
704 	int rc = 0;
705 
706 	switch (info->cmd) {
707 	case ETHTOOL_SRXFH:
708 		rc = ena_set_rss_hash(adapter->ena_dev, info);
709 		break;
710 	case ETHTOOL_SRXCLSRLDEL:
711 	case ETHTOOL_SRXCLSRLINS:
712 	default:
713 		netif_err(adapter, drv, netdev,
714 			  "Command parameter %d is not supported\n", info->cmd);
715 		rc = -EOPNOTSUPP;
716 	}
717 
718 	return rc;
719 }
720 
ena_get_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * info,u32 * rules)721 static int ena_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
722 			 u32 *rules)
723 {
724 	struct ena_adapter *adapter = netdev_priv(netdev);
725 	int rc = 0;
726 
727 	switch (info->cmd) {
728 	case ETHTOOL_GRXRINGS:
729 		info->data = adapter->num_io_queues;
730 		rc = 0;
731 		break;
732 	case ETHTOOL_GRXFH:
733 		rc = ena_get_rss_hash(adapter->ena_dev, info);
734 		break;
735 	case ETHTOOL_GRXCLSRLCNT:
736 	case ETHTOOL_GRXCLSRULE:
737 	case ETHTOOL_GRXCLSRLALL:
738 	default:
739 		netif_err(adapter, drv, netdev,
740 			  "Command parameter %d is not supported\n", info->cmd);
741 		rc = -EOPNOTSUPP;
742 	}
743 
744 	return rc;
745 }
746 
ena_get_rxfh_indir_size(struct net_device * netdev)747 static u32 ena_get_rxfh_indir_size(struct net_device *netdev)
748 {
749 	return ENA_RX_RSS_TABLE_SIZE;
750 }
751 
ena_get_rxfh_key_size(struct net_device * netdev)752 static u32 ena_get_rxfh_key_size(struct net_device *netdev)
753 {
754 	return ENA_HASH_KEY_SIZE;
755 }
756 
ena_indirection_table_set(struct ena_adapter * adapter,const u32 * indir)757 static int ena_indirection_table_set(struct ena_adapter *adapter,
758 				     const u32 *indir)
759 {
760 	struct ena_com_dev *ena_dev = adapter->ena_dev;
761 	int i, rc;
762 
763 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
764 		rc = ena_com_indirect_table_fill_entry(ena_dev,
765 						       i,
766 						       ENA_IO_RXQ_IDX(indir[i]));
767 		if (unlikely(rc)) {
768 			netif_err(adapter, drv, adapter->netdev,
769 				  "Cannot fill indirect table (index is too large)\n");
770 			return rc;
771 		}
772 	}
773 
774 	rc = ena_com_indirect_table_set(ena_dev);
775 	if (rc) {
776 		netif_err(adapter, drv, adapter->netdev,
777 			  "Cannot set indirect table\n");
778 		return rc == -EPERM ? -EOPNOTSUPP : rc;
779 	}
780 	return rc;
781 }
782 
ena_indirection_table_get(struct ena_adapter * adapter,u32 * indir)783 static int ena_indirection_table_get(struct ena_adapter *adapter, u32 *indir)
784 {
785 	struct ena_com_dev *ena_dev = adapter->ena_dev;
786 	int i, rc;
787 
788 	if (!indir)
789 		return 0;
790 
791 	rc = ena_com_indirect_table_get(ena_dev, indir);
792 	if (rc)
793 		return rc;
794 
795 	/* Our internal representation of the indices is: even indices
796 	 * for Tx and uneven indices for Rx. We need to convert the Rx
797 	 * indices to be consecutive
798 	 */
799 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++)
800 		indir[i] = ENA_IO_RXQ_IDX_TO_COMBINED_IDX(indir[i]);
801 
802 	return rc;
803 }
804 
ena_get_rxfh(struct net_device * netdev,u32 * indir,u8 * key,u8 * hfunc)805 static int ena_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
806 			u8 *hfunc)
807 {
808 	struct ena_adapter *adapter = netdev_priv(netdev);
809 	enum ena_admin_hash_functions ena_func;
810 	u8 func;
811 	int rc;
812 
813 	rc = ena_indirection_table_get(adapter, indir);
814 	if (rc)
815 		return rc;
816 
817 	/* We call this function in order to check if the device
818 	 * supports getting/setting the hash function.
819 	 */
820 	rc = ena_com_get_hash_function(adapter->ena_dev, &ena_func);
821 	if (rc) {
822 		if (rc == -EOPNOTSUPP)
823 			rc = 0;
824 
825 		return rc;
826 	}
827 
828 	rc = ena_com_get_hash_key(adapter->ena_dev, key);
829 	if (rc)
830 		return rc;
831 
832 	switch (ena_func) {
833 	case ENA_ADMIN_TOEPLITZ:
834 		func = ETH_RSS_HASH_TOP;
835 		break;
836 	case ENA_ADMIN_CRC32:
837 		func = ETH_RSS_HASH_CRC32;
838 		break;
839 	default:
840 		netif_err(adapter, drv, netdev,
841 			  "Command parameter is not supported\n");
842 		return -EOPNOTSUPP;
843 	}
844 
845 	if (hfunc)
846 		*hfunc = func;
847 
848 	return 0;
849 }
850 
ena_set_rxfh(struct net_device * netdev,const u32 * indir,const u8 * key,const u8 hfunc)851 static int ena_set_rxfh(struct net_device *netdev, const u32 *indir,
852 			const u8 *key, const u8 hfunc)
853 {
854 	struct ena_adapter *adapter = netdev_priv(netdev);
855 	struct ena_com_dev *ena_dev = adapter->ena_dev;
856 	enum ena_admin_hash_functions func = 0;
857 	int rc;
858 
859 	if (indir) {
860 		rc = ena_indirection_table_set(adapter, indir);
861 		if (rc)
862 			return rc;
863 	}
864 
865 	switch (hfunc) {
866 	case ETH_RSS_HASH_NO_CHANGE:
867 		func = ena_com_get_current_hash_function(ena_dev);
868 		break;
869 	case ETH_RSS_HASH_TOP:
870 		func = ENA_ADMIN_TOEPLITZ;
871 		break;
872 	case ETH_RSS_HASH_CRC32:
873 		func = ENA_ADMIN_CRC32;
874 		break;
875 	default:
876 		netif_err(adapter, drv, netdev, "Unsupported hfunc %d\n",
877 			  hfunc);
878 		return -EOPNOTSUPP;
879 	}
880 
881 	if (key || func) {
882 		rc = ena_com_fill_hash_function(ena_dev, func, key,
883 						ENA_HASH_KEY_SIZE,
884 						0xFFFFFFFF);
885 		if (unlikely(rc)) {
886 			netif_err(adapter, drv, netdev, "Cannot fill key\n");
887 			return rc == -EPERM ? -EOPNOTSUPP : rc;
888 		}
889 	}
890 
891 	return 0;
892 }
893 
ena_get_channels(struct net_device * netdev,struct ethtool_channels * channels)894 static void ena_get_channels(struct net_device *netdev,
895 			     struct ethtool_channels *channels)
896 {
897 	struct ena_adapter *adapter = netdev_priv(netdev);
898 
899 	channels->max_combined = adapter->max_num_io_queues;
900 	channels->combined_count = adapter->num_io_queues;
901 }
902 
ena_set_channels(struct net_device * netdev,struct ethtool_channels * channels)903 static int ena_set_channels(struct net_device *netdev,
904 			    struct ethtool_channels *channels)
905 {
906 	struct ena_adapter *adapter = netdev_priv(netdev);
907 	u32 count = channels->combined_count;
908 	/* The check for max value is already done in ethtool */
909 	if (count < ENA_MIN_NUM_IO_QUEUES)
910 		return -EINVAL;
911 
912 	if (!ena_xdp_legal_queue_count(adapter, count)) {
913 		if (ena_xdp_present(adapter))
914 			return -EINVAL;
915 
916 		xdp_clear_features_flag(netdev);
917 	} else {
918 		xdp_set_features_flag(netdev,
919 				      NETDEV_XDP_ACT_BASIC |
920 				      NETDEV_XDP_ACT_REDIRECT);
921 	}
922 
923 	return ena_update_queue_count(adapter, count);
924 }
925 
ena_get_tunable(struct net_device * netdev,const struct ethtool_tunable * tuna,void * data)926 static int ena_get_tunable(struct net_device *netdev,
927 			   const struct ethtool_tunable *tuna, void *data)
928 {
929 	struct ena_adapter *adapter = netdev_priv(netdev);
930 	int ret = 0;
931 
932 	switch (tuna->id) {
933 	case ETHTOOL_RX_COPYBREAK:
934 		*(u32 *)data = adapter->rx_copybreak;
935 		break;
936 	default:
937 		ret = -EINVAL;
938 		break;
939 	}
940 
941 	return ret;
942 }
943 
ena_set_tunable(struct net_device * netdev,const struct ethtool_tunable * tuna,const void * data)944 static int ena_set_tunable(struct net_device *netdev,
945 			   const struct ethtool_tunable *tuna,
946 			   const void *data)
947 {
948 	struct ena_adapter *adapter = netdev_priv(netdev);
949 	int ret = 0;
950 	u32 len;
951 
952 	switch (tuna->id) {
953 	case ETHTOOL_RX_COPYBREAK:
954 		len = *(u32 *)data;
955 		ret = ena_set_rx_copybreak(adapter, len);
956 		break;
957 	default:
958 		ret = -EINVAL;
959 		break;
960 	}
961 
962 	return ret;
963 }
964 
965 static const struct ethtool_ops ena_ethtool_ops = {
966 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
967 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
968 	.supported_ring_params	= ETHTOOL_RING_USE_TX_PUSH_BUF_LEN |
969 				  ETHTOOL_RING_USE_TX_PUSH,
970 	.get_link_ksettings	= ena_get_link_ksettings,
971 	.get_drvinfo		= ena_get_drvinfo,
972 	.get_msglevel		= ena_get_msglevel,
973 	.set_msglevel		= ena_set_msglevel,
974 	.get_link		= ethtool_op_get_link,
975 	.get_coalesce		= ena_get_coalesce,
976 	.set_coalesce		= ena_set_coalesce,
977 	.get_ringparam		= ena_get_ringparam,
978 	.set_ringparam		= ena_set_ringparam,
979 	.get_sset_count         = ena_get_sset_count,
980 	.get_strings		= ena_get_ethtool_strings,
981 	.get_ethtool_stats      = ena_get_ethtool_stats,
982 	.get_rxnfc		= ena_get_rxnfc,
983 	.set_rxnfc		= ena_set_rxnfc,
984 	.get_rxfh_indir_size    = ena_get_rxfh_indir_size,
985 	.get_rxfh_key_size	= ena_get_rxfh_key_size,
986 	.get_rxfh		= ena_get_rxfh,
987 	.set_rxfh		= ena_set_rxfh,
988 	.get_channels		= ena_get_channels,
989 	.set_channels		= ena_set_channels,
990 	.get_tunable		= ena_get_tunable,
991 	.set_tunable		= ena_set_tunable,
992 	.get_ts_info            = ethtool_op_get_ts_info,
993 };
994 
ena_set_ethtool_ops(struct net_device * netdev)995 void ena_set_ethtool_ops(struct net_device *netdev)
996 {
997 	netdev->ethtool_ops = &ena_ethtool_ops;
998 }
999 
ena_dump_stats_ex(struct ena_adapter * adapter,u8 * buf)1000 static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
1001 {
1002 	struct net_device *netdev = adapter->netdev;
1003 	u8 *strings_buf;
1004 	u64 *data_buf;
1005 	int strings_num;
1006 	int i, rc;
1007 
1008 	strings_num = ena_get_sw_stats_count(adapter);
1009 	if (strings_num <= 0) {
1010 		netif_err(adapter, drv, netdev, "Can't get stats num\n");
1011 		return;
1012 	}
1013 
1014 	strings_buf = devm_kcalloc(&adapter->pdev->dev,
1015 				   ETH_GSTRING_LEN, strings_num,
1016 				   GFP_ATOMIC);
1017 	if (!strings_buf) {
1018 		netif_err(adapter, drv, netdev,
1019 			  "Failed to allocate strings_buf\n");
1020 		return;
1021 	}
1022 
1023 	data_buf = devm_kcalloc(&adapter->pdev->dev,
1024 				strings_num, sizeof(u64),
1025 				GFP_ATOMIC);
1026 	if (!data_buf) {
1027 		netif_err(adapter, drv, netdev,
1028 			  "Failed to allocate data buf\n");
1029 		devm_kfree(&adapter->pdev->dev, strings_buf);
1030 		return;
1031 	}
1032 
1033 	ena_get_strings(adapter, strings_buf, false);
1034 	ena_get_stats(adapter, data_buf, false);
1035 
1036 	/* If there is a buffer, dump stats, otherwise print them to dmesg */
1037 	if (buf)
1038 		for (i = 0; i < strings_num; i++) {
1039 			rc = snprintf(buf, ETH_GSTRING_LEN + sizeof(u64),
1040 				      "%s %llu\n",
1041 				      strings_buf + i * ETH_GSTRING_LEN,
1042 				      data_buf[i]);
1043 			buf += rc;
1044 		}
1045 	else
1046 		for (i = 0; i < strings_num; i++)
1047 			netif_err(adapter, drv, netdev, "%s: %llu\n",
1048 				  strings_buf + i * ETH_GSTRING_LEN,
1049 				  data_buf[i]);
1050 
1051 	devm_kfree(&adapter->pdev->dev, strings_buf);
1052 	devm_kfree(&adapter->pdev->dev, data_buf);
1053 }
1054 
ena_dump_stats_to_buf(struct ena_adapter * adapter,u8 * buf)1055 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf)
1056 {
1057 	if (!buf)
1058 		return;
1059 
1060 	ena_dump_stats_ex(adapter, buf);
1061 }
1062 
ena_dump_stats_to_dmesg(struct ena_adapter * adapter)1063 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter)
1064 {
1065 	ena_dump_stats_ex(adapter, NULL);
1066 }
1067