blob: 0522bafb190b80fd460f1b202a719946df185c65 [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
Jubin John05d6ac12016-02-14 20:22:17 -08002 * Copyright(c) 2015, 2016 Intel Corporation.
Mike Marciniszyn77241052015-07-30 15:17:43 -04003 *
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 *
Mike Marciniszyn77241052015-07-30 15:17:43 -04009 * 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 *
Mike Marciniszyn77241052015-07-30 15:17:43 -040020 * 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 */
Mike Marciniszyn77241052015-07-30 15:17:43 -040047#include <linux/poll.h>
48#include <linux/cdev.h>
Mike Marciniszyn77241052015-07-30 15:17:43 -040049#include <linux/vmalloc.h>
Mike Marciniszyn77241052015-07-30 15:17:43 -040050#include <linux/io.h>
Mike Marciniszyn77241052015-07-30 15:17:43 -040051
Jason Gunthorpee6bd18f2016-04-10 19:13:13 -060052#include <rdma/ib.h>
53
Mike Marciniszyn77241052015-07-30 15:17:43 -040054#include "hfi.h"
55#include "pio.h"
56#include "device.h"
57#include "common.h"
58#include "trace.h"
59#include "user_sdma.h"
Mitko Haralanov701e4412015-10-30 18:58:43 -040060#include "user_exp_rcv.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040061#include "eprom.h"
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -080062#include "aspm.h"
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080063#include "mmu_rb.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040064
65#undef pr_fmt
66#define pr_fmt(fmt) DRIVER_NAME ": " fmt
67
68#define SEND_CTXT_HALT_TIMEOUT 1000 /* msecs */
69
70/*
71 * File operation functions
72 */
73static int hfi1_file_open(struct inode *, struct file *);
74static int hfi1_file_close(struct inode *, struct file *);
Mike Marciniszyn77241052015-07-30 15:17:43 -040075static ssize_t hfi1_write_iter(struct kiocb *, struct iov_iter *);
76static unsigned int hfi1_poll(struct file *, struct poll_table_struct *);
77static int hfi1_file_mmap(struct file *, struct vm_area_struct *);
78
79static u64 kvirt_to_phys(void *);
80static int assign_ctxt(struct file *, struct hfi1_user_info *);
81static int init_subctxts(struct hfi1_ctxtdata *, const struct hfi1_user_info *);
82static int user_init(struct file *);
83static int get_ctxt_info(struct file *, void __user *, __u32);
84static int get_base_info(struct file *, void __user *, __u32);
85static int setup_ctxt(struct file *);
86static int setup_subctxt(struct hfi1_ctxtdata *);
Dennis Dalessandro0eb62652016-05-19 05:25:50 -070087static int get_user_context(struct file *, struct hfi1_user_info *, int);
Mike Marciniszyn77241052015-07-30 15:17:43 -040088static int find_shared_ctxt(struct file *, const struct hfi1_user_info *);
89static int allocate_ctxt(struct file *, struct hfi1_devdata *,
90 struct hfi1_user_info *);
91static unsigned int poll_urgent(struct file *, struct poll_table_struct *);
92static unsigned int poll_next(struct file *, struct poll_table_struct *);
93static int user_event_ack(struct hfi1_ctxtdata *, int, unsigned long);
94static int set_ctxt_pkey(struct hfi1_ctxtdata *, unsigned, u16);
95static int manage_rcvq(struct hfi1_ctxtdata *, unsigned, int);
96static int vma_fault(struct vm_area_struct *, struct vm_fault *);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -070097static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
98 unsigned long arg);
Mike Marciniszyn77241052015-07-30 15:17:43 -040099
100static const struct file_operations hfi1_file_ops = {
101 .owner = THIS_MODULE,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400102 .write_iter = hfi1_write_iter,
103 .open = hfi1_file_open,
104 .release = hfi1_file_close,
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700105 .unlocked_ioctl = hfi1_file_ioctl,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400106 .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) | \
Geliang Tange260e402015-10-03 10:34:59 +0800159 HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr))))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400160
Mike Marciniszyn77241052015-07-30 15:17:43 -0400161#define dbg(fmt, ...) \
162 pr_info(fmt, ##__VA_ARGS__)
163
Mike Marciniszyn77241052015-07-30 15:17:43 -0400164static inline int is_valid_mmap(u64 token)
165{
166 return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC);
167}
168
169static int hfi1_file_open(struct inode *inode, struct file *fp)
170{
Ira Weinyea3a0ee2016-07-28 12:27:35 -0400171 struct hfi1_filedata *fd;
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700172 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
Mike Marciniszyn77241052015-07-30 15:17:43 -0400179 /* The real work is performed later in assign_ctxt() */
Ira Weinyea3a0ee2016-07-28 12:27:35 -0400180
181 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
182
183 if (fd) /* no cpu affinity by default */
184 fd->rec_cpu_num = -1;
185
186 fp->private_data = fd;
187
188 return fd ? 0 : -ENOMEM;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400189}
190
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700191static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
192 unsigned long arg)
193{
194 struct hfi1_filedata *fd = fp->private_data;
195 struct hfi1_ctxtdata *uctxt = fd->uctxt;
196 struct hfi1_user_info uinfo;
197 struct hfi1_tid_info tinfo;
198 int ret = 0;
199 unsigned long addr;
200 int uval = 0;
201 unsigned long ul_uval = 0;
202 u16 uval16 = 0;
203
Dennis Dalessandro8a1882e2016-05-19 05:26:37 -0700204 hfi1_cdbg(IOCTL, "IOCTL recv: 0x%x", cmd);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700205 if (cmd != HFI1_IOCTL_ASSIGN_CTXT &&
206 cmd != HFI1_IOCTL_GET_VERS &&
207 !uctxt)
208 return -EINVAL;
209
210 switch (cmd) {
211 case HFI1_IOCTL_ASSIGN_CTXT:
Ira Weinyca2f30a2016-06-09 07:51:33 -0700212 if (uctxt)
213 return -EINVAL;
214
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700215 if (copy_from_user(&uinfo,
216 (struct hfi1_user_info __user *)arg,
217 sizeof(uinfo)))
218 return -EFAULT;
219
220 ret = assign_ctxt(fp, &uinfo);
221 if (ret < 0)
222 return ret;
223 setup_ctxt(fp);
224 if (ret)
225 return ret;
226 ret = user_init(fp);
227 break;
228 case HFI1_IOCTL_CTXT_INFO:
229 ret = get_ctxt_info(fp, (void __user *)(unsigned long)arg,
230 sizeof(struct hfi1_ctxt_info));
231 break;
232 case HFI1_IOCTL_USER_INFO:
233 ret = get_base_info(fp, (void __user *)(unsigned long)arg,
234 sizeof(struct hfi1_base_info));
235 break;
236 case HFI1_IOCTL_CREDIT_UPD:
237 if (uctxt && uctxt->sc)
238 sc_return_credits(uctxt->sc);
239 break;
240
241 case HFI1_IOCTL_TID_UPDATE:
242 if (copy_from_user(&tinfo,
243 (struct hfi11_tid_info __user *)arg,
244 sizeof(tinfo)))
245 return -EFAULT;
246
247 ret = hfi1_user_exp_rcv_setup(fp, &tinfo);
248 if (!ret) {
249 /*
250 * Copy the number of tidlist entries we used
251 * and the length of the buffer we registered.
252 * These fields are adjacent in the structure so
253 * we can copy them at the same time.
254 */
255 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
256 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
257 sizeof(tinfo.tidcnt) +
258 sizeof(tinfo.length)))
259 ret = -EFAULT;
260 }
261 break;
262
263 case HFI1_IOCTL_TID_FREE:
264 if (copy_from_user(&tinfo,
265 (struct hfi11_tid_info __user *)arg,
266 sizeof(tinfo)))
267 return -EFAULT;
268
269 ret = hfi1_user_exp_rcv_clear(fp, &tinfo);
270 if (ret)
271 break;
272 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
273 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
274 sizeof(tinfo.tidcnt)))
275 ret = -EFAULT;
276 break;
277
278 case HFI1_IOCTL_TID_INVAL_READ:
279 if (copy_from_user(&tinfo,
280 (struct hfi11_tid_info __user *)arg,
281 sizeof(tinfo)))
282 return -EFAULT;
283
284 ret = hfi1_user_exp_rcv_invalid(fp, &tinfo);
285 if (ret)
286 break;
287 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
288 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
289 sizeof(tinfo.tidcnt)))
290 ret = -EFAULT;
291 break;
292
293 case HFI1_IOCTL_RECV_CTRL:
294 ret = get_user(uval, (int __user *)arg);
295 if (ret != 0)
296 return -EFAULT;
297 ret = manage_rcvq(uctxt, fd->subctxt, uval);
298 break;
299
300 case HFI1_IOCTL_POLL_TYPE:
301 ret = get_user(uval, (int __user *)arg);
302 if (ret != 0)
303 return -EFAULT;
304 uctxt->poll_type = (typeof(uctxt->poll_type))uval;
305 break;
306
307 case HFI1_IOCTL_ACK_EVENT:
308 ret = get_user(ul_uval, (unsigned long __user *)arg);
309 if (ret != 0)
310 return -EFAULT;
311 ret = user_event_ack(uctxt, fd->subctxt, ul_uval);
312 break;
313
314 case HFI1_IOCTL_SET_PKEY:
315 ret = get_user(uval16, (u16 __user *)arg);
316 if (ret != 0)
317 return -EFAULT;
318 if (HFI1_CAP_IS_USET(PKEY_CHECK))
319 ret = set_ctxt_pkey(uctxt, fd->subctxt, uval16);
320 else
321 return -EPERM;
322 break;
323
324 case HFI1_IOCTL_CTXT_RESET: {
325 struct send_context *sc;
326 struct hfi1_devdata *dd;
327
328 if (!uctxt || !uctxt->dd || !uctxt->sc)
329 return -EINVAL;
330
331 /*
332 * There is no protection here. User level has to
333 * guarantee that no one will be writing to the send
334 * context while it is being re-initialized.
335 * If user level breaks that guarantee, it will break
336 * it's own context and no one else's.
337 */
338 dd = uctxt->dd;
339 sc = uctxt->sc;
340 /*
341 * Wait until the interrupt handler has marked the
342 * context as halted or frozen. Report error if we time
343 * out.
344 */
345 wait_event_interruptible_timeout(
346 sc->halt_wait, (sc->flags & SCF_HALTED),
347 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
348 if (!(sc->flags & SCF_HALTED))
349 return -ENOLCK;
350
351 /*
352 * If the send context was halted due to a Freeze,
353 * wait until the device has been "unfrozen" before
354 * resetting the context.
355 */
356 if (sc->flags & SCF_FROZEN) {
357 wait_event_interruptible_timeout(
358 dd->event_queue,
359 !(ACCESS_ONCE(dd->flags) & HFI1_FROZEN),
360 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
361 if (dd->flags & HFI1_FROZEN)
362 return -ENOLCK;
363
364 if (dd->flags & HFI1_FORCED_FREEZE)
365 /*
366 * Don't allow context reset if we are into
367 * forced freeze
368 */
369 return -ENODEV;
370
371 sc_disable(sc);
372 ret = sc_enable(sc);
373 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB,
374 uctxt->ctxt);
375 } else {
376 ret = sc_restart(sc);
377 }
378 if (!ret)
379 sc_return_credits(sc);
380 break;
381 }
382
383 case HFI1_IOCTL_GET_VERS:
384 uval = HFI1_USER_SWVERSION;
385 if (put_user(uval, (int __user *)arg))
386 return -EFAULT;
387 break;
388
389 default:
390 return -EINVAL;
391 }
392
393 return ret;
394}
395
Mike Marciniszyn77241052015-07-30 15:17:43 -0400396static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from)
397{
Ira Weiny9e10af42015-10-30 18:58:40 -0400398 struct hfi1_filedata *fd = kiocb->ki_filp->private_data;
399 struct hfi1_user_sdma_pkt_q *pq = fd->pq;
400 struct hfi1_user_sdma_comp_q *cq = fd->cq;
Ira Weiny0904f322016-07-01 16:00:55 -0700401 int done = 0, reqs = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400402 unsigned long dim = from->nr_segs;
403
Ira Weiny0904f322016-07-01 16:00:55 -0700404 if (!cq || !pq)
405 return -EIO;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400406
Ira Weiny0904f322016-07-01 16:00:55 -0700407 if (!iter_is_iovec(from) || !dim)
408 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400409
410 hfi1_cdbg(SDMA, "SDMA request from %u:%u (%lu)",
Ira Weiny9e10af42015-10-30 18:58:40 -0400411 fd->uctxt->ctxt, fd->subctxt, dim);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400412
Ira Weiny0904f322016-07-01 16:00:55 -0700413 if (atomic_read(&pq->n_reqs) == pq->n_max_reqs)
414 return -ENOSPC;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400415
416 while (dim) {
Ira Weiny0904f322016-07-01 16:00:55 -0700417 int ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400418 unsigned long count = 0;
419
420 ret = hfi1_user_sdma_process_request(
421 kiocb->ki_filp, (struct iovec *)(from->iov + done),
422 dim, &count);
Ira Weiny0904f322016-07-01 16:00:55 -0700423 if (ret) {
424 reqs = ret;
425 break;
426 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400427 dim -= count;
428 done += count;
429 reqs++;
430 }
Ira Weiny0904f322016-07-01 16:00:55 -0700431
432 return reqs;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400433}
434
435static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma)
436{
Ira Weiny9e10af42015-10-30 18:58:40 -0400437 struct hfi1_filedata *fd = fp->private_data;
438 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400439 struct hfi1_devdata *dd;
440 unsigned long flags, pfn;
441 u64 token = vma->vm_pgoff << PAGE_SHIFT,
442 memaddr = 0;
443 u8 subctxt, mapio = 0, vmf = 0, type;
444 ssize_t memlen = 0;
445 int ret = 0;
446 u16 ctxt;
447
Mike Marciniszyn77241052015-07-30 15:17:43 -0400448 if (!is_valid_mmap(token) || !uctxt ||
449 !(vma->vm_flags & VM_SHARED)) {
450 ret = -EINVAL;
451 goto done;
452 }
453 dd = uctxt->dd;
454 ctxt = HFI1_MMAP_TOKEN_GET(CTXT, token);
455 subctxt = HFI1_MMAP_TOKEN_GET(SUBCTXT, token);
456 type = HFI1_MMAP_TOKEN_GET(TYPE, token);
Ira Weiny9e10af42015-10-30 18:58:40 -0400457 if (ctxt != uctxt->ctxt || subctxt != fd->subctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400458 ret = -EINVAL;
459 goto done;
460 }
461
462 flags = vma->vm_flags;
463
464 switch (type) {
465 case PIO_BUFS:
466 case PIO_BUFS_SOP:
467 memaddr = ((dd->physaddr + TXE_PIO_SEND) +
468 /* chip pio base */
Amitoj Kaur Chawlad32cf442015-10-16 22:09:08 +0530469 (uctxt->sc->hw_context * BIT(16))) +
Mike Marciniszyn77241052015-07-30 15:17:43 -0400470 /* 64K PIO space / ctxt */
471 (type == PIO_BUFS_SOP ?
472 (TXE_PIO_SIZE / 2) : 0); /* sop? */
473 /*
474 * Map only the amount allocated to the context, not the
475 * entire available context's PIO space.
476 */
Amitoj Kaur Chawla437b29d2016-03-04 22:45:00 +0530477 memlen = PAGE_ALIGN(uctxt->sc->credits * PIO_BLOCK_SIZE);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400478 flags &= ~VM_MAYREAD;
479 flags |= VM_DONTCOPY | VM_DONTEXPAND;
480 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
481 mapio = 1;
482 break;
483 case PIO_CRED:
484 if (flags & VM_WRITE) {
485 ret = -EPERM;
486 goto done;
487 }
488 /*
489 * The credit return location for this context could be on the
490 * second or third page allocated for credit returns (if number
491 * of enabled contexts > 64 and 128 respectively).
492 */
493 memaddr = dd->cr_base[uctxt->numa_id].pa +
494 (((u64)uctxt->sc->hw_free -
495 (u64)dd->cr_base[uctxt->numa_id].va) & PAGE_MASK);
496 memlen = PAGE_SIZE;
497 flags &= ~VM_MAYWRITE;
498 flags |= VM_DONTCOPY | VM_DONTEXPAND;
499 /*
500 * The driver has already allocated memory for credit
501 * returns and programmed it into the chip. Has that
502 * memory been flagged as non-cached?
503 */
504 /* vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); */
505 mapio = 1;
506 break;
507 case RCV_HDRQ:
508 memaddr = uctxt->rcvhdrq_phys;
509 memlen = uctxt->rcvhdrq_size;
510 break;
511 case RCV_EGRBUF: {
512 unsigned long addr;
513 int i;
514 /*
515 * The RcvEgr buffer need to be handled differently
516 * as multiple non-contiguous pages need to be mapped
517 * into the user process.
518 */
519 memlen = uctxt->egrbufs.size;
520 if ((vma->vm_end - vma->vm_start) != memlen) {
521 dd_dev_err(dd, "Eager buffer map size invalid (%lu != %lu)\n",
522 (vma->vm_end - vma->vm_start), memlen);
523 ret = -EINVAL;
524 goto done;
525 }
526 if (vma->vm_flags & VM_WRITE) {
527 ret = -EPERM;
528 goto done;
529 }
530 vma->vm_flags &= ~VM_MAYWRITE;
531 addr = vma->vm_start;
532 for (i = 0 ; i < uctxt->egrbufs.numbufs; i++) {
533 ret = remap_pfn_range(
534 vma, addr,
535 uctxt->egrbufs.buffers[i].phys >> PAGE_SHIFT,
536 uctxt->egrbufs.buffers[i].len,
537 vma->vm_page_prot);
538 if (ret < 0)
539 goto done;
540 addr += uctxt->egrbufs.buffers[i].len;
541 }
542 ret = 0;
543 goto done;
544 }
545 case UREGS:
546 /*
547 * Map only the page that contains this context's user
548 * registers.
549 */
550 memaddr = (unsigned long)
551 (dd->physaddr + RXE_PER_CONTEXT_USER)
552 + (uctxt->ctxt * RXE_PER_CONTEXT_SIZE);
553 /*
554 * TidFlow table is on the same page as the rest of the
555 * user registers.
556 */
557 memlen = PAGE_SIZE;
558 flags |= VM_DONTCOPY | VM_DONTEXPAND;
559 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
560 mapio = 1;
561 break;
562 case EVENTS:
563 /*
564 * Use the page where this context's flags are. User level
565 * knows where it's own bitmap is within the page.
566 */
Mitko Haralanov3c6c0652015-10-26 10:28:39 -0400567 memaddr = (unsigned long)(dd->events +
568 ((uctxt->ctxt - dd->first_user_ctxt) *
569 HFI1_MAX_SHARED_CTXTS)) & PAGE_MASK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400570 memlen = PAGE_SIZE;
571 /*
572 * v3.7 removes VM_RESERVED but the effect is kept by
573 * using VM_IO.
574 */
575 flags |= VM_IO | VM_DONTEXPAND;
576 vmf = 1;
577 break;
578 case STATUS:
579 memaddr = kvirt_to_phys((void *)dd->status);
580 memlen = PAGE_SIZE;
581 flags |= VM_IO | VM_DONTEXPAND;
582 break;
583 case RTAIL:
584 if (!HFI1_CAP_IS_USET(DMA_RTAIL)) {
585 /*
586 * If the memory allocation failed, the context alloc
587 * also would have failed, so we would never get here
588 */
589 ret = -EINVAL;
590 goto done;
591 }
592 if (flags & VM_WRITE) {
593 ret = -EPERM;
594 goto done;
595 }
596 memaddr = uctxt->rcvhdrqtailaddr_phys;
597 memlen = PAGE_SIZE;
598 flags &= ~VM_MAYWRITE;
599 break;
600 case SUBCTXT_UREGS:
601 memaddr = (u64)uctxt->subctxt_uregbase;
602 memlen = PAGE_SIZE;
603 flags |= VM_IO | VM_DONTEXPAND;
604 vmf = 1;
605 break;
606 case SUBCTXT_RCV_HDRQ:
607 memaddr = (u64)uctxt->subctxt_rcvhdr_base;
608 memlen = uctxt->rcvhdrq_size * uctxt->subctxt_cnt;
609 flags |= VM_IO | VM_DONTEXPAND;
610 vmf = 1;
611 break;
612 case SUBCTXT_EGRBUF:
613 memaddr = (u64)uctxt->subctxt_rcvegrbuf;
614 memlen = uctxt->egrbufs.size * uctxt->subctxt_cnt;
615 flags |= VM_IO | VM_DONTEXPAND;
616 flags &= ~VM_MAYWRITE;
617 vmf = 1;
618 break;
619 case SDMA_COMP: {
Ira Weiny9e10af42015-10-30 18:58:40 -0400620 struct hfi1_user_sdma_comp_q *cq = fd->cq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400621
Ira Weiny9e10af42015-10-30 18:58:40 -0400622 if (!cq) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400623 ret = -EFAULT;
624 goto done;
625 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400626 memaddr = (u64)cq->comps;
Amitoj Kaur Chawla437b29d2016-03-04 22:45:00 +0530627 memlen = PAGE_ALIGN(sizeof(*cq->comps) * cq->nentries);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400628 flags |= VM_IO | VM_DONTEXPAND;
629 vmf = 1;
630 break;
631 }
632 default:
633 ret = -EINVAL;
634 break;
635 }
636
637 if ((vma->vm_end - vma->vm_start) != memlen) {
638 hfi1_cdbg(PROC, "%u:%u Memory size mismatch %lu:%lu",
Ira Weiny9e10af42015-10-30 18:58:40 -0400639 uctxt->ctxt, fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400640 (vma->vm_end - vma->vm_start), memlen);
641 ret = -EINVAL;
642 goto done;
643 }
644
645 vma->vm_flags = flags;
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500646 hfi1_cdbg(PROC,
647 "%u:%u type:%u io/vf:%d/%d, addr:0x%llx, len:%lu(%lu), flags:0x%lx\n",
648 ctxt, subctxt, type, mapio, vmf, memaddr, memlen,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400649 vma->vm_end - vma->vm_start, vma->vm_flags);
650 pfn = (unsigned long)(memaddr >> PAGE_SHIFT);
651 if (vmf) {
652 vma->vm_pgoff = pfn;
653 vma->vm_ops = &vm_ops;
654 ret = 0;
655 } else if (mapio) {
656 ret = io_remap_pfn_range(vma, vma->vm_start, pfn, memlen,
657 vma->vm_page_prot);
658 } else {
659 ret = remap_pfn_range(vma, vma->vm_start, pfn, memlen,
660 vma->vm_page_prot);
661 }
662done:
663 return ret;
664}
665
666/*
667 * Local (non-chip) user memory is not mapped right away but as it is
668 * accessed by the user-level code.
669 */
670static int vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
671{
672 struct page *page;
673
674 page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
675 if (!page)
676 return VM_FAULT_SIGBUS;
677
678 get_page(page);
679 vmf->page = page;
680
681 return 0;
682}
683
684static unsigned int hfi1_poll(struct file *fp, struct poll_table_struct *pt)
685{
686 struct hfi1_ctxtdata *uctxt;
687 unsigned pollflag;
688
Ira Weiny9e10af42015-10-30 18:58:40 -0400689 uctxt = ((struct hfi1_filedata *)fp->private_data)->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400690 if (!uctxt)
691 pollflag = POLLERR;
692 else if (uctxt->poll_type == HFI1_POLL_TYPE_URGENT)
693 pollflag = poll_urgent(fp, pt);
694 else if (uctxt->poll_type == HFI1_POLL_TYPE_ANYRCV)
695 pollflag = poll_next(fp, pt);
696 else /* invalid */
697 pollflag = POLLERR;
698
699 return pollflag;
700}
701
702static int hfi1_file_close(struct inode *inode, struct file *fp)
703{
704 struct hfi1_filedata *fdata = fp->private_data;
705 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700706 struct hfi1_devdata *dd = container_of(inode->i_cdev,
707 struct hfi1_devdata,
708 user_cdev);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400709 unsigned long flags, *ev;
710
711 fp->private_data = NULL;
712
713 if (!uctxt)
714 goto done;
715
716 hfi1_cdbg(PROC, "freeing ctxt %u:%u", uctxt->ctxt, fdata->subctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400717 mutex_lock(&hfi1_mutex);
718
719 flush_wc();
720 /* drain user sdma queue */
Mitko Haralanov483119a2015-12-08 17:10:10 -0500721 hfi1_user_sdma_free_queues(fdata);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400722
Mitko Haralanov957558c2016-02-03 14:33:40 -0800723 /* release the cpu */
Sebastian Sanchezb094a362016-07-25 07:54:57 -0700724 hfi1_put_proc_affinity(fdata->rec_cpu_num);
Mitko Haralanov957558c2016-02-03 14:33:40 -0800725
Mike Marciniszyn77241052015-07-30 15:17:43 -0400726 /*
727 * Clear any left over, unhandled events so the next process that
728 * gets this context doesn't get confused.
729 */
730 ev = dd->events + ((uctxt->ctxt - dd->first_user_ctxt) *
731 HFI1_MAX_SHARED_CTXTS) + fdata->subctxt;
732 *ev = 0;
733
734 if (--uctxt->cnt) {
735 uctxt->active_slaves &= ~(1 << fdata->subctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400736 mutex_unlock(&hfi1_mutex);
737 goto done;
738 }
739
740 spin_lock_irqsave(&dd->uctxt_lock, flags);
741 /*
742 * Disable receive context and interrupt available, reset all
743 * RcvCtxtCtrl bits to default values.
744 */
745 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
746 HFI1_RCVCTRL_TIDFLOW_DIS |
747 HFI1_RCVCTRL_INTRAVAIL_DIS |
Mitko Haralanov566c1572016-02-03 14:32:49 -0800748 HFI1_RCVCTRL_TAILUPD_DIS |
Mike Marciniszyn77241052015-07-30 15:17:43 -0400749 HFI1_RCVCTRL_ONE_PKT_EGR_DIS |
750 HFI1_RCVCTRL_NO_RHQ_DROP_DIS |
751 HFI1_RCVCTRL_NO_EGR_DROP_DIS, uctxt->ctxt);
752 /* Clear the context's J_KEY */
753 hfi1_clear_ctxt_jkey(dd, uctxt->ctxt);
754 /*
755 * Reset context integrity checks to default.
756 * (writes to CSRs probably belong in chip.c)
757 */
758 write_kctxt_csr(dd, uctxt->sc->hw_context, SEND_CTXT_CHECK_ENABLE,
759 hfi1_pkt_default_send_ctxt_mask(dd, uctxt->sc->type));
760 sc_disable(uctxt->sc);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400761 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
762
763 dd->rcd[uctxt->ctxt] = NULL;
Mitko Haralanov94158442016-04-20 06:05:36 -0700764
765 hfi1_user_exp_rcv_free(fdata);
766 hfi1_clear_ctxt_pkey(dd, uctxt->ctxt);
767
Mike Marciniszyn77241052015-07-30 15:17:43 -0400768 uctxt->rcvwait_to = 0;
769 uctxt->piowait_to = 0;
770 uctxt->rcvnowait = 0;
771 uctxt->pionowait = 0;
772 uctxt->event_flags = 0;
773
Mike Marciniszyn77241052015-07-30 15:17:43 -0400774 hfi1_stats.sps_ctxts--;
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -0800775 if (++dd->freectxts == dd->num_user_contexts)
776 aspm_enable_all(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400777 mutex_unlock(&hfi1_mutex);
778 hfi1_free_ctxtdata(dd, uctxt);
779done:
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700780 kobject_put(&dd->kobj);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400781 kfree(fdata);
782 return 0;
783}
784
785/*
786 * Convert kernel *virtual* addresses to physical addresses.
787 * This is used to vmalloc'ed addresses.
788 */
789static u64 kvirt_to_phys(void *addr)
790{
791 struct page *page;
792 u64 paddr = 0;
793
794 page = vmalloc_to_page(addr);
795 if (page)
796 paddr = page_to_pfn(page) << PAGE_SHIFT;
797
798 return paddr;
799}
800
801static int assign_ctxt(struct file *fp, struct hfi1_user_info *uinfo)
802{
803 int i_minor, ret = 0;
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700804 unsigned int swmajor, swminor;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400805
806 swmajor = uinfo->userversion >> 16;
807 if (swmajor != HFI1_USER_SWMAJOR) {
808 ret = -ENODEV;
809 goto done;
810 }
811
812 swminor = uinfo->userversion & 0xffff;
813
Mike Marciniszyn77241052015-07-30 15:17:43 -0400814 mutex_lock(&hfi1_mutex);
815 /* First, lets check if we need to setup a shared context? */
Mitko Haralanov957558c2016-02-03 14:33:40 -0800816 if (uinfo->subctxt_cnt) {
817 struct hfi1_filedata *fd = fp->private_data;
818
Mike Marciniszyn77241052015-07-30 15:17:43 -0400819 ret = find_shared_ctxt(fp, uinfo);
Mitko Haralanov957558c2016-02-03 14:33:40 -0800820 if (ret < 0)
821 goto done_unlock;
Sebastian Sanchezb094a362016-07-25 07:54:57 -0700822 if (ret) {
823 fd->rec_cpu_num =
824 hfi1_get_proc_affinity(fd->uctxt->numa_id);
825 }
Mitko Haralanov957558c2016-02-03 14:33:40 -0800826 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400827
828 /*
829 * We execute the following block if we couldn't find a
830 * shared context or if context sharing is not required.
831 */
832 if (!ret) {
833 i_minor = iminor(file_inode(fp)) - HFI1_USER_MINOR_BASE;
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700834 ret = get_user_context(fp, uinfo, i_minor);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400835 }
Mitko Haralanov957558c2016-02-03 14:33:40 -0800836done_unlock:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400837 mutex_unlock(&hfi1_mutex);
838done:
839 return ret;
840}
841
842static int get_user_context(struct file *fp, struct hfi1_user_info *uinfo,
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700843 int devno)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400844{
845 struct hfi1_devdata *dd = NULL;
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700846 int devmax, npresent, nup;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400847
848 devmax = hfi1_count_units(&npresent, &nup);
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700849 if (!npresent)
850 return -ENXIO;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400851
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700852 if (!nup)
853 return -ENETDOWN;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400854
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700855 dd = hfi1_lookup(devno);
856 if (!dd)
857 return -ENODEV;
858 else if (!dd->freectxts)
859 return -EBUSY;
860
861 return allocate_ctxt(fp, dd, uinfo);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400862}
863
864static int find_shared_ctxt(struct file *fp,
865 const struct hfi1_user_info *uinfo)
866{
867 int devmax, ndev, i;
868 int ret = 0;
Ira Weiny9e10af42015-10-30 18:58:40 -0400869 struct hfi1_filedata *fd = fp->private_data;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400870
871 devmax = hfi1_count_units(NULL, NULL);
872
873 for (ndev = 0; ndev < devmax; ndev++) {
874 struct hfi1_devdata *dd = hfi1_lookup(ndev);
875
Mike Marciniszyn77241052015-07-30 15:17:43 -0400876 if (!(dd && (dd->flags & HFI1_PRESENT) && dd->kregbase))
877 continue;
878 for (i = dd->first_user_ctxt; i < dd->num_rcv_contexts; i++) {
879 struct hfi1_ctxtdata *uctxt = dd->rcd[i];
880
881 /* Skip ctxts which are not yet open */
882 if (!uctxt || !uctxt->cnt)
883 continue;
884 /* Skip ctxt if it doesn't match the requested one */
885 if (memcmp(uctxt->uuid, uinfo->uuid,
886 sizeof(uctxt->uuid)) ||
Jareer Abdel-Qader07839042015-10-26 10:28:33 -0400887 uctxt->jkey != generate_jkey(current_uid()) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -0400888 uctxt->subctxt_id != uinfo->subctxt_id ||
889 uctxt->subctxt_cnt != uinfo->subctxt_cnt)
890 continue;
891
892 /* Verify the sharing process matches the master */
893 if (uctxt->userversion != uinfo->userversion ||
894 uctxt->cnt >= uctxt->subctxt_cnt) {
895 ret = -EINVAL;
896 goto done;
897 }
Ira Weiny9e10af42015-10-30 18:58:40 -0400898 fd->uctxt = uctxt;
899 fd->subctxt = uctxt->cnt++;
Ira Weiny9e10af42015-10-30 18:58:40 -0400900 uctxt->active_slaves |= 1 << fd->subctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400901 ret = 1;
902 goto done;
903 }
904 }
905
906done:
907 return ret;
908}
909
910static int allocate_ctxt(struct file *fp, struct hfi1_devdata *dd,
911 struct hfi1_user_info *uinfo)
912{
Ira Weiny9e10af42015-10-30 18:58:40 -0400913 struct hfi1_filedata *fd = fp->private_data;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400914 struct hfi1_ctxtdata *uctxt;
915 unsigned ctxt;
Mitko Haralanov957558c2016-02-03 14:33:40 -0800916 int ret, numa;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400917
918 if (dd->flags & HFI1_FROZEN) {
919 /*
920 * Pick an error that is unique from all other errors
921 * that are returned so the user process knows that
922 * it tried to allocate while the SPC was frozen. It
923 * it should be able to retry with success in a short
924 * while.
925 */
926 return -EIO;
927 }
928
929 for (ctxt = dd->first_user_ctxt; ctxt < dd->num_rcv_contexts; ctxt++)
930 if (!dd->rcd[ctxt])
931 break;
932
933 if (ctxt == dd->num_rcv_contexts)
934 return -EBUSY;
935
Sebastian Sanchezb094a362016-07-25 07:54:57 -0700936 /*
937 * If we don't have a NUMA node requested, preference is towards
938 * device NUMA node.
939 */
940 fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node);
Mitko Haralanov957558c2016-02-03 14:33:40 -0800941 if (fd->rec_cpu_num != -1)
942 numa = cpu_to_node(fd->rec_cpu_num);
943 else
944 numa = numa_node_id();
945 uctxt = hfi1_create_ctxtdata(dd->pport, ctxt, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400946 if (!uctxt) {
947 dd_dev_err(dd,
948 "Unable to allocate ctxtdata memory, failing open\n");
949 return -ENOMEM;
950 }
Mitko Haralanov957558c2016-02-03 14:33:40 -0800951 hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)",
952 uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num,
953 uctxt->numa_id);
954
Mike Marciniszyn77241052015-07-30 15:17:43 -0400955 /*
956 * Allocate and enable a PIO send context.
957 */
958 uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize,
Mitko Haralanovcc572362016-02-03 14:33:49 -0800959 uctxt->dd->node);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400960 if (!uctxt->sc)
961 return -ENOMEM;
962
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500963 hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index,
964 uctxt->sc->hw_context);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400965 ret = sc_enable(uctxt->sc);
966 if (ret)
967 return ret;
968 /*
969 * Setup shared context resources if the user-level has requested
970 * shared contexts and this is the 'master' process.
971 * This has to be done here so the rest of the sub-contexts find the
972 * proper master.
973 */
Ira Weiny9e10af42015-10-30 18:58:40 -0400974 if (uinfo->subctxt_cnt && !fd->subctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400975 ret = init_subctxts(uctxt, uinfo);
976 /*
977 * On error, we don't need to disable and de-allocate the
978 * send context because it will be done during file close
979 */
980 if (ret)
981 return ret;
982 }
983 uctxt->userversion = uinfo->userversion;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400984 uctxt->flags = HFI1_CAP_UGET(MASK);
985 init_waitqueue_head(&uctxt->wait);
986 strlcpy(uctxt->comm, current->comm, sizeof(uctxt->comm));
987 memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid));
988 uctxt->jkey = generate_jkey(current_uid());
989 INIT_LIST_HEAD(&uctxt->sdma_queues);
990 spin_lock_init(&uctxt->sdma_qlock);
991 hfi1_stats.sps_ctxts++;
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -0800992 /*
993 * Disable ASPM when there are open user/PSM contexts to avoid
994 * issues with ASPM L1 exit latency
995 */
996 if (dd->freectxts-- == dd->num_user_contexts)
997 aspm_disable_all(dd);
Ira Weiny9e10af42015-10-30 18:58:40 -0400998 fd->uctxt = uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400999
1000 return 0;
1001}
1002
1003static int init_subctxts(struct hfi1_ctxtdata *uctxt,
1004 const struct hfi1_user_info *uinfo)
1005{
Mike Marciniszyn77241052015-07-30 15:17:43 -04001006 unsigned num_subctxts;
1007
1008 num_subctxts = uinfo->subctxt_cnt;
Mitko Haralanovacac10f2016-02-05 11:57:50 -05001009 if (num_subctxts > HFI1_MAX_SHARED_CTXTS)
1010 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001011
1012 uctxt->subctxt_cnt = uinfo->subctxt_cnt;
1013 uctxt->subctxt_id = uinfo->subctxt_id;
1014 uctxt->active_slaves = 1;
1015 uctxt->redirect_seq_cnt = 1;
1016 set_bit(HFI1_CTXT_MASTER_UNINIT, &uctxt->event_flags);
Mitko Haralanovacac10f2016-02-05 11:57:50 -05001017
1018 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001019}
1020
1021static int setup_subctxt(struct hfi1_ctxtdata *uctxt)
1022{
1023 int ret = 0;
1024 unsigned num_subctxts = uctxt->subctxt_cnt;
1025
1026 uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE);
1027 if (!uctxt->subctxt_uregbase) {
1028 ret = -ENOMEM;
1029 goto bail;
1030 }
1031 /* We can take the size of the RcvHdr Queue from the master */
1032 uctxt->subctxt_rcvhdr_base = vmalloc_user(uctxt->rcvhdrq_size *
1033 num_subctxts);
1034 if (!uctxt->subctxt_rcvhdr_base) {
1035 ret = -ENOMEM;
1036 goto bail_ureg;
1037 }
1038
1039 uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size *
1040 num_subctxts);
1041 if (!uctxt->subctxt_rcvegrbuf) {
1042 ret = -ENOMEM;
1043 goto bail_rhdr;
1044 }
1045 goto bail;
1046bail_rhdr:
1047 vfree(uctxt->subctxt_rcvhdr_base);
1048bail_ureg:
1049 vfree(uctxt->subctxt_uregbase);
1050 uctxt->subctxt_uregbase = NULL;
1051bail:
1052 return ret;
1053}
1054
1055static int user_init(struct file *fp)
1056{
Mike Marciniszyn77241052015-07-30 15:17:43 -04001057 unsigned int rcvctrl_ops = 0;
Ira Weiny9e10af42015-10-30 18:58:40 -04001058 struct hfi1_filedata *fd = fp->private_data;
1059 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001060
1061 /* make sure that the context has already been setup */
Mitko Haralanov94158442016-04-20 06:05:36 -07001062 if (!test_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags))
1063 return -EFAULT;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001064
1065 /* initialize poll variables... */
1066 uctxt->urgent = 0;
1067 uctxt->urgent_poll = 0;
1068
1069 /*
1070 * Now enable the ctxt for receive.
1071 * For chips that are set to DMA the tail register to memory
1072 * when they change (and when the update bit transitions from
1073 * 0 to 1. So for those chips, we turn it off and then back on.
1074 * This will (very briefly) affect any other open ctxts, but the
1075 * duration is very short, and therefore isn't an issue. We
1076 * explicitly set the in-memory tail copy to 0 beforehand, so we
1077 * don't have to wait to be sure the DMA update has happened
1078 * (chip resets head/tail to 0 on transition to enable).
1079 */
1080 if (uctxt->rcvhdrtail_kvaddr)
1081 clear_rcvhdrtail(uctxt);
1082
1083 /* Setup J_KEY before enabling the context */
1084 hfi1_set_ctxt_jkey(uctxt->dd, uctxt->ctxt, uctxt->jkey);
1085
1086 rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB;
1087 if (HFI1_CAP_KGET_MASK(uctxt->flags, HDRSUPP))
1088 rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB;
1089 /*
1090 * Ignore the bit in the flags for now until proper
1091 * support for multiple packet per rcv array entry is
1092 * added.
1093 */
1094 if (!HFI1_CAP_KGET_MASK(uctxt->flags, MULTI_PKT_EGR))
1095 rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
1096 if (HFI1_CAP_KGET_MASK(uctxt->flags, NODROP_EGR_FULL))
1097 rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
1098 if (HFI1_CAP_KGET_MASK(uctxt->flags, NODROP_RHQ_FULL))
1099 rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
Mitko Haralanov566c1572016-02-03 14:32:49 -08001100 /*
1101 * The RcvCtxtCtrl.TailUpd bit has to be explicitly written.
1102 * We can't rely on the correct value to be set from prior
1103 * uses of the chip or ctxt. Therefore, add the rcvctrl op
1104 * for both cases.
1105 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001106 if (HFI1_CAP_KGET_MASK(uctxt->flags, DMA_RTAIL))
1107 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB;
Mitko Haralanov566c1572016-02-03 14:32:49 -08001108 else
1109 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001110 hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt->ctxt);
1111
1112 /* Notify any waiting slaves */
1113 if (uctxt->subctxt_cnt) {
1114 clear_bit(HFI1_CTXT_MASTER_UNINIT, &uctxt->event_flags);
1115 wake_up(&uctxt->wait);
1116 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001117
Mitko Haralanov94158442016-04-20 06:05:36 -07001118 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001119}
1120
1121static int get_ctxt_info(struct file *fp, void __user *ubase, __u32 len)
1122{
1123 struct hfi1_ctxt_info cinfo;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001124 struct hfi1_filedata *fd = fp->private_data;
Ira Weiny9e10af42015-10-30 18:58:40 -04001125 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001126 int ret = 0;
1127
Dan Carpenterebe6b2e2015-09-16 09:42:25 +03001128 memset(&cinfo, 0, sizeof(cinfo));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001129 ret = hfi1_get_base_kinfo(uctxt, &cinfo);
1130 if (ret < 0)
1131 goto done;
1132 cinfo.num_active = hfi1_count_active_units();
1133 cinfo.unit = uctxt->dd->unit;
1134 cinfo.ctxt = uctxt->ctxt;
Ira Weiny9e10af42015-10-30 18:58:40 -04001135 cinfo.subctxt = fd->subctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001136 cinfo.rcvtids = roundup(uctxt->egrbufs.alloced,
1137 uctxt->dd->rcv_entries.group_size) +
1138 uctxt->expected_count;
1139 cinfo.credits = uctxt->sc->credits;
1140 cinfo.numa_node = uctxt->numa_id;
1141 cinfo.rec_cpu = fd->rec_cpu_num;
1142 cinfo.send_ctxt = uctxt->sc->hw_context;
1143
1144 cinfo.egrtids = uctxt->egrbufs.alloced;
1145 cinfo.rcvhdrq_cnt = uctxt->rcvhdrq_cnt;
1146 cinfo.rcvhdrq_entsize = uctxt->rcvhdrqentsize << 2;
Ira Weiny9e10af42015-10-30 18:58:40 -04001147 cinfo.sdma_ring_size = fd->cq->nentries;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001148 cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size;
1149
Ira Weiny9e10af42015-10-30 18:58:40 -04001150 trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, cinfo);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001151 if (copy_to_user(ubase, &cinfo, sizeof(cinfo)))
1152 ret = -EFAULT;
1153done:
1154 return ret;
1155}
1156
1157static int setup_ctxt(struct file *fp)
1158{
Ira Weiny9e10af42015-10-30 18:58:40 -04001159 struct hfi1_filedata *fd = fp->private_data;
1160 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001161 struct hfi1_devdata *dd = uctxt->dd;
1162 int ret = 0;
1163
1164 /*
Mitko Haralanov94158442016-04-20 06:05:36 -07001165 * Context should be set up only once, including allocation and
Mike Marciniszyn77241052015-07-30 15:17:43 -04001166 * programming of eager buffers. This is done if context sharing
1167 * is not requested or by the master process.
1168 */
Ira Weiny9e10af42015-10-30 18:58:40 -04001169 if (!uctxt->subctxt_cnt || !fd->subctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001170 ret = hfi1_init_ctxt(uctxt->sc);
1171 if (ret)
1172 goto done;
1173
1174 /* Now allocate the RcvHdr queue and eager buffers. */
1175 ret = hfi1_create_rcvhdrq(dd, uctxt);
1176 if (ret)
1177 goto done;
1178 ret = hfi1_setup_eagerbufs(uctxt);
1179 if (ret)
1180 goto done;
Ira Weiny9e10af42015-10-30 18:58:40 -04001181 if (uctxt->subctxt_cnt && !fd->subctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001182 ret = setup_subctxt(uctxt);
1183 if (ret)
1184 goto done;
1185 }
Mitko Haralanov94158442016-04-20 06:05:36 -07001186 } else {
1187 ret = wait_event_interruptible(uctxt->wait, !test_bit(
1188 HFI1_CTXT_MASTER_UNINIT,
1189 &uctxt->event_flags));
1190 if (ret)
1191 goto done;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001192 }
Mitko Haralanov94158442016-04-20 06:05:36 -07001193
Mike Marciniszyn77241052015-07-30 15:17:43 -04001194 ret = hfi1_user_sdma_alloc_queues(uctxt, fp);
1195 if (ret)
1196 goto done;
Mitko Haralanov94158442016-04-20 06:05:36 -07001197 /*
1198 * Expected receive has to be setup for all processes (including
1199 * shared contexts). However, it has to be done after the master
1200 * context has been fully configured as it depends on the
1201 * eager/expected split of the RcvArray entries.
1202 * Setting it up here ensures that the subcontexts will be waiting
1203 * (due to the above wait_event_interruptible() until the master
1204 * is setup.
1205 */
1206 ret = hfi1_user_exp_rcv_init(fp);
1207 if (ret)
1208 goto done;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001209
1210 set_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags);
1211done:
1212 return ret;
1213}
1214
1215static int get_base_info(struct file *fp, void __user *ubase, __u32 len)
1216{
1217 struct hfi1_base_info binfo;
Ira Weiny9e10af42015-10-30 18:58:40 -04001218 struct hfi1_filedata *fd = fp->private_data;
1219 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001220 struct hfi1_devdata *dd = uctxt->dd;
1221 ssize_t sz;
1222 unsigned offset;
1223 int ret = 0;
1224
1225 trace_hfi1_uctxtdata(uctxt->dd, uctxt);
1226
1227 memset(&binfo, 0, sizeof(binfo));
1228 binfo.hw_version = dd->revision;
1229 binfo.sw_version = HFI1_KERN_SWVERSION;
1230 binfo.bthqp = kdeth_qp;
1231 binfo.jkey = uctxt->jkey;
1232 /*
1233 * If more than 64 contexts are enabled the allocated credit
1234 * return will span two or three contiguous pages. Since we only
1235 * map the page containing the context's credit return address,
1236 * we need to calculate the offset in the proper page.
1237 */
1238 offset = ((u64)uctxt->sc->hw_free -
1239 (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE;
1240 binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001241 fd->subctxt, offset);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001242 binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001243 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001244 uctxt->sc->base_addr);
1245 binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP,
1246 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001247 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001248 uctxt->sc->base_addr);
1249 binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001250 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001251 uctxt->rcvhdrq);
1252 binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001253 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001254 uctxt->egrbufs.rcvtids[0].phys);
1255 binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001256 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001257 /*
1258 * user regs are at
1259 * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE))
1260 */
1261 binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001262 fd->subctxt, 0);
Geliang Tange260e402015-10-03 10:34:59 +08001263 offset = offset_in_page((((uctxt->ctxt - dd->first_user_ctxt) *
Ira Weiny9e10af42015-10-30 18:58:40 -04001264 HFI1_MAX_SHARED_CTXTS) + fd->subctxt) *
Geliang Tange260e402015-10-03 10:34:59 +08001265 sizeof(*dd->events));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001266 binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001267 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001268 offset);
1269 binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001270 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001271 dd->status);
1272 if (HFI1_CAP_IS_USET(DMA_RTAIL))
1273 binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001274 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001275 if (uctxt->subctxt_cnt) {
1276 binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS,
1277 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001278 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001279 binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ,
1280 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001281 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001282 binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF,
1283 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001284 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001285 }
1286 sz = (len < sizeof(binfo)) ? len : sizeof(binfo);
1287 if (copy_to_user(ubase, &binfo, sz))
1288 ret = -EFAULT;
1289 return ret;
1290}
1291
1292static unsigned int poll_urgent(struct file *fp,
1293 struct poll_table_struct *pt)
1294{
Ira Weiny9e10af42015-10-30 18:58:40 -04001295 struct hfi1_filedata *fd = fp->private_data;
1296 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001297 struct hfi1_devdata *dd = uctxt->dd;
1298 unsigned pollflag;
1299
1300 poll_wait(fp, &uctxt->wait, pt);
1301
1302 spin_lock_irq(&dd->uctxt_lock);
1303 if (uctxt->urgent != uctxt->urgent_poll) {
1304 pollflag = POLLIN | POLLRDNORM;
1305 uctxt->urgent_poll = uctxt->urgent;
1306 } else {
1307 pollflag = 0;
1308 set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags);
1309 }
1310 spin_unlock_irq(&dd->uctxt_lock);
1311
1312 return pollflag;
1313}
1314
1315static unsigned int poll_next(struct file *fp,
1316 struct poll_table_struct *pt)
1317{
Ira Weiny9e10af42015-10-30 18:58:40 -04001318 struct hfi1_filedata *fd = fp->private_data;
1319 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001320 struct hfi1_devdata *dd = uctxt->dd;
1321 unsigned pollflag;
1322
1323 poll_wait(fp, &uctxt->wait, pt);
1324
1325 spin_lock_irq(&dd->uctxt_lock);
1326 if (hdrqempty(uctxt)) {
1327 set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags);
1328 hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt->ctxt);
1329 pollflag = 0;
Jubin Johne4909742016-02-14 20:22:00 -08001330 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001331 pollflag = POLLIN | POLLRDNORM;
Jubin Johne4909742016-02-14 20:22:00 -08001332 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001333 spin_unlock_irq(&dd->uctxt_lock);
1334
1335 return pollflag;
1336}
1337
1338/*
1339 * Find all user contexts in use, and set the specified bit in their
1340 * event mask.
1341 * See also find_ctxt() for a similar use, that is specific to send buffers.
1342 */
1343int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit)
1344{
1345 struct hfi1_ctxtdata *uctxt;
1346 struct hfi1_devdata *dd = ppd->dd;
1347 unsigned ctxt;
1348 int ret = 0;
1349 unsigned long flags;
1350
1351 if (!dd->events) {
1352 ret = -EINVAL;
1353 goto done;
1354 }
1355
1356 spin_lock_irqsave(&dd->uctxt_lock, flags);
1357 for (ctxt = dd->first_user_ctxt; ctxt < dd->num_rcv_contexts;
1358 ctxt++) {
1359 uctxt = dd->rcd[ctxt];
1360 if (uctxt) {
1361 unsigned long *evs = dd->events +
1362 (uctxt->ctxt - dd->first_user_ctxt) *
1363 HFI1_MAX_SHARED_CTXTS;
1364 int i;
1365 /*
1366 * subctxt_cnt is 0 if not shared, so do base
1367 * separately, first, then remaining subctxt, if any
1368 */
1369 set_bit(evtbit, evs);
1370 for (i = 1; i < uctxt->subctxt_cnt; i++)
1371 set_bit(evtbit, evs + i);
1372 }
1373 }
1374 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1375done:
1376 return ret;
1377}
1378
1379/**
1380 * manage_rcvq - manage a context's receive queue
1381 * @uctxt: the context
1382 * @subctxt: the sub-context
1383 * @start_stop: action to carry out
1384 *
1385 * start_stop == 0 disables receive on the context, for use in queue
1386 * overflow conditions. start_stop==1 re-enables, to be used to
1387 * re-init the software copy of the head register
1388 */
1389static int manage_rcvq(struct hfi1_ctxtdata *uctxt, unsigned subctxt,
1390 int start_stop)
1391{
1392 struct hfi1_devdata *dd = uctxt->dd;
1393 unsigned int rcvctrl_op;
1394
1395 if (subctxt)
1396 goto bail;
1397 /* atomically clear receive enable ctxt. */
1398 if (start_stop) {
1399 /*
1400 * On enable, force in-memory copy of the tail register to
1401 * 0, so that protocol code doesn't have to worry about
1402 * whether or not the chip has yet updated the in-memory
1403 * copy or not on return from the system call. The chip
1404 * always resets it's tail register back to 0 on a
1405 * transition from disabled to enabled.
1406 */
1407 if (uctxt->rcvhdrtail_kvaddr)
1408 clear_rcvhdrtail(uctxt);
1409 rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB;
Jubin Johne4909742016-02-14 20:22:00 -08001410 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001411 rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS;
Jubin Johne4909742016-02-14 20:22:00 -08001412 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001413 hfi1_rcvctrl(dd, rcvctrl_op, uctxt->ctxt);
1414 /* always; new head should be equal to new tail; see above */
1415bail:
1416 return 0;
1417}
1418
1419/*
1420 * clear the event notifier events for this context.
1421 * User process then performs actions appropriate to bit having been
1422 * set, if desired, and checks again in future.
1423 */
1424static int user_event_ack(struct hfi1_ctxtdata *uctxt, int subctxt,
1425 unsigned long events)
1426{
1427 int i;
1428 struct hfi1_devdata *dd = uctxt->dd;
1429 unsigned long *evs;
1430
1431 if (!dd->events)
1432 return 0;
1433
1434 evs = dd->events + ((uctxt->ctxt - dd->first_user_ctxt) *
1435 HFI1_MAX_SHARED_CTXTS) + subctxt;
1436
1437 for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) {
1438 if (!test_bit(i, &events))
1439 continue;
1440 clear_bit(i, evs);
1441 }
1442 return 0;
1443}
1444
Mike Marciniszyn77241052015-07-30 15:17:43 -04001445static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned subctxt,
1446 u16 pkey)
1447{
1448 int ret = -ENOENT, i, intable = 0;
1449 struct hfi1_pportdata *ppd = uctxt->ppd;
1450 struct hfi1_devdata *dd = uctxt->dd;
1451
1452 if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY) {
1453 ret = -EINVAL;
1454 goto done;
1455 }
1456
1457 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++)
1458 if (pkey == ppd->pkeys[i]) {
1459 intable = 1;
1460 break;
1461 }
1462
1463 if (intable)
1464 ret = hfi1_set_ctxt_pkey(dd, uctxt->ctxt, pkey);
1465done:
1466 return ret;
1467}
1468
Mike Marciniszyn77241052015-07-30 15:17:43 -04001469static void user_remove(struct hfi1_devdata *dd)
1470{
Mike Marciniszyn77241052015-07-30 15:17:43 -04001471
1472 hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001473}
1474
1475static int user_add(struct hfi1_devdata *dd)
1476{
1477 char name[10];
1478 int ret;
1479
Mike Marciniszyn77241052015-07-30 15:17:43 -04001480 snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit);
Dennis Dalessandro0eb62652016-05-19 05:25:50 -07001481 ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops,
Ira Weinye116a642015-09-17 13:47:49 -04001482 &dd->user_cdev, &dd->user_device,
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001483 true, &dd->kobj);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001484 if (ret)
Dennis Dalessandro7312f292016-05-19 05:25:57 -07001485 user_remove(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001486
Mike Marciniszyn77241052015-07-30 15:17:43 -04001487 return ret;
1488}
1489
1490/*
1491 * Create per-unit files in /dev
1492 */
1493int hfi1_device_create(struct hfi1_devdata *dd)
1494{
Dennis Dalessandro0f7b1f92016-05-19 05:26:10 -07001495 return user_add(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001496}
1497
1498/*
1499 * Remove per-unit files in /dev
1500 * void, core kernel returns no errors for this stuff
1501 */
1502void hfi1_device_remove(struct hfi1_devdata *dd)
1503{
1504 user_remove(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001505}