Free Electrons

Embedded Linux Experts

  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
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
/*
 * Copyright (c) 2006 Oracle.  All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/ratelimit.h>

#include "rds.h"
#include "iw.h"


/*
 * This is stored as mr->r_trans_private.
 */
struct rds_iw_mr {
	struct rds_iw_device	*device;
	struct rds_iw_mr_pool	*pool;
	struct rdma_cm_id	*cm_id;

	struct ib_mr	*mr;
	struct ib_fast_reg_page_list *page_list;

	struct rds_iw_mapping	mapping;
	unsigned char		remap_count;
};

/*
 * Our own little MR pool
 */
struct rds_iw_mr_pool {
	struct rds_iw_device	*device;		/* back ptr to the device that owns us */

	struct mutex		flush_lock;		/* serialize fmr invalidate */
	struct work_struct	flush_worker;		/* flush worker */

	spinlock_t		list_lock;		/* protect variables below */
	atomic_t		item_count;		/* total # of MRs */
	atomic_t		dirty_count;		/* # dirty of MRs */
	struct list_head	dirty_list;		/* dirty mappings */
	struct list_head	clean_list;		/* unused & unamapped MRs */
	atomic_t		free_pinned;		/* memory pinned by free MRs */
	unsigned long		max_message_size;	/* in pages */
	unsigned long		max_items;
	unsigned long		max_items_soft;
	unsigned long		max_free_pinned;
	int			max_pages;
};

static int rds_iw_flush_mr_pool(struct rds_iw_mr_pool *pool, int free_all);
static void rds_iw_mr_pool_flush_worker(struct work_struct *work);
static int rds_iw_init_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr);
static int rds_iw_map_fastreg(struct rds_iw_mr_pool *pool,
			  struct rds_iw_mr *ibmr,
			  struct scatterlist *sg, unsigned int nents);
static void rds_iw_free_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr);
static unsigned int rds_iw_unmap_fastreg_list(struct rds_iw_mr_pool *pool,
			struct list_head *unmap_list,
			struct list_head *kill_list,
			int *unpinned);
static void rds_iw_destroy_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr);

static int rds_iw_get_device(struct rds_sock *rs, struct rds_iw_device **rds_iwdev, struct rdma_cm_id **cm_id)
{
	struct rds_iw_device *iwdev;
	struct rds_iw_cm_id *i_cm_id;

	*rds_iwdev = NULL;
	*cm_id = NULL;

	list_for_each_entry(iwdev, &rds_iw_devices, list) {
		spin_lock_irq(&iwdev->spinlock);
		list_for_each_entry(i_cm_id, &iwdev->cm_id_list, list) {
			struct sockaddr_in *src_addr, *dst_addr;

			src_addr = (struct sockaddr_in *)&i_cm_id->cm_id->route.addr.src_addr;
			dst_addr = (struct sockaddr_in *)&i_cm_id->cm_id->route.addr.dst_addr;

			rdsdebug("local ipaddr = %x port %d, "
				 "remote ipaddr = %x port %d"
				 "..looking for %x port %d, "
				 "remote ipaddr = %x port %d\n",
				src_addr->sin_addr.s_addr,
				src_addr->sin_port,
				dst_addr->sin_addr.s_addr,
				dst_addr->sin_port,
				rs->rs_bound_addr,
				rs->rs_bound_port,
				rs->rs_conn_addr,
				rs->rs_conn_port);
#ifdef WORKING_TUPLE_DETECTION
			if (src_addr->sin_addr.s_addr == rs->rs_bound_addr &&
			    src_addr->sin_port == rs->rs_bound_port &&
			    dst_addr->sin_addr.s_addr == rs->rs_conn_addr &&
			    dst_addr->sin_port == rs->rs_conn_port) {
#else
			/* FIXME - needs to compare the local and remote
			 * ipaddr/port tuple, but the ipaddr is the only
			 * available information in the rds_sock (as the rest are
			 * zero'ed.  It doesn't appear to be properly populated
			 * during connection setup...
			 */
			if (src_addr->sin_addr.s_addr == rs->rs_bound_addr) {
#endif
				spin_unlock_irq(&iwdev->spinlock);
				*rds_iwdev = iwdev;
				*cm_id = i_cm_id->cm_id;
				return 0;
			}
		}
		spin_unlock_irq(&iwdev->spinlock);
	}

	return 1;
}

static int rds_iw_add_cm_id(struct rds_iw_device *rds_iwdev, struct rdma_cm_id *cm_id)
{
	struct rds_iw_cm_id *i_cm_id;

	i_cm_id = kmalloc(sizeof *i_cm_id, GFP_KERNEL);
	if (!i_cm_id)
		return -ENOMEM;

	i_cm_id->cm_id = cm_id;

	spin_lock_irq(&rds_iwdev->spinlock);
	list_add_tail(&i_cm_id->list, &rds_iwdev->cm_id_list);
	spin_unlock_irq(&rds_iwdev->spinlock);

	return 0;
}

static void rds_iw_remove_cm_id(struct rds_iw_device *rds_iwdev,
				struct rdma_cm_id *cm_id)
{
	struct rds_iw_cm_id *i_cm_id;

	spin_lock_irq(&rds_iwdev->spinlock);
	list_for_each_entry(i_cm_id, &rds_iwdev->cm_id_list, list) {
		if (i_cm_id->cm_id == cm_id) {
			list_del(&i_cm_id->list);
			kfree(i_cm_id);
			break;
		}
	}
	spin_unlock_irq(&rds_iwdev->spinlock);
}


int rds_iw_update_cm_id(struct rds_iw_device *rds_iwdev, struct rdma_cm_id *cm_id)
{
	struct sockaddr_in *src_addr, *dst_addr;
	struct rds_iw_device *rds_iwdev_old;
	struct rds_sock rs;
	struct rdma_cm_id *pcm_id;
	int rc;

	src_addr = (struct sockaddr_in *)&cm_id->route.addr.src_addr;
	dst_addr = (struct sockaddr_in *)&cm_id->route.addr.dst_addr;

	rs.rs_bound_addr = src_addr->sin_addr.s_addr;
	rs.rs_bound_port = src_addr->sin_port;
	rs.rs_conn_addr = dst_addr->sin_addr.s_addr;
	rs.rs_conn_port = dst_addr->sin_port;

	rc = rds_iw_get_device(&rs, &rds_iwdev_old, &pcm_id);
	if (rc)
		rds_iw_remove_cm_id(rds_iwdev, cm_id);

	return rds_iw_add_cm_id(rds_iwdev, cm_id);
}

void rds_iw_add_conn(struct rds_iw_device *rds_iwdev, struct rds_connection *conn)
{
	struct rds_iw_connection *ic = conn->c_transport_data;

	/* conn was previously on the nodev_conns_list */
	spin_lock_irq(&iw_nodev_conns_lock);
	BUG_ON(list_empty(&iw_nodev_conns));
	BUG_ON(list_empty(&ic->iw_node));
	list_del(&ic->iw_node);

	spin_lock(&rds_iwdev->spinlock);
	list_add_tail(&ic->iw_node, &rds_iwdev->conn_list);
	spin_unlock(&rds_iwdev->spinlock);
	spin_unlock_irq(&iw_nodev_conns_lock);

	ic->rds_iwdev = rds_iwdev;
}

void rds_iw_remove_conn(struct rds_iw_device *rds_iwdev, struct rds_connection *conn)
{
	struct rds_iw_connection *ic = conn->c_transport_data;

	/* place conn on nodev_conns_list */
	spin_lock(&iw_nodev_conns_lock);

	spin_lock_irq(&rds_iwdev->spinlock);
	BUG_ON(list_empty(&ic->iw_node));
	list_del(&ic->iw_node);
	spin_unlock_irq(&rds_iwdev->spinlock);

	list_add_tail(&ic->iw_node, &iw_nodev_conns);

	spin_unlock(&iw_nodev_conns_lock);

	rds_iw_remove_cm_id(ic->rds_iwdev, ic->i_cm_id);
	ic->rds_iwdev = NULL;
}

void __rds_iw_destroy_conns(struct list_head *list, spinlock_t *list_lock)
{
	struct rds_iw_connection *ic, *_ic;
	LIST_HEAD(tmp_list);

	/* avoid calling conn_destroy with irqs off */
	spin_lock_irq(list_lock);
	list_splice(list, &tmp_list);
	INIT_LIST_HEAD(list);
	spin_unlock_irq(list_lock);

	list_for_each_entry_safe(ic, _ic, &tmp_list, iw_node)
		rds_conn_destroy(ic->conn);
}

static void rds_iw_set_scatterlist(struct rds_iw_scatterlist *sg,
		struct scatterlist *list, unsigned int sg_len)
{
	sg->list = list;
	sg->len = sg_len;
	sg->dma_len = 0;
	sg->dma_npages = 0;
	sg->bytes = 0;
}

static u64 *rds_iw_map_scatterlist(struct rds_iw_device *rds_iwdev,
			struct rds_iw_scatterlist *sg)
{
	struct ib_device *dev = rds_iwdev->dev;
	u64 *dma_pages = NULL;
	int i, j, ret;

	WARN_ON(sg->dma_len);

	sg->dma_len = ib_dma_map_sg(dev, sg->list, sg->len, DMA_BIDIRECTIONAL);
	if (unlikely(!sg->dma_len)) {
		printk(KERN_WARNING "RDS/IW: dma_map_sg failed!\n");
		return ERR_PTR(-EBUSY);
	}

	sg->bytes = 0;
	sg->dma_npages = 0;

	ret = -EINVAL;
	for (i = 0; i < sg->dma_len; ++i) {
		unsigned int dma_len = ib_sg_dma_len(dev, &sg->list[i]);
		u64 dma_addr = ib_sg_dma_address(dev, &sg->list[i]);
		u64 end_addr;

		sg->bytes += dma_len;

		end_addr = dma_addr + dma_len;
		if (dma_addr & PAGE_MASK) {
			if (i > 0)
				goto out_unmap;
			dma_addr &= ~PAGE_MASK;
		}
		if (end_addr & PAGE_MASK) {
			if (i < sg->dma_len - 1)
				goto out_unmap;
			end_addr = (end_addr + PAGE_MASK) & ~PAGE_MASK;
		}

		sg->dma_npages += (end_addr - dma_addr) >> PAGE_SHIFT;
	}

	/* Now gather the dma addrs into one list */
	if (sg->dma_npages > fastreg_message_size)
		goto out_unmap;

	dma_pages = kmalloc(sizeof(u64) * sg->dma_npages, GFP_ATOMIC);
	if (!dma_pages) {
		ret = -ENOMEM;
		goto out_unmap;
	}

	for (i = j = 0; i < sg->dma_len; ++i) {
		unsigned int dma_len = ib_sg_dma_len(dev, &sg->list[i]);
		u64 dma_addr = ib_sg_dma_address(dev, &sg->list[i]);
		u64 end_addr;

		end_addr = dma_addr + dma_len;
		dma_addr &= ~PAGE_MASK;
		for (; dma_addr < end_addr; dma_addr += PAGE_SIZE)
			dma_pages[j++] = dma_addr;
		BUG_ON(j > sg->dma_npages);
	}

	return dma_pages;

out_unmap:
	ib_dma_unmap_sg(rds_iwdev->dev, sg->list, sg->len, DMA_BIDIRECTIONAL);
	sg->dma_len = 0;
	kfree(dma_pages);
	return ERR_PTR(ret);
}


struct rds_iw_mr_pool *rds_iw_create_mr_pool(struct rds_iw_device *rds_iwdev)
{
	struct rds_iw_mr_pool *pool;

	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
	if (!pool) {
		printk(KERN_WARNING "RDS/IW: rds_iw_create_mr_pool alloc error\n");
		return ERR_PTR(-ENOMEM);
	}

	pool->device = rds_iwdev;
	INIT_LIST_HEAD(&pool->dirty_list);
	INIT_LIST_HEAD(&pool->clean_list);
	mutex_init(&pool->flush_lock);
	spin_lock_init(&pool->list_lock);
	INIT_WORK(&pool->flush_worker, rds_iw_mr_pool_flush_worker);

	pool->max_message_size = fastreg_message_size;
	pool->max_items = fastreg_pool_size;
	pool->max_free_pinned = pool->max_items * pool->max_message_size / 4;
	pool->max_pages = fastreg_message_size;

	/* We never allow more than max_items MRs to be allocated.
	 * When we exceed more than max_items_soft, we start freeing
	 * items more aggressively.
	 * Make sure that max_items > max_items_soft > max_items / 2
	 */
	pool->max_items_soft = pool->max_items * 3 / 4;

	return pool;
}

void rds_iw_get_mr_info(struct rds_iw_device *rds_iwdev, struct rds_info_rdma_connection *iinfo)
{
	struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool;

	iinfo->rdma_mr_max = pool->max_items;
	iinfo->rdma_mr_size = pool->max_pages;
}

void rds_iw_destroy_mr_pool(struct rds_iw_mr_pool *pool)
{
	flush_workqueue(rds_wq);
	rds_iw_flush_mr_pool(pool, 1);
	BUG_ON(atomic_read(&pool->item_count));
	BUG_ON(atomic_read(&pool->free_pinned));
	kfree(pool);
}

static inline struct rds_iw_mr *rds_iw_reuse_fmr(struct rds_iw_mr_pool *pool)
{
	struct rds_iw_mr *ibmr = NULL;
	unsigned long flags;

	spin_lock_irqsave(&pool->list_lock, flags);
	if (!list_empty(&pool->clean_list)) {
		ibmr = list_entry(pool->clean_list.next, struct rds_iw_mr, mapping.m_list);
		list_del_init(&ibmr->mapping.m_list);
	}
	spin_unlock_irqrestore(&pool->list_lock, flags);

	return ibmr;
}

static struct rds_iw_mr *rds_iw_alloc_mr(struct rds_iw_device *rds_iwdev)
{
	struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool;
	struct rds_iw_mr *ibmr = NULL;
	int err = 0, iter = 0;

	while (1) {
		ibmr = rds_iw_reuse_fmr(pool);
		if (ibmr)
			return ibmr;

		/* No clean MRs - now we have the choice of either
		 * allocating a fresh MR up to the limit imposed by the
		 * driver, or flush any dirty unused MRs.
		 * We try to avoid stalling in the send path if possible,
		 * so we allocate as long as we're allowed to.
		 *
		 * We're fussy with enforcing the FMR limit, though. If the driver
		 * tells us we can't use more than N fmrs, we shouldn't start
		 * arguing with it */
		if (atomic_inc_return(&pool->item_count) <= pool->max_items)
			break;

		atomic_dec(&pool->item_count);

		if (++iter > 2) {
			rds_iw_stats_inc(s_iw_rdma_mr_pool_depleted);
			return ERR_PTR(-EAGAIN);
		}

		/* We do have some empty MRs. Flush them out. */
		rds_iw_stats_inc(s_iw_rdma_mr_pool_wait);
		rds_iw_flush_mr_pool(pool, 0);
	}

	ibmr = kzalloc(sizeof(*ibmr), GFP_KERNEL);
	if (!ibmr) {
		err = -ENOMEM;
		goto out_no_cigar;
	}

	spin_lock_init(&ibmr->mapping.m_lock);
	INIT_LIST_HEAD(&ibmr->mapping.m_list);
	ibmr->mapping.m_mr = ibmr;

	err = rds_iw_init_fastreg(pool, ibmr);
	if (err)
		goto out_no_cigar;

	rds_iw_stats_inc(s_iw_rdma_mr_alloc);
	return ibmr;

out_no_cigar:
	if (ibmr) {
		rds_iw_destroy_fastreg(pool, ibmr);
		kfree(ibmr);
	}
	atomic_dec(&pool->item_count);
	return ERR_PTR(err);
}

void rds_iw_sync_mr(void *trans_private, int direction)
{
	struct rds_iw_mr *ibmr = trans_private;
	struct rds_iw_device *rds_iwdev = ibmr->device;

	switch (direction) {
	case DMA_FROM_DEVICE:
		ib_dma_sync_sg_for_cpu(rds_iwdev->dev, ibmr->mapping.m_sg.list,
			ibmr->mapping.m_sg.dma_len, DMA_BIDIRECTIONAL);
		break;
	case DMA_TO_DEVICE:
		ib_dma_sync_sg_for_device(rds_iwdev->dev, ibmr->mapping.m_sg.list,
			ibmr->mapping.m_sg.dma_len, DMA_BIDIRECTIONAL);
		break;
	}
}

/*
 * Flush our pool of MRs.
 * At a minimum, all currently unused MRs are unmapped.
 * If the number of MRs allocated exceeds the limit, we also try
 * to free as many MRs as needed to get back to this limit.
 */
static int rds_iw_flush_mr_pool(struct rds_iw_mr_pool *pool, int free_all)
{
	struct rds_iw_mr *ibmr, *next;
	LIST_HEAD(unmap_list);
	LIST_HEAD(kill_list);
	unsigned long flags;
	unsigned int nfreed = 0, ncleaned = 0, unpinned = 0;
	int ret = 0;

	rds_iw_stats_inc(s_iw_rdma_mr_pool_flush);

	mutex_lock(&pool->flush_lock);

	spin_lock_irqsave(&pool->list_lock, flags);
	/* Get the list of all mappings to be destroyed */
	list_splice_init(&pool->dirty_list, &unmap_list);
	if (free_all)
		list_splice_init(&pool->clean_list, &kill_list);
	spin_unlock_irqrestore(&pool->list_lock, flags);

	/* Batched invalidate of dirty MRs.
	 * For FMR based MRs, the mappings on the unmap list are
	 * actually members of an ibmr (ibmr->mapping). They either
	 * migrate to the kill_list, or have been cleaned and should be
	 * moved to the clean_list.
	 * For fastregs, they will be dynamically allocated, and
	 * will be destroyed by the unmap function.
	 */
	if (!list_empty(&unmap_list)) {
		ncleaned = rds_iw_unmap_fastreg_list(pool, &unmap_list,
						     &kill_list, &unpinned);
		/* If we've been asked to destroy all MRs, move those
		 * that were simply cleaned to the kill list */
		if (free_all)
			list_splice_init(&unmap_list, &kill_list);
	}

	/* Destroy any MRs that are past their best before date */
	list_for_each_entry_safe(ibmr, next, &kill_list, mapping.m_list) {
		rds_iw_stats_inc(s_iw_rdma_mr_free);
		list_del(&ibmr->mapping.m_list);
		rds_iw_destroy_fastreg(pool, ibmr);
		kfree(ibmr);
		nfreed++;
	}

	/* Anything that remains are laundered ibmrs, which we can add
	 * back to the clean list. */
	if (!list_empty(&unmap_list)) {
		spin_lock_irqsave(&pool->list_lock, flags);
		list_splice(&unmap_list, &pool->clean_list);
		spin_unlock_irqrestore(&pool->list_lock, flags);
	}

	atomic_sub(unpinned, &pool->free_pinned);
	atomic_sub(ncleaned, &pool->dirty_count);
	atomic_sub(nfreed, &pool->item_count);

	mutex_unlock(&pool->flush_lock);
	return ret;
}

static void rds_iw_mr_pool_flush_worker(struct work_struct *work)
{
	struct rds_iw_mr_pool *pool = container_of(work, struct rds_iw_mr_pool, flush_worker);

	rds_iw_flush_mr_pool(pool, 0);
}

void rds_iw_free_mr(void *trans_private, int invalidate)
{
	struct rds_iw_mr *ibmr = trans_private;
	struct rds_iw_mr_pool *pool = ibmr->device->mr_pool;

	rdsdebug("RDS/IW: free_mr nents %u\n", ibmr->mapping.m_sg.len);
	if (!pool)
		return;

	/* Return it to the pool's free list */
	rds_iw_free_fastreg(pool, ibmr);

	/* If we've pinned too many pages, request a flush */
	if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned ||
	    atomic_read(&pool->dirty_count) >= pool->max_items / 10)
		queue_work(rds_wq, &pool->flush_worker);

	if (invalidate) {
		if (likely(!in_interrupt())) {
			rds_iw_flush_mr_pool(pool, 0);
		} else {
			/* We get here if the user created a MR marked
			 * as use_once and invalidate at the same time. */
			queue_work(rds_wq, &pool->flush_worker);
		}
	}
}

void rds_iw_flush_mrs(void)
{
	struct rds_iw_device *rds_iwdev;

	list_for_each_entry(rds_iwdev, &rds_iw_devices, list) {
		struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool;

		if (pool)
			rds_iw_flush_mr_pool(pool, 0);
	}
}

void *rds_iw_get_mr(struct scatterlist *sg, unsigned long nents,
		    struct rds_sock *rs, u32 *key_ret)
{
	struct rds_iw_device *rds_iwdev;
	struct rds_iw_mr *ibmr = NULL;
	struct rdma_cm_id *cm_id;
	int ret;

	ret = rds_iw_get_device(rs, &rds_iwdev, &cm_id);
	if (ret || !cm_id) {
		ret = -ENODEV;
		goto out;
	}

	if (!rds_iwdev->mr_pool) {
		ret = -ENODEV;
		goto out;
	}

	ibmr = rds_iw_alloc_mr(rds_iwdev);
	if (IS_ERR(ibmr))
		return ibmr;

	ibmr->cm_id = cm_id;
	ibmr->device = rds_iwdev;

	ret = rds_iw_map_fastreg(rds_iwdev->mr_pool, ibmr, sg, nents);
	if (ret == 0)
		*key_ret = ibmr->mr->rkey;
	else
		printk(KERN_WARNING "RDS/IW: failed to map mr (errno=%d)\n", ret);

out:
	if (ret) {
		if (ibmr)
			rds_iw_free_mr(ibmr, 0);
		ibmr = ERR_PTR(ret);
	}
	return ibmr;
}

/*
 * iWARP fastreg handling
 *
 * The life cycle of a fastreg registration is a bit different from
 * FMRs.
 * The idea behind fastreg is to have one MR, to which we bind different
 * mappings over time. To avoid stalling on the expensive map and invalidate
 * operations, these operations are pipelined on the same send queue on
 * which we want to send the message containing the r_key.
 *
 * This creates a bit of a problem for us, as we do not have the destination
 * IP in GET_MR, so the connection must be setup prior to the GET_MR call for
 * RDMA to be correctly setup.  If a fastreg request is present, rds_iw_xmit
 * will try to queue a LOCAL_INV (if needed) and a FAST_REG_MR work request
 * before queuing the SEND. When completions for these arrive, they are
 * dispatched to the MR has a bit set showing that RDMa can be performed.
 *
 * There is another interesting aspect that's related to invalidation.
 * The application can request that a mapping is invalidated in FREE_MR.
 * The expectation there is that this invalidation step includes ALL
 * PREVIOUSLY FREED MRs.
 */
static int rds_iw_init_fastreg(struct rds_iw_mr_pool *pool,
				struct rds_iw_mr *ibmr)
{
	struct rds_iw_device *rds_iwdev = pool->device;
	struct ib_fast_reg_page_list *page_list = NULL;
	struct ib_mr *mr;
	int err;

	mr = ib_alloc_fast_reg_mr(rds_iwdev->pd, pool->max_message_size);
	if (IS_ERR(mr)) {
		err = PTR_ERR(mr);

		printk(KERN_WARNING "RDS/IW: ib_alloc_fast_reg_mr failed (err=%d)\n", err);
		return err;
	}

	/* FIXME - this is overkill, but mapping->m_sg.dma_len/mapping->m_sg.dma_npages
	 * is not filled in.
	 */
	page_list = ib_alloc_fast_reg_page_list(rds_iwdev->dev, pool->max_message_size);
	if (IS_ERR(page_list)) {
		err = PTR_ERR(page_list);

		printk(KERN_WARNING "RDS/IW: ib_alloc_fast_reg_page_list failed (err=%d)\n", err);
		ib_dereg_mr(mr);
		return err;
	}

	ibmr->page_list = page_list;
	ibmr->mr = mr;
	return 0;
}

static int rds_iw_rdma_build_fastreg(struct rds_iw_mapping *mapping)
{
	struct rds_iw_mr *ibmr = mapping->m_mr;
	struct ib_send_wr f_wr, *failed_wr;
	int ret;

	/*
	 * Perform a WR for the fast_reg_mr. Each individual page
	 * in the sg list is added to the fast reg page list and placed
	 * inside the fast_reg_mr WR.  The key used is a rolling 8bit
	 * counter, which should guarantee uniqueness.
	 */
	ib_update_fast_reg_key(ibmr->mr, ibmr->remap_count++);
	mapping->m_rkey = ibmr->mr->rkey;

	memset(&f_wr, 0, sizeof(f_wr));
	f_wr.wr_id = RDS_IW_FAST_REG_WR_ID;
	f_wr.opcode = IB_WR_FAST_REG_MR;
	f_wr.wr.fast_reg.length = mapping->m_sg.bytes;
	f_wr.wr.fast_reg.rkey = mapping->m_rkey;
	f_wr.wr.fast_reg.page_list = ibmr->page_list;
	f_wr.wr.fast_reg.page_list_len = mapping->m_sg.dma_len;
	f_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
	f_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE |
				IB_ACCESS_REMOTE_READ |
				IB_ACCESS_REMOTE_WRITE;
	f_wr.wr.fast_reg.iova_start = 0;
	f_wr.send_flags = IB_SEND_SIGNALED;

	failed_wr = &f_wr;
	ret = ib_post_send(ibmr->cm_id->qp, &f_wr, &failed_wr);
	BUG_ON(failed_wr != &f_wr);
	if (ret)
		printk_ratelimited(KERN_WARNING "RDS/IW: %s:%d ib_post_send returned %d\n",
			__func__, __LINE__, ret);
	return ret;
}

static int rds_iw_rdma_fastreg_inv(struct rds_iw_mr *ibmr)
{
	struct ib_send_wr s_wr, *failed_wr;
	int ret = 0;

	if (!ibmr->cm_id->qp || !ibmr->mr)
		goto out;

	memset(&s_wr, 0, sizeof(s_wr));
	s_wr.wr_id = RDS_IW_LOCAL_INV_WR_ID;
	s_wr.opcode = IB_WR_LOCAL_INV;
	s_wr.ex.invalidate_rkey = ibmr->mr->rkey;
	s_wr.send_flags = IB_SEND_SIGNALED;

	failed_wr = &s_wr;
	ret = ib_post_send(ibmr->cm_id->qp, &s_wr, &failed_wr);
	if (ret) {
		printk_ratelimited(KERN_WARNING "RDS/IW: %s:%d ib_post_send returned %d\n",
			__func__, __LINE__, ret);
		goto out;
	}
out:
	return ret;
}

static int rds_iw_map_fastreg(struct rds_iw_mr_pool *pool,
			struct rds_iw_mr *ibmr,
			struct scatterlist *sg,
			unsigned int sg_len)
{
	struct rds_iw_device *rds_iwdev = pool->device;
	struct rds_iw_mapping *mapping = &ibmr->mapping;
	u64 *dma_pages;
	int i, ret = 0;

	rds_iw_set_scatterlist(&mapping->m_sg, sg, sg_len);

	dma_pages = rds_iw_map_scatterlist(rds_iwdev, &mapping->m_sg);
	if (IS_ERR(dma_pages)) {
		ret = PTR_ERR(dma_pages);
		dma_pages = NULL;
		goto out;
	}

	if (mapping->m_sg.dma_len > pool->max_message_size) {
		ret = -EMSGSIZE;
		goto out;
	}

	for (i = 0; i < mapping->m_sg.dma_npages; ++i)
		ibmr->page_list->page_list[i] = dma_pages[i];

	ret = rds_iw_rdma_build_fastreg(mapping);
	if (ret)
		goto out;

	rds_iw_stats_inc(s_iw_rdma_mr_used);

out:
	kfree(dma_pages);

	return ret;
}

/*
 * "Free" a fastreg MR.
 */
static void rds_iw_free_fastreg(struct rds_iw_mr_pool *pool,
		struct rds_iw_mr *ibmr)
{
	unsigned long flags;
	int ret;

	if (!ibmr->mapping.m_sg.dma_len)
		return;

	ret = rds_iw_rdma_fastreg_inv(ibmr);
	if (ret)
		return;

	/* Try to post the LOCAL_INV WR to the queue. */
	spin_lock_irqsave(&pool->list_lock, flags);

	list_add_tail(&ibmr->mapping.m_list, &pool->dirty_list);
	atomic_add(ibmr->mapping.m_sg.len, &pool->free_pinned);
	atomic_inc(&pool->dirty_count);

	spin_unlock_irqrestore(&pool->list_lock, flags);
}

static unsigned int rds_iw_unmap_fastreg_list(struct rds_iw_mr_pool *pool,
				struct list_head *unmap_list,
				struct list_head *kill_list,
				int *unpinned)
{
	struct rds_iw_mapping *mapping, *next;
	unsigned int ncleaned = 0;
	LIST_HEAD(laundered);

	/* Batched invalidation of fastreg MRs.
	 * Why do we do it this way, even though we could pipeline unmap
	 * and remap? The reason is the application semantics - when the
	 * application requests an invalidation of MRs, it expects all
	 * previously released R_Keys to become invalid.
	 *
	 * If we implement MR reuse naively, we risk memory corruption
	 * (this has actually been observed). So the default behavior
	 * requires that a MR goes through an explicit unmap operation before
	 * we can reuse it again.
	 *
	 * We could probably improve on this a little, by allowing immediate
	 * reuse of a MR on the same socket (eg you could add small
	 * cache of unused MRs to strct rds_socket - GET_MR could grab one
	 * of these without requiring an explicit invalidate).
	 */
	while (!list_empty(unmap_list)) {
		unsigned long flags;

		spin_lock_irqsave(&pool->list_lock, flags);
		list_for_each_entry_safe(mapping, next, unmap_list, m_list) {
			*unpinned += mapping->m_sg.len;
			list_move(&mapping->m_list, &laundered);
			ncleaned++;
		}
		spin_unlock_irqrestore(&pool->list_lock, flags);
	}

	/* Move all laundered mappings back to the unmap list.
	 * We do not kill any WRs right now - it doesn't seem the
	 * fastreg API has a max_remap limit. */
	list_splice_init(&laundered, unmap_list);

	return ncleaned;
}

static void rds_iw_destroy_fastreg(struct rds_iw_mr_pool *pool,
		struct rds_iw_mr *ibmr)
{
	if (ibmr->page_list)
		ib_free_fast_reg_page_list(ibmr->page_list);
	if (ibmr->mr)
		ib_dereg_mr(ibmr->mr);
}