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;
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 ARK_PMD_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 if ((cons_index + queue->queue_size - queue->seed_index) >= 64U) {
105 eth_ark_rx_seed_mbufs(queue);
106 ark_mpu_set_producer(queue->mpu, queue->seed_index);
110 /* ************************************************************************* */
112 eth_ark_dev_rx_queue_setup(struct rte_eth_dev *dev,
115 unsigned int socket_id,
116 const struct rte_eth_rxconf *rx_conf,
117 struct rte_mempool *mb_pool)
119 static int warning1; /* = 0 */
120 struct ark_adapter *ark = dev->data->dev_private;
122 struct ark_rx_queue *queue;
126 int qidx = 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 ARK_PMD_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);
199 if (queue->seed_index != nb_desc) {
200 ARK_PMD_LOG(ERR, "Failed to allocate %u mbufs for RX queue %d\n",
206 status = eth_ark_rx_hw_setup(dev, queue, qidx, queue_idx);
208 if (unlikely(status != 0)) {
209 struct rte_mbuf **mbuf;
211 ARK_PMD_LOG(ERR, "Failed to initialize RX queue %d %s\n",
214 /* Free the mbufs allocated */
215 for (i = 0, mbuf = queue->reserve_q;
216 i < queue->seed_index; ++i, mbuf++) {
217 rte_pktmbuf_free(*mbuf);
219 rte_free(queue->reserve_q);
220 rte_free(queue->paddress_q);
222 return -1; /* ERROR CODE */
228 /* ************************************************************************* */
230 eth_ark_recv_pkts_noop(void *rx_queue __rte_unused,
231 struct rte_mbuf **rx_pkts __rte_unused,
232 uint16_t nb_pkts __rte_unused)
237 /* ************************************************************************* */
239 eth_ark_recv_pkts(void *rx_queue,
240 struct rte_mbuf **rx_pkts,
243 struct ark_rx_queue *queue;
244 register uint32_t cons_index, prod_index;
246 struct rte_mbuf *mbuf;
247 struct ark_rx_meta *meta;
249 queue = (struct ark_rx_queue *)rx_queue;
250 if (unlikely(queue == 0))
252 if (unlikely(nb_pkts == 0))
254 prod_index = queue->prod_index;
255 cons_index = queue->cons_index;
258 while (prod_index != cons_index) {
259 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
261 rte_mbuf_prefetch_part1(mbuf);
262 rte_mbuf_prefetch_part2(mbuf);
264 /* META DATA embedded in headroom */
265 meta = RTE_PTR_ADD(mbuf->buf_addr, ARK_RX_META_OFFSET);
267 mbuf->pkt_len = meta->pkt_len;
268 mbuf->data_len = meta->pkt_len;
269 /* set timestamp if enabled at least on one device */
270 if (ark_timestamp_rx_dynflag > 0) {
271 *RTE_MBUF_DYNFIELD(mbuf, ark_timestamp_dynfield_offset,
272 rte_mbuf_timestamp_t *) = meta->timestamp;
273 mbuf->ol_flags |= ark_timestamp_rx_dynflag;
275 rte_pmd_ark_mbuf_rx_userdata_set(mbuf, meta->user_data);
277 if (ARK_DEBUG_CORE) { /* debug sanity checks */
278 if ((meta->pkt_len > (1024 * 16)) ||
279 (meta->pkt_len == 0)) {
280 ARK_PMD_LOG(DEBUG, "RX: Bad Meta Q: %u"
283 " seed_index %" PRIU32
291 ARK_PMD_LOG(DEBUG, " : UDM"
294 queue->udm->rt_cfg.prod_idx,
296 ark_mpu_dump(queue->mpu,
299 dump_mbuf_data(mbuf, 0, 256);
300 /* its FUBAR so fix it */
306 if (unlikely(meta->pkt_len > ARK_RX_MAX_NOCHAIN))
307 cons_index = eth_ark_rx_jumbo
308 (queue, meta, mbuf, cons_index + 1);
318 eth_ark_rx_update_cons_index(queue, cons_index);
323 /* ************************************************************************* */
325 eth_ark_rx_jumbo(struct ark_rx_queue *queue,
326 struct ark_rx_meta *meta,
327 struct rte_mbuf *mbuf0,
330 struct rte_mbuf *mbuf_prev;
331 struct rte_mbuf *mbuf;
337 /* first buf populated by called */
340 data_len = RTE_MIN(meta->pkt_len, RTE_MBUF_DEFAULT_DATAROOM);
341 remaining = meta->pkt_len - data_len;
342 mbuf0->data_len = data_len;
344 /* HW guarantees that the data does not exceed prod_index! */
345 while (remaining != 0) {
346 data_len = RTE_MIN(remaining,
347 RTE_MBUF_DEFAULT_DATAROOM);
349 remaining -= data_len;
352 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
353 mbuf_prev->next = mbuf;
355 mbuf->data_len = data_len;
360 mbuf0->nb_segs = segments;
364 /* Drain the internal queue allowing hw to clear out. */
366 eth_ark_rx_queue_drain(struct ark_rx_queue *queue)
368 register uint32_t cons_index;
369 struct rte_mbuf *mbuf;
371 cons_index = queue->cons_index;
373 /* NOT performance optimized, since this is a one-shot call */
374 while ((cons_index ^ queue->prod_index) & queue->queue_mask) {
375 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
376 rte_pktmbuf_free(mbuf);
378 eth_ark_rx_update_cons_index(queue, cons_index);
383 eth_ark_dev_rx_queue_count(struct rte_eth_dev *dev, uint16_t queue_id)
385 struct ark_rx_queue *queue;
387 queue = dev->data->rx_queues[queue_id];
388 return (queue->prod_index - queue->cons_index); /* mod arith */
391 /* ************************************************************************* */
393 eth_ark_rx_start_queue(struct rte_eth_dev *dev, uint16_t queue_id)
395 struct ark_rx_queue *queue;
397 queue = dev->data->rx_queues[queue_id];
401 dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
403 ark_mpu_set_producer(queue->mpu, queue->seed_index);
404 ark_mpu_start(queue->mpu);
406 ark_udm_queue_enable(queue->udm, 1);
411 /* ************************************************************************* */
413 /* Queue can be restarted. data remains
416 eth_ark_rx_stop_queue(struct rte_eth_dev *dev, uint16_t queue_id)
418 struct ark_rx_queue *queue;
420 queue = dev->data->rx_queues[queue_id];
424 ark_udm_queue_enable(queue->udm, 0);
426 dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
431 /* ************************************************************************* */
433 eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue)
435 uint32_t limit = queue->cons_index + queue->queue_size;
436 uint32_t seed_index = queue->seed_index;
439 uint32_t seed_m = queue->seed_index & queue->queue_mask;
441 uint32_t nb = limit - seed_index;
443 /* Handle wrap around -- remainder is filled on the next call */
444 if (unlikely(seed_m + nb > queue->queue_size))
445 nb = queue->queue_size - seed_m;
447 struct rte_mbuf **mbufs = &queue->reserve_q[seed_m];
448 int status = rte_pktmbuf_alloc_bulk(queue->mb_pool, mbufs, nb);
450 if (unlikely(status != 0)) {
452 "Could not allocate %u mbufs from pool"
454 " %u free buffers remaining in queue\n",
455 nb, queue->queue_index,
456 queue->seed_index - queue->cons_index);
460 if (ARK_DEBUG_CORE) { /* DEBUG */
461 while (count != nb) {
462 struct rte_mbuf *mbuf_init =
463 queue->reserve_q[seed_m + count];
465 memset(mbuf_init->buf_addr, -1, 512);
466 *((uint32_t *)mbuf_init->buf_addr) =
468 *(uint16_t *)RTE_PTR_ADD(mbuf_init->buf_addr, 4) =
474 queue->seed_index += nb;
476 /* Duff's device https://en.wikipedia.org/wiki/Duff's_device */
479 while (count != nb) {
480 queue->paddress_q[seed_m++] =
481 (*mbufs++)->buf_iova;
485 queue->paddress_q[seed_m++] =
486 (*mbufs++)->buf_iova;
490 queue->paddress_q[seed_m++] =
491 (*mbufs++)->buf_iova;
495 queue->paddress_q[seed_m++] =
496 (*mbufs++)->buf_iova;
500 } /* while (count != nb) */
507 eth_ark_rx_dump_queue(struct rte_eth_dev *dev, uint16_t queue_id,
510 struct ark_rx_queue *queue;
512 queue = dev->data->rx_queues[queue_id];
514 ark_ethdev_rx_dump(msg, queue);
517 /* ************************************************************************* */
518 /* Call on device closed no user API, queue is stopped */
520 eth_ark_dev_rx_queue_release(void *vqueue)
522 struct ark_rx_queue *queue;
525 queue = (struct ark_rx_queue *)vqueue;
529 ark_udm_queue_enable(queue->udm, 0);
530 /* Stop the MPU since pointer are going away */
531 ark_mpu_stop(queue->mpu);
533 /* Need to clear out mbufs here, dropping packets along the way */
534 eth_ark_rx_queue_drain(queue);
536 for (i = 0; i < queue->queue_size; ++i)
537 rte_pktmbuf_free(queue->reserve_q[i]);
539 rte_free(queue->reserve_q);
540 rte_free(queue->paddress_q);
545 eth_rx_queue_stats_get(void *vqueue, struct rte_eth_stats *stats)
547 struct ark_rx_queue *queue;
548 struct ark_udm_t *udm;
555 uint64_t ibytes = ark_udm_bytes(udm);
556 uint64_t ipackets = ark_udm_packets(udm);
557 uint64_t idropped = ark_udm_dropped(queue->udm);
559 stats->q_ipackets[queue->queue_index] = ipackets;
560 stats->q_ibytes[queue->queue_index] = ibytes;
561 stats->q_errors[queue->queue_index] = idropped;
562 stats->ipackets += ipackets;
563 stats->ibytes += ibytes;
564 stats->imissed += idropped;
568 eth_rx_queue_stats_reset(void *vqueue)
570 struct ark_rx_queue *queue;
576 ark_mpu_reset_stats(queue->mpu);
577 ark_udm_queue_stats_reset(queue->udm);
581 eth_ark_udm_force_close(struct rte_eth_dev *dev)
583 struct ark_adapter *ark = dev->data->dev_private;
584 struct ark_rx_queue *queue;
588 if (!ark_udm_is_flushed(ark->udm.v)) {
589 /* restart the MPUs */
590 ARK_PMD_LOG(NOTICE, "UDM not flushed -- forcing flush\n");
591 for (i = 0; i < dev->data->nb_rx_queues; i++) {
592 queue = (struct ark_rx_queue *)dev->data->rx_queues[i];
596 ark_mpu_start(queue->mpu);
597 /* Add some buffers */
598 index = 100000 + queue->seed_index;
599 ark_mpu_set_producer(queue->mpu, index);
601 /* Wait to allow data to pass */
604 ARK_PMD_LOG(DEBUG, "UDM forced flush attempt, stopped = %d\n",
605 ark_udm_is_flushed(ark->udm.v));
607 ark_udm_reset(ark->udm.v);
611 ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue)
615 ARK_PMD_LOG(DEBUG, "RX QUEUE %d -- %s", queue->phys_qid, name);
616 ARK_PMD_LOG(DEBUG, ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32 "\n",
617 "queue_size", queue->queue_size,
618 "seed_index", queue->seed_index,
619 "prod_index", queue->prod_index,
620 "cons_index", queue->cons_index);
622 ark_mpu_dump(queue->mpu, name, queue->phys_qid);
623 ark_mpu_dump_setup(queue->mpu, queue->phys_qid);
624 ark_udm_dump(queue->udm, name);
625 ark_udm_dump_setup(queue->udm, queue->phys_qid);
628 /* Only used in debug.
629 * This function is a raw memory dump of a portion of an mbuf's memory
630 * region. The usual function, rte_pktmbuf_dump() only shows data
631 * with respect to the data_off field. This function show data
632 * anywhere in the mbuf's buffer. This is useful for examining
633 * data in the headroom or tailroom portion of an mbuf.
636 dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi)
640 ARK_PMD_LOG(DEBUG, " MBUF: %p len %d, off: %d\n",
641 mbuf, mbuf->pkt_len, mbuf->data_off);
642 for (i = lo; i < hi; i += 16) {
643 uint8_t *dp = RTE_PTR_ADD(mbuf->buf_addr, i);
645 ARK_PMD_LOG(DEBUG, " %6d: ", i);
646 for (j = 0; j < 16; j++)
647 ARK_PMD_LOG(DEBUG, " %02x", dp[j]);
649 ARK_PMD_LOG(DEBUG, "\n");