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
42struct ib_ucontext;
43struct ib_umem_odp;
44
45struct 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. */
62static 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. */
68static 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. */
74static inline unsigned long ib_umem_end(struct ib_umem *umem)
75{
76	return PAGE_ALIGN(umem->address + umem->length);
77}
78
79static 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
86struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr,
87			    size_t size, int access, int dmasync);
88void ib_umem_release(struct ib_umem *umem);
89int ib_umem_page_count(struct ib_umem *umem);
90int 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
97static 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}
102static inline void ib_umem_release(struct ib_umem *umem) { }
103static inline int ib_umem_page_count(struct ib_umem *umem) { return 0; }
104static 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