xref: /openbmc/linux/drivers/infiniband/hw/usnic/usnic_uiom_interval_tree.h (revision cf40a76e7d5874bb25f4404eecc58a2e033af885)
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_INTERVAL_TREE_H_
35e3cf00d0SUpinder Malhi #define USNIC_UIOM_INTERVAL_TREE_H_
36e3cf00d0SUpinder Malhi 
37e3cf00d0SUpinder Malhi #include <linux/rbtree.h>
38e3cf00d0SUpinder Malhi 
39e3cf00d0SUpinder Malhi struct usnic_uiom_interval_node {
40e3cf00d0SUpinder Malhi 	struct rb_node			rb;
41e3cf00d0SUpinder Malhi 	struct list_head		link;
42e3cf00d0SUpinder Malhi 	unsigned long			start;
43e3cf00d0SUpinder Malhi 	unsigned long			last;
44e3cf00d0SUpinder Malhi 	unsigned long			__subtree_last;
45e3cf00d0SUpinder Malhi 	unsigned int			ref_cnt;
46e3cf00d0SUpinder Malhi 	int				flags;
47e3cf00d0SUpinder Malhi };
48e3cf00d0SUpinder Malhi 
49e3cf00d0SUpinder Malhi extern void
50e3cf00d0SUpinder Malhi usnic_uiom_interval_tree_insert(struct usnic_uiom_interval_node *node,
51*f808c13fSDavidlohr Bueso 					struct rb_root_cached *root);
52e3cf00d0SUpinder Malhi extern void
53e3cf00d0SUpinder Malhi usnic_uiom_interval_tree_remove(struct usnic_uiom_interval_node *node,
54*f808c13fSDavidlohr Bueso 					struct rb_root_cached *root);
55e3cf00d0SUpinder Malhi extern struct usnic_uiom_interval_node *
56*f808c13fSDavidlohr Bueso usnic_uiom_interval_tree_iter_first(struct rb_root_cached *root,
57e3cf00d0SUpinder Malhi 					unsigned long start,
58e3cf00d0SUpinder Malhi 					unsigned long last);
59e3cf00d0SUpinder Malhi extern struct usnic_uiom_interval_node *
60e3cf00d0SUpinder Malhi usnic_uiom_interval_tree_iter_next(struct usnic_uiom_interval_node *node,
61e3cf00d0SUpinder Malhi 			unsigned long start, unsigned long last);
62e3cf00d0SUpinder Malhi /*
63e3cf00d0SUpinder Malhi  * Inserts {start...last} into {root}.  If there are overlaps,
64e3cf00d0SUpinder Malhi  * nodes will be broken up and merged
65e3cf00d0SUpinder Malhi  */
66*f808c13fSDavidlohr Bueso int usnic_uiom_insert_interval(struct rb_root_cached *root,
67e3cf00d0SUpinder Malhi 				unsigned long start, unsigned long last,
68e3cf00d0SUpinder Malhi 				int flags);
69e3cf00d0SUpinder Malhi /*
70e3cf00d0SUpinder Malhi  * Removed {start...last} from {root}.  The nodes removed are returned in
71e3cf00d0SUpinder Malhi  * 'removed.' The caller is responsibile for freeing memory of nodes in
72e3cf00d0SUpinder Malhi  * 'removed.'
73e3cf00d0SUpinder Malhi  */
74*f808c13fSDavidlohr Bueso void usnic_uiom_remove_interval(struct rb_root_cached *root,
75e3cf00d0SUpinder Malhi 				unsigned long start, unsigned long last,
76e3cf00d0SUpinder Malhi 				struct list_head *removed);
77e3cf00d0SUpinder Malhi /*
78e3cf00d0SUpinder Malhi  * Returns {start...last} - {root} (relative complement of {start...last} in
79e3cf00d0SUpinder Malhi  * {root}) in diff_set sorted ascendingly
80e3cf00d0SUpinder Malhi  */
81e3cf00d0SUpinder Malhi int usnic_uiom_get_intervals_diff(unsigned long start,
82e3cf00d0SUpinder Malhi 					unsigned long last, int flags,
83e3cf00d0SUpinder Malhi 					int flag_mask,
84*f808c13fSDavidlohr Bueso 					struct rb_root_cached *root,
85e3cf00d0SUpinder Malhi 					struct list_head *diff_set);
86e3cf00d0SUpinder Malhi /* Call this to free diff_set returned by usnic_uiom_get_intervals_diff */
87e3cf00d0SUpinder Malhi void usnic_uiom_put_interval_set(struct list_head *intervals);
88e3cf00d0SUpinder Malhi #endif /* USNIC_UIOM_INTERVAL_TREE_H_ */
89