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