197f17497SC.J. Collier/*-
297f17497SC.J. Collier *   BSD LICENSE
397f17497SC.J. Collier *
497f17497SC.J. Collier *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
597f17497SC.J. Collier *   All rights reserved.
697f17497SC.J. Collier *
797f17497SC.J. Collier *   Redistribution and use in source and binary forms, with or without
897f17497SC.J. Collier *   modification, are permitted provided that the following conditions
997f17497SC.J. Collier *   are met:
1097f17497SC.J. Collier *
1197f17497SC.J. Collier *     * Redistributions of source code must retain the above copyright
1297f17497SC.J. Collier *       notice, this list of conditions and the following disclaimer.
1397f17497SC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
1497f17497SC.J. Collier *       notice, this list of conditions and the following disclaimer in
1597f17497SC.J. Collier *       the documentation and/or other materials provided with the
1697f17497SC.J. Collier *       distribution.
1797f17497SC.J. Collier *     * Neither the name of Intel Corporation nor the names of its
1897f17497SC.J. Collier *       contributors may be used to endorse or promote products derived
1997f17497SC.J. Collier *       from this software without specific prior written permission.
2097f17497SC.J. Collier *
2197f17497SC.J. Collier *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2297f17497SC.J. Collier *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2397f17497SC.J. Collier *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2497f17497SC.J. Collier *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2597f17497SC.J. Collier *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2697f17497SC.J. Collier *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2797f17497SC.J. Collier *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2897f17497SC.J. Collier *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2997f17497SC.J. Collier *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3097f17497SC.J. Collier *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3197f17497SC.J. Collier *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3297f17497SC.J. Collier */
3397f17497SC.J. Collier
3497f17497SC.J. Collier#include <stdint.h>
3597f17497SC.J. Collier#include <stdio.h>
3697f17497SC.J. Collier#include <stdlib.h>
3797f17497SC.J. Collier#include <string.h>
3897f17497SC.J. Collier#include <errno.h>
3997f17497SC.J. Collier
4097f17497SC.J. Collier#include <rte_cycles.h>
4197f17497SC.J. Collier#include <rte_memory.h>
4297f17497SC.J. Collier#include <rte_memzone.h>
4397f17497SC.J. Collier#include <rte_branch_prediction.h>
4497f17497SC.J. Collier#include <rte_mempool.h>
4597f17497SC.J. Collier#include <rte_malloc.h>
4697f17497SC.J. Collier#include <rte_mbuf.h>
4797f17497SC.J. Collier#include <rte_ether.h>
4897f17497SC.J. Collier#include <rte_ethdev.h>
4997f17497SC.J. Collier#include <rte_prefetch.h>
5097f17497SC.J. Collier#include <rte_string_fns.h>
5197f17497SC.J. Collier#include <rte_errno.h>
5297f17497SC.J. Collier#include <rte_byteorder.h>
5397f17497SC.J. Collier
546b3e017eSChristian Ehrhardt#include "virtio_rxtx_simple.h"
5597f17497SC.J. Collier
5697f17497SC.J. Collier#ifndef __INTEL_COMPILER
5797f17497SC.J. Collier#pragma GCC diagnostic ignored "-Wcast-qual"
5897f17497SC.J. Collier#endif
5997f17497SC.J. Collier
6097f17497SC.J. Collieruint16_t
6197f17497SC.J. Colliervirtio_xmit_pkts_simple(void *tx_queue, struct rte_mbuf **tx_pkts,
6297f17497SC.J. Collier	uint16_t nb_pkts)
6397f17497SC.J. Collier{
648b25d1adSChristian Ehrhardt	struct virtnet_tx *txvq = tx_queue;
658b25d1adSChristian Ehrhardt	struct virtqueue *vq = txvq->vq;
6697f17497SC.J. Collier	uint16_t nb_used;
6797f17497SC.J. Collier	uint16_t desc_idx;
6897f17497SC.J. Collier	struct vring_desc *start_dp;
6997f17497SC.J. Collier	uint16_t nb_tail, nb_commit;
7097f17497SC.J. Collier	int i;
718b25d1adSChristian Ehrhardt	uint16_t desc_idx_max = (vq->vq_nentries >> 1) - 1;
7297f17497SC.J. Collier
738b25d1adSChristian Ehrhardt	nb_used = VIRTQUEUE_NUSED(vq);
7497f17497SC.J. Collier	rte_compiler_barrier();
7597f17497SC.J. Collier
7697f17497SC.J. Collier	if (nb_used >= VIRTIO_TX_FREE_THRESH)
7739157ec0SLuca Boccassi		virtio_xmit_cleanup_simple(vq);
7897f17497SC.J. Collier
798b25d1adSChristian Ehrhardt	nb_commit = nb_pkts = RTE_MIN((vq->vq_free_cnt >> 1), nb_pkts);
808b25d1adSChristian Ehrhardt	desc_idx = (uint16_t)(vq->vq_avail_idx & desc_idx_max);
818b25d1adSChristian Ehrhardt	start_dp = vq->vq_ring.desc;
8297f17497SC.J. Collier	nb_tail = (uint16_t) (desc_idx_max + 1 - desc_idx);
8397f17497SC.J. Collier
8497f17497SC.J. Collier	if (nb_commit >= nb_tail) {
8597f17497SC.J. Collier		for (i = 0; i < nb_tail; i++)
868b25d1adSChristian Ehrhardt			vq->vq_descx[desc_idx + i].cookie = tx_pkts[i];
8797f17497SC.J. Collier		for (i = 0; i < nb_tail; i++) {
8897f17497SC.J. Collier			start_dp[desc_idx].addr =
897b53c036SRicardo Salveti				VIRTIO_MBUF_DATA_DMA_ADDR(*tx_pkts, vq);
9097f17497SC.J. Collier			start_dp[desc_idx].len = (*tx_pkts)->pkt_len;
9197f17497SC.J. Collier			tx_pkts++;
9297f17497SC.J. Collier			desc_idx++;
9397f17497SC.J. Collier		}
9497f17497SC.J. Collier		nb_commit -= nb_tail;
9597f17497SC.J. Collier		desc_idx = 0;
9697f17497SC.J. Collier	}
9797f17497SC.J. Collier	for (i = 0; i < nb_commit; i++)
988b25d1adSChristian Ehrhardt		vq->vq_descx[desc_idx + i].cookie = tx_pkts[i];
9997f17497SC.J. Collier	for (i = 0; i < nb_commit; i++) {
1008b25d1adSChristian Ehrhardt		start_dp[desc_idx].addr =
1017b53c036SRicardo Salveti			VIRTIO_MBUF_DATA_DMA_ADDR(*tx_pkts, vq);
10297f17497SC.J. Collier		start_dp[desc_idx].len = (*tx_pkts)->pkt_len;
10397f17497SC.J. Collier		tx_pkts++;
10497f17497SC.J. Collier		desc_idx++;
10597f17497SC.J. Collier	}
10697f17497SC.J. Collier
10797f17497SC.J. Collier	rte_compiler_barrier();
10897f17497SC.J. Collier
1098b25d1adSChristian Ehrhardt	vq->vq_free_cnt -= (uint16_t)(nb_pkts << 1);
1108b25d1adSChristian Ehrhardt	vq->vq_avail_idx += nb_pkts;
1118b25d1adSChristian Ehrhardt	vq->vq_ring.avail->idx = vq->vq_avail_idx;
1128b25d1adSChristian Ehrhardt	txvq->stats.packets += nb_pkts;
11397f17497SC.J. Collier
11497f17497SC.J. Collier	if (likely(nb_pkts)) {
1158b25d1adSChristian Ehrhardt		if (unlikely(virtqueue_kick_prepare(vq)))
1168b25d1adSChristian Ehrhardt			virtqueue_notify(vq);
11797f17497SC.J. Collier	}
11897f17497SC.J. Collier
11997f17497SC.J. Collier	return nb_pkts;
12097f17497SC.J. Collier}
12197f17497SC.J. Collier
12297f17497SC.J. Collierint __attribute__((cold))
1238b25d1adSChristian Ehrhardtvirtio_rxq_vec_setup(struct virtnet_rx *rxq)
12497f17497SC.J. Collier{
12597f17497SC.J. Collier	uintptr_t p;
12697f17497SC.J. Collier	struct rte_mbuf mb_def = { .buf_addr = 0 }; /* zeroed mbuf */
12797f17497SC.J. Collier
12897f17497SC.J. Collier	mb_def.nb_segs = 1;
12997f17497SC.J. Collier	mb_def.data_off = RTE_PKTMBUF_HEADROOM;
13097f17497SC.J. Collier	mb_def.port = rxq->port_id;
13197f17497SC.J. Collier	rte_mbuf_refcnt_set(&mb_def, 1);
13297f17497SC.J. Collier
13397f17497SC.J. Collier	/* prevent compiler reordering: rearm_data covers previous fields */
13497f17497SC.J. Collier	rte_compiler_barrier();
13597f17497SC.J. Collier	p = (uintptr_t)&mb_def.rearm_data;
13697f17497SC.J. Collier	rxq->mbuf_initializer = *(uint64_t *)p;
13797f17497SC.J. Collier
13897f17497SC.J. Collier	return 0;
13997f17497SC.J. Collier}
1406b3e017eSChristian Ehrhardt
1416b3e017eSChristian Ehrhardt/* Stub for linkage when arch specific implementation is not available */
1426b3e017eSChristian Ehrhardtuint16_t __attribute__((weak))
1436b3e017eSChristian Ehrhardtvirtio_recv_pkts_vec(void *rx_queue __rte_unused,
1446b3e017eSChristian Ehrhardt		     struct rte_mbuf **rx_pkts __rte_unused,
1456b3e017eSChristian Ehrhardt		     uint16_t nb_pkts __rte_unused)
1466b3e017eSChristian Ehrhardt{
1476b3e017eSChristian Ehrhardt	rte_panic("Wrong weak function linked by linker\n");
1486b3e017eSChristian Ehrhardt	return 0;
1496b3e017eSChristian Ehrhardt}
150