1230557Sjimharris/*-
2230557Sjimharris * This file is provided under a dual BSD/GPLv2 license.  When using or
3230557Sjimharris * redistributing this file, you may do so under either license.
4230557Sjimharris *
5230557Sjimharris * GPL LICENSE SUMMARY
6230557Sjimharris *
7230557Sjimharris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8230557Sjimharris *
9230557Sjimharris * This program is free software; you can redistribute it and/or modify
10230557Sjimharris * it under the terms of version 2 of the GNU General Public License as
11230557Sjimharris * published by the Free Software Foundation.
12230557Sjimharris *
13230557Sjimharris * This program is distributed in the hope that it will be useful, but
14230557Sjimharris * WITHOUT ANY WARRANTY; without even the implied warranty of
15230557Sjimharris * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16230557Sjimharris * General Public License for more details.
17230557Sjimharris *
18230557Sjimharris * You should have received a copy of the GNU General Public License
19230557Sjimharris * along with this program; if not, write to the Free Software
20230557Sjimharris * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21230557Sjimharris * The full GNU General Public License is included in this distribution
22230557Sjimharris * in the file called LICENSE.GPL.
23230557Sjimharris *
24230557Sjimharris * BSD LICENSE
25230557Sjimharris *
26230557Sjimharris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27230557Sjimharris * All rights reserved.
28230557Sjimharris *
29230557Sjimharris * Redistribution and use in source and binary forms, with or without
30230557Sjimharris * modification, are permitted provided that the following conditions
31230557Sjimharris * are met:
32230557Sjimharris *
33230557Sjimharris *   * Redistributions of source code must retain the above copyright
34230557Sjimharris *     notice, this list of conditions and the following disclaimer.
35230557Sjimharris *   * Redistributions in binary form must reproduce the above copyright
36230557Sjimharris *     notice, this list of conditions and the following disclaimer in
37230557Sjimharris *     the documentation and/or other materials provided with the
38230557Sjimharris *     distribution.
39230557Sjimharris *
40230557Sjimharris * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41230557Sjimharris * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42230557Sjimharris * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43230557Sjimharris * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44230557Sjimharris * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45230557Sjimharris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46230557Sjimharris * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47230557Sjimharris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48230557Sjimharris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49230557Sjimharris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50230557Sjimharris * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51230557Sjimharris */
52230557Sjimharris
53230557Sjimharris#include <sys/cdefs.h>
54230557Sjimharris__FBSDID("$FreeBSD$");
55230557Sjimharris
56230557Sjimharris#include <dev/isci/scil/sati.h>
57230557Sjimharris#include <dev/isci/scil/scif_sas_logger.h>
58230557Sjimharris#include <dev/isci/scil/scif_sas_task_request.h>
59230557Sjimharris#include <dev/isci/scil/scif_sas_controller.h>
60230557Sjimharris#include <dev/isci/scil/scif_sas_remote_device.h>
61230557Sjimharris#include <dev/isci/scil/scif_sas_stp_task_request.h>
62230557Sjimharris#include <dev/isci/scil/scic_task_request.h>
63230557Sjimharris#include <dev/isci/scil/scic_controller.h>
64230557Sjimharris
65230557Sjimharris/**
66230557Sjimharris * @brief This method provides SATA/STP STARTED state specific handling for
67230557Sjimharris *        when the user attempts to complete the supplied IO request.
68230557Sjimharris *        It will perform data/response translation and free NCQ tags
69230557Sjimharris *        if necessary.
70230557Sjimharris *
71230557Sjimharris * @param[in] io_request This parameter specifies the IO request object
72230557Sjimharris *            to be started.
73230557Sjimharris *
74230557Sjimharris * @return This method returns a value indicating if the IO request was
75230557Sjimharris *         successfully completed or not.
76230557Sjimharris */
77230557Sjimharrisstatic
78230557SjimharrisSCI_STATUS scif_sas_stp_core_cb_task_request_complete_handler(
79230557Sjimharris   SCIF_SAS_CONTROLLER_T    * fw_controller,
80230557Sjimharris   SCIF_SAS_REMOTE_DEVICE_T * fw_device,
81230557Sjimharris   SCIF_SAS_REQUEST_T       * fw_request,
82230557Sjimharris   SCI_STATUS               * completion_status
83230557Sjimharris)
84230557Sjimharris{
85230557Sjimharris#if !defined(DISABLE_SATI_TASK_MANAGEMENT)
86230557Sjimharris   SCIF_SAS_TASK_REQUEST_T * fw_task = (SCIF_SAS_TASK_REQUEST_T *) fw_request;
87230557Sjimharris
88230557Sjimharris   SCIF_LOG_TRACE((
89230557Sjimharris      sci_base_object_get_logger(fw_controller),
90230557Sjimharris      SCIF_LOG_OBJECT_TASK_MANAGEMENT,
91230557Sjimharris      "scif_sas_stp_core_cb_task_request_complete_handler(0x%x, 0x%x, 0x%x, 0x%x) enter\n",
92230557Sjimharris      fw_controller, fw_device, fw_request, *completion_status
93230557Sjimharris   ));
94230557Sjimharris
95230557Sjimharris   // Translating the response is only necessary if some sort of error
96230557Sjimharris   // occurred resulting in having the error bit set in the ATA status
97230557Sjimharris   // register and values to decode in the ATA error register.
98230557Sjimharris   if (  (*completion_status == SCI_SUCCESS)
99230557Sjimharris      || (*completion_status == SCI_FAILURE_IO_RESPONSE_VALID) )
100230557Sjimharris   {
101230557Sjimharris      SATI_STATUS sati_status = sati_translate_task_response(
102230557Sjimharris                                   &fw_task->parent.stp.sequence,
103230557Sjimharris                                   fw_task,
104230557Sjimharris                                   fw_task
105230557Sjimharris                                );
106230557Sjimharris
107230557Sjimharris      if (sati_status == SATI_COMPLETE)
108230557Sjimharris         *completion_status = SCI_SUCCESS;
109230557Sjimharris      else if (sati_status == SATI_FAILURE_CHECK_RESPONSE_DATA)
110230557Sjimharris         *completion_status = SCI_FAILURE_IO_RESPONSE_VALID;
111230557Sjimharris      else if (sati_status == SATI_SEQUENCE_INCOMPLETE)
112230557Sjimharris      {
113230557Sjimharris         // The translation indicates that additional SATA requests are
114230557Sjimharris         // necessary to finish the original SCSI request.  As a result,
115230557Sjimharris         // do not complete the IO and begin the next stage of the
116230557Sjimharris         // translation.
117230557Sjimharris         /// @todo multiple ATA commands are required, but not supported yet.
118230557Sjimharris         return SCI_FAILURE;
119230557Sjimharris      }
120230557Sjimharris      else
121230557Sjimharris      {
122230557Sjimharris         // Something unexpected occurred during translation.  Fail the
123230557Sjimharris         // IO request to the user.
124230557Sjimharris         *completion_status = SCI_FAILURE;
125230557Sjimharris      }
126230557Sjimharris   }
127230557Sjimharris   else  //A stp task request sometimes fails.
128230557Sjimharris   {
129230557Sjimharris      if (scif_sas_task_request_get_function(fw_task) == SCI_SAS_ABORT_TASK_SET)
130230557Sjimharris      {
131230557Sjimharris         scif_sas_stp_task_request_abort_task_set_failure_handler(
132230557Sjimharris            fw_device, fw_task);
133230557Sjimharris      }
134230557Sjimharris   }
135230557Sjimharris
136230557Sjimharris   return SCI_SUCCESS;
137230557Sjimharris#else // !defined(DISABLE_SATI_TASK_MANAGEMENT)
138230557Sjimharris   return SCI_FAILURE;
139230557Sjimharris#endif // !defined(DISABLE_SATI_TASK_MANAGEMENT)
140230557Sjimharris}
141230557Sjimharris
142230557Sjimharris/**
143230557Sjimharris * @file
144230557Sjimharris *
145230557Sjimharris * @brief This file contains the method implementations for the
146230557Sjimharris *        SCIF_SAS_STP_TASK_REQUEST object.  The contents will implement
147230557Sjimharris *        SATA/STP specific functionality.
148230557Sjimharris */
149230557SjimharrisSCI_STATUS scif_sas_stp_task_request_construct(
150230557Sjimharris   SCIF_SAS_TASK_REQUEST_T * fw_task
151230557Sjimharris)
152230557Sjimharris{
153230557Sjimharris   SCI_STATUS                 sci_status = SCI_FAILURE;
154230557Sjimharris
155230557Sjimharris#if !defined(DISABLE_SATI_TASK_MANAGEMENT)
156230557Sjimharris   SATI_STATUS                sati_status;
157230557Sjimharris   SCIF_SAS_REMOTE_DEVICE_T * fw_device  = fw_task->parent.device;
158230557Sjimharris
159230557Sjimharris   SCIF_LOG_TRACE((
160230557Sjimharris      sci_base_object_get_logger(fw_task),
161230557Sjimharris      SCIF_LOG_OBJECT_TASK_MANAGEMENT,
162230557Sjimharris      "scif_sas_stp_task_request_construct(0x%x) enter\n",
163230557Sjimharris      fw_task
164230557Sjimharris   ));
165230557Sjimharris
166230557Sjimharris   // The translator will indirectly invoke core methods to set the fields
167230557Sjimharris   // of the ATA register FIS inside of this method.
168230557Sjimharris   sati_status = sati_translate_task_management(
169230557Sjimharris                    &fw_task->parent.stp.sequence,
170230557Sjimharris                    &fw_device->protocol_device.stp_device.sati_device,
171230557Sjimharris                    fw_task,
172230557Sjimharris                    fw_task
173230557Sjimharris                 );
174230557Sjimharris
175230557Sjimharris   if (sati_status == SATI_SUCCESS)
176230557Sjimharris   {
177230557Sjimharris      sci_status = scic_task_request_construct_sata(fw_task->parent.core_object);
178230557Sjimharris      //fw_task->parent.state_handlers = &stp_io_request_constructed_handlers;
179230557Sjimharris      fw_task->parent.protocol_complete_handler =
180230557Sjimharris         scif_sas_stp_core_cb_task_request_complete_handler;
181230557Sjimharris   }
182230557Sjimharris   else
183230557Sjimharris   {
184230557Sjimharris      SCIF_LOG_ERROR((
185230557Sjimharris         sci_base_object_get_logger(fw_task),
186230557Sjimharris         SCIF_LOG_OBJECT_TASK_MANAGEMENT,
187230557Sjimharris         "Task 0x%x received unexpected SAT translation failure 0x%x\n",
188230557Sjimharris         fw_task, sati_status
189230557Sjimharris      ));
190230557Sjimharris   }
191230557Sjimharris#endif // !defined(DISABLE_SATI_TASK_MANAGEMENT)
192230557Sjimharris
193230557Sjimharris   return sci_status;
194230557Sjimharris}
195230557Sjimharris
196230557Sjimharris
197230557Sjimharris/**
198230557Sjimharris * @brief This method provides handling for failed stp TASK MANAGEMENT
199230557Sjimharris *           request.
200230557Sjimharris *
201230557Sjimharris * @param[in] fw_device This parameter specifies the target device the
202230557Sjimharris *            task management request towards to.
203230557Sjimharris * @param[in] fw_request This parameter specifies the failed task management
204230557Sjimharris *            request.
205230557Sjimharris * @param[in] completion_status This parameter sprecifies the completion
206230557Sjimharris *            status of the task management request's core status.
207230557Sjimharris *
208230557Sjimharris * @return None.
209230557Sjimharris */
210230557Sjimharrisvoid scif_sas_stp_task_request_abort_task_set_failure_handler(
211230557Sjimharris   SCIF_SAS_REMOTE_DEVICE_T * fw_device,
212230557Sjimharris   SCIF_SAS_TASK_REQUEST_T  * fw_task
213230557Sjimharris)
214230557Sjimharris{
215230557Sjimharris#if !defined(DISABLE_SATI_TASK_MANAGEMENT)
216230557Sjimharris   SCIF_SAS_DOMAIN_T         * fw_domain = fw_device->domain;
217230557Sjimharris   SCI_FAST_LIST_ELEMENT_T   * pending_request_element;
218230557Sjimharris   SCIF_SAS_REQUEST_T        * pending_request = NULL;
219230557Sjimharris
220230557Sjimharris   pending_request_element = fw_domain->request_list.list_head;
221230557Sjimharris
222230557Sjimharris   // Cycle through the list of IO requests. search all the
223230557Sjimharris   // outstanding IOs with "waiting for abort task set" flag,
224230557Sjimharris   // completes them now.
225230557Sjimharris   while (pending_request_element != NULL)
226230557Sjimharris   {
227230557Sjimharris      pending_request =
228230557Sjimharris         (SCIF_SAS_REQUEST_T*) sci_fast_list_get_object(pending_request_element);
229230557Sjimharris
230230557Sjimharris      // The current element may be deleted from the list becasue of
231230557Sjimharris      // IO completion so advance to the next element early
232230557Sjimharris      pending_request_element = sci_fast_list_get_next(pending_request_element);
233230557Sjimharris
234230557Sjimharris      if ( pending_request->device == fw_device
235230557Sjimharris           && pending_request->is_waiting_for_abort_task_set == TRUE )
236230557Sjimharris      {
237230557Sjimharris         //In case the pending_request is still in the middle of aborting.
238230557Sjimharris         //abort it again to the core.
239230557Sjimharris         SCI_STATUS abort_status;
240230557Sjimharris
241230557Sjimharris         //Reset the flag now since we are process the read log ext command now.
242230557Sjimharris         pending_request->is_waiting_for_abort_task_set = FALSE;
243230557Sjimharris
244230557Sjimharris         abort_status = scic_controller_terminate_request(
245230557Sjimharris                           fw_domain->controller->core_object,
246230557Sjimharris                           fw_device->core_object,
247230557Sjimharris                           pending_request->core_object
248230557Sjimharris                        );
249230557Sjimharris
250230557Sjimharris         if (abort_status == SCI_FAILURE_INVALID_STATE)
251230557Sjimharris         {
252230557Sjimharris            //the request must have not be in aborting state anymore, complete it now.
253230557Sjimharris            scif_cb_io_request_complete(
254230557Sjimharris               fw_domain->controller,
255230557Sjimharris               fw_device,
256230557Sjimharris               pending_request,
257231296Sjimharris               SCI_IO_FAILURE_TERMINATED
258230557Sjimharris            );
259230557Sjimharris         }
260230557Sjimharris         //otherwise, the abort succeeded. Since the waiting flag is cleared,
261230557Sjimharris         //the pending request will be completed later.
262230557Sjimharris      }
263230557Sjimharris   }
264230557Sjimharris#endif //#if !defined(DISABLE_SATI_TASK_MANAGEMENT)
265230557Sjimharris}
266