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