]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/rds/iw_rdma.c
Merge remote-tracking branch 'bluetooth/master'
[karo-tx-linux.git] / net / rds / iw_rdma.c
1 /*
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>
34 #include <linux/slab.h>
35 #include <linux/ratelimit.h>
36
37 #include "rds.h"
38 #include "iw.h"
39
40
41 /*
42  * This is stored as mr->r_trans_private.
43  */
44 struct rds_iw_mr {
45         struct rds_iw_device    *device;
46         struct rds_iw_mr_pool   *pool;
47         struct rdma_cm_id       *cm_id;
48
49         struct ib_mr    *mr;
50         struct ib_fast_reg_page_list *page_list;
51
52         struct rds_iw_mapping   mapping;
53         unsigned char           remap_count;
54 };
55
56 /*
57  * Our own little MR pool
58  */
59 struct rds_iw_mr_pool {
60         struct rds_iw_device    *device;                /* back ptr to the device that owns us */
61
62         struct mutex            flush_lock;             /* serialize fmr invalidate */
63         struct work_struct      flush_worker;           /* flush worker */
64
65         spinlock_t              list_lock;              /* protect variables below */
66         atomic_t                item_count;             /* total # of MRs */
67         atomic_t                dirty_count;            /* # dirty of MRs */
68         struct list_head        dirty_list;             /* dirty mappings */
69         struct list_head        clean_list;             /* unused & unamapped MRs */
70         atomic_t                free_pinned;            /* memory pinned by free MRs */
71         unsigned long           max_message_size;       /* in pages */
72         unsigned long           max_items;
73         unsigned long           max_items_soft;
74         unsigned long           max_free_pinned;
75         int                     max_pages;
76 };
77
78 static void rds_iw_flush_mr_pool(struct rds_iw_mr_pool *pool, int free_all);
79 static void rds_iw_mr_pool_flush_worker(struct work_struct *work);
80 static int rds_iw_init_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr);
81 static int rds_iw_map_fastreg(struct rds_iw_mr_pool *pool,
82                           struct rds_iw_mr *ibmr,
83                           struct scatterlist *sg, unsigned int nents);
84 static void rds_iw_free_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr);
85 static unsigned int rds_iw_unmap_fastreg_list(struct rds_iw_mr_pool *pool,
86                         struct list_head *unmap_list,
87                         struct list_head *kill_list,
88                         int *unpinned);
89 static void rds_iw_destroy_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr);
90
91 static int rds_iw_get_device(struct sockaddr_in *src, struct sockaddr_in *dst,
92                              struct rds_iw_device **rds_iwdev,
93                              struct rdma_cm_id **cm_id)
94 {
95         struct rds_iw_device *iwdev;
96         struct rds_iw_cm_id *i_cm_id;
97
98         *rds_iwdev = NULL;
99         *cm_id = NULL;
100
101         list_for_each_entry(iwdev, &rds_iw_devices, list) {
102                 spin_lock_irq(&iwdev->spinlock);
103                 list_for_each_entry(i_cm_id, &iwdev->cm_id_list, list) {
104                         struct sockaddr_in *src_addr, *dst_addr;
105
106                         src_addr = (struct sockaddr_in *)&i_cm_id->cm_id->route.addr.src_addr;
107                         dst_addr = (struct sockaddr_in *)&i_cm_id->cm_id->route.addr.dst_addr;
108
109                         rdsdebug("local ipaddr = %x port %d, "
110                                  "remote ipaddr = %x port %d"
111                                  "..looking for %x port %d, "
112                                  "remote ipaddr = %x port %d\n",
113                                 src_addr->sin_addr.s_addr,
114                                 src_addr->sin_port,
115                                 dst_addr->sin_addr.s_addr,
116                                 dst_addr->sin_port,
117                                 src->sin_addr.s_addr,
118                                 src->sin_port,
119                                 dst->sin_addr.s_addr,
120                                 dst->sin_port);
121 #ifdef WORKING_TUPLE_DETECTION
122                         if (src_addr->sin_addr.s_addr == src->sin_addr.s_addr &&
123                             src_addr->sin_port == src->sin_port &&
124                             dst_addr->sin_addr.s_addr == dst->sin_addr.s_addr &&
125                             dst_addr->sin_port == dst->sin_port) {
126 #else
127                         /* FIXME - needs to compare the local and remote
128                          * ipaddr/port tuple, but the ipaddr is the only
129                          * available information in the rds_sock (as the rest are
130                          * zero'ed.  It doesn't appear to be properly populated
131                          * during connection setup...
132                          */
133                         if (src_addr->sin_addr.s_addr == src->sin_addr.s_addr) {
134 #endif
135                                 spin_unlock_irq(&iwdev->spinlock);
136                                 *rds_iwdev = iwdev;
137                                 *cm_id = i_cm_id->cm_id;
138                                 return 0;
139                         }
140                 }
141                 spin_unlock_irq(&iwdev->spinlock);
142         }
143
144         return 1;
145 }
146
147 static int rds_iw_add_cm_id(struct rds_iw_device *rds_iwdev, struct rdma_cm_id *cm_id)
148 {
149         struct rds_iw_cm_id *i_cm_id;
150
151         i_cm_id = kmalloc(sizeof *i_cm_id, GFP_KERNEL);
152         if (!i_cm_id)
153                 return -ENOMEM;
154
155         i_cm_id->cm_id = cm_id;
156
157         spin_lock_irq(&rds_iwdev->spinlock);
158         list_add_tail(&i_cm_id->list, &rds_iwdev->cm_id_list);
159         spin_unlock_irq(&rds_iwdev->spinlock);
160
161         return 0;
162 }
163
164 static void rds_iw_remove_cm_id(struct rds_iw_device *rds_iwdev,
165                                 struct rdma_cm_id *cm_id)
166 {
167         struct rds_iw_cm_id *i_cm_id;
168
169         spin_lock_irq(&rds_iwdev->spinlock);
170         list_for_each_entry(i_cm_id, &rds_iwdev->cm_id_list, list) {
171                 if (i_cm_id->cm_id == cm_id) {
172                         list_del(&i_cm_id->list);
173                         kfree(i_cm_id);
174                         break;
175                 }
176         }
177         spin_unlock_irq(&rds_iwdev->spinlock);
178 }
179
180
181 int rds_iw_update_cm_id(struct rds_iw_device *rds_iwdev, struct rdma_cm_id *cm_id)
182 {
183         struct sockaddr_in *src_addr, *dst_addr;
184         struct rds_iw_device *rds_iwdev_old;
185         struct rdma_cm_id *pcm_id;
186         int rc;
187
188         src_addr = (struct sockaddr_in *)&cm_id->route.addr.src_addr;
189         dst_addr = (struct sockaddr_in *)&cm_id->route.addr.dst_addr;
190
191         rc = rds_iw_get_device(src_addr, dst_addr, &rds_iwdev_old, &pcm_id);
192         if (rc)
193                 rds_iw_remove_cm_id(rds_iwdev, cm_id);
194
195         return rds_iw_add_cm_id(rds_iwdev, cm_id);
196 }
197
198 void rds_iw_add_conn(struct rds_iw_device *rds_iwdev, struct rds_connection *conn)
199 {
200         struct rds_iw_connection *ic = conn->c_transport_data;
201
202         /* conn was previously on the nodev_conns_list */
203         spin_lock_irq(&iw_nodev_conns_lock);
204         BUG_ON(list_empty(&iw_nodev_conns));
205         BUG_ON(list_empty(&ic->iw_node));
206         list_del(&ic->iw_node);
207
208         spin_lock(&rds_iwdev->spinlock);
209         list_add_tail(&ic->iw_node, &rds_iwdev->conn_list);
210         spin_unlock(&rds_iwdev->spinlock);
211         spin_unlock_irq(&iw_nodev_conns_lock);
212
213         ic->rds_iwdev = rds_iwdev;
214 }
215
216 void rds_iw_remove_conn(struct rds_iw_device *rds_iwdev, struct rds_connection *conn)
217 {
218         struct rds_iw_connection *ic = conn->c_transport_data;
219
220         /* place conn on nodev_conns_list */
221         spin_lock(&iw_nodev_conns_lock);
222
223         spin_lock_irq(&rds_iwdev->spinlock);
224         BUG_ON(list_empty(&ic->iw_node));
225         list_del(&ic->iw_node);
226         spin_unlock_irq(&rds_iwdev->spinlock);
227
228         list_add_tail(&ic->iw_node, &iw_nodev_conns);
229
230         spin_unlock(&iw_nodev_conns_lock);
231
232         rds_iw_remove_cm_id(ic->rds_iwdev, ic->i_cm_id);
233         ic->rds_iwdev = NULL;
234 }
235
236 void __rds_iw_destroy_conns(struct list_head *list, spinlock_t *list_lock)
237 {
238         struct rds_iw_connection *ic, *_ic;
239         LIST_HEAD(tmp_list);
240
241         /* avoid calling conn_destroy with irqs off */
242         spin_lock_irq(list_lock);
243         list_splice(list, &tmp_list);
244         INIT_LIST_HEAD(list);
245         spin_unlock_irq(list_lock);
246
247         list_for_each_entry_safe(ic, _ic, &tmp_list, iw_node)
248                 rds_conn_destroy(ic->conn);
249 }
250
251 static void rds_iw_set_scatterlist(struct rds_iw_scatterlist *sg,
252                 struct scatterlist *list, unsigned int sg_len)
253 {
254         sg->list = list;
255         sg->len = sg_len;
256         sg->dma_len = 0;
257         sg->dma_npages = 0;
258         sg->bytes = 0;
259 }
260
261 static u64 *rds_iw_map_scatterlist(struct rds_iw_device *rds_iwdev,
262                         struct rds_iw_scatterlist *sg)
263 {
264         struct ib_device *dev = rds_iwdev->dev;
265         u64 *dma_pages = NULL;
266         int i, j, ret;
267
268         WARN_ON(sg->dma_len);
269
270         sg->dma_len = ib_dma_map_sg(dev, sg->list, sg->len, DMA_BIDIRECTIONAL);
271         if (unlikely(!sg->dma_len)) {
272                 printk(KERN_WARNING "RDS/IW: dma_map_sg failed!\n");
273                 return ERR_PTR(-EBUSY);
274         }
275
276         sg->bytes = 0;
277         sg->dma_npages = 0;
278
279         ret = -EINVAL;
280         for (i = 0; i < sg->dma_len; ++i) {
281                 unsigned int dma_len = ib_sg_dma_len(dev, &sg->list[i]);
282                 u64 dma_addr = ib_sg_dma_address(dev, &sg->list[i]);
283                 u64 end_addr;
284
285                 sg->bytes += dma_len;
286
287                 end_addr = dma_addr + dma_len;
288                 if (dma_addr & PAGE_MASK) {
289                         if (i > 0)
290                                 goto out_unmap;
291                         dma_addr &= ~PAGE_MASK;
292                 }
293                 if (end_addr & PAGE_MASK) {
294                         if (i < sg->dma_len - 1)
295                                 goto out_unmap;
296                         end_addr = (end_addr + PAGE_MASK) & ~PAGE_MASK;
297                 }
298
299                 sg->dma_npages += (end_addr - dma_addr) >> PAGE_SHIFT;
300         }
301
302         /* Now gather the dma addrs into one list */
303         if (sg->dma_npages > fastreg_message_size)
304                 goto out_unmap;
305
306         dma_pages = kmalloc(sizeof(u64) * sg->dma_npages, GFP_ATOMIC);
307         if (!dma_pages) {
308                 ret = -ENOMEM;
309                 goto out_unmap;
310         }
311
312         for (i = j = 0; i < sg->dma_len; ++i) {
313                 unsigned int dma_len = ib_sg_dma_len(dev, &sg->list[i]);
314                 u64 dma_addr = ib_sg_dma_address(dev, &sg->list[i]);
315                 u64 end_addr;
316
317                 end_addr = dma_addr + dma_len;
318                 dma_addr &= ~PAGE_MASK;
319                 for (; dma_addr < end_addr; dma_addr += PAGE_SIZE)
320                         dma_pages[j++] = dma_addr;
321                 BUG_ON(j > sg->dma_npages);
322         }
323
324         return dma_pages;
325
326 out_unmap:
327         ib_dma_unmap_sg(rds_iwdev->dev, sg->list, sg->len, DMA_BIDIRECTIONAL);
328         sg->dma_len = 0;
329         kfree(dma_pages);
330         return ERR_PTR(ret);
331 }
332
333
334 struct rds_iw_mr_pool *rds_iw_create_mr_pool(struct rds_iw_device *rds_iwdev)
335 {
336         struct rds_iw_mr_pool *pool;
337
338         pool = kzalloc(sizeof(*pool), GFP_KERNEL);
339         if (!pool) {
340                 printk(KERN_WARNING "RDS/IW: rds_iw_create_mr_pool alloc error\n");
341                 return ERR_PTR(-ENOMEM);
342         }
343
344         pool->device = rds_iwdev;
345         INIT_LIST_HEAD(&pool->dirty_list);
346         INIT_LIST_HEAD(&pool->clean_list);
347         mutex_init(&pool->flush_lock);
348         spin_lock_init(&pool->list_lock);
349         INIT_WORK(&pool->flush_worker, rds_iw_mr_pool_flush_worker);
350
351         pool->max_message_size = fastreg_message_size;
352         pool->max_items = fastreg_pool_size;
353         pool->max_free_pinned = pool->max_items * pool->max_message_size / 4;
354         pool->max_pages = fastreg_message_size;
355
356         /* We never allow more than max_items MRs to be allocated.
357          * When we exceed more than max_items_soft, we start freeing
358          * items more aggressively.
359          * Make sure that max_items > max_items_soft > max_items / 2
360          */
361         pool->max_items_soft = pool->max_items * 3 / 4;
362
363         return pool;
364 }
365
366 void rds_iw_get_mr_info(struct rds_iw_device *rds_iwdev, struct rds_info_rdma_connection *iinfo)
367 {
368         struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool;
369
370         iinfo->rdma_mr_max = pool->max_items;
371         iinfo->rdma_mr_size = pool->max_pages;
372 }
373
374 void rds_iw_destroy_mr_pool(struct rds_iw_mr_pool *pool)
375 {
376         flush_workqueue(rds_wq);
377         rds_iw_flush_mr_pool(pool, 1);
378         BUG_ON(atomic_read(&pool->item_count));
379         BUG_ON(atomic_read(&pool->free_pinned));
380         kfree(pool);
381 }
382
383 static inline struct rds_iw_mr *rds_iw_reuse_fmr(struct rds_iw_mr_pool *pool)
384 {
385         struct rds_iw_mr *ibmr = NULL;
386         unsigned long flags;
387
388         spin_lock_irqsave(&pool->list_lock, flags);
389         if (!list_empty(&pool->clean_list)) {
390                 ibmr = list_entry(pool->clean_list.next, struct rds_iw_mr, mapping.m_list);
391                 list_del_init(&ibmr->mapping.m_list);
392         }
393         spin_unlock_irqrestore(&pool->list_lock, flags);
394
395         return ibmr;
396 }
397
398 static struct rds_iw_mr *rds_iw_alloc_mr(struct rds_iw_device *rds_iwdev)
399 {
400         struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool;
401         struct rds_iw_mr *ibmr = NULL;
402         int err = 0, iter = 0;
403
404         while (1) {
405                 ibmr = rds_iw_reuse_fmr(pool);
406                 if (ibmr)
407                         return ibmr;
408
409                 /* No clean MRs - now we have the choice of either
410                  * allocating a fresh MR up to the limit imposed by the
411                  * driver, or flush any dirty unused MRs.
412                  * We try to avoid stalling in the send path if possible,
413                  * so we allocate as long as we're allowed to.
414                  *
415                  * We're fussy with enforcing the FMR limit, though. If the driver
416                  * tells us we can't use more than N fmrs, we shouldn't start
417                  * arguing with it */
418                 if (atomic_inc_return(&pool->item_count) <= pool->max_items)
419                         break;
420
421                 atomic_dec(&pool->item_count);
422
423                 if (++iter > 2) {
424                         rds_iw_stats_inc(s_iw_rdma_mr_pool_depleted);
425                         return ERR_PTR(-EAGAIN);
426                 }
427
428                 /* We do have some empty MRs. Flush them out. */
429                 rds_iw_stats_inc(s_iw_rdma_mr_pool_wait);
430                 rds_iw_flush_mr_pool(pool, 0);
431         }
432
433         ibmr = kzalloc(sizeof(*ibmr), GFP_KERNEL);
434         if (!ibmr) {
435                 err = -ENOMEM;
436                 goto out_no_cigar;
437         }
438
439         spin_lock_init(&ibmr->mapping.m_lock);
440         INIT_LIST_HEAD(&ibmr->mapping.m_list);
441         ibmr->mapping.m_mr = ibmr;
442
443         err = rds_iw_init_fastreg(pool, ibmr);
444         if (err)
445                 goto out_no_cigar;
446
447         rds_iw_stats_inc(s_iw_rdma_mr_alloc);
448         return ibmr;
449
450 out_no_cigar:
451         if (ibmr) {
452                 rds_iw_destroy_fastreg(pool, ibmr);
453                 kfree(ibmr);
454         }
455         atomic_dec(&pool->item_count);
456         return ERR_PTR(err);
457 }
458
459 void rds_iw_sync_mr(void *trans_private, int direction)
460 {
461         struct rds_iw_mr *ibmr = trans_private;
462         struct rds_iw_device *rds_iwdev = ibmr->device;
463
464         switch (direction) {
465         case DMA_FROM_DEVICE:
466                 ib_dma_sync_sg_for_cpu(rds_iwdev->dev, ibmr->mapping.m_sg.list,
467                         ibmr->mapping.m_sg.dma_len, DMA_BIDIRECTIONAL);
468                 break;
469         case DMA_TO_DEVICE:
470                 ib_dma_sync_sg_for_device(rds_iwdev->dev, ibmr->mapping.m_sg.list,
471                         ibmr->mapping.m_sg.dma_len, DMA_BIDIRECTIONAL);
472                 break;
473         }
474 }
475
476 /*
477  * Flush our pool of MRs.
478  * At a minimum, all currently unused MRs are unmapped.
479  * If the number of MRs allocated exceeds the limit, we also try
480  * to free as many MRs as needed to get back to this limit.
481  */
482 static void rds_iw_flush_mr_pool(struct rds_iw_mr_pool *pool, int free_all)
483 {
484         struct rds_iw_mr *ibmr, *next;
485         LIST_HEAD(unmap_list);
486         LIST_HEAD(kill_list);
487         unsigned long flags;
488         unsigned int nfreed = 0, ncleaned = 0, unpinned = 0;
489
490         rds_iw_stats_inc(s_iw_rdma_mr_pool_flush);
491
492         mutex_lock(&pool->flush_lock);
493
494         spin_lock_irqsave(&pool->list_lock, flags);
495         /* Get the list of all mappings to be destroyed */
496         list_splice_init(&pool->dirty_list, &unmap_list);
497         if (free_all)
498                 list_splice_init(&pool->clean_list, &kill_list);
499         spin_unlock_irqrestore(&pool->list_lock, flags);
500
501         /* Batched invalidate of dirty MRs.
502          * For FMR based MRs, the mappings on the unmap list are
503          * actually members of an ibmr (ibmr->mapping). They either
504          * migrate to the kill_list, or have been cleaned and should be
505          * moved to the clean_list.
506          * For fastregs, they will be dynamically allocated, and
507          * will be destroyed by the unmap function.
508          */
509         if (!list_empty(&unmap_list)) {
510                 ncleaned = rds_iw_unmap_fastreg_list(pool, &unmap_list,
511                                                      &kill_list, &unpinned);
512                 /* If we've been asked to destroy all MRs, move those
513                  * that were simply cleaned to the kill list */
514                 if (free_all)
515                         list_splice_init(&unmap_list, &kill_list);
516         }
517
518         /* Destroy any MRs that are past their best before date */
519         list_for_each_entry_safe(ibmr, next, &kill_list, mapping.m_list) {
520                 rds_iw_stats_inc(s_iw_rdma_mr_free);
521                 list_del(&ibmr->mapping.m_list);
522                 rds_iw_destroy_fastreg(pool, ibmr);
523                 kfree(ibmr);
524                 nfreed++;
525         }
526
527         /* Anything that remains are laundered ibmrs, which we can add
528          * back to the clean list. */
529         if (!list_empty(&unmap_list)) {
530                 spin_lock_irqsave(&pool->list_lock, flags);
531                 list_splice(&unmap_list, &pool->clean_list);
532                 spin_unlock_irqrestore(&pool->list_lock, flags);
533         }
534
535         atomic_sub(unpinned, &pool->free_pinned);
536         atomic_sub(ncleaned, &pool->dirty_count);
537         atomic_sub(nfreed, &pool->item_count);
538
539         mutex_unlock(&pool->flush_lock);
540 }
541
542 static void rds_iw_mr_pool_flush_worker(struct work_struct *work)
543 {
544         struct rds_iw_mr_pool *pool = container_of(work, struct rds_iw_mr_pool, flush_worker);
545
546         rds_iw_flush_mr_pool(pool, 0);
547 }
548
549 void rds_iw_free_mr(void *trans_private, int invalidate)
550 {
551         struct rds_iw_mr *ibmr = trans_private;
552         struct rds_iw_mr_pool *pool = ibmr->device->mr_pool;
553
554         rdsdebug("RDS/IW: free_mr nents %u\n", ibmr->mapping.m_sg.len);
555         if (!pool)
556                 return;
557
558         /* Return it to the pool's free list */
559         rds_iw_free_fastreg(pool, ibmr);
560
561         /* If we've pinned too many pages, request a flush */
562         if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned ||
563             atomic_read(&pool->dirty_count) >= pool->max_items / 10)
564                 queue_work(rds_wq, &pool->flush_worker);
565
566         if (invalidate) {
567                 if (likely(!in_interrupt())) {
568                         rds_iw_flush_mr_pool(pool, 0);
569                 } else {
570                         /* We get here if the user created a MR marked
571                          * as use_once and invalidate at the same time. */
572                         queue_work(rds_wq, &pool->flush_worker);
573                 }
574         }
575 }
576
577 void rds_iw_flush_mrs(void)
578 {
579         struct rds_iw_device *rds_iwdev;
580
581         list_for_each_entry(rds_iwdev, &rds_iw_devices, list) {
582                 struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool;
583
584                 if (pool)
585                         rds_iw_flush_mr_pool(pool, 0);
586         }
587 }
588
589 void *rds_iw_get_mr(struct scatterlist *sg, unsigned long nents,
590                     struct rds_sock *rs, u32 *key_ret)
591 {
592         struct rds_iw_device *rds_iwdev;
593         struct rds_iw_mr *ibmr = NULL;
594         struct rdma_cm_id *cm_id;
595         struct sockaddr_in src = {
596                 .sin_addr.s_addr = rs->rs_bound_addr,
597                 .sin_port = rs->rs_bound_port,
598         };
599         struct sockaddr_in dst = {
600                 .sin_addr.s_addr = rs->rs_conn_addr,
601                 .sin_port = rs->rs_conn_port,
602         };
603         int ret;
604
605         ret = rds_iw_get_device(&src, &dst, &rds_iwdev, &cm_id);
606         if (ret || !cm_id) {
607                 ret = -ENODEV;
608                 goto out;
609         }
610
611         if (!rds_iwdev->mr_pool) {
612                 ret = -ENODEV;
613                 goto out;
614         }
615
616         ibmr = rds_iw_alloc_mr(rds_iwdev);
617         if (IS_ERR(ibmr))
618                 return ibmr;
619
620         ibmr->cm_id = cm_id;
621         ibmr->device = rds_iwdev;
622
623         ret = rds_iw_map_fastreg(rds_iwdev->mr_pool, ibmr, sg, nents);
624         if (ret == 0)
625                 *key_ret = ibmr->mr->rkey;
626         else
627                 printk(KERN_WARNING "RDS/IW: failed to map mr (errno=%d)\n", ret);
628
629 out:
630         if (ret) {
631                 if (ibmr)
632                         rds_iw_free_mr(ibmr, 0);
633                 ibmr = ERR_PTR(ret);
634         }
635         return ibmr;
636 }
637
638 /*
639  * iWARP fastreg handling
640  *
641  * The life cycle of a fastreg registration is a bit different from
642  * FMRs.
643  * The idea behind fastreg is to have one MR, to which we bind different
644  * mappings over time. To avoid stalling on the expensive map and invalidate
645  * operations, these operations are pipelined on the same send queue on
646  * which we want to send the message containing the r_key.
647  *
648  * This creates a bit of a problem for us, as we do not have the destination
649  * IP in GET_MR, so the connection must be setup prior to the GET_MR call for
650  * RDMA to be correctly setup.  If a fastreg request is present, rds_iw_xmit
651  * will try to queue a LOCAL_INV (if needed) and a FAST_REG_MR work request
652  * before queuing the SEND. When completions for these arrive, they are
653  * dispatched to the MR has a bit set showing that RDMa can be performed.
654  *
655  * There is another interesting aspect that's related to invalidation.
656  * The application can request that a mapping is invalidated in FREE_MR.
657  * The expectation there is that this invalidation step includes ALL
658  * PREVIOUSLY FREED MRs.
659  */
660 static int rds_iw_init_fastreg(struct rds_iw_mr_pool *pool,
661                                 struct rds_iw_mr *ibmr)
662 {
663         struct rds_iw_device *rds_iwdev = pool->device;
664         struct ib_fast_reg_page_list *page_list = NULL;
665         struct ib_mr *mr;
666         int err;
667
668         mr = ib_alloc_mr(rds_iwdev->pd, IB_MR_TYPE_MEM_REG,
669                          pool->max_message_size);
670         if (IS_ERR(mr)) {
671                 err = PTR_ERR(mr);
672
673                 printk(KERN_WARNING "RDS/IW: ib_alloc_mr failed (err=%d)\n", err);
674                 return err;
675         }
676
677         /* FIXME - this is overkill, but mapping->m_sg.dma_len/mapping->m_sg.dma_npages
678          * is not filled in.
679          */
680         page_list = ib_alloc_fast_reg_page_list(rds_iwdev->dev, pool->max_message_size);
681         if (IS_ERR(page_list)) {
682                 err = PTR_ERR(page_list);
683
684                 printk(KERN_WARNING "RDS/IW: ib_alloc_fast_reg_page_list failed (err=%d)\n", err);
685                 ib_dereg_mr(mr);
686                 return err;
687         }
688
689         ibmr->page_list = page_list;
690         ibmr->mr = mr;
691         return 0;
692 }
693
694 static int rds_iw_rdma_build_fastreg(struct rds_iw_mapping *mapping)
695 {
696         struct rds_iw_mr *ibmr = mapping->m_mr;
697         struct ib_send_wr f_wr, *failed_wr;
698         int ret;
699
700         /*
701          * Perform a WR for the fast_reg_mr. Each individual page
702          * in the sg list is added to the fast reg page list and placed
703          * inside the fast_reg_mr WR.  The key used is a rolling 8bit
704          * counter, which should guarantee uniqueness.
705          */
706         ib_update_fast_reg_key(ibmr->mr, ibmr->remap_count++);
707         mapping->m_rkey = ibmr->mr->rkey;
708
709         memset(&f_wr, 0, sizeof(f_wr));
710         f_wr.wr_id = RDS_IW_FAST_REG_WR_ID;
711         f_wr.opcode = IB_WR_FAST_REG_MR;
712         f_wr.wr.fast_reg.length = mapping->m_sg.bytes;
713         f_wr.wr.fast_reg.rkey = mapping->m_rkey;
714         f_wr.wr.fast_reg.page_list = ibmr->page_list;
715         f_wr.wr.fast_reg.page_list_len = mapping->m_sg.dma_len;
716         f_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
717         f_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE |
718                                 IB_ACCESS_REMOTE_READ |
719                                 IB_ACCESS_REMOTE_WRITE;
720         f_wr.wr.fast_reg.iova_start = 0;
721         f_wr.send_flags = IB_SEND_SIGNALED;
722
723         failed_wr = &f_wr;
724         ret = ib_post_send(ibmr->cm_id->qp, &f_wr, &failed_wr);
725         BUG_ON(failed_wr != &f_wr);
726         if (ret)
727                 printk_ratelimited(KERN_WARNING "RDS/IW: %s:%d ib_post_send returned %d\n",
728                         __func__, __LINE__, ret);
729         return ret;
730 }
731
732 static int rds_iw_rdma_fastreg_inv(struct rds_iw_mr *ibmr)
733 {
734         struct ib_send_wr s_wr, *failed_wr;
735         int ret = 0;
736
737         if (!ibmr->cm_id->qp || !ibmr->mr)
738                 goto out;
739
740         memset(&s_wr, 0, sizeof(s_wr));
741         s_wr.wr_id = RDS_IW_LOCAL_INV_WR_ID;
742         s_wr.opcode = IB_WR_LOCAL_INV;
743         s_wr.ex.invalidate_rkey = ibmr->mr->rkey;
744         s_wr.send_flags = IB_SEND_SIGNALED;
745
746         failed_wr = &s_wr;
747         ret = ib_post_send(ibmr->cm_id->qp, &s_wr, &failed_wr);
748         if (ret) {
749                 printk_ratelimited(KERN_WARNING "RDS/IW: %s:%d ib_post_send returned %d\n",
750                         __func__, __LINE__, ret);
751                 goto out;
752         }
753 out:
754         return ret;
755 }
756
757 static int rds_iw_map_fastreg(struct rds_iw_mr_pool *pool,
758                         struct rds_iw_mr *ibmr,
759                         struct scatterlist *sg,
760                         unsigned int sg_len)
761 {
762         struct rds_iw_device *rds_iwdev = pool->device;
763         struct rds_iw_mapping *mapping = &ibmr->mapping;
764         u64 *dma_pages;
765         int i, ret = 0;
766
767         rds_iw_set_scatterlist(&mapping->m_sg, sg, sg_len);
768
769         dma_pages = rds_iw_map_scatterlist(rds_iwdev, &mapping->m_sg);
770         if (IS_ERR(dma_pages)) {
771                 ret = PTR_ERR(dma_pages);
772                 dma_pages = NULL;
773                 goto out;
774         }
775
776         if (mapping->m_sg.dma_len > pool->max_message_size) {
777                 ret = -EMSGSIZE;
778                 goto out;
779         }
780
781         for (i = 0; i < mapping->m_sg.dma_npages; ++i)
782                 ibmr->page_list->page_list[i] = dma_pages[i];
783
784         ret = rds_iw_rdma_build_fastreg(mapping);
785         if (ret)
786                 goto out;
787
788         rds_iw_stats_inc(s_iw_rdma_mr_used);
789
790 out:
791         kfree(dma_pages);
792
793         return ret;
794 }
795
796 /*
797  * "Free" a fastreg MR.
798  */
799 static void rds_iw_free_fastreg(struct rds_iw_mr_pool *pool,
800                 struct rds_iw_mr *ibmr)
801 {
802         unsigned long flags;
803         int ret;
804
805         if (!ibmr->mapping.m_sg.dma_len)
806                 return;
807
808         ret = rds_iw_rdma_fastreg_inv(ibmr);
809         if (ret)
810                 return;
811
812         /* Try to post the LOCAL_INV WR to the queue. */
813         spin_lock_irqsave(&pool->list_lock, flags);
814
815         list_add_tail(&ibmr->mapping.m_list, &pool->dirty_list);
816         atomic_add(ibmr->mapping.m_sg.len, &pool->free_pinned);
817         atomic_inc(&pool->dirty_count);
818
819         spin_unlock_irqrestore(&pool->list_lock, flags);
820 }
821
822 static unsigned int rds_iw_unmap_fastreg_list(struct rds_iw_mr_pool *pool,
823                                 struct list_head *unmap_list,
824                                 struct list_head *kill_list,
825                                 int *unpinned)
826 {
827         struct rds_iw_mapping *mapping, *next;
828         unsigned int ncleaned = 0;
829         LIST_HEAD(laundered);
830
831         /* Batched invalidation of fastreg MRs.
832          * Why do we do it this way, even though we could pipeline unmap
833          * and remap? The reason is the application semantics - when the
834          * application requests an invalidation of MRs, it expects all
835          * previously released R_Keys to become invalid.
836          *
837          * If we implement MR reuse naively, we risk memory corruption
838          * (this has actually been observed). So the default behavior
839          * requires that a MR goes through an explicit unmap operation before
840          * we can reuse it again.
841          *
842          * We could probably improve on this a little, by allowing immediate
843          * reuse of a MR on the same socket (eg you could add small
844          * cache of unused MRs to strct rds_socket - GET_MR could grab one
845          * of these without requiring an explicit invalidate).
846          */
847         while (!list_empty(unmap_list)) {
848                 unsigned long flags;
849
850                 spin_lock_irqsave(&pool->list_lock, flags);
851                 list_for_each_entry_safe(mapping, next, unmap_list, m_list) {
852                         *unpinned += mapping->m_sg.len;
853                         list_move(&mapping->m_list, &laundered);
854                         ncleaned++;
855                 }
856                 spin_unlock_irqrestore(&pool->list_lock, flags);
857         }
858
859         /* Move all laundered mappings back to the unmap list.
860          * We do not kill any WRs right now - it doesn't seem the
861          * fastreg API has a max_remap limit. */
862         list_splice_init(&laundered, unmap_list);
863
864         return ncleaned;
865 }
866
867 static void rds_iw_destroy_fastreg(struct rds_iw_mr_pool *pool,
868                 struct rds_iw_mr *ibmr)
869 {
870         if (ibmr->page_list)
871                 ib_free_fast_reg_page_list(ibmr->page_list);
872         if (ibmr->mr)
873                 ib_dereg_mr(ibmr->mr);
874 }