virtio: use weaker barriers
[dpdk.git] / lib / librte_pmd_virtio / virtqueue.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #ifndef _VIRTQUEUE_H_
35 #define _VIRTQUEUE_H_
36
37 #include <stdint.h>
38
39 #include <rte_atomic.h>
40 #include <rte_mbuf.h>
41 #include <rte_memory.h>
42 #include <rte_memzone.h>
43 #include <rte_mempool.h>
44
45 #include "virtio_pci.h"
46 #include "virtio_ring.h"
47 #include "virtio_logs.h"
48
49 /*
50  * Per virtio_config.h in Linux.
51  *     For virtio_pci on SMP, we don't need to order with respect to MMIO
52  *     accesses through relaxed memory I/O windows, so smp_mb() et al are
53  *     sufficient.
54  *
55  * This driver is for virtio_pci on SMP and therefore can assume
56  * weaker (compiler barriers)
57  */
58 #define virtio_mb()     rte_mb()
59 #define virtio_rmb()    rte_compiler_barrier()
60 #define virtio_wmb()    rte_compiler_barrier()
61
62 #ifdef RTE_PMD_PACKET_PREFETCH
63 #define rte_packet_prefetch(p)  rte_prefetch1(p)
64 #else
65 #define rte_packet_prefetch(p)  do {} while(0)
66 #endif
67
68 #define VIRTQUEUE_MAX_NAME_SZ 32
69
70 #define RTE_MBUF_DATA_DMA_ADDR(mb) \
71         (uint64_t) ((mb)->buf_physaddr + (mb)->data_off)
72
73 #define VTNET_SQ_RQ_QUEUE_IDX 0
74 #define VTNET_SQ_TQ_QUEUE_IDX 1
75 #define VTNET_SQ_CQ_QUEUE_IDX 2
76
77 enum { VTNET_RQ = 0, VTNET_TQ = 1, VTNET_CQ = 2 };
78 /**
79  * The maximum virtqueue size is 2^15. Use that value as the end of
80  * descriptor chain terminator since it will never be a valid index
81  * in the descriptor table. This is used to verify we are correctly
82  * handling vq_free_cnt.
83  */
84 #define VQ_RING_DESC_CHAIN_END 32768
85
86 /**
87  * Control the RX mode, ie. promiscuous, allmulti, etc...
88  * All commands require an "out" sg entry containing a 1 byte
89  * state value, zero = disable, non-zero = enable.  Commands
90  * 0 and 1 are supported with the VIRTIO_NET_F_CTRL_RX feature.
91  * Commands 2-5 are added with VIRTIO_NET_F_CTRL_RX_EXTRA.
92  */
93 #define VIRTIO_NET_CTRL_RX              0
94 #define VIRTIO_NET_CTRL_RX_PROMISC      0
95 #define VIRTIO_NET_CTRL_RX_ALLMULTI     1
96 #define VIRTIO_NET_CTRL_RX_ALLUNI       2
97 #define VIRTIO_NET_CTRL_RX_NOMULTI      3
98 #define VIRTIO_NET_CTRL_RX_NOUNI        4
99 #define VIRTIO_NET_CTRL_RX_NOBCAST      5
100
101 /**
102  * Control VLAN filtering
103  *
104  * The VLAN filter table is controlled via a simple ADD/DEL interface.
105  * VLAN IDs not added may be filtered by the hypervisor.  Del is the
106  * opposite of add.  Both commands expect an out entry containing a 2
107  * byte VLAN ID.  VLAN filtering is available with the
108  * VIRTIO_NET_F_CTRL_VLAN feature bit.
109  */
110 #define VIRTIO_NET_CTRL_VLAN     2
111 #define VIRTIO_NET_CTRL_VLAN_ADD 0
112 #define VIRTIO_NET_CTRL_VLAN_DEL 1
113
114 struct virtio_net_ctrl_hdr {
115         uint8_t class;
116         uint8_t cmd;
117 } __attribute__((packed));
118
119 typedef uint8_t virtio_net_ctrl_ack;
120
121 #define VIRTIO_NET_OK     0
122 #define VIRTIO_NET_ERR    1
123
124 #define VIRTIO_MAX_CTRL_DATA 128
125
126 struct virtio_pmd_ctrl {
127         struct virtio_net_ctrl_hdr hdr;
128         virtio_net_ctrl_ack status;
129         uint8_t data[VIRTIO_MAX_CTRL_DATA];
130 };
131
132 struct virtqueue {
133         struct virtio_hw         *hw;     /**< virtio_hw structure pointer. */
134         const struct rte_memzone *mz;     /**< mem zone to populate RX ring. */
135         const struct rte_memzone *virtio_net_hdr_mz; /**< memzone to populate hdr. */
136         struct rte_mempool       *mpool;  /**< mempool for mbuf allocation */
137         uint16_t    queue_id;             /**< DPDK queue index. */
138         uint8_t     port_id;              /**< Device port identifier. */
139
140         void        *vq_ring_virt_mem;    /**< linear address of vring*/
141         int         vq_alignment;
142         int         vq_ring_size;
143         phys_addr_t vq_ring_mem;          /**< physical address of vring */
144
145         struct vring vq_ring;    /**< vring keeping desc, used and avail */
146         uint16_t    vq_free_cnt; /**< num of desc available */
147         uint16_t    vq_nentries; /**< vring desc numbers */
148         uint16_t    vq_queue_index;       /**< PCI queue index */
149         /**
150          * Head of the free chain in the descriptor table. If
151          * there are no free descriptors, this will be set to
152          * VQ_RING_DESC_CHAIN_END.
153          */
154         uint16_t  vq_desc_head_idx;
155         uint16_t  vq_desc_tail_idx;
156         /**
157          * Last consumed descriptor in the used table,
158          * trails vq_ring.used->idx.
159          */
160         uint16_t vq_used_cons_idx;
161         uint16_t vq_avail_idx;
162         phys_addr_t virtio_net_hdr_mem; /**< hdr for each xmit packet */
163
164         /* Statistics */
165         uint64_t        packets;
166         uint64_t        bytes;
167         uint64_t        errors;
168
169         struct vq_desc_extra {
170                 void              *cookie;
171                 uint16_t          ndescs;
172         } vq_descx[0];
173 };
174
175 /* If multiqueue is provided by host, then we suppport it. */
176 #ifndef VIRTIO_NET_F_MQ
177 /* Device supports Receive Flow Steering */
178 #define VIRTIO_NET_F_MQ 0x400000
179 #define VIRTIO_NET_CTRL_MQ   4
180 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET        0
181 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN        1
182 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX        0x8000
183 #endif
184
185 /**
186  * This is the first element of the scatter-gather list.  If you don't
187  * specify GSO or CSUM features, you can simply ignore the header.
188  */
189 struct virtio_net_hdr {
190 #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1    /**< Use csum_start,csum_offset*/
191         uint8_t flags;
192 #define VIRTIO_NET_HDR_GSO_NONE     0    /**< Not a GSO frame */
193 #define VIRTIO_NET_HDR_GSO_TCPV4    1    /**< GSO frame, IPv4 TCP (TSO) */
194 #define VIRTIO_NET_HDR_GSO_UDP      3    /**< GSO frame, IPv4 UDP (UFO) */
195 #define VIRTIO_NET_HDR_GSO_TCPV6    4    /**< GSO frame, IPv6 TCP */
196 #define VIRTIO_NET_HDR_GSO_ECN      0x80 /**< TCP has ECN set */
197         uint8_t gso_type;
198         uint16_t hdr_len;     /**< Ethernet + IP + tcp/udp hdrs */
199         uint16_t gso_size;    /**< Bytes to append to hdr_len per frame */
200         uint16_t csum_start;  /**< Position to start checksumming from */
201         uint16_t csum_offset; /**< Offset after that to place checksum */
202 };
203
204 /**
205  * This is the version of the header to use when the MRG_RXBUF
206  * feature has been negotiated.
207  */
208 struct virtio_net_hdr_mrg_rxbuf {
209         struct   virtio_net_hdr hdr;
210         uint16_t num_buffers; /**< Number of merged rx buffers */
211 };
212
213 /**
214  * Tell the backend not to interrupt us.
215  */
216 void virtqueue_disable_intr(struct virtqueue *vq);
217 /**
218  *  Dump virtqueue internal structures, for debug purpose only.
219  */
220 void virtqueue_dump(struct virtqueue *vq);
221 /**
222  *  Get all mbufs to be freed.
223  */
224 struct rte_mbuf *virtqueue_detatch_unused(struct virtqueue *vq);
225
226 static inline int
227 virtqueue_full(const struct virtqueue *vq)
228 {
229         return vq->vq_free_cnt == 0;
230 }
231
232 #define VIRTQUEUE_NUSED(vq) ((uint16_t)((vq)->vq_ring.used->idx - (vq)->vq_used_cons_idx))
233
234 static inline void
235 vq_update_avail_idx(struct virtqueue *vq)
236 {
237         virtio_wmb();
238         vq->vq_ring.avail->idx = vq->vq_avail_idx;
239 }
240
241 static inline void
242 vq_update_avail_ring(struct virtqueue *vq, uint16_t desc_idx)
243 {
244         uint16_t avail_idx;
245         /*
246          * Place the head of the descriptor chain into the next slot and make
247          * it usable to the host. The chain is made available now rather than
248          * deferring to virtqueue_notify() in the hopes that if the host is
249          * currently running on another CPU, we can keep it processing the new
250          * descriptor.
251          */
252         avail_idx = (uint16_t)(vq->vq_avail_idx & (vq->vq_nentries - 1));
253         vq->vq_ring.avail->ring[avail_idx] = desc_idx;
254         vq->vq_avail_idx++;
255 }
256
257 static inline int
258 virtqueue_kick_prepare(struct virtqueue *vq)
259 {
260         return !(vq->vq_ring.used->flags & VRING_USED_F_NO_NOTIFY);
261 }
262
263 static inline void
264 virtqueue_notify(struct virtqueue *vq)
265 {
266         /*
267          * Ensure updated avail->idx is visible to host.
268          * For virtio on IA, the notificaiton is through io port operation
269          * which is a serialization instruction itself.
270          */
271         VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_NOTIFY, vq->vq_queue_index);
272 }
273
274 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
275 #define VIRTQUEUE_DUMP(vq) do { \
276         uint16_t used_idx, nused; \
277         used_idx = (vq)->vq_ring.used->idx; \
278         nused = (uint16_t)(used_idx - (vq)->vq_used_cons_idx); \
279         PMD_INIT_LOG(DEBUG, \
280           "VQ: - size=%d; free=%d; used=%d; desc_head_idx=%d;" \
281           " avail.idx=%d; used_cons_idx=%d; used.idx=%d;" \
282           " avail.flags=0x%x; used.flags=0x%x", \
283           (vq)->vq_nentries, (vq)->vq_free_cnt, nused, \
284           (vq)->vq_desc_head_idx, (vq)->vq_ring.avail->idx, \
285           (vq)->vq_used_cons_idx, (vq)->vq_ring.used->idx, \
286           (vq)->vq_ring.avail->flags, (vq)->vq_ring.used->flags); \
287 } while (0)
288 #else
289 #define VIRTQUEUE_DUMP(vq) do { } while (0)
290 #endif
291
292 #endif /* _VIRTQUEUE_H_ */