1540914bcSEd Czeck /* SPDX-License-Identifier: BSD-3-Clause 2540914bcSEd Czeck * Copyright (c) 2015-2018 Atomic Rules LLC 38b154b69SEd Czeck */ 48b154b69SEd Czeck 58b154b69SEd Czeck #include <unistd.h> 68b154b69SEd Czeck 78b154b69SEd Czeck #include "ark_ethdev_rx.h" 88b154b69SEd Czeck #include "ark_global.h" 98b154b69SEd Czeck #include "ark_logs.h" 108b154b69SEd Czeck #include "ark_mpu.h" 118b154b69SEd Czeck #include "ark_udm.h" 128b154b69SEd Czeck 138b154b69SEd Czeck #define ARK_RX_META_SIZE 32 148b154b69SEd Czeck #define ARK_RX_META_OFFSET (RTE_PKTMBUF_HEADROOM - ARK_RX_META_SIZE) 158b154b69SEd Czeck 168b154b69SEd Czeck /* Forward declarations */ 178b154b69SEd Czeck struct ark_rx_queue; 188b154b69SEd Czeck struct ark_rx_meta; 198b154b69SEd Czeck 208b154b69SEd Czeck static void dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi); 218b154b69SEd Czeck static void ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue); 228b154b69SEd Czeck static uint32_t eth_ark_rx_jumbo(struct ark_rx_queue *queue, 238b154b69SEd Czeck struct ark_rx_meta *meta, 248b154b69SEd Czeck struct rte_mbuf *mbuf0, 258b154b69SEd Czeck uint32_t cons_index); 268b154b69SEd Czeck static inline int eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue); 278b154b69SEd Czeck 288b154b69SEd Czeck /* ************************************************************************* */ 298b154b69SEd Czeck struct ark_rx_queue { 308b154b69SEd Czeck /* array of mbufs to populate */ 318b154b69SEd Czeck struct rte_mbuf **reserve_q; 328b154b69SEd Czeck /* array of physical addresses of the mbuf data pointer */ 338b154b69SEd Czeck /* This point is a virtual address */ 34df6e0a06SSantosh Shukla rte_iova_t *paddress_q; 358b154b69SEd Czeck struct rte_mempool *mb_pool; 368b154b69SEd Czeck 378b154b69SEd Czeck struct ark_udm_t *udm; 388b154b69SEd Czeck struct ark_mpu_t *mpu; 398b154b69SEd Czeck 406c7f491eSEd Czeck rx_user_meta_hook_fn rx_user_meta_hook; 416c7f491eSEd Czeck void *ext_user_data; 426c7f491eSEd Czeck 43*2f27ef73SJohn Miller uint32_t dataroom; 44*2f27ef73SJohn Miller uint32_t headroom; 45*2f27ef73SJohn Miller 468b154b69SEd Czeck uint32_t queue_size; 478b154b69SEd Czeck uint32_t queue_mask; 488b154b69SEd Czeck 498b154b69SEd Czeck uint32_t seed_index; /* step 1 set with empty mbuf */ 508b154b69SEd Czeck uint32_t cons_index; /* step 3 consumed by driver */ 518b154b69SEd Czeck 528b154b69SEd Czeck /* The queue Id is used to identify the HW Q */ 538b154b69SEd Czeck uint16_t phys_qid; 548b154b69SEd Czeck 558b154b69SEd Czeck /* The queue Index is used within the dpdk device structures */ 568b154b69SEd Czeck uint16_t queue_index; 578b154b69SEd Czeck 5831d7ddedSEd Czeck uint32_t unused; 598b154b69SEd Czeck 606c7f491eSEd Czeck /* next cache line - fields written by device */ 618f196dc8SJerin Jacob RTE_MARKER cacheline1 __rte_cache_min_aligned; 628b154b69SEd Czeck 638b154b69SEd Czeck volatile uint32_t prod_index; /* step 2 filled by FPGA */ 648b154b69SEd Czeck } __rte_cache_aligned; 658b154b69SEd Czeck 668b154b69SEd Czeck /* ************************************************************************* */ 678b154b69SEd Czeck static int 688b154b69SEd Czeck eth_ark_rx_hw_setup(struct rte_eth_dev *dev, 698b154b69SEd Czeck struct ark_rx_queue *queue, 708b154b69SEd Czeck uint16_t rx_queue_id __rte_unused, uint16_t rx_queue_idx) 718b154b69SEd Czeck { 72df6e0a06SSantosh Shukla rte_iova_t queue_base; 73df6e0a06SSantosh Shukla rte_iova_t phys_addr_q_base; 74df6e0a06SSantosh Shukla rte_iova_t phys_addr_prod_index; 758b154b69SEd Czeck 7687cf4c6cSThomas Monjalon queue_base = rte_malloc_virt2iova(queue); 778b154b69SEd Czeck phys_addr_prod_index = queue_base + 788b154b69SEd Czeck offsetof(struct ark_rx_queue, prod_index); 798b154b69SEd Czeck 8087cf4c6cSThomas Monjalon phys_addr_q_base = rte_malloc_virt2iova(queue->paddress_q); 818b154b69SEd Czeck 828b154b69SEd Czeck /* Verify HW */ 83df6e0a06SSantosh Shukla if (ark_mpu_verify(queue->mpu, sizeof(rte_iova_t))) { 841502d443SEd Czeck ARK_PMD_LOG(ERR, "Illegal configuration rx queue\n"); 858b154b69SEd Czeck return -1; 868b154b69SEd Czeck } 878b154b69SEd Czeck 888b154b69SEd Czeck /* Stop and Reset and configure MPU */ 898b154b69SEd Czeck ark_mpu_configure(queue->mpu, phys_addr_q_base, queue->queue_size, 0); 908b154b69SEd Czeck 918b154b69SEd Czeck ark_udm_write_addr(queue->udm, phys_addr_prod_index); 928b154b69SEd Czeck 938b154b69SEd Czeck /* advance the valid pointer, but don't start until the queue starts */ 948b154b69SEd Czeck ark_mpu_reset_stats(queue->mpu); 958b154b69SEd Czeck 968b154b69SEd Czeck /* The seed is the producer index for the HW */ 978b154b69SEd Czeck ark_mpu_set_producer(queue->mpu, queue->seed_index); 988b154b69SEd Czeck dev->data->rx_queue_state[rx_queue_idx] = RTE_ETH_QUEUE_STATE_STOPPED; 998b154b69SEd Czeck 1008b154b69SEd Czeck return 0; 1018b154b69SEd Czeck } 1028b154b69SEd Czeck 1038b154b69SEd Czeck static inline void 1048b154b69SEd Czeck eth_ark_rx_update_cons_index(struct ark_rx_queue *queue, uint32_t cons_index) 1058b154b69SEd Czeck { 1068b154b69SEd Czeck queue->cons_index = cons_index; 10731d7ddedSEd Czeck if ((cons_index + queue->queue_size - queue->seed_index) >= 64U) { 1088b154b69SEd Czeck eth_ark_rx_seed_mbufs(queue); 1098b154b69SEd Czeck ark_mpu_set_producer(queue->mpu, queue->seed_index); 1108b154b69SEd Czeck } 1110c5b65f4SEd Czeck } 1128b154b69SEd Czeck 1138b154b69SEd Czeck /* ************************************************************************* */ 1148b154b69SEd Czeck int 1158b154b69SEd Czeck eth_ark_dev_rx_queue_setup(struct rte_eth_dev *dev, 1168b154b69SEd Czeck uint16_t queue_idx, 1178b154b69SEd Czeck uint16_t nb_desc, 1188b154b69SEd Czeck unsigned int socket_id, 1198b154b69SEd Czeck const struct rte_eth_rxconf *rx_conf, 1208b154b69SEd Czeck struct rte_mempool *mb_pool) 1218b154b69SEd Czeck { 1228b154b69SEd Czeck static int warning1; /* = 0 */ 1230bf8b0f1SStephen Hemminger struct ark_adapter *ark = dev->data->dev_private; 1248b154b69SEd Czeck 1258b154b69SEd Czeck struct ark_rx_queue *queue; 1268b154b69SEd Czeck uint32_t i; 1278b154b69SEd Czeck int status; 1288b154b69SEd Czeck 1297311db73SEd Czeck int qidx = queue_idx; 1308b154b69SEd Czeck 1318b154b69SEd Czeck /* We may already be setup, free memory prior to re-allocation */ 1328b154b69SEd Czeck if (dev->data->rx_queues[queue_idx] != NULL) { 1338b154b69SEd Czeck eth_ark_dev_rx_queue_release(dev->data->rx_queues[queue_idx]); 1348b154b69SEd Czeck dev->data->rx_queues[queue_idx] = NULL; 1358b154b69SEd Czeck } 1368b154b69SEd Czeck 1378b154b69SEd Czeck if (rx_conf != NULL && warning1 == 0) { 1388b154b69SEd Czeck warning1 = 1; 1391502d443SEd Czeck ARK_PMD_LOG(NOTICE, 1408b154b69SEd Czeck "Arkville ignores rte_eth_rxconf argument.\n"); 1418b154b69SEd Czeck } 1428b154b69SEd Czeck 1438b154b69SEd Czeck if (RTE_PKTMBUF_HEADROOM < ARK_RX_META_SIZE) { 1441502d443SEd Czeck ARK_PMD_LOG(ERR, 1458b154b69SEd Czeck "Error: DPDK Arkville requires head room > %d bytes (%s)\n", 1468b154b69SEd Czeck ARK_RX_META_SIZE, __func__); 1478b154b69SEd Czeck return -1; /* ERROR CODE */ 1488b154b69SEd Czeck } 1498b154b69SEd Czeck 1508b154b69SEd Czeck if (!rte_is_power_of_2(nb_desc)) { 1511502d443SEd Czeck ARK_PMD_LOG(ERR, 1528b154b69SEd Czeck "DPDK Arkville configuration queue size must be power of two %u (%s)\n", 1538b154b69SEd Czeck nb_desc, __func__); 1548b154b69SEd Czeck return -1; /* ERROR CODE */ 1558b154b69SEd Czeck } 1568b154b69SEd Czeck 1578b154b69SEd Czeck /* Allocate queue struct */ 1588b154b69SEd Czeck queue = rte_zmalloc_socket("Ark_rxqueue", 1598b154b69SEd Czeck sizeof(struct ark_rx_queue), 1608b154b69SEd Czeck 64, 1618b154b69SEd Czeck socket_id); 1628b154b69SEd Czeck if (queue == 0) { 1631502d443SEd Czeck ARK_PMD_LOG(ERR, "Failed to allocate memory in %s\n", __func__); 1648b154b69SEd Czeck return -ENOMEM; 1658b154b69SEd Czeck } 1668b154b69SEd Czeck 1678b154b69SEd Czeck /* NOTE zmalloc is used, no need to 0 indexes, etc. */ 1688b154b69SEd Czeck queue->mb_pool = mb_pool; 169*2f27ef73SJohn Miller queue->dataroom = rte_pktmbuf_data_room_size(mb_pool) - 170*2f27ef73SJohn Miller RTE_PKTMBUF_HEADROOM; 171*2f27ef73SJohn Miller queue->headroom = RTE_PKTMBUF_HEADROOM; 1728b154b69SEd Czeck queue->phys_qid = qidx; 1738b154b69SEd Czeck queue->queue_index = queue_idx; 1748b154b69SEd Czeck queue->queue_size = nb_desc; 1758b154b69SEd Czeck queue->queue_mask = nb_desc - 1; 1766c7f491eSEd Czeck queue->rx_user_meta_hook = ark->user_ext.rx_user_meta_hook; 1776c7f491eSEd Czeck queue->ext_user_data = ark->user_data[dev->data->port_id]; 1788b154b69SEd Czeck 1798b154b69SEd Czeck queue->reserve_q = 1808b154b69SEd Czeck rte_zmalloc_socket("Ark_rx_queue mbuf", 1818b154b69SEd Czeck nb_desc * sizeof(struct rte_mbuf *), 1828b154b69SEd Czeck 64, 1838b154b69SEd Czeck socket_id); 1848b154b69SEd Czeck queue->paddress_q = 1858b154b69SEd Czeck rte_zmalloc_socket("Ark_rx_queue paddr", 186df6e0a06SSantosh Shukla nb_desc * sizeof(rte_iova_t), 1878b154b69SEd Czeck 64, 1888b154b69SEd Czeck socket_id); 1898b154b69SEd Czeck 1908b154b69SEd Czeck if (queue->reserve_q == 0 || queue->paddress_q == 0) { 1911502d443SEd Czeck ARK_PMD_LOG(ERR, 1928b154b69SEd Czeck "Failed to allocate queue memory in %s\n", 1938b154b69SEd Czeck __func__); 1948b154b69SEd Czeck rte_free(queue->reserve_q); 1958b154b69SEd Czeck rte_free(queue->paddress_q); 1968b154b69SEd Czeck rte_free(queue); 1978b154b69SEd Czeck return -ENOMEM; 1988b154b69SEd Czeck } 1998b154b69SEd Czeck 2008b154b69SEd Czeck dev->data->rx_queues[queue_idx] = queue; 2018b154b69SEd Czeck queue->udm = RTE_PTR_ADD(ark->udm.v, qidx * ARK_UDM_QOFFSET); 2028b154b69SEd Czeck queue->mpu = RTE_PTR_ADD(ark->mpurx.v, qidx * ARK_MPU_QOFFSET); 2038b154b69SEd Czeck 204*2f27ef73SJohn Miller /* Configure UDM per queue */ 205*2f27ef73SJohn Miller ark_udm_stop(queue->udm, 0); 206*2f27ef73SJohn Miller ark_udm_configure(queue->udm, 207*2f27ef73SJohn Miller RTE_PKTMBUF_HEADROOM, 208*2f27ef73SJohn Miller queue->dataroom, 209*2f27ef73SJohn Miller ARK_RX_WRITE_TIME_NS); 210*2f27ef73SJohn Miller ark_udm_stats_reset(queue->udm); 211*2f27ef73SJohn Miller ark_udm_stop(queue->udm, 0); 212*2f27ef73SJohn Miller 2138b154b69SEd Czeck /* populate mbuf reserve */ 2148b154b69SEd Czeck status = eth_ark_rx_seed_mbufs(queue); 2158b154b69SEd Czeck 216be410a86SEd Czeck if (queue->seed_index != nb_desc) { 2171502d443SEd Czeck ARK_PMD_LOG(ERR, "Failed to allocate %u mbufs for RX queue %d\n", 218be410a86SEd Czeck nb_desc, qidx); 219be410a86SEd Czeck status = -1; 220be410a86SEd Czeck } 2218b154b69SEd Czeck /* MPU Setup */ 2228b154b69SEd Czeck if (status == 0) 2238b154b69SEd Czeck status = eth_ark_rx_hw_setup(dev, queue, qidx, queue_idx); 2248b154b69SEd Czeck 2258b154b69SEd Czeck if (unlikely(status != 0)) { 226be410a86SEd Czeck struct rte_mbuf **mbuf; 2278b154b69SEd Czeck 2281502d443SEd Czeck ARK_PMD_LOG(ERR, "Failed to initialize RX queue %d %s\n", 2298b154b69SEd Czeck qidx, 2308b154b69SEd Czeck __func__); 2318b154b69SEd Czeck /* Free the mbufs allocated */ 232be410a86SEd Czeck for (i = 0, mbuf = queue->reserve_q; 233be410a86SEd Czeck i < queue->seed_index; ++i, mbuf++) { 234be410a86SEd Czeck rte_pktmbuf_free(*mbuf); 2358b154b69SEd Czeck } 2368b154b69SEd Czeck rte_free(queue->reserve_q); 2378b154b69SEd Czeck rte_free(queue->paddress_q); 2388b154b69SEd Czeck rte_free(queue); 2398b154b69SEd Czeck return -1; /* ERROR CODE */ 2408b154b69SEd Czeck } 2418b154b69SEd Czeck 2428b154b69SEd Czeck return 0; 2438b154b69SEd Czeck } 2448b154b69SEd Czeck 2458b154b69SEd Czeck /* ************************************************************************* */ 2468b154b69SEd Czeck uint16_t 2478b154b69SEd Czeck eth_ark_recv_pkts(void *rx_queue, 2488b154b69SEd Czeck struct rte_mbuf **rx_pkts, 2498b154b69SEd Czeck uint16_t nb_pkts) 2508b154b69SEd Czeck { 2518b154b69SEd Czeck struct ark_rx_queue *queue; 2528b154b69SEd Czeck register uint32_t cons_index, prod_index; 2538b154b69SEd Czeck uint16_t nb; 2546c7f491eSEd Czeck uint16_t i; 2558b154b69SEd Czeck struct rte_mbuf *mbuf; 2566c7f491eSEd Czeck struct rte_mbuf **pmbuf; 2578b154b69SEd Czeck struct ark_rx_meta *meta; 2586c7f491eSEd Czeck rx_user_meta_hook_fn rx_user_meta_hook; 2598b154b69SEd Czeck 2608b154b69SEd Czeck queue = (struct ark_rx_queue *)rx_queue; 2618b154b69SEd Czeck if (unlikely(queue == 0)) 2628b154b69SEd Czeck return 0; 2638b154b69SEd Czeck if (unlikely(nb_pkts == 0)) 2648b154b69SEd Czeck return 0; 2658b154b69SEd Czeck prod_index = queue->prod_index; 2668b154b69SEd Czeck cons_index = queue->cons_index; 2676c7f491eSEd Czeck if (prod_index == cons_index) 2686c7f491eSEd Czeck return 0; 2698b154b69SEd Czeck nb = 0; 2708b154b69SEd Czeck 2718b154b69SEd Czeck while (prod_index != cons_index) { 2728b154b69SEd Czeck mbuf = queue->reserve_q[cons_index & queue->queue_mask]; 2738b154b69SEd Czeck /* prefetch mbuf */ 2748b154b69SEd Czeck rte_mbuf_prefetch_part1(mbuf); 2758b154b69SEd Czeck rte_mbuf_prefetch_part2(mbuf); 2768b154b69SEd Czeck 2778b154b69SEd Czeck /* META DATA embedded in headroom */ 2788b154b69SEd Czeck meta = RTE_PTR_ADD(mbuf->buf_addr, ARK_RX_META_OFFSET); 2798b154b69SEd Czeck 2808b154b69SEd Czeck mbuf->pkt_len = meta->pkt_len; 2818b154b69SEd Czeck mbuf->data_len = meta->pkt_len; 2828b154b69SEd Czeck 2831502d443SEd Czeck if (ARK_DEBUG_CORE) { /* debug sanity checks */ 284*2f27ef73SJohn Miller 2858b154b69SEd Czeck if ((meta->pkt_len > (1024 * 16)) || 2868b154b69SEd Czeck (meta->pkt_len == 0)) { 2871502d443SEd Czeck ARK_PMD_LOG(DEBUG, "RX: Bad Meta Q: %u" 2888b154b69SEd Czeck " cons: %" PRIU32 2898b154b69SEd Czeck " prod: %" PRIU32 2908b154b69SEd Czeck " seed_index %" PRIU32 2918b154b69SEd Czeck "\n", 2928b154b69SEd Czeck queue->phys_qid, 2938b154b69SEd Czeck cons_index, 2948b154b69SEd Czeck queue->prod_index, 2958b154b69SEd Czeck queue->seed_index); 2968b154b69SEd Czeck 2978b154b69SEd Czeck 2981502d443SEd Czeck ARK_PMD_LOG(DEBUG, " : UDM" 2998b154b69SEd Czeck " prod: %" PRIU32 3008b154b69SEd Czeck " len: %u\n", 3018b154b69SEd Czeck queue->udm->rt_cfg.prod_idx, 3028b154b69SEd Czeck meta->pkt_len); 3038b154b69SEd Czeck ark_mpu_dump(queue->mpu, 3048b154b69SEd Czeck " ", 3058b154b69SEd Czeck queue->phys_qid); 3068b154b69SEd Czeck dump_mbuf_data(mbuf, 0, 256); 3078b154b69SEd Czeck /* its FUBAR so fix it */ 3088b154b69SEd Czeck mbuf->pkt_len = 63; 3098b154b69SEd Czeck meta->pkt_len = 63; 3108b154b69SEd Czeck } 3118b154b69SEd Czeck } 3128b154b69SEd Czeck 313*2f27ef73SJohn Miller if (unlikely(meta->pkt_len > queue->dataroom)) 3148b154b69SEd Czeck cons_index = eth_ark_rx_jumbo 3158b154b69SEd Czeck (queue, meta, mbuf, cons_index + 1); 3168b154b69SEd Czeck else 3178b154b69SEd Czeck cons_index += 1; 3188b154b69SEd Czeck 3198b154b69SEd Czeck rx_pkts[nb] = mbuf; 3208b154b69SEd Czeck nb++; 3218b154b69SEd Czeck if (nb >= nb_pkts) 3228b154b69SEd Czeck break; 3238b154b69SEd Czeck } 3248b154b69SEd Czeck 3256c7f491eSEd Czeck rx_user_meta_hook = queue->rx_user_meta_hook; 3266c7f491eSEd Czeck for (pmbuf = rx_pkts, i = 0; rx_user_meta_hook && i < nb; i++) { 3276c7f491eSEd Czeck mbuf = *pmbuf++; 3286c7f491eSEd Czeck meta = RTE_PTR_ADD(mbuf->buf_addr, ARK_RX_META_OFFSET); 3296c7f491eSEd Czeck rx_user_meta_hook(mbuf, meta->user_meta, queue->ext_user_data); 3306c7f491eSEd Czeck } 3316c7f491eSEd Czeck 3328b154b69SEd Czeck eth_ark_rx_update_cons_index(queue, cons_index); 3338b154b69SEd Czeck 3348b154b69SEd Czeck return nb; 3358b154b69SEd Czeck } 3368b154b69SEd Czeck 3378b154b69SEd Czeck /* ************************************************************************* */ 3388b154b69SEd Czeck static uint32_t 3398b154b69SEd Czeck eth_ark_rx_jumbo(struct ark_rx_queue *queue, 3408b154b69SEd Czeck struct ark_rx_meta *meta, 3418b154b69SEd Czeck struct rte_mbuf *mbuf0, 3428b154b69SEd Czeck uint32_t cons_index) 3438b154b69SEd Czeck { 3448b154b69SEd Czeck struct rte_mbuf *mbuf_prev; 3458b154b69SEd Czeck struct rte_mbuf *mbuf; 3468b154b69SEd Czeck 3478b154b69SEd Czeck uint16_t remaining; 3488b154b69SEd Czeck uint16_t data_len; 3496c293ffdSIlya V. Matveychikov uint16_t segments; 3508b154b69SEd Czeck 3518b154b69SEd Czeck /* first buf populated by called */ 3528b154b69SEd Czeck mbuf_prev = mbuf0; 3538b154b69SEd Czeck segments = 1; 354*2f27ef73SJohn Miller data_len = RTE_MIN(meta->pkt_len, queue->dataroom); 3558b154b69SEd Czeck remaining = meta->pkt_len - data_len; 3568b154b69SEd Czeck mbuf0->data_len = data_len; 3578b154b69SEd Czeck 3588b154b69SEd Czeck /* HW guarantees that the data does not exceed prod_index! */ 3598b154b69SEd Czeck while (remaining != 0) { 3608b154b69SEd Czeck data_len = RTE_MIN(remaining, 361*2f27ef73SJohn Miller queue->dataroom); 3628b154b69SEd Czeck 3638b154b69SEd Czeck remaining -= data_len; 3648b154b69SEd Czeck segments += 1; 3658b154b69SEd Czeck 3668b154b69SEd Czeck mbuf = queue->reserve_q[cons_index & queue->queue_mask]; 3678b154b69SEd Czeck mbuf_prev->next = mbuf; 3688b154b69SEd Czeck mbuf_prev = mbuf; 3698b154b69SEd Czeck mbuf->data_len = data_len; 3708b154b69SEd Czeck 3718b154b69SEd Czeck cons_index += 1; 3728b154b69SEd Czeck } 3738b154b69SEd Czeck 3748b154b69SEd Czeck mbuf0->nb_segs = segments; 3758b154b69SEd Czeck return cons_index; 3768b154b69SEd Czeck } 3778b154b69SEd Czeck 3788b154b69SEd Czeck /* Drain the internal queue allowing hw to clear out. */ 3798b154b69SEd Czeck static void 3808b154b69SEd Czeck eth_ark_rx_queue_drain(struct ark_rx_queue *queue) 3818b154b69SEd Czeck { 3828b154b69SEd Czeck register uint32_t cons_index; 3838b154b69SEd Czeck struct rte_mbuf *mbuf; 3848b154b69SEd Czeck 3858b154b69SEd Czeck cons_index = queue->cons_index; 3868b154b69SEd Czeck 3878b154b69SEd Czeck /* NOT performance optimized, since this is a one-shot call */ 3888b154b69SEd Czeck while ((cons_index ^ queue->prod_index) & queue->queue_mask) { 3898b154b69SEd Czeck mbuf = queue->reserve_q[cons_index & queue->queue_mask]; 3908b154b69SEd Czeck rte_pktmbuf_free(mbuf); 3918b154b69SEd Czeck cons_index++; 3928b154b69SEd Czeck eth_ark_rx_update_cons_index(queue, cons_index); 3938b154b69SEd Czeck } 3948b154b69SEd Czeck } 3958b154b69SEd Czeck 3968b154b69SEd Czeck uint32_t 3978d7d4fcdSKonstantin Ananyev eth_ark_dev_rx_queue_count(void *rx_queue) 3988b154b69SEd Czeck { 3998b154b69SEd Czeck struct ark_rx_queue *queue; 4008b154b69SEd Czeck 4018d7d4fcdSKonstantin Ananyev queue = rx_queue; 4028b154b69SEd Czeck return (queue->prod_index - queue->cons_index); /* mod arith */ 4038b154b69SEd Czeck } 4048b154b69SEd Czeck 4058b154b69SEd Czeck /* ************************************************************************* */ 4068b154b69SEd Czeck int 4078b154b69SEd Czeck eth_ark_rx_start_queue(struct rte_eth_dev *dev, uint16_t queue_id) 4088b154b69SEd Czeck { 4098b154b69SEd Czeck struct ark_rx_queue *queue; 4108b154b69SEd Czeck 4118b154b69SEd Czeck queue = dev->data->rx_queues[queue_id]; 4128b154b69SEd Czeck if (queue == 0) 4138b154b69SEd Czeck return -1; 4148b154b69SEd Czeck 4158b154b69SEd Czeck dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STARTED; 4168b154b69SEd Czeck 4178b154b69SEd Czeck ark_mpu_set_producer(queue->mpu, queue->seed_index); 4188b154b69SEd Czeck ark_mpu_start(queue->mpu); 4198b154b69SEd Czeck 4208b154b69SEd Czeck ark_udm_queue_enable(queue->udm, 1); 4218b154b69SEd Czeck 4228b154b69SEd Czeck return 0; 4238b154b69SEd Czeck } 4248b154b69SEd Czeck 4258b154b69SEd Czeck /* ************************************************************************* */ 4268b154b69SEd Czeck 4278b154b69SEd Czeck /* Queue can be restarted. data remains 4288b154b69SEd Czeck */ 4298b154b69SEd Czeck int 4308b154b69SEd Czeck eth_ark_rx_stop_queue(struct rte_eth_dev *dev, uint16_t queue_id) 4318b154b69SEd Czeck { 4328b154b69SEd Czeck struct ark_rx_queue *queue; 4338b154b69SEd Czeck 4348b154b69SEd Czeck queue = dev->data->rx_queues[queue_id]; 4358b154b69SEd Czeck if (queue == 0) 4368b154b69SEd Czeck return -1; 4378b154b69SEd Czeck 4388b154b69SEd Czeck ark_udm_queue_enable(queue->udm, 0); 4398b154b69SEd Czeck 4408b154b69SEd Czeck dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STOPPED; 4418b154b69SEd Czeck 4428b154b69SEd Czeck return 0; 4438b154b69SEd Czeck } 4448b154b69SEd Czeck 4458b154b69SEd Czeck /* ************************************************************************* */ 4468b154b69SEd Czeck static inline int 4478b154b69SEd Czeck eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue) 4488b154b69SEd Czeck { 4498b154b69SEd Czeck uint32_t limit = queue->cons_index + queue->queue_size; 4508b154b69SEd Czeck uint32_t seed_index = queue->seed_index; 4518b154b69SEd Czeck 4528b154b69SEd Czeck uint32_t count = 0; 4538b154b69SEd Czeck uint32_t seed_m = queue->seed_index & queue->queue_mask; 4548b154b69SEd Czeck 4558b154b69SEd Czeck uint32_t nb = limit - seed_index; 4568b154b69SEd Czeck 4578b154b69SEd Czeck /* Handle wrap around -- remainder is filled on the next call */ 4588b154b69SEd Czeck if (unlikely(seed_m + nb > queue->queue_size)) 4598b154b69SEd Czeck nb = queue->queue_size - seed_m; 4608b154b69SEd Czeck 4618b154b69SEd Czeck struct rte_mbuf **mbufs = &queue->reserve_q[seed_m]; 4628b154b69SEd Czeck int status = rte_pktmbuf_alloc_bulk(queue->mb_pool, mbufs, nb); 4638b154b69SEd Czeck 464be410a86SEd Czeck if (unlikely(status != 0)) { 46531d7ddedSEd Czeck ARK_PMD_LOG(NOTICE, 46631d7ddedSEd Czeck "Could not allocate %u mbufs from pool" 46731d7ddedSEd Czeck " for RX queue %u;" 46831d7ddedSEd Czeck " %u free buffers remaining in queue\n", 46931d7ddedSEd Czeck nb, queue->queue_index, 47031d7ddedSEd Czeck queue->seed_index - queue->cons_index); 4718b154b69SEd Czeck return -1; 472be410a86SEd Czeck } 4738b154b69SEd Czeck 4741502d443SEd Czeck if (ARK_DEBUG_CORE) { /* DEBUG */ 4758b154b69SEd Czeck while (count != nb) { 4768b154b69SEd Czeck struct rte_mbuf *mbuf_init = 4778b154b69SEd Czeck queue->reserve_q[seed_m + count]; 4788b154b69SEd Czeck 4798b154b69SEd Czeck memset(mbuf_init->buf_addr, -1, 512); 4808b154b69SEd Czeck *((uint32_t *)mbuf_init->buf_addr) = 4818b154b69SEd Czeck seed_index + count; 4828b154b69SEd Czeck *(uint16_t *)RTE_PTR_ADD(mbuf_init->buf_addr, 4) = 4838b154b69SEd Czeck queue->phys_qid; 4848b154b69SEd Czeck count++; 4858b154b69SEd Czeck } 4868b154b69SEd Czeck count = 0; 4878b154b69SEd Czeck } /* DEBUG */ 4888b154b69SEd Czeck queue->seed_index += nb; 4898b154b69SEd Czeck 4908b154b69SEd Czeck /* Duff's device https://en.wikipedia.org/wiki/Duff's_device */ 4918b154b69SEd Czeck switch (nb % 4) { 4928b154b69SEd Czeck case 0: 4938b154b69SEd Czeck while (count != nb) { 4948b154b69SEd Czeck queue->paddress_q[seed_m++] = 495455da545SSantosh Shukla (*mbufs++)->buf_iova; 4968b154b69SEd Czeck count++; 4978b154b69SEd Czeck /* FALLTHROUGH */ 4988b154b69SEd Czeck case 3: 4998b154b69SEd Czeck queue->paddress_q[seed_m++] = 500455da545SSantosh Shukla (*mbufs++)->buf_iova; 5018b154b69SEd Czeck count++; 5028b154b69SEd Czeck /* FALLTHROUGH */ 5038b154b69SEd Czeck case 2: 5048b154b69SEd Czeck queue->paddress_q[seed_m++] = 505455da545SSantosh Shukla (*mbufs++)->buf_iova; 5068b154b69SEd Czeck count++; 5078b154b69SEd Czeck /* FALLTHROUGH */ 5088b154b69SEd Czeck case 1: 5098b154b69SEd Czeck queue->paddress_q[seed_m++] = 510455da545SSantosh Shukla (*mbufs++)->buf_iova; 5118b154b69SEd Czeck count++; 5128b154b69SEd Czeck /* FALLTHROUGH */ 5138b154b69SEd Czeck 5148b154b69SEd Czeck } /* while (count != nb) */ 5158b154b69SEd Czeck } /* switch */ 5168b154b69SEd Czeck 5178b154b69SEd Czeck return 0; 5188b154b69SEd Czeck } 5198b154b69SEd Czeck 5208b154b69SEd Czeck void 5218b154b69SEd Czeck eth_ark_rx_dump_queue(struct rte_eth_dev *dev, uint16_t queue_id, 5228b154b69SEd Czeck const char *msg) 5238b154b69SEd Czeck { 5248b154b69SEd Czeck struct ark_rx_queue *queue; 5258b154b69SEd Czeck 5268b154b69SEd Czeck queue = dev->data->rx_queues[queue_id]; 5278b154b69SEd Czeck 5288b154b69SEd Czeck ark_ethdev_rx_dump(msg, queue); 5298b154b69SEd Czeck } 5308b154b69SEd Czeck 5318b154b69SEd Czeck /* ************************************************************************* */ 5328b154b69SEd Czeck /* Call on device closed no user API, queue is stopped */ 5338b154b69SEd Czeck void 5348b154b69SEd Czeck eth_ark_dev_rx_queue_release(void *vqueue) 5358b154b69SEd Czeck { 5368b154b69SEd Czeck struct ark_rx_queue *queue; 5378b154b69SEd Czeck uint32_t i; 5388b154b69SEd Czeck 5398b154b69SEd Czeck queue = (struct ark_rx_queue *)vqueue; 5408b154b69SEd Czeck if (queue == 0) 5418b154b69SEd Czeck return; 5428b154b69SEd Czeck 5438b154b69SEd Czeck ark_udm_queue_enable(queue->udm, 0); 5448b154b69SEd Czeck /* Stop the MPU since pointer are going away */ 5458b154b69SEd Czeck ark_mpu_stop(queue->mpu); 5468b154b69SEd Czeck 5478b154b69SEd Czeck /* Need to clear out mbufs here, dropping packets along the way */ 5488b154b69SEd Czeck eth_ark_rx_queue_drain(queue); 5498b154b69SEd Czeck 5508b154b69SEd Czeck for (i = 0; i < queue->queue_size; ++i) 5518b154b69SEd Czeck rte_pktmbuf_free(queue->reserve_q[i]); 5528b154b69SEd Czeck 5538b154b69SEd Czeck rte_free(queue->reserve_q); 5548b154b69SEd Czeck rte_free(queue->paddress_q); 5558b154b69SEd Czeck rte_free(queue); 5568b154b69SEd Czeck } 5578b154b69SEd Czeck 5588b154b69SEd Czeck void 5598b154b69SEd Czeck eth_rx_queue_stats_get(void *vqueue, struct rte_eth_stats *stats) 5608b154b69SEd Czeck { 5618b154b69SEd Czeck struct ark_rx_queue *queue; 5628b154b69SEd Czeck struct ark_udm_t *udm; 5638b154b69SEd Czeck 5648b154b69SEd Czeck queue = vqueue; 5658b154b69SEd Czeck if (queue == 0) 5668b154b69SEd Czeck return; 5678b154b69SEd Czeck udm = queue->udm; 5688b154b69SEd Czeck 5698b154b69SEd Czeck uint64_t ibytes = ark_udm_bytes(udm); 5708b154b69SEd Czeck uint64_t ipackets = ark_udm_packets(udm); 5718b154b69SEd Czeck uint64_t idropped = ark_udm_dropped(queue->udm); 5728b154b69SEd Czeck 5738b154b69SEd Czeck stats->q_ipackets[queue->queue_index] = ipackets; 5748b154b69SEd Czeck stats->q_ibytes[queue->queue_index] = ibytes; 5758b154b69SEd Czeck stats->q_errors[queue->queue_index] = idropped; 5768b154b69SEd Czeck stats->ipackets += ipackets; 5778b154b69SEd Czeck stats->ibytes += ibytes; 5788b154b69SEd Czeck stats->imissed += idropped; 5798b154b69SEd Czeck } 5808b154b69SEd Czeck 5818b154b69SEd Czeck void 5828b154b69SEd Czeck eth_rx_queue_stats_reset(void *vqueue) 5838b154b69SEd Czeck { 5848b154b69SEd Czeck struct ark_rx_queue *queue; 5858b154b69SEd Czeck 5868b154b69SEd Czeck queue = vqueue; 5878b154b69SEd Czeck if (queue == 0) 5888b154b69SEd Czeck return; 5898b154b69SEd Czeck 5908b154b69SEd Czeck ark_mpu_reset_stats(queue->mpu); 5918b154b69SEd Czeck ark_udm_queue_stats_reset(queue->udm); 5928b154b69SEd Czeck } 5938b154b69SEd Czeck 5948b154b69SEd Czeck void 5958b154b69SEd Czeck eth_ark_udm_force_close(struct rte_eth_dev *dev) 5968b154b69SEd Czeck { 5970bf8b0f1SStephen Hemminger struct ark_adapter *ark = dev->data->dev_private; 5988b154b69SEd Czeck struct ark_rx_queue *queue; 5998b154b69SEd Czeck uint32_t index; 6008b154b69SEd Czeck uint16_t i; 6018b154b69SEd Czeck 6028b154b69SEd Czeck if (!ark_udm_is_flushed(ark->udm.v)) { 6038b154b69SEd Czeck /* restart the MPUs */ 6041502d443SEd Czeck ARK_PMD_LOG(NOTICE, "UDM not flushed -- forcing flush\n"); 6058b154b69SEd Czeck for (i = 0; i < dev->data->nb_rx_queues; i++) { 6068b154b69SEd Czeck queue = (struct ark_rx_queue *)dev->data->rx_queues[i]; 6078b154b69SEd Czeck if (queue == 0) 6088b154b69SEd Czeck continue; 6098b154b69SEd Czeck 6108b154b69SEd Czeck ark_mpu_start(queue->mpu); 6118b154b69SEd Czeck /* Add some buffers */ 6128b154b69SEd Czeck index = 100000 + queue->seed_index; 6138b154b69SEd Czeck ark_mpu_set_producer(queue->mpu, index); 6148b154b69SEd Czeck } 6158b154b69SEd Czeck /* Wait to allow data to pass */ 6168b154b69SEd Czeck usleep(100); 6178b154b69SEd Czeck 6181502d443SEd Czeck ARK_PMD_LOG(DEBUG, "UDM forced flush attempt, stopped = %d\n", 6198b154b69SEd Czeck ark_udm_is_flushed(ark->udm.v)); 6208b154b69SEd Czeck } 6218b154b69SEd Czeck ark_udm_reset(ark->udm.v); 6228b154b69SEd Czeck } 6238b154b69SEd Czeck 6248b154b69SEd Czeck static void 6258b154b69SEd Czeck ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue) 6268b154b69SEd Czeck { 6278b154b69SEd Czeck if (queue == NULL) 6288b154b69SEd Czeck return; 6291502d443SEd Czeck ARK_PMD_LOG(DEBUG, "RX QUEUE %d -- %s", queue->phys_qid, name); 6301502d443SEd Czeck ARK_PMD_LOG(DEBUG, ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32 "\n", 6318b154b69SEd Czeck "queue_size", queue->queue_size, 6328b154b69SEd Czeck "seed_index", queue->seed_index, 6338b154b69SEd Czeck "prod_index", queue->prod_index, 6348b154b69SEd Czeck "cons_index", queue->cons_index); 6358b154b69SEd Czeck 6368b154b69SEd Czeck ark_mpu_dump(queue->mpu, name, queue->phys_qid); 6378b154b69SEd Czeck ark_mpu_dump_setup(queue->mpu, queue->phys_qid); 6388b154b69SEd Czeck ark_udm_dump(queue->udm, name); 6398b154b69SEd Czeck ark_udm_dump_setup(queue->udm, queue->phys_qid); 6408b154b69SEd Czeck } 6418b154b69SEd Czeck 6428b154b69SEd Czeck /* Only used in debug. 6438b154b69SEd Czeck * This function is a raw memory dump of a portion of an mbuf's memory 6448b154b69SEd Czeck * region. The usual function, rte_pktmbuf_dump() only shows data 6458b154b69SEd Czeck * with respect to the data_off field. This function show data 6468b154b69SEd Czeck * anywhere in the mbuf's buffer. This is useful for examining 6478b154b69SEd Czeck * data in the headroom or tailroom portion of an mbuf. 6488b154b69SEd Czeck */ 6498b154b69SEd Czeck static void 6508b154b69SEd Czeck dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi) 6518b154b69SEd Czeck { 6528b154b69SEd Czeck uint16_t i, j; 6538b154b69SEd Czeck 654fea8ea18SDavid Marchand ARK_PMD_LOG(DEBUG, " MBUF: %p len %d, off: %d\n", 655fea8ea18SDavid Marchand mbuf, mbuf->pkt_len, mbuf->data_off); 6568b154b69SEd Czeck for (i = lo; i < hi; i += 16) { 6578b154b69SEd Czeck uint8_t *dp = RTE_PTR_ADD(mbuf->buf_addr, i); 6588b154b69SEd Czeck 6591502d443SEd Czeck ARK_PMD_LOG(DEBUG, " %6d: ", i); 6608b154b69SEd Czeck for (j = 0; j < 16; j++) 6611502d443SEd Czeck ARK_PMD_LOG(DEBUG, " %02x", dp[j]); 6628b154b69SEd Czeck 6631502d443SEd Czeck ARK_PMD_LOG(DEBUG, "\n"); 6648b154b69SEd Czeck } 6658b154b69SEd Czeck } 666