1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/kernel.h>
34
35#include "rds.h"
36#include "rdma.h"
37#include "iw.h"
38
39
40
41
42
43struct 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
57
58struct rds_iw_mr_pool {
59 struct rds_iw_device *device;
60
61 struct mutex flush_lock;
62 struct work_struct flush_worker;
63
64 spinlock_t list_lock;
65 atomic_t item_count;
66 atomic_t dirty_count;
67 struct list_head dirty_list;
68 struct list_head clean_list;
69 atomic_t free_pinned;
70 unsigned long max_message_size;
71 unsigned long max_items;
72 unsigned long max_items_soft;
73 unsigned long max_free_pinned;
74 int max_pages;
75};
76
77static int rds_iw_flush_mr_pool(struct rds_iw_mr_pool *pool, int free_all);
78static void rds_iw_mr_pool_flush_worker(struct work_struct *work);
79static int rds_iw_init_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr);
80static int rds_iw_map_fastreg(struct rds_iw_mr_pool *pool,
81 struct rds_iw_mr *ibmr,
82 struct scatterlist *sg, unsigned int nents);
83static void rds_iw_free_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr);
84static 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);
87static void rds_iw_destroy_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr);
88
89static 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
124
125
126
127
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
143static 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
160void 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
176int 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
199void 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
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
217void 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
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
237void __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
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 if (ic->conn->c_passive)
250 rds_conn_destroy(ic->conn->c_passive);
251 rds_conn_destroy(ic->conn);
252 }
253}
254
255static void rds_iw_set_scatterlist(struct rds_iw_scatterlist *sg,
256 struct scatterlist *list, unsigned int sg_len)
257{
258 sg->list = list;
259 sg->len = sg_len;
260 sg->dma_len = 0;
261 sg->dma_npages = 0;
262 sg->bytes = 0;
263}
264
265static u64 *rds_iw_map_scatterlist(struct rds_iw_device *rds_iwdev,
266 struct rds_iw_scatterlist *sg)
267{
268 struct ib_device *dev = rds_iwdev->dev;
269 u64 *dma_pages = NULL;
270 int i, j, ret;
271
272 WARN_ON(sg->dma_len);
273
274 sg->dma_len = ib_dma_map_sg(dev, sg->list, sg->len, DMA_BIDIRECTIONAL);
275 if (unlikely(!sg->dma_len)) {
276 printk(KERN_WARNING "RDS/IW: dma_map_sg failed!\n");
277 return ERR_PTR(-EBUSY);
278 }
279
280 sg->bytes = 0;
281 sg->dma_npages = 0;
282
283 ret = -EINVAL;
284 for (i = 0; i < sg->dma_len; ++i) {
285 unsigned int dma_len = ib_sg_dma_len(dev, &sg->list[i]);
286 u64 dma_addr = ib_sg_dma_address(dev, &sg->list[i]);
287 u64 end_addr;
288
289 sg->bytes += dma_len;
290
291 end_addr = dma_addr + dma_len;
292 if (dma_addr & PAGE_MASK) {
293 if (i > 0)
294 goto out_unmap;
295 dma_addr &= ~PAGE_MASK;
296 }
297 if (end_addr & PAGE_MASK) {
298 if (i < sg->dma_len - 1)
299 goto out_unmap;
300 end_addr = (end_addr + PAGE_MASK) & ~PAGE_MASK;
301 }
302
303 sg->dma_npages += (end_addr - dma_addr) >> PAGE_SHIFT;
304 }
305
306
307 if (sg->dma_npages > fastreg_message_size)
308 goto out_unmap;
309
310 dma_pages = kmalloc(sizeof(u64) * sg->dma_npages, GFP_ATOMIC);
311 if (!dma_pages) {
312 ret = -ENOMEM;
313 goto out_unmap;
314 }
315
316 for (i = j = 0; i < sg->dma_len; ++i) {
317 unsigned int dma_len = ib_sg_dma_len(dev, &sg->list[i]);
318 u64 dma_addr = ib_sg_dma_address(dev, &sg->list[i]);
319 u64 end_addr;
320
321 end_addr = dma_addr + dma_len;
322 dma_addr &= ~PAGE_MASK;
323 for (; dma_addr < end_addr; dma_addr += PAGE_SIZE)
324 dma_pages[j++] = dma_addr;
325 BUG_ON(j > sg->dma_npages);
326 }
327
328 return dma_pages;
329
330out_unmap:
331 ib_dma_unmap_sg(rds_iwdev->dev, sg->list, sg->len, DMA_BIDIRECTIONAL);
332 sg->dma_len = 0;
333 kfree(dma_pages);
334 return ERR_PTR(ret);
335}
336
337
338struct rds_iw_mr_pool *rds_iw_create_mr_pool(struct rds_iw_device *rds_iwdev)
339{
340 struct rds_iw_mr_pool *pool;
341
342 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
343 if (!pool) {
344 printk(KERN_WARNING "RDS/IW: rds_iw_create_mr_pool alloc error\n");
345 return ERR_PTR(-ENOMEM);
346 }
347
348 pool->device = rds_iwdev;
349 INIT_LIST_HEAD(&pool->dirty_list);
350 INIT_LIST_HEAD(&pool->clean_list);
351 mutex_init(&pool->flush_lock);
352 spin_lock_init(&pool->list_lock);
353 INIT_WORK(&pool->flush_worker, rds_iw_mr_pool_flush_worker);
354
355 pool->max_message_size = fastreg_message_size;
356 pool->max_items = fastreg_pool_size;
357 pool->max_free_pinned = pool->max_items * pool->max_message_size / 4;
358 pool->max_pages = fastreg_message_size;
359
360
361
362
363
364
365 pool->max_items_soft = pool->max_items * 3 / 4;
366
367 return pool;
368}
369
370void rds_iw_get_mr_info(struct rds_iw_device *rds_iwdev, struct rds_info_rdma_connection *iinfo)
371{
372 struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool;
373
374 iinfo->rdma_mr_max = pool->max_items;
375 iinfo->rdma_mr_size = pool->max_pages;
376}
377
378void rds_iw_destroy_mr_pool(struct rds_iw_mr_pool *pool)
379{
380 flush_workqueue(rds_wq);
381 rds_iw_flush_mr_pool(pool, 1);
382 BUG_ON(atomic_read(&pool->item_count));
383 BUG_ON(atomic_read(&pool->free_pinned));
384 kfree(pool);
385}
386
387static inline struct rds_iw_mr *rds_iw_reuse_fmr(struct rds_iw_mr_pool *pool)
388{
389 struct rds_iw_mr *ibmr = NULL;
390 unsigned long flags;
391
392 spin_lock_irqsave(&pool->list_lock, flags);
393 if (!list_empty(&pool->clean_list)) {
394 ibmr = list_entry(pool->clean_list.next, struct rds_iw_mr, mapping.m_list);
395 list_del_init(&ibmr->mapping.m_list);
396 }
397 spin_unlock_irqrestore(&pool->list_lock, flags);
398
399 return ibmr;
400}
401
402static struct rds_iw_mr *rds_iw_alloc_mr(struct rds_iw_device *rds_iwdev)
403{
404 struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool;
405 struct rds_iw_mr *ibmr = NULL;
406 int err = 0, iter = 0;
407
408 while (1) {
409 ibmr = rds_iw_reuse_fmr(pool);
410 if (ibmr)
411 return ibmr;
412
413
414
415
416
417
418
419
420
421
422 if (atomic_inc_return(&pool->item_count) <= pool->max_items)
423 break;
424
425 atomic_dec(&pool->item_count);
426
427 if (++iter > 2) {
428 rds_iw_stats_inc(s_iw_rdma_mr_pool_depleted);
429 return ERR_PTR(-EAGAIN);
430 }
431
432
433 rds_iw_stats_inc(s_iw_rdma_mr_pool_wait);
434 rds_iw_flush_mr_pool(pool, 0);
435 }
436
437 ibmr = kzalloc(sizeof(*ibmr), GFP_KERNEL);
438 if (!ibmr) {
439 err = -ENOMEM;
440 goto out_no_cigar;
441 }
442
443 spin_lock_init(&ibmr->mapping.m_lock);
444 INIT_LIST_HEAD(&ibmr->mapping.m_list);
445 ibmr->mapping.m_mr = ibmr;
446
447 err = rds_iw_init_fastreg(pool, ibmr);
448 if (err)
449 goto out_no_cigar;
450
451 rds_iw_stats_inc(s_iw_rdma_mr_alloc);
452 return ibmr;
453
454out_no_cigar:
455 if (ibmr) {
456 rds_iw_destroy_fastreg(pool, ibmr);
457 kfree(ibmr);
458 }
459 atomic_dec(&pool->item_count);
460 return ERR_PTR(err);
461}
462
463void rds_iw_sync_mr(void *trans_private, int direction)
464{
465 struct rds_iw_mr *ibmr = trans_private;
466 struct rds_iw_device *rds_iwdev = ibmr->device;
467
468 switch (direction) {
469 case DMA_FROM_DEVICE:
470 ib_dma_sync_sg_for_cpu(rds_iwdev->dev, ibmr->mapping.m_sg.list,
471 ibmr->mapping.m_sg.dma_len, DMA_BIDIRECTIONAL);
472 break;
473 case DMA_TO_DEVICE:
474 ib_dma_sync_sg_for_device(rds_iwdev->dev, ibmr->mapping.m_sg.list,
475 ibmr->mapping.m_sg.dma_len, DMA_BIDIRECTIONAL);
476 break;
477 }
478}
479
480static inline unsigned int rds_iw_flush_goal(struct rds_iw_mr_pool *pool, int free_all)
481{
482 unsigned int item_count;
483
484 item_count = atomic_read(&pool->item_count);
485 if (free_all)
486 return item_count;
487
488 return 0;
489}
490
491
492
493
494
495
496
497static int rds_iw_flush_mr_pool(struct rds_iw_mr_pool *pool, int free_all)
498{
499 struct rds_iw_mr *ibmr, *next;
500 LIST_HEAD(unmap_list);
501 LIST_HEAD(kill_list);
502 unsigned long flags;
503 unsigned int nfreed = 0, ncleaned = 0, free_goal;
504 int ret = 0;
505
506 rds_iw_stats_inc(s_iw_rdma_mr_pool_flush);
507
508 mutex_lock(&pool->flush_lock);
509
510 spin_lock_irqsave(&pool->list_lock, flags);
511
512 list_splice_init(&pool->dirty_list, &unmap_list);
513 if (free_all)
514 list_splice_init(&pool->clean_list, &kill_list);
515 spin_unlock_irqrestore(&pool->list_lock, flags);
516
517 free_goal = rds_iw_flush_goal(pool, free_all);
518
519
520
521
522
523
524
525
526
527 if (!list_empty(&unmap_list)) {
528 ncleaned = rds_iw_unmap_fastreg_list(pool, &unmap_list, &kill_list);
529
530
531 if (free_all)
532 list_splice_init(&unmap_list, &kill_list);
533 }
534
535
536 list_for_each_entry_safe(ibmr, next, &kill_list, mapping.m_list) {
537 rds_iw_stats_inc(s_iw_rdma_mr_free);
538 list_del(&ibmr->mapping.m_list);
539 rds_iw_destroy_fastreg(pool, ibmr);
540 kfree(ibmr);
541 nfreed++;
542 }
543
544
545
546 if (!list_empty(&unmap_list)) {
547 spin_lock_irqsave(&pool->list_lock, flags);
548 list_splice(&unmap_list, &pool->clean_list);
549 spin_unlock_irqrestore(&pool->list_lock, flags);
550 }
551
552 atomic_sub(ncleaned, &pool->dirty_count);
553 atomic_sub(nfreed, &pool->item_count);
554
555 mutex_unlock(&pool->flush_lock);
556 return ret;
557}
558
559static void rds_iw_mr_pool_flush_worker(struct work_struct *work)
560{
561 struct rds_iw_mr_pool *pool = container_of(work, struct rds_iw_mr_pool, flush_worker);
562
563 rds_iw_flush_mr_pool(pool, 0);
564}
565
566void rds_iw_free_mr(void *trans_private, int invalidate)
567{
568 struct rds_iw_mr *ibmr = trans_private;
569 struct rds_iw_mr_pool *pool = ibmr->device->mr_pool;
570
571 rdsdebug("RDS/IW: free_mr nents %u\n", ibmr->mapping.m_sg.len);
572 if (!pool)
573 return;
574
575
576 rds_iw_free_fastreg(pool, ibmr);
577
578
579 if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned
580 || atomic_read(&pool->dirty_count) >= pool->max_items / 10)
581 queue_work(rds_wq, &pool->flush_worker);
582
583 if (invalidate) {
584 if (likely(!in_interrupt())) {
585 rds_iw_flush_mr_pool(pool, 0);
586 } else {
587
588
589 queue_work(rds_wq, &pool->flush_worker);
590 }
591 }
592}
593
594void rds_iw_flush_mrs(void)
595{
596 struct rds_iw_device *rds_iwdev;
597
598 list_for_each_entry(rds_iwdev, &rds_iw_devices, list) {
599 struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool;
600
601 if (pool)
602 rds_iw_flush_mr_pool(pool, 0);
603 }
604}
605
606void *rds_iw_get_mr(struct scatterlist *sg, unsigned long nents,
607 struct rds_sock *rs, u32 *key_ret)
608{
609 struct rds_iw_device *rds_iwdev;
610 struct rds_iw_mr *ibmr = NULL;
611 struct rdma_cm_id *cm_id;
612 int ret;
613
614 ret = rds_iw_get_device(rs, &rds_iwdev, &cm_id);
615 if (ret || !cm_id) {
616 ret = -ENODEV;
617 goto out;
618 }
619
620 if (!rds_iwdev->mr_pool) {
621 ret = -ENODEV;
622 goto out;
623 }
624
625 ibmr = rds_iw_alloc_mr(rds_iwdev);
626 if (IS_ERR(ibmr))
627 return ibmr;
628
629 ibmr->cm_id = cm_id;
630 ibmr->device = rds_iwdev;
631
632 ret = rds_iw_map_fastreg(rds_iwdev->mr_pool, ibmr, sg, nents);
633 if (ret == 0)
634 *key_ret = ibmr->mr->rkey;
635 else
636 printk(KERN_WARNING "RDS/IW: failed to map mr (errno=%d)\n", ret);
637
638out:
639 if (ret) {
640 if (ibmr)
641 rds_iw_free_mr(ibmr, 0);
642 ibmr = ERR_PTR(ret);
643 }
644 return ibmr;
645}
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669static int rds_iw_init_fastreg(struct rds_iw_mr_pool *pool,
670 struct rds_iw_mr *ibmr)
671{
672 struct rds_iw_device *rds_iwdev = pool->device;
673 struct ib_fast_reg_page_list *page_list = NULL;
674 struct ib_mr *mr;
675 int err;
676
677 mr = ib_alloc_fast_reg_mr(rds_iwdev->pd, pool->max_message_size);
678 if (IS_ERR(mr)) {
679 err = PTR_ERR(mr);
680
681 printk(KERN_WARNING "RDS/IW: ib_alloc_fast_reg_mr failed (err=%d)\n", err);
682 return err;
683 }
684
685
686
687
688 page_list = ib_alloc_fast_reg_page_list(rds_iwdev->dev, pool->max_message_size);
689 if (IS_ERR(page_list)) {
690 err = PTR_ERR(page_list);
691
692 printk(KERN_WARNING "RDS/IW: ib_alloc_fast_reg_page_list failed (err=%d)\n", err);
693 ib_dereg_mr(mr);
694 return err;
695 }
696
697 ibmr->page_list = page_list;
698 ibmr->mr = mr;
699 return 0;
700}
701
702static int rds_iw_rdma_build_fastreg(struct rds_iw_mapping *mapping)
703{
704 struct rds_iw_mr *ibmr = mapping->m_mr;
705 struct ib_send_wr f_wr, *failed_wr;
706 int ret;
707
708
709
710
711
712
713
714 ib_update_fast_reg_key(ibmr->mr, ibmr->remap_count++);
715 mapping->m_rkey = ibmr->mr->rkey;
716
717 memset(&f_wr, 0, sizeof(f_wr));
718 f_wr.wr_id = RDS_IW_FAST_REG_WR_ID;
719 f_wr.opcode = IB_WR_FAST_REG_MR;
720 f_wr.wr.fast_reg.length = mapping->m_sg.bytes;
721 f_wr.wr.fast_reg.rkey = mapping->m_rkey;
722 f_wr.wr.fast_reg.page_list = ibmr->page_list;
723 f_wr.wr.fast_reg.page_list_len = mapping->m_sg.dma_len;
724 f_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
725 f_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE |
726 IB_ACCESS_REMOTE_READ |
727 IB_ACCESS_REMOTE_WRITE;
728 f_wr.wr.fast_reg.iova_start = 0;
729 f_wr.send_flags = IB_SEND_SIGNALED;
730
731 failed_wr = &f_wr;
732 ret = ib_post_send(ibmr->cm_id->qp, &f_wr, &failed_wr);
733 BUG_ON(failed_wr != &f_wr);
734 if (ret && printk_ratelimit())
735 printk(KERN_WARNING "RDS/IW: %s:%d ib_post_send returned %d\n",
736 __func__, __LINE__, ret);
737 return ret;
738}
739
740static int rds_iw_rdma_fastreg_inv(struct rds_iw_mr *ibmr)
741{
742 struct ib_send_wr s_wr, *failed_wr;
743 int ret = 0;
744
745 if (!ibmr->cm_id->qp || !ibmr->mr)
746 goto out;
747
748 memset(&s_wr, 0, sizeof(s_wr));
749 s_wr.wr_id = RDS_IW_LOCAL_INV_WR_ID;
750 s_wr.opcode = IB_WR_LOCAL_INV;
751 s_wr.ex.invalidate_rkey = ibmr->mr->rkey;
752 s_wr.send_flags = IB_SEND_SIGNALED;
753
754 failed_wr = &s_wr;
755 ret = ib_post_send(ibmr->cm_id->qp, &s_wr, &failed_wr);
756 if (ret && printk_ratelimit()) {
757 printk(KERN_WARNING "RDS/IW: %s:%d ib_post_send returned %d\n",
758 __func__, __LINE__, ret);
759 goto out;
760 }
761out:
762 return ret;
763}
764
765static int rds_iw_map_fastreg(struct rds_iw_mr_pool *pool,
766 struct rds_iw_mr *ibmr,
767 struct scatterlist *sg,
768 unsigned int sg_len)
769{
770 struct rds_iw_device *rds_iwdev = pool->device;
771 struct rds_iw_mapping *mapping = &ibmr->mapping;
772 u64 *dma_pages;
773 int i, ret = 0;
774
775 rds_iw_set_scatterlist(&mapping->m_sg, sg, sg_len);
776
777 dma_pages = rds_iw_map_scatterlist(rds_iwdev, &mapping->m_sg);
778 if (IS_ERR(dma_pages)) {
779 ret = PTR_ERR(dma_pages);
780 dma_pages = NULL;
781 goto out;
782 }
783
784 if (mapping->m_sg.dma_len > pool->max_message_size) {
785 ret = -EMSGSIZE;
786 goto out;
787 }
788
789 for (i = 0; i < mapping->m_sg.dma_npages; ++i)
790 ibmr->page_list->page_list[i] = dma_pages[i];
791
792 ret = rds_iw_rdma_build_fastreg(mapping);
793 if (ret)
794 goto out;
795
796 rds_iw_stats_inc(s_iw_rdma_mr_used);
797
798out:
799 kfree(dma_pages);
800
801 return ret;
802}
803
804
805
806
807static void rds_iw_free_fastreg(struct rds_iw_mr_pool *pool,
808 struct rds_iw_mr *ibmr)
809{
810 unsigned long flags;
811 int ret;
812
813 if (!ibmr->mapping.m_sg.dma_len)
814 return;
815
816 ret = rds_iw_rdma_fastreg_inv(ibmr);
817 if (ret)
818 return;
819
820
821 spin_lock_irqsave(&pool->list_lock, flags);
822
823 list_add_tail(&ibmr->mapping.m_list, &pool->dirty_list);
824 atomic_add(ibmr->mapping.m_sg.len, &pool->free_pinned);
825 atomic_inc(&pool->dirty_count);
826
827 spin_unlock_irqrestore(&pool->list_lock, flags);
828}
829
830static unsigned int rds_iw_unmap_fastreg_list(struct rds_iw_mr_pool *pool,
831 struct list_head *unmap_list,
832 struct list_head *kill_list)
833{
834 struct rds_iw_mapping *mapping, *next;
835 unsigned int ncleaned = 0;
836 LIST_HEAD(laundered);
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854 while (!list_empty(unmap_list)) {
855 unsigned long flags;
856
857 spin_lock_irqsave(&pool->list_lock, flags);
858 list_for_each_entry_safe(mapping, next, unmap_list, m_list) {
859 list_move(&mapping->m_list, &laundered);
860 ncleaned++;
861 }
862 spin_unlock_irqrestore(&pool->list_lock, flags);
863 }
864
865
866
867
868 list_splice_init(&laundered, unmap_list);
869
870 return ncleaned;
871}
872
873static void rds_iw_destroy_fastreg(struct rds_iw_mr_pool *pool,
874 struct rds_iw_mr *ibmr)
875{
876 if (ibmr->page_list)
877 ib_free_fast_reg_page_list(ibmr->page_list);
878 if (ibmr->mr)
879 ib_dereg_mr(ibmr->mr);
880}
881