net/qede/base: fix macros to check chip revision/metal
[dpdk.git] / drivers / net / virtio / virtio_rxtx_simple_sse.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39
40 #include <tmmintrin.h>
41
42 #include <rte_byteorder.h>
43 #include <rte_branch_prediction.h>
44 #include <rte_cycles.h>
45 #include <rte_ether.h>
46 #include <rte_ethdev.h>
47 #include <rte_errno.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_mempool.h>
51 #include <rte_malloc.h>
52 #include <rte_mbuf.h>
53 #include <rte_prefetch.h>
54 #include <rte_string_fns.h>
55
56 #include "virtio_rxtx_simple.h"
57
58 #define RTE_VIRTIO_VPMD_RX_BURST 32
59 #define RTE_VIRTIO_DESC_PER_LOOP 8
60 #define RTE_VIRTIO_VPMD_RX_REARM_THRESH RTE_VIRTIO_VPMD_RX_BURST
61
62 /* virtio vPMD receive routine, only accept(nb_pkts >= RTE_VIRTIO_DESC_PER_LOOP)
63  *
64  * This routine is for non-mergeable RX, one desc for each guest buffer.
65  * This routine is based on the RX ring layout optimization. Each entry in the
66  * avail ring points to the desc with the same index in the desc ring and this
67  * will never be changed in the driver.
68  *
69  * - nb_pkts < RTE_VIRTIO_DESC_PER_LOOP, just return no packet
70  */
71 uint16_t
72 virtio_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
73         uint16_t nb_pkts)
74 {
75         struct virtnet_rx *rxvq = rx_queue;
76         struct virtqueue *vq = rxvq->vq;
77         struct virtio_hw *hw = vq->hw;
78         uint16_t nb_used;
79         uint16_t desc_idx;
80         struct vring_used_elem *rused;
81         struct rte_mbuf **sw_ring;
82         struct rte_mbuf **sw_ring_end;
83         uint16_t nb_pkts_received = 0;
84         __m128i shuf_msk1, shuf_msk2, len_adjust;
85
86         shuf_msk1 = _mm_set_epi8(
87                 0xFF, 0xFF, 0xFF, 0xFF,
88                 0xFF, 0xFF,             /* vlan tci */
89                 5, 4,                   /* dat len */
90                 0xFF, 0xFF, 5, 4,       /* pkt len */
91                 0xFF, 0xFF, 0xFF, 0xFF  /* packet type */
92
93         );
94
95         shuf_msk2 = _mm_set_epi8(
96                 0xFF, 0xFF, 0xFF, 0xFF,
97                 0xFF, 0xFF,             /* vlan tci */
98                 13, 12,                 /* dat len */
99                 0xFF, 0xFF, 13, 12,     /* pkt len */
100                 0xFF, 0xFF, 0xFF, 0xFF  /* packet type */
101         );
102
103         /* Subtract the header length.
104         *  In which case do we need the header length in used->len ?
105         */
106         len_adjust = _mm_set_epi16(
107                 0, 0,
108                 0,
109                 (uint16_t)-vq->hw->vtnet_hdr_size,
110                 0, (uint16_t)-vq->hw->vtnet_hdr_size,
111                 0, 0);
112
113         if (unlikely(hw->started == 0))
114                 return nb_pkts_received;
115
116         if (unlikely(nb_pkts < RTE_VIRTIO_DESC_PER_LOOP))
117                 return 0;
118
119         nb_used = VIRTQUEUE_NUSED(vq);
120
121         rte_compiler_barrier();
122
123         if (unlikely(nb_used == 0))
124                 return 0;
125
126         nb_pkts = RTE_ALIGN_FLOOR(nb_pkts, RTE_VIRTIO_DESC_PER_LOOP);
127         nb_used = RTE_MIN(nb_used, nb_pkts);
128
129         desc_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
130         rused = &vq->vq_ring.used->ring[desc_idx];
131         sw_ring  = &vq->sw_ring[desc_idx];
132         sw_ring_end = &vq->sw_ring[vq->vq_nentries];
133
134         rte_prefetch0(rused);
135
136         if (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
137                 virtio_rxq_rearm_vec(rxvq);
138                 if (unlikely(virtqueue_kick_prepare(vq)))
139                         virtqueue_notify(vq);
140         }
141
142         for (nb_pkts_received = 0;
143                 nb_pkts_received < nb_used;) {
144                 __m128i desc[RTE_VIRTIO_DESC_PER_LOOP / 2];
145                 __m128i mbp[RTE_VIRTIO_DESC_PER_LOOP / 2];
146                 __m128i pkt_mb[RTE_VIRTIO_DESC_PER_LOOP];
147
148                 mbp[0] = _mm_loadu_si128((__m128i *)(sw_ring + 0));
149                 desc[0] = _mm_loadu_si128((__m128i *)(rused + 0));
150                 _mm_storeu_si128((__m128i *)&rx_pkts[0], mbp[0]);
151
152                 mbp[1] = _mm_loadu_si128((__m128i *)(sw_ring + 2));
153                 desc[1] = _mm_loadu_si128((__m128i *)(rused + 2));
154                 _mm_storeu_si128((__m128i *)&rx_pkts[2], mbp[1]);
155
156                 mbp[2] = _mm_loadu_si128((__m128i *)(sw_ring + 4));
157                 desc[2] = _mm_loadu_si128((__m128i *)(rused + 4));
158                 _mm_storeu_si128((__m128i *)&rx_pkts[4], mbp[2]);
159
160                 mbp[3] = _mm_loadu_si128((__m128i *)(sw_ring + 6));
161                 desc[3] = _mm_loadu_si128((__m128i *)(rused + 6));
162                 _mm_storeu_si128((__m128i *)&rx_pkts[6], mbp[3]);
163
164                 pkt_mb[1] = _mm_shuffle_epi8(desc[0], shuf_msk2);
165                 pkt_mb[0] = _mm_shuffle_epi8(desc[0], shuf_msk1);
166                 pkt_mb[1] = _mm_add_epi16(pkt_mb[1], len_adjust);
167                 pkt_mb[0] = _mm_add_epi16(pkt_mb[0], len_adjust);
168                 _mm_storeu_si128((void *)&rx_pkts[1]->rx_descriptor_fields1,
169                         pkt_mb[1]);
170                 _mm_storeu_si128((void *)&rx_pkts[0]->rx_descriptor_fields1,
171                         pkt_mb[0]);
172
173                 pkt_mb[3] = _mm_shuffle_epi8(desc[1], shuf_msk2);
174                 pkt_mb[2] = _mm_shuffle_epi8(desc[1], shuf_msk1);
175                 pkt_mb[3] = _mm_add_epi16(pkt_mb[3], len_adjust);
176                 pkt_mb[2] = _mm_add_epi16(pkt_mb[2], len_adjust);
177                 _mm_storeu_si128((void *)&rx_pkts[3]->rx_descriptor_fields1,
178                         pkt_mb[3]);
179                 _mm_storeu_si128((void *)&rx_pkts[2]->rx_descriptor_fields1,
180                         pkt_mb[2]);
181
182                 pkt_mb[5] = _mm_shuffle_epi8(desc[2], shuf_msk2);
183                 pkt_mb[4] = _mm_shuffle_epi8(desc[2], shuf_msk1);
184                 pkt_mb[5] = _mm_add_epi16(pkt_mb[5], len_adjust);
185                 pkt_mb[4] = _mm_add_epi16(pkt_mb[4], len_adjust);
186                 _mm_storeu_si128((void *)&rx_pkts[5]->rx_descriptor_fields1,
187                         pkt_mb[5]);
188                 _mm_storeu_si128((void *)&rx_pkts[4]->rx_descriptor_fields1,
189                         pkt_mb[4]);
190
191                 pkt_mb[7] = _mm_shuffle_epi8(desc[3], shuf_msk2);
192                 pkt_mb[6] = _mm_shuffle_epi8(desc[3], shuf_msk1);
193                 pkt_mb[7] = _mm_add_epi16(pkt_mb[7], len_adjust);
194                 pkt_mb[6] = _mm_add_epi16(pkt_mb[6], len_adjust);
195                 _mm_storeu_si128((void *)&rx_pkts[7]->rx_descriptor_fields1,
196                         pkt_mb[7]);
197                 _mm_storeu_si128((void *)&rx_pkts[6]->rx_descriptor_fields1,
198                         pkt_mb[6]);
199
200                 if (unlikely(nb_used <= RTE_VIRTIO_DESC_PER_LOOP)) {
201                         if (sw_ring + nb_used <= sw_ring_end)
202                                 nb_pkts_received += nb_used;
203                         else
204                                 nb_pkts_received += sw_ring_end - sw_ring;
205                         break;
206                 } else {
207                         if (unlikely(sw_ring + RTE_VIRTIO_DESC_PER_LOOP >=
208                                 sw_ring_end)) {
209                                 nb_pkts_received += sw_ring_end - sw_ring;
210                                 break;
211                         } else {
212                                 nb_pkts_received += RTE_VIRTIO_DESC_PER_LOOP;
213
214                                 rx_pkts += RTE_VIRTIO_DESC_PER_LOOP;
215                                 sw_ring += RTE_VIRTIO_DESC_PER_LOOP;
216                                 rused   += RTE_VIRTIO_DESC_PER_LOOP;
217                                 nb_used -= RTE_VIRTIO_DESC_PER_LOOP;
218                         }
219                 }
220         }
221
222         vq->vq_used_cons_idx += nb_pkts_received;
223         vq->vq_free_cnt += nb_pkts_received;
224         rxvq->stats.packets += nb_pkts_received;
225         return nb_pkts_received;
226 }