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 the implementation of the
62  *        SCIF_SAS_INTERNAL_IO_REQUEST object.
63  */
64 
65 
66 #include <dev/isci/scil/scic_io_request.h>
67 #include <dev/isci/scil/scic_remote_device.h>
68 #include <dev/isci/scil/scic_user_callback.h>
69 #include <dev/isci/scil/scic_controller.h>
70 #include <dev/isci/scil/scic_task_request.h>
71 #include <dev/isci/scil/scif_user_callback.h>
72 
73 #include <dev/isci/scil/scif_sas_controller.h>
74 #include <dev/isci/scil/scif_sas_domain.h>
75 #include <dev/isci/scil/scif_sas_remote_device.h>
76 #include <dev/isci/scil/scif_sas_io_request.h>
77 #include <dev/isci/scil/scif_sas_internal_io_request.h>
78 #include <dev/isci/scil/scif_sas_task_request.h>
79 #include <dev/isci/scil/scif_sas_stp_io_request.h>
80 #include <dev/isci/scil/scif_sas_logger.h>
81 #include <dev/isci/scil/scif_sas_smp_io_request.h>
82 #include <dev/isci/scil/sci_util.h>
83 
84 //******************************************************************************
85 //* P U B L I C   M E T H O D S
86 //******************************************************************************
87 
88 /**
89  * @brief this routine return all memory needed for an internal request, both
90  *        framework and core request.
91  *
92  * @return U32 size of all memory needed for an internal request
93  */
94 U32 scif_sas_internal_request_get_object_size(
95    void
96 )
97 {
98    return MAX(
99             (sizeof(SCIF_SAS_INTERNAL_IO_REQUEST_T) + scic_io_request_get_object_size()),
100             (sizeof(SCIF_SAS_TASK_REQUEST_T) + scic_task_request_get_object_size())
101              );
102 }
103 
104 
105 /**
106  * @brief This method constructs an internal smp request.
107  *
108  * @param[in] fw_controller The framework controller
109  * @param[in] fw_device The smp device that the internal io targets to.
110  * @param[in] internal_io_memory The memory space for the internal io.
111  * @param[in] io_tag The io tag for the internl io to be constructed.
112  * @param[in] smp_command A pointer to the smp request data structure according
113  *       to SAS protocol.
114  *
115  * @return Indicate if the internal io was successfully constructed.
116  * @retval SCI_SUCCESS This value is returned if the internal io was
117  *         successfully constructed.
118  * @retval SCI_FAILURE This value is returned if the internal io was failed to
119  *         be constructed.
120  */
121 SCI_STATUS scif_sas_internal_io_request_construct_smp(
122    SCIF_SAS_CONTROLLER_T       * fw_controller,
123    SCIF_SAS_REMOTE_DEVICE_T    * fw_device,
124    void                        * internal_io_memory,
125    U16                           io_tag,
126    SMP_REQUEST_T               * smp_command
127 )
128 {
129    SCIF_SAS_INTERNAL_IO_REQUEST_T * fw_internal_io  =
130      (SCIF_SAS_INTERNAL_IO_REQUEST_T*)internal_io_memory;
131 
132    SCIF_SAS_IO_REQUEST_T * fw_io =
133      (SCIF_SAS_IO_REQUEST_T*)internal_io_memory;
134 
135    SCI_STATUS status;
136 
137    //call common smp request construct routine.
138    status = scif_sas_io_request_construct_smp(
139                fw_controller,
140                fw_device,
141                internal_io_memory,
142                (char *)internal_io_memory + sizeof(SCIF_SAS_INTERNAL_IO_REQUEST_T),
143                SCI_CONTROLLER_INVALID_IO_TAG,
144                smp_command,
145                NULL //there is no associated user io object.
146             );
147 
148    //Codes below are all internal io related.
149    if (status == SCI_SUCCESS)
150    {
151       //set the is_internal flag
152       fw_io->parent.is_internal = TRUE;
153 
154       if (fw_internal_io->internal_io_timer == NULL)
155       {
156          //create the timer for this internal request.
157          fw_internal_io->internal_io_timer =
158             scif_cb_timer_create(
159                (SCI_CONTROLLER_HANDLE_T *)fw_controller,
160                scif_sas_internal_io_request_timeout_handler,
161                (void*)fw_io
162             );
163       }
164       else
165       {
166          ASSERT (0);
167       }
168 
169       //insert into high priority queue
170       if ( !sci_pool_full(fw_controller->hprq.pool) )
171       {
172          sci_pool_put(
173             fw_controller->hprq.pool, (POINTER_UINT) internal_io_memory
174          );
175       }
176       else
177       {
178          SCIF_LOG_ERROR((
179             sci_base_object_get_logger(fw_controller),
180             SCIF_LOG_OBJECT_CONTROLLER | SCIF_LOG_OBJECT_REMOTE_DEVICE,
181             "scif_sas_internal_io_request_construct_smp, high priority queue full!\n"
182          ));
183 
184          scif_sas_internal_io_request_destruct(fw_controller, fw_internal_io);
185 
186          //return failure status.
187          return SCI_FAILURE_INSUFFICIENT_RESOURCES;
188       }
189    }
190 
191    return status;
192 }
193 
194 
195 /**
196  * @brief This method constructs an internal smp request.
197  * @param[in] fw_io
198  *
199  * @return SCI_STATUS
200  */
201 SCI_STATUS scif_sas_internal_io_request_construct_stp(
202    SCIF_SAS_INTERNAL_IO_REQUEST_T * fw_io
203 )
204 {
205    //TBD
206    return SCI_SUCCESS;
207 }
208 
209 
210 /**
211  * @brief This method handles the timeout situation for an internal io.
212  *
213  * @param[in] fw_internal_io The timed out IO.
214  *
215  * @return none
216  */
217 void scif_sas_internal_io_request_timeout_handler(
218    void * fw_internal_io
219 )
220 {
221    SCIF_SAS_REQUEST_T * fw_request = (SCIF_SAS_REQUEST_T *)fw_internal_io;
222 
223    SCIF_LOG_TRACE((
224       sci_base_object_get_logger(fw_request),
225       SCIF_LOG_OBJECT_IO_REQUEST,
226       "scif_sas_internal_io_request_timeout_handler(0x%x) enter\n",
227       fw_internal_io
228    ));
229 
230    fw_request->state_handlers->abort_handler(&fw_request->parent);
231 }
232 
233 
234 /**
235  * @brief This methods takes care of completion of an internal request about its
236  *        "internal" related feature, including the memory recycling and timer.
237  *
238  * @param[in] fw_controller The framework controller object.
239  * @param[in] fw_internal_io The internal io to be completed.
240  * @param[in] completion_status the completeion status by core and framework so
241  *       far.
242  *
243  * @return none
244  */
245 void scif_sas_internal_io_request_complete(
246    SCIF_SAS_CONTROLLER_T          * fw_controller,
247    SCIF_SAS_INTERNAL_IO_REQUEST_T * fw_internal_io,
248    SCI_STATUS                       completion_status
249 )
250 {
251    SCIF_LOG_TRACE((
252       sci_base_object_get_logger(fw_controller),
253       SCIF_LOG_OBJECT_IO_REQUEST,
254       "scif_sas_internal_io_request_complete(0x%x, 0x%x, 0x%x) enter\n",
255        fw_controller, fw_internal_io, completion_status
256    ));
257 
258    scif_cb_timer_stop(fw_controller, fw_internal_io->internal_io_timer);
259    scif_sas_internal_io_request_destruct(fw_controller, fw_internal_io);
260 }
261 
262 
263 /**
264  * @brief This methods takes care of destruction of an internal request about its
265  *        "internal" related feature, including the memory recycling and timer.
266  *
267  * @param[in] fw_controller The framework controller object.
268  * @param[in] fw_internal_io The internal io to be completed.
269  *
270  * @return none
271  */
272 void scif_sas_internal_io_request_destruct(
273    SCIF_SAS_CONTROLLER_T          * fw_controller,
274    SCIF_SAS_INTERNAL_IO_REQUEST_T * fw_internal_io
275 )
276 {
277    if (fw_internal_io->internal_io_timer != NULL)
278    {
279       scif_cb_timer_destroy(fw_controller, fw_internal_io->internal_io_timer);
280       fw_internal_io->internal_io_timer = NULL;
281    }
282    scif_sas_controller_free_internal_request(fw_controller, fw_internal_io);
283 }
284 
285