vdpa/mlx5: support queue update
[dpdk.git] / drivers / net / virtio / virtio_rxtx_simple_sse.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <errno.h>
10
11 #include <tmmintrin.h>
12
13 #include <rte_byteorder.h>
14 #include <rte_branch_prediction.h>
15 #include <rte_cycles.h>
16 #include <rte_ether.h>
17 #include <rte_ethdev_driver.h>
18 #include <rte_errno.h>
19 #include <rte_memory.h>
20 #include <rte_mempool.h>
21 #include <rte_malloc.h>
22 #include <rte_mbuf.h>
23 #include <rte_prefetch.h>
24 #include <rte_string_fns.h>
25
26 #include "virtio_rxtx_simple.h"
27
28 #define RTE_VIRTIO_DESC_PER_LOOP 8
29
30 /* virtio vPMD receive routine, only accept(nb_pkts >= RTE_VIRTIO_DESC_PER_LOOP)
31  *
32  * This routine is for non-mergeable RX, one desc for each guest buffer.
33  * This routine is based on the RX ring layout optimization. Each entry in the
34  * avail ring points to the desc with the same index in the desc ring and this
35  * will never be changed in the driver.
36  *
37  * - nb_pkts < RTE_VIRTIO_DESC_PER_LOOP, just return no packet
38  */
39 uint16_t
40 virtio_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
41         uint16_t nb_pkts)
42 {
43         struct virtnet_rx *rxvq = rx_queue;
44         struct virtqueue *vq = rxvq->vq;
45         struct virtio_hw *hw = vq->hw;
46         uint16_t nb_used, nb_total;
47         uint16_t desc_idx;
48         struct vring_used_elem *rused;
49         struct rte_mbuf **sw_ring;
50         struct rte_mbuf **sw_ring_end;
51         struct rte_mbuf **ref_rx_pkts;
52         uint16_t nb_pkts_received = 0;
53         __m128i shuf_msk1, shuf_msk2, len_adjust;
54
55         shuf_msk1 = _mm_set_epi8(
56                 0xFF, 0xFF, 0xFF, 0xFF,
57                 0xFF, 0xFF,             /* vlan tci */
58                 5, 4,                   /* dat len */
59                 0xFF, 0xFF, 5, 4,       /* pkt len */
60                 0xFF, 0xFF, 0xFF, 0xFF  /* packet type */
61
62         );
63
64         shuf_msk2 = _mm_set_epi8(
65                 0xFF, 0xFF, 0xFF, 0xFF,
66                 0xFF, 0xFF,             /* vlan tci */
67                 13, 12,                 /* dat len */
68                 0xFF, 0xFF, 13, 12,     /* pkt len */
69                 0xFF, 0xFF, 0xFF, 0xFF  /* packet type */
70         );
71
72         /* Subtract the header length.
73         *  In which case do we need the header length in used->len ?
74         */
75         len_adjust = _mm_set_epi16(
76                 0, 0,
77                 0,
78                 (uint16_t)-vq->hw->vtnet_hdr_size,
79                 0, (uint16_t)-vq->hw->vtnet_hdr_size,
80                 0, 0);
81
82         if (unlikely(hw->started == 0))
83                 return nb_pkts_received;
84
85         if (unlikely(nb_pkts < RTE_VIRTIO_DESC_PER_LOOP))
86                 return 0;
87
88         nb_used = virtqueue_nused(vq);
89
90         if (unlikely(nb_used == 0))
91                 return 0;
92
93         nb_pkts = RTE_ALIGN_FLOOR(nb_pkts, RTE_VIRTIO_DESC_PER_LOOP);
94         nb_used = RTE_MIN(nb_used, nb_pkts);
95
96         desc_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
97         rused = &vq->vq_split.ring.used->ring[desc_idx];
98         sw_ring  = &vq->sw_ring[desc_idx];
99         sw_ring_end = &vq->sw_ring[vq->vq_nentries];
100
101         rte_prefetch0(rused);
102
103         if (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
104                 virtio_rxq_rearm_vec(rxvq);
105                 if (unlikely(virtqueue_kick_prepare(vq)))
106                         virtqueue_notify(vq);
107         }
108
109         nb_total = nb_used;
110         ref_rx_pkts = rx_pkts;
111         for (nb_pkts_received = 0;
112                 nb_pkts_received < nb_total;) {
113                 __m128i desc[RTE_VIRTIO_DESC_PER_LOOP / 2];
114                 __m128i mbp[RTE_VIRTIO_DESC_PER_LOOP / 2];
115                 __m128i pkt_mb[RTE_VIRTIO_DESC_PER_LOOP];
116
117                 mbp[0] = _mm_loadu_si128((__m128i *)(sw_ring + 0));
118                 desc[0] = _mm_loadu_si128((__m128i *)(rused + 0));
119                 _mm_storeu_si128((__m128i *)&rx_pkts[0], mbp[0]);
120
121                 mbp[1] = _mm_loadu_si128((__m128i *)(sw_ring + 2));
122                 desc[1] = _mm_loadu_si128((__m128i *)(rused + 2));
123                 _mm_storeu_si128((__m128i *)&rx_pkts[2], mbp[1]);
124
125                 mbp[2] = _mm_loadu_si128((__m128i *)(sw_ring + 4));
126                 desc[2] = _mm_loadu_si128((__m128i *)(rused + 4));
127                 _mm_storeu_si128((__m128i *)&rx_pkts[4], mbp[2]);
128
129                 mbp[3] = _mm_loadu_si128((__m128i *)(sw_ring + 6));
130                 desc[3] = _mm_loadu_si128((__m128i *)(rused + 6));
131                 _mm_storeu_si128((__m128i *)&rx_pkts[6], mbp[3]);
132
133                 pkt_mb[1] = _mm_shuffle_epi8(desc[0], shuf_msk2);
134                 pkt_mb[0] = _mm_shuffle_epi8(desc[0], shuf_msk1);
135                 pkt_mb[1] = _mm_add_epi16(pkt_mb[1], len_adjust);
136                 pkt_mb[0] = _mm_add_epi16(pkt_mb[0], len_adjust);
137                 _mm_storeu_si128((void *)&rx_pkts[1]->rx_descriptor_fields1,
138                         pkt_mb[1]);
139                 _mm_storeu_si128((void *)&rx_pkts[0]->rx_descriptor_fields1,
140                         pkt_mb[0]);
141
142                 pkt_mb[3] = _mm_shuffle_epi8(desc[1], shuf_msk2);
143                 pkt_mb[2] = _mm_shuffle_epi8(desc[1], shuf_msk1);
144                 pkt_mb[3] = _mm_add_epi16(pkt_mb[3], len_adjust);
145                 pkt_mb[2] = _mm_add_epi16(pkt_mb[2], len_adjust);
146                 _mm_storeu_si128((void *)&rx_pkts[3]->rx_descriptor_fields1,
147                         pkt_mb[3]);
148                 _mm_storeu_si128((void *)&rx_pkts[2]->rx_descriptor_fields1,
149                         pkt_mb[2]);
150
151                 pkt_mb[5] = _mm_shuffle_epi8(desc[2], shuf_msk2);
152                 pkt_mb[4] = _mm_shuffle_epi8(desc[2], shuf_msk1);
153                 pkt_mb[5] = _mm_add_epi16(pkt_mb[5], len_adjust);
154                 pkt_mb[4] = _mm_add_epi16(pkt_mb[4], len_adjust);
155                 _mm_storeu_si128((void *)&rx_pkts[5]->rx_descriptor_fields1,
156                         pkt_mb[5]);
157                 _mm_storeu_si128((void *)&rx_pkts[4]->rx_descriptor_fields1,
158                         pkt_mb[4]);
159
160                 pkt_mb[7] = _mm_shuffle_epi8(desc[3], shuf_msk2);
161                 pkt_mb[6] = _mm_shuffle_epi8(desc[3], shuf_msk1);
162                 pkt_mb[7] = _mm_add_epi16(pkt_mb[7], len_adjust);
163                 pkt_mb[6] = _mm_add_epi16(pkt_mb[6], len_adjust);
164                 _mm_storeu_si128((void *)&rx_pkts[7]->rx_descriptor_fields1,
165                         pkt_mb[7]);
166                 _mm_storeu_si128((void *)&rx_pkts[6]->rx_descriptor_fields1,
167                         pkt_mb[6]);
168
169                 if (unlikely(nb_used <= RTE_VIRTIO_DESC_PER_LOOP)) {
170                         if (sw_ring + nb_used <= sw_ring_end)
171                                 nb_pkts_received += nb_used;
172                         else
173                                 nb_pkts_received += sw_ring_end - sw_ring;
174                         break;
175                 } else {
176                         if (unlikely(sw_ring + RTE_VIRTIO_DESC_PER_LOOP >=
177                                 sw_ring_end)) {
178                                 nb_pkts_received += sw_ring_end - sw_ring;
179                                 break;
180                         } else {
181                                 nb_pkts_received += RTE_VIRTIO_DESC_PER_LOOP;
182
183                                 rx_pkts += RTE_VIRTIO_DESC_PER_LOOP;
184                                 sw_ring += RTE_VIRTIO_DESC_PER_LOOP;
185                                 rused   += RTE_VIRTIO_DESC_PER_LOOP;
186                                 nb_used -= RTE_VIRTIO_DESC_PER_LOOP;
187                         }
188                 }
189         }
190
191         vq->vq_used_cons_idx += nb_pkts_received;
192         vq->vq_free_cnt += nb_pkts_received;
193         rxvq->stats.packets += nb_pkts_received;
194         for (nb_used = 0; nb_used < nb_pkts_received; nb_used++)
195                 virtio_update_packet_stats(&rxvq->stats, ref_rx_pkts[nb_used]);
196
197         return nb_pkts_received;
198 }