1285809Sscottl/*******************************************************************************
2285809Sscottl*Copyright (c) 2014 PMC-Sierra, Inc.  All rights reserved.
3285809Sscottl*
4285809Sscottl*Redistribution and use in source and binary forms, with or without modification, are permitted provided
5285809Sscottl*that the following conditions are met:
6285809Sscottl*1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
7285809Sscottl*following disclaimer.
8285809Sscottl*2. Redistributions in binary form must reproduce the above copyright notice,
9285809Sscottl*this list of conditions and the following disclaimer in the documentation and/or other materials provided
10285809Sscottl*with the distribution.
11285809Sscottl*
12285809Sscottl*THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
13285809Sscottl*WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14285809Sscottl*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
15285809Sscottl*FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
16285809Sscottl*NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
17285809Sscottl*BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18285809Sscottl*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
19285809Sscottl*SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
20285809Sscottl*
21285809Sscottl* $FreeBSD$
22285809Sscottl*
23285809Sscottl********************************************************************************/
24285809Sscottl/*******************************************************************************/
25285809Sscottl/** \file
26285809Sscottl *
27285809Sscottl * The file defines list data structures for SAS/SATA TD layer
28285809Sscottl *
29285809Sscottl */
30285809Sscottl
31285809Sscottl#ifndef __TDLIST_H__
32285809Sscottl#define __TDLIST_H__
33285809Sscottl
34285809Sscottl
35285809Sscottltypedef struct tdList_s tdList_t;
36285809Sscottl
37285809Sscottlstruct tdList_s {
38285809Sscottl  tdList_t  *flink;
39285809Sscottl  tdList_t  *blink;
40285809Sscottl};
41285809Sscottl
42285809Sscottl#define TDLIST_NEXT_ENTRY(ptr, type, member)        \
43285809Sscottl  container_of((ptr)->flink, type, member)
44285809Sscottl
45285809Sscottl#define TDLIST_INIT_HDR(hdr)                        \
46285809Sscottl  do {                                              \
47285809Sscottl    ((tdList_t *)(hdr))->flink = (tdList_t *)(hdr); \
48285809Sscottl    ((tdList_t *)(hdr))->blink = (tdList_t *)(hdr); \
49285809Sscottl  } while (0)
50285809Sscottl
51285809Sscottl#define TDLIST_INIT_ELEMENT(hdr)                     \
52285809Sscottl  do {                                               \
53285809Sscottl    ((tdList_t *)(hdr))->flink = (tdList_t *)agNULL; \
54285809Sscottl    ((tdList_t *)(hdr))->blink = (tdList_t *)agNULL; \
55285809Sscottl  } while (0)
56285809Sscottl
57285809Sscottl#define TDLIST_ENQUEUE_AT_HEAD(toAddHdr,listHdr)                                \
58285809Sscottl  do {                                                                          \
59285809Sscottl    ((tdList_t *)(toAddHdr))->flink           = ((tdList_t *)(listHdr))->flink; \
60285809Sscottl    ((tdList_t *)(toAddHdr))->blink           = (tdList_t *)(listHdr) ;         \
61285809Sscottl    ((tdList_t *)(listHdr))->flink->blink     = (tdList_t *)(toAddHdr);         \
62285809Sscottl    ((tdList_t *)(listHdr))->flink            = (tdList_t *)(toAddHdr);         \
63285809Sscottl  } while (0)
64285809Sscottl
65285809Sscottl#define TDLIST_ENQUEUE_AT_TAIL(toAddHdr,listHdr)                                \
66285809Sscottl  do {                                                                          \
67285809Sscottl    ((tdList_t *)(toAddHdr))->flink           = (tdList_t *)(listHdr);          \
68285809Sscottl    ((tdList_t *)(toAddHdr))->blink           = ((tdList_t *)(listHdr))->blink; \
69285809Sscottl    ((tdList_t *)(listHdr))->blink->flink     = (tdList_t *)(toAddHdr);         \
70285809Sscottl    ((tdList_t *)(listHdr))->blink            = (tdList_t *)(toAddHdr);         \
71285809Sscottl  } while (0)
72285809Sscottl
73285809Sscottl#define TDLIST_EMPTY(listHdr) \
74285809Sscottl  (((tdList_t *)(listHdr))->flink == ((tdList_t *)(listHdr)))
75285809Sscottl
76285809Sscottl#define TDLIST_NOT_EMPTY(listHdr) \
77285809Sscottl  (!TDLIST_EMPTY(listHdr))
78285809Sscottl
79285809Sscottl#define TDLIST_DEQUEUE_THIS(hdr)                                      \
80285809Sscottl  do {                                                                \
81285809Sscottl    ((tdList_t *)(hdr))->blink->flink = ((tdList_t *)(hdr))->flink;   \
82285809Sscottl    ((tdList_t *)(hdr))->flink->blink = ((tdList_t *)(hdr))->blink;   \
83285809Sscottl    ((tdList_t *)(hdr))->flink = ((tdList_t *)(hdr))->blink = agNULL; \
84285809Sscottl  } while (0)
85285809Sscottl
86285809Sscottl#define TDLIST_DEQUEUE_FROM_HEAD_FAST(atHeadHdr,listHdr)                              \
87285809Sscottl  do {                                                                                \
88285809Sscottl    *((tdList_t **)(atHeadHdr))                 = ((tdList_t *)(listHdr))->flink;     \
89285809Sscottl    (*((tdList_t **)(atHeadHdr)))->flink->blink = (tdList_t *)(listHdr);              \
90285809Sscottl    ((tdList_t *)(listHdr))->flink              = (*(tdList_t **)(atHeadHdr))->flink; \
91285809Sscottl  } while (0)
92285809Sscottl
93285809Sscottl#define TDLIST_DEQUEUE_FROM_HEAD(atHeadHdr,listHdr)             \
94285809Sscottldo {                                                            \
95285809Sscottl  if (TDLIST_NOT_EMPTY((listHdr)))                              \
96285809Sscottl  {                                                             \
97285809Sscottl    TDLIST_DEQUEUE_FROM_HEAD_FAST(atHeadHdr,listHdr);           \
98285809Sscottl  }                                                             \
99285809Sscottl  else                                                          \
100285809Sscottl  {                                                             \
101285809Sscottl    (*((tdList_t **)(atHeadHdr))) = (tdList_t *)agNULL;         \
102285809Sscottl  }                                                             \
103285809Sscottl} while (0)
104285809Sscottl
105285809Sscottl#define TDLIST_DEQUEUE_FROM_TAIL_FAST(atTailHdr,listHdr)                                \
106285809Sscottl  do {                                                                                  \
107285809Sscottl    (*((tdList_t **)(atTailHdr)))               = ((tdList_t *)(listHdr))->blink;       \
108285809Sscottl    (*((tdList_t **)(atTailHdr)))->blink->flink = (tdList_t *)(listHdr);                \
109285809Sscottl    ((tdList_t *)(listHdr))->blink              = (*((tdList_t **)(atTailHdr)))->blink; \
110285809Sscottl  } while (0)
111285809Sscottl
112285809Sscottl#define TDLIST_DEQUEUE_FROM_TAIL(atTailHdr,listHdr)               \
113285809Sscottl  do {                                                            \
114285809Sscottl    if (TDLIST_NOT_EMPTY((listHdr)))                              \
115285809Sscottl    {                                                             \
116285809Sscottl      TDLIST_DEQUEUE_FROM_TAIL_FAST(atTailHdr,listHdr);           \
117285809Sscottl    }                                                             \
118285809Sscottl    else                                                          \
119285809Sscottl    {                                                             \
120285809Sscottl      (*((tdList_t **)(atTailHdr))) = (tdList_t *)agNULL;         \
121285809Sscottl    }                                                             \
122285809Sscottl  } while (0)
123285809Sscottl
124285809Sscottl#define TDLIST_ENQUEUE_LIST_AT_TAIL_FAST(toAddListHdr, listHdr)               \
125285809Sscottl  do {                                                                        \
126285809Sscottl    ((tdList_t *)toAddListHdr)->blink->flink = ((tdList_t *)listHdr);         \
127285809Sscottl    ((tdList_t *)toAddListHdr)->flink->blink = ((tdList_t *)listHdr)->blink;  \
128285809Sscottl    ((tdList_t *)listHdr)->blink->flink = ((tdList_t *)toAddListHdr)->flink;  \
129285809Sscottl    ((tdList_t *)listHdr)->blink = ((tdList_t *)toAddListHdr)->blink;         \
130285809Sscottl    TDLIST_INIT_HDR(toAddListHdr);                                            \
131285809Sscottl  } while (0)
132285809Sscottl
133285809Sscottl#define TDLIST_ENQUEUE_LIST_AT_TAIL(toAddListHdr, listHdr)                    \
134285809Sscottl  do {                                                                        \
135285809Sscottl    if (TDLIST_NOT_EMPTY(toAddListHdr))                                       \
136285809Sscottl    {                                                                         \
137285809Sscottl      TDLIST_ENQUEUE_LIST_AT_TAIL_FAST(toAddListHdr, listHdr);                \
138285809Sscottl    }                                                                         \
139285809Sscottl  } while (0)
140285809Sscottl
141285809Sscottl#define TDLIST_ENQUEUE_LIST_AT_HEAD_FAST(toAddListHdr, listHdr)               \
142285809Sscottl  do {                                                                        \
143285809Sscottl    ((tdList_t *)toAddListHdr)->blink->flink = ((tdList_t *)listHdr)->flink;  \
144285809Sscottl    ((tdList_t *)toAddListHdr)->flink->blink = ((tdList_t *)listHdr);         \
145285809Sscottl    ((tdList_t *)listHdr)->flink->blink = ((tdList_t *)toAddListHdr)->blink;  \
146285809Sscottl    ((tdList_t *)listHdr)->flink = ((tdList_t *)toAddListHdr)->flink;         \
147285809Sscottl    TDLIST_INIT_HDR(toAddListHdr);                                            \
148285809Sscottl  } while (0)
149285809Sscottl
150285809Sscottl#define TDLIST_ENQUEUE_LIST_AT_HEAD(toAddListHdr, listHdr)                    \
151285809Sscottl  do {                                                                        \
152285809Sscottl    if (TDLIST_NOT_EMPTY(toAddListHdr))                                       \
153285809Sscottl    {                                                                         \
154285809Sscottl      TDLIST_ENQUEUE_LIST_AT_HEAD_FAST(toAddListHdr, listHdr);                \
155285809Sscottl    }                                                                         \
156285809Sscottl  } while (0)
157285809Sscottl
158285809Sscottl#define TD_FIELD_OFFSET(baseType,fieldName) \
159285809Sscottl                    ((bit32)((bitptr)(&(((baseType *)0)->fieldName))))
160285809Sscottl
161285809Sscottl#define TDLIST_OBJECT_BASE(baseType,fieldName,fieldPtr)         \
162285809Sscottl                    (void *)fieldPtr == (void *)0 ? (baseType *)0 :             \
163285809Sscottl                    ((baseType *)((bit8 *)(fieldPtr) - ((bitptr)(&(((baseType *)0)->fieldName)))))
164285809Sscottl
165285809Sscottl
166285809Sscottl
167285809Sscottl#endif /* __TDLIST_H__ */
168285809Sscottl
169