net/dpaa: support Tx and Rx queue setup
[dpdk.git] / drivers / net / dpaa / dpaa_rxtx.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2016 Freescale Semiconductor, Inc. All rights reserved.
5  *   Copyright 2017 NXP.
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 /* System headers */
35 #include <stdio.h>
36 #include <inttypes.h>
37 #include <unistd.h>
38 #include <stdio.h>
39 #include <limits.h>
40 #include <sched.h>
41 #include <pthread.h>
42
43 #include <rte_config.h>
44 #include <rte_byteorder.h>
45 #include <rte_common.h>
46 #include <rte_interrupts.h>
47 #include <rte_log.h>
48 #include <rte_debug.h>
49 #include <rte_pci.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>
55 #include <rte_eal.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>
61 #include <rte_ring.h>
62 #include <rte_ip.h>
63 #include <rte_tcp.h>
64 #include <rte_udp.h>
65
66 #include "dpaa_ethdev.h"
67 #include "dpaa_rxtx.h"
68 #include <rte_dpaa_bus.h>
69 #include <dpaa_mempool.h>
70
71 #include <fsl_usd.h>
72 #include <fsl_qman.h>
73 #include <fsl_bman.h>
74 #include <of.h>
75 #include <netcfg.h>
76
77 #define DPAA_MBUF_TO_CONTIG_FD(_mbuf, _fd, _bpid) \
78         do { \
79                 (_fd)->cmd = 0; \
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; \
86         } while (0)
87
88 static inline struct rte_mbuf *dpaa_eth_fd_to_mbuf(struct qm_fd *fd,
89                                                         uint32_t ifid)
90 {
91         struct dpaa_bp_info *bp_info = DPAA_BPID_TO_POOL_INFO(fd->bpid);
92         struct rte_mbuf *mbuf;
93         void *ptr;
94         uint16_t offset =
95                 (fd->opaque & DPAA_FD_OFFSET_MASK) >> DPAA_FD_OFFSET_SHIFT;
96         uint32_t length = fd->opaque & DPAA_FD_LENGTH_MASK;
97
98         DPAA_DP_LOG(DEBUG, " FD--->MBUF");
99
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
104          */
105
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));
110
111         mbuf->data_off = offset;
112         mbuf->data_len = length;
113         mbuf->pkt_len = length;
114
115         mbuf->port = ifid;
116         mbuf->nb_segs = 1;
117         mbuf->ol_flags = 0;
118         mbuf->next = NULL;
119         rte_mbuf_refcnt_set(mbuf, 1);
120
121         return mbuf;
122 }
123
124 uint16_t dpaa_eth_queue_rx(void *q,
125                            struct rte_mbuf **bufs,
126                            uint16_t nb_bufs)
127 {
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;
131         int ret;
132
133         ret = rte_dpaa_portal_init((void *)0);
134         if (ret) {
135                 DPAA_PMD_ERR("Failure in affining portal");
136                 return 0;
137         }
138
139         ret = qman_set_vdq(fq, (nb_bufs > DPAA_MAX_DEQUEUE_NUM_FRAMES) ?
140                                 DPAA_MAX_DEQUEUE_NUM_FRAMES : nb_bufs);
141         if (ret)
142                 return 0;
143
144         do {
145                 dq = qman_dequeue(fq);
146                 if (!dq)
147                         continue;
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);
151
152         return num_rx;
153 }
154
155 static void *dpaa_get_pktbuf(struct dpaa_bp_info *bp_info)
156 {
157         int ret;
158         uint64_t buf = 0;
159         struct bm_buffer bufs;
160
161         ret = bman_acquire(bp_info->bp, &bufs, 1, 0);
162         if (ret <= 0) {
163                 DPAA_PMD_WARN("Failed to allocate buffers %d", ret);
164                 return (void *)buf;
165         }
166
167         DPAA_DP_LOG(DEBUG, "got buffer 0x%lx from pool %d",
168                     (uint64_t)bufs.addr, bufs.bpid);
169
170         buf = (uint64_t)rte_dpaa_mem_ptov(bufs.addr) - bp_info->meta_data_size;
171         if (!buf)
172                 goto out;
173
174 out:
175         return (void *)buf;
176 }
177
178 static struct rte_mbuf *dpaa_get_dmable_mbuf(struct rte_mbuf *mbuf,
179                                              struct dpaa_if *dpaa_intf)
180 {
181         struct rte_mbuf *dpaa_mbuf;
182
183         /* allocate pktbuffer on bpid for dpaa port */
184         dpaa_mbuf = dpaa_get_pktbuf(dpaa_intf->bp_info);
185         if (!dpaa_mbuf)
186                 return NULL;
187
188         memcpy((uint8_t *)(dpaa_mbuf->buf_addr) + mbuf->data_off, (void *)
189                 ((uint8_t *)(mbuf->buf_addr) + mbuf->data_off), mbuf->pkt_len);
190
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);
198         return dpaa_mbuf;
199 }
200
201 /* Handle mbufs which are not segmented (non SG) */
202 static inline void
203 tx_on_dpaa_pool_unsegmented(struct rte_mbuf *mbuf,
204                             struct dpaa_bp_info *bp_info,
205                             struct qm_fd *fd_arr)
206 {
207         struct rte_mbuf *mi = NULL;
208
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.
213                          */
214                         DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, 0xff);
215                         /* Buffer should be releasd by EAL */
216                         rte_mbuf_refcnt_update(mbuf, -1);
217                 } else {
218                         /* In case of direct mbuf and no cloning, mbuf can be
219                          * released by BMAN.
220                          */
221                         DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, bp_info->bpid);
222                 }
223         } else {
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.
230                          */
231                         DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, 0xff);
232                 } else {
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.
239                          */
240                         rte_mbuf_refcnt_update(mi, 1);
241                         DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, bp_info->bpid);
242                 }
243                 rte_pktmbuf_free(mbuf);
244         }
245 }
246
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)
252 {
253         DPAA_DP_LOG(DEBUG, "BMAN offloaded buffer, mbuf: %p", mbuf);
254
255         if (mbuf->nb_segs == 1) {
256                 /* Case for non-segmented buffers */
257                 tx_on_dpaa_pool_unsegmented(mbuf, bp_info, fd_arr);
258         } else {
259                 DPAA_PMD_DEBUG("Number of Segments not supported");
260                 return 1;
261         }
262
263         return 0;
264 }
265
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)
270 {
271         struct dpaa_if *dpaa_intf = txq->dpaa_intf;
272         struct rte_mbuf *dmable_mbuf;
273
274         DPAA_DP_LOG(DEBUG, "Non-BMAN offloaded buffer."
275                     "Allocating an offloaded buffer");
276         dmable_mbuf = dpaa_get_dmable_mbuf(mbuf, dpaa_intf);
277         if (!dmable_mbuf) {
278                 DPAA_DP_LOG(DEBUG, "no dpaa buffers.");
279                 return 1;
280         }
281
282         DPAA_MBUF_TO_CONTIG_FD(mbuf, fd_arr, dpaa_intf->bp_info->bpid);
283
284         return 0;
285 }
286
287 uint16_t
288 dpaa_eth_queue_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
289 {
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;
295         uint16_t state;
296         int ret;
297
298         ret = rte_dpaa_portal_init((void *)0);
299         if (ret) {
300                 DPAA_PMD_ERR("Failure in affining portal");
301                 return 0;
302         }
303
304         DPAA_DP_LOG(DEBUG, "Transmitting %d buffers on queue: %p", nb_bufs, q);
305
306         while (nb_bufs) {
307                 frames_to_send = (nb_bufs >> 3) ? MAX_TX_RING_SLOTS : nb_bufs;
308                 for (loop = 0; loop < frames_to_send; loop++, i++) {
309                         mbuf = bufs[i];
310                         if (RTE_MBUF_DIRECT(mbuf)) {
311                                 mp = mbuf->pool;
312                         } else {
313                                 mi = rte_mbuf_from_indirect(mbuf);
314                                 mp = mi->pool;
315                         }
316
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,
320                                                         &fd_arr[loop]);
321                                 if (unlikely(state)) {
322                                         /* Set frames_to_send & nb_bufs so
323                                          * that packets are transmitted till
324                                          * previous frame.
325                                          */
326                                         frames_to_send = loop;
327                                         nb_bufs = loop;
328                                         goto send_pkts;
329                                 }
330                         } else {
331                                 state = tx_on_external_pool(q, mbuf,
332                                                             &fd_arr[loop]);
333                                 if (unlikely(state)) {
334                                         /* Set frames_to_send & nb_bufs so
335                                          * that packets are transmitted till
336                                          * previous frame.
337                                          */
338                                         frames_to_send = loop;
339                                         nb_bufs = loop;
340                                         goto send_pkts;
341                                 }
342                         }
343                 }
344
345 send_pkts:
346                 loop = 0;
347                 while (loop < frames_to_send) {
348                         loop += qman_enqueue_multi(q, &fd_arr[loop],
349                                         frames_to_send - loop);
350                 }
351                 nb_bufs -= frames_to_send;
352         }
353
354         DPAA_DP_LOG(DEBUG, "Transmitted %d buffers on queue: %p", i, q);
355
356         return i;
357 }
358
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)
362 {
363         DPAA_DP_LOG(DEBUG, "Drop all packets");
364
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
368          */
369         return 0;
370 }