blob: 597c49020ac8ba1db088880b9d9767e049d51f10 [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"
Dan Williams6f231dd2011-07-02 22:56:22 -070059#include "scic_task_request.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070060#include "scic_io_request.h"
Dan Williams88f3b622011-04-22 19:18:03 -070061#include "remote_device.h"
62#include "remote_node_context.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070063#include "isci.h"
64#include "request.h"
65#include "sata.h"
66#include "task.h"
Dan Williams827a84d2011-05-07 15:59:09 -070067#include "scic_sds_request.h"
Dan Williamsce2b3262011-05-08 15:49:15 -070068#include "scic_controller.h"
69#include "timers.h"
Dave Jiangaf5ae892011-05-04 17:53:24 -070070
Dan Williams50e7f9b2011-03-09 21:27:46 -080071/**
Dan Williams1077a572011-03-11 10:13:51 -080072* isci_task_refuse() - complete the request to the upper layer driver in
73* the case where an I/O needs to be completed back in the submit path.
74* @ihost: host on which the the request was queued
75* @task: request to complete
76* @response: response code for the completed task.
77* @status: status code for the completed task.
Dan Williams50e7f9b2011-03-09 21:27:46 -080078*
Dan Williams50e7f9b2011-03-09 21:27:46 -080079*/
Dan Williams1077a572011-03-11 10:13:51 -080080static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
81 enum service_response response,
82 enum exec_status status)
Dan Williams50e7f9b2011-03-09 21:27:46 -080083
Dan Williams1077a572011-03-11 10:13:51 -080084{
85 enum isci_completion_selection disposition;
86
87 disposition = isci_perform_normal_io_completion;
88 disposition = isci_task_set_completion_status(task, response, status,
89 disposition);
Dan Williams50e7f9b2011-03-09 21:27:46 -080090
91 /* Tasks aborted specifically by a call to the lldd_abort_task
Dan Williams1077a572011-03-11 10:13:51 -080092 * function should not be completed to the host in the regular path.
93 */
94 switch (disposition) {
Dan Williams50e7f9b2011-03-09 21:27:46 -080095 case isci_perform_normal_io_completion:
96 /* Normal notification (task_done) */
Dan Williams1077a572011-03-11 10:13:51 -080097 dev_dbg(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -070098 "%s: Normal - task = %p, response=%d, "
99 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -0800100 __func__, task, response, status);
101
Dan Williams1077a572011-03-11 10:13:51 -0800102 task->lldd_task = NULL;
Dan Williams50e7f9b2011-03-09 21:27:46 -0800103
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700104 isci_execpath_callback(ihost, task, task->task_done);
Dan Williams50e7f9b2011-03-09 21:27:46 -0800105 break;
106
107 case isci_perform_aborted_io_completion:
108 /* No notification because this request is already in the
109 * abort path.
110 */
Dan Williams1077a572011-03-11 10:13:51 -0800111 dev_warn(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700112 "%s: Aborted - task = %p, response=%d, "
113 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -0800114 __func__, task, response, status);
115 break;
116
117 case isci_perform_error_io_completion:
118 /* Use sas_task_abort */
Dan Williams1077a572011-03-11 10:13:51 -0800119 dev_warn(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700120 "%s: Error - task = %p, response=%d, "
121 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -0800122 __func__, task, response, status);
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700123
124 isci_execpath_callback(ihost, task, sas_task_abort);
Dan Williams50e7f9b2011-03-09 21:27:46 -0800125 break;
126
127 default:
Dan Williams1077a572011-03-11 10:13:51 -0800128 dev_warn(&ihost->pdev->dev,
Dan Williams50e7f9b2011-03-09 21:27:46 -0800129 "%s: isci task notification default case!",
130 __func__);
131 sas_task_abort(task);
132 break;
133 }
134}
Dan Williams6f231dd2011-07-02 22:56:22 -0700135
Dan Williams1077a572011-03-11 10:13:51 -0800136#define for_each_sas_task(num, task) \
137 for (; num > 0; num--,\
138 task = list_entry(task->list.next, struct sas_task, list))
139
Dan Williams6f231dd2011-07-02 22:56:22 -0700140/**
141 * isci_task_execute_task() - This function is one of the SAS Domain Template
142 * functions. This function is called by libsas to send a task down to
143 * hardware.
144 * @task: This parameter specifies the SAS task to send.
145 * @num: This parameter specifies the number of tasks to queue.
146 * @gfp_flags: This parameter specifies the context of this call.
147 *
148 * status, zero indicates success.
149 */
150int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
151{
Dan Williams4393aa42011-03-31 13:10:44 -0700152 struct isci_host *ihost = dev_to_ihost(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700153 struct isci_request *request = NULL;
154 struct isci_remote_device *device;
155 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700156 int ret;
157 enum sci_status status;
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700158 enum isci_status device_status;
Dan Williams6f231dd2011-07-02 22:56:22 -0700159
Dan Williams1077a572011-03-11 10:13:51 -0800160 dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
Dan Williams6f231dd2011-07-02 22:56:22 -0700161
162 /* Check if we have room for more tasks */
Dan Williams1077a572011-03-11 10:13:51 -0800163 ret = isci_host_can_queue(ihost, num);
Dan Williams6f231dd2011-07-02 22:56:22 -0700164
165 if (ret) {
Dan Williams1077a572011-03-11 10:13:51 -0800166 dev_warn(&ihost->pdev->dev, "%s: queue full\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -0700167 return ret;
168 }
169
Dan Williams1077a572011-03-11 10:13:51 -0800170 for_each_sas_task(num, task) {
171 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700172 "task = %p, num = %d; dev = %p; cmd = %p\n",
173 task, num, task->dev, task->uldd_task);
174
Dan Williams4393aa42011-03-31 13:10:44 -0700175 device = task->dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700176
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700177 if (device)
178 device_status = device->status;
179 else
180 device_status = isci_freed;
181
182 /* From this point onward, any process that needs to guarantee
183 * that there is no kernel I/O being started will have to wait
184 * for the quiesce spinlock.
185 */
186
187 if (device_status != isci_ready_for_io) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700188
189 /* Forces a retry from scsi mid layer. */
Dave Jiangd2d61432011-04-21 05:36:23 +0000190 dev_dbg(&ihost->pdev->dev,
191 "%s: task %p: isci_host->status = %d, "
192 "device = %p; device_status = 0x%x\n\n",
193 __func__,
194 task,
195 isci_host_get_state(ihost),
196 device,
197 device_status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700198
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700199 if (device_status == isci_ready) {
200 /* Indicate QUEUE_FULL so that the scsi midlayer
201 * retries.
202 */
Dan Williams1077a572011-03-11 10:13:51 -0800203 isci_task_refuse(ihost, task,
204 SAS_TASK_COMPLETE,
205 SAS_QUEUE_FULL);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700206 } else {
207 /* Else, the device is going down. */
Dan Williams1077a572011-03-11 10:13:51 -0800208 isci_task_refuse(ihost, task,
209 SAS_TASK_UNDELIVERED,
210 SAS_DEVICE_UNKNOWN);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700211 }
Dan Williams1077a572011-03-11 10:13:51 -0800212 isci_host_can_dequeue(ihost, 1);
Dan Williams6f231dd2011-07-02 22:56:22 -0700213 } else {
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700214 /* There is a device and it's ready for I/O. */
215 spin_lock_irqsave(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700216
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700217 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
218
219 spin_unlock_irqrestore(&task->task_state_lock,
220 flags);
221
Dan Williams1077a572011-03-11 10:13:51 -0800222 isci_task_refuse(ihost, task,
223 SAS_TASK_UNDELIVERED,
224 SAM_STAT_TASK_ABORTED);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700225
226 /* The I/O was aborted. */
227
228 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700229 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
230 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700231
232 /* build and send the request. */
Dan Williams1077a572011-03-11 10:13:51 -0800233 status = isci_request_execute(ihost, task, &request,
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700234 gfp_flags);
235
236 if (status != SCI_SUCCESS) {
237
238 spin_lock_irqsave(&task->task_state_lock, flags);
239 /* Did not really start this command. */
240 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
241 spin_unlock_irqrestore(&task->task_state_lock, flags);
242
243 /* Indicate QUEUE_FULL so that the scsi
244 * midlayer retries. if the request
245 * failed for remote device reasons,
246 * it gets returned as
247 * SAS_TASK_UNDELIVERED next time
248 * through.
249 */
Dan Williams1077a572011-03-11 10:13:51 -0800250 isci_task_refuse(ihost, task,
251 SAS_TASK_COMPLETE,
252 SAS_QUEUE_FULL);
253 isci_host_can_dequeue(ihost, 1);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700254 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700255 }
256 }
Dan Williams1077a572011-03-11 10:13:51 -0800257 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700258 return 0;
259}
260
261
262
263/**
264 * isci_task_request_build() - This function builds the task request object.
265 * @isci_host: This parameter specifies the ISCI host object
266 * @request: This parameter points to the isci_request object allocated in the
267 * request construct function.
268 * @tmf: This parameter is the task management struct to be built
269 *
270 * SCI_SUCCESS on successfull completion, or specific failure code.
271 */
272static enum sci_status isci_task_request_build(
273 struct isci_host *isci_host,
274 struct isci_request **isci_request,
275 struct isci_tmf *isci_tmf)
276{
277 struct scic_sds_remote_device *sci_device;
278 enum sci_status status = SCI_FAILURE;
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700279 struct isci_request *request = NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700280 struct isci_remote_device *isci_device;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700281 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700282
283 dev_dbg(&isci_host->pdev->dev,
284 "%s: isci_tmf = %p\n", __func__, isci_tmf);
285
286 isci_device = isci_tmf->device;
Dan Williams57f20f42011-04-21 18:14:45 -0700287 sci_device = &isci_device->sci;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700288 dev = isci_device->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700289
290 /* do common allocation and init of request object. */
291 status = isci_request_alloc_tmf(
292 isci_host,
293 isci_tmf,
294 &request,
295 isci_device,
296 GFP_ATOMIC
297 );
298
299 if (status != SCI_SUCCESS)
300 goto out;
301
302 /* let the core do it's construct. */
Dan Williams0d843662011-05-08 01:56:57 -0700303 status = scic_task_request_construct(&isci_host->sci, sci_device,
304 SCI_CONTROLLER_INVALID_IO_TAG,
Dan Williams67ea8382011-05-08 11:47:15 -0700305 &request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700306
307 if (status != SCI_SUCCESS) {
308 dev_warn(&isci_host->pdev->dev,
309 "%s: scic_task_request_construct failed - "
310 "status = 0x%x\n",
311 __func__,
312 status);
313 goto errout;
314 }
315
Dan Williamsa1a113b2011-04-21 18:44:45 -0700316 /* XXX convert to get this from task->tproto like other drivers */
317 if (dev->dev_type == SAS_END_DEV) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700318 isci_tmf->proto = SAS_PROTOCOL_SSP;
Dan Williams67ea8382011-05-08 11:47:15 -0700319 status = scic_task_request_construct_ssp(&request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700320 if (status != SCI_SUCCESS)
321 goto errout;
322 }
323
Dan Williamsa1a113b2011-04-21 18:44:45 -0700324 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700325 isci_tmf->proto = SAS_PROTOCOL_SATA;
326 status = isci_sata_management_task_request_build(request);
327
328 if (status != SCI_SUCCESS)
329 goto errout;
330 }
331
332 goto out;
333
334 errout:
335
336 /* release the dma memory if we fail. */
337 isci_request_free(isci_host, request);
338 request = NULL;
339
340 out:
341 *isci_request = request;
342 return status;
343}
344
345/**
346 * isci_tmf_timeout_cb() - This function is called as a kernel callback when
347 * the timeout period for the TMF has expired.
348 *
349 *
350 */
351static void isci_tmf_timeout_cb(void *tmf_request_arg)
352{
353 struct isci_request *request = (struct isci_request *)tmf_request_arg;
354 struct isci_tmf *tmf = isci_request_access_tmf(request);
355 enum sci_status status;
356
Dan Williams6f231dd2011-07-02 22:56:22 -0700357 /* This task management request has timed-out. Terminate the request
358 * so that the request eventually completes to the requestor in the
359 * request completion callback path.
360 */
361 /* Note - the timer callback function itself has provided spinlock
362 * exclusion from the start and completion paths. No need to take
363 * the request->isci_host->scic_lock here.
364 */
365
366 if (tmf->timeout_timer != NULL) {
367 /* Call the users callback, if any. */
368 if (tmf->cb_state_func != NULL)
369 tmf->cb_state_func(isci_tmf_timed_out, tmf,
370 tmf->cb_data);
371
372 /* Terminate the TMF transmit request. */
373 status = scic_controller_terminate_request(
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000374 &request->isci_host->sci,
Dan Williams57f20f42011-04-21 18:14:45 -0700375 &request->isci_device->sci,
Dan Williams67ea8382011-05-08 11:47:15 -0700376 &request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700377
378 dev_dbg(&request->isci_host->pdev->dev,
379 "%s: tmf_request = %p; tmf = %p; status = %d\n",
380 __func__, request, tmf, status);
381 } else
382 dev_dbg(&request->isci_host->pdev->dev,
383 "%s: timer already canceled! "
384 "tmf_request = %p; tmf = %p\n",
385 __func__, request, tmf);
386
387 /* No need to unlock since the caller to this callback is doing it for
388 * us.
389 * request->isci_host->scic_lock
390 */
391}
392
393/**
394 * isci_task_execute_tmf() - This function builds and sends a task request,
395 * then waits for the completion.
396 * @isci_host: This parameter specifies the ISCI host object
397 * @tmf: This parameter is the pointer to the task management structure for
398 * this request.
399 * @timeout_ms: This parameter specifies the timeout period for the task
400 * management request.
401 *
402 * TMF_RESP_FUNC_COMPLETE on successful completion of the TMF (this includes
403 * error conditions reported in the IU status), or TMF_RESP_FUNC_FAILED.
404 */
405int isci_task_execute_tmf(
406 struct isci_host *isci_host,
407 struct isci_tmf *tmf,
408 unsigned long timeout_ms)
409{
410 DECLARE_COMPLETION_ONSTACK(completion);
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700411 enum sci_task_status status = SCI_TASK_FAILURE;
Dan Williams6f231dd2011-07-02 22:56:22 -0700412 struct scic_sds_remote_device *sci_device;
413 struct isci_remote_device *isci_device = tmf->device;
414 struct isci_request *request;
415 int ret = TMF_RESP_FUNC_FAILED;
416 unsigned long flags;
417
418 /* sanity check, return TMF_RESP_FUNC_FAILED
419 * if the device is not there and ready.
420 */
Dan Williams8acaec12011-03-07 14:47:35 -0800421 if (!isci_device || isci_device->status != isci_ready_for_io) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700422 dev_dbg(&isci_host->pdev->dev,
423 "%s: isci_device = %p not ready (%d)\n",
424 __func__,
Dan Williams8acaec12011-03-07 14:47:35 -0800425 isci_device, isci_device->status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700426 return TMF_RESP_FUNC_FAILED;
427 } else
428 dev_dbg(&isci_host->pdev->dev,
429 "%s: isci_device = %p\n",
430 __func__, isci_device);
431
Dan Williams57f20f42011-04-21 18:14:45 -0700432 sci_device = &isci_device->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700433
434 /* Assign the pointer to the TMF's completion kernel wait structure. */
435 tmf->complete = &completion;
436
437 isci_task_request_build(
438 isci_host,
439 &request,
440 tmf
441 );
442
443 if (!request) {
444 dev_warn(&isci_host->pdev->dev,
445 "%s: isci_task_request_build failed\n",
446 __func__);
447 return TMF_RESP_FUNC_FAILED;
448 }
449
450 /* Allocate the TMF timeout timer. */
Dan Williams6f231dd2011-07-02 22:56:22 -0700451 spin_lock_irqsave(&isci_host->scic_lock, flags);
Dan Williams7c40a802011-03-02 11:49:26 -0800452 tmf->timeout_timer = isci_timer_create(isci_host, request, isci_tmf_timeout_cb);
Dan Williams6f231dd2011-07-02 22:56:22 -0700453
454 /* Start the timer. */
455 if (tmf->timeout_timer)
456 isci_timer_start(tmf->timeout_timer, timeout_ms);
457 else
458 dev_warn(&isci_host->pdev->dev,
459 "%s: isci_timer_create failed!!!!\n",
460 __func__);
461
462 /* start the TMF io. */
463 status = scic_controller_start_task(
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000464 &isci_host->sci,
Dan Williams6f231dd2011-07-02 22:56:22 -0700465 sci_device,
Dan Williams67ea8382011-05-08 11:47:15 -0700466 &request->sci,
467 SCI_CONTROLLER_INVALID_IO_TAG);
Dan Williams6f231dd2011-07-02 22:56:22 -0700468
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700469 if (status != SCI_TASK_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700470 dev_warn(&isci_host->pdev->dev,
471 "%s: start_io failed - status = 0x%x, request = %p\n",
472 __func__,
473 status,
474 request);
475 goto cleanup_request;
476 }
477
478 /* Call the users callback, if any. */
479 if (tmf->cb_state_func != NULL)
480 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
481
482 /* Change the state of the TMF-bearing request to "started". */
483 isci_request_change_state(request, started);
484
485 /* add the request to the remote device request list. */
486 list_add(&request->dev_node, &isci_device->reqs_in_process);
487
488 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
489
490 /* Wait for the TMF to complete, or a timeout. */
491 wait_for_completion(&completion);
492
493 isci_print_tmf(tmf);
494
495 if (tmf->status == SCI_SUCCESS)
496 ret = TMF_RESP_FUNC_COMPLETE;
497 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
498 dev_dbg(&isci_host->pdev->dev,
499 "%s: tmf.status == "
500 "SCI_FAILURE_IO_RESPONSE_VALID\n",
501 __func__);
502 ret = TMF_RESP_FUNC_COMPLETE;
503 }
504 /* Else - leave the default "failed" status alone. */
505
506 dev_dbg(&isci_host->pdev->dev,
507 "%s: completed request = %p\n",
508 __func__,
509 request);
510
511 if (request->io_request_completion != NULL) {
512
513 /* The fact that this is non-NULL for a TMF request
514 * means there is a thread waiting for this TMF to
515 * finish.
516 */
517 complete(request->io_request_completion);
518 }
519
520 spin_lock_irqsave(&isci_host->scic_lock, flags);
521
522 cleanup_request:
523
524 /* Clean up the timer if needed. */
525 if (tmf->timeout_timer) {
Dan Williams7c40a802011-03-02 11:49:26 -0800526 isci_del_timer(isci_host, tmf->timeout_timer);
Dan Williams6f231dd2011-07-02 22:56:22 -0700527 tmf->timeout_timer = NULL;
528 }
529
530 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
531
532 isci_request_free(isci_host, request);
533
534 return ret;
535}
536
537void isci_task_build_tmf(
538 struct isci_tmf *tmf,
539 struct isci_remote_device *isci_device,
540 enum isci_tmf_function_codes code,
541 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
542 struct isci_tmf *,
543 void *),
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800544 void *cb_data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700545{
546 dev_dbg(&isci_device->isci_port->isci_host->pdev->dev,
547 "%s: isci_device = %p\n", __func__, isci_device);
548
549 memset(tmf, 0, sizeof(*tmf));
550
551 tmf->device = isci_device;
552 tmf->tmf_code = code;
553 tmf->timeout_timer = NULL;
554 tmf->cb_state_func = tmf_sent_cb;
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800555 tmf->cb_data = cb_data;
556}
Jeff Skirvin1fad9e92011-03-04 14:06:46 -0800557
Dan Williams35173d52011-03-26 16:43:01 -0700558static void isci_task_build_abort_task_tmf(
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800559 struct isci_tmf *tmf,
560 struct isci_remote_device *isci_device,
561 enum isci_tmf_function_codes code,
562 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
563 struct isci_tmf *,
564 void *),
565 struct isci_request *old_request)
566{
567 isci_task_build_tmf(tmf, isci_device, code, tmf_sent_cb,
568 (void *)old_request);
569 tmf->io_tag = old_request->io_tag;
Dan Williams6f231dd2011-07-02 22:56:22 -0700570}
571
572static struct isci_request *isci_task_get_request_from_task(
573 struct sas_task *task,
Dan Williams6f231dd2011-07-02 22:56:22 -0700574 struct isci_remote_device **isci_device)
575{
576
577 struct isci_request *request = NULL;
578 unsigned long flags;
579
580 spin_lock_irqsave(&task->task_state_lock, flags);
581
582 request = task->lldd_task;
583
584 /* If task is already done, the request isn't valid */
585 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
586 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
587 (request != NULL)) {
588
Dan Williams6f231dd2011-07-02 22:56:22 -0700589 if (isci_device != NULL)
590 *isci_device = request->isci_device;
591 }
592
593 spin_unlock_irqrestore(&task->task_state_lock, flags);
594
595 return request;
596}
597
598/**
599 * isci_task_validate_request_to_abort() - This function checks the given I/O
600 * against the "started" state. If the request is still "started", it's
601 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
602 * BEFORE CALLING THIS FUNCTION.
603 * @isci_request: This parameter specifies the request object to control.
604 * @isci_host: This parameter specifies the ISCI host object
605 * @isci_device: This is the device to which the request is pending.
606 * @aborted_io_completion: This is a completion structure that will be added to
607 * the request in case it is changed to aborting; this completion is
608 * triggered when the request is fully completed.
609 *
610 * Either "started" on successful change of the task status to "aborted", or
611 * "unallocated" if the task cannot be controlled.
612 */
613static enum isci_request_status isci_task_validate_request_to_abort(
614 struct isci_request *isci_request,
615 struct isci_host *isci_host,
616 struct isci_remote_device *isci_device,
617 struct completion *aborted_io_completion)
618{
619 enum isci_request_status old_state = unallocated;
620
621 /* Only abort the task if it's in the
622 * device's request_in_process list
623 */
624 if (isci_request && !list_empty(&isci_request->dev_node)) {
625 old_state = isci_request_change_started_to_aborted(
626 isci_request, aborted_io_completion);
627
Dan Williams6f231dd2011-07-02 22:56:22 -0700628 }
629
630 return old_state;
631}
632
633static void isci_request_cleanup_completed_loiterer(
634 struct isci_host *isci_host,
635 struct isci_remote_device *isci_device,
636 struct isci_request *isci_request)
637{
Jeff Skirvin18d3d722011-03-04 14:06:38 -0800638 struct sas_task *task;
639 unsigned long flags;
640
641 task = (isci_request->ttype == io_task)
642 ? isci_request_access_task(isci_request)
643 : NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700644
645 dev_dbg(&isci_host->pdev->dev,
646 "%s: isci_device=%p, request=%p, task=%p\n",
Jeff Skirvin18d3d722011-03-04 14:06:38 -0800647 __func__, isci_device, isci_request, task);
Dan Williams6f231dd2011-07-02 22:56:22 -0700648
649 spin_lock_irqsave(&isci_host->scic_lock, flags);
650 list_del_init(&isci_request->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -0700651 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
652
Jeff Skirvina5fde222011-03-04 14:06:42 -0800653 if (task != NULL) {
654
655 spin_lock_irqsave(&task->task_state_lock, flags);
656 task->lldd_task = NULL;
657
658 isci_set_task_doneflags(task);
659
660 /* If this task is not in the abort path, call task_done. */
661 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
662
663 spin_unlock_irqrestore(&task->task_state_lock, flags);
664 task->task_done(task);
665 } else
666 spin_unlock_irqrestore(&task->task_state_lock, flags);
667 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700668 isci_request_free(isci_host, isci_request);
669}
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800670
671/**
672* @isci_termination_timed_out(): this function will deal with a request for
673* which the wait for termination has timed-out.
674*
675* @isci_host This SCU.
676* @isci_request The I/O request being terminated.
677*/
678static void
679isci_termination_timed_out(
680 struct isci_host * host,
681 struct isci_request * request
682 )
683{
684 unsigned long state_flags;
685
686 dev_warn(&host->pdev->dev,
687 "%s: host = %p; request = %p\n",
688 __func__, host, request);
689
690 /* At this point, the request to terminate
691 * has timed out. The best we can do is to
692 * have the request die a silent death
693 * if it ever completes.
694 */
695 spin_lock_irqsave(&request->state_lock, state_flags);
696
697 if (request->status == started) {
698
699 /* Set the request state to "dead",
700 * and clear the task pointer so that an actual
701 * completion event callback doesn't do
702 * anything.
703 */
704 request->status = dead;
705
706 /* Clear the timeout completion event pointer.*/
707 request->io_request_completion = NULL;
708
709 if (request->ttype == io_task) {
710
711 /* Break links with the sas_task. */
712 if (request->ttype_ptr.io_task_ptr != NULL) {
713
714 request->ttype_ptr.io_task_ptr->lldd_task = NULL;
715 request->ttype_ptr.io_task_ptr = NULL;
716 }
717 }
718 }
719 spin_unlock_irqrestore(&request->state_lock, state_flags);
720}
721
722
Dan Williams6f231dd2011-07-02 22:56:22 -0700723/**
724 * isci_terminate_request_core() - This function will terminate the given
725 * request, and wait for it to complete. This function must only be called
726 * from a thread that can wait. Note that the request is terminated and
727 * completed (back to the host, if started there).
728 * @isci_host: This SCU.
729 * @isci_device: The target.
730 * @isci_request: The I/O request to be terminated.
731 *
732 *
733 */
734static void isci_terminate_request_core(
735 struct isci_host *isci_host,
736 struct isci_remote_device *isci_device,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800737 struct isci_request *isci_request)
Dan Williams6f231dd2011-07-02 22:56:22 -0700738{
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800739 enum sci_status status = SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700740 bool was_terminated = false;
741 bool needs_cleanup_handling = false;
742 enum isci_request_status request_status;
743 unsigned long flags;
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800744 unsigned long timeout_remaining;
745
Dan Williams6f231dd2011-07-02 22:56:22 -0700746
747 dev_dbg(&isci_host->pdev->dev,
748 "%s: device = %p; request = %p\n",
749 __func__, isci_device, isci_request);
750
Dan Williams6f231dd2011-07-02 22:56:22 -0700751 spin_lock_irqsave(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800752
753 /* Note that we are not going to control
754 * the target to abort the request.
755 */
756 isci_request->complete_in_target = true;
757
Dan Williams6f231dd2011-07-02 22:56:22 -0700758 /* Make sure the request wasn't just sitting around signalling
759 * device condition (if the request handle is NULL, then the
760 * request completed but needed additional handling here).
761 */
Dan Williams67ea8382011-05-08 11:47:15 -0700762 if (!isci_request->terminated) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700763 was_terminated = true;
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800764 needs_cleanup_handling = true;
Dan Williams6f231dd2011-07-02 22:56:22 -0700765 status = scic_controller_terminate_request(
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000766 &isci_host->sci,
Dan Williams57f20f42011-04-21 18:14:45 -0700767 &isci_device->sci,
Dan Williams67ea8382011-05-08 11:47:15 -0700768 &isci_request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700769 }
770 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
771
772 /*
773 * The only time the request to terminate will
774 * fail is when the io request is completed and
775 * being aborted.
776 */
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800777 if (status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700778 dev_err(&isci_host->pdev->dev,
779 "%s: scic_controller_terminate_request"
780 " returned = 0x%x\n",
781 __func__,
782 status);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800783 /* Clear the completion pointer from the request. */
784 isci_request->io_request_completion = NULL;
785
786 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700787 if (was_terminated) {
788 dev_dbg(&isci_host->pdev->dev,
789 "%s: before completion wait (%p)\n",
790 __func__,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800791 isci_request->io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700792
793 /* Wait here for the request to complete. */
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800794 #define TERMINATION_TIMEOUT_MSEC 50
795 timeout_remaining
796 = wait_for_completion_timeout(
797 isci_request->io_request_completion,
798 msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
Dan Williams6f231dd2011-07-02 22:56:22 -0700799
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800800 if (!timeout_remaining) {
801
802 isci_termination_timed_out(isci_host,
803 isci_request);
804
805 dev_err(&isci_host->pdev->dev,
806 "%s: *** Timeout waiting for "
807 "termination(%p/%p)\n",
808 __func__,
809 isci_request->io_request_completion,
810 isci_request);
811
812 } else
813 dev_dbg(&isci_host->pdev->dev,
814 "%s: after completion wait (%p)\n",
815 __func__,
816 isci_request->io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700817 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800818 /* Clear the completion pointer from the request. */
819 isci_request->io_request_completion = NULL;
820
821 /* Peek at the status of the request. This will tell
822 * us if there was special handling on the request such that it
823 * needs to be detached and freed here.
824 */
825 spin_lock_irqsave(&isci_request->state_lock, flags);
826 request_status = isci_request_get_state(isci_request);
827
828 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
829 && ((request_status == aborted)
830 || (request_status == aborting)
831 || (request_status == terminating)
832 || (request_status == completed)
833 || (request_status == dead)
834 )
835 ) {
836
837 /* The completion routine won't free a request in
838 * the aborted/aborting/etc. states, so we do
839 * it here.
840 */
841 needs_cleanup_handling = true;
842 }
843 spin_unlock_irqrestore(&isci_request->state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700844
845 if (needs_cleanup_handling)
846 isci_request_cleanup_completed_loiterer(
847 isci_host, isci_device, isci_request
848 );
849 }
850}
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800851
Dan Williams6f231dd2011-07-02 22:56:22 -0700852static void isci_terminate_request(
853 struct isci_host *isci_host,
854 struct isci_remote_device *isci_device,
855 struct isci_request *isci_request,
856 enum isci_request_status new_request_state)
857{
858 enum isci_request_status old_state;
Dan Williams6f231dd2011-07-02 22:56:22 -0700859 DECLARE_COMPLETION_ONSTACK(request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700860
861 /* Change state to "new_request_state" if it is currently "started" */
862 old_state = isci_request_change_started_to_newstate(
863 isci_request,
864 &request_completion,
865 new_request_state
866 );
867
Jeff Skirvinf219f012011-03-31 13:10:34 -0700868 if ((old_state == started) ||
869 (old_state == completed) ||
870 (old_state == aborting)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700871
Jeff Skirvina5fde222011-03-04 14:06:42 -0800872 /* If the old_state is started:
873 * This request was not already being aborted. If it had been,
Dan Williams6f231dd2011-07-02 22:56:22 -0700874 * then the aborting I/O (ie. the TMF request) would not be in
875 * the aborting state, and thus would be terminated here. Note
876 * that since the TMF completion's call to the kernel function
877 * "complete()" does not happen until the pending I/O request
878 * terminate fully completes, we do not have to implement a
879 * special wait here for already aborting requests - the
880 * termination of the TMF request will force the request
881 * to finish it's already started terminate.
Jeff Skirvina5fde222011-03-04 14:06:42 -0800882 *
883 * If old_state == completed:
884 * This request completed from the SCU hardware perspective
885 * and now just needs cleaning up in terms of freeing the
886 * request and potentially calling up to libsas.
Jeff Skirvinf219f012011-03-31 13:10:34 -0700887 *
888 * If old_state == aborting:
889 * This request has already gone through a TMF timeout, but may
890 * not have been terminated; needs cleaning up at least.
Dan Williams6f231dd2011-07-02 22:56:22 -0700891 */
892 isci_terminate_request_core(isci_host, isci_device,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800893 isci_request);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800894 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700895}
896
897/**
898 * isci_terminate_pending_requests() - This function will change the all of the
899 * requests on the given device's state to "aborting", will terminate the
900 * requests, and wait for them to complete. This function must only be
901 * called from a thread that can wait. Note that the requests are all
902 * terminated and completed (back to the host, if started there).
903 * @isci_host: This parameter specifies SCU.
904 * @isci_device: This parameter specifies the target.
905 *
906 *
907 */
908void isci_terminate_pending_requests(
909 struct isci_host *isci_host,
910 struct isci_remote_device *isci_device,
911 enum isci_request_status new_request_state)
912{
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800913 struct isci_request *request;
914 struct isci_request *next_request;
915 unsigned long flags;
916 struct list_head aborted_request_list;
917
918 INIT_LIST_HEAD(&aborted_request_list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700919
920 dev_dbg(&isci_host->pdev->dev,
921 "%s: isci_device = %p (new request state = %d)\n",
922 __func__, isci_device, new_request_state);
923
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800924 spin_lock_irqsave(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700925
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800926 /* Move all of the pending requests off of the device list. */
927 list_splice_init(&isci_device->reqs_in_process,
928 &aborted_request_list);
929
930 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
931
932 /* Iterate through the now-local list. */
933 list_for_each_entry_safe(request, next_request,
934 &aborted_request_list, dev_node) {
935
936 dev_warn(&isci_host->pdev->dev,
937 "%s: isci_device=%p request=%p; task=%p\n",
938 __func__,
939 isci_device, request,
940 ((request->ttype == io_task)
941 ? isci_request_access_task(request)
942 : NULL));
943
944 /* Mark all still pending I/O with the selected next
945 * state, terminate and free it.
946 */
947 isci_terminate_request(isci_host, isci_device,
948 request, new_request_state
949 );
Dan Williams6f231dd2011-07-02 22:56:22 -0700950 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700951}
952
953/**
954 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
955 * Template functions.
956 * @lun: This parameter specifies the lun to be reset.
957 *
958 * status, zero indicates success.
959 */
960static int isci_task_send_lu_reset_sas(
961 struct isci_host *isci_host,
962 struct isci_remote_device *isci_device,
963 u8 *lun)
964{
965 struct isci_tmf tmf;
966 int ret = TMF_RESP_FUNC_FAILED;
967
968 dev_dbg(&isci_host->pdev->dev,
969 "%s: isci_host = %p, isci_device = %p\n",
970 __func__, isci_host, isci_device);
971 /* Send the LUN reset to the target. By the time the call returns,
972 * the TMF has fully exected in the target (in which case the return
973 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
974 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
975 */
976 isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_lun_reset, NULL,
977 NULL);
978
979 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
980 ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
981
982 if (ret == TMF_RESP_FUNC_COMPLETE)
983 dev_dbg(&isci_host->pdev->dev,
984 "%s: %p: TMF_LU_RESET passed\n",
985 __func__, isci_device);
986 else
987 dev_dbg(&isci_host->pdev->dev,
988 "%s: %p: TMF_LU_RESET failed (%x)\n",
989 __func__, isci_device, ret);
990
991 return ret;
992}
993
994/**
995 * isci_task_lu_reset() - This function is one of the SAS Domain Template
996 * functions. This is one of the Task Management functoins called by libsas,
997 * to reset the given lun. Note the assumption that while this call is
998 * executing, no I/O will be sent by the host to the device.
999 * @lun: This parameter specifies the lun to be reset.
1000 *
1001 * status, zero indicates success.
1002 */
Dan Williams4393aa42011-03-31 13:10:44 -07001003int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
Dan Williams6f231dd2011-07-02 22:56:22 -07001004{
Dan Williams4393aa42011-03-31 13:10:44 -07001005 struct isci_host *isci_host = dev_to_ihost(domain_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001006 struct isci_remote_device *isci_device = NULL;
1007 int ret;
1008 bool device_stopping = false;
1009
Dan Williams4393aa42011-03-31 13:10:44 -07001010 isci_device = domain_device->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001011
Dan Williams4393aa42011-03-31 13:10:44 -07001012 dev_dbg(&isci_host->pdev->dev,
1013 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07001014 __func__, domain_device, isci_host, isci_device);
1015
Dan Williamsd06b4872011-05-02 13:59:25 -07001016 if (isci_device != NULL) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001017 device_stopping = (isci_device->status == isci_stopping)
1018 || (isci_device->status == isci_stopped);
Dan Williamsd06b4872011-05-02 13:59:25 -07001019 set_bit(IDEV_EH, &isci_device->flags);
1020 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001021
1022 /* If there is a device reset pending on any request in the
1023 * device's list, fail this LUN reset request in order to
1024 * escalate to the device reset.
1025 */
Dan Williams4393aa42011-03-31 13:10:44 -07001026 if (!isci_device || device_stopping ||
1027 isci_device_is_reset_pending(isci_host, isci_device)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001028 dev_warn(&isci_host->pdev->dev,
Dan Williams4393aa42011-03-31 13:10:44 -07001029 "%s: No dev (%p), or "
Dan Williams6f231dd2011-07-02 22:56:22 -07001030 "RESET PENDING: domain_device=%p\n",
Dan Williams4393aa42011-03-31 13:10:44 -07001031 __func__, isci_device, domain_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001032 return TMF_RESP_FUNC_FAILED;
1033 }
1034
Dan Williams6f231dd2011-07-02 22:56:22 -07001035 /* Send the task management part of the reset. */
1036 if (sas_protocol_ata(domain_device->tproto)) {
Dan Williams4393aa42011-03-31 13:10:44 -07001037 ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
Dan Williams6f231dd2011-07-02 22:56:22 -07001038 } else
1039 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
1040
1041 /* If the LUN reset worked, all the I/O can now be terminated. */
1042 if (ret == TMF_RESP_FUNC_COMPLETE)
1043 /* Terminate all I/O now. */
1044 isci_terminate_pending_requests(isci_host,
1045 isci_device,
1046 terminating);
1047
Dan Williams6f231dd2011-07-02 22:56:22 -07001048 return ret;
1049}
1050
1051
1052/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
1053int isci_task_clear_nexus_port(struct asd_sas_port *port)
1054{
1055 return TMF_RESP_FUNC_FAILED;
1056}
1057
1058
1059
1060int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
1061{
1062 return TMF_RESP_FUNC_FAILED;
1063}
1064
Dan Williams6f231dd2011-07-02 22:56:22 -07001065/* Task Management Functions. Must be called from process context. */
1066
1067/**
1068 * isci_abort_task_process_cb() - This is a helper function for the abort task
1069 * TMF command. It manages the request state with respect to the successful
1070 * transmission / completion of the abort task request.
1071 * @cb_state: This parameter specifies when this function was called - after
1072 * the TMF request has been started and after it has timed-out.
1073 * @tmf: This parameter specifies the TMF in progress.
1074 *
1075 *
1076 */
1077static void isci_abort_task_process_cb(
1078 enum isci_tmf_cb_state cb_state,
1079 struct isci_tmf *tmf,
1080 void *cb_data)
1081{
1082 struct isci_request *old_request;
1083
1084 old_request = (struct isci_request *)cb_data;
1085
1086 dev_dbg(&old_request->isci_host->pdev->dev,
1087 "%s: tmf=%p, old_request=%p\n",
1088 __func__, tmf, old_request);
1089
1090 switch (cb_state) {
1091
1092 case isci_tmf_started:
1093 /* The TMF has been started. Nothing to do here, since the
1094 * request state was already set to "aborted" by the abort
1095 * task function.
1096 */
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001097 if ((old_request->status != aborted)
1098 && (old_request->status != completed))
1099 dev_err(&old_request->isci_host->pdev->dev,
1100 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
1101 __func__, old_request->status, tmf, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001102 break;
1103
1104 case isci_tmf_timed_out:
1105
1106 /* Set the task's state to "aborting", since the abort task
1107 * function thread set it to "aborted" (above) in anticipation
1108 * of the task management request working correctly. Since the
1109 * timeout has now fired, the TMF request failed. We set the
1110 * state such that the request completion will indicate the
1111 * device is no longer present.
1112 */
1113 isci_request_change_state(old_request, aborting);
1114 break;
1115
1116 default:
1117 dev_err(&old_request->isci_host->pdev->dev,
1118 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
1119 __func__, cb_state, tmf, old_request);
1120 break;
1121 }
1122}
1123
1124/**
1125 * isci_task_abort_task() - This function is one of the SAS Domain Template
1126 * functions. This function is called by libsas to abort a specified task.
1127 * @task: This parameter specifies the SAS task to abort.
1128 *
1129 * status, zero indicates success.
1130 */
1131int isci_task_abort_task(struct sas_task *task)
1132{
Dan Williams4393aa42011-03-31 13:10:44 -07001133 struct isci_host *isci_host = dev_to_ihost(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001134 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001135 struct isci_request *old_request = NULL;
1136 enum isci_request_status old_state;
Dan Williams6f231dd2011-07-02 22:56:22 -07001137 struct isci_remote_device *isci_device = NULL;
Jeff Skirvina5fde222011-03-04 14:06:42 -08001138 struct isci_tmf tmf;
1139 int ret = TMF_RESP_FUNC_FAILED;
1140 unsigned long flags;
1141 bool any_dev_reset = false;
1142 bool device_stopping;
Dan Williams6f231dd2011-07-02 22:56:22 -07001143
1144 /* Get the isci_request reference from the task. Note that
1145 * this check does not depend on the pending request list
1146 * in the device, because tasks driving resets may land here
1147 * after completion in the core.
1148 */
Dan Williams4393aa42011-03-31 13:10:44 -07001149 old_request = isci_task_get_request_from_task(task, &isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001150
1151 dev_dbg(&isci_host->pdev->dev,
1152 "%s: task = %p\n", __func__, task);
1153
1154 /* Check if the device has been / is currently being removed.
1155 * If so, no task management will be done, and the I/O will
1156 * be terminated.
1157 */
1158 device_stopping = (isci_device->status == isci_stopping)
1159 || (isci_device->status == isci_stopped);
1160
Dan Williamsd06b4872011-05-02 13:59:25 -07001161 /* XXX need to fix device lookup lifetime (needs to be done
1162 * under scic_lock, among other things...), but for now assume
1163 * the device is available like the above code
1164 */
1165 set_bit(IDEV_EH, &isci_device->flags);
1166
Dan Williams6f231dd2011-07-02 22:56:22 -07001167 /* This version of the driver will fail abort requests for
1168 * SATA/STP. Failing the abort request this way will cause the
1169 * SCSI error handler thread to escalate to LUN reset
1170 */
1171 if (sas_protocol_ata(task->task_proto) && !device_stopping) {
1172 dev_warn(&isci_host->pdev->dev,
1173 " task %p is for a STP/SATA device;"
1174 " returning TMF_RESP_FUNC_FAILED\n"
1175 " to cause a LUN reset...\n", task);
1176 return TMF_RESP_FUNC_FAILED;
1177 }
1178
1179 dev_dbg(&isci_host->pdev->dev,
1180 "%s: old_request == %p\n", __func__, old_request);
1181
Jeff Skirvina5fde222011-03-04 14:06:42 -08001182 if (!device_stopping)
1183 any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
1184
Dan Williams6f231dd2011-07-02 22:56:22 -07001185 spin_lock_irqsave(&task->task_state_lock, flags);
1186
1187 /* Don't do resets to stopping devices. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001188 if (device_stopping) {
1189
Dan Williams6f231dd2011-07-02 22:56:22 -07001190 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
Jeff Skirvina5fde222011-03-04 14:06:42 -08001191 any_dev_reset = false;
Dan Williams6f231dd2011-07-02 22:56:22 -07001192
Jeff Skirvina5fde222011-03-04 14:06:42 -08001193 } else /* See if there is a pending device reset for this device. */
Dan Williams6f231dd2011-07-02 22:56:22 -07001194 any_dev_reset = any_dev_reset
Jeff Skirvina5fde222011-03-04 14:06:42 -08001195 || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
Dan Williams6f231dd2011-07-02 22:56:22 -07001196
1197 /* If the extraction of the request reference from the task
1198 * failed, then the request has been completed (or if there is a
1199 * pending reset then this abort request function must be failed
1200 * in order to escalate to the target reset).
1201 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001202 if ((old_request == NULL) || any_dev_reset) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001203
1204 /* If the device reset task flag is set, fail the task
1205 * management request. Otherwise, the original request
1206 * has completed.
1207 */
1208 if (any_dev_reset) {
1209
1210 /* Turn off the task's DONE to make sure this
1211 * task is escalated to a target reset.
1212 */
1213 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1214
Jeff Skirvina5fde222011-03-04 14:06:42 -08001215 /* Make the reset happen as soon as possible. */
1216 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1217
1218 spin_unlock_irqrestore(&task->task_state_lock, flags);
1219
Dan Williams6f231dd2011-07-02 22:56:22 -07001220 /* Fail the task management request in order to
1221 * escalate to the target reset.
1222 */
1223 ret = TMF_RESP_FUNC_FAILED;
1224
1225 dev_dbg(&isci_host->pdev->dev,
1226 "%s: Failing task abort in order to "
1227 "escalate to target reset because\n"
1228 "SAS_TASK_NEED_DEV_RESET is set for "
1229 "task %p on dev %p\n",
1230 __func__, task, isci_device);
1231
Jeff Skirvina5fde222011-03-04 14:06:42 -08001232
Dan Williams6f231dd2011-07-02 22:56:22 -07001233 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -07001234 /* The request has already completed and there
1235 * is nothing to do here other than to set the task
1236 * done bit, and indicate that the task abort function
1237 * was sucessful.
1238 */
1239 isci_set_task_doneflags(task);
1240
Jeff Skirvina5fde222011-03-04 14:06:42 -08001241 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001242
Jeff Skirvina5fde222011-03-04 14:06:42 -08001243 ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001244
Jeff Skirvina5fde222011-03-04 14:06:42 -08001245 dev_dbg(&isci_host->pdev->dev,
1246 "%s: abort task not needed for %p\n",
1247 __func__, task);
Dan Williams6f231dd2011-07-02 22:56:22 -07001248 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001249
1250 return ret;
1251 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001252 else
1253 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001254
1255 spin_lock_irqsave(&isci_host->scic_lock, flags);
1256
Jeff Skirvinf219f012011-03-31 13:10:34 -07001257 /* Check the request status and change to "aborted" if currently
Jeff Skirvina5fde222011-03-04 14:06:42 -08001258 * "starting"; if true then set the I/O kernel completion
Dan Williams6f231dd2011-07-02 22:56:22 -07001259 * struct that will be triggered when the request completes.
1260 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001261 old_state = isci_task_validate_request_to_abort(
1262 old_request, isci_host, isci_device,
1263 &aborted_io_completion);
Jeff Skirvinf219f012011-03-31 13:10:34 -07001264 if ((old_state != started) &&
1265 (old_state != completed) &&
1266 (old_state != aborting)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001267
1268 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1269
Jeff Skirvina5fde222011-03-04 14:06:42 -08001270 /* The request was already being handled by someone else (because
1271 * they got to set the state away from started).
1272 */
1273 dev_dbg(&isci_host->pdev->dev,
1274 "%s: device = %p; old_request %p already being aborted\n",
1275 __func__,
1276 isci_device, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001277
1278 return TMF_RESP_FUNC_COMPLETE;
1279 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001280 if ((task->task_proto == SAS_PROTOCOL_SMP)
1281 || device_stopping
1282 || old_request->complete_in_target
1283 ) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001284
1285 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1286
Jeff Skirvina5fde222011-03-04 14:06:42 -08001287 dev_dbg(&isci_host->pdev->dev,
1288 "%s: SMP request (%d)"
1289 " or device is stopping (%d)"
1290 " or complete_in_target (%d), thus no TMF\n",
1291 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
1292 device_stopping, old_request->complete_in_target);
1293
Dan Williams6f231dd2011-07-02 22:56:22 -07001294 /* Set the state on the task. */
1295 isci_task_all_done(task);
1296
1297 ret = TMF_RESP_FUNC_COMPLETE;
1298
1299 /* Stopping and SMP devices are not sent a TMF, and are not
Jeff Skirvina5fde222011-03-04 14:06:42 -08001300 * reset, but the outstanding I/O request is terminated below.
Dan Williams6f231dd2011-07-02 22:56:22 -07001301 */
Dan Williams6f231dd2011-07-02 22:56:22 -07001302 } else {
1303 /* Fill in the tmf stucture */
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -08001304 isci_task_build_abort_task_tmf(&tmf, isci_device,
1305 isci_tmf_ssp_task_abort,
1306 isci_abort_task_process_cb,
1307 old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001308
Dan Williams6f231dd2011-07-02 22:56:22 -07001309 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1310
1311 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
1312 ret = isci_task_execute_tmf(isci_host, &tmf,
1313 ISCI_ABORT_TASK_TIMEOUT_MS);
1314
Jeff Skirvina5fde222011-03-04 14:06:42 -08001315 if (ret != TMF_RESP_FUNC_COMPLETE)
Dan Williams6f231dd2011-07-02 22:56:22 -07001316 dev_err(&isci_host->pdev->dev,
1317 "%s: isci_task_send_tmf failed\n",
1318 __func__);
1319 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001320 if (ret == TMF_RESP_FUNC_COMPLETE) {
1321 old_request->complete_in_target = true;
Dan Williams6f231dd2011-07-02 22:56:22 -07001322
Jeff Skirvina5fde222011-03-04 14:06:42 -08001323 /* Clean up the request on our side, and wait for the aborted I/O to
1324 * complete.
1325 */
Jeff Skirvincbb65c62011-03-04 14:06:50 -08001326 isci_terminate_request_core(isci_host, isci_device, old_request);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001327 }
1328
1329 /* Make sure we do not leave a reference to aborted_io_completion */
1330 old_request->io_request_completion = NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -07001331 return ret;
1332}
1333
1334/**
1335 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1336 * functions. This is one of the Task Management functoins called by libsas,
1337 * to abort all task for the given lun.
1338 * @d_device: This parameter specifies the domain device associated with this
1339 * request.
1340 * @lun: This parameter specifies the lun associated with this request.
1341 *
1342 * status, zero indicates success.
1343 */
1344int isci_task_abort_task_set(
1345 struct domain_device *d_device,
1346 u8 *lun)
1347{
1348 return TMF_RESP_FUNC_FAILED;
1349}
1350
1351
1352/**
1353 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1354 * functions. This is one of the Task Management functoins called by libsas.
1355 * @d_device: This parameter specifies the domain device associated with this
1356 * request.
1357 * @lun: This parameter specifies the lun associated with this request.
1358 *
1359 * status, zero indicates success.
1360 */
1361int isci_task_clear_aca(
1362 struct domain_device *d_device,
1363 u8 *lun)
1364{
1365 return TMF_RESP_FUNC_FAILED;
1366}
1367
1368
1369
1370/**
1371 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1372 * functions. This is one of the Task Management functoins called by libsas.
1373 * @d_device: This parameter specifies the domain device associated with this
1374 * request.
1375 * @lun: This parameter specifies the lun associated with this request.
1376 *
1377 * status, zero indicates success.
1378 */
1379int isci_task_clear_task_set(
1380 struct domain_device *d_device,
1381 u8 *lun)
1382{
1383 return TMF_RESP_FUNC_FAILED;
1384}
1385
1386
1387/**
1388 * isci_task_query_task() - This function is implemented to cause libsas to
1389 * correctly escalate the failed abort to a LUN or target reset (this is
1390 * because sas_scsi_find_task libsas function does not correctly interpret
1391 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1392 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1393 * returned, libsas will turn this into a target reset
1394 * @task: This parameter specifies the sas task being queried.
1395 * @lun: This parameter specifies the lun associated with this request.
1396 *
1397 * status, zero indicates success.
1398 */
1399int isci_task_query_task(
1400 struct sas_task *task)
1401{
1402 /* See if there is a pending device reset for this device. */
1403 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1404 return TMF_RESP_FUNC_FAILED;
1405 else
1406 return TMF_RESP_FUNC_SUCC;
1407}
1408
Dave Jiangaf5ae892011-05-04 17:53:24 -07001409/*
Dan Williams6f231dd2011-07-02 22:56:22 -07001410 * isci_task_request_complete() - This function is called by the sci core when
1411 * an task request completes.
Dave Jiangaf5ae892011-05-04 17:53:24 -07001412 * @ihost: This parameter specifies the ISCI host object
1413 * @ireq: This parameter is the completed isci_request object.
Dan Williams6f231dd2011-07-02 22:56:22 -07001414 * @completion_status: This parameter specifies the completion status from the
1415 * sci core.
1416 *
1417 * none.
1418 */
Dave Jiangaf5ae892011-05-04 17:53:24 -07001419void
1420isci_task_request_complete(struct isci_host *ihost,
1421 struct isci_request *ireq,
1422 enum sci_task_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001423{
Dave Jiangaf5ae892011-05-04 17:53:24 -07001424 struct isci_remote_device *idev = ireq->isci_device;
Dan Williams6f231dd2011-07-02 22:56:22 -07001425 enum isci_request_status old_state;
Dave Jiangaf5ae892011-05-04 17:53:24 -07001426 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001427 struct completion *tmf_complete;
Dan Williams67ea8382011-05-08 11:47:15 -07001428 struct scic_sds_request *sci_req = &ireq->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -07001429
Dave Jiangaf5ae892011-05-04 17:53:24 -07001430 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001431 "%s: request = %p, status=%d\n",
Dave Jiangaf5ae892011-05-04 17:53:24 -07001432 __func__, ireq, completion_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001433
Dave Jiangaf5ae892011-05-04 17:53:24 -07001434 old_state = isci_request_change_state(ireq, completed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001435
1436 tmf->status = completion_status;
Dave Jiangaf5ae892011-05-04 17:53:24 -07001437 ireq->complete_in_target = true;
Dan Williams6f231dd2011-07-02 22:56:22 -07001438
Dave Jiangaf5ae892011-05-04 17:53:24 -07001439 if (tmf->proto == SAS_PROTOCOL_SSP) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001440 memcpy(&tmf->resp.resp_iu,
Dan Williamsb7645812011-05-08 02:35:32 -07001441 &sci_req->ssp.rsp,
Dave Jiangaf5ae892011-05-04 17:53:24 -07001442 SSP_RESP_IU_MAX_SIZE);
1443 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001444 memcpy(&tmf->resp.d2h_fis,
Dan Williamsb7645812011-05-08 02:35:32 -07001445 &sci_req->stp.rsp,
Dave Jiangf2f30082011-05-04 15:02:02 -07001446 sizeof(struct dev_to_host_fis));
Dan Williams6f231dd2011-07-02 22:56:22 -07001447 }
1448
1449 /* Manage the timer if it is still running. */
1450 if (tmf->timeout_timer) {
Dave Jiangaf5ae892011-05-04 17:53:24 -07001451 isci_del_timer(ihost, tmf->timeout_timer);
Dan Williams6f231dd2011-07-02 22:56:22 -07001452 tmf->timeout_timer = NULL;
1453 }
1454
1455 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1456 tmf_complete = tmf->complete;
1457
Dan Williams67ea8382011-05-08 11:47:15 -07001458 scic_controller_complete_io(&ihost->sci, &idev->sci, &ireq->sci);
1459 /* set the 'terminated' flag handle to make sure it cannot be terminated
Dan Williams6f231dd2011-07-02 22:56:22 -07001460 * or completed again.
1461 */
Dan Williams67ea8382011-05-08 11:47:15 -07001462 ireq->terminated = true;;
Dan Williams6f231dd2011-07-02 22:56:22 -07001463
Dave Jiangaf5ae892011-05-04 17:53:24 -07001464 isci_request_change_state(ireq, unallocated);
1465 list_del_init(&ireq->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -07001466
1467 /* The task management part completes last. */
1468 complete(tmf_complete);
1469}
1470
Dan Williamsd06b4872011-05-02 13:59:25 -07001471static int isci_reset_device(struct domain_device *dev, int hard_reset)
Dan Williams6f231dd2011-07-02 22:56:22 -07001472{
Dan Williamsd06b4872011-05-02 13:59:25 -07001473 struct isci_remote_device *idev = dev->lldd_dev;
1474 struct sas_phy *phy = sas_find_local_phy(dev);
1475 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001476 enum sci_status status;
Dan Williamsd06b4872011-05-02 13:59:25 -07001477 unsigned long flags;
1478 int rc;
Dan Williams6f231dd2011-07-02 22:56:22 -07001479
Dan Williamsd06b4872011-05-02 13:59:25 -07001480 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001481
Dan Williamsd06b4872011-05-02 13:59:25 -07001482 if (!idev) {
1483 dev_warn(&ihost->pdev->dev,
1484 "%s: idev is GONE!\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07001485 __func__);
1486
1487 return TMF_RESP_FUNC_COMPLETE; /* Nothing to reset. */
1488 }
1489
Dan Williamsd06b4872011-05-02 13:59:25 -07001490 spin_lock_irqsave(&ihost->scic_lock, flags);
1491 status = scic_remote_device_reset(&idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001492 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001493 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001494
Dan Williamsd06b4872011-05-02 13:59:25 -07001495 dev_warn(&ihost->pdev->dev,
1496 "%s: scic_remote_device_reset(%p) returned %d!\n",
1497 __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001498
1499 return TMF_RESP_FUNC_FAILED;
1500 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001501 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001502
Dan Williams6f231dd2011-07-02 22:56:22 -07001503 /* Make sure all pending requests are able to be fully terminated. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001504 isci_device_clear_reset_pending(ihost, idev);
1505
1506 rc = sas_phy_reset(phy, hard_reset);
1507 msleep(2000); /* just like mvsas */
Dan Williams6f231dd2011-07-02 22:56:22 -07001508
1509 /* Terminate in-progress I/O now. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001510 isci_remote_device_nuke_requests(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001511
Dan Williamsd06b4872011-05-02 13:59:25 -07001512 spin_lock_irqsave(&ihost->scic_lock, flags);
1513 status = scic_remote_device_reset_complete(&idev->sci);
1514 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001515
1516 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001517 dev_warn(&ihost->pdev->dev,
1518 "%s: scic_remote_device_reset_complete(%p) "
1519 "returned %d!\n", __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001520 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001521
Dan Williamsd06b4872011-05-02 13:59:25 -07001522 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001523
Dan Williamsd06b4872011-05-02 13:59:25 -07001524 return rc;
1525}
1526
1527int isci_task_I_T_nexus_reset(struct domain_device *dev)
1528{
1529 struct isci_host *ihost = dev_to_ihost(dev);
1530 int ret = TMF_RESP_FUNC_FAILED, hard_reset = 1;
1531 struct isci_remote_device *idev;
1532 unsigned long flags;
1533
1534 /* XXX mvsas is not protecting against ->lldd_dev_gone(), are we
1535 * being too paranoid, or is mvsas busted?!
1536 */
1537 spin_lock_irqsave(&ihost->scic_lock, flags);
1538 idev = dev->lldd_dev;
1539 if (!idev || !test_bit(IDEV_EH, &idev->flags))
1540 ret = TMF_RESP_FUNC_COMPLETE;
1541 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1542
1543 if (ret == TMF_RESP_FUNC_COMPLETE)
1544 return ret;
1545
1546 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1547 hard_reset = 0;
1548
1549 return isci_reset_device(dev, hard_reset);
1550}
1551
1552int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1553{
1554 struct domain_device *dev = sdev_to_domain_dev(cmd->device);
1555 int hard_reset = 1;
1556
1557 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1558 hard_reset = 0;
1559
1560 return isci_reset_device(dev, hard_reset);
Dan Williams6f231dd2011-07-02 22:56:22 -07001561}