• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 UINT32 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  * Calculate size of a shared ring, given the total available space for the
49  * ring and indexes (_sz), and the name tag of the request/response structure.
50  * A ring contains as many entries as will fit, rounded down to the nearest
51  * power of two (so we can mask with (size-1) to loop around).
52  */
53 #define __CONST_RING_SIZE(_s, _sz) \
54     (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \
55 	    sizeof(((struct _s##_sring *)0)->ring[0])))
56 /*
57  * The same for passing in an actual pointer instead of a name tag.
58  */
59 #define __RING_SIZE(_s, _sz) \
60     (__RD32(((_sz) - (INTN)(_s)->ring + (INTN)(_s)) / sizeof((_s)->ring[0])))
61 
62 /*
63  * Macros to make the correct C datatypes for a new kind of ring.
64  *
65  * To make a new ring datatype, you need to have two message structures,
66  * let's say request_t, and response_t already defined.
67  *
68  * In a header where you want the ring datatype declared, you then do:
69  *
70  *     DEFINE_RING_TYPES(mytag, request_t, response_t);
71  *
72  * These expand out to give you a set of types, as you can see below.
73  * The most important of these are:
74  *
75  *     mytag_sring_t      - The shared ring.
76  *     mytag_front_ring_t - The 'front' half of the ring.
77  *     mytag_back_ring_t  - The 'back' half of the ring.
78  *
79  * To initialize a ring in your code you need to know the location and size
80  * of the shared memory area (PAGE_SIZE, for instance). To initialise
81  * the front half:
82  *
83  *     mytag_front_ring_t front_ring;
84  *     SHARED_RING_INIT((mytag_sring_t *)shared_page);
85  *     FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
86  *
87  * Initializing the back follows similarly (note that only the front
88  * initializes the shared ring):
89  *
90  *     mytag_back_ring_t back_ring;
91  *     BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
92  */
93 
94 #define DEFINE_RING_TYPES(__name, __req_t, __rsp_t)                     \
95                                                                         \
96 /* Shared ring entry */                                                 \
97 union __name##_sring_entry {                                            \
98     __req_t req;                                                        \
99     __rsp_t rsp;                                                        \
100 };                                                                      \
101                                                                         \
102 /* Shared ring page */                                                  \
103 struct __name##_sring {                                                 \
104     RING_IDX req_prod, req_event;                                       \
105     RING_IDX rsp_prod, rsp_event;                                       \
106     union {                                                             \
107         struct {                                                        \
108             UINT8 smartpoll_active;                                   \
109         } netif;                                                        \
110         struct {                                                        \
111             UINT8 msg;                                                \
112         } tapif_user;                                                   \
113         UINT8 pvt_pad[4];                                             \
114     } private;                                                          \
115     UINT8 __pad[44];                                                  \
116     union __name##_sring_entry ring[1]; /* variable-length */           \
117 };                                                                      \
118                                                                         \
119 /* "Front" end's private variables */                                   \
120 struct __name##_front_ring {                                            \
121     RING_IDX req_prod_pvt;                                              \
122     RING_IDX rsp_cons;                                                  \
123     UINT32 nr_ents;                                               \
124     struct __name##_sring *sring;                                       \
125 };                                                                      \
126                                                                         \
127 /* "Back" end's private variables */                                    \
128 struct __name##_back_ring {                                             \
129     RING_IDX rsp_prod_pvt;                                              \
130     RING_IDX req_cons;                                                  \
131     UINT32 nr_ents;                                               \
132     struct __name##_sring *sring;                                       \
133 };                                                                      \
134                                                                         \
135 /* Syntactic sugar */                                                   \
136 typedef struct __name##_sring __name##_sring_t;                         \
137 typedef struct __name##_front_ring __name##_front_ring_t;               \
138 typedef struct __name##_back_ring __name##_back_ring_t
139 
140 /*
141  * Macros for manipulating rings.
142  *
143  * FRONT_RING_whatever works on the "front end" of a ring: here
144  * requests are pushed on to the ring and responses taken off it.
145  *
146  * BACK_RING_whatever works on the "back end" of a ring: here
147  * requests are taken off the ring and responses put on.
148  *
149  * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
150  * This is OK in 1-for-1 request-response situations where the
151  * requestor (front end) never has more than RING_SIZE()-1
152  * outstanding requests.
153  */
154 
155 /* Initialising empty rings */
156 #define SHARED_RING_INIT(_s) do {                                       \
157     (_s)->req_prod  = (_s)->rsp_prod  = 0;                              \
158     (_s)->req_event = (_s)->rsp_event = 1;                              \
159     (VOID)ZeroMem((_s)->private.pvt_pad, sizeof((_s)->private.pvt_pad)); \
160     (VOID)ZeroMem((_s)->__pad, sizeof((_s)->__pad));                  \
161 } while(0)
162 
163 #define FRONT_RING_INIT(_r, _s, __size) do {                            \
164     (_r)->req_prod_pvt = 0;                                             \
165     (_r)->rsp_cons = 0;                                                 \
166     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
167     (_r)->sring = (_s);                                                 \
168 } while (0)
169 
170 #define BACK_RING_INIT(_r, _s, __size) do {                             \
171     (_r)->rsp_prod_pvt = 0;                                             \
172     (_r)->req_cons = 0;                                                 \
173     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
174     (_r)->sring = (_s);                                                 \
175 } while (0)
176 
177 /* How big is this ring? */
178 #define RING_SIZE(_r)                                                   \
179     ((_r)->nr_ents)
180 
181 /* Number of free requests (for use on front side only). */
182 #define RING_FREE_REQUESTS(_r)                                          \
183     (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
184 
185 /* Test if there is an empty slot available on the front ring.
186  * (This is only meaningful from the front. )
187  */
188 #define RING_FULL(_r)                                                   \
189     (RING_FREE_REQUESTS(_r) == 0)
190 
191 /* Test if there are outstanding messages to be processed on a ring. */
192 #define RING_HAS_UNCONSUMED_RESPONSES(_r)                               \
193     ((_r)->sring->rsp_prod - (_r)->rsp_cons)
194 
195 #ifdef __GNUC__
196 #define RING_HAS_UNCONSUMED_REQUESTS(_r) ({                             \
197     UINT32 req = (_r)->sring->req_prod - (_r)->req_cons;          \
198     UINT32 rsp = RING_SIZE(_r) -                                  \
199         ((_r)->req_cons - (_r)->rsp_prod_pvt);                          \
200     req < rsp ? req : rsp;                                              \
201 })
202 #else
203 /* Same as above, but without the nice GCC ({ ... }) syntax. */
204 #define RING_HAS_UNCONSUMED_REQUESTS(_r)                                \
205     ((((_r)->sring->req_prod - (_r)->req_cons) <                        \
206       (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ?        \
207      ((_r)->sring->req_prod - (_r)->req_cons) :                         \
208      (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
209 #endif
210 
211 /* Direct access to individual ring elements, by index. */
212 #define RING_GET_REQUEST(_r, _idx)                                      \
213     (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
214 
215 #define RING_GET_RESPONSE(_r, _idx)                                     \
216     (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
217 
218 /* Loop termination condition: Would the specified index overflow the ring? */
219 #define RING_REQUEST_CONS_OVERFLOW(_r, _cons)                           \
220     (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
221 
222 /* Ill-behaved frontend determination: Can there be this many requests? */
223 #define RING_REQUEST_PROD_OVERFLOW(_r, _prod)                           \
224     (((_prod) - (_r)->rsp_prod_pvt) > RING_SIZE(_r))
225 
226 #define RING_PUSH_REQUESTS(_r) do {                                     \
227     xen_wmb(); /* back sees requests /before/ updated producer index */ \
228     (_r)->sring->req_prod = (_r)->req_prod_pvt;                         \
229 } while (0)
230 
231 #define RING_PUSH_RESPONSES(_r) do {                                    \
232     xen_wmb(); /* front sees resps /before/ updated producer index */   \
233     (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt;                         \
234 } while (0)
235 
236 /*
237  * Notification hold-off (req_event and rsp_event):
238  *
239  * When queueing requests or responses on a shared ring, it may not always be
240  * necessary to notify the remote end. For example, if requests are in flight
241  * in a backend, the front may be able to queue further requests without
242  * notifying the back (if the back checks for new requests when it queues
243  * responses).
244  *
245  * When enqueuing requests or responses:
246  *
247  *  Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
248  *  is a boolean return value. True indicates that the receiver requires an
249  *  asynchronous notification.
250  *
251  * After dequeuing requests or responses (before sleeping the connection):
252  *
253  *  Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
254  *  The second argument is a boolean return value. True indicates that there
255  *  are pending messages on the ring (i.e., the connection should not be put
256  *  to sleep).
257  *
258  *  These macros will set the req_event/rsp_event field to trigger a
259  *  notification on the very next message that is enqueued. If you want to
260  *  create batches of work (i.e., only receive a notification after several
261  *  messages have been enqueued) then you will need to create a customised
262  *  version of the FINAL_CHECK macro in your own code, which sets the event
263  *  field appropriately.
264  */
265 
266 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do {           \
267     RING_IDX __old = (_r)->sring->req_prod;                             \
268     RING_IDX __new = (_r)->req_prod_pvt;                                \
269     xen_wmb(); /* back sees requests /before/ updated producer index */ \
270     (_r)->sring->req_prod = __new;                                      \
271     xen_mb(); /* back sees new requests /before/ we check req_event */  \
272     (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) <           \
273                  (RING_IDX)(__new - __old));                            \
274 } while (0)
275 
276 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do {          \
277     RING_IDX __old = (_r)->sring->rsp_prod;                             \
278     RING_IDX __new = (_r)->rsp_prod_pvt;                                \
279     xen_wmb(); /* front sees resps /before/ updated producer index */   \
280     (_r)->sring->rsp_prod = __new;                                      \
281     xen_mb(); /* front sees new resps /before/ we check rsp_event */    \
282     (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) <           \
283                  (RING_IDX)(__new - __old));                            \
284 } while (0)
285 
286 #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do {             \
287     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
288     if (_work_to_do) break;                                             \
289     (_r)->sring->req_event = (_r)->req_cons + 1;                        \
290     xen_mb();                                                           \
291     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
292 } while (0)
293 
294 #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do {            \
295     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
296     if (_work_to_do) break;                                             \
297     (_r)->sring->rsp_event = (_r)->rsp_cons + 1;                        \
298     xen_mb();                                                           \
299     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
300 } while (0)
301 
302 #endif /* __XEN_PUBLIC_IO_RING_H__ */
303 
304 /*
305  * Local variables:
306  * mode: C
307  * c-file-style: "BSD"
308  * c-basic-offset: 4
309  * tab-width: 4
310  * indent-tabs-mode: nil
311  * End:
312  */
313