1e3cf00d0SUpinder Malhi /*
2e3cf00d0SUpinder Malhi  * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
3e3cf00d0SUpinder Malhi  *
43805eadeSJeff Squyres  * This software is available to you under a choice of one of two
53805eadeSJeff Squyres  * licenses.  You may choose to be licensed under the terms of the GNU
63805eadeSJeff Squyres  * General Public License (GPL) Version 2, available from the file
73805eadeSJeff Squyres  * COPYING in the main directory of this source tree, or the
83805eadeSJeff Squyres  * BSD license below:
93805eadeSJeff Squyres  *
103805eadeSJeff Squyres  *     Redistribution and use in source and binary forms, with or
113805eadeSJeff Squyres  *     without modification, are permitted provided that the following
123805eadeSJeff Squyres  *     conditions are met:
133805eadeSJeff Squyres  *
143805eadeSJeff Squyres  *      - Redistributions of source code must retain the above
153805eadeSJeff Squyres  *        copyright notice, this list of conditions and the following
163805eadeSJeff Squyres  *        disclaimer.
173805eadeSJeff Squyres  *
183805eadeSJeff Squyres  *      - Redistributions in binary form must reproduce the above
193805eadeSJeff Squyres  *        copyright notice, this list of conditions and the following
203805eadeSJeff Squyres  *        disclaimer in the documentation and/or other materials
213805eadeSJeff Squyres  *        provided with the distribution.
22e3cf00d0SUpinder Malhi  *
23e3cf00d0SUpinder Malhi  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24e3cf00d0SUpinder Malhi  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25e3cf00d0SUpinder Malhi  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26e3cf00d0SUpinder Malhi  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27e3cf00d0SUpinder Malhi  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28e3cf00d0SUpinder Malhi  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29e3cf00d0SUpinder Malhi  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30e3cf00d0SUpinder Malhi  * SOFTWARE.
31e3cf00d0SUpinder Malhi  *
32e3cf00d0SUpinder Malhi  */
33e3cf00d0SUpinder Malhi 
34e3cf00d0SUpinder Malhi #ifndef USNIC_UIOM_H_
35e3cf00d0SUpinder Malhi #define USNIC_UIOM_H_
36e3cf00d0SUpinder Malhi 
37e3cf00d0SUpinder Malhi #include <linux/list.h>
38e3cf00d0SUpinder Malhi #include <linux/scatterlist.h>
39e3cf00d0SUpinder Malhi 
40e3cf00d0SUpinder Malhi #include "usnic_uiom_interval_tree.h"
41e3cf00d0SUpinder Malhi 
4243cbd64bSJason Gunthorpe struct ib_ucontext;
4343cbd64bSJason Gunthorpe 
44e3cf00d0SUpinder Malhi #define USNIC_UIOM_READ			(1)
45e3cf00d0SUpinder Malhi #define USNIC_UIOM_WRITE		(2)
46e3cf00d0SUpinder Malhi 
47e3cf00d0SUpinder Malhi #define USNIC_UIOM_MAX_PD_CNT		(1000)
48e3cf00d0SUpinder Malhi #define USNIC_UIOM_MAX_MR_CNT		(1000000)
49e3cf00d0SUpinder Malhi #define USNIC_UIOM_MAX_MR_SIZE		(~0UL)
50e3cf00d0SUpinder Malhi #define USNIC_UIOM_PAGE_SIZE		(PAGE_SIZE)
51e3cf00d0SUpinder Malhi 
52e3cf00d0SUpinder Malhi struct usnic_uiom_dev {
53e3cf00d0SUpinder Malhi 	struct device			*dev;
54e3cf00d0SUpinder Malhi 	struct list_head		link;
55e3cf00d0SUpinder Malhi };
56e3cf00d0SUpinder Malhi 
57e3cf00d0SUpinder Malhi struct usnic_uiom_pd {
58e3cf00d0SUpinder Malhi 	struct iommu_domain		*domain;
59e3cf00d0SUpinder Malhi 	spinlock_t			lock;
60f808c13fSDavidlohr Bueso 	struct rb_root_cached		root;
61e3cf00d0SUpinder Malhi 	struct list_head		devs;
62e3cf00d0SUpinder Malhi 	int				dev_cnt;
63e3cf00d0SUpinder Malhi };
64e3cf00d0SUpinder Malhi 
65e3cf00d0SUpinder Malhi struct usnic_uiom_reg {
66e3cf00d0SUpinder Malhi 	struct usnic_uiom_pd		*pd;
67e3cf00d0SUpinder Malhi 	unsigned long			va;
68e3cf00d0SUpinder Malhi 	size_t				length;
69e3cf00d0SUpinder Malhi 	int				offset;
70e3cf00d0SUpinder Malhi 	int				page_size;
71e3cf00d0SUpinder Malhi 	int				writable;
72e3cf00d0SUpinder Malhi 	struct list_head		chunk_list;
73e3cf00d0SUpinder Malhi 	struct work_struct		work;
74ece8ea7bSJason Gunthorpe 	struct mm_struct		*owning_mm;
75e3cf00d0SUpinder Malhi };
76e3cf00d0SUpinder Malhi 
77e3cf00d0SUpinder Malhi struct usnic_uiom_chunk {
78e3cf00d0SUpinder Malhi 	struct list_head		list;
79e3cf00d0SUpinder Malhi 	int				nents;
805b361328SGustavo A. R. Silva 	struct scatterlist		page_list[];
81e3cf00d0SUpinder Malhi };
82e3cf00d0SUpinder Malhi 
83*cc97c6d9SRobin Murphy struct usnic_uiom_pd *usnic_uiom_alloc_pd(struct device *dev);
84e3cf00d0SUpinder Malhi void usnic_uiom_dealloc_pd(struct usnic_uiom_pd *pd);
85e3cf00d0SUpinder Malhi int usnic_uiom_attach_dev_to_pd(struct usnic_uiom_pd *pd, struct device *dev);
86e3cf00d0SUpinder Malhi void usnic_uiom_detach_dev_from_pd(struct usnic_uiom_pd *pd,
87e3cf00d0SUpinder Malhi 					struct device *dev);
88e3cf00d0SUpinder Malhi struct device **usnic_uiom_get_dev_list(struct usnic_uiom_pd *pd);
89e3cf00d0SUpinder Malhi void usnic_uiom_free_dev_list(struct device **devs);
90e3cf00d0SUpinder Malhi struct usnic_uiom_reg *usnic_uiom_reg_get(struct usnic_uiom_pd *pd,
91e3cf00d0SUpinder Malhi 						unsigned long addr, size_t size,
92e3cf00d0SUpinder Malhi 						int access, int dmasync);
93bdeacabdSShamir Rabinovitch void usnic_uiom_reg_release(struct usnic_uiom_reg *uiomr);
94e3cf00d0SUpinder Malhi #endif /* USNIC_UIOM_H_ */
95