1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2015-2020 Amazon.com, Inc. or its affiliates.
6 #include <rte_string_fns.h>
8 #include <rte_version.h>
10 #include <rte_kvargs.h>
12 #include "ena_ethdev.h"
14 #include "ena_platform.h"
16 #include "ena_eth_com.h"
18 #include <ena_common_defs.h>
19 #include <ena_regs_defs.h>
20 #include <ena_admin_defs.h>
21 #include <ena_eth_io_defs.h>
23 #define DRV_MODULE_VER_MAJOR 2
24 #define DRV_MODULE_VER_MINOR 4
25 #define DRV_MODULE_VER_SUBMINOR 0
27 #define __MERGE_64B_H_L(h, l) (((uint64_t)h << 32) | l)
29 #define GET_L4_HDR_LEN(mbuf) \
30 ((rte_pktmbuf_mtod_offset(mbuf, struct rte_tcp_hdr *, \
31 mbuf->l3_len + mbuf->l2_len)->data_off) >> 4)
33 #define ETH_GSTRING_LEN 32
35 #define ARRAY_SIZE(x) RTE_DIM(x)
37 #define ENA_MIN_RING_DESC 128
39 #define ENA_PTYPE_HAS_HASH (RTE_PTYPE_L4_TCP | RTE_PTYPE_L4_UDP)
41 enum ethtool_stringset {
47 char name[ETH_GSTRING_LEN];
51 #define ENA_STAT_ENTRY(stat, stat_type) { \
53 .stat_offset = offsetof(struct ena_stats_##stat_type, stat) \
56 #define ENA_STAT_RX_ENTRY(stat) \
57 ENA_STAT_ENTRY(stat, rx)
59 #define ENA_STAT_TX_ENTRY(stat) \
60 ENA_STAT_ENTRY(stat, tx)
62 #define ENA_STAT_ENI_ENTRY(stat) \
63 ENA_STAT_ENTRY(stat, eni)
65 #define ENA_STAT_GLOBAL_ENTRY(stat) \
66 ENA_STAT_ENTRY(stat, dev)
68 /* Device arguments */
69 #define ENA_DEVARG_LARGE_LLQ_HDR "large_llq_hdr"
72 * Each rte_memzone should have unique name.
73 * To satisfy it, count number of allocation and add it to name.
75 rte_atomic64_t ena_alloc_cnt;
77 static const struct ena_stats ena_stats_global_strings[] = {
78 ENA_STAT_GLOBAL_ENTRY(wd_expired),
79 ENA_STAT_GLOBAL_ENTRY(dev_start),
80 ENA_STAT_GLOBAL_ENTRY(dev_stop),
81 ENA_STAT_GLOBAL_ENTRY(tx_drops),
84 static const struct ena_stats ena_stats_eni_strings[] = {
85 ENA_STAT_ENI_ENTRY(bw_in_allowance_exceeded),
86 ENA_STAT_ENI_ENTRY(bw_out_allowance_exceeded),
87 ENA_STAT_ENI_ENTRY(pps_allowance_exceeded),
88 ENA_STAT_ENI_ENTRY(conntrack_allowance_exceeded),
89 ENA_STAT_ENI_ENTRY(linklocal_allowance_exceeded),
92 static const struct ena_stats ena_stats_tx_strings[] = {
93 ENA_STAT_TX_ENTRY(cnt),
94 ENA_STAT_TX_ENTRY(bytes),
95 ENA_STAT_TX_ENTRY(prepare_ctx_err),
96 ENA_STAT_TX_ENTRY(linearize),
97 ENA_STAT_TX_ENTRY(linearize_failed),
98 ENA_STAT_TX_ENTRY(tx_poll),
99 ENA_STAT_TX_ENTRY(doorbells),
100 ENA_STAT_TX_ENTRY(bad_req_id),
101 ENA_STAT_TX_ENTRY(available_desc),
104 static const struct ena_stats ena_stats_rx_strings[] = {
105 ENA_STAT_RX_ENTRY(cnt),
106 ENA_STAT_RX_ENTRY(bytes),
107 ENA_STAT_RX_ENTRY(refill_partial),
108 ENA_STAT_RX_ENTRY(bad_csum),
109 ENA_STAT_RX_ENTRY(mbuf_alloc_fail),
110 ENA_STAT_RX_ENTRY(bad_desc_num),
111 ENA_STAT_RX_ENTRY(bad_req_id),
114 #define ENA_STATS_ARRAY_GLOBAL ARRAY_SIZE(ena_stats_global_strings)
115 #define ENA_STATS_ARRAY_ENI ARRAY_SIZE(ena_stats_eni_strings)
116 #define ENA_STATS_ARRAY_TX ARRAY_SIZE(ena_stats_tx_strings)
117 #define ENA_STATS_ARRAY_RX ARRAY_SIZE(ena_stats_rx_strings)
119 #define QUEUE_OFFLOADS (DEV_TX_OFFLOAD_TCP_CKSUM |\
120 DEV_TX_OFFLOAD_UDP_CKSUM |\
121 DEV_TX_OFFLOAD_IPV4_CKSUM |\
122 DEV_TX_OFFLOAD_TCP_TSO)
123 #define MBUF_OFFLOADS (PKT_TX_L4_MASK |\
127 /** Vendor ID used by Amazon devices */
128 #define PCI_VENDOR_ID_AMAZON 0x1D0F
129 /** Amazon devices */
130 #define PCI_DEVICE_ID_ENA_VF 0xEC20
131 #define PCI_DEVICE_ID_ENA_VF_RSERV0 0xEC21
133 #define ENA_TX_OFFLOAD_MASK (\
140 #define ENA_TX_OFFLOAD_NOTSUP_MASK \
141 (PKT_TX_OFFLOAD_MASK ^ ENA_TX_OFFLOAD_MASK)
143 static const struct rte_pci_id pci_id_ena_map[] = {
144 { RTE_PCI_DEVICE(PCI_VENDOR_ID_AMAZON, PCI_DEVICE_ID_ENA_VF) },
145 { RTE_PCI_DEVICE(PCI_VENDOR_ID_AMAZON, PCI_DEVICE_ID_ENA_VF_RSERV0) },
149 static struct ena_aenq_handlers aenq_handlers;
151 static int ena_device_init(struct ena_com_dev *ena_dev,
152 struct rte_pci_device *pdev,
153 struct ena_com_dev_get_features_ctx *get_feat_ctx,
155 static int ena_dev_configure(struct rte_eth_dev *dev);
156 static void ena_tx_map_mbuf(struct ena_ring *tx_ring,
157 struct ena_tx_buffer *tx_info,
158 struct rte_mbuf *mbuf,
160 uint16_t *header_len);
161 static int ena_xmit_mbuf(struct ena_ring *tx_ring, struct rte_mbuf *mbuf);
162 static void ena_tx_cleanup(struct ena_ring *tx_ring);
163 static uint16_t eth_ena_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
165 static uint16_t eth_ena_prep_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
167 static int ena_tx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
168 uint16_t nb_desc, unsigned int socket_id,
169 const struct rte_eth_txconf *tx_conf);
170 static int ena_rx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
171 uint16_t nb_desc, unsigned int socket_id,
172 const struct rte_eth_rxconf *rx_conf,
173 struct rte_mempool *mp);
174 static inline void ena_init_rx_mbuf(struct rte_mbuf *mbuf, uint16_t len);
175 static struct rte_mbuf *ena_rx_mbuf(struct ena_ring *rx_ring,
176 struct ena_com_rx_buf_info *ena_bufs,
178 uint16_t *next_to_clean,
180 static uint16_t eth_ena_recv_pkts(void *rx_queue,
181 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
182 static int ena_add_single_rx_desc(struct ena_com_io_sq *io_sq,
183 struct rte_mbuf *mbuf, uint16_t id);
184 static int ena_populate_rx_queue(struct ena_ring *rxq, unsigned int count);
185 static void ena_init_rings(struct ena_adapter *adapter,
186 bool disable_meta_caching);
187 static int ena_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
188 static int ena_start(struct rte_eth_dev *dev);
189 static int ena_stop(struct rte_eth_dev *dev);
190 static int ena_close(struct rte_eth_dev *dev);
191 static int ena_dev_reset(struct rte_eth_dev *dev);
192 static int ena_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats);
193 static void ena_rx_queue_release_all(struct rte_eth_dev *dev);
194 static void ena_tx_queue_release_all(struct rte_eth_dev *dev);
195 static void ena_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid);
196 static void ena_tx_queue_release(struct rte_eth_dev *dev, uint16_t qid);
197 static void ena_rx_queue_release_bufs(struct ena_ring *ring);
198 static void ena_tx_queue_release_bufs(struct ena_ring *ring);
199 static int ena_link_update(struct rte_eth_dev *dev,
200 int wait_to_complete);
201 static int ena_create_io_queue(struct rte_eth_dev *dev, struct ena_ring *ring);
202 static void ena_queue_stop(struct ena_ring *ring);
203 static void ena_queue_stop_all(struct rte_eth_dev *dev,
204 enum ena_ring_type ring_type);
205 static int ena_queue_start(struct rte_eth_dev *dev, struct ena_ring *ring);
206 static int ena_queue_start_all(struct rte_eth_dev *dev,
207 enum ena_ring_type ring_type);
208 static void ena_stats_restart(struct rte_eth_dev *dev);
209 static int ena_infos_get(struct rte_eth_dev *dev,
210 struct rte_eth_dev_info *dev_info);
211 static void ena_interrupt_handler_rte(void *cb_arg);
212 static void ena_timer_wd_callback(struct rte_timer *timer, void *arg);
213 static void ena_destroy_device(struct rte_eth_dev *eth_dev);
214 static int eth_ena_dev_init(struct rte_eth_dev *eth_dev);
215 static int ena_xstats_get_names(struct rte_eth_dev *dev,
216 struct rte_eth_xstat_name *xstats_names,
218 static int ena_xstats_get(struct rte_eth_dev *dev,
219 struct rte_eth_xstat *stats,
221 static int ena_xstats_get_by_id(struct rte_eth_dev *dev,
225 static int ena_process_bool_devarg(const char *key,
228 static int ena_parse_devargs(struct ena_adapter *adapter,
229 struct rte_devargs *devargs);
230 static int ena_copy_eni_stats(struct ena_adapter *adapter);
231 static int ena_setup_rx_intr(struct rte_eth_dev *dev);
232 static int ena_rx_queue_intr_enable(struct rte_eth_dev *dev,
234 static int ena_rx_queue_intr_disable(struct rte_eth_dev *dev,
237 static const struct eth_dev_ops ena_dev_ops = {
238 .dev_configure = ena_dev_configure,
239 .dev_infos_get = ena_infos_get,
240 .rx_queue_setup = ena_rx_queue_setup,
241 .tx_queue_setup = ena_tx_queue_setup,
242 .dev_start = ena_start,
243 .dev_stop = ena_stop,
244 .link_update = ena_link_update,
245 .stats_get = ena_stats_get,
246 .xstats_get_names = ena_xstats_get_names,
247 .xstats_get = ena_xstats_get,
248 .xstats_get_by_id = ena_xstats_get_by_id,
249 .mtu_set = ena_mtu_set,
250 .rx_queue_release = ena_rx_queue_release,
251 .tx_queue_release = ena_tx_queue_release,
252 .dev_close = ena_close,
253 .dev_reset = ena_dev_reset,
254 .reta_update = ena_rss_reta_update,
255 .reta_query = ena_rss_reta_query,
256 .rx_queue_intr_enable = ena_rx_queue_intr_enable,
257 .rx_queue_intr_disable = ena_rx_queue_intr_disable,
258 .rss_hash_update = ena_rss_hash_update,
259 .rss_hash_conf_get = ena_rss_hash_conf_get,
262 static inline void ena_rx_mbuf_prepare(struct rte_mbuf *mbuf,
263 struct ena_com_rx_ctx *ena_rx_ctx,
266 uint64_t ol_flags = 0;
267 uint32_t packet_type = 0;
269 if (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP)
270 packet_type |= RTE_PTYPE_L4_TCP;
271 else if (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)
272 packet_type |= RTE_PTYPE_L4_UDP;
274 if (ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4) {
275 packet_type |= RTE_PTYPE_L3_IPV4;
276 if (unlikely(ena_rx_ctx->l3_csum_err))
277 ol_flags |= PKT_RX_IP_CKSUM_BAD;
279 ol_flags |= PKT_RX_IP_CKSUM_GOOD;
280 } else if (ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV6) {
281 packet_type |= RTE_PTYPE_L3_IPV6;
284 if (!ena_rx_ctx->l4_csum_checked || ena_rx_ctx->frag)
285 ol_flags |= PKT_RX_L4_CKSUM_UNKNOWN;
287 if (unlikely(ena_rx_ctx->l4_csum_err))
288 ol_flags |= PKT_RX_L4_CKSUM_BAD;
290 ol_flags |= PKT_RX_L4_CKSUM_GOOD;
293 likely((packet_type & ENA_PTYPE_HAS_HASH) && !ena_rx_ctx->frag)) {
294 ol_flags |= PKT_RX_RSS_HASH;
295 mbuf->hash.rss = ena_rx_ctx->hash;
298 mbuf->ol_flags = ol_flags;
299 mbuf->packet_type = packet_type;
302 static inline void ena_tx_mbuf_prepare(struct rte_mbuf *mbuf,
303 struct ena_com_tx_ctx *ena_tx_ctx,
304 uint64_t queue_offloads,
305 bool disable_meta_caching)
307 struct ena_com_tx_meta *ena_meta = &ena_tx_ctx->ena_meta;
309 if ((mbuf->ol_flags & MBUF_OFFLOADS) &&
310 (queue_offloads & QUEUE_OFFLOADS)) {
311 /* check if TSO is required */
312 if ((mbuf->ol_flags & PKT_TX_TCP_SEG) &&
313 (queue_offloads & DEV_TX_OFFLOAD_TCP_TSO)) {
314 ena_tx_ctx->tso_enable = true;
316 ena_meta->l4_hdr_len = GET_L4_HDR_LEN(mbuf);
319 /* check if L3 checksum is needed */
320 if ((mbuf->ol_flags & PKT_TX_IP_CKSUM) &&
321 (queue_offloads & DEV_TX_OFFLOAD_IPV4_CKSUM))
322 ena_tx_ctx->l3_csum_enable = true;
324 if (mbuf->ol_flags & PKT_TX_IPV6) {
325 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
327 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
329 /* set don't fragment (DF) flag */
330 if (mbuf->packet_type &
331 (RTE_PTYPE_L4_NONFRAG
332 | RTE_PTYPE_INNER_L4_NONFRAG))
333 ena_tx_ctx->df = true;
336 /* check if L4 checksum is needed */
337 if (((mbuf->ol_flags & PKT_TX_L4_MASK) == PKT_TX_TCP_CKSUM) &&
338 (queue_offloads & DEV_TX_OFFLOAD_TCP_CKSUM)) {
339 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
340 ena_tx_ctx->l4_csum_enable = true;
341 } else if (((mbuf->ol_flags & PKT_TX_L4_MASK) ==
343 (queue_offloads & DEV_TX_OFFLOAD_UDP_CKSUM)) {
344 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
345 ena_tx_ctx->l4_csum_enable = true;
347 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UNKNOWN;
348 ena_tx_ctx->l4_csum_enable = false;
351 ena_meta->mss = mbuf->tso_segsz;
352 ena_meta->l3_hdr_len = mbuf->l3_len;
353 ena_meta->l3_hdr_offset = mbuf->l2_len;
355 ena_tx_ctx->meta_valid = true;
356 } else if (disable_meta_caching) {
357 memset(ena_meta, 0, sizeof(*ena_meta));
358 ena_tx_ctx->meta_valid = true;
360 ena_tx_ctx->meta_valid = false;
364 static int validate_tx_req_id(struct ena_ring *tx_ring, u16 req_id)
366 struct ena_tx_buffer *tx_info = NULL;
368 if (likely(req_id < tx_ring->ring_size)) {
369 tx_info = &tx_ring->tx_buffer_info[req_id];
370 if (likely(tx_info->mbuf))
375 PMD_TX_LOG(ERR, "tx_info doesn't have valid mbuf\n");
377 PMD_TX_LOG(ERR, "Invalid req_id: %hu\n", req_id);
379 /* Trigger device reset */
380 ++tx_ring->tx_stats.bad_req_id;
381 tx_ring->adapter->reset_reason = ENA_REGS_RESET_INV_TX_REQ_ID;
382 tx_ring->adapter->trigger_reset = true;
386 static void ena_config_host_info(struct ena_com_dev *ena_dev)
388 struct ena_admin_host_info *host_info;
391 /* Allocate only the host info */
392 rc = ena_com_allocate_host_info(ena_dev);
394 PMD_DRV_LOG(ERR, "Cannot allocate host info\n");
398 host_info = ena_dev->host_attr.host_info;
400 host_info->os_type = ENA_ADMIN_OS_DPDK;
401 host_info->kernel_ver = RTE_VERSION;
402 strlcpy((char *)host_info->kernel_ver_str, rte_version(),
403 sizeof(host_info->kernel_ver_str));
404 host_info->os_dist = RTE_VERSION;
405 strlcpy((char *)host_info->os_dist_str, rte_version(),
406 sizeof(host_info->os_dist_str));
407 host_info->driver_version =
408 (DRV_MODULE_VER_MAJOR) |
409 (DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
410 (DRV_MODULE_VER_SUBMINOR <<
411 ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
412 host_info->num_cpus = rte_lcore_count();
414 host_info->driver_supported_features =
415 ENA_ADMIN_HOST_INFO_RX_OFFSET_MASK |
416 ENA_ADMIN_HOST_INFO_RSS_CONFIGURABLE_FUNCTION_KEY_MASK;
418 rc = ena_com_set_host_attributes(ena_dev);
420 if (rc == -ENA_COM_UNSUPPORTED)
421 PMD_DRV_LOG(WARNING, "Cannot set host attributes\n");
423 PMD_DRV_LOG(ERR, "Cannot set host attributes\n");
431 ena_com_delete_host_info(ena_dev);
434 /* This function calculates the number of xstats based on the current config */
435 static unsigned int ena_xstats_calc_num(struct rte_eth_dev_data *data)
437 return ENA_STATS_ARRAY_GLOBAL + ENA_STATS_ARRAY_ENI +
438 (data->nb_tx_queues * ENA_STATS_ARRAY_TX) +
439 (data->nb_rx_queues * ENA_STATS_ARRAY_RX);
442 static void ena_config_debug_area(struct ena_adapter *adapter)
447 ss_count = ena_xstats_calc_num(adapter->edev_data);
449 /* allocate 32 bytes for each string and 64bit for the value */
450 debug_area_size = ss_count * ETH_GSTRING_LEN + sizeof(u64) * ss_count;
452 rc = ena_com_allocate_debug_area(&adapter->ena_dev, debug_area_size);
454 PMD_DRV_LOG(ERR, "Cannot allocate debug area\n");
458 rc = ena_com_set_host_attributes(&adapter->ena_dev);
460 if (rc == -ENA_COM_UNSUPPORTED)
461 PMD_DRV_LOG(WARNING, "Cannot set host attributes\n");
463 PMD_DRV_LOG(ERR, "Cannot set host attributes\n");
470 ena_com_delete_debug_area(&adapter->ena_dev);
473 static int ena_close(struct rte_eth_dev *dev)
475 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
476 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
477 struct ena_adapter *adapter = dev->data->dev_private;
480 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
483 if (adapter->state == ENA_ADAPTER_STATE_RUNNING)
485 adapter->state = ENA_ADAPTER_STATE_CLOSED;
487 ena_rx_queue_release_all(dev);
488 ena_tx_queue_release_all(dev);
490 rte_free(adapter->drv_stats);
491 adapter->drv_stats = NULL;
493 rte_intr_disable(intr_handle);
494 rte_intr_callback_unregister(intr_handle,
495 ena_interrupt_handler_rte,
499 * MAC is not allocated dynamically. Setting NULL should prevent from
500 * release of the resource in the rte_eth_dev_release_port().
502 dev->data->mac_addrs = NULL;
508 ena_dev_reset(struct rte_eth_dev *dev)
512 /* Cannot release memory in secondary process */
513 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
514 PMD_DRV_LOG(WARNING, "dev_reset not supported in secondary.\n");
518 ena_destroy_device(dev);
519 rc = eth_ena_dev_init(dev);
521 PMD_INIT_LOG(CRIT, "Cannot initialize device\n");
526 static void ena_rx_queue_release_all(struct rte_eth_dev *dev)
528 int nb_queues = dev->data->nb_rx_queues;
531 for (i = 0; i < nb_queues; i++)
532 ena_rx_queue_release(dev, i);
535 static void ena_tx_queue_release_all(struct rte_eth_dev *dev)
537 int nb_queues = dev->data->nb_tx_queues;
540 for (i = 0; i < nb_queues; i++)
541 ena_tx_queue_release(dev, i);
544 static void ena_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
546 struct ena_ring *ring = dev->data->rx_queues[qid];
548 /* Free ring resources */
549 if (ring->rx_buffer_info)
550 rte_free(ring->rx_buffer_info);
551 ring->rx_buffer_info = NULL;
553 if (ring->rx_refill_buffer)
554 rte_free(ring->rx_refill_buffer);
555 ring->rx_refill_buffer = NULL;
557 if (ring->empty_rx_reqs)
558 rte_free(ring->empty_rx_reqs);
559 ring->empty_rx_reqs = NULL;
561 ring->configured = 0;
563 PMD_DRV_LOG(NOTICE, "Rx queue %d:%d released\n",
564 ring->port_id, ring->id);
567 static void ena_tx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
569 struct ena_ring *ring = dev->data->tx_queues[qid];
571 /* Free ring resources */
572 if (ring->push_buf_intermediate_buf)
573 rte_free(ring->push_buf_intermediate_buf);
575 if (ring->tx_buffer_info)
576 rte_free(ring->tx_buffer_info);
578 if (ring->empty_tx_reqs)
579 rte_free(ring->empty_tx_reqs);
581 ring->empty_tx_reqs = NULL;
582 ring->tx_buffer_info = NULL;
583 ring->push_buf_intermediate_buf = NULL;
585 ring->configured = 0;
587 PMD_DRV_LOG(NOTICE, "Tx queue %d:%d released\n",
588 ring->port_id, ring->id);
591 static void ena_rx_queue_release_bufs(struct ena_ring *ring)
595 for (i = 0; i < ring->ring_size; ++i) {
596 struct ena_rx_buffer *rx_info = &ring->rx_buffer_info[i];
598 rte_mbuf_raw_free(rx_info->mbuf);
599 rx_info->mbuf = NULL;
604 static void ena_tx_queue_release_bufs(struct ena_ring *ring)
608 for (i = 0; i < ring->ring_size; ++i) {
609 struct ena_tx_buffer *tx_buf = &ring->tx_buffer_info[i];
612 rte_pktmbuf_free(tx_buf->mbuf);
618 static int ena_link_update(struct rte_eth_dev *dev,
619 __rte_unused int wait_to_complete)
621 struct rte_eth_link *link = &dev->data->dev_link;
622 struct ena_adapter *adapter = dev->data->dev_private;
624 link->link_status = adapter->link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
625 link->link_speed = ETH_SPEED_NUM_NONE;
626 link->link_duplex = ETH_LINK_FULL_DUPLEX;
631 static int ena_queue_start_all(struct rte_eth_dev *dev,
632 enum ena_ring_type ring_type)
634 struct ena_adapter *adapter = dev->data->dev_private;
635 struct ena_ring *queues = NULL;
640 if (ring_type == ENA_RING_TYPE_RX) {
641 queues = adapter->rx_ring;
642 nb_queues = dev->data->nb_rx_queues;
644 queues = adapter->tx_ring;
645 nb_queues = dev->data->nb_tx_queues;
647 for (i = 0; i < nb_queues; i++) {
648 if (queues[i].configured) {
649 if (ring_type == ENA_RING_TYPE_RX) {
651 dev->data->rx_queues[i] == &queues[i],
652 "Inconsistent state of Rx queues\n");
655 dev->data->tx_queues[i] == &queues[i],
656 "Inconsistent state of Tx queues\n");
659 rc = ena_queue_start(dev, &queues[i]);
663 "Failed to start queue[%d] of type(%d)\n",
674 if (queues[i].configured)
675 ena_queue_stop(&queues[i]);
680 static uint32_t ena_get_mtu_conf(struct ena_adapter *adapter)
682 uint32_t max_frame_len = adapter->max_mtu;
684 if (adapter->edev_data->dev_conf.rxmode.offloads &
685 DEV_RX_OFFLOAD_JUMBO_FRAME)
687 adapter->edev_data->dev_conf.rxmode.max_rx_pkt_len;
689 return max_frame_len;
692 static int ena_check_valid_conf(struct ena_adapter *adapter)
694 uint32_t max_frame_len = ena_get_mtu_conf(adapter);
696 if (max_frame_len > adapter->max_mtu || max_frame_len < ENA_MIN_MTU) {
698 "Unsupported MTU of %d. Max MTU: %d, min MTU: %d\n",
699 max_frame_len, adapter->max_mtu, ENA_MIN_MTU);
700 return ENA_COM_UNSUPPORTED;
707 ena_calc_io_queue_size(struct ena_calc_queue_size_ctx *ctx,
708 bool use_large_llq_hdr)
710 struct ena_admin_feature_llq_desc *llq = &ctx->get_feat_ctx->llq;
711 struct ena_com_dev *ena_dev = ctx->ena_dev;
712 uint32_t max_tx_queue_size;
713 uint32_t max_rx_queue_size;
715 if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
716 struct ena_admin_queue_ext_feature_fields *max_queue_ext =
717 &ctx->get_feat_ctx->max_queue_ext.max_queue_ext;
718 max_rx_queue_size = RTE_MIN(max_queue_ext->max_rx_cq_depth,
719 max_queue_ext->max_rx_sq_depth);
720 max_tx_queue_size = max_queue_ext->max_tx_cq_depth;
722 if (ena_dev->tx_mem_queue_type ==
723 ENA_ADMIN_PLACEMENT_POLICY_DEV) {
724 max_tx_queue_size = RTE_MIN(max_tx_queue_size,
727 max_tx_queue_size = RTE_MIN(max_tx_queue_size,
728 max_queue_ext->max_tx_sq_depth);
731 ctx->max_rx_sgl_size = RTE_MIN(ENA_PKT_MAX_BUFS,
732 max_queue_ext->max_per_packet_rx_descs);
733 ctx->max_tx_sgl_size = RTE_MIN(ENA_PKT_MAX_BUFS,
734 max_queue_ext->max_per_packet_tx_descs);
736 struct ena_admin_queue_feature_desc *max_queues =
737 &ctx->get_feat_ctx->max_queues;
738 max_rx_queue_size = RTE_MIN(max_queues->max_cq_depth,
739 max_queues->max_sq_depth);
740 max_tx_queue_size = max_queues->max_cq_depth;
742 if (ena_dev->tx_mem_queue_type ==
743 ENA_ADMIN_PLACEMENT_POLICY_DEV) {
744 max_tx_queue_size = RTE_MIN(max_tx_queue_size,
747 max_tx_queue_size = RTE_MIN(max_tx_queue_size,
748 max_queues->max_sq_depth);
751 ctx->max_rx_sgl_size = RTE_MIN(ENA_PKT_MAX_BUFS,
752 max_queues->max_packet_rx_descs);
753 ctx->max_tx_sgl_size = RTE_MIN(ENA_PKT_MAX_BUFS,
754 max_queues->max_packet_tx_descs);
757 /* Round down to the nearest power of 2 */
758 max_rx_queue_size = rte_align32prevpow2(max_rx_queue_size);
759 max_tx_queue_size = rte_align32prevpow2(max_tx_queue_size);
761 if (use_large_llq_hdr) {
762 if ((llq->entry_size_ctrl_supported &
763 ENA_ADMIN_LIST_ENTRY_SIZE_256B) &&
764 (ena_dev->tx_mem_queue_type ==
765 ENA_ADMIN_PLACEMENT_POLICY_DEV)) {
766 max_tx_queue_size /= 2;
768 "Forcing large headers and decreasing maximum Tx queue size to %d\n",
772 "Forcing large headers failed: LLQ is disabled or device does not support large headers\n");
776 if (unlikely(max_rx_queue_size == 0 || max_tx_queue_size == 0)) {
777 PMD_INIT_LOG(ERR, "Invalid queue size\n");
781 ctx->max_tx_queue_size = max_tx_queue_size;
782 ctx->max_rx_queue_size = max_rx_queue_size;
787 static void ena_stats_restart(struct rte_eth_dev *dev)
789 struct ena_adapter *adapter = dev->data->dev_private;
791 rte_atomic64_init(&adapter->drv_stats->ierrors);
792 rte_atomic64_init(&adapter->drv_stats->oerrors);
793 rte_atomic64_init(&adapter->drv_stats->rx_nombuf);
794 adapter->drv_stats->rx_drops = 0;
797 static int ena_stats_get(struct rte_eth_dev *dev,
798 struct rte_eth_stats *stats)
800 struct ena_admin_basic_stats ena_stats;
801 struct ena_adapter *adapter = dev->data->dev_private;
802 struct ena_com_dev *ena_dev = &adapter->ena_dev;
807 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
810 memset(&ena_stats, 0, sizeof(ena_stats));
812 rte_spinlock_lock(&adapter->admin_lock);
813 rc = ena_com_get_dev_basic_stats(ena_dev, &ena_stats);
814 rte_spinlock_unlock(&adapter->admin_lock);
816 PMD_DRV_LOG(ERR, "Could not retrieve statistics from ENA\n");
820 /* Set of basic statistics from ENA */
821 stats->ipackets = __MERGE_64B_H_L(ena_stats.rx_pkts_high,
822 ena_stats.rx_pkts_low);
823 stats->opackets = __MERGE_64B_H_L(ena_stats.tx_pkts_high,
824 ena_stats.tx_pkts_low);
825 stats->ibytes = __MERGE_64B_H_L(ena_stats.rx_bytes_high,
826 ena_stats.rx_bytes_low);
827 stats->obytes = __MERGE_64B_H_L(ena_stats.tx_bytes_high,
828 ena_stats.tx_bytes_low);
830 /* Driver related stats */
831 stats->imissed = adapter->drv_stats->rx_drops;
832 stats->ierrors = rte_atomic64_read(&adapter->drv_stats->ierrors);
833 stats->oerrors = rte_atomic64_read(&adapter->drv_stats->oerrors);
834 stats->rx_nombuf = rte_atomic64_read(&adapter->drv_stats->rx_nombuf);
836 max_rings_stats = RTE_MIN(dev->data->nb_rx_queues,
837 RTE_ETHDEV_QUEUE_STAT_CNTRS);
838 for (i = 0; i < max_rings_stats; ++i) {
839 struct ena_stats_rx *rx_stats = &adapter->rx_ring[i].rx_stats;
841 stats->q_ibytes[i] = rx_stats->bytes;
842 stats->q_ipackets[i] = rx_stats->cnt;
843 stats->q_errors[i] = rx_stats->bad_desc_num +
844 rx_stats->bad_req_id;
847 max_rings_stats = RTE_MIN(dev->data->nb_tx_queues,
848 RTE_ETHDEV_QUEUE_STAT_CNTRS);
849 for (i = 0; i < max_rings_stats; ++i) {
850 struct ena_stats_tx *tx_stats = &adapter->tx_ring[i].tx_stats;
852 stats->q_obytes[i] = tx_stats->bytes;
853 stats->q_opackets[i] = tx_stats->cnt;
859 static int ena_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
861 struct ena_adapter *adapter;
862 struct ena_com_dev *ena_dev;
865 ena_assert_msg(dev->data != NULL, "Uninitialized device\n");
866 ena_assert_msg(dev->data->dev_private != NULL, "Uninitialized device\n");
867 adapter = dev->data->dev_private;
869 ena_dev = &adapter->ena_dev;
870 ena_assert_msg(ena_dev != NULL, "Uninitialized device\n");
872 if (mtu > ena_get_mtu_conf(adapter) || mtu < ENA_MIN_MTU) {
874 "Invalid MTU setting. New MTU: %d, max MTU: %d, min MTU: %d\n",
875 mtu, ena_get_mtu_conf(adapter), ENA_MIN_MTU);
879 rc = ena_com_set_dev_mtu(ena_dev, mtu);
881 PMD_DRV_LOG(ERR, "Could not set MTU: %d\n", mtu);
883 PMD_DRV_LOG(NOTICE, "MTU set to: %d\n", mtu);
888 static int ena_start(struct rte_eth_dev *dev)
890 struct ena_adapter *adapter = dev->data->dev_private;
894 /* Cannot allocate memory in secondary process */
895 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
896 PMD_DRV_LOG(WARNING, "dev_start not supported in secondary.\n");
900 rc = ena_check_valid_conf(adapter);
904 rc = ena_setup_rx_intr(dev);
908 rc = ena_queue_start_all(dev, ENA_RING_TYPE_RX);
912 rc = ena_queue_start_all(dev, ENA_RING_TYPE_TX);
916 if (adapter->edev_data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
917 rc = ena_rss_configure(adapter);
922 ena_stats_restart(dev);
924 adapter->timestamp_wd = rte_get_timer_cycles();
925 adapter->keep_alive_timeout = ENA_DEVICE_KALIVE_TIMEOUT;
927 ticks = rte_get_timer_hz();
928 rte_timer_reset(&adapter->timer_wd, ticks, PERIODICAL, rte_lcore_id(),
929 ena_timer_wd_callback, dev);
931 ++adapter->dev_stats.dev_start;
932 adapter->state = ENA_ADAPTER_STATE_RUNNING;
937 ena_queue_stop_all(dev, ENA_RING_TYPE_TX);
939 ena_queue_stop_all(dev, ENA_RING_TYPE_RX);
943 static int ena_stop(struct rte_eth_dev *dev)
945 struct ena_adapter *adapter = dev->data->dev_private;
946 struct ena_com_dev *ena_dev = &adapter->ena_dev;
947 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
948 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
951 /* Cannot free memory in secondary process */
952 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
953 PMD_DRV_LOG(WARNING, "dev_stop not supported in secondary.\n");
957 rte_timer_stop_sync(&adapter->timer_wd);
958 ena_queue_stop_all(dev, ENA_RING_TYPE_TX);
959 ena_queue_stop_all(dev, ENA_RING_TYPE_RX);
961 if (adapter->trigger_reset) {
962 rc = ena_com_dev_reset(ena_dev, adapter->reset_reason);
964 PMD_DRV_LOG(ERR, "Device reset failed, rc: %d\n", rc);
967 rte_intr_disable(intr_handle);
969 rte_intr_efd_disable(intr_handle);
970 if (intr_handle->intr_vec != NULL) {
971 rte_free(intr_handle->intr_vec);
972 intr_handle->intr_vec = NULL;
975 rte_intr_enable(intr_handle);
977 ++adapter->dev_stats.dev_stop;
978 adapter->state = ENA_ADAPTER_STATE_STOPPED;
979 dev->data->dev_started = 0;
984 static int ena_create_io_queue(struct rte_eth_dev *dev, struct ena_ring *ring)
986 struct ena_adapter *adapter = ring->adapter;
987 struct ena_com_dev *ena_dev = &adapter->ena_dev;
988 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
989 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
990 struct ena_com_create_io_ctx ctx =
991 /* policy set to _HOST just to satisfy icc compiler */
992 { ENA_ADMIN_PLACEMENT_POLICY_HOST,
998 ctx.msix_vector = -1;
999 if (ring->type == ENA_RING_TYPE_TX) {
1000 ena_qid = ENA_IO_TXQ_IDX(ring->id);
1001 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
1002 ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
1003 for (i = 0; i < ring->ring_size; i++)
1004 ring->empty_tx_reqs[i] = i;
1006 ena_qid = ENA_IO_RXQ_IDX(ring->id);
1007 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
1008 if (rte_intr_dp_is_en(intr_handle))
1009 ctx.msix_vector = intr_handle->intr_vec[ring->id];
1010 for (i = 0; i < ring->ring_size; i++)
1011 ring->empty_rx_reqs[i] = i;
1013 ctx.queue_size = ring->ring_size;
1015 ctx.numa_node = ring->numa_socket_id;
1017 rc = ena_com_create_io_queue(ena_dev, &ctx);
1020 "Failed to create IO queue[%d] (qid:%d), rc: %d\n",
1021 ring->id, ena_qid, rc);
1025 rc = ena_com_get_io_handlers(ena_dev, ena_qid,
1026 &ring->ena_com_io_sq,
1027 &ring->ena_com_io_cq);
1030 "Failed to get IO queue[%d] handlers, rc: %d\n",
1032 ena_com_destroy_io_queue(ena_dev, ena_qid);
1036 if (ring->type == ENA_RING_TYPE_TX)
1037 ena_com_update_numa_node(ring->ena_com_io_cq, ctx.numa_node);
1039 /* Start with Rx interrupts being masked. */
1040 if (ring->type == ENA_RING_TYPE_RX && rte_intr_dp_is_en(intr_handle))
1041 ena_rx_queue_intr_disable(dev, ring->id);
1046 static void ena_queue_stop(struct ena_ring *ring)
1048 struct ena_com_dev *ena_dev = &ring->adapter->ena_dev;
1050 if (ring->type == ENA_RING_TYPE_RX) {
1051 ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(ring->id));
1052 ena_rx_queue_release_bufs(ring);
1054 ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(ring->id));
1055 ena_tx_queue_release_bufs(ring);
1059 static void ena_queue_stop_all(struct rte_eth_dev *dev,
1060 enum ena_ring_type ring_type)
1062 struct ena_adapter *adapter = dev->data->dev_private;
1063 struct ena_ring *queues = NULL;
1064 uint16_t nb_queues, i;
1066 if (ring_type == ENA_RING_TYPE_RX) {
1067 queues = adapter->rx_ring;
1068 nb_queues = dev->data->nb_rx_queues;
1070 queues = adapter->tx_ring;
1071 nb_queues = dev->data->nb_tx_queues;
1074 for (i = 0; i < nb_queues; ++i)
1075 if (queues[i].configured)
1076 ena_queue_stop(&queues[i]);
1079 static int ena_queue_start(struct rte_eth_dev *dev, struct ena_ring *ring)
1083 ena_assert_msg(ring->configured == 1,
1084 "Trying to start unconfigured queue\n");
1086 rc = ena_create_io_queue(dev, ring);
1088 PMD_INIT_LOG(ERR, "Failed to create IO queue\n");
1092 ring->next_to_clean = 0;
1093 ring->next_to_use = 0;
1095 if (ring->type == ENA_RING_TYPE_TX) {
1096 ring->tx_stats.available_desc =
1097 ena_com_free_q_entries(ring->ena_com_io_sq);
1101 bufs_num = ring->ring_size - 1;
1102 rc = ena_populate_rx_queue(ring, bufs_num);
1103 if (rc != bufs_num) {
1104 ena_com_destroy_io_queue(&ring->adapter->ena_dev,
1105 ENA_IO_RXQ_IDX(ring->id));
1106 PMD_INIT_LOG(ERR, "Failed to populate Rx ring\n");
1107 return ENA_COM_FAULT;
1109 /* Flush per-core RX buffers pools cache as they can be used on other
1112 rte_mempool_cache_flush(NULL, ring->mb_pool);
1117 static int ena_tx_queue_setup(struct rte_eth_dev *dev,
1120 unsigned int socket_id,
1121 const struct rte_eth_txconf *tx_conf)
1123 struct ena_ring *txq = NULL;
1124 struct ena_adapter *adapter = dev->data->dev_private;
1127 txq = &adapter->tx_ring[queue_idx];
1129 if (txq->configured) {
1131 "API violation. Queue[%d] is already configured\n",
1133 return ENA_COM_FAULT;
1136 if (!rte_is_power_of_2(nb_desc)) {
1138 "Unsupported size of Tx queue: %d is not a power of 2.\n",
1143 if (nb_desc > adapter->max_tx_ring_size) {
1145 "Unsupported size of Tx queue (max size: %d)\n",
1146 adapter->max_tx_ring_size);
1150 txq->port_id = dev->data->port_id;
1151 txq->next_to_clean = 0;
1152 txq->next_to_use = 0;
1153 txq->ring_size = nb_desc;
1154 txq->size_mask = nb_desc - 1;
1155 txq->numa_socket_id = socket_id;
1156 txq->pkts_without_db = false;
1158 txq->tx_buffer_info = rte_zmalloc("txq->tx_buffer_info",
1159 sizeof(struct ena_tx_buffer) *
1161 RTE_CACHE_LINE_SIZE);
1162 if (!txq->tx_buffer_info) {
1164 "Failed to allocate memory for Tx buffer info\n");
1168 txq->empty_tx_reqs = rte_zmalloc("txq->empty_tx_reqs",
1169 sizeof(u16) * txq->ring_size,
1170 RTE_CACHE_LINE_SIZE);
1171 if (!txq->empty_tx_reqs) {
1173 "Failed to allocate memory for empty Tx requests\n");
1174 rte_free(txq->tx_buffer_info);
1178 txq->push_buf_intermediate_buf =
1179 rte_zmalloc("txq->push_buf_intermediate_buf",
1180 txq->tx_max_header_size,
1181 RTE_CACHE_LINE_SIZE);
1182 if (!txq->push_buf_intermediate_buf) {
1183 PMD_DRV_LOG(ERR, "Failed to alloc push buffer for LLQ\n");
1184 rte_free(txq->tx_buffer_info);
1185 rte_free(txq->empty_tx_reqs);
1189 for (i = 0; i < txq->ring_size; i++)
1190 txq->empty_tx_reqs[i] = i;
1192 if (tx_conf != NULL) {
1194 tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
1196 /* Store pointer to this queue in upper layer */
1197 txq->configured = 1;
1198 dev->data->tx_queues[queue_idx] = txq;
1203 static int ena_rx_queue_setup(struct rte_eth_dev *dev,
1206 unsigned int socket_id,
1207 const struct rte_eth_rxconf *rx_conf,
1208 struct rte_mempool *mp)
1210 struct ena_adapter *adapter = dev->data->dev_private;
1211 struct ena_ring *rxq = NULL;
1215 rxq = &adapter->rx_ring[queue_idx];
1216 if (rxq->configured) {
1218 "API violation. Queue[%d] is already configured\n",
1220 return ENA_COM_FAULT;
1223 if (!rte_is_power_of_2(nb_desc)) {
1225 "Unsupported size of Rx queue: %d is not a power of 2.\n",
1230 if (nb_desc > adapter->max_rx_ring_size) {
1232 "Unsupported size of Rx queue (max size: %d)\n",
1233 adapter->max_rx_ring_size);
1237 /* ENA isn't supporting buffers smaller than 1400 bytes */
1238 buffer_size = rte_pktmbuf_data_room_size(mp) - RTE_PKTMBUF_HEADROOM;
1239 if (buffer_size < ENA_RX_BUF_MIN_SIZE) {
1241 "Unsupported size of Rx buffer: %zu (min size: %d)\n",
1242 buffer_size, ENA_RX_BUF_MIN_SIZE);
1246 rxq->port_id = dev->data->port_id;
1247 rxq->next_to_clean = 0;
1248 rxq->next_to_use = 0;
1249 rxq->ring_size = nb_desc;
1250 rxq->size_mask = nb_desc - 1;
1251 rxq->numa_socket_id = socket_id;
1254 rxq->rx_buffer_info = rte_zmalloc("rxq->buffer_info",
1255 sizeof(struct ena_rx_buffer) * nb_desc,
1256 RTE_CACHE_LINE_SIZE);
1257 if (!rxq->rx_buffer_info) {
1259 "Failed to allocate memory for Rx buffer info\n");
1263 rxq->rx_refill_buffer = rte_zmalloc("rxq->rx_refill_buffer",
1264 sizeof(struct rte_mbuf *) * nb_desc,
1265 RTE_CACHE_LINE_SIZE);
1267 if (!rxq->rx_refill_buffer) {
1269 "Failed to allocate memory for Rx refill buffer\n");
1270 rte_free(rxq->rx_buffer_info);
1271 rxq->rx_buffer_info = NULL;
1275 rxq->empty_rx_reqs = rte_zmalloc("rxq->empty_rx_reqs",
1276 sizeof(uint16_t) * nb_desc,
1277 RTE_CACHE_LINE_SIZE);
1278 if (!rxq->empty_rx_reqs) {
1280 "Failed to allocate memory for empty Rx requests\n");
1281 rte_free(rxq->rx_buffer_info);
1282 rxq->rx_buffer_info = NULL;
1283 rte_free(rxq->rx_refill_buffer);
1284 rxq->rx_refill_buffer = NULL;
1288 for (i = 0; i < nb_desc; i++)
1289 rxq->empty_rx_reqs[i] = i;
1291 rxq->offloads = rx_conf->offloads | dev->data->dev_conf.rxmode.offloads;
1293 /* Store pointer to this queue in upper layer */
1294 rxq->configured = 1;
1295 dev->data->rx_queues[queue_idx] = rxq;
1300 static int ena_add_single_rx_desc(struct ena_com_io_sq *io_sq,
1301 struct rte_mbuf *mbuf, uint16_t id)
1303 struct ena_com_buf ebuf;
1306 /* prepare physical address for DMA transaction */
1307 ebuf.paddr = mbuf->buf_iova + RTE_PKTMBUF_HEADROOM;
1308 ebuf.len = mbuf->buf_len - RTE_PKTMBUF_HEADROOM;
1310 /* pass resource to device */
1311 rc = ena_com_add_single_rx_desc(io_sq, &ebuf, id);
1312 if (unlikely(rc != 0))
1313 PMD_RX_LOG(WARNING, "Failed adding Rx desc\n");
1318 static int ena_populate_rx_queue(struct ena_ring *rxq, unsigned int count)
1322 uint16_t next_to_use = rxq->next_to_use;
1324 #ifdef RTE_ETHDEV_DEBUG_RX
1327 struct rte_mbuf **mbufs = rxq->rx_refill_buffer;
1329 if (unlikely(!count))
1332 #ifdef RTE_ETHDEV_DEBUG_RX
1333 in_use = rxq->ring_size - 1 -
1334 ena_com_free_q_entries(rxq->ena_com_io_sq);
1335 if (unlikely((in_use + count) >= rxq->ring_size))
1336 PMD_RX_LOG(ERR, "Bad Rx ring state\n");
1339 /* get resources for incoming packets */
1340 rc = rte_pktmbuf_alloc_bulk(rxq->mb_pool, mbufs, count);
1341 if (unlikely(rc < 0)) {
1342 rte_atomic64_inc(&rxq->adapter->drv_stats->rx_nombuf);
1343 ++rxq->rx_stats.mbuf_alloc_fail;
1344 PMD_RX_LOG(DEBUG, "There are not enough free buffers\n");
1348 for (i = 0; i < count; i++) {
1349 struct rte_mbuf *mbuf = mbufs[i];
1350 struct ena_rx_buffer *rx_info;
1352 if (likely((i + 4) < count))
1353 rte_prefetch0(mbufs[i + 4]);
1355 req_id = rxq->empty_rx_reqs[next_to_use];
1356 rx_info = &rxq->rx_buffer_info[req_id];
1358 rc = ena_add_single_rx_desc(rxq->ena_com_io_sq, mbuf, req_id);
1359 if (unlikely(rc != 0))
1362 rx_info->mbuf = mbuf;
1363 next_to_use = ENA_IDX_NEXT_MASKED(next_to_use, rxq->size_mask);
1366 if (unlikely(i < count)) {
1368 "Refilled Rx queue[%d] with only %d/%d buffers\n",
1370 rte_pktmbuf_free_bulk(&mbufs[i], count - i);
1371 ++rxq->rx_stats.refill_partial;
1374 /* When we submitted free recources to device... */
1375 if (likely(i > 0)) {
1376 /* ...let HW know that it can fill buffers with data. */
1377 ena_com_write_sq_doorbell(rxq->ena_com_io_sq);
1379 rxq->next_to_use = next_to_use;
1385 static int ena_device_init(struct ena_com_dev *ena_dev,
1386 struct rte_pci_device *pdev,
1387 struct ena_com_dev_get_features_ctx *get_feat_ctx,
1390 uint32_t aenq_groups;
1392 bool readless_supported;
1394 /* Initialize mmio registers */
1395 rc = ena_com_mmio_reg_read_request_init(ena_dev);
1397 PMD_DRV_LOG(ERR, "Failed to init MMIO read less\n");
1401 /* The PCIe configuration space revision id indicate if mmio reg
1404 readless_supported = !(pdev->id.class_id & ENA_MMIO_DISABLE_REG_READ);
1405 ena_com_set_mmio_read_mode(ena_dev, readless_supported);
1408 rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
1410 PMD_DRV_LOG(ERR, "Cannot reset device\n");
1411 goto err_mmio_read_less;
1414 /* check FW version */
1415 rc = ena_com_validate_version(ena_dev);
1417 PMD_DRV_LOG(ERR, "Device version is too low\n");
1418 goto err_mmio_read_less;
1421 ena_dev->dma_addr_bits = ena_com_get_dma_width(ena_dev);
1423 /* ENA device administration layer init */
1424 rc = ena_com_admin_init(ena_dev, &aenq_handlers);
1427 "Cannot initialize ENA admin queue\n");
1428 goto err_mmio_read_less;
1431 /* To enable the msix interrupts the driver needs to know the number
1432 * of queues. So the driver uses polling mode to retrieve this
1435 ena_com_set_admin_polling_mode(ena_dev, true);
1437 ena_config_host_info(ena_dev);
1439 /* Get Device Attributes and features */
1440 rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
1443 "Cannot get attribute for ENA device, rc: %d\n", rc);
1444 goto err_admin_init;
1447 aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
1448 BIT(ENA_ADMIN_NOTIFICATION) |
1449 BIT(ENA_ADMIN_KEEP_ALIVE) |
1450 BIT(ENA_ADMIN_FATAL_ERROR) |
1451 BIT(ENA_ADMIN_WARNING);
1453 aenq_groups &= get_feat_ctx->aenq.supported_groups;
1454 rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
1456 PMD_DRV_LOG(ERR, "Cannot configure AENQ groups, rc: %d\n", rc);
1457 goto err_admin_init;
1460 *wd_state = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
1465 ena_com_admin_destroy(ena_dev);
1468 ena_com_mmio_reg_read_request_destroy(ena_dev);
1473 static void ena_interrupt_handler_rte(void *cb_arg)
1475 struct rte_eth_dev *dev = cb_arg;
1476 struct ena_adapter *adapter = dev->data->dev_private;
1477 struct ena_com_dev *ena_dev = &adapter->ena_dev;
1479 ena_com_admin_q_comp_intr_handler(ena_dev);
1480 if (likely(adapter->state != ENA_ADAPTER_STATE_CLOSED))
1481 ena_com_aenq_intr_handler(ena_dev, dev);
1484 static void check_for_missing_keep_alive(struct ena_adapter *adapter)
1486 if (!adapter->wd_state)
1489 if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT)
1492 if (unlikely((rte_get_timer_cycles() - adapter->timestamp_wd) >=
1493 adapter->keep_alive_timeout)) {
1494 PMD_DRV_LOG(ERR, "Keep alive timeout\n");
1495 adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO;
1496 adapter->trigger_reset = true;
1497 ++adapter->dev_stats.wd_expired;
1501 /* Check if admin queue is enabled */
1502 static void check_for_admin_com_state(struct ena_adapter *adapter)
1504 if (unlikely(!ena_com_get_admin_running_state(&adapter->ena_dev))) {
1505 PMD_DRV_LOG(ERR, "ENA admin queue is not in running state\n");
1506 adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO;
1507 adapter->trigger_reset = true;
1511 static void ena_timer_wd_callback(__rte_unused struct rte_timer *timer,
1514 struct rte_eth_dev *dev = arg;
1515 struct ena_adapter *adapter = dev->data->dev_private;
1517 check_for_missing_keep_alive(adapter);
1518 check_for_admin_com_state(adapter);
1520 if (unlikely(adapter->trigger_reset)) {
1521 PMD_DRV_LOG(ERR, "Trigger reset is on\n");
1522 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RESET,
1528 set_default_llq_configurations(struct ena_llq_configurations *llq_config,
1529 struct ena_admin_feature_llq_desc *llq,
1530 bool use_large_llq_hdr)
1532 llq_config->llq_header_location = ENA_ADMIN_INLINE_HEADER;
1533 llq_config->llq_stride_ctrl = ENA_ADMIN_MULTIPLE_DESCS_PER_ENTRY;
1534 llq_config->llq_num_decs_before_header =
1535 ENA_ADMIN_LLQ_NUM_DESCS_BEFORE_HEADER_2;
1537 if (use_large_llq_hdr &&
1538 (llq->entry_size_ctrl_supported & ENA_ADMIN_LIST_ENTRY_SIZE_256B)) {
1539 llq_config->llq_ring_entry_size =
1540 ENA_ADMIN_LIST_ENTRY_SIZE_256B;
1541 llq_config->llq_ring_entry_size_value = 256;
1543 llq_config->llq_ring_entry_size =
1544 ENA_ADMIN_LIST_ENTRY_SIZE_128B;
1545 llq_config->llq_ring_entry_size_value = 128;
1550 ena_set_queues_placement_policy(struct ena_adapter *adapter,
1551 struct ena_com_dev *ena_dev,
1552 struct ena_admin_feature_llq_desc *llq,
1553 struct ena_llq_configurations *llq_default_configurations)
1556 u32 llq_feature_mask;
1558 llq_feature_mask = 1 << ENA_ADMIN_LLQ;
1559 if (!(ena_dev->supported_features & llq_feature_mask)) {
1561 "LLQ is not supported. Fallback to host mode policy.\n");
1562 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
1566 rc = ena_com_config_dev_mode(ena_dev, llq, llq_default_configurations);
1568 PMD_INIT_LOG(WARNING,
1569 "Failed to config dev mode. Fallback to host mode policy.\n");
1570 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
1574 /* Nothing to config, exit */
1575 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_HOST)
1578 if (!adapter->dev_mem_base) {
1580 "Unable to access LLQ BAR resource. Fallback to host mode policy.\n");
1581 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
1585 ena_dev->mem_bar = adapter->dev_mem_base;
1590 static uint32_t ena_calc_max_io_queue_num(struct ena_com_dev *ena_dev,
1591 struct ena_com_dev_get_features_ctx *get_feat_ctx)
1593 uint32_t io_tx_sq_num, io_tx_cq_num, io_rx_num, max_num_io_queues;
1595 /* Regular queues capabilities */
1596 if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
1597 struct ena_admin_queue_ext_feature_fields *max_queue_ext =
1598 &get_feat_ctx->max_queue_ext.max_queue_ext;
1599 io_rx_num = RTE_MIN(max_queue_ext->max_rx_sq_num,
1600 max_queue_ext->max_rx_cq_num);
1601 io_tx_sq_num = max_queue_ext->max_tx_sq_num;
1602 io_tx_cq_num = max_queue_ext->max_tx_cq_num;
1604 struct ena_admin_queue_feature_desc *max_queues =
1605 &get_feat_ctx->max_queues;
1606 io_tx_sq_num = max_queues->max_sq_num;
1607 io_tx_cq_num = max_queues->max_cq_num;
1608 io_rx_num = RTE_MIN(io_tx_sq_num, io_tx_cq_num);
1611 /* In case of LLQ use the llq number in the get feature cmd */
1612 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
1613 io_tx_sq_num = get_feat_ctx->llq.max_llq_num;
1615 max_num_io_queues = RTE_MIN(ENA_MAX_NUM_IO_QUEUES, io_rx_num);
1616 max_num_io_queues = RTE_MIN(max_num_io_queues, io_tx_sq_num);
1617 max_num_io_queues = RTE_MIN(max_num_io_queues, io_tx_cq_num);
1619 if (unlikely(max_num_io_queues == 0)) {
1620 PMD_DRV_LOG(ERR, "Number of IO queues cannot not be 0\n");
1624 return max_num_io_queues;
1627 static int eth_ena_dev_init(struct rte_eth_dev *eth_dev)
1629 struct ena_calc_queue_size_ctx calc_queue_ctx = { 0 };
1630 struct rte_pci_device *pci_dev;
1631 struct rte_intr_handle *intr_handle;
1632 struct ena_adapter *adapter = eth_dev->data->dev_private;
1633 struct ena_com_dev *ena_dev = &adapter->ena_dev;
1634 struct ena_com_dev_get_features_ctx get_feat_ctx;
1635 struct ena_llq_configurations llq_config;
1636 const char *queue_type_str;
1637 uint32_t max_num_io_queues;
1639 static int adapters_found;
1640 bool disable_meta_caching;
1641 bool wd_state = false;
1643 eth_dev->dev_ops = &ena_dev_ops;
1644 eth_dev->rx_pkt_burst = ð_ena_recv_pkts;
1645 eth_dev->tx_pkt_burst = ð_ena_xmit_pkts;
1646 eth_dev->tx_pkt_prepare = ð_ena_prep_pkts;
1648 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1651 eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1653 memset(adapter, 0, sizeof(struct ena_adapter));
1654 ena_dev = &adapter->ena_dev;
1656 adapter->edev_data = eth_dev->data;
1658 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1660 PMD_INIT_LOG(INFO, "Initializing %x:%x:%x.%d\n",
1661 pci_dev->addr.domain,
1663 pci_dev->addr.devid,
1664 pci_dev->addr.function);
1666 intr_handle = &pci_dev->intr_handle;
1668 adapter->regs = pci_dev->mem_resource[ENA_REGS_BAR].addr;
1669 adapter->dev_mem_base = pci_dev->mem_resource[ENA_MEM_BAR].addr;
1671 if (!adapter->regs) {
1672 PMD_INIT_LOG(CRIT, "Failed to access registers BAR(%d)\n",
1677 ena_dev->reg_bar = adapter->regs;
1678 /* This is a dummy pointer for ena_com functions. */
1679 ena_dev->dmadev = adapter;
1681 adapter->id_number = adapters_found;
1683 snprintf(adapter->name, ENA_NAME_MAX_LEN, "ena_%d",
1684 adapter->id_number);
1686 rc = ena_parse_devargs(adapter, pci_dev->device.devargs);
1688 PMD_INIT_LOG(CRIT, "Failed to parse devargs\n");
1692 /* device specific initialization routine */
1693 rc = ena_device_init(ena_dev, pci_dev, &get_feat_ctx, &wd_state);
1695 PMD_INIT_LOG(CRIT, "Failed to init ENA device\n");
1698 adapter->wd_state = wd_state;
1700 set_default_llq_configurations(&llq_config, &get_feat_ctx.llq,
1701 adapter->use_large_llq_hdr);
1702 rc = ena_set_queues_placement_policy(adapter, ena_dev,
1703 &get_feat_ctx.llq, &llq_config);
1705 PMD_INIT_LOG(CRIT, "Failed to set placement policy\n");
1709 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_HOST)
1710 queue_type_str = "Regular";
1712 queue_type_str = "Low latency";
1713 PMD_DRV_LOG(INFO, "Placement policy: %s\n", queue_type_str);
1715 calc_queue_ctx.ena_dev = ena_dev;
1716 calc_queue_ctx.get_feat_ctx = &get_feat_ctx;
1718 max_num_io_queues = ena_calc_max_io_queue_num(ena_dev, &get_feat_ctx);
1719 rc = ena_calc_io_queue_size(&calc_queue_ctx,
1720 adapter->use_large_llq_hdr);
1721 if (unlikely((rc != 0) || (max_num_io_queues == 0))) {
1723 goto err_device_destroy;
1726 adapter->max_tx_ring_size = calc_queue_ctx.max_tx_queue_size;
1727 adapter->max_rx_ring_size = calc_queue_ctx.max_rx_queue_size;
1728 adapter->max_tx_sgl_size = calc_queue_ctx.max_tx_sgl_size;
1729 adapter->max_rx_sgl_size = calc_queue_ctx.max_rx_sgl_size;
1730 adapter->max_num_io_queues = max_num_io_queues;
1732 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
1733 disable_meta_caching =
1734 !!(get_feat_ctx.llq.accel_mode.u.get.supported_flags &
1735 BIT(ENA_ADMIN_DISABLE_META_CACHING));
1737 disable_meta_caching = false;
1740 /* prepare ring structures */
1741 ena_init_rings(adapter, disable_meta_caching);
1743 ena_config_debug_area(adapter);
1745 /* Set max MTU for this device */
1746 adapter->max_mtu = get_feat_ctx.dev_attr.max_mtu;
1748 /* set device support for offloads */
1749 adapter->offloads.tso4_supported = (get_feat_ctx.offload.tx &
1750 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK) != 0;
1751 adapter->offloads.tx_csum_supported = (get_feat_ctx.offload.tx &
1752 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK) != 0;
1753 adapter->offloads.rx_csum_supported =
1754 (get_feat_ctx.offload.rx_supported &
1755 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK) != 0;
1756 adapter->offloads.rss_hash_supported =
1757 (get_feat_ctx.offload.rx_supported &
1758 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_HASH_MASK) != 0;
1760 /* Copy MAC address and point DPDK to it */
1761 eth_dev->data->mac_addrs = (struct rte_ether_addr *)adapter->mac_addr;
1762 rte_ether_addr_copy((struct rte_ether_addr *)
1763 get_feat_ctx.dev_attr.mac_addr,
1764 (struct rte_ether_addr *)adapter->mac_addr);
1766 rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
1767 if (unlikely(rc != 0)) {
1768 PMD_DRV_LOG(ERR, "Failed to initialize RSS in ENA device\n");
1769 goto err_delete_debug_area;
1772 adapter->drv_stats = rte_zmalloc("adapter stats",
1773 sizeof(*adapter->drv_stats),
1774 RTE_CACHE_LINE_SIZE);
1775 if (!adapter->drv_stats) {
1777 "Failed to allocate memory for adapter statistics\n");
1779 goto err_rss_destroy;
1782 rte_spinlock_init(&adapter->admin_lock);
1784 rte_intr_callback_register(intr_handle,
1785 ena_interrupt_handler_rte,
1787 rte_intr_enable(intr_handle);
1788 ena_com_set_admin_polling_mode(ena_dev, false);
1789 ena_com_admin_aenq_enable(ena_dev);
1791 if (adapters_found == 0)
1792 rte_timer_subsystem_init();
1793 rte_timer_init(&adapter->timer_wd);
1796 adapter->state = ENA_ADAPTER_STATE_INIT;
1801 ena_com_rss_destroy(ena_dev);
1802 err_delete_debug_area:
1803 ena_com_delete_debug_area(ena_dev);
1806 ena_com_delete_host_info(ena_dev);
1807 ena_com_admin_destroy(ena_dev);
1813 static void ena_destroy_device(struct rte_eth_dev *eth_dev)
1815 struct ena_adapter *adapter = eth_dev->data->dev_private;
1816 struct ena_com_dev *ena_dev = &adapter->ena_dev;
1818 if (adapter->state == ENA_ADAPTER_STATE_FREE)
1821 ena_com_set_admin_running_state(ena_dev, false);
1823 if (adapter->state != ENA_ADAPTER_STATE_CLOSED)
1826 ena_com_rss_destroy(ena_dev);
1828 ena_com_delete_debug_area(ena_dev);
1829 ena_com_delete_host_info(ena_dev);
1831 ena_com_abort_admin_commands(ena_dev);
1832 ena_com_wait_for_abort_completion(ena_dev);
1833 ena_com_admin_destroy(ena_dev);
1834 ena_com_mmio_reg_read_request_destroy(ena_dev);
1836 adapter->state = ENA_ADAPTER_STATE_FREE;
1839 static int eth_ena_dev_uninit(struct rte_eth_dev *eth_dev)
1841 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1844 ena_destroy_device(eth_dev);
1849 static int ena_dev_configure(struct rte_eth_dev *dev)
1851 struct ena_adapter *adapter = dev->data->dev_private;
1853 adapter->state = ENA_ADAPTER_STATE_CONFIG;
1855 if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
1856 dev->data->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
1857 dev->data->dev_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
1859 adapter->tx_selected_offloads = dev->data->dev_conf.txmode.offloads;
1860 adapter->rx_selected_offloads = dev->data->dev_conf.rxmode.offloads;
1864 static void ena_init_rings(struct ena_adapter *adapter,
1865 bool disable_meta_caching)
1869 for (i = 0; i < adapter->max_num_io_queues; i++) {
1870 struct ena_ring *ring = &adapter->tx_ring[i];
1872 ring->configured = 0;
1873 ring->type = ENA_RING_TYPE_TX;
1874 ring->adapter = adapter;
1876 ring->tx_mem_queue_type = adapter->ena_dev.tx_mem_queue_type;
1877 ring->tx_max_header_size = adapter->ena_dev.tx_max_header_size;
1878 ring->sgl_size = adapter->max_tx_sgl_size;
1879 ring->disable_meta_caching = disable_meta_caching;
1882 for (i = 0; i < adapter->max_num_io_queues; i++) {
1883 struct ena_ring *ring = &adapter->rx_ring[i];
1885 ring->configured = 0;
1886 ring->type = ENA_RING_TYPE_RX;
1887 ring->adapter = adapter;
1889 ring->sgl_size = adapter->max_rx_sgl_size;
1893 static int ena_infos_get(struct rte_eth_dev *dev,
1894 struct rte_eth_dev_info *dev_info)
1896 struct ena_adapter *adapter;
1897 struct ena_com_dev *ena_dev;
1898 uint64_t rx_feat = 0, tx_feat = 0;
1900 ena_assert_msg(dev->data != NULL, "Uninitialized device\n");
1901 ena_assert_msg(dev->data->dev_private != NULL, "Uninitialized device\n");
1902 adapter = dev->data->dev_private;
1904 ena_dev = &adapter->ena_dev;
1905 ena_assert_msg(ena_dev != NULL, "Uninitialized device\n");
1907 dev_info->speed_capa =
1909 ETH_LINK_SPEED_2_5G |
1911 ETH_LINK_SPEED_10G |
1912 ETH_LINK_SPEED_25G |
1913 ETH_LINK_SPEED_40G |
1914 ETH_LINK_SPEED_50G |
1915 ETH_LINK_SPEED_100G;
1917 /* Set Tx & Rx features available for device */
1918 if (adapter->offloads.tso4_supported)
1919 tx_feat |= DEV_TX_OFFLOAD_TCP_TSO;
1921 if (adapter->offloads.tx_csum_supported)
1922 tx_feat |= DEV_TX_OFFLOAD_IPV4_CKSUM |
1923 DEV_TX_OFFLOAD_UDP_CKSUM |
1924 DEV_TX_OFFLOAD_TCP_CKSUM;
1926 if (adapter->offloads.rx_csum_supported)
1927 rx_feat |= DEV_RX_OFFLOAD_IPV4_CKSUM |
1928 DEV_RX_OFFLOAD_UDP_CKSUM |
1929 DEV_RX_OFFLOAD_TCP_CKSUM;
1931 rx_feat |= DEV_RX_OFFLOAD_JUMBO_FRAME;
1932 tx_feat |= DEV_TX_OFFLOAD_MULTI_SEGS;
1934 /* Inform framework about available features */
1935 dev_info->rx_offload_capa = rx_feat;
1936 if (adapter->offloads.rss_hash_supported)
1937 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_RSS_HASH;
1938 dev_info->rx_queue_offload_capa = rx_feat;
1939 dev_info->tx_offload_capa = tx_feat;
1940 dev_info->tx_queue_offload_capa = tx_feat;
1942 dev_info->flow_type_rss_offloads = ENA_ALL_RSS_HF;
1943 dev_info->hash_key_size = ENA_HASH_KEY_SIZE;
1945 dev_info->min_rx_bufsize = ENA_MIN_FRAME_LEN;
1946 dev_info->max_rx_pktlen = adapter->max_mtu;
1947 dev_info->max_mac_addrs = 1;
1949 dev_info->max_rx_queues = adapter->max_num_io_queues;
1950 dev_info->max_tx_queues = adapter->max_num_io_queues;
1951 dev_info->reta_size = ENA_RX_RSS_TABLE_SIZE;
1953 adapter->tx_supported_offloads = tx_feat;
1954 adapter->rx_supported_offloads = rx_feat;
1956 dev_info->rx_desc_lim.nb_max = adapter->max_rx_ring_size;
1957 dev_info->rx_desc_lim.nb_min = ENA_MIN_RING_DESC;
1958 dev_info->rx_desc_lim.nb_seg_max = RTE_MIN(ENA_PKT_MAX_BUFS,
1959 adapter->max_rx_sgl_size);
1960 dev_info->rx_desc_lim.nb_mtu_seg_max = RTE_MIN(ENA_PKT_MAX_BUFS,
1961 adapter->max_rx_sgl_size);
1963 dev_info->tx_desc_lim.nb_max = adapter->max_tx_ring_size;
1964 dev_info->tx_desc_lim.nb_min = ENA_MIN_RING_DESC;
1965 dev_info->tx_desc_lim.nb_seg_max = RTE_MIN(ENA_PKT_MAX_BUFS,
1966 adapter->max_tx_sgl_size);
1967 dev_info->tx_desc_lim.nb_mtu_seg_max = RTE_MIN(ENA_PKT_MAX_BUFS,
1968 adapter->max_tx_sgl_size);
1970 dev_info->default_rxportconf.ring_size = ENA_DEFAULT_RING_SIZE;
1971 dev_info->default_txportconf.ring_size = ENA_DEFAULT_RING_SIZE;
1976 static inline void ena_init_rx_mbuf(struct rte_mbuf *mbuf, uint16_t len)
1978 mbuf->data_len = len;
1979 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
1984 static struct rte_mbuf *ena_rx_mbuf(struct ena_ring *rx_ring,
1985 struct ena_com_rx_buf_info *ena_bufs,
1987 uint16_t *next_to_clean,
1990 struct rte_mbuf *mbuf;
1991 struct rte_mbuf *mbuf_head;
1992 struct ena_rx_buffer *rx_info;
1994 uint16_t ntc, len, req_id, buf = 0;
1996 if (unlikely(descs == 0))
1999 ntc = *next_to_clean;
2001 len = ena_bufs[buf].len;
2002 req_id = ena_bufs[buf].req_id;
2004 rx_info = &rx_ring->rx_buffer_info[req_id];
2006 mbuf = rx_info->mbuf;
2007 RTE_ASSERT(mbuf != NULL);
2009 ena_init_rx_mbuf(mbuf, len);
2011 /* Fill the mbuf head with the data specific for 1st segment. */
2013 mbuf_head->nb_segs = descs;
2014 mbuf_head->port = rx_ring->port_id;
2015 mbuf_head->pkt_len = len;
2016 mbuf_head->data_off += offset;
2018 rx_info->mbuf = NULL;
2019 rx_ring->empty_rx_reqs[ntc] = req_id;
2020 ntc = ENA_IDX_NEXT_MASKED(ntc, rx_ring->size_mask);
2024 len = ena_bufs[buf].len;
2025 req_id = ena_bufs[buf].req_id;
2027 rx_info = &rx_ring->rx_buffer_info[req_id];
2028 RTE_ASSERT(rx_info->mbuf != NULL);
2030 if (unlikely(len == 0)) {
2032 * Some devices can pass descriptor with the length 0.
2033 * To avoid confusion, the PMD is simply putting the
2034 * descriptor back, as it was never used. We'll avoid
2035 * mbuf allocation that way.
2037 rc = ena_add_single_rx_desc(rx_ring->ena_com_io_sq,
2038 rx_info->mbuf, req_id);
2039 if (unlikely(rc != 0)) {
2040 /* Free the mbuf in case of an error. */
2041 rte_mbuf_raw_free(rx_info->mbuf);
2044 * If there was no error, just exit the loop as
2045 * 0 length descriptor is always the last one.
2050 /* Create an mbuf chain. */
2051 mbuf->next = rx_info->mbuf;
2054 ena_init_rx_mbuf(mbuf, len);
2055 mbuf_head->pkt_len += len;
2059 * Mark the descriptor as depleted and perform necessary
2061 * This code will execute in two cases:
2062 * 1. Descriptor len was greater than 0 - normal situation.
2063 * 2. Descriptor len was 0 and we failed to add the descriptor
2064 * to the device. In that situation, we should try to add
2065 * the mbuf again in the populate routine and mark the
2066 * descriptor as used up by the device.
2068 rx_info->mbuf = NULL;
2069 rx_ring->empty_rx_reqs[ntc] = req_id;
2070 ntc = ENA_IDX_NEXT_MASKED(ntc, rx_ring->size_mask);
2073 *next_to_clean = ntc;
2078 static uint16_t eth_ena_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
2081 struct ena_ring *rx_ring = (struct ena_ring *)(rx_queue);
2082 unsigned int free_queue_entries;
2083 unsigned int refill_threshold;
2084 uint16_t next_to_clean = rx_ring->next_to_clean;
2085 uint16_t descs_in_use;
2086 struct rte_mbuf *mbuf;
2088 struct ena_com_rx_ctx ena_rx_ctx;
2092 #ifdef RTE_ETHDEV_DEBUG_RX
2093 /* Check adapter state */
2094 if (unlikely(rx_ring->adapter->state != ENA_ADAPTER_STATE_RUNNING)) {
2096 "Trying to receive pkts while device is NOT running\n");
2101 fill_hash = rx_ring->offloads & DEV_RX_OFFLOAD_RSS_HASH;
2103 descs_in_use = rx_ring->ring_size -
2104 ena_com_free_q_entries(rx_ring->ena_com_io_sq) - 1;
2105 nb_pkts = RTE_MIN(descs_in_use, nb_pkts);
2107 for (completed = 0; completed < nb_pkts; completed++) {
2108 ena_rx_ctx.max_bufs = rx_ring->sgl_size;
2109 ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
2110 ena_rx_ctx.descs = 0;
2111 ena_rx_ctx.pkt_offset = 0;
2112 /* receive packet context */
2113 rc = ena_com_rx_pkt(rx_ring->ena_com_io_cq,
2114 rx_ring->ena_com_io_sq,
2118 "Failed to get the packet from the device, rc: %d\n",
2120 if (rc == ENA_COM_NO_SPACE) {
2121 ++rx_ring->rx_stats.bad_desc_num;
2122 rx_ring->adapter->reset_reason =
2123 ENA_REGS_RESET_TOO_MANY_RX_DESCS;
2125 ++rx_ring->rx_stats.bad_req_id;
2126 rx_ring->adapter->reset_reason =
2127 ENA_REGS_RESET_INV_RX_REQ_ID;
2129 rx_ring->adapter->trigger_reset = true;
2133 mbuf = ena_rx_mbuf(rx_ring,
2134 ena_rx_ctx.ena_bufs,
2137 ena_rx_ctx.pkt_offset);
2138 if (unlikely(mbuf == NULL)) {
2139 for (i = 0; i < ena_rx_ctx.descs; ++i) {
2140 rx_ring->empty_rx_reqs[next_to_clean] =
2141 rx_ring->ena_bufs[i].req_id;
2142 next_to_clean = ENA_IDX_NEXT_MASKED(
2143 next_to_clean, rx_ring->size_mask);
2148 /* fill mbuf attributes if any */
2149 ena_rx_mbuf_prepare(mbuf, &ena_rx_ctx, fill_hash);
2151 if (unlikely(mbuf->ol_flags &
2152 (PKT_RX_IP_CKSUM_BAD | PKT_RX_L4_CKSUM_BAD))) {
2153 rte_atomic64_inc(&rx_ring->adapter->drv_stats->ierrors);
2154 ++rx_ring->rx_stats.bad_csum;
2157 rx_pkts[completed] = mbuf;
2158 rx_ring->rx_stats.bytes += mbuf->pkt_len;
2161 rx_ring->rx_stats.cnt += completed;
2162 rx_ring->next_to_clean = next_to_clean;
2164 free_queue_entries = ena_com_free_q_entries(rx_ring->ena_com_io_sq);
2166 RTE_MIN(rx_ring->ring_size / ENA_REFILL_THRESH_DIVIDER,
2167 (unsigned int)ENA_REFILL_THRESH_PACKET);
2169 /* Burst refill to save doorbells, memory barriers, const interval */
2170 if (free_queue_entries > refill_threshold) {
2171 ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq);
2172 ena_populate_rx_queue(rx_ring, free_queue_entries);
2179 eth_ena_prep_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
2185 struct ena_ring *tx_ring = (struct ena_ring *)(tx_queue);
2186 struct rte_ipv4_hdr *ip_hdr;
2188 uint16_t frag_field;
2190 for (i = 0; i != nb_pkts; i++) {
2192 ol_flags = m->ol_flags;
2194 if (!(ol_flags & PKT_TX_IPV4))
2197 /* If there was not L2 header length specified, assume it is
2198 * length of the ethernet header.
2200 if (unlikely(m->l2_len == 0))
2201 m->l2_len = sizeof(struct rte_ether_hdr);
2203 ip_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2205 frag_field = rte_be_to_cpu_16(ip_hdr->fragment_offset);
2207 if ((frag_field & RTE_IPV4_HDR_DF_FLAG) != 0) {
2208 m->packet_type |= RTE_PTYPE_L4_NONFRAG;
2210 /* If IPv4 header has DF flag enabled and TSO support is
2211 * disabled, partial chcecksum should not be calculated.
2213 if (!tx_ring->adapter->offloads.tso4_supported)
2217 if ((ol_flags & ENA_TX_OFFLOAD_NOTSUP_MASK) != 0 ||
2218 (ol_flags & PKT_TX_L4_MASK) ==
2219 PKT_TX_SCTP_CKSUM) {
2220 rte_errno = ENOTSUP;
2224 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
2225 ret = rte_validate_tx_offload(m);
2232 /* In case we are supposed to TSO and have DF not set (DF=0)
2233 * hardware must be provided with partial checksum, otherwise
2234 * it will take care of necessary calculations.
2237 ret = rte_net_intel_cksum_flags_prepare(m,
2238 ol_flags & ~PKT_TX_TCP_SEG);
2248 static void ena_update_hints(struct ena_adapter *adapter,
2249 struct ena_admin_ena_hw_hints *hints)
2251 if (hints->admin_completion_tx_timeout)
2252 adapter->ena_dev.admin_queue.completion_timeout =
2253 hints->admin_completion_tx_timeout * 1000;
2255 if (hints->mmio_read_timeout)
2256 /* convert to usec */
2257 adapter->ena_dev.mmio_read.reg_read_to =
2258 hints->mmio_read_timeout * 1000;
2260 if (hints->driver_watchdog_timeout) {
2261 if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT)
2262 adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT;
2264 // Convert msecs to ticks
2265 adapter->keep_alive_timeout =
2266 (hints->driver_watchdog_timeout *
2267 rte_get_timer_hz()) / 1000;
2271 static int ena_check_space_and_linearize_mbuf(struct ena_ring *tx_ring,
2272 struct rte_mbuf *mbuf)
2274 struct ena_com_dev *ena_dev;
2275 int num_segments, header_len, rc;
2277 ena_dev = &tx_ring->adapter->ena_dev;
2278 num_segments = mbuf->nb_segs;
2279 header_len = mbuf->data_len;
2281 if (likely(num_segments < tx_ring->sgl_size))
2284 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV &&
2285 (num_segments == tx_ring->sgl_size) &&
2286 (header_len < tx_ring->tx_max_header_size))
2289 /* Checking for space for 2 additional metadata descriptors due to
2290 * possible header split and metadata descriptor. Linearization will
2291 * be needed so we reduce the segments number from num_segments to 1
2293 if (!ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq, 3)) {
2294 PMD_TX_LOG(DEBUG, "Not enough space in the Tx queue\n");
2295 return ENA_COM_NO_MEM;
2297 ++tx_ring->tx_stats.linearize;
2298 rc = rte_pktmbuf_linearize(mbuf);
2300 PMD_TX_LOG(WARNING, "Mbuf linearize failed\n");
2301 rte_atomic64_inc(&tx_ring->adapter->drv_stats->ierrors);
2302 ++tx_ring->tx_stats.linearize_failed;
2309 /* Checking for space for 2 additional metadata descriptors due to
2310 * possible header split and metadata descriptor
2312 if (!ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
2313 num_segments + 2)) {
2314 PMD_TX_LOG(DEBUG, "Not enough space in the Tx queue\n");
2315 return ENA_COM_NO_MEM;
2321 static void ena_tx_map_mbuf(struct ena_ring *tx_ring,
2322 struct ena_tx_buffer *tx_info,
2323 struct rte_mbuf *mbuf,
2325 uint16_t *header_len)
2327 struct ena_com_buf *ena_buf;
2328 uint16_t delta, seg_len, push_len;
2331 seg_len = mbuf->data_len;
2333 tx_info->mbuf = mbuf;
2334 ena_buf = tx_info->bufs;
2336 if (tx_ring->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
2338 * Tx header might be (and will be in most cases) smaller than
2339 * tx_max_header_size. But it's not an issue to send more data
2340 * to the device, than actually needed if the mbuf size is
2341 * greater than tx_max_header_size.
2343 push_len = RTE_MIN(mbuf->pkt_len, tx_ring->tx_max_header_size);
2344 *header_len = push_len;
2346 if (likely(push_len <= seg_len)) {
2347 /* If the push header is in the single segment, then
2348 * just point it to the 1st mbuf data.
2350 *push_header = rte_pktmbuf_mtod(mbuf, uint8_t *);
2352 /* If the push header lays in the several segments, copy
2353 * it to the intermediate buffer.
2355 rte_pktmbuf_read(mbuf, 0, push_len,
2356 tx_ring->push_buf_intermediate_buf);
2357 *push_header = tx_ring->push_buf_intermediate_buf;
2358 delta = push_len - seg_len;
2361 *push_header = NULL;
2366 /* Process first segment taking into consideration pushed header */
2367 if (seg_len > push_len) {
2368 ena_buf->paddr = mbuf->buf_iova +
2371 ena_buf->len = seg_len - push_len;
2373 tx_info->num_of_bufs++;
2376 while ((mbuf = mbuf->next) != NULL) {
2377 seg_len = mbuf->data_len;
2379 /* Skip mbufs if whole data is pushed as a header */
2380 if (unlikely(delta > seg_len)) {
2385 ena_buf->paddr = mbuf->buf_iova + mbuf->data_off + delta;
2386 ena_buf->len = seg_len - delta;
2388 tx_info->num_of_bufs++;
2394 static int ena_xmit_mbuf(struct ena_ring *tx_ring, struct rte_mbuf *mbuf)
2396 struct ena_tx_buffer *tx_info;
2397 struct ena_com_tx_ctx ena_tx_ctx = { { 0 } };
2398 uint16_t next_to_use;
2399 uint16_t header_len;
2405 rc = ena_check_space_and_linearize_mbuf(tx_ring, mbuf);
2409 next_to_use = tx_ring->next_to_use;
2411 req_id = tx_ring->empty_tx_reqs[next_to_use];
2412 tx_info = &tx_ring->tx_buffer_info[req_id];
2413 tx_info->num_of_bufs = 0;
2415 ena_tx_map_mbuf(tx_ring, tx_info, mbuf, &push_header, &header_len);
2417 ena_tx_ctx.ena_bufs = tx_info->bufs;
2418 ena_tx_ctx.push_header = push_header;
2419 ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
2420 ena_tx_ctx.req_id = req_id;
2421 ena_tx_ctx.header_len = header_len;
2423 /* Set Tx offloads flags, if applicable */
2424 ena_tx_mbuf_prepare(mbuf, &ena_tx_ctx, tx_ring->offloads,
2425 tx_ring->disable_meta_caching);
2427 if (unlikely(ena_com_is_doorbell_needed(tx_ring->ena_com_io_sq,
2430 "LLQ Tx max burst size of queue %d achieved, writing doorbell to send burst\n",
2432 ena_com_write_sq_doorbell(tx_ring->ena_com_io_sq);
2433 tx_ring->tx_stats.doorbells++;
2434 tx_ring->pkts_without_db = false;
2437 /* prepare the packet's descriptors to dma engine */
2438 rc = ena_com_prepare_tx(tx_ring->ena_com_io_sq, &ena_tx_ctx,
2441 PMD_DRV_LOG(ERR, "Failed to prepare Tx buffers, rc: %d\n", rc);
2442 ++tx_ring->tx_stats.prepare_ctx_err;
2443 tx_ring->adapter->reset_reason =
2444 ENA_REGS_RESET_DRIVER_INVALID_STATE;
2445 tx_ring->adapter->trigger_reset = true;
2449 tx_info->tx_descs = nb_hw_desc;
2451 tx_ring->tx_stats.cnt++;
2452 tx_ring->tx_stats.bytes += mbuf->pkt_len;
2454 tx_ring->next_to_use = ENA_IDX_NEXT_MASKED(next_to_use,
2455 tx_ring->size_mask);
2460 static void ena_tx_cleanup(struct ena_ring *tx_ring)
2462 unsigned int cleanup_budget;
2463 unsigned int total_tx_descs = 0;
2464 uint16_t next_to_clean = tx_ring->next_to_clean;
2466 cleanup_budget = RTE_MIN(tx_ring->ring_size / ENA_REFILL_THRESH_DIVIDER,
2467 (unsigned int)ENA_REFILL_THRESH_PACKET);
2469 while (likely(total_tx_descs < cleanup_budget)) {
2470 struct rte_mbuf *mbuf;
2471 struct ena_tx_buffer *tx_info;
2474 if (ena_com_tx_comp_req_id_get(tx_ring->ena_com_io_cq, &req_id) != 0)
2477 if (unlikely(validate_tx_req_id(tx_ring, req_id) != 0))
2480 /* Get Tx info & store how many descs were processed */
2481 tx_info = &tx_ring->tx_buffer_info[req_id];
2483 mbuf = tx_info->mbuf;
2484 rte_pktmbuf_free(mbuf);
2486 tx_info->mbuf = NULL;
2487 tx_ring->empty_tx_reqs[next_to_clean] = req_id;
2489 total_tx_descs += tx_info->tx_descs;
2491 /* Put back descriptor to the ring for reuse */
2492 next_to_clean = ENA_IDX_NEXT_MASKED(next_to_clean,
2493 tx_ring->size_mask);
2496 if (likely(total_tx_descs > 0)) {
2497 /* acknowledge completion of sent packets */
2498 tx_ring->next_to_clean = next_to_clean;
2499 ena_com_comp_ack(tx_ring->ena_com_io_sq, total_tx_descs);
2500 ena_com_update_dev_comp_head(tx_ring->ena_com_io_cq);
2504 static uint16_t eth_ena_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
2507 struct ena_ring *tx_ring = (struct ena_ring *)(tx_queue);
2508 uint16_t sent_idx = 0;
2510 #ifdef RTE_ETHDEV_DEBUG_TX
2511 /* Check adapter state */
2512 if (unlikely(tx_ring->adapter->state != ENA_ADAPTER_STATE_RUNNING)) {
2514 "Trying to xmit pkts while device is NOT running\n");
2519 for (sent_idx = 0; sent_idx < nb_pkts; sent_idx++) {
2520 if (ena_xmit_mbuf(tx_ring, tx_pkts[sent_idx]))
2522 tx_ring->pkts_without_db = true;
2523 rte_prefetch0(tx_pkts[ENA_IDX_ADD_MASKED(sent_idx, 4,
2524 tx_ring->size_mask)]);
2527 tx_ring->tx_stats.available_desc =
2528 ena_com_free_q_entries(tx_ring->ena_com_io_sq);
2530 /* If there are ready packets to be xmitted... */
2531 if (likely(tx_ring->pkts_without_db)) {
2532 /* ...let HW do its best :-) */
2533 ena_com_write_sq_doorbell(tx_ring->ena_com_io_sq);
2534 tx_ring->tx_stats.doorbells++;
2535 tx_ring->pkts_without_db = false;
2538 ena_tx_cleanup(tx_ring);
2540 tx_ring->tx_stats.available_desc =
2541 ena_com_free_q_entries(tx_ring->ena_com_io_sq);
2542 tx_ring->tx_stats.tx_poll++;
2547 int ena_copy_eni_stats(struct ena_adapter *adapter)
2549 struct ena_admin_eni_stats admin_eni_stats;
2552 rte_spinlock_lock(&adapter->admin_lock);
2553 rc = ena_com_get_eni_stats(&adapter->ena_dev, &admin_eni_stats);
2554 rte_spinlock_unlock(&adapter->admin_lock);
2556 if (rc == ENA_COM_UNSUPPORTED) {
2558 "Retrieving ENI metrics is not supported\n");
2560 PMD_DRV_LOG(WARNING,
2561 "Failed to get ENI metrics, rc: %d\n", rc);
2566 rte_memcpy(&adapter->eni_stats, &admin_eni_stats,
2567 sizeof(struct ena_stats_eni));
2573 * DPDK callback to retrieve names of extended device statistics
2576 * Pointer to Ethernet device structure.
2577 * @param[out] xstats_names
2578 * Buffer to insert names into.
2583 * Number of xstats names.
2585 static int ena_xstats_get_names(struct rte_eth_dev *dev,
2586 struct rte_eth_xstat_name *xstats_names,
2589 unsigned int xstats_count = ena_xstats_calc_num(dev->data);
2590 unsigned int stat, i, count = 0;
2592 if (n < xstats_count || !xstats_names)
2593 return xstats_count;
2595 for (stat = 0; stat < ENA_STATS_ARRAY_GLOBAL; stat++, count++)
2596 strcpy(xstats_names[count].name,
2597 ena_stats_global_strings[stat].name);
2599 for (stat = 0; stat < ENA_STATS_ARRAY_ENI; stat++, count++)
2600 strcpy(xstats_names[count].name,
2601 ena_stats_eni_strings[stat].name);
2603 for (stat = 0; stat < ENA_STATS_ARRAY_RX; stat++)
2604 for (i = 0; i < dev->data->nb_rx_queues; i++, count++)
2605 snprintf(xstats_names[count].name,
2606 sizeof(xstats_names[count].name),
2608 ena_stats_rx_strings[stat].name);
2610 for (stat = 0; stat < ENA_STATS_ARRAY_TX; stat++)
2611 for (i = 0; i < dev->data->nb_tx_queues; i++, count++)
2612 snprintf(xstats_names[count].name,
2613 sizeof(xstats_names[count].name),
2615 ena_stats_tx_strings[stat].name);
2617 return xstats_count;
2621 * DPDK callback to get extended device statistics.
2624 * Pointer to Ethernet device structure.
2626 * Stats table output buffer.
2628 * The size of the stats table.
2631 * Number of xstats on success, negative on failure.
2633 static int ena_xstats_get(struct rte_eth_dev *dev,
2634 struct rte_eth_xstat *xstats,
2637 struct ena_adapter *adapter = dev->data->dev_private;
2638 unsigned int xstats_count = ena_xstats_calc_num(dev->data);
2639 unsigned int stat, i, count = 0;
2643 if (n < xstats_count)
2644 return xstats_count;
2649 for (stat = 0; stat < ENA_STATS_ARRAY_GLOBAL; stat++, count++) {
2650 stat_offset = ena_stats_global_strings[stat].stat_offset;
2651 stats_begin = &adapter->dev_stats;
2653 xstats[count].id = count;
2654 xstats[count].value = *((uint64_t *)
2655 ((char *)stats_begin + stat_offset));
2658 /* Even if the function below fails, we should copy previous (or initial
2659 * values) to keep structure of rte_eth_xstat consistent.
2661 ena_copy_eni_stats(adapter);
2662 for (stat = 0; stat < ENA_STATS_ARRAY_ENI; stat++, count++) {
2663 stat_offset = ena_stats_eni_strings[stat].stat_offset;
2664 stats_begin = &adapter->eni_stats;
2666 xstats[count].id = count;
2667 xstats[count].value = *((uint64_t *)
2668 ((char *)stats_begin + stat_offset));
2671 for (stat = 0; stat < ENA_STATS_ARRAY_RX; stat++) {
2672 for (i = 0; i < dev->data->nb_rx_queues; i++, count++) {
2673 stat_offset = ena_stats_rx_strings[stat].stat_offset;
2674 stats_begin = &adapter->rx_ring[i].rx_stats;
2676 xstats[count].id = count;
2677 xstats[count].value = *((uint64_t *)
2678 ((char *)stats_begin + stat_offset));
2682 for (stat = 0; stat < ENA_STATS_ARRAY_TX; stat++) {
2683 for (i = 0; i < dev->data->nb_tx_queues; i++, count++) {
2684 stat_offset = ena_stats_tx_strings[stat].stat_offset;
2685 stats_begin = &adapter->tx_ring[i].rx_stats;
2687 xstats[count].id = count;
2688 xstats[count].value = *((uint64_t *)
2689 ((char *)stats_begin + stat_offset));
2696 static int ena_xstats_get_by_id(struct rte_eth_dev *dev,
2697 const uint64_t *ids,
2701 struct ena_adapter *adapter = dev->data->dev_private;
2703 uint64_t rx_entries, tx_entries;
2707 bool was_eni_copied = false;
2709 for (i = 0; i < n; ++i) {
2711 /* Check if id belongs to global statistics */
2712 if (id < ENA_STATS_ARRAY_GLOBAL) {
2713 values[i] = *((uint64_t *)&adapter->dev_stats + id);
2718 /* Check if id belongs to ENI statistics */
2719 id -= ENA_STATS_ARRAY_GLOBAL;
2720 if (id < ENA_STATS_ARRAY_ENI) {
2721 /* Avoid reading ENI stats multiple times in a single
2722 * function call, as it requires communication with the
2725 if (!was_eni_copied) {
2726 was_eni_copied = true;
2727 ena_copy_eni_stats(adapter);
2729 values[i] = *((uint64_t *)&adapter->eni_stats + id);
2734 /* Check if id belongs to rx queue statistics */
2735 id -= ENA_STATS_ARRAY_ENI;
2736 rx_entries = ENA_STATS_ARRAY_RX * dev->data->nb_rx_queues;
2737 if (id < rx_entries) {
2738 qid = id % dev->data->nb_rx_queues;
2739 id /= dev->data->nb_rx_queues;
2740 values[i] = *((uint64_t *)
2741 &adapter->rx_ring[qid].rx_stats + id);
2745 /* Check if id belongs to rx queue statistics */
2747 tx_entries = ENA_STATS_ARRAY_TX * dev->data->nb_tx_queues;
2748 if (id < tx_entries) {
2749 qid = id % dev->data->nb_tx_queues;
2750 id /= dev->data->nb_tx_queues;
2751 values[i] = *((uint64_t *)
2752 &adapter->tx_ring[qid].tx_stats + id);
2761 static int ena_process_bool_devarg(const char *key,
2765 struct ena_adapter *adapter = opaque;
2768 /* Parse the value. */
2769 if (strcmp(value, "1") == 0) {
2771 } else if (strcmp(value, "0") == 0) {
2775 "Invalid value: '%s' for key '%s'. Accepted: '0' or '1'\n",
2780 /* Now, assign it to the proper adapter field. */
2781 if (strcmp(key, ENA_DEVARG_LARGE_LLQ_HDR) == 0)
2782 adapter->use_large_llq_hdr = bool_value;
2787 static int ena_parse_devargs(struct ena_adapter *adapter,
2788 struct rte_devargs *devargs)
2790 static const char * const allowed_args[] = {
2791 ENA_DEVARG_LARGE_LLQ_HDR,
2794 struct rte_kvargs *kvlist;
2797 if (devargs == NULL)
2800 kvlist = rte_kvargs_parse(devargs->args, allowed_args);
2801 if (kvlist == NULL) {
2802 PMD_INIT_LOG(ERR, "Invalid device arguments: %s\n",
2807 rc = rte_kvargs_process(kvlist, ENA_DEVARG_LARGE_LLQ_HDR,
2808 ena_process_bool_devarg, adapter);
2810 rte_kvargs_free(kvlist);
2815 static int ena_setup_rx_intr(struct rte_eth_dev *dev)
2817 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2818 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2820 uint16_t vectors_nb, i;
2821 bool rx_intr_requested = dev->data->dev_conf.intr_conf.rxq;
2823 if (!rx_intr_requested)
2826 if (!rte_intr_cap_multiple(intr_handle)) {
2828 "Rx interrupt requested, but it isn't supported by the PCI driver\n");
2832 /* Disable interrupt mapping before the configuration starts. */
2833 rte_intr_disable(intr_handle);
2835 /* Verify if there are enough vectors available. */
2836 vectors_nb = dev->data->nb_rx_queues;
2837 if (vectors_nb > RTE_MAX_RXTX_INTR_VEC_ID) {
2839 "Too many Rx interrupts requested, maximum number: %d\n",
2840 RTE_MAX_RXTX_INTR_VEC_ID);
2845 intr_handle->intr_vec = rte_zmalloc("intr_vec",
2846 dev->data->nb_rx_queues * sizeof(*intr_handle->intr_vec), 0);
2847 if (intr_handle->intr_vec == NULL) {
2849 "Failed to allocate interrupt vector for %d queues\n",
2850 dev->data->nb_rx_queues);
2855 rc = rte_intr_efd_enable(intr_handle, vectors_nb);
2859 if (!rte_intr_allow_others(intr_handle)) {
2861 "Not enough interrupts available to use both ENA Admin and Rx interrupts\n");
2862 goto disable_intr_efd;
2865 for (i = 0; i < vectors_nb; ++i)
2866 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + i;
2868 rte_intr_enable(intr_handle);
2872 rte_intr_efd_disable(intr_handle);
2874 rte_free(intr_handle->intr_vec);
2875 intr_handle->intr_vec = NULL;
2877 rte_intr_enable(intr_handle);
2881 static void ena_rx_queue_intr_set(struct rte_eth_dev *dev,
2885 struct ena_adapter *adapter = dev->data->dev_private;
2886 struct ena_ring *rxq = &adapter->rx_ring[queue_id];
2887 struct ena_eth_io_intr_reg intr_reg;
2889 ena_com_update_intr_reg(&intr_reg, 0, 0, unmask);
2890 ena_com_unmask_intr(rxq->ena_com_io_cq, &intr_reg);
2893 static int ena_rx_queue_intr_enable(struct rte_eth_dev *dev,
2896 ena_rx_queue_intr_set(dev, queue_id, true);
2901 static int ena_rx_queue_intr_disable(struct rte_eth_dev *dev,
2904 ena_rx_queue_intr_set(dev, queue_id, false);
2909 /*********************************************************************
2911 *********************************************************************/
2912 static int eth_ena_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2913 struct rte_pci_device *pci_dev)
2915 return rte_eth_dev_pci_generic_probe(pci_dev,
2916 sizeof(struct ena_adapter), eth_ena_dev_init);
2919 static int eth_ena_pci_remove(struct rte_pci_device *pci_dev)
2921 return rte_eth_dev_pci_generic_remove(pci_dev, eth_ena_dev_uninit);
2924 static struct rte_pci_driver rte_ena_pmd = {
2925 .id_table = pci_id_ena_map,
2926 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC |
2927 RTE_PCI_DRV_WC_ACTIVATE,
2928 .probe = eth_ena_pci_probe,
2929 .remove = eth_ena_pci_remove,
2932 RTE_PMD_REGISTER_PCI(net_ena, rte_ena_pmd);
2933 RTE_PMD_REGISTER_PCI_TABLE(net_ena, pci_id_ena_map);
2934 RTE_PMD_REGISTER_KMOD_DEP(net_ena, "* igb_uio | uio_pci_generic | vfio-pci");
2935 RTE_PMD_REGISTER_PARAM_STRING(net_ena, ENA_DEVARG_LARGE_LLQ_HDR "=<0|1>");
2936 RTE_LOG_REGISTER_SUFFIX(ena_logtype_init, init, NOTICE);
2937 RTE_LOG_REGISTER_SUFFIX(ena_logtype_driver, driver, NOTICE);
2938 #ifdef RTE_ETHDEV_DEBUG_RX
2939 RTE_LOG_REGISTER_SUFFIX(ena_logtype_rx, rx, DEBUG);
2941 #ifdef RTE_ETHDEV_DEBUG_TX
2942 RTE_LOG_REGISTER_SUFFIX(ena_logtype_tx, tx, DEBUG);
2944 RTE_LOG_REGISTER_SUFFIX(ena_logtype_com, com, WARNING);
2946 /******************************************************************************
2947 ******************************** AENQ Handlers *******************************
2948 *****************************************************************************/
2949 static void ena_update_on_link_change(void *adapter_data,
2950 struct ena_admin_aenq_entry *aenq_e)
2952 struct rte_eth_dev *eth_dev = adapter_data;
2953 struct ena_adapter *adapter = eth_dev->data->dev_private;
2954 struct ena_admin_aenq_link_change_desc *aenq_link_desc;
2957 aenq_link_desc = (struct ena_admin_aenq_link_change_desc *)aenq_e;
2959 status = get_ena_admin_aenq_link_change_desc_link_status(aenq_link_desc);
2960 adapter->link_status = status;
2962 ena_link_update(eth_dev, 0);
2963 rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
2966 static void ena_notification(void *adapter_data,
2967 struct ena_admin_aenq_entry *aenq_e)
2969 struct rte_eth_dev *eth_dev = adapter_data;
2970 struct ena_adapter *adapter = eth_dev->data->dev_private;
2971 struct ena_admin_ena_hw_hints *hints;
2973 if (aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION)
2974 PMD_DRV_LOG(WARNING, "Invalid AENQ group: %x. Expected: %x\n",
2975 aenq_e->aenq_common_desc.group,
2976 ENA_ADMIN_NOTIFICATION);
2978 switch (aenq_e->aenq_common_desc.syndrome) {
2979 case ENA_ADMIN_UPDATE_HINTS:
2980 hints = (struct ena_admin_ena_hw_hints *)
2981 (&aenq_e->inline_data_w4);
2982 ena_update_hints(adapter, hints);
2985 PMD_DRV_LOG(ERR, "Invalid AENQ notification link state: %d\n",
2986 aenq_e->aenq_common_desc.syndrome);
2990 static void ena_keep_alive(void *adapter_data,
2991 __rte_unused struct ena_admin_aenq_entry *aenq_e)
2993 struct rte_eth_dev *eth_dev = adapter_data;
2994 struct ena_adapter *adapter = eth_dev->data->dev_private;
2995 struct ena_admin_aenq_keep_alive_desc *desc;
2999 adapter->timestamp_wd = rte_get_timer_cycles();
3001 desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
3002 rx_drops = ((uint64_t)desc->rx_drops_high << 32) | desc->rx_drops_low;
3003 tx_drops = ((uint64_t)desc->tx_drops_high << 32) | desc->tx_drops_low;
3005 adapter->drv_stats->rx_drops = rx_drops;
3006 adapter->dev_stats.tx_drops = tx_drops;
3010 * This handler will called for unknown event group or unimplemented handlers
3012 static void unimplemented_aenq_handler(__rte_unused void *data,
3013 __rte_unused struct ena_admin_aenq_entry *aenq_e)
3016 "Unknown event was received or event with unimplemented handler\n");
3019 static struct ena_aenq_handlers aenq_handlers = {
3021 [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
3022 [ENA_ADMIN_NOTIFICATION] = ena_notification,
3023 [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive
3025 .unimplemented_handler = unimplemented_aenq_handler