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