remove trailing whitespaces
[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 virtio_net_ctrl_hdr {
107         uint8_t class;
108         uint8_t cmd;
109 } __attribute__((packed));
110
111 typedef uint8_t virtio_net_ctrl_ack;
112
113 #define VIRTIO_NET_OK     0
114 #define VIRTIO_NET_ERR    1
115
116 #define VIRTIO_MAX_CTRL_DATA 128
117
118 struct virtio_pmd_ctrl {
119         struct virtio_net_ctrl_hdr hdr;
120         virtio_net_ctrl_ack status;
121         uint8_t data[VIRTIO_MAX_CTRL_DATA];
122 };
123
124 struct virtqueue {
125         char        vq_name[VIRTQUEUE_MAX_NAME_SZ];
126         struct virtio_hw         *hw;     /**< virtio_hw structure pointer. */
127         const struct rte_memzone *mz;     /**< mem zone to populate RX ring. */
128         const struct rte_memzone *virtio_net_hdr_mz; /**< memzone to populate hdr. */
129         struct rte_mempool       *mpool;  /**< mempool for mbuf allocation */
130         uint16_t    queue_id;             /**< DPDK queue index. */
131         uint8_t     port_id;              /**< Device port identifier. */
132
133         void        *vq_ring_virt_mem;    /**< linear address of vring*/
134         int         vq_alignment;
135         int         vq_ring_size;
136         phys_addr_t vq_ring_mem;          /**< physical address of vring */
137
138         struct vring vq_ring;    /**< vring keeping desc, used and avail */
139         uint16_t    vq_free_cnt; /**< num of desc available */
140         uint16_t    vq_nentries; /**< vring desc numbers */
141         uint16_t    vq_queue_index;       /**< PCI queue index */
142         /**
143          * Head of the free chain in the descriptor table. If
144          * there are no free descriptors, this will be set to
145          * VQ_RING_DESC_CHAIN_END.
146          */
147         uint16_t  vq_desc_head_idx;
148         uint16_t  vq_desc_tail_idx;
149         /**
150          * Last consumed descriptor in the used table,
151          * trails vq_ring.used->idx.
152          */
153         uint16_t vq_used_cons_idx;
154         uint16_t vq_avail_idx;
155         void     *virtio_net_hdr_mem; /**< hdr for each xmit packet */
156
157         struct vq_desc_extra {
158                 void              *cookie;
159                 uint16_t          ndescs;
160         } vq_descx[0];
161 };
162
163 /* If multiqueue is provided by host, then we suppport it. */
164 #ifndef VIRTIO_NET_F_MQ
165 /* Device supports Receive Flow Steering */
166 #define VIRTIO_NET_F_MQ 0x400000
167 #define VIRTIO_NET_CTRL_MQ   4
168 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET        0
169 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN        1
170 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX        0x8000
171 #endif
172
173 /**
174  * This is the first element of the scatter-gather list.  If you don't
175  * specify GSO or CSUM features, you can simply ignore the header.
176  */
177 struct virtio_net_hdr {
178 #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1    /**< Use csum_start,csum_offset*/
179         uint8_t flags;
180 #define VIRTIO_NET_HDR_GSO_NONE     0    /**< Not a GSO frame */
181 #define VIRTIO_NET_HDR_GSO_TCPV4    1    /**< GSO frame, IPv4 TCP (TSO) */
182 #define VIRTIO_NET_HDR_GSO_UDP      3    /**< GSO frame, IPv4 UDP (UFO) */
183 #define VIRTIO_NET_HDR_GSO_TCPV6    4    /**< GSO frame, IPv6 TCP */
184 #define VIRTIO_NET_HDR_GSO_ECN      0x80 /**< TCP has ECN set */
185         uint8_t gso_type;
186         uint16_t hdr_len;     /**< Ethernet + IP + tcp/udp hdrs */
187         uint16_t gso_size;    /**< Bytes to append to hdr_len per frame */
188         uint16_t csum_start;  /**< Position to start checksumming from */
189         uint16_t csum_offset; /**< Offset after that to place checksum */
190 };
191
192 /**
193  * This is the version of the header to use when the MRG_RXBUF
194  * feature has been negotiated.
195  */
196 struct virtio_net_hdr_mrg_rxbuf {
197         struct   virtio_net_hdr hdr;
198         uint16_t num_buffers; /**< Number of merged rx buffers */
199 };
200
201 /**
202  * Tell the backend not to interrupt us.
203  */
204 void virtqueue_disable_intr(struct virtqueue *vq);
205 /**
206  *  Dump virtqueue internal structures, for debug purpose only.
207  */
208 void virtqueue_dump(struct virtqueue *vq);
209 /**
210  *  Get all mbufs to be freed.
211  */
212 struct rte_mbuf * virtqueue_detatch_unused(struct virtqueue *vq);
213
214 static inline int
215 virtqueue_full(const struct virtqueue *vq)
216 {
217         return (vq->vq_free_cnt == 0);
218 }
219
220 #define VIRTQUEUE_NUSED(vq) ((uint16_t)((vq)->vq_ring.used->idx - (vq)->vq_used_cons_idx))
221
222 static inline void __attribute__((always_inline))
223 vq_update_avail_idx(struct virtqueue *vq)
224 {
225         rte_compiler_barrier();
226         vq->vq_ring.avail->idx = vq->vq_avail_idx;
227 }
228
229 static inline void __attribute__((always_inline))
230 vq_update_avail_ring(struct virtqueue *vq, uint16_t desc_idx)
231 {
232         uint16_t avail_idx;
233         /*
234          * Place the head of the descriptor chain into the next slot and make
235          * it usable to the host. The chain is made available now rather than
236          * deferring to virtqueue_notify() in the hopes that if the host is
237          * currently running on another CPU, we can keep it processing the new
238          * descriptor.
239          */
240         avail_idx = (uint16_t)(vq->vq_avail_idx & (vq->vq_nentries - 1));
241         vq->vq_ring.avail->ring[avail_idx] = desc_idx;
242         vq->vq_avail_idx++;
243 }
244
245 static inline int __attribute__((always_inline))
246 virtqueue_kick_prepare(struct virtqueue * vq)
247 {
248         return !(vq->vq_ring.used->flags & VRING_USED_F_NO_NOTIFY);
249 }
250
251 static inline void __attribute__((always_inline))
252 virtqueue_notify(struct virtqueue *vq)
253 {
254         /*
255          * Ensure updated avail->idx is visible to host. mb() necessary?
256          * For virtio on IA, the notificaiton is through io port operation
257          * which is a serialization instruction itself.
258          */
259         VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_NOTIFY, vq->vq_queue_index);
260 }
261
262 static inline void __attribute__((always_inline))
263 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
264 {
265         struct vring_desc *dp, *dp_tail;
266         struct vq_desc_extra *dxp;
267         uint16_t desc_idx_last = desc_idx;
268
269         dp  = &vq->vq_ring.desc[desc_idx];
270         dxp = &vq->vq_descx[desc_idx];
271         vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
272         if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
273                 while (dp->flags & VRING_DESC_F_NEXT) {
274                         desc_idx_last = dp->next;
275                         dp = &vq->vq_ring.desc[dp->next];
276                 }
277         }
278         dxp->ndescs = 0;
279
280         /*
281          * We must append the existing free chain, if any, to the end of
282          * newly freed chain. If the virtqueue was completely used, then
283          * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
284          */
285         if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
286                 vq->vq_desc_head_idx = desc_idx;
287         } else {
288                 dp_tail = &vq->vq_ring.desc[vq->vq_desc_tail_idx];
289                 dp_tail->next = desc_idx;
290         }
291
292         vq->vq_desc_tail_idx = desc_idx_last;
293         dp->next = VQ_RING_DESC_CHAIN_END;
294 }
295
296 static inline int __attribute__((always_inline))
297 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
298 {
299         struct vq_desc_extra *dxp;
300         struct vring_desc *start_dp;
301         uint16_t needed;
302         uint16_t head_idx, idx;
303         needed = 1;
304
305         if (unlikely(vq->vq_free_cnt == 0))
306                 return (-ENOSPC);
307         if (unlikely(vq->vq_free_cnt < needed))
308                 return (-EMSGSIZE);
309
310         head_idx = vq->vq_desc_head_idx;
311         if (unlikely(head_idx >= vq->vq_nentries))
312                 return (-EFAULT);
313
314         idx = head_idx;
315         dxp = &vq->vq_descx[idx];
316         dxp->cookie = (void *)cookie;
317         dxp->ndescs = needed;
318
319         start_dp = vq->vq_ring.desc;
320         start_dp[idx].addr  =
321                 (uint64_t) (cookie->buf_physaddr + RTE_PKTMBUF_HEADROOM - sizeof(struct virtio_net_hdr));
322         start_dp[idx].len   = cookie->buf_len - RTE_PKTMBUF_HEADROOM + sizeof(struct virtio_net_hdr);
323         start_dp[idx].flags =  VRING_DESC_F_WRITE;
324         idx = start_dp[idx].next;
325         vq->vq_desc_head_idx = idx;
326         if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
327                 vq->vq_desc_tail_idx = idx;
328         vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
329         vq_update_avail_ring(vq, head_idx);
330
331         return (0);
332 }
333
334 static inline int __attribute__((always_inline))
335 virtqueue_enqueue_xmit(struct virtqueue *txvq, struct rte_mbuf *cookie)
336 {
337         struct vq_desc_extra *dxp;
338         struct vring_desc *start_dp;
339         uint16_t needed;
340         uint16_t head_idx, idx;
341         needed = 2;
342         if (unlikely(txvq->vq_free_cnt == 0))
343                 return (-ENOSPC);
344         if (unlikely(txvq->vq_free_cnt < needed))
345                 return (-EMSGSIZE);
346         head_idx = txvq->vq_desc_head_idx;
347         if (unlikely(head_idx >= txvq->vq_nentries))
348                 return (-EFAULT);
349
350         idx = head_idx;
351         dxp = &txvq->vq_descx[idx];
352         if (dxp->cookie != NULL)
353                 rte_pktmbuf_free_seg(dxp->cookie);
354         dxp->cookie = (void *)cookie;
355         dxp->ndescs = needed;
356
357         start_dp = txvq->vq_ring.desc;
358         start_dp[idx].addr  = (uint64_t)(uintptr_t)txvq->virtio_net_hdr_mem + idx * sizeof(struct virtio_net_hdr);
359         start_dp[idx].len   = sizeof(struct virtio_net_hdr);
360         start_dp[idx].flags = VRING_DESC_F_NEXT;
361         idx = start_dp[idx].next;
362         start_dp[idx].addr  = RTE_MBUF_DATA_DMA_ADDR(cookie);
363         start_dp[idx].len   = cookie->pkt.data_len;
364         start_dp[idx].flags = 0;
365         idx = start_dp[idx].next;
366         txvq->vq_desc_head_idx = idx;
367         if (txvq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
368                 txvq->vq_desc_tail_idx = idx;
369         txvq->vq_free_cnt = (uint16_t)(txvq->vq_free_cnt - needed);
370         vq_update_avail_ring(txvq, head_idx);
371
372         return (0);
373 }
374
375 static inline uint16_t __attribute__((always_inline))
376 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts, uint32_t *len, uint16_t num)
377 {
378         struct vring_used_elem *uep;
379         struct rte_mbuf *cookie;
380         uint16_t used_idx, desc_idx;
381         uint16_t i;
382
383         /*  Caller does the check */
384         for (i = 0; i < num ; i ++) {
385                 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
386                 uep = &vq->vq_ring.used->ring[used_idx];
387                 desc_idx = (uint16_t) uep->id;
388                 len[i] = uep->len;
389                 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
390
391                 if (unlikely(cookie == NULL)) {
392                         PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u\n",
393                                 vq->vq_used_cons_idx);
394                         break;
395                 }
396
397                 rte_prefetch0(cookie);
398                 rte_packet_prefetch(cookie->pkt.data);
399                 rx_pkts[i]  = cookie;
400                 vq->vq_used_cons_idx++;
401                 vq_ring_free_chain(vq, desc_idx);
402                 vq->vq_descx[desc_idx].cookie = NULL;
403         }
404
405         return (i);
406 }
407
408 static inline uint16_t __attribute__((always_inline))
409 virtqueue_dequeue_pkt_tx(struct virtqueue *vq)
410 {
411         struct vring_used_elem *uep;
412         uint16_t used_idx, desc_idx;
413
414         used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
415         uep = &vq->vq_ring.used->ring[used_idx];
416         desc_idx = (uint16_t) uep->id;
417         vq->vq_used_cons_idx++;
418         vq_ring_free_chain(vq, desc_idx);
419
420         return 0;
421 }
422
423 #ifdef  RTE_LIBRTE_VIRTIO_DEBUG_DUMP
424 #define VIRTQUEUE_DUMP(vq) do { \
425         uint16_t used_idx, nused; \
426         used_idx = (vq)->vq_ring.used->idx; \
427         nused = (uint16_t)(used_idx - (vq)->vq_used_cons_idx); \
428         PMD_INIT_LOG(DEBUG, \
429           "VQ: %s - size=%d; free=%d; used=%d; desc_head_idx=%d;" \
430           " avail.idx=%d; used_cons_idx=%d; used.idx=%d;" \
431           " avail.flags=0x%x; used.flags=0x%x\n", \
432           (vq)->vq_name, (vq)->vq_nentries, (vq)->vq_free_cnt, nused, \
433           (vq)->vq_desc_head_idx, (vq)->vq_ring.avail->idx, \
434           (vq)->vq_used_cons_idx, (vq)->vq_ring.used->idx, \
435           (vq)->vq_ring.avail->flags, (vq)->vq_ring.used->flags); \
436 } while (0)
437 #else
438 #define VIRTQUEUE_DUMP(vq) do { } while (0)
439 #endif
440
441 #endif /* _VIRTQUEUE_H_ */