13d9b7210SChristian Ehrhardt/*-
23d9b7210SChristian Ehrhardt *   BSD LICENSE
33d9b7210SChristian Ehrhardt *
43d9b7210SChristian Ehrhardt *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
53d9b7210SChristian Ehrhardt *   All rights reserved.
63d9b7210SChristian Ehrhardt *
73d9b7210SChristian Ehrhardt *   Redistribution and use in source and binary forms, with or without
83d9b7210SChristian Ehrhardt *   modification, are permitted provided that the following conditions
93d9b7210SChristian Ehrhardt *   are met:
103d9b7210SChristian Ehrhardt *
113d9b7210SChristian Ehrhardt *     * Redistributions of source code must retain the above copyright
123d9b7210SChristian Ehrhardt *       notice, this list of conditions and the following disclaimer.
133d9b7210SChristian Ehrhardt *     * Redistributions in binary form must reproduce the above copyright
143d9b7210SChristian Ehrhardt *       notice, this list of conditions and the following disclaimer in
153d9b7210SChristian Ehrhardt *       the documentation and/or other materials provided with the
163d9b7210SChristian Ehrhardt *       distribution.
173d9b7210SChristian Ehrhardt *     * Neither the name of Intel Corporation nor the names of its
183d9b7210SChristian Ehrhardt *       contributors may be used to endorse or promote products derived
193d9b7210SChristian Ehrhardt *       from this software without specific prior written permission.
203d9b7210SChristian Ehrhardt *
213d9b7210SChristian Ehrhardt *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
223d9b7210SChristian Ehrhardt *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
233d9b7210SChristian Ehrhardt *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
243d9b7210SChristian Ehrhardt *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
253d9b7210SChristian Ehrhardt *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
263d9b7210SChristian Ehrhardt *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
273d9b7210SChristian Ehrhardt *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
283d9b7210SChristian Ehrhardt *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
293d9b7210SChristian Ehrhardt *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
303d9b7210SChristian Ehrhardt *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
313d9b7210SChristian Ehrhardt *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
323d9b7210SChristian Ehrhardt */
333d9b7210SChristian Ehrhardt
343d9b7210SChristian Ehrhardt#ifndef _I40E_RXTX_VEC_COMMON_H_
353d9b7210SChristian Ehrhardt#define _I40E_RXTX_VEC_COMMON_H_
363d9b7210SChristian Ehrhardt#include <stdint.h>
373d9b7210SChristian Ehrhardt#include <rte_ethdev.h>
383d9b7210SChristian Ehrhardt#include <rte_malloc.h>
393d9b7210SChristian Ehrhardt
403d9b7210SChristian Ehrhardt#include "i40e_ethdev.h"
413d9b7210SChristian Ehrhardt#include "i40e_rxtx.h"
423d9b7210SChristian Ehrhardt
433d9b7210SChristian Ehrhardtstatic inline uint16_t
443d9b7210SChristian Ehrhardtreassemble_packets(struct i40e_rx_queue *rxq, struct rte_mbuf **rx_bufs,
453d9b7210SChristian Ehrhardt		   uint16_t nb_bufs, uint8_t *split_flags)
463d9b7210SChristian Ehrhardt{
473d9b7210SChristian Ehrhardt	struct rte_mbuf *pkts[RTE_I40E_VPMD_RX_BURST]; /*finished pkts*/
483d9b7210SChristian Ehrhardt	struct rte_mbuf *start = rxq->pkt_first_seg;
493d9b7210SChristian Ehrhardt	struct rte_mbuf *end =  rxq->pkt_last_seg;
503d9b7210SChristian Ehrhardt	unsigned pkt_idx, buf_idx;
513d9b7210SChristian Ehrhardt
523d9b7210SChristian Ehrhardt	for (buf_idx = 0, pkt_idx = 0; buf_idx < nb_bufs; buf_idx++) {
533d9b7210SChristian Ehrhardt		if (end != NULL) {
543d9b7210SChristian Ehrhardt			/* processing a split packet */
553d9b7210SChristian Ehrhardt			end->next = rx_bufs[buf_idx];
563d9b7210SChristian Ehrhardt			rx_bufs[buf_idx]->data_len += rxq->crc_len;
573d9b7210SChristian Ehrhardt
583d9b7210SChristian Ehrhardt			start->nb_segs++;
593d9b7210SChristian Ehrhardt			start->pkt_len += rx_bufs[buf_idx]->data_len;
603d9b7210SChristian Ehrhardt			end = end->next;
613d9b7210SChristian Ehrhardt
623d9b7210SChristian Ehrhardt			if (!split_flags[buf_idx]) {
633d9b7210SChristian Ehrhardt				/* it's the last packet of the set */
643d9b7210SChristian Ehrhardt				start->hash = end->hash;
653d9b7210SChristian Ehrhardt				start->ol_flags = end->ol_flags;
663d9b7210SChristian Ehrhardt				/* we need to strip crc for the whole packet */
673d9b7210SChristian Ehrhardt				start->pkt_len -= rxq->crc_len;
683d9b7210SChristian Ehrhardt				if (end->data_len > rxq->crc_len) {
693d9b7210SChristian Ehrhardt					end->data_len -= rxq->crc_len;
703d9b7210SChristian Ehrhardt				} else {
713d9b7210SChristian Ehrhardt					/* free up last mbuf */
723d9b7210SChristian Ehrhardt					struct rte_mbuf *secondlast = start;
733d9b7210SChristian Ehrhardt
740c3ed7dcSChristian Ehrhardt					start->nb_segs--;
753d9b7210SChristian Ehrhardt					while (secondlast->next != end)
763d9b7210SChristian Ehrhardt						secondlast = secondlast->next;
773d9b7210SChristian Ehrhardt					secondlast->data_len -= (rxq->crc_len -
783d9b7210SChristian Ehrhardt							end->data_len);
793d9b7210SChristian Ehrhardt					secondlast->next = NULL;
803d9b7210SChristian Ehrhardt					rte_pktmbuf_free_seg(end);
813d9b7210SChristian Ehrhardt					end = secondlast;
823d9b7210SChristian Ehrhardt				}
833d9b7210SChristian Ehrhardt				pkts[pkt_idx++] = start;
843d9b7210SChristian Ehrhardt				start = end = NULL;
853d9b7210SChristian Ehrhardt			}
863d9b7210SChristian Ehrhardt		} else {
873d9b7210SChristian Ehrhardt			/* not processing a split packet */
883d9b7210SChristian Ehrhardt			if (!split_flags[buf_idx]) {
893d9b7210SChristian Ehrhardt				/* not a split packet, save and skip */
903d9b7210SChristian Ehrhardt				pkts[pkt_idx++] = rx_bufs[buf_idx];
913d9b7210SChristian Ehrhardt				continue;
923d9b7210SChristian Ehrhardt			}
933d9b7210SChristian Ehrhardt			end = start = rx_bufs[buf_idx];
943d9b7210SChristian Ehrhardt			rx_bufs[buf_idx]->data_len += rxq->crc_len;
953d9b7210SChristian Ehrhardt			rx_bufs[buf_idx]->pkt_len += rxq->crc_len;
963d9b7210SChristian Ehrhardt		}
973d9b7210SChristian Ehrhardt	}
983d9b7210SChristian Ehrhardt
993d9b7210SChristian Ehrhardt	/* save the partial packet for next time */
1003d9b7210SChristian Ehrhardt	rxq->pkt_first_seg = start;
1013d9b7210SChristian Ehrhardt	rxq->pkt_last_seg = end;
1023d9b7210SChristian Ehrhardt	memcpy(rx_bufs, pkts, pkt_idx * (sizeof(*pkts)));
1033d9b7210SChristian Ehrhardt	return pkt_idx;
1043d9b7210SChristian Ehrhardt}
1053d9b7210SChristian Ehrhardt
1063d9b7210SChristian Ehrhardtstatic inline int __attribute__((always_inline))
1073d9b7210SChristian Ehrhardti40e_tx_free_bufs(struct i40e_tx_queue *txq)
1083d9b7210SChristian Ehrhardt{
1093d9b7210SChristian Ehrhardt	struct i40e_tx_entry *txep;
1103d9b7210SChristian Ehrhardt	uint32_t n;
1113d9b7210SChristian Ehrhardt	uint32_t i;
1123d9b7210SChristian Ehrhardt	int nb_free = 0;
1133d9b7210SChristian Ehrhardt	struct rte_mbuf *m, *free[RTE_I40E_TX_MAX_FREE_BUF_SZ];
1143d9b7210SChristian Ehrhardt
1153d9b7210SChristian Ehrhardt	/* check DD bits on threshold descriptor */
1163d9b7210SChristian Ehrhardt	if ((txq->tx_ring[txq->tx_next_dd].cmd_type_offset_bsz &
1173d9b7210SChristian Ehrhardt			rte_cpu_to_le_64(I40E_TXD_QW1_DTYPE_MASK)) !=
1183d9b7210SChristian Ehrhardt			rte_cpu_to_le_64(I40E_TX_DESC_DTYPE_DESC_DONE))
1193d9b7210SChristian Ehrhardt		return 0;
1203d9b7210SChristian Ehrhardt
1213d9b7210SChristian Ehrhardt	n = txq->tx_rs_thresh;
1223d9b7210SChristian Ehrhardt
1233d9b7210SChristian Ehrhardt	 /* first buffer to free from S/W ring is at index
1243d9b7210SChristian Ehrhardt	  * tx_next_dd - (tx_rs_thresh-1)
1253d9b7210SChristian Ehrhardt	  */
1263d9b7210SChristian Ehrhardt	txep = &txq->sw_ring[txq->tx_next_dd - (n - 1)];
1273d9b7210SChristian Ehrhardt	m = __rte_pktmbuf_prefree_seg(txep[0].mbuf);
1283d9b7210SChristian Ehrhardt	if (likely(m != NULL)) {
1293d9b7210SChristian Ehrhardt		free[0] = m;
1303d9b7210SChristian Ehrhardt		nb_free = 1;
1313d9b7210SChristian Ehrhardt		for (i = 1; i < n; i++) {
1323d9b7210SChristian Ehrhardt			m = __rte_pktmbuf_prefree_seg(txep[i].mbuf);
1333d9b7210SChristian Ehrhardt			if (likely(m != NULL)) {
1343d9b7210SChristian Ehrhardt				if (likely(m->pool == free[0]->pool)) {
1353d9b7210SChristian Ehrhardt					free[nb_free++] = m;
1363d9b7210SChristian Ehrhardt				} else {
1373d9b7210SChristian Ehrhardt					rte_mempool_put_bulk(free[0]->pool,
1383d9b7210SChristian Ehrhardt							     (void *)free,
1393d9b7210SChristian Ehrhardt							     nb_free);
1403d9b7210SChristian Ehrhardt					free[0] = m;
1413d9b7210SChristian Ehrhardt					nb_free = 1;
1423d9b7210SChristian Ehrhardt				}
1433d9b7210SChristian Ehrhardt			}
1443d9b7210SChristian Ehrhardt		}
1453d9b7210SChristian Ehrhardt		rte_mempool_put_bulk(free[0]->pool, (void **)free, nb_free);
1463d9b7210SChristian Ehrhardt	} else {
1473d9b7210SChristian Ehrhardt		for (i = 1; i < n; i++) {
1483d9b7210SChristian Ehrhardt			m = __rte_pktmbuf_prefree_seg(txep[i].mbuf);
1493d9b7210SChristian Ehrhardt			if (m != NULL)
1503d9b7210SChristian Ehrhardt				rte_mempool_put(m->pool, m);
1513d9b7210SChristian Ehrhardt		}
1523d9b7210SChristian Ehrhardt	}
1533d9b7210SChristian Ehrhardt
1543d9b7210SChristian Ehrhardt	/* buffers were freed, update counters */
1553d9b7210SChristian Ehrhardt	txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + txq->tx_rs_thresh);
1563d9b7210SChristian Ehrhardt	txq->tx_next_dd = (uint16_t)(txq->tx_next_dd + txq->tx_rs_thresh);
1573d9b7210SChristian Ehrhardt	if (txq->tx_next_dd >= txq->nb_tx_desc)
1583d9b7210SChristian Ehrhardt		txq->tx_next_dd = (uint16_t)(txq->tx_rs_thresh - 1);
1593d9b7210SChristian Ehrhardt
1603d9b7210SChristian Ehrhardt	return txq->tx_rs_thresh;
1613d9b7210SChristian Ehrhardt}
1623d9b7210SChristian Ehrhardt
1633d9b7210SChristian Ehrhardtstatic inline void __attribute__((always_inline))
1643d9b7210SChristian Ehrhardttx_backlog_entry(struct i40e_tx_entry *txep,
1653d9b7210SChristian Ehrhardt		 struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1663d9b7210SChristian Ehrhardt{
1673d9b7210SChristian Ehrhardt	int i;
1683d9b7210SChristian Ehrhardt
1693d9b7210SChristian Ehrhardt	for (i = 0; i < (int)nb_pkts; ++i)
1703d9b7210SChristian Ehrhardt		txep[i].mbuf = tx_pkts[i];
1713d9b7210SChristian Ehrhardt}
1723d9b7210SChristian Ehrhardt
1733d9b7210SChristian Ehrhardtstatic inline void
1743d9b7210SChristian Ehrhardt_i40e_rx_queue_release_mbufs_vec(struct i40e_rx_queue *rxq)
1753d9b7210SChristian Ehrhardt{
1763d9b7210SChristian Ehrhardt	const unsigned mask = rxq->nb_rx_desc - 1;
1773d9b7210SChristian Ehrhardt	unsigned i;
1783d9b7210SChristian Ehrhardt
1793d9b7210SChristian Ehrhardt	if (rxq->sw_ring == NULL || rxq->rxrearm_nb >= rxq->nb_rx_desc)
1803d9b7210SChristian Ehrhardt		return;
1813d9b7210SChristian Ehrhardt
1823d9b7210SChristian Ehrhardt	/* free all mbufs that are valid in the ring */
1833d9b7210SChristian Ehrhardt	if (rxq->rxrearm_nb == 0) {
1843d9b7210SChristian Ehrhardt		for (i = 0; i < rxq->nb_rx_desc; i++) {
1853d9b7210SChristian Ehrhardt			if (rxq->sw_ring[i].mbuf != NULL)
1863d9b7210SChristian Ehrhardt				rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
1873d9b7210SChristian Ehrhardt		}
1883d9b7210SChristian Ehrhardt	} else {
1893d9b7210SChristian Ehrhardt		for (i = rxq->rx_tail;
1903d9b7210SChristian Ehrhardt		     i != rxq->rxrearm_start;
1913d9b7210SChristian Ehrhardt		     i = (i + 1) & mask) {
1923d9b7210SChristian Ehrhardt			if (rxq->sw_ring[i].mbuf != NULL)
1933d9b7210SChristian Ehrhardt				rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
1943d9b7210SChristian Ehrhardt		}
1953d9b7210SChristian Ehrhardt	}
1963d9b7210SChristian Ehrhardt
1973d9b7210SChristian Ehrhardt	rxq->rxrearm_nb = rxq->nb_rx_desc;
1983d9b7210SChristian Ehrhardt
1993d9b7210SChristian Ehrhardt	/* set all entries to NULL */
2003d9b7210SChristian Ehrhardt	memset(rxq->sw_ring, 0, sizeof(rxq->sw_ring[0]) * rxq->nb_rx_desc);
2013d9b7210SChristian Ehrhardt}
2023d9b7210SChristian Ehrhardt
2033d9b7210SChristian Ehrhardtstatic inline int
2043d9b7210SChristian Ehrhardti40e_rxq_vec_setup_default(struct i40e_rx_queue *rxq)
2053d9b7210SChristian Ehrhardt{
2063d9b7210SChristian Ehrhardt	uintptr_t p;
2073d9b7210SChristian Ehrhardt	struct rte_mbuf mb_def = { .buf_addr = 0 }; /* zeroed mbuf */
2083d9b7210SChristian Ehrhardt
2093d9b7210SChristian Ehrhardt	mb_def.nb_segs = 1;
2103d9b7210SChristian Ehrhardt	mb_def.data_off = RTE_PKTMBUF_HEADROOM;
2113d9b7210SChristian Ehrhardt	mb_def.port = rxq->port_id;
2123d9b7210SChristian Ehrhardt	rte_mbuf_refcnt_set(&mb_def, 1);
2133d9b7210SChristian Ehrhardt
2143d9b7210SChristian Ehrhardt	/* prevent compiler reordering: rearm_data covers previous fields */
2153d9b7210SChristian Ehrhardt	rte_compiler_barrier();
2163d9b7210SChristian Ehrhardt	p = (uintptr_t)&mb_def.rearm_data;
2173d9b7210SChristian Ehrhardt	rxq->mbuf_initializer = *(uint64_t *)p;
2183d9b7210SChristian Ehrhardt	return 0;
2193d9b7210SChristian Ehrhardt}
2203d9b7210SChristian Ehrhardt
2213d9b7210SChristian Ehrhardtstatic inline int
2223d9b7210SChristian Ehrhardti40e_rx_vec_dev_conf_condition_check_default(struct rte_eth_dev *dev)
2233d9b7210SChristian Ehrhardt{
2243d9b7210SChristian Ehrhardt#ifndef RTE_LIBRTE_IEEE1588
2253d9b7210SChristian Ehrhardt	struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2263d9b7210SChristian Ehrhardt	struct rte_fdir_conf *fconf = &dev->data->dev_conf.fdir_conf;
2273d9b7210SChristian Ehrhardt
2283d9b7210SChristian Ehrhardt#ifndef RTE_LIBRTE_I40E_RX_OLFLAGS_ENABLE
2293d9b7210SChristian Ehrhardt	/* whithout rx ol_flags, no VP flag report */
2303d9b7210SChristian Ehrhardt	if (rxmode->hw_vlan_strip != 0 ||
2313d9b7210SChristian Ehrhardt	    rxmode->hw_vlan_extend != 0 ||
2323d9b7210SChristian Ehrhardt	    rxmode->hw_ip_checksum != 0)
2333d9b7210SChristian Ehrhardt		return -1;
2343d9b7210SChristian Ehrhardt#endif
2353d9b7210SChristian Ehrhardt
2363d9b7210SChristian Ehrhardt	/* no fdir support */
2373d9b7210SChristian Ehrhardt	if (fconf->mode != RTE_FDIR_MODE_NONE)
2383d9b7210SChristian Ehrhardt		return -1;
2393d9b7210SChristian Ehrhardt
2403d9b7210SChristian Ehrhardt	 /* - no csum error report support
2413d9b7210SChristian Ehrhardt	 * - no header split support
2423d9b7210SChristian Ehrhardt	 */
2433d9b7210SChristian Ehrhardt	if (rxmode->header_split == 1)
2443d9b7210SChristian Ehrhardt		return -1;
2453d9b7210SChristian Ehrhardt
2469365d6cfSChristian Ehrhardt	/* no QinQ support */
2479365d6cfSChristian Ehrhardt	if (rxmode->hw_vlan_extend == 1)
2489365d6cfSChristian Ehrhardt		return -1;
2499365d6cfSChristian Ehrhardt
2503d9b7210SChristian Ehrhardt	return 0;
2513d9b7210SChristian Ehrhardt#else
2523d9b7210SChristian Ehrhardt	RTE_SET_USED(dev);
2533d9b7210SChristian Ehrhardt	return -1;
2543d9b7210SChristian Ehrhardt#endif
2553d9b7210SChristian Ehrhardt}
2563d9b7210SChristian Ehrhardt#endif
257