xref: /freebsd/sys/ofed/include/rdma/rdmavt_mr.h (revision 81ad6265)
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  * $FreeBSD$
52  */
53 
54 /*
55  * For Memory Regions. This stuff should probably be moved into rdmavt/mr.h once
56  * drivers no longer need access to the MR directly.
57  */
58 
59 /*
60  * A segment is a linear region of low physical memory.
61  * Used by the verbs layer.
62  */
63 struct rvt_seg {
64 	void *vaddr;
65 	size_t length;
66 };
67 
68 /* The number of rvt_segs that fit in a page. */
69 #define RVT_SEGSZ     (PAGE_SIZE / sizeof(struct rvt_seg))
70 
71 struct rvt_segarray {
72 	struct rvt_seg segs[RVT_SEGSZ];
73 };
74 
75 struct rvt_mregion {
76 	struct ib_pd *pd;       /* shares refcnt of ibmr.pd */
77 	u64 user_base;          /* User's address for this region */
78 	u64 iova;               /* IB start address of this region */
79 	size_t length;
80 	u32 lkey;
81 	u32 offset;             /* offset (bytes) to start of region */
82 	int access_flags;
83 	u32 max_segs;           /* number of rvt_segs in all the arrays */
84 	u32 mapsz;              /* size of the map array */
85 	u8  page_shift;         /* 0 - non unform/non powerof2 sizes */
86 	u8  lkey_published;     /* in global table */
87 	atomic_t lkey_invalid;	/* true if current lkey is invalid */
88 	struct completion comp; /* complete when refcount goes to zero */
89 	atomic_t refcount;
90 	struct rvt_segarray *map[0];    /* the segments */
91 };
92 
93 #define RVT_MAX_LKEY_TABLE_BITS 23
94 
95 struct rvt_lkey_table {
96 	spinlock_t lock; /* protect changes in this struct */
97 	u32 next;               /* next unused index (speeds search) */
98 	u32 gen;                /* generation count */
99 	u32 max;                /* size of the table */
100 	struct rvt_mregion __rcu **table;
101 };
102 
103 /*
104  * These keep track of the copy progress within a memory region.
105  * Used by the verbs layer.
106  */
107 struct rvt_sge {
108 	struct rvt_mregion *mr;
109 	void *vaddr;            /* kernel virtual address of segment */
110 	u32 sge_length;         /* length of the SGE */
111 	u32 length;             /* remaining length of the segment */
112 	u16 m;                  /* current index: mr->map[m] */
113 	u16 n;                  /* current index: mr->map[m]->segs[n] */
114 };
115 
116 struct rvt_sge_state {
117 	struct rvt_sge *sg_list;      /* next SGE to be used if any */
118 	struct rvt_sge sge;   /* progress state for the current SGE */
119 	u32 total_len;
120 	u8 num_sge;
121 };
122 
123 static inline void rvt_put_mr(struct rvt_mregion *mr)
124 {
125 	if (unlikely(atomic_dec_and_test(&mr->refcount)))
126 		complete(&mr->comp);
127 }
128 
129 static inline void rvt_get_mr(struct rvt_mregion *mr)
130 {
131 	atomic_inc(&mr->refcount);
132 }
133 
134 static inline void rvt_put_ss(struct rvt_sge_state *ss)
135 {
136 	while (ss->num_sge) {
137 		rvt_put_mr(ss->sge.mr);
138 		if (--ss->num_sge)
139 			ss->sge = *ss->sg_list++;
140 	}
141 }
142 
143 #endif          /* DEF_RDMAVT_INCMRH */
144