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