blob: 7adaf71c19d117f983179adda133738d5a383480 [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"
Dave Jiangaf5ae892011-05-04 17:53:24 -070068
Dan Williams50e7f9b2011-03-09 21:27:46 -080069/**
Dan Williams1077a572011-03-11 10:13:51 -080070* isci_task_refuse() - complete the request to the upper layer driver in
71* the case where an I/O needs to be completed back in the submit path.
72* @ihost: host on which the the request was queued
73* @task: request to complete
74* @response: response code for the completed task.
75* @status: status code for the completed task.
Dan Williams50e7f9b2011-03-09 21:27:46 -080076*
Dan Williams50e7f9b2011-03-09 21:27:46 -080077*/
Dan Williams1077a572011-03-11 10:13:51 -080078static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
79 enum service_response response,
80 enum exec_status status)
Dan Williams50e7f9b2011-03-09 21:27:46 -080081
Dan Williams1077a572011-03-11 10:13:51 -080082{
83 enum isci_completion_selection disposition;
84
85 disposition = isci_perform_normal_io_completion;
86 disposition = isci_task_set_completion_status(task, response, status,
87 disposition);
Dan Williams50e7f9b2011-03-09 21:27:46 -080088
89 /* Tasks aborted specifically by a call to the lldd_abort_task
Dan Williams1077a572011-03-11 10:13:51 -080090 * function should not be completed to the host in the regular path.
91 */
92 switch (disposition) {
Dan Williams50e7f9b2011-03-09 21:27:46 -080093 case isci_perform_normal_io_completion:
94 /* Normal notification (task_done) */
Dan Williams1077a572011-03-11 10:13:51 -080095 dev_dbg(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -070096 "%s: Normal - task = %p, response=%d, "
97 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -080098 __func__, task, response, status);
99
Dan Williams1077a572011-03-11 10:13:51 -0800100 task->lldd_task = NULL;
Dan Williams50e7f9b2011-03-09 21:27:46 -0800101
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700102 isci_execpath_callback(ihost, task, task->task_done);
Dan Williams50e7f9b2011-03-09 21:27:46 -0800103 break;
104
105 case isci_perform_aborted_io_completion:
106 /* No notification because this request is already in the
107 * abort path.
108 */
Dan Williams1077a572011-03-11 10:13:51 -0800109 dev_warn(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700110 "%s: Aborted - task = %p, response=%d, "
111 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -0800112 __func__, task, response, status);
113 break;
114
115 case isci_perform_error_io_completion:
116 /* Use sas_task_abort */
Dan Williams1077a572011-03-11 10:13:51 -0800117 dev_warn(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700118 "%s: Error - task = %p, response=%d, "
119 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -0800120 __func__, task, response, status);
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700121
122 isci_execpath_callback(ihost, task, sas_task_abort);
Dan Williams50e7f9b2011-03-09 21:27:46 -0800123 break;
124
125 default:
Dan Williams1077a572011-03-11 10:13:51 -0800126 dev_warn(&ihost->pdev->dev,
Dan Williams50e7f9b2011-03-09 21:27:46 -0800127 "%s: isci task notification default case!",
128 __func__);
129 sas_task_abort(task);
130 break;
131 }
132}
Dan Williams6f231dd2011-07-02 22:56:22 -0700133
Dan Williams1077a572011-03-11 10:13:51 -0800134#define for_each_sas_task(num, task) \
135 for (; num > 0; num--,\
136 task = list_entry(task->list.next, struct sas_task, list))
137
Dan Williams6f231dd2011-07-02 22:56:22 -0700138/**
139 * isci_task_execute_task() - This function is one of the SAS Domain Template
140 * functions. This function is called by libsas to send a task down to
141 * hardware.
142 * @task: This parameter specifies the SAS task to send.
143 * @num: This parameter specifies the number of tasks to queue.
144 * @gfp_flags: This parameter specifies the context of this call.
145 *
146 * status, zero indicates success.
147 */
148int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
149{
Dan Williams4393aa42011-03-31 13:10:44 -0700150 struct isci_host *ihost = dev_to_ihost(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700151 struct isci_request *request = NULL;
152 struct isci_remote_device *device;
153 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700154 int ret;
155 enum sci_status status;
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700156 enum isci_status device_status;
Dan Williams6f231dd2011-07-02 22:56:22 -0700157
Dan Williams1077a572011-03-11 10:13:51 -0800158 dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
Dan Williams6f231dd2011-07-02 22:56:22 -0700159
160 /* Check if we have room for more tasks */
Dan Williams1077a572011-03-11 10:13:51 -0800161 ret = isci_host_can_queue(ihost, num);
Dan Williams6f231dd2011-07-02 22:56:22 -0700162
163 if (ret) {
Dan Williams1077a572011-03-11 10:13:51 -0800164 dev_warn(&ihost->pdev->dev, "%s: queue full\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -0700165 return ret;
166 }
167
Dan Williams1077a572011-03-11 10:13:51 -0800168 for_each_sas_task(num, task) {
169 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700170 "task = %p, num = %d; dev = %p; cmd = %p\n",
171 task, num, task->dev, task->uldd_task);
172
Dan Williams4393aa42011-03-31 13:10:44 -0700173 device = task->dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700174
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700175 if (device)
176 device_status = device->status;
177 else
178 device_status = isci_freed;
179
180 /* From this point onward, any process that needs to guarantee
181 * that there is no kernel I/O being started will have to wait
182 * for the quiesce spinlock.
183 */
184
185 if (device_status != isci_ready_for_io) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700186
187 /* Forces a retry from scsi mid layer. */
Dave Jiangd2d61432011-04-21 05:36:23 +0000188 dev_dbg(&ihost->pdev->dev,
189 "%s: task %p: isci_host->status = %d, "
190 "device = %p; device_status = 0x%x\n\n",
191 __func__,
192 task,
193 isci_host_get_state(ihost),
194 device,
195 device_status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700196
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700197 if (device_status == isci_ready) {
198 /* Indicate QUEUE_FULL so that the scsi midlayer
199 * retries.
200 */
Dan Williams1077a572011-03-11 10:13:51 -0800201 isci_task_refuse(ihost, task,
202 SAS_TASK_COMPLETE,
203 SAS_QUEUE_FULL);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700204 } else {
205 /* Else, the device is going down. */
Dan Williams1077a572011-03-11 10:13:51 -0800206 isci_task_refuse(ihost, task,
207 SAS_TASK_UNDELIVERED,
208 SAS_DEVICE_UNKNOWN);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700209 }
Dan Williams1077a572011-03-11 10:13:51 -0800210 isci_host_can_dequeue(ihost, 1);
Dan Williams6f231dd2011-07-02 22:56:22 -0700211 } else {
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700212 /* There is a device and it's ready for I/O. */
213 spin_lock_irqsave(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700214
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700215 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
216
217 spin_unlock_irqrestore(&task->task_state_lock,
218 flags);
219
Dan Williams1077a572011-03-11 10:13:51 -0800220 isci_task_refuse(ihost, task,
221 SAS_TASK_UNDELIVERED,
222 SAM_STAT_TASK_ABORTED);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700223
224 /* The I/O was aborted. */
225
226 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700227 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
228 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700229
230 /* build and send the request. */
Dan Williams1077a572011-03-11 10:13:51 -0800231 status = isci_request_execute(ihost, task, &request,
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700232 gfp_flags);
233
234 if (status != SCI_SUCCESS) {
235
236 spin_lock_irqsave(&task->task_state_lock, flags);
237 /* Did not really start this command. */
238 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
239 spin_unlock_irqrestore(&task->task_state_lock, flags);
240
241 /* Indicate QUEUE_FULL so that the scsi
242 * midlayer retries. if the request
243 * failed for remote device reasons,
244 * it gets returned as
245 * SAS_TASK_UNDELIVERED next time
246 * through.
247 */
Dan Williams1077a572011-03-11 10:13:51 -0800248 isci_task_refuse(ihost, task,
249 SAS_TASK_COMPLETE,
250 SAS_QUEUE_FULL);
251 isci_host_can_dequeue(ihost, 1);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700252 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700253 }
254 }
Dan Williams1077a572011-03-11 10:13:51 -0800255 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700256 return 0;
257}
258
259
260
261/**
262 * isci_task_request_build() - This function builds the task request object.
263 * @isci_host: This parameter specifies the ISCI host object
264 * @request: This parameter points to the isci_request object allocated in the
265 * request construct function.
266 * @tmf: This parameter is the task management struct to be built
267 *
268 * SCI_SUCCESS on successfull completion, or specific failure code.
269 */
270static enum sci_status isci_task_request_build(
271 struct isci_host *isci_host,
272 struct isci_request **isci_request,
273 struct isci_tmf *isci_tmf)
274{
275 struct scic_sds_remote_device *sci_device;
276 enum sci_status status = SCI_FAILURE;
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700277 struct isci_request *request = NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700278 struct isci_remote_device *isci_device;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700279 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700280
281 dev_dbg(&isci_host->pdev->dev,
282 "%s: isci_tmf = %p\n", __func__, isci_tmf);
283
284 isci_device = isci_tmf->device;
Dan Williams57f20f42011-04-21 18:14:45 -0700285 sci_device = &isci_device->sci;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700286 dev = isci_device->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700287
288 /* do common allocation and init of request object. */
289 status = isci_request_alloc_tmf(
290 isci_host,
291 isci_tmf,
292 &request,
293 isci_device,
294 GFP_ATOMIC
295 );
296
297 if (status != SCI_SUCCESS)
298 goto out;
299
300 /* let the core do it's construct. */
Dan Williams0d843662011-05-08 01:56:57 -0700301 status = scic_task_request_construct(&isci_host->sci, sci_device,
302 SCI_CONTROLLER_INVALID_IO_TAG,
Dan Williams67ea8382011-05-08 11:47:15 -0700303 &request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700304
305 if (status != SCI_SUCCESS) {
306 dev_warn(&isci_host->pdev->dev,
307 "%s: scic_task_request_construct failed - "
308 "status = 0x%x\n",
309 __func__,
310 status);
311 goto errout;
312 }
313
Dan Williamsa1a113b2011-04-21 18:44:45 -0700314 /* XXX convert to get this from task->tproto like other drivers */
315 if (dev->dev_type == SAS_END_DEV) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700316 isci_tmf->proto = SAS_PROTOCOL_SSP;
Dan Williams67ea8382011-05-08 11:47:15 -0700317 status = scic_task_request_construct_ssp(&request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700318 if (status != SCI_SUCCESS)
319 goto errout;
320 }
321
Dan Williamsa1a113b2011-04-21 18:44:45 -0700322 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700323 isci_tmf->proto = SAS_PROTOCOL_SATA;
324 status = isci_sata_management_task_request_build(request);
325
326 if (status != SCI_SUCCESS)
327 goto errout;
328 }
329
330 goto out;
331
332 errout:
333
334 /* release the dma memory if we fail. */
335 isci_request_free(isci_host, request);
336 request = NULL;
337
338 out:
339 *isci_request = request;
340 return status;
341}
342
343/**
344 * isci_tmf_timeout_cb() - This function is called as a kernel callback when
345 * the timeout period for the TMF has expired.
346 *
347 *
348 */
349static void isci_tmf_timeout_cb(void *tmf_request_arg)
350{
351 struct isci_request *request = (struct isci_request *)tmf_request_arg;
352 struct isci_tmf *tmf = isci_request_access_tmf(request);
353 enum sci_status status;
354
Dan Williams6f231dd2011-07-02 22:56:22 -0700355 /* This task management request has timed-out. Terminate the request
356 * so that the request eventually completes to the requestor in the
357 * request completion callback path.
358 */
359 /* Note - the timer callback function itself has provided spinlock
360 * exclusion from the start and completion paths. No need to take
361 * the request->isci_host->scic_lock here.
362 */
363
364 if (tmf->timeout_timer != NULL) {
365 /* Call the users callback, if any. */
366 if (tmf->cb_state_func != NULL)
367 tmf->cb_state_func(isci_tmf_timed_out, tmf,
368 tmf->cb_data);
369
370 /* Terminate the TMF transmit request. */
371 status = scic_controller_terminate_request(
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000372 &request->isci_host->sci,
Dan Williams57f20f42011-04-21 18:14:45 -0700373 &request->isci_device->sci,
Dan Williams67ea8382011-05-08 11:47:15 -0700374 &request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700375
376 dev_dbg(&request->isci_host->pdev->dev,
377 "%s: tmf_request = %p; tmf = %p; status = %d\n",
378 __func__, request, tmf, status);
379 } else
380 dev_dbg(&request->isci_host->pdev->dev,
381 "%s: timer already canceled! "
382 "tmf_request = %p; tmf = %p\n",
383 __func__, request, tmf);
384
385 /* No need to unlock since the caller to this callback is doing it for
386 * us.
387 * request->isci_host->scic_lock
388 */
389}
390
391/**
392 * isci_task_execute_tmf() - This function builds and sends a task request,
393 * then waits for the completion.
394 * @isci_host: This parameter specifies the ISCI host object
395 * @tmf: This parameter is the pointer to the task management structure for
396 * this request.
397 * @timeout_ms: This parameter specifies the timeout period for the task
398 * management request.
399 *
400 * TMF_RESP_FUNC_COMPLETE on successful completion of the TMF (this includes
401 * error conditions reported in the IU status), or TMF_RESP_FUNC_FAILED.
402 */
403int isci_task_execute_tmf(
404 struct isci_host *isci_host,
405 struct isci_tmf *tmf,
406 unsigned long timeout_ms)
407{
408 DECLARE_COMPLETION_ONSTACK(completion);
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700409 enum sci_task_status status = SCI_TASK_FAILURE;
Dan Williams6f231dd2011-07-02 22:56:22 -0700410 struct scic_sds_remote_device *sci_device;
411 struct isci_remote_device *isci_device = tmf->device;
412 struct isci_request *request;
413 int ret = TMF_RESP_FUNC_FAILED;
414 unsigned long flags;
415
416 /* sanity check, return TMF_RESP_FUNC_FAILED
417 * if the device is not there and ready.
418 */
Dan Williams8acaec12011-03-07 14:47:35 -0800419 if (!isci_device || isci_device->status != isci_ready_for_io) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700420 dev_dbg(&isci_host->pdev->dev,
421 "%s: isci_device = %p not ready (%d)\n",
422 __func__,
Dan Williams8acaec12011-03-07 14:47:35 -0800423 isci_device, isci_device->status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700424 return TMF_RESP_FUNC_FAILED;
425 } else
426 dev_dbg(&isci_host->pdev->dev,
427 "%s: isci_device = %p\n",
428 __func__, isci_device);
429
Dan Williams57f20f42011-04-21 18:14:45 -0700430 sci_device = &isci_device->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700431
432 /* Assign the pointer to the TMF's completion kernel wait structure. */
433 tmf->complete = &completion;
434
435 isci_task_request_build(
436 isci_host,
437 &request,
438 tmf
439 );
440
441 if (!request) {
442 dev_warn(&isci_host->pdev->dev,
443 "%s: isci_task_request_build failed\n",
444 __func__);
445 return TMF_RESP_FUNC_FAILED;
446 }
447
448 /* Allocate the TMF timeout timer. */
Dan Williams6f231dd2011-07-02 22:56:22 -0700449 spin_lock_irqsave(&isci_host->scic_lock, flags);
Dan Williams7c40a802011-03-02 11:49:26 -0800450 tmf->timeout_timer = isci_timer_create(isci_host, request, isci_tmf_timeout_cb);
Dan Williams6f231dd2011-07-02 22:56:22 -0700451
452 /* Start the timer. */
453 if (tmf->timeout_timer)
454 isci_timer_start(tmf->timeout_timer, timeout_ms);
455 else
456 dev_warn(&isci_host->pdev->dev,
457 "%s: isci_timer_create failed!!!!\n",
458 __func__);
459
460 /* start the TMF io. */
461 status = scic_controller_start_task(
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000462 &isci_host->sci,
Dan Williams6f231dd2011-07-02 22:56:22 -0700463 sci_device,
Dan Williams67ea8382011-05-08 11:47:15 -0700464 &request->sci,
465 SCI_CONTROLLER_INVALID_IO_TAG);
Dan Williams6f231dd2011-07-02 22:56:22 -0700466
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700467 if (status != SCI_TASK_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700468 dev_warn(&isci_host->pdev->dev,
469 "%s: start_io failed - status = 0x%x, request = %p\n",
470 __func__,
471 status,
472 request);
473 goto cleanup_request;
474 }
475
476 /* Call the users callback, if any. */
477 if (tmf->cb_state_func != NULL)
478 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
479
480 /* Change the state of the TMF-bearing request to "started". */
481 isci_request_change_state(request, started);
482
483 /* add the request to the remote device request list. */
484 list_add(&request->dev_node, &isci_device->reqs_in_process);
485
486 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
487
488 /* Wait for the TMF to complete, or a timeout. */
489 wait_for_completion(&completion);
490
491 isci_print_tmf(tmf);
492
493 if (tmf->status == SCI_SUCCESS)
494 ret = TMF_RESP_FUNC_COMPLETE;
495 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
496 dev_dbg(&isci_host->pdev->dev,
497 "%s: tmf.status == "
498 "SCI_FAILURE_IO_RESPONSE_VALID\n",
499 __func__);
500 ret = TMF_RESP_FUNC_COMPLETE;
501 }
502 /* Else - leave the default "failed" status alone. */
503
504 dev_dbg(&isci_host->pdev->dev,
505 "%s: completed request = %p\n",
506 __func__,
507 request);
508
509 if (request->io_request_completion != NULL) {
510
511 /* The fact that this is non-NULL for a TMF request
512 * means there is a thread waiting for this TMF to
513 * finish.
514 */
515 complete(request->io_request_completion);
516 }
517
518 spin_lock_irqsave(&isci_host->scic_lock, flags);
519
520 cleanup_request:
521
522 /* Clean up the timer if needed. */
523 if (tmf->timeout_timer) {
Dan Williams7c40a802011-03-02 11:49:26 -0800524 isci_del_timer(isci_host, tmf->timeout_timer);
Dan Williams6f231dd2011-07-02 22:56:22 -0700525 tmf->timeout_timer = NULL;
526 }
527
528 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
529
530 isci_request_free(isci_host, request);
531
532 return ret;
533}
534
535void isci_task_build_tmf(
536 struct isci_tmf *tmf,
537 struct isci_remote_device *isci_device,
538 enum isci_tmf_function_codes code,
539 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
540 struct isci_tmf *,
541 void *),
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800542 void *cb_data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700543{
544 dev_dbg(&isci_device->isci_port->isci_host->pdev->dev,
545 "%s: isci_device = %p\n", __func__, isci_device);
546
547 memset(tmf, 0, sizeof(*tmf));
548
549 tmf->device = isci_device;
550 tmf->tmf_code = code;
551 tmf->timeout_timer = NULL;
552 tmf->cb_state_func = tmf_sent_cb;
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800553 tmf->cb_data = cb_data;
554}
Jeff Skirvin1fad9e92011-03-04 14:06:46 -0800555
Dan Williams35173d52011-03-26 16:43:01 -0700556static void isci_task_build_abort_task_tmf(
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800557 struct isci_tmf *tmf,
558 struct isci_remote_device *isci_device,
559 enum isci_tmf_function_codes code,
560 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
561 struct isci_tmf *,
562 void *),
563 struct isci_request *old_request)
564{
565 isci_task_build_tmf(tmf, isci_device, code, tmf_sent_cb,
566 (void *)old_request);
567 tmf->io_tag = old_request->io_tag;
Dan Williams6f231dd2011-07-02 22:56:22 -0700568}
569
570static struct isci_request *isci_task_get_request_from_task(
571 struct sas_task *task,
Dan Williams6f231dd2011-07-02 22:56:22 -0700572 struct isci_remote_device **isci_device)
573{
574
575 struct isci_request *request = NULL;
576 unsigned long flags;
577
578 spin_lock_irqsave(&task->task_state_lock, flags);
579
580 request = task->lldd_task;
581
582 /* If task is already done, the request isn't valid */
583 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
584 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
585 (request != NULL)) {
586
Dan Williams6f231dd2011-07-02 22:56:22 -0700587 if (isci_device != NULL)
588 *isci_device = request->isci_device;
589 }
590
591 spin_unlock_irqrestore(&task->task_state_lock, flags);
592
593 return request;
594}
595
596/**
597 * isci_task_validate_request_to_abort() - This function checks the given I/O
598 * against the "started" state. If the request is still "started", it's
599 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
600 * BEFORE CALLING THIS FUNCTION.
601 * @isci_request: This parameter specifies the request object to control.
602 * @isci_host: This parameter specifies the ISCI host object
603 * @isci_device: This is the device to which the request is pending.
604 * @aborted_io_completion: This is a completion structure that will be added to
605 * the request in case it is changed to aborting; this completion is
606 * triggered when the request is fully completed.
607 *
608 * Either "started" on successful change of the task status to "aborted", or
609 * "unallocated" if the task cannot be controlled.
610 */
611static enum isci_request_status isci_task_validate_request_to_abort(
612 struct isci_request *isci_request,
613 struct isci_host *isci_host,
614 struct isci_remote_device *isci_device,
615 struct completion *aborted_io_completion)
616{
617 enum isci_request_status old_state = unallocated;
618
619 /* Only abort the task if it's in the
620 * device's request_in_process list
621 */
622 if (isci_request && !list_empty(&isci_request->dev_node)) {
623 old_state = isci_request_change_started_to_aborted(
624 isci_request, aborted_io_completion);
625
Dan Williams6f231dd2011-07-02 22:56:22 -0700626 }
627
628 return old_state;
629}
630
631static void isci_request_cleanup_completed_loiterer(
632 struct isci_host *isci_host,
633 struct isci_remote_device *isci_device,
634 struct isci_request *isci_request)
635{
Jeff Skirvin18d3d722011-03-04 14:06:38 -0800636 struct sas_task *task;
637 unsigned long flags;
638
639 task = (isci_request->ttype == io_task)
640 ? isci_request_access_task(isci_request)
641 : NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700642
643 dev_dbg(&isci_host->pdev->dev,
644 "%s: isci_device=%p, request=%p, task=%p\n",
Jeff Skirvin18d3d722011-03-04 14:06:38 -0800645 __func__, isci_device, isci_request, task);
Dan Williams6f231dd2011-07-02 22:56:22 -0700646
647 spin_lock_irqsave(&isci_host->scic_lock, flags);
648 list_del_init(&isci_request->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -0700649 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
650
Jeff Skirvina5fde222011-03-04 14:06:42 -0800651 if (task != NULL) {
652
653 spin_lock_irqsave(&task->task_state_lock, flags);
654 task->lldd_task = NULL;
655
656 isci_set_task_doneflags(task);
657
658 /* If this task is not in the abort path, call task_done. */
659 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
660
661 spin_unlock_irqrestore(&task->task_state_lock, flags);
662 task->task_done(task);
663 } else
664 spin_unlock_irqrestore(&task->task_state_lock, flags);
665 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700666 isci_request_free(isci_host, isci_request);
667}
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800668
669/**
670* @isci_termination_timed_out(): this function will deal with a request for
671* which the wait for termination has timed-out.
672*
673* @isci_host This SCU.
674* @isci_request The I/O request being terminated.
675*/
676static void
677isci_termination_timed_out(
678 struct isci_host * host,
679 struct isci_request * request
680 )
681{
682 unsigned long state_flags;
683
684 dev_warn(&host->pdev->dev,
685 "%s: host = %p; request = %p\n",
686 __func__, host, request);
687
688 /* At this point, the request to terminate
689 * has timed out. The best we can do is to
690 * have the request die a silent death
691 * if it ever completes.
692 */
693 spin_lock_irqsave(&request->state_lock, state_flags);
694
695 if (request->status == started) {
696
697 /* Set the request state to "dead",
698 * and clear the task pointer so that an actual
699 * completion event callback doesn't do
700 * anything.
701 */
702 request->status = dead;
703
704 /* Clear the timeout completion event pointer.*/
705 request->io_request_completion = NULL;
706
707 if (request->ttype == io_task) {
708
709 /* Break links with the sas_task. */
710 if (request->ttype_ptr.io_task_ptr != NULL) {
711
712 request->ttype_ptr.io_task_ptr->lldd_task = NULL;
713 request->ttype_ptr.io_task_ptr = NULL;
714 }
715 }
716 }
717 spin_unlock_irqrestore(&request->state_lock, state_flags);
718}
719
720
Dan Williams6f231dd2011-07-02 22:56:22 -0700721/**
722 * isci_terminate_request_core() - This function will terminate the given
723 * request, and wait for it to complete. This function must only be called
724 * from a thread that can wait. Note that the request is terminated and
725 * completed (back to the host, if started there).
726 * @isci_host: This SCU.
727 * @isci_device: The target.
728 * @isci_request: The I/O request to be terminated.
729 *
730 *
731 */
732static void isci_terminate_request_core(
733 struct isci_host *isci_host,
734 struct isci_remote_device *isci_device,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800735 struct isci_request *isci_request)
Dan Williams6f231dd2011-07-02 22:56:22 -0700736{
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800737 enum sci_status status = SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700738 bool was_terminated = false;
739 bool needs_cleanup_handling = false;
740 enum isci_request_status request_status;
741 unsigned long flags;
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800742 unsigned long timeout_remaining;
743
Dan Williams6f231dd2011-07-02 22:56:22 -0700744
745 dev_dbg(&isci_host->pdev->dev,
746 "%s: device = %p; request = %p\n",
747 __func__, isci_device, isci_request);
748
Dan Williams6f231dd2011-07-02 22:56:22 -0700749 spin_lock_irqsave(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800750
751 /* Note that we are not going to control
752 * the target to abort the request.
753 */
754 isci_request->complete_in_target = true;
755
Dan Williams6f231dd2011-07-02 22:56:22 -0700756 /* Make sure the request wasn't just sitting around signalling
757 * device condition (if the request handle is NULL, then the
758 * request completed but needed additional handling here).
759 */
Dan Williams67ea8382011-05-08 11:47:15 -0700760 if (!isci_request->terminated) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700761 was_terminated = true;
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800762 needs_cleanup_handling = true;
Dan Williams6f231dd2011-07-02 22:56:22 -0700763 status = scic_controller_terminate_request(
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000764 &isci_host->sci,
Dan Williams57f20f42011-04-21 18:14:45 -0700765 &isci_device->sci,
Dan Williams67ea8382011-05-08 11:47:15 -0700766 &isci_request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700767 }
768 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
769
770 /*
771 * The only time the request to terminate will
772 * fail is when the io request is completed and
773 * being aborted.
774 */
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800775 if (status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700776 dev_err(&isci_host->pdev->dev,
777 "%s: scic_controller_terminate_request"
778 " returned = 0x%x\n",
779 __func__,
780 status);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800781 /* Clear the completion pointer from the request. */
782 isci_request->io_request_completion = NULL;
783
784 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700785 if (was_terminated) {
786 dev_dbg(&isci_host->pdev->dev,
787 "%s: before completion wait (%p)\n",
788 __func__,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800789 isci_request->io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700790
791 /* Wait here for the request to complete. */
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800792 #define TERMINATION_TIMEOUT_MSEC 50
793 timeout_remaining
794 = wait_for_completion_timeout(
795 isci_request->io_request_completion,
796 msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
Dan Williams6f231dd2011-07-02 22:56:22 -0700797
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800798 if (!timeout_remaining) {
799
800 isci_termination_timed_out(isci_host,
801 isci_request);
802
803 dev_err(&isci_host->pdev->dev,
804 "%s: *** Timeout waiting for "
805 "termination(%p/%p)\n",
806 __func__,
807 isci_request->io_request_completion,
808 isci_request);
809
810 } else
811 dev_dbg(&isci_host->pdev->dev,
812 "%s: after completion wait (%p)\n",
813 __func__,
814 isci_request->io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700815 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800816 /* Clear the completion pointer from the request. */
817 isci_request->io_request_completion = NULL;
818
819 /* Peek at the status of the request. This will tell
820 * us if there was special handling on the request such that it
821 * needs to be detached and freed here.
822 */
823 spin_lock_irqsave(&isci_request->state_lock, flags);
824 request_status = isci_request_get_state(isci_request);
825
826 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
827 && ((request_status == aborted)
828 || (request_status == aborting)
829 || (request_status == terminating)
830 || (request_status == completed)
831 || (request_status == dead)
832 )
833 ) {
834
835 /* The completion routine won't free a request in
836 * the aborted/aborting/etc. states, so we do
837 * it here.
838 */
839 needs_cleanup_handling = true;
840 }
841 spin_unlock_irqrestore(&isci_request->state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700842
843 if (needs_cleanup_handling)
844 isci_request_cleanup_completed_loiterer(
845 isci_host, isci_device, isci_request
846 );
847 }
848}
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800849
Dan Williams6f231dd2011-07-02 22:56:22 -0700850static void isci_terminate_request(
851 struct isci_host *isci_host,
852 struct isci_remote_device *isci_device,
853 struct isci_request *isci_request,
854 enum isci_request_status new_request_state)
855{
856 enum isci_request_status old_state;
Dan Williams6f231dd2011-07-02 22:56:22 -0700857 DECLARE_COMPLETION_ONSTACK(request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700858
859 /* Change state to "new_request_state" if it is currently "started" */
860 old_state = isci_request_change_started_to_newstate(
861 isci_request,
862 &request_completion,
863 new_request_state
864 );
865
Jeff Skirvinf219f012011-03-31 13:10:34 -0700866 if ((old_state == started) ||
867 (old_state == completed) ||
868 (old_state == aborting)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700869
Jeff Skirvina5fde222011-03-04 14:06:42 -0800870 /* If the old_state is started:
871 * This request was not already being aborted. If it had been,
Dan Williams6f231dd2011-07-02 22:56:22 -0700872 * then the aborting I/O (ie. the TMF request) would not be in
873 * the aborting state, and thus would be terminated here. Note
874 * that since the TMF completion's call to the kernel function
875 * "complete()" does not happen until the pending I/O request
876 * terminate fully completes, we do not have to implement a
877 * special wait here for already aborting requests - the
878 * termination of the TMF request will force the request
879 * to finish it's already started terminate.
Jeff Skirvina5fde222011-03-04 14:06:42 -0800880 *
881 * If old_state == completed:
882 * This request completed from the SCU hardware perspective
883 * and now just needs cleaning up in terms of freeing the
884 * request and potentially calling up to libsas.
Jeff Skirvinf219f012011-03-31 13:10:34 -0700885 *
886 * If old_state == aborting:
887 * This request has already gone through a TMF timeout, but may
888 * not have been terminated; needs cleaning up at least.
Dan Williams6f231dd2011-07-02 22:56:22 -0700889 */
890 isci_terminate_request_core(isci_host, isci_device,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800891 isci_request);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800892 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700893}
894
895/**
896 * isci_terminate_pending_requests() - This function will change the all of the
897 * requests on the given device's state to "aborting", will terminate the
898 * requests, and wait for them to complete. This function must only be
899 * called from a thread that can wait. Note that the requests are all
900 * terminated and completed (back to the host, if started there).
901 * @isci_host: This parameter specifies SCU.
902 * @isci_device: This parameter specifies the target.
903 *
904 *
905 */
906void isci_terminate_pending_requests(
907 struct isci_host *isci_host,
908 struct isci_remote_device *isci_device,
909 enum isci_request_status new_request_state)
910{
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800911 struct isci_request *request;
912 struct isci_request *next_request;
913 unsigned long flags;
914 struct list_head aborted_request_list;
915
916 INIT_LIST_HEAD(&aborted_request_list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700917
918 dev_dbg(&isci_host->pdev->dev,
919 "%s: isci_device = %p (new request state = %d)\n",
920 __func__, isci_device, new_request_state);
921
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800922 spin_lock_irqsave(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700923
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800924 /* Move all of the pending requests off of the device list. */
925 list_splice_init(&isci_device->reqs_in_process,
926 &aborted_request_list);
927
928 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
929
930 /* Iterate through the now-local list. */
931 list_for_each_entry_safe(request, next_request,
932 &aborted_request_list, dev_node) {
933
934 dev_warn(&isci_host->pdev->dev,
935 "%s: isci_device=%p request=%p; task=%p\n",
936 __func__,
937 isci_device, request,
938 ((request->ttype == io_task)
939 ? isci_request_access_task(request)
940 : NULL));
941
942 /* Mark all still pending I/O with the selected next
943 * state, terminate and free it.
944 */
945 isci_terminate_request(isci_host, isci_device,
946 request, new_request_state
947 );
Dan Williams6f231dd2011-07-02 22:56:22 -0700948 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700949}
950
951/**
952 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
953 * Template functions.
954 * @lun: This parameter specifies the lun to be reset.
955 *
956 * status, zero indicates success.
957 */
958static int isci_task_send_lu_reset_sas(
959 struct isci_host *isci_host,
960 struct isci_remote_device *isci_device,
961 u8 *lun)
962{
963 struct isci_tmf tmf;
964 int ret = TMF_RESP_FUNC_FAILED;
965
966 dev_dbg(&isci_host->pdev->dev,
967 "%s: isci_host = %p, isci_device = %p\n",
968 __func__, isci_host, isci_device);
969 /* Send the LUN reset to the target. By the time the call returns,
970 * the TMF has fully exected in the target (in which case the return
971 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
972 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
973 */
974 isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_lun_reset, NULL,
975 NULL);
976
977 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
978 ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
979
980 if (ret == TMF_RESP_FUNC_COMPLETE)
981 dev_dbg(&isci_host->pdev->dev,
982 "%s: %p: TMF_LU_RESET passed\n",
983 __func__, isci_device);
984 else
985 dev_dbg(&isci_host->pdev->dev,
986 "%s: %p: TMF_LU_RESET failed (%x)\n",
987 __func__, isci_device, ret);
988
989 return ret;
990}
991
992/**
993 * isci_task_lu_reset() - This function is one of the SAS Domain Template
994 * functions. This is one of the Task Management functoins called by libsas,
995 * to reset the given lun. Note the assumption that while this call is
996 * executing, no I/O will be sent by the host to the device.
997 * @lun: This parameter specifies the lun to be reset.
998 *
999 * status, zero indicates success.
1000 */
Dan Williams4393aa42011-03-31 13:10:44 -07001001int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
Dan Williams6f231dd2011-07-02 22:56:22 -07001002{
Dan Williams4393aa42011-03-31 13:10:44 -07001003 struct isci_host *isci_host = dev_to_ihost(domain_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001004 struct isci_remote_device *isci_device = NULL;
1005 int ret;
1006 bool device_stopping = false;
1007
Dan Williams4393aa42011-03-31 13:10:44 -07001008 isci_device = domain_device->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001009
Dan Williams4393aa42011-03-31 13:10:44 -07001010 dev_dbg(&isci_host->pdev->dev,
1011 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07001012 __func__, domain_device, isci_host, isci_device);
1013
Dan Williamsd06b4872011-05-02 13:59:25 -07001014 if (isci_device != NULL) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001015 device_stopping = (isci_device->status == isci_stopping)
1016 || (isci_device->status == isci_stopped);
Dan Williamsd06b4872011-05-02 13:59:25 -07001017 set_bit(IDEV_EH, &isci_device->flags);
1018 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001019
1020 /* If there is a device reset pending on any request in the
1021 * device's list, fail this LUN reset request in order to
1022 * escalate to the device reset.
1023 */
Dan Williams4393aa42011-03-31 13:10:44 -07001024 if (!isci_device || device_stopping ||
1025 isci_device_is_reset_pending(isci_host, isci_device)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001026 dev_warn(&isci_host->pdev->dev,
Dan Williams4393aa42011-03-31 13:10:44 -07001027 "%s: No dev (%p), or "
Dan Williams6f231dd2011-07-02 22:56:22 -07001028 "RESET PENDING: domain_device=%p\n",
Dan Williams4393aa42011-03-31 13:10:44 -07001029 __func__, isci_device, domain_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001030 return TMF_RESP_FUNC_FAILED;
1031 }
1032
Dan Williams6f231dd2011-07-02 22:56:22 -07001033 /* Send the task management part of the reset. */
1034 if (sas_protocol_ata(domain_device->tproto)) {
Dan Williams4393aa42011-03-31 13:10:44 -07001035 ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
Dan Williams6f231dd2011-07-02 22:56:22 -07001036 } else
1037 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
1038
1039 /* If the LUN reset worked, all the I/O can now be terminated. */
1040 if (ret == TMF_RESP_FUNC_COMPLETE)
1041 /* Terminate all I/O now. */
1042 isci_terminate_pending_requests(isci_host,
1043 isci_device,
1044 terminating);
1045
Dan Williams6f231dd2011-07-02 22:56:22 -07001046 return ret;
1047}
1048
1049
1050/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
1051int isci_task_clear_nexus_port(struct asd_sas_port *port)
1052{
1053 return TMF_RESP_FUNC_FAILED;
1054}
1055
1056
1057
1058int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
1059{
1060 return TMF_RESP_FUNC_FAILED;
1061}
1062
Dan Williams6f231dd2011-07-02 22:56:22 -07001063/* Task Management Functions. Must be called from process context. */
1064
1065/**
1066 * isci_abort_task_process_cb() - This is a helper function for the abort task
1067 * TMF command. It manages the request state with respect to the successful
1068 * transmission / completion of the abort task request.
1069 * @cb_state: This parameter specifies when this function was called - after
1070 * the TMF request has been started and after it has timed-out.
1071 * @tmf: This parameter specifies the TMF in progress.
1072 *
1073 *
1074 */
1075static void isci_abort_task_process_cb(
1076 enum isci_tmf_cb_state cb_state,
1077 struct isci_tmf *tmf,
1078 void *cb_data)
1079{
1080 struct isci_request *old_request;
1081
1082 old_request = (struct isci_request *)cb_data;
1083
1084 dev_dbg(&old_request->isci_host->pdev->dev,
1085 "%s: tmf=%p, old_request=%p\n",
1086 __func__, tmf, old_request);
1087
1088 switch (cb_state) {
1089
1090 case isci_tmf_started:
1091 /* The TMF has been started. Nothing to do here, since the
1092 * request state was already set to "aborted" by the abort
1093 * task function.
1094 */
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001095 if ((old_request->status != aborted)
1096 && (old_request->status != completed))
1097 dev_err(&old_request->isci_host->pdev->dev,
1098 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
1099 __func__, old_request->status, tmf, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001100 break;
1101
1102 case isci_tmf_timed_out:
1103
1104 /* Set the task's state to "aborting", since the abort task
1105 * function thread set it to "aborted" (above) in anticipation
1106 * of the task management request working correctly. Since the
1107 * timeout has now fired, the TMF request failed. We set the
1108 * state such that the request completion will indicate the
1109 * device is no longer present.
1110 */
1111 isci_request_change_state(old_request, aborting);
1112 break;
1113
1114 default:
1115 dev_err(&old_request->isci_host->pdev->dev,
1116 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
1117 __func__, cb_state, tmf, old_request);
1118 break;
1119 }
1120}
1121
1122/**
1123 * isci_task_abort_task() - This function is one of the SAS Domain Template
1124 * functions. This function is called by libsas to abort a specified task.
1125 * @task: This parameter specifies the SAS task to abort.
1126 *
1127 * status, zero indicates success.
1128 */
1129int isci_task_abort_task(struct sas_task *task)
1130{
Dan Williams4393aa42011-03-31 13:10:44 -07001131 struct isci_host *isci_host = dev_to_ihost(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001132 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001133 struct isci_request *old_request = NULL;
1134 enum isci_request_status old_state;
Dan Williams6f231dd2011-07-02 22:56:22 -07001135 struct isci_remote_device *isci_device = NULL;
Jeff Skirvina5fde222011-03-04 14:06:42 -08001136 struct isci_tmf tmf;
1137 int ret = TMF_RESP_FUNC_FAILED;
1138 unsigned long flags;
1139 bool any_dev_reset = false;
1140 bool device_stopping;
Dan Williams6f231dd2011-07-02 22:56:22 -07001141
1142 /* Get the isci_request reference from the task. Note that
1143 * this check does not depend on the pending request list
1144 * in the device, because tasks driving resets may land here
1145 * after completion in the core.
1146 */
Dan Williams4393aa42011-03-31 13:10:44 -07001147 old_request = isci_task_get_request_from_task(task, &isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001148
1149 dev_dbg(&isci_host->pdev->dev,
1150 "%s: task = %p\n", __func__, task);
1151
1152 /* Check if the device has been / is currently being removed.
1153 * If so, no task management will be done, and the I/O will
1154 * be terminated.
1155 */
1156 device_stopping = (isci_device->status == isci_stopping)
1157 || (isci_device->status == isci_stopped);
1158
Dan Williamsd06b4872011-05-02 13:59:25 -07001159 /* XXX need to fix device lookup lifetime (needs to be done
1160 * under scic_lock, among other things...), but for now assume
1161 * the device is available like the above code
1162 */
1163 set_bit(IDEV_EH, &isci_device->flags);
1164
Dan Williams6f231dd2011-07-02 22:56:22 -07001165 /* This version of the driver will fail abort requests for
1166 * SATA/STP. Failing the abort request this way will cause the
1167 * SCSI error handler thread to escalate to LUN reset
1168 */
1169 if (sas_protocol_ata(task->task_proto) && !device_stopping) {
1170 dev_warn(&isci_host->pdev->dev,
1171 " task %p is for a STP/SATA device;"
1172 " returning TMF_RESP_FUNC_FAILED\n"
1173 " to cause a LUN reset...\n", task);
1174 return TMF_RESP_FUNC_FAILED;
1175 }
1176
1177 dev_dbg(&isci_host->pdev->dev,
1178 "%s: old_request == %p\n", __func__, old_request);
1179
Jeff Skirvina5fde222011-03-04 14:06:42 -08001180 if (!device_stopping)
1181 any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
1182
Dan Williams6f231dd2011-07-02 22:56:22 -07001183 spin_lock_irqsave(&task->task_state_lock, flags);
1184
1185 /* Don't do resets to stopping devices. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001186 if (device_stopping) {
1187
Dan Williams6f231dd2011-07-02 22:56:22 -07001188 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
Jeff Skirvina5fde222011-03-04 14:06:42 -08001189 any_dev_reset = false;
Dan Williams6f231dd2011-07-02 22:56:22 -07001190
Jeff Skirvina5fde222011-03-04 14:06:42 -08001191 } else /* See if there is a pending device reset for this device. */
Dan Williams6f231dd2011-07-02 22:56:22 -07001192 any_dev_reset = any_dev_reset
Jeff Skirvina5fde222011-03-04 14:06:42 -08001193 || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
Dan Williams6f231dd2011-07-02 22:56:22 -07001194
1195 /* If the extraction of the request reference from the task
1196 * failed, then the request has been completed (or if there is a
1197 * pending reset then this abort request function must be failed
1198 * in order to escalate to the target reset).
1199 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001200 if ((old_request == NULL) || any_dev_reset) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001201
1202 /* If the device reset task flag is set, fail the task
1203 * management request. Otherwise, the original request
1204 * has completed.
1205 */
1206 if (any_dev_reset) {
1207
1208 /* Turn off the task's DONE to make sure this
1209 * task is escalated to a target reset.
1210 */
1211 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1212
Jeff Skirvina5fde222011-03-04 14:06:42 -08001213 /* Make the reset happen as soon as possible. */
1214 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1215
1216 spin_unlock_irqrestore(&task->task_state_lock, flags);
1217
Dan Williams6f231dd2011-07-02 22:56:22 -07001218 /* Fail the task management request in order to
1219 * escalate to the target reset.
1220 */
1221 ret = TMF_RESP_FUNC_FAILED;
1222
1223 dev_dbg(&isci_host->pdev->dev,
1224 "%s: Failing task abort in order to "
1225 "escalate to target reset because\n"
1226 "SAS_TASK_NEED_DEV_RESET is set for "
1227 "task %p on dev %p\n",
1228 __func__, task, isci_device);
1229
Jeff Skirvina5fde222011-03-04 14:06:42 -08001230
Dan Williams6f231dd2011-07-02 22:56:22 -07001231 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -07001232 /* The request has already completed and there
1233 * is nothing to do here other than to set the task
1234 * done bit, and indicate that the task abort function
1235 * was sucessful.
1236 */
1237 isci_set_task_doneflags(task);
1238
Jeff Skirvina5fde222011-03-04 14:06:42 -08001239 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001240
Jeff Skirvina5fde222011-03-04 14:06:42 -08001241 ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001242
Jeff Skirvina5fde222011-03-04 14:06:42 -08001243 dev_dbg(&isci_host->pdev->dev,
1244 "%s: abort task not needed for %p\n",
1245 __func__, task);
Dan Williams6f231dd2011-07-02 22:56:22 -07001246 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001247
1248 return ret;
1249 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001250 else
1251 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001252
1253 spin_lock_irqsave(&isci_host->scic_lock, flags);
1254
Jeff Skirvinf219f012011-03-31 13:10:34 -07001255 /* Check the request status and change to "aborted" if currently
Jeff Skirvina5fde222011-03-04 14:06:42 -08001256 * "starting"; if true then set the I/O kernel completion
Dan Williams6f231dd2011-07-02 22:56:22 -07001257 * struct that will be triggered when the request completes.
1258 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001259 old_state = isci_task_validate_request_to_abort(
1260 old_request, isci_host, isci_device,
1261 &aborted_io_completion);
Jeff Skirvinf219f012011-03-31 13:10:34 -07001262 if ((old_state != started) &&
1263 (old_state != completed) &&
1264 (old_state != aborting)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001265
1266 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1267
Jeff Skirvina5fde222011-03-04 14:06:42 -08001268 /* The request was already being handled by someone else (because
1269 * they got to set the state away from started).
1270 */
1271 dev_dbg(&isci_host->pdev->dev,
1272 "%s: device = %p; old_request %p already being aborted\n",
1273 __func__,
1274 isci_device, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001275
1276 return TMF_RESP_FUNC_COMPLETE;
1277 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001278 if ((task->task_proto == SAS_PROTOCOL_SMP)
1279 || device_stopping
1280 || old_request->complete_in_target
1281 ) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001282
1283 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1284
Jeff Skirvina5fde222011-03-04 14:06:42 -08001285 dev_dbg(&isci_host->pdev->dev,
1286 "%s: SMP request (%d)"
1287 " or device is stopping (%d)"
1288 " or complete_in_target (%d), thus no TMF\n",
1289 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
1290 device_stopping, old_request->complete_in_target);
1291
Dan Williams6f231dd2011-07-02 22:56:22 -07001292 /* Set the state on the task. */
1293 isci_task_all_done(task);
1294
1295 ret = TMF_RESP_FUNC_COMPLETE;
1296
1297 /* Stopping and SMP devices are not sent a TMF, and are not
Jeff Skirvina5fde222011-03-04 14:06:42 -08001298 * reset, but the outstanding I/O request is terminated below.
Dan Williams6f231dd2011-07-02 22:56:22 -07001299 */
Dan Williams6f231dd2011-07-02 22:56:22 -07001300 } else {
1301 /* Fill in the tmf stucture */
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -08001302 isci_task_build_abort_task_tmf(&tmf, isci_device,
1303 isci_tmf_ssp_task_abort,
1304 isci_abort_task_process_cb,
1305 old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001306
Dan Williams6f231dd2011-07-02 22:56:22 -07001307 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1308
1309 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
1310 ret = isci_task_execute_tmf(isci_host, &tmf,
1311 ISCI_ABORT_TASK_TIMEOUT_MS);
1312
Jeff Skirvina5fde222011-03-04 14:06:42 -08001313 if (ret != TMF_RESP_FUNC_COMPLETE)
Dan Williams6f231dd2011-07-02 22:56:22 -07001314 dev_err(&isci_host->pdev->dev,
1315 "%s: isci_task_send_tmf failed\n",
1316 __func__);
1317 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001318 if (ret == TMF_RESP_FUNC_COMPLETE) {
1319 old_request->complete_in_target = true;
Dan Williams6f231dd2011-07-02 22:56:22 -07001320
Jeff Skirvina5fde222011-03-04 14:06:42 -08001321 /* Clean up the request on our side, and wait for the aborted I/O to
1322 * complete.
1323 */
Jeff Skirvincbb65c62011-03-04 14:06:50 -08001324 isci_terminate_request_core(isci_host, isci_device, old_request);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001325 }
1326
1327 /* Make sure we do not leave a reference to aborted_io_completion */
1328 old_request->io_request_completion = NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -07001329 return ret;
1330}
1331
1332/**
1333 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1334 * functions. This is one of the Task Management functoins called by libsas,
1335 * to abort all task for the given lun.
1336 * @d_device: This parameter specifies the domain device associated with this
1337 * request.
1338 * @lun: This parameter specifies the lun associated with this request.
1339 *
1340 * status, zero indicates success.
1341 */
1342int isci_task_abort_task_set(
1343 struct domain_device *d_device,
1344 u8 *lun)
1345{
1346 return TMF_RESP_FUNC_FAILED;
1347}
1348
1349
1350/**
1351 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1352 * functions. This is one of the Task Management functoins called by libsas.
1353 * @d_device: This parameter specifies the domain device associated with this
1354 * request.
1355 * @lun: This parameter specifies the lun associated with this request.
1356 *
1357 * status, zero indicates success.
1358 */
1359int isci_task_clear_aca(
1360 struct domain_device *d_device,
1361 u8 *lun)
1362{
1363 return TMF_RESP_FUNC_FAILED;
1364}
1365
1366
1367
1368/**
1369 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1370 * functions. This is one of the Task Management functoins called by libsas.
1371 * @d_device: This parameter specifies the domain device associated with this
1372 * request.
1373 * @lun: This parameter specifies the lun associated with this request.
1374 *
1375 * status, zero indicates success.
1376 */
1377int isci_task_clear_task_set(
1378 struct domain_device *d_device,
1379 u8 *lun)
1380{
1381 return TMF_RESP_FUNC_FAILED;
1382}
1383
1384
1385/**
1386 * isci_task_query_task() - This function is implemented to cause libsas to
1387 * correctly escalate the failed abort to a LUN or target reset (this is
1388 * because sas_scsi_find_task libsas function does not correctly interpret
1389 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1390 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1391 * returned, libsas will turn this into a target reset
1392 * @task: This parameter specifies the sas task being queried.
1393 * @lun: This parameter specifies the lun associated with this request.
1394 *
1395 * status, zero indicates success.
1396 */
1397int isci_task_query_task(
1398 struct sas_task *task)
1399{
1400 /* See if there is a pending device reset for this device. */
1401 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1402 return TMF_RESP_FUNC_FAILED;
1403 else
1404 return TMF_RESP_FUNC_SUCC;
1405}
1406
Dave Jiangaf5ae892011-05-04 17:53:24 -07001407/*
Dan Williams6f231dd2011-07-02 22:56:22 -07001408 * isci_task_request_complete() - This function is called by the sci core when
1409 * an task request completes.
Dave Jiangaf5ae892011-05-04 17:53:24 -07001410 * @ihost: This parameter specifies the ISCI host object
1411 * @ireq: This parameter is the completed isci_request object.
Dan Williams6f231dd2011-07-02 22:56:22 -07001412 * @completion_status: This parameter specifies the completion status from the
1413 * sci core.
1414 *
1415 * none.
1416 */
Dave Jiangaf5ae892011-05-04 17:53:24 -07001417void
1418isci_task_request_complete(struct isci_host *ihost,
1419 struct isci_request *ireq,
1420 enum sci_task_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001421{
Dave Jiangaf5ae892011-05-04 17:53:24 -07001422 struct isci_remote_device *idev = ireq->isci_device;
Dan Williams6f231dd2011-07-02 22:56:22 -07001423 enum isci_request_status old_state;
Dave Jiangaf5ae892011-05-04 17:53:24 -07001424 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001425 struct completion *tmf_complete;
Dan Williams67ea8382011-05-08 11:47:15 -07001426 struct scic_sds_request *sci_req = &ireq->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -07001427
Dave Jiangaf5ae892011-05-04 17:53:24 -07001428 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001429 "%s: request = %p, status=%d\n",
Dave Jiangaf5ae892011-05-04 17:53:24 -07001430 __func__, ireq, completion_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001431
Dave Jiangaf5ae892011-05-04 17:53:24 -07001432 old_state = isci_request_change_state(ireq, completed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001433
1434 tmf->status = completion_status;
Dave Jiangaf5ae892011-05-04 17:53:24 -07001435 ireq->complete_in_target = true;
Dan Williams6f231dd2011-07-02 22:56:22 -07001436
Dave Jiangaf5ae892011-05-04 17:53:24 -07001437 if (tmf->proto == SAS_PROTOCOL_SSP) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001438 memcpy(&tmf->resp.resp_iu,
Dan Williamsb7645812011-05-08 02:35:32 -07001439 &sci_req->ssp.rsp,
Dave Jiangaf5ae892011-05-04 17:53:24 -07001440 SSP_RESP_IU_MAX_SIZE);
1441 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001442 memcpy(&tmf->resp.d2h_fis,
Dan Williamsb7645812011-05-08 02:35:32 -07001443 &sci_req->stp.rsp,
Dave Jiangf2f30082011-05-04 15:02:02 -07001444 sizeof(struct dev_to_host_fis));
Dan Williams6f231dd2011-07-02 22:56:22 -07001445 }
1446
1447 /* Manage the timer if it is still running. */
1448 if (tmf->timeout_timer) {
Dave Jiangaf5ae892011-05-04 17:53:24 -07001449 isci_del_timer(ihost, tmf->timeout_timer);
Dan Williams6f231dd2011-07-02 22:56:22 -07001450 tmf->timeout_timer = NULL;
1451 }
1452
1453 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1454 tmf_complete = tmf->complete;
1455
Dan Williams67ea8382011-05-08 11:47:15 -07001456 scic_controller_complete_io(&ihost->sci, &idev->sci, &ireq->sci);
1457 /* set the 'terminated' flag handle to make sure it cannot be terminated
Dan Williams6f231dd2011-07-02 22:56:22 -07001458 * or completed again.
1459 */
Dan Williams67ea8382011-05-08 11:47:15 -07001460 ireq->terminated = true;;
Dan Williams6f231dd2011-07-02 22:56:22 -07001461
Dave Jiangaf5ae892011-05-04 17:53:24 -07001462 isci_request_change_state(ireq, unallocated);
1463 list_del_init(&ireq->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -07001464
1465 /* The task management part completes last. */
1466 complete(tmf_complete);
1467}
1468
Dan Williamsd06b4872011-05-02 13:59:25 -07001469static int isci_reset_device(struct domain_device *dev, int hard_reset)
Dan Williams6f231dd2011-07-02 22:56:22 -07001470{
Dan Williamsd06b4872011-05-02 13:59:25 -07001471 struct isci_remote_device *idev = dev->lldd_dev;
1472 struct sas_phy *phy = sas_find_local_phy(dev);
1473 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001474 enum sci_status status;
Dan Williamsd06b4872011-05-02 13:59:25 -07001475 unsigned long flags;
1476 int rc;
Dan Williams6f231dd2011-07-02 22:56:22 -07001477
Dan Williamsd06b4872011-05-02 13:59:25 -07001478 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001479
Dan Williamsd06b4872011-05-02 13:59:25 -07001480 if (!idev) {
1481 dev_warn(&ihost->pdev->dev,
1482 "%s: idev is GONE!\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07001483 __func__);
1484
1485 return TMF_RESP_FUNC_COMPLETE; /* Nothing to reset. */
1486 }
1487
Dan Williamsd06b4872011-05-02 13:59:25 -07001488 spin_lock_irqsave(&ihost->scic_lock, flags);
1489 status = scic_remote_device_reset(&idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001490 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001491 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001492
Dan Williamsd06b4872011-05-02 13:59:25 -07001493 dev_warn(&ihost->pdev->dev,
1494 "%s: scic_remote_device_reset(%p) returned %d!\n",
1495 __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001496
1497 return TMF_RESP_FUNC_FAILED;
1498 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001499 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001500
Dan Williams6f231dd2011-07-02 22:56:22 -07001501 /* Make sure all pending requests are able to be fully terminated. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001502 isci_device_clear_reset_pending(ihost, idev);
1503
1504 rc = sas_phy_reset(phy, hard_reset);
1505 msleep(2000); /* just like mvsas */
Dan Williams6f231dd2011-07-02 22:56:22 -07001506
1507 /* Terminate in-progress I/O now. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001508 isci_remote_device_nuke_requests(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001509
Dan Williamsd06b4872011-05-02 13:59:25 -07001510 spin_lock_irqsave(&ihost->scic_lock, flags);
1511 status = scic_remote_device_reset_complete(&idev->sci);
1512 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001513
1514 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001515 dev_warn(&ihost->pdev->dev,
1516 "%s: scic_remote_device_reset_complete(%p) "
1517 "returned %d!\n", __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001518 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001519
Dan Williamsd06b4872011-05-02 13:59:25 -07001520 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001521
Dan Williamsd06b4872011-05-02 13:59:25 -07001522 return rc;
1523}
1524
1525int isci_task_I_T_nexus_reset(struct domain_device *dev)
1526{
1527 struct isci_host *ihost = dev_to_ihost(dev);
1528 int ret = TMF_RESP_FUNC_FAILED, hard_reset = 1;
1529 struct isci_remote_device *idev;
1530 unsigned long flags;
1531
1532 /* XXX mvsas is not protecting against ->lldd_dev_gone(), are we
1533 * being too paranoid, or is mvsas busted?!
1534 */
1535 spin_lock_irqsave(&ihost->scic_lock, flags);
1536 idev = dev->lldd_dev;
1537 if (!idev || !test_bit(IDEV_EH, &idev->flags))
1538 ret = TMF_RESP_FUNC_COMPLETE;
1539 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1540
1541 if (ret == TMF_RESP_FUNC_COMPLETE)
1542 return ret;
1543
1544 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1545 hard_reset = 0;
1546
1547 return isci_reset_device(dev, hard_reset);
1548}
1549
1550int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1551{
1552 struct domain_device *dev = sdev_to_domain_dev(cmd->device);
1553 int hard_reset = 1;
1554
1555 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1556 hard_reset = 0;
1557
1558 return isci_reset_device(dev, hard_reset);
Dan Williams6f231dd2011-07-02 22:56:22 -07001559}