1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2013-2016 Intel Corporation
7 #include <ethdev_driver.h>
8 #include <rte_common.h>
11 #include "base/fm10k_type.h"
13 #ifdef RTE_PMD_PACKET_PREFETCH
14 #define rte_packet_prefetch(p) rte_prefetch1(p)
16 #define rte_packet_prefetch(p) do {} while (0)
19 #ifdef RTE_ETHDEV_DEBUG_RX
20 static inline void dump_rxd(union fm10k_rx_desc *rxd)
22 PMD_RX_LOG(DEBUG, "+----------------|----------------+");
23 PMD_RX_LOG(DEBUG, "| GLORT | PKT HDR & TYPE |");
24 PMD_RX_LOG(DEBUG, "| 0x%08x | 0x%08x |", rxd->d.glort,
26 PMD_RX_LOG(DEBUG, "+----------------|----------------+");
27 PMD_RX_LOG(DEBUG, "| VLAN & LEN | STATUS |");
28 PMD_RX_LOG(DEBUG, "| 0x%08x | 0x%08x |", rxd->d.vlan_len,
30 PMD_RX_LOG(DEBUG, "+----------------|----------------+");
31 PMD_RX_LOG(DEBUG, "| RESERVED | RSS_HASH |");
32 PMD_RX_LOG(DEBUG, "| 0x%08x | 0x%08x |", 0, rxd->d.rss);
33 PMD_RX_LOG(DEBUG, "+----------------|----------------+");
34 PMD_RX_LOG(DEBUG, "| TIME TAG |");
35 PMD_RX_LOG(DEBUG, "| 0x%016"PRIx64" |", rxd->q.timestamp);
36 PMD_RX_LOG(DEBUG, "+----------------|----------------+");
40 #define FM10K_TX_OFFLOAD_MASK (RTE_MBUF_F_TX_VLAN | \
41 RTE_MBUF_F_TX_IPV6 | \
42 RTE_MBUF_F_TX_IPV4 | \
43 RTE_MBUF_F_TX_IP_CKSUM | \
44 RTE_MBUF_F_TX_L4_MASK | \
45 RTE_MBUF_F_TX_TCP_SEG)
47 #define FM10K_TX_OFFLOAD_NOTSUP_MASK \
48 (RTE_MBUF_F_TX_OFFLOAD_MASK ^ FM10K_TX_OFFLOAD_MASK)
50 /* @note: When this function is changed, make corresponding change to
51 * fm10k_dev_supported_ptypes_get()
54 rx_desc_to_ol_flags(struct rte_mbuf *m, const union fm10k_rx_desc *d)
57 ptype_table[FM10K_RXD_PKTTYPE_MASK >> FM10K_RXD_PKTTYPE_SHIFT]
58 __rte_cache_aligned = {
59 [FM10K_PKTTYPE_OTHER] = RTE_PTYPE_L2_ETHER,
60 [FM10K_PKTTYPE_IPV4] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4,
61 [FM10K_PKTTYPE_IPV4_EX] = RTE_PTYPE_L2_ETHER |
62 RTE_PTYPE_L3_IPV4_EXT,
63 [FM10K_PKTTYPE_IPV6] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6,
64 [FM10K_PKTTYPE_IPV6_EX] = RTE_PTYPE_L2_ETHER |
65 RTE_PTYPE_L3_IPV6_EXT,
66 [FM10K_PKTTYPE_IPV4 | FM10K_PKTTYPE_TCP] = RTE_PTYPE_L2_ETHER |
67 RTE_PTYPE_L3_IPV4 | RTE_PTYPE_L4_TCP,
68 [FM10K_PKTTYPE_IPV6 | FM10K_PKTTYPE_TCP] = RTE_PTYPE_L2_ETHER |
69 RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_TCP,
70 [FM10K_PKTTYPE_IPV4 | FM10K_PKTTYPE_UDP] = RTE_PTYPE_L2_ETHER |
71 RTE_PTYPE_L3_IPV4 | RTE_PTYPE_L4_UDP,
72 [FM10K_PKTTYPE_IPV6 | FM10K_PKTTYPE_UDP] = RTE_PTYPE_L2_ETHER |
73 RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_UDP,
76 m->packet_type = ptype_table[(d->w.pkt_info & FM10K_RXD_PKTTYPE_MASK)
77 >> FM10K_RXD_PKTTYPE_SHIFT];
79 if (d->w.pkt_info & FM10K_RXD_RSSTYPE_MASK)
80 m->ol_flags |= RTE_MBUF_F_RX_RSS_HASH;
82 if (unlikely((d->d.staterr &
83 (FM10K_RXD_STATUS_IPCS | FM10K_RXD_STATUS_IPE)) ==
84 (FM10K_RXD_STATUS_IPCS | FM10K_RXD_STATUS_IPE)))
85 m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_BAD;
87 m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_GOOD;
89 if (unlikely((d->d.staterr &
90 (FM10K_RXD_STATUS_L4CS | FM10K_RXD_STATUS_L4E)) ==
91 (FM10K_RXD_STATUS_L4CS | FM10K_RXD_STATUS_L4E)))
92 m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_BAD;
94 m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_GOOD;
98 fm10k_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
101 struct rte_mbuf *mbuf;
102 union fm10k_rx_desc desc;
103 struct fm10k_rx_queue *q = rx_queue;
109 next_dd = q->next_dd;
111 nb_pkts = RTE_MIN(nb_pkts, q->alloc_thresh);
112 for (count = 0; count < nb_pkts; ++count) {
113 if (!(q->hw_ring[next_dd].d.staterr & FM10K_RXD_STATUS_DD))
115 mbuf = q->sw_ring[next_dd];
116 desc = q->hw_ring[next_dd];
117 #ifdef RTE_ETHDEV_DEBUG_RX
120 rte_pktmbuf_pkt_len(mbuf) = desc.w.length;
121 rte_pktmbuf_data_len(mbuf) = desc.w.length;
124 #ifdef RTE_LIBRTE_FM10K_RX_OLFLAGS_ENABLE
125 rx_desc_to_ol_flags(mbuf, &desc);
128 mbuf->hash.rss = desc.d.rss;
130 * Packets in fm10k device always carry at least one VLAN tag.
131 * For those packets coming in without VLAN tag,
132 * the port default VLAN tag will be used.
133 * So, always RTE_MBUF_F_RX_VLAN flag is set and vlan_tci
134 * is valid for each RX packet's mbuf.
136 mbuf->ol_flags |= RTE_MBUF_F_RX_VLAN | RTE_MBUF_F_RX_VLAN_STRIPPED;
137 mbuf->vlan_tci = desc.w.vlan;
139 * mbuf->vlan_tci_outer is an idle field in fm10k driver,
140 * so it can be selected to store sglort value.
143 mbuf->vlan_tci_outer = rte_le_to_cpu_16(desc.w.sglort);
145 rx_pkts[count] = mbuf;
146 if (++next_dd == q->nb_desc) {
151 /* Prefetch next mbuf while processing current one. */
152 rte_prefetch0(q->sw_ring[next_dd]);
155 * When next RX descriptor is on a cache-line boundary,
156 * prefetch the next 4 RX descriptors and the next 8 pointers
159 if ((next_dd & 0x3) == 0) {
160 rte_prefetch0(&q->hw_ring[next_dd]);
161 rte_prefetch0(&q->sw_ring[next_dd]);
165 q->next_dd = next_dd;
167 if ((q->next_dd > q->next_trigger) || (alloc == 1)) {
168 ret = rte_mempool_get_bulk(q->mp,
169 (void **)&q->sw_ring[q->next_alloc],
172 if (unlikely(ret != 0)) {
173 uint16_t port = q->port_id;
174 PMD_RX_LOG(ERR, "Failed to alloc mbuf");
176 * Need to restore next_dd if we cannot allocate new
177 * buffers to replenish the old ones.
179 q->next_dd = (q->next_dd + q->nb_desc - count) %
181 rte_eth_devices[port].data->rx_mbuf_alloc_failed++;
185 for (; q->next_alloc <= q->next_trigger; ++q->next_alloc) {
186 mbuf = q->sw_ring[q->next_alloc];
188 /* setup static mbuf fields */
189 fm10k_pktmbuf_reset(mbuf, q->port_id);
191 /* write descriptor */
192 desc.q.pkt_addr = MBUF_DMA_ADDR_DEFAULT(mbuf);
193 desc.q.hdr_addr = MBUF_DMA_ADDR_DEFAULT(mbuf);
194 q->hw_ring[q->next_alloc] = desc;
196 FM10K_PCI_REG_WRITE(q->tail_ptr, q->next_trigger);
197 q->next_trigger += q->alloc_thresh;
198 if (q->next_trigger >= q->nb_desc) {
199 q->next_trigger = q->alloc_thresh - 1;
208 fm10k_recv_scattered_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
211 struct rte_mbuf *mbuf;
212 union fm10k_rx_desc desc;
213 struct fm10k_rx_queue *q = rx_queue;
215 uint16_t nb_rcv, nb_seg;
218 struct rte_mbuf *first_seg = q->pkt_first_seg;
219 struct rte_mbuf *last_seg = q->pkt_last_seg;
222 next_dd = q->next_dd;
225 nb_seg = RTE_MIN(nb_pkts, q->alloc_thresh);
226 for (count = 0; count < nb_seg; count++) {
227 if (!(q->hw_ring[next_dd].d.staterr & FM10K_RXD_STATUS_DD))
229 mbuf = q->sw_ring[next_dd];
230 desc = q->hw_ring[next_dd];
231 #ifdef RTE_ETHDEV_DEBUG_RX
235 if (++next_dd == q->nb_desc) {
240 /* Prefetch next mbuf while processing current one. */
241 rte_prefetch0(q->sw_ring[next_dd]);
244 * When next RX descriptor is on a cache-line boundary,
245 * prefetch the next 4 RX descriptors and the next 8 pointers
248 if ((next_dd & 0x3) == 0) {
249 rte_prefetch0(&q->hw_ring[next_dd]);
250 rte_prefetch0(&q->sw_ring[next_dd]);
253 /* Fill data length */
254 rte_pktmbuf_data_len(mbuf) = desc.w.length;
257 * If this is the first buffer of the received packet,
258 * set the pointer to the first mbuf of the packet and
259 * initialize its context.
260 * Otherwise, update the total length and the number of segments
261 * of the current scattered packet, and update the pointer to
262 * the last mbuf of the current packet.
266 first_seg->pkt_len = desc.w.length;
269 (uint16_t)(first_seg->pkt_len +
270 rte_pktmbuf_data_len(mbuf));
271 first_seg->nb_segs++;
272 last_seg->next = mbuf;
276 * If this is not the last buffer of the received packet,
277 * update the pointer to the last mbuf of the current scattered
278 * packet and continue to parse the RX ring.
280 if (!(desc.d.staterr & FM10K_RXD_STATUS_EOP)) {
285 first_seg->ol_flags = 0;
286 #ifdef RTE_LIBRTE_FM10K_RX_OLFLAGS_ENABLE
287 rx_desc_to_ol_flags(first_seg, &desc);
289 first_seg->hash.rss = desc.d.rss;
291 * Packets in fm10k device always carry at least one VLAN tag.
292 * For those packets coming in without VLAN tag,
293 * the port default VLAN tag will be used.
294 * So, always RTE_MBUF_F_RX_VLAN flag is set and vlan_tci
295 * is valid for each RX packet's mbuf.
297 first_seg->ol_flags |= RTE_MBUF_F_RX_VLAN | RTE_MBUF_F_RX_VLAN_STRIPPED;
298 first_seg->vlan_tci = desc.w.vlan;
300 * mbuf->vlan_tci_outer is an idle field in fm10k driver,
301 * so it can be selected to store sglort value.
304 first_seg->vlan_tci_outer =
305 rte_le_to_cpu_16(desc.w.sglort);
307 /* Prefetch data of first segment, if configured to do so. */
308 rte_packet_prefetch((char *)first_seg->buf_addr +
309 first_seg->data_off);
312 * Store the mbuf address into the next entry of the array
313 * of returned packets.
315 rx_pkts[nb_rcv++] = first_seg;
318 * Setup receipt context for a new packet.
323 q->next_dd = next_dd;
325 if ((q->next_dd > q->next_trigger) || (alloc == 1)) {
326 ret = rte_mempool_get_bulk(q->mp,
327 (void **)&q->sw_ring[q->next_alloc],
330 if (unlikely(ret != 0)) {
331 uint16_t port = q->port_id;
332 PMD_RX_LOG(ERR, "Failed to alloc mbuf");
334 * Need to restore next_dd if we cannot allocate new
335 * buffers to replenish the old ones.
337 q->next_dd = (q->next_dd + q->nb_desc - count) %
339 rte_eth_devices[port].data->rx_mbuf_alloc_failed++;
343 for (; q->next_alloc <= q->next_trigger; ++q->next_alloc) {
344 mbuf = q->sw_ring[q->next_alloc];
346 /* setup static mbuf fields */
347 fm10k_pktmbuf_reset(mbuf, q->port_id);
349 /* write descriptor */
350 desc.q.pkt_addr = MBUF_DMA_ADDR_DEFAULT(mbuf);
351 desc.q.hdr_addr = MBUF_DMA_ADDR_DEFAULT(mbuf);
352 q->hw_ring[q->next_alloc] = desc;
354 FM10K_PCI_REG_WRITE(q->tail_ptr, q->next_trigger);
355 q->next_trigger += q->alloc_thresh;
356 if (q->next_trigger >= q->nb_desc) {
357 q->next_trigger = q->alloc_thresh - 1;
362 q->pkt_first_seg = first_seg;
363 q->pkt_last_seg = last_seg;
369 fm10k_dev_rx_queue_count(void *rx_queue)
371 #define FM10K_RXQ_SCAN_INTERVAL 4
372 volatile union fm10k_rx_desc *rxdp;
373 struct fm10k_rx_queue *rxq;
377 rxdp = &rxq->hw_ring[rxq->next_dd];
378 while ((desc < rxq->nb_desc) &&
379 rxdp->w.status & rte_cpu_to_le_16(FM10K_RXD_STATUS_DD)) {
381 * Check the DD bit of a rx descriptor of each group of 4 desc,
382 * to avoid checking too frequently and downgrading performance
385 desc += FM10K_RXQ_SCAN_INTERVAL;
386 rxdp += FM10K_RXQ_SCAN_INTERVAL;
387 if (rxq->next_dd + desc >= rxq->nb_desc)
388 rxdp = &rxq->hw_ring[rxq->next_dd + desc -
396 fm10k_dev_rx_descriptor_status(void *rx_queue, uint16_t offset)
398 volatile union fm10k_rx_desc *rxdp;
399 struct fm10k_rx_queue *rxq = rx_queue;
400 uint16_t nb_hold, trigger_last;
404 if (unlikely(offset >= rxq->nb_desc)) {
405 PMD_DRV_LOG(ERR, "Invalid RX descriptor offset %u", offset);
409 if (rxq->next_trigger < rxq->alloc_thresh)
410 trigger_last = rxq->next_trigger +
411 rxq->nb_desc - rxq->alloc_thresh;
413 trigger_last = rxq->next_trigger - rxq->alloc_thresh;
415 if (rxq->next_dd < trigger_last)
416 nb_hold = rxq->next_dd + rxq->nb_desc - trigger_last;
418 nb_hold = rxq->next_dd - trigger_last;
420 if (offset >= rxq->nb_desc - nb_hold)
421 return RTE_ETH_RX_DESC_UNAVAIL;
423 desc = rxq->next_dd + offset;
424 if (desc >= rxq->nb_desc)
425 desc -= rxq->nb_desc;
427 rxdp = &rxq->hw_ring[desc];
429 ret = !!(rxdp->w.status &
430 rte_cpu_to_le_16(FM10K_RXD_STATUS_DD));
436 fm10k_dev_tx_descriptor_status(void *tx_queue, uint16_t offset)
438 volatile struct fm10k_tx_desc *txdp;
439 struct fm10k_tx_queue *txq = tx_queue;
441 uint16_t next_rs = txq->nb_desc;
442 struct fifo rs_tracker = txq->rs_tracker;
443 struct fifo *r = &rs_tracker;
445 if (unlikely(offset >= txq->nb_desc))
448 desc = txq->next_free + offset;
449 /* go to next desc that has the RS bit */
450 desc = (desc / txq->rs_thresh + 1) *
453 if (desc >= txq->nb_desc) {
454 desc -= txq->nb_desc;
455 if (desc >= txq->nb_desc)
456 desc -= txq->nb_desc;
460 for ( ; r->head != r->endp; ) {
461 if (*r->head >= desc && *r->head < next_rs)
466 txdp = &txq->hw_ring[next_rs];
467 if (txdp->flags & FM10K_TXD_FLAG_DONE)
468 return RTE_ETH_TX_DESC_DONE;
470 return RTE_ETH_TX_DESC_FULL;
474 * Free multiple TX mbuf at a time if they are in the same pool
476 * @txep: software desc ring index that starts to free
477 * @num: number of descs to free
480 static inline void tx_free_bulk_mbuf(struct rte_mbuf **txep, int num)
482 struct rte_mbuf *m, *free[RTE_FM10K_TX_MAX_FREE_BUF_SZ];
486 if (unlikely(num == 0))
489 m = rte_pktmbuf_prefree_seg(txep[0]);
490 if (likely(m != NULL)) {
493 for (i = 1; i < num; i++) {
494 m = rte_pktmbuf_prefree_seg(txep[i]);
495 if (likely(m != NULL)) {
496 if (likely(m->pool == free[0]->pool))
499 rte_mempool_put_bulk(free[0]->pool,
500 (void *)free, nb_free);
507 rte_mempool_put_bulk(free[0]->pool, (void **)free, nb_free);
509 for (i = 1; i < num; i++) {
510 m = rte_pktmbuf_prefree_seg(txep[i]);
512 rte_mempool_put(m->pool, m);
518 static inline void tx_free_descriptors(struct fm10k_tx_queue *q)
520 uint16_t next_rs, count = 0;
522 next_rs = fifo_peek(&q->rs_tracker);
523 if (!(q->hw_ring[next_rs].flags & FM10K_TXD_FLAG_DONE))
526 /* the DONE flag is set on this descriptor so remove the ID
527 * from the RS bit tracker and free the buffers */
528 fifo_remove(&q->rs_tracker);
530 /* wrap around? if so, free buffers from last_free up to but NOT
531 * including nb_desc */
532 if (q->last_free > next_rs) {
533 count = q->nb_desc - q->last_free;
534 tx_free_bulk_mbuf(&q->sw_ring[q->last_free], count);
538 /* adjust free descriptor count before the next loop */
539 q->nb_free += count + (next_rs + 1 - q->last_free);
541 /* free buffers from last_free, up to and including next_rs */
542 if (q->last_free <= next_rs) {
543 count = next_rs - q->last_free + 1;
544 tx_free_bulk_mbuf(&q->sw_ring[q->last_free], count);
545 q->last_free += count;
548 if (q->last_free == q->nb_desc)
552 static inline void tx_xmit_pkt(struct fm10k_tx_queue *q, struct rte_mbuf *mb)
555 uint8_t flags, hdrlen;
557 /* always set the LAST flag on the last descriptor used to
558 * transmit the packet */
559 flags = FM10K_TXD_FLAG_LAST;
560 last_id = q->next_free + mb->nb_segs - 1;
561 if (last_id >= q->nb_desc)
562 last_id = last_id - q->nb_desc;
564 /* but only set the RS flag on the last descriptor if rs_thresh
565 * descriptors will be used since the RS flag was last set */
566 if ((q->nb_used + mb->nb_segs) >= q->rs_thresh) {
567 flags |= FM10K_TXD_FLAG_RS;
568 fifo_insert(&q->rs_tracker, last_id);
571 q->nb_used = q->nb_used + mb->nb_segs;
574 q->nb_free -= mb->nb_segs;
576 q->hw_ring[q->next_free].flags = 0;
578 q->hw_ring[q->next_free].flags |= FM10K_TXD_FLAG_FTAG;
579 /* set checksum flags on first descriptor of packet. SCTP checksum
580 * offload is not supported, but we do not explicitly check for this
581 * case in favor of greatly simplified processing. */
582 if (mb->ol_flags & (RTE_MBUF_F_TX_IP_CKSUM | RTE_MBUF_F_TX_L4_MASK | RTE_MBUF_F_TX_TCP_SEG))
583 q->hw_ring[q->next_free].flags |= FM10K_TXD_FLAG_CSUM;
585 /* set vlan if requested */
586 if (mb->ol_flags & RTE_MBUF_F_TX_VLAN)
587 q->hw_ring[q->next_free].vlan = mb->vlan_tci;
589 q->hw_ring[q->next_free].vlan = 0;
591 q->sw_ring[q->next_free] = mb;
592 q->hw_ring[q->next_free].buffer_addr =
593 rte_cpu_to_le_64(MBUF_DMA_ADDR(mb));
594 q->hw_ring[q->next_free].buflen =
595 rte_cpu_to_le_16(rte_pktmbuf_data_len(mb));
597 if (mb->ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
598 hdrlen = mb->l2_len + mb->l3_len + mb->l4_len;
599 hdrlen += (mb->ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK) ?
600 mb->outer_l2_len + mb->outer_l3_len : 0;
601 if (q->hw_ring[q->next_free].flags & FM10K_TXD_FLAG_FTAG)
602 hdrlen += sizeof(struct fm10k_ftag);
604 if (likely((hdrlen >= FM10K_TSO_MIN_HEADERLEN) &&
605 (hdrlen <= FM10K_TSO_MAX_HEADERLEN) &&
606 (mb->tso_segsz >= FM10K_TSO_MINMSS))) {
607 q->hw_ring[q->next_free].mss = mb->tso_segsz;
608 q->hw_ring[q->next_free].hdrlen = hdrlen;
612 if (++q->next_free == q->nb_desc)
615 /* fill up the rings */
616 for (mb = mb->next; mb != NULL; mb = mb->next) {
617 q->sw_ring[q->next_free] = mb;
618 q->hw_ring[q->next_free].buffer_addr =
619 rte_cpu_to_le_64(MBUF_DMA_ADDR(mb));
620 q->hw_ring[q->next_free].buflen =
621 rte_cpu_to_le_16(rte_pktmbuf_data_len(mb));
622 q->hw_ring[q->next_free].flags = 0;
623 if (++q->next_free == q->nb_desc)
627 q->hw_ring[last_id].flags |= flags;
631 fm10k_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
634 struct fm10k_tx_queue *q = tx_queue;
638 for (count = 0; count < nb_pkts; ++count) {
641 /* running low on descriptors? try to free some... */
642 if (q->nb_free < q->free_thresh)
643 tx_free_descriptors(q);
645 /* make sure there are enough free descriptors to transmit the
646 * entire packet before doing anything */
647 if (q->nb_free < mb->nb_segs)
650 /* sanity check to make sure the mbuf is valid */
651 if ((mb->nb_segs == 0) ||
652 ((mb->nb_segs > 1) && (mb->next == NULL)))
655 /* process the packet */
659 /* update the tail pointer if any packets were processed */
660 if (likely(count > 0))
661 FM10K_PCI_REG_WRITE(q->tail_ptr, q->next_free);
667 fm10k_prep_pkts(__rte_unused void *tx_queue, struct rte_mbuf **tx_pkts,
673 for (i = 0; i < nb_pkts; i++) {
676 if ((m->ol_flags & RTE_MBUF_F_TX_TCP_SEG) &&
677 (m->tso_segsz < FM10K_TSO_MINMSS)) {
682 if (m->ol_flags & FM10K_TX_OFFLOAD_NOTSUP_MASK) {
687 #ifdef RTE_ETHDEV_DEBUG_TX
688 ret = rte_validate_tx_offload(m);
694 ret = rte_net_intel_cksum_prepare(m);