1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Aquantia Corporation
5 #include <rte_malloc.h>
6 #include <rte_ethdev_driver.h>
8 #include "atl_ethdev.h"
9 #include "atl_hw_regs.h"
12 #include "hw_atl/hw_atl_llh.h"
13 #include "hw_atl/hw_atl_b0.h"
14 #include "hw_atl/hw_atl_b0_internal.h"
17 * Structure associated with each descriptor of the RX ring of a RX queue.
20 struct rte_mbuf *mbuf;
24 * Structure associated with each RX queue.
27 struct rte_mempool *mb_pool;
28 struct hw_atl_rxd_s *hw_ring;
29 uint64_t hw_ring_phys_addr;
30 struct atl_rx_entry *sw_ring;
34 uint16_t rx_free_thresh;
43 atl_reset_rx_queue(struct atl_rx_queue *rxq)
45 struct hw_atl_rxd_s *rxd = NULL;
48 PMD_INIT_FUNC_TRACE();
50 for (i = 0; i < rxq->nb_rx_desc; i++) {
51 rxd = (struct hw_atl_rxd_s *)&rxq->hw_ring[i];
60 atl_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
61 uint16_t nb_rx_desc, unsigned int socket_id,
62 const struct rte_eth_rxconf *rx_conf,
63 struct rte_mempool *mb_pool)
65 struct atl_rx_queue *rxq;
66 const struct rte_memzone *mz;
68 PMD_INIT_FUNC_TRACE();
70 /* make sure a valid number of descriptors have been requested */
71 if (nb_rx_desc < AQ_HW_MIN_RX_RING_SIZE ||
72 nb_rx_desc > AQ_HW_MAX_RX_RING_SIZE) {
73 PMD_INIT_LOG(ERR, "Number of Rx descriptors must be "
74 "less than or equal to %d, "
75 "greater than or equal to %d", AQ_HW_MAX_RX_RING_SIZE,
76 AQ_HW_MIN_RX_RING_SIZE);
81 * if this queue existed already, free the associated memory. The
82 * queue cannot be reused in case we need to allocate memory on
83 * different socket than was previously used.
85 if (dev->data->rx_queues[rx_queue_id] != NULL) {
86 atl_rx_queue_release(dev->data->rx_queues[rx_queue_id]);
87 dev->data->rx_queues[rx_queue_id] = NULL;
90 /* allocate memory for the queue structure */
91 rxq = rte_zmalloc_socket("atlantic Rx queue", sizeof(*rxq),
92 RTE_CACHE_LINE_SIZE, socket_id);
94 PMD_INIT_LOG(ERR, "Cannot allocate queue structure");
99 rxq->mb_pool = mb_pool;
100 rxq->nb_rx_desc = nb_rx_desc;
101 rxq->port_id = dev->data->port_id;
102 rxq->queue_id = rx_queue_id;
103 rxq->rx_free_thresh = rx_conf->rx_free_thresh;
105 rxq->l3_csum_enabled = dev->data->dev_conf.rxmode.offloads &
106 DEV_RX_OFFLOAD_IPV4_CKSUM;
107 rxq->l4_csum_enabled = dev->data->dev_conf.rxmode.offloads &
108 (DEV_RX_OFFLOAD_UDP_CKSUM | DEV_RX_OFFLOAD_TCP_CKSUM);
109 if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)
110 PMD_DRV_LOG(ERR, "PMD does not support KEEP_CRC offload");
112 /* allocate memory for the software ring */
113 rxq->sw_ring = rte_zmalloc_socket("atlantic sw rx ring",
114 nb_rx_desc * sizeof(struct atl_rx_entry),
115 RTE_CACHE_LINE_SIZE, socket_id);
116 if (rxq->sw_ring == NULL) {
118 "Port %d: Cannot allocate software ring for queue %d",
119 rxq->port_id, rxq->queue_id);
125 * allocate memory for the hardware descriptor ring. A memzone large
126 * enough to hold the maximum ring size is requested to allow for
127 * resizing in later calls to the queue setup function.
129 mz = rte_eth_dma_zone_reserve(dev, "rx hw_ring", rx_queue_id,
131 sizeof(struct hw_atl_rxd_s),
135 "Port %d: Cannot allocate hardware ring for queue %d",
136 rxq->port_id, rxq->queue_id);
137 rte_free(rxq->sw_ring);
141 rxq->hw_ring = mz->addr;
142 rxq->hw_ring_phys_addr = mz->iova;
144 atl_reset_rx_queue(rxq);
146 dev->data->rx_queues[rx_queue_id] = rxq;
151 atl_tx_init(struct rte_eth_dev *eth_dev __rte_unused)
157 atl_rx_init(struct rte_eth_dev *eth_dev)
159 struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
160 struct atl_rx_queue *rxq;
161 uint64_t base_addr = 0;
165 PMD_INIT_FUNC_TRACE();
167 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
168 rxq = eth_dev->data->rx_queues[i];
169 base_addr = rxq->hw_ring_phys_addr;
171 /* Take requested pool mbuf size and adapt
172 * descriptor buffer to best fit
174 int buff_size = rte_pktmbuf_data_room_size(rxq->mb_pool) -
175 RTE_PKTMBUF_HEADROOM;
177 buff_size = RTE_ALIGN_FLOOR(buff_size, 1024);
178 if (buff_size > HW_ATL_B0_RXD_BUF_SIZE_MAX) {
179 PMD_INIT_LOG(WARNING,
180 "Port %d queue %d: mem pool buff size is too big\n",
181 rxq->port_id, rxq->queue_id);
182 buff_size = HW_ATL_B0_RXD_BUF_SIZE_MAX;
184 if (buff_size < 1024) {
186 "Port %d queue %d: mem pool buff size is too small\n",
187 rxq->port_id, rxq->queue_id);
190 rxq->buff_size = buff_size;
192 err = hw_atl_b0_hw_ring_rx_init(hw, base_addr, rxq->queue_id,
193 rxq->nb_rx_desc, buff_size, 0,
197 PMD_INIT_LOG(ERR, "Port %d: Cannot init RX queue %d",
198 rxq->port_id, rxq->queue_id);
207 atl_alloc_rx_queue_mbufs(struct atl_rx_queue *rxq)
209 struct atl_rx_entry *rx_entry = rxq->sw_ring;
210 struct hw_atl_rxd_s *rxd;
211 uint64_t dma_addr = 0;
214 PMD_INIT_FUNC_TRACE();
217 for (i = 0; i < rxq->nb_rx_desc; i++) {
218 struct rte_mbuf *mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
222 "Port %d: mbuf alloc failed for rx queue %d",
223 rxq->port_id, rxq->queue_id);
227 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
228 mbuf->port = rxq->port_id;
230 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
231 rxd = (struct hw_atl_rxd_s *)&rxq->hw_ring[i];
232 rxd->buf_addr = dma_addr;
234 rx_entry[i].mbuf = mbuf;
241 atl_rx_queue_release_mbufs(struct atl_rx_queue *rxq)
245 PMD_INIT_FUNC_TRACE();
247 if (rxq->sw_ring != NULL) {
248 for (i = 0; i < rxq->nb_rx_desc; i++) {
249 if (rxq->sw_ring[i].mbuf != NULL) {
250 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
251 rxq->sw_ring[i].mbuf = NULL;
258 atl_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
260 struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
261 struct atl_rx_queue *rxq = NULL;
263 PMD_INIT_FUNC_TRACE();
265 if (rx_queue_id < dev->data->nb_rx_queues) {
266 rxq = dev->data->rx_queues[rx_queue_id];
268 if (atl_alloc_rx_queue_mbufs(rxq) != 0) {
270 "Port %d: Allocate mbufs for queue %d failed",
271 rxq->port_id, rxq->queue_id);
275 hw_atl_b0_hw_ring_rx_start(hw, rx_queue_id);
278 hw_atl_reg_rx_dma_desc_tail_ptr_set(hw, rxq->nb_rx_desc - 1,
280 dev->data->rx_queue_state[rx_queue_id] =
281 RTE_ETH_QUEUE_STATE_STARTED;
290 atl_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
292 struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
293 struct atl_rx_queue *rxq = NULL;
295 PMD_INIT_FUNC_TRACE();
297 if (rx_queue_id < dev->data->nb_rx_queues) {
298 rxq = dev->data->rx_queues[rx_queue_id];
300 hw_atl_b0_hw_ring_rx_stop(hw, rx_queue_id);
302 atl_rx_queue_release_mbufs(rxq);
303 atl_reset_rx_queue(rxq);
305 dev->data->rx_queue_state[rx_queue_id] =
306 RTE_ETH_QUEUE_STATE_STOPPED;
315 atl_rx_queue_release(void *rx_queue)
317 PMD_INIT_FUNC_TRACE();
319 if (rx_queue != NULL) {
320 struct atl_rx_queue *rxq = (struct atl_rx_queue *)rx_queue;
322 atl_rx_queue_release_mbufs(rxq);
323 rte_free(rxq->sw_ring);
329 atl_prep_pkts(void *tx_queue __rte_unused,
330 struct rte_mbuf **tx_pkts __rte_unused,
331 uint16_t nb_pkts __rte_unused)
337 atl_free_queues(struct rte_eth_dev *dev)
341 PMD_INIT_FUNC_TRACE();
343 for (i = 0; i < dev->data->nb_rx_queues; i++) {
344 atl_rx_queue_release(dev->data->rx_queues[i]);
345 dev->data->rx_queues[i] = 0;
347 dev->data->nb_rx_queues = 0;
351 atl_start_queues(struct rte_eth_dev *dev)
355 PMD_INIT_FUNC_TRACE();
357 for (i = 0; i < dev->data->nb_rx_queues; i++) {
358 if (atl_rx_queue_start(dev, i) != 0) {
360 "Port %d: Start Rx queue %d failed",
361 dev->data->port_id, i);
370 atl_stop_queues(struct rte_eth_dev *dev)
374 PMD_INIT_FUNC_TRACE();
376 for (i = 0; i < dev->data->nb_rx_queues; i++) {
377 if (atl_rx_queue_stop(dev, i) != 0) {
379 "Port %d: Stop Rx queue %d failed",
380 dev->data->port_id, i);
389 atl_desc_to_offload_flags(struct atl_rx_queue *rxq,
390 struct hw_atl_rxd_wb_s *rxd_wb)
392 uint64_t mbuf_flags = 0;
394 PMD_INIT_FUNC_TRACE();
397 if (rxq->l3_csum_enabled && ((rxd_wb->pkt_type & 0x3) == 0)) {
398 /* IPv4 csum error ? */
399 if (rxd_wb->rx_stat & BIT(1))
400 mbuf_flags |= PKT_RX_IP_CKSUM_BAD;
402 mbuf_flags |= PKT_RX_IP_CKSUM_GOOD;
404 mbuf_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
407 /* CSUM calculated ? */
408 if (rxq->l4_csum_enabled && (rxd_wb->rx_stat & BIT(3))) {
409 if (rxd_wb->rx_stat & BIT(2))
410 mbuf_flags |= PKT_RX_L4_CKSUM_BAD;
412 mbuf_flags |= PKT_RX_L4_CKSUM_GOOD;
414 mbuf_flags |= PKT_RX_L4_CKSUM_UNKNOWN;
421 atl_desc_to_pkt_type(struct hw_atl_rxd_wb_s *rxd_wb)
423 uint32_t type = RTE_PTYPE_UNKNOWN;
424 uint16_t l2_l3_type = rxd_wb->pkt_type & 0x3;
425 uint16_t l4_type = (rxd_wb->pkt_type & 0x1C) >> 2;
427 switch (l2_l3_type) {
429 type = RTE_PTYPE_L3_IPV4;
432 type = RTE_PTYPE_L3_IPV6;
435 type = RTE_PTYPE_L2_ETHER;
438 type = RTE_PTYPE_L2_ETHER_ARP;
444 type |= RTE_PTYPE_L4_TCP;
447 type |= RTE_PTYPE_L4_UDP;
450 type |= RTE_PTYPE_L4_SCTP;
453 type |= RTE_PTYPE_L4_ICMP;
457 if (rxd_wb->pkt_type & BIT(5))
458 type |= RTE_PTYPE_L2_ETHER_VLAN;
464 atl_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
466 struct atl_rx_queue *rxq = (struct atl_rx_queue *)rx_queue;
467 struct rte_eth_dev *dev = &rte_eth_devices[rxq->port_id];
468 struct atl_adapter *adapter =
469 ATL_DEV_TO_ADAPTER(&rte_eth_devices[rxq->port_id]);
470 struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(adapter);
471 struct atl_rx_entry *sw_ring = rxq->sw_ring;
473 struct rte_mbuf *new_mbuf;
474 struct rte_mbuf *rx_mbuf, *rx_mbuf_prev, *rx_mbuf_first;
475 struct atl_rx_entry *rx_entry;
477 uint16_t nb_hold = 0;
478 struct hw_atl_rxd_wb_s rxd_wb;
479 struct hw_atl_rxd_s *rxd = NULL;
480 uint16_t tail = rxq->rx_tail;
482 uint16_t pkt_len = 0;
484 while (nb_rx < nb_pkts) {
485 uint16_t eop_tail = tail;
487 rxd = (struct hw_atl_rxd_s *)&rxq->hw_ring[tail];
488 rxd_wb = *(struct hw_atl_rxd_wb_s *)rxd;
490 if (!rxd_wb.dd) { /* RxD is not done */
494 PMD_RX_LOG(ERR, "port_id=%u queue_id=%u tail=%u "
495 "eop=0x%x pkt_len=%u hash=0x%x hash_type=0x%x",
496 (unsigned int)rxq->port_id,
497 (unsigned int)rxq->queue_id,
498 (unsigned int)tail, (unsigned int)rxd_wb.eop,
499 (unsigned int)rte_le_to_cpu_16(rxd_wb.pkt_len),
500 rxd_wb.rss_hash, rxd_wb.rss_type);
502 /* RxD is not done */
505 struct hw_atl_rxd_wb_s *eop_rxwbd;
507 eop_tail = (eop_tail + 1) % rxq->nb_rx_desc;
508 eop_rxwbd = (struct hw_atl_rxd_wb_s *)
509 &rxq->hw_ring[eop_tail];
510 if (!eop_rxwbd->dd) {
511 /* no EOP received yet */
515 if (eop_rxwbd->dd && eop_rxwbd->eop)
519 if (eop_tail == tail)
523 rx_mbuf_first = NULL;
525 /* Run through packet segments */
527 new_mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
528 if (new_mbuf == NULL) {
530 "RX mbuf alloc failed port_id=%u "
531 "queue_id=%u", (unsigned int)rxq->port_id,
532 (unsigned int)rxq->queue_id);
533 dev->data->rx_mbuf_alloc_failed++;
538 rx_entry = &sw_ring[tail];
540 rx_mbuf = rx_entry->mbuf;
541 rx_entry->mbuf = new_mbuf;
542 dma_addr = rte_cpu_to_le_64(
543 rte_mbuf_data_iova_default(new_mbuf));
545 /* setup RX descriptor */
547 rxd->buf_addr = dma_addr;
550 * Initialize the returned mbuf.
551 * 1) setup generic mbuf fields:
552 * - number of segments,
555 * - RX port identifier.
556 * 2) integrate hardware offload data, if any:
557 * < - RSS flag & hash,
558 * - IP checksum flag,
559 * - VLAN TCI, if any,
562 pkt_len = (uint16_t)rte_le_to_cpu_16(rxd_wb.pkt_len);
563 rx_mbuf->data_off = RTE_PKTMBUF_HEADROOM;
564 rte_prefetch1((char *)rx_mbuf->buf_addr +
566 rx_mbuf->nb_segs = 0;
567 rx_mbuf->next = NULL;
568 rx_mbuf->pkt_len = pkt_len;
569 rx_mbuf->data_len = pkt_len;
571 u16 remainder_len = pkt_len % rxq->buff_size;
573 remainder_len = rxq->buff_size;
574 rx_mbuf->data_len = remainder_len;
576 rx_mbuf->data_len = pkt_len > rxq->buff_size ?
577 rxq->buff_size : pkt_len;
579 rx_mbuf->port = rxq->port_id;
581 rx_mbuf->hash.rss = rxd_wb.rss_hash;
583 rx_mbuf->vlan_tci = rxd_wb.vlan;
586 atl_desc_to_offload_flags(rxq, &rxd_wb);
587 rx_mbuf->packet_type = atl_desc_to_pkt_type(&rxd_wb);
590 rx_mbuf_first = rx_mbuf;
591 rx_mbuf_first->nb_segs++;
594 rx_mbuf_prev->next = rx_mbuf;
595 rx_mbuf_prev = rx_mbuf;
597 tail = (tail + 1) % rxq->nb_rx_desc;
598 /* Prefetch next mbufs */
599 rte_prefetch0(sw_ring[tail].mbuf);
600 if ((tail & 0x3) == 0) {
601 rte_prefetch0(&sw_ring[tail]);
602 rte_prefetch0(&sw_ring[tail]);
605 /* filled mbuf_first */
608 rxd = (struct hw_atl_rxd_s *)&rxq->hw_ring[tail];
609 rxd_wb = *(struct hw_atl_rxd_wb_s *)rxd;
613 * Store the mbuf address into the next entry of the array
614 * of returned packets.
616 rx_pkts[nb_rx++] = rx_mbuf_first;
618 PMD_RX_LOG(ERR, "add mbuf segs=%d pkt_len=%d",
619 rx_mbuf_first->nb_segs,
620 rx_mbuf_first->pkt_len);
628 * If the number of free RX descriptors is greater than the RX free
629 * threshold of the queue, advance the Receive Descriptor Tail (RDT)
631 * Update the RDT with the value of the last processed RX descriptor
632 * minus 1, to guarantee that the RDT register is never equal to the
633 * RDH register, which creates a "full" ring situtation from the
634 * hardware point of view...
636 nb_hold = (uint16_t)(nb_hold + rxq->nb_rx_hold);
637 if (nb_hold > rxq->rx_free_thresh) {
638 PMD_RX_LOG(ERR, "port_id=%u queue_id=%u rx_tail=%u "
639 "nb_hold=%u nb_rx=%u",
640 (unsigned int)rxq->port_id, (unsigned int)rxq->queue_id,
641 (unsigned int)tail, (unsigned int)nb_hold,
642 (unsigned int)nb_rx);
643 tail = (uint16_t)((tail == 0) ?
644 (rxq->nb_rx_desc - 1) : (tail - 1));
646 hw_atl_reg_rx_dma_desc_tail_ptr_set(hw, tail, rxq->queue_id);
651 rxq->nb_rx_hold = nb_hold;
658 atl_xmit_pkts(void *tx_queue __rte_unused,
659 struct rte_mbuf **tx_pkts __rte_unused,
660 uint16_t nb_pkts __rte_unused)