net/dpaa2: support more than 16 burst size in Rx
[dpdk.git] / drivers / net / dpaa2 / dpaa2_rxtx.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4  *   Copyright 2016 NXP
5  *
6  */
7
8 #include <time.h>
9 #include <net/if.h>
10
11 #include <rte_mbuf.h>
12 #include <rte_ethdev.h>
13 #include <rte_malloc.h>
14 #include <rte_memcpy.h>
15 #include <rte_string_fns.h>
16 #include <rte_dev.h>
17
18 #include <rte_fslmc.h>
19 #include <fslmc_logs.h>
20 #include <fslmc_vfio.h>
21 #include <dpaa2_hw_pvt.h>
22 #include <dpaa2_hw_dpio.h>
23 #include <dpaa2_hw_mempool.h>
24 #include <dpaa2_eventdev.h>
25
26 #include "dpaa2_ethdev.h"
27 #include "base/dpaa2_hw_dpni_annot.h"
28
29 #define DPAA2_MBUF_TO_CONTIG_FD(_mbuf, _fd, _bpid)  do { \
30         DPAA2_SET_FD_ADDR(_fd, DPAA2_MBUF_VADDR_TO_IOVA(_mbuf)); \
31         DPAA2_SET_FD_LEN(_fd, _mbuf->data_len); \
32         DPAA2_SET_ONLY_FD_BPID(_fd, _bpid); \
33         DPAA2_SET_FD_OFFSET(_fd, _mbuf->data_off); \
34         DPAA2_SET_FD_ASAL(_fd, DPAA2_ASAL_VAL); \
35 } while (0)
36
37 static inline void __attribute__((hot))
38 dpaa2_dev_rx_parse_frc(struct rte_mbuf *m, uint16_t frc)
39 {
40         PMD_RX_LOG(DEBUG, "frc = 0x%x   ", frc);
41
42         m->packet_type = RTE_PTYPE_UNKNOWN;
43         switch (frc) {
44         case DPAA2_PKT_TYPE_ETHER:
45                 m->packet_type = RTE_PTYPE_L2_ETHER;
46                 break;
47         case DPAA2_PKT_TYPE_IPV4:
48                 m->packet_type = RTE_PTYPE_L2_ETHER |
49                         RTE_PTYPE_L3_IPV4;
50                 break;
51         case DPAA2_PKT_TYPE_IPV6:
52                 m->packet_type = RTE_PTYPE_L2_ETHER |
53                         RTE_PTYPE_L3_IPV6;
54                 break;
55         case DPAA2_PKT_TYPE_IPV4_EXT:
56                 m->packet_type = RTE_PTYPE_L2_ETHER |
57                         RTE_PTYPE_L3_IPV4_EXT;
58                 break;
59         case DPAA2_PKT_TYPE_IPV6_EXT:
60                 m->packet_type = RTE_PTYPE_L2_ETHER |
61                         RTE_PTYPE_L3_IPV6_EXT;
62                 break;
63         case DPAA2_PKT_TYPE_IPV4_TCP:
64                 m->packet_type = RTE_PTYPE_L2_ETHER |
65                         RTE_PTYPE_L3_IPV4 | RTE_PTYPE_L4_TCP;
66                 break;
67         case DPAA2_PKT_TYPE_IPV6_TCP:
68                 m->packet_type = RTE_PTYPE_L2_ETHER |
69                         RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_TCP;
70                 break;
71         case DPAA2_PKT_TYPE_IPV4_UDP:
72                 m->packet_type = RTE_PTYPE_L2_ETHER |
73                         RTE_PTYPE_L3_IPV4 | RTE_PTYPE_L4_UDP;
74                 break;
75         case DPAA2_PKT_TYPE_IPV6_UDP:
76                 m->packet_type = RTE_PTYPE_L2_ETHER |
77                         RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_UDP;
78                 break;
79         case DPAA2_PKT_TYPE_IPV4_SCTP:
80                 m->packet_type = RTE_PTYPE_L2_ETHER |
81                         RTE_PTYPE_L3_IPV4 | RTE_PTYPE_L4_SCTP;
82                 break;
83         case DPAA2_PKT_TYPE_IPV6_SCTP:
84                 m->packet_type = RTE_PTYPE_L2_ETHER |
85                         RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_SCTP;
86                 break;
87         case DPAA2_PKT_TYPE_IPV4_ICMP:
88                 m->packet_type = RTE_PTYPE_L2_ETHER |
89                         RTE_PTYPE_L3_IPV4 | RTE_PTYPE_L4_ICMP;
90                 break;
91         case DPAA2_PKT_TYPE_IPV6_ICMP:
92                 m->packet_type = RTE_PTYPE_L2_ETHER |
93                         RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_ICMP;
94                 break;
95         case DPAA2_PKT_TYPE_VLAN_1:
96         case DPAA2_PKT_TYPE_VLAN_2:
97                 m->ol_flags |= PKT_RX_VLAN;
98                 break;
99         /* More switch cases can be added */
100         /* TODO: Add handling for checksum error check from FRC */
101         default:
102                 m->packet_type = RTE_PTYPE_UNKNOWN;
103         }
104 }
105
106 static inline uint32_t __attribute__((hot))
107 dpaa2_dev_rx_parse_slow(uint64_t hw_annot_addr)
108 {
109         uint32_t pkt_type = RTE_PTYPE_UNKNOWN;
110         struct dpaa2_annot_hdr *annotation =
111                         (struct dpaa2_annot_hdr *)hw_annot_addr;
112
113         PMD_RX_LOG(DEBUG, "annotation = 0x%lx   ", annotation->word4);
114         if (BIT_ISSET_AT_POS(annotation->word3, L2_ARP_PRESENT)) {
115                 pkt_type = RTE_PTYPE_L2_ETHER_ARP;
116                 goto parse_done;
117         } else if (BIT_ISSET_AT_POS(annotation->word3, L2_ETH_MAC_PRESENT)) {
118                 pkt_type = RTE_PTYPE_L2_ETHER;
119         } else {
120                 goto parse_done;
121         }
122
123         if (BIT_ISSET_AT_POS(annotation->word4, L3_IPV4_1_PRESENT |
124                              L3_IPV4_N_PRESENT)) {
125                 pkt_type |= RTE_PTYPE_L3_IPV4;
126                 if (BIT_ISSET_AT_POS(annotation->word4, L3_IP_1_OPT_PRESENT |
127                         L3_IP_N_OPT_PRESENT))
128                         pkt_type |= RTE_PTYPE_L3_IPV4_EXT;
129
130         } else if (BIT_ISSET_AT_POS(annotation->word4, L3_IPV6_1_PRESENT |
131                   L3_IPV6_N_PRESENT)) {
132                 pkt_type |= RTE_PTYPE_L3_IPV6;
133                 if (BIT_ISSET_AT_POS(annotation->word4, L3_IP_1_OPT_PRESENT |
134                     L3_IP_N_OPT_PRESENT))
135                         pkt_type |= RTE_PTYPE_L3_IPV6_EXT;
136         } else {
137                 goto parse_done;
138         }
139
140         if (BIT_ISSET_AT_POS(annotation->word4, L3_IP_1_FIRST_FRAGMENT |
141             L3_IP_1_MORE_FRAGMENT |
142             L3_IP_N_FIRST_FRAGMENT |
143             L3_IP_N_MORE_FRAGMENT)) {
144                 pkt_type |= RTE_PTYPE_L4_FRAG;
145                 goto parse_done;
146         } else {
147                 pkt_type |= RTE_PTYPE_L4_NONFRAG;
148         }
149
150         if (BIT_ISSET_AT_POS(annotation->word4, L3_PROTO_UDP_PRESENT))
151                 pkt_type |= RTE_PTYPE_L4_UDP;
152
153         else if (BIT_ISSET_AT_POS(annotation->word4, L3_PROTO_TCP_PRESENT))
154                 pkt_type |= RTE_PTYPE_L4_TCP;
155
156         else if (BIT_ISSET_AT_POS(annotation->word4, L3_PROTO_SCTP_PRESENT))
157                 pkt_type |= RTE_PTYPE_L4_SCTP;
158
159         else if (BIT_ISSET_AT_POS(annotation->word4, L3_PROTO_ICMP_PRESENT))
160                 pkt_type |= RTE_PTYPE_L4_ICMP;
161
162         else if (BIT_ISSET_AT_POS(annotation->word4, L3_IP_UNKNOWN_PROTOCOL))
163                 pkt_type |= RTE_PTYPE_UNKNOWN;
164
165 parse_done:
166         return pkt_type;
167 }
168
169
170 static inline uint32_t __attribute__((hot))
171 dpaa2_dev_rx_parse(uint64_t hw_annot_addr)
172 {
173         struct dpaa2_annot_hdr *annotation =
174                         (struct dpaa2_annot_hdr *)hw_annot_addr;
175
176         PMD_RX_LOG(DEBUG, "annotation = 0x%lx   ", annotation->word4);
177
178         /* Return some common types from parse processing */
179         switch (annotation->word4) {
180         case DPAA2_L3_IPv4:
181                 return RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4;
182         case DPAA2_L3_IPv6:
183                 return  RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6;
184         case DPAA2_L3_IPv4_TCP:
185                 return  RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4 |
186                                 RTE_PTYPE_L4_TCP;
187         case DPAA2_L3_IPv4_UDP:
188                 return  RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4 |
189                                 RTE_PTYPE_L4_UDP;
190         case DPAA2_L3_IPv6_TCP:
191                 return  RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6 |
192                                 RTE_PTYPE_L4_TCP;
193         case DPAA2_L3_IPv6_UDP:
194                 return  RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6 |
195                                 RTE_PTYPE_L4_UDP;
196         default:
197                 PMD_RX_LOG(DEBUG, "Slow parse the parsing results\n");
198                 break;
199         }
200
201         return dpaa2_dev_rx_parse_slow(hw_annot_addr);
202 }
203
204 static inline void __attribute__((hot))
205 dpaa2_dev_rx_offload(uint64_t hw_annot_addr, struct rte_mbuf *mbuf)
206 {
207         struct dpaa2_annot_hdr *annotation =
208                 (struct dpaa2_annot_hdr *)hw_annot_addr;
209
210         if (BIT_ISSET_AT_POS(annotation->word3,
211                              L2_VLAN_1_PRESENT | L2_VLAN_N_PRESENT))
212                 mbuf->ol_flags |= PKT_RX_VLAN;
213
214         if (BIT_ISSET_AT_POS(annotation->word8, DPAA2_ETH_FAS_L3CE))
215                 mbuf->ol_flags |= PKT_RX_IP_CKSUM_BAD;
216
217         if (BIT_ISSET_AT_POS(annotation->word8, DPAA2_ETH_FAS_L4CE))
218                 mbuf->ol_flags |= PKT_RX_L4_CKSUM_BAD;
219 }
220
221 static inline struct rte_mbuf *__attribute__((hot))
222 eth_sg_fd_to_mbuf(const struct qbman_fd *fd)
223 {
224         struct qbman_sge *sgt, *sge;
225         dma_addr_t sg_addr;
226         int i = 0;
227         uint64_t fd_addr;
228         struct rte_mbuf *first_seg, *next_seg, *cur_seg, *temp;
229
230         fd_addr = (uint64_t)DPAA2_IOVA_TO_VADDR(DPAA2_GET_FD_ADDR(fd));
231
232         /* Get Scatter gather table address */
233         sgt = (struct qbman_sge *)(fd_addr + DPAA2_GET_FD_OFFSET(fd));
234
235         sge = &sgt[i++];
236         sg_addr = (uint64_t)DPAA2_IOVA_TO_VADDR(DPAA2_GET_FLE_ADDR(sge));
237
238         /* First Scatter gather entry */
239         first_seg = DPAA2_INLINE_MBUF_FROM_BUF(sg_addr,
240                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size);
241         /* Prepare all the metadata for first segment */
242         first_seg->buf_addr = (uint8_t *)sg_addr;
243         first_seg->ol_flags = 0;
244         first_seg->data_off = DPAA2_GET_FLE_OFFSET(sge);
245         first_seg->data_len = sge->length  & 0x1FFFF;
246         first_seg->pkt_len = DPAA2_GET_FD_LEN(fd);
247         first_seg->nb_segs = 1;
248         first_seg->next = NULL;
249         if (dpaa2_svr_family == SVR_LX2160A)
250                 dpaa2_dev_rx_parse_frc(first_seg,
251                                 DPAA2_GET_FD_FRC_PARSE_SUM(fd));
252         else {
253                 first_seg->packet_type = dpaa2_dev_rx_parse(
254                          (uint64_t)DPAA2_IOVA_TO_VADDR(DPAA2_GET_FD_ADDR(fd))
255                          + DPAA2_FD_PTA_SIZE);
256                 dpaa2_dev_rx_offload((uint64_t)DPAA2_IOVA_TO_VADDR(
257                         DPAA2_GET_FD_ADDR(fd)) +
258                         DPAA2_FD_PTA_SIZE, first_seg);
259         }
260         rte_mbuf_refcnt_set(first_seg, 1);
261         cur_seg = first_seg;
262         while (!DPAA2_SG_IS_FINAL(sge)) {
263                 sge = &sgt[i++];
264                 sg_addr = (uint64_t)DPAA2_IOVA_TO_VADDR(
265                                 DPAA2_GET_FLE_ADDR(sge));
266                 next_seg = DPAA2_INLINE_MBUF_FROM_BUF(sg_addr,
267                         rte_dpaa2_bpid_info[DPAA2_GET_FLE_BPID(sge)].meta_data_size);
268                 next_seg->buf_addr  = (uint8_t *)sg_addr;
269                 next_seg->data_off  = DPAA2_GET_FLE_OFFSET(sge);
270                 next_seg->data_len  = sge->length  & 0x1FFFF;
271                 first_seg->nb_segs += 1;
272                 rte_mbuf_refcnt_set(next_seg, 1);
273                 cur_seg->next = next_seg;
274                 next_seg->next = NULL;
275                 cur_seg = next_seg;
276         }
277         temp = DPAA2_INLINE_MBUF_FROM_BUF(fd_addr,
278                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size);
279         rte_mbuf_refcnt_set(temp, 1);
280         rte_pktmbuf_free_seg(temp);
281
282         return (void *)first_seg;
283 }
284
285 static inline struct rte_mbuf *__attribute__((hot))
286 eth_fd_to_mbuf(const struct qbman_fd *fd)
287 {
288         struct rte_mbuf *mbuf = DPAA2_INLINE_MBUF_FROM_BUF(
289                 DPAA2_IOVA_TO_VADDR(DPAA2_GET_FD_ADDR(fd)),
290                      rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size);
291
292         /* need to repopulated some of the fields,
293          * as they may have changed in last transmission
294          */
295         mbuf->nb_segs = 1;
296         mbuf->ol_flags = 0;
297         mbuf->data_off = DPAA2_GET_FD_OFFSET(fd);
298         mbuf->data_len = DPAA2_GET_FD_LEN(fd);
299         mbuf->pkt_len = mbuf->data_len;
300         mbuf->next = NULL;
301         rte_mbuf_refcnt_set(mbuf, 1);
302
303         /* Parse the packet */
304         /* parse results for LX2 are there in FRC field of FD.
305          * For other DPAA2 platforms , parse results are after
306          * the private - sw annotation area
307          */
308
309         if (dpaa2_svr_family == SVR_LX2160A)
310                 dpaa2_dev_rx_parse_frc(mbuf, DPAA2_GET_FD_FRC_PARSE_SUM(fd));
311         else {
312                 mbuf->packet_type = dpaa2_dev_rx_parse(
313                         (uint64_t)DPAA2_IOVA_TO_VADDR(DPAA2_GET_FD_ADDR(fd))
314                          + DPAA2_FD_PTA_SIZE);
315                 dpaa2_dev_rx_offload((uint64_t)DPAA2_IOVA_TO_VADDR(
316                              DPAA2_GET_FD_ADDR(fd)) +
317                              DPAA2_FD_PTA_SIZE, mbuf);
318         }
319
320         PMD_RX_LOG(DEBUG, "to mbuf - mbuf =%p, mbuf->buf_addr =%p, off = %d,"
321                 "fd_off=%d fd =%lx, meta = %d  bpid =%d, len=%d\n",
322                 mbuf, mbuf->buf_addr, mbuf->data_off,
323                 DPAA2_GET_FD_OFFSET(fd), DPAA2_GET_FD_ADDR(fd),
324                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size,
325                 DPAA2_GET_FD_BPID(fd), DPAA2_GET_FD_LEN(fd));
326
327         return mbuf;
328 }
329
330 static int __attribute__ ((noinline)) __attribute__((hot))
331 eth_mbuf_to_sg_fd(struct rte_mbuf *mbuf,
332                   struct qbman_fd *fd, uint16_t bpid)
333 {
334         struct rte_mbuf *cur_seg = mbuf, *prev_seg, *mi, *temp;
335         struct qbman_sge *sgt, *sge = NULL;
336         int i;
337
338         /* First Prepare FD to be transmited*/
339         /* Resetting the buffer pool id and offset field*/
340         fd->simple.bpid_offset = 0;
341
342         if (unlikely(mbuf->ol_flags & PKT_TX_VLAN_PKT)) {
343                 int ret = rte_vlan_insert(&mbuf);
344                 if (ret)
345                         return ret;
346         }
347
348         temp = rte_pktmbuf_alloc(mbuf->pool);
349         if (temp == NULL) {
350                 PMD_TX_LOG(ERR, "No memory to allocate S/G table");
351                 return -ENOMEM;
352         }
353
354         DPAA2_SET_FD_ADDR(fd, DPAA2_MBUF_VADDR_TO_IOVA(temp));
355         DPAA2_SET_FD_LEN(fd, mbuf->pkt_len);
356         DPAA2_SET_FD_OFFSET(fd, temp->data_off);
357         DPAA2_SET_FD_BPID(fd, bpid);
358         DPAA2_SET_FD_ASAL(fd, DPAA2_ASAL_VAL);
359         DPAA2_FD_SET_FORMAT(fd, qbman_fd_sg);
360         /*Set Scatter gather table and Scatter gather entries*/
361         sgt = (struct qbman_sge *)(
362                         (uint64_t)DPAA2_IOVA_TO_VADDR(DPAA2_GET_FD_ADDR(fd))
363                         + DPAA2_GET_FD_OFFSET(fd));
364
365         for (i = 0; i < mbuf->nb_segs; i++) {
366                 sge = &sgt[i];
367                 /*Resetting the buffer pool id and offset field*/
368                 sge->fin_bpid_offset = 0;
369                 DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(cur_seg));
370                 DPAA2_SET_FLE_OFFSET(sge, cur_seg->data_off);
371                 sge->length = cur_seg->data_len;
372                 if (RTE_MBUF_DIRECT(cur_seg)) {
373                         if (rte_mbuf_refcnt_read(cur_seg) > 1) {
374                                 /* If refcnt > 1, invalid bpid is set to ensure
375                                  * buffer is not freed by HW
376                                  */
377                                 DPAA2_SET_FLE_IVP(sge);
378                                 rte_mbuf_refcnt_update(cur_seg, -1);
379                         } else
380                                 DPAA2_SET_FLE_BPID(sge,
381                                                 mempool_to_bpid(cur_seg->pool));
382                         cur_seg = cur_seg->next;
383                 } else {
384                         /* Get owner MBUF from indirect buffer */
385                         mi = rte_mbuf_from_indirect(cur_seg);
386                         if (rte_mbuf_refcnt_read(mi) > 1) {
387                                 /* If refcnt > 1, invalid bpid is set to ensure
388                                  * owner buffer is not freed by HW
389                                  */
390                                 DPAA2_SET_FLE_IVP(sge);
391                         } else {
392                                 DPAA2_SET_FLE_BPID(sge,
393                                                    mempool_to_bpid(mi->pool));
394                                 rte_mbuf_refcnt_update(mi, 1);
395                         }
396                         prev_seg = cur_seg;
397                         cur_seg = cur_seg->next;
398                         prev_seg->next = NULL;
399                         rte_pktmbuf_free(prev_seg);
400                 }
401         }
402         DPAA2_SG_SET_FINAL(sge, true);
403         return 0;
404 }
405
406 static void
407 eth_mbuf_to_fd(struct rte_mbuf *mbuf,
408                struct qbman_fd *fd, uint16_t bpid) __attribute__((unused));
409
410 static void __attribute__ ((noinline)) __attribute__((hot))
411 eth_mbuf_to_fd(struct rte_mbuf *mbuf,
412                struct qbman_fd *fd, uint16_t bpid)
413 {
414         if (unlikely(mbuf->ol_flags & PKT_TX_VLAN_PKT)) {
415                 if (rte_vlan_insert(&mbuf)) {
416                         rte_pktmbuf_free(mbuf);
417                         return;
418                 }
419         }
420         /*Resetting the buffer pool id and offset field*/
421         fd->simple.bpid_offset = 0;
422
423         DPAA2_MBUF_TO_CONTIG_FD(mbuf, fd, bpid);
424
425         PMD_TX_LOG(DEBUG, "mbuf =%p, mbuf->buf_addr =%p, off = %d,"
426                 "fd_off=%d fd =%lx, meta = %d  bpid =%d, len=%d\n",
427                 mbuf, mbuf->buf_addr, mbuf->data_off,
428                 DPAA2_GET_FD_OFFSET(fd), DPAA2_GET_FD_ADDR(fd),
429                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size,
430                 DPAA2_GET_FD_BPID(fd), DPAA2_GET_FD_LEN(fd));
431         if (RTE_MBUF_DIRECT(mbuf)) {
432                 if (rte_mbuf_refcnt_read(mbuf) > 1) {
433                         DPAA2_SET_FD_IVP(fd);
434                         rte_mbuf_refcnt_update(mbuf, -1);
435                 }
436         } else {
437                 struct rte_mbuf *mi;
438
439                 mi = rte_mbuf_from_indirect(mbuf);
440                 if (rte_mbuf_refcnt_read(mi) > 1)
441                         DPAA2_SET_FD_IVP(fd);
442                 else
443                         rte_mbuf_refcnt_update(mi, 1);
444                 rte_pktmbuf_free(mbuf);
445         }
446 }
447
448 static inline int __attribute__((hot))
449 eth_copy_mbuf_to_fd(struct rte_mbuf *mbuf,
450                     struct qbman_fd *fd, uint16_t bpid)
451 {
452         struct rte_mbuf *m;
453         void *mb = NULL;
454
455         if (unlikely(mbuf->ol_flags & PKT_TX_VLAN_PKT)) {
456                 int ret = rte_vlan_insert(&mbuf);
457                 if (ret)
458                         return ret;
459         }
460
461         if (rte_dpaa2_mbuf_alloc_bulk(
462                 rte_dpaa2_bpid_info[bpid].bp_list->mp, &mb, 1)) {
463                 PMD_TX_LOG(WARNING, "Unable to allocated DPAA2 buffer");
464                 return -1;
465         }
466         m = (struct rte_mbuf *)mb;
467         memcpy((char *)m->buf_addr + mbuf->data_off,
468                (void *)((char *)mbuf->buf_addr + mbuf->data_off),
469                 mbuf->pkt_len);
470
471         /* Copy required fields */
472         m->data_off = mbuf->data_off;
473         m->ol_flags = mbuf->ol_flags;
474         m->packet_type = mbuf->packet_type;
475         m->tx_offload = mbuf->tx_offload;
476
477         /*Resetting the buffer pool id and offset field*/
478         fd->simple.bpid_offset = 0;
479
480         DPAA2_MBUF_TO_CONTIG_FD(m, fd, bpid);
481
482         PMD_TX_LOG(DEBUG, " mbuf %p BMAN buf addr %p",
483                    (void *)mbuf, mbuf->buf_addr);
484
485         PMD_TX_LOG(DEBUG, " fdaddr =%lx bpid =%d meta =%d off =%d, len =%d",
486                    DPAA2_GET_FD_ADDR(fd),
487                 DPAA2_GET_FD_BPID(fd),
488                 rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size,
489                 DPAA2_GET_FD_OFFSET(fd),
490                 DPAA2_GET_FD_LEN(fd));
491
492         return 0;
493 }
494
495 uint16_t
496 dpaa2_dev_prefetch_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
497 {
498         /* Function receive frames for a given device and VQ*/
499         struct dpaa2_queue *dpaa2_q = (struct dpaa2_queue *)queue;
500         struct qbman_result *dq_storage, *dq_storage1 = 0;
501         uint32_t fqid = dpaa2_q->fqid;
502         int ret, num_rx = 0, next_pull = 0, num_pulled, num_to_pull;
503         uint8_t pending, is_repeat, status;
504         struct qbman_swp *swp;
505         const struct qbman_fd *fd, *next_fd;
506         struct qbman_pull_desc pulldesc;
507         struct queue_storage_info_t *q_storage = dpaa2_q->q_storage;
508         struct rte_eth_dev *dev = dpaa2_q->dev;
509
510         if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
511                 ret = dpaa2_affine_qbman_swp();
512                 if (ret) {
513                         RTE_LOG(ERR, PMD, "Failure in affining portal\n");
514                         return 0;
515                 }
516         }
517         swp = DPAA2_PER_LCORE_PORTAL;
518
519         /* if the original request for this q was from another portal */
520         if (unlikely(DPAA2_PER_LCORE_DPIO->index !=
521                 q_storage->active_dpio_id)) {
522                 if (check_swp_active_dqs(DPAA2_PER_LCORE_DPIO->index)) {
523                         while (!qbman_check_command_complete(get_swp_active_dqs
524                                 (DPAA2_PER_LCORE_DPIO->index)))
525                                 ;
526                         clear_swp_active_dqs(DPAA2_PER_LCORE_DPIO->index);
527                 }
528                 q_storage->active_dpio_id = DPAA2_PER_LCORE_DPIO->index;
529         }
530
531         if (unlikely(!q_storage->active_dqs)) {
532                 q_storage->toggle = 0;
533                 dq_storage = q_storage->dq_storage[q_storage->toggle];
534                 q_storage->last_num_pkts = (nb_pkts > DPAA2_DQRR_RING_SIZE) ?
535                                                DPAA2_DQRR_RING_SIZE : nb_pkts;
536                 qbman_pull_desc_clear(&pulldesc);
537                 qbman_pull_desc_set_numframes(&pulldesc,
538                                               q_storage->last_num_pkts);
539                 qbman_pull_desc_set_fq(&pulldesc, fqid);
540                 qbman_pull_desc_set_storage(&pulldesc, dq_storage,
541                         (dma_addr_t)(DPAA2_VADDR_TO_IOVA(dq_storage)), 1);
542                 while (1) {
543                         if (qbman_swp_pull(swp, &pulldesc)) {
544                                 PMD_RX_LOG(WARNING,
545                                         "VDQ command not issued.QBMAN busy\n");
546                                 /* Portal was busy, try again */
547                                 continue;
548                         }
549                         break;
550                 }
551                 q_storage->active_dqs = dq_storage;
552                 set_swp_active_dqs(DPAA2_PER_LCORE_DPIO->index, dq_storage);
553         }
554
555         /* pkt to pull in current pull request */
556         num_to_pull = q_storage->last_num_pkts;
557
558         /* Number of packet requested is more than current pull request */
559         if (nb_pkts > num_to_pull)
560                 next_pull = nb_pkts - num_to_pull;
561
562         dq_storage = q_storage->active_dqs;
563         /* Check if the previous issued command is completed.
564          * Also seems like the SWP is shared between the Ethernet Driver
565          * and the SEC driver.
566          */
567         while (!qbman_check_command_complete(dq_storage))
568                 ;
569         if (dq_storage == get_swp_active_dqs(q_storage->active_dpio_id))
570                 clear_swp_active_dqs(q_storage->active_dpio_id);
571
572 repeat:
573         is_repeat = 0;
574
575         /* issue the deq command one more time to get another set of packets */
576         if (next_pull) {
577                 q_storage->toggle ^= 1;
578                 dq_storage1 = q_storage->dq_storage[q_storage->toggle];
579                 qbman_pull_desc_clear(&pulldesc);
580
581                 if (next_pull > DPAA2_DQRR_RING_SIZE) {
582                         qbman_pull_desc_set_numframes(&pulldesc,
583                                         DPAA2_DQRR_RING_SIZE);
584                         next_pull = next_pull - DPAA2_DQRR_RING_SIZE;
585                         q_storage->last_num_pkts = DPAA2_DQRR_RING_SIZE;
586                 } else {
587                         qbman_pull_desc_set_numframes(&pulldesc, next_pull);
588                         q_storage->last_num_pkts = next_pull;
589                         next_pull = 0;
590                 }
591                 qbman_pull_desc_set_fq(&pulldesc, fqid);
592                 qbman_pull_desc_set_storage(&pulldesc, dq_storage1,
593                         (dma_addr_t)(DPAA2_VADDR_TO_IOVA(dq_storage1)), 1);
594                 while (1) {
595                         if (qbman_swp_pull(swp, &pulldesc)) {
596                                 PMD_RX_LOG(WARNING,
597                                         "VDQ command not issued.QBMAN busy\n");
598                                 /* Portal was busy, try again */
599                                 continue;
600                         }
601                         break;
602                 }
603                 is_repeat = 1;
604                 q_storage->active_dqs = dq_storage1;
605                 set_swp_active_dqs(DPAA2_PER_LCORE_DPIO->index, dq_storage1);
606         }
607
608         rte_prefetch0((void *)((uint64_t)(dq_storage + 1)));
609
610         num_pulled = 0;
611         pending = 1;
612
613         do {
614                 /* Loop until the dq_storage is updated with
615                  * new token by QBMAN
616                  */
617                 while (!qbman_check_new_result(dq_storage))
618                         ;
619                 rte_prefetch0((void *)((uint64_t)(dq_storage + 2)));
620                 /* Check whether Last Pull command is Expired and
621                  * setting Condition for Loop termination
622                  */
623                 if (qbman_result_DQ_is_pull_complete(dq_storage)) {
624                         pending = 0;
625                         /* Check for valid frame. */
626                         status = qbman_result_DQ_flags(dq_storage);
627                         if (unlikely((status & QBMAN_DQ_STAT_VALIDFRAME) == 0))
628                                 continue;
629                 }
630                 fd = qbman_result_DQ_fd(dq_storage);
631
632                 next_fd = qbman_result_DQ_fd(dq_storage + 1);
633                 /* Prefetch Annotation address for the parse results */
634                 rte_prefetch0((void *)(DPAA2_GET_FD_ADDR(next_fd)
635                                 + DPAA2_FD_PTA_SIZE + 16));
636
637                 if (unlikely(DPAA2_FD_GET_FORMAT(fd) == qbman_fd_sg))
638                         bufs[num_rx] = eth_sg_fd_to_mbuf(fd);
639                 else
640                         bufs[num_rx] = eth_fd_to_mbuf(fd);
641                 bufs[num_rx]->port = dev->data->port_id;
642
643                 if (dev->data->dev_conf.rxmode.hw_vlan_strip)
644                         rte_vlan_strip(bufs[num_rx]);
645
646                 dq_storage++;
647                 num_rx++;
648                 num_pulled++;
649         } while (pending);
650
651         /* Another VDQ request pending and this request returned full */
652         if (is_repeat) {
653                 /* all packets pulled from this pull request */
654                 if (num_pulled == num_to_pull)  {
655                         /* pkt to pull in current pull request */
656                         num_to_pull = q_storage->last_num_pkts;
657
658                         dq_storage = dq_storage1;
659
660                         while (!qbman_check_command_complete(dq_storage))
661                                 ;
662                         goto repeat;
663                 } else {
664                         /* if this request did not returned all pkts */
665                         goto next_time;
666                 }
667         }
668
669         q_storage->toggle ^= 1;
670         dq_storage = q_storage->dq_storage[q_storage->toggle];
671         q_storage->last_num_pkts = (nb_pkts > DPAA2_DQRR_RING_SIZE) ?
672                                        DPAA2_DQRR_RING_SIZE : nb_pkts;
673         qbman_pull_desc_clear(&pulldesc);
674         qbman_pull_desc_set_numframes(&pulldesc, q_storage->last_num_pkts);
675         qbman_pull_desc_set_fq(&pulldesc, fqid);
676         qbman_pull_desc_set_storage(&pulldesc, dq_storage,
677                         (dma_addr_t)(DPAA2_VADDR_TO_IOVA(dq_storage)), 1);
678         /* issue a volatile dequeue command for next pull */
679         while (1) {
680                 if (qbman_swp_pull(swp, &pulldesc)) {
681                         PMD_RX_LOG(WARNING, "VDQ command is not issued."
682                                    "QBMAN is busy\n");
683                         continue;
684                 }
685                 break;
686         }
687         q_storage->active_dqs = dq_storage;
688         set_swp_active_dqs(DPAA2_PER_LCORE_DPIO->index, dq_storage);
689
690 next_time:
691         dpaa2_q->rx_pkts += num_rx;
692
693         return num_rx;
694 }
695
696 void __attribute__((hot))
697 dpaa2_dev_process_parallel_event(struct qbman_swp *swp,
698                                  const struct qbman_fd *fd,
699                                  const struct qbman_result *dq,
700                                  struct dpaa2_queue *rxq,
701                                  struct rte_event *ev)
702 {
703         ev->mbuf = eth_fd_to_mbuf(fd);
704
705         ev->flow_id = rxq->ev.flow_id;
706         ev->sub_event_type = rxq->ev.sub_event_type;
707         ev->event_type = RTE_EVENT_TYPE_ETHDEV;
708         ev->op = RTE_EVENT_OP_NEW;
709         ev->sched_type = rxq->ev.sched_type;
710         ev->queue_id = rxq->ev.queue_id;
711         ev->priority = rxq->ev.priority;
712
713         qbman_swp_dqrr_consume(swp, dq);
714 }
715
716 void dpaa2_dev_process_atomic_event(struct qbman_swp *swp __attribute__((unused)),
717                                     const struct qbman_fd *fd,
718                                     const struct qbman_result *dq,
719                                     struct dpaa2_queue *rxq,
720                                     struct rte_event *ev)
721 {
722         uint8_t dqrr_index = qbman_get_dqrr_idx(dq);
723
724         ev->mbuf = eth_fd_to_mbuf(fd);
725
726         ev->flow_id = rxq->ev.flow_id;
727         ev->sub_event_type = rxq->ev.sub_event_type;
728         ev->event_type = RTE_EVENT_TYPE_ETHDEV;
729         ev->op = RTE_EVENT_OP_NEW;
730         ev->sched_type = rxq->ev.sched_type;
731         ev->queue_id = rxq->ev.queue_id;
732         ev->priority = rxq->ev.priority;
733
734         ev->mbuf->seqn = dqrr_index + 1;
735         DPAA2_PER_LCORE_DQRR_SIZE++;
736         DPAA2_PER_LCORE_DQRR_HELD |= 1 << dqrr_index;
737         DPAA2_PER_LCORE_DQRR_MBUF(dqrr_index) = ev->mbuf;
738 }
739
740 /*
741  * Callback to handle sending packets through WRIOP based interface
742  */
743 uint16_t
744 dpaa2_dev_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
745 {
746         /* Function to transmit the frames to given device and VQ*/
747         uint32_t loop, retry_count;
748         int32_t ret;
749         struct qbman_fd fd_arr[MAX_TX_RING_SLOTS];
750         struct rte_mbuf *mi;
751         uint32_t frames_to_send;
752         struct rte_mempool *mp;
753         struct qbman_eq_desc eqdesc;
754         struct dpaa2_queue *dpaa2_q = (struct dpaa2_queue *)queue;
755         struct qbman_swp *swp;
756         uint16_t num_tx = 0;
757         uint16_t bpid;
758         struct rte_eth_dev *dev = dpaa2_q->dev;
759         struct dpaa2_dev_priv *priv = dev->data->dev_private;
760         uint32_t flags[MAX_TX_RING_SLOTS] = {0};
761
762         if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
763                 ret = dpaa2_affine_qbman_swp();
764                 if (ret) {
765                         RTE_LOG(ERR, PMD, "Failure in affining portal\n");
766                         return 0;
767                 }
768         }
769         swp = DPAA2_PER_LCORE_PORTAL;
770
771         PMD_TX_LOG(DEBUG, "===> dev =%p, fqid =%d", dev, dpaa2_q->fqid);
772
773         /*Prepare enqueue descriptor*/
774         qbman_eq_desc_clear(&eqdesc);
775         qbman_eq_desc_set_no_orp(&eqdesc, DPAA2_EQ_RESP_ERR_FQ);
776         qbman_eq_desc_set_response(&eqdesc, 0, 0);
777         qbman_eq_desc_set_qd(&eqdesc, priv->qdid,
778                              dpaa2_q->flow_id, dpaa2_q->tc_index);
779         /*Clear the unused FD fields before sending*/
780         while (nb_pkts) {
781                 /*Check if the queue is congested*/
782                 retry_count = 0;
783                 while (qbman_result_SCN_state(dpaa2_q->cscn)) {
784                         retry_count++;
785                         /* Retry for some time before giving up */
786                         if (retry_count > CONG_RETRY_COUNT)
787                                 goto skip_tx;
788                 }
789
790                 frames_to_send = (nb_pkts >> 3) ? MAX_TX_RING_SLOTS : nb_pkts;
791
792                 for (loop = 0; loop < frames_to_send; loop++) {
793                         if ((*bufs)->seqn) {
794                                 uint8_t dqrr_index = (*bufs)->seqn - 1;
795
796                                 flags[loop] = QBMAN_ENQUEUE_FLAG_DCA |
797                                                 dqrr_index;
798                                 DPAA2_PER_LCORE_DQRR_SIZE--;
799                                 DPAA2_PER_LCORE_DQRR_HELD &= ~(1 << dqrr_index);
800                                 (*bufs)->seqn = DPAA2_INVALID_MBUF_SEQN;
801                         }
802
803                         fd_arr[loop].simple.frc = 0;
804                         DPAA2_RESET_FD_CTRL((&fd_arr[loop]));
805                         DPAA2_SET_FD_FLC((&fd_arr[loop]), NULL);
806                         if (likely(RTE_MBUF_DIRECT(*bufs))) {
807                                 mp = (*bufs)->pool;
808                                 /* Check the basic scenario and set
809                                  * the FD appropriately here itself.
810                                  */
811                                 if (likely(mp && mp->ops_index ==
812                                     priv->bp_list->dpaa2_ops_index &&
813                                     (*bufs)->nb_segs == 1 &&
814                                     rte_mbuf_refcnt_read((*bufs)) == 1)) {
815                                         if (unlikely((*bufs)->ol_flags
816                                                 & PKT_TX_VLAN_PKT)) {
817                                                 ret = rte_vlan_insert(bufs);
818                                                 if (ret)
819                                                         goto send_n_return;
820                                         }
821                                         DPAA2_MBUF_TO_CONTIG_FD((*bufs),
822                                         &fd_arr[loop], mempool_to_bpid(mp));
823                                         bufs++;
824                                         continue;
825                                 }
826                         } else {
827                                 mi = rte_mbuf_from_indirect(*bufs);
828                                 mp = mi->pool;
829                         }
830                         /* Not a hw_pkt pool allocated frame */
831                         if (unlikely(!mp || !priv->bp_list)) {
832                                 PMD_TX_LOG(ERR, "err: no bpool attached");
833                                 goto send_n_return;
834                         }
835
836                         if (mp->ops_index != priv->bp_list->dpaa2_ops_index) {
837                                 PMD_TX_LOG(ERR, "non hw offload bufffer ");
838                                 /* alloc should be from the default buffer pool
839                                  * attached to this interface
840                                  */
841                                 bpid = priv->bp_list->buf_pool.bpid;
842
843                                 if (unlikely((*bufs)->nb_segs > 1)) {
844                                         PMD_TX_LOG(ERR, "S/G support not added"
845                                                 " for non hw offload buffer");
846                                         goto send_n_return;
847                                 }
848                                 if (eth_copy_mbuf_to_fd(*bufs,
849                                                         &fd_arr[loop], bpid)) {
850                                         goto send_n_return;
851                                 }
852                                 /* free the original packet */
853                                 rte_pktmbuf_free(*bufs);
854                         } else {
855                                 bpid = mempool_to_bpid(mp);
856                                 if (unlikely((*bufs)->nb_segs > 1)) {
857                                         if (eth_mbuf_to_sg_fd(*bufs,
858                                                         &fd_arr[loop], bpid))
859                                                 goto send_n_return;
860                                 } else {
861                                         eth_mbuf_to_fd(*bufs,
862                                                        &fd_arr[loop], bpid);
863                                 }
864                         }
865                         bufs++;
866                 }
867                 loop = 0;
868                 while (loop < frames_to_send) {
869                         loop += qbman_swp_enqueue_multiple(swp, &eqdesc,
870                                         &fd_arr[loop], &flags[loop],
871                                         frames_to_send - loop);
872                 }
873
874                 num_tx += frames_to_send;
875                 nb_pkts -= frames_to_send;
876         }
877         dpaa2_q->tx_pkts += num_tx;
878         return num_tx;
879
880 send_n_return:
881         /* send any already prepared fd */
882         if (loop) {
883                 unsigned int i = 0;
884
885                 while (i < loop) {
886                         i += qbman_swp_enqueue_multiple(swp, &eqdesc,
887                                                         &fd_arr[i],
888                                                         &flags[loop],
889                                                         loop - i);
890                 }
891                 num_tx += loop;
892         }
893 skip_tx:
894         dpaa2_q->tx_pkts += num_tx;
895         return num_tx;
896 }
897
898 /**
899  * Dummy DPDK callback for TX.
900  *
901  * This function is used to temporarily replace the real callback during
902  * unsafe control operations on the queue, or in case of error.
903  *
904  * @param dpdk_txq
905  *   Generic pointer to TX queue structure.
906  * @param[in] pkts
907  *   Packets to transmit.
908  * @param pkts_n
909  *   Number of packets in array.
910  *
911  * @return
912  *   Number of packets successfully transmitted (<= pkts_n).
913  */
914 uint16_t
915 dummy_dev_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
916 {
917         (void)queue;
918         (void)bufs;
919         (void)nb_pkts;
920         return 0;
921 }