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