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