xref: /freebsd/sys/contrib/xen/io/ring.h (revision 3a9fd8242b35884921dfc4e886f284a75870a536)
1*3a9fd824SRoger Pau Monné /******************************************************************************
2*3a9fd824SRoger Pau Monné  * ring.h
3*3a9fd824SRoger Pau Monné  *
4*3a9fd824SRoger Pau Monné  * Shared producer-consumer ring macros.
5*3a9fd824SRoger Pau Monné  *
6*3a9fd824SRoger Pau Monné  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*3a9fd824SRoger Pau Monné  * of this software and associated documentation files (the "Software"), to
8*3a9fd824SRoger Pau Monné  * deal in the Software without restriction, including without limitation the
9*3a9fd824SRoger Pau Monné  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*3a9fd824SRoger Pau Monné  * sell copies of the Software, and to permit persons to whom the Software is
11*3a9fd824SRoger Pau Monné  * furnished to do so, subject to the following conditions:
12*3a9fd824SRoger Pau Monné  *
13*3a9fd824SRoger Pau Monné  * The above copyright notice and this permission notice shall be included in
14*3a9fd824SRoger Pau Monné  * all copies or substantial portions of the Software.
15*3a9fd824SRoger Pau Monné  *
16*3a9fd824SRoger Pau Monné  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*3a9fd824SRoger Pau Monné  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*3a9fd824SRoger Pau Monné  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*3a9fd824SRoger Pau Monné  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*3a9fd824SRoger Pau Monné  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21*3a9fd824SRoger Pau Monné  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22*3a9fd824SRoger Pau Monné  * DEALINGS IN THE SOFTWARE.
23*3a9fd824SRoger Pau Monné  *
24*3a9fd824SRoger Pau Monné  * Tim Deegan and Andrew Warfield November 2004.
25*3a9fd824SRoger Pau Monné  */
26*3a9fd824SRoger Pau Monné 
27*3a9fd824SRoger Pau Monné #ifndef __XEN_PUBLIC_IO_RING_H__
28*3a9fd824SRoger Pau Monné #define __XEN_PUBLIC_IO_RING_H__
29*3a9fd824SRoger Pau Monné 
30*3a9fd824SRoger Pau Monné /*
31*3a9fd824SRoger Pau Monné  * When #include'ing this header, you need to provide the following
32*3a9fd824SRoger Pau Monné  * declaration upfront:
33*3a9fd824SRoger Pau Monné  * - standard integers types (uint8_t, uint16_t, etc)
34*3a9fd824SRoger Pau Monné  * They are provided by stdint.h of the standard headers.
35*3a9fd824SRoger Pau Monné  *
36*3a9fd824SRoger Pau Monné  * In addition, if you intend to use the FLEX macros, you also need to
37*3a9fd824SRoger Pau Monné  * provide the following, before invoking the FLEX macros:
38*3a9fd824SRoger Pau Monné  * - size_t
39*3a9fd824SRoger Pau Monné  * - memcpy
40*3a9fd824SRoger Pau Monné  * - grant_ref_t
41*3a9fd824SRoger Pau Monné  * These declarations are provided by string.h of the standard headers,
42*3a9fd824SRoger Pau Monné  * and grant_table.h from the Xen public headers.
43*3a9fd824SRoger Pau Monné  */
44*3a9fd824SRoger Pau Monné 
45*3a9fd824SRoger Pau Monné #include "../xen-compat.h"
46*3a9fd824SRoger Pau Monné 
47*3a9fd824SRoger Pau Monné #if __XEN_INTERFACE_VERSION__ < 0x00030208
48*3a9fd824SRoger Pau Monné #define xen_mb()  mb()
49*3a9fd824SRoger Pau Monné #define xen_rmb() rmb()
50*3a9fd824SRoger Pau Monné #define xen_wmb() wmb()
51*3a9fd824SRoger Pau Monné #endif
52*3a9fd824SRoger Pau Monné 
53*3a9fd824SRoger Pau Monné typedef unsigned int RING_IDX;
54*3a9fd824SRoger Pau Monné 
55*3a9fd824SRoger Pau Monné /* Round a 32-bit unsigned constant down to the nearest power of two. */
56*3a9fd824SRoger Pau Monné #define __RD2(_x)  (((_x) & 0x00000002) ? 0x2                  : ((_x) & 0x1))
57*3a9fd824SRoger Pau Monné #define __RD4(_x)  (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2    : __RD2(_x))
58*3a9fd824SRoger Pau Monné #define __RD8(_x)  (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4    : __RD4(_x))
59*3a9fd824SRoger Pau Monné #define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8    : __RD8(_x))
60*3a9fd824SRoger Pau Monné #define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
61*3a9fd824SRoger Pau Monné 
62*3a9fd824SRoger Pau Monné /*
63*3a9fd824SRoger Pau Monné  * Calculate size of a shared ring, given the total available space for the
64*3a9fd824SRoger Pau Monné  * ring and indexes (_sz), and the name tag of the request/response structure.
65*3a9fd824SRoger Pau Monné  * A ring contains as many entries as will fit, rounded down to the nearest
66*3a9fd824SRoger Pau Monné  * power of two (so we can mask with (size-1) to loop around).
67*3a9fd824SRoger Pau Monné  */
68*3a9fd824SRoger Pau Monné #define __CONST_RING_SIZE(_s, _sz) \
69*3a9fd824SRoger Pau Monné     (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \
70*3a9fd824SRoger Pau Monné 	    sizeof(((struct _s##_sring *)0)->ring[0])))
71*3a9fd824SRoger Pau Monné /*
72*3a9fd824SRoger Pau Monné  * The same for passing in an actual pointer instead of a name tag.
73*3a9fd824SRoger Pau Monné  */
74*3a9fd824SRoger Pau Monné #define __RING_SIZE(_s, _sz) \
75*3a9fd824SRoger Pau Monné     (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
76*3a9fd824SRoger Pau Monné 
77*3a9fd824SRoger Pau Monné /*
78*3a9fd824SRoger Pau Monné  * Macros to make the correct C datatypes for a new kind of ring.
79*3a9fd824SRoger Pau Monné  *
80*3a9fd824SRoger Pau Monné  * To make a new ring datatype, you need to have two message structures,
81*3a9fd824SRoger Pau Monné  * let's say request_t, and response_t already defined.
82*3a9fd824SRoger Pau Monné  *
83*3a9fd824SRoger Pau Monné  * In a header where you want the ring datatype declared, you then do:
84*3a9fd824SRoger Pau Monné  *
85*3a9fd824SRoger Pau Monné  *     DEFINE_RING_TYPES(mytag, request_t, response_t);
86*3a9fd824SRoger Pau Monné  *
87*3a9fd824SRoger Pau Monné  * These expand out to give you a set of types, as you can see below.
88*3a9fd824SRoger Pau Monné  * The most important of these are:
89*3a9fd824SRoger Pau Monné  *
90*3a9fd824SRoger Pau Monné  *     mytag_sring_t      - The shared ring.
91*3a9fd824SRoger Pau Monné  *     mytag_front_ring_t - The 'front' half of the ring.
92*3a9fd824SRoger Pau Monné  *     mytag_back_ring_t  - The 'back' half of the ring.
93*3a9fd824SRoger Pau Monné  *
94*3a9fd824SRoger Pau Monné  * To initialize a ring in your code you need to know the location and size
95*3a9fd824SRoger Pau Monné  * of the shared memory area (PAGE_SIZE, for instance). To initialise
96*3a9fd824SRoger Pau Monné  * the front half:
97*3a9fd824SRoger Pau Monné  *
98*3a9fd824SRoger Pau Monné  *     mytag_front_ring_t front_ring;
99*3a9fd824SRoger Pau Monné  *     SHARED_RING_INIT((mytag_sring_t *)shared_page);
100*3a9fd824SRoger Pau Monné  *     FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
101*3a9fd824SRoger Pau Monné  *
102*3a9fd824SRoger Pau Monné  * Initializing the back follows similarly (note that only the front
103*3a9fd824SRoger Pau Monné  * initializes the shared ring):
104*3a9fd824SRoger Pau Monné  *
105*3a9fd824SRoger Pau Monné  *     mytag_back_ring_t back_ring;
106*3a9fd824SRoger Pau Monné  *     BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
107*3a9fd824SRoger Pau Monné  */
108*3a9fd824SRoger Pau Monné 
109*3a9fd824SRoger Pau Monné #define DEFINE_RING_TYPES(__name, __req_t, __rsp_t)                     \
110*3a9fd824SRoger Pau Monné                                                                         \
111*3a9fd824SRoger Pau Monné /* Shared ring entry */                                                 \
112*3a9fd824SRoger Pau Monné union __name##_sring_entry {                                            \
113*3a9fd824SRoger Pau Monné     __req_t req;                                                        \
114*3a9fd824SRoger Pau Monné     __rsp_t rsp;                                                        \
115*3a9fd824SRoger Pau Monné };                                                                      \
116*3a9fd824SRoger Pau Monné                                                                         \
117*3a9fd824SRoger Pau Monné /* Shared ring page */                                                  \
118*3a9fd824SRoger Pau Monné struct __name##_sring {                                                 \
119*3a9fd824SRoger Pau Monné     RING_IDX req_prod, req_event;                                       \
120*3a9fd824SRoger Pau Monné     RING_IDX rsp_prod, rsp_event;                                       \
121*3a9fd824SRoger Pau Monné     union {                                                             \
122*3a9fd824SRoger Pau Monné         struct {                                                        \
123*3a9fd824SRoger Pau Monné             uint8_t smartpoll_active;                                   \
124*3a9fd824SRoger Pau Monné         } netif;                                                        \
125*3a9fd824SRoger Pau Monné         struct {                                                        \
126*3a9fd824SRoger Pau Monné             uint8_t msg;                                                \
127*3a9fd824SRoger Pau Monné         } tapif_user;                                                   \
128*3a9fd824SRoger Pau Monné         uint8_t pvt_pad[4];                                             \
129*3a9fd824SRoger Pau Monné     } pvt;                                                              \
130*3a9fd824SRoger Pau Monné     uint8_t __pad[44];                                                  \
131*3a9fd824SRoger Pau Monné     union __name##_sring_entry ring[1]; /* variable-length */           \
132*3a9fd824SRoger Pau Monné };                                                                      \
133*3a9fd824SRoger Pau Monné                                                                         \
134*3a9fd824SRoger Pau Monné /* "Front" end's private variables */                                   \
135*3a9fd824SRoger Pau Monné struct __name##_front_ring {                                            \
136*3a9fd824SRoger Pau Monné     RING_IDX req_prod_pvt;                                              \
137*3a9fd824SRoger Pau Monné     RING_IDX rsp_cons;                                                  \
138*3a9fd824SRoger Pau Monné     unsigned int nr_ents;                                               \
139*3a9fd824SRoger Pau Monné     struct __name##_sring *sring;                                       \
140*3a9fd824SRoger Pau Monné };                                                                      \
141*3a9fd824SRoger Pau Monné                                                                         \
142*3a9fd824SRoger Pau Monné /* "Back" end's private variables */                                    \
143*3a9fd824SRoger Pau Monné struct __name##_back_ring {                                             \
144*3a9fd824SRoger Pau Monné     RING_IDX rsp_prod_pvt;                                              \
145*3a9fd824SRoger Pau Monné     RING_IDX req_cons;                                                  \
146*3a9fd824SRoger Pau Monné     unsigned int nr_ents;                                               \
147*3a9fd824SRoger Pau Monné     struct __name##_sring *sring;                                       \
148*3a9fd824SRoger Pau Monné };                                                                      \
149*3a9fd824SRoger Pau Monné                                                                         \
150*3a9fd824SRoger Pau Monné /* Syntactic sugar */                                                   \
151*3a9fd824SRoger Pau Monné typedef struct __name##_sring __name##_sring_t;                         \
152*3a9fd824SRoger Pau Monné typedef struct __name##_front_ring __name##_front_ring_t;               \
153*3a9fd824SRoger Pau Monné typedef struct __name##_back_ring __name##_back_ring_t
154*3a9fd824SRoger Pau Monné 
155*3a9fd824SRoger Pau Monné /*
156*3a9fd824SRoger Pau Monné  * Macros for manipulating rings.
157*3a9fd824SRoger Pau Monné  *
158*3a9fd824SRoger Pau Monné  * FRONT_RING_whatever works on the "front end" of a ring: here
159*3a9fd824SRoger Pau Monné  * requests are pushed on to the ring and responses taken off it.
160*3a9fd824SRoger Pau Monné  *
161*3a9fd824SRoger Pau Monné  * BACK_RING_whatever works on the "back end" of a ring: here
162*3a9fd824SRoger Pau Monné  * requests are taken off the ring and responses put on.
163*3a9fd824SRoger Pau Monné  *
164*3a9fd824SRoger Pau Monné  * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
165*3a9fd824SRoger Pau Monné  * This is OK in 1-for-1 request-response situations where the
166*3a9fd824SRoger Pau Monné  * requestor (front end) never has more than RING_SIZE()-1
167*3a9fd824SRoger Pau Monné  * outstanding requests.
168*3a9fd824SRoger Pau Monné  */
169*3a9fd824SRoger Pau Monné 
170*3a9fd824SRoger Pau Monné /* Initialising empty rings */
171*3a9fd824SRoger Pau Monné #define SHARED_RING_INIT(_s) do {                                       \
172*3a9fd824SRoger Pau Monné     (_s)->req_prod  = (_s)->rsp_prod  = 0;                              \
173*3a9fd824SRoger Pau Monné     (_s)->req_event = (_s)->rsp_event = 1;                              \
174*3a9fd824SRoger Pau Monné     (void)memset((_s)->pvt.pvt_pad, 0, sizeof((_s)->pvt.pvt_pad));      \
175*3a9fd824SRoger Pau Monné     (void)memset((_s)->__pad, 0, sizeof((_s)->__pad));                  \
176*3a9fd824SRoger Pau Monné } while(0)
177*3a9fd824SRoger Pau Monné 
178*3a9fd824SRoger Pau Monné #define FRONT_RING_ATTACH(_r, _s, _i, __size) do {                      \
179*3a9fd824SRoger Pau Monné     (_r)->req_prod_pvt = (_i);                                          \
180*3a9fd824SRoger Pau Monné     (_r)->rsp_cons = (_i);                                              \
181*3a9fd824SRoger Pau Monné     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
182*3a9fd824SRoger Pau Monné     (_r)->sring = (_s);                                                 \
183*3a9fd824SRoger Pau Monné } while (0)
184*3a9fd824SRoger Pau Monné 
185*3a9fd824SRoger Pau Monné #define FRONT_RING_INIT(_r, _s, __size) FRONT_RING_ATTACH(_r, _s, 0, __size)
186*3a9fd824SRoger Pau Monné 
187*3a9fd824SRoger Pau Monné #define BACK_RING_ATTACH(_r, _s, _i, __size) do {                       \
188*3a9fd824SRoger Pau Monné     (_r)->rsp_prod_pvt = (_i);                                          \
189*3a9fd824SRoger Pau Monné     (_r)->req_cons = (_i);                                              \
190*3a9fd824SRoger Pau Monné     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
191*3a9fd824SRoger Pau Monné     (_r)->sring = (_s);                                                 \
192*3a9fd824SRoger Pau Monné } while (0)
193*3a9fd824SRoger Pau Monné 
194*3a9fd824SRoger Pau Monné #define BACK_RING_INIT(_r, _s, __size) BACK_RING_ATTACH(_r, _s, 0, __size)
195*3a9fd824SRoger Pau Monné 
196*3a9fd824SRoger Pau Monné /* How big is this ring? */
197*3a9fd824SRoger Pau Monné #define RING_SIZE(_r)                                                   \
198*3a9fd824SRoger Pau Monné     ((_r)->nr_ents)
199*3a9fd824SRoger Pau Monné 
200*3a9fd824SRoger Pau Monné /* Number of free requests (for use on front side only). */
201*3a9fd824SRoger Pau Monné #define RING_FREE_REQUESTS(_r)                                          \
202*3a9fd824SRoger Pau Monné     (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
203*3a9fd824SRoger Pau Monné 
204*3a9fd824SRoger Pau Monné /* Test if there is an empty slot available on the front ring.
205*3a9fd824SRoger Pau Monné  * (This is only meaningful from the front. )
206*3a9fd824SRoger Pau Monné  */
207*3a9fd824SRoger Pau Monné #define RING_FULL(_r)                                                   \
208*3a9fd824SRoger Pau Monné     (RING_FREE_REQUESTS(_r) == 0)
209*3a9fd824SRoger Pau Monné 
210*3a9fd824SRoger Pau Monné /* Test if there are outstanding messages to be processed on a ring. */
211*3a9fd824SRoger Pau Monné #define RING_HAS_UNCONSUMED_RESPONSES(_r)                               \
212*3a9fd824SRoger Pau Monné     ((_r)->sring->rsp_prod - (_r)->rsp_cons)
213*3a9fd824SRoger Pau Monné 
214*3a9fd824SRoger Pau Monné #ifdef __GNUC__
215*3a9fd824SRoger Pau Monné #define RING_HAS_UNCONSUMED_REQUESTS(_r) ({                             \
216*3a9fd824SRoger Pau Monné     unsigned int req = (_r)->sring->req_prod - (_r)->req_cons;          \
217*3a9fd824SRoger Pau Monné     unsigned int rsp = RING_SIZE(_r) -                                  \
218*3a9fd824SRoger Pau Monné         ((_r)->req_cons - (_r)->rsp_prod_pvt);                          \
219*3a9fd824SRoger Pau Monné     req < rsp ? req : rsp;                                              \
220*3a9fd824SRoger Pau Monné })
221*3a9fd824SRoger Pau Monné #else
222*3a9fd824SRoger Pau Monné /* Same as above, but without the nice GCC ({ ... }) syntax. */
223*3a9fd824SRoger Pau Monné #define RING_HAS_UNCONSUMED_REQUESTS(_r)                                \
224*3a9fd824SRoger Pau Monné     ((((_r)->sring->req_prod - (_r)->req_cons) <                        \
225*3a9fd824SRoger Pau Monné       (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ?        \
226*3a9fd824SRoger Pau Monné      ((_r)->sring->req_prod - (_r)->req_cons) :                         \
227*3a9fd824SRoger Pau Monné      (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
228*3a9fd824SRoger Pau Monné #endif
229*3a9fd824SRoger Pau Monné 
230*3a9fd824SRoger Pau Monné /* Direct access to individual ring elements, by index. */
231*3a9fd824SRoger Pau Monné #define RING_GET_REQUEST(_r, _idx)                                      \
232*3a9fd824SRoger Pau Monné     (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
233*3a9fd824SRoger Pau Monné 
234*3a9fd824SRoger Pau Monné #define RING_GET_RESPONSE(_r, _idx)                                     \
235*3a9fd824SRoger Pau Monné     (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
236*3a9fd824SRoger Pau Monné 
237*3a9fd824SRoger Pau Monné /*
238*3a9fd824SRoger Pau Monné  * Get a local copy of a request/response.
239*3a9fd824SRoger Pau Monné  *
240*3a9fd824SRoger Pau Monné  * Use this in preference to RING_GET_{REQUEST,RESPONSE}() so all processing is
241*3a9fd824SRoger Pau Monné  * done on a local copy that cannot be modified by the other end.
242*3a9fd824SRoger Pau Monné  *
243*3a9fd824SRoger Pau Monné  * Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this
244*3a9fd824SRoger Pau Monné  * to be ineffective where dest is a struct which consists of only bitfields.
245*3a9fd824SRoger Pau Monné  */
246*3a9fd824SRoger Pau Monné #define RING_COPY_(type, r, idx, dest) do {				\
247*3a9fd824SRoger Pau Monné 	/* Use volatile to force the copy into dest. */			\
248*3a9fd824SRoger Pau Monné 	*(dest) = *(volatile __typeof__(dest))RING_GET_##type(r, idx);	\
249*3a9fd824SRoger Pau Monné } while (0)
250*3a9fd824SRoger Pau Monné 
251*3a9fd824SRoger Pau Monné #define RING_COPY_REQUEST(r, idx, req)  RING_COPY_(REQUEST, r, idx, req)
252*3a9fd824SRoger Pau Monné #define RING_COPY_RESPONSE(r, idx, rsp) RING_COPY_(RESPONSE, r, idx, rsp)
253*3a9fd824SRoger Pau Monné 
254*3a9fd824SRoger Pau Monné /* Loop termination condition: Would the specified index overflow the ring? */
255*3a9fd824SRoger Pau Monné #define RING_REQUEST_CONS_OVERFLOW(_r, _cons)                           \
256*3a9fd824SRoger Pau Monné     (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
257*3a9fd824SRoger Pau Monné 
258*3a9fd824SRoger Pau Monné /* Ill-behaved frontend determination: Can there be this many requests? */
259*3a9fd824SRoger Pau Monné #define RING_REQUEST_PROD_OVERFLOW(_r, _prod)                           \
260*3a9fd824SRoger Pau Monné     (((_prod) - (_r)->rsp_prod_pvt) > RING_SIZE(_r))
261*3a9fd824SRoger Pau Monné 
262*3a9fd824SRoger Pau Monné /* Ill-behaved backend determination: Can there be this many responses? */
263*3a9fd824SRoger Pau Monné #define RING_RESPONSE_PROD_OVERFLOW(_r, _prod)                          \
264*3a9fd824SRoger Pau Monné     (((_prod) - (_r)->rsp_cons) > RING_SIZE(_r))
265*3a9fd824SRoger Pau Monné 
266*3a9fd824SRoger Pau Monné #define RING_PUSH_REQUESTS(_r) do {                                     \
267*3a9fd824SRoger Pau Monné     xen_wmb(); /* back sees requests /before/ updated producer index */ \
268*3a9fd824SRoger Pau Monné     (_r)->sring->req_prod = (_r)->req_prod_pvt;                         \
269*3a9fd824SRoger Pau Monné } while (0)
270*3a9fd824SRoger Pau Monné 
271*3a9fd824SRoger Pau Monné #define RING_PUSH_RESPONSES(_r) do {                                    \
272*3a9fd824SRoger Pau Monné     xen_wmb(); /* front sees resps /before/ updated producer index */   \
273*3a9fd824SRoger Pau Monné     (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt;                         \
274*3a9fd824SRoger Pau Monné } while (0)
275*3a9fd824SRoger Pau Monné 
276*3a9fd824SRoger Pau Monné /*
277*3a9fd824SRoger Pau Monné  * Notification hold-off (req_event and rsp_event):
278*3a9fd824SRoger Pau Monné  *
279*3a9fd824SRoger Pau Monné  * When queueing requests or responses on a shared ring, it may not always be
280*3a9fd824SRoger Pau Monné  * necessary to notify the remote end. For example, if requests are in flight
281*3a9fd824SRoger Pau Monné  * in a backend, the front may be able to queue further requests without
282*3a9fd824SRoger Pau Monné  * notifying the back (if the back checks for new requests when it queues
283*3a9fd824SRoger Pau Monné  * responses).
284*3a9fd824SRoger Pau Monné  *
285*3a9fd824SRoger Pau Monné  * When enqueuing requests or responses:
286*3a9fd824SRoger Pau Monné  *
287*3a9fd824SRoger Pau Monné  *  Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
288*3a9fd824SRoger Pau Monné  *  is a boolean return value. True indicates that the receiver requires an
289*3a9fd824SRoger Pau Monné  *  asynchronous notification.
290*3a9fd824SRoger Pau Monné  *
291*3a9fd824SRoger Pau Monné  * After dequeuing requests or responses (before sleeping the connection):
292*3a9fd824SRoger Pau Monné  *
293*3a9fd824SRoger Pau Monné  *  Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
294*3a9fd824SRoger Pau Monné  *  The second argument is a boolean return value. True indicates that there
295*3a9fd824SRoger Pau Monné  *  are pending messages on the ring (i.e., the connection should not be put
296*3a9fd824SRoger Pau Monné  *  to sleep).
297*3a9fd824SRoger Pau Monné  *
298*3a9fd824SRoger Pau Monné  *  These macros will set the req_event/rsp_event field to trigger a
299*3a9fd824SRoger Pau Monné  *  notification on the very next message that is enqueued. If you want to
300*3a9fd824SRoger Pau Monné  *  create batches of work (i.e., only receive a notification after several
301*3a9fd824SRoger Pau Monné  *  messages have been enqueued) then you will need to create a customised
302*3a9fd824SRoger Pau Monné  *  version of the FINAL_CHECK macro in your own code, which sets the event
303*3a9fd824SRoger Pau Monné  *  field appropriately.
304*3a9fd824SRoger Pau Monné  */
305*3a9fd824SRoger Pau Monné 
306*3a9fd824SRoger Pau Monné #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do {           \
307*3a9fd824SRoger Pau Monné     RING_IDX __old = (_r)->sring->req_prod;                             \
308*3a9fd824SRoger Pau Monné     RING_IDX __new = (_r)->req_prod_pvt;                                \
309*3a9fd824SRoger Pau Monné     xen_wmb(); /* back sees requests /before/ updated producer index */ \
310*3a9fd824SRoger Pau Monné     (_r)->sring->req_prod = __new;                                      \
311*3a9fd824SRoger Pau Monné     xen_mb(); /* back sees new requests /before/ we check req_event */  \
312*3a9fd824SRoger Pau Monné     (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) <           \
313*3a9fd824SRoger Pau Monné                  (RING_IDX)(__new - __old));                            \
314*3a9fd824SRoger Pau Monné } while (0)
315*3a9fd824SRoger Pau Monné 
316*3a9fd824SRoger Pau Monné #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do {          \
317*3a9fd824SRoger Pau Monné     RING_IDX __old = (_r)->sring->rsp_prod;                             \
318*3a9fd824SRoger Pau Monné     RING_IDX __new = (_r)->rsp_prod_pvt;                                \
319*3a9fd824SRoger Pau Monné     xen_wmb(); /* front sees resps /before/ updated producer index */   \
320*3a9fd824SRoger Pau Monné     (_r)->sring->rsp_prod = __new;                                      \
321*3a9fd824SRoger Pau Monné     xen_mb(); /* front sees new resps /before/ we check rsp_event */    \
322*3a9fd824SRoger Pau Monné     (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) <           \
323*3a9fd824SRoger Pau Monné                  (RING_IDX)(__new - __old));                            \
324*3a9fd824SRoger Pau Monné } while (0)
325*3a9fd824SRoger Pau Monné 
326*3a9fd824SRoger Pau Monné #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do {             \
327*3a9fd824SRoger Pau Monné     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
328*3a9fd824SRoger Pau Monné     if (_work_to_do) break;                                             \
329*3a9fd824SRoger Pau Monné     (_r)->sring->req_event = (_r)->req_cons + 1;                        \
330*3a9fd824SRoger Pau Monné     xen_mb();                                                           \
331*3a9fd824SRoger Pau Monné     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
332*3a9fd824SRoger Pau Monné } while (0)
333*3a9fd824SRoger Pau Monné 
334*3a9fd824SRoger Pau Monné #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do {            \
335*3a9fd824SRoger Pau Monné     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
336*3a9fd824SRoger Pau Monné     if (_work_to_do) break;                                             \
337*3a9fd824SRoger Pau Monné     (_r)->sring->rsp_event = (_r)->rsp_cons + 1;                        \
338*3a9fd824SRoger Pau Monné     xen_mb();                                                           \
339*3a9fd824SRoger Pau Monné     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
340*3a9fd824SRoger Pau Monné } while (0)
341*3a9fd824SRoger Pau Monné 
342*3a9fd824SRoger Pau Monné 
343*3a9fd824SRoger Pau Monné /*
344*3a9fd824SRoger Pau Monné  * DEFINE_XEN_FLEX_RING_AND_INTF defines two monodirectional rings and
345*3a9fd824SRoger Pau Monné  * functions to check if there is data on the ring, and to read and
346*3a9fd824SRoger Pau Monné  * write to them.
347*3a9fd824SRoger Pau Monné  *
348*3a9fd824SRoger Pau Monné  * DEFINE_XEN_FLEX_RING is similar to DEFINE_XEN_FLEX_RING_AND_INTF, but
349*3a9fd824SRoger Pau Monné  * does not define the indexes page. As different protocols can have
350*3a9fd824SRoger Pau Monné  * extensions to the basic format, this macro allow them to define their
351*3a9fd824SRoger Pau Monné  * own struct.
352*3a9fd824SRoger Pau Monné  *
353*3a9fd824SRoger Pau Monné  * XEN_FLEX_RING_SIZE
354*3a9fd824SRoger Pau Monné  *   Convenience macro to calculate the size of one of the two rings
355*3a9fd824SRoger Pau Monné  *   from the overall order.
356*3a9fd824SRoger Pau Monné  *
357*3a9fd824SRoger Pau Monné  * $NAME_mask
358*3a9fd824SRoger Pau Monné  *   Function to apply the size mask to an index, to reduce the index
359*3a9fd824SRoger Pau Monné  *   within the range [0-size].
360*3a9fd824SRoger Pau Monné  *
361*3a9fd824SRoger Pau Monné  * $NAME_read_packet
362*3a9fd824SRoger Pau Monné  *   Function to read data from the ring. The amount of data to read is
363*3a9fd824SRoger Pau Monné  *   specified by the "size" argument.
364*3a9fd824SRoger Pau Monné  *
365*3a9fd824SRoger Pau Monné  * $NAME_write_packet
366*3a9fd824SRoger Pau Monné  *   Function to write data to the ring. The amount of data to write is
367*3a9fd824SRoger Pau Monné  *   specified by the "size" argument.
368*3a9fd824SRoger Pau Monné  *
369*3a9fd824SRoger Pau Monné  * $NAME_get_ring_ptr
370*3a9fd824SRoger Pau Monné  *   Convenience function that returns a pointer to read/write to the
371*3a9fd824SRoger Pau Monné  *   ring at the right location.
372*3a9fd824SRoger Pau Monné  *
373*3a9fd824SRoger Pau Monné  * $NAME_data_intf
374*3a9fd824SRoger Pau Monné  *   Indexes page, shared between frontend and backend. It also
375*3a9fd824SRoger Pau Monné  *   contains the array of grant refs.
376*3a9fd824SRoger Pau Monné  *
377*3a9fd824SRoger Pau Monné  * $NAME_queued
378*3a9fd824SRoger Pau Monné  *   Function to calculate how many bytes are currently on the ring,
379*3a9fd824SRoger Pau Monné  *   ready to be read. It can also be used to calculate how much free
380*3a9fd824SRoger Pau Monné  *   space is currently on the ring (XEN_FLEX_RING_SIZE() -
381*3a9fd824SRoger Pau Monné  *   $NAME_queued()).
382*3a9fd824SRoger Pau Monné  */
383*3a9fd824SRoger Pau Monné 
384*3a9fd824SRoger Pau Monné #ifndef XEN_PAGE_SHIFT
385*3a9fd824SRoger Pau Monné /* The PAGE_SIZE for ring protocols and hypercall interfaces is always
386*3a9fd824SRoger Pau Monné  * 4K, regardless of the architecture, and page granularity chosen by
387*3a9fd824SRoger Pau Monné  * operating systems.
388*3a9fd824SRoger Pau Monné  */
389*3a9fd824SRoger Pau Monné #define XEN_PAGE_SHIFT 12
390*3a9fd824SRoger Pau Monné #endif
391*3a9fd824SRoger Pau Monné #define XEN_FLEX_RING_SIZE(order)                                             \
392*3a9fd824SRoger Pau Monné     (1UL << ((order) + XEN_PAGE_SHIFT - 1))
393*3a9fd824SRoger Pau Monné 
394*3a9fd824SRoger Pau Monné #define DEFINE_XEN_FLEX_RING(name)                                            \
395*3a9fd824SRoger Pau Monné static inline RING_IDX name##_mask(RING_IDX idx, RING_IDX ring_size)          \
396*3a9fd824SRoger Pau Monné {                                                                             \
397*3a9fd824SRoger Pau Monné     return idx & (ring_size - 1);                                             \
398*3a9fd824SRoger Pau Monné }                                                                             \
399*3a9fd824SRoger Pau Monné                                                                               \
400*3a9fd824SRoger Pau Monné static inline unsigned char *name##_get_ring_ptr(unsigned char *buf,          \
401*3a9fd824SRoger Pau Monné                                                  RING_IDX idx,                \
402*3a9fd824SRoger Pau Monné                                                  RING_IDX ring_size)          \
403*3a9fd824SRoger Pau Monné {                                                                             \
404*3a9fd824SRoger Pau Monné     return buf + name##_mask(idx, ring_size);                                 \
405*3a9fd824SRoger Pau Monné }                                                                             \
406*3a9fd824SRoger Pau Monné                                                                               \
407*3a9fd824SRoger Pau Monné static inline void name##_read_packet(void *opaque,                           \
408*3a9fd824SRoger Pau Monné                                       const unsigned char *buf,               \
409*3a9fd824SRoger Pau Monné                                       size_t size,                            \
410*3a9fd824SRoger Pau Monné                                       RING_IDX masked_prod,                   \
411*3a9fd824SRoger Pau Monné                                       RING_IDX *masked_cons,                  \
412*3a9fd824SRoger Pau Monné                                       RING_IDX ring_size)                     \
413*3a9fd824SRoger Pau Monné {                                                                             \
414*3a9fd824SRoger Pau Monné     if (*masked_cons < masked_prod ||                                         \
415*3a9fd824SRoger Pau Monné         size <= ring_size - *masked_cons) {                                   \
416*3a9fd824SRoger Pau Monné         memcpy(opaque, buf + *masked_cons, size);                             \
417*3a9fd824SRoger Pau Monné     } else {                                                                  \
418*3a9fd824SRoger Pau Monné         memcpy(opaque, buf + *masked_cons, ring_size - *masked_cons);         \
419*3a9fd824SRoger Pau Monné         memcpy((unsigned char *)opaque + ring_size - *masked_cons, buf,       \
420*3a9fd824SRoger Pau Monné                size - (ring_size - *masked_cons));                            \
421*3a9fd824SRoger Pau Monné     }                                                                         \
422*3a9fd824SRoger Pau Monné     *masked_cons = name##_mask(*masked_cons + size, ring_size);               \
423*3a9fd824SRoger Pau Monné }                                                                             \
424*3a9fd824SRoger Pau Monné                                                                               \
425*3a9fd824SRoger Pau Monné static inline void name##_write_packet(unsigned char *buf,                    \
426*3a9fd824SRoger Pau Monné                                        const void *opaque,                    \
427*3a9fd824SRoger Pau Monné                                        size_t size,                           \
428*3a9fd824SRoger Pau Monné                                        RING_IDX *masked_prod,                 \
429*3a9fd824SRoger Pau Monné                                        RING_IDX masked_cons,                  \
430*3a9fd824SRoger Pau Monné                                        RING_IDX ring_size)                    \
431*3a9fd824SRoger Pau Monné {                                                                             \
432*3a9fd824SRoger Pau Monné     if (*masked_prod < masked_cons ||                                         \
433*3a9fd824SRoger Pau Monné         size <= ring_size - *masked_prod) {                                   \
434*3a9fd824SRoger Pau Monné         memcpy(buf + *masked_prod, opaque, size);                             \
435*3a9fd824SRoger Pau Monné     } else {                                                                  \
436*3a9fd824SRoger Pau Monné         memcpy(buf + *masked_prod, opaque, ring_size - *masked_prod);         \
437*3a9fd824SRoger Pau Monné         memcpy(buf, (unsigned char *)opaque + (ring_size - *masked_prod),     \
438*3a9fd824SRoger Pau Monné                size - (ring_size - *masked_prod));                            \
439*3a9fd824SRoger Pau Monné     }                                                                         \
440*3a9fd824SRoger Pau Monné     *masked_prod = name##_mask(*masked_prod + size, ring_size);               \
441*3a9fd824SRoger Pau Monné }                                                                             \
442*3a9fd824SRoger Pau Monné                                                                               \
443*3a9fd824SRoger Pau Monné static inline RING_IDX name##_queued(RING_IDX prod,                           \
444*3a9fd824SRoger Pau Monné                                      RING_IDX cons,                           \
445*3a9fd824SRoger Pau Monné                                      RING_IDX ring_size)                      \
446*3a9fd824SRoger Pau Monné {                                                                             \
447*3a9fd824SRoger Pau Monné     RING_IDX size;                                                            \
448*3a9fd824SRoger Pau Monné                                                                               \
449*3a9fd824SRoger Pau Monné     if (prod == cons)                                                         \
450*3a9fd824SRoger Pau Monné         return 0;                                                             \
451*3a9fd824SRoger Pau Monné                                                                               \
452*3a9fd824SRoger Pau Monné     prod = name##_mask(prod, ring_size);                                      \
453*3a9fd824SRoger Pau Monné     cons = name##_mask(cons, ring_size);                                      \
454*3a9fd824SRoger Pau Monné                                                                               \
455*3a9fd824SRoger Pau Monné     if (prod == cons)                                                         \
456*3a9fd824SRoger Pau Monné         return ring_size;                                                     \
457*3a9fd824SRoger Pau Monné                                                                               \
458*3a9fd824SRoger Pau Monné     if (prod > cons)                                                          \
459*3a9fd824SRoger Pau Monné         size = prod - cons;                                                   \
460*3a9fd824SRoger Pau Monné     else                                                                      \
461*3a9fd824SRoger Pau Monné         size = ring_size - (cons - prod);                                     \
462*3a9fd824SRoger Pau Monné     return size;                                                              \
463*3a9fd824SRoger Pau Monné }                                                                             \
464*3a9fd824SRoger Pau Monné                                                                               \
465*3a9fd824SRoger Pau Monné struct name##_data {                                                          \
466*3a9fd824SRoger Pau Monné     unsigned char *in; /* half of the allocation */                           \
467*3a9fd824SRoger Pau Monné     unsigned char *out; /* half of the allocation */                          \
468*3a9fd824SRoger Pau Monné }
469*3a9fd824SRoger Pau Monné 
470*3a9fd824SRoger Pau Monné #define DEFINE_XEN_FLEX_RING_AND_INTF(name)                                   \
471*3a9fd824SRoger Pau Monné struct name##_data_intf {                                                     \
472*3a9fd824SRoger Pau Monné     RING_IDX in_cons, in_prod;                                                \
473*3a9fd824SRoger Pau Monné                                                                               \
474*3a9fd824SRoger Pau Monné     uint8_t pad1[56];                                                         \
475*3a9fd824SRoger Pau Monné                                                                               \
476*3a9fd824SRoger Pau Monné     RING_IDX out_cons, out_prod;                                              \
477*3a9fd824SRoger Pau Monné                                                                               \
478*3a9fd824SRoger Pau Monné     uint8_t pad2[56];                                                         \
479*3a9fd824SRoger Pau Monné                                                                               \
480*3a9fd824SRoger Pau Monné     RING_IDX ring_order;                                                      \
481*3a9fd824SRoger Pau Monné     grant_ref_t ref[];                                                        \
482*3a9fd824SRoger Pau Monné };                                                                            \
483*3a9fd824SRoger Pau Monné DEFINE_XEN_FLEX_RING(name)
484*3a9fd824SRoger Pau Monné 
485*3a9fd824SRoger Pau Monné #endif /* __XEN_PUBLIC_IO_RING_H__ */
486*3a9fd824SRoger Pau Monné 
487*3a9fd824SRoger Pau Monné /*
488*3a9fd824SRoger Pau Monné  * Local variables:
489*3a9fd824SRoger Pau Monné  * mode: C
490*3a9fd824SRoger Pau Monné  * c-file-style: "BSD"
491*3a9fd824SRoger Pau Monné  * c-basic-offset: 4
492*3a9fd824SRoger Pau Monné  * tab-width: 4
493*3a9fd824SRoger Pau Monné  * indent-tabs-mode: nil
494*3a9fd824SRoger Pau Monné  * End:
495*3a9fd824SRoger Pau Monné  */
496