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