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