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