xref: /illumos-gate/usr/src/uts/common/io/mega_sas/list.h (revision 1c9de0c9)
1 /*
2  * list.h: header for mega_sas
3  *
4  * Solaris MegaRAID driver for SAS controllers
5  * Copyright (c) 2004-2008, LSI Logic Corporation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  *    this list of conditions and the following disclaimer in the documentation
16  *    and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the author nor the names of its contributors may be
19  *    used to endorse or promote products derived from this software without
20  *    specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
33  * DAMAGE.
34  */
35 
36 /*
37  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
38  * Use is subject to license terms.
39  */
40 
41 #ifndef	_LIST_H_
42 #define	_LIST_H_
43 
44 #pragma ident	"%Z%%M%	%I%	%E% SMI"
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 /*
51  * Simple doubly linked list implementation.
52  *
53  * Some of the internal functions ("__xxx") are useful when
54  * manipulating whole lists rather than single entries, as
55  * sometimes we already know the next/prev entries and we can
56  * generate better code by using them directly rather than
57  * using the generic single-entry routines.
58  */
59 
60 struct mlist_head {
61 	struct mlist_head *next, *prev;
62 };
63 
64 typedef struct mlist_head mlist_t;
65 
66 #define	LIST_HEAD_INIT(name) { &(name), &(name) }
67 
68 #define	LIST_HEAD(name) \
69 	struct mlist_head name = LIST_HEAD_INIT(name)
70 
71 #define	INIT_LIST_HEAD(ptr) { \
72 	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
73 }
74 
75 
76 /*
77  * Insert a new entry between two known consecutive entries.
78  *
79  * This is only for internal list manipulation where we know
80  * the prev/next entries already!
81  */
82 static void __list_add(struct mlist_head *new,
83 	struct mlist_head *prev,
84 	struct mlist_head *next)
85 {
86 	next->prev = new;
87 	new->next = next;
88 	new->prev = prev;
89 	prev->next = new;
90 }
91 
92 
93 /*
94  * mlist_add - add a new entry
95  * @new: new entry to be added
96  * @head: list head to add it after
97  *
98  * Insert a new entry after the specified head.
99  * This is good for implementing stacks.
100  */
101 static void mlist_add(struct mlist_head *new, struct mlist_head *head)
102 {
103 	__list_add(new, head, head->next);
104 }
105 
106 
107 /*
108  * mlist_add_tail - add a new entry
109  * @new: new entry to be added
110  * @head: list head to add it before
111  *
112  * Insert a new entry before the specified head.
113  * This is useful for implementing queues.
114  */
115 static void mlist_add_tail(struct mlist_head *new, struct mlist_head *head)
116 {
117 	__list_add(new, head->prev, head);
118 }
119 
120 
121 
122 /*
123  * Delete a list entry by making the prev/next entries
124  * point to each other.
125  *
126  * This is only for internal list manipulation where we know
127  * the prev/next entries already!
128  */
129 static void __list_del(struct mlist_head *prev,
130 			struct mlist_head *next)
131 {
132 	next->prev = prev;
133 	prev->next = next;
134 }
135 
136 
137 /*
138  * mlist_del - deletes entry from list.
139  * @entry:	the element to delete from the list.
140  * Note:	list_empty on entry does not return true after this, the entry
141  * is in an undefined state.
142  */
143 /* LINTED E_STATIC_UNUSED */
144 static void mlist_del(struct mlist_head *entry)
145 {
146 	__list_del(entry->prev, entry->next);
147 	entry->next = entry->prev = 0;
148 }
149 
150 
151 /*
152  * mlist_del_init - deletes entry from list and reinitialize it.
153  * @entry: the element to delete from the list.
154  */
155 static void mlist_del_init(struct mlist_head *entry)
156 {
157 	__list_del(entry->prev, entry->next);
158 	INIT_LIST_HEAD(entry);
159 }
160 
161 
162 /*
163  * mlist_empty - tests whether a list is empty
164  * @head: the list to test.
165  */
166 static int mlist_empty(struct mlist_head *head)
167 {
168 	return (head->next == head);
169 }
170 
171 
172 /*
173  * mlist_splice - join two lists
174  * @list: the new list to add.
175  * @head: the place to add it in the first list.
176  */
177 static void mlist_splice(struct mlist_head *list, struct mlist_head *head)
178 {
179 	struct mlist_head *first = list->next;
180 
181 	if (first != list) {
182 		struct mlist_head *last = list->prev;
183 		struct mlist_head *at = head->next;
184 
185 		first->prev = head;
186 		head->next = first;
187 
188 		last->next = at;
189 		at->prev = last;
190 	}
191 }
192 
193 
194 
195 /* TODO: set this */
196 #if 0
197 #pragma	inline(list_add, list_add_tail, __list_del, list_del,
198 		list_del_init, list_empty, list_splice)
199 #endif
200 
201 
202 /*
203  * mlist_entry - get the struct for this entry
204  * @ptr:	the &struct mlist_head pointer.
205  * @type:	the type of the struct this is embedded in.
206  * @member:	the name of the list_struct within the struct.
207  */
208 #define	mlist_entry(ptr, type, member) \
209 	((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
210 
211 
212 /*
213  * mlist_for_each	-	iterate over a list
214  * @pos:	the &struct mlist_head to use as a loop counter.
215  * @head:	the head for your list.
216  */
217 #define	mlist_for_each(pos, head) \
218 	for (pos = (head)->next, prefetch(pos->next); pos != (head); \
219 		pos = pos->next, prefetch(pos->next))
220 
221 
222 /*
223  * mlist_for_each_safe - iterate over a list safe against removal of list entry
224  * @pos:	the &struct mlist_head to use as a loop counter.
225  * @n:		another &struct mlist_head to use as temporary storage
226  * @head:	the head for your list.
227  */
228 #define	mlist_for_each_safe(pos, n, head) \
229 	for (pos = (head)->next, n = pos->next; pos != (head); \
230 		pos = n, n = pos->next)
231 
232 #ifdef __cplusplus
233 }
234 #endif
235 
236 #endif /* _LIST_H_ */
237