blob: 700708c82678277a3b4640d2f729ef35087702d3 [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
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 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55
56#include <linux/completion.h>
Dan Williams50e7f9b2011-03-09 21:27:46 -080057#include <linux/irqflags.h>
Dave Jiangaf5ae892011-05-04 17:53:24 -070058#include "sas.h"
Jeff Skirvin61aaff42011-06-21 12:16:33 -070059#include <scsi/libsas.h>
Dan Williams88f3b622011-04-22 19:18:03 -070060#include "remote_device.h"
61#include "remote_node_context.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070062#include "isci.h"
63#include "request.h"
64#include "sata.h"
65#include "task.h"
Dan Williams312e0c22011-06-28 13:47:09 -070066#include "host.h"
Dave Jiangaf5ae892011-05-04 17:53:24 -070067
Dan Williams50e7f9b2011-03-09 21:27:46 -080068/**
Dan Williams1077a572011-03-11 10:13:51 -080069* isci_task_refuse() - complete the request to the upper layer driver in
70* the case where an I/O needs to be completed back in the submit path.
71* @ihost: host on which the the request was queued
72* @task: request to complete
73* @response: response code for the completed task.
74* @status: status code for the completed task.
Dan Williams50e7f9b2011-03-09 21:27:46 -080075*
Dan Williams50e7f9b2011-03-09 21:27:46 -080076*/
Dan Williams1077a572011-03-11 10:13:51 -080077static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
78 enum service_response response,
79 enum exec_status status)
Dan Williams50e7f9b2011-03-09 21:27:46 -080080
Dan Williams1077a572011-03-11 10:13:51 -080081{
82 enum isci_completion_selection disposition;
83
84 disposition = isci_perform_normal_io_completion;
85 disposition = isci_task_set_completion_status(task, response, status,
86 disposition);
Dan Williams50e7f9b2011-03-09 21:27:46 -080087
88 /* Tasks aborted specifically by a call to the lldd_abort_task
Dan Williams1077a572011-03-11 10:13:51 -080089 * function should not be completed to the host in the regular path.
90 */
91 switch (disposition) {
Dan Williams50e7f9b2011-03-09 21:27:46 -080092 case isci_perform_normal_io_completion:
93 /* Normal notification (task_done) */
Dan Williams1077a572011-03-11 10:13:51 -080094 dev_dbg(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -070095 "%s: Normal - task = %p, response=%d, "
96 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -080097 __func__, task, response, status);
98
Dan Williams1077a572011-03-11 10:13:51 -080099 task->lldd_task = NULL;
Dan Williams50e7f9b2011-03-09 21:27:46 -0800100
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700101 isci_execpath_callback(ihost, task, task->task_done);
Dan Williams50e7f9b2011-03-09 21:27:46 -0800102 break;
103
104 case isci_perform_aborted_io_completion:
105 /* No notification because this request is already in the
106 * abort path.
107 */
Dan Williams1077a572011-03-11 10:13:51 -0800108 dev_warn(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700109 "%s: Aborted - task = %p, response=%d, "
110 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -0800111 __func__, task, response, status);
112 break;
113
114 case isci_perform_error_io_completion:
115 /* Use sas_task_abort */
Dan Williams1077a572011-03-11 10:13:51 -0800116 dev_warn(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700117 "%s: Error - task = %p, response=%d, "
118 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -0800119 __func__, task, response, status);
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700120
121 isci_execpath_callback(ihost, task, sas_task_abort);
Dan Williams50e7f9b2011-03-09 21:27:46 -0800122 break;
123
124 default:
Dan Williams1077a572011-03-11 10:13:51 -0800125 dev_warn(&ihost->pdev->dev,
Dan Williams50e7f9b2011-03-09 21:27:46 -0800126 "%s: isci task notification default case!",
127 __func__);
128 sas_task_abort(task);
129 break;
130 }
131}
Dan Williams6f231dd2011-07-02 22:56:22 -0700132
Dan Williams1077a572011-03-11 10:13:51 -0800133#define for_each_sas_task(num, task) \
134 for (; num > 0; num--,\
135 task = list_entry(task->list.next, struct sas_task, list))
136
Jeff Skirvin9274f452011-06-23 17:09:02 -0700137
138static inline int isci_device_io_ready(struct isci_remote_device *idev,
139 struct sas_task *task)
140{
141 return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
142 (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
143 isci_task_is_ncq_recovery(task))
144 : 0;
145}
Dan Williams6f231dd2011-07-02 22:56:22 -0700146/**
147 * isci_task_execute_task() - This function is one of the SAS Domain Template
148 * functions. This function is called by libsas to send a task down to
149 * hardware.
150 * @task: This parameter specifies the SAS task to send.
151 * @num: This parameter specifies the number of tasks to queue.
152 * @gfp_flags: This parameter specifies the context of this call.
153 *
154 * status, zero indicates success.
155 */
156int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
157{
Dan Williams4393aa42011-03-31 13:10:44 -0700158 struct isci_host *ihost = dev_to_ihost(task->dev);
Dan Williams209fae12011-06-13 17:39:44 -0700159 struct isci_remote_device *idev;
Dan Williamsf2088262011-06-16 11:26:12 -0700160 unsigned long flags;
161 bool io_ready;
Dan Williams312e0c22011-06-28 13:47:09 -0700162 u16 tag;
Dan Williams6f231dd2011-07-02 22:56:22 -0700163
Dan Williams1077a572011-03-11 10:13:51 -0800164 dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
Dan Williams6f231dd2011-07-02 22:56:22 -0700165
Dan Williams1077a572011-03-11 10:13:51 -0800166 for_each_sas_task(num, task) {
Dan Williams312e0c22011-06-28 13:47:09 -0700167 enum sci_status status = SCI_FAILURE;
168
Dan Williams209fae12011-06-13 17:39:44 -0700169 spin_lock_irqsave(&ihost->scic_lock, flags);
170 idev = isci_lookup_device(task->dev);
Jeff Skirvin9274f452011-06-23 17:09:02 -0700171 io_ready = isci_device_io_ready(idev, task);
Dan Williams312e0c22011-06-28 13:47:09 -0700172 tag = isci_alloc_tag(ihost);
Dan Williams209fae12011-06-13 17:39:44 -0700173 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700174
Dan Williamsf2088262011-06-16 11:26:12 -0700175 dev_dbg(&ihost->pdev->dev,
176 "task: %p, num: %d dev: %p idev: %p:%#lx cmd = %p\n",
177 task, num, task->dev, idev, idev ? idev->flags : 0,
178 task->uldd_task);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700179
Dan Williamsf2088262011-06-16 11:26:12 -0700180 if (!idev) {
181 isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
182 SAS_DEVICE_UNKNOWN);
Dan Williams312e0c22011-06-28 13:47:09 -0700183 } else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
Dan Williamsf2088262011-06-16 11:26:12 -0700184 /* Indicate QUEUE_FULL so that the scsi midlayer
185 * retries.
186 */
187 isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
188 SAS_QUEUE_FULL);
Dan Williams6f231dd2011-07-02 22:56:22 -0700189 } else {
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700190 /* There is a device and it's ready for I/O. */
191 spin_lock_irqsave(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700192
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700193 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
Dan Williamsf2088262011-06-16 11:26:12 -0700194 /* The I/O was aborted. */
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700195 spin_unlock_irqrestore(&task->task_state_lock,
196 flags);
197
Dan Williams1077a572011-03-11 10:13:51 -0800198 isci_task_refuse(ihost, task,
199 SAS_TASK_UNDELIVERED,
200 SAM_STAT_TASK_ABORTED);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700201 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700202 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
203 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700204
205 /* build and send the request. */
Dan Williamsdb056252011-06-17 14:18:39 -0700206 status = isci_request_execute(ihost, idev, task, tag);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700207
208 if (status != SCI_SUCCESS) {
209
210 spin_lock_irqsave(&task->task_state_lock, flags);
211 /* Did not really start this command. */
212 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
213 spin_unlock_irqrestore(&task->task_state_lock, flags);
214
215 /* Indicate QUEUE_FULL so that the scsi
216 * midlayer retries. if the request
217 * failed for remote device reasons,
218 * it gets returned as
219 * SAS_TASK_UNDELIVERED next time
220 * through.
221 */
Dan Williams1077a572011-03-11 10:13:51 -0800222 isci_task_refuse(ihost, task,
223 SAS_TASK_COMPLETE,
224 SAS_QUEUE_FULL);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700225 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700226 }
227 }
Dan Williams312e0c22011-06-28 13:47:09 -0700228 if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
229 spin_lock_irqsave(&ihost->scic_lock, flags);
230 /* command never hit the device, so just free
231 * the tci and skip the sequence increment
232 */
233 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
234 spin_unlock_irqrestore(&ihost->scic_lock, flags);
235 }
Dan Williams209fae12011-06-13 17:39:44 -0700236 isci_put_device(idev);
Dan Williams1077a572011-03-11 10:13:51 -0800237 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700238 return 0;
239}
240
Dan Williams0d0cf142011-06-13 00:51:30 -0700241static struct isci_request *isci_task_request_build(struct isci_host *ihost,
Dan Williams209fae12011-06-13 17:39:44 -0700242 struct isci_remote_device *idev,
Dan Williams312e0c22011-06-28 13:47:09 -0700243 u16 tag, struct isci_tmf *isci_tmf)
Dan Williams6f231dd2011-07-02 22:56:22 -0700244{
Dan Williams6f231dd2011-07-02 22:56:22 -0700245 enum sci_status status = SCI_FAILURE;
Dan Williams0d0cf142011-06-13 00:51:30 -0700246 struct isci_request *ireq = NULL;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700247 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700248
Dan Williams0d0cf142011-06-13 00:51:30 -0700249 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700250 "%s: isci_tmf = %p\n", __func__, isci_tmf);
251
Dan Williams0d0cf142011-06-13 00:51:30 -0700252 dev = idev->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700253
254 /* do common allocation and init of request object. */
Dan Williamsdb056252011-06-17 14:18:39 -0700255 ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
Dan Williams0d0cf142011-06-13 00:51:30 -0700256 if (!ireq)
257 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700258
259 /* let the core do it's construct. */
Dan Williams312e0c22011-06-28 13:47:09 -0700260 status = scic_task_request_construct(&ihost->sci, &idev->sci, tag,
Dan Williams5076a1a2011-06-27 14:57:03 -0700261 ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700262
263 if (status != SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700264 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700265 "%s: scic_task_request_construct failed - "
266 "status = 0x%x\n",
267 __func__,
268 status);
Dan Williamsdb056252011-06-17 14:18:39 -0700269 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700270 }
271
Dan Williamsa1a113b2011-04-21 18:44:45 -0700272 /* XXX convert to get this from task->tproto like other drivers */
273 if (dev->dev_type == SAS_END_DEV) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700274 isci_tmf->proto = SAS_PROTOCOL_SSP;
Dan Williams5076a1a2011-06-27 14:57:03 -0700275 status = scic_task_request_construct_ssp(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700276 if (status != SCI_SUCCESS)
Dan Williamsdb056252011-06-17 14:18:39 -0700277 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700278 }
279
Dan Williamsa1a113b2011-04-21 18:44:45 -0700280 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700281 isci_tmf->proto = SAS_PROTOCOL_SATA;
Dan Williams0d0cf142011-06-13 00:51:30 -0700282 status = isci_sata_management_task_request_build(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700283
284 if (status != SCI_SUCCESS)
Dan Williamsdb056252011-06-17 14:18:39 -0700285 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700286 }
Dan Williams0d0cf142011-06-13 00:51:30 -0700287 return ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700288}
289
Dan Williams209fae12011-06-13 17:39:44 -0700290int isci_task_execute_tmf(struct isci_host *ihost,
291 struct isci_remote_device *isci_device,
292 struct isci_tmf *tmf, unsigned long timeout_ms)
Dan Williams6f231dd2011-07-02 22:56:22 -0700293{
294 DECLARE_COMPLETION_ONSTACK(completion);
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700295 enum sci_task_status status = SCI_TASK_FAILURE;
Dan Williams6f231dd2011-07-02 22:56:22 -0700296 struct scic_sds_remote_device *sci_device;
Dan Williams0d0cf142011-06-13 00:51:30 -0700297 struct isci_request *ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700298 int ret = TMF_RESP_FUNC_FAILED;
299 unsigned long flags;
Edmund Nadolskifd183882011-05-19 12:00:15 +0000300 unsigned long timeleft;
Dan Williams312e0c22011-06-28 13:47:09 -0700301 u16 tag;
302
303 spin_lock_irqsave(&ihost->scic_lock, flags);
304 tag = isci_alloc_tag(ihost);
305 spin_unlock_irqrestore(&ihost->scic_lock, flags);
306
307 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
308 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700309
310 /* sanity check, return TMF_RESP_FUNC_FAILED
311 * if the device is not there and ready.
312 */
Jeff Skirvin9274f452011-06-23 17:09:02 -0700313 if (!isci_device ||
314 (!test_bit(IDEV_IO_READY, &isci_device->flags) &&
315 !test_bit(IDEV_IO_NCQERROR, &isci_device->flags))) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700316 dev_dbg(&ihost->pdev->dev,
Dan Williamsf2088262011-06-16 11:26:12 -0700317 "%s: isci_device = %p not ready (%#lx)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700318 __func__,
Dan Williamsf2088262011-06-16 11:26:12 -0700319 isci_device, isci_device ? isci_device->flags : 0);
Dan Williams312e0c22011-06-28 13:47:09 -0700320 goto err_tci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700321 } else
Dan Williams0d0cf142011-06-13 00:51:30 -0700322 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700323 "%s: isci_device = %p\n",
324 __func__, isci_device);
325
Dan Williams57f20f42011-04-21 18:14:45 -0700326 sci_device = &isci_device->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700327
328 /* Assign the pointer to the TMF's completion kernel wait structure. */
329 tmf->complete = &completion;
330
Dan Williams312e0c22011-06-28 13:47:09 -0700331 ireq = isci_task_request_build(ihost, isci_device, tag, tmf);
332 if (!ireq)
333 goto err_tci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700334
Dan Williams0d0cf142011-06-13 00:51:30 -0700335 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700336
337 /* start the TMF io. */
Dan Williams312e0c22011-06-28 13:47:09 -0700338 status = scic_controller_start_task(&ihost->sci,
339 sci_device,
Dan Williams5076a1a2011-06-27 14:57:03 -0700340 ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700341
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700342 if (status != SCI_TASK_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700343 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700344 "%s: start_io failed - status = 0x%x, request = %p\n",
345 __func__,
346 status,
Dan Williams0d0cf142011-06-13 00:51:30 -0700347 ireq);
348 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsdb056252011-06-17 14:18:39 -0700349 goto err_tci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700350 }
351
Dan Williams6f231dd2011-07-02 22:56:22 -0700352 if (tmf->cb_state_func != NULL)
353 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
354
Dan Williams0d0cf142011-06-13 00:51:30 -0700355 isci_request_change_state(ireq, started);
Dan Williams6f231dd2011-07-02 22:56:22 -0700356
357 /* add the request to the remote device request list. */
Dan Williams0d0cf142011-06-13 00:51:30 -0700358 list_add(&ireq->dev_node, &isci_device->reqs_in_process);
Dan Williams6f231dd2011-07-02 22:56:22 -0700359
Dan Williams0d0cf142011-06-13 00:51:30 -0700360 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700361
362 /* Wait for the TMF to complete, or a timeout. */
Edmund Nadolskifd183882011-05-19 12:00:15 +0000363 timeleft = wait_for_completion_timeout(&completion,
Dan Williams086a0da2011-06-21 16:23:03 -0700364 msecs_to_jiffies(timeout_ms));
Edmund Nadolskifd183882011-05-19 12:00:15 +0000365
366 if (timeleft == 0) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700367 spin_lock_irqsave(&ihost->scic_lock, flags);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000368
369 if (tmf->cb_state_func != NULL)
370 tmf->cb_state_func(isci_tmf_timed_out, tmf, tmf->cb_data);
371
Dan Williams086a0da2011-06-21 16:23:03 -0700372 scic_controller_terminate_request(&ihost->sci,
373 &isci_device->sci,
Dan Williams5076a1a2011-06-27 14:57:03 -0700374 ireq);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000375
Dan Williams0d0cf142011-06-13 00:51:30 -0700376 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams086a0da2011-06-21 16:23:03 -0700377
378 wait_for_completion(tmf->complete);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000379 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700380
381 isci_print_tmf(tmf);
382
383 if (tmf->status == SCI_SUCCESS)
384 ret = TMF_RESP_FUNC_COMPLETE;
385 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700386 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700387 "%s: tmf.status == "
388 "SCI_FAILURE_IO_RESPONSE_VALID\n",
389 __func__);
390 ret = TMF_RESP_FUNC_COMPLETE;
391 }
392 /* Else - leave the default "failed" status alone. */
393
Dan Williams0d0cf142011-06-13 00:51:30 -0700394 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700395 "%s: completed request = %p\n",
396 __func__,
Dan Williams0d0cf142011-06-13 00:51:30 -0700397 ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700398
Dan Williams6f231dd2011-07-02 22:56:22 -0700399 return ret;
Dan Williams312e0c22011-06-28 13:47:09 -0700400
Dan Williams312e0c22011-06-28 13:47:09 -0700401 err_tci:
402 spin_lock_irqsave(&ihost->scic_lock, flags);
403 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
404 spin_unlock_irqrestore(&ihost->scic_lock, flags);
405
406 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700407}
408
409void isci_task_build_tmf(
410 struct isci_tmf *tmf,
Dan Williams6f231dd2011-07-02 22:56:22 -0700411 enum isci_tmf_function_codes code,
412 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
413 struct isci_tmf *,
414 void *),
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800415 void *cb_data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700416{
Dan Williams6f231dd2011-07-02 22:56:22 -0700417 memset(tmf, 0, sizeof(*tmf));
418
Dan Williams6f231dd2011-07-02 22:56:22 -0700419 tmf->tmf_code = code;
Dan Williams6f231dd2011-07-02 22:56:22 -0700420 tmf->cb_state_func = tmf_sent_cb;
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800421 tmf->cb_data = cb_data;
422}
Jeff Skirvin1fad9e92011-03-04 14:06:46 -0800423
Dan Williams35173d52011-03-26 16:43:01 -0700424static void isci_task_build_abort_task_tmf(
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800425 struct isci_tmf *tmf,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800426 enum isci_tmf_function_codes code,
427 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
428 struct isci_tmf *,
429 void *),
430 struct isci_request *old_request)
431{
Dan Williams209fae12011-06-13 17:39:44 -0700432 isci_task_build_tmf(tmf, code, tmf_sent_cb,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800433 (void *)old_request);
434 tmf->io_tag = old_request->io_tag;
Dan Williams6f231dd2011-07-02 22:56:22 -0700435}
436
Dan Williams6f231dd2011-07-02 22:56:22 -0700437/**
438 * isci_task_validate_request_to_abort() - This function checks the given I/O
439 * against the "started" state. If the request is still "started", it's
440 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
441 * BEFORE CALLING THIS FUNCTION.
442 * @isci_request: This parameter specifies the request object to control.
443 * @isci_host: This parameter specifies the ISCI host object
444 * @isci_device: This is the device to which the request is pending.
445 * @aborted_io_completion: This is a completion structure that will be added to
446 * the request in case it is changed to aborting; this completion is
447 * triggered when the request is fully completed.
448 *
449 * Either "started" on successful change of the task status to "aborted", or
450 * "unallocated" if the task cannot be controlled.
451 */
452static enum isci_request_status isci_task_validate_request_to_abort(
453 struct isci_request *isci_request,
454 struct isci_host *isci_host,
455 struct isci_remote_device *isci_device,
456 struct completion *aborted_io_completion)
457{
458 enum isci_request_status old_state = unallocated;
459
460 /* Only abort the task if it's in the
461 * device's request_in_process list
462 */
463 if (isci_request && !list_empty(&isci_request->dev_node)) {
464 old_state = isci_request_change_started_to_aborted(
465 isci_request, aborted_io_completion);
466
Dan Williams6f231dd2011-07-02 22:56:22 -0700467 }
468
469 return old_state;
470}
471
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700472/**
473* isci_request_cleanup_completed_loiterer() - This function will take care of
474* the final cleanup on any request which has been explicitly terminated.
475* @isci_host: This parameter specifies the ISCI host object
476* @isci_device: This is the device to which the request is pending.
477* @isci_request: This parameter specifies the terminated request object.
478* @task: This parameter is the libsas I/O request.
479*/
Dan Williams6f231dd2011-07-02 22:56:22 -0700480static void isci_request_cleanup_completed_loiterer(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700481 struct isci_host *isci_host,
Dan Williams6f231dd2011-07-02 22:56:22 -0700482 struct isci_remote_device *isci_device,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700483 struct isci_request *isci_request,
484 struct sas_task *task)
Dan Williams6f231dd2011-07-02 22:56:22 -0700485{
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700486 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700487
488 dev_dbg(&isci_host->pdev->dev,
489 "%s: isci_device=%p, request=%p, task=%p\n",
Jeff Skirvin18d3d722011-03-04 14:06:38 -0800490 __func__, isci_device, isci_request, task);
Dan Williams6f231dd2011-07-02 22:56:22 -0700491
Jeff Skirvina5fde222011-03-04 14:06:42 -0800492 if (task != NULL) {
493
494 spin_lock_irqsave(&task->task_state_lock, flags);
495 task->lldd_task = NULL;
496
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700497 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
498
Jeff Skirvina5fde222011-03-04 14:06:42 -0800499 isci_set_task_doneflags(task);
500
501 /* If this task is not in the abort path, call task_done. */
502 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
503
504 spin_unlock_irqrestore(&task->task_state_lock, flags);
505 task->task_done(task);
506 } else
507 spin_unlock_irqrestore(&task->task_state_lock, flags);
508 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800509
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700510 if (isci_request != NULL) {
511 spin_lock_irqsave(&isci_host->scic_lock, flags);
512 list_del_init(&isci_request->dev_node);
513 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800514 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800515}
516
Dan Williams6f231dd2011-07-02 22:56:22 -0700517/**
518 * isci_terminate_request_core() - This function will terminate the given
519 * request, and wait for it to complete. This function must only be called
520 * from a thread that can wait. Note that the request is terminated and
521 * completed (back to the host, if started there).
522 * @isci_host: This SCU.
523 * @isci_device: The target.
524 * @isci_request: The I/O request to be terminated.
525 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700526 */
527static void isci_terminate_request_core(
528 struct isci_host *isci_host,
529 struct isci_remote_device *isci_device,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800530 struct isci_request *isci_request)
Dan Williams6f231dd2011-07-02 22:56:22 -0700531{
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800532 enum sci_status status = SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700533 bool was_terminated = false;
534 bool needs_cleanup_handling = false;
535 enum isci_request_status request_status;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700536 unsigned long flags;
537 unsigned long termination_completed = 1;
538 struct completion *io_request_completion;
539 struct sas_task *task;
Dan Williams6f231dd2011-07-02 22:56:22 -0700540
541 dev_dbg(&isci_host->pdev->dev,
542 "%s: device = %p; request = %p\n",
543 __func__, isci_device, isci_request);
544
Dan Williams6f231dd2011-07-02 22:56:22 -0700545 spin_lock_irqsave(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800546
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700547 io_request_completion = isci_request->io_request_completion;
548
549 task = (isci_request->ttype == io_task)
550 ? isci_request_access_task(isci_request)
551 : NULL;
552
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800553 /* Note that we are not going to control
Dan Williams38d88792011-06-23 14:33:48 -0700554 * the target to abort the request.
555 */
556 set_bit(IREQ_COMPLETE_IN_TARGET, &isci_request->flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800557
Dan Williams6f231dd2011-07-02 22:56:22 -0700558 /* Make sure the request wasn't just sitting around signalling
559 * device condition (if the request handle is NULL, then the
560 * request completed but needed additional handling here).
561 */
Dan Williams38d88792011-06-23 14:33:48 -0700562 if (!test_bit(IREQ_TERMINATED, &isci_request->flags)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700563 was_terminated = true;
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800564 needs_cleanup_handling = true;
Dan Williams6f231dd2011-07-02 22:56:22 -0700565 status = scic_controller_terminate_request(
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000566 &isci_host->sci,
Dan Williams57f20f42011-04-21 18:14:45 -0700567 &isci_device->sci,
Dan Williams5076a1a2011-06-27 14:57:03 -0700568 isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700569 }
570 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
571
572 /*
573 * The only time the request to terminate will
574 * fail is when the io request is completed and
575 * being aborted.
576 */
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800577 if (status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700578 dev_err(&isci_host->pdev->dev,
579 "%s: scic_controller_terminate_request"
580 " returned = 0x%x\n",
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700581 __func__, status);
582
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800583 isci_request->io_request_completion = NULL;
584
585 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700586 if (was_terminated) {
587 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700588 "%s: before completion wait (%p/%p)\n",
589 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700590
591 /* Wait here for the request to complete. */
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700592 #define TERMINATION_TIMEOUT_MSEC 500
593 termination_completed
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800594 = wait_for_completion_timeout(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700595 io_request_completion,
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800596 msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
Dan Williams6f231dd2011-07-02 22:56:22 -0700597
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700598 if (!termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800599
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700600 /* The request to terminate has timed out. */
601 spin_lock_irqsave(&isci_host->scic_lock,
602 flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800603
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700604 /* Check for state changes. */
Dan Williams38d88792011-06-23 14:33:48 -0700605 if (!test_bit(IREQ_TERMINATED, &isci_request->flags)) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800606
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700607 /* The best we can do is to have the
608 * request die a silent death if it
609 * ever really completes.
610 *
611 * Set the request state to "dead",
612 * and clear the task pointer so that
613 * an actual completion event callback
614 * doesn't do anything.
615 */
616 isci_request->status = dead;
617 isci_request->io_request_completion
618 = NULL;
619
620 if (isci_request->ttype == io_task) {
621
622 /* Break links with the
623 * sas_task.
624 */
625 isci_request->ttype_ptr.io_task_ptr
626 = NULL;
627 }
628 } else
629 termination_completed = 1;
630
631 spin_unlock_irqrestore(&isci_host->scic_lock,
632 flags);
633
634 if (!termination_completed) {
635
636 dev_err(&isci_host->pdev->dev,
637 "%s: *** Timeout waiting for "
638 "termination(%p/%p)\n",
639 __func__, io_request_completion,
640 isci_request);
641
642 /* The request can no longer be referenced
643 * safely since it may go away if the
644 * termination every really does complete.
645 */
646 isci_request = NULL;
647 }
648 }
649 if (termination_completed)
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800650 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700651 "%s: after completion wait (%p/%p)\n",
652 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700653 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800654
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700655 if (termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800656
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700657 isci_request->io_request_completion = NULL;
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800658
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700659 /* Peek at the status of the request. This will tell
660 * us if there was special handling on the request such that it
661 * needs to be detached and freed here.
662 */
663 spin_lock_irqsave(&isci_request->state_lock, flags);
664 request_status = isci_request_get_state(isci_request);
665
666 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
667 && ((request_status == aborted)
668 || (request_status == aborting)
669 || (request_status == terminating)
670 || (request_status == completed)
671 || (request_status == dead)
672 )
673 ) {
674
675 /* The completion routine won't free a request in
676 * the aborted/aborting/etc. states, so we do
677 * it here.
678 */
679 needs_cleanup_handling = true;
680 }
681 spin_unlock_irqrestore(&isci_request->state_lock, flags);
682
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800683 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700684 if (needs_cleanup_handling)
685 isci_request_cleanup_completed_loiterer(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700686 isci_host, isci_device, isci_request, task);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800687 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700688}
689
690/**
691 * isci_terminate_pending_requests() - This function will change the all of the
692 * requests on the given device's state to "aborting", will terminate the
693 * requests, and wait for them to complete. This function must only be
694 * called from a thread that can wait. Note that the requests are all
695 * terminated and completed (back to the host, if started there).
696 * @isci_host: This parameter specifies SCU.
697 * @isci_device: This parameter specifies the target.
698 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700699 */
Dan Williams980d3ae2011-06-20 15:11:22 -0700700void isci_terminate_pending_requests(struct isci_host *ihost,
701 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700702{
Dan Williams980d3ae2011-06-20 15:11:22 -0700703 struct completion request_completion;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700704 enum isci_request_status old_state;
Dan Williams980d3ae2011-06-20 15:11:22 -0700705 unsigned long flags;
706 LIST_HEAD(list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700707
Dan Williams980d3ae2011-06-20 15:11:22 -0700708 spin_lock_irqsave(&ihost->scic_lock, flags);
709 list_splice_init(&idev->reqs_in_process, &list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700710
Dan Williams980d3ae2011-06-20 15:11:22 -0700711 /* assumes that isci_terminate_request_core deletes from the list */
712 while (!list_empty(&list)) {
713 struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -0700714
Dan Williams980d3ae2011-06-20 15:11:22 -0700715 /* Change state to "terminating" if it is currently
716 * "started".
717 */
718 old_state = isci_request_change_started_to_newstate(ireq,
719 &request_completion,
720 terminating);
721 switch (old_state) {
722 case started:
723 case completed:
724 case aborting:
725 break;
726 default:
727 /* termination in progress, or otherwise dispositioned.
728 * We know the request was on 'list' so should be safe
729 * to move it back to reqs_in_process
730 */
731 list_move(&ireq->dev_node, &idev->reqs_in_process);
732 ireq = NULL;
733 break;
734 }
735
736 if (!ireq)
737 continue;
738 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800739
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700740 init_completion(&request_completion);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800741
Dan Williams980d3ae2011-06-20 15:11:22 -0700742 dev_dbg(&ihost->pdev->dev,
743 "%s: idev=%p request=%p; task=%p old_state=%d\n",
744 __func__, idev, ireq,
745 ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL,
746 old_state);
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700747
Dan Williams980d3ae2011-06-20 15:11:22 -0700748 /* If the old_state is started:
749 * This request was not already being aborted. If it had been,
750 * then the aborting I/O (ie. the TMF request) would not be in
751 * the aborting state, and thus would be terminated here. Note
752 * that since the TMF completion's call to the kernel function
753 * "complete()" does not happen until the pending I/O request
754 * terminate fully completes, we do not have to implement a
755 * special wait here for already aborting requests - the
756 * termination of the TMF request will force the request
757 * to finish it's already started terminate.
758 *
759 * If old_state == completed:
760 * This request completed from the SCU hardware perspective
761 * and now just needs cleaning up in terms of freeing the
762 * request and potentially calling up to libsas.
763 *
764 * If old_state == aborting:
765 * This request has already gone through a TMF timeout, but may
766 * not have been terminated; needs cleaning up at least.
767 */
768 isci_terminate_request_core(ihost, idev, ireq);
769 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700770 }
Dan Williams980d3ae2011-06-20 15:11:22 -0700771 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700772}
773
774/**
775 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
776 * Template functions.
777 * @lun: This parameter specifies the lun to be reset.
778 *
779 * status, zero indicates success.
780 */
781static int isci_task_send_lu_reset_sas(
782 struct isci_host *isci_host,
783 struct isci_remote_device *isci_device,
784 u8 *lun)
785{
786 struct isci_tmf tmf;
787 int ret = TMF_RESP_FUNC_FAILED;
788
789 dev_dbg(&isci_host->pdev->dev,
790 "%s: isci_host = %p, isci_device = %p\n",
791 __func__, isci_host, isci_device);
792 /* Send the LUN reset to the target. By the time the call returns,
793 * the TMF has fully exected in the target (in which case the return
794 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
795 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
796 */
Dan Williams209fae12011-06-13 17:39:44 -0700797 isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset, NULL, NULL);
Dan Williams6f231dd2011-07-02 22:56:22 -0700798
799 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
Dan Williams209fae12011-06-13 17:39:44 -0700800 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
Dan Williams6f231dd2011-07-02 22:56:22 -0700801
802 if (ret == TMF_RESP_FUNC_COMPLETE)
803 dev_dbg(&isci_host->pdev->dev,
804 "%s: %p: TMF_LU_RESET passed\n",
805 __func__, isci_device);
806 else
807 dev_dbg(&isci_host->pdev->dev,
808 "%s: %p: TMF_LU_RESET failed (%x)\n",
809 __func__, isci_device, ret);
810
811 return ret;
812}
813
814/**
815 * isci_task_lu_reset() - This function is one of the SAS Domain Template
816 * functions. This is one of the Task Management functoins called by libsas,
817 * to reset the given lun. Note the assumption that while this call is
818 * executing, no I/O will be sent by the host to the device.
819 * @lun: This parameter specifies the lun to be reset.
820 *
821 * status, zero indicates success.
822 */
Dan Williams4393aa42011-03-31 13:10:44 -0700823int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
Dan Williams6f231dd2011-07-02 22:56:22 -0700824{
Dan Williams4393aa42011-03-31 13:10:44 -0700825 struct isci_host *isci_host = dev_to_ihost(domain_device);
Dan Williams209fae12011-06-13 17:39:44 -0700826 struct isci_remote_device *isci_device;
827 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700828 int ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700829
Dan Williams209fae12011-06-13 17:39:44 -0700830 spin_lock_irqsave(&isci_host->scic_lock, flags);
831 isci_device = isci_lookup_device(domain_device);
832 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700833
Dan Williams4393aa42011-03-31 13:10:44 -0700834 dev_dbg(&isci_host->pdev->dev,
835 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700836 __func__, domain_device, isci_host, isci_device);
837
Dan Williams209fae12011-06-13 17:39:44 -0700838 if (isci_device)
Dan Williamsd06b4872011-05-02 13:59:25 -0700839 set_bit(IDEV_EH, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700840
841 /* If there is a device reset pending on any request in the
842 * device's list, fail this LUN reset request in order to
843 * escalate to the device reset.
844 */
Dan Williams209fae12011-06-13 17:39:44 -0700845 if (!isci_device ||
Dan Williams4393aa42011-03-31 13:10:44 -0700846 isci_device_is_reset_pending(isci_host, isci_device)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700847 dev_warn(&isci_host->pdev->dev,
Dan Williams4393aa42011-03-31 13:10:44 -0700848 "%s: No dev (%p), or "
Dan Williams6f231dd2011-07-02 22:56:22 -0700849 "RESET PENDING: domain_device=%p\n",
Dan Williams4393aa42011-03-31 13:10:44 -0700850 __func__, isci_device, domain_device);
Dan Williams209fae12011-06-13 17:39:44 -0700851 ret = TMF_RESP_FUNC_FAILED;
852 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -0700853 }
854
Dan Williams6f231dd2011-07-02 22:56:22 -0700855 /* Send the task management part of the reset. */
856 if (sas_protocol_ata(domain_device->tproto)) {
Dan Williams4393aa42011-03-31 13:10:44 -0700857 ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
Dan Williams6f231dd2011-07-02 22:56:22 -0700858 } else
859 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
860
861 /* If the LUN reset worked, all the I/O can now be terminated. */
862 if (ret == TMF_RESP_FUNC_COMPLETE)
863 /* Terminate all I/O now. */
864 isci_terminate_pending_requests(isci_host,
Dan Williams980d3ae2011-06-20 15:11:22 -0700865 isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700866
Dan Williams209fae12011-06-13 17:39:44 -0700867 out:
868 isci_put_device(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700869 return ret;
870}
871
872
873/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
874int isci_task_clear_nexus_port(struct asd_sas_port *port)
875{
876 return TMF_RESP_FUNC_FAILED;
877}
878
879
880
881int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
882{
883 return TMF_RESP_FUNC_FAILED;
884}
885
Dan Williams6f231dd2011-07-02 22:56:22 -0700886/* Task Management Functions. Must be called from process context. */
887
888/**
889 * isci_abort_task_process_cb() - This is a helper function for the abort task
890 * TMF command. It manages the request state with respect to the successful
891 * transmission / completion of the abort task request.
892 * @cb_state: This parameter specifies when this function was called - after
893 * the TMF request has been started and after it has timed-out.
894 * @tmf: This parameter specifies the TMF in progress.
895 *
896 *
897 */
898static void isci_abort_task_process_cb(
899 enum isci_tmf_cb_state cb_state,
900 struct isci_tmf *tmf,
901 void *cb_data)
902{
903 struct isci_request *old_request;
904
905 old_request = (struct isci_request *)cb_data;
906
907 dev_dbg(&old_request->isci_host->pdev->dev,
908 "%s: tmf=%p, old_request=%p\n",
909 __func__, tmf, old_request);
910
911 switch (cb_state) {
912
913 case isci_tmf_started:
914 /* The TMF has been started. Nothing to do here, since the
915 * request state was already set to "aborted" by the abort
916 * task function.
917 */
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700918 if ((old_request->status != aborted)
919 && (old_request->status != completed))
920 dev_err(&old_request->isci_host->pdev->dev,
921 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
922 __func__, old_request->status, tmf, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700923 break;
924
925 case isci_tmf_timed_out:
926
927 /* Set the task's state to "aborting", since the abort task
928 * function thread set it to "aborted" (above) in anticipation
929 * of the task management request working correctly. Since the
930 * timeout has now fired, the TMF request failed. We set the
931 * state such that the request completion will indicate the
932 * device is no longer present.
933 */
934 isci_request_change_state(old_request, aborting);
935 break;
936
937 default:
938 dev_err(&old_request->isci_host->pdev->dev,
939 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
940 __func__, cb_state, tmf, old_request);
941 break;
942 }
943}
944
945/**
946 * isci_task_abort_task() - This function is one of the SAS Domain Template
947 * functions. This function is called by libsas to abort a specified task.
948 * @task: This parameter specifies the SAS task to abort.
949 *
950 * status, zero indicates success.
951 */
952int isci_task_abort_task(struct sas_task *task)
953{
Dan Williams4393aa42011-03-31 13:10:44 -0700954 struct isci_host *isci_host = dev_to_ihost(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700955 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800956 struct isci_request *old_request = NULL;
957 enum isci_request_status old_state;
Dan Williams6f231dd2011-07-02 22:56:22 -0700958 struct isci_remote_device *isci_device = NULL;
Jeff Skirvina5fde222011-03-04 14:06:42 -0800959 struct isci_tmf tmf;
960 int ret = TMF_RESP_FUNC_FAILED;
961 unsigned long flags;
962 bool any_dev_reset = false;
Dan Williams6f231dd2011-07-02 22:56:22 -0700963
964 /* Get the isci_request reference from the task. Note that
965 * this check does not depend on the pending request list
966 * in the device, because tasks driving resets may land here
967 * after completion in the core.
968 */
Dan Williams209fae12011-06-13 17:39:44 -0700969 spin_lock_irqsave(&isci_host->scic_lock, flags);
970 spin_lock(&task->task_state_lock);
971
972 old_request = task->lldd_task;
973
974 /* If task is already done, the request isn't valid */
975 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
976 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
977 old_request)
978 isci_device = isci_lookup_device(task->dev);
979
980 spin_unlock(&task->task_state_lock);
981 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700982
983 dev_dbg(&isci_host->pdev->dev,
984 "%s: task = %p\n", __func__, task);
985
Dan Williams209fae12011-06-13 17:39:44 -0700986 if (!isci_device || !old_request)
987 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -0700988
Dan Williamsd06b4872011-05-02 13:59:25 -0700989 set_bit(IDEV_EH, &isci_device->flags);
990
Dan Williams6f231dd2011-07-02 22:56:22 -0700991 /* This version of the driver will fail abort requests for
992 * SATA/STP. Failing the abort request this way will cause the
993 * SCSI error handler thread to escalate to LUN reset
994 */
Dan Williams209fae12011-06-13 17:39:44 -0700995 if (sas_protocol_ata(task->task_proto)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700996 dev_warn(&isci_host->pdev->dev,
997 " task %p is for a STP/SATA device;"
998 " returning TMF_RESP_FUNC_FAILED\n"
999 " to cause a LUN reset...\n", task);
Dan Williams209fae12011-06-13 17:39:44 -07001000 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001001 }
1002
1003 dev_dbg(&isci_host->pdev->dev,
1004 "%s: old_request == %p\n", __func__, old_request);
1005
Dan Williams209fae12011-06-13 17:39:44 -07001006 any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001007
Dan Williams6f231dd2011-07-02 22:56:22 -07001008 spin_lock_irqsave(&task->task_state_lock, flags);
1009
Dan Williams209fae12011-06-13 17:39:44 -07001010 any_dev_reset = any_dev_reset || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
Dan Williams6f231dd2011-07-02 22:56:22 -07001011
1012 /* If the extraction of the request reference from the task
1013 * failed, then the request has been completed (or if there is a
1014 * pending reset then this abort request function must be failed
1015 * in order to escalate to the target reset).
1016 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001017 if ((old_request == NULL) || any_dev_reset) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001018
1019 /* If the device reset task flag is set, fail the task
1020 * management request. Otherwise, the original request
1021 * has completed.
1022 */
1023 if (any_dev_reset) {
1024
1025 /* Turn off the task's DONE to make sure this
1026 * task is escalated to a target reset.
1027 */
1028 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1029
Jeff Skirvina5fde222011-03-04 14:06:42 -08001030 /* Make the reset happen as soon as possible. */
1031 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1032
1033 spin_unlock_irqrestore(&task->task_state_lock, flags);
1034
Dan Williams6f231dd2011-07-02 22:56:22 -07001035 /* Fail the task management request in order to
1036 * escalate to the target reset.
1037 */
1038 ret = TMF_RESP_FUNC_FAILED;
1039
1040 dev_dbg(&isci_host->pdev->dev,
1041 "%s: Failing task abort in order to "
1042 "escalate to target reset because\n"
1043 "SAS_TASK_NEED_DEV_RESET is set for "
1044 "task %p on dev %p\n",
1045 __func__, task, isci_device);
1046
Jeff Skirvina5fde222011-03-04 14:06:42 -08001047
Dan Williams6f231dd2011-07-02 22:56:22 -07001048 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -07001049 /* The request has already completed and there
1050 * is nothing to do here other than to set the task
1051 * done bit, and indicate that the task abort function
1052 * was sucessful.
1053 */
1054 isci_set_task_doneflags(task);
1055
Jeff Skirvina5fde222011-03-04 14:06:42 -08001056 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001057
Jeff Skirvina5fde222011-03-04 14:06:42 -08001058 ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001059
Jeff Skirvina5fde222011-03-04 14:06:42 -08001060 dev_dbg(&isci_host->pdev->dev,
1061 "%s: abort task not needed for %p\n",
1062 __func__, task);
Dan Williams6f231dd2011-07-02 22:56:22 -07001063 }
Dan Williams209fae12011-06-13 17:39:44 -07001064 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001065 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001066 else
1067 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001068
1069 spin_lock_irqsave(&isci_host->scic_lock, flags);
1070
Jeff Skirvinf219f012011-03-31 13:10:34 -07001071 /* Check the request status and change to "aborted" if currently
Jeff Skirvina5fde222011-03-04 14:06:42 -08001072 * "starting"; if true then set the I/O kernel completion
Dan Williams6f231dd2011-07-02 22:56:22 -07001073 * struct that will be triggered when the request completes.
1074 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001075 old_state = isci_task_validate_request_to_abort(
1076 old_request, isci_host, isci_device,
1077 &aborted_io_completion);
Jeff Skirvinf219f012011-03-31 13:10:34 -07001078 if ((old_state != started) &&
1079 (old_state != completed) &&
1080 (old_state != aborting)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001081
1082 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1083
Jeff Skirvina5fde222011-03-04 14:06:42 -08001084 /* The request was already being handled by someone else (because
1085 * they got to set the state away from started).
1086 */
1087 dev_dbg(&isci_host->pdev->dev,
1088 "%s: device = %p; old_request %p already being aborted\n",
1089 __func__,
1090 isci_device, old_request);
Dan Williams209fae12011-06-13 17:39:44 -07001091 ret = TMF_RESP_FUNC_COMPLETE;
1092 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001093 }
Dan Williams38d88792011-06-23 14:33:48 -07001094 if (task->task_proto == SAS_PROTOCOL_SMP ||
1095 test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001096
1097 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1098
Jeff Skirvina5fde222011-03-04 14:06:42 -08001099 dev_dbg(&isci_host->pdev->dev,
1100 "%s: SMP request (%d)"
Jeff Skirvina5fde222011-03-04 14:06:42 -08001101 " or complete_in_target (%d), thus no TMF\n",
1102 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
Dan Williams38d88792011-06-23 14:33:48 -07001103 test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags));
Jeff Skirvina5fde222011-03-04 14:06:42 -08001104
Dan Williams6f231dd2011-07-02 22:56:22 -07001105 /* Set the state on the task. */
1106 isci_task_all_done(task);
1107
1108 ret = TMF_RESP_FUNC_COMPLETE;
1109
1110 /* Stopping and SMP devices are not sent a TMF, and are not
Jeff Skirvina5fde222011-03-04 14:06:42 -08001111 * reset, but the outstanding I/O request is terminated below.
Dan Williams6f231dd2011-07-02 22:56:22 -07001112 */
Dan Williams6f231dd2011-07-02 22:56:22 -07001113 } else {
1114 /* Fill in the tmf stucture */
Dan Williams209fae12011-06-13 17:39:44 -07001115 isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -08001116 isci_abort_task_process_cb,
1117 old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001118
Dan Williams6f231dd2011-07-02 22:56:22 -07001119 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1120
1121 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
Dan Williams209fae12011-06-13 17:39:44 -07001122 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf,
Dan Williams6f231dd2011-07-02 22:56:22 -07001123 ISCI_ABORT_TASK_TIMEOUT_MS);
1124
Jeff Skirvina5fde222011-03-04 14:06:42 -08001125 if (ret != TMF_RESP_FUNC_COMPLETE)
Dan Williams6f231dd2011-07-02 22:56:22 -07001126 dev_err(&isci_host->pdev->dev,
1127 "%s: isci_task_send_tmf failed\n",
1128 __func__);
1129 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001130 if (ret == TMF_RESP_FUNC_COMPLETE) {
Dan Williams38d88792011-06-23 14:33:48 -07001131 set_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001132
Jeff Skirvin77c852f2011-06-20 14:09:16 -07001133 /* Clean up the request on our side, and wait for the aborted
1134 * I/O to complete.
1135 */
Jeff Skirvincbb65c62011-03-04 14:06:50 -08001136 isci_terminate_request_core(isci_host, isci_device, old_request);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001137 }
1138
1139 /* Make sure we do not leave a reference to aborted_io_completion */
1140 old_request->io_request_completion = NULL;
Dan Williams209fae12011-06-13 17:39:44 -07001141 out:
1142 isci_put_device(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001143 return ret;
1144}
1145
1146/**
1147 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1148 * functions. This is one of the Task Management functoins called by libsas,
1149 * to abort all task for the given lun.
1150 * @d_device: This parameter specifies the domain device associated with this
1151 * request.
1152 * @lun: This parameter specifies the lun associated with this request.
1153 *
1154 * status, zero indicates success.
1155 */
1156int isci_task_abort_task_set(
1157 struct domain_device *d_device,
1158 u8 *lun)
1159{
1160 return TMF_RESP_FUNC_FAILED;
1161}
1162
1163
1164/**
1165 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1166 * functions. This is one of the Task Management functoins called by libsas.
1167 * @d_device: This parameter specifies the domain device associated with this
1168 * request.
1169 * @lun: This parameter specifies the lun associated with this request.
1170 *
1171 * status, zero indicates success.
1172 */
1173int isci_task_clear_aca(
1174 struct domain_device *d_device,
1175 u8 *lun)
1176{
1177 return TMF_RESP_FUNC_FAILED;
1178}
1179
1180
1181
1182/**
1183 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1184 * functions. This is one of the Task Management functoins called by libsas.
1185 * @d_device: This parameter specifies the domain device associated with this
1186 * request.
1187 * @lun: This parameter specifies the lun associated with this request.
1188 *
1189 * status, zero indicates success.
1190 */
1191int isci_task_clear_task_set(
1192 struct domain_device *d_device,
1193 u8 *lun)
1194{
1195 return TMF_RESP_FUNC_FAILED;
1196}
1197
1198
1199/**
1200 * isci_task_query_task() - This function is implemented to cause libsas to
1201 * correctly escalate the failed abort to a LUN or target reset (this is
1202 * because sas_scsi_find_task libsas function does not correctly interpret
1203 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1204 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1205 * returned, libsas will turn this into a target reset
1206 * @task: This parameter specifies the sas task being queried.
1207 * @lun: This parameter specifies the lun associated with this request.
1208 *
1209 * status, zero indicates success.
1210 */
1211int isci_task_query_task(
1212 struct sas_task *task)
1213{
1214 /* See if there is a pending device reset for this device. */
1215 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1216 return TMF_RESP_FUNC_FAILED;
1217 else
1218 return TMF_RESP_FUNC_SUCC;
1219}
1220
Dave Jiangaf5ae892011-05-04 17:53:24 -07001221/*
Dan Williams6f231dd2011-07-02 22:56:22 -07001222 * isci_task_request_complete() - This function is called by the sci core when
1223 * an task request completes.
Dave Jiangaf5ae892011-05-04 17:53:24 -07001224 * @ihost: This parameter specifies the ISCI host object
1225 * @ireq: This parameter is the completed isci_request object.
Dan Williams6f231dd2011-07-02 22:56:22 -07001226 * @completion_status: This parameter specifies the completion status from the
1227 * sci core.
1228 *
1229 * none.
1230 */
Dave Jiangaf5ae892011-05-04 17:53:24 -07001231void
1232isci_task_request_complete(struct isci_host *ihost,
1233 struct isci_request *ireq,
1234 enum sci_task_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001235{
Dave Jiangaf5ae892011-05-04 17:53:24 -07001236 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001237 struct completion *tmf_complete;
1238
Dave Jiangaf5ae892011-05-04 17:53:24 -07001239 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001240 "%s: request = %p, status=%d\n",
Dave Jiangaf5ae892011-05-04 17:53:24 -07001241 __func__, ireq, completion_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001242
Dave Jiang8d2c65c2011-06-01 09:03:08 +00001243 isci_request_change_state(ireq, completed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001244
1245 tmf->status = completion_status;
Dan Williams38d88792011-06-23 14:33:48 -07001246 set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001247
Dave Jiangaf5ae892011-05-04 17:53:24 -07001248 if (tmf->proto == SAS_PROTOCOL_SSP) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001249 memcpy(&tmf->resp.resp_iu,
Dan Williams5076a1a2011-06-27 14:57:03 -07001250 &ireq->ssp.rsp,
Dave Jiangaf5ae892011-05-04 17:53:24 -07001251 SSP_RESP_IU_MAX_SIZE);
1252 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001253 memcpy(&tmf->resp.d2h_fis,
Dan Williams5076a1a2011-06-27 14:57:03 -07001254 &ireq->stp.rsp,
Dave Jiangf2f30082011-05-04 15:02:02 -07001255 sizeof(struct dev_to_host_fis));
Dan Williams6f231dd2011-07-02 22:56:22 -07001256 }
1257
Dan Williams6f231dd2011-07-02 22:56:22 -07001258 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1259 tmf_complete = tmf->complete;
1260
Dan Williams5076a1a2011-06-27 14:57:03 -07001261 scic_controller_complete_io(&ihost->sci, ireq->target_device, ireq);
Dan Williams67ea8382011-05-08 11:47:15 -07001262 /* set the 'terminated' flag handle to make sure it cannot be terminated
Dan Williams6f231dd2011-07-02 22:56:22 -07001263 * or completed again.
1264 */
Dan Williams38d88792011-06-23 14:33:48 -07001265 set_bit(IREQ_TERMINATED, &ireq->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001266
Dave Jiangaf5ae892011-05-04 17:53:24 -07001267 isci_request_change_state(ireq, unallocated);
1268 list_del_init(&ireq->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -07001269
1270 /* The task management part completes last. */
1271 complete(tmf_complete);
1272}
1273
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001274static void isci_smp_task_timedout(unsigned long _task)
1275{
1276 struct sas_task *task = (void *) _task;
1277 unsigned long flags;
1278
1279 spin_lock_irqsave(&task->task_state_lock, flags);
1280 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1281 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1282 spin_unlock_irqrestore(&task->task_state_lock, flags);
1283
1284 complete(&task->completion);
1285}
1286
1287static void isci_smp_task_done(struct sas_task *task)
1288{
1289 if (!del_timer(&task->timer))
1290 return;
1291 complete(&task->completion);
1292}
1293
1294static struct sas_task *isci_alloc_task(void)
1295{
1296 struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL);
1297
1298 if (task) {
1299 INIT_LIST_HEAD(&task->list);
1300 spin_lock_init(&task->task_state_lock);
1301 task->task_state_flags = SAS_TASK_STATE_PENDING;
1302 init_timer(&task->timer);
1303 init_completion(&task->completion);
1304 }
1305
1306 return task;
1307}
1308
1309static void isci_free_task(struct isci_host *ihost, struct sas_task *task)
1310{
1311 if (task) {
1312 BUG_ON(!list_empty(&task->list));
1313 kfree(task);
1314 }
1315}
1316
1317static int isci_smp_execute_task(struct isci_host *ihost,
1318 struct domain_device *dev, void *req,
1319 int req_size, void *resp, int resp_size)
1320{
1321 int res, retry;
1322 struct sas_task *task = NULL;
1323
1324 for (retry = 0; retry < 3; retry++) {
1325 task = isci_alloc_task();
1326 if (!task)
1327 return -ENOMEM;
1328
1329 task->dev = dev;
1330 task->task_proto = dev->tproto;
1331 sg_init_one(&task->smp_task.smp_req, req, req_size);
1332 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
1333
1334 task->task_done = isci_smp_task_done;
1335
1336 task->timer.data = (unsigned long) task;
1337 task->timer.function = isci_smp_task_timedout;
1338 task->timer.expires = jiffies + 10*HZ;
1339 add_timer(&task->timer);
1340
1341 res = isci_task_execute_task(task, 1, GFP_KERNEL);
1342
1343 if (res) {
1344 del_timer(&task->timer);
1345 dev_err(&ihost->pdev->dev,
1346 "%s: executing SMP task failed:%d\n",
1347 __func__, res);
1348 goto ex_err;
1349 }
1350
1351 wait_for_completion(&task->completion);
1352 res = -ECOMM;
1353 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1354 dev_err(&ihost->pdev->dev,
1355 "%s: smp task timed out or aborted\n",
1356 __func__);
1357 isci_task_abort_task(task);
1358 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1359 dev_err(&ihost->pdev->dev,
1360 "%s: SMP task aborted and not done\n",
1361 __func__);
1362 goto ex_err;
1363 }
1364 }
1365 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1366 task->task_status.stat == SAM_STAT_GOOD) {
1367 res = 0;
1368 break;
1369 }
1370 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1371 task->task_status.stat == SAS_DATA_UNDERRUN) {
1372 /* no error, but return the number of bytes of
1373 * underrun */
1374 res = task->task_status.residual;
1375 break;
1376 }
1377 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1378 task->task_status.stat == SAS_DATA_OVERRUN) {
1379 res = -EMSGSIZE;
1380 break;
1381 } else {
1382 dev_err(&ihost->pdev->dev,
1383 "%s: task to dev %016llx response: 0x%x "
1384 "status 0x%x\n", __func__,
1385 SAS_ADDR(dev->sas_addr),
1386 task->task_status.resp,
1387 task->task_status.stat);
1388 isci_free_task(ihost, task);
1389 task = NULL;
1390 }
1391 }
1392ex_err:
1393 BUG_ON(retry == 3 && task != NULL);
1394 isci_free_task(ihost, task);
1395 return res;
1396}
1397
1398#define DISCOVER_REQ_SIZE 16
1399#define DISCOVER_RESP_SIZE 56
1400
1401int isci_smp_get_phy_attached_dev_type(struct isci_host *ihost,
1402 struct domain_device *dev,
1403 int phy_id, int *adt)
1404{
1405 struct smp_resp *disc_resp;
1406 u8 *disc_req;
1407 int res;
1408
1409 disc_resp = kzalloc(DISCOVER_RESP_SIZE, GFP_KERNEL);
1410 if (!disc_resp)
1411 return -ENOMEM;
1412
1413 disc_req = kzalloc(DISCOVER_REQ_SIZE, GFP_KERNEL);
1414 if (disc_req) {
1415 disc_req[0] = SMP_REQUEST;
1416 disc_req[1] = SMP_DISCOVER;
1417 disc_req[9] = phy_id;
1418 } else {
1419 kfree(disc_resp);
1420 return -ENOMEM;
1421 }
1422 res = isci_smp_execute_task(ihost, dev, disc_req, DISCOVER_REQ_SIZE,
1423 disc_resp, DISCOVER_RESP_SIZE);
1424 if (!res) {
1425 if (disc_resp->result != SMP_RESP_FUNC_ACC)
1426 res = disc_resp->result;
1427 else
1428 *adt = disc_resp->disc.attached_dev_type;
1429 }
1430 kfree(disc_req);
1431 kfree(disc_resp);
1432
1433 return res;
1434}
1435
1436static void isci_wait_for_smp_phy_reset(struct isci_remote_device *idev, int phy_num)
1437{
1438 struct domain_device *dev = idev->domain_dev;
1439 struct isci_port *iport = idev->isci_port;
1440 struct isci_host *ihost = iport->isci_host;
1441 int res, iteration = 0, attached_device_type;
1442 #define STP_WAIT_MSECS 25000
1443 unsigned long tmo = msecs_to_jiffies(STP_WAIT_MSECS);
1444 unsigned long deadline = jiffies + tmo;
1445 enum {
1446 SMP_PHYWAIT_PHYDOWN,
1447 SMP_PHYWAIT_PHYUP,
1448 SMP_PHYWAIT_DONE
1449 } phy_state = SMP_PHYWAIT_PHYDOWN;
1450
1451 /* While there is time, wait for the phy to go away and come back */
1452 while (time_is_after_jiffies(deadline) && phy_state != SMP_PHYWAIT_DONE) {
1453 int event = atomic_read(&iport->event);
1454
1455 ++iteration;
1456
1457 tmo = wait_event_timeout(ihost->eventq,
1458 event != atomic_read(&iport->event) ||
1459 !test_bit(IPORT_BCN_BLOCKED, &iport->flags),
1460 tmo);
1461 /* link down, stop polling */
1462 if (!test_bit(IPORT_BCN_BLOCKED, &iport->flags))
1463 break;
1464
1465 dev_dbg(&ihost->pdev->dev,
1466 "%s: iport %p, iteration %d,"
1467 " phase %d: time_remaining %lu, bcns = %d\n",
1468 __func__, iport, iteration, phy_state,
1469 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags));
1470
1471 res = isci_smp_get_phy_attached_dev_type(ihost, dev, phy_num,
1472 &attached_device_type);
1473 tmo = deadline - jiffies;
1474
1475 if (res) {
1476 dev_warn(&ihost->pdev->dev,
1477 "%s: iteration %d, phase %d:"
1478 " SMP error=%d, time_remaining=%lu\n",
1479 __func__, iteration, phy_state, res, tmo);
1480 break;
1481 }
1482 dev_dbg(&ihost->pdev->dev,
1483 "%s: iport %p, iteration %d,"
1484 " phase %d: time_remaining %lu, bcns = %d, "
1485 "attdevtype = %x\n",
1486 __func__, iport, iteration, phy_state,
1487 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags),
1488 attached_device_type);
1489
1490 switch (phy_state) {
1491 case SMP_PHYWAIT_PHYDOWN:
1492 /* Has the device gone away? */
1493 if (!attached_device_type)
1494 phy_state = SMP_PHYWAIT_PHYUP;
1495
1496 break;
1497
1498 case SMP_PHYWAIT_PHYUP:
1499 /* Has the device come back? */
1500 if (attached_device_type)
1501 phy_state = SMP_PHYWAIT_DONE;
1502 break;
1503
1504 case SMP_PHYWAIT_DONE:
1505 break;
1506 }
1507
1508 }
1509 dev_dbg(&ihost->pdev->dev, "%s: done\n", __func__);
1510}
1511
Dan Williams209fae12011-06-13 17:39:44 -07001512static int isci_reset_device(struct isci_host *ihost,
1513 struct isci_remote_device *idev, int hard_reset)
Dan Williams6f231dd2011-07-02 22:56:22 -07001514{
Dan Williams209fae12011-06-13 17:39:44 -07001515 struct sas_phy *phy = sas_find_local_phy(idev->domain_dev);
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001516 struct isci_port *iport = idev->isci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001517 enum sci_status status;
Dan Williamsd06b4872011-05-02 13:59:25 -07001518 unsigned long flags;
1519 int rc;
Dan Williams6f231dd2011-07-02 22:56:22 -07001520
Dan Williamsd06b4872011-05-02 13:59:25 -07001521 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001522
Dan Williamsd06b4872011-05-02 13:59:25 -07001523 spin_lock_irqsave(&ihost->scic_lock, flags);
1524 status = scic_remote_device_reset(&idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001525 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001526 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001527
Dan Williamsd06b4872011-05-02 13:59:25 -07001528 dev_warn(&ihost->pdev->dev,
1529 "%s: scic_remote_device_reset(%p) returned %d!\n",
1530 __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001531
1532 return TMF_RESP_FUNC_FAILED;
1533 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001534 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001535
Dan Williams6f231dd2011-07-02 22:56:22 -07001536 /* Make sure all pending requests are able to be fully terminated. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001537 isci_device_clear_reset_pending(ihost, idev);
1538
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001539 /* If this is a device on an expander, disable BCN processing. */
1540 if (!scsi_is_sas_phy_local(phy))
1541 set_bit(IPORT_BCN_BLOCKED, &iport->flags);
1542
Dan Williamsd06b4872011-05-02 13:59:25 -07001543 rc = sas_phy_reset(phy, hard_reset);
Dan Williams6f231dd2011-07-02 22:56:22 -07001544
1545 /* Terminate in-progress I/O now. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001546 isci_remote_device_nuke_requests(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001547
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001548 /* Since all pending TCs have been cleaned, resume the RNC. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001549 spin_lock_irqsave(&ihost->scic_lock, flags);
1550 status = scic_remote_device_reset_complete(&idev->sci);
1551 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001552
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001553 /* If this is a device on an expander, bring the phy back up. */
1554 if (!scsi_is_sas_phy_local(phy)) {
1555 /* A phy reset will cause the device to go away then reappear.
1556 * Since libsas will take action on incoming BCNs (eg. remove
1557 * a device going through an SMP phy-control driven reset),
1558 * we need to wait until the phy comes back up before letting
1559 * discovery proceed in libsas.
1560 */
1561 isci_wait_for_smp_phy_reset(idev, phy->number);
1562
1563 spin_lock_irqsave(&ihost->scic_lock, flags);
1564 isci_port_bcn_enable(ihost, idev->isci_port);
1565 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1566 }
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001567
Dan Williams6f231dd2011-07-02 22:56:22 -07001568 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001569 dev_warn(&ihost->pdev->dev,
1570 "%s: scic_remote_device_reset_complete(%p) "
1571 "returned %d!\n", __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001572 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001573
Dan Williamsd06b4872011-05-02 13:59:25 -07001574 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001575
Dan Williamsd06b4872011-05-02 13:59:25 -07001576 return rc;
1577}
1578
1579int isci_task_I_T_nexus_reset(struct domain_device *dev)
1580{
1581 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001582 struct isci_remote_device *idev;
Dan Williams209fae12011-06-13 17:39:44 -07001583 int ret, hard_reset = 1;
Dan Williamsd06b4872011-05-02 13:59:25 -07001584 unsigned long flags;
1585
Dan Williamsd06b4872011-05-02 13:59:25 -07001586 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams209fae12011-06-13 17:39:44 -07001587 idev = isci_lookup_device(dev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001588 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1589
Dan Williams209fae12011-06-13 17:39:44 -07001590 if (!idev || !test_bit(IDEV_EH, &idev->flags)) {
1591 ret = TMF_RESP_FUNC_COMPLETE;
1592 goto out;
1593 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001594
1595 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1596 hard_reset = 0;
1597
Dan Williams209fae12011-06-13 17:39:44 -07001598 ret = isci_reset_device(ihost, idev, hard_reset);
1599 out:
1600 isci_put_device(idev);
1601 return ret;
Dan Williamsd06b4872011-05-02 13:59:25 -07001602}
1603
1604int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1605{
1606 struct domain_device *dev = sdev_to_domain_dev(cmd->device);
Dan Williams209fae12011-06-13 17:39:44 -07001607 struct isci_host *ihost = dev_to_ihost(dev);
1608 struct isci_remote_device *idev;
1609 int ret, hard_reset = 1;
1610 unsigned long flags;
Dan Williamsd06b4872011-05-02 13:59:25 -07001611
1612 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1613 hard_reset = 0;
1614
Dan Williams209fae12011-06-13 17:39:44 -07001615 spin_lock_irqsave(&ihost->scic_lock, flags);
1616 idev = isci_lookup_device(dev);
1617 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1618
1619 if (!idev) {
1620 ret = TMF_RESP_FUNC_COMPLETE;
1621 goto out;
1622 }
1623
1624 ret = isci_reset_device(ihost, idev, hard_reset);
1625 out:
1626 isci_put_device(idev);
1627 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -07001628}