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.h>
37 #include <rte_atomic.h>
39 #include <rte_errno.h>
40 #include <rte_version.h>
41 #include <rte_eal_memconfig.h>
43 #include "ena_ethdev.h"
45 #include "ena_platform.h"
47 #include "ena_eth_com.h"
49 #include <ena_common_defs.h>
50 #include <ena_regs_defs.h>
51 #include <ena_admin_defs.h>
52 #include <ena_eth_io_defs.h>
54 #define DRV_MODULE_VER_MAJOR 1
55 #define DRV_MODULE_VER_MINOR 0
56 #define DRV_MODULE_VER_SUBMINOR 0
58 #define ENA_IO_TXQ_IDX(q) (2 * (q))
59 #define ENA_IO_RXQ_IDX(q) (2 * (q) + 1)
60 /*reverse version of ENA_IO_RXQ_IDX*/
61 #define ENA_IO_RXQ_IDX_REV(q) ((q - 1) / 2)
63 /* While processing submitted and completed descriptors (rx and tx path
64 * respectively) in a loop it is desired to:
65 * - perform batch submissions while populating sumbissmion queue
66 * - avoid blocking transmission of other packets during cleanup phase
67 * Hence the utilization ratio of 1/8 of a queue size.
69 #define ENA_RING_DESCS_RATIO(ring_size) (ring_size / 8)
71 #define __MERGE_64B_H_L(h, l) (((uint64_t)h << 32) | l)
72 #define TEST_BIT(val, bit_shift) (val & (1UL << bit_shift))
74 #define GET_L4_HDR_LEN(mbuf) \
75 ((rte_pktmbuf_mtod_offset(mbuf, struct tcp_hdr *, \
76 mbuf->l3_len + mbuf->l2_len)->data_off) >> 4)
78 #define ENA_RX_RSS_TABLE_LOG_SIZE 7
79 #define ENA_RX_RSS_TABLE_SIZE (1 << ENA_RX_RSS_TABLE_LOG_SIZE)
80 #define ENA_HASH_KEY_SIZE 40
81 #define ENA_ETH_SS_STATS 0xFF
82 #define ETH_GSTRING_LEN 32
84 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
86 enum ethtool_stringset {
92 char name[ETH_GSTRING_LEN];
96 #define ENA_STAT_ENA_COM_ENTRY(stat) { \
98 .stat_offset = offsetof(struct ena_com_stats_admin, stat) \
101 #define ENA_STAT_ENTRY(stat, stat_type) { \
103 .stat_offset = offsetof(struct ena_stats_##stat_type, stat) \
106 #define ENA_STAT_RX_ENTRY(stat) \
107 ENA_STAT_ENTRY(stat, rx)
109 #define ENA_STAT_TX_ENTRY(stat) \
110 ENA_STAT_ENTRY(stat, tx)
112 #define ENA_STAT_GLOBAL_ENTRY(stat) \
113 ENA_STAT_ENTRY(stat, dev)
115 static const struct ena_stats ena_stats_global_strings[] = {
116 ENA_STAT_GLOBAL_ENTRY(tx_timeout),
117 ENA_STAT_GLOBAL_ENTRY(io_suspend),
118 ENA_STAT_GLOBAL_ENTRY(io_resume),
119 ENA_STAT_GLOBAL_ENTRY(wd_expired),
120 ENA_STAT_GLOBAL_ENTRY(interface_up),
121 ENA_STAT_GLOBAL_ENTRY(interface_down),
122 ENA_STAT_GLOBAL_ENTRY(admin_q_pause),
125 static const struct ena_stats ena_stats_tx_strings[] = {
126 ENA_STAT_TX_ENTRY(cnt),
127 ENA_STAT_TX_ENTRY(bytes),
128 ENA_STAT_TX_ENTRY(queue_stop),
129 ENA_STAT_TX_ENTRY(queue_wakeup),
130 ENA_STAT_TX_ENTRY(dma_mapping_err),
131 ENA_STAT_TX_ENTRY(linearize),
132 ENA_STAT_TX_ENTRY(linearize_failed),
133 ENA_STAT_TX_ENTRY(tx_poll),
134 ENA_STAT_TX_ENTRY(doorbells),
135 ENA_STAT_TX_ENTRY(prepare_ctx_err),
136 ENA_STAT_TX_ENTRY(missing_tx_comp),
137 ENA_STAT_TX_ENTRY(bad_req_id),
140 static const struct ena_stats ena_stats_rx_strings[] = {
141 ENA_STAT_RX_ENTRY(cnt),
142 ENA_STAT_RX_ENTRY(bytes),
143 ENA_STAT_RX_ENTRY(refil_partial),
144 ENA_STAT_RX_ENTRY(bad_csum),
145 ENA_STAT_RX_ENTRY(page_alloc_fail),
146 ENA_STAT_RX_ENTRY(skb_alloc_fail),
147 ENA_STAT_RX_ENTRY(dma_mapping_err),
148 ENA_STAT_RX_ENTRY(bad_desc_num),
149 ENA_STAT_RX_ENTRY(small_copy_len_pkt),
152 static const struct ena_stats ena_stats_ena_com_strings[] = {
153 ENA_STAT_ENA_COM_ENTRY(aborted_cmd),
154 ENA_STAT_ENA_COM_ENTRY(submitted_cmd),
155 ENA_STAT_ENA_COM_ENTRY(completed_cmd),
156 ENA_STAT_ENA_COM_ENTRY(out_of_space),
157 ENA_STAT_ENA_COM_ENTRY(no_completion),
160 #define ENA_STATS_ARRAY_GLOBAL ARRAY_SIZE(ena_stats_global_strings)
161 #define ENA_STATS_ARRAY_TX ARRAY_SIZE(ena_stats_tx_strings)
162 #define ENA_STATS_ARRAY_RX ARRAY_SIZE(ena_stats_rx_strings)
163 #define ENA_STATS_ARRAY_ENA_COM ARRAY_SIZE(ena_stats_ena_com_strings)
165 /** Vendor ID used by Amazon devices */
166 #define PCI_VENDOR_ID_AMAZON 0x1D0F
167 /** Amazon devices */
168 #define PCI_DEVICE_ID_ENA_VF 0xEC20
169 #define PCI_DEVICE_ID_ENA_LLQ_VF 0xEC21
171 static struct rte_pci_id pci_id_ena_map[] = {
172 { RTE_PCI_DEVICE(PCI_VENDOR_ID_AMAZON, PCI_DEVICE_ID_ENA_VF) },
173 { RTE_PCI_DEVICE(PCI_VENDOR_ID_AMAZON, PCI_DEVICE_ID_ENA_LLQ_VF) },
177 static int ena_device_init(struct ena_com_dev *ena_dev,
178 struct ena_com_dev_get_features_ctx *get_feat_ctx);
179 static int ena_dev_configure(struct rte_eth_dev *dev);
180 static uint16_t eth_ena_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
182 static int ena_tx_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_txconf *tx_conf);
185 static int ena_rx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
186 uint16_t nb_desc, unsigned int socket_id,
187 const struct rte_eth_rxconf *rx_conf,
188 struct rte_mempool *mp);
189 static uint16_t eth_ena_recv_pkts(void *rx_queue,
190 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
191 static int ena_populate_rx_queue(struct ena_ring *rxq, unsigned int count);
192 static void ena_init_rings(struct ena_adapter *adapter);
193 static int ena_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
194 static int ena_start(struct rte_eth_dev *dev);
195 static void ena_close(struct rte_eth_dev *dev);
196 static void ena_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats);
197 static void ena_rx_queue_release_all(struct rte_eth_dev *dev);
198 static void ena_tx_queue_release_all(struct rte_eth_dev *dev);
199 static void ena_rx_queue_release(void *queue);
200 static void ena_tx_queue_release(void *queue);
201 static void ena_rx_queue_release_bufs(struct ena_ring *ring);
202 static void ena_tx_queue_release_bufs(struct ena_ring *ring);
203 static int ena_link_update(struct rte_eth_dev *dev,
204 __rte_unused int wait_to_complete);
205 static int ena_queue_restart(struct ena_ring *ring);
206 static int ena_queue_restart_all(struct rte_eth_dev *dev,
207 enum ena_ring_type ring_type);
208 static void ena_stats_restart(struct rte_eth_dev *dev);
209 static void ena_infos_get(__rte_unused struct rte_eth_dev *dev,
210 struct rte_eth_dev_info *dev_info);
211 static int ena_rss_reta_update(struct rte_eth_dev *dev,
212 struct rte_eth_rss_reta_entry64 *reta_conf,
214 static int ena_rss_reta_query(struct rte_eth_dev *dev,
215 struct rte_eth_rss_reta_entry64 *reta_conf,
217 static int ena_get_sset_count(struct rte_eth_dev *dev, int sset);
219 static struct eth_dev_ops ena_dev_ops = {
220 .dev_configure = ena_dev_configure,
221 .dev_infos_get = ena_infos_get,
222 .rx_queue_setup = ena_rx_queue_setup,
223 .tx_queue_setup = ena_tx_queue_setup,
224 .dev_start = ena_start,
225 .link_update = ena_link_update,
226 .stats_get = ena_stats_get,
227 .mtu_set = ena_mtu_set,
228 .rx_queue_release = ena_rx_queue_release,
229 .tx_queue_release = ena_tx_queue_release,
230 .dev_close = ena_close,
231 .reta_update = ena_rss_reta_update,
232 .reta_query = ena_rss_reta_query,
235 #define NUMA_NO_NODE SOCKET_ID_ANY
237 static inline int ena_cpu_to_node(int cpu)
239 struct rte_config *config = rte_eal_get_configuration();
241 if (likely(cpu < RTE_MAX_MEMZONE))
242 return config->mem_config->memzone[cpu].socket_id;
247 static inline void ena_rx_mbuf_prepare(struct rte_mbuf *mbuf,
248 struct ena_com_rx_ctx *ena_rx_ctx)
250 uint64_t ol_flags = 0;
252 if (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP)
253 ol_flags |= PKT_TX_TCP_CKSUM;
254 else if (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)
255 ol_flags |= PKT_TX_UDP_CKSUM;
257 if (ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4)
258 ol_flags |= PKT_TX_IPV4;
259 else if (ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV6)
260 ol_flags |= PKT_TX_IPV6;
262 if (unlikely(ena_rx_ctx->l4_csum_err))
263 ol_flags |= PKT_RX_L4_CKSUM_BAD;
264 if (unlikely(ena_rx_ctx->l3_csum_err))
265 ol_flags |= PKT_RX_IP_CKSUM_BAD;
267 mbuf->ol_flags = ol_flags;
270 static inline void ena_tx_mbuf_prepare(struct rte_mbuf *mbuf,
271 struct ena_com_tx_ctx *ena_tx_ctx)
273 struct ena_com_tx_meta *ena_meta = &ena_tx_ctx->ena_meta;
276 (PKT_TX_L4_MASK | PKT_TX_IP_CKSUM | PKT_TX_TCP_SEG)) {
277 /* check if TSO is required */
278 if (mbuf->ol_flags & PKT_TX_TCP_SEG) {
279 ena_tx_ctx->tso_enable = true;
281 ena_meta->l4_hdr_len = GET_L4_HDR_LEN(mbuf);
284 /* check if L3 checksum is needed */
285 if (mbuf->ol_flags & PKT_TX_IP_CKSUM)
286 ena_tx_ctx->l3_csum_enable = true;
288 if (mbuf->ol_flags & PKT_TX_IPV6) {
289 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
291 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
293 /* set don't fragment (DF) flag */
294 if (mbuf->packet_type &
295 (RTE_PTYPE_L4_NONFRAG
296 | RTE_PTYPE_INNER_L4_NONFRAG))
297 ena_tx_ctx->df = true;
300 /* check if L4 checksum is needed */
301 switch (mbuf->ol_flags & PKT_TX_L4_MASK) {
302 case PKT_TX_TCP_CKSUM:
303 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
304 ena_tx_ctx->l4_csum_enable = true;
306 case PKT_TX_UDP_CKSUM:
307 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
308 ena_tx_ctx->l4_csum_enable = true;
311 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UNKNOWN;
312 ena_tx_ctx->l4_csum_enable = false;
316 ena_meta->mss = mbuf->tso_segsz;
317 ena_meta->l3_hdr_len = mbuf->l3_len;
318 ena_meta->l3_hdr_offset = mbuf->l2_len;
319 /* this param needed only for TSO */
320 ena_meta->l3_outer_hdr_len = 0;
321 ena_meta->l3_outer_hdr_offset = 0;
323 ena_tx_ctx->meta_valid = true;
325 ena_tx_ctx->meta_valid = false;
329 static void ena_config_host_info(struct ena_com_dev *ena_dev)
331 struct ena_admin_host_info *host_info;
334 /* Allocate only the host info */
335 rc = ena_com_allocate_host_info(ena_dev);
337 RTE_LOG(ERR, PMD, "Cannot allocate host info\n");
341 host_info = ena_dev->host_attr.host_info;
343 host_info->os_type = ENA_ADMIN_OS_DPDK;
344 host_info->kernel_ver = RTE_VERSION;
345 snprintf((char *)host_info->kernel_ver_str,
346 sizeof(host_info->kernel_ver_str),
347 "%s", rte_version());
348 host_info->os_dist = RTE_VERSION;
349 snprintf((char *)host_info->os_dist_str,
350 sizeof(host_info->os_dist_str),
351 "%s", rte_version());
352 host_info->driver_version =
353 (DRV_MODULE_VER_MAJOR) |
354 (DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
355 (DRV_MODULE_VER_SUBMINOR <<
356 ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
358 rc = ena_com_set_host_attributes(ena_dev);
360 RTE_LOG(ERR, PMD, "Cannot set host attributes\n");
368 ena_com_delete_host_info(ena_dev);
372 ena_get_sset_count(struct rte_eth_dev *dev, int sset)
374 if (sset != ETH_SS_STATS)
377 /* Workaround for clang:
378 * touch internal structures to prevent
381 ENA_TOUCH(ena_stats_global_strings);
382 ENA_TOUCH(ena_stats_tx_strings);
383 ENA_TOUCH(ena_stats_rx_strings);
384 ENA_TOUCH(ena_stats_ena_com_strings);
386 return dev->data->nb_tx_queues *
387 (ENA_STATS_ARRAY_TX + ENA_STATS_ARRAY_RX) +
388 ENA_STATS_ARRAY_GLOBAL + ENA_STATS_ARRAY_ENA_COM;
391 static void ena_config_debug_area(struct ena_adapter *adapter)
396 ss_count = ena_get_sset_count(adapter->rte_dev, ETH_SS_STATS);
398 RTE_LOG(ERR, PMD, "SS count is negative\n");
402 /* allocate 32 bytes for each string and 64bit for the value */
403 debug_area_size = ss_count * ETH_GSTRING_LEN + sizeof(u64) * ss_count;
405 rc = ena_com_allocate_debug_area(&adapter->ena_dev, debug_area_size);
407 RTE_LOG(ERR, PMD, "Cannot allocate debug area\n");
411 rc = ena_com_set_host_attributes(&adapter->ena_dev);
413 RTE_LOG(WARNING, PMD, "Cannot set host attributes\n");
420 ena_com_delete_debug_area(&adapter->ena_dev);
423 static void ena_close(struct rte_eth_dev *dev)
425 struct ena_adapter *adapter =
426 (struct ena_adapter *)(dev->data->dev_private);
428 adapter->state = ENA_ADAPTER_STATE_STOPPED;
430 ena_rx_queue_release_all(dev);
431 ena_tx_queue_release_all(dev);
434 static int ena_rss_reta_update(struct rte_eth_dev *dev,
435 struct rte_eth_rss_reta_entry64 *reta_conf,
438 struct ena_adapter *adapter =
439 (struct ena_adapter *)(dev->data->dev_private);
440 struct ena_com_dev *ena_dev = &adapter->ena_dev;
446 if ((reta_size == 0) || (reta_conf == NULL))
449 if (reta_size > ENA_RX_RSS_TABLE_SIZE) {
450 RTE_LOG(WARNING, PMD,
451 "indirection table %d is bigger than supported (%d)\n",
452 reta_size, ENA_RX_RSS_TABLE_SIZE);
457 for (i = 0 ; i < reta_size ; i++) {
458 /* each reta_conf is for 64 entries.
459 * to support 128 we use 2 conf of 64
461 conf_idx = i / RTE_RETA_GROUP_SIZE;
462 idx = i % RTE_RETA_GROUP_SIZE;
463 if (TEST_BIT(reta_conf[conf_idx].mask, idx)) {
465 ENA_IO_RXQ_IDX(reta_conf[conf_idx].reta[idx]);
466 ret = ena_com_indirect_table_fill_entry(ena_dev,
469 if (unlikely(ret && (ret != ENA_COM_PERMISSION))) {
471 "Cannot fill indirect table\n");
478 ret = ena_com_indirect_table_set(ena_dev);
479 if (unlikely(ret && (ret != ENA_COM_PERMISSION))) {
480 RTE_LOG(ERR, PMD, "Cannot flush the indirect table\n");
485 RTE_LOG(DEBUG, PMD, "%s(): RSS configured %d entries for port %d\n",
486 __func__, reta_size, adapter->rte_dev->data->port_id);
491 /* Query redirection table. */
492 static int ena_rss_reta_query(struct rte_eth_dev *dev,
493 struct rte_eth_rss_reta_entry64 *reta_conf,
496 struct ena_adapter *adapter =
497 (struct ena_adapter *)(dev->data->dev_private);
498 struct ena_com_dev *ena_dev = &adapter->ena_dev;
501 u32 indirect_table[ENA_RX_RSS_TABLE_SIZE] = {0};
505 if (reta_size == 0 || reta_conf == NULL ||
506 (reta_size > RTE_RETA_GROUP_SIZE && ((reta_conf + 1) == NULL)))
509 ret = ena_com_indirect_table_get(ena_dev, indirect_table);
510 if (unlikely(ret && (ret != ENA_COM_PERMISSION))) {
511 RTE_LOG(ERR, PMD, "cannot get indirect table\n");
516 for (i = 0 ; i < reta_size ; i++) {
517 reta_conf_idx = i / RTE_RETA_GROUP_SIZE;
518 reta_idx = i % RTE_RETA_GROUP_SIZE;
519 if (TEST_BIT(reta_conf[reta_conf_idx].mask, reta_idx))
520 reta_conf[reta_conf_idx].reta[reta_idx] =
521 ENA_IO_RXQ_IDX_REV(indirect_table[i]);
527 static int ena_rss_init_default(struct ena_adapter *adapter)
529 struct ena_com_dev *ena_dev = &adapter->ena_dev;
530 uint16_t nb_rx_queues = adapter->rte_dev->data->nb_rx_queues;
534 rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
536 RTE_LOG(ERR, PMD, "Cannot init indirect table\n");
540 for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
541 val = i % nb_rx_queues;
542 rc = ena_com_indirect_table_fill_entry(ena_dev, i,
543 ENA_IO_RXQ_IDX(val));
544 if (unlikely(rc && (rc != ENA_COM_PERMISSION))) {
545 RTE_LOG(ERR, PMD, "Cannot fill indirect table\n");
550 rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
551 ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
552 if (unlikely(rc && (rc != ENA_COM_PERMISSION))) {
553 RTE_LOG(INFO, PMD, "Cannot fill hash function\n");
557 rc = ena_com_set_default_hash_ctrl(ena_dev);
558 if (unlikely(rc && (rc != ENA_COM_PERMISSION))) {
559 RTE_LOG(INFO, PMD, "Cannot fill hash control\n");
563 rc = ena_com_indirect_table_set(ena_dev);
564 if (unlikely(rc && (rc != ENA_COM_PERMISSION))) {
565 RTE_LOG(ERR, PMD, "Cannot flush the indirect table\n");
568 RTE_LOG(DEBUG, PMD, "RSS configured for port %d\n",
569 adapter->rte_dev->data->port_id);
574 ena_com_rss_destroy(ena_dev);
580 static void ena_rx_queue_release_all(struct rte_eth_dev *dev)
582 struct ena_ring **queues = (struct ena_ring **)dev->data->rx_queues;
583 int nb_queues = dev->data->nb_rx_queues;
586 for (i = 0; i < nb_queues; i++)
587 ena_rx_queue_release(queues[i]);
590 static void ena_tx_queue_release_all(struct rte_eth_dev *dev)
592 struct ena_ring **queues = (struct ena_ring **)dev->data->tx_queues;
593 int nb_queues = dev->data->nb_tx_queues;
596 for (i = 0; i < nb_queues; i++)
597 ena_tx_queue_release(queues[i]);
600 static void ena_rx_queue_release(void *queue)
602 struct ena_ring *ring = (struct ena_ring *)queue;
603 struct ena_adapter *adapter = ring->adapter;
606 ena_assert_msg(ring->configured,
607 "API violation - releasing not configured queue");
608 ena_assert_msg(ring->adapter->state != ENA_ADAPTER_STATE_RUNNING,
611 /* Destroy HW queue */
612 ena_qid = ENA_IO_RXQ_IDX(ring->id);
613 ena_com_destroy_io_queue(&adapter->ena_dev, ena_qid);
616 ena_rx_queue_release_bufs(ring);
618 /* Free ring resources */
619 if (ring->rx_buffer_info)
620 rte_free(ring->rx_buffer_info);
621 ring->rx_buffer_info = NULL;
623 ring->configured = 0;
625 RTE_LOG(NOTICE, PMD, "RX Queue %d:%d released\n",
626 ring->port_id, ring->id);
629 static void ena_tx_queue_release(void *queue)
631 struct ena_ring *ring = (struct ena_ring *)queue;
632 struct ena_adapter *adapter = ring->adapter;
635 ena_assert_msg(ring->configured,
636 "API violation. Releasing not configured queue");
637 ena_assert_msg(ring->adapter->state != ENA_ADAPTER_STATE_RUNNING,
640 /* Destroy HW queue */
641 ena_qid = ENA_IO_TXQ_IDX(ring->id);
642 ena_com_destroy_io_queue(&adapter->ena_dev, ena_qid);
645 ena_tx_queue_release_bufs(ring);
647 /* Free ring resources */
648 if (ring->tx_buffer_info)
649 rte_free(ring->tx_buffer_info);
651 if (ring->empty_tx_reqs)
652 rte_free(ring->empty_tx_reqs);
654 ring->empty_tx_reqs = NULL;
655 ring->tx_buffer_info = NULL;
657 ring->configured = 0;
659 RTE_LOG(NOTICE, PMD, "TX Queue %d:%d released\n",
660 ring->port_id, ring->id);
663 static void ena_rx_queue_release_bufs(struct ena_ring *ring)
665 unsigned int ring_mask = ring->ring_size - 1;
667 while (ring->next_to_clean != ring->next_to_use) {
669 ring->rx_buffer_info[ring->next_to_clean & ring_mask];
672 __rte_mbuf_raw_free(m);
674 ring->next_to_clean++;
678 static void ena_tx_queue_release_bufs(struct ena_ring *ring)
682 for (i = 0; i < ring->ring_size; ++i) {
683 struct ena_tx_buffer *tx_buf = &ring->tx_buffer_info[i];
686 rte_pktmbuf_free(tx_buf->mbuf);
688 ring->next_to_clean++;
692 static int ena_link_update(struct rte_eth_dev *dev,
693 __rte_unused int wait_to_complete)
695 struct rte_eth_link *link = &dev->data->dev_link;
697 link->link_status = 1;
698 link->link_speed = ETH_SPEED_NUM_10G;
699 link->link_duplex = ETH_LINK_FULL_DUPLEX;
704 static int ena_queue_restart_all(struct rte_eth_dev *dev,
705 enum ena_ring_type ring_type)
707 struct ena_adapter *adapter =
708 (struct ena_adapter *)(dev->data->dev_private);
709 struct ena_ring *queues = NULL;
713 queues = (ring_type == ENA_RING_TYPE_RX) ?
714 adapter->rx_ring : adapter->tx_ring;
716 for (i = 0; i < adapter->num_queues; i++) {
717 if (queues[i].configured) {
718 if (ring_type == ENA_RING_TYPE_RX) {
720 dev->data->rx_queues[i] == &queues[i],
721 "Inconsistent state of rx queues\n");
724 dev->data->tx_queues[i] == &queues[i],
725 "Inconsistent state of tx queues\n");
728 rc = ena_queue_restart(&queues[i]);
732 "failed to restart queue %d type(%d)\n",
742 static uint32_t ena_get_mtu_conf(struct ena_adapter *adapter)
744 uint32_t max_frame_len = adapter->max_mtu;
746 if (adapter->rte_eth_dev_data->dev_conf.rxmode.jumbo_frame == 1)
748 adapter->rte_eth_dev_data->dev_conf.rxmode.max_rx_pkt_len;
750 return max_frame_len;
753 static int ena_check_valid_conf(struct ena_adapter *adapter)
755 uint32_t max_frame_len = ena_get_mtu_conf(adapter);
757 if (max_frame_len > adapter->max_mtu) {
758 PMD_INIT_LOG(ERR, "Unsupported MTU of %d\n", max_frame_len);
766 ena_calc_queue_size(struct ena_com_dev *ena_dev,
767 struct ena_com_dev_get_features_ctx *get_feat_ctx)
769 uint32_t queue_size = ENA_DEFAULT_RING_SIZE;
771 queue_size = RTE_MIN(queue_size,
772 get_feat_ctx->max_queues.max_cq_depth);
773 queue_size = RTE_MIN(queue_size,
774 get_feat_ctx->max_queues.max_sq_depth);
776 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
777 queue_size = RTE_MIN(queue_size,
778 get_feat_ctx->max_queues.max_llq_depth);
780 /* Round down to power of 2 */
781 if (!rte_is_power_of_2(queue_size))
782 queue_size = rte_align32pow2(queue_size >> 1);
784 if (queue_size == 0) {
785 PMD_INIT_LOG(ERR, "Invalid queue size\n");
792 static void ena_stats_restart(struct rte_eth_dev *dev)
794 struct ena_adapter *adapter =
795 (struct ena_adapter *)(dev->data->dev_private);
797 rte_atomic64_init(&adapter->drv_stats->ierrors);
798 rte_atomic64_init(&adapter->drv_stats->oerrors);
799 rte_atomic64_init(&adapter->drv_stats->rx_nombuf);
802 static void ena_stats_get(struct rte_eth_dev *dev,
803 struct rte_eth_stats *stats)
805 struct ena_admin_basic_stats ena_stats;
806 struct ena_adapter *adapter =
807 (struct ena_adapter *)(dev->data->dev_private);
808 struct ena_com_dev *ena_dev = &adapter->ena_dev;
811 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
814 memset(&ena_stats, 0, sizeof(ena_stats));
815 rc = ena_com_get_dev_basic_stats(ena_dev, &ena_stats);
817 RTE_LOG(ERR, PMD, "Could not retrieve statistics from ENA");
821 /* Set of basic statistics from ENA */
822 stats->ipackets = __MERGE_64B_H_L(ena_stats.rx_pkts_high,
823 ena_stats.rx_pkts_low);
824 stats->opackets = __MERGE_64B_H_L(ena_stats.tx_pkts_high,
825 ena_stats.tx_pkts_low);
826 stats->ibytes = __MERGE_64B_H_L(ena_stats.rx_bytes_high,
827 ena_stats.rx_bytes_low);
828 stats->obytes = __MERGE_64B_H_L(ena_stats.tx_bytes_high,
829 ena_stats.tx_bytes_low);
830 stats->imissed = __MERGE_64B_H_L(ena_stats.rx_drops_high,
831 ena_stats.rx_drops_low);
833 /* Driver related stats */
834 stats->ierrors = rte_atomic64_read(&adapter->drv_stats->ierrors);
835 stats->oerrors = rte_atomic64_read(&adapter->drv_stats->oerrors);
836 stats->rx_nombuf = rte_atomic64_read(&adapter->drv_stats->rx_nombuf);
839 static int ena_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
841 struct ena_adapter *adapter;
842 struct ena_com_dev *ena_dev;
845 ena_assert_msg(dev->data != NULL, "Uninitialized device");
846 ena_assert_msg(dev->data->dev_private != NULL, "Uninitialized device");
847 adapter = (struct ena_adapter *)(dev->data->dev_private);
849 ena_dev = &adapter->ena_dev;
850 ena_assert_msg(ena_dev != NULL, "Uninitialized device");
852 if (mtu > ena_get_mtu_conf(adapter)) {
854 "Given MTU (%d) exceeds maximum MTU supported (%d)\n",
855 mtu, ena_get_mtu_conf(adapter));
860 rc = ena_com_set_dev_mtu(ena_dev, mtu);
862 RTE_LOG(ERR, PMD, "Could not set MTU: %d\n", mtu);
864 RTE_LOG(NOTICE, PMD, "Set MTU: %d\n", mtu);
870 static int ena_start(struct rte_eth_dev *dev)
872 struct ena_adapter *adapter =
873 (struct ena_adapter *)(dev->data->dev_private);
876 if (!(adapter->state == ENA_ADAPTER_STATE_CONFIG ||
877 adapter->state == ENA_ADAPTER_STATE_STOPPED)) {
878 PMD_INIT_LOG(ERR, "API violation");
882 rc = ena_check_valid_conf(adapter);
886 rc = ena_queue_restart_all(dev, ENA_RING_TYPE_RX);
890 rc = ena_queue_restart_all(dev, ENA_RING_TYPE_TX);
894 if (adapter->rte_dev->data->dev_conf.rxmode.mq_mode &
895 ETH_MQ_RX_RSS_FLAG) {
896 rc = ena_rss_init_default(adapter);
901 ena_stats_restart(dev);
903 adapter->state = ENA_ADAPTER_STATE_RUNNING;
908 static int ena_queue_restart(struct ena_ring *ring)
912 ena_assert_msg(ring->configured == 1,
913 "Trying to restart unconfigured queue\n");
915 ring->next_to_clean = 0;
916 ring->next_to_use = 0;
918 if (ring->type == ENA_RING_TYPE_TX)
921 bufs_num = ring->ring_size - 1;
922 rc = ena_populate_rx_queue(ring, bufs_num);
923 if (rc != bufs_num) {
924 PMD_INIT_LOG(ERR, "Failed to populate rx ring !");
931 static int ena_tx_queue_setup(struct rte_eth_dev *dev,
934 __rte_unused unsigned int socket_id,
935 __rte_unused const struct rte_eth_txconf *tx_conf)
937 struct ena_com_create_io_ctx ctx =
938 /* policy set to _HOST just to satisfy icc compiler */
939 { ENA_ADMIN_PLACEMENT_POLICY_HOST,
940 ENA_COM_IO_QUEUE_DIRECTION_TX, 0, 0, 0, 0 };
941 struct ena_ring *txq = NULL;
942 struct ena_adapter *adapter =
943 (struct ena_adapter *)(dev->data->dev_private);
947 struct ena_com_dev *ena_dev = &adapter->ena_dev;
949 txq = &adapter->tx_ring[queue_idx];
951 if (txq->configured) {
953 "API violation. Queue %d is already configured\n",
958 if (!rte_is_power_of_2(nb_desc)) {
960 "Unsupported size of RX queue: %d is not a power of 2.",
965 if (nb_desc > adapter->tx_ring_size) {
967 "Unsupported size of TX queue (max size: %d)\n",
968 adapter->tx_ring_size);
972 ena_qid = ENA_IO_TXQ_IDX(queue_idx);
974 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
976 ctx.msix_vector = -1; /* admin interrupts not used */
977 ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
978 ctx.queue_size = adapter->tx_ring_size;
979 ctx.numa_node = ena_cpu_to_node(queue_idx);
981 rc = ena_com_create_io_queue(ena_dev, &ctx);
984 "failed to create io TX queue #%d (qid:%d) rc: %d\n",
985 queue_idx, ena_qid, rc);
987 txq->ena_com_io_cq = &ena_dev->io_cq_queues[ena_qid];
988 txq->ena_com_io_sq = &ena_dev->io_sq_queues[ena_qid];
990 rc = ena_com_get_io_handlers(ena_dev, ena_qid,
992 &txq->ena_com_io_cq);
995 "Failed to get TX queue handlers. TX queue num %d rc: %d\n",
997 ena_com_destroy_io_queue(ena_dev, ena_qid);
1001 txq->port_id = dev->data->port_id;
1002 txq->next_to_clean = 0;
1003 txq->next_to_use = 0;
1004 txq->ring_size = nb_desc;
1006 txq->tx_buffer_info = rte_zmalloc("txq->tx_buffer_info",
1007 sizeof(struct ena_tx_buffer) *
1009 RTE_CACHE_LINE_SIZE);
1010 if (!txq->tx_buffer_info) {
1011 RTE_LOG(ERR, PMD, "failed to alloc mem for tx buffer info\n");
1015 txq->empty_tx_reqs = rte_zmalloc("txq->empty_tx_reqs",
1016 sizeof(u16) * txq->ring_size,
1017 RTE_CACHE_LINE_SIZE);
1018 if (!txq->empty_tx_reqs) {
1019 RTE_LOG(ERR, PMD, "failed to alloc mem for tx reqs\n");
1020 rte_free(txq->tx_buffer_info);
1023 for (i = 0; i < txq->ring_size; i++)
1024 txq->empty_tx_reqs[i] = i;
1026 /* Store pointer to this queue in upper layer */
1027 txq->configured = 1;
1028 dev->data->tx_queues[queue_idx] = txq;
1033 static int ena_rx_queue_setup(struct rte_eth_dev *dev,
1036 __rte_unused unsigned int socket_id,
1037 __rte_unused const struct rte_eth_rxconf *rx_conf,
1038 struct rte_mempool *mp)
1040 struct ena_com_create_io_ctx ctx =
1041 /* policy set to _HOST just to satisfy icc compiler */
1042 { ENA_ADMIN_PLACEMENT_POLICY_HOST,
1043 ENA_COM_IO_QUEUE_DIRECTION_RX, 0, 0, 0, 0 };
1044 struct ena_adapter *adapter =
1045 (struct ena_adapter *)(dev->data->dev_private);
1046 struct ena_ring *rxq = NULL;
1047 uint16_t ena_qid = 0;
1049 struct ena_com_dev *ena_dev = &adapter->ena_dev;
1051 rxq = &adapter->rx_ring[queue_idx];
1052 if (rxq->configured) {
1054 "API violation. Queue %d is already configured\n",
1059 if (!rte_is_power_of_2(nb_desc)) {
1061 "Unsupported size of TX queue: %d is not a power of 2.",
1066 if (nb_desc > adapter->rx_ring_size) {
1068 "Unsupported size of RX queue (max size: %d)\n",
1069 adapter->rx_ring_size);
1073 ena_qid = ENA_IO_RXQ_IDX(queue_idx);
1076 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
1077 ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
1078 ctx.msix_vector = -1; /* admin interrupts not used */
1079 ctx.queue_size = adapter->rx_ring_size;
1080 ctx.numa_node = ena_cpu_to_node(queue_idx);
1082 rc = ena_com_create_io_queue(ena_dev, &ctx);
1084 RTE_LOG(ERR, PMD, "failed to create io RX queue #%d rc: %d\n",
1087 rxq->ena_com_io_cq = &ena_dev->io_cq_queues[ena_qid];
1088 rxq->ena_com_io_sq = &ena_dev->io_sq_queues[ena_qid];
1090 rc = ena_com_get_io_handlers(ena_dev, ena_qid,
1091 &rxq->ena_com_io_sq,
1092 &rxq->ena_com_io_cq);
1095 "Failed to get RX queue handlers. RX queue num %d rc: %d\n",
1097 ena_com_destroy_io_queue(ena_dev, ena_qid);
1100 rxq->port_id = dev->data->port_id;
1101 rxq->next_to_clean = 0;
1102 rxq->next_to_use = 0;
1103 rxq->ring_size = nb_desc;
1106 rxq->rx_buffer_info = rte_zmalloc("rxq->buffer_info",
1107 sizeof(struct rte_mbuf *) * nb_desc,
1108 RTE_CACHE_LINE_SIZE);
1109 if (!rxq->rx_buffer_info) {
1110 RTE_LOG(ERR, PMD, "failed to alloc mem for rx buffer info\n");
1114 /* Store pointer to this queue in upper layer */
1115 rxq->configured = 1;
1116 dev->data->rx_queues[queue_idx] = rxq;
1121 static int ena_populate_rx_queue(struct ena_ring *rxq, unsigned int count)
1125 uint16_t ring_size = rxq->ring_size;
1126 uint16_t ring_mask = ring_size - 1;
1127 uint16_t next_to_use = rxq->next_to_use;
1129 struct rte_mbuf **mbufs = &rxq->rx_buffer_info[0];
1131 if (unlikely(!count))
1134 in_use = rxq->next_to_use - rxq->next_to_clean;
1135 ena_assert_msg(((in_use + count) < ring_size), "bad ring state");
1137 count = RTE_MIN(count,
1138 (uint16_t)(ring_size - (next_to_use & ring_mask)));
1140 /* get resources for incoming packets */
1141 rc = rte_mempool_get_bulk(rxq->mb_pool,
1142 (void **)(&mbufs[next_to_use & ring_mask]),
1144 if (unlikely(rc < 0)) {
1145 rte_atomic64_inc(&rxq->adapter->drv_stats->rx_nombuf);
1146 PMD_RX_LOG(DEBUG, "there are no enough free buffers");
1150 for (i = 0; i < count; i++) {
1151 uint16_t next_to_use_masked = next_to_use & ring_mask;
1152 struct rte_mbuf *mbuf = mbufs[next_to_use_masked];
1153 struct ena_com_buf ebuf;
1155 rte_prefetch0(mbufs[((next_to_use + 4) & ring_mask)]);
1156 /* prepare physical address for DMA transaction */
1157 ebuf.paddr = mbuf->buf_physaddr + RTE_PKTMBUF_HEADROOM;
1158 ebuf.len = mbuf->buf_len - RTE_PKTMBUF_HEADROOM;
1159 /* pass resource to device */
1160 rc = ena_com_add_single_rx_desc(rxq->ena_com_io_sq,
1161 &ebuf, next_to_use_masked);
1163 rte_mempool_put_bulk(rxq->mb_pool, (void **)(&mbuf),
1165 RTE_LOG(WARNING, PMD, "failed adding rx desc\n");
1171 /* When we submitted free recources to device... */
1173 /* ...let HW know that it can fill buffers with data */
1175 ena_com_write_sq_doorbell(rxq->ena_com_io_sq);
1177 rxq->next_to_use = next_to_use;
1183 static int ena_device_init(struct ena_com_dev *ena_dev,
1184 struct ena_com_dev_get_features_ctx *get_feat_ctx)
1187 bool readless_supported;
1189 /* Initialize mmio registers */
1190 rc = ena_com_mmio_reg_read_request_init(ena_dev);
1192 RTE_LOG(ERR, PMD, "failed to init mmio read less\n");
1196 /* The PCIe configuration space revision id indicate if mmio reg
1199 readless_supported =
1200 !(((struct rte_pci_device *)ena_dev->dmadev)->id.class_id
1201 & ENA_MMIO_DISABLE_REG_READ);
1202 ena_com_set_mmio_read_mode(ena_dev, readless_supported);
1205 rc = ena_com_dev_reset(ena_dev);
1207 RTE_LOG(ERR, PMD, "cannot reset device\n");
1208 goto err_mmio_read_less;
1211 /* check FW version */
1212 rc = ena_com_validate_version(ena_dev);
1214 RTE_LOG(ERR, PMD, "device version is too low\n");
1215 goto err_mmio_read_less;
1218 ena_dev->dma_addr_bits = ena_com_get_dma_width(ena_dev);
1220 /* ENA device administration layer init */
1221 rc = ena_com_admin_init(ena_dev, NULL, true);
1224 "cannot initialize ena admin queue with device\n");
1225 goto err_mmio_read_less;
1228 /* To enable the msix interrupts the driver needs to know the number
1229 * of queues. So the driver uses polling mode to retrieve this
1232 ena_com_set_admin_polling_mode(ena_dev, true);
1234 ena_config_host_info(ena_dev);
1236 /* Get Device Attributes and features */
1237 rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
1240 "cannot get attribute for ena device rc= %d\n", rc);
1241 goto err_admin_init;
1247 ena_com_admin_destroy(ena_dev);
1250 ena_com_mmio_reg_read_request_destroy(ena_dev);
1255 static int eth_ena_dev_init(struct rte_eth_dev *eth_dev)
1257 struct rte_pci_device *pci_dev;
1258 struct ena_adapter *adapter =
1259 (struct ena_adapter *)(eth_dev->data->dev_private);
1260 struct ena_com_dev *ena_dev = &adapter->ena_dev;
1261 struct ena_com_dev_get_features_ctx get_feat_ctx;
1264 static int adapters_found;
1266 memset(adapter, 0, sizeof(struct ena_adapter));
1267 ena_dev = &adapter->ena_dev;
1269 eth_dev->dev_ops = &ena_dev_ops;
1270 eth_dev->rx_pkt_burst = ð_ena_recv_pkts;
1271 eth_dev->tx_pkt_burst = ð_ena_xmit_pkts;
1272 adapter->rte_eth_dev_data = eth_dev->data;
1273 adapter->rte_dev = eth_dev;
1275 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1278 pci_dev = eth_dev->pci_dev;
1279 adapter->pdev = pci_dev;
1281 PMD_INIT_LOG(INFO, "Initializing %x:%x:%x.%d\n",
1282 pci_dev->addr.domain,
1284 pci_dev->addr.devid,
1285 pci_dev->addr.function);
1287 adapter->regs = pci_dev->mem_resource[ENA_REGS_BAR].addr;
1288 adapter->dev_mem_base = pci_dev->mem_resource[ENA_MEM_BAR].addr;
1290 /* Present ENA_MEM_BAR indicates available LLQ mode.
1291 * Use corresponding policy
1293 if (adapter->dev_mem_base)
1294 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_DEV;
1295 else if (adapter->regs)
1296 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
1298 PMD_INIT_LOG(CRIT, "Failed to access registers BAR(%d)\n",
1301 ena_dev->reg_bar = adapter->regs;
1302 ena_dev->dmadev = adapter->pdev;
1304 adapter->id_number = adapters_found;
1306 snprintf(adapter->name, ENA_NAME_MAX_LEN, "ena_%d",
1307 adapter->id_number);
1309 /* device specific initialization routine */
1310 rc = ena_device_init(ena_dev, &get_feat_ctx);
1312 PMD_INIT_LOG(CRIT, "Failed to init ENA device\n");
1316 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
1317 if (get_feat_ctx.max_queues.max_llq_num == 0) {
1319 "Trying to use LLQ but llq_num is 0.\n"
1320 "Fall back into regular queues.\n");
1321 ena_dev->tx_mem_queue_type =
1322 ENA_ADMIN_PLACEMENT_POLICY_HOST;
1323 adapter->num_queues =
1324 get_feat_ctx.max_queues.max_sq_num;
1326 adapter->num_queues =
1327 get_feat_ctx.max_queues.max_llq_num;
1330 adapter->num_queues = get_feat_ctx.max_queues.max_sq_num;
1333 queue_size = ena_calc_queue_size(ena_dev, &get_feat_ctx);
1334 if ((queue_size <= 0) || (adapter->num_queues <= 0))
1337 adapter->tx_ring_size = queue_size;
1338 adapter->rx_ring_size = queue_size;
1340 /* prepare ring structures */
1341 ena_init_rings(adapter);
1343 ena_config_debug_area(adapter);
1345 /* Set max MTU for this device */
1346 adapter->max_mtu = get_feat_ctx.dev_attr.max_mtu;
1348 /* Copy MAC address and point DPDK to it */
1349 eth_dev->data->mac_addrs = (struct ether_addr *)adapter->mac_addr;
1350 ether_addr_copy((struct ether_addr *)get_feat_ctx.dev_attr.mac_addr,
1351 (struct ether_addr *)adapter->mac_addr);
1353 adapter->drv_stats = rte_zmalloc("adapter stats",
1354 sizeof(*adapter->drv_stats),
1355 RTE_CACHE_LINE_SIZE);
1356 if (!adapter->drv_stats) {
1357 RTE_LOG(ERR, PMD, "failed to alloc mem for adapter stats\n");
1362 adapter->state = ENA_ADAPTER_STATE_INIT;
1367 static int ena_dev_configure(struct rte_eth_dev *dev)
1369 struct ena_adapter *adapter =
1370 (struct ena_adapter *)(dev->data->dev_private);
1372 if (!(adapter->state == ENA_ADAPTER_STATE_INIT ||
1373 adapter->state == ENA_ADAPTER_STATE_STOPPED)) {
1374 PMD_INIT_LOG(ERR, "Illegal adapter state: %d\n",
1379 switch (adapter->state) {
1380 case ENA_ADAPTER_STATE_INIT:
1381 case ENA_ADAPTER_STATE_STOPPED:
1382 adapter->state = ENA_ADAPTER_STATE_CONFIG;
1384 case ENA_ADAPTER_STATE_CONFIG:
1385 RTE_LOG(WARNING, PMD,
1386 "Ivalid driver state while trying to configure device\n");
1395 static void ena_init_rings(struct ena_adapter *adapter)
1399 for (i = 0; i < adapter->num_queues; i++) {
1400 struct ena_ring *ring = &adapter->tx_ring[i];
1402 ring->configured = 0;
1403 ring->type = ENA_RING_TYPE_TX;
1404 ring->adapter = adapter;
1406 ring->tx_mem_queue_type = adapter->ena_dev.tx_mem_queue_type;
1407 ring->tx_max_header_size = adapter->ena_dev.tx_max_header_size;
1410 for (i = 0; i < adapter->num_queues; i++) {
1411 struct ena_ring *ring = &adapter->rx_ring[i];
1413 ring->configured = 0;
1414 ring->type = ENA_RING_TYPE_RX;
1415 ring->adapter = adapter;
1420 static void ena_infos_get(struct rte_eth_dev *dev,
1421 struct rte_eth_dev_info *dev_info)
1423 struct ena_adapter *adapter;
1424 struct ena_com_dev *ena_dev;
1425 struct ena_com_dev_get_features_ctx feat;
1426 uint32_t rx_feat = 0, tx_feat = 0;
1429 ena_assert_msg(dev->data != NULL, "Uninitialized device");
1430 ena_assert_msg(dev->data->dev_private != NULL, "Uninitialized device");
1431 adapter = (struct ena_adapter *)(dev->data->dev_private);
1433 ena_dev = &adapter->ena_dev;
1434 ena_assert_msg(ena_dev != NULL, "Uninitialized device");
1436 dev_info->speed_capa =
1438 ETH_LINK_SPEED_2_5G |
1440 ETH_LINK_SPEED_10G |
1441 ETH_LINK_SPEED_25G |
1442 ETH_LINK_SPEED_40G |
1443 ETH_LINK_SPEED_50G |
1444 ETH_LINK_SPEED_100G;
1446 /* Get supported features from HW */
1447 rc = ena_com_get_dev_attr_feat(ena_dev, &feat);
1450 "Cannot get attribute for ena device rc= %d\n", rc);
1454 /* Set Tx & Rx features available for device */
1455 if (feat.offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK)
1456 tx_feat |= DEV_TX_OFFLOAD_TCP_TSO;
1458 if (feat.offload.tx &
1459 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)
1460 tx_feat |= DEV_TX_OFFLOAD_IPV4_CKSUM |
1461 DEV_TX_OFFLOAD_UDP_CKSUM |
1462 DEV_TX_OFFLOAD_TCP_CKSUM;
1464 if (feat.offload.tx &
1465 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK)
1466 rx_feat |= DEV_RX_OFFLOAD_IPV4_CKSUM |
1467 DEV_RX_OFFLOAD_UDP_CKSUM |
1468 DEV_RX_OFFLOAD_TCP_CKSUM;
1470 /* Inform framework about available features */
1471 dev_info->rx_offload_capa = rx_feat;
1472 dev_info->tx_offload_capa = tx_feat;
1474 dev_info->min_rx_bufsize = ENA_MIN_FRAME_LEN;
1475 dev_info->max_rx_pktlen = adapter->max_mtu;
1476 dev_info->max_mac_addrs = 1;
1478 dev_info->max_rx_queues = adapter->num_queues;
1479 dev_info->max_tx_queues = adapter->num_queues;
1480 dev_info->reta_size = ENA_RX_RSS_TABLE_SIZE;
1483 static uint16_t eth_ena_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
1486 struct ena_ring *rx_ring = (struct ena_ring *)(rx_queue);
1487 unsigned int ring_size = rx_ring->ring_size;
1488 unsigned int ring_mask = ring_size - 1;
1489 uint16_t next_to_clean = rx_ring->next_to_clean;
1490 uint16_t desc_in_use = 0;
1491 unsigned int recv_idx = 0;
1492 struct rte_mbuf *mbuf = NULL;
1493 struct rte_mbuf *mbuf_head = NULL;
1494 struct rte_mbuf *mbuf_prev = NULL;
1495 struct rte_mbuf **rx_buff_info = rx_ring->rx_buffer_info;
1496 unsigned int completed;
1498 struct ena_com_rx_ctx ena_rx_ctx;
1501 /* Check adapter state */
1502 if (unlikely(rx_ring->adapter->state != ENA_ADAPTER_STATE_RUNNING)) {
1504 "Trying to receive pkts while device is NOT running\n");
1508 desc_in_use = rx_ring->next_to_use - next_to_clean;
1509 if (unlikely(nb_pkts > desc_in_use))
1510 nb_pkts = desc_in_use;
1512 for (completed = 0; completed < nb_pkts; completed++) {
1515 ena_rx_ctx.max_bufs = rx_ring->ring_size;
1516 ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
1517 ena_rx_ctx.descs = 0;
1518 /* receive packet context */
1519 rc = ena_com_rx_pkt(rx_ring->ena_com_io_cq,
1520 rx_ring->ena_com_io_sq,
1523 RTE_LOG(ERR, PMD, "ena_com_rx_pkt error %d\n", rc);
1527 if (unlikely(ena_rx_ctx.descs == 0))
1530 while (segments < ena_rx_ctx.descs) {
1531 mbuf = rx_buff_info[next_to_clean & ring_mask];
1532 mbuf->data_len = ena_rx_ctx.ena_bufs[segments].len;
1533 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
1536 if (segments == 0) {
1537 mbuf->nb_segs = ena_rx_ctx.descs;
1538 mbuf->port = rx_ring->port_id;
1542 /* for multi-segment pkts create mbuf chain */
1543 mbuf_prev->next = mbuf;
1545 mbuf_head->pkt_len += mbuf->data_len;
1552 /* fill mbuf attributes if any */
1553 ena_rx_mbuf_prepare(mbuf_head, &ena_rx_ctx);
1554 mbuf_head->hash.rss = (uint32_t)rx_ring->id;
1556 /* pass to DPDK application head mbuf */
1557 rx_pkts[recv_idx] = mbuf_head;
1561 rx_ring->next_to_clean = next_to_clean;
1563 desc_in_use = desc_in_use - completed + 1;
1564 /* Burst refill to save doorbells, memory barriers, const interval */
1565 if (ring_size - desc_in_use > ENA_RING_DESCS_RATIO(ring_size))
1566 ena_populate_rx_queue(rx_ring, ring_size - desc_in_use);
1571 static uint16_t eth_ena_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
1574 struct ena_ring *tx_ring = (struct ena_ring *)(tx_queue);
1575 uint16_t next_to_use = tx_ring->next_to_use;
1576 uint16_t next_to_clean = tx_ring->next_to_clean;
1577 struct rte_mbuf *mbuf;
1578 unsigned int ring_size = tx_ring->ring_size;
1579 unsigned int ring_mask = ring_size - 1;
1580 struct ena_com_tx_ctx ena_tx_ctx;
1581 struct ena_tx_buffer *tx_info;
1582 struct ena_com_buf *ebuf;
1583 uint16_t rc, req_id, total_tx_descs = 0;
1584 uint16_t sent_idx = 0, empty_tx_reqs;
1587 /* Check adapter state */
1588 if (unlikely(tx_ring->adapter->state != ENA_ADAPTER_STATE_RUNNING)) {
1590 "Trying to xmit pkts while device is NOT running\n");
1594 empty_tx_reqs = ring_size - (next_to_use - next_to_clean);
1595 if (nb_pkts > empty_tx_reqs)
1596 nb_pkts = empty_tx_reqs;
1598 for (sent_idx = 0; sent_idx < nb_pkts; sent_idx++) {
1599 mbuf = tx_pkts[sent_idx];
1601 req_id = tx_ring->empty_tx_reqs[next_to_use & ring_mask];
1602 tx_info = &tx_ring->tx_buffer_info[req_id];
1603 tx_info->mbuf = mbuf;
1604 tx_info->num_of_bufs = 0;
1605 ebuf = tx_info->bufs;
1607 /* Prepare TX context */
1608 memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
1609 memset(&ena_tx_ctx.ena_meta, 0x0,
1610 sizeof(struct ena_com_tx_meta));
1611 ena_tx_ctx.ena_bufs = ebuf;
1612 ena_tx_ctx.req_id = req_id;
1613 if (tx_ring->tx_mem_queue_type ==
1614 ENA_ADMIN_PLACEMENT_POLICY_DEV) {
1615 /* prepare the push buffer with
1616 * virtual address of the data
1618 ena_tx_ctx.header_len =
1619 RTE_MIN(mbuf->data_len,
1620 tx_ring->tx_max_header_size);
1621 ena_tx_ctx.push_header =
1622 (void *)((char *)mbuf->buf_addr +
1624 } /* there's no else as we take advantage of memset zeroing */
1626 /* Set TX offloads flags, if applicable */
1627 ena_tx_mbuf_prepare(mbuf, &ena_tx_ctx);
1629 if (unlikely(mbuf->ol_flags &
1630 (PKT_RX_L4_CKSUM_BAD | PKT_RX_IP_CKSUM_BAD)))
1631 rte_atomic64_inc(&tx_ring->adapter->drv_stats->ierrors);
1633 rte_prefetch0(tx_pkts[(sent_idx + 4) & ring_mask]);
1635 /* Process first segment taking into
1636 * consideration pushed header
1638 if (mbuf->data_len > ena_tx_ctx.header_len) {
1639 ebuf->paddr = mbuf->buf_physaddr +
1641 ena_tx_ctx.header_len;
1642 ebuf->len = mbuf->data_len - ena_tx_ctx.header_len;
1644 tx_info->num_of_bufs++;
1647 while ((mbuf = mbuf->next) != NULL) {
1648 ebuf->paddr = mbuf->buf_physaddr + mbuf->data_off;
1649 ebuf->len = mbuf->data_len;
1651 tx_info->num_of_bufs++;
1654 ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
1656 /* Write data to device */
1657 rc = ena_com_prepare_tx(tx_ring->ena_com_io_sq,
1658 &ena_tx_ctx, &nb_hw_desc);
1662 tx_info->tx_descs = nb_hw_desc;
1667 /* If there are ready packets to be xmitted... */
1669 /* ...let HW do its best :-) */
1671 ena_com_write_sq_doorbell(tx_ring->ena_com_io_sq);
1673 tx_ring->next_to_use = next_to_use;
1676 /* Clear complete packets */
1677 while (ena_com_tx_comp_req_id_get(tx_ring->ena_com_io_cq, &req_id) >= 0) {
1678 /* Get Tx info & store how many descs were processed */
1679 tx_info = &tx_ring->tx_buffer_info[req_id];
1680 total_tx_descs += tx_info->tx_descs;
1682 /* Free whole mbuf chain */
1683 mbuf = tx_info->mbuf;
1684 rte_pktmbuf_free(mbuf);
1685 tx_info->mbuf = NULL;
1687 /* Put back descriptor to the ring for reuse */
1688 tx_ring->empty_tx_reqs[next_to_clean & ring_mask] = req_id;
1691 /* If too many descs to clean, leave it for another run */
1692 if (unlikely(total_tx_descs > ENA_RING_DESCS_RATIO(ring_size)))
1696 if (total_tx_descs > 0) {
1697 /* acknowledge completion of sent packets */
1698 ena_com_comp_ack(tx_ring->ena_com_io_sq, total_tx_descs);
1699 tx_ring->next_to_clean = next_to_clean;
1705 static struct eth_driver rte_ena_pmd = {
1707 .id_table = pci_id_ena_map,
1708 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
1709 .probe = rte_eth_dev_pci_probe,
1710 .remove = rte_eth_dev_pci_remove,
1712 .eth_dev_init = eth_ena_dev_init,
1713 .dev_private_size = sizeof(struct ena_adapter),
1716 RTE_PMD_REGISTER_PCI(net_ena, rte_ena_pmd.pci_drv);
1717 RTE_PMD_REGISTER_PCI_TABLE(net_ena, pci_id_ena_map);