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_MAX_NOCHAIN (RTE_MBUF_DEFAULT_DATAROOM)
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;
47 uint32_t seed_index; /* step 1 set with empty mbuf */
48 uint32_t cons_index; /* step 3 consumed by driver */
50 /* The queue Id is used to identify the HW Q */
53 /* The queue Index is used within the dpdk device structures */
58 /* next cache line - fields written by device */
59 RTE_MARKER cacheline1 __rte_cache_min_aligned;
61 volatile uint32_t prod_index; /* step 2 filled by FPGA */
62 } __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;
171 queue->rx_user_meta_hook = ark->user_ext.rx_user_meta_hook;
172 queue->ext_user_data = ark->user_data[dev->data->port_id];
175 rte_zmalloc_socket("Ark_rx_queue mbuf",
176 nb_desc * sizeof(struct rte_mbuf *),
180 rte_zmalloc_socket("Ark_rx_queue paddr",
181 nb_desc * sizeof(rte_iova_t),
185 if (queue->reserve_q == 0 || queue->paddress_q == 0) {
187 "Failed to allocate queue memory in %s\n",
189 rte_free(queue->reserve_q);
190 rte_free(queue->paddress_q);
195 dev->data->rx_queues[queue_idx] = queue;
196 queue->udm = RTE_PTR_ADD(ark->udm.v, qidx * ARK_UDM_QOFFSET);
197 queue->mpu = RTE_PTR_ADD(ark->mpurx.v, qidx * ARK_MPU_QOFFSET);
199 /* populate mbuf reserve */
200 status = eth_ark_rx_seed_mbufs(queue);
202 if (queue->seed_index != nb_desc) {
203 ARK_PMD_LOG(ERR, "Failed to allocate %u mbufs for RX queue %d\n",
209 status = eth_ark_rx_hw_setup(dev, queue, qidx, queue_idx);
211 if (unlikely(status != 0)) {
212 struct rte_mbuf **mbuf;
214 ARK_PMD_LOG(ERR, "Failed to initialize RX queue %d %s\n",
217 /* Free the mbufs allocated */
218 for (i = 0, mbuf = queue->reserve_q;
219 i < queue->seed_index; ++i, mbuf++) {
220 rte_pktmbuf_free(*mbuf);
222 rte_free(queue->reserve_q);
223 rte_free(queue->paddress_q);
225 return -1; /* ERROR CODE */
231 /* ************************************************************************* */
233 eth_ark_recv_pkts(void *rx_queue,
234 struct rte_mbuf **rx_pkts,
237 struct ark_rx_queue *queue;
238 register uint32_t cons_index, prod_index;
241 struct rte_mbuf *mbuf;
242 struct rte_mbuf **pmbuf;
243 struct ark_rx_meta *meta;
244 rx_user_meta_hook_fn rx_user_meta_hook;
246 queue = (struct ark_rx_queue *)rx_queue;
247 if (unlikely(queue == 0))
249 if (unlikely(nb_pkts == 0))
251 prod_index = queue->prod_index;
252 cons_index = queue->cons_index;
253 if (prod_index == cons_index)
257 while (prod_index != cons_index) {
258 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
260 rte_mbuf_prefetch_part1(mbuf);
261 rte_mbuf_prefetch_part2(mbuf);
263 /* META DATA embedded in headroom */
264 meta = RTE_PTR_ADD(mbuf->buf_addr, ARK_RX_META_OFFSET);
266 mbuf->pkt_len = meta->pkt_len;
267 mbuf->data_len = meta->pkt_len;
269 if (ARK_DEBUG_CORE) { /* debug sanity checks */
270 if ((meta->pkt_len > (1024 * 16)) ||
271 (meta->pkt_len == 0)) {
272 ARK_PMD_LOG(DEBUG, "RX: Bad Meta Q: %u"
275 " seed_index %" PRIU32
283 ARK_PMD_LOG(DEBUG, " : UDM"
286 queue->udm->rt_cfg.prod_idx,
288 ark_mpu_dump(queue->mpu,
291 dump_mbuf_data(mbuf, 0, 256);
292 /* its FUBAR so fix it */
298 if (unlikely(meta->pkt_len > ARK_RX_MAX_NOCHAIN))
299 cons_index = eth_ark_rx_jumbo
300 (queue, meta, mbuf, cons_index + 1);
310 rx_user_meta_hook = queue->rx_user_meta_hook;
311 for (pmbuf = rx_pkts, i = 0; rx_user_meta_hook && i < nb; i++) {
313 meta = RTE_PTR_ADD(mbuf->buf_addr, ARK_RX_META_OFFSET);
314 rx_user_meta_hook(mbuf, meta->user_meta, queue->ext_user_data);
317 eth_ark_rx_update_cons_index(queue, cons_index);
322 /* ************************************************************************* */
324 eth_ark_rx_jumbo(struct ark_rx_queue *queue,
325 struct ark_rx_meta *meta,
326 struct rte_mbuf *mbuf0,
329 struct rte_mbuf *mbuf_prev;
330 struct rte_mbuf *mbuf;
336 /* first buf populated by called */
339 data_len = RTE_MIN(meta->pkt_len, RTE_MBUF_DEFAULT_DATAROOM);
340 remaining = meta->pkt_len - data_len;
341 mbuf0->data_len = data_len;
343 /* HW guarantees that the data does not exceed prod_index! */
344 while (remaining != 0) {
345 data_len = RTE_MIN(remaining,
346 RTE_MBUF_DEFAULT_DATAROOM);
348 remaining -= data_len;
351 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
352 mbuf_prev->next = mbuf;
354 mbuf->data_len = data_len;
359 mbuf0->nb_segs = segments;
363 /* Drain the internal queue allowing hw to clear out. */
365 eth_ark_rx_queue_drain(struct ark_rx_queue *queue)
367 register uint32_t cons_index;
368 struct rte_mbuf *mbuf;
370 cons_index = queue->cons_index;
372 /* NOT performance optimized, since this is a one-shot call */
373 while ((cons_index ^ queue->prod_index) & queue->queue_mask) {
374 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
375 rte_pktmbuf_free(mbuf);
377 eth_ark_rx_update_cons_index(queue, cons_index);
382 eth_ark_dev_rx_queue_count(void *rx_queue)
384 struct ark_rx_queue *queue;
387 return (queue->prod_index - queue->cons_index); /* mod arith */
390 /* ************************************************************************* */
392 eth_ark_rx_start_queue(struct rte_eth_dev *dev, uint16_t queue_id)
394 struct ark_rx_queue *queue;
396 queue = dev->data->rx_queues[queue_id];
400 dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
402 ark_mpu_set_producer(queue->mpu, queue->seed_index);
403 ark_mpu_start(queue->mpu);
405 ark_udm_queue_enable(queue->udm, 1);
410 /* ************************************************************************* */
412 /* Queue can be restarted. data remains
415 eth_ark_rx_stop_queue(struct rte_eth_dev *dev, uint16_t queue_id)
417 struct ark_rx_queue *queue;
419 queue = dev->data->rx_queues[queue_id];
423 ark_udm_queue_enable(queue->udm, 0);
425 dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
430 /* ************************************************************************* */
432 eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue)
434 uint32_t limit = queue->cons_index + queue->queue_size;
435 uint32_t seed_index = queue->seed_index;
438 uint32_t seed_m = queue->seed_index & queue->queue_mask;
440 uint32_t nb = limit - seed_index;
442 /* Handle wrap around -- remainder is filled on the next call */
443 if (unlikely(seed_m + nb > queue->queue_size))
444 nb = queue->queue_size - seed_m;
446 struct rte_mbuf **mbufs = &queue->reserve_q[seed_m];
447 int status = rte_pktmbuf_alloc_bulk(queue->mb_pool, mbufs, nb);
449 if (unlikely(status != 0)) {
451 "Could not allocate %u mbufs from pool"
453 " %u free buffers remaining in queue\n",
454 nb, queue->queue_index,
455 queue->seed_index - queue->cons_index);
459 if (ARK_DEBUG_CORE) { /* DEBUG */
460 while (count != nb) {
461 struct rte_mbuf *mbuf_init =
462 queue->reserve_q[seed_m + count];
464 memset(mbuf_init->buf_addr, -1, 512);
465 *((uint32_t *)mbuf_init->buf_addr) =
467 *(uint16_t *)RTE_PTR_ADD(mbuf_init->buf_addr, 4) =
473 queue->seed_index += nb;
475 /* Duff's device https://en.wikipedia.org/wiki/Duff's_device */
478 while (count != nb) {
479 queue->paddress_q[seed_m++] =
480 (*mbufs++)->buf_iova;
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 } /* while (count != nb) */
506 eth_ark_rx_dump_queue(struct rte_eth_dev *dev, uint16_t queue_id,
509 struct ark_rx_queue *queue;
511 queue = dev->data->rx_queues[queue_id];
513 ark_ethdev_rx_dump(msg, queue);
516 /* ************************************************************************* */
517 /* Call on device closed no user API, queue is stopped */
519 eth_ark_dev_rx_queue_release(void *vqueue)
521 struct ark_rx_queue *queue;
524 queue = (struct ark_rx_queue *)vqueue;
528 ark_udm_queue_enable(queue->udm, 0);
529 /* Stop the MPU since pointer are going away */
530 ark_mpu_stop(queue->mpu);
532 /* Need to clear out mbufs here, dropping packets along the way */
533 eth_ark_rx_queue_drain(queue);
535 for (i = 0; i < queue->queue_size; ++i)
536 rte_pktmbuf_free(queue->reserve_q[i]);
538 rte_free(queue->reserve_q);
539 rte_free(queue->paddress_q);
544 eth_rx_queue_stats_get(void *vqueue, struct rte_eth_stats *stats)
546 struct ark_rx_queue *queue;
547 struct ark_udm_t *udm;
554 uint64_t ibytes = ark_udm_bytes(udm);
555 uint64_t ipackets = ark_udm_packets(udm);
556 uint64_t idropped = ark_udm_dropped(queue->udm);
558 stats->q_ipackets[queue->queue_index] = ipackets;
559 stats->q_ibytes[queue->queue_index] = ibytes;
560 stats->q_errors[queue->queue_index] = idropped;
561 stats->ipackets += ipackets;
562 stats->ibytes += ibytes;
563 stats->imissed += idropped;
567 eth_rx_queue_stats_reset(void *vqueue)
569 struct ark_rx_queue *queue;
575 ark_mpu_reset_stats(queue->mpu);
576 ark_udm_queue_stats_reset(queue->udm);
580 eth_ark_udm_force_close(struct rte_eth_dev *dev)
582 struct ark_adapter *ark = dev->data->dev_private;
583 struct ark_rx_queue *queue;
587 if (!ark_udm_is_flushed(ark->udm.v)) {
588 /* restart the MPUs */
589 ARK_PMD_LOG(NOTICE, "UDM not flushed -- forcing flush\n");
590 for (i = 0; i < dev->data->nb_rx_queues; i++) {
591 queue = (struct ark_rx_queue *)dev->data->rx_queues[i];
595 ark_mpu_start(queue->mpu);
596 /* Add some buffers */
597 index = 100000 + queue->seed_index;
598 ark_mpu_set_producer(queue->mpu, index);
600 /* Wait to allow data to pass */
603 ARK_PMD_LOG(DEBUG, "UDM forced flush attempt, stopped = %d\n",
604 ark_udm_is_flushed(ark->udm.v));
606 ark_udm_reset(ark->udm.v);
610 ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue)
614 ARK_PMD_LOG(DEBUG, "RX QUEUE %d -- %s", queue->phys_qid, name);
615 ARK_PMD_LOG(DEBUG, ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32 "\n",
616 "queue_size", queue->queue_size,
617 "seed_index", queue->seed_index,
618 "prod_index", queue->prod_index,
619 "cons_index", queue->cons_index);
621 ark_mpu_dump(queue->mpu, name, queue->phys_qid);
622 ark_mpu_dump_setup(queue->mpu, queue->phys_qid);
623 ark_udm_dump(queue->udm, name);
624 ark_udm_dump_setup(queue->udm, queue->phys_qid);
627 /* Only used in debug.
628 * This function is a raw memory dump of a portion of an mbuf's memory
629 * region. The usual function, rte_pktmbuf_dump() only shows data
630 * with respect to the data_off field. This function show data
631 * anywhere in the mbuf's buffer. This is useful for examining
632 * data in the headroom or tailroom portion of an mbuf.
635 dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi)
639 ARK_PMD_LOG(DEBUG, " MBUF: %p len %d, off: %d\n",
640 mbuf, mbuf->pkt_len, mbuf->data_off);
641 for (i = lo; i < hi; i += 16) {
642 uint8_t *dp = RTE_PTR_ADD(mbuf->buf_addr, i);
644 ARK_PMD_LOG(DEBUG, " %6d: ", i);
645 for (j = 0; j < 16; j++)
646 ARK_PMD_LOG(DEBUG, " %02x", dp[j]);
648 ARK_PMD_LOG(DEBUG, "\n");