1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3 *
4 * Copyright (c) 2007 Cisco Systems. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35 #ifndef IB_UMEM_H
36 #define IB_UMEM_H
37
38 #include <linux/list.h>
39 #include <linux/scatterlist.h>
40 #include <linux/workqueue.h>
41
42 struct ib_ucontext;
43 struct ib_umem_odp;
44
45 struct ib_umem {
46 struct ib_ucontext *context;
47 size_t length;
48 unsigned long address;
49 int page_size;
50 int writable;
51 struct work_struct work;
52 pid_t pid;
53 struct mm_struct *mm;
54 unsigned long diff;
55 struct ib_umem_odp *odp_data;
56 struct sg_table sg_head;
57 int nmap;
58 int npages;
59 };
60
61 /* Returns the offset of the umem start relative to the first page. */
ib_umem_offset(struct ib_umem * umem)62 static inline int ib_umem_offset(struct ib_umem *umem)
63 {
64 return umem->address & ((unsigned long)umem->page_size - 1);
65 }
66
67 /* Returns the first page of an ODP umem. */
ib_umem_start(struct ib_umem * umem)68 static inline unsigned long ib_umem_start(struct ib_umem *umem)
69 {
70 return umem->address - ib_umem_offset(umem);
71 }
72
73 /* Returns the address of the page after the last one of an ODP umem. */
ib_umem_end(struct ib_umem * umem)74 static inline unsigned long ib_umem_end(struct ib_umem *umem)
75 {
76 return PAGE_ALIGN(umem->address + umem->length);
77 }
78
ib_umem_num_pages(struct ib_umem * umem)79 static inline size_t ib_umem_num_pages(struct ib_umem *umem)
80 {
81 return (ib_umem_end(umem) - ib_umem_start(umem)) >> PAGE_SHIFT;
82 }
83
84 #ifdef CONFIG_INFINIBAND_USER_MEM
85
86 struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr,
87 size_t size, int access, int dmasync);
88 void ib_umem_release(struct ib_umem *umem);
89 int ib_umem_page_count(struct ib_umem *umem);
90 int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
91 size_t length);
92
93 #else /* CONFIG_INFINIBAND_USER_MEM */
94
95 #include <linux/err.h>
96
ib_umem_get(struct ib_ucontext * context,unsigned long addr,size_t size,int access,int dmasync)97 static inline struct ib_umem *ib_umem_get(struct ib_ucontext *context,
98 unsigned long addr, size_t size,
99 int access, int dmasync) {
100 return ERR_PTR(-EINVAL);
101 }
ib_umem_release(struct ib_umem * umem)102 static inline void ib_umem_release(struct ib_umem *umem) { }
ib_umem_page_count(struct ib_umem * umem)103 static inline int ib_umem_page_count(struct ib_umem *umem) { return 0; }
ib_umem_copy_from(void * dst,struct ib_umem * umem,size_t offset,size_t length)104 static inline int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
105 size_t length) {
106 return -EINVAL;
107 }
108 #endif /* CONFIG_INFINIBAND_USER_MEM */
109
110 #endif /* IB_UMEM_H */
111