/******************************************************************************1* ring.h2*3* Shared producer-consumer ring macros.4*5* Tim Deegan and Andrew Warfield November 2004.6*/78#ifndef __XEN_PUBLIC_IO_RING_H__9#define __XEN_PUBLIC_IO_RING_H__1011typedef unsigned int RING_IDX;1213/* Round a 32-bit unsigned constant down to the nearest power of two. */14#define __RD2(_x) (((_x) & 0x00000002) ? 0x2 : ((_x) & 0x1))15#define __RD4(_x) (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2 : __RD2(_x))16#define __RD8(_x) (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4 : __RD4(_x))17#define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8 : __RD8(_x))18#define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))1920/*21* Calculate size of a shared ring, given the total available space for the22* ring and indexes (_sz), and the name tag of the request/response structure.23* A ring contains as many entries as will fit, rounded down to the nearest24* power of two (so we can mask with (size-1) to loop around).25*/26#define __CONST_RING_SIZE(_s, _sz) \27(__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \28sizeof(((struct _s##_sring *)0)->ring[0])))2930/*31* The same for passing in an actual pointer instead of a name tag.32*/33#define __RING_SIZE(_s, _sz) \34(__RD32(((_sz) - (long)&(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))3536/*37* Macros to make the correct C datatypes for a new kind of ring.38*39* To make a new ring datatype, you need to have two message structures,40* let's say struct request, and struct response already defined.41*42* In a header where you want the ring datatype declared, you then do:43*44* DEFINE_RING_TYPES(mytag, struct request, struct response);45*46* These expand out to give you a set of types, as you can see below.47* The most important of these are:48*49* struct mytag_sring - The shared ring.50* struct mytag_front_ring - The 'front' half of the ring.51* struct mytag_back_ring - The 'back' half of the ring.52*53* To initialize a ring in your code you need to know the location and size54* of the shared memory area (PAGE_SIZE, for instance). To initialise55* the front half:56*57* struct mytag_front_ring front_ring;58* SHARED_RING_INIT((struct mytag_sring *)shared_page);59* FRONT_RING_INIT(&front_ring, (struct mytag_sring *)shared_page,60* PAGE_SIZE);61*62* Initializing the back follows similarly (note that only the front63* initializes the shared ring):64*65* struct mytag_back_ring back_ring;66* BACK_RING_INIT(&back_ring, (struct mytag_sring *)shared_page,67* PAGE_SIZE);68*/6970#define DEFINE_RING_TYPES(__name, __req_t, __rsp_t) \71\72/* Shared ring entry */ \73union __name##_sring_entry { \74__req_t req; \75__rsp_t rsp; \76}; \77\78/* Shared ring page */ \79struct __name##_sring { \80RING_IDX req_prod, req_event; \81RING_IDX rsp_prod, rsp_event; \82uint8_t pad[48]; \83union __name##_sring_entry ring[1]; /* variable-length */ \84}; \85\86/* "Front" end's private variables */ \87struct __name##_front_ring { \88RING_IDX req_prod_pvt; \89RING_IDX rsp_cons; \90unsigned int nr_ents; \91struct __name##_sring *sring; \92}; \93\94/* "Back" end's private variables */ \95struct __name##_back_ring { \96RING_IDX rsp_prod_pvt; \97RING_IDX req_cons; \98unsigned int nr_ents; \99struct __name##_sring *sring; \100};101102/*103* Macros for manipulating rings.104*105* FRONT_RING_whatever works on the "front end" of a ring: here106* requests are pushed on to the ring and responses taken off it.107*108* BACK_RING_whatever works on the "back end" of a ring: here109* requests are taken off the ring and responses put on.110*111* N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.112* This is OK in 1-for-1 request-response situations where the113* requestor (front end) never has more than RING_SIZE()-1114* outstanding requests.115*/116117/* Initialising empty rings */118#define SHARED_RING_INIT(_s) do { \119(_s)->req_prod = (_s)->rsp_prod = 0; \120(_s)->req_event = (_s)->rsp_event = 1; \121memset((_s)->pad, 0, sizeof((_s)->pad)); \122} while(0)123124#define FRONT_RING_INIT(_r, _s, __size) do { \125(_r)->req_prod_pvt = 0; \126(_r)->rsp_cons = 0; \127(_r)->nr_ents = __RING_SIZE(_s, __size); \128(_r)->sring = (_s); \129} while (0)130131#define BACK_RING_INIT(_r, _s, __size) do { \132(_r)->rsp_prod_pvt = 0; \133(_r)->req_cons = 0; \134(_r)->nr_ents = __RING_SIZE(_s, __size); \135(_r)->sring = (_s); \136} while (0)137138/* Initialize to existing shared indexes -- for recovery */139#define FRONT_RING_ATTACH(_r, _s, __size) do { \140(_r)->sring = (_s); \141(_r)->req_prod_pvt = (_s)->req_prod; \142(_r)->rsp_cons = (_s)->rsp_prod; \143(_r)->nr_ents = __RING_SIZE(_s, __size); \144} while (0)145146#define BACK_RING_ATTACH(_r, _s, __size) do { \147(_r)->sring = (_s); \148(_r)->rsp_prod_pvt = (_s)->rsp_prod; \149(_r)->req_cons = (_s)->req_prod; \150(_r)->nr_ents = __RING_SIZE(_s, __size); \151} while (0)152153/* How big is this ring? */154#define RING_SIZE(_r) \155((_r)->nr_ents)156157/* Number of free requests (for use on front side only). */158#define RING_FREE_REQUESTS(_r) \159(RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))160161/* Test if there is an empty slot available on the front ring.162* (This is only meaningful from the front. )163*/164#define RING_FULL(_r) \165(RING_FREE_REQUESTS(_r) == 0)166167/* Test if there are outstanding messages to be processed on a ring. */168#define RING_HAS_UNCONSUMED_RESPONSES(_r) \169((_r)->sring->rsp_prod - (_r)->rsp_cons)170171#define RING_HAS_UNCONSUMED_REQUESTS(_r) \172({ \173unsigned int req = (_r)->sring->req_prod - (_r)->req_cons; \174unsigned int rsp = RING_SIZE(_r) - \175((_r)->req_cons - (_r)->rsp_prod_pvt); \176req < rsp ? req : rsp; \177})178179/* Direct access to individual ring elements, by index. */180#define RING_GET_REQUEST(_r, _idx) \181(&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))182183#define RING_GET_RESPONSE(_r, _idx) \184(&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))185186/* Loop termination condition: Would the specified index overflow the ring? */187#define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \188(((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))189190#define RING_PUSH_REQUESTS(_r) do { \191wmb(); /* back sees requests /before/ updated producer index */ \192(_r)->sring->req_prod = (_r)->req_prod_pvt; \193} while (0)194195#define RING_PUSH_RESPONSES(_r) do { \196wmb(); /* front sees responses /before/ updated producer index */ \197(_r)->sring->rsp_prod = (_r)->rsp_prod_pvt; \198} while (0)199200/*201* Notification hold-off (req_event and rsp_event):202*203* When queueing requests or responses on a shared ring, it may not always be204* necessary to notify the remote end. For example, if requests are in flight205* in a backend, the front may be able to queue further requests without206* notifying the back (if the back checks for new requests when it queues207* responses).208*209* When enqueuing requests or responses:210*211* Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument212* is a boolean return value. True indicates that the receiver requires an213* asynchronous notification.214*215* After dequeuing requests or responses (before sleeping the connection):216*217* Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().218* The second argument is a boolean return value. True indicates that there219* are pending messages on the ring (i.e., the connection should not be put220* to sleep).221*222* These macros will set the req_event/rsp_event field to trigger a223* notification on the very next message that is enqueued. If you want to224* create batches of work (i.e., only receive a notification after several225* messages have been enqueued) then you will need to create a customised226* version of the FINAL_CHECK macro in your own code, which sets the event227* field appropriately.228*/229230#define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \231RING_IDX __old = (_r)->sring->req_prod; \232RING_IDX __new = (_r)->req_prod_pvt; \233wmb(); /* back sees requests /before/ updated producer index */ \234(_r)->sring->req_prod = __new; \235mb(); /* back sees new requests /before/ we check req_event */ \236(_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) < \237(RING_IDX)(__new - __old)); \238} while (0)239240#define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \241RING_IDX __old = (_r)->sring->rsp_prod; \242RING_IDX __new = (_r)->rsp_prod_pvt; \243wmb(); /* front sees responses /before/ updated producer index */ \244(_r)->sring->rsp_prod = __new; \245mb(); /* front sees new responses /before/ we check rsp_event */ \246(_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) < \247(RING_IDX)(__new - __old)); \248} while (0)249250#define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \251(_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \252if (_work_to_do) break; \253(_r)->sring->req_event = (_r)->req_cons + 1; \254mb(); \255(_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \256} while (0)257258#define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \259(_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \260if (_work_to_do) break; \261(_r)->sring->rsp_event = (_r)->rsp_cons + 1; \262mb(); \263(_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \264} while (0)265266#endif /* __XEN_PUBLIC_IO_RING_H__ */267268269