blob: 3baf71944471f5e1df29997e1851ce6c65c325ca [file] [log] [blame]
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001/*
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07002 * Copyright(c) 2015-2017 Intel Corporation.
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05003 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
Mitko Haralanovf727a0c2016-02-05 11:57:46 -050020 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47#include <asm/page.h>
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -080048#include <linux/string.h>
Mitko Haralanovf727a0c2016-02-05 11:57:46 -050049
50#include "user_exp_rcv.h"
51#include "trace.h"
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080052#include "mmu_rb.h"
Mitko Haralanovf727a0c2016-02-05 11:57:46 -050053
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080054struct tid_rb_node {
55 struct mmu_rb_node mmu;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -050056 unsigned long phys;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -050057 struct tid_group *grp;
58 u32 rcventry;
59 dma_addr_t dma_addr;
60 bool freed;
61 unsigned npages;
62 struct page *pages[0];
63};
64
Mitko Haralanovf88e0c82016-02-05 11:57:52 -050065struct tid_pageset {
66 u16 idx;
67 u16 count;
68};
69
Mitko Haralanov3abb33a2016-02-05 11:57:54 -050070#define num_user_pages(vaddr, len) \
71 (1 + (((((unsigned long)(vaddr) + \
72 (unsigned long)(len) - 1) & PAGE_MASK) - \
73 ((unsigned long)vaddr & PAGE_MASK)) >> PAGE_SHIFT))
74
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070075static void unlock_exp_tids(struct hfi1_ctxtdata *uctxt,
76 struct exp_tid_set *set,
77 struct hfi1_filedata *fd);
78static u32 find_phys_blocks(struct page **pages, unsigned npages,
79 struct tid_pageset *list);
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -070080static int set_rcvarray_entry(struct hfi1_filedata *fd, unsigned long vaddr,
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070081 u32 rcventry, struct tid_group *grp,
82 struct page **pages, unsigned npages);
83static int tid_rb_insert(void *arg, struct mmu_rb_node *node);
Ira Weiny2677a762016-07-28 15:21:26 -040084static void cacheless_tid_rb_remove(struct hfi1_filedata *fdata,
85 struct tid_rb_node *tnode);
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070086static void tid_rb_remove(void *arg, struct mmu_rb_node *node);
87static int tid_rb_invalidate(void *arg, struct mmu_rb_node *mnode);
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -070088static int program_rcvarray(struct hfi1_filedata *fd, unsigned long vaddr,
89 struct tid_group *grp, struct tid_pageset *sets,
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070090 unsigned start, u16 count, struct page **pages,
91 u32 *tidlist, unsigned *tididx, unsigned *pmapped);
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -070092static int unprogram_rcvarray(struct hfi1_filedata *fd, u32 tidinfo,
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070093 struct tid_group **grp);
Ira Weiny2677a762016-07-28 15:21:26 -040094static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080095
96static struct mmu_rb_ops tid_rb_ops = {
Dean Luicka7cd2dc2016-07-28 12:27:37 -040097 .insert = tid_rb_insert,
98 .remove = tid_rb_remove,
99 .invalidate = tid_rb_invalidate
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800100};
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500101
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500102/*
103 * Initialize context and file private data needed for Expected
104 * receive caching. This needs to be done after the context has
105 * been configured with the eager/expected RcvEntry counts.
106 */
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700107int hfi1_user_exp_rcv_init(struct hfi1_filedata *fd)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500108{
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500109 struct hfi1_ctxtdata *uctxt = fd->uctxt;
110 struct hfi1_devdata *dd = uctxt->dd;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700111 int ret = 0;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500112
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500113 spin_lock_init(&fd->tid_lock);
114 spin_lock_init(&fd->invalid_lock);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500115
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800116 fd->entry_to_rb = kcalloc(uctxt->expected_count,
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700117 sizeof(struct rb_node *),
118 GFP_KERNEL);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800119 if (!fd->entry_to_rb)
120 return -ENOMEM;
121
Dean Luick622c2022016-07-28 15:21:21 -0400122 if (!HFI1_CAP_UGET_MASK(uctxt->flags, TID_UNMAP)) {
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500123 fd->invalid_tid_idx = 0;
Markus Elfring4076e512017-02-09 15:30:53 +0100124 fd->invalid_tids = kcalloc(uctxt->expected_count,
125 sizeof(*fd->invalid_tids),
126 GFP_KERNEL);
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700127 if (!fd->invalid_tids) {
128 kfree(fd->entry_to_rb);
129 fd->entry_to_rb = NULL;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700130 return -ENOMEM;
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700131 }
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800132
133 /*
134 * Register MMU notifier callbacks. If the registration
Dean Luick622c2022016-07-28 15:21:21 -0400135 * fails, continue without TID caching for this context.
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800136 */
Dean Luickb85ced92016-07-28 15:21:24 -0400137 ret = hfi1_mmu_rb_register(fd, fd->mm, &tid_rb_ops,
138 dd->pport->hfi1_wq,
139 &fd->handler);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800140 if (ret) {
141 dd_dev_info(dd,
142 "Failed MMU notifier registration %d\n",
143 ret);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800144 ret = 0;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500145 }
146 }
147
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500148 /*
149 * PSM does not have a good way to separate, count, and
150 * effectively enforce a limit on RcvArray entries used by
151 * subctxts (when context sharing is used) when TID caching
152 * is enabled. To help with that, we calculate a per-process
153 * RcvArray entry share and enforce that.
154 * If TID caching is not in use, PSM deals with usage on its
155 * own. In that case, we allow any subctxt to take all of the
156 * entries.
157 *
158 * Make sure that we set the tid counts only after successful
159 * init.
160 */
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500161 spin_lock(&fd->tid_lock);
Dean Luick622c2022016-07-28 15:21:21 -0400162 if (uctxt->subctxt_cnt && fd->handler) {
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500163 u16 remainder;
164
165 fd->tid_limit = uctxt->expected_count / uctxt->subctxt_cnt;
166 remainder = uctxt->expected_count % uctxt->subctxt_cnt;
167 if (remainder && fd->subctxt < remainder)
168 fd->tid_limit++;
169 } else {
170 fd->tid_limit = uctxt->expected_count;
171 }
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500172 spin_unlock(&fd->tid_lock);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700173
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500174 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500175}
176
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700177void hfi1_user_exp_rcv_free(struct hfi1_filedata *fd)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500178{
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500179 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500180
181 /*
182 * The notifier would have been removed when the process'es mm
183 * was freed.
184 */
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700185 if (fd->handler) {
Dean Luicke0b09ac2016-07-28 15:21:20 -0400186 hfi1_mmu_rb_unregister(fd->handler);
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700187 } else {
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500188 if (!EXP_TID_SET_EMPTY(uctxt->tid_full_list))
Dean Luicke0b09ac2016-07-28 15:21:20 -0400189 unlock_exp_tids(uctxt, &uctxt->tid_full_list, fd);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500190 if (!EXP_TID_SET_EMPTY(uctxt->tid_used_list))
Dean Luicke0b09ac2016-07-28 15:21:20 -0400191 unlock_exp_tids(uctxt, &uctxt->tid_used_list, fd);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500192 }
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800193
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700194 kfree(fd->invalid_tids);
195 fd->invalid_tids = NULL;
196
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800197 kfree(fd->entry_to_rb);
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700198 fd->entry_to_rb = NULL;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500199}
200
Mitko Haralanovb8abe342016-02-05 11:57:51 -0500201/*
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500202 * RcvArray entry allocation for Expected Receives is done by the
203 * following algorithm:
204 *
205 * The context keeps 3 lists of groups of RcvArray entries:
206 * 1. List of empty groups - tid_group_list
207 * This list is created during user context creation and
208 * contains elements which describe sets (of 8) of empty
209 * RcvArray entries.
210 * 2. List of partially used groups - tid_used_list
211 * This list contains sets of RcvArray entries which are
212 * not completely used up. Another mapping request could
213 * use some of all of the remaining entries.
214 * 3. List of full groups - tid_full_list
215 * This is the list where sets that are completely used
216 * up go.
217 *
218 * An attempt to optimize the usage of RcvArray entries is
219 * made by finding all sets of physically contiguous pages in a
220 * user's buffer.
221 * These physically contiguous sets are further split into
222 * sizes supported by the receive engine of the HFI. The
223 * resulting sets of pages are stored in struct tid_pageset,
224 * which describes the sets as:
225 * * .count - number of pages in this set
226 * * .idx - starting index into struct page ** array
227 * of this set
228 *
229 * From this point on, the algorithm deals with the page sets
230 * described above. The number of pagesets is divided by the
231 * RcvArray group size to produce the number of full groups
232 * needed.
233 *
234 * Groups from the 3 lists are manipulated using the following
235 * rules:
236 * 1. For each set of 8 pagesets, a complete group from
237 * tid_group_list is taken, programmed, and moved to
238 * the tid_full_list list.
239 * 2. For all remaining pagesets:
240 * 2.1 If the tid_used_list is empty and the tid_group_list
241 * is empty, stop processing pageset and return only
242 * what has been programmed up to this point.
243 * 2.2 If the tid_used_list is empty and the tid_group_list
244 * is not empty, move a group from tid_group_list to
245 * tid_used_list.
246 * 2.3 For each group is tid_used_group, program as much as
247 * can fit into the group. If the group becomes fully
248 * used, move it to tid_full_list.
249 */
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700250int hfi1_user_exp_rcv_setup(struct hfi1_filedata *fd,
251 struct hfi1_tid_info *tinfo)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500252{
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500253 int ret = 0, need_group = 0, pinned;
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500254 struct hfi1_ctxtdata *uctxt = fd->uctxt;
255 struct hfi1_devdata *dd = uctxt->dd;
256 unsigned npages, ngroups, pageidx = 0, pageset_count, npagesets,
257 tididx = 0, mapped, mapped_pages = 0;
258 unsigned long vaddr = tinfo->vaddr;
259 struct page **pages = NULL;
260 u32 *tidlist = NULL;
261 struct tid_pageset *pagesets = NULL;
262
263 /* Get the number of pages the user buffer spans */
264 npages = num_user_pages(vaddr, tinfo->length);
265 if (!npages)
266 return -EINVAL;
267
268 if (npages > uctxt->expected_count) {
269 dd_dev_err(dd, "Expected buffer too big\n");
270 return -EINVAL;
271 }
272
273 /* Verify that access is OK for the user buffer */
274 if (!access_ok(VERIFY_WRITE, (void __user *)vaddr,
275 npages * PAGE_SIZE)) {
276 dd_dev_err(dd, "Fail vaddr %p, %u pages, !access_ok\n",
277 (void *)vaddr, npages);
278 return -EFAULT;
279 }
280
281 pagesets = kcalloc(uctxt->expected_count, sizeof(*pagesets),
282 GFP_KERNEL);
283 if (!pagesets)
284 return -ENOMEM;
285
286 /* Allocate the array of struct page pointers needed for pinning */
287 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
288 if (!pages) {
289 ret = -ENOMEM;
290 goto bail;
291 }
292
293 /*
294 * Pin all the pages of the user buffer. If we can't pin all the
295 * pages, accept the amount pinned so far and program only that.
296 * User space knows how to deal with partially programmed buffers.
297 */
Ira Weiny3faa3d92016-07-28 15:21:19 -0400298 if (!hfi1_can_pin_pages(dd, fd->mm, fd->tid_n_pinned, npages)) {
Mitko Haralanov0ad2d3d2016-04-12 10:46:29 -0700299 ret = -ENOMEM;
300 goto bail;
301 }
302
Ira Weiny3faa3d92016-07-28 15:21:19 -0400303 pinned = hfi1_acquire_user_pages(fd->mm, vaddr, npages, true, pages);
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500304 if (pinned <= 0) {
305 ret = pinned;
306 goto bail;
307 }
Mitko Haralanova7922f72016-03-08 11:15:39 -0800308 fd->tid_n_pinned += npages;
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500309
310 /* Find sets of physically contiguous pages */
311 npagesets = find_phys_blocks(pages, pinned, pagesets);
312
313 /*
314 * We don't need to access this under a lock since tid_used is per
315 * process and the same process cannot be in hfi1_user_exp_rcv_clear()
316 * and hfi1_user_exp_rcv_setup() at the same time.
317 */
318 spin_lock(&fd->tid_lock);
319 if (fd->tid_used + npagesets > fd->tid_limit)
320 pageset_count = fd->tid_limit - fd->tid_used;
321 else
322 pageset_count = npagesets;
323 spin_unlock(&fd->tid_lock);
324
325 if (!pageset_count)
326 goto bail;
327
328 ngroups = pageset_count / dd->rcv_entries.group_size;
329 tidlist = kcalloc(pageset_count, sizeof(*tidlist), GFP_KERNEL);
330 if (!tidlist) {
331 ret = -ENOMEM;
332 goto nomem;
333 }
334
335 tididx = 0;
336
337 /*
338 * From this point on, we are going to be using shared (between master
339 * and subcontexts) context resources. We need to take the lock.
340 */
341 mutex_lock(&uctxt->exp_lock);
342 /*
343 * The first step is to program the RcvArray entries which are complete
344 * groups.
345 */
346 while (ngroups && uctxt->tid_group_list.count) {
347 struct tid_group *grp =
348 tid_group_pop(&uctxt->tid_group_list);
349
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700350 ret = program_rcvarray(fd, vaddr, grp, pagesets,
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500351 pageidx, dd->rcv_entries.group_size,
352 pages, tidlist, &tididx, &mapped);
353 /*
354 * If there was a failure to program the RcvArray
355 * entries for the entire group, reset the grp fields
356 * and add the grp back to the free group list.
357 */
358 if (ret <= 0) {
359 tid_group_add_tail(grp, &uctxt->tid_group_list);
360 hfi1_cdbg(TID,
361 "Failed to program RcvArray group %d", ret);
362 goto unlock;
363 }
364
365 tid_group_add_tail(grp, &uctxt->tid_full_list);
366 ngroups--;
367 pageidx += ret;
368 mapped_pages += mapped;
369 }
370
371 while (pageidx < pageset_count) {
372 struct tid_group *grp, *ptr;
373 /*
374 * If we don't have any partially used tid groups, check
375 * if we have empty groups. If so, take one from there and
376 * put in the partially used list.
377 */
378 if (!uctxt->tid_used_list.count || need_group) {
379 if (!uctxt->tid_group_list.count)
380 goto unlock;
381
382 grp = tid_group_pop(&uctxt->tid_group_list);
383 tid_group_add_tail(grp, &uctxt->tid_used_list);
384 need_group = 0;
385 }
386 /*
387 * There is an optimization opportunity here - instead of
388 * fitting as many page sets as we can, check for a group
389 * later on in the list that could fit all of them.
390 */
391 list_for_each_entry_safe(grp, ptr, &uctxt->tid_used_list.list,
392 list) {
393 unsigned use = min_t(unsigned, pageset_count - pageidx,
394 grp->size - grp->used);
395
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700396 ret = program_rcvarray(fd, vaddr, grp, pagesets,
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500397 pageidx, use, pages, tidlist,
398 &tididx, &mapped);
399 if (ret < 0) {
400 hfi1_cdbg(TID,
401 "Failed to program RcvArray entries %d",
402 ret);
403 ret = -EFAULT;
404 goto unlock;
405 } else if (ret > 0) {
406 if (grp->used == grp->size)
407 tid_group_move(grp,
408 &uctxt->tid_used_list,
409 &uctxt->tid_full_list);
410 pageidx += ret;
411 mapped_pages += mapped;
412 need_group = 0;
413 /* Check if we are done so we break out early */
414 if (pageidx >= pageset_count)
415 break;
416 } else if (WARN_ON(ret == 0)) {
417 /*
418 * If ret is 0, we did not program any entries
419 * into this group, which can only happen if
420 * we've screwed up the accounting somewhere.
421 * Warn and try to continue.
422 */
423 need_group = 1;
424 }
425 }
426 }
427unlock:
428 mutex_unlock(&uctxt->exp_lock);
429nomem:
430 hfi1_cdbg(TID, "total mapped: tidpairs:%u pages:%u (%d)", tididx,
431 mapped_pages, ret);
432 if (tididx) {
433 spin_lock(&fd->tid_lock);
434 fd->tid_used += tididx;
435 spin_unlock(&fd->tid_lock);
436 tinfo->tidcnt = tididx;
437 tinfo->length = mapped_pages * PAGE_SIZE;
438
439 if (copy_to_user((void __user *)(unsigned long)tinfo->tidlist,
440 tidlist, sizeof(tidlist[0]) * tididx)) {
441 /*
442 * On failure to copy to the user level, we need to undo
443 * everything done so far so we don't leak resources.
444 */
445 tinfo->tidlist = (unsigned long)&tidlist;
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700446 hfi1_user_exp_rcv_clear(fd, tinfo);
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500447 tinfo->tidlist = 0;
448 ret = -EFAULT;
449 goto bail;
450 }
451 }
452
453 /*
454 * If not everything was mapped (due to insufficient RcvArray entries,
455 * for example), unpin all unmapped pages so we can pin them nex time.
456 */
Mitko Haralanova7922f72016-03-08 11:15:39 -0800457 if (mapped_pages != pinned) {
Ira Weiny3faa3d92016-07-28 15:21:19 -0400458 hfi1_release_user_pages(fd->mm, &pages[mapped_pages],
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500459 pinned - mapped_pages,
460 false);
Mitko Haralanova7922f72016-03-08 11:15:39 -0800461 fd->tid_n_pinned -= pinned - mapped_pages;
462 }
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500463bail:
464 kfree(pagesets);
465 kfree(pages);
466 kfree(tidlist);
467 return ret > 0 ? 0 : ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500468}
469
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700470int hfi1_user_exp_rcv_clear(struct hfi1_filedata *fd,
471 struct hfi1_tid_info *tinfo)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500472{
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500473 int ret = 0;
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500474 struct hfi1_ctxtdata *uctxt = fd->uctxt;
475 u32 *tidinfo;
476 unsigned tididx;
477
Michael J. Ruhldb730892017-04-09 10:16:03 -0700478 if (unlikely(tinfo->tidcnt > fd->tid_used))
479 return -EINVAL;
480
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800481 tidinfo = memdup_user((void __user *)(unsigned long)tinfo->tidlist,
482 sizeof(tidinfo[0]) * tinfo->tidcnt);
483 if (IS_ERR(tidinfo))
484 return PTR_ERR(tidinfo);
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500485
486 mutex_lock(&uctxt->exp_lock);
487 for (tididx = 0; tididx < tinfo->tidcnt; tididx++) {
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700488 ret = unprogram_rcvarray(fd, tidinfo[tididx], NULL);
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500489 if (ret) {
490 hfi1_cdbg(TID, "Failed to unprogram rcv array %d",
491 ret);
492 break;
493 }
494 }
495 spin_lock(&fd->tid_lock);
496 fd->tid_used -= tididx;
497 spin_unlock(&fd->tid_lock);
498 tinfo->tidcnt = tididx;
499 mutex_unlock(&uctxt->exp_lock);
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800500
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500501 kfree(tidinfo);
502 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500503}
504
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700505int hfi1_user_exp_rcv_invalid(struct hfi1_filedata *fd,
506 struct hfi1_tid_info *tinfo)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500507{
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500508 struct hfi1_ctxtdata *uctxt = fd->uctxt;
509 unsigned long *ev = uctxt->dd->events +
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700510 (((uctxt->ctxt - uctxt->dd->first_dyn_alloc_ctxt) *
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500511 HFI1_MAX_SHARED_CTXTS) + fd->subctxt);
512 u32 *array;
513 int ret = 0;
514
515 if (!fd->invalid_tids)
516 return -EINVAL;
517
518 /*
519 * copy_to_user() can sleep, which will leave the invalid_lock
520 * locked and cause the MMU notifier to be blocked on the lock
521 * for a long time.
522 * Copy the data to a local buffer so we can release the lock.
523 */
524 array = kcalloc(uctxt->expected_count, sizeof(*array), GFP_KERNEL);
525 if (!array)
526 return -EFAULT;
527
528 spin_lock(&fd->invalid_lock);
529 if (fd->invalid_tid_idx) {
530 memcpy(array, fd->invalid_tids, sizeof(*array) *
531 fd->invalid_tid_idx);
532 memset(fd->invalid_tids, 0, sizeof(*fd->invalid_tids) *
533 fd->invalid_tid_idx);
534 tinfo->tidcnt = fd->invalid_tid_idx;
535 fd->invalid_tid_idx = 0;
536 /*
537 * Reset the user flag while still holding the lock.
538 * Otherwise, PSM can miss events.
539 */
540 clear_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
541 } else {
542 tinfo->tidcnt = 0;
543 }
544 spin_unlock(&fd->invalid_lock);
545
546 if (tinfo->tidcnt) {
547 if (copy_to_user((void __user *)tinfo->tidlist,
548 array, sizeof(*array) * tinfo->tidcnt))
549 ret = -EFAULT;
550 }
551 kfree(array);
552
553 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500554}
555
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500556static u32 find_phys_blocks(struct page **pages, unsigned npages,
557 struct tid_pageset *list)
558{
559 unsigned pagecount, pageidx, setcount = 0, i;
560 unsigned long pfn, this_pfn;
561
562 if (!npages)
563 return 0;
564
565 /*
566 * Look for sets of physically contiguous pages in the user buffer.
567 * This will allow us to optimize Expected RcvArray entry usage by
568 * using the bigger supported sizes.
569 */
570 pfn = page_to_pfn(pages[0]);
571 for (pageidx = 0, pagecount = 1, i = 1; i <= npages; i++) {
572 this_pfn = i < npages ? page_to_pfn(pages[i]) : 0;
573
574 /*
575 * If the pfn's are not sequential, pages are not physically
576 * contiguous.
577 */
578 if (this_pfn != ++pfn) {
579 /*
580 * At this point we have to loop over the set of
581 * physically contiguous pages and break them down it
582 * sizes supported by the HW.
583 * There are two main constraints:
584 * 1. The max buffer size is MAX_EXPECTED_BUFFER.
585 * If the total set size is bigger than that
586 * program only a MAX_EXPECTED_BUFFER chunk.
587 * 2. The buffer size has to be a power of two. If
588 * it is not, round down to the closes power of
589 * 2 and program that size.
590 */
591 while (pagecount) {
592 int maxpages = pagecount;
593 u32 bufsize = pagecount * PAGE_SIZE;
594
595 if (bufsize > MAX_EXPECTED_BUFFER)
596 maxpages =
597 MAX_EXPECTED_BUFFER >>
598 PAGE_SHIFT;
599 else if (!is_power_of_2(bufsize))
600 maxpages =
601 rounddown_pow_of_two(bufsize) >>
602 PAGE_SHIFT;
603
604 list[setcount].idx = pageidx;
605 list[setcount].count = maxpages;
606 pagecount -= maxpages;
607 pageidx += maxpages;
608 setcount++;
609 }
610 pageidx = i;
611 pagecount = 1;
612 pfn = this_pfn;
613 } else {
614 pagecount++;
615 }
616 }
617 return setcount;
618}
619
620/**
621 * program_rcvarray() - program an RcvArray group with receive buffers
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700622 * @fd: filedata pointer
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500623 * @vaddr: starting user virtual address
624 * @grp: RcvArray group
625 * @sets: array of struct tid_pageset holding information on physically
626 * contiguous chunks from the user buffer
627 * @start: starting index into sets array
628 * @count: number of struct tid_pageset's to program
629 * @pages: an array of struct page * for the user buffer
630 * @tidlist: the array of u32 elements when the information about the
631 * programmed RcvArray entries is to be encoded.
632 * @tididx: starting offset into tidlist
633 * @pmapped: (output parameter) number of pages programmed into the RcvArray
634 * entries.
635 *
636 * This function will program up to 'count' number of RcvArray entries from the
637 * group 'grp'. To make best use of write-combining writes, the function will
638 * perform writes to the unused RcvArray entries which will be ignored by the
639 * HW. Each RcvArray entry will be programmed with a physically contiguous
640 * buffer chunk from the user's virtual buffer.
641 *
642 * Return:
643 * -EINVAL if the requested count is larger than the size of the group,
644 * -ENOMEM or -EFAULT on error from set_rcvarray_entry(), or
645 * number of RcvArray entries programmed.
646 */
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700647static int program_rcvarray(struct hfi1_filedata *fd, unsigned long vaddr,
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500648 struct tid_group *grp,
649 struct tid_pageset *sets,
650 unsigned start, u16 count, struct page **pages,
651 u32 *tidlist, unsigned *tididx, unsigned *pmapped)
652{
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500653 struct hfi1_ctxtdata *uctxt = fd->uctxt;
654 struct hfi1_devdata *dd = uctxt->dd;
655 u16 idx;
656 u32 tidinfo = 0, rcventry, useidx = 0;
657 int mapped = 0;
658
659 /* Count should never be larger than the group size */
660 if (count > grp->size)
661 return -EINVAL;
662
663 /* Find the first unused entry in the group */
664 for (idx = 0; idx < grp->size; idx++) {
665 if (!(grp->map & (1 << idx))) {
666 useidx = idx;
667 break;
668 }
669 rcv_array_wc_fill(dd, grp->base + idx);
670 }
671
672 idx = 0;
673 while (idx < count) {
674 u16 npages, pageidx, setidx = start + idx;
675 int ret = 0;
676
677 /*
678 * If this entry in the group is used, move to the next one.
679 * If we go past the end of the group, exit the loop.
680 */
681 if (useidx >= grp->size) {
682 break;
683 } else if (grp->map & (1 << useidx)) {
684 rcv_array_wc_fill(dd, grp->base + useidx);
685 useidx++;
686 continue;
687 }
688
689 rcventry = grp->base + useidx;
690 npages = sets[setidx].count;
691 pageidx = sets[setidx].idx;
692
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700693 ret = set_rcvarray_entry(fd, vaddr + (pageidx * PAGE_SIZE),
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500694 rcventry, grp, pages + pageidx,
695 npages);
696 if (ret)
697 return ret;
698 mapped += npages;
699
700 tidinfo = rcventry2tidinfo(rcventry - uctxt->expected_base) |
701 EXP_TID_SET(LEN, npages);
702 tidlist[(*tididx)++] = tidinfo;
703 grp->used++;
704 grp->map |= 1 << useidx++;
705 idx++;
706 }
707
708 /* Fill the rest of the group with "blank" writes */
709 for (; useidx < grp->size; useidx++)
710 rcv_array_wc_fill(dd, grp->base + useidx);
711 *pmapped = mapped;
712 return idx;
713}
714
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700715static int set_rcvarray_entry(struct hfi1_filedata *fd, unsigned long vaddr,
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500716 u32 rcventry, struct tid_group *grp,
717 struct page **pages, unsigned npages)
718{
719 int ret;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500720 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800721 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500722 struct hfi1_devdata *dd = uctxt->dd;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500723 dma_addr_t phys;
724
725 /*
726 * Allocate the node first so we can handle a potential
727 * failure before we've programmed anything.
728 */
729 node = kzalloc(sizeof(*node) + (sizeof(struct page *) * npages),
730 GFP_KERNEL);
731 if (!node)
732 return -ENOMEM;
733
734 phys = pci_map_single(dd->pcidev,
735 __va(page_to_phys(pages[0])),
736 npages * PAGE_SIZE, PCI_DMA_FROMDEVICE);
737 if (dma_mapping_error(&dd->pcidev->dev, phys)) {
738 dd_dev_err(dd, "Failed to DMA map Exp Rcv pages 0x%llx\n",
739 phys);
740 kfree(node);
741 return -EFAULT;
742 }
743
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800744 node->mmu.addr = vaddr;
745 node->mmu.len = npages * PAGE_SIZE;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500746 node->phys = page_to_phys(pages[0]);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500747 node->npages = npages;
748 node->rcventry = rcventry;
749 node->dma_addr = phys;
750 node->grp = grp;
751 node->freed = false;
752 memcpy(node->pages, pages, sizeof(struct page *) * npages);
753
Dean Luick622c2022016-07-28 15:21:21 -0400754 if (!fd->handler)
Dean Luicke0b09ac2016-07-28 15:21:20 -0400755 ret = tid_rb_insert(fd, &node->mmu);
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800756 else
Dean Luicke0b09ac2016-07-28 15:21:20 -0400757 ret = hfi1_mmu_rb_insert(fd->handler, &node->mmu);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500758
759 if (ret) {
760 hfi1_cdbg(TID, "Failed to insert RB node %u 0x%lx, 0x%lx %d",
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800761 node->rcventry, node->mmu.addr, node->phys, ret);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500762 pci_unmap_single(dd->pcidev, phys, npages * PAGE_SIZE,
763 PCI_DMA_FROMDEVICE);
764 kfree(node);
765 return -EFAULT;
766 }
767 hfi1_put_tid(dd, rcventry, PT_EXPECTED, phys, ilog2(npages) + 1);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800768 trace_hfi1_exp_tid_reg(uctxt->ctxt, fd->subctxt, rcventry, npages,
769 node->mmu.addr, node->phys, phys);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500770 return 0;
771}
772
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700773static int unprogram_rcvarray(struct hfi1_filedata *fd, u32 tidinfo,
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500774 struct tid_group **grp)
775{
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500776 struct hfi1_ctxtdata *uctxt = fd->uctxt;
777 struct hfi1_devdata *dd = uctxt->dd;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800778 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500779 u8 tidctrl = EXP_TID_GET(tidinfo, CTRL);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800780 u32 tididx = EXP_TID_GET(tidinfo, IDX) << 1, rcventry;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500781
782 if (tididx >= uctxt->expected_count) {
783 dd_dev_err(dd, "Invalid RcvArray entry (%u) index for ctxt %u\n",
784 tididx, uctxt->ctxt);
785 return -EINVAL;
786 }
787
788 if (tidctrl == 0x3)
789 return -EINVAL;
790
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800791 rcventry = tididx + (tidctrl - 1);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500792
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800793 node = fd->entry_to_rb[rcventry];
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800794 if (!node || node->rcventry != (uctxt->expected_base + rcventry))
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500795 return -EBADF;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800796
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500797 if (grp)
798 *grp = node->grp;
Ira Weiny2677a762016-07-28 15:21:26 -0400799
800 if (!fd->handler)
801 cacheless_tid_rb_remove(fd, node);
802 else
803 hfi1_mmu_rb_remove(fd->handler, &node->mmu);
804
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500805 return 0;
806}
807
Ira Weiny5ed3b152016-07-28 12:27:32 -0400808static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500809{
810 struct hfi1_ctxtdata *uctxt = fd->uctxt;
811 struct hfi1_devdata *dd = uctxt->dd;
812
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500813 trace_hfi1_exp_tid_unreg(uctxt->ctxt, fd->subctxt, node->rcventry,
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800814 node->npages, node->mmu.addr, node->phys,
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500815 node->dma_addr);
816
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500817 /*
818 * Make sure device has seen the write before we unpin the
819 * pages.
820 */
Mike Marciniszyncb51c5d2017-07-24 07:45:31 -0700821 hfi1_put_tid(dd, node->rcventry, PT_INVALID_FLUSH, 0, 0);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500822
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800823 pci_unmap_single(dd->pcidev, node->dma_addr, node->mmu.len,
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500824 PCI_DMA_FROMDEVICE);
Ira Weiny3faa3d92016-07-28 15:21:19 -0400825 hfi1_release_user_pages(fd->mm, node->pages, node->npages, true);
Mitko Haralanova7922f72016-03-08 11:15:39 -0800826 fd->tid_n_pinned -= node->npages;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500827
828 node->grp->used--;
829 node->grp->map &= ~(1 << (node->rcventry - node->grp->base));
830
831 if (node->grp->used == node->grp->size - 1)
832 tid_group_move(node->grp, &uctxt->tid_full_list,
833 &uctxt->tid_used_list);
834 else if (!node->grp->used)
835 tid_group_move(node->grp, &uctxt->tid_used_list,
836 &uctxt->tid_group_list);
837 kfree(node);
838}
839
Ira Weiny2677a762016-07-28 15:21:26 -0400840/*
841 * As a simple helper for hfi1_user_exp_rcv_free, this function deals with
842 * clearing nodes in the non-cached case.
843 */
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500844static void unlock_exp_tids(struct hfi1_ctxtdata *uctxt,
Dean Luicke0b09ac2016-07-28 15:21:20 -0400845 struct exp_tid_set *set,
846 struct hfi1_filedata *fd)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500847{
848 struct tid_group *grp, *ptr;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500849 int i;
850
851 list_for_each_entry_safe(grp, ptr, &set->list, list) {
852 list_del_init(&grp->list);
853
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500854 for (i = 0; i < grp->size; i++) {
855 if (grp->map & (1 << i)) {
856 u16 rcventry = grp->base + i;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800857 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500858
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800859 node = fd->entry_to_rb[rcventry -
860 uctxt->expected_base];
861 if (!node || node->rcventry != rcventry)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500862 continue;
Ira Weiny2677a762016-07-28 15:21:26 -0400863
864 cacheless_tid_rb_remove(fd, node);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500865 }
866 }
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500867 }
868}
869
Ira Weiny2677a762016-07-28 15:21:26 -0400870/*
871 * Always return 0 from this function. A non-zero return indicates that the
872 * remove operation will be called and that memory should be unpinned.
873 * However, the driver cannot unpin out from under PSM. Instead, retain the
874 * memory (by returning 0) and inform PSM that the memory is going away. PSM
875 * will call back later when it has removed the memory from its list.
876 */
Dean Luicke0b09ac2016-07-28 15:21:20 -0400877static int tid_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500878{
Dean Luicke0b09ac2016-07-28 15:21:20 -0400879 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800880 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
881 struct tid_rb_node *node =
882 container_of(mnode, struct tid_rb_node, mmu);
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500883
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800884 if (node->freed)
885 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500886
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800887 trace_hfi1_exp_tid_inval(uctxt->ctxt, fdata->subctxt, node->mmu.addr,
888 node->rcventry, node->npages, node->dma_addr);
889 node->freed = true;
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -0500890
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800891 spin_lock(&fdata->invalid_lock);
892 if (fdata->invalid_tid_idx < uctxt->expected_count) {
893 fdata->invalid_tids[fdata->invalid_tid_idx] =
894 rcventry2tidinfo(node->rcventry - uctxt->expected_base);
895 fdata->invalid_tids[fdata->invalid_tid_idx] |=
896 EXP_TID_SET(LEN, node->npages);
897 if (!fdata->invalid_tid_idx) {
898 unsigned long *ev;
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500899
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -0500900 /*
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800901 * hfi1_set_uevent_bits() sets a user event flag
902 * for all processes. Because calling into the
903 * driver to process TID cache invalidations is
904 * expensive and TID cache invalidations are
905 * handled on a per-process basis, we can
906 * optimize this to set the flag only for the
907 * process in question.
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -0500908 */
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800909 ev = uctxt->dd->events +
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700910 (((uctxt->ctxt - uctxt->dd->first_dyn_alloc_ctxt) *
911 HFI1_MAX_SHARED_CTXTS) + fdata->subctxt);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800912 set_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -0500913 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800914 fdata->invalid_tid_idx++;
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -0500915 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800916 spin_unlock(&fdata->invalid_lock);
917 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500918}
919
Dean Luicke0b09ac2016-07-28 15:21:20 -0400920static int tid_rb_insert(void *arg, struct mmu_rb_node *node)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500921{
Dean Luicke0b09ac2016-07-28 15:21:20 -0400922 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800923 struct tid_rb_node *tnode =
924 container_of(node, struct tid_rb_node, mmu);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800925 u32 base = fdata->uctxt->expected_base;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500926
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800927 fdata->entry_to_rb[tnode->rcventry - base] = tnode;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500928 return 0;
929}
930
Ira Weiny2677a762016-07-28 15:21:26 -0400931static void cacheless_tid_rb_remove(struct hfi1_filedata *fdata,
932 struct tid_rb_node *tnode)
933{
934 u32 base = fdata->uctxt->expected_base;
935
936 fdata->entry_to_rb[tnode->rcventry - base] = NULL;
937 clear_tid_node(fdata, tnode);
938}
939
Dean Luick082b3532016-07-28 15:21:25 -0400940static void tid_rb_remove(void *arg, struct mmu_rb_node *node)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500941{
Dean Luicke0b09ac2016-07-28 15:21:20 -0400942 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800943 struct tid_rb_node *tnode =
944 container_of(node, struct tid_rb_node, mmu);
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500945
Ira Weiny2677a762016-07-28 15:21:26 -0400946 cacheless_tid_rb_remove(fdata, tnode);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800947}