4 * Copyright (c) 2015-2016 Amazon.com, Inc. or its affiliates.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <rte_ether.h>
35 #include <rte_ethdev_driver.h>
36 #include <rte_ethdev_pci.h>
38 #include <rte_atomic.h>
40 #include <rte_errno.h>
41 #include <rte_version.h>
42 #include <rte_eal_memconfig.h>
45 #include "ena_ethdev.h"
47 #include "ena_platform.h"
49 #include "ena_eth_com.h"
51 #include <ena_common_defs.h>
52 #include <ena_regs_defs.h>
53 #include <ena_admin_defs.h>
54 #include <ena_eth_io_defs.h>
56 #define DRV_MODULE_VER_MAJOR 1
57 #define DRV_MODULE_VER_MINOR 1
58 #define DRV_MODULE_VER_SUBMINOR 0
60 #define ENA_IO_TXQ_IDX(q) (2 * (q))
61 #define ENA_IO_RXQ_IDX(q) (2 * (q) + 1)
62 /*reverse version of ENA_IO_RXQ_IDX*/
63 #define ENA_IO_RXQ_IDX_REV(q) ((q - 1) / 2)
65 /* While processing submitted and completed descriptors (rx and tx path
66 * respectively) in a loop it is desired to:
67 * - perform batch submissions while populating sumbissmion queue
68 * - avoid blocking transmission of other packets during cleanup phase
69 * Hence the utilization ratio of 1/8 of a queue size.
71 #define ENA_RING_DESCS_RATIO(ring_size) (ring_size / 8)
73 #define __MERGE_64B_H_L(h, l) (((uint64_t)h << 32) | l)
74 #define TEST_BIT(val, bit_shift) (val & (1UL << bit_shift))
76 #define GET_L4_HDR_LEN(mbuf) \
77 ((rte_pktmbuf_mtod_offset(mbuf, struct tcp_hdr *, \
78 mbuf->l3_len + mbuf->l2_len)->data_off) >> 4)
80 #define ENA_RX_RSS_TABLE_LOG_SIZE 7
81 #define ENA_RX_RSS_TABLE_SIZE (1 << ENA_RX_RSS_TABLE_LOG_SIZE)
82 #define ENA_HASH_KEY_SIZE 40
83 #define ENA_ETH_SS_STATS 0xFF
84 #define ETH_GSTRING_LEN 32
86 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
88 #define ENA_MAX_RING_DESC ENA_DEFAULT_RING_SIZE
89 #define ENA_MIN_RING_DESC 128
91 enum ethtool_stringset {
97 char name[ETH_GSTRING_LEN];
101 #define ENA_STAT_ENA_COM_ENTRY(stat) { \
103 .stat_offset = offsetof(struct ena_com_stats_admin, stat) \
106 #define ENA_STAT_ENTRY(stat, stat_type) { \
108 .stat_offset = offsetof(struct ena_stats_##stat_type, stat) \
111 #define ENA_STAT_RX_ENTRY(stat) \
112 ENA_STAT_ENTRY(stat, rx)
114 #define ENA_STAT_TX_ENTRY(stat) \
115 ENA_STAT_ENTRY(stat, tx)
117 #define ENA_STAT_GLOBAL_ENTRY(stat) \
118 ENA_STAT_ENTRY(stat, dev)
121 * Each rte_memzone should have unique name.
122 * To satisfy it, count number of allocation and add it to name.
124 uint32_t ena_alloc_cnt;
126 static const struct ena_stats ena_stats_global_strings[] = {
127 ENA_STAT_GLOBAL_ENTRY(tx_timeout),
128 ENA_STAT_GLOBAL_ENTRY(io_suspend),
129 ENA_STAT_GLOBAL_ENTRY(io_resume),
130 ENA_STAT_GLOBAL_ENTRY(wd_expired),
131 ENA_STAT_GLOBAL_ENTRY(interface_up),
132 ENA_STAT_GLOBAL_ENTRY(interface_down),
133 ENA_STAT_GLOBAL_ENTRY(admin_q_pause),
136 static const struct ena_stats ena_stats_tx_strings[] = {
137 ENA_STAT_TX_ENTRY(cnt),
138 ENA_STAT_TX_ENTRY(bytes),
139 ENA_STAT_TX_ENTRY(queue_stop),
140 ENA_STAT_TX_ENTRY(queue_wakeup),
141 ENA_STAT_TX_ENTRY(dma_mapping_err),
142 ENA_STAT_TX_ENTRY(linearize),
143 ENA_STAT_TX_ENTRY(linearize_failed),
144 ENA_STAT_TX_ENTRY(tx_poll),
145 ENA_STAT_TX_ENTRY(doorbells),
146 ENA_STAT_TX_ENTRY(prepare_ctx_err),
147 ENA_STAT_TX_ENTRY(missing_tx_comp),
148 ENA_STAT_TX_ENTRY(bad_req_id),
151 static const struct ena_stats ena_stats_rx_strings[] = {
152 ENA_STAT_RX_ENTRY(cnt),
153 ENA_STAT_RX_ENTRY(bytes),
154 ENA_STAT_RX_ENTRY(refil_partial),
155 ENA_STAT_RX_ENTRY(bad_csum),
156 ENA_STAT_RX_ENTRY(page_alloc_fail),
157 ENA_STAT_RX_ENTRY(skb_alloc_fail),
158 ENA_STAT_RX_ENTRY(dma_mapping_err),
159 ENA_STAT_RX_ENTRY(bad_desc_num),
160 ENA_STAT_RX_ENTRY(small_copy_len_pkt),
163 static const struct ena_stats ena_stats_ena_com_strings[] = {
164 ENA_STAT_ENA_COM_ENTRY(aborted_cmd),
165 ENA_STAT_ENA_COM_ENTRY(submitted_cmd),
166 ENA_STAT_ENA_COM_ENTRY(completed_cmd),
167 ENA_STAT_ENA_COM_ENTRY(out_of_space),
168 ENA_STAT_ENA_COM_ENTRY(no_completion),
171 #define ENA_STATS_ARRAY_GLOBAL ARRAY_SIZE(ena_stats_global_strings)
172 #define ENA_STATS_ARRAY_TX ARRAY_SIZE(ena_stats_tx_strings)
173 #define ENA_STATS_ARRAY_RX ARRAY_SIZE(ena_stats_rx_strings)
174 #define ENA_STATS_ARRAY_ENA_COM ARRAY_SIZE(ena_stats_ena_com_strings)
176 #define QUEUE_OFFLOADS (DEV_TX_OFFLOAD_TCP_CKSUM |\
177 DEV_TX_OFFLOAD_UDP_CKSUM |\
178 DEV_TX_OFFLOAD_IPV4_CKSUM |\
179 DEV_TX_OFFLOAD_TCP_TSO)
180 #define MBUF_OFFLOADS (PKT_TX_L4_MASK |\
184 /** Vendor ID used by Amazon devices */
185 #define PCI_VENDOR_ID_AMAZON 0x1D0F
186 /** Amazon devices */
187 #define PCI_DEVICE_ID_ENA_VF 0xEC20
188 #define PCI_DEVICE_ID_ENA_LLQ_VF 0xEC21
190 #define ENA_TX_OFFLOAD_MASK (\
195 #define ENA_TX_OFFLOAD_NOTSUP_MASK \
196 (PKT_TX_OFFLOAD_MASK ^ ENA_TX_OFFLOAD_MASK)
198 int ena_logtype_init;
199 int ena_logtype_driver;
201 static const struct rte_pci_id pci_id_ena_map[] = {
202 { RTE_PCI_DEVICE(PCI_VENDOR_ID_AMAZON, PCI_DEVICE_ID_ENA_VF) },
203 { RTE_PCI_DEVICE(PCI_VENDOR_ID_AMAZON, PCI_DEVICE_ID_ENA_LLQ_VF) },
207 static struct ena_aenq_handlers aenq_handlers;
209 static int ena_device_init(struct ena_com_dev *ena_dev,
210 struct ena_com_dev_get_features_ctx *get_feat_ctx,
212 static int ena_dev_configure(struct rte_eth_dev *dev);
213 static uint16_t eth_ena_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
215 static uint16_t eth_ena_prep_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
217 static int ena_tx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
218 uint16_t nb_desc, unsigned int socket_id,
219 const struct rte_eth_txconf *tx_conf);
220 static int ena_rx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
221 uint16_t nb_desc, unsigned int socket_id,
222 const struct rte_eth_rxconf *rx_conf,
223 struct rte_mempool *mp);
224 static uint16_t eth_ena_recv_pkts(void *rx_queue,
225 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
226 static int ena_populate_rx_queue(struct ena_ring *rxq, unsigned int count);
227 static void ena_init_rings(struct ena_adapter *adapter);
228 static int ena_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
229 static int ena_start(struct rte_eth_dev *dev);
230 static void ena_stop(struct rte_eth_dev *dev);
231 static void ena_close(struct rte_eth_dev *dev);
232 static int ena_dev_reset(struct rte_eth_dev *dev);
233 static int ena_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats);
234 static void ena_rx_queue_release_all(struct rte_eth_dev *dev);
235 static void ena_tx_queue_release_all(struct rte_eth_dev *dev);
236 static void ena_rx_queue_release(void *queue);
237 static void ena_tx_queue_release(void *queue);
238 static void ena_rx_queue_release_bufs(struct ena_ring *ring);
239 static void ena_tx_queue_release_bufs(struct ena_ring *ring);
240 static int ena_link_update(struct rte_eth_dev *dev,
241 int wait_to_complete);
242 static int ena_queue_restart(struct ena_ring *ring);
243 static int ena_queue_restart_all(struct rte_eth_dev *dev,
244 enum ena_ring_type ring_type);
245 static void ena_stats_restart(struct rte_eth_dev *dev);
246 static void ena_infos_get(struct rte_eth_dev *dev,
247 struct rte_eth_dev_info *dev_info);
248 static int ena_rss_reta_update(struct rte_eth_dev *dev,
249 struct rte_eth_rss_reta_entry64 *reta_conf,
251 static int ena_rss_reta_query(struct rte_eth_dev *dev,
252 struct rte_eth_rss_reta_entry64 *reta_conf,
254 static int ena_get_sset_count(struct rte_eth_dev *dev, int sset);
255 static void ena_interrupt_handler_rte(void *cb_arg);
256 static void ena_timer_wd_callback(struct rte_timer *timer, void *arg);
258 static const struct eth_dev_ops ena_dev_ops = {
259 .dev_configure = ena_dev_configure,
260 .dev_infos_get = ena_infos_get,
261 .rx_queue_setup = ena_rx_queue_setup,
262 .tx_queue_setup = ena_tx_queue_setup,
263 .dev_start = ena_start,
264 .dev_stop = ena_stop,
265 .link_update = ena_link_update,
266 .stats_get = ena_stats_get,
267 .mtu_set = ena_mtu_set,
268 .rx_queue_release = ena_rx_queue_release,
269 .tx_queue_release = ena_tx_queue_release,
270 .dev_close = ena_close,
271 .dev_reset = ena_dev_reset,
272 .reta_update = ena_rss_reta_update,
273 .reta_query = ena_rss_reta_query,
276 #define NUMA_NO_NODE SOCKET_ID_ANY
278 static inline int ena_cpu_to_node(int cpu)
280 struct rte_config *config = rte_eal_get_configuration();
281 struct rte_fbarray *arr = &config->mem_config->memzones;
282 const struct rte_memzone *mz;
284 if (unlikely(cpu >= RTE_MAX_MEMZONE))
287 mz = rte_fbarray_get(arr, cpu);
289 return mz->socket_id;
292 static inline void ena_rx_mbuf_prepare(struct rte_mbuf *mbuf,
293 struct ena_com_rx_ctx *ena_rx_ctx)
295 uint64_t ol_flags = 0;
296 uint32_t packet_type = 0;
298 if (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP)
299 packet_type |= RTE_PTYPE_L4_TCP;
300 else if (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)
301 packet_type |= RTE_PTYPE_L4_UDP;
303 if (ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4)
304 packet_type |= RTE_PTYPE_L3_IPV4;
305 else if (ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV6)
306 packet_type |= RTE_PTYPE_L3_IPV6;
308 if (unlikely(ena_rx_ctx->l4_csum_err))
309 ol_flags |= PKT_RX_L4_CKSUM_BAD;
310 if (unlikely(ena_rx_ctx->l3_csum_err))
311 ol_flags |= PKT_RX_IP_CKSUM_BAD;
313 mbuf->ol_flags = ol_flags;
314 mbuf->packet_type = packet_type;
317 static inline void ena_tx_mbuf_prepare(struct rte_mbuf *mbuf,
318 struct ena_com_tx_ctx *ena_tx_ctx,
319 uint64_t queue_offloads)
321 struct ena_com_tx_meta *ena_meta = &ena_tx_ctx->ena_meta;
323 if ((mbuf->ol_flags & MBUF_OFFLOADS) &&
324 (queue_offloads & QUEUE_OFFLOADS)) {
325 /* check if TSO is required */
326 if ((mbuf->ol_flags & PKT_TX_TCP_SEG) &&
327 (queue_offloads & DEV_TX_OFFLOAD_TCP_TSO)) {
328 ena_tx_ctx->tso_enable = true;
330 ena_meta->l4_hdr_len = GET_L4_HDR_LEN(mbuf);
333 /* check if L3 checksum is needed */
334 if ((mbuf->ol_flags & PKT_TX_IP_CKSUM) &&
335 (queue_offloads & DEV_TX_OFFLOAD_IPV4_CKSUM))
336 ena_tx_ctx->l3_csum_enable = true;
338 if (mbuf->ol_flags & PKT_TX_IPV6) {
339 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
341 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
343 /* set don't fragment (DF) flag */
344 if (mbuf->packet_type &
345 (RTE_PTYPE_L4_NONFRAG
346 | RTE_PTYPE_INNER_L4_NONFRAG))
347 ena_tx_ctx->df = true;
350 /* check if L4 checksum is needed */
351 if ((mbuf->ol_flags & PKT_TX_TCP_CKSUM) &&
352 (queue_offloads & DEV_TX_OFFLOAD_TCP_CKSUM)) {
353 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
354 ena_tx_ctx->l4_csum_enable = true;
355 } else if ((mbuf->ol_flags & PKT_TX_UDP_CKSUM) &&
356 (queue_offloads & DEV_TX_OFFLOAD_UDP_CKSUM)) {
357 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
358 ena_tx_ctx->l4_csum_enable = true;
360 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UNKNOWN;
361 ena_tx_ctx->l4_csum_enable = false;
364 ena_meta->mss = mbuf->tso_segsz;
365 ena_meta->l3_hdr_len = mbuf->l3_len;
366 ena_meta->l3_hdr_offset = mbuf->l2_len;
368 ena_tx_ctx->meta_valid = true;
370 ena_tx_ctx->meta_valid = false;
374 static inline int validate_rx_req_id(struct ena_ring *rx_ring, uint16_t req_id)
376 if (likely(req_id < rx_ring->ring_size))
379 RTE_LOG(ERR, PMD, "Invalid rx req_id: %hu\n", req_id);
381 rx_ring->adapter->reset_reason = ENA_REGS_RESET_INV_RX_REQ_ID;
382 rx_ring->adapter->trigger_reset = true;
387 static int validate_tx_req_id(struct ena_ring *tx_ring, u16 req_id)
389 struct ena_tx_buffer *tx_info = NULL;
391 if (likely(req_id < tx_ring->ring_size)) {
392 tx_info = &tx_ring->tx_buffer_info[req_id];
393 if (likely(tx_info->mbuf))
398 RTE_LOG(ERR, PMD, "tx_info doesn't have valid mbuf\n");
400 RTE_LOG(ERR, PMD, "Invalid req_id: %hu\n", req_id);
402 /* Trigger device reset */
403 tx_ring->adapter->reset_reason = ENA_REGS_RESET_INV_TX_REQ_ID;
404 tx_ring->adapter->trigger_reset = true;
408 static void ena_config_host_info(struct ena_com_dev *ena_dev)
410 struct ena_admin_host_info *host_info;
413 /* Allocate only the host info */
414 rc = ena_com_allocate_host_info(ena_dev);
416 RTE_LOG(ERR, PMD, "Cannot allocate host info\n");
420 host_info = ena_dev->host_attr.host_info;
422 host_info->os_type = ENA_ADMIN_OS_DPDK;
423 host_info->kernel_ver = RTE_VERSION;
424 snprintf((char *)host_info->kernel_ver_str,
425 sizeof(host_info->kernel_ver_str),
426 "%s", rte_version());
427 host_info->os_dist = RTE_VERSION;
428 snprintf((char *)host_info->os_dist_str,
429 sizeof(host_info->os_dist_str),
430 "%s", rte_version());
431 host_info->driver_version =
432 (DRV_MODULE_VER_MAJOR) |
433 (DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
434 (DRV_MODULE_VER_SUBMINOR <<
435 ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
437 rc = ena_com_set_host_attributes(ena_dev);
439 if (rc == -ENA_COM_UNSUPPORTED)
440 RTE_LOG(WARNING, PMD, "Cannot set host attributes\n");
442 RTE_LOG(ERR, PMD, "Cannot set host attributes\n");
450 ena_com_delete_host_info(ena_dev);
454 ena_get_sset_count(struct rte_eth_dev *dev, int sset)
456 if (sset != ETH_SS_STATS)
459 /* Workaround for clang:
460 * touch internal structures to prevent
463 ENA_TOUCH(ena_stats_global_strings);
464 ENA_TOUCH(ena_stats_tx_strings);
465 ENA_TOUCH(ena_stats_rx_strings);
466 ENA_TOUCH(ena_stats_ena_com_strings);
468 return dev->data->nb_tx_queues *
469 (ENA_STATS_ARRAY_TX + ENA_STATS_ARRAY_RX) +
470 ENA_STATS_ARRAY_GLOBAL + ENA_STATS_ARRAY_ENA_COM;
473 static void ena_config_debug_area(struct ena_adapter *adapter)
478 ss_count = ena_get_sset_count(adapter->rte_dev, ETH_SS_STATS);
480 RTE_LOG(ERR, PMD, "SS count is negative\n");
484 /* allocate 32 bytes for each string and 64bit for the value */
485 debug_area_size = ss_count * ETH_GSTRING_LEN + sizeof(u64) * ss_count;
487 rc = ena_com_allocate_debug_area(&adapter->ena_dev, debug_area_size);
489 RTE_LOG(ERR, PMD, "Cannot allocate debug area\n");
493 rc = ena_com_set_host_attributes(&adapter->ena_dev);
495 if (rc == -ENA_COM_UNSUPPORTED)
496 RTE_LOG(WARNING, PMD, "Cannot set host attributes\n");
498 RTE_LOG(ERR, PMD, "Cannot set host attributes\n");
505 ena_com_delete_debug_area(&adapter->ena_dev);
508 static void ena_close(struct rte_eth_dev *dev)
510 struct ena_adapter *adapter =
511 (struct ena_adapter *)(dev->data->dev_private);
514 adapter->state = ENA_ADAPTER_STATE_CLOSED;
516 ena_rx_queue_release_all(dev);
517 ena_tx_queue_release_all(dev);
521 ena_dev_reset(struct rte_eth_dev *dev)
523 struct rte_mempool *mb_pool_rx[ENA_MAX_NUM_QUEUES];
524 struct rte_eth_dev *eth_dev;
525 struct rte_pci_device *pci_dev;
526 struct rte_intr_handle *intr_handle;
527 struct ena_com_dev *ena_dev;
528 struct ena_com_dev_get_features_ctx get_feat_ctx;
529 struct ena_adapter *adapter;
534 adapter = (struct ena_adapter *)(dev->data->dev_private);
535 ena_dev = &adapter->ena_dev;
536 eth_dev = adapter->rte_dev;
537 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
538 intr_handle = &pci_dev->intr_handle;
539 nb_queues = eth_dev->data->nb_rx_queues;
541 ena_com_set_admin_running_state(ena_dev, false);
543 rc = ena_com_dev_reset(ena_dev, adapter->reset_reason);
545 RTE_LOG(ERR, PMD, "Device reset failed\n");
547 for (i = 0; i < nb_queues; i++)
548 mb_pool_rx[i] = adapter->rx_ring[i].mb_pool;
550 ena_rx_queue_release_all(eth_dev);
551 ena_tx_queue_release_all(eth_dev);
553 rte_intr_disable(intr_handle);
555 ena_com_abort_admin_commands(ena_dev);
556 ena_com_wait_for_abort_completion(ena_dev);
557 ena_com_admin_destroy(ena_dev);
558 ena_com_mmio_reg_read_request_destroy(ena_dev);
560 rc = ena_device_init(ena_dev, &get_feat_ctx, &wd_state);
562 PMD_INIT_LOG(CRIT, "Cannot initialize device\n");
565 adapter->wd_state = wd_state;
567 rte_intr_enable(intr_handle);
568 ena_com_set_admin_polling_mode(ena_dev, false);
569 ena_com_admin_aenq_enable(ena_dev);
571 for (i = 0; i < nb_queues; ++i)
572 ena_rx_queue_setup(eth_dev, i, adapter->rx_ring_size, 0, NULL,
575 for (i = 0; i < nb_queues; ++i)
576 ena_tx_queue_setup(eth_dev, i, adapter->tx_ring_size, 0, NULL);
578 adapter->trigger_reset = false;
583 static int ena_rss_reta_update(struct rte_eth_dev *dev,
584 struct rte_eth_rss_reta_entry64 *reta_conf,
587 struct ena_adapter *adapter =
588 (struct ena_adapter *)(dev->data->dev_private);
589 struct ena_com_dev *ena_dev = &adapter->ena_dev;
595 if ((reta_size == 0) || (reta_conf == NULL))
598 if (reta_size > ENA_RX_RSS_TABLE_SIZE) {
599 RTE_LOG(WARNING, PMD,
600 "indirection table %d is bigger than supported (%d)\n",
601 reta_size, ENA_RX_RSS_TABLE_SIZE);
605 for (i = 0 ; i < reta_size ; i++) {
606 /* each reta_conf is for 64 entries.
607 * to support 128 we use 2 conf of 64
609 conf_idx = i / RTE_RETA_GROUP_SIZE;
610 idx = i % RTE_RETA_GROUP_SIZE;
611 if (TEST_BIT(reta_conf[conf_idx].mask, idx)) {
613 ENA_IO_RXQ_IDX(reta_conf[conf_idx].reta[idx]);
615 rc = ena_com_indirect_table_fill_entry(ena_dev,
618 if (unlikely(rc && rc != ENA_COM_UNSUPPORTED)) {
620 "Cannot fill indirect table\n");
626 rc = ena_com_indirect_table_set(ena_dev);
627 if (unlikely(rc && rc != ENA_COM_UNSUPPORTED)) {
628 RTE_LOG(ERR, PMD, "Cannot flush the indirect table\n");
632 RTE_LOG(DEBUG, PMD, "%s(): RSS configured %d entries for port %d\n",
633 __func__, reta_size, adapter->rte_dev->data->port_id);
638 /* Query redirection table. */
639 static int ena_rss_reta_query(struct rte_eth_dev *dev,
640 struct rte_eth_rss_reta_entry64 *reta_conf,
643 struct ena_adapter *adapter =
644 (struct ena_adapter *)(dev->data->dev_private);
645 struct ena_com_dev *ena_dev = &adapter->ena_dev;
648 u32 indirect_table[ENA_RX_RSS_TABLE_SIZE] = {0};
652 if (reta_size == 0 || reta_conf == NULL ||
653 (reta_size > RTE_RETA_GROUP_SIZE && ((reta_conf + 1) == NULL)))
656 rc = ena_com_indirect_table_get(ena_dev, indirect_table);
657 if (unlikely(rc && rc != ENA_COM_UNSUPPORTED)) {
658 RTE_LOG(ERR, PMD, "cannot get indirect table\n");
662 for (i = 0 ; i < reta_size ; i++) {
663 reta_conf_idx = i / RTE_RETA_GROUP_SIZE;
664 reta_idx = i % RTE_RETA_GROUP_SIZE;
665 if (TEST_BIT(reta_conf[reta_conf_idx].mask, reta_idx))
666 reta_conf[reta_conf_idx].reta[reta_idx] =
667 ENA_IO_RXQ_IDX_REV(indirect_table[i]);
673 static int ena_rss_init_default(struct ena_adapter *adapter)
675 struct ena_com_dev *ena_dev = &adapter->ena_dev;
676 uint16_t nb_rx_queues = adapter->rte_dev->data->nb_rx_queues;
680 rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
682 RTE_LOG(ERR, PMD, "Cannot init indirect table\n");
686 for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
687 val = i % nb_rx_queues;
688 rc = ena_com_indirect_table_fill_entry(ena_dev, i,
689 ENA_IO_RXQ_IDX(val));
690 if (unlikely(rc && (rc != ENA_COM_UNSUPPORTED))) {
691 RTE_LOG(ERR, PMD, "Cannot fill indirect table\n");
696 rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
697 ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
698 if (unlikely(rc && (rc != ENA_COM_UNSUPPORTED))) {
699 RTE_LOG(INFO, PMD, "Cannot fill hash function\n");
703 rc = ena_com_set_default_hash_ctrl(ena_dev);
704 if (unlikely(rc && (rc != ENA_COM_UNSUPPORTED))) {
705 RTE_LOG(INFO, PMD, "Cannot fill hash control\n");
709 rc = ena_com_indirect_table_set(ena_dev);
710 if (unlikely(rc && (rc != ENA_COM_UNSUPPORTED))) {
711 RTE_LOG(ERR, PMD, "Cannot flush the indirect table\n");
714 RTE_LOG(DEBUG, PMD, "RSS configured for port %d\n",
715 adapter->rte_dev->data->port_id);
720 ena_com_rss_destroy(ena_dev);
726 static void ena_rx_queue_release_all(struct rte_eth_dev *dev)
728 struct ena_ring **queues = (struct ena_ring **)dev->data->rx_queues;
729 int nb_queues = dev->data->nb_rx_queues;
732 for (i = 0; i < nb_queues; i++)
733 ena_rx_queue_release(queues[i]);
736 static void ena_tx_queue_release_all(struct rte_eth_dev *dev)
738 struct ena_ring **queues = (struct ena_ring **)dev->data->tx_queues;
739 int nb_queues = dev->data->nb_tx_queues;
742 for (i = 0; i < nb_queues; i++)
743 ena_tx_queue_release(queues[i]);
746 static void ena_rx_queue_release(void *queue)
748 struct ena_ring *ring = (struct ena_ring *)queue;
749 struct ena_adapter *adapter = ring->adapter;
752 ena_assert_msg(ring->configured,
753 "API violation - releasing not configured queue");
754 ena_assert_msg(ring->adapter->state != ENA_ADAPTER_STATE_RUNNING,
757 /* Destroy HW queue */
758 ena_qid = ENA_IO_RXQ_IDX(ring->id);
759 ena_com_destroy_io_queue(&adapter->ena_dev, ena_qid);
762 ena_rx_queue_release_bufs(ring);
764 /* Free ring resources */
765 if (ring->rx_buffer_info)
766 rte_free(ring->rx_buffer_info);
767 ring->rx_buffer_info = NULL;
769 if (ring->empty_rx_reqs)
770 rte_free(ring->empty_rx_reqs);
771 ring->empty_rx_reqs = NULL;
773 ring->configured = 0;
775 RTE_LOG(NOTICE, PMD, "RX Queue %d:%d released\n",
776 ring->port_id, ring->id);
779 static void ena_tx_queue_release(void *queue)
781 struct ena_ring *ring = (struct ena_ring *)queue;
782 struct ena_adapter *adapter = ring->adapter;
785 ena_assert_msg(ring->configured,
786 "API violation. Releasing not configured queue");
787 ena_assert_msg(ring->adapter->state != ENA_ADAPTER_STATE_RUNNING,
790 /* Destroy HW queue */
791 ena_qid = ENA_IO_TXQ_IDX(ring->id);
792 ena_com_destroy_io_queue(&adapter->ena_dev, ena_qid);
795 ena_tx_queue_release_bufs(ring);
797 /* Free ring resources */
798 if (ring->tx_buffer_info)
799 rte_free(ring->tx_buffer_info);
801 if (ring->empty_tx_reqs)
802 rte_free(ring->empty_tx_reqs);
804 ring->empty_tx_reqs = NULL;
805 ring->tx_buffer_info = NULL;
807 ring->configured = 0;
809 RTE_LOG(NOTICE, PMD, "TX Queue %d:%d released\n",
810 ring->port_id, ring->id);
813 static void ena_rx_queue_release_bufs(struct ena_ring *ring)
815 unsigned int ring_mask = ring->ring_size - 1;
817 while (ring->next_to_clean != ring->next_to_use) {
819 ring->rx_buffer_info[ring->next_to_clean & ring_mask];
822 rte_mbuf_raw_free(m);
824 ring->next_to_clean++;
828 static void ena_tx_queue_release_bufs(struct ena_ring *ring)
832 for (i = 0; i < ring->ring_size; ++i) {
833 struct ena_tx_buffer *tx_buf = &ring->tx_buffer_info[i];
836 rte_pktmbuf_free(tx_buf->mbuf);
838 ring->next_to_clean++;
842 static int ena_link_update(struct rte_eth_dev *dev,
843 __rte_unused int wait_to_complete)
845 struct rte_eth_link *link = &dev->data->dev_link;
846 struct ena_adapter *adapter;
848 adapter = (struct ena_adapter *)(dev->data->dev_private);
850 link->link_status = adapter->link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
851 link->link_speed = ETH_SPEED_NUM_NONE;
852 link->link_duplex = ETH_LINK_FULL_DUPLEX;
857 static int ena_queue_restart_all(struct rte_eth_dev *dev,
858 enum ena_ring_type ring_type)
860 struct ena_adapter *adapter =
861 (struct ena_adapter *)(dev->data->dev_private);
862 struct ena_ring *queues = NULL;
867 if (ring_type == ENA_RING_TYPE_RX) {
868 queues = adapter->rx_ring;
869 nb_queues = dev->data->nb_rx_queues;
871 queues = adapter->tx_ring;
872 nb_queues = dev->data->nb_tx_queues;
874 for (i = 0; i < nb_queues; i++) {
875 if (queues[i].configured) {
876 if (ring_type == ENA_RING_TYPE_RX) {
878 dev->data->rx_queues[i] == &queues[i],
879 "Inconsistent state of rx queues\n");
882 dev->data->tx_queues[i] == &queues[i],
883 "Inconsistent state of tx queues\n");
886 rc = ena_queue_restart(&queues[i]);
890 "failed to restart queue %d type(%d)",
900 static uint32_t ena_get_mtu_conf(struct ena_adapter *adapter)
902 uint32_t max_frame_len = adapter->max_mtu;
904 if (adapter->rte_eth_dev_data->dev_conf.rxmode.offloads &
905 DEV_RX_OFFLOAD_JUMBO_FRAME)
907 adapter->rte_eth_dev_data->dev_conf.rxmode.max_rx_pkt_len;
909 return max_frame_len;
912 static int ena_check_valid_conf(struct ena_adapter *adapter)
914 uint32_t max_frame_len = ena_get_mtu_conf(adapter);
916 if (max_frame_len > adapter->max_mtu || max_frame_len < ENA_MIN_MTU) {
917 PMD_INIT_LOG(ERR, "Unsupported MTU of %d. "
918 "max mtu: %d, min mtu: %d\n",
919 max_frame_len, adapter->max_mtu, ENA_MIN_MTU);
920 return ENA_COM_UNSUPPORTED;
927 ena_calc_queue_size(struct ena_com_dev *ena_dev,
928 u16 *max_tx_sgl_size,
929 struct ena_com_dev_get_features_ctx *get_feat_ctx)
931 uint32_t queue_size = ENA_DEFAULT_RING_SIZE;
933 queue_size = RTE_MIN(queue_size,
934 get_feat_ctx->max_queues.max_cq_depth);
935 queue_size = RTE_MIN(queue_size,
936 get_feat_ctx->max_queues.max_sq_depth);
938 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
939 queue_size = RTE_MIN(queue_size,
940 get_feat_ctx->max_queues.max_llq_depth);
942 /* Round down to power of 2 */
943 if (!rte_is_power_of_2(queue_size))
944 queue_size = rte_align32pow2(queue_size >> 1);
946 if (unlikely(queue_size == 0)) {
947 PMD_INIT_LOG(ERR, "Invalid queue size");
951 *max_tx_sgl_size = RTE_MIN(ENA_PKT_MAX_BUFS,
952 get_feat_ctx->max_queues.max_packet_tx_descs);
957 static void ena_stats_restart(struct rte_eth_dev *dev)
959 struct ena_adapter *adapter =
960 (struct ena_adapter *)(dev->data->dev_private);
962 rte_atomic64_init(&adapter->drv_stats->ierrors);
963 rte_atomic64_init(&adapter->drv_stats->oerrors);
964 rte_atomic64_init(&adapter->drv_stats->rx_nombuf);
967 static int ena_stats_get(struct rte_eth_dev *dev,
968 struct rte_eth_stats *stats)
970 struct ena_admin_basic_stats ena_stats;
971 struct ena_adapter *adapter =
972 (struct ena_adapter *)(dev->data->dev_private);
973 struct ena_com_dev *ena_dev = &adapter->ena_dev;
976 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
979 memset(&ena_stats, 0, sizeof(ena_stats));
980 rc = ena_com_get_dev_basic_stats(ena_dev, &ena_stats);
982 RTE_LOG(ERR, PMD, "Could not retrieve statistics from ENA");
986 /* Set of basic statistics from ENA */
987 stats->ipackets = __MERGE_64B_H_L(ena_stats.rx_pkts_high,
988 ena_stats.rx_pkts_low);
989 stats->opackets = __MERGE_64B_H_L(ena_stats.tx_pkts_high,
990 ena_stats.tx_pkts_low);
991 stats->ibytes = __MERGE_64B_H_L(ena_stats.rx_bytes_high,
992 ena_stats.rx_bytes_low);
993 stats->obytes = __MERGE_64B_H_L(ena_stats.tx_bytes_high,
994 ena_stats.tx_bytes_low);
995 stats->imissed = __MERGE_64B_H_L(ena_stats.rx_drops_high,
996 ena_stats.rx_drops_low);
998 /* Driver related stats */
999 stats->ierrors = rte_atomic64_read(&adapter->drv_stats->ierrors);
1000 stats->oerrors = rte_atomic64_read(&adapter->drv_stats->oerrors);
1001 stats->rx_nombuf = rte_atomic64_read(&adapter->drv_stats->rx_nombuf);
1005 static int ena_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1007 struct ena_adapter *adapter;
1008 struct ena_com_dev *ena_dev;
1011 ena_assert_msg(dev->data != NULL, "Uninitialized device");
1012 ena_assert_msg(dev->data->dev_private != NULL, "Uninitialized device");
1013 adapter = (struct ena_adapter *)(dev->data->dev_private);
1015 ena_dev = &adapter->ena_dev;
1016 ena_assert_msg(ena_dev != NULL, "Uninitialized device");
1018 if (mtu > ena_get_mtu_conf(adapter) || mtu < ENA_MIN_MTU) {
1020 "Invalid MTU setting. new_mtu: %d "
1021 "max mtu: %d min mtu: %d\n",
1022 mtu, ena_get_mtu_conf(adapter), ENA_MIN_MTU);
1026 rc = ena_com_set_dev_mtu(ena_dev, mtu);
1028 RTE_LOG(ERR, PMD, "Could not set MTU: %d\n", mtu);
1030 RTE_LOG(NOTICE, PMD, "Set MTU: %d\n", mtu);
1035 static int ena_start(struct rte_eth_dev *dev)
1037 struct ena_adapter *adapter =
1038 (struct ena_adapter *)(dev->data->dev_private);
1042 rc = ena_check_valid_conf(adapter);
1046 rc = ena_queue_restart_all(dev, ENA_RING_TYPE_RX);
1050 rc = ena_queue_restart_all(dev, ENA_RING_TYPE_TX);
1054 if (adapter->rte_dev->data->dev_conf.rxmode.mq_mode &
1055 ETH_MQ_RX_RSS_FLAG && adapter->rte_dev->data->nb_rx_queues > 0) {
1056 rc = ena_rss_init_default(adapter);
1061 ena_stats_restart(dev);
1063 adapter->timestamp_wd = rte_get_timer_cycles();
1064 adapter->keep_alive_timeout = ENA_DEVICE_KALIVE_TIMEOUT;
1066 ticks = rte_get_timer_hz();
1067 rte_timer_reset(&adapter->timer_wd, ticks, PERIODICAL, rte_lcore_id(),
1068 ena_timer_wd_callback, adapter);
1070 adapter->state = ENA_ADAPTER_STATE_RUNNING;
1075 static void ena_stop(struct rte_eth_dev *dev)
1077 struct ena_adapter *adapter =
1078 (struct ena_adapter *)(dev->data->dev_private);
1080 rte_timer_stop_sync(&adapter->timer_wd);
1082 adapter->state = ENA_ADAPTER_STATE_STOPPED;
1085 static int ena_queue_restart(struct ena_ring *ring)
1089 ena_assert_msg(ring->configured == 1,
1090 "Trying to restart unconfigured queue\n");
1092 ring->next_to_clean = 0;
1093 ring->next_to_use = 0;
1095 if (ring->type == ENA_RING_TYPE_TX)
1098 bufs_num = ring->ring_size - 1;
1099 rc = ena_populate_rx_queue(ring, bufs_num);
1100 if (rc != bufs_num) {
1101 PMD_INIT_LOG(ERR, "Failed to populate rx ring !");
1102 return ENA_COM_FAULT;
1108 static int ena_tx_queue_setup(struct rte_eth_dev *dev,
1111 __rte_unused unsigned int socket_id,
1112 const struct rte_eth_txconf *tx_conf)
1114 struct ena_com_create_io_ctx ctx =
1115 /* policy set to _HOST just to satisfy icc compiler */
1116 { ENA_ADMIN_PLACEMENT_POLICY_HOST,
1117 ENA_COM_IO_QUEUE_DIRECTION_TX, 0, 0, 0, 0 };
1118 struct ena_ring *txq = NULL;
1119 struct ena_adapter *adapter =
1120 (struct ena_adapter *)(dev->data->dev_private);
1124 struct ena_com_dev *ena_dev = &adapter->ena_dev;
1126 txq = &adapter->tx_ring[queue_idx];
1128 if (txq->configured) {
1130 "API violation. Queue %d is already configured\n",
1132 return ENA_COM_FAULT;
1135 if (!rte_is_power_of_2(nb_desc)) {
1137 "Unsupported size of TX queue: %d is not a power of 2.",
1142 if (nb_desc > adapter->tx_ring_size) {
1144 "Unsupported size of TX queue (max size: %d)\n",
1145 adapter->tx_ring_size);
1149 ena_qid = ENA_IO_TXQ_IDX(queue_idx);
1151 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
1153 ctx.msix_vector = -1; /* admin interrupts not used */
1154 ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
1155 ctx.queue_size = adapter->tx_ring_size;
1156 ctx.numa_node = ena_cpu_to_node(queue_idx);
1158 rc = ena_com_create_io_queue(ena_dev, &ctx);
1161 "failed to create io TX queue #%d (qid:%d) rc: %d\n",
1162 queue_idx, ena_qid, rc);
1165 txq->ena_com_io_cq = &ena_dev->io_cq_queues[ena_qid];
1166 txq->ena_com_io_sq = &ena_dev->io_sq_queues[ena_qid];
1168 rc = ena_com_get_io_handlers(ena_dev, ena_qid,
1169 &txq->ena_com_io_sq,
1170 &txq->ena_com_io_cq);
1173 "Failed to get TX queue handlers. TX queue num %d rc: %d\n",
1175 goto err_destroy_io_queue;
1178 ena_com_update_numa_node(txq->ena_com_io_cq, ctx.numa_node);
1180 txq->port_id = dev->data->port_id;
1181 txq->next_to_clean = 0;
1182 txq->next_to_use = 0;
1183 txq->ring_size = nb_desc;
1185 txq->tx_buffer_info = rte_zmalloc("txq->tx_buffer_info",
1186 sizeof(struct ena_tx_buffer) *
1188 RTE_CACHE_LINE_SIZE);
1189 if (!txq->tx_buffer_info) {
1190 RTE_LOG(ERR, PMD, "failed to alloc mem for tx buffer info\n");
1192 goto err_destroy_io_queue;
1195 txq->empty_tx_reqs = rte_zmalloc("txq->empty_tx_reqs",
1196 sizeof(u16) * txq->ring_size,
1197 RTE_CACHE_LINE_SIZE);
1198 if (!txq->empty_tx_reqs) {
1199 RTE_LOG(ERR, PMD, "failed to alloc mem for tx reqs\n");
1204 for (i = 0; i < txq->ring_size; i++)
1205 txq->empty_tx_reqs[i] = i;
1207 if (tx_conf != NULL) {
1209 tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
1212 /* Store pointer to this queue in upper layer */
1213 txq->configured = 1;
1214 dev->data->tx_queues[queue_idx] = txq;
1219 rte_free(txq->tx_buffer_info);
1221 err_destroy_io_queue:
1222 ena_com_destroy_io_queue(ena_dev, ena_qid);
1226 static int ena_rx_queue_setup(struct rte_eth_dev *dev,
1229 __rte_unused unsigned int socket_id,
1230 __rte_unused const struct rte_eth_rxconf *rx_conf,
1231 struct rte_mempool *mp)
1233 struct ena_com_create_io_ctx ctx =
1234 /* policy set to _HOST just to satisfy icc compiler */
1235 { ENA_ADMIN_PLACEMENT_POLICY_HOST,
1236 ENA_COM_IO_QUEUE_DIRECTION_RX, 0, 0, 0, 0 };
1237 struct ena_adapter *adapter =
1238 (struct ena_adapter *)(dev->data->dev_private);
1239 struct ena_ring *rxq = NULL;
1240 uint16_t ena_qid = 0;
1242 struct ena_com_dev *ena_dev = &adapter->ena_dev;
1244 rxq = &adapter->rx_ring[queue_idx];
1245 if (rxq->configured) {
1247 "API violation. Queue %d is already configured\n",
1249 return ENA_COM_FAULT;
1252 if (!rte_is_power_of_2(nb_desc)) {
1254 "Unsupported size of RX queue: %d is not a power of 2.",
1259 if (nb_desc > adapter->rx_ring_size) {
1261 "Unsupported size of RX queue (max size: %d)\n",
1262 adapter->rx_ring_size);
1266 ena_qid = ENA_IO_RXQ_IDX(queue_idx);
1269 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
1270 ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
1271 ctx.msix_vector = -1; /* admin interrupts not used */
1272 ctx.queue_size = adapter->rx_ring_size;
1273 ctx.numa_node = ena_cpu_to_node(queue_idx);
1275 rc = ena_com_create_io_queue(ena_dev, &ctx);
1277 RTE_LOG(ERR, PMD, "failed to create io RX queue #%d rc: %d\n",
1282 rxq->ena_com_io_cq = &ena_dev->io_cq_queues[ena_qid];
1283 rxq->ena_com_io_sq = &ena_dev->io_sq_queues[ena_qid];
1285 rc = ena_com_get_io_handlers(ena_dev, ena_qid,
1286 &rxq->ena_com_io_sq,
1287 &rxq->ena_com_io_cq);
1290 "Failed to get RX queue handlers. RX queue num %d rc: %d\n",
1292 ena_com_destroy_io_queue(ena_dev, ena_qid);
1296 rxq->port_id = dev->data->port_id;
1297 rxq->next_to_clean = 0;
1298 rxq->next_to_use = 0;
1299 rxq->ring_size = nb_desc;
1302 rxq->rx_buffer_info = rte_zmalloc("rxq->buffer_info",
1303 sizeof(struct rte_mbuf *) * nb_desc,
1304 RTE_CACHE_LINE_SIZE);
1305 if (!rxq->rx_buffer_info) {
1306 RTE_LOG(ERR, PMD, "failed to alloc mem for rx buffer info\n");
1307 ena_com_destroy_io_queue(ena_dev, ena_qid);
1311 rxq->empty_rx_reqs = rte_zmalloc("rxq->empty_rx_reqs",
1312 sizeof(uint16_t) * nb_desc,
1313 RTE_CACHE_LINE_SIZE);
1314 if (!rxq->empty_rx_reqs) {
1315 RTE_LOG(ERR, PMD, "failed to alloc mem for empty rx reqs\n");
1316 rte_free(rxq->rx_buffer_info);
1317 rxq->rx_buffer_info = NULL;
1318 ena_com_destroy_io_queue(ena_dev, ena_qid);
1322 for (i = 0; i < nb_desc; i++)
1323 rxq->empty_tx_reqs[i] = i;
1325 /* Store pointer to this queue in upper layer */
1326 rxq->configured = 1;
1327 dev->data->rx_queues[queue_idx] = rxq;
1332 static int ena_populate_rx_queue(struct ena_ring *rxq, unsigned int count)
1336 uint16_t ring_size = rxq->ring_size;
1337 uint16_t ring_mask = ring_size - 1;
1338 uint16_t next_to_use = rxq->next_to_use;
1339 uint16_t in_use, req_id;
1340 struct rte_mbuf **mbufs = &rxq->rx_buffer_info[0];
1342 if (unlikely(!count))
1345 in_use = rxq->next_to_use - rxq->next_to_clean;
1346 ena_assert_msg(((in_use + count) < ring_size), "bad ring state");
1348 count = RTE_MIN(count,
1349 (uint16_t)(ring_size - (next_to_use & ring_mask)));
1351 /* get resources for incoming packets */
1352 rc = rte_mempool_get_bulk(rxq->mb_pool,
1353 (void **)(&mbufs[next_to_use & ring_mask]),
1355 if (unlikely(rc < 0)) {
1356 rte_atomic64_inc(&rxq->adapter->drv_stats->rx_nombuf);
1357 PMD_RX_LOG(DEBUG, "there are no enough free buffers");
1361 for (i = 0; i < count; i++) {
1362 uint16_t next_to_use_masked = next_to_use & ring_mask;
1363 struct rte_mbuf *mbuf = mbufs[next_to_use_masked];
1364 struct ena_com_buf ebuf;
1366 rte_prefetch0(mbufs[((next_to_use + 4) & ring_mask)]);
1368 req_id = rxq->empty_rx_reqs[next_to_use_masked];
1369 rc = validate_rx_req_id(rxq, req_id);
1370 if (unlikely(rc < 0))
1373 /* prepare physical address for DMA transaction */
1374 ebuf.paddr = mbuf->buf_iova + RTE_PKTMBUF_HEADROOM;
1375 ebuf.len = mbuf->buf_len - RTE_PKTMBUF_HEADROOM;
1376 /* pass resource to device */
1377 rc = ena_com_add_single_rx_desc(rxq->ena_com_io_sq,
1380 rte_mempool_put_bulk(rxq->mb_pool, (void **)(&mbuf),
1382 RTE_LOG(WARNING, PMD, "failed adding rx desc\n");
1388 if (unlikely(i < count))
1389 RTE_LOG(WARNING, PMD, "refilled rx qid %d with only %d "
1390 "buffers (from %d)\n", rxq->id, i, count);
1392 /* When we submitted free recources to device... */
1393 if (likely(i > 0)) {
1394 /* ...let HW know that it can fill buffers with data
1396 * Add memory barrier to make sure the desc were written before
1400 ena_com_write_sq_doorbell(rxq->ena_com_io_sq);
1402 rxq->next_to_use = next_to_use;
1408 static int ena_device_init(struct ena_com_dev *ena_dev,
1409 struct ena_com_dev_get_features_ctx *get_feat_ctx,
1412 uint32_t aenq_groups;
1414 bool readless_supported;
1416 /* Initialize mmio registers */
1417 rc = ena_com_mmio_reg_read_request_init(ena_dev);
1419 RTE_LOG(ERR, PMD, "failed to init mmio read less\n");
1423 /* The PCIe configuration space revision id indicate if mmio reg
1426 readless_supported =
1427 !(((struct rte_pci_device *)ena_dev->dmadev)->id.class_id
1428 & ENA_MMIO_DISABLE_REG_READ);
1429 ena_com_set_mmio_read_mode(ena_dev, readless_supported);
1432 rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
1434 RTE_LOG(ERR, PMD, "cannot reset device\n");
1435 goto err_mmio_read_less;
1438 /* check FW version */
1439 rc = ena_com_validate_version(ena_dev);
1441 RTE_LOG(ERR, PMD, "device version is too low\n");
1442 goto err_mmio_read_less;
1445 ena_dev->dma_addr_bits = ena_com_get_dma_width(ena_dev);
1447 /* ENA device administration layer init */
1448 rc = ena_com_admin_init(ena_dev, &aenq_handlers, true);
1451 "cannot initialize ena admin queue with device\n");
1452 goto err_mmio_read_less;
1455 /* To enable the msix interrupts the driver needs to know the number
1456 * of queues. So the driver uses polling mode to retrieve this
1459 ena_com_set_admin_polling_mode(ena_dev, true);
1461 ena_config_host_info(ena_dev);
1463 /* Get Device Attributes and features */
1464 rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
1467 "cannot get attribute for ena device rc= %d\n", rc);
1468 goto err_admin_init;
1471 aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
1472 BIT(ENA_ADMIN_NOTIFICATION) |
1473 BIT(ENA_ADMIN_KEEP_ALIVE) |
1474 BIT(ENA_ADMIN_FATAL_ERROR) |
1475 BIT(ENA_ADMIN_WARNING);
1477 aenq_groups &= get_feat_ctx->aenq.supported_groups;
1478 rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
1480 RTE_LOG(ERR, PMD, "Cannot configure aenq groups rc: %d\n", rc);
1481 goto err_admin_init;
1484 *wd_state = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
1489 ena_com_admin_destroy(ena_dev);
1492 ena_com_mmio_reg_read_request_destroy(ena_dev);
1497 static void ena_interrupt_handler_rte(void *cb_arg)
1499 struct ena_adapter *adapter = (struct ena_adapter *)cb_arg;
1500 struct ena_com_dev *ena_dev = &adapter->ena_dev;
1502 ena_com_admin_q_comp_intr_handler(ena_dev);
1503 if (likely(adapter->state != ENA_ADAPTER_STATE_CLOSED))
1504 ena_com_aenq_intr_handler(ena_dev, adapter);
1507 static void check_for_missing_keep_alive(struct ena_adapter *adapter)
1509 if (!adapter->wd_state)
1512 if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT)
1515 if (unlikely((rte_get_timer_cycles() - adapter->timestamp_wd) >=
1516 adapter->keep_alive_timeout)) {
1517 RTE_LOG(ERR, PMD, "Keep alive timeout\n");
1518 adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO;
1519 adapter->trigger_reset = true;
1523 /* Check if admin queue is enabled */
1524 static void check_for_admin_com_state(struct ena_adapter *adapter)
1526 if (unlikely(!ena_com_get_admin_running_state(&adapter->ena_dev))) {
1527 RTE_LOG(ERR, PMD, "ENA admin queue is not in running state!\n");
1528 adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO;
1529 adapter->trigger_reset = true;
1533 static void ena_timer_wd_callback(__rte_unused struct rte_timer *timer,
1536 struct ena_adapter *adapter = (struct ena_adapter *)arg;
1537 struct rte_eth_dev *dev = adapter->rte_dev;
1539 check_for_missing_keep_alive(adapter);
1540 check_for_admin_com_state(adapter);
1542 if (unlikely(adapter->trigger_reset)) {
1543 RTE_LOG(ERR, PMD, "Trigger reset is on\n");
1544 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RESET,
1549 static int ena_calc_io_queue_num(__rte_unused struct ena_com_dev *ena_dev,
1550 struct ena_com_dev_get_features_ctx *get_feat_ctx)
1552 int io_sq_num, io_cq_num, io_queue_num;
1554 io_sq_num = get_feat_ctx->max_queues.max_sq_num;
1555 io_cq_num = get_feat_ctx->max_queues.max_cq_num;
1557 io_queue_num = RTE_MIN(io_sq_num, io_cq_num);
1559 if (unlikely(io_queue_num == 0)) {
1560 RTE_LOG(ERR, PMD, "Number of IO queues should not be 0\n");
1564 return io_queue_num;
1567 static int eth_ena_dev_init(struct rte_eth_dev *eth_dev)
1569 struct rte_pci_device *pci_dev;
1570 struct rte_intr_handle *intr_handle;
1571 struct ena_adapter *adapter =
1572 (struct ena_adapter *)(eth_dev->data->dev_private);
1573 struct ena_com_dev *ena_dev = &adapter->ena_dev;
1574 struct ena_com_dev_get_features_ctx get_feat_ctx;
1576 u16 tx_sgl_size = 0;
1578 static int adapters_found;
1581 memset(adapter, 0, sizeof(struct ena_adapter));
1582 ena_dev = &adapter->ena_dev;
1584 eth_dev->dev_ops = &ena_dev_ops;
1585 eth_dev->rx_pkt_burst = ð_ena_recv_pkts;
1586 eth_dev->tx_pkt_burst = ð_ena_xmit_pkts;
1587 eth_dev->tx_pkt_prepare = ð_ena_prep_pkts;
1588 adapter->rte_eth_dev_data = eth_dev->data;
1589 adapter->rte_dev = eth_dev;
1591 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1594 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1595 adapter->pdev = pci_dev;
1597 PMD_INIT_LOG(INFO, "Initializing %x:%x:%x.%d",
1598 pci_dev->addr.domain,
1600 pci_dev->addr.devid,
1601 pci_dev->addr.function);
1603 intr_handle = &pci_dev->intr_handle;
1605 adapter->regs = pci_dev->mem_resource[ENA_REGS_BAR].addr;
1606 adapter->dev_mem_base = pci_dev->mem_resource[ENA_MEM_BAR].addr;
1608 if (!adapter->regs) {
1609 PMD_INIT_LOG(CRIT, "Failed to access registers BAR(%d)",
1614 ena_dev->reg_bar = adapter->regs;
1615 ena_dev->dmadev = adapter->pdev;
1617 adapter->id_number = adapters_found;
1619 snprintf(adapter->name, ENA_NAME_MAX_LEN, "ena_%d",
1620 adapter->id_number);
1622 /* device specific initialization routine */
1623 rc = ena_device_init(ena_dev, &get_feat_ctx, &wd_state);
1625 PMD_INIT_LOG(CRIT, "Failed to init ENA device");
1628 adapter->wd_state = wd_state;
1630 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
1631 adapter->num_queues = ena_calc_io_queue_num(ena_dev,
1634 queue_size = ena_calc_queue_size(ena_dev, &tx_sgl_size, &get_feat_ctx);
1635 if (queue_size <= 0 || adapter->num_queues <= 0) {
1637 goto err_device_destroy;
1640 adapter->tx_ring_size = queue_size;
1641 adapter->rx_ring_size = queue_size;
1643 adapter->max_tx_sgl_size = tx_sgl_size;
1645 /* prepare ring structures */
1646 ena_init_rings(adapter);
1648 ena_config_debug_area(adapter);
1650 /* Set max MTU for this device */
1651 adapter->max_mtu = get_feat_ctx.dev_attr.max_mtu;
1653 /* set device support for TSO */
1654 adapter->tso4_supported = get_feat_ctx.offload.tx &
1655 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK;
1657 /* Copy MAC address and point DPDK to it */
1658 eth_dev->data->mac_addrs = (struct ether_addr *)adapter->mac_addr;
1659 ether_addr_copy((struct ether_addr *)get_feat_ctx.dev_attr.mac_addr,
1660 (struct ether_addr *)adapter->mac_addr);
1662 adapter->drv_stats = rte_zmalloc("adapter stats",
1663 sizeof(*adapter->drv_stats),
1664 RTE_CACHE_LINE_SIZE);
1665 if (!adapter->drv_stats) {
1666 RTE_LOG(ERR, PMD, "failed to alloc mem for adapter stats\n");
1668 goto err_delete_debug_area;
1671 rte_intr_callback_register(intr_handle,
1672 ena_interrupt_handler_rte,
1674 rte_intr_enable(intr_handle);
1675 ena_com_set_admin_polling_mode(ena_dev, false);
1676 ena_com_admin_aenq_enable(ena_dev);
1678 if (adapters_found == 0)
1679 rte_timer_subsystem_init();
1680 rte_timer_init(&adapter->timer_wd);
1683 adapter->state = ENA_ADAPTER_STATE_INIT;
1687 err_delete_debug_area:
1688 ena_com_delete_debug_area(ena_dev);
1691 ena_com_delete_host_info(ena_dev);
1692 ena_com_admin_destroy(ena_dev);
1698 static int eth_ena_dev_uninit(struct rte_eth_dev *eth_dev)
1700 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1701 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1702 struct ena_adapter *adapter =
1703 (struct ena_adapter *)(eth_dev->data->dev_private);
1705 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1708 if (adapter->state != ENA_ADAPTER_STATE_CLOSED)
1711 eth_dev->dev_ops = NULL;
1712 eth_dev->rx_pkt_burst = NULL;
1713 eth_dev->tx_pkt_burst = NULL;
1714 eth_dev->tx_pkt_prepare = NULL;
1716 rte_free(adapter->drv_stats);
1717 adapter->drv_stats = NULL;
1719 rte_intr_disable(intr_handle);
1720 rte_intr_callback_unregister(intr_handle,
1721 ena_interrupt_handler_rte,
1724 adapter->state = ENA_ADAPTER_STATE_FREE;
1729 static int ena_dev_configure(struct rte_eth_dev *dev)
1731 struct ena_adapter *adapter =
1732 (struct ena_adapter *)(dev->data->dev_private);
1734 adapter->state = ENA_ADAPTER_STATE_CONFIG;
1736 adapter->tx_selected_offloads = dev->data->dev_conf.txmode.offloads;
1737 adapter->rx_selected_offloads = dev->data->dev_conf.rxmode.offloads;
1741 static void ena_init_rings(struct ena_adapter *adapter)
1745 for (i = 0; i < adapter->num_queues; i++) {
1746 struct ena_ring *ring = &adapter->tx_ring[i];
1748 ring->configured = 0;
1749 ring->type = ENA_RING_TYPE_TX;
1750 ring->adapter = adapter;
1752 ring->tx_mem_queue_type = adapter->ena_dev.tx_mem_queue_type;
1753 ring->tx_max_header_size = adapter->ena_dev.tx_max_header_size;
1754 ring->sgl_size = adapter->max_tx_sgl_size;
1757 for (i = 0; i < adapter->num_queues; i++) {
1758 struct ena_ring *ring = &adapter->rx_ring[i];
1760 ring->configured = 0;
1761 ring->type = ENA_RING_TYPE_RX;
1762 ring->adapter = adapter;
1767 static void ena_infos_get(struct rte_eth_dev *dev,
1768 struct rte_eth_dev_info *dev_info)
1770 struct ena_adapter *adapter;
1771 struct ena_com_dev *ena_dev;
1772 struct ena_com_dev_get_features_ctx feat;
1773 uint64_t rx_feat = 0, tx_feat = 0;
1776 ena_assert_msg(dev->data != NULL, "Uninitialized device");
1777 ena_assert_msg(dev->data->dev_private != NULL, "Uninitialized device");
1778 adapter = (struct ena_adapter *)(dev->data->dev_private);
1780 ena_dev = &adapter->ena_dev;
1781 ena_assert_msg(ena_dev != NULL, "Uninitialized device");
1783 dev_info->speed_capa =
1785 ETH_LINK_SPEED_2_5G |
1787 ETH_LINK_SPEED_10G |
1788 ETH_LINK_SPEED_25G |
1789 ETH_LINK_SPEED_40G |
1790 ETH_LINK_SPEED_50G |
1791 ETH_LINK_SPEED_100G;
1793 /* Get supported features from HW */
1794 rc = ena_com_get_dev_attr_feat(ena_dev, &feat);
1797 "Cannot get attribute for ena device rc= %d\n", rc);
1801 /* Set Tx & Rx features available for device */
1802 if (feat.offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK)
1803 tx_feat |= DEV_TX_OFFLOAD_TCP_TSO;
1805 if (feat.offload.tx &
1806 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)
1807 tx_feat |= DEV_TX_OFFLOAD_IPV4_CKSUM |
1808 DEV_TX_OFFLOAD_UDP_CKSUM |
1809 DEV_TX_OFFLOAD_TCP_CKSUM;
1811 if (feat.offload.rx_supported &
1812 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK)
1813 rx_feat |= DEV_RX_OFFLOAD_IPV4_CKSUM |
1814 DEV_RX_OFFLOAD_UDP_CKSUM |
1815 DEV_RX_OFFLOAD_TCP_CKSUM;
1817 rx_feat |= DEV_RX_OFFLOAD_JUMBO_FRAME;
1819 /* Inform framework about available features */
1820 dev_info->rx_offload_capa = rx_feat;
1821 dev_info->rx_queue_offload_capa = rx_feat;
1822 dev_info->tx_offload_capa = tx_feat;
1823 dev_info->tx_queue_offload_capa = tx_feat;
1825 dev_info->min_rx_bufsize = ENA_MIN_FRAME_LEN;
1826 dev_info->max_rx_pktlen = adapter->max_mtu;
1827 dev_info->max_mac_addrs = 1;
1829 dev_info->max_rx_queues = adapter->num_queues;
1830 dev_info->max_tx_queues = adapter->num_queues;
1831 dev_info->reta_size = ENA_RX_RSS_TABLE_SIZE;
1833 adapter->tx_supported_offloads = tx_feat;
1834 adapter->rx_supported_offloads = rx_feat;
1836 dev_info->rx_desc_lim.nb_max = ENA_MAX_RING_DESC;
1837 dev_info->rx_desc_lim.nb_min = ENA_MIN_RING_DESC;
1839 dev_info->tx_desc_lim.nb_max = ENA_MAX_RING_DESC;
1840 dev_info->tx_desc_lim.nb_min = ENA_MIN_RING_DESC;
1841 dev_info->tx_desc_lim.nb_seg_max = RTE_MIN(ENA_PKT_MAX_BUFS,
1842 feat.max_queues.max_packet_tx_descs);
1843 dev_info->tx_desc_lim.nb_mtu_seg_max = RTE_MIN(ENA_PKT_MAX_BUFS,
1844 feat.max_queues.max_packet_tx_descs);
1847 static uint16_t eth_ena_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
1850 struct ena_ring *rx_ring = (struct ena_ring *)(rx_queue);
1851 unsigned int ring_size = rx_ring->ring_size;
1852 unsigned int ring_mask = ring_size - 1;
1853 uint16_t next_to_clean = rx_ring->next_to_clean;
1854 uint16_t desc_in_use = 0;
1856 unsigned int recv_idx = 0;
1857 struct rte_mbuf *mbuf = NULL;
1858 struct rte_mbuf *mbuf_head = NULL;
1859 struct rte_mbuf *mbuf_prev = NULL;
1860 struct rte_mbuf **rx_buff_info = rx_ring->rx_buffer_info;
1861 unsigned int completed;
1863 struct ena_com_rx_ctx ena_rx_ctx;
1866 /* Check adapter state */
1867 if (unlikely(rx_ring->adapter->state != ENA_ADAPTER_STATE_RUNNING)) {
1869 "Trying to receive pkts while device is NOT running\n");
1873 desc_in_use = rx_ring->next_to_use - next_to_clean;
1874 if (unlikely(nb_pkts > desc_in_use))
1875 nb_pkts = desc_in_use;
1877 for (completed = 0; completed < nb_pkts; completed++) {
1880 ena_rx_ctx.max_bufs = rx_ring->ring_size;
1881 ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
1882 ena_rx_ctx.descs = 0;
1883 /* receive packet context */
1884 rc = ena_com_rx_pkt(rx_ring->ena_com_io_cq,
1885 rx_ring->ena_com_io_sq,
1888 RTE_LOG(ERR, PMD, "ena_com_rx_pkt error %d\n", rc);
1889 rx_ring->adapter->trigger_reset = true;
1893 if (unlikely(ena_rx_ctx.descs == 0))
1896 while (segments < ena_rx_ctx.descs) {
1897 req_id = ena_rx_ctx.ena_bufs[segments].req_id;
1898 rc = validate_rx_req_id(rx_ring, req_id);
1902 mbuf = rx_buff_info[req_id];
1903 mbuf->data_len = ena_rx_ctx.ena_bufs[segments].len;
1904 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
1907 if (unlikely(segments == 0)) {
1908 mbuf->nb_segs = ena_rx_ctx.descs;
1909 mbuf->port = rx_ring->port_id;
1913 /* for multi-segment pkts create mbuf chain */
1914 mbuf_prev->next = mbuf;
1916 mbuf_head->pkt_len += mbuf->data_len;
1919 rx_ring->empty_rx_reqs[next_to_clean & ring_mask] =
1925 /* fill mbuf attributes if any */
1926 ena_rx_mbuf_prepare(mbuf_head, &ena_rx_ctx);
1927 mbuf_head->hash.rss = (uint32_t)rx_ring->id;
1929 /* pass to DPDK application head mbuf */
1930 rx_pkts[recv_idx] = mbuf_head;
1934 rx_ring->next_to_clean = next_to_clean;
1936 desc_in_use = desc_in_use - completed + 1;
1937 /* Burst refill to save doorbells, memory barriers, const interval */
1938 if (ring_size - desc_in_use > ENA_RING_DESCS_RATIO(ring_size))
1939 ena_populate_rx_queue(rx_ring, ring_size - desc_in_use);
1945 eth_ena_prep_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
1951 struct ena_ring *tx_ring = (struct ena_ring *)(tx_queue);
1952 struct ipv4_hdr *ip_hdr;
1954 uint16_t frag_field;
1956 for (i = 0; i != nb_pkts; i++) {
1958 ol_flags = m->ol_flags;
1960 if (!(ol_flags & PKT_TX_IPV4))
1963 /* If there was not L2 header length specified, assume it is
1964 * length of the ethernet header.
1966 if (unlikely(m->l2_len == 0))
1967 m->l2_len = sizeof(struct ether_hdr);
1969 ip_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
1971 frag_field = rte_be_to_cpu_16(ip_hdr->fragment_offset);
1973 if ((frag_field & IPV4_HDR_DF_FLAG) != 0) {
1974 m->packet_type |= RTE_PTYPE_L4_NONFRAG;
1976 /* If IPv4 header has DF flag enabled and TSO support is
1977 * disabled, partial chcecksum should not be calculated.
1979 if (!tx_ring->adapter->tso4_supported)
1983 if ((ol_flags & ENA_TX_OFFLOAD_NOTSUP_MASK) != 0 ||
1984 (ol_flags & PKT_TX_L4_MASK) ==
1985 PKT_TX_SCTP_CKSUM) {
1986 rte_errno = -ENOTSUP;
1990 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1991 ret = rte_validate_tx_offload(m);
1998 /* In case we are supposed to TSO and have DF not set (DF=0)
1999 * hardware must be provided with partial checksum, otherwise
2000 * it will take care of necessary calculations.
2003 ret = rte_net_intel_cksum_flags_prepare(m,
2004 ol_flags & ~PKT_TX_TCP_SEG);
2014 static void ena_update_hints(struct ena_adapter *adapter,
2015 struct ena_admin_ena_hw_hints *hints)
2017 if (hints->admin_completion_tx_timeout)
2018 adapter->ena_dev.admin_queue.completion_timeout =
2019 hints->admin_completion_tx_timeout * 1000;
2021 if (hints->mmio_read_timeout)
2022 /* convert to usec */
2023 adapter->ena_dev.mmio_read.reg_read_to =
2024 hints->mmio_read_timeout * 1000;
2026 if (hints->driver_watchdog_timeout) {
2027 if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT)
2028 adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT;
2030 // Convert msecs to ticks
2031 adapter->keep_alive_timeout =
2032 (hints->driver_watchdog_timeout *
2033 rte_get_timer_hz()) / 1000;
2037 static int ena_check_and_linearize_mbuf(struct ena_ring *tx_ring,
2038 struct rte_mbuf *mbuf)
2040 int num_segments, rc;
2042 num_segments = mbuf->nb_segs;
2044 if (likely(num_segments < tx_ring->sgl_size))
2047 rc = rte_pktmbuf_linearize(mbuf);
2049 RTE_LOG(WARNING, PMD, "Mbuf linearize failed\n");
2054 static uint16_t eth_ena_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
2057 struct ena_ring *tx_ring = (struct ena_ring *)(tx_queue);
2058 uint16_t next_to_use = tx_ring->next_to_use;
2059 uint16_t next_to_clean = tx_ring->next_to_clean;
2060 struct rte_mbuf *mbuf;
2061 unsigned int ring_size = tx_ring->ring_size;
2062 unsigned int ring_mask = ring_size - 1;
2063 struct ena_com_tx_ctx ena_tx_ctx;
2064 struct ena_tx_buffer *tx_info;
2065 struct ena_com_buf *ebuf;
2066 uint16_t rc, req_id, total_tx_descs = 0;
2067 uint16_t sent_idx = 0, empty_tx_reqs;
2070 /* Check adapter state */
2071 if (unlikely(tx_ring->adapter->state != ENA_ADAPTER_STATE_RUNNING)) {
2073 "Trying to xmit pkts while device is NOT running\n");
2077 empty_tx_reqs = ring_size - (next_to_use - next_to_clean);
2078 if (nb_pkts > empty_tx_reqs)
2079 nb_pkts = empty_tx_reqs;
2081 for (sent_idx = 0; sent_idx < nb_pkts; sent_idx++) {
2082 mbuf = tx_pkts[sent_idx];
2084 rc = ena_check_and_linearize_mbuf(tx_ring, mbuf);
2088 req_id = tx_ring->empty_tx_reqs[next_to_use & ring_mask];
2089 tx_info = &tx_ring->tx_buffer_info[req_id];
2090 tx_info->mbuf = mbuf;
2091 tx_info->num_of_bufs = 0;
2092 ebuf = tx_info->bufs;
2094 /* Prepare TX context */
2095 memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
2096 memset(&ena_tx_ctx.ena_meta, 0x0,
2097 sizeof(struct ena_com_tx_meta));
2098 ena_tx_ctx.ena_bufs = ebuf;
2099 ena_tx_ctx.req_id = req_id;
2100 if (tx_ring->tx_mem_queue_type ==
2101 ENA_ADMIN_PLACEMENT_POLICY_DEV) {
2102 /* prepare the push buffer with
2103 * virtual address of the data
2105 ena_tx_ctx.header_len =
2106 RTE_MIN(mbuf->data_len,
2107 tx_ring->tx_max_header_size);
2108 ena_tx_ctx.push_header =
2109 (void *)((char *)mbuf->buf_addr +
2111 } /* there's no else as we take advantage of memset zeroing */
2113 /* Set TX offloads flags, if applicable */
2114 ena_tx_mbuf_prepare(mbuf, &ena_tx_ctx, tx_ring->offloads);
2116 if (unlikely(mbuf->ol_flags &
2117 (PKT_RX_L4_CKSUM_BAD | PKT_RX_IP_CKSUM_BAD)))
2118 rte_atomic64_inc(&tx_ring->adapter->drv_stats->ierrors);
2120 rte_prefetch0(tx_pkts[(sent_idx + 4) & ring_mask]);
2122 /* Process first segment taking into
2123 * consideration pushed header
2125 if (mbuf->data_len > ena_tx_ctx.header_len) {
2126 ebuf->paddr = mbuf->buf_iova +
2128 ena_tx_ctx.header_len;
2129 ebuf->len = mbuf->data_len - ena_tx_ctx.header_len;
2131 tx_info->num_of_bufs++;
2134 while ((mbuf = mbuf->next) != NULL) {
2135 ebuf->paddr = mbuf->buf_iova + mbuf->data_off;
2136 ebuf->len = mbuf->data_len;
2138 tx_info->num_of_bufs++;
2141 ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
2143 /* Write data to device */
2144 rc = ena_com_prepare_tx(tx_ring->ena_com_io_sq,
2145 &ena_tx_ctx, &nb_hw_desc);
2149 tx_info->tx_descs = nb_hw_desc;
2154 /* If there are ready packets to be xmitted... */
2156 /* ...let HW do its best :-) */
2158 ena_com_write_sq_doorbell(tx_ring->ena_com_io_sq);
2160 tx_ring->next_to_use = next_to_use;
2163 /* Clear complete packets */
2164 while (ena_com_tx_comp_req_id_get(tx_ring->ena_com_io_cq, &req_id) >= 0) {
2165 rc = validate_tx_req_id(tx_ring, req_id);
2169 /* Get Tx info & store how many descs were processed */
2170 tx_info = &tx_ring->tx_buffer_info[req_id];
2171 total_tx_descs += tx_info->tx_descs;
2173 /* Free whole mbuf chain */
2174 mbuf = tx_info->mbuf;
2175 rte_pktmbuf_free(mbuf);
2176 tx_info->mbuf = NULL;
2178 /* Put back descriptor to the ring for reuse */
2179 tx_ring->empty_tx_reqs[next_to_clean & ring_mask] = req_id;
2182 /* If too many descs to clean, leave it for another run */
2183 if (unlikely(total_tx_descs > ENA_RING_DESCS_RATIO(ring_size)))
2187 if (total_tx_descs > 0) {
2188 /* acknowledge completion of sent packets */
2189 ena_com_comp_ack(tx_ring->ena_com_io_sq, total_tx_descs);
2190 tx_ring->next_to_clean = next_to_clean;
2196 /*********************************************************************
2198 *********************************************************************/
2199 static int eth_ena_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2200 struct rte_pci_device *pci_dev)
2202 return rte_eth_dev_pci_generic_probe(pci_dev,
2203 sizeof(struct ena_adapter), eth_ena_dev_init);
2206 static int eth_ena_pci_remove(struct rte_pci_device *pci_dev)
2208 return rte_eth_dev_pci_generic_remove(pci_dev, eth_ena_dev_uninit);
2211 static struct rte_pci_driver rte_ena_pmd = {
2212 .id_table = pci_id_ena_map,
2213 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC |
2214 RTE_PCI_DRV_WC_ACTIVATE,
2215 .probe = eth_ena_pci_probe,
2216 .remove = eth_ena_pci_remove,
2219 RTE_PMD_REGISTER_PCI(net_ena, rte_ena_pmd);
2220 RTE_PMD_REGISTER_PCI_TABLE(net_ena, pci_id_ena_map);
2221 RTE_PMD_REGISTER_KMOD_DEP(net_ena, "* igb_uio | uio_pci_generic | vfio-pci");
2223 RTE_INIT(ena_init_log)
2225 ena_logtype_init = rte_log_register("pmd.net.ena.init");
2226 if (ena_logtype_init >= 0)
2227 rte_log_set_level(ena_logtype_init, RTE_LOG_NOTICE);
2228 ena_logtype_driver = rte_log_register("pmd.net.ena.driver");
2229 if (ena_logtype_driver >= 0)
2230 rte_log_set_level(ena_logtype_driver, RTE_LOG_NOTICE);
2233 /******************************************************************************
2234 ******************************** AENQ Handlers *******************************
2235 *****************************************************************************/
2236 static void ena_update_on_link_change(void *adapter_data,
2237 struct ena_admin_aenq_entry *aenq_e)
2239 struct rte_eth_dev *eth_dev;
2240 struct ena_adapter *adapter;
2241 struct ena_admin_aenq_link_change_desc *aenq_link_desc;
2244 adapter = (struct ena_adapter *)adapter_data;
2245 aenq_link_desc = (struct ena_admin_aenq_link_change_desc *)aenq_e;
2246 eth_dev = adapter->rte_dev;
2248 status = get_ena_admin_aenq_link_change_desc_link_status(aenq_link_desc);
2249 adapter->link_status = status;
2251 ena_link_update(eth_dev, 0);
2252 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
2255 static void ena_notification(void *data,
2256 struct ena_admin_aenq_entry *aenq_e)
2258 struct ena_adapter *adapter = (struct ena_adapter *)data;
2259 struct ena_admin_ena_hw_hints *hints;
2261 if (aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION)
2262 RTE_LOG(WARNING, PMD, "Invalid group(%x) expected %x\n",
2263 aenq_e->aenq_common_desc.group,
2264 ENA_ADMIN_NOTIFICATION);
2266 switch (aenq_e->aenq_common_desc.syndrom) {
2267 case ENA_ADMIN_UPDATE_HINTS:
2268 hints = (struct ena_admin_ena_hw_hints *)
2269 (&aenq_e->inline_data_w4);
2270 ena_update_hints(adapter, hints);
2273 RTE_LOG(ERR, PMD, "Invalid aenq notification link state %d\n",
2274 aenq_e->aenq_common_desc.syndrom);
2278 static void ena_keep_alive(void *adapter_data,
2279 __rte_unused struct ena_admin_aenq_entry *aenq_e)
2281 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
2283 adapter->timestamp_wd = rte_get_timer_cycles();
2287 * This handler will called for unknown event group or unimplemented handlers
2289 static void unimplemented_aenq_handler(__rte_unused void *data,
2290 __rte_unused struct ena_admin_aenq_entry *aenq_e)
2292 RTE_LOG(ERR, PMD, "Unknown event was received or event with "
2293 "unimplemented handler\n");
2296 static struct ena_aenq_handlers aenq_handlers = {
2298 [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
2299 [ENA_ADMIN_NOTIFICATION] = ena_notification,
2300 [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive
2302 .unimplemented_handler = unimplemented_aenq_handler