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