This source file includes following definitions.
- is_valid_mmap
- hfi1_file_open
- hfi1_file_ioctl
- hfi1_write_iter
- hfi1_file_mmap
- vma_fault
- hfi1_poll
- hfi1_file_close
- kvirt_to_phys
- complete_subctxt
- assign_ctxt
- match_ctxt
- find_sub_ctxt
- allocate_ctxt
- deallocate_ctxt
- init_subctxts
- setup_subctxt
- user_init
- get_ctxt_info
- init_user_ctxt
- setup_base_ctxt
- get_base_info
- user_exp_rcv_setup
- user_exp_rcv_clear
- user_exp_rcv_invalid
- poll_urgent
- poll_next
- hfi1_set_uevent_bits
- manage_rcvq
- user_event_ack
- set_ctxt_pkey
- ctxt_reset
- user_remove
- user_add
- hfi1_device_create
- hfi1_device_remove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
70
71
72
73
74 static int hfi1_file_open(struct inode *inode, struct file *fp);
75 static int hfi1_file_close(struct inode *inode, struct file *fp);
76 static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from);
77 static __poll_t hfi1_poll(struct file *fp, struct poll_table_struct *pt);
78 static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma);
79
80 static u64 kvirt_to_phys(void *addr);
81 static int assign_ctxt(struct hfi1_filedata *fd, unsigned long arg, u32 len);
82 static void init_subctxts(struct hfi1_ctxtdata *uctxt,
83 const struct hfi1_user_info *uinfo);
84 static int init_user_ctxt(struct hfi1_filedata *fd,
85 struct hfi1_ctxtdata *uctxt);
86 static void user_init(struct hfi1_ctxtdata *uctxt);
87 static int get_ctxt_info(struct hfi1_filedata *fd, unsigned long arg, u32 len);
88 static int get_base_info(struct hfi1_filedata *fd, unsigned long arg, u32 len);
89 static int user_exp_rcv_setup(struct hfi1_filedata *fd, unsigned long arg,
90 u32 len);
91 static int user_exp_rcv_clear(struct hfi1_filedata *fd, unsigned long arg,
92 u32 len);
93 static int user_exp_rcv_invalid(struct hfi1_filedata *fd, unsigned long arg,
94 u32 len);
95 static int setup_base_ctxt(struct hfi1_filedata *fd,
96 struct hfi1_ctxtdata *uctxt);
97 static int setup_subctxt(struct hfi1_ctxtdata *uctxt);
98
99 static int find_sub_ctxt(struct hfi1_filedata *fd,
100 const struct hfi1_user_info *uinfo);
101 static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
102 struct hfi1_user_info *uinfo,
103 struct hfi1_ctxtdata **cd);
104 static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt);
105 static __poll_t poll_urgent(struct file *fp, struct poll_table_struct *pt);
106 static __poll_t poll_next(struct file *fp, struct poll_table_struct *pt);
107 static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
108 unsigned long arg);
109 static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned long arg);
110 static int ctxt_reset(struct hfi1_ctxtdata *uctxt);
111 static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
112 unsigned long arg);
113 static vm_fault_t vma_fault(struct vm_fault *vmf);
114 static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
115 unsigned long arg);
116
117 static 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
128 static const struct vm_operations_struct vm_ops = {
129 .fault = vma_fault,
130 };
131
132
133
134
135 enum 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
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
181 static inline int is_valid_mmap(u64 token)
182 {
183 return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC);
184 }
185
186 static 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
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;
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;
215 nomem:
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
223 static 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
302 static 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 idx = srcu_read_lock(&fd->pq_srcu);
312 pq = srcu_dereference(fd->pq, &fd->pq_srcu);
313 if (!cq || !pq) {
314 srcu_read_unlock(&fd->pq_srcu, idx);
315 return -EIO;
316 }
317
318 if (!iter_is_iovec(from) || !dim) {
319 srcu_read_unlock(&fd->pq_srcu, idx);
320 return -EINVAL;
321 }
322
323 trace_hfi1_sdma_request(fd->dd, fd->uctxt->ctxt, fd->subctxt, dim);
324
325 if (atomic_read(&pq->n_reqs) == pq->n_max_reqs) {
326 srcu_read_unlock(&fd->pq_srcu, idx);
327 return -ENOSPC;
328 }
329
330 while (dim) {
331 int ret;
332 unsigned long count = 0;
333
334 ret = hfi1_user_sdma_process_request(
335 fd, (struct iovec *)(from->iov + done),
336 dim, &count);
337 if (ret) {
338 reqs = ret;
339 break;
340 }
341 dim -= count;
342 done += count;
343 reqs++;
344 }
345
346 srcu_read_unlock(&fd->pq_srcu, idx);
347 return reqs;
348 }
349
350 static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma)
351 {
352 struct hfi1_filedata *fd = fp->private_data;
353 struct hfi1_ctxtdata *uctxt = fd->uctxt;
354 struct hfi1_devdata *dd;
355 unsigned long flags;
356 u64 token = vma->vm_pgoff << PAGE_SHIFT,
357 memaddr = 0;
358 void *memvirt = NULL;
359 u8 subctxt, mapio = 0, vmf = 0, type;
360 ssize_t memlen = 0;
361 int ret = 0;
362 u16 ctxt;
363
364 if (!is_valid_mmap(token) || !uctxt ||
365 !(vma->vm_flags & VM_SHARED)) {
366 ret = -EINVAL;
367 goto done;
368 }
369 dd = uctxt->dd;
370 ctxt = HFI1_MMAP_TOKEN_GET(CTXT, token);
371 subctxt = HFI1_MMAP_TOKEN_GET(SUBCTXT, token);
372 type = HFI1_MMAP_TOKEN_GET(TYPE, token);
373 if (ctxt != uctxt->ctxt || subctxt != fd->subctxt) {
374 ret = -EINVAL;
375 goto done;
376 }
377
378 flags = vma->vm_flags;
379
380 switch (type) {
381 case PIO_BUFS:
382 case PIO_BUFS_SOP:
383 memaddr = ((dd->physaddr + TXE_PIO_SEND) +
384
385 (uctxt->sc->hw_context * BIT(16))) +
386
387 (type == PIO_BUFS_SOP ?
388 (TXE_PIO_SIZE / 2) : 0);
389
390
391
392
393 memlen = PAGE_ALIGN(uctxt->sc->credits * PIO_BLOCK_SIZE);
394 flags &= ~VM_MAYREAD;
395 flags |= VM_DONTCOPY | VM_DONTEXPAND;
396 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
397 mapio = 1;
398 break;
399 case PIO_CRED:
400 if (flags & VM_WRITE) {
401 ret = -EPERM;
402 goto done;
403 }
404
405
406
407
408
409 memvirt = dd->cr_base[uctxt->numa_id].va;
410 memaddr = virt_to_phys(memvirt) +
411 (((u64)uctxt->sc->hw_free -
412 (u64)dd->cr_base[uctxt->numa_id].va) & PAGE_MASK);
413 memlen = PAGE_SIZE;
414 flags &= ~VM_MAYWRITE;
415 flags |= VM_DONTCOPY | VM_DONTEXPAND;
416
417
418
419
420
421
422 mapio = 1;
423 break;
424 case RCV_HDRQ:
425 memlen = rcvhdrq_size(uctxt);
426 memvirt = uctxt->rcvhdrq;
427 break;
428 case RCV_EGRBUF: {
429 unsigned long addr;
430 int i;
431
432
433
434
435
436 memlen = uctxt->egrbufs.size;
437 if ((vma->vm_end - vma->vm_start) != memlen) {
438 dd_dev_err(dd, "Eager buffer map size invalid (%lu != %lu)\n",
439 (vma->vm_end - vma->vm_start), memlen);
440 ret = -EINVAL;
441 goto done;
442 }
443 if (vma->vm_flags & VM_WRITE) {
444 ret = -EPERM;
445 goto done;
446 }
447 vma->vm_flags &= ~VM_MAYWRITE;
448 addr = vma->vm_start;
449 for (i = 0 ; i < uctxt->egrbufs.numbufs; i++) {
450 memlen = uctxt->egrbufs.buffers[i].len;
451 memvirt = uctxt->egrbufs.buffers[i].addr;
452 ret = remap_pfn_range(
453 vma, addr,
454
455
456
457
458
459 PFN_DOWN(__pa(memvirt)),
460 memlen,
461 vma->vm_page_prot);
462 if (ret < 0)
463 goto done;
464 addr += memlen;
465 }
466 ret = 0;
467 goto done;
468 }
469 case UREGS:
470
471
472
473
474 memaddr = (unsigned long)
475 (dd->physaddr + RXE_PER_CONTEXT_USER)
476 + (uctxt->ctxt * RXE_PER_CONTEXT_SIZE);
477
478
479
480
481 memlen = PAGE_SIZE;
482 flags |= VM_DONTCOPY | VM_DONTEXPAND;
483 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
484 mapio = 1;
485 break;
486 case EVENTS:
487
488
489
490
491 memaddr = (unsigned long)
492 (dd->events + uctxt_offset(uctxt)) & PAGE_MASK;
493 memlen = PAGE_SIZE;
494
495
496
497
498 flags |= VM_IO | VM_DONTEXPAND;
499 vmf = 1;
500 break;
501 case STATUS:
502 if (flags & VM_WRITE) {
503 ret = -EPERM;
504 goto done;
505 }
506 memaddr = kvirt_to_phys((void *)dd->status);
507 memlen = PAGE_SIZE;
508 flags |= VM_IO | VM_DONTEXPAND;
509 break;
510 case RTAIL:
511 if (!HFI1_CAP_IS_USET(DMA_RTAIL)) {
512
513
514
515
516 ret = -EINVAL;
517 goto done;
518 }
519 if ((flags & VM_WRITE) || !uctxt->rcvhdrtail_kvaddr) {
520 ret = -EPERM;
521 goto done;
522 }
523 memlen = PAGE_SIZE;
524 memvirt = (void *)uctxt->rcvhdrtail_kvaddr;
525 flags &= ~VM_MAYWRITE;
526 break;
527 case SUBCTXT_UREGS:
528 memaddr = (u64)uctxt->subctxt_uregbase;
529 memlen = PAGE_SIZE;
530 flags |= VM_IO | VM_DONTEXPAND;
531 vmf = 1;
532 break;
533 case SUBCTXT_RCV_HDRQ:
534 memaddr = (u64)uctxt->subctxt_rcvhdr_base;
535 memlen = rcvhdrq_size(uctxt) * uctxt->subctxt_cnt;
536 flags |= VM_IO | VM_DONTEXPAND;
537 vmf = 1;
538 break;
539 case SUBCTXT_EGRBUF:
540 memaddr = (u64)uctxt->subctxt_rcvegrbuf;
541 memlen = uctxt->egrbufs.size * uctxt->subctxt_cnt;
542 flags |= VM_IO | VM_DONTEXPAND;
543 flags &= ~VM_MAYWRITE;
544 vmf = 1;
545 break;
546 case SDMA_COMP: {
547 struct hfi1_user_sdma_comp_q *cq = fd->cq;
548
549 if (!cq) {
550 ret = -EFAULT;
551 goto done;
552 }
553 memaddr = (u64)cq->comps;
554 memlen = PAGE_ALIGN(sizeof(*cq->comps) * cq->nentries);
555 flags |= VM_IO | VM_DONTEXPAND;
556 vmf = 1;
557 break;
558 }
559 default:
560 ret = -EINVAL;
561 break;
562 }
563
564 if ((vma->vm_end - vma->vm_start) != memlen) {
565 hfi1_cdbg(PROC, "%u:%u Memory size mismatch %lu:%lu",
566 uctxt->ctxt, fd->subctxt,
567 (vma->vm_end - vma->vm_start), memlen);
568 ret = -EINVAL;
569 goto done;
570 }
571
572 vma->vm_flags = flags;
573 hfi1_cdbg(PROC,
574 "%u:%u type:%u io/vf:%d/%d, addr:0x%llx, len:%lu(%lu), flags:0x%lx\n",
575 ctxt, subctxt, type, mapio, vmf, memaddr, memlen,
576 vma->vm_end - vma->vm_start, vma->vm_flags);
577 if (vmf) {
578 vma->vm_pgoff = PFN_DOWN(memaddr);
579 vma->vm_ops = &vm_ops;
580 ret = 0;
581 } else if (mapio) {
582 ret = io_remap_pfn_range(vma, vma->vm_start,
583 PFN_DOWN(memaddr),
584 memlen,
585 vma->vm_page_prot);
586 } else if (memvirt) {
587 ret = remap_pfn_range(vma, vma->vm_start,
588 PFN_DOWN(__pa(memvirt)),
589 memlen,
590 vma->vm_page_prot);
591 } else {
592 ret = remap_pfn_range(vma, vma->vm_start,
593 PFN_DOWN(memaddr),
594 memlen,
595 vma->vm_page_prot);
596 }
597 done:
598 return ret;
599 }
600
601
602
603
604
605 static vm_fault_t vma_fault(struct vm_fault *vmf)
606 {
607 struct page *page;
608
609 page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
610 if (!page)
611 return VM_FAULT_SIGBUS;
612
613 get_page(page);
614 vmf->page = page;
615
616 return 0;
617 }
618
619 static __poll_t hfi1_poll(struct file *fp, struct poll_table_struct *pt)
620 {
621 struct hfi1_ctxtdata *uctxt;
622 __poll_t pollflag;
623
624 uctxt = ((struct hfi1_filedata *)fp->private_data)->uctxt;
625 if (!uctxt)
626 pollflag = EPOLLERR;
627 else if (uctxt->poll_type == HFI1_POLL_TYPE_URGENT)
628 pollflag = poll_urgent(fp, pt);
629 else if (uctxt->poll_type == HFI1_POLL_TYPE_ANYRCV)
630 pollflag = poll_next(fp, pt);
631 else
632 pollflag = EPOLLERR;
633
634 return pollflag;
635 }
636
637 static int hfi1_file_close(struct inode *inode, struct file *fp)
638 {
639 struct hfi1_filedata *fdata = fp->private_data;
640 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
641 struct hfi1_devdata *dd = container_of(inode->i_cdev,
642 struct hfi1_devdata,
643 user_cdev);
644 unsigned long flags, *ev;
645
646 fp->private_data = NULL;
647
648 if (!uctxt)
649 goto done;
650
651 hfi1_cdbg(PROC, "closing ctxt %u:%u", uctxt->ctxt, fdata->subctxt);
652
653 flush_wc();
654
655 hfi1_user_sdma_free_queues(fdata, uctxt);
656
657
658 hfi1_put_proc_affinity(fdata->rec_cpu_num);
659
660
661 hfi1_user_exp_rcv_free(fdata);
662
663
664
665
666
667 fdata->uctxt = NULL;
668 hfi1_rcd_put(uctxt);
669
670
671
672
673
674 ev = dd->events + uctxt_offset(uctxt) + fdata->subctxt;
675 *ev = 0;
676
677 spin_lock_irqsave(&dd->uctxt_lock, flags);
678 __clear_bit(fdata->subctxt, uctxt->in_use_ctxts);
679 if (!bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
680 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
681 goto done;
682 }
683 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
684
685
686
687
688
689 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
690 HFI1_RCVCTRL_TIDFLOW_DIS |
691 HFI1_RCVCTRL_INTRAVAIL_DIS |
692 HFI1_RCVCTRL_TAILUPD_DIS |
693 HFI1_RCVCTRL_ONE_PKT_EGR_DIS |
694 HFI1_RCVCTRL_NO_RHQ_DROP_DIS |
695 HFI1_RCVCTRL_NO_EGR_DROP_DIS |
696 HFI1_RCVCTRL_URGENT_DIS, uctxt);
697
698 hfi1_clear_ctxt_jkey(dd, uctxt);
699
700
701
702
703 if (uctxt->sc) {
704 sc_disable(uctxt->sc);
705 set_pio_integrity(uctxt->sc);
706 }
707
708 hfi1_free_ctxt_rcv_groups(uctxt);
709 hfi1_clear_ctxt_pkey(dd, uctxt);
710
711 uctxt->event_flags = 0;
712
713 deallocate_ctxt(uctxt);
714 done:
715 mmdrop(fdata->mm);
716 kobject_put(&dd->kobj);
717
718 if (atomic_dec_and_test(&dd->user_refcount))
719 complete(&dd->user_comp);
720
721 cleanup_srcu_struct(&fdata->pq_srcu);
722 kfree(fdata);
723 return 0;
724 }
725
726
727
728
729
730 static u64 kvirt_to_phys(void *addr)
731 {
732 struct page *page;
733 u64 paddr = 0;
734
735 page = vmalloc_to_page(addr);
736 if (page)
737 paddr = page_to_pfn(page) << PAGE_SHIFT;
738
739 return paddr;
740 }
741
742
743
744
745
746
747
748
749
750
751
752
753
754 static int complete_subctxt(struct hfi1_filedata *fd)
755 {
756 int ret;
757 unsigned long flags;
758
759
760
761
762
763 ret = wait_event_interruptible(
764 fd->uctxt->wait,
765 !test_bit(HFI1_CTXT_BASE_UNINIT, &fd->uctxt->event_flags));
766
767 if (test_bit(HFI1_CTXT_BASE_FAILED, &fd->uctxt->event_flags))
768 ret = -ENOMEM;
769
770
771 if (!ret) {
772 fd->rec_cpu_num = hfi1_get_proc_affinity(fd->uctxt->numa_id);
773 ret = init_user_ctxt(fd, fd->uctxt);
774 }
775
776 if (ret) {
777 spin_lock_irqsave(&fd->dd->uctxt_lock, flags);
778 __clear_bit(fd->subctxt, fd->uctxt->in_use_ctxts);
779 spin_unlock_irqrestore(&fd->dd->uctxt_lock, flags);
780 hfi1_rcd_put(fd->uctxt);
781 fd->uctxt = NULL;
782 }
783
784 return ret;
785 }
786
787 static int assign_ctxt(struct hfi1_filedata *fd, unsigned long arg, u32 len)
788 {
789 int ret;
790 unsigned int swmajor;
791 struct hfi1_ctxtdata *uctxt = NULL;
792 struct hfi1_user_info uinfo;
793
794 if (fd->uctxt)
795 return -EINVAL;
796
797 if (sizeof(uinfo) != len)
798 return -EINVAL;
799
800 if (copy_from_user(&uinfo, (void __user *)arg, sizeof(uinfo)))
801 return -EFAULT;
802
803 swmajor = uinfo.userversion >> 16;
804 if (swmajor != HFI1_USER_SWMAJOR)
805 return -ENODEV;
806
807 if (uinfo.subctxt_cnt > HFI1_MAX_SHARED_CTXTS)
808 return -EINVAL;
809
810
811
812
813
814 mutex_lock(&hfi1_mutex);
815
816
817
818
819 ret = find_sub_ctxt(fd, &uinfo);
820
821
822
823
824
825 if (!ret)
826 ret = allocate_ctxt(fd, fd->dd, &uinfo, &uctxt);
827
828 mutex_unlock(&hfi1_mutex);
829
830
831 switch (ret) {
832 case 0:
833 ret = setup_base_ctxt(fd, uctxt);
834 if (ret)
835 deallocate_ctxt(uctxt);
836 break;
837 case 1:
838 ret = complete_subctxt(fd);
839 break;
840 default:
841 break;
842 }
843
844 return ret;
845 }
846
847
848
849
850
851
852
853
854
855
856 static int match_ctxt(struct hfi1_filedata *fd,
857 const struct hfi1_user_info *uinfo,
858 struct hfi1_ctxtdata *uctxt)
859 {
860 struct hfi1_devdata *dd = fd->dd;
861 unsigned long flags;
862 u16 subctxt;
863
864
865 if (uctxt->sc && (uctxt->sc->type == SC_KERNEL))
866 return 0;
867
868
869 if (memcmp(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid)) ||
870 uctxt->jkey != generate_jkey(current_uid()) ||
871 uctxt->subctxt_id != uinfo->subctxt_id ||
872 uctxt->subctxt_cnt != uinfo->subctxt_cnt)
873 return 0;
874
875
876 if (uctxt->userversion != uinfo->userversion)
877 return -EINVAL;
878
879
880 spin_lock_irqsave(&dd->uctxt_lock, flags);
881 if (bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
882
883 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
884 return 0;
885 }
886
887 subctxt = find_first_zero_bit(uctxt->in_use_ctxts,
888 HFI1_MAX_SHARED_CTXTS);
889 if (subctxt >= uctxt->subctxt_cnt) {
890 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
891 return -EBUSY;
892 }
893
894 fd->subctxt = subctxt;
895 __set_bit(fd->subctxt, uctxt->in_use_ctxts);
896 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
897
898 fd->uctxt = uctxt;
899 hfi1_rcd_get(uctxt);
900
901 return 1;
902 }
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918 static int find_sub_ctxt(struct hfi1_filedata *fd,
919 const struct hfi1_user_info *uinfo)
920 {
921 struct hfi1_ctxtdata *uctxt;
922 struct hfi1_devdata *dd = fd->dd;
923 u16 i;
924 int ret;
925
926 if (!uinfo->subctxt_cnt)
927 return 0;
928
929 for (i = dd->first_dyn_alloc_ctxt; i < dd->num_rcv_contexts; i++) {
930 uctxt = hfi1_rcd_get_by_index(dd, i);
931 if (uctxt) {
932 ret = match_ctxt(fd, uinfo, uctxt);
933 hfi1_rcd_put(uctxt);
934
935 if (ret)
936 return ret;
937 }
938 }
939
940 return 0;
941 }
942
943 static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
944 struct hfi1_user_info *uinfo,
945 struct hfi1_ctxtdata **rcd)
946 {
947 struct hfi1_ctxtdata *uctxt;
948 int ret, numa;
949
950 if (dd->flags & HFI1_FROZEN) {
951
952
953
954
955
956
957
958 return -EIO;
959 }
960
961 if (!dd->freectxts)
962 return -EBUSY;
963
964
965
966
967
968 fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node);
969 if (fd->rec_cpu_num != -1)
970 numa = cpu_to_node(fd->rec_cpu_num);
971 else
972 numa = numa_node_id();
973 ret = hfi1_create_ctxtdata(dd->pport, numa, &uctxt);
974 if (ret < 0) {
975 dd_dev_err(dd, "user ctxtdata allocation failed\n");
976 return ret;
977 }
978 hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)",
979 uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num,
980 uctxt->numa_id);
981
982
983
984
985 uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize, dd->node);
986 if (!uctxt->sc) {
987 ret = -ENOMEM;
988 goto ctxdata_free;
989 }
990 hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index,
991 uctxt->sc->hw_context);
992 ret = sc_enable(uctxt->sc);
993 if (ret)
994 goto ctxdata_free;
995
996
997
998
999
1000
1001
1002
1003
1004
1005 __set_bit(0, uctxt->in_use_ctxts);
1006 if (uinfo->subctxt_cnt)
1007 init_subctxts(uctxt, uinfo);
1008 uctxt->userversion = uinfo->userversion;
1009 uctxt->flags = hfi1_cap_mask;
1010 init_waitqueue_head(&uctxt->wait);
1011 strlcpy(uctxt->comm, current->comm, sizeof(uctxt->comm));
1012 memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid));
1013 uctxt->jkey = generate_jkey(current_uid());
1014 hfi1_stats.sps_ctxts++;
1015
1016
1017
1018
1019 if (dd->freectxts-- == dd->num_user_contexts)
1020 aspm_disable_all(dd);
1021
1022 *rcd = uctxt;
1023
1024 return 0;
1025
1026 ctxdata_free:
1027 hfi1_free_ctxt(uctxt);
1028 return ret;
1029 }
1030
1031 static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt)
1032 {
1033 mutex_lock(&hfi1_mutex);
1034 hfi1_stats.sps_ctxts--;
1035 if (++uctxt->dd->freectxts == uctxt->dd->num_user_contexts)
1036 aspm_enable_all(uctxt->dd);
1037 mutex_unlock(&hfi1_mutex);
1038
1039 hfi1_free_ctxt(uctxt);
1040 }
1041
1042 static void init_subctxts(struct hfi1_ctxtdata *uctxt,
1043 const struct hfi1_user_info *uinfo)
1044 {
1045 uctxt->subctxt_cnt = uinfo->subctxt_cnt;
1046 uctxt->subctxt_id = uinfo->subctxt_id;
1047 set_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
1048 }
1049
1050 static int setup_subctxt(struct hfi1_ctxtdata *uctxt)
1051 {
1052 int ret = 0;
1053 u16 num_subctxts = uctxt->subctxt_cnt;
1054
1055 uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE);
1056 if (!uctxt->subctxt_uregbase)
1057 return -ENOMEM;
1058
1059
1060 uctxt->subctxt_rcvhdr_base = vmalloc_user(rcvhdrq_size(uctxt) *
1061 num_subctxts);
1062 if (!uctxt->subctxt_rcvhdr_base) {
1063 ret = -ENOMEM;
1064 goto bail_ureg;
1065 }
1066
1067 uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size *
1068 num_subctxts);
1069 if (!uctxt->subctxt_rcvegrbuf) {
1070 ret = -ENOMEM;
1071 goto bail_rhdr;
1072 }
1073
1074 return 0;
1075
1076 bail_rhdr:
1077 vfree(uctxt->subctxt_rcvhdr_base);
1078 uctxt->subctxt_rcvhdr_base = NULL;
1079 bail_ureg:
1080 vfree(uctxt->subctxt_uregbase);
1081 uctxt->subctxt_uregbase = NULL;
1082
1083 return ret;
1084 }
1085
1086 static void user_init(struct hfi1_ctxtdata *uctxt)
1087 {
1088 unsigned int rcvctrl_ops = 0;
1089
1090
1091 uctxt->urgent = 0;
1092 uctxt->urgent_poll = 0;
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105 if (uctxt->rcvhdrtail_kvaddr)
1106 clear_rcvhdrtail(uctxt);
1107
1108
1109 hfi1_set_ctxt_jkey(uctxt->dd, uctxt, uctxt->jkey);
1110
1111 rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB;
1112 rcvctrl_ops |= HFI1_RCVCTRL_URGENT_ENB;
1113 if (HFI1_CAP_UGET_MASK(uctxt->flags, HDRSUPP))
1114 rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB;
1115
1116
1117
1118
1119
1120 if (!HFI1_CAP_UGET_MASK(uctxt->flags, MULTI_PKT_EGR))
1121 rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
1122 if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_EGR_FULL))
1123 rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
1124 if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_RHQ_FULL))
1125 rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
1126
1127
1128
1129
1130
1131
1132 if (HFI1_CAP_UGET_MASK(uctxt->flags, DMA_RTAIL))
1133 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB;
1134 else
1135 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS;
1136 hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt);
1137 }
1138
1139 static int get_ctxt_info(struct hfi1_filedata *fd, unsigned long arg, u32 len)
1140 {
1141 struct hfi1_ctxt_info cinfo;
1142 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1143
1144 if (sizeof(cinfo) != len)
1145 return -EINVAL;
1146
1147 memset(&cinfo, 0, sizeof(cinfo));
1148 cinfo.runtime_flags = (((uctxt->flags >> HFI1_CAP_MISC_SHIFT) &
1149 HFI1_CAP_MISC_MASK) << HFI1_CAP_USER_SHIFT) |
1150 HFI1_CAP_UGET_MASK(uctxt->flags, MASK) |
1151 HFI1_CAP_KGET_MASK(uctxt->flags, K2U);
1152
1153 if (!fd->handler)
1154 cinfo.runtime_flags |= HFI1_CAP_TID_UNMAP;
1155
1156 cinfo.num_active = hfi1_count_active_units();
1157 cinfo.unit = uctxt->dd->unit;
1158 cinfo.ctxt = uctxt->ctxt;
1159 cinfo.subctxt = fd->subctxt;
1160 cinfo.rcvtids = roundup(uctxt->egrbufs.alloced,
1161 uctxt->dd->rcv_entries.group_size) +
1162 uctxt->expected_count;
1163 cinfo.credits = uctxt->sc->credits;
1164 cinfo.numa_node = uctxt->numa_id;
1165 cinfo.rec_cpu = fd->rec_cpu_num;
1166 cinfo.send_ctxt = uctxt->sc->hw_context;
1167
1168 cinfo.egrtids = uctxt->egrbufs.alloced;
1169 cinfo.rcvhdrq_cnt = uctxt->rcvhdrq_cnt;
1170 cinfo.rcvhdrq_entsize = uctxt->rcvhdrqentsize << 2;
1171 cinfo.sdma_ring_size = fd->cq->nentries;
1172 cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size;
1173
1174 trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, &cinfo);
1175 if (copy_to_user((void __user *)arg, &cinfo, len))
1176 return -EFAULT;
1177
1178 return 0;
1179 }
1180
1181 static int init_user_ctxt(struct hfi1_filedata *fd,
1182 struct hfi1_ctxtdata *uctxt)
1183 {
1184 int ret;
1185
1186 ret = hfi1_user_sdma_alloc_queues(uctxt, fd);
1187 if (ret)
1188 return ret;
1189
1190 ret = hfi1_user_exp_rcv_init(fd, uctxt);
1191 if (ret)
1192 hfi1_user_sdma_free_queues(fd, uctxt);
1193
1194 return ret;
1195 }
1196
1197 static int setup_base_ctxt(struct hfi1_filedata *fd,
1198 struct hfi1_ctxtdata *uctxt)
1199 {
1200 struct hfi1_devdata *dd = uctxt->dd;
1201 int ret = 0;
1202
1203 hfi1_init_ctxt(uctxt->sc);
1204
1205
1206 ret = hfi1_create_rcvhdrq(dd, uctxt);
1207 if (ret)
1208 goto done;
1209
1210 ret = hfi1_setup_eagerbufs(uctxt);
1211 if (ret)
1212 goto done;
1213
1214
1215 if (uctxt->subctxt_cnt)
1216 ret = setup_subctxt(uctxt);
1217 if (ret)
1218 goto done;
1219
1220 ret = hfi1_alloc_ctxt_rcv_groups(uctxt);
1221 if (ret)
1222 goto done;
1223
1224 ret = init_user_ctxt(fd, uctxt);
1225 if (ret)
1226 goto done;
1227
1228 user_init(uctxt);
1229
1230
1231 fd->uctxt = uctxt;
1232 hfi1_rcd_get(uctxt);
1233
1234 done:
1235 if (uctxt->subctxt_cnt) {
1236
1237
1238
1239
1240 if (ret)
1241 set_bit(HFI1_CTXT_BASE_FAILED, &uctxt->event_flags);
1242
1243
1244
1245
1246
1247 clear_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
1248 wake_up(&uctxt->wait);
1249 }
1250
1251 return ret;
1252 }
1253
1254 static int get_base_info(struct hfi1_filedata *fd, unsigned long arg, u32 len)
1255 {
1256 struct hfi1_base_info binfo;
1257 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1258 struct hfi1_devdata *dd = uctxt->dd;
1259 unsigned offset;
1260
1261 trace_hfi1_uctxtdata(uctxt->dd, uctxt, fd->subctxt);
1262
1263 if (sizeof(binfo) != len)
1264 return -EINVAL;
1265
1266 memset(&binfo, 0, sizeof(binfo));
1267 binfo.hw_version = dd->revision;
1268 binfo.sw_version = HFI1_KERN_SWVERSION;
1269 binfo.bthqp = kdeth_qp;
1270 binfo.jkey = uctxt->jkey;
1271
1272
1273
1274
1275
1276
1277 offset = ((u64)uctxt->sc->hw_free -
1278 (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE;
1279 binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt,
1280 fd->subctxt, offset);
1281 binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt,
1282 fd->subctxt,
1283 uctxt->sc->base_addr);
1284 binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP,
1285 uctxt->ctxt,
1286 fd->subctxt,
1287 uctxt->sc->base_addr);
1288 binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt,
1289 fd->subctxt,
1290 uctxt->rcvhdrq);
1291 binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt,
1292 fd->subctxt,
1293 uctxt->egrbufs.rcvtids[0].dma);
1294 binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt,
1295 fd->subctxt, 0);
1296
1297
1298
1299
1300 binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt,
1301 fd->subctxt, 0);
1302 offset = offset_in_page((uctxt_offset(uctxt) + fd->subctxt) *
1303 sizeof(*dd->events));
1304 binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt,
1305 fd->subctxt,
1306 offset);
1307 binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt,
1308 fd->subctxt,
1309 dd->status);
1310 if (HFI1_CAP_IS_USET(DMA_RTAIL))
1311 binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt,
1312 fd->subctxt, 0);
1313 if (uctxt->subctxt_cnt) {
1314 binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS,
1315 uctxt->ctxt,
1316 fd->subctxt, 0);
1317 binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ,
1318 uctxt->ctxt,
1319 fd->subctxt, 0);
1320 binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF,
1321 uctxt->ctxt,
1322 fd->subctxt, 0);
1323 }
1324
1325 if (copy_to_user((void __user *)arg, &binfo, len))
1326 return -EFAULT;
1327
1328 return 0;
1329 }
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340 static int user_exp_rcv_setup(struct hfi1_filedata *fd, unsigned long arg,
1341 u32 len)
1342 {
1343 int ret;
1344 unsigned long addr;
1345 struct hfi1_tid_info tinfo;
1346
1347 if (sizeof(tinfo) != len)
1348 return -EINVAL;
1349
1350 if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo))))
1351 return -EFAULT;
1352
1353 ret = hfi1_user_exp_rcv_setup(fd, &tinfo);
1354 if (!ret) {
1355
1356
1357
1358
1359 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
1360 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
1361 sizeof(tinfo.tidcnt)))
1362 return -EFAULT;
1363
1364 addr = arg + offsetof(struct hfi1_tid_info, length);
1365 if (copy_to_user((void __user *)addr, &tinfo.length,
1366 sizeof(tinfo.length)))
1367 ret = -EFAULT;
1368 }
1369
1370 return ret;
1371 }
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383 static int user_exp_rcv_clear(struct hfi1_filedata *fd, unsigned long arg,
1384 u32 len)
1385 {
1386 int ret;
1387 unsigned long addr;
1388 struct hfi1_tid_info tinfo;
1389
1390 if (sizeof(tinfo) != len)
1391 return -EINVAL;
1392
1393 if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo))))
1394 return -EFAULT;
1395
1396 ret = hfi1_user_exp_rcv_clear(fd, &tinfo);
1397 if (!ret) {
1398 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
1399 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
1400 sizeof(tinfo.tidcnt)))
1401 return -EFAULT;
1402 }
1403
1404 return ret;
1405 }
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416 static int user_exp_rcv_invalid(struct hfi1_filedata *fd, unsigned long arg,
1417 u32 len)
1418 {
1419 int ret;
1420 unsigned long addr;
1421 struct hfi1_tid_info tinfo;
1422
1423 if (sizeof(tinfo) != len)
1424 return -EINVAL;
1425
1426 if (!fd->invalid_tids)
1427 return -EINVAL;
1428
1429 if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo))))
1430 return -EFAULT;
1431
1432 ret = hfi1_user_exp_rcv_invalid(fd, &tinfo);
1433 if (ret)
1434 return ret;
1435
1436 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
1437 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
1438 sizeof(tinfo.tidcnt)))
1439 ret = -EFAULT;
1440
1441 return ret;
1442 }
1443
1444 static __poll_t poll_urgent(struct file *fp,
1445 struct poll_table_struct *pt)
1446 {
1447 struct hfi1_filedata *fd = fp->private_data;
1448 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1449 struct hfi1_devdata *dd = uctxt->dd;
1450 __poll_t pollflag;
1451
1452 poll_wait(fp, &uctxt->wait, pt);
1453
1454 spin_lock_irq(&dd->uctxt_lock);
1455 if (uctxt->urgent != uctxt->urgent_poll) {
1456 pollflag = EPOLLIN | EPOLLRDNORM;
1457 uctxt->urgent_poll = uctxt->urgent;
1458 } else {
1459 pollflag = 0;
1460 set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags);
1461 }
1462 spin_unlock_irq(&dd->uctxt_lock);
1463
1464 return pollflag;
1465 }
1466
1467 static __poll_t poll_next(struct file *fp,
1468 struct poll_table_struct *pt)
1469 {
1470 struct hfi1_filedata *fd = fp->private_data;
1471 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1472 struct hfi1_devdata *dd = uctxt->dd;
1473 __poll_t pollflag;
1474
1475 poll_wait(fp, &uctxt->wait, pt);
1476
1477 spin_lock_irq(&dd->uctxt_lock);
1478 if (hdrqempty(uctxt)) {
1479 set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags);
1480 hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt);
1481 pollflag = 0;
1482 } else {
1483 pollflag = EPOLLIN | EPOLLRDNORM;
1484 }
1485 spin_unlock_irq(&dd->uctxt_lock);
1486
1487 return pollflag;
1488 }
1489
1490
1491
1492
1493
1494
1495 int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit)
1496 {
1497 struct hfi1_ctxtdata *uctxt;
1498 struct hfi1_devdata *dd = ppd->dd;
1499 u16 ctxt;
1500
1501 if (!dd->events)
1502 return -EINVAL;
1503
1504 for (ctxt = dd->first_dyn_alloc_ctxt; ctxt < dd->num_rcv_contexts;
1505 ctxt++) {
1506 uctxt = hfi1_rcd_get_by_index(dd, ctxt);
1507 if (uctxt) {
1508 unsigned long *evs;
1509 int i;
1510
1511
1512
1513
1514 evs = dd->events + uctxt_offset(uctxt);
1515 set_bit(evtbit, evs);
1516 for (i = 1; i < uctxt->subctxt_cnt; i++)
1517 set_bit(evtbit, evs + i);
1518 hfi1_rcd_put(uctxt);
1519 }
1520 }
1521
1522 return 0;
1523 }
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535 static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
1536 unsigned long arg)
1537 {
1538 struct hfi1_devdata *dd = uctxt->dd;
1539 unsigned int rcvctrl_op;
1540 int start_stop;
1541
1542 if (subctxt)
1543 return 0;
1544
1545 if (get_user(start_stop, (int __user *)arg))
1546 return -EFAULT;
1547
1548
1549 if (start_stop) {
1550
1551
1552
1553
1554
1555
1556
1557
1558 if (uctxt->rcvhdrtail_kvaddr)
1559 clear_rcvhdrtail(uctxt);
1560 rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB;
1561 } else {
1562 rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS;
1563 }
1564 hfi1_rcvctrl(dd, rcvctrl_op, uctxt);
1565
1566
1567 return 0;
1568 }
1569
1570
1571
1572
1573
1574
1575 static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
1576 unsigned long arg)
1577 {
1578 int i;
1579 struct hfi1_devdata *dd = uctxt->dd;
1580 unsigned long *evs;
1581 unsigned long events;
1582
1583 if (!dd->events)
1584 return 0;
1585
1586 if (get_user(events, (unsigned long __user *)arg))
1587 return -EFAULT;
1588
1589 evs = dd->events + uctxt_offset(uctxt) + subctxt;
1590
1591 for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) {
1592 if (!test_bit(i, &events))
1593 continue;
1594 clear_bit(i, evs);
1595 }
1596 return 0;
1597 }
1598
1599 static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned long arg)
1600 {
1601 int i;
1602 struct hfi1_pportdata *ppd = uctxt->ppd;
1603 struct hfi1_devdata *dd = uctxt->dd;
1604 u16 pkey;
1605
1606 if (!HFI1_CAP_IS_USET(PKEY_CHECK))
1607 return -EPERM;
1608
1609 if (get_user(pkey, (u16 __user *)arg))
1610 return -EFAULT;
1611
1612 if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY)
1613 return -EINVAL;
1614
1615 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++)
1616 if (pkey == ppd->pkeys[i])
1617 return hfi1_set_ctxt_pkey(dd, uctxt, pkey);
1618
1619 return -ENOENT;
1620 }
1621
1622
1623
1624
1625
1626 static int ctxt_reset(struct hfi1_ctxtdata *uctxt)
1627 {
1628 struct send_context *sc;
1629 struct hfi1_devdata *dd;
1630 int ret = 0;
1631
1632 if (!uctxt || !uctxt->dd || !uctxt->sc)
1633 return -EINVAL;
1634
1635
1636
1637
1638
1639
1640
1641 dd = uctxt->dd;
1642 sc = uctxt->sc;
1643
1644
1645
1646
1647
1648 wait_event_interruptible_timeout(
1649 sc->halt_wait, (sc->flags & SCF_HALTED),
1650 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
1651 if (!(sc->flags & SCF_HALTED))
1652 return -ENOLCK;
1653
1654
1655
1656
1657
1658 if (sc->flags & SCF_FROZEN) {
1659 wait_event_interruptible_timeout(
1660 dd->event_queue,
1661 !(READ_ONCE(dd->flags) & HFI1_FROZEN),
1662 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
1663 if (dd->flags & HFI1_FROZEN)
1664 return -ENOLCK;
1665
1666 if (dd->flags & HFI1_FORCED_FREEZE)
1667
1668
1669
1670
1671 return -ENODEV;
1672
1673 sc_disable(sc);
1674 ret = sc_enable(sc);
1675 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB, uctxt);
1676 } else {
1677 ret = sc_restart(sc);
1678 }
1679 if (!ret)
1680 sc_return_credits(sc);
1681
1682 return ret;
1683 }
1684
1685 static void user_remove(struct hfi1_devdata *dd)
1686 {
1687
1688 hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device);
1689 }
1690
1691 static int user_add(struct hfi1_devdata *dd)
1692 {
1693 char name[10];
1694 int ret;
1695
1696 snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit);
1697 ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops,
1698 &dd->user_cdev, &dd->user_device,
1699 true, &dd->kobj);
1700 if (ret)
1701 user_remove(dd);
1702
1703 return ret;
1704 }
1705
1706
1707
1708
1709 int hfi1_device_create(struct hfi1_devdata *dd)
1710 {
1711 return user_add(dd);
1712 }
1713
1714
1715
1716
1717
1718 void hfi1_device_remove(struct hfi1_devdata *dd)
1719 {
1720 user_remove(dd);
1721 }