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