net/enic: enable overlay offload for VXLAN and GENEVE
[dpdk.git] / drivers / net / enic / base / vnic_wq.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2008-2017 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  */
5
6 #ifndef _VNIC_WQ_H_
7 #define _VNIC_WQ_H_
8
9
10 #include "vnic_dev.h"
11 #include "vnic_cq.h"
12 #include <rte_memzone.h>
13
14 /* Work queue control */
15 struct vnic_wq_ctrl {
16         u64 ring_base;                  /* 0x00 */
17         u32 ring_size;                  /* 0x08 */
18         u32 pad0;
19         u32 posted_index;               /* 0x10 */
20         u32 pad1;
21         u32 cq_index;                   /* 0x18 */
22         u32 pad2;
23         u32 enable;                     /* 0x20 */
24         u32 pad3;
25         u32 running;                    /* 0x28 */
26         u32 pad4;
27         u32 fetch_index;                /* 0x30 */
28         u32 pad5;
29         u32 dca_value;                  /* 0x38 */
30         u32 pad6;
31         u32 error_interrupt_enable;     /* 0x40 */
32         u32 pad7;
33         u32 error_interrupt_offset;     /* 0x48 */
34         u32 pad8;
35         u32 error_status;               /* 0x50 */
36         u32 pad9;
37 };
38
39 /* 16 bytes */
40 struct vnic_wq_buf {
41         struct rte_mempool *pool;
42         void *mb;
43 };
44
45 struct vnic_wq {
46         unsigned int index;
47         uint64_t tx_offload_notsup_mask;
48         struct vnic_dev *vdev;
49         struct vnic_wq_ctrl __iomem *ctrl;              /* memory-mapped */
50         struct vnic_dev_ring ring;
51         struct vnic_wq_buf *bufs;
52         unsigned int head_idx;
53         unsigned int tail_idx;
54         unsigned int socket_id;
55         const struct rte_memzone *cqmsg_rz;
56         uint16_t last_completed_index;
57 };
58
59 static inline unsigned int vnic_wq_desc_avail(struct vnic_wq *wq)
60 {
61         /* how many does SW own? */
62         return wq->ring.desc_avail;
63 }
64
65 static inline unsigned int vnic_wq_desc_used(struct vnic_wq *wq)
66 {
67         /* how many does HW own? */
68         return wq->ring.desc_count - wq->ring.desc_avail - 1;
69 }
70
71 #define PI_LOG2_CACHE_LINE_SIZE        5
72 #define PI_INDEX_BITS            12
73 #define PI_INDEX_MASK ((1U << PI_INDEX_BITS) - 1)
74 #define PI_PREFETCH_LEN_MASK ((1U << PI_LOG2_CACHE_LINE_SIZE) - 1)
75 #define PI_PREFETCH_LEN_OFF 16
76 #define PI_PREFETCH_ADDR_BITS 43
77 #define PI_PREFETCH_ADDR_MASK ((1ULL << PI_PREFETCH_ADDR_BITS) - 1)
78 #define PI_PREFETCH_ADDR_OFF 21
79
80 /** How many cache lines are touched by buffer (addr, len). */
81 static inline unsigned int num_cache_lines_touched(dma_addr_t addr,
82                                                         unsigned int len)
83 {
84         const unsigned long mask = PI_PREFETCH_LEN_MASK;
85         const unsigned long laddr = (unsigned long)addr;
86         unsigned long lines, equiv_len;
87         /* A. If addr is aligned, our solution is just to round up len to the
88         next boundary.
89
90         e.g. addr = 0, len = 48
91         +--------------------+
92         |XXXXXXXXXXXXXXXXXXXX|    32-byte cacheline a
93         +--------------------+
94         |XXXXXXXXXX          |    cacheline b
95         +--------------------+
96
97         B. If addr is not aligned, however, we may use an extra
98         cacheline.  e.g. addr = 12, len = 22
99
100         +--------------------+
101         |       XXXXXXXXXXXXX|
102         +--------------------+
103         |XX                  |
104         +--------------------+
105
106         Our solution is to make the problem equivalent to case A
107         above by adding the empty space in the first cacheline to the length:
108         unsigned long len;
109
110         +--------------------+
111         |eeeeeeeXXXXXXXXXXXXX|    "e" is empty space, which we add to len
112         +--------------------+
113         |XX                  |
114         +--------------------+
115
116         */
117         equiv_len = len + (laddr & mask);
118
119         /* Now we can just round up this len to the next 32-byte boundary. */
120         lines = (equiv_len + mask) & (~mask);
121
122         /* Scale bytes -> cachelines. */
123         return lines >> PI_LOG2_CACHE_LINE_SIZE;
124 }
125
126 static inline u64 vnic_cached_posted_index(dma_addr_t addr, unsigned int len,
127                                                 unsigned int index)
128 {
129         unsigned int num_cache_lines = num_cache_lines_touched(addr, len);
130         /* Wish we could avoid a branch here.  We could have separate
131          * vnic_wq_post() and vinc_wq_post_inline(), the latter
132          * only supporting < 1k (2^5 * 2^5) sends, I suppose.  This would
133          * eliminate the if (eop) branch as well.
134          */
135         if (num_cache_lines > PI_PREFETCH_LEN_MASK)
136                 num_cache_lines = 0;
137         return (index & PI_INDEX_MASK) |
138         ((num_cache_lines & PI_PREFETCH_LEN_MASK) << PI_PREFETCH_LEN_OFF) |
139                 (((addr >> PI_LOG2_CACHE_LINE_SIZE) &
140         PI_PREFETCH_ADDR_MASK) << PI_PREFETCH_ADDR_OFF);
141 }
142
143 static inline uint32_t
144 buf_idx_incr(uint32_t n_descriptors, uint32_t idx)
145 {
146         idx++;
147         if (unlikely(idx == n_descriptors))
148                 idx = 0;
149         return idx;
150 }
151
152 void vnic_wq_free(struct vnic_wq *wq);
153 int vnic_wq_alloc(struct vnic_dev *vdev, struct vnic_wq *wq, unsigned int index,
154         unsigned int desc_count, unsigned int desc_size);
155 void vnic_wq_init_start(struct vnic_wq *wq, unsigned int cq_index,
156         unsigned int fetch_index, unsigned int posted_index,
157         unsigned int error_interrupt_enable,
158         unsigned int error_interrupt_offset);
159 void vnic_wq_init(struct vnic_wq *wq, unsigned int cq_index,
160         unsigned int error_interrupt_enable,
161         unsigned int error_interrupt_offset);
162 void vnic_wq_error_out(struct vnic_wq *wq, unsigned int error);
163 unsigned int vnic_wq_error_status(struct vnic_wq *wq);
164 void vnic_wq_enable(struct vnic_wq *wq);
165 int vnic_wq_disable(struct vnic_wq *wq);
166 void vnic_wq_clean(struct vnic_wq *wq,
167                    void (*buf_clean)(struct vnic_wq_buf *buf));
168 #endif /* _VNIC_WQ_H_ */