blob: 2dd8758f0644b246179c0852fa0fe46883b4477a [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);
97static unsigned int poll_urgent(struct file *fp, struct poll_table_struct *pt);
98static unsigned int poll_next(struct file *fp, struct poll_table_struct *pt);
Michael J. Ruhl8737ce92017-05-04 05:15:15 -070099static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700100 unsigned long events);
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700101static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, u16 subctxt, u16 pkey);
102static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700103 int start_stop);
104static int vma_fault(struct vm_fault *vmf);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700105static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
106 unsigned long arg);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400107
108static const struct file_operations hfi1_file_ops = {
109 .owner = THIS_MODULE,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400110 .write_iter = hfi1_write_iter,
111 .open = hfi1_file_open,
112 .release = hfi1_file_close,
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700113 .unlocked_ioctl = hfi1_file_ioctl,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400114 .poll = hfi1_poll,
115 .mmap = hfi1_file_mmap,
116 .llseek = noop_llseek,
117};
118
119static struct vm_operations_struct vm_ops = {
120 .fault = vma_fault,
121};
122
123/*
124 * Types of memories mapped into user processes' space
125 */
126enum mmap_types {
127 PIO_BUFS = 1,
128 PIO_BUFS_SOP,
129 PIO_CRED,
130 RCV_HDRQ,
131 RCV_EGRBUF,
132 UREGS,
133 EVENTS,
134 STATUS,
135 RTAIL,
136 SUBCTXT_UREGS,
137 SUBCTXT_RCV_HDRQ,
138 SUBCTXT_EGRBUF,
139 SDMA_COMP
140};
141
142/*
143 * Masks and offsets defining the mmap tokens
144 */
145#define HFI1_MMAP_OFFSET_MASK 0xfffULL
146#define HFI1_MMAP_OFFSET_SHIFT 0
147#define HFI1_MMAP_SUBCTXT_MASK 0xfULL
148#define HFI1_MMAP_SUBCTXT_SHIFT 12
149#define HFI1_MMAP_CTXT_MASK 0xffULL
150#define HFI1_MMAP_CTXT_SHIFT 16
151#define HFI1_MMAP_TYPE_MASK 0xfULL
152#define HFI1_MMAP_TYPE_SHIFT 24
153#define HFI1_MMAP_MAGIC_MASK 0xffffffffULL
154#define HFI1_MMAP_MAGIC_SHIFT 32
155
156#define HFI1_MMAP_MAGIC 0xdabbad00
157
158#define HFI1_MMAP_TOKEN_SET(field, val) \
159 (((val) & HFI1_MMAP_##field##_MASK) << HFI1_MMAP_##field##_SHIFT)
160#define HFI1_MMAP_TOKEN_GET(field, token) \
161 (((token) >> HFI1_MMAP_##field##_SHIFT) & HFI1_MMAP_##field##_MASK)
162#define HFI1_MMAP_TOKEN(type, ctxt, subctxt, addr) \
163 (HFI1_MMAP_TOKEN_SET(MAGIC, HFI1_MMAP_MAGIC) | \
164 HFI1_MMAP_TOKEN_SET(TYPE, type) | \
165 HFI1_MMAP_TOKEN_SET(CTXT, ctxt) | \
166 HFI1_MMAP_TOKEN_SET(SUBCTXT, subctxt) | \
Geliang Tange260e402015-10-03 10:34:59 +0800167 HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr))))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400168
Mike Marciniszyn77241052015-07-30 15:17:43 -0400169#define dbg(fmt, ...) \
170 pr_info(fmt, ##__VA_ARGS__)
171
Mike Marciniszyn77241052015-07-30 15:17:43 -0400172static inline int is_valid_mmap(u64 token)
173{
174 return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC);
175}
176
177static int hfi1_file_open(struct inode *inode, struct file *fp)
178{
Ira Weinyea3a0ee2016-07-28 12:27:35 -0400179 struct hfi1_filedata *fd;
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700180 struct hfi1_devdata *dd = container_of(inode->i_cdev,
181 struct hfi1_devdata,
182 user_cdev);
183
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700184 if (!((dd->flags & HFI1_PRESENT) && dd->kregbase))
185 return -EINVAL;
186
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -0700187 if (!atomic_inc_not_zero(&dd->user_refcount))
188 return -ENXIO;
189
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700190 /* Just take a ref now. Not all opens result in a context assign */
191 kobject_get(&dd->kobj);
192
Mike Marciniszyn77241052015-07-30 15:17:43 -0400193 /* The real work is performed later in assign_ctxt() */
Ira Weinyea3a0ee2016-07-28 12:27:35 -0400194
195 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
196
Ira Weiny3faa3d92016-07-28 15:21:19 -0400197 if (fd) {
198 fd->rec_cpu_num = -1; /* no cpu affinity by default */
199 fd->mm = current->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -0800200 mmgrab(fd->mm);
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700201 fd->dd = dd;
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -0700202 fp->private_data = fd;
203 } else {
204 fp->private_data = NULL;
205
206 if (atomic_dec_and_test(&dd->user_refcount))
207 complete(&dd->user_comp);
208
209 return -ENOMEM;
Ira Weiny3faa3d92016-07-28 15:21:19 -0400210 }
Ira Weinyea3a0ee2016-07-28 12:27:35 -0400211
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -0700212 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400213}
214
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700215static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
216 unsigned long arg)
217{
218 struct hfi1_filedata *fd = fp->private_data;
219 struct hfi1_ctxtdata *uctxt = fd->uctxt;
220 struct hfi1_user_info uinfo;
221 struct hfi1_tid_info tinfo;
222 int ret = 0;
223 unsigned long addr;
224 int uval = 0;
225 unsigned long ul_uval = 0;
226 u16 uval16 = 0;
227
Dennis Dalessandro8a1882e2016-05-19 05:26:37 -0700228 hfi1_cdbg(IOCTL, "IOCTL recv: 0x%x", cmd);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700229 if (cmd != HFI1_IOCTL_ASSIGN_CTXT &&
230 cmd != HFI1_IOCTL_GET_VERS &&
231 !uctxt)
232 return -EINVAL;
233
234 switch (cmd) {
235 case HFI1_IOCTL_ASSIGN_CTXT:
Ira Weinyca2f30a2016-06-09 07:51:33 -0700236 if (uctxt)
237 return -EINVAL;
238
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700239 if (copy_from_user(&uinfo,
240 (struct hfi1_user_info __user *)arg,
241 sizeof(uinfo)))
242 return -EFAULT;
243
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700244 ret = assign_ctxt(fd, &uinfo);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700245 break;
246 case HFI1_IOCTL_CTXT_INFO:
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700247 ret = get_ctxt_info(fd, (void __user *)(unsigned long)arg,
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700248 sizeof(struct hfi1_ctxt_info));
249 break;
250 case HFI1_IOCTL_USER_INFO:
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700251 ret = get_base_info(fd, (void __user *)(unsigned long)arg,
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700252 sizeof(struct hfi1_base_info));
253 break;
254 case HFI1_IOCTL_CREDIT_UPD:
Markus Elfringf7ca5352016-07-23 08:30:52 +0200255 if (uctxt)
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700256 sc_return_credits(uctxt->sc);
257 break;
258
259 case HFI1_IOCTL_TID_UPDATE:
260 if (copy_from_user(&tinfo,
261 (struct hfi11_tid_info __user *)arg,
262 sizeof(tinfo)))
263 return -EFAULT;
264
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700265 ret = hfi1_user_exp_rcv_setup(fd, &tinfo);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700266 if (!ret) {
267 /*
268 * Copy the number of tidlist entries we used
269 * and the length of the buffer we registered.
270 * These fields are adjacent in the structure so
271 * we can copy them at the same time.
272 */
273 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
274 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
275 sizeof(tinfo.tidcnt) +
276 sizeof(tinfo.length)))
277 ret = -EFAULT;
278 }
279 break;
280
281 case HFI1_IOCTL_TID_FREE:
282 if (copy_from_user(&tinfo,
283 (struct hfi11_tid_info __user *)arg,
284 sizeof(tinfo)))
285 return -EFAULT;
286
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700287 ret = hfi1_user_exp_rcv_clear(fd, &tinfo);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700288 if (ret)
289 break;
290 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
291 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
292 sizeof(tinfo.tidcnt)))
293 ret = -EFAULT;
294 break;
295
296 case HFI1_IOCTL_TID_INVAL_READ:
297 if (copy_from_user(&tinfo,
298 (struct hfi11_tid_info __user *)arg,
299 sizeof(tinfo)))
300 return -EFAULT;
301
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700302 ret = hfi1_user_exp_rcv_invalid(fd, &tinfo);
Dennis Dalessandro8d970cf2016-05-19 05:26:24 -0700303 if (ret)
304 break;
305 addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
306 if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
307 sizeof(tinfo.tidcnt)))
308 ret = -EFAULT;
309 break;
310
311 case HFI1_IOCTL_RECV_CTRL:
312 ret = get_user(uval, (int __user *)arg);
313 if (ret != 0)
314 return -EFAULT;
315 ret = manage_rcvq(uctxt, fd->subctxt, uval);
316 break;
317
318 case HFI1_IOCTL_POLL_TYPE:
319 ret = get_user(uval, (int __user *)arg);
320 if (ret != 0)
321 return -EFAULT;
322 uctxt->poll_type = (typeof(uctxt->poll_type))uval;
323 break;
324
325 case HFI1_IOCTL_ACK_EVENT:
326 ret = get_user(ul_uval, (unsigned long __user *)arg);
327 if (ret != 0)
328 return -EFAULT;
329 ret = user_event_ack(uctxt, fd->subctxt, ul_uval);
330 break;
331
332 case HFI1_IOCTL_SET_PKEY:
333 ret = get_user(uval16, (u16 __user *)arg);
334 if (ret != 0)
335 return -EFAULT;
336 if (HFI1_CAP_IS_USET(PKEY_CHECK))
337 ret = set_ctxt_pkey(uctxt, fd->subctxt, uval16);
338 else
339 return -EPERM;
340 break;
341
342 case HFI1_IOCTL_CTXT_RESET: {
343 struct send_context *sc;
344 struct hfi1_devdata *dd;
345
346 if (!uctxt || !uctxt->dd || !uctxt->sc)
347 return -EINVAL;
348
349 /*
350 * There is no protection here. User level has to
351 * guarantee that no one will be writing to the send
352 * context while it is being re-initialized.
353 * If user level breaks that guarantee, it will break
354 * it's own context and no one else's.
355 */
356 dd = uctxt->dd;
357 sc = uctxt->sc;
358 /*
359 * Wait until the interrupt handler has marked the
360 * context as halted or frozen. Report error if we time
361 * out.
362 */
363 wait_event_interruptible_timeout(
364 sc->halt_wait, (sc->flags & SCF_HALTED),
365 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
366 if (!(sc->flags & SCF_HALTED))
367 return -ENOLCK;
368
369 /*
370 * If the send context was halted due to a Freeze,
371 * wait until the device has been "unfrozen" before
372 * resetting the context.
373 */
374 if (sc->flags & SCF_FROZEN) {
375 wait_event_interruptible_timeout(
376 dd->event_queue,
377 !(ACCESS_ONCE(dd->flags) & HFI1_FROZEN),
378 msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
379 if (dd->flags & HFI1_FROZEN)
380 return -ENOLCK;
381
382 if (dd->flags & HFI1_FORCED_FREEZE)
383 /*
384 * Don't allow context reset if we are into
385 * forced freeze
386 */
387 return -ENODEV;
388
389 sc_disable(sc);
390 ret = sc_enable(sc);
391 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB,
392 uctxt->ctxt);
393 } 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);
777 if (!bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400778 mutex_unlock(&hfi1_mutex);
779 goto done;
780 }
781
782 spin_lock_irqsave(&dd->uctxt_lock, flags);
783 /*
784 * Disable receive context and interrupt available, reset all
785 * RcvCtxtCtrl bits to default values.
786 */
787 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
788 HFI1_RCVCTRL_TIDFLOW_DIS |
789 HFI1_RCVCTRL_INTRAVAIL_DIS |
Mitko Haralanov566c1572016-02-03 14:32:49 -0800790 HFI1_RCVCTRL_TAILUPD_DIS |
Mike Marciniszyn77241052015-07-30 15:17:43 -0400791 HFI1_RCVCTRL_ONE_PKT_EGR_DIS |
792 HFI1_RCVCTRL_NO_RHQ_DROP_DIS |
793 HFI1_RCVCTRL_NO_EGR_DROP_DIS, uctxt->ctxt);
794 /* Clear the context's J_KEY */
795 hfi1_clear_ctxt_jkey(dd, uctxt->ctxt);
796 /*
797 * Reset context integrity checks to default.
798 * (writes to CSRs probably belong in chip.c)
799 */
800 write_kctxt_csr(dd, uctxt->sc->hw_context, SEND_CTXT_CHECK_ENABLE,
801 hfi1_pkt_default_send_ctxt_mask(dd, uctxt->sc->type));
802 sc_disable(uctxt->sc);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400803 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
804
805 dd->rcd[uctxt->ctxt] = NULL;
Mitko Haralanov94158442016-04-20 06:05:36 -0700806
Mike Marciniszyn9c1a99c32017-06-09 15:59:40 -0700807 hfi1_free_ctxt_rcv_groups(uctxt);
Michael J. Ruhl637a9a72017-05-04 05:15:03 -0700808 hfi1_clear_ctxt_pkey(dd, uctxt);
Mitko Haralanov94158442016-04-20 06:05:36 -0700809
Mike Marciniszyn77241052015-07-30 15:17:43 -0400810 uctxt->rcvwait_to = 0;
811 uctxt->piowait_to = 0;
812 uctxt->rcvnowait = 0;
813 uctxt->pionowait = 0;
814 uctxt->event_flags = 0;
815
Mike Marciniszyn77241052015-07-30 15:17:43 -0400816 hfi1_stats.sps_ctxts--;
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -0800817 if (++dd->freectxts == dd->num_user_contexts)
818 aspm_enable_all(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400819 mutex_unlock(&hfi1_mutex);
820 hfi1_free_ctxtdata(dd, uctxt);
821done:
Ira Weinye0cf75d2016-08-16 13:27:03 -0700822 mmdrop(fdata->mm);
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700823 kobject_put(&dd->kobj);
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -0700824
825 if (atomic_dec_and_test(&dd->user_refcount))
826 complete(&dd->user_comp);
827
Mike Marciniszyn77241052015-07-30 15:17:43 -0400828 kfree(fdata);
829 return 0;
830}
831
832/*
833 * Convert kernel *virtual* addresses to physical addresses.
834 * This is used to vmalloc'ed addresses.
835 */
836static u64 kvirt_to_phys(void *addr)
837{
838 struct page *page;
839 u64 paddr = 0;
840
841 page = vmalloc_to_page(addr);
842 if (page)
843 paddr = page_to_pfn(page) << PAGE_SHIFT;
844
845 return paddr;
846}
847
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700848static int assign_ctxt(struct hfi1_filedata *fd, struct hfi1_user_info *uinfo)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400849{
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700850 int ret;
Dennis Dalessandro0eb62652016-05-19 05:25:50 -0700851 unsigned int swmajor, swminor;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400852
853 swmajor = uinfo->userversion >> 16;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700854 if (swmajor != HFI1_USER_SWMAJOR)
855 return -ENODEV;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400856
857 swminor = uinfo->userversion & 0xffff;
858
Mike Marciniszyn77241052015-07-30 15:17:43 -0400859 mutex_lock(&hfi1_mutex);
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700860 /*
861 * Get a sub context if necessary.
862 * ret < 0 error, 0 no context, 1 sub-context found
863 */
864 ret = 0;
Mitko Haralanov957558c2016-02-03 14:33:40 -0800865 if (uinfo->subctxt_cnt) {
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700866 ret = find_sub_ctxt(fd, uinfo);
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700867 if (ret > 0)
Sebastian Sanchezb094a362016-07-25 07:54:57 -0700868 fd->rec_cpu_num =
869 hfi1_get_proc_affinity(fd->uctxt->numa_id);
Mitko Haralanov957558c2016-02-03 14:33:40 -0800870 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400871
872 /*
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700873 * Allocate a base context if context sharing is not required or we
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700874 * couldn't find a sub context.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400875 */
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700876 if (!ret)
877 ret = allocate_ctxt(fd, fd->dd, uinfo);
878
Mike Marciniszyn77241052015-07-30 15:17:43 -0400879 mutex_unlock(&hfi1_mutex);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700880
881 /* Depending on the context type, do the appropriate init */
882 if (ret > 0) {
883 /*
884 * sub-context info can only be set up after the base
885 * context has been completed.
886 */
887 ret = wait_event_interruptible(fd->uctxt->wait, !test_bit(
888 HFI1_CTXT_BASE_UNINIT,
889 &fd->uctxt->event_flags));
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700890 if (test_bit(HFI1_CTXT_BASE_FAILED, &fd->uctxt->event_flags)) {
891 clear_bit(fd->subctxt, fd->uctxt->in_use_ctxts);
892 return -ENOMEM;
893 }
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700894 /* The only thing a sub context needs is the user_xxx stuff */
895 if (!ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700896 ret = init_user_ctxt(fd);
897
898 if (ret)
899 clear_bit(fd->subctxt, fd->uctxt->in_use_ctxts);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700900 } else if (!ret) {
901 ret = setup_base_ctxt(fd);
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700902 if (fd->uctxt->subctxt_cnt) {
903 /* If there is an error, set the failed bit. */
904 if (ret)
905 set_bit(HFI1_CTXT_BASE_FAILED,
906 &fd->uctxt->event_flags);
907 /*
908 * Base context is done, notify anybody using a
909 * sub-context that is waiting for this completion
910 */
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700911 clear_bit(HFI1_CTXT_BASE_UNINIT,
912 &fd->uctxt->event_flags);
913 wake_up(&fd->uctxt->wait);
914 }
915 }
916
Mike Marciniszyn77241052015-07-30 15:17:43 -0400917 return ret;
918}
919
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700920/*
921 * The hfi1_mutex must be held when this function is called. It is
922 * necessary to ensure serialized access to the bitmask in_use_ctxts.
923 */
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700924static int find_sub_ctxt(struct hfi1_filedata *fd,
925 const struct hfi1_user_info *uinfo)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400926{
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700927 int i;
928 struct hfi1_devdata *dd = fd->dd;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700929 u16 subctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400930
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700931 for (i = dd->first_dyn_alloc_ctxt; i < dd->num_rcv_contexts; i++) {
932 struct hfi1_ctxtdata *uctxt = dd->rcd[i];
Mike Marciniszyn77241052015-07-30 15:17:43 -0400933
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700934 /* Skip ctxts which are not yet open */
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700935 if (!uctxt ||
936 bitmap_empty(uctxt->in_use_ctxts,
937 HFI1_MAX_SHARED_CTXTS))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400938 continue;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400939
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700940 /* Skip dynamically allocted kernel contexts */
941 if (uctxt->sc && (uctxt->sc->type == SC_KERNEL))
942 continue;
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700943
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700944 /* Skip ctxt if it doesn't match the requested one */
945 if (memcmp(uctxt->uuid, uinfo->uuid,
946 sizeof(uctxt->uuid)) ||
947 uctxt->jkey != generate_jkey(current_uid()) ||
948 uctxt->subctxt_id != uinfo->subctxt_id ||
949 uctxt->subctxt_cnt != uinfo->subctxt_cnt)
950 continue;
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700951
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700952 /* Verify the sharing process matches the master */
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700953 if (uctxt->userversion != uinfo->userversion)
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700954 return -EINVAL;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700955
956 /* Find an unused context */
957 subctxt = find_first_zero_bit(uctxt->in_use_ctxts,
958 HFI1_MAX_SHARED_CTXTS);
959 if (subctxt >= uctxt->subctxt_cnt)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700960 return -EBUSY;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700961
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700962 fd->uctxt = uctxt;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700963 fd->subctxt = subctxt;
964 __set_bit(fd->subctxt, uctxt->in_use_ctxts);
965
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700966 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400967 }
968
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700969 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400970}
971
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700972static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400973 struct hfi1_user_info *uinfo)
974{
975 struct hfi1_ctxtdata *uctxt;
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700976 unsigned int ctxt;
Mitko Haralanov957558c2016-02-03 14:33:40 -0800977 int ret, numa;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400978
979 if (dd->flags & HFI1_FROZEN) {
980 /*
981 * Pick an error that is unique from all other errors
982 * that are returned so the user process knows that
983 * it tried to allocate while the SPC was frozen. It
984 * it should be able to retry with success in a short
985 * while.
986 */
987 return -EIO;
988 }
989
Michael J. Ruhl5fbded42017-05-04 05:14:57 -0700990 /*
991 * This check is sort of redundant to the next EBUSY error. It would
992 * also indicate an inconsistancy in the driver if this value was
993 * zero, but there were still contexts available.
994 */
995 if (!dd->freectxts)
996 return -EBUSY;
997
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700998 for (ctxt = dd->first_dyn_alloc_ctxt;
999 ctxt < dd->num_rcv_contexts; ctxt++)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001000 if (!dd->rcd[ctxt])
1001 break;
1002
1003 if (ctxt == dd->num_rcv_contexts)
1004 return -EBUSY;
1005
Sebastian Sanchezb094a362016-07-25 07:54:57 -07001006 /*
1007 * If we don't have a NUMA node requested, preference is towards
1008 * device NUMA node.
1009 */
1010 fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node);
Mitko Haralanov957558c2016-02-03 14:33:40 -08001011 if (fd->rec_cpu_num != -1)
1012 numa = cpu_to_node(fd->rec_cpu_num);
1013 else
1014 numa = numa_node_id();
1015 uctxt = hfi1_create_ctxtdata(dd->pport, ctxt, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001016 if (!uctxt) {
1017 dd_dev_err(dd,
1018 "Unable to allocate ctxtdata memory, failing open\n");
1019 return -ENOMEM;
1020 }
Mitko Haralanov957558c2016-02-03 14:33:40 -08001021 hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)",
1022 uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num,
1023 uctxt->numa_id);
1024
Mike Marciniszyn77241052015-07-30 15:17:43 -04001025 /*
1026 * Allocate and enable a PIO send context.
1027 */
1028 uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize,
Mitko Haralanovcc572362016-02-03 14:33:49 -08001029 uctxt->dd->node);
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001030 if (!uctxt->sc) {
1031 ret = -ENOMEM;
1032 goto ctxdata_free;
1033 }
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001034 hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index,
1035 uctxt->sc->hw_context);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001036 ret = sc_enable(uctxt->sc);
1037 if (ret)
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001038 goto ctxdata_free;
1039
Mike Marciniszyn77241052015-07-30 15:17:43 -04001040 /*
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001041 * Setup sub context resources if the user-level has requested
1042 * sub contexts.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001043 * This has to be done here so the rest of the sub-contexts find the
1044 * proper master.
1045 */
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001046 if (uinfo->subctxt_cnt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001047 ret = init_subctxts(uctxt, uinfo);
1048 /*
1049 * On error, we don't need to disable and de-allocate the
1050 * send context because it will be done during file close
1051 */
1052 if (ret)
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001053 goto ctxdata_free;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001054 }
1055 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());
1061 INIT_LIST_HEAD(&uctxt->sdma_queues);
1062 spin_lock_init(&uctxt->sdma_qlock);
1063 hfi1_stats.sps_ctxts++;
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -08001064 /*
1065 * Disable ASPM when there are open user/PSM contexts to avoid
1066 * issues with ASPM L1 exit latency
1067 */
1068 if (dd->freectxts-- == dd->num_user_contexts)
1069 aspm_disable_all(dd);
Ira Weiny9e10af42015-10-30 18:58:40 -04001070 fd->uctxt = uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001071
1072 return 0;
Jakub Pawlak3a6982d2016-09-25 07:42:23 -07001073
1074ctxdata_free:
1075 dd->rcd[ctxt] = NULL;
1076 hfi1_free_ctxtdata(dd, uctxt);
1077 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001078}
1079
1080static int init_subctxts(struct hfi1_ctxtdata *uctxt,
1081 const struct hfi1_user_info *uinfo)
1082{
Michael J. Ruhl8737ce92017-05-04 05:15:15 -07001083 u16 num_subctxts;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001084
1085 num_subctxts = uinfo->subctxt_cnt;
Mitko Haralanovacac10f2016-02-05 11:57:50 -05001086 if (num_subctxts > HFI1_MAX_SHARED_CTXTS)
1087 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001088
1089 uctxt->subctxt_cnt = uinfo->subctxt_cnt;
1090 uctxt->subctxt_id = uinfo->subctxt_id;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001091 uctxt->redirect_seq_cnt = 1;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001092 set_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
Mitko Haralanovacac10f2016-02-05 11:57:50 -05001093
1094 return 0;
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 */
1156 hfi1_set_ctxt_jkey(uctxt->dd, uctxt->ctxt, uctxt->jkey);
1157
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;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001182 hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt->ctxt);
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. Ruhl9b60d2c2017-05-04 05:15:09 -07001226static int init_user_ctxt(struct hfi1_filedata *fd)
1227{
1228 struct hfi1_ctxtdata *uctxt = fd->uctxt;
1229 int ret;
1230
1231 ret = hfi1_user_sdma_alloc_queues(uctxt, fd);
1232 if (ret)
1233 return ret;
1234
1235 ret = hfi1_user_exp_rcv_init(fd);
1236
1237 return ret;
1238}
1239
1240static int setup_base_ctxt(struct hfi1_filedata *fd)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001241{
Ira Weiny9e10af42015-10-30 18:58:40 -04001242 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001243 struct hfi1_devdata *dd = uctxt->dd;
1244 int ret = 0;
1245
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001246 hfi1_init_ctxt(uctxt->sc);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001247
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001248 /* Now allocate the RcvHdr queue and eager buffers. */
1249 ret = hfi1_create_rcvhdrq(dd, uctxt);
Mitko Haralanov94158442016-04-20 06:05:36 -07001250 if (ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001251 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001252
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001253 ret = hfi1_setup_eagerbufs(uctxt);
1254 if (ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001255 goto setup_failed;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001256
1257 /* If sub-contexts are enabled, do the appropriate setup */
1258 if (uctxt->subctxt_cnt)
1259 ret = setup_subctxt(uctxt);
1260 if (ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001261 goto setup_failed;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001262
Mike Marciniszyn9c1a99c32017-06-09 15:59:40 -07001263 ret = hfi1_alloc_ctxt_rcv_groups(uctxt);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001264 if (ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001265 goto setup_failed;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001266
1267 ret = init_user_ctxt(fd);
1268 if (ret)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001269 goto setup_failed;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001270
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001271 user_init(uctxt);
1272
1273 return 0;
1274
1275setup_failed:
1276 hfi1_free_ctxtdata(dd, uctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001277 return ret;
1278}
1279
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -07001280static int get_base_info(struct hfi1_filedata *fd, void __user *ubase,
1281 __u32 len)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001282{
1283 struct hfi1_base_info binfo;
Ira Weiny9e10af42015-10-30 18:58:40 -04001284 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001285 struct hfi1_devdata *dd = uctxt->dd;
1286 ssize_t sz;
1287 unsigned offset;
1288 int ret = 0;
1289
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -07001290 trace_hfi1_uctxtdata(uctxt->dd, uctxt, fd->subctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001291
1292 memset(&binfo, 0, sizeof(binfo));
1293 binfo.hw_version = dd->revision;
1294 binfo.sw_version = HFI1_KERN_SWVERSION;
1295 binfo.bthqp = kdeth_qp;
1296 binfo.jkey = uctxt->jkey;
1297 /*
1298 * If more than 64 contexts are enabled the allocated credit
1299 * return will span two or three contiguous pages. Since we only
1300 * map the page containing the context's credit return address,
1301 * we need to calculate the offset in the proper page.
1302 */
1303 offset = ((u64)uctxt->sc->hw_free -
1304 (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE;
1305 binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001306 fd->subctxt, offset);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001307 binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001308 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001309 uctxt->sc->base_addr);
1310 binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP,
1311 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001312 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001313 uctxt->sc->base_addr);
1314 binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001315 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001316 uctxt->rcvhdrq);
1317 binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001318 fd->subctxt,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001319 uctxt->egrbufs.rcvtids[0].dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001320 binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001321 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001322 /*
1323 * user regs are at
1324 * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE))
1325 */
1326 binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001327 fd->subctxt, 0);
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001328 offset = offset_in_page((((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
Ira Weiny9e10af42015-10-30 18:58:40 -04001329 HFI1_MAX_SHARED_CTXTS) + fd->subctxt) *
Geliang Tange260e402015-10-03 10:34:59 +08001330 sizeof(*dd->events));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001331 binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001332 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001333 offset);
1334 binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001335 fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001336 dd->status);
1337 if (HFI1_CAP_IS_USET(DMA_RTAIL))
1338 binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001339 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001340 if (uctxt->subctxt_cnt) {
1341 binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS,
1342 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001343 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001344 binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ,
1345 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001346 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001347 binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF,
1348 uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -04001349 fd->subctxt, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001350 }
1351 sz = (len < sizeof(binfo)) ? len : sizeof(binfo);
1352 if (copy_to_user(ubase, &binfo, sz))
1353 ret = -EFAULT;
1354 return ret;
1355}
1356
1357static unsigned int poll_urgent(struct file *fp,
1358 struct poll_table_struct *pt)
1359{
Ira Weiny9e10af42015-10-30 18:58:40 -04001360 struct hfi1_filedata *fd = fp->private_data;
1361 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001362 struct hfi1_devdata *dd = uctxt->dd;
1363 unsigned pollflag;
1364
1365 poll_wait(fp, &uctxt->wait, pt);
1366
1367 spin_lock_irq(&dd->uctxt_lock);
1368 if (uctxt->urgent != uctxt->urgent_poll) {
1369 pollflag = POLLIN | POLLRDNORM;
1370 uctxt->urgent_poll = uctxt->urgent;
1371 } else {
1372 pollflag = 0;
1373 set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags);
1374 }
1375 spin_unlock_irq(&dd->uctxt_lock);
1376
1377 return pollflag;
1378}
1379
1380static unsigned int poll_next(struct file *fp,
1381 struct poll_table_struct *pt)
1382{
Ira Weiny9e10af42015-10-30 18:58:40 -04001383 struct hfi1_filedata *fd = fp->private_data;
1384 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001385 struct hfi1_devdata *dd = uctxt->dd;
1386 unsigned pollflag;
1387
1388 poll_wait(fp, &uctxt->wait, pt);
1389
1390 spin_lock_irq(&dd->uctxt_lock);
1391 if (hdrqempty(uctxt)) {
1392 set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags);
1393 hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt->ctxt);
1394 pollflag = 0;
Jubin Johne4909742016-02-14 20:22:00 -08001395 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001396 pollflag = POLLIN | POLLRDNORM;
Jubin Johne4909742016-02-14 20:22:00 -08001397 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001398 spin_unlock_irq(&dd->uctxt_lock);
1399
1400 return pollflag;
1401}
1402
1403/*
1404 * Find all user contexts in use, and set the specified bit in their
1405 * event mask.
1406 * See also find_ctxt() for a similar use, that is specific to send buffers.
1407 */
1408int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit)
1409{
1410 struct hfi1_ctxtdata *uctxt;
1411 struct hfi1_devdata *dd = ppd->dd;
1412 unsigned ctxt;
1413 int ret = 0;
1414 unsigned long flags;
1415
1416 if (!dd->events) {
1417 ret = -EINVAL;
1418 goto done;
1419 }
1420
1421 spin_lock_irqsave(&dd->uctxt_lock, flags);
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001422 for (ctxt = dd->first_dyn_alloc_ctxt; ctxt < dd->num_rcv_contexts;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001423 ctxt++) {
1424 uctxt = dd->rcd[ctxt];
1425 if (uctxt) {
1426 unsigned long *evs = dd->events +
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001427 (uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
Mike Marciniszyn77241052015-07-30 15:17:43 -04001428 HFI1_MAX_SHARED_CTXTS;
1429 int i;
1430 /*
1431 * subctxt_cnt is 0 if not shared, so do base
1432 * separately, first, then remaining subctxt, if any
1433 */
1434 set_bit(evtbit, evs);
1435 for (i = 1; i < uctxt->subctxt_cnt; i++)
1436 set_bit(evtbit, evs + i);
1437 }
1438 }
1439 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1440done:
1441 return ret;
1442}
1443
1444/**
1445 * manage_rcvq - manage a context's receive queue
1446 * @uctxt: the context
1447 * @subctxt: the sub-context
1448 * @start_stop: action to carry out
1449 *
1450 * start_stop == 0 disables receive on the context, for use in queue
1451 * overflow conditions. start_stop==1 re-enables, to be used to
1452 * re-init the software copy of the head register
1453 */
Michael J. Ruhl8737ce92017-05-04 05:15:15 -07001454static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001455 int start_stop)
1456{
1457 struct hfi1_devdata *dd = uctxt->dd;
1458 unsigned int rcvctrl_op;
1459
1460 if (subctxt)
1461 goto bail;
1462 /* atomically clear receive enable ctxt. */
1463 if (start_stop) {
1464 /*
1465 * On enable, force in-memory copy of the tail register to
1466 * 0, so that protocol code doesn't have to worry about
1467 * whether or not the chip has yet updated the in-memory
1468 * copy or not on return from the system call. The chip
1469 * always resets it's tail register back to 0 on a
1470 * transition from disabled to enabled.
1471 */
1472 if (uctxt->rcvhdrtail_kvaddr)
1473 clear_rcvhdrtail(uctxt);
1474 rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB;
Jubin Johne4909742016-02-14 20:22:00 -08001475 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001476 rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS;
Jubin Johne4909742016-02-14 20:22:00 -08001477 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001478 hfi1_rcvctrl(dd, rcvctrl_op, uctxt->ctxt);
1479 /* always; new head should be equal to new tail; see above */
1480bail:
1481 return 0;
1482}
1483
1484/*
1485 * clear the event notifier events for this context.
1486 * User process then performs actions appropriate to bit having been
1487 * set, if desired, and checks again in future.
1488 */
Michael J. Ruhl8737ce92017-05-04 05:15:15 -07001489static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001490 unsigned long events)
1491{
1492 int i;
1493 struct hfi1_devdata *dd = uctxt->dd;
1494 unsigned long *evs;
1495
1496 if (!dd->events)
1497 return 0;
1498
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001499 evs = dd->events + ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) *
Mike Marciniszyn77241052015-07-30 15:17:43 -04001500 HFI1_MAX_SHARED_CTXTS) + subctxt;
1501
1502 for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) {
1503 if (!test_bit(i, &events))
1504 continue;
1505 clear_bit(i, evs);
1506 }
1507 return 0;
1508}
1509
Michael J. Ruhl8737ce92017-05-04 05:15:15 -07001510static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, u16 subctxt, u16 pkey)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001511{
1512 int ret = -ENOENT, i, intable = 0;
1513 struct hfi1_pportdata *ppd = uctxt->ppd;
1514 struct hfi1_devdata *dd = uctxt->dd;
1515
1516 if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY) {
1517 ret = -EINVAL;
1518 goto done;
1519 }
1520
1521 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++)
1522 if (pkey == ppd->pkeys[i]) {
1523 intable = 1;
1524 break;
1525 }
1526
1527 if (intable)
1528 ret = hfi1_set_ctxt_pkey(dd, uctxt->ctxt, pkey);
1529done:
1530 return ret;
1531}
1532
Mike Marciniszyn77241052015-07-30 15:17:43 -04001533static void user_remove(struct hfi1_devdata *dd)
1534{
Mike Marciniszyn77241052015-07-30 15:17:43 -04001535
1536 hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001537}
1538
1539static int user_add(struct hfi1_devdata *dd)
1540{
1541 char name[10];
1542 int ret;
1543
Mike Marciniszyn77241052015-07-30 15:17:43 -04001544 snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit);
Dennis Dalessandro0eb62652016-05-19 05:25:50 -07001545 ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops,
Ira Weinye116a642015-09-17 13:47:49 -04001546 &dd->user_cdev, &dd->user_device,
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001547 true, &dd->kobj);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001548 if (ret)
Dennis Dalessandro7312f292016-05-19 05:25:57 -07001549 user_remove(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001550
Mike Marciniszyn77241052015-07-30 15:17:43 -04001551 return ret;
1552}
1553
1554/*
1555 * Create per-unit files in /dev
1556 */
1557int hfi1_device_create(struct hfi1_devdata *dd)
1558{
Dennis Dalessandro0f7b1f92016-05-19 05:26:10 -07001559 return user_add(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001560}
1561
1562/*
1563 * Remove per-unit files in /dev
1564 * void, core kernel returns no errors for this stuff
1565 */
1566void hfi1_device_remove(struct hfi1_devdata *dd)
1567{
1568 user_remove(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001569}