blob: 3efdddc39d49ba64fe894d815f198343fb8526f6 [file] [log] [blame]
Andy Grover08b48a12009-02-24 15:30:32 +00001/*
2 * Copyright (c) 2006 Oracle. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Chris Mason764f2dd2010-04-22 21:59:15 -040035#include <linux/rculist.h>
Andy Grover08b48a12009-02-24 15:30:32 +000036
37#include "rds.h"
Andy Grover08b48a12009-02-24 15:30:32 +000038#include "ib.h"
Chris Mason6fa70da2010-06-11 11:17:59 -070039#include "xlist.h"
Andy Grover08b48a12009-02-24 15:30:32 +000040
Chris Mason6fa70da2010-06-11 11:17:59 -070041static DEFINE_PER_CPU(unsigned long, clean_list_grace);
42#define CLEAN_LIST_BUSY_BIT 0
Andy Grover08b48a12009-02-24 15:30:32 +000043
44/*
45 * This is stored as mr->r_trans_private.
46 */
47struct rds_ib_mr {
48 struct rds_ib_device *device;
49 struct rds_ib_mr_pool *pool;
50 struct ib_fmr *fmr;
Chris Mason6fa70da2010-06-11 11:17:59 -070051
52 struct xlist_head xlist;
53
54 /* unmap_list is for freeing */
55 struct list_head unmap_list;
Andy Grover08b48a12009-02-24 15:30:32 +000056 unsigned int remap_count;
57
58 struct scatterlist *sg;
59 unsigned int sg_len;
60 u64 *dma;
61 int sg_dma_len;
62};
63
64/*
65 * Our own little FMR pool
66 */
67struct rds_ib_mr_pool {
68 struct mutex flush_lock; /* serialize fmr invalidate */
Chris Mason7a0ff5d2010-06-11 11:26:02 -070069 struct delayed_work flush_worker; /* flush worker */
Andy Grover08b48a12009-02-24 15:30:32 +000070
Andy Grover08b48a12009-02-24 15:30:32 +000071 atomic_t item_count; /* total # of MRs */
72 atomic_t dirty_count; /* # dirty of MRs */
Chris Mason6fa70da2010-06-11 11:17:59 -070073
74 struct xlist_head drop_list; /* MRs that have reached their max_maps limit */
75 struct xlist_head free_list; /* unused MRs */
76 struct xlist_head clean_list; /* global unused & unamapped MRs */
77 wait_queue_head_t flush_wait;
78
Andy Grover08b48a12009-02-24 15:30:32 +000079 atomic_t free_pinned; /* memory pinned by free MRs */
80 unsigned long max_items;
81 unsigned long max_items_soft;
82 unsigned long max_free_pinned;
83 struct ib_fmr_attr fmr_attr;
84};
85
Chris Mason6fa70da2010-06-11 11:17:59 -070086static int rds_ib_flush_mr_pool(struct rds_ib_mr_pool *pool, int free_all, struct rds_ib_mr **);
Andy Grover08b48a12009-02-24 15:30:32 +000087static void rds_ib_teardown_mr(struct rds_ib_mr *ibmr);
88static void rds_ib_mr_pool_flush_worker(struct work_struct *work);
89
90static struct rds_ib_device *rds_ib_get_device(__be32 ipaddr)
91{
92 struct rds_ib_device *rds_ibdev;
93 struct rds_ib_ipaddr *i_ipaddr;
94
95 list_for_each_entry(rds_ibdev, &rds_ib_devices, list) {
Chris Mason764f2dd2010-04-22 21:59:15 -040096 rcu_read_lock();
97 list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) {
Andy Grover08b48a12009-02-24 15:30:32 +000098 if (i_ipaddr->ipaddr == ipaddr) {
Zach Brown3e0249f2010-05-18 15:48:51 -070099 atomic_inc(&rds_ibdev->refcount);
Chris Mason764f2dd2010-04-22 21:59:15 -0400100 rcu_read_unlock();
Andy Grover08b48a12009-02-24 15:30:32 +0000101 return rds_ibdev;
102 }
103 }
Chris Mason764f2dd2010-04-22 21:59:15 -0400104 rcu_read_unlock();
Andy Grover08b48a12009-02-24 15:30:32 +0000105 }
106
107 return NULL;
108}
109
110static int rds_ib_add_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
111{
112 struct rds_ib_ipaddr *i_ipaddr;
113
114 i_ipaddr = kmalloc(sizeof *i_ipaddr, GFP_KERNEL);
115 if (!i_ipaddr)
116 return -ENOMEM;
117
118 i_ipaddr->ipaddr = ipaddr;
119
120 spin_lock_irq(&rds_ibdev->spinlock);
Chris Mason764f2dd2010-04-22 21:59:15 -0400121 list_add_tail_rcu(&i_ipaddr->list, &rds_ibdev->ipaddr_list);
Andy Grover08b48a12009-02-24 15:30:32 +0000122 spin_unlock_irq(&rds_ibdev->spinlock);
123
124 return 0;
125}
126
127static void rds_ib_remove_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
128{
Andy Grover4a818022010-04-23 11:04:21 -0700129 struct rds_ib_ipaddr *i_ipaddr;
Chris Mason764f2dd2010-04-22 21:59:15 -0400130 struct rds_ib_ipaddr *to_free = NULL;
131
Andy Grover08b48a12009-02-24 15:30:32 +0000132
133 spin_lock_irq(&rds_ibdev->spinlock);
Chris Mason764f2dd2010-04-22 21:59:15 -0400134 list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) {
Andy Grover08b48a12009-02-24 15:30:32 +0000135 if (i_ipaddr->ipaddr == ipaddr) {
Chris Mason764f2dd2010-04-22 21:59:15 -0400136 list_del_rcu(&i_ipaddr->list);
137 to_free = i_ipaddr;
Andy Grover08b48a12009-02-24 15:30:32 +0000138 break;
139 }
140 }
141 spin_unlock_irq(&rds_ibdev->spinlock);
Chris Mason764f2dd2010-04-22 21:59:15 -0400142
143 if (to_free) {
144 synchronize_rcu();
145 kfree(to_free);
146 }
Andy Grover08b48a12009-02-24 15:30:32 +0000147}
148
149int rds_ib_update_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
150{
151 struct rds_ib_device *rds_ibdev_old;
152
153 rds_ibdev_old = rds_ib_get_device(ipaddr);
Zach Brown3e0249f2010-05-18 15:48:51 -0700154 if (rds_ibdev_old) {
Andy Grover08b48a12009-02-24 15:30:32 +0000155 rds_ib_remove_ipaddr(rds_ibdev_old, ipaddr);
Zach Brown3e0249f2010-05-18 15:48:51 -0700156 rds_ib_dev_put(rds_ibdev_old);
157 }
Andy Grover08b48a12009-02-24 15:30:32 +0000158
159 return rds_ib_add_ipaddr(rds_ibdev, ipaddr);
160}
161
Andy Grover745cbcc2009-04-01 08:20:19 +0000162void rds_ib_add_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn)
Andy Grover08b48a12009-02-24 15:30:32 +0000163{
164 struct rds_ib_connection *ic = conn->c_transport_data;
165
166 /* conn was previously on the nodev_conns_list */
167 spin_lock_irq(&ib_nodev_conns_lock);
168 BUG_ON(list_empty(&ib_nodev_conns));
169 BUG_ON(list_empty(&ic->ib_node));
170 list_del(&ic->ib_node);
Andy Grover08b48a12009-02-24 15:30:32 +0000171
172 spin_lock_irq(&rds_ibdev->spinlock);
173 list_add_tail(&ic->ib_node, &rds_ibdev->conn_list);
174 spin_unlock_irq(&rds_ibdev->spinlock);
Andy Grover08b48a12009-02-24 15:30:32 +0000175 spin_unlock_irq(&ib_nodev_conns_lock);
176
Andy Grover745cbcc2009-04-01 08:20:19 +0000177 ic->rds_ibdev = rds_ibdev;
Zach Brown3e0249f2010-05-18 15:48:51 -0700178 atomic_inc(&rds_ibdev->refcount);
Andy Grover08b48a12009-02-24 15:30:32 +0000179}
180
Andy Grover745cbcc2009-04-01 08:20:19 +0000181void rds_ib_remove_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn)
182{
183 struct rds_ib_connection *ic = conn->c_transport_data;
184
185 /* place conn on nodev_conns_list */
186 spin_lock(&ib_nodev_conns_lock);
187
188 spin_lock_irq(&rds_ibdev->spinlock);
189 BUG_ON(list_empty(&ic->ib_node));
190 list_del(&ic->ib_node);
191 spin_unlock_irq(&rds_ibdev->spinlock);
192
193 list_add_tail(&ic->ib_node, &ib_nodev_conns);
194
195 spin_unlock(&ib_nodev_conns_lock);
196
197 ic->rds_ibdev = NULL;
Zach Brown3e0249f2010-05-18 15:48:51 -0700198 rds_ib_dev_put(rds_ibdev);
Andy Grover745cbcc2009-04-01 08:20:19 +0000199}
200
Zach Brown8aeb1ba2010-06-25 14:58:16 -0700201void rds_ib_destroy_nodev_conns(void)
Andy Grover08b48a12009-02-24 15:30:32 +0000202{
203 struct rds_ib_connection *ic, *_ic;
204 LIST_HEAD(tmp_list);
205
206 /* avoid calling conn_destroy with irqs off */
Zach Brown8aeb1ba2010-06-25 14:58:16 -0700207 spin_lock_irq(&ib_nodev_conns_lock);
208 list_splice(&ib_nodev_conns, &tmp_list);
209 spin_unlock_irq(&ib_nodev_conns_lock);
Andy Grover08b48a12009-02-24 15:30:32 +0000210
Andy Grover433d3082009-10-30 08:51:55 +0000211 list_for_each_entry_safe(ic, _ic, &tmp_list, ib_node)
Andy Grover08b48a12009-02-24 15:30:32 +0000212 rds_conn_destroy(ic->conn);
Andy Grover08b48a12009-02-24 15:30:32 +0000213}
214
215struct rds_ib_mr_pool *rds_ib_create_mr_pool(struct rds_ib_device *rds_ibdev)
216{
217 struct rds_ib_mr_pool *pool;
218
219 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
220 if (!pool)
221 return ERR_PTR(-ENOMEM);
222
Chris Mason6fa70da2010-06-11 11:17:59 -0700223 INIT_XLIST_HEAD(&pool->free_list);
224 INIT_XLIST_HEAD(&pool->drop_list);
225 INIT_XLIST_HEAD(&pool->clean_list);
Andy Grover08b48a12009-02-24 15:30:32 +0000226 mutex_init(&pool->flush_lock);
Chris Mason6fa70da2010-06-11 11:17:59 -0700227 init_waitqueue_head(&pool->flush_wait);
Chris Mason7a0ff5d2010-06-11 11:26:02 -0700228 INIT_DELAYED_WORK(&pool->flush_worker, rds_ib_mr_pool_flush_worker);
Andy Grover08b48a12009-02-24 15:30:32 +0000229
230 pool->fmr_attr.max_pages = fmr_message_size;
231 pool->fmr_attr.max_maps = rds_ibdev->fmr_max_remaps;
Andy Grovera870d622009-07-17 13:13:33 +0000232 pool->fmr_attr.page_shift = PAGE_SHIFT;
Andy Grover08b48a12009-02-24 15:30:32 +0000233 pool->max_free_pinned = rds_ibdev->max_fmrs * fmr_message_size / 4;
234
235 /* We never allow more than max_items MRs to be allocated.
236 * When we exceed more than max_items_soft, we start freeing
237 * items more aggressively.
238 * Make sure that max_items > max_items_soft > max_items / 2
239 */
240 pool->max_items_soft = rds_ibdev->max_fmrs * 3 / 4;
241 pool->max_items = rds_ibdev->max_fmrs;
242
243 return pool;
244}
245
246void rds_ib_get_mr_info(struct rds_ib_device *rds_ibdev, struct rds_info_rdma_connection *iinfo)
247{
248 struct rds_ib_mr_pool *pool = rds_ibdev->mr_pool;
249
250 iinfo->rdma_mr_max = pool->max_items;
251 iinfo->rdma_mr_size = pool->fmr_attr.max_pages;
252}
253
254void rds_ib_destroy_mr_pool(struct rds_ib_mr_pool *pool)
255{
Chris Mason7a0ff5d2010-06-11 11:26:02 -0700256 cancel_delayed_work_sync(&pool->flush_worker);
Chris Mason6fa70da2010-06-11 11:17:59 -0700257 rds_ib_flush_mr_pool(pool, 1, NULL);
Andy Grover571c02f2010-03-11 13:50:01 +0000258 WARN_ON(atomic_read(&pool->item_count));
259 WARN_ON(atomic_read(&pool->free_pinned));
Andy Grover08b48a12009-02-24 15:30:32 +0000260 kfree(pool);
261}
262
Chris Mason6fa70da2010-06-11 11:17:59 -0700263static void refill_local(struct rds_ib_mr_pool *pool, struct xlist_head *xl,
264 struct rds_ib_mr **ibmr_ret)
265{
266 struct xlist_head *ibmr_xl;
267 ibmr_xl = xlist_del_head_fast(xl);
268 *ibmr_ret = list_entry(ibmr_xl, struct rds_ib_mr, xlist);
269}
270
Andy Grover08b48a12009-02-24 15:30:32 +0000271static inline struct rds_ib_mr *rds_ib_reuse_fmr(struct rds_ib_mr_pool *pool)
272{
273 struct rds_ib_mr *ibmr = NULL;
Chris Mason6fa70da2010-06-11 11:17:59 -0700274 struct xlist_head *ret;
275 unsigned long *flag;
Andy Grover08b48a12009-02-24 15:30:32 +0000276
Chris Mason6fa70da2010-06-11 11:17:59 -0700277 preempt_disable();
278 flag = &__get_cpu_var(clean_list_grace);
279 set_bit(CLEAN_LIST_BUSY_BIT, flag);
280 ret = xlist_del_head(&pool->clean_list);
281 if (ret)
282 ibmr = list_entry(ret, struct rds_ib_mr, xlist);
Andy Grover08b48a12009-02-24 15:30:32 +0000283
Chris Mason6fa70da2010-06-11 11:17:59 -0700284 clear_bit(CLEAN_LIST_BUSY_BIT, flag);
285 preempt_enable();
Andy Grover08b48a12009-02-24 15:30:32 +0000286 return ibmr;
287}
288
Chris Mason6fa70da2010-06-11 11:17:59 -0700289static inline void wait_clean_list_grace(void)
290{
291 int cpu;
292 unsigned long *flag;
293
294 for_each_online_cpu(cpu) {
295 flag = &per_cpu(clean_list_grace, cpu);
296 while (test_bit(CLEAN_LIST_BUSY_BIT, flag))
297 cpu_relax();
298 }
299}
300
Andy Grover08b48a12009-02-24 15:30:32 +0000301static struct rds_ib_mr *rds_ib_alloc_fmr(struct rds_ib_device *rds_ibdev)
302{
303 struct rds_ib_mr_pool *pool = rds_ibdev->mr_pool;
304 struct rds_ib_mr *ibmr = NULL;
305 int err = 0, iter = 0;
306
307 while (1) {
308 ibmr = rds_ib_reuse_fmr(pool);
309 if (ibmr)
310 return ibmr;
311
312 /* No clean MRs - now we have the choice of either
313 * allocating a fresh MR up to the limit imposed by the
314 * driver, or flush any dirty unused MRs.
315 * We try to avoid stalling in the send path if possible,
316 * so we allocate as long as we're allowed to.
317 *
318 * We're fussy with enforcing the FMR limit, though. If the driver
319 * tells us we can't use more than N fmrs, we shouldn't start
320 * arguing with it */
321 if (atomic_inc_return(&pool->item_count) <= pool->max_items)
322 break;
323
324 atomic_dec(&pool->item_count);
325
326 if (++iter > 2) {
327 rds_ib_stats_inc(s_ib_rdma_mr_pool_depleted);
328 return ERR_PTR(-EAGAIN);
329 }
330
331 /* We do have some empty MRs. Flush them out. */
332 rds_ib_stats_inc(s_ib_rdma_mr_pool_wait);
Chris Mason6fa70da2010-06-11 11:17:59 -0700333 rds_ib_flush_mr_pool(pool, 0, &ibmr);
334 if (ibmr)
335 return ibmr;
Andy Grover08b48a12009-02-24 15:30:32 +0000336 }
337
Andy Grovere4c52c92010-04-23 10:49:53 -0700338 ibmr = kzalloc_node(sizeof(*ibmr), GFP_KERNEL, rdsibdev_to_node(rds_ibdev));
Andy Grover08b48a12009-02-24 15:30:32 +0000339 if (!ibmr) {
340 err = -ENOMEM;
341 goto out_no_cigar;
342 }
343
Chris Mason38a4e5e2010-05-11 15:09:45 -0700344 memset(ibmr, 0, sizeof(*ibmr));
345
Andy Grover08b48a12009-02-24 15:30:32 +0000346 ibmr->fmr = ib_alloc_fmr(rds_ibdev->pd,
347 (IB_ACCESS_LOCAL_WRITE |
348 IB_ACCESS_REMOTE_READ |
Andy Grover15133f62010-01-12 14:33:38 -0800349 IB_ACCESS_REMOTE_WRITE|
350 IB_ACCESS_REMOTE_ATOMIC),
Andy Grover08b48a12009-02-24 15:30:32 +0000351 &pool->fmr_attr);
352 if (IS_ERR(ibmr->fmr)) {
353 err = PTR_ERR(ibmr->fmr);
354 ibmr->fmr = NULL;
355 printk(KERN_WARNING "RDS/IB: ib_alloc_fmr failed (err=%d)\n", err);
356 goto out_no_cigar;
357 }
358
359 rds_ib_stats_inc(s_ib_rdma_mr_alloc);
360 return ibmr;
361
362out_no_cigar:
363 if (ibmr) {
364 if (ibmr->fmr)
365 ib_dealloc_fmr(ibmr->fmr);
366 kfree(ibmr);
367 }
368 atomic_dec(&pool->item_count);
369 return ERR_PTR(err);
370}
371
372static int rds_ib_map_fmr(struct rds_ib_device *rds_ibdev, struct rds_ib_mr *ibmr,
373 struct scatterlist *sg, unsigned int nents)
374{
375 struct ib_device *dev = rds_ibdev->dev;
376 struct scatterlist *scat = sg;
377 u64 io_addr = 0;
378 u64 *dma_pages;
379 u32 len;
380 int page_cnt, sg_dma_len;
381 int i, j;
382 int ret;
383
384 sg_dma_len = ib_dma_map_sg(dev, sg, nents,
385 DMA_BIDIRECTIONAL);
386 if (unlikely(!sg_dma_len)) {
387 printk(KERN_WARNING "RDS/IB: dma_map_sg failed!\n");
388 return -EBUSY;
389 }
390
391 len = 0;
392 page_cnt = 0;
393
394 for (i = 0; i < sg_dma_len; ++i) {
395 unsigned int dma_len = ib_sg_dma_len(dev, &scat[i]);
396 u64 dma_addr = ib_sg_dma_address(dev, &scat[i]);
397
Andy Grovera870d622009-07-17 13:13:33 +0000398 if (dma_addr & ~PAGE_MASK) {
Andy Grover08b48a12009-02-24 15:30:32 +0000399 if (i > 0)
400 return -EINVAL;
401 else
402 ++page_cnt;
403 }
Andy Grovera870d622009-07-17 13:13:33 +0000404 if ((dma_addr + dma_len) & ~PAGE_MASK) {
Andy Grover08b48a12009-02-24 15:30:32 +0000405 if (i < sg_dma_len - 1)
406 return -EINVAL;
407 else
408 ++page_cnt;
409 }
410
411 len += dma_len;
412 }
413
Andy Grovera870d622009-07-17 13:13:33 +0000414 page_cnt += len >> PAGE_SHIFT;
Andy Grover08b48a12009-02-24 15:30:32 +0000415 if (page_cnt > fmr_message_size)
416 return -EINVAL;
417
Andy Grovere4c52c92010-04-23 10:49:53 -0700418 dma_pages = kmalloc_node(sizeof(u64) * page_cnt, GFP_ATOMIC,
419 rdsibdev_to_node(rds_ibdev));
Andy Grover08b48a12009-02-24 15:30:32 +0000420 if (!dma_pages)
421 return -ENOMEM;
422
423 page_cnt = 0;
424 for (i = 0; i < sg_dma_len; ++i) {
425 unsigned int dma_len = ib_sg_dma_len(dev, &scat[i]);
426 u64 dma_addr = ib_sg_dma_address(dev, &scat[i]);
427
Andy Grovera870d622009-07-17 13:13:33 +0000428 for (j = 0; j < dma_len; j += PAGE_SIZE)
Andy Grover08b48a12009-02-24 15:30:32 +0000429 dma_pages[page_cnt++] =
Andy Grovera870d622009-07-17 13:13:33 +0000430 (dma_addr & PAGE_MASK) + j;
Andy Grover08b48a12009-02-24 15:30:32 +0000431 }
432
433 ret = ib_map_phys_fmr(ibmr->fmr,
434 dma_pages, page_cnt, io_addr);
435 if (ret)
436 goto out;
437
438 /* Success - we successfully remapped the MR, so we can
439 * safely tear down the old mapping. */
440 rds_ib_teardown_mr(ibmr);
441
442 ibmr->sg = scat;
443 ibmr->sg_len = nents;
444 ibmr->sg_dma_len = sg_dma_len;
445 ibmr->remap_count++;
446
447 rds_ib_stats_inc(s_ib_rdma_mr_used);
448 ret = 0;
449
450out:
451 kfree(dma_pages);
452
453 return ret;
454}
455
456void rds_ib_sync_mr(void *trans_private, int direction)
457{
458 struct rds_ib_mr *ibmr = trans_private;
459 struct rds_ib_device *rds_ibdev = ibmr->device;
460
461 switch (direction) {
462 case DMA_FROM_DEVICE:
463 ib_dma_sync_sg_for_cpu(rds_ibdev->dev, ibmr->sg,
464 ibmr->sg_dma_len, DMA_BIDIRECTIONAL);
465 break;
466 case DMA_TO_DEVICE:
467 ib_dma_sync_sg_for_device(rds_ibdev->dev, ibmr->sg,
468 ibmr->sg_dma_len, DMA_BIDIRECTIONAL);
469 break;
470 }
471}
472
473static void __rds_ib_teardown_mr(struct rds_ib_mr *ibmr)
474{
475 struct rds_ib_device *rds_ibdev = ibmr->device;
476
477 if (ibmr->sg_dma_len) {
478 ib_dma_unmap_sg(rds_ibdev->dev,
479 ibmr->sg, ibmr->sg_len,
480 DMA_BIDIRECTIONAL);
481 ibmr->sg_dma_len = 0;
482 }
483
484 /* Release the s/g list */
485 if (ibmr->sg_len) {
486 unsigned int i;
487
488 for (i = 0; i < ibmr->sg_len; ++i) {
489 struct page *page = sg_page(&ibmr->sg[i]);
490
491 /* FIXME we need a way to tell a r/w MR
492 * from a r/o MR */
Andy Grover9e2effb2010-03-12 16:22:32 -0800493 BUG_ON(irqs_disabled());
Andy Grover08b48a12009-02-24 15:30:32 +0000494 set_page_dirty(page);
495 put_page(page);
496 }
497 kfree(ibmr->sg);
498
499 ibmr->sg = NULL;
500 ibmr->sg_len = 0;
501 }
502}
503
504static void rds_ib_teardown_mr(struct rds_ib_mr *ibmr)
505{
506 unsigned int pinned = ibmr->sg_len;
507
508 __rds_ib_teardown_mr(ibmr);
509 if (pinned) {
510 struct rds_ib_device *rds_ibdev = ibmr->device;
511 struct rds_ib_mr_pool *pool = rds_ibdev->mr_pool;
512
513 atomic_sub(pinned, &pool->free_pinned);
514 }
515}
516
517static inline unsigned int rds_ib_flush_goal(struct rds_ib_mr_pool *pool, int free_all)
518{
519 unsigned int item_count;
520
521 item_count = atomic_read(&pool->item_count);
522 if (free_all)
523 return item_count;
524
525 return 0;
526}
527
528/*
Chris Mason6fa70da2010-06-11 11:17:59 -0700529 * given an xlist of mrs, put them all into the list_head for more processing
530 */
531static void xlist_append_to_list(struct xlist_head *xlist, struct list_head *list)
532{
533 struct rds_ib_mr *ibmr;
534 struct xlist_head splice;
535 struct xlist_head *cur;
536 struct xlist_head *next;
537
538 splice.next = NULL;
539 xlist_splice(xlist, &splice);
540 cur = splice.next;
541 while (cur) {
542 next = cur->next;
543 ibmr = list_entry(cur, struct rds_ib_mr, xlist);
544 list_add_tail(&ibmr->unmap_list, list);
545 cur = next;
546 }
547}
548
549/*
550 * this takes a list head of mrs and turns it into an xlist of clusters.
551 * each cluster has an xlist of MR_CLUSTER_SIZE mrs that are ready for
552 * reuse.
553 */
554static void list_append_to_xlist(struct rds_ib_mr_pool *pool,
555 struct list_head *list, struct xlist_head *xlist,
556 struct xlist_head **tail_ret)
557{
558 struct rds_ib_mr *ibmr;
559 struct xlist_head *cur_mr = xlist;
560 struct xlist_head *tail_mr = NULL;
561
562 list_for_each_entry(ibmr, list, unmap_list) {
563 tail_mr = &ibmr->xlist;
564 tail_mr->next = NULL;
565 cur_mr->next = tail_mr;
566 cur_mr = tail_mr;
567 }
568 *tail_ret = tail_mr;
569}
570
571/*
Andy Grover08b48a12009-02-24 15:30:32 +0000572 * Flush our pool of MRs.
573 * At a minimum, all currently unused MRs are unmapped.
574 * If the number of MRs allocated exceeds the limit, we also try
575 * to free as many MRs as needed to get back to this limit.
576 */
Chris Mason6fa70da2010-06-11 11:17:59 -0700577static int rds_ib_flush_mr_pool(struct rds_ib_mr_pool *pool,
578 int free_all, struct rds_ib_mr **ibmr_ret)
Andy Grover08b48a12009-02-24 15:30:32 +0000579{
580 struct rds_ib_mr *ibmr, *next;
Chris Mason6fa70da2010-06-11 11:17:59 -0700581 struct xlist_head clean_xlist;
582 struct xlist_head *clean_tail;
Andy Grover08b48a12009-02-24 15:30:32 +0000583 LIST_HEAD(unmap_list);
584 LIST_HEAD(fmr_list);
585 unsigned long unpinned = 0;
Andy Grover08b48a12009-02-24 15:30:32 +0000586 unsigned int nfreed = 0, ncleaned = 0, free_goal;
587 int ret = 0;
588
589 rds_ib_stats_inc(s_ib_rdma_mr_pool_flush);
590
Chris Mason6fa70da2010-06-11 11:17:59 -0700591 if (ibmr_ret) {
592 DEFINE_WAIT(wait);
593 while(!mutex_trylock(&pool->flush_lock)) {
594 ibmr = rds_ib_reuse_fmr(pool);
595 if (ibmr) {
596 *ibmr_ret = ibmr;
597 finish_wait(&pool->flush_wait, &wait);
598 goto out_nolock;
599 }
Andy Grover08b48a12009-02-24 15:30:32 +0000600
Chris Mason6fa70da2010-06-11 11:17:59 -0700601 prepare_to_wait(&pool->flush_wait, &wait,
602 TASK_UNINTERRUPTIBLE);
603 if (xlist_empty(&pool->clean_list))
604 schedule();
605
606 ibmr = rds_ib_reuse_fmr(pool);
607 if (ibmr) {
608 *ibmr_ret = ibmr;
609 finish_wait(&pool->flush_wait, &wait);
610 goto out_nolock;
611 }
612 }
613 finish_wait(&pool->flush_wait, &wait);
614 } else
615 mutex_lock(&pool->flush_lock);
616
617 if (ibmr_ret) {
618 ibmr = rds_ib_reuse_fmr(pool);
619 if (ibmr) {
620 *ibmr_ret = ibmr;
621 goto out;
622 }
623 }
624
Andy Grover08b48a12009-02-24 15:30:32 +0000625 /* Get the list of all MRs to be dropped. Ordering matters -
Chris Mason6fa70da2010-06-11 11:17:59 -0700626 * we want to put drop_list ahead of free_list.
627 */
628 xlist_append_to_list(&pool->drop_list, &unmap_list);
629 xlist_append_to_list(&pool->free_list, &unmap_list);
Andy Grover08b48a12009-02-24 15:30:32 +0000630 if (free_all)
Chris Mason6fa70da2010-06-11 11:17:59 -0700631 xlist_append_to_list(&pool->clean_list, &unmap_list);
Andy Grover08b48a12009-02-24 15:30:32 +0000632
633 free_goal = rds_ib_flush_goal(pool, free_all);
634
635 if (list_empty(&unmap_list))
636 goto out;
637
638 /* String all ib_mr's onto one list and hand them to ib_unmap_fmr */
Chris Mason6fa70da2010-06-11 11:17:59 -0700639 list_for_each_entry(ibmr, &unmap_list, unmap_list)
Andy Grover08b48a12009-02-24 15:30:32 +0000640 list_add(&ibmr->fmr->list, &fmr_list);
Chris Mason6fa70da2010-06-11 11:17:59 -0700641
Andy Grover08b48a12009-02-24 15:30:32 +0000642 ret = ib_unmap_fmr(&fmr_list);
643 if (ret)
644 printk(KERN_WARNING "RDS/IB: ib_unmap_fmr failed (err=%d)\n", ret);
645
646 /* Now we can destroy the DMA mapping and unpin any pages */
Chris Mason6fa70da2010-06-11 11:17:59 -0700647 list_for_each_entry_safe(ibmr, next, &unmap_list, unmap_list) {
Andy Grover08b48a12009-02-24 15:30:32 +0000648 unpinned += ibmr->sg_len;
649 __rds_ib_teardown_mr(ibmr);
650 if (nfreed < free_goal || ibmr->remap_count >= pool->fmr_attr.max_maps) {
651 rds_ib_stats_inc(s_ib_rdma_mr_free);
Chris Mason6fa70da2010-06-11 11:17:59 -0700652 list_del(&ibmr->unmap_list);
Andy Grover08b48a12009-02-24 15:30:32 +0000653 ib_dealloc_fmr(ibmr->fmr);
654 kfree(ibmr);
655 nfreed++;
656 }
657 ncleaned++;
658 }
659
Chris Mason6fa70da2010-06-11 11:17:59 -0700660 if (!list_empty(&unmap_list)) {
661 /* we have to make sure that none of the things we're about
662 * to put on the clean list would race with other cpus trying
663 * to pull items off. The xlist would explode if we managed to
664 * remove something from the clean list and then add it back again
665 * while another CPU was spinning on that same item in xlist_del_head.
666 *
667 * This is pretty unlikely, but just in case wait for an xlist grace period
668 * here before adding anything back into the clean list.
669 */
670 wait_clean_list_grace();
671
672 list_append_to_xlist(pool, &unmap_list, &clean_xlist, &clean_tail);
673 if (ibmr_ret)
674 refill_local(pool, &clean_xlist, ibmr_ret);
675
676 /* refill_local may have emptied our list */
677 if (!xlist_empty(&clean_xlist))
678 xlist_add(clean_xlist.next, clean_tail, &pool->clean_list);
679
680 }
Andy Grover08b48a12009-02-24 15:30:32 +0000681
682 atomic_sub(unpinned, &pool->free_pinned);
683 atomic_sub(ncleaned, &pool->dirty_count);
684 atomic_sub(nfreed, &pool->item_count);
685
686out:
687 mutex_unlock(&pool->flush_lock);
Chris Mason6fa70da2010-06-11 11:17:59 -0700688 if (waitqueue_active(&pool->flush_wait))
689 wake_up(&pool->flush_wait);
690out_nolock:
Andy Grover08b48a12009-02-24 15:30:32 +0000691 return ret;
692}
693
Zach Brown515e0792010-07-06 15:09:56 -0700694struct workqueue_struct *rds_ib_fmr_wq;
695
Zach Brownef87b7e2010-07-09 12:26:20 -0700696int rds_ib_fmr_init(void)
Zach Brown515e0792010-07-06 15:09:56 -0700697{
698 rds_ib_fmr_wq = create_workqueue("rds_fmr_flushd");
699 if (!rds_ib_fmr_wq)
700 return -ENOMEM;
701 return 0;
702}
703
704/*
705 * By the time this is called all the IB devices should have been torn down and
706 * had their pools freed. As each pool is freed its work struct is waited on,
707 * so the pool flushing work queue should be idle by the time we get here.
708 */
Zach Brownef87b7e2010-07-09 12:26:20 -0700709void rds_ib_fmr_exit(void)
Zach Brown515e0792010-07-06 15:09:56 -0700710{
711 destroy_workqueue(rds_ib_fmr_wq);
712}
713
Andy Grover08b48a12009-02-24 15:30:32 +0000714static void rds_ib_mr_pool_flush_worker(struct work_struct *work)
715{
Chris Mason7a0ff5d2010-06-11 11:26:02 -0700716 struct rds_ib_mr_pool *pool = container_of(work, struct rds_ib_mr_pool, flush_worker.work);
Andy Grover08b48a12009-02-24 15:30:32 +0000717
Chris Mason6fa70da2010-06-11 11:17:59 -0700718 rds_ib_flush_mr_pool(pool, 0, NULL);
Andy Grover08b48a12009-02-24 15:30:32 +0000719}
720
721void rds_ib_free_mr(void *trans_private, int invalidate)
722{
723 struct rds_ib_mr *ibmr = trans_private;
724 struct rds_ib_device *rds_ibdev = ibmr->device;
725 struct rds_ib_mr_pool *pool = rds_ibdev->mr_pool;
Andy Grover08b48a12009-02-24 15:30:32 +0000726
727 rdsdebug("RDS/IB: free_mr nents %u\n", ibmr->sg_len);
728
729 /* Return it to the pool's free list */
Andy Grover08b48a12009-02-24 15:30:32 +0000730 if (ibmr->remap_count >= pool->fmr_attr.max_maps)
Chris Mason6fa70da2010-06-11 11:17:59 -0700731 xlist_add(&ibmr->xlist, &ibmr->xlist, &pool->drop_list);
Andy Grover08b48a12009-02-24 15:30:32 +0000732 else
Chris Mason6fa70da2010-06-11 11:17:59 -0700733 xlist_add(&ibmr->xlist, &ibmr->xlist, &pool->free_list);
Andy Grover08b48a12009-02-24 15:30:32 +0000734
735 atomic_add(ibmr->sg_len, &pool->free_pinned);
736 atomic_inc(&pool->dirty_count);
Andy Grover08b48a12009-02-24 15:30:32 +0000737
738 /* If we've pinned too many pages, request a flush */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800739 if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned ||
740 atomic_read(&pool->dirty_count) >= pool->max_items / 10)
Zach Brown515e0792010-07-06 15:09:56 -0700741 queue_delayed_work(rds_ib_fmr_wq, &pool->flush_worker, 10);
Andy Grover08b48a12009-02-24 15:30:32 +0000742
743 if (invalidate) {
744 if (likely(!in_interrupt())) {
Chris Mason6fa70da2010-06-11 11:17:59 -0700745 rds_ib_flush_mr_pool(pool, 0, NULL);
Andy Grover08b48a12009-02-24 15:30:32 +0000746 } else {
747 /* We get here if the user created a MR marked
748 * as use_once and invalidate at the same time. */
Zach Brown515e0792010-07-06 15:09:56 -0700749 queue_delayed_work(rds_ib_fmr_wq,
750 &pool->flush_worker, 10);
Andy Grover08b48a12009-02-24 15:30:32 +0000751 }
752 }
Zach Brown3e0249f2010-05-18 15:48:51 -0700753
754 rds_ib_dev_put(rds_ibdev);
Andy Grover08b48a12009-02-24 15:30:32 +0000755}
756
757void rds_ib_flush_mrs(void)
758{
759 struct rds_ib_device *rds_ibdev;
760
761 list_for_each_entry(rds_ibdev, &rds_ib_devices, list) {
762 struct rds_ib_mr_pool *pool = rds_ibdev->mr_pool;
763
764 if (pool)
Chris Mason6fa70da2010-06-11 11:17:59 -0700765 rds_ib_flush_mr_pool(pool, 0, NULL);
Andy Grover08b48a12009-02-24 15:30:32 +0000766 }
767}
768
769void *rds_ib_get_mr(struct scatterlist *sg, unsigned long nents,
770 struct rds_sock *rs, u32 *key_ret)
771{
772 struct rds_ib_device *rds_ibdev;
773 struct rds_ib_mr *ibmr = NULL;
774 int ret;
775
776 rds_ibdev = rds_ib_get_device(rs->rs_bound_addr);
777 if (!rds_ibdev) {
778 ret = -ENODEV;
779 goto out;
780 }
781
782 if (!rds_ibdev->mr_pool) {
783 ret = -ENODEV;
784 goto out;
785 }
786
787 ibmr = rds_ib_alloc_fmr(rds_ibdev);
788 if (IS_ERR(ibmr))
789 return ibmr;
790
791 ret = rds_ib_map_fmr(rds_ibdev, ibmr, sg, nents);
792 if (ret == 0)
793 *key_ret = ibmr->fmr->rkey;
794 else
795 printk(KERN_WARNING "RDS/IB: map_fmr failed (errno=%d)\n", ret);
796
797 ibmr->device = rds_ibdev;
Zach Brown3e0249f2010-05-18 15:48:51 -0700798 rds_ibdev = NULL;
Andy Grover08b48a12009-02-24 15:30:32 +0000799
800 out:
801 if (ret) {
802 if (ibmr)
803 rds_ib_free_mr(ibmr, 0);
804 ibmr = ERR_PTR(ret);
805 }
Zach Brown3e0249f2010-05-18 15:48:51 -0700806 if (rds_ibdev)
807 rds_ib_dev_put(rds_ibdev);
Andy Grover08b48a12009-02-24 15:30:32 +0000808 return ibmr;
809}
Chris Mason6fa70da2010-06-11 11:17:59 -0700810