GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / infiniband / hw / hfi1 / file_ops.c
1 /*
2  * Copyright(c) 2015-2017 Intel Corporation.
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * BSD LICENSE
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  *
24  *  - Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  *  - Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in
28  *    the documentation and/or other materials provided with the
29  *    distribution.
30  *  - Neither the name of Intel Corporation nor the names of its
31  *    contributors may be used to endorse or promote products derived
32  *    from this software without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  */
47 #include <linux/poll.h>
48 #include <linux/cdev.h>
49 #include <linux/vmalloc.h>
50 #include <linux/io.h>
51 #include <linux/sched/mm.h>
52 #include <linux/bitmap.h>
53
54 #include <rdma/ib.h>
55
56 #include "hfi.h"
57 #include "pio.h"
58 #include "device.h"
59 #include "common.h"
60 #include "trace.h"
61 #include "mmu_rb.h"
62 #include "user_sdma.h"
63 #include "user_exp_rcv.h"
64 #include "aspm.h"
65
66 #undef pr_fmt
67 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
68
69 #define SEND_CTXT_HALT_TIMEOUT 1000 /* msecs */
70
71 /*
72  * File operation functions
73  */
74 static int hfi1_file_open(struct inode *inode, struct file *fp);
75 static int hfi1_file_close(struct inode *inode, struct file *fp);
76 static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from);
77 static unsigned int hfi1_poll(struct file *fp, struct poll_table_struct *pt);
78 static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma);
79
80 static u64 kvirt_to_phys(void *addr);
81 static int assign_ctxt(struct hfi1_filedata *fd, struct hfi1_user_info *uinfo);
82 static void init_subctxts(struct hfi1_ctxtdata *uctxt,
83                           const struct hfi1_user_info *uinfo);
84 static int init_user_ctxt(struct hfi1_filedata *fd,
85                           struct hfi1_ctxtdata *uctxt);
86 static void user_init(struct hfi1_ctxtdata *uctxt);
87 static int get_ctxt_info(struct hfi1_filedata *fd, void __user *ubase,
88                          __u32 len);
89 static int get_base_info(struct hfi1_filedata *fd, void __user *ubase,
90                          __u32 len);
91 static int setup_base_ctxt(struct hfi1_filedata *fd,
92                            struct hfi1_ctxtdata *uctxt);
93 static int setup_subctxt(struct hfi1_ctxtdata *uctxt);
94
95 static int find_sub_ctxt(struct hfi1_filedata *fd,
96                          const struct hfi1_user_info *uinfo);
97 static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
98                          struct hfi1_user_info *uinfo,
99                          struct hfi1_ctxtdata **cd);
100 static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt);
101 static unsigned int poll_urgent(struct file *fp, struct poll_table_struct *pt);
102 static unsigned int poll_next(struct file *fp, struct poll_table_struct *pt);
103 static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
104                           unsigned long events);
105 static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, u16 subctxt, u16 pkey);
106 static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
107                        int start_stop);
108 static int vma_fault(struct vm_fault *vmf);
109 static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
110                             unsigned long arg);
111
112 static const struct file_operations hfi1_file_ops = {
113         .owner = THIS_MODULE,
114         .write_iter = hfi1_write_iter,
115         .open = hfi1_file_open,
116         .release = hfi1_file_close,
117         .unlocked_ioctl = hfi1_file_ioctl,
118         .poll = hfi1_poll,
119         .mmap = hfi1_file_mmap,
120         .llseek = noop_llseek,
121 };
122
123 static const struct vm_operations_struct vm_ops = {
124         .fault = vma_fault,
125 };
126
127 /*
128  * Types of memories mapped into user processes' space
129  */
130 enum mmap_types {
131         PIO_BUFS = 1,
132         PIO_BUFS_SOP,
133         PIO_CRED,
134         RCV_HDRQ,
135         RCV_EGRBUF,
136         UREGS,
137         EVENTS,
138         STATUS,
139         RTAIL,
140         SUBCTXT_UREGS,
141         SUBCTXT_RCV_HDRQ,
142         SUBCTXT_EGRBUF,
143         SDMA_COMP
144 };
145
146 /*
147  * Masks and offsets defining the mmap tokens
148  */
149 #define HFI1_MMAP_OFFSET_MASK   0xfffULL
150 #define HFI1_MMAP_OFFSET_SHIFT  0
151 #define HFI1_MMAP_SUBCTXT_MASK  0xfULL
152 #define HFI1_MMAP_SUBCTXT_SHIFT 12
153 #define HFI1_MMAP_CTXT_MASK     0xffULL
154 #define HFI1_MMAP_CTXT_SHIFT    16
155 #define HFI1_MMAP_TYPE_MASK     0xfULL
156 #define HFI1_MMAP_TYPE_SHIFT    24
157 #define HFI1_MMAP_MAGIC_MASK    0xffffffffULL
158 #define HFI1_MMAP_MAGIC_SHIFT   32
159
160 #define HFI1_MMAP_MAGIC         0xdabbad00
161
162 #define HFI1_MMAP_TOKEN_SET(field, val) \
163         (((val) & HFI1_MMAP_##field##_MASK) << HFI1_MMAP_##field##_SHIFT)
164 #define HFI1_MMAP_TOKEN_GET(field, token) \
165         (((token) >> HFI1_MMAP_##field##_SHIFT) & HFI1_MMAP_##field##_MASK)
166 #define HFI1_MMAP_TOKEN(type, ctxt, subctxt, addr)   \
167         (HFI1_MMAP_TOKEN_SET(MAGIC, HFI1_MMAP_MAGIC) | \
168         HFI1_MMAP_TOKEN_SET(TYPE, type) | \
169         HFI1_MMAP_TOKEN_SET(CTXT, ctxt) | \
170         HFI1_MMAP_TOKEN_SET(SUBCTXT, subctxt) | \
171         HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr))))
172
173 #define dbg(fmt, ...)                           \
174         pr_info(fmt, ##__VA_ARGS__)
175
176 static inline int is_valid_mmap(u64 token)
177 {
178         return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC);
179 }
180
181 static int hfi1_file_open(struct inode *inode, struct file *fp)
182 {
183         struct hfi1_filedata *fd;
184         struct hfi1_devdata *dd = container_of(inode->i_cdev,
185                                                struct hfi1_devdata,
186                                                user_cdev);
187
188         if (!((dd->flags & HFI1_PRESENT) && dd->kregbase1))
189                 return -EINVAL;
190
191         if (!atomic_inc_not_zero(&dd->user_refcount))
192                 return -ENXIO;
193
194         /* The real work is performed later in assign_ctxt() */
195
196         fd = kzalloc(sizeof(*fd), GFP_KERNEL);
197
198         if (!fd || init_srcu_struct(&fd->pq_srcu))
199                 goto nomem;
200         spin_lock_init(&fd->pq_rcu_lock);
201         spin_lock_init(&fd->tid_lock);
202         spin_lock_init(&fd->invalid_lock);
203         fd->rec_cpu_num = -1; /* no cpu affinity by default */
204         fd->mm = current->mm;
205         mmgrab(fd->mm);
206         fd->dd = dd;
207         kobject_get(&fd->dd->kobj);
208         fp->private_data = fd;
209         return 0;
210 nomem:
211         kfree(fd);
212         fp->private_data = NULL;
213         if (atomic_dec_and_test(&dd->user_refcount))
214                 complete(&dd->user_comp);
215         return -ENOMEM;
216 }
217
218 static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
219                             unsigned long arg)
220 {
221         struct hfi1_filedata *fd = fp->private_data;
222         struct hfi1_ctxtdata *uctxt = fd->uctxt;
223         struct hfi1_user_info uinfo;
224         struct hfi1_tid_info tinfo;
225         int ret = 0;
226         unsigned long addr;
227         int uval = 0;
228         unsigned long ul_uval = 0;
229         u16 uval16 = 0;
230
231         hfi1_cdbg(IOCTL, "IOCTL recv: 0x%x", cmd);
232         if (cmd != HFI1_IOCTL_ASSIGN_CTXT &&
233             cmd != HFI1_IOCTL_GET_VERS &&
234             !uctxt)
235                 return -EINVAL;
236
237         switch (cmd) {
238         case HFI1_IOCTL_ASSIGN_CTXT:
239                 if (uctxt)
240                         return -EINVAL;
241
242                 if (copy_from_user(&uinfo,
243                                    (struct hfi1_user_info __user *)arg,
244                                    sizeof(uinfo)))
245                         return -EFAULT;
246
247                 ret = assign_ctxt(fd, &uinfo);
248                 break;
249         case HFI1_IOCTL_CTXT_INFO:
250                 ret = get_ctxt_info(fd, (void __user *)(unsigned long)arg,
251                                     sizeof(struct hfi1_ctxt_info));
252                 break;
253         case HFI1_IOCTL_USER_INFO:
254                 ret = get_base_info(fd, (void __user *)(unsigned long)arg,
255                                     sizeof(struct hfi1_base_info));
256                 break;
257         case HFI1_IOCTL_CREDIT_UPD:
258                 if (uctxt)
259                         sc_return_credits(uctxt->sc);
260                 break;
261
262         case HFI1_IOCTL_TID_UPDATE:
263                 if (copy_from_user(&tinfo,
264                                    (struct hfi11_tid_info __user *)arg,
265                                    sizeof(tinfo)))
266                         return -EFAULT;
267
268                 ret = hfi1_user_exp_rcv_setup(fd, &tinfo);
269                 if (!ret) {
270                         /*
271                          * Copy the number of tidlist entries we used
272                          * and the length of the buffer we registered.
273                          */
274                         addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
275                         if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
276                                          sizeof(tinfo.tidcnt)))
277                                 return -EFAULT;
278
279                         addr = arg + offsetof(struct hfi1_tid_info, length);
280                         if (copy_to_user((void __user *)addr, &tinfo.length,
281                                          sizeof(tinfo.length)))
282                                 ret = -EFAULT;
283                 }
284                 break;
285
286         case HFI1_IOCTL_TID_FREE:
287                 if (copy_from_user(&tinfo,
288                                    (struct hfi11_tid_info __user *)arg,
289                                    sizeof(tinfo)))
290                         return -EFAULT;
291
292                 ret = hfi1_user_exp_rcv_clear(fd, &tinfo);
293                 if (ret)
294                         break;
295                 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
296                 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
297                                  sizeof(tinfo.tidcnt)))
298                         ret = -EFAULT;
299                 break;
300
301         case HFI1_IOCTL_TID_INVAL_READ:
302                 if (copy_from_user(&tinfo,
303                                    (struct hfi11_tid_info __user *)arg,
304                                    sizeof(tinfo)))
305                         return -EFAULT;
306
307                 ret = hfi1_user_exp_rcv_invalid(fd, &tinfo);
308                 if (ret)
309                         break;
310                 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
311                 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
312                                  sizeof(tinfo.tidcnt)))
313                         ret = -EFAULT;
314                 break;
315
316         case HFI1_IOCTL_RECV_CTRL:
317                 ret = get_user(uval, (int __user *)arg);
318                 if (ret != 0)
319                         return -EFAULT;
320                 ret = manage_rcvq(uctxt, fd->subctxt, uval);
321                 break;
322
323         case HFI1_IOCTL_POLL_TYPE:
324                 ret = get_user(uval, (int __user *)arg);
325                 if (ret != 0)
326                         return -EFAULT;
327                 uctxt->poll_type = (typeof(uctxt->poll_type))uval;
328                 break;
329
330         case HFI1_IOCTL_ACK_EVENT:
331                 ret = get_user(ul_uval, (unsigned long __user *)arg);
332                 if (ret != 0)
333                         return -EFAULT;
334                 ret = user_event_ack(uctxt, fd->subctxt, ul_uval);
335                 break;
336
337         case HFI1_IOCTL_SET_PKEY:
338                 ret = get_user(uval16, (u16 __user *)arg);
339                 if (ret != 0)
340                         return -EFAULT;
341                 if (HFI1_CAP_IS_USET(PKEY_CHECK))
342                         ret = set_ctxt_pkey(uctxt, fd->subctxt, uval16);
343                 else
344                         return -EPERM;
345                 break;
346
347         case HFI1_IOCTL_CTXT_RESET: {
348                 struct send_context *sc;
349                 struct hfi1_devdata *dd;
350
351                 if (!uctxt || !uctxt->dd || !uctxt->sc)
352                         return -EINVAL;
353
354                 /*
355                  * There is no protection here. User level has to
356                  * guarantee that no one will be writing to the send
357                  * context while it is being re-initialized.
358                  * If user level breaks that guarantee, it will break
359                  * it's own context and no one else's.
360                  */
361                 dd = uctxt->dd;
362                 sc = uctxt->sc;
363                 /*
364                  * Wait until the interrupt handler has marked the
365                  * context as halted or frozen. Report error if we time
366                  * out.
367                  */
368                 wait_event_interruptible_timeout(
369                         sc->halt_wait, (sc->flags & SCF_HALTED),
370                         msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
371                 if (!(sc->flags & SCF_HALTED))
372                         return -ENOLCK;
373
374                 /*
375                  * If the send context was halted due to a Freeze,
376                  * wait until the device has been "unfrozen" before
377                  * resetting the context.
378                  */
379                 if (sc->flags & SCF_FROZEN) {
380                         wait_event_interruptible_timeout(
381                                 dd->event_queue,
382                                 !(ACCESS_ONCE(dd->flags) & HFI1_FROZEN),
383                                 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
384                         if (dd->flags & HFI1_FROZEN)
385                                 return -ENOLCK;
386
387                         if (dd->flags & HFI1_FORCED_FREEZE)
388                                 /*
389                                  * Don't allow context reset if we are into
390                                  * forced freeze
391                                  */
392                                 return -ENODEV;
393
394                         sc_disable(sc);
395                         ret = sc_enable(sc);
396                         hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB, uctxt);
397                 } else {
398                         ret = sc_restart(sc);
399                 }
400                 if (!ret)
401                         sc_return_credits(sc);
402                 break;
403         }
404
405         case HFI1_IOCTL_GET_VERS:
406                 uval = HFI1_USER_SWVERSION;
407                 if (put_user(uval, (int __user *)arg))
408                         return -EFAULT;
409                 break;
410
411         default:
412                 return -EINVAL;
413         }
414
415         return ret;
416 }
417
418 static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from)
419 {
420         struct hfi1_filedata *fd = kiocb->ki_filp->private_data;
421         struct hfi1_user_sdma_pkt_q *pq;
422         struct hfi1_user_sdma_comp_q *cq = fd->cq;
423         int done = 0, reqs = 0;
424         unsigned long dim = from->nr_segs;
425         int idx;
426
427         if (!HFI1_CAP_IS_KSET(SDMA))
428                 return -EINVAL;
429         idx = srcu_read_lock(&fd->pq_srcu);
430         pq = srcu_dereference(fd->pq, &fd->pq_srcu);
431         if (!cq || !pq) {
432                 srcu_read_unlock(&fd->pq_srcu, idx);
433                 return -EIO;
434         }
435
436         if (!iter_is_iovec(from) || !dim) {
437                 srcu_read_unlock(&fd->pq_srcu, idx);
438                 return -EINVAL;
439         }
440
441         trace_hfi1_sdma_request(fd->dd, fd->uctxt->ctxt, fd->subctxt, dim);
442
443         if (atomic_read(&pq->n_reqs) == pq->n_max_reqs) {
444                 srcu_read_unlock(&fd->pq_srcu, idx);
445                 return -ENOSPC;
446         }
447
448         while (dim) {
449                 int ret;
450                 unsigned long count = 0;
451
452                 ret = hfi1_user_sdma_process_request(
453                         fd, (struct iovec *)(from->iov + done),
454                         dim, &count);
455                 if (ret) {
456                         reqs = ret;
457                         break;
458                 }
459                 dim -= count;
460                 done += count;
461                 reqs++;
462         }
463
464         srcu_read_unlock(&fd->pq_srcu, idx);
465         return reqs;
466 }
467
468 static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma)
469 {
470         struct hfi1_filedata *fd = fp->private_data;
471         struct hfi1_ctxtdata *uctxt = fd->uctxt;
472         struct hfi1_devdata *dd;
473         unsigned long flags;
474         u64 token = vma->vm_pgoff << PAGE_SHIFT,
475                 memaddr = 0;
476         void *memvirt = NULL;
477         u8 subctxt, mapio = 0, vmf = 0, type;
478         ssize_t memlen = 0;
479         int ret = 0;
480         u16 ctxt;
481
482         if (!is_valid_mmap(token) || !uctxt ||
483             !(vma->vm_flags & VM_SHARED)) {
484                 ret = -EINVAL;
485                 goto done;
486         }
487         dd = uctxt->dd;
488         ctxt = HFI1_MMAP_TOKEN_GET(CTXT, token);
489         subctxt = HFI1_MMAP_TOKEN_GET(SUBCTXT, token);
490         type = HFI1_MMAP_TOKEN_GET(TYPE, token);
491         if (ctxt != uctxt->ctxt || subctxt != fd->subctxt) {
492                 ret = -EINVAL;
493                 goto done;
494         }
495
496         flags = vma->vm_flags;
497
498         switch (type) {
499         case PIO_BUFS:
500         case PIO_BUFS_SOP:
501                 memaddr = ((dd->physaddr + TXE_PIO_SEND) +
502                                 /* chip pio base */
503                            (uctxt->sc->hw_context * BIT(16))) +
504                                 /* 64K PIO space / ctxt */
505                         (type == PIO_BUFS_SOP ?
506                                 (TXE_PIO_SIZE / 2) : 0); /* sop? */
507                 /*
508                  * Map only the amount allocated to the context, not the
509                  * entire available context's PIO space.
510                  */
511                 memlen = PAGE_ALIGN(uctxt->sc->credits * PIO_BLOCK_SIZE);
512                 flags &= ~VM_MAYREAD;
513                 flags |= VM_DONTCOPY | VM_DONTEXPAND;
514                 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
515                 mapio = 1;
516                 break;
517         case PIO_CRED:
518                 if (flags & VM_WRITE) {
519                         ret = -EPERM;
520                         goto done;
521                 }
522                 /*
523                  * The credit return location for this context could be on the
524                  * second or third page allocated for credit returns (if number
525                  * of enabled contexts > 64 and 128 respectively).
526                  */
527                 memvirt = dd->cr_base[uctxt->numa_id].va;
528                 memaddr = virt_to_phys(memvirt) +
529                         (((u64)uctxt->sc->hw_free -
530                           (u64)dd->cr_base[uctxt->numa_id].va) & PAGE_MASK);
531                 memlen = PAGE_SIZE;
532                 flags &= ~VM_MAYWRITE;
533                 flags |= VM_DONTCOPY | VM_DONTEXPAND;
534                 /*
535                  * The driver has already allocated memory for credit
536                  * returns and programmed it into the chip. Has that
537                  * memory been flagged as non-cached?
538                  */
539                 /* vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); */
540                 mapio = 1;
541                 break;
542         case RCV_HDRQ:
543                 memlen = uctxt->rcvhdrq_size;
544                 memvirt = uctxt->rcvhdrq;
545                 break;
546         case RCV_EGRBUF: {
547                 unsigned long addr;
548                 int i;
549                 /*
550                  * The RcvEgr buffer need to be handled differently
551                  * as multiple non-contiguous pages need to be mapped
552                  * into the user process.
553                  */
554                 memlen = uctxt->egrbufs.size;
555                 if ((vma->vm_end - vma->vm_start) != memlen) {
556                         dd_dev_err(dd, "Eager buffer map size invalid (%lu != %lu)\n",
557                                    (vma->vm_end - vma->vm_start), memlen);
558                         ret = -EINVAL;
559                         goto done;
560                 }
561                 if (vma->vm_flags & VM_WRITE) {
562                         ret = -EPERM;
563                         goto done;
564                 }
565                 vma->vm_flags &= ~VM_MAYWRITE;
566                 addr = vma->vm_start;
567                 for (i = 0 ; i < uctxt->egrbufs.numbufs; i++) {
568                         memlen = uctxt->egrbufs.buffers[i].len;
569                         memvirt = uctxt->egrbufs.buffers[i].addr;
570                         ret = remap_pfn_range(
571                                 vma, addr,
572                                 /*
573                                  * virt_to_pfn() does the same, but
574                                  * it's not available on x86_64
575                                  * when CONFIG_MMU is enabled.
576                                  */
577                                 PFN_DOWN(__pa(memvirt)),
578                                 memlen,
579                                 vma->vm_page_prot);
580                         if (ret < 0)
581                                 goto done;
582                         addr += memlen;
583                 }
584                 ret = 0;
585                 goto done;
586         }
587         case UREGS:
588                 /*
589                  * Map only the page that contains this context's user
590                  * registers.
591                  */
592                 memaddr = (unsigned long)
593                         (dd->physaddr + RXE_PER_CONTEXT_USER)
594                         + (uctxt->ctxt * RXE_PER_CONTEXT_SIZE);
595                 /*
596                  * TidFlow table is on the same page as the rest of the
597                  * user registers.
598                  */
599                 memlen = PAGE_SIZE;
600                 flags |= VM_DONTCOPY | VM_DONTEXPAND;
601                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
602                 mapio = 1;
603                 break;
604         case EVENTS:
605                 /*
606                  * Use the page where this context's flags are. User level
607                  * knows where it's own bitmap is within the page.
608                  */
609                 memaddr = (unsigned long)(dd->events +
610                                   ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
611                                    HFI1_MAX_SHARED_CTXTS)) & PAGE_MASK;
612                 memlen = PAGE_SIZE;
613                 /*
614                  * v3.7 removes VM_RESERVED but the effect is kept by
615                  * using VM_IO.
616                  */
617                 flags |= VM_IO | VM_DONTEXPAND;
618                 vmf = 1;
619                 break;
620         case STATUS:
621                 if (flags & VM_WRITE) {
622                         ret = -EPERM;
623                         goto done;
624                 }
625                 memaddr = kvirt_to_phys((void *)dd->status);
626                 memlen = PAGE_SIZE;
627                 flags |= VM_IO | VM_DONTEXPAND;
628                 break;
629         case RTAIL:
630                 if (!HFI1_CAP_IS_USET(DMA_RTAIL)) {
631                         /*
632                          * If the memory allocation failed, the context alloc
633                          * also would have failed, so we would never get here
634                          */
635                         ret = -EINVAL;
636                         goto done;
637                 }
638                 if ((flags & VM_WRITE) || !uctxt->rcvhdrtail_kvaddr) {
639                         ret = -EPERM;
640                         goto done;
641                 }
642                 memlen = PAGE_SIZE;
643                 memvirt = (void *)uctxt->rcvhdrtail_kvaddr;
644                 flags &= ~VM_MAYWRITE;
645                 break;
646         case SUBCTXT_UREGS:
647                 memaddr = (u64)uctxt->subctxt_uregbase;
648                 memlen = PAGE_SIZE;
649                 flags |= VM_IO | VM_DONTEXPAND;
650                 vmf = 1;
651                 break;
652         case SUBCTXT_RCV_HDRQ:
653                 memaddr = (u64)uctxt->subctxt_rcvhdr_base;
654                 memlen = uctxt->rcvhdrq_size * uctxt->subctxt_cnt;
655                 flags |= VM_IO | VM_DONTEXPAND;
656                 vmf = 1;
657                 break;
658         case SUBCTXT_EGRBUF:
659                 memaddr = (u64)uctxt->subctxt_rcvegrbuf;
660                 memlen = uctxt->egrbufs.size * uctxt->subctxt_cnt;
661                 flags |= VM_IO | VM_DONTEXPAND;
662                 flags &= ~VM_MAYWRITE;
663                 vmf = 1;
664                 break;
665         case SDMA_COMP: {
666                 struct hfi1_user_sdma_comp_q *cq = fd->cq;
667
668                 if (!cq) {
669                         ret = -EFAULT;
670                         goto done;
671                 }
672                 memaddr = (u64)cq->comps;
673                 memlen = PAGE_ALIGN(sizeof(*cq->comps) * cq->nentries);
674                 flags |= VM_IO | VM_DONTEXPAND;
675                 vmf = 1;
676                 break;
677         }
678         default:
679                 ret = -EINVAL;
680                 break;
681         }
682
683         if ((vma->vm_end - vma->vm_start) != memlen) {
684                 hfi1_cdbg(PROC, "%u:%u Memory size mismatch %lu:%lu",
685                           uctxt->ctxt, fd->subctxt,
686                           (vma->vm_end - vma->vm_start), memlen);
687                 ret = -EINVAL;
688                 goto done;
689         }
690
691         vma->vm_flags = flags;
692         hfi1_cdbg(PROC,
693                   "%u:%u type:%u io/vf:%d/%d, addr:0x%llx, len:%lu(%lu), flags:0x%lx\n",
694                     ctxt, subctxt, type, mapio, vmf, memaddr, memlen,
695                     vma->vm_end - vma->vm_start, vma->vm_flags);
696         if (vmf) {
697                 vma->vm_pgoff = PFN_DOWN(memaddr);
698                 vma->vm_ops = &vm_ops;
699                 ret = 0;
700         } else if (mapio) {
701                 ret = io_remap_pfn_range(vma, vma->vm_start,
702                                          PFN_DOWN(memaddr),
703                                          memlen,
704                                          vma->vm_page_prot);
705         } else if (memvirt) {
706                 ret = remap_pfn_range(vma, vma->vm_start,
707                                       PFN_DOWN(__pa(memvirt)),
708                                       memlen,
709                                       vma->vm_page_prot);
710         } else {
711                 ret = remap_pfn_range(vma, vma->vm_start,
712                                       PFN_DOWN(memaddr),
713                                       memlen,
714                                       vma->vm_page_prot);
715         }
716 done:
717         return ret;
718 }
719
720 /*
721  * Local (non-chip) user memory is not mapped right away but as it is
722  * accessed by the user-level code.
723  */
724 static int vma_fault(struct vm_fault *vmf)
725 {
726         struct page *page;
727
728         page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
729         if (!page)
730                 return VM_FAULT_SIGBUS;
731
732         get_page(page);
733         vmf->page = page;
734
735         return 0;
736 }
737
738 static unsigned int hfi1_poll(struct file *fp, struct poll_table_struct *pt)
739 {
740         struct hfi1_ctxtdata *uctxt;
741         unsigned pollflag;
742
743         uctxt = ((struct hfi1_filedata *)fp->private_data)->uctxt;
744         if (!uctxt)
745                 pollflag = POLLERR;
746         else if (uctxt->poll_type == HFI1_POLL_TYPE_URGENT)
747                 pollflag = poll_urgent(fp, pt);
748         else  if (uctxt->poll_type == HFI1_POLL_TYPE_ANYRCV)
749                 pollflag = poll_next(fp, pt);
750         else /* invalid */
751                 pollflag = POLLERR;
752
753         return pollflag;
754 }
755
756 static int hfi1_file_close(struct inode *inode, struct file *fp)
757 {
758         struct hfi1_filedata *fdata = fp->private_data;
759         struct hfi1_ctxtdata *uctxt = fdata->uctxt;
760         struct hfi1_devdata *dd = container_of(inode->i_cdev,
761                                                struct hfi1_devdata,
762                                                user_cdev);
763         unsigned long flags, *ev;
764
765         fp->private_data = NULL;
766
767         if (!uctxt)
768                 goto done;
769
770         hfi1_cdbg(PROC, "closing ctxt %u:%u", uctxt->ctxt, fdata->subctxt);
771
772         flush_wc();
773         /* drain user sdma queue */
774         hfi1_user_sdma_free_queues(fdata, uctxt);
775
776         /* release the cpu */
777         hfi1_put_proc_affinity(fdata->rec_cpu_num);
778
779         /* clean up rcv side */
780         hfi1_user_exp_rcv_free(fdata);
781
782         /*
783          * fdata->uctxt is used in the above cleanup.  It is not ready to be
784          * removed until here.
785          */
786         fdata->uctxt = NULL;
787         hfi1_rcd_put(uctxt);
788
789         /*
790          * Clear any left over, unhandled events so the next process that
791          * gets this context doesn't get confused.
792          */
793         ev = dd->events + ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
794                            HFI1_MAX_SHARED_CTXTS) + fdata->subctxt;
795         *ev = 0;
796
797         spin_lock_irqsave(&dd->uctxt_lock, flags);
798         __clear_bit(fdata->subctxt, uctxt->in_use_ctxts);
799         if (!bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
800                 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
801                 goto done;
802         }
803         spin_unlock_irqrestore(&dd->uctxt_lock, flags);
804
805         /*
806          * Disable receive context and interrupt available, reset all
807          * RcvCtxtCtrl bits to default values.
808          */
809         hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
810                      HFI1_RCVCTRL_TIDFLOW_DIS |
811                      HFI1_RCVCTRL_INTRAVAIL_DIS |
812                      HFI1_RCVCTRL_TAILUPD_DIS |
813                      HFI1_RCVCTRL_ONE_PKT_EGR_DIS |
814                      HFI1_RCVCTRL_NO_RHQ_DROP_DIS |
815                      HFI1_RCVCTRL_NO_EGR_DROP_DIS, uctxt);
816         /* Clear the context's J_KEY */
817         hfi1_clear_ctxt_jkey(dd, uctxt);
818         /*
819          * If a send context is allocated, reset context integrity
820          * checks to default and disable the send context.
821          */
822         if (uctxt->sc) {
823                 sc_disable(uctxt->sc);
824                 set_pio_integrity(uctxt->sc);
825         }
826
827         hfi1_free_ctxt_rcv_groups(uctxt);
828         hfi1_clear_ctxt_pkey(dd, uctxt);
829
830         uctxt->event_flags = 0;
831
832         deallocate_ctxt(uctxt);
833 done:
834         mmdrop(fdata->mm);
835         kobject_put(&dd->kobj);
836
837         if (atomic_dec_and_test(&dd->user_refcount))
838                 complete(&dd->user_comp);
839
840         cleanup_srcu_struct(&fdata->pq_srcu);
841         kfree(fdata);
842         return 0;
843 }
844
845 /*
846  * Convert kernel *virtual* addresses to physical addresses.
847  * This is used to vmalloc'ed addresses.
848  */
849 static u64 kvirt_to_phys(void *addr)
850 {
851         struct page *page;
852         u64 paddr = 0;
853
854         page = vmalloc_to_page(addr);
855         if (page)
856                 paddr = page_to_pfn(page) << PAGE_SHIFT;
857
858         return paddr;
859 }
860
861 /**
862  * complete_subctxt
863  * @fd: valid filedata pointer
864  *
865  * Sub-context info can only be set up after the base context
866  * has been completed.  This is indicated by the clearing of the
867  * HFI1_CTXT_BASE_UINIT bit.
868  *
869  * Wait for the bit to be cleared, and then complete the subcontext
870  * initialization.
871  *
872  */
873 static int complete_subctxt(struct hfi1_filedata *fd)
874 {
875         int ret;
876         unsigned long flags;
877
878         /*
879          * sub-context info can only be set up after the base context
880          * has been completed.
881          */
882         ret = wait_event_interruptible(
883                 fd->uctxt->wait,
884                 !test_bit(HFI1_CTXT_BASE_UNINIT, &fd->uctxt->event_flags));
885
886         if (test_bit(HFI1_CTXT_BASE_FAILED, &fd->uctxt->event_flags))
887                 ret = -ENOMEM;
888
889         /* Finish the sub-context init */
890         if (!ret) {
891                 fd->rec_cpu_num = hfi1_get_proc_affinity(fd->uctxt->numa_id);
892                 ret = init_user_ctxt(fd, fd->uctxt);
893         }
894
895         if (ret) {
896                 spin_lock_irqsave(&fd->dd->uctxt_lock, flags);
897                 __clear_bit(fd->subctxt, fd->uctxt->in_use_ctxts);
898                 spin_unlock_irqrestore(&fd->dd->uctxt_lock, flags);
899                 hfi1_rcd_put(fd->uctxt);
900                 fd->uctxt = NULL;
901         }
902
903         return ret;
904 }
905
906 static int assign_ctxt(struct hfi1_filedata *fd, struct hfi1_user_info *uinfo)
907 {
908         int ret;
909         unsigned int swmajor, swminor;
910         struct hfi1_ctxtdata *uctxt = NULL;
911
912         swmajor = uinfo->userversion >> 16;
913         if (swmajor != HFI1_USER_SWMAJOR)
914                 return -ENODEV;
915
916         if (uinfo->subctxt_cnt > HFI1_MAX_SHARED_CTXTS)
917                 return -EINVAL;
918
919         swminor = uinfo->userversion & 0xffff;
920
921         /*
922          * Acquire the mutex to protect against multiple creations of what
923          * could be a shared base context.
924          */
925         mutex_lock(&hfi1_mutex);
926         /*
927          * Get a sub context if available  (fd->uctxt will be set).
928          * ret < 0 error, 0 no context, 1 sub-context found
929          */
930         ret = find_sub_ctxt(fd, uinfo);
931
932         /*
933          * Allocate a base context if context sharing is not required or a
934          * sub context wasn't found.
935          */
936         if (!ret)
937                 ret = allocate_ctxt(fd, fd->dd, uinfo, &uctxt);
938
939         mutex_unlock(&hfi1_mutex);
940
941         /* Depending on the context type, finish the appropriate init */
942         switch (ret) {
943         case 0:
944                 ret = setup_base_ctxt(fd, uctxt);
945                 if (ret)
946                         deallocate_ctxt(uctxt);
947                 break;
948         case 1:
949                 ret = complete_subctxt(fd);
950                 break;
951         default:
952                 break;
953         }
954
955         return ret;
956 }
957
958 /**
959  * match_ctxt
960  * @fd: valid filedata pointer
961  * @uinfo: user info to compare base context with
962  * @uctxt: context to compare uinfo to.
963  *
964  * Compare the given context with the given information to see if it
965  * can be used for a sub context.
966  */
967 static int match_ctxt(struct hfi1_filedata *fd,
968                       const struct hfi1_user_info *uinfo,
969                       struct hfi1_ctxtdata *uctxt)
970 {
971         struct hfi1_devdata *dd = fd->dd;
972         unsigned long flags;
973         u16 subctxt;
974
975         /* Skip dynamically allocated kernel contexts */
976         if (uctxt->sc && (uctxt->sc->type == SC_KERNEL))
977                 return 0;
978
979         /* Skip ctxt if it doesn't match the requested one */
980         if (memcmp(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid)) ||
981             uctxt->jkey != generate_jkey(current_uid()) ||
982             uctxt->subctxt_id != uinfo->subctxt_id ||
983             uctxt->subctxt_cnt != uinfo->subctxt_cnt)
984                 return 0;
985
986         /* Verify the sharing process matches the base */
987         if (uctxt->userversion != uinfo->userversion)
988                 return -EINVAL;
989
990         /* Find an unused sub context */
991         spin_lock_irqsave(&dd->uctxt_lock, flags);
992         if (bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
993                 /* context is being closed, do not use */
994                 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
995                 return 0;
996         }
997
998         subctxt = find_first_zero_bit(uctxt->in_use_ctxts,
999                                       HFI1_MAX_SHARED_CTXTS);
1000         if (subctxt >= uctxt->subctxt_cnt) {
1001                 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1002                 return -EBUSY;
1003         }
1004
1005         fd->subctxt = subctxt;
1006         __set_bit(fd->subctxt, uctxt->in_use_ctxts);
1007         spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1008
1009         fd->uctxt = uctxt;
1010         hfi1_rcd_get(uctxt);
1011
1012         return 1;
1013 }
1014
1015 /**
1016  * find_sub_ctxt
1017  * @fd: valid filedata pointer
1018  * @uinfo: matching info to use to find a possible context to share.
1019  *
1020  * The hfi1_mutex must be held when this function is called.  It is
1021  * necessary to ensure serialized creation of shared contexts.
1022  *
1023  * Return:
1024  *    0      No sub-context found
1025  *    1      Subcontext found and allocated
1026  *    errno  EINVAL (incorrect parameters)
1027  *           EBUSY (all sub contexts in use)
1028  */
1029 static int find_sub_ctxt(struct hfi1_filedata *fd,
1030                          const struct hfi1_user_info *uinfo)
1031 {
1032         struct hfi1_ctxtdata *uctxt;
1033         struct hfi1_devdata *dd = fd->dd;
1034         u16 i;
1035         int ret;
1036
1037         if (!uinfo->subctxt_cnt)
1038                 return 0;
1039
1040         for (i = dd->first_dyn_alloc_ctxt; i < dd->num_rcv_contexts; i++) {
1041                 uctxt = hfi1_rcd_get_by_index(dd, i);
1042                 if (uctxt) {
1043                         ret = match_ctxt(fd, uinfo, uctxt);
1044                         hfi1_rcd_put(uctxt);
1045                         /* value of != 0 will return */
1046                         if (ret)
1047                                 return ret;
1048                 }
1049         }
1050
1051         return 0;
1052 }
1053
1054 static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
1055                          struct hfi1_user_info *uinfo,
1056                          struct hfi1_ctxtdata **rcd)
1057 {
1058         struct hfi1_ctxtdata *uctxt;
1059         int ret, numa;
1060
1061         if (dd->flags & HFI1_FROZEN) {
1062                 /*
1063                  * Pick an error that is unique from all other errors
1064                  * that are returned so the user process knows that
1065                  * it tried to allocate while the SPC was frozen.  It
1066                  * it should be able to retry with success in a short
1067                  * while.
1068                  */
1069                 return -EIO;
1070         }
1071
1072         if (!dd->freectxts)
1073                 return -EBUSY;
1074
1075         /*
1076          * If we don't have a NUMA node requested, preference is towards
1077          * device NUMA node.
1078          */
1079         fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node);
1080         if (fd->rec_cpu_num != -1)
1081                 numa = cpu_to_node(fd->rec_cpu_num);
1082         else
1083                 numa = numa_node_id();
1084         ret = hfi1_create_ctxtdata(dd->pport, numa, &uctxt);
1085         if (ret < 0) {
1086                 dd_dev_err(dd, "user ctxtdata allocation failed\n");
1087                 return ret;
1088         }
1089         hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)",
1090                   uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num,
1091                   uctxt->numa_id);
1092
1093         /*
1094          * Allocate and enable a PIO send context.
1095          */
1096         uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize, dd->node);
1097         if (!uctxt->sc) {
1098                 ret = -ENOMEM;
1099                 goto ctxdata_free;
1100         }
1101         hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index,
1102                   uctxt->sc->hw_context);
1103         ret = sc_enable(uctxt->sc);
1104         if (ret)
1105                 goto ctxdata_free;
1106
1107         /*
1108          * Setup sub context information if the user-level has requested
1109          * sub contexts.
1110          * This has to be done here so the rest of the sub-contexts find the
1111          * proper base context.
1112          */
1113         if (uinfo->subctxt_cnt)
1114                 init_subctxts(uctxt, uinfo);
1115         uctxt->userversion = uinfo->userversion;
1116         uctxt->flags = hfi1_cap_mask; /* save current flag state */
1117         init_waitqueue_head(&uctxt->wait);
1118         strlcpy(uctxt->comm, current->comm, sizeof(uctxt->comm));
1119         memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid));
1120         uctxt->jkey = generate_jkey(current_uid());
1121         hfi1_stats.sps_ctxts++;
1122         /*
1123          * Disable ASPM when there are open user/PSM contexts to avoid
1124          * issues with ASPM L1 exit latency
1125          */
1126         if (dd->freectxts-- == dd->num_user_contexts)
1127                 aspm_disable_all(dd);
1128
1129         *rcd = uctxt;
1130
1131         return 0;
1132
1133 ctxdata_free:
1134         hfi1_free_ctxt(uctxt);
1135         return ret;
1136 }
1137
1138 static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt)
1139 {
1140         mutex_lock(&hfi1_mutex);
1141         hfi1_stats.sps_ctxts--;
1142         if (++uctxt->dd->freectxts == uctxt->dd->num_user_contexts)
1143                 aspm_enable_all(uctxt->dd);
1144         mutex_unlock(&hfi1_mutex);
1145
1146         hfi1_free_ctxt(uctxt);
1147 }
1148
1149 static void init_subctxts(struct hfi1_ctxtdata *uctxt,
1150                           const struct hfi1_user_info *uinfo)
1151 {
1152         uctxt->subctxt_cnt = uinfo->subctxt_cnt;
1153         uctxt->subctxt_id = uinfo->subctxt_id;
1154         set_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
1155 }
1156
1157 static int setup_subctxt(struct hfi1_ctxtdata *uctxt)
1158 {
1159         int ret = 0;
1160         u16 num_subctxts = uctxt->subctxt_cnt;
1161
1162         uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE);
1163         if (!uctxt->subctxt_uregbase)
1164                 return -ENOMEM;
1165
1166         /* We can take the size of the RcvHdr Queue from the master */
1167         uctxt->subctxt_rcvhdr_base = vmalloc_user(uctxt->rcvhdrq_size *
1168                                                   num_subctxts);
1169         if (!uctxt->subctxt_rcvhdr_base) {
1170                 ret = -ENOMEM;
1171                 goto bail_ureg;
1172         }
1173
1174         uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size *
1175                                                 num_subctxts);
1176         if (!uctxt->subctxt_rcvegrbuf) {
1177                 ret = -ENOMEM;
1178                 goto bail_rhdr;
1179         }
1180
1181         return 0;
1182
1183 bail_rhdr:
1184         vfree(uctxt->subctxt_rcvhdr_base);
1185         uctxt->subctxt_rcvhdr_base = NULL;
1186 bail_ureg:
1187         vfree(uctxt->subctxt_uregbase);
1188         uctxt->subctxt_uregbase = NULL;
1189
1190         return ret;
1191 }
1192
1193 static void user_init(struct hfi1_ctxtdata *uctxt)
1194 {
1195         unsigned int rcvctrl_ops = 0;
1196
1197         /* initialize poll variables... */
1198         uctxt->urgent = 0;
1199         uctxt->urgent_poll = 0;
1200
1201         /*
1202          * Now enable the ctxt for receive.
1203          * For chips that are set to DMA the tail register to memory
1204          * when they change (and when the update bit transitions from
1205          * 0 to 1.  So for those chips, we turn it off and then back on.
1206          * This will (very briefly) affect any other open ctxts, but the
1207          * duration is very short, and therefore isn't an issue.  We
1208          * explicitly set the in-memory tail copy to 0 beforehand, so we
1209          * don't have to wait to be sure the DMA update has happened
1210          * (chip resets head/tail to 0 on transition to enable).
1211          */
1212         if (uctxt->rcvhdrtail_kvaddr)
1213                 clear_rcvhdrtail(uctxt);
1214
1215         /* Setup J_KEY before enabling the context */
1216         hfi1_set_ctxt_jkey(uctxt->dd, uctxt, uctxt->jkey);
1217
1218         rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB;
1219         if (HFI1_CAP_UGET_MASK(uctxt->flags, HDRSUPP))
1220                 rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB;
1221         /*
1222          * Ignore the bit in the flags for now until proper
1223          * support for multiple packet per rcv array entry is
1224          * added.
1225          */
1226         if (!HFI1_CAP_UGET_MASK(uctxt->flags, MULTI_PKT_EGR))
1227                 rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
1228         if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_EGR_FULL))
1229                 rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
1230         if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_RHQ_FULL))
1231                 rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
1232         /*
1233          * The RcvCtxtCtrl.TailUpd bit has to be explicitly written.
1234          * We can't rely on the correct value to be set from prior
1235          * uses of the chip or ctxt. Therefore, add the rcvctrl op
1236          * for both cases.
1237          */
1238         if (HFI1_CAP_UGET_MASK(uctxt->flags, DMA_RTAIL))
1239                 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB;
1240         else
1241                 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS;
1242         hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt);
1243 }
1244
1245 static int get_ctxt_info(struct hfi1_filedata *fd, void __user *ubase,
1246                          __u32 len)
1247 {
1248         struct hfi1_ctxt_info cinfo;
1249         struct hfi1_ctxtdata *uctxt = fd->uctxt;
1250         int ret = 0;
1251
1252         memset(&cinfo, 0, sizeof(cinfo));
1253         cinfo.runtime_flags = (((uctxt->flags >> HFI1_CAP_MISC_SHIFT) &
1254                                 HFI1_CAP_MISC_MASK) << HFI1_CAP_USER_SHIFT) |
1255                         HFI1_CAP_UGET_MASK(uctxt->flags, MASK) |
1256                         HFI1_CAP_KGET_MASK(uctxt->flags, K2U);
1257         /* adjust flag if this fd is not able to cache */
1258         if (!fd->handler)
1259                 cinfo.runtime_flags |= HFI1_CAP_TID_UNMAP; /* no caching */
1260
1261         cinfo.num_active = hfi1_count_active_units();
1262         cinfo.unit = uctxt->dd->unit;
1263         cinfo.ctxt = uctxt->ctxt;
1264         cinfo.subctxt = fd->subctxt;
1265         cinfo.rcvtids = roundup(uctxt->egrbufs.alloced,
1266                                 uctxt->dd->rcv_entries.group_size) +
1267                 uctxt->expected_count;
1268         cinfo.credits = uctxt->sc->credits;
1269         cinfo.numa_node = uctxt->numa_id;
1270         cinfo.rec_cpu = fd->rec_cpu_num;
1271         cinfo.send_ctxt = uctxt->sc->hw_context;
1272
1273         cinfo.egrtids = uctxt->egrbufs.alloced;
1274         cinfo.rcvhdrq_cnt = uctxt->rcvhdrq_cnt;
1275         cinfo.rcvhdrq_entsize = uctxt->rcvhdrqentsize << 2;
1276         cinfo.sdma_ring_size = fd->cq->nentries;
1277         cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size;
1278
1279         trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, cinfo);
1280         if (copy_to_user(ubase, &cinfo, sizeof(cinfo)))
1281                 ret = -EFAULT;
1282
1283         return ret;
1284 }
1285
1286 static int init_user_ctxt(struct hfi1_filedata *fd,
1287                           struct hfi1_ctxtdata *uctxt)
1288 {
1289         int ret;
1290
1291         ret = hfi1_user_sdma_alloc_queues(uctxt, fd);
1292         if (ret)
1293                 return ret;
1294
1295         ret = hfi1_user_exp_rcv_init(fd, uctxt);
1296         if (ret)
1297                 hfi1_user_sdma_free_queues(fd, uctxt);
1298
1299         return ret;
1300 }
1301
1302 static int setup_base_ctxt(struct hfi1_filedata *fd,
1303                            struct hfi1_ctxtdata *uctxt)
1304 {
1305         struct hfi1_devdata *dd = uctxt->dd;
1306         int ret = 0;
1307
1308         hfi1_init_ctxt(uctxt->sc);
1309
1310         /* Now allocate the RcvHdr queue and eager buffers. */
1311         ret = hfi1_create_rcvhdrq(dd, uctxt);
1312         if (ret)
1313                 goto done;
1314
1315         ret = hfi1_setup_eagerbufs(uctxt);
1316         if (ret)
1317                 goto done;
1318
1319         /* If sub-contexts are enabled, do the appropriate setup */
1320         if (uctxt->subctxt_cnt)
1321                 ret = setup_subctxt(uctxt);
1322         if (ret)
1323                 goto done;
1324
1325         ret = hfi1_alloc_ctxt_rcv_groups(uctxt);
1326         if (ret)
1327                 goto done;
1328
1329         ret = init_user_ctxt(fd, uctxt);
1330         if (ret) {
1331                 hfi1_free_ctxt_rcv_groups(uctxt);
1332                 goto done;
1333         }
1334
1335         user_init(uctxt);
1336
1337         /* Now that the context is set up, the fd can get a reference. */
1338         fd->uctxt = uctxt;
1339         hfi1_rcd_get(uctxt);
1340
1341 done:
1342         if (uctxt->subctxt_cnt) {
1343                 /*
1344                  * On error, set the failed bit so sub-contexts will clean up
1345                  * correctly.
1346                  */
1347                 if (ret)
1348                         set_bit(HFI1_CTXT_BASE_FAILED, &uctxt->event_flags);
1349
1350                 /*
1351                  * Base context is done (successfully or not), notify anybody
1352                  * using a sub-context that is waiting for this completion.
1353                  */
1354                 clear_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
1355                 wake_up(&uctxt->wait);
1356         }
1357
1358         return ret;
1359 }
1360
1361 static int get_base_info(struct hfi1_filedata *fd, void __user *ubase,
1362                          __u32 len)
1363 {
1364         struct hfi1_base_info binfo;
1365         struct hfi1_ctxtdata *uctxt = fd->uctxt;
1366         struct hfi1_devdata *dd = uctxt->dd;
1367         ssize_t sz;
1368         unsigned offset;
1369         int ret = 0;
1370
1371         trace_hfi1_uctxtdata(uctxt->dd, uctxt, fd->subctxt);
1372
1373         memset(&binfo, 0, sizeof(binfo));
1374         binfo.hw_version = dd->revision;
1375         binfo.sw_version = HFI1_KERN_SWVERSION;
1376         binfo.bthqp = kdeth_qp;
1377         binfo.jkey = uctxt->jkey;
1378         /*
1379          * If more than 64 contexts are enabled the allocated credit
1380          * return will span two or three contiguous pages. Since we only
1381          * map the page containing the context's credit return address,
1382          * we need to calculate the offset in the proper page.
1383          */
1384         offset = ((u64)uctxt->sc->hw_free -
1385                   (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE;
1386         binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt,
1387                                                 fd->subctxt, offset);
1388         binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt,
1389                                             fd->subctxt,
1390                                             uctxt->sc->base_addr);
1391         binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP,
1392                                                 uctxt->ctxt,
1393                                                 fd->subctxt,
1394                                                 uctxt->sc->base_addr);
1395         binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt,
1396                                                fd->subctxt,
1397                                                uctxt->rcvhdrq);
1398         binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt,
1399                                                fd->subctxt,
1400                                                uctxt->egrbufs.rcvtids[0].dma);
1401         binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt,
1402                                                  fd->subctxt, 0);
1403         /*
1404          * user regs are at
1405          * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE))
1406          */
1407         binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt,
1408                                             fd->subctxt, 0);
1409         offset = offset_in_page((((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
1410                     HFI1_MAX_SHARED_CTXTS) + fd->subctxt) *
1411                   sizeof(*dd->events));
1412         binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt,
1413                                               fd->subctxt,
1414                                               offset);
1415         binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt,
1416                                               fd->subctxt,
1417                                               dd->status);
1418         if (HFI1_CAP_IS_USET(DMA_RTAIL))
1419                 binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt,
1420                                                        fd->subctxt, 0);
1421         if (uctxt->subctxt_cnt) {
1422                 binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS,
1423                                                         uctxt->ctxt,
1424                                                         fd->subctxt, 0);
1425                 binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ,
1426                                                          uctxt->ctxt,
1427                                                          fd->subctxt, 0);
1428                 binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF,
1429                                                          uctxt->ctxt,
1430                                                          fd->subctxt, 0);
1431         }
1432         sz = (len < sizeof(binfo)) ? len : sizeof(binfo);
1433         if (copy_to_user(ubase, &binfo, sz))
1434                 ret = -EFAULT;
1435         return ret;
1436 }
1437
1438 static unsigned int poll_urgent(struct file *fp,
1439                                 struct poll_table_struct *pt)
1440 {
1441         struct hfi1_filedata *fd = fp->private_data;
1442         struct hfi1_ctxtdata *uctxt = fd->uctxt;
1443         struct hfi1_devdata *dd = uctxt->dd;
1444         unsigned pollflag;
1445
1446         poll_wait(fp, &uctxt->wait, pt);
1447
1448         spin_lock_irq(&dd->uctxt_lock);
1449         if (uctxt->urgent != uctxt->urgent_poll) {
1450                 pollflag = POLLIN | POLLRDNORM;
1451                 uctxt->urgent_poll = uctxt->urgent;
1452         } else {
1453                 pollflag = 0;
1454                 set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags);
1455         }
1456         spin_unlock_irq(&dd->uctxt_lock);
1457
1458         return pollflag;
1459 }
1460
1461 static unsigned int poll_next(struct file *fp,
1462                               struct poll_table_struct *pt)
1463 {
1464         struct hfi1_filedata *fd = fp->private_data;
1465         struct hfi1_ctxtdata *uctxt = fd->uctxt;
1466         struct hfi1_devdata *dd = uctxt->dd;
1467         unsigned pollflag;
1468
1469         poll_wait(fp, &uctxt->wait, pt);
1470
1471         spin_lock_irq(&dd->uctxt_lock);
1472         if (hdrqempty(uctxt)) {
1473                 set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags);
1474                 hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt);
1475                 pollflag = 0;
1476         } else {
1477                 pollflag = POLLIN | POLLRDNORM;
1478         }
1479         spin_unlock_irq(&dd->uctxt_lock);
1480
1481         return pollflag;
1482 }
1483
1484 /*
1485  * Find all user contexts in use, and set the specified bit in their
1486  * event mask.
1487  * See also find_ctxt() for a similar use, that is specific to send buffers.
1488  */
1489 int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit)
1490 {
1491         struct hfi1_ctxtdata *uctxt;
1492         struct hfi1_devdata *dd = ppd->dd;
1493         u16 ctxt;
1494
1495         if (!dd->events)
1496                 return -EINVAL;
1497
1498         for (ctxt = dd->first_dyn_alloc_ctxt; ctxt < dd->num_rcv_contexts;
1499              ctxt++) {
1500                 uctxt = hfi1_rcd_get_by_index(dd, ctxt);
1501                 if (uctxt) {
1502                         unsigned long *evs = dd->events +
1503                                 (uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
1504                                 HFI1_MAX_SHARED_CTXTS;
1505                         int i;
1506                         /*
1507                          * subctxt_cnt is 0 if not shared, so do base
1508                          * separately, first, then remaining subctxt, if any
1509                          */
1510                         set_bit(evtbit, evs);
1511                         for (i = 1; i < uctxt->subctxt_cnt; i++)
1512                                 set_bit(evtbit, evs + i);
1513                         hfi1_rcd_put(uctxt);
1514                 }
1515         }
1516
1517         return 0;
1518 }
1519
1520 /**
1521  * manage_rcvq - manage a context's receive queue
1522  * @uctxt: the context
1523  * @subctxt: the sub-context
1524  * @start_stop: action to carry out
1525  *
1526  * start_stop == 0 disables receive on the context, for use in queue
1527  * overflow conditions.  start_stop==1 re-enables, to be used to
1528  * re-init the software copy of the head register
1529  */
1530 static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
1531                        int start_stop)
1532 {
1533         struct hfi1_devdata *dd = uctxt->dd;
1534         unsigned int rcvctrl_op;
1535
1536         if (subctxt)
1537                 goto bail;
1538         /* atomically clear receive enable ctxt. */
1539         if (start_stop) {
1540                 /*
1541                  * On enable, force in-memory copy of the tail register to
1542                  * 0, so that protocol code doesn't have to worry about
1543                  * whether or not the chip has yet updated the in-memory
1544                  * copy or not on return from the system call. The chip
1545                  * always resets it's tail register back to 0 on a
1546                  * transition from disabled to enabled.
1547                  */
1548                 if (uctxt->rcvhdrtail_kvaddr)
1549                         clear_rcvhdrtail(uctxt);
1550                 rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB;
1551         } else {
1552                 rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS;
1553         }
1554         hfi1_rcvctrl(dd, rcvctrl_op, uctxt);
1555         /* always; new head should be equal to new tail; see above */
1556 bail:
1557         return 0;
1558 }
1559
1560 /*
1561  * clear the event notifier events for this context.
1562  * User process then performs actions appropriate to bit having been
1563  * set, if desired, and checks again in future.
1564  */
1565 static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
1566                           unsigned long events)
1567 {
1568         int i;
1569         struct hfi1_devdata *dd = uctxt->dd;
1570         unsigned long *evs;
1571
1572         if (!dd->events)
1573                 return 0;
1574
1575         evs = dd->events + ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
1576                             HFI1_MAX_SHARED_CTXTS) + subctxt;
1577
1578         for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) {
1579                 if (!test_bit(i, &events))
1580                         continue;
1581                 clear_bit(i, evs);
1582         }
1583         return 0;
1584 }
1585
1586 static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, u16 subctxt, u16 pkey)
1587 {
1588         int ret = -ENOENT, i, intable = 0;
1589         struct hfi1_pportdata *ppd = uctxt->ppd;
1590         struct hfi1_devdata *dd = uctxt->dd;
1591
1592         if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY) {
1593                 ret = -EINVAL;
1594                 goto done;
1595         }
1596
1597         for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++)
1598                 if (pkey == ppd->pkeys[i]) {
1599                         intable = 1;
1600                         break;
1601                 }
1602
1603         if (intable)
1604                 ret = hfi1_set_ctxt_pkey(dd, uctxt, pkey);
1605 done:
1606         return ret;
1607 }
1608
1609 static void user_remove(struct hfi1_devdata *dd)
1610 {
1611
1612         hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device);
1613 }
1614
1615 static int user_add(struct hfi1_devdata *dd)
1616 {
1617         char name[10];
1618         int ret;
1619
1620         snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit);
1621         ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops,
1622                              &dd->user_cdev, &dd->user_device,
1623                              true, &dd->kobj);
1624         if (ret)
1625                 user_remove(dd);
1626
1627         return ret;
1628 }
1629
1630 /*
1631  * Create per-unit files in /dev
1632  */
1633 int hfi1_device_create(struct hfi1_devdata *dd)
1634 {
1635         return user_add(dd);
1636 }
1637
1638 /*
1639  * Remove per-unit files in /dev
1640  * void, core kernel returns no errors for this stuff
1641  */
1642 void hfi1_device_remove(struct hfi1_devdata *dd)
1643 {
1644         user_remove(dd);
1645 }