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;
44 uint32_t seed_index; /* step 1 set with empty mbuf */
45 uint32_t cons_index; /* step 3 consumed by driver */
47 /* The queue Id is used to identify the HW Q */
50 /* The queue Index is used within the dpdk device structures */
55 /* separate cache line */
56 /* second cache line - fields only used in slow path */
57 MARKER cacheline1 __rte_cache_min_aligned;
59 volatile uint32_t prod_index; /* step 2 filled by FPGA */
60 } __rte_cache_aligned;
63 /* ************************************************************************* */
65 eth_ark_rx_hw_setup(struct rte_eth_dev *dev,
66 struct ark_rx_queue *queue,
67 uint16_t rx_queue_id __rte_unused, uint16_t rx_queue_idx)
69 rte_iova_t queue_base;
70 rte_iova_t phys_addr_q_base;
71 rte_iova_t phys_addr_prod_index;
73 queue_base = rte_malloc_virt2iova(queue);
74 phys_addr_prod_index = queue_base +
75 offsetof(struct ark_rx_queue, prod_index);
77 phys_addr_q_base = rte_malloc_virt2iova(queue->paddress_q);
80 if (ark_mpu_verify(queue->mpu, sizeof(rte_iova_t))) {
81 PMD_DRV_LOG(ERR, "Illegal configuration rx queue\n");
85 /* Stop and Reset and configure MPU */
86 ark_mpu_configure(queue->mpu, phys_addr_q_base, queue->queue_size, 0);
88 ark_udm_write_addr(queue->udm, phys_addr_prod_index);
90 /* advance the valid pointer, but don't start until the queue starts */
91 ark_mpu_reset_stats(queue->mpu);
93 /* The seed is the producer index for the HW */
94 ark_mpu_set_producer(queue->mpu, queue->seed_index);
95 dev->data->rx_queue_state[rx_queue_idx] = RTE_ETH_QUEUE_STATE_STOPPED;
101 eth_ark_rx_update_cons_index(struct ark_rx_queue *queue, uint32_t cons_index)
103 queue->cons_index = cons_index;
104 eth_ark_rx_seed_mbufs(queue);
105 ark_mpu_set_producer(queue->mpu, queue->seed_index);
108 /* ************************************************************************* */
110 eth_ark_dev_rx_queue_setup(struct rte_eth_dev *dev,
113 unsigned int socket_id,
114 const struct rte_eth_rxconf *rx_conf,
115 struct rte_mempool *mb_pool)
117 static int warning1; /* = 0 */
118 struct ark_adapter *ark = (struct ark_adapter *)dev->data->dev_private;
120 struct ark_rx_queue *queue;
124 /* Future works: divide the Q's evenly with multi-ports */
125 int port = dev->data->port_id;
126 int qidx = port + queue_idx;
128 /* We may already be setup, free memory prior to re-allocation */
129 if (dev->data->rx_queues[queue_idx] != NULL) {
130 eth_ark_dev_rx_queue_release(dev->data->rx_queues[queue_idx]);
131 dev->data->rx_queues[queue_idx] = NULL;
134 if (rx_conf != NULL && warning1 == 0) {
137 "Arkville ignores rte_eth_rxconf argument.\n");
140 if (RTE_PKTMBUF_HEADROOM < ARK_RX_META_SIZE) {
142 "Error: DPDK Arkville requires head room > %d bytes (%s)\n",
143 ARK_RX_META_SIZE, __func__);
144 return -1; /* ERROR CODE */
147 if (!rte_is_power_of_2(nb_desc)) {
149 "DPDK Arkville configuration queue size must be power of two %u (%s)\n",
151 return -1; /* ERROR CODE */
154 /* Allocate queue struct */
155 queue = rte_zmalloc_socket("Ark_rxqueue",
156 sizeof(struct ark_rx_queue),
160 PMD_DRV_LOG(ERR, "Failed to allocate memory in %s\n", __func__);
164 /* NOTE zmalloc is used, no need to 0 indexes, etc. */
165 queue->mb_pool = mb_pool;
166 queue->phys_qid = qidx;
167 queue->queue_index = queue_idx;
168 queue->queue_size = nb_desc;
169 queue->queue_mask = nb_desc - 1;
172 rte_zmalloc_socket("Ark_rx_queue mbuf",
173 nb_desc * sizeof(struct rte_mbuf *),
177 rte_zmalloc_socket("Ark_rx_queue paddr",
178 nb_desc * sizeof(rte_iova_t),
182 if (queue->reserve_q == 0 || queue->paddress_q == 0) {
184 "Failed to allocate queue memory in %s\n",
186 rte_free(queue->reserve_q);
187 rte_free(queue->paddress_q);
192 dev->data->rx_queues[queue_idx] = queue;
193 queue->udm = RTE_PTR_ADD(ark->udm.v, qidx * ARK_UDM_QOFFSET);
194 queue->mpu = RTE_PTR_ADD(ark->mpurx.v, qidx * ARK_MPU_QOFFSET);
196 /* populate mbuf reserve */
197 status = eth_ark_rx_seed_mbufs(queue);
201 status = eth_ark_rx_hw_setup(dev, queue, qidx, queue_idx);
203 if (unlikely(status != 0)) {
204 struct rte_mbuf *mbuf;
206 PMD_DRV_LOG(ERR, "Failed to initialize RX queue %d %s\n",
209 /* Free the mbufs allocated */
210 for (i = 0, mbuf = queue->reserve_q[0];
211 i < nb_desc; ++i, mbuf++) {
212 rte_pktmbuf_free(mbuf);
214 rte_free(queue->reserve_q);
215 rte_free(queue->paddress_q);
217 return -1; /* ERROR CODE */
223 /* ************************************************************************* */
225 eth_ark_recv_pkts_noop(void *rx_queue __rte_unused,
226 struct rte_mbuf **rx_pkts __rte_unused,
227 uint16_t nb_pkts __rte_unused)
232 /* ************************************************************************* */
234 eth_ark_recv_pkts(void *rx_queue,
235 struct rte_mbuf **rx_pkts,
238 struct ark_rx_queue *queue;
239 register uint32_t cons_index, prod_index;
241 struct rte_mbuf *mbuf;
242 struct ark_rx_meta *meta;
244 queue = (struct ark_rx_queue *)rx_queue;
245 if (unlikely(queue == 0))
247 if (unlikely(nb_pkts == 0))
249 prod_index = queue->prod_index;
250 cons_index = queue->cons_index;
253 while (prod_index != cons_index) {
254 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
256 rte_mbuf_prefetch_part1(mbuf);
257 rte_mbuf_prefetch_part2(mbuf);
259 /* META DATA embedded in headroom */
260 meta = RTE_PTR_ADD(mbuf->buf_addr, ARK_RX_META_OFFSET);
262 mbuf->port = meta->port;
263 mbuf->pkt_len = meta->pkt_len;
264 mbuf->data_len = meta->pkt_len;
265 mbuf->timestamp = meta->timestamp;
266 mbuf->udata64 = meta->user_data;
268 if (ARK_RX_DEBUG) { /* debug sanity checks */
269 if ((meta->pkt_len > (1024 * 16)) ||
270 (meta->pkt_len == 0)) {
271 PMD_RX_LOG(DEBUG, "RX: Bad Meta Q: %u"
274 " seed_index %" PRIU32
282 PMD_RX_LOG(DEBUG, " : UDM"
285 queue->udm->rt_cfg.prod_idx,
287 ark_mpu_dump(queue->mpu,
290 dump_mbuf_data(mbuf, 0, 256);
291 /* its FUBAR so fix it */
295 /* seqn is only set under debug */
296 mbuf->seqn = cons_index;
299 if (unlikely(meta->pkt_len > ARK_RX_MAX_NOCHAIN))
300 cons_index = eth_ark_rx_jumbo
301 (queue, meta, mbuf, cons_index + 1);
311 if (unlikely(nb != 0))
312 /* report next free to FPGA */
313 eth_ark_rx_update_cons_index(queue, cons_index);
318 /* ************************************************************************* */
320 eth_ark_rx_jumbo(struct ark_rx_queue *queue,
321 struct ark_rx_meta *meta,
322 struct rte_mbuf *mbuf0,
325 struct rte_mbuf *mbuf_prev;
326 struct rte_mbuf *mbuf;
332 /* first buf populated by called */
335 data_len = RTE_MIN(meta->pkt_len, RTE_MBUF_DEFAULT_DATAROOM);
336 remaining = meta->pkt_len - data_len;
337 mbuf0->data_len = data_len;
339 /* HW guarantees that the data does not exceed prod_index! */
340 while (remaining != 0) {
341 data_len = RTE_MIN(remaining,
342 RTE_MBUF_DEFAULT_DATAROOM +
343 RTE_PKTMBUF_HEADROOM);
345 remaining -= data_len;
348 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
349 mbuf_prev->next = mbuf;
351 mbuf->data_len = data_len;
354 mbuf->seqn = cons_index; /* for debug only */
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(struct rte_eth_dev *dev, uint16_t queue_id)
384 struct ark_rx_queue *queue;
386 queue = dev->data->rx_queues[queue_id];
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))
452 if (ARK_RX_DEBUG) { /* DEBUG */
453 while (count != nb) {
454 struct rte_mbuf *mbuf_init =
455 queue->reserve_q[seed_m + count];
457 memset(mbuf_init->buf_addr, -1, 512);
458 *((uint32_t *)mbuf_init->buf_addr) =
460 *(uint16_t *)RTE_PTR_ADD(mbuf_init->buf_addr, 4) =
466 queue->seed_index += nb;
468 /* Duff's device https://en.wikipedia.org/wiki/Duff's_device */
471 while (count != nb) {
472 queue->paddress_q[seed_m++] =
473 (*mbufs++)->buf_iova;
477 queue->paddress_q[seed_m++] =
478 (*mbufs++)->buf_iova;
482 queue->paddress_q[seed_m++] =
483 (*mbufs++)->buf_iova;
487 queue->paddress_q[seed_m++] =
488 (*mbufs++)->buf_iova;
492 } /* while (count != nb) */
499 eth_ark_rx_dump_queue(struct rte_eth_dev *dev, uint16_t queue_id,
502 struct ark_rx_queue *queue;
504 queue = dev->data->rx_queues[queue_id];
506 ark_ethdev_rx_dump(msg, queue);
509 /* ************************************************************************* */
510 /* Call on device closed no user API, queue is stopped */
512 eth_ark_dev_rx_queue_release(void *vqueue)
514 struct ark_rx_queue *queue;
517 queue = (struct ark_rx_queue *)vqueue;
521 ark_udm_queue_enable(queue->udm, 0);
522 /* Stop the MPU since pointer are going away */
523 ark_mpu_stop(queue->mpu);
525 /* Need to clear out mbufs here, dropping packets along the way */
526 eth_ark_rx_queue_drain(queue);
528 for (i = 0; i < queue->queue_size; ++i)
529 rte_pktmbuf_free(queue->reserve_q[i]);
531 rte_free(queue->reserve_q);
532 rte_free(queue->paddress_q);
537 eth_rx_queue_stats_get(void *vqueue, struct rte_eth_stats *stats)
539 struct ark_rx_queue *queue;
540 struct ark_udm_t *udm;
547 uint64_t ibytes = ark_udm_bytes(udm);
548 uint64_t ipackets = ark_udm_packets(udm);
549 uint64_t idropped = ark_udm_dropped(queue->udm);
551 stats->q_ipackets[queue->queue_index] = ipackets;
552 stats->q_ibytes[queue->queue_index] = ibytes;
553 stats->q_errors[queue->queue_index] = idropped;
554 stats->ipackets += ipackets;
555 stats->ibytes += ibytes;
556 stats->imissed += idropped;
560 eth_rx_queue_stats_reset(void *vqueue)
562 struct ark_rx_queue *queue;
568 ark_mpu_reset_stats(queue->mpu);
569 ark_udm_queue_stats_reset(queue->udm);
573 eth_ark_udm_force_close(struct rte_eth_dev *dev)
575 struct ark_adapter *ark = (struct ark_adapter *)dev->data->dev_private;
576 struct ark_rx_queue *queue;
580 if (!ark_udm_is_flushed(ark->udm.v)) {
581 /* restart the MPUs */
582 PMD_DRV_LOG(ERR, "ARK: %s UDM not flushed\n", __func__);
583 for (i = 0; i < dev->data->nb_rx_queues; i++) {
584 queue = (struct ark_rx_queue *)dev->data->rx_queues[i];
588 ark_mpu_start(queue->mpu);
589 /* Add some buffers */
590 index = 100000 + queue->seed_index;
591 ark_mpu_set_producer(queue->mpu, index);
593 /* Wait to allow data to pass */
596 PMD_DEBUG_LOG(DEBUG, "UDM forced flush attempt, stopped = %d\n",
597 ark_udm_is_flushed(ark->udm.v));
599 ark_udm_reset(ark->udm.v);
603 ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue)
607 PMD_DEBUG_LOG(DEBUG, "RX QUEUE %d -- %s", queue->phys_qid, name);
608 PMD_DEBUG_LOG(DEBUG, ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32 "\n",
609 "queue_size", queue->queue_size,
610 "seed_index", queue->seed_index,
611 "prod_index", queue->prod_index,
612 "cons_index", queue->cons_index);
614 ark_mpu_dump(queue->mpu, name, queue->phys_qid);
615 ark_mpu_dump_setup(queue->mpu, queue->phys_qid);
616 ark_udm_dump(queue->udm, name);
617 ark_udm_dump_setup(queue->udm, queue->phys_qid);
620 /* Only used in debug.
621 * This function is a raw memory dump of a portion of an mbuf's memory
622 * region. The usual function, rte_pktmbuf_dump() only shows data
623 * with respect to the data_off field. This function show data
624 * anywhere in the mbuf's buffer. This is useful for examining
625 * data in the headroom or tailroom portion of an mbuf.
628 dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi)
632 PMD_DRV_LOG(INFO, " MBUF: %p len %d, off: %d, seq: %" PRIU32 "\n", mbuf,
633 mbuf->pkt_len, mbuf->data_off, mbuf->seqn);
634 for (i = lo; i < hi; i += 16) {
635 uint8_t *dp = RTE_PTR_ADD(mbuf->buf_addr, i);
637 PMD_DRV_LOG(INFO, " %6d: ", i);
638 for (j = 0; j < 16; j++)
639 PMD_DRV_LOG(INFO, " %02x", dp[j]);
641 PMD_DRV_LOG(INFO, "\n");