3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2015 Intel Corporation.
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 * Copyright(c) 2015 Intel Corporation.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
27 * - Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * - Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in
31 * the documentation and/or other materials provided with the
33 * - Neither the name of Intel Corporation nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 #include <linux/pci.h>
51 #include <linux/poll.h>
52 #include <linux/cdev.h>
53 #include <linux/swap.h>
54 #include <linux/vmalloc.h>
55 #include <linux/highmem.h>
57 #include <linux/jiffies.h>
58 #include <asm/pgtable.h>
59 #include <linux/delay.h>
60 #include <linux/export.h>
61 #include <linux/module.h>
62 #include <linux/cred.h>
63 #include <linux/uio.h>
72 #include "user_sdma.h"
76 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
78 #define SEND_CTXT_HALT_TIMEOUT 1000 /* msecs */
81 * File operation functions
83 static int hfi1_file_open(struct inode *, struct file *);
84 static int hfi1_file_close(struct inode *, struct file *);
85 static ssize_t hfi1_file_write(struct file *, const char __user *,
87 static ssize_t hfi1_write_iter(struct kiocb *, struct iov_iter *);
88 static unsigned int hfi1_poll(struct file *, struct poll_table_struct *);
89 static int hfi1_file_mmap(struct file *, struct vm_area_struct *);
91 static u64 kvirt_to_phys(void *);
92 static int assign_ctxt(struct file *, struct hfi1_user_info *);
93 static int init_subctxts(struct hfi1_ctxtdata *, const struct hfi1_user_info *);
94 static int user_init(struct file *);
95 static int get_ctxt_info(struct file *, void __user *, __u32);
96 static int get_base_info(struct file *, void __user *, __u32);
97 static int setup_ctxt(struct file *);
98 static int setup_subctxt(struct hfi1_ctxtdata *);
99 static int get_user_context(struct file *, struct hfi1_user_info *,
101 static int find_shared_ctxt(struct file *, const struct hfi1_user_info *);
102 static int allocate_ctxt(struct file *, struct hfi1_devdata *,
103 struct hfi1_user_info *);
104 static unsigned int poll_urgent(struct file *, struct poll_table_struct *);
105 static unsigned int poll_next(struct file *, struct poll_table_struct *);
106 static int user_event_ack(struct hfi1_ctxtdata *, int, unsigned long);
107 static int set_ctxt_pkey(struct hfi1_ctxtdata *, unsigned, u16);
108 static int manage_rcvq(struct hfi1_ctxtdata *, unsigned, int);
109 static int vma_fault(struct vm_area_struct *, struct vm_fault *);
110 static int exp_tid_setup(struct file *, struct hfi1_tid_info *);
111 static int exp_tid_free(struct file *, struct hfi1_tid_info *);
112 static void unlock_exp_tids(struct hfi1_ctxtdata *);
114 static const struct file_operations hfi1_file_ops = {
115 .owner = THIS_MODULE,
116 .write = hfi1_file_write,
117 .write_iter = hfi1_write_iter,
118 .open = hfi1_file_open,
119 .release = hfi1_file_close,
121 .mmap = hfi1_file_mmap,
122 .llseek = noop_llseek,
125 static struct vm_operations_struct vm_ops = {
130 * Types of memories mapped into user processes' space
149 * Masks and offsets defining the mmap tokens
151 #define HFI1_MMAP_OFFSET_MASK 0xfffULL
152 #define HFI1_MMAP_OFFSET_SHIFT 0
153 #define HFI1_MMAP_SUBCTXT_MASK 0xfULL
154 #define HFI1_MMAP_SUBCTXT_SHIFT 12
155 #define HFI1_MMAP_CTXT_MASK 0xffULL
156 #define HFI1_MMAP_CTXT_SHIFT 16
157 #define HFI1_MMAP_TYPE_MASK 0xfULL
158 #define HFI1_MMAP_TYPE_SHIFT 24
159 #define HFI1_MMAP_MAGIC_MASK 0xffffffffULL
160 #define HFI1_MMAP_MAGIC_SHIFT 32
162 #define HFI1_MMAP_MAGIC 0xdabbad00
164 #define HFI1_MMAP_TOKEN_SET(field, val) \
165 (((val) & HFI1_MMAP_##field##_MASK) << HFI1_MMAP_##field##_SHIFT)
166 #define HFI1_MMAP_TOKEN_GET(field, token) \
167 (((token) >> HFI1_MMAP_##field##_SHIFT) & HFI1_MMAP_##field##_MASK)
168 #define HFI1_MMAP_TOKEN(type, ctxt, subctxt, addr) \
169 (HFI1_MMAP_TOKEN_SET(MAGIC, HFI1_MMAP_MAGIC) | \
170 HFI1_MMAP_TOKEN_SET(TYPE, type) | \
171 HFI1_MMAP_TOKEN_SET(CTXT, ctxt) | \
172 HFI1_MMAP_TOKEN_SET(SUBCTXT, subctxt) | \
173 HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr))))
175 #define EXP_TID_SET(field, value) \
176 (((value) & EXP_TID_TID##field##_MASK) << \
177 EXP_TID_TID##field##_SHIFT)
178 #define EXP_TID_CLEAR(tid, field) { \
179 (tid) &= ~(EXP_TID_TID##field##_MASK << \
180 EXP_TID_TID##field##_SHIFT); \
182 #define EXP_TID_RESET(tid, field, value) do { \
183 EXP_TID_CLEAR(tid, field); \
184 (tid) |= EXP_TID_SET(field, value); \
187 #define dbg(fmt, ...) \
188 pr_info(fmt, ##__VA_ARGS__)
191 static inline int is_valid_mmap(u64 token)
193 return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC);
196 static int hfi1_file_open(struct inode *inode, struct file *fp)
198 /* The real work is performed later in assign_ctxt() */
199 fp->private_data = kzalloc(sizeof(struct hfi1_filedata), GFP_KERNEL);
200 if (fp->private_data) /* no cpu affinity by default */
201 ((struct hfi1_filedata *)fp->private_data)->rec_cpu_num = -1;
202 return fp->private_data ? 0 : -ENOMEM;
205 static ssize_t hfi1_file_write(struct file *fp, const char __user *data,
206 size_t count, loff_t *offset)
208 const struct hfi1_cmd __user *ucmd;
209 struct hfi1_ctxtdata *uctxt = ctxt_fp(fp);
211 struct hfi1_user_info uinfo;
212 struct hfi1_tid_info tinfo;
213 ssize_t consumed = 0, copy = 0, ret = 0;
216 int uctxt_required = 1;
217 int must_be_root = 0;
219 /* FIXME: This interface cannot continue out of staging */
220 if (WARN_ON_ONCE(!ib_safe_file_access(fp)))
223 if (count < sizeof(cmd)) {
228 ucmd = (const struct hfi1_cmd __user *)data;
229 if (copy_from_user(&cmd, ucmd, sizeof(cmd))) {
234 consumed = sizeof(cmd);
237 case HFI1_CMD_ASSIGN_CTXT:
238 uctxt_required = 0; /* assigned user context not required */
239 copy = sizeof(uinfo);
242 case HFI1_CMD_SDMA_STATUS_UPD:
243 case HFI1_CMD_CREDIT_UPD:
246 case HFI1_CMD_TID_UPDATE:
247 case HFI1_CMD_TID_FREE:
248 copy = sizeof(tinfo);
251 case HFI1_CMD_USER_INFO:
252 case HFI1_CMD_RECV_CTRL:
253 case HFI1_CMD_POLL_TYPE:
254 case HFI1_CMD_ACK_EVENT:
255 case HFI1_CMD_CTXT_INFO:
256 case HFI1_CMD_SET_PKEY:
257 case HFI1_CMD_CTXT_RESET:
261 case HFI1_CMD_EP_INFO:
262 case HFI1_CMD_EP_ERASE_CHIP:
263 case HFI1_CMD_EP_ERASE_P0:
264 case HFI1_CMD_EP_ERASE_P1:
265 case HFI1_CMD_EP_READ_P0:
266 case HFI1_CMD_EP_READ_P1:
267 case HFI1_CMD_EP_WRITE_P0:
268 case HFI1_CMD_EP_WRITE_P1:
269 uctxt_required = 0; /* assigned user context not required */
270 must_be_root = 1; /* validate user */
278 /* If the command comes with user data, copy it. */
280 if (copy_from_user(dest, (void __user *)cmd.addr, copy)) {
288 * Make sure there is a uctxt when needed.
290 if (uctxt_required && !uctxt) {
295 /* only root can do these operations */
296 if (must_be_root && !capable(CAP_SYS_ADMIN)) {
302 case HFI1_CMD_ASSIGN_CTXT:
303 ret = assign_ctxt(fp, &uinfo);
306 ret = setup_ctxt(fp);
311 case HFI1_CMD_CTXT_INFO:
312 ret = get_ctxt_info(fp, (void __user *)(unsigned long)
315 case HFI1_CMD_USER_INFO:
316 ret = get_base_info(fp, (void __user *)(unsigned long)
319 case HFI1_CMD_SDMA_STATUS_UPD:
321 case HFI1_CMD_CREDIT_UPD:
322 if (uctxt && uctxt->sc)
323 sc_return_credits(uctxt->sc);
325 case HFI1_CMD_TID_UPDATE:
326 ret = exp_tid_setup(fp, &tinfo);
330 * Copy the number of tidlist entries we used
331 * and the length of the buffer we registered.
332 * These fields are adjacent in the structure so
333 * we can copy them at the same time.
335 addr = (unsigned long)cmd.addr +
336 offsetof(struct hfi1_tid_info, tidcnt);
337 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
338 sizeof(tinfo.tidcnt) +
339 sizeof(tinfo.length)))
343 case HFI1_CMD_TID_FREE:
344 ret = exp_tid_free(fp, &tinfo);
346 case HFI1_CMD_RECV_CTRL:
347 ret = manage_rcvq(uctxt, subctxt_fp(fp), (int)user_val);
349 case HFI1_CMD_POLL_TYPE:
350 uctxt->poll_type = (typeof(uctxt->poll_type))user_val;
352 case HFI1_CMD_ACK_EVENT:
353 ret = user_event_ack(uctxt, subctxt_fp(fp), user_val);
355 case HFI1_CMD_SET_PKEY:
356 if (HFI1_CAP_IS_USET(PKEY_CHECK))
357 ret = set_ctxt_pkey(uctxt, subctxt_fp(fp), user_val);
361 case HFI1_CMD_CTXT_RESET: {
362 struct send_context *sc;
363 struct hfi1_devdata *dd;
365 if (!uctxt || !uctxt->dd || !uctxt->sc) {
370 * There is no protection here. User level has to
371 * guarantee that no one will be writing to the send
372 * context while it is being re-initialized.
373 * If user level breaks that guarantee, it will break
374 * it's own context and no one else's.
379 * Wait until the interrupt handler has marked the
380 * context as halted or frozen. Report error if we time
383 wait_event_interruptible_timeout(
384 sc->halt_wait, (sc->flags & SCF_HALTED),
385 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
386 if (!(sc->flags & SCF_HALTED)) {
391 * If the send context was halted due to a Freeze,
392 * wait until the device has been "unfrozen" before
393 * resetting the context.
395 if (sc->flags & SCF_FROZEN) {
396 wait_event_interruptible_timeout(
398 !(ACCESS_ONCE(dd->flags) & HFI1_FROZEN),
399 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
400 if (dd->flags & HFI1_FROZEN) {
404 if (dd->flags & HFI1_FORCED_FREEZE) {
405 /* Don't allow context reset if we are into
412 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB,
415 ret = sc_restart(sc);
417 sc_return_credits(sc);
420 case HFI1_CMD_EP_INFO:
421 case HFI1_CMD_EP_ERASE_CHIP:
422 case HFI1_CMD_EP_ERASE_P0:
423 case HFI1_CMD_EP_ERASE_P1:
424 case HFI1_CMD_EP_READ_P0:
425 case HFI1_CMD_EP_READ_P1:
426 case HFI1_CMD_EP_WRITE_P0:
427 case HFI1_CMD_EP_WRITE_P1:
428 ret = handle_eprom_command(&cmd);
438 static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from)
440 struct hfi1_user_sdma_pkt_q *pq;
441 struct hfi1_user_sdma_comp_q *cq;
442 int ret = 0, done = 0, reqs = 0;
443 unsigned long dim = from->nr_segs;
445 if (!user_sdma_comp_fp(kiocb->ki_filp) ||
446 !user_sdma_pkt_fp(kiocb->ki_filp)) {
451 if (!iter_is_iovec(from) || !dim) {
456 hfi1_cdbg(SDMA, "SDMA request from %u:%u (%lu)",
457 ctxt_fp(kiocb->ki_filp)->ctxt, subctxt_fp(kiocb->ki_filp),
459 pq = user_sdma_pkt_fp(kiocb->ki_filp);
460 cq = user_sdma_comp_fp(kiocb->ki_filp);
462 if (atomic_read(&pq->n_reqs) == pq->n_max_reqs) {
468 unsigned long count = 0;
470 ret = hfi1_user_sdma_process_request(
471 kiocb->ki_filp, (struct iovec *)(from->iov + done),
480 return ret ? ret : reqs;
483 static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma)
485 struct hfi1_ctxtdata *uctxt;
486 struct hfi1_devdata *dd;
487 unsigned long flags, pfn;
488 u64 token = vma->vm_pgoff << PAGE_SHIFT,
490 u8 subctxt, mapio = 0, vmf = 0, type;
496 if (!is_valid_mmap(token) || !uctxt ||
497 !(vma->vm_flags & VM_SHARED)) {
502 ctxt = HFI1_MMAP_TOKEN_GET(CTXT, token);
503 subctxt = HFI1_MMAP_TOKEN_GET(SUBCTXT, token);
504 type = HFI1_MMAP_TOKEN_GET(TYPE, token);
505 if (ctxt != uctxt->ctxt || subctxt != subctxt_fp(fp)) {
510 flags = vma->vm_flags;
515 memaddr = ((dd->physaddr + TXE_PIO_SEND) +
517 (uctxt->sc->hw_context * BIT(16))) +
518 /* 64K PIO space / ctxt */
519 (type == PIO_BUFS_SOP ?
520 (TXE_PIO_SIZE / 2) : 0); /* sop? */
522 * Map only the amount allocated to the context, not the
523 * entire available context's PIO space.
525 memlen = ALIGN(uctxt->sc->credits * PIO_BLOCK_SIZE,
527 flags &= ~VM_MAYREAD;
528 flags |= VM_DONTCOPY | VM_DONTEXPAND;
529 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
533 if (flags & VM_WRITE) {
538 * The credit return location for this context could be on the
539 * second or third page allocated for credit returns (if number
540 * of enabled contexts > 64 and 128 respectively).
542 memaddr = dd->cr_base[uctxt->numa_id].pa +
543 (((u64)uctxt->sc->hw_free -
544 (u64)dd->cr_base[uctxt->numa_id].va) & PAGE_MASK);
546 flags &= ~VM_MAYWRITE;
547 flags |= VM_DONTCOPY | VM_DONTEXPAND;
549 * The driver has already allocated memory for credit
550 * returns and programmed it into the chip. Has that
551 * memory been flagged as non-cached?
553 /* vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); */
557 memaddr = uctxt->rcvhdrq_phys;
558 memlen = uctxt->rcvhdrq_size;
564 * The RcvEgr buffer need to be handled differently
565 * as multiple non-contiguous pages need to be mapped
566 * into the user process.
568 memlen = uctxt->egrbufs.size;
569 if ((vma->vm_end - vma->vm_start) != memlen) {
570 dd_dev_err(dd, "Eager buffer map size invalid (%lu != %lu)\n",
571 (vma->vm_end - vma->vm_start), memlen);
575 if (vma->vm_flags & VM_WRITE) {
579 vma->vm_flags &= ~VM_MAYWRITE;
580 addr = vma->vm_start;
581 for (i = 0 ; i < uctxt->egrbufs.numbufs; i++) {
582 ret = remap_pfn_range(
584 uctxt->egrbufs.buffers[i].phys >> PAGE_SHIFT,
585 uctxt->egrbufs.buffers[i].len,
589 addr += uctxt->egrbufs.buffers[i].len;
596 * Map only the page that contains this context's user
599 memaddr = (unsigned long)
600 (dd->physaddr + RXE_PER_CONTEXT_USER)
601 + (uctxt->ctxt * RXE_PER_CONTEXT_SIZE);
603 * TidFlow table is on the same page as the rest of the
607 flags |= VM_DONTCOPY | VM_DONTEXPAND;
608 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
613 * Use the page where this context's flags are. User level
614 * knows where it's own bitmap is within the page.
616 memaddr = (unsigned long)(dd->events +
617 ((uctxt->ctxt - dd->first_user_ctxt) *
618 HFI1_MAX_SHARED_CTXTS)) & PAGE_MASK;
621 * v3.7 removes VM_RESERVED but the effect is kept by
624 flags |= VM_IO | VM_DONTEXPAND;
628 memaddr = kvirt_to_phys((void *)dd->status);
630 flags |= VM_IO | VM_DONTEXPAND;
633 if (!HFI1_CAP_IS_USET(DMA_RTAIL)) {
635 * If the memory allocation failed, the context alloc
636 * also would have failed, so we would never get here
641 if (flags & VM_WRITE) {
645 memaddr = uctxt->rcvhdrqtailaddr_phys;
647 flags &= ~VM_MAYWRITE;
650 memaddr = (u64)uctxt->subctxt_uregbase;
652 flags |= VM_IO | VM_DONTEXPAND;
655 case SUBCTXT_RCV_HDRQ:
656 memaddr = (u64)uctxt->subctxt_rcvhdr_base;
657 memlen = uctxt->rcvhdrq_size * uctxt->subctxt_cnt;
658 flags |= VM_IO | VM_DONTEXPAND;
662 memaddr = (u64)uctxt->subctxt_rcvegrbuf;
663 memlen = uctxt->egrbufs.size * uctxt->subctxt_cnt;
664 flags |= VM_IO | VM_DONTEXPAND;
665 flags &= ~VM_MAYWRITE;
669 struct hfi1_user_sdma_comp_q *cq;
671 if (!user_sdma_comp_fp(fp)) {
675 cq = user_sdma_comp_fp(fp);
676 memaddr = (u64)cq->comps;
677 memlen = ALIGN(sizeof(*cq->comps) * cq->nentries, PAGE_SIZE);
678 flags |= VM_IO | VM_DONTEXPAND;
687 if ((vma->vm_end - vma->vm_start) != memlen) {
688 hfi1_cdbg(PROC, "%u:%u Memory size mismatch %lu:%lu",
689 uctxt->ctxt, subctxt_fp(fp),
690 (vma->vm_end - vma->vm_start), memlen);
695 vma->vm_flags = flags;
697 "%s: %u:%u type:%u io/vf:%d/%d, addr:0x%llx, len:%lu(%lu), flags:0x%lx\n",
698 __func__, ctxt, subctxt, type, mapio, vmf, memaddr, memlen,
699 vma->vm_end - vma->vm_start, vma->vm_flags);
700 pfn = (unsigned long)(memaddr >> PAGE_SHIFT);
703 vma->vm_ops = &vm_ops;
706 ret = io_remap_pfn_range(vma, vma->vm_start, pfn, memlen,
709 ret = remap_pfn_range(vma, vma->vm_start, pfn, memlen,
717 * Local (non-chip) user memory is not mapped right away but as it is
718 * accessed by the user-level code.
720 static int vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
724 page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
726 return VM_FAULT_SIGBUS;
734 static unsigned int hfi1_poll(struct file *fp, struct poll_table_struct *pt)
736 struct hfi1_ctxtdata *uctxt;
742 else if (uctxt->poll_type == HFI1_POLL_TYPE_URGENT)
743 pollflag = poll_urgent(fp, pt);
744 else if (uctxt->poll_type == HFI1_POLL_TYPE_ANYRCV)
745 pollflag = poll_next(fp, pt);
752 static int hfi1_file_close(struct inode *inode, struct file *fp)
754 struct hfi1_filedata *fdata = fp->private_data;
755 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
756 struct hfi1_devdata *dd;
757 unsigned long flags, *ev;
759 fp->private_data = NULL;
764 hfi1_cdbg(PROC, "freeing ctxt %u:%u", uctxt->ctxt, fdata->subctxt);
766 mutex_lock(&hfi1_mutex);
769 /* drain user sdma queue */
771 hfi1_user_sdma_free_queues(fdata);
774 * Clear any left over, unhandled events so the next process that
775 * gets this context doesn't get confused.
777 ev = dd->events + ((uctxt->ctxt - dd->first_user_ctxt) *
778 HFI1_MAX_SHARED_CTXTS) + fdata->subctxt;
782 uctxt->active_slaves &= ~(1 << fdata->subctxt);
783 uctxt->subpid[fdata->subctxt] = 0;
784 mutex_unlock(&hfi1_mutex);
788 spin_lock_irqsave(&dd->uctxt_lock, flags);
790 * Disable receive context and interrupt available, reset all
791 * RcvCtxtCtrl bits to default values.
793 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
794 HFI1_RCVCTRL_TIDFLOW_DIS |
795 HFI1_RCVCTRL_INTRAVAIL_DIS |
796 HFI1_RCVCTRL_ONE_PKT_EGR_DIS |
797 HFI1_RCVCTRL_NO_RHQ_DROP_DIS |
798 HFI1_RCVCTRL_NO_EGR_DROP_DIS, uctxt->ctxt);
799 /* Clear the context's J_KEY */
800 hfi1_clear_ctxt_jkey(dd, uctxt->ctxt);
802 * Reset context integrity checks to default.
803 * (writes to CSRs probably belong in chip.c)
805 write_kctxt_csr(dd, uctxt->sc->hw_context, SEND_CTXT_CHECK_ENABLE,
806 hfi1_pkt_default_send_ctxt_mask(dd, uctxt->sc->type));
807 sc_disable(uctxt->sc);
809 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
811 dd->rcd[uctxt->ctxt] = NULL;
812 uctxt->rcvwait_to = 0;
813 uctxt->piowait_to = 0;
814 uctxt->rcvnowait = 0;
815 uctxt->pionowait = 0;
816 uctxt->event_flags = 0;
818 hfi1_clear_tids(uctxt);
819 hfi1_clear_ctxt_pkey(dd, uctxt->ctxt);
821 if (uctxt->tid_pg_list)
822 unlock_exp_tids(uctxt);
824 hfi1_stats.sps_ctxts--;
826 mutex_unlock(&hfi1_mutex);
827 hfi1_free_ctxtdata(dd, uctxt);
834 * Convert kernel *virtual* addresses to physical addresses.
835 * This is used to vmalloc'ed addresses.
837 static u64 kvirt_to_phys(void *addr)
842 page = vmalloc_to_page(addr);
844 paddr = page_to_pfn(page) << PAGE_SHIFT;
849 static int assign_ctxt(struct file *fp, struct hfi1_user_info *uinfo)
851 int i_minor, ret = 0;
852 unsigned swmajor, swminor, alg = HFI1_ALG_ACROSS;
854 swmajor = uinfo->userversion >> 16;
855 if (swmajor != HFI1_USER_SWMAJOR) {
860 swminor = uinfo->userversion & 0xffff;
862 if (uinfo->hfi1_alg < HFI1_ALG_COUNT)
863 alg = uinfo->hfi1_alg;
865 mutex_lock(&hfi1_mutex);
866 /* First, lets check if we need to setup a shared context? */
867 if (uinfo->subctxt_cnt)
868 ret = find_shared_ctxt(fp, uinfo);
871 * We execute the following block if we couldn't find a
872 * shared context or if context sharing is not required.
875 i_minor = iminor(file_inode(fp)) - HFI1_USER_MINOR_BASE;
876 ret = get_user_context(fp, uinfo, i_minor - 1, alg);
878 mutex_unlock(&hfi1_mutex);
883 static int get_user_context(struct file *fp, struct hfi1_user_info *uinfo,
884 int devno, unsigned alg)
886 struct hfi1_devdata *dd = NULL;
887 int ret = 0, devmax, npresent, nup, dev;
889 devmax = hfi1_count_units(&npresent, &nup);
899 dd = hfi1_lookup(devno);
902 else if (!dd->freectxts)
905 struct hfi1_devdata *pdd;
907 if (alg == HFI1_ALG_ACROSS) {
910 for (dev = 0; dev < devmax; dev++) {
911 pdd = hfi1_lookup(dev);
912 if (pdd && pdd->freectxts &&
913 pdd->freectxts > free) {
915 free = pdd->freectxts;
919 for (dev = 0; dev < devmax; dev++) {
920 pdd = hfi1_lookup(dev);
921 if (pdd && pdd->freectxts) {
931 return ret ? ret : allocate_ctxt(fp, dd, uinfo);
934 static int find_shared_ctxt(struct file *fp,
935 const struct hfi1_user_info *uinfo)
940 devmax = hfi1_count_units(NULL, NULL);
942 for (ndev = 0; ndev < devmax; ndev++) {
943 struct hfi1_devdata *dd = hfi1_lookup(ndev);
945 /* device portion of usable() */
946 if (!(dd && (dd->flags & HFI1_PRESENT) && dd->kregbase))
948 for (i = dd->first_user_ctxt; i < dd->num_rcv_contexts; i++) {
949 struct hfi1_ctxtdata *uctxt = dd->rcd[i];
951 /* Skip ctxts which are not yet open */
952 if (!uctxt || !uctxt->cnt)
954 /* Skip ctxt if it doesn't match the requested one */
955 if (memcmp(uctxt->uuid, uinfo->uuid,
956 sizeof(uctxt->uuid)) ||
957 uctxt->jkey != generate_jkey(current_uid()) ||
958 uctxt->subctxt_id != uinfo->subctxt_id ||
959 uctxt->subctxt_cnt != uinfo->subctxt_cnt)
962 /* Verify the sharing process matches the master */
963 if (uctxt->userversion != uinfo->userversion ||
964 uctxt->cnt >= uctxt->subctxt_cnt) {
969 subctxt_fp(fp) = uctxt->cnt++;
970 uctxt->subpid[subctxt_fp(fp)] = current->pid;
971 uctxt->active_slaves |= 1 << subctxt_fp(fp);
981 static int allocate_ctxt(struct file *fp, struct hfi1_devdata *dd,
982 struct hfi1_user_info *uinfo)
984 struct hfi1_ctxtdata *uctxt;
988 if (dd->flags & HFI1_FROZEN) {
990 * Pick an error that is unique from all other errors
991 * that are returned so the user process knows that
992 * it tried to allocate while the SPC was frozen. It
993 * it should be able to retry with success in a short
999 for (ctxt = dd->first_user_ctxt; ctxt < dd->num_rcv_contexts; ctxt++)
1003 if (ctxt == dd->num_rcv_contexts)
1006 uctxt = hfi1_create_ctxtdata(dd->pport, ctxt);
1009 "Unable to allocate ctxtdata memory, failing open\n");
1013 * Allocate and enable a PIO send context.
1015 uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize,
1020 dbg("allocated send context %u(%u)\n", uctxt->sc->sw_index,
1021 uctxt->sc->hw_context);
1022 ret = sc_enable(uctxt->sc);
1026 * Setup shared context resources if the user-level has requested
1027 * shared contexts and this is the 'master' process.
1028 * This has to be done here so the rest of the sub-contexts find the
1031 if (uinfo->subctxt_cnt && !subctxt_fp(fp)) {
1032 ret = init_subctxts(uctxt, uinfo);
1034 * On error, we don't need to disable and de-allocate the
1035 * send context because it will be done during file close
1040 uctxt->userversion = uinfo->userversion;
1041 uctxt->pid = current->pid;
1042 uctxt->flags = HFI1_CAP_UGET(MASK);
1043 init_waitqueue_head(&uctxt->wait);
1044 strlcpy(uctxt->comm, current->comm, sizeof(uctxt->comm));
1045 memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid));
1046 uctxt->jkey = generate_jkey(current_uid());
1047 INIT_LIST_HEAD(&uctxt->sdma_queues);
1048 spin_lock_init(&uctxt->sdma_qlock);
1049 hfi1_stats.sps_ctxts++;
1051 ctxt_fp(fp) = uctxt;
1056 static int init_subctxts(struct hfi1_ctxtdata *uctxt,
1057 const struct hfi1_user_info *uinfo)
1060 unsigned num_subctxts;
1062 num_subctxts = uinfo->subctxt_cnt;
1063 if (num_subctxts > HFI1_MAX_SHARED_CTXTS) {
1068 uctxt->subctxt_cnt = uinfo->subctxt_cnt;
1069 uctxt->subctxt_id = uinfo->subctxt_id;
1070 uctxt->active_slaves = 1;
1071 uctxt->redirect_seq_cnt = 1;
1072 set_bit(HFI1_CTXT_MASTER_UNINIT, &uctxt->event_flags);
1077 static int setup_subctxt(struct hfi1_ctxtdata *uctxt)
1080 unsigned num_subctxts = uctxt->subctxt_cnt;
1082 uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE);
1083 if (!uctxt->subctxt_uregbase) {
1087 /* We can take the size of the RcvHdr Queue from the master */
1088 uctxt->subctxt_rcvhdr_base = vmalloc_user(uctxt->rcvhdrq_size *
1090 if (!uctxt->subctxt_rcvhdr_base) {
1095 uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size *
1097 if (!uctxt->subctxt_rcvegrbuf) {
1103 vfree(uctxt->subctxt_rcvhdr_base);
1105 vfree(uctxt->subctxt_uregbase);
1106 uctxt->subctxt_uregbase = NULL;
1111 static int user_init(struct file *fp)
1114 unsigned int rcvctrl_ops = 0;
1115 struct hfi1_ctxtdata *uctxt = ctxt_fp(fp);
1117 /* make sure that the context has already been setup */
1118 if (!test_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags)) {
1124 * Subctxts don't need to initialize anything since master
1127 if (subctxt_fp(fp)) {
1128 ret = wait_event_interruptible(uctxt->wait,
1129 !test_bit(HFI1_CTXT_MASTER_UNINIT,
1130 &uctxt->event_flags));
1134 /* initialize poll variables... */
1136 uctxt->urgent_poll = 0;
1139 * Now enable the ctxt for receive.
1140 * For chips that are set to DMA the tail register to memory
1141 * when they change (and when the update bit transitions from
1142 * 0 to 1. So for those chips, we turn it off and then back on.
1143 * This will (very briefly) affect any other open ctxts, but the
1144 * duration is very short, and therefore isn't an issue. We
1145 * explicitly set the in-memory tail copy to 0 beforehand, so we
1146 * don't have to wait to be sure the DMA update has happened
1147 * (chip resets head/tail to 0 on transition to enable).
1149 if (uctxt->rcvhdrtail_kvaddr)
1150 clear_rcvhdrtail(uctxt);
1152 /* Setup J_KEY before enabling the context */
1153 hfi1_set_ctxt_jkey(uctxt->dd, uctxt->ctxt, uctxt->jkey);
1155 rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB;
1156 if (HFI1_CAP_KGET_MASK(uctxt->flags, HDRSUPP))
1157 rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB;
1159 * Ignore the bit in the flags for now until proper
1160 * support for multiple packet per rcv array entry is
1163 if (!HFI1_CAP_KGET_MASK(uctxt->flags, MULTI_PKT_EGR))
1164 rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
1165 if (HFI1_CAP_KGET_MASK(uctxt->flags, NODROP_EGR_FULL))
1166 rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
1167 if (HFI1_CAP_KGET_MASK(uctxt->flags, NODROP_RHQ_FULL))
1168 rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
1169 if (HFI1_CAP_KGET_MASK(uctxt->flags, DMA_RTAIL))
1170 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB;
1171 hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt->ctxt);
1173 /* Notify any waiting slaves */
1174 if (uctxt->subctxt_cnt) {
1175 clear_bit(HFI1_CTXT_MASTER_UNINIT, &uctxt->event_flags);
1176 wake_up(&uctxt->wait);
1184 static int get_ctxt_info(struct file *fp, void __user *ubase, __u32 len)
1186 struct hfi1_ctxt_info cinfo;
1187 struct hfi1_ctxtdata *uctxt = ctxt_fp(fp);
1188 struct hfi1_filedata *fd = fp->private_data;
1191 memset(&cinfo, 0, sizeof(cinfo));
1192 ret = hfi1_get_base_kinfo(uctxt, &cinfo);
1195 cinfo.num_active = hfi1_count_active_units();
1196 cinfo.unit = uctxt->dd->unit;
1197 cinfo.ctxt = uctxt->ctxt;
1198 cinfo.subctxt = subctxt_fp(fp);
1199 cinfo.rcvtids = roundup(uctxt->egrbufs.alloced,
1200 uctxt->dd->rcv_entries.group_size) +
1201 uctxt->expected_count;
1202 cinfo.credits = uctxt->sc->credits;
1203 cinfo.numa_node = uctxt->numa_id;
1204 cinfo.rec_cpu = fd->rec_cpu_num;
1205 cinfo.send_ctxt = uctxt->sc->hw_context;
1207 cinfo.egrtids = uctxt->egrbufs.alloced;
1208 cinfo.rcvhdrq_cnt = uctxt->rcvhdrq_cnt;
1209 cinfo.rcvhdrq_entsize = uctxt->rcvhdrqentsize << 2;
1210 cinfo.sdma_ring_size = user_sdma_comp_fp(fp)->nentries;
1211 cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size;
1213 trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, subctxt_fp(fp), cinfo);
1214 if (copy_to_user(ubase, &cinfo, sizeof(cinfo)))
1220 static int setup_ctxt(struct file *fp)
1222 struct hfi1_ctxtdata *uctxt = ctxt_fp(fp);
1223 struct hfi1_devdata *dd = uctxt->dd;
1227 * Context should be set up only once (including allocation and
1228 * programming of eager buffers. This is done if context sharing
1229 * is not requested or by the master process.
1231 if (!uctxt->subctxt_cnt || !subctxt_fp(fp)) {
1232 ret = hfi1_init_ctxt(uctxt->sc);
1236 /* Now allocate the RcvHdr queue and eager buffers. */
1237 ret = hfi1_create_rcvhdrq(dd, uctxt);
1240 ret = hfi1_setup_eagerbufs(uctxt);
1243 if (uctxt->subctxt_cnt && !subctxt_fp(fp)) {
1244 ret = setup_subctxt(uctxt);
1248 /* Setup Expected Rcv memories */
1249 uctxt->tid_pg_list = vzalloc(uctxt->expected_count *
1250 sizeof(struct page **));
1251 if (!uctxt->tid_pg_list) {
1255 uctxt->physshadow = vzalloc(uctxt->expected_count *
1256 sizeof(*uctxt->physshadow));
1257 if (!uctxt->physshadow) {
1261 /* allocate expected TID map and initialize the cursor */
1262 atomic_set(&uctxt->tidcursor, 0);
1263 uctxt->numtidgroups = uctxt->expected_count /
1264 dd->rcv_entries.group_size;
1265 uctxt->tidmapcnt = uctxt->numtidgroups / BITS_PER_LONG +
1266 !!(uctxt->numtidgroups % BITS_PER_LONG);
1267 uctxt->tidusemap = kzalloc_node(uctxt->tidmapcnt *
1268 sizeof(*uctxt->tidusemap),
1269 GFP_KERNEL, uctxt->numa_id);
1270 if (!uctxt->tidusemap) {
1275 * In case that the number of groups is not a multiple of
1276 * 64 (the number of groups in a tidusemap element), mark
1277 * the extra ones as used. This will effectively make them
1278 * permanently used and should never be assigned. Otherwise,
1279 * the code which checks how many free groups we have will
1280 * get completely confused about the state of the bits.
1282 if (uctxt->numtidgroups % BITS_PER_LONG)
1283 uctxt->tidusemap[uctxt->tidmapcnt - 1] =
1284 ~((1ULL << (uctxt->numtidgroups %
1285 BITS_PER_LONG)) - 1);
1286 trace_hfi1_exp_tid_map(uctxt->ctxt, subctxt_fp(fp), 0,
1287 uctxt->tidusemap, uctxt->tidmapcnt);
1289 ret = hfi1_user_sdma_alloc_queues(uctxt, fp);
1293 set_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags);
1298 static int get_base_info(struct file *fp, void __user *ubase, __u32 len)
1300 struct hfi1_base_info binfo;
1301 struct hfi1_ctxtdata *uctxt = ctxt_fp(fp);
1302 struct hfi1_devdata *dd = uctxt->dd;
1307 trace_hfi1_uctxtdata(uctxt->dd, uctxt);
1309 memset(&binfo, 0, sizeof(binfo));
1310 binfo.hw_version = dd->revision;
1311 binfo.sw_version = HFI1_KERN_SWVERSION;
1312 binfo.bthqp = kdeth_qp;
1313 binfo.jkey = uctxt->jkey;
1315 * If more than 64 contexts are enabled the allocated credit
1316 * return will span two or three contiguous pages. Since we only
1317 * map the page containing the context's credit return address,
1318 * we need to calculate the offset in the proper page.
1320 offset = ((u64)uctxt->sc->hw_free -
1321 (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE;
1322 binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt,
1323 subctxt_fp(fp), offset);
1324 binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt,
1326 uctxt->sc->base_addr);
1327 binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP,
1330 uctxt->sc->base_addr);
1331 binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt,
1334 binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt,
1336 uctxt->egrbufs.rcvtids[0].phys);
1337 binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt,
1341 * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE))
1343 binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt,
1345 offset = offset_in_page((((uctxt->ctxt - dd->first_user_ctxt) *
1346 HFI1_MAX_SHARED_CTXTS) + subctxt_fp(fp)) *
1347 sizeof(*dd->events));
1348 binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt,
1351 binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt,
1354 if (HFI1_CAP_IS_USET(DMA_RTAIL))
1355 binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt,
1357 if (uctxt->subctxt_cnt) {
1358 binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS,
1361 binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ,
1364 binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF,
1368 sz = (len < sizeof(binfo)) ? len : sizeof(binfo);
1369 if (copy_to_user(ubase, &binfo, sz))
1374 static unsigned int poll_urgent(struct file *fp,
1375 struct poll_table_struct *pt)
1377 struct hfi1_ctxtdata *uctxt = ctxt_fp(fp);
1378 struct hfi1_devdata *dd = uctxt->dd;
1381 poll_wait(fp, &uctxt->wait, pt);
1383 spin_lock_irq(&dd->uctxt_lock);
1384 if (uctxt->urgent != uctxt->urgent_poll) {
1385 pollflag = POLLIN | POLLRDNORM;
1386 uctxt->urgent_poll = uctxt->urgent;
1389 set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags);
1391 spin_unlock_irq(&dd->uctxt_lock);
1396 static unsigned int poll_next(struct file *fp,
1397 struct poll_table_struct *pt)
1399 struct hfi1_ctxtdata *uctxt = ctxt_fp(fp);
1400 struct hfi1_devdata *dd = uctxt->dd;
1403 poll_wait(fp, &uctxt->wait, pt);
1405 spin_lock_irq(&dd->uctxt_lock);
1406 if (hdrqempty(uctxt)) {
1407 set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags);
1408 hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt->ctxt);
1411 pollflag = POLLIN | POLLRDNORM;
1412 spin_unlock_irq(&dd->uctxt_lock);
1418 * Find all user contexts in use, and set the specified bit in their
1420 * See also find_ctxt() for a similar use, that is specific to send buffers.
1422 int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit)
1424 struct hfi1_ctxtdata *uctxt;
1425 struct hfi1_devdata *dd = ppd->dd;
1428 unsigned long flags;
1435 spin_lock_irqsave(&dd->uctxt_lock, flags);
1436 for (ctxt = dd->first_user_ctxt; ctxt < dd->num_rcv_contexts;
1438 uctxt = dd->rcd[ctxt];
1440 unsigned long *evs = dd->events +
1441 (uctxt->ctxt - dd->first_user_ctxt) *
1442 HFI1_MAX_SHARED_CTXTS;
1445 * subctxt_cnt is 0 if not shared, so do base
1446 * separately, first, then remaining subctxt, if any
1448 set_bit(evtbit, evs);
1449 for (i = 1; i < uctxt->subctxt_cnt; i++)
1450 set_bit(evtbit, evs + i);
1453 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1459 * manage_rcvq - manage a context's receive queue
1460 * @uctxt: the context
1461 * @subctxt: the sub-context
1462 * @start_stop: action to carry out
1464 * start_stop == 0 disables receive on the context, for use in queue
1465 * overflow conditions. start_stop==1 re-enables, to be used to
1466 * re-init the software copy of the head register
1468 static int manage_rcvq(struct hfi1_ctxtdata *uctxt, unsigned subctxt,
1471 struct hfi1_devdata *dd = uctxt->dd;
1472 unsigned int rcvctrl_op;
1476 /* atomically clear receive enable ctxt. */
1479 * On enable, force in-memory copy of the tail register to
1480 * 0, so that protocol code doesn't have to worry about
1481 * whether or not the chip has yet updated the in-memory
1482 * copy or not on return from the system call. The chip
1483 * always resets it's tail register back to 0 on a
1484 * transition from disabled to enabled.
1486 if (uctxt->rcvhdrtail_kvaddr)
1487 clear_rcvhdrtail(uctxt);
1488 rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB;
1490 rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS;
1491 hfi1_rcvctrl(dd, rcvctrl_op, uctxt->ctxt);
1492 /* always; new head should be equal to new tail; see above */
1498 * clear the event notifier events for this context.
1499 * User process then performs actions appropriate to bit having been
1500 * set, if desired, and checks again in future.
1502 static int user_event_ack(struct hfi1_ctxtdata *uctxt, int subctxt,
1503 unsigned long events)
1506 struct hfi1_devdata *dd = uctxt->dd;
1512 evs = dd->events + ((uctxt->ctxt - dd->first_user_ctxt) *
1513 HFI1_MAX_SHARED_CTXTS) + subctxt;
1515 for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) {
1516 if (!test_bit(i, &events))
1523 #define num_user_pages(vaddr, len) \
1524 (1 + (((((unsigned long)(vaddr) + \
1525 (unsigned long)(len) - 1) & PAGE_MASK) - \
1526 ((unsigned long)vaddr & PAGE_MASK)) >> PAGE_SHIFT))
1529 * tzcnt - count the number of trailing zeros in a 64bit value
1530 * @value: the value to be examined
1532 * Returns the number of trailing least significant zeros in the
1533 * the input value. If the value is zero, return the number of
1534 * bits of the value.
1536 static inline u8 tzcnt(u64 value)
1538 return value ? __builtin_ctzl(value) : sizeof(value) * 8;
1541 static inline unsigned num_free_groups(unsigned long map, u16 *start)
1544 u16 bitidx = *start;
1546 if (bitidx >= BITS_PER_LONG)
1548 /* "Turn off" any bits set before our bit index */
1549 map &= ~((1ULL << bitidx) - 1);
1550 free = tzcnt(map) - bitidx;
1551 while (!free && bitidx < BITS_PER_LONG) {
1552 /* Zero out the last set bit so we look at the rest */
1553 map &= ~(1ULL << bitidx);
1555 * Account for the previously checked bits and advance
1556 * the bit index. We don't have to check for bitidx
1557 * getting bigger than BITS_PER_LONG here as it would
1558 * mean extra instructions that we don't need. If it
1559 * did happen, it would push free to a negative value
1560 * which will break the loop.
1562 free = tzcnt(map) - ++bitidx;
1568 static int exp_tid_setup(struct file *fp, struct hfi1_tid_info *tinfo)
1571 struct hfi1_ctxtdata *uctxt = ctxt_fp(fp);
1572 struct hfi1_devdata *dd = uctxt->dd;
1573 unsigned tid, mapped = 0, npages, ngroups, exp_groups,
1574 tidpairs = uctxt->expected_count / 2;
1575 struct page **pages;
1576 unsigned long vaddr, tidmap[uctxt->tidmapcnt];
1578 u32 tidlist[tidpairs], pairidx = 0, tidcursor;
1579 u16 useidx, idx, bitidx, tidcnt = 0;
1581 vaddr = tinfo->vaddr;
1583 if (offset_in_page(vaddr)) {
1588 npages = num_user_pages(vaddr, tinfo->length);
1593 if (!access_ok(VERIFY_WRITE, (void __user *)vaddr,
1594 npages * PAGE_SIZE)) {
1595 dd_dev_err(dd, "Fail vaddr %p, %u pages, !access_ok\n",
1596 (void *)vaddr, npages);
1601 memset(tidmap, 0, sizeof(tidmap[0]) * uctxt->tidmapcnt);
1602 memset(tidlist, 0, sizeof(tidlist[0]) * tidpairs);
1604 exp_groups = uctxt->expected_count / dd->rcv_entries.group_size;
1605 /* which group set do we look at first? */
1606 tidcursor = atomic_read(&uctxt->tidcursor);
1607 useidx = (tidcursor >> 16) & 0xffff;
1608 bitidx = tidcursor & 0xffff;
1611 * Keep going until we've mapped all pages or we've exhausted all
1613 * This iterates over the number of tidmaps + 1
1614 * (idx <= uctxt->tidmapcnt) so we check the bitmap which we
1615 * started from one more time for any free bits before the
1616 * starting point bit.
1618 for (mapped = 0, idx = 0;
1619 mapped < npages && idx <= uctxt->tidmapcnt;) {
1621 unsigned free, pinned, pmapped = 0, bits_used;
1625 * "Reserve" the needed group bits under lock so other
1626 * processes can't step in the middle of it. Once
1627 * reserved, we don't need the lock anymore since we
1628 * are guaranteed the groups.
1630 spin_lock(&uctxt->exp_lock);
1631 if (uctxt->tidusemap[useidx] == -1ULL ||
1632 bitidx >= BITS_PER_LONG) {
1633 /* no free groups in the set, use the next */
1634 useidx = (useidx + 1) % uctxt->tidmapcnt;
1637 spin_unlock(&uctxt->exp_lock);
1640 ngroups = ((npages - mapped) / dd->rcv_entries.group_size) +
1641 !!((npages - mapped) % dd->rcv_entries.group_size);
1644 * If we've gotten here, the current set of groups does have
1645 * one or more free groups.
1647 free = num_free_groups(uctxt->tidusemap[useidx], &bitidx);
1650 * Despite the check above, free could still come back
1651 * as 0 because we don't check the entire bitmap but
1652 * we start from bitidx.
1654 spin_unlock(&uctxt->exp_lock);
1657 bits_used = min(free, ngroups);
1658 tidmap[useidx] |= ((1ULL << bits_used) - 1) << bitidx;
1659 uctxt->tidusemap[useidx] |= tidmap[useidx];
1660 spin_unlock(&uctxt->exp_lock);
1663 * At this point, we know where in the map we have free bits.
1664 * properly offset into the various "shadow" arrays and compute
1665 * the RcvArray entry index.
1667 offset = ((useidx * BITS_PER_LONG) + bitidx) *
1668 dd->rcv_entries.group_size;
1669 pages = uctxt->tid_pg_list + offset;
1670 phys = uctxt->physshadow + offset;
1671 tid = uctxt->expected_base + offset;
1673 /* Calculate how many pages we can pin based on free bits */
1674 pinned = min((bits_used * dd->rcv_entries.group_size),
1677 * Now that we know how many free RcvArray entries we have,
1678 * we can pin that many user pages.
1680 ret = hfi1_get_user_pages(vaddr + (mapped * PAGE_SIZE),
1684 * We can't continue because the pages array won't be
1685 * initialized. This should never happen,
1686 * unless perhaps the user has mpin'ed the pages
1690 "Failed to lock addr %p, %u pages: errno %d\n",
1691 (void *) vaddr, pinned, -ret);
1693 * Let go of the bits that we reserved since we are not
1694 * going to use them.
1696 spin_lock(&uctxt->exp_lock);
1697 uctxt->tidusemap[useidx] &=
1698 ~(((1ULL << bits_used) - 1) << bitidx);
1699 spin_unlock(&uctxt->exp_lock);
1703 * How many groups do we need based on how many pages we have
1706 ngroups = (pinned / dd->rcv_entries.group_size) +
1707 !!(pinned % dd->rcv_entries.group_size);
1709 * Keep programming RcvArray entries for all the <ngroups> free
1712 for (i = 0, grp = 0; grp < ngroups; i++, grp++) {
1714 u32 pair_size = 0, tidsize;
1716 * This inner loop will program an entire group or the
1717 * array of pinned pages (which ever limit is hit
1720 for (j = 0; j < dd->rcv_entries.group_size &&
1721 pmapped < pinned; j++, pmapped++, tid++) {
1722 tidsize = PAGE_SIZE;
1723 phys[pmapped] = hfi1_map_page(dd->pcidev,
1725 tidsize, PCI_DMA_FROMDEVICE);
1726 trace_hfi1_exp_rcv_set(uctxt->ctxt,
1732 * Each RcvArray entry is programmed with one
1733 * page * worth of memory. This will handle
1734 * the 8K MTU as well as anything smaller
1735 * due to the fact that both entries in the
1736 * RcvTidPair are programmed with a page.
1737 * PSM currently does not handle anything
1738 * bigger than 8K MTU, so should we even worry
1741 hfi1_put_tid(dd, tid, PT_EXPECTED,
1743 ilog2(tidsize >> PAGE_SHIFT) + 1);
1744 pair_size += tidsize >> PAGE_SHIFT;
1745 EXP_TID_RESET(tidlist[pairidx], LEN, pair_size);
1749 (tid - uctxt->expected_base)
1752 EXP_TID_SET(CTRL, 1);
1756 EXP_TID_SET(CTRL, 2);
1762 * We've programmed the entire group (or as much of the
1763 * group as we'll use. Now, it's time to push it out...
1768 atomic_set(&uctxt->tidcursor,
1769 (((useidx & 0xffffff) << 16) |
1770 ((bitidx + bits_used) & 0xffffff)));
1772 trace_hfi1_exp_tid_map(uctxt->ctxt, subctxt_fp(fp), 0, uctxt->tidusemap,
1776 /* If we've mapped anything, copy relevant info to user */
1778 if (copy_to_user((void __user *)(unsigned long)tinfo->tidlist,
1779 tidlist, sizeof(tidlist[0]) * tidcnt)) {
1783 /* copy TID info to user */
1784 if (copy_to_user((void __user *)(unsigned long)tinfo->tidmap,
1785 tidmap, sizeof(tidmap[0]) * uctxt->tidmapcnt))
1790 * Calculate mapped length. New Exp TID protocol does not "unwind" and
1791 * report an error if it can't map the entire buffer. It just reports
1792 * the length that was mapped.
1794 tinfo->length = mapped * PAGE_SIZE;
1795 tinfo->tidcnt = tidcnt;
1799 static int exp_tid_free(struct file *fp, struct hfi1_tid_info *tinfo)
1801 struct hfi1_ctxtdata *uctxt = ctxt_fp(fp);
1802 struct hfi1_devdata *dd = uctxt->dd;
1803 unsigned long tidmap[uctxt->tidmapcnt];
1804 struct page **pages;
1806 u16 idx, bitidx, tid;
1809 if (copy_from_user(&tidmap, (void __user *)(unsigned long)
1811 sizeof(tidmap[0]) * uctxt->tidmapcnt)) {
1815 for (idx = 0; idx < uctxt->tidmapcnt; idx++) {
1822 while ((bitidx = tzcnt(map)) < BITS_PER_LONG) {
1824 struct page *pshadow[dd->rcv_entries.group_size];
1825 unsigned offset = ((idx * BITS_PER_LONG) + bitidx) *
1826 dd->rcv_entries.group_size;
1828 pages = uctxt->tid_pg_list + offset;
1829 phys = uctxt->physshadow + offset;
1830 tid = uctxt->expected_base + offset;
1831 for (i = 0; i < dd->rcv_entries.group_size;
1834 hfi1_put_tid(dd, tid, PT_INVALID,
1836 trace_hfi1_exp_rcv_free(uctxt->ctxt,
1840 pci_unmap_page(dd->pcidev, phys[i],
1841 PAGE_SIZE, PCI_DMA_FROMDEVICE);
1842 pshadow[pcount] = pages[i];
1849 hfi1_release_user_pages(pshadow, pcount);
1850 clear_bit(bitidx, &uctxt->tidusemap[idx]);
1851 map &= ~(1ULL<<bitidx);
1854 trace_hfi1_exp_tid_map(uctxt->ctxt, subctxt_fp(fp), 1, uctxt->tidusemap,
1860 static void unlock_exp_tids(struct hfi1_ctxtdata *uctxt)
1862 struct hfi1_devdata *dd = uctxt->dd;
1865 dd_dev_info(dd, "ctxt %u unlocking any locked expTID pages\n",
1867 for (tid = 0; tid < uctxt->expected_count; tid++) {
1868 struct page *p = uctxt->tid_pg_list[tid];
1874 phys = uctxt->physshadow[tid];
1875 uctxt->physshadow[tid] = 0;
1876 uctxt->tid_pg_list[tid] = NULL;
1877 pci_unmap_page(dd->pcidev, phys, PAGE_SIZE, PCI_DMA_FROMDEVICE);
1878 hfi1_release_user_pages(&p, 1);
1882 static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned subctxt,
1885 int ret = -ENOENT, i, intable = 0;
1886 struct hfi1_pportdata *ppd = uctxt->ppd;
1887 struct hfi1_devdata *dd = uctxt->dd;
1889 if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY) {
1894 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++)
1895 if (pkey == ppd->pkeys[i]) {
1901 ret = hfi1_set_ctxt_pkey(dd, uctxt->ctxt, pkey);
1906 static int ui_open(struct inode *inode, struct file *filp)
1908 struct hfi1_devdata *dd;
1910 dd = container_of(inode->i_cdev, struct hfi1_devdata, ui_cdev);
1911 filp->private_data = dd; /* for other methods */
1915 static int ui_release(struct inode *inode, struct file *filp)
1921 static loff_t ui_lseek(struct file *filp, loff_t offset, int whence)
1923 struct hfi1_devdata *dd = filp->private_data;
1929 offset += filp->f_pos;
1932 offset = ((dd->kregend - dd->kregbase) + DC8051_DATA_MEM_SIZE) -
1942 if (offset >= (dd->kregend - dd->kregbase) + DC8051_DATA_MEM_SIZE)
1945 filp->f_pos = offset;
1951 /* NOTE: assumes unsigned long is 8 bytes */
1952 static ssize_t ui_read(struct file *filp, char __user *buf, size_t count,
1955 struct hfi1_devdata *dd = filp->private_data;
1956 void __iomem *base = dd->kregbase;
1957 unsigned long total, csr_off,
1958 barlen = (dd->kregend - dd->kregbase);
1961 /* only read 8 byte quantities */
1962 if ((count % 8) != 0)
1964 /* offset must be 8-byte aligned */
1965 if ((*f_pos % 8) != 0)
1967 /* destination buffer must be 8-byte aligned */
1968 if ((unsigned long)buf % 8 != 0)
1970 /* must be in range */
1971 if (*f_pos + count > (barlen + DC8051_DATA_MEM_SIZE))
1973 /* only set the base if we are not starting past the BAR */
1974 if (*f_pos < barlen)
1977 for (total = 0; total < count; total += 8, csr_off += 8) {
1978 /* accessing LCB CSRs requires more checks */
1979 if (is_lcb_offset(csr_off)) {
1980 if (read_lcb_csr(dd, csr_off, (u64 *)&data))
1984 * Cannot read ASIC GPIO/QSFP* clear and force CSRs without a
1985 * false parity error. Avoid the whole issue by not reading
1986 * them. These registers are defined as having a read value
1989 else if (csr_off == ASIC_GPIO_CLEAR
1990 || csr_off == ASIC_GPIO_FORCE
1991 || csr_off == ASIC_QSFP1_CLEAR
1992 || csr_off == ASIC_QSFP1_FORCE
1993 || csr_off == ASIC_QSFP2_CLEAR
1994 || csr_off == ASIC_QSFP2_FORCE)
1996 else if (csr_off >= barlen) {
1998 * read_8051_data can read more than just 8 bytes at
1999 * a time. However, folding this into the loop and
2000 * handling the reads in 8 byte increments allows us
2001 * to smoothly transition from chip memory to 8051
2004 if (read_8051_data(dd,
2005 (u32)(csr_off - barlen),
2006 sizeof(data), &data))
2009 data = readq(base + total);
2010 if (put_user(data, (unsigned long __user *)(buf + total)))
2017 /* NOTE: assumes unsigned long is 8 bytes */
2018 static ssize_t ui_write(struct file *filp, const char __user *buf,
2019 size_t count, loff_t *f_pos)
2021 struct hfi1_devdata *dd = filp->private_data;
2023 unsigned long total, data, csr_off;
2026 /* only write 8 byte quantities */
2027 if ((count % 8) != 0)
2029 /* offset must be 8-byte aligned */
2030 if ((*f_pos % 8) != 0)
2032 /* source buffer must be 8-byte aligned */
2033 if ((unsigned long)buf % 8 != 0)
2035 /* must be in range */
2036 if (*f_pos + count > dd->kregend - dd->kregbase)
2039 base = (void __iomem *)dd->kregbase + *f_pos;
2042 for (total = 0; total < count; total += 8, csr_off += 8) {
2043 if (get_user(data, (unsigned long __user *)(buf + total)))
2045 /* accessing LCB CSRs requires a special procedure */
2046 if (is_lcb_offset(csr_off)) {
2048 int ret = acquire_lcb_access(dd, 1);
2056 release_lcb_access(dd, 1);
2060 writeq(data, base + total);
2063 release_lcb_access(dd, 1);
2068 static const struct file_operations ui_file_ops = {
2069 .owner = THIS_MODULE,
2074 .release = ui_release,
2077 #define UI_OFFSET 192 /* device minor offset for UI devices */
2078 static int create_ui = 1;
2080 static struct cdev wildcard_cdev;
2081 static struct device *wildcard_device;
2083 static atomic_t user_count = ATOMIC_INIT(0);
2085 static void user_remove(struct hfi1_devdata *dd)
2087 if (atomic_dec_return(&user_count) == 0)
2088 hfi1_cdev_cleanup(&wildcard_cdev, &wildcard_device);
2090 hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device);
2091 hfi1_cdev_cleanup(&dd->ui_cdev, &dd->ui_device);
2094 static int user_add(struct hfi1_devdata *dd)
2099 if (atomic_inc_return(&user_count) == 1) {
2100 ret = hfi1_cdev_init(0, class_name(), &hfi1_file_ops,
2101 &wildcard_cdev, &wildcard_device,
2107 snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit);
2108 ret = hfi1_cdev_init(dd->unit + 1, name, &hfi1_file_ops,
2109 &dd->user_cdev, &dd->user_device,
2115 snprintf(name, sizeof(name),
2116 "%s_ui%d", class_name(), dd->unit);
2117 ret = hfi1_cdev_init(dd->unit + UI_OFFSET, name, &ui_file_ops,
2118 &dd->ui_cdev, &dd->ui_device,
2131 * Create per-unit files in /dev
2133 int hfi1_device_create(struct hfi1_devdata *dd)
2138 ret = hfi1_diag_add(dd);
2145 * Remove per-unit files in /dev
2146 * void, core kernel returns no errors for this stuff
2148 void hfi1_device_remove(struct hfi1_devdata *dd)
2151 hfi1_diag_remove(dd);