isci: Intel(R) C600 Series Chipset Storage Control Unit Driver
[linux-flexiantxendom0-3.2.10.git] / drivers / scsi / isci / core / sci_pool.h
1 /*
2  * This file is provided under a dual BSD/GPLv2 license.  When using or
3  * redistributing this file, you may do so under either license.
4  *
5  * GPL LICENSE SUMMARY
6  *
7  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21  * The full GNU General Public License is included in this distribution
22  * in the file called LICENSE.GPL.
23  *
24  * BSD LICENSE
25  *
26  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  *
33  *   * Redistributions of source code must retain the above copyright
34  *     notice, this list of conditions and the following disclaimer.
35  *   * Redistributions in binary form must reproduce the above copyright
36  *     notice, this list of conditions and the following disclaimer in
37  *     the documentation and/or other materials provided with the
38  *     distribution.
39  *   * Neither the name of Intel Corporation nor the names of its
40  *     contributors may be used to endorse or promote products derived
41  *     from this software without specific prior written permission.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54  */
55
56 /**
57  * This file contains the interface to the pool class. This class allows two
58  *    different two different priority tasks to insert and remove items from
59  *    the free pool. The user of the pool is expected to evaluate the pool
60  *    condition empty before a get operation and pool condition full before a
61  *    put operation. Methods Provided: - sci_pool_create() -
62  *    sci_pool_initialize() - sci_pool_empty() - sci_pool_full() -
63  *    sci_pool_get() - sci_pool_put()
64  *
65  *
66  */
67
68 #ifndef _SCI_POOL_H_
69 #define _SCI_POOL_H_
70
71 /**
72  * SCI_POOL_INCREMENT() -
73  *
74  * Private operation for the pool
75  */
76 #define SCI_POOL_INCREMENT(this_pool, index) \
77         (((index) + 1) == (this_pool).size ? 0 : (index) + 1)
78
79 /**
80  * SCI_POOL_CREATE() -
81  *
82  * This creates a pool structure of pool_name. The members in the pool are of
83  * type with number of elements equal to size.
84  */
85 #define SCI_POOL_CREATE(pool_name, type, pool_size) \
86         struct \
87         { \
88                 u32 size; \
89                 u32 get; \
90                 u32 put; \
91                 type array[(pool_size) + 1]; \
92         } pool_name
93
94
95 /**
96  * sci_pool_empty() -
97  *
98  * This macro evaluates the pool and returns true if the pool is empty. If the
99  * pool is empty the user should not perform any get operation on the pool.
100  */
101 #define sci_pool_empty(this_pool) \
102         ((this_pool).get == (this_pool).put)
103
104 /**
105  * sci_pool_full() -
106  *
107  * This macro evaluates the pool and returns true if the pool is full.  If the
108  * pool is full the user should not perform any put operation.
109  */
110 #define sci_pool_full(this_pool) \
111         (SCI_POOL_INCREMENT(this_pool, (this_pool).put) == (this_pool).get)
112
113 /**
114  * sci_pool_size() -
115  *
116  * This macro returns the size of the pool created.  The internal size of the
117  * pool is actually 1 larger then necessary in order to ensure get and put
118  * pointers can be written simultaneously by different users.  As a result,
119  * this macro subtracts 1 from the internal size
120  */
121 #define sci_pool_size(this_pool) \
122         ((this_pool).size - 1)
123
124 /**
125  * sci_pool_count() -
126  *
127  * This macro indicates the number of elements currently contained in the pool.
128  */
129 #define sci_pool_count(this_pool) \
130         (\
131                 sci_pool_empty((this_pool)) \
132                 ? 0 \
133                 : (\
134                         sci_pool_full((this_pool)) \
135                         ? sci_pool_size((this_pool)) \
136                         : (\
137                                 (this_pool).get > (this_pool).put \
138                                 ? ((this_pool).size - (this_pool).get + (this_pool).put) \
139                                 : ((this_pool).put - (this_pool).get) \
140                                 ) \
141                         ) \
142         )
143
144 /**
145  * sci_pool_initialize() -
146  *
147  * This macro initializes the pool to an empty condition.
148  */
149 #define sci_pool_initialize(this_pool) \
150         { \
151                 (this_pool).size = (sizeof((this_pool).array) / sizeof((this_pool).array[0])); \
152                 (this_pool).get = 0; \
153                 (this_pool).put = 0; \
154         }
155
156 /**
157  * sci_pool_get() -
158  *
159  * This macro will get the next free element from the pool. This should only be
160  * called if the pool is not empty.
161  */
162 #define sci_pool_get(this_pool, my_value) \
163         { \
164                 (my_value) = (this_pool).array[(this_pool).get]; \
165                 (this_pool).get = SCI_POOL_INCREMENT((this_pool), (this_pool).get); \
166         }
167
168 /**
169  * sci_pool_put() -
170  *
171  * This macro will put the value into the pool. This should only be called if
172  * the pool is not full.
173  */
174 #define sci_pool_put(this_pool, the_value) \
175         { \
176                 (this_pool).array[(this_pool).put] = (the_value); \
177                 (this_pool).put = SCI_POOL_INCREMENT((this_pool), (this_pool).put); \
178         }
179
180 /**
181  * sci_pool_erase() -
182  *
183  * This macro will search the pool and remove any elements in the pool matching
184  * the supplied value. This method can only be utilized on pools
185  */
186 #define sci_pool_erase(this_pool, type, the_value) \
187         { \
188                 type tmp_value; \
189                 u32 index; \
190                 u32 element_count = sci_pool_count((this_pool)); \
191  \
192                 for (index = 0; index < element_count; index++) {       \
193                         sci_pool_get((this_pool), tmp_value); \
194                         if (tmp_value != (the_value)) \
195                                 sci_pool_put((this_pool), tmp_value); \
196                 } \
197         }
198
199 #endif /* _SCI_POOL_H_ */