1/* $NetBSD: ring.h,v 1.3 2016/01/06 15:28:40 bouyer Exp $ */
2/******************************************************************************
3 * ring.h
4 *
5 * Shared producer-consumer ring macros.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to
9 * deal in the Software without restriction, including without limitation the
10 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Tim Deegan and Andrew Warfield November 2004.
26 */
27
28#ifndef __XEN_PUBLIC_IO_RING_H__
29#define __XEN_PUBLIC_IO_RING_H__
30
31#include "../xen-compat.h"
32
33#if __XEN_INTERFACE_VERSION__ < 0x00030208
34#if defined(__Linux__)
35#define xen_mb() mb()
36#define xen_rmb() rmb()
37#define xen_wmb() wmb()
38#endif
39#endif
40
41#if defined(__NetBSD__)
42#define xen_mb() x86_mfence()
43#define xen_rmb() x86_lfence()
44#define xen_wmb() x86_sfence()
45#endif
46
47typedef unsigned int RING_IDX;
48
49/* Round a 32-bit unsigned constant down to the nearest power of two. */
50#define __RD2(_x) (((_x) & 0x00000002) ? 0x2 : ((_x) & 0x1))
51#define __RD4(_x) (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2 : __RD2(_x))
52#define __RD8(_x) (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4 : __RD4(_x))
53#define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8 : __RD8(_x))
54#define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
55
56/*
57 * Calculate size of a shared ring, given the total available space for the
58 * ring and indexes (_sz), and the name tag of the request/response structure.
59 * A ring contains as many entries as will fit, rounded down to the nearest
60 * power of two (so we can mask with (size-1) to loop around).
61 */
62#define __CONST_RING_SIZE(_s, _sz) \
63 (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \
64 sizeof(((struct _s##_sring *)0)->ring[0])))
65/*
66 * The same for passing in an actual pointer instead of a name tag.
67 */
68#define __RING_SIZE(_s, _sz) \
69 (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
70
71/*
72 * Macros to make the correct C datatypes for a new kind of ring.
73 *
74 * To make a new ring datatype, you need to have two message structures,
75 * let's say request_t, and response_t already defined.
76 *
77 * In a header where you want the ring datatype declared, you then do:
78 *
79 * DEFINE_RING_TYPES(mytag, request_t, response_t);
80 *
81 * These expand out to give you a set of types, as you can see below.
82 * The most important of these are:
83 *
84 * mytag_sring_t - The shared ring.
85 * mytag_front_ring_t - The 'front' half of the ring.
86 * mytag_back_ring_t - The 'back' half of the ring.
87 *
88 * To initialize a ring in your code you need to know the location and size
89 * of the shared memory area (PAGE_SIZE, for instance). To initialise
90 * the front half:
91 *
92 * mytag_front_ring_t front_ring;
93 * SHARED_RING_INIT((mytag_sring_t *)shared_page);
94 * FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
95 *
96 * Initializing the back follows similarly (note that only the front
97 * initializes the shared ring):
98 *
99 * mytag_back_ring_t back_ring;
100 * BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
101 */
102
103#define DEFINE_RING_TYPES(__name, __req_t, __rsp_t) \
104 \
105/* Shared ring entry */ \
106union __name##_sring_entry { \
107 __req_t req; \
108 __rsp_t rsp; \
109}; \
110 \
111/* Shared ring page */ \
112struct __name##_sring { \
113 RING_IDX req_prod, req_event; \
114 RING_IDX rsp_prod, rsp_event; \
115 union { \
116 struct { \
117 uint8_t smartpoll_active; \
118 } netif; \
119 struct { \
120 uint8_t msg; \
121 } tapif_user; \
122 uint8_t pvt_pad[4]; \
123 } private; \
124 uint8_t __pad[44]; \
125 union __name##_sring_entry ring[1]; /* variable-length */ \
126}; \
127 \
128/* "Front" end's private variables */ \
129struct __name##_front_ring { \
130 RING_IDX req_prod_pvt; \
131 RING_IDX rsp_cons; \
132 unsigned int nr_ents; \
133 struct __name##_sring *sring; \
134}; \
135 \
136/* "Back" end's private variables */ \
137struct __name##_back_ring { \
138 RING_IDX rsp_prod_pvt; \
139 RING_IDX req_cons; \
140 unsigned int nr_ents; \
141 struct __name##_sring *sring; \
142}; \
143 \
144/* Syntactic sugar */ \
145typedef struct __name##_sring __name##_sring_t; \
146typedef struct __name##_front_ring __name##_front_ring_t; \
147typedef struct __name##_back_ring __name##_back_ring_t
148
149/*
150 * Macros for manipulating rings.
151 *
152 * FRONT_RING_whatever works on the "front end" of a ring: here
153 * requests are pushed on to the ring and responses taken off it.
154 *
155 * BACK_RING_whatever works on the "back end" of a ring: here
156 * requests are taken off the ring and responses put on.
157 *
158 * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
159 * This is OK in 1-for-1 request-response situations where the
160 * requestor (front end) never has more than RING_SIZE()-1
161 * outstanding requests.
162 */
163
164/* Initialising empty rings */
165#define SHARED_RING_INIT(_s) do { \
166 (_s)->req_prod = (_s)->rsp_prod = 0; \
167 (_s)->req_event = (_s)->rsp_event = 1; \
168 (void)memset((_s)->private.pvt_pad, 0, sizeof((_s)->private.pvt_pad)); \
169 (void)memset((_s)->__pad, 0, sizeof((_s)->__pad)); \
170} while(0)
171
172#define FRONT_RING_INIT(_r, _s, __size) do { \
173 (_r)->req_prod_pvt = 0; \
174 (_r)->rsp_cons = 0; \
175 (_r)->nr_ents = __RING_SIZE(_s, __size); \
176 (_r)->sring = (_s); \
177} while (0)
178
179#define BACK_RING_INIT(_r, _s, __size) do { \
180 (_r)->rsp_prod_pvt = 0; \
181 (_r)->req_cons = 0; \
182 (_r)->nr_ents = __RING_SIZE(_s, __size); \
183 (_r)->sring = (_s); \
184} while (0)
185
186/* Initialize to existing shared indexes -- for recovery */
187#define FRONT_RING_ATTACH(_r, _s, __size) do { \
188 (_r)->sring = (_s); \
189 (_r)->req_prod_pvt = (_s)->req_prod; \
190 (_r)->rsp_cons = (_s)->rsp_prod; \
191 (_r)->nr_ents = __RING_SIZE(_s, __size); \
192} while (0)
193
194#define BACK_RING_ATTACH(_r, _s, __size) do { \
195 (_r)->sring = (_s); \
196 (_r)->rsp_prod_pvt = (_s)->rsp_prod; \
197 (_r)->req_cons = (_s)->req_prod; \
198 (_r)->nr_ents = __RING_SIZE(_s, __size); \
199} while (0)
200
201/* How big is this ring? */
202#define RING_SIZE(_r) \
203 ((_r)->nr_ents)
204
205/* Number of free requests (for use on front side only). */
206#define RING_FREE_REQUESTS(_r) \
207 (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
208
209/* Test if there is an empty slot available on the front ring.
210 * (This is only meaningful from the front. )
211 */
212#define RING_FULL(_r) \
213 (RING_FREE_REQUESTS(_r) == 0)
214
215/* Test if there are outstanding messages to be processed on a ring. */
216#define RING_HAS_UNCONSUMED_RESPONSES(_r) \
217 ((_r)->sring->rsp_prod - (_r)->rsp_cons)
218
219#ifdef __GNUC__
220#define RING_HAS_UNCONSUMED_REQUESTS(_r) ({ \
221 unsigned int req = (_r)->sring->req_prod - (_r)->req_cons; \
222 unsigned int rsp = RING_SIZE(_r) - \
223 ((_r)->req_cons - (_r)->rsp_prod_pvt); \
224 req < rsp ? req : rsp; \
225})
226#else
227/* Same as above, but without the nice GCC ({ ... }) syntax. */
228#define RING_HAS_UNCONSUMED_REQUESTS(_r) \
229 ((((_r)->sring->req_prod - (_r)->req_cons) < \
230 (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ? \
231 ((_r)->sring->req_prod - (_r)->req_cons) : \
232 (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
233#endif
234
235/* Direct access to individual ring elements, by index. */
236#define RING_GET_REQUEST(_r, _idx) \
237 (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
238
239/*
240 * Get a local copy of a request.
241 *
242 * Use this in preference to RING_GET_REQUEST() so all processing is
243 * done on a local copy that cannot be modified by the other end.
244 *
245 * Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this
246 * to be ineffective where _req is a struct which consists of only bitfields.
247 */
248#define RING_COPY_REQUEST(_r, _idx, _req) do { \
249 /* Use volatile to force the copy into _req. */ \
250 *(_req) = *(volatile typeof(_req))RING_GET_REQUEST(_r, _idx); \
251} while (0)
252
253#define RING_GET_RESPONSE(_r, _idx) \
254 (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
255
256/* Loop termination condition: Would the specified index overflow the ring? */
257#define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \
258 (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
259
260#define RING_PUSH_REQUESTS(_r) do { \
261 xen_wmb(); /* back sees requests /before/ updated producer index */ \
262 (_r)->sring->req_prod = (_r)->req_prod_pvt; \
263} while (0)
264
265#define RING_PUSH_RESPONSES(_r) do { \
266 xen_wmb(); /* front sees resps /before/ updated producer index */ \
267 (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt; \
268} while (0)
269
270/*
271 * Notification hold-off (req_event and rsp_event):
272 *
273 * When queueing requests or responses on a shared ring, it may not always be
274 * necessary to notify the remote end. For example, if requests are in flight
275 * in a backend, the front may be able to queue further requests without
276 * notifying the back (if the back checks for new requests when it queues
277 * responses).
278 *
279 * When enqueuing requests or responses:
280 *
281 * Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
282 * is a boolean return value. True indicates that the receiver requires an
283 * asynchronous notification.
284 *
285 * After dequeuing requests or responses (before sleeping the connection):
286 *
287 * Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
288 * The second argument is a boolean return value. True indicates that there
289 * are pending messages on the ring (i.e., the connection should not be put
290 * to sleep).
291 *
292 * These macros will set the req_event/rsp_event field to trigger a
293 * notification on the very next message that is enqueued. If you want to
294 * create batches of work (i.e., only receive a notification after several
295 * messages have been enqueued) then you will need to create a customised
296 * version of the FINAL_CHECK macro in your own code, which sets the event
297 * field appropriately.
298 */
299
300#define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \
301 RING_IDX __old = (_r)->sring->req_prod; \
302 RING_IDX __new = (_r)->req_prod_pvt; \
303 xen_wmb(); /* back sees requests /before/ updated producer index */ \
304 (_r)->sring->req_prod = __new; \
305 xen_mb(); /* back sees new requests /before/ we check req_event */ \
306 (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) < \
307 (RING_IDX)(__new - __old)); \
308} while (0)
309
310#define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \
311 RING_IDX __old = (_r)->sring->rsp_prod; \
312 RING_IDX __new = (_r)->rsp_prod_pvt; \
313 xen_wmb(); /* front sees resps /before/ updated producer index */ \
314 (_r)->sring->rsp_prod = __new; \
315 xen_mb(); /* front sees new resps /before/ we check rsp_event */ \
316 (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) < \
317 (RING_IDX)(__new - __old)); \
318} while (0)
319
320#define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \
321 (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
322 if (_work_to_do) break; \
323 (_r)->sring->req_event = (_r)->req_cons + 1; \
324 xen_mb(); \
325 (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
326} while (0)
327
328#define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \
329 (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
330 if (_work_to_do) break; \
331 (_r)->sring->rsp_event = (_r)->rsp_cons + 1; \
332 xen_mb(); \
333 (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
334} while (0)
335
336#endif /* __XEN_PUBLIC_IO_RING_H__ */
337
338/*
339 * Local variables:
340 * mode: C
341 * c-set-style: "BSD"
342 * c-basic-offset: 4
343 * tab-width: 4
344 * indent-tabs-mode: nil
345 * End:
346 */
347