Commit | Line | Data |
---|---|---|
77241056 MM |
1 | /* |
2 | * | |
3 | * This file is provided under a dual BSD/GPLv2 license. When using or | |
4 | * redistributing this file, you may do so under either license. | |
5 | * | |
6 | * GPL LICENSE SUMMARY | |
7 | * | |
8 | * Copyright(c) 2015 Intel Corporation. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of version 2 of the GNU General Public License as | |
12 | * published by the Free Software Foundation. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, but | |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * General Public License for more details. | |
18 | * | |
19 | * BSD LICENSE | |
20 | * | |
21 | * Copyright(c) 2015 Intel Corporation. | |
22 | * | |
23 | * Redistribution and use in source and binary forms, with or without | |
24 | * modification, are permitted provided that the following conditions | |
25 | * are met: | |
26 | * | |
27 | * - Redistributions of source code must retain the above copyright | |
28 | * notice, this list of conditions and the following disclaimer. | |
29 | * - Redistributions in binary form must reproduce the above copyright | |
30 | * notice, this list of conditions and the following disclaimer in | |
31 | * the documentation and/or other materials provided with the | |
32 | * distribution. | |
33 | * - Neither the name of Intel Corporation nor the names of its | |
34 | * contributors may be used to endorse or promote products derived | |
35 | * from this software without specific prior written permission. | |
36 | * | |
37 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
38 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
39 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
40 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
41 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
42 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
43 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
44 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
45 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
46 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
47 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
48 | * | |
49 | */ | |
77241056 MM |
50 | #include <linux/poll.h> |
51 | #include <linux/cdev.h> | |
77241056 | 52 | #include <linux/vmalloc.h> |
77241056 | 53 | #include <linux/io.h> |
77241056 MM |
54 | |
55 | #include "hfi.h" | |
56 | #include "pio.h" | |
57 | #include "device.h" | |
58 | #include "common.h" | |
59 | #include "trace.h" | |
60 | #include "user_sdma.h" | |
701e441d | 61 | #include "user_exp_rcv.h" |
77241056 | 62 | #include "eprom.h" |
affa48de | 63 | #include "aspm.h" |
77241056 MM |
64 | |
65 | #undef pr_fmt | |
66 | #define pr_fmt(fmt) DRIVER_NAME ": " fmt | |
67 | ||
68 | #define SEND_CTXT_HALT_TIMEOUT 1000 /* msecs */ | |
69 | ||
70 | /* | |
71 | * File operation functions | |
72 | */ | |
73 | static int hfi1_file_open(struct inode *, struct file *); | |
74 | static int hfi1_file_close(struct inode *, struct file *); | |
75 | static ssize_t hfi1_file_write(struct file *, const char __user *, | |
76 | size_t, loff_t *); | |
77 | static ssize_t hfi1_write_iter(struct kiocb *, struct iov_iter *); | |
78 | static unsigned int hfi1_poll(struct file *, struct poll_table_struct *); | |
79 | static int hfi1_file_mmap(struct file *, struct vm_area_struct *); | |
80 | ||
81 | static u64 kvirt_to_phys(void *); | |
82 | static int assign_ctxt(struct file *, struct hfi1_user_info *); | |
83 | static int init_subctxts(struct hfi1_ctxtdata *, const struct hfi1_user_info *); | |
84 | static int user_init(struct file *); | |
85 | static int get_ctxt_info(struct file *, void __user *, __u32); | |
86 | static int get_base_info(struct file *, void __user *, __u32); | |
87 | static int setup_ctxt(struct file *); | |
88 | static int setup_subctxt(struct hfi1_ctxtdata *); | |
89 | static int get_user_context(struct file *, struct hfi1_user_info *, | |
90 | int, unsigned); | |
91 | static int find_shared_ctxt(struct file *, const struct hfi1_user_info *); | |
92 | static int allocate_ctxt(struct file *, struct hfi1_devdata *, | |
93 | struct hfi1_user_info *); | |
94 | static unsigned int poll_urgent(struct file *, struct poll_table_struct *); | |
95 | static unsigned int poll_next(struct file *, struct poll_table_struct *); | |
96 | static int user_event_ack(struct hfi1_ctxtdata *, int, unsigned long); | |
97 | static int set_ctxt_pkey(struct hfi1_ctxtdata *, unsigned, u16); | |
98 | static int manage_rcvq(struct hfi1_ctxtdata *, unsigned, int); | |
99 | static int vma_fault(struct vm_area_struct *, struct vm_fault *); | |
77241056 MM |
100 | |
101 | static const struct file_operations hfi1_file_ops = { | |
102 | .owner = THIS_MODULE, | |
103 | .write = hfi1_file_write, | |
104 | .write_iter = hfi1_write_iter, | |
105 | .open = hfi1_file_open, | |
106 | .release = hfi1_file_close, | |
107 | .poll = hfi1_poll, | |
108 | .mmap = hfi1_file_mmap, | |
109 | .llseek = noop_llseek, | |
110 | }; | |
111 | ||
112 | static struct vm_operations_struct vm_ops = { | |
113 | .fault = vma_fault, | |
114 | }; | |
115 | ||
116 | /* | |
117 | * Types of memories mapped into user processes' space | |
118 | */ | |
119 | enum mmap_types { | |
120 | PIO_BUFS = 1, | |
121 | PIO_BUFS_SOP, | |
122 | PIO_CRED, | |
123 | RCV_HDRQ, | |
124 | RCV_EGRBUF, | |
125 | UREGS, | |
126 | EVENTS, | |
127 | STATUS, | |
128 | RTAIL, | |
129 | SUBCTXT_UREGS, | |
130 | SUBCTXT_RCV_HDRQ, | |
131 | SUBCTXT_EGRBUF, | |
132 | SDMA_COMP | |
133 | }; | |
134 | ||
135 | /* | |
136 | * Masks and offsets defining the mmap tokens | |
137 | */ | |
138 | #define HFI1_MMAP_OFFSET_MASK 0xfffULL | |
139 | #define HFI1_MMAP_OFFSET_SHIFT 0 | |
140 | #define HFI1_MMAP_SUBCTXT_MASK 0xfULL | |
141 | #define HFI1_MMAP_SUBCTXT_SHIFT 12 | |
142 | #define HFI1_MMAP_CTXT_MASK 0xffULL | |
143 | #define HFI1_MMAP_CTXT_SHIFT 16 | |
144 | #define HFI1_MMAP_TYPE_MASK 0xfULL | |
145 | #define HFI1_MMAP_TYPE_SHIFT 24 | |
146 | #define HFI1_MMAP_MAGIC_MASK 0xffffffffULL | |
147 | #define HFI1_MMAP_MAGIC_SHIFT 32 | |
148 | ||
149 | #define HFI1_MMAP_MAGIC 0xdabbad00 | |
150 | ||
151 | #define HFI1_MMAP_TOKEN_SET(field, val) \ | |
152 | (((val) & HFI1_MMAP_##field##_MASK) << HFI1_MMAP_##field##_SHIFT) | |
153 | #define HFI1_MMAP_TOKEN_GET(field, token) \ | |
154 | (((token) >> HFI1_MMAP_##field##_SHIFT) & HFI1_MMAP_##field##_MASK) | |
155 | #define HFI1_MMAP_TOKEN(type, ctxt, subctxt, addr) \ | |
156 | (HFI1_MMAP_TOKEN_SET(MAGIC, HFI1_MMAP_MAGIC) | \ | |
157 | HFI1_MMAP_TOKEN_SET(TYPE, type) | \ | |
158 | HFI1_MMAP_TOKEN_SET(CTXT, ctxt) | \ | |
159 | HFI1_MMAP_TOKEN_SET(SUBCTXT, subctxt) | \ | |
e260e404 | 160 | HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr)))) |
77241056 | 161 | |
77241056 MM |
162 | #define dbg(fmt, ...) \ |
163 | pr_info(fmt, ##__VA_ARGS__) | |
164 | ||
77241056 MM |
165 | static inline int is_valid_mmap(u64 token) |
166 | { | |
167 | return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC); | |
168 | } | |
169 | ||
170 | static int hfi1_file_open(struct inode *inode, struct file *fp) | |
171 | { | |
172 | /* The real work is performed later in assign_ctxt() */ | |
173 | fp->private_data = kzalloc(sizeof(struct hfi1_filedata), GFP_KERNEL); | |
174 | if (fp->private_data) /* no cpu affinity by default */ | |
175 | ((struct hfi1_filedata *)fp->private_data)->rec_cpu_num = -1; | |
176 | return fp->private_data ? 0 : -ENOMEM; | |
177 | } | |
178 | ||
179 | static ssize_t hfi1_file_write(struct file *fp, const char __user *data, | |
180 | size_t count, loff_t *offset) | |
181 | { | |
182 | const struct hfi1_cmd __user *ucmd; | |
9e10af47 IW |
183 | struct hfi1_filedata *fd = fp->private_data; |
184 | struct hfi1_ctxtdata *uctxt = fd->uctxt; | |
77241056 MM |
185 | struct hfi1_cmd cmd; |
186 | struct hfi1_user_info uinfo; | |
187 | struct hfi1_tid_info tinfo; | |
0b091fb3 | 188 | unsigned long addr; |
77241056 MM |
189 | ssize_t consumed = 0, copy = 0, ret = 0; |
190 | void *dest = NULL; | |
191 | __u64 user_val = 0; | |
192 | int uctxt_required = 1; | |
193 | int must_be_root = 0; | |
194 | ||
195 | if (count < sizeof(cmd)) { | |
196 | ret = -EINVAL; | |
197 | goto bail; | |
198 | } | |
199 | ||
200 | ucmd = (const struct hfi1_cmd __user *)data; | |
201 | if (copy_from_user(&cmd, ucmd, sizeof(cmd))) { | |
202 | ret = -EFAULT; | |
203 | goto bail; | |
204 | } | |
205 | ||
206 | consumed = sizeof(cmd); | |
207 | ||
208 | switch (cmd.type) { | |
209 | case HFI1_CMD_ASSIGN_CTXT: | |
210 | uctxt_required = 0; /* assigned user context not required */ | |
211 | copy = sizeof(uinfo); | |
212 | dest = &uinfo; | |
213 | break; | |
214 | case HFI1_CMD_SDMA_STATUS_UPD: | |
215 | case HFI1_CMD_CREDIT_UPD: | |
216 | copy = 0; | |
217 | break; | |
218 | case HFI1_CMD_TID_UPDATE: | |
219 | case HFI1_CMD_TID_FREE: | |
0b091fb3 | 220 | case HFI1_CMD_TID_INVAL_READ: |
77241056 MM |
221 | copy = sizeof(tinfo); |
222 | dest = &tinfo; | |
223 | break; | |
224 | case HFI1_CMD_USER_INFO: | |
225 | case HFI1_CMD_RECV_CTRL: | |
226 | case HFI1_CMD_POLL_TYPE: | |
227 | case HFI1_CMD_ACK_EVENT: | |
228 | case HFI1_CMD_CTXT_INFO: | |
229 | case HFI1_CMD_SET_PKEY: | |
230 | case HFI1_CMD_CTXT_RESET: | |
231 | copy = 0; | |
232 | user_val = cmd.addr; | |
233 | break; | |
234 | case HFI1_CMD_EP_INFO: | |
235 | case HFI1_CMD_EP_ERASE_CHIP: | |
cd371e09 DL |
236 | case HFI1_CMD_EP_ERASE_RANGE: |
237 | case HFI1_CMD_EP_READ_RANGE: | |
238 | case HFI1_CMD_EP_WRITE_RANGE: | |
77241056 MM |
239 | uctxt_required = 0; /* assigned user context not required */ |
240 | must_be_root = 1; /* validate user */ | |
241 | copy = 0; | |
242 | break; | |
243 | default: | |
244 | ret = -EINVAL; | |
245 | goto bail; | |
246 | } | |
247 | ||
248 | /* If the command comes with user data, copy it. */ | |
249 | if (copy) { | |
250 | if (copy_from_user(dest, (void __user *)cmd.addr, copy)) { | |
251 | ret = -EFAULT; | |
252 | goto bail; | |
253 | } | |
254 | consumed += copy; | |
255 | } | |
256 | ||
257 | /* | |
258 | * Make sure there is a uctxt when needed. | |
259 | */ | |
260 | if (uctxt_required && !uctxt) { | |
261 | ret = -EINVAL; | |
262 | goto bail; | |
263 | } | |
264 | ||
265 | /* only root can do these operations */ | |
266 | if (must_be_root && !capable(CAP_SYS_ADMIN)) { | |
267 | ret = -EPERM; | |
268 | goto bail; | |
269 | } | |
270 | ||
271 | switch (cmd.type) { | |
272 | case HFI1_CMD_ASSIGN_CTXT: | |
273 | ret = assign_ctxt(fp, &uinfo); | |
274 | if (ret < 0) | |
275 | goto bail; | |
276 | ret = setup_ctxt(fp); | |
277 | if (ret) | |
278 | goto bail; | |
279 | ret = user_init(fp); | |
280 | break; | |
281 | case HFI1_CMD_CTXT_INFO: | |
282 | ret = get_ctxt_info(fp, (void __user *)(unsigned long) | |
283 | user_val, cmd.len); | |
284 | break; | |
285 | case HFI1_CMD_USER_INFO: | |
286 | ret = get_base_info(fp, (void __user *)(unsigned long) | |
287 | user_val, cmd.len); | |
288 | break; | |
289 | case HFI1_CMD_SDMA_STATUS_UPD: | |
290 | break; | |
291 | case HFI1_CMD_CREDIT_UPD: | |
292 | if (uctxt && uctxt->sc) | |
293 | sc_return_credits(uctxt->sc); | |
294 | break; | |
295 | case HFI1_CMD_TID_UPDATE: | |
0b091fb3 | 296 | ret = hfi1_user_exp_rcv_setup(fp, &tinfo); |
77241056 | 297 | if (!ret) { |
77241056 MM |
298 | /* |
299 | * Copy the number of tidlist entries we used | |
300 | * and the length of the buffer we registered. | |
301 | * These fields are adjacent in the structure so | |
302 | * we can copy them at the same time. | |
303 | */ | |
304 | addr = (unsigned long)cmd.addr + | |
305 | offsetof(struct hfi1_tid_info, tidcnt); | |
306 | if (copy_to_user((void __user *)addr, &tinfo.tidcnt, | |
307 | sizeof(tinfo.tidcnt) + | |
308 | sizeof(tinfo.length))) | |
309 | ret = -EFAULT; | |
310 | } | |
311 | break; | |
0b091fb3 MH |
312 | case HFI1_CMD_TID_INVAL_READ: |
313 | ret = hfi1_user_exp_rcv_invalid(fp, &tinfo); | |
314 | if (ret) | |
315 | break; | |
316 | addr = (unsigned long)cmd.addr + | |
317 | offsetof(struct hfi1_tid_info, tidcnt); | |
318 | if (copy_to_user((void __user *)addr, &tinfo.tidcnt, | |
319 | sizeof(tinfo.tidcnt))) | |
320 | ret = -EFAULT; | |
321 | break; | |
77241056 | 322 | case HFI1_CMD_TID_FREE: |
0b091fb3 MH |
323 | ret = hfi1_user_exp_rcv_clear(fp, &tinfo); |
324 | if (ret) | |
325 | break; | |
326 | addr = (unsigned long)cmd.addr + | |
327 | offsetof(struct hfi1_tid_info, tidcnt); | |
328 | if (copy_to_user((void __user *)addr, &tinfo.tidcnt, | |
329 | sizeof(tinfo.tidcnt))) | |
330 | ret = -EFAULT; | |
77241056 MM |
331 | break; |
332 | case HFI1_CMD_RECV_CTRL: | |
9e10af47 | 333 | ret = manage_rcvq(uctxt, fd->subctxt, (int)user_val); |
77241056 MM |
334 | break; |
335 | case HFI1_CMD_POLL_TYPE: | |
336 | uctxt->poll_type = (typeof(uctxt->poll_type))user_val; | |
337 | break; | |
338 | case HFI1_CMD_ACK_EVENT: | |
9e10af47 | 339 | ret = user_event_ack(uctxt, fd->subctxt, user_val); |
77241056 MM |
340 | break; |
341 | case HFI1_CMD_SET_PKEY: | |
342 | if (HFI1_CAP_IS_USET(PKEY_CHECK)) | |
9e10af47 | 343 | ret = set_ctxt_pkey(uctxt, fd->subctxt, user_val); |
77241056 MM |
344 | else |
345 | ret = -EPERM; | |
346 | break; | |
347 | case HFI1_CMD_CTXT_RESET: { | |
348 | struct send_context *sc; | |
349 | struct hfi1_devdata *dd; | |
350 | ||
351 | if (!uctxt || !uctxt->dd || !uctxt->sc) { | |
352 | ret = -EINVAL; | |
353 | break; | |
354 | } | |
355 | /* | |
356 | * There is no protection here. User level has to | |
357 | * guarantee that no one will be writing to the send | |
358 | * context while it is being re-initialized. | |
359 | * If user level breaks that guarantee, it will break | |
360 | * it's own context and no one else's. | |
361 | */ | |
362 | dd = uctxt->dd; | |
363 | sc = uctxt->sc; | |
364 | /* | |
365 | * Wait until the interrupt handler has marked the | |
366 | * context as halted or frozen. Report error if we time | |
367 | * out. | |
368 | */ | |
369 | wait_event_interruptible_timeout( | |
370 | sc->halt_wait, (sc->flags & SCF_HALTED), | |
371 | msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT)); | |
372 | if (!(sc->flags & SCF_HALTED)) { | |
373 | ret = -ENOLCK; | |
374 | break; | |
375 | } | |
376 | /* | |
377 | * If the send context was halted due to a Freeze, | |
378 | * wait until the device has been "unfrozen" before | |
379 | * resetting the context. | |
380 | */ | |
381 | if (sc->flags & SCF_FROZEN) { | |
382 | wait_event_interruptible_timeout( | |
383 | dd->event_queue, | |
384 | !(ACCESS_ONCE(dd->flags) & HFI1_FROZEN), | |
385 | msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT)); | |
386 | if (dd->flags & HFI1_FROZEN) { | |
387 | ret = -ENOLCK; | |
388 | break; | |
389 | } | |
390 | if (dd->flags & HFI1_FORCED_FREEZE) { | |
4d114fdd JJ |
391 | /* |
392 | * Don't allow context reset if we are into | |
393 | * forced freeze | |
394 | */ | |
77241056 MM |
395 | ret = -ENODEV; |
396 | break; | |
397 | } | |
398 | sc_disable(sc); | |
399 | ret = sc_enable(sc); | |
400 | hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB, | |
401 | uctxt->ctxt); | |
e490974e | 402 | } else { |
77241056 | 403 | ret = sc_restart(sc); |
e490974e | 404 | } |
77241056 MM |
405 | if (!ret) |
406 | sc_return_credits(sc); | |
407 | break; | |
408 | } | |
409 | case HFI1_CMD_EP_INFO: | |
410 | case HFI1_CMD_EP_ERASE_CHIP: | |
cd371e09 DL |
411 | case HFI1_CMD_EP_ERASE_RANGE: |
412 | case HFI1_CMD_EP_READ_RANGE: | |
413 | case HFI1_CMD_EP_WRITE_RANGE: | |
d24bc648 | 414 | ret = handle_eprom_command(fp, &cmd); |
77241056 MM |
415 | break; |
416 | } | |
417 | ||
418 | if (ret >= 0) | |
419 | ret = consumed; | |
420 | bail: | |
421 | return ret; | |
422 | } | |
423 | ||
424 | static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from) | |
425 | { | |
9e10af47 IW |
426 | struct hfi1_filedata *fd = kiocb->ki_filp->private_data; |
427 | struct hfi1_user_sdma_pkt_q *pq = fd->pq; | |
428 | struct hfi1_user_sdma_comp_q *cq = fd->cq; | |
77241056 MM |
429 | int ret = 0, done = 0, reqs = 0; |
430 | unsigned long dim = from->nr_segs; | |
431 | ||
9e10af47 | 432 | if (!cq || !pq) { |
77241056 MM |
433 | ret = -EIO; |
434 | goto done; | |
435 | } | |
436 | ||
437 | if (!iter_is_iovec(from) || !dim) { | |
438 | ret = -EINVAL; | |
439 | goto done; | |
440 | } | |
441 | ||
442 | hfi1_cdbg(SDMA, "SDMA request from %u:%u (%lu)", | |
9e10af47 | 443 | fd->uctxt->ctxt, fd->subctxt, dim); |
77241056 MM |
444 | |
445 | if (atomic_read(&pq->n_reqs) == pq->n_max_reqs) { | |
446 | ret = -ENOSPC; | |
447 | goto done; | |
448 | } | |
449 | ||
450 | while (dim) { | |
451 | unsigned long count = 0; | |
452 | ||
453 | ret = hfi1_user_sdma_process_request( | |
454 | kiocb->ki_filp, (struct iovec *)(from->iov + done), | |
455 | dim, &count); | |
456 | if (ret) | |
457 | goto done; | |
458 | dim -= count; | |
459 | done += count; | |
460 | reqs++; | |
461 | } | |
462 | done: | |
463 | return ret ? ret : reqs; | |
464 | } | |
465 | ||
466 | static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma) | |
467 | { | |
9e10af47 IW |
468 | struct hfi1_filedata *fd = fp->private_data; |
469 | struct hfi1_ctxtdata *uctxt = fd->uctxt; | |
77241056 MM |
470 | struct hfi1_devdata *dd; |
471 | unsigned long flags, pfn; | |
472 | u64 token = vma->vm_pgoff << PAGE_SHIFT, | |
473 | memaddr = 0; | |
474 | u8 subctxt, mapio = 0, vmf = 0, type; | |
475 | ssize_t memlen = 0; | |
476 | int ret = 0; | |
477 | u16 ctxt; | |
478 | ||
77241056 MM |
479 | if (!is_valid_mmap(token) || !uctxt || |
480 | !(vma->vm_flags & VM_SHARED)) { | |
481 | ret = -EINVAL; | |
482 | goto done; | |
483 | } | |
484 | dd = uctxt->dd; | |
485 | ctxt = HFI1_MMAP_TOKEN_GET(CTXT, token); | |
486 | subctxt = HFI1_MMAP_TOKEN_GET(SUBCTXT, token); | |
487 | type = HFI1_MMAP_TOKEN_GET(TYPE, token); | |
9e10af47 | 488 | if (ctxt != uctxt->ctxt || subctxt != fd->subctxt) { |
77241056 MM |
489 | ret = -EINVAL; |
490 | goto done; | |
491 | } | |
492 | ||
493 | flags = vma->vm_flags; | |
494 | ||
495 | switch (type) { | |
496 | case PIO_BUFS: | |
497 | case PIO_BUFS_SOP: | |
498 | memaddr = ((dd->physaddr + TXE_PIO_SEND) + | |
499 | /* chip pio base */ | |
d32cf44a | 500 | (uctxt->sc->hw_context * BIT(16))) + |
77241056 MM |
501 | /* 64K PIO space / ctxt */ |
502 | (type == PIO_BUFS_SOP ? | |
503 | (TXE_PIO_SIZE / 2) : 0); /* sop? */ | |
504 | /* | |
505 | * Map only the amount allocated to the context, not the | |
506 | * entire available context's PIO space. | |
507 | */ | |
508 | memlen = ALIGN(uctxt->sc->credits * PIO_BLOCK_SIZE, | |
509 | PAGE_SIZE); | |
510 | flags &= ~VM_MAYREAD; | |
511 | flags |= VM_DONTCOPY | VM_DONTEXPAND; | |
512 | vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); | |
513 | mapio = 1; | |
514 | break; | |
515 | case PIO_CRED: | |
516 | if (flags & VM_WRITE) { | |
517 | ret = -EPERM; | |
518 | goto done; | |
519 | } | |
520 | /* | |
521 | * The credit return location for this context could be on the | |
522 | * second or third page allocated for credit returns (if number | |
523 | * of enabled contexts > 64 and 128 respectively). | |
524 | */ | |
525 | memaddr = dd->cr_base[uctxt->numa_id].pa + | |
526 | (((u64)uctxt->sc->hw_free - | |
527 | (u64)dd->cr_base[uctxt->numa_id].va) & PAGE_MASK); | |
528 | memlen = PAGE_SIZE; | |
529 | flags &= ~VM_MAYWRITE; | |
530 | flags |= VM_DONTCOPY | VM_DONTEXPAND; | |
531 | /* | |
532 | * The driver has already allocated memory for credit | |
533 | * returns and programmed it into the chip. Has that | |
534 | * memory been flagged as non-cached? | |
535 | */ | |
536 | /* vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); */ | |
537 | mapio = 1; | |
538 | break; | |
539 | case RCV_HDRQ: | |
540 | memaddr = uctxt->rcvhdrq_phys; | |
541 | memlen = uctxt->rcvhdrq_size; | |
542 | break; | |
543 | case RCV_EGRBUF: { | |
544 | unsigned long addr; | |
545 | int i; | |
546 | /* | |
547 | * The RcvEgr buffer need to be handled differently | |
548 | * as multiple non-contiguous pages need to be mapped | |
549 | * into the user process. | |
550 | */ | |
551 | memlen = uctxt->egrbufs.size; | |
552 | if ((vma->vm_end - vma->vm_start) != memlen) { | |
553 | dd_dev_err(dd, "Eager buffer map size invalid (%lu != %lu)\n", | |
554 | (vma->vm_end - vma->vm_start), memlen); | |
555 | ret = -EINVAL; | |
556 | goto done; | |
557 | } | |
558 | if (vma->vm_flags & VM_WRITE) { | |
559 | ret = -EPERM; | |
560 | goto done; | |
561 | } | |
562 | vma->vm_flags &= ~VM_MAYWRITE; | |
563 | addr = vma->vm_start; | |
564 | for (i = 0 ; i < uctxt->egrbufs.numbufs; i++) { | |
565 | ret = remap_pfn_range( | |
566 | vma, addr, | |
567 | uctxt->egrbufs.buffers[i].phys >> PAGE_SHIFT, | |
568 | uctxt->egrbufs.buffers[i].len, | |
569 | vma->vm_page_prot); | |
570 | if (ret < 0) | |
571 | goto done; | |
572 | addr += uctxt->egrbufs.buffers[i].len; | |
573 | } | |
574 | ret = 0; | |
575 | goto done; | |
576 | } | |
577 | case UREGS: | |
578 | /* | |
579 | * Map only the page that contains this context's user | |
580 | * registers. | |
581 | */ | |
582 | memaddr = (unsigned long) | |
583 | (dd->physaddr + RXE_PER_CONTEXT_USER) | |
584 | + (uctxt->ctxt * RXE_PER_CONTEXT_SIZE); | |
585 | /* | |
586 | * TidFlow table is on the same page as the rest of the | |
587 | * user registers. | |
588 | */ | |
589 | memlen = PAGE_SIZE; | |
590 | flags |= VM_DONTCOPY | VM_DONTEXPAND; | |
591 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); | |
592 | mapio = 1; | |
593 | break; | |
594 | case EVENTS: | |
595 | /* | |
596 | * Use the page where this context's flags are. User level | |
597 | * knows where it's own bitmap is within the page. | |
598 | */ | |
3c6c065a MH |
599 | memaddr = (unsigned long)(dd->events + |
600 | ((uctxt->ctxt - dd->first_user_ctxt) * | |
601 | HFI1_MAX_SHARED_CTXTS)) & PAGE_MASK; | |
77241056 MM |
602 | memlen = PAGE_SIZE; |
603 | /* | |
604 | * v3.7 removes VM_RESERVED but the effect is kept by | |
605 | * using VM_IO. | |
606 | */ | |
607 | flags |= VM_IO | VM_DONTEXPAND; | |
608 | vmf = 1; | |
609 | break; | |
610 | case STATUS: | |
611 | memaddr = kvirt_to_phys((void *)dd->status); | |
612 | memlen = PAGE_SIZE; | |
613 | flags |= VM_IO | VM_DONTEXPAND; | |
614 | break; | |
615 | case RTAIL: | |
616 | if (!HFI1_CAP_IS_USET(DMA_RTAIL)) { | |
617 | /* | |
618 | * If the memory allocation failed, the context alloc | |
619 | * also would have failed, so we would never get here | |
620 | */ | |
621 | ret = -EINVAL; | |
622 | goto done; | |
623 | } | |
624 | if (flags & VM_WRITE) { | |
625 | ret = -EPERM; | |
626 | goto done; | |
627 | } | |
628 | memaddr = uctxt->rcvhdrqtailaddr_phys; | |
629 | memlen = PAGE_SIZE; | |
630 | flags &= ~VM_MAYWRITE; | |
631 | break; | |
632 | case SUBCTXT_UREGS: | |
633 | memaddr = (u64)uctxt->subctxt_uregbase; | |
634 | memlen = PAGE_SIZE; | |
635 | flags |= VM_IO | VM_DONTEXPAND; | |
636 | vmf = 1; | |
637 | break; | |
638 | case SUBCTXT_RCV_HDRQ: | |
639 | memaddr = (u64)uctxt->subctxt_rcvhdr_base; | |
640 | memlen = uctxt->rcvhdrq_size * uctxt->subctxt_cnt; | |
641 | flags |= VM_IO | VM_DONTEXPAND; | |
642 | vmf = 1; | |
643 | break; | |
644 | case SUBCTXT_EGRBUF: | |
645 | memaddr = (u64)uctxt->subctxt_rcvegrbuf; | |
646 | memlen = uctxt->egrbufs.size * uctxt->subctxt_cnt; | |
647 | flags |= VM_IO | VM_DONTEXPAND; | |
648 | flags &= ~VM_MAYWRITE; | |
649 | vmf = 1; | |
650 | break; | |
651 | case SDMA_COMP: { | |
9e10af47 | 652 | struct hfi1_user_sdma_comp_q *cq = fd->cq; |
77241056 | 653 | |
9e10af47 | 654 | if (!cq) { |
77241056 MM |
655 | ret = -EFAULT; |
656 | goto done; | |
657 | } | |
77241056 MM |
658 | memaddr = (u64)cq->comps; |
659 | memlen = ALIGN(sizeof(*cq->comps) * cq->nentries, PAGE_SIZE); | |
660 | flags |= VM_IO | VM_DONTEXPAND; | |
661 | vmf = 1; | |
662 | break; | |
663 | } | |
664 | default: | |
665 | ret = -EINVAL; | |
666 | break; | |
667 | } | |
668 | ||
669 | if ((vma->vm_end - vma->vm_start) != memlen) { | |
670 | hfi1_cdbg(PROC, "%u:%u Memory size mismatch %lu:%lu", | |
9e10af47 | 671 | uctxt->ctxt, fd->subctxt, |
77241056 MM |
672 | (vma->vm_end - vma->vm_start), memlen); |
673 | ret = -EINVAL; | |
674 | goto done; | |
675 | } | |
676 | ||
677 | vma->vm_flags = flags; | |
6c63e423 SS |
678 | hfi1_cdbg(PROC, |
679 | "%u:%u type:%u io/vf:%d/%d, addr:0x%llx, len:%lu(%lu), flags:0x%lx\n", | |
680 | ctxt, subctxt, type, mapio, vmf, memaddr, memlen, | |
77241056 MM |
681 | vma->vm_end - vma->vm_start, vma->vm_flags); |
682 | pfn = (unsigned long)(memaddr >> PAGE_SHIFT); | |
683 | if (vmf) { | |
684 | vma->vm_pgoff = pfn; | |
685 | vma->vm_ops = &vm_ops; | |
686 | ret = 0; | |
687 | } else if (mapio) { | |
688 | ret = io_remap_pfn_range(vma, vma->vm_start, pfn, memlen, | |
689 | vma->vm_page_prot); | |
690 | } else { | |
691 | ret = remap_pfn_range(vma, vma->vm_start, pfn, memlen, | |
692 | vma->vm_page_prot); | |
693 | } | |
694 | done: | |
695 | return ret; | |
696 | } | |
697 | ||
698 | /* | |
699 | * Local (non-chip) user memory is not mapped right away but as it is | |
700 | * accessed by the user-level code. | |
701 | */ | |
702 | static int vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf) | |
703 | { | |
704 | struct page *page; | |
705 | ||
706 | page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT)); | |
707 | if (!page) | |
708 | return VM_FAULT_SIGBUS; | |
709 | ||
710 | get_page(page); | |
711 | vmf->page = page; | |
712 | ||
713 | return 0; | |
714 | } | |
715 | ||
716 | static unsigned int hfi1_poll(struct file *fp, struct poll_table_struct *pt) | |
717 | { | |
718 | struct hfi1_ctxtdata *uctxt; | |
719 | unsigned pollflag; | |
720 | ||
9e10af47 | 721 | uctxt = ((struct hfi1_filedata *)fp->private_data)->uctxt; |
77241056 MM |
722 | if (!uctxt) |
723 | pollflag = POLLERR; | |
724 | else if (uctxt->poll_type == HFI1_POLL_TYPE_URGENT) | |
725 | pollflag = poll_urgent(fp, pt); | |
726 | else if (uctxt->poll_type == HFI1_POLL_TYPE_ANYRCV) | |
727 | pollflag = poll_next(fp, pt); | |
728 | else /* invalid */ | |
729 | pollflag = POLLERR; | |
730 | ||
731 | return pollflag; | |
732 | } | |
733 | ||
734 | static int hfi1_file_close(struct inode *inode, struct file *fp) | |
735 | { | |
736 | struct hfi1_filedata *fdata = fp->private_data; | |
737 | struct hfi1_ctxtdata *uctxt = fdata->uctxt; | |
738 | struct hfi1_devdata *dd; | |
739 | unsigned long flags, *ev; | |
740 | ||
741 | fp->private_data = NULL; | |
742 | ||
743 | if (!uctxt) | |
744 | goto done; | |
745 | ||
746 | hfi1_cdbg(PROC, "freeing ctxt %u:%u", uctxt->ctxt, fdata->subctxt); | |
747 | dd = uctxt->dd; | |
748 | mutex_lock(&hfi1_mutex); | |
749 | ||
750 | flush_wc(); | |
751 | /* drain user sdma queue */ | |
483119a7 | 752 | hfi1_user_sdma_free_queues(fdata); |
77241056 | 753 | |
957558c9 MH |
754 | /* release the cpu */ |
755 | hfi1_put_proc_affinity(dd, fdata->rec_cpu_num); | |
756 | ||
77241056 MM |
757 | /* |
758 | * Clear any left over, unhandled events so the next process that | |
759 | * gets this context doesn't get confused. | |
760 | */ | |
761 | ev = dd->events + ((uctxt->ctxt - dd->first_user_ctxt) * | |
762 | HFI1_MAX_SHARED_CTXTS) + fdata->subctxt; | |
763 | *ev = 0; | |
764 | ||
765 | if (--uctxt->cnt) { | |
766 | uctxt->active_slaves &= ~(1 << fdata->subctxt); | |
767 | uctxt->subpid[fdata->subctxt] = 0; | |
768 | mutex_unlock(&hfi1_mutex); | |
769 | goto done; | |
770 | } | |
771 | ||
772 | spin_lock_irqsave(&dd->uctxt_lock, flags); | |
773 | /* | |
774 | * Disable receive context and interrupt available, reset all | |
775 | * RcvCtxtCtrl bits to default values. | |
776 | */ | |
777 | hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS | | |
778 | HFI1_RCVCTRL_TIDFLOW_DIS | | |
779 | HFI1_RCVCTRL_INTRAVAIL_DIS | | |
566c157c | 780 | HFI1_RCVCTRL_TAILUPD_DIS | |
77241056 MM |
781 | HFI1_RCVCTRL_ONE_PKT_EGR_DIS | |
782 | HFI1_RCVCTRL_NO_RHQ_DROP_DIS | | |
783 | HFI1_RCVCTRL_NO_EGR_DROP_DIS, uctxt->ctxt); | |
784 | /* Clear the context's J_KEY */ | |
785 | hfi1_clear_ctxt_jkey(dd, uctxt->ctxt); | |
786 | /* | |
787 | * Reset context integrity checks to default. | |
788 | * (writes to CSRs probably belong in chip.c) | |
789 | */ | |
790 | write_kctxt_csr(dd, uctxt->sc->hw_context, SEND_CTXT_CHECK_ENABLE, | |
791 | hfi1_pkt_default_send_ctxt_mask(dd, uctxt->sc->type)); | |
792 | sc_disable(uctxt->sc); | |
793 | uctxt->pid = 0; | |
794 | spin_unlock_irqrestore(&dd->uctxt_lock, flags); | |
795 | ||
796 | dd->rcd[uctxt->ctxt] = NULL; | |
797 | uctxt->rcvwait_to = 0; | |
798 | uctxt->piowait_to = 0; | |
799 | uctxt->rcvnowait = 0; | |
800 | uctxt->pionowait = 0; | |
801 | uctxt->event_flags = 0; | |
802 | ||
0b091fb3 | 803 | hfi1_user_exp_rcv_free(fdata); |
77241056 MM |
804 | hfi1_clear_ctxt_pkey(dd, uctxt->ctxt); |
805 | ||
77241056 | 806 | hfi1_stats.sps_ctxts--; |
affa48de AD |
807 | if (++dd->freectxts == dd->num_user_contexts) |
808 | aspm_enable_all(dd); | |
77241056 MM |
809 | mutex_unlock(&hfi1_mutex); |
810 | hfi1_free_ctxtdata(dd, uctxt); | |
811 | done: | |
812 | kfree(fdata); | |
813 | return 0; | |
814 | } | |
815 | ||
816 | /* | |
817 | * Convert kernel *virtual* addresses to physical addresses. | |
818 | * This is used to vmalloc'ed addresses. | |
819 | */ | |
820 | static u64 kvirt_to_phys(void *addr) | |
821 | { | |
822 | struct page *page; | |
823 | u64 paddr = 0; | |
824 | ||
825 | page = vmalloc_to_page(addr); | |
826 | if (page) | |
827 | paddr = page_to_pfn(page) << PAGE_SHIFT; | |
828 | ||
829 | return paddr; | |
830 | } | |
831 | ||
832 | static int assign_ctxt(struct file *fp, struct hfi1_user_info *uinfo) | |
833 | { | |
834 | int i_minor, ret = 0; | |
835 | unsigned swmajor, swminor, alg = HFI1_ALG_ACROSS; | |
836 | ||
837 | swmajor = uinfo->userversion >> 16; | |
838 | if (swmajor != HFI1_USER_SWMAJOR) { | |
839 | ret = -ENODEV; | |
840 | goto done; | |
841 | } | |
842 | ||
843 | swminor = uinfo->userversion & 0xffff; | |
844 | ||
845 | if (uinfo->hfi1_alg < HFI1_ALG_COUNT) | |
846 | alg = uinfo->hfi1_alg; | |
847 | ||
848 | mutex_lock(&hfi1_mutex); | |
849 | /* First, lets check if we need to setup a shared context? */ | |
957558c9 MH |
850 | if (uinfo->subctxt_cnt) { |
851 | struct hfi1_filedata *fd = fp->private_data; | |
852 | ||
77241056 | 853 | ret = find_shared_ctxt(fp, uinfo); |
957558c9 MH |
854 | if (ret < 0) |
855 | goto done_unlock; | |
856 | if (ret) | |
857 | fd->rec_cpu_num = hfi1_get_proc_affinity( | |
858 | fd->uctxt->dd, fd->uctxt->numa_id); | |
859 | } | |
77241056 MM |
860 | |
861 | /* | |
862 | * We execute the following block if we couldn't find a | |
863 | * shared context or if context sharing is not required. | |
864 | */ | |
865 | if (!ret) { | |
866 | i_minor = iminor(file_inode(fp)) - HFI1_USER_MINOR_BASE; | |
867 | ret = get_user_context(fp, uinfo, i_minor - 1, alg); | |
868 | } | |
957558c9 | 869 | done_unlock: |
77241056 MM |
870 | mutex_unlock(&hfi1_mutex); |
871 | done: | |
872 | return ret; | |
873 | } | |
874 | ||
70224973 DL |
875 | /* return true if the device available for general use */ |
876 | static int usable_device(struct hfi1_devdata *dd) | |
877 | { | |
878 | struct hfi1_pportdata *ppd = dd->pport; | |
879 | ||
880 | return driver_lstate(ppd) == IB_PORT_ACTIVE; | |
881 | } | |
882 | ||
77241056 MM |
883 | static int get_user_context(struct file *fp, struct hfi1_user_info *uinfo, |
884 | int devno, unsigned alg) | |
885 | { | |
886 | struct hfi1_devdata *dd = NULL; | |
887 | int ret = 0, devmax, npresent, nup, dev; | |
888 | ||
889 | devmax = hfi1_count_units(&npresent, &nup); | |
890 | if (!npresent) { | |
891 | ret = -ENXIO; | |
892 | goto done; | |
893 | } | |
894 | if (!nup) { | |
895 | ret = -ENETDOWN; | |
896 | goto done; | |
897 | } | |
898 | if (devno >= 0) { | |
899 | dd = hfi1_lookup(devno); | |
900 | if (!dd) | |
901 | ret = -ENODEV; | |
902 | else if (!dd->freectxts) | |
903 | ret = -EBUSY; | |
904 | } else { | |
905 | struct hfi1_devdata *pdd; | |
906 | ||
907 | if (alg == HFI1_ALG_ACROSS) { | |
908 | unsigned free = 0U; | |
909 | ||
910 | for (dev = 0; dev < devmax; dev++) { | |
911 | pdd = hfi1_lookup(dev); | |
70224973 DL |
912 | if (!pdd) |
913 | continue; | |
914 | if (!usable_device(pdd)) | |
915 | continue; | |
916 | if (pdd->freectxts && | |
77241056 MM |
917 | pdd->freectxts > free) { |
918 | dd = pdd; | |
919 | free = pdd->freectxts; | |
920 | } | |
921 | } | |
922 | } else { | |
923 | for (dev = 0; dev < devmax; dev++) { | |
924 | pdd = hfi1_lookup(dev); | |
70224973 DL |
925 | if (!pdd) |
926 | continue; | |
927 | if (!usable_device(pdd)) | |
928 | continue; | |
929 | if (pdd->freectxts) { | |
77241056 MM |
930 | dd = pdd; |
931 | break; | |
932 | } | |
933 | } | |
934 | } | |
935 | if (!dd) | |
936 | ret = -EBUSY; | |
937 | } | |
938 | done: | |
939 | return ret ? ret : allocate_ctxt(fp, dd, uinfo); | |
940 | } | |
941 | ||
942 | static int find_shared_ctxt(struct file *fp, | |
943 | const struct hfi1_user_info *uinfo) | |
944 | { | |
945 | int devmax, ndev, i; | |
946 | int ret = 0; | |
9e10af47 | 947 | struct hfi1_filedata *fd = fp->private_data; |
77241056 MM |
948 | |
949 | devmax = hfi1_count_units(NULL, NULL); | |
950 | ||
951 | for (ndev = 0; ndev < devmax; ndev++) { | |
952 | struct hfi1_devdata *dd = hfi1_lookup(ndev); | |
953 | ||
77241056 MM |
954 | if (!(dd && (dd->flags & HFI1_PRESENT) && dd->kregbase)) |
955 | continue; | |
956 | for (i = dd->first_user_ctxt; i < dd->num_rcv_contexts; i++) { | |
957 | struct hfi1_ctxtdata *uctxt = dd->rcd[i]; | |
958 | ||
959 | /* Skip ctxts which are not yet open */ | |
960 | if (!uctxt || !uctxt->cnt) | |
961 | continue; | |
962 | /* Skip ctxt if it doesn't match the requested one */ | |
963 | if (memcmp(uctxt->uuid, uinfo->uuid, | |
964 | sizeof(uctxt->uuid)) || | |
07839049 | 965 | uctxt->jkey != generate_jkey(current_uid()) || |
77241056 MM |
966 | uctxt->subctxt_id != uinfo->subctxt_id || |
967 | uctxt->subctxt_cnt != uinfo->subctxt_cnt) | |
968 | continue; | |
969 | ||
970 | /* Verify the sharing process matches the master */ | |
971 | if (uctxt->userversion != uinfo->userversion || | |
972 | uctxt->cnt >= uctxt->subctxt_cnt) { | |
973 | ret = -EINVAL; | |
974 | goto done; | |
975 | } | |
9e10af47 IW |
976 | fd->uctxt = uctxt; |
977 | fd->subctxt = uctxt->cnt++; | |
978 | uctxt->subpid[fd->subctxt] = current->pid; | |
979 | uctxt->active_slaves |= 1 << fd->subctxt; | |
77241056 MM |
980 | ret = 1; |
981 | goto done; | |
982 | } | |
983 | } | |
984 | ||
985 | done: | |
986 | return ret; | |
987 | } | |
988 | ||
989 | static int allocate_ctxt(struct file *fp, struct hfi1_devdata *dd, | |
990 | struct hfi1_user_info *uinfo) | |
991 | { | |
9e10af47 | 992 | struct hfi1_filedata *fd = fp->private_data; |
77241056 MM |
993 | struct hfi1_ctxtdata *uctxt; |
994 | unsigned ctxt; | |
957558c9 | 995 | int ret, numa; |
77241056 MM |
996 | |
997 | if (dd->flags & HFI1_FROZEN) { | |
998 | /* | |
999 | * Pick an error that is unique from all other errors | |
1000 | * that are returned so the user process knows that | |
1001 | * it tried to allocate while the SPC was frozen. It | |
1002 | * it should be able to retry with success in a short | |
1003 | * while. | |
1004 | */ | |
1005 | return -EIO; | |
1006 | } | |
1007 | ||
1008 | for (ctxt = dd->first_user_ctxt; ctxt < dd->num_rcv_contexts; ctxt++) | |
1009 | if (!dd->rcd[ctxt]) | |
1010 | break; | |
1011 | ||
1012 | if (ctxt == dd->num_rcv_contexts) | |
1013 | return -EBUSY; | |
1014 | ||
957558c9 MH |
1015 | fd->rec_cpu_num = hfi1_get_proc_affinity(dd, -1); |
1016 | if (fd->rec_cpu_num != -1) | |
1017 | numa = cpu_to_node(fd->rec_cpu_num); | |
1018 | else | |
1019 | numa = numa_node_id(); | |
1020 | uctxt = hfi1_create_ctxtdata(dd->pport, ctxt, numa); | |
77241056 MM |
1021 | if (!uctxt) { |
1022 | dd_dev_err(dd, | |
1023 | "Unable to allocate ctxtdata memory, failing open\n"); | |
1024 | return -ENOMEM; | |
1025 | } | |
957558c9 MH |
1026 | hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)", |
1027 | uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num, | |
1028 | uctxt->numa_id); | |
1029 | ||
77241056 MM |
1030 | /* |
1031 | * Allocate and enable a PIO send context. | |
1032 | */ | |
1033 | uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize, | |
cc57236f | 1034 | uctxt->dd->node); |
77241056 MM |
1035 | if (!uctxt->sc) |
1036 | return -ENOMEM; | |
1037 | ||
6c63e423 SS |
1038 | hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index, |
1039 | uctxt->sc->hw_context); | |
77241056 MM |
1040 | ret = sc_enable(uctxt->sc); |
1041 | if (ret) | |
1042 | return ret; | |
1043 | /* | |
1044 | * Setup shared context resources if the user-level has requested | |
1045 | * shared contexts and this is the 'master' process. | |
1046 | * This has to be done here so the rest of the sub-contexts find the | |
1047 | * proper master. | |
1048 | */ | |
9e10af47 | 1049 | if (uinfo->subctxt_cnt && !fd->subctxt) { |
77241056 MM |
1050 | ret = init_subctxts(uctxt, uinfo); |
1051 | /* | |
1052 | * On error, we don't need to disable and de-allocate the | |
1053 | * send context because it will be done during file close | |
1054 | */ | |
1055 | if (ret) | |
1056 | return ret; | |
1057 | } | |
1058 | uctxt->userversion = uinfo->userversion; | |
1059 | uctxt->pid = current->pid; | |
1060 | uctxt->flags = HFI1_CAP_UGET(MASK); | |
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()); | |
1065 | INIT_LIST_HEAD(&uctxt->sdma_queues); | |
1066 | spin_lock_init(&uctxt->sdma_qlock); | |
1067 | hfi1_stats.sps_ctxts++; | |
affa48de AD |
1068 | /* |
1069 | * Disable ASPM when there are open user/PSM contexts to avoid | |
1070 | * issues with ASPM L1 exit latency | |
1071 | */ | |
1072 | if (dd->freectxts-- == dd->num_user_contexts) | |
1073 | aspm_disable_all(dd); | |
9e10af47 | 1074 | fd->uctxt = uctxt; |
77241056 MM |
1075 | |
1076 | return 0; | |
1077 | } | |
1078 | ||
1079 | static int init_subctxts(struct hfi1_ctxtdata *uctxt, | |
1080 | const struct hfi1_user_info *uinfo) | |
1081 | { | |
77241056 MM |
1082 | unsigned num_subctxts; |
1083 | ||
1084 | num_subctxts = uinfo->subctxt_cnt; | |
acac10fd MH |
1085 | if (num_subctxts > HFI1_MAX_SHARED_CTXTS) |
1086 | return -EINVAL; | |
77241056 MM |
1087 | |
1088 | uctxt->subctxt_cnt = uinfo->subctxt_cnt; | |
1089 | uctxt->subctxt_id = uinfo->subctxt_id; | |
1090 | uctxt->active_slaves = 1; | |
1091 | uctxt->redirect_seq_cnt = 1; | |
1092 | set_bit(HFI1_CTXT_MASTER_UNINIT, &uctxt->event_flags); | |
acac10fd MH |
1093 | |
1094 | return 0; | |
77241056 MM |
1095 | } |
1096 | ||
1097 | static int setup_subctxt(struct hfi1_ctxtdata *uctxt) | |
1098 | { | |
1099 | int ret = 0; | |
1100 | unsigned num_subctxts = uctxt->subctxt_cnt; | |
1101 | ||
1102 | uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE); | |
1103 | if (!uctxt->subctxt_uregbase) { | |
1104 | ret = -ENOMEM; | |
1105 | goto bail; | |
1106 | } | |
1107 | /* We can take the size of the RcvHdr Queue from the master */ | |
1108 | uctxt->subctxt_rcvhdr_base = vmalloc_user(uctxt->rcvhdrq_size * | |
1109 | num_subctxts); | |
1110 | if (!uctxt->subctxt_rcvhdr_base) { | |
1111 | ret = -ENOMEM; | |
1112 | goto bail_ureg; | |
1113 | } | |
1114 | ||
1115 | uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size * | |
1116 | num_subctxts); | |
1117 | if (!uctxt->subctxt_rcvegrbuf) { | |
1118 | ret = -ENOMEM; | |
1119 | goto bail_rhdr; | |
1120 | } | |
1121 | goto bail; | |
1122 | bail_rhdr: | |
1123 | vfree(uctxt->subctxt_rcvhdr_base); | |
1124 | bail_ureg: | |
1125 | vfree(uctxt->subctxt_uregbase); | |
1126 | uctxt->subctxt_uregbase = NULL; | |
1127 | bail: | |
1128 | return ret; | |
1129 | } | |
1130 | ||
1131 | static int user_init(struct file *fp) | |
1132 | { | |
1133 | int ret; | |
1134 | unsigned int rcvctrl_ops = 0; | |
9e10af47 IW |
1135 | struct hfi1_filedata *fd = fp->private_data; |
1136 | struct hfi1_ctxtdata *uctxt = fd->uctxt; | |
77241056 MM |
1137 | |
1138 | /* make sure that the context has already been setup */ | |
1139 | if (!test_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags)) { | |
1140 | ret = -EFAULT; | |
1141 | goto done; | |
1142 | } | |
1143 | ||
1144 | /* | |
1145 | * Subctxts don't need to initialize anything since master | |
1146 | * has done it. | |
1147 | */ | |
9e10af47 | 1148 | if (fd->subctxt) { |
17fb4f29 JJ |
1149 | ret = wait_event_interruptible(uctxt->wait, !test_bit( |
1150 | HFI1_CTXT_MASTER_UNINIT, | |
1151 | &uctxt->event_flags)); | |
0b091fb3 | 1152 | goto expected; |
77241056 MM |
1153 | } |
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 */ | |
1174 | hfi1_set_ctxt_jkey(uctxt->dd, uctxt->ctxt, uctxt->jkey); | |
1175 | ||
1176 | rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB; | |
1177 | if (HFI1_CAP_KGET_MASK(uctxt->flags, HDRSUPP)) | |
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 | */ | |
1184 | if (!HFI1_CAP_KGET_MASK(uctxt->flags, MULTI_PKT_EGR)) | |
1185 | rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB; | |
1186 | if (HFI1_CAP_KGET_MASK(uctxt->flags, NODROP_EGR_FULL)) | |
1187 | rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB; | |
1188 | if (HFI1_CAP_KGET_MASK(uctxt->flags, NODROP_RHQ_FULL)) | |
1189 | rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB; | |
566c157c MH |
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 | */ | |
77241056 MM |
1196 | if (HFI1_CAP_KGET_MASK(uctxt->flags, DMA_RTAIL)) |
1197 | rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB; | |
566c157c MH |
1198 | else |
1199 | rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS; | |
77241056 MM |
1200 | hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt->ctxt); |
1201 | ||
1202 | /* Notify any waiting slaves */ | |
1203 | if (uctxt->subctxt_cnt) { | |
1204 | clear_bit(HFI1_CTXT_MASTER_UNINIT, &uctxt->event_flags); | |
1205 | wake_up(&uctxt->wait); | |
1206 | } | |
77241056 | 1207 | |
0b091fb3 MH |
1208 | expected: |
1209 | /* | |
1210 | * Expected receive has to be setup for all processes (including | |
1211 | * shared contexts). However, it has to be done after the master | |
1212 | * context has been fully configured as it depends on the | |
1213 | * eager/expected split of the RcvArray entries. | |
1214 | * Setting it up here ensures that the subcontexts will be waiting | |
1215 | * (due to the above wait_event_interruptible() until the master | |
1216 | * is setup. | |
1217 | */ | |
1218 | ret = hfi1_user_exp_rcv_init(fp); | |
77241056 MM |
1219 | done: |
1220 | return ret; | |
1221 | } | |
1222 | ||
1223 | static int get_ctxt_info(struct file *fp, void __user *ubase, __u32 len) | |
1224 | { | |
1225 | struct hfi1_ctxt_info cinfo; | |
77241056 | 1226 | struct hfi1_filedata *fd = fp->private_data; |
9e10af47 | 1227 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
77241056 MM |
1228 | int ret = 0; |
1229 | ||
ebe6b2e8 | 1230 | memset(&cinfo, 0, sizeof(cinfo)); |
77241056 MM |
1231 | ret = hfi1_get_base_kinfo(uctxt, &cinfo); |
1232 | if (ret < 0) | |
1233 | goto done; | |
1234 | cinfo.num_active = hfi1_count_active_units(); | |
1235 | cinfo.unit = uctxt->dd->unit; | |
1236 | cinfo.ctxt = uctxt->ctxt; | |
9e10af47 | 1237 | cinfo.subctxt = fd->subctxt; |
77241056 MM |
1238 | cinfo.rcvtids = roundup(uctxt->egrbufs.alloced, |
1239 | uctxt->dd->rcv_entries.group_size) + | |
1240 | uctxt->expected_count; | |
1241 | cinfo.credits = uctxt->sc->credits; | |
1242 | cinfo.numa_node = uctxt->numa_id; | |
1243 | cinfo.rec_cpu = fd->rec_cpu_num; | |
1244 | cinfo.send_ctxt = uctxt->sc->hw_context; | |
1245 | ||
1246 | cinfo.egrtids = uctxt->egrbufs.alloced; | |
1247 | cinfo.rcvhdrq_cnt = uctxt->rcvhdrq_cnt; | |
1248 | cinfo.rcvhdrq_entsize = uctxt->rcvhdrqentsize << 2; | |
9e10af47 | 1249 | cinfo.sdma_ring_size = fd->cq->nentries; |
77241056 MM |
1250 | cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size; |
1251 | ||
9e10af47 | 1252 | trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, cinfo); |
77241056 MM |
1253 | if (copy_to_user(ubase, &cinfo, sizeof(cinfo))) |
1254 | ret = -EFAULT; | |
1255 | done: | |
1256 | return ret; | |
1257 | } | |
1258 | ||
1259 | static int setup_ctxt(struct file *fp) | |
1260 | { | |
9e10af47 IW |
1261 | struct hfi1_filedata *fd = fp->private_data; |
1262 | struct hfi1_ctxtdata *uctxt = fd->uctxt; | |
77241056 MM |
1263 | struct hfi1_devdata *dd = uctxt->dd; |
1264 | int ret = 0; | |
1265 | ||
1266 | /* | |
1267 | * Context should be set up only once (including allocation and | |
1268 | * programming of eager buffers. This is done if context sharing | |
1269 | * is not requested or by the master process. | |
1270 | */ | |
9e10af47 | 1271 | if (!uctxt->subctxt_cnt || !fd->subctxt) { |
77241056 MM |
1272 | ret = hfi1_init_ctxt(uctxt->sc); |
1273 | if (ret) | |
1274 | goto done; | |
1275 | ||
1276 | /* Now allocate the RcvHdr queue and eager buffers. */ | |
1277 | ret = hfi1_create_rcvhdrq(dd, uctxt); | |
1278 | if (ret) | |
1279 | goto done; | |
1280 | ret = hfi1_setup_eagerbufs(uctxt); | |
1281 | if (ret) | |
1282 | goto done; | |
9e10af47 | 1283 | if (uctxt->subctxt_cnt && !fd->subctxt) { |
77241056 MM |
1284 | ret = setup_subctxt(uctxt); |
1285 | if (ret) | |
1286 | goto done; | |
1287 | } | |
77241056 MM |
1288 | } |
1289 | ret = hfi1_user_sdma_alloc_queues(uctxt, fp); | |
1290 | if (ret) | |
1291 | goto done; | |
1292 | ||
1293 | set_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags); | |
1294 | done: | |
1295 | return ret; | |
1296 | } | |
1297 | ||
1298 | static int get_base_info(struct file *fp, void __user *ubase, __u32 len) | |
1299 | { | |
1300 | struct hfi1_base_info binfo; | |
9e10af47 IW |
1301 | struct hfi1_filedata *fd = fp->private_data; |
1302 | struct hfi1_ctxtdata *uctxt = fd->uctxt; | |
77241056 MM |
1303 | struct hfi1_devdata *dd = uctxt->dd; |
1304 | ssize_t sz; | |
1305 | unsigned offset; | |
1306 | int ret = 0; | |
1307 | ||
1308 | trace_hfi1_uctxtdata(uctxt->dd, uctxt); | |
1309 | ||
1310 | memset(&binfo, 0, sizeof(binfo)); | |
1311 | binfo.hw_version = dd->revision; | |
1312 | binfo.sw_version = HFI1_KERN_SWVERSION; | |
1313 | binfo.bthqp = kdeth_qp; | |
1314 | binfo.jkey = uctxt->jkey; | |
1315 | /* | |
1316 | * If more than 64 contexts are enabled the allocated credit | |
1317 | * return will span two or three contiguous pages. Since we only | |
1318 | * map the page containing the context's credit return address, | |
1319 | * we need to calculate the offset in the proper page. | |
1320 | */ | |
1321 | offset = ((u64)uctxt->sc->hw_free - | |
1322 | (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE; | |
1323 | binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt, | |
9e10af47 | 1324 | fd->subctxt, offset); |
77241056 | 1325 | binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt, |
9e10af47 | 1326 | fd->subctxt, |
77241056 MM |
1327 | uctxt->sc->base_addr); |
1328 | binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP, | |
1329 | uctxt->ctxt, | |
9e10af47 | 1330 | fd->subctxt, |
77241056 MM |
1331 | uctxt->sc->base_addr); |
1332 | binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt, | |
9e10af47 | 1333 | fd->subctxt, |
77241056 MM |
1334 | uctxt->rcvhdrq); |
1335 | binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt, | |
9e10af47 | 1336 | fd->subctxt, |
77241056 MM |
1337 | uctxt->egrbufs.rcvtids[0].phys); |
1338 | binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt, | |
9e10af47 | 1339 | fd->subctxt, 0); |
77241056 MM |
1340 | /* |
1341 | * user regs are at | |
1342 | * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE)) | |
1343 | */ | |
1344 | binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt, | |
9e10af47 | 1345 | fd->subctxt, 0); |
e260e404 | 1346 | offset = offset_in_page((((uctxt->ctxt - dd->first_user_ctxt) * |
9e10af47 | 1347 | HFI1_MAX_SHARED_CTXTS) + fd->subctxt) * |
e260e404 | 1348 | sizeof(*dd->events)); |
77241056 | 1349 | binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt, |
9e10af47 | 1350 | fd->subctxt, |
77241056 MM |
1351 | offset); |
1352 | binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt, | |
9e10af47 | 1353 | fd->subctxt, |
77241056 MM |
1354 | dd->status); |
1355 | if (HFI1_CAP_IS_USET(DMA_RTAIL)) | |
1356 | binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt, | |
9e10af47 | 1357 | fd->subctxt, 0); |
77241056 MM |
1358 | if (uctxt->subctxt_cnt) { |
1359 | binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS, | |
1360 | uctxt->ctxt, | |
9e10af47 | 1361 | fd->subctxt, 0); |
77241056 MM |
1362 | binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ, |
1363 | uctxt->ctxt, | |
9e10af47 | 1364 | fd->subctxt, 0); |
77241056 MM |
1365 | binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF, |
1366 | uctxt->ctxt, | |
9e10af47 | 1367 | fd->subctxt, 0); |
77241056 MM |
1368 | } |
1369 | sz = (len < sizeof(binfo)) ? len : sizeof(binfo); | |
1370 | if (copy_to_user(ubase, &binfo, sz)) | |
1371 | ret = -EFAULT; | |
1372 | return ret; | |
1373 | } | |
1374 | ||
1375 | static unsigned int poll_urgent(struct file *fp, | |
1376 | struct poll_table_struct *pt) | |
1377 | { | |
9e10af47 IW |
1378 | struct hfi1_filedata *fd = fp->private_data; |
1379 | struct hfi1_ctxtdata *uctxt = fd->uctxt; | |
77241056 MM |
1380 | struct hfi1_devdata *dd = uctxt->dd; |
1381 | unsigned pollflag; | |
1382 | ||
1383 | poll_wait(fp, &uctxt->wait, pt); | |
1384 | ||
1385 | spin_lock_irq(&dd->uctxt_lock); | |
1386 | if (uctxt->urgent != uctxt->urgent_poll) { | |
1387 | pollflag = POLLIN | POLLRDNORM; | |
1388 | uctxt->urgent_poll = uctxt->urgent; | |
1389 | } else { | |
1390 | pollflag = 0; | |
1391 | set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags); | |
1392 | } | |
1393 | spin_unlock_irq(&dd->uctxt_lock); | |
1394 | ||
1395 | return pollflag; | |
1396 | } | |
1397 | ||
1398 | static unsigned int poll_next(struct file *fp, | |
1399 | struct poll_table_struct *pt) | |
1400 | { | |
9e10af47 IW |
1401 | struct hfi1_filedata *fd = fp->private_data; |
1402 | struct hfi1_ctxtdata *uctxt = fd->uctxt; | |
77241056 MM |
1403 | struct hfi1_devdata *dd = uctxt->dd; |
1404 | unsigned pollflag; | |
1405 | ||
1406 | poll_wait(fp, &uctxt->wait, pt); | |
1407 | ||
1408 | spin_lock_irq(&dd->uctxt_lock); | |
1409 | if (hdrqempty(uctxt)) { | |
1410 | set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags); | |
1411 | hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt->ctxt); | |
1412 | pollflag = 0; | |
e490974e | 1413 | } else { |
77241056 | 1414 | pollflag = POLLIN | POLLRDNORM; |
e490974e | 1415 | } |
77241056 MM |
1416 | spin_unlock_irq(&dd->uctxt_lock); |
1417 | ||
1418 | return pollflag; | |
1419 | } | |
1420 | ||
1421 | /* | |
1422 | * Find all user contexts in use, and set the specified bit in their | |
1423 | * event mask. | |
1424 | * See also find_ctxt() for a similar use, that is specific to send buffers. | |
1425 | */ | |
1426 | int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit) | |
1427 | { | |
1428 | struct hfi1_ctxtdata *uctxt; | |
1429 | struct hfi1_devdata *dd = ppd->dd; | |
1430 | unsigned ctxt; | |
1431 | int ret = 0; | |
1432 | unsigned long flags; | |
1433 | ||
1434 | if (!dd->events) { | |
1435 | ret = -EINVAL; | |
1436 | goto done; | |
1437 | } | |
1438 | ||
1439 | spin_lock_irqsave(&dd->uctxt_lock, flags); | |
1440 | for (ctxt = dd->first_user_ctxt; ctxt < dd->num_rcv_contexts; | |
1441 | ctxt++) { | |
1442 | uctxt = dd->rcd[ctxt]; | |
1443 | if (uctxt) { | |
1444 | unsigned long *evs = dd->events + | |
1445 | (uctxt->ctxt - dd->first_user_ctxt) * | |
1446 | HFI1_MAX_SHARED_CTXTS; | |
1447 | int i; | |
1448 | /* | |
1449 | * subctxt_cnt is 0 if not shared, so do base | |
1450 | * separately, first, then remaining subctxt, if any | |
1451 | */ | |
1452 | set_bit(evtbit, evs); | |
1453 | for (i = 1; i < uctxt->subctxt_cnt; i++) | |
1454 | set_bit(evtbit, evs + i); | |
1455 | } | |
1456 | } | |
1457 | spin_unlock_irqrestore(&dd->uctxt_lock, flags); | |
1458 | done: | |
1459 | return ret; | |
1460 | } | |
1461 | ||
1462 | /** | |
1463 | * manage_rcvq - manage a context's receive queue | |
1464 | * @uctxt: the context | |
1465 | * @subctxt: the sub-context | |
1466 | * @start_stop: action to carry out | |
1467 | * | |
1468 | * start_stop == 0 disables receive on the context, for use in queue | |
1469 | * overflow conditions. start_stop==1 re-enables, to be used to | |
1470 | * re-init the software copy of the head register | |
1471 | */ | |
1472 | static int manage_rcvq(struct hfi1_ctxtdata *uctxt, unsigned subctxt, | |
1473 | int start_stop) | |
1474 | { | |
1475 | struct hfi1_devdata *dd = uctxt->dd; | |
1476 | unsigned int rcvctrl_op; | |
1477 | ||
1478 | if (subctxt) | |
1479 | goto bail; | |
1480 | /* atomically clear receive enable ctxt. */ | |
1481 | if (start_stop) { | |
1482 | /* | |
1483 | * On enable, force in-memory copy of the tail register to | |
1484 | * 0, so that protocol code doesn't have to worry about | |
1485 | * whether or not the chip has yet updated the in-memory | |
1486 | * copy or not on return from the system call. The chip | |
1487 | * always resets it's tail register back to 0 on a | |
1488 | * transition from disabled to enabled. | |
1489 | */ | |
1490 | if (uctxt->rcvhdrtail_kvaddr) | |
1491 | clear_rcvhdrtail(uctxt); | |
1492 | rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB; | |
e490974e | 1493 | } else { |
77241056 | 1494 | rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS; |
e490974e | 1495 | } |
77241056 MM |
1496 | hfi1_rcvctrl(dd, rcvctrl_op, uctxt->ctxt); |
1497 | /* always; new head should be equal to new tail; see above */ | |
1498 | bail: | |
1499 | return 0; | |
1500 | } | |
1501 | ||
1502 | /* | |
1503 | * clear the event notifier events for this context. | |
1504 | * User process then performs actions appropriate to bit having been | |
1505 | * set, if desired, and checks again in future. | |
1506 | */ | |
1507 | static int user_event_ack(struct hfi1_ctxtdata *uctxt, int subctxt, | |
1508 | unsigned long events) | |
1509 | { | |
1510 | int i; | |
1511 | struct hfi1_devdata *dd = uctxt->dd; | |
1512 | unsigned long *evs; | |
1513 | ||
1514 | if (!dd->events) | |
1515 | return 0; | |
1516 | ||
1517 | evs = dd->events + ((uctxt->ctxt - dd->first_user_ctxt) * | |
1518 | HFI1_MAX_SHARED_CTXTS) + subctxt; | |
1519 | ||
1520 | for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) { | |
1521 | if (!test_bit(i, &events)) | |
1522 | continue; | |
1523 | clear_bit(i, evs); | |
1524 | } | |
1525 | return 0; | |
1526 | } | |
1527 | ||
77241056 MM |
1528 | static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned subctxt, |
1529 | u16 pkey) | |
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) | |
1547 | ret = hfi1_set_ctxt_pkey(dd, uctxt->ctxt, pkey); | |
1548 | done: | |
1549 | return ret; | |
1550 | } | |
1551 | ||
1552 | static int ui_open(struct inode *inode, struct file *filp) | |
1553 | { | |
1554 | struct hfi1_devdata *dd; | |
1555 | ||
1556 | dd = container_of(inode->i_cdev, struct hfi1_devdata, ui_cdev); | |
1557 | filp->private_data = dd; /* for other methods */ | |
1558 | return 0; | |
1559 | } | |
1560 | ||
1561 | static int ui_release(struct inode *inode, struct file *filp) | |
1562 | { | |
1563 | /* nothing to do */ | |
1564 | return 0; | |
1565 | } | |
1566 | ||
1567 | static loff_t ui_lseek(struct file *filp, loff_t offset, int whence) | |
1568 | { | |
1569 | struct hfi1_devdata *dd = filp->private_data; | |
1570 | ||
1571 | switch (whence) { | |
1572 | case SEEK_SET: | |
1573 | break; | |
1574 | case SEEK_CUR: | |
1575 | offset += filp->f_pos; | |
1576 | break; | |
1577 | case SEEK_END: | |
1578 | offset = ((dd->kregend - dd->kregbase) + DC8051_DATA_MEM_SIZE) - | |
1579 | offset; | |
1580 | break; | |
1581 | default: | |
1582 | return -EINVAL; | |
1583 | } | |
1584 | ||
1585 | if (offset < 0) | |
1586 | return -EINVAL; | |
1587 | ||
1588 | if (offset >= (dd->kregend - dd->kregbase) + DC8051_DATA_MEM_SIZE) | |
1589 | return -EINVAL; | |
1590 | ||
1591 | filp->f_pos = offset; | |
1592 | ||
1593 | return filp->f_pos; | |
1594 | } | |
1595 | ||
77241056 MM |
1596 | /* NOTE: assumes unsigned long is 8 bytes */ |
1597 | static ssize_t ui_read(struct file *filp, char __user *buf, size_t count, | |
17fb4f29 | 1598 | loff_t *f_pos) |
77241056 MM |
1599 | { |
1600 | struct hfi1_devdata *dd = filp->private_data; | |
1601 | void __iomem *base = dd->kregbase; | |
1602 | unsigned long total, csr_off, | |
1603 | barlen = (dd->kregend - dd->kregbase); | |
1604 | u64 data; | |
1605 | ||
1606 | /* only read 8 byte quantities */ | |
1607 | if ((count % 8) != 0) | |
1608 | return -EINVAL; | |
1609 | /* offset must be 8-byte aligned */ | |
1610 | if ((*f_pos % 8) != 0) | |
1611 | return -EINVAL; | |
1612 | /* destination buffer must be 8-byte aligned */ | |
1613 | if ((unsigned long)buf % 8 != 0) | |
1614 | return -EINVAL; | |
1615 | /* must be in range */ | |
1616 | if (*f_pos + count > (barlen + DC8051_DATA_MEM_SIZE)) | |
1617 | return -EINVAL; | |
1618 | /* only set the base if we are not starting past the BAR */ | |
1619 | if (*f_pos < barlen) | |
1620 | base += *f_pos; | |
1621 | csr_off = *f_pos; | |
1622 | for (total = 0; total < count; total += 8, csr_off += 8) { | |
1623 | /* accessing LCB CSRs requires more checks */ | |
1624 | if (is_lcb_offset(csr_off)) { | |
1625 | if (read_lcb_csr(dd, csr_off, (u64 *)&data)) | |
1626 | break; /* failed */ | |
1627 | } | |
1628 | /* | |
1629 | * Cannot read ASIC GPIO/QSFP* clear and force CSRs without a | |
1630 | * false parity error. Avoid the whole issue by not reading | |
1631 | * them. These registers are defined as having a read value | |
1632 | * of 0. | |
1633 | */ | |
d0d236ea JJ |
1634 | else if (csr_off == ASIC_GPIO_CLEAR || |
1635 | csr_off == ASIC_GPIO_FORCE || | |
1636 | csr_off == ASIC_QSFP1_CLEAR || | |
1637 | csr_off == ASIC_QSFP1_FORCE || | |
1638 | csr_off == ASIC_QSFP2_CLEAR || | |
1639 | csr_off == ASIC_QSFP2_FORCE) | |
77241056 MM |
1640 | data = 0; |
1641 | else if (csr_off >= barlen) { | |
1642 | /* | |
1643 | * read_8051_data can read more than just 8 bytes at | |
1644 | * a time. However, folding this into the loop and | |
1645 | * handling the reads in 8 byte increments allows us | |
1646 | * to smoothly transition from chip memory to 8051 | |
1647 | * memory. | |
1648 | */ | |
1649 | if (read_8051_data(dd, | |
1650 | (u32)(csr_off - barlen), | |
1651 | sizeof(data), &data)) | |
1652 | break; /* failed */ | |
1653 | } else | |
1654 | data = readq(base + total); | |
1655 | if (put_user(data, (unsigned long __user *)(buf + total))) | |
1656 | break; | |
1657 | } | |
1658 | *f_pos += total; | |
1659 | return total; | |
1660 | } | |
1661 | ||
1662 | /* NOTE: assumes unsigned long is 8 bytes */ | |
1663 | static ssize_t ui_write(struct file *filp, const char __user *buf, | |
1664 | size_t count, loff_t *f_pos) | |
1665 | { | |
1666 | struct hfi1_devdata *dd = filp->private_data; | |
1667 | void __iomem *base; | |
1668 | unsigned long total, data, csr_off; | |
1669 | int in_lcb; | |
1670 | ||
1671 | /* only write 8 byte quantities */ | |
1672 | if ((count % 8) != 0) | |
1673 | return -EINVAL; | |
1674 | /* offset must be 8-byte aligned */ | |
1675 | if ((*f_pos % 8) != 0) | |
1676 | return -EINVAL; | |
1677 | /* source buffer must be 8-byte aligned */ | |
1678 | if ((unsigned long)buf % 8 != 0) | |
1679 | return -EINVAL; | |
1680 | /* must be in range */ | |
1681 | if (*f_pos + count > dd->kregend - dd->kregbase) | |
1682 | return -EINVAL; | |
1683 | ||
1684 | base = (void __iomem *)dd->kregbase + *f_pos; | |
1685 | csr_off = *f_pos; | |
1686 | in_lcb = 0; | |
1687 | for (total = 0; total < count; total += 8, csr_off += 8) { | |
1688 | if (get_user(data, (unsigned long __user *)(buf + total))) | |
1689 | break; | |
1690 | /* accessing LCB CSRs requires a special procedure */ | |
1691 | if (is_lcb_offset(csr_off)) { | |
1692 | if (!in_lcb) { | |
1693 | int ret = acquire_lcb_access(dd, 1); | |
1694 | ||
1695 | if (ret) | |
1696 | break; | |
1697 | in_lcb = 1; | |
1698 | } | |
1699 | } else { | |
1700 | if (in_lcb) { | |
1701 | release_lcb_access(dd, 1); | |
1702 | in_lcb = 0; | |
1703 | } | |
1704 | } | |
1705 | writeq(data, base + total); | |
1706 | } | |
1707 | if (in_lcb) | |
1708 | release_lcb_access(dd, 1); | |
1709 | *f_pos += total; | |
1710 | return total; | |
1711 | } | |
1712 | ||
1713 | static const struct file_operations ui_file_ops = { | |
1714 | .owner = THIS_MODULE, | |
1715 | .llseek = ui_lseek, | |
1716 | .read = ui_read, | |
1717 | .write = ui_write, | |
1718 | .open = ui_open, | |
1719 | .release = ui_release, | |
1720 | }; | |
b91cc573 | 1721 | |
77241056 MM |
1722 | #define UI_OFFSET 192 /* device minor offset for UI devices */ |
1723 | static int create_ui = 1; | |
1724 | ||
1725 | static struct cdev wildcard_cdev; | |
1726 | static struct device *wildcard_device; | |
1727 | ||
1728 | static atomic_t user_count = ATOMIC_INIT(0); | |
1729 | ||
1730 | static void user_remove(struct hfi1_devdata *dd) | |
1731 | { | |
1732 | if (atomic_dec_return(&user_count) == 0) | |
1733 | hfi1_cdev_cleanup(&wildcard_cdev, &wildcard_device); | |
1734 | ||
1735 | hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device); | |
1736 | hfi1_cdev_cleanup(&dd->ui_cdev, &dd->ui_device); | |
1737 | } | |
1738 | ||
1739 | static int user_add(struct hfi1_devdata *dd) | |
1740 | { | |
1741 | char name[10]; | |
1742 | int ret; | |
1743 | ||
1744 | if (atomic_inc_return(&user_count) == 1) { | |
1745 | ret = hfi1_cdev_init(0, class_name(), &hfi1_file_ops, | |
e116a64f IW |
1746 | &wildcard_cdev, &wildcard_device, |
1747 | true); | |
77241056 MM |
1748 | if (ret) |
1749 | goto done; | |
1750 | } | |
1751 | ||
1752 | snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit); | |
1753 | ret = hfi1_cdev_init(dd->unit + 1, name, &hfi1_file_ops, | |
e116a64f IW |
1754 | &dd->user_cdev, &dd->user_device, |
1755 | true); | |
77241056 MM |
1756 | if (ret) |
1757 | goto done; | |
1758 | ||
1759 | if (create_ui) { | |
1760 | snprintf(name, sizeof(name), | |
1761 | "%s_ui%d", class_name(), dd->unit); | |
1762 | ret = hfi1_cdev_init(dd->unit + UI_OFFSET, name, &ui_file_ops, | |
e116a64f IW |
1763 | &dd->ui_cdev, &dd->ui_device, |
1764 | false); | |
77241056 MM |
1765 | if (ret) |
1766 | goto done; | |
1767 | } | |
1768 | ||
1769 | return 0; | |
1770 | done: | |
1771 | user_remove(dd); | |
1772 | return ret; | |
1773 | } | |
1774 | ||
1775 | /* | |
1776 | * Create per-unit files in /dev | |
1777 | */ | |
1778 | int hfi1_device_create(struct hfi1_devdata *dd) | |
1779 | { | |
1780 | int r, ret; | |
1781 | ||
1782 | r = user_add(dd); | |
1783 | ret = hfi1_diag_add(dd); | |
1784 | if (r && !ret) | |
1785 | ret = r; | |
1786 | return ret; | |
1787 | } | |
1788 | ||
1789 | /* | |
1790 | * Remove per-unit files in /dev | |
1791 | * void, core kernel returns no errors for this stuff | |
1792 | */ | |
1793 | void hfi1_device_remove(struct hfi1_devdata *dd) | |
1794 | { | |
1795 | user_remove(dd); | |
1796 | hfi1_diag_remove(dd); | |
1797 | } |