Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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