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 *
40 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 *
52 * $FreeBSD$
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
66extern "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 */
81typedef 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 */
112void 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