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