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