4 * Copyright (c) 2015-2017 Atomic Rules LLC
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "ark_ethdev_rx.h"
37 #include "ark_global.h"
42 #define ARK_RX_META_SIZE 32
43 #define ARK_RX_META_OFFSET (RTE_PKTMBUF_HEADROOM - ARK_RX_META_SIZE)
44 #define ARK_RX_MAX_NOCHAIN (RTE_MBUF_DEFAULT_DATAROOM)
46 /* Forward declarations */
50 static void dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi);
51 static void ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue);
52 static uint32_t eth_ark_rx_jumbo(struct ark_rx_queue *queue,
53 struct ark_rx_meta *meta,
54 struct rte_mbuf *mbuf0,
56 static inline int eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue);
58 /* ************************************************************************* */
60 /* array of mbufs to populate */
61 struct rte_mbuf **reserve_q;
62 /* array of physical addresses of the mbuf data pointer */
63 /* This point is a virtual address */
64 phys_addr_t *paddress_q;
65 struct rte_mempool *mb_pool;
67 struct ark_udm_t *udm;
68 struct ark_mpu_t *mpu;
73 uint32_t seed_index; /* step 1 set with empty mbuf */
74 uint32_t cons_index; /* step 3 consumed by driver */
76 /* The queue Id is used to identify the HW Q */
79 /* The queue Index is used within the dpdk device structures */
84 /* separate cache line */
85 /* second cache line - fields only used in slow path */
86 MARKER cacheline1 __rte_cache_min_aligned;
88 volatile uint32_t prod_index; /* step 2 filled by FPGA */
89 } __rte_cache_aligned;
92 /* ************************************************************************* */
94 eth_ark_rx_hw_setup(struct rte_eth_dev *dev,
95 struct ark_rx_queue *queue,
96 uint16_t rx_queue_id __rte_unused, uint16_t rx_queue_idx)
98 phys_addr_t queue_base;
99 phys_addr_t phys_addr_q_base;
100 phys_addr_t phys_addr_prod_index;
102 queue_base = rte_malloc_virt2phy(queue);
103 phys_addr_prod_index = queue_base +
104 offsetof(struct ark_rx_queue, prod_index);
106 phys_addr_q_base = rte_malloc_virt2phy(queue->paddress_q);
109 if (ark_mpu_verify(queue->mpu, sizeof(phys_addr_t))) {
110 PMD_DRV_LOG(ERR, "Illegal configuration rx queue\n");
114 /* Stop and Reset and configure MPU */
115 ark_mpu_configure(queue->mpu, phys_addr_q_base, queue->queue_size, 0);
117 ark_udm_write_addr(queue->udm, phys_addr_prod_index);
119 /* advance the valid pointer, but don't start until the queue starts */
120 ark_mpu_reset_stats(queue->mpu);
122 /* The seed is the producer index for the HW */
123 ark_mpu_set_producer(queue->mpu, queue->seed_index);
124 dev->data->rx_queue_state[rx_queue_idx] = RTE_ETH_QUEUE_STATE_STOPPED;
130 eth_ark_rx_update_cons_index(struct ark_rx_queue *queue, uint32_t cons_index)
132 queue->cons_index = cons_index;
133 eth_ark_rx_seed_mbufs(queue);
134 ark_mpu_set_producer(queue->mpu, queue->seed_index);
137 /* ************************************************************************* */
139 eth_ark_dev_rx_queue_setup(struct rte_eth_dev *dev,
142 unsigned int socket_id,
143 const struct rte_eth_rxconf *rx_conf,
144 struct rte_mempool *mb_pool)
146 static int warning1; /* = 0 */
147 struct ark_adapter *ark = (struct ark_adapter *)dev->data->dev_private;
149 struct ark_rx_queue *queue;
153 /* Future works: divide the Q's evenly with multi-ports */
154 int port = dev->data->port_id;
155 int qidx = port + queue_idx;
157 /* We may already be setup, free memory prior to re-allocation */
158 if (dev->data->rx_queues[queue_idx] != NULL) {
159 eth_ark_dev_rx_queue_release(dev->data->rx_queues[queue_idx]);
160 dev->data->rx_queues[queue_idx] = NULL;
163 if (rx_conf != NULL && warning1 == 0) {
166 "Arkville ignores rte_eth_rxconf argument.\n");
169 if (RTE_PKTMBUF_HEADROOM < ARK_RX_META_SIZE) {
171 "Error: DPDK Arkville requires head room > %d bytes (%s)\n",
172 ARK_RX_META_SIZE, __func__);
173 return -1; /* ERROR CODE */
176 if (!rte_is_power_of_2(nb_desc)) {
178 "DPDK Arkville configuration queue size must be power of two %u (%s)\n",
180 return -1; /* ERROR CODE */
183 /* Allocate queue struct */
184 queue = rte_zmalloc_socket("Ark_rxqueue",
185 sizeof(struct ark_rx_queue),
189 PMD_DRV_LOG(ERR, "Failed to allocate memory in %s\n", __func__);
193 /* NOTE zmalloc is used, no need to 0 indexes, etc. */
194 queue->mb_pool = mb_pool;
195 queue->phys_qid = qidx;
196 queue->queue_index = queue_idx;
197 queue->queue_size = nb_desc;
198 queue->queue_mask = nb_desc - 1;
201 rte_zmalloc_socket("Ark_rx_queue mbuf",
202 nb_desc * sizeof(struct rte_mbuf *),
206 rte_zmalloc_socket("Ark_rx_queue paddr",
207 nb_desc * sizeof(phys_addr_t),
211 if (queue->reserve_q == 0 || queue->paddress_q == 0) {
213 "Failed to allocate queue memory in %s\n",
215 rte_free(queue->reserve_q);
216 rte_free(queue->paddress_q);
221 dev->data->rx_queues[queue_idx] = queue;
222 queue->udm = RTE_PTR_ADD(ark->udm.v, qidx * ARK_UDM_QOFFSET);
223 queue->mpu = RTE_PTR_ADD(ark->mpurx.v, qidx * ARK_MPU_QOFFSET);
225 /* populate mbuf reserve */
226 status = eth_ark_rx_seed_mbufs(queue);
230 status = eth_ark_rx_hw_setup(dev, queue, qidx, queue_idx);
232 if (unlikely(status != 0)) {
233 struct rte_mbuf *mbuf;
235 PMD_DRV_LOG(ERR, "Failed to initialize RX queue %d %s\n",
238 /* Free the mbufs allocated */
239 for (i = 0, mbuf = queue->reserve_q[0];
240 i < nb_desc; ++i, mbuf++) {
241 rte_pktmbuf_free(mbuf);
243 rte_free(queue->reserve_q);
244 rte_free(queue->paddress_q);
246 return -1; /* ERROR CODE */
252 /* ************************************************************************* */
254 eth_ark_recv_pkts_noop(void *rx_queue __rte_unused,
255 struct rte_mbuf **rx_pkts __rte_unused,
256 uint16_t nb_pkts __rte_unused)
261 /* ************************************************************************* */
263 eth_ark_recv_pkts(void *rx_queue,
264 struct rte_mbuf **rx_pkts,
267 struct ark_rx_queue *queue;
268 register uint32_t cons_index, prod_index;
270 struct rte_mbuf *mbuf;
271 struct ark_rx_meta *meta;
273 queue = (struct ark_rx_queue *)rx_queue;
274 if (unlikely(queue == 0))
276 if (unlikely(nb_pkts == 0))
278 prod_index = queue->prod_index;
279 cons_index = queue->cons_index;
282 while (prod_index != cons_index) {
283 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
285 rte_mbuf_prefetch_part1(mbuf);
286 rte_mbuf_prefetch_part2(mbuf);
288 /* META DATA embedded in headroom */
289 meta = RTE_PTR_ADD(mbuf->buf_addr, ARK_RX_META_OFFSET);
291 mbuf->port = meta->port;
292 mbuf->pkt_len = meta->pkt_len;
293 mbuf->data_len = meta->pkt_len;
294 mbuf->timestamp = meta->timestamp;
295 mbuf->udata64 = meta->user_data;
297 if (ARK_RX_DEBUG) { /* debug sanity checks */
298 if ((meta->pkt_len > (1024 * 16)) ||
299 (meta->pkt_len == 0)) {
300 PMD_RX_LOG(DEBUG, "RX: Bad Meta Q: %u"
303 " seed_index %" PRIU32
311 PMD_RX_LOG(DEBUG, " : UDM"
314 queue->udm->rt_cfg.prod_idx,
316 ark_mpu_dump(queue->mpu,
319 dump_mbuf_data(mbuf, 0, 256);
320 /* its FUBAR so fix it */
324 /* seqn is only set under debug */
325 mbuf->seqn = cons_index;
328 if (unlikely(meta->pkt_len > ARK_RX_MAX_NOCHAIN))
329 cons_index = eth_ark_rx_jumbo
330 (queue, meta, mbuf, cons_index + 1);
340 if (unlikely(nb != 0))
341 /* report next free to FPGA */
342 eth_ark_rx_update_cons_index(queue, cons_index);
347 /* ************************************************************************* */
349 eth_ark_rx_jumbo(struct ark_rx_queue *queue,
350 struct ark_rx_meta *meta,
351 struct rte_mbuf *mbuf0,
354 struct rte_mbuf *mbuf_prev;
355 struct rte_mbuf *mbuf;
361 /* first buf populated by called */
364 data_len = RTE_MIN(meta->pkt_len, RTE_MBUF_DEFAULT_DATAROOM);
365 remaining = meta->pkt_len - data_len;
366 mbuf0->data_len = data_len;
368 /* HW guarantees that the data does not exceed prod_index! */
369 while (remaining != 0) {
370 data_len = RTE_MIN(remaining,
371 RTE_MBUF_DEFAULT_DATAROOM +
372 RTE_PKTMBUF_HEADROOM);
374 remaining -= data_len;
377 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
378 mbuf_prev->next = mbuf;
380 mbuf->data_len = data_len;
383 mbuf->seqn = cons_index; /* for debug only */
388 mbuf0->nb_segs = segments;
392 /* Drain the internal queue allowing hw to clear out. */
394 eth_ark_rx_queue_drain(struct ark_rx_queue *queue)
396 register uint32_t cons_index;
397 struct rte_mbuf *mbuf;
399 cons_index = queue->cons_index;
401 /* NOT performance optimized, since this is a one-shot call */
402 while ((cons_index ^ queue->prod_index) & queue->queue_mask) {
403 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
404 rte_pktmbuf_free(mbuf);
406 eth_ark_rx_update_cons_index(queue, cons_index);
411 eth_ark_dev_rx_queue_count(struct rte_eth_dev *dev, uint16_t queue_id)
413 struct ark_rx_queue *queue;
415 queue = dev->data->rx_queues[queue_id];
416 return (queue->prod_index - queue->cons_index); /* mod arith */
419 /* ************************************************************************* */
421 eth_ark_rx_start_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 dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
431 ark_mpu_set_producer(queue->mpu, queue->seed_index);
432 ark_mpu_start(queue->mpu);
434 ark_udm_queue_enable(queue->udm, 1);
439 /* ************************************************************************* */
441 /* Queue can be restarted. data remains
444 eth_ark_rx_stop_queue(struct rte_eth_dev *dev, uint16_t queue_id)
446 struct ark_rx_queue *queue;
448 queue = dev->data->rx_queues[queue_id];
452 ark_udm_queue_enable(queue->udm, 0);
454 dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
459 /* ************************************************************************* */
461 eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue)
463 uint32_t limit = queue->cons_index + queue->queue_size;
464 uint32_t seed_index = queue->seed_index;
467 uint32_t seed_m = queue->seed_index & queue->queue_mask;
469 uint32_t nb = limit - seed_index;
471 /* Handle wrap around -- remainder is filled on the next call */
472 if (unlikely(seed_m + nb > queue->queue_size))
473 nb = queue->queue_size - seed_m;
475 struct rte_mbuf **mbufs = &queue->reserve_q[seed_m];
476 int status = rte_pktmbuf_alloc_bulk(queue->mb_pool, mbufs, nb);
478 if (unlikely(status != 0))
481 if (ARK_RX_DEBUG) { /* DEBUG */
482 while (count != nb) {
483 struct rte_mbuf *mbuf_init =
484 queue->reserve_q[seed_m + count];
486 memset(mbuf_init->buf_addr, -1, 512);
487 *((uint32_t *)mbuf_init->buf_addr) =
489 *(uint16_t *)RTE_PTR_ADD(mbuf_init->buf_addr, 4) =
495 queue->seed_index += nb;
497 /* Duff's device https://en.wikipedia.org/wiki/Duff's_device */
500 while (count != nb) {
501 queue->paddress_q[seed_m++] =
502 (*mbufs++)->buf_physaddr;
506 queue->paddress_q[seed_m++] =
507 (*mbufs++)->buf_physaddr;
511 queue->paddress_q[seed_m++] =
512 (*mbufs++)->buf_physaddr;
516 queue->paddress_q[seed_m++] =
517 (*mbufs++)->buf_physaddr;
521 } /* while (count != nb) */
528 eth_ark_rx_dump_queue(struct rte_eth_dev *dev, uint16_t queue_id,
531 struct ark_rx_queue *queue;
533 queue = dev->data->rx_queues[queue_id];
535 ark_ethdev_rx_dump(msg, queue);
538 /* ************************************************************************* */
539 /* Call on device closed no user API, queue is stopped */
541 eth_ark_dev_rx_queue_release(void *vqueue)
543 struct ark_rx_queue *queue;
546 queue = (struct ark_rx_queue *)vqueue;
550 ark_udm_queue_enable(queue->udm, 0);
551 /* Stop the MPU since pointer are going away */
552 ark_mpu_stop(queue->mpu);
554 /* Need to clear out mbufs here, dropping packets along the way */
555 eth_ark_rx_queue_drain(queue);
557 for (i = 0; i < queue->queue_size; ++i)
558 rte_pktmbuf_free(queue->reserve_q[i]);
560 rte_free(queue->reserve_q);
561 rte_free(queue->paddress_q);
566 eth_rx_queue_stats_get(void *vqueue, struct rte_eth_stats *stats)
568 struct ark_rx_queue *queue;
569 struct ark_udm_t *udm;
576 uint64_t ibytes = ark_udm_bytes(udm);
577 uint64_t ipackets = ark_udm_packets(udm);
578 uint64_t idropped = ark_udm_dropped(queue->udm);
580 stats->q_ipackets[queue->queue_index] = ipackets;
581 stats->q_ibytes[queue->queue_index] = ibytes;
582 stats->q_errors[queue->queue_index] = idropped;
583 stats->ipackets += ipackets;
584 stats->ibytes += ibytes;
585 stats->imissed += idropped;
589 eth_rx_queue_stats_reset(void *vqueue)
591 struct ark_rx_queue *queue;
597 ark_mpu_reset_stats(queue->mpu);
598 ark_udm_queue_stats_reset(queue->udm);
602 eth_ark_udm_force_close(struct rte_eth_dev *dev)
604 struct ark_adapter *ark = (struct ark_adapter *)dev->data->dev_private;
605 struct ark_rx_queue *queue;
609 if (!ark_udm_is_flushed(ark->udm.v)) {
610 /* restart the MPUs */
611 PMD_DRV_LOG(ERR, "ARK: %s UDM not flushed\n", __func__);
612 for (i = 0; i < dev->data->nb_rx_queues; i++) {
613 queue = (struct ark_rx_queue *)dev->data->rx_queues[i];
617 ark_mpu_start(queue->mpu);
618 /* Add some buffers */
619 index = 100000 + queue->seed_index;
620 ark_mpu_set_producer(queue->mpu, index);
622 /* Wait to allow data to pass */
625 PMD_DEBUG_LOG(DEBUG, "UDM forced flush attempt, stopped = %d\n",
626 ark_udm_is_flushed(ark->udm.v));
628 ark_udm_reset(ark->udm.v);
632 ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue)
636 PMD_DEBUG_LOG(DEBUG, "RX QUEUE %d -- %s", queue->phys_qid, name);
637 PMD_DEBUG_LOG(DEBUG, ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32 "\n",
638 "queue_size", queue->queue_size,
639 "seed_index", queue->seed_index,
640 "prod_index", queue->prod_index,
641 "cons_index", queue->cons_index);
643 ark_mpu_dump(queue->mpu, name, queue->phys_qid);
644 ark_mpu_dump_setup(queue->mpu, queue->phys_qid);
645 ark_udm_dump(queue->udm, name);
646 ark_udm_dump_setup(queue->udm, queue->phys_qid);
649 /* Only used in debug.
650 * This function is a raw memory dump of a portion of an mbuf's memory
651 * region. The usual function, rte_pktmbuf_dump() only shows data
652 * with respect to the data_off field. This function show data
653 * anywhere in the mbuf's buffer. This is useful for examining
654 * data in the headroom or tailroom portion of an mbuf.
657 dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi)
661 PMD_DRV_LOG(INFO, " MBUF: %p len %d, off: %d, seq: %" PRIU32 "\n", mbuf,
662 mbuf->pkt_len, mbuf->data_off, mbuf->seqn);
663 for (i = lo; i < hi; i += 16) {
664 uint8_t *dp = RTE_PTR_ADD(mbuf->buf_addr, i);
666 PMD_DRV_LOG(INFO, " %6d: ", i);
667 for (j = 0; j < 16; j++)
668 PMD_DRV_LOG(INFO, " %02x", dp[j]);
670 PMD_DRV_LOG(INFO, "\n");