/******************************************************************************1* ring.h2*3* Shared producer-consumer ring macros.4*5* Permission is hereby granted, free of charge, to any person obtaining a copy6* of this software and associated documentation files (the "Software"), to7* deal in the Software without restriction, including without limitation the8* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or9* sell copies of the Software, and to permit persons to whom the Software is10* furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included in13* all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21* DEALINGS IN THE SOFTWARE.22*23* Tim Deegan and Andrew Warfield November 2004.24*/2526#ifndef __XEN_PUBLIC_IO_RING_H__27#define __XEN_PUBLIC_IO_RING_H__2829/*30* When #include'ing this header, you need to provide the following31* declaration upfront:32* - standard integers types (uint8_t, uint16_t, etc)33* They are provided by stdint.h of the standard headers.34*35* In addition, if you intend to use the FLEX macros, you also need to36* provide the following, before invoking the FLEX macros:37* - size_t38* - memcpy39* - grant_ref_t40* These declarations are provided by string.h of the standard headers,41* and grant_table.h from the Xen public headers.42*/4344#include "../xen-compat.h"4546#if __XEN_INTERFACE_VERSION__ < 0x0003020847#define xen_mb() mb()48#define xen_rmb() rmb()49#define xen_wmb() wmb()50#endif5152typedef unsigned int RING_IDX;5354/* Round a 32-bit unsigned constant down to the nearest power of two. */55#define __RD2(_x) (((_x) & 0x00000002) ? 0x2 : ((_x) & 0x1))56#define __RD4(_x) (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2 : __RD2(_x))57#define __RD8(_x) (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4 : __RD4(_x))58#define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8 : __RD8(_x))59#define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))6061/*62* Calculate size of a shared ring, given the total available space for the63* ring and indexes (_sz), and the name tag of the request/response structure.64* A ring contains as many entries as will fit, rounded down to the nearest65* power of two (so we can mask with (size-1) to loop around).66*/67#define __CONST_RING_SIZE(_s, _sz) \68(__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \69sizeof(((struct _s##_sring *)0)->ring[0])))70/*71* The same for passing in an actual pointer instead of a name tag.72*/73#define __RING_SIZE(_s, _sz) \74(__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))7576/*77* Macros to make the correct C datatypes for a new kind of ring.78*79* To make a new ring datatype, you need to have two message structures,80* let's say request_t, and response_t already defined.81*82* In a header where you want the ring datatype declared, you then do:83*84* DEFINE_RING_TYPES(mytag, request_t, response_t);85*86* These expand out to give you a set of types, as you can see below.87* The most important of these are:88*89* mytag_sring_t - The shared ring.90* mytag_front_ring_t - The 'front' half of the ring.91* mytag_back_ring_t - The 'back' half of the ring.92*93* To initialize a ring in your code you need to know the location and size94* of the shared memory area (PAGE_SIZE, for instance). To initialise95* the front half:96*97* mytag_front_ring_t front_ring;98* SHARED_RING_INIT((mytag_sring_t *)shared_page);99* FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);100*101* Initializing the back follows similarly (note that only the front102* initializes the shared ring):103*104* mytag_back_ring_t back_ring;105* BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);106*/107108#define DEFINE_RING_TYPES(__name, __req_t, __rsp_t) \109\110/* Shared ring entry */ \111union __name##_sring_entry { \112__req_t req; \113__rsp_t rsp; \114}; \115\116/* Shared ring page */ \117struct __name##_sring { \118RING_IDX req_prod, req_event; \119RING_IDX rsp_prod, rsp_event; \120union { \121struct { \122uint8_t smartpoll_active; \123} netif; \124struct { \125uint8_t msg; \126} tapif_user; \127uint8_t pvt_pad[4]; \128} pvt; \129uint8_t __pad[44]; \130union __name##_sring_entry ring[1]; /* variable-length */ \131}; \132\133/* "Front" end's private variables */ \134struct __name##_front_ring { \135RING_IDX req_prod_pvt; \136RING_IDX rsp_cons; \137unsigned int nr_ents; \138struct __name##_sring *sring; \139}; \140\141/* "Back" end's private variables */ \142struct __name##_back_ring { \143RING_IDX rsp_prod_pvt; \144RING_IDX req_cons; \145unsigned int nr_ents; \146struct __name##_sring *sring; \147}; \148\149/* Syntactic sugar */ \150typedef struct __name##_sring __name##_sring_t; \151typedef struct __name##_front_ring __name##_front_ring_t; \152typedef struct __name##_back_ring __name##_back_ring_t153154/*155* Macros for manipulating rings.156*157* FRONT_RING_whatever works on the "front end" of a ring: here158* requests are pushed on to the ring and responses taken off it.159*160* BACK_RING_whatever works on the "back end" of a ring: here161* requests are taken off the ring and responses put on.162*163* N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.164* This is OK in 1-for-1 request-response situations where the165* requestor (front end) never has more than RING_SIZE()-1166* outstanding requests.167*/168169/* Initialising empty rings */170#define SHARED_RING_INIT(_s) do { \171(_s)->req_prod = (_s)->rsp_prod = 0; \172(_s)->req_event = (_s)->rsp_event = 1; \173(void)memset((_s)->pvt.pvt_pad, 0, sizeof((_s)->pvt.pvt_pad)); \174(void)memset((_s)->__pad, 0, sizeof((_s)->__pad)); \175} while(0)176177#define FRONT_RING_ATTACH(_r, _s, _i, __size) do { \178(_r)->req_prod_pvt = (_i); \179(_r)->rsp_cons = (_i); \180(_r)->nr_ents = __RING_SIZE(_s, __size); \181(_r)->sring = (_s); \182} while (0)183184#define FRONT_RING_INIT(_r, _s, __size) FRONT_RING_ATTACH(_r, _s, 0, __size)185186#define BACK_RING_ATTACH(_r, _s, _i, __size) do { \187(_r)->rsp_prod_pvt = (_i); \188(_r)->req_cons = (_i); \189(_r)->nr_ents = __RING_SIZE(_s, __size); \190(_r)->sring = (_s); \191} while (0)192193#define BACK_RING_INIT(_r, _s, __size) BACK_RING_ATTACH(_r, _s, 0, __size)194195/* How big is this ring? */196#define RING_SIZE(_r) \197((_r)->nr_ents)198199/* Number of free requests (for use on front side only). */200#define RING_FREE_REQUESTS(_r) \201(RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))202203/* Test if there is an empty slot available on the front ring.204* (This is only meaningful from the front. )205*/206#define RING_FULL(_r) \207(RING_FREE_REQUESTS(_r) == 0)208209/* Test if there are outstanding messages to be processed on a ring. */210#define RING_HAS_UNCONSUMED_RESPONSES(_r) \211((_r)->sring->rsp_prod - (_r)->rsp_cons)212213#ifdef __GNUC__214#define RING_HAS_UNCONSUMED_REQUESTS(_r) ({ \215unsigned int req = (_r)->sring->req_prod - (_r)->req_cons; \216unsigned int rsp = RING_SIZE(_r) - \217((_r)->req_cons - (_r)->rsp_prod_pvt); \218req < rsp ? req : rsp; \219})220#else221/* Same as above, but without the nice GCC ({ ... }) syntax. */222#define RING_HAS_UNCONSUMED_REQUESTS(_r) \223((((_r)->sring->req_prod - (_r)->req_cons) < \224(RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ? \225((_r)->sring->req_prod - (_r)->req_cons) : \226(RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))227#endif228229/* Direct access to individual ring elements, by index. */230#define RING_GET_REQUEST(_r, _idx) \231(&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))232233#define RING_GET_RESPONSE(_r, _idx) \234(&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))235236/*237* Get a local copy of a request/response.238*239* Use this in preference to RING_GET_{REQUEST,RESPONSE}() so all processing is240* done on a local copy that cannot be modified by the other end.241*242* Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this243* to be ineffective where dest is a struct which consists of only bitfields.244*/245#define RING_COPY_(type, r, idx, dest) do { \246/* Use volatile to force the copy into dest. */ \247*(dest) = *(volatile __typeof__(dest))RING_GET_##type(r, idx); \248} while (0)249250#define RING_COPY_REQUEST(r, idx, req) RING_COPY_(REQUEST, r, idx, req)251#define RING_COPY_RESPONSE(r, idx, rsp) RING_COPY_(RESPONSE, r, idx, rsp)252253/* Loop termination condition: Would the specified index overflow the ring? */254#define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \255(((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))256257/* Ill-behaved frontend determination: Can there be this many requests? */258#define RING_REQUEST_PROD_OVERFLOW(_r, _prod) \259(((_prod) - (_r)->rsp_prod_pvt) > RING_SIZE(_r))260261/* Ill-behaved backend determination: Can there be this many responses? */262#define RING_RESPONSE_PROD_OVERFLOW(_r, _prod) \263(((_prod) - (_r)->rsp_cons) > RING_SIZE(_r))264265#define RING_PUSH_REQUESTS(_r) do { \266xen_wmb(); /* back sees requests /before/ updated producer index */ \267(_r)->sring->req_prod = (_r)->req_prod_pvt; \268} while (0)269270#define RING_PUSH_RESPONSES(_r) do { \271xen_wmb(); /* front sees resps /before/ updated producer index */ \272(_r)->sring->rsp_prod = (_r)->rsp_prod_pvt; \273} while (0)274275/*276* Notification hold-off (req_event and rsp_event):277*278* When queueing requests or responses on a shared ring, it may not always be279* necessary to notify the remote end. For example, if requests are in flight280* in a backend, the front may be able to queue further requests without281* notifying the back (if the back checks for new requests when it queues282* responses).283*284* When enqueuing requests or responses:285*286* Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument287* is a boolean return value. True indicates that the receiver requires an288* asynchronous notification.289*290* After dequeuing requests or responses (before sleeping the connection):291*292* Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().293* The second argument is a boolean return value. True indicates that there294* are pending messages on the ring (i.e., the connection should not be put295* to sleep).296*297* These macros will set the req_event/rsp_event field to trigger a298* notification on the very next message that is enqueued. If you want to299* create batches of work (i.e., only receive a notification after several300* messages have been enqueued) then you will need to create a customised301* version of the FINAL_CHECK macro in your own code, which sets the event302* field appropriately.303*/304305#define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \306RING_IDX __old = (_r)->sring->req_prod; \307RING_IDX __new = (_r)->req_prod_pvt; \308xen_wmb(); /* back sees requests /before/ updated producer index */ \309(_r)->sring->req_prod = __new; \310xen_mb(); /* back sees new requests /before/ we check req_event */ \311(_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) < \312(RING_IDX)(__new - __old)); \313} while (0)314315#define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \316RING_IDX __old = (_r)->sring->rsp_prod; \317RING_IDX __new = (_r)->rsp_prod_pvt; \318xen_wmb(); /* front sees resps /before/ updated producer index */ \319(_r)->sring->rsp_prod = __new; \320xen_mb(); /* front sees new resps /before/ we check rsp_event */ \321(_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) < \322(RING_IDX)(__new - __old)); \323} while (0)324325#define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \326(_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \327if (_work_to_do) break; \328(_r)->sring->req_event = (_r)->req_cons + 1; \329xen_mb(); \330(_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \331} while (0)332333#define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \334(_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \335if (_work_to_do) break; \336(_r)->sring->rsp_event = (_r)->rsp_cons + 1; \337xen_mb(); \338(_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \339} while (0)340341342/*343* DEFINE_XEN_FLEX_RING_AND_INTF defines two monodirectional rings and344* functions to check if there is data on the ring, and to read and345* write to them.346*347* DEFINE_XEN_FLEX_RING is similar to DEFINE_XEN_FLEX_RING_AND_INTF, but348* does not define the indexes page. As different protocols can have349* extensions to the basic format, this macro allow them to define their350* own struct.351*352* XEN_FLEX_RING_SIZE353* Convenience macro to calculate the size of one of the two rings354* from the overall order.355*356* $NAME_mask357* Function to apply the size mask to an index, to reduce the index358* within the range [0-size].359*360* $NAME_read_packet361* Function to read data from the ring. The amount of data to read is362* specified by the "size" argument.363*364* $NAME_write_packet365* Function to write data to the ring. The amount of data to write is366* specified by the "size" argument.367*368* $NAME_get_ring_ptr369* Convenience function that returns a pointer to read/write to the370* ring at the right location.371*372* $NAME_data_intf373* Indexes page, shared between frontend and backend. It also374* contains the array of grant refs.375*376* $NAME_queued377* Function to calculate how many bytes are currently on the ring,378* ready to be read. It can also be used to calculate how much free379* space is currently on the ring (XEN_FLEX_RING_SIZE() -380* $NAME_queued()).381*/382383#ifndef XEN_PAGE_SHIFT384/* The PAGE_SIZE for ring protocols and hypercall interfaces is always385* 4K, regardless of the architecture, and page granularity chosen by386* operating systems.387*/388#define XEN_PAGE_SHIFT 12389#endif390#define XEN_FLEX_RING_SIZE(order) \391(1UL << ((order) + XEN_PAGE_SHIFT - 1))392393#define DEFINE_XEN_FLEX_RING(name) \394static inline RING_IDX name##_mask(RING_IDX idx, RING_IDX ring_size) \395{ \396return idx & (ring_size - 1); \397} \398\399static inline unsigned char *name##_get_ring_ptr(unsigned char *buf, \400RING_IDX idx, \401RING_IDX ring_size) \402{ \403return buf + name##_mask(idx, ring_size); \404} \405\406static inline void name##_read_packet(void *opaque, \407const unsigned char *buf, \408size_t size, \409RING_IDX masked_prod, \410RING_IDX *masked_cons, \411RING_IDX ring_size) \412{ \413if (*masked_cons < masked_prod || \414size <= ring_size - *masked_cons) { \415memcpy(opaque, buf + *masked_cons, size); \416} else { \417memcpy(opaque, buf + *masked_cons, ring_size - *masked_cons); \418memcpy((unsigned char *)opaque + ring_size - *masked_cons, buf, \419size - (ring_size - *masked_cons)); \420} \421*masked_cons = name##_mask(*masked_cons + size, ring_size); \422} \423\424static inline void name##_write_packet(unsigned char *buf, \425const void *opaque, \426size_t size, \427RING_IDX *masked_prod, \428RING_IDX masked_cons, \429RING_IDX ring_size) \430{ \431if (*masked_prod < masked_cons || \432size <= ring_size - *masked_prod) { \433memcpy(buf + *masked_prod, opaque, size); \434} else { \435memcpy(buf + *masked_prod, opaque, ring_size - *masked_prod); \436memcpy(buf, (unsigned char *)opaque + (ring_size - *masked_prod), \437size - (ring_size - *masked_prod)); \438} \439*masked_prod = name##_mask(*masked_prod + size, ring_size); \440} \441\442static inline RING_IDX name##_queued(RING_IDX prod, \443RING_IDX cons, \444RING_IDX ring_size) \445{ \446RING_IDX size; \447\448if (prod == cons) \449return 0; \450\451prod = name##_mask(prod, ring_size); \452cons = name##_mask(cons, ring_size); \453\454if (prod == cons) \455return ring_size; \456\457if (prod > cons) \458size = prod - cons; \459else \460size = ring_size - (cons - prod); \461return size; \462} \463\464struct name##_data { \465unsigned char *in; /* half of the allocation */ \466unsigned char *out; /* half of the allocation */ \467}468469#define DEFINE_XEN_FLEX_RING_AND_INTF(name) \470struct name##_data_intf { \471RING_IDX in_cons, in_prod; \472\473uint8_t pad1[56]; \474\475RING_IDX out_cons, out_prod; \476\477uint8_t pad2[56]; \478\479RING_IDX ring_order; \480grant_ref_t ref[]; \481}; \482DEFINE_XEN_FLEX_RING(name)483484#endif /* __XEN_PUBLIC_IO_RING_H__ */485486/*487* Local variables:488* mode: C489* c-file-style: "BSD"490* c-basic-offset: 4491* tab-width: 4492* indent-tabs-mode: nil493* End:494*/495496497