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