e1000: move to drivers/net/
[dpdk.git] / lib / librte_pmd_enic / vnic / vnic_wq.h
1 /*
2  * Copyright 2008-2010 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
5  * Copyright (c) 2014, Cisco Systems, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 #ident "$Id: vnic_wq.h 183023 2014-07-22 23:47:25Z xuywang $"
35
36 #ifndef _VNIC_WQ_H_
37 #define _VNIC_WQ_H_
38
39
40 #include "vnic_dev.h"
41 #include "vnic_cq.h"
42
43 /* Work queue control */
44 struct vnic_wq_ctrl {
45         u64 ring_base;                  /* 0x00 */
46         u32 ring_size;                  /* 0x08 */
47         u32 pad0;
48         u32 posted_index;               /* 0x10 */
49         u32 pad1;
50         u32 cq_index;                   /* 0x18 */
51         u32 pad2;
52         u32 enable;                     /* 0x20 */
53         u32 pad3;
54         u32 running;                    /* 0x28 */
55         u32 pad4;
56         u32 fetch_index;                /* 0x30 */
57         u32 pad5;
58         u32 dca_value;                  /* 0x38 */
59         u32 pad6;
60         u32 error_interrupt_enable;     /* 0x40 */
61         u32 pad7;
62         u32 error_interrupt_offset;     /* 0x48 */
63         u32 pad8;
64         u32 error_status;               /* 0x50 */
65         u32 pad9;
66 };
67
68 struct vnic_wq_buf {
69         struct vnic_wq_buf *next;
70         dma_addr_t dma_addr;
71         void *os_buf;
72         unsigned int len;
73         unsigned int index;
74         int sop;
75         void *desc;
76         uint64_t wr_id; /* Cookie */
77         uint8_t cq_entry; /* Gets completion event from hw */
78         uint8_t desc_skip_cnt; /* Num descs to occupy */
79         uint8_t compressed_send; /* Both hdr and payload in one desc */
80 };
81
82 /* Break the vnic_wq_buf allocations into blocks of 32/64 entries */
83 #define VNIC_WQ_BUF_MIN_BLK_ENTRIES 32
84 #define VNIC_WQ_BUF_DFLT_BLK_ENTRIES 64
85 #define VNIC_WQ_BUF_BLK_ENTRIES(entries) \
86         ((unsigned int)((entries < VNIC_WQ_BUF_DFLT_BLK_ENTRIES) ? \
87         VNIC_WQ_BUF_MIN_BLK_ENTRIES : VNIC_WQ_BUF_DFLT_BLK_ENTRIES))
88 #define VNIC_WQ_BUF_BLK_SZ(entries) \
89         (VNIC_WQ_BUF_BLK_ENTRIES(entries) * sizeof(struct vnic_wq_buf))
90 #define VNIC_WQ_BUF_BLKS_NEEDED(entries) \
91         DIV_ROUND_UP(entries, VNIC_WQ_BUF_BLK_ENTRIES(entries))
92 #define VNIC_WQ_BUF_BLKS_MAX VNIC_WQ_BUF_BLKS_NEEDED(4096)
93
94 struct vnic_wq {
95         unsigned int index;
96         struct vnic_dev *vdev;
97         struct vnic_wq_ctrl __iomem *ctrl;              /* memory-mapped */
98         struct vnic_dev_ring ring;
99         struct vnic_wq_buf *bufs[VNIC_WQ_BUF_BLKS_MAX];
100         struct vnic_wq_buf *to_use;
101         struct vnic_wq_buf *to_clean;
102         unsigned int pkts_outstanding;
103         unsigned int socket_id;
104 };
105
106 static inline unsigned int vnic_wq_desc_avail(struct vnic_wq *wq)
107 {
108         /* how many does SW own? */
109         return wq->ring.desc_avail;
110 }
111
112 static inline unsigned int vnic_wq_desc_used(struct vnic_wq *wq)
113 {
114         /* how many does HW own? */
115         return wq->ring.desc_count - wq->ring.desc_avail - 1;
116 }
117
118 static inline void *vnic_wq_next_desc(struct vnic_wq *wq)
119 {
120         return wq->to_use->desc;
121 }
122
123 #define PI_LOG2_CACHE_LINE_SIZE        5
124 #define PI_INDEX_BITS            12
125 #define PI_INDEX_MASK ((1U << PI_INDEX_BITS) - 1)
126 #define PI_PREFETCH_LEN_MASK ((1U << PI_LOG2_CACHE_LINE_SIZE) - 1)
127 #define PI_PREFETCH_LEN_OFF 16
128 #define PI_PREFETCH_ADDR_BITS 43
129 #define PI_PREFETCH_ADDR_MASK ((1ULL << PI_PREFETCH_ADDR_BITS) - 1)
130 #define PI_PREFETCH_ADDR_OFF 21
131
132 /** How many cache lines are touched by buffer (addr, len). */
133 static inline unsigned int num_cache_lines_touched(dma_addr_t addr,
134                                                         unsigned int len)
135 {
136         const unsigned long mask = PI_PREFETCH_LEN_MASK;
137         const unsigned long laddr = (unsigned long)addr;
138         unsigned long lines, equiv_len;
139         /* A. If addr is aligned, our solution is just to round up len to the
140         next boundary.
141
142         e.g. addr = 0, len = 48
143         +--------------------+
144         |XXXXXXXXXXXXXXXXXXXX|    32-byte cacheline a
145         +--------------------+
146         |XXXXXXXXXX          |    cacheline b
147         +--------------------+
148
149         B. If addr is not aligned, however, we may use an extra
150         cacheline.  e.g. addr = 12, len = 22
151
152         +--------------------+
153         |       XXXXXXXXXXXXX|
154         +--------------------+
155         |XX                  |
156         +--------------------+
157
158         Our solution is to make the problem equivalent to case A
159         above by adding the empty space in the first cacheline to the length:
160         unsigned long len;
161
162         +--------------------+
163         |eeeeeeeXXXXXXXXXXXXX|    "e" is empty space, which we add to len
164         +--------------------+
165         |XX                  |
166         +--------------------+
167
168         */
169         equiv_len = len + (laddr & mask);
170
171         /* Now we can just round up this len to the next 32-byte boundary. */
172         lines = (equiv_len + mask) & (~mask);
173
174         /* Scale bytes -> cachelines. */
175         return lines >> PI_LOG2_CACHE_LINE_SIZE;
176 }
177
178 static inline u64 vnic_cached_posted_index(dma_addr_t addr, unsigned int len,
179                                                 unsigned int index)
180 {
181         unsigned int num_cache_lines = num_cache_lines_touched(addr, len);
182         /* Wish we could avoid a branch here.  We could have separate
183          * vnic_wq_post() and vinc_wq_post_inline(), the latter
184          * only supporting < 1k (2^5 * 2^5) sends, I suppose.  This would
185          * eliminate the if (eop) branch as well.
186          */
187         if (num_cache_lines > PI_PREFETCH_LEN_MASK)
188                 num_cache_lines = 0;
189         return (index & PI_INDEX_MASK) |
190         ((num_cache_lines & PI_PREFETCH_LEN_MASK) << PI_PREFETCH_LEN_OFF) |
191                 (((addr >> PI_LOG2_CACHE_LINE_SIZE) &
192         PI_PREFETCH_ADDR_MASK) << PI_PREFETCH_ADDR_OFF);
193 }
194
195 static inline void vnic_wq_post(struct vnic_wq *wq,
196         void *os_buf, dma_addr_t dma_addr,
197         unsigned int len, int sop, int eop,
198         uint8_t desc_skip_cnt, uint8_t cq_entry,
199         uint8_t compressed_send, uint64_t wrid)
200 {
201         struct vnic_wq_buf *buf = wq->to_use;
202
203         buf->sop = sop;
204         buf->cq_entry = cq_entry;
205         buf->compressed_send = compressed_send;
206         buf->desc_skip_cnt = desc_skip_cnt;
207         buf->os_buf = os_buf;
208         buf->dma_addr = dma_addr;
209         buf->len = len;
210         buf->wr_id = wrid;
211
212         buf = buf->next;
213         if (eop) {
214 #ifdef DO_PREFETCH
215                 uint64_t wr = vnic_cached_posted_index(dma_addr, len,
216                                                         buf->index);
217 #endif
218                 /* Adding write memory barrier prevents compiler and/or CPU
219                  * reordering, thus avoiding descriptor posting before
220                  * descriptor is initialized. Otherwise, hardware can read
221                  * stale descriptor fields.
222                  */
223                 wmb();
224 #ifdef DO_PREFETCH
225                 /* Intel chipsets seem to limit the rate of PIOs that we can
226                  * push on the bus.  Thus, it is very important to do a single
227                  * 64 bit write here.  With two 32-bit writes, my maximum
228                  * pkt/sec rate was cut almost in half. -AJF
229                  */
230                 iowrite64((uint64_t)wr, &wq->ctrl->posted_index);
231 #else
232                 iowrite32(buf->index, &wq->ctrl->posted_index);
233 #endif
234         }
235         wq->to_use = buf;
236
237         wq->ring.desc_avail -= desc_skip_cnt;
238 }
239
240 static inline void vnic_wq_service(struct vnic_wq *wq,
241         struct cq_desc *cq_desc, u16 completed_index,
242         void (*buf_service)(struct vnic_wq *wq,
243         struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque),
244         void *opaque)
245 {
246         struct vnic_wq_buf *buf;
247
248         buf = wq->to_clean;
249         while (1) {
250
251                 (*buf_service)(wq, cq_desc, buf, opaque);
252
253                 wq->ring.desc_avail++;
254
255                 wq->to_clean = buf->next;
256
257                 if (buf->index == completed_index)
258                         break;
259
260                 buf = wq->to_clean;
261         }
262 }
263
264 void vnic_wq_free(struct vnic_wq *wq);
265 int vnic_wq_alloc(struct vnic_dev *vdev, struct vnic_wq *wq, unsigned int index,
266         unsigned int desc_count, unsigned int desc_size);
267 void vnic_wq_init_start(struct vnic_wq *wq, unsigned int cq_index,
268         unsigned int fetch_index, unsigned int posted_index,
269         unsigned int error_interrupt_enable,
270         unsigned int error_interrupt_offset);
271 void vnic_wq_init(struct vnic_wq *wq, unsigned int cq_index,
272         unsigned int error_interrupt_enable,
273         unsigned int error_interrupt_offset);
274 void vnic_wq_error_out(struct vnic_wq *wq, unsigned int error);
275 unsigned int vnic_wq_error_status(struct vnic_wq *wq);
276 void vnic_wq_enable(struct vnic_wq *wq);
277 int vnic_wq_disable(struct vnic_wq *wq);
278 void vnic_wq_clean(struct vnic_wq *wq,
279         void (*buf_clean)(struct vnic_wq *wq, struct vnic_wq_buf *buf));
280 int vnic_wq_mem_size(struct vnic_wq *wq, unsigned int desc_count,
281         unsigned int desc_size);
282
283 #endif /* _VNIC_WQ_H_ */