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