2 * Copyright(c) 2015-2017 Intel Corporation.
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.
9 * 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.
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.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
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
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.
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.
47 #include <linux/poll.h>
48 #include <linux/cdev.h>
49 #include <linux/vmalloc.h>
51 #include <linux/sched/mm.h>
52 #include <linux/bitmap.h>
62 #include "user_sdma.h"
63 #include "user_exp_rcv.h"
67 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
69 #define SEND_CTXT_HALT_TIMEOUT 1000 /* msecs */
72 * File operation functions
74 static int hfi1_file_open(struct inode *inode, struct file *fp);
75 static int hfi1_file_close(struct inode *inode, struct file *fp);
76 static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from);
77 static unsigned int hfi1_poll(struct file *fp, struct poll_table_struct *pt);
78 static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma);
80 static u64 kvirt_to_phys(void *addr);
81 static int assign_ctxt(struct hfi1_filedata *fd, struct hfi1_user_info *uinfo);
82 static void init_subctxts(struct hfi1_ctxtdata *uctxt,
83 const struct hfi1_user_info *uinfo);
84 static int init_user_ctxt(struct hfi1_filedata *fd,
85 struct hfi1_ctxtdata *uctxt);
86 static void user_init(struct hfi1_ctxtdata *uctxt);
87 static int get_ctxt_info(struct hfi1_filedata *fd, void __user *ubase,
89 static int get_base_info(struct hfi1_filedata *fd, void __user *ubase,
91 static int setup_base_ctxt(struct hfi1_filedata *fd,
92 struct hfi1_ctxtdata *uctxt);
93 static int setup_subctxt(struct hfi1_ctxtdata *uctxt);
95 static int find_sub_ctxt(struct hfi1_filedata *fd,
96 const struct hfi1_user_info *uinfo);
97 static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
98 struct hfi1_user_info *uinfo,
99 struct hfi1_ctxtdata **cd);
100 static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt);
101 static unsigned int poll_urgent(struct file *fp, struct poll_table_struct *pt);
102 static unsigned int poll_next(struct file *fp, struct poll_table_struct *pt);
103 static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
104 unsigned long events);
105 static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, u16 subctxt, u16 pkey);
106 static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
108 static int vma_fault(struct vm_fault *vmf);
109 static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
112 static const struct file_operations hfi1_file_ops = {
113 .owner = THIS_MODULE,
114 .write_iter = hfi1_write_iter,
115 .open = hfi1_file_open,
116 .release = hfi1_file_close,
117 .unlocked_ioctl = hfi1_file_ioctl,
119 .mmap = hfi1_file_mmap,
120 .llseek = noop_llseek,
123 static const struct vm_operations_struct vm_ops = {
128 * Types of memories mapped into user processes' space
147 * Masks and offsets defining the mmap tokens
149 #define HFI1_MMAP_OFFSET_MASK 0xfffULL
150 #define HFI1_MMAP_OFFSET_SHIFT 0
151 #define HFI1_MMAP_SUBCTXT_MASK 0xfULL
152 #define HFI1_MMAP_SUBCTXT_SHIFT 12
153 #define HFI1_MMAP_CTXT_MASK 0xffULL
154 #define HFI1_MMAP_CTXT_SHIFT 16
155 #define HFI1_MMAP_TYPE_MASK 0xfULL
156 #define HFI1_MMAP_TYPE_SHIFT 24
157 #define HFI1_MMAP_MAGIC_MASK 0xffffffffULL
158 #define HFI1_MMAP_MAGIC_SHIFT 32
160 #define HFI1_MMAP_MAGIC 0xdabbad00
162 #define HFI1_MMAP_TOKEN_SET(field, val) \
163 (((val) & HFI1_MMAP_##field##_MASK) << HFI1_MMAP_##field##_SHIFT)
164 #define HFI1_MMAP_TOKEN_GET(field, token) \
165 (((token) >> HFI1_MMAP_##field##_SHIFT) & HFI1_MMAP_##field##_MASK)
166 #define HFI1_MMAP_TOKEN(type, ctxt, subctxt, addr) \
167 (HFI1_MMAP_TOKEN_SET(MAGIC, HFI1_MMAP_MAGIC) | \
168 HFI1_MMAP_TOKEN_SET(TYPE, type) | \
169 HFI1_MMAP_TOKEN_SET(CTXT, ctxt) | \
170 HFI1_MMAP_TOKEN_SET(SUBCTXT, subctxt) | \
171 HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr))))
173 #define dbg(fmt, ...) \
174 pr_info(fmt, ##__VA_ARGS__)
176 static inline int is_valid_mmap(u64 token)
178 return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC);
181 static int hfi1_file_open(struct inode *inode, struct file *fp)
183 struct hfi1_filedata *fd;
184 struct hfi1_devdata *dd = container_of(inode->i_cdev,
188 if (!((dd->flags & HFI1_PRESENT) && dd->kregbase1))
191 if (!atomic_inc_not_zero(&dd->user_refcount))
194 /* The real work is performed later in assign_ctxt() */
196 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
198 if (!fd || init_srcu_struct(&fd->pq_srcu))
200 spin_lock_init(&fd->pq_rcu_lock);
201 spin_lock_init(&fd->tid_lock);
202 spin_lock_init(&fd->invalid_lock);
203 fd->rec_cpu_num = -1; /* no cpu affinity by default */
204 fd->mm = current->mm;
207 kobject_get(&fd->dd->kobj);
208 fp->private_data = fd;
212 fp->private_data = NULL;
213 if (atomic_dec_and_test(&dd->user_refcount))
214 complete(&dd->user_comp);
218 static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
221 struct hfi1_filedata *fd = fp->private_data;
222 struct hfi1_ctxtdata *uctxt = fd->uctxt;
223 struct hfi1_user_info uinfo;
224 struct hfi1_tid_info tinfo;
228 unsigned long ul_uval = 0;
231 hfi1_cdbg(IOCTL, "IOCTL recv: 0x%x", cmd);
232 if (cmd != HFI1_IOCTL_ASSIGN_CTXT &&
233 cmd != HFI1_IOCTL_GET_VERS &&
238 case HFI1_IOCTL_ASSIGN_CTXT:
242 if (copy_from_user(&uinfo,
243 (struct hfi1_user_info __user *)arg,
247 ret = assign_ctxt(fd, &uinfo);
249 case HFI1_IOCTL_CTXT_INFO:
250 ret = get_ctxt_info(fd, (void __user *)(unsigned long)arg,
251 sizeof(struct hfi1_ctxt_info));
253 case HFI1_IOCTL_USER_INFO:
254 ret = get_base_info(fd, (void __user *)(unsigned long)arg,
255 sizeof(struct hfi1_base_info));
257 case HFI1_IOCTL_CREDIT_UPD:
259 sc_return_credits(uctxt->sc);
262 case HFI1_IOCTL_TID_UPDATE:
263 if (copy_from_user(&tinfo,
264 (struct hfi11_tid_info __user *)arg,
268 ret = hfi1_user_exp_rcv_setup(fd, &tinfo);
271 * Copy the number of tidlist entries we used
272 * and the length of the buffer we registered.
274 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
275 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
276 sizeof(tinfo.tidcnt)))
279 addr = arg + offsetof(struct hfi1_tid_info, length);
280 if (copy_to_user((void __user *)addr, &tinfo.length,
281 sizeof(tinfo.length)))
286 case HFI1_IOCTL_TID_FREE:
287 if (copy_from_user(&tinfo,
288 (struct hfi11_tid_info __user *)arg,
292 ret = hfi1_user_exp_rcv_clear(fd, &tinfo);
295 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
296 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
297 sizeof(tinfo.tidcnt)))
301 case HFI1_IOCTL_TID_INVAL_READ:
302 if (copy_from_user(&tinfo,
303 (struct hfi11_tid_info __user *)arg,
307 ret = hfi1_user_exp_rcv_invalid(fd, &tinfo);
310 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
311 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
312 sizeof(tinfo.tidcnt)))
316 case HFI1_IOCTL_RECV_CTRL:
317 ret = get_user(uval, (int __user *)arg);
320 ret = manage_rcvq(uctxt, fd->subctxt, uval);
323 case HFI1_IOCTL_POLL_TYPE:
324 ret = get_user(uval, (int __user *)arg);
327 uctxt->poll_type = (typeof(uctxt->poll_type))uval;
330 case HFI1_IOCTL_ACK_EVENT:
331 ret = get_user(ul_uval, (unsigned long __user *)arg);
334 ret = user_event_ack(uctxt, fd->subctxt, ul_uval);
337 case HFI1_IOCTL_SET_PKEY:
338 ret = get_user(uval16, (u16 __user *)arg);
341 if (HFI1_CAP_IS_USET(PKEY_CHECK))
342 ret = set_ctxt_pkey(uctxt, fd->subctxt, uval16);
347 case HFI1_IOCTL_CTXT_RESET: {
348 struct send_context *sc;
349 struct hfi1_devdata *dd;
351 if (!uctxt || !uctxt->dd || !uctxt->sc)
355 * There is no protection here. User level has to
356 * guarantee that no one will be writing to the send
357 * context while it is being re-initialized.
358 * If user level breaks that guarantee, it will break
359 * it's own context and no one else's.
364 * Wait until the interrupt handler has marked the
365 * context as halted or frozen. Report error if we time
368 wait_event_interruptible_timeout(
369 sc->halt_wait, (sc->flags & SCF_HALTED),
370 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
371 if (!(sc->flags & SCF_HALTED))
375 * If the send context was halted due to a Freeze,
376 * wait until the device has been "unfrozen" before
377 * resetting the context.
379 if (sc->flags & SCF_FROZEN) {
380 wait_event_interruptible_timeout(
382 !(ACCESS_ONCE(dd->flags) & HFI1_FROZEN),
383 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
384 if (dd->flags & HFI1_FROZEN)
387 if (dd->flags & HFI1_FORCED_FREEZE)
389 * Don't allow context reset if we are into
396 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB, uctxt);
398 ret = sc_restart(sc);
401 sc_return_credits(sc);
405 case HFI1_IOCTL_GET_VERS:
406 uval = HFI1_USER_SWVERSION;
407 if (put_user(uval, (int __user *)arg))
418 static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from)
420 struct hfi1_filedata *fd = kiocb->ki_filp->private_data;
421 struct hfi1_user_sdma_pkt_q *pq;
422 struct hfi1_user_sdma_comp_q *cq = fd->cq;
423 int done = 0, reqs = 0;
424 unsigned long dim = from->nr_segs;
427 idx = srcu_read_lock(&fd->pq_srcu);
428 pq = srcu_dereference(fd->pq, &fd->pq_srcu);
430 srcu_read_unlock(&fd->pq_srcu, idx);
434 if (!iter_is_iovec(from) || !dim) {
435 srcu_read_unlock(&fd->pq_srcu, idx);
439 trace_hfi1_sdma_request(fd->dd, fd->uctxt->ctxt, fd->subctxt, dim);
441 if (atomic_read(&pq->n_reqs) == pq->n_max_reqs) {
442 srcu_read_unlock(&fd->pq_srcu, idx);
448 unsigned long count = 0;
450 ret = hfi1_user_sdma_process_request(
451 fd, (struct iovec *)(from->iov + done),
462 srcu_read_unlock(&fd->pq_srcu, idx);
466 static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma)
468 struct hfi1_filedata *fd = fp->private_data;
469 struct hfi1_ctxtdata *uctxt = fd->uctxt;
470 struct hfi1_devdata *dd;
472 u64 token = vma->vm_pgoff << PAGE_SHIFT,
474 void *memvirt = NULL;
475 u8 subctxt, mapio = 0, vmf = 0, type;
480 if (!is_valid_mmap(token) || !uctxt ||
481 !(vma->vm_flags & VM_SHARED)) {
486 ctxt = HFI1_MMAP_TOKEN_GET(CTXT, token);
487 subctxt = HFI1_MMAP_TOKEN_GET(SUBCTXT, token);
488 type = HFI1_MMAP_TOKEN_GET(TYPE, token);
489 if (ctxt != uctxt->ctxt || subctxt != fd->subctxt) {
494 flags = vma->vm_flags;
499 memaddr = ((dd->physaddr + TXE_PIO_SEND) +
501 (uctxt->sc->hw_context * BIT(16))) +
502 /* 64K PIO space / ctxt */
503 (type == PIO_BUFS_SOP ?
504 (TXE_PIO_SIZE / 2) : 0); /* sop? */
506 * Map only the amount allocated to the context, not the
507 * entire available context's PIO space.
509 memlen = PAGE_ALIGN(uctxt->sc->credits * PIO_BLOCK_SIZE);
510 flags &= ~VM_MAYREAD;
511 flags |= VM_DONTCOPY | VM_DONTEXPAND;
512 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
516 if (flags & VM_WRITE) {
521 * The credit return location for this context could be on the
522 * second or third page allocated for credit returns (if number
523 * of enabled contexts > 64 and 128 respectively).
525 memvirt = dd->cr_base[uctxt->numa_id].va;
526 memaddr = virt_to_phys(memvirt) +
527 (((u64)uctxt->sc->hw_free -
528 (u64)dd->cr_base[uctxt->numa_id].va) & PAGE_MASK);
530 flags &= ~VM_MAYWRITE;
531 flags |= VM_DONTCOPY | VM_DONTEXPAND;
533 * The driver has already allocated memory for credit
534 * returns and programmed it into the chip. Has that
535 * memory been flagged as non-cached?
537 /* vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); */
541 memlen = uctxt->rcvhdrq_size;
542 memvirt = uctxt->rcvhdrq;
548 * The RcvEgr buffer need to be handled differently
549 * as multiple non-contiguous pages need to be mapped
550 * into the user process.
552 memlen = uctxt->egrbufs.size;
553 if ((vma->vm_end - vma->vm_start) != memlen) {
554 dd_dev_err(dd, "Eager buffer map size invalid (%lu != %lu)\n",
555 (vma->vm_end - vma->vm_start), memlen);
559 if (vma->vm_flags & VM_WRITE) {
563 vma->vm_flags &= ~VM_MAYWRITE;
564 addr = vma->vm_start;
565 for (i = 0 ; i < uctxt->egrbufs.numbufs; i++) {
566 memlen = uctxt->egrbufs.buffers[i].len;
567 memvirt = uctxt->egrbufs.buffers[i].addr;
568 ret = remap_pfn_range(
571 * virt_to_pfn() does the same, but
572 * it's not available on x86_64
573 * when CONFIG_MMU is enabled.
575 PFN_DOWN(__pa(memvirt)),
587 * Map only the page that contains this context's user
590 memaddr = (unsigned long)
591 (dd->physaddr + RXE_PER_CONTEXT_USER)
592 + (uctxt->ctxt * RXE_PER_CONTEXT_SIZE);
594 * TidFlow table is on the same page as the rest of the
598 flags |= VM_DONTCOPY | VM_DONTEXPAND;
599 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
604 * Use the page where this context's flags are. User level
605 * knows where it's own bitmap is within the page.
607 memaddr = (unsigned long)(dd->events +
608 ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
609 HFI1_MAX_SHARED_CTXTS)) & PAGE_MASK;
612 * v3.7 removes VM_RESERVED but the effect is kept by
615 flags |= VM_IO | VM_DONTEXPAND;
619 if (flags & VM_WRITE) {
623 memaddr = kvirt_to_phys((void *)dd->status);
625 flags |= VM_IO | VM_DONTEXPAND;
628 if (!HFI1_CAP_IS_USET(DMA_RTAIL)) {
630 * If the memory allocation failed, the context alloc
631 * also would have failed, so we would never get here
636 if ((flags & VM_WRITE) || !uctxt->rcvhdrtail_kvaddr) {
641 memvirt = (void *)uctxt->rcvhdrtail_kvaddr;
642 flags &= ~VM_MAYWRITE;
645 memaddr = (u64)uctxt->subctxt_uregbase;
647 flags |= VM_IO | VM_DONTEXPAND;
650 case SUBCTXT_RCV_HDRQ:
651 memaddr = (u64)uctxt->subctxt_rcvhdr_base;
652 memlen = uctxt->rcvhdrq_size * uctxt->subctxt_cnt;
653 flags |= VM_IO | VM_DONTEXPAND;
657 memaddr = (u64)uctxt->subctxt_rcvegrbuf;
658 memlen = uctxt->egrbufs.size * uctxt->subctxt_cnt;
659 flags |= VM_IO | VM_DONTEXPAND;
660 flags &= ~VM_MAYWRITE;
664 struct hfi1_user_sdma_comp_q *cq = fd->cq;
670 memaddr = (u64)cq->comps;
671 memlen = PAGE_ALIGN(sizeof(*cq->comps) * cq->nentries);
672 flags |= VM_IO | VM_DONTEXPAND;
681 if ((vma->vm_end - vma->vm_start) != memlen) {
682 hfi1_cdbg(PROC, "%u:%u Memory size mismatch %lu:%lu",
683 uctxt->ctxt, fd->subctxt,
684 (vma->vm_end - vma->vm_start), memlen);
689 vma->vm_flags = flags;
691 "%u:%u type:%u io/vf:%d/%d, addr:0x%llx, len:%lu(%lu), flags:0x%lx\n",
692 ctxt, subctxt, type, mapio, vmf, memaddr, memlen,
693 vma->vm_end - vma->vm_start, vma->vm_flags);
695 vma->vm_pgoff = PFN_DOWN(memaddr);
696 vma->vm_ops = &vm_ops;
699 ret = io_remap_pfn_range(vma, vma->vm_start,
703 } else if (memvirt) {
704 ret = remap_pfn_range(vma, vma->vm_start,
705 PFN_DOWN(__pa(memvirt)),
709 ret = remap_pfn_range(vma, vma->vm_start,
719 * Local (non-chip) user memory is not mapped right away but as it is
720 * accessed by the user-level code.
722 static int vma_fault(struct vm_fault *vmf)
726 page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
728 return VM_FAULT_SIGBUS;
736 static unsigned int hfi1_poll(struct file *fp, struct poll_table_struct *pt)
738 struct hfi1_ctxtdata *uctxt;
741 uctxt = ((struct hfi1_filedata *)fp->private_data)->uctxt;
744 else if (uctxt->poll_type == HFI1_POLL_TYPE_URGENT)
745 pollflag = poll_urgent(fp, pt);
746 else if (uctxt->poll_type == HFI1_POLL_TYPE_ANYRCV)
747 pollflag = poll_next(fp, pt);
754 static int hfi1_file_close(struct inode *inode, struct file *fp)
756 struct hfi1_filedata *fdata = fp->private_data;
757 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
758 struct hfi1_devdata *dd = container_of(inode->i_cdev,
761 unsigned long flags, *ev;
763 fp->private_data = NULL;
768 hfi1_cdbg(PROC, "closing ctxt %u:%u", uctxt->ctxt, fdata->subctxt);
771 /* drain user sdma queue */
772 hfi1_user_sdma_free_queues(fdata, uctxt);
774 /* release the cpu */
775 hfi1_put_proc_affinity(fdata->rec_cpu_num);
777 /* clean up rcv side */
778 hfi1_user_exp_rcv_free(fdata);
781 * fdata->uctxt is used in the above cleanup. It is not ready to be
782 * removed until here.
788 * Clear any left over, unhandled events so the next process that
789 * gets this context doesn't get confused.
791 ev = dd->events + ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
792 HFI1_MAX_SHARED_CTXTS) + fdata->subctxt;
795 spin_lock_irqsave(&dd->uctxt_lock, flags);
796 __clear_bit(fdata->subctxt, uctxt->in_use_ctxts);
797 if (!bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
798 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
801 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
804 * Disable receive context and interrupt available, reset all
805 * RcvCtxtCtrl bits to default values.
807 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
808 HFI1_RCVCTRL_TIDFLOW_DIS |
809 HFI1_RCVCTRL_INTRAVAIL_DIS |
810 HFI1_RCVCTRL_TAILUPD_DIS |
811 HFI1_RCVCTRL_ONE_PKT_EGR_DIS |
812 HFI1_RCVCTRL_NO_RHQ_DROP_DIS |
813 HFI1_RCVCTRL_NO_EGR_DROP_DIS, uctxt);
814 /* Clear the context's J_KEY */
815 hfi1_clear_ctxt_jkey(dd, uctxt);
817 * If a send context is allocated, reset context integrity
818 * checks to default and disable the send context.
821 sc_disable(uctxt->sc);
822 set_pio_integrity(uctxt->sc);
825 hfi1_free_ctxt_rcv_groups(uctxt);
826 hfi1_clear_ctxt_pkey(dd, uctxt);
828 uctxt->event_flags = 0;
830 deallocate_ctxt(uctxt);
833 kobject_put(&dd->kobj);
835 if (atomic_dec_and_test(&dd->user_refcount))
836 complete(&dd->user_comp);
838 cleanup_srcu_struct(&fdata->pq_srcu);
844 * Convert kernel *virtual* addresses to physical addresses.
845 * This is used to vmalloc'ed addresses.
847 static u64 kvirt_to_phys(void *addr)
852 page = vmalloc_to_page(addr);
854 paddr = page_to_pfn(page) << PAGE_SHIFT;
861 * @fd: valid filedata pointer
863 * Sub-context info can only be set up after the base context
864 * has been completed. This is indicated by the clearing of the
865 * HFI1_CTXT_BASE_UINIT bit.
867 * Wait for the bit to be cleared, and then complete the subcontext
871 static int complete_subctxt(struct hfi1_filedata *fd)
877 * sub-context info can only be set up after the base context
878 * has been completed.
880 ret = wait_event_interruptible(
882 !test_bit(HFI1_CTXT_BASE_UNINIT, &fd->uctxt->event_flags));
884 if (test_bit(HFI1_CTXT_BASE_FAILED, &fd->uctxt->event_flags))
887 /* Finish the sub-context init */
889 fd->rec_cpu_num = hfi1_get_proc_affinity(fd->uctxt->numa_id);
890 ret = init_user_ctxt(fd, fd->uctxt);
894 spin_lock_irqsave(&fd->dd->uctxt_lock, flags);
895 __clear_bit(fd->subctxt, fd->uctxt->in_use_ctxts);
896 spin_unlock_irqrestore(&fd->dd->uctxt_lock, flags);
897 hfi1_rcd_put(fd->uctxt);
904 static int assign_ctxt(struct hfi1_filedata *fd, struct hfi1_user_info *uinfo)
907 unsigned int swmajor, swminor;
908 struct hfi1_ctxtdata *uctxt = NULL;
910 swmajor = uinfo->userversion >> 16;
911 if (swmajor != HFI1_USER_SWMAJOR)
914 if (uinfo->subctxt_cnt > HFI1_MAX_SHARED_CTXTS)
917 swminor = uinfo->userversion & 0xffff;
920 * Acquire the mutex to protect against multiple creations of what
921 * could be a shared base context.
923 mutex_lock(&hfi1_mutex);
925 * Get a sub context if available (fd->uctxt will be set).
926 * ret < 0 error, 0 no context, 1 sub-context found
928 ret = find_sub_ctxt(fd, uinfo);
931 * Allocate a base context if context sharing is not required or a
932 * sub context wasn't found.
935 ret = allocate_ctxt(fd, fd->dd, uinfo, &uctxt);
937 mutex_unlock(&hfi1_mutex);
939 /* Depending on the context type, finish the appropriate init */
942 ret = setup_base_ctxt(fd, uctxt);
944 deallocate_ctxt(uctxt);
947 ret = complete_subctxt(fd);
958 * @fd: valid filedata pointer
959 * @uinfo: user info to compare base context with
960 * @uctxt: context to compare uinfo to.
962 * Compare the given context with the given information to see if it
963 * can be used for a sub context.
965 static int match_ctxt(struct hfi1_filedata *fd,
966 const struct hfi1_user_info *uinfo,
967 struct hfi1_ctxtdata *uctxt)
969 struct hfi1_devdata *dd = fd->dd;
973 /* Skip dynamically allocated kernel contexts */
974 if (uctxt->sc && (uctxt->sc->type == SC_KERNEL))
977 /* Skip ctxt if it doesn't match the requested one */
978 if (memcmp(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid)) ||
979 uctxt->jkey != generate_jkey(current_uid()) ||
980 uctxt->subctxt_id != uinfo->subctxt_id ||
981 uctxt->subctxt_cnt != uinfo->subctxt_cnt)
984 /* Verify the sharing process matches the base */
985 if (uctxt->userversion != uinfo->userversion)
988 /* Find an unused sub context */
989 spin_lock_irqsave(&dd->uctxt_lock, flags);
990 if (bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
991 /* context is being closed, do not use */
992 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
996 subctxt = find_first_zero_bit(uctxt->in_use_ctxts,
997 HFI1_MAX_SHARED_CTXTS);
998 if (subctxt >= uctxt->subctxt_cnt) {
999 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1003 fd->subctxt = subctxt;
1004 __set_bit(fd->subctxt, uctxt->in_use_ctxts);
1005 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1008 hfi1_rcd_get(uctxt);
1015 * @fd: valid filedata pointer
1016 * @uinfo: matching info to use to find a possible context to share.
1018 * The hfi1_mutex must be held when this function is called. It is
1019 * necessary to ensure serialized creation of shared contexts.
1022 * 0 No sub-context found
1023 * 1 Subcontext found and allocated
1024 * errno EINVAL (incorrect parameters)
1025 * EBUSY (all sub contexts in use)
1027 static int find_sub_ctxt(struct hfi1_filedata *fd,
1028 const struct hfi1_user_info *uinfo)
1030 struct hfi1_ctxtdata *uctxt;
1031 struct hfi1_devdata *dd = fd->dd;
1035 if (!uinfo->subctxt_cnt)
1038 for (i = dd->first_dyn_alloc_ctxt; i < dd->num_rcv_contexts; i++) {
1039 uctxt = hfi1_rcd_get_by_index(dd, i);
1041 ret = match_ctxt(fd, uinfo, uctxt);
1042 hfi1_rcd_put(uctxt);
1043 /* value of != 0 will return */
1052 static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
1053 struct hfi1_user_info *uinfo,
1054 struct hfi1_ctxtdata **rcd)
1056 struct hfi1_ctxtdata *uctxt;
1059 if (dd->flags & HFI1_FROZEN) {
1061 * Pick an error that is unique from all other errors
1062 * that are returned so the user process knows that
1063 * it tried to allocate while the SPC was frozen. It
1064 * it should be able to retry with success in a short
1074 * If we don't have a NUMA node requested, preference is towards
1077 fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node);
1078 if (fd->rec_cpu_num != -1)
1079 numa = cpu_to_node(fd->rec_cpu_num);
1081 numa = numa_node_id();
1082 ret = hfi1_create_ctxtdata(dd->pport, numa, &uctxt);
1084 dd_dev_err(dd, "user ctxtdata allocation failed\n");
1087 hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)",
1088 uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num,
1092 * Allocate and enable a PIO send context.
1094 uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize, dd->node);
1099 hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index,
1100 uctxt->sc->hw_context);
1101 ret = sc_enable(uctxt->sc);
1106 * Setup sub context information if the user-level has requested
1108 * This has to be done here so the rest of the sub-contexts find the
1109 * proper base context.
1111 if (uinfo->subctxt_cnt)
1112 init_subctxts(uctxt, uinfo);
1113 uctxt->userversion = uinfo->userversion;
1114 uctxt->flags = hfi1_cap_mask; /* save current flag state */
1115 init_waitqueue_head(&uctxt->wait);
1116 strlcpy(uctxt->comm, current->comm, sizeof(uctxt->comm));
1117 memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid));
1118 uctxt->jkey = generate_jkey(current_uid());
1119 hfi1_stats.sps_ctxts++;
1121 * Disable ASPM when there are open user/PSM contexts to avoid
1122 * issues with ASPM L1 exit latency
1124 if (dd->freectxts-- == dd->num_user_contexts)
1125 aspm_disable_all(dd);
1132 hfi1_free_ctxt(uctxt);
1136 static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt)
1138 mutex_lock(&hfi1_mutex);
1139 hfi1_stats.sps_ctxts--;
1140 if (++uctxt->dd->freectxts == uctxt->dd->num_user_contexts)
1141 aspm_enable_all(uctxt->dd);
1142 mutex_unlock(&hfi1_mutex);
1144 hfi1_free_ctxt(uctxt);
1147 static void init_subctxts(struct hfi1_ctxtdata *uctxt,
1148 const struct hfi1_user_info *uinfo)
1150 uctxt->subctxt_cnt = uinfo->subctxt_cnt;
1151 uctxt->subctxt_id = uinfo->subctxt_id;
1152 set_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
1155 static int setup_subctxt(struct hfi1_ctxtdata *uctxt)
1158 u16 num_subctxts = uctxt->subctxt_cnt;
1160 uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE);
1161 if (!uctxt->subctxt_uregbase)
1164 /* We can take the size of the RcvHdr Queue from the master */
1165 uctxt->subctxt_rcvhdr_base = vmalloc_user(uctxt->rcvhdrq_size *
1167 if (!uctxt->subctxt_rcvhdr_base) {
1172 uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size *
1174 if (!uctxt->subctxt_rcvegrbuf) {
1182 vfree(uctxt->subctxt_rcvhdr_base);
1183 uctxt->subctxt_rcvhdr_base = NULL;
1185 vfree(uctxt->subctxt_uregbase);
1186 uctxt->subctxt_uregbase = NULL;
1191 static void user_init(struct hfi1_ctxtdata *uctxt)
1193 unsigned int rcvctrl_ops = 0;
1195 /* initialize poll variables... */
1197 uctxt->urgent_poll = 0;
1200 * Now enable the ctxt for receive.
1201 * For chips that are set to DMA the tail register to memory
1202 * when they change (and when the update bit transitions from
1203 * 0 to 1. So for those chips, we turn it off and then back on.
1204 * This will (very briefly) affect any other open ctxts, but the
1205 * duration is very short, and therefore isn't an issue. We
1206 * explicitly set the in-memory tail copy to 0 beforehand, so we
1207 * don't have to wait to be sure the DMA update has happened
1208 * (chip resets head/tail to 0 on transition to enable).
1210 if (uctxt->rcvhdrtail_kvaddr)
1211 clear_rcvhdrtail(uctxt);
1213 /* Setup J_KEY before enabling the context */
1214 hfi1_set_ctxt_jkey(uctxt->dd, uctxt, uctxt->jkey);
1216 rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB;
1217 if (HFI1_CAP_UGET_MASK(uctxt->flags, HDRSUPP))
1218 rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB;
1220 * Ignore the bit in the flags for now until proper
1221 * support for multiple packet per rcv array entry is
1224 if (!HFI1_CAP_UGET_MASK(uctxt->flags, MULTI_PKT_EGR))
1225 rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
1226 if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_EGR_FULL))
1227 rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
1228 if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_RHQ_FULL))
1229 rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
1231 * The RcvCtxtCtrl.TailUpd bit has to be explicitly written.
1232 * We can't rely on the correct value to be set from prior
1233 * uses of the chip or ctxt. Therefore, add the rcvctrl op
1236 if (HFI1_CAP_UGET_MASK(uctxt->flags, DMA_RTAIL))
1237 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB;
1239 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS;
1240 hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt);
1243 static int get_ctxt_info(struct hfi1_filedata *fd, void __user *ubase,
1246 struct hfi1_ctxt_info cinfo;
1247 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1250 memset(&cinfo, 0, sizeof(cinfo));
1251 cinfo.runtime_flags = (((uctxt->flags >> HFI1_CAP_MISC_SHIFT) &
1252 HFI1_CAP_MISC_MASK) << HFI1_CAP_USER_SHIFT) |
1253 HFI1_CAP_UGET_MASK(uctxt->flags, MASK) |
1254 HFI1_CAP_KGET_MASK(uctxt->flags, K2U);
1255 /* adjust flag if this fd is not able to cache */
1257 cinfo.runtime_flags |= HFI1_CAP_TID_UNMAP; /* no caching */
1259 cinfo.num_active = hfi1_count_active_units();
1260 cinfo.unit = uctxt->dd->unit;
1261 cinfo.ctxt = uctxt->ctxt;
1262 cinfo.subctxt = fd->subctxt;
1263 cinfo.rcvtids = roundup(uctxt->egrbufs.alloced,
1264 uctxt->dd->rcv_entries.group_size) +
1265 uctxt->expected_count;
1266 cinfo.credits = uctxt->sc->credits;
1267 cinfo.numa_node = uctxt->numa_id;
1268 cinfo.rec_cpu = fd->rec_cpu_num;
1269 cinfo.send_ctxt = uctxt->sc->hw_context;
1271 cinfo.egrtids = uctxt->egrbufs.alloced;
1272 cinfo.rcvhdrq_cnt = uctxt->rcvhdrq_cnt;
1273 cinfo.rcvhdrq_entsize = uctxt->rcvhdrqentsize << 2;
1274 cinfo.sdma_ring_size = fd->cq->nentries;
1275 cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size;
1277 trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, cinfo);
1278 if (copy_to_user(ubase, &cinfo, sizeof(cinfo)))
1284 static int init_user_ctxt(struct hfi1_filedata *fd,
1285 struct hfi1_ctxtdata *uctxt)
1289 ret = hfi1_user_sdma_alloc_queues(uctxt, fd);
1293 ret = hfi1_user_exp_rcv_init(fd, uctxt);
1295 hfi1_user_sdma_free_queues(fd, uctxt);
1300 static int setup_base_ctxt(struct hfi1_filedata *fd,
1301 struct hfi1_ctxtdata *uctxt)
1303 struct hfi1_devdata *dd = uctxt->dd;
1306 hfi1_init_ctxt(uctxt->sc);
1308 /* Now allocate the RcvHdr queue and eager buffers. */
1309 ret = hfi1_create_rcvhdrq(dd, uctxt);
1313 ret = hfi1_setup_eagerbufs(uctxt);
1317 /* If sub-contexts are enabled, do the appropriate setup */
1318 if (uctxt->subctxt_cnt)
1319 ret = setup_subctxt(uctxt);
1323 ret = hfi1_alloc_ctxt_rcv_groups(uctxt);
1327 ret = init_user_ctxt(fd, uctxt);
1333 /* Now that the context is set up, the fd can get a reference. */
1335 hfi1_rcd_get(uctxt);
1338 if (uctxt->subctxt_cnt) {
1340 * On error, set the failed bit so sub-contexts will clean up
1344 set_bit(HFI1_CTXT_BASE_FAILED, &uctxt->event_flags);
1347 * Base context is done (successfully or not), notify anybody
1348 * using a sub-context that is waiting for this completion.
1350 clear_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
1351 wake_up(&uctxt->wait);
1357 static int get_base_info(struct hfi1_filedata *fd, void __user *ubase,
1360 struct hfi1_base_info binfo;
1361 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1362 struct hfi1_devdata *dd = uctxt->dd;
1367 trace_hfi1_uctxtdata(uctxt->dd, uctxt, fd->subctxt);
1369 memset(&binfo, 0, sizeof(binfo));
1370 binfo.hw_version = dd->revision;
1371 binfo.sw_version = HFI1_KERN_SWVERSION;
1372 binfo.bthqp = kdeth_qp;
1373 binfo.jkey = uctxt->jkey;
1375 * If more than 64 contexts are enabled the allocated credit
1376 * return will span two or three contiguous pages. Since we only
1377 * map the page containing the context's credit return address,
1378 * we need to calculate the offset in the proper page.
1380 offset = ((u64)uctxt->sc->hw_free -
1381 (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE;
1382 binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt,
1383 fd->subctxt, offset);
1384 binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt,
1386 uctxt->sc->base_addr);
1387 binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP,
1390 uctxt->sc->base_addr);
1391 binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt,
1394 binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt,
1396 uctxt->egrbufs.rcvtids[0].dma);
1397 binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt,
1401 * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE))
1403 binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt,
1405 offset = offset_in_page((((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
1406 HFI1_MAX_SHARED_CTXTS) + fd->subctxt) *
1407 sizeof(*dd->events));
1408 binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt,
1411 binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt,
1414 if (HFI1_CAP_IS_USET(DMA_RTAIL))
1415 binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt,
1417 if (uctxt->subctxt_cnt) {
1418 binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS,
1421 binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ,
1424 binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF,
1428 sz = (len < sizeof(binfo)) ? len : sizeof(binfo);
1429 if (copy_to_user(ubase, &binfo, sz))
1434 static unsigned int poll_urgent(struct file *fp,
1435 struct poll_table_struct *pt)
1437 struct hfi1_filedata *fd = fp->private_data;
1438 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1439 struct hfi1_devdata *dd = uctxt->dd;
1442 poll_wait(fp, &uctxt->wait, pt);
1444 spin_lock_irq(&dd->uctxt_lock);
1445 if (uctxt->urgent != uctxt->urgent_poll) {
1446 pollflag = POLLIN | POLLRDNORM;
1447 uctxt->urgent_poll = uctxt->urgent;
1450 set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags);
1452 spin_unlock_irq(&dd->uctxt_lock);
1457 static unsigned int poll_next(struct file *fp,
1458 struct poll_table_struct *pt)
1460 struct hfi1_filedata *fd = fp->private_data;
1461 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1462 struct hfi1_devdata *dd = uctxt->dd;
1465 poll_wait(fp, &uctxt->wait, pt);
1467 spin_lock_irq(&dd->uctxt_lock);
1468 if (hdrqempty(uctxt)) {
1469 set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags);
1470 hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt);
1473 pollflag = POLLIN | POLLRDNORM;
1475 spin_unlock_irq(&dd->uctxt_lock);
1481 * Find all user contexts in use, and set the specified bit in their
1483 * See also find_ctxt() for a similar use, that is specific to send buffers.
1485 int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit)
1487 struct hfi1_ctxtdata *uctxt;
1488 struct hfi1_devdata *dd = ppd->dd;
1494 for (ctxt = dd->first_dyn_alloc_ctxt; ctxt < dd->num_rcv_contexts;
1496 uctxt = hfi1_rcd_get_by_index(dd, ctxt);
1498 unsigned long *evs = dd->events +
1499 (uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
1500 HFI1_MAX_SHARED_CTXTS;
1503 * subctxt_cnt is 0 if not shared, so do base
1504 * separately, first, then remaining subctxt, if any
1506 set_bit(evtbit, evs);
1507 for (i = 1; i < uctxt->subctxt_cnt; i++)
1508 set_bit(evtbit, evs + i);
1509 hfi1_rcd_put(uctxt);
1517 * manage_rcvq - manage a context's receive queue
1518 * @uctxt: the context
1519 * @subctxt: the sub-context
1520 * @start_stop: action to carry out
1522 * start_stop == 0 disables receive on the context, for use in queue
1523 * overflow conditions. start_stop==1 re-enables, to be used to
1524 * re-init the software copy of the head register
1526 static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
1529 struct hfi1_devdata *dd = uctxt->dd;
1530 unsigned int rcvctrl_op;
1534 /* atomically clear receive enable ctxt. */
1537 * On enable, force in-memory copy of the tail register to
1538 * 0, so that protocol code doesn't have to worry about
1539 * whether or not the chip has yet updated the in-memory
1540 * copy or not on return from the system call. The chip
1541 * always resets it's tail register back to 0 on a
1542 * transition from disabled to enabled.
1544 if (uctxt->rcvhdrtail_kvaddr)
1545 clear_rcvhdrtail(uctxt);
1546 rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB;
1548 rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS;
1550 hfi1_rcvctrl(dd, rcvctrl_op, uctxt);
1551 /* always; new head should be equal to new tail; see above */
1557 * clear the event notifier events for this context.
1558 * User process then performs actions appropriate to bit having been
1559 * set, if desired, and checks again in future.
1561 static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
1562 unsigned long events)
1565 struct hfi1_devdata *dd = uctxt->dd;
1571 evs = dd->events + ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
1572 HFI1_MAX_SHARED_CTXTS) + subctxt;
1574 for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) {
1575 if (!test_bit(i, &events))
1582 static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, u16 subctxt, u16 pkey)
1584 int ret = -ENOENT, i, intable = 0;
1585 struct hfi1_pportdata *ppd = uctxt->ppd;
1586 struct hfi1_devdata *dd = uctxt->dd;
1588 if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY) {
1593 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++)
1594 if (pkey == ppd->pkeys[i]) {
1600 ret = hfi1_set_ctxt_pkey(dd, uctxt, pkey);
1605 static void user_remove(struct hfi1_devdata *dd)
1608 hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device);
1611 static int user_add(struct hfi1_devdata *dd)
1616 snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit);
1617 ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops,
1618 &dd->user_cdev, &dd->user_device,
1627 * Create per-unit files in /dev
1629 int hfi1_device_create(struct hfi1_devdata *dd)
1631 return user_add(dd);
1635 * Remove per-unit files in /dev
1636 * void, core kernel returns no errors for this stuff
1638 void hfi1_device_remove(struct hfi1_devdata *dd)