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