1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
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
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #ifndef _LINUXKPI_LINUX_LIST_H_
32 #define _LINUXKPI_LINUX_LIST_H_
33 
34 #ifndef _STANDALONE
35 /*
36  * Since LIST_HEAD conflicts with the Linux definition we must include any
37  * FreeBSD header which requires it here so it is resolved with the correct
38  * definition prior to the undef.
39  */
40 #include <linux/types.h>
41 
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/queue.h>
45 #include <sys/cpuset.h>
46 #include <sys/jail.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <sys/proc.h>
50 #include <sys/vnode.h>
51 #include <sys/conf.h>
52 #include <sys/socket.h>
53 #include <sys/mbuf.h>
54 
55 #include <net/bpf.h>
56 #include <net/ethernet.h>
57 #include <net/if.h>
58 #include <net/if_var.h>
59 #include <net/if_types.h>
60 #include <net/if_media.h>
61 #include <net/vnet.h>
62 
63 #include <netinet/in.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/in_var.h>
66 #include <netinet/tcp_lro.h>
67 
68 #include <netinet6/in6_var.h>
69 #include <netinet6/nd6.h>
70 
71 #include <net80211/ieee80211.h>
72 #include <net80211/ieee80211_var.h>
73 #include <net80211/ieee80211_node.h>
74 
75 #include <vm/vm.h>
76 #include <vm/vm_object.h>
77 #include <vm/pmap.h>
78 #endif
79 
80 #ifndef prefetch
81 #define	prefetch(x)
82 #endif
83 
84 #define LINUX_LIST_HEAD_INIT(name) { &(name), &(name) }
85 
86 #define LINUX_LIST_HEAD(name) \
87 	struct list_head name = LINUX_LIST_HEAD_INIT(name)
88 
89 #ifndef LIST_HEAD_DEF
90 #define	LIST_HEAD_DEF
91 struct list_head {
92 	struct list_head *next;
93 	struct list_head *prev;
94 };
95 #endif
96 
97 static inline void
98 INIT_LIST_HEAD(struct list_head *list)
99 {
100 
101 	list->next = list->prev = list;
102 }
103 
104 static inline int
105 list_empty(const struct list_head *head)
106 {
107 
108 	return (head->next == head);
109 }
110 
111 static inline int
112 list_empty_careful(const struct list_head *head)
113 {
114 	struct list_head *next = head->next;
115 
116 	return ((next == head) && (next == head->prev));
117 }
118 
119 static inline void
120 __list_del(struct list_head *prev, struct list_head *next)
121 {
122 	next->prev = prev;
123 	WRITE_ONCE(prev->next, next);
124 }
125 
126 static inline void
127 __list_del_entry(struct list_head *entry)
128 {
129 
130 	__list_del(entry->prev, entry->next);
131 }
132 
133 static inline void
134 list_del(struct list_head *entry)
135 {
136 
137 	__list_del(entry->prev, entry->next);
138 }
139 
140 static inline void
141 list_replace(struct list_head *old, struct list_head *new)
142 {
143 	new->next = old->next;
144 	new->next->prev = new;
145 	new->prev = old->prev;
146 	new->prev->next = new;
147 }
148 
149 static inline void
150 list_replace_init(struct list_head *old, struct list_head *new)
151 {
152 	list_replace(old, new);
153 	INIT_LIST_HEAD(old);
154 }
155 
156 static inline void
157 linux_list_add(struct list_head *new, struct list_head *prev,
158     struct list_head *next)
159 {
160 
161 	next->prev = new;
162 	new->next = next;
163 	new->prev = prev;
164 	prev->next = new;
165 }
166 
167 static inline void
168 list_del_init(struct list_head *entry)
169 {
170 
171 	list_del(entry);
172 	INIT_LIST_HEAD(entry);
173 }
174 
175 #define	list_entry(ptr, type, field)	container_of(ptr, type, field)
176 
177 #define	list_first_entry(ptr, type, member) \
178 	list_entry((ptr)->next, type, member)
179 
180 #define	list_last_entry(ptr, type, member)	\
181 	list_entry((ptr)->prev, type, member)
182 
183 #define	list_first_entry_or_null(ptr, type, member) \
184 	(!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
185 
186 #define	list_next_entry(ptr, member)					\
187 	list_entry(((ptr)->member.next), typeof(*(ptr)), member)
188 
189 #define	list_safe_reset_next(ptr, n, member) \
190 	(n) = list_next_entry(ptr, member)
191 
192 #define	list_prev_entry(ptr, member)					\
193 	list_entry(((ptr)->member.prev), typeof(*(ptr)), member)
194 
195 #define	list_for_each(p, head)						\
196 	for (p = (head)->next; p != (head); p = (p)->next)
197 
198 #define	list_for_each_safe(p, n, head)					\
199 	for (p = (head)->next, n = (p)->next; p != (head); p = n, n = (p)->next)
200 
201 #define list_for_each_entry(p, h, field)				\
202 	for (p = list_entry((h)->next, typeof(*p), field); &(p)->field != (h); \
203 	    p = list_entry((p)->field.next, typeof(*p), field))
204 
205 #define list_for_each_entry_safe(p, n, h, field)			\
206 	for (p = list_entry((h)->next, typeof(*p), field),		\
207 	    n = list_entry((p)->field.next, typeof(*p), field); &(p)->field != (h);\
208 	    p = n, n = list_entry(n->field.next, typeof(*n), field))
209 
210 #define	list_for_each_entry_from(p, h, field) \
211 	for ( ; &(p)->field != (h); \
212 	    p = list_entry((p)->field.next, typeof(*p), field))
213 
214 #define	list_for_each_entry_continue(p, h, field)			\
215 	for (p = list_next_entry((p), field); &(p)->field != (h);	\
216 	    p = list_next_entry((p), field))
217 
218 #define	list_for_each_entry_safe_from(pos, n, head, member)			\
219 	for (n = list_entry((pos)->member.next, typeof(*pos), member);		\
220 	     &(pos)->member != (head);						\
221 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
222 
223 #define	list_for_each_entry_reverse(p, h, field)			\
224 	for (p = list_entry((h)->prev, typeof(*p), field); &(p)->field != (h); \
225 	    p = list_entry((p)->field.prev, typeof(*p), field))
226 
227 #define	list_for_each_entry_safe_reverse(p, n, h, field)		\
228 	for (p = list_entry((h)->prev, typeof(*p), field),		\
229 	    n = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
230 	    p = n, n = list_entry(n->field.prev, typeof(*n), field))
231 
232 #define	list_for_each_entry_continue_reverse(p, h, field) \
233 	for (p = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
234 	    p = list_entry((p)->field.prev, typeof(*p), field))
235 
236 #define	list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = (p)->prev)
237 
238 #define	list_for_each_entry_from_reverse(p, h, field)	\
239 	for (; &p->field != (h);			\
240 	     p = list_prev_entry(p, field))
241 
242 static inline void
243 list_add(struct list_head *new, struct list_head *head)
244 {
245 
246 	linux_list_add(new, head, head->next);
247 }
248 
249 static inline void
250 list_add_tail(struct list_head *new, struct list_head *head)
251 {
252 
253 	linux_list_add(new, head->prev, head);
254 }
255 
256 static inline void
257 list_move(struct list_head *list, struct list_head *head)
258 {
259 
260 	list_del(list);
261 	list_add(list, head);
262 }
263 
264 static inline void
265 list_move_tail(struct list_head *entry, struct list_head *head)
266 {
267 
268 	list_del(entry);
269 	list_add_tail(entry, head);
270 }
271 
272 static inline void
273 list_rotate_to_front(struct list_head *entry, struct list_head *head)
274 {
275 
276 	list_move_tail(entry, head);
277 }
278 
279 static inline void
280 list_bulk_move_tail(struct list_head *head, struct list_head *first,
281     struct list_head *last)
282 {
283 	first->prev->next = last->next;
284 	last->next->prev = first->prev;
285 	head->prev->next = first;
286 	first->prev = head->prev;
287 	last->next = head;
288 	head->prev = last;
289 }
290 
291 static inline void
292 linux_list_splice(const struct list_head *list, struct list_head *prev,
293     struct list_head *next)
294 {
295 	struct list_head *first;
296 	struct list_head *last;
297 
298 	if (list_empty(list))
299 		return;
300 	first = list->next;
301 	last = list->prev;
302 	first->prev = prev;
303 	prev->next = first;
304 	last->next = next;
305 	next->prev = last;
306 }
307 
308 static inline void
309 list_splice(const struct list_head *list, struct list_head *head)
310 {
311 
312 	linux_list_splice(list, head, head->next);
313 }
314 
315 static inline void
316 list_splice_tail(struct list_head *list, struct list_head *head)
317 {
318 
319 	linux_list_splice(list, head->prev, head);
320 }
321 
322 static inline void
323 list_splice_init(struct list_head *list, struct list_head *head)
324 {
325 
326 	linux_list_splice(list, head, head->next);
327 	INIT_LIST_HEAD(list);
328 }
329 
330 static inline void
331 list_splice_tail_init(struct list_head *list, struct list_head *head)
332 {
333 
334 	linux_list_splice(list, head->prev, head);
335 	INIT_LIST_HEAD(list);
336 }
337 
338 #undef LIST_HEAD
339 #define LIST_HEAD(name)	struct list_head name = { &(name), &(name) }
340 
341 struct hlist_head {
342 	struct hlist_node *first;
343 };
344 
345 struct hlist_node {
346 	struct hlist_node *next, **pprev;
347 };
348 #define	HLIST_HEAD_INIT { }
349 #define	HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
350 #define	INIT_HLIST_HEAD(head) (head)->first = NULL
351 #define	INIT_HLIST_NODE(node)						\
352 do {									\
353 	(node)->next = NULL;						\
354 	(node)->pprev = NULL;						\
355 } while (0)
356 
357 static inline int
358 hlist_unhashed(const struct hlist_node *h)
359 {
360 
361 	return !h->pprev;
362 }
363 
364 static inline int
365 hlist_empty(const struct hlist_head *h)
366 {
367 
368 	return !READ_ONCE(h->first);
369 }
370 
371 static inline void
372 hlist_del(struct hlist_node *n)
373 {
374 
375 	WRITE_ONCE(*(n->pprev), n->next);
376 	if (n->next != NULL)
377 		n->next->pprev = n->pprev;
378 }
379 
380 static inline void
381 hlist_del_init(struct hlist_node *n)
382 {
383 
384 	if (hlist_unhashed(n))
385 		return;
386 	hlist_del(n);
387 	INIT_HLIST_NODE(n);
388 }
389 
390 static inline void
391 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
392 {
393 
394 	n->next = h->first;
395 	if (h->first != NULL)
396 		h->first->pprev = &n->next;
397 	WRITE_ONCE(h->first, n);
398 	n->pprev = &h->first;
399 }
400 
401 static inline void
402 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
403 {
404 
405 	n->pprev = next->pprev;
406 	n->next = next;
407 	next->pprev = &n->next;
408 	WRITE_ONCE(*(n->pprev), n);
409 }
410 
411 static inline void
412 hlist_add_behind(struct hlist_node *n, struct hlist_node *prev)
413 {
414 
415 	n->next = prev->next;
416 	WRITE_ONCE(prev->next, n);
417 	n->pprev = &prev->next;
418 
419 	if (n->next != NULL)
420 		n->next->pprev = &n->next;
421 }
422 
423 static inline void
424 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
425 {
426 
427 	new->first = old->first;
428 	if (new->first)
429 		new->first->pprev = &new->first;
430 	old->first = NULL;
431 }
432 
433 static inline int list_is_singular(const struct list_head *head)
434 {
435 	return !list_empty(head) && (head->next == head->prev);
436 }
437 
438 static inline void __list_cut_position(struct list_head *list,
439 		struct list_head *head, struct list_head *entry)
440 {
441 	struct list_head *new_first = entry->next;
442 	list->next = head->next;
443 	list->next->prev = list;
444 	list->prev = entry;
445 	entry->next = list;
446 	head->next = new_first;
447 	new_first->prev = head;
448 }
449 
450 static inline void list_cut_position(struct list_head *list,
451 		struct list_head *head, struct list_head *entry)
452 {
453 	if (list_empty(head))
454 		return;
455 	if (list_is_singular(head) &&
456 		(head->next != entry && head != entry))
457 		return;
458 	if (entry == head)
459 		INIT_LIST_HEAD(list);
460 	else
461 		__list_cut_position(list, head, entry);
462 }
463 
464 static inline int list_is_first(const struct list_head *list,
465 				const struct list_head *head)
466 {
467 
468 	return (list->prev == head);
469 }
470 
471 static inline int list_is_last(const struct list_head *list,
472 				const struct list_head *head)
473 {
474 	return list->next == head;
475 }
476 
477 #define	hlist_entry(ptr, type, field)	container_of(ptr, type, field)
478 
479 #define	hlist_for_each(p, head)						\
480 	for (p = (head)->first; p; p = (p)->next)
481 
482 #define	hlist_for_each_safe(p, n, head)					\
483 	for (p = (head)->first; p && ({ n = (p)->next; 1; }); p = n)
484 
485 #define	hlist_entry_safe(ptr, type, member) \
486 	((ptr) ? hlist_entry(ptr, type, member) : NULL)
487 
488 #define	hlist_for_each_entry(pos, head, member)				\
489 	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
490 	     pos;							\
491 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
492 
493 #define	hlist_for_each_entry_continue(pos, member)			\
494 	for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); \
495 	     (pos);							\
496 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
497 
498 #define	hlist_for_each_entry_from(pos, member)				\
499 	for (; (pos);								\
500 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
501 
502 #define	hlist_for_each_entry_safe(pos, n, head, member)			\
503 	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \
504 	     (pos) && ({ n = (pos)->member.next; 1; });			\
505 	     pos = hlist_entry_safe(n, typeof(*(pos)), member))
506 
507 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 51300
508 extern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
509     const struct list_head *a, const struct list_head *b));
510 #else
511 extern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
512     struct list_head *a, struct list_head *b));
513 #endif
514 
515 #endif /* _LINUXKPI_LINUX_LIST_H_ */
516