xref: /freebsd/sys/ofed/include/rdma/rdmavt_mr.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1 #ifndef DEF_RDMAVT_INCMR_H
2 #define DEF_RDMAVT_INCMR_H
3 
4 /*-
5  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
6  *
7  * Copyright(c) 2016 Intel Corporation.
8  *
9  * This file is provided under a dual BSD/GPLv2 license.  When using or
10  * redistributing this file, you may do so under either license.
11  *
12  * GPL LICENSE SUMMARY
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of version 2 of the GNU General Public License as
16  * published by the Free Software Foundation.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * General Public License for more details.
22  *
23  * BSD LICENSE
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  *
29  *  - Redistributions of source code must retain the above copyright
30  *    notice, this list of conditions and the following disclaimer.
31  *  - Redistributions in binary form must reproduce the above copyright
32  *    notice, this list of conditions and the following disclaimer in
33  *    the documentation and/or other materials provided with the
34  *    distribution.
35  *  - Neither the name of Intel Corporation nor the names of its
36  *    contributors may be used to endorse or promote products derived
37  *    from this software without specific prior written permission.
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
40  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
41  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
42  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
43  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
46  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
47  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
48  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
49  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50  */
51 
52 /*
53  * For Memory Regions. This stuff should probably be moved into rdmavt/mr.h once
54  * drivers no longer need access to the MR directly.
55  */
56 
57 /*
58  * A segment is a linear region of low physical memory.
59  * Used by the verbs layer.
60  */
61 struct rvt_seg {
62 	void *vaddr;
63 	size_t length;
64 };
65 
66 /* The number of rvt_segs that fit in a page. */
67 #define RVT_SEGSZ     (PAGE_SIZE / sizeof(struct rvt_seg))
68 
69 struct rvt_segarray {
70 	struct rvt_seg segs[RVT_SEGSZ];
71 };
72 
73 struct rvt_mregion {
74 	struct ib_pd *pd;       /* shares refcnt of ibmr.pd */
75 	u64 user_base;          /* User's address for this region */
76 	u64 iova;               /* IB start address of this region */
77 	size_t length;
78 	u32 lkey;
79 	u32 offset;             /* offset (bytes) to start of region */
80 	int access_flags;
81 	u32 max_segs;           /* number of rvt_segs in all the arrays */
82 	u32 mapsz;              /* size of the map array */
83 	u8  page_shift;         /* 0 - non unform/non powerof2 sizes */
84 	u8  lkey_published;     /* in global table */
85 	atomic_t lkey_invalid;	/* true if current lkey is invalid */
86 	struct completion comp; /* complete when refcount goes to zero */
87 	atomic_t refcount;
88 	struct rvt_segarray *map[0];    /* the segments */
89 };
90 
91 #define RVT_MAX_LKEY_TABLE_BITS 23
92 
93 struct rvt_lkey_table {
94 	spinlock_t lock; /* protect changes in this struct */
95 	u32 next;               /* next unused index (speeds search) */
96 	u32 gen;                /* generation count */
97 	u32 max;                /* size of the table */
98 	struct rvt_mregion __rcu **table;
99 };
100 
101 /*
102  * These keep track of the copy progress within a memory region.
103  * Used by the verbs layer.
104  */
105 struct rvt_sge {
106 	struct rvt_mregion *mr;
107 	void *vaddr;            /* kernel virtual address of segment */
108 	u32 sge_length;         /* length of the SGE */
109 	u32 length;             /* remaining length of the segment */
110 	u16 m;                  /* current index: mr->map[m] */
111 	u16 n;                  /* current index: mr->map[m]->segs[n] */
112 };
113 
114 struct rvt_sge_state {
115 	struct rvt_sge *sg_list;      /* next SGE to be used if any */
116 	struct rvt_sge sge;   /* progress state for the current SGE */
117 	u32 total_len;
118 	u8 num_sge;
119 };
120 
rvt_put_mr(struct rvt_mregion * mr)121 static inline void rvt_put_mr(struct rvt_mregion *mr)
122 {
123 	if (unlikely(atomic_dec_and_test(&mr->refcount)))
124 		complete(&mr->comp);
125 }
126 
rvt_get_mr(struct rvt_mregion * mr)127 static inline void rvt_get_mr(struct rvt_mregion *mr)
128 {
129 	atomic_inc(&mr->refcount);
130 }
131 
rvt_put_ss(struct rvt_sge_state * ss)132 static inline void rvt_put_ss(struct rvt_sge_state *ss)
133 {
134 	while (ss->num_sge) {
135 		rvt_put_mr(ss->sge.mr);
136 		if (--ss->num_sge)
137 			ss->sge = *ss->sg_list++;
138 	}
139 }
140 
141 #endif          /* DEF_RDMAVT_INCMRH */
142