rxe_queue.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #ifndef RXE_QUEUE_H
  34. #define RXE_QUEUE_H
  35. /* implements a simple circular buffer that can optionally be
  36. * shared between user space and the kernel and can be resized
  37. * the requested element size is rounded up to a power of 2
  38. * and the number of elements in the buffer is also rounded
  39. * up to a power of 2. Since the queue is empty when the
  40. * producer and consumer indices match the maximum capacity
  41. * of the queue is one less than the number of element slots
  42. */
  43. /* this data structure is shared between user space and kernel
  44. * space for those cases where the queue is shared. It contains
  45. * the producer and consumer indices. Is also contains a copy
  46. * of the queue size parameters for user space to use but the
  47. * kernel must use the parameters in the rxe_queue struct
  48. * this MUST MATCH the corresponding librxe struct
  49. * for performance reasons arrange to have producer and consumer
  50. * pointers in separate cache lines
  51. * the kernel should always mask the indices to avoid accessing
  52. * memory outside of the data area
  53. */
  54. struct rxe_queue_buf {
  55. __u32 log2_elem_size;
  56. __u32 index_mask;
  57. __u32 pad_1[30];
  58. __u32 producer_index;
  59. __u32 pad_2[31];
  60. __u32 consumer_index;
  61. __u32 pad_3[31];
  62. __u8 data[0];
  63. };
  64. struct rxe_queue {
  65. struct rxe_dev *rxe;
  66. struct rxe_queue_buf *buf;
  67. struct rxe_mmap_info *ip;
  68. size_t buf_size;
  69. size_t elem_size;
  70. unsigned int log2_elem_size;
  71. unsigned int index_mask;
  72. };
  73. int do_mmap_info(struct rxe_dev *rxe,
  74. struct mminfo __user *outbuf,
  75. struct ib_ucontext *context,
  76. struct rxe_queue_buf *buf,
  77. size_t buf_size,
  78. struct rxe_mmap_info **ip_p);
  79. void rxe_queue_reset(struct rxe_queue *q);
  80. struct rxe_queue *rxe_queue_init(struct rxe_dev *rxe,
  81. int *num_elem,
  82. unsigned int elem_size);
  83. int rxe_queue_resize(struct rxe_queue *q,
  84. unsigned int *num_elem_p,
  85. unsigned int elem_size,
  86. struct ib_ucontext *context,
  87. struct mminfo __user *outbuf,
  88. /* Protect producers while resizing queue */
  89. spinlock_t *producer_lock,
  90. /* Protect consumers while resizing queue */
  91. spinlock_t *consumer_lock);
  92. void rxe_queue_cleanup(struct rxe_queue *queue);
  93. static inline int next_index(struct rxe_queue *q, int index)
  94. {
  95. return (index + 1) & q->buf->index_mask;
  96. }
  97. static inline int queue_empty(struct rxe_queue *q)
  98. {
  99. return ((q->buf->producer_index - q->buf->consumer_index)
  100. & q->index_mask) == 0;
  101. }
  102. static inline int queue_full(struct rxe_queue *q)
  103. {
  104. return ((q->buf->producer_index + 1 - q->buf->consumer_index)
  105. & q->index_mask) == 0;
  106. }
  107. static inline void advance_producer(struct rxe_queue *q)
  108. {
  109. q->buf->producer_index = (q->buf->producer_index + 1)
  110. & q->index_mask;
  111. }
  112. static inline void advance_consumer(struct rxe_queue *q)
  113. {
  114. q->buf->consumer_index = (q->buf->consumer_index + 1)
  115. & q->index_mask;
  116. }
  117. static inline void *producer_addr(struct rxe_queue *q)
  118. {
  119. return q->buf->data + ((q->buf->producer_index & q->index_mask)
  120. << q->log2_elem_size);
  121. }
  122. static inline void *consumer_addr(struct rxe_queue *q)
  123. {
  124. return q->buf->data + ((q->buf->consumer_index & q->index_mask)
  125. << q->log2_elem_size);
  126. }
  127. static inline unsigned int producer_index(struct rxe_queue *q)
  128. {
  129. return q->buf->producer_index;
  130. }
  131. static inline unsigned int consumer_index(struct rxe_queue *q)
  132. {
  133. return q->buf->consumer_index;
  134. }
  135. static inline void *addr_from_index(struct rxe_queue *q, unsigned int index)
  136. {
  137. return q->buf->data + ((index & q->index_mask)
  138. << q->buf->log2_elem_size);
  139. }
  140. static inline unsigned int index_from_addr(const struct rxe_queue *q,
  141. const void *addr)
  142. {
  143. return (((u8 *)addr - q->buf->data) >> q->log2_elem_size)
  144. & q->index_mask;
  145. }
  146. static inline unsigned int queue_count(const struct rxe_queue *q)
  147. {
  148. return (q->buf->producer_index - q->buf->consumer_index)
  149. & q->index_mask;
  150. }
  151. static inline void *queue_head(struct rxe_queue *q)
  152. {
  153. return queue_empty(q) ? NULL : consumer_addr(q);
  154. }
  155. #endif /* RXE_QUEUE_H */