4 * Copyright (C) Cavium networks Ltd. 2016.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Cavium networks nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <rte_atomic.h>
39 #include <rte_branch_prediction.h>
40 #include <rte_byteorder.h>
41 #include <rte_common.h>
42 #include <rte_cycles.h>
43 #include <rte_errno.h>
44 #include <rte_ethdev.h>
45 #include <rte_ether.h>
48 #include <rte_prefetch.h>
50 #include "base/nicvf_plat.h"
52 #include "nicvf_ethdev.h"
53 #include "nicvf_rxtx.h"
54 #include "nicvf_logs.h"
56 static inline void __hot
57 fill_sq_desc_header(union sq_entry_t *entry, struct rte_mbuf *pkt)
59 /* Local variable sqe to avoid read from sq desc memory*/
63 /* Fill SQ header descriptor */
65 sqe.hdr.subdesc_type = SQ_DESC_TYPE_HEADER;
66 /* Number of sub-descriptors following this one */
67 sqe.hdr.subdesc_cnt = pkt->nb_segs;
68 sqe.hdr.tot_len = pkt->pkt_len;
70 ol_flags = pkt->ol_flags & NICVF_TX_OFFLOAD_MASK;
71 if (unlikely(ol_flags)) {
73 if (ol_flags & PKT_TX_TCP_CKSUM)
74 sqe.hdr.csum_l4 = SEND_L4_CSUM_TCP;
75 else if (ol_flags & PKT_TX_UDP_CKSUM)
76 sqe.hdr.csum_l4 = SEND_L4_CSUM_UDP;
78 sqe.hdr.csum_l4 = SEND_L4_CSUM_DISABLE;
79 sqe.hdr.l4_offset = pkt->l3_len + pkt->l2_len;
82 if (ol_flags & PKT_TX_IP_CKSUM) {
84 sqe.hdr.l3_offset = pkt->l2_len;
88 entry->buff[0] = sqe.buff[0];
92 nicvf_single_pool_free_xmited_buffers(struct nicvf_txq *sq)
96 uint32_t head = sq->head;
97 struct rte_mbuf **txbuffs = sq->txbuffs;
98 void *obj_p[NICVF_MAX_TX_FREE_THRESH] __rte_cache_aligned;
100 curr_head = nicvf_addr_read(sq->sq_head) >> 4;
101 while (head != curr_head) {
103 obj_p[j++] = txbuffs[head];
105 head = (head + 1) & sq->qlen_mask;
108 rte_mempool_put_bulk(sq->pool, obj_p, j);
109 sq->head = curr_head;
111 NICVF_TX_ASSERT(sq->xmit_bufs >= 0);
115 nicvf_multi_pool_free_xmited_buffers(struct nicvf_txq *sq)
119 uint32_t head = sq->head;
120 struct rte_mbuf **txbuffs = sq->txbuffs;
122 curr_head = nicvf_addr_read(sq->sq_head) >> 4;
123 while (head != curr_head) {
125 rte_pktmbuf_free_seg(txbuffs[head]);
129 head = (head + 1) & sq->qlen_mask;
132 sq->head = curr_head;
134 NICVF_TX_ASSERT(sq->xmit_bufs >= 0);
137 static inline uint32_t __hot
138 nicvf_free_tx_desc(struct nicvf_txq *sq)
140 return ((sq->head - sq->tail - 1) & sq->qlen_mask);
143 /* Send Header + Packet */
144 #define TX_DESC_PER_PKT 2
146 static inline uint32_t __hot
147 nicvf_free_xmitted_buffers(struct nicvf_txq *sq, struct rte_mbuf **tx_pkts,
150 uint32_t free_desc = nicvf_free_tx_desc(sq);
152 if (free_desc < nb_pkts * TX_DESC_PER_PKT ||
153 sq->xmit_bufs > sq->tx_free_thresh) {
154 if (unlikely(sq->pool == NULL))
155 sq->pool = tx_pkts[0]->pool;
158 /* Freed now, let see the number of free descs again */
159 free_desc = nicvf_free_tx_desc(sq);
165 nicvf_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
170 struct nicvf_txq *sq = tx_queue;
171 union sq_entry_t *desc_ptr = sq->desc;
172 struct rte_mbuf **txbuffs = sq->txbuffs;
173 struct rte_mbuf *pkt;
174 uint32_t qlen_mask = sq->qlen_mask;
177 free_desc = nicvf_free_xmitted_buffers(sq, tx_pkts, nb_pkts);
179 for (i = 0; i < nb_pkts && (int)free_desc >= TX_DESC_PER_PKT; i++) {
182 txbuffs[tail] = NULL;
183 fill_sq_desc_header(desc_ptr + tail, pkt);
184 tail = (tail + 1) & qlen_mask;
187 fill_sq_desc_gather(desc_ptr + tail, pkt);
188 tail = (tail + 1) & qlen_mask;
189 free_desc -= TX_DESC_PER_PKT;
196 /* Inform HW to xmit the packets */
197 nicvf_addr_write(sq->sq_door, i * TX_DESC_PER_PKT);
202 nicvf_xmit_pkts_multiseg(void *tx_queue, struct rte_mbuf **tx_pkts,
206 uint32_t used_desc, next_used_desc, used_bufs, free_desc, tail;
207 struct nicvf_txq *sq = tx_queue;
208 union sq_entry_t *desc_ptr = sq->desc;
209 struct rte_mbuf **txbuffs = sq->txbuffs;
210 struct rte_mbuf *pkt, *seg;
211 uint32_t qlen_mask = sq->qlen_mask;
218 free_desc = nicvf_free_xmitted_buffers(sq, tx_pkts, nb_pkts);
220 for (i = 0; i < nb_pkts; i++) {
223 nb_segs = pkt->nb_segs;
225 next_used_desc = used_desc + nb_segs + 1;
226 if (next_used_desc > free_desc)
228 used_desc = next_used_desc;
229 used_bufs += nb_segs;
231 txbuffs[tail] = NULL;
232 fill_sq_desc_header(desc_ptr + tail, pkt);
233 tail = (tail + 1) & qlen_mask;
236 fill_sq_desc_gather(desc_ptr + tail, pkt);
237 tail = (tail + 1) & qlen_mask;
240 for (k = 1; k < nb_segs; k++) {
242 fill_sq_desc_gather(desc_ptr + tail, seg);
243 tail = (tail + 1) & qlen_mask;
249 sq->xmit_bufs += used_bufs;
252 /* Inform HW to xmit the packets */
253 nicvf_addr_write(sq->sq_door, used_desc);