4 * Copyright 2016 Freescale Semiconductor, Inc. All rights reserved.
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 Freescale Semiconductor, Inc 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.
43 #include <rte_config.h>
44 #include <rte_byteorder.h>
45 #include <rte_common.h>
46 #include <rte_interrupts.h>
48 #include <rte_debug.h>
50 #include <rte_atomic.h>
51 #include <rte_branch_prediction.h>
52 #include <rte_memory.h>
53 #include <rte_memzone.h>
54 #include <rte_tailq.h>
56 #include <rte_alarm.h>
57 #include <rte_ether.h>
58 #include <rte_ethdev.h>
59 #include <rte_atomic.h>
60 #include <rte_malloc.h>
66 #include "dpaa_ethdev.h"
67 #include "dpaa_rxtx.h"
68 #include <rte_dpaa_bus.h>
69 #include <dpaa_mempool.h>
77 #define DPAA_MBUF_TO_CONTIG_FD(_mbuf, _fd, _bpid) \
80 (_fd)->opaque_addr = 0; \
81 (_fd)->opaque = QM_FD_CONTIG << DPAA_FD_FORMAT_SHIFT; \
82 (_fd)->opaque |= ((_mbuf)->data_off) << DPAA_FD_OFFSET_SHIFT; \
83 (_fd)->opaque |= (_mbuf)->pkt_len; \
84 (_fd)->addr = (_mbuf)->buf_physaddr; \
85 (_fd)->bpid = _bpid; \
88 static inline struct rte_mbuf *dpaa_eth_fd_to_mbuf(struct qm_fd *fd,
91 struct dpaa_bp_info *bp_info = DPAA_BPID_TO_POOL_INFO(fd->bpid);
92 struct rte_mbuf *mbuf;
95 (fd->opaque & DPAA_FD_OFFSET_MASK) >> DPAA_FD_OFFSET_SHIFT;
96 uint32_t length = fd->opaque & DPAA_FD_LENGTH_MASK;
98 DPAA_DP_LOG(DEBUG, " FD--->MBUF");
100 /* Ignoring case when format != qm_fd_contig */
101 ptr = rte_dpaa_mem_ptov(fd->addr);
102 /* Ignoring case when ptr would be NULL. That is only possible incase
103 * of a corrupted packet
106 mbuf = (struct rte_mbuf *)((char *)ptr - bp_info->meta_data_size);
107 /* Prefetch the Parse results and packet data to L1 */
108 rte_prefetch0((void *)((uint8_t *)ptr + DEFAULT_RX_ICEOF));
109 rte_prefetch0((void *)((uint8_t *)ptr + offset));
111 mbuf->data_off = offset;
112 mbuf->data_len = length;
113 mbuf->pkt_len = length;
119 rte_mbuf_refcnt_set(mbuf, 1);
124 uint16_t dpaa_eth_queue_rx(void *q,
125 struct rte_mbuf **bufs,
128 struct qman_fq *fq = q;
129 struct qm_dqrr_entry *dq;
130 uint32_t num_rx = 0, ifid = ((struct dpaa_if *)fq->dpaa_intf)->ifid;
133 ret = rte_dpaa_portal_init((void *)0);
135 DPAA_PMD_ERR("Failure in affining portal");
139 ret = qman_set_vdq(fq, (nb_bufs > DPAA_MAX_DEQUEUE_NUM_FRAMES) ?
140 DPAA_MAX_DEQUEUE_NUM_FRAMES : nb_bufs);
145 dq = qman_dequeue(fq);
148 bufs[num_rx++] = dpaa_eth_fd_to_mbuf(&dq->fd, ifid);
149 qman_dqrr_consume(fq, dq);
150 } while (fq->flags & QMAN_FQ_STATE_VDQCR);
155 static void *dpaa_get_pktbuf(struct dpaa_bp_info *bp_info)
159 struct bm_buffer bufs;
161 ret = bman_acquire(bp_info->bp, &bufs, 1, 0);
163 DPAA_PMD_WARN("Failed to allocate buffers %d", ret);
167 DPAA_DP_LOG(DEBUG, "got buffer 0x%lx from pool %d",
168 (uint64_t)bufs.addr, bufs.bpid);
170 buf = (uint64_t)rte_dpaa_mem_ptov(bufs.addr) - bp_info->meta_data_size;
178 static struct rte_mbuf *dpaa_get_dmable_mbuf(struct rte_mbuf *mbuf,
179 struct dpaa_if *dpaa_intf)
181 struct rte_mbuf *dpaa_mbuf;
183 /* allocate pktbuffer on bpid for dpaa port */
184 dpaa_mbuf = dpaa_get_pktbuf(dpaa_intf->bp_info);
188 memcpy((uint8_t *)(dpaa_mbuf->buf_addr) + mbuf->data_off, (void *)
189 ((uint8_t *)(mbuf->buf_addr) + mbuf->data_off), mbuf->pkt_len);
191 /* Copy only the required fields */
192 dpaa_mbuf->data_off = mbuf->data_off;
193 dpaa_mbuf->pkt_len = mbuf->pkt_len;
194 dpaa_mbuf->ol_flags = mbuf->ol_flags;
195 dpaa_mbuf->packet_type = mbuf->packet_type;
196 dpaa_mbuf->tx_offload = mbuf->tx_offload;
197 rte_pktmbuf_free(mbuf);
201 /* Handle mbufs which are not segmented (non SG) */
203 tx_on_dpaa_pool_unsegmented(struct rte_mbuf *mbuf,
204 struct dpaa_bp_info *bp_info,
205 struct qm_fd *fd_arr)
207 struct rte_mbuf *mi = NULL;
209 if (RTE_MBUF_DIRECT(mbuf)) {
210 if (rte_mbuf_refcnt_read(mbuf) > 1) {
211 /* In case of direct mbuf and mbuf being cloned,
212 * BMAN should _not_ release buffer.
214 DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, 0xff);
215 /* Buffer should be releasd by EAL */
216 rte_mbuf_refcnt_update(mbuf, -1);
218 /* In case of direct mbuf and no cloning, mbuf can be
221 DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, bp_info->bpid);
224 /* This is data-containing core mbuf: 'mi' */
225 mi = rte_mbuf_from_indirect(mbuf);
226 if (rte_mbuf_refcnt_read(mi) > 1) {
227 /* In case of indirect mbuf, and mbuf being cloned,
228 * BMAN should _not_ release it and let EAL release
229 * it through pktmbuf_free below.
231 DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, 0xff);
233 /* In case of indirect mbuf, and no cloning, core mbuf
234 * should be released by BMAN.
235 * Increate refcnt of core mbuf so that when
236 * pktmbuf_free is called and mbuf is released, EAL
237 * doesn't try to release core mbuf which would have
238 * been released by BMAN.
240 rte_mbuf_refcnt_update(mi, 1);
241 DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, bp_info->bpid);
243 rte_pktmbuf_free(mbuf);
247 /* Handle all mbufs on dpaa BMAN managed pool */
248 static inline uint16_t
249 tx_on_dpaa_pool(struct rte_mbuf *mbuf,
250 struct dpaa_bp_info *bp_info,
251 struct qm_fd *fd_arr)
253 DPAA_DP_LOG(DEBUG, "BMAN offloaded buffer, mbuf: %p", mbuf);
255 if (mbuf->nb_segs == 1) {
256 /* Case for non-segmented buffers */
257 tx_on_dpaa_pool_unsegmented(mbuf, bp_info, fd_arr);
259 DPAA_PMD_DEBUG("Number of Segments not supported");
266 /* Handle all mbufs on an external pool (non-dpaa) */
267 static inline uint16_t
268 tx_on_external_pool(struct qman_fq *txq, struct rte_mbuf *mbuf,
269 struct qm_fd *fd_arr)
271 struct dpaa_if *dpaa_intf = txq->dpaa_intf;
272 struct rte_mbuf *dmable_mbuf;
274 DPAA_DP_LOG(DEBUG, "Non-BMAN offloaded buffer."
275 "Allocating an offloaded buffer");
276 dmable_mbuf = dpaa_get_dmable_mbuf(mbuf, dpaa_intf);
278 DPAA_DP_LOG(DEBUG, "no dpaa buffers.");
282 DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, dpaa_intf->bp_info->bpid);
288 dpaa_eth_queue_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
290 struct rte_mbuf *mbuf, *mi = NULL;
291 struct rte_mempool *mp;
292 struct dpaa_bp_info *bp_info;
293 struct qm_fd fd_arr[MAX_TX_RING_SLOTS];
294 uint32_t frames_to_send, loop, i = 0;
298 ret = rte_dpaa_portal_init((void *)0);
300 DPAA_PMD_ERR("Failure in affining portal");
304 DPAA_DP_LOG(DEBUG, "Transmitting %d buffers on queue: %p", nb_bufs, q);
307 frames_to_send = (nb_bufs >> 3) ? MAX_TX_RING_SLOTS : nb_bufs;
308 for (loop = 0; loop < frames_to_send; loop++, i++) {
310 if (RTE_MBUF_DIRECT(mbuf)) {
313 mi = rte_mbuf_from_indirect(mbuf);
317 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
318 if (likely(mp->ops_index == bp_info->dpaa_ops_index)) {
319 state = tx_on_dpaa_pool(mbuf, bp_info,
321 if (unlikely(state)) {
322 /* Set frames_to_send & nb_bufs so
323 * that packets are transmitted till
326 frames_to_send = loop;
331 state = tx_on_external_pool(q, mbuf,
333 if (unlikely(state)) {
334 /* Set frames_to_send & nb_bufs so
335 * that packets are transmitted till
338 frames_to_send = loop;
347 while (loop < frames_to_send) {
348 loop += qman_enqueue_multi(q, &fd_arr[loop],
349 frames_to_send - loop);
351 nb_bufs -= frames_to_send;
354 DPAA_DP_LOG(DEBUG, "Transmitted %d buffers on queue: %p", i, q);
359 uint16_t dpaa_eth_tx_drop_all(void *q __rte_unused,
360 struct rte_mbuf **bufs __rte_unused,
361 uint16_t nb_bufs __rte_unused)
363 DPAA_DP_LOG(DEBUG, "Drop all packets");
365 /* Drop all incoming packets. No need to free packets here
366 * because the rte_eth f/w frees up the packets through tx_buffer
367 * callback in case this functions returns count less than nb_bufs