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);
30 /* ************************************************************************* */
32 /* array of mbufs to populate */
33 struct rte_mbuf **reserve_q;
34 /* array of physical addresses of the mbuf data pointer */
35 /* This point is a virtual address */
36 rte_iova_t *paddress_q;
37 struct rte_mempool *mb_pool;
39 struct ark_udm_t *udm;
40 struct ark_mpu_t *mpu;
45 uint32_t seed_index; /* step 1 set with empty mbuf */
46 uint32_t cons_index; /* step 3 consumed by driver */
48 /* The queue Id is used to identify the HW Q */
51 /* The queue Index is used within the dpdk device structures */
56 /* separate cache line */
57 /* second cache line - fields only used in slow path */
58 RTE_MARKER cacheline1 __rte_cache_min_aligned;
60 volatile uint32_t prod_index; /* step 2 filled by FPGA */
61 } __rte_cache_aligned;
64 /* ************************************************************************* */
66 eth_ark_rx_hw_setup(struct rte_eth_dev *dev,
67 struct ark_rx_queue *queue,
68 uint16_t rx_queue_id __rte_unused, uint16_t rx_queue_idx)
70 rte_iova_t queue_base;
71 rte_iova_t phys_addr_q_base;
72 rte_iova_t phys_addr_prod_index;
74 queue_base = rte_malloc_virt2iova(queue);
75 phys_addr_prod_index = queue_base +
76 offsetof(struct ark_rx_queue, prod_index);
78 phys_addr_q_base = rte_malloc_virt2iova(queue->paddress_q);
81 if (ark_mpu_verify(queue->mpu, sizeof(rte_iova_t))) {
82 ARK_PMD_LOG(ERR, "Illegal configuration rx queue\n");
86 /* Stop and Reset and configure MPU */
87 ark_mpu_configure(queue->mpu, phys_addr_q_base, queue->queue_size, 0);
89 ark_udm_write_addr(queue->udm, phys_addr_prod_index);
91 /* advance the valid pointer, but don't start until the queue starts */
92 ark_mpu_reset_stats(queue->mpu);
94 /* The seed is the producer index for the HW */
95 ark_mpu_set_producer(queue->mpu, queue->seed_index);
96 dev->data->rx_queue_state[rx_queue_idx] = RTE_ETH_QUEUE_STATE_STOPPED;
102 eth_ark_rx_update_cons_index(struct ark_rx_queue *queue, uint32_t cons_index)
104 queue->cons_index = cons_index;
105 if ((cons_index + queue->queue_size - queue->seed_index) >= 64U) {
106 eth_ark_rx_seed_mbufs(queue);
107 ark_mpu_set_producer(queue->mpu, queue->seed_index);
111 /* ************************************************************************* */
113 eth_ark_dev_rx_queue_setup(struct rte_eth_dev *dev,
116 unsigned int socket_id,
117 const struct rte_eth_rxconf *rx_conf,
118 struct rte_mempool *mb_pool)
120 static int warning1; /* = 0 */
121 struct ark_adapter *ark = dev->data->dev_private;
123 struct ark_rx_queue *queue;
127 int qidx = queue_idx;
129 /* We may already be setup, free memory prior to re-allocation */
130 if (dev->data->rx_queues[queue_idx] != NULL) {
131 eth_ark_dev_rx_queue_release(dev->data->rx_queues[queue_idx]);
132 dev->data->rx_queues[queue_idx] = NULL;
135 if (rx_conf != NULL && warning1 == 0) {
138 "Arkville ignores rte_eth_rxconf argument.\n");
141 if (RTE_PKTMBUF_HEADROOM < ARK_RX_META_SIZE) {
143 "Error: DPDK Arkville requires head room > %d bytes (%s)\n",
144 ARK_RX_META_SIZE, __func__);
145 return -1; /* ERROR CODE */
148 if (!rte_is_power_of_2(nb_desc)) {
150 "DPDK Arkville configuration queue size must be power of two %u (%s)\n",
152 return -1; /* ERROR CODE */
155 /* Allocate queue struct */
156 queue = rte_zmalloc_socket("Ark_rxqueue",
157 sizeof(struct ark_rx_queue),
161 ARK_PMD_LOG(ERR, "Failed to allocate memory in %s\n", __func__);
165 /* NOTE zmalloc is used, no need to 0 indexes, etc. */
166 queue->mb_pool = mb_pool;
167 queue->phys_qid = qidx;
168 queue->queue_index = queue_idx;
169 queue->queue_size = nb_desc;
170 queue->queue_mask = nb_desc - 1;
173 rte_zmalloc_socket("Ark_rx_queue mbuf",
174 nb_desc * sizeof(struct rte_mbuf *),
178 rte_zmalloc_socket("Ark_rx_queue paddr",
179 nb_desc * sizeof(rte_iova_t),
183 if (queue->reserve_q == 0 || queue->paddress_q == 0) {
185 "Failed to allocate queue memory in %s\n",
187 rte_free(queue->reserve_q);
188 rte_free(queue->paddress_q);
193 dev->data->rx_queues[queue_idx] = queue;
194 queue->udm = RTE_PTR_ADD(ark->udm.v, qidx * ARK_UDM_QOFFSET);
195 queue->mpu = RTE_PTR_ADD(ark->mpurx.v, qidx * ARK_MPU_QOFFSET);
197 /* populate mbuf reserve */
198 status = eth_ark_rx_seed_mbufs(queue);
200 if (queue->seed_index != nb_desc) {
201 ARK_PMD_LOG(ERR, "Failed to allocate %u mbufs for RX queue %d\n",
207 status = eth_ark_rx_hw_setup(dev, queue, qidx, queue_idx);
209 if (unlikely(status != 0)) {
210 struct rte_mbuf **mbuf;
212 ARK_PMD_LOG(ERR, "Failed to initialize RX queue %d %s\n",
215 /* Free the mbufs allocated */
216 for (i = 0, mbuf = queue->reserve_q;
217 i < queue->seed_index; ++i, mbuf++) {
218 rte_pktmbuf_free(*mbuf);
220 rte_free(queue->reserve_q);
221 rte_free(queue->paddress_q);
223 return -1; /* ERROR CODE */
229 /* ************************************************************************* */
231 eth_ark_recv_pkts_noop(void *rx_queue __rte_unused,
232 struct rte_mbuf **rx_pkts __rte_unused,
233 uint16_t nb_pkts __rte_unused)
238 /* ************************************************************************* */
240 eth_ark_recv_pkts(void *rx_queue,
241 struct rte_mbuf **rx_pkts,
244 struct ark_rx_queue *queue;
245 register uint32_t cons_index, prod_index;
247 struct rte_mbuf *mbuf;
248 struct ark_rx_meta *meta;
250 queue = (struct ark_rx_queue *)rx_queue;
251 if (unlikely(queue == 0))
253 if (unlikely(nb_pkts == 0))
255 prod_index = queue->prod_index;
256 cons_index = queue->cons_index;
259 while (prod_index != cons_index) {
260 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
262 rte_mbuf_prefetch_part1(mbuf);
263 rte_mbuf_prefetch_part2(mbuf);
265 /* META DATA embedded in headroom */
266 meta = RTE_PTR_ADD(mbuf->buf_addr, ARK_RX_META_OFFSET);
268 mbuf->port = meta->port;
269 mbuf->pkt_len = meta->pkt_len;
270 mbuf->data_len = meta->pkt_len;
271 /* set timestamp if enabled at least on one device */
272 if (ark_timestamp_rx_dynflag > 0) {
273 *RTE_MBUF_DYNFIELD(mbuf, ark_timestamp_dynfield_offset,
274 rte_mbuf_timestamp_t *) = meta->timestamp;
275 mbuf->ol_flags |= ark_timestamp_rx_dynflag;
277 rte_pmd_ark_mbuf_rx_userdata_set(mbuf, meta->user_data);
279 if (ARK_DEBUG_CORE) { /* debug sanity checks */
280 if ((meta->pkt_len > (1024 * 16)) ||
281 (meta->pkt_len == 0)) {
282 ARK_PMD_LOG(DEBUG, "RX: Bad Meta Q: %u"
285 " seed_index %" PRIU32
293 ARK_PMD_LOG(DEBUG, " : UDM"
296 queue->udm->rt_cfg.prod_idx,
298 ark_mpu_dump(queue->mpu,
301 dump_mbuf_data(mbuf, 0, 256);
302 /* its FUBAR so fix it */
308 if (unlikely(meta->pkt_len > ARK_RX_MAX_NOCHAIN))
309 cons_index = eth_ark_rx_jumbo
310 (queue, meta, mbuf, cons_index + 1);
320 eth_ark_rx_update_cons_index(queue, cons_index);
325 /* ************************************************************************* */
327 eth_ark_rx_jumbo(struct ark_rx_queue *queue,
328 struct ark_rx_meta *meta,
329 struct rte_mbuf *mbuf0,
332 struct rte_mbuf *mbuf_prev;
333 struct rte_mbuf *mbuf;
339 /* first buf populated by called */
342 data_len = RTE_MIN(meta->pkt_len, RTE_MBUF_DEFAULT_DATAROOM);
343 remaining = meta->pkt_len - data_len;
344 mbuf0->data_len = data_len;
346 /* HW guarantees that the data does not exceed prod_index! */
347 while (remaining != 0) {
348 data_len = RTE_MIN(remaining,
349 RTE_MBUF_DEFAULT_DATAROOM +
350 RTE_PKTMBUF_HEADROOM);
352 remaining -= data_len;
355 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
356 mbuf_prev->next = mbuf;
358 mbuf->data_len = data_len;
364 mbuf0->nb_segs = segments;
368 /* Drain the internal queue allowing hw to clear out. */
370 eth_ark_rx_queue_drain(struct ark_rx_queue *queue)
372 register uint32_t cons_index;
373 struct rte_mbuf *mbuf;
375 cons_index = queue->cons_index;
377 /* NOT performance optimized, since this is a one-shot call */
378 while ((cons_index ^ queue->prod_index) & queue->queue_mask) {
379 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
380 rte_pktmbuf_free(mbuf);
382 eth_ark_rx_update_cons_index(queue, cons_index);
387 eth_ark_dev_rx_queue_count(struct rte_eth_dev *dev, uint16_t queue_id)
389 struct ark_rx_queue *queue;
391 queue = dev->data->rx_queues[queue_id];
392 return (queue->prod_index - queue->cons_index); /* mod arith */
395 /* ************************************************************************* */
397 eth_ark_rx_start_queue(struct rte_eth_dev *dev, uint16_t queue_id)
399 struct ark_rx_queue *queue;
401 queue = dev->data->rx_queues[queue_id];
405 dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
407 ark_mpu_set_producer(queue->mpu, queue->seed_index);
408 ark_mpu_start(queue->mpu);
410 ark_udm_queue_enable(queue->udm, 1);
415 /* ************************************************************************* */
417 /* Queue can be restarted. data remains
420 eth_ark_rx_stop_queue(struct rte_eth_dev *dev, uint16_t queue_id)
422 struct ark_rx_queue *queue;
424 queue = dev->data->rx_queues[queue_id];
428 ark_udm_queue_enable(queue->udm, 0);
430 dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
435 /* ************************************************************************* */
437 eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue)
439 uint32_t limit = queue->cons_index + queue->queue_size;
440 uint32_t seed_index = queue->seed_index;
443 uint32_t seed_m = queue->seed_index & queue->queue_mask;
445 uint32_t nb = limit - seed_index;
447 /* Handle wrap around -- remainder is filled on the next call */
448 if (unlikely(seed_m + nb > queue->queue_size))
449 nb = queue->queue_size - seed_m;
451 struct rte_mbuf **mbufs = &queue->reserve_q[seed_m];
452 int status = rte_pktmbuf_alloc_bulk(queue->mb_pool, mbufs, nb);
454 if (unlikely(status != 0)) {
456 "Could not allocate %u mbufs from pool"
458 " %u free buffers remaining in queue\n",
459 nb, queue->queue_index,
460 queue->seed_index - queue->cons_index);
464 if (ARK_DEBUG_CORE) { /* DEBUG */
465 while (count != nb) {
466 struct rte_mbuf *mbuf_init =
467 queue->reserve_q[seed_m + count];
469 memset(mbuf_init->buf_addr, -1, 512);
470 *((uint32_t *)mbuf_init->buf_addr) =
472 *(uint16_t *)RTE_PTR_ADD(mbuf_init->buf_addr, 4) =
478 queue->seed_index += nb;
480 /* Duff's device https://en.wikipedia.org/wiki/Duff's_device */
483 while (count != nb) {
484 queue->paddress_q[seed_m++] =
485 (*mbufs++)->buf_iova;
489 queue->paddress_q[seed_m++] =
490 (*mbufs++)->buf_iova;
494 queue->paddress_q[seed_m++] =
495 (*mbufs++)->buf_iova;
499 queue->paddress_q[seed_m++] =
500 (*mbufs++)->buf_iova;
504 } /* while (count != nb) */
511 eth_ark_rx_dump_queue(struct rte_eth_dev *dev, uint16_t queue_id,
514 struct ark_rx_queue *queue;
516 queue = dev->data->rx_queues[queue_id];
518 ark_ethdev_rx_dump(msg, queue);
521 /* ************************************************************************* */
522 /* Call on device closed no user API, queue is stopped */
524 eth_ark_dev_rx_queue_release(void *vqueue)
526 struct ark_rx_queue *queue;
529 queue = (struct ark_rx_queue *)vqueue;
533 ark_udm_queue_enable(queue->udm, 0);
534 /* Stop the MPU since pointer are going away */
535 ark_mpu_stop(queue->mpu);
537 /* Need to clear out mbufs here, dropping packets along the way */
538 eth_ark_rx_queue_drain(queue);
540 for (i = 0; i < queue->queue_size; ++i)
541 rte_pktmbuf_free(queue->reserve_q[i]);
543 rte_free(queue->reserve_q);
544 rte_free(queue->paddress_q);
549 eth_rx_queue_stats_get(void *vqueue, struct rte_eth_stats *stats)
551 struct ark_rx_queue *queue;
552 struct ark_udm_t *udm;
559 uint64_t ibytes = ark_udm_bytes(udm);
560 uint64_t ipackets = ark_udm_packets(udm);
561 uint64_t idropped = ark_udm_dropped(queue->udm);
563 stats->q_ipackets[queue->queue_index] = ipackets;
564 stats->q_ibytes[queue->queue_index] = ibytes;
565 stats->q_errors[queue->queue_index] = idropped;
566 stats->ipackets += ipackets;
567 stats->ibytes += ibytes;
568 stats->imissed += idropped;
572 eth_rx_queue_stats_reset(void *vqueue)
574 struct ark_rx_queue *queue;
580 ark_mpu_reset_stats(queue->mpu);
581 ark_udm_queue_stats_reset(queue->udm);
585 eth_ark_udm_force_close(struct rte_eth_dev *dev)
587 struct ark_adapter *ark = dev->data->dev_private;
588 struct ark_rx_queue *queue;
592 if (!ark_udm_is_flushed(ark->udm.v)) {
593 /* restart the MPUs */
594 ARK_PMD_LOG(NOTICE, "UDM not flushed -- forcing flush\n");
595 for (i = 0; i < dev->data->nb_rx_queues; i++) {
596 queue = (struct ark_rx_queue *)dev->data->rx_queues[i];
600 ark_mpu_start(queue->mpu);
601 /* Add some buffers */
602 index = 100000 + queue->seed_index;
603 ark_mpu_set_producer(queue->mpu, index);
605 /* Wait to allow data to pass */
608 ARK_PMD_LOG(DEBUG, "UDM forced flush attempt, stopped = %d\n",
609 ark_udm_is_flushed(ark->udm.v));
611 ark_udm_reset(ark->udm.v);
615 ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue)
619 ARK_PMD_LOG(DEBUG, "RX QUEUE %d -- %s", queue->phys_qid, name);
620 ARK_PMD_LOG(DEBUG, ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32 "\n",
621 "queue_size", queue->queue_size,
622 "seed_index", queue->seed_index,
623 "prod_index", queue->prod_index,
624 "cons_index", queue->cons_index);
626 ark_mpu_dump(queue->mpu, name, queue->phys_qid);
627 ark_mpu_dump_setup(queue->mpu, queue->phys_qid);
628 ark_udm_dump(queue->udm, name);
629 ark_udm_dump_setup(queue->udm, queue->phys_qid);
632 /* Only used in debug.
633 * This function is a raw memory dump of a portion of an mbuf's memory
634 * region. The usual function, rte_pktmbuf_dump() only shows data
635 * with respect to the data_off field. This function show data
636 * anywhere in the mbuf's buffer. This is useful for examining
637 * data in the headroom or tailroom portion of an mbuf.
640 dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi)
644 ARK_PMD_LOG(DEBUG, " MBUF: %p len %d, off: %d\n",
645 mbuf, mbuf->pkt_len, mbuf->data_off);
646 for (i = lo; i < hi; i += 16) {
647 uint8_t *dp = RTE_PTR_ADD(mbuf->buf_addr, i);
649 ARK_PMD_LOG(DEBUG, " %6d: ", i);
650 for (j = 0; j < 16; j++)
651 ARK_PMD_LOG(DEBUG, " %02x", dp[j]);
653 ARK_PMD_LOG(DEBUG, "\n");