Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2004 Topspin Communications. All rights reserved. |
Hal Rosenstock | c183a4c | 2005-07-27 11:45:20 -0700 | [diff] [blame] | 3 | * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Hal Rosenstock | c183a4c | 2005-07-27 11:45:20 -0700 | [diff] [blame] | 33 | * $Id: fmr_pool.c 2730 2005-06-28 16:43:03Z sean.hefty $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | #include <linux/errno.h> |
| 37 | #include <linux/spinlock.h> |
| 38 | #include <linux/slab.h> |
| 39 | #include <linux/jhash.h> |
| 40 | #include <linux/kthread.h> |
| 41 | |
Roland Dreier | a4d61e8 | 2005-08-25 13:40:04 -0700 | [diff] [blame] | 42 | #include <rdma/ib_fmr_pool.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #include "core_priv.h" |
| 45 | |
Roland Dreier | 1a70a05 | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 46 | #define PFX "fmr_pool: " |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | enum { |
| 49 | IB_FMR_MAX_REMAPS = 32, |
| 50 | |
| 51 | IB_FMR_HASH_BITS = 8, |
| 52 | IB_FMR_HASH_SIZE = 1 << IB_FMR_HASH_BITS, |
| 53 | IB_FMR_HASH_MASK = IB_FMR_HASH_SIZE - 1 |
| 54 | }; |
| 55 | |
| 56 | /* |
| 57 | * If an FMR is not in use, then the list member will point to either |
| 58 | * its pool's free_list (if the FMR can be mapped again; that is, |
Or Gerlitz | 6c8c1aa | 2006-06-17 20:37:37 -0700 | [diff] [blame] | 59 | * remap_count < pool->max_remaps) or its pool's dirty_list (if the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | * FMR needs to be unmapped before being remapped). In either of |
| 61 | * these cases it is a bug if the ref_count is not 0. In other words, |
| 62 | * if ref_count is > 0, then the list member must not be linked into |
| 63 | * either free_list or dirty_list. |
| 64 | * |
| 65 | * The cache_node member is used to link the FMR into a cache bucket |
| 66 | * (if caching is enabled). This is independent of the reference |
| 67 | * count of the FMR. When a valid FMR is released, its ref_count is |
| 68 | * decremented, and if ref_count reaches 0, the FMR is placed in |
| 69 | * either free_list or dirty_list as appropriate. However, it is not |
| 70 | * removed from the cache and may be "revived" if a call to |
| 71 | * ib_fmr_register_physical() occurs before the FMR is remapped. In |
| 72 | * this case we just increment the ref_count and remove the FMR from |
| 73 | * free_list/dirty_list. |
| 74 | * |
| 75 | * Before we remap an FMR from free_list, we remove it from the cache |
| 76 | * (to prevent another user from obtaining a stale FMR). When an FMR |
| 77 | * is released, we add it to the tail of the free list, so that our |
| 78 | * cache eviction policy is "least recently used." |
| 79 | * |
| 80 | * All manipulation of ref_count, list and cache_node is protected by |
| 81 | * pool_lock to maintain consistency. |
| 82 | */ |
| 83 | |
| 84 | struct ib_fmr_pool { |
| 85 | spinlock_t pool_lock; |
| 86 | |
| 87 | int pool_size; |
| 88 | int max_pages; |
Or Gerlitz | 6c8c1aa | 2006-06-17 20:37:37 -0700 | [diff] [blame] | 89 | int max_remaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | int dirty_watermark; |
| 91 | int dirty_len; |
| 92 | struct list_head free_list; |
| 93 | struct list_head dirty_list; |
| 94 | struct hlist_head *cache_bucket; |
| 95 | |
| 96 | void (*flush_function)(struct ib_fmr_pool *pool, |
| 97 | void * arg); |
| 98 | void *flush_arg; |
| 99 | |
| 100 | struct task_struct *thread; |
| 101 | |
| 102 | atomic_t req_ser; |
| 103 | atomic_t flush_ser; |
| 104 | |
| 105 | wait_queue_head_t force_wait; |
| 106 | }; |
| 107 | |
| 108 | static inline u32 ib_fmr_hash(u64 first_page) |
| 109 | { |
Roland Dreier | de0d964 | 2005-04-16 15:26:10 -0700 | [diff] [blame] | 110 | return jhash_2words((u32) first_page, (u32) (first_page >> 32), 0) & |
| 111 | (IB_FMR_HASH_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* Caller must hold pool_lock */ |
| 115 | static inline struct ib_pool_fmr *ib_fmr_cache_lookup(struct ib_fmr_pool *pool, |
| 116 | u64 *page_list, |
| 117 | int page_list_len, |
| 118 | u64 io_virtual_address) |
| 119 | { |
| 120 | struct hlist_head *bucket; |
| 121 | struct ib_pool_fmr *fmr; |
| 122 | struct hlist_node *pos; |
| 123 | |
| 124 | if (!pool->cache_bucket) |
| 125 | return NULL; |
| 126 | |
| 127 | bucket = pool->cache_bucket + ib_fmr_hash(*page_list); |
| 128 | |
| 129 | hlist_for_each_entry(fmr, pos, bucket, cache_node) |
| 130 | if (io_virtual_address == fmr->io_virtual_address && |
| 131 | page_list_len == fmr->page_list_len && |
| 132 | !memcmp(page_list, fmr->page_list, |
| 133 | page_list_len * sizeof *page_list)) |
| 134 | return fmr; |
| 135 | |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | static void ib_fmr_batch_release(struct ib_fmr_pool *pool) |
| 140 | { |
| 141 | int ret; |
Olaf Kirch | a3cd7d9 | 2008-01-16 18:37:16 +0100 | [diff] [blame] | 142 | struct ib_pool_fmr *fmr, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | LIST_HEAD(unmap_list); |
| 144 | LIST_HEAD(fmr_list); |
| 145 | |
| 146 | spin_lock_irq(&pool->pool_lock); |
| 147 | |
| 148 | list_for_each_entry(fmr, &pool->dirty_list, list) { |
| 149 | hlist_del_init(&fmr->cache_node); |
| 150 | fmr->remap_count = 0; |
| 151 | list_add_tail(&fmr->fmr->list, &fmr_list); |
| 152 | |
| 153 | #ifdef DEBUG |
| 154 | if (fmr->ref_count !=0) { |
Anton Blanchard | 8a68bbe | 2007-08-29 08:36:22 -0500 | [diff] [blame] | 155 | printk(KERN_WARNING PFX "Unmapping FMR 0x%08x with ref count %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | fmr, fmr->ref_count); |
| 157 | } |
| 158 | #endif |
| 159 | } |
| 160 | |
Olaf Kirch | a3cd7d9 | 2008-01-16 18:37:16 +0100 | [diff] [blame] | 161 | /* |
| 162 | * The free_list may hold FMRs that have been put there |
| 163 | * because they haven't reached the max_remap count. |
| 164 | * Invalidate their mapping as well. |
| 165 | */ |
| 166 | list_for_each_entry_safe(fmr, next, &pool->free_list, list) { |
| 167 | if (fmr->remap_count == 0) |
| 168 | continue; |
| 169 | hlist_del_init(&fmr->cache_node); |
| 170 | fmr->remap_count = 0; |
| 171 | list_add_tail(&fmr->fmr->list, &fmr_list); |
| 172 | list_move(&fmr->list, &unmap_list); |
| 173 | } |
| 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | list_splice(&pool->dirty_list, &unmap_list); |
| 176 | INIT_LIST_HEAD(&pool->dirty_list); |
| 177 | pool->dirty_len = 0; |
| 178 | |
| 179 | spin_unlock_irq(&pool->pool_lock); |
| 180 | |
| 181 | if (list_empty(&unmap_list)) { |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | ret = ib_unmap_fmr(&fmr_list); |
| 186 | if (ret) |
Anton Blanchard | 8a68bbe | 2007-08-29 08:36:22 -0500 | [diff] [blame] | 187 | printk(KERN_WARNING PFX "ib_unmap_fmr returned %d\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | spin_lock_irq(&pool->pool_lock); |
| 190 | list_splice(&unmap_list, &pool->free_list); |
| 191 | spin_unlock_irq(&pool->pool_lock); |
| 192 | } |
| 193 | |
| 194 | static int ib_fmr_cleanup_thread(void *pool_ptr) |
| 195 | { |
| 196 | struct ib_fmr_pool *pool = pool_ptr; |
| 197 | |
| 198 | do { |
Olaf Kirch | a656eb7 | 2008-01-16 18:36:27 +0100 | [diff] [blame] | 199 | if (atomic_read(&pool->flush_ser) - atomic_read(&pool->req_ser) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | ib_fmr_batch_release(pool); |
| 201 | |
| 202 | atomic_inc(&pool->flush_ser); |
| 203 | wake_up_interruptible(&pool->force_wait); |
| 204 | |
| 205 | if (pool->flush_function) |
| 206 | pool->flush_function(pool, pool->flush_arg); |
| 207 | } |
| 208 | |
| 209 | set_current_state(TASK_INTERRUPTIBLE); |
Olaf Kirch | a656eb7 | 2008-01-16 18:36:27 +0100 | [diff] [blame] | 210 | if (atomic_read(&pool->flush_ser) - atomic_read(&pool->req_ser) >= 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | !kthread_should_stop()) |
| 212 | schedule(); |
| 213 | __set_current_state(TASK_RUNNING); |
| 214 | } while (!kthread_should_stop()); |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * ib_create_fmr_pool - Create an FMR pool |
| 221 | * @pd:Protection domain for FMRs |
| 222 | * @params:FMR pool parameters |
| 223 | * |
| 224 | * Create a pool of FMRs. Return value is pointer to new pool or |
| 225 | * error code if creation failed. |
| 226 | */ |
| 227 | struct ib_fmr_pool *ib_create_fmr_pool(struct ib_pd *pd, |
| 228 | struct ib_fmr_pool_param *params) |
| 229 | { |
| 230 | struct ib_device *device; |
| 231 | struct ib_fmr_pool *pool; |
Or Gerlitz | 6c8c1aa | 2006-06-17 20:37:37 -0700 | [diff] [blame] | 232 | struct ib_device_attr *attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | int i; |
| 234 | int ret; |
Or Gerlitz | 6c8c1aa | 2006-06-17 20:37:37 -0700 | [diff] [blame] | 235 | int max_remaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | if (!params) |
| 238 | return ERR_PTR(-EINVAL); |
| 239 | |
| 240 | device = pd->device; |
| 241 | if (!device->alloc_fmr || !device->dealloc_fmr || |
| 242 | !device->map_phys_fmr || !device->unmap_fmr) { |
Roland Dreier | 1a70a05 | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 243 | printk(KERN_INFO PFX "Device %s does not support FMRs\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | device->name); |
| 245 | return ERR_PTR(-ENOSYS); |
| 246 | } |
| 247 | |
Or Gerlitz | 6c8c1aa | 2006-06-17 20:37:37 -0700 | [diff] [blame] | 248 | attr = kmalloc(sizeof *attr, GFP_KERNEL); |
| 249 | if (!attr) { |
Anton Blanchard | 8a68bbe | 2007-08-29 08:36:22 -0500 | [diff] [blame] | 250 | printk(KERN_WARNING PFX "couldn't allocate device attr struct\n"); |
Or Gerlitz | 6c8c1aa | 2006-06-17 20:37:37 -0700 | [diff] [blame] | 251 | return ERR_PTR(-ENOMEM); |
| 252 | } |
| 253 | |
| 254 | ret = ib_query_device(device, attr); |
| 255 | if (ret) { |
Anton Blanchard | 8a68bbe | 2007-08-29 08:36:22 -0500 | [diff] [blame] | 256 | printk(KERN_WARNING PFX "couldn't query device: %d\n", ret); |
Or Gerlitz | 6c8c1aa | 2006-06-17 20:37:37 -0700 | [diff] [blame] | 257 | kfree(attr); |
| 258 | return ERR_PTR(ret); |
| 259 | } |
| 260 | |
| 261 | if (!attr->max_map_per_fmr) |
| 262 | max_remaps = IB_FMR_MAX_REMAPS; |
| 263 | else |
| 264 | max_remaps = attr->max_map_per_fmr; |
| 265 | |
| 266 | kfree(attr); |
| 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | pool = kmalloc(sizeof *pool, GFP_KERNEL); |
| 269 | if (!pool) { |
Anton Blanchard | 8a68bbe | 2007-08-29 08:36:22 -0500 | [diff] [blame] | 270 | printk(KERN_WARNING PFX "couldn't allocate pool struct\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | return ERR_PTR(-ENOMEM); |
| 272 | } |
| 273 | |
| 274 | pool->cache_bucket = NULL; |
| 275 | |
| 276 | pool->flush_function = params->flush_function; |
| 277 | pool->flush_arg = params->flush_arg; |
| 278 | |
| 279 | INIT_LIST_HEAD(&pool->free_list); |
| 280 | INIT_LIST_HEAD(&pool->dirty_list); |
| 281 | |
| 282 | if (params->cache) { |
| 283 | pool->cache_bucket = |
| 284 | kmalloc(IB_FMR_HASH_SIZE * sizeof *pool->cache_bucket, |
| 285 | GFP_KERNEL); |
| 286 | if (!pool->cache_bucket) { |
Anton Blanchard | 8a68bbe | 2007-08-29 08:36:22 -0500 | [diff] [blame] | 287 | printk(KERN_WARNING PFX "Failed to allocate cache in pool\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | ret = -ENOMEM; |
| 289 | goto out_free_pool; |
| 290 | } |
| 291 | |
| 292 | for (i = 0; i < IB_FMR_HASH_SIZE; ++i) |
| 293 | INIT_HLIST_HEAD(pool->cache_bucket + i); |
| 294 | } |
| 295 | |
| 296 | pool->pool_size = 0; |
| 297 | pool->max_pages = params->max_pages_per_fmr; |
Or Gerlitz | 6c8c1aa | 2006-06-17 20:37:37 -0700 | [diff] [blame] | 298 | pool->max_remaps = max_remaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | pool->dirty_watermark = params->dirty_watermark; |
| 300 | pool->dirty_len = 0; |
| 301 | spin_lock_init(&pool->pool_lock); |
| 302 | atomic_set(&pool->req_ser, 0); |
| 303 | atomic_set(&pool->flush_ser, 0); |
| 304 | init_waitqueue_head(&pool->force_wait); |
| 305 | |
Anton Blanchard | 3f776e8 | 2007-10-30 14:57:43 -0700 | [diff] [blame] | 306 | pool->thread = kthread_run(ib_fmr_cleanup_thread, |
| 307 | pool, |
| 308 | "ib_fmr(%s)", |
| 309 | device->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | if (IS_ERR(pool->thread)) { |
Anton Blanchard | 8a68bbe | 2007-08-29 08:36:22 -0500 | [diff] [blame] | 311 | printk(KERN_WARNING PFX "couldn't start cleanup thread\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | ret = PTR_ERR(pool->thread); |
| 313 | goto out_free_pool; |
| 314 | } |
| 315 | |
| 316 | { |
| 317 | struct ib_pool_fmr *fmr; |
Roland Dreier | 38abaa6 | 2007-02-16 14:41:14 -0800 | [diff] [blame] | 318 | struct ib_fmr_attr fmr_attr = { |
Or Gerlitz | d36f34a | 2006-02-02 10:43:45 -0800 | [diff] [blame] | 319 | .max_pages = params->max_pages_per_fmr, |
Or Gerlitz | 6c8c1aa | 2006-06-17 20:37:37 -0700 | [diff] [blame] | 320 | .max_maps = pool->max_remaps, |
Or Gerlitz | d36f34a | 2006-02-02 10:43:45 -0800 | [diff] [blame] | 321 | .page_shift = params->page_shift |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | }; |
Or Gerlitz | 1d96354 | 2008-01-29 12:56:18 +0200 | [diff] [blame] | 323 | int bytes_per_fmr = sizeof *fmr; |
| 324 | |
| 325 | if (pool->cache_bucket) |
| 326 | bytes_per_fmr += params->max_pages_per_fmr * sizeof (u64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
| 328 | for (i = 0; i < params->pool_size; ++i) { |
Or Gerlitz | 1d96354 | 2008-01-29 12:56:18 +0200 | [diff] [blame] | 329 | fmr = kmalloc(bytes_per_fmr, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | if (!fmr) { |
Roland Dreier | 1a70a05 | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 331 | printk(KERN_WARNING PFX "failed to allocate fmr " |
Anton Blanchard | 8a68bbe | 2007-08-29 08:36:22 -0500 | [diff] [blame] | 332 | "struct for FMR %d\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | goto out_fail; |
| 334 | } |
| 335 | |
| 336 | fmr->pool = pool; |
| 337 | fmr->remap_count = 0; |
| 338 | fmr->ref_count = 0; |
| 339 | INIT_HLIST_NODE(&fmr->cache_node); |
| 340 | |
Roland Dreier | 38abaa6 | 2007-02-16 14:41:14 -0800 | [diff] [blame] | 341 | fmr->fmr = ib_alloc_fmr(pd, params->access, &fmr_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | if (IS_ERR(fmr->fmr)) { |
Roland Dreier | 1a70a05 | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 343 | printk(KERN_WARNING PFX "fmr_create failed " |
Anton Blanchard | 8a68bbe | 2007-08-29 08:36:22 -0500 | [diff] [blame] | 344 | "for FMR %d\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | kfree(fmr); |
| 346 | goto out_fail; |
| 347 | } |
| 348 | |
| 349 | list_add_tail(&fmr->list, &pool->free_list); |
| 350 | ++pool->pool_size; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return pool; |
| 355 | |
| 356 | out_free_pool: |
| 357 | kfree(pool->cache_bucket); |
| 358 | kfree(pool); |
| 359 | |
| 360 | return ERR_PTR(ret); |
| 361 | |
| 362 | out_fail: |
| 363 | ib_destroy_fmr_pool(pool); |
| 364 | |
| 365 | return ERR_PTR(-ENOMEM); |
| 366 | } |
| 367 | EXPORT_SYMBOL(ib_create_fmr_pool); |
| 368 | |
| 369 | /** |
| 370 | * ib_destroy_fmr_pool - Free FMR pool |
| 371 | * @pool:FMR pool to free |
| 372 | * |
| 373 | * Destroy an FMR pool and free all associated resources. |
| 374 | */ |
Hal Rosenstock | c183a4c | 2005-07-27 11:45:20 -0700 | [diff] [blame] | 375 | void ib_destroy_fmr_pool(struct ib_fmr_pool *pool) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
| 377 | struct ib_pool_fmr *fmr; |
| 378 | struct ib_pool_fmr *tmp; |
Roland Dreier | ffbf4c3 | 2005-08-15 07:35:16 -0700 | [diff] [blame] | 379 | LIST_HEAD(fmr_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | int i; |
| 381 | |
| 382 | kthread_stop(pool->thread); |
| 383 | ib_fmr_batch_release(pool); |
| 384 | |
| 385 | i = 0; |
| 386 | list_for_each_entry_safe(fmr, tmp, &pool->free_list, list) { |
| 387 | ib_dealloc_fmr(fmr->fmr); |
| 388 | list_del(&fmr->list); |
| 389 | kfree(fmr); |
| 390 | ++i; |
| 391 | } |
| 392 | |
| 393 | if (i < pool->pool_size) |
Anton Blanchard | 8a68bbe | 2007-08-29 08:36:22 -0500 | [diff] [blame] | 394 | printk(KERN_WARNING PFX "pool still has %d regions registered\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | pool->pool_size - i); |
| 396 | |
| 397 | kfree(pool->cache_bucket); |
| 398 | kfree(pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | } |
| 400 | EXPORT_SYMBOL(ib_destroy_fmr_pool); |
| 401 | |
| 402 | /** |
| 403 | * ib_flush_fmr_pool - Invalidate all unmapped FMRs |
| 404 | * @pool:FMR pool to flush |
| 405 | * |
| 406 | * Ensure that all unmapped FMRs are fully invalidated. |
| 407 | */ |
| 408 | int ib_flush_fmr_pool(struct ib_fmr_pool *pool) |
| 409 | { |
Roland Dreier | f47e22c | 2006-12-12 11:50:19 -0800 | [diff] [blame] | 410 | int serial = atomic_inc_return(&pool->req_ser); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
| 412 | wake_up_process(pool->thread); |
| 413 | |
| 414 | if (wait_event_interruptible(pool->force_wait, |
Roland Dreier | f47e22c | 2006-12-12 11:50:19 -0800 | [diff] [blame] | 415 | atomic_read(&pool->flush_ser) - serial >= 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | return -EINTR; |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | EXPORT_SYMBOL(ib_flush_fmr_pool); |
| 421 | |
| 422 | /** |
| 423 | * ib_fmr_pool_map_phys - |
| 424 | * @pool:FMR pool to allocate FMR from |
| 425 | * @page_list:List of pages to map |
| 426 | * @list_len:Number of pages in @page_list |
| 427 | * @io_virtual_address:I/O virtual address for new FMR |
| 428 | * |
| 429 | * Map an FMR from an FMR pool. |
| 430 | */ |
| 431 | struct ib_pool_fmr *ib_fmr_pool_map_phys(struct ib_fmr_pool *pool_handle, |
| 432 | u64 *page_list, |
| 433 | int list_len, |
Michael S. Tsirkin | adfaa88 | 2006-07-14 00:23:55 -0700 | [diff] [blame] | 434 | u64 io_virtual_address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | { |
| 436 | struct ib_fmr_pool *pool = pool_handle; |
| 437 | struct ib_pool_fmr *fmr; |
| 438 | unsigned long flags; |
| 439 | int result; |
| 440 | |
| 441 | if (list_len < 1 || list_len > pool->max_pages) |
| 442 | return ERR_PTR(-EINVAL); |
| 443 | |
| 444 | spin_lock_irqsave(&pool->pool_lock, flags); |
| 445 | fmr = ib_fmr_cache_lookup(pool, |
| 446 | page_list, |
| 447 | list_len, |
Michael S. Tsirkin | adfaa88 | 2006-07-14 00:23:55 -0700 | [diff] [blame] | 448 | io_virtual_address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | if (fmr) { |
| 450 | /* found in cache */ |
| 451 | ++fmr->ref_count; |
| 452 | if (fmr->ref_count == 1) { |
| 453 | list_del(&fmr->list); |
| 454 | } |
| 455 | |
| 456 | spin_unlock_irqrestore(&pool->pool_lock, flags); |
| 457 | |
| 458 | return fmr; |
| 459 | } |
| 460 | |
| 461 | if (list_empty(&pool->free_list)) { |
| 462 | spin_unlock_irqrestore(&pool->pool_lock, flags); |
| 463 | return ERR_PTR(-EAGAIN); |
| 464 | } |
| 465 | |
| 466 | fmr = list_entry(pool->free_list.next, struct ib_pool_fmr, list); |
| 467 | list_del(&fmr->list); |
| 468 | hlist_del_init(&fmr->cache_node); |
| 469 | spin_unlock_irqrestore(&pool->pool_lock, flags); |
| 470 | |
| 471 | result = ib_map_phys_fmr(fmr->fmr, page_list, list_len, |
Michael S. Tsirkin | adfaa88 | 2006-07-14 00:23:55 -0700 | [diff] [blame] | 472 | io_virtual_address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
| 474 | if (result) { |
| 475 | spin_lock_irqsave(&pool->pool_lock, flags); |
| 476 | list_add(&fmr->list, &pool->free_list); |
| 477 | spin_unlock_irqrestore(&pool->pool_lock, flags); |
| 478 | |
Roland Dreier | 1a70a05 | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 479 | printk(KERN_WARNING PFX "fmr_map returns %d\n", result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
| 481 | return ERR_PTR(result); |
| 482 | } |
| 483 | |
| 484 | ++fmr->remap_count; |
| 485 | fmr->ref_count = 1; |
| 486 | |
| 487 | if (pool->cache_bucket) { |
Michael S. Tsirkin | adfaa88 | 2006-07-14 00:23:55 -0700 | [diff] [blame] | 488 | fmr->io_virtual_address = io_virtual_address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | fmr->page_list_len = list_len; |
| 490 | memcpy(fmr->page_list, page_list, list_len * sizeof(*page_list)); |
| 491 | |
| 492 | spin_lock_irqsave(&pool->pool_lock, flags); |
| 493 | hlist_add_head(&fmr->cache_node, |
| 494 | pool->cache_bucket + ib_fmr_hash(fmr->page_list[0])); |
| 495 | spin_unlock_irqrestore(&pool->pool_lock, flags); |
| 496 | } |
| 497 | |
| 498 | return fmr; |
| 499 | } |
| 500 | EXPORT_SYMBOL(ib_fmr_pool_map_phys); |
| 501 | |
| 502 | /** |
| 503 | * ib_fmr_pool_unmap - Unmap FMR |
| 504 | * @fmr:FMR to unmap |
| 505 | * |
| 506 | * Unmap an FMR. The FMR mapping may remain valid until the FMR is |
| 507 | * reused (or until ib_flush_fmr_pool() is called). |
| 508 | */ |
| 509 | int ib_fmr_pool_unmap(struct ib_pool_fmr *fmr) |
| 510 | { |
| 511 | struct ib_fmr_pool *pool; |
| 512 | unsigned long flags; |
| 513 | |
| 514 | pool = fmr->pool; |
| 515 | |
| 516 | spin_lock_irqsave(&pool->pool_lock, flags); |
| 517 | |
| 518 | --fmr->ref_count; |
| 519 | if (!fmr->ref_count) { |
Or Gerlitz | 6c8c1aa | 2006-06-17 20:37:37 -0700 | [diff] [blame] | 520 | if (fmr->remap_count < pool->max_remaps) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | list_add_tail(&fmr->list, &pool->free_list); |
| 522 | } else { |
| 523 | list_add_tail(&fmr->list, &pool->dirty_list); |
Olaf Kirch | a656eb7 | 2008-01-16 18:36:27 +0100 | [diff] [blame] | 524 | if (++pool->dirty_len >= pool->dirty_watermark) { |
| 525 | atomic_inc(&pool->req_ser); |
| 526 | wake_up_process(pool->thread); |
| 527 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | #ifdef DEBUG |
| 532 | if (fmr->ref_count < 0) |
Anton Blanchard | 8a68bbe | 2007-08-29 08:36:22 -0500 | [diff] [blame] | 533 | printk(KERN_WARNING PFX "FMR %p has ref count %d < 0\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | fmr, fmr->ref_count); |
| 535 | #endif |
| 536 | |
| 537 | spin_unlock_irqrestore(&pool->pool_lock, flags); |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | EXPORT_SYMBOL(ib_fmr_pool_unmap); |