1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright(c) 2020 Cornelis Networks, Inc.
3*4882a593Smuzhiyun * Copyright(c) 2015-2020 Intel Corporation.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This file is provided under a dual BSD/GPLv2 license. When using or
6*4882a593Smuzhiyun * redistributing this file, you may do so under either license.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * GPL LICENSE SUMMARY
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
11*4882a593Smuzhiyun * it under the terms of version 2 of the GNU General Public License as
12*4882a593Smuzhiyun * published by the Free Software Foundation.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * This program is distributed in the hope that it will be useful, but
15*4882a593Smuzhiyun * WITHOUT ANY WARRANTY; without even the implied warranty of
16*4882a593Smuzhiyun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17*4882a593Smuzhiyun * General Public License for more details.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * BSD LICENSE
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
22*4882a593Smuzhiyun * modification, are permitted provided that the following conditions
23*4882a593Smuzhiyun * are met:
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * - Redistributions of source code must retain the above copyright
26*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer.
27*4882a593Smuzhiyun * - Redistributions in binary form must reproduce the above copyright
28*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in
29*4882a593Smuzhiyun * the documentation and/or other materials provided with the
30*4882a593Smuzhiyun * distribution.
31*4882a593Smuzhiyun * - Neither the name of Intel Corporation nor the names of its
32*4882a593Smuzhiyun * contributors may be used to endorse or promote products derived
33*4882a593Smuzhiyun * from this software without specific prior written permission.
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36*4882a593Smuzhiyun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37*4882a593Smuzhiyun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38*4882a593Smuzhiyun * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39*4882a593Smuzhiyun * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40*4882a593Smuzhiyun * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41*4882a593Smuzhiyun * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42*4882a593Smuzhiyun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43*4882a593Smuzhiyun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45*4882a593Smuzhiyun * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46*4882a593Smuzhiyun *
47*4882a593Smuzhiyun */
48*4882a593Smuzhiyun #include <linux/poll.h>
49*4882a593Smuzhiyun #include <linux/cdev.h>
50*4882a593Smuzhiyun #include <linux/vmalloc.h>
51*4882a593Smuzhiyun #include <linux/io.h>
52*4882a593Smuzhiyun #include <linux/sched/mm.h>
53*4882a593Smuzhiyun #include <linux/bitmap.h>
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun #include <rdma/ib.h>
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun #include "hfi.h"
58*4882a593Smuzhiyun #include "pio.h"
59*4882a593Smuzhiyun #include "device.h"
60*4882a593Smuzhiyun #include "common.h"
61*4882a593Smuzhiyun #include "trace.h"
62*4882a593Smuzhiyun #include "mmu_rb.h"
63*4882a593Smuzhiyun #include "user_sdma.h"
64*4882a593Smuzhiyun #include "user_exp_rcv.h"
65*4882a593Smuzhiyun #include "aspm.h"
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun #undef pr_fmt
68*4882a593Smuzhiyun #define pr_fmt(fmt) DRIVER_NAME ": " fmt
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun #define SEND_CTXT_HALT_TIMEOUT 1000 /* msecs */
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /*
73*4882a593Smuzhiyun * File operation functions
74*4882a593Smuzhiyun */
75*4882a593Smuzhiyun static int hfi1_file_open(struct inode *inode, struct file *fp);
76*4882a593Smuzhiyun static int hfi1_file_close(struct inode *inode, struct file *fp);
77*4882a593Smuzhiyun static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from);
78*4882a593Smuzhiyun static __poll_t hfi1_poll(struct file *fp, struct poll_table_struct *pt);
79*4882a593Smuzhiyun static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma);
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun static u64 kvirt_to_phys(void *addr);
82*4882a593Smuzhiyun static int assign_ctxt(struct hfi1_filedata *fd, unsigned long arg, u32 len);
83*4882a593Smuzhiyun static void init_subctxts(struct hfi1_ctxtdata *uctxt,
84*4882a593Smuzhiyun const struct hfi1_user_info *uinfo);
85*4882a593Smuzhiyun static int init_user_ctxt(struct hfi1_filedata *fd,
86*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt);
87*4882a593Smuzhiyun static void user_init(struct hfi1_ctxtdata *uctxt);
88*4882a593Smuzhiyun static int get_ctxt_info(struct hfi1_filedata *fd, unsigned long arg, u32 len);
89*4882a593Smuzhiyun static int get_base_info(struct hfi1_filedata *fd, unsigned long arg, u32 len);
90*4882a593Smuzhiyun static int user_exp_rcv_setup(struct hfi1_filedata *fd, unsigned long arg,
91*4882a593Smuzhiyun u32 len);
92*4882a593Smuzhiyun static int user_exp_rcv_clear(struct hfi1_filedata *fd, unsigned long arg,
93*4882a593Smuzhiyun u32 len);
94*4882a593Smuzhiyun static int user_exp_rcv_invalid(struct hfi1_filedata *fd, unsigned long arg,
95*4882a593Smuzhiyun u32 len);
96*4882a593Smuzhiyun static int setup_base_ctxt(struct hfi1_filedata *fd,
97*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt);
98*4882a593Smuzhiyun static int setup_subctxt(struct hfi1_ctxtdata *uctxt);
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun static int find_sub_ctxt(struct hfi1_filedata *fd,
101*4882a593Smuzhiyun const struct hfi1_user_info *uinfo);
102*4882a593Smuzhiyun static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
103*4882a593Smuzhiyun struct hfi1_user_info *uinfo,
104*4882a593Smuzhiyun struct hfi1_ctxtdata **cd);
105*4882a593Smuzhiyun static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt);
106*4882a593Smuzhiyun static __poll_t poll_urgent(struct file *fp, struct poll_table_struct *pt);
107*4882a593Smuzhiyun static __poll_t poll_next(struct file *fp, struct poll_table_struct *pt);
108*4882a593Smuzhiyun static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
109*4882a593Smuzhiyun unsigned long arg);
110*4882a593Smuzhiyun static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned long arg);
111*4882a593Smuzhiyun static int ctxt_reset(struct hfi1_ctxtdata *uctxt);
112*4882a593Smuzhiyun static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
113*4882a593Smuzhiyun unsigned long arg);
114*4882a593Smuzhiyun static vm_fault_t vma_fault(struct vm_fault *vmf);
115*4882a593Smuzhiyun static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
116*4882a593Smuzhiyun unsigned long arg);
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun static const struct file_operations hfi1_file_ops = {
119*4882a593Smuzhiyun .owner = THIS_MODULE,
120*4882a593Smuzhiyun .write_iter = hfi1_write_iter,
121*4882a593Smuzhiyun .open = hfi1_file_open,
122*4882a593Smuzhiyun .release = hfi1_file_close,
123*4882a593Smuzhiyun .unlocked_ioctl = hfi1_file_ioctl,
124*4882a593Smuzhiyun .poll = hfi1_poll,
125*4882a593Smuzhiyun .mmap = hfi1_file_mmap,
126*4882a593Smuzhiyun .llseek = noop_llseek,
127*4882a593Smuzhiyun };
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun static const struct vm_operations_struct vm_ops = {
130*4882a593Smuzhiyun .fault = vma_fault,
131*4882a593Smuzhiyun };
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /*
134*4882a593Smuzhiyun * Types of memories mapped into user processes' space
135*4882a593Smuzhiyun */
136*4882a593Smuzhiyun enum mmap_types {
137*4882a593Smuzhiyun PIO_BUFS = 1,
138*4882a593Smuzhiyun PIO_BUFS_SOP,
139*4882a593Smuzhiyun PIO_CRED,
140*4882a593Smuzhiyun RCV_HDRQ,
141*4882a593Smuzhiyun RCV_EGRBUF,
142*4882a593Smuzhiyun UREGS,
143*4882a593Smuzhiyun EVENTS,
144*4882a593Smuzhiyun STATUS,
145*4882a593Smuzhiyun RTAIL,
146*4882a593Smuzhiyun SUBCTXT_UREGS,
147*4882a593Smuzhiyun SUBCTXT_RCV_HDRQ,
148*4882a593Smuzhiyun SUBCTXT_EGRBUF,
149*4882a593Smuzhiyun SDMA_COMP
150*4882a593Smuzhiyun };
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /*
153*4882a593Smuzhiyun * Masks and offsets defining the mmap tokens
154*4882a593Smuzhiyun */
155*4882a593Smuzhiyun #define HFI1_MMAP_OFFSET_MASK 0xfffULL
156*4882a593Smuzhiyun #define HFI1_MMAP_OFFSET_SHIFT 0
157*4882a593Smuzhiyun #define HFI1_MMAP_SUBCTXT_MASK 0xfULL
158*4882a593Smuzhiyun #define HFI1_MMAP_SUBCTXT_SHIFT 12
159*4882a593Smuzhiyun #define HFI1_MMAP_CTXT_MASK 0xffULL
160*4882a593Smuzhiyun #define HFI1_MMAP_CTXT_SHIFT 16
161*4882a593Smuzhiyun #define HFI1_MMAP_TYPE_MASK 0xfULL
162*4882a593Smuzhiyun #define HFI1_MMAP_TYPE_SHIFT 24
163*4882a593Smuzhiyun #define HFI1_MMAP_MAGIC_MASK 0xffffffffULL
164*4882a593Smuzhiyun #define HFI1_MMAP_MAGIC_SHIFT 32
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun #define HFI1_MMAP_MAGIC 0xdabbad00
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun #define HFI1_MMAP_TOKEN_SET(field, val) \
169*4882a593Smuzhiyun (((val) & HFI1_MMAP_##field##_MASK) << HFI1_MMAP_##field##_SHIFT)
170*4882a593Smuzhiyun #define HFI1_MMAP_TOKEN_GET(field, token) \
171*4882a593Smuzhiyun (((token) >> HFI1_MMAP_##field##_SHIFT) & HFI1_MMAP_##field##_MASK)
172*4882a593Smuzhiyun #define HFI1_MMAP_TOKEN(type, ctxt, subctxt, addr) \
173*4882a593Smuzhiyun (HFI1_MMAP_TOKEN_SET(MAGIC, HFI1_MMAP_MAGIC) | \
174*4882a593Smuzhiyun HFI1_MMAP_TOKEN_SET(TYPE, type) | \
175*4882a593Smuzhiyun HFI1_MMAP_TOKEN_SET(CTXT, ctxt) | \
176*4882a593Smuzhiyun HFI1_MMAP_TOKEN_SET(SUBCTXT, subctxt) | \
177*4882a593Smuzhiyun HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr))))
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun #define dbg(fmt, ...) \
180*4882a593Smuzhiyun pr_info(fmt, ##__VA_ARGS__)
181*4882a593Smuzhiyun
is_valid_mmap(u64 token)182*4882a593Smuzhiyun static inline int is_valid_mmap(u64 token)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC);
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
hfi1_file_open(struct inode * inode,struct file * fp)187*4882a593Smuzhiyun static int hfi1_file_open(struct inode *inode, struct file *fp)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun struct hfi1_filedata *fd;
190*4882a593Smuzhiyun struct hfi1_devdata *dd = container_of(inode->i_cdev,
191*4882a593Smuzhiyun struct hfi1_devdata,
192*4882a593Smuzhiyun user_cdev);
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun if (!((dd->flags & HFI1_PRESENT) && dd->kregbase1))
195*4882a593Smuzhiyun return -EINVAL;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun if (!atomic_inc_not_zero(&dd->user_refcount))
198*4882a593Smuzhiyun return -ENXIO;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /* The real work is performed later in assign_ctxt() */
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun fd = kzalloc(sizeof(*fd), GFP_KERNEL);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun if (!fd || init_srcu_struct(&fd->pq_srcu))
205*4882a593Smuzhiyun goto nomem;
206*4882a593Smuzhiyun spin_lock_init(&fd->pq_rcu_lock);
207*4882a593Smuzhiyun spin_lock_init(&fd->tid_lock);
208*4882a593Smuzhiyun spin_lock_init(&fd->invalid_lock);
209*4882a593Smuzhiyun fd->rec_cpu_num = -1; /* no cpu affinity by default */
210*4882a593Smuzhiyun fd->dd = dd;
211*4882a593Smuzhiyun fp->private_data = fd;
212*4882a593Smuzhiyun return 0;
213*4882a593Smuzhiyun nomem:
214*4882a593Smuzhiyun kfree(fd);
215*4882a593Smuzhiyun fp->private_data = NULL;
216*4882a593Smuzhiyun if (atomic_dec_and_test(&dd->user_refcount))
217*4882a593Smuzhiyun complete(&dd->user_comp);
218*4882a593Smuzhiyun return -ENOMEM;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
hfi1_file_ioctl(struct file * fp,unsigned int cmd,unsigned long arg)221*4882a593Smuzhiyun static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
222*4882a593Smuzhiyun unsigned long arg)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun struct hfi1_filedata *fd = fp->private_data;
225*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt = fd->uctxt;
226*4882a593Smuzhiyun int ret = 0;
227*4882a593Smuzhiyun int uval = 0;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun hfi1_cdbg(IOCTL, "IOCTL recv: 0x%x", cmd);
230*4882a593Smuzhiyun if (cmd != HFI1_IOCTL_ASSIGN_CTXT &&
231*4882a593Smuzhiyun cmd != HFI1_IOCTL_GET_VERS &&
232*4882a593Smuzhiyun !uctxt)
233*4882a593Smuzhiyun return -EINVAL;
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun switch (cmd) {
236*4882a593Smuzhiyun case HFI1_IOCTL_ASSIGN_CTXT:
237*4882a593Smuzhiyun ret = assign_ctxt(fd, arg, _IOC_SIZE(cmd));
238*4882a593Smuzhiyun break;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun case HFI1_IOCTL_CTXT_INFO:
241*4882a593Smuzhiyun ret = get_ctxt_info(fd, arg, _IOC_SIZE(cmd));
242*4882a593Smuzhiyun break;
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun case HFI1_IOCTL_USER_INFO:
245*4882a593Smuzhiyun ret = get_base_info(fd, arg, _IOC_SIZE(cmd));
246*4882a593Smuzhiyun break;
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun case HFI1_IOCTL_CREDIT_UPD:
249*4882a593Smuzhiyun if (uctxt)
250*4882a593Smuzhiyun sc_return_credits(uctxt->sc);
251*4882a593Smuzhiyun break;
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun case HFI1_IOCTL_TID_UPDATE:
254*4882a593Smuzhiyun ret = user_exp_rcv_setup(fd, arg, _IOC_SIZE(cmd));
255*4882a593Smuzhiyun break;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun case HFI1_IOCTL_TID_FREE:
258*4882a593Smuzhiyun ret = user_exp_rcv_clear(fd, arg, _IOC_SIZE(cmd));
259*4882a593Smuzhiyun break;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun case HFI1_IOCTL_TID_INVAL_READ:
262*4882a593Smuzhiyun ret = user_exp_rcv_invalid(fd, arg, _IOC_SIZE(cmd));
263*4882a593Smuzhiyun break;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun case HFI1_IOCTL_RECV_CTRL:
266*4882a593Smuzhiyun ret = manage_rcvq(uctxt, fd->subctxt, arg);
267*4882a593Smuzhiyun break;
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun case HFI1_IOCTL_POLL_TYPE:
270*4882a593Smuzhiyun if (get_user(uval, (int __user *)arg))
271*4882a593Smuzhiyun return -EFAULT;
272*4882a593Smuzhiyun uctxt->poll_type = (typeof(uctxt->poll_type))uval;
273*4882a593Smuzhiyun break;
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun case HFI1_IOCTL_ACK_EVENT:
276*4882a593Smuzhiyun ret = user_event_ack(uctxt, fd->subctxt, arg);
277*4882a593Smuzhiyun break;
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun case HFI1_IOCTL_SET_PKEY:
280*4882a593Smuzhiyun ret = set_ctxt_pkey(uctxt, arg);
281*4882a593Smuzhiyun break;
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun case HFI1_IOCTL_CTXT_RESET:
284*4882a593Smuzhiyun ret = ctxt_reset(uctxt);
285*4882a593Smuzhiyun break;
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun case HFI1_IOCTL_GET_VERS:
288*4882a593Smuzhiyun uval = HFI1_USER_SWVERSION;
289*4882a593Smuzhiyun if (put_user(uval, (int __user *)arg))
290*4882a593Smuzhiyun return -EFAULT;
291*4882a593Smuzhiyun break;
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun default:
294*4882a593Smuzhiyun return -EINVAL;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun return ret;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
hfi1_write_iter(struct kiocb * kiocb,struct iov_iter * from)300*4882a593Smuzhiyun static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun struct hfi1_filedata *fd = kiocb->ki_filp->private_data;
303*4882a593Smuzhiyun struct hfi1_user_sdma_pkt_q *pq;
304*4882a593Smuzhiyun struct hfi1_user_sdma_comp_q *cq = fd->cq;
305*4882a593Smuzhiyun int done = 0, reqs = 0;
306*4882a593Smuzhiyun unsigned long dim = from->nr_segs;
307*4882a593Smuzhiyun int idx;
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun if (!HFI1_CAP_IS_KSET(SDMA))
310*4882a593Smuzhiyun return -EINVAL;
311*4882a593Smuzhiyun idx = srcu_read_lock(&fd->pq_srcu);
312*4882a593Smuzhiyun pq = srcu_dereference(fd->pq, &fd->pq_srcu);
313*4882a593Smuzhiyun if (!cq || !pq) {
314*4882a593Smuzhiyun srcu_read_unlock(&fd->pq_srcu, idx);
315*4882a593Smuzhiyun return -EIO;
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun if (!iter_is_iovec(from) || !dim) {
319*4882a593Smuzhiyun srcu_read_unlock(&fd->pq_srcu, idx);
320*4882a593Smuzhiyun return -EINVAL;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun trace_hfi1_sdma_request(fd->dd, fd->uctxt->ctxt, fd->subctxt, dim);
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun if (atomic_read(&pq->n_reqs) == pq->n_max_reqs) {
326*4882a593Smuzhiyun srcu_read_unlock(&fd->pq_srcu, idx);
327*4882a593Smuzhiyun return -ENOSPC;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun while (dim) {
331*4882a593Smuzhiyun int ret;
332*4882a593Smuzhiyun unsigned long count = 0;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun ret = hfi1_user_sdma_process_request(
335*4882a593Smuzhiyun fd, (struct iovec *)(from->iov + done),
336*4882a593Smuzhiyun dim, &count);
337*4882a593Smuzhiyun if (ret) {
338*4882a593Smuzhiyun reqs = ret;
339*4882a593Smuzhiyun break;
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun dim -= count;
342*4882a593Smuzhiyun done += count;
343*4882a593Smuzhiyun reqs++;
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun srcu_read_unlock(&fd->pq_srcu, idx);
347*4882a593Smuzhiyun return reqs;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun
hfi1_file_mmap(struct file * fp,struct vm_area_struct * vma)350*4882a593Smuzhiyun static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma)
351*4882a593Smuzhiyun {
352*4882a593Smuzhiyun struct hfi1_filedata *fd = fp->private_data;
353*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt = fd->uctxt;
354*4882a593Smuzhiyun struct hfi1_devdata *dd;
355*4882a593Smuzhiyun unsigned long flags;
356*4882a593Smuzhiyun u64 token = vma->vm_pgoff << PAGE_SHIFT,
357*4882a593Smuzhiyun memaddr = 0;
358*4882a593Smuzhiyun void *memvirt = NULL;
359*4882a593Smuzhiyun u8 subctxt, mapio = 0, vmf = 0, type;
360*4882a593Smuzhiyun ssize_t memlen = 0;
361*4882a593Smuzhiyun int ret = 0;
362*4882a593Smuzhiyun u16 ctxt;
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun if (!is_valid_mmap(token) || !uctxt ||
365*4882a593Smuzhiyun !(vma->vm_flags & VM_SHARED)) {
366*4882a593Smuzhiyun ret = -EINVAL;
367*4882a593Smuzhiyun goto done;
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun dd = uctxt->dd;
370*4882a593Smuzhiyun ctxt = HFI1_MMAP_TOKEN_GET(CTXT, token);
371*4882a593Smuzhiyun subctxt = HFI1_MMAP_TOKEN_GET(SUBCTXT, token);
372*4882a593Smuzhiyun type = HFI1_MMAP_TOKEN_GET(TYPE, token);
373*4882a593Smuzhiyun if (ctxt != uctxt->ctxt || subctxt != fd->subctxt) {
374*4882a593Smuzhiyun ret = -EINVAL;
375*4882a593Smuzhiyun goto done;
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun flags = vma->vm_flags;
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun switch (type) {
381*4882a593Smuzhiyun case PIO_BUFS:
382*4882a593Smuzhiyun case PIO_BUFS_SOP:
383*4882a593Smuzhiyun memaddr = ((dd->physaddr + TXE_PIO_SEND) +
384*4882a593Smuzhiyun /* chip pio base */
385*4882a593Smuzhiyun (uctxt->sc->hw_context * BIT(16))) +
386*4882a593Smuzhiyun /* 64K PIO space / ctxt */
387*4882a593Smuzhiyun (type == PIO_BUFS_SOP ?
388*4882a593Smuzhiyun (TXE_PIO_SIZE / 2) : 0); /* sop? */
389*4882a593Smuzhiyun /*
390*4882a593Smuzhiyun * Map only the amount allocated to the context, not the
391*4882a593Smuzhiyun * entire available context's PIO space.
392*4882a593Smuzhiyun */
393*4882a593Smuzhiyun memlen = PAGE_ALIGN(uctxt->sc->credits * PIO_BLOCK_SIZE);
394*4882a593Smuzhiyun flags &= ~VM_MAYREAD;
395*4882a593Smuzhiyun flags |= VM_DONTCOPY | VM_DONTEXPAND;
396*4882a593Smuzhiyun vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
397*4882a593Smuzhiyun mapio = 1;
398*4882a593Smuzhiyun break;
399*4882a593Smuzhiyun case PIO_CRED:
400*4882a593Smuzhiyun if (flags & VM_WRITE) {
401*4882a593Smuzhiyun ret = -EPERM;
402*4882a593Smuzhiyun goto done;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun /*
405*4882a593Smuzhiyun * The credit return location for this context could be on the
406*4882a593Smuzhiyun * second or third page allocated for credit returns (if number
407*4882a593Smuzhiyun * of enabled contexts > 64 and 128 respectively).
408*4882a593Smuzhiyun */
409*4882a593Smuzhiyun memvirt = dd->cr_base[uctxt->numa_id].va;
410*4882a593Smuzhiyun memaddr = virt_to_phys(memvirt) +
411*4882a593Smuzhiyun (((u64)uctxt->sc->hw_free -
412*4882a593Smuzhiyun (u64)dd->cr_base[uctxt->numa_id].va) & PAGE_MASK);
413*4882a593Smuzhiyun memlen = PAGE_SIZE;
414*4882a593Smuzhiyun flags &= ~VM_MAYWRITE;
415*4882a593Smuzhiyun flags |= VM_DONTCOPY | VM_DONTEXPAND;
416*4882a593Smuzhiyun /*
417*4882a593Smuzhiyun * The driver has already allocated memory for credit
418*4882a593Smuzhiyun * returns and programmed it into the chip. Has that
419*4882a593Smuzhiyun * memory been flagged as non-cached?
420*4882a593Smuzhiyun */
421*4882a593Smuzhiyun /* vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); */
422*4882a593Smuzhiyun mapio = 1;
423*4882a593Smuzhiyun break;
424*4882a593Smuzhiyun case RCV_HDRQ:
425*4882a593Smuzhiyun memlen = rcvhdrq_size(uctxt);
426*4882a593Smuzhiyun memvirt = uctxt->rcvhdrq;
427*4882a593Smuzhiyun break;
428*4882a593Smuzhiyun case RCV_EGRBUF: {
429*4882a593Smuzhiyun unsigned long addr;
430*4882a593Smuzhiyun int i;
431*4882a593Smuzhiyun /*
432*4882a593Smuzhiyun * The RcvEgr buffer need to be handled differently
433*4882a593Smuzhiyun * as multiple non-contiguous pages need to be mapped
434*4882a593Smuzhiyun * into the user process.
435*4882a593Smuzhiyun */
436*4882a593Smuzhiyun memlen = uctxt->egrbufs.size;
437*4882a593Smuzhiyun if ((vma->vm_end - vma->vm_start) != memlen) {
438*4882a593Smuzhiyun dd_dev_err(dd, "Eager buffer map size invalid (%lu != %lu)\n",
439*4882a593Smuzhiyun (vma->vm_end - vma->vm_start), memlen);
440*4882a593Smuzhiyun ret = -EINVAL;
441*4882a593Smuzhiyun goto done;
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun if (vma->vm_flags & VM_WRITE) {
444*4882a593Smuzhiyun ret = -EPERM;
445*4882a593Smuzhiyun goto done;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun vma->vm_flags &= ~VM_MAYWRITE;
448*4882a593Smuzhiyun addr = vma->vm_start;
449*4882a593Smuzhiyun for (i = 0 ; i < uctxt->egrbufs.numbufs; i++) {
450*4882a593Smuzhiyun memlen = uctxt->egrbufs.buffers[i].len;
451*4882a593Smuzhiyun memvirt = uctxt->egrbufs.buffers[i].addr;
452*4882a593Smuzhiyun ret = remap_pfn_range(
453*4882a593Smuzhiyun vma, addr,
454*4882a593Smuzhiyun /*
455*4882a593Smuzhiyun * virt_to_pfn() does the same, but
456*4882a593Smuzhiyun * it's not available on x86_64
457*4882a593Smuzhiyun * when CONFIG_MMU is enabled.
458*4882a593Smuzhiyun */
459*4882a593Smuzhiyun PFN_DOWN(__pa(memvirt)),
460*4882a593Smuzhiyun memlen,
461*4882a593Smuzhiyun vma->vm_page_prot);
462*4882a593Smuzhiyun if (ret < 0)
463*4882a593Smuzhiyun goto done;
464*4882a593Smuzhiyun addr += memlen;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun ret = 0;
467*4882a593Smuzhiyun goto done;
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun case UREGS:
470*4882a593Smuzhiyun /*
471*4882a593Smuzhiyun * Map only the page that contains this context's user
472*4882a593Smuzhiyun * registers.
473*4882a593Smuzhiyun */
474*4882a593Smuzhiyun memaddr = (unsigned long)
475*4882a593Smuzhiyun (dd->physaddr + RXE_PER_CONTEXT_USER)
476*4882a593Smuzhiyun + (uctxt->ctxt * RXE_PER_CONTEXT_SIZE);
477*4882a593Smuzhiyun /*
478*4882a593Smuzhiyun * TidFlow table is on the same page as the rest of the
479*4882a593Smuzhiyun * user registers.
480*4882a593Smuzhiyun */
481*4882a593Smuzhiyun memlen = PAGE_SIZE;
482*4882a593Smuzhiyun flags |= VM_DONTCOPY | VM_DONTEXPAND;
483*4882a593Smuzhiyun vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
484*4882a593Smuzhiyun mapio = 1;
485*4882a593Smuzhiyun break;
486*4882a593Smuzhiyun case EVENTS:
487*4882a593Smuzhiyun /*
488*4882a593Smuzhiyun * Use the page where this context's flags are. User level
489*4882a593Smuzhiyun * knows where it's own bitmap is within the page.
490*4882a593Smuzhiyun */
491*4882a593Smuzhiyun memaddr = (unsigned long)
492*4882a593Smuzhiyun (dd->events + uctxt_offset(uctxt)) & PAGE_MASK;
493*4882a593Smuzhiyun memlen = PAGE_SIZE;
494*4882a593Smuzhiyun /*
495*4882a593Smuzhiyun * v3.7 removes VM_RESERVED but the effect is kept by
496*4882a593Smuzhiyun * using VM_IO.
497*4882a593Smuzhiyun */
498*4882a593Smuzhiyun flags |= VM_IO | VM_DONTEXPAND;
499*4882a593Smuzhiyun vmf = 1;
500*4882a593Smuzhiyun break;
501*4882a593Smuzhiyun case STATUS:
502*4882a593Smuzhiyun if (flags & VM_WRITE) {
503*4882a593Smuzhiyun ret = -EPERM;
504*4882a593Smuzhiyun goto done;
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun memaddr = kvirt_to_phys((void *)dd->status);
507*4882a593Smuzhiyun memlen = PAGE_SIZE;
508*4882a593Smuzhiyun flags |= VM_IO | VM_DONTEXPAND;
509*4882a593Smuzhiyun break;
510*4882a593Smuzhiyun case RTAIL:
511*4882a593Smuzhiyun if (!HFI1_CAP_IS_USET(DMA_RTAIL)) {
512*4882a593Smuzhiyun /*
513*4882a593Smuzhiyun * If the memory allocation failed, the context alloc
514*4882a593Smuzhiyun * also would have failed, so we would never get here
515*4882a593Smuzhiyun */
516*4882a593Smuzhiyun ret = -EINVAL;
517*4882a593Smuzhiyun goto done;
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun if ((flags & VM_WRITE) || !hfi1_rcvhdrtail_kvaddr(uctxt)) {
520*4882a593Smuzhiyun ret = -EPERM;
521*4882a593Smuzhiyun goto done;
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun memlen = PAGE_SIZE;
524*4882a593Smuzhiyun memvirt = (void *)hfi1_rcvhdrtail_kvaddr(uctxt);
525*4882a593Smuzhiyun flags &= ~VM_MAYWRITE;
526*4882a593Smuzhiyun break;
527*4882a593Smuzhiyun case SUBCTXT_UREGS:
528*4882a593Smuzhiyun memaddr = (u64)uctxt->subctxt_uregbase;
529*4882a593Smuzhiyun memlen = PAGE_SIZE;
530*4882a593Smuzhiyun flags |= VM_IO | VM_DONTEXPAND;
531*4882a593Smuzhiyun vmf = 1;
532*4882a593Smuzhiyun break;
533*4882a593Smuzhiyun case SUBCTXT_RCV_HDRQ:
534*4882a593Smuzhiyun memaddr = (u64)uctxt->subctxt_rcvhdr_base;
535*4882a593Smuzhiyun memlen = rcvhdrq_size(uctxt) * uctxt->subctxt_cnt;
536*4882a593Smuzhiyun flags |= VM_IO | VM_DONTEXPAND;
537*4882a593Smuzhiyun vmf = 1;
538*4882a593Smuzhiyun break;
539*4882a593Smuzhiyun case SUBCTXT_EGRBUF:
540*4882a593Smuzhiyun memaddr = (u64)uctxt->subctxt_rcvegrbuf;
541*4882a593Smuzhiyun memlen = uctxt->egrbufs.size * uctxt->subctxt_cnt;
542*4882a593Smuzhiyun flags |= VM_IO | VM_DONTEXPAND;
543*4882a593Smuzhiyun flags &= ~VM_MAYWRITE;
544*4882a593Smuzhiyun vmf = 1;
545*4882a593Smuzhiyun break;
546*4882a593Smuzhiyun case SDMA_COMP: {
547*4882a593Smuzhiyun struct hfi1_user_sdma_comp_q *cq = fd->cq;
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun if (!cq) {
550*4882a593Smuzhiyun ret = -EFAULT;
551*4882a593Smuzhiyun goto done;
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun memaddr = (u64)cq->comps;
554*4882a593Smuzhiyun memlen = PAGE_ALIGN(sizeof(*cq->comps) * cq->nentries);
555*4882a593Smuzhiyun flags |= VM_IO | VM_DONTEXPAND;
556*4882a593Smuzhiyun vmf = 1;
557*4882a593Smuzhiyun break;
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun default:
560*4882a593Smuzhiyun ret = -EINVAL;
561*4882a593Smuzhiyun break;
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun if ((vma->vm_end - vma->vm_start) != memlen) {
565*4882a593Smuzhiyun hfi1_cdbg(PROC, "%u:%u Memory size mismatch %lu:%lu",
566*4882a593Smuzhiyun uctxt->ctxt, fd->subctxt,
567*4882a593Smuzhiyun (vma->vm_end - vma->vm_start), memlen);
568*4882a593Smuzhiyun ret = -EINVAL;
569*4882a593Smuzhiyun goto done;
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun vma->vm_flags = flags;
573*4882a593Smuzhiyun hfi1_cdbg(PROC,
574*4882a593Smuzhiyun "%u:%u type:%u io/vf:%d/%d, addr:0x%llx, len:%lu(%lu), flags:0x%lx\n",
575*4882a593Smuzhiyun ctxt, subctxt, type, mapio, vmf, memaddr, memlen,
576*4882a593Smuzhiyun vma->vm_end - vma->vm_start, vma->vm_flags);
577*4882a593Smuzhiyun if (vmf) {
578*4882a593Smuzhiyun vma->vm_pgoff = PFN_DOWN(memaddr);
579*4882a593Smuzhiyun vma->vm_ops = &vm_ops;
580*4882a593Smuzhiyun ret = 0;
581*4882a593Smuzhiyun } else if (mapio) {
582*4882a593Smuzhiyun ret = io_remap_pfn_range(vma, vma->vm_start,
583*4882a593Smuzhiyun PFN_DOWN(memaddr),
584*4882a593Smuzhiyun memlen,
585*4882a593Smuzhiyun vma->vm_page_prot);
586*4882a593Smuzhiyun } else if (memvirt) {
587*4882a593Smuzhiyun ret = remap_pfn_range(vma, vma->vm_start,
588*4882a593Smuzhiyun PFN_DOWN(__pa(memvirt)),
589*4882a593Smuzhiyun memlen,
590*4882a593Smuzhiyun vma->vm_page_prot);
591*4882a593Smuzhiyun } else {
592*4882a593Smuzhiyun ret = remap_pfn_range(vma, vma->vm_start,
593*4882a593Smuzhiyun PFN_DOWN(memaddr),
594*4882a593Smuzhiyun memlen,
595*4882a593Smuzhiyun vma->vm_page_prot);
596*4882a593Smuzhiyun }
597*4882a593Smuzhiyun done:
598*4882a593Smuzhiyun return ret;
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun /*
602*4882a593Smuzhiyun * Local (non-chip) user memory is not mapped right away but as it is
603*4882a593Smuzhiyun * accessed by the user-level code.
604*4882a593Smuzhiyun */
vma_fault(struct vm_fault * vmf)605*4882a593Smuzhiyun static vm_fault_t vma_fault(struct vm_fault *vmf)
606*4882a593Smuzhiyun {
607*4882a593Smuzhiyun struct page *page;
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
610*4882a593Smuzhiyun if (!page)
611*4882a593Smuzhiyun return VM_FAULT_SIGBUS;
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun get_page(page);
614*4882a593Smuzhiyun vmf->page = page;
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun return 0;
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun
hfi1_poll(struct file * fp,struct poll_table_struct * pt)619*4882a593Smuzhiyun static __poll_t hfi1_poll(struct file *fp, struct poll_table_struct *pt)
620*4882a593Smuzhiyun {
621*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt;
622*4882a593Smuzhiyun __poll_t pollflag;
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun uctxt = ((struct hfi1_filedata *)fp->private_data)->uctxt;
625*4882a593Smuzhiyun if (!uctxt)
626*4882a593Smuzhiyun pollflag = EPOLLERR;
627*4882a593Smuzhiyun else if (uctxt->poll_type == HFI1_POLL_TYPE_URGENT)
628*4882a593Smuzhiyun pollflag = poll_urgent(fp, pt);
629*4882a593Smuzhiyun else if (uctxt->poll_type == HFI1_POLL_TYPE_ANYRCV)
630*4882a593Smuzhiyun pollflag = poll_next(fp, pt);
631*4882a593Smuzhiyun else /* invalid */
632*4882a593Smuzhiyun pollflag = EPOLLERR;
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun return pollflag;
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun
hfi1_file_close(struct inode * inode,struct file * fp)637*4882a593Smuzhiyun static int hfi1_file_close(struct inode *inode, struct file *fp)
638*4882a593Smuzhiyun {
639*4882a593Smuzhiyun struct hfi1_filedata *fdata = fp->private_data;
640*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt = fdata->uctxt;
641*4882a593Smuzhiyun struct hfi1_devdata *dd = container_of(inode->i_cdev,
642*4882a593Smuzhiyun struct hfi1_devdata,
643*4882a593Smuzhiyun user_cdev);
644*4882a593Smuzhiyun unsigned long flags, *ev;
645*4882a593Smuzhiyun
646*4882a593Smuzhiyun fp->private_data = NULL;
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun if (!uctxt)
649*4882a593Smuzhiyun goto done;
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun hfi1_cdbg(PROC, "closing ctxt %u:%u", uctxt->ctxt, fdata->subctxt);
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun flush_wc();
654*4882a593Smuzhiyun /* drain user sdma queue */
655*4882a593Smuzhiyun hfi1_user_sdma_free_queues(fdata, uctxt);
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun /* release the cpu */
658*4882a593Smuzhiyun hfi1_put_proc_affinity(fdata->rec_cpu_num);
659*4882a593Smuzhiyun
660*4882a593Smuzhiyun /* clean up rcv side */
661*4882a593Smuzhiyun hfi1_user_exp_rcv_free(fdata);
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun /*
664*4882a593Smuzhiyun * fdata->uctxt is used in the above cleanup. It is not ready to be
665*4882a593Smuzhiyun * removed until here.
666*4882a593Smuzhiyun */
667*4882a593Smuzhiyun fdata->uctxt = NULL;
668*4882a593Smuzhiyun hfi1_rcd_put(uctxt);
669*4882a593Smuzhiyun
670*4882a593Smuzhiyun /*
671*4882a593Smuzhiyun * Clear any left over, unhandled events so the next process that
672*4882a593Smuzhiyun * gets this context doesn't get confused.
673*4882a593Smuzhiyun */
674*4882a593Smuzhiyun ev = dd->events + uctxt_offset(uctxt) + fdata->subctxt;
675*4882a593Smuzhiyun *ev = 0;
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun spin_lock_irqsave(&dd->uctxt_lock, flags);
678*4882a593Smuzhiyun __clear_bit(fdata->subctxt, uctxt->in_use_ctxts);
679*4882a593Smuzhiyun if (!bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
680*4882a593Smuzhiyun spin_unlock_irqrestore(&dd->uctxt_lock, flags);
681*4882a593Smuzhiyun goto done;
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun spin_unlock_irqrestore(&dd->uctxt_lock, flags);
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun /*
686*4882a593Smuzhiyun * Disable receive context and interrupt available, reset all
687*4882a593Smuzhiyun * RcvCtxtCtrl bits to default values.
688*4882a593Smuzhiyun */
689*4882a593Smuzhiyun hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
690*4882a593Smuzhiyun HFI1_RCVCTRL_TIDFLOW_DIS |
691*4882a593Smuzhiyun HFI1_RCVCTRL_INTRAVAIL_DIS |
692*4882a593Smuzhiyun HFI1_RCVCTRL_TAILUPD_DIS |
693*4882a593Smuzhiyun HFI1_RCVCTRL_ONE_PKT_EGR_DIS |
694*4882a593Smuzhiyun HFI1_RCVCTRL_NO_RHQ_DROP_DIS |
695*4882a593Smuzhiyun HFI1_RCVCTRL_NO_EGR_DROP_DIS |
696*4882a593Smuzhiyun HFI1_RCVCTRL_URGENT_DIS, uctxt);
697*4882a593Smuzhiyun /* Clear the context's J_KEY */
698*4882a593Smuzhiyun hfi1_clear_ctxt_jkey(dd, uctxt);
699*4882a593Smuzhiyun /*
700*4882a593Smuzhiyun * If a send context is allocated, reset context integrity
701*4882a593Smuzhiyun * checks to default and disable the send context.
702*4882a593Smuzhiyun */
703*4882a593Smuzhiyun if (uctxt->sc) {
704*4882a593Smuzhiyun sc_disable(uctxt->sc);
705*4882a593Smuzhiyun set_pio_integrity(uctxt->sc);
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun hfi1_free_ctxt_rcv_groups(uctxt);
709*4882a593Smuzhiyun hfi1_clear_ctxt_pkey(dd, uctxt);
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun uctxt->event_flags = 0;
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun deallocate_ctxt(uctxt);
714*4882a593Smuzhiyun done:
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun if (atomic_dec_and_test(&dd->user_refcount))
717*4882a593Smuzhiyun complete(&dd->user_comp);
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun cleanup_srcu_struct(&fdata->pq_srcu);
720*4882a593Smuzhiyun kfree(fdata);
721*4882a593Smuzhiyun return 0;
722*4882a593Smuzhiyun }
723*4882a593Smuzhiyun
724*4882a593Smuzhiyun /*
725*4882a593Smuzhiyun * Convert kernel *virtual* addresses to physical addresses.
726*4882a593Smuzhiyun * This is used to vmalloc'ed addresses.
727*4882a593Smuzhiyun */
kvirt_to_phys(void * addr)728*4882a593Smuzhiyun static u64 kvirt_to_phys(void *addr)
729*4882a593Smuzhiyun {
730*4882a593Smuzhiyun struct page *page;
731*4882a593Smuzhiyun u64 paddr = 0;
732*4882a593Smuzhiyun
733*4882a593Smuzhiyun page = vmalloc_to_page(addr);
734*4882a593Smuzhiyun if (page)
735*4882a593Smuzhiyun paddr = page_to_pfn(page) << PAGE_SHIFT;
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun return paddr;
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun /**
741*4882a593Smuzhiyun * complete_subctxt
742*4882a593Smuzhiyun * @fd: valid filedata pointer
743*4882a593Smuzhiyun *
744*4882a593Smuzhiyun * Sub-context info can only be set up after the base context
745*4882a593Smuzhiyun * has been completed. This is indicated by the clearing of the
746*4882a593Smuzhiyun * HFI1_CTXT_BASE_UINIT bit.
747*4882a593Smuzhiyun *
748*4882a593Smuzhiyun * Wait for the bit to be cleared, and then complete the subcontext
749*4882a593Smuzhiyun * initialization.
750*4882a593Smuzhiyun *
751*4882a593Smuzhiyun */
complete_subctxt(struct hfi1_filedata * fd)752*4882a593Smuzhiyun static int complete_subctxt(struct hfi1_filedata *fd)
753*4882a593Smuzhiyun {
754*4882a593Smuzhiyun int ret;
755*4882a593Smuzhiyun unsigned long flags;
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun /*
758*4882a593Smuzhiyun * sub-context info can only be set up after the base context
759*4882a593Smuzhiyun * has been completed.
760*4882a593Smuzhiyun */
761*4882a593Smuzhiyun ret = wait_event_interruptible(
762*4882a593Smuzhiyun fd->uctxt->wait,
763*4882a593Smuzhiyun !test_bit(HFI1_CTXT_BASE_UNINIT, &fd->uctxt->event_flags));
764*4882a593Smuzhiyun
765*4882a593Smuzhiyun if (test_bit(HFI1_CTXT_BASE_FAILED, &fd->uctxt->event_flags))
766*4882a593Smuzhiyun ret = -ENOMEM;
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun /* Finish the sub-context init */
769*4882a593Smuzhiyun if (!ret) {
770*4882a593Smuzhiyun fd->rec_cpu_num = hfi1_get_proc_affinity(fd->uctxt->numa_id);
771*4882a593Smuzhiyun ret = init_user_ctxt(fd, fd->uctxt);
772*4882a593Smuzhiyun }
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun if (ret) {
775*4882a593Smuzhiyun spin_lock_irqsave(&fd->dd->uctxt_lock, flags);
776*4882a593Smuzhiyun __clear_bit(fd->subctxt, fd->uctxt->in_use_ctxts);
777*4882a593Smuzhiyun spin_unlock_irqrestore(&fd->dd->uctxt_lock, flags);
778*4882a593Smuzhiyun hfi1_rcd_put(fd->uctxt);
779*4882a593Smuzhiyun fd->uctxt = NULL;
780*4882a593Smuzhiyun }
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun return ret;
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun
assign_ctxt(struct hfi1_filedata * fd,unsigned long arg,u32 len)785*4882a593Smuzhiyun static int assign_ctxt(struct hfi1_filedata *fd, unsigned long arg, u32 len)
786*4882a593Smuzhiyun {
787*4882a593Smuzhiyun int ret;
788*4882a593Smuzhiyun unsigned int swmajor;
789*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt = NULL;
790*4882a593Smuzhiyun struct hfi1_user_info uinfo;
791*4882a593Smuzhiyun
792*4882a593Smuzhiyun if (fd->uctxt)
793*4882a593Smuzhiyun return -EINVAL;
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun if (sizeof(uinfo) != len)
796*4882a593Smuzhiyun return -EINVAL;
797*4882a593Smuzhiyun
798*4882a593Smuzhiyun if (copy_from_user(&uinfo, (void __user *)arg, sizeof(uinfo)))
799*4882a593Smuzhiyun return -EFAULT;
800*4882a593Smuzhiyun
801*4882a593Smuzhiyun swmajor = uinfo.userversion >> 16;
802*4882a593Smuzhiyun if (swmajor != HFI1_USER_SWMAJOR)
803*4882a593Smuzhiyun return -ENODEV;
804*4882a593Smuzhiyun
805*4882a593Smuzhiyun if (uinfo.subctxt_cnt > HFI1_MAX_SHARED_CTXTS)
806*4882a593Smuzhiyun return -EINVAL;
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun /*
809*4882a593Smuzhiyun * Acquire the mutex to protect against multiple creations of what
810*4882a593Smuzhiyun * could be a shared base context.
811*4882a593Smuzhiyun */
812*4882a593Smuzhiyun mutex_lock(&hfi1_mutex);
813*4882a593Smuzhiyun /*
814*4882a593Smuzhiyun * Get a sub context if available (fd->uctxt will be set).
815*4882a593Smuzhiyun * ret < 0 error, 0 no context, 1 sub-context found
816*4882a593Smuzhiyun */
817*4882a593Smuzhiyun ret = find_sub_ctxt(fd, &uinfo);
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun /*
820*4882a593Smuzhiyun * Allocate a base context if context sharing is not required or a
821*4882a593Smuzhiyun * sub context wasn't found.
822*4882a593Smuzhiyun */
823*4882a593Smuzhiyun if (!ret)
824*4882a593Smuzhiyun ret = allocate_ctxt(fd, fd->dd, &uinfo, &uctxt);
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun mutex_unlock(&hfi1_mutex);
827*4882a593Smuzhiyun
828*4882a593Smuzhiyun /* Depending on the context type, finish the appropriate init */
829*4882a593Smuzhiyun switch (ret) {
830*4882a593Smuzhiyun case 0:
831*4882a593Smuzhiyun ret = setup_base_ctxt(fd, uctxt);
832*4882a593Smuzhiyun if (ret)
833*4882a593Smuzhiyun deallocate_ctxt(uctxt);
834*4882a593Smuzhiyun break;
835*4882a593Smuzhiyun case 1:
836*4882a593Smuzhiyun ret = complete_subctxt(fd);
837*4882a593Smuzhiyun break;
838*4882a593Smuzhiyun default:
839*4882a593Smuzhiyun break;
840*4882a593Smuzhiyun }
841*4882a593Smuzhiyun
842*4882a593Smuzhiyun return ret;
843*4882a593Smuzhiyun }
844*4882a593Smuzhiyun
845*4882a593Smuzhiyun /**
846*4882a593Smuzhiyun * match_ctxt
847*4882a593Smuzhiyun * @fd: valid filedata pointer
848*4882a593Smuzhiyun * @uinfo: user info to compare base context with
849*4882a593Smuzhiyun * @uctxt: context to compare uinfo to.
850*4882a593Smuzhiyun *
851*4882a593Smuzhiyun * Compare the given context with the given information to see if it
852*4882a593Smuzhiyun * can be used for a sub context.
853*4882a593Smuzhiyun */
match_ctxt(struct hfi1_filedata * fd,const struct hfi1_user_info * uinfo,struct hfi1_ctxtdata * uctxt)854*4882a593Smuzhiyun static int match_ctxt(struct hfi1_filedata *fd,
855*4882a593Smuzhiyun const struct hfi1_user_info *uinfo,
856*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt)
857*4882a593Smuzhiyun {
858*4882a593Smuzhiyun struct hfi1_devdata *dd = fd->dd;
859*4882a593Smuzhiyun unsigned long flags;
860*4882a593Smuzhiyun u16 subctxt;
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun /* Skip dynamically allocated kernel contexts */
863*4882a593Smuzhiyun if (uctxt->sc && (uctxt->sc->type == SC_KERNEL))
864*4882a593Smuzhiyun return 0;
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun /* Skip ctxt if it doesn't match the requested one */
867*4882a593Smuzhiyun if (memcmp(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid)) ||
868*4882a593Smuzhiyun uctxt->jkey != generate_jkey(current_uid()) ||
869*4882a593Smuzhiyun uctxt->subctxt_id != uinfo->subctxt_id ||
870*4882a593Smuzhiyun uctxt->subctxt_cnt != uinfo->subctxt_cnt)
871*4882a593Smuzhiyun return 0;
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun /* Verify the sharing process matches the base */
874*4882a593Smuzhiyun if (uctxt->userversion != uinfo->userversion)
875*4882a593Smuzhiyun return -EINVAL;
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun /* Find an unused sub context */
878*4882a593Smuzhiyun spin_lock_irqsave(&dd->uctxt_lock, flags);
879*4882a593Smuzhiyun if (bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
880*4882a593Smuzhiyun /* context is being closed, do not use */
881*4882a593Smuzhiyun spin_unlock_irqrestore(&dd->uctxt_lock, flags);
882*4882a593Smuzhiyun return 0;
883*4882a593Smuzhiyun }
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun subctxt = find_first_zero_bit(uctxt->in_use_ctxts,
886*4882a593Smuzhiyun HFI1_MAX_SHARED_CTXTS);
887*4882a593Smuzhiyun if (subctxt >= uctxt->subctxt_cnt) {
888*4882a593Smuzhiyun spin_unlock_irqrestore(&dd->uctxt_lock, flags);
889*4882a593Smuzhiyun return -EBUSY;
890*4882a593Smuzhiyun }
891*4882a593Smuzhiyun
892*4882a593Smuzhiyun fd->subctxt = subctxt;
893*4882a593Smuzhiyun __set_bit(fd->subctxt, uctxt->in_use_ctxts);
894*4882a593Smuzhiyun spin_unlock_irqrestore(&dd->uctxt_lock, flags);
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun fd->uctxt = uctxt;
897*4882a593Smuzhiyun hfi1_rcd_get(uctxt);
898*4882a593Smuzhiyun
899*4882a593Smuzhiyun return 1;
900*4882a593Smuzhiyun }
901*4882a593Smuzhiyun
902*4882a593Smuzhiyun /**
903*4882a593Smuzhiyun * find_sub_ctxt
904*4882a593Smuzhiyun * @fd: valid filedata pointer
905*4882a593Smuzhiyun * @uinfo: matching info to use to find a possible context to share.
906*4882a593Smuzhiyun *
907*4882a593Smuzhiyun * The hfi1_mutex must be held when this function is called. It is
908*4882a593Smuzhiyun * necessary to ensure serialized creation of shared contexts.
909*4882a593Smuzhiyun *
910*4882a593Smuzhiyun * Return:
911*4882a593Smuzhiyun * 0 No sub-context found
912*4882a593Smuzhiyun * 1 Subcontext found and allocated
913*4882a593Smuzhiyun * errno EINVAL (incorrect parameters)
914*4882a593Smuzhiyun * EBUSY (all sub contexts in use)
915*4882a593Smuzhiyun */
find_sub_ctxt(struct hfi1_filedata * fd,const struct hfi1_user_info * uinfo)916*4882a593Smuzhiyun static int find_sub_ctxt(struct hfi1_filedata *fd,
917*4882a593Smuzhiyun const struct hfi1_user_info *uinfo)
918*4882a593Smuzhiyun {
919*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt;
920*4882a593Smuzhiyun struct hfi1_devdata *dd = fd->dd;
921*4882a593Smuzhiyun u16 i;
922*4882a593Smuzhiyun int ret;
923*4882a593Smuzhiyun
924*4882a593Smuzhiyun if (!uinfo->subctxt_cnt)
925*4882a593Smuzhiyun return 0;
926*4882a593Smuzhiyun
927*4882a593Smuzhiyun for (i = dd->first_dyn_alloc_ctxt; i < dd->num_rcv_contexts; i++) {
928*4882a593Smuzhiyun uctxt = hfi1_rcd_get_by_index(dd, i);
929*4882a593Smuzhiyun if (uctxt) {
930*4882a593Smuzhiyun ret = match_ctxt(fd, uinfo, uctxt);
931*4882a593Smuzhiyun hfi1_rcd_put(uctxt);
932*4882a593Smuzhiyun /* value of != 0 will return */
933*4882a593Smuzhiyun if (ret)
934*4882a593Smuzhiyun return ret;
935*4882a593Smuzhiyun }
936*4882a593Smuzhiyun }
937*4882a593Smuzhiyun
938*4882a593Smuzhiyun return 0;
939*4882a593Smuzhiyun }
940*4882a593Smuzhiyun
allocate_ctxt(struct hfi1_filedata * fd,struct hfi1_devdata * dd,struct hfi1_user_info * uinfo,struct hfi1_ctxtdata ** rcd)941*4882a593Smuzhiyun static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
942*4882a593Smuzhiyun struct hfi1_user_info *uinfo,
943*4882a593Smuzhiyun struct hfi1_ctxtdata **rcd)
944*4882a593Smuzhiyun {
945*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt;
946*4882a593Smuzhiyun int ret, numa;
947*4882a593Smuzhiyun
948*4882a593Smuzhiyun if (dd->flags & HFI1_FROZEN) {
949*4882a593Smuzhiyun /*
950*4882a593Smuzhiyun * Pick an error that is unique from all other errors
951*4882a593Smuzhiyun * that are returned so the user process knows that
952*4882a593Smuzhiyun * it tried to allocate while the SPC was frozen. It
953*4882a593Smuzhiyun * it should be able to retry with success in a short
954*4882a593Smuzhiyun * while.
955*4882a593Smuzhiyun */
956*4882a593Smuzhiyun return -EIO;
957*4882a593Smuzhiyun }
958*4882a593Smuzhiyun
959*4882a593Smuzhiyun if (!dd->freectxts)
960*4882a593Smuzhiyun return -EBUSY;
961*4882a593Smuzhiyun
962*4882a593Smuzhiyun /*
963*4882a593Smuzhiyun * If we don't have a NUMA node requested, preference is towards
964*4882a593Smuzhiyun * device NUMA node.
965*4882a593Smuzhiyun */
966*4882a593Smuzhiyun fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node);
967*4882a593Smuzhiyun if (fd->rec_cpu_num != -1)
968*4882a593Smuzhiyun numa = cpu_to_node(fd->rec_cpu_num);
969*4882a593Smuzhiyun else
970*4882a593Smuzhiyun numa = numa_node_id();
971*4882a593Smuzhiyun ret = hfi1_create_ctxtdata(dd->pport, numa, &uctxt);
972*4882a593Smuzhiyun if (ret < 0) {
973*4882a593Smuzhiyun dd_dev_err(dd, "user ctxtdata allocation failed\n");
974*4882a593Smuzhiyun return ret;
975*4882a593Smuzhiyun }
976*4882a593Smuzhiyun hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)",
977*4882a593Smuzhiyun uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num,
978*4882a593Smuzhiyun uctxt->numa_id);
979*4882a593Smuzhiyun
980*4882a593Smuzhiyun /*
981*4882a593Smuzhiyun * Allocate and enable a PIO send context.
982*4882a593Smuzhiyun */
983*4882a593Smuzhiyun uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize, dd->node);
984*4882a593Smuzhiyun if (!uctxt->sc) {
985*4882a593Smuzhiyun ret = -ENOMEM;
986*4882a593Smuzhiyun goto ctxdata_free;
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index,
989*4882a593Smuzhiyun uctxt->sc->hw_context);
990*4882a593Smuzhiyun ret = sc_enable(uctxt->sc);
991*4882a593Smuzhiyun if (ret)
992*4882a593Smuzhiyun goto ctxdata_free;
993*4882a593Smuzhiyun
994*4882a593Smuzhiyun /*
995*4882a593Smuzhiyun * Setup sub context information if the user-level has requested
996*4882a593Smuzhiyun * sub contexts.
997*4882a593Smuzhiyun * This has to be done here so the rest of the sub-contexts find the
998*4882a593Smuzhiyun * proper base context.
999*4882a593Smuzhiyun * NOTE: _set_bit() can be used here because the context creation is
1000*4882a593Smuzhiyun * protected by the mutex (rather than the spin_lock), and will be the
1001*4882a593Smuzhiyun * very first instance of this context.
1002*4882a593Smuzhiyun */
1003*4882a593Smuzhiyun __set_bit(0, uctxt->in_use_ctxts);
1004*4882a593Smuzhiyun if (uinfo->subctxt_cnt)
1005*4882a593Smuzhiyun init_subctxts(uctxt, uinfo);
1006*4882a593Smuzhiyun uctxt->userversion = uinfo->userversion;
1007*4882a593Smuzhiyun uctxt->flags = hfi1_cap_mask; /* save current flag state */
1008*4882a593Smuzhiyun init_waitqueue_head(&uctxt->wait);
1009*4882a593Smuzhiyun strlcpy(uctxt->comm, current->comm, sizeof(uctxt->comm));
1010*4882a593Smuzhiyun memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid));
1011*4882a593Smuzhiyun uctxt->jkey = generate_jkey(current_uid());
1012*4882a593Smuzhiyun hfi1_stats.sps_ctxts++;
1013*4882a593Smuzhiyun /*
1014*4882a593Smuzhiyun * Disable ASPM when there are open user/PSM contexts to avoid
1015*4882a593Smuzhiyun * issues with ASPM L1 exit latency
1016*4882a593Smuzhiyun */
1017*4882a593Smuzhiyun if (dd->freectxts-- == dd->num_user_contexts)
1018*4882a593Smuzhiyun aspm_disable_all(dd);
1019*4882a593Smuzhiyun
1020*4882a593Smuzhiyun *rcd = uctxt;
1021*4882a593Smuzhiyun
1022*4882a593Smuzhiyun return 0;
1023*4882a593Smuzhiyun
1024*4882a593Smuzhiyun ctxdata_free:
1025*4882a593Smuzhiyun hfi1_free_ctxt(uctxt);
1026*4882a593Smuzhiyun return ret;
1027*4882a593Smuzhiyun }
1028*4882a593Smuzhiyun
deallocate_ctxt(struct hfi1_ctxtdata * uctxt)1029*4882a593Smuzhiyun static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt)
1030*4882a593Smuzhiyun {
1031*4882a593Smuzhiyun mutex_lock(&hfi1_mutex);
1032*4882a593Smuzhiyun hfi1_stats.sps_ctxts--;
1033*4882a593Smuzhiyun if (++uctxt->dd->freectxts == uctxt->dd->num_user_contexts)
1034*4882a593Smuzhiyun aspm_enable_all(uctxt->dd);
1035*4882a593Smuzhiyun mutex_unlock(&hfi1_mutex);
1036*4882a593Smuzhiyun
1037*4882a593Smuzhiyun hfi1_free_ctxt(uctxt);
1038*4882a593Smuzhiyun }
1039*4882a593Smuzhiyun
init_subctxts(struct hfi1_ctxtdata * uctxt,const struct hfi1_user_info * uinfo)1040*4882a593Smuzhiyun static void init_subctxts(struct hfi1_ctxtdata *uctxt,
1041*4882a593Smuzhiyun const struct hfi1_user_info *uinfo)
1042*4882a593Smuzhiyun {
1043*4882a593Smuzhiyun uctxt->subctxt_cnt = uinfo->subctxt_cnt;
1044*4882a593Smuzhiyun uctxt->subctxt_id = uinfo->subctxt_id;
1045*4882a593Smuzhiyun set_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
1046*4882a593Smuzhiyun }
1047*4882a593Smuzhiyun
setup_subctxt(struct hfi1_ctxtdata * uctxt)1048*4882a593Smuzhiyun static int setup_subctxt(struct hfi1_ctxtdata *uctxt)
1049*4882a593Smuzhiyun {
1050*4882a593Smuzhiyun int ret = 0;
1051*4882a593Smuzhiyun u16 num_subctxts = uctxt->subctxt_cnt;
1052*4882a593Smuzhiyun
1053*4882a593Smuzhiyun uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE);
1054*4882a593Smuzhiyun if (!uctxt->subctxt_uregbase)
1055*4882a593Smuzhiyun return -ENOMEM;
1056*4882a593Smuzhiyun
1057*4882a593Smuzhiyun /* We can take the size of the RcvHdr Queue from the master */
1058*4882a593Smuzhiyun uctxt->subctxt_rcvhdr_base = vmalloc_user(rcvhdrq_size(uctxt) *
1059*4882a593Smuzhiyun num_subctxts);
1060*4882a593Smuzhiyun if (!uctxt->subctxt_rcvhdr_base) {
1061*4882a593Smuzhiyun ret = -ENOMEM;
1062*4882a593Smuzhiyun goto bail_ureg;
1063*4882a593Smuzhiyun }
1064*4882a593Smuzhiyun
1065*4882a593Smuzhiyun uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size *
1066*4882a593Smuzhiyun num_subctxts);
1067*4882a593Smuzhiyun if (!uctxt->subctxt_rcvegrbuf) {
1068*4882a593Smuzhiyun ret = -ENOMEM;
1069*4882a593Smuzhiyun goto bail_rhdr;
1070*4882a593Smuzhiyun }
1071*4882a593Smuzhiyun
1072*4882a593Smuzhiyun return 0;
1073*4882a593Smuzhiyun
1074*4882a593Smuzhiyun bail_rhdr:
1075*4882a593Smuzhiyun vfree(uctxt->subctxt_rcvhdr_base);
1076*4882a593Smuzhiyun uctxt->subctxt_rcvhdr_base = NULL;
1077*4882a593Smuzhiyun bail_ureg:
1078*4882a593Smuzhiyun vfree(uctxt->subctxt_uregbase);
1079*4882a593Smuzhiyun uctxt->subctxt_uregbase = NULL;
1080*4882a593Smuzhiyun
1081*4882a593Smuzhiyun return ret;
1082*4882a593Smuzhiyun }
1083*4882a593Smuzhiyun
user_init(struct hfi1_ctxtdata * uctxt)1084*4882a593Smuzhiyun static void user_init(struct hfi1_ctxtdata *uctxt)
1085*4882a593Smuzhiyun {
1086*4882a593Smuzhiyun unsigned int rcvctrl_ops = 0;
1087*4882a593Smuzhiyun
1088*4882a593Smuzhiyun /* initialize poll variables... */
1089*4882a593Smuzhiyun uctxt->urgent = 0;
1090*4882a593Smuzhiyun uctxt->urgent_poll = 0;
1091*4882a593Smuzhiyun
1092*4882a593Smuzhiyun /*
1093*4882a593Smuzhiyun * Now enable the ctxt for receive.
1094*4882a593Smuzhiyun * For chips that are set to DMA the tail register to memory
1095*4882a593Smuzhiyun * when they change (and when the update bit transitions from
1096*4882a593Smuzhiyun * 0 to 1. So for those chips, we turn it off and then back on.
1097*4882a593Smuzhiyun * This will (very briefly) affect any other open ctxts, but the
1098*4882a593Smuzhiyun * duration is very short, and therefore isn't an issue. We
1099*4882a593Smuzhiyun * explicitly set the in-memory tail copy to 0 beforehand, so we
1100*4882a593Smuzhiyun * don't have to wait to be sure the DMA update has happened
1101*4882a593Smuzhiyun * (chip resets head/tail to 0 on transition to enable).
1102*4882a593Smuzhiyun */
1103*4882a593Smuzhiyun if (hfi1_rcvhdrtail_kvaddr(uctxt))
1104*4882a593Smuzhiyun clear_rcvhdrtail(uctxt);
1105*4882a593Smuzhiyun
1106*4882a593Smuzhiyun /* Setup J_KEY before enabling the context */
1107*4882a593Smuzhiyun hfi1_set_ctxt_jkey(uctxt->dd, uctxt, uctxt->jkey);
1108*4882a593Smuzhiyun
1109*4882a593Smuzhiyun rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB;
1110*4882a593Smuzhiyun rcvctrl_ops |= HFI1_RCVCTRL_URGENT_ENB;
1111*4882a593Smuzhiyun if (HFI1_CAP_UGET_MASK(uctxt->flags, HDRSUPP))
1112*4882a593Smuzhiyun rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB;
1113*4882a593Smuzhiyun /*
1114*4882a593Smuzhiyun * Ignore the bit in the flags for now until proper
1115*4882a593Smuzhiyun * support for multiple packet per rcv array entry is
1116*4882a593Smuzhiyun * added.
1117*4882a593Smuzhiyun */
1118*4882a593Smuzhiyun if (!HFI1_CAP_UGET_MASK(uctxt->flags, MULTI_PKT_EGR))
1119*4882a593Smuzhiyun rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
1120*4882a593Smuzhiyun if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_EGR_FULL))
1121*4882a593Smuzhiyun rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
1122*4882a593Smuzhiyun if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_RHQ_FULL))
1123*4882a593Smuzhiyun rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
1124*4882a593Smuzhiyun /*
1125*4882a593Smuzhiyun * The RcvCtxtCtrl.TailUpd bit has to be explicitly written.
1126*4882a593Smuzhiyun * We can't rely on the correct value to be set from prior
1127*4882a593Smuzhiyun * uses of the chip or ctxt. Therefore, add the rcvctrl op
1128*4882a593Smuzhiyun * for both cases.
1129*4882a593Smuzhiyun */
1130*4882a593Smuzhiyun if (HFI1_CAP_UGET_MASK(uctxt->flags, DMA_RTAIL))
1131*4882a593Smuzhiyun rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB;
1132*4882a593Smuzhiyun else
1133*4882a593Smuzhiyun rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS;
1134*4882a593Smuzhiyun hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt);
1135*4882a593Smuzhiyun }
1136*4882a593Smuzhiyun
get_ctxt_info(struct hfi1_filedata * fd,unsigned long arg,u32 len)1137*4882a593Smuzhiyun static int get_ctxt_info(struct hfi1_filedata *fd, unsigned long arg, u32 len)
1138*4882a593Smuzhiyun {
1139*4882a593Smuzhiyun struct hfi1_ctxt_info cinfo;
1140*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt = fd->uctxt;
1141*4882a593Smuzhiyun
1142*4882a593Smuzhiyun if (sizeof(cinfo) != len)
1143*4882a593Smuzhiyun return -EINVAL;
1144*4882a593Smuzhiyun
1145*4882a593Smuzhiyun memset(&cinfo, 0, sizeof(cinfo));
1146*4882a593Smuzhiyun cinfo.runtime_flags = (((uctxt->flags >> HFI1_CAP_MISC_SHIFT) &
1147*4882a593Smuzhiyun HFI1_CAP_MISC_MASK) << HFI1_CAP_USER_SHIFT) |
1148*4882a593Smuzhiyun HFI1_CAP_UGET_MASK(uctxt->flags, MASK) |
1149*4882a593Smuzhiyun HFI1_CAP_KGET_MASK(uctxt->flags, K2U);
1150*4882a593Smuzhiyun /* adjust flag if this fd is not able to cache */
1151*4882a593Smuzhiyun if (!fd->use_mn)
1152*4882a593Smuzhiyun cinfo.runtime_flags |= HFI1_CAP_TID_UNMAP; /* no caching */
1153*4882a593Smuzhiyun
1154*4882a593Smuzhiyun cinfo.num_active = hfi1_count_active_units();
1155*4882a593Smuzhiyun cinfo.unit = uctxt->dd->unit;
1156*4882a593Smuzhiyun cinfo.ctxt = uctxt->ctxt;
1157*4882a593Smuzhiyun cinfo.subctxt = fd->subctxt;
1158*4882a593Smuzhiyun cinfo.rcvtids = roundup(uctxt->egrbufs.alloced,
1159*4882a593Smuzhiyun uctxt->dd->rcv_entries.group_size) +
1160*4882a593Smuzhiyun uctxt->expected_count;
1161*4882a593Smuzhiyun cinfo.credits = uctxt->sc->credits;
1162*4882a593Smuzhiyun cinfo.numa_node = uctxt->numa_id;
1163*4882a593Smuzhiyun cinfo.rec_cpu = fd->rec_cpu_num;
1164*4882a593Smuzhiyun cinfo.send_ctxt = uctxt->sc->hw_context;
1165*4882a593Smuzhiyun
1166*4882a593Smuzhiyun cinfo.egrtids = uctxt->egrbufs.alloced;
1167*4882a593Smuzhiyun cinfo.rcvhdrq_cnt = get_hdrq_cnt(uctxt);
1168*4882a593Smuzhiyun cinfo.rcvhdrq_entsize = get_hdrqentsize(uctxt) << 2;
1169*4882a593Smuzhiyun cinfo.sdma_ring_size = fd->cq->nentries;
1170*4882a593Smuzhiyun cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size;
1171*4882a593Smuzhiyun
1172*4882a593Smuzhiyun trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, &cinfo);
1173*4882a593Smuzhiyun if (copy_to_user((void __user *)arg, &cinfo, len))
1174*4882a593Smuzhiyun return -EFAULT;
1175*4882a593Smuzhiyun
1176*4882a593Smuzhiyun return 0;
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun
init_user_ctxt(struct hfi1_filedata * fd,struct hfi1_ctxtdata * uctxt)1179*4882a593Smuzhiyun static int init_user_ctxt(struct hfi1_filedata *fd,
1180*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt)
1181*4882a593Smuzhiyun {
1182*4882a593Smuzhiyun int ret;
1183*4882a593Smuzhiyun
1184*4882a593Smuzhiyun ret = hfi1_user_sdma_alloc_queues(uctxt, fd);
1185*4882a593Smuzhiyun if (ret)
1186*4882a593Smuzhiyun return ret;
1187*4882a593Smuzhiyun
1188*4882a593Smuzhiyun ret = hfi1_user_exp_rcv_init(fd, uctxt);
1189*4882a593Smuzhiyun if (ret)
1190*4882a593Smuzhiyun hfi1_user_sdma_free_queues(fd, uctxt);
1191*4882a593Smuzhiyun
1192*4882a593Smuzhiyun return ret;
1193*4882a593Smuzhiyun }
1194*4882a593Smuzhiyun
setup_base_ctxt(struct hfi1_filedata * fd,struct hfi1_ctxtdata * uctxt)1195*4882a593Smuzhiyun static int setup_base_ctxt(struct hfi1_filedata *fd,
1196*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt)
1197*4882a593Smuzhiyun {
1198*4882a593Smuzhiyun struct hfi1_devdata *dd = uctxt->dd;
1199*4882a593Smuzhiyun int ret = 0;
1200*4882a593Smuzhiyun
1201*4882a593Smuzhiyun hfi1_init_ctxt(uctxt->sc);
1202*4882a593Smuzhiyun
1203*4882a593Smuzhiyun /* Now allocate the RcvHdr queue and eager buffers. */
1204*4882a593Smuzhiyun ret = hfi1_create_rcvhdrq(dd, uctxt);
1205*4882a593Smuzhiyun if (ret)
1206*4882a593Smuzhiyun goto done;
1207*4882a593Smuzhiyun
1208*4882a593Smuzhiyun ret = hfi1_setup_eagerbufs(uctxt);
1209*4882a593Smuzhiyun if (ret)
1210*4882a593Smuzhiyun goto done;
1211*4882a593Smuzhiyun
1212*4882a593Smuzhiyun /* If sub-contexts are enabled, do the appropriate setup */
1213*4882a593Smuzhiyun if (uctxt->subctxt_cnt)
1214*4882a593Smuzhiyun ret = setup_subctxt(uctxt);
1215*4882a593Smuzhiyun if (ret)
1216*4882a593Smuzhiyun goto done;
1217*4882a593Smuzhiyun
1218*4882a593Smuzhiyun ret = hfi1_alloc_ctxt_rcv_groups(uctxt);
1219*4882a593Smuzhiyun if (ret)
1220*4882a593Smuzhiyun goto done;
1221*4882a593Smuzhiyun
1222*4882a593Smuzhiyun ret = init_user_ctxt(fd, uctxt);
1223*4882a593Smuzhiyun if (ret) {
1224*4882a593Smuzhiyun hfi1_free_ctxt_rcv_groups(uctxt);
1225*4882a593Smuzhiyun goto done;
1226*4882a593Smuzhiyun }
1227*4882a593Smuzhiyun
1228*4882a593Smuzhiyun user_init(uctxt);
1229*4882a593Smuzhiyun
1230*4882a593Smuzhiyun /* Now that the context is set up, the fd can get a reference. */
1231*4882a593Smuzhiyun fd->uctxt = uctxt;
1232*4882a593Smuzhiyun hfi1_rcd_get(uctxt);
1233*4882a593Smuzhiyun
1234*4882a593Smuzhiyun done:
1235*4882a593Smuzhiyun if (uctxt->subctxt_cnt) {
1236*4882a593Smuzhiyun /*
1237*4882a593Smuzhiyun * On error, set the failed bit so sub-contexts will clean up
1238*4882a593Smuzhiyun * correctly.
1239*4882a593Smuzhiyun */
1240*4882a593Smuzhiyun if (ret)
1241*4882a593Smuzhiyun set_bit(HFI1_CTXT_BASE_FAILED, &uctxt->event_flags);
1242*4882a593Smuzhiyun
1243*4882a593Smuzhiyun /*
1244*4882a593Smuzhiyun * Base context is done (successfully or not), notify anybody
1245*4882a593Smuzhiyun * using a sub-context that is waiting for this completion.
1246*4882a593Smuzhiyun */
1247*4882a593Smuzhiyun clear_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
1248*4882a593Smuzhiyun wake_up(&uctxt->wait);
1249*4882a593Smuzhiyun }
1250*4882a593Smuzhiyun
1251*4882a593Smuzhiyun return ret;
1252*4882a593Smuzhiyun }
1253*4882a593Smuzhiyun
get_base_info(struct hfi1_filedata * fd,unsigned long arg,u32 len)1254*4882a593Smuzhiyun static int get_base_info(struct hfi1_filedata *fd, unsigned long arg, u32 len)
1255*4882a593Smuzhiyun {
1256*4882a593Smuzhiyun struct hfi1_base_info binfo;
1257*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt = fd->uctxt;
1258*4882a593Smuzhiyun struct hfi1_devdata *dd = uctxt->dd;
1259*4882a593Smuzhiyun unsigned offset;
1260*4882a593Smuzhiyun
1261*4882a593Smuzhiyun trace_hfi1_uctxtdata(uctxt->dd, uctxt, fd->subctxt);
1262*4882a593Smuzhiyun
1263*4882a593Smuzhiyun if (sizeof(binfo) != len)
1264*4882a593Smuzhiyun return -EINVAL;
1265*4882a593Smuzhiyun
1266*4882a593Smuzhiyun memset(&binfo, 0, sizeof(binfo));
1267*4882a593Smuzhiyun binfo.hw_version = dd->revision;
1268*4882a593Smuzhiyun binfo.sw_version = HFI1_KERN_SWVERSION;
1269*4882a593Smuzhiyun binfo.bthqp = RVT_KDETH_QP_PREFIX;
1270*4882a593Smuzhiyun binfo.jkey = uctxt->jkey;
1271*4882a593Smuzhiyun /*
1272*4882a593Smuzhiyun * If more than 64 contexts are enabled the allocated credit
1273*4882a593Smuzhiyun * return will span two or three contiguous pages. Since we only
1274*4882a593Smuzhiyun * map the page containing the context's credit return address,
1275*4882a593Smuzhiyun * we need to calculate the offset in the proper page.
1276*4882a593Smuzhiyun */
1277*4882a593Smuzhiyun offset = ((u64)uctxt->sc->hw_free -
1278*4882a593Smuzhiyun (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE;
1279*4882a593Smuzhiyun binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt,
1280*4882a593Smuzhiyun fd->subctxt, offset);
1281*4882a593Smuzhiyun binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt,
1282*4882a593Smuzhiyun fd->subctxt,
1283*4882a593Smuzhiyun uctxt->sc->base_addr);
1284*4882a593Smuzhiyun binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP,
1285*4882a593Smuzhiyun uctxt->ctxt,
1286*4882a593Smuzhiyun fd->subctxt,
1287*4882a593Smuzhiyun uctxt->sc->base_addr);
1288*4882a593Smuzhiyun binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt,
1289*4882a593Smuzhiyun fd->subctxt,
1290*4882a593Smuzhiyun uctxt->rcvhdrq);
1291*4882a593Smuzhiyun binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt,
1292*4882a593Smuzhiyun fd->subctxt,
1293*4882a593Smuzhiyun uctxt->egrbufs.rcvtids[0].dma);
1294*4882a593Smuzhiyun binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt,
1295*4882a593Smuzhiyun fd->subctxt, 0);
1296*4882a593Smuzhiyun /*
1297*4882a593Smuzhiyun * user regs are at
1298*4882a593Smuzhiyun * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE))
1299*4882a593Smuzhiyun */
1300*4882a593Smuzhiyun binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt,
1301*4882a593Smuzhiyun fd->subctxt, 0);
1302*4882a593Smuzhiyun offset = offset_in_page((uctxt_offset(uctxt) + fd->subctxt) *
1303*4882a593Smuzhiyun sizeof(*dd->events));
1304*4882a593Smuzhiyun binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt,
1305*4882a593Smuzhiyun fd->subctxt,
1306*4882a593Smuzhiyun offset);
1307*4882a593Smuzhiyun binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt,
1308*4882a593Smuzhiyun fd->subctxt,
1309*4882a593Smuzhiyun dd->status);
1310*4882a593Smuzhiyun if (HFI1_CAP_IS_USET(DMA_RTAIL))
1311*4882a593Smuzhiyun binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt,
1312*4882a593Smuzhiyun fd->subctxt, 0);
1313*4882a593Smuzhiyun if (uctxt->subctxt_cnt) {
1314*4882a593Smuzhiyun binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS,
1315*4882a593Smuzhiyun uctxt->ctxt,
1316*4882a593Smuzhiyun fd->subctxt, 0);
1317*4882a593Smuzhiyun binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ,
1318*4882a593Smuzhiyun uctxt->ctxt,
1319*4882a593Smuzhiyun fd->subctxt, 0);
1320*4882a593Smuzhiyun binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF,
1321*4882a593Smuzhiyun uctxt->ctxt,
1322*4882a593Smuzhiyun fd->subctxt, 0);
1323*4882a593Smuzhiyun }
1324*4882a593Smuzhiyun
1325*4882a593Smuzhiyun if (copy_to_user((void __user *)arg, &binfo, len))
1326*4882a593Smuzhiyun return -EFAULT;
1327*4882a593Smuzhiyun
1328*4882a593Smuzhiyun return 0;
1329*4882a593Smuzhiyun }
1330*4882a593Smuzhiyun
1331*4882a593Smuzhiyun /**
1332*4882a593Smuzhiyun * user_exp_rcv_setup - Set up the given tid rcv list
1333*4882a593Smuzhiyun * @fd: file data of the current driver instance
1334*4882a593Smuzhiyun * @arg: ioctl argumnent for user space information
1335*4882a593Smuzhiyun * @len: length of data structure associated with ioctl command
1336*4882a593Smuzhiyun *
1337*4882a593Smuzhiyun * Wrapper to validate ioctl information before doing _rcv_setup.
1338*4882a593Smuzhiyun *
1339*4882a593Smuzhiyun */
user_exp_rcv_setup(struct hfi1_filedata * fd,unsigned long arg,u32 len)1340*4882a593Smuzhiyun static int user_exp_rcv_setup(struct hfi1_filedata *fd, unsigned long arg,
1341*4882a593Smuzhiyun u32 len)
1342*4882a593Smuzhiyun {
1343*4882a593Smuzhiyun int ret;
1344*4882a593Smuzhiyun unsigned long addr;
1345*4882a593Smuzhiyun struct hfi1_tid_info tinfo;
1346*4882a593Smuzhiyun
1347*4882a593Smuzhiyun if (sizeof(tinfo) != len)
1348*4882a593Smuzhiyun return -EINVAL;
1349*4882a593Smuzhiyun
1350*4882a593Smuzhiyun if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo))))
1351*4882a593Smuzhiyun return -EFAULT;
1352*4882a593Smuzhiyun
1353*4882a593Smuzhiyun ret = hfi1_user_exp_rcv_setup(fd, &tinfo);
1354*4882a593Smuzhiyun if (!ret) {
1355*4882a593Smuzhiyun /*
1356*4882a593Smuzhiyun * Copy the number of tidlist entries we used
1357*4882a593Smuzhiyun * and the length of the buffer we registered.
1358*4882a593Smuzhiyun */
1359*4882a593Smuzhiyun addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
1360*4882a593Smuzhiyun if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
1361*4882a593Smuzhiyun sizeof(tinfo.tidcnt)))
1362*4882a593Smuzhiyun return -EFAULT;
1363*4882a593Smuzhiyun
1364*4882a593Smuzhiyun addr = arg + offsetof(struct hfi1_tid_info, length);
1365*4882a593Smuzhiyun if (copy_to_user((void __user *)addr, &tinfo.length,
1366*4882a593Smuzhiyun sizeof(tinfo.length)))
1367*4882a593Smuzhiyun ret = -EFAULT;
1368*4882a593Smuzhiyun }
1369*4882a593Smuzhiyun
1370*4882a593Smuzhiyun return ret;
1371*4882a593Smuzhiyun }
1372*4882a593Smuzhiyun
1373*4882a593Smuzhiyun /**
1374*4882a593Smuzhiyun * user_exp_rcv_clear - Clear the given tid rcv list
1375*4882a593Smuzhiyun * @fd: file data of the current driver instance
1376*4882a593Smuzhiyun * @arg: ioctl argumnent for user space information
1377*4882a593Smuzhiyun * @len: length of data structure associated with ioctl command
1378*4882a593Smuzhiyun *
1379*4882a593Smuzhiyun * The hfi1_user_exp_rcv_clear() can be called from the error path. Because
1380*4882a593Smuzhiyun * of this, we need to use this wrapper to copy the user space information
1381*4882a593Smuzhiyun * before doing the clear.
1382*4882a593Smuzhiyun */
user_exp_rcv_clear(struct hfi1_filedata * fd,unsigned long arg,u32 len)1383*4882a593Smuzhiyun static int user_exp_rcv_clear(struct hfi1_filedata *fd, unsigned long arg,
1384*4882a593Smuzhiyun u32 len)
1385*4882a593Smuzhiyun {
1386*4882a593Smuzhiyun int ret;
1387*4882a593Smuzhiyun unsigned long addr;
1388*4882a593Smuzhiyun struct hfi1_tid_info tinfo;
1389*4882a593Smuzhiyun
1390*4882a593Smuzhiyun if (sizeof(tinfo) != len)
1391*4882a593Smuzhiyun return -EINVAL;
1392*4882a593Smuzhiyun
1393*4882a593Smuzhiyun if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo))))
1394*4882a593Smuzhiyun return -EFAULT;
1395*4882a593Smuzhiyun
1396*4882a593Smuzhiyun ret = hfi1_user_exp_rcv_clear(fd, &tinfo);
1397*4882a593Smuzhiyun if (!ret) {
1398*4882a593Smuzhiyun addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
1399*4882a593Smuzhiyun if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
1400*4882a593Smuzhiyun sizeof(tinfo.tidcnt)))
1401*4882a593Smuzhiyun return -EFAULT;
1402*4882a593Smuzhiyun }
1403*4882a593Smuzhiyun
1404*4882a593Smuzhiyun return ret;
1405*4882a593Smuzhiyun }
1406*4882a593Smuzhiyun
1407*4882a593Smuzhiyun /**
1408*4882a593Smuzhiyun * user_exp_rcv_invalid - Invalidate the given tid rcv list
1409*4882a593Smuzhiyun * @fd: file data of the current driver instance
1410*4882a593Smuzhiyun * @arg: ioctl argumnent for user space information
1411*4882a593Smuzhiyun * @len: length of data structure associated with ioctl command
1412*4882a593Smuzhiyun *
1413*4882a593Smuzhiyun * Wrapper to validate ioctl information before doing _rcv_invalid.
1414*4882a593Smuzhiyun *
1415*4882a593Smuzhiyun */
user_exp_rcv_invalid(struct hfi1_filedata * fd,unsigned long arg,u32 len)1416*4882a593Smuzhiyun static int user_exp_rcv_invalid(struct hfi1_filedata *fd, unsigned long arg,
1417*4882a593Smuzhiyun u32 len)
1418*4882a593Smuzhiyun {
1419*4882a593Smuzhiyun int ret;
1420*4882a593Smuzhiyun unsigned long addr;
1421*4882a593Smuzhiyun struct hfi1_tid_info tinfo;
1422*4882a593Smuzhiyun
1423*4882a593Smuzhiyun if (sizeof(tinfo) != len)
1424*4882a593Smuzhiyun return -EINVAL;
1425*4882a593Smuzhiyun
1426*4882a593Smuzhiyun if (!fd->invalid_tids)
1427*4882a593Smuzhiyun return -EINVAL;
1428*4882a593Smuzhiyun
1429*4882a593Smuzhiyun if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo))))
1430*4882a593Smuzhiyun return -EFAULT;
1431*4882a593Smuzhiyun
1432*4882a593Smuzhiyun ret = hfi1_user_exp_rcv_invalid(fd, &tinfo);
1433*4882a593Smuzhiyun if (ret)
1434*4882a593Smuzhiyun return ret;
1435*4882a593Smuzhiyun
1436*4882a593Smuzhiyun addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
1437*4882a593Smuzhiyun if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
1438*4882a593Smuzhiyun sizeof(tinfo.tidcnt)))
1439*4882a593Smuzhiyun ret = -EFAULT;
1440*4882a593Smuzhiyun
1441*4882a593Smuzhiyun return ret;
1442*4882a593Smuzhiyun }
1443*4882a593Smuzhiyun
poll_urgent(struct file * fp,struct poll_table_struct * pt)1444*4882a593Smuzhiyun static __poll_t poll_urgent(struct file *fp,
1445*4882a593Smuzhiyun struct poll_table_struct *pt)
1446*4882a593Smuzhiyun {
1447*4882a593Smuzhiyun struct hfi1_filedata *fd = fp->private_data;
1448*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt = fd->uctxt;
1449*4882a593Smuzhiyun struct hfi1_devdata *dd = uctxt->dd;
1450*4882a593Smuzhiyun __poll_t pollflag;
1451*4882a593Smuzhiyun
1452*4882a593Smuzhiyun poll_wait(fp, &uctxt->wait, pt);
1453*4882a593Smuzhiyun
1454*4882a593Smuzhiyun spin_lock_irq(&dd->uctxt_lock);
1455*4882a593Smuzhiyun if (uctxt->urgent != uctxt->urgent_poll) {
1456*4882a593Smuzhiyun pollflag = EPOLLIN | EPOLLRDNORM;
1457*4882a593Smuzhiyun uctxt->urgent_poll = uctxt->urgent;
1458*4882a593Smuzhiyun } else {
1459*4882a593Smuzhiyun pollflag = 0;
1460*4882a593Smuzhiyun set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags);
1461*4882a593Smuzhiyun }
1462*4882a593Smuzhiyun spin_unlock_irq(&dd->uctxt_lock);
1463*4882a593Smuzhiyun
1464*4882a593Smuzhiyun return pollflag;
1465*4882a593Smuzhiyun }
1466*4882a593Smuzhiyun
poll_next(struct file * fp,struct poll_table_struct * pt)1467*4882a593Smuzhiyun static __poll_t poll_next(struct file *fp,
1468*4882a593Smuzhiyun struct poll_table_struct *pt)
1469*4882a593Smuzhiyun {
1470*4882a593Smuzhiyun struct hfi1_filedata *fd = fp->private_data;
1471*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt = fd->uctxt;
1472*4882a593Smuzhiyun struct hfi1_devdata *dd = uctxt->dd;
1473*4882a593Smuzhiyun __poll_t pollflag;
1474*4882a593Smuzhiyun
1475*4882a593Smuzhiyun poll_wait(fp, &uctxt->wait, pt);
1476*4882a593Smuzhiyun
1477*4882a593Smuzhiyun spin_lock_irq(&dd->uctxt_lock);
1478*4882a593Smuzhiyun if (hdrqempty(uctxt)) {
1479*4882a593Smuzhiyun set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags);
1480*4882a593Smuzhiyun hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt);
1481*4882a593Smuzhiyun pollflag = 0;
1482*4882a593Smuzhiyun } else {
1483*4882a593Smuzhiyun pollflag = EPOLLIN | EPOLLRDNORM;
1484*4882a593Smuzhiyun }
1485*4882a593Smuzhiyun spin_unlock_irq(&dd->uctxt_lock);
1486*4882a593Smuzhiyun
1487*4882a593Smuzhiyun return pollflag;
1488*4882a593Smuzhiyun }
1489*4882a593Smuzhiyun
1490*4882a593Smuzhiyun /*
1491*4882a593Smuzhiyun * Find all user contexts in use, and set the specified bit in their
1492*4882a593Smuzhiyun * event mask.
1493*4882a593Smuzhiyun * See also find_ctxt() for a similar use, that is specific to send buffers.
1494*4882a593Smuzhiyun */
hfi1_set_uevent_bits(struct hfi1_pportdata * ppd,const int evtbit)1495*4882a593Smuzhiyun int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit)
1496*4882a593Smuzhiyun {
1497*4882a593Smuzhiyun struct hfi1_ctxtdata *uctxt;
1498*4882a593Smuzhiyun struct hfi1_devdata *dd = ppd->dd;
1499*4882a593Smuzhiyun u16 ctxt;
1500*4882a593Smuzhiyun
1501*4882a593Smuzhiyun if (!dd->events)
1502*4882a593Smuzhiyun return -EINVAL;
1503*4882a593Smuzhiyun
1504*4882a593Smuzhiyun for (ctxt = dd->first_dyn_alloc_ctxt; ctxt < dd->num_rcv_contexts;
1505*4882a593Smuzhiyun ctxt++) {
1506*4882a593Smuzhiyun uctxt = hfi1_rcd_get_by_index(dd, ctxt);
1507*4882a593Smuzhiyun if (uctxt) {
1508*4882a593Smuzhiyun unsigned long *evs;
1509*4882a593Smuzhiyun int i;
1510*4882a593Smuzhiyun /*
1511*4882a593Smuzhiyun * subctxt_cnt is 0 if not shared, so do base
1512*4882a593Smuzhiyun * separately, first, then remaining subctxt, if any
1513*4882a593Smuzhiyun */
1514*4882a593Smuzhiyun evs = dd->events + uctxt_offset(uctxt);
1515*4882a593Smuzhiyun set_bit(evtbit, evs);
1516*4882a593Smuzhiyun for (i = 1; i < uctxt->subctxt_cnt; i++)
1517*4882a593Smuzhiyun set_bit(evtbit, evs + i);
1518*4882a593Smuzhiyun hfi1_rcd_put(uctxt);
1519*4882a593Smuzhiyun }
1520*4882a593Smuzhiyun }
1521*4882a593Smuzhiyun
1522*4882a593Smuzhiyun return 0;
1523*4882a593Smuzhiyun }
1524*4882a593Smuzhiyun
1525*4882a593Smuzhiyun /**
1526*4882a593Smuzhiyun * manage_rcvq - manage a context's receive queue
1527*4882a593Smuzhiyun * @uctxt: the context
1528*4882a593Smuzhiyun * @subctxt: the sub-context
1529*4882a593Smuzhiyun * @start_stop: action to carry out
1530*4882a593Smuzhiyun *
1531*4882a593Smuzhiyun * start_stop == 0 disables receive on the context, for use in queue
1532*4882a593Smuzhiyun * overflow conditions. start_stop==1 re-enables, to be used to
1533*4882a593Smuzhiyun * re-init the software copy of the head register
1534*4882a593Smuzhiyun */
manage_rcvq(struct hfi1_ctxtdata * uctxt,u16 subctxt,unsigned long arg)1535*4882a593Smuzhiyun static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
1536*4882a593Smuzhiyun unsigned long arg)
1537*4882a593Smuzhiyun {
1538*4882a593Smuzhiyun struct hfi1_devdata *dd = uctxt->dd;
1539*4882a593Smuzhiyun unsigned int rcvctrl_op;
1540*4882a593Smuzhiyun int start_stop;
1541*4882a593Smuzhiyun
1542*4882a593Smuzhiyun if (subctxt)
1543*4882a593Smuzhiyun return 0;
1544*4882a593Smuzhiyun
1545*4882a593Smuzhiyun if (get_user(start_stop, (int __user *)arg))
1546*4882a593Smuzhiyun return -EFAULT;
1547*4882a593Smuzhiyun
1548*4882a593Smuzhiyun /* atomically clear receive enable ctxt. */
1549*4882a593Smuzhiyun if (start_stop) {
1550*4882a593Smuzhiyun /*
1551*4882a593Smuzhiyun * On enable, force in-memory copy of the tail register to
1552*4882a593Smuzhiyun * 0, so that protocol code doesn't have to worry about
1553*4882a593Smuzhiyun * whether or not the chip has yet updated the in-memory
1554*4882a593Smuzhiyun * copy or not on return from the system call. The chip
1555*4882a593Smuzhiyun * always resets it's tail register back to 0 on a
1556*4882a593Smuzhiyun * transition from disabled to enabled.
1557*4882a593Smuzhiyun */
1558*4882a593Smuzhiyun if (hfi1_rcvhdrtail_kvaddr(uctxt))
1559*4882a593Smuzhiyun clear_rcvhdrtail(uctxt);
1560*4882a593Smuzhiyun rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB;
1561*4882a593Smuzhiyun } else {
1562*4882a593Smuzhiyun rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS;
1563*4882a593Smuzhiyun }
1564*4882a593Smuzhiyun hfi1_rcvctrl(dd, rcvctrl_op, uctxt);
1565*4882a593Smuzhiyun /* always; new head should be equal to new tail; see above */
1566*4882a593Smuzhiyun
1567*4882a593Smuzhiyun return 0;
1568*4882a593Smuzhiyun }
1569*4882a593Smuzhiyun
1570*4882a593Smuzhiyun /*
1571*4882a593Smuzhiyun * clear the event notifier events for this context.
1572*4882a593Smuzhiyun * User process then performs actions appropriate to bit having been
1573*4882a593Smuzhiyun * set, if desired, and checks again in future.
1574*4882a593Smuzhiyun */
user_event_ack(struct hfi1_ctxtdata * uctxt,u16 subctxt,unsigned long arg)1575*4882a593Smuzhiyun static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
1576*4882a593Smuzhiyun unsigned long arg)
1577*4882a593Smuzhiyun {
1578*4882a593Smuzhiyun int i;
1579*4882a593Smuzhiyun struct hfi1_devdata *dd = uctxt->dd;
1580*4882a593Smuzhiyun unsigned long *evs;
1581*4882a593Smuzhiyun unsigned long events;
1582*4882a593Smuzhiyun
1583*4882a593Smuzhiyun if (!dd->events)
1584*4882a593Smuzhiyun return 0;
1585*4882a593Smuzhiyun
1586*4882a593Smuzhiyun if (get_user(events, (unsigned long __user *)arg))
1587*4882a593Smuzhiyun return -EFAULT;
1588*4882a593Smuzhiyun
1589*4882a593Smuzhiyun evs = dd->events + uctxt_offset(uctxt) + subctxt;
1590*4882a593Smuzhiyun
1591*4882a593Smuzhiyun for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) {
1592*4882a593Smuzhiyun if (!test_bit(i, &events))
1593*4882a593Smuzhiyun continue;
1594*4882a593Smuzhiyun clear_bit(i, evs);
1595*4882a593Smuzhiyun }
1596*4882a593Smuzhiyun return 0;
1597*4882a593Smuzhiyun }
1598*4882a593Smuzhiyun
set_ctxt_pkey(struct hfi1_ctxtdata * uctxt,unsigned long arg)1599*4882a593Smuzhiyun static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned long arg)
1600*4882a593Smuzhiyun {
1601*4882a593Smuzhiyun int i;
1602*4882a593Smuzhiyun struct hfi1_pportdata *ppd = uctxt->ppd;
1603*4882a593Smuzhiyun struct hfi1_devdata *dd = uctxt->dd;
1604*4882a593Smuzhiyun u16 pkey;
1605*4882a593Smuzhiyun
1606*4882a593Smuzhiyun if (!HFI1_CAP_IS_USET(PKEY_CHECK))
1607*4882a593Smuzhiyun return -EPERM;
1608*4882a593Smuzhiyun
1609*4882a593Smuzhiyun if (get_user(pkey, (u16 __user *)arg))
1610*4882a593Smuzhiyun return -EFAULT;
1611*4882a593Smuzhiyun
1612*4882a593Smuzhiyun if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY)
1613*4882a593Smuzhiyun return -EINVAL;
1614*4882a593Smuzhiyun
1615*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++)
1616*4882a593Smuzhiyun if (pkey == ppd->pkeys[i])
1617*4882a593Smuzhiyun return hfi1_set_ctxt_pkey(dd, uctxt, pkey);
1618*4882a593Smuzhiyun
1619*4882a593Smuzhiyun return -ENOENT;
1620*4882a593Smuzhiyun }
1621*4882a593Smuzhiyun
1622*4882a593Smuzhiyun /**
1623*4882a593Smuzhiyun * ctxt_reset - Reset the user context
1624*4882a593Smuzhiyun * @uctxt: valid user context
1625*4882a593Smuzhiyun */
ctxt_reset(struct hfi1_ctxtdata * uctxt)1626*4882a593Smuzhiyun static int ctxt_reset(struct hfi1_ctxtdata *uctxt)
1627*4882a593Smuzhiyun {
1628*4882a593Smuzhiyun struct send_context *sc;
1629*4882a593Smuzhiyun struct hfi1_devdata *dd;
1630*4882a593Smuzhiyun int ret = 0;
1631*4882a593Smuzhiyun
1632*4882a593Smuzhiyun if (!uctxt || !uctxt->dd || !uctxt->sc)
1633*4882a593Smuzhiyun return -EINVAL;
1634*4882a593Smuzhiyun
1635*4882a593Smuzhiyun /*
1636*4882a593Smuzhiyun * There is no protection here. User level has to guarantee that
1637*4882a593Smuzhiyun * no one will be writing to the send context while it is being
1638*4882a593Smuzhiyun * re-initialized. If user level breaks that guarantee, it will
1639*4882a593Smuzhiyun * break it's own context and no one else's.
1640*4882a593Smuzhiyun */
1641*4882a593Smuzhiyun dd = uctxt->dd;
1642*4882a593Smuzhiyun sc = uctxt->sc;
1643*4882a593Smuzhiyun
1644*4882a593Smuzhiyun /*
1645*4882a593Smuzhiyun * Wait until the interrupt handler has marked the context as
1646*4882a593Smuzhiyun * halted or frozen. Report error if we time out.
1647*4882a593Smuzhiyun */
1648*4882a593Smuzhiyun wait_event_interruptible_timeout(
1649*4882a593Smuzhiyun sc->halt_wait, (sc->flags & SCF_HALTED),
1650*4882a593Smuzhiyun msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
1651*4882a593Smuzhiyun if (!(sc->flags & SCF_HALTED))
1652*4882a593Smuzhiyun return -ENOLCK;
1653*4882a593Smuzhiyun
1654*4882a593Smuzhiyun /*
1655*4882a593Smuzhiyun * If the send context was halted due to a Freeze, wait until the
1656*4882a593Smuzhiyun * device has been "unfrozen" before resetting the context.
1657*4882a593Smuzhiyun */
1658*4882a593Smuzhiyun if (sc->flags & SCF_FROZEN) {
1659*4882a593Smuzhiyun wait_event_interruptible_timeout(
1660*4882a593Smuzhiyun dd->event_queue,
1661*4882a593Smuzhiyun !(READ_ONCE(dd->flags) & HFI1_FROZEN),
1662*4882a593Smuzhiyun msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
1663*4882a593Smuzhiyun if (dd->flags & HFI1_FROZEN)
1664*4882a593Smuzhiyun return -ENOLCK;
1665*4882a593Smuzhiyun
1666*4882a593Smuzhiyun if (dd->flags & HFI1_FORCED_FREEZE)
1667*4882a593Smuzhiyun /*
1668*4882a593Smuzhiyun * Don't allow context reset if we are into
1669*4882a593Smuzhiyun * forced freeze
1670*4882a593Smuzhiyun */
1671*4882a593Smuzhiyun return -ENODEV;
1672*4882a593Smuzhiyun
1673*4882a593Smuzhiyun sc_disable(sc);
1674*4882a593Smuzhiyun ret = sc_enable(sc);
1675*4882a593Smuzhiyun hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB, uctxt);
1676*4882a593Smuzhiyun } else {
1677*4882a593Smuzhiyun ret = sc_restart(sc);
1678*4882a593Smuzhiyun }
1679*4882a593Smuzhiyun if (!ret)
1680*4882a593Smuzhiyun sc_return_credits(sc);
1681*4882a593Smuzhiyun
1682*4882a593Smuzhiyun return ret;
1683*4882a593Smuzhiyun }
1684*4882a593Smuzhiyun
user_remove(struct hfi1_devdata * dd)1685*4882a593Smuzhiyun static void user_remove(struct hfi1_devdata *dd)
1686*4882a593Smuzhiyun {
1687*4882a593Smuzhiyun
1688*4882a593Smuzhiyun hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device);
1689*4882a593Smuzhiyun }
1690*4882a593Smuzhiyun
user_add(struct hfi1_devdata * dd)1691*4882a593Smuzhiyun static int user_add(struct hfi1_devdata *dd)
1692*4882a593Smuzhiyun {
1693*4882a593Smuzhiyun char name[10];
1694*4882a593Smuzhiyun int ret;
1695*4882a593Smuzhiyun
1696*4882a593Smuzhiyun snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit);
1697*4882a593Smuzhiyun ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops,
1698*4882a593Smuzhiyun &dd->user_cdev, &dd->user_device,
1699*4882a593Smuzhiyun true, &dd->verbs_dev.rdi.ibdev.dev.kobj);
1700*4882a593Smuzhiyun if (ret)
1701*4882a593Smuzhiyun user_remove(dd);
1702*4882a593Smuzhiyun
1703*4882a593Smuzhiyun return ret;
1704*4882a593Smuzhiyun }
1705*4882a593Smuzhiyun
1706*4882a593Smuzhiyun /*
1707*4882a593Smuzhiyun * Create per-unit files in /dev
1708*4882a593Smuzhiyun */
hfi1_device_create(struct hfi1_devdata * dd)1709*4882a593Smuzhiyun int hfi1_device_create(struct hfi1_devdata *dd)
1710*4882a593Smuzhiyun {
1711*4882a593Smuzhiyun return user_add(dd);
1712*4882a593Smuzhiyun }
1713*4882a593Smuzhiyun
1714*4882a593Smuzhiyun /*
1715*4882a593Smuzhiyun * Remove per-unit files in /dev
1716*4882a593Smuzhiyun * void, core kernel returns no errors for this stuff
1717*4882a593Smuzhiyun */
hfi1_device_remove(struct hfi1_devdata * dd)1718*4882a593Smuzhiyun void hfi1_device_remove(struct hfi1_devdata *dd)
1719*4882a593Smuzhiyun {
1720*4882a593Smuzhiyun user_remove(dd);
1721*4882a593Smuzhiyun }
1722