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