rxe_pool.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_POOL_H
  34. #define RXE_POOL_H
  35. #define RXE_POOL_ALIGN (16)
  36. #define RXE_POOL_CACHE_FLAGS (0)
  37. enum rxe_pool_flags {
  38. RXE_POOL_ATOMIC = BIT(0),
  39. RXE_POOL_INDEX = BIT(1),
  40. RXE_POOL_KEY = BIT(2),
  41. };
  42. enum rxe_elem_type {
  43. RXE_TYPE_UC,
  44. RXE_TYPE_PD,
  45. RXE_TYPE_AH,
  46. RXE_TYPE_SRQ,
  47. RXE_TYPE_QP,
  48. RXE_TYPE_CQ,
  49. RXE_TYPE_MR,
  50. RXE_TYPE_MW,
  51. RXE_TYPE_MC_GRP,
  52. RXE_TYPE_MC_ELEM,
  53. RXE_NUM_TYPES, /* keep me last */
  54. };
  55. struct rxe_pool_entry;
  56. struct rxe_type_info {
  57. const char *name;
  58. size_t size;
  59. void (*cleanup)(struct rxe_pool_entry *obj);
  60. enum rxe_pool_flags flags;
  61. u32 max_index;
  62. u32 min_index;
  63. size_t key_offset;
  64. size_t key_size;
  65. struct kmem_cache *cache;
  66. };
  67. extern struct rxe_type_info rxe_type_info[];
  68. enum rxe_pool_state {
  69. rxe_pool_invalid,
  70. rxe_pool_valid,
  71. };
  72. struct rxe_pool_entry {
  73. struct rxe_pool *pool;
  74. struct kref ref_cnt;
  75. struct list_head list;
  76. /* only used if indexed or keyed */
  77. struct rb_node node;
  78. u32 index;
  79. };
  80. struct rxe_pool {
  81. struct rxe_dev *rxe;
  82. spinlock_t pool_lock; /* pool spinlock */
  83. size_t elem_size;
  84. struct kref ref_cnt;
  85. void (*cleanup)(struct rxe_pool_entry *obj);
  86. enum rxe_pool_state state;
  87. enum rxe_pool_flags flags;
  88. enum rxe_elem_type type;
  89. unsigned int max_elem;
  90. atomic_t num_elem;
  91. /* only used if indexed or keyed */
  92. struct rb_root tree;
  93. unsigned long *table;
  94. size_t table_size;
  95. u32 max_index;
  96. u32 min_index;
  97. u32 last;
  98. size_t key_offset;
  99. size_t key_size;
  100. };
  101. /* initialize slab caches for managed objects */
  102. int rxe_cache_init(void);
  103. /* cleanup slab caches for managed objects */
  104. void rxe_cache_exit(void);
  105. /* initialize a pool of objects with given limit on
  106. * number of elements. gets parameters from rxe_type_info
  107. * pool elements will be allocated out of a slab cache
  108. */
  109. int rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
  110. enum rxe_elem_type type, u32 max_elem);
  111. /* free resources from object pool */
  112. int rxe_pool_cleanup(struct rxe_pool *pool);
  113. /* allocate an object from pool */
  114. void *rxe_alloc(struct rxe_pool *pool);
  115. /* assign an index to an indexed object and insert object into
  116. * pool's rb tree
  117. */
  118. void rxe_add_index(void *elem);
  119. /* drop an index and remove object from rb tree */
  120. void rxe_drop_index(void *elem);
  121. /* assign a key to a keyed object and insert object into
  122. * pool's rb tree
  123. */
  124. void rxe_add_key(void *elem, void *key);
  125. /* remove elem from rb tree */
  126. void rxe_drop_key(void *elem);
  127. /* lookup an indexed object from index. takes a reference on object */
  128. void *rxe_pool_get_index(struct rxe_pool *pool, u32 index);
  129. /* lookup keyed object from key. takes a reference on the object */
  130. void *rxe_pool_get_key(struct rxe_pool *pool, void *key);
  131. /* cleanup an object when all references are dropped */
  132. void rxe_elem_release(struct kref *kref);
  133. /* take a reference on an object */
  134. #define rxe_add_ref(elem) kref_get(&(elem)->pelem.ref_cnt)
  135. /* drop a reference on an object */
  136. #define rxe_drop_ref(elem) kref_put(&(elem)->pelem.ref_cnt, rxe_elem_release)
  137. #endif /* RXE_POOL_H */