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