blob: e8cf17b024a4f890c8eb9d75b96c8fb6fd99de7f [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"
Dan Williams6f231dd2011-07-02 22:56:22 -070064#include "task.h"
Dan Williams312e0c22011-06-28 13:47:09 -070065#include "host.h"
Dave Jiangaf5ae892011-05-04 17:53:24 -070066
Dan Williams50e7f9b2011-03-09 21:27:46 -080067/**
Dan Williams1077a572011-03-11 10:13:51 -080068* isci_task_refuse() - complete the request to the upper layer driver in
69* the case where an I/O needs to be completed back in the submit path.
70* @ihost: host on which the the request was queued
71* @task: request to complete
72* @response: response code for the completed task.
73* @status: status code for the completed task.
Dan Williams50e7f9b2011-03-09 21:27:46 -080074*
Dan Williams50e7f9b2011-03-09 21:27:46 -080075*/
Dan Williams1077a572011-03-11 10:13:51 -080076static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
77 enum service_response response,
78 enum exec_status status)
Dan Williams50e7f9b2011-03-09 21:27:46 -080079
Dan Williams1077a572011-03-11 10:13:51 -080080{
81 enum isci_completion_selection disposition;
82
83 disposition = isci_perform_normal_io_completion;
84 disposition = isci_task_set_completion_status(task, response, status,
85 disposition);
Dan Williams50e7f9b2011-03-09 21:27:46 -080086
87 /* Tasks aborted specifically by a call to the lldd_abort_task
Dan Williams1077a572011-03-11 10:13:51 -080088 * function should not be completed to the host in the regular path.
89 */
90 switch (disposition) {
James Bottomleya5ec7f862011-07-03 14:14:45 -050091 case isci_perform_normal_io_completion:
92 /* Normal notification (task_done) */
93 dev_dbg(&ihost->pdev->dev,
94 "%s: Normal - task = %p, response=%d, "
95 "status=%d\n",
96 __func__, task, response, status);
Dan Williams50e7f9b2011-03-09 21:27:46 -080097
James Bottomleya5ec7f862011-07-03 14:14:45 -050098 task->lldd_task = NULL;
Dan Williams50e7f9b2011-03-09 21:27:46 -080099
James Bottomleya5ec7f862011-07-03 14:14:45 -0500100 isci_execpath_callback(ihost, task, task->task_done);
101 break;
Dan Williams50e7f9b2011-03-09 21:27:46 -0800102
James Bottomleya5ec7f862011-07-03 14:14:45 -0500103 case isci_perform_aborted_io_completion:
104 /*
105 * No notification because this request is already in the
106 * abort path.
107 */
108 dev_dbg(&ihost->pdev->dev,
109 "%s: Aborted - task = %p, response=%d, "
110 "status=%d\n",
111 __func__, task, response, status);
112 break;
Dan Williams50e7f9b2011-03-09 21:27:46 -0800113
James Bottomleya5ec7f862011-07-03 14:14:45 -0500114 case isci_perform_error_io_completion:
115 /* Use sas_task_abort */
116 dev_dbg(&ihost->pdev->dev,
117 "%s: Error - task = %p, response=%d, "
118 "status=%d\n",
119 __func__, task, response, status);
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700120
James Bottomleya5ec7f862011-07-03 14:14:45 -0500121 isci_execpath_callback(ihost, task, sas_task_abort);
122 break;
Dan Williams50e7f9b2011-03-09 21:27:46 -0800123
James Bottomleya5ec7f862011-07-03 14:14:45 -0500124 default:
125 dev_dbg(&ihost->pdev->dev,
126 "%s: isci task notification default case!",
127 __func__);
128 sas_task_abort(task);
129 break;
Dan Williams50e7f9b2011-03-09 21:27:46 -0800130 }
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
Jeff Skirvinc2cb8a52011-10-27 15:04:56 -0700215 if (test_bit(IDEV_GONE, &idev->flags)) {
216
217 /* Indicate that the device
218 * is gone.
219 */
220 isci_task_refuse(ihost, task,
221 SAS_TASK_UNDELIVERED,
222 SAS_DEVICE_UNKNOWN);
223 } else {
224 /* Indicate QUEUE_FULL so that
225 * the scsi midlayer retries.
226 * If the request failed for
227 * remote device reasons, it
228 * gets returned as
229 * SAS_TASK_UNDELIVERED next
230 * time through.
231 */
232 isci_task_refuse(ihost, task,
233 SAS_TASK_COMPLETE,
234 SAS_QUEUE_FULL);
235 }
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700236 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700237 }
238 }
Dan Williams312e0c22011-06-28 13:47:09 -0700239 if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
240 spin_lock_irqsave(&ihost->scic_lock, flags);
241 /* command never hit the device, so just free
242 * the tci and skip the sequence increment
243 */
244 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
245 spin_unlock_irqrestore(&ihost->scic_lock, flags);
246 }
Dan Williams209fae12011-06-13 17:39:44 -0700247 isci_put_device(idev);
Dan Williams1077a572011-03-11 10:13:51 -0800248 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700249 return 0;
250}
251
Dan Williams16ba7702011-07-01 10:52:55 -0700252static enum sci_status isci_sata_management_task_request_build(struct isci_request *ireq)
253{
254 struct isci_tmf *isci_tmf;
255 enum sci_status status;
256
257 if (tmf_task != ireq->ttype)
258 return SCI_FAILURE;
259
260 isci_tmf = isci_request_access_tmf(ireq);
261
262 switch (isci_tmf->tmf_code) {
263
264 case isci_tmf_sata_srst_high:
265 case isci_tmf_sata_srst_low: {
266 struct host_to_dev_fis *fis = &ireq->stp.cmd;
267
268 memset(fis, 0, sizeof(*fis));
269
270 fis->fis_type = 0x27;
271 fis->flags &= ~0x80;
272 fis->flags &= 0xF0;
273 if (isci_tmf->tmf_code == isci_tmf_sata_srst_high)
274 fis->control |= ATA_SRST;
275 else
276 fis->control &= ~ATA_SRST;
277 break;
278 }
279 /* other management commnd go here... */
280 default:
281 return SCI_FAILURE;
282 }
283
284 /* core builds the protocol specific request
285 * based on the h2d fis.
286 */
287 status = sci_task_request_construct_sata(ireq);
288
289 return status;
290}
291
Dan Williams0d0cf142011-06-13 00:51:30 -0700292static struct isci_request *isci_task_request_build(struct isci_host *ihost,
Dan Williams209fae12011-06-13 17:39:44 -0700293 struct isci_remote_device *idev,
Dan Williams312e0c22011-06-28 13:47:09 -0700294 u16 tag, struct isci_tmf *isci_tmf)
Dan Williams6f231dd2011-07-02 22:56:22 -0700295{
Dan Williams6f231dd2011-07-02 22:56:22 -0700296 enum sci_status status = SCI_FAILURE;
Dan Williams0d0cf142011-06-13 00:51:30 -0700297 struct isci_request *ireq = NULL;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700298 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700299
Dan Williams0d0cf142011-06-13 00:51:30 -0700300 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700301 "%s: isci_tmf = %p\n", __func__, isci_tmf);
302
Dan Williams0d0cf142011-06-13 00:51:30 -0700303 dev = idev->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700304
305 /* do common allocation and init of request object. */
Dan Williamsdb056252011-06-17 14:18:39 -0700306 ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
Dan Williams0d0cf142011-06-13 00:51:30 -0700307 if (!ireq)
308 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700309
310 /* let the core do it's construct. */
Dan Williams89a73012011-06-30 19:14:33 -0700311 status = sci_task_request_construct(ihost, idev, tag,
Dan Williams5076a1a2011-06-27 14:57:03 -0700312 ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700313
314 if (status != SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700315 dev_warn(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -0700316 "%s: sci_task_request_construct failed - "
Dan Williams6f231dd2011-07-02 22:56:22 -0700317 "status = 0x%x\n",
318 __func__,
319 status);
Dan Williamsdb056252011-06-17 14:18:39 -0700320 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700321 }
322
Dan Williamsa1a113b2011-04-21 18:44:45 -0700323 /* XXX convert to get this from task->tproto like other drivers */
324 if (dev->dev_type == SAS_END_DEV) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700325 isci_tmf->proto = SAS_PROTOCOL_SSP;
Dan Williams89a73012011-06-30 19:14:33 -0700326 status = sci_task_request_construct_ssp(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700327 if (status != SCI_SUCCESS)
Dan Williamsdb056252011-06-17 14:18:39 -0700328 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700329 }
330
Dan Williamsa1a113b2011-04-21 18:44:45 -0700331 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700332 isci_tmf->proto = SAS_PROTOCOL_SATA;
Dan Williams0d0cf142011-06-13 00:51:30 -0700333 status = isci_sata_management_task_request_build(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700334
335 if (status != SCI_SUCCESS)
Dan Williamsdb056252011-06-17 14:18:39 -0700336 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700337 }
Dan Williams0d0cf142011-06-13 00:51:30 -0700338 return ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700339}
340
Dan Williams16ba7702011-07-01 10:52:55 -0700341static int isci_task_execute_tmf(struct isci_host *ihost,
342 struct isci_remote_device *idev,
343 struct isci_tmf *tmf, unsigned long timeout_ms)
Dan Williams6f231dd2011-07-02 22:56:22 -0700344{
345 DECLARE_COMPLETION_ONSTACK(completion);
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700346 enum sci_task_status status = SCI_TASK_FAILURE;
Dan Williams0d0cf142011-06-13 00:51:30 -0700347 struct isci_request *ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700348 int ret = TMF_RESP_FUNC_FAILED;
349 unsigned long flags;
Edmund Nadolskifd183882011-05-19 12:00:15 +0000350 unsigned long timeleft;
Dan Williams312e0c22011-06-28 13:47:09 -0700351 u16 tag;
352
353 spin_lock_irqsave(&ihost->scic_lock, flags);
354 tag = isci_alloc_tag(ihost);
355 spin_unlock_irqrestore(&ihost->scic_lock, flags);
356
357 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
358 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700359
360 /* sanity check, return TMF_RESP_FUNC_FAILED
361 * if the device is not there and ready.
362 */
Dan Williams78a6f062011-06-30 16:31:37 -0700363 if (!idev ||
364 (!test_bit(IDEV_IO_READY, &idev->flags) &&
365 !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700366 dev_dbg(&ihost->pdev->dev,
Dan Williams78a6f062011-06-30 16:31:37 -0700367 "%s: idev = %p not ready (%#lx)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700368 __func__,
Dan Williams78a6f062011-06-30 16:31:37 -0700369 idev, idev ? idev->flags : 0);
Dan Williams312e0c22011-06-28 13:47:09 -0700370 goto err_tci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700371 } else
Dan Williams0d0cf142011-06-13 00:51:30 -0700372 dev_dbg(&ihost->pdev->dev,
Dan Williams78a6f062011-06-30 16:31:37 -0700373 "%s: idev = %p\n",
374 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700375
376 /* Assign the pointer to the TMF's completion kernel wait structure. */
377 tmf->complete = &completion;
378
Dan Williams78a6f062011-06-30 16:31:37 -0700379 ireq = isci_task_request_build(ihost, idev, tag, tmf);
Dan Williams312e0c22011-06-28 13:47:09 -0700380 if (!ireq)
381 goto err_tci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700382
Dan Williams0d0cf142011-06-13 00:51:30 -0700383 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700384
385 /* start the TMF io. */
Dan Williams89a73012011-06-30 19:14:33 -0700386 status = sci_controller_start_task(ihost, idev, ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700387
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700388 if (status != SCI_TASK_SUCCESS) {
Dan Williamsa8a0a132011-07-01 12:07:25 -0700389 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700390 "%s: start_io failed - status = 0x%x, request = %p\n",
391 __func__,
392 status,
Dan Williams0d0cf142011-06-13 00:51:30 -0700393 ireq);
394 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsdb056252011-06-17 14:18:39 -0700395 goto err_tci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700396 }
397
Dan Williams6f231dd2011-07-02 22:56:22 -0700398 if (tmf->cb_state_func != NULL)
399 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
400
Dan Williams0d0cf142011-06-13 00:51:30 -0700401 isci_request_change_state(ireq, started);
Dan Williams6f231dd2011-07-02 22:56:22 -0700402
403 /* add the request to the remote device request list. */
Dan Williams78a6f062011-06-30 16:31:37 -0700404 list_add(&ireq->dev_node, &idev->reqs_in_process);
Dan Williams6f231dd2011-07-02 22:56:22 -0700405
Dan Williams0d0cf142011-06-13 00:51:30 -0700406 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700407
408 /* Wait for the TMF to complete, or a timeout. */
Edmund Nadolskifd183882011-05-19 12:00:15 +0000409 timeleft = wait_for_completion_timeout(&completion,
Dan Williams086a0da2011-06-21 16:23:03 -0700410 msecs_to_jiffies(timeout_ms));
Edmund Nadolskifd183882011-05-19 12:00:15 +0000411
412 if (timeleft == 0) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700413 spin_lock_irqsave(&ihost->scic_lock, flags);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000414
415 if (tmf->cb_state_func != NULL)
416 tmf->cb_state_func(isci_tmf_timed_out, tmf, tmf->cb_data);
417
Dan Williams89a73012011-06-30 19:14:33 -0700418 sci_controller_terminate_request(ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700419 idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700420 ireq);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000421
Dan Williams0d0cf142011-06-13 00:51:30 -0700422 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams086a0da2011-06-21 16:23:03 -0700423
424 wait_for_completion(tmf->complete);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000425 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700426
427 isci_print_tmf(tmf);
428
429 if (tmf->status == SCI_SUCCESS)
430 ret = TMF_RESP_FUNC_COMPLETE;
431 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700432 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700433 "%s: tmf.status == "
434 "SCI_FAILURE_IO_RESPONSE_VALID\n",
435 __func__);
436 ret = TMF_RESP_FUNC_COMPLETE;
437 }
438 /* Else - leave the default "failed" status alone. */
439
Dan Williams0d0cf142011-06-13 00:51:30 -0700440 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700441 "%s: completed request = %p\n",
442 __func__,
Dan Williams0d0cf142011-06-13 00:51:30 -0700443 ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700444
Dan Williams6f231dd2011-07-02 22:56:22 -0700445 return ret;
Dan Williams312e0c22011-06-28 13:47:09 -0700446
Dan Williams312e0c22011-06-28 13:47:09 -0700447 err_tci:
448 spin_lock_irqsave(&ihost->scic_lock, flags);
449 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
450 spin_unlock_irqrestore(&ihost->scic_lock, flags);
451
452 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700453}
454
Dan Williams16ba7702011-07-01 10:52:55 -0700455static void isci_task_build_tmf(struct isci_tmf *tmf,
456 enum isci_tmf_function_codes code,
457 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
458 struct isci_tmf *,
459 void *),
460 void *cb_data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700461{
Dan Williams6f231dd2011-07-02 22:56:22 -0700462 memset(tmf, 0, sizeof(*tmf));
463
Dan Williams6f231dd2011-07-02 22:56:22 -0700464 tmf->tmf_code = code;
Dan Williams6f231dd2011-07-02 22:56:22 -0700465 tmf->cb_state_func = tmf_sent_cb;
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800466 tmf->cb_data = cb_data;
467}
Jeff Skirvin1fad9e92011-03-04 14:06:46 -0800468
Dan Williams16ba7702011-07-01 10:52:55 -0700469static void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
470 enum isci_tmf_function_codes code,
471 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
472 struct isci_tmf *,
473 void *),
474 struct isci_request *old_request)
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800475{
Dan Williams16ba7702011-07-01 10:52:55 -0700476 isci_task_build_tmf(tmf, code, tmf_sent_cb, old_request);
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800477 tmf->io_tag = old_request->io_tag;
Dan Williams6f231dd2011-07-02 22:56:22 -0700478}
479
Dan Williams6f231dd2011-07-02 22:56:22 -0700480/**
481 * isci_task_validate_request_to_abort() - This function checks the given I/O
482 * against the "started" state. If the request is still "started", it's
483 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
484 * BEFORE CALLING THIS FUNCTION.
485 * @isci_request: This parameter specifies the request object to control.
486 * @isci_host: This parameter specifies the ISCI host object
487 * @isci_device: This is the device to which the request is pending.
488 * @aborted_io_completion: This is a completion structure that will be added to
489 * the request in case it is changed to aborting; this completion is
490 * triggered when the request is fully completed.
491 *
492 * Either "started" on successful change of the task status to "aborted", or
493 * "unallocated" if the task cannot be controlled.
494 */
495static enum isci_request_status isci_task_validate_request_to_abort(
496 struct isci_request *isci_request,
497 struct isci_host *isci_host,
498 struct isci_remote_device *isci_device,
499 struct completion *aborted_io_completion)
500{
501 enum isci_request_status old_state = unallocated;
502
503 /* Only abort the task if it's in the
504 * device's request_in_process list
505 */
506 if (isci_request && !list_empty(&isci_request->dev_node)) {
507 old_state = isci_request_change_started_to_aborted(
508 isci_request, aborted_io_completion);
509
Dan Williams6f231dd2011-07-02 22:56:22 -0700510 }
511
512 return old_state;
513}
514
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700515/**
516* isci_request_cleanup_completed_loiterer() - This function will take care of
517* the final cleanup on any request which has been explicitly terminated.
518* @isci_host: This parameter specifies the ISCI host object
519* @isci_device: This is the device to which the request is pending.
520* @isci_request: This parameter specifies the terminated request object.
521* @task: This parameter is the libsas I/O request.
522*/
Dan Williams6f231dd2011-07-02 22:56:22 -0700523static void isci_request_cleanup_completed_loiterer(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700524 struct isci_host *isci_host,
Dan Williams6f231dd2011-07-02 22:56:22 -0700525 struct isci_remote_device *isci_device,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700526 struct isci_request *isci_request,
527 struct sas_task *task)
Dan Williams6f231dd2011-07-02 22:56:22 -0700528{
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700529 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700530
531 dev_dbg(&isci_host->pdev->dev,
532 "%s: isci_device=%p, request=%p, task=%p\n",
Jeff Skirvin18d3d722011-03-04 14:06:38 -0800533 __func__, isci_device, isci_request, task);
Dan Williams6f231dd2011-07-02 22:56:22 -0700534
Jeff Skirvina5fde222011-03-04 14:06:42 -0800535 if (task != NULL) {
536
537 spin_lock_irqsave(&task->task_state_lock, flags);
538 task->lldd_task = NULL;
539
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700540 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
541
Jeff Skirvina5fde222011-03-04 14:06:42 -0800542 isci_set_task_doneflags(task);
543
544 /* If this task is not in the abort path, call task_done. */
545 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
546
547 spin_unlock_irqrestore(&task->task_state_lock, flags);
548 task->task_done(task);
549 } else
550 spin_unlock_irqrestore(&task->task_state_lock, flags);
551 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800552
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700553 if (isci_request != NULL) {
554 spin_lock_irqsave(&isci_host->scic_lock, flags);
555 list_del_init(&isci_request->dev_node);
556 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800557 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800558}
559
Dan Williams6f231dd2011-07-02 22:56:22 -0700560/**
561 * isci_terminate_request_core() - This function will terminate the given
562 * request, and wait for it to complete. This function must only be called
563 * from a thread that can wait. Note that the request is terminated and
564 * completed (back to the host, if started there).
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700565 * @ihost: This SCU.
Dan Williams78a6f062011-06-30 16:31:37 -0700566 * @idev: The target.
Dan Williams6f231dd2011-07-02 22:56:22 -0700567 * @isci_request: The I/O request to be terminated.
568 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700569 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700570static void isci_terminate_request_core(struct isci_host *ihost,
571 struct isci_remote_device *idev,
572 struct isci_request *isci_request)
Dan Williams6f231dd2011-07-02 22:56:22 -0700573{
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800574 enum sci_status status = SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700575 bool was_terminated = false;
576 bool needs_cleanup_handling = false;
577 enum isci_request_status request_status;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700578 unsigned long flags;
579 unsigned long termination_completed = 1;
580 struct completion *io_request_completion;
581 struct sas_task *task;
Dan Williams6f231dd2011-07-02 22:56:22 -0700582
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700583 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700584 "%s: device = %p; request = %p\n",
Dan Williams78a6f062011-06-30 16:31:37 -0700585 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700586
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700587 spin_lock_irqsave(&ihost->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800588
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700589 io_request_completion = isci_request->io_request_completion;
590
591 task = (isci_request->ttype == io_task)
592 ? isci_request_access_task(isci_request)
593 : NULL;
594
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800595 /* Note that we are not going to control
Dan Williams38d88792011-06-23 14:33:48 -0700596 * the target to abort the request.
597 */
598 set_bit(IREQ_COMPLETE_IN_TARGET, &isci_request->flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800599
Dan Williams6f231dd2011-07-02 22:56:22 -0700600 /* Make sure the request wasn't just sitting around signalling
601 * device condition (if the request handle is NULL, then the
602 * request completed but needed additional handling here).
603 */
Dan Williams38d88792011-06-23 14:33:48 -0700604 if (!test_bit(IREQ_TERMINATED, &isci_request->flags)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700605 was_terminated = true;
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800606 needs_cleanup_handling = true;
Dan Williams89a73012011-06-30 19:14:33 -0700607 status = sci_controller_terminate_request(ihost,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700608 idev,
609 isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700610 }
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700611 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700612
613 /*
614 * The only time the request to terminate will
615 * fail is when the io request is completed and
616 * being aborted.
617 */
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800618 if (status != SCI_SUCCESS) {
Dan Williamsa8a0a132011-07-01 12:07:25 -0700619 dev_dbg(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -0700620 "%s: sci_controller_terminate_request"
Dan Williams6f231dd2011-07-02 22:56:22 -0700621 " returned = 0x%x\n",
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700622 __func__, status);
623
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800624 isci_request->io_request_completion = NULL;
625
626 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700627 if (was_terminated) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700628 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700629 "%s: before completion wait (%p/%p)\n",
630 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700631
632 /* Wait here for the request to complete. */
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700633 #define TERMINATION_TIMEOUT_MSEC 500
634 termination_completed
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800635 = wait_for_completion_timeout(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700636 io_request_completion,
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800637 msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
Dan Williams6f231dd2011-07-02 22:56:22 -0700638
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700639 if (!termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800640
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700641 /* The request to terminate has timed out. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700642 spin_lock_irqsave(&ihost->scic_lock,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700643 flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800644
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700645 /* Check for state changes. */
Dan Williams38d88792011-06-23 14:33:48 -0700646 if (!test_bit(IREQ_TERMINATED, &isci_request->flags)) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800647
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700648 /* The best we can do is to have the
649 * request die a silent death if it
650 * ever really completes.
651 *
652 * Set the request state to "dead",
653 * and clear the task pointer so that
654 * an actual completion event callback
655 * doesn't do anything.
656 */
657 isci_request->status = dead;
658 isci_request->io_request_completion
659 = NULL;
660
661 if (isci_request->ttype == io_task) {
662
663 /* Break links with the
664 * sas_task.
665 */
666 isci_request->ttype_ptr.io_task_ptr
667 = NULL;
668 }
669 } else
670 termination_completed = 1;
671
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700672 spin_unlock_irqrestore(&ihost->scic_lock,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700673 flags);
674
675 if (!termination_completed) {
676
Dan Williamsa8a0a132011-07-01 12:07:25 -0700677 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700678 "%s: *** Timeout waiting for "
679 "termination(%p/%p)\n",
680 __func__, io_request_completion,
681 isci_request);
682
683 /* The request can no longer be referenced
684 * safely since it may go away if the
685 * termination every really does complete.
686 */
687 isci_request = NULL;
688 }
689 }
690 if (termination_completed)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700691 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700692 "%s: after completion wait (%p/%p)\n",
693 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700694 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800695
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700696 if (termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800697
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700698 isci_request->io_request_completion = NULL;
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800699
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700700 /* Peek at the status of the request. This will tell
701 * us if there was special handling on the request such that it
702 * needs to be detached and freed here.
703 */
704 spin_lock_irqsave(&isci_request->state_lock, flags);
Dan Williams34a99152011-07-01 02:25:15 -0700705 request_status = isci_request->status;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700706
707 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
708 && ((request_status == aborted)
709 || (request_status == aborting)
710 || (request_status == terminating)
711 || (request_status == completed)
712 || (request_status == dead)
713 )
714 ) {
715
716 /* The completion routine won't free a request in
717 * the aborted/aborting/etc. states, so we do
718 * it here.
719 */
720 needs_cleanup_handling = true;
721 }
722 spin_unlock_irqrestore(&isci_request->state_lock, flags);
723
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800724 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700725 if (needs_cleanup_handling)
726 isci_request_cleanup_completed_loiterer(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700727 ihost, idev, isci_request, task);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800728 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700729}
730
731/**
732 * isci_terminate_pending_requests() - This function will change the all of the
733 * requests on the given device's state to "aborting", will terminate the
734 * requests, and wait for them to complete. This function must only be
735 * called from a thread that can wait. Note that the requests are all
736 * terminated and completed (back to the host, if started there).
737 * @isci_host: This parameter specifies SCU.
Dan Williams78a6f062011-06-30 16:31:37 -0700738 * @idev: This parameter specifies the target.
Dan Williams6f231dd2011-07-02 22:56:22 -0700739 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700740 */
Dan Williams980d3ae2011-06-20 15:11:22 -0700741void isci_terminate_pending_requests(struct isci_host *ihost,
742 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700743{
Dan Williams980d3ae2011-06-20 15:11:22 -0700744 struct completion request_completion;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700745 enum isci_request_status old_state;
Dan Williams980d3ae2011-06-20 15:11:22 -0700746 unsigned long flags;
747 LIST_HEAD(list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700748
Dan Williams980d3ae2011-06-20 15:11:22 -0700749 spin_lock_irqsave(&ihost->scic_lock, flags);
750 list_splice_init(&idev->reqs_in_process, &list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700751
Dan Williams980d3ae2011-06-20 15:11:22 -0700752 /* assumes that isci_terminate_request_core deletes from the list */
753 while (!list_empty(&list)) {
754 struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -0700755
Dan Williams980d3ae2011-06-20 15:11:22 -0700756 /* Change state to "terminating" if it is currently
757 * "started".
758 */
759 old_state = isci_request_change_started_to_newstate(ireq,
760 &request_completion,
761 terminating);
762 switch (old_state) {
763 case started:
764 case completed:
765 case aborting:
766 break;
767 default:
768 /* termination in progress, or otherwise dispositioned.
769 * We know the request was on 'list' so should be safe
770 * to move it back to reqs_in_process
771 */
772 list_move(&ireq->dev_node, &idev->reqs_in_process);
773 ireq = NULL;
774 break;
775 }
776
777 if (!ireq)
778 continue;
779 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800780
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700781 init_completion(&request_completion);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800782
Dan Williams980d3ae2011-06-20 15:11:22 -0700783 dev_dbg(&ihost->pdev->dev,
784 "%s: idev=%p request=%p; task=%p old_state=%d\n",
785 __func__, idev, ireq,
786 ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL,
787 old_state);
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700788
Dan Williams980d3ae2011-06-20 15:11:22 -0700789 /* If the old_state is started:
790 * This request was not already being aborted. If it had been,
791 * then the aborting I/O (ie. the TMF request) would not be in
792 * the aborting state, and thus would be terminated here. Note
793 * that since the TMF completion's call to the kernel function
794 * "complete()" does not happen until the pending I/O request
795 * terminate fully completes, we do not have to implement a
796 * special wait here for already aborting requests - the
797 * termination of the TMF request will force the request
798 * to finish it's already started terminate.
799 *
800 * If old_state == completed:
801 * This request completed from the SCU hardware perspective
802 * and now just needs cleaning up in terms of freeing the
803 * request and potentially calling up to libsas.
804 *
805 * If old_state == aborting:
806 * This request has already gone through a TMF timeout, but may
807 * not have been terminated; needs cleaning up at least.
808 */
809 isci_terminate_request_core(ihost, idev, ireq);
810 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700811 }
Dan Williams980d3ae2011-06-20 15:11:22 -0700812 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700813}
814
815/**
816 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
817 * Template functions.
818 * @lun: This parameter specifies the lun to be reset.
819 *
820 * status, zero indicates success.
821 */
822static int isci_task_send_lu_reset_sas(
823 struct isci_host *isci_host,
824 struct isci_remote_device *isci_device,
825 u8 *lun)
826{
827 struct isci_tmf tmf;
828 int ret = TMF_RESP_FUNC_FAILED;
829
830 dev_dbg(&isci_host->pdev->dev,
831 "%s: isci_host = %p, isci_device = %p\n",
832 __func__, isci_host, isci_device);
833 /* Send the LUN reset to the target. By the time the call returns,
834 * the TMF has fully exected in the target (in which case the return
835 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
836 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
837 */
Dan Williams209fae12011-06-13 17:39:44 -0700838 isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset, NULL, NULL);
Dan Williams6f231dd2011-07-02 22:56:22 -0700839
840 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
Dan Williams209fae12011-06-13 17:39:44 -0700841 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
Dan Williams6f231dd2011-07-02 22:56:22 -0700842
843 if (ret == TMF_RESP_FUNC_COMPLETE)
844 dev_dbg(&isci_host->pdev->dev,
845 "%s: %p: TMF_LU_RESET passed\n",
846 __func__, isci_device);
847 else
848 dev_dbg(&isci_host->pdev->dev,
849 "%s: %p: TMF_LU_RESET failed (%x)\n",
850 __func__, isci_device, ret);
851
852 return ret;
853}
854
Dan Williams16ba7702011-07-01 10:52:55 -0700855static int isci_task_send_lu_reset_sata(struct isci_host *ihost,
856 struct isci_remote_device *idev, u8 *lun)
857{
858 int ret = TMF_RESP_FUNC_FAILED;
859 struct isci_tmf tmf;
860
861 /* Send the soft reset to the target */
862 #define ISCI_SRST_TIMEOUT_MS 25000 /* 25 second timeout. */
863 isci_task_build_tmf(&tmf, isci_tmf_sata_srst_high, NULL, NULL);
864
865 ret = isci_task_execute_tmf(ihost, idev, &tmf, ISCI_SRST_TIMEOUT_MS);
866
867 if (ret != TMF_RESP_FUNC_COMPLETE) {
Dan Williamsa8a0a132011-07-01 12:07:25 -0700868 dev_dbg(&ihost->pdev->dev,
Dan Williams16ba7702011-07-01 10:52:55 -0700869 "%s: Assert SRST failed (%p) = %x",
870 __func__, idev, ret);
871
872 /* Return the failure so that the LUN reset is escalated
873 * to a target reset.
874 */
875 }
876 return ret;
877}
878
Dan Williams6f231dd2011-07-02 22:56:22 -0700879/**
880 * isci_task_lu_reset() - This function is one of the SAS Domain Template
881 * functions. This is one of the Task Management functoins called by libsas,
882 * to reset the given lun. Note the assumption that while this call is
883 * executing, no I/O will be sent by the host to the device.
884 * @lun: This parameter specifies the lun to be reset.
885 *
886 * status, zero indicates success.
887 */
Dan Williams4393aa42011-03-31 13:10:44 -0700888int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
Dan Williams6f231dd2011-07-02 22:56:22 -0700889{
Dan Williams4393aa42011-03-31 13:10:44 -0700890 struct isci_host *isci_host = dev_to_ihost(domain_device);
Dan Williams209fae12011-06-13 17:39:44 -0700891 struct isci_remote_device *isci_device;
892 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700893 int ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700894
Dan Williams209fae12011-06-13 17:39:44 -0700895 spin_lock_irqsave(&isci_host->scic_lock, flags);
896 isci_device = isci_lookup_device(domain_device);
897 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700898
Dan Williams4393aa42011-03-31 13:10:44 -0700899 dev_dbg(&isci_host->pdev->dev,
900 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700901 __func__, domain_device, isci_host, isci_device);
902
Dan Williams209fae12011-06-13 17:39:44 -0700903 if (isci_device)
Dan Williamsd06b4872011-05-02 13:59:25 -0700904 set_bit(IDEV_EH, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700905
906 /* If there is a device reset pending on any request in the
907 * device's list, fail this LUN reset request in order to
908 * escalate to the device reset.
909 */
Dan Williams209fae12011-06-13 17:39:44 -0700910 if (!isci_device ||
Dan Williams4393aa42011-03-31 13:10:44 -0700911 isci_device_is_reset_pending(isci_host, isci_device)) {
Dan Williamsa8a0a132011-07-01 12:07:25 -0700912 dev_dbg(&isci_host->pdev->dev,
Dan Williams4393aa42011-03-31 13:10:44 -0700913 "%s: No dev (%p), or "
Dan Williams6f231dd2011-07-02 22:56:22 -0700914 "RESET PENDING: domain_device=%p\n",
Dan Williams4393aa42011-03-31 13:10:44 -0700915 __func__, isci_device, domain_device);
Dan Williams209fae12011-06-13 17:39:44 -0700916 ret = TMF_RESP_FUNC_FAILED;
917 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -0700918 }
919
Dan Williams6f231dd2011-07-02 22:56:22 -0700920 /* Send the task management part of the reset. */
921 if (sas_protocol_ata(domain_device->tproto)) {
Dan Williams4393aa42011-03-31 13:10:44 -0700922 ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
Dan Williams6f231dd2011-07-02 22:56:22 -0700923 } else
924 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
925
926 /* If the LUN reset worked, all the I/O can now be terminated. */
927 if (ret == TMF_RESP_FUNC_COMPLETE)
928 /* Terminate all I/O now. */
929 isci_terminate_pending_requests(isci_host,
Dan Williams980d3ae2011-06-20 15:11:22 -0700930 isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700931
Dan Williams209fae12011-06-13 17:39:44 -0700932 out:
933 isci_put_device(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700934 return ret;
935}
936
937
938/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
939int isci_task_clear_nexus_port(struct asd_sas_port *port)
940{
941 return TMF_RESP_FUNC_FAILED;
942}
943
944
945
946int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
947{
948 return TMF_RESP_FUNC_FAILED;
949}
950
Dan Williams6f231dd2011-07-02 22:56:22 -0700951/* Task Management Functions. Must be called from process context. */
952
953/**
954 * isci_abort_task_process_cb() - This is a helper function for the abort task
955 * TMF command. It manages the request state with respect to the successful
956 * transmission / completion of the abort task request.
957 * @cb_state: This parameter specifies when this function was called - after
958 * the TMF request has been started and after it has timed-out.
959 * @tmf: This parameter specifies the TMF in progress.
960 *
961 *
962 */
963static void isci_abort_task_process_cb(
964 enum isci_tmf_cb_state cb_state,
965 struct isci_tmf *tmf,
966 void *cb_data)
967{
968 struct isci_request *old_request;
969
970 old_request = (struct isci_request *)cb_data;
971
972 dev_dbg(&old_request->isci_host->pdev->dev,
973 "%s: tmf=%p, old_request=%p\n",
974 __func__, tmf, old_request);
975
976 switch (cb_state) {
977
978 case isci_tmf_started:
979 /* The TMF has been started. Nothing to do here, since the
980 * request state was already set to "aborted" by the abort
981 * task function.
982 */
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700983 if ((old_request->status != aborted)
984 && (old_request->status != completed))
Dan Williamsa8a0a132011-07-01 12:07:25 -0700985 dev_dbg(&old_request->isci_host->pdev->dev,
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700986 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
987 __func__, old_request->status, tmf, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700988 break;
989
990 case isci_tmf_timed_out:
991
992 /* Set the task's state to "aborting", since the abort task
993 * function thread set it to "aborted" (above) in anticipation
994 * of the task management request working correctly. Since the
995 * timeout has now fired, the TMF request failed. We set the
996 * state such that the request completion will indicate the
997 * device is no longer present.
998 */
999 isci_request_change_state(old_request, aborting);
1000 break;
1001
1002 default:
Dan Williamsa8a0a132011-07-01 12:07:25 -07001003 dev_dbg(&old_request->isci_host->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001004 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
1005 __func__, cb_state, tmf, old_request);
1006 break;
1007 }
1008}
1009
1010/**
1011 * isci_task_abort_task() - This function is one of the SAS Domain Template
1012 * functions. This function is called by libsas to abort a specified task.
1013 * @task: This parameter specifies the SAS task to abort.
1014 *
1015 * status, zero indicates success.
1016 */
1017int isci_task_abort_task(struct sas_task *task)
1018{
Dan Williams4393aa42011-03-31 13:10:44 -07001019 struct isci_host *isci_host = dev_to_ihost(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001020 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001021 struct isci_request *old_request = NULL;
1022 enum isci_request_status old_state;
Dan Williams6f231dd2011-07-02 22:56:22 -07001023 struct isci_remote_device *isci_device = NULL;
Jeff Skirvina5fde222011-03-04 14:06:42 -08001024 struct isci_tmf tmf;
1025 int ret = TMF_RESP_FUNC_FAILED;
1026 unsigned long flags;
1027 bool any_dev_reset = false;
Dan Williams6f231dd2011-07-02 22:56:22 -07001028
1029 /* Get the isci_request reference from the task. Note that
1030 * this check does not depend on the pending request list
1031 * in the device, because tasks driving resets may land here
1032 * after completion in the core.
1033 */
Dan Williams209fae12011-06-13 17:39:44 -07001034 spin_lock_irqsave(&isci_host->scic_lock, flags);
1035 spin_lock(&task->task_state_lock);
1036
1037 old_request = task->lldd_task;
1038
1039 /* If task is already done, the request isn't valid */
1040 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
1041 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
1042 old_request)
1043 isci_device = isci_lookup_device(task->dev);
1044
1045 spin_unlock(&task->task_state_lock);
1046 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001047
1048 dev_dbg(&isci_host->pdev->dev,
1049 "%s: task = %p\n", __func__, task);
1050
Dan Williams209fae12011-06-13 17:39:44 -07001051 if (!isci_device || !old_request)
1052 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001053
Dan Williamsd06b4872011-05-02 13:59:25 -07001054 set_bit(IDEV_EH, &isci_device->flags);
1055
Dan Williams6f231dd2011-07-02 22:56:22 -07001056 /* This version of the driver will fail abort requests for
1057 * SATA/STP. Failing the abort request this way will cause the
1058 * SCSI error handler thread to escalate to LUN reset
1059 */
Dan Williams209fae12011-06-13 17:39:44 -07001060 if (sas_protocol_ata(task->task_proto)) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07001061 dev_dbg(&isci_host->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001062 " task %p is for a STP/SATA device;"
1063 " returning TMF_RESP_FUNC_FAILED\n"
1064 " to cause a LUN reset...\n", task);
Dan Williams209fae12011-06-13 17:39:44 -07001065 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001066 }
1067
1068 dev_dbg(&isci_host->pdev->dev,
1069 "%s: old_request == %p\n", __func__, old_request);
1070
James Bottomleya5ec7f862011-07-03 14:14:45 -05001071 any_dev_reset = isci_device_is_reset_pending(isci_host, isci_device);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001072
Dan Williams6f231dd2011-07-02 22:56:22 -07001073 spin_lock_irqsave(&task->task_state_lock, flags);
1074
Dan Williams209fae12011-06-13 17:39:44 -07001075 any_dev_reset = any_dev_reset || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
Dan Williams6f231dd2011-07-02 22:56:22 -07001076
1077 /* If the extraction of the request reference from the task
1078 * failed, then the request has been completed (or if there is a
1079 * pending reset then this abort request function must be failed
1080 * in order to escalate to the target reset).
1081 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001082 if ((old_request == NULL) || any_dev_reset) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001083
1084 /* If the device reset task flag is set, fail the task
1085 * management request. Otherwise, the original request
1086 * has completed.
1087 */
1088 if (any_dev_reset) {
1089
1090 /* Turn off the task's DONE to make sure this
1091 * task is escalated to a target reset.
1092 */
1093 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1094
Jeff Skirvina5fde222011-03-04 14:06:42 -08001095 /* Make the reset happen as soon as possible. */
1096 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1097
1098 spin_unlock_irqrestore(&task->task_state_lock, flags);
1099
Dan Williams6f231dd2011-07-02 22:56:22 -07001100 /* Fail the task management request in order to
1101 * escalate to the target reset.
1102 */
1103 ret = TMF_RESP_FUNC_FAILED;
1104
1105 dev_dbg(&isci_host->pdev->dev,
1106 "%s: Failing task abort in order to "
1107 "escalate to target reset because\n"
1108 "SAS_TASK_NEED_DEV_RESET is set for "
1109 "task %p on dev %p\n",
1110 __func__, task, isci_device);
1111
Jeff Skirvina5fde222011-03-04 14:06:42 -08001112
Dan Williams6f231dd2011-07-02 22:56:22 -07001113 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -07001114 /* The request has already completed and there
1115 * is nothing to do here other than to set the task
1116 * done bit, and indicate that the task abort function
1117 * was sucessful.
1118 */
1119 isci_set_task_doneflags(task);
1120
Jeff Skirvina5fde222011-03-04 14:06:42 -08001121 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001122
Jeff Skirvina5fde222011-03-04 14:06:42 -08001123 ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001124
Jeff Skirvina5fde222011-03-04 14:06:42 -08001125 dev_dbg(&isci_host->pdev->dev,
1126 "%s: abort task not needed for %p\n",
1127 __func__, task);
Dan Williams6f231dd2011-07-02 22:56:22 -07001128 }
Dan Williams209fae12011-06-13 17:39:44 -07001129 goto out;
James Bottomleya5ec7f862011-07-03 14:14:45 -05001130 } else {
Jeff Skirvina5fde222011-03-04 14:06:42 -08001131 spin_unlock_irqrestore(&task->task_state_lock, flags);
James Bottomleya5ec7f862011-07-03 14:14:45 -05001132 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001133
1134 spin_lock_irqsave(&isci_host->scic_lock, flags);
1135
Jeff Skirvinf219f012011-03-31 13:10:34 -07001136 /* Check the request status and change to "aborted" if currently
Jeff Skirvina5fde222011-03-04 14:06:42 -08001137 * "starting"; if true then set the I/O kernel completion
Dan Williams6f231dd2011-07-02 22:56:22 -07001138 * struct that will be triggered when the request completes.
1139 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001140 old_state = isci_task_validate_request_to_abort(
1141 old_request, isci_host, isci_device,
1142 &aborted_io_completion);
Jeff Skirvinf219f012011-03-31 13:10:34 -07001143 if ((old_state != started) &&
1144 (old_state != completed) &&
1145 (old_state != aborting)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001146
1147 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1148
Jeff Skirvina5fde222011-03-04 14:06:42 -08001149 /* The request was already being handled by someone else (because
1150 * they got to set the state away from started).
1151 */
1152 dev_dbg(&isci_host->pdev->dev,
1153 "%s: device = %p; old_request %p already being aborted\n",
1154 __func__,
1155 isci_device, old_request);
Dan Williams209fae12011-06-13 17:39:44 -07001156 ret = TMF_RESP_FUNC_COMPLETE;
1157 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001158 }
Dan Williams38d88792011-06-23 14:33:48 -07001159 if (task->task_proto == SAS_PROTOCOL_SMP ||
1160 test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001161
1162 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1163
Jeff Skirvina5fde222011-03-04 14:06:42 -08001164 dev_dbg(&isci_host->pdev->dev,
1165 "%s: SMP request (%d)"
Jeff Skirvina5fde222011-03-04 14:06:42 -08001166 " or complete_in_target (%d), thus no TMF\n",
1167 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
Dan Williams38d88792011-06-23 14:33:48 -07001168 test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags));
Jeff Skirvina5fde222011-03-04 14:06:42 -08001169
Dan Williams6f231dd2011-07-02 22:56:22 -07001170 /* Set the state on the task. */
1171 isci_task_all_done(task);
1172
1173 ret = TMF_RESP_FUNC_COMPLETE;
1174
1175 /* Stopping and SMP devices are not sent a TMF, and are not
Jeff Skirvina5fde222011-03-04 14:06:42 -08001176 * reset, but the outstanding I/O request is terminated below.
Dan Williams6f231dd2011-07-02 22:56:22 -07001177 */
Dan Williams6f231dd2011-07-02 22:56:22 -07001178 } else {
1179 /* Fill in the tmf stucture */
Dan Williams209fae12011-06-13 17:39:44 -07001180 isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -08001181 isci_abort_task_process_cb,
1182 old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001183
Dan Williams6f231dd2011-07-02 22:56:22 -07001184 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1185
1186 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
Dan Williams209fae12011-06-13 17:39:44 -07001187 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf,
Dan Williams6f231dd2011-07-02 22:56:22 -07001188 ISCI_ABORT_TASK_TIMEOUT_MS);
1189
Jeff Skirvina5fde222011-03-04 14:06:42 -08001190 if (ret != TMF_RESP_FUNC_COMPLETE)
Dan Williamsa8a0a132011-07-01 12:07:25 -07001191 dev_dbg(&isci_host->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001192 "%s: isci_task_send_tmf failed\n",
1193 __func__);
1194 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001195 if (ret == TMF_RESP_FUNC_COMPLETE) {
Dan Williams38d88792011-06-23 14:33:48 -07001196 set_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001197
Jeff Skirvin77c852f2011-06-20 14:09:16 -07001198 /* Clean up the request on our side, and wait for the aborted
1199 * I/O to complete.
1200 */
Jeff Skirvincbb65c62011-03-04 14:06:50 -08001201 isci_terminate_request_core(isci_host, isci_device, old_request);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001202 }
1203
1204 /* Make sure we do not leave a reference to aborted_io_completion */
1205 old_request->io_request_completion = NULL;
Dan Williams209fae12011-06-13 17:39:44 -07001206 out:
1207 isci_put_device(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001208 return ret;
1209}
1210
1211/**
1212 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1213 * functions. This is one of the Task Management functoins called by libsas,
1214 * to abort all task for the given lun.
1215 * @d_device: This parameter specifies the domain device associated with this
1216 * request.
1217 * @lun: This parameter specifies the lun associated with this request.
1218 *
1219 * status, zero indicates success.
1220 */
1221int isci_task_abort_task_set(
1222 struct domain_device *d_device,
1223 u8 *lun)
1224{
1225 return TMF_RESP_FUNC_FAILED;
1226}
1227
1228
1229/**
1230 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1231 * functions. This is one of the Task Management functoins called by libsas.
1232 * @d_device: This parameter specifies the domain device associated with this
1233 * request.
1234 * @lun: This parameter specifies the lun associated with this request.
1235 *
1236 * status, zero indicates success.
1237 */
1238int isci_task_clear_aca(
1239 struct domain_device *d_device,
1240 u8 *lun)
1241{
1242 return TMF_RESP_FUNC_FAILED;
1243}
1244
1245
1246
1247/**
1248 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1249 * functions. This is one of the Task Management functoins called by libsas.
1250 * @d_device: This parameter specifies the domain device associated with this
1251 * request.
1252 * @lun: This parameter specifies the lun associated with this request.
1253 *
1254 * status, zero indicates success.
1255 */
1256int isci_task_clear_task_set(
1257 struct domain_device *d_device,
1258 u8 *lun)
1259{
1260 return TMF_RESP_FUNC_FAILED;
1261}
1262
1263
1264/**
1265 * isci_task_query_task() - This function is implemented to cause libsas to
1266 * correctly escalate the failed abort to a LUN or target reset (this is
1267 * because sas_scsi_find_task libsas function does not correctly interpret
1268 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1269 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1270 * returned, libsas will turn this into a target reset
1271 * @task: This parameter specifies the sas task being queried.
1272 * @lun: This parameter specifies the lun associated with this request.
1273 *
1274 * status, zero indicates success.
1275 */
1276int isci_task_query_task(
1277 struct sas_task *task)
1278{
1279 /* See if there is a pending device reset for this device. */
1280 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1281 return TMF_RESP_FUNC_FAILED;
1282 else
1283 return TMF_RESP_FUNC_SUCC;
1284}
1285
Dave Jiangaf5ae892011-05-04 17:53:24 -07001286/*
Dan Williams6f231dd2011-07-02 22:56:22 -07001287 * isci_task_request_complete() - This function is called by the sci core when
1288 * an task request completes.
Dave Jiangaf5ae892011-05-04 17:53:24 -07001289 * @ihost: This parameter specifies the ISCI host object
1290 * @ireq: This parameter is the completed isci_request object.
Dan Williams6f231dd2011-07-02 22:56:22 -07001291 * @completion_status: This parameter specifies the completion status from the
1292 * sci core.
1293 *
1294 * none.
1295 */
Dave Jiangaf5ae892011-05-04 17:53:24 -07001296void
1297isci_task_request_complete(struct isci_host *ihost,
1298 struct isci_request *ireq,
1299 enum sci_task_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001300{
Dave Jiangaf5ae892011-05-04 17:53:24 -07001301 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001302 struct completion *tmf_complete;
1303
Dave Jiangaf5ae892011-05-04 17:53:24 -07001304 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001305 "%s: request = %p, status=%d\n",
Dave Jiangaf5ae892011-05-04 17:53:24 -07001306 __func__, ireq, completion_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001307
Dave Jiang8d2c65c2011-06-01 09:03:08 +00001308 isci_request_change_state(ireq, completed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001309
1310 tmf->status = completion_status;
Dan Williams38d88792011-06-23 14:33:48 -07001311 set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001312
Dave Jiangaf5ae892011-05-04 17:53:24 -07001313 if (tmf->proto == SAS_PROTOCOL_SSP) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001314 memcpy(&tmf->resp.resp_iu,
Dan Williams5076a1a2011-06-27 14:57:03 -07001315 &ireq->ssp.rsp,
Dave Jiangaf5ae892011-05-04 17:53:24 -07001316 SSP_RESP_IU_MAX_SIZE);
1317 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001318 memcpy(&tmf->resp.d2h_fis,
Dan Williams5076a1a2011-06-27 14:57:03 -07001319 &ireq->stp.rsp,
Dave Jiangf2f30082011-05-04 15:02:02 -07001320 sizeof(struct dev_to_host_fis));
Dan Williams6f231dd2011-07-02 22:56:22 -07001321 }
1322
Dan Williams6f231dd2011-07-02 22:56:22 -07001323 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1324 tmf_complete = tmf->complete;
1325
Dan Williams89a73012011-06-30 19:14:33 -07001326 sci_controller_complete_io(ihost, ireq->target_device, ireq);
Dan Williams67ea8382011-05-08 11:47:15 -07001327 /* set the 'terminated' flag handle to make sure it cannot be terminated
Dan Williams6f231dd2011-07-02 22:56:22 -07001328 * or completed again.
1329 */
Dan Williams38d88792011-06-23 14:33:48 -07001330 set_bit(IREQ_TERMINATED, &ireq->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001331
Dave Jiangaf5ae892011-05-04 17:53:24 -07001332 isci_request_change_state(ireq, unallocated);
1333 list_del_init(&ireq->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -07001334
1335 /* The task management part completes last. */
1336 complete(tmf_complete);
1337}
1338
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001339static void isci_smp_task_timedout(unsigned long _task)
1340{
1341 struct sas_task *task = (void *) _task;
1342 unsigned long flags;
1343
1344 spin_lock_irqsave(&task->task_state_lock, flags);
1345 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1346 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1347 spin_unlock_irqrestore(&task->task_state_lock, flags);
1348
1349 complete(&task->completion);
1350}
1351
1352static void isci_smp_task_done(struct sas_task *task)
1353{
1354 if (!del_timer(&task->timer))
1355 return;
1356 complete(&task->completion);
1357}
1358
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001359static int isci_smp_execute_task(struct isci_host *ihost,
1360 struct domain_device *dev, void *req,
1361 int req_size, void *resp, int resp_size)
1362{
1363 int res, retry;
1364 struct sas_task *task = NULL;
1365
1366 for (retry = 0; retry < 3; retry++) {
Dan Williams4fcf8122011-07-29 17:26:39 -07001367 task = sas_alloc_task(GFP_KERNEL);
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001368 if (!task)
1369 return -ENOMEM;
1370
1371 task->dev = dev;
1372 task->task_proto = dev->tproto;
1373 sg_init_one(&task->smp_task.smp_req, req, req_size);
1374 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
1375
1376 task->task_done = isci_smp_task_done;
1377
1378 task->timer.data = (unsigned long) task;
1379 task->timer.function = isci_smp_task_timedout;
1380 task->timer.expires = jiffies + 10*HZ;
1381 add_timer(&task->timer);
1382
1383 res = isci_task_execute_task(task, 1, GFP_KERNEL);
1384
1385 if (res) {
1386 del_timer(&task->timer);
Dan Williamsa8a0a132011-07-01 12:07:25 -07001387 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001388 "%s: executing SMP task failed:%d\n",
1389 __func__, res);
1390 goto ex_err;
1391 }
1392
1393 wait_for_completion(&task->completion);
1394 res = -ECOMM;
1395 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07001396 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001397 "%s: smp task timed out or aborted\n",
1398 __func__);
1399 isci_task_abort_task(task);
1400 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07001401 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001402 "%s: SMP task aborted and not done\n",
1403 __func__);
1404 goto ex_err;
1405 }
1406 }
1407 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1408 task->task_status.stat == SAM_STAT_GOOD) {
1409 res = 0;
1410 break;
1411 }
1412 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1413 task->task_status.stat == SAS_DATA_UNDERRUN) {
1414 /* no error, but return the number of bytes of
1415 * underrun */
1416 res = task->task_status.residual;
1417 break;
1418 }
1419 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1420 task->task_status.stat == SAS_DATA_OVERRUN) {
1421 res = -EMSGSIZE;
1422 break;
1423 } else {
Dan Williamsa8a0a132011-07-01 12:07:25 -07001424 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001425 "%s: task to dev %016llx response: 0x%x "
1426 "status 0x%x\n", __func__,
1427 SAS_ADDR(dev->sas_addr),
1428 task->task_status.resp,
1429 task->task_status.stat);
Dan Williams4fcf8122011-07-29 17:26:39 -07001430 sas_free_task(task);
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001431 task = NULL;
1432 }
1433 }
1434ex_err:
1435 BUG_ON(retry == 3 && task != NULL);
Dan Williams4fcf8122011-07-29 17:26:39 -07001436 sas_free_task(task);
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001437 return res;
1438}
1439
1440#define DISCOVER_REQ_SIZE 16
1441#define DISCOVER_RESP_SIZE 56
1442
1443int isci_smp_get_phy_attached_dev_type(struct isci_host *ihost,
1444 struct domain_device *dev,
1445 int phy_id, int *adt)
1446{
1447 struct smp_resp *disc_resp;
1448 u8 *disc_req;
1449 int res;
1450
1451 disc_resp = kzalloc(DISCOVER_RESP_SIZE, GFP_KERNEL);
1452 if (!disc_resp)
1453 return -ENOMEM;
1454
1455 disc_req = kzalloc(DISCOVER_REQ_SIZE, GFP_KERNEL);
1456 if (disc_req) {
1457 disc_req[0] = SMP_REQUEST;
1458 disc_req[1] = SMP_DISCOVER;
1459 disc_req[9] = phy_id;
1460 } else {
1461 kfree(disc_resp);
1462 return -ENOMEM;
1463 }
1464 res = isci_smp_execute_task(ihost, dev, disc_req, DISCOVER_REQ_SIZE,
1465 disc_resp, DISCOVER_RESP_SIZE);
1466 if (!res) {
1467 if (disc_resp->result != SMP_RESP_FUNC_ACC)
1468 res = disc_resp->result;
1469 else
1470 *adt = disc_resp->disc.attached_dev_type;
1471 }
1472 kfree(disc_req);
1473 kfree(disc_resp);
1474
1475 return res;
1476}
1477
1478static void isci_wait_for_smp_phy_reset(struct isci_remote_device *idev, int phy_num)
1479{
1480 struct domain_device *dev = idev->domain_dev;
1481 struct isci_port *iport = idev->isci_port;
1482 struct isci_host *ihost = iport->isci_host;
1483 int res, iteration = 0, attached_device_type;
1484 #define STP_WAIT_MSECS 25000
1485 unsigned long tmo = msecs_to_jiffies(STP_WAIT_MSECS);
1486 unsigned long deadline = jiffies + tmo;
1487 enum {
1488 SMP_PHYWAIT_PHYDOWN,
1489 SMP_PHYWAIT_PHYUP,
1490 SMP_PHYWAIT_DONE
1491 } phy_state = SMP_PHYWAIT_PHYDOWN;
1492
1493 /* While there is time, wait for the phy to go away and come back */
1494 while (time_is_after_jiffies(deadline) && phy_state != SMP_PHYWAIT_DONE) {
1495 int event = atomic_read(&iport->event);
1496
1497 ++iteration;
1498
1499 tmo = wait_event_timeout(ihost->eventq,
1500 event != atomic_read(&iport->event) ||
1501 !test_bit(IPORT_BCN_BLOCKED, &iport->flags),
1502 tmo);
1503 /* link down, stop polling */
1504 if (!test_bit(IPORT_BCN_BLOCKED, &iport->flags))
1505 break;
1506
1507 dev_dbg(&ihost->pdev->dev,
1508 "%s: iport %p, iteration %d,"
1509 " phase %d: time_remaining %lu, bcns = %d\n",
1510 __func__, iport, iteration, phy_state,
1511 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags));
1512
1513 res = isci_smp_get_phy_attached_dev_type(ihost, dev, phy_num,
1514 &attached_device_type);
1515 tmo = deadline - jiffies;
1516
1517 if (res) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07001518 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001519 "%s: iteration %d, phase %d:"
1520 " SMP error=%d, time_remaining=%lu\n",
1521 __func__, iteration, phy_state, res, tmo);
1522 break;
1523 }
1524 dev_dbg(&ihost->pdev->dev,
1525 "%s: iport %p, iteration %d,"
1526 " phase %d: time_remaining %lu, bcns = %d, "
1527 "attdevtype = %x\n",
1528 __func__, iport, iteration, phy_state,
1529 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags),
1530 attached_device_type);
1531
1532 switch (phy_state) {
1533 case SMP_PHYWAIT_PHYDOWN:
1534 /* Has the device gone away? */
1535 if (!attached_device_type)
1536 phy_state = SMP_PHYWAIT_PHYUP;
1537
1538 break;
1539
1540 case SMP_PHYWAIT_PHYUP:
1541 /* Has the device come back? */
1542 if (attached_device_type)
1543 phy_state = SMP_PHYWAIT_DONE;
1544 break;
1545
1546 case SMP_PHYWAIT_DONE:
1547 break;
1548 }
1549
1550 }
1551 dev_dbg(&ihost->pdev->dev, "%s: done\n", __func__);
1552}
1553
Dan Williams209fae12011-06-13 17:39:44 -07001554static int isci_reset_device(struct isci_host *ihost,
Jeff Skirvinbc6f3872011-07-01 13:03:44 -07001555 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001556{
Dan Williams209fae12011-06-13 17:39:44 -07001557 struct sas_phy *phy = sas_find_local_phy(idev->domain_dev);
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001558 struct isci_port *iport = idev->isci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001559 enum sci_status status;
Dan Williamsd06b4872011-05-02 13:59:25 -07001560 unsigned long flags;
1561 int rc;
Dan Williams6f231dd2011-07-02 22:56:22 -07001562
Dan Williamsd06b4872011-05-02 13:59:25 -07001563 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001564
Dan Williamsd06b4872011-05-02 13:59:25 -07001565 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -07001566 status = sci_remote_device_reset(idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001567 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001568 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001569
Dan Williamsa8a0a132011-07-01 12:07:25 -07001570 dev_dbg(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -07001571 "%s: sci_remote_device_reset(%p) returned %d!\n",
Dan Williamsd06b4872011-05-02 13:59:25 -07001572 __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001573
1574 return TMF_RESP_FUNC_FAILED;
1575 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001576 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001577
Dan Williams6f231dd2011-07-02 22:56:22 -07001578 /* Make sure all pending requests are able to be fully terminated. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001579 isci_device_clear_reset_pending(ihost, idev);
1580
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001581 /* If this is a device on an expander, disable BCN processing. */
1582 if (!scsi_is_sas_phy_local(phy))
1583 set_bit(IPORT_BCN_BLOCKED, &iport->flags);
1584
Jeff Skirvinbc6f3872011-07-01 13:03:44 -07001585 rc = sas_phy_reset(phy, true);
Dan Williams6f231dd2011-07-02 22:56:22 -07001586
1587 /* Terminate in-progress I/O now. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001588 isci_remote_device_nuke_requests(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001589
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001590 /* Since all pending TCs have been cleaned, resume the RNC. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001591 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -07001592 status = sci_remote_device_reset_complete(idev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001593 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001594
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001595 /* If this is a device on an expander, bring the phy back up. */
1596 if (!scsi_is_sas_phy_local(phy)) {
1597 /* A phy reset will cause the device to go away then reappear.
1598 * Since libsas will take action on incoming BCNs (eg. remove
1599 * a device going through an SMP phy-control driven reset),
1600 * we need to wait until the phy comes back up before letting
1601 * discovery proceed in libsas.
1602 */
1603 isci_wait_for_smp_phy_reset(idev, phy->number);
1604
1605 spin_lock_irqsave(&ihost->scic_lock, flags);
1606 isci_port_bcn_enable(ihost, idev->isci_port);
1607 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1608 }
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001609
Dan Williams6f231dd2011-07-02 22:56:22 -07001610 if (status != SCI_SUCCESS) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07001611 dev_dbg(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -07001612 "%s: sci_remote_device_reset_complete(%p) "
Dan Williamsd06b4872011-05-02 13:59:25 -07001613 "returned %d!\n", __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001614 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001615
Dan Williamsd06b4872011-05-02 13:59:25 -07001616 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001617
Dan Williamsd06b4872011-05-02 13:59:25 -07001618 return rc;
1619}
1620
1621int isci_task_I_T_nexus_reset(struct domain_device *dev)
1622{
1623 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001624 struct isci_remote_device *idev;
1625 unsigned long flags;
Jeff Skirvinbc6f3872011-07-01 13:03:44 -07001626 int ret;
Dan Williamsd06b4872011-05-02 13:59:25 -07001627
Dan Williamsd06b4872011-05-02 13:59:25 -07001628 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams209fae12011-06-13 17:39:44 -07001629 idev = isci_lookup_device(dev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001630 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1631
Dan Williams209fae12011-06-13 17:39:44 -07001632 if (!idev || !test_bit(IDEV_EH, &idev->flags)) {
1633 ret = TMF_RESP_FUNC_COMPLETE;
1634 goto out;
1635 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001636
Jeff Skirvinbc6f3872011-07-01 13:03:44 -07001637 ret = isci_reset_device(ihost, idev);
Dan Williams209fae12011-06-13 17:39:44 -07001638 out:
1639 isci_put_device(idev);
1640 return ret;
Dan Williamsd06b4872011-05-02 13:59:25 -07001641}
1642
1643int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1644{
1645 struct domain_device *dev = sdev_to_domain_dev(cmd->device);
Dan Williams209fae12011-06-13 17:39:44 -07001646 struct isci_host *ihost = dev_to_ihost(dev);
1647 struct isci_remote_device *idev;
Dan Williams209fae12011-06-13 17:39:44 -07001648 unsigned long flags;
Jeff Skirvinbc6f3872011-07-01 13:03:44 -07001649 int ret;
Dan Williamsd06b4872011-05-02 13:59:25 -07001650
Dan Williams209fae12011-06-13 17:39:44 -07001651 spin_lock_irqsave(&ihost->scic_lock, flags);
1652 idev = isci_lookup_device(dev);
1653 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1654
1655 if (!idev) {
1656 ret = TMF_RESP_FUNC_COMPLETE;
1657 goto out;
1658 }
1659
Jeff Skirvinbc6f3872011-07-01 13:03:44 -07001660 ret = isci_reset_device(ihost, idev);
Dan Williams209fae12011-06-13 17:39:44 -07001661 out:
1662 isci_put_device(idev);
1663 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -07001664}