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); |
Michael J. Ruhl | 4249201 | 2017-07-24 07:45:43 -0700 | [diff] [blame] | 97 | static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt); |
Michael J. Ruhl | f4cd876 | 2017-05-04 05:14:39 -0700 | [diff] [blame] | 98 | static unsigned int poll_urgent(struct file *fp, struct poll_table_struct *pt); |
| 99 | 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] | 100 | static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt, |
Michael J. Ruhl | f4cd876 | 2017-05-04 05:14:39 -0700 | [diff] [blame] | 101 | unsigned long events); |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame] | 102 | static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, u16 subctxt, u16 pkey); |
| 103 | static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt, |
Michael J. Ruhl | f4cd876 | 2017-05-04 05:14:39 -0700 | [diff] [blame] | 104 | int start_stop); |
| 105 | static int vma_fault(struct vm_fault *vmf); |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 106 | static long hfi1_file_ioctl(struct file *fp, unsigned int cmd, |
| 107 | unsigned long arg); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 108 | |
| 109 | static const struct file_operations hfi1_file_ops = { |
| 110 | .owner = THIS_MODULE, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 111 | .write_iter = hfi1_write_iter, |
| 112 | .open = hfi1_file_open, |
| 113 | .release = hfi1_file_close, |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 114 | .unlocked_ioctl = hfi1_file_ioctl, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 115 | .poll = hfi1_poll, |
| 116 | .mmap = hfi1_file_mmap, |
| 117 | .llseek = noop_llseek, |
| 118 | }; |
| 119 | |
| 120 | static struct vm_operations_struct vm_ops = { |
| 121 | .fault = vma_fault, |
| 122 | }; |
| 123 | |
| 124 | /* |
| 125 | * Types of memories mapped into user processes' space |
| 126 | */ |
| 127 | enum mmap_types { |
| 128 | PIO_BUFS = 1, |
| 129 | PIO_BUFS_SOP, |
| 130 | PIO_CRED, |
| 131 | RCV_HDRQ, |
| 132 | RCV_EGRBUF, |
| 133 | UREGS, |
| 134 | EVENTS, |
| 135 | STATUS, |
| 136 | RTAIL, |
| 137 | SUBCTXT_UREGS, |
| 138 | SUBCTXT_RCV_HDRQ, |
| 139 | SUBCTXT_EGRBUF, |
| 140 | SDMA_COMP |
| 141 | }; |
| 142 | |
| 143 | /* |
| 144 | * Masks and offsets defining the mmap tokens |
| 145 | */ |
| 146 | #define HFI1_MMAP_OFFSET_MASK 0xfffULL |
| 147 | #define HFI1_MMAP_OFFSET_SHIFT 0 |
| 148 | #define HFI1_MMAP_SUBCTXT_MASK 0xfULL |
| 149 | #define HFI1_MMAP_SUBCTXT_SHIFT 12 |
| 150 | #define HFI1_MMAP_CTXT_MASK 0xffULL |
| 151 | #define HFI1_MMAP_CTXT_SHIFT 16 |
| 152 | #define HFI1_MMAP_TYPE_MASK 0xfULL |
| 153 | #define HFI1_MMAP_TYPE_SHIFT 24 |
| 154 | #define HFI1_MMAP_MAGIC_MASK 0xffffffffULL |
| 155 | #define HFI1_MMAP_MAGIC_SHIFT 32 |
| 156 | |
| 157 | #define HFI1_MMAP_MAGIC 0xdabbad00 |
| 158 | |
| 159 | #define HFI1_MMAP_TOKEN_SET(field, val) \ |
| 160 | (((val) & HFI1_MMAP_##field##_MASK) << HFI1_MMAP_##field##_SHIFT) |
| 161 | #define HFI1_MMAP_TOKEN_GET(field, token) \ |
| 162 | (((token) >> HFI1_MMAP_##field##_SHIFT) & HFI1_MMAP_##field##_MASK) |
| 163 | #define HFI1_MMAP_TOKEN(type, ctxt, subctxt, addr) \ |
| 164 | (HFI1_MMAP_TOKEN_SET(MAGIC, HFI1_MMAP_MAGIC) | \ |
| 165 | HFI1_MMAP_TOKEN_SET(TYPE, type) | \ |
| 166 | HFI1_MMAP_TOKEN_SET(CTXT, ctxt) | \ |
| 167 | HFI1_MMAP_TOKEN_SET(SUBCTXT, subctxt) | \ |
Geliang Tang | e260e40 | 2015-10-03 10:34:59 +0800 | [diff] [blame] | 168 | HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr)))) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 169 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 170 | #define dbg(fmt, ...) \ |
| 171 | pr_info(fmt, ##__VA_ARGS__) |
| 172 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 173 | static inline int is_valid_mmap(u64 token) |
| 174 | { |
| 175 | return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC); |
| 176 | } |
| 177 | |
| 178 | static int hfi1_file_open(struct inode *inode, struct file *fp) |
| 179 | { |
Ira Weiny | ea3a0ee | 2016-07-28 12:27:35 -0400 | [diff] [blame] | 180 | struct hfi1_filedata *fd; |
Dennis Dalessandro | e11ffbd | 2016-05-19 05:26:44 -0700 | [diff] [blame] | 181 | struct hfi1_devdata *dd = container_of(inode->i_cdev, |
| 182 | struct hfi1_devdata, |
| 183 | user_cdev); |
| 184 | |
Mike Marciniszyn | cb51c5d | 2017-07-24 07:45:31 -0700 | [diff] [blame] | 185 | if (!((dd->flags & HFI1_PRESENT) && dd->kregbase1)) |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 186 | return -EINVAL; |
| 187 | |
Tadeusz Struk | acd7c8f | 2016-10-25 08:57:55 -0700 | [diff] [blame] | 188 | if (!atomic_inc_not_zero(&dd->user_refcount)) |
| 189 | return -ENXIO; |
| 190 | |
Dennis Dalessandro | e11ffbd | 2016-05-19 05:26:44 -0700 | [diff] [blame] | 191 | /* Just take a ref now. Not all opens result in a context assign */ |
| 192 | kobject_get(&dd->kobj); |
| 193 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 194 | /* The real work is performed later in assign_ctxt() */ |
Ira Weiny | ea3a0ee | 2016-07-28 12:27:35 -0400 | [diff] [blame] | 195 | |
| 196 | fd = kzalloc(sizeof(*fd), GFP_KERNEL); |
| 197 | |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame] | 198 | if (fd) { |
| 199 | fd->rec_cpu_num = -1; /* no cpu affinity by default */ |
| 200 | fd->mm = current->mm; |
Vegard Nossum | f1f1007 | 2017-02-27 14:30:07 -0800 | [diff] [blame] | 201 | mmgrab(fd->mm); |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 202 | fd->dd = dd; |
Tadeusz Struk | acd7c8f | 2016-10-25 08:57:55 -0700 | [diff] [blame] | 203 | fp->private_data = fd; |
| 204 | } else { |
| 205 | fp->private_data = NULL; |
| 206 | |
| 207 | if (atomic_dec_and_test(&dd->user_refcount)) |
| 208 | complete(&dd->user_comp); |
| 209 | |
| 210 | return -ENOMEM; |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame] | 211 | } |
Ira Weiny | ea3a0ee | 2016-07-28 12:27:35 -0400 | [diff] [blame] | 212 | |
Tadeusz Struk | acd7c8f | 2016-10-25 08:57:55 -0700 | [diff] [blame] | 213 | return 0; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 216 | static long hfi1_file_ioctl(struct file *fp, unsigned int cmd, |
| 217 | unsigned long arg) |
| 218 | { |
| 219 | struct hfi1_filedata *fd = fp->private_data; |
| 220 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
| 221 | struct hfi1_user_info uinfo; |
| 222 | struct hfi1_tid_info tinfo; |
| 223 | int ret = 0; |
| 224 | unsigned long addr; |
| 225 | int uval = 0; |
| 226 | unsigned long ul_uval = 0; |
| 227 | u16 uval16 = 0; |
| 228 | |
Dennis Dalessandro | 8a1882e | 2016-05-19 05:26:37 -0700 | [diff] [blame] | 229 | hfi1_cdbg(IOCTL, "IOCTL recv: 0x%x", cmd); |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 230 | if (cmd != HFI1_IOCTL_ASSIGN_CTXT && |
| 231 | cmd != HFI1_IOCTL_GET_VERS && |
| 232 | !uctxt) |
| 233 | return -EINVAL; |
| 234 | |
| 235 | switch (cmd) { |
| 236 | case HFI1_IOCTL_ASSIGN_CTXT: |
Ira Weiny | ca2f30a | 2016-06-09 07:51:33 -0700 | [diff] [blame] | 237 | if (uctxt) |
| 238 | return -EINVAL; |
| 239 | |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 240 | if (copy_from_user(&uinfo, |
| 241 | (struct hfi1_user_info __user *)arg, |
| 242 | sizeof(uinfo))) |
| 243 | return -EFAULT; |
| 244 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 245 | ret = assign_ctxt(fd, &uinfo); |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 246 | break; |
| 247 | case HFI1_IOCTL_CTXT_INFO: |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 248 | ret = get_ctxt_info(fd, (void __user *)(unsigned long)arg, |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 249 | sizeof(struct hfi1_ctxt_info)); |
| 250 | break; |
| 251 | case HFI1_IOCTL_USER_INFO: |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 252 | ret = get_base_info(fd, (void __user *)(unsigned long)arg, |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 253 | sizeof(struct hfi1_base_info)); |
| 254 | break; |
| 255 | case HFI1_IOCTL_CREDIT_UPD: |
Markus Elfring | f7ca535 | 2016-07-23 08:30:52 +0200 | [diff] [blame] | 256 | if (uctxt) |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 257 | sc_return_credits(uctxt->sc); |
| 258 | break; |
| 259 | |
| 260 | case HFI1_IOCTL_TID_UPDATE: |
| 261 | if (copy_from_user(&tinfo, |
| 262 | (struct hfi11_tid_info __user *)arg, |
| 263 | sizeof(tinfo))) |
| 264 | return -EFAULT; |
| 265 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 266 | ret = hfi1_user_exp_rcv_setup(fd, &tinfo); |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 267 | if (!ret) { |
| 268 | /* |
| 269 | * Copy the number of tidlist entries we used |
| 270 | * and the length of the buffer we registered. |
| 271 | * These fields are adjacent in the structure so |
| 272 | * we can copy them at the same time. |
| 273 | */ |
| 274 | addr = arg + offsetof(struct hfi1_tid_info, tidcnt); |
| 275 | if (copy_to_user((void __user *)addr, &tinfo.tidcnt, |
| 276 | sizeof(tinfo.tidcnt) + |
| 277 | sizeof(tinfo.length))) |
| 278 | ret = -EFAULT; |
| 279 | } |
| 280 | break; |
| 281 | |
| 282 | case HFI1_IOCTL_TID_FREE: |
| 283 | if (copy_from_user(&tinfo, |
| 284 | (struct hfi11_tid_info __user *)arg, |
| 285 | sizeof(tinfo))) |
| 286 | return -EFAULT; |
| 287 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 288 | ret = hfi1_user_exp_rcv_clear(fd, &tinfo); |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 289 | if (ret) |
| 290 | break; |
| 291 | addr = arg + offsetof(struct hfi1_tid_info, tidcnt); |
| 292 | if (copy_to_user((void __user *)addr, &tinfo.tidcnt, |
| 293 | sizeof(tinfo.tidcnt))) |
| 294 | ret = -EFAULT; |
| 295 | break; |
| 296 | |
| 297 | case HFI1_IOCTL_TID_INVAL_READ: |
| 298 | if (copy_from_user(&tinfo, |
| 299 | (struct hfi11_tid_info __user *)arg, |
| 300 | sizeof(tinfo))) |
| 301 | return -EFAULT; |
| 302 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 303 | ret = hfi1_user_exp_rcv_invalid(fd, &tinfo); |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 304 | if (ret) |
| 305 | break; |
| 306 | addr = arg + offsetof(struct hfi1_tid_info, tidcnt); |
| 307 | if (copy_to_user((void __user *)addr, &tinfo.tidcnt, |
| 308 | sizeof(tinfo.tidcnt))) |
| 309 | ret = -EFAULT; |
| 310 | break; |
| 311 | |
| 312 | case HFI1_IOCTL_RECV_CTRL: |
| 313 | ret = get_user(uval, (int __user *)arg); |
| 314 | if (ret != 0) |
| 315 | return -EFAULT; |
| 316 | ret = manage_rcvq(uctxt, fd->subctxt, uval); |
| 317 | break; |
| 318 | |
| 319 | case HFI1_IOCTL_POLL_TYPE: |
| 320 | ret = get_user(uval, (int __user *)arg); |
| 321 | if (ret != 0) |
| 322 | return -EFAULT; |
| 323 | uctxt->poll_type = (typeof(uctxt->poll_type))uval; |
| 324 | break; |
| 325 | |
| 326 | case HFI1_IOCTL_ACK_EVENT: |
| 327 | ret = get_user(ul_uval, (unsigned long __user *)arg); |
| 328 | if (ret != 0) |
| 329 | return -EFAULT; |
| 330 | ret = user_event_ack(uctxt, fd->subctxt, ul_uval); |
| 331 | break; |
| 332 | |
| 333 | case HFI1_IOCTL_SET_PKEY: |
| 334 | ret = get_user(uval16, (u16 __user *)arg); |
| 335 | if (ret != 0) |
| 336 | return -EFAULT; |
| 337 | if (HFI1_CAP_IS_USET(PKEY_CHECK)) |
| 338 | ret = set_ctxt_pkey(uctxt, fd->subctxt, uval16); |
| 339 | else |
| 340 | return -EPERM; |
| 341 | break; |
| 342 | |
| 343 | case HFI1_IOCTL_CTXT_RESET: { |
| 344 | struct send_context *sc; |
| 345 | struct hfi1_devdata *dd; |
| 346 | |
| 347 | if (!uctxt || !uctxt->dd || !uctxt->sc) |
| 348 | return -EINVAL; |
| 349 | |
| 350 | /* |
| 351 | * There is no protection here. User level has to |
| 352 | * guarantee that no one will be writing to the send |
| 353 | * context while it is being re-initialized. |
| 354 | * If user level breaks that guarantee, it will break |
| 355 | * it's own context and no one else's. |
| 356 | */ |
| 357 | dd = uctxt->dd; |
| 358 | sc = uctxt->sc; |
| 359 | /* |
| 360 | * Wait until the interrupt handler has marked the |
| 361 | * context as halted or frozen. Report error if we time |
| 362 | * out. |
| 363 | */ |
| 364 | wait_event_interruptible_timeout( |
| 365 | sc->halt_wait, (sc->flags & SCF_HALTED), |
| 366 | msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT)); |
| 367 | if (!(sc->flags & SCF_HALTED)) |
| 368 | return -ENOLCK; |
| 369 | |
| 370 | /* |
| 371 | * If the send context was halted due to a Freeze, |
| 372 | * wait until the device has been "unfrozen" before |
| 373 | * resetting the context. |
| 374 | */ |
| 375 | if (sc->flags & SCF_FROZEN) { |
| 376 | wait_event_interruptible_timeout( |
| 377 | dd->event_queue, |
| 378 | !(ACCESS_ONCE(dd->flags) & HFI1_FROZEN), |
| 379 | msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT)); |
| 380 | if (dd->flags & HFI1_FROZEN) |
| 381 | return -ENOLCK; |
| 382 | |
| 383 | if (dd->flags & HFI1_FORCED_FREEZE) |
| 384 | /* |
| 385 | * Don't allow context reset if we are into |
| 386 | * forced freeze |
| 387 | */ |
| 388 | return -ENODEV; |
| 389 | |
| 390 | sc_disable(sc); |
| 391 | ret = sc_enable(sc); |
Michael J. Ruhl | 2250563 | 2017-07-24 07:46:06 -0700 | [diff] [blame^] | 392 | hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB, uctxt); |
Dennis Dalessandro | 8d970cf | 2016-05-19 05:26:24 -0700 | [diff] [blame] | 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); |
Michael J. Ruhl | f683c80 | 2017-06-09 16:00:19 -0700 | [diff] [blame] | 777 | fdata->uctxt = NULL; |
| 778 | hfi1_rcd_put(uctxt); /* fdata reference */ |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame] | 779 | if (!bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 780 | mutex_unlock(&hfi1_mutex); |
| 781 | goto done; |
| 782 | } |
| 783 | |
| 784 | spin_lock_irqsave(&dd->uctxt_lock, flags); |
| 785 | /* |
| 786 | * Disable receive context and interrupt available, reset all |
| 787 | * RcvCtxtCtrl bits to default values. |
| 788 | */ |
| 789 | hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS | |
| 790 | HFI1_RCVCTRL_TIDFLOW_DIS | |
| 791 | HFI1_RCVCTRL_INTRAVAIL_DIS | |
Mitko Haralanov | 566c157 | 2016-02-03 14:32:49 -0800 | [diff] [blame] | 792 | HFI1_RCVCTRL_TAILUPD_DIS | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 793 | HFI1_RCVCTRL_ONE_PKT_EGR_DIS | |
| 794 | HFI1_RCVCTRL_NO_RHQ_DROP_DIS | |
Michael J. Ruhl | 2250563 | 2017-07-24 07:46:06 -0700 | [diff] [blame^] | 795 | HFI1_RCVCTRL_NO_EGR_DROP_DIS, uctxt); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 796 | /* Clear the context's J_KEY */ |
Michael J. Ruhl | 1757397 | 2017-07-24 07:46:01 -0700 | [diff] [blame] | 797 | hfi1_clear_ctxt_jkey(dd, uctxt); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 798 | /* |
Michael J. Ruhl | f683c80 | 2017-06-09 16:00:19 -0700 | [diff] [blame] | 799 | * If a send context is allocated, reset context integrity |
| 800 | * checks to default and disable the send context. |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 801 | */ |
Michael J. Ruhl | f683c80 | 2017-06-09 16:00:19 -0700 | [diff] [blame] | 802 | if (uctxt->sc) { |
| 803 | set_pio_integrity(uctxt->sc); |
| 804 | sc_disable(uctxt->sc); |
| 805 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 806 | spin_unlock_irqrestore(&dd->uctxt_lock, flags); |
| 807 | |
Mike Marciniszyn | 9c1a99c3 | 2017-06-09 15:59:40 -0700 | [diff] [blame] | 808 | hfi1_free_ctxt_rcv_groups(uctxt); |
Michael J. Ruhl | 637a9a7 | 2017-05-04 05:15:03 -0700 | [diff] [blame] | 809 | hfi1_clear_ctxt_pkey(dd, uctxt); |
Mitko Haralanov | 9415844 | 2016-04-20 06:05:36 -0700 | [diff] [blame] | 810 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 811 | uctxt->event_flags = 0; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 812 | mutex_unlock(&hfi1_mutex); |
Michael J. Ruhl | 4249201 | 2017-07-24 07:45:43 -0700 | [diff] [blame] | 813 | |
| 814 | deallocate_ctxt(uctxt); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 815 | done: |
Ira Weiny | e0cf75d | 2016-08-16 13:27:03 -0700 | [diff] [blame] | 816 | mmdrop(fdata->mm); |
Dennis Dalessandro | e11ffbd | 2016-05-19 05:26:44 -0700 | [diff] [blame] | 817 | kobject_put(&dd->kobj); |
Tadeusz Struk | acd7c8f | 2016-10-25 08:57:55 -0700 | [diff] [blame] | 818 | |
| 819 | if (atomic_dec_and_test(&dd->user_refcount)) |
| 820 | complete(&dd->user_comp); |
| 821 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 822 | kfree(fdata); |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * Convert kernel *virtual* addresses to physical addresses. |
| 828 | * This is used to vmalloc'ed addresses. |
| 829 | */ |
| 830 | static u64 kvirt_to_phys(void *addr) |
| 831 | { |
| 832 | struct page *page; |
| 833 | u64 paddr = 0; |
| 834 | |
| 835 | page = vmalloc_to_page(addr); |
| 836 | if (page) |
| 837 | paddr = page_to_pfn(page) << PAGE_SHIFT; |
| 838 | |
| 839 | return paddr; |
| 840 | } |
| 841 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 842 | 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] | 843 | { |
Michael J. Ruhl | 62239fc | 2017-05-04 05:15:21 -0700 | [diff] [blame] | 844 | int ret; |
Dennis Dalessandro | 0eb6265 | 2016-05-19 05:25:50 -0700 | [diff] [blame] | 845 | unsigned int swmajor, swminor; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 846 | |
| 847 | swmajor = uinfo->userversion >> 16; |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 848 | if (swmajor != HFI1_USER_SWMAJOR) |
| 849 | return -ENODEV; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 850 | |
| 851 | swminor = uinfo->userversion & 0xffff; |
| 852 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 853 | mutex_lock(&hfi1_mutex); |
Michael J. Ruhl | 62239fc | 2017-05-04 05:15:21 -0700 | [diff] [blame] | 854 | /* |
| 855 | * Get a sub context if necessary. |
| 856 | * ret < 0 error, 0 no context, 1 sub-context found |
| 857 | */ |
| 858 | ret = 0; |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 859 | if (uinfo->subctxt_cnt) { |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 860 | ret = find_sub_ctxt(fd, uinfo); |
Michael J. Ruhl | 62239fc | 2017-05-04 05:15:21 -0700 | [diff] [blame] | 861 | if (ret > 0) |
Sebastian Sanchez | b094a36 | 2016-07-25 07:54:57 -0700 | [diff] [blame] | 862 | fd->rec_cpu_num = |
| 863 | hfi1_get_proc_affinity(fd->uctxt->numa_id); |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 864 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 865 | |
| 866 | /* |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame] | 867 | * 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] | 868 | * couldn't find a sub context. |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 869 | */ |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 870 | if (!ret) |
| 871 | ret = allocate_ctxt(fd, fd->dd, uinfo); |
| 872 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 873 | mutex_unlock(&hfi1_mutex); |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 874 | |
| 875 | /* Depending on the context type, do the appropriate init */ |
| 876 | if (ret > 0) { |
| 877 | /* |
| 878 | * sub-context info can only be set up after the base |
| 879 | * context has been completed. |
| 880 | */ |
| 881 | ret = wait_event_interruptible(fd->uctxt->wait, !test_bit( |
| 882 | HFI1_CTXT_BASE_UNINIT, |
| 883 | &fd->uctxt->event_flags)); |
Michael J. Ruhl | f683c80 | 2017-06-09 16:00:19 -0700 | [diff] [blame] | 884 | if (test_bit(HFI1_CTXT_BASE_FAILED, &fd->uctxt->event_flags)) |
| 885 | ret = -ENOMEM; |
| 886 | |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 887 | /* The only thing a sub context needs is the user_xxx stuff */ |
| 888 | if (!ret) |
Michael J. Ruhl | 62239fc | 2017-05-04 05:15:21 -0700 | [diff] [blame] | 889 | ret = init_user_ctxt(fd); |
| 890 | |
Michael J. Ruhl | 4249201 | 2017-07-24 07:45:43 -0700 | [diff] [blame] | 891 | if (ret) |
Michael J. Ruhl | 62239fc | 2017-05-04 05:15:21 -0700 | [diff] [blame] | 892 | clear_bit(fd->subctxt, fd->uctxt->in_use_ctxts); |
Michael J. Ruhl | 4249201 | 2017-07-24 07:45:43 -0700 | [diff] [blame] | 893 | |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 894 | } else if (!ret) { |
| 895 | ret = setup_base_ctxt(fd); |
Michael J. Ruhl | 62239fc | 2017-05-04 05:15:21 -0700 | [diff] [blame] | 896 | if (fd->uctxt->subctxt_cnt) { |
| 897 | /* If there is an error, set the failed bit. */ |
| 898 | if (ret) |
| 899 | set_bit(HFI1_CTXT_BASE_FAILED, |
| 900 | &fd->uctxt->event_flags); |
| 901 | /* |
| 902 | * Base context is done, notify anybody using a |
| 903 | * sub-context that is waiting for this completion |
| 904 | */ |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 905 | clear_bit(HFI1_CTXT_BASE_UNINIT, |
| 906 | &fd->uctxt->event_flags); |
| 907 | wake_up(&fd->uctxt->wait); |
| 908 | } |
Michael J. Ruhl | 4249201 | 2017-07-24 07:45:43 -0700 | [diff] [blame] | 909 | if (ret) |
| 910 | deallocate_ctxt(fd->uctxt); |
| 911 | } |
| 912 | |
| 913 | /* If an error occurred, clear the reference */ |
| 914 | if (ret && fd->uctxt) { |
| 915 | hfi1_rcd_put(fd->uctxt); |
| 916 | fd->uctxt = NULL; |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 919 | return ret; |
| 920 | } |
| 921 | |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame] | 922 | /* |
| 923 | * The hfi1_mutex must be held when this function is called. It is |
| 924 | * necessary to ensure serialized access to the bitmask in_use_ctxts. |
| 925 | */ |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 926 | static int find_sub_ctxt(struct hfi1_filedata *fd, |
| 927 | const struct hfi1_user_info *uinfo) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 928 | { |
Michael J. Ruhl | e6f7622 | 2017-07-24 07:45:55 -0700 | [diff] [blame] | 929 | u16 i; |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 930 | struct hfi1_devdata *dd = fd->dd; |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame] | 931 | u16 subctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 932 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 933 | for (i = dd->first_dyn_alloc_ctxt; i < dd->num_rcv_contexts; i++) { |
| 934 | struct hfi1_ctxtdata *uctxt = dd->rcd[i]; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 935 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 936 | /* Skip ctxts which are not yet open */ |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame] | 937 | if (!uctxt || |
| 938 | bitmap_empty(uctxt->in_use_ctxts, |
| 939 | HFI1_MAX_SHARED_CTXTS)) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 940 | continue; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 941 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 942 | /* Skip dynamically allocted kernel contexts */ |
| 943 | if (uctxt->sc && (uctxt->sc->type == SC_KERNEL)) |
| 944 | continue; |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 945 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 946 | /* Skip ctxt if it doesn't match the requested one */ |
| 947 | if (memcmp(uctxt->uuid, uinfo->uuid, |
| 948 | sizeof(uctxt->uuid)) || |
| 949 | uctxt->jkey != generate_jkey(current_uid()) || |
| 950 | uctxt->subctxt_id != uinfo->subctxt_id || |
| 951 | uctxt->subctxt_cnt != uinfo->subctxt_cnt) |
| 952 | continue; |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 953 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 954 | /* Verify the sharing process matches the master */ |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame] | 955 | if (uctxt->userversion != uinfo->userversion) |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 956 | return -EINVAL; |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame] | 957 | |
| 958 | /* Find an unused context */ |
| 959 | subctxt = find_first_zero_bit(uctxt->in_use_ctxts, |
| 960 | HFI1_MAX_SHARED_CTXTS); |
| 961 | if (subctxt >= uctxt->subctxt_cnt) |
Michael J. Ruhl | 62239fc | 2017-05-04 05:15:21 -0700 | [diff] [blame] | 962 | return -EBUSY; |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame] | 963 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 964 | fd->uctxt = uctxt; |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame] | 965 | fd->subctxt = subctxt; |
Michael J. Ruhl | f683c80 | 2017-06-09 16:00:19 -0700 | [diff] [blame] | 966 | |
| 967 | hfi1_rcd_get(uctxt); |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame] | 968 | __set_bit(fd->subctxt, uctxt->in_use_ctxts); |
| 969 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 970 | return 1; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 971 | } |
| 972 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 973 | return 0; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 974 | } |
| 975 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 976 | static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 977 | struct hfi1_user_info *uinfo) |
| 978 | { |
| 979 | struct hfi1_ctxtdata *uctxt; |
Michael J. Ruhl | e6f7622 | 2017-07-24 07:45:55 -0700 | [diff] [blame] | 980 | u16 ctxt; |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 981 | int ret, numa; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 982 | |
| 983 | if (dd->flags & HFI1_FROZEN) { |
| 984 | /* |
| 985 | * Pick an error that is unique from all other errors |
| 986 | * that are returned so the user process knows that |
| 987 | * it tried to allocate while the SPC was frozen. It |
| 988 | * it should be able to retry with success in a short |
| 989 | * while. |
| 990 | */ |
| 991 | return -EIO; |
| 992 | } |
| 993 | |
Michael J. Ruhl | 5fbded4 | 2017-05-04 05:14:57 -0700 | [diff] [blame] | 994 | /* |
| 995 | * This check is sort of redundant to the next EBUSY error. It would |
| 996 | * also indicate an inconsistancy in the driver if this value was |
| 997 | * zero, but there were still contexts available. |
| 998 | */ |
| 999 | if (!dd->freectxts) |
| 1000 | return -EBUSY; |
| 1001 | |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 1002 | for (ctxt = dd->first_dyn_alloc_ctxt; |
| 1003 | ctxt < dd->num_rcv_contexts; ctxt++) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1004 | if (!dd->rcd[ctxt]) |
| 1005 | break; |
| 1006 | |
| 1007 | if (ctxt == dd->num_rcv_contexts) |
| 1008 | return -EBUSY; |
| 1009 | |
Sebastian Sanchez | b094a36 | 2016-07-25 07:54:57 -0700 | [diff] [blame] | 1010 | /* |
| 1011 | * If we don't have a NUMA node requested, preference is towards |
| 1012 | * device NUMA node. |
| 1013 | */ |
| 1014 | fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node); |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 1015 | if (fd->rec_cpu_num != -1) |
| 1016 | numa = cpu_to_node(fd->rec_cpu_num); |
| 1017 | else |
| 1018 | numa = numa_node_id(); |
| 1019 | uctxt = hfi1_create_ctxtdata(dd->pport, ctxt, numa); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1020 | if (!uctxt) { |
| 1021 | dd_dev_err(dd, |
| 1022 | "Unable to allocate ctxtdata memory, failing open\n"); |
| 1023 | return -ENOMEM; |
| 1024 | } |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 1025 | hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)", |
| 1026 | uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num, |
| 1027 | uctxt->numa_id); |
| 1028 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1029 | /* |
| 1030 | * Allocate and enable a PIO send context. |
| 1031 | */ |
| 1032 | uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize, |
Mitko Haralanov | cc57236 | 2016-02-03 14:33:49 -0800 | [diff] [blame] | 1033 | uctxt->dd->node); |
Jakub Pawlak | 3a6982d | 2016-09-25 07:42:23 -0700 | [diff] [blame] | 1034 | if (!uctxt->sc) { |
| 1035 | ret = -ENOMEM; |
| 1036 | goto ctxdata_free; |
| 1037 | } |
Sebastian Sanchez | 6c63e42 | 2015-11-06 20:06:56 -0500 | [diff] [blame] | 1038 | hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index, |
| 1039 | uctxt->sc->hw_context); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1040 | ret = sc_enable(uctxt->sc); |
| 1041 | if (ret) |
Jakub Pawlak | 3a6982d | 2016-09-25 07:42:23 -0700 | [diff] [blame] | 1042 | goto ctxdata_free; |
| 1043 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1044 | /* |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1045 | * Setup sub context resources if the user-level has requested |
| 1046 | * sub contexts. |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1047 | * This has to be done here so the rest of the sub-contexts find the |
| 1048 | * proper master. |
| 1049 | */ |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1050 | if (uinfo->subctxt_cnt) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1051 | ret = init_subctxts(uctxt, uinfo); |
| 1052 | /* |
| 1053 | * On error, we don't need to disable and de-allocate the |
| 1054 | * send context because it will be done during file close |
| 1055 | */ |
| 1056 | if (ret) |
Jakub Pawlak | 3a6982d | 2016-09-25 07:42:23 -0700 | [diff] [blame] | 1057 | goto ctxdata_free; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1058 | } |
| 1059 | uctxt->userversion = uinfo->userversion; |
Dean Luick | bdf7752 | 2016-07-28 15:21:13 -0400 | [diff] [blame] | 1060 | uctxt->flags = hfi1_cap_mask; /* save current flag state */ |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1061 | init_waitqueue_head(&uctxt->wait); |
| 1062 | strlcpy(uctxt->comm, current->comm, sizeof(uctxt->comm)); |
| 1063 | memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid)); |
| 1064 | uctxt->jkey = generate_jkey(current_uid()); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1065 | hfi1_stats.sps_ctxts++; |
Ashutosh Dixit | affa48d | 2016-02-03 14:33:06 -0800 | [diff] [blame] | 1066 | /* |
| 1067 | * Disable ASPM when there are open user/PSM contexts to avoid |
| 1068 | * issues with ASPM L1 exit latency |
| 1069 | */ |
| 1070 | if (dd->freectxts-- == dd->num_user_contexts) |
| 1071 | aspm_disable_all(dd); |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1072 | fd->uctxt = uctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1073 | |
Michael J. Ruhl | f683c80 | 2017-06-09 16:00:19 -0700 | [diff] [blame] | 1074 | /* Count the reference for the fd */ |
| 1075 | hfi1_rcd_get(uctxt); |
| 1076 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1077 | return 0; |
Jakub Pawlak | 3a6982d | 2016-09-25 07:42:23 -0700 | [diff] [blame] | 1078 | |
| 1079 | ctxdata_free: |
| 1080 | dd->rcd[ctxt] = NULL; |
Michael J. Ruhl | f683c80 | 2017-06-09 16:00:19 -0700 | [diff] [blame] | 1081 | hfi1_rcd_put(uctxt); |
Jakub Pawlak | 3a6982d | 2016-09-25 07:42:23 -0700 | [diff] [blame] | 1082 | return ret; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1083 | } |
| 1084 | |
Michael J. Ruhl | 4249201 | 2017-07-24 07:45:43 -0700 | [diff] [blame] | 1085 | static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt) |
| 1086 | { |
| 1087 | mutex_lock(&hfi1_mutex); |
| 1088 | hfi1_stats.sps_ctxts--; |
| 1089 | if (++uctxt->dd->freectxts == uctxt->dd->num_user_contexts) |
| 1090 | aspm_enable_all(uctxt->dd); |
| 1091 | |
| 1092 | /* _rcd_put() should be done after releasing mutex */ |
| 1093 | uctxt->dd->rcd[uctxt->ctxt] = NULL; |
| 1094 | mutex_unlock(&hfi1_mutex); |
| 1095 | hfi1_rcd_put(uctxt); /* dd reference */ |
| 1096 | } |
| 1097 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1098 | static int init_subctxts(struct hfi1_ctxtdata *uctxt, |
| 1099 | const struct hfi1_user_info *uinfo) |
| 1100 | { |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame] | 1101 | u16 num_subctxts; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1102 | |
| 1103 | num_subctxts = uinfo->subctxt_cnt; |
Mitko Haralanov | acac10f | 2016-02-05 11:57:50 -0500 | [diff] [blame] | 1104 | if (num_subctxts > HFI1_MAX_SHARED_CTXTS) |
| 1105 | return -EINVAL; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1106 | |
| 1107 | uctxt->subctxt_cnt = uinfo->subctxt_cnt; |
| 1108 | uctxt->subctxt_id = uinfo->subctxt_id; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1109 | uctxt->redirect_seq_cnt = 1; |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1110 | set_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags); |
Mitko Haralanov | acac10f | 2016-02-05 11:57:50 -0500 | [diff] [blame] | 1111 | |
| 1112 | return 0; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | static int setup_subctxt(struct hfi1_ctxtdata *uctxt) |
| 1116 | { |
| 1117 | int ret = 0; |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame] | 1118 | u16 num_subctxts = uctxt->subctxt_cnt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1119 | |
| 1120 | uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE); |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1121 | if (!uctxt->subctxt_uregbase) |
| 1122 | return -ENOMEM; |
| 1123 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1124 | /* We can take the size of the RcvHdr Queue from the master */ |
| 1125 | uctxt->subctxt_rcvhdr_base = vmalloc_user(uctxt->rcvhdrq_size * |
| 1126 | num_subctxts); |
| 1127 | if (!uctxt->subctxt_rcvhdr_base) { |
| 1128 | ret = -ENOMEM; |
| 1129 | goto bail_ureg; |
| 1130 | } |
| 1131 | |
| 1132 | uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size * |
| 1133 | num_subctxts); |
| 1134 | if (!uctxt->subctxt_rcvegrbuf) { |
| 1135 | ret = -ENOMEM; |
| 1136 | goto bail_rhdr; |
| 1137 | } |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1138 | |
| 1139 | return 0; |
| 1140 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1141 | bail_rhdr: |
| 1142 | vfree(uctxt->subctxt_rcvhdr_base); |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1143 | uctxt->subctxt_rcvhdr_base = NULL; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1144 | bail_ureg: |
| 1145 | vfree(uctxt->subctxt_uregbase); |
| 1146 | uctxt->subctxt_uregbase = NULL; |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1147 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1148 | return ret; |
| 1149 | } |
| 1150 | |
Michael J. Ruhl | 62239fc | 2017-05-04 05:15:21 -0700 | [diff] [blame] | 1151 | static void user_init(struct hfi1_ctxtdata *uctxt) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1152 | { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1153 | unsigned int rcvctrl_ops = 0; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1154 | |
| 1155 | /* initialize poll variables... */ |
| 1156 | uctxt->urgent = 0; |
| 1157 | uctxt->urgent_poll = 0; |
| 1158 | |
| 1159 | /* |
| 1160 | * Now enable the ctxt for receive. |
| 1161 | * For chips that are set to DMA the tail register to memory |
| 1162 | * when they change (and when the update bit transitions from |
| 1163 | * 0 to 1. So for those chips, we turn it off and then back on. |
| 1164 | * This will (very briefly) affect any other open ctxts, but the |
| 1165 | * duration is very short, and therefore isn't an issue. We |
| 1166 | * explicitly set the in-memory tail copy to 0 beforehand, so we |
| 1167 | * don't have to wait to be sure the DMA update has happened |
| 1168 | * (chip resets head/tail to 0 on transition to enable). |
| 1169 | */ |
| 1170 | if (uctxt->rcvhdrtail_kvaddr) |
| 1171 | clear_rcvhdrtail(uctxt); |
| 1172 | |
| 1173 | /* Setup J_KEY before enabling the context */ |
Michael J. Ruhl | 1757397 | 2017-07-24 07:46:01 -0700 | [diff] [blame] | 1174 | hfi1_set_ctxt_jkey(uctxt->dd, uctxt, uctxt->jkey); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1175 | |
| 1176 | rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB; |
Dean Luick | bdf7752 | 2016-07-28 15:21:13 -0400 | [diff] [blame] | 1177 | if (HFI1_CAP_UGET_MASK(uctxt->flags, HDRSUPP)) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1178 | rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB; |
| 1179 | /* |
| 1180 | * Ignore the bit in the flags for now until proper |
| 1181 | * support for multiple packet per rcv array entry is |
| 1182 | * added. |
| 1183 | */ |
Dean Luick | bdf7752 | 2016-07-28 15:21:13 -0400 | [diff] [blame] | 1184 | if (!HFI1_CAP_UGET_MASK(uctxt->flags, MULTI_PKT_EGR)) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1185 | rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB; |
Dean Luick | bdf7752 | 2016-07-28 15:21:13 -0400 | [diff] [blame] | 1186 | if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_EGR_FULL)) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1187 | rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB; |
Dean Luick | bdf7752 | 2016-07-28 15:21:13 -0400 | [diff] [blame] | 1188 | if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_RHQ_FULL)) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1189 | rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB; |
Mitko Haralanov | 566c157 | 2016-02-03 14:32:49 -0800 | [diff] [blame] | 1190 | /* |
| 1191 | * The RcvCtxtCtrl.TailUpd bit has to be explicitly written. |
| 1192 | * We can't rely on the correct value to be set from prior |
| 1193 | * uses of the chip or ctxt. Therefore, add the rcvctrl op |
| 1194 | * for both cases. |
| 1195 | */ |
Dean Luick | bdf7752 | 2016-07-28 15:21:13 -0400 | [diff] [blame] | 1196 | if (HFI1_CAP_UGET_MASK(uctxt->flags, DMA_RTAIL)) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1197 | rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB; |
Mitko Haralanov | 566c157 | 2016-02-03 14:32:49 -0800 | [diff] [blame] | 1198 | else |
| 1199 | rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS; |
Michael J. Ruhl | 2250563 | 2017-07-24 07:46:06 -0700 | [diff] [blame^] | 1200 | hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1201 | } |
| 1202 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 1203 | static int get_ctxt_info(struct hfi1_filedata *fd, void __user *ubase, |
| 1204 | __u32 len) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1205 | { |
| 1206 | struct hfi1_ctxt_info cinfo; |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1207 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1208 | int ret = 0; |
| 1209 | |
Dan Carpenter | ebe6b2e | 2015-09-16 09:42:25 +0300 | [diff] [blame] | 1210 | memset(&cinfo, 0, sizeof(cinfo)); |
Dean Luick | bdf7752 | 2016-07-28 15:21:13 -0400 | [diff] [blame] | 1211 | cinfo.runtime_flags = (((uctxt->flags >> HFI1_CAP_MISC_SHIFT) & |
| 1212 | HFI1_CAP_MISC_MASK) << HFI1_CAP_USER_SHIFT) | |
| 1213 | HFI1_CAP_UGET_MASK(uctxt->flags, MASK) | |
| 1214 | HFI1_CAP_KGET_MASK(uctxt->flags, K2U); |
Dean Luick | 622c202 | 2016-07-28 15:21:21 -0400 | [diff] [blame] | 1215 | /* adjust flag if this fd is not able to cache */ |
| 1216 | if (!fd->handler) |
| 1217 | cinfo.runtime_flags |= HFI1_CAP_TID_UNMAP; /* no caching */ |
| 1218 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1219 | cinfo.num_active = hfi1_count_active_units(); |
| 1220 | cinfo.unit = uctxt->dd->unit; |
| 1221 | cinfo.ctxt = uctxt->ctxt; |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1222 | cinfo.subctxt = fd->subctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1223 | cinfo.rcvtids = roundup(uctxt->egrbufs.alloced, |
| 1224 | uctxt->dd->rcv_entries.group_size) + |
| 1225 | uctxt->expected_count; |
| 1226 | cinfo.credits = uctxt->sc->credits; |
| 1227 | cinfo.numa_node = uctxt->numa_id; |
| 1228 | cinfo.rec_cpu = fd->rec_cpu_num; |
| 1229 | cinfo.send_ctxt = uctxt->sc->hw_context; |
| 1230 | |
| 1231 | cinfo.egrtids = uctxt->egrbufs.alloced; |
| 1232 | cinfo.rcvhdrq_cnt = uctxt->rcvhdrq_cnt; |
| 1233 | cinfo.rcvhdrq_entsize = uctxt->rcvhdrqentsize << 2; |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1234 | cinfo.sdma_ring_size = fd->cq->nentries; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1235 | cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size; |
| 1236 | |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1237 | trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, cinfo); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1238 | if (copy_to_user(ubase, &cinfo, sizeof(cinfo))) |
| 1239 | ret = -EFAULT; |
Dean Luick | bdf7752 | 2016-07-28 15:21:13 -0400 | [diff] [blame] | 1240 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1241 | return ret; |
| 1242 | } |
| 1243 | |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1244 | static int init_user_ctxt(struct hfi1_filedata *fd) |
| 1245 | { |
| 1246 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
| 1247 | int ret; |
| 1248 | |
| 1249 | ret = hfi1_user_sdma_alloc_queues(uctxt, fd); |
| 1250 | if (ret) |
| 1251 | return ret; |
| 1252 | |
| 1253 | ret = hfi1_user_exp_rcv_init(fd); |
| 1254 | |
| 1255 | return ret; |
| 1256 | } |
| 1257 | |
| 1258 | static int setup_base_ctxt(struct hfi1_filedata *fd) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1259 | { |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1260 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1261 | struct hfi1_devdata *dd = uctxt->dd; |
| 1262 | int ret = 0; |
| 1263 | |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1264 | hfi1_init_ctxt(uctxt->sc); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1265 | |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1266 | /* Now allocate the RcvHdr queue and eager buffers. */ |
| 1267 | ret = hfi1_create_rcvhdrq(dd, uctxt); |
Mitko Haralanov | 9415844 | 2016-04-20 06:05:36 -0700 | [diff] [blame] | 1268 | if (ret) |
Michael J. Ruhl | 62239fc | 2017-05-04 05:15:21 -0700 | [diff] [blame] | 1269 | return ret; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1270 | |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1271 | ret = hfi1_setup_eagerbufs(uctxt); |
| 1272 | if (ret) |
Michael J. Ruhl | 62239fc | 2017-05-04 05:15:21 -0700 | [diff] [blame] | 1273 | goto setup_failed; |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1274 | |
| 1275 | /* If sub-contexts are enabled, do the appropriate setup */ |
| 1276 | if (uctxt->subctxt_cnt) |
| 1277 | ret = setup_subctxt(uctxt); |
| 1278 | if (ret) |
Michael J. Ruhl | 62239fc | 2017-05-04 05:15:21 -0700 | [diff] [blame] | 1279 | goto setup_failed; |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1280 | |
Mike Marciniszyn | 9c1a99c3 | 2017-06-09 15:59:40 -0700 | [diff] [blame] | 1281 | ret = hfi1_alloc_ctxt_rcv_groups(uctxt); |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1282 | if (ret) |
Michael J. Ruhl | 62239fc | 2017-05-04 05:15:21 -0700 | [diff] [blame] | 1283 | goto setup_failed; |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1284 | |
| 1285 | ret = init_user_ctxt(fd); |
| 1286 | if (ret) |
Michael J. Ruhl | 62239fc | 2017-05-04 05:15:21 -0700 | [diff] [blame] | 1287 | goto setup_failed; |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1288 | |
Michael J. Ruhl | 62239fc | 2017-05-04 05:15:21 -0700 | [diff] [blame] | 1289 | user_init(uctxt); |
| 1290 | |
| 1291 | return 0; |
| 1292 | |
| 1293 | setup_failed: |
Michael J. Ruhl | f683c80 | 2017-06-09 16:00:19 -0700 | [diff] [blame] | 1294 | /* Call _free_ctxtdata, not _rcd_put(). We still need the context. */ |
Michael J. Ruhl | 62239fc | 2017-05-04 05:15:21 -0700 | [diff] [blame] | 1295 | hfi1_free_ctxtdata(dd, uctxt); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1296 | return ret; |
| 1297 | } |
| 1298 | |
Michael J. Ruhl | 5042cdd | 2017-05-04 05:14:45 -0700 | [diff] [blame] | 1299 | static int get_base_info(struct hfi1_filedata *fd, void __user *ubase, |
| 1300 | __u32 len) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1301 | { |
| 1302 | struct hfi1_base_info binfo; |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1303 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1304 | struct hfi1_devdata *dd = uctxt->dd; |
| 1305 | ssize_t sz; |
| 1306 | unsigned offset; |
| 1307 | int ret = 0; |
| 1308 | |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 1309 | trace_hfi1_uctxtdata(uctxt->dd, uctxt, fd->subctxt); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1310 | |
| 1311 | memset(&binfo, 0, sizeof(binfo)); |
| 1312 | binfo.hw_version = dd->revision; |
| 1313 | binfo.sw_version = HFI1_KERN_SWVERSION; |
| 1314 | binfo.bthqp = kdeth_qp; |
| 1315 | binfo.jkey = uctxt->jkey; |
| 1316 | /* |
| 1317 | * If more than 64 contexts are enabled the allocated credit |
| 1318 | * return will span two or three contiguous pages. Since we only |
| 1319 | * map the page containing the context's credit return address, |
| 1320 | * we need to calculate the offset in the proper page. |
| 1321 | */ |
| 1322 | offset = ((u64)uctxt->sc->hw_free - |
| 1323 | (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE; |
| 1324 | binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1325 | fd->subctxt, offset); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1326 | binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1327 | fd->subctxt, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1328 | uctxt->sc->base_addr); |
| 1329 | binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP, |
| 1330 | uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1331 | fd->subctxt, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1332 | uctxt->sc->base_addr); |
| 1333 | binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1334 | fd->subctxt, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1335 | uctxt->rcvhdrq); |
| 1336 | binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1337 | fd->subctxt, |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1338 | uctxt->egrbufs.rcvtids[0].dma); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1339 | binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1340 | fd->subctxt, 0); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1341 | /* |
| 1342 | * user regs are at |
| 1343 | * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE)) |
| 1344 | */ |
| 1345 | binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1346 | fd->subctxt, 0); |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 1347 | offset = offset_in_page((((uctxt->ctxt - dd->first_dyn_alloc_ctxt) * |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1348 | HFI1_MAX_SHARED_CTXTS) + fd->subctxt) * |
Geliang Tang | e260e40 | 2015-10-03 10:34:59 +0800 | [diff] [blame] | 1349 | sizeof(*dd->events)); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1350 | binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1351 | fd->subctxt, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1352 | offset); |
| 1353 | binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1354 | fd->subctxt, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1355 | dd->status); |
| 1356 | if (HFI1_CAP_IS_USET(DMA_RTAIL)) |
| 1357 | binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1358 | fd->subctxt, 0); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1359 | if (uctxt->subctxt_cnt) { |
| 1360 | binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS, |
| 1361 | uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1362 | fd->subctxt, 0); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1363 | binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ, |
| 1364 | uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1365 | fd->subctxt, 0); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1366 | binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF, |
| 1367 | uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1368 | fd->subctxt, 0); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1369 | } |
| 1370 | sz = (len < sizeof(binfo)) ? len : sizeof(binfo); |
| 1371 | if (copy_to_user(ubase, &binfo, sz)) |
| 1372 | ret = -EFAULT; |
| 1373 | return ret; |
| 1374 | } |
| 1375 | |
| 1376 | static unsigned int poll_urgent(struct file *fp, |
| 1377 | struct poll_table_struct *pt) |
| 1378 | { |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1379 | struct hfi1_filedata *fd = fp->private_data; |
| 1380 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1381 | struct hfi1_devdata *dd = uctxt->dd; |
| 1382 | unsigned pollflag; |
| 1383 | |
| 1384 | poll_wait(fp, &uctxt->wait, pt); |
| 1385 | |
| 1386 | spin_lock_irq(&dd->uctxt_lock); |
| 1387 | if (uctxt->urgent != uctxt->urgent_poll) { |
| 1388 | pollflag = POLLIN | POLLRDNORM; |
| 1389 | uctxt->urgent_poll = uctxt->urgent; |
| 1390 | } else { |
| 1391 | pollflag = 0; |
| 1392 | set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags); |
| 1393 | } |
| 1394 | spin_unlock_irq(&dd->uctxt_lock); |
| 1395 | |
| 1396 | return pollflag; |
| 1397 | } |
| 1398 | |
| 1399 | static unsigned int poll_next(struct file *fp, |
| 1400 | struct poll_table_struct *pt) |
| 1401 | { |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 1402 | struct hfi1_filedata *fd = fp->private_data; |
| 1403 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1404 | struct hfi1_devdata *dd = uctxt->dd; |
| 1405 | unsigned pollflag; |
| 1406 | |
| 1407 | poll_wait(fp, &uctxt->wait, pt); |
| 1408 | |
| 1409 | spin_lock_irq(&dd->uctxt_lock); |
| 1410 | if (hdrqempty(uctxt)) { |
| 1411 | set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags); |
Michael J. Ruhl | 2250563 | 2017-07-24 07:46:06 -0700 | [diff] [blame^] | 1412 | hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1413 | pollflag = 0; |
Jubin John | e490974 | 2016-02-14 20:22:00 -0800 | [diff] [blame] | 1414 | } else { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1415 | pollflag = POLLIN | POLLRDNORM; |
Jubin John | e490974 | 2016-02-14 20:22:00 -0800 | [diff] [blame] | 1416 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1417 | spin_unlock_irq(&dd->uctxt_lock); |
| 1418 | |
| 1419 | return pollflag; |
| 1420 | } |
| 1421 | |
| 1422 | /* |
| 1423 | * Find all user contexts in use, and set the specified bit in their |
| 1424 | * event mask. |
| 1425 | * See also find_ctxt() for a similar use, that is specific to send buffers. |
| 1426 | */ |
| 1427 | int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit) |
| 1428 | { |
| 1429 | struct hfi1_ctxtdata *uctxt; |
| 1430 | struct hfi1_devdata *dd = ppd->dd; |
Michael J. Ruhl | e6f7622 | 2017-07-24 07:45:55 -0700 | [diff] [blame] | 1431 | u16 ctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1432 | int ret = 0; |
| 1433 | unsigned long flags; |
| 1434 | |
| 1435 | if (!dd->events) { |
| 1436 | ret = -EINVAL; |
| 1437 | goto done; |
| 1438 | } |
| 1439 | |
| 1440 | spin_lock_irqsave(&dd->uctxt_lock, flags); |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 1441 | for (ctxt = dd->first_dyn_alloc_ctxt; ctxt < dd->num_rcv_contexts; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1442 | ctxt++) { |
| 1443 | uctxt = dd->rcd[ctxt]; |
| 1444 | if (uctxt) { |
| 1445 | unsigned long *evs = dd->events + |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 1446 | (uctxt->ctxt - dd->first_dyn_alloc_ctxt) * |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1447 | HFI1_MAX_SHARED_CTXTS; |
| 1448 | int i; |
| 1449 | /* |
| 1450 | * subctxt_cnt is 0 if not shared, so do base |
| 1451 | * separately, first, then remaining subctxt, if any |
| 1452 | */ |
| 1453 | set_bit(evtbit, evs); |
| 1454 | for (i = 1; i < uctxt->subctxt_cnt; i++) |
| 1455 | set_bit(evtbit, evs + i); |
| 1456 | } |
| 1457 | } |
| 1458 | spin_unlock_irqrestore(&dd->uctxt_lock, flags); |
| 1459 | done: |
| 1460 | return ret; |
| 1461 | } |
| 1462 | |
| 1463 | /** |
| 1464 | * manage_rcvq - manage a context's receive queue |
| 1465 | * @uctxt: the context |
| 1466 | * @subctxt: the sub-context |
| 1467 | * @start_stop: action to carry out |
| 1468 | * |
| 1469 | * start_stop == 0 disables receive on the context, for use in queue |
| 1470 | * overflow conditions. start_stop==1 re-enables, to be used to |
| 1471 | * re-init the software copy of the head register |
| 1472 | */ |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame] | 1473 | static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1474 | int start_stop) |
| 1475 | { |
| 1476 | struct hfi1_devdata *dd = uctxt->dd; |
| 1477 | unsigned int rcvctrl_op; |
| 1478 | |
| 1479 | if (subctxt) |
| 1480 | goto bail; |
| 1481 | /* atomically clear receive enable ctxt. */ |
| 1482 | if (start_stop) { |
| 1483 | /* |
| 1484 | * On enable, force in-memory copy of the tail register to |
| 1485 | * 0, so that protocol code doesn't have to worry about |
| 1486 | * whether or not the chip has yet updated the in-memory |
| 1487 | * copy or not on return from the system call. The chip |
| 1488 | * always resets it's tail register back to 0 on a |
| 1489 | * transition from disabled to enabled. |
| 1490 | */ |
| 1491 | if (uctxt->rcvhdrtail_kvaddr) |
| 1492 | clear_rcvhdrtail(uctxt); |
| 1493 | rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB; |
Jubin John | e490974 | 2016-02-14 20:22:00 -0800 | [diff] [blame] | 1494 | } else { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1495 | rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS; |
Jubin John | e490974 | 2016-02-14 20:22:00 -0800 | [diff] [blame] | 1496 | } |
Michael J. Ruhl | 2250563 | 2017-07-24 07:46:06 -0700 | [diff] [blame^] | 1497 | hfi1_rcvctrl(dd, rcvctrl_op, uctxt); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1498 | /* always; new head should be equal to new tail; see above */ |
| 1499 | bail: |
| 1500 | return 0; |
| 1501 | } |
| 1502 | |
| 1503 | /* |
| 1504 | * clear the event notifier events for this context. |
| 1505 | * User process then performs actions appropriate to bit having been |
| 1506 | * set, if desired, and checks again in future. |
| 1507 | */ |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame] | 1508 | static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1509 | unsigned long events) |
| 1510 | { |
| 1511 | int i; |
| 1512 | struct hfi1_devdata *dd = uctxt->dd; |
| 1513 | unsigned long *evs; |
| 1514 | |
| 1515 | if (!dd->events) |
| 1516 | return 0; |
| 1517 | |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 1518 | evs = dd->events + ((uctxt->ctxt - dd->first_dyn_alloc_ctxt) * |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1519 | HFI1_MAX_SHARED_CTXTS) + subctxt; |
| 1520 | |
| 1521 | for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) { |
| 1522 | if (!test_bit(i, &events)) |
| 1523 | continue; |
| 1524 | clear_bit(i, evs); |
| 1525 | } |
| 1526 | return 0; |
| 1527 | } |
| 1528 | |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame] | 1529 | 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] | 1530 | { |
| 1531 | int ret = -ENOENT, i, intable = 0; |
| 1532 | struct hfi1_pportdata *ppd = uctxt->ppd; |
| 1533 | struct hfi1_devdata *dd = uctxt->dd; |
| 1534 | |
| 1535 | if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY) { |
| 1536 | ret = -EINVAL; |
| 1537 | goto done; |
| 1538 | } |
| 1539 | |
| 1540 | for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) |
| 1541 | if (pkey == ppd->pkeys[i]) { |
| 1542 | intable = 1; |
| 1543 | break; |
| 1544 | } |
| 1545 | |
| 1546 | if (intable) |
Michael J. Ruhl | 1757397 | 2017-07-24 07:46:01 -0700 | [diff] [blame] | 1547 | ret = hfi1_set_ctxt_pkey(dd, uctxt, pkey); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1548 | done: |
| 1549 | return ret; |
| 1550 | } |
| 1551 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1552 | static void user_remove(struct hfi1_devdata *dd) |
| 1553 | { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1554 | |
| 1555 | hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | static int user_add(struct hfi1_devdata *dd) |
| 1559 | { |
| 1560 | char name[10]; |
| 1561 | int ret; |
| 1562 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1563 | snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit); |
Dennis Dalessandro | 0eb6265 | 2016-05-19 05:25:50 -0700 | [diff] [blame] | 1564 | ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops, |
Ira Weiny | e116a64 | 2015-09-17 13:47:49 -0400 | [diff] [blame] | 1565 | &dd->user_cdev, &dd->user_device, |
Dennis Dalessandro | e11ffbd | 2016-05-19 05:26:44 -0700 | [diff] [blame] | 1566 | true, &dd->kobj); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1567 | if (ret) |
Dennis Dalessandro | 7312f29 | 2016-05-19 05:25:57 -0700 | [diff] [blame] | 1568 | user_remove(dd); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1569 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1570 | return ret; |
| 1571 | } |
| 1572 | |
| 1573 | /* |
| 1574 | * Create per-unit files in /dev |
| 1575 | */ |
| 1576 | int hfi1_device_create(struct hfi1_devdata *dd) |
| 1577 | { |
Dennis Dalessandro | 0f7b1f9 | 2016-05-19 05:26:10 -0700 | [diff] [blame] | 1578 | return user_add(dd); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | /* |
| 1582 | * Remove per-unit files in /dev |
| 1583 | * void, core kernel returns no errors for this stuff |
| 1584 | */ |
| 1585 | void hfi1_device_remove(struct hfi1_devdata *dd) |
| 1586 | { |
| 1587 | user_remove(dd); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1588 | } |