Imported Upstream version 16.07-rc1
[deb_dpdk.git] / drivers / net / ixgbe / ixgbe_rxtx_vec_common.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   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 Intel Corporation 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 #ifndef _IXGBE_RXTX_VEC_COMMON_H_
35 #define _IXGBE_RXTX_VEC_COMMON_H_
36 #include <stdint.h>
37 #include <rte_ethdev.h>
38
39 #include "ixgbe_ethdev.h"
40 #include "ixgbe_rxtx.h"
41
42 static inline uint16_t
43 reassemble_packets(struct ixgbe_rx_queue *rxq, struct rte_mbuf **rx_bufs,
44                    uint16_t nb_bufs, uint8_t *split_flags)
45 {
46         struct rte_mbuf *pkts[nb_bufs]; /*finished pkts*/
47         struct rte_mbuf *start = rxq->pkt_first_seg;
48         struct rte_mbuf *end =  rxq->pkt_last_seg;
49         unsigned int pkt_idx, buf_idx;
50
51         for (buf_idx = 0, pkt_idx = 0; buf_idx < nb_bufs; buf_idx++) {
52                 if (end != NULL) {
53                         /* processing a split packet */
54                         end->next = rx_bufs[buf_idx];
55                         rx_bufs[buf_idx]->data_len += rxq->crc_len;
56
57                         start->nb_segs++;
58                         start->pkt_len += rx_bufs[buf_idx]->data_len;
59                         end = end->next;
60
61                         if (!split_flags[buf_idx]) {
62                                 /* it's the last packet of the set */
63                                 start->hash = end->hash;
64                                 start->ol_flags = end->ol_flags;
65                                 /* we need to strip crc for the whole packet */
66                                 start->pkt_len -= rxq->crc_len;
67                                 if (end->data_len > rxq->crc_len)
68                                         end->data_len -= rxq->crc_len;
69                                 else {
70                                         /* free up last mbuf */
71                                         struct rte_mbuf *secondlast = start;
72
73                                         start->nb_segs--;
74                                         while (secondlast->next != end)
75                                                 secondlast = secondlast->next;
76                                         secondlast->data_len -= (rxq->crc_len -
77                                                         end->data_len);
78                                         secondlast->next = NULL;
79                                         rte_pktmbuf_free_seg(end);
80                                 }
81                                 pkts[pkt_idx++] = start;
82                                 start = end = NULL;
83                         }
84                 } else {
85                         /* not processing a split packet */
86                         if (!split_flags[buf_idx]) {
87                                 /* not a split packet, save and skip */
88                                 pkts[pkt_idx++] = rx_bufs[buf_idx];
89                                 continue;
90                         }
91                         end = start = rx_bufs[buf_idx];
92                         rx_bufs[buf_idx]->data_len += rxq->crc_len;
93                         rx_bufs[buf_idx]->pkt_len += rxq->crc_len;
94                 }
95         }
96
97         /* save the partial packet for next time */
98         rxq->pkt_first_seg = start;
99         rxq->pkt_last_seg = end;
100         memcpy(rx_bufs, pkts, pkt_idx * (sizeof(*pkts)));
101         return pkt_idx;
102 }
103
104 static inline int __attribute__((always_inline))
105 ixgbe_tx_free_bufs(struct ixgbe_tx_queue *txq)
106 {
107         struct ixgbe_tx_entry_v *txep;
108         uint32_t status;
109         uint32_t n;
110         uint32_t i;
111         int nb_free = 0;
112         struct rte_mbuf *m, *free[RTE_IXGBE_TX_MAX_FREE_BUF_SZ];
113
114         /* check DD bit on threshold descriptor */
115         status = txq->tx_ring[txq->tx_next_dd].wb.status;
116         if (!(status & IXGBE_ADVTXD_STAT_DD))
117                 return 0;
118
119         n = txq->tx_rs_thresh;
120
121         /*
122          * first buffer to free from S/W ring is at index
123          * tx_next_dd - (tx_rs_thresh-1)
124          */
125         txep = &txq->sw_ring_v[txq->tx_next_dd - (n - 1)];
126         m = __rte_pktmbuf_prefree_seg(txep[0].mbuf);
127         if (likely(m != NULL)) {
128                 free[0] = m;
129                 nb_free = 1;
130                 for (i = 1; i < n; i++) {
131                         m = __rte_pktmbuf_prefree_seg(txep[i].mbuf);
132                         if (likely(m != NULL)) {
133                                 if (likely(m->pool == free[0]->pool))
134                                         free[nb_free++] = m;
135                                 else {
136                                         rte_mempool_put_bulk(free[0]->pool,
137                                                         (void *)free, nb_free);
138                                         free[0] = m;
139                                         nb_free = 1;
140                                 }
141                         }
142                 }
143                 rte_mempool_put_bulk(free[0]->pool, (void **)free, nb_free);
144         } else {
145                 for (i = 1; i < n; i++) {
146                         m = __rte_pktmbuf_prefree_seg(txep[i].mbuf);
147                         if (m != NULL)
148                                 rte_mempool_put(m->pool, m);
149                 }
150         }
151
152         /* buffers were freed, update counters */
153         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + txq->tx_rs_thresh);
154         txq->tx_next_dd = (uint16_t)(txq->tx_next_dd + txq->tx_rs_thresh);
155         if (txq->tx_next_dd >= txq->nb_tx_desc)
156                 txq->tx_next_dd = (uint16_t)(txq->tx_rs_thresh - 1);
157
158         return txq->tx_rs_thresh;
159 }
160
161 static inline void __attribute__((always_inline))
162 tx_backlog_entry(struct ixgbe_tx_entry_v *txep,
163                  struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
164 {
165         int i;
166
167         for (i = 0; i < (int)nb_pkts; ++i)
168                 txep[i].mbuf = tx_pkts[i];
169 }
170
171 static inline void
172 _ixgbe_tx_queue_release_mbufs_vec(struct ixgbe_tx_queue *txq)
173 {
174         unsigned int i;
175         struct ixgbe_tx_entry_v *txe;
176         const uint16_t max_desc = (uint16_t)(txq->nb_tx_desc - 1);
177
178         if (txq->sw_ring == NULL || txq->nb_tx_free == max_desc)
179                 return;
180
181         /* release the used mbufs in sw_ring */
182         for (i = txq->tx_next_dd - (txq->tx_rs_thresh - 1);
183              i != txq->tx_tail;
184              i = (i + 1) & max_desc) {
185                 txe = &txq->sw_ring_v[i];
186                 rte_pktmbuf_free_seg(txe->mbuf);
187         }
188         txq->nb_tx_free = max_desc;
189
190         /* reset tx_entry */
191         for (i = 0; i < txq->nb_tx_desc; i++) {
192                 txe = &txq->sw_ring_v[i];
193                 txe->mbuf = NULL;
194         }
195 }
196
197 static inline void
198 _ixgbe_rx_queue_release_mbufs_vec(struct ixgbe_rx_queue *rxq)
199 {
200         const unsigned int mask = rxq->nb_rx_desc - 1;
201         unsigned int i;
202
203         if (rxq->sw_ring == NULL || rxq->rxrearm_nb >= rxq->nb_rx_desc)
204                 return;
205
206         /* free all mbufs that are valid in the ring */
207         for (i = rxq->rx_tail; i != rxq->rxrearm_start; i = (i + 1) & mask)
208                 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
209         rxq->rxrearm_nb = rxq->nb_rx_desc;
210
211         /* set all entries to NULL */
212         memset(rxq->sw_ring, 0, sizeof(rxq->sw_ring[0]) * rxq->nb_rx_desc);
213 }
214
215 static inline void
216 _ixgbe_tx_free_swring_vec(struct ixgbe_tx_queue *txq)
217 {
218         if (txq == NULL)
219                 return;
220
221         if (txq->sw_ring != NULL) {
222                 rte_free(txq->sw_ring_v - 1);
223                 txq->sw_ring_v = NULL;
224         }
225 }
226
227 static inline void
228 _ixgbe_reset_tx_queue_vec(struct ixgbe_tx_queue *txq)
229 {
230         static const union ixgbe_adv_tx_desc zeroed_desc = { { 0 } };
231         struct ixgbe_tx_entry_v *txe = txq->sw_ring_v;
232         uint16_t i;
233
234         /* Zero out HW ring memory */
235         for (i = 0; i < txq->nb_tx_desc; i++)
236                 txq->tx_ring[i] = zeroed_desc;
237
238         /* Initialize SW ring entries */
239         for (i = 0; i < txq->nb_tx_desc; i++) {
240                 volatile union ixgbe_adv_tx_desc *txd = &txq->tx_ring[i];
241
242                 txd->wb.status = IXGBE_TXD_STAT_DD;
243                 txe[i].mbuf = NULL;
244         }
245
246         txq->tx_next_dd = (uint16_t)(txq->tx_rs_thresh - 1);
247         txq->tx_next_rs = (uint16_t)(txq->tx_rs_thresh - 1);
248
249         txq->tx_tail = 0;
250         txq->nb_tx_used = 0;
251         /*
252          * Always allow 1 descriptor to be un-allocated to avoid
253          * a H/W race condition
254          */
255         txq->last_desc_cleaned = (uint16_t)(txq->nb_tx_desc - 1);
256         txq->nb_tx_free = (uint16_t)(txq->nb_tx_desc - 1);
257         txq->ctx_curr = 0;
258         memset((void *)&txq->ctx_cache, 0,
259                 IXGBE_CTX_NUM * sizeof(struct ixgbe_advctx_info));
260 }
261
262 static inline int
263 ixgbe_rxq_vec_setup_default(struct ixgbe_rx_queue *rxq)
264 {
265         uintptr_t p;
266         struct rte_mbuf mb_def = { .buf_addr = 0 }; /* zeroed mbuf */
267
268         mb_def.nb_segs = 1;
269         mb_def.data_off = RTE_PKTMBUF_HEADROOM;
270         mb_def.port = rxq->port_id;
271         rte_mbuf_refcnt_set(&mb_def, 1);
272
273         /* prevent compiler reordering: rearm_data covers previous fields */
274         rte_compiler_barrier();
275         p = (uintptr_t)&mb_def.rearm_data;
276         rxq->mbuf_initializer = *(uint64_t *)p;
277         return 0;
278 }
279
280 static inline int
281 ixgbe_txq_vec_setup_default(struct ixgbe_tx_queue *txq,
282                             const struct ixgbe_txq_ops *txq_ops)
283 {
284         if (txq->sw_ring_v == NULL)
285                 return -1;
286
287         /* leave the first one for overflow */
288         txq->sw_ring_v = txq->sw_ring_v + 1;
289         txq->ops = txq_ops;
290
291         return 0;
292 }
293
294 static inline int
295 ixgbe_rx_vec_dev_conf_condition_check_default(struct rte_eth_dev *dev)
296 {
297 #ifndef RTE_LIBRTE_IEEE1588
298         struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
299         struct rte_fdir_conf *fconf = &dev->data->dev_conf.fdir_conf;
300
301 #ifndef RTE_IXGBE_RX_OLFLAGS_ENABLE
302         /* whithout rx ol_flags, no VP flag report */
303         if (rxmode->hw_vlan_strip != 0 ||
304             rxmode->hw_vlan_extend != 0)
305                 return -1;
306 #endif
307
308         /* no fdir support */
309         if (fconf->mode != RTE_FDIR_MODE_NONE)
310                 return -1;
311
312         /*
313          * - no csum error report support
314          * - no header split support
315          */
316         if (rxmode->hw_ip_checksum == 1 ||
317             rxmode->header_split == 1)
318                 return -1;
319
320         return 0;
321 #else
322         RTE_SET_USED(dev);
323         return -1;
324 #endif
325 }
326 #endif