blob: a2f7e719dc4d8195ac7216e009c7afeaddc6f0ba [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 *,
Mitko Haralanov3abb33a2016-02-05 11:57:54 -050085 struct rb_root *);
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 Luicka7cd2dc2016-07-28 12:27:37 -040089static int tid_rb_insert(struct rb_root *, struct mmu_rb_node *);
90static void tid_rb_remove(struct rb_root *, struct mmu_rb_node *,
Mitko Haralanovf19bd642016-04-12 10:45:57 -070091 struct mm_struct *);
Dean Luicka7cd2dc2016-07-28 12:27:37 -040092static int tid_rb_invalidate(struct rb_root *, 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 fd->tid_rb_root = RB_ROOT;
166
167 if (!uctxt->subctxt_cnt || !fd->subctxt) {
168 exp_tid_group_init(&uctxt->tid_group_list);
169 exp_tid_group_init(&uctxt->tid_used_list);
170 exp_tid_group_init(&uctxt->tid_full_list);
171
172 tidbase = uctxt->expected_base;
173 for (i = 0; i < uctxt->expected_count /
174 dd->rcv_entries.group_size; i++) {
175 struct tid_group *grp;
176
177 grp = kzalloc(sizeof(*grp), GFP_KERNEL);
178 if (!grp) {
179 /*
180 * If we fail here, the groups already
181 * allocated will be freed by the close
182 * call.
183 */
184 ret = -ENOMEM;
185 goto done;
186 }
187 grp->size = dd->rcv_entries.group_size;
188 grp->base = tidbase;
189 tid_group_add_tail(grp, &uctxt->tid_group_list);
190 tidbase += dd->rcv_entries.group_size;
191 }
192 }
193
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800194 fd->entry_to_rb = kcalloc(uctxt->expected_count,
195 sizeof(struct rb_node *),
196 GFP_KERNEL);
197 if (!fd->entry_to_rb)
198 return -ENOMEM;
199
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500200 if (!HFI1_CAP_IS_USET(TID_UNMAP)) {
201 fd->invalid_tid_idx = 0;
202 fd->invalid_tids = kzalloc(uctxt->expected_count *
203 sizeof(u32), GFP_KERNEL);
204 if (!fd->invalid_tids) {
205 ret = -ENOMEM;
206 goto done;
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800207 }
208
209 /*
210 * Register MMU notifier callbacks. If the registration
211 * fails, continue but turn off the TID caching for
212 * all user contexts.
213 */
Ira Weiny3faa3d92016-07-28 15:21:19 -0400214 ret = hfi1_mmu_rb_register(fd->mm, &fd->tid_rb_root,
215 &tid_rb_ops);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800216 if (ret) {
217 dd_dev_info(dd,
218 "Failed MMU notifier registration %d\n",
219 ret);
220 HFI1_CAP_USET(TID_UNMAP);
221 ret = 0;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500222 }
223 }
224
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500225 /*
226 * PSM does not have a good way to separate, count, and
227 * effectively enforce a limit on RcvArray entries used by
228 * subctxts (when context sharing is used) when TID caching
229 * is enabled. To help with that, we calculate a per-process
230 * RcvArray entry share and enforce that.
231 * If TID caching is not in use, PSM deals with usage on its
232 * own. In that case, we allow any subctxt to take all of the
233 * entries.
234 *
235 * Make sure that we set the tid counts only after successful
236 * init.
237 */
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500238 spin_lock(&fd->tid_lock);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500239 if (uctxt->subctxt_cnt && !HFI1_CAP_IS_USET(TID_UNMAP)) {
240 u16 remainder;
241
242 fd->tid_limit = uctxt->expected_count / uctxt->subctxt_cnt;
243 remainder = uctxt->expected_count % uctxt->subctxt_cnt;
244 if (remainder && fd->subctxt < remainder)
245 fd->tid_limit++;
246 } else {
247 fd->tid_limit = uctxt->expected_count;
248 }
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500249 spin_unlock(&fd->tid_lock);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500250done:
251 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500252}
253
254int hfi1_user_exp_rcv_free(struct hfi1_filedata *fd)
255{
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500256 struct hfi1_ctxtdata *uctxt = fd->uctxt;
257 struct tid_group *grp, *gptr;
258
Mitko Haralanov94158442016-04-20 06:05:36 -0700259 if (!test_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags))
260 return 0;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500261 /*
262 * The notifier would have been removed when the process'es mm
263 * was freed.
264 */
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800265 if (!HFI1_CAP_IS_USET(TID_UNMAP))
266 hfi1_mmu_rb_unregister(&fd->tid_rb_root);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500267
268 kfree(fd->invalid_tids);
269
270 if (!uctxt->cnt) {
271 if (!EXP_TID_SET_EMPTY(uctxt->tid_full_list))
272 unlock_exp_tids(uctxt, &uctxt->tid_full_list,
273 &fd->tid_rb_root);
274 if (!EXP_TID_SET_EMPTY(uctxt->tid_used_list))
275 unlock_exp_tids(uctxt, &uctxt->tid_used_list,
276 &fd->tid_rb_root);
277 list_for_each_entry_safe(grp, gptr, &uctxt->tid_group_list.list,
278 list) {
279 list_del_init(&grp->list);
280 kfree(grp);
281 }
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500282 hfi1_clear_tids(uctxt);
283 }
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800284
285 kfree(fd->entry_to_rb);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500286 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500287}
288
Mitko Haralanovb8abe342016-02-05 11:57:51 -0500289/*
290 * Write an "empty" RcvArray entry.
291 * This function exists so the TID registaration code can use it
292 * to write to unused/unneeded entries and still take advantage
293 * of the WC performance improvements. The HFI will ignore this
294 * write to the RcvArray entry.
295 */
296static inline void rcv_array_wc_fill(struct hfi1_devdata *dd, u32 index)
297{
298 /*
299 * Doing the WC fill writes only makes sense if the device is
300 * present and the RcvArray has been mapped as WC memory.
301 */
302 if ((dd->flags & HFI1_PRESENT) && dd->rcvarray_wc)
303 writeq(0, dd->rcvarray_wc + (index * 8));
304}
305
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500306/*
307 * RcvArray entry allocation for Expected Receives is done by the
308 * following algorithm:
309 *
310 * The context keeps 3 lists of groups of RcvArray entries:
311 * 1. List of empty groups - tid_group_list
312 * This list is created during user context creation and
313 * contains elements which describe sets (of 8) of empty
314 * RcvArray entries.
315 * 2. List of partially used groups - tid_used_list
316 * This list contains sets of RcvArray entries which are
317 * not completely used up. Another mapping request could
318 * use some of all of the remaining entries.
319 * 3. List of full groups - tid_full_list
320 * This is the list where sets that are completely used
321 * up go.
322 *
323 * An attempt to optimize the usage of RcvArray entries is
324 * made by finding all sets of physically contiguous pages in a
325 * user's buffer.
326 * These physically contiguous sets are further split into
327 * sizes supported by the receive engine of the HFI. The
328 * resulting sets of pages are stored in struct tid_pageset,
329 * which describes the sets as:
330 * * .count - number of pages in this set
331 * * .idx - starting index into struct page ** array
332 * of this set
333 *
334 * From this point on, the algorithm deals with the page sets
335 * described above. The number of pagesets is divided by the
336 * RcvArray group size to produce the number of full groups
337 * needed.
338 *
339 * Groups from the 3 lists are manipulated using the following
340 * rules:
341 * 1. For each set of 8 pagesets, a complete group from
342 * tid_group_list is taken, programmed, and moved to
343 * the tid_full_list list.
344 * 2. For all remaining pagesets:
345 * 2.1 If the tid_used_list is empty and the tid_group_list
346 * is empty, stop processing pageset and return only
347 * what has been programmed up to this point.
348 * 2.2 If the tid_used_list is empty and the tid_group_list
349 * is not empty, move a group from tid_group_list to
350 * tid_used_list.
351 * 2.3 For each group is tid_used_group, program as much as
352 * can fit into the group. If the group becomes fully
353 * used, move it to tid_full_list.
354 */
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500355int hfi1_user_exp_rcv_setup(struct file *fp, struct hfi1_tid_info *tinfo)
356{
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500357 int ret = 0, need_group = 0, pinned;
358 struct hfi1_filedata *fd = fp->private_data;
359 struct hfi1_ctxtdata *uctxt = fd->uctxt;
360 struct hfi1_devdata *dd = uctxt->dd;
361 unsigned npages, ngroups, pageidx = 0, pageset_count, npagesets,
362 tididx = 0, mapped, mapped_pages = 0;
363 unsigned long vaddr = tinfo->vaddr;
364 struct page **pages = NULL;
365 u32 *tidlist = NULL;
366 struct tid_pageset *pagesets = NULL;
367
368 /* Get the number of pages the user buffer spans */
369 npages = num_user_pages(vaddr, tinfo->length);
370 if (!npages)
371 return -EINVAL;
372
373 if (npages > uctxt->expected_count) {
374 dd_dev_err(dd, "Expected buffer too big\n");
375 return -EINVAL;
376 }
377
378 /* Verify that access is OK for the user buffer */
379 if (!access_ok(VERIFY_WRITE, (void __user *)vaddr,
380 npages * PAGE_SIZE)) {
381 dd_dev_err(dd, "Fail vaddr %p, %u pages, !access_ok\n",
382 (void *)vaddr, npages);
383 return -EFAULT;
384 }
385
386 pagesets = kcalloc(uctxt->expected_count, sizeof(*pagesets),
387 GFP_KERNEL);
388 if (!pagesets)
389 return -ENOMEM;
390
391 /* Allocate the array of struct page pointers needed for pinning */
392 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
393 if (!pages) {
394 ret = -ENOMEM;
395 goto bail;
396 }
397
398 /*
399 * Pin all the pages of the user buffer. If we can't pin all the
400 * pages, accept the amount pinned so far and program only that.
401 * User space knows how to deal with partially programmed buffers.
402 */
Ira Weiny3faa3d92016-07-28 15:21:19 -0400403 if (!hfi1_can_pin_pages(dd, fd->mm, fd->tid_n_pinned, npages)) {
Mitko Haralanov0ad2d3d2016-04-12 10:46:29 -0700404 ret = -ENOMEM;
405 goto bail;
406 }
407
Ira Weiny3faa3d92016-07-28 15:21:19 -0400408 pinned = hfi1_acquire_user_pages(fd->mm, vaddr, npages, true, pages);
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500409 if (pinned <= 0) {
410 ret = pinned;
411 goto bail;
412 }
Mitko Haralanova7922f72016-03-08 11:15:39 -0800413 fd->tid_n_pinned += npages;
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500414
415 /* Find sets of physically contiguous pages */
416 npagesets = find_phys_blocks(pages, pinned, pagesets);
417
418 /*
419 * We don't need to access this under a lock since tid_used is per
420 * process and the same process cannot be in hfi1_user_exp_rcv_clear()
421 * and hfi1_user_exp_rcv_setup() at the same time.
422 */
423 spin_lock(&fd->tid_lock);
424 if (fd->tid_used + npagesets > fd->tid_limit)
425 pageset_count = fd->tid_limit - fd->tid_used;
426 else
427 pageset_count = npagesets;
428 spin_unlock(&fd->tid_lock);
429
430 if (!pageset_count)
431 goto bail;
432
433 ngroups = pageset_count / dd->rcv_entries.group_size;
434 tidlist = kcalloc(pageset_count, sizeof(*tidlist), GFP_KERNEL);
435 if (!tidlist) {
436 ret = -ENOMEM;
437 goto nomem;
438 }
439
440 tididx = 0;
441
442 /*
443 * From this point on, we are going to be using shared (between master
444 * and subcontexts) context resources. We need to take the lock.
445 */
446 mutex_lock(&uctxt->exp_lock);
447 /*
448 * The first step is to program the RcvArray entries which are complete
449 * groups.
450 */
451 while (ngroups && uctxt->tid_group_list.count) {
452 struct tid_group *grp =
453 tid_group_pop(&uctxt->tid_group_list);
454
455 ret = program_rcvarray(fp, vaddr, grp, pagesets,
456 pageidx, dd->rcv_entries.group_size,
457 pages, tidlist, &tididx, &mapped);
458 /*
459 * If there was a failure to program the RcvArray
460 * entries for the entire group, reset the grp fields
461 * and add the grp back to the free group list.
462 */
463 if (ret <= 0) {
464 tid_group_add_tail(grp, &uctxt->tid_group_list);
465 hfi1_cdbg(TID,
466 "Failed to program RcvArray group %d", ret);
467 goto unlock;
468 }
469
470 tid_group_add_tail(grp, &uctxt->tid_full_list);
471 ngroups--;
472 pageidx += ret;
473 mapped_pages += mapped;
474 }
475
476 while (pageidx < pageset_count) {
477 struct tid_group *grp, *ptr;
478 /*
479 * If we don't have any partially used tid groups, check
480 * if we have empty groups. If so, take one from there and
481 * put in the partially used list.
482 */
483 if (!uctxt->tid_used_list.count || need_group) {
484 if (!uctxt->tid_group_list.count)
485 goto unlock;
486
487 grp = tid_group_pop(&uctxt->tid_group_list);
488 tid_group_add_tail(grp, &uctxt->tid_used_list);
489 need_group = 0;
490 }
491 /*
492 * There is an optimization opportunity here - instead of
493 * fitting as many page sets as we can, check for a group
494 * later on in the list that could fit all of them.
495 */
496 list_for_each_entry_safe(grp, ptr, &uctxt->tid_used_list.list,
497 list) {
498 unsigned use = min_t(unsigned, pageset_count - pageidx,
499 grp->size - grp->used);
500
501 ret = program_rcvarray(fp, vaddr, grp, pagesets,
502 pageidx, use, pages, tidlist,
503 &tididx, &mapped);
504 if (ret < 0) {
505 hfi1_cdbg(TID,
506 "Failed to program RcvArray entries %d",
507 ret);
508 ret = -EFAULT;
509 goto unlock;
510 } else if (ret > 0) {
511 if (grp->used == grp->size)
512 tid_group_move(grp,
513 &uctxt->tid_used_list,
514 &uctxt->tid_full_list);
515 pageidx += ret;
516 mapped_pages += mapped;
517 need_group = 0;
518 /* Check if we are done so we break out early */
519 if (pageidx >= pageset_count)
520 break;
521 } else if (WARN_ON(ret == 0)) {
522 /*
523 * If ret is 0, we did not program any entries
524 * into this group, which can only happen if
525 * we've screwed up the accounting somewhere.
526 * Warn and try to continue.
527 */
528 need_group = 1;
529 }
530 }
531 }
532unlock:
533 mutex_unlock(&uctxt->exp_lock);
534nomem:
535 hfi1_cdbg(TID, "total mapped: tidpairs:%u pages:%u (%d)", tididx,
536 mapped_pages, ret);
537 if (tididx) {
538 spin_lock(&fd->tid_lock);
539 fd->tid_used += tididx;
540 spin_unlock(&fd->tid_lock);
541 tinfo->tidcnt = tididx;
542 tinfo->length = mapped_pages * PAGE_SIZE;
543
544 if (copy_to_user((void __user *)(unsigned long)tinfo->tidlist,
545 tidlist, sizeof(tidlist[0]) * tididx)) {
546 /*
547 * On failure to copy to the user level, we need to undo
548 * everything done so far so we don't leak resources.
549 */
550 tinfo->tidlist = (unsigned long)&tidlist;
551 hfi1_user_exp_rcv_clear(fp, tinfo);
552 tinfo->tidlist = 0;
553 ret = -EFAULT;
554 goto bail;
555 }
556 }
557
558 /*
559 * If not everything was mapped (due to insufficient RcvArray entries,
560 * for example), unpin all unmapped pages so we can pin them nex time.
561 */
Mitko Haralanova7922f72016-03-08 11:15:39 -0800562 if (mapped_pages != pinned) {
Ira Weiny3faa3d92016-07-28 15:21:19 -0400563 hfi1_release_user_pages(fd->mm, &pages[mapped_pages],
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500564 pinned - mapped_pages,
565 false);
Mitko Haralanova7922f72016-03-08 11:15:39 -0800566 fd->tid_n_pinned -= pinned - mapped_pages;
567 }
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500568bail:
569 kfree(pagesets);
570 kfree(pages);
571 kfree(tidlist);
572 return ret > 0 ? 0 : ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500573}
574
575int hfi1_user_exp_rcv_clear(struct file *fp, struct hfi1_tid_info *tinfo)
576{
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500577 int ret = 0;
578 struct hfi1_filedata *fd = fp->private_data;
579 struct hfi1_ctxtdata *uctxt = fd->uctxt;
580 u32 *tidinfo;
581 unsigned tididx;
582
583 tidinfo = kcalloc(tinfo->tidcnt, sizeof(*tidinfo), GFP_KERNEL);
584 if (!tidinfo)
585 return -ENOMEM;
586
587 if (copy_from_user(tidinfo, (void __user *)(unsigned long)
588 tinfo->tidlist, sizeof(tidinfo[0]) *
589 tinfo->tidcnt)) {
590 ret = -EFAULT;
591 goto done;
592 }
593
594 mutex_lock(&uctxt->exp_lock);
595 for (tididx = 0; tididx < tinfo->tidcnt; tididx++) {
596 ret = unprogram_rcvarray(fp, tidinfo[tididx], NULL);
597 if (ret) {
598 hfi1_cdbg(TID, "Failed to unprogram rcv array %d",
599 ret);
600 break;
601 }
602 }
603 spin_lock(&fd->tid_lock);
604 fd->tid_used -= tididx;
605 spin_unlock(&fd->tid_lock);
606 tinfo->tidcnt = tididx;
607 mutex_unlock(&uctxt->exp_lock);
608done:
609 kfree(tidinfo);
610 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500611}
612
613int hfi1_user_exp_rcv_invalid(struct file *fp, struct hfi1_tid_info *tinfo)
614{
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500615 struct hfi1_filedata *fd = fp->private_data;
616 struct hfi1_ctxtdata *uctxt = fd->uctxt;
617 unsigned long *ev = uctxt->dd->events +
618 (((uctxt->ctxt - uctxt->dd->first_user_ctxt) *
619 HFI1_MAX_SHARED_CTXTS) + fd->subctxt);
620 u32 *array;
621 int ret = 0;
622
623 if (!fd->invalid_tids)
624 return -EINVAL;
625
626 /*
627 * copy_to_user() can sleep, which will leave the invalid_lock
628 * locked and cause the MMU notifier to be blocked on the lock
629 * for a long time.
630 * Copy the data to a local buffer so we can release the lock.
631 */
632 array = kcalloc(uctxt->expected_count, sizeof(*array), GFP_KERNEL);
633 if (!array)
634 return -EFAULT;
635
636 spin_lock(&fd->invalid_lock);
637 if (fd->invalid_tid_idx) {
638 memcpy(array, fd->invalid_tids, sizeof(*array) *
639 fd->invalid_tid_idx);
640 memset(fd->invalid_tids, 0, sizeof(*fd->invalid_tids) *
641 fd->invalid_tid_idx);
642 tinfo->tidcnt = fd->invalid_tid_idx;
643 fd->invalid_tid_idx = 0;
644 /*
645 * Reset the user flag while still holding the lock.
646 * Otherwise, PSM can miss events.
647 */
648 clear_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
649 } else {
650 tinfo->tidcnt = 0;
651 }
652 spin_unlock(&fd->invalid_lock);
653
654 if (tinfo->tidcnt) {
655 if (copy_to_user((void __user *)tinfo->tidlist,
656 array, sizeof(*array) * tinfo->tidcnt))
657 ret = -EFAULT;
658 }
659 kfree(array);
660
661 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500662}
663
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500664static u32 find_phys_blocks(struct page **pages, unsigned npages,
665 struct tid_pageset *list)
666{
667 unsigned pagecount, pageidx, setcount = 0, i;
668 unsigned long pfn, this_pfn;
669
670 if (!npages)
671 return 0;
672
673 /*
674 * Look for sets of physically contiguous pages in the user buffer.
675 * This will allow us to optimize Expected RcvArray entry usage by
676 * using the bigger supported sizes.
677 */
678 pfn = page_to_pfn(pages[0]);
679 for (pageidx = 0, pagecount = 1, i = 1; i <= npages; i++) {
680 this_pfn = i < npages ? page_to_pfn(pages[i]) : 0;
681
682 /*
683 * If the pfn's are not sequential, pages are not physically
684 * contiguous.
685 */
686 if (this_pfn != ++pfn) {
687 /*
688 * At this point we have to loop over the set of
689 * physically contiguous pages and break them down it
690 * sizes supported by the HW.
691 * There are two main constraints:
692 * 1. The max buffer size is MAX_EXPECTED_BUFFER.
693 * If the total set size is bigger than that
694 * program only a MAX_EXPECTED_BUFFER chunk.
695 * 2. The buffer size has to be a power of two. If
696 * it is not, round down to the closes power of
697 * 2 and program that size.
698 */
699 while (pagecount) {
700 int maxpages = pagecount;
701 u32 bufsize = pagecount * PAGE_SIZE;
702
703 if (bufsize > MAX_EXPECTED_BUFFER)
704 maxpages =
705 MAX_EXPECTED_BUFFER >>
706 PAGE_SHIFT;
707 else if (!is_power_of_2(bufsize))
708 maxpages =
709 rounddown_pow_of_two(bufsize) >>
710 PAGE_SHIFT;
711
712 list[setcount].idx = pageidx;
713 list[setcount].count = maxpages;
714 pagecount -= maxpages;
715 pageidx += maxpages;
716 setcount++;
717 }
718 pageidx = i;
719 pagecount = 1;
720 pfn = this_pfn;
721 } else {
722 pagecount++;
723 }
724 }
725 return setcount;
726}
727
728/**
729 * program_rcvarray() - program an RcvArray group with receive buffers
730 * @fp: file pointer
731 * @vaddr: starting user virtual address
732 * @grp: RcvArray group
733 * @sets: array of struct tid_pageset holding information on physically
734 * contiguous chunks from the user buffer
735 * @start: starting index into sets array
736 * @count: number of struct tid_pageset's to program
737 * @pages: an array of struct page * for the user buffer
738 * @tidlist: the array of u32 elements when the information about the
739 * programmed RcvArray entries is to be encoded.
740 * @tididx: starting offset into tidlist
741 * @pmapped: (output parameter) number of pages programmed into the RcvArray
742 * entries.
743 *
744 * This function will program up to 'count' number of RcvArray entries from the
745 * group 'grp'. To make best use of write-combining writes, the function will
746 * perform writes to the unused RcvArray entries which will be ignored by the
747 * HW. Each RcvArray entry will be programmed with a physically contiguous
748 * buffer chunk from the user's virtual buffer.
749 *
750 * Return:
751 * -EINVAL if the requested count is larger than the size of the group,
752 * -ENOMEM or -EFAULT on error from set_rcvarray_entry(), or
753 * number of RcvArray entries programmed.
754 */
755static int program_rcvarray(struct file *fp, unsigned long vaddr,
756 struct tid_group *grp,
757 struct tid_pageset *sets,
758 unsigned start, u16 count, struct page **pages,
759 u32 *tidlist, unsigned *tididx, unsigned *pmapped)
760{
761 struct hfi1_filedata *fd = fp->private_data;
762 struct hfi1_ctxtdata *uctxt = fd->uctxt;
763 struct hfi1_devdata *dd = uctxt->dd;
764 u16 idx;
765 u32 tidinfo = 0, rcventry, useidx = 0;
766 int mapped = 0;
767
768 /* Count should never be larger than the group size */
769 if (count > grp->size)
770 return -EINVAL;
771
772 /* Find the first unused entry in the group */
773 for (idx = 0; idx < grp->size; idx++) {
774 if (!(grp->map & (1 << idx))) {
775 useidx = idx;
776 break;
777 }
778 rcv_array_wc_fill(dd, grp->base + idx);
779 }
780
781 idx = 0;
782 while (idx < count) {
783 u16 npages, pageidx, setidx = start + idx;
784 int ret = 0;
785
786 /*
787 * If this entry in the group is used, move to the next one.
788 * If we go past the end of the group, exit the loop.
789 */
790 if (useidx >= grp->size) {
791 break;
792 } else if (grp->map & (1 << useidx)) {
793 rcv_array_wc_fill(dd, grp->base + useidx);
794 useidx++;
795 continue;
796 }
797
798 rcventry = grp->base + useidx;
799 npages = sets[setidx].count;
800 pageidx = sets[setidx].idx;
801
802 ret = set_rcvarray_entry(fp, vaddr + (pageidx * PAGE_SIZE),
803 rcventry, grp, pages + pageidx,
804 npages);
805 if (ret)
806 return ret;
807 mapped += npages;
808
809 tidinfo = rcventry2tidinfo(rcventry - uctxt->expected_base) |
810 EXP_TID_SET(LEN, npages);
811 tidlist[(*tididx)++] = tidinfo;
812 grp->used++;
813 grp->map |= 1 << useidx++;
814 idx++;
815 }
816
817 /* Fill the rest of the group with "blank" writes */
818 for (; useidx < grp->size; useidx++)
819 rcv_array_wc_fill(dd, grp->base + useidx);
820 *pmapped = mapped;
821 return idx;
822}
823
824static int set_rcvarray_entry(struct file *fp, unsigned long vaddr,
825 u32 rcventry, struct tid_group *grp,
826 struct page **pages, unsigned npages)
827{
828 int ret;
829 struct hfi1_filedata *fd = fp->private_data;
830 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800831 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500832 struct hfi1_devdata *dd = uctxt->dd;
833 struct rb_root *root = &fd->tid_rb_root;
834 dma_addr_t phys;
835
836 /*
837 * Allocate the node first so we can handle a potential
838 * failure before we've programmed anything.
839 */
840 node = kzalloc(sizeof(*node) + (sizeof(struct page *) * npages),
841 GFP_KERNEL);
842 if (!node)
843 return -ENOMEM;
844
845 phys = pci_map_single(dd->pcidev,
846 __va(page_to_phys(pages[0])),
847 npages * PAGE_SIZE, PCI_DMA_FROMDEVICE);
848 if (dma_mapping_error(&dd->pcidev->dev, phys)) {
849 dd_dev_err(dd, "Failed to DMA map Exp Rcv pages 0x%llx\n",
850 phys);
851 kfree(node);
852 return -EFAULT;
853 }
854
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800855 node->mmu.addr = vaddr;
856 node->mmu.len = npages * PAGE_SIZE;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500857 node->phys = page_to_phys(pages[0]);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500858 node->npages = npages;
859 node->rcventry = rcventry;
860 node->dma_addr = phys;
861 node->grp = grp;
862 node->freed = false;
863 memcpy(node->pages, pages, sizeof(struct page *) * npages);
864
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800865 if (HFI1_CAP_IS_USET(TID_UNMAP))
Dean Luicka7cd2dc2016-07-28 12:27:37 -0400866 ret = tid_rb_insert(root, &node->mmu);
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800867 else
868 ret = hfi1_mmu_rb_insert(root, &node->mmu);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500869
870 if (ret) {
871 hfi1_cdbg(TID, "Failed to insert RB node %u 0x%lx, 0x%lx %d",
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800872 node->rcventry, node->mmu.addr, node->phys, ret);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500873 pci_unmap_single(dd->pcidev, phys, npages * PAGE_SIZE,
874 PCI_DMA_FROMDEVICE);
875 kfree(node);
876 return -EFAULT;
877 }
878 hfi1_put_tid(dd, rcventry, PT_EXPECTED, phys, ilog2(npages) + 1);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800879 trace_hfi1_exp_tid_reg(uctxt->ctxt, fd->subctxt, rcventry, npages,
880 node->mmu.addr, node->phys, phys);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500881 return 0;
882}
883
884static int unprogram_rcvarray(struct file *fp, u32 tidinfo,
885 struct tid_group **grp)
886{
887 struct hfi1_filedata *fd = fp->private_data;
888 struct hfi1_ctxtdata *uctxt = fd->uctxt;
889 struct hfi1_devdata *dd = uctxt->dd;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800890 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500891 u8 tidctrl = EXP_TID_GET(tidinfo, CTRL);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800892 u32 tididx = EXP_TID_GET(tidinfo, IDX) << 1, rcventry;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500893
894 if (tididx >= uctxt->expected_count) {
895 dd_dev_err(dd, "Invalid RcvArray entry (%u) index for ctxt %u\n",
896 tididx, uctxt->ctxt);
897 return -EINVAL;
898 }
899
900 if (tidctrl == 0x3)
901 return -EINVAL;
902
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800903 rcventry = tididx + (tidctrl - 1);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500904
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800905 node = fd->entry_to_rb[rcventry];
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800906 if (!node || node->rcventry != (uctxt->expected_base + rcventry))
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500907 return -EBADF;
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800908 if (HFI1_CAP_IS_USET(TID_UNMAP))
Ira Weiny3faa3d92016-07-28 15:21:19 -0400909 tid_rb_remove(&fd->tid_rb_root, &node->mmu, fd->mm);
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800910 else
911 hfi1_mmu_rb_remove(&fd->tid_rb_root, &node->mmu);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800912
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500913 if (grp)
914 *grp = node->grp;
Ira Weiny5ed3b152016-07-28 12:27:32 -0400915 clear_tid_node(fd, node);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500916 return 0;
917}
918
Ira Weiny5ed3b152016-07-28 12:27:32 -0400919static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500920{
921 struct hfi1_ctxtdata *uctxt = fd->uctxt;
922 struct hfi1_devdata *dd = uctxt->dd;
923
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500924 trace_hfi1_exp_tid_unreg(uctxt->ctxt, fd->subctxt, node->rcventry,
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800925 node->npages, node->mmu.addr, node->phys,
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500926 node->dma_addr);
927
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500928 hfi1_put_tid(dd, node->rcventry, PT_INVALID, 0, 0);
929 /*
930 * Make sure device has seen the write before we unpin the
931 * pages.
932 */
933 flush_wc();
934
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800935 pci_unmap_single(dd->pcidev, node->dma_addr, node->mmu.len,
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500936 PCI_DMA_FROMDEVICE);
Ira Weiny3faa3d92016-07-28 15:21:19 -0400937 hfi1_release_user_pages(fd->mm, node->pages, node->npages, true);
Mitko Haralanova7922f72016-03-08 11:15:39 -0800938 fd->tid_n_pinned -= node->npages;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500939
940 node->grp->used--;
941 node->grp->map &= ~(1 << (node->rcventry - node->grp->base));
942
943 if (node->grp->used == node->grp->size - 1)
944 tid_group_move(node->grp, &uctxt->tid_full_list,
945 &uctxt->tid_used_list);
946 else if (!node->grp->used)
947 tid_group_move(node->grp, &uctxt->tid_used_list,
948 &uctxt->tid_group_list);
949 kfree(node);
950}
951
952static void unlock_exp_tids(struct hfi1_ctxtdata *uctxt,
953 struct exp_tid_set *set, struct rb_root *root)
954{
955 struct tid_group *grp, *ptr;
956 struct hfi1_filedata *fd = container_of(root, struct hfi1_filedata,
957 tid_rb_root);
958 int i;
959
960 list_for_each_entry_safe(grp, ptr, &set->list, list) {
961 list_del_init(&grp->list);
962
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500963 for (i = 0; i < grp->size; i++) {
964 if (grp->map & (1 << i)) {
965 u16 rcventry = grp->base + i;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800966 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500967
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800968 node = fd->entry_to_rb[rcventry -
969 uctxt->expected_base];
970 if (!node || node->rcventry != rcventry)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500971 continue;
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800972 if (HFI1_CAP_IS_USET(TID_UNMAP))
Dean Luicka7cd2dc2016-07-28 12:27:37 -0400973 tid_rb_remove(&fd->tid_rb_root,
Ira Weiny3faa3d92016-07-28 15:21:19 -0400974 &node->mmu, fd->mm);
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800975 else
976 hfi1_mmu_rb_remove(&fd->tid_rb_root,
977 &node->mmu);
Ira Weiny5ed3b152016-07-28 12:27:32 -0400978 clear_tid_node(fd, node);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500979 }
980 }
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500981 }
982}
983
Dean Luicka7cd2dc2016-07-28 12:27:37 -0400984static int tid_rb_invalidate(struct rb_root *root, struct mmu_rb_node *mnode)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500985{
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800986 struct hfi1_filedata *fdata =
987 container_of(root, struct hfi1_filedata, tid_rb_root);
988 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
989 struct tid_rb_node *node =
990 container_of(mnode, struct tid_rb_node, mmu);
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500991
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800992 if (node->freed)
993 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500994
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800995 trace_hfi1_exp_tid_inval(uctxt->ctxt, fdata->subctxt, node->mmu.addr,
996 node->rcventry, node->npages, node->dma_addr);
997 node->freed = true;
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -0500998
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800999 spin_lock(&fdata->invalid_lock);
1000 if (fdata->invalid_tid_idx < uctxt->expected_count) {
1001 fdata->invalid_tids[fdata->invalid_tid_idx] =
1002 rcventry2tidinfo(node->rcventry - uctxt->expected_base);
1003 fdata->invalid_tids[fdata->invalid_tid_idx] |=
1004 EXP_TID_SET(LEN, node->npages);
1005 if (!fdata->invalid_tid_idx) {
1006 unsigned long *ev;
Mitko Haralanov0b091fb2016-02-05 11:57:58 -05001007
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001008 /*
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001009 * hfi1_set_uevent_bits() sets a user event flag
1010 * for all processes. Because calling into the
1011 * driver to process TID cache invalidations is
1012 * expensive and TID cache invalidations are
1013 * handled on a per-process basis, we can
1014 * optimize this to set the flag only for the
1015 * process in question.
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001016 */
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001017 ev = uctxt->dd->events +
1018 (((uctxt->ctxt - uctxt->dd->first_user_ctxt) *
1019 HFI1_MAX_SHARED_CTXTS) + fdata->subctxt);
1020 set_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001021 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001022 fdata->invalid_tid_idx++;
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001023 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001024 spin_unlock(&fdata->invalid_lock);
1025 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001026}
1027
Dean Luicka7cd2dc2016-07-28 12:27:37 -04001028static int tid_rb_insert(struct rb_root *root, struct mmu_rb_node *node)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001029{
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001030 struct hfi1_filedata *fdata =
1031 container_of(root, struct hfi1_filedata, tid_rb_root);
1032 struct tid_rb_node *tnode =
1033 container_of(node, struct tid_rb_node, mmu);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -08001034 u32 base = fdata->uctxt->expected_base;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001035
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001036 fdata->entry_to_rb[tnode->rcventry - base] = tnode;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001037 return 0;
1038}
1039
Dean Luicka7cd2dc2016-07-28 12:27:37 -04001040static void tid_rb_remove(struct rb_root *root, struct mmu_rb_node *node,
Mitko Haralanovf19bd642016-04-12 10:45:57 -07001041 struct mm_struct *mm)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001042{
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001043 struct hfi1_filedata *fdata =
1044 container_of(root, struct hfi1_filedata, tid_rb_root);
1045 struct tid_rb_node *tnode =
1046 container_of(node, struct tid_rb_node, mmu);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -08001047 u32 base = fdata->uctxt->expected_base;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001048
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001049 fdata->entry_to_rb[tnode->rcventry - base] = NULL;
Mitko Haralanova92ba6d2016-02-03 14:34:41 -08001050}