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