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