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