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