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