net/octeontx: add packet receive burst function
[dpdk.git] / drivers / net / octeontx / octeontx_rxtx.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium, Inc. 2017. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Cavium, Inc nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37
38 #include <rte_atomic.h>
39 #include <rte_common.h>
40 #include <rte_ethdev.h>
41 #include <rte_ether.h>
42 #include <rte_log.h>
43 #include <rte_mbuf.h>
44 #include <rte_prefetch.h>
45
46 #include "octeontx_ethdev.h"
47 #include "octeontx_rxtx.h"
48 #include "octeontx_logs.h"
49
50 static __rte_always_inline uint16_t __hot
51 __octeontx_xmit_pkts(void *lmtline_va, void *ioreg_va, int64_t *fc_status_va,
52                         struct rte_mbuf *tx_pkt)
53 {
54         uint64_t cmd_buf[4];
55         uint16_t gaura_id;
56
57         if (unlikely(*((volatile int64_t *)fc_status_va) < 0))
58                 return -ENOSPC;
59
60         /* Get the gaura Id */
61         gaura_id = octeontx_fpa_bufpool_gpool((uintptr_t)tx_pkt->pool->pool_id);
62
63         /* Setup PKO_SEND_HDR_S */
64         cmd_buf[0] = tx_pkt->data_len & 0xffff;
65         cmd_buf[1] = 0x0;
66
67         /* Set don't free bit if reference count > 1 */
68         if (rte_mbuf_refcnt_read(tx_pkt) > 1)
69                 cmd_buf[0] |= (1ULL << 58); /* SET DF */
70
71         /* Setup PKO_SEND_GATHER_S */
72         cmd_buf[(1 << 1) | 1] = rte_mbuf_data_dma_addr(tx_pkt);
73         cmd_buf[(1 << 1) | 0] = PKO_SEND_GATHER_SUBDC |
74                                 PKO_SEND_GATHER_LDTYPE(0x1ull) |
75                                 PKO_SEND_GATHER_GAUAR((long)gaura_id) |
76                                 tx_pkt->data_len;
77
78         octeontx_reg_lmtst(lmtline_va, ioreg_va, cmd_buf, PKO_CMD_SZ);
79
80         return 0;
81 }
82
83 uint16_t __hot
84 octeontx_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
85 {
86         int count;
87         struct octeontx_txq *txq = tx_queue;
88         octeontx_dq_t *dq = &txq->dq;
89         int res;
90
91         count = 0;
92
93         while (count < nb_pkts) {
94                 res = __octeontx_xmit_pkts(dq->lmtline_va, dq->ioreg_va,
95                                            dq->fc_status_va,
96                                            tx_pkts[count]);
97                 if (res < 0)
98                         break;
99
100                 count++;
101         }
102
103         return count; /* return number of pkts transmitted */
104 }
105
106 uint16_t __hot
107 octeontx_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
108 {
109         struct rte_mbuf *mbuf;
110         struct octeontx_rxq *rxq;
111         struct rte_event ev;
112         octtx_wqe_t *wqe;
113         size_t count;
114         uint16_t valid_event;
115
116         rxq = rx_queue;
117         count = 0;
118         while (count < nb_pkts) {
119                 valid_event = rte_event_dequeue_burst(rxq->evdev,
120                                                         rxq->ev_ports, &ev,
121                                                         1, 0);
122                 if (!valid_event)
123                         break;
124
125                 wqe = (octtx_wqe_t *)(uintptr_t)ev.u64;
126                 rte_prefetch_non_temporal(wqe);
127
128                 /* Get mbuf from wqe */
129                 mbuf = (struct rte_mbuf *)((uintptr_t)wqe -
130                                                 OCTTX_PACKET_WQE_SKIP);
131                 mbuf->data_off = RTE_PTR_DIFF(wqe->s.w3.addr, mbuf->buf_addr);
132                 mbuf->pkt_len = wqe->s.w1.len;
133                 mbuf->data_len = mbuf->pkt_len;
134                 mbuf->nb_segs = 1;
135                 mbuf->ol_flags = 0;
136                 mbuf->port = rxq->port_id;
137                 rte_mbuf_refcnt_set(mbuf, 1);
138                 rx_pkts[count++] = mbuf;
139         }
140
141         return count; /* return number of pkts received */
142 }