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 void dpaa_slow_parsing(struct rte_mbuf *m __rte_unused,
89 uint64_t prs __rte_unused)
91 DPAA_DP_LOG(DEBUG, "Slow parsing");
92 /*TBD:XXX: to be implemented*/
95 static inline void dpaa_eth_packet_info(struct rte_mbuf *m,
96 uint64_t fd_virt_addr)
98 struct annotations_t *annot = GET_ANNOTATIONS(fd_virt_addr);
99 uint64_t prs = *((uint64_t *)(&annot->parse)) & DPAA_PARSE_MASK;
101 DPAA_DP_LOG(DEBUG, " Parsing mbuf: %p with annotations: %p", m, annot);
104 case DPAA_PKT_TYPE_NONE:
107 case DPAA_PKT_TYPE_ETHER:
108 m->packet_type = RTE_PTYPE_L2_ETHER;
110 case DPAA_PKT_TYPE_IPV4:
111 m->packet_type = RTE_PTYPE_L2_ETHER |
114 case DPAA_PKT_TYPE_IPV6:
115 m->packet_type = RTE_PTYPE_L2_ETHER |
118 case DPAA_PKT_TYPE_IPV4_FRAG:
119 case DPAA_PKT_TYPE_IPV4_FRAG_UDP:
120 case DPAA_PKT_TYPE_IPV4_FRAG_TCP:
121 case DPAA_PKT_TYPE_IPV4_FRAG_SCTP:
122 m->packet_type = RTE_PTYPE_L2_ETHER |
123 RTE_PTYPE_L3_IPV4 | RTE_PTYPE_L4_FRAG;
125 case DPAA_PKT_TYPE_IPV6_FRAG:
126 case DPAA_PKT_TYPE_IPV6_FRAG_UDP:
127 case DPAA_PKT_TYPE_IPV6_FRAG_TCP:
128 case DPAA_PKT_TYPE_IPV6_FRAG_SCTP:
129 m->packet_type = RTE_PTYPE_L2_ETHER |
130 RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_FRAG;
132 case DPAA_PKT_TYPE_IPV4_EXT:
133 m->packet_type = RTE_PTYPE_L2_ETHER |
134 RTE_PTYPE_L3_IPV4_EXT;
136 case DPAA_PKT_TYPE_IPV6_EXT:
137 m->packet_type = RTE_PTYPE_L2_ETHER |
138 RTE_PTYPE_L3_IPV6_EXT;
140 case DPAA_PKT_TYPE_IPV4_TCP:
141 m->packet_type = RTE_PTYPE_L2_ETHER |
142 RTE_PTYPE_L3_IPV4 | RTE_PTYPE_L4_TCP;
144 case DPAA_PKT_TYPE_IPV6_TCP:
145 m->packet_type = RTE_PTYPE_L2_ETHER |
146 RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_TCP;
148 case DPAA_PKT_TYPE_IPV4_UDP:
149 m->packet_type = RTE_PTYPE_L2_ETHER |
150 RTE_PTYPE_L3_IPV4 | RTE_PTYPE_L4_UDP;
152 case DPAA_PKT_TYPE_IPV6_UDP:
153 m->packet_type = RTE_PTYPE_L2_ETHER |
154 RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_UDP;
156 case DPAA_PKT_TYPE_IPV4_EXT_UDP:
157 m->packet_type = RTE_PTYPE_L2_ETHER |
158 RTE_PTYPE_L3_IPV4_EXT | RTE_PTYPE_L4_UDP;
160 case DPAA_PKT_TYPE_IPV6_EXT_UDP:
161 m->packet_type = RTE_PTYPE_L2_ETHER |
162 RTE_PTYPE_L3_IPV6_EXT | RTE_PTYPE_L4_UDP;
164 case DPAA_PKT_TYPE_IPV4_EXT_TCP:
165 m->packet_type = RTE_PTYPE_L2_ETHER |
166 RTE_PTYPE_L3_IPV4_EXT | RTE_PTYPE_L4_TCP;
168 case DPAA_PKT_TYPE_IPV6_EXT_TCP:
169 m->packet_type = RTE_PTYPE_L2_ETHER |
170 RTE_PTYPE_L3_IPV6_EXT | RTE_PTYPE_L4_TCP;
172 case DPAA_PKT_TYPE_IPV4_SCTP:
173 m->packet_type = RTE_PTYPE_L2_ETHER |
174 RTE_PTYPE_L3_IPV4 | RTE_PTYPE_L4_SCTP;
176 case DPAA_PKT_TYPE_IPV6_SCTP:
177 m->packet_type = RTE_PTYPE_L2_ETHER |
178 RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_SCTP;
180 /* More switch cases can be added */
182 dpaa_slow_parsing(m, prs);
185 m->tx_offload = annot->parse.ip_off[0];
186 m->tx_offload |= (annot->parse.l4_off - annot->parse.ip_off[0])
187 << DPAA_PKT_L3_LEN_SHIFT;
189 /* Set the hash values */
190 m->hash.rss = (uint32_t)(rte_be_to_cpu_64(annot->hash));
191 m->ol_flags = PKT_RX_RSS_HASH;
192 /* All packets with Bad checksum are dropped by interface (and
193 * corresponding notification issued to RX error queues).
195 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
197 /* Check if Vlan is present */
198 if (prs & DPAA_PARSE_VLAN_MASK)
199 m->ol_flags |= PKT_RX_VLAN_PKT;
200 /* Packet received without stripping the vlan */
203 static inline struct rte_mbuf *dpaa_eth_fd_to_mbuf(struct qm_fd *fd,
206 struct dpaa_bp_info *bp_info = DPAA_BPID_TO_POOL_INFO(fd->bpid);
207 struct rte_mbuf *mbuf;
210 (fd->opaque & DPAA_FD_OFFSET_MASK) >> DPAA_FD_OFFSET_SHIFT;
211 uint32_t length = fd->opaque & DPAA_FD_LENGTH_MASK;
213 DPAA_DP_LOG(DEBUG, " FD--->MBUF");
215 /* Ignoring case when format != qm_fd_contig */
216 ptr = rte_dpaa_mem_ptov(fd->addr);
217 /* Ignoring case when ptr would be NULL. That is only possible incase
218 * of a corrupted packet
221 mbuf = (struct rte_mbuf *)((char *)ptr - bp_info->meta_data_size);
222 /* Prefetch the Parse results and packet data to L1 */
223 rte_prefetch0((void *)((uint8_t *)ptr + DEFAULT_RX_ICEOF));
224 rte_prefetch0((void *)((uint8_t *)ptr + offset));
226 mbuf->data_off = offset;
227 mbuf->data_len = length;
228 mbuf->pkt_len = length;
234 rte_mbuf_refcnt_set(mbuf, 1);
235 dpaa_eth_packet_info(mbuf, (uint64_t)mbuf->buf_addr);
240 uint16_t dpaa_eth_queue_rx(void *q,
241 struct rte_mbuf **bufs,
244 struct qman_fq *fq = q;
245 struct qm_dqrr_entry *dq;
246 uint32_t num_rx = 0, ifid = ((struct dpaa_if *)fq->dpaa_intf)->ifid;
249 ret = rte_dpaa_portal_init((void *)0);
251 DPAA_PMD_ERR("Failure in affining portal");
255 ret = qman_set_vdq(fq, (nb_bufs > DPAA_MAX_DEQUEUE_NUM_FRAMES) ?
256 DPAA_MAX_DEQUEUE_NUM_FRAMES : nb_bufs);
261 dq = qman_dequeue(fq);
264 bufs[num_rx++] = dpaa_eth_fd_to_mbuf(&dq->fd, ifid);
265 qman_dqrr_consume(fq, dq);
266 } while (fq->flags & QMAN_FQ_STATE_VDQCR);
271 static void *dpaa_get_pktbuf(struct dpaa_bp_info *bp_info)
275 struct bm_buffer bufs;
277 ret = bman_acquire(bp_info->bp, &bufs, 1, 0);
279 DPAA_PMD_WARN("Failed to allocate buffers %d", ret);
283 DPAA_DP_LOG(DEBUG, "got buffer 0x%lx from pool %d",
284 (uint64_t)bufs.addr, bufs.bpid);
286 buf = (uint64_t)rte_dpaa_mem_ptov(bufs.addr) - bp_info->meta_data_size;
294 static struct rte_mbuf *dpaa_get_dmable_mbuf(struct rte_mbuf *mbuf,
295 struct dpaa_if *dpaa_intf)
297 struct rte_mbuf *dpaa_mbuf;
299 /* allocate pktbuffer on bpid for dpaa port */
300 dpaa_mbuf = dpaa_get_pktbuf(dpaa_intf->bp_info);
304 memcpy((uint8_t *)(dpaa_mbuf->buf_addr) + mbuf->data_off, (void *)
305 ((uint8_t *)(mbuf->buf_addr) + mbuf->data_off), mbuf->pkt_len);
307 /* Copy only the required fields */
308 dpaa_mbuf->data_off = mbuf->data_off;
309 dpaa_mbuf->pkt_len = mbuf->pkt_len;
310 dpaa_mbuf->ol_flags = mbuf->ol_flags;
311 dpaa_mbuf->packet_type = mbuf->packet_type;
312 dpaa_mbuf->tx_offload = mbuf->tx_offload;
313 rte_pktmbuf_free(mbuf);
317 /* Handle mbufs which are not segmented (non SG) */
319 tx_on_dpaa_pool_unsegmented(struct rte_mbuf *mbuf,
320 struct dpaa_bp_info *bp_info,
321 struct qm_fd *fd_arr)
323 struct rte_mbuf *mi = NULL;
325 if (RTE_MBUF_DIRECT(mbuf)) {
326 if (rte_mbuf_refcnt_read(mbuf) > 1) {
327 /* In case of direct mbuf and mbuf being cloned,
328 * BMAN should _not_ release buffer.
330 DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, 0xff);
331 /* Buffer should be releasd by EAL */
332 rte_mbuf_refcnt_update(mbuf, -1);
334 /* In case of direct mbuf and no cloning, mbuf can be
337 DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, bp_info->bpid);
340 /* This is data-containing core mbuf: 'mi' */
341 mi = rte_mbuf_from_indirect(mbuf);
342 if (rte_mbuf_refcnt_read(mi) > 1) {
343 /* In case of indirect mbuf, and mbuf being cloned,
344 * BMAN should _not_ release it and let EAL release
345 * it through pktmbuf_free below.
347 DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, 0xff);
349 /* In case of indirect mbuf, and no cloning, core mbuf
350 * should be released by BMAN.
351 * Increate refcnt of core mbuf so that when
352 * pktmbuf_free is called and mbuf is released, EAL
353 * doesn't try to release core mbuf which would have
354 * been released by BMAN.
356 rte_mbuf_refcnt_update(mi, 1);
357 DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, bp_info->bpid);
359 rte_pktmbuf_free(mbuf);
363 /* Handle all mbufs on dpaa BMAN managed pool */
364 static inline uint16_t
365 tx_on_dpaa_pool(struct rte_mbuf *mbuf,
366 struct dpaa_bp_info *bp_info,
367 struct qm_fd *fd_arr)
369 DPAA_DP_LOG(DEBUG, "BMAN offloaded buffer, mbuf: %p", mbuf);
371 if (mbuf->nb_segs == 1) {
372 /* Case for non-segmented buffers */
373 tx_on_dpaa_pool_unsegmented(mbuf, bp_info, fd_arr);
375 DPAA_PMD_DEBUG("Number of Segments not supported");
382 /* Handle all mbufs on an external pool (non-dpaa) */
383 static inline uint16_t
384 tx_on_external_pool(struct qman_fq *txq, struct rte_mbuf *mbuf,
385 struct qm_fd *fd_arr)
387 struct dpaa_if *dpaa_intf = txq->dpaa_intf;
388 struct rte_mbuf *dmable_mbuf;
390 DPAA_DP_LOG(DEBUG, "Non-BMAN offloaded buffer."
391 "Allocating an offloaded buffer");
392 dmable_mbuf = dpaa_get_dmable_mbuf(mbuf, dpaa_intf);
394 DPAA_DP_LOG(DEBUG, "no dpaa buffers.");
398 DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, dpaa_intf->bp_info->bpid);
404 dpaa_eth_queue_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
406 struct rte_mbuf *mbuf, *mi = NULL;
407 struct rte_mempool *mp;
408 struct dpaa_bp_info *bp_info;
409 struct qm_fd fd_arr[MAX_TX_RING_SLOTS];
410 uint32_t frames_to_send, loop, i = 0;
414 ret = rte_dpaa_portal_init((void *)0);
416 DPAA_PMD_ERR("Failure in affining portal");
420 DPAA_DP_LOG(DEBUG, "Transmitting %d buffers on queue: %p", nb_bufs, q);
423 frames_to_send = (nb_bufs >> 3) ? MAX_TX_RING_SLOTS : nb_bufs;
424 for (loop = 0; loop < frames_to_send; loop++, i++) {
426 if (RTE_MBUF_DIRECT(mbuf)) {
429 mi = rte_mbuf_from_indirect(mbuf);
433 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
434 if (likely(mp->ops_index == bp_info->dpaa_ops_index)) {
435 state = tx_on_dpaa_pool(mbuf, bp_info,
437 if (unlikely(state)) {
438 /* Set frames_to_send & nb_bufs so
439 * that packets are transmitted till
442 frames_to_send = loop;
447 state = tx_on_external_pool(q, mbuf,
449 if (unlikely(state)) {
450 /* Set frames_to_send & nb_bufs so
451 * that packets are transmitted till
454 frames_to_send = loop;
463 while (loop < frames_to_send) {
464 loop += qman_enqueue_multi(q, &fd_arr[loop],
465 frames_to_send - loop);
467 nb_bufs -= frames_to_send;
470 DPAA_DP_LOG(DEBUG, "Transmitted %d buffers on queue: %p", i, q);
475 uint16_t dpaa_eth_tx_drop_all(void *q __rte_unused,
476 struct rte_mbuf **bufs __rte_unused,
477 uint16_t nb_bufs __rte_unused)
479 DPAA_DP_LOG(DEBUG, "Drop all packets");
481 /* Drop all incoming packets. No need to free packets here
482 * because the rte_eth f/w frees up the packets through tx_buffer
483 * callback in case this functions returns count less than nb_bufs