blob: 9b740db349632510ccb93c775aa8e2267071d5f9 [file] [log] [blame]
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001/*
Jubin John05d6ac12016-02-14 20:22:17 -08002 * Copyright(c) 2015, 2016 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>
48
49#include "user_exp_rcv.h"
50#include "trace.h"
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080051#include "mmu_rb.h"
Mitko Haralanovf727a0c2016-02-05 11:57:46 -050052
Mitko Haralanovb8abe342016-02-05 11:57:51 -050053struct tid_group {
54 struct list_head list;
55 unsigned base;
56 u8 size;
57 u8 used;
58 u8 map;
59};
60
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080061struct tid_rb_node {
62 struct mmu_rb_node mmu;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -050063 unsigned long phys;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -050064 struct tid_group *grp;
65 u32 rcventry;
66 dma_addr_t dma_addr;
67 bool freed;
68 unsigned npages;
69 struct page *pages[0];
70};
71
Mitko Haralanovf88e0c82016-02-05 11:57:52 -050072struct tid_pageset {
73 u16 idx;
74 u16 count;
75};
76
Mitko Haralanovb8abe342016-02-05 11:57:51 -050077#define EXP_TID_SET_EMPTY(set) (set.count == 0 && list_empty(&set.list))
78
Mitko Haralanov3abb33a2016-02-05 11:57:54 -050079#define num_user_pages(vaddr, len) \
80 (1 + (((((unsigned long)(vaddr) + \
81 (unsigned long)(len) - 1) & PAGE_MASK) - \
82 ((unsigned long)vaddr & PAGE_MASK)) >> PAGE_SHIFT))
83
Mitko Haralanovf88e0c82016-02-05 11:57:52 -050084static void unlock_exp_tids(struct hfi1_ctxtdata *, struct exp_tid_set *,
Dean Luicke0b09ac2016-07-28 15:21:20 -040085 struct hfi1_filedata *);
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -050086static u32 find_phys_blocks(struct page **, unsigned, struct tid_pageset *);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -050087static int set_rcvarray_entry(struct file *, unsigned long, u32,
Mitko Haralanov3abb33a2016-02-05 11:57:54 -050088 struct tid_group *, struct page **, unsigned);
Dean Luicke0b09ac2016-07-28 15:21:20 -040089static int tid_rb_insert(void *, struct mmu_rb_node *);
90static void tid_rb_remove(void *, struct mmu_rb_node *,
Mitko Haralanovf19bd642016-04-12 10:45:57 -070091 struct mm_struct *);
Dean Luicke0b09ac2016-07-28 15:21:20 -040092static int tid_rb_invalidate(void *, struct mmu_rb_node *);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -050093static int program_rcvarray(struct file *, unsigned long, struct tid_group *,
94 struct tid_pageset *, unsigned, u16, struct page **,
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -050095 u32 *, unsigned *, unsigned *);
Mitko Haralanov455d7f12016-02-05 11:57:56 -050096static int unprogram_rcvarray(struct file *, u32, struct tid_group **);
Ira Weiny5ed3b152016-07-28 12:27:32 -040097static void clear_tid_node(struct hfi1_filedata *, struct tid_rb_node *);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080098
99static struct mmu_rb_ops tid_rb_ops = {
Dean Luicka7cd2dc2016-07-28 12:27:37 -0400100 .insert = tid_rb_insert,
101 .remove = tid_rb_remove,
102 .invalidate = tid_rb_invalidate
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800103};
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500104
105static inline u32 rcventry2tidinfo(u32 rcventry)
106{
107 u32 pair = rcventry & ~0x1;
108
109 return EXP_TID_SET(IDX, pair >> 1) |
110 EXP_TID_SET(CTRL, 1 << (rcventry - pair));
111}
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500112
Mitko Haralanovb8abe342016-02-05 11:57:51 -0500113static inline void exp_tid_group_init(struct exp_tid_set *set)
114{
115 INIT_LIST_HEAD(&set->list);
116 set->count = 0;
117}
118
119static inline void tid_group_remove(struct tid_group *grp,
120 struct exp_tid_set *set)
121{
122 list_del_init(&grp->list);
123 set->count--;
124}
125
126static inline void tid_group_add_tail(struct tid_group *grp,
127 struct exp_tid_set *set)
128{
129 list_add_tail(&grp->list, &set->list);
130 set->count++;
131}
132
133static inline struct tid_group *tid_group_pop(struct exp_tid_set *set)
134{
135 struct tid_group *grp =
136 list_first_entry(&set->list, struct tid_group, list);
137 list_del_init(&grp->list);
138 set->count--;
139 return grp;
140}
141
142static inline void tid_group_move(struct tid_group *group,
143 struct exp_tid_set *s1,
144 struct exp_tid_set *s2)
145{
146 tid_group_remove(group, s1);
147 tid_group_add_tail(group, s2);
148}
149
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500150/*
151 * Initialize context and file private data needed for Expected
152 * receive caching. This needs to be done after the context has
153 * been configured with the eager/expected RcvEntry counts.
154 */
155int hfi1_user_exp_rcv_init(struct file *fp)
156{
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500157 struct hfi1_filedata *fd = fp->private_data;
158 struct hfi1_ctxtdata *uctxt = fd->uctxt;
159 struct hfi1_devdata *dd = uctxt->dd;
160 unsigned tidbase;
161 int i, ret = 0;
162
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500163 spin_lock_init(&fd->tid_lock);
164 spin_lock_init(&fd->invalid_lock);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500165
166 if (!uctxt->subctxt_cnt || !fd->subctxt) {
167 exp_tid_group_init(&uctxt->tid_group_list);
168 exp_tid_group_init(&uctxt->tid_used_list);
169 exp_tid_group_init(&uctxt->tid_full_list);
170
171 tidbase = uctxt->expected_base;
172 for (i = 0; i < uctxt->expected_count /
173 dd->rcv_entries.group_size; i++) {
174 struct tid_group *grp;
175
176 grp = kzalloc(sizeof(*grp), GFP_KERNEL);
177 if (!grp) {
178 /*
179 * If we fail here, the groups already
180 * allocated will be freed by the close
181 * call.
182 */
183 ret = -ENOMEM;
184 goto done;
185 }
186 grp->size = dd->rcv_entries.group_size;
187 grp->base = tidbase;
188 tid_group_add_tail(grp, &uctxt->tid_group_list);
189 tidbase += dd->rcv_entries.group_size;
190 }
191 }
192
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800193 fd->entry_to_rb = kcalloc(uctxt->expected_count,
194 sizeof(struct rb_node *),
195 GFP_KERNEL);
196 if (!fd->entry_to_rb)
197 return -ENOMEM;
198
Dean Luick622c2022016-07-28 15:21:21 -0400199 if (!HFI1_CAP_UGET_MASK(uctxt->flags, TID_UNMAP)) {
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500200 fd->invalid_tid_idx = 0;
201 fd->invalid_tids = kzalloc(uctxt->expected_count *
202 sizeof(u32), GFP_KERNEL);
203 if (!fd->invalid_tids) {
204 ret = -ENOMEM;
205 goto done;
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800206 }
207
208 /*
209 * Register MMU notifier callbacks. If the registration
Dean Luick622c2022016-07-28 15:21:21 -0400210 * fails, continue without TID caching for this context.
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800211 */
Dean Luicke0b09ac2016-07-28 15:21:20 -0400212 ret = hfi1_mmu_rb_register(fd, fd->mm, &tid_rb_ops, &fd->handler);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800213 if (ret) {
214 dd_dev_info(dd,
215 "Failed MMU notifier registration %d\n",
216 ret);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800217 ret = 0;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500218 }
219 }
220
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500221 /*
222 * PSM does not have a good way to separate, count, and
223 * effectively enforce a limit on RcvArray entries used by
224 * subctxts (when context sharing is used) when TID caching
225 * is enabled. To help with that, we calculate a per-process
226 * RcvArray entry share and enforce that.
227 * If TID caching is not in use, PSM deals with usage on its
228 * own. In that case, we allow any subctxt to take all of the
229 * entries.
230 *
231 * Make sure that we set the tid counts only after successful
232 * init.
233 */
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500234 spin_lock(&fd->tid_lock);
Dean Luick622c2022016-07-28 15:21:21 -0400235 if (uctxt->subctxt_cnt && fd->handler) {
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500236 u16 remainder;
237
238 fd->tid_limit = uctxt->expected_count / uctxt->subctxt_cnt;
239 remainder = uctxt->expected_count % uctxt->subctxt_cnt;
240 if (remainder && fd->subctxt < remainder)
241 fd->tid_limit++;
242 } else {
243 fd->tid_limit = uctxt->expected_count;
244 }
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500245 spin_unlock(&fd->tid_lock);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500246done:
247 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500248}
249
250int hfi1_user_exp_rcv_free(struct hfi1_filedata *fd)
251{
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500252 struct hfi1_ctxtdata *uctxt = fd->uctxt;
253 struct tid_group *grp, *gptr;
254
Mitko Haralanov94158442016-04-20 06:05:36 -0700255 if (!test_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags))
256 return 0;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500257 /*
258 * The notifier would have been removed when the process'es mm
259 * was freed.
260 */
Dean Luick622c2022016-07-28 15:21:21 -0400261 if (fd->handler)
Dean Luicke0b09ac2016-07-28 15:21:20 -0400262 hfi1_mmu_rb_unregister(fd->handler);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500263
264 kfree(fd->invalid_tids);
265
266 if (!uctxt->cnt) {
267 if (!EXP_TID_SET_EMPTY(uctxt->tid_full_list))
Dean Luicke0b09ac2016-07-28 15:21:20 -0400268 unlock_exp_tids(uctxt, &uctxt->tid_full_list, fd);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500269 if (!EXP_TID_SET_EMPTY(uctxt->tid_used_list))
Dean Luicke0b09ac2016-07-28 15:21:20 -0400270 unlock_exp_tids(uctxt, &uctxt->tid_used_list, fd);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500271 list_for_each_entry_safe(grp, gptr, &uctxt->tid_group_list.list,
272 list) {
273 list_del_init(&grp->list);
274 kfree(grp);
275 }
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500276 hfi1_clear_tids(uctxt);
277 }
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800278
279 kfree(fd->entry_to_rb);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500280 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500281}
282
Mitko Haralanovb8abe342016-02-05 11:57:51 -0500283/*
284 * Write an "empty" RcvArray entry.
285 * This function exists so the TID registaration code can use it
286 * to write to unused/unneeded entries and still take advantage
287 * of the WC performance improvements. The HFI will ignore this
288 * write to the RcvArray entry.
289 */
290static inline void rcv_array_wc_fill(struct hfi1_devdata *dd, u32 index)
291{
292 /*
293 * Doing the WC fill writes only makes sense if the device is
294 * present and the RcvArray has been mapped as WC memory.
295 */
296 if ((dd->flags & HFI1_PRESENT) && dd->rcvarray_wc)
297 writeq(0, dd->rcvarray_wc + (index * 8));
298}
299
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500300/*
301 * RcvArray entry allocation for Expected Receives is done by the
302 * following algorithm:
303 *
304 * The context keeps 3 lists of groups of RcvArray entries:
305 * 1. List of empty groups - tid_group_list
306 * This list is created during user context creation and
307 * contains elements which describe sets (of 8) of empty
308 * RcvArray entries.
309 * 2. List of partially used groups - tid_used_list
310 * This list contains sets of RcvArray entries which are
311 * not completely used up. Another mapping request could
312 * use some of all of the remaining entries.
313 * 3. List of full groups - tid_full_list
314 * This is the list where sets that are completely used
315 * up go.
316 *
317 * An attempt to optimize the usage of RcvArray entries is
318 * made by finding all sets of physically contiguous pages in a
319 * user's buffer.
320 * These physically contiguous sets are further split into
321 * sizes supported by the receive engine of the HFI. The
322 * resulting sets of pages are stored in struct tid_pageset,
323 * which describes the sets as:
324 * * .count - number of pages in this set
325 * * .idx - starting index into struct page ** array
326 * of this set
327 *
328 * From this point on, the algorithm deals with the page sets
329 * described above. The number of pagesets is divided by the
330 * RcvArray group size to produce the number of full groups
331 * needed.
332 *
333 * Groups from the 3 lists are manipulated using the following
334 * rules:
335 * 1. For each set of 8 pagesets, a complete group from
336 * tid_group_list is taken, programmed, and moved to
337 * the tid_full_list list.
338 * 2. For all remaining pagesets:
339 * 2.1 If the tid_used_list is empty and the tid_group_list
340 * is empty, stop processing pageset and return only
341 * what has been programmed up to this point.
342 * 2.2 If the tid_used_list is empty and the tid_group_list
343 * is not empty, move a group from tid_group_list to
344 * tid_used_list.
345 * 2.3 For each group is tid_used_group, program as much as
346 * can fit into the group. If the group becomes fully
347 * used, move it to tid_full_list.
348 */
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500349int hfi1_user_exp_rcv_setup(struct file *fp, struct hfi1_tid_info *tinfo)
350{
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500351 int ret = 0, need_group = 0, pinned;
352 struct hfi1_filedata *fd = fp->private_data;
353 struct hfi1_ctxtdata *uctxt = fd->uctxt;
354 struct hfi1_devdata *dd = uctxt->dd;
355 unsigned npages, ngroups, pageidx = 0, pageset_count, npagesets,
356 tididx = 0, mapped, mapped_pages = 0;
357 unsigned long vaddr = tinfo->vaddr;
358 struct page **pages = NULL;
359 u32 *tidlist = NULL;
360 struct tid_pageset *pagesets = NULL;
361
362 /* Get the number of pages the user buffer spans */
363 npages = num_user_pages(vaddr, tinfo->length);
364 if (!npages)
365 return -EINVAL;
366
367 if (npages > uctxt->expected_count) {
368 dd_dev_err(dd, "Expected buffer too big\n");
369 return -EINVAL;
370 }
371
372 /* Verify that access is OK for the user buffer */
373 if (!access_ok(VERIFY_WRITE, (void __user *)vaddr,
374 npages * PAGE_SIZE)) {
375 dd_dev_err(dd, "Fail vaddr %p, %u pages, !access_ok\n",
376 (void *)vaddr, npages);
377 return -EFAULT;
378 }
379
380 pagesets = kcalloc(uctxt->expected_count, sizeof(*pagesets),
381 GFP_KERNEL);
382 if (!pagesets)
383 return -ENOMEM;
384
385 /* Allocate the array of struct page pointers needed for pinning */
386 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
387 if (!pages) {
388 ret = -ENOMEM;
389 goto bail;
390 }
391
392 /*
393 * Pin all the pages of the user buffer. If we can't pin all the
394 * pages, accept the amount pinned so far and program only that.
395 * User space knows how to deal with partially programmed buffers.
396 */
Ira Weiny3faa3d92016-07-28 15:21:19 -0400397 if (!hfi1_can_pin_pages(dd, fd->mm, fd->tid_n_pinned, npages)) {
Mitko Haralanov0ad2d3d2016-04-12 10:46:29 -0700398 ret = -ENOMEM;
399 goto bail;
400 }
401
Ira Weiny3faa3d92016-07-28 15:21:19 -0400402 pinned = hfi1_acquire_user_pages(fd->mm, vaddr, npages, true, pages);
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500403 if (pinned <= 0) {
404 ret = pinned;
405 goto bail;
406 }
Mitko Haralanova7922f72016-03-08 11:15:39 -0800407 fd->tid_n_pinned += npages;
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500408
409 /* Find sets of physically contiguous pages */
410 npagesets = find_phys_blocks(pages, pinned, pagesets);
411
412 /*
413 * We don't need to access this under a lock since tid_used is per
414 * process and the same process cannot be in hfi1_user_exp_rcv_clear()
415 * and hfi1_user_exp_rcv_setup() at the same time.
416 */
417 spin_lock(&fd->tid_lock);
418 if (fd->tid_used + npagesets > fd->tid_limit)
419 pageset_count = fd->tid_limit - fd->tid_used;
420 else
421 pageset_count = npagesets;
422 spin_unlock(&fd->tid_lock);
423
424 if (!pageset_count)
425 goto bail;
426
427 ngroups = pageset_count / dd->rcv_entries.group_size;
428 tidlist = kcalloc(pageset_count, sizeof(*tidlist), GFP_KERNEL);
429 if (!tidlist) {
430 ret = -ENOMEM;
431 goto nomem;
432 }
433
434 tididx = 0;
435
436 /*
437 * From this point on, we are going to be using shared (between master
438 * and subcontexts) context resources. We need to take the lock.
439 */
440 mutex_lock(&uctxt->exp_lock);
441 /*
442 * The first step is to program the RcvArray entries which are complete
443 * groups.
444 */
445 while (ngroups && uctxt->tid_group_list.count) {
446 struct tid_group *grp =
447 tid_group_pop(&uctxt->tid_group_list);
448
449 ret = program_rcvarray(fp, vaddr, grp, pagesets,
450 pageidx, dd->rcv_entries.group_size,
451 pages, tidlist, &tididx, &mapped);
452 /*
453 * If there was a failure to program the RcvArray
454 * entries for the entire group, reset the grp fields
455 * and add the grp back to the free group list.
456 */
457 if (ret <= 0) {
458 tid_group_add_tail(grp, &uctxt->tid_group_list);
459 hfi1_cdbg(TID,
460 "Failed to program RcvArray group %d", ret);
461 goto unlock;
462 }
463
464 tid_group_add_tail(grp, &uctxt->tid_full_list);
465 ngroups--;
466 pageidx += ret;
467 mapped_pages += mapped;
468 }
469
470 while (pageidx < pageset_count) {
471 struct tid_group *grp, *ptr;
472 /*
473 * If we don't have any partially used tid groups, check
474 * if we have empty groups. If so, take one from there and
475 * put in the partially used list.
476 */
477 if (!uctxt->tid_used_list.count || need_group) {
478 if (!uctxt->tid_group_list.count)
479 goto unlock;
480
481 grp = tid_group_pop(&uctxt->tid_group_list);
482 tid_group_add_tail(grp, &uctxt->tid_used_list);
483 need_group = 0;
484 }
485 /*
486 * There is an optimization opportunity here - instead of
487 * fitting as many page sets as we can, check for a group
488 * later on in the list that could fit all of them.
489 */
490 list_for_each_entry_safe(grp, ptr, &uctxt->tid_used_list.list,
491 list) {
492 unsigned use = min_t(unsigned, pageset_count - pageidx,
493 grp->size - grp->used);
494
495 ret = program_rcvarray(fp, vaddr, grp, pagesets,
496 pageidx, use, pages, tidlist,
497 &tididx, &mapped);
498 if (ret < 0) {
499 hfi1_cdbg(TID,
500 "Failed to program RcvArray entries %d",
501 ret);
502 ret = -EFAULT;
503 goto unlock;
504 } else if (ret > 0) {
505 if (grp->used == grp->size)
506 tid_group_move(grp,
507 &uctxt->tid_used_list,
508 &uctxt->tid_full_list);
509 pageidx += ret;
510 mapped_pages += mapped;
511 need_group = 0;
512 /* Check if we are done so we break out early */
513 if (pageidx >= pageset_count)
514 break;
515 } else if (WARN_ON(ret == 0)) {
516 /*
517 * If ret is 0, we did not program any entries
518 * into this group, which can only happen if
519 * we've screwed up the accounting somewhere.
520 * Warn and try to continue.
521 */
522 need_group = 1;
523 }
524 }
525 }
526unlock:
527 mutex_unlock(&uctxt->exp_lock);
528nomem:
529 hfi1_cdbg(TID, "total mapped: tidpairs:%u pages:%u (%d)", tididx,
530 mapped_pages, ret);
531 if (tididx) {
532 spin_lock(&fd->tid_lock);
533 fd->tid_used += tididx;
534 spin_unlock(&fd->tid_lock);
535 tinfo->tidcnt = tididx;
536 tinfo->length = mapped_pages * PAGE_SIZE;
537
538 if (copy_to_user((void __user *)(unsigned long)tinfo->tidlist,
539 tidlist, sizeof(tidlist[0]) * tididx)) {
540 /*
541 * On failure to copy to the user level, we need to undo
542 * everything done so far so we don't leak resources.
543 */
544 tinfo->tidlist = (unsigned long)&tidlist;
545 hfi1_user_exp_rcv_clear(fp, tinfo);
546 tinfo->tidlist = 0;
547 ret = -EFAULT;
548 goto bail;
549 }
550 }
551
552 /*
553 * If not everything was mapped (due to insufficient RcvArray entries,
554 * for example), unpin all unmapped pages so we can pin them nex time.
555 */
Mitko Haralanova7922f72016-03-08 11:15:39 -0800556 if (mapped_pages != pinned) {
Ira Weiny3faa3d92016-07-28 15:21:19 -0400557 hfi1_release_user_pages(fd->mm, &pages[mapped_pages],
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500558 pinned - mapped_pages,
559 false);
Mitko Haralanova7922f72016-03-08 11:15:39 -0800560 fd->tid_n_pinned -= pinned - mapped_pages;
561 }
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500562bail:
563 kfree(pagesets);
564 kfree(pages);
565 kfree(tidlist);
566 return ret > 0 ? 0 : ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500567}
568
569int hfi1_user_exp_rcv_clear(struct file *fp, struct hfi1_tid_info *tinfo)
570{
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500571 int ret = 0;
572 struct hfi1_filedata *fd = fp->private_data;
573 struct hfi1_ctxtdata *uctxt = fd->uctxt;
574 u32 *tidinfo;
575 unsigned tididx;
576
577 tidinfo = kcalloc(tinfo->tidcnt, sizeof(*tidinfo), GFP_KERNEL);
578 if (!tidinfo)
579 return -ENOMEM;
580
581 if (copy_from_user(tidinfo, (void __user *)(unsigned long)
582 tinfo->tidlist, sizeof(tidinfo[0]) *
583 tinfo->tidcnt)) {
584 ret = -EFAULT;
585 goto done;
586 }
587
588 mutex_lock(&uctxt->exp_lock);
589 for (tididx = 0; tididx < tinfo->tidcnt; tididx++) {
590 ret = unprogram_rcvarray(fp, tidinfo[tididx], NULL);
591 if (ret) {
592 hfi1_cdbg(TID, "Failed to unprogram rcv array %d",
593 ret);
594 break;
595 }
596 }
597 spin_lock(&fd->tid_lock);
598 fd->tid_used -= tididx;
599 spin_unlock(&fd->tid_lock);
600 tinfo->tidcnt = tididx;
601 mutex_unlock(&uctxt->exp_lock);
602done:
603 kfree(tidinfo);
604 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500605}
606
607int hfi1_user_exp_rcv_invalid(struct file *fp, struct hfi1_tid_info *tinfo)
608{
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500609 struct hfi1_filedata *fd = fp->private_data;
610 struct hfi1_ctxtdata *uctxt = fd->uctxt;
611 unsigned long *ev = uctxt->dd->events +
612 (((uctxt->ctxt - uctxt->dd->first_user_ctxt) *
613 HFI1_MAX_SHARED_CTXTS) + fd->subctxt);
614 u32 *array;
615 int ret = 0;
616
617 if (!fd->invalid_tids)
618 return -EINVAL;
619
620 /*
621 * copy_to_user() can sleep, which will leave the invalid_lock
622 * locked and cause the MMU notifier to be blocked on the lock
623 * for a long time.
624 * Copy the data to a local buffer so we can release the lock.
625 */
626 array = kcalloc(uctxt->expected_count, sizeof(*array), GFP_KERNEL);
627 if (!array)
628 return -EFAULT;
629
630 spin_lock(&fd->invalid_lock);
631 if (fd->invalid_tid_idx) {
632 memcpy(array, fd->invalid_tids, sizeof(*array) *
633 fd->invalid_tid_idx);
634 memset(fd->invalid_tids, 0, sizeof(*fd->invalid_tids) *
635 fd->invalid_tid_idx);
636 tinfo->tidcnt = fd->invalid_tid_idx;
637 fd->invalid_tid_idx = 0;
638 /*
639 * Reset the user flag while still holding the lock.
640 * Otherwise, PSM can miss events.
641 */
642 clear_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
643 } else {
644 tinfo->tidcnt = 0;
645 }
646 spin_unlock(&fd->invalid_lock);
647
648 if (tinfo->tidcnt) {
649 if (copy_to_user((void __user *)tinfo->tidlist,
650 array, sizeof(*array) * tinfo->tidcnt))
651 ret = -EFAULT;
652 }
653 kfree(array);
654
655 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500656}
657
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500658static u32 find_phys_blocks(struct page **pages, unsigned npages,
659 struct tid_pageset *list)
660{
661 unsigned pagecount, pageidx, setcount = 0, i;
662 unsigned long pfn, this_pfn;
663
664 if (!npages)
665 return 0;
666
667 /*
668 * Look for sets of physically contiguous pages in the user buffer.
669 * This will allow us to optimize Expected RcvArray entry usage by
670 * using the bigger supported sizes.
671 */
672 pfn = page_to_pfn(pages[0]);
673 for (pageidx = 0, pagecount = 1, i = 1; i <= npages; i++) {
674 this_pfn = i < npages ? page_to_pfn(pages[i]) : 0;
675
676 /*
677 * If the pfn's are not sequential, pages are not physically
678 * contiguous.
679 */
680 if (this_pfn != ++pfn) {
681 /*
682 * At this point we have to loop over the set of
683 * physically contiguous pages and break them down it
684 * sizes supported by the HW.
685 * There are two main constraints:
686 * 1. The max buffer size is MAX_EXPECTED_BUFFER.
687 * If the total set size is bigger than that
688 * program only a MAX_EXPECTED_BUFFER chunk.
689 * 2. The buffer size has to be a power of two. If
690 * it is not, round down to the closes power of
691 * 2 and program that size.
692 */
693 while (pagecount) {
694 int maxpages = pagecount;
695 u32 bufsize = pagecount * PAGE_SIZE;
696
697 if (bufsize > MAX_EXPECTED_BUFFER)
698 maxpages =
699 MAX_EXPECTED_BUFFER >>
700 PAGE_SHIFT;
701 else if (!is_power_of_2(bufsize))
702 maxpages =
703 rounddown_pow_of_two(bufsize) >>
704 PAGE_SHIFT;
705
706 list[setcount].idx = pageidx;
707 list[setcount].count = maxpages;
708 pagecount -= maxpages;
709 pageidx += maxpages;
710 setcount++;
711 }
712 pageidx = i;
713 pagecount = 1;
714 pfn = this_pfn;
715 } else {
716 pagecount++;
717 }
718 }
719 return setcount;
720}
721
722/**
723 * program_rcvarray() - program an RcvArray group with receive buffers
724 * @fp: file pointer
725 * @vaddr: starting user virtual address
726 * @grp: RcvArray group
727 * @sets: array of struct tid_pageset holding information on physically
728 * contiguous chunks from the user buffer
729 * @start: starting index into sets array
730 * @count: number of struct tid_pageset's to program
731 * @pages: an array of struct page * for the user buffer
732 * @tidlist: the array of u32 elements when the information about the
733 * programmed RcvArray entries is to be encoded.
734 * @tididx: starting offset into tidlist
735 * @pmapped: (output parameter) number of pages programmed into the RcvArray
736 * entries.
737 *
738 * This function will program up to 'count' number of RcvArray entries from the
739 * group 'grp'. To make best use of write-combining writes, the function will
740 * perform writes to the unused RcvArray entries which will be ignored by the
741 * HW. Each RcvArray entry will be programmed with a physically contiguous
742 * buffer chunk from the user's virtual buffer.
743 *
744 * Return:
745 * -EINVAL if the requested count is larger than the size of the group,
746 * -ENOMEM or -EFAULT on error from set_rcvarray_entry(), or
747 * number of RcvArray entries programmed.
748 */
749static int program_rcvarray(struct file *fp, unsigned long vaddr,
750 struct tid_group *grp,
751 struct tid_pageset *sets,
752 unsigned start, u16 count, struct page **pages,
753 u32 *tidlist, unsigned *tididx, unsigned *pmapped)
754{
755 struct hfi1_filedata *fd = fp->private_data;
756 struct hfi1_ctxtdata *uctxt = fd->uctxt;
757 struct hfi1_devdata *dd = uctxt->dd;
758 u16 idx;
759 u32 tidinfo = 0, rcventry, useidx = 0;
760 int mapped = 0;
761
762 /* Count should never be larger than the group size */
763 if (count > grp->size)
764 return -EINVAL;
765
766 /* Find the first unused entry in the group */
767 for (idx = 0; idx < grp->size; idx++) {
768 if (!(grp->map & (1 << idx))) {
769 useidx = idx;
770 break;
771 }
772 rcv_array_wc_fill(dd, grp->base + idx);
773 }
774
775 idx = 0;
776 while (idx < count) {
777 u16 npages, pageidx, setidx = start + idx;
778 int ret = 0;
779
780 /*
781 * If this entry in the group is used, move to the next one.
782 * If we go past the end of the group, exit the loop.
783 */
784 if (useidx >= grp->size) {
785 break;
786 } else if (grp->map & (1 << useidx)) {
787 rcv_array_wc_fill(dd, grp->base + useidx);
788 useidx++;
789 continue;
790 }
791
792 rcventry = grp->base + useidx;
793 npages = sets[setidx].count;
794 pageidx = sets[setidx].idx;
795
796 ret = set_rcvarray_entry(fp, vaddr + (pageidx * PAGE_SIZE),
797 rcventry, grp, pages + pageidx,
798 npages);
799 if (ret)
800 return ret;
801 mapped += npages;
802
803 tidinfo = rcventry2tidinfo(rcventry - uctxt->expected_base) |
804 EXP_TID_SET(LEN, npages);
805 tidlist[(*tididx)++] = tidinfo;
806 grp->used++;
807 grp->map |= 1 << useidx++;
808 idx++;
809 }
810
811 /* Fill the rest of the group with "blank" writes */
812 for (; useidx < grp->size; useidx++)
813 rcv_array_wc_fill(dd, grp->base + useidx);
814 *pmapped = mapped;
815 return idx;
816}
817
818static int set_rcvarray_entry(struct file *fp, unsigned long vaddr,
819 u32 rcventry, struct tid_group *grp,
820 struct page **pages, unsigned npages)
821{
822 int ret;
823 struct hfi1_filedata *fd = fp->private_data;
824 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800825 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500826 struct hfi1_devdata *dd = uctxt->dd;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500827 dma_addr_t phys;
828
829 /*
830 * Allocate the node first so we can handle a potential
831 * failure before we've programmed anything.
832 */
833 node = kzalloc(sizeof(*node) + (sizeof(struct page *) * npages),
834 GFP_KERNEL);
835 if (!node)
836 return -ENOMEM;
837
838 phys = pci_map_single(dd->pcidev,
839 __va(page_to_phys(pages[0])),
840 npages * PAGE_SIZE, PCI_DMA_FROMDEVICE);
841 if (dma_mapping_error(&dd->pcidev->dev, phys)) {
842 dd_dev_err(dd, "Failed to DMA map Exp Rcv pages 0x%llx\n",
843 phys);
844 kfree(node);
845 return -EFAULT;
846 }
847
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800848 node->mmu.addr = vaddr;
849 node->mmu.len = npages * PAGE_SIZE;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500850 node->phys = page_to_phys(pages[0]);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500851 node->npages = npages;
852 node->rcventry = rcventry;
853 node->dma_addr = phys;
854 node->grp = grp;
855 node->freed = false;
856 memcpy(node->pages, pages, sizeof(struct page *) * npages);
857
Dean Luick622c2022016-07-28 15:21:21 -0400858 if (!fd->handler)
Dean Luicke0b09ac2016-07-28 15:21:20 -0400859 ret = tid_rb_insert(fd, &node->mmu);
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800860 else
Dean Luicke0b09ac2016-07-28 15:21:20 -0400861 ret = hfi1_mmu_rb_insert(fd->handler, &node->mmu);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500862
863 if (ret) {
864 hfi1_cdbg(TID, "Failed to insert RB node %u 0x%lx, 0x%lx %d",
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800865 node->rcventry, node->mmu.addr, node->phys, ret);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500866 pci_unmap_single(dd->pcidev, phys, npages * PAGE_SIZE,
867 PCI_DMA_FROMDEVICE);
868 kfree(node);
869 return -EFAULT;
870 }
871 hfi1_put_tid(dd, rcventry, PT_EXPECTED, phys, ilog2(npages) + 1);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800872 trace_hfi1_exp_tid_reg(uctxt->ctxt, fd->subctxt, rcventry, npages,
873 node->mmu.addr, node->phys, phys);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500874 return 0;
875}
876
877static int unprogram_rcvarray(struct file *fp, u32 tidinfo,
878 struct tid_group **grp)
879{
880 struct hfi1_filedata *fd = fp->private_data;
881 struct hfi1_ctxtdata *uctxt = fd->uctxt;
882 struct hfi1_devdata *dd = uctxt->dd;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800883 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500884 u8 tidctrl = EXP_TID_GET(tidinfo, CTRL);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800885 u32 tididx = EXP_TID_GET(tidinfo, IDX) << 1, rcventry;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500886
887 if (tididx >= uctxt->expected_count) {
888 dd_dev_err(dd, "Invalid RcvArray entry (%u) index for ctxt %u\n",
889 tididx, uctxt->ctxt);
890 return -EINVAL;
891 }
892
893 if (tidctrl == 0x3)
894 return -EINVAL;
895
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800896 rcventry = tididx + (tidctrl - 1);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500897
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800898 node = fd->entry_to_rb[rcventry];
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800899 if (!node || node->rcventry != (uctxt->expected_base + rcventry))
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500900 return -EBADF;
Dean Luick622c2022016-07-28 15:21:21 -0400901 if (!fd->handler)
Dean Luicke0b09ac2016-07-28 15:21:20 -0400902 tid_rb_remove(fd, &node->mmu, fd->mm);
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800903 else
Dean Luicke0b09ac2016-07-28 15:21:20 -0400904 hfi1_mmu_rb_remove(fd->handler, &node->mmu);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800905
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500906 if (grp)
907 *grp = node->grp;
Ira Weiny5ed3b152016-07-28 12:27:32 -0400908 clear_tid_node(fd, node);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500909 return 0;
910}
911
Ira Weiny5ed3b152016-07-28 12:27:32 -0400912static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500913{
914 struct hfi1_ctxtdata *uctxt = fd->uctxt;
915 struct hfi1_devdata *dd = uctxt->dd;
916
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500917 trace_hfi1_exp_tid_unreg(uctxt->ctxt, fd->subctxt, node->rcventry,
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800918 node->npages, node->mmu.addr, node->phys,
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500919 node->dma_addr);
920
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500921 hfi1_put_tid(dd, node->rcventry, PT_INVALID, 0, 0);
922 /*
923 * Make sure device has seen the write before we unpin the
924 * pages.
925 */
926 flush_wc();
927
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800928 pci_unmap_single(dd->pcidev, node->dma_addr, node->mmu.len,
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500929 PCI_DMA_FROMDEVICE);
Ira Weiny3faa3d92016-07-28 15:21:19 -0400930 hfi1_release_user_pages(fd->mm, node->pages, node->npages, true);
Mitko Haralanova7922f72016-03-08 11:15:39 -0800931 fd->tid_n_pinned -= node->npages;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500932
933 node->grp->used--;
934 node->grp->map &= ~(1 << (node->rcventry - node->grp->base));
935
936 if (node->grp->used == node->grp->size - 1)
937 tid_group_move(node->grp, &uctxt->tid_full_list,
938 &uctxt->tid_used_list);
939 else if (!node->grp->used)
940 tid_group_move(node->grp, &uctxt->tid_used_list,
941 &uctxt->tid_group_list);
942 kfree(node);
943}
944
945static void unlock_exp_tids(struct hfi1_ctxtdata *uctxt,
Dean Luicke0b09ac2016-07-28 15:21:20 -0400946 struct exp_tid_set *set,
947 struct hfi1_filedata *fd)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500948{
949 struct tid_group *grp, *ptr;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500950 int i;
951
952 list_for_each_entry_safe(grp, ptr, &set->list, list) {
953 list_del_init(&grp->list);
954
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500955 for (i = 0; i < grp->size; i++) {
956 if (grp->map & (1 << i)) {
957 u16 rcventry = grp->base + i;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800958 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500959
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800960 node = fd->entry_to_rb[rcventry -
961 uctxt->expected_base];
962 if (!node || node->rcventry != rcventry)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500963 continue;
Dean Luick622c2022016-07-28 15:21:21 -0400964 if (!fd->handler)
Dean Luicke0b09ac2016-07-28 15:21:20 -0400965 tid_rb_remove(fd, &node->mmu, fd->mm);
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800966 else
Dean Luicke0b09ac2016-07-28 15:21:20 -0400967 hfi1_mmu_rb_remove(fd->handler,
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800968 &node->mmu);
Ira Weiny5ed3b152016-07-28 12:27:32 -0400969 clear_tid_node(fd, node);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500970 }
971 }
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500972 }
973}
974
Dean Luicke0b09ac2016-07-28 15:21:20 -0400975static int tid_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500976{
Dean Luicke0b09ac2016-07-28 15:21:20 -0400977 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800978 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
979 struct tid_rb_node *node =
980 container_of(mnode, struct tid_rb_node, mmu);
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500981
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800982 if (node->freed)
983 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500984
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800985 trace_hfi1_exp_tid_inval(uctxt->ctxt, fdata->subctxt, node->mmu.addr,
986 node->rcventry, node->npages, node->dma_addr);
987 node->freed = true;
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -0500988
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800989 spin_lock(&fdata->invalid_lock);
990 if (fdata->invalid_tid_idx < uctxt->expected_count) {
991 fdata->invalid_tids[fdata->invalid_tid_idx] =
992 rcventry2tidinfo(node->rcventry - uctxt->expected_base);
993 fdata->invalid_tids[fdata->invalid_tid_idx] |=
994 EXP_TID_SET(LEN, node->npages);
995 if (!fdata->invalid_tid_idx) {
996 unsigned long *ev;
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500997
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -0500998 /*
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800999 * hfi1_set_uevent_bits() sets a user event flag
1000 * for all processes. Because calling into the
1001 * driver to process TID cache invalidations is
1002 * expensive and TID cache invalidations are
1003 * handled on a per-process basis, we can
1004 * optimize this to set the flag only for the
1005 * process in question.
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001006 */
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001007 ev = uctxt->dd->events +
1008 (((uctxt->ctxt - uctxt->dd->first_user_ctxt) *
1009 HFI1_MAX_SHARED_CTXTS) + fdata->subctxt);
1010 set_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001011 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001012 fdata->invalid_tid_idx++;
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001013 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001014 spin_unlock(&fdata->invalid_lock);
1015 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001016}
1017
Dean Luicke0b09ac2016-07-28 15:21:20 -04001018static int tid_rb_insert(void *arg, struct mmu_rb_node *node)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001019{
Dean Luicke0b09ac2016-07-28 15:21:20 -04001020 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001021 struct tid_rb_node *tnode =
1022 container_of(node, struct tid_rb_node, mmu);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -08001023 u32 base = fdata->uctxt->expected_base;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001024
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001025 fdata->entry_to_rb[tnode->rcventry - base] = tnode;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001026 return 0;
1027}
1028
Dean Luicke0b09ac2016-07-28 15:21:20 -04001029static void tid_rb_remove(void *arg, struct mmu_rb_node *node,
Mitko Haralanovf19bd642016-04-12 10:45:57 -07001030 struct mm_struct *mm)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001031{
Dean Luicke0b09ac2016-07-28 15:21:20 -04001032 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001033 struct tid_rb_node *tnode =
1034 container_of(node, struct tid_rb_node, mmu);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -08001035 u32 base = fdata->uctxt->expected_base;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001036
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001037 fdata->entry_to_rb[tnode->rcventry - base] = NULL;
Mitko Haralanova92ba6d2016-02-03 14:34:41 -08001038}