ath9k_htc: Update to upstream's commit d19607454d656cb14d8c16dfbf161eebb542e8fe dated...
[linux-libre-firmware.git] / ath9k_htc / target_firmware / magpie_fw_dev / target / inc / asf_queue.h
1 /*
2  * Copyright (c) 1991, 1993
3  *  The Regents of the University of California.  All rights reserved.
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  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *  @(#)queue.h 8.5 (Berkeley) 8/20/94
30  * $FreeBSD: src/sys/sys/queue.h,v 1.58 2004/04/07 04:19:49 imp Exp $
31  * $Id: //depot/sw/branches/fusion_usb/target_firmware/magpie_fw_dev/asf/include/asf_queue.h#1 $
32  */
33
34 #ifndef _ASF_QUEUE_H_
35 #define _ASF_QUEUE_H_
36
37 /**
38  * General purpose routines
39  */
40 #define asf_offsetof(type, member) ((adf_os_size_t) &((type *)0)->member)
41
42 #define asf_containerof(ptr, type, member) ({           \
43         const typeof( ((type *)0)->member ) *__lptr = (ptr);    \
44         (type *)( (char *)__mptr - asf_offsetof(type,member) );})
45
46 /*
47  * This file defines four types of data structures: singly-linked lists,
48  * singly-linked tail queues, lists and tail queues.
49  *
50  * A singly-linked list is headed by a single forward pointer. The elements
51  * are singly linked for minimum space and pointer manipulation overhead at
52  * the expense of O(n) removal for arbitrary elements. New elements can be
53  * added to the list after an existing element or at the head of the list.
54  * Elements being removed from the head of the list should use the explicit
55  * macro for this purpose for optimum efficiency. A singly-linked list may
56  * only be traversed in the forward direction.  Singly-linked lists are ideal
57  * for applications with large datasets and few or no removals or for
58  * implementing a LIFO queue.
59  *
60  * A singly-linked tail queue is headed by a pair of pointers, one to the
61  * head of the list and the other to the tail of the list. The elements are
62  * singly linked for minimum space and pointer manipulation overhead at the
63  * expense of O(n) removal for arbitrary elements. New elements can be added
64  * to the list after an existing element, at the head of the list, or at the
65  * end of the list. Elements being removed from the head of the tail queue
66  * should use the explicit macro for this purpose for optimum efficiency.
67  * A singly-linked tail queue may only be traversed in the forward direction.
68  * Singly-linked tail queues are ideal for applications with large datasets
69  * and few or no removals or for implementing a FIFO queue.
70  *
71  * A list is headed by a single forward pointer (or an array of forward
72  * pointers for a hash table header). The elements are doubly linked
73  * so that an arbitrary element can be removed without a need to
74  * traverse the list. New elements can be added to the list before
75  * or after an existing element or at the head of the list. A list
76  * may only be traversed in the forward direction.
77  *
78  * A tail queue is headed by a pair of pointers, one to the head of the
79  * list and the other to the tail of the list. The elements are doubly
80  * linked so that an arbitrary element can be removed without a need to
81  * traverse the list. New elements can be added to the list before or
82  * after an existing element, at the head of the list, or at the end of
83  * the list. A tail queue may be traversed in either direction.
84  *
85  * For details on the use of these macros, see the queue(3) manual page.
86  *
87  *
88  *              asf_slist   asf_list    asf_stailq  asf_tailq
89  * _head            +   +   +   +
90  * _head_initializer        +   +   +   +
91  * _entry           +   +   +   +
92  * _init            +   +   +   +
93  * _empty           +   +   +   +
94  * _first           +   +   +   +
95  * _next            +   +   +   +
96  * _prev            -   -   -   +
97  * _last            -   -   +   +
98  * _foreach         +   +   +   +
99  * _foreach_safe        +   +   +   +
100  * _foreach_reverse     -   -   -   +
101  * _foreach_reverse_safe    -   -   -   +
102  * _insert_head         +   +   +   +
103  * _insert_before       -   +   -   +
104  * _insert_after        +   +   +   +
105  * _insert_tail         -   -   +   +
106  * _concat          -   -   +   +
107  * _remove_head         +   -   +   -
108  * _remove          +   +   +   +
109  *
110  */
111 #define QUEUE_MACRO_DEBUG 0
112 #if QUEUE_MACRO_DEBUG
113 /* Store the last 2 places the queue element or head was altered */
114 struct asf_qm_trace {
115     char * lastfile;
116     int lastline;
117     char * prevfile;
118     int prevline;
119 };
120
121 #define TRACEBUF    struct asf_qm_trace trace
122 #define trashit(x)  do {(x) = (void *)-1;} while (0)
123
124 #define qmd_trace_head(head) do {                   \
125     (head)->trace.prevline = (head)->trace.lastline;        \
126     (head)->trace.prevfile = (head)->trace.lastfile;        \
127     (head)->trace.lastline = __LINE__;              \
128     (head)->trace.lastfile = __FILE__;              \
129 } while (0)
130
131 #define qmd_trace_elem(elem) do {                   \
132     (elem)->trace.prevline = (elem)->trace.lastline;        \
133     (elem)->trace.prevfile = (elem)->trace.lastfile;        \
134     (elem)->trace.lastline = __LINE__;              \
135     (elem)->trace.lastfile = __FILE__;              \
136 } while (0)
137
138 #else
139 #define qmd_trace_elem(elem)
140 #define qmd_trace_head(head)
141 #define TRACEBUF
142 #define trashit(x)
143 #endif  /* QUEUE_MACRO_DEBUG */
144
145 /*
146  * Singly-linked List declarations.
147  */
148 #define asf_slist_head(name, type)                      \
149 struct name {                               \
150     struct type *slh_first; /* first element */         \
151 }
152
153 #define asf_slist_head_initializer(head)                    \
154     { NULL }
155
156 #define asf_slist_entry(type)                       \
157 struct {                                \
158     struct type *sle_next;  /* next element */          \
159 }
160
161 /*
162  * Singly-linked List functions.
163  */
164 #define asf_slist_empty(head)   ((head)->slh_first == NULL)
165
166 #define asf_slist_first(head)   ((head)->slh_first)
167
168 #define asf_slist_foreach(var, head, field)                 \
169     for ((var) = asf_slist_first((head));               \
170         (var);                          \
171         (var) = asf_slist_next((var), field))
172
173 #define asf_slist_foreach_safe(var, head, field, tvar)          \
174     for ((var) = asf_slist_first((head));               \
175         (var) && ((tvar) = asf_slist_next((var), field), 1);        \
176         (var) = (tvar))
177
178 #define asf_slist_foreach_prevptr(var, varp, head, field)           \
179     for ((varp) = &asf_slist_first((head));             \
180         ((var) = *(varp)) != NULL;                  \
181         (varp) = &asf_slist_next((var), field))
182
183 #define asf_slist_init(head) do {                       \
184     asf_slist_first((head)) = NULL;                 \
185 } while (0)
186
187 #define asf_slist_insert_after(slistelm, elm, field) do {           \
188     asf_slist_next((elm), field) = asf_slist_next((slistelm), field);   \
189     asf_slist_next((slistelm), field) = (elm);              \
190 } while (0)
191
192 #define asf_slist_insert_head(head, elm, field) do {            \
193     asf_slist_next((elm), field) = asf_slist_first((head));         \
194     asf_slist_first((head)) = (elm);                    \
195 } while (0)
196
197 #define asf_slist_next(elm, field)  ((elm)->field.sle_next)
198
199 #define asf_slist_remove(head, elm, type, field) do {           \
200     if (asf_slist_first((head)) == (elm)) {             \
201         asf_slist_remove_head((head), field);           \
202     }                               \
203     else {                              \
204         struct type *curelm = asf_slist_first((head));      \
205         while (asf_slist_next(curelm, field) != (elm))      \
206             curelm = asf_slist_next(curelm, field);     \
207         asf_slist_next(curelm, field) =             \
208             asf_slist_next(asf_slist_next(curelm, field), field);   \
209     }                               \
210 } while (0)
211
212 #define asf_slist_remove_head(head, field) do {             \
213     asf_slist_first((head)) = asf_slist_next(asf_slist_first((head)), field);   \
214 } while (0)
215
216 /*
217  * Singly-linked Tail queue declarations.
218  */
219 #define asf_stailq_head(name, type)                     \
220 struct name {                               \
221     struct type *stqh_first;/* first element */         \
222     struct type **stqh_last;/* addr of last next element */     \
223 }
224
225 #define asf_stailq_head_initializer(head)                   \
226     { NULL, &(head).stqh_first }
227
228 #define asf_stailq_entry(type)                      \
229 struct {                                \
230     struct type *stqe_next; /* next element */          \
231 }
232
233 /*
234  * Singly-linked Tail queue functions.
235  */
236 #define asf_stailq_concat(head1, head2) do {                \
237     if (!asf_stailq_empty((head2))) {                   \
238         *(head1)->stqh_last = (head2)->stqh_first;      \
239         (head1)->stqh_last = (head2)->stqh_last;        \
240         asf_stailq_init((head2));                   \
241     }                               \
242 } while (0)
243
244 #define asf_stailq_empty(head)  ((head)->stqh_first == NULL)
245
246 #define asf_stailq_first(head)  ((head)->stqh_first)
247
248 #define asf_stailq_foreach(var, head, field)                \
249     for((var) = asf_stailq_first((head));               \
250        (var);                           \
251        (var) = asf_stailq_next((var), field))
252
253
254 #define asf_stailq_foreach_safe(var, head, field, tvar)         \
255     for ((var) = asf_stailq_first((head));              \
256         (var) && ((tvar) = asf_stailq_next((var), field), 1);       \
257         (var) = (tvar))
258
259 #define asf_stailq_init(head) do {                      \
260     asf_stailq_first((head)) = NULL;                    \
261     (head)->stqh_last = &asf_stailq_first((head));          \
262 } while (0)
263
264 #define asf_stailq_insert_after(head, tqelm, elm, field) do {       \
265     if ((asf_stailq_next((elm), field) = asf_stailq_next((tqelm), field)) == NULL)\
266         (head)->stqh_last = &asf_stailq_next((elm), field);     \
267     asf_stailq_next((tqelm), field) = (elm);                \
268 } while (0)
269
270 #define asf_stailq_insert_head(head, elm, field) do {           \
271     if ((asf_stailq_next((elm), field) = asf_stailq_first((head))) == NULL) \
272         (head)->stqh_last = &asf_stailq_next((elm), field);     \
273     asf_stailq_first((head)) = (elm);                   \
274 } while (0)
275
276 #define asf_stailq_insert_tail(head, elm, field) do {           \
277     asf_stailq_next((elm), field) = NULL;               \
278     *(head)->stqh_last = (elm);                 \
279     (head)->stqh_last = &asf_stailq_next((elm), field);         \
280 } while (0)
281
282 #define asf_stailq_last(head, type, field)                  \
283     (asf_stailq_empty((head)) ?                     \
284         NULL :                          \
285             ((struct type *)                    \
286         ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
287
288 #define asf_stailq_next(elm, field) ((elm)->field.stqe_next)
289
290 #define asf_stailq_remove(head, elm, type, field) do {          \
291     if (asf_stailq_first((head)) == (elm)) {                \
292         asf_stailq_remove_head((head), field);          \
293     }                               \
294     else {                              \
295         struct type *curelm = asf_stailq_first((head));     \
296         while (asf_stailq_next(curelm, field) != (elm))     \
297             curelm = asf_stailq_next(curelm, field);        \
298         if ((asf_stailq_next(curelm, field) =           \
299              asf_stailq_next(asf_stailq_next(curelm, field), field)) == NULL)\
300             (head)->stqh_last = &asf_stailq_next((curelm), field);\
301     }                               \
302 } while (0)
303
304
305 #define asf_stailq_remove_after(head, elm, field) do {          \
306     if (asf_stailq_next(elm, field)) {      \
307         if ((asf_stailq_next(elm, field) =          \
308             asf_stailq_next(asf_stailq_next(elm, field), field)) == NULL)\
309             (head)->stqh_last = &asf_stailq_next((elm), field); \
310     }                               \
311 } while (0)
312
313
314 #define asf_stailq_remove_head(head, field) do {                \
315     if ((asf_stailq_first((head)) =                 \
316          asf_stailq_next(asf_stailq_first((head)), field)) == NULL)     \
317         (head)->stqh_last = &asf_stailq_first((head));      \
318 } while (0)
319
320 #define asf_stailq_remove_head_until(head, elm, field) do {         \
321     if ((asf_stailq_first((head)) = asf_stailq_next((elm), field)) == NULL) \
322         (head)->stqh_last = &asf_stailq_first((head));      \
323 } while (0)
324
325 /*
326  * List declarations.
327  */
328 #define asf_list_head(name, type)                   \
329 struct name {                               \
330     struct type *lh_first;  /* first element */         \
331 }
332
333 #define asf_list_head_initializer(head)                 \
334     { NULL }
335
336 #define asf_list_entry(type)                        \
337 struct {                                \
338     struct type *le_next;   /* next element */          \
339     struct type **le_prev;  /* address of previous next element */  \
340 }
341
342 /*
343  * List functions.
344  */
345
346 #define asf_list_empty(head)    ((head)->lh_first == NULL)
347
348 #define asf_list_first(head)    ((head)->lh_first)
349
350 #define asf_list_foreach(var, head, field)                  \
351     for ((var) = asf_list_first((head));                \
352         (var);                          \
353         (var) = asf_list_next((var), field))
354
355 #define asf_list_foreach_safe(var, head, field, tvar)           \
356     for ((var) = asf_list_first((head));                \
357         (var) && ((tvar) = asf_list_next((var), field), 1);     \
358         (var) = (tvar))
359
360 #define asf_list_init(head) do {                        \
361     asf_list_first((head)) = NULL;                  \
362 } while (0)
363
364 #define asf_list_insert_after(listelm, elm, field) do {         \
365     if ((asf_list_next((elm), field) = asf_list_next((listelm), field)) != NULL)\
366         asf_list_next((listelm), field)->field.le_prev =        \
367             &asf_list_next((elm), field);               \
368     asf_list_next((listelm), field) = (elm);                \
369     (elm)->field.le_prev = &asf_list_next((listelm), field);        \
370 } while (0)
371
372 #define asf_list_insert_before(listelm, elm, field) do {            \
373     (elm)->field.le_prev = (listelm)->field.le_prev;        \
374     asf_list_next((elm), field) = (listelm);                \
375     *(listelm)->field.le_prev = (elm);              \
376     (listelm)->field.le_prev = &asf_list_next((elm), field);        \
377 } while (0)
378
379 #define asf_list_insert_head(head, elm, field) do {             \
380     if ((asf_list_next((elm), field) = asf_list_first((head))) != NULL) \
381         asf_list_first((head))->field.le_prev = &asf_list_next((elm), field);\
382     asf_list_first((head)) = (elm);                 \
383     (elm)->field.le_prev = &asf_list_first((head));         \
384 } while (0)
385
386 #define asf_list_next(elm, field)   ((elm)->field.le_next)
387
388 #define asf_list_remove(elm, field) do {                    \
389     if (asf_list_next((elm), field) != NULL)                \
390         asf_list_next((elm), field)->field.le_prev =        \
391             (elm)->field.le_prev;               \
392     *(elm)->field.le_prev = asf_list_next((elm), field);        \
393 } while (0)
394
395 /*
396  * Tail queue declarations.
397  */
398 #define asf_tailq_head(name, type)                      \
399 struct name {                               \
400     struct type *tqh_first; /* first element */         \
401     struct type **tqh_last; /* addr of last next element */     \
402     TRACEBUF;                           \
403 }
404
405 #define asf_tailq_head_initializer(head)                    \
406     { NULL, &(head).tqh_first }
407
408 #define asf_tailq_entry(type)                       \
409 struct {                                \
410     struct type *tqe_next;  /* next element */          \
411     struct type **tqe_prev; /* address of previous next element */  \
412     TRACEBUF;                           \
413 }
414
415 /*
416  * Tail queue functions.
417  */
418 #define asf_tailq_concat(head1, head2, field) do {              \
419     if (!asf_tailq_empty(head2)) {                  \
420         *(head1)->tqh_last = (head2)->tqh_first;        \
421         (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
422         (head1)->tqh_last = (head2)->tqh_last;          \
423         asf_tailq_init((head2));                    \
424         qmd_trace_head(head);                   \
425         qmd_trace_head(head2);                  \
426     }                               \
427 } while (0)
428
429 #define asf_tailq_empty(head)   ((head)->tqh_first == NULL)
430
431 #define asf_tailq_first(head)   ((head)->tqh_first)
432
433 #define asf_tailq_foreach(var, head, field)                 \
434     for ((var) = asf_tailq_first((head));               \
435         (var);                          \
436         (var) = asf_tailq_next((var), field))
437
438 #define asf_tailq_foreach_safe(var, head, field, tvar)          \
439     for ((var) = asf_tailq_first((head));               \
440         (var) && ((tvar) = asf_tailq_next((var), field), 1);        \
441         (var) = (tvar))
442
443 #define asf_tailq_foreach_reverse(var, head, headname, field)       \
444     for ((var) = asf_tailq_last((head), headname);          \
445         (var);                          \
446         (var) = asf_tailq_prev((var), headname, field))
447
448 #define asf_tailq_foreach_reverse_safe(var, head, headname, field, tvar)    \
449     for ((var) = asf_tailq_last((head), headname);          \
450         (var) && ((tvar) = asf_tailq_prev((var), headname, field), 1);  \
451         (var) = (tvar))
452
453 #define asf_tailq_init(head) do {                       \
454     asf_tailq_first((head)) = NULL;                 \
455     (head)->tqh_last = &asf_tailq_first((head));            \
456     qmd_trace_head(head);                       \
457 } while (0)
458
459 #define asf_tailq_insert_after(head, listelm, elm, field) do {      \
460     if ((asf_tailq_next((elm), field) = asf_tailq_next((listelm), field)) != NULL)\
461         asf_tailq_next((elm), field)->field.tqe_prev =      \
462             &asf_tailq_next((elm), field);              \
463     else {                              \
464         (head)->tqh_last = &asf_tailq_next((elm), field);       \
465         qmd_trace_head(head);                   \
466     }                               \
467     asf_tailq_next((listelm), field) = (elm);               \
468     (elm)->field.tqe_prev = &asf_tailq_next((listelm), field);      \
469     qmd_trace_elem(&(elm)->field);                  \
470     qmd_trace_elem(&listelm->field);                \
471 } while (0)
472
473 #define asf_tailq_insert_before(listelm, elm, field) do {           \
474     (elm)->field.tqe_prev = (listelm)->field.tqe_prev;      \
475     asf_tailq_next((elm), field) = (listelm);               \
476     *(listelm)->field.tqe_prev = (elm);             \
477     (listelm)->field.tqe_prev = &asf_tailq_next((elm), field);      \
478     qmd_trace_elem(&(elm)->field);                  \
479     qmd_trace_elem(&listelm->field);                \
480 } while (0)
481
482 #define asf_tailq_insert_head(head, elm, field) do {            \
483     if ((asf_tailq_next((elm), field) = asf_tailq_first((head))) != NULL)   \
484         asf_tailq_first((head))->field.tqe_prev =           \
485             &asf_tailq_next((elm), field);              \
486     else                                \
487         (head)->tqh_last = &asf_tailq_next((elm), field);       \
488     asf_tailq_first((head)) = (elm);                    \
489     (elm)->field.tqe_prev = &asf_tailq_first((head));           \
490     qmd_trace_head(head);                       \
491     qmd_trace_elem(&(elm)->field);                  \
492 } while (0)
493
494 #define asf_tailq_insert_tail(head, elm, field) do {            \
495     asf_tailq_next((elm), field) = NULL;                \
496     (elm)->field.tqe_prev = (head)->tqh_last;           \
497     *(head)->tqh_last = (elm);                  \
498     (head)->tqh_last = &asf_tailq_next((elm), field);           \
499     qmd_trace_head(head);                       \
500     qmd_trace_elem(&(elm)->field);                  \
501 } while (0)
502
503 #define asf_tailq_last(head, headname)                  \
504     (*(((struct headname *)((head)->tqh_last))->tqh_last))
505
506 #define asf_tailq_next(elm, field) ((elm)->field.tqe_next)
507
508 #define asf_tailq_prev(elm, headname, field)                \
509     (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
510
511 #define asf_tailq_remove(head, elm, field) do {             \
512     if ((asf_tailq_next((elm), field)) != NULL)             \
513         asf_tailq_next((elm), field)->field.tqe_prev =      \
514             (elm)->field.tqe_prev;              \
515     else {                              \
516         (head)->tqh_last = (elm)->field.tqe_prev;       \
517         qmd_trace_head(head);                   \
518     }                               \
519     *(elm)->field.tqe_prev = asf_tailq_next((elm), field);      \
520     trashit((elm)->field.tqe_next);                 \
521     trashit((elm)->field.tqe_prev);                 \
522     qmd_trace_elem(&(elm)->field);                  \
523 } while (0)
524
525
526 #ifdef _KERNEL
527
528 /*
529  * XXX asf_insque() and remque() are an old way of handling certain queues.
530  * They bogusly assumes that all queue heads look alike.
531  */
532
533 struct asf_qhead {
534     struct asf_qhead *qh_link;
535     struct asf_qhead *qh_rlink;
536 };
537
538 #if defined(__GNUC__) || defined(__INTEL_COMPILER)
539
540 static __inline void
541 asf_insque(void *a, void *b)
542 {
543     struct asf_qhead *element = (struct asf_qhead *)a,
544          *head = (struct asf_qhead *)b;
545
546     element->qh_link = head->qh_link;
547     element->qh_rlink = head;
548     head->qh_link = element;
549     element->qh_link->qh_rlink = element;
550 }
551
552 static __inline void
553 asf_remque(void *a)
554 {
555     struct asf_qhead *element = (struct asf_qhead *)a;
556
557     element->qh_link->qh_rlink = element->qh_rlink;
558     element->qh_rlink->qh_link = element->qh_link;
559     element->qh_rlink = 0;
560 }
561
562 #else /* !(__GNUC__ || __INTEL_COMPILER) */
563
564 void    asf_insque(void *a, void *b);
565 void    asf_remque(void *a);
566
567 #endif /* __GNUC__ || __INTEL_COMPILER */
568
569 #endif /* _KERNEL */
570
571 #endif /* !_ASF_QUEUE_H_ */