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