blob: 5a624401b817d485d2ac51783def51be6ff059e1 [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07002 * Copyright(c) 2015-2017 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>
Ingo Molnar6e84f312017-02-08 18:51:29 +010051#include <linux/sched/mm.h>
Mike Marciniszyn77241052015-07-30 15:17:43 -040052
Jason Gunthorpee6bd18f2016-04-10 19:13:13 -060053#include <rdma/ib.h>
54
Mike Marciniszyn77241052015-07-30 15:17:43 -040055#include "hfi.h"
56#include "pio.h"
57#include "device.h"
58#include "common.h"
59#include "trace.h"
60#include "user_sdma.h"
Mitko Haralanov701e4412015-10-30 18:58:43 -040061#include "user_exp_rcv.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 */
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070073static int hfi1_file_open(struct inode *inode, struct file *fp);
74static int hfi1_file_close(struct inode *inode, struct file *fp);
75static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from);
76static unsigned int hfi1_poll(struct file *fp, struct poll_table_struct *pt);
77static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma);
Mike Marciniszyn77241052015-07-30 15:17:43 -040078
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070079static u64 kvirt_to_phys(void *addr);
Michael J. Ruhl5fbded42017-05-04 05:14:57 -070080static int assign_ctxt(struct hfi1_filedata *fd, struct hfi1_user_info *uinfo);
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070081static int init_subctxts(struct hfi1_ctxtdata *uctxt,
82 const struct hfi1_user_info *uinfo);
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -070083static int user_init(struct hfi1_filedata *fd);
84static int get_ctxt_info(struct hfi1_filedata *fd, void __user *ubase,
85 __u32 len);
86static int get_base_info(struct hfi1_filedata *fd, void __user *ubase,
87 __u32 len);
88static int setup_ctxt(struct hfi1_filedata *fd);
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070089static int setup_subctxt(struct hfi1_ctxtdata *uctxt);
Michael J. Ruhl5fbded42017-05-04 05:14:57 -070090
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -070091static int find_shared_ctxt(struct hfi1_filedata *fd,
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070092 const struct hfi1_user_info *uinfo);
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -070093static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070094 struct hfi1_user_info *uinfo);
95static unsigned int poll_urgent(struct file *fp, struct poll_table_struct *pt);
96static unsigned int poll_next(struct file *fp, struct poll_table_struct *pt);
97static int user_event_ack(struct hfi1_ctxtdata *uctxt, int subctxt,
98 unsigned long events);
99static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned subctxt,
100 u16 pkey);
101static int manage_rcvq(struct hfi1_ctxtdata *uctxt, unsigned subctxt,
102 int start_stop);
103static int vma_fault(struct vm_fault *vmf);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700104static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
105 unsigned long arg);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400106
107static const struct file_operations hfi1_file_ops = {
108 .owner = THIS_MODULE,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400109 .write_iter = hfi1_write_iter,
110 .open = hfi1_file_open,
111 .release = hfi1_file_close,
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700112 .unlocked_ioctl = hfi1_file_ioctl,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400113 .poll = hfi1_poll,
114 .mmap = hfi1_file_mmap,
115 .llseek = noop_llseek,
116};
117
118static struct vm_operations_struct vm_ops = {
119 .fault = vma_fault,
120};
121
122/*
123 * Types of memories mapped into user processes' space
124 */
125enum mmap_types {
126 PIO_BUFS = 1,
127 PIO_BUFS_SOP,
128 PIO_CRED,
129 RCV_HDRQ,
130 RCV_EGRBUF,
131 UREGS,
132 EVENTS,
133 STATUS,
134 RTAIL,
135 SUBCTXT_UREGS,
136 SUBCTXT_RCV_HDRQ,
137 SUBCTXT_EGRBUF,
138 SDMA_COMP
139};
140
141/*
142 * Masks and offsets defining the mmap tokens
143 */
144#define HFI1_MMAP_OFFSET_MASK 0xfffULL
145#define HFI1_MMAP_OFFSET_SHIFT 0
146#define HFI1_MMAP_SUBCTXT_MASK 0xfULL
147#define HFI1_MMAP_SUBCTXT_SHIFT 12
148#define HFI1_MMAP_CTXT_MASK 0xffULL
149#define HFI1_MMAP_CTXT_SHIFT 16
150#define HFI1_MMAP_TYPE_MASK 0xfULL
151#define HFI1_MMAP_TYPE_SHIFT 24
152#define HFI1_MMAP_MAGIC_MASK 0xffffffffULL
153#define HFI1_MMAP_MAGIC_SHIFT 32
154
155#define HFI1_MMAP_MAGIC 0xdabbad00
156
157#define HFI1_MMAP_TOKEN_SET(field, val) \
158 (((val) & HFI1_MMAP_##field##_MASK) << HFI1_MMAP_##field##_SHIFT)
159#define HFI1_MMAP_TOKEN_GET(field, token) \
160 (((token) >> HFI1_MMAP_##field##_SHIFT) & HFI1_MMAP_##field##_MASK)
161#define HFI1_MMAP_TOKEN(type, ctxt, subctxt, addr) \
162 (HFI1_MMAP_TOKEN_SET(MAGIC, HFI1_MMAP_MAGIC) | \
163 HFI1_MMAP_TOKEN_SET(TYPE, type) | \
164 HFI1_MMAP_TOKEN_SET(CTXT, ctxt) | \
165 HFI1_MMAP_TOKEN_SET(SUBCTXT, subctxt) | \
Geliang Tange260e402015-10-03 10:34:59 +0800166 HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr))))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400167
Mike Marciniszyn77241052015-07-30 15:17:43 -0400168#define dbg(fmt, ...) \
169 pr_info(fmt, ##__VA_ARGS__)
170
Mike Marciniszyn77241052015-07-30 15:17:43 -0400171static inline int is_valid_mmap(u64 token)
172{
173 return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC);
174}
175
176static int hfi1_file_open(struct inode *inode, struct file *fp)
177{
Ira Weinyea3a0ee2016-07-28 12:27:35 -0400178 struct hfi1_filedata *fd;
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700179 struct hfi1_devdata *dd = container_of(inode->i_cdev,
180 struct hfi1_devdata,
181 user_cdev);
182
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700183 if (!((dd->flags & HFI1_PRESENT) && dd->kregbase))
184 return -EINVAL;
185
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -0700186 if (!atomic_inc_not_zero(&dd->user_refcount))
187 return -ENXIO;
188
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700189 /* Just take a ref now. Not all opens result in a context assign */
190 kobject_get(&dd->kobj);
191
Mike Marciniszyn77241052015-07-30 15:17:43 -0400192 /* The real work is performed later in assign_ctxt() */
Ira Weinyea3a0ee2016-07-28 12:27:35 -0400193
194 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
195
Ira Weiny3faa3d92016-07-28 15:21:19 -0400196 if (fd) {
197 fd->rec_cpu_num = -1; /* no cpu affinity by default */
198 fd->mm = current->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -0800199 mmgrab(fd->mm);
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700200 fd->dd = dd;
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -0700201 fp->private_data = fd;
202 } else {
203 fp->private_data = NULL;
204
205 if (atomic_dec_and_test(&dd->user_refcount))
206 complete(&dd->user_comp);
207
208 return -ENOMEM;
Ira Weiny3faa3d92016-07-28 15:21:19 -0400209 }
Ira Weinyea3a0ee2016-07-28 12:27:35 -0400210
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -0700211 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400212}
213
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700214static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
215 unsigned long arg)
216{
217 struct hfi1_filedata *fd = fp->private_data;
218 struct hfi1_ctxtdata *uctxt = fd->uctxt;
219 struct hfi1_user_info uinfo;
220 struct hfi1_tid_info tinfo;
221 int ret = 0;
222 unsigned long addr;
223 int uval = 0;
224 unsigned long ul_uval = 0;
225 u16 uval16 = 0;
226
Dennis Dalessandro8a1882e2016-05-19 05:26:37 -0700227 hfi1_cdbg(IOCTL, "IOCTL recv: 0x%x", cmd);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700228 if (cmd != HFI1_IOCTL_ASSIGN_CTXT &&
229 cmd != HFI1_IOCTL_GET_VERS &&
230 !uctxt)
231 return -EINVAL;
232
233 switch (cmd) {
234 case HFI1_IOCTL_ASSIGN_CTXT:
Ira Weinyca2f30a2016-06-09 07:51:33 -0700235 if (uctxt)
236 return -EINVAL;
237
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700238 if (copy_from_user(&uinfo,
239 (struct hfi1_user_info __user *)arg,
240 sizeof(uinfo)))
241 return -EFAULT;
242
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700243 ret = assign_ctxt(fd, &uinfo);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700244 if (ret < 0)
245 return ret;
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700246 ret = setup_ctxt(fd);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700247 if (ret)
248 return ret;
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700249 ret = user_init(fd);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700250 break;
251 case HFI1_IOCTL_CTXT_INFO:
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700252 ret = get_ctxt_info(fd, (void __user *)(unsigned long)arg,
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700253 sizeof(struct hfi1_ctxt_info));
254 break;
255 case HFI1_IOCTL_USER_INFO:
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700256 ret = get_base_info(fd, (void __user *)(unsigned long)arg,
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700257 sizeof(struct hfi1_base_info));
258 break;
259 case HFI1_IOCTL_CREDIT_UPD:
Markus Elfringf7ca5352016-07-23 08:30:52 +0200260 if (uctxt)
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700261 sc_return_credits(uctxt->sc);
262 break;
263
264 case HFI1_IOCTL_TID_UPDATE:
265 if (copy_from_user(&tinfo,
266 (struct hfi11_tid_info __user *)arg,
267 sizeof(tinfo)))
268 return -EFAULT;
269
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700270 ret = hfi1_user_exp_rcv_setup(fd, &tinfo);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700271 if (!ret) {
272 /*
273 * Copy the number of tidlist entries we used
274 * and the length of the buffer we registered.
275 * These fields are adjacent in the structure so
276 * we can copy them at the same time.
277 */
278 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
279 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
280 sizeof(tinfo.tidcnt) +
281 sizeof(tinfo.length)))
282 ret = -EFAULT;
283 }
284 break;
285
286 case HFI1_IOCTL_TID_FREE:
287 if (copy_from_user(&tinfo,
288 (struct hfi11_tid_info __user *)arg,
289 sizeof(tinfo)))
290 return -EFAULT;
291
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700292 ret = hfi1_user_exp_rcv_clear(fd, &tinfo);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700293 if (ret)
294 break;
295 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
296 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
297 sizeof(tinfo.tidcnt)))
298 ret = -EFAULT;
299 break;
300
301 case HFI1_IOCTL_TID_INVAL_READ:
302 if (copy_from_user(&tinfo,
303 (struct hfi11_tid_info __user *)arg,
304 sizeof(tinfo)))
305 return -EFAULT;
306
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700307 ret = hfi1_user_exp_rcv_invalid(fd, &tinfo);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700308 if (ret)
309 break;
310 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
311 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
312 sizeof(tinfo.tidcnt)))
313 ret = -EFAULT;
314 break;
315
316 case HFI1_IOCTL_RECV_CTRL:
317 ret = get_user(uval, (int __user *)arg);
318 if (ret != 0)
319 return -EFAULT;
320 ret = manage_rcvq(uctxt, fd->subctxt, uval);
321 break;
322
323 case HFI1_IOCTL_POLL_TYPE:
324 ret = get_user(uval, (int __user *)arg);
325 if (ret != 0)
326 return -EFAULT;
327 uctxt->poll_type = (typeof(uctxt->poll_type))uval;
328 break;
329
330 case HFI1_IOCTL_ACK_EVENT:
331 ret = get_user(ul_uval, (unsigned long __user *)arg);
332 if (ret != 0)
333 return -EFAULT;
334 ret = user_event_ack(uctxt, fd->subctxt, ul_uval);
335 break;
336
337 case HFI1_IOCTL_SET_PKEY:
338 ret = get_user(uval16, (u16 __user *)arg);
339 if (ret != 0)
340 return -EFAULT;
341 if (HFI1_CAP_IS_USET(PKEY_CHECK))
342 ret = set_ctxt_pkey(uctxt, fd->subctxt, uval16);
343 else
344 return -EPERM;
345 break;
346
347 case HFI1_IOCTL_CTXT_RESET: {
348 struct send_context *sc;
349 struct hfi1_devdata *dd;
350
351 if (!uctxt || !uctxt->dd || !uctxt->sc)
352 return -EINVAL;
353
354 /*
355 * There is no protection here. User level has to
356 * guarantee that no one will be writing to the send
357 * context while it is being re-initialized.
358 * If user level breaks that guarantee, it will break
359 * it's own context and no one else's.
360 */
361 dd = uctxt->dd;
362 sc = uctxt->sc;
363 /*
364 * Wait until the interrupt handler has marked the
365 * context as halted or frozen. Report error if we time
366 * out.
367 */
368 wait_event_interruptible_timeout(
369 sc->halt_wait, (sc->flags & SCF_HALTED),
370 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
371 if (!(sc->flags & SCF_HALTED))
372 return -ENOLCK;
373
374 /*
375 * If the send context was halted due to a Freeze,
376 * wait until the device has been "unfrozen" before
377 * resetting the context.
378 */
379 if (sc->flags & SCF_FROZEN) {
380 wait_event_interruptible_timeout(
381 dd->event_queue,
382 !(ACCESS_ONCE(dd->flags) & HFI1_FROZEN),
383 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
384 if (dd->flags & HFI1_FROZEN)
385 return -ENOLCK;
386
387 if (dd->flags & HFI1_FORCED_FREEZE)
388 /*
389 * Don't allow context reset if we are into
390 * forced freeze
391 */
392 return -ENODEV;
393
394 sc_disable(sc);
395 ret = sc_enable(sc);
396 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB,
397 uctxt->ctxt);
398 } else {
399 ret = sc_restart(sc);
400 }
401 if (!ret)
402 sc_return_credits(sc);
403 break;
404 }
405
406 case HFI1_IOCTL_GET_VERS:
407 uval = HFI1_USER_SWVERSION;
408 if (put_user(uval, (int __user *)arg))
409 return -EFAULT;
410 break;
411
412 default:
413 return -EINVAL;
414 }
415
416 return ret;
417}
418
Mike Marciniszyn77241052015-07-30 15:17:43 -0400419static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from)
420{
Ira Weiny9e10af42015-10-30 18:58:40 -0400421 struct hfi1_filedata *fd = kiocb->ki_filp->private_data;
422 struct hfi1_user_sdma_pkt_q *pq = fd->pq;
423 struct hfi1_user_sdma_comp_q *cq = fd->cq;
Ira Weiny0904f322016-07-01 16:00:55 -0700424 int done = 0, reqs = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400425 unsigned long dim = from->nr_segs;
426
Ira Weiny0904f322016-07-01 16:00:55 -0700427 if (!cq || !pq)
428 return -EIO;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400429
Ira Weiny0904f322016-07-01 16:00:55 -0700430 if (!iter_is_iovec(from) || !dim)
431 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400432
433 hfi1_cdbg(SDMA, "SDMA request from %u:%u (%lu)",
Ira Weiny9e10af42015-10-30 18:58:40 -0400434 fd->uctxt->ctxt, fd->subctxt, dim);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400435
Ira Weiny0904f322016-07-01 16:00:55 -0700436 if (atomic_read(&pq->n_reqs) == pq->n_max_reqs)
437 return -ENOSPC;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400438
439 while (dim) {
Ira Weiny0904f322016-07-01 16:00:55 -0700440 int ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400441 unsigned long count = 0;
442
443 ret = hfi1_user_sdma_process_request(
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700444 fd, (struct iovec *)(from->iov + done),
Mike Marciniszyn77241052015-07-30 15:17:43 -0400445 dim, &count);
Ira Weiny0904f322016-07-01 16:00:55 -0700446 if (ret) {
447 reqs = ret;
448 break;
449 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400450 dim -= count;
451 done += count;
452 reqs++;
453 }
Ira Weiny0904f322016-07-01 16:00:55 -0700454
455 return reqs;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400456}
457
458static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma)
459{
Ira Weiny9e10af42015-10-30 18:58:40 -0400460 struct hfi1_filedata *fd = fp->private_data;
461 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400462 struct hfi1_devdata *dd;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700463 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400464 u64 token = vma->vm_pgoff << PAGE_SHIFT,
465 memaddr = 0;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700466 void *memvirt = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400467 u8 subctxt, mapio = 0, vmf = 0, type;
468 ssize_t memlen = 0;
469 int ret = 0;
470 u16 ctxt;
471
Mike Marciniszyn77241052015-07-30 15:17:43 -0400472 if (!is_valid_mmap(token) || !uctxt ||
473 !(vma->vm_flags & VM_SHARED)) {
474 ret = -EINVAL;
475 goto done;
476 }
477 dd = uctxt->dd;
478 ctxt = HFI1_MMAP_TOKEN_GET(CTXT, token);
479 subctxt = HFI1_MMAP_TOKEN_GET(SUBCTXT, token);
480 type = HFI1_MMAP_TOKEN_GET(TYPE, token);
Ira Weiny9e10af42015-10-30 18:58:40 -0400481 if (ctxt != uctxt->ctxt || subctxt != fd->subctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400482 ret = -EINVAL;
483 goto done;
484 }
485
486 flags = vma->vm_flags;
487
488 switch (type) {
489 case PIO_BUFS:
490 case PIO_BUFS_SOP:
491 memaddr = ((dd->physaddr + TXE_PIO_SEND) +
492 /* chip pio base */
Amitoj Kaur Chawlad32cf442015-10-16 22:09:08 +0530493 (uctxt->sc->hw_context * BIT(16))) +
Mike Marciniszyn77241052015-07-30 15:17:43 -0400494 /* 64K PIO space / ctxt */
495 (type == PIO_BUFS_SOP ?
496 (TXE_PIO_SIZE / 2) : 0); /* sop? */
497 /*
498 * Map only the amount allocated to the context, not the
499 * entire available context's PIO space.
500 */
Amitoj Kaur Chawla437b29d2016-03-04 22:45:00 +0530501 memlen = PAGE_ALIGN(uctxt->sc->credits * PIO_BLOCK_SIZE);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400502 flags &= ~VM_MAYREAD;
503 flags |= VM_DONTCOPY | VM_DONTEXPAND;
504 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
505 mapio = 1;
506 break;
507 case PIO_CRED:
508 if (flags & VM_WRITE) {
509 ret = -EPERM;
510 goto done;
511 }
512 /*
513 * The credit return location for this context could be on the
514 * second or third page allocated for credit returns (if number
515 * of enabled contexts > 64 and 128 respectively).
516 */
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700517 memvirt = dd->cr_base[uctxt->numa_id].va;
518 memaddr = virt_to_phys(memvirt) +
Mike Marciniszyn77241052015-07-30 15:17:43 -0400519 (((u64)uctxt->sc->hw_free -
520 (u64)dd->cr_base[uctxt->numa_id].va) & PAGE_MASK);
521 memlen = PAGE_SIZE;
522 flags &= ~VM_MAYWRITE;
523 flags |= VM_DONTCOPY | VM_DONTEXPAND;
524 /*
525 * The driver has already allocated memory for credit
526 * returns and programmed it into the chip. Has that
527 * memory been flagged as non-cached?
528 */
529 /* vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); */
530 mapio = 1;
531 break;
532 case RCV_HDRQ:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400533 memlen = uctxt->rcvhdrq_size;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700534 memvirt = uctxt->rcvhdrq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400535 break;
536 case RCV_EGRBUF: {
537 unsigned long addr;
538 int i;
539 /*
540 * The RcvEgr buffer need to be handled differently
541 * as multiple non-contiguous pages need to be mapped
542 * into the user process.
543 */
544 memlen = uctxt->egrbufs.size;
545 if ((vma->vm_end - vma->vm_start) != memlen) {
546 dd_dev_err(dd, "Eager buffer map size invalid (%lu != %lu)\n",
547 (vma->vm_end - vma->vm_start), memlen);
548 ret = -EINVAL;
549 goto done;
550 }
551 if (vma->vm_flags & VM_WRITE) {
552 ret = -EPERM;
553 goto done;
554 }
555 vma->vm_flags &= ~VM_MAYWRITE;
556 addr = vma->vm_start;
557 for (i = 0 ; i < uctxt->egrbufs.numbufs; i++) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700558 memlen = uctxt->egrbufs.buffers[i].len;
559 memvirt = uctxt->egrbufs.buffers[i].addr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400560 ret = remap_pfn_range(
561 vma, addr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700562 /*
563 * virt_to_pfn() does the same, but
564 * it's not available on x86_64
565 * when CONFIG_MMU is enabled.
566 */
567 PFN_DOWN(__pa(memvirt)),
568 memlen,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400569 vma->vm_page_prot);
570 if (ret < 0)
571 goto done;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700572 addr += memlen;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400573 }
574 ret = 0;
575 goto done;
576 }
577 case UREGS:
578 /*
579 * Map only the page that contains this context's user
580 * registers.
581 */
582 memaddr = (unsigned long)
583 (dd->physaddr + RXE_PER_CONTEXT_USER)
584 + (uctxt->ctxt * RXE_PER_CONTEXT_SIZE);
585 /*
586 * TidFlow table is on the same page as the rest of the
587 * user registers.
588 */
589 memlen = PAGE_SIZE;
590 flags |= VM_DONTCOPY | VM_DONTEXPAND;
591 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
592 mapio = 1;
593 break;
594 case EVENTS:
595 /*
596 * Use the page where this context's flags are. User level
597 * knows where it's own bitmap is within the page.
598 */
Mitko Haralanov3c6c0652015-10-26 10:28:39 -0400599 memaddr = (unsigned long)(dd->events +
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700600 ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
601 HFI1_MAX_SHARED_CTXTS)) & PAGE_MASK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400602 memlen = PAGE_SIZE;
603 /*
604 * v3.7 removes VM_RESERVED but the effect is kept by
605 * using VM_IO.
606 */
607 flags |= VM_IO | VM_DONTEXPAND;
608 vmf = 1;
609 break;
610 case STATUS:
Ira Weiny12220262017-04-09 10:17:24 -0700611 if (flags & (unsigned long)(VM_WRITE | VM_EXEC)) {
612 ret = -EPERM;
613 goto done;
614 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400615 memaddr = kvirt_to_phys((void *)dd->status);
616 memlen = PAGE_SIZE;
617 flags |= VM_IO | VM_DONTEXPAND;
618 break;
619 case RTAIL:
620 if (!HFI1_CAP_IS_USET(DMA_RTAIL)) {
621 /*
622 * If the memory allocation failed, the context alloc
623 * also would have failed, so we would never get here
624 */
625 ret = -EINVAL;
626 goto done;
627 }
628 if (flags & VM_WRITE) {
629 ret = -EPERM;
630 goto done;
631 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400632 memlen = PAGE_SIZE;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700633 memvirt = (void *)uctxt->rcvhdrtail_kvaddr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400634 flags &= ~VM_MAYWRITE;
635 break;
636 case SUBCTXT_UREGS:
637 memaddr = (u64)uctxt->subctxt_uregbase;
638 memlen = PAGE_SIZE;
639 flags |= VM_IO | VM_DONTEXPAND;
640 vmf = 1;
641 break;
642 case SUBCTXT_RCV_HDRQ:
643 memaddr = (u64)uctxt->subctxt_rcvhdr_base;
644 memlen = uctxt->rcvhdrq_size * uctxt->subctxt_cnt;
645 flags |= VM_IO | VM_DONTEXPAND;
646 vmf = 1;
647 break;
648 case SUBCTXT_EGRBUF:
649 memaddr = (u64)uctxt->subctxt_rcvegrbuf;
650 memlen = uctxt->egrbufs.size * uctxt->subctxt_cnt;
651 flags |= VM_IO | VM_DONTEXPAND;
652 flags &= ~VM_MAYWRITE;
653 vmf = 1;
654 break;
655 case SDMA_COMP: {
Ira Weiny9e10af42015-10-30 18:58:40 -0400656 struct hfi1_user_sdma_comp_q *cq = fd->cq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400657
Ira Weiny9e10af42015-10-30 18:58:40 -0400658 if (!cq) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400659 ret = -EFAULT;
660 goto done;
661 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400662 memaddr = (u64)cq->comps;
Amitoj Kaur Chawla437b29d2016-03-04 22:45:00 +0530663 memlen = PAGE_ALIGN(sizeof(*cq->comps) * cq->nentries);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400664 flags |= VM_IO | VM_DONTEXPAND;
665 vmf = 1;
666 break;
667 }
668 default:
669 ret = -EINVAL;
670 break;
671 }
672
673 if ((vma->vm_end - vma->vm_start) != memlen) {
674 hfi1_cdbg(PROC, "%u:%u Memory size mismatch %lu:%lu",
Ira Weiny9e10af42015-10-30 18:58:40 -0400675 uctxt->ctxt, fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400676 (vma->vm_end - vma->vm_start), memlen);
677 ret = -EINVAL;
678 goto done;
679 }
680
681 vma->vm_flags = flags;
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500682 hfi1_cdbg(PROC,
683 "%u:%u type:%u io/vf:%d/%d, addr:0x%llx, len:%lu(%lu), flags:0x%lx\n",
684 ctxt, subctxt, type, mapio, vmf, memaddr, memlen,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400685 vma->vm_end - vma->vm_start, vma->vm_flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400686 if (vmf) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700687 vma->vm_pgoff = PFN_DOWN(memaddr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400688 vma->vm_ops = &vm_ops;
689 ret = 0;
690 } else if (mapio) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700691 ret = io_remap_pfn_range(vma, vma->vm_start,
692 PFN_DOWN(memaddr),
693 memlen,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400694 vma->vm_page_prot);
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700695 } else if (memvirt) {
696 ret = remap_pfn_range(vma, vma->vm_start,
697 PFN_DOWN(__pa(memvirt)),
698 memlen,
699 vma->vm_page_prot);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400700 } else {
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700701 ret = remap_pfn_range(vma, vma->vm_start,
702 PFN_DOWN(memaddr),
703 memlen,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400704 vma->vm_page_prot);
705 }
706done:
707 return ret;
708}
709
710/*
711 * Local (non-chip) user memory is not mapped right away but as it is
712 * accessed by the user-level code.
713 */
Dave Jiang11bac802017-02-24 14:56:41 -0800714static int vma_fault(struct vm_fault *vmf)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400715{
716 struct page *page;
717
718 page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
719 if (!page)
720 return VM_FAULT_SIGBUS;
721
722 get_page(page);
723 vmf->page = page;
724
725 return 0;
726}
727
728static unsigned int hfi1_poll(struct file *fp, struct poll_table_struct *pt)
729{
730 struct hfi1_ctxtdata *uctxt;
731 unsigned pollflag;
732
Ira Weiny9e10af42015-10-30 18:58:40 -0400733 uctxt = ((struct hfi1_filedata *)fp->private_data)->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400734 if (!uctxt)
735 pollflag = POLLERR;
736 else if (uctxt->poll_type == HFI1_POLL_TYPE_URGENT)
737 pollflag = poll_urgent(fp, pt);
738 else if (uctxt->poll_type == HFI1_POLL_TYPE_ANYRCV)
739 pollflag = poll_next(fp, pt);
740 else /* invalid */
741 pollflag = POLLERR;
742
743 return pollflag;
744}
745
746static int hfi1_file_close(struct inode *inode, struct file *fp)
747{
748 struct hfi1_filedata *fdata = fp->private_data;
749 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700750 struct hfi1_devdata *dd = container_of(inode->i_cdev,
751 struct hfi1_devdata,
752 user_cdev);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400753 unsigned long flags, *ev;
754
755 fp->private_data = NULL;
756
757 if (!uctxt)
758 goto done;
759
760 hfi1_cdbg(PROC, "freeing ctxt %u:%u", uctxt->ctxt, fdata->subctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400761 mutex_lock(&hfi1_mutex);
762
763 flush_wc();
764 /* drain user sdma queue */
Mitko Haralanov483119a2015-12-08 17:10:10 -0500765 hfi1_user_sdma_free_queues(fdata);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400766
Mitko Haralanov957558c2016-02-03 14:33:40 -0800767 /* release the cpu */
Sebastian Sanchezb094a362016-07-25 07:54:57 -0700768 hfi1_put_proc_affinity(fdata->rec_cpu_num);
Mitko Haralanov957558c2016-02-03 14:33:40 -0800769
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700770 /* clean up rcv side */
771 hfi1_user_exp_rcv_free(fdata);
772
Mike Marciniszyn77241052015-07-30 15:17:43 -0400773 /*
774 * Clear any left over, unhandled events so the next process that
775 * gets this context doesn't get confused.
776 */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700777 ev = dd->events + ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
Mike Marciniszyn77241052015-07-30 15:17:43 -0400778 HFI1_MAX_SHARED_CTXTS) + fdata->subctxt;
779 *ev = 0;
780
781 if (--uctxt->cnt) {
782 uctxt->active_slaves &= ~(1 << fdata->subctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400783 mutex_unlock(&hfi1_mutex);
784 goto done;
785 }
786
787 spin_lock_irqsave(&dd->uctxt_lock, flags);
788 /*
789 * Disable receive context and interrupt available, reset all
790 * RcvCtxtCtrl bits to default values.
791 */
792 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
793 HFI1_RCVCTRL_TIDFLOW_DIS |
794 HFI1_RCVCTRL_INTRAVAIL_DIS |
Mitko Haralanov566c1572016-02-03 14:32:49 -0800795 HFI1_RCVCTRL_TAILUPD_DIS |
Mike Marciniszyn77241052015-07-30 15:17:43 -0400796 HFI1_RCVCTRL_ONE_PKT_EGR_DIS |
797 HFI1_RCVCTRL_NO_RHQ_DROP_DIS |
798 HFI1_RCVCTRL_NO_EGR_DROP_DIS, uctxt->ctxt);
799 /* Clear the context's J_KEY */
800 hfi1_clear_ctxt_jkey(dd, uctxt->ctxt);
801 /*
802 * Reset context integrity checks to default.
803 * (writes to CSRs probably belong in chip.c)
804 */
805 write_kctxt_csr(dd, uctxt->sc->hw_context, SEND_CTXT_CHECK_ENABLE,
806 hfi1_pkt_default_send_ctxt_mask(dd, uctxt->sc->type));
807 sc_disable(uctxt->sc);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400808 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
809
810 dd->rcd[uctxt->ctxt] = NULL;
Mitko Haralanov94158442016-04-20 06:05:36 -0700811
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700812 hfi1_user_exp_rcv_grp_free(uctxt);
Michael J. Ruhl637a9a72017-05-04 05:15:03 -0700813 hfi1_clear_ctxt_pkey(dd, uctxt);
Mitko Haralanov94158442016-04-20 06:05:36 -0700814
Mike Marciniszyn77241052015-07-30 15:17:43 -0400815 uctxt->rcvwait_to = 0;
816 uctxt->piowait_to = 0;
817 uctxt->rcvnowait = 0;
818 uctxt->pionowait = 0;
819 uctxt->event_flags = 0;
820
Mike Marciniszyn77241052015-07-30 15:17:43 -0400821 hfi1_stats.sps_ctxts--;
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -0800822 if (++dd->freectxts == dd->num_user_contexts)
823 aspm_enable_all(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400824 mutex_unlock(&hfi1_mutex);
825 hfi1_free_ctxtdata(dd, uctxt);
826done:
Ira Weinye0cf75d2016-08-16 13:27:03 -0700827 mmdrop(fdata->mm);
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700828 kobject_put(&dd->kobj);
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -0700829
830 if (atomic_dec_and_test(&dd->user_refcount))
831 complete(&dd->user_comp);
832
Mike Marciniszyn77241052015-07-30 15:17:43 -0400833 kfree(fdata);
834 return 0;
835}
836
837/*
838 * Convert kernel *virtual* addresses to physical addresses.
839 * This is used to vmalloc'ed addresses.
840 */
841static u64 kvirt_to_phys(void *addr)
842{
843 struct page *page;
844 u64 paddr = 0;
845
846 page = vmalloc_to_page(addr);
847 if (page)
848 paddr = page_to_pfn(page) << PAGE_SHIFT;
849
850 return paddr;
851}
852
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700853static int assign_ctxt(struct hfi1_filedata *fd, struct hfi1_user_info *uinfo)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400854{
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700855 int ret = 0;
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700856 unsigned int swmajor, swminor;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400857
858 swmajor = uinfo->userversion >> 16;
859 if (swmajor != HFI1_USER_SWMAJOR) {
860 ret = -ENODEV;
861 goto done;
862 }
863
864 swminor = uinfo->userversion & 0xffff;
865
Mike Marciniszyn77241052015-07-30 15:17:43 -0400866 mutex_lock(&hfi1_mutex);
867 /* First, lets check if we need to setup a shared context? */
Mitko Haralanov957558c2016-02-03 14:33:40 -0800868 if (uinfo->subctxt_cnt) {
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700869 ret = find_shared_ctxt(fd, uinfo);
Mitko Haralanov957558c2016-02-03 14:33:40 -0800870 if (ret < 0)
871 goto done_unlock;
Sebastian Sanchezb094a362016-07-25 07:54:57 -0700872 if (ret) {
873 fd->rec_cpu_num =
874 hfi1_get_proc_affinity(fd->uctxt->numa_id);
875 }
Mitko Haralanov957558c2016-02-03 14:33:40 -0800876 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400877
878 /*
879 * We execute the following block if we couldn't find a
880 * shared context or if context sharing is not required.
881 */
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700882 if (!ret)
883 ret = allocate_ctxt(fd, fd->dd, uinfo);
884
Mitko Haralanov957558c2016-02-03 14:33:40 -0800885done_unlock:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400886 mutex_unlock(&hfi1_mutex);
887done:
888 return ret;
889}
890
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700891static int find_shared_ctxt(struct hfi1_filedata *fd,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400892 const struct hfi1_user_info *uinfo)
893{
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700894 int i;
895 struct hfi1_devdata *dd = fd->dd;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400896
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700897 for (i = dd->first_dyn_alloc_ctxt; i < dd->num_rcv_contexts; i++) {
898 struct hfi1_ctxtdata *uctxt = dd->rcd[i];
Mike Marciniszyn77241052015-07-30 15:17:43 -0400899
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700900 /* Skip ctxts which are not yet open */
901 if (!uctxt || !uctxt->cnt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400902 continue;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400903
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700904 /* Skip dynamically allocted kernel contexts */
905 if (uctxt->sc && (uctxt->sc->type == SC_KERNEL))
906 continue;
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700907
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700908 /* Skip ctxt if it doesn't match the requested one */
909 if (memcmp(uctxt->uuid, uinfo->uuid,
910 sizeof(uctxt->uuid)) ||
911 uctxt->jkey != generate_jkey(current_uid()) ||
912 uctxt->subctxt_id != uinfo->subctxt_id ||
913 uctxt->subctxt_cnt != uinfo->subctxt_cnt)
914 continue;
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700915
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700916 /* Verify the sharing process matches the master */
917 if (uctxt->userversion != uinfo->userversion ||
918 uctxt->cnt >= uctxt->subctxt_cnt) {
919 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400920 }
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700921 fd->uctxt = uctxt;
922 fd->subctxt = uctxt->cnt++;
923 uctxt->active_slaves |= 1 << fd->subctxt;
924 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400925 }
926
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700927 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400928}
929
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700930static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400931 struct hfi1_user_info *uinfo)
932{
933 struct hfi1_ctxtdata *uctxt;
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700934 unsigned int ctxt;
Mitko Haralanov957558c2016-02-03 14:33:40 -0800935 int ret, numa;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400936
937 if (dd->flags & HFI1_FROZEN) {
938 /*
939 * Pick an error that is unique from all other errors
940 * that are returned so the user process knows that
941 * it tried to allocate while the SPC was frozen. It
942 * it should be able to retry with success in a short
943 * while.
944 */
945 return -EIO;
946 }
947
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700948 /*
949 * This check is sort of redundant to the next EBUSY error. It would
950 * also indicate an inconsistancy in the driver if this value was
951 * zero, but there were still contexts available.
952 */
953 if (!dd->freectxts)
954 return -EBUSY;
955
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700956 for (ctxt = dd->first_dyn_alloc_ctxt;
957 ctxt < dd->num_rcv_contexts; ctxt++)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400958 if (!dd->rcd[ctxt])
959 break;
960
961 if (ctxt == dd->num_rcv_contexts)
962 return -EBUSY;
963
Sebastian Sanchezb094a362016-07-25 07:54:57 -0700964 /*
965 * If we don't have a NUMA node requested, preference is towards
966 * device NUMA node.
967 */
968 fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node);
Mitko Haralanov957558c2016-02-03 14:33:40 -0800969 if (fd->rec_cpu_num != -1)
970 numa = cpu_to_node(fd->rec_cpu_num);
971 else
972 numa = numa_node_id();
973 uctxt = hfi1_create_ctxtdata(dd->pport, ctxt, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400974 if (!uctxt) {
975 dd_dev_err(dd,
976 "Unable to allocate ctxtdata memory, failing open\n");
977 return -ENOMEM;
978 }
Mitko Haralanov957558c2016-02-03 14:33:40 -0800979 hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)",
980 uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num,
981 uctxt->numa_id);
982
Mike Marciniszyn77241052015-07-30 15:17:43 -0400983 /*
984 * Allocate and enable a PIO send context.
985 */
986 uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize,
Mitko Haralanovcc572362016-02-03 14:33:49 -0800987 uctxt->dd->node);
Jakub Pawlak3a6982d2016-09-25 07:42:23 -0700988 if (!uctxt->sc) {
989 ret = -ENOMEM;
990 goto ctxdata_free;
991 }
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500992 hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index,
993 uctxt->sc->hw_context);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400994 ret = sc_enable(uctxt->sc);
995 if (ret)
Jakub Pawlak3a6982d2016-09-25 07:42:23 -0700996 goto ctxdata_free;
997
Mike Marciniszyn77241052015-07-30 15:17:43 -0400998 /*
999 * Setup shared context resources if the user-level has requested
1000 * shared contexts and this is the 'master' process.
1001 * This has to be done here so the rest of the sub-contexts find the
1002 * proper master.
1003 */
Ira Weiny9e10af42015-10-30 18:58:40 -04001004 if (uinfo->subctxt_cnt && !fd->subctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001005 ret = init_subctxts(uctxt, uinfo);
1006 /*
1007 * On error, we don't need to disable and de-allocate the
1008 * send context because it will be done during file close
1009 */
1010 if (ret)
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001011 goto ctxdata_free;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001012 }
1013 uctxt->userversion = uinfo->userversion;
Dean Luickbdf77522016-07-28 15:21:13 -04001014 uctxt->flags = hfi1_cap_mask; /* save current flag state */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001015 init_waitqueue_head(&uctxt->wait);
1016 strlcpy(uctxt->comm, current->comm, sizeof(uctxt->comm));
1017 memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid));
1018 uctxt->jkey = generate_jkey(current_uid());
1019 INIT_LIST_HEAD(&uctxt->sdma_queues);
1020 spin_lock_init(&uctxt->sdma_qlock);
1021 hfi1_stats.sps_ctxts++;
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -08001022 /*
1023 * Disable ASPM when there are open user/PSM contexts to avoid
1024 * issues with ASPM L1 exit latency
1025 */
1026 if (dd->freectxts-- == dd->num_user_contexts)
1027 aspm_disable_all(dd);
Ira Weiny9e10af42015-10-30 18:58:40 -04001028 fd->uctxt = uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001029
1030 return 0;
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001031
1032ctxdata_free:
1033 dd->rcd[ctxt] = NULL;
1034 hfi1_free_ctxtdata(dd, uctxt);
1035 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001036}
1037
1038static int init_subctxts(struct hfi1_ctxtdata *uctxt,
1039 const struct hfi1_user_info *uinfo)
1040{
Mike Marciniszyn77241052015-07-30 15:17:43 -04001041 unsigned num_subctxts;
1042
1043 num_subctxts = uinfo->subctxt_cnt;
Mitko Haralanovacac10f2016-02-05 11:57:50 -05001044 if (num_subctxts > HFI1_MAX_SHARED_CTXTS)
1045 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001046
1047 uctxt->subctxt_cnt = uinfo->subctxt_cnt;
1048 uctxt->subctxt_id = uinfo->subctxt_id;
1049 uctxt->active_slaves = 1;
1050 uctxt->redirect_seq_cnt = 1;
1051 set_bit(HFI1_CTXT_MASTER_UNINIT, &uctxt->event_flags);
Mitko Haralanovacac10f2016-02-05 11:57:50 -05001052
1053 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001054}
1055
1056static int setup_subctxt(struct hfi1_ctxtdata *uctxt)
1057{
1058 int ret = 0;
1059 unsigned num_subctxts = uctxt->subctxt_cnt;
1060
1061 uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE);
1062 if (!uctxt->subctxt_uregbase) {
1063 ret = -ENOMEM;
1064 goto bail;
1065 }
1066 /* We can take the size of the RcvHdr Queue from the master */
1067 uctxt->subctxt_rcvhdr_base = vmalloc_user(uctxt->rcvhdrq_size *
1068 num_subctxts);
1069 if (!uctxt->subctxt_rcvhdr_base) {
1070 ret = -ENOMEM;
1071 goto bail_ureg;
1072 }
1073
1074 uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size *
1075 num_subctxts);
1076 if (!uctxt->subctxt_rcvegrbuf) {
1077 ret = -ENOMEM;
1078 goto bail_rhdr;
1079 }
1080 goto bail;
1081bail_rhdr:
1082 vfree(uctxt->subctxt_rcvhdr_base);
1083bail_ureg:
1084 vfree(uctxt->subctxt_uregbase);
1085 uctxt->subctxt_uregbase = NULL;
1086bail:
1087 return ret;
1088}
1089
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -07001090static int user_init(struct hfi1_filedata *fd)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001091{
Mike Marciniszyn77241052015-07-30 15:17:43 -04001092 unsigned int rcvctrl_ops = 0;
Ira Weiny9e10af42015-10-30 18:58:40 -04001093 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001094
1095 /* make sure that the context has already been setup */
Mitko Haralanov94158442016-04-20 06:05:36 -07001096 if (!test_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags))
1097 return -EFAULT;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001098
1099 /* initialize poll variables... */
1100 uctxt->urgent = 0;
1101 uctxt->urgent_poll = 0;
1102
1103 /*
1104 * Now enable the ctxt for receive.
1105 * For chips that are set to DMA the tail register to memory
1106 * when they change (and when the update bit transitions from
1107 * 0 to 1. So for those chips, we turn it off and then back on.
1108 * This will (very briefly) affect any other open ctxts, but the
1109 * duration is very short, and therefore isn't an issue. We
1110 * explicitly set the in-memory tail copy to 0 beforehand, so we
1111 * don't have to wait to be sure the DMA update has happened
1112 * (chip resets head/tail to 0 on transition to enable).
1113 */
1114 if (uctxt->rcvhdrtail_kvaddr)
1115 clear_rcvhdrtail(uctxt);
1116
1117 /* Setup J_KEY before enabling the context */
1118 hfi1_set_ctxt_jkey(uctxt->dd, uctxt->ctxt, uctxt->jkey);
1119
1120 rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB;
Dean Luickbdf77522016-07-28 15:21:13 -04001121 if (HFI1_CAP_UGET_MASK(uctxt->flags, HDRSUPP))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001122 rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB;
1123 /*
1124 * Ignore the bit in the flags for now until proper
1125 * support for multiple packet per rcv array entry is
1126 * added.
1127 */
Dean Luickbdf77522016-07-28 15:21:13 -04001128 if (!HFI1_CAP_UGET_MASK(uctxt->flags, MULTI_PKT_EGR))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001129 rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
Dean Luickbdf77522016-07-28 15:21:13 -04001130 if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_EGR_FULL))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001131 rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
Dean Luickbdf77522016-07-28 15:21:13 -04001132 if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_RHQ_FULL))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001133 rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
Mitko Haralanov566c1572016-02-03 14:32:49 -08001134 /*
1135 * The RcvCtxtCtrl.TailUpd bit has to be explicitly written.
1136 * We can't rely on the correct value to be set from prior
1137 * uses of the chip or ctxt. Therefore, add the rcvctrl op
1138 * for both cases.
1139 */
Dean Luickbdf77522016-07-28 15:21:13 -04001140 if (HFI1_CAP_UGET_MASK(uctxt->flags, DMA_RTAIL))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001141 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB;
Mitko Haralanov566c1572016-02-03 14:32:49 -08001142 else
1143 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001144 hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt->ctxt);
1145
1146 /* Notify any waiting slaves */
1147 if (uctxt->subctxt_cnt) {
1148 clear_bit(HFI1_CTXT_MASTER_UNINIT, &uctxt->event_flags);
1149 wake_up(&uctxt->wait);
1150 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001151
Mitko Haralanov94158442016-04-20 06:05:36 -07001152 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001153}
1154
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -07001155static int get_ctxt_info(struct hfi1_filedata *fd, void __user *ubase,
1156 __u32 len)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001157{
1158 struct hfi1_ctxt_info cinfo;
Ira Weiny9e10af42015-10-30 18:58:40 -04001159 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001160 int ret = 0;
1161
Dan Carpenterebe6b2e2015-09-16 09:42:25 +03001162 memset(&cinfo, 0, sizeof(cinfo));
Dean Luickbdf77522016-07-28 15:21:13 -04001163 cinfo.runtime_flags = (((uctxt->flags >> HFI1_CAP_MISC_SHIFT) &
1164 HFI1_CAP_MISC_MASK) << HFI1_CAP_USER_SHIFT) |
1165 HFI1_CAP_UGET_MASK(uctxt->flags, MASK) |
1166 HFI1_CAP_KGET_MASK(uctxt->flags, K2U);
Dean Luick622c2022016-07-28 15:21:21 -04001167 /* adjust flag if this fd is not able to cache */
1168 if (!fd->handler)
1169 cinfo.runtime_flags |= HFI1_CAP_TID_UNMAP; /* no caching */
1170
Mike Marciniszyn77241052015-07-30 15:17:43 -04001171 cinfo.num_active = hfi1_count_active_units();
1172 cinfo.unit = uctxt->dd->unit;
1173 cinfo.ctxt = uctxt->ctxt;
Ira Weiny9e10af42015-10-30 18:58:40 -04001174 cinfo.subctxt = fd->subctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001175 cinfo.rcvtids = roundup(uctxt->egrbufs.alloced,
1176 uctxt->dd->rcv_entries.group_size) +
1177 uctxt->expected_count;
1178 cinfo.credits = uctxt->sc->credits;
1179 cinfo.numa_node = uctxt->numa_id;
1180 cinfo.rec_cpu = fd->rec_cpu_num;
1181 cinfo.send_ctxt = uctxt->sc->hw_context;
1182
1183 cinfo.egrtids = uctxt->egrbufs.alloced;
1184 cinfo.rcvhdrq_cnt = uctxt->rcvhdrq_cnt;
1185 cinfo.rcvhdrq_entsize = uctxt->rcvhdrqentsize << 2;
Ira Weiny9e10af42015-10-30 18:58:40 -04001186 cinfo.sdma_ring_size = fd->cq->nentries;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001187 cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size;
1188
Ira Weiny9e10af42015-10-30 18:58:40 -04001189 trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, cinfo);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001190 if (copy_to_user(ubase, &cinfo, sizeof(cinfo)))
1191 ret = -EFAULT;
Dean Luickbdf77522016-07-28 15:21:13 -04001192
Mike Marciniszyn77241052015-07-30 15:17:43 -04001193 return ret;
1194}
1195
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -07001196static int setup_ctxt(struct hfi1_filedata *fd)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001197{
Ira Weiny9e10af42015-10-30 18:58:40 -04001198 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001199 struct hfi1_devdata *dd = uctxt->dd;
1200 int ret = 0;
1201
1202 /*
Mitko Haralanov94158442016-04-20 06:05:36 -07001203 * Context should be set up only once, including allocation and
Mike Marciniszyn77241052015-07-30 15:17:43 -04001204 * programming of eager buffers. This is done if context sharing
1205 * is not requested or by the master process.
1206 */
Ira Weiny9e10af42015-10-30 18:58:40 -04001207 if (!uctxt->subctxt_cnt || !fd->subctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001208 ret = hfi1_init_ctxt(uctxt->sc);
1209 if (ret)
1210 goto done;
1211
1212 /* Now allocate the RcvHdr queue and eager buffers. */
1213 ret = hfi1_create_rcvhdrq(dd, uctxt);
1214 if (ret)
1215 goto done;
1216 ret = hfi1_setup_eagerbufs(uctxt);
1217 if (ret)
1218 goto done;
Ira Weiny9e10af42015-10-30 18:58:40 -04001219 if (uctxt->subctxt_cnt && !fd->subctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001220 ret = setup_subctxt(uctxt);
1221 if (ret)
1222 goto done;
1223 }
Mitko Haralanov94158442016-04-20 06:05:36 -07001224 } else {
1225 ret = wait_event_interruptible(uctxt->wait, !test_bit(
1226 HFI1_CTXT_MASTER_UNINIT,
1227 &uctxt->event_flags));
1228 if (ret)
1229 goto done;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001230 }
Mitko Haralanov94158442016-04-20 06:05:36 -07001231
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -07001232 ret = hfi1_user_sdma_alloc_queues(uctxt, fd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001233 if (ret)
1234 goto done;
Mitko Haralanov94158442016-04-20 06:05:36 -07001235 /*
1236 * Expected receive has to be setup for all processes (including
1237 * shared contexts). However, it has to be done after the master
1238 * context has been fully configured as it depends on the
1239 * eager/expected split of the RcvArray entries.
1240 * Setting it up here ensures that the subcontexts will be waiting
1241 * (due to the above wait_event_interruptible() until the master
1242 * is setup.
1243 */
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -07001244 ret = hfi1_user_exp_rcv_init(fd);
Mitko Haralanov94158442016-04-20 06:05:36 -07001245 if (ret)
1246 goto done;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001247
1248 set_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags);
1249done:
1250 return ret;
1251}
1252
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -07001253static int get_base_info(struct hfi1_filedata *fd, void __user *ubase,
1254 __u32 len)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001255{
1256 struct hfi1_base_info binfo;
Ira Weiny9e10af42015-10-30 18:58:40 -04001257 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001258 struct hfi1_devdata *dd = uctxt->dd;
1259 ssize_t sz;
1260 unsigned offset;
1261 int ret = 0;
1262
1263 trace_hfi1_uctxtdata(uctxt->dd, uctxt);
1264
1265 memset(&binfo, 0, sizeof(binfo));
1266 binfo.hw_version = dd->revision;
1267 binfo.sw_version = HFI1_KERN_SWVERSION;
1268 binfo.bthqp = kdeth_qp;
1269 binfo.jkey = uctxt->jkey;
1270 /*
1271 * If more than 64 contexts are enabled the allocated credit
1272 * return will span two or three contiguous pages. Since we only
1273 * map the page containing the context's credit return address,
1274 * we need to calculate the offset in the proper page.
1275 */
1276 offset = ((u64)uctxt->sc->hw_free -
1277 (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE;
1278 binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001279 fd->subctxt, offset);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001280 binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001281 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001282 uctxt->sc->base_addr);
1283 binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP,
1284 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001285 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001286 uctxt->sc->base_addr);
1287 binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001288 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001289 uctxt->rcvhdrq);
1290 binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001291 fd->subctxt,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001292 uctxt->egrbufs.rcvtids[0].dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001293 binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001294 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001295 /*
1296 * user regs are at
1297 * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE))
1298 */
1299 binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001300 fd->subctxt, 0);
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001301 offset = offset_in_page((((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
Ira Weiny9e10af42015-10-30 18:58:40 -04001302 HFI1_MAX_SHARED_CTXTS) + fd->subctxt) *
Geliang Tange260e402015-10-03 10:34:59 +08001303 sizeof(*dd->events));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001304 binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001305 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001306 offset);
1307 binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001308 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001309 dd->status);
1310 if (HFI1_CAP_IS_USET(DMA_RTAIL))
1311 binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001312 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001313 if (uctxt->subctxt_cnt) {
1314 binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS,
1315 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001316 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001317 binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ,
1318 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001319 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001320 binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF,
1321 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001322 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001323 }
1324 sz = (len < sizeof(binfo)) ? len : sizeof(binfo);
1325 if (copy_to_user(ubase, &binfo, sz))
1326 ret = -EFAULT;
1327 return ret;
1328}
1329
1330static unsigned int poll_urgent(struct file *fp,
1331 struct poll_table_struct *pt)
1332{
Ira Weiny9e10af42015-10-30 18:58:40 -04001333 struct hfi1_filedata *fd = fp->private_data;
1334 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001335 struct hfi1_devdata *dd = uctxt->dd;
1336 unsigned pollflag;
1337
1338 poll_wait(fp, &uctxt->wait, pt);
1339
1340 spin_lock_irq(&dd->uctxt_lock);
1341 if (uctxt->urgent != uctxt->urgent_poll) {
1342 pollflag = POLLIN | POLLRDNORM;
1343 uctxt->urgent_poll = uctxt->urgent;
1344 } else {
1345 pollflag = 0;
1346 set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags);
1347 }
1348 spin_unlock_irq(&dd->uctxt_lock);
1349
1350 return pollflag;
1351}
1352
1353static unsigned int poll_next(struct file *fp,
1354 struct poll_table_struct *pt)
1355{
Ira Weiny9e10af42015-10-30 18:58:40 -04001356 struct hfi1_filedata *fd = fp->private_data;
1357 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001358 struct hfi1_devdata *dd = uctxt->dd;
1359 unsigned pollflag;
1360
1361 poll_wait(fp, &uctxt->wait, pt);
1362
1363 spin_lock_irq(&dd->uctxt_lock);
1364 if (hdrqempty(uctxt)) {
1365 set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags);
1366 hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt->ctxt);
1367 pollflag = 0;
Jubin Johne4909742016-02-14 20:22:00 -08001368 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001369 pollflag = POLLIN | POLLRDNORM;
Jubin Johne4909742016-02-14 20:22:00 -08001370 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001371 spin_unlock_irq(&dd->uctxt_lock);
1372
1373 return pollflag;
1374}
1375
1376/*
1377 * Find all user contexts in use, and set the specified bit in their
1378 * event mask.
1379 * See also find_ctxt() for a similar use, that is specific to send buffers.
1380 */
1381int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit)
1382{
1383 struct hfi1_ctxtdata *uctxt;
1384 struct hfi1_devdata *dd = ppd->dd;
1385 unsigned ctxt;
1386 int ret = 0;
1387 unsigned long flags;
1388
1389 if (!dd->events) {
1390 ret = -EINVAL;
1391 goto done;
1392 }
1393
1394 spin_lock_irqsave(&dd->uctxt_lock, flags);
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001395 for (ctxt = dd->first_dyn_alloc_ctxt; ctxt < dd->num_rcv_contexts;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001396 ctxt++) {
1397 uctxt = dd->rcd[ctxt];
1398 if (uctxt) {
1399 unsigned long *evs = dd->events +
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001400 (uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
Mike Marciniszyn77241052015-07-30 15:17:43 -04001401 HFI1_MAX_SHARED_CTXTS;
1402 int i;
1403 /*
1404 * subctxt_cnt is 0 if not shared, so do base
1405 * separately, first, then remaining subctxt, if any
1406 */
1407 set_bit(evtbit, evs);
1408 for (i = 1; i < uctxt->subctxt_cnt; i++)
1409 set_bit(evtbit, evs + i);
1410 }
1411 }
1412 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1413done:
1414 return ret;
1415}
1416
1417/**
1418 * manage_rcvq - manage a context's receive queue
1419 * @uctxt: the context
1420 * @subctxt: the sub-context
1421 * @start_stop: action to carry out
1422 *
1423 * start_stop == 0 disables receive on the context, for use in queue
1424 * overflow conditions. start_stop==1 re-enables, to be used to
1425 * re-init the software copy of the head register
1426 */
1427static int manage_rcvq(struct hfi1_ctxtdata *uctxt, unsigned subctxt,
1428 int start_stop)
1429{
1430 struct hfi1_devdata *dd = uctxt->dd;
1431 unsigned int rcvctrl_op;
1432
1433 if (subctxt)
1434 goto bail;
1435 /* atomically clear receive enable ctxt. */
1436 if (start_stop) {
1437 /*
1438 * On enable, force in-memory copy of the tail register to
1439 * 0, so that protocol code doesn't have to worry about
1440 * whether or not the chip has yet updated the in-memory
1441 * copy or not on return from the system call. The chip
1442 * always resets it's tail register back to 0 on a
1443 * transition from disabled to enabled.
1444 */
1445 if (uctxt->rcvhdrtail_kvaddr)
1446 clear_rcvhdrtail(uctxt);
1447 rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB;
Jubin Johne4909742016-02-14 20:22:00 -08001448 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001449 rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS;
Jubin Johne4909742016-02-14 20:22:00 -08001450 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001451 hfi1_rcvctrl(dd, rcvctrl_op, uctxt->ctxt);
1452 /* always; new head should be equal to new tail; see above */
1453bail:
1454 return 0;
1455}
1456
1457/*
1458 * clear the event notifier events for this context.
1459 * User process then performs actions appropriate to bit having been
1460 * set, if desired, and checks again in future.
1461 */
1462static int user_event_ack(struct hfi1_ctxtdata *uctxt, int subctxt,
1463 unsigned long events)
1464{
1465 int i;
1466 struct hfi1_devdata *dd = uctxt->dd;
1467 unsigned long *evs;
1468
1469 if (!dd->events)
1470 return 0;
1471
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001472 evs = dd->events + ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
Mike Marciniszyn77241052015-07-30 15:17:43 -04001473 HFI1_MAX_SHARED_CTXTS) + subctxt;
1474
1475 for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) {
1476 if (!test_bit(i, &events))
1477 continue;
1478 clear_bit(i, evs);
1479 }
1480 return 0;
1481}
1482
Mike Marciniszyn77241052015-07-30 15:17:43 -04001483static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned subctxt,
1484 u16 pkey)
1485{
1486 int ret = -ENOENT, i, intable = 0;
1487 struct hfi1_pportdata *ppd = uctxt->ppd;
1488 struct hfi1_devdata *dd = uctxt->dd;
1489
1490 if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY) {
1491 ret = -EINVAL;
1492 goto done;
1493 }
1494
1495 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++)
1496 if (pkey == ppd->pkeys[i]) {
1497 intable = 1;
1498 break;
1499 }
1500
1501 if (intable)
1502 ret = hfi1_set_ctxt_pkey(dd, uctxt->ctxt, pkey);
1503done:
1504 return ret;
1505}
1506
Mike Marciniszyn77241052015-07-30 15:17:43 -04001507static void user_remove(struct hfi1_devdata *dd)
1508{
Mike Marciniszyn77241052015-07-30 15:17:43 -04001509
1510 hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001511}
1512
1513static int user_add(struct hfi1_devdata *dd)
1514{
1515 char name[10];
1516 int ret;
1517
Mike Marciniszyn77241052015-07-30 15:17:43 -04001518 snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit);
Dennis Dalessandro0eb62652016-05-19 05:25:50 -07001519 ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops,
Ira Weinye116a642015-09-17 13:47:49 -04001520 &dd->user_cdev, &dd->user_device,
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001521 true, &dd->kobj);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001522 if (ret)
Dennis Dalessandro7312f292016-05-19 05:25:57 -07001523 user_remove(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001524
Mike Marciniszyn77241052015-07-30 15:17:43 -04001525 return ret;
1526}
1527
1528/*
1529 * Create per-unit files in /dev
1530 */
1531int hfi1_device_create(struct hfi1_devdata *dd)
1532{
Dennis Dalessandro0f7b1f92016-05-19 05:26:10 -07001533 return user_add(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001534}
1535
1536/*
1537 * Remove per-unit files in /dev
1538 * void, core kernel returns no errors for this stuff
1539 */
1540void hfi1_device_remove(struct hfi1_devdata *dd)
1541{
1542 user_remove(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001543}