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_ethdev_driver.h>
9 #include <rte_ethdev_pci.h>
11 #include <rte_atomic.h>
13 #include <rte_errno.h>
14 #include <rte_version.h>
16 #include <rte_kvargs.h>
18 #include "ena_ethdev.h"
20 #include "ena_platform.h"
22 #include "ena_eth_com.h"
24 #include <ena_common_defs.h>
25 #include <ena_regs_defs.h>
26 #include <ena_admin_defs.h>
27 #include <ena_eth_io_defs.h>
29 #define DRV_MODULE_VER_MAJOR 2
30 #define DRV_MODULE_VER_MINOR 1
31 #define DRV_MODULE_VER_SUBMINOR 0
33 #define ENA_IO_TXQ_IDX(q) (2 * (q))
34 #define ENA_IO_RXQ_IDX(q) (2 * (q) + 1)
35 /*reverse version of ENA_IO_RXQ_IDX*/
36 #define ENA_IO_RXQ_IDX_REV(q) ((q - 1) / 2)
38 #define __MERGE_64B_H_L(h, l) (((uint64_t)h << 32) | l)
39 #define TEST_BIT(val, bit_shift) (val & (1UL << bit_shift))
41 #define GET_L4_HDR_LEN(mbuf) \
42 ((rte_pktmbuf_mtod_offset(mbuf, struct rte_tcp_hdr *, \
43 mbuf->l3_len + mbuf->l2_len)->data_off) >> 4)
45 #define ENA_RX_RSS_TABLE_LOG_SIZE 7
46 #define ENA_RX_RSS_TABLE_SIZE (1 << ENA_RX_RSS_TABLE_LOG_SIZE)
47 #define ENA_HASH_KEY_SIZE 40
48 #define ETH_GSTRING_LEN 32
50 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
52 #define ENA_MIN_RING_DESC 128
54 enum ethtool_stringset {
60 char name[ETH_GSTRING_LEN];
64 #define ENA_STAT_ENTRY(stat, stat_type) { \
66 .stat_offset = offsetof(struct ena_stats_##stat_type, stat) \
69 #define ENA_STAT_RX_ENTRY(stat) \
70 ENA_STAT_ENTRY(stat, rx)
72 #define ENA_STAT_TX_ENTRY(stat) \
73 ENA_STAT_ENTRY(stat, tx)
75 #define ENA_STAT_ENI_ENTRY(stat) \
76 ENA_STAT_ENTRY(stat, eni)
78 #define ENA_STAT_GLOBAL_ENTRY(stat) \
79 ENA_STAT_ENTRY(stat, dev)
81 /* Device arguments */
82 #define ENA_DEVARG_LARGE_LLQ_HDR "large_llq_hdr"
85 * Each rte_memzone should have unique name.
86 * To satisfy it, count number of allocation and add it to name.
88 rte_atomic32_t ena_alloc_cnt;
90 static const struct ena_stats ena_stats_global_strings[] = {
91 ENA_STAT_GLOBAL_ENTRY(wd_expired),
92 ENA_STAT_GLOBAL_ENTRY(dev_start),
93 ENA_STAT_GLOBAL_ENTRY(dev_stop),
94 ENA_STAT_GLOBAL_ENTRY(tx_drops),
97 static const struct ena_stats ena_stats_eni_strings[] = {
98 ENA_STAT_ENI_ENTRY(bw_in_allowance_exceeded),
99 ENA_STAT_ENI_ENTRY(bw_out_allowance_exceeded),
100 ENA_STAT_ENI_ENTRY(pps_allowance_exceeded),
101 ENA_STAT_ENI_ENTRY(conntrack_allowance_exceeded),
102 ENA_STAT_ENI_ENTRY(linklocal_allowance_exceeded),
105 static const struct ena_stats ena_stats_tx_strings[] = {
106 ENA_STAT_TX_ENTRY(cnt),
107 ENA_STAT_TX_ENTRY(bytes),
108 ENA_STAT_TX_ENTRY(prepare_ctx_err),
109 ENA_STAT_TX_ENTRY(linearize),
110 ENA_STAT_TX_ENTRY(linearize_failed),
111 ENA_STAT_TX_ENTRY(tx_poll),
112 ENA_STAT_TX_ENTRY(doorbells),
113 ENA_STAT_TX_ENTRY(bad_req_id),
114 ENA_STAT_TX_ENTRY(available_desc),
117 static const struct ena_stats ena_stats_rx_strings[] = {
118 ENA_STAT_RX_ENTRY(cnt),
119 ENA_STAT_RX_ENTRY(bytes),
120 ENA_STAT_RX_ENTRY(refill_partial),
121 ENA_STAT_RX_ENTRY(bad_csum),
122 ENA_STAT_RX_ENTRY(mbuf_alloc_fail),
123 ENA_STAT_RX_ENTRY(bad_desc_num),
124 ENA_STAT_RX_ENTRY(bad_req_id),
127 #define ENA_STATS_ARRAY_GLOBAL ARRAY_SIZE(ena_stats_global_strings)
128 #define ENA_STATS_ARRAY_ENI ARRAY_SIZE(ena_stats_eni_strings)
129 #define ENA_STATS_ARRAY_TX ARRAY_SIZE(ena_stats_tx_strings)
130 #define ENA_STATS_ARRAY_RX ARRAY_SIZE(ena_stats_rx_strings)
132 #define QUEUE_OFFLOADS (DEV_TX_OFFLOAD_TCP_CKSUM |\
133 DEV_TX_OFFLOAD_UDP_CKSUM |\
134 DEV_TX_OFFLOAD_IPV4_CKSUM |\
135 DEV_TX_OFFLOAD_TCP_TSO)
136 #define MBUF_OFFLOADS (PKT_TX_L4_MASK |\
140 /** Vendor ID used by Amazon devices */
141 #define PCI_VENDOR_ID_AMAZON 0x1D0F
142 /** Amazon devices */
143 #define PCI_DEVICE_ID_ENA_VF 0xEC20
144 #define PCI_DEVICE_ID_ENA_LLQ_VF 0xEC21
146 #define ENA_TX_OFFLOAD_MASK (\
153 #define ENA_TX_OFFLOAD_NOTSUP_MASK \
154 (PKT_TX_OFFLOAD_MASK ^ ENA_TX_OFFLOAD_MASK)
156 static const struct rte_pci_id pci_id_ena_map[] = {
157 { RTE_PCI_DEVICE(PCI_VENDOR_ID_AMAZON, PCI_DEVICE_ID_ENA_VF) },
158 { RTE_PCI_DEVICE(PCI_VENDOR_ID_AMAZON, PCI_DEVICE_ID_ENA_LLQ_VF) },
162 static struct ena_aenq_handlers aenq_handlers;
164 static int ena_device_init(struct ena_com_dev *ena_dev,
165 struct ena_com_dev_get_features_ctx *get_feat_ctx,
167 static int ena_dev_configure(struct rte_eth_dev *dev);
168 static void ena_tx_map_mbuf(struct ena_ring *tx_ring,
169 struct ena_tx_buffer *tx_info,
170 struct rte_mbuf *mbuf,
172 uint16_t *header_len);
173 static int ena_xmit_mbuf(struct ena_ring *tx_ring, struct rte_mbuf *mbuf);
174 static void ena_tx_cleanup(struct ena_ring *tx_ring);
175 static uint16_t eth_ena_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
177 static uint16_t eth_ena_prep_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
179 static int ena_tx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
180 uint16_t nb_desc, unsigned int socket_id,
181 const struct rte_eth_txconf *tx_conf);
182 static int ena_rx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
183 uint16_t nb_desc, unsigned int socket_id,
184 const struct rte_eth_rxconf *rx_conf,
185 struct rte_mempool *mp);
186 static inline void ena_init_rx_mbuf(struct rte_mbuf *mbuf, uint16_t len);
187 static struct rte_mbuf *ena_rx_mbuf(struct ena_ring *rx_ring,
188 struct ena_com_rx_buf_info *ena_bufs,
190 uint16_t *next_to_clean,
192 static uint16_t eth_ena_recv_pkts(void *rx_queue,
193 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
194 static int ena_add_single_rx_desc(struct ena_com_io_sq *io_sq,
195 struct rte_mbuf *mbuf, uint16_t id);
196 static int ena_populate_rx_queue(struct ena_ring *rxq, unsigned int count);
197 static void ena_init_rings(struct ena_adapter *adapter,
198 bool disable_meta_caching);
199 static int ena_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
200 static int ena_start(struct rte_eth_dev *dev);
201 static void ena_stop(struct rte_eth_dev *dev);
202 static int ena_close(struct rte_eth_dev *dev);
203 static int ena_dev_reset(struct rte_eth_dev *dev);
204 static int ena_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats);
205 static void ena_rx_queue_release_all(struct rte_eth_dev *dev);
206 static void ena_tx_queue_release_all(struct rte_eth_dev *dev);
207 static void ena_rx_queue_release(void *queue);
208 static void ena_tx_queue_release(void *queue);
209 static void ena_rx_queue_release_bufs(struct ena_ring *ring);
210 static void ena_tx_queue_release_bufs(struct ena_ring *ring);
211 static int ena_link_update(struct rte_eth_dev *dev,
212 int wait_to_complete);
213 static int ena_create_io_queue(struct ena_ring *ring);
214 static void ena_queue_stop(struct ena_ring *ring);
215 static void ena_queue_stop_all(struct rte_eth_dev *dev,
216 enum ena_ring_type ring_type);
217 static int ena_queue_start(struct ena_ring *ring);
218 static int ena_queue_start_all(struct rte_eth_dev *dev,
219 enum ena_ring_type ring_type);
220 static void ena_stats_restart(struct rte_eth_dev *dev);
221 static int ena_infos_get(struct rte_eth_dev *dev,
222 struct rte_eth_dev_info *dev_info);
223 static int ena_rss_reta_update(struct rte_eth_dev *dev,
224 struct rte_eth_rss_reta_entry64 *reta_conf,
226 static int ena_rss_reta_query(struct rte_eth_dev *dev,
227 struct rte_eth_rss_reta_entry64 *reta_conf,
229 static void ena_interrupt_handler_rte(void *cb_arg);
230 static void ena_timer_wd_callback(struct rte_timer *timer, void *arg);
231 static void ena_destroy_device(struct rte_eth_dev *eth_dev);
232 static int eth_ena_dev_init(struct rte_eth_dev *eth_dev);
233 static int ena_xstats_get_names(struct rte_eth_dev *dev,
234 struct rte_eth_xstat_name *xstats_names,
236 static int ena_xstats_get(struct rte_eth_dev *dev,
237 struct rte_eth_xstat *stats,
239 static int ena_xstats_get_by_id(struct rte_eth_dev *dev,
243 static int ena_process_bool_devarg(const char *key,
246 static int ena_parse_devargs(struct ena_adapter *adapter,
247 struct rte_devargs *devargs);
248 static int ena_copy_eni_stats(struct ena_adapter *adapter);
250 static const struct eth_dev_ops ena_dev_ops = {
251 .dev_configure = ena_dev_configure,
252 .dev_infos_get = ena_infos_get,
253 .rx_queue_setup = ena_rx_queue_setup,
254 .tx_queue_setup = ena_tx_queue_setup,
255 .dev_start = ena_start,
256 .dev_stop = ena_stop,
257 .link_update = ena_link_update,
258 .stats_get = ena_stats_get,
259 .xstats_get_names = ena_xstats_get_names,
260 .xstats_get = ena_xstats_get,
261 .xstats_get_by_id = ena_xstats_get_by_id,
262 .mtu_set = ena_mtu_set,
263 .rx_queue_release = ena_rx_queue_release,
264 .tx_queue_release = ena_tx_queue_release,
265 .dev_close = ena_close,
266 .dev_reset = ena_dev_reset,
267 .reta_update = ena_rss_reta_update,
268 .reta_query = ena_rss_reta_query,
271 void ena_rss_key_fill(void *key, size_t size)
273 static bool key_generated;
274 static uint8_t default_key[ENA_HASH_KEY_SIZE];
277 RTE_ASSERT(size <= ENA_HASH_KEY_SIZE);
279 if (!key_generated) {
280 for (i = 0; i < ENA_HASH_KEY_SIZE; ++i)
281 default_key[i] = rte_rand() & 0xff;
282 key_generated = true;
285 rte_memcpy(key, default_key, size);
288 static inline void ena_rx_mbuf_prepare(struct rte_mbuf *mbuf,
289 struct ena_com_rx_ctx *ena_rx_ctx)
291 uint64_t ol_flags = 0;
292 uint32_t packet_type = 0;
294 if (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP)
295 packet_type |= RTE_PTYPE_L4_TCP;
296 else if (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)
297 packet_type |= RTE_PTYPE_L4_UDP;
299 if (ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4)
300 packet_type |= RTE_PTYPE_L3_IPV4;
301 else if (ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV6)
302 packet_type |= RTE_PTYPE_L3_IPV6;
304 if (!ena_rx_ctx->l4_csum_checked)
305 ol_flags |= PKT_RX_L4_CKSUM_UNKNOWN;
307 if (unlikely(ena_rx_ctx->l4_csum_err) && !ena_rx_ctx->frag)
308 ol_flags |= PKT_RX_L4_CKSUM_BAD;
310 ol_flags |= PKT_RX_L4_CKSUM_UNKNOWN;
312 if (unlikely(ena_rx_ctx->l3_csum_err))
313 ol_flags |= PKT_RX_IP_CKSUM_BAD;
315 mbuf->ol_flags = ol_flags;
316 mbuf->packet_type = packet_type;
319 static inline void ena_tx_mbuf_prepare(struct rte_mbuf *mbuf,
320 struct ena_com_tx_ctx *ena_tx_ctx,
321 uint64_t queue_offloads,
322 bool disable_meta_caching)
324 struct ena_com_tx_meta *ena_meta = &ena_tx_ctx->ena_meta;
326 if ((mbuf->ol_flags & MBUF_OFFLOADS) &&
327 (queue_offloads & QUEUE_OFFLOADS)) {
328 /* check if TSO is required */
329 if ((mbuf->ol_flags & PKT_TX_TCP_SEG) &&
330 (queue_offloads & DEV_TX_OFFLOAD_TCP_TSO)) {
331 ena_tx_ctx->tso_enable = true;
333 ena_meta->l4_hdr_len = GET_L4_HDR_LEN(mbuf);
336 /* check if L3 checksum is needed */
337 if ((mbuf->ol_flags & PKT_TX_IP_CKSUM) &&
338 (queue_offloads & DEV_TX_OFFLOAD_IPV4_CKSUM))
339 ena_tx_ctx->l3_csum_enable = true;
341 if (mbuf->ol_flags & PKT_TX_IPV6) {
342 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
344 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
346 /* set don't fragment (DF) flag */
347 if (mbuf->packet_type &
348 (RTE_PTYPE_L4_NONFRAG
349 | RTE_PTYPE_INNER_L4_NONFRAG))
350 ena_tx_ctx->df = true;
353 /* check if L4 checksum is needed */
354 if (((mbuf->ol_flags & PKT_TX_L4_MASK) == PKT_TX_TCP_CKSUM) &&
355 (queue_offloads & DEV_TX_OFFLOAD_TCP_CKSUM)) {
356 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
357 ena_tx_ctx->l4_csum_enable = true;
358 } else if (((mbuf->ol_flags & PKT_TX_L4_MASK) ==
360 (queue_offloads & DEV_TX_OFFLOAD_UDP_CKSUM)) {
361 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
362 ena_tx_ctx->l4_csum_enable = true;
364 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UNKNOWN;
365 ena_tx_ctx->l4_csum_enable = false;
368 ena_meta->mss = mbuf->tso_segsz;
369 ena_meta->l3_hdr_len = mbuf->l3_len;
370 ena_meta->l3_hdr_offset = mbuf->l2_len;
372 ena_tx_ctx->meta_valid = true;
373 } else if (disable_meta_caching) {
374 memset(ena_meta, 0, sizeof(*ena_meta));
375 ena_tx_ctx->meta_valid = true;
377 ena_tx_ctx->meta_valid = false;
381 static inline int validate_rx_req_id(struct ena_ring *rx_ring, uint16_t req_id)
383 if (likely(req_id < rx_ring->ring_size))
386 PMD_DRV_LOG(ERR, "Invalid rx req_id: %hu\n", req_id);
388 rx_ring->adapter->reset_reason = ENA_REGS_RESET_INV_RX_REQ_ID;
389 rx_ring->adapter->trigger_reset = true;
390 ++rx_ring->rx_stats.bad_req_id;
395 static int validate_tx_req_id(struct ena_ring *tx_ring, u16 req_id)
397 struct ena_tx_buffer *tx_info = NULL;
399 if (likely(req_id < tx_ring->ring_size)) {
400 tx_info = &tx_ring->tx_buffer_info[req_id];
401 if (likely(tx_info->mbuf))
406 PMD_DRV_LOG(ERR, "tx_info doesn't have valid mbuf\n");
408 PMD_DRV_LOG(ERR, "Invalid req_id: %hu\n", req_id);
410 /* Trigger device reset */
411 ++tx_ring->tx_stats.bad_req_id;
412 tx_ring->adapter->reset_reason = ENA_REGS_RESET_INV_TX_REQ_ID;
413 tx_ring->adapter->trigger_reset = true;
417 static void ena_config_host_info(struct ena_com_dev *ena_dev)
419 struct ena_admin_host_info *host_info;
422 /* Allocate only the host info */
423 rc = ena_com_allocate_host_info(ena_dev);
425 PMD_DRV_LOG(ERR, "Cannot allocate host info\n");
429 host_info = ena_dev->host_attr.host_info;
431 host_info->os_type = ENA_ADMIN_OS_DPDK;
432 host_info->kernel_ver = RTE_VERSION;
433 strlcpy((char *)host_info->kernel_ver_str, rte_version(),
434 sizeof(host_info->kernel_ver_str));
435 host_info->os_dist = RTE_VERSION;
436 strlcpy((char *)host_info->os_dist_str, rte_version(),
437 sizeof(host_info->os_dist_str));
438 host_info->driver_version =
439 (DRV_MODULE_VER_MAJOR) |
440 (DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
441 (DRV_MODULE_VER_SUBMINOR <<
442 ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
443 host_info->num_cpus = rte_lcore_count();
445 host_info->driver_supported_features =
446 ENA_ADMIN_HOST_INFO_RX_OFFSET_MASK;
448 rc = ena_com_set_host_attributes(ena_dev);
450 if (rc == -ENA_COM_UNSUPPORTED)
451 PMD_DRV_LOG(WARNING, "Cannot set host attributes\n");
453 PMD_DRV_LOG(ERR, "Cannot set host attributes\n");
461 ena_com_delete_host_info(ena_dev);
464 /* This function calculates the number of xstats based on the current config */
465 static unsigned int ena_xstats_calc_num(struct rte_eth_dev *dev)
467 return ENA_STATS_ARRAY_GLOBAL + ENA_STATS_ARRAY_ENI +
468 (dev->data->nb_tx_queues * ENA_STATS_ARRAY_TX) +
469 (dev->data->nb_rx_queues * ENA_STATS_ARRAY_RX);
472 static void ena_config_debug_area(struct ena_adapter *adapter)
477 ss_count = ena_xstats_calc_num(adapter->rte_dev);
479 /* allocate 32 bytes for each string and 64bit for the value */
480 debug_area_size = ss_count * ETH_GSTRING_LEN + sizeof(u64) * ss_count;
482 rc = ena_com_allocate_debug_area(&adapter->ena_dev, debug_area_size);
484 PMD_DRV_LOG(ERR, "Cannot allocate debug area\n");
488 rc = ena_com_set_host_attributes(&adapter->ena_dev);
490 if (rc == -ENA_COM_UNSUPPORTED)
491 PMD_DRV_LOG(WARNING, "Cannot set host attributes\n");
493 PMD_DRV_LOG(ERR, "Cannot set host attributes\n");
500 ena_com_delete_debug_area(&adapter->ena_dev);
503 static int ena_close(struct rte_eth_dev *dev)
505 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
506 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
507 struct ena_adapter *adapter = dev->data->dev_private;
509 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
512 if (adapter->state == ENA_ADAPTER_STATE_RUNNING)
514 adapter->state = ENA_ADAPTER_STATE_CLOSED;
516 ena_rx_queue_release_all(dev);
517 ena_tx_queue_release_all(dev);
519 rte_free(adapter->drv_stats);
520 adapter->drv_stats = NULL;
522 rte_intr_disable(intr_handle);
523 rte_intr_callback_unregister(intr_handle,
524 ena_interrupt_handler_rte,
528 * MAC is not allocated dynamically. Setting NULL should prevent from
529 * release of the resource in the rte_eth_dev_release_port().
531 dev->data->mac_addrs = NULL;
537 ena_dev_reset(struct rte_eth_dev *dev)
541 ena_destroy_device(dev);
542 rc = eth_ena_dev_init(dev);
544 PMD_INIT_LOG(CRIT, "Cannot initialize device");
549 static int ena_rss_reta_update(struct rte_eth_dev *dev,
550 struct rte_eth_rss_reta_entry64 *reta_conf,
553 struct ena_adapter *adapter = dev->data->dev_private;
554 struct ena_com_dev *ena_dev = &adapter->ena_dev;
560 if ((reta_size == 0) || (reta_conf == NULL))
563 if (reta_size > ENA_RX_RSS_TABLE_SIZE) {
565 "indirection table %d is bigger than supported (%d)\n",
566 reta_size, ENA_RX_RSS_TABLE_SIZE);
570 for (i = 0 ; i < reta_size ; i++) {
571 /* each reta_conf is for 64 entries.
572 * to support 128 we use 2 conf of 64
574 conf_idx = i / RTE_RETA_GROUP_SIZE;
575 idx = i % RTE_RETA_GROUP_SIZE;
576 if (TEST_BIT(reta_conf[conf_idx].mask, idx)) {
578 ENA_IO_RXQ_IDX(reta_conf[conf_idx].reta[idx]);
580 rc = ena_com_indirect_table_fill_entry(ena_dev,
583 if (unlikely(rc && rc != ENA_COM_UNSUPPORTED)) {
585 "Cannot fill indirect table\n");
591 rte_spinlock_lock(&adapter->admin_lock);
592 rc = ena_com_indirect_table_set(ena_dev);
593 rte_spinlock_unlock(&adapter->admin_lock);
594 if (unlikely(rc && rc != ENA_COM_UNSUPPORTED)) {
595 PMD_DRV_LOG(ERR, "Cannot flush the indirect table\n");
599 PMD_DRV_LOG(DEBUG, "%s(): RSS configured %d entries for port %d\n",
600 __func__, reta_size, adapter->rte_dev->data->port_id);
605 /* Query redirection table. */
606 static int ena_rss_reta_query(struct rte_eth_dev *dev,
607 struct rte_eth_rss_reta_entry64 *reta_conf,
610 struct ena_adapter *adapter = dev->data->dev_private;
611 struct ena_com_dev *ena_dev = &adapter->ena_dev;
614 u32 indirect_table[ENA_RX_RSS_TABLE_SIZE] = {0};
618 if (reta_size == 0 || reta_conf == NULL ||
619 (reta_size > RTE_RETA_GROUP_SIZE && ((reta_conf + 1) == NULL)))
622 rte_spinlock_lock(&adapter->admin_lock);
623 rc = ena_com_indirect_table_get(ena_dev, indirect_table);
624 rte_spinlock_unlock(&adapter->admin_lock);
625 if (unlikely(rc && rc != ENA_COM_UNSUPPORTED)) {
626 PMD_DRV_LOG(ERR, "cannot get indirect table\n");
630 for (i = 0 ; i < reta_size ; i++) {
631 reta_conf_idx = i / RTE_RETA_GROUP_SIZE;
632 reta_idx = i % RTE_RETA_GROUP_SIZE;
633 if (TEST_BIT(reta_conf[reta_conf_idx].mask, reta_idx))
634 reta_conf[reta_conf_idx].reta[reta_idx] =
635 ENA_IO_RXQ_IDX_REV(indirect_table[i]);
641 static int ena_rss_init_default(struct ena_adapter *adapter)
643 struct ena_com_dev *ena_dev = &adapter->ena_dev;
644 uint16_t nb_rx_queues = adapter->rte_dev->data->nb_rx_queues;
648 rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
650 PMD_DRV_LOG(ERR, "Cannot init indirect table\n");
654 for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
655 val = i % nb_rx_queues;
656 rc = ena_com_indirect_table_fill_entry(ena_dev, i,
657 ENA_IO_RXQ_IDX(val));
658 if (unlikely(rc && (rc != ENA_COM_UNSUPPORTED))) {
659 PMD_DRV_LOG(ERR, "Cannot fill indirect table\n");
664 rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
665 ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
666 if (unlikely(rc && (rc != ENA_COM_UNSUPPORTED))) {
667 PMD_DRV_LOG(INFO, "Cannot fill hash function\n");
671 rc = ena_com_set_default_hash_ctrl(ena_dev);
672 if (unlikely(rc && (rc != ENA_COM_UNSUPPORTED))) {
673 PMD_DRV_LOG(INFO, "Cannot fill hash control\n");
677 rc = ena_com_indirect_table_set(ena_dev);
678 if (unlikely(rc && (rc != ENA_COM_UNSUPPORTED))) {
679 PMD_DRV_LOG(ERR, "Cannot flush the indirect table\n");
682 PMD_DRV_LOG(DEBUG, "RSS configured for port %d\n",
683 adapter->rte_dev->data->port_id);
688 ena_com_rss_destroy(ena_dev);
694 static void ena_rx_queue_release_all(struct rte_eth_dev *dev)
696 struct ena_ring **queues = (struct ena_ring **)dev->data->rx_queues;
697 int nb_queues = dev->data->nb_rx_queues;
700 for (i = 0; i < nb_queues; i++)
701 ena_rx_queue_release(queues[i]);
704 static void ena_tx_queue_release_all(struct rte_eth_dev *dev)
706 struct ena_ring **queues = (struct ena_ring **)dev->data->tx_queues;
707 int nb_queues = dev->data->nb_tx_queues;
710 for (i = 0; i < nb_queues; i++)
711 ena_tx_queue_release(queues[i]);
714 static void ena_rx_queue_release(void *queue)
716 struct ena_ring *ring = (struct ena_ring *)queue;
718 /* Free ring resources */
719 if (ring->rx_buffer_info)
720 rte_free(ring->rx_buffer_info);
721 ring->rx_buffer_info = NULL;
723 if (ring->rx_refill_buffer)
724 rte_free(ring->rx_refill_buffer);
725 ring->rx_refill_buffer = NULL;
727 if (ring->empty_rx_reqs)
728 rte_free(ring->empty_rx_reqs);
729 ring->empty_rx_reqs = NULL;
731 ring->configured = 0;
733 PMD_DRV_LOG(NOTICE, "RX Queue %d:%d released\n",
734 ring->port_id, ring->id);
737 static void ena_tx_queue_release(void *queue)
739 struct ena_ring *ring = (struct ena_ring *)queue;
741 /* Free ring resources */
742 if (ring->push_buf_intermediate_buf)
743 rte_free(ring->push_buf_intermediate_buf);
745 if (ring->tx_buffer_info)
746 rte_free(ring->tx_buffer_info);
748 if (ring->empty_tx_reqs)
749 rte_free(ring->empty_tx_reqs);
751 ring->empty_tx_reqs = NULL;
752 ring->tx_buffer_info = NULL;
753 ring->push_buf_intermediate_buf = NULL;
755 ring->configured = 0;
757 PMD_DRV_LOG(NOTICE, "TX Queue %d:%d released\n",
758 ring->port_id, ring->id);
761 static void ena_rx_queue_release_bufs(struct ena_ring *ring)
765 for (i = 0; i < ring->ring_size; ++i) {
766 struct ena_rx_buffer *rx_info = &ring->rx_buffer_info[i];
768 rte_mbuf_raw_free(rx_info->mbuf);
769 rx_info->mbuf = NULL;
774 static void ena_tx_queue_release_bufs(struct ena_ring *ring)
778 for (i = 0; i < ring->ring_size; ++i) {
779 struct ena_tx_buffer *tx_buf = &ring->tx_buffer_info[i];
782 rte_pktmbuf_free(tx_buf->mbuf);
786 static int ena_link_update(struct rte_eth_dev *dev,
787 __rte_unused int wait_to_complete)
789 struct rte_eth_link *link = &dev->data->dev_link;
790 struct ena_adapter *adapter = dev->data->dev_private;
792 link->link_status = adapter->link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
793 link->link_speed = ETH_SPEED_NUM_NONE;
794 link->link_duplex = ETH_LINK_FULL_DUPLEX;
799 static int ena_queue_start_all(struct rte_eth_dev *dev,
800 enum ena_ring_type ring_type)
802 struct ena_adapter *adapter = dev->data->dev_private;
803 struct ena_ring *queues = NULL;
808 if (ring_type == ENA_RING_TYPE_RX) {
809 queues = adapter->rx_ring;
810 nb_queues = dev->data->nb_rx_queues;
812 queues = adapter->tx_ring;
813 nb_queues = dev->data->nb_tx_queues;
815 for (i = 0; i < nb_queues; i++) {
816 if (queues[i].configured) {
817 if (ring_type == ENA_RING_TYPE_RX) {
819 dev->data->rx_queues[i] == &queues[i],
820 "Inconsistent state of rx queues\n");
823 dev->data->tx_queues[i] == &queues[i],
824 "Inconsistent state of tx queues\n");
827 rc = ena_queue_start(&queues[i]);
831 "failed to start queue %d type(%d)",
842 if (queues[i].configured)
843 ena_queue_stop(&queues[i]);
848 static uint32_t ena_get_mtu_conf(struct ena_adapter *adapter)
850 uint32_t max_frame_len = adapter->max_mtu;
852 if (adapter->rte_eth_dev_data->dev_conf.rxmode.offloads &
853 DEV_RX_OFFLOAD_JUMBO_FRAME)
855 adapter->rte_eth_dev_data->dev_conf.rxmode.max_rx_pkt_len;
857 return max_frame_len;
860 static int ena_check_valid_conf(struct ena_adapter *adapter)
862 uint32_t max_frame_len = ena_get_mtu_conf(adapter);
864 if (max_frame_len > adapter->max_mtu || max_frame_len < ENA_MIN_MTU) {
865 PMD_INIT_LOG(ERR, "Unsupported MTU of %d. "
866 "max mtu: %d, min mtu: %d",
867 max_frame_len, adapter->max_mtu, ENA_MIN_MTU);
868 return ENA_COM_UNSUPPORTED;
875 ena_calc_io_queue_size(struct ena_calc_queue_size_ctx *ctx,
876 bool use_large_llq_hdr)
878 struct ena_admin_feature_llq_desc *llq = &ctx->get_feat_ctx->llq;
879 struct ena_com_dev *ena_dev = ctx->ena_dev;
880 uint32_t max_tx_queue_size;
881 uint32_t max_rx_queue_size;
883 if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
884 struct ena_admin_queue_ext_feature_fields *max_queue_ext =
885 &ctx->get_feat_ctx->max_queue_ext.max_queue_ext;
886 max_rx_queue_size = RTE_MIN(max_queue_ext->max_rx_cq_depth,
887 max_queue_ext->max_rx_sq_depth);
888 max_tx_queue_size = max_queue_ext->max_tx_cq_depth;
890 if (ena_dev->tx_mem_queue_type ==
891 ENA_ADMIN_PLACEMENT_POLICY_DEV) {
892 max_tx_queue_size = RTE_MIN(max_tx_queue_size,
895 max_tx_queue_size = RTE_MIN(max_tx_queue_size,
896 max_queue_ext->max_tx_sq_depth);
899 ctx->max_rx_sgl_size = RTE_MIN(ENA_PKT_MAX_BUFS,
900 max_queue_ext->max_per_packet_rx_descs);
901 ctx->max_tx_sgl_size = RTE_MIN(ENA_PKT_MAX_BUFS,
902 max_queue_ext->max_per_packet_tx_descs);
904 struct ena_admin_queue_feature_desc *max_queues =
905 &ctx->get_feat_ctx->max_queues;
906 max_rx_queue_size = RTE_MIN(max_queues->max_cq_depth,
907 max_queues->max_sq_depth);
908 max_tx_queue_size = max_queues->max_cq_depth;
910 if (ena_dev->tx_mem_queue_type ==
911 ENA_ADMIN_PLACEMENT_POLICY_DEV) {
912 max_tx_queue_size = RTE_MIN(max_tx_queue_size,
915 max_tx_queue_size = RTE_MIN(max_tx_queue_size,
916 max_queues->max_sq_depth);
919 ctx->max_rx_sgl_size = RTE_MIN(ENA_PKT_MAX_BUFS,
920 max_queues->max_packet_rx_descs);
921 ctx->max_tx_sgl_size = RTE_MIN(ENA_PKT_MAX_BUFS,
922 max_queues->max_packet_tx_descs);
925 /* Round down to the nearest power of 2 */
926 max_rx_queue_size = rte_align32prevpow2(max_rx_queue_size);
927 max_tx_queue_size = rte_align32prevpow2(max_tx_queue_size);
929 if (use_large_llq_hdr) {
930 if ((llq->entry_size_ctrl_supported &
931 ENA_ADMIN_LIST_ENTRY_SIZE_256B) &&
932 (ena_dev->tx_mem_queue_type ==
933 ENA_ADMIN_PLACEMENT_POLICY_DEV)) {
934 max_tx_queue_size /= 2;
936 "Forcing large headers and decreasing maximum TX queue size to %d\n",
940 "Forcing large headers failed: LLQ is disabled or device does not support large headers\n");
944 if (unlikely(max_rx_queue_size == 0 || max_tx_queue_size == 0)) {
945 PMD_INIT_LOG(ERR, "Invalid queue size");
949 ctx->max_tx_queue_size = max_tx_queue_size;
950 ctx->max_rx_queue_size = max_rx_queue_size;
955 static void ena_stats_restart(struct rte_eth_dev *dev)
957 struct ena_adapter *adapter = dev->data->dev_private;
959 rte_atomic64_init(&adapter->drv_stats->ierrors);
960 rte_atomic64_init(&adapter->drv_stats->oerrors);
961 rte_atomic64_init(&adapter->drv_stats->rx_nombuf);
962 adapter->drv_stats->rx_drops = 0;
965 static int ena_stats_get(struct rte_eth_dev *dev,
966 struct rte_eth_stats *stats)
968 struct ena_admin_basic_stats ena_stats;
969 struct ena_adapter *adapter = dev->data->dev_private;
970 struct ena_com_dev *ena_dev = &adapter->ena_dev;
975 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
978 memset(&ena_stats, 0, sizeof(ena_stats));
980 rte_spinlock_lock(&adapter->admin_lock);
981 rc = ena_com_get_dev_basic_stats(ena_dev, &ena_stats);
982 rte_spinlock_unlock(&adapter->admin_lock);
984 PMD_DRV_LOG(ERR, "Could not retrieve statistics from ENA\n");
988 /* Set of basic statistics from ENA */
989 stats->ipackets = __MERGE_64B_H_L(ena_stats.rx_pkts_high,
990 ena_stats.rx_pkts_low);
991 stats->opackets = __MERGE_64B_H_L(ena_stats.tx_pkts_high,
992 ena_stats.tx_pkts_low);
993 stats->ibytes = __MERGE_64B_H_L(ena_stats.rx_bytes_high,
994 ena_stats.rx_bytes_low);
995 stats->obytes = __MERGE_64B_H_L(ena_stats.tx_bytes_high,
996 ena_stats.tx_bytes_low);
998 /* Driver related stats */
999 stats->imissed = adapter->drv_stats->rx_drops;
1000 stats->ierrors = rte_atomic64_read(&adapter->drv_stats->ierrors);
1001 stats->oerrors = rte_atomic64_read(&adapter->drv_stats->oerrors);
1002 stats->rx_nombuf = rte_atomic64_read(&adapter->drv_stats->rx_nombuf);
1004 max_rings_stats = RTE_MIN(dev->data->nb_rx_queues,
1005 RTE_ETHDEV_QUEUE_STAT_CNTRS);
1006 for (i = 0; i < max_rings_stats; ++i) {
1007 struct ena_stats_rx *rx_stats = &adapter->rx_ring[i].rx_stats;
1009 stats->q_ibytes[i] = rx_stats->bytes;
1010 stats->q_ipackets[i] = rx_stats->cnt;
1011 stats->q_errors[i] = rx_stats->bad_desc_num +
1012 rx_stats->bad_req_id;
1015 max_rings_stats = RTE_MIN(dev->data->nb_tx_queues,
1016 RTE_ETHDEV_QUEUE_STAT_CNTRS);
1017 for (i = 0; i < max_rings_stats; ++i) {
1018 struct ena_stats_tx *tx_stats = &adapter->tx_ring[i].tx_stats;
1020 stats->q_obytes[i] = tx_stats->bytes;
1021 stats->q_opackets[i] = tx_stats->cnt;
1027 static int ena_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1029 struct ena_adapter *adapter;
1030 struct ena_com_dev *ena_dev;
1033 ena_assert_msg(dev->data != NULL, "Uninitialized device\n");
1034 ena_assert_msg(dev->data->dev_private != NULL, "Uninitialized device\n");
1035 adapter = dev->data->dev_private;
1037 ena_dev = &adapter->ena_dev;
1038 ena_assert_msg(ena_dev != NULL, "Uninitialized device\n");
1040 if (mtu > ena_get_mtu_conf(adapter) || mtu < ENA_MIN_MTU) {
1042 "Invalid MTU setting. new_mtu: %d "
1043 "max mtu: %d min mtu: %d\n",
1044 mtu, ena_get_mtu_conf(adapter), ENA_MIN_MTU);
1048 rc = ena_com_set_dev_mtu(ena_dev, mtu);
1050 PMD_DRV_LOG(ERR, "Could not set MTU: %d\n", mtu);
1052 PMD_DRV_LOG(NOTICE, "Set MTU: %d\n", mtu);
1057 static int ena_start(struct rte_eth_dev *dev)
1059 struct ena_adapter *adapter = dev->data->dev_private;
1063 rc = ena_check_valid_conf(adapter);
1067 rc = ena_queue_start_all(dev, ENA_RING_TYPE_RX);
1071 rc = ena_queue_start_all(dev, ENA_RING_TYPE_TX);
1075 if (adapter->rte_dev->data->dev_conf.rxmode.mq_mode &
1076 ETH_MQ_RX_RSS_FLAG && adapter->rte_dev->data->nb_rx_queues > 0) {
1077 rc = ena_rss_init_default(adapter);
1082 ena_stats_restart(dev);
1084 adapter->timestamp_wd = rte_get_timer_cycles();
1085 adapter->keep_alive_timeout = ENA_DEVICE_KALIVE_TIMEOUT;
1087 ticks = rte_get_timer_hz();
1088 rte_timer_reset(&adapter->timer_wd, ticks, PERIODICAL, rte_lcore_id(),
1089 ena_timer_wd_callback, adapter);
1091 ++adapter->dev_stats.dev_start;
1092 adapter->state = ENA_ADAPTER_STATE_RUNNING;
1097 ena_queue_stop_all(dev, ENA_RING_TYPE_TX);
1099 ena_queue_stop_all(dev, ENA_RING_TYPE_RX);
1103 static void ena_stop(struct rte_eth_dev *dev)
1105 struct ena_adapter *adapter = dev->data->dev_private;
1106 struct ena_com_dev *ena_dev = &adapter->ena_dev;
1109 rte_timer_stop_sync(&adapter->timer_wd);
1110 ena_queue_stop_all(dev, ENA_RING_TYPE_TX);
1111 ena_queue_stop_all(dev, ENA_RING_TYPE_RX);
1113 if (adapter->trigger_reset) {
1114 rc = ena_com_dev_reset(ena_dev, adapter->reset_reason);
1116 PMD_DRV_LOG(ERR, "Device reset failed rc=%d\n", rc);
1119 ++adapter->dev_stats.dev_stop;
1120 adapter->state = ENA_ADAPTER_STATE_STOPPED;
1121 dev->data->dev_started = 0;
1124 static int ena_create_io_queue(struct ena_ring *ring)
1126 struct ena_adapter *adapter;
1127 struct ena_com_dev *ena_dev;
1128 struct ena_com_create_io_ctx ctx =
1129 /* policy set to _HOST just to satisfy icc compiler */
1130 { ENA_ADMIN_PLACEMENT_POLICY_HOST,
1136 adapter = ring->adapter;
1137 ena_dev = &adapter->ena_dev;
1139 if (ring->type == ENA_RING_TYPE_TX) {
1140 ena_qid = ENA_IO_TXQ_IDX(ring->id);
1141 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
1142 ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
1143 for (i = 0; i < ring->ring_size; i++)
1144 ring->empty_tx_reqs[i] = i;
1146 ena_qid = ENA_IO_RXQ_IDX(ring->id);
1147 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
1148 for (i = 0; i < ring->ring_size; i++)
1149 ring->empty_rx_reqs[i] = i;
1151 ctx.queue_size = ring->ring_size;
1153 ctx.msix_vector = -1; /* interrupts not used */
1154 ctx.numa_node = ring->numa_socket_id;
1156 rc = ena_com_create_io_queue(ena_dev, &ctx);
1159 "failed to create io queue #%d (qid:%d) rc: %d\n",
1160 ring->id, ena_qid, rc);
1164 rc = ena_com_get_io_handlers(ena_dev, ena_qid,
1165 &ring->ena_com_io_sq,
1166 &ring->ena_com_io_cq);
1169 "Failed to get io queue handlers. queue num %d rc: %d\n",
1171 ena_com_destroy_io_queue(ena_dev, ena_qid);
1175 if (ring->type == ENA_RING_TYPE_TX)
1176 ena_com_update_numa_node(ring->ena_com_io_cq, ctx.numa_node);
1181 static void ena_queue_stop(struct ena_ring *ring)
1183 struct ena_com_dev *ena_dev = &ring->adapter->ena_dev;
1185 if (ring->type == ENA_RING_TYPE_RX) {
1186 ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(ring->id));
1187 ena_rx_queue_release_bufs(ring);
1189 ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(ring->id));
1190 ena_tx_queue_release_bufs(ring);
1194 static void ena_queue_stop_all(struct rte_eth_dev *dev,
1195 enum ena_ring_type ring_type)
1197 struct ena_adapter *adapter = dev->data->dev_private;
1198 struct ena_ring *queues = NULL;
1199 uint16_t nb_queues, i;
1201 if (ring_type == ENA_RING_TYPE_RX) {
1202 queues = adapter->rx_ring;
1203 nb_queues = dev->data->nb_rx_queues;
1205 queues = adapter->tx_ring;
1206 nb_queues = dev->data->nb_tx_queues;
1209 for (i = 0; i < nb_queues; ++i)
1210 if (queues[i].configured)
1211 ena_queue_stop(&queues[i]);
1214 static int ena_queue_start(struct ena_ring *ring)
1218 ena_assert_msg(ring->configured == 1,
1219 "Trying to start unconfigured queue\n");
1221 rc = ena_create_io_queue(ring);
1223 PMD_INIT_LOG(ERR, "Failed to create IO queue!");
1227 ring->next_to_clean = 0;
1228 ring->next_to_use = 0;
1230 if (ring->type == ENA_RING_TYPE_TX) {
1231 ring->tx_stats.available_desc =
1232 ena_com_free_q_entries(ring->ena_com_io_sq);
1236 bufs_num = ring->ring_size - 1;
1237 rc = ena_populate_rx_queue(ring, bufs_num);
1238 if (rc != bufs_num) {
1239 ena_com_destroy_io_queue(&ring->adapter->ena_dev,
1240 ENA_IO_RXQ_IDX(ring->id));
1241 PMD_INIT_LOG(ERR, "Failed to populate rx ring !");
1242 return ENA_COM_FAULT;
1248 static int ena_tx_queue_setup(struct rte_eth_dev *dev,
1251 unsigned int socket_id,
1252 const struct rte_eth_txconf *tx_conf)
1254 struct ena_ring *txq = NULL;
1255 struct ena_adapter *adapter = dev->data->dev_private;
1258 txq = &adapter->tx_ring[queue_idx];
1260 if (txq->configured) {
1262 "API violation. Queue %d is already configured\n",
1264 return ENA_COM_FAULT;
1267 if (!rte_is_power_of_2(nb_desc)) {
1269 "Unsupported size of TX queue: %d is not a power of 2.\n",
1274 if (nb_desc > adapter->max_tx_ring_size) {
1276 "Unsupported size of TX queue (max size: %d)\n",
1277 adapter->max_tx_ring_size);
1281 if (nb_desc == RTE_ETH_DEV_FALLBACK_TX_RINGSIZE)
1282 nb_desc = adapter->max_tx_ring_size;
1284 txq->port_id = dev->data->port_id;
1285 txq->next_to_clean = 0;
1286 txq->next_to_use = 0;
1287 txq->ring_size = nb_desc;
1288 txq->size_mask = nb_desc - 1;
1289 txq->numa_socket_id = socket_id;
1291 txq->tx_buffer_info = rte_zmalloc("txq->tx_buffer_info",
1292 sizeof(struct ena_tx_buffer) *
1294 RTE_CACHE_LINE_SIZE);
1295 if (!txq->tx_buffer_info) {
1296 PMD_DRV_LOG(ERR, "failed to alloc mem for tx buffer info\n");
1300 txq->empty_tx_reqs = rte_zmalloc("txq->empty_tx_reqs",
1301 sizeof(u16) * txq->ring_size,
1302 RTE_CACHE_LINE_SIZE);
1303 if (!txq->empty_tx_reqs) {
1304 PMD_DRV_LOG(ERR, "failed to alloc mem for tx reqs\n");
1305 rte_free(txq->tx_buffer_info);
1309 txq->push_buf_intermediate_buf =
1310 rte_zmalloc("txq->push_buf_intermediate_buf",
1311 txq->tx_max_header_size,
1312 RTE_CACHE_LINE_SIZE);
1313 if (!txq->push_buf_intermediate_buf) {
1314 PMD_DRV_LOG(ERR, "failed to alloc push buff for LLQ\n");
1315 rte_free(txq->tx_buffer_info);
1316 rte_free(txq->empty_tx_reqs);
1320 for (i = 0; i < txq->ring_size; i++)
1321 txq->empty_tx_reqs[i] = i;
1323 if (tx_conf != NULL) {
1325 tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
1327 /* Store pointer to this queue in upper layer */
1328 txq->configured = 1;
1329 dev->data->tx_queues[queue_idx] = txq;
1334 static int ena_rx_queue_setup(struct rte_eth_dev *dev,
1337 unsigned int socket_id,
1338 __rte_unused const struct rte_eth_rxconf *rx_conf,
1339 struct rte_mempool *mp)
1341 struct ena_adapter *adapter = dev->data->dev_private;
1342 struct ena_ring *rxq = NULL;
1346 rxq = &adapter->rx_ring[queue_idx];
1347 if (rxq->configured) {
1349 "API violation. Queue %d is already configured\n",
1351 return ENA_COM_FAULT;
1354 if (nb_desc == RTE_ETH_DEV_FALLBACK_RX_RINGSIZE)
1355 nb_desc = adapter->max_rx_ring_size;
1357 if (!rte_is_power_of_2(nb_desc)) {
1359 "Unsupported size of RX queue: %d is not a power of 2.\n",
1364 if (nb_desc > adapter->max_rx_ring_size) {
1366 "Unsupported size of RX queue (max size: %d)\n",
1367 adapter->max_rx_ring_size);
1371 /* ENA isn't supporting buffers smaller than 1400 bytes */
1372 buffer_size = rte_pktmbuf_data_room_size(mp) - RTE_PKTMBUF_HEADROOM;
1373 if (buffer_size < ENA_RX_BUF_MIN_SIZE) {
1375 "Unsupported size of RX buffer: %zu (min size: %d)\n",
1376 buffer_size, ENA_RX_BUF_MIN_SIZE);
1380 rxq->port_id = dev->data->port_id;
1381 rxq->next_to_clean = 0;
1382 rxq->next_to_use = 0;
1383 rxq->ring_size = nb_desc;
1384 rxq->size_mask = nb_desc - 1;
1385 rxq->numa_socket_id = socket_id;
1388 rxq->rx_buffer_info = rte_zmalloc("rxq->buffer_info",
1389 sizeof(struct ena_rx_buffer) * nb_desc,
1390 RTE_CACHE_LINE_SIZE);
1391 if (!rxq->rx_buffer_info) {
1392 PMD_DRV_LOG(ERR, "failed to alloc mem for rx buffer info\n");
1396 rxq->rx_refill_buffer = rte_zmalloc("rxq->rx_refill_buffer",
1397 sizeof(struct rte_mbuf *) * nb_desc,
1398 RTE_CACHE_LINE_SIZE);
1400 if (!rxq->rx_refill_buffer) {
1401 PMD_DRV_LOG(ERR, "failed to alloc mem for rx refill buffer\n");
1402 rte_free(rxq->rx_buffer_info);
1403 rxq->rx_buffer_info = NULL;
1407 rxq->empty_rx_reqs = rte_zmalloc("rxq->empty_rx_reqs",
1408 sizeof(uint16_t) * nb_desc,
1409 RTE_CACHE_LINE_SIZE);
1410 if (!rxq->empty_rx_reqs) {
1411 PMD_DRV_LOG(ERR, "failed to alloc mem for empty rx reqs\n");
1412 rte_free(rxq->rx_buffer_info);
1413 rxq->rx_buffer_info = NULL;
1414 rte_free(rxq->rx_refill_buffer);
1415 rxq->rx_refill_buffer = NULL;
1419 for (i = 0; i < nb_desc; i++)
1420 rxq->empty_rx_reqs[i] = i;
1422 /* Store pointer to this queue in upper layer */
1423 rxq->configured = 1;
1424 dev->data->rx_queues[queue_idx] = rxq;
1429 static int ena_add_single_rx_desc(struct ena_com_io_sq *io_sq,
1430 struct rte_mbuf *mbuf, uint16_t id)
1432 struct ena_com_buf ebuf;
1435 /* prepare physical address for DMA transaction */
1436 ebuf.paddr = mbuf->buf_iova + RTE_PKTMBUF_HEADROOM;
1437 ebuf.len = mbuf->buf_len - RTE_PKTMBUF_HEADROOM;
1439 /* pass resource to device */
1440 rc = ena_com_add_single_rx_desc(io_sq, &ebuf, id);
1441 if (unlikely(rc != 0))
1442 PMD_DRV_LOG(WARNING, "failed adding rx desc\n");
1447 static int ena_populate_rx_queue(struct ena_ring *rxq, unsigned int count)
1451 uint16_t next_to_use = rxq->next_to_use;
1452 uint16_t in_use, req_id;
1453 struct rte_mbuf **mbufs = rxq->rx_refill_buffer;
1455 if (unlikely(!count))
1458 in_use = rxq->ring_size - 1 -
1459 ena_com_free_q_entries(rxq->ena_com_io_sq);
1460 ena_assert_msg(((in_use + count) < rxq->ring_size),
1461 "bad ring state\n");
1463 /* get resources for incoming packets */
1464 rc = rte_mempool_get_bulk(rxq->mb_pool, (void **)mbufs, count);
1465 if (unlikely(rc < 0)) {
1466 rte_atomic64_inc(&rxq->adapter->drv_stats->rx_nombuf);
1467 ++rxq->rx_stats.mbuf_alloc_fail;
1468 PMD_RX_LOG(DEBUG, "there are no enough free buffers");
1472 for (i = 0; i < count; i++) {
1473 struct rte_mbuf *mbuf = mbufs[i];
1474 struct ena_rx_buffer *rx_info;
1476 if (likely((i + 4) < count))
1477 rte_prefetch0(mbufs[i + 4]);
1479 req_id = rxq->empty_rx_reqs[next_to_use];
1480 rc = validate_rx_req_id(rxq, req_id);
1484 rx_info = &rxq->rx_buffer_info[req_id];
1486 rc = ena_add_single_rx_desc(rxq->ena_com_io_sq, mbuf, req_id);
1487 if (unlikely(rc != 0))
1490 rx_info->mbuf = mbuf;
1491 next_to_use = ENA_IDX_NEXT_MASKED(next_to_use, rxq->size_mask);
1494 if (unlikely(i < count)) {
1495 PMD_DRV_LOG(WARNING, "refilled rx qid %d with only %d "
1496 "buffers (from %d)\n", rxq->id, i, count);
1497 rte_mempool_put_bulk(rxq->mb_pool, (void **)(&mbufs[i]),
1499 ++rxq->rx_stats.refill_partial;
1502 /* When we submitted free recources to device... */
1503 if (likely(i > 0)) {
1504 /* ...let HW know that it can fill buffers with data. */
1505 ena_com_write_sq_doorbell(rxq->ena_com_io_sq);
1507 rxq->next_to_use = next_to_use;
1513 static int ena_device_init(struct ena_com_dev *ena_dev,
1514 struct ena_com_dev_get_features_ctx *get_feat_ctx,
1517 uint32_t aenq_groups;
1519 bool readless_supported;
1521 /* Initialize mmio registers */
1522 rc = ena_com_mmio_reg_read_request_init(ena_dev);
1524 PMD_DRV_LOG(ERR, "failed to init mmio read less\n");
1528 /* The PCIe configuration space revision id indicate if mmio reg
1531 readless_supported =
1532 !(((struct rte_pci_device *)ena_dev->dmadev)->id.class_id
1533 & ENA_MMIO_DISABLE_REG_READ);
1534 ena_com_set_mmio_read_mode(ena_dev, readless_supported);
1537 rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
1539 PMD_DRV_LOG(ERR, "cannot reset device\n");
1540 goto err_mmio_read_less;
1543 /* check FW version */
1544 rc = ena_com_validate_version(ena_dev);
1546 PMD_DRV_LOG(ERR, "device version is too low\n");
1547 goto err_mmio_read_less;
1550 ena_dev->dma_addr_bits = ena_com_get_dma_width(ena_dev);
1552 /* ENA device administration layer init */
1553 rc = ena_com_admin_init(ena_dev, &aenq_handlers);
1556 "cannot initialize ena admin queue with device\n");
1557 goto err_mmio_read_less;
1560 /* To enable the msix interrupts the driver needs to know the number
1561 * of queues. So the driver uses polling mode to retrieve this
1564 ena_com_set_admin_polling_mode(ena_dev, true);
1566 ena_config_host_info(ena_dev);
1568 /* Get Device Attributes and features */
1569 rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
1572 "cannot get attribute for ena device rc= %d\n", rc);
1573 goto err_admin_init;
1576 aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
1577 BIT(ENA_ADMIN_NOTIFICATION) |
1578 BIT(ENA_ADMIN_KEEP_ALIVE) |
1579 BIT(ENA_ADMIN_FATAL_ERROR) |
1580 BIT(ENA_ADMIN_WARNING);
1582 aenq_groups &= get_feat_ctx->aenq.supported_groups;
1583 rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
1585 PMD_DRV_LOG(ERR, "Cannot configure aenq groups rc: %d\n", rc);
1586 goto err_admin_init;
1589 *wd_state = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
1594 ena_com_admin_destroy(ena_dev);
1597 ena_com_mmio_reg_read_request_destroy(ena_dev);
1602 static void ena_interrupt_handler_rte(void *cb_arg)
1604 struct ena_adapter *adapter = cb_arg;
1605 struct ena_com_dev *ena_dev = &adapter->ena_dev;
1607 ena_com_admin_q_comp_intr_handler(ena_dev);
1608 if (likely(adapter->state != ENA_ADAPTER_STATE_CLOSED))
1609 ena_com_aenq_intr_handler(ena_dev, adapter);
1612 static void check_for_missing_keep_alive(struct ena_adapter *adapter)
1614 if (!adapter->wd_state)
1617 if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT)
1620 if (unlikely((rte_get_timer_cycles() - adapter->timestamp_wd) >=
1621 adapter->keep_alive_timeout)) {
1622 PMD_DRV_LOG(ERR, "Keep alive timeout\n");
1623 adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO;
1624 adapter->trigger_reset = true;
1625 ++adapter->dev_stats.wd_expired;
1629 /* Check if admin queue is enabled */
1630 static void check_for_admin_com_state(struct ena_adapter *adapter)
1632 if (unlikely(!ena_com_get_admin_running_state(&adapter->ena_dev))) {
1633 PMD_DRV_LOG(ERR, "ENA admin queue is not in running state!\n");
1634 adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO;
1635 adapter->trigger_reset = true;
1639 static void ena_timer_wd_callback(__rte_unused struct rte_timer *timer,
1642 struct ena_adapter *adapter = arg;
1643 struct rte_eth_dev *dev = adapter->rte_dev;
1645 check_for_missing_keep_alive(adapter);
1646 check_for_admin_com_state(adapter);
1648 if (unlikely(adapter->trigger_reset)) {
1649 PMD_DRV_LOG(ERR, "Trigger reset is on\n");
1650 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RESET,
1656 set_default_llq_configurations(struct ena_llq_configurations *llq_config,
1657 struct ena_admin_feature_llq_desc *llq,
1658 bool use_large_llq_hdr)
1660 llq_config->llq_header_location = ENA_ADMIN_INLINE_HEADER;
1661 llq_config->llq_stride_ctrl = ENA_ADMIN_MULTIPLE_DESCS_PER_ENTRY;
1662 llq_config->llq_num_decs_before_header =
1663 ENA_ADMIN_LLQ_NUM_DESCS_BEFORE_HEADER_2;
1665 if (use_large_llq_hdr &&
1666 (llq->entry_size_ctrl_supported & ENA_ADMIN_LIST_ENTRY_SIZE_256B)) {
1667 llq_config->llq_ring_entry_size =
1668 ENA_ADMIN_LIST_ENTRY_SIZE_256B;
1669 llq_config->llq_ring_entry_size_value = 256;
1671 llq_config->llq_ring_entry_size =
1672 ENA_ADMIN_LIST_ENTRY_SIZE_128B;
1673 llq_config->llq_ring_entry_size_value = 128;
1678 ena_set_queues_placement_policy(struct ena_adapter *adapter,
1679 struct ena_com_dev *ena_dev,
1680 struct ena_admin_feature_llq_desc *llq,
1681 struct ena_llq_configurations *llq_default_configurations)
1684 u32 llq_feature_mask;
1686 llq_feature_mask = 1 << ENA_ADMIN_LLQ;
1687 if (!(ena_dev->supported_features & llq_feature_mask)) {
1689 "LLQ is not supported. Fallback to host mode policy.\n");
1690 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
1694 rc = ena_com_config_dev_mode(ena_dev, llq, llq_default_configurations);
1696 PMD_INIT_LOG(WARNING, "Failed to config dev mode. "
1697 "Fallback to host mode policy.");
1698 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
1702 /* Nothing to config, exit */
1703 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_HOST)
1706 if (!adapter->dev_mem_base) {
1707 PMD_DRV_LOG(ERR, "Unable to access LLQ bar resource. "
1708 "Fallback to host mode policy.\n.");
1709 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
1713 ena_dev->mem_bar = adapter->dev_mem_base;
1718 static uint32_t ena_calc_max_io_queue_num(struct ena_com_dev *ena_dev,
1719 struct ena_com_dev_get_features_ctx *get_feat_ctx)
1721 uint32_t io_tx_sq_num, io_tx_cq_num, io_rx_num, max_num_io_queues;
1723 /* Regular queues capabilities */
1724 if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
1725 struct ena_admin_queue_ext_feature_fields *max_queue_ext =
1726 &get_feat_ctx->max_queue_ext.max_queue_ext;
1727 io_rx_num = RTE_MIN(max_queue_ext->max_rx_sq_num,
1728 max_queue_ext->max_rx_cq_num);
1729 io_tx_sq_num = max_queue_ext->max_tx_sq_num;
1730 io_tx_cq_num = max_queue_ext->max_tx_cq_num;
1732 struct ena_admin_queue_feature_desc *max_queues =
1733 &get_feat_ctx->max_queues;
1734 io_tx_sq_num = max_queues->max_sq_num;
1735 io_tx_cq_num = max_queues->max_cq_num;
1736 io_rx_num = RTE_MIN(io_tx_sq_num, io_tx_cq_num);
1739 /* In case of LLQ use the llq number in the get feature cmd */
1740 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
1741 io_tx_sq_num = get_feat_ctx->llq.max_llq_num;
1743 max_num_io_queues = RTE_MIN(ENA_MAX_NUM_IO_QUEUES, io_rx_num);
1744 max_num_io_queues = RTE_MIN(max_num_io_queues, io_tx_sq_num);
1745 max_num_io_queues = RTE_MIN(max_num_io_queues, io_tx_cq_num);
1747 if (unlikely(max_num_io_queues == 0)) {
1748 PMD_DRV_LOG(ERR, "Number of IO queues should not be 0\n");
1752 return max_num_io_queues;
1755 static int eth_ena_dev_init(struct rte_eth_dev *eth_dev)
1757 struct ena_calc_queue_size_ctx calc_queue_ctx = { 0 };
1758 struct rte_pci_device *pci_dev;
1759 struct rte_intr_handle *intr_handle;
1760 struct ena_adapter *adapter = eth_dev->data->dev_private;
1761 struct ena_com_dev *ena_dev = &adapter->ena_dev;
1762 struct ena_com_dev_get_features_ctx get_feat_ctx;
1763 struct ena_llq_configurations llq_config;
1764 const char *queue_type_str;
1765 uint32_t max_num_io_queues;
1767 static int adapters_found;
1768 bool disable_meta_caching;
1769 bool wd_state = false;
1771 eth_dev->dev_ops = &ena_dev_ops;
1772 eth_dev->rx_pkt_burst = ð_ena_recv_pkts;
1773 eth_dev->tx_pkt_burst = ð_ena_xmit_pkts;
1774 eth_dev->tx_pkt_prepare = ð_ena_prep_pkts;
1776 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1779 memset(adapter, 0, sizeof(struct ena_adapter));
1780 ena_dev = &adapter->ena_dev;
1782 adapter->rte_eth_dev_data = eth_dev->data;
1783 adapter->rte_dev = eth_dev;
1785 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1786 adapter->pdev = pci_dev;
1788 PMD_INIT_LOG(INFO, "Initializing %x:%x:%x.%d",
1789 pci_dev->addr.domain,
1791 pci_dev->addr.devid,
1792 pci_dev->addr.function);
1794 intr_handle = &pci_dev->intr_handle;
1796 adapter->regs = pci_dev->mem_resource[ENA_REGS_BAR].addr;
1797 adapter->dev_mem_base = pci_dev->mem_resource[ENA_MEM_BAR].addr;
1799 if (!adapter->regs) {
1800 PMD_INIT_LOG(CRIT, "Failed to access registers BAR(%d)",
1805 ena_dev->reg_bar = adapter->regs;
1806 ena_dev->dmadev = adapter->pdev;
1808 adapter->id_number = adapters_found;
1810 snprintf(adapter->name, ENA_NAME_MAX_LEN, "ena_%d",
1811 adapter->id_number);
1813 rc = ena_parse_devargs(adapter, pci_dev->device.devargs);
1815 PMD_INIT_LOG(CRIT, "Failed to parse devargs\n");
1819 /* device specific initialization routine */
1820 rc = ena_device_init(ena_dev, &get_feat_ctx, &wd_state);
1822 PMD_INIT_LOG(CRIT, "Failed to init ENA device");
1825 adapter->wd_state = wd_state;
1827 set_default_llq_configurations(&llq_config, &get_feat_ctx.llq,
1828 adapter->use_large_llq_hdr);
1829 rc = ena_set_queues_placement_policy(adapter, ena_dev,
1830 &get_feat_ctx.llq, &llq_config);
1832 PMD_INIT_LOG(CRIT, "Failed to set placement policy");
1836 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_HOST)
1837 queue_type_str = "Regular";
1839 queue_type_str = "Low latency";
1840 PMD_DRV_LOG(INFO, "Placement policy: %s\n", queue_type_str);
1842 calc_queue_ctx.ena_dev = ena_dev;
1843 calc_queue_ctx.get_feat_ctx = &get_feat_ctx;
1845 max_num_io_queues = ena_calc_max_io_queue_num(ena_dev, &get_feat_ctx);
1846 rc = ena_calc_io_queue_size(&calc_queue_ctx,
1847 adapter->use_large_llq_hdr);
1848 if (unlikely((rc != 0) || (max_num_io_queues == 0))) {
1850 goto err_device_destroy;
1853 adapter->max_tx_ring_size = calc_queue_ctx.max_tx_queue_size;
1854 adapter->max_rx_ring_size = calc_queue_ctx.max_rx_queue_size;
1855 adapter->max_tx_sgl_size = calc_queue_ctx.max_tx_sgl_size;
1856 adapter->max_rx_sgl_size = calc_queue_ctx.max_rx_sgl_size;
1857 adapter->max_num_io_queues = max_num_io_queues;
1859 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
1860 disable_meta_caching =
1861 !!(get_feat_ctx.llq.accel_mode.u.get.supported_flags &
1862 BIT(ENA_ADMIN_DISABLE_META_CACHING));
1864 disable_meta_caching = false;
1867 /* prepare ring structures */
1868 ena_init_rings(adapter, disable_meta_caching);
1870 ena_config_debug_area(adapter);
1872 /* Set max MTU for this device */
1873 adapter->max_mtu = get_feat_ctx.dev_attr.max_mtu;
1875 /* set device support for offloads */
1876 adapter->offloads.tso4_supported = (get_feat_ctx.offload.tx &
1877 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK) != 0;
1878 adapter->offloads.tx_csum_supported = (get_feat_ctx.offload.tx &
1879 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK) != 0;
1880 adapter->offloads.rx_csum_supported =
1881 (get_feat_ctx.offload.rx_supported &
1882 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK) != 0;
1884 /* Copy MAC address and point DPDK to it */
1885 eth_dev->data->mac_addrs = (struct rte_ether_addr *)adapter->mac_addr;
1886 rte_ether_addr_copy((struct rte_ether_addr *)
1887 get_feat_ctx.dev_attr.mac_addr,
1888 (struct rte_ether_addr *)adapter->mac_addr);
1890 adapter->drv_stats = rte_zmalloc("adapter stats",
1891 sizeof(*adapter->drv_stats),
1892 RTE_CACHE_LINE_SIZE);
1893 if (!adapter->drv_stats) {
1894 PMD_DRV_LOG(ERR, "failed to alloc mem for adapter stats\n");
1896 goto err_delete_debug_area;
1899 rte_spinlock_init(&adapter->admin_lock);
1901 rte_intr_callback_register(intr_handle,
1902 ena_interrupt_handler_rte,
1904 rte_intr_enable(intr_handle);
1905 ena_com_set_admin_polling_mode(ena_dev, false);
1906 ena_com_admin_aenq_enable(ena_dev);
1908 if (adapters_found == 0)
1909 rte_timer_subsystem_init();
1910 rte_timer_init(&adapter->timer_wd);
1913 adapter->state = ENA_ADAPTER_STATE_INIT;
1917 err_delete_debug_area:
1918 ena_com_delete_debug_area(ena_dev);
1921 ena_com_delete_host_info(ena_dev);
1922 ena_com_admin_destroy(ena_dev);
1928 static void ena_destroy_device(struct rte_eth_dev *eth_dev)
1930 struct ena_adapter *adapter = eth_dev->data->dev_private;
1931 struct ena_com_dev *ena_dev = &adapter->ena_dev;
1933 if (adapter->state == ENA_ADAPTER_STATE_FREE)
1936 ena_com_set_admin_running_state(ena_dev, false);
1938 if (adapter->state != ENA_ADAPTER_STATE_CLOSED)
1941 ena_com_delete_debug_area(ena_dev);
1942 ena_com_delete_host_info(ena_dev);
1944 ena_com_abort_admin_commands(ena_dev);
1945 ena_com_wait_for_abort_completion(ena_dev);
1946 ena_com_admin_destroy(ena_dev);
1947 ena_com_mmio_reg_read_request_destroy(ena_dev);
1949 adapter->state = ENA_ADAPTER_STATE_FREE;
1952 static int eth_ena_dev_uninit(struct rte_eth_dev *eth_dev)
1954 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1957 ena_destroy_device(eth_dev);
1959 eth_dev->dev_ops = NULL;
1960 eth_dev->rx_pkt_burst = NULL;
1961 eth_dev->tx_pkt_burst = NULL;
1962 eth_dev->tx_pkt_prepare = NULL;
1967 static int ena_dev_configure(struct rte_eth_dev *dev)
1969 struct ena_adapter *adapter = dev->data->dev_private;
1971 adapter->state = ENA_ADAPTER_STATE_CONFIG;
1973 adapter->tx_selected_offloads = dev->data->dev_conf.txmode.offloads;
1974 adapter->rx_selected_offloads = dev->data->dev_conf.rxmode.offloads;
1978 static void ena_init_rings(struct ena_adapter *adapter,
1979 bool disable_meta_caching)
1983 for (i = 0; i < adapter->max_num_io_queues; i++) {
1984 struct ena_ring *ring = &adapter->tx_ring[i];
1986 ring->configured = 0;
1987 ring->type = ENA_RING_TYPE_TX;
1988 ring->adapter = adapter;
1990 ring->tx_mem_queue_type = adapter->ena_dev.tx_mem_queue_type;
1991 ring->tx_max_header_size = adapter->ena_dev.tx_max_header_size;
1992 ring->sgl_size = adapter->max_tx_sgl_size;
1993 ring->disable_meta_caching = disable_meta_caching;
1996 for (i = 0; i < adapter->max_num_io_queues; i++) {
1997 struct ena_ring *ring = &adapter->rx_ring[i];
1999 ring->configured = 0;
2000 ring->type = ENA_RING_TYPE_RX;
2001 ring->adapter = adapter;
2003 ring->sgl_size = adapter->max_rx_sgl_size;
2007 static int ena_infos_get(struct rte_eth_dev *dev,
2008 struct rte_eth_dev_info *dev_info)
2010 struct ena_adapter *adapter;
2011 struct ena_com_dev *ena_dev;
2012 uint64_t rx_feat = 0, tx_feat = 0;
2014 ena_assert_msg(dev->data != NULL, "Uninitialized device\n");
2015 ena_assert_msg(dev->data->dev_private != NULL, "Uninitialized device\n");
2016 adapter = dev->data->dev_private;
2018 ena_dev = &adapter->ena_dev;
2019 ena_assert_msg(ena_dev != NULL, "Uninitialized device\n");
2021 dev_info->speed_capa =
2023 ETH_LINK_SPEED_2_5G |
2025 ETH_LINK_SPEED_10G |
2026 ETH_LINK_SPEED_25G |
2027 ETH_LINK_SPEED_40G |
2028 ETH_LINK_SPEED_50G |
2029 ETH_LINK_SPEED_100G;
2031 /* Set Tx & Rx features available for device */
2032 if (adapter->offloads.tso4_supported)
2033 tx_feat |= DEV_TX_OFFLOAD_TCP_TSO;
2035 if (adapter->offloads.tx_csum_supported)
2036 tx_feat |= DEV_TX_OFFLOAD_IPV4_CKSUM |
2037 DEV_TX_OFFLOAD_UDP_CKSUM |
2038 DEV_TX_OFFLOAD_TCP_CKSUM;
2040 if (adapter->offloads.rx_csum_supported)
2041 rx_feat |= DEV_RX_OFFLOAD_IPV4_CKSUM |
2042 DEV_RX_OFFLOAD_UDP_CKSUM |
2043 DEV_RX_OFFLOAD_TCP_CKSUM;
2045 rx_feat |= DEV_RX_OFFLOAD_JUMBO_FRAME;
2047 /* Inform framework about available features */
2048 dev_info->rx_offload_capa = rx_feat;
2049 dev_info->rx_queue_offload_capa = rx_feat;
2050 dev_info->tx_offload_capa = tx_feat;
2051 dev_info->tx_queue_offload_capa = tx_feat;
2053 dev_info->flow_type_rss_offloads = ETH_RSS_IP | ETH_RSS_TCP |
2056 dev_info->min_rx_bufsize = ENA_MIN_FRAME_LEN;
2057 dev_info->max_rx_pktlen = adapter->max_mtu;
2058 dev_info->max_mac_addrs = 1;
2060 dev_info->max_rx_queues = adapter->max_num_io_queues;
2061 dev_info->max_tx_queues = adapter->max_num_io_queues;
2062 dev_info->reta_size = ENA_RX_RSS_TABLE_SIZE;
2064 adapter->tx_supported_offloads = tx_feat;
2065 adapter->rx_supported_offloads = rx_feat;
2067 dev_info->rx_desc_lim.nb_max = adapter->max_rx_ring_size;
2068 dev_info->rx_desc_lim.nb_min = ENA_MIN_RING_DESC;
2069 dev_info->rx_desc_lim.nb_seg_max = RTE_MIN(ENA_PKT_MAX_BUFS,
2070 adapter->max_rx_sgl_size);
2071 dev_info->rx_desc_lim.nb_mtu_seg_max = RTE_MIN(ENA_PKT_MAX_BUFS,
2072 adapter->max_rx_sgl_size);
2074 dev_info->tx_desc_lim.nb_max = adapter->max_tx_ring_size;
2075 dev_info->tx_desc_lim.nb_min = ENA_MIN_RING_DESC;
2076 dev_info->tx_desc_lim.nb_seg_max = RTE_MIN(ENA_PKT_MAX_BUFS,
2077 adapter->max_tx_sgl_size);
2078 dev_info->tx_desc_lim.nb_mtu_seg_max = RTE_MIN(ENA_PKT_MAX_BUFS,
2079 adapter->max_tx_sgl_size);
2084 static inline void ena_init_rx_mbuf(struct rte_mbuf *mbuf, uint16_t len)
2086 mbuf->data_len = len;
2087 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
2092 static struct rte_mbuf *ena_rx_mbuf(struct ena_ring *rx_ring,
2093 struct ena_com_rx_buf_info *ena_bufs,
2095 uint16_t *next_to_clean,
2098 struct rte_mbuf *mbuf;
2099 struct rte_mbuf *mbuf_head;
2100 struct ena_rx_buffer *rx_info;
2102 uint16_t ntc, len, req_id, buf = 0;
2104 if (unlikely(descs == 0))
2107 ntc = *next_to_clean;
2109 len = ena_bufs[buf].len;
2110 req_id = ena_bufs[buf].req_id;
2111 if (unlikely(validate_rx_req_id(rx_ring, req_id)))
2114 rx_info = &rx_ring->rx_buffer_info[req_id];
2116 mbuf = rx_info->mbuf;
2117 RTE_ASSERT(mbuf != NULL);
2119 ena_init_rx_mbuf(mbuf, len);
2121 /* Fill the mbuf head with the data specific for 1st segment. */
2123 mbuf_head->nb_segs = descs;
2124 mbuf_head->port = rx_ring->port_id;
2125 mbuf_head->pkt_len = len;
2126 mbuf_head->data_off += offset;
2128 rx_info->mbuf = NULL;
2129 rx_ring->empty_rx_reqs[ntc] = req_id;
2130 ntc = ENA_IDX_NEXT_MASKED(ntc, rx_ring->size_mask);
2134 len = ena_bufs[buf].len;
2135 req_id = ena_bufs[buf].req_id;
2136 if (unlikely(validate_rx_req_id(rx_ring, req_id))) {
2137 rte_mbuf_raw_free(mbuf_head);
2141 rx_info = &rx_ring->rx_buffer_info[req_id];
2142 RTE_ASSERT(rx_info->mbuf != NULL);
2144 if (unlikely(len == 0)) {
2146 * Some devices can pass descriptor with the length 0.
2147 * To avoid confusion, the PMD is simply putting the
2148 * descriptor back, as it was never used. We'll avoid
2149 * mbuf allocation that way.
2151 rc = ena_add_single_rx_desc(rx_ring->ena_com_io_sq,
2152 rx_info->mbuf, req_id);
2153 if (unlikely(rc != 0)) {
2154 /* Free the mbuf in case of an error. */
2155 rte_mbuf_raw_free(rx_info->mbuf);
2158 * If there was no error, just exit the loop as
2159 * 0 length descriptor is always the last one.
2164 /* Create an mbuf chain. */
2165 mbuf->next = rx_info->mbuf;
2168 ena_init_rx_mbuf(mbuf, len);
2169 mbuf_head->pkt_len += len;
2173 * Mark the descriptor as depleted and perform necessary
2175 * This code will execute in two cases:
2176 * 1. Descriptor len was greater than 0 - normal situation.
2177 * 2. Descriptor len was 0 and we failed to add the descriptor
2178 * to the device. In that situation, we should try to add
2179 * the mbuf again in the populate routine and mark the
2180 * descriptor as used up by the device.
2182 rx_info->mbuf = NULL;
2183 rx_ring->empty_rx_reqs[ntc] = req_id;
2184 ntc = ENA_IDX_NEXT_MASKED(ntc, rx_ring->size_mask);
2187 *next_to_clean = ntc;
2192 static uint16_t eth_ena_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
2195 struct ena_ring *rx_ring = (struct ena_ring *)(rx_queue);
2196 unsigned int free_queue_entries;
2197 unsigned int refill_threshold;
2198 uint16_t next_to_clean = rx_ring->next_to_clean;
2199 uint16_t descs_in_use;
2200 struct rte_mbuf *mbuf;
2202 struct ena_com_rx_ctx ena_rx_ctx;
2205 /* Check adapter state */
2206 if (unlikely(rx_ring->adapter->state != ENA_ADAPTER_STATE_RUNNING)) {
2208 "Trying to receive pkts while device is NOT running\n");
2212 descs_in_use = rx_ring->ring_size -
2213 ena_com_free_q_entries(rx_ring->ena_com_io_sq) - 1;
2214 nb_pkts = RTE_MIN(descs_in_use, nb_pkts);
2216 for (completed = 0; completed < nb_pkts; completed++) {
2217 ena_rx_ctx.max_bufs = rx_ring->sgl_size;
2218 ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
2219 ena_rx_ctx.descs = 0;
2220 ena_rx_ctx.pkt_offset = 0;
2221 /* receive packet context */
2222 rc = ena_com_rx_pkt(rx_ring->ena_com_io_cq,
2223 rx_ring->ena_com_io_sq,
2226 PMD_DRV_LOG(ERR, "ena_com_rx_pkt error %d\n", rc);
2227 rx_ring->adapter->reset_reason =
2228 ENA_REGS_RESET_TOO_MANY_RX_DESCS;
2229 rx_ring->adapter->trigger_reset = true;
2230 ++rx_ring->rx_stats.bad_desc_num;
2234 mbuf = ena_rx_mbuf(rx_ring,
2235 ena_rx_ctx.ena_bufs,
2238 ena_rx_ctx.pkt_offset);
2239 if (unlikely(mbuf == NULL)) {
2240 for (i = 0; i < ena_rx_ctx.descs; ++i) {
2241 rx_ring->empty_rx_reqs[next_to_clean] =
2242 rx_ring->ena_bufs[i].req_id;
2243 next_to_clean = ENA_IDX_NEXT_MASKED(
2244 next_to_clean, rx_ring->size_mask);
2249 /* fill mbuf attributes if any */
2250 ena_rx_mbuf_prepare(mbuf, &ena_rx_ctx);
2252 if (unlikely(mbuf->ol_flags &
2253 (PKT_RX_IP_CKSUM_BAD | PKT_RX_L4_CKSUM_BAD))) {
2254 rte_atomic64_inc(&rx_ring->adapter->drv_stats->ierrors);
2255 ++rx_ring->rx_stats.bad_csum;
2258 mbuf->hash.rss = ena_rx_ctx.hash;
2260 rx_pkts[completed] = mbuf;
2261 rx_ring->rx_stats.bytes += mbuf->pkt_len;
2264 rx_ring->rx_stats.cnt += completed;
2265 rx_ring->next_to_clean = next_to_clean;
2267 free_queue_entries = ena_com_free_q_entries(rx_ring->ena_com_io_sq);
2269 RTE_MIN(rx_ring->ring_size / ENA_REFILL_THRESH_DIVIDER,
2270 (unsigned int)ENA_REFILL_THRESH_PACKET);
2272 /* Burst refill to save doorbells, memory barriers, const interval */
2273 if (free_queue_entries > refill_threshold) {
2274 ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq);
2275 ena_populate_rx_queue(rx_ring, free_queue_entries);
2282 eth_ena_prep_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
2288 struct ena_ring *tx_ring = (struct ena_ring *)(tx_queue);
2289 struct rte_ipv4_hdr *ip_hdr;
2291 uint16_t frag_field;
2293 for (i = 0; i != nb_pkts; i++) {
2295 ol_flags = m->ol_flags;
2297 if (!(ol_flags & PKT_TX_IPV4))
2300 /* If there was not L2 header length specified, assume it is
2301 * length of the ethernet header.
2303 if (unlikely(m->l2_len == 0))
2304 m->l2_len = sizeof(struct rte_ether_hdr);
2306 ip_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2308 frag_field = rte_be_to_cpu_16(ip_hdr->fragment_offset);
2310 if ((frag_field & RTE_IPV4_HDR_DF_FLAG) != 0) {
2311 m->packet_type |= RTE_PTYPE_L4_NONFRAG;
2313 /* If IPv4 header has DF flag enabled and TSO support is
2314 * disabled, partial chcecksum should not be calculated.
2316 if (!tx_ring->adapter->offloads.tso4_supported)
2320 if ((ol_flags & ENA_TX_OFFLOAD_NOTSUP_MASK) != 0 ||
2321 (ol_flags & PKT_TX_L4_MASK) ==
2322 PKT_TX_SCTP_CKSUM) {
2323 rte_errno = ENOTSUP;
2327 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
2328 ret = rte_validate_tx_offload(m);
2335 /* In case we are supposed to TSO and have DF not set (DF=0)
2336 * hardware must be provided with partial checksum, otherwise
2337 * it will take care of necessary calculations.
2340 ret = rte_net_intel_cksum_flags_prepare(m,
2341 ol_flags & ~PKT_TX_TCP_SEG);
2351 static void ena_update_hints(struct ena_adapter *adapter,
2352 struct ena_admin_ena_hw_hints *hints)
2354 if (hints->admin_completion_tx_timeout)
2355 adapter->ena_dev.admin_queue.completion_timeout =
2356 hints->admin_completion_tx_timeout * 1000;
2358 if (hints->mmio_read_timeout)
2359 /* convert to usec */
2360 adapter->ena_dev.mmio_read.reg_read_to =
2361 hints->mmio_read_timeout * 1000;
2363 if (hints->driver_watchdog_timeout) {
2364 if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT)
2365 adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT;
2367 // Convert msecs to ticks
2368 adapter->keep_alive_timeout =
2369 (hints->driver_watchdog_timeout *
2370 rte_get_timer_hz()) / 1000;
2374 static int ena_check_and_linearize_mbuf(struct ena_ring *tx_ring,
2375 struct rte_mbuf *mbuf)
2377 struct ena_com_dev *ena_dev;
2378 int num_segments, header_len, rc;
2380 ena_dev = &tx_ring->adapter->ena_dev;
2381 num_segments = mbuf->nb_segs;
2382 header_len = mbuf->data_len;
2384 if (likely(num_segments < tx_ring->sgl_size))
2387 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV &&
2388 (num_segments == tx_ring->sgl_size) &&
2389 (header_len < tx_ring->tx_max_header_size))
2392 ++tx_ring->tx_stats.linearize;
2393 rc = rte_pktmbuf_linearize(mbuf);
2395 PMD_DRV_LOG(WARNING, "Mbuf linearize failed\n");
2396 rte_atomic64_inc(&tx_ring->adapter->drv_stats->ierrors);
2397 ++tx_ring->tx_stats.linearize_failed;
2404 static void ena_tx_map_mbuf(struct ena_ring *tx_ring,
2405 struct ena_tx_buffer *tx_info,
2406 struct rte_mbuf *mbuf,
2408 uint16_t *header_len)
2410 struct ena_com_buf *ena_buf;
2411 uint16_t delta, seg_len, push_len;
2414 seg_len = mbuf->data_len;
2416 tx_info->mbuf = mbuf;
2417 ena_buf = tx_info->bufs;
2419 if (tx_ring->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
2421 * Tx header might be (and will be in most cases) smaller than
2422 * tx_max_header_size. But it's not an issue to send more data
2423 * to the device, than actually needed if the mbuf size is
2424 * greater than tx_max_header_size.
2426 push_len = RTE_MIN(mbuf->pkt_len, tx_ring->tx_max_header_size);
2427 *header_len = push_len;
2429 if (likely(push_len <= seg_len)) {
2430 /* If the push header is in the single segment, then
2431 * just point it to the 1st mbuf data.
2433 *push_header = rte_pktmbuf_mtod(mbuf, uint8_t *);
2435 /* If the push header lays in the several segments, copy
2436 * it to the intermediate buffer.
2438 rte_pktmbuf_read(mbuf, 0, push_len,
2439 tx_ring->push_buf_intermediate_buf);
2440 *push_header = tx_ring->push_buf_intermediate_buf;
2441 delta = push_len - seg_len;
2444 *push_header = NULL;
2449 /* Process first segment taking into consideration pushed header */
2450 if (seg_len > push_len) {
2451 ena_buf->paddr = mbuf->buf_iova +
2454 ena_buf->len = seg_len - push_len;
2456 tx_info->num_of_bufs++;
2459 while ((mbuf = mbuf->next) != NULL) {
2460 seg_len = mbuf->data_len;
2462 /* Skip mbufs if whole data is pushed as a header */
2463 if (unlikely(delta > seg_len)) {
2468 ena_buf->paddr = mbuf->buf_iova + mbuf->data_off + delta;
2469 ena_buf->len = seg_len - delta;
2471 tx_info->num_of_bufs++;
2477 static int ena_xmit_mbuf(struct ena_ring *tx_ring, struct rte_mbuf *mbuf)
2479 struct ena_tx_buffer *tx_info;
2480 struct ena_com_tx_ctx ena_tx_ctx = { { 0 } };
2481 uint16_t next_to_use;
2482 uint16_t header_len;
2488 rc = ena_check_and_linearize_mbuf(tx_ring, mbuf);
2492 next_to_use = tx_ring->next_to_use;
2494 req_id = tx_ring->empty_tx_reqs[next_to_use];
2495 tx_info = &tx_ring->tx_buffer_info[req_id];
2496 tx_info->num_of_bufs = 0;
2498 ena_tx_map_mbuf(tx_ring, tx_info, mbuf, &push_header, &header_len);
2500 ena_tx_ctx.ena_bufs = tx_info->bufs;
2501 ena_tx_ctx.push_header = push_header;
2502 ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
2503 ena_tx_ctx.req_id = req_id;
2504 ena_tx_ctx.header_len = header_len;
2506 /* Set Tx offloads flags, if applicable */
2507 ena_tx_mbuf_prepare(mbuf, &ena_tx_ctx, tx_ring->offloads,
2508 tx_ring->disable_meta_caching);
2510 if (unlikely(ena_com_is_doorbell_needed(tx_ring->ena_com_io_sq,
2513 "llq tx max burst size of queue %d achieved, writing doorbell to send burst\n",
2515 ena_com_write_sq_doorbell(tx_ring->ena_com_io_sq);
2518 /* prepare the packet's descriptors to dma engine */
2519 rc = ena_com_prepare_tx(tx_ring->ena_com_io_sq, &ena_tx_ctx,
2522 ++tx_ring->tx_stats.prepare_ctx_err;
2526 tx_info->tx_descs = nb_hw_desc;
2528 tx_ring->tx_stats.cnt++;
2529 tx_ring->tx_stats.bytes += mbuf->pkt_len;
2531 tx_ring->next_to_use = ENA_IDX_NEXT_MASKED(next_to_use,
2532 tx_ring->size_mask);
2537 static void ena_tx_cleanup(struct ena_ring *tx_ring)
2539 unsigned int cleanup_budget;
2540 unsigned int total_tx_descs = 0;
2541 uint16_t next_to_clean = tx_ring->next_to_clean;
2543 cleanup_budget = RTE_MIN(tx_ring->ring_size / ENA_REFILL_THRESH_DIVIDER,
2544 (unsigned int)ENA_REFILL_THRESH_PACKET);
2546 while (likely(total_tx_descs < cleanup_budget)) {
2547 struct rte_mbuf *mbuf;
2548 struct ena_tx_buffer *tx_info;
2551 if (ena_com_tx_comp_req_id_get(tx_ring->ena_com_io_cq, &req_id) != 0)
2554 if (unlikely(validate_tx_req_id(tx_ring, req_id) != 0))
2557 /* Get Tx info & store how many descs were processed */
2558 tx_info = &tx_ring->tx_buffer_info[req_id];
2560 mbuf = tx_info->mbuf;
2561 rte_pktmbuf_free(mbuf);
2563 tx_info->mbuf = NULL;
2564 tx_ring->empty_tx_reqs[next_to_clean] = req_id;
2566 total_tx_descs += tx_info->tx_descs;
2568 /* Put back descriptor to the ring for reuse */
2569 next_to_clean = ENA_IDX_NEXT_MASKED(next_to_clean,
2570 tx_ring->size_mask);
2573 if (likely(total_tx_descs > 0)) {
2574 /* acknowledge completion of sent packets */
2575 tx_ring->next_to_clean = next_to_clean;
2576 ena_com_comp_ack(tx_ring->ena_com_io_sq, total_tx_descs);
2577 ena_com_update_dev_comp_head(tx_ring->ena_com_io_cq);
2581 static uint16_t eth_ena_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
2584 struct ena_ring *tx_ring = (struct ena_ring *)(tx_queue);
2585 uint16_t sent_idx = 0;
2587 /* Check adapter state */
2588 if (unlikely(tx_ring->adapter->state != ENA_ADAPTER_STATE_RUNNING)) {
2590 "Trying to xmit pkts while device is NOT running\n");
2594 nb_pkts = RTE_MIN(ena_com_free_q_entries(tx_ring->ena_com_io_sq),
2597 for (sent_idx = 0; sent_idx < nb_pkts; sent_idx++) {
2598 if (ena_xmit_mbuf(tx_ring, tx_pkts[sent_idx]))
2601 rte_prefetch0(tx_pkts[ENA_IDX_ADD_MASKED(sent_idx, 4,
2602 tx_ring->size_mask)]);
2605 tx_ring->tx_stats.available_desc =
2606 ena_com_free_q_entries(tx_ring->ena_com_io_sq);
2608 /* If there are ready packets to be xmitted... */
2610 /* ...let HW do its best :-) */
2611 ena_com_write_sq_doorbell(tx_ring->ena_com_io_sq);
2612 tx_ring->tx_stats.doorbells++;
2615 ena_tx_cleanup(tx_ring);
2617 tx_ring->tx_stats.available_desc =
2618 ena_com_free_q_entries(tx_ring->ena_com_io_sq);
2619 tx_ring->tx_stats.tx_poll++;
2624 int ena_copy_eni_stats(struct ena_adapter *adapter)
2626 struct ena_admin_eni_stats admin_eni_stats;
2629 rte_spinlock_lock(&adapter->admin_lock);
2630 rc = ena_com_get_eni_stats(&adapter->ena_dev, &admin_eni_stats);
2631 rte_spinlock_unlock(&adapter->admin_lock);
2633 if (rc == ENA_COM_UNSUPPORTED) {
2635 "Retrieving ENI metrics is not supported.\n");
2637 PMD_DRV_LOG(WARNING,
2638 "Failed to get ENI metrics: %d\n", rc);
2643 rte_memcpy(&adapter->eni_stats, &admin_eni_stats,
2644 sizeof(struct ena_stats_eni));
2650 * DPDK callback to retrieve names of extended device statistics
2653 * Pointer to Ethernet device structure.
2654 * @param[out] xstats_names
2655 * Buffer to insert names into.
2660 * Number of xstats names.
2662 static int ena_xstats_get_names(struct rte_eth_dev *dev,
2663 struct rte_eth_xstat_name *xstats_names,
2666 unsigned int xstats_count = ena_xstats_calc_num(dev);
2667 unsigned int stat, i, count = 0;
2669 if (n < xstats_count || !xstats_names)
2670 return xstats_count;
2672 for (stat = 0; stat < ENA_STATS_ARRAY_GLOBAL; stat++, count++)
2673 strcpy(xstats_names[count].name,
2674 ena_stats_global_strings[stat].name);
2676 for (stat = 0; stat < ENA_STATS_ARRAY_ENI; stat++, count++)
2677 strcpy(xstats_names[count].name,
2678 ena_stats_eni_strings[stat].name);
2680 for (stat = 0; stat < ENA_STATS_ARRAY_RX; stat++)
2681 for (i = 0; i < dev->data->nb_rx_queues; i++, count++)
2682 snprintf(xstats_names[count].name,
2683 sizeof(xstats_names[count].name),
2685 ena_stats_rx_strings[stat].name);
2687 for (stat = 0; stat < ENA_STATS_ARRAY_TX; stat++)
2688 for (i = 0; i < dev->data->nb_tx_queues; i++, count++)
2689 snprintf(xstats_names[count].name,
2690 sizeof(xstats_names[count].name),
2692 ena_stats_tx_strings[stat].name);
2694 return xstats_count;
2698 * DPDK callback to get extended device statistics.
2701 * Pointer to Ethernet device structure.
2703 * Stats table output buffer.
2705 * The size of the stats table.
2708 * Number of xstats on success, negative on failure.
2710 static int ena_xstats_get(struct rte_eth_dev *dev,
2711 struct rte_eth_xstat *xstats,
2714 struct ena_adapter *adapter = dev->data->dev_private;
2715 unsigned int xstats_count = ena_xstats_calc_num(dev);
2716 unsigned int stat, i, count = 0;
2720 if (n < xstats_count)
2721 return xstats_count;
2726 for (stat = 0; stat < ENA_STATS_ARRAY_GLOBAL; stat++, count++) {
2727 stat_offset = ena_stats_rx_strings[stat].stat_offset;
2728 stats_begin = &adapter->dev_stats;
2730 xstats[count].id = count;
2731 xstats[count].value = *((uint64_t *)
2732 ((char *)stats_begin + stat_offset));
2735 /* Even if the function below fails, we should copy previous (or initial
2736 * values) to keep structure of rte_eth_xstat consistent.
2738 ena_copy_eni_stats(adapter);
2739 for (stat = 0; stat < ENA_STATS_ARRAY_ENI; stat++, count++) {
2740 stat_offset = ena_stats_eni_strings[stat].stat_offset;
2741 stats_begin = &adapter->eni_stats;
2743 xstats[count].id = count;
2744 xstats[count].value = *((uint64_t *)
2745 ((char *)stats_begin + stat_offset));
2748 for (stat = 0; stat < ENA_STATS_ARRAY_RX; stat++) {
2749 for (i = 0; i < dev->data->nb_rx_queues; i++, count++) {
2750 stat_offset = ena_stats_rx_strings[stat].stat_offset;
2751 stats_begin = &adapter->rx_ring[i].rx_stats;
2753 xstats[count].id = count;
2754 xstats[count].value = *((uint64_t *)
2755 ((char *)stats_begin + stat_offset));
2759 for (stat = 0; stat < ENA_STATS_ARRAY_TX; stat++) {
2760 for (i = 0; i < dev->data->nb_tx_queues; i++, count++) {
2761 stat_offset = ena_stats_tx_strings[stat].stat_offset;
2762 stats_begin = &adapter->tx_ring[i].rx_stats;
2764 xstats[count].id = count;
2765 xstats[count].value = *((uint64_t *)
2766 ((char *)stats_begin + stat_offset));
2773 static int ena_xstats_get_by_id(struct rte_eth_dev *dev,
2774 const uint64_t *ids,
2778 struct ena_adapter *adapter = dev->data->dev_private;
2780 uint64_t rx_entries, tx_entries;
2784 bool was_eni_copied = false;
2786 for (i = 0; i < n; ++i) {
2788 /* Check if id belongs to global statistics */
2789 if (id < ENA_STATS_ARRAY_GLOBAL) {
2790 values[i] = *((uint64_t *)&adapter->dev_stats + id);
2795 /* Check if id belongs to ENI statistics */
2796 id -= ENA_STATS_ARRAY_GLOBAL;
2797 if (id < ENA_STATS_ARRAY_ENI) {
2798 /* Avoid reading ENI stats multiple times in a single
2799 * function call, as it requires communication with the
2802 if (!was_eni_copied) {
2803 was_eni_copied = true;
2804 ena_copy_eni_stats(adapter);
2806 values[i] = *((uint64_t *)&adapter->eni_stats + id);
2811 /* Check if id belongs to rx queue statistics */
2812 id -= ENA_STATS_ARRAY_ENI;
2813 rx_entries = ENA_STATS_ARRAY_RX * dev->data->nb_rx_queues;
2814 if (id < rx_entries) {
2815 qid = id % dev->data->nb_rx_queues;
2816 id /= dev->data->nb_rx_queues;
2817 values[i] = *((uint64_t *)
2818 &adapter->rx_ring[qid].rx_stats + id);
2822 /* Check if id belongs to rx queue statistics */
2824 tx_entries = ENA_STATS_ARRAY_TX * dev->data->nb_tx_queues;
2825 if (id < tx_entries) {
2826 qid = id % dev->data->nb_tx_queues;
2827 id /= dev->data->nb_tx_queues;
2828 values[i] = *((uint64_t *)
2829 &adapter->tx_ring[qid].tx_stats + id);
2838 static int ena_process_bool_devarg(const char *key,
2842 struct ena_adapter *adapter = opaque;
2845 /* Parse the value. */
2846 if (strcmp(value, "1") == 0) {
2848 } else if (strcmp(value, "0") == 0) {
2852 "Invalid value: '%s' for key '%s'. Accepted: '0' or '1'\n",
2857 /* Now, assign it to the proper adapter field. */
2858 if (strcmp(key, ENA_DEVARG_LARGE_LLQ_HDR))
2859 adapter->use_large_llq_hdr = bool_value;
2864 static int ena_parse_devargs(struct ena_adapter *adapter,
2865 struct rte_devargs *devargs)
2867 static const char * const allowed_args[] = {
2868 ENA_DEVARG_LARGE_LLQ_HDR,
2870 struct rte_kvargs *kvlist;
2873 if (devargs == NULL)
2876 kvlist = rte_kvargs_parse(devargs->args, allowed_args);
2877 if (kvlist == NULL) {
2878 PMD_INIT_LOG(ERR, "Invalid device arguments: %s\n",
2883 rc = rte_kvargs_process(kvlist, ENA_DEVARG_LARGE_LLQ_HDR,
2884 ena_process_bool_devarg, adapter);
2886 rte_kvargs_free(kvlist);
2891 /*********************************************************************
2893 *********************************************************************/
2894 static int eth_ena_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2895 struct rte_pci_device *pci_dev)
2897 return rte_eth_dev_pci_generic_probe(pci_dev,
2898 sizeof(struct ena_adapter), eth_ena_dev_init);
2901 static int eth_ena_pci_remove(struct rte_pci_device *pci_dev)
2903 return rte_eth_dev_pci_generic_remove(pci_dev, eth_ena_dev_uninit);
2906 static struct rte_pci_driver rte_ena_pmd = {
2907 .id_table = pci_id_ena_map,
2908 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC |
2909 RTE_PCI_DRV_WC_ACTIVATE,
2910 .probe = eth_ena_pci_probe,
2911 .remove = eth_ena_pci_remove,
2914 RTE_PMD_REGISTER_PCI(net_ena, rte_ena_pmd);
2915 RTE_PMD_REGISTER_PCI_TABLE(net_ena, pci_id_ena_map);
2916 RTE_PMD_REGISTER_KMOD_DEP(net_ena, "* igb_uio | uio_pci_generic | vfio-pci");
2917 RTE_PMD_REGISTER_PARAM_STRING(net_ena, ENA_DEVARG_LARGE_LLQ_HDR "=<0|1>");
2918 RTE_LOG_REGISTER(ena_logtype_init, pmd.net.ena.init, NOTICE);
2919 RTE_LOG_REGISTER(ena_logtype_driver, pmd.net.ena.driver, NOTICE);
2920 #ifdef RTE_LIBRTE_ENA_DEBUG_RX
2921 RTE_LOG_REGISTER(ena_logtype_rx, pmd.net.ena.rx, NOTICE);
2923 #ifdef RTE_LIBRTE_ENA_DEBUG_TX
2924 RTE_LOG_REGISTER(ena_logtype_tx, pmd.net.ena.tx, NOTICE);
2926 #ifdef RTE_LIBRTE_ENA_DEBUG_TX_FREE
2927 RTE_LOG_REGISTER(ena_logtype_tx_free, pmd.net.ena.tx_free, NOTICE);
2929 #ifdef RTE_LIBRTE_ENA_COM_DEBUG
2930 RTE_LOG_REGISTER(ena_logtype_com, pmd.net.ena.com, NOTICE);
2933 /******************************************************************************
2934 ******************************** AENQ Handlers *******************************
2935 *****************************************************************************/
2936 static void ena_update_on_link_change(void *adapter_data,
2937 struct ena_admin_aenq_entry *aenq_e)
2939 struct rte_eth_dev *eth_dev;
2940 struct ena_adapter *adapter;
2941 struct ena_admin_aenq_link_change_desc *aenq_link_desc;
2944 adapter = adapter_data;
2945 aenq_link_desc = (struct ena_admin_aenq_link_change_desc *)aenq_e;
2946 eth_dev = adapter->rte_dev;
2948 status = get_ena_admin_aenq_link_change_desc_link_status(aenq_link_desc);
2949 adapter->link_status = status;
2951 ena_link_update(eth_dev, 0);
2952 rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
2955 static void ena_notification(void *data,
2956 struct ena_admin_aenq_entry *aenq_e)
2958 struct ena_adapter *adapter = data;
2959 struct ena_admin_ena_hw_hints *hints;
2961 if (aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION)
2962 PMD_DRV_LOG(WARNING, "Invalid group(%x) expected %x\n",
2963 aenq_e->aenq_common_desc.group,
2964 ENA_ADMIN_NOTIFICATION);
2966 switch (aenq_e->aenq_common_desc.syndrom) {
2967 case ENA_ADMIN_UPDATE_HINTS:
2968 hints = (struct ena_admin_ena_hw_hints *)
2969 (&aenq_e->inline_data_w4);
2970 ena_update_hints(adapter, hints);
2973 PMD_DRV_LOG(ERR, "Invalid aenq notification link state %d\n",
2974 aenq_e->aenq_common_desc.syndrom);
2978 static void ena_keep_alive(void *adapter_data,
2979 __rte_unused struct ena_admin_aenq_entry *aenq_e)
2981 struct ena_adapter *adapter = adapter_data;
2982 struct ena_admin_aenq_keep_alive_desc *desc;
2986 adapter->timestamp_wd = rte_get_timer_cycles();
2988 desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
2989 rx_drops = ((uint64_t)desc->rx_drops_high << 32) | desc->rx_drops_low;
2990 tx_drops = ((uint64_t)desc->tx_drops_high << 32) | desc->tx_drops_low;
2992 adapter->drv_stats->rx_drops = rx_drops;
2993 adapter->dev_stats.tx_drops = tx_drops;
2997 * This handler will called for unknown event group or unimplemented handlers
2999 static void unimplemented_aenq_handler(__rte_unused void *data,
3000 __rte_unused struct ena_admin_aenq_entry *aenq_e)
3002 PMD_DRV_LOG(ERR, "Unknown event was received or event with "
3003 "unimplemented handler\n");
3006 static struct ena_aenq_handlers aenq_handlers = {
3008 [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
3009 [ENA_ADMIN_NOTIFICATION] = ena_notification,
3010 [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive
3012 .unimplemented_handler = unimplemented_aenq_handler