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
|
/*
* Intel MIC Platform Software Stack (MPSS)
*
* Copyright(c) 2015 Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* Intel SCIF driver.
*
*/
#include "scif_main.h"
#include <linux/mmu_notifier.h>
#include <linux/highmem.h>
/*
* scif_insert_tcw:
*
* Insert a temp window to the temp registration list sorted by va_for_temp.
* RMA lock must be held.
*/
void scif_insert_tcw(struct scif_window *window, struct list_head *head)
{
struct scif_window *curr = NULL;
struct scif_window *prev = list_entry(head, struct scif_window, list);
struct list_head *item;
INIT_LIST_HEAD(&window->list);
/* Compare with tail and if the entry is new tail add it to the end */
if (!list_empty(head)) {
curr = list_entry(head->prev, struct scif_window, list);
if (curr->va_for_temp < window->va_for_temp) {
list_add_tail(&window->list, head);
return;
}
}
list_for_each(item, head) {
curr = list_entry(item, struct scif_window, list);
if (curr->va_for_temp > window->va_for_temp)
break;
prev = curr;
}
list_add(&window->list, &prev->list);
}
/*
* scif_insert_window:
*
* Insert a window to the self registration list sorted by offset.
* RMA lock must be held.
*/
void scif_insert_window(struct scif_window *window, struct list_head *head)
{
struct scif_window *curr = NULL, *prev = NULL;
struct list_head *item;
INIT_LIST_HEAD(&window->list);
list_for_each(item, head) {
curr = list_entry(item, struct scif_window, list);
if (curr->offset > window->offset)
break;
prev = curr;
}
if (!prev)
list_add(&window->list, head);
else
list_add(&window->list, &prev->list);
scif_set_window_ref(window, window->nr_pages);
}
/*
* scif_query_tcw:
*
* Query the temp cached registration list of ep for an overlapping window
* in case of permission mismatch, destroy the previous window. if permissions
* match and overlap is partial, destroy the window but return the new range
* RMA lock must be held.
*/
int scif_query_tcw(struct scif_endpt *ep, struct scif_rma_req *req)
{
struct list_head *item, *temp, *head = req->head;
struct scif_window *window;
u64 start_va_window, start_va_req = req->va_for_temp;
u64 end_va_window, end_va_req = start_va_req + req->nr_bytes;
if (!req->nr_bytes)
return -EINVAL;
/*
* Avoid traversing the entire list to find out that there
* is no entry that matches
*/
if (!list_empty(head)) {
window = list_last_entry(head, struct scif_window, list);
end_va_window = window->va_for_temp +
(window->nr_pages << PAGE_SHIFT);
if (start_va_req > end_va_window)
return -ENXIO;
}
list_for_each_safe(item, temp, head) {
window = list_entry(item, struct scif_window, list);
start_va_window = window->va_for_temp;
end_va_window = window->va_for_temp +
(window->nr_pages << PAGE_SHIFT);
if (start_va_req < start_va_window &&
end_va_req < start_va_window)
break;
if (start_va_req >= end_va_window)
continue;
if ((window->prot & req->prot) == req->prot) {
if (start_va_req >= start_va_window &&
end_va_req <= end_va_window) {
*req->out_window = window;
return 0;
}
/* expand window */
if (start_va_req < start_va_window) {
req->nr_bytes +=
start_va_window - start_va_req;
req->va_for_temp = start_va_window;
}
if (end_va_req >= end_va_window)
req->nr_bytes += end_va_window - end_va_req;
}
/* Destroy the old window to create a new one */
__scif_rma_destroy_tcw_helper(window);
break;
}
return -ENXIO;
}
/*
* scif_query_window:
*
* Query the registration list and check if a valid contiguous
* range of windows exist.
* RMA lock must be held.
*/
int scif_query_window(struct scif_rma_req *req)
{
struct list_head *item;
struct scif_window *window;
s64 end_offset, offset = req->offset;
u64 tmp_min, nr_bytes_left = req->nr_bytes;
if (!req->nr_bytes)
return -EINVAL;
list_for_each(item, req->head) {
window = list_entry(item, struct scif_window, list);
end_offset = window->offset +
(window->nr_pages << PAGE_SHIFT);
if (offset < window->offset)
/* Offset not found! */
return -ENXIO;
if (offset >= end_offset)
continue;
/* Check read/write protections. */
if ((window->prot & req->prot) != req->prot)
return -EPERM;
if (nr_bytes_left == req->nr_bytes)
/* Store the first window */
*req->out_window = window;
tmp_min = min((u64)end_offset - offset, nr_bytes_left);
nr_bytes_left -= tmp_min;
offset += tmp_min;
/*
* Range requested encompasses
* multiple windows contiguously.
*/
if (!nr_bytes_left) {
/* Done for partial window */
if (req->type == SCIF_WINDOW_PARTIAL ||
req->type == SCIF_WINDOW_SINGLE)
return 0;
/* Extra logic for full windows */
if (offset == end_offset)
/* Spanning multiple whole windows */
return 0;
/* Not spanning multiple whole windows */
return -ENXIO;
}
if (req->type == SCIF_WINDOW_SINGLE)
break;
}
dev_err(scif_info.mdev.this_device,
"%s %d ENXIO\n", __func__, __LINE__);
return -ENXIO;
}
/*
* scif_rma_list_unregister:
*
* Traverse the self registration list starting from window:
* 1) Call scif_unregister_window(..)
* RMA lock must be held.
*/
int scif_rma_list_unregister(struct scif_window *window,
s64 offset, int nr_pages)
{
struct scif_endpt *ep = (struct scif_endpt *)window->ep;
struct list_head *head = &ep->rma_info.reg_list;
s64 end_offset;
int err = 0;
int loop_nr_pages;
struct scif_window *_window;
list_for_each_entry_safe_from(window, _window, head, list) {
end_offset = window->offset + (window->nr_pages << PAGE_SHIFT);
loop_nr_pages = min((int)((end_offset - offset) >> PAGE_SHIFT),
nr_pages);
err = scif_unregister_window(window);
if (err)
return err;
nr_pages -= loop_nr_pages;
offset += (loop_nr_pages << PAGE_SHIFT);
if (!nr_pages)
break;
}
return 0;
}
/*
* scif_unmap_all_window:
*
* Traverse all the windows in the self registration list and:
* 1) Delete any DMA mappings created
*/
void scif_unmap_all_windows(scif_epd_t epd)
{
struct list_head *item, *tmp;
struct scif_window *window;
struct scif_endpt *ep = (struct scif_endpt *)epd;
struct list_head *head = &ep->rma_info.reg_list;
mutex_lock(&ep->rma_info.rma_lock);
list_for_each_safe(item, tmp, head) {
window = list_entry(item, struct scif_window, list);
scif_unmap_window(ep->remote_dev, window);
}
mutex_unlock(&ep->rma_info.rma_lock);
}
/*
* scif_unregister_all_window:
*
* Traverse all the windows in the self registration list and:
* 1) Call scif_unregister_window(..)
* RMA lock must be held.
*/
int scif_unregister_all_windows(scif_epd_t epd)
{
struct list_head *item, *tmp;
struct scif_window *window;
struct scif_endpt *ep = (struct scif_endpt *)epd;
struct list_head *head = &ep->rma_info.reg_list;
int err = 0;
mutex_lock(&ep->rma_info.rma_lock);
retry:
item = NULL;
tmp = NULL;
list_for_each_safe(item, tmp, head) {
window = list_entry(item, struct scif_window, list);
ep->rma_info.async_list_del = 0;
err = scif_unregister_window(window);
if (err)
dev_err(scif_info.mdev.this_device,
"%s %d err %d\n",
__func__, __LINE__, err);
/*
* Need to restart list traversal if there has been
* an asynchronous list entry deletion.
*/
if (READ_ONCE(ep->rma_info.async_list_del))
goto retry;
}
mutex_unlock(&ep->rma_info.rma_lock);
if (!list_empty(&ep->rma_info.mmn_list)) {
spin_lock(&scif_info.rmalock);
list_add_tail(&ep->mmu_list, &scif_info.mmu_notif_cleanup);
spin_unlock(&scif_info.rmalock);
schedule_work(&scif_info.mmu_notif_work);
}
return err;
}
|