xref: /freebsd/contrib/ofed/libbnxtre/list.h (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*
2  * Copyright (c) 2024, Broadcom. All rights reserved.  The term
3  * Broadcom refers to Broadcom Limited and/or its subsidiaries.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef __BNXT_RE_LIST_H__
31 #define __BNXT_RE_LIST_H__
32 
33 struct bnxt_re_list_node {
34 	struct bnxt_re_list_node *next, *prev;
35 	uint8_t valid;
36 };
37 
38 struct bnxt_re_list_head {
39 	struct bnxt_re_list_node node;
40 	pthread_mutex_t lock;
41 };
42 
43 #define DBLY_LIST_HEAD_INIT(name) { { true, &(name.node), &(name.node) } , \
44 			PTHREAD_MUTEX_INITIALIZER }
45 
46 #define DBLY_LIST_HEAD(name) \
47 	struct bnxt_re_list_head name = DBLY_LIST_HEAD_INIT(name); \
48 
49 #define INIT_DBLY_LIST_NODE(ptr) do { \
50 	(ptr)->next = (ptr); (ptr)->prev = (ptr); (ptr)->valid = false;	\
51 } while (0)
52 
53 #define INIT_DBLY_LIST_HEAD(ptr) INIT_DBLY_LIST_NODE(ptr.node)
54 
55 static inline void __list_add_node(struct bnxt_re_list_node *new,
56 				       struct bnxt_re_list_node *prev,
57 				       struct bnxt_re_list_node *next)
58 {
59 	next->prev = new;
60 	new->next = next;
61 	new->prev = prev;
62 	prev->next = new;
63 }
64 
65 static inline void list_add_node_tail(struct bnxt_re_list_node *new,
66 					  struct bnxt_re_list_head *head)
67 {
68 	__list_add_node(new, head->node.prev, &head->node);
69 	new->valid = true;
70 }
71 
72 static inline void __list_del_node(struct bnxt_re_list_node *prev,
73 				   struct bnxt_re_list_node *next)
74 {
75 	next->prev = prev;
76 	prev->next = next;
77 }
78 
79 static inline void list_del_node(struct bnxt_re_list_node *entry)
80 {
81 	__list_del_node(entry->prev, entry->next);
82 	entry->next = entry->prev = 0;
83 	entry->valid = false;
84 }
85 
86 #define bnxt_re_list_empty(head)			\
87 	(((head)->node.next == &(head)->node) &&	\
88 	((head)->node.prev == &(head)->node))
89 
90 #define list_lock(head) pthread_mutex_lock(&((head)->lock))
91 #define list_unlock(head) pthread_mutex_unlock(&((head)->lock))
92 
93 #define list_node(ptr, type, member) \
94     ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
95 
96 #define list_node_valid(node)	(node)->valid
97 
98 /**
99  * list_for_each_node_safe - iterate over a list safe against removal of list entry
100  * @pos:	the &struct bnxt_re_list_head to use as a loop counter.
101  * @n:		another &struct bnxt_re_list_head to use as temporary storage
102  * @head:	the head for your list.
103  */
104 #define list_for_each_node_safe(pos, n, head) \
105 	for (pos = (head)->node.next, n = pos->next; pos != &((head)->node); \
106 		pos = n, n = pos->next)
107 
108 static inline void bnxt_re_list_add_node(struct bnxt_re_list_node *node,
109 					 struct bnxt_re_list_head *head)
110 {
111 	if (!list_node_valid(node))
112 		list_add_node_tail(node, head);
113 }
114 
115 static inline void bnxt_re_list_del_node(struct bnxt_re_list_node *node,
116 					 struct bnxt_re_list_head *head)
117 {
118 	if (list_node_valid(node))
119 		list_del_node(node);
120 }
121 
122 #endif	/* __bnxt_re_LIST_H__ */
123