blob: 7361366d80e45bd0dcae1c3e432b706bb10b4bff [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>
Michael J. Ruhl8737ce92017-05-04 05:15:15 -070052#include <linux/bitmap.h>
Mike Marciniszyn77241052015-07-30 15:17:43 -040053
Jason Gunthorpee6bd18f2016-04-10 19:13:13 -060054#include <rdma/ib.h>
55
Mike Marciniszyn77241052015-07-30 15:17:43 -040056#include "hfi.h"
57#include "pio.h"
58#include "device.h"
59#include "common.h"
60#include "trace.h"
61#include "user_sdma.h"
Mitko Haralanov701e4412015-10-30 18:58:43 -040062#include "user_exp_rcv.h"
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -080063#include "aspm.h"
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080064#include "mmu_rb.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040065
66#undef pr_fmt
67#define pr_fmt(fmt) DRIVER_NAME ": " fmt
68
69#define SEND_CTXT_HALT_TIMEOUT 1000 /* msecs */
70
71/*
72 * File operation functions
73 */
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070074static int hfi1_file_open(struct inode *inode, struct file *fp);
75static int hfi1_file_close(struct inode *inode, struct file *fp);
76static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from);
77static unsigned int hfi1_poll(struct file *fp, struct poll_table_struct *pt);
78static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma);
Mike Marciniszyn77241052015-07-30 15:17:43 -040079
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070080static u64 kvirt_to_phys(void *addr);
Michael J. Ruhl5fbded42017-05-04 05:14:57 -070081static int assign_ctxt(struct hfi1_filedata *fd, struct hfi1_user_info *uinfo);
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -070082static void init_subctxts(struct hfi1_ctxtdata *uctxt,
83 const struct hfi1_user_info *uinfo);
Michael J. Ruhle87473b2017-07-29 08:43:32 -070084static int init_user_ctxt(struct hfi1_filedata *fd,
85 struct hfi1_ctxtdata *uctxt);
Michael J. Ruhl62239fc2017-05-04 05:15:21 -070086static void user_init(struct hfi1_ctxtdata *uctxt);
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -070087static int get_ctxt_info(struct hfi1_filedata *fd, void __user *ubase,
88 __u32 len);
89static int get_base_info(struct hfi1_filedata *fd, void __user *ubase,
90 __u32 len);
Michael J. Ruhle87473b2017-07-29 08:43:32 -070091static int setup_base_ctxt(struct hfi1_filedata *fd,
92 struct hfi1_ctxtdata *uctxt);
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070093static int setup_subctxt(struct hfi1_ctxtdata *uctxt);
Michael J. Ruhl5fbded42017-05-04 05:14:57 -070094
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -070095static int find_sub_ctxt(struct hfi1_filedata *fd,
96 const struct hfi1_user_info *uinfo);
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -070097static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
Michael J. Ruhle87473b2017-07-29 08:43:32 -070098 struct hfi1_user_info *uinfo,
99 struct hfi1_ctxtdata **cd);
Michael J. Ruhl42492012017-07-24 07:45:43 -0700100static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt);
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700101static unsigned int poll_urgent(struct file *fp, struct poll_table_struct *pt);
102static unsigned int poll_next(struct file *fp, struct poll_table_struct *pt);
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700103static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700104 unsigned long events);
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700105static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, u16 subctxt, u16 pkey);
106static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700107 int start_stop);
108static int vma_fault(struct vm_fault *vmf);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700109static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
110 unsigned long arg);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400111
112static const struct file_operations hfi1_file_ops = {
113 .owner = THIS_MODULE,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400114 .write_iter = hfi1_write_iter,
115 .open = hfi1_file_open,
116 .release = hfi1_file_close,
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700117 .unlocked_ioctl = hfi1_file_ioctl,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400118 .poll = hfi1_poll,
119 .mmap = hfi1_file_mmap,
120 .llseek = noop_llseek,
121};
122
123static struct vm_operations_struct vm_ops = {
124 .fault = vma_fault,
125};
126
127/*
128 * Types of memories mapped into user processes' space
129 */
130enum mmap_types {
131 PIO_BUFS = 1,
132 PIO_BUFS_SOP,
133 PIO_CRED,
134 RCV_HDRQ,
135 RCV_EGRBUF,
136 UREGS,
137 EVENTS,
138 STATUS,
139 RTAIL,
140 SUBCTXT_UREGS,
141 SUBCTXT_RCV_HDRQ,
142 SUBCTXT_EGRBUF,
143 SDMA_COMP
144};
145
146/*
147 * Masks and offsets defining the mmap tokens
148 */
149#define HFI1_MMAP_OFFSET_MASK 0xfffULL
150#define HFI1_MMAP_OFFSET_SHIFT 0
151#define HFI1_MMAP_SUBCTXT_MASK 0xfULL
152#define HFI1_MMAP_SUBCTXT_SHIFT 12
153#define HFI1_MMAP_CTXT_MASK 0xffULL
154#define HFI1_MMAP_CTXT_SHIFT 16
155#define HFI1_MMAP_TYPE_MASK 0xfULL
156#define HFI1_MMAP_TYPE_SHIFT 24
157#define HFI1_MMAP_MAGIC_MASK 0xffffffffULL
158#define HFI1_MMAP_MAGIC_SHIFT 32
159
160#define HFI1_MMAP_MAGIC 0xdabbad00
161
162#define HFI1_MMAP_TOKEN_SET(field, val) \
163 (((val) & HFI1_MMAP_##field##_MASK) << HFI1_MMAP_##field##_SHIFT)
164#define HFI1_MMAP_TOKEN_GET(field, token) \
165 (((token) >> HFI1_MMAP_##field##_SHIFT) & HFI1_MMAP_##field##_MASK)
166#define HFI1_MMAP_TOKEN(type, ctxt, subctxt, addr) \
167 (HFI1_MMAP_TOKEN_SET(MAGIC, HFI1_MMAP_MAGIC) | \
168 HFI1_MMAP_TOKEN_SET(TYPE, type) | \
169 HFI1_MMAP_TOKEN_SET(CTXT, ctxt) | \
170 HFI1_MMAP_TOKEN_SET(SUBCTXT, subctxt) | \
Geliang Tange260e402015-10-03 10:34:59 +0800171 HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr))))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400172
Mike Marciniszyn77241052015-07-30 15:17:43 -0400173#define dbg(fmt, ...) \
174 pr_info(fmt, ##__VA_ARGS__)
175
Mike Marciniszyn77241052015-07-30 15:17:43 -0400176static inline int is_valid_mmap(u64 token)
177{
178 return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC);
179}
180
181static int hfi1_file_open(struct inode *inode, struct file *fp)
182{
Ira Weinyea3a0ee2016-07-28 12:27:35 -0400183 struct hfi1_filedata *fd;
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700184 struct hfi1_devdata *dd = container_of(inode->i_cdev,
185 struct hfi1_devdata,
186 user_cdev);
187
Mike Marciniszyncb51c5d2017-07-24 07:45:31 -0700188 if (!((dd->flags & HFI1_PRESENT) && dd->kregbase1))
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700189 return -EINVAL;
190
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -0700191 if (!atomic_inc_not_zero(&dd->user_refcount))
192 return -ENXIO;
193
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700194 /* Just take a ref now. Not all opens result in a context assign */
195 kobject_get(&dd->kobj);
196
Mike Marciniszyn77241052015-07-30 15:17:43 -0400197 /* The real work is performed later in assign_ctxt() */
Ira Weinyea3a0ee2016-07-28 12:27:35 -0400198
199 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
200
Ira Weiny3faa3d92016-07-28 15:21:19 -0400201 if (fd) {
202 fd->rec_cpu_num = -1; /* no cpu affinity by default */
203 fd->mm = current->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -0800204 mmgrab(fd->mm);
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700205 fd->dd = dd;
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -0700206 fp->private_data = fd;
207 } else {
208 fp->private_data = NULL;
209
210 if (atomic_dec_and_test(&dd->user_refcount))
211 complete(&dd->user_comp);
212
213 return -ENOMEM;
Ira Weiny3faa3d92016-07-28 15:21:19 -0400214 }
Ira Weinyea3a0ee2016-07-28 12:27:35 -0400215
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -0700216 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400217}
218
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700219static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
220 unsigned long arg)
221{
222 struct hfi1_filedata *fd = fp->private_data;
223 struct hfi1_ctxtdata *uctxt = fd->uctxt;
224 struct hfi1_user_info uinfo;
225 struct hfi1_tid_info tinfo;
226 int ret = 0;
227 unsigned long addr;
228 int uval = 0;
229 unsigned long ul_uval = 0;
230 u16 uval16 = 0;
231
Dennis Dalessandro8a1882e2016-05-19 05:26:37 -0700232 hfi1_cdbg(IOCTL, "IOCTL recv: 0x%x", cmd);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700233 if (cmd != HFI1_IOCTL_ASSIGN_CTXT &&
234 cmd != HFI1_IOCTL_GET_VERS &&
235 !uctxt)
236 return -EINVAL;
237
238 switch (cmd) {
239 case HFI1_IOCTL_ASSIGN_CTXT:
Ira Weinyca2f30a2016-06-09 07:51:33 -0700240 if (uctxt)
241 return -EINVAL;
242
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700243 if (copy_from_user(&uinfo,
244 (struct hfi1_user_info __user *)arg,
245 sizeof(uinfo)))
246 return -EFAULT;
247
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700248 ret = assign_ctxt(fd, &uinfo);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700249 break;
250 case HFI1_IOCTL_CTXT_INFO:
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700251 ret = get_ctxt_info(fd, (void __user *)(unsigned long)arg,
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700252 sizeof(struct hfi1_ctxt_info));
253 break;
254 case HFI1_IOCTL_USER_INFO:
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700255 ret = get_base_info(fd, (void __user *)(unsigned long)arg,
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700256 sizeof(struct hfi1_base_info));
257 break;
258 case HFI1_IOCTL_CREDIT_UPD:
Markus Elfringf7ca5352016-07-23 08:30:52 +0200259 if (uctxt)
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700260 sc_return_credits(uctxt->sc);
261 break;
262
263 case HFI1_IOCTL_TID_UPDATE:
264 if (copy_from_user(&tinfo,
265 (struct hfi11_tid_info __user *)arg,
266 sizeof(tinfo)))
267 return -EFAULT;
268
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700269 ret = hfi1_user_exp_rcv_setup(fd, &tinfo);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700270 if (!ret) {
271 /*
272 * Copy the number of tidlist entries we used
273 * and the length of the buffer we registered.
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700274 */
275 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
276 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
Michael J. Ruhlf13a6e52017-07-24 07:46:42 -0700277 sizeof(tinfo.tidcnt)))
278 return -EFAULT;
279
280 addr = arg + offsetof(struct hfi1_tid_info, length);
281 if (copy_to_user((void __user *)addr, &tinfo.length,
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700282 sizeof(tinfo.length)))
283 ret = -EFAULT;
284 }
285 break;
286
287 case HFI1_IOCTL_TID_FREE:
288 if (copy_from_user(&tinfo,
289 (struct hfi11_tid_info __user *)arg,
290 sizeof(tinfo)))
291 return -EFAULT;
292
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700293 ret = hfi1_user_exp_rcv_clear(fd, &tinfo);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700294 if (ret)
295 break;
296 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
297 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
298 sizeof(tinfo.tidcnt)))
299 ret = -EFAULT;
300 break;
301
302 case HFI1_IOCTL_TID_INVAL_READ:
303 if (copy_from_user(&tinfo,
304 (struct hfi11_tid_info __user *)arg,
305 sizeof(tinfo)))
306 return -EFAULT;
307
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700308 ret = hfi1_user_exp_rcv_invalid(fd, &tinfo);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700309 if (ret)
310 break;
311 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
312 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
313 sizeof(tinfo.tidcnt)))
314 ret = -EFAULT;
315 break;
316
317 case HFI1_IOCTL_RECV_CTRL:
318 ret = get_user(uval, (int __user *)arg);
319 if (ret != 0)
320 return -EFAULT;
321 ret = manage_rcvq(uctxt, fd->subctxt, uval);
322 break;
323
324 case HFI1_IOCTL_POLL_TYPE:
325 ret = get_user(uval, (int __user *)arg);
326 if (ret != 0)
327 return -EFAULT;
328 uctxt->poll_type = (typeof(uctxt->poll_type))uval;
329 break;
330
331 case HFI1_IOCTL_ACK_EVENT:
332 ret = get_user(ul_uval, (unsigned long __user *)arg);
333 if (ret != 0)
334 return -EFAULT;
335 ret = user_event_ack(uctxt, fd->subctxt, ul_uval);
336 break;
337
338 case HFI1_IOCTL_SET_PKEY:
339 ret = get_user(uval16, (u16 __user *)arg);
340 if (ret != 0)
341 return -EFAULT;
342 if (HFI1_CAP_IS_USET(PKEY_CHECK))
343 ret = set_ctxt_pkey(uctxt, fd->subctxt, uval16);
344 else
345 return -EPERM;
346 break;
347
348 case HFI1_IOCTL_CTXT_RESET: {
349 struct send_context *sc;
350 struct hfi1_devdata *dd;
351
352 if (!uctxt || !uctxt->dd || !uctxt->sc)
353 return -EINVAL;
354
355 /*
356 * There is no protection here. User level has to
357 * guarantee that no one will be writing to the send
358 * context while it is being re-initialized.
359 * If user level breaks that guarantee, it will break
360 * it's own context and no one else's.
361 */
362 dd = uctxt->dd;
363 sc = uctxt->sc;
364 /*
365 * Wait until the interrupt handler has marked the
366 * context as halted or frozen. Report error if we time
367 * out.
368 */
369 wait_event_interruptible_timeout(
370 sc->halt_wait, (sc->flags & SCF_HALTED),
371 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
372 if (!(sc->flags & SCF_HALTED))
373 return -ENOLCK;
374
375 /*
376 * If the send context was halted due to a Freeze,
377 * wait until the device has been "unfrozen" before
378 * resetting the context.
379 */
380 if (sc->flags & SCF_FROZEN) {
381 wait_event_interruptible_timeout(
382 dd->event_queue,
383 !(ACCESS_ONCE(dd->flags) & HFI1_FROZEN),
384 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
385 if (dd->flags & HFI1_FROZEN)
386 return -ENOLCK;
387
388 if (dd->flags & HFI1_FORCED_FREEZE)
389 /*
390 * Don't allow context reset if we are into
391 * forced freeze
392 */
393 return -ENODEV;
394
395 sc_disable(sc);
396 ret = sc_enable(sc);
Michael J. Ruhl22505632017-07-24 07:46:06 -0700397 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB, uctxt);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700398 } 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
762 flush_wc();
763 /* drain user sdma queue */
Michael J. Ruhle87473b2017-07-29 08:43:32 -0700764 hfi1_user_sdma_free_queues(fdata, uctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400765
Mitko Haralanov957558c2016-02-03 14:33:40 -0800766 /* release the cpu */
Sebastian Sanchezb094a362016-07-25 07:54:57 -0700767 hfi1_put_proc_affinity(fdata->rec_cpu_num);
Mitko Haralanov957558c2016-02-03 14:33:40 -0800768
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700769 /* clean up rcv side */
770 hfi1_user_exp_rcv_free(fdata);
771
Mike Marciniszyn77241052015-07-30 15:17:43 -0400772 /*
773 * Clear any left over, unhandled events so the next process that
774 * gets this context doesn't get confused.
775 */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700776 ev = dd->events + ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
Mike Marciniszyn77241052015-07-30 15:17:43 -0400777 HFI1_MAX_SHARED_CTXTS) + fdata->subctxt;
778 *ev = 0;
779
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700780 mutex_lock(&hfi1_mutex);
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700781 __clear_bit(fdata->subctxt, uctxt->in_use_ctxts);
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700782 fdata->uctxt = NULL;
783 hfi1_rcd_put(uctxt); /* fdata reference */
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700784 if (!bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400785 mutex_unlock(&hfi1_mutex);
786 goto done;
787 }
788
789 spin_lock_irqsave(&dd->uctxt_lock, flags);
790 /*
791 * Disable receive context and interrupt available, reset all
792 * RcvCtxtCtrl bits to default values.
793 */
794 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
795 HFI1_RCVCTRL_TIDFLOW_DIS |
796 HFI1_RCVCTRL_INTRAVAIL_DIS |
Mitko Haralanov566c1572016-02-03 14:32:49 -0800797 HFI1_RCVCTRL_TAILUPD_DIS |
Mike Marciniszyn77241052015-07-30 15:17:43 -0400798 HFI1_RCVCTRL_ONE_PKT_EGR_DIS |
799 HFI1_RCVCTRL_NO_RHQ_DROP_DIS |
Michael J. Ruhl22505632017-07-24 07:46:06 -0700800 HFI1_RCVCTRL_NO_EGR_DROP_DIS, uctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400801 /* Clear the context's J_KEY */
Michael J. Ruhl17573972017-07-24 07:46:01 -0700802 hfi1_clear_ctxt_jkey(dd, uctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400803 /*
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700804 * If a send context is allocated, reset context integrity
805 * checks to default and disable the send context.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400806 */
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700807 if (uctxt->sc) {
808 set_pio_integrity(uctxt->sc);
809 sc_disable(uctxt->sc);
810 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400811 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
812
Mike Marciniszyn9c1a99c32017-06-09 15:59:40 -0700813 hfi1_free_ctxt_rcv_groups(uctxt);
Michael J. Ruhl637a9a72017-05-04 05:15:03 -0700814 hfi1_clear_ctxt_pkey(dd, uctxt);
Mitko Haralanov94158442016-04-20 06:05:36 -0700815
Mike Marciniszyn77241052015-07-30 15:17:43 -0400816 uctxt->event_flags = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400817 mutex_unlock(&hfi1_mutex);
Michael J. Ruhl42492012017-07-24 07:45:43 -0700818
819 deallocate_ctxt(uctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400820done:
Ira Weinye0cf75d2016-08-16 13:27:03 -0700821 mmdrop(fdata->mm);
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700822 kobject_put(&dd->kobj);
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -0700823
824 if (atomic_dec_and_test(&dd->user_refcount))
825 complete(&dd->user_comp);
826
Mike Marciniszyn77241052015-07-30 15:17:43 -0400827 kfree(fdata);
828 return 0;
829}
830
831/*
832 * Convert kernel *virtual* addresses to physical addresses.
833 * This is used to vmalloc'ed addresses.
834 */
835static u64 kvirt_to_phys(void *addr)
836{
837 struct page *page;
838 u64 paddr = 0;
839
840 page = vmalloc_to_page(addr);
841 if (page)
842 paddr = page_to_pfn(page) << PAGE_SHIFT;
843
844 return paddr;
845}
846
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700847static int complete_subctxt(struct hfi1_filedata *fd)
848{
849 int ret;
850
851 /*
852 * sub-context info can only be set up after the base context
853 * has been completed.
854 */
855 ret = wait_event_interruptible(
856 fd->uctxt->wait,
857 !test_bit(HFI1_CTXT_BASE_UNINIT, &fd->uctxt->event_flags));
858
859 if (test_bit(HFI1_CTXT_BASE_FAILED, &fd->uctxt->event_flags))
860 ret = -ENOMEM;
861
862 /* The only thing a sub context needs is the user_xxx stuff */
863 if (!ret) {
864 fd->rec_cpu_num = hfi1_get_proc_affinity(fd->uctxt->numa_id);
865 ret = init_user_ctxt(fd, fd->uctxt);
866 }
867
868 if (ret) {
869 hfi1_rcd_put(fd->uctxt);
870 fd->uctxt = NULL;
871 mutex_lock(&hfi1_mutex);
872 __clear_bit(fd->subctxt, fd->uctxt->in_use_ctxts);
873 mutex_unlock(&hfi1_mutex);
874 }
875
876 return ret;
877}
878
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700879static int assign_ctxt(struct hfi1_filedata *fd, struct hfi1_user_info *uinfo)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400880{
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700881 int ret;
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700882 unsigned int swmajor, swminor;
Michael J. Ruhle87473b2017-07-29 08:43:32 -0700883 struct hfi1_ctxtdata *uctxt = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400884
885 swmajor = uinfo->userversion >> 16;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700886 if (swmajor != HFI1_USER_SWMAJOR)
887 return -ENODEV;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400888
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700889 if (uinfo->subctxt_cnt > HFI1_MAX_SHARED_CTXTS)
890 return -EINVAL;
891
Mike Marciniszyn77241052015-07-30 15:17:43 -0400892 swminor = uinfo->userversion & 0xffff;
893
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700894 /*
895 * Acquire the mutex to protect against multiple creations of what
896 * could be a shared base context.
897 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400898 mutex_lock(&hfi1_mutex);
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700899 /*
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700900 * Get a sub context if available (fd->uctxt will be set).
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700901 * ret < 0 error, 0 no context, 1 sub-context found
902 */
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700903 ret = find_sub_ctxt(fd, uinfo);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400904
905 /*
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700906 * Allocate a base context if context sharing is not required or a
907 * sub context wasn't found.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400908 */
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700909 if (!ret)
Michael J. Ruhle87473b2017-07-29 08:43:32 -0700910 ret = allocate_ctxt(fd, fd->dd, uinfo, &uctxt);
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700911
Mike Marciniszyn77241052015-07-30 15:17:43 -0400912 mutex_unlock(&hfi1_mutex);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700913
914 /* Depending on the context type, do the appropriate init */
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700915 switch (ret) {
916 case 0:
Michael J. Ruhle87473b2017-07-29 08:43:32 -0700917 ret = setup_base_ctxt(fd, uctxt);
918 if (uctxt->subctxt_cnt) {
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700919 /*
920 * Base context is done, notify anybody using a
921 * sub-context that is waiting for this completion
922 */
Michael J. Ruhle87473b2017-07-29 08:43:32 -0700923 clear_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
924 wake_up(&uctxt->wait);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700925 }
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700926 break;
927 case 1:
928 ret = complete_subctxt(fd);
929 break;
930 default:
931 break;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700932 }
933
Mike Marciniszyn77241052015-07-30 15:17:43 -0400934 return ret;
935}
936
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700937/*
938 * The hfi1_mutex must be held when this function is called. It is
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700939 * necessary to ensure serialized creation of shared contexts.
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700940 */
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700941static int find_sub_ctxt(struct hfi1_filedata *fd,
942 const struct hfi1_user_info *uinfo)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400943{
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700944 u16 i;
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700945 struct hfi1_devdata *dd = fd->dd;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700946 u16 subctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400947
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700948 if (!uinfo->subctxt_cnt)
949 return 0;
950
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700951 for (i = dd->first_dyn_alloc_ctxt; i < dd->num_rcv_contexts; i++) {
952 struct hfi1_ctxtdata *uctxt = dd->rcd[i];
Mike Marciniszyn77241052015-07-30 15:17:43 -0400953
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700954 /* Skip ctxts which are not yet open */
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700955 if (!uctxt ||
956 bitmap_empty(uctxt->in_use_ctxts,
957 HFI1_MAX_SHARED_CTXTS))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400958 continue;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400959
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700960 /* Skip dynamically allocted kernel contexts */
961 if (uctxt->sc && (uctxt->sc->type == SC_KERNEL))
962 continue;
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700963
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700964 /* Skip ctxt if it doesn't match the requested one */
965 if (memcmp(uctxt->uuid, uinfo->uuid,
966 sizeof(uctxt->uuid)) ||
967 uctxt->jkey != generate_jkey(current_uid()) ||
968 uctxt->subctxt_id != uinfo->subctxt_id ||
969 uctxt->subctxt_cnt != uinfo->subctxt_cnt)
970 continue;
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700971
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700972 /* Verify the sharing process matches the master */
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700973 if (uctxt->userversion != uinfo->userversion)
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700974 return -EINVAL;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700975
976 /* Find an unused context */
977 subctxt = find_first_zero_bit(uctxt->in_use_ctxts,
978 HFI1_MAX_SHARED_CTXTS);
979 if (subctxt >= uctxt->subctxt_cnt)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700980 return -EBUSY;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700981
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700982 fd->uctxt = uctxt;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700983 fd->subctxt = subctxt;
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700984
985 hfi1_rcd_get(uctxt);
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700986 __set_bit(fd->subctxt, uctxt->in_use_ctxts);
987
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700988 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400989 }
990
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700991 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400992}
993
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700994static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
Michael J. Ruhle87473b2017-07-29 08:43:32 -0700995 struct hfi1_user_info *uinfo,
996 struct hfi1_ctxtdata **cd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400997{
998 struct hfi1_ctxtdata *uctxt;
Mitko Haralanov957558c2016-02-03 14:33:40 -0800999 int ret, numa;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001000
1001 if (dd->flags & HFI1_FROZEN) {
1002 /*
1003 * Pick an error that is unique from all other errors
1004 * that are returned so the user process knows that
1005 * it tried to allocate while the SPC was frozen. It
1006 * it should be able to retry with success in a short
1007 * while.
1008 */
1009 return -EIO;
1010 }
1011
Michael J. Ruhl5fbded42017-05-04 05:14:57 -07001012 if (!dd->freectxts)
1013 return -EBUSY;
1014
Sebastian Sanchezb094a362016-07-25 07:54:57 -07001015 /*
1016 * If we don't have a NUMA node requested, preference is towards
1017 * device NUMA node.
1018 */
1019 fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node);
Mitko Haralanov957558c2016-02-03 14:33:40 -08001020 if (fd->rec_cpu_num != -1)
1021 numa = cpu_to_node(fd->rec_cpu_num);
1022 else
1023 numa = numa_node_id();
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -07001024 ret = hfi1_create_ctxtdata(dd->pport, numa, &uctxt);
1025 if (ret < 0) {
1026 dd_dev_err(dd, "user ctxtdata allocation failed\n");
1027 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001028 }
Mitko Haralanov957558c2016-02-03 14:33:40 -08001029 hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)",
1030 uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num,
1031 uctxt->numa_id);
1032
Mike Marciniszyn77241052015-07-30 15:17:43 -04001033 /*
1034 * Allocate and enable a PIO send context.
1035 */
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -07001036 uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize, dd->node);
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001037 if (!uctxt->sc) {
1038 ret = -ENOMEM;
1039 goto ctxdata_free;
1040 }
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001041 hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index,
1042 uctxt->sc->hw_context);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001043 ret = sc_enable(uctxt->sc);
1044 if (ret)
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001045 goto ctxdata_free;
1046
Mike Marciniszyn77241052015-07-30 15:17:43 -04001047 /*
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -07001048 * Setup sub context information if the user-level has requested
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001049 * sub contexts.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001050 * This has to be done here so the rest of the sub-contexts find the
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -07001051 * proper base context.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001052 */
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -07001053 if (uinfo->subctxt_cnt)
1054 init_subctxts(uctxt, uinfo);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001055 uctxt->userversion = uinfo->userversion;
Dean Luickbdf77522016-07-28 15:21:13 -04001056 uctxt->flags = hfi1_cap_mask; /* save current flag state */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001057 init_waitqueue_head(&uctxt->wait);
1058 strlcpy(uctxt->comm, current->comm, sizeof(uctxt->comm));
1059 memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid));
1060 uctxt->jkey = generate_jkey(current_uid());
Mike Marciniszyn77241052015-07-30 15:17:43 -04001061 hfi1_stats.sps_ctxts++;
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -08001062 /*
1063 * Disable ASPM when there are open user/PSM contexts to avoid
1064 * issues with ASPM L1 exit latency
1065 */
1066 if (dd->freectxts-- == dd->num_user_contexts)
1067 aspm_disable_all(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001068
Michael J. Ruhle87473b2017-07-29 08:43:32 -07001069 *cd = uctxt;
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001070
Mike Marciniszyn77241052015-07-30 15:17:43 -04001071 return 0;
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001072
1073ctxdata_free:
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -07001074 hfi1_free_ctxt(dd, uctxt);
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001075 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001076}
1077
Michael J. Ruhl42492012017-07-24 07:45:43 -07001078static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt)
1079{
1080 mutex_lock(&hfi1_mutex);
1081 hfi1_stats.sps_ctxts--;
1082 if (++uctxt->dd->freectxts == uctxt->dd->num_user_contexts)
1083 aspm_enable_all(uctxt->dd);
Michael J. Ruhl42492012017-07-24 07:45:43 -07001084 mutex_unlock(&hfi1_mutex);
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -07001085
1086 hfi1_free_ctxt(uctxt->dd, uctxt);
Michael J. Ruhl42492012017-07-24 07:45:43 -07001087}
1088
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -07001089static void init_subctxts(struct hfi1_ctxtdata *uctxt,
1090 const struct hfi1_user_info *uinfo)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001091{
Mike Marciniszyn77241052015-07-30 15:17:43 -04001092 uctxt->subctxt_cnt = uinfo->subctxt_cnt;
1093 uctxt->subctxt_id = uinfo->subctxt_id;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001094 set_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001095}
1096
1097static int setup_subctxt(struct hfi1_ctxtdata *uctxt)
1098{
1099 int ret = 0;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -07001100 u16 num_subctxts = uctxt->subctxt_cnt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001101
1102 uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001103 if (!uctxt->subctxt_uregbase)
1104 return -ENOMEM;
1105
Mike Marciniszyn77241052015-07-30 15:17:43 -04001106 /* We can take the size of the RcvHdr Queue from the master */
1107 uctxt->subctxt_rcvhdr_base = vmalloc_user(uctxt->rcvhdrq_size *
1108 num_subctxts);
1109 if (!uctxt->subctxt_rcvhdr_base) {
1110 ret = -ENOMEM;
1111 goto bail_ureg;
1112 }
1113
1114 uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size *
1115 num_subctxts);
1116 if (!uctxt->subctxt_rcvegrbuf) {
1117 ret = -ENOMEM;
1118 goto bail_rhdr;
1119 }
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001120
1121 return 0;
1122
Mike Marciniszyn77241052015-07-30 15:17:43 -04001123bail_rhdr:
1124 vfree(uctxt->subctxt_rcvhdr_base);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001125 uctxt->subctxt_rcvhdr_base = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001126bail_ureg:
1127 vfree(uctxt->subctxt_uregbase);
1128 uctxt->subctxt_uregbase = NULL;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001129
Mike Marciniszyn77241052015-07-30 15:17:43 -04001130 return ret;
1131}
1132
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001133static void user_init(struct hfi1_ctxtdata *uctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001134{
Mike Marciniszyn77241052015-07-30 15:17:43 -04001135 unsigned int rcvctrl_ops = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001136
1137 /* initialize poll variables... */
1138 uctxt->urgent = 0;
1139 uctxt->urgent_poll = 0;
1140
1141 /*
1142 * Now enable the ctxt for receive.
1143 * For chips that are set to DMA the tail register to memory
1144 * when they change (and when the update bit transitions from
1145 * 0 to 1. So for those chips, we turn it off and then back on.
1146 * This will (very briefly) affect any other open ctxts, but the
1147 * duration is very short, and therefore isn't an issue. We
1148 * explicitly set the in-memory tail copy to 0 beforehand, so we
1149 * don't have to wait to be sure the DMA update has happened
1150 * (chip resets head/tail to 0 on transition to enable).
1151 */
1152 if (uctxt->rcvhdrtail_kvaddr)
1153 clear_rcvhdrtail(uctxt);
1154
1155 /* Setup J_KEY before enabling the context */
Michael J. Ruhl17573972017-07-24 07:46:01 -07001156 hfi1_set_ctxt_jkey(uctxt->dd, uctxt, uctxt->jkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001157
1158 rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB;
Dean Luickbdf77522016-07-28 15:21:13 -04001159 if (HFI1_CAP_UGET_MASK(uctxt->flags, HDRSUPP))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001160 rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB;
1161 /*
1162 * Ignore the bit in the flags for now until proper
1163 * support for multiple packet per rcv array entry is
1164 * added.
1165 */
Dean Luickbdf77522016-07-28 15:21:13 -04001166 if (!HFI1_CAP_UGET_MASK(uctxt->flags, MULTI_PKT_EGR))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001167 rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
Dean Luickbdf77522016-07-28 15:21:13 -04001168 if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_EGR_FULL))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001169 rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
Dean Luickbdf77522016-07-28 15:21:13 -04001170 if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_RHQ_FULL))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001171 rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
Mitko Haralanov566c1572016-02-03 14:32:49 -08001172 /*
1173 * The RcvCtxtCtrl.TailUpd bit has to be explicitly written.
1174 * We can't rely on the correct value to be set from prior
1175 * uses of the chip or ctxt. Therefore, add the rcvctrl op
1176 * for both cases.
1177 */
Dean Luickbdf77522016-07-28 15:21:13 -04001178 if (HFI1_CAP_UGET_MASK(uctxt->flags, DMA_RTAIL))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001179 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB;
Mitko Haralanov566c1572016-02-03 14:32:49 -08001180 else
1181 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS;
Michael J. Ruhl22505632017-07-24 07:46:06 -07001182 hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001183}
1184
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -07001185static int get_ctxt_info(struct hfi1_filedata *fd, void __user *ubase,
1186 __u32 len)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001187{
1188 struct hfi1_ctxt_info cinfo;
Ira Weiny9e10af42015-10-30 18:58:40 -04001189 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001190 int ret = 0;
1191
Dan Carpenterebe6b2e2015-09-16 09:42:25 +03001192 memset(&cinfo, 0, sizeof(cinfo));
Dean Luickbdf77522016-07-28 15:21:13 -04001193 cinfo.runtime_flags = (((uctxt->flags >> HFI1_CAP_MISC_SHIFT) &
1194 HFI1_CAP_MISC_MASK) << HFI1_CAP_USER_SHIFT) |
1195 HFI1_CAP_UGET_MASK(uctxt->flags, MASK) |
1196 HFI1_CAP_KGET_MASK(uctxt->flags, K2U);
Dean Luick622c2022016-07-28 15:21:21 -04001197 /* adjust flag if this fd is not able to cache */
1198 if (!fd->handler)
1199 cinfo.runtime_flags |= HFI1_CAP_TID_UNMAP; /* no caching */
1200
Mike Marciniszyn77241052015-07-30 15:17:43 -04001201 cinfo.num_active = hfi1_count_active_units();
1202 cinfo.unit = uctxt->dd->unit;
1203 cinfo.ctxt = uctxt->ctxt;
Ira Weiny9e10af42015-10-30 18:58:40 -04001204 cinfo.subctxt = fd->subctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001205 cinfo.rcvtids = roundup(uctxt->egrbufs.alloced,
1206 uctxt->dd->rcv_entries.group_size) +
1207 uctxt->expected_count;
1208 cinfo.credits = uctxt->sc->credits;
1209 cinfo.numa_node = uctxt->numa_id;
1210 cinfo.rec_cpu = fd->rec_cpu_num;
1211 cinfo.send_ctxt = uctxt->sc->hw_context;
1212
1213 cinfo.egrtids = uctxt->egrbufs.alloced;
1214 cinfo.rcvhdrq_cnt = uctxt->rcvhdrq_cnt;
1215 cinfo.rcvhdrq_entsize = uctxt->rcvhdrqentsize << 2;
Ira Weiny9e10af42015-10-30 18:58:40 -04001216 cinfo.sdma_ring_size = fd->cq->nentries;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001217 cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size;
1218
Ira Weiny9e10af42015-10-30 18:58:40 -04001219 trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, cinfo);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001220 if (copy_to_user(ubase, &cinfo, sizeof(cinfo)))
1221 ret = -EFAULT;
Dean Luickbdf77522016-07-28 15:21:13 -04001222
Mike Marciniszyn77241052015-07-30 15:17:43 -04001223 return ret;
1224}
1225
Michael J. Ruhle87473b2017-07-29 08:43:32 -07001226static int init_user_ctxt(struct hfi1_filedata *fd,
1227 struct hfi1_ctxtdata *uctxt)
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001228{
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001229 int ret;
1230
1231 ret = hfi1_user_sdma_alloc_queues(uctxt, fd);
1232 if (ret)
1233 return ret;
1234
Michael J. Ruhle87473b2017-07-29 08:43:32 -07001235 ret = hfi1_user_exp_rcv_init(fd, uctxt);
1236 if (ret)
1237 hfi1_user_sdma_free_queues(fd, uctxt);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001238
1239 return ret;
1240}
1241
Michael J. Ruhle87473b2017-07-29 08:43:32 -07001242static int setup_base_ctxt(struct hfi1_filedata *fd,
1243 struct hfi1_ctxtdata *uctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001244{
Mike Marciniszyn77241052015-07-30 15:17:43 -04001245 struct hfi1_devdata *dd = uctxt->dd;
1246 int ret = 0;
1247
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001248 hfi1_init_ctxt(uctxt->sc);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001249
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001250 /* Now allocate the RcvHdr queue and eager buffers. */
1251 ret = hfi1_create_rcvhdrq(dd, uctxt);
Mitko Haralanov94158442016-04-20 06:05:36 -07001252 if (ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001253 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001254
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001255 ret = hfi1_setup_eagerbufs(uctxt);
1256 if (ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001257 goto setup_failed;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001258
1259 /* If sub-contexts are enabled, do the appropriate setup */
1260 if (uctxt->subctxt_cnt)
1261 ret = setup_subctxt(uctxt);
1262 if (ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001263 goto setup_failed;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001264
Mike Marciniszyn9c1a99c32017-06-09 15:59:40 -07001265 ret = hfi1_alloc_ctxt_rcv_groups(uctxt);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001266 if (ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001267 goto setup_failed;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001268
Michael J. Ruhle87473b2017-07-29 08:43:32 -07001269 ret = init_user_ctxt(fd, uctxt);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001270 if (ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001271 goto setup_failed;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001272
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001273 user_init(uctxt);
1274
Michael J. Ruhle87473b2017-07-29 08:43:32 -07001275 /* Now that the context is set up, the fd can get a reference. */
1276 fd->uctxt = uctxt;
1277 hfi1_rcd_get(uctxt);
1278
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001279 return 0;
1280
1281setup_failed:
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -07001282 set_bit(HFI1_CTXT_BASE_FAILED, &uctxt->event_flags);
1283 deallocate_ctxt(uctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001284 return ret;
1285}
1286
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -07001287static int get_base_info(struct hfi1_filedata *fd, void __user *ubase,
1288 __u32 len)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001289{
1290 struct hfi1_base_info binfo;
Ira Weiny9e10af42015-10-30 18:58:40 -04001291 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001292 struct hfi1_devdata *dd = uctxt->dd;
1293 ssize_t sz;
1294 unsigned offset;
1295 int ret = 0;
1296
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001297 trace_hfi1_uctxtdata(uctxt->dd, uctxt, fd->subctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001298
1299 memset(&binfo, 0, sizeof(binfo));
1300 binfo.hw_version = dd->revision;
1301 binfo.sw_version = HFI1_KERN_SWVERSION;
1302 binfo.bthqp = kdeth_qp;
1303 binfo.jkey = uctxt->jkey;
1304 /*
1305 * If more than 64 contexts are enabled the allocated credit
1306 * return will span two or three contiguous pages. Since we only
1307 * map the page containing the context's credit return address,
1308 * we need to calculate the offset in the proper page.
1309 */
1310 offset = ((u64)uctxt->sc->hw_free -
1311 (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE;
1312 binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001313 fd->subctxt, offset);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001314 binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001315 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001316 uctxt->sc->base_addr);
1317 binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP,
1318 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001319 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001320 uctxt->sc->base_addr);
1321 binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001322 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001323 uctxt->rcvhdrq);
1324 binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001325 fd->subctxt,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001326 uctxt->egrbufs.rcvtids[0].dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001327 binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001328 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001329 /*
1330 * user regs are at
1331 * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE))
1332 */
1333 binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001334 fd->subctxt, 0);
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001335 offset = offset_in_page((((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
Ira Weiny9e10af42015-10-30 18:58:40 -04001336 HFI1_MAX_SHARED_CTXTS) + fd->subctxt) *
Geliang Tange260e402015-10-03 10:34:59 +08001337 sizeof(*dd->events));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001338 binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001339 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001340 offset);
1341 binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001342 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001343 dd->status);
1344 if (HFI1_CAP_IS_USET(DMA_RTAIL))
1345 binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001346 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001347 if (uctxt->subctxt_cnt) {
1348 binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS,
1349 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001350 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001351 binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ,
1352 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001353 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001354 binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF,
1355 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001356 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001357 }
1358 sz = (len < sizeof(binfo)) ? len : sizeof(binfo);
1359 if (copy_to_user(ubase, &binfo, sz))
1360 ret = -EFAULT;
1361 return ret;
1362}
1363
1364static unsigned int poll_urgent(struct file *fp,
1365 struct poll_table_struct *pt)
1366{
Ira Weiny9e10af42015-10-30 18:58:40 -04001367 struct hfi1_filedata *fd = fp->private_data;
1368 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001369 struct hfi1_devdata *dd = uctxt->dd;
1370 unsigned pollflag;
1371
1372 poll_wait(fp, &uctxt->wait, pt);
1373
1374 spin_lock_irq(&dd->uctxt_lock);
1375 if (uctxt->urgent != uctxt->urgent_poll) {
1376 pollflag = POLLIN | POLLRDNORM;
1377 uctxt->urgent_poll = uctxt->urgent;
1378 } else {
1379 pollflag = 0;
1380 set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags);
1381 }
1382 spin_unlock_irq(&dd->uctxt_lock);
1383
1384 return pollflag;
1385}
1386
1387static unsigned int poll_next(struct file *fp,
1388 struct poll_table_struct *pt)
1389{
Ira Weiny9e10af42015-10-30 18:58:40 -04001390 struct hfi1_filedata *fd = fp->private_data;
1391 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001392 struct hfi1_devdata *dd = uctxt->dd;
1393 unsigned pollflag;
1394
1395 poll_wait(fp, &uctxt->wait, pt);
1396
1397 spin_lock_irq(&dd->uctxt_lock);
1398 if (hdrqempty(uctxt)) {
1399 set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags);
Michael J. Ruhl22505632017-07-24 07:46:06 -07001400 hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001401 pollflag = 0;
Jubin Johne4909742016-02-14 20:22:00 -08001402 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001403 pollflag = POLLIN | POLLRDNORM;
Jubin Johne4909742016-02-14 20:22:00 -08001404 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001405 spin_unlock_irq(&dd->uctxt_lock);
1406
1407 return pollflag;
1408}
1409
1410/*
1411 * Find all user contexts in use, and set the specified bit in their
1412 * event mask.
1413 * See also find_ctxt() for a similar use, that is specific to send buffers.
1414 */
1415int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit)
1416{
1417 struct hfi1_ctxtdata *uctxt;
1418 struct hfi1_devdata *dd = ppd->dd;
Michael J. Ruhle6f76222017-07-24 07:45:55 -07001419 u16 ctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001420 int ret = 0;
1421 unsigned long flags;
1422
1423 if (!dd->events) {
1424 ret = -EINVAL;
1425 goto done;
1426 }
1427
1428 spin_lock_irqsave(&dd->uctxt_lock, flags);
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001429 for (ctxt = dd->first_dyn_alloc_ctxt; ctxt < dd->num_rcv_contexts;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001430 ctxt++) {
1431 uctxt = dd->rcd[ctxt];
1432 if (uctxt) {
1433 unsigned long *evs = dd->events +
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001434 (uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
Mike Marciniszyn77241052015-07-30 15:17:43 -04001435 HFI1_MAX_SHARED_CTXTS;
1436 int i;
1437 /*
1438 * subctxt_cnt is 0 if not shared, so do base
1439 * separately, first, then remaining subctxt, if any
1440 */
1441 set_bit(evtbit, evs);
1442 for (i = 1; i < uctxt->subctxt_cnt; i++)
1443 set_bit(evtbit, evs + i);
1444 }
1445 }
1446 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1447done:
1448 return ret;
1449}
1450
1451/**
1452 * manage_rcvq - manage a context's receive queue
1453 * @uctxt: the context
1454 * @subctxt: the sub-context
1455 * @start_stop: action to carry out
1456 *
1457 * start_stop == 0 disables receive on the context, for use in queue
1458 * overflow conditions. start_stop==1 re-enables, to be used to
1459 * re-init the software copy of the head register
1460 */
Michael J. Ruhl8737ce92017-05-04 05:15:15 -07001461static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001462 int start_stop)
1463{
1464 struct hfi1_devdata *dd = uctxt->dd;
1465 unsigned int rcvctrl_op;
1466
1467 if (subctxt)
1468 goto bail;
1469 /* atomically clear receive enable ctxt. */
1470 if (start_stop) {
1471 /*
1472 * On enable, force in-memory copy of the tail register to
1473 * 0, so that protocol code doesn't have to worry about
1474 * whether or not the chip has yet updated the in-memory
1475 * copy or not on return from the system call. The chip
1476 * always resets it's tail register back to 0 on a
1477 * transition from disabled to enabled.
1478 */
1479 if (uctxt->rcvhdrtail_kvaddr)
1480 clear_rcvhdrtail(uctxt);
1481 rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB;
Jubin Johne4909742016-02-14 20:22:00 -08001482 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001483 rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS;
Jubin Johne4909742016-02-14 20:22:00 -08001484 }
Michael J. Ruhl22505632017-07-24 07:46:06 -07001485 hfi1_rcvctrl(dd, rcvctrl_op, uctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001486 /* always; new head should be equal to new tail; see above */
1487bail:
1488 return 0;
1489}
1490
1491/*
1492 * clear the event notifier events for this context.
1493 * User process then performs actions appropriate to bit having been
1494 * set, if desired, and checks again in future.
1495 */
Michael J. Ruhl8737ce92017-05-04 05:15:15 -07001496static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001497 unsigned long events)
1498{
1499 int i;
1500 struct hfi1_devdata *dd = uctxt->dd;
1501 unsigned long *evs;
1502
1503 if (!dd->events)
1504 return 0;
1505
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001506 evs = dd->events + ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
Mike Marciniszyn77241052015-07-30 15:17:43 -04001507 HFI1_MAX_SHARED_CTXTS) + subctxt;
1508
1509 for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) {
1510 if (!test_bit(i, &events))
1511 continue;
1512 clear_bit(i, evs);
1513 }
1514 return 0;
1515}
1516
Michael J. Ruhl8737ce92017-05-04 05:15:15 -07001517static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, u16 subctxt, u16 pkey)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001518{
1519 int ret = -ENOENT, i, intable = 0;
1520 struct hfi1_pportdata *ppd = uctxt->ppd;
1521 struct hfi1_devdata *dd = uctxt->dd;
1522
1523 if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY) {
1524 ret = -EINVAL;
1525 goto done;
1526 }
1527
1528 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++)
1529 if (pkey == ppd->pkeys[i]) {
1530 intable = 1;
1531 break;
1532 }
1533
1534 if (intable)
Michael J. Ruhl17573972017-07-24 07:46:01 -07001535 ret = hfi1_set_ctxt_pkey(dd, uctxt, pkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001536done:
1537 return ret;
1538}
1539
Mike Marciniszyn77241052015-07-30 15:17:43 -04001540static void user_remove(struct hfi1_devdata *dd)
1541{
Mike Marciniszyn77241052015-07-30 15:17:43 -04001542
1543 hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001544}
1545
1546static int user_add(struct hfi1_devdata *dd)
1547{
1548 char name[10];
1549 int ret;
1550
Mike Marciniszyn77241052015-07-30 15:17:43 -04001551 snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit);
Dennis Dalessandro0eb62652016-05-19 05:25:50 -07001552 ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops,
Ira Weinye116a642015-09-17 13:47:49 -04001553 &dd->user_cdev, &dd->user_device,
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001554 true, &dd->kobj);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001555 if (ret)
Dennis Dalessandro7312f292016-05-19 05:25:57 -07001556 user_remove(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001557
Mike Marciniszyn77241052015-07-30 15:17:43 -04001558 return ret;
1559}
1560
1561/*
1562 * Create per-unit files in /dev
1563 */
1564int hfi1_device_create(struct hfi1_devdata *dd)
1565{
Dennis Dalessandro0f7b1f92016-05-19 05:26:10 -07001566 return user_add(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001567}
1568
1569/*
1570 * Remove per-unit files in /dev
1571 * void, core kernel returns no errors for this stuff
1572 */
1573void hfi1_device_remove(struct hfi1_devdata *dd)
1574{
1575 user_remove(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001576}