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#include <dev/isci/scil/sati.h>
57#include <dev/isci/scil/scif_sas_logger.h>
58#include <dev/isci/scil/scif_sas_task_request.h>
59#include <dev/isci/scil/scif_sas_controller.h>
60#include <dev/isci/scil/scif_sas_remote_device.h>
61#include <dev/isci/scil/scif_sas_stp_task_request.h>
62#include <dev/isci/scil/scic_task_request.h>
63#include <dev/isci/scil/scic_controller.h>
64
65/**
66 * @brief This method provides SATA/STP STARTED state specific handling for
67 *        when the user attempts to complete the supplied IO request.
68 *        It will perform data/response translation and free NCQ tags
69 *        if necessary.
70 *
71 * @param[in] io_request This parameter specifies the IO request object
72 *            to be started.
73 *
74 * @return This method returns a value indicating if the IO request was
75 *         successfully completed or not.
76 */
77static
78SCI_STATUS scif_sas_stp_core_cb_task_request_complete_handler(
79   SCIF_SAS_CONTROLLER_T    * fw_controller,
80   SCIF_SAS_REMOTE_DEVICE_T * fw_device,
81   SCIF_SAS_REQUEST_T       * fw_request,
82   SCI_STATUS               * completion_status
83)
84{
85#if !defined(DISABLE_SATI_TASK_MANAGEMENT)
86   SCIF_SAS_TASK_REQUEST_T * fw_task = (SCIF_SAS_TASK_REQUEST_T *) fw_request;
87
88   SCIF_LOG_TRACE((
89      sci_base_object_get_logger(fw_controller),
90      SCIF_LOG_OBJECT_TASK_MANAGEMENT,
91      "scif_sas_stp_core_cb_task_request_complete_handler(0x%x, 0x%x, 0x%x, 0x%x) enter\n",
92      fw_controller, fw_device, fw_request, *completion_status
93   ));
94
95   // Translating the response is only necessary if some sort of error
96   // occurred resulting in having the error bit set in the ATA status
97   // register and values to decode in the ATA error register.
98   if (  (*completion_status == SCI_SUCCESS)
99      || (*completion_status == SCI_FAILURE_IO_RESPONSE_VALID) )
100   {
101      SATI_STATUS sati_status = sati_translate_task_response(
102                                   &fw_task->parent.stp.sequence,
103                                   fw_task,
104                                   fw_task
105                                );
106
107      if (sati_status == SATI_COMPLETE)
108         *completion_status = SCI_SUCCESS;
109      else if (sati_status == SATI_FAILURE_CHECK_RESPONSE_DATA)
110         *completion_status = SCI_FAILURE_IO_RESPONSE_VALID;
111      else if (sati_status == SATI_SEQUENCE_INCOMPLETE)
112      {
113         // The translation indicates that additional SATA requests are
114         // necessary to finish the original SCSI request.  As a result,
115         // do not complete the IO and begin the next stage of the
116         // translation.
117         /// @todo multiple ATA commands are required, but not supported yet.
118         return SCI_FAILURE;
119      }
120      else
121      {
122         // Something unexpected occurred during translation.  Fail the
123         // IO request to the user.
124         *completion_status = SCI_FAILURE;
125      }
126   }
127   else  //A stp task request sometimes fails.
128   {
129      if (scif_sas_task_request_get_function(fw_task) == SCI_SAS_ABORT_TASK_SET)
130      {
131         scif_sas_stp_task_request_abort_task_set_failure_handler(
132            fw_device, fw_task);
133      }
134   }
135
136   return SCI_SUCCESS;
137#else // !defined(DISABLE_SATI_TASK_MANAGEMENT)
138   return SCI_FAILURE;
139#endif // !defined(DISABLE_SATI_TASK_MANAGEMENT)
140}
141
142/**
143 * @file
144 *
145 * @brief This file contains the method implementations for the
146 *        SCIF_SAS_STP_TASK_REQUEST object.  The contents will implement
147 *        SATA/STP specific functionality.
148 */
149SCI_STATUS scif_sas_stp_task_request_construct(
150   SCIF_SAS_TASK_REQUEST_T * fw_task
151)
152{
153   SCI_STATUS                 sci_status = SCI_FAILURE;
154
155#if !defined(DISABLE_SATI_TASK_MANAGEMENT)
156   SATI_STATUS                sati_status;
157   SCIF_SAS_REMOTE_DEVICE_T * fw_device  = fw_task->parent.device;
158
159   SCIF_LOG_TRACE((
160      sci_base_object_get_logger(fw_task),
161      SCIF_LOG_OBJECT_TASK_MANAGEMENT,
162      "scif_sas_stp_task_request_construct(0x%x) enter\n",
163      fw_task
164   ));
165
166   // The translator will indirectly invoke core methods to set the fields
167   // of the ATA register FIS inside of this method.
168   sati_status = sati_translate_task_management(
169                    &fw_task->parent.stp.sequence,
170                    &fw_device->protocol_device.stp_device.sati_device,
171                    fw_task,
172                    fw_task
173                 );
174
175   if (sati_status == SATI_SUCCESS)
176   {
177      sci_status = scic_task_request_construct_sata(fw_task->parent.core_object);
178      //fw_task->parent.state_handlers = &stp_io_request_constructed_handlers;
179      fw_task->parent.protocol_complete_handler =
180         scif_sas_stp_core_cb_task_request_complete_handler;
181   }
182   else
183   {
184      SCIF_LOG_ERROR((
185         sci_base_object_get_logger(fw_task),
186         SCIF_LOG_OBJECT_TASK_MANAGEMENT,
187         "Task 0x%x received unexpected SAT translation failure 0x%x\n",
188         fw_task, sati_status
189      ));
190   }
191#endif // !defined(DISABLE_SATI_TASK_MANAGEMENT)
192
193   return sci_status;
194}
195
196
197/**
198 * @brief This method provides handling for failed stp TASK MANAGEMENT
199 *           request.
200 *
201 * @param[in] fw_device This parameter specifies the target device the
202 *            task management request towards to.
203 * @param[in] fw_request This parameter specifies the failed task management
204 *            request.
205 * @param[in] completion_status This parameter sprecifies the completion
206 *            status of the task management request's core status.
207 *
208 * @return None.
209 */
210void scif_sas_stp_task_request_abort_task_set_failure_handler(
211   SCIF_SAS_REMOTE_DEVICE_T * fw_device,
212   SCIF_SAS_TASK_REQUEST_T  * fw_task
213)
214{
215#if !defined(DISABLE_SATI_TASK_MANAGEMENT)
216   SCIF_SAS_DOMAIN_T         * fw_domain = fw_device->domain;
217   SCI_FAST_LIST_ELEMENT_T   * pending_request_element;
218   SCIF_SAS_REQUEST_T        * pending_request = NULL;
219
220   pending_request_element = fw_domain->request_list.list_head;
221
222   // Cycle through the list of IO requests. search all the
223   // outstanding IOs with "waiting for abort task set" flag,
224   // completes them now.
225   while (pending_request_element != NULL)
226   {
227      pending_request =
228         (SCIF_SAS_REQUEST_T*) sci_fast_list_get_object(pending_request_element);
229
230      // The current element may be deleted from the list becasue of
231      // IO completion so advance to the next element early
232      pending_request_element = sci_fast_list_get_next(pending_request_element);
233
234      if ( pending_request->device == fw_device
235           && pending_request->is_waiting_for_abort_task_set == TRUE )
236      {
237         //In case the pending_request is still in the middle of aborting.
238         //abort it again to the core.
239         SCI_STATUS abort_status;
240
241         //Reset the flag now since we are process the read log ext command now.
242         pending_request->is_waiting_for_abort_task_set = FALSE;
243
244         abort_status = scic_controller_terminate_request(
245                           fw_domain->controller->core_object,
246                           fw_device->core_object,
247                           pending_request->core_object
248                        );
249
250         if (abort_status == SCI_FAILURE_INVALID_STATE)
251         {
252            //the request must have not be in aborting state anymore, complete it now.
253            scif_cb_io_request_complete(
254               fw_domain->controller,
255               fw_device,
256               pending_request,
257               SCI_IO_FAILURE_TERMINATED
258            );
259         }
260         //otherwise, the abort succeeded. Since the waiting flag is cleared,
261         //the pending request will be completed later.
262      }
263   }
264#endif //#if !defined(DISABLE_SATI_TASK_MANAGEMENT)
265}
266