blob: 13bb6a5e4d373601e4b9403294b9196a03ed0415 [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
Ira Weiny3faa3d92016-07-28 15:21:19 -0400183 if (fd) {
184 fd->rec_cpu_num = -1; /* no cpu affinity by default */
185 fd->mm = current->mm;
Ira Weinye0cf75d2016-08-16 13:27:03 -0700186 atomic_inc(&fd->mm->mm_count);
Ira Weiny3faa3d92016-07-28 15:21:19 -0400187 }
Ira Weinyea3a0ee2016-07-28 12:27:35 -0400188
189 fp->private_data = fd;
190
191 return fd ? 0 : -ENOMEM;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400192}
193
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700194static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
195 unsigned long arg)
196{
197 struct hfi1_filedata *fd = fp->private_data;
198 struct hfi1_ctxtdata *uctxt = fd->uctxt;
199 struct hfi1_user_info uinfo;
200 struct hfi1_tid_info tinfo;
201 int ret = 0;
202 unsigned long addr;
203 int uval = 0;
204 unsigned long ul_uval = 0;
205 u16 uval16 = 0;
206
Dennis Dalessandro8a1882e2016-05-19 05:26:37 -0700207 hfi1_cdbg(IOCTL, "IOCTL recv: 0x%x", cmd);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700208 if (cmd != HFI1_IOCTL_ASSIGN_CTXT &&
209 cmd != HFI1_IOCTL_GET_VERS &&
210 !uctxt)
211 return -EINVAL;
212
213 switch (cmd) {
214 case HFI1_IOCTL_ASSIGN_CTXT:
Ira Weinyca2f30a2016-06-09 07:51:33 -0700215 if (uctxt)
216 return -EINVAL;
217
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700218 if (copy_from_user(&uinfo,
219 (struct hfi1_user_info __user *)arg,
220 sizeof(uinfo)))
221 return -EFAULT;
222
223 ret = assign_ctxt(fp, &uinfo);
224 if (ret < 0)
225 return ret;
Christophe Jaillet57bb5622016-08-10 07:34:27 +0200226 ret = setup_ctxt(fp);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700227 if (ret)
228 return ret;
229 ret = user_init(fp);
230 break;
231 case HFI1_IOCTL_CTXT_INFO:
232 ret = get_ctxt_info(fp, (void __user *)(unsigned long)arg,
233 sizeof(struct hfi1_ctxt_info));
234 break;
235 case HFI1_IOCTL_USER_INFO:
236 ret = get_base_info(fp, (void __user *)(unsigned long)arg,
237 sizeof(struct hfi1_base_info));
238 break;
239 case HFI1_IOCTL_CREDIT_UPD:
Markus Elfringf7ca5352016-07-23 08:30:52 +0200240 if (uctxt)
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700241 sc_return_credits(uctxt->sc);
242 break;
243
244 case HFI1_IOCTL_TID_UPDATE:
245 if (copy_from_user(&tinfo,
246 (struct hfi11_tid_info __user *)arg,
247 sizeof(tinfo)))
248 return -EFAULT;
249
250 ret = hfi1_user_exp_rcv_setup(fp, &tinfo);
251 if (!ret) {
252 /*
253 * Copy the number of tidlist entries we used
254 * and the length of the buffer we registered.
255 * These fields are adjacent in the structure so
256 * we can copy them at the same time.
257 */
258 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
259 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
260 sizeof(tinfo.tidcnt) +
261 sizeof(tinfo.length)))
262 ret = -EFAULT;
263 }
264 break;
265
266 case HFI1_IOCTL_TID_FREE:
267 if (copy_from_user(&tinfo,
268 (struct hfi11_tid_info __user *)arg,
269 sizeof(tinfo)))
270 return -EFAULT;
271
272 ret = hfi1_user_exp_rcv_clear(fp, &tinfo);
273 if (ret)
274 break;
275 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
276 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
277 sizeof(tinfo.tidcnt)))
278 ret = -EFAULT;
279 break;
280
281 case HFI1_IOCTL_TID_INVAL_READ:
282 if (copy_from_user(&tinfo,
283 (struct hfi11_tid_info __user *)arg,
284 sizeof(tinfo)))
285 return -EFAULT;
286
287 ret = hfi1_user_exp_rcv_invalid(fp, &tinfo);
288 if (ret)
289 break;
290 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
291 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
292 sizeof(tinfo.tidcnt)))
293 ret = -EFAULT;
294 break;
295
296 case HFI1_IOCTL_RECV_CTRL:
297 ret = get_user(uval, (int __user *)arg);
298 if (ret != 0)
299 return -EFAULT;
300 ret = manage_rcvq(uctxt, fd->subctxt, uval);
301 break;
302
303 case HFI1_IOCTL_POLL_TYPE:
304 ret = get_user(uval, (int __user *)arg);
305 if (ret != 0)
306 return -EFAULT;
307 uctxt->poll_type = (typeof(uctxt->poll_type))uval;
308 break;
309
310 case HFI1_IOCTL_ACK_EVENT:
311 ret = get_user(ul_uval, (unsigned long __user *)arg);
312 if (ret != 0)
313 return -EFAULT;
314 ret = user_event_ack(uctxt, fd->subctxt, ul_uval);
315 break;
316
317 case HFI1_IOCTL_SET_PKEY:
318 ret = get_user(uval16, (u16 __user *)arg);
319 if (ret != 0)
320 return -EFAULT;
321 if (HFI1_CAP_IS_USET(PKEY_CHECK))
322 ret = set_ctxt_pkey(uctxt, fd->subctxt, uval16);
323 else
324 return -EPERM;
325 break;
326
327 case HFI1_IOCTL_CTXT_RESET: {
328 struct send_context *sc;
329 struct hfi1_devdata *dd;
330
331 if (!uctxt || !uctxt->dd || !uctxt->sc)
332 return -EINVAL;
333
334 /*
335 * There is no protection here. User level has to
336 * guarantee that no one will be writing to the send
337 * context while it is being re-initialized.
338 * If user level breaks that guarantee, it will break
339 * it's own context and no one else's.
340 */
341 dd = uctxt->dd;
342 sc = uctxt->sc;
343 /*
344 * Wait until the interrupt handler has marked the
345 * context as halted or frozen. Report error if we time
346 * out.
347 */
348 wait_event_interruptible_timeout(
349 sc->halt_wait, (sc->flags & SCF_HALTED),
350 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
351 if (!(sc->flags & SCF_HALTED))
352 return -ENOLCK;
353
354 /*
355 * If the send context was halted due to a Freeze,
356 * wait until the device has been "unfrozen" before
357 * resetting the context.
358 */
359 if (sc->flags & SCF_FROZEN) {
360 wait_event_interruptible_timeout(
361 dd->event_queue,
362 !(ACCESS_ONCE(dd->flags) & HFI1_FROZEN),
363 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
364 if (dd->flags & HFI1_FROZEN)
365 return -ENOLCK;
366
367 if (dd->flags & HFI1_FORCED_FREEZE)
368 /*
369 * Don't allow context reset if we are into
370 * forced freeze
371 */
372 return -ENODEV;
373
374 sc_disable(sc);
375 ret = sc_enable(sc);
376 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB,
377 uctxt->ctxt);
378 } else {
379 ret = sc_restart(sc);
380 }
381 if (!ret)
382 sc_return_credits(sc);
383 break;
384 }
385
386 case HFI1_IOCTL_GET_VERS:
387 uval = HFI1_USER_SWVERSION;
388 if (put_user(uval, (int __user *)arg))
389 return -EFAULT;
390 break;
391
392 default:
393 return -EINVAL;
394 }
395
396 return ret;
397}
398
Mike Marciniszyn77241052015-07-30 15:17:43 -0400399static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from)
400{
Ira Weiny9e10af42015-10-30 18:58:40 -0400401 struct hfi1_filedata *fd = kiocb->ki_filp->private_data;
402 struct hfi1_user_sdma_pkt_q *pq = fd->pq;
403 struct hfi1_user_sdma_comp_q *cq = fd->cq;
Ira Weiny0904f322016-07-01 16:00:55 -0700404 int done = 0, reqs = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400405 unsigned long dim = from->nr_segs;
406
Ira Weiny0904f322016-07-01 16:00:55 -0700407 if (!cq || !pq)
408 return -EIO;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400409
Ira Weiny0904f322016-07-01 16:00:55 -0700410 if (!iter_is_iovec(from) || !dim)
411 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400412
413 hfi1_cdbg(SDMA, "SDMA request from %u:%u (%lu)",
Ira Weiny9e10af42015-10-30 18:58:40 -0400414 fd->uctxt->ctxt, fd->subctxt, dim);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400415
Ira Weiny0904f322016-07-01 16:00:55 -0700416 if (atomic_read(&pq->n_reqs) == pq->n_max_reqs)
417 return -ENOSPC;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400418
419 while (dim) {
Ira Weiny0904f322016-07-01 16:00:55 -0700420 int ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400421 unsigned long count = 0;
422
423 ret = hfi1_user_sdma_process_request(
424 kiocb->ki_filp, (struct iovec *)(from->iov + done),
425 dim, &count);
Ira Weiny0904f322016-07-01 16:00:55 -0700426 if (ret) {
427 reqs = ret;
428 break;
429 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400430 dim -= count;
431 done += count;
432 reqs++;
433 }
Ira Weiny0904f322016-07-01 16:00:55 -0700434
435 return reqs;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400436}
437
438static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma)
439{
Ira Weiny9e10af42015-10-30 18:58:40 -0400440 struct hfi1_filedata *fd = fp->private_data;
441 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400442 struct hfi1_devdata *dd;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700443 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400444 u64 token = vma->vm_pgoff << PAGE_SHIFT,
445 memaddr = 0;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700446 void *memvirt = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400447 u8 subctxt, mapio = 0, vmf = 0, type;
448 ssize_t memlen = 0;
449 int ret = 0;
450 u16 ctxt;
451
Mike Marciniszyn77241052015-07-30 15:17:43 -0400452 if (!is_valid_mmap(token) || !uctxt ||
453 !(vma->vm_flags & VM_SHARED)) {
454 ret = -EINVAL;
455 goto done;
456 }
457 dd = uctxt->dd;
458 ctxt = HFI1_MMAP_TOKEN_GET(CTXT, token);
459 subctxt = HFI1_MMAP_TOKEN_GET(SUBCTXT, token);
460 type = HFI1_MMAP_TOKEN_GET(TYPE, token);
Ira Weiny9e10af42015-10-30 18:58:40 -0400461 if (ctxt != uctxt->ctxt || subctxt != fd->subctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400462 ret = -EINVAL;
463 goto done;
464 }
465
466 flags = vma->vm_flags;
467
468 switch (type) {
469 case PIO_BUFS:
470 case PIO_BUFS_SOP:
471 memaddr = ((dd->physaddr + TXE_PIO_SEND) +
472 /* chip pio base */
Amitoj Kaur Chawlad32cf442015-10-16 22:09:08 +0530473 (uctxt->sc->hw_context * BIT(16))) +
Mike Marciniszyn77241052015-07-30 15:17:43 -0400474 /* 64K PIO space / ctxt */
475 (type == PIO_BUFS_SOP ?
476 (TXE_PIO_SIZE / 2) : 0); /* sop? */
477 /*
478 * Map only the amount allocated to the context, not the
479 * entire available context's PIO space.
480 */
Amitoj Kaur Chawla437b29d2016-03-04 22:45:00 +0530481 memlen = PAGE_ALIGN(uctxt->sc->credits * PIO_BLOCK_SIZE);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400482 flags &= ~VM_MAYREAD;
483 flags |= VM_DONTCOPY | VM_DONTEXPAND;
484 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
485 mapio = 1;
486 break;
487 case PIO_CRED:
488 if (flags & VM_WRITE) {
489 ret = -EPERM;
490 goto done;
491 }
492 /*
493 * The credit return location for this context could be on the
494 * second or third page allocated for credit returns (if number
495 * of enabled contexts > 64 and 128 respectively).
496 */
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700497 memvirt = dd->cr_base[uctxt->numa_id].va;
498 memaddr = virt_to_phys(memvirt) +
Mike Marciniszyn77241052015-07-30 15:17:43 -0400499 (((u64)uctxt->sc->hw_free -
500 (u64)dd->cr_base[uctxt->numa_id].va) & PAGE_MASK);
501 memlen = PAGE_SIZE;
502 flags &= ~VM_MAYWRITE;
503 flags |= VM_DONTCOPY | VM_DONTEXPAND;
504 /*
505 * The driver has already allocated memory for credit
506 * returns and programmed it into the chip. Has that
507 * memory been flagged as non-cached?
508 */
509 /* vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); */
510 mapio = 1;
511 break;
512 case RCV_HDRQ:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400513 memlen = uctxt->rcvhdrq_size;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700514 memvirt = uctxt->rcvhdrq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400515 break;
516 case RCV_EGRBUF: {
517 unsigned long addr;
518 int i;
519 /*
520 * The RcvEgr buffer need to be handled differently
521 * as multiple non-contiguous pages need to be mapped
522 * into the user process.
523 */
524 memlen = uctxt->egrbufs.size;
525 if ((vma->vm_end - vma->vm_start) != memlen) {
526 dd_dev_err(dd, "Eager buffer map size invalid (%lu != %lu)\n",
527 (vma->vm_end - vma->vm_start), memlen);
528 ret = -EINVAL;
529 goto done;
530 }
531 if (vma->vm_flags & VM_WRITE) {
532 ret = -EPERM;
533 goto done;
534 }
535 vma->vm_flags &= ~VM_MAYWRITE;
536 addr = vma->vm_start;
537 for (i = 0 ; i < uctxt->egrbufs.numbufs; i++) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700538 memlen = uctxt->egrbufs.buffers[i].len;
539 memvirt = uctxt->egrbufs.buffers[i].addr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400540 ret = remap_pfn_range(
541 vma, addr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700542 /*
543 * virt_to_pfn() does the same, but
544 * it's not available on x86_64
545 * when CONFIG_MMU is enabled.
546 */
547 PFN_DOWN(__pa(memvirt)),
548 memlen,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400549 vma->vm_page_prot);
550 if (ret < 0)
551 goto done;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700552 addr += memlen;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400553 }
554 ret = 0;
555 goto done;
556 }
557 case UREGS:
558 /*
559 * Map only the page that contains this context's user
560 * registers.
561 */
562 memaddr = (unsigned long)
563 (dd->physaddr + RXE_PER_CONTEXT_USER)
564 + (uctxt->ctxt * RXE_PER_CONTEXT_SIZE);
565 /*
566 * TidFlow table is on the same page as the rest of the
567 * user registers.
568 */
569 memlen = PAGE_SIZE;
570 flags |= VM_DONTCOPY | VM_DONTEXPAND;
571 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
572 mapio = 1;
573 break;
574 case EVENTS:
575 /*
576 * Use the page where this context's flags are. User level
577 * knows where it's own bitmap is within the page.
578 */
Mitko Haralanov3c6c0652015-10-26 10:28:39 -0400579 memaddr = (unsigned long)(dd->events +
580 ((uctxt->ctxt - dd->first_user_ctxt) *
581 HFI1_MAX_SHARED_CTXTS)) & PAGE_MASK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400582 memlen = PAGE_SIZE;
583 /*
584 * v3.7 removes VM_RESERVED but the effect is kept by
585 * using VM_IO.
586 */
587 flags |= VM_IO | VM_DONTEXPAND;
588 vmf = 1;
589 break;
590 case STATUS:
591 memaddr = kvirt_to_phys((void *)dd->status);
592 memlen = PAGE_SIZE;
593 flags |= VM_IO | VM_DONTEXPAND;
594 break;
595 case RTAIL:
596 if (!HFI1_CAP_IS_USET(DMA_RTAIL)) {
597 /*
598 * If the memory allocation failed, the context alloc
599 * also would have failed, so we would never get here
600 */
601 ret = -EINVAL;
602 goto done;
603 }
604 if (flags & VM_WRITE) {
605 ret = -EPERM;
606 goto done;
607 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400608 memlen = PAGE_SIZE;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700609 memvirt = (void *)uctxt->rcvhdrtail_kvaddr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400610 flags &= ~VM_MAYWRITE;
611 break;
612 case SUBCTXT_UREGS:
613 memaddr = (u64)uctxt->subctxt_uregbase;
614 memlen = PAGE_SIZE;
615 flags |= VM_IO | VM_DONTEXPAND;
616 vmf = 1;
617 break;
618 case SUBCTXT_RCV_HDRQ:
619 memaddr = (u64)uctxt->subctxt_rcvhdr_base;
620 memlen = uctxt->rcvhdrq_size * uctxt->subctxt_cnt;
621 flags |= VM_IO | VM_DONTEXPAND;
622 vmf = 1;
623 break;
624 case SUBCTXT_EGRBUF:
625 memaddr = (u64)uctxt->subctxt_rcvegrbuf;
626 memlen = uctxt->egrbufs.size * uctxt->subctxt_cnt;
627 flags |= VM_IO | VM_DONTEXPAND;
628 flags &= ~VM_MAYWRITE;
629 vmf = 1;
630 break;
631 case SDMA_COMP: {
Ira Weiny9e10af42015-10-30 18:58:40 -0400632 struct hfi1_user_sdma_comp_q *cq = fd->cq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400633
Ira Weiny9e10af42015-10-30 18:58:40 -0400634 if (!cq) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400635 ret = -EFAULT;
636 goto done;
637 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400638 memaddr = (u64)cq->comps;
Amitoj Kaur Chawla437b29d2016-03-04 22:45:00 +0530639 memlen = PAGE_ALIGN(sizeof(*cq->comps) * cq->nentries);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400640 flags |= VM_IO | VM_DONTEXPAND;
641 vmf = 1;
642 break;
643 }
644 default:
645 ret = -EINVAL;
646 break;
647 }
648
649 if ((vma->vm_end - vma->vm_start) != memlen) {
650 hfi1_cdbg(PROC, "%u:%u Memory size mismatch %lu:%lu",
Ira Weiny9e10af42015-10-30 18:58:40 -0400651 uctxt->ctxt, fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400652 (vma->vm_end - vma->vm_start), memlen);
653 ret = -EINVAL;
654 goto done;
655 }
656
657 vma->vm_flags = flags;
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500658 hfi1_cdbg(PROC,
659 "%u:%u type:%u io/vf:%d/%d, addr:0x%llx, len:%lu(%lu), flags:0x%lx\n",
660 ctxt, subctxt, type, mapio, vmf, memaddr, memlen,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400661 vma->vm_end - vma->vm_start, vma->vm_flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400662 if (vmf) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700663 vma->vm_pgoff = PFN_DOWN(memaddr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400664 vma->vm_ops = &vm_ops;
665 ret = 0;
666 } else if (mapio) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700667 ret = io_remap_pfn_range(vma, vma->vm_start,
668 PFN_DOWN(memaddr),
669 memlen,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400670 vma->vm_page_prot);
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700671 } else if (memvirt) {
672 ret = remap_pfn_range(vma, vma->vm_start,
673 PFN_DOWN(__pa(memvirt)),
674 memlen,
675 vma->vm_page_prot);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400676 } else {
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700677 ret = remap_pfn_range(vma, vma->vm_start,
678 PFN_DOWN(memaddr),
679 memlen,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400680 vma->vm_page_prot);
681 }
682done:
683 return ret;
684}
685
686/*
687 * Local (non-chip) user memory is not mapped right away but as it is
688 * accessed by the user-level code.
689 */
690static int vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
691{
692 struct page *page;
693
694 page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
695 if (!page)
696 return VM_FAULT_SIGBUS;
697
698 get_page(page);
699 vmf->page = page;
700
701 return 0;
702}
703
704static unsigned int hfi1_poll(struct file *fp, struct poll_table_struct *pt)
705{
706 struct hfi1_ctxtdata *uctxt;
707 unsigned pollflag;
708
Ira Weiny9e10af42015-10-30 18:58:40 -0400709 uctxt = ((struct hfi1_filedata *)fp->private_data)->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400710 if (!uctxt)
711 pollflag = POLLERR;
712 else if (uctxt->poll_type == HFI1_POLL_TYPE_URGENT)
713 pollflag = poll_urgent(fp, pt);
714 else if (uctxt->poll_type == HFI1_POLL_TYPE_ANYRCV)
715 pollflag = poll_next(fp, pt);
716 else /* invalid */
717 pollflag = POLLERR;
718
719 return pollflag;
720}
721
722static int hfi1_file_close(struct inode *inode, struct file *fp)
723{
724 struct hfi1_filedata *fdata = fp->private_data;
725 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700726 struct hfi1_devdata *dd = container_of(inode->i_cdev,
727 struct hfi1_devdata,
728 user_cdev);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400729 unsigned long flags, *ev;
730
731 fp->private_data = NULL;
732
733 if (!uctxt)
734 goto done;
735
736 hfi1_cdbg(PROC, "freeing ctxt %u:%u", uctxt->ctxt, fdata->subctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400737 mutex_lock(&hfi1_mutex);
738
739 flush_wc();
740 /* drain user sdma queue */
Mitko Haralanov483119a2015-12-08 17:10:10 -0500741 hfi1_user_sdma_free_queues(fdata);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400742
Mitko Haralanov957558c2016-02-03 14:33:40 -0800743 /* release the cpu */
Sebastian Sanchezb094a362016-07-25 07:54:57 -0700744 hfi1_put_proc_affinity(fdata->rec_cpu_num);
Mitko Haralanov957558c2016-02-03 14:33:40 -0800745
Mike Marciniszyn77241052015-07-30 15:17:43 -0400746 /*
747 * Clear any left over, unhandled events so the next process that
748 * gets this context doesn't get confused.
749 */
750 ev = dd->events + ((uctxt->ctxt - dd->first_user_ctxt) *
751 HFI1_MAX_SHARED_CTXTS) + fdata->subctxt;
752 *ev = 0;
753
754 if (--uctxt->cnt) {
755 uctxt->active_slaves &= ~(1 << fdata->subctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400756 mutex_unlock(&hfi1_mutex);
757 goto done;
758 }
759
760 spin_lock_irqsave(&dd->uctxt_lock, flags);
761 /*
762 * Disable receive context and interrupt available, reset all
763 * RcvCtxtCtrl bits to default values.
764 */
765 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
766 HFI1_RCVCTRL_TIDFLOW_DIS |
767 HFI1_RCVCTRL_INTRAVAIL_DIS |
Mitko Haralanov566c1572016-02-03 14:32:49 -0800768 HFI1_RCVCTRL_TAILUPD_DIS |
Mike Marciniszyn77241052015-07-30 15:17:43 -0400769 HFI1_RCVCTRL_ONE_PKT_EGR_DIS |
770 HFI1_RCVCTRL_NO_RHQ_DROP_DIS |
771 HFI1_RCVCTRL_NO_EGR_DROP_DIS, uctxt->ctxt);
772 /* Clear the context's J_KEY */
773 hfi1_clear_ctxt_jkey(dd, uctxt->ctxt);
774 /*
775 * Reset context integrity checks to default.
776 * (writes to CSRs probably belong in chip.c)
777 */
778 write_kctxt_csr(dd, uctxt->sc->hw_context, SEND_CTXT_CHECK_ENABLE,
779 hfi1_pkt_default_send_ctxt_mask(dd, uctxt->sc->type));
780 sc_disable(uctxt->sc);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400781 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
782
783 dd->rcd[uctxt->ctxt] = NULL;
Mitko Haralanov94158442016-04-20 06:05:36 -0700784
785 hfi1_user_exp_rcv_free(fdata);
786 hfi1_clear_ctxt_pkey(dd, uctxt->ctxt);
787
Mike Marciniszyn77241052015-07-30 15:17:43 -0400788 uctxt->rcvwait_to = 0;
789 uctxt->piowait_to = 0;
790 uctxt->rcvnowait = 0;
791 uctxt->pionowait = 0;
792 uctxt->event_flags = 0;
793
Mike Marciniszyn77241052015-07-30 15:17:43 -0400794 hfi1_stats.sps_ctxts--;
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -0800795 if (++dd->freectxts == dd->num_user_contexts)
796 aspm_enable_all(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400797 mutex_unlock(&hfi1_mutex);
798 hfi1_free_ctxtdata(dd, uctxt);
799done:
Ira Weinye0cf75d2016-08-16 13:27:03 -0700800 mmdrop(fdata->mm);
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700801 kobject_put(&dd->kobj);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400802 kfree(fdata);
803 return 0;
804}
805
806/*
807 * Convert kernel *virtual* addresses to physical addresses.
808 * This is used to vmalloc'ed addresses.
809 */
810static u64 kvirt_to_phys(void *addr)
811{
812 struct page *page;
813 u64 paddr = 0;
814
815 page = vmalloc_to_page(addr);
816 if (page)
817 paddr = page_to_pfn(page) << PAGE_SHIFT;
818
819 return paddr;
820}
821
822static int assign_ctxt(struct file *fp, struct hfi1_user_info *uinfo)
823{
824 int i_minor, ret = 0;
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700825 unsigned int swmajor, swminor;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400826
827 swmajor = uinfo->userversion >> 16;
828 if (swmajor != HFI1_USER_SWMAJOR) {
829 ret = -ENODEV;
830 goto done;
831 }
832
833 swminor = uinfo->userversion & 0xffff;
834
Mike Marciniszyn77241052015-07-30 15:17:43 -0400835 mutex_lock(&hfi1_mutex);
836 /* First, lets check if we need to setup a shared context? */
Mitko Haralanov957558c2016-02-03 14:33:40 -0800837 if (uinfo->subctxt_cnt) {
838 struct hfi1_filedata *fd = fp->private_data;
839
Mike Marciniszyn77241052015-07-30 15:17:43 -0400840 ret = find_shared_ctxt(fp, uinfo);
Mitko Haralanov957558c2016-02-03 14:33:40 -0800841 if (ret < 0)
842 goto done_unlock;
Sebastian Sanchezb094a362016-07-25 07:54:57 -0700843 if (ret) {
844 fd->rec_cpu_num =
845 hfi1_get_proc_affinity(fd->uctxt->numa_id);
846 }
Mitko Haralanov957558c2016-02-03 14:33:40 -0800847 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400848
849 /*
850 * We execute the following block if we couldn't find a
851 * shared context or if context sharing is not required.
852 */
853 if (!ret) {
854 i_minor = iminor(file_inode(fp)) - HFI1_USER_MINOR_BASE;
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700855 ret = get_user_context(fp, uinfo, i_minor);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400856 }
Mitko Haralanov957558c2016-02-03 14:33:40 -0800857done_unlock:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400858 mutex_unlock(&hfi1_mutex);
859done:
860 return ret;
861}
862
863static int get_user_context(struct file *fp, struct hfi1_user_info *uinfo,
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700864 int devno)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400865{
866 struct hfi1_devdata *dd = NULL;
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700867 int devmax, npresent, nup;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400868
869 devmax = hfi1_count_units(&npresent, &nup);
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700870 if (!npresent)
871 return -ENXIO;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400872
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700873 if (!nup)
874 return -ENETDOWN;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400875
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700876 dd = hfi1_lookup(devno);
877 if (!dd)
878 return -ENODEV;
879 else if (!dd->freectxts)
880 return -EBUSY;
881
882 return allocate_ctxt(fp, dd, uinfo);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400883}
884
885static int find_shared_ctxt(struct file *fp,
886 const struct hfi1_user_info *uinfo)
887{
888 int devmax, ndev, i;
889 int ret = 0;
Ira Weiny9e10af42015-10-30 18:58:40 -0400890 struct hfi1_filedata *fd = fp->private_data;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400891
892 devmax = hfi1_count_units(NULL, NULL);
893
894 for (ndev = 0; ndev < devmax; ndev++) {
895 struct hfi1_devdata *dd = hfi1_lookup(ndev);
896
Mike Marciniszyn77241052015-07-30 15:17:43 -0400897 if (!(dd && (dd->flags & HFI1_PRESENT) && dd->kregbase))
898 continue;
899 for (i = dd->first_user_ctxt; i < dd->num_rcv_contexts; i++) {
900 struct hfi1_ctxtdata *uctxt = dd->rcd[i];
901
902 /* Skip ctxts which are not yet open */
903 if (!uctxt || !uctxt->cnt)
904 continue;
905 /* Skip ctxt if it doesn't match the requested one */
906 if (memcmp(uctxt->uuid, uinfo->uuid,
907 sizeof(uctxt->uuid)) ||
Jareer Abdel-Qader07839042015-10-26 10:28:33 -0400908 uctxt->jkey != generate_jkey(current_uid()) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -0400909 uctxt->subctxt_id != uinfo->subctxt_id ||
910 uctxt->subctxt_cnt != uinfo->subctxt_cnt)
911 continue;
912
913 /* Verify the sharing process matches the master */
914 if (uctxt->userversion != uinfo->userversion ||
915 uctxt->cnt >= uctxt->subctxt_cnt) {
916 ret = -EINVAL;
917 goto done;
918 }
Ira Weiny9e10af42015-10-30 18:58:40 -0400919 fd->uctxt = uctxt;
920 fd->subctxt = uctxt->cnt++;
Ira Weiny9e10af42015-10-30 18:58:40 -0400921 uctxt->active_slaves |= 1 << fd->subctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400922 ret = 1;
923 goto done;
924 }
925 }
926
927done:
928 return ret;
929}
930
931static int allocate_ctxt(struct file *fp, struct hfi1_devdata *dd,
932 struct hfi1_user_info *uinfo)
933{
Ira Weiny9e10af42015-10-30 18:58:40 -0400934 struct hfi1_filedata *fd = fp->private_data;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400935 struct hfi1_ctxtdata *uctxt;
936 unsigned ctxt;
Mitko Haralanov957558c2016-02-03 14:33:40 -0800937 int ret, numa;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400938
939 if (dd->flags & HFI1_FROZEN) {
940 /*
941 * Pick an error that is unique from all other errors
942 * that are returned so the user process knows that
943 * it tried to allocate while the SPC was frozen. It
944 * it should be able to retry with success in a short
945 * while.
946 */
947 return -EIO;
948 }
949
950 for (ctxt = dd->first_user_ctxt; ctxt < dd->num_rcv_contexts; ctxt++)
951 if (!dd->rcd[ctxt])
952 break;
953
954 if (ctxt == dd->num_rcv_contexts)
955 return -EBUSY;
956
Sebastian Sanchezb094a362016-07-25 07:54:57 -0700957 /*
958 * If we don't have a NUMA node requested, preference is towards
959 * device NUMA node.
960 */
961 fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node);
Mitko Haralanov957558c2016-02-03 14:33:40 -0800962 if (fd->rec_cpu_num != -1)
963 numa = cpu_to_node(fd->rec_cpu_num);
964 else
965 numa = numa_node_id();
966 uctxt = hfi1_create_ctxtdata(dd->pport, ctxt, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400967 if (!uctxt) {
968 dd_dev_err(dd,
969 "Unable to allocate ctxtdata memory, failing open\n");
970 return -ENOMEM;
971 }
Mitko Haralanov957558c2016-02-03 14:33:40 -0800972 hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)",
973 uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num,
974 uctxt->numa_id);
975
Mike Marciniszyn77241052015-07-30 15:17:43 -0400976 /*
977 * Allocate and enable a PIO send context.
978 */
979 uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize,
Mitko Haralanovcc572362016-02-03 14:33:49 -0800980 uctxt->dd->node);
Jakub Pawlak3a6982d2016-09-25 07:42:23 -0700981 if (!uctxt->sc) {
982 ret = -ENOMEM;
983 goto ctxdata_free;
984 }
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500985 hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index,
986 uctxt->sc->hw_context);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400987 ret = sc_enable(uctxt->sc);
988 if (ret)
Jakub Pawlak3a6982d2016-09-25 07:42:23 -0700989 goto ctxdata_free;
990
Mike Marciniszyn77241052015-07-30 15:17:43 -0400991 /*
992 * Setup shared context resources if the user-level has requested
993 * shared contexts and this is the 'master' process.
994 * This has to be done here so the rest of the sub-contexts find the
995 * proper master.
996 */
Ira Weiny9e10af42015-10-30 18:58:40 -0400997 if (uinfo->subctxt_cnt && !fd->subctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400998 ret = init_subctxts(uctxt, uinfo);
999 /*
1000 * On error, we don't need to disable and de-allocate the
1001 * send context because it will be done during file close
1002 */
1003 if (ret)
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001004 goto ctxdata_free;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001005 }
1006 uctxt->userversion = uinfo->userversion;
Dean Luickbdf77522016-07-28 15:21:13 -04001007 uctxt->flags = hfi1_cap_mask; /* save current flag state */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001008 init_waitqueue_head(&uctxt->wait);
1009 strlcpy(uctxt->comm, current->comm, sizeof(uctxt->comm));
1010 memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid));
1011 uctxt->jkey = generate_jkey(current_uid());
1012 INIT_LIST_HEAD(&uctxt->sdma_queues);
1013 spin_lock_init(&uctxt->sdma_qlock);
1014 hfi1_stats.sps_ctxts++;
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -08001015 /*
1016 * Disable ASPM when there are open user/PSM contexts to avoid
1017 * issues with ASPM L1 exit latency
1018 */
1019 if (dd->freectxts-- == dd->num_user_contexts)
1020 aspm_disable_all(dd);
Ira Weiny9e10af42015-10-30 18:58:40 -04001021 fd->uctxt = uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001022
1023 return 0;
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001024
1025ctxdata_free:
1026 dd->rcd[ctxt] = NULL;
1027 hfi1_free_ctxtdata(dd, uctxt);
1028 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001029}
1030
1031static int init_subctxts(struct hfi1_ctxtdata *uctxt,
1032 const struct hfi1_user_info *uinfo)
1033{
Mike Marciniszyn77241052015-07-30 15:17:43 -04001034 unsigned num_subctxts;
1035
1036 num_subctxts = uinfo->subctxt_cnt;
Mitko Haralanovacac10f2016-02-05 11:57:50 -05001037 if (num_subctxts > HFI1_MAX_SHARED_CTXTS)
1038 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001039
1040 uctxt->subctxt_cnt = uinfo->subctxt_cnt;
1041 uctxt->subctxt_id = uinfo->subctxt_id;
1042 uctxt->active_slaves = 1;
1043 uctxt->redirect_seq_cnt = 1;
1044 set_bit(HFI1_CTXT_MASTER_UNINIT, &uctxt->event_flags);
Mitko Haralanovacac10f2016-02-05 11:57:50 -05001045
1046 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001047}
1048
1049static int setup_subctxt(struct hfi1_ctxtdata *uctxt)
1050{
1051 int ret = 0;
1052 unsigned num_subctxts = uctxt->subctxt_cnt;
1053
1054 uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE);
1055 if (!uctxt->subctxt_uregbase) {
1056 ret = -ENOMEM;
1057 goto bail;
1058 }
1059 /* We can take the size of the RcvHdr Queue from the master */
1060 uctxt->subctxt_rcvhdr_base = vmalloc_user(uctxt->rcvhdrq_size *
1061 num_subctxts);
1062 if (!uctxt->subctxt_rcvhdr_base) {
1063 ret = -ENOMEM;
1064 goto bail_ureg;
1065 }
1066
1067 uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size *
1068 num_subctxts);
1069 if (!uctxt->subctxt_rcvegrbuf) {
1070 ret = -ENOMEM;
1071 goto bail_rhdr;
1072 }
1073 goto bail;
1074bail_rhdr:
1075 vfree(uctxt->subctxt_rcvhdr_base);
1076bail_ureg:
1077 vfree(uctxt->subctxt_uregbase);
1078 uctxt->subctxt_uregbase = NULL;
1079bail:
1080 return ret;
1081}
1082
1083static int user_init(struct file *fp)
1084{
Mike Marciniszyn77241052015-07-30 15:17:43 -04001085 unsigned int rcvctrl_ops = 0;
Ira Weiny9e10af42015-10-30 18:58:40 -04001086 struct hfi1_filedata *fd = fp->private_data;
1087 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001088
1089 /* make sure that the context has already been setup */
Mitko Haralanov94158442016-04-20 06:05:36 -07001090 if (!test_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags))
1091 return -EFAULT;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001092
1093 /* initialize poll variables... */
1094 uctxt->urgent = 0;
1095 uctxt->urgent_poll = 0;
1096
1097 /*
1098 * Now enable the ctxt for receive.
1099 * For chips that are set to DMA the tail register to memory
1100 * when they change (and when the update bit transitions from
1101 * 0 to 1. So for those chips, we turn it off and then back on.
1102 * This will (very briefly) affect any other open ctxts, but the
1103 * duration is very short, and therefore isn't an issue. We
1104 * explicitly set the in-memory tail copy to 0 beforehand, so we
1105 * don't have to wait to be sure the DMA update has happened
1106 * (chip resets head/tail to 0 on transition to enable).
1107 */
1108 if (uctxt->rcvhdrtail_kvaddr)
1109 clear_rcvhdrtail(uctxt);
1110
1111 /* Setup J_KEY before enabling the context */
1112 hfi1_set_ctxt_jkey(uctxt->dd, uctxt->ctxt, uctxt->jkey);
1113
1114 rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB;
Dean Luickbdf77522016-07-28 15:21:13 -04001115 if (HFI1_CAP_UGET_MASK(uctxt->flags, HDRSUPP))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001116 rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB;
1117 /*
1118 * Ignore the bit in the flags for now until proper
1119 * support for multiple packet per rcv array entry is
1120 * added.
1121 */
Dean Luickbdf77522016-07-28 15:21:13 -04001122 if (!HFI1_CAP_UGET_MASK(uctxt->flags, MULTI_PKT_EGR))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001123 rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
Dean Luickbdf77522016-07-28 15:21:13 -04001124 if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_EGR_FULL))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001125 rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
Dean Luickbdf77522016-07-28 15:21:13 -04001126 if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_RHQ_FULL))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001127 rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
Mitko Haralanov566c1572016-02-03 14:32:49 -08001128 /*
1129 * The RcvCtxtCtrl.TailUpd bit has to be explicitly written.
1130 * We can't rely on the correct value to be set from prior
1131 * uses of the chip or ctxt. Therefore, add the rcvctrl op
1132 * for both cases.
1133 */
Dean Luickbdf77522016-07-28 15:21:13 -04001134 if (HFI1_CAP_UGET_MASK(uctxt->flags, DMA_RTAIL))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001135 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB;
Mitko Haralanov566c1572016-02-03 14:32:49 -08001136 else
1137 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001138 hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt->ctxt);
1139
1140 /* Notify any waiting slaves */
1141 if (uctxt->subctxt_cnt) {
1142 clear_bit(HFI1_CTXT_MASTER_UNINIT, &uctxt->event_flags);
1143 wake_up(&uctxt->wait);
1144 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001145
Mitko Haralanov94158442016-04-20 06:05:36 -07001146 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001147}
1148
1149static int get_ctxt_info(struct file *fp, void __user *ubase, __u32 len)
1150{
1151 struct hfi1_ctxt_info cinfo;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001152 struct hfi1_filedata *fd = fp->private_data;
Ira Weiny9e10af42015-10-30 18:58:40 -04001153 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001154 int ret = 0;
1155
Dan Carpenterebe6b2e2015-09-16 09:42:25 +03001156 memset(&cinfo, 0, sizeof(cinfo));
Dean Luickbdf77522016-07-28 15:21:13 -04001157 cinfo.runtime_flags = (((uctxt->flags >> HFI1_CAP_MISC_SHIFT) &
1158 HFI1_CAP_MISC_MASK) << HFI1_CAP_USER_SHIFT) |
1159 HFI1_CAP_UGET_MASK(uctxt->flags, MASK) |
1160 HFI1_CAP_KGET_MASK(uctxt->flags, K2U);
Dean Luick622c2022016-07-28 15:21:21 -04001161 /* adjust flag if this fd is not able to cache */
1162 if (!fd->handler)
1163 cinfo.runtime_flags |= HFI1_CAP_TID_UNMAP; /* no caching */
1164
Mike Marciniszyn77241052015-07-30 15:17:43 -04001165 cinfo.num_active = hfi1_count_active_units();
1166 cinfo.unit = uctxt->dd->unit;
1167 cinfo.ctxt = uctxt->ctxt;
Ira Weiny9e10af42015-10-30 18:58:40 -04001168 cinfo.subctxt = fd->subctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001169 cinfo.rcvtids = roundup(uctxt->egrbufs.alloced,
1170 uctxt->dd->rcv_entries.group_size) +
1171 uctxt->expected_count;
1172 cinfo.credits = uctxt->sc->credits;
1173 cinfo.numa_node = uctxt->numa_id;
1174 cinfo.rec_cpu = fd->rec_cpu_num;
1175 cinfo.send_ctxt = uctxt->sc->hw_context;
1176
1177 cinfo.egrtids = uctxt->egrbufs.alloced;
1178 cinfo.rcvhdrq_cnt = uctxt->rcvhdrq_cnt;
1179 cinfo.rcvhdrq_entsize = uctxt->rcvhdrqentsize << 2;
Ira Weiny9e10af42015-10-30 18:58:40 -04001180 cinfo.sdma_ring_size = fd->cq->nentries;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001181 cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size;
1182
Ira Weiny9e10af42015-10-30 18:58:40 -04001183 trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, cinfo);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001184 if (copy_to_user(ubase, &cinfo, sizeof(cinfo)))
1185 ret = -EFAULT;
Dean Luickbdf77522016-07-28 15:21:13 -04001186
Mike Marciniszyn77241052015-07-30 15:17:43 -04001187 return ret;
1188}
1189
1190static int setup_ctxt(struct file *fp)
1191{
Ira Weiny9e10af42015-10-30 18:58:40 -04001192 struct hfi1_filedata *fd = fp->private_data;
1193 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001194 struct hfi1_devdata *dd = uctxt->dd;
1195 int ret = 0;
1196
1197 /*
Mitko Haralanov94158442016-04-20 06:05:36 -07001198 * Context should be set up only once, including allocation and
Mike Marciniszyn77241052015-07-30 15:17:43 -04001199 * programming of eager buffers. This is done if context sharing
1200 * is not requested or by the master process.
1201 */
Ira Weiny9e10af42015-10-30 18:58:40 -04001202 if (!uctxt->subctxt_cnt || !fd->subctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001203 ret = hfi1_init_ctxt(uctxt->sc);
1204 if (ret)
1205 goto done;
1206
1207 /* Now allocate the RcvHdr queue and eager buffers. */
1208 ret = hfi1_create_rcvhdrq(dd, uctxt);
1209 if (ret)
1210 goto done;
1211 ret = hfi1_setup_eagerbufs(uctxt);
1212 if (ret)
1213 goto done;
Ira Weiny9e10af42015-10-30 18:58:40 -04001214 if (uctxt->subctxt_cnt && !fd->subctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001215 ret = setup_subctxt(uctxt);
1216 if (ret)
1217 goto done;
1218 }
Mitko Haralanov94158442016-04-20 06:05:36 -07001219 } else {
1220 ret = wait_event_interruptible(uctxt->wait, !test_bit(
1221 HFI1_CTXT_MASTER_UNINIT,
1222 &uctxt->event_flags));
1223 if (ret)
1224 goto done;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001225 }
Mitko Haralanov94158442016-04-20 06:05:36 -07001226
Mike Marciniszyn77241052015-07-30 15:17:43 -04001227 ret = hfi1_user_sdma_alloc_queues(uctxt, fp);
1228 if (ret)
1229 goto done;
Mitko Haralanov94158442016-04-20 06:05:36 -07001230 /*
1231 * Expected receive has to be setup for all processes (including
1232 * shared contexts). However, it has to be done after the master
1233 * context has been fully configured as it depends on the
1234 * eager/expected split of the RcvArray entries.
1235 * Setting it up here ensures that the subcontexts will be waiting
1236 * (due to the above wait_event_interruptible() until the master
1237 * is setup.
1238 */
1239 ret = hfi1_user_exp_rcv_init(fp);
1240 if (ret)
1241 goto done;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001242
1243 set_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags);
1244done:
1245 return ret;
1246}
1247
1248static int get_base_info(struct file *fp, void __user *ubase, __u32 len)
1249{
1250 struct hfi1_base_info binfo;
Ira Weiny9e10af42015-10-30 18:58:40 -04001251 struct hfi1_filedata *fd = fp->private_data;
1252 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001253 struct hfi1_devdata *dd = uctxt->dd;
1254 ssize_t sz;
1255 unsigned offset;
1256 int ret = 0;
1257
1258 trace_hfi1_uctxtdata(uctxt->dd, uctxt);
1259
1260 memset(&binfo, 0, sizeof(binfo));
1261 binfo.hw_version = dd->revision;
1262 binfo.sw_version = HFI1_KERN_SWVERSION;
1263 binfo.bthqp = kdeth_qp;
1264 binfo.jkey = uctxt->jkey;
1265 /*
1266 * If more than 64 contexts are enabled the allocated credit
1267 * return will span two or three contiguous pages. Since we only
1268 * map the page containing the context's credit return address,
1269 * we need to calculate the offset in the proper page.
1270 */
1271 offset = ((u64)uctxt->sc->hw_free -
1272 (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE;
1273 binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001274 fd->subctxt, offset);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001275 binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001276 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001277 uctxt->sc->base_addr);
1278 binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP,
1279 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001280 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001281 uctxt->sc->base_addr);
1282 binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001283 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001284 uctxt->rcvhdrq);
1285 binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001286 fd->subctxt,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001287 uctxt->egrbufs.rcvtids[0].dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001288 binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001289 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001290 /*
1291 * user regs are at
1292 * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE))
1293 */
1294 binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001295 fd->subctxt, 0);
Geliang Tange260e402015-10-03 10:34:59 +08001296 offset = offset_in_page((((uctxt->ctxt - dd->first_user_ctxt) *
Ira Weiny9e10af42015-10-30 18:58:40 -04001297 HFI1_MAX_SHARED_CTXTS) + fd->subctxt) *
Geliang Tange260e402015-10-03 10:34:59 +08001298 sizeof(*dd->events));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001299 binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001300 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001301 offset);
1302 binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001303 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001304 dd->status);
1305 if (HFI1_CAP_IS_USET(DMA_RTAIL))
1306 binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001307 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001308 if (uctxt->subctxt_cnt) {
1309 binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS,
1310 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001311 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001312 binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ,
1313 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001314 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001315 binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF,
1316 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001317 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001318 }
1319 sz = (len < sizeof(binfo)) ? len : sizeof(binfo);
1320 if (copy_to_user(ubase, &binfo, sz))
1321 ret = -EFAULT;
1322 return ret;
1323}
1324
1325static unsigned int poll_urgent(struct file *fp,
1326 struct poll_table_struct *pt)
1327{
Ira Weiny9e10af42015-10-30 18:58:40 -04001328 struct hfi1_filedata *fd = fp->private_data;
1329 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001330 struct hfi1_devdata *dd = uctxt->dd;
1331 unsigned pollflag;
1332
1333 poll_wait(fp, &uctxt->wait, pt);
1334
1335 spin_lock_irq(&dd->uctxt_lock);
1336 if (uctxt->urgent != uctxt->urgent_poll) {
1337 pollflag = POLLIN | POLLRDNORM;
1338 uctxt->urgent_poll = uctxt->urgent;
1339 } else {
1340 pollflag = 0;
1341 set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags);
1342 }
1343 spin_unlock_irq(&dd->uctxt_lock);
1344
1345 return pollflag;
1346}
1347
1348static unsigned int poll_next(struct file *fp,
1349 struct poll_table_struct *pt)
1350{
Ira Weiny9e10af42015-10-30 18:58:40 -04001351 struct hfi1_filedata *fd = fp->private_data;
1352 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001353 struct hfi1_devdata *dd = uctxt->dd;
1354 unsigned pollflag;
1355
1356 poll_wait(fp, &uctxt->wait, pt);
1357
1358 spin_lock_irq(&dd->uctxt_lock);
1359 if (hdrqempty(uctxt)) {
1360 set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags);
1361 hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt->ctxt);
1362 pollflag = 0;
Jubin Johne4909742016-02-14 20:22:00 -08001363 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001364 pollflag = POLLIN | POLLRDNORM;
Jubin Johne4909742016-02-14 20:22:00 -08001365 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001366 spin_unlock_irq(&dd->uctxt_lock);
1367
1368 return pollflag;
1369}
1370
1371/*
1372 * Find all user contexts in use, and set the specified bit in their
1373 * event mask.
1374 * See also find_ctxt() for a similar use, that is specific to send buffers.
1375 */
1376int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit)
1377{
1378 struct hfi1_ctxtdata *uctxt;
1379 struct hfi1_devdata *dd = ppd->dd;
1380 unsigned ctxt;
1381 int ret = 0;
1382 unsigned long flags;
1383
1384 if (!dd->events) {
1385 ret = -EINVAL;
1386 goto done;
1387 }
1388
1389 spin_lock_irqsave(&dd->uctxt_lock, flags);
1390 for (ctxt = dd->first_user_ctxt; ctxt < dd->num_rcv_contexts;
1391 ctxt++) {
1392 uctxt = dd->rcd[ctxt];
1393 if (uctxt) {
1394 unsigned long *evs = dd->events +
1395 (uctxt->ctxt - dd->first_user_ctxt) *
1396 HFI1_MAX_SHARED_CTXTS;
1397 int i;
1398 /*
1399 * subctxt_cnt is 0 if not shared, so do base
1400 * separately, first, then remaining subctxt, if any
1401 */
1402 set_bit(evtbit, evs);
1403 for (i = 1; i < uctxt->subctxt_cnt; i++)
1404 set_bit(evtbit, evs + i);
1405 }
1406 }
1407 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1408done:
1409 return ret;
1410}
1411
1412/**
1413 * manage_rcvq - manage a context's receive queue
1414 * @uctxt: the context
1415 * @subctxt: the sub-context
1416 * @start_stop: action to carry out
1417 *
1418 * start_stop == 0 disables receive on the context, for use in queue
1419 * overflow conditions. start_stop==1 re-enables, to be used to
1420 * re-init the software copy of the head register
1421 */
1422static int manage_rcvq(struct hfi1_ctxtdata *uctxt, unsigned subctxt,
1423 int start_stop)
1424{
1425 struct hfi1_devdata *dd = uctxt->dd;
1426 unsigned int rcvctrl_op;
1427
1428 if (subctxt)
1429 goto bail;
1430 /* atomically clear receive enable ctxt. */
1431 if (start_stop) {
1432 /*
1433 * On enable, force in-memory copy of the tail register to
1434 * 0, so that protocol code doesn't have to worry about
1435 * whether or not the chip has yet updated the in-memory
1436 * copy or not on return from the system call. The chip
1437 * always resets it's tail register back to 0 on a
1438 * transition from disabled to enabled.
1439 */
1440 if (uctxt->rcvhdrtail_kvaddr)
1441 clear_rcvhdrtail(uctxt);
1442 rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB;
Jubin Johne4909742016-02-14 20:22:00 -08001443 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001444 rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS;
Jubin Johne4909742016-02-14 20:22:00 -08001445 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001446 hfi1_rcvctrl(dd, rcvctrl_op, uctxt->ctxt);
1447 /* always; new head should be equal to new tail; see above */
1448bail:
1449 return 0;
1450}
1451
1452/*
1453 * clear the event notifier events for this context.
1454 * User process then performs actions appropriate to bit having been
1455 * set, if desired, and checks again in future.
1456 */
1457static int user_event_ack(struct hfi1_ctxtdata *uctxt, int subctxt,
1458 unsigned long events)
1459{
1460 int i;
1461 struct hfi1_devdata *dd = uctxt->dd;
1462 unsigned long *evs;
1463
1464 if (!dd->events)
1465 return 0;
1466
1467 evs = dd->events + ((uctxt->ctxt - dd->first_user_ctxt) *
1468 HFI1_MAX_SHARED_CTXTS) + subctxt;
1469
1470 for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) {
1471 if (!test_bit(i, &events))
1472 continue;
1473 clear_bit(i, evs);
1474 }
1475 return 0;
1476}
1477
Mike Marciniszyn77241052015-07-30 15:17:43 -04001478static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned subctxt,
1479 u16 pkey)
1480{
1481 int ret = -ENOENT, i, intable = 0;
1482 struct hfi1_pportdata *ppd = uctxt->ppd;
1483 struct hfi1_devdata *dd = uctxt->dd;
1484
1485 if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY) {
1486 ret = -EINVAL;
1487 goto done;
1488 }
1489
1490 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++)
1491 if (pkey == ppd->pkeys[i]) {
1492 intable = 1;
1493 break;
1494 }
1495
1496 if (intable)
1497 ret = hfi1_set_ctxt_pkey(dd, uctxt->ctxt, pkey);
1498done:
1499 return ret;
1500}
1501
Mike Marciniszyn77241052015-07-30 15:17:43 -04001502static void user_remove(struct hfi1_devdata *dd)
1503{
Mike Marciniszyn77241052015-07-30 15:17:43 -04001504
1505 hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001506}
1507
1508static int user_add(struct hfi1_devdata *dd)
1509{
1510 char name[10];
1511 int ret;
1512
Mike Marciniszyn77241052015-07-30 15:17:43 -04001513 snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit);
Dennis Dalessandro0eb62652016-05-19 05:25:50 -07001514 ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops,
Ira Weinye116a642015-09-17 13:47:49 -04001515 &dd->user_cdev, &dd->user_device,
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001516 true, &dd->kobj);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001517 if (ret)
Dennis Dalessandro7312f292016-05-19 05:25:57 -07001518 user_remove(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001519
Mike Marciniszyn77241052015-07-30 15:17:43 -04001520 return ret;
1521}
1522
1523/*
1524 * Create per-unit files in /dev
1525 */
1526int hfi1_device_create(struct hfi1_devdata *dd)
1527{
Dennis Dalessandro0f7b1f92016-05-19 05:26:10 -07001528 return user_add(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001529}
1530
1531/*
1532 * Remove per-unit files in /dev
1533 * void, core kernel returns no errors for this stuff
1534 */
1535void hfi1_device_remove(struct hfi1_devdata *dd)
1536{
1537 user_remove(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001538}