update Intel copyright years to 2014
[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 #define mb()  rte_mb()
50 #define wmb() rte_wmb()
51 #define rmb() rte_rmb()
52
53 #define VIRTQUEUE_MAX_NAME_SZ 32
54
55 #define RTE_MBUF_DATA_DMA_ADDR(mb) \
56         (uint64_t) ((mb)->buf_physaddr + (uint64_t)((char *)((mb)->pkt.data) - \
57         (char *)(mb)->buf_addr))
58
59 #define VTNET_SQ_RQ_QUEUE_IDX 0
60 #define VTNET_SQ_TQ_QUEUE_IDX 1
61 #define VTNET_SQ_CQ_QUEUE_IDX 2
62
63 enum { VTNET_RQ = 0, VTNET_TQ = 1, VTNET_CQ = 2 };
64 /**
65  * The maximum virtqueue size is 2^15. Use that value as the end of
66  * descriptor chain terminator since it will never be a valid index
67  * in the descriptor table. This is used to verify we are correctly
68  * handling vq_free_cnt.
69  */
70 #define VQ_RING_DESC_CHAIN_END 32768
71
72 /**
73  * Control the RX mode, ie. promiscuous, allmulti, etc...
74  * All commands require an "out" sg entry containing a 1 byte
75  * state value, zero = disable, non-zero = enable.  Commands
76  * 0 and 1 are supported with the VIRTIO_NET_F_CTRL_RX feature.
77  * Commands 2-5 are added with VIRTIO_NET_F_CTRL_RX_EXTRA.
78  */
79 #define VIRTIO_NET_CTRL_RX              0
80 #define VIRTIO_NET_CTRL_RX_PROMISC      0
81 #define VIRTIO_NET_CTRL_RX_ALLMULTI     1
82 #define VIRTIO_NET_CTRL_RX_ALLUNI       2
83 #define VIRTIO_NET_CTRL_RX_NOMULTI      3
84 #define VIRTIO_NET_CTRL_RX_NOUNI        4
85 #define VIRTIO_NET_CTRL_RX_NOBCAST      5 
86
87 /**
88  * Control VLAN filtering
89  *
90  * The VLAN filter table is controlled via a simple ADD/DEL interface.
91  * VLAN IDs not added may be filtered by the hypervisor.  Del is the
92  * opposite of add.  Both commands expect an out entry containing a 2
93  * byte VLAN ID.  VLAN filtering is available with the
94  * VIRTIO_NET_F_CTRL_VLAN feature bit.
95  */
96 #define VIRTIO_NET_CTRL_VLAN     2
97 #define VIRTIO_NET_CTRL_VLAN_ADD 0
98 #define VIRTIO_NET_CTRL_VLAN_DEL 1
99
100 struct virtqueue {
101         char        vq_name[VIRTQUEUE_MAX_NAME_SZ];
102         struct virtio_hw         *hw;     /**< virtio_hw structure pointer. */
103         const struct rte_memzone *mz;     /**< mem zone to populate RX ring. */
104         const struct rte_memzone *virtio_net_hdr_mz; /**< memzone to populate hdr. */
105         struct rte_mempool       *mpool;  /**< mempool for mbuf allocation */
106         uint16_t    queue_id;             /**< DPDK queue index. */
107         uint8_t     port_id;              /**< Device port identifier. */
108
109         void        *vq_ring_virt_mem;    /**< linear address of vring*/
110         int         vq_alignment;
111         int         vq_ring_size;
112         phys_addr_t vq_ring_mem;          /**< physical address of vring */
113
114         struct vring vq_ring;    /**< vring keeping desc, used and avail */
115         uint16_t    vq_free_cnt; /**< num of desc available */
116         uint16_t    vq_nentries; /**< vring desc numbers */
117         uint16_t    vq_queue_index;       /**< PCI queue index */
118         /**
119          * Head of the free chain in the descriptor table. If
120          * there are no free descriptors, this will be set to
121          * VQ_RING_DESC_CHAIN_END.
122          */
123         uint16_t  vq_desc_head_idx;
124         /**
125          * Last consumed descriptor in the used table,
126          * trails vq_ring.used->idx.
127          */
128         uint16_t vq_used_cons_idx;
129         void     *virtio_net_hdr_mem; /**< hdr for each xmit packet */
130
131         struct vq_desc_extra {
132                 void              *cookie;
133                 uint16_t          ndescs;
134         } vq_descx[0];
135 };
136
137 /**
138  * This is the first element of the scatter-gather list.  If you don't
139  * specify GSO or CSUM features, you can simply ignore the header.
140  */
141 struct virtio_net_hdr {
142 #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1    /**< Use csum_start,csum_offset*/
143         uint8_t flags;
144 #define VIRTIO_NET_HDR_GSO_NONE     0    /**< Not a GSO frame */
145 #define VIRTIO_NET_HDR_GSO_TCPV4    1    /**< GSO frame, IPv4 TCP (TSO) */
146 #define VIRTIO_NET_HDR_GSO_UDP      3    /**< GSO frame, IPv4 UDP (UFO) */
147 #define VIRTIO_NET_HDR_GSO_TCPV6    4    /**< GSO frame, IPv6 TCP */
148 #define VIRTIO_NET_HDR_GSO_ECN      0x80 /**< TCP has ECN set */
149         uint8_t gso_type;
150         uint16_t hdr_len;     /**< Ethernet + IP + tcp/udp hdrs */
151         uint16_t gso_size;    /**< Bytes to append to hdr_len per frame */
152         uint16_t csum_start;  /**< Position to start checksumming from */
153         uint16_t csum_offset; /**< Offset after that to place checksum */
154 };
155
156 /**
157  * This is the version of the header to use when the MRG_RXBUF
158  * feature has been negotiated.
159  */
160 struct virtio_net_hdr_mrg_rxbuf {
161         struct   virtio_net_hdr hdr;
162         uint16_t num_buffers; /**< Number of merged rx buffers */
163 };
164
165 /**
166  * Tell the backend not to interrupt us.
167  */
168 void virtqueue_disable_intr(struct virtqueue *vq);
169 /**
170  *  Dump virtqueue internal structures, for debug purpose only.
171  */
172 void virtqueue_dump(struct virtqueue *vq);
173 /**
174  *  Get all mbufs to be freed.
175  */
176 struct rte_mbuf * virtqueue_detatch_unused(struct virtqueue *vq);
177
178 static inline int
179 virtqueue_full(const struct virtqueue *vq)
180 {
181         return (vq->vq_free_cnt == 0);
182 }
183
184 #define VIRTQUEUE_NUSED(vq) ((uint16_t)((vq)->vq_ring.used->idx - (vq)->vq_used_cons_idx))
185
186 static inline void
187 vq_ring_update_avail(struct virtqueue *vq, uint16_t desc_idx)
188 {
189         uint16_t avail_idx;
190         /*
191          * Place the head of the descriptor chain into the next slot and make
192          * it usable to the host. The chain is made available now rather than
193          * deferring to virtqueue_notify() in the hopes that if the host is
194          * currently running on another CPU, we can keep it processing the new
195          * descriptor.
196          */
197         avail_idx = (uint16_t)(vq->vq_ring.avail->idx & (vq->vq_nentries - 1));
198         vq->vq_ring.avail->ring[avail_idx] = desc_idx;
199         mb();
200         vq->vq_ring.avail->idx++;
201 }
202
203 static inline int virtqueue_kick_prepare(struct virtqueue * vq)
204 {
205         return !(vq->vq_ring.used->flags & VRING_USED_F_NO_NOTIFY);
206 }
207
208 static inline void
209 virtqueue_notify(struct virtqueue *vq)
210 {
211         /* 
212          * Ensure updated avail->idx is visible to host. mb() necessary?
213          * For virtio on IA, the notificaiton is through io port operation
214          * which is a serialization instruction itself.
215          */
216         VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_NOTIFY, vq->vq_queue_index);
217 }
218
219 static inline void
220 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
221 {
222         struct vring_desc *dp;
223         struct vq_desc_extra *dxp;
224
225         dp  = &vq->vq_ring.desc[desc_idx];
226         dxp = &vq->vq_descx[desc_idx];
227         vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
228         if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
229                 while (dp->flags & VRING_DESC_F_NEXT) {
230                         dp = &vq->vq_ring.desc[dp->next];
231                 }
232         }
233         dxp->ndescs = 0;
234         
235         /*
236          * We must append the existing free chain, if any, to the end of
237          * newly freed chain. If the virtqueue was completely used, then
238          * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
239          */
240         dp->next = vq->vq_desc_head_idx;
241         vq->vq_desc_head_idx = desc_idx;
242 }
243
244 static inline int
245 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
246 {
247         struct vq_desc_extra *dxp;
248         struct vring_desc *start_dp;
249         uint16_t needed;
250         uint16_t head_idx, idx;
251         needed = 1;
252
253         if (unlikely(vq->vq_free_cnt == 0))
254                 return (-ENOSPC);
255         if (unlikely(vq->vq_free_cnt < needed))
256                 return (-EMSGSIZE);
257
258         head_idx = vq->vq_desc_head_idx;
259         if (unlikely(head_idx >= vq->vq_nentries))
260                 return (-EFAULT);
261
262         idx = head_idx;
263         dxp = &vq->vq_descx[idx];
264         dxp->cookie = (void *)cookie;
265         dxp->ndescs = needed;
266
267         start_dp = vq->vq_ring.desc;
268         start_dp[idx].addr  =
269                 (uint64_t) (cookie->buf_physaddr + RTE_PKTMBUF_HEADROOM - sizeof(struct virtio_net_hdr));
270         start_dp[idx].len   = cookie->buf_len - RTE_PKTMBUF_HEADROOM + sizeof(struct virtio_net_hdr);
271         start_dp[idx].flags =  VRING_DESC_F_WRITE;
272         idx = start_dp[idx].next;
273         vq->vq_desc_head_idx = idx;
274         vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
275         vq_ring_update_avail(vq, head_idx);
276
277         return (0);
278 }
279
280 static inline int
281 virtqueue_enqueue_xmit(struct virtqueue *txvq, struct rte_mbuf *cookie)
282 {
283         struct vq_desc_extra *dxp;
284         struct vring_desc *start_dp;
285         uint16_t needed;
286         uint16_t head_idx, idx;
287         needed = 2;
288         if (unlikely(txvq->vq_free_cnt == 0))
289                 return (-ENOSPC);
290         if (unlikely(txvq->vq_free_cnt < needed))
291                 return (-EMSGSIZE);
292         head_idx = txvq->vq_desc_head_idx;
293         if (unlikely(head_idx >= txvq->vq_nentries)) 
294                 return (-EFAULT);
295
296         idx = head_idx;
297         dxp = &txvq->vq_descx[idx];
298         dxp->cookie = (void *)cookie;
299         dxp->ndescs = needed;
300
301         start_dp = txvq->vq_ring.desc;
302         start_dp[idx].addr  = (uint64_t)(uintptr_t)txvq->virtio_net_hdr_mem + idx * sizeof(struct virtio_net_hdr);
303         start_dp[idx].len   = sizeof(struct virtio_net_hdr);
304         start_dp[idx].flags = VRING_DESC_F_NEXT;
305         idx = start_dp[idx].next;
306         start_dp[idx].addr  = RTE_MBUF_DATA_DMA_ADDR(cookie);
307         start_dp[idx].len   = cookie->pkt.data_len;
308         start_dp[idx].flags = 0;
309         idx = start_dp[idx].next;
310         txvq->vq_desc_head_idx = idx;
311         txvq->vq_free_cnt = (uint16_t)(txvq->vq_free_cnt - needed);
312         vq_ring_update_avail(txvq, head_idx);
313
314         return (0);
315 }
316
317 static inline uint16_t
318 virtqueue_dequeue_burst(struct virtqueue *vq, struct rte_mbuf **rx_pkts, uint32_t *len, uint16_t num)
319 {
320         struct vring_used_elem *uep;
321         struct rte_mbuf *cookie;
322         uint16_t used_idx, desc_idx;
323         uint16_t i;
324         /*  Caller does the check */
325         for (i = 0; i < num ; i ++) {
326                 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
327                 uep = &vq->vq_ring.used->ring[used_idx];
328                 desc_idx = (uint16_t) uep->id;
329                 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
330                 if (unlikely(cookie == NULL)) {
331                         PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u\n", 
332                                 vq->vq_used_cons_idx);
333                         break;
334                 }
335                 len[i] = uep->len;
336                 rx_pkts[i]  = cookie;
337                 vq->vq_used_cons_idx++;
338                 vq_ring_free_chain(vq, desc_idx);
339                 vq->vq_descx[desc_idx].cookie = NULL;
340         }
341         return (i);
342 }
343
344 #ifdef  RTE_LIBRTE_VIRTIO_DEBUG_DUMP
345 #define VIRTQUEUE_DUMP(vq) do { \
346         uint16_t used_idx, nused; \
347         used_idx = (vq)->vq_ring.used->idx; \
348         nused = (uint16_t)(used_idx - (vq)->vq_used_cons_idx); \
349         PMD_INIT_LOG(DEBUG, \
350           "VQ: %s - size=%d; free=%d; used=%d; desc_head_idx=%d;" \
351           " avail.idx=%d; used_cons_idx=%d; used.idx=%d;" \
352           " avail.flags=0x%x; used.flags=0x%x\n", \
353           (vq)->vq_name, (vq)->vq_nentries, (vq)->vq_free_cnt, nused, \
354           (vq)->vq_desc_head_idx, (vq)->vq_ring.avail->idx, \
355           (vq)->vq_used_cons_idx, (vq)->vq_ring.used->idx, \
356           (vq)->vq_ring.avail->flags, (vq)->vq_ring.used->flags); \
357 } while (0)
358 #else
359 #define VIRTQUEUE_DUMP(vq) do { } while (0)
360 #endif
361
362 #endif /* _VIRTQUEUE_H_ */