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
53#include <sys/cdefs.h>
54__FBSDID("$FreeBSD$");
55
56/**
57 * @file
58 *
59 * @brief This file contains all of the method implementations for the
60 *        SCIF_SAS_LIBRARY object.
61 */
62
63
64#include <dev/isci/scil/scic_library.h>
65#include <dev/isci/scil/sci_pool.h>
66
67#include <dev/isci/scil/scif_sas_library.h>
68#include <dev/isci/scil/scif_sas_logger.h>
69#include <dev/isci/scil/scif_sas_controller.h>
70
71
72/**
73 * This macro simply calculates the size of the framework library.  This
74 * includes the memory for each controller object.
75 */
76#define SCIF_LIBRARY_SIZE(max_controllers)                             \
77(                                                                      \
78   sizeof(SCIF_SAS_LIBRARY_T) +                                        \
79   (sizeof(SCIF_SAS_CONTROLLER_T) * (max_controllers))                 \
80)
81
82
83//******************************************************************************
84//* P U B L I C   M E T H O D S
85//******************************************************************************
86
87
88U32 scif_library_get_object_size(
89   U8 max_controller_count
90)
91{
92   return ( SCIF_LIBRARY_SIZE(max_controller_count) +
93            scic_library_get_object_size(max_controller_count) );
94}
95
96// ---------------------------------------------------------------------------
97
98SCI_LIBRARY_HANDLE_T scif_library_construct(
99   void * library_memory,
100   U8     max_controller_count
101)
102{
103   SCI_STATUS status;
104   SCIF_SAS_LIBRARY_T * fw_library = (SCIF_SAS_LIBRARY_T *) library_memory;
105
106   // Just clear out the memory of the structure to be safe.
107   memset(fw_library, 0, scif_library_get_object_size(max_controller_count));
108
109   // Invoke the parent object constructor.
110   SCI_BASE_LIBRARY_CONSTRUCT(fw_library,
111                              &fw_library->parent,
112                              max_controller_count,
113                              struct SCIF_SAS_CONTROLLER,
114                              status);
115
116   // The memory for the framework controller objects start immediately
117   // after the library object.
118   fw_library->controllers = (SCIF_SAS_CONTROLLER_T*)
119                             ((U8*)library_memory + sizeof(SCIF_SAS_LIBRARY_T));
120
121   // Construct the core library.
122   fw_library->core_object = scic_library_construct(
123                                (U8 *)library_memory +
124                                SCIF_LIBRARY_SIZE(max_controller_count),
125                                max_controller_count
126                             );
127
128   // Ensure construction completed successfully for the core.
129   if (fw_library->core_object != SCI_INVALID_HANDLE)
130   {
131      // Set the association in the core library to this framework library.
132      sci_object_set_association(
133         (SCI_OBJECT_HANDLE_T) fw_library->core_object,
134         (void *) fw_library
135      );
136
137      return fw_library;
138   }
139
140   return SCI_INVALID_HANDLE;
141}
142
143// ---------------------------------------------------------------------------
144
145SCI_STATUS scif_library_allocate_controller(
146   SCI_LIBRARY_HANDLE_T      library,
147   SCI_CONTROLLER_HANDLE_T * new_controller
148)
149{
150   SCI_STATUS  status;
151
152   // Ensure the user supplied a valid library handle.
153   if (library != SCI_INVALID_HANDLE)
154   {
155      SCIF_SAS_LIBRARY_T * fw_library = (SCIF_SAS_LIBRARY_T *) library;
156
157      // Allocate the framework library.
158      SCI_BASE_LIBRARY_ALLOCATE_CONTROLLER(fw_library, new_controller, &status);
159      if (status == SCI_SUCCESS)
160      {
161         SCIF_SAS_CONTROLLER_T * fw_controller;
162
163         // Allocate the core controller and save the handle in the framework
164         // controller object.
165         fw_controller = (SCIF_SAS_CONTROLLER_T*) *new_controller;
166
167         // Just clear out the memory of the structure to be safe.
168         memset(fw_controller, 0, sizeof(SCIF_SAS_CONTROLLER_T));
169
170         status = scic_library_allocate_controller(
171                     fw_library->core_object, &(fw_controller->core_object)
172                  );
173
174         // Free the framework controller if the core controller allocation
175         // failed.
176         if (status != SCI_SUCCESS)
177            scif_library_free_controller(library, fw_controller);
178      }
179
180      if (status != SCI_SUCCESS)
181      {
182         SCIF_LOG_WARNING((
183            sci_base_object_get_logger(fw_library),
184            SCIF_LOG_OBJECT_LIBRARY,
185            "Library:0x%x Status:0x%x controller allocation failed\n",
186            fw_library, status
187         ));
188      }
189   }
190   else
191      status = SCI_FAILURE_INVALID_PARAMETER_VALUE;
192
193   return status;
194}
195
196// ---------------------------------------------------------------------------
197
198SCI_STATUS scif_library_free_controller(
199   SCI_LIBRARY_HANDLE_T     library,
200   SCI_CONTROLLER_HANDLE_T  controller
201)
202{
203   SCI_STATUS  status;
204
205   if ( (library != SCI_INVALID_HANDLE) && (controller != SCI_INVALID_HANDLE) )
206   {
207      SCI_STATUS              core_status;
208      SCIF_SAS_LIBRARY_T    * fw_library    = (SCIF_SAS_LIBRARY_T*) library;
209      SCIF_SAS_CONTROLLER_T * fw_controller = (SCIF_SAS_CONTROLLER_T*) controller;
210
211      core_status = scic_library_free_controller(
212                       fw_library->core_object, fw_controller->core_object
213                    );
214
215      scif_sas_controller_destruct(fw_controller);
216
217      SCI_BASE_LIBRARY_FREE_CONTROLLER(
218         (SCIF_SAS_LIBRARY_T *) library,
219         controller,
220         SCIF_SAS_CONTROLLER_T,
221         &status
222      );
223
224      if ( (status == SCI_SUCCESS) && (core_status != SCI_SUCCESS) )
225         status = core_status;
226
227      if (status != SCI_SUCCESS)
228      {
229         SCIF_LOG_WARNING((
230            sci_base_object_get_logger(fw_library),
231            SCIF_LOG_OBJECT_LIBRARY,
232            "Library:0x%x Controller:0x%x Status:0x%x free controller failed\n",
233            fw_library, fw_controller, status
234         ));
235      }
236   }
237   else
238      status = SCI_FAILURE_INVALID_PARAMETER_VALUE;
239
240   return status;
241}
242
243// ---------------------------------------------------------------------------
244
245SCI_LIBRARY_HANDLE_T scif_library_get_scic_handle(
246   SCI_LIBRARY_HANDLE_T   scif_library
247)
248{
249   SCIF_SAS_LIBRARY_T * fw_library = (SCIF_SAS_LIBRARY_T*) scif_library;
250
251   return fw_library->core_object;
252}
253
254// ---------------------------------------------------------------------------
255
256#define SCIF_SAS_LIBRARY_MAX_TIMERS 32
257
258U16 scif_library_get_max_timer_count(
259   void
260)
261{
262   /// @todo Need to calculate the exact maximum number of timers needed.
263   return SCIF_SAS_LIBRARY_MAX_TIMERS + scic_library_get_max_timer_count();
264}
265
266//******************************************************************************
267//* P R O T E C T E D   M E T H O D S
268//******************************************************************************
269
270
271