xref: /freebsd/sys/dev/isci/scil/sci_base_library.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
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 #ifndef _SCI_BASE_LIBRARY_H_
55 #define _SCI_BASE_LIBRARY_H_
56 
57 /**
58  * @file
59  *
60  * @brief This file contains the protected interface structures, constants
61  *        and interface methods for the SCI_BASE_LIBRARY object.
62  *
63  */
64 
65 #ifdef __cplusplus
66 extern "C" {
67 #endif // __cplusplus
68 
69 #include <dev/isci/scil/sci_library.h>
70 #include <dev/isci/scil/sci_pool.h>
71 #include <dev/isci/scil/sci_base_object.h>
72 #include <dev/isci/scil/sci_base_logger.h>
73 #include <dev/isci/scil/sci_controller_constants.h>
74 
75 /**
76  * @struct SCI_BASE_LIBRARY
77  *
78  * @brief This structure contains all of the objects common to all library
79  *        sub-objects.
80  */
81 typedef struct SCI_BASE_LIBRARY
82 {
83    /**
84     * This class derives directly from the base object class.  As a result,
85     * the field is named "parent" and is the first field contained in the
86     * structure.
87     */
88    SCI_BASE_OBJECT_T  parent;
89 
90    /**
91     * This field provides the logger object to be utilized by all objects
92     * contained inside of a library.
93     */
94    SCI_BASE_LOGGER_T  logger;
95 
96    // Create a pool structure to manage free controller indices.
97    SCI_POOL_CREATE(controller_id_pool, U16, SCI_MAX_CONTROLLERS);
98 
99 } SCI_BASE_LIBRARY_T;
100 
101 
102 /**
103  * @brief This method will construct the base library object.
104  *
105  * @param[in] this_library This parameter specifies the library object
106  *            to be constructed.
107  * @param[in] max_controllers This parameter specifies the maximum number
108  *            of controllers to be supported by this library.
109  *
110  * @return none
111  */
112 void sci_base_library_construct(
113    SCI_BASE_LIBRARY_T * this_library,
114    U32                  max_controllers
115 );
116 
117 /**
118  * This macro provides common code for allocating a controller from a library.
119  * It will ensure that we successfully allocate an available controller index
120  * and return SCI_FAILURE_INSUFFICIENT_RESOURCES if unsuccessful.
121  */
122 #define SCI_BASE_LIBRARY_ALLOCATE_CONTROLLER( \
123    library, \
124    controller_ptr, \
125    rc \
126 ) \
127 { \
128    U16 index; \
129    *rc = SCI_SUCCESS; \
130    if (! sci_pool_empty((library)->parent.controller_id_pool)) \
131    { \
132       sci_pool_get((library)->parent.controller_id_pool, index); \
133       *controller_ptr = (SCI_CONTROLLER_HANDLE_T) \
134                         & (library)->controllers[index]; \
135    } \
136    else \
137       *rc = SCI_FAILURE_INSUFFICIENT_RESOURCES; \
138 }
139 
140 /**
141  * This macro provides common code for freeing a controller to a library.
142  * It calculates the index to the controller instance in the array by
143  * determining the offset.
144  */
145 #define SCI_BASE_LIBRARY_FREE_CONTROLLER( \
146    library, \
147    controller, \
148    CONTROLLER_TYPE, \
149    rc \
150 ) \
151 { \
152    U16 index = (U16) \
153                ((((char *)(controller)) - ((char *)(library)->controllers))\
154                 / sizeof(CONTROLLER_TYPE)); \
155    *rc = SCI_SUCCESS; \
156    if (  (index < SCI_MAX_CONTROLLERS) \
157       && (! sci_pool_full((library)->parent.controller_id_pool)) ) \
158    { \
159       sci_pool_put((library)->parent.controller_id_pool, index); \
160    } \
161    else \
162       *rc = SCI_FAILURE_CONTROLLER_NOT_FOUND; \
163 }
164 
165 
166 
167 /**
168  * This macro provides common code for constructing library. It
169  * It initialize and fill the library's controller_id_pool.
170  */
171 #define SCI_BASE_LIBRARY_CONSTRUCT( \
172    library, \
173    base_library, \
174    max_controllers, \
175    CONTROLLER_TYPE, \
176    status \
177 ) \
178 { \
179    U32 controller_index; \
180    sci_base_object_construct(&(base_library)->parent, &(base_library)->logger); \
181    sci_pool_initialize((base_library)->controller_id_pool); \
182    for (controller_index = 0; controller_index < max_controller_count; controller_index++) \
183    { \
184       SCI_BASE_LIBRARY_FREE_CONTROLLER( \
185          library, \
186          &library->controllers[controller_index], \
187          CONTROLLER_TYPE, \
188          &status \
189       ); \
190    } \
191 }
192 
193 #ifdef __cplusplus
194 }
195 #endif // __cplusplus
196 
197 #endif // _SCI_BASE_LIBRARY_H_
198