4 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
33 #include <rte_ethdev.h>
34 #include <rte_common.h>
36 #include "base/fm10k_type.h"
38 #ifdef RTE_PMD_PACKET_PREFETCH
39 #define rte_packet_prefetch(p) rte_prefetch1(p)
41 #define rte_packet_prefetch(p) do {} while (0)
44 static inline void dump_rxd(union fm10k_rx_desc *rxd)
46 #ifndef RTE_LIBRTE_FM10K_DEBUG_RX
49 PMD_RX_LOG(DEBUG, "+----------------|----------------+");
50 PMD_RX_LOG(DEBUG, "| GLORT | PKT HDR & TYPE |");
51 PMD_RX_LOG(DEBUG, "| 0x%08x | 0x%08x |", rxd->d.glort,
53 PMD_RX_LOG(DEBUG, "+----------------|----------------+");
54 PMD_RX_LOG(DEBUG, "| VLAN & LEN | STATUS |");
55 PMD_RX_LOG(DEBUG, "| 0x%08x | 0x%08x |", rxd->d.vlan_len,
57 PMD_RX_LOG(DEBUG, "+----------------|----------------+");
58 PMD_RX_LOG(DEBUG, "| RESERVED | RSS_HASH |");
59 PMD_RX_LOG(DEBUG, "| 0x%08x | 0x%08x |", 0, rxd->d.rss);
60 PMD_RX_LOG(DEBUG, "+----------------|----------------+");
61 PMD_RX_LOG(DEBUG, "| TIME TAG |");
62 PMD_RX_LOG(DEBUG, "| 0x%016lx |", rxd->q.timestamp);
63 PMD_RX_LOG(DEBUG, "+----------------|----------------+");
67 rx_desc_to_ol_flags(struct rte_mbuf *m, const union fm10k_rx_desc *d)
70 static const uint16_t pt_lut[] = { 0,
71 PKT_RX_IPV4_HDR, PKT_RX_IPV4_HDR_EXT,
72 PKT_RX_IPV6_HDR, PKT_RX_IPV6_HDR_EXT,
76 if (d->w.pkt_info & FM10K_RXD_RSSTYPE_MASK)
77 m->ol_flags |= PKT_RX_RSS_HASH;
79 if (unlikely((d->d.staterr &
80 (FM10K_RXD_STATUS_IPCS | FM10K_RXD_STATUS_IPE)) ==
81 (FM10K_RXD_STATUS_IPCS | FM10K_RXD_STATUS_IPE)))
82 m->ol_flags |= PKT_RX_IP_CKSUM_BAD;
84 if (unlikely((d->d.staterr &
85 (FM10K_RXD_STATUS_L4CS | FM10K_RXD_STATUS_L4E)) ==
86 (FM10K_RXD_STATUS_L4CS | FM10K_RXD_STATUS_L4E)))
87 m->ol_flags |= PKT_RX_L4_CKSUM_BAD;
89 if (d->d.staterr & FM10K_RXD_STATUS_VEXT)
90 m->ol_flags |= PKT_RX_VLAN_PKT;
92 if (unlikely(d->d.staterr & FM10K_RXD_STATUS_HBO))
93 m->ol_flags |= PKT_RX_HBUF_OVERFLOW;
95 if (unlikely(d->d.staterr & FM10K_RXD_STATUS_RXE))
96 m->ol_flags |= PKT_RX_RECIP_ERR;
98 ptype = (d->d.data & FM10K_RXD_PKTTYPE_MASK_L3) >>
99 FM10K_RXD_PKTTYPE_SHIFT;
100 m->ol_flags |= pt_lut[(uint8_t)ptype];
104 fm10k_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
107 struct rte_mbuf *mbuf;
108 union fm10k_rx_desc desc;
109 struct fm10k_rx_queue *q = rx_queue;
115 next_dd = q->next_dd;
117 nb_pkts = RTE_MIN(nb_pkts, q->alloc_thresh);
118 for (count = 0; count < nb_pkts; ++count) {
119 mbuf = q->sw_ring[next_dd];
120 desc = q->hw_ring[next_dd];
121 if (!(desc.d.staterr & FM10K_RXD_STATUS_DD))
123 #ifdef RTE_LIBRTE_FM10K_DEBUG_RX
126 rte_pktmbuf_pkt_len(mbuf) = desc.w.length;
127 rte_pktmbuf_data_len(mbuf) = desc.w.length;
130 #ifdef RTE_LIBRTE_FM10K_RX_OLFLAGS_ENABLE
131 rx_desc_to_ol_flags(mbuf, &desc);
134 mbuf->hash.rss = desc.d.rss;
136 rx_pkts[count] = mbuf;
137 if (++next_dd == q->nb_desc) {
142 /* Prefetch next mbuf while processing current one. */
143 rte_prefetch0(q->sw_ring[next_dd]);
146 * When next RX descriptor is on a cache-line boundary,
147 * prefetch the next 4 RX descriptors and the next 8 pointers
150 if ((next_dd & 0x3) == 0) {
151 rte_prefetch0(&q->hw_ring[next_dd]);
152 rte_prefetch0(&q->sw_ring[next_dd]);
156 q->next_dd = next_dd;
158 if ((q->next_dd > q->next_trigger) || (alloc == 1)) {
159 ret = rte_mempool_get_bulk(q->mp,
160 (void **)&q->sw_ring[q->next_alloc],
163 if (unlikely(ret != 0)) {
164 uint8_t port = q->port_id;
165 PMD_RX_LOG(ERR, "Failed to alloc mbuf");
167 * Need to restore next_dd if we cannot allocate new
168 * buffers to replenish the old ones.
170 q->next_dd = (q->next_dd + q->nb_desc - count) %
172 rte_eth_devices[port].data->rx_mbuf_alloc_failed++;
176 for (; q->next_alloc <= q->next_trigger; ++q->next_alloc) {
177 mbuf = q->sw_ring[q->next_alloc];
179 /* setup static mbuf fields */
180 fm10k_pktmbuf_reset(mbuf, q->port_id);
182 /* write descriptor */
183 desc.q.pkt_addr = MBUF_DMA_ADDR_DEFAULT(mbuf);
184 desc.q.hdr_addr = MBUF_DMA_ADDR_DEFAULT(mbuf);
185 q->hw_ring[q->next_alloc] = desc;
187 FM10K_PCI_REG_WRITE(q->tail_ptr, q->next_trigger);
188 q->next_trigger += q->alloc_thresh;
189 if (q->next_trigger >= q->nb_desc) {
190 q->next_trigger = q->alloc_thresh - 1;
198 static inline void tx_free_descriptors(struct fm10k_tx_queue *q)
200 uint16_t next_rs, count = 0;
202 next_rs = fifo_peek(&q->rs_tracker);
203 if (!(q->hw_ring[next_rs].flags & FM10K_TXD_FLAG_DONE))
206 /* the DONE flag is set on this descriptor so remove the ID
207 * from the RS bit tracker and free the buffers */
208 fifo_remove(&q->rs_tracker);
210 /* wrap around? if so, free buffers from last_free up to but NOT
211 * including nb_desc */
212 if (q->last_free > next_rs) {
213 count = q->nb_desc - q->last_free;
214 while (q->last_free < q->nb_desc) {
215 rte_pktmbuf_free_seg(q->sw_ring[q->last_free]);
216 q->sw_ring[q->last_free] = NULL;
222 /* adjust free descriptor count before the next loop */
223 q->nb_free += count + (next_rs + 1 - q->last_free);
225 /* free buffers from last_free, up to and including next_rs */
226 while (q->last_free <= next_rs) {
227 rte_pktmbuf_free_seg(q->sw_ring[q->last_free]);
228 q->sw_ring[q->last_free] = NULL;
232 if (q->last_free == q->nb_desc)
236 static inline void tx_xmit_pkt(struct fm10k_tx_queue *q, struct rte_mbuf *mb)
241 /* always set the LAST flag on the last descriptor used to
242 * transmit the packet */
243 flags = FM10K_TXD_FLAG_LAST;
244 last_id = q->next_free + mb->nb_segs - 1;
245 if (last_id >= q->nb_desc)
246 last_id = last_id - q->nb_desc;
248 /* but only set the RS flag on the last descriptor if rs_thresh
249 * descriptors will be used since the RS flag was last set */
250 if ((q->nb_used + mb->nb_segs) >= q->rs_thresh) {
251 flags |= FM10K_TXD_FLAG_RS;
252 fifo_insert(&q->rs_tracker, last_id);
255 q->nb_used = q->nb_used + mb->nb_segs;
258 q->hw_ring[last_id].flags = flags;
259 q->nb_free -= mb->nb_segs;
261 /* set checksum flags on first descriptor of packet. SCTP checksum
262 * offload is not supported, but we do not explicitly check for this
263 * case in favor of greatly simplified processing. */
264 if (mb->ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_L4_MASK))
265 q->hw_ring[q->next_free].flags |= FM10K_TXD_FLAG_CSUM;
267 /* set vlan if requested */
268 if (mb->ol_flags & PKT_TX_VLAN_PKT)
269 q->hw_ring[q->next_free].vlan = mb->vlan_tci;
271 /* fill up the rings */
272 for (; mb != NULL; mb = mb->next) {
273 q->sw_ring[q->next_free] = mb;
274 q->hw_ring[q->next_free].buffer_addr =
275 rte_cpu_to_le_64(MBUF_DMA_ADDR(mb));
276 q->hw_ring[q->next_free].buflen =
277 rte_cpu_to_le_16(rte_pktmbuf_data_len(mb));
278 if (++q->next_free == q->nb_desc)
284 fm10k_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
287 struct fm10k_tx_queue *q = tx_queue;
291 for (count = 0; count < nb_pkts; ++count) {
294 /* running low on descriptors? try to free some... */
295 if (q->nb_free < q->free_trigger)
296 tx_free_descriptors(q);
298 /* make sure there are enough free descriptors to transmit the
299 * entire packet before doing anything */
300 if (q->nb_free < mb->nb_segs)
303 /* sanity check to make sure the mbuf is valid */
304 if ((mb->nb_segs == 0) ||
305 ((mb->nb_segs > 1) && (mb->next == NULL)))
308 /* process the packet */
312 /* update the tail pointer if any packets were processed */
313 if (likely(count > 0))
314 FM10K_PCI_REG_WRITE(q->tail_ptr, q->next_free);