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 struct mm_struct *owning_mm;
48 size_t length;
49 unsigned long address;
50 int page_shift;
51 u32 writable : 1;
52 u32 hugetlb : 1;
53 u32 is_odp : 1;
54 struct work_struct work;
55 struct sg_table sg_head;
56 int nmap;
57 int npages;
58 };
59
60 /* Returns the offset of the umem start relative to the first page. */
ib_umem_offset(struct ib_umem * umem)61 static inline int ib_umem_offset(struct ib_umem *umem)
62 {
63 return umem->address & (BIT(umem->page_shift) - 1);
64 }
65
66 /* Returns the first page of an ODP umem. */
ib_umem_start(struct ib_umem * umem)67 static inline unsigned long ib_umem_start(struct ib_umem *umem)
68 {
69 return umem->address - ib_umem_offset(umem);
70 }
71
72 /* Returns the address of the page after the last one of an ODP umem. */
ib_umem_end(struct ib_umem * umem)73 static inline unsigned long ib_umem_end(struct ib_umem *umem)
74 {
75 return ALIGN(umem->address + umem->length, BIT(umem->page_shift));
76 }
77
ib_umem_num_pages(struct ib_umem * umem)78 static inline size_t ib_umem_num_pages(struct ib_umem *umem)
79 {
80 return (ib_umem_end(umem) - ib_umem_start(umem)) >> umem->page_shift;
81 }
82
83 #ifdef CONFIG_INFINIBAND_USER_MEM
84
85 struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr,
86 size_t size, int access, int dmasync);
87 void ib_umem_release(struct ib_umem *umem);
88 int ib_umem_page_count(struct ib_umem *umem);
89 int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
90 size_t length);
91
92 #else /* CONFIG_INFINIBAND_USER_MEM */
93
94 #include <linux/err.h>
95
ib_umem_get(struct ib_ucontext * context,unsigned long addr,size_t size,int access,int dmasync)96 static inline struct ib_umem *ib_umem_get(struct ib_ucontext *context,
97 unsigned long addr, size_t size,
98 int access, int dmasync) {
99 return ERR_PTR(-EINVAL);
100 }
ib_umem_release(struct ib_umem * umem)101 static inline void ib_umem_release(struct ib_umem *umem) { }
ib_umem_page_count(struct ib_umem * umem)102 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)103 static inline int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
104 size_t length) {
105 return -EINVAL;
106 }
107 #endif /* CONFIG_INFINIBAND_USER_MEM */
108
109 #endif /* IB_UMEM_H */
110