xref: /freebsd/sys/dev/isci/scil/sci_base_library.h (revision 4f52dfbb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of version 2 of the GNU General Public License as
13  * published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23  * The full GNU General Public License is included in this distribution
24  * in the file called LICENSE.GPL.
25  *
26  * BSD LICENSE
27  *
28  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  *
35  *   * Redistributions of source code must retain the above copyright
36  *     notice, this list of conditions and the following disclaimer.
37  *   * Redistributions in binary form must reproduce the above copyright
38  *     notice, this list of conditions and the following disclaimer in
39  *     the documentation and/or other materials provided with the
40  *     distribution.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53  *
54  * $FreeBSD$
55  */
56 #ifndef _SCI_BASE_LIBRARY_H_
57 #define _SCI_BASE_LIBRARY_H_
58 
59 /**
60  * @file
61  *
62  * @brief This file contains the protected interface structures, constants
63  *        and interface methods for the SCI_BASE_LIBRARY object.
64  *
65  */
66 
67 #ifdef __cplusplus
68 extern "C" {
69 #endif // __cplusplus
70 
71 #include <dev/isci/scil/sci_library.h>
72 #include <dev/isci/scil/sci_pool.h>
73 #include <dev/isci/scil/sci_base_object.h>
74 #include <dev/isci/scil/sci_base_logger.h>
75 #include <dev/isci/scil/sci_controller_constants.h>
76 
77 /**
78  * @struct SCI_BASE_LIBRARY
79  *
80  * @brief This structure contains all of the objects common to all library
81  *        sub-objects.
82  */
83 typedef struct SCI_BASE_LIBRARY
84 {
85    /**
86     * This class derives directly from the base object class.  As a result,
87     * the field is named "parent" and is the first field contained in the
88     * structure.
89     */
90    SCI_BASE_OBJECT_T  parent;
91 
92    /**
93     * This field provides the logger object to be utilized by all objects
94     * contained inside of a library.
95     */
96    SCI_BASE_LOGGER_T  logger;
97 
98    // Create a pool structure to manage free controller indices.
99    SCI_POOL_CREATE(controller_id_pool, U16, SCI_MAX_CONTROLLERS);
100 
101 } SCI_BASE_LIBRARY_T;
102 
103 
104 /**
105  * @brief This method will construct the base library object.
106  *
107  * @param[in] this_library This parameter specifies the library object
108  *            to be constructed.
109  * @param[in] max_controllers This parameter specifies the maximum number
110  *            of controllers to be supported by this library.
111  *
112  * @return none
113  */
114 void sci_base_library_construct(
115    SCI_BASE_LIBRARY_T * this_library,
116    U32                  max_controllers
117 );
118 
119 /**
120  * This macro provides common code for allocating a controller from a library.
121  * It will ensure that we successfully allocate an available controller index
122  * and return SCI_FAILURE_INSUFFICIENT_RESOURCES if unsuccessful.
123  */
124 #define SCI_BASE_LIBRARY_ALLOCATE_CONTROLLER( \
125    library, \
126    controller_ptr, \
127    rc \
128 ) \
129 { \
130    U16 index; \
131    *rc = SCI_SUCCESS; \
132    if (! sci_pool_empty((library)->parent.controller_id_pool)) \
133    { \
134       sci_pool_get((library)->parent.controller_id_pool, index); \
135       *controller_ptr = (SCI_CONTROLLER_HANDLE_T) \
136                         & (library)->controllers[index]; \
137    } \
138    else \
139       *rc = SCI_FAILURE_INSUFFICIENT_RESOURCES; \
140 }
141 
142 /**
143  * This macro provides common code for freeing a controller to a library.
144  * It calculates the index to the controller instance in the array by
145  * determining the offset.
146  */
147 #define SCI_BASE_LIBRARY_FREE_CONTROLLER( \
148    library, \
149    controller, \
150    CONTROLLER_TYPE, \
151    rc \
152 ) \
153 { \
154    U16 index = (U16) \
155                ((((char *)(controller)) - ((char *)(library)->controllers))\
156                 / sizeof(CONTROLLER_TYPE)); \
157    *rc = SCI_SUCCESS; \
158    if (  (index < SCI_MAX_CONTROLLERS) \
159       && (! sci_pool_full((library)->parent.controller_id_pool)) ) \
160    { \
161       sci_pool_put((library)->parent.controller_id_pool, index); \
162    } \
163    else \
164       *rc = SCI_FAILURE_CONTROLLER_NOT_FOUND; \
165 }
166 
167 
168 
169 /**
170  * This macro provides common code for constructing library. It
171  * It initialize and fill the library's controller_id_pool.
172  */
173 #define SCI_BASE_LIBRARY_CONSTRUCT( \
174    library, \
175    base_library, \
176    max_controllers, \
177    CONTROLLER_TYPE, \
178    status \
179 ) \
180 { \
181    U32 controller_index; \
182    sci_base_object_construct(&(base_library)->parent, &(base_library)->logger); \
183    sci_pool_initialize((base_library)->controller_id_pool); \
184    for (controller_index = 0; controller_index < max_controller_count; controller_index++) \
185    { \
186       SCI_BASE_LIBRARY_FREE_CONTROLLER( \
187          library, \
188          &library->controllers[controller_index], \
189          CONTROLLER_TYPE, \
190          &status \
191       ); \
192    } \
193 }
194 
195 #ifdef __cplusplus
196 }
197 #endif // __cplusplus
198 
199 #endif // _SCI_BASE_LIBRARY_H_
200