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