1 // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
3 * Copyright(c) 2020 Cornelis Networks, Inc.
4 * Copyright(c) 2015-2020 Intel Corporation.
7 #include <linux/poll.h>
8 #include <linux/cdev.h>
9 #include <linux/vmalloc.h>
11 #include <linux/sched/mm.h>
12 #include <linux/bitmap.h>
22 #include "user_sdma.h"
23 #include "user_exp_rcv.h"
27 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
29 #define SEND_CTXT_HALT_TIMEOUT 1000 /* msecs */
32 * File operation functions
34 static int hfi1_file_open(struct inode *inode, struct file *fp);
35 static int hfi1_file_close(struct inode *inode, struct file *fp);
36 static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from);
37 static __poll_t hfi1_poll(struct file *fp, struct poll_table_struct *pt);
38 static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma);
40 static u64 kvirt_to_phys(void *addr);
41 static int assign_ctxt(struct hfi1_filedata *fd, unsigned long arg, u32 len);
42 static void init_subctxts(struct hfi1_ctxtdata *uctxt,
43 const struct hfi1_user_info *uinfo);
44 static int init_user_ctxt(struct hfi1_filedata *fd,
45 struct hfi1_ctxtdata *uctxt);
46 static void user_init(struct hfi1_ctxtdata *uctxt);
47 static int get_ctxt_info(struct hfi1_filedata *fd, unsigned long arg, u32 len);
48 static int get_base_info(struct hfi1_filedata *fd, unsigned long arg, u32 len);
49 static int user_exp_rcv_setup(struct hfi1_filedata *fd, unsigned long arg,
51 static int user_exp_rcv_clear(struct hfi1_filedata *fd, unsigned long arg,
53 static int user_exp_rcv_invalid(struct hfi1_filedata *fd, unsigned long arg,
55 static int setup_base_ctxt(struct hfi1_filedata *fd,
56 struct hfi1_ctxtdata *uctxt);
57 static int setup_subctxt(struct hfi1_ctxtdata *uctxt);
59 static int find_sub_ctxt(struct hfi1_filedata *fd,
60 const struct hfi1_user_info *uinfo);
61 static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
62 struct hfi1_user_info *uinfo,
63 struct hfi1_ctxtdata **cd);
64 static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt);
65 static __poll_t poll_urgent(struct file *fp, struct poll_table_struct *pt);
66 static __poll_t poll_next(struct file *fp, struct poll_table_struct *pt);
67 static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
69 static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned long arg);
70 static int ctxt_reset(struct hfi1_ctxtdata *uctxt);
71 static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
73 static vm_fault_t vma_fault(struct vm_fault *vmf);
74 static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
77 static const struct file_operations hfi1_file_ops = {
79 .write_iter = hfi1_write_iter,
80 .open = hfi1_file_open,
81 .release = hfi1_file_close,
82 .unlocked_ioctl = hfi1_file_ioctl,
84 .mmap = hfi1_file_mmap,
85 .llseek = noop_llseek,
88 static const struct vm_operations_struct vm_ops = {
93 * Types of memories mapped into user processes' space
112 * Masks and offsets defining the mmap tokens
114 #define HFI1_MMAP_OFFSET_MASK 0xfffULL
115 #define HFI1_MMAP_OFFSET_SHIFT 0
116 #define HFI1_MMAP_SUBCTXT_MASK 0xfULL
117 #define HFI1_MMAP_SUBCTXT_SHIFT 12
118 #define HFI1_MMAP_CTXT_MASK 0xffULL
119 #define HFI1_MMAP_CTXT_SHIFT 16
120 #define HFI1_MMAP_TYPE_MASK 0xfULL
121 #define HFI1_MMAP_TYPE_SHIFT 24
122 #define HFI1_MMAP_MAGIC_MASK 0xffffffffULL
123 #define HFI1_MMAP_MAGIC_SHIFT 32
125 #define HFI1_MMAP_MAGIC 0xdabbad00
127 #define HFI1_MMAP_TOKEN_SET(field, val) \
128 (((val) & HFI1_MMAP_##field##_MASK) << HFI1_MMAP_##field##_SHIFT)
129 #define HFI1_MMAP_TOKEN_GET(field, token) \
130 (((token) >> HFI1_MMAP_##field##_SHIFT) & HFI1_MMAP_##field##_MASK)
131 #define HFI1_MMAP_TOKEN(type, ctxt, subctxt, addr) \
132 (HFI1_MMAP_TOKEN_SET(MAGIC, HFI1_MMAP_MAGIC) | \
133 HFI1_MMAP_TOKEN_SET(TYPE, type) | \
134 HFI1_MMAP_TOKEN_SET(CTXT, ctxt) | \
135 HFI1_MMAP_TOKEN_SET(SUBCTXT, subctxt) | \
136 HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr))))
138 #define dbg(fmt, ...) \
139 pr_info(fmt, ##__VA_ARGS__)
141 static inline int is_valid_mmap(u64 token)
143 return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC);
146 static int hfi1_file_open(struct inode *inode, struct file *fp)
148 struct hfi1_filedata *fd;
149 struct hfi1_devdata *dd = container_of(inode->i_cdev,
153 if (!((dd->flags & HFI1_PRESENT) && dd->kregbase1))
156 if (!refcount_inc_not_zero(&dd->user_refcount))
159 /* The real work is performed later in assign_ctxt() */
161 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
163 if (!fd || init_srcu_struct(&fd->pq_srcu))
165 spin_lock_init(&fd->pq_rcu_lock);
166 spin_lock_init(&fd->tid_lock);
167 spin_lock_init(&fd->invalid_lock);
168 fd->rec_cpu_num = -1; /* no cpu affinity by default */
170 fp->private_data = fd;
174 fp->private_data = NULL;
175 if (refcount_dec_and_test(&dd->user_refcount))
176 complete(&dd->user_comp);
180 static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
183 struct hfi1_filedata *fd = fp->private_data;
184 struct hfi1_ctxtdata *uctxt = fd->uctxt;
188 hfi1_cdbg(IOCTL, "IOCTL recv: 0x%x", cmd);
189 if (cmd != HFI1_IOCTL_ASSIGN_CTXT &&
190 cmd != HFI1_IOCTL_GET_VERS &&
195 case HFI1_IOCTL_ASSIGN_CTXT:
196 ret = assign_ctxt(fd, arg, _IOC_SIZE(cmd));
199 case HFI1_IOCTL_CTXT_INFO:
200 ret = get_ctxt_info(fd, arg, _IOC_SIZE(cmd));
203 case HFI1_IOCTL_USER_INFO:
204 ret = get_base_info(fd, arg, _IOC_SIZE(cmd));
207 case HFI1_IOCTL_CREDIT_UPD:
209 sc_return_credits(uctxt->sc);
212 case HFI1_IOCTL_TID_UPDATE:
213 ret = user_exp_rcv_setup(fd, arg, _IOC_SIZE(cmd));
216 case HFI1_IOCTL_TID_FREE:
217 ret = user_exp_rcv_clear(fd, arg, _IOC_SIZE(cmd));
220 case HFI1_IOCTL_TID_INVAL_READ:
221 ret = user_exp_rcv_invalid(fd, arg, _IOC_SIZE(cmd));
224 case HFI1_IOCTL_RECV_CTRL:
225 ret = manage_rcvq(uctxt, fd->subctxt, arg);
228 case HFI1_IOCTL_POLL_TYPE:
229 if (get_user(uval, (int __user *)arg))
231 uctxt->poll_type = (typeof(uctxt->poll_type))uval;
234 case HFI1_IOCTL_ACK_EVENT:
235 ret = user_event_ack(uctxt, fd->subctxt, arg);
238 case HFI1_IOCTL_SET_PKEY:
239 ret = set_ctxt_pkey(uctxt, arg);
242 case HFI1_IOCTL_CTXT_RESET:
243 ret = ctxt_reset(uctxt);
246 case HFI1_IOCTL_GET_VERS:
247 uval = HFI1_USER_SWVERSION;
248 if (put_user(uval, (int __user *)arg))
259 static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from)
261 struct hfi1_filedata *fd = kiocb->ki_filp->private_data;
262 struct hfi1_user_sdma_pkt_q *pq;
263 struct hfi1_user_sdma_comp_q *cq = fd->cq;
264 int done = 0, reqs = 0;
265 unsigned long dim = from->nr_segs;
268 if (!HFI1_CAP_IS_KSET(SDMA))
270 idx = srcu_read_lock(&fd->pq_srcu);
271 pq = srcu_dereference(fd->pq, &fd->pq_srcu);
273 srcu_read_unlock(&fd->pq_srcu, idx);
277 if (!iter_is_iovec(from) || !dim) {
278 srcu_read_unlock(&fd->pq_srcu, idx);
282 trace_hfi1_sdma_request(fd->dd, fd->uctxt->ctxt, fd->subctxt, dim);
284 if (atomic_read(&pq->n_reqs) == pq->n_max_reqs) {
285 srcu_read_unlock(&fd->pq_srcu, idx);
291 unsigned long count = 0;
293 ret = hfi1_user_sdma_process_request(
294 fd, (struct iovec *)(from->iov + done),
305 srcu_read_unlock(&fd->pq_srcu, idx);
309 static inline void mmap_cdbg(u16 ctxt, u8 subctxt, u8 type, u8 mapio, u8 vmf,
310 u64 memaddr, void *memvirt, dma_addr_t memdma,
311 ssize_t memlen, struct vm_area_struct *vma)
314 "%u:%u type:%u io/vf/dma:%d/%d/%d, addr:0x%llx, len:%lu(%lu), flags:0x%lx",
315 ctxt, subctxt, type, mapio, vmf, !!memdma,
316 memaddr ?: (u64)memvirt, memlen,
317 vma->vm_end - vma->vm_start, vma->vm_flags);
320 static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma)
322 struct hfi1_filedata *fd = fp->private_data;
323 struct hfi1_ctxtdata *uctxt = fd->uctxt;
324 struct hfi1_devdata *dd;
326 u64 token = vma->vm_pgoff << PAGE_SHIFT,
328 void *memvirt = NULL;
329 dma_addr_t memdma = 0;
330 u8 subctxt, mapio = 0, vmf = 0, type;
335 if (!is_valid_mmap(token) || !uctxt ||
336 !(vma->vm_flags & VM_SHARED)) {
341 ctxt = HFI1_MMAP_TOKEN_GET(CTXT, token);
342 subctxt = HFI1_MMAP_TOKEN_GET(SUBCTXT, token);
343 type = HFI1_MMAP_TOKEN_GET(TYPE, token);
344 if (ctxt != uctxt->ctxt || subctxt != fd->subctxt) {
350 * vm_pgoff is used as a buffer selector cookie. Always mmap from
354 flags = vma->vm_flags;
359 memaddr = ((dd->physaddr + TXE_PIO_SEND) +
361 (uctxt->sc->hw_context * BIT(16))) +
362 /* 64K PIO space / ctxt */
363 (type == PIO_BUFS_SOP ?
364 (TXE_PIO_SIZE / 2) : 0); /* sop? */
366 * Map only the amount allocated to the context, not the
367 * entire available context's PIO space.
369 memlen = PAGE_ALIGN(uctxt->sc->credits * PIO_BLOCK_SIZE);
370 flags &= ~VM_MAYREAD;
371 flags |= VM_DONTCOPY | VM_DONTEXPAND;
372 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
377 if (flags & VM_WRITE) {
382 * The credit return location for this context could be on the
383 * second or third page allocated for credit returns (if number
384 * of enabled contexts > 64 and 128 respectively).
386 cr_page_offset = ((u64)uctxt->sc->hw_free -
387 (u64)dd->cr_base[uctxt->numa_id].va) &
389 memvirt = dd->cr_base[uctxt->numa_id].va + cr_page_offset;
390 memdma = dd->cr_base[uctxt->numa_id].dma + cr_page_offset;
392 flags &= ~VM_MAYWRITE;
393 flags |= VM_DONTCOPY | VM_DONTEXPAND;
395 * The driver has already allocated memory for credit
396 * returns and programmed it into the chip. Has that
397 * memory been flagged as non-cached?
399 /* vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); */
403 memlen = rcvhdrq_size(uctxt);
404 memvirt = uctxt->rcvhdrq;
405 memdma = uctxt->rcvhdrq_dma;
408 unsigned long vm_start_save;
409 unsigned long vm_end_save;
412 * The RcvEgr buffer need to be handled differently
413 * as multiple non-contiguous pages need to be mapped
414 * into the user process.
416 memlen = uctxt->egrbufs.size;
417 if ((vma->vm_end - vma->vm_start) != memlen) {
418 dd_dev_err(dd, "Eager buffer map size invalid (%lu != %lu)\n",
419 (vma->vm_end - vma->vm_start), memlen);
423 if (vma->vm_flags & VM_WRITE) {
427 vm_flags_clear(vma, VM_MAYWRITE);
429 * Mmap multiple separate allocations into a single vma. From
430 * here, dma_mmap_coherent() calls dma_direct_mmap(), which
431 * requires the mmap to exactly fill the vma starting at
432 * vma_start. Adjust the vma start and end for each eager
433 * buffer segment mapped. Restore the originals when done.
435 vm_start_save = vma->vm_start;
436 vm_end_save = vma->vm_end;
437 vma->vm_end = vma->vm_start;
438 for (i = 0 ; i < uctxt->egrbufs.numbufs; i++) {
439 memlen = uctxt->egrbufs.buffers[i].len;
440 memvirt = uctxt->egrbufs.buffers[i].addr;
441 memdma = uctxt->egrbufs.buffers[i].dma;
442 vma->vm_end += memlen;
443 mmap_cdbg(ctxt, subctxt, type, mapio, vmf, memaddr,
444 memvirt, memdma, memlen, vma);
445 ret = dma_mmap_coherent(&dd->pcidev->dev, vma,
446 memvirt, memdma, memlen);
448 vma->vm_start = vm_start_save;
449 vma->vm_end = vm_end_save;
452 vma->vm_start += memlen;
454 vma->vm_start = vm_start_save;
455 vma->vm_end = vm_end_save;
461 * Map only the page that contains this context's user
464 memaddr = (unsigned long)
465 (dd->physaddr + RXE_PER_CONTEXT_USER)
466 + (uctxt->ctxt * RXE_PER_CONTEXT_SIZE);
468 * TidFlow table is on the same page as the rest of the
472 flags |= VM_DONTCOPY | VM_DONTEXPAND;
473 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
478 * Use the page where this context's flags are. User level
479 * knows where it's own bitmap is within the page.
481 memaddr = (unsigned long)
482 (dd->events + uctxt_offset(uctxt)) & PAGE_MASK;
485 * v3.7 removes VM_RESERVED but the effect is kept by
488 flags |= VM_IO | VM_DONTEXPAND;
492 if (flags & VM_WRITE) {
496 memaddr = kvirt_to_phys((void *)dd->status);
498 flags |= VM_IO | VM_DONTEXPAND;
501 if (!HFI1_CAP_IS_USET(DMA_RTAIL)) {
503 * If the memory allocation failed, the context alloc
504 * also would have failed, so we would never get here
509 if ((flags & VM_WRITE) || !hfi1_rcvhdrtail_kvaddr(uctxt)) {
514 memvirt = (void *)hfi1_rcvhdrtail_kvaddr(uctxt);
515 memdma = uctxt->rcvhdrqtailaddr_dma;
516 flags &= ~VM_MAYWRITE;
519 memaddr = (u64)uctxt->subctxt_uregbase;
521 flags |= VM_IO | VM_DONTEXPAND;
524 case SUBCTXT_RCV_HDRQ:
525 memaddr = (u64)uctxt->subctxt_rcvhdr_base;
526 memlen = rcvhdrq_size(uctxt) * uctxt->subctxt_cnt;
527 flags |= VM_IO | VM_DONTEXPAND;
531 memaddr = (u64)uctxt->subctxt_rcvegrbuf;
532 memlen = uctxt->egrbufs.size * uctxt->subctxt_cnt;
533 flags |= VM_IO | VM_DONTEXPAND;
534 flags &= ~VM_MAYWRITE;
538 struct hfi1_user_sdma_comp_q *cq = fd->cq;
544 memaddr = (u64)cq->comps;
545 memlen = PAGE_ALIGN(sizeof(*cq->comps) * cq->nentries);
546 flags |= VM_IO | VM_DONTEXPAND;
555 if ((vma->vm_end - vma->vm_start) != memlen) {
556 hfi1_cdbg(PROC, "%u:%u Memory size mismatch %lu:%lu",
557 uctxt->ctxt, fd->subctxt,
558 (vma->vm_end - vma->vm_start), memlen);
563 vm_flags_reset(vma, flags);
564 mmap_cdbg(ctxt, subctxt, type, mapio, vmf, memaddr, memvirt, memdma,
567 vma->vm_pgoff = PFN_DOWN(memaddr);
568 vma->vm_ops = &vm_ops;
571 ret = dma_mmap_coherent(&dd->pcidev->dev, vma,
572 memvirt, memdma, memlen);
574 ret = io_remap_pfn_range(vma, vma->vm_start,
578 } else if (memvirt) {
579 ret = remap_pfn_range(vma, vma->vm_start,
580 PFN_DOWN(__pa(memvirt)),
584 ret = remap_pfn_range(vma, vma->vm_start,
594 * Local (non-chip) user memory is not mapped right away but as it is
595 * accessed by the user-level code.
597 static vm_fault_t vma_fault(struct vm_fault *vmf)
601 page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
603 return VM_FAULT_SIGBUS;
611 static __poll_t hfi1_poll(struct file *fp, struct poll_table_struct *pt)
613 struct hfi1_ctxtdata *uctxt;
616 uctxt = ((struct hfi1_filedata *)fp->private_data)->uctxt;
619 else if (uctxt->poll_type == HFI1_POLL_TYPE_URGENT)
620 pollflag = poll_urgent(fp, pt);
621 else if (uctxt->poll_type == HFI1_POLL_TYPE_ANYRCV)
622 pollflag = poll_next(fp, pt);
629 static int hfi1_file_close(struct inode *inode, struct file *fp)
631 struct hfi1_filedata *fdata = fp->private_data;
632 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
633 struct hfi1_devdata *dd = container_of(inode->i_cdev,
636 unsigned long flags, *ev;
638 fp->private_data = NULL;
643 hfi1_cdbg(PROC, "closing ctxt %u:%u", uctxt->ctxt, fdata->subctxt);
646 /* drain user sdma queue */
647 hfi1_user_sdma_free_queues(fdata, uctxt);
649 /* release the cpu */
650 hfi1_put_proc_affinity(fdata->rec_cpu_num);
652 /* clean up rcv side */
653 hfi1_user_exp_rcv_free(fdata);
656 * fdata->uctxt is used in the above cleanup. It is not ready to be
657 * removed until here.
663 * Clear any left over, unhandled events so the next process that
664 * gets this context doesn't get confused.
666 ev = dd->events + uctxt_offset(uctxt) + fdata->subctxt;
669 spin_lock_irqsave(&dd->uctxt_lock, flags);
670 __clear_bit(fdata->subctxt, uctxt->in_use_ctxts);
671 if (!bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
672 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
675 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
678 * Disable receive context and interrupt available, reset all
679 * RcvCtxtCtrl bits to default values.
681 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
682 HFI1_RCVCTRL_TIDFLOW_DIS |
683 HFI1_RCVCTRL_INTRAVAIL_DIS |
684 HFI1_RCVCTRL_TAILUPD_DIS |
685 HFI1_RCVCTRL_ONE_PKT_EGR_DIS |
686 HFI1_RCVCTRL_NO_RHQ_DROP_DIS |
687 HFI1_RCVCTRL_NO_EGR_DROP_DIS |
688 HFI1_RCVCTRL_URGENT_DIS, uctxt);
689 /* Clear the context's J_KEY */
690 hfi1_clear_ctxt_jkey(dd, uctxt);
692 * If a send context is allocated, reset context integrity
693 * checks to default and disable the send context.
696 sc_disable(uctxt->sc);
697 set_pio_integrity(uctxt->sc);
700 hfi1_free_ctxt_rcv_groups(uctxt);
701 hfi1_clear_ctxt_pkey(dd, uctxt);
703 uctxt->event_flags = 0;
705 deallocate_ctxt(uctxt);
708 if (refcount_dec_and_test(&dd->user_refcount))
709 complete(&dd->user_comp);
711 cleanup_srcu_struct(&fdata->pq_srcu);
717 * Convert kernel *virtual* addresses to physical addresses.
718 * This is used to vmalloc'ed addresses.
720 static u64 kvirt_to_phys(void *addr)
725 page = vmalloc_to_page(addr);
727 paddr = page_to_pfn(page) << PAGE_SHIFT;
733 * complete_subctxt - complete sub-context info
734 * @fd: valid filedata pointer
736 * Sub-context info can only be set up after the base context
737 * has been completed. This is indicated by the clearing of the
738 * HFI1_CTXT_BASE_UINIT bit.
740 * Wait for the bit to be cleared, and then complete the subcontext
744 static int complete_subctxt(struct hfi1_filedata *fd)
750 * sub-context info can only be set up after the base context
751 * has been completed.
753 ret = wait_event_interruptible(
755 !test_bit(HFI1_CTXT_BASE_UNINIT, &fd->uctxt->event_flags));
757 if (test_bit(HFI1_CTXT_BASE_FAILED, &fd->uctxt->event_flags))
760 /* Finish the sub-context init */
762 fd->rec_cpu_num = hfi1_get_proc_affinity(fd->uctxt->numa_id);
763 ret = init_user_ctxt(fd, fd->uctxt);
767 spin_lock_irqsave(&fd->dd->uctxt_lock, flags);
768 __clear_bit(fd->subctxt, fd->uctxt->in_use_ctxts);
769 spin_unlock_irqrestore(&fd->dd->uctxt_lock, flags);
770 hfi1_rcd_put(fd->uctxt);
777 static int assign_ctxt(struct hfi1_filedata *fd, unsigned long arg, u32 len)
780 unsigned int swmajor;
781 struct hfi1_ctxtdata *uctxt = NULL;
782 struct hfi1_user_info uinfo;
787 if (sizeof(uinfo) != len)
790 if (copy_from_user(&uinfo, (void __user *)arg, sizeof(uinfo)))
793 swmajor = uinfo.userversion >> 16;
794 if (swmajor != HFI1_USER_SWMAJOR)
797 if (uinfo.subctxt_cnt > HFI1_MAX_SHARED_CTXTS)
801 * Acquire the mutex to protect against multiple creations of what
802 * could be a shared base context.
804 mutex_lock(&hfi1_mutex);
806 * Get a sub context if available (fd->uctxt will be set).
807 * ret < 0 error, 0 no context, 1 sub-context found
809 ret = find_sub_ctxt(fd, &uinfo);
812 * Allocate a base context if context sharing is not required or a
813 * sub context wasn't found.
816 ret = allocate_ctxt(fd, fd->dd, &uinfo, &uctxt);
818 mutex_unlock(&hfi1_mutex);
820 /* Depending on the context type, finish the appropriate init */
823 ret = setup_base_ctxt(fd, uctxt);
825 deallocate_ctxt(uctxt);
828 ret = complete_subctxt(fd);
838 * match_ctxt - match context
839 * @fd: valid filedata pointer
840 * @uinfo: user info to compare base context with
841 * @uctxt: context to compare uinfo to.
843 * Compare the given context with the given information to see if it
844 * can be used for a sub context.
846 static int match_ctxt(struct hfi1_filedata *fd,
847 const struct hfi1_user_info *uinfo,
848 struct hfi1_ctxtdata *uctxt)
850 struct hfi1_devdata *dd = fd->dd;
854 /* Skip dynamically allocated kernel contexts */
855 if (uctxt->sc && (uctxt->sc->type == SC_KERNEL))
858 /* Skip ctxt if it doesn't match the requested one */
859 if (memcmp(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid)) ||
860 uctxt->jkey != generate_jkey(current_uid()) ||
861 uctxt->subctxt_id != uinfo->subctxt_id ||
862 uctxt->subctxt_cnt != uinfo->subctxt_cnt)
865 /* Verify the sharing process matches the base */
866 if (uctxt->userversion != uinfo->userversion)
869 /* Find an unused sub context */
870 spin_lock_irqsave(&dd->uctxt_lock, flags);
871 if (bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
872 /* context is being closed, do not use */
873 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
877 subctxt = find_first_zero_bit(uctxt->in_use_ctxts,
878 HFI1_MAX_SHARED_CTXTS);
879 if (subctxt >= uctxt->subctxt_cnt) {
880 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
884 fd->subctxt = subctxt;
885 __set_bit(fd->subctxt, uctxt->in_use_ctxts);
886 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
895 * find_sub_ctxt - fund sub-context
896 * @fd: valid filedata pointer
897 * @uinfo: matching info to use to find a possible context to share.
899 * The hfi1_mutex must be held when this function is called. It is
900 * necessary to ensure serialized creation of shared contexts.
903 * 0 No sub-context found
904 * 1 Subcontext found and allocated
905 * errno EINVAL (incorrect parameters)
906 * EBUSY (all sub contexts in use)
908 static int find_sub_ctxt(struct hfi1_filedata *fd,
909 const struct hfi1_user_info *uinfo)
911 struct hfi1_ctxtdata *uctxt;
912 struct hfi1_devdata *dd = fd->dd;
916 if (!uinfo->subctxt_cnt)
919 for (i = dd->first_dyn_alloc_ctxt; i < dd->num_rcv_contexts; i++) {
920 uctxt = hfi1_rcd_get_by_index(dd, i);
922 ret = match_ctxt(fd, uinfo, uctxt);
924 /* value of != 0 will return */
933 static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
934 struct hfi1_user_info *uinfo,
935 struct hfi1_ctxtdata **rcd)
937 struct hfi1_ctxtdata *uctxt;
940 if (dd->flags & HFI1_FROZEN) {
942 * Pick an error that is unique from all other errors
943 * that are returned so the user process knows that
944 * it tried to allocate while the SPC was frozen. It
945 * it should be able to retry with success in a short
955 * If we don't have a NUMA node requested, preference is towards
958 fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node);
959 if (fd->rec_cpu_num != -1)
960 numa = cpu_to_node(fd->rec_cpu_num);
962 numa = numa_node_id();
963 ret = hfi1_create_ctxtdata(dd->pport, numa, &uctxt);
965 dd_dev_err(dd, "user ctxtdata allocation failed\n");
968 hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)",
969 uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num,
973 * Allocate and enable a PIO send context.
975 uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize, dd->node);
980 hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index,
981 uctxt->sc->hw_context);
982 ret = sc_enable(uctxt->sc);
987 * Setup sub context information if the user-level has requested
989 * This has to be done here so the rest of the sub-contexts find the
990 * proper base context.
991 * NOTE: _set_bit() can be used here because the context creation is
992 * protected by the mutex (rather than the spin_lock), and will be the
993 * very first instance of this context.
995 __set_bit(0, uctxt->in_use_ctxts);
996 if (uinfo->subctxt_cnt)
997 init_subctxts(uctxt, uinfo);
998 uctxt->userversion = uinfo->userversion;
999 uctxt->flags = hfi1_cap_mask; /* save current flag state */
1000 init_waitqueue_head(&uctxt->wait);
1001 strscpy(uctxt->comm, current->comm, sizeof(uctxt->comm));
1002 memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid));
1003 uctxt->jkey = generate_jkey(current_uid());
1004 hfi1_stats.sps_ctxts++;
1006 * Disable ASPM when there are open user/PSM contexts to avoid
1007 * issues with ASPM L1 exit latency
1009 if (dd->freectxts-- == dd->num_user_contexts)
1010 aspm_disable_all(dd);
1017 hfi1_free_ctxt(uctxt);
1021 static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt)
1023 mutex_lock(&hfi1_mutex);
1024 hfi1_stats.sps_ctxts--;
1025 if (++uctxt->dd->freectxts == uctxt->dd->num_user_contexts)
1026 aspm_enable_all(uctxt->dd);
1027 mutex_unlock(&hfi1_mutex);
1029 hfi1_free_ctxt(uctxt);
1032 static void init_subctxts(struct hfi1_ctxtdata *uctxt,
1033 const struct hfi1_user_info *uinfo)
1035 uctxt->subctxt_cnt = uinfo->subctxt_cnt;
1036 uctxt->subctxt_id = uinfo->subctxt_id;
1037 set_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
1040 static int setup_subctxt(struct hfi1_ctxtdata *uctxt)
1043 u16 num_subctxts = uctxt->subctxt_cnt;
1045 uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE);
1046 if (!uctxt->subctxt_uregbase)
1049 /* We can take the size of the RcvHdr Queue from the master */
1050 uctxt->subctxt_rcvhdr_base = vmalloc_user(rcvhdrq_size(uctxt) *
1052 if (!uctxt->subctxt_rcvhdr_base) {
1057 uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size *
1059 if (!uctxt->subctxt_rcvegrbuf) {
1067 vfree(uctxt->subctxt_rcvhdr_base);
1068 uctxt->subctxt_rcvhdr_base = NULL;
1070 vfree(uctxt->subctxt_uregbase);
1071 uctxt->subctxt_uregbase = NULL;
1076 static void user_init(struct hfi1_ctxtdata *uctxt)
1078 unsigned int rcvctrl_ops = 0;
1080 /* initialize poll variables... */
1082 uctxt->urgent_poll = 0;
1085 * Now enable the ctxt for receive.
1086 * For chips that are set to DMA the tail register to memory
1087 * when they change (and when the update bit transitions from
1088 * 0 to 1. So for those chips, we turn it off and then back on.
1089 * This will (very briefly) affect any other open ctxts, but the
1090 * duration is very short, and therefore isn't an issue. We
1091 * explicitly set the in-memory tail copy to 0 beforehand, so we
1092 * don't have to wait to be sure the DMA update has happened
1093 * (chip resets head/tail to 0 on transition to enable).
1095 if (hfi1_rcvhdrtail_kvaddr(uctxt))
1096 clear_rcvhdrtail(uctxt);
1098 /* Setup J_KEY before enabling the context */
1099 hfi1_set_ctxt_jkey(uctxt->dd, uctxt, uctxt->jkey);
1101 rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB;
1102 rcvctrl_ops |= HFI1_RCVCTRL_URGENT_ENB;
1103 if (HFI1_CAP_UGET_MASK(uctxt->flags, HDRSUPP))
1104 rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB;
1106 * Ignore the bit in the flags for now until proper
1107 * support for multiple packet per rcv array entry is
1110 if (!HFI1_CAP_UGET_MASK(uctxt->flags, MULTI_PKT_EGR))
1111 rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
1112 if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_EGR_FULL))
1113 rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
1114 if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_RHQ_FULL))
1115 rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
1117 * The RcvCtxtCtrl.TailUpd bit has to be explicitly written.
1118 * We can't rely on the correct value to be set from prior
1119 * uses of the chip or ctxt. Therefore, add the rcvctrl op
1122 if (HFI1_CAP_UGET_MASK(uctxt->flags, DMA_RTAIL))
1123 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB;
1125 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS;
1126 hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt);
1129 static int get_ctxt_info(struct hfi1_filedata *fd, unsigned long arg, u32 len)
1131 struct hfi1_ctxt_info cinfo;
1132 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1134 if (sizeof(cinfo) != len)
1137 memset(&cinfo, 0, sizeof(cinfo));
1138 cinfo.runtime_flags = (((uctxt->flags >> HFI1_CAP_MISC_SHIFT) &
1139 HFI1_CAP_MISC_MASK) << HFI1_CAP_USER_SHIFT) |
1140 HFI1_CAP_UGET_MASK(uctxt->flags, MASK) |
1141 HFI1_CAP_KGET_MASK(uctxt->flags, K2U);
1142 /* adjust flag if this fd is not able to cache */
1144 cinfo.runtime_flags |= HFI1_CAP_TID_UNMAP; /* no caching */
1146 cinfo.num_active = hfi1_count_active_units();
1147 cinfo.unit = uctxt->dd->unit;
1148 cinfo.ctxt = uctxt->ctxt;
1149 cinfo.subctxt = fd->subctxt;
1150 cinfo.rcvtids = roundup(uctxt->egrbufs.alloced,
1151 uctxt->dd->rcv_entries.group_size) +
1152 uctxt->expected_count;
1153 cinfo.credits = uctxt->sc->credits;
1154 cinfo.numa_node = uctxt->numa_id;
1155 cinfo.rec_cpu = fd->rec_cpu_num;
1156 cinfo.send_ctxt = uctxt->sc->hw_context;
1158 cinfo.egrtids = uctxt->egrbufs.alloced;
1159 cinfo.rcvhdrq_cnt = get_hdrq_cnt(uctxt);
1160 cinfo.rcvhdrq_entsize = get_hdrqentsize(uctxt) << 2;
1161 cinfo.sdma_ring_size = fd->cq->nentries;
1162 cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size;
1164 trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, &cinfo);
1165 if (copy_to_user((void __user *)arg, &cinfo, len))
1171 static int init_user_ctxt(struct hfi1_filedata *fd,
1172 struct hfi1_ctxtdata *uctxt)
1176 ret = hfi1_user_sdma_alloc_queues(uctxt, fd);
1180 ret = hfi1_user_exp_rcv_init(fd, uctxt);
1182 hfi1_user_sdma_free_queues(fd, uctxt);
1187 static int setup_base_ctxt(struct hfi1_filedata *fd,
1188 struct hfi1_ctxtdata *uctxt)
1190 struct hfi1_devdata *dd = uctxt->dd;
1193 hfi1_init_ctxt(uctxt->sc);
1195 /* Now allocate the RcvHdr queue and eager buffers. */
1196 ret = hfi1_create_rcvhdrq(dd, uctxt);
1200 ret = hfi1_setup_eagerbufs(uctxt);
1204 /* If sub-contexts are enabled, do the appropriate setup */
1205 if (uctxt->subctxt_cnt)
1206 ret = setup_subctxt(uctxt);
1210 ret = hfi1_alloc_ctxt_rcv_groups(uctxt);
1214 ret = init_user_ctxt(fd, uctxt);
1216 hfi1_free_ctxt_rcv_groups(uctxt);
1222 /* Now that the context is set up, the fd can get a reference. */
1224 hfi1_rcd_get(uctxt);
1227 if (uctxt->subctxt_cnt) {
1229 * On error, set the failed bit so sub-contexts will clean up
1233 set_bit(HFI1_CTXT_BASE_FAILED, &uctxt->event_flags);
1236 * Base context is done (successfully or not), notify anybody
1237 * using a sub-context that is waiting for this completion.
1239 clear_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
1240 wake_up(&uctxt->wait);
1246 static int get_base_info(struct hfi1_filedata *fd, unsigned long arg, u32 len)
1248 struct hfi1_base_info binfo;
1249 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1250 struct hfi1_devdata *dd = uctxt->dd;
1253 trace_hfi1_uctxtdata(uctxt->dd, uctxt, fd->subctxt);
1255 if (sizeof(binfo) != len)
1258 memset(&binfo, 0, sizeof(binfo));
1259 binfo.hw_version = dd->revision;
1260 binfo.sw_version = HFI1_USER_SWVERSION;
1261 binfo.bthqp = RVT_KDETH_QP_PREFIX;
1262 binfo.jkey = uctxt->jkey;
1264 * If more than 64 contexts are enabled the allocated credit
1265 * return will span two or three contiguous pages. Since we only
1266 * map the page containing the context's credit return address,
1267 * we need to calculate the offset in the proper page.
1269 offset = ((u64)uctxt->sc->hw_free -
1270 (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE;
1271 binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt,
1272 fd->subctxt, offset);
1273 binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt,
1275 uctxt->sc->base_addr);
1276 binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP,
1279 uctxt->sc->base_addr);
1280 binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt,
1283 binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt,
1285 uctxt->egrbufs.rcvtids[0].dma);
1286 binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt,
1290 * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE))
1292 binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt,
1294 offset = offset_in_page((uctxt_offset(uctxt) + fd->subctxt) *
1295 sizeof(*dd->events));
1296 binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt,
1299 binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt,
1302 if (HFI1_CAP_IS_USET(DMA_RTAIL))
1303 binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt,
1305 if (uctxt->subctxt_cnt) {
1306 binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS,
1309 binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ,
1312 binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF,
1317 if (copy_to_user((void __user *)arg, &binfo, len))
1324 * user_exp_rcv_setup - Set up the given tid rcv list
1325 * @fd: file data of the current driver instance
1326 * @arg: ioctl argumnent for user space information
1327 * @len: length of data structure associated with ioctl command
1329 * Wrapper to validate ioctl information before doing _rcv_setup.
1332 static int user_exp_rcv_setup(struct hfi1_filedata *fd, unsigned long arg,
1337 struct hfi1_tid_info tinfo;
1339 if (sizeof(tinfo) != len)
1342 if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo))))
1345 ret = hfi1_user_exp_rcv_setup(fd, &tinfo);
1348 * Copy the number of tidlist entries we used
1349 * and the length of the buffer we registered.
1351 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
1352 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
1353 sizeof(tinfo.tidcnt)))
1356 addr = arg + offsetof(struct hfi1_tid_info, length);
1357 if (!ret && copy_to_user((void __user *)addr, &tinfo.length,
1358 sizeof(tinfo.length)))
1362 hfi1_user_exp_rcv_invalid(fd, &tinfo);
1369 * user_exp_rcv_clear - Clear the given tid rcv list
1370 * @fd: file data of the current driver instance
1371 * @arg: ioctl argumnent for user space information
1372 * @len: length of data structure associated with ioctl command
1374 * The hfi1_user_exp_rcv_clear() can be called from the error path. Because
1375 * of this, we need to use this wrapper to copy the user space information
1376 * before doing the clear.
1378 static int user_exp_rcv_clear(struct hfi1_filedata *fd, unsigned long arg,
1383 struct hfi1_tid_info tinfo;
1385 if (sizeof(tinfo) != len)
1388 if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo))))
1391 ret = hfi1_user_exp_rcv_clear(fd, &tinfo);
1393 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
1394 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
1395 sizeof(tinfo.tidcnt)))
1403 * user_exp_rcv_invalid - Invalidate the given tid rcv list
1404 * @fd: file data of the current driver instance
1405 * @arg: ioctl argumnent for user space information
1406 * @len: length of data structure associated with ioctl command
1408 * Wrapper to validate ioctl information before doing _rcv_invalid.
1411 static int user_exp_rcv_invalid(struct hfi1_filedata *fd, unsigned long arg,
1416 struct hfi1_tid_info tinfo;
1418 if (sizeof(tinfo) != len)
1421 if (!fd->invalid_tids)
1424 if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo))))
1427 ret = hfi1_user_exp_rcv_invalid(fd, &tinfo);
1431 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
1432 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
1433 sizeof(tinfo.tidcnt)))
1439 static __poll_t poll_urgent(struct file *fp,
1440 struct poll_table_struct *pt)
1442 struct hfi1_filedata *fd = fp->private_data;
1443 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1444 struct hfi1_devdata *dd = uctxt->dd;
1447 poll_wait(fp, &uctxt->wait, pt);
1449 spin_lock_irq(&dd->uctxt_lock);
1450 if (uctxt->urgent != uctxt->urgent_poll) {
1451 pollflag = EPOLLIN | EPOLLRDNORM;
1452 uctxt->urgent_poll = uctxt->urgent;
1455 set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags);
1457 spin_unlock_irq(&dd->uctxt_lock);
1462 static __poll_t poll_next(struct file *fp,
1463 struct poll_table_struct *pt)
1465 struct hfi1_filedata *fd = fp->private_data;
1466 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1467 struct hfi1_devdata *dd = uctxt->dd;
1470 poll_wait(fp, &uctxt->wait, pt);
1472 spin_lock_irq(&dd->uctxt_lock);
1473 if (hdrqempty(uctxt)) {
1474 set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags);
1475 hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt);
1478 pollflag = EPOLLIN | EPOLLRDNORM;
1480 spin_unlock_irq(&dd->uctxt_lock);
1486 * Find all user contexts in use, and set the specified bit in their
1488 * See also find_ctxt() for a similar use, that is specific to send buffers.
1490 int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit)
1492 struct hfi1_ctxtdata *uctxt;
1493 struct hfi1_devdata *dd = ppd->dd;
1499 for (ctxt = dd->first_dyn_alloc_ctxt; ctxt < dd->num_rcv_contexts;
1501 uctxt = hfi1_rcd_get_by_index(dd, ctxt);
1506 * subctxt_cnt is 0 if not shared, so do base
1507 * separately, first, then remaining subctxt, if any
1509 evs = dd->events + uctxt_offset(uctxt);
1510 set_bit(evtbit, evs);
1511 for (i = 1; i < uctxt->subctxt_cnt; i++)
1512 set_bit(evtbit, evs + i);
1513 hfi1_rcd_put(uctxt);
1521 * manage_rcvq - manage a context's receive queue
1522 * @uctxt: the context
1523 * @subctxt: the sub-context
1524 * @arg: start/stop action to carry out
1526 * start_stop == 0 disables receive on the context, for use in queue
1527 * overflow conditions. start_stop==1 re-enables, to be used to
1528 * re-init the software copy of the head register
1530 static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
1533 struct hfi1_devdata *dd = uctxt->dd;
1534 unsigned int rcvctrl_op;
1540 if (get_user(start_stop, (int __user *)arg))
1543 /* atomically clear receive enable ctxt. */
1546 * On enable, force in-memory copy of the tail register to
1547 * 0, so that protocol code doesn't have to worry about
1548 * whether or not the chip has yet updated the in-memory
1549 * copy or not on return from the system call. The chip
1550 * always resets it's tail register back to 0 on a
1551 * transition from disabled to enabled.
1553 if (hfi1_rcvhdrtail_kvaddr(uctxt))
1554 clear_rcvhdrtail(uctxt);
1555 rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB;
1557 rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS;
1559 hfi1_rcvctrl(dd, rcvctrl_op, uctxt);
1560 /* always; new head should be equal to new tail; see above */
1566 * clear the event notifier events for this context.
1567 * User process then performs actions appropriate to bit having been
1568 * set, if desired, and checks again in future.
1570 static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
1574 struct hfi1_devdata *dd = uctxt->dd;
1576 unsigned long events;
1581 if (get_user(events, (unsigned long __user *)arg))
1584 evs = dd->events + uctxt_offset(uctxt) + subctxt;
1586 for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) {
1587 if (!test_bit(i, &events))
1594 static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned long arg)
1597 struct hfi1_pportdata *ppd = uctxt->ppd;
1598 struct hfi1_devdata *dd = uctxt->dd;
1601 if (!HFI1_CAP_IS_USET(PKEY_CHECK))
1604 if (get_user(pkey, (u16 __user *)arg))
1607 if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY)
1610 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++)
1611 if (pkey == ppd->pkeys[i])
1612 return hfi1_set_ctxt_pkey(dd, uctxt, pkey);
1618 * ctxt_reset - Reset the user context
1619 * @uctxt: valid user context
1621 static int ctxt_reset(struct hfi1_ctxtdata *uctxt)
1623 struct send_context *sc;
1624 struct hfi1_devdata *dd;
1627 if (!uctxt || !uctxt->dd || !uctxt->sc)
1631 * There is no protection here. User level has to guarantee that
1632 * no one will be writing to the send context while it is being
1633 * re-initialized. If user level breaks that guarantee, it will
1634 * break it's own context and no one else's.
1640 * Wait until the interrupt handler has marked the context as
1641 * halted or frozen. Report error if we time out.
1643 wait_event_interruptible_timeout(
1644 sc->halt_wait, (sc->flags & SCF_HALTED),
1645 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
1646 if (!(sc->flags & SCF_HALTED))
1650 * If the send context was halted due to a Freeze, wait until the
1651 * device has been "unfrozen" before resetting the context.
1653 if (sc->flags & SCF_FROZEN) {
1654 wait_event_interruptible_timeout(
1656 !(READ_ONCE(dd->flags) & HFI1_FROZEN),
1657 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
1658 if (dd->flags & HFI1_FROZEN)
1661 if (dd->flags & HFI1_FORCED_FREEZE)
1663 * Don't allow context reset if we are into
1669 ret = sc_enable(sc);
1670 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB, uctxt);
1672 ret = sc_restart(sc);
1675 sc_return_credits(sc);
1680 static void user_remove(struct hfi1_devdata *dd)
1683 hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device);
1686 static int user_add(struct hfi1_devdata *dd)
1691 snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit);
1692 ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops,
1693 &dd->user_cdev, &dd->user_device,
1694 true, &dd->verbs_dev.rdi.ibdev.dev.kobj);
1702 * Create per-unit files in /dev
1704 int hfi1_device_create(struct hfi1_devdata *dd)
1706 return user_add(dd);
1710 * Remove per-unit files in /dev
1711 * void, core kernel returns no errors for this stuff
1713 void hfi1_device_remove(struct hfi1_devdata *dd)