0001
0002
0003
0004
0005
0006
0007 #include <linux/poll.h>
0008 #include <linux/cdev.h>
0009 #include <linux/vmalloc.h>
0010 #include <linux/io.h>
0011 #include <linux/sched/mm.h>
0012 #include <linux/bitmap.h>
0013
0014 #include <rdma/ib.h>
0015
0016 #include "hfi.h"
0017 #include "pio.h"
0018 #include "device.h"
0019 #include "common.h"
0020 #include "trace.h"
0021 #include "mmu_rb.h"
0022 #include "user_sdma.h"
0023 #include "user_exp_rcv.h"
0024 #include "aspm.h"
0025
0026 #undef pr_fmt
0027 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
0028
0029 #define SEND_CTXT_HALT_TIMEOUT 1000
0030
0031
0032
0033
0034 static int hfi1_file_open(struct inode *inode, struct file *fp);
0035 static int hfi1_file_close(struct inode *inode, struct file *fp);
0036 static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from);
0037 static __poll_t hfi1_poll(struct file *fp, struct poll_table_struct *pt);
0038 static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma);
0039
0040 static u64 kvirt_to_phys(void *addr);
0041 static int assign_ctxt(struct hfi1_filedata *fd, unsigned long arg, u32 len);
0042 static void init_subctxts(struct hfi1_ctxtdata *uctxt,
0043 const struct hfi1_user_info *uinfo);
0044 static int init_user_ctxt(struct hfi1_filedata *fd,
0045 struct hfi1_ctxtdata *uctxt);
0046 static void user_init(struct hfi1_ctxtdata *uctxt);
0047 static int get_ctxt_info(struct hfi1_filedata *fd, unsigned long arg, u32 len);
0048 static int get_base_info(struct hfi1_filedata *fd, unsigned long arg, u32 len);
0049 static int user_exp_rcv_setup(struct hfi1_filedata *fd, unsigned long arg,
0050 u32 len);
0051 static int user_exp_rcv_clear(struct hfi1_filedata *fd, unsigned long arg,
0052 u32 len);
0053 static int user_exp_rcv_invalid(struct hfi1_filedata *fd, unsigned long arg,
0054 u32 len);
0055 static int setup_base_ctxt(struct hfi1_filedata *fd,
0056 struct hfi1_ctxtdata *uctxt);
0057 static int setup_subctxt(struct hfi1_ctxtdata *uctxt);
0058
0059 static int find_sub_ctxt(struct hfi1_filedata *fd,
0060 const struct hfi1_user_info *uinfo);
0061 static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
0062 struct hfi1_user_info *uinfo,
0063 struct hfi1_ctxtdata **cd);
0064 static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt);
0065 static __poll_t poll_urgent(struct file *fp, struct poll_table_struct *pt);
0066 static __poll_t poll_next(struct file *fp, struct poll_table_struct *pt);
0067 static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
0068 unsigned long arg);
0069 static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned long arg);
0070 static int ctxt_reset(struct hfi1_ctxtdata *uctxt);
0071 static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
0072 unsigned long arg);
0073 static vm_fault_t vma_fault(struct vm_fault *vmf);
0074 static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
0075 unsigned long arg);
0076
0077 static const struct file_operations hfi1_file_ops = {
0078 .owner = THIS_MODULE,
0079 .write_iter = hfi1_write_iter,
0080 .open = hfi1_file_open,
0081 .release = hfi1_file_close,
0082 .unlocked_ioctl = hfi1_file_ioctl,
0083 .poll = hfi1_poll,
0084 .mmap = hfi1_file_mmap,
0085 .llseek = noop_llseek,
0086 };
0087
0088 static const struct vm_operations_struct vm_ops = {
0089 .fault = vma_fault,
0090 };
0091
0092
0093
0094
0095 enum mmap_types {
0096 PIO_BUFS = 1,
0097 PIO_BUFS_SOP,
0098 PIO_CRED,
0099 RCV_HDRQ,
0100 RCV_EGRBUF,
0101 UREGS,
0102 EVENTS,
0103 STATUS,
0104 RTAIL,
0105 SUBCTXT_UREGS,
0106 SUBCTXT_RCV_HDRQ,
0107 SUBCTXT_EGRBUF,
0108 SDMA_COMP
0109 };
0110
0111
0112
0113
0114 #define HFI1_MMAP_OFFSET_MASK 0xfffULL
0115 #define HFI1_MMAP_OFFSET_SHIFT 0
0116 #define HFI1_MMAP_SUBCTXT_MASK 0xfULL
0117 #define HFI1_MMAP_SUBCTXT_SHIFT 12
0118 #define HFI1_MMAP_CTXT_MASK 0xffULL
0119 #define HFI1_MMAP_CTXT_SHIFT 16
0120 #define HFI1_MMAP_TYPE_MASK 0xfULL
0121 #define HFI1_MMAP_TYPE_SHIFT 24
0122 #define HFI1_MMAP_MAGIC_MASK 0xffffffffULL
0123 #define HFI1_MMAP_MAGIC_SHIFT 32
0124
0125 #define HFI1_MMAP_MAGIC 0xdabbad00
0126
0127 #define HFI1_MMAP_TOKEN_SET(field, val) \
0128 (((val) & HFI1_MMAP_##field##_MASK) << HFI1_MMAP_##field##_SHIFT)
0129 #define HFI1_MMAP_TOKEN_GET(field, token) \
0130 (((token) >> HFI1_MMAP_##field##_SHIFT) & HFI1_MMAP_##field##_MASK)
0131 #define HFI1_MMAP_TOKEN(type, ctxt, subctxt, addr) \
0132 (HFI1_MMAP_TOKEN_SET(MAGIC, HFI1_MMAP_MAGIC) | \
0133 HFI1_MMAP_TOKEN_SET(TYPE, type) | \
0134 HFI1_MMAP_TOKEN_SET(CTXT, ctxt) | \
0135 HFI1_MMAP_TOKEN_SET(SUBCTXT, subctxt) | \
0136 HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr))))
0137
0138 #define dbg(fmt, ...) \
0139 pr_info(fmt, ##__VA_ARGS__)
0140
0141 static inline int is_valid_mmap(u64 token)
0142 {
0143 return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC);
0144 }
0145
0146 static int hfi1_file_open(struct inode *inode, struct file *fp)
0147 {
0148 struct hfi1_filedata *fd;
0149 struct hfi1_devdata *dd = container_of(inode->i_cdev,
0150 struct hfi1_devdata,
0151 user_cdev);
0152
0153 if (!((dd->flags & HFI1_PRESENT) && dd->kregbase1))
0154 return -EINVAL;
0155
0156 if (!refcount_inc_not_zero(&dd->user_refcount))
0157 return -ENXIO;
0158
0159
0160
0161 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
0162
0163 if (!fd || init_srcu_struct(&fd->pq_srcu))
0164 goto nomem;
0165 spin_lock_init(&fd->pq_rcu_lock);
0166 spin_lock_init(&fd->tid_lock);
0167 spin_lock_init(&fd->invalid_lock);
0168 fd->rec_cpu_num = -1;
0169 fd->dd = dd;
0170 fp->private_data = fd;
0171 return 0;
0172 nomem:
0173 kfree(fd);
0174 fp->private_data = NULL;
0175 if (refcount_dec_and_test(&dd->user_refcount))
0176 complete(&dd->user_comp);
0177 return -ENOMEM;
0178 }
0179
0180 static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
0181 unsigned long arg)
0182 {
0183 struct hfi1_filedata *fd = fp->private_data;
0184 struct hfi1_ctxtdata *uctxt = fd->uctxt;
0185 int ret = 0;
0186 int uval = 0;
0187
0188 hfi1_cdbg(IOCTL, "IOCTL recv: 0x%x", cmd);
0189 if (cmd != HFI1_IOCTL_ASSIGN_CTXT &&
0190 cmd != HFI1_IOCTL_GET_VERS &&
0191 !uctxt)
0192 return -EINVAL;
0193
0194 switch (cmd) {
0195 case HFI1_IOCTL_ASSIGN_CTXT:
0196 ret = assign_ctxt(fd, arg, _IOC_SIZE(cmd));
0197 break;
0198
0199 case HFI1_IOCTL_CTXT_INFO:
0200 ret = get_ctxt_info(fd, arg, _IOC_SIZE(cmd));
0201 break;
0202
0203 case HFI1_IOCTL_USER_INFO:
0204 ret = get_base_info(fd, arg, _IOC_SIZE(cmd));
0205 break;
0206
0207 case HFI1_IOCTL_CREDIT_UPD:
0208 if (uctxt)
0209 sc_return_credits(uctxt->sc);
0210 break;
0211
0212 case HFI1_IOCTL_TID_UPDATE:
0213 ret = user_exp_rcv_setup(fd, arg, _IOC_SIZE(cmd));
0214 break;
0215
0216 case HFI1_IOCTL_TID_FREE:
0217 ret = user_exp_rcv_clear(fd, arg, _IOC_SIZE(cmd));
0218 break;
0219
0220 case HFI1_IOCTL_TID_INVAL_READ:
0221 ret = user_exp_rcv_invalid(fd, arg, _IOC_SIZE(cmd));
0222 break;
0223
0224 case HFI1_IOCTL_RECV_CTRL:
0225 ret = manage_rcvq(uctxt, fd->subctxt, arg);
0226 break;
0227
0228 case HFI1_IOCTL_POLL_TYPE:
0229 if (get_user(uval, (int __user *)arg))
0230 return -EFAULT;
0231 uctxt->poll_type = (typeof(uctxt->poll_type))uval;
0232 break;
0233
0234 case HFI1_IOCTL_ACK_EVENT:
0235 ret = user_event_ack(uctxt, fd->subctxt, arg);
0236 break;
0237
0238 case HFI1_IOCTL_SET_PKEY:
0239 ret = set_ctxt_pkey(uctxt, arg);
0240 break;
0241
0242 case HFI1_IOCTL_CTXT_RESET:
0243 ret = ctxt_reset(uctxt);
0244 break;
0245
0246 case HFI1_IOCTL_GET_VERS:
0247 uval = HFI1_USER_SWVERSION;
0248 if (put_user(uval, (int __user *)arg))
0249 return -EFAULT;
0250 break;
0251
0252 default:
0253 return -EINVAL;
0254 }
0255
0256 return ret;
0257 }
0258
0259 static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from)
0260 {
0261 struct hfi1_filedata *fd = kiocb->ki_filp->private_data;
0262 struct hfi1_user_sdma_pkt_q *pq;
0263 struct hfi1_user_sdma_comp_q *cq = fd->cq;
0264 int done = 0, reqs = 0;
0265 unsigned long dim = from->nr_segs;
0266 int idx;
0267
0268 if (!HFI1_CAP_IS_KSET(SDMA))
0269 return -EINVAL;
0270 idx = srcu_read_lock(&fd->pq_srcu);
0271 pq = srcu_dereference(fd->pq, &fd->pq_srcu);
0272 if (!cq || !pq) {
0273 srcu_read_unlock(&fd->pq_srcu, idx);
0274 return -EIO;
0275 }
0276
0277 if (!iter_is_iovec(from) || !dim) {
0278 srcu_read_unlock(&fd->pq_srcu, idx);
0279 return -EINVAL;
0280 }
0281
0282 trace_hfi1_sdma_request(fd->dd, fd->uctxt->ctxt, fd->subctxt, dim);
0283
0284 if (atomic_read(&pq->n_reqs) == pq->n_max_reqs) {
0285 srcu_read_unlock(&fd->pq_srcu, idx);
0286 return -ENOSPC;
0287 }
0288
0289 while (dim) {
0290 int ret;
0291 unsigned long count = 0;
0292
0293 ret = hfi1_user_sdma_process_request(
0294 fd, (struct iovec *)(from->iov + done),
0295 dim, &count);
0296 if (ret) {
0297 reqs = ret;
0298 break;
0299 }
0300 dim -= count;
0301 done += count;
0302 reqs++;
0303 }
0304
0305 srcu_read_unlock(&fd->pq_srcu, idx);
0306 return reqs;
0307 }
0308
0309 static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma)
0310 {
0311 struct hfi1_filedata *fd = fp->private_data;
0312 struct hfi1_ctxtdata *uctxt = fd->uctxt;
0313 struct hfi1_devdata *dd;
0314 unsigned long flags;
0315 u64 token = vma->vm_pgoff << PAGE_SHIFT,
0316 memaddr = 0;
0317 void *memvirt = NULL;
0318 u8 subctxt, mapio = 0, vmf = 0, type;
0319 ssize_t memlen = 0;
0320 int ret = 0;
0321 u16 ctxt;
0322
0323 if (!is_valid_mmap(token) || !uctxt ||
0324 !(vma->vm_flags & VM_SHARED)) {
0325 ret = -EINVAL;
0326 goto done;
0327 }
0328 dd = uctxt->dd;
0329 ctxt = HFI1_MMAP_TOKEN_GET(CTXT, token);
0330 subctxt = HFI1_MMAP_TOKEN_GET(SUBCTXT, token);
0331 type = HFI1_MMAP_TOKEN_GET(TYPE, token);
0332 if (ctxt != uctxt->ctxt || subctxt != fd->subctxt) {
0333 ret = -EINVAL;
0334 goto done;
0335 }
0336
0337 flags = vma->vm_flags;
0338
0339 switch (type) {
0340 case PIO_BUFS:
0341 case PIO_BUFS_SOP:
0342 memaddr = ((dd->physaddr + TXE_PIO_SEND) +
0343
0344 (uctxt->sc->hw_context * BIT(16))) +
0345
0346 (type == PIO_BUFS_SOP ?
0347 (TXE_PIO_SIZE / 2) : 0);
0348
0349
0350
0351
0352 memlen = PAGE_ALIGN(uctxt->sc->credits * PIO_BLOCK_SIZE);
0353 flags &= ~VM_MAYREAD;
0354 flags |= VM_DONTCOPY | VM_DONTEXPAND;
0355 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
0356 mapio = 1;
0357 break;
0358 case PIO_CRED:
0359 if (flags & VM_WRITE) {
0360 ret = -EPERM;
0361 goto done;
0362 }
0363
0364
0365
0366
0367
0368 memvirt = dd->cr_base[uctxt->numa_id].va;
0369 memaddr = virt_to_phys(memvirt) +
0370 (((u64)uctxt->sc->hw_free -
0371 (u64)dd->cr_base[uctxt->numa_id].va) & PAGE_MASK);
0372 memlen = PAGE_SIZE;
0373 flags &= ~VM_MAYWRITE;
0374 flags |= VM_DONTCOPY | VM_DONTEXPAND;
0375
0376
0377
0378
0379
0380
0381 mapio = 1;
0382 break;
0383 case RCV_HDRQ:
0384 memlen = rcvhdrq_size(uctxt);
0385 memvirt = uctxt->rcvhdrq;
0386 break;
0387 case RCV_EGRBUF: {
0388 unsigned long addr;
0389 int i;
0390
0391
0392
0393
0394
0395 memlen = uctxt->egrbufs.size;
0396 if ((vma->vm_end - vma->vm_start) != memlen) {
0397 dd_dev_err(dd, "Eager buffer map size invalid (%lu != %lu)\n",
0398 (vma->vm_end - vma->vm_start), memlen);
0399 ret = -EINVAL;
0400 goto done;
0401 }
0402 if (vma->vm_flags & VM_WRITE) {
0403 ret = -EPERM;
0404 goto done;
0405 }
0406 vma->vm_flags &= ~VM_MAYWRITE;
0407 addr = vma->vm_start;
0408 for (i = 0 ; i < uctxt->egrbufs.numbufs; i++) {
0409 memlen = uctxt->egrbufs.buffers[i].len;
0410 memvirt = uctxt->egrbufs.buffers[i].addr;
0411 ret = remap_pfn_range(
0412 vma, addr,
0413
0414
0415
0416
0417
0418 PFN_DOWN(__pa(memvirt)),
0419 memlen,
0420 vma->vm_page_prot);
0421 if (ret < 0)
0422 goto done;
0423 addr += memlen;
0424 }
0425 ret = 0;
0426 goto done;
0427 }
0428 case UREGS:
0429
0430
0431
0432
0433 memaddr = (unsigned long)
0434 (dd->physaddr + RXE_PER_CONTEXT_USER)
0435 + (uctxt->ctxt * RXE_PER_CONTEXT_SIZE);
0436
0437
0438
0439
0440 memlen = PAGE_SIZE;
0441 flags |= VM_DONTCOPY | VM_DONTEXPAND;
0442 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
0443 mapio = 1;
0444 break;
0445 case EVENTS:
0446
0447
0448
0449
0450 memaddr = (unsigned long)
0451 (dd->events + uctxt_offset(uctxt)) & PAGE_MASK;
0452 memlen = PAGE_SIZE;
0453
0454
0455
0456
0457 flags |= VM_IO | VM_DONTEXPAND;
0458 vmf = 1;
0459 break;
0460 case STATUS:
0461 if (flags & VM_WRITE) {
0462 ret = -EPERM;
0463 goto done;
0464 }
0465 memaddr = kvirt_to_phys((void *)dd->status);
0466 memlen = PAGE_SIZE;
0467 flags |= VM_IO | VM_DONTEXPAND;
0468 break;
0469 case RTAIL:
0470 if (!HFI1_CAP_IS_USET(DMA_RTAIL)) {
0471
0472
0473
0474
0475 ret = -EINVAL;
0476 goto done;
0477 }
0478 if ((flags & VM_WRITE) || !hfi1_rcvhdrtail_kvaddr(uctxt)) {
0479 ret = -EPERM;
0480 goto done;
0481 }
0482 memlen = PAGE_SIZE;
0483 memvirt = (void *)hfi1_rcvhdrtail_kvaddr(uctxt);
0484 flags &= ~VM_MAYWRITE;
0485 break;
0486 case SUBCTXT_UREGS:
0487 memaddr = (u64)uctxt->subctxt_uregbase;
0488 memlen = PAGE_SIZE;
0489 flags |= VM_IO | VM_DONTEXPAND;
0490 vmf = 1;
0491 break;
0492 case SUBCTXT_RCV_HDRQ:
0493 memaddr = (u64)uctxt->subctxt_rcvhdr_base;
0494 memlen = rcvhdrq_size(uctxt) * uctxt->subctxt_cnt;
0495 flags |= VM_IO | VM_DONTEXPAND;
0496 vmf = 1;
0497 break;
0498 case SUBCTXT_EGRBUF:
0499 memaddr = (u64)uctxt->subctxt_rcvegrbuf;
0500 memlen = uctxt->egrbufs.size * uctxt->subctxt_cnt;
0501 flags |= VM_IO | VM_DONTEXPAND;
0502 flags &= ~VM_MAYWRITE;
0503 vmf = 1;
0504 break;
0505 case SDMA_COMP: {
0506 struct hfi1_user_sdma_comp_q *cq = fd->cq;
0507
0508 if (!cq) {
0509 ret = -EFAULT;
0510 goto done;
0511 }
0512 memaddr = (u64)cq->comps;
0513 memlen = PAGE_ALIGN(sizeof(*cq->comps) * cq->nentries);
0514 flags |= VM_IO | VM_DONTEXPAND;
0515 vmf = 1;
0516 break;
0517 }
0518 default:
0519 ret = -EINVAL;
0520 break;
0521 }
0522
0523 if ((vma->vm_end - vma->vm_start) != memlen) {
0524 hfi1_cdbg(PROC, "%u:%u Memory size mismatch %lu:%lu",
0525 uctxt->ctxt, fd->subctxt,
0526 (vma->vm_end - vma->vm_start), memlen);
0527 ret = -EINVAL;
0528 goto done;
0529 }
0530
0531 vma->vm_flags = flags;
0532 hfi1_cdbg(PROC,
0533 "%u:%u type:%u io/vf:%d/%d, addr:0x%llx, len:%lu(%lu), flags:0x%lx\n",
0534 ctxt, subctxt, type, mapio, vmf, memaddr, memlen,
0535 vma->vm_end - vma->vm_start, vma->vm_flags);
0536 if (vmf) {
0537 vma->vm_pgoff = PFN_DOWN(memaddr);
0538 vma->vm_ops = &vm_ops;
0539 ret = 0;
0540 } else if (mapio) {
0541 ret = io_remap_pfn_range(vma, vma->vm_start,
0542 PFN_DOWN(memaddr),
0543 memlen,
0544 vma->vm_page_prot);
0545 } else if (memvirt) {
0546 ret = remap_pfn_range(vma, vma->vm_start,
0547 PFN_DOWN(__pa(memvirt)),
0548 memlen,
0549 vma->vm_page_prot);
0550 } else {
0551 ret = remap_pfn_range(vma, vma->vm_start,
0552 PFN_DOWN(memaddr),
0553 memlen,
0554 vma->vm_page_prot);
0555 }
0556 done:
0557 return ret;
0558 }
0559
0560
0561
0562
0563
0564 static vm_fault_t vma_fault(struct vm_fault *vmf)
0565 {
0566 struct page *page;
0567
0568 page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
0569 if (!page)
0570 return VM_FAULT_SIGBUS;
0571
0572 get_page(page);
0573 vmf->page = page;
0574
0575 return 0;
0576 }
0577
0578 static __poll_t hfi1_poll(struct file *fp, struct poll_table_struct *pt)
0579 {
0580 struct hfi1_ctxtdata *uctxt;
0581 __poll_t pollflag;
0582
0583 uctxt = ((struct hfi1_filedata *)fp->private_data)->uctxt;
0584 if (!uctxt)
0585 pollflag = EPOLLERR;
0586 else if (uctxt->poll_type == HFI1_POLL_TYPE_URGENT)
0587 pollflag = poll_urgent(fp, pt);
0588 else if (uctxt->poll_type == HFI1_POLL_TYPE_ANYRCV)
0589 pollflag = poll_next(fp, pt);
0590 else
0591 pollflag = EPOLLERR;
0592
0593 return pollflag;
0594 }
0595
0596 static int hfi1_file_close(struct inode *inode, struct file *fp)
0597 {
0598 struct hfi1_filedata *fdata = fp->private_data;
0599 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
0600 struct hfi1_devdata *dd = container_of(inode->i_cdev,
0601 struct hfi1_devdata,
0602 user_cdev);
0603 unsigned long flags, *ev;
0604
0605 fp->private_data = NULL;
0606
0607 if (!uctxt)
0608 goto done;
0609
0610 hfi1_cdbg(PROC, "closing ctxt %u:%u", uctxt->ctxt, fdata->subctxt);
0611
0612 flush_wc();
0613
0614 hfi1_user_sdma_free_queues(fdata, uctxt);
0615
0616
0617 hfi1_put_proc_affinity(fdata->rec_cpu_num);
0618
0619
0620 hfi1_user_exp_rcv_free(fdata);
0621
0622
0623
0624
0625
0626 fdata->uctxt = NULL;
0627 hfi1_rcd_put(uctxt);
0628
0629
0630
0631
0632
0633 ev = dd->events + uctxt_offset(uctxt) + fdata->subctxt;
0634 *ev = 0;
0635
0636 spin_lock_irqsave(&dd->uctxt_lock, flags);
0637 __clear_bit(fdata->subctxt, uctxt->in_use_ctxts);
0638 if (!bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
0639 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
0640 goto done;
0641 }
0642 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
0643
0644
0645
0646
0647
0648 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
0649 HFI1_RCVCTRL_TIDFLOW_DIS |
0650 HFI1_RCVCTRL_INTRAVAIL_DIS |
0651 HFI1_RCVCTRL_TAILUPD_DIS |
0652 HFI1_RCVCTRL_ONE_PKT_EGR_DIS |
0653 HFI1_RCVCTRL_NO_RHQ_DROP_DIS |
0654 HFI1_RCVCTRL_NO_EGR_DROP_DIS |
0655 HFI1_RCVCTRL_URGENT_DIS, uctxt);
0656
0657 hfi1_clear_ctxt_jkey(dd, uctxt);
0658
0659
0660
0661
0662 if (uctxt->sc) {
0663 sc_disable(uctxt->sc);
0664 set_pio_integrity(uctxt->sc);
0665 }
0666
0667 hfi1_free_ctxt_rcv_groups(uctxt);
0668 hfi1_clear_ctxt_pkey(dd, uctxt);
0669
0670 uctxt->event_flags = 0;
0671
0672 deallocate_ctxt(uctxt);
0673 done:
0674
0675 if (refcount_dec_and_test(&dd->user_refcount))
0676 complete(&dd->user_comp);
0677
0678 cleanup_srcu_struct(&fdata->pq_srcu);
0679 kfree(fdata);
0680 return 0;
0681 }
0682
0683
0684
0685
0686
0687 static u64 kvirt_to_phys(void *addr)
0688 {
0689 struct page *page;
0690 u64 paddr = 0;
0691
0692 page = vmalloc_to_page(addr);
0693 if (page)
0694 paddr = page_to_pfn(page) << PAGE_SHIFT;
0695
0696 return paddr;
0697 }
0698
0699
0700
0701
0702
0703
0704
0705
0706
0707
0708
0709
0710
0711 static int complete_subctxt(struct hfi1_filedata *fd)
0712 {
0713 int ret;
0714 unsigned long flags;
0715
0716
0717
0718
0719
0720 ret = wait_event_interruptible(
0721 fd->uctxt->wait,
0722 !test_bit(HFI1_CTXT_BASE_UNINIT, &fd->uctxt->event_flags));
0723
0724 if (test_bit(HFI1_CTXT_BASE_FAILED, &fd->uctxt->event_flags))
0725 ret = -ENOMEM;
0726
0727
0728 if (!ret) {
0729 fd->rec_cpu_num = hfi1_get_proc_affinity(fd->uctxt->numa_id);
0730 ret = init_user_ctxt(fd, fd->uctxt);
0731 }
0732
0733 if (ret) {
0734 spin_lock_irqsave(&fd->dd->uctxt_lock, flags);
0735 __clear_bit(fd->subctxt, fd->uctxt->in_use_ctxts);
0736 spin_unlock_irqrestore(&fd->dd->uctxt_lock, flags);
0737 hfi1_rcd_put(fd->uctxt);
0738 fd->uctxt = NULL;
0739 }
0740
0741 return ret;
0742 }
0743
0744 static int assign_ctxt(struct hfi1_filedata *fd, unsigned long arg, u32 len)
0745 {
0746 int ret;
0747 unsigned int swmajor;
0748 struct hfi1_ctxtdata *uctxt = NULL;
0749 struct hfi1_user_info uinfo;
0750
0751 if (fd->uctxt)
0752 return -EINVAL;
0753
0754 if (sizeof(uinfo) != len)
0755 return -EINVAL;
0756
0757 if (copy_from_user(&uinfo, (void __user *)arg, sizeof(uinfo)))
0758 return -EFAULT;
0759
0760 swmajor = uinfo.userversion >> 16;
0761 if (swmajor != HFI1_USER_SWMAJOR)
0762 return -ENODEV;
0763
0764 if (uinfo.subctxt_cnt > HFI1_MAX_SHARED_CTXTS)
0765 return -EINVAL;
0766
0767
0768
0769
0770
0771 mutex_lock(&hfi1_mutex);
0772
0773
0774
0775
0776 ret = find_sub_ctxt(fd, &uinfo);
0777
0778
0779
0780
0781
0782 if (!ret)
0783 ret = allocate_ctxt(fd, fd->dd, &uinfo, &uctxt);
0784
0785 mutex_unlock(&hfi1_mutex);
0786
0787
0788 switch (ret) {
0789 case 0:
0790 ret = setup_base_ctxt(fd, uctxt);
0791 if (ret)
0792 deallocate_ctxt(uctxt);
0793 break;
0794 case 1:
0795 ret = complete_subctxt(fd);
0796 break;
0797 default:
0798 break;
0799 }
0800
0801 return ret;
0802 }
0803
0804
0805
0806
0807
0808
0809
0810
0811
0812
0813 static int match_ctxt(struct hfi1_filedata *fd,
0814 const struct hfi1_user_info *uinfo,
0815 struct hfi1_ctxtdata *uctxt)
0816 {
0817 struct hfi1_devdata *dd = fd->dd;
0818 unsigned long flags;
0819 u16 subctxt;
0820
0821
0822 if (uctxt->sc && (uctxt->sc->type == SC_KERNEL))
0823 return 0;
0824
0825
0826 if (memcmp(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid)) ||
0827 uctxt->jkey != generate_jkey(current_uid()) ||
0828 uctxt->subctxt_id != uinfo->subctxt_id ||
0829 uctxt->subctxt_cnt != uinfo->subctxt_cnt)
0830 return 0;
0831
0832
0833 if (uctxt->userversion != uinfo->userversion)
0834 return -EINVAL;
0835
0836
0837 spin_lock_irqsave(&dd->uctxt_lock, flags);
0838 if (bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
0839
0840 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
0841 return 0;
0842 }
0843
0844 subctxt = find_first_zero_bit(uctxt->in_use_ctxts,
0845 HFI1_MAX_SHARED_CTXTS);
0846 if (subctxt >= uctxt->subctxt_cnt) {
0847 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
0848 return -EBUSY;
0849 }
0850
0851 fd->subctxt = subctxt;
0852 __set_bit(fd->subctxt, uctxt->in_use_ctxts);
0853 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
0854
0855 fd->uctxt = uctxt;
0856 hfi1_rcd_get(uctxt);
0857
0858 return 1;
0859 }
0860
0861
0862
0863
0864
0865
0866
0867
0868
0869
0870
0871
0872
0873
0874
0875 static int find_sub_ctxt(struct hfi1_filedata *fd,
0876 const struct hfi1_user_info *uinfo)
0877 {
0878 struct hfi1_ctxtdata *uctxt;
0879 struct hfi1_devdata *dd = fd->dd;
0880 u16 i;
0881 int ret;
0882
0883 if (!uinfo->subctxt_cnt)
0884 return 0;
0885
0886 for (i = dd->first_dyn_alloc_ctxt; i < dd->num_rcv_contexts; i++) {
0887 uctxt = hfi1_rcd_get_by_index(dd, i);
0888 if (uctxt) {
0889 ret = match_ctxt(fd, uinfo, uctxt);
0890 hfi1_rcd_put(uctxt);
0891
0892 if (ret)
0893 return ret;
0894 }
0895 }
0896
0897 return 0;
0898 }
0899
0900 static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
0901 struct hfi1_user_info *uinfo,
0902 struct hfi1_ctxtdata **rcd)
0903 {
0904 struct hfi1_ctxtdata *uctxt;
0905 int ret, numa;
0906
0907 if (dd->flags & HFI1_FROZEN) {
0908
0909
0910
0911
0912
0913
0914
0915 return -EIO;
0916 }
0917
0918 if (!dd->freectxts)
0919 return -EBUSY;
0920
0921
0922
0923
0924
0925 fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node);
0926 if (fd->rec_cpu_num != -1)
0927 numa = cpu_to_node(fd->rec_cpu_num);
0928 else
0929 numa = numa_node_id();
0930 ret = hfi1_create_ctxtdata(dd->pport, numa, &uctxt);
0931 if (ret < 0) {
0932 dd_dev_err(dd, "user ctxtdata allocation failed\n");
0933 return ret;
0934 }
0935 hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)",
0936 uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num,
0937 uctxt->numa_id);
0938
0939
0940
0941
0942 uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize, dd->node);
0943 if (!uctxt->sc) {
0944 ret = -ENOMEM;
0945 goto ctxdata_free;
0946 }
0947 hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index,
0948 uctxt->sc->hw_context);
0949 ret = sc_enable(uctxt->sc);
0950 if (ret)
0951 goto ctxdata_free;
0952
0953
0954
0955
0956
0957
0958
0959
0960
0961
0962 __set_bit(0, uctxt->in_use_ctxts);
0963 if (uinfo->subctxt_cnt)
0964 init_subctxts(uctxt, uinfo);
0965 uctxt->userversion = uinfo->userversion;
0966 uctxt->flags = hfi1_cap_mask;
0967 init_waitqueue_head(&uctxt->wait);
0968 strlcpy(uctxt->comm, current->comm, sizeof(uctxt->comm));
0969 memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid));
0970 uctxt->jkey = generate_jkey(current_uid());
0971 hfi1_stats.sps_ctxts++;
0972
0973
0974
0975
0976 if (dd->freectxts-- == dd->num_user_contexts)
0977 aspm_disable_all(dd);
0978
0979 *rcd = uctxt;
0980
0981 return 0;
0982
0983 ctxdata_free:
0984 hfi1_free_ctxt(uctxt);
0985 return ret;
0986 }
0987
0988 static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt)
0989 {
0990 mutex_lock(&hfi1_mutex);
0991 hfi1_stats.sps_ctxts--;
0992 if (++uctxt->dd->freectxts == uctxt->dd->num_user_contexts)
0993 aspm_enable_all(uctxt->dd);
0994 mutex_unlock(&hfi1_mutex);
0995
0996 hfi1_free_ctxt(uctxt);
0997 }
0998
0999 static void init_subctxts(struct hfi1_ctxtdata *uctxt,
1000 const struct hfi1_user_info *uinfo)
1001 {
1002 uctxt->subctxt_cnt = uinfo->subctxt_cnt;
1003 uctxt->subctxt_id = uinfo->subctxt_id;
1004 set_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
1005 }
1006
1007 static int setup_subctxt(struct hfi1_ctxtdata *uctxt)
1008 {
1009 int ret = 0;
1010 u16 num_subctxts = uctxt->subctxt_cnt;
1011
1012 uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE);
1013 if (!uctxt->subctxt_uregbase)
1014 return -ENOMEM;
1015
1016
1017 uctxt->subctxt_rcvhdr_base = vmalloc_user(rcvhdrq_size(uctxt) *
1018 num_subctxts);
1019 if (!uctxt->subctxt_rcvhdr_base) {
1020 ret = -ENOMEM;
1021 goto bail_ureg;
1022 }
1023
1024 uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size *
1025 num_subctxts);
1026 if (!uctxt->subctxt_rcvegrbuf) {
1027 ret = -ENOMEM;
1028 goto bail_rhdr;
1029 }
1030
1031 return 0;
1032
1033 bail_rhdr:
1034 vfree(uctxt->subctxt_rcvhdr_base);
1035 uctxt->subctxt_rcvhdr_base = NULL;
1036 bail_ureg:
1037 vfree(uctxt->subctxt_uregbase);
1038 uctxt->subctxt_uregbase = NULL;
1039
1040 return ret;
1041 }
1042
1043 static void user_init(struct hfi1_ctxtdata *uctxt)
1044 {
1045 unsigned int rcvctrl_ops = 0;
1046
1047
1048 uctxt->urgent = 0;
1049 uctxt->urgent_poll = 0;
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062 if (hfi1_rcvhdrtail_kvaddr(uctxt))
1063 clear_rcvhdrtail(uctxt);
1064
1065
1066 hfi1_set_ctxt_jkey(uctxt->dd, uctxt, uctxt->jkey);
1067
1068 rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB;
1069 rcvctrl_ops |= HFI1_RCVCTRL_URGENT_ENB;
1070 if (HFI1_CAP_UGET_MASK(uctxt->flags, HDRSUPP))
1071 rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB;
1072
1073
1074
1075
1076
1077 if (!HFI1_CAP_UGET_MASK(uctxt->flags, MULTI_PKT_EGR))
1078 rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
1079 if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_EGR_FULL))
1080 rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
1081 if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_RHQ_FULL))
1082 rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
1083
1084
1085
1086
1087
1088
1089 if (HFI1_CAP_UGET_MASK(uctxt->flags, DMA_RTAIL))
1090 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB;
1091 else
1092 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS;
1093 hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt);
1094 }
1095
1096 static int get_ctxt_info(struct hfi1_filedata *fd, unsigned long arg, u32 len)
1097 {
1098 struct hfi1_ctxt_info cinfo;
1099 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1100
1101 if (sizeof(cinfo) != len)
1102 return -EINVAL;
1103
1104 memset(&cinfo, 0, sizeof(cinfo));
1105 cinfo.runtime_flags = (((uctxt->flags >> HFI1_CAP_MISC_SHIFT) &
1106 HFI1_CAP_MISC_MASK) << HFI1_CAP_USER_SHIFT) |
1107 HFI1_CAP_UGET_MASK(uctxt->flags, MASK) |
1108 HFI1_CAP_KGET_MASK(uctxt->flags, K2U);
1109
1110 if (!fd->use_mn)
1111 cinfo.runtime_flags |= HFI1_CAP_TID_UNMAP;
1112
1113 cinfo.num_active = hfi1_count_active_units();
1114 cinfo.unit = uctxt->dd->unit;
1115 cinfo.ctxt = uctxt->ctxt;
1116 cinfo.subctxt = fd->subctxt;
1117 cinfo.rcvtids = roundup(uctxt->egrbufs.alloced,
1118 uctxt->dd->rcv_entries.group_size) +
1119 uctxt->expected_count;
1120 cinfo.credits = uctxt->sc->credits;
1121 cinfo.numa_node = uctxt->numa_id;
1122 cinfo.rec_cpu = fd->rec_cpu_num;
1123 cinfo.send_ctxt = uctxt->sc->hw_context;
1124
1125 cinfo.egrtids = uctxt->egrbufs.alloced;
1126 cinfo.rcvhdrq_cnt = get_hdrq_cnt(uctxt);
1127 cinfo.rcvhdrq_entsize = get_hdrqentsize(uctxt) << 2;
1128 cinfo.sdma_ring_size = fd->cq->nentries;
1129 cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size;
1130
1131 trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, &cinfo);
1132 if (copy_to_user((void __user *)arg, &cinfo, len))
1133 return -EFAULT;
1134
1135 return 0;
1136 }
1137
1138 static int init_user_ctxt(struct hfi1_filedata *fd,
1139 struct hfi1_ctxtdata *uctxt)
1140 {
1141 int ret;
1142
1143 ret = hfi1_user_sdma_alloc_queues(uctxt, fd);
1144 if (ret)
1145 return ret;
1146
1147 ret = hfi1_user_exp_rcv_init(fd, uctxt);
1148 if (ret)
1149 hfi1_user_sdma_free_queues(fd, uctxt);
1150
1151 return ret;
1152 }
1153
1154 static int setup_base_ctxt(struct hfi1_filedata *fd,
1155 struct hfi1_ctxtdata *uctxt)
1156 {
1157 struct hfi1_devdata *dd = uctxt->dd;
1158 int ret = 0;
1159
1160 hfi1_init_ctxt(uctxt->sc);
1161
1162
1163 ret = hfi1_create_rcvhdrq(dd, uctxt);
1164 if (ret)
1165 goto done;
1166
1167 ret = hfi1_setup_eagerbufs(uctxt);
1168 if (ret)
1169 goto done;
1170
1171
1172 if (uctxt->subctxt_cnt)
1173 ret = setup_subctxt(uctxt);
1174 if (ret)
1175 goto done;
1176
1177 ret = hfi1_alloc_ctxt_rcv_groups(uctxt);
1178 if (ret)
1179 goto done;
1180
1181 ret = init_user_ctxt(fd, uctxt);
1182 if (ret) {
1183 hfi1_free_ctxt_rcv_groups(uctxt);
1184 goto done;
1185 }
1186
1187 user_init(uctxt);
1188
1189
1190 fd->uctxt = uctxt;
1191 hfi1_rcd_get(uctxt);
1192
1193 done:
1194 if (uctxt->subctxt_cnt) {
1195
1196
1197
1198
1199 if (ret)
1200 set_bit(HFI1_CTXT_BASE_FAILED, &uctxt->event_flags);
1201
1202
1203
1204
1205
1206 clear_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
1207 wake_up(&uctxt->wait);
1208 }
1209
1210 return ret;
1211 }
1212
1213 static int get_base_info(struct hfi1_filedata *fd, unsigned long arg, u32 len)
1214 {
1215 struct hfi1_base_info binfo;
1216 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1217 struct hfi1_devdata *dd = uctxt->dd;
1218 unsigned offset;
1219
1220 trace_hfi1_uctxtdata(uctxt->dd, uctxt, fd->subctxt);
1221
1222 if (sizeof(binfo) != len)
1223 return -EINVAL;
1224
1225 memset(&binfo, 0, sizeof(binfo));
1226 binfo.hw_version = dd->revision;
1227 binfo.sw_version = HFI1_USER_SWVERSION;
1228 binfo.bthqp = RVT_KDETH_QP_PREFIX;
1229 binfo.jkey = uctxt->jkey;
1230
1231
1232
1233
1234
1235
1236 offset = ((u64)uctxt->sc->hw_free -
1237 (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE;
1238 binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt,
1239 fd->subctxt, offset);
1240 binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt,
1241 fd->subctxt,
1242 uctxt->sc->base_addr);
1243 binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP,
1244 uctxt->ctxt,
1245 fd->subctxt,
1246 uctxt->sc->base_addr);
1247 binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt,
1248 fd->subctxt,
1249 uctxt->rcvhdrq);
1250 binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt,
1251 fd->subctxt,
1252 uctxt->egrbufs.rcvtids[0].dma);
1253 binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt,
1254 fd->subctxt, 0);
1255
1256
1257
1258
1259 binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt,
1260 fd->subctxt, 0);
1261 offset = offset_in_page((uctxt_offset(uctxt) + fd->subctxt) *
1262 sizeof(*dd->events));
1263 binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt,
1264 fd->subctxt,
1265 offset);
1266 binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt,
1267 fd->subctxt,
1268 dd->status);
1269 if (HFI1_CAP_IS_USET(DMA_RTAIL))
1270 binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt,
1271 fd->subctxt, 0);
1272 if (uctxt->subctxt_cnt) {
1273 binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS,
1274 uctxt->ctxt,
1275 fd->subctxt, 0);
1276 binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ,
1277 uctxt->ctxt,
1278 fd->subctxt, 0);
1279 binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF,
1280 uctxt->ctxt,
1281 fd->subctxt, 0);
1282 }
1283
1284 if (copy_to_user((void __user *)arg, &binfo, len))
1285 return -EFAULT;
1286
1287 return 0;
1288 }
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299 static int user_exp_rcv_setup(struct hfi1_filedata *fd, unsigned long arg,
1300 u32 len)
1301 {
1302 int ret;
1303 unsigned long addr;
1304 struct hfi1_tid_info tinfo;
1305
1306 if (sizeof(tinfo) != len)
1307 return -EINVAL;
1308
1309 if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo))))
1310 return -EFAULT;
1311
1312 ret = hfi1_user_exp_rcv_setup(fd, &tinfo);
1313 if (!ret) {
1314
1315
1316
1317
1318 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
1319 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
1320 sizeof(tinfo.tidcnt)))
1321 return -EFAULT;
1322
1323 addr = arg + offsetof(struct hfi1_tid_info, length);
1324 if (copy_to_user((void __user *)addr, &tinfo.length,
1325 sizeof(tinfo.length)))
1326 ret = -EFAULT;
1327 }
1328
1329 return ret;
1330 }
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342 static int user_exp_rcv_clear(struct hfi1_filedata *fd, unsigned long arg,
1343 u32 len)
1344 {
1345 int ret;
1346 unsigned long addr;
1347 struct hfi1_tid_info tinfo;
1348
1349 if (sizeof(tinfo) != len)
1350 return -EINVAL;
1351
1352 if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo))))
1353 return -EFAULT;
1354
1355 ret = hfi1_user_exp_rcv_clear(fd, &tinfo);
1356 if (!ret) {
1357 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
1358 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
1359 sizeof(tinfo.tidcnt)))
1360 return -EFAULT;
1361 }
1362
1363 return ret;
1364 }
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375 static int user_exp_rcv_invalid(struct hfi1_filedata *fd, unsigned long arg,
1376 u32 len)
1377 {
1378 int ret;
1379 unsigned long addr;
1380 struct hfi1_tid_info tinfo;
1381
1382 if (sizeof(tinfo) != len)
1383 return -EINVAL;
1384
1385 if (!fd->invalid_tids)
1386 return -EINVAL;
1387
1388 if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo))))
1389 return -EFAULT;
1390
1391 ret = hfi1_user_exp_rcv_invalid(fd, &tinfo);
1392 if (ret)
1393 return ret;
1394
1395 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
1396 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
1397 sizeof(tinfo.tidcnt)))
1398 ret = -EFAULT;
1399
1400 return ret;
1401 }
1402
1403 static __poll_t poll_urgent(struct file *fp,
1404 struct poll_table_struct *pt)
1405 {
1406 struct hfi1_filedata *fd = fp->private_data;
1407 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1408 struct hfi1_devdata *dd = uctxt->dd;
1409 __poll_t pollflag;
1410
1411 poll_wait(fp, &uctxt->wait, pt);
1412
1413 spin_lock_irq(&dd->uctxt_lock);
1414 if (uctxt->urgent != uctxt->urgent_poll) {
1415 pollflag = EPOLLIN | EPOLLRDNORM;
1416 uctxt->urgent_poll = uctxt->urgent;
1417 } else {
1418 pollflag = 0;
1419 set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags);
1420 }
1421 spin_unlock_irq(&dd->uctxt_lock);
1422
1423 return pollflag;
1424 }
1425
1426 static __poll_t poll_next(struct file *fp,
1427 struct poll_table_struct *pt)
1428 {
1429 struct hfi1_filedata *fd = fp->private_data;
1430 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1431 struct hfi1_devdata *dd = uctxt->dd;
1432 __poll_t pollflag;
1433
1434 poll_wait(fp, &uctxt->wait, pt);
1435
1436 spin_lock_irq(&dd->uctxt_lock);
1437 if (hdrqempty(uctxt)) {
1438 set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags);
1439 hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt);
1440 pollflag = 0;
1441 } else {
1442 pollflag = EPOLLIN | EPOLLRDNORM;
1443 }
1444 spin_unlock_irq(&dd->uctxt_lock);
1445
1446 return pollflag;
1447 }
1448
1449
1450
1451
1452
1453
1454 int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit)
1455 {
1456 struct hfi1_ctxtdata *uctxt;
1457 struct hfi1_devdata *dd = ppd->dd;
1458 u16 ctxt;
1459
1460 if (!dd->events)
1461 return -EINVAL;
1462
1463 for (ctxt = dd->first_dyn_alloc_ctxt; ctxt < dd->num_rcv_contexts;
1464 ctxt++) {
1465 uctxt = hfi1_rcd_get_by_index(dd, ctxt);
1466 if (uctxt) {
1467 unsigned long *evs;
1468 int i;
1469
1470
1471
1472
1473 evs = dd->events + uctxt_offset(uctxt);
1474 set_bit(evtbit, evs);
1475 for (i = 1; i < uctxt->subctxt_cnt; i++)
1476 set_bit(evtbit, evs + i);
1477 hfi1_rcd_put(uctxt);
1478 }
1479 }
1480
1481 return 0;
1482 }
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494 static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
1495 unsigned long arg)
1496 {
1497 struct hfi1_devdata *dd = uctxt->dd;
1498 unsigned int rcvctrl_op;
1499 int start_stop;
1500
1501 if (subctxt)
1502 return 0;
1503
1504 if (get_user(start_stop, (int __user *)arg))
1505 return -EFAULT;
1506
1507
1508 if (start_stop) {
1509
1510
1511
1512
1513
1514
1515
1516
1517 if (hfi1_rcvhdrtail_kvaddr(uctxt))
1518 clear_rcvhdrtail(uctxt);
1519 rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB;
1520 } else {
1521 rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS;
1522 }
1523 hfi1_rcvctrl(dd, rcvctrl_op, uctxt);
1524
1525
1526 return 0;
1527 }
1528
1529
1530
1531
1532
1533
1534 static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
1535 unsigned long arg)
1536 {
1537 int i;
1538 struct hfi1_devdata *dd = uctxt->dd;
1539 unsigned long *evs;
1540 unsigned long events;
1541
1542 if (!dd->events)
1543 return 0;
1544
1545 if (get_user(events, (unsigned long __user *)arg))
1546 return -EFAULT;
1547
1548 evs = dd->events + uctxt_offset(uctxt) + subctxt;
1549
1550 for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) {
1551 if (!test_bit(i, &events))
1552 continue;
1553 clear_bit(i, evs);
1554 }
1555 return 0;
1556 }
1557
1558 static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned long arg)
1559 {
1560 int i;
1561 struct hfi1_pportdata *ppd = uctxt->ppd;
1562 struct hfi1_devdata *dd = uctxt->dd;
1563 u16 pkey;
1564
1565 if (!HFI1_CAP_IS_USET(PKEY_CHECK))
1566 return -EPERM;
1567
1568 if (get_user(pkey, (u16 __user *)arg))
1569 return -EFAULT;
1570
1571 if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY)
1572 return -EINVAL;
1573
1574 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++)
1575 if (pkey == ppd->pkeys[i])
1576 return hfi1_set_ctxt_pkey(dd, uctxt, pkey);
1577
1578 return -ENOENT;
1579 }
1580
1581
1582
1583
1584
1585 static int ctxt_reset(struct hfi1_ctxtdata *uctxt)
1586 {
1587 struct send_context *sc;
1588 struct hfi1_devdata *dd;
1589 int ret = 0;
1590
1591 if (!uctxt || !uctxt->dd || !uctxt->sc)
1592 return -EINVAL;
1593
1594
1595
1596
1597
1598
1599
1600 dd = uctxt->dd;
1601 sc = uctxt->sc;
1602
1603
1604
1605
1606
1607 wait_event_interruptible_timeout(
1608 sc->halt_wait, (sc->flags & SCF_HALTED),
1609 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
1610 if (!(sc->flags & SCF_HALTED))
1611 return -ENOLCK;
1612
1613
1614
1615
1616
1617 if (sc->flags & SCF_FROZEN) {
1618 wait_event_interruptible_timeout(
1619 dd->event_queue,
1620 !(READ_ONCE(dd->flags) & HFI1_FROZEN),
1621 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
1622 if (dd->flags & HFI1_FROZEN)
1623 return -ENOLCK;
1624
1625 if (dd->flags & HFI1_FORCED_FREEZE)
1626
1627
1628
1629
1630 return -ENODEV;
1631
1632 sc_disable(sc);
1633 ret = sc_enable(sc);
1634 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB, uctxt);
1635 } else {
1636 ret = sc_restart(sc);
1637 }
1638 if (!ret)
1639 sc_return_credits(sc);
1640
1641 return ret;
1642 }
1643
1644 static void user_remove(struct hfi1_devdata *dd)
1645 {
1646
1647 hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device);
1648 }
1649
1650 static int user_add(struct hfi1_devdata *dd)
1651 {
1652 char name[10];
1653 int ret;
1654
1655 snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit);
1656 ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops,
1657 &dd->user_cdev, &dd->user_device,
1658 true, &dd->verbs_dev.rdi.ibdev.dev.kobj);
1659 if (ret)
1660 user_remove(dd);
1661
1662 return ret;
1663 }
1664
1665
1666
1667
1668 int hfi1_device_create(struct hfi1_devdata *dd)
1669 {
1670 return user_add(dd);
1671 }
1672
1673
1674
1675
1676
1677 void hfi1_device_remove(struct hfi1_devdata *dd)
1678 {
1679 user_remove(dd);
1680 }