net/dpaa2: enable Rx and Tx operations
[dpdk.git] / drivers / net / dpaa2 / dpaa2_rxtx.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
5  *   Copyright (c) 2016 NXP. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <time.h>
35 #include <net/if.h>
36
37 #include <rte_mbuf.h>
38 #include <rte_ethdev.h>
39 #include <rte_malloc.h>
40 #include <rte_memcpy.h>
41 #include <rte_string_fns.h>
42 #include <rte_dev.h>
43 #include <rte_ethdev.h>
44
45 #include <fslmc_logs.h>
46 #include <fslmc_vfio.h>
47 #include <dpaa2_hw_pvt.h>
48 #include <dpaa2_hw_dpio.h>
49 #include <dpaa2_hw_mempool.h>
50
51 #include "dpaa2_ethdev.h"
52
53 static inline struct rte_mbuf *__attribute__((hot))
54 eth_fd_to_mbuf(const struct qbman_fd *fd)
55 {
56         struct rte_mbuf *mbuf = DPAA2_INLINE_MBUF_FROM_BUF(
57                         DPAA2_GET_FD_ADDR(fd),
58                      rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size);
59
60         /* need to repopulated some of the fields,
61          * as they may have changed in last transmission
62          */
63         mbuf->nb_segs = 1;
64         mbuf->ol_flags = 0;
65         mbuf->data_off = DPAA2_GET_FD_OFFSET(fd);
66         mbuf->data_len = DPAA2_GET_FD_LEN(fd);
67         mbuf->pkt_len = mbuf->data_len;
68
69         mbuf->packet_type = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4;
70
71         mbuf->next = NULL;
72         rte_mbuf_refcnt_set(mbuf, 1);
73
74         PMD_RX_LOG(DEBUG, "to mbuf - mbuf =%p, mbuf->buf_addr =%p, off = %d,"
75                 "fd_off=%d fd =%lx, meta = %d  bpid =%d, len=%d\n",
76                 mbuf, mbuf->buf_addr, mbuf->data_off,
77                 DPAA2_GET_FD_OFFSET(fd), DPAA2_GET_FD_ADDR(fd),
78                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size,
79                 DPAA2_GET_FD_BPID(fd), DPAA2_GET_FD_LEN(fd));
80
81         return mbuf;
82 }
83
84 static void __attribute__ ((noinline)) __attribute__((hot))
85 eth_mbuf_to_fd(struct rte_mbuf *mbuf,
86                struct qbman_fd *fd, uint16_t bpid)
87 {
88         /*Resetting the buffer pool id and offset field*/
89         fd->simple.bpid_offset = 0;
90
91         DPAA2_SET_FD_ADDR(fd, (mbuf->buf_addr));
92         DPAA2_SET_FD_LEN(fd, mbuf->data_len);
93         DPAA2_SET_FD_BPID(fd, bpid);
94         DPAA2_SET_FD_OFFSET(fd, mbuf->data_off);
95         DPAA2_SET_FD_ASAL(fd, DPAA2_ASAL_VAL);
96
97         PMD_TX_LOG(DEBUG, "mbuf =%p, mbuf->buf_addr =%p, off = %d,"
98                 "fd_off=%d fd =%lx, meta = %d  bpid =%d, len=%d\n",
99                 mbuf, mbuf->buf_addr, mbuf->data_off,
100                 DPAA2_GET_FD_OFFSET(fd), DPAA2_GET_FD_ADDR(fd),
101                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size,
102                 DPAA2_GET_FD_BPID(fd), DPAA2_GET_FD_LEN(fd));
103 }
104
105 uint16_t
106 dpaa2_dev_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
107 {
108         /* Function is responsible to receive frames for a given device and VQ*/
109         struct dpaa2_queue *dpaa2_q = (struct dpaa2_queue *)queue;
110         struct qbman_result *dq_storage;
111         uint32_t fqid = dpaa2_q->fqid;
112         int ret, num_rx = 0;
113         uint8_t is_last = 0, status;
114         struct qbman_swp *swp;
115         const struct qbman_fd *fd;
116         struct qbman_pull_desc pulldesc;
117         struct rte_eth_dev *dev = dpaa2_q->dev;
118
119         if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
120                 ret = dpaa2_affine_qbman_swp();
121                 if (ret) {
122                         RTE_LOG(ERR, PMD, "Failure in affining portal\n");
123                         return 0;
124                 }
125         }
126         swp = DPAA2_PER_LCORE_PORTAL;
127         dq_storage = dpaa2_q->q_storage->dq_storage[0];
128
129         qbman_pull_desc_clear(&pulldesc);
130         qbman_pull_desc_set_numframes(&pulldesc,
131                                       (nb_pkts > DPAA2_DQRR_RING_SIZE) ?
132                                        DPAA2_DQRR_RING_SIZE : nb_pkts);
133         qbman_pull_desc_set_fq(&pulldesc, fqid);
134         /* todo optimization - we can have dq_storage_phys available*/
135         qbman_pull_desc_set_storage(&pulldesc, dq_storage,
136                         (dma_addr_t)(dq_storage), 1);
137
138         /*Issue a volatile dequeue command. */
139         while (1) {
140                 if (qbman_swp_pull(swp, &pulldesc)) {
141                         PMD_RX_LOG(ERR, "VDQ command is not issued."
142                                    "QBMAN is busy\n");
143                         /* Portal was busy, try again */
144                         continue;
145                 }
146                 break;
147         };
148
149         /* Receive the packets till Last Dequeue entry is found with
150          * respect to the above issues PULL command.
151          */
152         while (!is_last) {
153                 struct rte_mbuf *mbuf;
154                 /*Check if the previous issued command is completed.
155                  * Also seems like the SWP is shared between the
156                  * Ethernet Driver and the SEC driver.
157                  */
158                 while (!qbman_check_command_complete(swp, dq_storage))
159                         ;
160                 /* Loop until the dq_storage is updated with
161                  * new token by QBMAN
162                  */
163                 while (!qbman_result_has_new_result(swp, dq_storage))
164                         ;
165                 /* Check whether Last Pull command is Expired and
166                  * setting Condition for Loop termination
167                  */
168                 if (qbman_result_DQ_is_pull_complete(dq_storage)) {
169                         is_last = 1;
170                         /* Check for valid frame. */
171                         status = (uint8_t)qbman_result_DQ_flags(dq_storage);
172                         if (unlikely((status & QBMAN_DQ_STAT_VALIDFRAME) == 0))
173                                 continue;
174                 }
175
176                 fd = qbman_result_DQ_fd(dq_storage);
177                 mbuf = (struct rte_mbuf *)(DPAA2_GET_FD_ADDR(fd)
178                    - rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size);
179                 /* Prefeth mbuf */
180                 rte_prefetch0(mbuf);
181                 /* Prefetch Annotation address for the parse results */
182                 rte_prefetch0((void *)((uint64_t)DPAA2_GET_FD_ADDR(fd)
183                                                 + DPAA2_FD_PTA_SIZE + 16));
184
185                 bufs[num_rx] = eth_fd_to_mbuf(fd);
186                 bufs[num_rx]->port = dev->data->port_id;
187
188                 num_rx++;
189                 dq_storage++;
190         } /* End of Packet Rx loop */
191
192         dpaa2_q->rx_pkts += num_rx;
193
194         /*Return the total number of packets received to DPAA2 app*/
195         return num_rx;
196 }
197
198 /*
199  * Callback to handle sending packets through WRIOP based interface
200  */
201 uint16_t
202 dpaa2_dev_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
203 {
204         /* Function to transmit the frames to given device and VQ*/
205         uint32_t loop;
206         int32_t ret;
207         struct qbman_fd fd_arr[MAX_TX_RING_SLOTS];
208         uint32_t frames_to_send;
209         struct rte_mempool *mp;
210         struct qbman_eq_desc eqdesc;
211         struct dpaa2_queue *dpaa2_q = (struct dpaa2_queue *)queue;
212         struct qbman_swp *swp;
213         uint16_t num_tx = 0;
214         uint16_t bpid;
215         struct rte_eth_dev *dev = dpaa2_q->dev;
216         struct dpaa2_dev_priv *priv = dev->data->dev_private;
217
218         if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
219                 ret = dpaa2_affine_qbman_swp();
220                 if (ret) {
221                         RTE_LOG(ERR, PMD, "Failure in affining portal\n");
222                         return 0;
223                 }
224         }
225         swp = DPAA2_PER_LCORE_PORTAL;
226
227         PMD_TX_LOG(DEBUG, "===> dev =%p, fqid =%d", dev, dpaa2_q->fqid);
228
229         /*Prepare enqueue descriptor*/
230         qbman_eq_desc_clear(&eqdesc);
231         qbman_eq_desc_set_no_orp(&eqdesc, DPAA2_EQ_RESP_ERR_FQ);
232         qbman_eq_desc_set_response(&eqdesc, 0, 0);
233         qbman_eq_desc_set_qd(&eqdesc, priv->qdid,
234                              dpaa2_q->flow_id, dpaa2_q->tc_index);
235
236         /*Clear the unused FD fields before sending*/
237         while (nb_pkts) {
238                 frames_to_send = (nb_pkts >> 3) ? MAX_TX_RING_SLOTS : nb_pkts;
239
240                 for (loop = 0; loop < frames_to_send; loop++) {
241                         fd_arr[loop].simple.frc = 0;
242                         DPAA2_RESET_FD_CTRL((&fd_arr[loop]));
243                         DPAA2_SET_FD_FLC((&fd_arr[loop]), NULL);
244                         mp = (*bufs)->pool;
245                         bpid = mempool_to_bpid(mp);
246                         eth_mbuf_to_fd(*bufs, &fd_arr[loop], bpid);
247                         bufs++;
248                 }
249                 loop = 0;
250                 while (loop < frames_to_send) {
251                         loop += qbman_swp_send_multiple(swp, &eqdesc,
252                                         &fd_arr[loop], frames_to_send - loop);
253                 }
254
255                 num_tx += frames_to_send;
256                 dpaa2_q->tx_pkts += frames_to_send;
257                 nb_pkts -= frames_to_send;
258         }
259         return num_tx;
260 }