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