2 * Copyright 2008-2010 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
5 * Copyright (c) 2014, Cisco Systems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
42 /* Work queue control */
44 u64 ring_base; /* 0x00 */
45 u32 ring_size; /* 0x08 */
47 u32 posted_index; /* 0x10 */
49 u32 cq_index; /* 0x18 */
51 u32 enable; /* 0x20 */
53 u32 running; /* 0x28 */
55 u32 fetch_index; /* 0x30 */
57 u32 dca_value; /* 0x38 */
59 u32 error_interrupt_enable; /* 0x40 */
61 u32 error_interrupt_offset; /* 0x48 */
63 u32 error_status; /* 0x50 */
68 struct vnic_wq_buf *next;
75 uint64_t wr_id; /* Cookie */
76 uint8_t cq_entry; /* Gets completion event from hw */
77 uint8_t desc_skip_cnt; /* Num descs to occupy */
78 uint8_t compressed_send; /* Both hdr and payload in one desc */
81 /* Break the vnic_wq_buf allocations into blocks of 32/64 entries */
82 #define VNIC_WQ_BUF_MIN_BLK_ENTRIES 32
83 #define VNIC_WQ_BUF_DFLT_BLK_ENTRIES 64
84 #define VNIC_WQ_BUF_BLK_ENTRIES(entries) \
85 ((unsigned int)((entries < VNIC_WQ_BUF_DFLT_BLK_ENTRIES) ? \
86 VNIC_WQ_BUF_MIN_BLK_ENTRIES : VNIC_WQ_BUF_DFLT_BLK_ENTRIES))
87 #define VNIC_WQ_BUF_BLK_SZ(entries) \
88 (VNIC_WQ_BUF_BLK_ENTRIES(entries) * sizeof(struct vnic_wq_buf))
89 #define VNIC_WQ_BUF_BLKS_NEEDED(entries) \
90 DIV_ROUND_UP(entries, VNIC_WQ_BUF_BLK_ENTRIES(entries))
91 #define VNIC_WQ_BUF_BLKS_MAX VNIC_WQ_BUF_BLKS_NEEDED(4096)
95 struct vnic_dev *vdev;
96 struct vnic_wq_ctrl __iomem *ctrl; /* memory-mapped */
97 struct vnic_dev_ring ring;
98 struct vnic_wq_buf *bufs[VNIC_WQ_BUF_BLKS_MAX];
99 struct vnic_wq_buf *to_use;
100 struct vnic_wq_buf *to_clean;
101 unsigned int pkts_outstanding;
102 unsigned int socket_id;
105 static inline unsigned int vnic_wq_desc_avail(struct vnic_wq *wq)
107 /* how many does SW own? */
108 return wq->ring.desc_avail;
111 static inline unsigned int vnic_wq_desc_used(struct vnic_wq *wq)
113 /* how many does HW own? */
114 return wq->ring.desc_count - wq->ring.desc_avail - 1;
117 static inline void *vnic_wq_next_desc(struct vnic_wq *wq)
119 return wq->to_use->desc;
122 #define PI_LOG2_CACHE_LINE_SIZE 5
123 #define PI_INDEX_BITS 12
124 #define PI_INDEX_MASK ((1U << PI_INDEX_BITS) - 1)
125 #define PI_PREFETCH_LEN_MASK ((1U << PI_LOG2_CACHE_LINE_SIZE) - 1)
126 #define PI_PREFETCH_LEN_OFF 16
127 #define PI_PREFETCH_ADDR_BITS 43
128 #define PI_PREFETCH_ADDR_MASK ((1ULL << PI_PREFETCH_ADDR_BITS) - 1)
129 #define PI_PREFETCH_ADDR_OFF 21
131 /** How many cache lines are touched by buffer (addr, len). */
132 static inline unsigned int num_cache_lines_touched(dma_addr_t addr,
135 const unsigned long mask = PI_PREFETCH_LEN_MASK;
136 const unsigned long laddr = (unsigned long)addr;
137 unsigned long lines, equiv_len;
138 /* A. If addr is aligned, our solution is just to round up len to the
141 e.g. addr = 0, len = 48
142 +--------------------+
143 |XXXXXXXXXXXXXXXXXXXX| 32-byte cacheline a
144 +--------------------+
145 |XXXXXXXXXX | cacheline b
146 +--------------------+
148 B. If addr is not aligned, however, we may use an extra
149 cacheline. e.g. addr = 12, len = 22
151 +--------------------+
153 +--------------------+
155 +--------------------+
157 Our solution is to make the problem equivalent to case A
158 above by adding the empty space in the first cacheline to the length:
161 +--------------------+
162 |eeeeeeeXXXXXXXXXXXXX| "e" is empty space, which we add to len
163 +--------------------+
165 +--------------------+
168 equiv_len = len + (laddr & mask);
170 /* Now we can just round up this len to the next 32-byte boundary. */
171 lines = (equiv_len + mask) & (~mask);
173 /* Scale bytes -> cachelines. */
174 return lines >> PI_LOG2_CACHE_LINE_SIZE;
177 static inline u64 vnic_cached_posted_index(dma_addr_t addr, unsigned int len,
180 unsigned int num_cache_lines = num_cache_lines_touched(addr, len);
181 /* Wish we could avoid a branch here. We could have separate
182 * vnic_wq_post() and vinc_wq_post_inline(), the latter
183 * only supporting < 1k (2^5 * 2^5) sends, I suppose. This would
184 * eliminate the if (eop) branch as well.
186 if (num_cache_lines > PI_PREFETCH_LEN_MASK)
188 return (index & PI_INDEX_MASK) |
189 ((num_cache_lines & PI_PREFETCH_LEN_MASK) << PI_PREFETCH_LEN_OFF) |
190 (((addr >> PI_LOG2_CACHE_LINE_SIZE) &
191 PI_PREFETCH_ADDR_MASK) << PI_PREFETCH_ADDR_OFF);
194 static inline void vnic_wq_post(struct vnic_wq *wq,
195 void *os_buf, dma_addr_t dma_addr,
196 unsigned int len, int sop, int eop,
197 uint8_t desc_skip_cnt, uint8_t cq_entry,
198 uint8_t compressed_send, uint64_t wrid)
200 struct vnic_wq_buf *buf = wq->to_use;
203 buf->cq_entry = cq_entry;
204 buf->compressed_send = compressed_send;
205 buf->desc_skip_cnt = desc_skip_cnt;
206 buf->os_buf = os_buf;
207 buf->dma_addr = dma_addr;
214 uint64_t wr = vnic_cached_posted_index(dma_addr, len,
217 /* Adding write memory barrier prevents compiler and/or CPU
218 * reordering, thus avoiding descriptor posting before
219 * descriptor is initialized. Otherwise, hardware can read
220 * stale descriptor fields.
224 /* Intel chipsets seem to limit the rate of PIOs that we can
225 * push on the bus. Thus, it is very important to do a single
226 * 64 bit write here. With two 32-bit writes, my maximum
227 * pkt/sec rate was cut almost in half. -AJF
229 iowrite64((uint64_t)wr, &wq->ctrl->posted_index);
231 iowrite32(buf->index, &wq->ctrl->posted_index);
236 wq->ring.desc_avail -= desc_skip_cnt;
239 static inline void vnic_wq_service(struct vnic_wq *wq,
240 struct cq_desc *cq_desc, u16 completed_index,
241 void (*buf_service)(struct vnic_wq *wq,
242 struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque),
245 struct vnic_wq_buf *buf;
250 (*buf_service)(wq, cq_desc, buf, opaque);
252 wq->ring.desc_avail++;
254 wq->to_clean = buf->next;
256 if (buf->index == completed_index)
263 void vnic_wq_free(struct vnic_wq *wq);
264 int vnic_wq_alloc(struct vnic_dev *vdev, struct vnic_wq *wq, unsigned int index,
265 unsigned int desc_count, unsigned int desc_size);
266 void vnic_wq_init_start(struct vnic_wq *wq, unsigned int cq_index,
267 unsigned int fetch_index, unsigned int posted_index,
268 unsigned int error_interrupt_enable,
269 unsigned int error_interrupt_offset);
270 void vnic_wq_init(struct vnic_wq *wq, unsigned int cq_index,
271 unsigned int error_interrupt_enable,
272 unsigned int error_interrupt_offset);
273 void vnic_wq_error_out(struct vnic_wq *wq, unsigned int error);
274 unsigned int vnic_wq_error_status(struct vnic_wq *wq);
275 void vnic_wq_enable(struct vnic_wq *wq);
276 int vnic_wq_disable(struct vnic_wq *wq);
277 void vnic_wq_clean(struct vnic_wq *wq,
278 void (*buf_clean)(struct vnic_wq *wq, struct vnic_wq_buf *buf));
279 int vnic_wq_mem_size(struct vnic_wq *wq, unsigned int desc_count,
280 unsigned int desc_size);
282 #endif /* _VNIC_WQ_H_ */