blob: 1286dc1b98b2acda72ce158ef8abd443cc3c8dc1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
Hal Rosenstockc183a4c2005-07-27 11:45:20 -07003 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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 Rosenstockc183a4c2005-07-27 11:45:20 -070033 * $Id: fmr_pool.c 2730 2005-06-28 16:43:03Z sean.hefty $
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
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 Dreiera4d61e82005-08-25 13:40:04 -070042#include <rdma/ib_fmr_pool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include "core_priv.h"
45
Roland Dreier1a70a052007-05-06 21:18:11 -070046#define PFX "fmr_pool: "
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048enum {
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 Gerlitz6c8c1aa2006-06-17 20:37:37 -070059 * remap_count < pool->max_remaps) or its pool's dirty_list (if the
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * 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
84struct ib_fmr_pool {
85 spinlock_t pool_lock;
86
87 int pool_size;
88 int max_pages;
Or Gerlitz6c8c1aa2006-06-17 20:37:37 -070089 int max_remaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 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
108static inline u32 ib_fmr_hash(u64 first_page)
109{
Roland Dreierde0d9642005-04-16 15:26:10 -0700110 return jhash_2words((u32) first_page, (u32) (first_page >> 32), 0) &
111 (IB_FMR_HASH_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114/* Caller must hold pool_lock */
115static 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
139static void ib_fmr_batch_release(struct ib_fmr_pool *pool)
140{
141 int ret;
Pete Wyckoff35fb5342008-02-26 13:27:31 -0500142 struct ib_pool_fmr *fmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 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 Blanchard8a68bbe2007-08-29 08:36:22 -0500155 printk(KERN_WARNING PFX "Unmapping FMR 0x%08x with ref count %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 fmr, fmr->ref_count);
157 }
158#endif
159 }
160
Robert P. J. Day157de222008-04-16 21:09:26 -0700161 list_splice_init(&pool->dirty_list, &unmap_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 pool->dirty_len = 0;
163
164 spin_unlock_irq(&pool->pool_lock);
165
166 if (list_empty(&unmap_list)) {
167 return;
168 }
169
170 ret = ib_unmap_fmr(&fmr_list);
171 if (ret)
Anton Blanchard8a68bbe2007-08-29 08:36:22 -0500172 printk(KERN_WARNING PFX "ib_unmap_fmr returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 spin_lock_irq(&pool->pool_lock);
175 list_splice(&unmap_list, &pool->free_list);
176 spin_unlock_irq(&pool->pool_lock);
177}
178
179static int ib_fmr_cleanup_thread(void *pool_ptr)
180{
181 struct ib_fmr_pool *pool = pool_ptr;
182
183 do {
Olaf Kircha656eb72008-01-16 18:36:27 +0100184 if (atomic_read(&pool->flush_ser) - atomic_read(&pool->req_ser) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 ib_fmr_batch_release(pool);
186
187 atomic_inc(&pool->flush_ser);
188 wake_up_interruptible(&pool->force_wait);
189
190 if (pool->flush_function)
191 pool->flush_function(pool, pool->flush_arg);
192 }
193
194 set_current_state(TASK_INTERRUPTIBLE);
Olaf Kircha656eb72008-01-16 18:36:27 +0100195 if (atomic_read(&pool->flush_ser) - atomic_read(&pool->req_ser) >= 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 !kthread_should_stop())
197 schedule();
198 __set_current_state(TASK_RUNNING);
199 } while (!kthread_should_stop());
200
201 return 0;
202}
203
204/**
205 * ib_create_fmr_pool - Create an FMR pool
206 * @pd:Protection domain for FMRs
207 * @params:FMR pool parameters
208 *
209 * Create a pool of FMRs. Return value is pointer to new pool or
210 * error code if creation failed.
211 */
212struct ib_fmr_pool *ib_create_fmr_pool(struct ib_pd *pd,
213 struct ib_fmr_pool_param *params)
214{
215 struct ib_device *device;
216 struct ib_fmr_pool *pool;
Or Gerlitz6c8c1aa2006-06-17 20:37:37 -0700217 struct ib_device_attr *attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 int i;
219 int ret;
Or Gerlitz6c8c1aa2006-06-17 20:37:37 -0700220 int max_remaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 if (!params)
223 return ERR_PTR(-EINVAL);
224
225 device = pd->device;
226 if (!device->alloc_fmr || !device->dealloc_fmr ||
227 !device->map_phys_fmr || !device->unmap_fmr) {
Roland Dreier1a70a052007-05-06 21:18:11 -0700228 printk(KERN_INFO PFX "Device %s does not support FMRs\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 device->name);
230 return ERR_PTR(-ENOSYS);
231 }
232
Or Gerlitz6c8c1aa2006-06-17 20:37:37 -0700233 attr = kmalloc(sizeof *attr, GFP_KERNEL);
234 if (!attr) {
Anton Blanchard8a68bbe2007-08-29 08:36:22 -0500235 printk(KERN_WARNING PFX "couldn't allocate device attr struct\n");
Or Gerlitz6c8c1aa2006-06-17 20:37:37 -0700236 return ERR_PTR(-ENOMEM);
237 }
238
239 ret = ib_query_device(device, attr);
240 if (ret) {
Anton Blanchard8a68bbe2007-08-29 08:36:22 -0500241 printk(KERN_WARNING PFX "couldn't query device: %d\n", ret);
Or Gerlitz6c8c1aa2006-06-17 20:37:37 -0700242 kfree(attr);
243 return ERR_PTR(ret);
244 }
245
246 if (!attr->max_map_per_fmr)
247 max_remaps = IB_FMR_MAX_REMAPS;
248 else
249 max_remaps = attr->max_map_per_fmr;
250
251 kfree(attr);
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 pool = kmalloc(sizeof *pool, GFP_KERNEL);
254 if (!pool) {
Anton Blanchard8a68bbe2007-08-29 08:36:22 -0500255 printk(KERN_WARNING PFX "couldn't allocate pool struct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return ERR_PTR(-ENOMEM);
257 }
258
259 pool->cache_bucket = NULL;
260
261 pool->flush_function = params->flush_function;
262 pool->flush_arg = params->flush_arg;
263
264 INIT_LIST_HEAD(&pool->free_list);
265 INIT_LIST_HEAD(&pool->dirty_list);
266
267 if (params->cache) {
268 pool->cache_bucket =
269 kmalloc(IB_FMR_HASH_SIZE * sizeof *pool->cache_bucket,
270 GFP_KERNEL);
271 if (!pool->cache_bucket) {
Anton Blanchard8a68bbe2007-08-29 08:36:22 -0500272 printk(KERN_WARNING PFX "Failed to allocate cache in pool\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 ret = -ENOMEM;
274 goto out_free_pool;
275 }
276
277 for (i = 0; i < IB_FMR_HASH_SIZE; ++i)
278 INIT_HLIST_HEAD(pool->cache_bucket + i);
279 }
280
281 pool->pool_size = 0;
282 pool->max_pages = params->max_pages_per_fmr;
Or Gerlitz6c8c1aa2006-06-17 20:37:37 -0700283 pool->max_remaps = max_remaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 pool->dirty_watermark = params->dirty_watermark;
285 pool->dirty_len = 0;
286 spin_lock_init(&pool->pool_lock);
287 atomic_set(&pool->req_ser, 0);
288 atomic_set(&pool->flush_ser, 0);
289 init_waitqueue_head(&pool->force_wait);
290
Anton Blanchard3f776e82007-10-30 14:57:43 -0700291 pool->thread = kthread_run(ib_fmr_cleanup_thread,
292 pool,
293 "ib_fmr(%s)",
294 device->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (IS_ERR(pool->thread)) {
Anton Blanchard8a68bbe2007-08-29 08:36:22 -0500296 printk(KERN_WARNING PFX "couldn't start cleanup thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 ret = PTR_ERR(pool->thread);
298 goto out_free_pool;
299 }
300
301 {
302 struct ib_pool_fmr *fmr;
Roland Dreier38abaa62007-02-16 14:41:14 -0800303 struct ib_fmr_attr fmr_attr = {
Or Gerlitzd36f34a2006-02-02 10:43:45 -0800304 .max_pages = params->max_pages_per_fmr,
Or Gerlitz6c8c1aa2006-06-17 20:37:37 -0700305 .max_maps = pool->max_remaps,
Or Gerlitzd36f34a2006-02-02 10:43:45 -0800306 .page_shift = params->page_shift
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 };
Or Gerlitz1d963542008-01-29 12:56:18 +0200308 int bytes_per_fmr = sizeof *fmr;
309
310 if (pool->cache_bucket)
311 bytes_per_fmr += params->max_pages_per_fmr * sizeof (u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 for (i = 0; i < params->pool_size; ++i) {
Or Gerlitz1d963542008-01-29 12:56:18 +0200314 fmr = kmalloc(bytes_per_fmr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (!fmr) {
Roland Dreier1a70a052007-05-06 21:18:11 -0700316 printk(KERN_WARNING PFX "failed to allocate fmr "
Anton Blanchard8a68bbe2007-08-29 08:36:22 -0500317 "struct for FMR %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 goto out_fail;
319 }
320
321 fmr->pool = pool;
322 fmr->remap_count = 0;
323 fmr->ref_count = 0;
324 INIT_HLIST_NODE(&fmr->cache_node);
325
Roland Dreier38abaa62007-02-16 14:41:14 -0800326 fmr->fmr = ib_alloc_fmr(pd, params->access, &fmr_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (IS_ERR(fmr->fmr)) {
Roland Dreier1a70a052007-05-06 21:18:11 -0700328 printk(KERN_WARNING PFX "fmr_create failed "
Anton Blanchard8a68bbe2007-08-29 08:36:22 -0500329 "for FMR %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 kfree(fmr);
331 goto out_fail;
332 }
333
334 list_add_tail(&fmr->list, &pool->free_list);
335 ++pool->pool_size;
336 }
337 }
338
339 return pool;
340
341 out_free_pool:
342 kfree(pool->cache_bucket);
343 kfree(pool);
344
345 return ERR_PTR(ret);
346
347 out_fail:
348 ib_destroy_fmr_pool(pool);
349
350 return ERR_PTR(-ENOMEM);
351}
352EXPORT_SYMBOL(ib_create_fmr_pool);
353
354/**
355 * ib_destroy_fmr_pool - Free FMR pool
356 * @pool:FMR pool to free
357 *
358 * Destroy an FMR pool and free all associated resources.
359 */
Hal Rosenstockc183a4c2005-07-27 11:45:20 -0700360void ib_destroy_fmr_pool(struct ib_fmr_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 struct ib_pool_fmr *fmr;
363 struct ib_pool_fmr *tmp;
Roland Dreierffbf4c32005-08-15 07:35:16 -0700364 LIST_HEAD(fmr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int i;
366
367 kthread_stop(pool->thread);
368 ib_fmr_batch_release(pool);
369
370 i = 0;
371 list_for_each_entry_safe(fmr, tmp, &pool->free_list, list) {
Pete Wyckoff35fb5342008-02-26 13:27:31 -0500372 if (fmr->remap_count) {
373 INIT_LIST_HEAD(&fmr_list);
374 list_add_tail(&fmr->fmr->list, &fmr_list);
375 ib_unmap_fmr(&fmr_list);
376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 ib_dealloc_fmr(fmr->fmr);
378 list_del(&fmr->list);
379 kfree(fmr);
380 ++i;
381 }
382
383 if (i < pool->pool_size)
Anton Blanchard8a68bbe2007-08-29 08:36:22 -0500384 printk(KERN_WARNING PFX "pool still has %d regions registered\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 pool->pool_size - i);
386
387 kfree(pool->cache_bucket);
388 kfree(pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390EXPORT_SYMBOL(ib_destroy_fmr_pool);
391
392/**
393 * ib_flush_fmr_pool - Invalidate all unmapped FMRs
394 * @pool:FMR pool to flush
395 *
396 * Ensure that all unmapped FMRs are fully invalidated.
397 */
398int ib_flush_fmr_pool(struct ib_fmr_pool *pool)
399{
Pete Wyckoff33155292008-02-26 13:27:53 -0500400 int serial;
401 struct ib_pool_fmr *fmr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Pete Wyckoff33155292008-02-26 13:27:53 -0500403 /*
404 * The free_list holds FMRs that may have been used
405 * but have not been remapped enough times to be dirty.
406 * Put them on the dirty list now so that the cleanup
407 * thread will reap them too.
408 */
409 spin_lock_irq(&pool->pool_lock);
410 list_for_each_entry_safe(fmr, next, &pool->free_list, list) {
411 if (fmr->remap_count > 0)
412 list_move(&fmr->list, &pool->dirty_list);
413 }
414 spin_unlock_irq(&pool->pool_lock);
415
416 serial = atomic_inc_return(&pool->req_ser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 wake_up_process(pool->thread);
418
419 if (wait_event_interruptible(pool->force_wait,
Roland Dreierf47e22c2006-12-12 11:50:19 -0800420 atomic_read(&pool->flush_ser) - serial >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return -EINTR;
422
423 return 0;
424}
425EXPORT_SYMBOL(ib_flush_fmr_pool);
426
427/**
428 * ib_fmr_pool_map_phys -
429 * @pool:FMR pool to allocate FMR from
430 * @page_list:List of pages to map
431 * @list_len:Number of pages in @page_list
432 * @io_virtual_address:I/O virtual address for new FMR
433 *
434 * Map an FMR from an FMR pool.
435 */
436struct ib_pool_fmr *ib_fmr_pool_map_phys(struct ib_fmr_pool *pool_handle,
437 u64 *page_list,
438 int list_len,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700439 u64 io_virtual_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 struct ib_fmr_pool *pool = pool_handle;
442 struct ib_pool_fmr *fmr;
443 unsigned long flags;
444 int result;
445
446 if (list_len < 1 || list_len > pool->max_pages)
447 return ERR_PTR(-EINVAL);
448
449 spin_lock_irqsave(&pool->pool_lock, flags);
450 fmr = ib_fmr_cache_lookup(pool,
451 page_list,
452 list_len,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700453 io_virtual_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (fmr) {
455 /* found in cache */
456 ++fmr->ref_count;
457 if (fmr->ref_count == 1) {
458 list_del(&fmr->list);
459 }
460
461 spin_unlock_irqrestore(&pool->pool_lock, flags);
462
463 return fmr;
464 }
465
466 if (list_empty(&pool->free_list)) {
467 spin_unlock_irqrestore(&pool->pool_lock, flags);
468 return ERR_PTR(-EAGAIN);
469 }
470
471 fmr = list_entry(pool->free_list.next, struct ib_pool_fmr, list);
472 list_del(&fmr->list);
473 hlist_del_init(&fmr->cache_node);
474 spin_unlock_irqrestore(&pool->pool_lock, flags);
475
476 result = ib_map_phys_fmr(fmr->fmr, page_list, list_len,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700477 io_virtual_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 if (result) {
480 spin_lock_irqsave(&pool->pool_lock, flags);
481 list_add(&fmr->list, &pool->free_list);
482 spin_unlock_irqrestore(&pool->pool_lock, flags);
483
Roland Dreier1a70a052007-05-06 21:18:11 -0700484 printk(KERN_WARNING PFX "fmr_map returns %d\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 return ERR_PTR(result);
487 }
488
489 ++fmr->remap_count;
490 fmr->ref_count = 1;
491
492 if (pool->cache_bucket) {
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700493 fmr->io_virtual_address = io_virtual_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 fmr->page_list_len = list_len;
495 memcpy(fmr->page_list, page_list, list_len * sizeof(*page_list));
496
497 spin_lock_irqsave(&pool->pool_lock, flags);
498 hlist_add_head(&fmr->cache_node,
499 pool->cache_bucket + ib_fmr_hash(fmr->page_list[0]));
500 spin_unlock_irqrestore(&pool->pool_lock, flags);
501 }
502
503 return fmr;
504}
505EXPORT_SYMBOL(ib_fmr_pool_map_phys);
506
507/**
508 * ib_fmr_pool_unmap - Unmap FMR
509 * @fmr:FMR to unmap
510 *
511 * Unmap an FMR. The FMR mapping may remain valid until the FMR is
512 * reused (or until ib_flush_fmr_pool() is called).
513 */
514int ib_fmr_pool_unmap(struct ib_pool_fmr *fmr)
515{
516 struct ib_fmr_pool *pool;
517 unsigned long flags;
518
519 pool = fmr->pool;
520
521 spin_lock_irqsave(&pool->pool_lock, flags);
522
523 --fmr->ref_count;
524 if (!fmr->ref_count) {
Or Gerlitz6c8c1aa2006-06-17 20:37:37 -0700525 if (fmr->remap_count < pool->max_remaps) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 list_add_tail(&fmr->list, &pool->free_list);
527 } else {
528 list_add_tail(&fmr->list, &pool->dirty_list);
Olaf Kircha656eb72008-01-16 18:36:27 +0100529 if (++pool->dirty_len >= pool->dirty_watermark) {
530 atomic_inc(&pool->req_ser);
531 wake_up_process(pool->thread);
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534 }
535
536#ifdef DEBUG
537 if (fmr->ref_count < 0)
Anton Blanchard8a68bbe2007-08-29 08:36:22 -0500538 printk(KERN_WARNING PFX "FMR %p has ref count %d < 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 fmr, fmr->ref_count);
540#endif
541
542 spin_unlock_irqrestore(&pool->pool_lock, flags);
543
544 return 0;
545}
546EXPORT_SYMBOL(ib_fmr_pool_unmap);