1230557Sjimharris/*-
2230557Sjimharris * This file is provided under a dual BSD/GPLv2 license.  When using or
3230557Sjimharris * redistributing this file, you may do so under either license.
4230557Sjimharris *
5230557Sjimharris * GPL LICENSE SUMMARY
6230557Sjimharris *
7230557Sjimharris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8230557Sjimharris *
9230557Sjimharris * This program is free software; you can redistribute it and/or modify
10230557Sjimharris * it under the terms of version 2 of the GNU General Public License as
11230557Sjimharris * published by the Free Software Foundation.
12230557Sjimharris *
13230557Sjimharris * This program is distributed in the hope that it will be useful, but
14230557Sjimharris * WITHOUT ANY WARRANTY; without even the implied warranty of
15230557Sjimharris * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16230557Sjimharris * General Public License for more details.
17230557Sjimharris *
18230557Sjimharris * You should have received a copy of the GNU General Public License
19230557Sjimharris * along with this program; if not, write to the Free Software
20230557Sjimharris * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21230557Sjimharris * The full GNU General Public License is included in this distribution
22230557Sjimharris * in the file called LICENSE.GPL.
23230557Sjimharris *
24230557Sjimharris * BSD LICENSE
25230557Sjimharris *
26230557Sjimharris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27230557Sjimharris * All rights reserved.
28230557Sjimharris *
29230557Sjimharris * Redistribution and use in source and binary forms, with or without
30230557Sjimharris * modification, are permitted provided that the following conditions
31230557Sjimharris * are met:
32230557Sjimharris *
33230557Sjimharris *   * Redistributions of source code must retain the above copyright
34230557Sjimharris *     notice, this list of conditions and the following disclaimer.
35230557Sjimharris *   * Redistributions in binary form must reproduce the above copyright
36230557Sjimharris *     notice, this list of conditions and the following disclaimer in
37230557Sjimharris *     the documentation and/or other materials provided with the
38230557Sjimharris *     distribution.
39230557Sjimharris *
40230557Sjimharris * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41230557Sjimharris * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42230557Sjimharris * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43230557Sjimharris * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44230557Sjimharris * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45230557Sjimharris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46230557Sjimharris * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47230557Sjimharris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48230557Sjimharris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49230557Sjimharris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50230557Sjimharris * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51230557Sjimharris */
52230557Sjimharris
53230557Sjimharris#include <sys/cdefs.h>
54230557Sjimharris__FBSDID("$FreeBSD$");
55230557Sjimharris
56230557Sjimharris/**
57230557Sjimharris * @file
58230557Sjimharris *
59230557Sjimharris * @brief This file contains all of the method implementations for the
60230557Sjimharris *        SCIF_SAS_LIBRARY object.
61230557Sjimharris */
62230557Sjimharris
63230557Sjimharris
64230557Sjimharris#include <dev/isci/scil/scic_library.h>
65230557Sjimharris#include <dev/isci/scil/sci_pool.h>
66230557Sjimharris
67230557Sjimharris#include <dev/isci/scil/scif_sas_library.h>
68230557Sjimharris#include <dev/isci/scil/scif_sas_logger.h>
69230557Sjimharris#include <dev/isci/scil/scif_sas_controller.h>
70230557Sjimharris
71230557Sjimharris
72230557Sjimharris/**
73230557Sjimharris * This macro simply calculates the size of the framework library.  This
74230557Sjimharris * includes the memory for each controller object.
75230557Sjimharris */
76230557Sjimharris#define SCIF_LIBRARY_SIZE(max_controllers)                             \
77230557Sjimharris(                                                                      \
78230557Sjimharris   sizeof(SCIF_SAS_LIBRARY_T) +                                        \
79230557Sjimharris   (sizeof(SCIF_SAS_CONTROLLER_T) * (max_controllers))                 \
80230557Sjimharris)
81230557Sjimharris
82230557Sjimharris
83230557Sjimharris//******************************************************************************
84230557Sjimharris//* P U B L I C   M E T H O D S
85230557Sjimharris//******************************************************************************
86230557Sjimharris
87230557Sjimharris
88230557SjimharrisU32 scif_library_get_object_size(
89230557Sjimharris   U8 max_controller_count
90230557Sjimharris)
91230557Sjimharris{
92230557Sjimharris   return ( SCIF_LIBRARY_SIZE(max_controller_count) +
93230557Sjimharris            scic_library_get_object_size(max_controller_count) );
94230557Sjimharris}
95230557Sjimharris
96230557Sjimharris// ---------------------------------------------------------------------------
97230557Sjimharris
98230557SjimharrisSCI_LIBRARY_HANDLE_T scif_library_construct(
99230557Sjimharris   void * library_memory,
100230557Sjimharris   U8     max_controller_count
101230557Sjimharris)
102230557Sjimharris{
103230557Sjimharris   SCI_STATUS status;
104230557Sjimharris   SCIF_SAS_LIBRARY_T * fw_library = (SCIF_SAS_LIBRARY_T *) library_memory;
105230557Sjimharris
106230557Sjimharris   // Just clear out the memory of the structure to be safe.
107230557Sjimharris   memset(fw_library, 0, scif_library_get_object_size(max_controller_count));
108230557Sjimharris
109230557Sjimharris   // Invoke the parent object constructor.
110230557Sjimharris   SCI_BASE_LIBRARY_CONSTRUCT(fw_library,
111230557Sjimharris                              &fw_library->parent,
112230557Sjimharris                              max_controller_count,
113230557Sjimharris                              struct SCIF_SAS_CONTROLLER,
114230557Sjimharris                              status);
115230557Sjimharris
116230557Sjimharris   // The memory for the framework controller objects start immediately
117230557Sjimharris   // after the library object.
118230557Sjimharris   fw_library->controllers = (SCIF_SAS_CONTROLLER_T*)
119230557Sjimharris                             ((U8*)library_memory + sizeof(SCIF_SAS_LIBRARY_T));
120230557Sjimharris
121230557Sjimharris   // Construct the core library.
122230557Sjimharris   fw_library->core_object = scic_library_construct(
123230557Sjimharris                                (U8 *)library_memory +
124230557Sjimharris                                SCIF_LIBRARY_SIZE(max_controller_count),
125230557Sjimharris                                max_controller_count
126230557Sjimharris                             );
127230557Sjimharris
128230557Sjimharris   // Ensure construction completed successfully for the core.
129230557Sjimharris   if (fw_library->core_object != SCI_INVALID_HANDLE)
130230557Sjimharris   {
131230557Sjimharris      // Set the association in the core library to this framework library.
132230557Sjimharris      sci_object_set_association(
133230557Sjimharris         (SCI_OBJECT_HANDLE_T) fw_library->core_object,
134230557Sjimharris         (void *) fw_library
135230557Sjimharris      );
136230557Sjimharris
137230557Sjimharris      return fw_library;
138230557Sjimharris   }
139230557Sjimharris
140230557Sjimharris   return SCI_INVALID_HANDLE;
141230557Sjimharris}
142230557Sjimharris
143230557Sjimharris// ---------------------------------------------------------------------------
144230557Sjimharris
145230557SjimharrisSCI_STATUS scif_library_allocate_controller(
146230557Sjimharris   SCI_LIBRARY_HANDLE_T      library,
147230557Sjimharris   SCI_CONTROLLER_HANDLE_T * new_controller
148230557Sjimharris)
149230557Sjimharris{
150230557Sjimharris   SCI_STATUS  status;
151230557Sjimharris
152230557Sjimharris   // Ensure the user supplied a valid library handle.
153230557Sjimharris   if (library != SCI_INVALID_HANDLE)
154230557Sjimharris   {
155230557Sjimharris      SCIF_SAS_LIBRARY_T * fw_library = (SCIF_SAS_LIBRARY_T *) library;
156230557Sjimharris
157230557Sjimharris      // Allocate the framework library.
158230557Sjimharris      SCI_BASE_LIBRARY_ALLOCATE_CONTROLLER(fw_library, new_controller, &status);
159230557Sjimharris      if (status == SCI_SUCCESS)
160230557Sjimharris      {
161230557Sjimharris         SCIF_SAS_CONTROLLER_T * fw_controller;
162230557Sjimharris
163230557Sjimharris         // Allocate the core controller and save the handle in the framework
164230557Sjimharris         // controller object.
165230557Sjimharris         fw_controller = (SCIF_SAS_CONTROLLER_T*) *new_controller;
166230557Sjimharris
167230557Sjimharris         // Just clear out the memory of the structure to be safe.
168230557Sjimharris         memset(fw_controller, 0, sizeof(SCIF_SAS_CONTROLLER_T));
169230557Sjimharris
170230557Sjimharris         status = scic_library_allocate_controller(
171230557Sjimharris                     fw_library->core_object, &(fw_controller->core_object)
172230557Sjimharris                  );
173230557Sjimharris
174230557Sjimharris         // Free the framework controller if the core controller allocation
175230557Sjimharris         // failed.
176230557Sjimharris         if (status != SCI_SUCCESS)
177230557Sjimharris            scif_library_free_controller(library, fw_controller);
178230557Sjimharris      }
179230557Sjimharris
180230557Sjimharris      if (status != SCI_SUCCESS)
181230557Sjimharris      {
182230557Sjimharris         SCIF_LOG_WARNING((
183230557Sjimharris            sci_base_object_get_logger(fw_library),
184230557Sjimharris            SCIF_LOG_OBJECT_LIBRARY,
185230557Sjimharris            "Library:0x%x Status:0x%x controller allocation failed\n",
186230557Sjimharris            fw_library, status
187230557Sjimharris         ));
188230557Sjimharris      }
189230557Sjimharris   }
190230557Sjimharris   else
191230557Sjimharris      status = SCI_FAILURE_INVALID_PARAMETER_VALUE;
192230557Sjimharris
193230557Sjimharris   return status;
194230557Sjimharris}
195230557Sjimharris
196230557Sjimharris// ---------------------------------------------------------------------------
197230557Sjimharris
198230557SjimharrisSCI_STATUS scif_library_free_controller(
199230557Sjimharris   SCI_LIBRARY_HANDLE_T     library,
200230557Sjimharris   SCI_CONTROLLER_HANDLE_T  controller
201230557Sjimharris)
202230557Sjimharris{
203230557Sjimharris   SCI_STATUS  status;
204230557Sjimharris
205230557Sjimharris   if ( (library != SCI_INVALID_HANDLE) && (controller != SCI_INVALID_HANDLE) )
206230557Sjimharris   {
207230557Sjimharris      SCI_STATUS              core_status;
208230557Sjimharris      SCIF_SAS_LIBRARY_T    * fw_library    = (SCIF_SAS_LIBRARY_T*) library;
209230557Sjimharris      SCIF_SAS_CONTROLLER_T * fw_controller = (SCIF_SAS_CONTROLLER_T*) controller;
210230557Sjimharris
211230557Sjimharris      core_status = scic_library_free_controller(
212230557Sjimharris                       fw_library->core_object, fw_controller->core_object
213230557Sjimharris                    );
214230557Sjimharris
215230557Sjimharris      scif_sas_controller_destruct(fw_controller);
216230557Sjimharris
217230557Sjimharris      SCI_BASE_LIBRARY_FREE_CONTROLLER(
218230557Sjimharris         (SCIF_SAS_LIBRARY_T *) library,
219230557Sjimharris         controller,
220230557Sjimharris         SCIF_SAS_CONTROLLER_T,
221230557Sjimharris         &status
222230557Sjimharris      );
223230557Sjimharris
224230557Sjimharris      if ( (status == SCI_SUCCESS) && (core_status != SCI_SUCCESS) )
225230557Sjimharris         status = core_status;
226230557Sjimharris
227230557Sjimharris      if (status != SCI_SUCCESS)
228230557Sjimharris      {
229230557Sjimharris         SCIF_LOG_WARNING((
230230557Sjimharris            sci_base_object_get_logger(fw_library),
231230557Sjimharris            SCIF_LOG_OBJECT_LIBRARY,
232230557Sjimharris            "Library:0x%x Controller:0x%x Status:0x%x free controller failed\n",
233230557Sjimharris            fw_library, fw_controller, status
234230557Sjimharris         ));
235230557Sjimharris      }
236230557Sjimharris   }
237230557Sjimharris   else
238230557Sjimharris      status = SCI_FAILURE_INVALID_PARAMETER_VALUE;
239230557Sjimharris
240230557Sjimharris   return status;
241230557Sjimharris}
242230557Sjimharris
243230557Sjimharris// ---------------------------------------------------------------------------
244230557Sjimharris
245230557SjimharrisSCI_LIBRARY_HANDLE_T scif_library_get_scic_handle(
246230557Sjimharris   SCI_LIBRARY_HANDLE_T   scif_library
247230557Sjimharris)
248230557Sjimharris{
249230557Sjimharris   SCIF_SAS_LIBRARY_T * fw_library = (SCIF_SAS_LIBRARY_T*) scif_library;
250230557Sjimharris
251230557Sjimharris   return fw_library->core_object;
252230557Sjimharris}
253230557Sjimharris
254230557Sjimharris// ---------------------------------------------------------------------------
255230557Sjimharris
256230557Sjimharris#define SCIF_SAS_LIBRARY_MAX_TIMERS 32
257230557Sjimharris
258230557SjimharrisU16 scif_library_get_max_timer_count(
259230557Sjimharris   void
260230557Sjimharris)
261230557Sjimharris{
262230557Sjimharris   /// @todo Need to calculate the exact maximum number of timers needed.
263230557Sjimharris   return SCIF_SAS_LIBRARY_MAX_TIMERS + scic_library_get_max_timer_count();
264230557Sjimharris}
265230557Sjimharris
266230557Sjimharris//******************************************************************************
267230557Sjimharris//* P R O T E C T E D   M E T H O D S
268230557Sjimharris//******************************************************************************
269230557Sjimharris
270230557Sjimharris
271