162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright(c) 2020 Cornelis Networks, Inc. 462306a36Sopenharmony_ci * Copyright(c) 2015-2020 Intel Corporation. 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci#include <linux/poll.h> 862306a36Sopenharmony_ci#include <linux/cdev.h> 962306a36Sopenharmony_ci#include <linux/vmalloc.h> 1062306a36Sopenharmony_ci#include <linux/io.h> 1162306a36Sopenharmony_ci#include <linux/sched/mm.h> 1262306a36Sopenharmony_ci#include <linux/bitmap.h> 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#include <rdma/ib.h> 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci#include "hfi.h" 1762306a36Sopenharmony_ci#include "pio.h" 1862306a36Sopenharmony_ci#include "device.h" 1962306a36Sopenharmony_ci#include "common.h" 2062306a36Sopenharmony_ci#include "trace.h" 2162306a36Sopenharmony_ci#include "mmu_rb.h" 2262306a36Sopenharmony_ci#include "user_sdma.h" 2362306a36Sopenharmony_ci#include "user_exp_rcv.h" 2462306a36Sopenharmony_ci#include "aspm.h" 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci#undef pr_fmt 2762306a36Sopenharmony_ci#define pr_fmt(fmt) DRIVER_NAME ": " fmt 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci#define SEND_CTXT_HALT_TIMEOUT 1000 /* msecs */ 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci/* 3262306a36Sopenharmony_ci * File operation functions 3362306a36Sopenharmony_ci */ 3462306a36Sopenharmony_cistatic int hfi1_file_open(struct inode *inode, struct file *fp); 3562306a36Sopenharmony_cistatic int hfi1_file_close(struct inode *inode, struct file *fp); 3662306a36Sopenharmony_cistatic ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from); 3762306a36Sopenharmony_cistatic __poll_t hfi1_poll(struct file *fp, struct poll_table_struct *pt); 3862306a36Sopenharmony_cistatic int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma); 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_cistatic u64 kvirt_to_phys(void *addr); 4162306a36Sopenharmony_cistatic int assign_ctxt(struct hfi1_filedata *fd, unsigned long arg, u32 len); 4262306a36Sopenharmony_cistatic void init_subctxts(struct hfi1_ctxtdata *uctxt, 4362306a36Sopenharmony_ci const struct hfi1_user_info *uinfo); 4462306a36Sopenharmony_cistatic int init_user_ctxt(struct hfi1_filedata *fd, 4562306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt); 4662306a36Sopenharmony_cistatic void user_init(struct hfi1_ctxtdata *uctxt); 4762306a36Sopenharmony_cistatic int get_ctxt_info(struct hfi1_filedata *fd, unsigned long arg, u32 len); 4862306a36Sopenharmony_cistatic int get_base_info(struct hfi1_filedata *fd, unsigned long arg, u32 len); 4962306a36Sopenharmony_cistatic int user_exp_rcv_setup(struct hfi1_filedata *fd, unsigned long arg, 5062306a36Sopenharmony_ci u32 len); 5162306a36Sopenharmony_cistatic int user_exp_rcv_clear(struct hfi1_filedata *fd, unsigned long arg, 5262306a36Sopenharmony_ci u32 len); 5362306a36Sopenharmony_cistatic int user_exp_rcv_invalid(struct hfi1_filedata *fd, unsigned long arg, 5462306a36Sopenharmony_ci u32 len); 5562306a36Sopenharmony_cistatic int setup_base_ctxt(struct hfi1_filedata *fd, 5662306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt); 5762306a36Sopenharmony_cistatic int setup_subctxt(struct hfi1_ctxtdata *uctxt); 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_cistatic int find_sub_ctxt(struct hfi1_filedata *fd, 6062306a36Sopenharmony_ci const struct hfi1_user_info *uinfo); 6162306a36Sopenharmony_cistatic int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd, 6262306a36Sopenharmony_ci struct hfi1_user_info *uinfo, 6362306a36Sopenharmony_ci struct hfi1_ctxtdata **cd); 6462306a36Sopenharmony_cistatic void deallocate_ctxt(struct hfi1_ctxtdata *uctxt); 6562306a36Sopenharmony_cistatic __poll_t poll_urgent(struct file *fp, struct poll_table_struct *pt); 6662306a36Sopenharmony_cistatic __poll_t poll_next(struct file *fp, struct poll_table_struct *pt); 6762306a36Sopenharmony_cistatic int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt, 6862306a36Sopenharmony_ci unsigned long arg); 6962306a36Sopenharmony_cistatic int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned long arg); 7062306a36Sopenharmony_cistatic int ctxt_reset(struct hfi1_ctxtdata *uctxt); 7162306a36Sopenharmony_cistatic int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt, 7262306a36Sopenharmony_ci unsigned long arg); 7362306a36Sopenharmony_cistatic vm_fault_t vma_fault(struct vm_fault *vmf); 7462306a36Sopenharmony_cistatic long hfi1_file_ioctl(struct file *fp, unsigned int cmd, 7562306a36Sopenharmony_ci unsigned long arg); 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_cistatic const struct file_operations hfi1_file_ops = { 7862306a36Sopenharmony_ci .owner = THIS_MODULE, 7962306a36Sopenharmony_ci .write_iter = hfi1_write_iter, 8062306a36Sopenharmony_ci .open = hfi1_file_open, 8162306a36Sopenharmony_ci .release = hfi1_file_close, 8262306a36Sopenharmony_ci .unlocked_ioctl = hfi1_file_ioctl, 8362306a36Sopenharmony_ci .poll = hfi1_poll, 8462306a36Sopenharmony_ci .mmap = hfi1_file_mmap, 8562306a36Sopenharmony_ci .llseek = noop_llseek, 8662306a36Sopenharmony_ci}; 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_cistatic const struct vm_operations_struct vm_ops = { 8962306a36Sopenharmony_ci .fault = vma_fault, 9062306a36Sopenharmony_ci}; 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci/* 9362306a36Sopenharmony_ci * Types of memories mapped into user processes' space 9462306a36Sopenharmony_ci */ 9562306a36Sopenharmony_cienum mmap_types { 9662306a36Sopenharmony_ci PIO_BUFS = 1, 9762306a36Sopenharmony_ci PIO_BUFS_SOP, 9862306a36Sopenharmony_ci PIO_CRED, 9962306a36Sopenharmony_ci RCV_HDRQ, 10062306a36Sopenharmony_ci RCV_EGRBUF, 10162306a36Sopenharmony_ci UREGS, 10262306a36Sopenharmony_ci EVENTS, 10362306a36Sopenharmony_ci STATUS, 10462306a36Sopenharmony_ci RTAIL, 10562306a36Sopenharmony_ci SUBCTXT_UREGS, 10662306a36Sopenharmony_ci SUBCTXT_RCV_HDRQ, 10762306a36Sopenharmony_ci SUBCTXT_EGRBUF, 10862306a36Sopenharmony_ci SDMA_COMP 10962306a36Sopenharmony_ci}; 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci/* 11262306a36Sopenharmony_ci * Masks and offsets defining the mmap tokens 11362306a36Sopenharmony_ci */ 11462306a36Sopenharmony_ci#define HFI1_MMAP_OFFSET_MASK 0xfffULL 11562306a36Sopenharmony_ci#define HFI1_MMAP_OFFSET_SHIFT 0 11662306a36Sopenharmony_ci#define HFI1_MMAP_SUBCTXT_MASK 0xfULL 11762306a36Sopenharmony_ci#define HFI1_MMAP_SUBCTXT_SHIFT 12 11862306a36Sopenharmony_ci#define HFI1_MMAP_CTXT_MASK 0xffULL 11962306a36Sopenharmony_ci#define HFI1_MMAP_CTXT_SHIFT 16 12062306a36Sopenharmony_ci#define HFI1_MMAP_TYPE_MASK 0xfULL 12162306a36Sopenharmony_ci#define HFI1_MMAP_TYPE_SHIFT 24 12262306a36Sopenharmony_ci#define HFI1_MMAP_MAGIC_MASK 0xffffffffULL 12362306a36Sopenharmony_ci#define HFI1_MMAP_MAGIC_SHIFT 32 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ci#define HFI1_MMAP_MAGIC 0xdabbad00 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci#define HFI1_MMAP_TOKEN_SET(field, val) \ 12862306a36Sopenharmony_ci (((val) & HFI1_MMAP_##field##_MASK) << HFI1_MMAP_##field##_SHIFT) 12962306a36Sopenharmony_ci#define HFI1_MMAP_TOKEN_GET(field, token) \ 13062306a36Sopenharmony_ci (((token) >> HFI1_MMAP_##field##_SHIFT) & HFI1_MMAP_##field##_MASK) 13162306a36Sopenharmony_ci#define HFI1_MMAP_TOKEN(type, ctxt, subctxt, addr) \ 13262306a36Sopenharmony_ci (HFI1_MMAP_TOKEN_SET(MAGIC, HFI1_MMAP_MAGIC) | \ 13362306a36Sopenharmony_ci HFI1_MMAP_TOKEN_SET(TYPE, type) | \ 13462306a36Sopenharmony_ci HFI1_MMAP_TOKEN_SET(CTXT, ctxt) | \ 13562306a36Sopenharmony_ci HFI1_MMAP_TOKEN_SET(SUBCTXT, subctxt) | \ 13662306a36Sopenharmony_ci HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr)))) 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci#define dbg(fmt, ...) \ 13962306a36Sopenharmony_ci pr_info(fmt, ##__VA_ARGS__) 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_cistatic inline int is_valid_mmap(u64 token) 14262306a36Sopenharmony_ci{ 14362306a36Sopenharmony_ci return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC); 14462306a36Sopenharmony_ci} 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_cistatic int hfi1_file_open(struct inode *inode, struct file *fp) 14762306a36Sopenharmony_ci{ 14862306a36Sopenharmony_ci struct hfi1_filedata *fd; 14962306a36Sopenharmony_ci struct hfi1_devdata *dd = container_of(inode->i_cdev, 15062306a36Sopenharmony_ci struct hfi1_devdata, 15162306a36Sopenharmony_ci user_cdev); 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci if (!((dd->flags & HFI1_PRESENT) && dd->kregbase1)) 15462306a36Sopenharmony_ci return -EINVAL; 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci if (!refcount_inc_not_zero(&dd->user_refcount)) 15762306a36Sopenharmony_ci return -ENXIO; 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci /* The real work is performed later in assign_ctxt() */ 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci fd = kzalloc(sizeof(*fd), GFP_KERNEL); 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci if (!fd || init_srcu_struct(&fd->pq_srcu)) 16462306a36Sopenharmony_ci goto nomem; 16562306a36Sopenharmony_ci spin_lock_init(&fd->pq_rcu_lock); 16662306a36Sopenharmony_ci spin_lock_init(&fd->tid_lock); 16762306a36Sopenharmony_ci spin_lock_init(&fd->invalid_lock); 16862306a36Sopenharmony_ci fd->rec_cpu_num = -1; /* no cpu affinity by default */ 16962306a36Sopenharmony_ci fd->dd = dd; 17062306a36Sopenharmony_ci fp->private_data = fd; 17162306a36Sopenharmony_ci return 0; 17262306a36Sopenharmony_cinomem: 17362306a36Sopenharmony_ci kfree(fd); 17462306a36Sopenharmony_ci fp->private_data = NULL; 17562306a36Sopenharmony_ci if (refcount_dec_and_test(&dd->user_refcount)) 17662306a36Sopenharmony_ci complete(&dd->user_comp); 17762306a36Sopenharmony_ci return -ENOMEM; 17862306a36Sopenharmony_ci} 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_cistatic long hfi1_file_ioctl(struct file *fp, unsigned int cmd, 18162306a36Sopenharmony_ci unsigned long arg) 18262306a36Sopenharmony_ci{ 18362306a36Sopenharmony_ci struct hfi1_filedata *fd = fp->private_data; 18462306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt = fd->uctxt; 18562306a36Sopenharmony_ci int ret = 0; 18662306a36Sopenharmony_ci int uval = 0; 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ci hfi1_cdbg(IOCTL, "IOCTL recv: 0x%x", cmd); 18962306a36Sopenharmony_ci if (cmd != HFI1_IOCTL_ASSIGN_CTXT && 19062306a36Sopenharmony_ci cmd != HFI1_IOCTL_GET_VERS && 19162306a36Sopenharmony_ci !uctxt) 19262306a36Sopenharmony_ci return -EINVAL; 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci switch (cmd) { 19562306a36Sopenharmony_ci case HFI1_IOCTL_ASSIGN_CTXT: 19662306a36Sopenharmony_ci ret = assign_ctxt(fd, arg, _IOC_SIZE(cmd)); 19762306a36Sopenharmony_ci break; 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci case HFI1_IOCTL_CTXT_INFO: 20062306a36Sopenharmony_ci ret = get_ctxt_info(fd, arg, _IOC_SIZE(cmd)); 20162306a36Sopenharmony_ci break; 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ci case HFI1_IOCTL_USER_INFO: 20462306a36Sopenharmony_ci ret = get_base_info(fd, arg, _IOC_SIZE(cmd)); 20562306a36Sopenharmony_ci break; 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci case HFI1_IOCTL_CREDIT_UPD: 20862306a36Sopenharmony_ci if (uctxt) 20962306a36Sopenharmony_ci sc_return_credits(uctxt->sc); 21062306a36Sopenharmony_ci break; 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ci case HFI1_IOCTL_TID_UPDATE: 21362306a36Sopenharmony_ci ret = user_exp_rcv_setup(fd, arg, _IOC_SIZE(cmd)); 21462306a36Sopenharmony_ci break; 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_ci case HFI1_IOCTL_TID_FREE: 21762306a36Sopenharmony_ci ret = user_exp_rcv_clear(fd, arg, _IOC_SIZE(cmd)); 21862306a36Sopenharmony_ci break; 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ci case HFI1_IOCTL_TID_INVAL_READ: 22162306a36Sopenharmony_ci ret = user_exp_rcv_invalid(fd, arg, _IOC_SIZE(cmd)); 22262306a36Sopenharmony_ci break; 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ci case HFI1_IOCTL_RECV_CTRL: 22562306a36Sopenharmony_ci ret = manage_rcvq(uctxt, fd->subctxt, arg); 22662306a36Sopenharmony_ci break; 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ci case HFI1_IOCTL_POLL_TYPE: 22962306a36Sopenharmony_ci if (get_user(uval, (int __user *)arg)) 23062306a36Sopenharmony_ci return -EFAULT; 23162306a36Sopenharmony_ci uctxt->poll_type = (typeof(uctxt->poll_type))uval; 23262306a36Sopenharmony_ci break; 23362306a36Sopenharmony_ci 23462306a36Sopenharmony_ci case HFI1_IOCTL_ACK_EVENT: 23562306a36Sopenharmony_ci ret = user_event_ack(uctxt, fd->subctxt, arg); 23662306a36Sopenharmony_ci break; 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ci case HFI1_IOCTL_SET_PKEY: 23962306a36Sopenharmony_ci ret = set_ctxt_pkey(uctxt, arg); 24062306a36Sopenharmony_ci break; 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_ci case HFI1_IOCTL_CTXT_RESET: 24362306a36Sopenharmony_ci ret = ctxt_reset(uctxt); 24462306a36Sopenharmony_ci break; 24562306a36Sopenharmony_ci 24662306a36Sopenharmony_ci case HFI1_IOCTL_GET_VERS: 24762306a36Sopenharmony_ci uval = HFI1_USER_SWVERSION; 24862306a36Sopenharmony_ci if (put_user(uval, (int __user *)arg)) 24962306a36Sopenharmony_ci return -EFAULT; 25062306a36Sopenharmony_ci break; 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_ci default: 25362306a36Sopenharmony_ci return -EINVAL; 25462306a36Sopenharmony_ci } 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_ci return ret; 25762306a36Sopenharmony_ci} 25862306a36Sopenharmony_ci 25962306a36Sopenharmony_cistatic ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from) 26062306a36Sopenharmony_ci{ 26162306a36Sopenharmony_ci struct hfi1_filedata *fd = kiocb->ki_filp->private_data; 26262306a36Sopenharmony_ci struct hfi1_user_sdma_pkt_q *pq; 26362306a36Sopenharmony_ci struct hfi1_user_sdma_comp_q *cq = fd->cq; 26462306a36Sopenharmony_ci int done = 0, reqs = 0; 26562306a36Sopenharmony_ci unsigned long dim = from->nr_segs; 26662306a36Sopenharmony_ci int idx; 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_ci if (!HFI1_CAP_IS_KSET(SDMA)) 26962306a36Sopenharmony_ci return -EINVAL; 27062306a36Sopenharmony_ci if (!from->user_backed) 27162306a36Sopenharmony_ci return -EINVAL; 27262306a36Sopenharmony_ci idx = srcu_read_lock(&fd->pq_srcu); 27362306a36Sopenharmony_ci pq = srcu_dereference(fd->pq, &fd->pq_srcu); 27462306a36Sopenharmony_ci if (!cq || !pq) { 27562306a36Sopenharmony_ci srcu_read_unlock(&fd->pq_srcu, idx); 27662306a36Sopenharmony_ci return -EIO; 27762306a36Sopenharmony_ci } 27862306a36Sopenharmony_ci 27962306a36Sopenharmony_ci trace_hfi1_sdma_request(fd->dd, fd->uctxt->ctxt, fd->subctxt, dim); 28062306a36Sopenharmony_ci 28162306a36Sopenharmony_ci if (atomic_read(&pq->n_reqs) == pq->n_max_reqs) { 28262306a36Sopenharmony_ci srcu_read_unlock(&fd->pq_srcu, idx); 28362306a36Sopenharmony_ci return -ENOSPC; 28462306a36Sopenharmony_ci } 28562306a36Sopenharmony_ci 28662306a36Sopenharmony_ci while (dim) { 28762306a36Sopenharmony_ci const struct iovec *iov = iter_iov(from); 28862306a36Sopenharmony_ci int ret; 28962306a36Sopenharmony_ci unsigned long count = 0; 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ci ret = hfi1_user_sdma_process_request( 29262306a36Sopenharmony_ci fd, (struct iovec *)(iov + done), 29362306a36Sopenharmony_ci dim, &count); 29462306a36Sopenharmony_ci if (ret) { 29562306a36Sopenharmony_ci reqs = ret; 29662306a36Sopenharmony_ci break; 29762306a36Sopenharmony_ci } 29862306a36Sopenharmony_ci dim -= count; 29962306a36Sopenharmony_ci done += count; 30062306a36Sopenharmony_ci reqs++; 30162306a36Sopenharmony_ci } 30262306a36Sopenharmony_ci 30362306a36Sopenharmony_ci srcu_read_unlock(&fd->pq_srcu, idx); 30462306a36Sopenharmony_ci return reqs; 30562306a36Sopenharmony_ci} 30662306a36Sopenharmony_ci 30762306a36Sopenharmony_cistatic inline void mmap_cdbg(u16 ctxt, u8 subctxt, u8 type, u8 mapio, u8 vmf, 30862306a36Sopenharmony_ci u64 memaddr, void *memvirt, dma_addr_t memdma, 30962306a36Sopenharmony_ci ssize_t memlen, struct vm_area_struct *vma) 31062306a36Sopenharmony_ci{ 31162306a36Sopenharmony_ci hfi1_cdbg(PROC, 31262306a36Sopenharmony_ci "%u:%u type:%u io/vf/dma:%d/%d/%d, addr:0x%llx, len:%lu(%lu), flags:0x%lx", 31362306a36Sopenharmony_ci ctxt, subctxt, type, mapio, vmf, !!memdma, 31462306a36Sopenharmony_ci memaddr ?: (u64)memvirt, memlen, 31562306a36Sopenharmony_ci vma->vm_end - vma->vm_start, vma->vm_flags); 31662306a36Sopenharmony_ci} 31762306a36Sopenharmony_ci 31862306a36Sopenharmony_cistatic int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma) 31962306a36Sopenharmony_ci{ 32062306a36Sopenharmony_ci struct hfi1_filedata *fd = fp->private_data; 32162306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt = fd->uctxt; 32262306a36Sopenharmony_ci struct hfi1_devdata *dd; 32362306a36Sopenharmony_ci unsigned long flags; 32462306a36Sopenharmony_ci u64 token = vma->vm_pgoff << PAGE_SHIFT, 32562306a36Sopenharmony_ci memaddr = 0; 32662306a36Sopenharmony_ci void *memvirt = NULL; 32762306a36Sopenharmony_ci dma_addr_t memdma = 0; 32862306a36Sopenharmony_ci u8 subctxt, mapio = 0, vmf = 0, type; 32962306a36Sopenharmony_ci ssize_t memlen = 0; 33062306a36Sopenharmony_ci int ret = 0; 33162306a36Sopenharmony_ci u16 ctxt; 33262306a36Sopenharmony_ci 33362306a36Sopenharmony_ci if (!is_valid_mmap(token) || !uctxt || 33462306a36Sopenharmony_ci !(vma->vm_flags & VM_SHARED)) { 33562306a36Sopenharmony_ci ret = -EINVAL; 33662306a36Sopenharmony_ci goto done; 33762306a36Sopenharmony_ci } 33862306a36Sopenharmony_ci dd = uctxt->dd; 33962306a36Sopenharmony_ci ctxt = HFI1_MMAP_TOKEN_GET(CTXT, token); 34062306a36Sopenharmony_ci subctxt = HFI1_MMAP_TOKEN_GET(SUBCTXT, token); 34162306a36Sopenharmony_ci type = HFI1_MMAP_TOKEN_GET(TYPE, token); 34262306a36Sopenharmony_ci if (ctxt != uctxt->ctxt || subctxt != fd->subctxt) { 34362306a36Sopenharmony_ci ret = -EINVAL; 34462306a36Sopenharmony_ci goto done; 34562306a36Sopenharmony_ci } 34662306a36Sopenharmony_ci 34762306a36Sopenharmony_ci /* 34862306a36Sopenharmony_ci * vm_pgoff is used as a buffer selector cookie. Always mmap from 34962306a36Sopenharmony_ci * the beginning. 35062306a36Sopenharmony_ci */ 35162306a36Sopenharmony_ci vma->vm_pgoff = 0; 35262306a36Sopenharmony_ci flags = vma->vm_flags; 35362306a36Sopenharmony_ci 35462306a36Sopenharmony_ci switch (type) { 35562306a36Sopenharmony_ci case PIO_BUFS: 35662306a36Sopenharmony_ci case PIO_BUFS_SOP: 35762306a36Sopenharmony_ci memaddr = ((dd->physaddr + TXE_PIO_SEND) + 35862306a36Sopenharmony_ci /* chip pio base */ 35962306a36Sopenharmony_ci (uctxt->sc->hw_context * BIT(16))) + 36062306a36Sopenharmony_ci /* 64K PIO space / ctxt */ 36162306a36Sopenharmony_ci (type == PIO_BUFS_SOP ? 36262306a36Sopenharmony_ci (TXE_PIO_SIZE / 2) : 0); /* sop? */ 36362306a36Sopenharmony_ci /* 36462306a36Sopenharmony_ci * Map only the amount allocated to the context, not the 36562306a36Sopenharmony_ci * entire available context's PIO space. 36662306a36Sopenharmony_ci */ 36762306a36Sopenharmony_ci memlen = PAGE_ALIGN(uctxt->sc->credits * PIO_BLOCK_SIZE); 36862306a36Sopenharmony_ci flags &= ~VM_MAYREAD; 36962306a36Sopenharmony_ci flags |= VM_DONTCOPY | VM_DONTEXPAND; 37062306a36Sopenharmony_ci vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); 37162306a36Sopenharmony_ci mapio = 1; 37262306a36Sopenharmony_ci break; 37362306a36Sopenharmony_ci case PIO_CRED: { 37462306a36Sopenharmony_ci u64 cr_page_offset; 37562306a36Sopenharmony_ci if (flags & VM_WRITE) { 37662306a36Sopenharmony_ci ret = -EPERM; 37762306a36Sopenharmony_ci goto done; 37862306a36Sopenharmony_ci } 37962306a36Sopenharmony_ci /* 38062306a36Sopenharmony_ci * The credit return location for this context could be on the 38162306a36Sopenharmony_ci * second or third page allocated for credit returns (if number 38262306a36Sopenharmony_ci * of enabled contexts > 64 and 128 respectively). 38362306a36Sopenharmony_ci */ 38462306a36Sopenharmony_ci cr_page_offset = ((u64)uctxt->sc->hw_free - 38562306a36Sopenharmony_ci (u64)dd->cr_base[uctxt->numa_id].va) & 38662306a36Sopenharmony_ci PAGE_MASK; 38762306a36Sopenharmony_ci memvirt = dd->cr_base[uctxt->numa_id].va + cr_page_offset; 38862306a36Sopenharmony_ci memdma = dd->cr_base[uctxt->numa_id].dma + cr_page_offset; 38962306a36Sopenharmony_ci memlen = PAGE_SIZE; 39062306a36Sopenharmony_ci flags &= ~VM_MAYWRITE; 39162306a36Sopenharmony_ci flags |= VM_DONTCOPY | VM_DONTEXPAND; 39262306a36Sopenharmony_ci /* 39362306a36Sopenharmony_ci * The driver has already allocated memory for credit 39462306a36Sopenharmony_ci * returns and programmed it into the chip. Has that 39562306a36Sopenharmony_ci * memory been flagged as non-cached? 39662306a36Sopenharmony_ci */ 39762306a36Sopenharmony_ci /* vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); */ 39862306a36Sopenharmony_ci break; 39962306a36Sopenharmony_ci } 40062306a36Sopenharmony_ci case RCV_HDRQ: 40162306a36Sopenharmony_ci memlen = rcvhdrq_size(uctxt); 40262306a36Sopenharmony_ci memvirt = uctxt->rcvhdrq; 40362306a36Sopenharmony_ci memdma = uctxt->rcvhdrq_dma; 40462306a36Sopenharmony_ci break; 40562306a36Sopenharmony_ci case RCV_EGRBUF: { 40662306a36Sopenharmony_ci unsigned long vm_start_save; 40762306a36Sopenharmony_ci unsigned long vm_end_save; 40862306a36Sopenharmony_ci int i; 40962306a36Sopenharmony_ci /* 41062306a36Sopenharmony_ci * The RcvEgr buffer need to be handled differently 41162306a36Sopenharmony_ci * as multiple non-contiguous pages need to be mapped 41262306a36Sopenharmony_ci * into the user process. 41362306a36Sopenharmony_ci */ 41462306a36Sopenharmony_ci memlen = uctxt->egrbufs.size; 41562306a36Sopenharmony_ci if ((vma->vm_end - vma->vm_start) != memlen) { 41662306a36Sopenharmony_ci dd_dev_err(dd, "Eager buffer map size invalid (%lu != %lu)\n", 41762306a36Sopenharmony_ci (vma->vm_end - vma->vm_start), memlen); 41862306a36Sopenharmony_ci ret = -EINVAL; 41962306a36Sopenharmony_ci goto done; 42062306a36Sopenharmony_ci } 42162306a36Sopenharmony_ci if (vma->vm_flags & VM_WRITE) { 42262306a36Sopenharmony_ci ret = -EPERM; 42362306a36Sopenharmony_ci goto done; 42462306a36Sopenharmony_ci } 42562306a36Sopenharmony_ci vm_flags_clear(vma, VM_MAYWRITE); 42662306a36Sopenharmony_ci /* 42762306a36Sopenharmony_ci * Mmap multiple separate allocations into a single vma. From 42862306a36Sopenharmony_ci * here, dma_mmap_coherent() calls dma_direct_mmap(), which 42962306a36Sopenharmony_ci * requires the mmap to exactly fill the vma starting at 43062306a36Sopenharmony_ci * vma_start. Adjust the vma start and end for each eager 43162306a36Sopenharmony_ci * buffer segment mapped. Restore the originals when done. 43262306a36Sopenharmony_ci */ 43362306a36Sopenharmony_ci vm_start_save = vma->vm_start; 43462306a36Sopenharmony_ci vm_end_save = vma->vm_end; 43562306a36Sopenharmony_ci vma->vm_end = vma->vm_start; 43662306a36Sopenharmony_ci for (i = 0 ; i < uctxt->egrbufs.numbufs; i++) { 43762306a36Sopenharmony_ci memlen = uctxt->egrbufs.buffers[i].len; 43862306a36Sopenharmony_ci memvirt = uctxt->egrbufs.buffers[i].addr; 43962306a36Sopenharmony_ci memdma = uctxt->egrbufs.buffers[i].dma; 44062306a36Sopenharmony_ci vma->vm_end += memlen; 44162306a36Sopenharmony_ci mmap_cdbg(ctxt, subctxt, type, mapio, vmf, memaddr, 44262306a36Sopenharmony_ci memvirt, memdma, memlen, vma); 44362306a36Sopenharmony_ci ret = dma_mmap_coherent(&dd->pcidev->dev, vma, 44462306a36Sopenharmony_ci memvirt, memdma, memlen); 44562306a36Sopenharmony_ci if (ret < 0) { 44662306a36Sopenharmony_ci vma->vm_start = vm_start_save; 44762306a36Sopenharmony_ci vma->vm_end = vm_end_save; 44862306a36Sopenharmony_ci goto done; 44962306a36Sopenharmony_ci } 45062306a36Sopenharmony_ci vma->vm_start += memlen; 45162306a36Sopenharmony_ci } 45262306a36Sopenharmony_ci vma->vm_start = vm_start_save; 45362306a36Sopenharmony_ci vma->vm_end = vm_end_save; 45462306a36Sopenharmony_ci ret = 0; 45562306a36Sopenharmony_ci goto done; 45662306a36Sopenharmony_ci } 45762306a36Sopenharmony_ci case UREGS: 45862306a36Sopenharmony_ci /* 45962306a36Sopenharmony_ci * Map only the page that contains this context's user 46062306a36Sopenharmony_ci * registers. 46162306a36Sopenharmony_ci */ 46262306a36Sopenharmony_ci memaddr = (unsigned long) 46362306a36Sopenharmony_ci (dd->physaddr + RXE_PER_CONTEXT_USER) 46462306a36Sopenharmony_ci + (uctxt->ctxt * RXE_PER_CONTEXT_SIZE); 46562306a36Sopenharmony_ci /* 46662306a36Sopenharmony_ci * TidFlow table is on the same page as the rest of the 46762306a36Sopenharmony_ci * user registers. 46862306a36Sopenharmony_ci */ 46962306a36Sopenharmony_ci memlen = PAGE_SIZE; 47062306a36Sopenharmony_ci flags |= VM_DONTCOPY | VM_DONTEXPAND; 47162306a36Sopenharmony_ci vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 47262306a36Sopenharmony_ci mapio = 1; 47362306a36Sopenharmony_ci break; 47462306a36Sopenharmony_ci case EVENTS: 47562306a36Sopenharmony_ci /* 47662306a36Sopenharmony_ci * Use the page where this context's flags are. User level 47762306a36Sopenharmony_ci * knows where it's own bitmap is within the page. 47862306a36Sopenharmony_ci */ 47962306a36Sopenharmony_ci memaddr = (unsigned long) 48062306a36Sopenharmony_ci (dd->events + uctxt_offset(uctxt)) & PAGE_MASK; 48162306a36Sopenharmony_ci memlen = PAGE_SIZE; 48262306a36Sopenharmony_ci /* 48362306a36Sopenharmony_ci * v3.7 removes VM_RESERVED but the effect is kept by 48462306a36Sopenharmony_ci * using VM_IO. 48562306a36Sopenharmony_ci */ 48662306a36Sopenharmony_ci flags |= VM_IO | VM_DONTEXPAND; 48762306a36Sopenharmony_ci vmf = 1; 48862306a36Sopenharmony_ci break; 48962306a36Sopenharmony_ci case STATUS: 49062306a36Sopenharmony_ci if (flags & VM_WRITE) { 49162306a36Sopenharmony_ci ret = -EPERM; 49262306a36Sopenharmony_ci goto done; 49362306a36Sopenharmony_ci } 49462306a36Sopenharmony_ci memaddr = kvirt_to_phys((void *)dd->status); 49562306a36Sopenharmony_ci memlen = PAGE_SIZE; 49662306a36Sopenharmony_ci flags |= VM_IO | VM_DONTEXPAND; 49762306a36Sopenharmony_ci break; 49862306a36Sopenharmony_ci case RTAIL: 49962306a36Sopenharmony_ci if (!HFI1_CAP_IS_USET(DMA_RTAIL)) { 50062306a36Sopenharmony_ci /* 50162306a36Sopenharmony_ci * If the memory allocation failed, the context alloc 50262306a36Sopenharmony_ci * also would have failed, so we would never get here 50362306a36Sopenharmony_ci */ 50462306a36Sopenharmony_ci ret = -EINVAL; 50562306a36Sopenharmony_ci goto done; 50662306a36Sopenharmony_ci } 50762306a36Sopenharmony_ci if ((flags & VM_WRITE) || !hfi1_rcvhdrtail_kvaddr(uctxt)) { 50862306a36Sopenharmony_ci ret = -EPERM; 50962306a36Sopenharmony_ci goto done; 51062306a36Sopenharmony_ci } 51162306a36Sopenharmony_ci memlen = PAGE_SIZE; 51262306a36Sopenharmony_ci memvirt = (void *)hfi1_rcvhdrtail_kvaddr(uctxt); 51362306a36Sopenharmony_ci memdma = uctxt->rcvhdrqtailaddr_dma; 51462306a36Sopenharmony_ci flags &= ~VM_MAYWRITE; 51562306a36Sopenharmony_ci break; 51662306a36Sopenharmony_ci case SUBCTXT_UREGS: 51762306a36Sopenharmony_ci memaddr = (u64)uctxt->subctxt_uregbase; 51862306a36Sopenharmony_ci memlen = PAGE_SIZE; 51962306a36Sopenharmony_ci flags |= VM_IO | VM_DONTEXPAND; 52062306a36Sopenharmony_ci vmf = 1; 52162306a36Sopenharmony_ci break; 52262306a36Sopenharmony_ci case SUBCTXT_RCV_HDRQ: 52362306a36Sopenharmony_ci memaddr = (u64)uctxt->subctxt_rcvhdr_base; 52462306a36Sopenharmony_ci memlen = rcvhdrq_size(uctxt) * uctxt->subctxt_cnt; 52562306a36Sopenharmony_ci flags |= VM_IO | VM_DONTEXPAND; 52662306a36Sopenharmony_ci vmf = 1; 52762306a36Sopenharmony_ci break; 52862306a36Sopenharmony_ci case SUBCTXT_EGRBUF: 52962306a36Sopenharmony_ci memaddr = (u64)uctxt->subctxt_rcvegrbuf; 53062306a36Sopenharmony_ci memlen = uctxt->egrbufs.size * uctxt->subctxt_cnt; 53162306a36Sopenharmony_ci flags |= VM_IO | VM_DONTEXPAND; 53262306a36Sopenharmony_ci flags &= ~VM_MAYWRITE; 53362306a36Sopenharmony_ci vmf = 1; 53462306a36Sopenharmony_ci break; 53562306a36Sopenharmony_ci case SDMA_COMP: { 53662306a36Sopenharmony_ci struct hfi1_user_sdma_comp_q *cq = fd->cq; 53762306a36Sopenharmony_ci 53862306a36Sopenharmony_ci if (!cq) { 53962306a36Sopenharmony_ci ret = -EFAULT; 54062306a36Sopenharmony_ci goto done; 54162306a36Sopenharmony_ci } 54262306a36Sopenharmony_ci memaddr = (u64)cq->comps; 54362306a36Sopenharmony_ci memlen = PAGE_ALIGN(sizeof(*cq->comps) * cq->nentries); 54462306a36Sopenharmony_ci flags |= VM_IO | VM_DONTEXPAND; 54562306a36Sopenharmony_ci vmf = 1; 54662306a36Sopenharmony_ci break; 54762306a36Sopenharmony_ci } 54862306a36Sopenharmony_ci default: 54962306a36Sopenharmony_ci ret = -EINVAL; 55062306a36Sopenharmony_ci break; 55162306a36Sopenharmony_ci } 55262306a36Sopenharmony_ci 55362306a36Sopenharmony_ci if ((vma->vm_end - vma->vm_start) != memlen) { 55462306a36Sopenharmony_ci hfi1_cdbg(PROC, "%u:%u Memory size mismatch %lu:%lu", 55562306a36Sopenharmony_ci uctxt->ctxt, fd->subctxt, 55662306a36Sopenharmony_ci (vma->vm_end - vma->vm_start), memlen); 55762306a36Sopenharmony_ci ret = -EINVAL; 55862306a36Sopenharmony_ci goto done; 55962306a36Sopenharmony_ci } 56062306a36Sopenharmony_ci 56162306a36Sopenharmony_ci vm_flags_reset(vma, flags); 56262306a36Sopenharmony_ci mmap_cdbg(ctxt, subctxt, type, mapio, vmf, memaddr, memvirt, memdma, 56362306a36Sopenharmony_ci memlen, vma); 56462306a36Sopenharmony_ci if (vmf) { 56562306a36Sopenharmony_ci vma->vm_pgoff = PFN_DOWN(memaddr); 56662306a36Sopenharmony_ci vma->vm_ops = &vm_ops; 56762306a36Sopenharmony_ci ret = 0; 56862306a36Sopenharmony_ci } else if (memdma) { 56962306a36Sopenharmony_ci ret = dma_mmap_coherent(&dd->pcidev->dev, vma, 57062306a36Sopenharmony_ci memvirt, memdma, memlen); 57162306a36Sopenharmony_ci } else if (mapio) { 57262306a36Sopenharmony_ci ret = io_remap_pfn_range(vma, vma->vm_start, 57362306a36Sopenharmony_ci PFN_DOWN(memaddr), 57462306a36Sopenharmony_ci memlen, 57562306a36Sopenharmony_ci vma->vm_page_prot); 57662306a36Sopenharmony_ci } else if (memvirt) { 57762306a36Sopenharmony_ci ret = remap_pfn_range(vma, vma->vm_start, 57862306a36Sopenharmony_ci PFN_DOWN(__pa(memvirt)), 57962306a36Sopenharmony_ci memlen, 58062306a36Sopenharmony_ci vma->vm_page_prot); 58162306a36Sopenharmony_ci } else { 58262306a36Sopenharmony_ci ret = remap_pfn_range(vma, vma->vm_start, 58362306a36Sopenharmony_ci PFN_DOWN(memaddr), 58462306a36Sopenharmony_ci memlen, 58562306a36Sopenharmony_ci vma->vm_page_prot); 58662306a36Sopenharmony_ci } 58762306a36Sopenharmony_cidone: 58862306a36Sopenharmony_ci return ret; 58962306a36Sopenharmony_ci} 59062306a36Sopenharmony_ci 59162306a36Sopenharmony_ci/* 59262306a36Sopenharmony_ci * Local (non-chip) user memory is not mapped right away but as it is 59362306a36Sopenharmony_ci * accessed by the user-level code. 59462306a36Sopenharmony_ci */ 59562306a36Sopenharmony_cistatic vm_fault_t vma_fault(struct vm_fault *vmf) 59662306a36Sopenharmony_ci{ 59762306a36Sopenharmony_ci struct page *page; 59862306a36Sopenharmony_ci 59962306a36Sopenharmony_ci page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT)); 60062306a36Sopenharmony_ci if (!page) 60162306a36Sopenharmony_ci return VM_FAULT_SIGBUS; 60262306a36Sopenharmony_ci 60362306a36Sopenharmony_ci get_page(page); 60462306a36Sopenharmony_ci vmf->page = page; 60562306a36Sopenharmony_ci 60662306a36Sopenharmony_ci return 0; 60762306a36Sopenharmony_ci} 60862306a36Sopenharmony_ci 60962306a36Sopenharmony_cistatic __poll_t hfi1_poll(struct file *fp, struct poll_table_struct *pt) 61062306a36Sopenharmony_ci{ 61162306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt; 61262306a36Sopenharmony_ci __poll_t pollflag; 61362306a36Sopenharmony_ci 61462306a36Sopenharmony_ci uctxt = ((struct hfi1_filedata *)fp->private_data)->uctxt; 61562306a36Sopenharmony_ci if (!uctxt) 61662306a36Sopenharmony_ci pollflag = EPOLLERR; 61762306a36Sopenharmony_ci else if (uctxt->poll_type == HFI1_POLL_TYPE_URGENT) 61862306a36Sopenharmony_ci pollflag = poll_urgent(fp, pt); 61962306a36Sopenharmony_ci else if (uctxt->poll_type == HFI1_POLL_TYPE_ANYRCV) 62062306a36Sopenharmony_ci pollflag = poll_next(fp, pt); 62162306a36Sopenharmony_ci else /* invalid */ 62262306a36Sopenharmony_ci pollflag = EPOLLERR; 62362306a36Sopenharmony_ci 62462306a36Sopenharmony_ci return pollflag; 62562306a36Sopenharmony_ci} 62662306a36Sopenharmony_ci 62762306a36Sopenharmony_cistatic int hfi1_file_close(struct inode *inode, struct file *fp) 62862306a36Sopenharmony_ci{ 62962306a36Sopenharmony_ci struct hfi1_filedata *fdata = fp->private_data; 63062306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt = fdata->uctxt; 63162306a36Sopenharmony_ci struct hfi1_devdata *dd = container_of(inode->i_cdev, 63262306a36Sopenharmony_ci struct hfi1_devdata, 63362306a36Sopenharmony_ci user_cdev); 63462306a36Sopenharmony_ci unsigned long flags, *ev; 63562306a36Sopenharmony_ci 63662306a36Sopenharmony_ci fp->private_data = NULL; 63762306a36Sopenharmony_ci 63862306a36Sopenharmony_ci if (!uctxt) 63962306a36Sopenharmony_ci goto done; 64062306a36Sopenharmony_ci 64162306a36Sopenharmony_ci hfi1_cdbg(PROC, "closing ctxt %u:%u", uctxt->ctxt, fdata->subctxt); 64262306a36Sopenharmony_ci 64362306a36Sopenharmony_ci flush_wc(); 64462306a36Sopenharmony_ci /* drain user sdma queue */ 64562306a36Sopenharmony_ci hfi1_user_sdma_free_queues(fdata, uctxt); 64662306a36Sopenharmony_ci 64762306a36Sopenharmony_ci /* release the cpu */ 64862306a36Sopenharmony_ci hfi1_put_proc_affinity(fdata->rec_cpu_num); 64962306a36Sopenharmony_ci 65062306a36Sopenharmony_ci /* clean up rcv side */ 65162306a36Sopenharmony_ci hfi1_user_exp_rcv_free(fdata); 65262306a36Sopenharmony_ci 65362306a36Sopenharmony_ci /* 65462306a36Sopenharmony_ci * fdata->uctxt is used in the above cleanup. It is not ready to be 65562306a36Sopenharmony_ci * removed until here. 65662306a36Sopenharmony_ci */ 65762306a36Sopenharmony_ci fdata->uctxt = NULL; 65862306a36Sopenharmony_ci hfi1_rcd_put(uctxt); 65962306a36Sopenharmony_ci 66062306a36Sopenharmony_ci /* 66162306a36Sopenharmony_ci * Clear any left over, unhandled events so the next process that 66262306a36Sopenharmony_ci * gets this context doesn't get confused. 66362306a36Sopenharmony_ci */ 66462306a36Sopenharmony_ci ev = dd->events + uctxt_offset(uctxt) + fdata->subctxt; 66562306a36Sopenharmony_ci *ev = 0; 66662306a36Sopenharmony_ci 66762306a36Sopenharmony_ci spin_lock_irqsave(&dd->uctxt_lock, flags); 66862306a36Sopenharmony_ci __clear_bit(fdata->subctxt, uctxt->in_use_ctxts); 66962306a36Sopenharmony_ci if (!bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) { 67062306a36Sopenharmony_ci spin_unlock_irqrestore(&dd->uctxt_lock, flags); 67162306a36Sopenharmony_ci goto done; 67262306a36Sopenharmony_ci } 67362306a36Sopenharmony_ci spin_unlock_irqrestore(&dd->uctxt_lock, flags); 67462306a36Sopenharmony_ci 67562306a36Sopenharmony_ci /* 67662306a36Sopenharmony_ci * Disable receive context and interrupt available, reset all 67762306a36Sopenharmony_ci * RcvCtxtCtrl bits to default values. 67862306a36Sopenharmony_ci */ 67962306a36Sopenharmony_ci hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS | 68062306a36Sopenharmony_ci HFI1_RCVCTRL_TIDFLOW_DIS | 68162306a36Sopenharmony_ci HFI1_RCVCTRL_INTRAVAIL_DIS | 68262306a36Sopenharmony_ci HFI1_RCVCTRL_TAILUPD_DIS | 68362306a36Sopenharmony_ci HFI1_RCVCTRL_ONE_PKT_EGR_DIS | 68462306a36Sopenharmony_ci HFI1_RCVCTRL_NO_RHQ_DROP_DIS | 68562306a36Sopenharmony_ci HFI1_RCVCTRL_NO_EGR_DROP_DIS | 68662306a36Sopenharmony_ci HFI1_RCVCTRL_URGENT_DIS, uctxt); 68762306a36Sopenharmony_ci /* Clear the context's J_KEY */ 68862306a36Sopenharmony_ci hfi1_clear_ctxt_jkey(dd, uctxt); 68962306a36Sopenharmony_ci /* 69062306a36Sopenharmony_ci * If a send context is allocated, reset context integrity 69162306a36Sopenharmony_ci * checks to default and disable the send context. 69262306a36Sopenharmony_ci */ 69362306a36Sopenharmony_ci if (uctxt->sc) { 69462306a36Sopenharmony_ci sc_disable(uctxt->sc); 69562306a36Sopenharmony_ci set_pio_integrity(uctxt->sc); 69662306a36Sopenharmony_ci } 69762306a36Sopenharmony_ci 69862306a36Sopenharmony_ci hfi1_free_ctxt_rcv_groups(uctxt); 69962306a36Sopenharmony_ci hfi1_clear_ctxt_pkey(dd, uctxt); 70062306a36Sopenharmony_ci 70162306a36Sopenharmony_ci uctxt->event_flags = 0; 70262306a36Sopenharmony_ci 70362306a36Sopenharmony_ci deallocate_ctxt(uctxt); 70462306a36Sopenharmony_cidone: 70562306a36Sopenharmony_ci 70662306a36Sopenharmony_ci if (refcount_dec_and_test(&dd->user_refcount)) 70762306a36Sopenharmony_ci complete(&dd->user_comp); 70862306a36Sopenharmony_ci 70962306a36Sopenharmony_ci cleanup_srcu_struct(&fdata->pq_srcu); 71062306a36Sopenharmony_ci kfree(fdata); 71162306a36Sopenharmony_ci return 0; 71262306a36Sopenharmony_ci} 71362306a36Sopenharmony_ci 71462306a36Sopenharmony_ci/* 71562306a36Sopenharmony_ci * Convert kernel *virtual* addresses to physical addresses. 71662306a36Sopenharmony_ci * This is used to vmalloc'ed addresses. 71762306a36Sopenharmony_ci */ 71862306a36Sopenharmony_cistatic u64 kvirt_to_phys(void *addr) 71962306a36Sopenharmony_ci{ 72062306a36Sopenharmony_ci struct page *page; 72162306a36Sopenharmony_ci u64 paddr = 0; 72262306a36Sopenharmony_ci 72362306a36Sopenharmony_ci page = vmalloc_to_page(addr); 72462306a36Sopenharmony_ci if (page) 72562306a36Sopenharmony_ci paddr = page_to_pfn(page) << PAGE_SHIFT; 72662306a36Sopenharmony_ci 72762306a36Sopenharmony_ci return paddr; 72862306a36Sopenharmony_ci} 72962306a36Sopenharmony_ci 73062306a36Sopenharmony_ci/** 73162306a36Sopenharmony_ci * complete_subctxt - complete sub-context info 73262306a36Sopenharmony_ci * @fd: valid filedata pointer 73362306a36Sopenharmony_ci * 73462306a36Sopenharmony_ci * Sub-context info can only be set up after the base context 73562306a36Sopenharmony_ci * has been completed. This is indicated by the clearing of the 73662306a36Sopenharmony_ci * HFI1_CTXT_BASE_UINIT bit. 73762306a36Sopenharmony_ci * 73862306a36Sopenharmony_ci * Wait for the bit to be cleared, and then complete the subcontext 73962306a36Sopenharmony_ci * initialization. 74062306a36Sopenharmony_ci * 74162306a36Sopenharmony_ci */ 74262306a36Sopenharmony_cistatic int complete_subctxt(struct hfi1_filedata *fd) 74362306a36Sopenharmony_ci{ 74462306a36Sopenharmony_ci int ret; 74562306a36Sopenharmony_ci unsigned long flags; 74662306a36Sopenharmony_ci 74762306a36Sopenharmony_ci /* 74862306a36Sopenharmony_ci * sub-context info can only be set up after the base context 74962306a36Sopenharmony_ci * has been completed. 75062306a36Sopenharmony_ci */ 75162306a36Sopenharmony_ci ret = wait_event_interruptible( 75262306a36Sopenharmony_ci fd->uctxt->wait, 75362306a36Sopenharmony_ci !test_bit(HFI1_CTXT_BASE_UNINIT, &fd->uctxt->event_flags)); 75462306a36Sopenharmony_ci 75562306a36Sopenharmony_ci if (test_bit(HFI1_CTXT_BASE_FAILED, &fd->uctxt->event_flags)) 75662306a36Sopenharmony_ci ret = -ENOMEM; 75762306a36Sopenharmony_ci 75862306a36Sopenharmony_ci /* Finish the sub-context init */ 75962306a36Sopenharmony_ci if (!ret) { 76062306a36Sopenharmony_ci fd->rec_cpu_num = hfi1_get_proc_affinity(fd->uctxt->numa_id); 76162306a36Sopenharmony_ci ret = init_user_ctxt(fd, fd->uctxt); 76262306a36Sopenharmony_ci } 76362306a36Sopenharmony_ci 76462306a36Sopenharmony_ci if (ret) { 76562306a36Sopenharmony_ci spin_lock_irqsave(&fd->dd->uctxt_lock, flags); 76662306a36Sopenharmony_ci __clear_bit(fd->subctxt, fd->uctxt->in_use_ctxts); 76762306a36Sopenharmony_ci spin_unlock_irqrestore(&fd->dd->uctxt_lock, flags); 76862306a36Sopenharmony_ci hfi1_rcd_put(fd->uctxt); 76962306a36Sopenharmony_ci fd->uctxt = NULL; 77062306a36Sopenharmony_ci } 77162306a36Sopenharmony_ci 77262306a36Sopenharmony_ci return ret; 77362306a36Sopenharmony_ci} 77462306a36Sopenharmony_ci 77562306a36Sopenharmony_cistatic int assign_ctxt(struct hfi1_filedata *fd, unsigned long arg, u32 len) 77662306a36Sopenharmony_ci{ 77762306a36Sopenharmony_ci int ret; 77862306a36Sopenharmony_ci unsigned int swmajor; 77962306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt = NULL; 78062306a36Sopenharmony_ci struct hfi1_user_info uinfo; 78162306a36Sopenharmony_ci 78262306a36Sopenharmony_ci if (fd->uctxt) 78362306a36Sopenharmony_ci return -EINVAL; 78462306a36Sopenharmony_ci 78562306a36Sopenharmony_ci if (sizeof(uinfo) != len) 78662306a36Sopenharmony_ci return -EINVAL; 78762306a36Sopenharmony_ci 78862306a36Sopenharmony_ci if (copy_from_user(&uinfo, (void __user *)arg, sizeof(uinfo))) 78962306a36Sopenharmony_ci return -EFAULT; 79062306a36Sopenharmony_ci 79162306a36Sopenharmony_ci swmajor = uinfo.userversion >> 16; 79262306a36Sopenharmony_ci if (swmajor != HFI1_USER_SWMAJOR) 79362306a36Sopenharmony_ci return -ENODEV; 79462306a36Sopenharmony_ci 79562306a36Sopenharmony_ci if (uinfo.subctxt_cnt > HFI1_MAX_SHARED_CTXTS) 79662306a36Sopenharmony_ci return -EINVAL; 79762306a36Sopenharmony_ci 79862306a36Sopenharmony_ci /* 79962306a36Sopenharmony_ci * Acquire the mutex to protect against multiple creations of what 80062306a36Sopenharmony_ci * could be a shared base context. 80162306a36Sopenharmony_ci */ 80262306a36Sopenharmony_ci mutex_lock(&hfi1_mutex); 80362306a36Sopenharmony_ci /* 80462306a36Sopenharmony_ci * Get a sub context if available (fd->uctxt will be set). 80562306a36Sopenharmony_ci * ret < 0 error, 0 no context, 1 sub-context found 80662306a36Sopenharmony_ci */ 80762306a36Sopenharmony_ci ret = find_sub_ctxt(fd, &uinfo); 80862306a36Sopenharmony_ci 80962306a36Sopenharmony_ci /* 81062306a36Sopenharmony_ci * Allocate a base context if context sharing is not required or a 81162306a36Sopenharmony_ci * sub context wasn't found. 81262306a36Sopenharmony_ci */ 81362306a36Sopenharmony_ci if (!ret) 81462306a36Sopenharmony_ci ret = allocate_ctxt(fd, fd->dd, &uinfo, &uctxt); 81562306a36Sopenharmony_ci 81662306a36Sopenharmony_ci mutex_unlock(&hfi1_mutex); 81762306a36Sopenharmony_ci 81862306a36Sopenharmony_ci /* Depending on the context type, finish the appropriate init */ 81962306a36Sopenharmony_ci switch (ret) { 82062306a36Sopenharmony_ci case 0: 82162306a36Sopenharmony_ci ret = setup_base_ctxt(fd, uctxt); 82262306a36Sopenharmony_ci if (ret) 82362306a36Sopenharmony_ci deallocate_ctxt(uctxt); 82462306a36Sopenharmony_ci break; 82562306a36Sopenharmony_ci case 1: 82662306a36Sopenharmony_ci ret = complete_subctxt(fd); 82762306a36Sopenharmony_ci break; 82862306a36Sopenharmony_ci default: 82962306a36Sopenharmony_ci break; 83062306a36Sopenharmony_ci } 83162306a36Sopenharmony_ci 83262306a36Sopenharmony_ci return ret; 83362306a36Sopenharmony_ci} 83462306a36Sopenharmony_ci 83562306a36Sopenharmony_ci/** 83662306a36Sopenharmony_ci * match_ctxt - match context 83762306a36Sopenharmony_ci * @fd: valid filedata pointer 83862306a36Sopenharmony_ci * @uinfo: user info to compare base context with 83962306a36Sopenharmony_ci * @uctxt: context to compare uinfo to. 84062306a36Sopenharmony_ci * 84162306a36Sopenharmony_ci * Compare the given context with the given information to see if it 84262306a36Sopenharmony_ci * can be used for a sub context. 84362306a36Sopenharmony_ci */ 84462306a36Sopenharmony_cistatic int match_ctxt(struct hfi1_filedata *fd, 84562306a36Sopenharmony_ci const struct hfi1_user_info *uinfo, 84662306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt) 84762306a36Sopenharmony_ci{ 84862306a36Sopenharmony_ci struct hfi1_devdata *dd = fd->dd; 84962306a36Sopenharmony_ci unsigned long flags; 85062306a36Sopenharmony_ci u16 subctxt; 85162306a36Sopenharmony_ci 85262306a36Sopenharmony_ci /* Skip dynamically allocated kernel contexts */ 85362306a36Sopenharmony_ci if (uctxt->sc && (uctxt->sc->type == SC_KERNEL)) 85462306a36Sopenharmony_ci return 0; 85562306a36Sopenharmony_ci 85662306a36Sopenharmony_ci /* Skip ctxt if it doesn't match the requested one */ 85762306a36Sopenharmony_ci if (memcmp(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid)) || 85862306a36Sopenharmony_ci uctxt->jkey != generate_jkey(current_uid()) || 85962306a36Sopenharmony_ci uctxt->subctxt_id != uinfo->subctxt_id || 86062306a36Sopenharmony_ci uctxt->subctxt_cnt != uinfo->subctxt_cnt) 86162306a36Sopenharmony_ci return 0; 86262306a36Sopenharmony_ci 86362306a36Sopenharmony_ci /* Verify the sharing process matches the base */ 86462306a36Sopenharmony_ci if (uctxt->userversion != uinfo->userversion) 86562306a36Sopenharmony_ci return -EINVAL; 86662306a36Sopenharmony_ci 86762306a36Sopenharmony_ci /* Find an unused sub context */ 86862306a36Sopenharmony_ci spin_lock_irqsave(&dd->uctxt_lock, flags); 86962306a36Sopenharmony_ci if (bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) { 87062306a36Sopenharmony_ci /* context is being closed, do not use */ 87162306a36Sopenharmony_ci spin_unlock_irqrestore(&dd->uctxt_lock, flags); 87262306a36Sopenharmony_ci return 0; 87362306a36Sopenharmony_ci } 87462306a36Sopenharmony_ci 87562306a36Sopenharmony_ci subctxt = find_first_zero_bit(uctxt->in_use_ctxts, 87662306a36Sopenharmony_ci HFI1_MAX_SHARED_CTXTS); 87762306a36Sopenharmony_ci if (subctxt >= uctxt->subctxt_cnt) { 87862306a36Sopenharmony_ci spin_unlock_irqrestore(&dd->uctxt_lock, flags); 87962306a36Sopenharmony_ci return -EBUSY; 88062306a36Sopenharmony_ci } 88162306a36Sopenharmony_ci 88262306a36Sopenharmony_ci fd->subctxt = subctxt; 88362306a36Sopenharmony_ci __set_bit(fd->subctxt, uctxt->in_use_ctxts); 88462306a36Sopenharmony_ci spin_unlock_irqrestore(&dd->uctxt_lock, flags); 88562306a36Sopenharmony_ci 88662306a36Sopenharmony_ci fd->uctxt = uctxt; 88762306a36Sopenharmony_ci hfi1_rcd_get(uctxt); 88862306a36Sopenharmony_ci 88962306a36Sopenharmony_ci return 1; 89062306a36Sopenharmony_ci} 89162306a36Sopenharmony_ci 89262306a36Sopenharmony_ci/** 89362306a36Sopenharmony_ci * find_sub_ctxt - fund sub-context 89462306a36Sopenharmony_ci * @fd: valid filedata pointer 89562306a36Sopenharmony_ci * @uinfo: matching info to use to find a possible context to share. 89662306a36Sopenharmony_ci * 89762306a36Sopenharmony_ci * The hfi1_mutex must be held when this function is called. It is 89862306a36Sopenharmony_ci * necessary to ensure serialized creation of shared contexts. 89962306a36Sopenharmony_ci * 90062306a36Sopenharmony_ci * Return: 90162306a36Sopenharmony_ci * 0 No sub-context found 90262306a36Sopenharmony_ci * 1 Subcontext found and allocated 90362306a36Sopenharmony_ci * errno EINVAL (incorrect parameters) 90462306a36Sopenharmony_ci * EBUSY (all sub contexts in use) 90562306a36Sopenharmony_ci */ 90662306a36Sopenharmony_cistatic int find_sub_ctxt(struct hfi1_filedata *fd, 90762306a36Sopenharmony_ci const struct hfi1_user_info *uinfo) 90862306a36Sopenharmony_ci{ 90962306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt; 91062306a36Sopenharmony_ci struct hfi1_devdata *dd = fd->dd; 91162306a36Sopenharmony_ci u16 i; 91262306a36Sopenharmony_ci int ret; 91362306a36Sopenharmony_ci 91462306a36Sopenharmony_ci if (!uinfo->subctxt_cnt) 91562306a36Sopenharmony_ci return 0; 91662306a36Sopenharmony_ci 91762306a36Sopenharmony_ci for (i = dd->first_dyn_alloc_ctxt; i < dd->num_rcv_contexts; i++) { 91862306a36Sopenharmony_ci uctxt = hfi1_rcd_get_by_index(dd, i); 91962306a36Sopenharmony_ci if (uctxt) { 92062306a36Sopenharmony_ci ret = match_ctxt(fd, uinfo, uctxt); 92162306a36Sopenharmony_ci hfi1_rcd_put(uctxt); 92262306a36Sopenharmony_ci /* value of != 0 will return */ 92362306a36Sopenharmony_ci if (ret) 92462306a36Sopenharmony_ci return ret; 92562306a36Sopenharmony_ci } 92662306a36Sopenharmony_ci } 92762306a36Sopenharmony_ci 92862306a36Sopenharmony_ci return 0; 92962306a36Sopenharmony_ci} 93062306a36Sopenharmony_ci 93162306a36Sopenharmony_cistatic int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd, 93262306a36Sopenharmony_ci struct hfi1_user_info *uinfo, 93362306a36Sopenharmony_ci struct hfi1_ctxtdata **rcd) 93462306a36Sopenharmony_ci{ 93562306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt; 93662306a36Sopenharmony_ci int ret, numa; 93762306a36Sopenharmony_ci 93862306a36Sopenharmony_ci if (dd->flags & HFI1_FROZEN) { 93962306a36Sopenharmony_ci /* 94062306a36Sopenharmony_ci * Pick an error that is unique from all other errors 94162306a36Sopenharmony_ci * that are returned so the user process knows that 94262306a36Sopenharmony_ci * it tried to allocate while the SPC was frozen. It 94362306a36Sopenharmony_ci * it should be able to retry with success in a short 94462306a36Sopenharmony_ci * while. 94562306a36Sopenharmony_ci */ 94662306a36Sopenharmony_ci return -EIO; 94762306a36Sopenharmony_ci } 94862306a36Sopenharmony_ci 94962306a36Sopenharmony_ci if (!dd->freectxts) 95062306a36Sopenharmony_ci return -EBUSY; 95162306a36Sopenharmony_ci 95262306a36Sopenharmony_ci /* 95362306a36Sopenharmony_ci * If we don't have a NUMA node requested, preference is towards 95462306a36Sopenharmony_ci * device NUMA node. 95562306a36Sopenharmony_ci */ 95662306a36Sopenharmony_ci fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node); 95762306a36Sopenharmony_ci if (fd->rec_cpu_num != -1) 95862306a36Sopenharmony_ci numa = cpu_to_node(fd->rec_cpu_num); 95962306a36Sopenharmony_ci else 96062306a36Sopenharmony_ci numa = numa_node_id(); 96162306a36Sopenharmony_ci ret = hfi1_create_ctxtdata(dd->pport, numa, &uctxt); 96262306a36Sopenharmony_ci if (ret < 0) { 96362306a36Sopenharmony_ci dd_dev_err(dd, "user ctxtdata allocation failed\n"); 96462306a36Sopenharmony_ci return ret; 96562306a36Sopenharmony_ci } 96662306a36Sopenharmony_ci hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)", 96762306a36Sopenharmony_ci uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num, 96862306a36Sopenharmony_ci uctxt->numa_id); 96962306a36Sopenharmony_ci 97062306a36Sopenharmony_ci /* 97162306a36Sopenharmony_ci * Allocate and enable a PIO send context. 97262306a36Sopenharmony_ci */ 97362306a36Sopenharmony_ci uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize, dd->node); 97462306a36Sopenharmony_ci if (!uctxt->sc) { 97562306a36Sopenharmony_ci ret = -ENOMEM; 97662306a36Sopenharmony_ci goto ctxdata_free; 97762306a36Sopenharmony_ci } 97862306a36Sopenharmony_ci hfi1_cdbg(PROC, "allocated send context %u(%u)", uctxt->sc->sw_index, 97962306a36Sopenharmony_ci uctxt->sc->hw_context); 98062306a36Sopenharmony_ci ret = sc_enable(uctxt->sc); 98162306a36Sopenharmony_ci if (ret) 98262306a36Sopenharmony_ci goto ctxdata_free; 98362306a36Sopenharmony_ci 98462306a36Sopenharmony_ci /* 98562306a36Sopenharmony_ci * Setup sub context information if the user-level has requested 98662306a36Sopenharmony_ci * sub contexts. 98762306a36Sopenharmony_ci * This has to be done here so the rest of the sub-contexts find the 98862306a36Sopenharmony_ci * proper base context. 98962306a36Sopenharmony_ci * NOTE: _set_bit() can be used here because the context creation is 99062306a36Sopenharmony_ci * protected by the mutex (rather than the spin_lock), and will be the 99162306a36Sopenharmony_ci * very first instance of this context. 99262306a36Sopenharmony_ci */ 99362306a36Sopenharmony_ci __set_bit(0, uctxt->in_use_ctxts); 99462306a36Sopenharmony_ci if (uinfo->subctxt_cnt) 99562306a36Sopenharmony_ci init_subctxts(uctxt, uinfo); 99662306a36Sopenharmony_ci uctxt->userversion = uinfo->userversion; 99762306a36Sopenharmony_ci uctxt->flags = hfi1_cap_mask; /* save current flag state */ 99862306a36Sopenharmony_ci init_waitqueue_head(&uctxt->wait); 99962306a36Sopenharmony_ci strscpy(uctxt->comm, current->comm, sizeof(uctxt->comm)); 100062306a36Sopenharmony_ci memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid)); 100162306a36Sopenharmony_ci uctxt->jkey = generate_jkey(current_uid()); 100262306a36Sopenharmony_ci hfi1_stats.sps_ctxts++; 100362306a36Sopenharmony_ci /* 100462306a36Sopenharmony_ci * Disable ASPM when there are open user/PSM contexts to avoid 100562306a36Sopenharmony_ci * issues with ASPM L1 exit latency 100662306a36Sopenharmony_ci */ 100762306a36Sopenharmony_ci if (dd->freectxts-- == dd->num_user_contexts) 100862306a36Sopenharmony_ci aspm_disable_all(dd); 100962306a36Sopenharmony_ci 101062306a36Sopenharmony_ci *rcd = uctxt; 101162306a36Sopenharmony_ci 101262306a36Sopenharmony_ci return 0; 101362306a36Sopenharmony_ci 101462306a36Sopenharmony_cictxdata_free: 101562306a36Sopenharmony_ci hfi1_free_ctxt(uctxt); 101662306a36Sopenharmony_ci return ret; 101762306a36Sopenharmony_ci} 101862306a36Sopenharmony_ci 101962306a36Sopenharmony_cistatic void deallocate_ctxt(struct hfi1_ctxtdata *uctxt) 102062306a36Sopenharmony_ci{ 102162306a36Sopenharmony_ci mutex_lock(&hfi1_mutex); 102262306a36Sopenharmony_ci hfi1_stats.sps_ctxts--; 102362306a36Sopenharmony_ci if (++uctxt->dd->freectxts == uctxt->dd->num_user_contexts) 102462306a36Sopenharmony_ci aspm_enable_all(uctxt->dd); 102562306a36Sopenharmony_ci mutex_unlock(&hfi1_mutex); 102662306a36Sopenharmony_ci 102762306a36Sopenharmony_ci hfi1_free_ctxt(uctxt); 102862306a36Sopenharmony_ci} 102962306a36Sopenharmony_ci 103062306a36Sopenharmony_cistatic void init_subctxts(struct hfi1_ctxtdata *uctxt, 103162306a36Sopenharmony_ci const struct hfi1_user_info *uinfo) 103262306a36Sopenharmony_ci{ 103362306a36Sopenharmony_ci uctxt->subctxt_cnt = uinfo->subctxt_cnt; 103462306a36Sopenharmony_ci uctxt->subctxt_id = uinfo->subctxt_id; 103562306a36Sopenharmony_ci set_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags); 103662306a36Sopenharmony_ci} 103762306a36Sopenharmony_ci 103862306a36Sopenharmony_cistatic int setup_subctxt(struct hfi1_ctxtdata *uctxt) 103962306a36Sopenharmony_ci{ 104062306a36Sopenharmony_ci int ret = 0; 104162306a36Sopenharmony_ci u16 num_subctxts = uctxt->subctxt_cnt; 104262306a36Sopenharmony_ci 104362306a36Sopenharmony_ci uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE); 104462306a36Sopenharmony_ci if (!uctxt->subctxt_uregbase) 104562306a36Sopenharmony_ci return -ENOMEM; 104662306a36Sopenharmony_ci 104762306a36Sopenharmony_ci /* We can take the size of the RcvHdr Queue from the master */ 104862306a36Sopenharmony_ci uctxt->subctxt_rcvhdr_base = vmalloc_user(rcvhdrq_size(uctxt) * 104962306a36Sopenharmony_ci num_subctxts); 105062306a36Sopenharmony_ci if (!uctxt->subctxt_rcvhdr_base) { 105162306a36Sopenharmony_ci ret = -ENOMEM; 105262306a36Sopenharmony_ci goto bail_ureg; 105362306a36Sopenharmony_ci } 105462306a36Sopenharmony_ci 105562306a36Sopenharmony_ci uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size * 105662306a36Sopenharmony_ci num_subctxts); 105762306a36Sopenharmony_ci if (!uctxt->subctxt_rcvegrbuf) { 105862306a36Sopenharmony_ci ret = -ENOMEM; 105962306a36Sopenharmony_ci goto bail_rhdr; 106062306a36Sopenharmony_ci } 106162306a36Sopenharmony_ci 106262306a36Sopenharmony_ci return 0; 106362306a36Sopenharmony_ci 106462306a36Sopenharmony_cibail_rhdr: 106562306a36Sopenharmony_ci vfree(uctxt->subctxt_rcvhdr_base); 106662306a36Sopenharmony_ci uctxt->subctxt_rcvhdr_base = NULL; 106762306a36Sopenharmony_cibail_ureg: 106862306a36Sopenharmony_ci vfree(uctxt->subctxt_uregbase); 106962306a36Sopenharmony_ci uctxt->subctxt_uregbase = NULL; 107062306a36Sopenharmony_ci 107162306a36Sopenharmony_ci return ret; 107262306a36Sopenharmony_ci} 107362306a36Sopenharmony_ci 107462306a36Sopenharmony_cistatic void user_init(struct hfi1_ctxtdata *uctxt) 107562306a36Sopenharmony_ci{ 107662306a36Sopenharmony_ci unsigned int rcvctrl_ops = 0; 107762306a36Sopenharmony_ci 107862306a36Sopenharmony_ci /* initialize poll variables... */ 107962306a36Sopenharmony_ci uctxt->urgent = 0; 108062306a36Sopenharmony_ci uctxt->urgent_poll = 0; 108162306a36Sopenharmony_ci 108262306a36Sopenharmony_ci /* 108362306a36Sopenharmony_ci * Now enable the ctxt for receive. 108462306a36Sopenharmony_ci * For chips that are set to DMA the tail register to memory 108562306a36Sopenharmony_ci * when they change (and when the update bit transitions from 108662306a36Sopenharmony_ci * 0 to 1. So for those chips, we turn it off and then back on. 108762306a36Sopenharmony_ci * This will (very briefly) affect any other open ctxts, but the 108862306a36Sopenharmony_ci * duration is very short, and therefore isn't an issue. We 108962306a36Sopenharmony_ci * explicitly set the in-memory tail copy to 0 beforehand, so we 109062306a36Sopenharmony_ci * don't have to wait to be sure the DMA update has happened 109162306a36Sopenharmony_ci * (chip resets head/tail to 0 on transition to enable). 109262306a36Sopenharmony_ci */ 109362306a36Sopenharmony_ci if (hfi1_rcvhdrtail_kvaddr(uctxt)) 109462306a36Sopenharmony_ci clear_rcvhdrtail(uctxt); 109562306a36Sopenharmony_ci 109662306a36Sopenharmony_ci /* Setup J_KEY before enabling the context */ 109762306a36Sopenharmony_ci hfi1_set_ctxt_jkey(uctxt->dd, uctxt, uctxt->jkey); 109862306a36Sopenharmony_ci 109962306a36Sopenharmony_ci rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB; 110062306a36Sopenharmony_ci rcvctrl_ops |= HFI1_RCVCTRL_URGENT_ENB; 110162306a36Sopenharmony_ci if (HFI1_CAP_UGET_MASK(uctxt->flags, HDRSUPP)) 110262306a36Sopenharmony_ci rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB; 110362306a36Sopenharmony_ci /* 110462306a36Sopenharmony_ci * Ignore the bit in the flags for now until proper 110562306a36Sopenharmony_ci * support for multiple packet per rcv array entry is 110662306a36Sopenharmony_ci * added. 110762306a36Sopenharmony_ci */ 110862306a36Sopenharmony_ci if (!HFI1_CAP_UGET_MASK(uctxt->flags, MULTI_PKT_EGR)) 110962306a36Sopenharmony_ci rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB; 111062306a36Sopenharmony_ci if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_EGR_FULL)) 111162306a36Sopenharmony_ci rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB; 111262306a36Sopenharmony_ci if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_RHQ_FULL)) 111362306a36Sopenharmony_ci rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB; 111462306a36Sopenharmony_ci /* 111562306a36Sopenharmony_ci * The RcvCtxtCtrl.TailUpd bit has to be explicitly written. 111662306a36Sopenharmony_ci * We can't rely on the correct value to be set from prior 111762306a36Sopenharmony_ci * uses of the chip or ctxt. Therefore, add the rcvctrl op 111862306a36Sopenharmony_ci * for both cases. 111962306a36Sopenharmony_ci */ 112062306a36Sopenharmony_ci if (HFI1_CAP_UGET_MASK(uctxt->flags, DMA_RTAIL)) 112162306a36Sopenharmony_ci rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB; 112262306a36Sopenharmony_ci else 112362306a36Sopenharmony_ci rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS; 112462306a36Sopenharmony_ci hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt); 112562306a36Sopenharmony_ci} 112662306a36Sopenharmony_ci 112762306a36Sopenharmony_cistatic int get_ctxt_info(struct hfi1_filedata *fd, unsigned long arg, u32 len) 112862306a36Sopenharmony_ci{ 112962306a36Sopenharmony_ci struct hfi1_ctxt_info cinfo; 113062306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt = fd->uctxt; 113162306a36Sopenharmony_ci 113262306a36Sopenharmony_ci if (sizeof(cinfo) != len) 113362306a36Sopenharmony_ci return -EINVAL; 113462306a36Sopenharmony_ci 113562306a36Sopenharmony_ci memset(&cinfo, 0, sizeof(cinfo)); 113662306a36Sopenharmony_ci cinfo.runtime_flags = (((uctxt->flags >> HFI1_CAP_MISC_SHIFT) & 113762306a36Sopenharmony_ci HFI1_CAP_MISC_MASK) << HFI1_CAP_USER_SHIFT) | 113862306a36Sopenharmony_ci HFI1_CAP_UGET_MASK(uctxt->flags, MASK) | 113962306a36Sopenharmony_ci HFI1_CAP_KGET_MASK(uctxt->flags, K2U); 114062306a36Sopenharmony_ci /* adjust flag if this fd is not able to cache */ 114162306a36Sopenharmony_ci if (!fd->use_mn) 114262306a36Sopenharmony_ci cinfo.runtime_flags |= HFI1_CAP_TID_UNMAP; /* no caching */ 114362306a36Sopenharmony_ci 114462306a36Sopenharmony_ci cinfo.num_active = hfi1_count_active_units(); 114562306a36Sopenharmony_ci cinfo.unit = uctxt->dd->unit; 114662306a36Sopenharmony_ci cinfo.ctxt = uctxt->ctxt; 114762306a36Sopenharmony_ci cinfo.subctxt = fd->subctxt; 114862306a36Sopenharmony_ci cinfo.rcvtids = roundup(uctxt->egrbufs.alloced, 114962306a36Sopenharmony_ci uctxt->dd->rcv_entries.group_size) + 115062306a36Sopenharmony_ci uctxt->expected_count; 115162306a36Sopenharmony_ci cinfo.credits = uctxt->sc->credits; 115262306a36Sopenharmony_ci cinfo.numa_node = uctxt->numa_id; 115362306a36Sopenharmony_ci cinfo.rec_cpu = fd->rec_cpu_num; 115462306a36Sopenharmony_ci cinfo.send_ctxt = uctxt->sc->hw_context; 115562306a36Sopenharmony_ci 115662306a36Sopenharmony_ci cinfo.egrtids = uctxt->egrbufs.alloced; 115762306a36Sopenharmony_ci cinfo.rcvhdrq_cnt = get_hdrq_cnt(uctxt); 115862306a36Sopenharmony_ci cinfo.rcvhdrq_entsize = get_hdrqentsize(uctxt) << 2; 115962306a36Sopenharmony_ci cinfo.sdma_ring_size = fd->cq->nentries; 116062306a36Sopenharmony_ci cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size; 116162306a36Sopenharmony_ci 116262306a36Sopenharmony_ci trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, &cinfo); 116362306a36Sopenharmony_ci if (copy_to_user((void __user *)arg, &cinfo, len)) 116462306a36Sopenharmony_ci return -EFAULT; 116562306a36Sopenharmony_ci 116662306a36Sopenharmony_ci return 0; 116762306a36Sopenharmony_ci} 116862306a36Sopenharmony_ci 116962306a36Sopenharmony_cistatic int init_user_ctxt(struct hfi1_filedata *fd, 117062306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt) 117162306a36Sopenharmony_ci{ 117262306a36Sopenharmony_ci int ret; 117362306a36Sopenharmony_ci 117462306a36Sopenharmony_ci ret = hfi1_user_sdma_alloc_queues(uctxt, fd); 117562306a36Sopenharmony_ci if (ret) 117662306a36Sopenharmony_ci return ret; 117762306a36Sopenharmony_ci 117862306a36Sopenharmony_ci ret = hfi1_user_exp_rcv_init(fd, uctxt); 117962306a36Sopenharmony_ci if (ret) 118062306a36Sopenharmony_ci hfi1_user_sdma_free_queues(fd, uctxt); 118162306a36Sopenharmony_ci 118262306a36Sopenharmony_ci return ret; 118362306a36Sopenharmony_ci} 118462306a36Sopenharmony_ci 118562306a36Sopenharmony_cistatic int setup_base_ctxt(struct hfi1_filedata *fd, 118662306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt) 118762306a36Sopenharmony_ci{ 118862306a36Sopenharmony_ci struct hfi1_devdata *dd = uctxt->dd; 118962306a36Sopenharmony_ci int ret = 0; 119062306a36Sopenharmony_ci 119162306a36Sopenharmony_ci hfi1_init_ctxt(uctxt->sc); 119262306a36Sopenharmony_ci 119362306a36Sopenharmony_ci /* Now allocate the RcvHdr queue and eager buffers. */ 119462306a36Sopenharmony_ci ret = hfi1_create_rcvhdrq(dd, uctxt); 119562306a36Sopenharmony_ci if (ret) 119662306a36Sopenharmony_ci goto done; 119762306a36Sopenharmony_ci 119862306a36Sopenharmony_ci ret = hfi1_setup_eagerbufs(uctxt); 119962306a36Sopenharmony_ci if (ret) 120062306a36Sopenharmony_ci goto done; 120162306a36Sopenharmony_ci 120262306a36Sopenharmony_ci /* If sub-contexts are enabled, do the appropriate setup */ 120362306a36Sopenharmony_ci if (uctxt->subctxt_cnt) 120462306a36Sopenharmony_ci ret = setup_subctxt(uctxt); 120562306a36Sopenharmony_ci if (ret) 120662306a36Sopenharmony_ci goto done; 120762306a36Sopenharmony_ci 120862306a36Sopenharmony_ci ret = hfi1_alloc_ctxt_rcv_groups(uctxt); 120962306a36Sopenharmony_ci if (ret) 121062306a36Sopenharmony_ci goto done; 121162306a36Sopenharmony_ci 121262306a36Sopenharmony_ci ret = init_user_ctxt(fd, uctxt); 121362306a36Sopenharmony_ci if (ret) { 121462306a36Sopenharmony_ci hfi1_free_ctxt_rcv_groups(uctxt); 121562306a36Sopenharmony_ci goto done; 121662306a36Sopenharmony_ci } 121762306a36Sopenharmony_ci 121862306a36Sopenharmony_ci user_init(uctxt); 121962306a36Sopenharmony_ci 122062306a36Sopenharmony_ci /* Now that the context is set up, the fd can get a reference. */ 122162306a36Sopenharmony_ci fd->uctxt = uctxt; 122262306a36Sopenharmony_ci hfi1_rcd_get(uctxt); 122362306a36Sopenharmony_ci 122462306a36Sopenharmony_cidone: 122562306a36Sopenharmony_ci if (uctxt->subctxt_cnt) { 122662306a36Sopenharmony_ci /* 122762306a36Sopenharmony_ci * On error, set the failed bit so sub-contexts will clean up 122862306a36Sopenharmony_ci * correctly. 122962306a36Sopenharmony_ci */ 123062306a36Sopenharmony_ci if (ret) 123162306a36Sopenharmony_ci set_bit(HFI1_CTXT_BASE_FAILED, &uctxt->event_flags); 123262306a36Sopenharmony_ci 123362306a36Sopenharmony_ci /* 123462306a36Sopenharmony_ci * Base context is done (successfully or not), notify anybody 123562306a36Sopenharmony_ci * using a sub-context that is waiting for this completion. 123662306a36Sopenharmony_ci */ 123762306a36Sopenharmony_ci clear_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags); 123862306a36Sopenharmony_ci wake_up(&uctxt->wait); 123962306a36Sopenharmony_ci } 124062306a36Sopenharmony_ci 124162306a36Sopenharmony_ci return ret; 124262306a36Sopenharmony_ci} 124362306a36Sopenharmony_ci 124462306a36Sopenharmony_cistatic int get_base_info(struct hfi1_filedata *fd, unsigned long arg, u32 len) 124562306a36Sopenharmony_ci{ 124662306a36Sopenharmony_ci struct hfi1_base_info binfo; 124762306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt = fd->uctxt; 124862306a36Sopenharmony_ci struct hfi1_devdata *dd = uctxt->dd; 124962306a36Sopenharmony_ci unsigned offset; 125062306a36Sopenharmony_ci 125162306a36Sopenharmony_ci trace_hfi1_uctxtdata(uctxt->dd, uctxt, fd->subctxt); 125262306a36Sopenharmony_ci 125362306a36Sopenharmony_ci if (sizeof(binfo) != len) 125462306a36Sopenharmony_ci return -EINVAL; 125562306a36Sopenharmony_ci 125662306a36Sopenharmony_ci memset(&binfo, 0, sizeof(binfo)); 125762306a36Sopenharmony_ci binfo.hw_version = dd->revision; 125862306a36Sopenharmony_ci binfo.sw_version = HFI1_USER_SWVERSION; 125962306a36Sopenharmony_ci binfo.bthqp = RVT_KDETH_QP_PREFIX; 126062306a36Sopenharmony_ci binfo.jkey = uctxt->jkey; 126162306a36Sopenharmony_ci /* 126262306a36Sopenharmony_ci * If more than 64 contexts are enabled the allocated credit 126362306a36Sopenharmony_ci * return will span two or three contiguous pages. Since we only 126462306a36Sopenharmony_ci * map the page containing the context's credit return address, 126562306a36Sopenharmony_ci * we need to calculate the offset in the proper page. 126662306a36Sopenharmony_ci */ 126762306a36Sopenharmony_ci offset = ((u64)uctxt->sc->hw_free - 126862306a36Sopenharmony_ci (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE; 126962306a36Sopenharmony_ci binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt, 127062306a36Sopenharmony_ci fd->subctxt, offset); 127162306a36Sopenharmony_ci binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt, 127262306a36Sopenharmony_ci fd->subctxt, 127362306a36Sopenharmony_ci uctxt->sc->base_addr); 127462306a36Sopenharmony_ci binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP, 127562306a36Sopenharmony_ci uctxt->ctxt, 127662306a36Sopenharmony_ci fd->subctxt, 127762306a36Sopenharmony_ci uctxt->sc->base_addr); 127862306a36Sopenharmony_ci binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt, 127962306a36Sopenharmony_ci fd->subctxt, 128062306a36Sopenharmony_ci uctxt->rcvhdrq); 128162306a36Sopenharmony_ci binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt, 128262306a36Sopenharmony_ci fd->subctxt, 128362306a36Sopenharmony_ci uctxt->egrbufs.rcvtids[0].dma); 128462306a36Sopenharmony_ci binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt, 128562306a36Sopenharmony_ci fd->subctxt, 0); 128662306a36Sopenharmony_ci /* 128762306a36Sopenharmony_ci * user regs are at 128862306a36Sopenharmony_ci * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE)) 128962306a36Sopenharmony_ci */ 129062306a36Sopenharmony_ci binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt, 129162306a36Sopenharmony_ci fd->subctxt, 0); 129262306a36Sopenharmony_ci offset = offset_in_page((uctxt_offset(uctxt) + fd->subctxt) * 129362306a36Sopenharmony_ci sizeof(*dd->events)); 129462306a36Sopenharmony_ci binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt, 129562306a36Sopenharmony_ci fd->subctxt, 129662306a36Sopenharmony_ci offset); 129762306a36Sopenharmony_ci binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt, 129862306a36Sopenharmony_ci fd->subctxt, 129962306a36Sopenharmony_ci dd->status); 130062306a36Sopenharmony_ci if (HFI1_CAP_IS_USET(DMA_RTAIL)) 130162306a36Sopenharmony_ci binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt, 130262306a36Sopenharmony_ci fd->subctxt, 0); 130362306a36Sopenharmony_ci if (uctxt->subctxt_cnt) { 130462306a36Sopenharmony_ci binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS, 130562306a36Sopenharmony_ci uctxt->ctxt, 130662306a36Sopenharmony_ci fd->subctxt, 0); 130762306a36Sopenharmony_ci binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ, 130862306a36Sopenharmony_ci uctxt->ctxt, 130962306a36Sopenharmony_ci fd->subctxt, 0); 131062306a36Sopenharmony_ci binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF, 131162306a36Sopenharmony_ci uctxt->ctxt, 131262306a36Sopenharmony_ci fd->subctxt, 0); 131362306a36Sopenharmony_ci } 131462306a36Sopenharmony_ci 131562306a36Sopenharmony_ci if (copy_to_user((void __user *)arg, &binfo, len)) 131662306a36Sopenharmony_ci return -EFAULT; 131762306a36Sopenharmony_ci 131862306a36Sopenharmony_ci return 0; 131962306a36Sopenharmony_ci} 132062306a36Sopenharmony_ci 132162306a36Sopenharmony_ci/** 132262306a36Sopenharmony_ci * user_exp_rcv_setup - Set up the given tid rcv list 132362306a36Sopenharmony_ci * @fd: file data of the current driver instance 132462306a36Sopenharmony_ci * @arg: ioctl argumnent for user space information 132562306a36Sopenharmony_ci * @len: length of data structure associated with ioctl command 132662306a36Sopenharmony_ci * 132762306a36Sopenharmony_ci * Wrapper to validate ioctl information before doing _rcv_setup. 132862306a36Sopenharmony_ci * 132962306a36Sopenharmony_ci */ 133062306a36Sopenharmony_cistatic int user_exp_rcv_setup(struct hfi1_filedata *fd, unsigned long arg, 133162306a36Sopenharmony_ci u32 len) 133262306a36Sopenharmony_ci{ 133362306a36Sopenharmony_ci int ret; 133462306a36Sopenharmony_ci unsigned long addr; 133562306a36Sopenharmony_ci struct hfi1_tid_info tinfo; 133662306a36Sopenharmony_ci 133762306a36Sopenharmony_ci if (sizeof(tinfo) != len) 133862306a36Sopenharmony_ci return -EINVAL; 133962306a36Sopenharmony_ci 134062306a36Sopenharmony_ci if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo)))) 134162306a36Sopenharmony_ci return -EFAULT; 134262306a36Sopenharmony_ci 134362306a36Sopenharmony_ci ret = hfi1_user_exp_rcv_setup(fd, &tinfo); 134462306a36Sopenharmony_ci if (!ret) { 134562306a36Sopenharmony_ci /* 134662306a36Sopenharmony_ci * Copy the number of tidlist entries we used 134762306a36Sopenharmony_ci * and the length of the buffer we registered. 134862306a36Sopenharmony_ci */ 134962306a36Sopenharmony_ci addr = arg + offsetof(struct hfi1_tid_info, tidcnt); 135062306a36Sopenharmony_ci if (copy_to_user((void __user *)addr, &tinfo.tidcnt, 135162306a36Sopenharmony_ci sizeof(tinfo.tidcnt))) 135262306a36Sopenharmony_ci ret = -EFAULT; 135362306a36Sopenharmony_ci 135462306a36Sopenharmony_ci addr = arg + offsetof(struct hfi1_tid_info, length); 135562306a36Sopenharmony_ci if (!ret && copy_to_user((void __user *)addr, &tinfo.length, 135662306a36Sopenharmony_ci sizeof(tinfo.length))) 135762306a36Sopenharmony_ci ret = -EFAULT; 135862306a36Sopenharmony_ci 135962306a36Sopenharmony_ci if (ret) 136062306a36Sopenharmony_ci hfi1_user_exp_rcv_invalid(fd, &tinfo); 136162306a36Sopenharmony_ci } 136262306a36Sopenharmony_ci 136362306a36Sopenharmony_ci return ret; 136462306a36Sopenharmony_ci} 136562306a36Sopenharmony_ci 136662306a36Sopenharmony_ci/** 136762306a36Sopenharmony_ci * user_exp_rcv_clear - Clear the given tid rcv list 136862306a36Sopenharmony_ci * @fd: file data of the current driver instance 136962306a36Sopenharmony_ci * @arg: ioctl argumnent for user space information 137062306a36Sopenharmony_ci * @len: length of data structure associated with ioctl command 137162306a36Sopenharmony_ci * 137262306a36Sopenharmony_ci * The hfi1_user_exp_rcv_clear() can be called from the error path. Because 137362306a36Sopenharmony_ci * of this, we need to use this wrapper to copy the user space information 137462306a36Sopenharmony_ci * before doing the clear. 137562306a36Sopenharmony_ci */ 137662306a36Sopenharmony_cistatic int user_exp_rcv_clear(struct hfi1_filedata *fd, unsigned long arg, 137762306a36Sopenharmony_ci u32 len) 137862306a36Sopenharmony_ci{ 137962306a36Sopenharmony_ci int ret; 138062306a36Sopenharmony_ci unsigned long addr; 138162306a36Sopenharmony_ci struct hfi1_tid_info tinfo; 138262306a36Sopenharmony_ci 138362306a36Sopenharmony_ci if (sizeof(tinfo) != len) 138462306a36Sopenharmony_ci return -EINVAL; 138562306a36Sopenharmony_ci 138662306a36Sopenharmony_ci if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo)))) 138762306a36Sopenharmony_ci return -EFAULT; 138862306a36Sopenharmony_ci 138962306a36Sopenharmony_ci ret = hfi1_user_exp_rcv_clear(fd, &tinfo); 139062306a36Sopenharmony_ci if (!ret) { 139162306a36Sopenharmony_ci addr = arg + offsetof(struct hfi1_tid_info, tidcnt); 139262306a36Sopenharmony_ci if (copy_to_user((void __user *)addr, &tinfo.tidcnt, 139362306a36Sopenharmony_ci sizeof(tinfo.tidcnt))) 139462306a36Sopenharmony_ci return -EFAULT; 139562306a36Sopenharmony_ci } 139662306a36Sopenharmony_ci 139762306a36Sopenharmony_ci return ret; 139862306a36Sopenharmony_ci} 139962306a36Sopenharmony_ci 140062306a36Sopenharmony_ci/** 140162306a36Sopenharmony_ci * user_exp_rcv_invalid - Invalidate the given tid rcv list 140262306a36Sopenharmony_ci * @fd: file data of the current driver instance 140362306a36Sopenharmony_ci * @arg: ioctl argumnent for user space information 140462306a36Sopenharmony_ci * @len: length of data structure associated with ioctl command 140562306a36Sopenharmony_ci * 140662306a36Sopenharmony_ci * Wrapper to validate ioctl information before doing _rcv_invalid. 140762306a36Sopenharmony_ci * 140862306a36Sopenharmony_ci */ 140962306a36Sopenharmony_cistatic int user_exp_rcv_invalid(struct hfi1_filedata *fd, unsigned long arg, 141062306a36Sopenharmony_ci u32 len) 141162306a36Sopenharmony_ci{ 141262306a36Sopenharmony_ci int ret; 141362306a36Sopenharmony_ci unsigned long addr; 141462306a36Sopenharmony_ci struct hfi1_tid_info tinfo; 141562306a36Sopenharmony_ci 141662306a36Sopenharmony_ci if (sizeof(tinfo) != len) 141762306a36Sopenharmony_ci return -EINVAL; 141862306a36Sopenharmony_ci 141962306a36Sopenharmony_ci if (!fd->invalid_tids) 142062306a36Sopenharmony_ci return -EINVAL; 142162306a36Sopenharmony_ci 142262306a36Sopenharmony_ci if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo)))) 142362306a36Sopenharmony_ci return -EFAULT; 142462306a36Sopenharmony_ci 142562306a36Sopenharmony_ci ret = hfi1_user_exp_rcv_invalid(fd, &tinfo); 142662306a36Sopenharmony_ci if (ret) 142762306a36Sopenharmony_ci return ret; 142862306a36Sopenharmony_ci 142962306a36Sopenharmony_ci addr = arg + offsetof(struct hfi1_tid_info, tidcnt); 143062306a36Sopenharmony_ci if (copy_to_user((void __user *)addr, &tinfo.tidcnt, 143162306a36Sopenharmony_ci sizeof(tinfo.tidcnt))) 143262306a36Sopenharmony_ci ret = -EFAULT; 143362306a36Sopenharmony_ci 143462306a36Sopenharmony_ci return ret; 143562306a36Sopenharmony_ci} 143662306a36Sopenharmony_ci 143762306a36Sopenharmony_cistatic __poll_t poll_urgent(struct file *fp, 143862306a36Sopenharmony_ci struct poll_table_struct *pt) 143962306a36Sopenharmony_ci{ 144062306a36Sopenharmony_ci struct hfi1_filedata *fd = fp->private_data; 144162306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt = fd->uctxt; 144262306a36Sopenharmony_ci struct hfi1_devdata *dd = uctxt->dd; 144362306a36Sopenharmony_ci __poll_t pollflag; 144462306a36Sopenharmony_ci 144562306a36Sopenharmony_ci poll_wait(fp, &uctxt->wait, pt); 144662306a36Sopenharmony_ci 144762306a36Sopenharmony_ci spin_lock_irq(&dd->uctxt_lock); 144862306a36Sopenharmony_ci if (uctxt->urgent != uctxt->urgent_poll) { 144962306a36Sopenharmony_ci pollflag = EPOLLIN | EPOLLRDNORM; 145062306a36Sopenharmony_ci uctxt->urgent_poll = uctxt->urgent; 145162306a36Sopenharmony_ci } else { 145262306a36Sopenharmony_ci pollflag = 0; 145362306a36Sopenharmony_ci set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags); 145462306a36Sopenharmony_ci } 145562306a36Sopenharmony_ci spin_unlock_irq(&dd->uctxt_lock); 145662306a36Sopenharmony_ci 145762306a36Sopenharmony_ci return pollflag; 145862306a36Sopenharmony_ci} 145962306a36Sopenharmony_ci 146062306a36Sopenharmony_cistatic __poll_t poll_next(struct file *fp, 146162306a36Sopenharmony_ci struct poll_table_struct *pt) 146262306a36Sopenharmony_ci{ 146362306a36Sopenharmony_ci struct hfi1_filedata *fd = fp->private_data; 146462306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt = fd->uctxt; 146562306a36Sopenharmony_ci struct hfi1_devdata *dd = uctxt->dd; 146662306a36Sopenharmony_ci __poll_t pollflag; 146762306a36Sopenharmony_ci 146862306a36Sopenharmony_ci poll_wait(fp, &uctxt->wait, pt); 146962306a36Sopenharmony_ci 147062306a36Sopenharmony_ci spin_lock_irq(&dd->uctxt_lock); 147162306a36Sopenharmony_ci if (hdrqempty(uctxt)) { 147262306a36Sopenharmony_ci set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags); 147362306a36Sopenharmony_ci hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt); 147462306a36Sopenharmony_ci pollflag = 0; 147562306a36Sopenharmony_ci } else { 147662306a36Sopenharmony_ci pollflag = EPOLLIN | EPOLLRDNORM; 147762306a36Sopenharmony_ci } 147862306a36Sopenharmony_ci spin_unlock_irq(&dd->uctxt_lock); 147962306a36Sopenharmony_ci 148062306a36Sopenharmony_ci return pollflag; 148162306a36Sopenharmony_ci} 148262306a36Sopenharmony_ci 148362306a36Sopenharmony_ci/* 148462306a36Sopenharmony_ci * Find all user contexts in use, and set the specified bit in their 148562306a36Sopenharmony_ci * event mask. 148662306a36Sopenharmony_ci * See also find_ctxt() for a similar use, that is specific to send buffers. 148762306a36Sopenharmony_ci */ 148862306a36Sopenharmony_ciint hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit) 148962306a36Sopenharmony_ci{ 149062306a36Sopenharmony_ci struct hfi1_ctxtdata *uctxt; 149162306a36Sopenharmony_ci struct hfi1_devdata *dd = ppd->dd; 149262306a36Sopenharmony_ci u16 ctxt; 149362306a36Sopenharmony_ci 149462306a36Sopenharmony_ci if (!dd->events) 149562306a36Sopenharmony_ci return -EINVAL; 149662306a36Sopenharmony_ci 149762306a36Sopenharmony_ci for (ctxt = dd->first_dyn_alloc_ctxt; ctxt < dd->num_rcv_contexts; 149862306a36Sopenharmony_ci ctxt++) { 149962306a36Sopenharmony_ci uctxt = hfi1_rcd_get_by_index(dd, ctxt); 150062306a36Sopenharmony_ci if (uctxt) { 150162306a36Sopenharmony_ci unsigned long *evs; 150262306a36Sopenharmony_ci int i; 150362306a36Sopenharmony_ci /* 150462306a36Sopenharmony_ci * subctxt_cnt is 0 if not shared, so do base 150562306a36Sopenharmony_ci * separately, first, then remaining subctxt, if any 150662306a36Sopenharmony_ci */ 150762306a36Sopenharmony_ci evs = dd->events + uctxt_offset(uctxt); 150862306a36Sopenharmony_ci set_bit(evtbit, evs); 150962306a36Sopenharmony_ci for (i = 1; i < uctxt->subctxt_cnt; i++) 151062306a36Sopenharmony_ci set_bit(evtbit, evs + i); 151162306a36Sopenharmony_ci hfi1_rcd_put(uctxt); 151262306a36Sopenharmony_ci } 151362306a36Sopenharmony_ci } 151462306a36Sopenharmony_ci 151562306a36Sopenharmony_ci return 0; 151662306a36Sopenharmony_ci} 151762306a36Sopenharmony_ci 151862306a36Sopenharmony_ci/** 151962306a36Sopenharmony_ci * manage_rcvq - manage a context's receive queue 152062306a36Sopenharmony_ci * @uctxt: the context 152162306a36Sopenharmony_ci * @subctxt: the sub-context 152262306a36Sopenharmony_ci * @arg: start/stop action to carry out 152362306a36Sopenharmony_ci * 152462306a36Sopenharmony_ci * start_stop == 0 disables receive on the context, for use in queue 152562306a36Sopenharmony_ci * overflow conditions. start_stop==1 re-enables, to be used to 152662306a36Sopenharmony_ci * re-init the software copy of the head register 152762306a36Sopenharmony_ci */ 152862306a36Sopenharmony_cistatic int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt, 152962306a36Sopenharmony_ci unsigned long arg) 153062306a36Sopenharmony_ci{ 153162306a36Sopenharmony_ci struct hfi1_devdata *dd = uctxt->dd; 153262306a36Sopenharmony_ci unsigned int rcvctrl_op; 153362306a36Sopenharmony_ci int start_stop; 153462306a36Sopenharmony_ci 153562306a36Sopenharmony_ci if (subctxt) 153662306a36Sopenharmony_ci return 0; 153762306a36Sopenharmony_ci 153862306a36Sopenharmony_ci if (get_user(start_stop, (int __user *)arg)) 153962306a36Sopenharmony_ci return -EFAULT; 154062306a36Sopenharmony_ci 154162306a36Sopenharmony_ci /* atomically clear receive enable ctxt. */ 154262306a36Sopenharmony_ci if (start_stop) { 154362306a36Sopenharmony_ci /* 154462306a36Sopenharmony_ci * On enable, force in-memory copy of the tail register to 154562306a36Sopenharmony_ci * 0, so that protocol code doesn't have to worry about 154662306a36Sopenharmony_ci * whether or not the chip has yet updated the in-memory 154762306a36Sopenharmony_ci * copy or not on return from the system call. The chip 154862306a36Sopenharmony_ci * always resets it's tail register back to 0 on a 154962306a36Sopenharmony_ci * transition from disabled to enabled. 155062306a36Sopenharmony_ci */ 155162306a36Sopenharmony_ci if (hfi1_rcvhdrtail_kvaddr(uctxt)) 155262306a36Sopenharmony_ci clear_rcvhdrtail(uctxt); 155362306a36Sopenharmony_ci rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB; 155462306a36Sopenharmony_ci } else { 155562306a36Sopenharmony_ci rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS; 155662306a36Sopenharmony_ci } 155762306a36Sopenharmony_ci hfi1_rcvctrl(dd, rcvctrl_op, uctxt); 155862306a36Sopenharmony_ci /* always; new head should be equal to new tail; see above */ 155962306a36Sopenharmony_ci 156062306a36Sopenharmony_ci return 0; 156162306a36Sopenharmony_ci} 156262306a36Sopenharmony_ci 156362306a36Sopenharmony_ci/* 156462306a36Sopenharmony_ci * clear the event notifier events for this context. 156562306a36Sopenharmony_ci * User process then performs actions appropriate to bit having been 156662306a36Sopenharmony_ci * set, if desired, and checks again in future. 156762306a36Sopenharmony_ci */ 156862306a36Sopenharmony_cistatic int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt, 156962306a36Sopenharmony_ci unsigned long arg) 157062306a36Sopenharmony_ci{ 157162306a36Sopenharmony_ci int i; 157262306a36Sopenharmony_ci struct hfi1_devdata *dd = uctxt->dd; 157362306a36Sopenharmony_ci unsigned long *evs; 157462306a36Sopenharmony_ci unsigned long events; 157562306a36Sopenharmony_ci 157662306a36Sopenharmony_ci if (!dd->events) 157762306a36Sopenharmony_ci return 0; 157862306a36Sopenharmony_ci 157962306a36Sopenharmony_ci if (get_user(events, (unsigned long __user *)arg)) 158062306a36Sopenharmony_ci return -EFAULT; 158162306a36Sopenharmony_ci 158262306a36Sopenharmony_ci evs = dd->events + uctxt_offset(uctxt) + subctxt; 158362306a36Sopenharmony_ci 158462306a36Sopenharmony_ci for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) { 158562306a36Sopenharmony_ci if (!test_bit(i, &events)) 158662306a36Sopenharmony_ci continue; 158762306a36Sopenharmony_ci clear_bit(i, evs); 158862306a36Sopenharmony_ci } 158962306a36Sopenharmony_ci return 0; 159062306a36Sopenharmony_ci} 159162306a36Sopenharmony_ci 159262306a36Sopenharmony_cistatic int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned long arg) 159362306a36Sopenharmony_ci{ 159462306a36Sopenharmony_ci int i; 159562306a36Sopenharmony_ci struct hfi1_pportdata *ppd = uctxt->ppd; 159662306a36Sopenharmony_ci struct hfi1_devdata *dd = uctxt->dd; 159762306a36Sopenharmony_ci u16 pkey; 159862306a36Sopenharmony_ci 159962306a36Sopenharmony_ci if (!HFI1_CAP_IS_USET(PKEY_CHECK)) 160062306a36Sopenharmony_ci return -EPERM; 160162306a36Sopenharmony_ci 160262306a36Sopenharmony_ci if (get_user(pkey, (u16 __user *)arg)) 160362306a36Sopenharmony_ci return -EFAULT; 160462306a36Sopenharmony_ci 160562306a36Sopenharmony_ci if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY) 160662306a36Sopenharmony_ci return -EINVAL; 160762306a36Sopenharmony_ci 160862306a36Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) 160962306a36Sopenharmony_ci if (pkey == ppd->pkeys[i]) 161062306a36Sopenharmony_ci return hfi1_set_ctxt_pkey(dd, uctxt, pkey); 161162306a36Sopenharmony_ci 161262306a36Sopenharmony_ci return -ENOENT; 161362306a36Sopenharmony_ci} 161462306a36Sopenharmony_ci 161562306a36Sopenharmony_ci/** 161662306a36Sopenharmony_ci * ctxt_reset - Reset the user context 161762306a36Sopenharmony_ci * @uctxt: valid user context 161862306a36Sopenharmony_ci */ 161962306a36Sopenharmony_cistatic int ctxt_reset(struct hfi1_ctxtdata *uctxt) 162062306a36Sopenharmony_ci{ 162162306a36Sopenharmony_ci struct send_context *sc; 162262306a36Sopenharmony_ci struct hfi1_devdata *dd; 162362306a36Sopenharmony_ci int ret = 0; 162462306a36Sopenharmony_ci 162562306a36Sopenharmony_ci if (!uctxt || !uctxt->dd || !uctxt->sc) 162662306a36Sopenharmony_ci return -EINVAL; 162762306a36Sopenharmony_ci 162862306a36Sopenharmony_ci /* 162962306a36Sopenharmony_ci * There is no protection here. User level has to guarantee that 163062306a36Sopenharmony_ci * no one will be writing to the send context while it is being 163162306a36Sopenharmony_ci * re-initialized. If user level breaks that guarantee, it will 163262306a36Sopenharmony_ci * break it's own context and no one else's. 163362306a36Sopenharmony_ci */ 163462306a36Sopenharmony_ci dd = uctxt->dd; 163562306a36Sopenharmony_ci sc = uctxt->sc; 163662306a36Sopenharmony_ci 163762306a36Sopenharmony_ci /* 163862306a36Sopenharmony_ci * Wait until the interrupt handler has marked the context as 163962306a36Sopenharmony_ci * halted or frozen. Report error if we time out. 164062306a36Sopenharmony_ci */ 164162306a36Sopenharmony_ci wait_event_interruptible_timeout( 164262306a36Sopenharmony_ci sc->halt_wait, (sc->flags & SCF_HALTED), 164362306a36Sopenharmony_ci msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT)); 164462306a36Sopenharmony_ci if (!(sc->flags & SCF_HALTED)) 164562306a36Sopenharmony_ci return -ENOLCK; 164662306a36Sopenharmony_ci 164762306a36Sopenharmony_ci /* 164862306a36Sopenharmony_ci * If the send context was halted due to a Freeze, wait until the 164962306a36Sopenharmony_ci * device has been "unfrozen" before resetting the context. 165062306a36Sopenharmony_ci */ 165162306a36Sopenharmony_ci if (sc->flags & SCF_FROZEN) { 165262306a36Sopenharmony_ci wait_event_interruptible_timeout( 165362306a36Sopenharmony_ci dd->event_queue, 165462306a36Sopenharmony_ci !(READ_ONCE(dd->flags) & HFI1_FROZEN), 165562306a36Sopenharmony_ci msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT)); 165662306a36Sopenharmony_ci if (dd->flags & HFI1_FROZEN) 165762306a36Sopenharmony_ci return -ENOLCK; 165862306a36Sopenharmony_ci 165962306a36Sopenharmony_ci if (dd->flags & HFI1_FORCED_FREEZE) 166062306a36Sopenharmony_ci /* 166162306a36Sopenharmony_ci * Don't allow context reset if we are into 166262306a36Sopenharmony_ci * forced freeze 166362306a36Sopenharmony_ci */ 166462306a36Sopenharmony_ci return -ENODEV; 166562306a36Sopenharmony_ci 166662306a36Sopenharmony_ci sc_disable(sc); 166762306a36Sopenharmony_ci ret = sc_enable(sc); 166862306a36Sopenharmony_ci hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB, uctxt); 166962306a36Sopenharmony_ci } else { 167062306a36Sopenharmony_ci ret = sc_restart(sc); 167162306a36Sopenharmony_ci } 167262306a36Sopenharmony_ci if (!ret) 167362306a36Sopenharmony_ci sc_return_credits(sc); 167462306a36Sopenharmony_ci 167562306a36Sopenharmony_ci return ret; 167662306a36Sopenharmony_ci} 167762306a36Sopenharmony_ci 167862306a36Sopenharmony_cistatic void user_remove(struct hfi1_devdata *dd) 167962306a36Sopenharmony_ci{ 168062306a36Sopenharmony_ci 168162306a36Sopenharmony_ci hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device); 168262306a36Sopenharmony_ci} 168362306a36Sopenharmony_ci 168462306a36Sopenharmony_cistatic int user_add(struct hfi1_devdata *dd) 168562306a36Sopenharmony_ci{ 168662306a36Sopenharmony_ci char name[10]; 168762306a36Sopenharmony_ci int ret; 168862306a36Sopenharmony_ci 168962306a36Sopenharmony_ci snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit); 169062306a36Sopenharmony_ci ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops, 169162306a36Sopenharmony_ci &dd->user_cdev, &dd->user_device, 169262306a36Sopenharmony_ci true, &dd->verbs_dev.rdi.ibdev.dev.kobj); 169362306a36Sopenharmony_ci if (ret) 169462306a36Sopenharmony_ci user_remove(dd); 169562306a36Sopenharmony_ci 169662306a36Sopenharmony_ci return ret; 169762306a36Sopenharmony_ci} 169862306a36Sopenharmony_ci 169962306a36Sopenharmony_ci/* 170062306a36Sopenharmony_ci * Create per-unit files in /dev 170162306a36Sopenharmony_ci */ 170262306a36Sopenharmony_ciint hfi1_device_create(struct hfi1_devdata *dd) 170362306a36Sopenharmony_ci{ 170462306a36Sopenharmony_ci return user_add(dd); 170562306a36Sopenharmony_ci} 170662306a36Sopenharmony_ci 170762306a36Sopenharmony_ci/* 170862306a36Sopenharmony_ci * Remove per-unit files in /dev 170962306a36Sopenharmony_ci * void, core kernel returns no errors for this stuff 171062306a36Sopenharmony_ci */ 171162306a36Sopenharmony_civoid hfi1_device_remove(struct hfi1_devdata *dd) 171262306a36Sopenharmony_ci{ 171362306a36Sopenharmony_ci user_remove(dd); 171462306a36Sopenharmony_ci} 1715