blob: 269a948189e040af766694ac433ec0c2f6b973fb [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
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500199 if (!HFI1_CAP_IS_USET(TID_UNMAP)) {
200 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
210 * fails, continue but turn off the TID caching for
211 * all user contexts.
212 */
Dean Luicke0b09ac2016-07-28 15:21:20 -0400213 ret = hfi1_mmu_rb_register(fd, fd->mm, &tid_rb_ops, &fd->handler);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800214 if (ret) {
215 dd_dev_info(dd,
216 "Failed MMU notifier registration %d\n",
217 ret);
218 HFI1_CAP_USET(TID_UNMAP);
219 ret = 0;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500220 }
221 }
222
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500223 /*
224 * PSM does not have a good way to separate, count, and
225 * effectively enforce a limit on RcvArray entries used by
226 * subctxts (when context sharing is used) when TID caching
227 * is enabled. To help with that, we calculate a per-process
228 * RcvArray entry share and enforce that.
229 * If TID caching is not in use, PSM deals with usage on its
230 * own. In that case, we allow any subctxt to take all of the
231 * entries.
232 *
233 * Make sure that we set the tid counts only after successful
234 * init.
235 */
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500236 spin_lock(&fd->tid_lock);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500237 if (uctxt->subctxt_cnt && !HFI1_CAP_IS_USET(TID_UNMAP)) {
238 u16 remainder;
239
240 fd->tid_limit = uctxt->expected_count / uctxt->subctxt_cnt;
241 remainder = uctxt->expected_count % uctxt->subctxt_cnt;
242 if (remainder && fd->subctxt < remainder)
243 fd->tid_limit++;
244 } else {
245 fd->tid_limit = uctxt->expected_count;
246 }
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500247 spin_unlock(&fd->tid_lock);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500248done:
249 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500250}
251
252int hfi1_user_exp_rcv_free(struct hfi1_filedata *fd)
253{
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500254 struct hfi1_ctxtdata *uctxt = fd->uctxt;
255 struct tid_group *grp, *gptr;
256
Mitko Haralanov94158442016-04-20 06:05:36 -0700257 if (!test_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags))
258 return 0;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500259 /*
260 * The notifier would have been removed when the process'es mm
261 * was freed.
262 */
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800263 if (!HFI1_CAP_IS_USET(TID_UNMAP))
Dean Luicke0b09ac2016-07-28 15:21:20 -0400264 hfi1_mmu_rb_unregister(fd->handler);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500265
266 kfree(fd->invalid_tids);
267
268 if (!uctxt->cnt) {
269 if (!EXP_TID_SET_EMPTY(uctxt->tid_full_list))
Dean Luicke0b09ac2016-07-28 15:21:20 -0400270 unlock_exp_tids(uctxt, &uctxt->tid_full_list, fd);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500271 if (!EXP_TID_SET_EMPTY(uctxt->tid_used_list))
Dean Luicke0b09ac2016-07-28 15:21:20 -0400272 unlock_exp_tids(uctxt, &uctxt->tid_used_list, fd);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500273 list_for_each_entry_safe(grp, gptr, &uctxt->tid_group_list.list,
274 list) {
275 list_del_init(&grp->list);
276 kfree(grp);
277 }
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500278 hfi1_clear_tids(uctxt);
279 }
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800280
281 kfree(fd->entry_to_rb);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500282 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500283}
284
Mitko Haralanovb8abe342016-02-05 11:57:51 -0500285/*
286 * Write an "empty" RcvArray entry.
287 * This function exists so the TID registaration code can use it
288 * to write to unused/unneeded entries and still take advantage
289 * of the WC performance improvements. The HFI will ignore this
290 * write to the RcvArray entry.
291 */
292static inline void rcv_array_wc_fill(struct hfi1_devdata *dd, u32 index)
293{
294 /*
295 * Doing the WC fill writes only makes sense if the device is
296 * present and the RcvArray has been mapped as WC memory.
297 */
298 if ((dd->flags & HFI1_PRESENT) && dd->rcvarray_wc)
299 writeq(0, dd->rcvarray_wc + (index * 8));
300}
301
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500302/*
303 * RcvArray entry allocation for Expected Receives is done by the
304 * following algorithm:
305 *
306 * The context keeps 3 lists of groups of RcvArray entries:
307 * 1. List of empty groups - tid_group_list
308 * This list is created during user context creation and
309 * contains elements which describe sets (of 8) of empty
310 * RcvArray entries.
311 * 2. List of partially used groups - tid_used_list
312 * This list contains sets of RcvArray entries which are
313 * not completely used up. Another mapping request could
314 * use some of all of the remaining entries.
315 * 3. List of full groups - tid_full_list
316 * This is the list where sets that are completely used
317 * up go.
318 *
319 * An attempt to optimize the usage of RcvArray entries is
320 * made by finding all sets of physically contiguous pages in a
321 * user's buffer.
322 * These physically contiguous sets are further split into
323 * sizes supported by the receive engine of the HFI. The
324 * resulting sets of pages are stored in struct tid_pageset,
325 * which describes the sets as:
326 * * .count - number of pages in this set
327 * * .idx - starting index into struct page ** array
328 * of this set
329 *
330 * From this point on, the algorithm deals with the page sets
331 * described above. The number of pagesets is divided by the
332 * RcvArray group size to produce the number of full groups
333 * needed.
334 *
335 * Groups from the 3 lists are manipulated using the following
336 * rules:
337 * 1. For each set of 8 pagesets, a complete group from
338 * tid_group_list is taken, programmed, and moved to
339 * the tid_full_list list.
340 * 2. For all remaining pagesets:
341 * 2.1 If the tid_used_list is empty and the tid_group_list
342 * is empty, stop processing pageset and return only
343 * what has been programmed up to this point.
344 * 2.2 If the tid_used_list is empty and the tid_group_list
345 * is not empty, move a group from tid_group_list to
346 * tid_used_list.
347 * 2.3 For each group is tid_used_group, program as much as
348 * can fit into the group. If the group becomes fully
349 * used, move it to tid_full_list.
350 */
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500351int hfi1_user_exp_rcv_setup(struct file *fp, struct hfi1_tid_info *tinfo)
352{
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500353 int ret = 0, need_group = 0, pinned;
354 struct hfi1_filedata *fd = fp->private_data;
355 struct hfi1_ctxtdata *uctxt = fd->uctxt;
356 struct hfi1_devdata *dd = uctxt->dd;
357 unsigned npages, ngroups, pageidx = 0, pageset_count, npagesets,
358 tididx = 0, mapped, mapped_pages = 0;
359 unsigned long vaddr = tinfo->vaddr;
360 struct page **pages = NULL;
361 u32 *tidlist = NULL;
362 struct tid_pageset *pagesets = NULL;
363
364 /* Get the number of pages the user buffer spans */
365 npages = num_user_pages(vaddr, tinfo->length);
366 if (!npages)
367 return -EINVAL;
368
369 if (npages > uctxt->expected_count) {
370 dd_dev_err(dd, "Expected buffer too big\n");
371 return -EINVAL;
372 }
373
374 /* Verify that access is OK for the user buffer */
375 if (!access_ok(VERIFY_WRITE, (void __user *)vaddr,
376 npages * PAGE_SIZE)) {
377 dd_dev_err(dd, "Fail vaddr %p, %u pages, !access_ok\n",
378 (void *)vaddr, npages);
379 return -EFAULT;
380 }
381
382 pagesets = kcalloc(uctxt->expected_count, sizeof(*pagesets),
383 GFP_KERNEL);
384 if (!pagesets)
385 return -ENOMEM;
386
387 /* Allocate the array of struct page pointers needed for pinning */
388 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
389 if (!pages) {
390 ret = -ENOMEM;
391 goto bail;
392 }
393
394 /*
395 * Pin all the pages of the user buffer. If we can't pin all the
396 * pages, accept the amount pinned so far and program only that.
397 * User space knows how to deal with partially programmed buffers.
398 */
Ira Weiny3faa3d92016-07-28 15:21:19 -0400399 if (!hfi1_can_pin_pages(dd, fd->mm, fd->tid_n_pinned, npages)) {
Mitko Haralanov0ad2d3d2016-04-12 10:46:29 -0700400 ret = -ENOMEM;
401 goto bail;
402 }
403
Ira Weiny3faa3d92016-07-28 15:21:19 -0400404 pinned = hfi1_acquire_user_pages(fd->mm, vaddr, npages, true, pages);
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500405 if (pinned <= 0) {
406 ret = pinned;
407 goto bail;
408 }
Mitko Haralanova7922f72016-03-08 11:15:39 -0800409 fd->tid_n_pinned += npages;
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500410
411 /* Find sets of physically contiguous pages */
412 npagesets = find_phys_blocks(pages, pinned, pagesets);
413
414 /*
415 * We don't need to access this under a lock since tid_used is per
416 * process and the same process cannot be in hfi1_user_exp_rcv_clear()
417 * and hfi1_user_exp_rcv_setup() at the same time.
418 */
419 spin_lock(&fd->tid_lock);
420 if (fd->tid_used + npagesets > fd->tid_limit)
421 pageset_count = fd->tid_limit - fd->tid_used;
422 else
423 pageset_count = npagesets;
424 spin_unlock(&fd->tid_lock);
425
426 if (!pageset_count)
427 goto bail;
428
429 ngroups = pageset_count / dd->rcv_entries.group_size;
430 tidlist = kcalloc(pageset_count, sizeof(*tidlist), GFP_KERNEL);
431 if (!tidlist) {
432 ret = -ENOMEM;
433 goto nomem;
434 }
435
436 tididx = 0;
437
438 /*
439 * From this point on, we are going to be using shared (between master
440 * and subcontexts) context resources. We need to take the lock.
441 */
442 mutex_lock(&uctxt->exp_lock);
443 /*
444 * The first step is to program the RcvArray entries which are complete
445 * groups.
446 */
447 while (ngroups && uctxt->tid_group_list.count) {
448 struct tid_group *grp =
449 tid_group_pop(&uctxt->tid_group_list);
450
451 ret = program_rcvarray(fp, vaddr, grp, pagesets,
452 pageidx, dd->rcv_entries.group_size,
453 pages, tidlist, &tididx, &mapped);
454 /*
455 * If there was a failure to program the RcvArray
456 * entries for the entire group, reset the grp fields
457 * and add the grp back to the free group list.
458 */
459 if (ret <= 0) {
460 tid_group_add_tail(grp, &uctxt->tid_group_list);
461 hfi1_cdbg(TID,
462 "Failed to program RcvArray group %d", ret);
463 goto unlock;
464 }
465
466 tid_group_add_tail(grp, &uctxt->tid_full_list);
467 ngroups--;
468 pageidx += ret;
469 mapped_pages += mapped;
470 }
471
472 while (pageidx < pageset_count) {
473 struct tid_group *grp, *ptr;
474 /*
475 * If we don't have any partially used tid groups, check
476 * if we have empty groups. If so, take one from there and
477 * put in the partially used list.
478 */
479 if (!uctxt->tid_used_list.count || need_group) {
480 if (!uctxt->tid_group_list.count)
481 goto unlock;
482
483 grp = tid_group_pop(&uctxt->tid_group_list);
484 tid_group_add_tail(grp, &uctxt->tid_used_list);
485 need_group = 0;
486 }
487 /*
488 * There is an optimization opportunity here - instead of
489 * fitting as many page sets as we can, check for a group
490 * later on in the list that could fit all of them.
491 */
492 list_for_each_entry_safe(grp, ptr, &uctxt->tid_used_list.list,
493 list) {
494 unsigned use = min_t(unsigned, pageset_count - pageidx,
495 grp->size - grp->used);
496
497 ret = program_rcvarray(fp, vaddr, grp, pagesets,
498 pageidx, use, pages, tidlist,
499 &tididx, &mapped);
500 if (ret < 0) {
501 hfi1_cdbg(TID,
502 "Failed to program RcvArray entries %d",
503 ret);
504 ret = -EFAULT;
505 goto unlock;
506 } else if (ret > 0) {
507 if (grp->used == grp->size)
508 tid_group_move(grp,
509 &uctxt->tid_used_list,
510 &uctxt->tid_full_list);
511 pageidx += ret;
512 mapped_pages += mapped;
513 need_group = 0;
514 /* Check if we are done so we break out early */
515 if (pageidx >= pageset_count)
516 break;
517 } else if (WARN_ON(ret == 0)) {
518 /*
519 * If ret is 0, we did not program any entries
520 * into this group, which can only happen if
521 * we've screwed up the accounting somewhere.
522 * Warn and try to continue.
523 */
524 need_group = 1;
525 }
526 }
527 }
528unlock:
529 mutex_unlock(&uctxt->exp_lock);
530nomem:
531 hfi1_cdbg(TID, "total mapped: tidpairs:%u pages:%u (%d)", tididx,
532 mapped_pages, ret);
533 if (tididx) {
534 spin_lock(&fd->tid_lock);
535 fd->tid_used += tididx;
536 spin_unlock(&fd->tid_lock);
537 tinfo->tidcnt = tididx;
538 tinfo->length = mapped_pages * PAGE_SIZE;
539
540 if (copy_to_user((void __user *)(unsigned long)tinfo->tidlist,
541 tidlist, sizeof(tidlist[0]) * tididx)) {
542 /*
543 * On failure to copy to the user level, we need to undo
544 * everything done so far so we don't leak resources.
545 */
546 tinfo->tidlist = (unsigned long)&tidlist;
547 hfi1_user_exp_rcv_clear(fp, tinfo);
548 tinfo->tidlist = 0;
549 ret = -EFAULT;
550 goto bail;
551 }
552 }
553
554 /*
555 * If not everything was mapped (due to insufficient RcvArray entries,
556 * for example), unpin all unmapped pages so we can pin them nex time.
557 */
Mitko Haralanova7922f72016-03-08 11:15:39 -0800558 if (mapped_pages != pinned) {
Ira Weiny3faa3d92016-07-28 15:21:19 -0400559 hfi1_release_user_pages(fd->mm, &pages[mapped_pages],
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500560 pinned - mapped_pages,
561 false);
Mitko Haralanova7922f72016-03-08 11:15:39 -0800562 fd->tid_n_pinned -= pinned - mapped_pages;
563 }
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500564bail:
565 kfree(pagesets);
566 kfree(pages);
567 kfree(tidlist);
568 return ret > 0 ? 0 : ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500569}
570
571int hfi1_user_exp_rcv_clear(struct file *fp, struct hfi1_tid_info *tinfo)
572{
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500573 int ret = 0;
574 struct hfi1_filedata *fd = fp->private_data;
575 struct hfi1_ctxtdata *uctxt = fd->uctxt;
576 u32 *tidinfo;
577 unsigned tididx;
578
579 tidinfo = kcalloc(tinfo->tidcnt, sizeof(*tidinfo), GFP_KERNEL);
580 if (!tidinfo)
581 return -ENOMEM;
582
583 if (copy_from_user(tidinfo, (void __user *)(unsigned long)
584 tinfo->tidlist, sizeof(tidinfo[0]) *
585 tinfo->tidcnt)) {
586 ret = -EFAULT;
587 goto done;
588 }
589
590 mutex_lock(&uctxt->exp_lock);
591 for (tididx = 0; tididx < tinfo->tidcnt; tididx++) {
592 ret = unprogram_rcvarray(fp, tidinfo[tididx], NULL);
593 if (ret) {
594 hfi1_cdbg(TID, "Failed to unprogram rcv array %d",
595 ret);
596 break;
597 }
598 }
599 spin_lock(&fd->tid_lock);
600 fd->tid_used -= tididx;
601 spin_unlock(&fd->tid_lock);
602 tinfo->tidcnt = tididx;
603 mutex_unlock(&uctxt->exp_lock);
604done:
605 kfree(tidinfo);
606 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500607}
608
609int hfi1_user_exp_rcv_invalid(struct file *fp, struct hfi1_tid_info *tinfo)
610{
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500611 struct hfi1_filedata *fd = fp->private_data;
612 struct hfi1_ctxtdata *uctxt = fd->uctxt;
613 unsigned long *ev = uctxt->dd->events +
614 (((uctxt->ctxt - uctxt->dd->first_user_ctxt) *
615 HFI1_MAX_SHARED_CTXTS) + fd->subctxt);
616 u32 *array;
617 int ret = 0;
618
619 if (!fd->invalid_tids)
620 return -EINVAL;
621
622 /*
623 * copy_to_user() can sleep, which will leave the invalid_lock
624 * locked and cause the MMU notifier to be blocked on the lock
625 * for a long time.
626 * Copy the data to a local buffer so we can release the lock.
627 */
628 array = kcalloc(uctxt->expected_count, sizeof(*array), GFP_KERNEL);
629 if (!array)
630 return -EFAULT;
631
632 spin_lock(&fd->invalid_lock);
633 if (fd->invalid_tid_idx) {
634 memcpy(array, fd->invalid_tids, sizeof(*array) *
635 fd->invalid_tid_idx);
636 memset(fd->invalid_tids, 0, sizeof(*fd->invalid_tids) *
637 fd->invalid_tid_idx);
638 tinfo->tidcnt = fd->invalid_tid_idx;
639 fd->invalid_tid_idx = 0;
640 /*
641 * Reset the user flag while still holding the lock.
642 * Otherwise, PSM can miss events.
643 */
644 clear_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
645 } else {
646 tinfo->tidcnt = 0;
647 }
648 spin_unlock(&fd->invalid_lock);
649
650 if (tinfo->tidcnt) {
651 if (copy_to_user((void __user *)tinfo->tidlist,
652 array, sizeof(*array) * tinfo->tidcnt))
653 ret = -EFAULT;
654 }
655 kfree(array);
656
657 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500658}
659
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500660static u32 find_phys_blocks(struct page **pages, unsigned npages,
661 struct tid_pageset *list)
662{
663 unsigned pagecount, pageidx, setcount = 0, i;
664 unsigned long pfn, this_pfn;
665
666 if (!npages)
667 return 0;
668
669 /*
670 * Look for sets of physically contiguous pages in the user buffer.
671 * This will allow us to optimize Expected RcvArray entry usage by
672 * using the bigger supported sizes.
673 */
674 pfn = page_to_pfn(pages[0]);
675 for (pageidx = 0, pagecount = 1, i = 1; i <= npages; i++) {
676 this_pfn = i < npages ? page_to_pfn(pages[i]) : 0;
677
678 /*
679 * If the pfn's are not sequential, pages are not physically
680 * contiguous.
681 */
682 if (this_pfn != ++pfn) {
683 /*
684 * At this point we have to loop over the set of
685 * physically contiguous pages and break them down it
686 * sizes supported by the HW.
687 * There are two main constraints:
688 * 1. The max buffer size is MAX_EXPECTED_BUFFER.
689 * If the total set size is bigger than that
690 * program only a MAX_EXPECTED_BUFFER chunk.
691 * 2. The buffer size has to be a power of two. If
692 * it is not, round down to the closes power of
693 * 2 and program that size.
694 */
695 while (pagecount) {
696 int maxpages = pagecount;
697 u32 bufsize = pagecount * PAGE_SIZE;
698
699 if (bufsize > MAX_EXPECTED_BUFFER)
700 maxpages =
701 MAX_EXPECTED_BUFFER >>
702 PAGE_SHIFT;
703 else if (!is_power_of_2(bufsize))
704 maxpages =
705 rounddown_pow_of_two(bufsize) >>
706 PAGE_SHIFT;
707
708 list[setcount].idx = pageidx;
709 list[setcount].count = maxpages;
710 pagecount -= maxpages;
711 pageidx += maxpages;
712 setcount++;
713 }
714 pageidx = i;
715 pagecount = 1;
716 pfn = this_pfn;
717 } else {
718 pagecount++;
719 }
720 }
721 return setcount;
722}
723
724/**
725 * program_rcvarray() - program an RcvArray group with receive buffers
726 * @fp: file pointer
727 * @vaddr: starting user virtual address
728 * @grp: RcvArray group
729 * @sets: array of struct tid_pageset holding information on physically
730 * contiguous chunks from the user buffer
731 * @start: starting index into sets array
732 * @count: number of struct tid_pageset's to program
733 * @pages: an array of struct page * for the user buffer
734 * @tidlist: the array of u32 elements when the information about the
735 * programmed RcvArray entries is to be encoded.
736 * @tididx: starting offset into tidlist
737 * @pmapped: (output parameter) number of pages programmed into the RcvArray
738 * entries.
739 *
740 * This function will program up to 'count' number of RcvArray entries from the
741 * group 'grp'. To make best use of write-combining writes, the function will
742 * perform writes to the unused RcvArray entries which will be ignored by the
743 * HW. Each RcvArray entry will be programmed with a physically contiguous
744 * buffer chunk from the user's virtual buffer.
745 *
746 * Return:
747 * -EINVAL if the requested count is larger than the size of the group,
748 * -ENOMEM or -EFAULT on error from set_rcvarray_entry(), or
749 * number of RcvArray entries programmed.
750 */
751static int program_rcvarray(struct file *fp, unsigned long vaddr,
752 struct tid_group *grp,
753 struct tid_pageset *sets,
754 unsigned start, u16 count, struct page **pages,
755 u32 *tidlist, unsigned *tididx, unsigned *pmapped)
756{
757 struct hfi1_filedata *fd = fp->private_data;
758 struct hfi1_ctxtdata *uctxt = fd->uctxt;
759 struct hfi1_devdata *dd = uctxt->dd;
760 u16 idx;
761 u32 tidinfo = 0, rcventry, useidx = 0;
762 int mapped = 0;
763
764 /* Count should never be larger than the group size */
765 if (count > grp->size)
766 return -EINVAL;
767
768 /* Find the first unused entry in the group */
769 for (idx = 0; idx < grp->size; idx++) {
770 if (!(grp->map & (1 << idx))) {
771 useidx = idx;
772 break;
773 }
774 rcv_array_wc_fill(dd, grp->base + idx);
775 }
776
777 idx = 0;
778 while (idx < count) {
779 u16 npages, pageidx, setidx = start + idx;
780 int ret = 0;
781
782 /*
783 * If this entry in the group is used, move to the next one.
784 * If we go past the end of the group, exit the loop.
785 */
786 if (useidx >= grp->size) {
787 break;
788 } else if (grp->map & (1 << useidx)) {
789 rcv_array_wc_fill(dd, grp->base + useidx);
790 useidx++;
791 continue;
792 }
793
794 rcventry = grp->base + useidx;
795 npages = sets[setidx].count;
796 pageidx = sets[setidx].idx;
797
798 ret = set_rcvarray_entry(fp, vaddr + (pageidx * PAGE_SIZE),
799 rcventry, grp, pages + pageidx,
800 npages);
801 if (ret)
802 return ret;
803 mapped += npages;
804
805 tidinfo = rcventry2tidinfo(rcventry - uctxt->expected_base) |
806 EXP_TID_SET(LEN, npages);
807 tidlist[(*tididx)++] = tidinfo;
808 grp->used++;
809 grp->map |= 1 << useidx++;
810 idx++;
811 }
812
813 /* Fill the rest of the group with "blank" writes */
814 for (; useidx < grp->size; useidx++)
815 rcv_array_wc_fill(dd, grp->base + useidx);
816 *pmapped = mapped;
817 return idx;
818}
819
820static int set_rcvarray_entry(struct file *fp, unsigned long vaddr,
821 u32 rcventry, struct tid_group *grp,
822 struct page **pages, unsigned npages)
823{
824 int ret;
825 struct hfi1_filedata *fd = fp->private_data;
826 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800827 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500828 struct hfi1_devdata *dd = uctxt->dd;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500829 dma_addr_t phys;
830
831 /*
832 * Allocate the node first so we can handle a potential
833 * failure before we've programmed anything.
834 */
835 node = kzalloc(sizeof(*node) + (sizeof(struct page *) * npages),
836 GFP_KERNEL);
837 if (!node)
838 return -ENOMEM;
839
840 phys = pci_map_single(dd->pcidev,
841 __va(page_to_phys(pages[0])),
842 npages * PAGE_SIZE, PCI_DMA_FROMDEVICE);
843 if (dma_mapping_error(&dd->pcidev->dev, phys)) {
844 dd_dev_err(dd, "Failed to DMA map Exp Rcv pages 0x%llx\n",
845 phys);
846 kfree(node);
847 return -EFAULT;
848 }
849
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800850 node->mmu.addr = vaddr;
851 node->mmu.len = npages * PAGE_SIZE;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500852 node->phys = page_to_phys(pages[0]);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500853 node->npages = npages;
854 node->rcventry = rcventry;
855 node->dma_addr = phys;
856 node->grp = grp;
857 node->freed = false;
858 memcpy(node->pages, pages, sizeof(struct page *) * npages);
859
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800860 if (HFI1_CAP_IS_USET(TID_UNMAP))
Dean Luicke0b09ac2016-07-28 15:21:20 -0400861 ret = tid_rb_insert(fd, &node->mmu);
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800862 else
Dean Luicke0b09ac2016-07-28 15:21:20 -0400863 ret = hfi1_mmu_rb_insert(fd->handler, &node->mmu);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500864
865 if (ret) {
866 hfi1_cdbg(TID, "Failed to insert RB node %u 0x%lx, 0x%lx %d",
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800867 node->rcventry, node->mmu.addr, node->phys, ret);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500868 pci_unmap_single(dd->pcidev, phys, npages * PAGE_SIZE,
869 PCI_DMA_FROMDEVICE);
870 kfree(node);
871 return -EFAULT;
872 }
873 hfi1_put_tid(dd, rcventry, PT_EXPECTED, phys, ilog2(npages) + 1);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800874 trace_hfi1_exp_tid_reg(uctxt->ctxt, fd->subctxt, rcventry, npages,
875 node->mmu.addr, node->phys, phys);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500876 return 0;
877}
878
879static int unprogram_rcvarray(struct file *fp, u32 tidinfo,
880 struct tid_group **grp)
881{
882 struct hfi1_filedata *fd = fp->private_data;
883 struct hfi1_ctxtdata *uctxt = fd->uctxt;
884 struct hfi1_devdata *dd = uctxt->dd;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800885 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500886 u8 tidctrl = EXP_TID_GET(tidinfo, CTRL);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800887 u32 tididx = EXP_TID_GET(tidinfo, IDX) << 1, rcventry;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500888
889 if (tididx >= uctxt->expected_count) {
890 dd_dev_err(dd, "Invalid RcvArray entry (%u) index for ctxt %u\n",
891 tididx, uctxt->ctxt);
892 return -EINVAL;
893 }
894
895 if (tidctrl == 0x3)
896 return -EINVAL;
897
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800898 rcventry = tididx + (tidctrl - 1);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500899
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800900 node = fd->entry_to_rb[rcventry];
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800901 if (!node || node->rcventry != (uctxt->expected_base + rcventry))
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500902 return -EBADF;
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800903 if (HFI1_CAP_IS_USET(TID_UNMAP))
Dean Luicke0b09ac2016-07-28 15:21:20 -0400904 tid_rb_remove(fd, &node->mmu, fd->mm);
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800905 else
Dean Luicke0b09ac2016-07-28 15:21:20 -0400906 hfi1_mmu_rb_remove(fd->handler, &node->mmu);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800907
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500908 if (grp)
909 *grp = node->grp;
Ira Weiny5ed3b152016-07-28 12:27:32 -0400910 clear_tid_node(fd, node);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500911 return 0;
912}
913
Ira Weiny5ed3b152016-07-28 12:27:32 -0400914static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500915{
916 struct hfi1_ctxtdata *uctxt = fd->uctxt;
917 struct hfi1_devdata *dd = uctxt->dd;
918
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500919 trace_hfi1_exp_tid_unreg(uctxt->ctxt, fd->subctxt, node->rcventry,
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800920 node->npages, node->mmu.addr, node->phys,
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500921 node->dma_addr);
922
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500923 hfi1_put_tid(dd, node->rcventry, PT_INVALID, 0, 0);
924 /*
925 * Make sure device has seen the write before we unpin the
926 * pages.
927 */
928 flush_wc();
929
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800930 pci_unmap_single(dd->pcidev, node->dma_addr, node->mmu.len,
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500931 PCI_DMA_FROMDEVICE);
Ira Weiny3faa3d92016-07-28 15:21:19 -0400932 hfi1_release_user_pages(fd->mm, node->pages, node->npages, true);
Mitko Haralanova7922f72016-03-08 11:15:39 -0800933 fd->tid_n_pinned -= node->npages;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500934
935 node->grp->used--;
936 node->grp->map &= ~(1 << (node->rcventry - node->grp->base));
937
938 if (node->grp->used == node->grp->size - 1)
939 tid_group_move(node->grp, &uctxt->tid_full_list,
940 &uctxt->tid_used_list);
941 else if (!node->grp->used)
942 tid_group_move(node->grp, &uctxt->tid_used_list,
943 &uctxt->tid_group_list);
944 kfree(node);
945}
946
947static void unlock_exp_tids(struct hfi1_ctxtdata *uctxt,
Dean Luicke0b09ac2016-07-28 15:21:20 -0400948 struct exp_tid_set *set,
949 struct hfi1_filedata *fd)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500950{
951 struct tid_group *grp, *ptr;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500952 int i;
953
954 list_for_each_entry_safe(grp, ptr, &set->list, list) {
955 list_del_init(&grp->list);
956
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500957 for (i = 0; i < grp->size; i++) {
958 if (grp->map & (1 << i)) {
959 u16 rcventry = grp->base + i;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800960 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500961
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800962 node = fd->entry_to_rb[rcventry -
963 uctxt->expected_base];
964 if (!node || node->rcventry != rcventry)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500965 continue;
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800966 if (HFI1_CAP_IS_USET(TID_UNMAP))
Dean Luicke0b09ac2016-07-28 15:21:20 -0400967 tid_rb_remove(fd, &node->mmu, fd->mm);
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800968 else
Dean Luicke0b09ac2016-07-28 15:21:20 -0400969 hfi1_mmu_rb_remove(fd->handler,
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800970 &node->mmu);
Ira Weiny5ed3b152016-07-28 12:27:32 -0400971 clear_tid_node(fd, node);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500972 }
973 }
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500974 }
975}
976
Dean Luicke0b09ac2016-07-28 15:21:20 -0400977static int tid_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500978{
Dean Luicke0b09ac2016-07-28 15:21:20 -0400979 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800980 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
981 struct tid_rb_node *node =
982 container_of(mnode, struct tid_rb_node, mmu);
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500983
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800984 if (node->freed)
985 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500986
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800987 trace_hfi1_exp_tid_inval(uctxt->ctxt, fdata->subctxt, node->mmu.addr,
988 node->rcventry, node->npages, node->dma_addr);
989 node->freed = true;
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -0500990
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800991 spin_lock(&fdata->invalid_lock);
992 if (fdata->invalid_tid_idx < uctxt->expected_count) {
993 fdata->invalid_tids[fdata->invalid_tid_idx] =
994 rcventry2tidinfo(node->rcventry - uctxt->expected_base);
995 fdata->invalid_tids[fdata->invalid_tid_idx] |=
996 EXP_TID_SET(LEN, node->npages);
997 if (!fdata->invalid_tid_idx) {
998 unsigned long *ev;
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500999
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001000 /*
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001001 * hfi1_set_uevent_bits() sets a user event flag
1002 * for all processes. Because calling into the
1003 * driver to process TID cache invalidations is
1004 * expensive and TID cache invalidations are
1005 * handled on a per-process basis, we can
1006 * optimize this to set the flag only for the
1007 * process in question.
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001008 */
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001009 ev = uctxt->dd->events +
1010 (((uctxt->ctxt - uctxt->dd->first_user_ctxt) *
1011 HFI1_MAX_SHARED_CTXTS) + fdata->subctxt);
1012 set_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001013 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001014 fdata->invalid_tid_idx++;
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001015 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001016 spin_unlock(&fdata->invalid_lock);
1017 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001018}
1019
Dean Luicke0b09ac2016-07-28 15:21:20 -04001020static int tid_rb_insert(void *arg, struct mmu_rb_node *node)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001021{
Dean Luicke0b09ac2016-07-28 15:21:20 -04001022 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001023 struct tid_rb_node *tnode =
1024 container_of(node, struct tid_rb_node, mmu);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -08001025 u32 base = fdata->uctxt->expected_base;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001026
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001027 fdata->entry_to_rb[tnode->rcventry - base] = tnode;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001028 return 0;
1029}
1030
Dean Luicke0b09ac2016-07-28 15:21:20 -04001031static void tid_rb_remove(void *arg, struct mmu_rb_node *node,
Mitko Haralanovf19bd642016-04-12 10:45:57 -07001032 struct mm_struct *mm)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001033{
Dean Luicke0b09ac2016-07-28 15:21:20 -04001034 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001035 struct tid_rb_node *tnode =
1036 container_of(node, struct tid_rb_node, mmu);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -08001037 u32 base = fdata->uctxt->expected_base;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001038
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001039 fdata->entry_to_rb[tnode->rcventry - base] = NULL;
Mitko Haralanova92ba6d2016-02-03 14:34:41 -08001040}