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