net/af_xdp: support need wakeup feature
[dpdk.git] / drivers / net / iavf / iavf_rxtx_vec_common.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #ifndef _IAVF_RXTX_VEC_COMMON_H_
6 #define _IAVF_RXTX_VEC_COMMON_H_
7 #include <stdint.h>
8 #include <rte_ethdev_driver.h>
9 #include <rte_malloc.h>
10
11 #include "iavf.h"
12 #include "iavf_rxtx.h"
13
14 static inline uint16_t
15 reassemble_packets(struct iavf_rx_queue *rxq, struct rte_mbuf **rx_bufs,
16                    uint16_t nb_bufs, uint8_t *split_flags)
17 {
18         struct rte_mbuf *pkts[IAVF_VPMD_RX_MAX_BURST];
19         struct rte_mbuf *start = rxq->pkt_first_seg;
20         struct rte_mbuf *end =  rxq->pkt_last_seg;
21         unsigned int pkt_idx, buf_idx;
22
23         for (buf_idx = 0, pkt_idx = 0; buf_idx < nb_bufs; buf_idx++) {
24                 if (end) {
25                         /* processing a split packet */
26                         end->next = rx_bufs[buf_idx];
27                         rx_bufs[buf_idx]->data_len += rxq->crc_len;
28
29                         start->nb_segs++;
30                         start->pkt_len += rx_bufs[buf_idx]->data_len;
31                         end = end->next;
32
33                         if (!split_flags[buf_idx]) {
34                                 /* it's the last packet of the set */
35                                 start->hash = end->hash;
36                                 start->ol_flags = end->ol_flags;
37                                 /* we need to strip crc for the whole packet */
38                                 start->pkt_len -= rxq->crc_len;
39                                 if (end->data_len > rxq->crc_len) {
40                                         end->data_len -= rxq->crc_len;
41                                 } else {
42                                         /* free up last mbuf */
43                                         struct rte_mbuf *secondlast = start;
44
45                                         start->nb_segs--;
46                                         while (secondlast->next != end)
47                                                 secondlast = secondlast->next;
48                                         secondlast->data_len -= (rxq->crc_len -
49                                                         end->data_len);
50                                         secondlast->next = NULL;
51                                         rte_pktmbuf_free_seg(end);
52                                 }
53                                 pkts[pkt_idx++] = start;
54                                 start = NULL;
55                                 end = NULL;
56                         }
57                 } else {
58                         /* not processing a split packet */
59                         if (!split_flags[buf_idx]) {
60                                 /* not a split packet, save and skip */
61                                 pkts[pkt_idx++] = rx_bufs[buf_idx];
62                                 continue;
63                         }
64                         end = start = rx_bufs[buf_idx];
65                         rx_bufs[buf_idx]->data_len += rxq->crc_len;
66                         rx_bufs[buf_idx]->pkt_len += rxq->crc_len;
67                 }
68         }
69
70         /* save the partial packet for next time */
71         rxq->pkt_first_seg = start;
72         rxq->pkt_last_seg = end;
73         memcpy(rx_bufs, pkts, pkt_idx * (sizeof(*pkts)));
74         return pkt_idx;
75 }
76
77 static __rte_always_inline int
78 iavf_tx_free_bufs(struct iavf_tx_queue *txq)
79 {
80         struct iavf_tx_entry *txep;
81         uint32_t n;
82         uint32_t i;
83         int nb_free = 0;
84         struct rte_mbuf *m, *free[IAVF_VPMD_TX_MAX_FREE_BUF];
85
86         /* check DD bits on threshold descriptor */
87         if ((txq->tx_ring[txq->next_dd].cmd_type_offset_bsz &
88                         rte_cpu_to_le_64(IAVF_TXD_QW1_DTYPE_MASK)) !=
89                         rte_cpu_to_le_64(IAVF_TX_DESC_DTYPE_DESC_DONE))
90                 return 0;
91
92         n = txq->rs_thresh;
93
94          /* first buffer to free from S/W ring is at index
95           * tx_next_dd - (tx_rs_thresh-1)
96           */
97         txep = &txq->sw_ring[txq->next_dd - (n - 1)];
98         m = rte_pktmbuf_prefree_seg(txep[0].mbuf);
99         if (likely(m != NULL)) {
100                 free[0] = m;
101                 nb_free = 1;
102                 for (i = 1; i < n; i++) {
103                         m = rte_pktmbuf_prefree_seg(txep[i].mbuf);
104                         if (likely(m != NULL)) {
105                                 if (likely(m->pool == free[0]->pool)) {
106                                         free[nb_free++] = m;
107                                 } else {
108                                         rte_mempool_put_bulk(free[0]->pool,
109                                                              (void *)free,
110                                                              nb_free);
111                                         free[0] = m;
112                                         nb_free = 1;
113                                 }
114                         }
115                 }
116                 rte_mempool_put_bulk(free[0]->pool, (void **)free, nb_free);
117         } else {
118                 for (i = 1; i < n; i++) {
119                         m = rte_pktmbuf_prefree_seg(txep[i].mbuf);
120                         if (m)
121                                 rte_mempool_put(m->pool, m);
122                 }
123         }
124
125         /* buffers were freed, update counters */
126         txq->nb_free = (uint16_t)(txq->nb_free + txq->rs_thresh);
127         txq->next_dd = (uint16_t)(txq->next_dd + txq->rs_thresh);
128         if (txq->next_dd >= txq->nb_tx_desc)
129                 txq->next_dd = (uint16_t)(txq->rs_thresh - 1);
130
131         return txq->rs_thresh;
132 }
133
134 static __rte_always_inline void
135 tx_backlog_entry(struct iavf_tx_entry *txep,
136                  struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
137 {
138         int i;
139
140         for (i = 0; i < (int)nb_pkts; ++i)
141                 txep[i].mbuf = tx_pkts[i];
142 }
143
144 static inline void
145 _iavf_rx_queue_release_mbufs_vec(struct iavf_rx_queue *rxq)
146 {
147         const unsigned int mask = rxq->nb_rx_desc - 1;
148         unsigned int i;
149
150         if (!rxq->sw_ring || rxq->rxrearm_nb >= rxq->nb_rx_desc)
151                 return;
152
153         /* free all mbufs that are valid in the ring */
154         if (rxq->rxrearm_nb == 0) {
155                 for (i = 0; i < rxq->nb_rx_desc; i++) {
156                         if (rxq->sw_ring[i])
157                                 rte_pktmbuf_free_seg(rxq->sw_ring[i]);
158                 }
159         } else {
160                 for (i = rxq->rx_tail;
161                      i != rxq->rxrearm_start;
162                      i = (i + 1) & mask) {
163                         if (rxq->sw_ring[i])
164                                 rte_pktmbuf_free_seg(rxq->sw_ring[i]);
165                 }
166         }
167
168         rxq->rxrearm_nb = rxq->nb_rx_desc;
169
170         /* set all entries to NULL */
171         memset(rxq->sw_ring, 0, sizeof(rxq->sw_ring[0]) * rxq->nb_rx_desc);
172 }
173
174 static inline void
175 _iavf_tx_queue_release_mbufs_vec(struct iavf_tx_queue *txq)
176 {
177         unsigned i;
178         const uint16_t max_desc = (uint16_t)(txq->nb_tx_desc - 1);
179
180         if (!txq->sw_ring || txq->nb_free == max_desc)
181                 return;
182
183         i = txq->next_dd - txq->rs_thresh + 1;
184         if (txq->tx_tail < i) {
185                 for (; i < txq->nb_tx_desc; i++) {
186                         rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
187                         txq->sw_ring[i].mbuf = NULL;
188                 }
189                 i = 0;
190         }
191 }
192
193 static inline int
194 iavf_rxq_vec_setup_default(struct iavf_rx_queue *rxq)
195 {
196         uintptr_t p;
197         struct rte_mbuf mb_def = { .buf_addr = 0 }; /* zeroed mbuf */
198
199         mb_def.nb_segs = 1;
200         mb_def.data_off = RTE_PKTMBUF_HEADROOM;
201         mb_def.port = rxq->port_id;
202         rte_mbuf_refcnt_set(&mb_def, 1);
203
204         /* prevent compiler reordering: rearm_data covers previous fields */
205         rte_compiler_barrier();
206         p = (uintptr_t)&mb_def.rearm_data;
207         rxq->mbuf_initializer = *(uint64_t *)p;
208         return 0;
209 }
210 #endif