blob: 7be75e0d4f7eebf4c149e4ae042ccf4b8fce83ca [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. Ruhlf4cd8762017-05-04 05:14:39 -070082static int init_subctxts(struct hfi1_ctxtdata *uctxt,
83 const struct hfi1_user_info *uinfo);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -070084static int init_user_ctxt(struct hfi1_filedata *fd);
Michael J. Ruhl62239fc2017-05-04 05:15:21 -070085static void user_init(struct hfi1_ctxtdata *uctxt);
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -070086static int get_ctxt_info(struct hfi1_filedata *fd, void __user *ubase,
87 __u32 len);
88static int get_base_info(struct hfi1_filedata *fd, void __user *ubase,
89 __u32 len);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -070090static int setup_base_ctxt(struct hfi1_filedata *fd);
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070091static int setup_subctxt(struct hfi1_ctxtdata *uctxt);
Michael J. Ruhl5fbded42017-05-04 05:14:57 -070092
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -070093static int find_sub_ctxt(struct hfi1_filedata *fd,
94 const struct hfi1_user_info *uinfo);
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -070095static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070096 struct hfi1_user_info *uinfo);
Michael J. Ruhl42492012017-07-24 07:45:43 -070097static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt);
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070098static unsigned int poll_urgent(struct file *fp, struct poll_table_struct *pt);
99static unsigned int poll_next(struct file *fp, struct poll_table_struct *pt);
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700100static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700101 unsigned long events);
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700102static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, u16 subctxt, u16 pkey);
103static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700104 int start_stop);
105static int vma_fault(struct vm_fault *vmf);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700106static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
107 unsigned long arg);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400108
109static const struct file_operations hfi1_file_ops = {
110 .owner = THIS_MODULE,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400111 .write_iter = hfi1_write_iter,
112 .open = hfi1_file_open,
113 .release = hfi1_file_close,
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700114 .unlocked_ioctl = hfi1_file_ioctl,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400115 .poll = hfi1_poll,
116 .mmap = hfi1_file_mmap,
117 .llseek = noop_llseek,
118};
119
120static struct vm_operations_struct vm_ops = {
121 .fault = vma_fault,
122};
123
124/*
125 * Types of memories mapped into user processes' space
126 */
127enum mmap_types {
128 PIO_BUFS = 1,
129 PIO_BUFS_SOP,
130 PIO_CRED,
131 RCV_HDRQ,
132 RCV_EGRBUF,
133 UREGS,
134 EVENTS,
135 STATUS,
136 RTAIL,
137 SUBCTXT_UREGS,
138 SUBCTXT_RCV_HDRQ,
139 SUBCTXT_EGRBUF,
140 SDMA_COMP
141};
142
143/*
144 * Masks and offsets defining the mmap tokens
145 */
146#define HFI1_MMAP_OFFSET_MASK 0xfffULL
147#define HFI1_MMAP_OFFSET_SHIFT 0
148#define HFI1_MMAP_SUBCTXT_MASK 0xfULL
149#define HFI1_MMAP_SUBCTXT_SHIFT 12
150#define HFI1_MMAP_CTXT_MASK 0xffULL
151#define HFI1_MMAP_CTXT_SHIFT 16
152#define HFI1_MMAP_TYPE_MASK 0xfULL
153#define HFI1_MMAP_TYPE_SHIFT 24
154#define HFI1_MMAP_MAGIC_MASK 0xffffffffULL
155#define HFI1_MMAP_MAGIC_SHIFT 32
156
157#define HFI1_MMAP_MAGIC 0xdabbad00
158
159#define HFI1_MMAP_TOKEN_SET(field, val) \
160 (((val) & HFI1_MMAP_##field##_MASK) << HFI1_MMAP_##field##_SHIFT)
161#define HFI1_MMAP_TOKEN_GET(field, token) \
162 (((token) >> HFI1_MMAP_##field##_SHIFT) & HFI1_MMAP_##field##_MASK)
163#define HFI1_MMAP_TOKEN(type, ctxt, subctxt, addr) \
164 (HFI1_MMAP_TOKEN_SET(MAGIC, HFI1_MMAP_MAGIC) | \
165 HFI1_MMAP_TOKEN_SET(TYPE, type) | \
166 HFI1_MMAP_TOKEN_SET(CTXT, ctxt) | \
167 HFI1_MMAP_TOKEN_SET(SUBCTXT, subctxt) | \
Geliang Tange260e402015-10-03 10:34:59 +0800168 HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr))))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400169
Mike Marciniszyn77241052015-07-30 15:17:43 -0400170#define dbg(fmt, ...) \
171 pr_info(fmt, ##__VA_ARGS__)
172
Mike Marciniszyn77241052015-07-30 15:17:43 -0400173static inline int is_valid_mmap(u64 token)
174{
175 return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC);
176}
177
178static int hfi1_file_open(struct inode *inode, struct file *fp)
179{
Ira Weinyea3a0ee2016-07-28 12:27:35 -0400180 struct hfi1_filedata *fd;
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700181 struct hfi1_devdata *dd = container_of(inode->i_cdev,
182 struct hfi1_devdata,
183 user_cdev);
184
Mike Marciniszyncb51c5d2017-07-24 07:45:31 -0700185 if (!((dd->flags & HFI1_PRESENT) && dd->kregbase1))
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700186 return -EINVAL;
187
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -0700188 if (!atomic_inc_not_zero(&dd->user_refcount))
189 return -ENXIO;
190
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700191 /* Just take a ref now. Not all opens result in a context assign */
192 kobject_get(&dd->kobj);
193
Mike Marciniszyn77241052015-07-30 15:17:43 -0400194 /* The real work is performed later in assign_ctxt() */
Ira Weinyea3a0ee2016-07-28 12:27:35 -0400195
196 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
197
Ira Weiny3faa3d92016-07-28 15:21:19 -0400198 if (fd) {
199 fd->rec_cpu_num = -1; /* no cpu affinity by default */
200 fd->mm = current->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -0800201 mmgrab(fd->mm);
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700202 fd->dd = dd;
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -0700203 fp->private_data = fd;
204 } else {
205 fp->private_data = NULL;
206
207 if (atomic_dec_and_test(&dd->user_refcount))
208 complete(&dd->user_comp);
209
210 return -ENOMEM;
Ira Weiny3faa3d92016-07-28 15:21:19 -0400211 }
Ira Weinyea3a0ee2016-07-28 12:27:35 -0400212
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -0700213 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400214}
215
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700216static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
217 unsigned long arg)
218{
219 struct hfi1_filedata *fd = fp->private_data;
220 struct hfi1_ctxtdata *uctxt = fd->uctxt;
221 struct hfi1_user_info uinfo;
222 struct hfi1_tid_info tinfo;
223 int ret = 0;
224 unsigned long addr;
225 int uval = 0;
226 unsigned long ul_uval = 0;
227 u16 uval16 = 0;
228
Dennis Dalessandro8a1882e2016-05-19 05:26:37 -0700229 hfi1_cdbg(IOCTL, "IOCTL recv: 0x%x", cmd);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700230 if (cmd != HFI1_IOCTL_ASSIGN_CTXT &&
231 cmd != HFI1_IOCTL_GET_VERS &&
232 !uctxt)
233 return -EINVAL;
234
235 switch (cmd) {
236 case HFI1_IOCTL_ASSIGN_CTXT:
Ira Weinyca2f30a2016-06-09 07:51:33 -0700237 if (uctxt)
238 return -EINVAL;
239
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700240 if (copy_from_user(&uinfo,
241 (struct hfi1_user_info __user *)arg,
242 sizeof(uinfo)))
243 return -EFAULT;
244
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700245 ret = assign_ctxt(fd, &uinfo);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700246 break;
247 case HFI1_IOCTL_CTXT_INFO:
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700248 ret = get_ctxt_info(fd, (void __user *)(unsigned long)arg,
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700249 sizeof(struct hfi1_ctxt_info));
250 break;
251 case HFI1_IOCTL_USER_INFO:
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700252 ret = get_base_info(fd, (void __user *)(unsigned long)arg,
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700253 sizeof(struct hfi1_base_info));
254 break;
255 case HFI1_IOCTL_CREDIT_UPD:
Markus Elfringf7ca5352016-07-23 08:30:52 +0200256 if (uctxt)
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700257 sc_return_credits(uctxt->sc);
258 break;
259
260 case HFI1_IOCTL_TID_UPDATE:
261 if (copy_from_user(&tinfo,
262 (struct hfi11_tid_info __user *)arg,
263 sizeof(tinfo)))
264 return -EFAULT;
265
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700266 ret = hfi1_user_exp_rcv_setup(fd, &tinfo);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700267 if (!ret) {
268 /*
269 * Copy the number of tidlist entries we used
270 * and the length of the buffer we registered.
271 * These fields are adjacent in the structure so
272 * we can copy them at the same time.
273 */
274 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
275 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
276 sizeof(tinfo.tidcnt) +
277 sizeof(tinfo.length)))
278 ret = -EFAULT;
279 }
280 break;
281
282 case HFI1_IOCTL_TID_FREE:
283 if (copy_from_user(&tinfo,
284 (struct hfi11_tid_info __user *)arg,
285 sizeof(tinfo)))
286 return -EFAULT;
287
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700288 ret = hfi1_user_exp_rcv_clear(fd, &tinfo);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700289 if (ret)
290 break;
291 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
292 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
293 sizeof(tinfo.tidcnt)))
294 ret = -EFAULT;
295 break;
296
297 case HFI1_IOCTL_TID_INVAL_READ:
298 if (copy_from_user(&tinfo,
299 (struct hfi11_tid_info __user *)arg,
300 sizeof(tinfo)))
301 return -EFAULT;
302
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700303 ret = hfi1_user_exp_rcv_invalid(fd, &tinfo);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700304 if (ret)
305 break;
306 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
307 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
308 sizeof(tinfo.tidcnt)))
309 ret = -EFAULT;
310 break;
311
312 case HFI1_IOCTL_RECV_CTRL:
313 ret = get_user(uval, (int __user *)arg);
314 if (ret != 0)
315 return -EFAULT;
316 ret = manage_rcvq(uctxt, fd->subctxt, uval);
317 break;
318
319 case HFI1_IOCTL_POLL_TYPE:
320 ret = get_user(uval, (int __user *)arg);
321 if (ret != 0)
322 return -EFAULT;
323 uctxt->poll_type = (typeof(uctxt->poll_type))uval;
324 break;
325
326 case HFI1_IOCTL_ACK_EVENT:
327 ret = get_user(ul_uval, (unsigned long __user *)arg);
328 if (ret != 0)
329 return -EFAULT;
330 ret = user_event_ack(uctxt, fd->subctxt, ul_uval);
331 break;
332
333 case HFI1_IOCTL_SET_PKEY:
334 ret = get_user(uval16, (u16 __user *)arg);
335 if (ret != 0)
336 return -EFAULT;
337 if (HFI1_CAP_IS_USET(PKEY_CHECK))
338 ret = set_ctxt_pkey(uctxt, fd->subctxt, uval16);
339 else
340 return -EPERM;
341 break;
342
343 case HFI1_IOCTL_CTXT_RESET: {
344 struct send_context *sc;
345 struct hfi1_devdata *dd;
346
347 if (!uctxt || !uctxt->dd || !uctxt->sc)
348 return -EINVAL;
349
350 /*
351 * There is no protection here. User level has to
352 * guarantee that no one will be writing to the send
353 * context while it is being re-initialized.
354 * If user level breaks that guarantee, it will break
355 * it's own context and no one else's.
356 */
357 dd = uctxt->dd;
358 sc = uctxt->sc;
359 /*
360 * Wait until the interrupt handler has marked the
361 * context as halted or frozen. Report error if we time
362 * out.
363 */
364 wait_event_interruptible_timeout(
365 sc->halt_wait, (sc->flags & SCF_HALTED),
366 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
367 if (!(sc->flags & SCF_HALTED))
368 return -ENOLCK;
369
370 /*
371 * If the send context was halted due to a Freeze,
372 * wait until the device has been "unfrozen" before
373 * resetting the context.
374 */
375 if (sc->flags & SCF_FROZEN) {
376 wait_event_interruptible_timeout(
377 dd->event_queue,
378 !(ACCESS_ONCE(dd->flags) & HFI1_FROZEN),
379 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
380 if (dd->flags & HFI1_FROZEN)
381 return -ENOLCK;
382
383 if (dd->flags & HFI1_FORCED_FREEZE)
384 /*
385 * Don't allow context reset if we are into
386 * forced freeze
387 */
388 return -ENODEV;
389
390 sc_disable(sc);
391 ret = sc_enable(sc);
Michael J. Ruhl22505632017-07-24 07:46:06 -0700392 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB, uctxt);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700393 } else {
394 ret = sc_restart(sc);
395 }
396 if (!ret)
397 sc_return_credits(sc);
398 break;
399 }
400
401 case HFI1_IOCTL_GET_VERS:
402 uval = HFI1_USER_SWVERSION;
403 if (put_user(uval, (int __user *)arg))
404 return -EFAULT;
405 break;
406
407 default:
408 return -EINVAL;
409 }
410
411 return ret;
412}
413
Mike Marciniszyn77241052015-07-30 15:17:43 -0400414static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from)
415{
Ira Weiny9e10af42015-10-30 18:58:40 -0400416 struct hfi1_filedata *fd = kiocb->ki_filp->private_data;
417 struct hfi1_user_sdma_pkt_q *pq = fd->pq;
418 struct hfi1_user_sdma_comp_q *cq = fd->cq;
Ira Weiny0904f322016-07-01 16:00:55 -0700419 int done = 0, reqs = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400420 unsigned long dim = from->nr_segs;
421
Ira Weiny0904f322016-07-01 16:00:55 -0700422 if (!cq || !pq)
423 return -EIO;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400424
Ira Weiny0904f322016-07-01 16:00:55 -0700425 if (!iter_is_iovec(from) || !dim)
426 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400427
428 hfi1_cdbg(SDMA, "SDMA request from %u:%u (%lu)",
Ira Weiny9e10af42015-10-30 18:58:40 -0400429 fd->uctxt->ctxt, fd->subctxt, dim);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400430
Ira Weiny0904f322016-07-01 16:00:55 -0700431 if (atomic_read(&pq->n_reqs) == pq->n_max_reqs)
432 return -ENOSPC;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400433
434 while (dim) {
Ira Weiny0904f322016-07-01 16:00:55 -0700435 int ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400436 unsigned long count = 0;
437
438 ret = hfi1_user_sdma_process_request(
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700439 fd, (struct iovec *)(from->iov + done),
Mike Marciniszyn77241052015-07-30 15:17:43 -0400440 dim, &count);
Ira Weiny0904f322016-07-01 16:00:55 -0700441 if (ret) {
442 reqs = ret;
443 break;
444 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400445 dim -= count;
446 done += count;
447 reqs++;
448 }
Ira Weiny0904f322016-07-01 16:00:55 -0700449
450 return reqs;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400451}
452
453static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma)
454{
Ira Weiny9e10af42015-10-30 18:58:40 -0400455 struct hfi1_filedata *fd = fp->private_data;
456 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400457 struct hfi1_devdata *dd;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700458 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400459 u64 token = vma->vm_pgoff << PAGE_SHIFT,
460 memaddr = 0;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700461 void *memvirt = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400462 u8 subctxt, mapio = 0, vmf = 0, type;
463 ssize_t memlen = 0;
464 int ret = 0;
465 u16 ctxt;
466
Mike Marciniszyn77241052015-07-30 15:17:43 -0400467 if (!is_valid_mmap(token) || !uctxt ||
468 !(vma->vm_flags & VM_SHARED)) {
469 ret = -EINVAL;
470 goto done;
471 }
472 dd = uctxt->dd;
473 ctxt = HFI1_MMAP_TOKEN_GET(CTXT, token);
474 subctxt = HFI1_MMAP_TOKEN_GET(SUBCTXT, token);
475 type = HFI1_MMAP_TOKEN_GET(TYPE, token);
Ira Weiny9e10af42015-10-30 18:58:40 -0400476 if (ctxt != uctxt->ctxt || subctxt != fd->subctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400477 ret = -EINVAL;
478 goto done;
479 }
480
481 flags = vma->vm_flags;
482
483 switch (type) {
484 case PIO_BUFS:
485 case PIO_BUFS_SOP:
486 memaddr = ((dd->physaddr + TXE_PIO_SEND) +
487 /* chip pio base */
Amitoj Kaur Chawlad32cf442015-10-16 22:09:08 +0530488 (uctxt->sc->hw_context * BIT(16))) +
Mike Marciniszyn77241052015-07-30 15:17:43 -0400489 /* 64K PIO space / ctxt */
490 (type == PIO_BUFS_SOP ?
491 (TXE_PIO_SIZE / 2) : 0); /* sop? */
492 /*
493 * Map only the amount allocated to the context, not the
494 * entire available context's PIO space.
495 */
Amitoj Kaur Chawla437b29d2016-03-04 22:45:00 +0530496 memlen = PAGE_ALIGN(uctxt->sc->credits * PIO_BLOCK_SIZE);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400497 flags &= ~VM_MAYREAD;
498 flags |= VM_DONTCOPY | VM_DONTEXPAND;
499 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
500 mapio = 1;
501 break;
502 case PIO_CRED:
503 if (flags & VM_WRITE) {
504 ret = -EPERM;
505 goto done;
506 }
507 /*
508 * The credit return location for this context could be on the
509 * second or third page allocated for credit returns (if number
510 * of enabled contexts > 64 and 128 respectively).
511 */
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700512 memvirt = dd->cr_base[uctxt->numa_id].va;
513 memaddr = virt_to_phys(memvirt) +
Mike Marciniszyn77241052015-07-30 15:17:43 -0400514 (((u64)uctxt->sc->hw_free -
515 (u64)dd->cr_base[uctxt->numa_id].va) & PAGE_MASK);
516 memlen = PAGE_SIZE;
517 flags &= ~VM_MAYWRITE;
518 flags |= VM_DONTCOPY | VM_DONTEXPAND;
519 /*
520 * The driver has already allocated memory for credit
521 * returns and programmed it into the chip. Has that
522 * memory been flagged as non-cached?
523 */
524 /* vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); */
525 mapio = 1;
526 break;
527 case RCV_HDRQ:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400528 memlen = uctxt->rcvhdrq_size;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700529 memvirt = uctxt->rcvhdrq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400530 break;
531 case RCV_EGRBUF: {
532 unsigned long addr;
533 int i;
534 /*
535 * The RcvEgr buffer need to be handled differently
536 * as multiple non-contiguous pages need to be mapped
537 * into the user process.
538 */
539 memlen = uctxt->egrbufs.size;
540 if ((vma->vm_end - vma->vm_start) != memlen) {
541 dd_dev_err(dd, "Eager buffer map size invalid (%lu != %lu)\n",
542 (vma->vm_end - vma->vm_start), memlen);
543 ret = -EINVAL;
544 goto done;
545 }
546 if (vma->vm_flags & VM_WRITE) {
547 ret = -EPERM;
548 goto done;
549 }
550 vma->vm_flags &= ~VM_MAYWRITE;
551 addr = vma->vm_start;
552 for (i = 0 ; i < uctxt->egrbufs.numbufs; i++) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700553 memlen = uctxt->egrbufs.buffers[i].len;
554 memvirt = uctxt->egrbufs.buffers[i].addr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400555 ret = remap_pfn_range(
556 vma, addr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700557 /*
558 * virt_to_pfn() does the same, but
559 * it's not available on x86_64
560 * when CONFIG_MMU is enabled.
561 */
562 PFN_DOWN(__pa(memvirt)),
563 memlen,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400564 vma->vm_page_prot);
565 if (ret < 0)
566 goto done;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700567 addr += memlen;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400568 }
569 ret = 0;
570 goto done;
571 }
572 case UREGS:
573 /*
574 * Map only the page that contains this context's user
575 * registers.
576 */
577 memaddr = (unsigned long)
578 (dd->physaddr + RXE_PER_CONTEXT_USER)
579 + (uctxt->ctxt * RXE_PER_CONTEXT_SIZE);
580 /*
581 * TidFlow table is on the same page as the rest of the
582 * user registers.
583 */
584 memlen = PAGE_SIZE;
585 flags |= VM_DONTCOPY | VM_DONTEXPAND;
586 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
587 mapio = 1;
588 break;
589 case EVENTS:
590 /*
591 * Use the page where this context's flags are. User level
592 * knows where it's own bitmap is within the page.
593 */
Mitko Haralanov3c6c0652015-10-26 10:28:39 -0400594 memaddr = (unsigned long)(dd->events +
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700595 ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
596 HFI1_MAX_SHARED_CTXTS)) & PAGE_MASK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400597 memlen = PAGE_SIZE;
598 /*
599 * v3.7 removes VM_RESERVED but the effect is kept by
600 * using VM_IO.
601 */
602 flags |= VM_IO | VM_DONTEXPAND;
603 vmf = 1;
604 break;
605 case STATUS:
Ira Weiny12220262017-04-09 10:17:24 -0700606 if (flags & (unsigned long)(VM_WRITE | VM_EXEC)) {
607 ret = -EPERM;
608 goto done;
609 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400610 memaddr = kvirt_to_phys((void *)dd->status);
611 memlen = PAGE_SIZE;
612 flags |= VM_IO | VM_DONTEXPAND;
613 break;
614 case RTAIL:
615 if (!HFI1_CAP_IS_USET(DMA_RTAIL)) {
616 /*
617 * If the memory allocation failed, the context alloc
618 * also would have failed, so we would never get here
619 */
620 ret = -EINVAL;
621 goto done;
622 }
623 if (flags & VM_WRITE) {
624 ret = -EPERM;
625 goto done;
626 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400627 memlen = PAGE_SIZE;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700628 memvirt = (void *)uctxt->rcvhdrtail_kvaddr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400629 flags &= ~VM_MAYWRITE;
630 break;
631 case SUBCTXT_UREGS:
632 memaddr = (u64)uctxt->subctxt_uregbase;
633 memlen = PAGE_SIZE;
634 flags |= VM_IO | VM_DONTEXPAND;
635 vmf = 1;
636 break;
637 case SUBCTXT_RCV_HDRQ:
638 memaddr = (u64)uctxt->subctxt_rcvhdr_base;
639 memlen = uctxt->rcvhdrq_size * uctxt->subctxt_cnt;
640 flags |= VM_IO | VM_DONTEXPAND;
641 vmf = 1;
642 break;
643 case SUBCTXT_EGRBUF:
644 memaddr = (u64)uctxt->subctxt_rcvegrbuf;
645 memlen = uctxt->egrbufs.size * uctxt->subctxt_cnt;
646 flags |= VM_IO | VM_DONTEXPAND;
647 flags &= ~VM_MAYWRITE;
648 vmf = 1;
649 break;
650 case SDMA_COMP: {
Ira Weiny9e10af42015-10-30 18:58:40 -0400651 struct hfi1_user_sdma_comp_q *cq = fd->cq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400652
Ira Weiny9e10af42015-10-30 18:58:40 -0400653 if (!cq) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400654 ret = -EFAULT;
655 goto done;
656 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400657 memaddr = (u64)cq->comps;
Amitoj Kaur Chawla437b29d2016-03-04 22:45:00 +0530658 memlen = PAGE_ALIGN(sizeof(*cq->comps) * cq->nentries);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400659 flags |= VM_IO | VM_DONTEXPAND;
660 vmf = 1;
661 break;
662 }
663 default:
664 ret = -EINVAL;
665 break;
666 }
667
668 if ((vma->vm_end - vma->vm_start) != memlen) {
669 hfi1_cdbg(PROC, "%u:%u Memory size mismatch %lu:%lu",
Ira Weiny9e10af42015-10-30 18:58:40 -0400670 uctxt->ctxt, fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400671 (vma->vm_end - vma->vm_start), memlen);
672 ret = -EINVAL;
673 goto done;
674 }
675
676 vma->vm_flags = flags;
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500677 hfi1_cdbg(PROC,
678 "%u:%u type:%u io/vf:%d/%d, addr:0x%llx, len:%lu(%lu), flags:0x%lx\n",
679 ctxt, subctxt, type, mapio, vmf, memaddr, memlen,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400680 vma->vm_end - vma->vm_start, vma->vm_flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400681 if (vmf) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700682 vma->vm_pgoff = PFN_DOWN(memaddr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400683 vma->vm_ops = &vm_ops;
684 ret = 0;
685 } else if (mapio) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700686 ret = io_remap_pfn_range(vma, vma->vm_start,
687 PFN_DOWN(memaddr),
688 memlen,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400689 vma->vm_page_prot);
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700690 } else if (memvirt) {
691 ret = remap_pfn_range(vma, vma->vm_start,
692 PFN_DOWN(__pa(memvirt)),
693 memlen,
694 vma->vm_page_prot);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400695 } else {
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700696 ret = remap_pfn_range(vma, vma->vm_start,
697 PFN_DOWN(memaddr),
698 memlen,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400699 vma->vm_page_prot);
700 }
701done:
702 return ret;
703}
704
705/*
706 * Local (non-chip) user memory is not mapped right away but as it is
707 * accessed by the user-level code.
708 */
Dave Jiang11bac802017-02-24 14:56:41 -0800709static int vma_fault(struct vm_fault *vmf)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400710{
711 struct page *page;
712
713 page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
714 if (!page)
715 return VM_FAULT_SIGBUS;
716
717 get_page(page);
718 vmf->page = page;
719
720 return 0;
721}
722
723static unsigned int hfi1_poll(struct file *fp, struct poll_table_struct *pt)
724{
725 struct hfi1_ctxtdata *uctxt;
726 unsigned pollflag;
727
Ira Weiny9e10af42015-10-30 18:58:40 -0400728 uctxt = ((struct hfi1_filedata *)fp->private_data)->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400729 if (!uctxt)
730 pollflag = POLLERR;
731 else if (uctxt->poll_type == HFI1_POLL_TYPE_URGENT)
732 pollflag = poll_urgent(fp, pt);
733 else if (uctxt->poll_type == HFI1_POLL_TYPE_ANYRCV)
734 pollflag = poll_next(fp, pt);
735 else /* invalid */
736 pollflag = POLLERR;
737
738 return pollflag;
739}
740
741static int hfi1_file_close(struct inode *inode, struct file *fp)
742{
743 struct hfi1_filedata *fdata = fp->private_data;
744 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700745 struct hfi1_devdata *dd = container_of(inode->i_cdev,
746 struct hfi1_devdata,
747 user_cdev);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400748 unsigned long flags, *ev;
749
750 fp->private_data = NULL;
751
752 if (!uctxt)
753 goto done;
754
755 hfi1_cdbg(PROC, "freeing ctxt %u:%u", uctxt->ctxt, fdata->subctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400756 mutex_lock(&hfi1_mutex);
757
758 flush_wc();
759 /* drain user sdma queue */
Mitko Haralanov483119a2015-12-08 17:10:10 -0500760 hfi1_user_sdma_free_queues(fdata);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400761
Mitko Haralanov957558c2016-02-03 14:33:40 -0800762 /* release the cpu */
Sebastian Sanchezb094a362016-07-25 07:54:57 -0700763 hfi1_put_proc_affinity(fdata->rec_cpu_num);
Mitko Haralanov957558c2016-02-03 14:33:40 -0800764
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700765 /* clean up rcv side */
766 hfi1_user_exp_rcv_free(fdata);
767
Mike Marciniszyn77241052015-07-30 15:17:43 -0400768 /*
769 * Clear any left over, unhandled events so the next process that
770 * gets this context doesn't get confused.
771 */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700772 ev = dd->events + ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
Mike Marciniszyn77241052015-07-30 15:17:43 -0400773 HFI1_MAX_SHARED_CTXTS) + fdata->subctxt;
774 *ev = 0;
775
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700776 __clear_bit(fdata->subctxt, uctxt->in_use_ctxts);
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700777 fdata->uctxt = NULL;
778 hfi1_rcd_put(uctxt); /* fdata reference */
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700779 if (!bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400780 mutex_unlock(&hfi1_mutex);
781 goto done;
782 }
783
784 spin_lock_irqsave(&dd->uctxt_lock, flags);
785 /*
786 * Disable receive context and interrupt available, reset all
787 * RcvCtxtCtrl bits to default values.
788 */
789 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
790 HFI1_RCVCTRL_TIDFLOW_DIS |
791 HFI1_RCVCTRL_INTRAVAIL_DIS |
Mitko Haralanov566c1572016-02-03 14:32:49 -0800792 HFI1_RCVCTRL_TAILUPD_DIS |
Mike Marciniszyn77241052015-07-30 15:17:43 -0400793 HFI1_RCVCTRL_ONE_PKT_EGR_DIS |
794 HFI1_RCVCTRL_NO_RHQ_DROP_DIS |
Michael J. Ruhl22505632017-07-24 07:46:06 -0700795 HFI1_RCVCTRL_NO_EGR_DROP_DIS, uctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400796 /* Clear the context's J_KEY */
Michael J. Ruhl17573972017-07-24 07:46:01 -0700797 hfi1_clear_ctxt_jkey(dd, uctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400798 /*
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700799 * If a send context is allocated, reset context integrity
800 * checks to default and disable the send context.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400801 */
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700802 if (uctxt->sc) {
803 set_pio_integrity(uctxt->sc);
804 sc_disable(uctxt->sc);
805 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400806 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
807
Mike Marciniszyn9c1a99c32017-06-09 15:59:40 -0700808 hfi1_free_ctxt_rcv_groups(uctxt);
Michael J. Ruhl637a9a72017-05-04 05:15:03 -0700809 hfi1_clear_ctxt_pkey(dd, uctxt);
Mitko Haralanov94158442016-04-20 06:05:36 -0700810
Mike Marciniszyn77241052015-07-30 15:17:43 -0400811 uctxt->event_flags = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400812 mutex_unlock(&hfi1_mutex);
Michael J. Ruhl42492012017-07-24 07:45:43 -0700813
814 deallocate_ctxt(uctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400815done:
Ira Weinye0cf75d2016-08-16 13:27:03 -0700816 mmdrop(fdata->mm);
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700817 kobject_put(&dd->kobj);
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -0700818
819 if (atomic_dec_and_test(&dd->user_refcount))
820 complete(&dd->user_comp);
821
Mike Marciniszyn77241052015-07-30 15:17:43 -0400822 kfree(fdata);
823 return 0;
824}
825
826/*
827 * Convert kernel *virtual* addresses to physical addresses.
828 * This is used to vmalloc'ed addresses.
829 */
830static u64 kvirt_to_phys(void *addr)
831{
832 struct page *page;
833 u64 paddr = 0;
834
835 page = vmalloc_to_page(addr);
836 if (page)
837 paddr = page_to_pfn(page) << PAGE_SHIFT;
838
839 return paddr;
840}
841
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700842static int assign_ctxt(struct hfi1_filedata *fd, struct hfi1_user_info *uinfo)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400843{
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700844 int ret;
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700845 unsigned int swmajor, swminor;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400846
847 swmajor = uinfo->userversion >> 16;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700848 if (swmajor != HFI1_USER_SWMAJOR)
849 return -ENODEV;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400850
851 swminor = uinfo->userversion & 0xffff;
852
Mike Marciniszyn77241052015-07-30 15:17:43 -0400853 mutex_lock(&hfi1_mutex);
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700854 /*
855 * Get a sub context if necessary.
856 * ret < 0 error, 0 no context, 1 sub-context found
857 */
858 ret = 0;
Mitko Haralanov957558c2016-02-03 14:33:40 -0800859 if (uinfo->subctxt_cnt) {
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700860 ret = find_sub_ctxt(fd, uinfo);
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700861 if (ret > 0)
Sebastian Sanchezb094a362016-07-25 07:54:57 -0700862 fd->rec_cpu_num =
863 hfi1_get_proc_affinity(fd->uctxt->numa_id);
Mitko Haralanov957558c2016-02-03 14:33:40 -0800864 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400865
866 /*
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700867 * Allocate a base context if context sharing is not required or we
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700868 * couldn't find a sub context.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400869 */
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700870 if (!ret)
871 ret = allocate_ctxt(fd, fd->dd, uinfo);
872
Mike Marciniszyn77241052015-07-30 15:17:43 -0400873 mutex_unlock(&hfi1_mutex);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700874
875 /* Depending on the context type, do the appropriate init */
876 if (ret > 0) {
877 /*
878 * sub-context info can only be set up after the base
879 * context has been completed.
880 */
881 ret = wait_event_interruptible(fd->uctxt->wait, !test_bit(
882 HFI1_CTXT_BASE_UNINIT,
883 &fd->uctxt->event_flags));
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700884 if (test_bit(HFI1_CTXT_BASE_FAILED, &fd->uctxt->event_flags))
885 ret = -ENOMEM;
886
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700887 /* The only thing a sub context needs is the user_xxx stuff */
888 if (!ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700889 ret = init_user_ctxt(fd);
890
Michael J. Ruhl42492012017-07-24 07:45:43 -0700891 if (ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700892 clear_bit(fd->subctxt, fd->uctxt->in_use_ctxts);
Michael J. Ruhl42492012017-07-24 07:45:43 -0700893
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700894 } else if (!ret) {
895 ret = setup_base_ctxt(fd);
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700896 if (fd->uctxt->subctxt_cnt) {
897 /* If there is an error, set the failed bit. */
898 if (ret)
899 set_bit(HFI1_CTXT_BASE_FAILED,
900 &fd->uctxt->event_flags);
901 /*
902 * Base context is done, notify anybody using a
903 * sub-context that is waiting for this completion
904 */
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700905 clear_bit(HFI1_CTXT_BASE_UNINIT,
906 &fd->uctxt->event_flags);
907 wake_up(&fd->uctxt->wait);
908 }
Michael J. Ruhl42492012017-07-24 07:45:43 -0700909 if (ret)
910 deallocate_ctxt(fd->uctxt);
911 }
912
913 /* If an error occurred, clear the reference */
914 if (ret && fd->uctxt) {
915 hfi1_rcd_put(fd->uctxt);
916 fd->uctxt = NULL;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700917 }
918
Mike Marciniszyn77241052015-07-30 15:17:43 -0400919 return ret;
920}
921
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700922/*
923 * The hfi1_mutex must be held when this function is called. It is
924 * necessary to ensure serialized access to the bitmask in_use_ctxts.
925 */
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700926static int find_sub_ctxt(struct hfi1_filedata *fd,
927 const struct hfi1_user_info *uinfo)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400928{
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700929 u16 i;
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700930 struct hfi1_devdata *dd = fd->dd;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700931 u16 subctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400932
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700933 for (i = dd->first_dyn_alloc_ctxt; i < dd->num_rcv_contexts; i++) {
934 struct hfi1_ctxtdata *uctxt = dd->rcd[i];
Mike Marciniszyn77241052015-07-30 15:17:43 -0400935
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700936 /* Skip ctxts which are not yet open */
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700937 if (!uctxt ||
938 bitmap_empty(uctxt->in_use_ctxts,
939 HFI1_MAX_SHARED_CTXTS))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400940 continue;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400941
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700942 /* Skip dynamically allocted kernel contexts */
943 if (uctxt->sc && (uctxt->sc->type == SC_KERNEL))
944 continue;
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700945
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700946 /* Skip ctxt if it doesn't match the requested one */
947 if (memcmp(uctxt->uuid, uinfo->uuid,
948 sizeof(uctxt->uuid)) ||
949 uctxt->jkey != generate_jkey(current_uid()) ||
950 uctxt->subctxt_id != uinfo->subctxt_id ||
951 uctxt->subctxt_cnt != uinfo->subctxt_cnt)
952 continue;
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700953
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700954 /* Verify the sharing process matches the master */
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700955 if (uctxt->userversion != uinfo->userversion)
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700956 return -EINVAL;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700957
958 /* Find an unused context */
959 subctxt = find_first_zero_bit(uctxt->in_use_ctxts,
960 HFI1_MAX_SHARED_CTXTS);
961 if (subctxt >= uctxt->subctxt_cnt)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700962 return -EBUSY;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700963
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700964 fd->uctxt = uctxt;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700965 fd->subctxt = subctxt;
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700966
967 hfi1_rcd_get(uctxt);
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700968 __set_bit(fd->subctxt, uctxt->in_use_ctxts);
969
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700970 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400971 }
972
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700973 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400974}
975
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700976static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400977 struct hfi1_user_info *uinfo)
978{
979 struct hfi1_ctxtdata *uctxt;
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700980 u16 ctxt;
Mitko Haralanov957558c2016-02-03 14:33:40 -0800981 int ret, numa;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400982
983 if (dd->flags & HFI1_FROZEN) {
984 /*
985 * Pick an error that is unique from all other errors
986 * that are returned so the user process knows that
987 * it tried to allocate while the SPC was frozen. It
988 * it should be able to retry with success in a short
989 * while.
990 */
991 return -EIO;
992 }
993
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700994 /*
995 * This check is sort of redundant to the next EBUSY error. It would
996 * also indicate an inconsistancy in the driver if this value was
997 * zero, but there were still contexts available.
998 */
999 if (!dd->freectxts)
1000 return -EBUSY;
1001
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001002 for (ctxt = dd->first_dyn_alloc_ctxt;
1003 ctxt < dd->num_rcv_contexts; ctxt++)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001004 if (!dd->rcd[ctxt])
1005 break;
1006
1007 if (ctxt == dd->num_rcv_contexts)
1008 return -EBUSY;
1009
Sebastian Sanchezb094a362016-07-25 07:54:57 -07001010 /*
1011 * If we don't have a NUMA node requested, preference is towards
1012 * device NUMA node.
1013 */
1014 fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node);
Mitko Haralanov957558c2016-02-03 14:33:40 -08001015 if (fd->rec_cpu_num != -1)
1016 numa = cpu_to_node(fd->rec_cpu_num);
1017 else
1018 numa = numa_node_id();
1019 uctxt = hfi1_create_ctxtdata(dd->pport, ctxt, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001020 if (!uctxt) {
1021 dd_dev_err(dd,
1022 "Unable to allocate ctxtdata memory, failing open\n");
1023 return -ENOMEM;
1024 }
Mitko Haralanov957558c2016-02-03 14:33:40 -08001025 hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)",
1026 uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num,
1027 uctxt->numa_id);
1028
Mike Marciniszyn77241052015-07-30 15:17:43 -04001029 /*
1030 * Allocate and enable a PIO send context.
1031 */
1032 uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize,
Mitko Haralanovcc572362016-02-03 14:33:49 -08001033 uctxt->dd->node);
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001034 if (!uctxt->sc) {
1035 ret = -ENOMEM;
1036 goto ctxdata_free;
1037 }
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001038 hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index,
1039 uctxt->sc->hw_context);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001040 ret = sc_enable(uctxt->sc);
1041 if (ret)
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001042 goto ctxdata_free;
1043
Mike Marciniszyn77241052015-07-30 15:17:43 -04001044 /*
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001045 * Setup sub context resources if the user-level has requested
1046 * sub contexts.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001047 * This has to be done here so the rest of the sub-contexts find the
1048 * proper master.
1049 */
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001050 if (uinfo->subctxt_cnt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001051 ret = init_subctxts(uctxt, uinfo);
1052 /*
1053 * On error, we don't need to disable and de-allocate the
1054 * send context because it will be done during file close
1055 */
1056 if (ret)
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001057 goto ctxdata_free;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001058 }
1059 uctxt->userversion = uinfo->userversion;
Dean Luickbdf77522016-07-28 15:21:13 -04001060 uctxt->flags = hfi1_cap_mask; /* save current flag state */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001061 init_waitqueue_head(&uctxt->wait);
1062 strlcpy(uctxt->comm, current->comm, sizeof(uctxt->comm));
1063 memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid));
1064 uctxt->jkey = generate_jkey(current_uid());
Mike Marciniszyn77241052015-07-30 15:17:43 -04001065 hfi1_stats.sps_ctxts++;
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -08001066 /*
1067 * Disable ASPM when there are open user/PSM contexts to avoid
1068 * issues with ASPM L1 exit latency
1069 */
1070 if (dd->freectxts-- == dd->num_user_contexts)
1071 aspm_disable_all(dd);
Ira Weiny9e10af42015-10-30 18:58:40 -04001072 fd->uctxt = uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001073
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001074 /* Count the reference for the fd */
1075 hfi1_rcd_get(uctxt);
1076
Mike Marciniszyn77241052015-07-30 15:17:43 -04001077 return 0;
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001078
1079ctxdata_free:
1080 dd->rcd[ctxt] = NULL;
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001081 hfi1_rcd_put(uctxt);
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001082 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001083}
1084
Michael J. Ruhl42492012017-07-24 07:45:43 -07001085static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt)
1086{
1087 mutex_lock(&hfi1_mutex);
1088 hfi1_stats.sps_ctxts--;
1089 if (++uctxt->dd->freectxts == uctxt->dd->num_user_contexts)
1090 aspm_enable_all(uctxt->dd);
1091
1092 /* _rcd_put() should be done after releasing mutex */
1093 uctxt->dd->rcd[uctxt->ctxt] = NULL;
1094 mutex_unlock(&hfi1_mutex);
1095 hfi1_rcd_put(uctxt); /* dd reference */
1096}
1097
Mike Marciniszyn77241052015-07-30 15:17:43 -04001098static int init_subctxts(struct hfi1_ctxtdata *uctxt,
1099 const struct hfi1_user_info *uinfo)
1100{
Michael J. Ruhl8737ce92017-05-04 05:15:15 -07001101 u16 num_subctxts;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001102
1103 num_subctxts = uinfo->subctxt_cnt;
Mitko Haralanovacac10f2016-02-05 11:57:50 -05001104 if (num_subctxts > HFI1_MAX_SHARED_CTXTS)
1105 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001106
1107 uctxt->subctxt_cnt = uinfo->subctxt_cnt;
1108 uctxt->subctxt_id = uinfo->subctxt_id;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001109 uctxt->redirect_seq_cnt = 1;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001110 set_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
Mitko Haralanovacac10f2016-02-05 11:57:50 -05001111
1112 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001113}
1114
1115static int setup_subctxt(struct hfi1_ctxtdata *uctxt)
1116{
1117 int ret = 0;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -07001118 u16 num_subctxts = uctxt->subctxt_cnt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001119
1120 uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001121 if (!uctxt->subctxt_uregbase)
1122 return -ENOMEM;
1123
Mike Marciniszyn77241052015-07-30 15:17:43 -04001124 /* We can take the size of the RcvHdr Queue from the master */
1125 uctxt->subctxt_rcvhdr_base = vmalloc_user(uctxt->rcvhdrq_size *
1126 num_subctxts);
1127 if (!uctxt->subctxt_rcvhdr_base) {
1128 ret = -ENOMEM;
1129 goto bail_ureg;
1130 }
1131
1132 uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size *
1133 num_subctxts);
1134 if (!uctxt->subctxt_rcvegrbuf) {
1135 ret = -ENOMEM;
1136 goto bail_rhdr;
1137 }
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001138
1139 return 0;
1140
Mike Marciniszyn77241052015-07-30 15:17:43 -04001141bail_rhdr:
1142 vfree(uctxt->subctxt_rcvhdr_base);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001143 uctxt->subctxt_rcvhdr_base = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001144bail_ureg:
1145 vfree(uctxt->subctxt_uregbase);
1146 uctxt->subctxt_uregbase = NULL;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001147
Mike Marciniszyn77241052015-07-30 15:17:43 -04001148 return ret;
1149}
1150
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001151static void user_init(struct hfi1_ctxtdata *uctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001152{
Mike Marciniszyn77241052015-07-30 15:17:43 -04001153 unsigned int rcvctrl_ops = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001154
1155 /* initialize poll variables... */
1156 uctxt->urgent = 0;
1157 uctxt->urgent_poll = 0;
1158
1159 /*
1160 * Now enable the ctxt for receive.
1161 * For chips that are set to DMA the tail register to memory
1162 * when they change (and when the update bit transitions from
1163 * 0 to 1. So for those chips, we turn it off and then back on.
1164 * This will (very briefly) affect any other open ctxts, but the
1165 * duration is very short, and therefore isn't an issue. We
1166 * explicitly set the in-memory tail copy to 0 beforehand, so we
1167 * don't have to wait to be sure the DMA update has happened
1168 * (chip resets head/tail to 0 on transition to enable).
1169 */
1170 if (uctxt->rcvhdrtail_kvaddr)
1171 clear_rcvhdrtail(uctxt);
1172
1173 /* Setup J_KEY before enabling the context */
Michael J. Ruhl17573972017-07-24 07:46:01 -07001174 hfi1_set_ctxt_jkey(uctxt->dd, uctxt, uctxt->jkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001175
1176 rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB;
Dean Luickbdf77522016-07-28 15:21:13 -04001177 if (HFI1_CAP_UGET_MASK(uctxt->flags, HDRSUPP))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001178 rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB;
1179 /*
1180 * Ignore the bit in the flags for now until proper
1181 * support for multiple packet per rcv array entry is
1182 * added.
1183 */
Dean Luickbdf77522016-07-28 15:21:13 -04001184 if (!HFI1_CAP_UGET_MASK(uctxt->flags, MULTI_PKT_EGR))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001185 rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
Dean Luickbdf77522016-07-28 15:21:13 -04001186 if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_EGR_FULL))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001187 rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
Dean Luickbdf77522016-07-28 15:21:13 -04001188 if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_RHQ_FULL))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001189 rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
Mitko Haralanov566c1572016-02-03 14:32:49 -08001190 /*
1191 * The RcvCtxtCtrl.TailUpd bit has to be explicitly written.
1192 * We can't rely on the correct value to be set from prior
1193 * uses of the chip or ctxt. Therefore, add the rcvctrl op
1194 * for both cases.
1195 */
Dean Luickbdf77522016-07-28 15:21:13 -04001196 if (HFI1_CAP_UGET_MASK(uctxt->flags, DMA_RTAIL))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001197 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB;
Mitko Haralanov566c1572016-02-03 14:32:49 -08001198 else
1199 rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS;
Michael J. Ruhl22505632017-07-24 07:46:06 -07001200 hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001201}
1202
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -07001203static int get_ctxt_info(struct hfi1_filedata *fd, void __user *ubase,
1204 __u32 len)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001205{
1206 struct hfi1_ctxt_info cinfo;
Ira Weiny9e10af42015-10-30 18:58:40 -04001207 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001208 int ret = 0;
1209
Dan Carpenterebe6b2e2015-09-16 09:42:25 +03001210 memset(&cinfo, 0, sizeof(cinfo));
Dean Luickbdf77522016-07-28 15:21:13 -04001211 cinfo.runtime_flags = (((uctxt->flags >> HFI1_CAP_MISC_SHIFT) &
1212 HFI1_CAP_MISC_MASK) << HFI1_CAP_USER_SHIFT) |
1213 HFI1_CAP_UGET_MASK(uctxt->flags, MASK) |
1214 HFI1_CAP_KGET_MASK(uctxt->flags, K2U);
Dean Luick622c2022016-07-28 15:21:21 -04001215 /* adjust flag if this fd is not able to cache */
1216 if (!fd->handler)
1217 cinfo.runtime_flags |= HFI1_CAP_TID_UNMAP; /* no caching */
1218
Mike Marciniszyn77241052015-07-30 15:17:43 -04001219 cinfo.num_active = hfi1_count_active_units();
1220 cinfo.unit = uctxt->dd->unit;
1221 cinfo.ctxt = uctxt->ctxt;
Ira Weiny9e10af42015-10-30 18:58:40 -04001222 cinfo.subctxt = fd->subctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001223 cinfo.rcvtids = roundup(uctxt->egrbufs.alloced,
1224 uctxt->dd->rcv_entries.group_size) +
1225 uctxt->expected_count;
1226 cinfo.credits = uctxt->sc->credits;
1227 cinfo.numa_node = uctxt->numa_id;
1228 cinfo.rec_cpu = fd->rec_cpu_num;
1229 cinfo.send_ctxt = uctxt->sc->hw_context;
1230
1231 cinfo.egrtids = uctxt->egrbufs.alloced;
1232 cinfo.rcvhdrq_cnt = uctxt->rcvhdrq_cnt;
1233 cinfo.rcvhdrq_entsize = uctxt->rcvhdrqentsize << 2;
Ira Weiny9e10af42015-10-30 18:58:40 -04001234 cinfo.sdma_ring_size = fd->cq->nentries;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001235 cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size;
1236
Ira Weiny9e10af42015-10-30 18:58:40 -04001237 trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, cinfo);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001238 if (copy_to_user(ubase, &cinfo, sizeof(cinfo)))
1239 ret = -EFAULT;
Dean Luickbdf77522016-07-28 15:21:13 -04001240
Mike Marciniszyn77241052015-07-30 15:17:43 -04001241 return ret;
1242}
1243
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001244static int init_user_ctxt(struct hfi1_filedata *fd)
1245{
1246 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1247 int ret;
1248
1249 ret = hfi1_user_sdma_alloc_queues(uctxt, fd);
1250 if (ret)
1251 return ret;
1252
1253 ret = hfi1_user_exp_rcv_init(fd);
1254
1255 return ret;
1256}
1257
1258static int setup_base_ctxt(struct hfi1_filedata *fd)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001259{
Ira Weiny9e10af42015-10-30 18:58:40 -04001260 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001261 struct hfi1_devdata *dd = uctxt->dd;
1262 int ret = 0;
1263
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001264 hfi1_init_ctxt(uctxt->sc);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001265
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001266 /* Now allocate the RcvHdr queue and eager buffers. */
1267 ret = hfi1_create_rcvhdrq(dd, uctxt);
Mitko Haralanov94158442016-04-20 06:05:36 -07001268 if (ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001269 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001270
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001271 ret = hfi1_setup_eagerbufs(uctxt);
1272 if (ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001273 goto setup_failed;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001274
1275 /* If sub-contexts are enabled, do the appropriate setup */
1276 if (uctxt->subctxt_cnt)
1277 ret = setup_subctxt(uctxt);
1278 if (ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001279 goto setup_failed;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001280
Mike Marciniszyn9c1a99c32017-06-09 15:59:40 -07001281 ret = hfi1_alloc_ctxt_rcv_groups(uctxt);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001282 if (ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001283 goto setup_failed;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001284
1285 ret = init_user_ctxt(fd);
1286 if (ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001287 goto setup_failed;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001288
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001289 user_init(uctxt);
1290
1291 return 0;
1292
1293setup_failed:
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001294 /* Call _free_ctxtdata, not _rcd_put(). We still need the context. */
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001295 hfi1_free_ctxtdata(dd, uctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001296 return ret;
1297}
1298
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -07001299static int get_base_info(struct hfi1_filedata *fd, void __user *ubase,
1300 __u32 len)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001301{
1302 struct hfi1_base_info binfo;
Ira Weiny9e10af42015-10-30 18:58:40 -04001303 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001304 struct hfi1_devdata *dd = uctxt->dd;
1305 ssize_t sz;
1306 unsigned offset;
1307 int ret = 0;
1308
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001309 trace_hfi1_uctxtdata(uctxt->dd, uctxt, fd->subctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001310
1311 memset(&binfo, 0, sizeof(binfo));
1312 binfo.hw_version = dd->revision;
1313 binfo.sw_version = HFI1_KERN_SWVERSION;
1314 binfo.bthqp = kdeth_qp;
1315 binfo.jkey = uctxt->jkey;
1316 /*
1317 * If more than 64 contexts are enabled the allocated credit
1318 * return will span two or three contiguous pages. Since we only
1319 * map the page containing the context's credit return address,
1320 * we need to calculate the offset in the proper page.
1321 */
1322 offset = ((u64)uctxt->sc->hw_free -
1323 (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE;
1324 binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001325 fd->subctxt, offset);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001326 binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001327 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001328 uctxt->sc->base_addr);
1329 binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP,
1330 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001331 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001332 uctxt->sc->base_addr);
1333 binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001334 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001335 uctxt->rcvhdrq);
1336 binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001337 fd->subctxt,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001338 uctxt->egrbufs.rcvtids[0].dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001339 binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001340 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001341 /*
1342 * user regs are at
1343 * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE))
1344 */
1345 binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001346 fd->subctxt, 0);
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001347 offset = offset_in_page((((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
Ira Weiny9e10af42015-10-30 18:58:40 -04001348 HFI1_MAX_SHARED_CTXTS) + fd->subctxt) *
Geliang Tange260e402015-10-03 10:34:59 +08001349 sizeof(*dd->events));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001350 binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001351 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001352 offset);
1353 binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001354 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001355 dd->status);
1356 if (HFI1_CAP_IS_USET(DMA_RTAIL))
1357 binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001358 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001359 if (uctxt->subctxt_cnt) {
1360 binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS,
1361 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001362 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001363 binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ,
1364 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001365 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001366 binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF,
1367 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001368 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001369 }
1370 sz = (len < sizeof(binfo)) ? len : sizeof(binfo);
1371 if (copy_to_user(ubase, &binfo, sz))
1372 ret = -EFAULT;
1373 return ret;
1374}
1375
1376static unsigned int poll_urgent(struct file *fp,
1377 struct poll_table_struct *pt)
1378{
Ira Weiny9e10af42015-10-30 18:58:40 -04001379 struct hfi1_filedata *fd = fp->private_data;
1380 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001381 struct hfi1_devdata *dd = uctxt->dd;
1382 unsigned pollflag;
1383
1384 poll_wait(fp, &uctxt->wait, pt);
1385
1386 spin_lock_irq(&dd->uctxt_lock);
1387 if (uctxt->urgent != uctxt->urgent_poll) {
1388 pollflag = POLLIN | POLLRDNORM;
1389 uctxt->urgent_poll = uctxt->urgent;
1390 } else {
1391 pollflag = 0;
1392 set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags);
1393 }
1394 spin_unlock_irq(&dd->uctxt_lock);
1395
1396 return pollflag;
1397}
1398
1399static unsigned int poll_next(struct file *fp,
1400 struct poll_table_struct *pt)
1401{
Ira Weiny9e10af42015-10-30 18:58:40 -04001402 struct hfi1_filedata *fd = fp->private_data;
1403 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001404 struct hfi1_devdata *dd = uctxt->dd;
1405 unsigned pollflag;
1406
1407 poll_wait(fp, &uctxt->wait, pt);
1408
1409 spin_lock_irq(&dd->uctxt_lock);
1410 if (hdrqempty(uctxt)) {
1411 set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags);
Michael J. Ruhl22505632017-07-24 07:46:06 -07001412 hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001413 pollflag = 0;
Jubin Johne4909742016-02-14 20:22:00 -08001414 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001415 pollflag = POLLIN | POLLRDNORM;
Jubin Johne4909742016-02-14 20:22:00 -08001416 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001417 spin_unlock_irq(&dd->uctxt_lock);
1418
1419 return pollflag;
1420}
1421
1422/*
1423 * Find all user contexts in use, and set the specified bit in their
1424 * event mask.
1425 * See also find_ctxt() for a similar use, that is specific to send buffers.
1426 */
1427int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit)
1428{
1429 struct hfi1_ctxtdata *uctxt;
1430 struct hfi1_devdata *dd = ppd->dd;
Michael J. Ruhle6f76222017-07-24 07:45:55 -07001431 u16 ctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001432 int ret = 0;
1433 unsigned long flags;
1434
1435 if (!dd->events) {
1436 ret = -EINVAL;
1437 goto done;
1438 }
1439
1440 spin_lock_irqsave(&dd->uctxt_lock, flags);
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001441 for (ctxt = dd->first_dyn_alloc_ctxt; ctxt < dd->num_rcv_contexts;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001442 ctxt++) {
1443 uctxt = dd->rcd[ctxt];
1444 if (uctxt) {
1445 unsigned long *evs = dd->events +
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001446 (uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
Mike Marciniszyn77241052015-07-30 15:17:43 -04001447 HFI1_MAX_SHARED_CTXTS;
1448 int i;
1449 /*
1450 * subctxt_cnt is 0 if not shared, so do base
1451 * separately, first, then remaining subctxt, if any
1452 */
1453 set_bit(evtbit, evs);
1454 for (i = 1; i < uctxt->subctxt_cnt; i++)
1455 set_bit(evtbit, evs + i);
1456 }
1457 }
1458 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1459done:
1460 return ret;
1461}
1462
1463/**
1464 * manage_rcvq - manage a context's receive queue
1465 * @uctxt: the context
1466 * @subctxt: the sub-context
1467 * @start_stop: action to carry out
1468 *
1469 * start_stop == 0 disables receive on the context, for use in queue
1470 * overflow conditions. start_stop==1 re-enables, to be used to
1471 * re-init the software copy of the head register
1472 */
Michael J. Ruhl8737ce92017-05-04 05:15:15 -07001473static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001474 int start_stop)
1475{
1476 struct hfi1_devdata *dd = uctxt->dd;
1477 unsigned int rcvctrl_op;
1478
1479 if (subctxt)
1480 goto bail;
1481 /* atomically clear receive enable ctxt. */
1482 if (start_stop) {
1483 /*
1484 * On enable, force in-memory copy of the tail register to
1485 * 0, so that protocol code doesn't have to worry about
1486 * whether or not the chip has yet updated the in-memory
1487 * copy or not on return from the system call. The chip
1488 * always resets it's tail register back to 0 on a
1489 * transition from disabled to enabled.
1490 */
1491 if (uctxt->rcvhdrtail_kvaddr)
1492 clear_rcvhdrtail(uctxt);
1493 rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB;
Jubin Johne4909742016-02-14 20:22:00 -08001494 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001495 rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS;
Jubin Johne4909742016-02-14 20:22:00 -08001496 }
Michael J. Ruhl22505632017-07-24 07:46:06 -07001497 hfi1_rcvctrl(dd, rcvctrl_op, uctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001498 /* always; new head should be equal to new tail; see above */
1499bail:
1500 return 0;
1501}
1502
1503/*
1504 * clear the event notifier events for this context.
1505 * User process then performs actions appropriate to bit having been
1506 * set, if desired, and checks again in future.
1507 */
Michael J. Ruhl8737ce92017-05-04 05:15:15 -07001508static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001509 unsigned long events)
1510{
1511 int i;
1512 struct hfi1_devdata *dd = uctxt->dd;
1513 unsigned long *evs;
1514
1515 if (!dd->events)
1516 return 0;
1517
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001518 evs = dd->events + ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
Mike Marciniszyn77241052015-07-30 15:17:43 -04001519 HFI1_MAX_SHARED_CTXTS) + subctxt;
1520
1521 for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) {
1522 if (!test_bit(i, &events))
1523 continue;
1524 clear_bit(i, evs);
1525 }
1526 return 0;
1527}
1528
Michael J. Ruhl8737ce92017-05-04 05:15:15 -07001529static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, u16 subctxt, u16 pkey)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001530{
1531 int ret = -ENOENT, i, intable = 0;
1532 struct hfi1_pportdata *ppd = uctxt->ppd;
1533 struct hfi1_devdata *dd = uctxt->dd;
1534
1535 if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY) {
1536 ret = -EINVAL;
1537 goto done;
1538 }
1539
1540 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++)
1541 if (pkey == ppd->pkeys[i]) {
1542 intable = 1;
1543 break;
1544 }
1545
1546 if (intable)
Michael J. Ruhl17573972017-07-24 07:46:01 -07001547 ret = hfi1_set_ctxt_pkey(dd, uctxt, pkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001548done:
1549 return ret;
1550}
1551
Mike Marciniszyn77241052015-07-30 15:17:43 -04001552static void user_remove(struct hfi1_devdata *dd)
1553{
Mike Marciniszyn77241052015-07-30 15:17:43 -04001554
1555 hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001556}
1557
1558static int user_add(struct hfi1_devdata *dd)
1559{
1560 char name[10];
1561 int ret;
1562
Mike Marciniszyn77241052015-07-30 15:17:43 -04001563 snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit);
Dennis Dalessandro0eb62652016-05-19 05:25:50 -07001564 ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops,
Ira Weinye116a642015-09-17 13:47:49 -04001565 &dd->user_cdev, &dd->user_device,
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001566 true, &dd->kobj);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001567 if (ret)
Dennis Dalessandro7312f292016-05-19 05:25:57 -07001568 user_remove(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001569
Mike Marciniszyn77241052015-07-30 15:17:43 -04001570 return ret;
1571}
1572
1573/*
1574 * Create per-unit files in /dev
1575 */
1576int hfi1_device_create(struct hfi1_devdata *dd)
1577{
Dennis Dalessandro0f7b1f92016-05-19 05:26:10 -07001578 return user_add(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001579}
1580
1581/*
1582 * Remove per-unit files in /dev
1583 * void, core kernel returns no errors for this stuff
1584 */
1585void hfi1_device_remove(struct hfi1_devdata *dd)
1586{
1587 user_remove(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001588}