Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1 | /* |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 2 | * Copyright(c) 2015-2017 Intel Corporation. |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 3 | * |
| 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 Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 9 | * 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 Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 20 | * 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 Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 47 | #include <linux/poll.h> |
| 48 | #include <linux/cdev.h> |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 49 | #include <linux/vmalloc.h> |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 50 | #include <linux/io.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 51 | #include <linux/sched/mm.h> |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 52 | |
Jason Gunthorpe | e6bd18f | 2016-04-10 19:13:13 -0600 | [diff] [blame] | 53 | #include <rdma/ib.h> |
| 54 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 55 | #include "hfi.h" |
| 56 | #include "pio.h" |
| 57 | #include "device.h" |
| 58 | #include "common.h" |
| 59 | #include "trace.h" |
| 60 | #include "user_sdma.h" |
Mitko Haralanov | 701e441 | 2015-10-30 18:58:43 -0400 | [diff] [blame] | 61 | #include "user_exp_rcv.h" |
Ashutosh Dixit | affa48d | 2016-02-03 14:33:06 -0800 | [diff] [blame] | 62 | #include "aspm.h" |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 63 | #include "mmu_rb.h" |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 64 | |
| 65 | #undef pr_fmt |
| 66 | #define pr_fmt(fmt) DRIVER_NAME ": " fmt |
| 67 | |
| 68 | #define SEND_CTXT_HALT_TIMEOUT 1000 /* msecs */ |
| 69 | |
| 70 | /* |
| 71 | * File operation functions |
| 72 | */ |
Michael J. Ruhl | f4cd876 | 2017-05-04 05:14:39 -0700 | [diff] [blame] | 73 | static int hfi1_file_open(struct inode *inode, struct file *fp); |
| 74 | static int hfi1_file_close(struct inode *inode, struct file *fp); |
| 75 | static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from); |
| 76 | static unsigned int hfi1_poll(struct file *fp, struct poll_table_struct *pt); |
| 77 | static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 78 | |
Michael J. Ruhl | f4cd876 | 2017-05-04 05:14:39 -0700 | [diff] [blame] | 79 | static u64 kvirt_to_phys(void *addr); |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 80 | static int assign_ctxt(struct hfi1_filedata *fd, struct hfi1_user_info *uinfo); |
Michael J. Ruhl | f4cd876 | 2017-05-04 05:14:39 -0700 | [diff] [blame] | 81 | static int init_subctxts(struct hfi1_ctxtdata *uctxt, |
| 82 | const struct hfi1_user_info *uinfo); |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 83 | static int user_init(struct hfi1_filedata *fd); |
| 84 | static int get_ctxt_info(struct hfi1_filedata *fd, void __user *ubase, |
| 85 | __u32 len); |
| 86 | static int get_base_info(struct hfi1_filedata *fd, void __user *ubase, |
| 87 | __u32 len); |
| 88 | static int setup_ctxt(struct hfi1_filedata *fd); |
Michael J. Ruhl | f4cd876 | 2017-05-04 05:14:39 -0700 | [diff] [blame] | 89 | static int setup_subctxt(struct hfi1_ctxtdata *uctxt); |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 90 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 91 | static int find_shared_ctxt(struct hfi1_filedata *fd, |
Michael J. Ruhl | f4cd876 | 2017-05-04 05:14:39 -0700 | [diff] [blame] | 92 | const struct hfi1_user_info *uinfo); |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 93 | static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd, |
Michael J. Ruhl | f4cd876 | 2017-05-04 05:14:39 -0700 | [diff] [blame] | 94 | struct hfi1_user_info *uinfo); |
| 95 | static unsigned int poll_urgent(struct file *fp, struct poll_table_struct *pt); |
| 96 | static unsigned int poll_next(struct file *fp, struct poll_table_struct *pt); |
| 97 | static int user_event_ack(struct hfi1_ctxtdata *uctxt, int subctxt, |
| 98 | unsigned long events); |
| 99 | static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned subctxt, |
| 100 | u16 pkey); |
| 101 | static int manage_rcvq(struct hfi1_ctxtdata *uctxt, unsigned subctxt, |
| 102 | int start_stop); |
| 103 | static int vma_fault(struct vm_fault *vmf); |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 104 | static long hfi1_file_ioctl(struct file *fp, unsigned int cmd, |
| 105 | unsigned long arg); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 106 | |
| 107 | static const struct file_operations hfi1_file_ops = { |
| 108 | .owner = THIS_MODULE, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 109 | .write_iter = hfi1_write_iter, |
| 110 | .open = hfi1_file_open, |
| 111 | .release = hfi1_file_close, |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 112 | .unlocked_ioctl = hfi1_file_ioctl, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 113 | .poll = hfi1_poll, |
| 114 | .mmap = hfi1_file_mmap, |
| 115 | .llseek = noop_llseek, |
| 116 | }; |
| 117 | |
| 118 | static struct vm_operations_struct vm_ops = { |
| 119 | .fault = vma_fault, |
| 120 | }; |
| 121 | |
| 122 | /* |
| 123 | * Types of memories mapped into user processes' space |
| 124 | */ |
| 125 | enum mmap_types { |
| 126 | PIO_BUFS = 1, |
| 127 | PIO_BUFS_SOP, |
| 128 | PIO_CRED, |
| 129 | RCV_HDRQ, |
| 130 | RCV_EGRBUF, |
| 131 | UREGS, |
| 132 | EVENTS, |
| 133 | STATUS, |
| 134 | RTAIL, |
| 135 | SUBCTXT_UREGS, |
| 136 | SUBCTXT_RCV_HDRQ, |
| 137 | SUBCTXT_EGRBUF, |
| 138 | SDMA_COMP |
| 139 | }; |
| 140 | |
| 141 | /* |
| 142 | * Masks and offsets defining the mmap tokens |
| 143 | */ |
| 144 | #define HFI1_MMAP_OFFSET_MASK 0xfffULL |
| 145 | #define HFI1_MMAP_OFFSET_SHIFT 0 |
| 146 | #define HFI1_MMAP_SUBCTXT_MASK 0xfULL |
| 147 | #define HFI1_MMAP_SUBCTXT_SHIFT 12 |
| 148 | #define HFI1_MMAP_CTXT_MASK 0xffULL |
| 149 | #define HFI1_MMAP_CTXT_SHIFT 16 |
| 150 | #define HFI1_MMAP_TYPE_MASK 0xfULL |
| 151 | #define HFI1_MMAP_TYPE_SHIFT 24 |
| 152 | #define HFI1_MMAP_MAGIC_MASK 0xffffffffULL |
| 153 | #define HFI1_MMAP_MAGIC_SHIFT 32 |
| 154 | |
| 155 | #define HFI1_MMAP_MAGIC 0xdabbad00 |
| 156 | |
| 157 | #define HFI1_MMAP_TOKEN_SET(field, val) \ |
| 158 | (((val) & HFI1_MMAP_##field##_MASK) << HFI1_MMAP_##field##_SHIFT) |
| 159 | #define HFI1_MMAP_TOKEN_GET(field, token) \ |
| 160 | (((token) >> HFI1_MMAP_##field##_SHIFT) & HFI1_MMAP_##field##_MASK) |
| 161 | #define HFI1_MMAP_TOKEN(type, ctxt, subctxt, addr) \ |
| 162 | (HFI1_MMAP_TOKEN_SET(MAGIC, HFI1_MMAP_MAGIC) | \ |
| 163 | HFI1_MMAP_TOKEN_SET(TYPE, type) | \ |
| 164 | HFI1_MMAP_TOKEN_SET(CTXT, ctxt) | \ |
| 165 | HFI1_MMAP_TOKEN_SET(SUBCTXT, subctxt) | \ |
Geliang Tang | e260e40 | 2015-10-03 10:34:59 +0800 | [diff] [blame] | 166 | HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr)))) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 167 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 168 | #define dbg(fmt, ...) \ |
| 169 | pr_info(fmt, ##__VA_ARGS__) |
| 170 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 171 | static inline int is_valid_mmap(u64 token) |
| 172 | { |
| 173 | return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC); |
| 174 | } |
| 175 | |
| 176 | static int hfi1_file_open(struct inode *inode, struct file *fp) |
| 177 | { |
Ira Weiny | ea3a0ee | 2016-07-28 12:27:35 -0400 | [diff] [blame] | 178 | struct hfi1_filedata *fd; |
Dennis Dalessandro | e11ffbd | 2016-05-19 05:26:44 -0700 | [diff] [blame] | 179 | struct hfi1_devdata *dd = container_of(inode->i_cdev, |
| 180 | struct hfi1_devdata, |
| 181 | user_cdev); |
| 182 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 183 | if (!((dd->flags & HFI1_PRESENT) && dd->kregbase)) |
| 184 | return -EINVAL; |
| 185 | |
Tadeusz Struk | acd7c8f | 2016-10-25 08:57:55 -0700 | [diff] [blame] | 186 | if (!atomic_inc_not_zero(&dd->user_refcount)) |
| 187 | return -ENXIO; |
| 188 | |
Dennis Dalessandro | e11ffbd | 2016-05-19 05:26:44 -0700 | [diff] [blame] | 189 | /* Just take a ref now. Not all opens result in a context assign */ |
| 190 | kobject_get(&dd->kobj); |
| 191 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 192 | /* The real work is performed later in assign_ctxt() */ |
Ira Weiny | ea3a0ee | 2016-07-28 12:27:35 -0400 | [diff] [blame] | 193 | |
| 194 | fd = kzalloc(sizeof(*fd), GFP_KERNEL); |
| 195 | |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame] | 196 | if (fd) { |
| 197 | fd->rec_cpu_num = -1; /* no cpu affinity by default */ |
| 198 | fd->mm = current->mm; |
Vegard Nossum | f1f1007 | 2017-02-27 14:30:07 -0800 | [diff] [blame] | 199 | mmgrab(fd->mm); |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 200 | fd->dd = dd; |
Tadeusz Struk | acd7c8f | 2016-10-25 08:57:55 -0700 | [diff] [blame] | 201 | fp->private_data = fd; |
| 202 | } else { |
| 203 | fp->private_data = NULL; |
| 204 | |
| 205 | if (atomic_dec_and_test(&dd->user_refcount)) |
| 206 | complete(&dd->user_comp); |
| 207 | |
| 208 | return -ENOMEM; |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame] | 209 | } |
Ira Weiny | ea3a0ee | 2016-07-28 12:27:35 -0400 | [diff] [blame] | 210 | |
Tadeusz Struk | acd7c8f | 2016-10-25 08:57:55 -0700 | [diff] [blame] | 211 | return 0; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 212 | } |
| 213 | |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 214 | static long hfi1_file_ioctl(struct file *fp, unsigned int cmd, |
| 215 | unsigned long arg) |
| 216 | { |
| 217 | struct hfi1_filedata *fd = fp->private_data; |
| 218 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
| 219 | struct hfi1_user_info uinfo; |
| 220 | struct hfi1_tid_info tinfo; |
| 221 | int ret = 0; |
| 222 | unsigned long addr; |
| 223 | int uval = 0; |
| 224 | unsigned long ul_uval = 0; |
| 225 | u16 uval16 = 0; |
| 226 | |
Dennis Dalessandro | 8a1882e | 2016-05-19 05:26:37 -0700 | [diff] [blame] | 227 | hfi1_cdbg(IOCTL, "IOCTL recv: 0x%x", cmd); |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 228 | if (cmd != HFI1_IOCTL_ASSIGN_CTXT && |
| 229 | cmd != HFI1_IOCTL_GET_VERS && |
| 230 | !uctxt) |
| 231 | return -EINVAL; |
| 232 | |
| 233 | switch (cmd) { |
| 234 | case HFI1_IOCTL_ASSIGN_CTXT: |
Ira Weiny | ca2f30a | 2016-06-09 07:51:33 -0700 | [diff] [blame] | 235 | if (uctxt) |
| 236 | return -EINVAL; |
| 237 | |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 238 | if (copy_from_user(&uinfo, |
| 239 | (struct hfi1_user_info __user *)arg, |
| 240 | sizeof(uinfo))) |
| 241 | return -EFAULT; |
| 242 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 243 | ret = assign_ctxt(fd, &uinfo); |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 244 | if (ret < 0) |
| 245 | return ret; |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 246 | ret = setup_ctxt(fd); |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 247 | if (ret) |
| 248 | return ret; |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 249 | ret = user_init(fd); |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 250 | break; |
| 251 | case HFI1_IOCTL_CTXT_INFO: |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 252 | ret = get_ctxt_info(fd, (void __user *)(unsigned long)arg, |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 253 | sizeof(struct hfi1_ctxt_info)); |
| 254 | break; |
| 255 | case HFI1_IOCTL_USER_INFO: |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 256 | ret = get_base_info(fd, (void __user *)(unsigned long)arg, |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 257 | sizeof(struct hfi1_base_info)); |
| 258 | break; |
| 259 | case HFI1_IOCTL_CREDIT_UPD: |
Markus Elfring | f7ca535 | 2016-07-23 08:30:52 +0200 | [diff] [blame] | 260 | if (uctxt) |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 261 | sc_return_credits(uctxt->sc); |
| 262 | break; |
| 263 | |
| 264 | case HFI1_IOCTL_TID_UPDATE: |
| 265 | if (copy_from_user(&tinfo, |
| 266 | (struct hfi11_tid_info __user *)arg, |
| 267 | sizeof(tinfo))) |
| 268 | return -EFAULT; |
| 269 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 270 | ret = hfi1_user_exp_rcv_setup(fd, &tinfo); |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 271 | if (!ret) { |
| 272 | /* |
| 273 | * Copy the number of tidlist entries we used |
| 274 | * and the length of the buffer we registered. |
| 275 | * These fields are adjacent in the structure so |
| 276 | * we can copy them at the same time. |
| 277 | */ |
| 278 | addr = arg + offsetof(struct hfi1_tid_info, tidcnt); |
| 279 | if (copy_to_user((void __user *)addr, &tinfo.tidcnt, |
| 280 | sizeof(tinfo.tidcnt) + |
| 281 | sizeof(tinfo.length))) |
| 282 | ret = -EFAULT; |
| 283 | } |
| 284 | break; |
| 285 | |
| 286 | case HFI1_IOCTL_TID_FREE: |
| 287 | if (copy_from_user(&tinfo, |
| 288 | (struct hfi11_tid_info __user *)arg, |
| 289 | sizeof(tinfo))) |
| 290 | return -EFAULT; |
| 291 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 292 | ret = hfi1_user_exp_rcv_clear(fd, &tinfo); |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 293 | if (ret) |
| 294 | break; |
| 295 | addr = arg + offsetof(struct hfi1_tid_info, tidcnt); |
| 296 | if (copy_to_user((void __user *)addr, &tinfo.tidcnt, |
| 297 | sizeof(tinfo.tidcnt))) |
| 298 | ret = -EFAULT; |
| 299 | break; |
| 300 | |
| 301 | case HFI1_IOCTL_TID_INVAL_READ: |
| 302 | if (copy_from_user(&tinfo, |
| 303 | (struct hfi11_tid_info __user *)arg, |
| 304 | sizeof(tinfo))) |
| 305 | return -EFAULT; |
| 306 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 307 | ret = hfi1_user_exp_rcv_invalid(fd, &tinfo); |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 308 | if (ret) |
| 309 | break; |
| 310 | addr = arg + offsetof(struct hfi1_tid_info, tidcnt); |
| 311 | if (copy_to_user((void __user *)addr, &tinfo.tidcnt, |
| 312 | sizeof(tinfo.tidcnt))) |
| 313 | ret = -EFAULT; |
| 314 | break; |
| 315 | |
| 316 | case HFI1_IOCTL_RECV_CTRL: |
| 317 | ret = get_user(uval, (int __user *)arg); |
| 318 | if (ret != 0) |
| 319 | return -EFAULT; |
| 320 | ret = manage_rcvq(uctxt, fd->subctxt, uval); |
| 321 | break; |
| 322 | |
| 323 | case HFI1_IOCTL_POLL_TYPE: |
| 324 | ret = get_user(uval, (int __user *)arg); |
| 325 | if (ret != 0) |
| 326 | return -EFAULT; |
| 327 | uctxt->poll_type = (typeof(uctxt->poll_type))uval; |
| 328 | break; |
| 329 | |
| 330 | case HFI1_IOCTL_ACK_EVENT: |
| 331 | ret = get_user(ul_uval, (unsigned long __user *)arg); |
| 332 | if (ret != 0) |
| 333 | return -EFAULT; |
| 334 | ret = user_event_ack(uctxt, fd->subctxt, ul_uval); |
| 335 | break; |
| 336 | |
| 337 | case HFI1_IOCTL_SET_PKEY: |
| 338 | ret = get_user(uval16, (u16 __user *)arg); |
| 339 | if (ret != 0) |
| 340 | return -EFAULT; |
| 341 | if (HFI1_CAP_IS_USET(PKEY_CHECK)) |
| 342 | ret = set_ctxt_pkey(uctxt, fd->subctxt, uval16); |
| 343 | else |
| 344 | return -EPERM; |
| 345 | break; |
| 346 | |
| 347 | case HFI1_IOCTL_CTXT_RESET: { |
| 348 | struct send_context *sc; |
| 349 | struct hfi1_devdata *dd; |
| 350 | |
| 351 | if (!uctxt || !uctxt->dd || !uctxt->sc) |
| 352 | return -EINVAL; |
| 353 | |
| 354 | /* |
| 355 | * There is no protection here. User level has to |
| 356 | * guarantee that no one will be writing to the send |
| 357 | * context while it is being re-initialized. |
| 358 | * If user level breaks that guarantee, it will break |
| 359 | * it's own context and no one else's. |
| 360 | */ |
| 361 | dd = uctxt->dd; |
| 362 | sc = uctxt->sc; |
| 363 | /* |
| 364 | * Wait until the interrupt handler has marked the |
| 365 | * context as halted or frozen. Report error if we time |
| 366 | * out. |
| 367 | */ |
| 368 | wait_event_interruptible_timeout( |
| 369 | sc->halt_wait, (sc->flags & SCF_HALTED), |
| 370 | msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT)); |
| 371 | if (!(sc->flags & SCF_HALTED)) |
| 372 | return -ENOLCK; |
| 373 | |
| 374 | /* |
| 375 | * If the send context was halted due to a Freeze, |
| 376 | * wait until the device has been "unfrozen" before |
| 377 | * resetting the context. |
| 378 | */ |
| 379 | if (sc->flags & SCF_FROZEN) { |
| 380 | wait_event_interruptible_timeout( |
| 381 | dd->event_queue, |
| 382 | !(ACCESS_ONCE(dd->flags) & HFI1_FROZEN), |
| 383 | msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT)); |
| 384 | if (dd->flags & HFI1_FROZEN) |
| 385 | return -ENOLCK; |
| 386 | |
| 387 | if (dd->flags & HFI1_FORCED_FREEZE) |
| 388 | /* |
| 389 | * Don't allow context reset if we are into |
| 390 | * forced freeze |
| 391 | */ |
| 392 | return -ENODEV; |
| 393 | |
| 394 | sc_disable(sc); |
| 395 | ret = sc_enable(sc); |
| 396 | hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB, |
| 397 | uctxt->ctxt); |
| 398 | } else { |
| 399 | ret = sc_restart(sc); |
| 400 | } |
| 401 | if (!ret) |
| 402 | sc_return_credits(sc); |
| 403 | break; |
| 404 | } |
| 405 | |
| 406 | case HFI1_IOCTL_GET_VERS: |
| 407 | uval = HFI1_USER_SWVERSION; |
| 408 | if (put_user(uval, (int __user *)arg)) |
| 409 | return -EFAULT; |
| 410 | break; |
| 411 | |
| 412 | default: |
| 413 | return -EINVAL; |
| 414 | } |
| 415 | |
| 416 | return ret; |
| 417 | } |
| 418 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 419 | static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from) |
| 420 | { |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 421 | struct hfi1_filedata *fd = kiocb->ki_filp->private_data; |
| 422 | struct hfi1_user_sdma_pkt_q *pq = fd->pq; |
| 423 | struct hfi1_user_sdma_comp_q *cq = fd->cq; |
Ira Weiny | 0904f32 | 2016-07-01 16:00:55 -0700 | [diff] [blame] | 424 | int done = 0, reqs = 0; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 425 | unsigned long dim = from->nr_segs; |
| 426 | |
Ira Weiny | 0904f32 | 2016-07-01 16:00:55 -0700 | [diff] [blame] | 427 | if (!cq || !pq) |
| 428 | return -EIO; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 429 | |
Ira Weiny | 0904f32 | 2016-07-01 16:00:55 -0700 | [diff] [blame] | 430 | if (!iter_is_iovec(from) || !dim) |
| 431 | return -EINVAL; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 432 | |
| 433 | hfi1_cdbg(SDMA, "SDMA request from %u:%u (%lu)", |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 434 | fd->uctxt->ctxt, fd->subctxt, dim); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 435 | |
Ira Weiny | 0904f32 | 2016-07-01 16:00:55 -0700 | [diff] [blame] | 436 | if (atomic_read(&pq->n_reqs) == pq->n_max_reqs) |
| 437 | return -ENOSPC; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 438 | |
| 439 | while (dim) { |
Ira Weiny | 0904f32 | 2016-07-01 16:00:55 -0700 | [diff] [blame] | 440 | int ret; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 441 | unsigned long count = 0; |
| 442 | |
| 443 | ret = hfi1_user_sdma_process_request( |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 444 | fd, (struct iovec *)(from->iov + done), |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 445 | dim, &count); |
Ira Weiny | 0904f32 | 2016-07-01 16:00:55 -0700 | [diff] [blame] | 446 | if (ret) { |
| 447 | reqs = ret; |
| 448 | break; |
| 449 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 450 | dim -= count; |
| 451 | done += count; |
| 452 | reqs++; |
| 453 | } |
Ira Weiny | 0904f32 | 2016-07-01 16:00:55 -0700 | [diff] [blame] | 454 | |
| 455 | return reqs; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma) |
| 459 | { |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 460 | struct hfi1_filedata *fd = fp->private_data; |
| 461 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 462 | struct hfi1_devdata *dd; |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 463 | unsigned long flags; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 464 | u64 token = vma->vm_pgoff << PAGE_SHIFT, |
| 465 | memaddr = 0; |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 466 | void *memvirt = NULL; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 467 | u8 subctxt, mapio = 0, vmf = 0, type; |
| 468 | ssize_t memlen = 0; |
| 469 | int ret = 0; |
| 470 | u16 ctxt; |
| 471 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 472 | if (!is_valid_mmap(token) || !uctxt || |
| 473 | !(vma->vm_flags & VM_SHARED)) { |
| 474 | ret = -EINVAL; |
| 475 | goto done; |
| 476 | } |
| 477 | dd = uctxt->dd; |
| 478 | ctxt = HFI1_MMAP_TOKEN_GET(CTXT, token); |
| 479 | subctxt = HFI1_MMAP_TOKEN_GET(SUBCTXT, token); |
| 480 | type = HFI1_MMAP_TOKEN_GET(TYPE, token); |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 481 | if (ctxt != uctxt->ctxt || subctxt != fd->subctxt) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 482 | ret = -EINVAL; |
| 483 | goto done; |
| 484 | } |
| 485 | |
| 486 | flags = vma->vm_flags; |
| 487 | |
| 488 | switch (type) { |
| 489 | case PIO_BUFS: |
| 490 | case PIO_BUFS_SOP: |
| 491 | memaddr = ((dd->physaddr + TXE_PIO_SEND) + |
| 492 | /* chip pio base */ |
Amitoj Kaur Chawla | d32cf44 | 2015-10-16 22:09:08 +0530 | [diff] [blame] | 493 | (uctxt->sc->hw_context * BIT(16))) + |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 494 | /* 64K PIO space / ctxt */ |
| 495 | (type == PIO_BUFS_SOP ? |
| 496 | (TXE_PIO_SIZE / 2) : 0); /* sop? */ |
| 497 | /* |
| 498 | * Map only the amount allocated to the context, not the |
| 499 | * entire available context's PIO space. |
| 500 | */ |
Amitoj Kaur Chawla | 437b29d | 2016-03-04 22:45:00 +0530 | [diff] [blame] | 501 | memlen = PAGE_ALIGN(uctxt->sc->credits * PIO_BLOCK_SIZE); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 502 | flags &= ~VM_MAYREAD; |
| 503 | flags |= VM_DONTCOPY | VM_DONTEXPAND; |
| 504 | vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); |
| 505 | mapio = 1; |
| 506 | break; |
| 507 | case PIO_CRED: |
| 508 | if (flags & VM_WRITE) { |
| 509 | ret = -EPERM; |
| 510 | goto done; |
| 511 | } |
| 512 | /* |
| 513 | * The credit return location for this context could be on the |
| 514 | * second or third page allocated for credit returns (if number |
| 515 | * of enabled contexts > 64 and 128 respectively). |
| 516 | */ |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 517 | memvirt = dd->cr_base[uctxt->numa_id].va; |
| 518 | memaddr = virt_to_phys(memvirt) + |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 519 | (((u64)uctxt->sc->hw_free - |
| 520 | (u64)dd->cr_base[uctxt->numa_id].va) & PAGE_MASK); |
| 521 | memlen = PAGE_SIZE; |
| 522 | flags &= ~VM_MAYWRITE; |
| 523 | flags |= VM_DONTCOPY | VM_DONTEXPAND; |
| 524 | /* |
| 525 | * The driver has already allocated memory for credit |
| 526 | * returns and programmed it into the chip. Has that |
| 527 | * memory been flagged as non-cached? |
| 528 | */ |
| 529 | /* vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); */ |
| 530 | mapio = 1; |
| 531 | break; |
| 532 | case RCV_HDRQ: |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 533 | memlen = uctxt->rcvhdrq_size; |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 534 | memvirt = uctxt->rcvhdrq; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 535 | break; |
| 536 | case RCV_EGRBUF: { |
| 537 | unsigned long addr; |
| 538 | int i; |
| 539 | /* |
| 540 | * The RcvEgr buffer need to be handled differently |
| 541 | * as multiple non-contiguous pages need to be mapped |
| 542 | * into the user process. |
| 543 | */ |
| 544 | memlen = uctxt->egrbufs.size; |
| 545 | if ((vma->vm_end - vma->vm_start) != memlen) { |
| 546 | dd_dev_err(dd, "Eager buffer map size invalid (%lu != %lu)\n", |
| 547 | (vma->vm_end - vma->vm_start), memlen); |
| 548 | ret = -EINVAL; |
| 549 | goto done; |
| 550 | } |
| 551 | if (vma->vm_flags & VM_WRITE) { |
| 552 | ret = -EPERM; |
| 553 | goto done; |
| 554 | } |
| 555 | vma->vm_flags &= ~VM_MAYWRITE; |
| 556 | addr = vma->vm_start; |
| 557 | for (i = 0 ; i < uctxt->egrbufs.numbufs; i++) { |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 558 | memlen = uctxt->egrbufs.buffers[i].len; |
| 559 | memvirt = uctxt->egrbufs.buffers[i].addr; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 560 | ret = remap_pfn_range( |
| 561 | vma, addr, |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 562 | /* |
| 563 | * virt_to_pfn() does the same, but |
| 564 | * it's not available on x86_64 |
| 565 | * when CONFIG_MMU is enabled. |
| 566 | */ |
| 567 | PFN_DOWN(__pa(memvirt)), |
| 568 | memlen, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 569 | vma->vm_page_prot); |
| 570 | if (ret < 0) |
| 571 | goto done; |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 572 | addr += memlen; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 573 | } |
| 574 | ret = 0; |
| 575 | goto done; |
| 576 | } |
| 577 | case UREGS: |
| 578 | /* |
| 579 | * Map only the page that contains this context's user |
| 580 | * registers. |
| 581 | */ |
| 582 | memaddr = (unsigned long) |
| 583 | (dd->physaddr + RXE_PER_CONTEXT_USER) |
| 584 | + (uctxt->ctxt * RXE_PER_CONTEXT_SIZE); |
| 585 | /* |
| 586 | * TidFlow table is on the same page as the rest of the |
| 587 | * user registers. |
| 588 | */ |
| 589 | memlen = PAGE_SIZE; |
| 590 | flags |= VM_DONTCOPY | VM_DONTEXPAND; |
| 591 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 592 | mapio = 1; |
| 593 | break; |
| 594 | case EVENTS: |
| 595 | /* |
| 596 | * Use the page where this context's flags are. User level |
| 597 | * knows where it's own bitmap is within the page. |
| 598 | */ |
Mitko Haralanov | 3c6c065 | 2015-10-26 10:28:39 -0400 | [diff] [blame] | 599 | memaddr = (unsigned long)(dd->events + |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 600 | ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) * |
| 601 | HFI1_MAX_SHARED_CTXTS)) & PAGE_MASK; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 602 | memlen = PAGE_SIZE; |
| 603 | /* |
| 604 | * v3.7 removes VM_RESERVED but the effect is kept by |
| 605 | * using VM_IO. |
| 606 | */ |
| 607 | flags |= VM_IO | VM_DONTEXPAND; |
| 608 | vmf = 1; |
| 609 | break; |
| 610 | case STATUS: |
Ira Weiny | 1222026 | 2017-04-09 10:17:24 -0700 | [diff] [blame] | 611 | if (flags & (unsigned long)(VM_WRITE | VM_EXEC)) { |
| 612 | ret = -EPERM; |
| 613 | goto done; |
| 614 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 615 | memaddr = kvirt_to_phys((void *)dd->status); |
| 616 | memlen = PAGE_SIZE; |
| 617 | flags |= VM_IO | VM_DONTEXPAND; |
| 618 | break; |
| 619 | case RTAIL: |
| 620 | if (!HFI1_CAP_IS_USET(DMA_RTAIL)) { |
| 621 | /* |
| 622 | * If the memory allocation failed, the context alloc |
| 623 | * also would have failed, so we would never get here |
| 624 | */ |
| 625 | ret = -EINVAL; |
| 626 | goto done; |
| 627 | } |
| 628 | if (flags & VM_WRITE) { |
| 629 | ret = -EPERM; |
| 630 | goto done; |
| 631 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 632 | memlen = PAGE_SIZE; |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 633 | memvirt = (void *)uctxt->rcvhdrtail_kvaddr; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 634 | flags &= ~VM_MAYWRITE; |
| 635 | break; |
| 636 | case SUBCTXT_UREGS: |
| 637 | memaddr = (u64)uctxt->subctxt_uregbase; |
| 638 | memlen = PAGE_SIZE; |
| 639 | flags |= VM_IO | VM_DONTEXPAND; |
| 640 | vmf = 1; |
| 641 | break; |
| 642 | case SUBCTXT_RCV_HDRQ: |
| 643 | memaddr = (u64)uctxt->subctxt_rcvhdr_base; |
| 644 | memlen = uctxt->rcvhdrq_size * uctxt->subctxt_cnt; |
| 645 | flags |= VM_IO | VM_DONTEXPAND; |
| 646 | vmf = 1; |
| 647 | break; |
| 648 | case SUBCTXT_EGRBUF: |
| 649 | memaddr = (u64)uctxt->subctxt_rcvegrbuf; |
| 650 | memlen = uctxt->egrbufs.size * uctxt->subctxt_cnt; |
| 651 | flags |= VM_IO | VM_DONTEXPAND; |
| 652 | flags &= ~VM_MAYWRITE; |
| 653 | vmf = 1; |
| 654 | break; |
| 655 | case SDMA_COMP: { |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 656 | struct hfi1_user_sdma_comp_q *cq = fd->cq; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 657 | |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 658 | if (!cq) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 659 | ret = -EFAULT; |
| 660 | goto done; |
| 661 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 662 | memaddr = (u64)cq->comps; |
Amitoj Kaur Chawla | 437b29d | 2016-03-04 22:45:00 +0530 | [diff] [blame] | 663 | memlen = PAGE_ALIGN(sizeof(*cq->comps) * cq->nentries); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 664 | flags |= VM_IO | VM_DONTEXPAND; |
| 665 | vmf = 1; |
| 666 | break; |
| 667 | } |
| 668 | default: |
| 669 | ret = -EINVAL; |
| 670 | break; |
| 671 | } |
| 672 | |
| 673 | if ((vma->vm_end - vma->vm_start) != memlen) { |
| 674 | hfi1_cdbg(PROC, "%u:%u Memory size mismatch %lu:%lu", |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 675 | uctxt->ctxt, fd->subctxt, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 676 | (vma->vm_end - vma->vm_start), memlen); |
| 677 | ret = -EINVAL; |
| 678 | goto done; |
| 679 | } |
| 680 | |
| 681 | vma->vm_flags = flags; |
Sebastian Sanchez | 6c63e42 | 2015-11-06 20:06:56 -0500 | [diff] [blame] | 682 | hfi1_cdbg(PROC, |
| 683 | "%u:%u type:%u io/vf:%d/%d, addr:0x%llx, len:%lu(%lu), flags:0x%lx\n", |
| 684 | ctxt, subctxt, type, mapio, vmf, memaddr, memlen, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 685 | vma->vm_end - vma->vm_start, vma->vm_flags); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 686 | if (vmf) { |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 687 | vma->vm_pgoff = PFN_DOWN(memaddr); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 688 | vma->vm_ops = &vm_ops; |
| 689 | ret = 0; |
| 690 | } else if (mapio) { |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 691 | ret = io_remap_pfn_range(vma, vma->vm_start, |
| 692 | PFN_DOWN(memaddr), |
| 693 | memlen, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 694 | vma->vm_page_prot); |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 695 | } else if (memvirt) { |
| 696 | ret = remap_pfn_range(vma, vma->vm_start, |
| 697 | PFN_DOWN(__pa(memvirt)), |
| 698 | memlen, |
| 699 | vma->vm_page_prot); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 700 | } else { |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 701 | ret = remap_pfn_range(vma, vma->vm_start, |
| 702 | PFN_DOWN(memaddr), |
| 703 | memlen, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 704 | vma->vm_page_prot); |
| 705 | } |
| 706 | done: |
| 707 | return ret; |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * Local (non-chip) user memory is not mapped right away but as it is |
| 712 | * accessed by the user-level code. |
| 713 | */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 714 | static int vma_fault(struct vm_fault *vmf) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 715 | { |
| 716 | struct page *page; |
| 717 | |
| 718 | page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT)); |
| 719 | if (!page) |
| 720 | return VM_FAULT_SIGBUS; |
| 721 | |
| 722 | get_page(page); |
| 723 | vmf->page = page; |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static unsigned int hfi1_poll(struct file *fp, struct poll_table_struct *pt) |
| 729 | { |
| 730 | struct hfi1_ctxtdata *uctxt; |
| 731 | unsigned pollflag; |
| 732 | |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 733 | uctxt = ((struct hfi1_filedata *)fp->private_data)->uctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 734 | if (!uctxt) |
| 735 | pollflag = POLLERR; |
| 736 | else if (uctxt->poll_type == HFI1_POLL_TYPE_URGENT) |
| 737 | pollflag = poll_urgent(fp, pt); |
| 738 | else if (uctxt->poll_type == HFI1_POLL_TYPE_ANYRCV) |
| 739 | pollflag = poll_next(fp, pt); |
| 740 | else /* invalid */ |
| 741 | pollflag = POLLERR; |
| 742 | |
| 743 | return pollflag; |
| 744 | } |
| 745 | |
| 746 | static int hfi1_file_close(struct inode *inode, struct file *fp) |
| 747 | { |
| 748 | struct hfi1_filedata *fdata = fp->private_data; |
| 749 | struct hfi1_ctxtdata *uctxt = fdata->uctxt; |
Dennis Dalessandro | e11ffbd | 2016-05-19 05:26:44 -0700 | [diff] [blame] | 750 | struct hfi1_devdata *dd = container_of(inode->i_cdev, |
| 751 | struct hfi1_devdata, |
| 752 | user_cdev); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 753 | unsigned long flags, *ev; |
| 754 | |
| 755 | fp->private_data = NULL; |
| 756 | |
| 757 | if (!uctxt) |
| 758 | goto done; |
| 759 | |
| 760 | hfi1_cdbg(PROC, "freeing ctxt %u:%u", uctxt->ctxt, fdata->subctxt); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 761 | mutex_lock(&hfi1_mutex); |
| 762 | |
| 763 | flush_wc(); |
| 764 | /* drain user sdma queue */ |
Mitko Haralanov | 483119a | 2015-12-08 17:10:10 -0500 | [diff] [blame] | 765 | hfi1_user_sdma_free_queues(fdata); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 766 | |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 767 | /* release the cpu */ |
Sebastian Sanchez | b094a36 | 2016-07-25 07:54:57 -0700 | [diff] [blame] | 768 | hfi1_put_proc_affinity(fdata->rec_cpu_num); |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 769 | |
Michael J. Ruhl | 224d71f | 2017-05-04 05:14:34 -0700 | [diff] [blame] | 770 | /* clean up rcv side */ |
| 771 | hfi1_user_exp_rcv_free(fdata); |
| 772 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 773 | /* |
| 774 | * Clear any left over, unhandled events so the next process that |
| 775 | * gets this context doesn't get confused. |
| 776 | */ |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 777 | ev = dd->events + ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) * |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 778 | HFI1_MAX_SHARED_CTXTS) + fdata->subctxt; |
| 779 | *ev = 0; |
| 780 | |
| 781 | if (--uctxt->cnt) { |
| 782 | uctxt->active_slaves &= ~(1 << fdata->subctxt); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 783 | mutex_unlock(&hfi1_mutex); |
| 784 | goto done; |
| 785 | } |
| 786 | |
| 787 | spin_lock_irqsave(&dd->uctxt_lock, flags); |
| 788 | /* |
| 789 | * Disable receive context and interrupt available, reset all |
| 790 | * RcvCtxtCtrl bits to default values. |
| 791 | */ |
| 792 | hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS | |
| 793 | HFI1_RCVCTRL_TIDFLOW_DIS | |
| 794 | HFI1_RCVCTRL_INTRAVAIL_DIS | |
Mitko Haralanov | 566c157 | 2016-02-03 14:32:49 -0800 | [diff] [blame] | 795 | HFI1_RCVCTRL_TAILUPD_DIS | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 796 | HFI1_RCVCTRL_ONE_PKT_EGR_DIS | |
| 797 | HFI1_RCVCTRL_NO_RHQ_DROP_DIS | |
| 798 | HFI1_RCVCTRL_NO_EGR_DROP_DIS, uctxt->ctxt); |
| 799 | /* Clear the context's J_KEY */ |
| 800 | hfi1_clear_ctxt_jkey(dd, uctxt->ctxt); |
| 801 | /* |
| 802 | * Reset context integrity checks to default. |
| 803 | * (writes to CSRs probably belong in chip.c) |
| 804 | */ |
| 805 | write_kctxt_csr(dd, uctxt->sc->hw_context, SEND_CTXT_CHECK_ENABLE, |
| 806 | hfi1_pkt_default_send_ctxt_mask(dd, uctxt->sc->type)); |
| 807 | sc_disable(uctxt->sc); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 808 | spin_unlock_irqrestore(&dd->uctxt_lock, flags); |
| 809 | |
| 810 | dd->rcd[uctxt->ctxt] = NULL; |
Mitko Haralanov | 9415844 | 2016-04-20 06:05:36 -0700 | [diff] [blame] | 811 | |
Michael J. Ruhl | 224d71f | 2017-05-04 05:14:34 -0700 | [diff] [blame] | 812 | hfi1_user_exp_rcv_grp_free(uctxt); |
Michael J. Ruhl | 637a9a7 | 2017-05-04 05:15:03 -0700 | [diff] [blame^] | 813 | hfi1_clear_ctxt_pkey(dd, uctxt); |
Mitko Haralanov | 9415844 | 2016-04-20 06:05:36 -0700 | [diff] [blame] | 814 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 815 | uctxt->rcvwait_to = 0; |
| 816 | uctxt->piowait_to = 0; |
| 817 | uctxt->rcvnowait = 0; |
| 818 | uctxt->pionowait = 0; |
| 819 | uctxt->event_flags = 0; |
| 820 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 821 | hfi1_stats.sps_ctxts--; |
Ashutosh Dixit | affa48d | 2016-02-03 14:33:06 -0800 | [diff] [blame] | 822 | if (++dd->freectxts == dd->num_user_contexts) |
| 823 | aspm_enable_all(dd); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 824 | mutex_unlock(&hfi1_mutex); |
| 825 | hfi1_free_ctxtdata(dd, uctxt); |
| 826 | done: |
Ira Weiny | e0cf75d | 2016-08-16 13:27:03 -0700 | [diff] [blame] | 827 | mmdrop(fdata->mm); |
Dennis Dalessandro | e11ffbd | 2016-05-19 05:26:44 -0700 | [diff] [blame] | 828 | kobject_put(&dd->kobj); |
Tadeusz Struk | acd7c8f | 2016-10-25 08:57:55 -0700 | [diff] [blame] | 829 | |
| 830 | if (atomic_dec_and_test(&dd->user_refcount)) |
| 831 | complete(&dd->user_comp); |
| 832 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 833 | kfree(fdata); |
| 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | /* |
| 838 | * Convert kernel *virtual* addresses to physical addresses. |
| 839 | * This is used to vmalloc'ed addresses. |
| 840 | */ |
| 841 | static u64 kvirt_to_phys(void *addr) |
| 842 | { |
| 843 | struct page *page; |
| 844 | u64 paddr = 0; |
| 845 | |
| 846 | page = vmalloc_to_page(addr); |
| 847 | if (page) |
| 848 | paddr = page_to_pfn(page) << PAGE_SHIFT; |
| 849 | |
| 850 | return paddr; |
| 851 | } |
| 852 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 853 | static int assign_ctxt(struct hfi1_filedata *fd, struct hfi1_user_info *uinfo) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 854 | { |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 855 | int ret = 0; |
Dennis Dalessandro | 0eb6265 | 2016-05-19 05:25:50 -0700 | [diff] [blame] | 856 | unsigned int swmajor, swminor; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 857 | |
| 858 | swmajor = uinfo->userversion >> 16; |
| 859 | if (swmajor != HFI1_USER_SWMAJOR) { |
| 860 | ret = -ENODEV; |
| 861 | goto done; |
| 862 | } |
| 863 | |
| 864 | swminor = uinfo->userversion & 0xffff; |
| 865 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 866 | mutex_lock(&hfi1_mutex); |
| 867 | /* First, lets check if we need to setup a shared context? */ |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 868 | if (uinfo->subctxt_cnt) { |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 869 | ret = find_shared_ctxt(fd, uinfo); |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 870 | if (ret < 0) |
| 871 | goto done_unlock; |
Sebastian Sanchez | b094a36 | 2016-07-25 07:54:57 -0700 | [diff] [blame] | 872 | if (ret) { |
| 873 | fd->rec_cpu_num = |
| 874 | hfi1_get_proc_affinity(fd->uctxt->numa_id); |
| 875 | } |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 876 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 877 | |
| 878 | /* |
| 879 | * We execute the following block if we couldn't find a |
| 880 | * shared context or if context sharing is not required. |
| 881 | */ |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 882 | if (!ret) |
| 883 | ret = allocate_ctxt(fd, fd->dd, uinfo); |
| 884 | |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 885 | done_unlock: |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 886 | mutex_unlock(&hfi1_mutex); |
| 887 | done: |
| 888 | return ret; |
| 889 | } |
| 890 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 891 | static int find_shared_ctxt(struct hfi1_filedata *fd, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 892 | const struct hfi1_user_info *uinfo) |
| 893 | { |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 894 | int i; |
| 895 | struct hfi1_devdata *dd = fd->dd; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 896 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 897 | for (i = dd->first_dyn_alloc_ctxt; i < dd->num_rcv_contexts; i++) { |
| 898 | struct hfi1_ctxtdata *uctxt = dd->rcd[i]; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 899 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 900 | /* Skip ctxts which are not yet open */ |
| 901 | if (!uctxt || !uctxt->cnt) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 902 | continue; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 903 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 904 | /* Skip dynamically allocted kernel contexts */ |
| 905 | if (uctxt->sc && (uctxt->sc->type == SC_KERNEL)) |
| 906 | continue; |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 907 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 908 | /* Skip ctxt if it doesn't match the requested one */ |
| 909 | if (memcmp(uctxt->uuid, uinfo->uuid, |
| 910 | sizeof(uctxt->uuid)) || |
| 911 | uctxt->jkey != generate_jkey(current_uid()) || |
| 912 | uctxt->subctxt_id != uinfo->subctxt_id || |
| 913 | uctxt->subctxt_cnt != uinfo->subctxt_cnt) |
| 914 | continue; |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 915 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 916 | /* Verify the sharing process matches the master */ |
| 917 | if (uctxt->userversion != uinfo->userversion || |
| 918 | uctxt->cnt >= uctxt->subctxt_cnt) { |
| 919 | return -EINVAL; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 920 | } |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 921 | fd->uctxt = uctxt; |
| 922 | fd->subctxt = uctxt->cnt++; |
| 923 | uctxt->active_slaves |= 1 << fd->subctxt; |
| 924 | return 1; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 925 | } |
| 926 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 927 | return 0; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 928 | } |
| 929 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 930 | static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 931 | struct hfi1_user_info *uinfo) |
| 932 | { |
| 933 | struct hfi1_ctxtdata *uctxt; |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 934 | unsigned int ctxt; |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 935 | int ret, numa; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 936 | |
| 937 | if (dd->flags & HFI1_FROZEN) { |
| 938 | /* |
| 939 | * Pick an error that is unique from all other errors |
| 940 | * that are returned so the user process knows that |
| 941 | * it tried to allocate while the SPC was frozen. It |
| 942 | * it should be able to retry with success in a short |
| 943 | * while. |
| 944 | */ |
| 945 | return -EIO; |
| 946 | } |
| 947 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 948 | /* |
| 949 | * This check is sort of redundant to the next EBUSY error. It would |
| 950 | * also indicate an inconsistancy in the driver if this value was |
| 951 | * zero, but there were still contexts available. |
| 952 | */ |
| 953 | if (!dd->freectxts) |
| 954 | return -EBUSY; |
| 955 | |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 956 | for (ctxt = dd->first_dyn_alloc_ctxt; |
| 957 | ctxt < dd->num_rcv_contexts; ctxt++) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 958 | if (!dd->rcd[ctxt]) |
| 959 | break; |
| 960 | |
| 961 | if (ctxt == dd->num_rcv_contexts) |
| 962 | return -EBUSY; |
| 963 | |
Sebastian Sanchez | b094a36 | 2016-07-25 07:54:57 -0700 | [diff] [blame] | 964 | /* |
| 965 | * If we don't have a NUMA node requested, preference is towards |
| 966 | * device NUMA node. |
| 967 | */ |
| 968 | fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node); |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 969 | if (fd->rec_cpu_num != -1) |
| 970 | numa = cpu_to_node(fd->rec_cpu_num); |
| 971 | else |
| 972 | numa = numa_node_id(); |
| 973 | uctxt = hfi1_create_ctxtdata(dd->pport, ctxt, numa); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 974 | if (!uctxt) { |
| 975 | dd_dev_err(dd, |
| 976 | "Unable to allocate ctxtdata memory, failing open\n"); |
| 977 | return -ENOMEM; |
| 978 | } |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 979 | hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)", |
| 980 | uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num, |
| 981 | uctxt->numa_id); |
| 982 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 983 | /* |
| 984 | * Allocate and enable a PIO send context. |
| 985 | */ |
| 986 | uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize, |
Mitko Haralanov | cc57236 | 2016-02-03 14:33:49 -0800 | [diff] [blame] | 987 | uctxt->dd->node); |
Jakub Pawlak | 3a6982d | 2016-09-25 07:42:23 -0700 | [diff] [blame] | 988 | if (!uctxt->sc) { |
| 989 | ret = -ENOMEM; |
| 990 | goto ctxdata_free; |
| 991 | } |
Sebastian Sanchez | 6c63e42 | 2015-11-06 20:06:56 -0500 | [diff] [blame] | 992 | hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index, |
| 993 | uctxt->sc->hw_context); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 994 | ret = sc_enable(uctxt->sc); |
| 995 | if (ret) |
Jakub Pawlak | 3a6982d | 2016-09-25 07:42:23 -0700 | [diff] [blame] | 996 | goto ctxdata_free; |
| 997 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 998 | /* |
| 999 | * Setup shared context resources if the user-level has requested |
| 1000 | * shared contexts and this is the 'master' process. |
| 1001 | * This has to be done here so the rest of the sub-contexts find the |
| 1002 | * proper master. |
| 1003 | */ |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1004 | if (uinfo->subctxt_cnt && !fd->subctxt) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1005 | ret = init_subctxts(uctxt, uinfo); |
| 1006 | /* |
| 1007 | * On error, we don't need to disable and de-allocate the |
| 1008 | * send context because it will be done during file close |
| 1009 | */ |
| 1010 | if (ret) |
Jakub Pawlak | 3a6982d | 2016-09-25 07:42:23 -0700 | [diff] [blame] | 1011 | goto ctxdata_free; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1012 | } |
| 1013 | uctxt->userversion = uinfo->userversion; |
Dean Luick | bdf7752 | 2016-07-28 15:21:13 -0400 | [diff] [blame] | 1014 | uctxt->flags = hfi1_cap_mask; /* save current flag state */ |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1015 | init_waitqueue_head(&uctxt->wait); |
| 1016 | strlcpy(uctxt->comm, current->comm, sizeof(uctxt->comm)); |
| 1017 | memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid)); |
| 1018 | uctxt->jkey = generate_jkey(current_uid()); |
| 1019 | INIT_LIST_HEAD(&uctxt->sdma_queues); |
| 1020 | spin_lock_init(&uctxt->sdma_qlock); |
| 1021 | hfi1_stats.sps_ctxts++; |
Ashutosh Dixit | affa48d | 2016-02-03 14:33:06 -0800 | [diff] [blame] | 1022 | /* |
| 1023 | * Disable ASPM when there are open user/PSM contexts to avoid |
| 1024 | * issues with ASPM L1 exit latency |
| 1025 | */ |
| 1026 | if (dd->freectxts-- == dd->num_user_contexts) |
| 1027 | aspm_disable_all(dd); |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1028 | fd->uctxt = uctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1029 | |
| 1030 | return 0; |
Jakub Pawlak | 3a6982d | 2016-09-25 07:42:23 -0700 | [diff] [blame] | 1031 | |
| 1032 | ctxdata_free: |
| 1033 | dd->rcd[ctxt] = NULL; |
| 1034 | hfi1_free_ctxtdata(dd, uctxt); |
| 1035 | return ret; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | static int init_subctxts(struct hfi1_ctxtdata *uctxt, |
| 1039 | const struct hfi1_user_info *uinfo) |
| 1040 | { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1041 | unsigned num_subctxts; |
| 1042 | |
| 1043 | num_subctxts = uinfo->subctxt_cnt; |
Mitko Haralanov | acac10f | 2016-02-05 11:57:50 -0500 | [diff] [blame] | 1044 | if (num_subctxts > HFI1_MAX_SHARED_CTXTS) |
| 1045 | return -EINVAL; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1046 | |
| 1047 | uctxt->subctxt_cnt = uinfo->subctxt_cnt; |
| 1048 | uctxt->subctxt_id = uinfo->subctxt_id; |
| 1049 | uctxt->active_slaves = 1; |
| 1050 | uctxt->redirect_seq_cnt = 1; |
| 1051 | set_bit(HFI1_CTXT_MASTER_UNINIT, &uctxt->event_flags); |
Mitko Haralanov | acac10f | 2016-02-05 11:57:50 -0500 | [diff] [blame] | 1052 | |
| 1053 | return 0; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | static int setup_subctxt(struct hfi1_ctxtdata *uctxt) |
| 1057 | { |
| 1058 | int ret = 0; |
| 1059 | unsigned num_subctxts = uctxt->subctxt_cnt; |
| 1060 | |
| 1061 | uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE); |
| 1062 | if (!uctxt->subctxt_uregbase) { |
| 1063 | ret = -ENOMEM; |
| 1064 | goto bail; |
| 1065 | } |
| 1066 | /* We can take the size of the RcvHdr Queue from the master */ |
| 1067 | uctxt->subctxt_rcvhdr_base = vmalloc_user(uctxt->rcvhdrq_size * |
| 1068 | num_subctxts); |
| 1069 | if (!uctxt->subctxt_rcvhdr_base) { |
| 1070 | ret = -ENOMEM; |
| 1071 | goto bail_ureg; |
| 1072 | } |
| 1073 | |
| 1074 | uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size * |
| 1075 | num_subctxts); |
| 1076 | if (!uctxt->subctxt_rcvegrbuf) { |
| 1077 | ret = -ENOMEM; |
| 1078 | goto bail_rhdr; |
| 1079 | } |
| 1080 | goto bail; |
| 1081 | bail_rhdr: |
| 1082 | vfree(uctxt->subctxt_rcvhdr_base); |
| 1083 | bail_ureg: |
| 1084 | vfree(uctxt->subctxt_uregbase); |
| 1085 | uctxt->subctxt_uregbase = NULL; |
| 1086 | bail: |
| 1087 | return ret; |
| 1088 | } |
| 1089 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 1090 | static int user_init(struct hfi1_filedata *fd) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1091 | { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1092 | unsigned int rcvctrl_ops = 0; |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1093 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1094 | |
| 1095 | /* make sure that the context has already been setup */ |
Mitko Haralanov | 9415844 | 2016-04-20 06:05:36 -0700 | [diff] [blame] | 1096 | if (!test_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags)) |
| 1097 | return -EFAULT; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1098 | |
| 1099 | /* initialize poll variables... */ |
| 1100 | uctxt->urgent = 0; |
| 1101 | uctxt->urgent_poll = 0; |
| 1102 | |
| 1103 | /* |
| 1104 | * Now enable the ctxt for receive. |
| 1105 | * For chips that are set to DMA the tail register to memory |
| 1106 | * when they change (and when the update bit transitions from |
| 1107 | * 0 to 1. So for those chips, we turn it off and then back on. |
| 1108 | * This will (very briefly) affect any other open ctxts, but the |
| 1109 | * duration is very short, and therefore isn't an issue. We |
| 1110 | * explicitly set the in-memory tail copy to 0 beforehand, so we |
| 1111 | * don't have to wait to be sure the DMA update has happened |
| 1112 | * (chip resets head/tail to 0 on transition to enable). |
| 1113 | */ |
| 1114 | if (uctxt->rcvhdrtail_kvaddr) |
| 1115 | clear_rcvhdrtail(uctxt); |
| 1116 | |
| 1117 | /* Setup J_KEY before enabling the context */ |
| 1118 | hfi1_set_ctxt_jkey(uctxt->dd, uctxt->ctxt, uctxt->jkey); |
| 1119 | |
| 1120 | rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB; |
Dean Luick | bdf7752 | 2016-07-28 15:21:13 -0400 | [diff] [blame] | 1121 | if (HFI1_CAP_UGET_MASK(uctxt->flags, HDRSUPP)) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1122 | rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB; |
| 1123 | /* |
| 1124 | * Ignore the bit in the flags for now until proper |
| 1125 | * support for multiple packet per rcv array entry is |
| 1126 | * added. |
| 1127 | */ |
Dean Luick | bdf7752 | 2016-07-28 15:21:13 -0400 | [diff] [blame] | 1128 | if (!HFI1_CAP_UGET_MASK(uctxt->flags, MULTI_PKT_EGR)) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1129 | rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB; |
Dean Luick | bdf7752 | 2016-07-28 15:21:13 -0400 | [diff] [blame] | 1130 | if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_EGR_FULL)) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1131 | rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB; |
Dean Luick | bdf7752 | 2016-07-28 15:21:13 -0400 | [diff] [blame] | 1132 | if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_RHQ_FULL)) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1133 | rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB; |
Mitko Haralanov | 566c157 | 2016-02-03 14:32:49 -0800 | [diff] [blame] | 1134 | /* |
| 1135 | * The RcvCtxtCtrl.TailUpd bit has to be explicitly written. |
| 1136 | * We can't rely on the correct value to be set from prior |
| 1137 | * uses of the chip or ctxt. Therefore, add the rcvctrl op |
| 1138 | * for both cases. |
| 1139 | */ |
Dean Luick | bdf7752 | 2016-07-28 15:21:13 -0400 | [diff] [blame] | 1140 | if (HFI1_CAP_UGET_MASK(uctxt->flags, DMA_RTAIL)) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1141 | rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB; |
Mitko Haralanov | 566c157 | 2016-02-03 14:32:49 -0800 | [diff] [blame] | 1142 | else |
| 1143 | rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1144 | hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt->ctxt); |
| 1145 | |
| 1146 | /* Notify any waiting slaves */ |
| 1147 | if (uctxt->subctxt_cnt) { |
| 1148 | clear_bit(HFI1_CTXT_MASTER_UNINIT, &uctxt->event_flags); |
| 1149 | wake_up(&uctxt->wait); |
| 1150 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1151 | |
Mitko Haralanov | 9415844 | 2016-04-20 06:05:36 -0700 | [diff] [blame] | 1152 | return 0; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1153 | } |
| 1154 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 1155 | static int get_ctxt_info(struct hfi1_filedata *fd, void __user *ubase, |
| 1156 | __u32 len) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1157 | { |
| 1158 | struct hfi1_ctxt_info cinfo; |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1159 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1160 | int ret = 0; |
| 1161 | |
Dan Carpenter | ebe6b2e | 2015-09-16 09:42:25 +0300 | [diff] [blame] | 1162 | memset(&cinfo, 0, sizeof(cinfo)); |
Dean Luick | bdf7752 | 2016-07-28 15:21:13 -0400 | [diff] [blame] | 1163 | cinfo.runtime_flags = (((uctxt->flags >> HFI1_CAP_MISC_SHIFT) & |
| 1164 | HFI1_CAP_MISC_MASK) << HFI1_CAP_USER_SHIFT) | |
| 1165 | HFI1_CAP_UGET_MASK(uctxt->flags, MASK) | |
| 1166 | HFI1_CAP_KGET_MASK(uctxt->flags, K2U); |
Dean Luick | 622c202 | 2016-07-28 15:21:21 -0400 | [diff] [blame] | 1167 | /* adjust flag if this fd is not able to cache */ |
| 1168 | if (!fd->handler) |
| 1169 | cinfo.runtime_flags |= HFI1_CAP_TID_UNMAP; /* no caching */ |
| 1170 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1171 | cinfo.num_active = hfi1_count_active_units(); |
| 1172 | cinfo.unit = uctxt->dd->unit; |
| 1173 | cinfo.ctxt = uctxt->ctxt; |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1174 | cinfo.subctxt = fd->subctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1175 | cinfo.rcvtids = roundup(uctxt->egrbufs.alloced, |
| 1176 | uctxt->dd->rcv_entries.group_size) + |
| 1177 | uctxt->expected_count; |
| 1178 | cinfo.credits = uctxt->sc->credits; |
| 1179 | cinfo.numa_node = uctxt->numa_id; |
| 1180 | cinfo.rec_cpu = fd->rec_cpu_num; |
| 1181 | cinfo.send_ctxt = uctxt->sc->hw_context; |
| 1182 | |
| 1183 | cinfo.egrtids = uctxt->egrbufs.alloced; |
| 1184 | cinfo.rcvhdrq_cnt = uctxt->rcvhdrq_cnt; |
| 1185 | cinfo.rcvhdrq_entsize = uctxt->rcvhdrqentsize << 2; |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1186 | cinfo.sdma_ring_size = fd->cq->nentries; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1187 | cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size; |
| 1188 | |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1189 | trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, cinfo); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1190 | if (copy_to_user(ubase, &cinfo, sizeof(cinfo))) |
| 1191 | ret = -EFAULT; |
Dean Luick | bdf7752 | 2016-07-28 15:21:13 -0400 | [diff] [blame] | 1192 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1193 | return ret; |
| 1194 | } |
| 1195 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 1196 | static int setup_ctxt(struct hfi1_filedata *fd) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1197 | { |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1198 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1199 | struct hfi1_devdata *dd = uctxt->dd; |
| 1200 | int ret = 0; |
| 1201 | |
| 1202 | /* |
Mitko Haralanov | 9415844 | 2016-04-20 06:05:36 -0700 | [diff] [blame] | 1203 | * Context should be set up only once, including allocation and |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1204 | * programming of eager buffers. This is done if context sharing |
| 1205 | * is not requested or by the master process. |
| 1206 | */ |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1207 | if (!uctxt->subctxt_cnt || !fd->subctxt) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1208 | ret = hfi1_init_ctxt(uctxt->sc); |
| 1209 | if (ret) |
| 1210 | goto done; |
| 1211 | |
| 1212 | /* Now allocate the RcvHdr queue and eager buffers. */ |
| 1213 | ret = hfi1_create_rcvhdrq(dd, uctxt); |
| 1214 | if (ret) |
| 1215 | goto done; |
| 1216 | ret = hfi1_setup_eagerbufs(uctxt); |
| 1217 | if (ret) |
| 1218 | goto done; |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1219 | if (uctxt->subctxt_cnt && !fd->subctxt) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1220 | ret = setup_subctxt(uctxt); |
| 1221 | if (ret) |
| 1222 | goto done; |
| 1223 | } |
Mitko Haralanov | 9415844 | 2016-04-20 06:05:36 -0700 | [diff] [blame] | 1224 | } else { |
| 1225 | ret = wait_event_interruptible(uctxt->wait, !test_bit( |
| 1226 | HFI1_CTXT_MASTER_UNINIT, |
| 1227 | &uctxt->event_flags)); |
| 1228 | if (ret) |
| 1229 | goto done; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1230 | } |
Mitko Haralanov | 9415844 | 2016-04-20 06:05:36 -0700 | [diff] [blame] | 1231 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 1232 | ret = hfi1_user_sdma_alloc_queues(uctxt, fd); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1233 | if (ret) |
| 1234 | goto done; |
Mitko Haralanov | 9415844 | 2016-04-20 06:05:36 -0700 | [diff] [blame] | 1235 | /* |
| 1236 | * Expected receive has to be setup for all processes (including |
| 1237 | * shared contexts). However, it has to be done after the master |
| 1238 | * context has been fully configured as it depends on the |
| 1239 | * eager/expected split of the RcvArray entries. |
| 1240 | * Setting it up here ensures that the subcontexts will be waiting |
| 1241 | * (due to the above wait_event_interruptible() until the master |
| 1242 | * is setup. |
| 1243 | */ |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 1244 | ret = hfi1_user_exp_rcv_init(fd); |
Mitko Haralanov | 9415844 | 2016-04-20 06:05:36 -0700 | [diff] [blame] | 1245 | if (ret) |
| 1246 | goto done; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1247 | |
| 1248 | set_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags); |
| 1249 | done: |
| 1250 | return ret; |
| 1251 | } |
| 1252 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 1253 | static int get_base_info(struct hfi1_filedata *fd, void __user *ubase, |
| 1254 | __u32 len) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1255 | { |
| 1256 | struct hfi1_base_info binfo; |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1257 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1258 | struct hfi1_devdata *dd = uctxt->dd; |
| 1259 | ssize_t sz; |
| 1260 | unsigned offset; |
| 1261 | int ret = 0; |
| 1262 | |
| 1263 | trace_hfi1_uctxtdata(uctxt->dd, uctxt); |
| 1264 | |
| 1265 | memset(&binfo, 0, sizeof(binfo)); |
| 1266 | binfo.hw_version = dd->revision; |
| 1267 | binfo.sw_version = HFI1_KERN_SWVERSION; |
| 1268 | binfo.bthqp = kdeth_qp; |
| 1269 | binfo.jkey = uctxt->jkey; |
| 1270 | /* |
| 1271 | * If more than 64 contexts are enabled the allocated credit |
| 1272 | * return will span two or three contiguous pages. Since we only |
| 1273 | * map the page containing the context's credit return address, |
| 1274 | * we need to calculate the offset in the proper page. |
| 1275 | */ |
| 1276 | offset = ((u64)uctxt->sc->hw_free - |
| 1277 | (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE; |
| 1278 | binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1279 | fd->subctxt, offset); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1280 | binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1281 | fd->subctxt, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1282 | uctxt->sc->base_addr); |
| 1283 | binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP, |
| 1284 | uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1285 | fd->subctxt, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1286 | uctxt->sc->base_addr); |
| 1287 | binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1288 | fd->subctxt, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1289 | uctxt->rcvhdrq); |
| 1290 | binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1291 | fd->subctxt, |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1292 | uctxt->egrbufs.rcvtids[0].dma); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1293 | binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1294 | fd->subctxt, 0); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1295 | /* |
| 1296 | * user regs are at |
| 1297 | * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE)) |
| 1298 | */ |
| 1299 | binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1300 | fd->subctxt, 0); |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 1301 | offset = offset_in_page((((uctxt->ctxt - dd->first_dyn_alloc_ctxt) * |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1302 | HFI1_MAX_SHARED_CTXTS) + fd->subctxt) * |
Geliang Tang | e260e40 | 2015-10-03 10:34:59 +0800 | [diff] [blame] | 1303 | sizeof(*dd->events)); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1304 | binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1305 | fd->subctxt, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1306 | offset); |
| 1307 | binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1308 | fd->subctxt, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1309 | dd->status); |
| 1310 | if (HFI1_CAP_IS_USET(DMA_RTAIL)) |
| 1311 | binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1312 | fd->subctxt, 0); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1313 | if (uctxt->subctxt_cnt) { |
| 1314 | binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS, |
| 1315 | uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1316 | fd->subctxt, 0); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1317 | binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ, |
| 1318 | uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1319 | fd->subctxt, 0); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1320 | binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF, |
| 1321 | uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1322 | fd->subctxt, 0); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1323 | } |
| 1324 | sz = (len < sizeof(binfo)) ? len : sizeof(binfo); |
| 1325 | if (copy_to_user(ubase, &binfo, sz)) |
| 1326 | ret = -EFAULT; |
| 1327 | return ret; |
| 1328 | } |
| 1329 | |
| 1330 | static unsigned int poll_urgent(struct file *fp, |
| 1331 | struct poll_table_struct *pt) |
| 1332 | { |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1333 | struct hfi1_filedata *fd = fp->private_data; |
| 1334 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1335 | struct hfi1_devdata *dd = uctxt->dd; |
| 1336 | unsigned pollflag; |
| 1337 | |
| 1338 | poll_wait(fp, &uctxt->wait, pt); |
| 1339 | |
| 1340 | spin_lock_irq(&dd->uctxt_lock); |
| 1341 | if (uctxt->urgent != uctxt->urgent_poll) { |
| 1342 | pollflag = POLLIN | POLLRDNORM; |
| 1343 | uctxt->urgent_poll = uctxt->urgent; |
| 1344 | } else { |
| 1345 | pollflag = 0; |
| 1346 | set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags); |
| 1347 | } |
| 1348 | spin_unlock_irq(&dd->uctxt_lock); |
| 1349 | |
| 1350 | return pollflag; |
| 1351 | } |
| 1352 | |
| 1353 | static unsigned int poll_next(struct file *fp, |
| 1354 | struct poll_table_struct *pt) |
| 1355 | { |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1356 | struct hfi1_filedata *fd = fp->private_data; |
| 1357 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1358 | struct hfi1_devdata *dd = uctxt->dd; |
| 1359 | unsigned pollflag; |
| 1360 | |
| 1361 | poll_wait(fp, &uctxt->wait, pt); |
| 1362 | |
| 1363 | spin_lock_irq(&dd->uctxt_lock); |
| 1364 | if (hdrqempty(uctxt)) { |
| 1365 | set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags); |
| 1366 | hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt->ctxt); |
| 1367 | pollflag = 0; |
Jubin John | e490974 | 2016-02-14 20:22:00 -0800 | [diff] [blame] | 1368 | } else { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1369 | pollflag = POLLIN | POLLRDNORM; |
Jubin John | e490974 | 2016-02-14 20:22:00 -0800 | [diff] [blame] | 1370 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1371 | spin_unlock_irq(&dd->uctxt_lock); |
| 1372 | |
| 1373 | return pollflag; |
| 1374 | } |
| 1375 | |
| 1376 | /* |
| 1377 | * Find all user contexts in use, and set the specified bit in their |
| 1378 | * event mask. |
| 1379 | * See also find_ctxt() for a similar use, that is specific to send buffers. |
| 1380 | */ |
| 1381 | int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit) |
| 1382 | { |
| 1383 | struct hfi1_ctxtdata *uctxt; |
| 1384 | struct hfi1_devdata *dd = ppd->dd; |
| 1385 | unsigned ctxt; |
| 1386 | int ret = 0; |
| 1387 | unsigned long flags; |
| 1388 | |
| 1389 | if (!dd->events) { |
| 1390 | ret = -EINVAL; |
| 1391 | goto done; |
| 1392 | } |
| 1393 | |
| 1394 | spin_lock_irqsave(&dd->uctxt_lock, flags); |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 1395 | for (ctxt = dd->first_dyn_alloc_ctxt; ctxt < dd->num_rcv_contexts; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1396 | ctxt++) { |
| 1397 | uctxt = dd->rcd[ctxt]; |
| 1398 | if (uctxt) { |
| 1399 | unsigned long *evs = dd->events + |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 1400 | (uctxt->ctxt - dd->first_dyn_alloc_ctxt) * |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1401 | HFI1_MAX_SHARED_CTXTS; |
| 1402 | int i; |
| 1403 | /* |
| 1404 | * subctxt_cnt is 0 if not shared, so do base |
| 1405 | * separately, first, then remaining subctxt, if any |
| 1406 | */ |
| 1407 | set_bit(evtbit, evs); |
| 1408 | for (i = 1; i < uctxt->subctxt_cnt; i++) |
| 1409 | set_bit(evtbit, evs + i); |
| 1410 | } |
| 1411 | } |
| 1412 | spin_unlock_irqrestore(&dd->uctxt_lock, flags); |
| 1413 | done: |
| 1414 | return ret; |
| 1415 | } |
| 1416 | |
| 1417 | /** |
| 1418 | * manage_rcvq - manage a context's receive queue |
| 1419 | * @uctxt: the context |
| 1420 | * @subctxt: the sub-context |
| 1421 | * @start_stop: action to carry out |
| 1422 | * |
| 1423 | * start_stop == 0 disables receive on the context, for use in queue |
| 1424 | * overflow conditions. start_stop==1 re-enables, to be used to |
| 1425 | * re-init the software copy of the head register |
| 1426 | */ |
| 1427 | static int manage_rcvq(struct hfi1_ctxtdata *uctxt, unsigned subctxt, |
| 1428 | int start_stop) |
| 1429 | { |
| 1430 | struct hfi1_devdata *dd = uctxt->dd; |
| 1431 | unsigned int rcvctrl_op; |
| 1432 | |
| 1433 | if (subctxt) |
| 1434 | goto bail; |
| 1435 | /* atomically clear receive enable ctxt. */ |
| 1436 | if (start_stop) { |
| 1437 | /* |
| 1438 | * On enable, force in-memory copy of the tail register to |
| 1439 | * 0, so that protocol code doesn't have to worry about |
| 1440 | * whether or not the chip has yet updated the in-memory |
| 1441 | * copy or not on return from the system call. The chip |
| 1442 | * always resets it's tail register back to 0 on a |
| 1443 | * transition from disabled to enabled. |
| 1444 | */ |
| 1445 | if (uctxt->rcvhdrtail_kvaddr) |
| 1446 | clear_rcvhdrtail(uctxt); |
| 1447 | rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB; |
Jubin John | e490974 | 2016-02-14 20:22:00 -0800 | [diff] [blame] | 1448 | } else { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1449 | rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS; |
Jubin John | e490974 | 2016-02-14 20:22:00 -0800 | [diff] [blame] | 1450 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1451 | hfi1_rcvctrl(dd, rcvctrl_op, uctxt->ctxt); |
| 1452 | /* always; new head should be equal to new tail; see above */ |
| 1453 | bail: |
| 1454 | return 0; |
| 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | * clear the event notifier events for this context. |
| 1459 | * User process then performs actions appropriate to bit having been |
| 1460 | * set, if desired, and checks again in future. |
| 1461 | */ |
| 1462 | static int user_event_ack(struct hfi1_ctxtdata *uctxt, int subctxt, |
| 1463 | unsigned long events) |
| 1464 | { |
| 1465 | int i; |
| 1466 | struct hfi1_devdata *dd = uctxt->dd; |
| 1467 | unsigned long *evs; |
| 1468 | |
| 1469 | if (!dd->events) |
| 1470 | return 0; |
| 1471 | |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 1472 | evs = dd->events + ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) * |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1473 | HFI1_MAX_SHARED_CTXTS) + subctxt; |
| 1474 | |
| 1475 | for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) { |
| 1476 | if (!test_bit(i, &events)) |
| 1477 | continue; |
| 1478 | clear_bit(i, evs); |
| 1479 | } |
| 1480 | return 0; |
| 1481 | } |
| 1482 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1483 | static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned subctxt, |
| 1484 | u16 pkey) |
| 1485 | { |
| 1486 | int ret = -ENOENT, i, intable = 0; |
| 1487 | struct hfi1_pportdata *ppd = uctxt->ppd; |
| 1488 | struct hfi1_devdata *dd = uctxt->dd; |
| 1489 | |
| 1490 | if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY) { |
| 1491 | ret = -EINVAL; |
| 1492 | goto done; |
| 1493 | } |
| 1494 | |
| 1495 | for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) |
| 1496 | if (pkey == ppd->pkeys[i]) { |
| 1497 | intable = 1; |
| 1498 | break; |
| 1499 | } |
| 1500 | |
| 1501 | if (intable) |
| 1502 | ret = hfi1_set_ctxt_pkey(dd, uctxt->ctxt, pkey); |
| 1503 | done: |
| 1504 | return ret; |
| 1505 | } |
| 1506 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1507 | static void user_remove(struct hfi1_devdata *dd) |
| 1508 | { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1509 | |
| 1510 | hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | static int user_add(struct hfi1_devdata *dd) |
| 1514 | { |
| 1515 | char name[10]; |
| 1516 | int ret; |
| 1517 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1518 | snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit); |
Dennis Dalessandro | 0eb6265 | 2016-05-19 05:25:50 -0700 | [diff] [blame] | 1519 | ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops, |
Ira Weiny | e116a64 | 2015-09-17 13:47:49 -0400 | [diff] [blame] | 1520 | &dd->user_cdev, &dd->user_device, |
Dennis Dalessandro | e11ffbd | 2016-05-19 05:26:44 -0700 | [diff] [blame] | 1521 | true, &dd->kobj); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1522 | if (ret) |
Dennis Dalessandro | 7312f29 | 2016-05-19 05:25:57 -0700 | [diff] [blame] | 1523 | user_remove(dd); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1524 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1525 | return ret; |
| 1526 | } |
| 1527 | |
| 1528 | /* |
| 1529 | * Create per-unit files in /dev |
| 1530 | */ |
| 1531 | int hfi1_device_create(struct hfi1_devdata *dd) |
| 1532 | { |
Dennis Dalessandro | 0f7b1f9 | 2016-05-19 05:26:10 -0700 | [diff] [blame] | 1533 | return user_add(dd); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | /* |
| 1537 | * Remove per-unit files in /dev |
| 1538 | * void, core kernel returns no errors for this stuff |
| 1539 | */ |
| 1540 | void hfi1_device_remove(struct hfi1_devdata *dd) |
| 1541 | { |
| 1542 | user_remove(dd); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1543 | } |