isci: fix ssp response iu buffer size in isci_tmf
[deliverable/linux.git] / drivers / scsi / isci / task.c
CommitLineData
6f231dda
DW
1/*
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>
50e7f9b5 57#include <linux/irqflags.h>
af5ae893 58#include "sas.h"
61aaff49 59#include <scsi/libsas.h>
88f3b62a
DW
60#include "remote_device.h"
61#include "remote_node_context.h"
6f231dda
DW
62#include "isci.h"
63#include "request.h"
64#include "sata.h"
65#include "task.h"
af5ae893 66
50e7f9b5 67/**
1077a574
DW
68* isci_task_refuse() - complete the request to the upper layer driver in
69* the case where an I/O needs to be completed back in the submit path.
70* @ihost: host on which the the request was queued
71* @task: request to complete
72* @response: response code for the completed task.
73* @status: status code for the completed task.
50e7f9b5 74*
50e7f9b5 75*/
1077a574
DW
76static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
77 enum service_response response,
78 enum exec_status status)
79
50e7f9b5 80{
1077a574 81 enum isci_completion_selection disposition;
50e7f9b5 82
1077a574
DW
83 disposition = isci_perform_normal_io_completion;
84 disposition = isci_task_set_completion_status(task, response, status,
85 disposition);
50e7f9b5
DW
86
87 /* Tasks aborted specifically by a call to the lldd_abort_task
1077a574
DW
88 * function should not be completed to the host in the regular path.
89 */
90 switch (disposition) {
50e7f9b5
DW
91 case isci_perform_normal_io_completion:
92 /* Normal notification (task_done) */
1077a574 93 dev_dbg(&ihost->pdev->dev,
ed8a72d1
JS
94 "%s: Normal - task = %p, response=%d, "
95 "status=%d\n",
50e7f9b5
DW
96 __func__, task, response, status);
97
1077a574 98 task->lldd_task = NULL;
50e7f9b5 99
ed8a72d1 100 isci_execpath_callback(ihost, task, task->task_done);
50e7f9b5
DW
101 break;
102
103 case isci_perform_aborted_io_completion:
104 /* No notification because this request is already in the
105 * abort path.
106 */
1077a574 107 dev_warn(&ihost->pdev->dev,
ed8a72d1
JS
108 "%s: Aborted - task = %p, response=%d, "
109 "status=%d\n",
50e7f9b5
DW
110 __func__, task, response, status);
111 break;
112
113 case isci_perform_error_io_completion:
114 /* Use sas_task_abort */
1077a574 115 dev_warn(&ihost->pdev->dev,
ed8a72d1
JS
116 "%s: Error - task = %p, response=%d, "
117 "status=%d\n",
50e7f9b5 118 __func__, task, response, status);
ed8a72d1
JS
119
120 isci_execpath_callback(ihost, task, sas_task_abort);
50e7f9b5
DW
121 break;
122
123 default:
1077a574 124 dev_warn(&ihost->pdev->dev,
50e7f9b5
DW
125 "%s: isci task notification default case!",
126 __func__);
127 sas_task_abort(task);
128 break;
129 }
130}
6f231dda 131
1077a574
DW
132#define for_each_sas_task(num, task) \
133 for (; num > 0; num--,\
134 task = list_entry(task->list.next, struct sas_task, list))
135
6f231dda
DW
136/**
137 * isci_task_execute_task() - This function is one of the SAS Domain Template
138 * functions. This function is called by libsas to send a task down to
139 * hardware.
140 * @task: This parameter specifies the SAS task to send.
141 * @num: This parameter specifies the number of tasks to queue.
142 * @gfp_flags: This parameter specifies the context of this call.
143 *
144 * status, zero indicates success.
145 */
146int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
147{
4393aa4e 148 struct isci_host *ihost = dev_to_ihost(task->dev);
6f231dda
DW
149 struct isci_remote_device *device;
150 unsigned long flags;
6f231dda
DW
151 int ret;
152 enum sci_status status;
f0846c68 153 enum isci_status device_status;
6f231dda 154
1077a574 155 dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
6f231dda
DW
156
157 /* Check if we have room for more tasks */
1077a574 158 ret = isci_host_can_queue(ihost, num);
6f231dda
DW
159
160 if (ret) {
1077a574 161 dev_warn(&ihost->pdev->dev, "%s: queue full\n", __func__);
6f231dda
DW
162 return ret;
163 }
164
1077a574
DW
165 for_each_sas_task(num, task) {
166 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
167 "task = %p, num = %d; dev = %p; cmd = %p\n",
168 task, num, task->dev, task->uldd_task);
169
4393aa4e 170 device = task->dev->lldd_dev;
6f231dda 171
f0846c68
JS
172 if (device)
173 device_status = device->status;
174 else
175 device_status = isci_freed;
176
177 /* From this point onward, any process that needs to guarantee
178 * that there is no kernel I/O being started will have to wait
179 * for the quiesce spinlock.
180 */
181
182 if (device_status != isci_ready_for_io) {
6f231dda
DW
183
184 /* Forces a retry from scsi mid layer. */
d2d61433
DJ
185 dev_dbg(&ihost->pdev->dev,
186 "%s: task %p: isci_host->status = %d, "
187 "device = %p; device_status = 0x%x\n\n",
188 __func__,
189 task,
190 isci_host_get_state(ihost),
191 device,
192 device_status);
6f231dda 193
f0846c68
JS
194 if (device_status == isci_ready) {
195 /* Indicate QUEUE_FULL so that the scsi midlayer
196 * retries.
197 */
1077a574
DW
198 isci_task_refuse(ihost, task,
199 SAS_TASK_COMPLETE,
200 SAS_QUEUE_FULL);
f0846c68
JS
201 } else {
202 /* Else, the device is going down. */
1077a574
DW
203 isci_task_refuse(ihost, task,
204 SAS_TASK_UNDELIVERED,
205 SAS_DEVICE_UNKNOWN);
f0846c68 206 }
1077a574 207 isci_host_can_dequeue(ihost, 1);
f0846c68
JS
208 } else {
209 /* There is a device and it's ready for I/O. */
210 spin_lock_irqsave(&task->task_state_lock, flags);
6f231dda 211
f0846c68 212 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
6f231dda 213
f0846c68
JS
214 spin_unlock_irqrestore(&task->task_state_lock,
215 flags);
6f231dda 216
1077a574
DW
217 isci_task_refuse(ihost, task,
218 SAS_TASK_UNDELIVERED,
219 SAM_STAT_TASK_ABORTED);
6f231dda 220
f0846c68 221 /* The I/O was aborted. */
6f231dda 222
f0846c68 223 } else {
6f231dda
DW
224 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
225 spin_unlock_irqrestore(&task->task_state_lock, flags);
f0846c68
JS
226
227 /* build and send the request. */
0d0cf14c 228 status = isci_request_execute(ihost, task, gfp_flags);
f0846c68
JS
229
230 if (status != SCI_SUCCESS) {
231
232 spin_lock_irqsave(&task->task_state_lock, flags);
233 /* Did not really start this command. */
234 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
235 spin_unlock_irqrestore(&task->task_state_lock, flags);
236
237 /* Indicate QUEUE_FULL so that the scsi
238 * midlayer retries. if the request
239 * failed for remote device reasons,
240 * it gets returned as
241 * SAS_TASK_UNDELIVERED next time
242 * through.
243 */
1077a574
DW
244 isci_task_refuse(ihost, task,
245 SAS_TASK_COMPLETE,
246 SAS_QUEUE_FULL);
247 isci_host_can_dequeue(ihost, 1);
f0846c68 248 }
6f231dda
DW
249 }
250 }
1077a574 251 }
6f231dda
DW
252 return 0;
253}
254
0d0cf14c
DW
255static struct isci_request *isci_task_request_build(struct isci_host *ihost,
256 struct isci_tmf *isci_tmf)
6f231dda 257{
0d0cf14c 258 struct scic_sds_remote_device *sci_dev;
6f231dda 259 enum sci_status status = SCI_FAILURE;
0d0cf14c
DW
260 struct isci_request *ireq = NULL;
261 struct isci_remote_device *idev;
a1a113b0 262 struct domain_device *dev;
6f231dda 263
0d0cf14c 264 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
265 "%s: isci_tmf = %p\n", __func__, isci_tmf);
266
0d0cf14c
DW
267 idev = isci_tmf->device;
268 sci_dev = &idev->sci;
269 dev = idev->domain_dev;
6f231dda
DW
270
271 /* do common allocation and init of request object. */
0d0cf14c
DW
272 ireq = isci_request_alloc_tmf(ihost, isci_tmf, idev, GFP_ATOMIC);
273 if (!ireq)
274 return NULL;
6f231dda
DW
275
276 /* let the core do it's construct. */
0d0cf14c 277 status = scic_task_request_construct(&ihost->sci, sci_dev,
0d84366f 278 SCI_CONTROLLER_INVALID_IO_TAG,
0d0cf14c 279 &ireq->sci);
6f231dda
DW
280
281 if (status != SCI_SUCCESS) {
0d0cf14c 282 dev_warn(&ihost->pdev->dev,
6f231dda
DW
283 "%s: scic_task_request_construct failed - "
284 "status = 0x%x\n",
285 __func__,
286 status);
287 goto errout;
288 }
289
a1a113b0
DW
290 /* XXX convert to get this from task->tproto like other drivers */
291 if (dev->dev_type == SAS_END_DEV) {
6f231dda 292 isci_tmf->proto = SAS_PROTOCOL_SSP;
0d0cf14c 293 status = scic_task_request_construct_ssp(&ireq->sci);
6f231dda
DW
294 if (status != SCI_SUCCESS)
295 goto errout;
296 }
297
a1a113b0 298 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
6f231dda 299 isci_tmf->proto = SAS_PROTOCOL_SATA;
0d0cf14c 300 status = isci_sata_management_task_request_build(ireq);
6f231dda
DW
301
302 if (status != SCI_SUCCESS)
303 goto errout;
304 }
0d0cf14c 305 return ireq;
6f231dda 306 errout:
0d0cf14c
DW
307 isci_request_free(ihost, ireq);
308 ireq = NULL;
309 return ireq;
6f231dda
DW
310}
311
6f231dda
DW
312/**
313 * isci_task_execute_tmf() - This function builds and sends a task request,
314 * then waits for the completion.
315 * @isci_host: This parameter specifies the ISCI host object
316 * @tmf: This parameter is the pointer to the task management structure for
317 * this request.
318 * @timeout_ms: This parameter specifies the timeout period for the task
319 * management request.
320 *
321 * TMF_RESP_FUNC_COMPLETE on successful completion of the TMF (this includes
322 * error conditions reported in the IU status), or TMF_RESP_FUNC_FAILED.
323 */
0d0cf14c
DW
324int isci_task_execute_tmf(struct isci_host *ihost, struct isci_tmf *tmf,
325 unsigned long timeout_ms)
6f231dda
DW
326{
327 DECLARE_COMPLETION_ONSTACK(completion);
467e855a 328 enum sci_task_status status = SCI_TASK_FAILURE;
6f231dda
DW
329 struct scic_sds_remote_device *sci_device;
330 struct isci_remote_device *isci_device = tmf->device;
0d0cf14c 331 struct isci_request *ireq;
6f231dda
DW
332 int ret = TMF_RESP_FUNC_FAILED;
333 unsigned long flags;
fd18388b 334 unsigned long timeleft;
6f231dda
DW
335
336 /* sanity check, return TMF_RESP_FUNC_FAILED
337 * if the device is not there and ready.
338 */
8acaec15 339 if (!isci_device || isci_device->status != isci_ready_for_io) {
0d0cf14c 340 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
341 "%s: isci_device = %p not ready (%d)\n",
342 __func__,
8acaec15 343 isci_device, isci_device->status);
6f231dda
DW
344 return TMF_RESP_FUNC_FAILED;
345 } else
0d0cf14c 346 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
347 "%s: isci_device = %p\n",
348 __func__, isci_device);
349
57f20f4e 350 sci_device = &isci_device->sci;
6f231dda
DW
351
352 /* Assign the pointer to the TMF's completion kernel wait structure. */
353 tmf->complete = &completion;
354
0d0cf14c
DW
355 ireq = isci_task_request_build(ihost, tmf);
356 if (!ireq) {
357 dev_warn(&ihost->pdev->dev,
6f231dda
DW
358 "%s: isci_task_request_build failed\n",
359 __func__);
360 return TMF_RESP_FUNC_FAILED;
361 }
362
0d0cf14c 363 spin_lock_irqsave(&ihost->scic_lock, flags);
6f231dda
DW
364
365 /* start the TMF io. */
366 status = scic_controller_start_task(
0d0cf14c 367 &ihost->sci,
6f231dda 368 sci_device,
0d0cf14c 369 &ireq->sci,
67ea838d 370 SCI_CONTROLLER_INVALID_IO_TAG);
6f231dda 371
467e855a 372 if (status != SCI_TASK_SUCCESS) {
0d0cf14c 373 dev_warn(&ihost->pdev->dev,
6f231dda
DW
374 "%s: start_io failed - status = 0x%x, request = %p\n",
375 __func__,
376 status,
0d0cf14c
DW
377 ireq);
378 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda
DW
379 goto cleanup_request;
380 }
381
6f231dda
DW
382 if (tmf->cb_state_func != NULL)
383 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
384
0d0cf14c 385 isci_request_change_state(ireq, started);
6f231dda
DW
386
387 /* add the request to the remote device request list. */
0d0cf14c 388 list_add(&ireq->dev_node, &isci_device->reqs_in_process);
6f231dda 389
0d0cf14c 390 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda
DW
391
392 /* Wait for the TMF to complete, or a timeout. */
fd18388b
EN
393 timeleft = wait_for_completion_timeout(&completion,
394 jiffies + msecs_to_jiffies(timeout_ms));
395
396 if (timeleft == 0) {
0d0cf14c 397 spin_lock_irqsave(&ihost->scic_lock, flags);
fd18388b
EN
398
399 if (tmf->cb_state_func != NULL)
400 tmf->cb_state_func(isci_tmf_timed_out, tmf, tmf->cb_data);
401
402 status = scic_controller_terminate_request(
0d0cf14c
DW
403 &ireq->isci_host->sci,
404 &ireq->isci_device->sci,
405 &ireq->sci);
fd18388b 406
0d0cf14c 407 spin_unlock_irqrestore(&ihost->scic_lock, flags);
fd18388b 408 }
6f231dda
DW
409
410 isci_print_tmf(tmf);
411
412 if (tmf->status == SCI_SUCCESS)
413 ret = TMF_RESP_FUNC_COMPLETE;
414 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
0d0cf14c 415 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
416 "%s: tmf.status == "
417 "SCI_FAILURE_IO_RESPONSE_VALID\n",
418 __func__);
419 ret = TMF_RESP_FUNC_COMPLETE;
420 }
421 /* Else - leave the default "failed" status alone. */
422
0d0cf14c 423 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
424 "%s: completed request = %p\n",
425 __func__,
0d0cf14c 426 ireq);
6f231dda 427
0d0cf14c 428 if (ireq->io_request_completion != NULL) {
fd18388b 429 /* A thread is waiting for this TMF to finish. */
0d0cf14c 430 complete(ireq->io_request_completion);
6f231dda
DW
431 }
432
6f231dda 433 cleanup_request:
0d0cf14c 434 isci_request_free(ihost, ireq);
6f231dda
DW
435 return ret;
436}
437
438void isci_task_build_tmf(
439 struct isci_tmf *tmf,
440 struct isci_remote_device *isci_device,
441 enum isci_tmf_function_codes code,
442 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
443 struct isci_tmf *,
444 void *),
c3f42feb 445 void *cb_data)
6f231dda
DW
446{
447 dev_dbg(&isci_device->isci_port->isci_host->pdev->dev,
448 "%s: isci_device = %p\n", __func__, isci_device);
449
450 memset(tmf, 0, sizeof(*tmf));
451
452 tmf->device = isci_device;
453 tmf->tmf_code = code;
fd18388b 454
6f231dda 455 tmf->cb_state_func = tmf_sent_cb;
c3f42feb
JS
456 tmf->cb_data = cb_data;
457}
1fad9e93 458
35173d57 459static void isci_task_build_abort_task_tmf(
c3f42feb
JS
460 struct isci_tmf *tmf,
461 struct isci_remote_device *isci_device,
462 enum isci_tmf_function_codes code,
463 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
464 struct isci_tmf *,
465 void *),
466 struct isci_request *old_request)
467{
468 isci_task_build_tmf(tmf, isci_device, code, tmf_sent_cb,
469 (void *)old_request);
470 tmf->io_tag = old_request->io_tag;
6f231dda
DW
471}
472
473static struct isci_request *isci_task_get_request_from_task(
474 struct sas_task *task,
6f231dda
DW
475 struct isci_remote_device **isci_device)
476{
477
478 struct isci_request *request = NULL;
479 unsigned long flags;
480
481 spin_lock_irqsave(&task->task_state_lock, flags);
482
483 request = task->lldd_task;
484
485 /* If task is already done, the request isn't valid */
486 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
487 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
488 (request != NULL)) {
489
6f231dda
DW
490 if (isci_device != NULL)
491 *isci_device = request->isci_device;
492 }
493
494 spin_unlock_irqrestore(&task->task_state_lock, flags);
495
496 return request;
497}
498
499/**
500 * isci_task_validate_request_to_abort() - This function checks the given I/O
501 * against the "started" state. If the request is still "started", it's
502 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
503 * BEFORE CALLING THIS FUNCTION.
504 * @isci_request: This parameter specifies the request object to control.
505 * @isci_host: This parameter specifies the ISCI host object
506 * @isci_device: This is the device to which the request is pending.
507 * @aborted_io_completion: This is a completion structure that will be added to
508 * the request in case it is changed to aborting; this completion is
509 * triggered when the request is fully completed.
510 *
511 * Either "started" on successful change of the task status to "aborted", or
512 * "unallocated" if the task cannot be controlled.
513 */
514static enum isci_request_status isci_task_validate_request_to_abort(
515 struct isci_request *isci_request,
516 struct isci_host *isci_host,
517 struct isci_remote_device *isci_device,
518 struct completion *aborted_io_completion)
519{
520 enum isci_request_status old_state = unallocated;
521
522 /* Only abort the task if it's in the
523 * device's request_in_process list
524 */
525 if (isci_request && !list_empty(&isci_request->dev_node)) {
526 old_state = isci_request_change_started_to_aborted(
527 isci_request, aborted_io_completion);
528
6f231dda
DW
529 }
530
531 return old_state;
532}
533
77c852f3
JS
534/**
535* isci_request_cleanup_completed_loiterer() - This function will take care of
536* the final cleanup on any request which has been explicitly terminated.
537* @isci_host: This parameter specifies the ISCI host object
538* @isci_device: This is the device to which the request is pending.
539* @isci_request: This parameter specifies the terminated request object.
540* @task: This parameter is the libsas I/O request.
541*/
6f231dda 542static void isci_request_cleanup_completed_loiterer(
77c852f3 543 struct isci_host *isci_host,
6f231dda 544 struct isci_remote_device *isci_device,
77c852f3
JS
545 struct isci_request *isci_request,
546 struct sas_task *task)
6f231dda 547{
77c852f3 548 unsigned long flags;
6f231dda
DW
549
550 dev_dbg(&isci_host->pdev->dev,
551 "%s: isci_device=%p, request=%p, task=%p\n",
18d3d72a 552 __func__, isci_device, isci_request, task);
6f231dda 553
a5fde225
JS
554 if (task != NULL) {
555
556 spin_lock_irqsave(&task->task_state_lock, flags);
557 task->lldd_task = NULL;
558
77c852f3
JS
559 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
560
a5fde225
JS
561 isci_set_task_doneflags(task);
562
563 /* If this task is not in the abort path, call task_done. */
564 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
565
566 spin_unlock_irqrestore(&task->task_state_lock, flags);
567 task->task_done(task);
568 } else
569 spin_unlock_irqrestore(&task->task_state_lock, flags);
570 }
4dc043c4 571
77c852f3
JS
572 if (isci_request != NULL) {
573 spin_lock_irqsave(&isci_host->scic_lock, flags);
574 list_del_init(&isci_request->dev_node);
575 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
4dc043c4 576
77c852f3 577 isci_request_free(isci_host, isci_request);
4dc043c4 578 }
4dc043c4
JS
579}
580
6f231dda
DW
581/**
582 * isci_terminate_request_core() - This function will terminate the given
583 * request, and wait for it to complete. This function must only be called
584 * from a thread that can wait. Note that the request is terminated and
585 * completed (back to the host, if started there).
586 * @isci_host: This SCU.
587 * @isci_device: The target.
588 * @isci_request: The I/O request to be terminated.
589 *
6f231dda
DW
590 */
591static void isci_terminate_request_core(
592 struct isci_host *isci_host,
593 struct isci_remote_device *isci_device,
cbb65c66 594 struct isci_request *isci_request)
6f231dda 595{
cbb65c66 596 enum sci_status status = SCI_SUCCESS;
6f231dda
DW
597 bool was_terminated = false;
598 bool needs_cleanup_handling = false;
599 enum isci_request_status request_status;
77c852f3
JS
600 unsigned long flags;
601 unsigned long termination_completed = 1;
602 struct completion *io_request_completion;
603 struct sas_task *task;
6f231dda
DW
604
605 dev_dbg(&isci_host->pdev->dev,
606 "%s: device = %p; request = %p\n",
607 __func__, isci_device, isci_request);
608
6f231dda 609 spin_lock_irqsave(&isci_host->scic_lock, flags);
4dc043c4 610
77c852f3
JS
611 io_request_completion = isci_request->io_request_completion;
612
613 task = (isci_request->ttype == io_task)
614 ? isci_request_access_task(isci_request)
615 : NULL;
616
4dc043c4
JS
617 /* Note that we are not going to control
618 * the target to abort the request.
619 */
620 isci_request->complete_in_target = true;
621
6f231dda
DW
622 /* Make sure the request wasn't just sitting around signalling
623 * device condition (if the request handle is NULL, then the
624 * request completed but needed additional handling here).
625 */
67ea838d 626 if (!isci_request->terminated) {
6f231dda 627 was_terminated = true;
cbb65c66 628 needs_cleanup_handling = true;
6f231dda 629 status = scic_controller_terminate_request(
cc3dbd0a 630 &isci_host->sci,
57f20f4e 631 &isci_device->sci,
67ea838d 632 &isci_request->sci);
6f231dda
DW
633 }
634 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
635
636 /*
637 * The only time the request to terminate will
638 * fail is when the io request is completed and
639 * being aborted.
640 */
4dc043c4 641 if (status != SCI_SUCCESS) {
6f231dda
DW
642 dev_err(&isci_host->pdev->dev,
643 "%s: scic_controller_terminate_request"
644 " returned = 0x%x\n",
77c852f3
JS
645 __func__, status);
646
4dc043c4
JS
647 isci_request->io_request_completion = NULL;
648
649 } else {
6f231dda
DW
650 if (was_terminated) {
651 dev_dbg(&isci_host->pdev->dev,
77c852f3
JS
652 "%s: before completion wait (%p/%p)\n",
653 __func__, isci_request, io_request_completion);
6f231dda
DW
654
655 /* Wait here for the request to complete. */
77c852f3
JS
656 #define TERMINATION_TIMEOUT_MSEC 500
657 termination_completed
4dc043c4 658 = wait_for_completion_timeout(
77c852f3 659 io_request_completion,
4dc043c4
JS
660 msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
661
77c852f3
JS
662 if (!termination_completed) {
663
664 /* The request to terminate has timed out. */
665 spin_lock_irqsave(&isci_host->scic_lock,
666 flags);
667
668 /* Check for state changes. */
669 if (!isci_request->terminated) {
670
671 /* The best we can do is to have the
672 * request die a silent death if it
673 * ever really completes.
674 *
675 * Set the request state to "dead",
676 * and clear the task pointer so that
677 * an actual completion event callback
678 * doesn't do anything.
679 */
680 isci_request->status = dead;
681 isci_request->io_request_completion
682 = NULL;
683
684 if (isci_request->ttype == io_task) {
685
686 /* Break links with the
687 * sas_task.
688 */
689 isci_request->ttype_ptr.io_task_ptr
690 = NULL;
691 }
692 } else
693 termination_completed = 1;
694
695 spin_unlock_irqrestore(&isci_host->scic_lock,
696 flags);
4dc043c4 697
77c852f3 698 if (!termination_completed) {
4dc043c4 699
77c852f3
JS
700 dev_err(&isci_host->pdev->dev,
701 "%s: *** Timeout waiting for "
702 "termination(%p/%p)\n",
703 __func__, io_request_completion,
704 isci_request);
4dc043c4 705
77c852f3
JS
706 /* The request can no longer be referenced
707 * safely since it may go away if the
708 * termination every really does complete.
709 */
710 isci_request = NULL;
711 }
712 }
713 if (termination_completed)
4dc043c4 714 dev_dbg(&isci_host->pdev->dev,
77c852f3
JS
715 "%s: after completion wait (%p/%p)\n",
716 __func__, isci_request, io_request_completion);
4dc043c4 717 }
4dc043c4 718
77c852f3 719 if (termination_completed) {
cbb65c66 720
77c852f3 721 isci_request->io_request_completion = NULL;
6f231dda 722
77c852f3
JS
723 /* Peek at the status of the request. This will tell
724 * us if there was special handling on the request such that it
725 * needs to be detached and freed here.
726 */
727 spin_lock_irqsave(&isci_request->state_lock, flags);
728 request_status = isci_request_get_state(isci_request);
729
730 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
731 && ((request_status == aborted)
732 || (request_status == aborting)
733 || (request_status == terminating)
734 || (request_status == completed)
735 || (request_status == dead)
736 )
737 ) {
738
739 /* The completion routine won't free a request in
740 * the aborted/aborting/etc. states, so we do
741 * it here.
742 */
743 needs_cleanup_handling = true;
744 }
745 spin_unlock_irqrestore(&isci_request->state_lock, flags);
6f231dda 746
77c852f3
JS
747 }
748 if (needs_cleanup_handling)
749 isci_request_cleanup_completed_loiterer(
750 isci_host, isci_device, isci_request, task);
a5fde225 751 }
6f231dda
DW
752}
753
754/**
755 * isci_terminate_pending_requests() - This function will change the all of the
756 * requests on the given device's state to "aborting", will terminate the
757 * requests, and wait for them to complete. This function must only be
758 * called from a thread that can wait. Note that the requests are all
759 * terminated and completed (back to the host, if started there).
760 * @isci_host: This parameter specifies SCU.
761 * @isci_device: This parameter specifies the target.
762 *
6f231dda 763 */
980d3aeb
DW
764void isci_terminate_pending_requests(struct isci_host *ihost,
765 struct isci_remote_device *idev)
6f231dda 766{
980d3aeb 767 struct completion request_completion;
77c852f3 768 enum isci_request_status old_state;
980d3aeb
DW
769 unsigned long flags;
770 LIST_HEAD(list);
6f231dda 771
980d3aeb
DW
772 spin_lock_irqsave(&ihost->scic_lock, flags);
773 list_splice_init(&idev->reqs_in_process, &list);
6f231dda 774
980d3aeb
DW
775 /* assumes that isci_terminate_request_core deletes from the list */
776 while (!list_empty(&list)) {
777 struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
6f231dda 778
980d3aeb
DW
779 /* Change state to "terminating" if it is currently
780 * "started".
781 */
782 old_state = isci_request_change_started_to_newstate(ireq,
783 &request_completion,
784 terminating);
785 switch (old_state) {
786 case started:
787 case completed:
788 case aborting:
789 break;
790 default:
791 /* termination in progress, or otherwise dispositioned.
792 * We know the request was on 'list' so should be safe
793 * to move it back to reqs_in_process
794 */
795 list_move(&ireq->dev_node, &idev->reqs_in_process);
796 ireq = NULL;
797 break;
798 }
4dc043c4 799
980d3aeb
DW
800 if (!ireq)
801 continue;
802 spin_unlock_irqrestore(&ihost->scic_lock, flags);
77c852f3 803
980d3aeb 804 init_completion(&request_completion);
77c852f3 805
980d3aeb
DW
806 dev_dbg(&ihost->pdev->dev,
807 "%s: idev=%p request=%p; task=%p old_state=%d\n",
808 __func__, idev, ireq,
809 ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL,
810 old_state);
811
812 /* If the old_state is started:
813 * This request was not already being aborted. If it had been,
814 * then the aborting I/O (ie. the TMF request) would not be in
815 * the aborting state, and thus would be terminated here. Note
816 * that since the TMF completion's call to the kernel function
817 * "complete()" does not happen until the pending I/O request
818 * terminate fully completes, we do not have to implement a
819 * special wait here for already aborting requests - the
820 * termination of the TMF request will force the request
821 * to finish it's already started terminate.
822 *
823 * If old_state == completed:
824 * This request completed from the SCU hardware perspective
825 * and now just needs cleaning up in terms of freeing the
826 * request and potentially calling up to libsas.
827 *
828 * If old_state == aborting:
829 * This request has already gone through a TMF timeout, but may
830 * not have been terminated; needs cleaning up at least.
831 */
832 isci_terminate_request_core(ihost, idev, ireq);
833 spin_lock_irqsave(&ihost->scic_lock, flags);
4dc043c4 834 }
980d3aeb 835 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda
DW
836}
837
838/**
839 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
840 * Template functions.
841 * @lun: This parameter specifies the lun to be reset.
842 *
843 * status, zero indicates success.
844 */
845static int isci_task_send_lu_reset_sas(
846 struct isci_host *isci_host,
847 struct isci_remote_device *isci_device,
848 u8 *lun)
849{
850 struct isci_tmf tmf;
851 int ret = TMF_RESP_FUNC_FAILED;
852
853 dev_dbg(&isci_host->pdev->dev,
854 "%s: isci_host = %p, isci_device = %p\n",
855 __func__, isci_host, isci_device);
856 /* Send the LUN reset to the target. By the time the call returns,
857 * the TMF has fully exected in the target (in which case the return
858 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
859 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
860 */
861 isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_lun_reset, NULL,
862 NULL);
863
864 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
865 ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
866
867 if (ret == TMF_RESP_FUNC_COMPLETE)
868 dev_dbg(&isci_host->pdev->dev,
869 "%s: %p: TMF_LU_RESET passed\n",
870 __func__, isci_device);
871 else
872 dev_dbg(&isci_host->pdev->dev,
873 "%s: %p: TMF_LU_RESET failed (%x)\n",
874 __func__, isci_device, ret);
875
876 return ret;
877}
878
879/**
880 * isci_task_lu_reset() - This function is one of the SAS Domain Template
881 * functions. This is one of the Task Management functoins called by libsas,
882 * to reset the given lun. Note the assumption that while this call is
883 * executing, no I/O will be sent by the host to the device.
884 * @lun: This parameter specifies the lun to be reset.
885 *
886 * status, zero indicates success.
887 */
4393aa4e 888int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
6f231dda 889{
4393aa4e 890 struct isci_host *isci_host = dev_to_ihost(domain_device);
6f231dda
DW
891 struct isci_remote_device *isci_device = NULL;
892 int ret;
893 bool device_stopping = false;
894
4393aa4e 895 isci_device = domain_device->lldd_dev;
6f231dda 896
4393aa4e
DW
897 dev_dbg(&isci_host->pdev->dev,
898 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
6f231dda
DW
899 __func__, domain_device, isci_host, isci_device);
900
d06b487b 901 if (isci_device != NULL) {
6f231dda
DW
902 device_stopping = (isci_device->status == isci_stopping)
903 || (isci_device->status == isci_stopped);
d06b487b
DW
904 set_bit(IDEV_EH, &isci_device->flags);
905 }
6f231dda
DW
906
907 /* If there is a device reset pending on any request in the
908 * device's list, fail this LUN reset request in order to
909 * escalate to the device reset.
910 */
4393aa4e
DW
911 if (!isci_device || device_stopping ||
912 isci_device_is_reset_pending(isci_host, isci_device)) {
6f231dda 913 dev_warn(&isci_host->pdev->dev,
4393aa4e 914 "%s: No dev (%p), or "
6f231dda 915 "RESET PENDING: domain_device=%p\n",
4393aa4e 916 __func__, isci_device, domain_device);
6f231dda
DW
917 return TMF_RESP_FUNC_FAILED;
918 }
919
6f231dda
DW
920 /* Send the task management part of the reset. */
921 if (sas_protocol_ata(domain_device->tproto)) {
4393aa4e 922 ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
6f231dda
DW
923 } else
924 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
925
926 /* If the LUN reset worked, all the I/O can now be terminated. */
927 if (ret == TMF_RESP_FUNC_COMPLETE)
928 /* Terminate all I/O now. */
929 isci_terminate_pending_requests(isci_host,
980d3aeb 930 isci_device);
6f231dda 931
6f231dda
DW
932 return ret;
933}
934
935
936/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
937int isci_task_clear_nexus_port(struct asd_sas_port *port)
938{
939 return TMF_RESP_FUNC_FAILED;
940}
941
942
943
944int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
945{
946 return TMF_RESP_FUNC_FAILED;
947}
948
6f231dda
DW
949/* Task Management Functions. Must be called from process context. */
950
951/**
952 * isci_abort_task_process_cb() - This is a helper function for the abort task
953 * TMF command. It manages the request state with respect to the successful
954 * transmission / completion of the abort task request.
955 * @cb_state: This parameter specifies when this function was called - after
956 * the TMF request has been started and after it has timed-out.
957 * @tmf: This parameter specifies the TMF in progress.
958 *
959 *
960 */
961static void isci_abort_task_process_cb(
962 enum isci_tmf_cb_state cb_state,
963 struct isci_tmf *tmf,
964 void *cb_data)
965{
966 struct isci_request *old_request;
967
968 old_request = (struct isci_request *)cb_data;
969
970 dev_dbg(&old_request->isci_host->pdev->dev,
971 "%s: tmf=%p, old_request=%p\n",
972 __func__, tmf, old_request);
973
974 switch (cb_state) {
975
976 case isci_tmf_started:
977 /* The TMF has been started. Nothing to do here, since the
978 * request state was already set to "aborted" by the abort
979 * task function.
980 */
6cb4d6b3
BB
981 if ((old_request->status != aborted)
982 && (old_request->status != completed))
983 dev_err(&old_request->isci_host->pdev->dev,
984 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
985 __func__, old_request->status, tmf, old_request);
6f231dda
DW
986 break;
987
988 case isci_tmf_timed_out:
989
990 /* Set the task's state to "aborting", since the abort task
991 * function thread set it to "aborted" (above) in anticipation
992 * of the task management request working correctly. Since the
993 * timeout has now fired, the TMF request failed. We set the
994 * state such that the request completion will indicate the
995 * device is no longer present.
996 */
997 isci_request_change_state(old_request, aborting);
998 break;
999
1000 default:
1001 dev_err(&old_request->isci_host->pdev->dev,
1002 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
1003 __func__, cb_state, tmf, old_request);
1004 break;
1005 }
1006}
1007
1008/**
1009 * isci_task_abort_task() - This function is one of the SAS Domain Template
1010 * functions. This function is called by libsas to abort a specified task.
1011 * @task: This parameter specifies the SAS task to abort.
1012 *
1013 * status, zero indicates success.
1014 */
1015int isci_task_abort_task(struct sas_task *task)
1016{
4393aa4e 1017 struct isci_host *isci_host = dev_to_ihost(task->dev);
6f231dda 1018 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
a5fde225
JS
1019 struct isci_request *old_request = NULL;
1020 enum isci_request_status old_state;
6f231dda 1021 struct isci_remote_device *isci_device = NULL;
a5fde225
JS
1022 struct isci_tmf tmf;
1023 int ret = TMF_RESP_FUNC_FAILED;
1024 unsigned long flags;
1025 bool any_dev_reset = false;
1026 bool device_stopping;
6f231dda
DW
1027
1028 /* Get the isci_request reference from the task. Note that
1029 * this check does not depend on the pending request list
1030 * in the device, because tasks driving resets may land here
1031 * after completion in the core.
1032 */
4393aa4e 1033 old_request = isci_task_get_request_from_task(task, &isci_device);
6f231dda
DW
1034
1035 dev_dbg(&isci_host->pdev->dev,
1036 "%s: task = %p\n", __func__, task);
1037
1038 /* Check if the device has been / is currently being removed.
1039 * If so, no task management will be done, and the I/O will
1040 * be terminated.
1041 */
1042 device_stopping = (isci_device->status == isci_stopping)
1043 || (isci_device->status == isci_stopped);
1044
d06b487b
DW
1045 /* XXX need to fix device lookup lifetime (needs to be done
1046 * under scic_lock, among other things...), but for now assume
1047 * the device is available like the above code
1048 */
1049 set_bit(IDEV_EH, &isci_device->flags);
1050
6f231dda
DW
1051 /* This version of the driver will fail abort requests for
1052 * SATA/STP. Failing the abort request this way will cause the
1053 * SCSI error handler thread to escalate to LUN reset
1054 */
1055 if (sas_protocol_ata(task->task_proto) && !device_stopping) {
1056 dev_warn(&isci_host->pdev->dev,
1057 " task %p is for a STP/SATA device;"
1058 " returning TMF_RESP_FUNC_FAILED\n"
1059 " to cause a LUN reset...\n", task);
1060 return TMF_RESP_FUNC_FAILED;
1061 }
1062
1063 dev_dbg(&isci_host->pdev->dev,
1064 "%s: old_request == %p\n", __func__, old_request);
1065
a5fde225
JS
1066 if (!device_stopping)
1067 any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
1068
6f231dda
DW
1069 spin_lock_irqsave(&task->task_state_lock, flags);
1070
1071 /* Don't do resets to stopping devices. */
a5fde225 1072 if (device_stopping) {
6f231dda 1073
a5fde225
JS
1074 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1075 any_dev_reset = false;
6f231dda 1076
a5fde225 1077 } else /* See if there is a pending device reset for this device. */
6f231dda 1078 any_dev_reset = any_dev_reset
a5fde225 1079 || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
6f231dda
DW
1080
1081 /* If the extraction of the request reference from the task
1082 * failed, then the request has been completed (or if there is a
1083 * pending reset then this abort request function must be failed
1084 * in order to escalate to the target reset).
1085 */
a5fde225 1086 if ((old_request == NULL) || any_dev_reset) {
6f231dda
DW
1087
1088 /* If the device reset task flag is set, fail the task
1089 * management request. Otherwise, the original request
1090 * has completed.
1091 */
1092 if (any_dev_reset) {
1093
1094 /* Turn off the task's DONE to make sure this
1095 * task is escalated to a target reset.
1096 */
1097 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1098
a5fde225
JS
1099 /* Make the reset happen as soon as possible. */
1100 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1101
1102 spin_unlock_irqrestore(&task->task_state_lock, flags);
1103
6f231dda
DW
1104 /* Fail the task management request in order to
1105 * escalate to the target reset.
1106 */
1107 ret = TMF_RESP_FUNC_FAILED;
1108
1109 dev_dbg(&isci_host->pdev->dev,
1110 "%s: Failing task abort in order to "
1111 "escalate to target reset because\n"
1112 "SAS_TASK_NEED_DEV_RESET is set for "
1113 "task %p on dev %p\n",
1114 __func__, task, isci_device);
1115
6f231dda 1116
a5fde225 1117 } else {
6f231dda
DW
1118 /* The request has already completed and there
1119 * is nothing to do here other than to set the task
1120 * done bit, and indicate that the task abort function
1121 * was sucessful.
1122 */
1123 isci_set_task_doneflags(task);
1124
a5fde225 1125 spin_unlock_irqrestore(&task->task_state_lock, flags);
6f231dda 1126
a5fde225 1127 ret = TMF_RESP_FUNC_COMPLETE;
6f231dda 1128
a5fde225
JS
1129 dev_dbg(&isci_host->pdev->dev,
1130 "%s: abort task not needed for %p\n",
1131 __func__, task);
6f231dda 1132 }
6f231dda
DW
1133
1134 return ret;
1135 }
a5fde225
JS
1136 else
1137 spin_unlock_irqrestore(&task->task_state_lock, flags);
6f231dda
DW
1138
1139 spin_lock_irqsave(&isci_host->scic_lock, flags);
1140
f219f010 1141 /* Check the request status and change to "aborted" if currently
a5fde225 1142 * "starting"; if true then set the I/O kernel completion
6f231dda
DW
1143 * struct that will be triggered when the request completes.
1144 */
a5fde225
JS
1145 old_state = isci_task_validate_request_to_abort(
1146 old_request, isci_host, isci_device,
1147 &aborted_io_completion);
f219f010
JS
1148 if ((old_state != started) &&
1149 (old_state != completed) &&
1150 (old_state != aborting)) {
6f231dda
DW
1151
1152 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1153
a5fde225
JS
1154 /* The request was already being handled by someone else (because
1155 * they got to set the state away from started).
1156 */
1157 dev_dbg(&isci_host->pdev->dev,
1158 "%s: device = %p; old_request %p already being aborted\n",
1159 __func__,
1160 isci_device, old_request);
6f231dda
DW
1161
1162 return TMF_RESP_FUNC_COMPLETE;
1163 }
a5fde225
JS
1164 if ((task->task_proto == SAS_PROTOCOL_SMP)
1165 || device_stopping
1166 || old_request->complete_in_target
1167 ) {
6f231dda
DW
1168
1169 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1170
a5fde225
JS
1171 dev_dbg(&isci_host->pdev->dev,
1172 "%s: SMP request (%d)"
1173 " or device is stopping (%d)"
1174 " or complete_in_target (%d), thus no TMF\n",
1175 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
1176 device_stopping, old_request->complete_in_target);
1177
6f231dda
DW
1178 /* Set the state on the task. */
1179 isci_task_all_done(task);
1180
1181 ret = TMF_RESP_FUNC_COMPLETE;
1182
1183 /* Stopping and SMP devices are not sent a TMF, and are not
a5fde225 1184 * reset, but the outstanding I/O request is terminated below.
6f231dda 1185 */
6f231dda
DW
1186 } else {
1187 /* Fill in the tmf stucture */
c3f42feb
JS
1188 isci_task_build_abort_task_tmf(&tmf, isci_device,
1189 isci_tmf_ssp_task_abort,
1190 isci_abort_task_process_cb,
1191 old_request);
6f231dda 1192
6f231dda
DW
1193 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1194
1195 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
1196 ret = isci_task_execute_tmf(isci_host, &tmf,
1197 ISCI_ABORT_TASK_TIMEOUT_MS);
1198
a5fde225 1199 if (ret != TMF_RESP_FUNC_COMPLETE)
6f231dda
DW
1200 dev_err(&isci_host->pdev->dev,
1201 "%s: isci_task_send_tmf failed\n",
1202 __func__);
1203 }
a5fde225
JS
1204 if (ret == TMF_RESP_FUNC_COMPLETE) {
1205 old_request->complete_in_target = true;
1206
77c852f3
JS
1207 /* Clean up the request on our side, and wait for the aborted
1208 * I/O to complete.
1209 */
cbb65c66 1210 isci_terminate_request_core(isci_host, isci_device, old_request);
a5fde225 1211 }
6f231dda 1212
a5fde225
JS
1213 /* Make sure we do not leave a reference to aborted_io_completion */
1214 old_request->io_request_completion = NULL;
6f231dda
DW
1215 return ret;
1216}
1217
1218/**
1219 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1220 * functions. This is one of the Task Management functoins called by libsas,
1221 * to abort all task for the given lun.
1222 * @d_device: This parameter specifies the domain device associated with this
1223 * request.
1224 * @lun: This parameter specifies the lun associated with this request.
1225 *
1226 * status, zero indicates success.
1227 */
1228int isci_task_abort_task_set(
1229 struct domain_device *d_device,
1230 u8 *lun)
1231{
1232 return TMF_RESP_FUNC_FAILED;
1233}
1234
1235
1236/**
1237 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1238 * functions. This is one of the Task Management functoins called by libsas.
1239 * @d_device: This parameter specifies the domain device associated with this
1240 * request.
1241 * @lun: This parameter specifies the lun associated with this request.
1242 *
1243 * status, zero indicates success.
1244 */
1245int isci_task_clear_aca(
1246 struct domain_device *d_device,
1247 u8 *lun)
1248{
1249 return TMF_RESP_FUNC_FAILED;
1250}
1251
1252
1253
1254/**
1255 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1256 * functions. This is one of the Task Management functoins called by libsas.
1257 * @d_device: This parameter specifies the domain device associated with this
1258 * request.
1259 * @lun: This parameter specifies the lun associated with this request.
1260 *
1261 * status, zero indicates success.
1262 */
1263int isci_task_clear_task_set(
1264 struct domain_device *d_device,
1265 u8 *lun)
1266{
1267 return TMF_RESP_FUNC_FAILED;
1268}
1269
1270
1271/**
1272 * isci_task_query_task() - This function is implemented to cause libsas to
1273 * correctly escalate the failed abort to a LUN or target reset (this is
1274 * because sas_scsi_find_task libsas function does not correctly interpret
1275 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1276 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1277 * returned, libsas will turn this into a target reset
1278 * @task: This parameter specifies the sas task being queried.
1279 * @lun: This parameter specifies the lun associated with this request.
1280 *
1281 * status, zero indicates success.
1282 */
1283int isci_task_query_task(
1284 struct sas_task *task)
1285{
1286 /* See if there is a pending device reset for this device. */
1287 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1288 return TMF_RESP_FUNC_FAILED;
1289 else
1290 return TMF_RESP_FUNC_SUCC;
1291}
1292
af5ae893 1293/*
6f231dda
DW
1294 * isci_task_request_complete() - This function is called by the sci core when
1295 * an task request completes.
af5ae893
DJ
1296 * @ihost: This parameter specifies the ISCI host object
1297 * @ireq: This parameter is the completed isci_request object.
6f231dda
DW
1298 * @completion_status: This parameter specifies the completion status from the
1299 * sci core.
1300 *
1301 * none.
1302 */
af5ae893
DJ
1303void
1304isci_task_request_complete(struct isci_host *ihost,
1305 struct isci_request *ireq,
1306 enum sci_task_status completion_status)
6f231dda 1307{
af5ae893 1308 struct isci_remote_device *idev = ireq->isci_device;
af5ae893 1309 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
6f231dda 1310 struct completion *tmf_complete;
67ea838d 1311 struct scic_sds_request *sci_req = &ireq->sci;
6f231dda 1312
af5ae893 1313 dev_dbg(&ihost->pdev->dev,
6f231dda 1314 "%s: request = %p, status=%d\n",
af5ae893 1315 __func__, ireq, completion_status);
6f231dda 1316
8d2c65c0 1317 isci_request_change_state(ireq, completed);
6f231dda
DW
1318
1319 tmf->status = completion_status;
af5ae893 1320 ireq->complete_in_target = true;
6f231dda 1321
af5ae893 1322 if (tmf->proto == SAS_PROTOCOL_SSP) {
6f231dda 1323 memcpy(&tmf->resp.resp_iu,
b7645818 1324 &sci_req->ssp.rsp,
af5ae893
DJ
1325 SSP_RESP_IU_MAX_SIZE);
1326 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
6f231dda 1327 memcpy(&tmf->resp.d2h_fis,
b7645818 1328 &sci_req->stp.rsp,
f2f30080 1329 sizeof(struct dev_to_host_fis));
6f231dda
DW
1330 }
1331
6f231dda
DW
1332 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1333 tmf_complete = tmf->complete;
1334
67ea838d
DW
1335 scic_controller_complete_io(&ihost->sci, &idev->sci, &ireq->sci);
1336 /* set the 'terminated' flag handle to make sure it cannot be terminated
6f231dda
DW
1337 * or completed again.
1338 */
67ea838d 1339 ireq->terminated = true;;
6f231dda 1340
af5ae893
DJ
1341 isci_request_change_state(ireq, unallocated);
1342 list_del_init(&ireq->dev_node);
6f231dda
DW
1343
1344 /* The task management part completes last. */
1345 complete(tmf_complete);
1346}
1347
61aaff49
JS
1348static void isci_smp_task_timedout(unsigned long _task)
1349{
1350 struct sas_task *task = (void *) _task;
1351 unsigned long flags;
1352
1353 spin_lock_irqsave(&task->task_state_lock, flags);
1354 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1355 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1356 spin_unlock_irqrestore(&task->task_state_lock, flags);
1357
1358 complete(&task->completion);
1359}
1360
1361static void isci_smp_task_done(struct sas_task *task)
1362{
1363 if (!del_timer(&task->timer))
1364 return;
1365 complete(&task->completion);
1366}
1367
1368static struct sas_task *isci_alloc_task(void)
1369{
1370 struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL);
1371
1372 if (task) {
1373 INIT_LIST_HEAD(&task->list);
1374 spin_lock_init(&task->task_state_lock);
1375 task->task_state_flags = SAS_TASK_STATE_PENDING;
1376 init_timer(&task->timer);
1377 init_completion(&task->completion);
1378 }
1379
1380 return task;
1381}
1382
1383static void isci_free_task(struct isci_host *ihost, struct sas_task *task)
1384{
1385 if (task) {
1386 BUG_ON(!list_empty(&task->list));
1387 kfree(task);
1388 }
1389}
1390
1391static int isci_smp_execute_task(struct isci_host *ihost,
1392 struct domain_device *dev, void *req,
1393 int req_size, void *resp, int resp_size)
1394{
1395 int res, retry;
1396 struct sas_task *task = NULL;
1397
1398 for (retry = 0; retry < 3; retry++) {
1399 task = isci_alloc_task();
1400 if (!task)
1401 return -ENOMEM;
1402
1403 task->dev = dev;
1404 task->task_proto = dev->tproto;
1405 sg_init_one(&task->smp_task.smp_req, req, req_size);
1406 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
1407
1408 task->task_done = isci_smp_task_done;
1409
1410 task->timer.data = (unsigned long) task;
1411 task->timer.function = isci_smp_task_timedout;
1412 task->timer.expires = jiffies + 10*HZ;
1413 add_timer(&task->timer);
1414
1415 res = isci_task_execute_task(task, 1, GFP_KERNEL);
1416
1417 if (res) {
1418 del_timer(&task->timer);
1419 dev_err(&ihost->pdev->dev,
1420 "%s: executing SMP task failed:%d\n",
1421 __func__, res);
1422 goto ex_err;
1423 }
1424
1425 wait_for_completion(&task->completion);
1426 res = -ECOMM;
1427 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1428 dev_err(&ihost->pdev->dev,
1429 "%s: smp task timed out or aborted\n",
1430 __func__);
1431 isci_task_abort_task(task);
1432 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1433 dev_err(&ihost->pdev->dev,
1434 "%s: SMP task aborted and not done\n",
1435 __func__);
1436 goto ex_err;
1437 }
1438 }
1439 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1440 task->task_status.stat == SAM_STAT_GOOD) {
1441 res = 0;
1442 break;
1443 }
1444 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1445 task->task_status.stat == SAS_DATA_UNDERRUN) {
1446 /* no error, but return the number of bytes of
1447 * underrun */
1448 res = task->task_status.residual;
1449 break;
1450 }
1451 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1452 task->task_status.stat == SAS_DATA_OVERRUN) {
1453 res = -EMSGSIZE;
1454 break;
1455 } else {
1456 dev_err(&ihost->pdev->dev,
1457 "%s: task to dev %016llx response: 0x%x "
1458 "status 0x%x\n", __func__,
1459 SAS_ADDR(dev->sas_addr),
1460 task->task_status.resp,
1461 task->task_status.stat);
1462 isci_free_task(ihost, task);
1463 task = NULL;
1464 }
1465 }
1466ex_err:
1467 BUG_ON(retry == 3 && task != NULL);
1468 isci_free_task(ihost, task);
1469 return res;
1470}
1471
1472#define DISCOVER_REQ_SIZE 16
1473#define DISCOVER_RESP_SIZE 56
1474
1475int isci_smp_get_phy_attached_dev_type(struct isci_host *ihost,
1476 struct domain_device *dev,
1477 int phy_id, int *adt)
1478{
1479 struct smp_resp *disc_resp;
1480 u8 *disc_req;
1481 int res;
1482
1483 disc_resp = kzalloc(DISCOVER_RESP_SIZE, GFP_KERNEL);
1484 if (!disc_resp)
1485 return -ENOMEM;
1486
1487 disc_req = kzalloc(DISCOVER_REQ_SIZE, GFP_KERNEL);
1488 if (disc_req) {
1489 disc_req[0] = SMP_REQUEST;
1490 disc_req[1] = SMP_DISCOVER;
1491 disc_req[9] = phy_id;
1492 } else {
1493 kfree(disc_resp);
1494 return -ENOMEM;
1495 }
1496 res = isci_smp_execute_task(ihost, dev, disc_req, DISCOVER_REQ_SIZE,
1497 disc_resp, DISCOVER_RESP_SIZE);
1498 if (!res) {
1499 if (disc_resp->result != SMP_RESP_FUNC_ACC)
1500 res = disc_resp->result;
1501 else
1502 *adt = disc_resp->disc.attached_dev_type;
1503 }
1504 kfree(disc_req);
1505 kfree(disc_resp);
1506
1507 return res;
1508}
1509
1510static void isci_wait_for_smp_phy_reset(struct isci_remote_device *idev, int phy_num)
1511{
1512 struct domain_device *dev = idev->domain_dev;
1513 struct isci_port *iport = idev->isci_port;
1514 struct isci_host *ihost = iport->isci_host;
1515 int res, iteration = 0, attached_device_type;
1516 #define STP_WAIT_MSECS 25000
1517 unsigned long tmo = msecs_to_jiffies(STP_WAIT_MSECS);
1518 unsigned long deadline = jiffies + tmo;
1519 enum {
1520 SMP_PHYWAIT_PHYDOWN,
1521 SMP_PHYWAIT_PHYUP,
1522 SMP_PHYWAIT_DONE
1523 } phy_state = SMP_PHYWAIT_PHYDOWN;
1524
1525 /* While there is time, wait for the phy to go away and come back */
1526 while (time_is_after_jiffies(deadline) && phy_state != SMP_PHYWAIT_DONE) {
1527 int event = atomic_read(&iport->event);
1528
1529 ++iteration;
1530
1531 tmo = wait_event_timeout(ihost->eventq,
1532 event != atomic_read(&iport->event) ||
1533 !test_bit(IPORT_BCN_BLOCKED, &iport->flags),
1534 tmo);
1535 /* link down, stop polling */
1536 if (!test_bit(IPORT_BCN_BLOCKED, &iport->flags))
1537 break;
1538
1539 dev_dbg(&ihost->pdev->dev,
1540 "%s: iport %p, iteration %d,"
1541 " phase %d: time_remaining %lu, bcns = %d\n",
1542 __func__, iport, iteration, phy_state,
1543 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags));
1544
1545 res = isci_smp_get_phy_attached_dev_type(ihost, dev, phy_num,
1546 &attached_device_type);
1547 tmo = deadline - jiffies;
1548
1549 if (res) {
1550 dev_warn(&ihost->pdev->dev,
1551 "%s: iteration %d, phase %d:"
1552 " SMP error=%d, time_remaining=%lu\n",
1553 __func__, iteration, phy_state, res, tmo);
1554 break;
1555 }
1556 dev_dbg(&ihost->pdev->dev,
1557 "%s: iport %p, iteration %d,"
1558 " phase %d: time_remaining %lu, bcns = %d, "
1559 "attdevtype = %x\n",
1560 __func__, iport, iteration, phy_state,
1561 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags),
1562 attached_device_type);
1563
1564 switch (phy_state) {
1565 case SMP_PHYWAIT_PHYDOWN:
1566 /* Has the device gone away? */
1567 if (!attached_device_type)
1568 phy_state = SMP_PHYWAIT_PHYUP;
1569
1570 break;
1571
1572 case SMP_PHYWAIT_PHYUP:
1573 /* Has the device come back? */
1574 if (attached_device_type)
1575 phy_state = SMP_PHYWAIT_DONE;
1576 break;
1577
1578 case SMP_PHYWAIT_DONE:
1579 break;
1580 }
1581
1582 }
1583 dev_dbg(&ihost->pdev->dev, "%s: done\n", __func__);
1584}
1585
d06b487b 1586static int isci_reset_device(struct domain_device *dev, int hard_reset)
6f231dda 1587{
d06b487b
DW
1588 struct isci_remote_device *idev = dev->lldd_dev;
1589 struct sas_phy *phy = sas_find_local_phy(dev);
1590 struct isci_host *ihost = dev_to_ihost(dev);
61aaff49 1591 struct isci_port *iport = idev->isci_port;
6f231dda 1592 enum sci_status status;
d06b487b
DW
1593 unsigned long flags;
1594 int rc;
6f231dda 1595
d06b487b 1596 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
6f231dda 1597
d06b487b
DW
1598 if (!idev) {
1599 dev_warn(&ihost->pdev->dev,
1600 "%s: idev is GONE!\n",
6f231dda
DW
1601 __func__);
1602
1603 return TMF_RESP_FUNC_COMPLETE; /* Nothing to reset. */
1604 }
1605
d06b487b
DW
1606 spin_lock_irqsave(&ihost->scic_lock, flags);
1607 status = scic_remote_device_reset(&idev->sci);
6f231dda 1608 if (status != SCI_SUCCESS) {
d06b487b 1609 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda 1610
d06b487b
DW
1611 dev_warn(&ihost->pdev->dev,
1612 "%s: scic_remote_device_reset(%p) returned %d!\n",
1613 __func__, idev, status);
6f231dda
DW
1614
1615 return TMF_RESP_FUNC_FAILED;
1616 }
d06b487b 1617 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda 1618
6f231dda 1619 /* Make sure all pending requests are able to be fully terminated. */
d06b487b 1620 isci_device_clear_reset_pending(ihost, idev);
6f231dda 1621
61aaff49
JS
1622 /* If this is a device on an expander, disable BCN processing. */
1623 if (!scsi_is_sas_phy_local(phy))
1624 set_bit(IPORT_BCN_BLOCKED, &iport->flags);
1625
d06b487b 1626 rc = sas_phy_reset(phy, hard_reset);
6f231dda 1627
d06b487b
DW
1628 /* Terminate in-progress I/O now. */
1629 isci_remote_device_nuke_requests(ihost, idev);
6f231dda 1630
ff717ab0 1631 /* Since all pending TCs have been cleaned, resume the RNC. */
d06b487b
DW
1632 spin_lock_irqsave(&ihost->scic_lock, flags);
1633 status = scic_remote_device_reset_complete(&idev->sci);
1634 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda 1635
61aaff49
JS
1636 /* If this is a device on an expander, bring the phy back up. */
1637 if (!scsi_is_sas_phy_local(phy)) {
1638 /* A phy reset will cause the device to go away then reappear.
1639 * Since libsas will take action on incoming BCNs (eg. remove
1640 * a device going through an SMP phy-control driven reset),
1641 * we need to wait until the phy comes back up before letting
1642 * discovery proceed in libsas.
1643 */
1644 isci_wait_for_smp_phy_reset(idev, phy->number);
1645
1646 spin_lock_irqsave(&ihost->scic_lock, flags);
1647 isci_port_bcn_enable(ihost, idev->isci_port);
1648 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1649 }
ff717ab0 1650
d06b487b
DW
1651 if (status != SCI_SUCCESS) {
1652 dev_warn(&ihost->pdev->dev,
1653 "%s: scic_remote_device_reset_complete(%p) "
1654 "returned %d!\n", __func__, idev, status);
6f231dda
DW
1655 }
1656
d06b487b 1657 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
6f231dda 1658
d06b487b
DW
1659 return rc;
1660}
6f231dda 1661
d06b487b
DW
1662int isci_task_I_T_nexus_reset(struct domain_device *dev)
1663{
1664 struct isci_host *ihost = dev_to_ihost(dev);
1665 int ret = TMF_RESP_FUNC_FAILED, hard_reset = 1;
1666 struct isci_remote_device *idev;
1667 unsigned long flags;
1668
1669 /* XXX mvsas is not protecting against ->lldd_dev_gone(), are we
1670 * being too paranoid, or is mvsas busted?!
1671 */
1672 spin_lock_irqsave(&ihost->scic_lock, flags);
1673 idev = dev->lldd_dev;
1674 if (!idev || !test_bit(IDEV_EH, &idev->flags))
1675 ret = TMF_RESP_FUNC_COMPLETE;
1676 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1677
1678 if (ret == TMF_RESP_FUNC_COMPLETE)
1679 return ret;
1680
1681 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1682 hard_reset = 0;
1683
1684 return isci_reset_device(dev, hard_reset);
1685}
1686
1687int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1688{
1689 struct domain_device *dev = sdev_to_domain_dev(cmd->device);
1690 int hard_reset = 1;
1691
1692 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1693 hard_reset = 0;
6f231dda 1694
d06b487b 1695 return isci_reset_device(dev, hard_reset);
6f231dda 1696}
This page took 0.116617 seconds and 5 git commands to generate.