1*fe267a55SPedro F. Giffuni /*-
2*fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3*fe267a55SPedro F. Giffuni *
4aa0a1e58SJeff Roberson * Copyright (c) 2007 Cisco Systems. All rights reserved.
5aa0a1e58SJeff Roberson *
6aa0a1e58SJeff Roberson * This software is available to you under a choice of one of two
7aa0a1e58SJeff Roberson * licenses. You may choose to be licensed under the terms of the GNU
8aa0a1e58SJeff Roberson * General Public License (GPL) Version 2, available from the file
9aa0a1e58SJeff Roberson * COPYING in the main directory of this source tree, or the
10aa0a1e58SJeff Roberson * OpenIB.org BSD license below:
11aa0a1e58SJeff Roberson *
12aa0a1e58SJeff Roberson * Redistribution and use in source and binary forms, with or
13aa0a1e58SJeff Roberson * without modification, are permitted provided that the following
14aa0a1e58SJeff Roberson * conditions are met:
15aa0a1e58SJeff Roberson *
16aa0a1e58SJeff Roberson * - Redistributions of source code must retain the above
17aa0a1e58SJeff Roberson * copyright notice, this list of conditions and the following
18aa0a1e58SJeff Roberson * disclaimer.
19aa0a1e58SJeff Roberson *
20aa0a1e58SJeff Roberson * - Redistributions in binary form must reproduce the above
21aa0a1e58SJeff Roberson * copyright notice, this list of conditions and the following
22aa0a1e58SJeff Roberson * disclaimer in the documentation and/or other materials
23aa0a1e58SJeff Roberson * provided with the distribution.
24aa0a1e58SJeff Roberson *
25aa0a1e58SJeff Roberson * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26aa0a1e58SJeff Roberson * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27aa0a1e58SJeff Roberson * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28aa0a1e58SJeff Roberson * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29aa0a1e58SJeff Roberson * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30aa0a1e58SJeff Roberson * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31aa0a1e58SJeff Roberson * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32aa0a1e58SJeff Roberson * SOFTWARE.
33aa0a1e58SJeff Roberson */
34aa0a1e58SJeff Roberson
35aa0a1e58SJeff Roberson #ifndef IB_UMEM_H
36aa0a1e58SJeff Roberson #define IB_UMEM_H
37aa0a1e58SJeff Roberson
38aa0a1e58SJeff Roberson #include <linux/list.h>
39aa0a1e58SJeff Roberson #include <linux/scatterlist.h>
40aa0a1e58SJeff Roberson #include <linux/workqueue.h>
41aa0a1e58SJeff Roberson
42aa0a1e58SJeff Roberson struct ib_ucontext;
43478d3005SHans Petter Selasky struct ib_umem_odp;
44aa0a1e58SJeff Roberson
45aa0a1e58SJeff Roberson struct ib_umem {
46aa0a1e58SJeff Roberson struct ib_ucontext *context;
47aa0a1e58SJeff Roberson size_t length;
48478d3005SHans Petter Selasky unsigned long address;
49aa0a1e58SJeff Roberson int page_size;
50aa0a1e58SJeff Roberson int writable;
51aa0a1e58SJeff Roberson struct work_struct work;
52478d3005SHans Petter Selasky pid_t pid;
53478d3005SHans Petter Selasky struct mm_struct *mm;
54aa0a1e58SJeff Roberson unsigned long diff;
55478d3005SHans Petter Selasky struct ib_umem_odp *odp_data;
56b5c1e0cbSHans Petter Selasky struct sg_table sg_head;
57aa0a1e58SJeff Roberson int nmap;
58b5c1e0cbSHans Petter Selasky int npages;
59aa0a1e58SJeff Roberson };
60aa0a1e58SJeff Roberson
61478d3005SHans Petter Selasky /* Returns the offset of the umem start relative to the first page. */
ib_umem_offset(struct ib_umem * umem)62478d3005SHans Petter Selasky static inline int ib_umem_offset(struct ib_umem *umem)
63478d3005SHans Petter Selasky {
64478d3005SHans Petter Selasky return umem->address & ((unsigned long)umem->page_size - 1);
65478d3005SHans Petter Selasky }
66478d3005SHans Petter Selasky
67478d3005SHans Petter Selasky /* Returns the first page of an ODP umem. */
ib_umem_start(struct ib_umem * umem)68478d3005SHans Petter Selasky static inline unsigned long ib_umem_start(struct ib_umem *umem)
69478d3005SHans Petter Selasky {
70478d3005SHans Petter Selasky return umem->address - ib_umem_offset(umem);
71478d3005SHans Petter Selasky }
72478d3005SHans Petter Selasky
73478d3005SHans Petter Selasky /* Returns the address of the page after the last one of an ODP umem. */
ib_umem_end(struct ib_umem * umem)74478d3005SHans Petter Selasky static inline unsigned long ib_umem_end(struct ib_umem *umem)
75478d3005SHans Petter Selasky {
76478d3005SHans Petter Selasky return PAGE_ALIGN(umem->address + umem->length);
77478d3005SHans Petter Selasky }
78478d3005SHans Petter Selasky
ib_umem_num_pages(struct ib_umem * umem)79478d3005SHans Petter Selasky static inline size_t ib_umem_num_pages(struct ib_umem *umem)
80478d3005SHans Petter Selasky {
81478d3005SHans Petter Selasky return (ib_umem_end(umem) - ib_umem_start(umem)) >> PAGE_SHIFT;
82478d3005SHans Petter Selasky }
83478d3005SHans Petter Selasky
84478d3005SHans Petter Selasky #ifdef CONFIG_INFINIBAND_USER_MEM
85478d3005SHans Petter Selasky
86aa0a1e58SJeff Roberson struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr,
87aa0a1e58SJeff Roberson size_t size, int access, int dmasync);
88aa0a1e58SJeff Roberson void ib_umem_release(struct ib_umem *umem);
89aa0a1e58SJeff Roberson int ib_umem_page_count(struct ib_umem *umem);
90478d3005SHans Petter Selasky int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
91478d3005SHans Petter Selasky size_t length);
92478d3005SHans Petter Selasky
93478d3005SHans Petter Selasky #else /* CONFIG_INFINIBAND_USER_MEM */
94478d3005SHans Petter Selasky
95478d3005SHans Petter Selasky #include <linux/err.h>
96478d3005SHans Petter Selasky
ib_umem_get(struct ib_ucontext * context,unsigned long addr,size_t size,int access,int dmasync)97478d3005SHans Petter Selasky static inline struct ib_umem *ib_umem_get(struct ib_ucontext *context,
98478d3005SHans Petter Selasky unsigned long addr, size_t size,
99478d3005SHans Petter Selasky int access, int dmasync) {
100478d3005SHans Petter Selasky return ERR_PTR(-EINVAL);
101478d3005SHans Petter Selasky }
ib_umem_release(struct ib_umem * umem)102478d3005SHans Petter Selasky static inline void ib_umem_release(struct ib_umem *umem) { }
ib_umem_page_count(struct ib_umem * umem)103478d3005SHans Petter Selasky 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)104478d3005SHans Petter Selasky static inline int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
105478d3005SHans Petter Selasky size_t length) {
106478d3005SHans Petter Selasky return -EINVAL;
107478d3005SHans Petter Selasky }
108478d3005SHans Petter Selasky #endif /* CONFIG_INFINIBAND_USER_MEM */
109aa0a1e58SJeff Roberson
110aa0a1e58SJeff Roberson #endif /* IB_UMEM_H */
111