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