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