2 * Copyright(c) 2020 Cornelis Networks, Inc.
3 * Copyright(c) 2015-2020 Intel Corporation.
5 * This file is provided under a dual BSD/GPLv2 license. When using or
6 * redistributing this file, you may do so under either license.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
25 * - Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * - Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in
29 * the documentation and/or other materials provided with the
31 * - Neither the name of Intel Corporation nor the names of its
32 * contributors may be used to endorse or promote products derived
33 * from this software without specific prior written permission.
35 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 #include <linux/poll.h>
49 #include <linux/cdev.h>
50 #include <linux/vmalloc.h>
52 #include <linux/sched/mm.h>
53 #include <linux/bitmap.h>
63 #include "user_sdma.h"
64 #include "user_exp_rcv.h"
68 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
70 #define SEND_CTXT_HALT_TIMEOUT 1000 /* msecs */
73 * File operation functions
75 static int hfi1_file_open(struct inode
*inode
, struct file
*fp
);
76 static int hfi1_file_close(struct inode
*inode
, struct file
*fp
);
77 static ssize_t
hfi1_write_iter(struct kiocb
*kiocb
, struct iov_iter
*from
);
78 static __poll_t
hfi1_poll(struct file
*fp
, struct poll_table_struct
*pt
);
79 static int hfi1_file_mmap(struct file
*fp
, struct vm_area_struct
*vma
);
81 static u64
kvirt_to_phys(void *addr
);
82 static int assign_ctxt(struct hfi1_filedata
*fd
, unsigned long arg
, u32 len
);
83 static void init_subctxts(struct hfi1_ctxtdata
*uctxt
,
84 const struct hfi1_user_info
*uinfo
);
85 static int init_user_ctxt(struct hfi1_filedata
*fd
,
86 struct hfi1_ctxtdata
*uctxt
);
87 static void user_init(struct hfi1_ctxtdata
*uctxt
);
88 static int get_ctxt_info(struct hfi1_filedata
*fd
, unsigned long arg
, u32 len
);
89 static int get_base_info(struct hfi1_filedata
*fd
, unsigned long arg
, u32 len
);
90 static int user_exp_rcv_setup(struct hfi1_filedata
*fd
, unsigned long arg
,
92 static int user_exp_rcv_clear(struct hfi1_filedata
*fd
, unsigned long arg
,
94 static int user_exp_rcv_invalid(struct hfi1_filedata
*fd
, unsigned long arg
,
96 static int setup_base_ctxt(struct hfi1_filedata
*fd
,
97 struct hfi1_ctxtdata
*uctxt
);
98 static int setup_subctxt(struct hfi1_ctxtdata
*uctxt
);
100 static int find_sub_ctxt(struct hfi1_filedata
*fd
,
101 const struct hfi1_user_info
*uinfo
);
102 static int allocate_ctxt(struct hfi1_filedata
*fd
, struct hfi1_devdata
*dd
,
103 struct hfi1_user_info
*uinfo
,
104 struct hfi1_ctxtdata
**cd
);
105 static void deallocate_ctxt(struct hfi1_ctxtdata
*uctxt
);
106 static __poll_t
poll_urgent(struct file
*fp
, struct poll_table_struct
*pt
);
107 static __poll_t
poll_next(struct file
*fp
, struct poll_table_struct
*pt
);
108 static int user_event_ack(struct hfi1_ctxtdata
*uctxt
, u16 subctxt
,
110 static int set_ctxt_pkey(struct hfi1_ctxtdata
*uctxt
, unsigned long arg
);
111 static int ctxt_reset(struct hfi1_ctxtdata
*uctxt
);
112 static int manage_rcvq(struct hfi1_ctxtdata
*uctxt
, u16 subctxt
,
114 static vm_fault_t
vma_fault(struct vm_fault
*vmf
);
115 static long hfi1_file_ioctl(struct file
*fp
, unsigned int cmd
,
118 static const struct file_operations hfi1_file_ops
= {
119 .owner
= THIS_MODULE
,
120 .write_iter
= hfi1_write_iter
,
121 .open
= hfi1_file_open
,
122 .release
= hfi1_file_close
,
123 .unlocked_ioctl
= hfi1_file_ioctl
,
125 .mmap
= hfi1_file_mmap
,
126 .llseek
= noop_llseek
,
129 static const struct vm_operations_struct vm_ops
= {
134 * Types of memories mapped into user processes' space
153 * Masks and offsets defining the mmap tokens
155 #define HFI1_MMAP_OFFSET_MASK 0xfffULL
156 #define HFI1_MMAP_OFFSET_SHIFT 0
157 #define HFI1_MMAP_SUBCTXT_MASK 0xfULL
158 #define HFI1_MMAP_SUBCTXT_SHIFT 12
159 #define HFI1_MMAP_CTXT_MASK 0xffULL
160 #define HFI1_MMAP_CTXT_SHIFT 16
161 #define HFI1_MMAP_TYPE_MASK 0xfULL
162 #define HFI1_MMAP_TYPE_SHIFT 24
163 #define HFI1_MMAP_MAGIC_MASK 0xffffffffULL
164 #define HFI1_MMAP_MAGIC_SHIFT 32
166 #define HFI1_MMAP_MAGIC 0xdabbad00
168 #define HFI1_MMAP_TOKEN_SET(field, val) \
169 (((val) & HFI1_MMAP_##field##_MASK) << HFI1_MMAP_##field##_SHIFT)
170 #define HFI1_MMAP_TOKEN_GET(field, token) \
171 (((token) >> HFI1_MMAP_##field##_SHIFT) & HFI1_MMAP_##field##_MASK)
172 #define HFI1_MMAP_TOKEN(type, ctxt, subctxt, addr) \
173 (HFI1_MMAP_TOKEN_SET(MAGIC, HFI1_MMAP_MAGIC) | \
174 HFI1_MMAP_TOKEN_SET(TYPE, type) | \
175 HFI1_MMAP_TOKEN_SET(CTXT, ctxt) | \
176 HFI1_MMAP_TOKEN_SET(SUBCTXT, subctxt) | \
177 HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr))))
179 #define dbg(fmt, ...) \
180 pr_info(fmt, ##__VA_ARGS__)
182 static inline int is_valid_mmap(u64 token
)
184 return (HFI1_MMAP_TOKEN_GET(MAGIC
, token
) == HFI1_MMAP_MAGIC
);
187 static int hfi1_file_open(struct inode
*inode
, struct file
*fp
)
189 struct hfi1_filedata
*fd
;
190 struct hfi1_devdata
*dd
= container_of(inode
->i_cdev
,
194 if (!((dd
->flags
& HFI1_PRESENT
) && dd
->kregbase1
))
197 if (!atomic_inc_not_zero(&dd
->user_refcount
))
200 /* The real work is performed later in assign_ctxt() */
202 fd
= kzalloc(sizeof(*fd
), GFP_KERNEL
);
204 if (!fd
|| init_srcu_struct(&fd
->pq_srcu
))
206 spin_lock_init(&fd
->pq_rcu_lock
);
207 spin_lock_init(&fd
->tid_lock
);
208 spin_lock_init(&fd
->invalid_lock
);
209 fd
->rec_cpu_num
= -1; /* no cpu affinity by default */
211 fp
->private_data
= fd
;
215 fp
->private_data
= NULL
;
216 if (atomic_dec_and_test(&dd
->user_refcount
))
217 complete(&dd
->user_comp
);
221 static long hfi1_file_ioctl(struct file
*fp
, unsigned int cmd
,
224 struct hfi1_filedata
*fd
= fp
->private_data
;
225 struct hfi1_ctxtdata
*uctxt
= fd
->uctxt
;
229 hfi1_cdbg(IOCTL
, "IOCTL recv: 0x%x", cmd
);
230 if (cmd
!= HFI1_IOCTL_ASSIGN_CTXT
&&
231 cmd
!= HFI1_IOCTL_GET_VERS
&&
236 case HFI1_IOCTL_ASSIGN_CTXT
:
237 ret
= assign_ctxt(fd
, arg
, _IOC_SIZE(cmd
));
240 case HFI1_IOCTL_CTXT_INFO
:
241 ret
= get_ctxt_info(fd
, arg
, _IOC_SIZE(cmd
));
244 case HFI1_IOCTL_USER_INFO
:
245 ret
= get_base_info(fd
, arg
, _IOC_SIZE(cmd
));
248 case HFI1_IOCTL_CREDIT_UPD
:
250 sc_return_credits(uctxt
->sc
);
253 case HFI1_IOCTL_TID_UPDATE
:
254 ret
= user_exp_rcv_setup(fd
, arg
, _IOC_SIZE(cmd
));
257 case HFI1_IOCTL_TID_FREE
:
258 ret
= user_exp_rcv_clear(fd
, arg
, _IOC_SIZE(cmd
));
261 case HFI1_IOCTL_TID_INVAL_READ
:
262 ret
= user_exp_rcv_invalid(fd
, arg
, _IOC_SIZE(cmd
));
265 case HFI1_IOCTL_RECV_CTRL
:
266 ret
= manage_rcvq(uctxt
, fd
->subctxt
, arg
);
269 case HFI1_IOCTL_POLL_TYPE
:
270 if (get_user(uval
, (int __user
*)arg
))
272 uctxt
->poll_type
= (typeof(uctxt
->poll_type
))uval
;
275 case HFI1_IOCTL_ACK_EVENT
:
276 ret
= user_event_ack(uctxt
, fd
->subctxt
, arg
);
279 case HFI1_IOCTL_SET_PKEY
:
280 ret
= set_ctxt_pkey(uctxt
, arg
);
283 case HFI1_IOCTL_CTXT_RESET
:
284 ret
= ctxt_reset(uctxt
);
287 case HFI1_IOCTL_GET_VERS
:
288 uval
= HFI1_USER_SWVERSION
;
289 if (put_user(uval
, (int __user
*)arg
))
300 static ssize_t
hfi1_write_iter(struct kiocb
*kiocb
, struct iov_iter
*from
)
302 struct hfi1_filedata
*fd
= kiocb
->ki_filp
->private_data
;
303 struct hfi1_user_sdma_pkt_q
*pq
;
304 struct hfi1_user_sdma_comp_q
*cq
= fd
->cq
;
305 int done
= 0, reqs
= 0;
306 unsigned long dim
= from
->nr_segs
;
309 idx
= srcu_read_lock(&fd
->pq_srcu
);
310 pq
= srcu_dereference(fd
->pq
, &fd
->pq_srcu
);
312 srcu_read_unlock(&fd
->pq_srcu
, idx
);
316 if (!iter_is_iovec(from
) || !dim
) {
317 srcu_read_unlock(&fd
->pq_srcu
, idx
);
321 trace_hfi1_sdma_request(fd
->dd
, fd
->uctxt
->ctxt
, fd
->subctxt
, dim
);
323 if (atomic_read(&pq
->n_reqs
) == pq
->n_max_reqs
) {
324 srcu_read_unlock(&fd
->pq_srcu
, idx
);
330 unsigned long count
= 0;
332 ret
= hfi1_user_sdma_process_request(
333 fd
, (struct iovec
*)(from
->iov
+ done
),
344 srcu_read_unlock(&fd
->pq_srcu
, idx
);
348 static int hfi1_file_mmap(struct file
*fp
, struct vm_area_struct
*vma
)
350 struct hfi1_filedata
*fd
= fp
->private_data
;
351 struct hfi1_ctxtdata
*uctxt
= fd
->uctxt
;
352 struct hfi1_devdata
*dd
;
354 u64 token
= vma
->vm_pgoff
<< PAGE_SHIFT
,
356 void *memvirt
= NULL
;
357 u8 subctxt
, mapio
= 0, vmf
= 0, type
;
362 if (!is_valid_mmap(token
) || !uctxt
||
363 !(vma
->vm_flags
& VM_SHARED
)) {
368 ctxt
= HFI1_MMAP_TOKEN_GET(CTXT
, token
);
369 subctxt
= HFI1_MMAP_TOKEN_GET(SUBCTXT
, token
);
370 type
= HFI1_MMAP_TOKEN_GET(TYPE
, token
);
371 if (ctxt
!= uctxt
->ctxt
|| subctxt
!= fd
->subctxt
) {
376 flags
= vma
->vm_flags
;
381 memaddr
= ((dd
->physaddr
+ TXE_PIO_SEND
) +
383 (uctxt
->sc
->hw_context
* BIT(16))) +
384 /* 64K PIO space / ctxt */
385 (type
== PIO_BUFS_SOP
?
386 (TXE_PIO_SIZE
/ 2) : 0); /* sop? */
388 * Map only the amount allocated to the context, not the
389 * entire available context's PIO space.
391 memlen
= PAGE_ALIGN(uctxt
->sc
->credits
* PIO_BLOCK_SIZE
);
392 flags
&= ~VM_MAYREAD
;
393 flags
|= VM_DONTCOPY
| VM_DONTEXPAND
;
394 vma
->vm_page_prot
= pgprot_writecombine(vma
->vm_page_prot
);
398 if (flags
& VM_WRITE
) {
403 * The credit return location for this context could be on the
404 * second or third page allocated for credit returns (if number
405 * of enabled contexts > 64 and 128 respectively).
407 memvirt
= dd
->cr_base
[uctxt
->numa_id
].va
;
408 memaddr
= virt_to_phys(memvirt
) +
409 (((u64
)uctxt
->sc
->hw_free
-
410 (u64
)dd
->cr_base
[uctxt
->numa_id
].va
) & PAGE_MASK
);
412 flags
&= ~VM_MAYWRITE
;
413 flags
|= VM_DONTCOPY
| VM_DONTEXPAND
;
415 * The driver has already allocated memory for credit
416 * returns and programmed it into the chip. Has that
417 * memory been flagged as non-cached?
419 /* vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); */
423 memlen
= rcvhdrq_size(uctxt
);
424 memvirt
= uctxt
->rcvhdrq
;
430 * The RcvEgr buffer need to be handled differently
431 * as multiple non-contiguous pages need to be mapped
432 * into the user process.
434 memlen
= uctxt
->egrbufs
.size
;
435 if ((vma
->vm_end
- vma
->vm_start
) != memlen
) {
436 dd_dev_err(dd
, "Eager buffer map size invalid (%lu != %lu)\n",
437 (vma
->vm_end
- vma
->vm_start
), memlen
);
441 if (vma
->vm_flags
& VM_WRITE
) {
445 vma
->vm_flags
&= ~VM_MAYWRITE
;
446 addr
= vma
->vm_start
;
447 for (i
= 0 ; i
< uctxt
->egrbufs
.numbufs
; i
++) {
448 memlen
= uctxt
->egrbufs
.buffers
[i
].len
;
449 memvirt
= uctxt
->egrbufs
.buffers
[i
].addr
;
450 ret
= remap_pfn_range(
453 * virt_to_pfn() does the same, but
454 * it's not available on x86_64
455 * when CONFIG_MMU is enabled.
457 PFN_DOWN(__pa(memvirt
)),
469 * Map only the page that contains this context's user
472 memaddr
= (unsigned long)
473 (dd
->physaddr
+ RXE_PER_CONTEXT_USER
)
474 + (uctxt
->ctxt
* RXE_PER_CONTEXT_SIZE
);
476 * TidFlow table is on the same page as the rest of the
480 flags
|= VM_DONTCOPY
| VM_DONTEXPAND
;
481 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
486 * Use the page where this context's flags are. User level
487 * knows where it's own bitmap is within the page.
489 memaddr
= (unsigned long)
490 (dd
->events
+ uctxt_offset(uctxt
)) & PAGE_MASK
;
493 * v3.7 removes VM_RESERVED but the effect is kept by
496 flags
|= VM_IO
| VM_DONTEXPAND
;
500 if (flags
& VM_WRITE
) {
504 memaddr
= kvirt_to_phys((void *)dd
->status
);
506 flags
|= VM_IO
| VM_DONTEXPAND
;
509 if (!HFI1_CAP_IS_USET(DMA_RTAIL
)) {
511 * If the memory allocation failed, the context alloc
512 * also would have failed, so we would never get here
517 if ((flags
& VM_WRITE
) || !hfi1_rcvhdrtail_kvaddr(uctxt
)) {
522 memvirt
= (void *)hfi1_rcvhdrtail_kvaddr(uctxt
);
523 flags
&= ~VM_MAYWRITE
;
526 memaddr
= (u64
)uctxt
->subctxt_uregbase
;
528 flags
|= VM_IO
| VM_DONTEXPAND
;
531 case SUBCTXT_RCV_HDRQ
:
532 memaddr
= (u64
)uctxt
->subctxt_rcvhdr_base
;
533 memlen
= rcvhdrq_size(uctxt
) * uctxt
->subctxt_cnt
;
534 flags
|= VM_IO
| VM_DONTEXPAND
;
538 memaddr
= (u64
)uctxt
->subctxt_rcvegrbuf
;
539 memlen
= uctxt
->egrbufs
.size
* uctxt
->subctxt_cnt
;
540 flags
|= VM_IO
| VM_DONTEXPAND
;
541 flags
&= ~VM_MAYWRITE
;
545 struct hfi1_user_sdma_comp_q
*cq
= fd
->cq
;
551 memaddr
= (u64
)cq
->comps
;
552 memlen
= PAGE_ALIGN(sizeof(*cq
->comps
) * cq
->nentries
);
553 flags
|= VM_IO
| VM_DONTEXPAND
;
562 if ((vma
->vm_end
- vma
->vm_start
) != memlen
) {
563 hfi1_cdbg(PROC
, "%u:%u Memory size mismatch %lu:%lu",
564 uctxt
->ctxt
, fd
->subctxt
,
565 (vma
->vm_end
- vma
->vm_start
), memlen
);
570 vma
->vm_flags
= flags
;
572 "%u:%u type:%u io/vf:%d/%d, addr:0x%llx, len:%lu(%lu), flags:0x%lx\n",
573 ctxt
, subctxt
, type
, mapio
, vmf
, memaddr
, memlen
,
574 vma
->vm_end
- vma
->vm_start
, vma
->vm_flags
);
576 vma
->vm_pgoff
= PFN_DOWN(memaddr
);
577 vma
->vm_ops
= &vm_ops
;
580 ret
= io_remap_pfn_range(vma
, vma
->vm_start
,
584 } else if (memvirt
) {
585 ret
= remap_pfn_range(vma
, vma
->vm_start
,
586 PFN_DOWN(__pa(memvirt
)),
590 ret
= remap_pfn_range(vma
, vma
->vm_start
,
600 * Local (non-chip) user memory is not mapped right away but as it is
601 * accessed by the user-level code.
603 static vm_fault_t
vma_fault(struct vm_fault
*vmf
)
607 page
= vmalloc_to_page((void *)(vmf
->pgoff
<< PAGE_SHIFT
));
609 return VM_FAULT_SIGBUS
;
617 static __poll_t
hfi1_poll(struct file
*fp
, struct poll_table_struct
*pt
)
619 struct hfi1_ctxtdata
*uctxt
;
622 uctxt
= ((struct hfi1_filedata
*)fp
->private_data
)->uctxt
;
625 else if (uctxt
->poll_type
== HFI1_POLL_TYPE_URGENT
)
626 pollflag
= poll_urgent(fp
, pt
);
627 else if (uctxt
->poll_type
== HFI1_POLL_TYPE_ANYRCV
)
628 pollflag
= poll_next(fp
, pt
);
635 static int hfi1_file_close(struct inode
*inode
, struct file
*fp
)
637 struct hfi1_filedata
*fdata
= fp
->private_data
;
638 struct hfi1_ctxtdata
*uctxt
= fdata
->uctxt
;
639 struct hfi1_devdata
*dd
= container_of(inode
->i_cdev
,
642 unsigned long flags
, *ev
;
644 fp
->private_data
= NULL
;
649 hfi1_cdbg(PROC
, "closing ctxt %u:%u", uctxt
->ctxt
, fdata
->subctxt
);
652 /* drain user sdma queue */
653 hfi1_user_sdma_free_queues(fdata
, uctxt
);
655 /* release the cpu */
656 hfi1_put_proc_affinity(fdata
->rec_cpu_num
);
658 /* clean up rcv side */
659 hfi1_user_exp_rcv_free(fdata
);
662 * fdata->uctxt is used in the above cleanup. It is not ready to be
663 * removed until here.
669 * Clear any left over, unhandled events so the next process that
670 * gets this context doesn't get confused.
672 ev
= dd
->events
+ uctxt_offset(uctxt
) + fdata
->subctxt
;
675 spin_lock_irqsave(&dd
->uctxt_lock
, flags
);
676 __clear_bit(fdata
->subctxt
, uctxt
->in_use_ctxts
);
677 if (!bitmap_empty(uctxt
->in_use_ctxts
, HFI1_MAX_SHARED_CTXTS
)) {
678 spin_unlock_irqrestore(&dd
->uctxt_lock
, flags
);
681 spin_unlock_irqrestore(&dd
->uctxt_lock
, flags
);
684 * Disable receive context and interrupt available, reset all
685 * RcvCtxtCtrl bits to default values.
687 hfi1_rcvctrl(dd
, HFI1_RCVCTRL_CTXT_DIS
|
688 HFI1_RCVCTRL_TIDFLOW_DIS
|
689 HFI1_RCVCTRL_INTRAVAIL_DIS
|
690 HFI1_RCVCTRL_TAILUPD_DIS
|
691 HFI1_RCVCTRL_ONE_PKT_EGR_DIS
|
692 HFI1_RCVCTRL_NO_RHQ_DROP_DIS
|
693 HFI1_RCVCTRL_NO_EGR_DROP_DIS
|
694 HFI1_RCVCTRL_URGENT_DIS
, uctxt
);
695 /* Clear the context's J_KEY */
696 hfi1_clear_ctxt_jkey(dd
, uctxt
);
698 * If a send context is allocated, reset context integrity
699 * checks to default and disable the send context.
702 sc_disable(uctxt
->sc
);
703 set_pio_integrity(uctxt
->sc
);
706 hfi1_free_ctxt_rcv_groups(uctxt
);
707 hfi1_clear_ctxt_pkey(dd
, uctxt
);
709 uctxt
->event_flags
= 0;
711 deallocate_ctxt(uctxt
);
714 if (atomic_dec_and_test(&dd
->user_refcount
))
715 complete(&dd
->user_comp
);
717 cleanup_srcu_struct(&fdata
->pq_srcu
);
723 * Convert kernel *virtual* addresses to physical addresses.
724 * This is used to vmalloc'ed addresses.
726 static u64
kvirt_to_phys(void *addr
)
731 page
= vmalloc_to_page(addr
);
733 paddr
= page_to_pfn(page
) << PAGE_SHIFT
;
740 * @fd: valid filedata pointer
742 * Sub-context info can only be set up after the base context
743 * has been completed. This is indicated by the clearing of the
744 * HFI1_CTXT_BASE_UINIT bit.
746 * Wait for the bit to be cleared, and then complete the subcontext
750 static int complete_subctxt(struct hfi1_filedata
*fd
)
756 * sub-context info can only be set up after the base context
757 * has been completed.
759 ret
= wait_event_interruptible(
761 !test_bit(HFI1_CTXT_BASE_UNINIT
, &fd
->uctxt
->event_flags
));
763 if (test_bit(HFI1_CTXT_BASE_FAILED
, &fd
->uctxt
->event_flags
))
766 /* Finish the sub-context init */
768 fd
->rec_cpu_num
= hfi1_get_proc_affinity(fd
->uctxt
->numa_id
);
769 ret
= init_user_ctxt(fd
, fd
->uctxt
);
773 spin_lock_irqsave(&fd
->dd
->uctxt_lock
, flags
);
774 __clear_bit(fd
->subctxt
, fd
->uctxt
->in_use_ctxts
);
775 spin_unlock_irqrestore(&fd
->dd
->uctxt_lock
, flags
);
776 hfi1_rcd_put(fd
->uctxt
);
783 static int assign_ctxt(struct hfi1_filedata
*fd
, unsigned long arg
, u32 len
)
786 unsigned int swmajor
;
787 struct hfi1_ctxtdata
*uctxt
= NULL
;
788 struct hfi1_user_info uinfo
;
793 if (sizeof(uinfo
) != len
)
796 if (copy_from_user(&uinfo
, (void __user
*)arg
, sizeof(uinfo
)))
799 swmajor
= uinfo
.userversion
>> 16;
800 if (swmajor
!= HFI1_USER_SWMAJOR
)
803 if (uinfo
.subctxt_cnt
> HFI1_MAX_SHARED_CTXTS
)
807 * Acquire the mutex to protect against multiple creations of what
808 * could be a shared base context.
810 mutex_lock(&hfi1_mutex
);
812 * Get a sub context if available (fd->uctxt will be set).
813 * ret < 0 error, 0 no context, 1 sub-context found
815 ret
= find_sub_ctxt(fd
, &uinfo
);
818 * Allocate a base context if context sharing is not required or a
819 * sub context wasn't found.
822 ret
= allocate_ctxt(fd
, fd
->dd
, &uinfo
, &uctxt
);
824 mutex_unlock(&hfi1_mutex
);
826 /* Depending on the context type, finish the appropriate init */
829 ret
= setup_base_ctxt(fd
, uctxt
);
831 deallocate_ctxt(uctxt
);
834 ret
= complete_subctxt(fd
);
845 * @fd: valid filedata pointer
846 * @uinfo: user info to compare base context with
847 * @uctxt: context to compare uinfo to.
849 * Compare the given context with the given information to see if it
850 * can be used for a sub context.
852 static int match_ctxt(struct hfi1_filedata
*fd
,
853 const struct hfi1_user_info
*uinfo
,
854 struct hfi1_ctxtdata
*uctxt
)
856 struct hfi1_devdata
*dd
= fd
->dd
;
860 /* Skip dynamically allocated kernel contexts */
861 if (uctxt
->sc
&& (uctxt
->sc
->type
== SC_KERNEL
))
864 /* Skip ctxt if it doesn't match the requested one */
865 if (memcmp(uctxt
->uuid
, uinfo
->uuid
, sizeof(uctxt
->uuid
)) ||
866 uctxt
->jkey
!= generate_jkey(current_uid()) ||
867 uctxt
->subctxt_id
!= uinfo
->subctxt_id
||
868 uctxt
->subctxt_cnt
!= uinfo
->subctxt_cnt
)
871 /* Verify the sharing process matches the base */
872 if (uctxt
->userversion
!= uinfo
->userversion
)
875 /* Find an unused sub context */
876 spin_lock_irqsave(&dd
->uctxt_lock
, flags
);
877 if (bitmap_empty(uctxt
->in_use_ctxts
, HFI1_MAX_SHARED_CTXTS
)) {
878 /* context is being closed, do not use */
879 spin_unlock_irqrestore(&dd
->uctxt_lock
, flags
);
883 subctxt
= find_first_zero_bit(uctxt
->in_use_ctxts
,
884 HFI1_MAX_SHARED_CTXTS
);
885 if (subctxt
>= uctxt
->subctxt_cnt
) {
886 spin_unlock_irqrestore(&dd
->uctxt_lock
, flags
);
890 fd
->subctxt
= subctxt
;
891 __set_bit(fd
->subctxt
, uctxt
->in_use_ctxts
);
892 spin_unlock_irqrestore(&dd
->uctxt_lock
, flags
);
902 * @fd: valid filedata pointer
903 * @uinfo: matching info to use to find a possible context to share.
905 * The hfi1_mutex must be held when this function is called. It is
906 * necessary to ensure serialized creation of shared contexts.
909 * 0 No sub-context found
910 * 1 Subcontext found and allocated
911 * errno EINVAL (incorrect parameters)
912 * EBUSY (all sub contexts in use)
914 static int find_sub_ctxt(struct hfi1_filedata
*fd
,
915 const struct hfi1_user_info
*uinfo
)
917 struct hfi1_ctxtdata
*uctxt
;
918 struct hfi1_devdata
*dd
= fd
->dd
;
922 if (!uinfo
->subctxt_cnt
)
925 for (i
= dd
->first_dyn_alloc_ctxt
; i
< dd
->num_rcv_contexts
; i
++) {
926 uctxt
= hfi1_rcd_get_by_index(dd
, i
);
928 ret
= match_ctxt(fd
, uinfo
, uctxt
);
930 /* value of != 0 will return */
939 static int allocate_ctxt(struct hfi1_filedata
*fd
, struct hfi1_devdata
*dd
,
940 struct hfi1_user_info
*uinfo
,
941 struct hfi1_ctxtdata
**rcd
)
943 struct hfi1_ctxtdata
*uctxt
;
946 if (dd
->flags
& HFI1_FROZEN
) {
948 * Pick an error that is unique from all other errors
949 * that are returned so the user process knows that
950 * it tried to allocate while the SPC was frozen. It
951 * it should be able to retry with success in a short
961 * If we don't have a NUMA node requested, preference is towards
964 fd
->rec_cpu_num
= hfi1_get_proc_affinity(dd
->node
);
965 if (fd
->rec_cpu_num
!= -1)
966 numa
= cpu_to_node(fd
->rec_cpu_num
);
968 numa
= numa_node_id();
969 ret
= hfi1_create_ctxtdata(dd
->pport
, numa
, &uctxt
);
971 dd_dev_err(dd
, "user ctxtdata allocation failed\n");
974 hfi1_cdbg(PROC
, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)",
975 uctxt
->ctxt
, fd
->subctxt
, current
->pid
, fd
->rec_cpu_num
,
979 * Allocate and enable a PIO send context.
981 uctxt
->sc
= sc_alloc(dd
, SC_USER
, uctxt
->rcvhdrqentsize
, dd
->node
);
986 hfi1_cdbg(PROC
, "allocated send context %u(%u)\n", uctxt
->sc
->sw_index
,
987 uctxt
->sc
->hw_context
);
988 ret
= sc_enable(uctxt
->sc
);
993 * Setup sub context information if the user-level has requested
995 * This has to be done here so the rest of the sub-contexts find the
996 * proper base context.
997 * NOTE: _set_bit() can be used here because the context creation is
998 * protected by the mutex (rather than the spin_lock), and will be the
999 * very first instance of this context.
1001 __set_bit(0, uctxt
->in_use_ctxts
);
1002 if (uinfo
->subctxt_cnt
)
1003 init_subctxts(uctxt
, uinfo
);
1004 uctxt
->userversion
= uinfo
->userversion
;
1005 uctxt
->flags
= hfi1_cap_mask
; /* save current flag state */
1006 init_waitqueue_head(&uctxt
->wait
);
1007 strlcpy(uctxt
->comm
, current
->comm
, sizeof(uctxt
->comm
));
1008 memcpy(uctxt
->uuid
, uinfo
->uuid
, sizeof(uctxt
->uuid
));
1009 uctxt
->jkey
= generate_jkey(current_uid());
1010 hfi1_stats
.sps_ctxts
++;
1012 * Disable ASPM when there are open user/PSM contexts to avoid
1013 * issues with ASPM L1 exit latency
1015 if (dd
->freectxts
-- == dd
->num_user_contexts
)
1016 aspm_disable_all(dd
);
1023 hfi1_free_ctxt(uctxt
);
1027 static void deallocate_ctxt(struct hfi1_ctxtdata
*uctxt
)
1029 mutex_lock(&hfi1_mutex
);
1030 hfi1_stats
.sps_ctxts
--;
1031 if (++uctxt
->dd
->freectxts
== uctxt
->dd
->num_user_contexts
)
1032 aspm_enable_all(uctxt
->dd
);
1033 mutex_unlock(&hfi1_mutex
);
1035 hfi1_free_ctxt(uctxt
);
1038 static void init_subctxts(struct hfi1_ctxtdata
*uctxt
,
1039 const struct hfi1_user_info
*uinfo
)
1041 uctxt
->subctxt_cnt
= uinfo
->subctxt_cnt
;
1042 uctxt
->subctxt_id
= uinfo
->subctxt_id
;
1043 set_bit(HFI1_CTXT_BASE_UNINIT
, &uctxt
->event_flags
);
1046 static int setup_subctxt(struct hfi1_ctxtdata
*uctxt
)
1049 u16 num_subctxts
= uctxt
->subctxt_cnt
;
1051 uctxt
->subctxt_uregbase
= vmalloc_user(PAGE_SIZE
);
1052 if (!uctxt
->subctxt_uregbase
)
1055 /* We can take the size of the RcvHdr Queue from the master */
1056 uctxt
->subctxt_rcvhdr_base
= vmalloc_user(rcvhdrq_size(uctxt
) *
1058 if (!uctxt
->subctxt_rcvhdr_base
) {
1063 uctxt
->subctxt_rcvegrbuf
= vmalloc_user(uctxt
->egrbufs
.size
*
1065 if (!uctxt
->subctxt_rcvegrbuf
) {
1073 vfree(uctxt
->subctxt_rcvhdr_base
);
1074 uctxt
->subctxt_rcvhdr_base
= NULL
;
1076 vfree(uctxt
->subctxt_uregbase
);
1077 uctxt
->subctxt_uregbase
= NULL
;
1082 static void user_init(struct hfi1_ctxtdata
*uctxt
)
1084 unsigned int rcvctrl_ops
= 0;
1086 /* initialize poll variables... */
1088 uctxt
->urgent_poll
= 0;
1091 * Now enable the ctxt for receive.
1092 * For chips that are set to DMA the tail register to memory
1093 * when they change (and when the update bit transitions from
1094 * 0 to 1. So for those chips, we turn it off and then back on.
1095 * This will (very briefly) affect any other open ctxts, but the
1096 * duration is very short, and therefore isn't an issue. We
1097 * explicitly set the in-memory tail copy to 0 beforehand, so we
1098 * don't have to wait to be sure the DMA update has happened
1099 * (chip resets head/tail to 0 on transition to enable).
1101 if (hfi1_rcvhdrtail_kvaddr(uctxt
))
1102 clear_rcvhdrtail(uctxt
);
1104 /* Setup J_KEY before enabling the context */
1105 hfi1_set_ctxt_jkey(uctxt
->dd
, uctxt
, uctxt
->jkey
);
1107 rcvctrl_ops
= HFI1_RCVCTRL_CTXT_ENB
;
1108 rcvctrl_ops
|= HFI1_RCVCTRL_URGENT_ENB
;
1109 if (HFI1_CAP_UGET_MASK(uctxt
->flags
, HDRSUPP
))
1110 rcvctrl_ops
|= HFI1_RCVCTRL_TIDFLOW_ENB
;
1112 * Ignore the bit in the flags for now until proper
1113 * support for multiple packet per rcv array entry is
1116 if (!HFI1_CAP_UGET_MASK(uctxt
->flags
, MULTI_PKT_EGR
))
1117 rcvctrl_ops
|= HFI1_RCVCTRL_ONE_PKT_EGR_ENB
;
1118 if (HFI1_CAP_UGET_MASK(uctxt
->flags
, NODROP_EGR_FULL
))
1119 rcvctrl_ops
|= HFI1_RCVCTRL_NO_EGR_DROP_ENB
;
1120 if (HFI1_CAP_UGET_MASK(uctxt
->flags
, NODROP_RHQ_FULL
))
1121 rcvctrl_ops
|= HFI1_RCVCTRL_NO_RHQ_DROP_ENB
;
1123 * The RcvCtxtCtrl.TailUpd bit has to be explicitly written.
1124 * We can't rely on the correct value to be set from prior
1125 * uses of the chip or ctxt. Therefore, add the rcvctrl op
1128 if (HFI1_CAP_UGET_MASK(uctxt
->flags
, DMA_RTAIL
))
1129 rcvctrl_ops
|= HFI1_RCVCTRL_TAILUPD_ENB
;
1131 rcvctrl_ops
|= HFI1_RCVCTRL_TAILUPD_DIS
;
1132 hfi1_rcvctrl(uctxt
->dd
, rcvctrl_ops
, uctxt
);
1135 static int get_ctxt_info(struct hfi1_filedata
*fd
, unsigned long arg
, u32 len
)
1137 struct hfi1_ctxt_info cinfo
;
1138 struct hfi1_ctxtdata
*uctxt
= fd
->uctxt
;
1140 if (sizeof(cinfo
) != len
)
1143 memset(&cinfo
, 0, sizeof(cinfo
));
1144 cinfo
.runtime_flags
= (((uctxt
->flags
>> HFI1_CAP_MISC_SHIFT
) &
1145 HFI1_CAP_MISC_MASK
) << HFI1_CAP_USER_SHIFT
) |
1146 HFI1_CAP_UGET_MASK(uctxt
->flags
, MASK
) |
1147 HFI1_CAP_KGET_MASK(uctxt
->flags
, K2U
);
1148 /* adjust flag if this fd is not able to cache */
1150 cinfo
.runtime_flags
|= HFI1_CAP_TID_UNMAP
; /* no caching */
1152 cinfo
.num_active
= hfi1_count_active_units();
1153 cinfo
.unit
= uctxt
->dd
->unit
;
1154 cinfo
.ctxt
= uctxt
->ctxt
;
1155 cinfo
.subctxt
= fd
->subctxt
;
1156 cinfo
.rcvtids
= roundup(uctxt
->egrbufs
.alloced
,
1157 uctxt
->dd
->rcv_entries
.group_size
) +
1158 uctxt
->expected_count
;
1159 cinfo
.credits
= uctxt
->sc
->credits
;
1160 cinfo
.numa_node
= uctxt
->numa_id
;
1161 cinfo
.rec_cpu
= fd
->rec_cpu_num
;
1162 cinfo
.send_ctxt
= uctxt
->sc
->hw_context
;
1164 cinfo
.egrtids
= uctxt
->egrbufs
.alloced
;
1165 cinfo
.rcvhdrq_cnt
= get_hdrq_cnt(uctxt
);
1166 cinfo
.rcvhdrq_entsize
= get_hdrqentsize(uctxt
) << 2;
1167 cinfo
.sdma_ring_size
= fd
->cq
->nentries
;
1168 cinfo
.rcvegr_size
= uctxt
->egrbufs
.rcvtid_size
;
1170 trace_hfi1_ctxt_info(uctxt
->dd
, uctxt
->ctxt
, fd
->subctxt
, &cinfo
);
1171 if (copy_to_user((void __user
*)arg
, &cinfo
, len
))
1177 static int init_user_ctxt(struct hfi1_filedata
*fd
,
1178 struct hfi1_ctxtdata
*uctxt
)
1182 ret
= hfi1_user_sdma_alloc_queues(uctxt
, fd
);
1186 ret
= hfi1_user_exp_rcv_init(fd
, uctxt
);
1188 hfi1_user_sdma_free_queues(fd
, uctxt
);
1193 static int setup_base_ctxt(struct hfi1_filedata
*fd
,
1194 struct hfi1_ctxtdata
*uctxt
)
1196 struct hfi1_devdata
*dd
= uctxt
->dd
;
1199 hfi1_init_ctxt(uctxt
->sc
);
1201 /* Now allocate the RcvHdr queue and eager buffers. */
1202 ret
= hfi1_create_rcvhdrq(dd
, uctxt
);
1206 ret
= hfi1_setup_eagerbufs(uctxt
);
1210 /* If sub-contexts are enabled, do the appropriate setup */
1211 if (uctxt
->subctxt_cnt
)
1212 ret
= setup_subctxt(uctxt
);
1216 ret
= hfi1_alloc_ctxt_rcv_groups(uctxt
);
1220 ret
= init_user_ctxt(fd
, uctxt
);
1226 /* Now that the context is set up, the fd can get a reference. */
1228 hfi1_rcd_get(uctxt
);
1231 if (uctxt
->subctxt_cnt
) {
1233 * On error, set the failed bit so sub-contexts will clean up
1237 set_bit(HFI1_CTXT_BASE_FAILED
, &uctxt
->event_flags
);
1240 * Base context is done (successfully or not), notify anybody
1241 * using a sub-context that is waiting for this completion.
1243 clear_bit(HFI1_CTXT_BASE_UNINIT
, &uctxt
->event_flags
);
1244 wake_up(&uctxt
->wait
);
1250 static int get_base_info(struct hfi1_filedata
*fd
, unsigned long arg
, u32 len
)
1252 struct hfi1_base_info binfo
;
1253 struct hfi1_ctxtdata
*uctxt
= fd
->uctxt
;
1254 struct hfi1_devdata
*dd
= uctxt
->dd
;
1257 trace_hfi1_uctxtdata(uctxt
->dd
, uctxt
, fd
->subctxt
);
1259 if (sizeof(binfo
) != len
)
1262 memset(&binfo
, 0, sizeof(binfo
));
1263 binfo
.hw_version
= dd
->revision
;
1264 binfo
.sw_version
= HFI1_KERN_SWVERSION
;
1265 binfo
.bthqp
= RVT_KDETH_QP_PREFIX
;
1266 binfo
.jkey
= uctxt
->jkey
;
1268 * If more than 64 contexts are enabled the allocated credit
1269 * return will span two or three contiguous pages. Since we only
1270 * map the page containing the context's credit return address,
1271 * we need to calculate the offset in the proper page.
1273 offset
= ((u64
)uctxt
->sc
->hw_free
-
1274 (u64
)dd
->cr_base
[uctxt
->numa_id
].va
) % PAGE_SIZE
;
1275 binfo
.sc_credits_addr
= HFI1_MMAP_TOKEN(PIO_CRED
, uctxt
->ctxt
,
1276 fd
->subctxt
, offset
);
1277 binfo
.pio_bufbase
= HFI1_MMAP_TOKEN(PIO_BUFS
, uctxt
->ctxt
,
1279 uctxt
->sc
->base_addr
);
1280 binfo
.pio_bufbase_sop
= HFI1_MMAP_TOKEN(PIO_BUFS_SOP
,
1283 uctxt
->sc
->base_addr
);
1284 binfo
.rcvhdr_bufbase
= HFI1_MMAP_TOKEN(RCV_HDRQ
, uctxt
->ctxt
,
1287 binfo
.rcvegr_bufbase
= HFI1_MMAP_TOKEN(RCV_EGRBUF
, uctxt
->ctxt
,
1289 uctxt
->egrbufs
.rcvtids
[0].dma
);
1290 binfo
.sdma_comp_bufbase
= HFI1_MMAP_TOKEN(SDMA_COMP
, uctxt
->ctxt
,
1294 * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE))
1296 binfo
.user_regbase
= HFI1_MMAP_TOKEN(UREGS
, uctxt
->ctxt
,
1298 offset
= offset_in_page((uctxt_offset(uctxt
) + fd
->subctxt
) *
1299 sizeof(*dd
->events
));
1300 binfo
.events_bufbase
= HFI1_MMAP_TOKEN(EVENTS
, uctxt
->ctxt
,
1303 binfo
.status_bufbase
= HFI1_MMAP_TOKEN(STATUS
, uctxt
->ctxt
,
1306 if (HFI1_CAP_IS_USET(DMA_RTAIL
))
1307 binfo
.rcvhdrtail_base
= HFI1_MMAP_TOKEN(RTAIL
, uctxt
->ctxt
,
1309 if (uctxt
->subctxt_cnt
) {
1310 binfo
.subctxt_uregbase
= HFI1_MMAP_TOKEN(SUBCTXT_UREGS
,
1313 binfo
.subctxt_rcvhdrbuf
= HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ
,
1316 binfo
.subctxt_rcvegrbuf
= HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF
,
1321 if (copy_to_user((void __user
*)arg
, &binfo
, len
))
1328 * user_exp_rcv_setup - Set up the given tid rcv list
1329 * @fd: file data of the current driver instance
1330 * @arg: ioctl argumnent for user space information
1331 * @len: length of data structure associated with ioctl command
1333 * Wrapper to validate ioctl information before doing _rcv_setup.
1336 static int user_exp_rcv_setup(struct hfi1_filedata
*fd
, unsigned long arg
,
1341 struct hfi1_tid_info tinfo
;
1343 if (sizeof(tinfo
) != len
)
1346 if (copy_from_user(&tinfo
, (void __user
*)arg
, (sizeof(tinfo
))))
1349 ret
= hfi1_user_exp_rcv_setup(fd
, &tinfo
);
1352 * Copy the number of tidlist entries we used
1353 * and the length of the buffer we registered.
1355 addr
= arg
+ offsetof(struct hfi1_tid_info
, tidcnt
);
1356 if (copy_to_user((void __user
*)addr
, &tinfo
.tidcnt
,
1357 sizeof(tinfo
.tidcnt
)))
1360 addr
= arg
+ offsetof(struct hfi1_tid_info
, length
);
1361 if (copy_to_user((void __user
*)addr
, &tinfo
.length
,
1362 sizeof(tinfo
.length
)))
1370 * user_exp_rcv_clear - Clear the given tid rcv list
1371 * @fd: file data of the current driver instance
1372 * @arg: ioctl argumnent for user space information
1373 * @len: length of data structure associated with ioctl command
1375 * The hfi1_user_exp_rcv_clear() can be called from the error path. Because
1376 * of this, we need to use this wrapper to copy the user space information
1377 * before doing the clear.
1379 static int user_exp_rcv_clear(struct hfi1_filedata
*fd
, unsigned long arg
,
1384 struct hfi1_tid_info tinfo
;
1386 if (sizeof(tinfo
) != len
)
1389 if (copy_from_user(&tinfo
, (void __user
*)arg
, (sizeof(tinfo
))))
1392 ret
= hfi1_user_exp_rcv_clear(fd
, &tinfo
);
1394 addr
= arg
+ offsetof(struct hfi1_tid_info
, tidcnt
);
1395 if (copy_to_user((void __user
*)addr
, &tinfo
.tidcnt
,
1396 sizeof(tinfo
.tidcnt
)))
1404 * user_exp_rcv_invalid - Invalidate the given tid rcv list
1405 * @fd: file data of the current driver instance
1406 * @arg: ioctl argumnent for user space information
1407 * @len: length of data structure associated with ioctl command
1409 * Wrapper to validate ioctl information before doing _rcv_invalid.
1412 static int user_exp_rcv_invalid(struct hfi1_filedata
*fd
, unsigned long arg
,
1417 struct hfi1_tid_info tinfo
;
1419 if (sizeof(tinfo
) != len
)
1422 if (!fd
->invalid_tids
)
1425 if (copy_from_user(&tinfo
, (void __user
*)arg
, (sizeof(tinfo
))))
1428 ret
= hfi1_user_exp_rcv_invalid(fd
, &tinfo
);
1432 addr
= arg
+ offsetof(struct hfi1_tid_info
, tidcnt
);
1433 if (copy_to_user((void __user
*)addr
, &tinfo
.tidcnt
,
1434 sizeof(tinfo
.tidcnt
)))
1440 static __poll_t
poll_urgent(struct file
*fp
,
1441 struct poll_table_struct
*pt
)
1443 struct hfi1_filedata
*fd
= fp
->private_data
;
1444 struct hfi1_ctxtdata
*uctxt
= fd
->uctxt
;
1445 struct hfi1_devdata
*dd
= uctxt
->dd
;
1448 poll_wait(fp
, &uctxt
->wait
, pt
);
1450 spin_lock_irq(&dd
->uctxt_lock
);
1451 if (uctxt
->urgent
!= uctxt
->urgent_poll
) {
1452 pollflag
= EPOLLIN
| EPOLLRDNORM
;
1453 uctxt
->urgent_poll
= uctxt
->urgent
;
1456 set_bit(HFI1_CTXT_WAITING_URG
, &uctxt
->event_flags
);
1458 spin_unlock_irq(&dd
->uctxt_lock
);
1463 static __poll_t
poll_next(struct file
*fp
,
1464 struct poll_table_struct
*pt
)
1466 struct hfi1_filedata
*fd
= fp
->private_data
;
1467 struct hfi1_ctxtdata
*uctxt
= fd
->uctxt
;
1468 struct hfi1_devdata
*dd
= uctxt
->dd
;
1471 poll_wait(fp
, &uctxt
->wait
, pt
);
1473 spin_lock_irq(&dd
->uctxt_lock
);
1474 if (hdrqempty(uctxt
)) {
1475 set_bit(HFI1_CTXT_WAITING_RCV
, &uctxt
->event_flags
);
1476 hfi1_rcvctrl(dd
, HFI1_RCVCTRL_INTRAVAIL_ENB
, uctxt
);
1479 pollflag
= EPOLLIN
| EPOLLRDNORM
;
1481 spin_unlock_irq(&dd
->uctxt_lock
);
1487 * Find all user contexts in use, and set the specified bit in their
1489 * See also find_ctxt() for a similar use, that is specific to send buffers.
1491 int hfi1_set_uevent_bits(struct hfi1_pportdata
*ppd
, const int evtbit
)
1493 struct hfi1_ctxtdata
*uctxt
;
1494 struct hfi1_devdata
*dd
= ppd
->dd
;
1500 for (ctxt
= dd
->first_dyn_alloc_ctxt
; ctxt
< dd
->num_rcv_contexts
;
1502 uctxt
= hfi1_rcd_get_by_index(dd
, ctxt
);
1507 * subctxt_cnt is 0 if not shared, so do base
1508 * separately, first, then remaining subctxt, if any
1510 evs
= dd
->events
+ uctxt_offset(uctxt
);
1511 set_bit(evtbit
, evs
);
1512 for (i
= 1; i
< uctxt
->subctxt_cnt
; i
++)
1513 set_bit(evtbit
, evs
+ i
);
1514 hfi1_rcd_put(uctxt
);
1522 * manage_rcvq - manage a context's receive queue
1523 * @uctxt: the context
1524 * @subctxt: the sub-context
1525 * @start_stop: action to carry out
1527 * start_stop == 0 disables receive on the context, for use in queue
1528 * overflow conditions. start_stop==1 re-enables, to be used to
1529 * re-init the software copy of the head register
1531 static int manage_rcvq(struct hfi1_ctxtdata
*uctxt
, u16 subctxt
,
1534 struct hfi1_devdata
*dd
= uctxt
->dd
;
1535 unsigned int rcvctrl_op
;
1541 if (get_user(start_stop
, (int __user
*)arg
))
1544 /* atomically clear receive enable ctxt. */
1547 * On enable, force in-memory copy of the tail register to
1548 * 0, so that protocol code doesn't have to worry about
1549 * whether or not the chip has yet updated the in-memory
1550 * copy or not on return from the system call. The chip
1551 * always resets it's tail register back to 0 on a
1552 * transition from disabled to enabled.
1554 if (hfi1_rcvhdrtail_kvaddr(uctxt
))
1555 clear_rcvhdrtail(uctxt
);
1556 rcvctrl_op
= HFI1_RCVCTRL_CTXT_ENB
;
1558 rcvctrl_op
= HFI1_RCVCTRL_CTXT_DIS
;
1560 hfi1_rcvctrl(dd
, rcvctrl_op
, uctxt
);
1561 /* always; new head should be equal to new tail; see above */
1567 * clear the event notifier events for this context.
1568 * User process then performs actions appropriate to bit having been
1569 * set, if desired, and checks again in future.
1571 static int user_event_ack(struct hfi1_ctxtdata
*uctxt
, u16 subctxt
,
1575 struct hfi1_devdata
*dd
= uctxt
->dd
;
1577 unsigned long events
;
1582 if (get_user(events
, (unsigned long __user
*)arg
))
1585 evs
= dd
->events
+ uctxt_offset(uctxt
) + subctxt
;
1587 for (i
= 0; i
<= _HFI1_MAX_EVENT_BIT
; i
++) {
1588 if (!test_bit(i
, &events
))
1595 static int set_ctxt_pkey(struct hfi1_ctxtdata
*uctxt
, unsigned long arg
)
1598 struct hfi1_pportdata
*ppd
= uctxt
->ppd
;
1599 struct hfi1_devdata
*dd
= uctxt
->dd
;
1602 if (!HFI1_CAP_IS_USET(PKEY_CHECK
))
1605 if (get_user(pkey
, (u16 __user
*)arg
))
1608 if (pkey
== LIM_MGMT_P_KEY
|| pkey
== FULL_MGMT_P_KEY
)
1611 for (i
= 0; i
< ARRAY_SIZE(ppd
->pkeys
); i
++)
1612 if (pkey
== ppd
->pkeys
[i
])
1613 return hfi1_set_ctxt_pkey(dd
, uctxt
, pkey
);
1619 * ctxt_reset - Reset the user context
1620 * @uctxt: valid user context
1622 static int ctxt_reset(struct hfi1_ctxtdata
*uctxt
)
1624 struct send_context
*sc
;
1625 struct hfi1_devdata
*dd
;
1628 if (!uctxt
|| !uctxt
->dd
|| !uctxt
->sc
)
1632 * There is no protection here. User level has to guarantee that
1633 * no one will be writing to the send context while it is being
1634 * re-initialized. If user level breaks that guarantee, it will
1635 * break it's own context and no one else's.
1641 * Wait until the interrupt handler has marked the context as
1642 * halted or frozen. Report error if we time out.
1644 wait_event_interruptible_timeout(
1645 sc
->halt_wait
, (sc
->flags
& SCF_HALTED
),
1646 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT
));
1647 if (!(sc
->flags
& SCF_HALTED
))
1651 * If the send context was halted due to a Freeze, wait until the
1652 * device has been "unfrozen" before resetting the context.
1654 if (sc
->flags
& SCF_FROZEN
) {
1655 wait_event_interruptible_timeout(
1657 !(READ_ONCE(dd
->flags
) & HFI1_FROZEN
),
1658 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT
));
1659 if (dd
->flags
& HFI1_FROZEN
)
1662 if (dd
->flags
& HFI1_FORCED_FREEZE
)
1664 * Don't allow context reset if we are into
1670 ret
= sc_enable(sc
);
1671 hfi1_rcvctrl(dd
, HFI1_RCVCTRL_CTXT_ENB
, uctxt
);
1673 ret
= sc_restart(sc
);
1676 sc_return_credits(sc
);
1681 static void user_remove(struct hfi1_devdata
*dd
)
1684 hfi1_cdev_cleanup(&dd
->user_cdev
, &dd
->user_device
);
1687 static int user_add(struct hfi1_devdata
*dd
)
1692 snprintf(name
, sizeof(name
), "%s_%d", class_name(), dd
->unit
);
1693 ret
= hfi1_cdev_init(dd
->unit
, name
, &hfi1_file_ops
,
1694 &dd
->user_cdev
, &dd
->user_device
,
1695 true, &dd
->verbs_dev
.rdi
.ibdev
.dev
.kobj
);
1703 * Create per-unit files in /dev
1705 int hfi1_device_create(struct hfi1_devdata
*dd
)
1707 return user_add(dd
);
1711 * Remove per-unit files in /dev
1712 * void, core kernel returns no errors for this stuff
1714 void hfi1_device_remove(struct hfi1_devdata
*dd
)