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 void dpaa_checksum(struct rte_mbuf *mbuf)
205 struct ether_hdr *eth_hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
206 char *l3_hdr = (char *)eth_hdr + mbuf->l2_len;
207 struct ipv4_hdr *ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
208 struct ipv6_hdr *ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
210 DPAA_DP_LOG(DEBUG, "Calculating checksum for mbuf: %p", mbuf);
212 if (((mbuf->packet_type & RTE_PTYPE_L3_MASK) == RTE_PTYPE_L3_IPV4) ||
213 ((mbuf->packet_type & RTE_PTYPE_L3_MASK) ==
214 RTE_PTYPE_L3_IPV4_EXT)) {
215 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
216 ipv4_hdr->hdr_checksum = 0;
217 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
218 } else if (((mbuf->packet_type & RTE_PTYPE_L3_MASK) ==
219 RTE_PTYPE_L3_IPV6) ||
220 ((mbuf->packet_type & RTE_PTYPE_L3_MASK) ==
221 RTE_PTYPE_L3_IPV6_EXT))
222 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
224 if ((mbuf->packet_type & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP) {
225 struct tcp_hdr *tcp_hdr = (struct tcp_hdr *)(l3_hdr +
228 if (eth_hdr->ether_type == htons(ETHER_TYPE_IPv4))
229 tcp_hdr->cksum = rte_ipv4_udptcp_cksum(ipv4_hdr,
231 else /* assume ethertype == ETHER_TYPE_IPv6 */
232 tcp_hdr->cksum = rte_ipv6_udptcp_cksum(ipv6_hdr,
234 } else if ((mbuf->packet_type & RTE_PTYPE_L4_MASK) ==
236 struct udp_hdr *udp_hdr = (struct udp_hdr *)(l3_hdr +
238 udp_hdr->dgram_cksum = 0;
239 if (eth_hdr->ether_type == htons(ETHER_TYPE_IPv4))
240 udp_hdr->dgram_cksum = rte_ipv4_udptcp_cksum(ipv4_hdr,
242 else /* assume ethertype == ETHER_TYPE_IPv6 */
243 udp_hdr->dgram_cksum = rte_ipv6_udptcp_cksum(ipv6_hdr,
248 static inline void dpaa_checksum_offload(struct rte_mbuf *mbuf,
249 struct qm_fd *fd, char *prs_buf)
251 struct dpaa_eth_parse_results_t *prs;
253 DPAA_DP_LOG(DEBUG, " Offloading checksum for mbuf: %p", mbuf);
255 prs = GET_TX_PRS(prs_buf);
258 if (((mbuf->packet_type & RTE_PTYPE_L3_MASK) == RTE_PTYPE_L3_IPV4) ||
259 ((mbuf->packet_type & RTE_PTYPE_L3_MASK) ==
260 RTE_PTYPE_L3_IPV4_EXT))
261 prs->l3r = DPAA_L3_PARSE_RESULT_IPV4;
262 else if (((mbuf->packet_type & RTE_PTYPE_L3_MASK) ==
263 RTE_PTYPE_L3_IPV6) ||
264 ((mbuf->packet_type & RTE_PTYPE_L3_MASK) ==
265 RTE_PTYPE_L3_IPV6_EXT))
266 prs->l3r = DPAA_L3_PARSE_RESULT_IPV6;
268 if ((mbuf->packet_type & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP)
269 prs->l4r = DPAA_L4_PARSE_RESULT_TCP;
270 else if ((mbuf->packet_type & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP)
271 prs->l4r = DPAA_L4_PARSE_RESULT_UDP;
273 prs->ip_off[0] = mbuf->l2_len;
274 prs->l4_off = mbuf->l3_len + mbuf->l2_len;
275 /* Enable L3 (and L4, if TCP or UDP) HW checksum*/
276 fd->cmd = DPAA_FD_CMD_RPD | DPAA_FD_CMD_DTC;
279 static inline struct rte_mbuf *dpaa_eth_fd_to_mbuf(struct qm_fd *fd,
282 struct dpaa_bp_info *bp_info = DPAA_BPID_TO_POOL_INFO(fd->bpid);
283 struct rte_mbuf *mbuf;
286 (fd->opaque & DPAA_FD_OFFSET_MASK) >> DPAA_FD_OFFSET_SHIFT;
287 uint32_t length = fd->opaque & DPAA_FD_LENGTH_MASK;
289 DPAA_DP_LOG(DEBUG, " FD--->MBUF");
291 /* Ignoring case when format != qm_fd_contig */
292 ptr = rte_dpaa_mem_ptov(fd->addr);
293 /* Ignoring case when ptr would be NULL. That is only possible incase
294 * of a corrupted packet
297 mbuf = (struct rte_mbuf *)((char *)ptr - bp_info->meta_data_size);
298 /* Prefetch the Parse results and packet data to L1 */
299 rte_prefetch0((void *)((uint8_t *)ptr + DEFAULT_RX_ICEOF));
300 rte_prefetch0((void *)((uint8_t *)ptr + offset));
302 mbuf->data_off = offset;
303 mbuf->data_len = length;
304 mbuf->pkt_len = length;
310 rte_mbuf_refcnt_set(mbuf, 1);
311 dpaa_eth_packet_info(mbuf, (uint64_t)mbuf->buf_addr);
316 uint16_t dpaa_eth_queue_rx(void *q,
317 struct rte_mbuf **bufs,
320 struct qman_fq *fq = q;
321 struct qm_dqrr_entry *dq;
322 uint32_t num_rx = 0, ifid = ((struct dpaa_if *)fq->dpaa_intf)->ifid;
325 ret = rte_dpaa_portal_init((void *)0);
327 DPAA_PMD_ERR("Failure in affining portal");
331 ret = qman_set_vdq(fq, (nb_bufs > DPAA_MAX_DEQUEUE_NUM_FRAMES) ?
332 DPAA_MAX_DEQUEUE_NUM_FRAMES : nb_bufs);
337 dq = qman_dequeue(fq);
340 bufs[num_rx++] = dpaa_eth_fd_to_mbuf(&dq->fd, ifid);
341 qman_dqrr_consume(fq, dq);
342 } while (fq->flags & QMAN_FQ_STATE_VDQCR);
347 static void *dpaa_get_pktbuf(struct dpaa_bp_info *bp_info)
351 struct bm_buffer bufs;
353 ret = bman_acquire(bp_info->bp, &bufs, 1, 0);
355 DPAA_PMD_WARN("Failed to allocate buffers %d", ret);
359 DPAA_DP_LOG(DEBUG, "got buffer 0x%lx from pool %d",
360 (uint64_t)bufs.addr, bufs.bpid);
362 buf = (uint64_t)rte_dpaa_mem_ptov(bufs.addr) - bp_info->meta_data_size;
370 static struct rte_mbuf *dpaa_get_dmable_mbuf(struct rte_mbuf *mbuf,
371 struct dpaa_if *dpaa_intf)
373 struct rte_mbuf *dpaa_mbuf;
375 /* allocate pktbuffer on bpid for dpaa port */
376 dpaa_mbuf = dpaa_get_pktbuf(dpaa_intf->bp_info);
380 memcpy((uint8_t *)(dpaa_mbuf->buf_addr) + mbuf->data_off, (void *)
381 ((uint8_t *)(mbuf->buf_addr) + mbuf->data_off), mbuf->pkt_len);
383 /* Copy only the required fields */
384 dpaa_mbuf->data_off = mbuf->data_off;
385 dpaa_mbuf->pkt_len = mbuf->pkt_len;
386 dpaa_mbuf->ol_flags = mbuf->ol_flags;
387 dpaa_mbuf->packet_type = mbuf->packet_type;
388 dpaa_mbuf->tx_offload = mbuf->tx_offload;
389 rte_pktmbuf_free(mbuf);
393 /* Handle mbufs which are not segmented (non SG) */
395 tx_on_dpaa_pool_unsegmented(struct rte_mbuf *mbuf,
396 struct dpaa_bp_info *bp_info,
397 struct qm_fd *fd_arr)
399 struct rte_mbuf *mi = NULL;
401 if (RTE_MBUF_DIRECT(mbuf)) {
402 if (rte_mbuf_refcnt_read(mbuf) > 1) {
403 /* In case of direct mbuf and mbuf being cloned,
404 * BMAN should _not_ release buffer.
406 DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, 0xff);
407 /* Buffer should be releasd by EAL */
408 rte_mbuf_refcnt_update(mbuf, -1);
410 /* In case of direct mbuf and no cloning, mbuf can be
413 DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, bp_info->bpid);
416 /* This is data-containing core mbuf: 'mi' */
417 mi = rte_mbuf_from_indirect(mbuf);
418 if (rte_mbuf_refcnt_read(mi) > 1) {
419 /* In case of indirect mbuf, and mbuf being cloned,
420 * BMAN should _not_ release it and let EAL release
421 * it through pktmbuf_free below.
423 DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, 0xff);
425 /* In case of indirect mbuf, and no cloning, core mbuf
426 * should be released by BMAN.
427 * Increate refcnt of core mbuf so that when
428 * pktmbuf_free is called and mbuf is released, EAL
429 * doesn't try to release core mbuf which would have
430 * been released by BMAN.
432 rte_mbuf_refcnt_update(mi, 1);
433 DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, bp_info->bpid);
435 rte_pktmbuf_free(mbuf);
438 if (mbuf->ol_flags & DPAA_TX_CKSUM_OFFLOAD_MASK) {
439 if (mbuf->data_off < (DEFAULT_TX_ICEOF +
440 sizeof(struct dpaa_eth_parse_results_t))) {
441 DPAA_DP_LOG(DEBUG, "Checksum offload Err: "
442 "Not enough Headroom "
443 "space for correct Checksum offload."
444 "So Calculating checksum in Software.");
447 dpaa_checksum_offload(mbuf, fd_arr, mbuf->buf_addr);
452 /* Handle all mbufs on dpaa BMAN managed pool */
453 static inline uint16_t
454 tx_on_dpaa_pool(struct rte_mbuf *mbuf,
455 struct dpaa_bp_info *bp_info,
456 struct qm_fd *fd_arr)
458 DPAA_DP_LOG(DEBUG, "BMAN offloaded buffer, mbuf: %p", mbuf);
460 if (mbuf->nb_segs == 1) {
461 /* Case for non-segmented buffers */
462 tx_on_dpaa_pool_unsegmented(mbuf, bp_info, fd_arr);
464 DPAA_PMD_DEBUG("Number of Segments not supported");
471 /* Handle all mbufs on an external pool (non-dpaa) */
472 static inline uint16_t
473 tx_on_external_pool(struct qman_fq *txq, struct rte_mbuf *mbuf,
474 struct qm_fd *fd_arr)
476 struct dpaa_if *dpaa_intf = txq->dpaa_intf;
477 struct rte_mbuf *dmable_mbuf;
479 DPAA_DP_LOG(DEBUG, "Non-BMAN offloaded buffer."
480 "Allocating an offloaded buffer");
481 dmable_mbuf = dpaa_get_dmable_mbuf(mbuf, dpaa_intf);
483 DPAA_DP_LOG(DEBUG, "no dpaa buffers.");
487 DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, dpaa_intf->bp_info->bpid);
493 dpaa_eth_queue_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
495 struct rte_mbuf *mbuf, *mi = NULL;
496 struct rte_mempool *mp;
497 struct dpaa_bp_info *bp_info;
498 struct qm_fd fd_arr[MAX_TX_RING_SLOTS];
499 uint32_t frames_to_send, loop, i = 0;
503 ret = rte_dpaa_portal_init((void *)0);
505 DPAA_PMD_ERR("Failure in affining portal");
509 DPAA_DP_LOG(DEBUG, "Transmitting %d buffers on queue: %p", nb_bufs, q);
512 frames_to_send = (nb_bufs >> 3) ? MAX_TX_RING_SLOTS : nb_bufs;
513 for (loop = 0; loop < frames_to_send; loop++, i++) {
515 if (RTE_MBUF_DIRECT(mbuf)) {
518 mi = rte_mbuf_from_indirect(mbuf);
522 bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
523 if (likely(mp->ops_index == bp_info->dpaa_ops_index)) {
524 state = tx_on_dpaa_pool(mbuf, bp_info,
526 if (unlikely(state)) {
527 /* Set frames_to_send & nb_bufs so
528 * that packets are transmitted till
531 frames_to_send = loop;
536 state = tx_on_external_pool(q, mbuf,
538 if (unlikely(state)) {
539 /* Set frames_to_send & nb_bufs so
540 * that packets are transmitted till
543 frames_to_send = loop;
552 while (loop < frames_to_send) {
553 loop += qman_enqueue_multi(q, &fd_arr[loop],
554 frames_to_send - loop);
556 nb_bufs -= frames_to_send;
559 DPAA_DP_LOG(DEBUG, "Transmitted %d buffers on queue: %p", i, q);
564 uint16_t dpaa_eth_tx_drop_all(void *q __rte_unused,
565 struct rte_mbuf **bufs __rte_unused,
566 uint16_t nb_bufs __rte_unused)
568 DPAA_DP_LOG(DEBUG, "Drop all packets");
570 /* Drop all incoming packets. No need to free packets here
571 * because the rte_eth f/w frees up the packets through tx_buffer
572 * callback in case this functions returns count less than nb_bufs