1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2015-2018 Atomic Rules LLC
7 #include "ark_ethdev_rx.h"
8 #include "ark_global.h"
13 #define ARK_RX_META_SIZE 32
14 #define ARK_RX_META_OFFSET (RTE_PKTMBUF_HEADROOM - ARK_RX_META_SIZE)
15 #define ARK_RX_MPU_CHUNK (64U)
17 /* Forward declarations */
21 static void dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi);
22 static void ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue);
23 static uint32_t eth_ark_rx_jumbo(struct ark_rx_queue *queue,
24 struct ark_rx_meta *meta,
25 struct rte_mbuf *mbuf0,
27 static inline int eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue);
29 /* ************************************************************************* */
31 /* array of mbufs to populate */
32 struct rte_mbuf **reserve_q;
33 /* array of physical addresses of the mbuf data pointer */
34 /* This point is a virtual address */
35 rte_iova_t *paddress_q;
36 struct rte_mempool *mb_pool;
38 struct ark_udm_t *udm;
39 struct ark_mpu_t *mpu;
41 rx_user_meta_hook_fn rx_user_meta_hook;
50 uint32_t seed_index; /* step 1 set with empty mbuf */
51 uint32_t cons_index; /* step 3 consumed by driver */
53 /* The queue Id is used to identify the HW Q */
56 /* The queue Index is used within the dpdk device structures */
61 /* next cache line - fields written by device */
62 RTE_MARKER cacheline1 __rte_cache_min_aligned;
64 volatile uint32_t prod_index; /* step 2 filled by FPGA */
65 } __rte_cache_aligned;
67 /* ************************************************************************* */
69 eth_ark_rx_hw_setup(struct rte_eth_dev *dev,
70 struct ark_rx_queue *queue,
71 uint16_t rx_queue_id __rte_unused, uint16_t rx_queue_idx)
73 rte_iova_t queue_base;
74 rte_iova_t phys_addr_q_base;
75 rte_iova_t phys_addr_prod_index;
77 queue_base = rte_malloc_virt2iova(queue);
78 phys_addr_prod_index = queue_base +
79 offsetof(struct ark_rx_queue, prod_index);
81 phys_addr_q_base = rte_malloc_virt2iova(queue->paddress_q);
84 if (ark_mpu_verify(queue->mpu, sizeof(rte_iova_t))) {
85 ARK_PMD_LOG(ERR, "Illegal configuration rx queue\n");
89 /* Stop and Reset and configure MPU */
90 ark_mpu_configure(queue->mpu, phys_addr_q_base, queue->queue_size, 0);
92 ark_udm_write_addr(queue->udm, phys_addr_prod_index);
94 /* advance the valid pointer, but don't start until the queue starts */
95 ark_mpu_reset_stats(queue->mpu);
97 /* The seed is the producer index for the HW */
98 ark_mpu_set_producer(queue->mpu, queue->seed_index);
99 dev->data->rx_queue_state[rx_queue_idx] = RTE_ETH_QUEUE_STATE_STOPPED;
105 eth_ark_rx_update_cons_index(struct ark_rx_queue *queue, uint32_t cons_index)
107 queue->cons_index = cons_index;
108 if ((cons_index + queue->queue_size - queue->seed_index) >= ARK_RX_MPU_CHUNK) {
109 eth_ark_rx_seed_mbufs(queue);
110 ark_mpu_set_producer(queue->mpu, queue->seed_index);
114 /* ************************************************************************* */
116 eth_ark_dev_rx_queue_setup(struct rte_eth_dev *dev,
119 unsigned int socket_id,
120 const struct rte_eth_rxconf *rx_conf,
121 struct rte_mempool *mb_pool)
123 static int warning1; /* = 0 */
124 struct ark_adapter *ark = dev->data->dev_private;
126 struct ark_rx_queue *queue;
130 int qidx = queue_idx;
132 /* We may already be setup, free memory prior to re-allocation */
133 if (dev->data->rx_queues[queue_idx] != NULL) {
134 eth_ark_dev_rx_queue_release(dev->data->rx_queues[queue_idx]);
135 dev->data->rx_queues[queue_idx] = NULL;
138 if (rx_conf != NULL && warning1 == 0) {
141 "Arkville ignores rte_eth_rxconf argument.\n");
144 if (RTE_PKTMBUF_HEADROOM < ARK_RX_META_SIZE) {
146 "Error: DPDK Arkville requires head room > %d bytes (%s)\n",
147 ARK_RX_META_SIZE, __func__);
148 return -1; /* ERROR CODE */
151 if (!rte_is_power_of_2(nb_desc)) {
153 "DPDK Arkville configuration queue size must be power of two %u (%s)\n",
155 return -1; /* ERROR CODE */
158 /* Allocate queue struct */
159 queue = rte_zmalloc_socket("Ark_rxqueue",
160 sizeof(struct ark_rx_queue),
164 ARK_PMD_LOG(ERR, "Failed to allocate memory in %s\n", __func__);
168 /* NOTE zmalloc is used, no need to 0 indexes, etc. */
169 queue->mb_pool = mb_pool;
170 queue->dataroom = rte_pktmbuf_data_room_size(mb_pool) -
171 RTE_PKTMBUF_HEADROOM;
172 queue->headroom = RTE_PKTMBUF_HEADROOM;
173 queue->phys_qid = qidx;
174 queue->queue_index = queue_idx;
175 queue->queue_size = nb_desc;
176 queue->queue_mask = nb_desc - 1;
177 queue->rx_user_meta_hook = ark->user_ext.rx_user_meta_hook;
178 queue->ext_user_data = ark->user_data[dev->data->port_id];
181 rte_zmalloc_socket("Ark_rx_queue mbuf",
182 nb_desc * sizeof(struct rte_mbuf *),
186 rte_zmalloc_socket("Ark_rx_queue paddr",
187 nb_desc * sizeof(rte_iova_t),
191 if (queue->reserve_q == 0 || queue->paddress_q == 0) {
193 "Failed to allocate queue memory in %s\n",
195 rte_free(queue->reserve_q);
196 rte_free(queue->paddress_q);
201 dev->data->rx_queues[queue_idx] = queue;
202 queue->udm = RTE_PTR_ADD(ark->udm.v, qidx * ARK_UDM_QOFFSET);
203 queue->mpu = RTE_PTR_ADD(ark->mpurx.v, qidx * ARK_MPU_QOFFSET);
205 /* Configure UDM per queue */
206 ark_udm_stop(queue->udm, 0);
207 ark_udm_configure(queue->udm,
208 RTE_PKTMBUF_HEADROOM,
210 ARK_RX_WRITE_TIME_NS);
211 ark_udm_stats_reset(queue->udm);
212 ark_udm_stop(queue->udm, 0);
214 /* populate mbuf reserve */
215 status = eth_ark_rx_seed_mbufs(queue);
217 if (queue->seed_index != nb_desc) {
218 ARK_PMD_LOG(ERR, "Failed to allocate %u mbufs for RX queue %d\n",
224 status = eth_ark_rx_hw_setup(dev, queue, qidx, queue_idx);
226 if (unlikely(status != 0)) {
227 struct rte_mbuf **mbuf;
229 ARK_PMD_LOG(ERR, "Failed to initialize RX queue %d %s\n",
232 /* Free the mbufs allocated */
233 for (i = 0, mbuf = queue->reserve_q;
234 i < queue->seed_index; ++i, mbuf++) {
235 rte_pktmbuf_free(*mbuf);
237 rte_free(queue->reserve_q);
238 rte_free(queue->paddress_q);
240 return -1; /* ERROR CODE */
246 /* ************************************************************************* */
248 eth_ark_recv_pkts(void *rx_queue,
249 struct rte_mbuf **rx_pkts,
252 struct ark_rx_queue *queue;
253 register uint32_t cons_index, prod_index;
256 struct rte_mbuf *mbuf;
257 struct rte_mbuf **pmbuf;
258 struct ark_rx_meta *meta;
259 rx_user_meta_hook_fn rx_user_meta_hook;
261 queue = (struct ark_rx_queue *)rx_queue;
262 if (unlikely(queue == 0))
264 if (unlikely(nb_pkts == 0))
266 prod_index = queue->prod_index;
267 cons_index = queue->cons_index;
268 if (prod_index == cons_index)
272 while (prod_index != cons_index) {
273 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
275 rte_mbuf_prefetch_part1(mbuf);
276 rte_mbuf_prefetch_part2(mbuf);
278 /* META DATA embedded in headroom */
279 meta = RTE_PTR_ADD(mbuf->buf_addr, ARK_RX_META_OFFSET);
281 mbuf->pkt_len = meta->pkt_len;
282 mbuf->data_len = meta->pkt_len;
284 if (ARK_DEBUG_CORE) { /* debug sanity checks */
286 if ((meta->pkt_len > (1024 * 16)) ||
287 (meta->pkt_len == 0)) {
288 ARK_PMD_LOG(DEBUG, "RX: Bad Meta Q: %u"
291 " seed_index %" PRIU32
299 ARK_PMD_LOG(DEBUG, " : UDM"
302 queue->udm->rt_cfg.prod_idx,
304 ark_mpu_dump(queue->mpu,
307 dump_mbuf_data(mbuf, 0, 256);
308 /* its FUBAR so fix it */
314 if (unlikely(meta->pkt_len > queue->dataroom))
315 cons_index = eth_ark_rx_jumbo
316 (queue, meta, mbuf, cons_index + 1);
326 rx_user_meta_hook = queue->rx_user_meta_hook;
327 for (pmbuf = rx_pkts, i = 0; rx_user_meta_hook && i < nb; i++) {
329 meta = RTE_PTR_ADD(mbuf->buf_addr, ARK_RX_META_OFFSET);
330 rx_user_meta_hook(mbuf, meta->user_meta, queue->ext_user_data);
333 eth_ark_rx_update_cons_index(queue, cons_index);
338 /* ************************************************************************* */
340 eth_ark_rx_jumbo(struct ark_rx_queue *queue,
341 struct ark_rx_meta *meta,
342 struct rte_mbuf *mbuf0,
345 struct rte_mbuf *mbuf_prev;
346 struct rte_mbuf *mbuf;
352 /* first buf populated by called */
355 data_len = RTE_MIN(meta->pkt_len, queue->dataroom);
356 remaining = meta->pkt_len - data_len;
357 mbuf0->data_len = data_len;
359 /* HW guarantees that the data does not exceed prod_index! */
360 while (remaining != 0) {
361 data_len = RTE_MIN(remaining,
364 remaining -= data_len;
367 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
368 mbuf_prev->next = mbuf;
370 mbuf->data_len = data_len;
375 mbuf0->nb_segs = segments;
379 /* Drain the internal queue allowing hw to clear out. */
381 eth_ark_rx_queue_drain(struct ark_rx_queue *queue)
383 register uint32_t cons_index;
384 struct rte_mbuf *mbuf;
386 cons_index = queue->cons_index;
388 /* NOT performance optimized, since this is a one-shot call */
389 while ((cons_index ^ queue->prod_index) & queue->queue_mask) {
390 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
391 rte_pktmbuf_free(mbuf);
393 eth_ark_rx_update_cons_index(queue, cons_index);
398 eth_ark_dev_rx_queue_count(void *rx_queue)
400 struct ark_rx_queue *queue;
403 return (queue->prod_index - queue->cons_index); /* mod arith */
406 /* ************************************************************************* */
408 eth_ark_rx_start_queue(struct rte_eth_dev *dev, uint16_t queue_id)
410 struct ark_rx_queue *queue;
412 queue = dev->data->rx_queues[queue_id];
416 dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
418 ark_mpu_set_producer(queue->mpu, queue->seed_index);
419 ark_mpu_start(queue->mpu);
421 ark_udm_queue_enable(queue->udm, 1);
426 /* ************************************************************************* */
428 /* Queue can be restarted. data remains
431 eth_ark_rx_stop_queue(struct rte_eth_dev *dev, uint16_t queue_id)
433 struct ark_rx_queue *queue;
435 queue = dev->data->rx_queues[queue_id];
439 ark_udm_queue_enable(queue->udm, 0);
441 dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
446 /* ************************************************************************* */
448 eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue)
450 uint32_t limit = (queue->cons_index & ~(ARK_RX_MPU_CHUNK - 1)) +
452 uint32_t seed_index = queue->seed_index;
455 uint32_t seed_m = queue->seed_index & queue->queue_mask;
457 uint32_t nb = limit - seed_index;
459 /* Handle wrap around -- remainder is filled on the next call */
460 if (unlikely(seed_m + nb > queue->queue_size))
461 nb = queue->queue_size - seed_m;
463 struct rte_mbuf **mbufs = &queue->reserve_q[seed_m];
464 int status = rte_pktmbuf_alloc_bulk(queue->mb_pool, mbufs, nb);
466 if (unlikely(status != 0)) {
468 "Could not allocate %u mbufs from pool"
470 " %u free buffers remaining in queue\n",
471 nb, queue->queue_index,
472 queue->seed_index - queue->cons_index);
476 if (ARK_DEBUG_CORE) { /* DEBUG */
477 while (count != nb) {
478 struct rte_mbuf *mbuf_init =
479 queue->reserve_q[seed_m + count];
481 memset(mbuf_init->buf_addr, -1, 512);
482 *((uint32_t *)mbuf_init->buf_addr) =
484 *(uint16_t *)RTE_PTR_ADD(mbuf_init->buf_addr, 4) =
490 queue->seed_index += nb;
492 /* Duff's device https://en.wikipedia.org/wiki/Duff's_device */
495 while (count != nb) {
496 queue->paddress_q[seed_m++] =
497 (*mbufs++)->buf_iova;
501 queue->paddress_q[seed_m++] =
502 (*mbufs++)->buf_iova;
506 queue->paddress_q[seed_m++] =
507 (*mbufs++)->buf_iova;
511 queue->paddress_q[seed_m++] =
512 (*mbufs++)->buf_iova;
516 } /* while (count != nb) */
523 eth_ark_rx_dump_queue(struct rte_eth_dev *dev, uint16_t queue_id,
526 struct ark_rx_queue *queue;
528 queue = dev->data->rx_queues[queue_id];
530 ark_ethdev_rx_dump(msg, queue);
533 /* ************************************************************************* */
534 /* Call on device closed no user API, queue is stopped */
536 eth_ark_dev_rx_queue_release(void *vqueue)
538 struct ark_rx_queue *queue;
541 queue = (struct ark_rx_queue *)vqueue;
545 ark_udm_queue_enable(queue->udm, 0);
546 /* Stop the MPU since pointer are going away */
547 ark_mpu_stop(queue->mpu);
549 /* Need to clear out mbufs here, dropping packets along the way */
550 eth_ark_rx_queue_drain(queue);
552 for (i = 0; i < queue->queue_size; ++i)
553 rte_pktmbuf_free(queue->reserve_q[i]);
555 rte_free(queue->reserve_q);
556 rte_free(queue->paddress_q);
561 eth_rx_queue_stats_get(void *vqueue, struct rte_eth_stats *stats)
563 struct ark_rx_queue *queue;
564 struct ark_udm_t *udm;
571 uint64_t ibytes = ark_udm_bytes(udm);
572 uint64_t ipackets = ark_udm_packets(udm);
573 uint64_t idropped = ark_udm_dropped(queue->udm);
575 stats->q_ipackets[queue->queue_index] = ipackets;
576 stats->q_ibytes[queue->queue_index] = ibytes;
577 stats->q_errors[queue->queue_index] = idropped;
578 stats->ipackets += ipackets;
579 stats->ibytes += ibytes;
580 stats->imissed += idropped;
584 eth_rx_queue_stats_reset(void *vqueue)
586 struct ark_rx_queue *queue;
592 ark_mpu_reset_stats(queue->mpu);
593 ark_udm_queue_stats_reset(queue->udm);
597 eth_ark_udm_force_close(struct rte_eth_dev *dev)
599 struct ark_adapter *ark = dev->data->dev_private;
600 struct ark_rx_queue *queue;
604 if (!ark_udm_is_flushed(ark->udm.v)) {
605 /* restart the MPUs */
606 ARK_PMD_LOG(NOTICE, "UDM not flushed -- forcing flush\n");
607 for (i = 0; i < dev->data->nb_rx_queues; i++) {
608 queue = (struct ark_rx_queue *)dev->data->rx_queues[i];
612 ark_mpu_start(queue->mpu);
613 /* Add some buffers */
614 index = ARK_RX_MPU_CHUNK + queue->seed_index;
615 ark_mpu_set_producer(queue->mpu, index);
617 /* Wait to allow data to pass */
620 ARK_PMD_LOG(NOTICE, "UDM forced flush attempt, stopped = %d\n",
621 ark_udm_is_flushed(ark->udm.v));
623 ark_udm_reset(ark->udm.v);
627 ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue)
631 ARK_PMD_LOG(DEBUG, "RX QUEUE %d -- %s", queue->phys_qid, name);
632 ARK_PMD_LOG(DEBUG, ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32 "\n",
633 "queue_size", queue->queue_size,
634 "seed_index", queue->seed_index,
635 "prod_index", queue->prod_index,
636 "cons_index", queue->cons_index);
638 ark_mpu_dump(queue->mpu, name, queue->phys_qid);
639 ark_mpu_dump_setup(queue->mpu, queue->phys_qid);
640 ark_udm_dump(queue->udm, name);
641 ark_udm_dump_setup(queue->udm, queue->phys_qid);
644 /* Only used in debug.
645 * This function is a raw memory dump of a portion of an mbuf's memory
646 * region. The usual function, rte_pktmbuf_dump() only shows data
647 * with respect to the data_off field. This function show data
648 * anywhere in the mbuf's buffer. This is useful for examining
649 * data in the headroom or tailroom portion of an mbuf.
652 dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi)
656 ARK_PMD_LOG(DEBUG, " MBUF: %p len %d, off: %d\n",
657 mbuf, mbuf->pkt_len, mbuf->data_off);
658 for (i = lo; i < hi; i += 16) {
659 uint8_t *dp = RTE_PTR_ADD(mbuf->buf_addr, i);
661 ARK_PMD_LOG(DEBUG, " %6d: ", i);
662 for (j = 0; j < 16; j++)
663 ARK_PMD_LOG(DEBUG, " %02x", dp[j]);
665 ARK_PMD_LOG(DEBUG, "\n");