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