net/enic: streamline mbuf handling in Tx path
[dpdk.git] / drivers / net / enic / base / 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
35 #ifndef _VNIC_WQ_H_
36 #define _VNIC_WQ_H_
37
38
39 #include "vnic_dev.h"
40 #include "vnic_cq.h"
41
42 /* Work queue control */
43 struct vnic_wq_ctrl {
44         u64 ring_base;                  /* 0x00 */
45         u32 ring_size;                  /* 0x08 */
46         u32 pad0;
47         u32 posted_index;               /* 0x10 */
48         u32 pad1;
49         u32 cq_index;                   /* 0x18 */
50         u32 pad2;
51         u32 enable;                     /* 0x20 */
52         u32 pad3;
53         u32 running;                    /* 0x28 */
54         u32 pad4;
55         u32 fetch_index;                /* 0x30 */
56         u32 pad5;
57         u32 dca_value;                  /* 0x38 */
58         u32 pad6;
59         u32 error_interrupt_enable;     /* 0x40 */
60         u32 pad7;
61         u32 error_interrupt_offset;     /* 0x48 */
62         u32 pad8;
63         u32 error_status;               /* 0x50 */
64         u32 pad9;
65 };
66
67 /* 16 bytes */
68 struct vnic_wq_buf {
69         void *mb;
70 };
71
72 struct vnic_wq {
73         unsigned int index;
74         struct vnic_dev *vdev;
75         struct vnic_wq_ctrl __iomem *ctrl;              /* memory-mapped */
76         struct vnic_dev_ring ring;
77         struct vnic_wq_buf *bufs;
78         unsigned int head_idx;
79         unsigned int tail_idx;
80         unsigned int pkts_outstanding;
81         unsigned int socket_id;
82 };
83
84 static inline unsigned int vnic_wq_desc_avail(struct vnic_wq *wq)
85 {
86         /* how many does SW own? */
87         return wq->ring.desc_avail;
88 }
89
90 static inline unsigned int vnic_wq_desc_used(struct vnic_wq *wq)
91 {
92         /* how many does HW own? */
93         return wq->ring.desc_count - wq->ring.desc_avail - 1;
94 }
95
96 #define PI_LOG2_CACHE_LINE_SIZE        5
97 #define PI_INDEX_BITS            12
98 #define PI_INDEX_MASK ((1U << PI_INDEX_BITS) - 1)
99 #define PI_PREFETCH_LEN_MASK ((1U << PI_LOG2_CACHE_LINE_SIZE) - 1)
100 #define PI_PREFETCH_LEN_OFF 16
101 #define PI_PREFETCH_ADDR_BITS 43
102 #define PI_PREFETCH_ADDR_MASK ((1ULL << PI_PREFETCH_ADDR_BITS) - 1)
103 #define PI_PREFETCH_ADDR_OFF 21
104
105 /** How many cache lines are touched by buffer (addr, len). */
106 static inline unsigned int num_cache_lines_touched(dma_addr_t addr,
107                                                         unsigned int len)
108 {
109         const unsigned long mask = PI_PREFETCH_LEN_MASK;
110         const unsigned long laddr = (unsigned long)addr;
111         unsigned long lines, equiv_len;
112         /* A. If addr is aligned, our solution is just to round up len to the
113         next boundary.
114
115         e.g. addr = 0, len = 48
116         +--------------------+
117         |XXXXXXXXXXXXXXXXXXXX|    32-byte cacheline a
118         +--------------------+
119         |XXXXXXXXXX          |    cacheline b
120         +--------------------+
121
122         B. If addr is not aligned, however, we may use an extra
123         cacheline.  e.g. addr = 12, len = 22
124
125         +--------------------+
126         |       XXXXXXXXXXXXX|
127         +--------------------+
128         |XX                  |
129         +--------------------+
130
131         Our solution is to make the problem equivalent to case A
132         above by adding the empty space in the first cacheline to the length:
133         unsigned long len;
134
135         +--------------------+
136         |eeeeeeeXXXXXXXXXXXXX|    "e" is empty space, which we add to len
137         +--------------------+
138         |XX                  |
139         +--------------------+
140
141         */
142         equiv_len = len + (laddr & mask);
143
144         /* Now we can just round up this len to the next 32-byte boundary. */
145         lines = (equiv_len + mask) & (~mask);
146
147         /* Scale bytes -> cachelines. */
148         return lines >> PI_LOG2_CACHE_LINE_SIZE;
149 }
150
151 static inline u64 vnic_cached_posted_index(dma_addr_t addr, unsigned int len,
152                                                 unsigned int index)
153 {
154         unsigned int num_cache_lines = num_cache_lines_touched(addr, len);
155         /* Wish we could avoid a branch here.  We could have separate
156          * vnic_wq_post() and vinc_wq_post_inline(), the latter
157          * only supporting < 1k (2^5 * 2^5) sends, I suppose.  This would
158          * eliminate the if (eop) branch as well.
159          */
160         if (num_cache_lines > PI_PREFETCH_LEN_MASK)
161                 num_cache_lines = 0;
162         return (index & PI_INDEX_MASK) |
163         ((num_cache_lines & PI_PREFETCH_LEN_MASK) << PI_PREFETCH_LEN_OFF) |
164                 (((addr >> PI_LOG2_CACHE_LINE_SIZE) &
165         PI_PREFETCH_ADDR_MASK) << PI_PREFETCH_ADDR_OFF);
166 }
167
168 static inline uint32_t
169 buf_idx_incr(uint32_t n_descriptors, uint32_t idx)
170 {
171         idx++;
172         if (unlikely(idx == n_descriptors))
173                 idx = 0;
174         return idx;
175 }
176
177 static inline void vnic_wq_service(struct vnic_wq *wq,
178         struct cq_desc *cq_desc, u16 completed_index,
179         void (*buf_service)(struct vnic_wq *wq,
180         struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque),
181         void *opaque)
182 {
183         struct vnic_wq_buf *buf;
184         unsigned int to_clean = wq->tail_idx;
185
186         buf = &wq->bufs[to_clean];
187         while (1) {
188
189                 (*buf_service)(wq, cq_desc, buf, opaque);
190
191                 wq->ring.desc_avail++;
192
193
194                 to_clean = buf_idx_incr(wq->ring.desc_count, to_clean);
195
196                 if (to_clean == completed_index)
197                         break;
198
199                 buf = &wq->bufs[to_clean];
200         }
201         wq->tail_idx = to_clean;
202 }
203
204 void vnic_wq_free(struct vnic_wq *wq);
205 int vnic_wq_alloc(struct vnic_dev *vdev, struct vnic_wq *wq, unsigned int index,
206         unsigned int desc_count, unsigned int desc_size);
207 void vnic_wq_init_start(struct vnic_wq *wq, unsigned int cq_index,
208         unsigned int fetch_index, unsigned int posted_index,
209         unsigned int error_interrupt_enable,
210         unsigned int error_interrupt_offset);
211 void vnic_wq_init(struct vnic_wq *wq, unsigned int cq_index,
212         unsigned int error_interrupt_enable,
213         unsigned int error_interrupt_offset);
214 void vnic_wq_error_out(struct vnic_wq *wq, unsigned int error);
215 unsigned int vnic_wq_error_status(struct vnic_wq *wq);
216 void vnic_wq_enable(struct vnic_wq *wq);
217 int vnic_wq_disable(struct vnic_wq *wq);
218 void vnic_wq_clean(struct vnic_wq *wq,
219         void (*buf_clean)(struct vnic_wq *wq, struct vnic_wq_buf *buf));
220 int vnic_wq_mem_size(struct vnic_wq *wq, unsigned int desc_count,
221         unsigned int desc_size);
222
223 #endif /* _VNIC_WQ_H_ */