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