1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2015-2018 Atomic Rules LLC
7 #include "rte_pmd_ark.h"
8 #include "ark_ethdev_rx.h"
9 #include "ark_global.h"
14 #define ARK_RX_META_SIZE 32
15 #define ARK_RX_META_OFFSET (RTE_PKTMBUF_HEADROOM - ARK_RX_META_SIZE)
16 #define ARK_RX_MAX_NOCHAIN (RTE_MBUF_DEFAULT_DATAROOM)
18 /* Forward declarations */
22 static void dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi);
23 static void ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue);
24 static uint32_t eth_ark_rx_jumbo(struct ark_rx_queue *queue,
25 struct ark_rx_meta *meta,
26 struct rte_mbuf *mbuf0,
28 static inline int eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue);
29 static int eth_ark_rx_seed_recovery(struct ark_rx_queue *queue,
31 struct rte_mbuf **mbufs);
33 /* ************************************************************************* */
35 /* array of mbufs to populate */
36 struct rte_mbuf **reserve_q;
37 /* array of physical addresses of the mbuf data pointer */
38 /* This point is a virtual address */
39 rte_iova_t *paddress_q;
40 struct rte_mempool *mb_pool;
42 struct ark_udm_t *udm;
43 struct ark_mpu_t *mpu;
48 uint32_t seed_index; /* step 1 set with empty mbuf */
49 uint32_t cons_index; /* step 3 consumed by driver */
51 /* The queue Id is used to identify the HW Q */
54 /* The queue Index is used within the dpdk device structures */
59 /* separate cache line */
60 /* second cache line - fields only used in slow path */
61 RTE_MARKER cacheline1 __rte_cache_min_aligned;
63 volatile uint32_t prod_index; /* step 2 filled by FPGA */
64 } __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 eth_ark_rx_seed_mbufs(queue);
109 if (((cons_index - queue->last_cons) >= 64U)) {
110 queue->last_cons = cons_index;
111 ark_mpu_set_producer(queue->mpu, queue->seed_index);
115 /* ************************************************************************* */
117 eth_ark_dev_rx_queue_setup(struct rte_eth_dev *dev,
120 unsigned int socket_id,
121 const struct rte_eth_rxconf *rx_conf,
122 struct rte_mempool *mb_pool)
124 static int warning1; /* = 0 */
125 struct ark_adapter *ark = dev->data->dev_private;
127 struct ark_rx_queue *queue;
131 int qidx = queue_idx;
133 /* We may already be setup, free memory prior to re-allocation */
134 if (dev->data->rx_queues[queue_idx] != NULL) {
135 eth_ark_dev_rx_queue_release(dev->data->rx_queues[queue_idx]);
136 dev->data->rx_queues[queue_idx] = NULL;
139 if (rx_conf != NULL && warning1 == 0) {
142 "Arkville ignores rte_eth_rxconf argument.\n");
145 if (RTE_PKTMBUF_HEADROOM < ARK_RX_META_SIZE) {
147 "Error: DPDK Arkville requires head room > %d bytes (%s)\n",
148 ARK_RX_META_SIZE, __func__);
149 return -1; /* ERROR CODE */
152 if (!rte_is_power_of_2(nb_desc)) {
154 "DPDK Arkville configuration queue size must be power of two %u (%s)\n",
156 return -1; /* ERROR CODE */
159 /* Allocate queue struct */
160 queue = rte_zmalloc_socket("Ark_rxqueue",
161 sizeof(struct ark_rx_queue),
165 ARK_PMD_LOG(ERR, "Failed to allocate memory in %s\n", __func__);
169 /* NOTE zmalloc is used, no need to 0 indexes, etc. */
170 queue->mb_pool = mb_pool;
171 queue->phys_qid = qidx;
172 queue->queue_index = queue_idx;
173 queue->queue_size = nb_desc;
174 queue->queue_mask = nb_desc - 1;
177 rte_zmalloc_socket("Ark_rx_queue mbuf",
178 nb_desc * sizeof(struct rte_mbuf *),
182 rte_zmalloc_socket("Ark_rx_queue paddr",
183 nb_desc * sizeof(rte_iova_t),
187 if (queue->reserve_q == 0 || queue->paddress_q == 0) {
189 "Failed to allocate queue memory in %s\n",
191 rte_free(queue->reserve_q);
192 rte_free(queue->paddress_q);
197 dev->data->rx_queues[queue_idx] = queue;
198 queue->udm = RTE_PTR_ADD(ark->udm.v, qidx * ARK_UDM_QOFFSET);
199 queue->mpu = RTE_PTR_ADD(ark->mpurx.v, qidx * ARK_MPU_QOFFSET);
201 /* populate mbuf reserve */
202 status = eth_ark_rx_seed_mbufs(queue);
204 if (queue->seed_index != nb_desc) {
205 ARK_PMD_LOG(ERR, "Failed to allocate %u mbufs for RX queue %d\n",
211 status = eth_ark_rx_hw_setup(dev, queue, qidx, queue_idx);
213 if (unlikely(status != 0)) {
214 struct rte_mbuf **mbuf;
216 ARK_PMD_LOG(ERR, "Failed to initialize RX queue %d %s\n",
219 /* Free the mbufs allocated */
220 for (i = 0, mbuf = queue->reserve_q;
221 i < queue->seed_index; ++i, mbuf++) {
222 rte_pktmbuf_free(*mbuf);
224 rte_free(queue->reserve_q);
225 rte_free(queue->paddress_q);
227 return -1; /* ERROR CODE */
233 /* ************************************************************************* */
235 eth_ark_recv_pkts_noop(void *rx_queue __rte_unused,
236 struct rte_mbuf **rx_pkts __rte_unused,
237 uint16_t nb_pkts __rte_unused)
242 /* ************************************************************************* */
244 eth_ark_recv_pkts(void *rx_queue,
245 struct rte_mbuf **rx_pkts,
248 struct ark_rx_queue *queue;
249 register uint32_t cons_index, prod_index;
251 struct rte_mbuf *mbuf;
252 struct ark_rx_meta *meta;
254 queue = (struct ark_rx_queue *)rx_queue;
255 if (unlikely(queue == 0))
257 if (unlikely(nb_pkts == 0))
259 prod_index = queue->prod_index;
260 cons_index = queue->cons_index;
263 while (prod_index != cons_index) {
264 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
266 rte_mbuf_prefetch_part1(mbuf);
267 rte_mbuf_prefetch_part2(mbuf);
269 /* META DATA embedded in headroom */
270 meta = RTE_PTR_ADD(mbuf->buf_addr, ARK_RX_META_OFFSET);
272 mbuf->port = meta->port;
273 mbuf->pkt_len = meta->pkt_len;
274 mbuf->data_len = meta->pkt_len;
275 mbuf->timestamp = meta->timestamp;
276 rte_pmd_ark_mbuf_rx_userdata_set(mbuf, meta->user_data);
278 if (ARK_DEBUG_CORE) { /* debug sanity checks */
279 if ((meta->pkt_len > (1024 * 16)) ||
280 (meta->pkt_len == 0)) {
281 ARK_PMD_LOG(DEBUG, "RX: Bad Meta Q: %u"
284 " seed_index %" PRIU32
292 ARK_PMD_LOG(DEBUG, " : UDM"
295 queue->udm->rt_cfg.prod_idx,
297 ark_mpu_dump(queue->mpu,
300 dump_mbuf_data(mbuf, 0, 256);
301 /* its FUBAR so fix it */
307 if (unlikely(meta->pkt_len > ARK_RX_MAX_NOCHAIN))
308 cons_index = eth_ark_rx_jumbo
309 (queue, meta, mbuf, cons_index + 1);
319 if (unlikely(nb != 0))
320 /* report next free to FPGA */
321 eth_ark_rx_update_cons_index(queue, cons_index);
326 /* ************************************************************************* */
328 eth_ark_rx_jumbo(struct ark_rx_queue *queue,
329 struct ark_rx_meta *meta,
330 struct rte_mbuf *mbuf0,
333 struct rte_mbuf *mbuf_prev;
334 struct rte_mbuf *mbuf;
340 /* first buf populated by called */
343 data_len = RTE_MIN(meta->pkt_len, RTE_MBUF_DEFAULT_DATAROOM);
344 remaining = meta->pkt_len - data_len;
345 mbuf0->data_len = data_len;
347 /* HW guarantees that the data does not exceed prod_index! */
348 while (remaining != 0) {
349 data_len = RTE_MIN(remaining,
350 RTE_MBUF_DEFAULT_DATAROOM +
351 RTE_PKTMBUF_HEADROOM);
353 remaining -= data_len;
356 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
357 mbuf_prev->next = mbuf;
359 mbuf->data_len = data_len;
365 mbuf0->nb_segs = segments;
369 /* Drain the internal queue allowing hw to clear out. */
371 eth_ark_rx_queue_drain(struct ark_rx_queue *queue)
373 register uint32_t cons_index;
374 struct rte_mbuf *mbuf;
376 cons_index = queue->cons_index;
378 /* NOT performance optimized, since this is a one-shot call */
379 while ((cons_index ^ queue->prod_index) & queue->queue_mask) {
380 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
381 rte_pktmbuf_free(mbuf);
383 eth_ark_rx_update_cons_index(queue, cons_index);
388 eth_ark_dev_rx_queue_count(struct rte_eth_dev *dev, uint16_t queue_id)
390 struct ark_rx_queue *queue;
392 queue = dev->data->rx_queues[queue_id];
393 return (queue->prod_index - queue->cons_index); /* mod arith */
396 /* ************************************************************************* */
398 eth_ark_rx_start_queue(struct rte_eth_dev *dev, uint16_t queue_id)
400 struct ark_rx_queue *queue;
402 queue = dev->data->rx_queues[queue_id];
406 dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
408 ark_mpu_set_producer(queue->mpu, queue->seed_index);
409 ark_mpu_start(queue->mpu);
411 ark_udm_queue_enable(queue->udm, 1);
416 /* ************************************************************************* */
418 /* Queue can be restarted. data remains
421 eth_ark_rx_stop_queue(struct rte_eth_dev *dev, uint16_t queue_id)
423 struct ark_rx_queue *queue;
425 queue = dev->data->rx_queues[queue_id];
429 ark_udm_queue_enable(queue->udm, 0);
431 dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
436 /* ************************************************************************* */
438 eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue)
440 uint32_t limit = queue->cons_index + queue->queue_size;
441 uint32_t seed_index = queue->seed_index;
444 uint32_t seed_m = queue->seed_index & queue->queue_mask;
446 uint32_t nb = limit - seed_index;
448 /* Handle wrap around -- remainder is filled on the next call */
449 if (unlikely(seed_m + nb > queue->queue_size))
450 nb = queue->queue_size - seed_m;
452 struct rte_mbuf **mbufs = &queue->reserve_q[seed_m];
453 int status = rte_pktmbuf_alloc_bulk(queue->mb_pool, mbufs, nb);
455 if (unlikely(status != 0)) {
456 /* Try to recover from lack of mbufs in pool */
457 status = eth_ark_rx_seed_recovery(queue, &nb, mbufs);
458 if (unlikely(status != 0)) {
463 if (ARK_DEBUG_CORE) { /* DEBUG */
464 while (count != nb) {
465 struct rte_mbuf *mbuf_init =
466 queue->reserve_q[seed_m + count];
468 memset(mbuf_init->buf_addr, -1, 512);
469 *((uint32_t *)mbuf_init->buf_addr) =
471 *(uint16_t *)RTE_PTR_ADD(mbuf_init->buf_addr, 4) =
477 queue->seed_index += nb;
479 /* Duff's device https://en.wikipedia.org/wiki/Duff's_device */
482 while (count != nb) {
483 queue->paddress_q[seed_m++] =
484 (*mbufs++)->buf_iova;
488 queue->paddress_q[seed_m++] =
489 (*mbufs++)->buf_iova;
493 queue->paddress_q[seed_m++] =
494 (*mbufs++)->buf_iova;
498 queue->paddress_q[seed_m++] =
499 (*mbufs++)->buf_iova;
503 } /* while (count != nb) */
510 eth_ark_rx_seed_recovery(struct ark_rx_queue *queue,
512 struct rte_mbuf **mbufs)
516 /* Ignore small allocation failures */
521 status = rte_pktmbuf_alloc_bulk(queue->mb_pool, mbufs, *pnb);
524 "ARK: Could not allocate %u mbufs from pool for RX queue %u;"
525 " %u free buffers remaining in queue\n",
526 *pnb, queue->queue_index,
527 queue->seed_index - queue->cons_index);
533 eth_ark_rx_dump_queue(struct rte_eth_dev *dev, uint16_t queue_id,
536 struct ark_rx_queue *queue;
538 queue = dev->data->rx_queues[queue_id];
540 ark_ethdev_rx_dump(msg, queue);
543 /* ************************************************************************* */
544 /* Call on device closed no user API, queue is stopped */
546 eth_ark_dev_rx_queue_release(void *vqueue)
548 struct ark_rx_queue *queue;
551 queue = (struct ark_rx_queue *)vqueue;
555 ark_udm_queue_enable(queue->udm, 0);
556 /* Stop the MPU since pointer are going away */
557 ark_mpu_stop(queue->mpu);
559 /* Need to clear out mbufs here, dropping packets along the way */
560 eth_ark_rx_queue_drain(queue);
562 for (i = 0; i < queue->queue_size; ++i)
563 rte_pktmbuf_free(queue->reserve_q[i]);
565 rte_free(queue->reserve_q);
566 rte_free(queue->paddress_q);
571 eth_rx_queue_stats_get(void *vqueue, struct rte_eth_stats *stats)
573 struct ark_rx_queue *queue;
574 struct ark_udm_t *udm;
581 uint64_t ibytes = ark_udm_bytes(udm);
582 uint64_t ipackets = ark_udm_packets(udm);
583 uint64_t idropped = ark_udm_dropped(queue->udm);
585 stats->q_ipackets[queue->queue_index] = ipackets;
586 stats->q_ibytes[queue->queue_index] = ibytes;
587 stats->q_errors[queue->queue_index] = idropped;
588 stats->ipackets += ipackets;
589 stats->ibytes += ibytes;
590 stats->imissed += idropped;
594 eth_rx_queue_stats_reset(void *vqueue)
596 struct ark_rx_queue *queue;
602 ark_mpu_reset_stats(queue->mpu);
603 ark_udm_queue_stats_reset(queue->udm);
607 eth_ark_udm_force_close(struct rte_eth_dev *dev)
609 struct ark_adapter *ark = dev->data->dev_private;
610 struct ark_rx_queue *queue;
614 if (!ark_udm_is_flushed(ark->udm.v)) {
615 /* restart the MPUs */
616 ARK_PMD_LOG(NOTICE, "UDM not flushed -- forcing flush\n");
617 for (i = 0; i < dev->data->nb_rx_queues; i++) {
618 queue = (struct ark_rx_queue *)dev->data->rx_queues[i];
622 ark_mpu_start(queue->mpu);
623 /* Add some buffers */
624 index = 100000 + queue->seed_index;
625 ark_mpu_set_producer(queue->mpu, index);
627 /* Wait to allow data to pass */
630 ARK_PMD_LOG(DEBUG, "UDM forced flush attempt, stopped = %d\n",
631 ark_udm_is_flushed(ark->udm.v));
633 ark_udm_reset(ark->udm.v);
637 ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue)
641 ARK_PMD_LOG(DEBUG, "RX QUEUE %d -- %s", queue->phys_qid, name);
642 ARK_PMD_LOG(DEBUG, ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32 "\n",
643 "queue_size", queue->queue_size,
644 "seed_index", queue->seed_index,
645 "prod_index", queue->prod_index,
646 "cons_index", queue->cons_index);
648 ark_mpu_dump(queue->mpu, name, queue->phys_qid);
649 ark_mpu_dump_setup(queue->mpu, queue->phys_qid);
650 ark_udm_dump(queue->udm, name);
651 ark_udm_dump_setup(queue->udm, queue->phys_qid);
654 /* Only used in debug.
655 * This function is a raw memory dump of a portion of an mbuf's memory
656 * region. The usual function, rte_pktmbuf_dump() only shows data
657 * with respect to the data_off field. This function show data
658 * anywhere in the mbuf's buffer. This is useful for examining
659 * data in the headroom or tailroom portion of an mbuf.
662 dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi)
666 ARK_PMD_LOG(DEBUG, " MBUF: %p len %d, off: %d\n",
667 mbuf, mbuf->pkt_len, mbuf->data_off);
668 for (i = lo; i < hi; i += 16) {
669 uint8_t *dp = RTE_PTR_ADD(mbuf->buf_addr, i);
671 ARK_PMD_LOG(DEBUG, " %6d: ", i);
672 for (j = 0; j < 16; j++)
673 ARK_PMD_LOG(DEBUG, " %02x", dp[j]);
675 ARK_PMD_LOG(DEBUG, "\n");