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 static inline void 90 INIT_LIST_HEAD(struct list_head *list) 91 { 92 93 list->next = list->prev = list; 94 } 95 96 static inline int 97 list_empty(const struct list_head *head) 98 { 99 100 return (head->next == head); 101 } 102 103 static inline int 104 list_empty_careful(const struct list_head *head) 105 { 106 struct list_head *next = head->next; 107 108 return ((next == head) && (next == head->prev)); 109 } 110 111 static inline void 112 __list_del(struct list_head *prev, struct list_head *next) 113 { 114 next->prev = prev; 115 WRITE_ONCE(prev->next, next); 116 } 117 118 static inline void 119 __list_del_entry(struct list_head *entry) 120 { 121 122 __list_del(entry->prev, entry->next); 123 } 124 125 static inline void 126 list_del(struct list_head *entry) 127 { 128 129 __list_del(entry->prev, entry->next); 130 } 131 132 static inline void 133 list_replace(struct list_head *old, struct list_head *new) 134 { 135 new->next = old->next; 136 new->next->prev = new; 137 new->prev = old->prev; 138 new->prev->next = new; 139 } 140 141 static inline void 142 list_replace_init(struct list_head *old, struct list_head *new) 143 { 144 list_replace(old, new); 145 INIT_LIST_HEAD(old); 146 } 147 148 static inline void 149 linux_list_add(struct list_head *new, struct list_head *prev, 150 struct list_head *next) 151 { 152 153 next->prev = new; 154 new->next = next; 155 new->prev = prev; 156 prev->next = new; 157 } 158 159 static inline void 160 list_del_init(struct list_head *entry) 161 { 162 163 list_del(entry); 164 INIT_LIST_HEAD(entry); 165 } 166 167 #define list_entry(ptr, type, field) container_of(ptr, type, field) 168 169 #define list_first_entry(ptr, type, member) \ 170 list_entry((ptr)->next, type, member) 171 172 #define list_last_entry(ptr, type, member) \ 173 list_entry((ptr)->prev, type, member) 174 175 #define list_first_entry_or_null(ptr, type, member) \ 176 (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL) 177 178 #define list_next_entry(ptr, member) \ 179 list_entry(((ptr)->member.next), typeof(*(ptr)), member) 180 181 #define list_safe_reset_next(ptr, n, member) \ 182 (n) = list_next_entry(ptr, member) 183 184 #define list_prev_entry(ptr, member) \ 185 list_entry(((ptr)->member.prev), typeof(*(ptr)), member) 186 187 #define list_for_each(p, head) \ 188 for (p = (head)->next; p != (head); p = (p)->next) 189 190 #define list_for_each_safe(p, n, head) \ 191 for (p = (head)->next, n = (p)->next; p != (head); p = n, n = (p)->next) 192 193 #define list_for_each_entry(p, h, field) \ 194 for (p = list_entry((h)->next, typeof(*p), field); &(p)->field != (h); \ 195 p = list_entry((p)->field.next, typeof(*p), field)) 196 197 #define list_for_each_entry_safe(p, n, h, field) \ 198 for (p = list_entry((h)->next, typeof(*p), field), \ 199 n = list_entry((p)->field.next, typeof(*p), field); &(p)->field != (h);\ 200 p = n, n = list_entry(n->field.next, typeof(*n), field)) 201 202 #define list_for_each_entry_from(p, h, field) \ 203 for ( ; &(p)->field != (h); \ 204 p = list_entry((p)->field.next, typeof(*p), field)) 205 206 #define list_for_each_entry_continue(p, h, field) \ 207 for (p = list_next_entry((p), field); &(p)->field != (h); \ 208 p = list_next_entry((p), field)) 209 210 #define list_for_each_entry_safe_from(pos, n, head, member) \ 211 for (n = list_entry((pos)->member.next, typeof(*pos), member); \ 212 &(pos)->member != (head); \ 213 pos = n, n = list_entry(n->member.next, typeof(*n), member)) 214 215 #define list_for_each_entry_reverse(p, h, field) \ 216 for (p = list_entry((h)->prev, typeof(*p), field); &(p)->field != (h); \ 217 p = list_entry((p)->field.prev, typeof(*p), field)) 218 219 #define list_for_each_entry_safe_reverse(p, n, h, field) \ 220 for (p = list_entry((h)->prev, typeof(*p), field), \ 221 n = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \ 222 p = n, n = list_entry(n->field.prev, typeof(*n), field)) 223 224 #define list_for_each_entry_continue_reverse(p, h, field) \ 225 for (p = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \ 226 p = list_entry((p)->field.prev, typeof(*p), field)) 227 228 #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = (p)->prev) 229 230 #define list_for_each_entry_from_reverse(p, h, field) \ 231 for (; &p->field != (h); \ 232 p = list_prev_entry(p, field)) 233 234 static inline void 235 list_add(struct list_head *new, struct list_head *head) 236 { 237 238 linux_list_add(new, head, head->next); 239 } 240 241 static inline void 242 list_add_tail(struct list_head *new, struct list_head *head) 243 { 244 245 linux_list_add(new, head->prev, head); 246 } 247 248 static inline void 249 list_move(struct list_head *list, struct list_head *head) 250 { 251 252 list_del(list); 253 list_add(list, head); 254 } 255 256 static inline void 257 list_move_tail(struct list_head *entry, struct list_head *head) 258 { 259 260 list_del(entry); 261 list_add_tail(entry, head); 262 } 263 264 static inline void 265 list_rotate_to_front(struct list_head *entry, struct list_head *head) 266 { 267 268 list_move_tail(entry, head); 269 } 270 271 static inline void 272 list_bulk_move_tail(struct list_head *head, struct list_head *first, 273 struct list_head *last) 274 { 275 first->prev->next = last->next; 276 last->next->prev = first->prev; 277 head->prev->next = first; 278 first->prev = head->prev; 279 last->next = head; 280 head->prev = last; 281 } 282 283 static inline void 284 linux_list_splice(const struct list_head *list, struct list_head *prev, 285 struct list_head *next) 286 { 287 struct list_head *first; 288 struct list_head *last; 289 290 if (list_empty(list)) 291 return; 292 first = list->next; 293 last = list->prev; 294 first->prev = prev; 295 prev->next = first; 296 last->next = next; 297 next->prev = last; 298 } 299 300 static inline void 301 list_splice(const struct list_head *list, struct list_head *head) 302 { 303 304 linux_list_splice(list, head, head->next); 305 } 306 307 static inline void 308 list_splice_tail(struct list_head *list, struct list_head *head) 309 { 310 311 linux_list_splice(list, head->prev, head); 312 } 313 314 static inline void 315 list_splice_init(struct list_head *list, struct list_head *head) 316 { 317 318 linux_list_splice(list, head, head->next); 319 INIT_LIST_HEAD(list); 320 } 321 322 static inline void 323 list_splice_tail_init(struct list_head *list, struct list_head *head) 324 { 325 326 linux_list_splice(list, head->prev, head); 327 INIT_LIST_HEAD(list); 328 } 329 330 #undef LIST_HEAD 331 #define LIST_HEAD(name) struct list_head name = { &(name), &(name) } 332 333 struct hlist_head { 334 struct hlist_node *first; 335 }; 336 337 struct hlist_node { 338 struct hlist_node *next, **pprev; 339 }; 340 #define HLIST_HEAD_INIT { } 341 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT 342 #define INIT_HLIST_HEAD(head) (head)->first = NULL 343 #define INIT_HLIST_NODE(node) \ 344 do { \ 345 (node)->next = NULL; \ 346 (node)->pprev = NULL; \ 347 } while (0) 348 349 static inline int 350 hlist_unhashed(const struct hlist_node *h) 351 { 352 353 return !h->pprev; 354 } 355 356 static inline int 357 hlist_empty(const struct hlist_head *h) 358 { 359 360 return !READ_ONCE(h->first); 361 } 362 363 static inline void 364 hlist_del(struct hlist_node *n) 365 { 366 367 WRITE_ONCE(*(n->pprev), n->next); 368 if (n->next != NULL) 369 n->next->pprev = n->pprev; 370 } 371 372 static inline void 373 hlist_del_init(struct hlist_node *n) 374 { 375 376 if (hlist_unhashed(n)) 377 return; 378 hlist_del(n); 379 INIT_HLIST_NODE(n); 380 } 381 382 static inline void 383 hlist_add_head(struct hlist_node *n, struct hlist_head *h) 384 { 385 386 n->next = h->first; 387 if (h->first != NULL) 388 h->first->pprev = &n->next; 389 WRITE_ONCE(h->first, n); 390 n->pprev = &h->first; 391 } 392 393 static inline void 394 hlist_add_before(struct hlist_node *n, struct hlist_node *next) 395 { 396 397 n->pprev = next->pprev; 398 n->next = next; 399 next->pprev = &n->next; 400 WRITE_ONCE(*(n->pprev), n); 401 } 402 403 static inline void 404 hlist_add_behind(struct hlist_node *n, struct hlist_node *prev) 405 { 406 407 n->next = prev->next; 408 WRITE_ONCE(prev->next, n); 409 n->pprev = &prev->next; 410 411 if (n->next != NULL) 412 n->next->pprev = &n->next; 413 } 414 415 static inline void 416 hlist_move_list(struct hlist_head *old, struct hlist_head *new) 417 { 418 419 new->first = old->first; 420 if (new->first) 421 new->first->pprev = &new->first; 422 old->first = NULL; 423 } 424 425 static inline int list_is_singular(const struct list_head *head) 426 { 427 return !list_empty(head) && (head->next == head->prev); 428 } 429 430 static inline void __list_cut_position(struct list_head *list, 431 struct list_head *head, struct list_head *entry) 432 { 433 struct list_head *new_first = entry->next; 434 list->next = head->next; 435 list->next->prev = list; 436 list->prev = entry; 437 entry->next = list; 438 head->next = new_first; 439 new_first->prev = head; 440 } 441 442 static inline void list_cut_position(struct list_head *list, 443 struct list_head *head, struct list_head *entry) 444 { 445 if (list_empty(head)) 446 return; 447 if (list_is_singular(head) && 448 (head->next != entry && head != entry)) 449 return; 450 if (entry == head) 451 INIT_LIST_HEAD(list); 452 else 453 __list_cut_position(list, head, entry); 454 } 455 456 static inline int list_is_first(const struct list_head *list, 457 const struct list_head *head) 458 { 459 460 return (list->prev == head); 461 } 462 463 static inline int list_is_last(const struct list_head *list, 464 const struct list_head *head) 465 { 466 return list->next == head; 467 } 468 469 #define hlist_entry(ptr, type, field) container_of(ptr, type, field) 470 471 #define hlist_for_each(p, head) \ 472 for (p = (head)->first; p; p = (p)->next) 473 474 #define hlist_for_each_safe(p, n, head) \ 475 for (p = (head)->first; p && ({ n = (p)->next; 1; }); p = n) 476 477 #define hlist_entry_safe(ptr, type, member) \ 478 ((ptr) ? hlist_entry(ptr, type, member) : NULL) 479 480 #define hlist_for_each_entry(pos, head, member) \ 481 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ 482 pos; \ 483 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 484 485 #define hlist_for_each_entry_continue(pos, member) \ 486 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); \ 487 (pos); \ 488 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 489 490 #define hlist_for_each_entry_from(pos, member) \ 491 for (; (pos); \ 492 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 493 494 #define hlist_for_each_entry_safe(pos, n, head, member) \ 495 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \ 496 (pos) && ({ n = (pos)->member.next; 1; }); \ 497 pos = hlist_entry_safe(n, typeof(*(pos)), member)) 498 499 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 51300 500 extern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv, 501 const struct list_head *a, const struct list_head *b)); 502 #else 503 extern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv, 504 struct list_head *a, struct list_head *b)); 505 #endif 506 507 #endif /* _LINUXKPI_LINUX_LIST_H_ */ 508