isci: remove sci_device_handle
[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;
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
0cf89d1d 167 if ((device && ((isci_remote_device_get_state(device) == isci_ready) ||
6f231dda
DW
168 (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;
3a97eec6 288 sci_device = to_sci_dev(isci_device);
6f231dda
DW
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,
3a97eec6 398 to_sci_dev(request->isci_device),
6f231dda
DW
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
3a97eec6 459 sci_device = to_sci_dev(isci_device);
6f231dda
DW
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. */
6f231dda 478 spin_lock_irqsave(&isci_host->scic_lock, flags);
7c40a803 479 tmf->timeout_timer = isci_timer_create(isci_host, request, isci_tmf_timeout_cb);
6f231dda
DW
480
481 /* Start the timer. */
482 if (tmf->timeout_timer)
483 isci_timer_start(tmf->timeout_timer, timeout_ms);
484 else
485 dev_warn(&isci_host->pdev->dev,
486 "%s: isci_timer_create failed!!!!\n",
487 __func__);
488
489 /* start the TMF io. */
490 status = scic_controller_start_task(
491 isci_host->core_controller,
492 sci_device,
493 request->sci_request_handle,
494 SCI_CONTROLLER_INVALID_IO_TAG
495 );
496
497 if (status != SCI_SUCCESS) {
498 dev_warn(&isci_host->pdev->dev,
499 "%s: start_io failed - status = 0x%x, request = %p\n",
500 __func__,
501 status,
502 request);
503 goto cleanup_request;
504 }
505
506 /* Call the users callback, if any. */
507 if (tmf->cb_state_func != NULL)
508 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
509
510 /* Change the state of the TMF-bearing request to "started". */
511 isci_request_change_state(request, started);
512
513 /* add the request to the remote device request list. */
514 list_add(&request->dev_node, &isci_device->reqs_in_process);
515
516 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
517
518 /* Wait for the TMF to complete, or a timeout. */
519 wait_for_completion(&completion);
520
521 isci_print_tmf(tmf);
522
523 if (tmf->status == SCI_SUCCESS)
524 ret = TMF_RESP_FUNC_COMPLETE;
525 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
526 dev_dbg(&isci_host->pdev->dev,
527 "%s: tmf.status == "
528 "SCI_FAILURE_IO_RESPONSE_VALID\n",
529 __func__);
530 ret = TMF_RESP_FUNC_COMPLETE;
531 }
532 /* Else - leave the default "failed" status alone. */
533
534 dev_dbg(&isci_host->pdev->dev,
535 "%s: completed request = %p\n",
536 __func__,
537 request);
538
539 if (request->io_request_completion != NULL) {
540
541 /* The fact that this is non-NULL for a TMF request
542 * means there is a thread waiting for this TMF to
543 * finish.
544 */
545 complete(request->io_request_completion);
546 }
547
548 spin_lock_irqsave(&isci_host->scic_lock, flags);
549
550 cleanup_request:
551
552 /* Clean up the timer if needed. */
553 if (tmf->timeout_timer) {
7c40a803 554 isci_del_timer(isci_host, tmf->timeout_timer);
6f231dda
DW
555 tmf->timeout_timer = NULL;
556 }
557
558 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
559
560 isci_request_free(isci_host, request);
561
562 return ret;
563}
564
565void isci_task_build_tmf(
566 struct isci_tmf *tmf,
567 struct isci_remote_device *isci_device,
568 enum isci_tmf_function_codes code,
569 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
570 struct isci_tmf *,
571 void *),
572 void *cb_data)
573{
574 dev_dbg(&isci_device->isci_port->isci_host->pdev->dev,
575 "%s: isci_device = %p\n", __func__, isci_device);
576
577 memset(tmf, 0, sizeof(*tmf));
578
579 tmf->device = isci_device;
580 tmf->tmf_code = code;
581 tmf->timeout_timer = NULL;
582 tmf->cb_state_func = tmf_sent_cb;
583 tmf->cb_data = cb_data;
584}
585
586static struct isci_request *isci_task_get_request_from_task(
587 struct sas_task *task,
588 struct isci_host **isci_host,
589 struct isci_remote_device **isci_device)
590{
591
592 struct isci_request *request = NULL;
593 unsigned long flags;
594
595 spin_lock_irqsave(&task->task_state_lock, flags);
596
597 request = task->lldd_task;
598
599 /* If task is already done, the request isn't valid */
600 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
601 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
602 (request != NULL)) {
603
604 if (isci_host != NULL)
605 *isci_host = request->isci_host;
606
607 if (isci_device != NULL)
608 *isci_device = request->isci_device;
609 }
610
611 spin_unlock_irqrestore(&task->task_state_lock, flags);
612
613 return request;
614}
615
616/**
617 * isci_task_validate_request_to_abort() - This function checks the given I/O
618 * against the "started" state. If the request is still "started", it's
619 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
620 * BEFORE CALLING THIS FUNCTION.
621 * @isci_request: This parameter specifies the request object to control.
622 * @isci_host: This parameter specifies the ISCI host object
623 * @isci_device: This is the device to which the request is pending.
624 * @aborted_io_completion: This is a completion structure that will be added to
625 * the request in case it is changed to aborting; this completion is
626 * triggered when the request is fully completed.
627 *
628 * Either "started" on successful change of the task status to "aborted", or
629 * "unallocated" if the task cannot be controlled.
630 */
631static enum isci_request_status isci_task_validate_request_to_abort(
632 struct isci_request *isci_request,
633 struct isci_host *isci_host,
634 struct isci_remote_device *isci_device,
635 struct completion *aborted_io_completion)
636{
637 enum isci_request_status old_state = unallocated;
638
639 /* Only abort the task if it's in the
640 * device's request_in_process list
641 */
642 if (isci_request && !list_empty(&isci_request->dev_node)) {
643 old_state = isci_request_change_started_to_aborted(
644 isci_request, aborted_io_completion);
645
646 /* Only abort requests in the started state. */
647 if (old_state != started)
648 old_state = unallocated;
649 }
650
651 return old_state;
652}
653
654static void isci_request_cleanup_completed_loiterer(
655 struct isci_host *isci_host,
656 struct isci_remote_device *isci_device,
657 struct isci_request *isci_request)
658{
659 struct sas_task *task = isci_request_access_task(isci_request);
660 unsigned long flags;
661
662 dev_dbg(&isci_host->pdev->dev,
663 "%s: isci_device=%p, request=%p, task=%p\n",
664 __func__, isci_device, isci_request,
665 isci_request->ttype_ptr.io_task_ptr);
666
667 spin_lock_irqsave(&isci_host->scic_lock, flags);
668 list_del_init(&isci_request->dev_node);
669 if (task != NULL)
670 task->lldd_task = NULL;
671 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
672
673 isci_request_free(isci_host, isci_request);
674}
675/**
676 * isci_terminate_request_core() - This function will terminate the given
677 * request, and wait for it to complete. This function must only be called
678 * from a thread that can wait. Note that the request is terminated and
679 * completed (back to the host, if started there).
680 * @isci_host: This SCU.
681 * @isci_device: The target.
682 * @isci_request: The I/O request to be terminated.
683 *
684 *
685 */
686static void isci_terminate_request_core(
687 struct isci_host *isci_host,
688 struct isci_remote_device *isci_device,
689 struct isci_request *isci_request,
690 struct completion *request_completion)
691{
692 enum sci_status status = SCI_SUCCESS;
693 bool was_terminated = false;
694 bool needs_cleanup_handling = false;
695 enum isci_request_status request_status;
696 unsigned long flags;
697
698 dev_dbg(&isci_host->pdev->dev,
699 "%s: device = %p; request = %p\n",
700 __func__, isci_device, isci_request);
701
702 /* Peek at the current status of the request. This will tell
703 * us if there was special handling on the request such that it
704 * needs to be detached and freed here.
705 */
706 spin_lock_irqsave(&isci_request->state_lock, flags);
707 request_status = isci_request_get_state(isci_request);
708
709 /* TMFs are in their own thread */
710 if ((isci_request->ttype == io_task) &&
711 ((request_status == aborted) ||
712 (request_status == aborting) ||
713 (request_status == terminating)))
714 /* The completion routine won't free a request in
715 * the aborted/aborting/terminating state, so we do
716 * it here.
717 */
718 needs_cleanup_handling = true;
719
720 spin_unlock_irqrestore(&isci_request->state_lock, flags);
721
722 spin_lock_irqsave(&isci_host->scic_lock, flags);
723 /* Make sure the request wasn't just sitting around signalling
724 * device condition (if the request handle is NULL, then the
725 * request completed but needed additional handling here).
726 */
727 if (isci_request->sci_request_handle != NULL) {
728 was_terminated = true;
729 status = scic_controller_terminate_request(
730 isci_host->core_controller,
3a97eec6 731 to_sci_dev(isci_device),
6f231dda
DW
732 isci_request->sci_request_handle
733 );
734 }
735 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
736
737 /*
738 * The only time the request to terminate will
739 * fail is when the io request is completed and
740 * being aborted.
741 */
742 if (status != SCI_SUCCESS)
743 dev_err(&isci_host->pdev->dev,
744 "%s: scic_controller_terminate_request"
745 " returned = 0x%x\n",
746 __func__,
747 status);
748 else {
749 if (was_terminated) {
750 dev_dbg(&isci_host->pdev->dev,
751 "%s: before completion wait (%p)\n",
752 __func__,
753 request_completion);
754
755 /* Wait here for the request to complete. */
756 wait_for_completion(request_completion);
757
758 dev_dbg(&isci_host->pdev->dev,
759 "%s: after completion wait (%p)\n",
760 __func__,
761 request_completion);
762 }
763
764 if (needs_cleanup_handling)
765 isci_request_cleanup_completed_loiterer(
766 isci_host, isci_device, isci_request
767 );
768 }
769}
770static void isci_terminate_request(
771 struct isci_host *isci_host,
772 struct isci_remote_device *isci_device,
773 struct isci_request *isci_request,
774 enum isci_request_status new_request_state)
775{
776 enum isci_request_status old_state;
777
778 DECLARE_COMPLETION_ONSTACK(request_completion);
779 unsigned long flags;
780
781 spin_lock_irqsave(&isci_host->scic_lock, flags);
782
783 /* Change state to "new_request_state" if it is currently "started" */
784 old_state = isci_request_change_started_to_newstate(
785 isci_request,
786 &request_completion,
787 new_request_state
788 );
789
790 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
791
792 if (old_state == started)
793 /* This request was not already being aborted. If it had been,
794 * then the aborting I/O (ie. the TMF request) would not be in
795 * the aborting state, and thus would be terminated here. Note
796 * that since the TMF completion's call to the kernel function
797 * "complete()" does not happen until the pending I/O request
798 * terminate fully completes, we do not have to implement a
799 * special wait here for already aborting requests - the
800 * termination of the TMF request will force the request
801 * to finish it's already started terminate.
802 */
803 isci_terminate_request_core(isci_host, isci_device,
804 isci_request, &request_completion);
805}
806
807/**
808 * isci_terminate_pending_requests() - This function will change the all of the
809 * requests on the given device's state to "aborting", will terminate the
810 * requests, and wait for them to complete. This function must only be
811 * called from a thread that can wait. Note that the requests are all
812 * terminated and completed (back to the host, if started there).
813 * @isci_host: This parameter specifies SCU.
814 * @isci_device: This parameter specifies the target.
815 *
816 *
817 */
818void isci_terminate_pending_requests(
819 struct isci_host *isci_host,
820 struct isci_remote_device *isci_device,
821 enum isci_request_status new_request_state)
822{
823 struct isci_request *isci_request;
824 struct sas_task *task;
825 bool done = false;
826 unsigned long flags;
827
828 dev_dbg(&isci_host->pdev->dev,
829 "%s: isci_device = %p (new request state = %d)\n",
830 __func__, isci_device, new_request_state);
831
832 #define ISCI_TERMINATE_SHOW_PENDING_REQUESTS
833 #ifdef ISCI_TERMINATE_SHOW_PENDING_REQUESTS
834 {
835 struct isci_request *request;
836
837 /* Only abort the task if it's in the
838 * device's request_in_process list
839 */
840 list_for_each_entry(request,
841 &isci_device->reqs_in_process,
842 dev_node)
843 dev_dbg(&isci_host->pdev->dev,
844 "%s: isci_device = %p; request is on "
845 "reqs_in_process list: %p\n",
846 __func__, isci_device, request);
847 }
848 #endif /* ISCI_TERMINATE_SHOW_PENDING_REQUESTS */
849
850 /* Clean up all pending requests. */
851 do {
852 spin_lock_irqsave(&isci_host->scic_lock, flags);
853
854 if (list_empty(&isci_device->reqs_in_process)) {
855
856 done = true;
857 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
858
859 dev_dbg(&isci_host->pdev->dev,
860 "%s: isci_device = %p; done.\n",
861 __func__, isci_device);
862 } else {
863 /* The list was not empty - grab the first request. */
864 isci_request = list_first_entry(
865 &isci_device->reqs_in_process,
866 struct isci_request, dev_node
867 );
868 /* Note that we are not expecting to have to control
869 * the target to abort the request.
870 */
871 isci_request->complete_in_target = true;
872
873 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
874
875 /* Get the libsas task reference. */
876 task = isci_request_access_task(isci_request);
877
878 dev_dbg(&isci_host->pdev->dev,
879 "%s: isci_device=%p request=%p; task=%p\n",
880 __func__, isci_device, isci_request, task);
881
882 /* Mark all still pending I/O with the selected next
883 * state.
884 */
885 isci_terminate_request(isci_host, isci_device,
886 isci_request, new_request_state
887 );
888
889 /* Set the 'done' state on the task. */
890 if (task)
891 isci_task_all_done(task);
892 }
893 } while (!done);
894}
895
896/**
897 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
898 * Template functions.
899 * @lun: This parameter specifies the lun to be reset.
900 *
901 * status, zero indicates success.
902 */
903static int isci_task_send_lu_reset_sas(
904 struct isci_host *isci_host,
905 struct isci_remote_device *isci_device,
906 u8 *lun)
907{
908 struct isci_tmf tmf;
909 int ret = TMF_RESP_FUNC_FAILED;
910
911 dev_dbg(&isci_host->pdev->dev,
912 "%s: isci_host = %p, isci_device = %p\n",
913 __func__, isci_host, isci_device);
914 /* Send the LUN reset to the target. By the time the call returns,
915 * the TMF has fully exected in the target (in which case the return
916 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
917 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
918 */
919 isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_lun_reset, NULL,
920 NULL);
921
922 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
923 ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
924
925 if (ret == TMF_RESP_FUNC_COMPLETE)
926 dev_dbg(&isci_host->pdev->dev,
927 "%s: %p: TMF_LU_RESET passed\n",
928 __func__, isci_device);
929 else
930 dev_dbg(&isci_host->pdev->dev,
931 "%s: %p: TMF_LU_RESET failed (%x)\n",
932 __func__, isci_device, ret);
933
934 return ret;
935}
936
937/**
938 * isci_task_lu_reset() - This function is one of the SAS Domain Template
939 * functions. This is one of the Task Management functoins called by libsas,
940 * to reset the given lun. Note the assumption that while this call is
941 * executing, no I/O will be sent by the host to the device.
942 * @lun: This parameter specifies the lun to be reset.
943 *
944 * status, zero indicates success.
945 */
946int isci_task_lu_reset(
947 struct domain_device *domain_device,
948 u8 *lun)
949{
950 struct isci_host *isci_host = NULL;
951 struct isci_remote_device *isci_device = NULL;
952 int ret;
953 bool device_stopping = false;
954
955 if (domain_device == NULL) {
956 pr_warn("%s: domain_device == NULL\n", __func__);
957 return TMF_RESP_FUNC_FAILED;
958 }
959
960 isci_device = isci_dev_from_domain_dev(domain_device);
961
962 if (domain_device->port != NULL)
963 isci_host = isci_host_from_sas_ha(domain_device->port->ha);
964
965 pr_debug("%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
966 __func__, domain_device, isci_host, isci_device);
967
968 if (isci_device != NULL)
969 device_stopping = (isci_device->status == isci_stopping)
970 || (isci_device->status == isci_stopped);
971
972 /* If there is a device reset pending on any request in the
973 * device's list, fail this LUN reset request in order to
974 * escalate to the device reset.
975 */
976 if ((isci_device == NULL) ||
977 (isci_host == NULL) ||
978 ((isci_host != NULL) &&
979 (isci_device != NULL) &&
980 (device_stopping ||
981 (isci_device_is_reset_pending(isci_host, isci_device))))) {
982 dev_warn(&isci_host->pdev->dev,
983 "%s: No dev (%p), no host (%p), or "
984 "RESET PENDING: domain_device=%p\n",
985 __func__, isci_device, isci_host, domain_device);
986 return TMF_RESP_FUNC_FAILED;
987 }
988
989 /* Stop I/O to the remote device. */
990 isci_device_set_host_quiesce_lock_state(isci_device, true);
991
992 /* Send the task management part of the reset. */
993 if (sas_protocol_ata(domain_device->tproto)) {
994 ret = isci_task_send_lu_reset_sata(
995 isci_host, isci_device, lun
996 );
997 } else
998 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
999
1000 /* If the LUN reset worked, all the I/O can now be terminated. */
1001 if (ret == TMF_RESP_FUNC_COMPLETE)
1002 /* Terminate all I/O now. */
1003 isci_terminate_pending_requests(isci_host,
1004 isci_device,
1005 terminating);
1006
1007 /* Resume I/O to the remote device. */
1008 isci_device_set_host_quiesce_lock_state(isci_device, false);
1009
1010 return ret;
1011}
1012
1013
1014/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
1015int isci_task_clear_nexus_port(struct asd_sas_port *port)
1016{
1017 return TMF_RESP_FUNC_FAILED;
1018}
1019
1020
1021
1022int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
1023{
1024 return TMF_RESP_FUNC_FAILED;
1025}
1026
1027int isci_task_I_T_nexus_reset(struct domain_device *dev)
1028{
1029 return TMF_RESP_FUNC_FAILED;
1030}
1031
1032
1033/* Task Management Functions. Must be called from process context. */
1034
1035/**
1036 * isci_abort_task_process_cb() - This is a helper function for the abort task
1037 * TMF command. It manages the request state with respect to the successful
1038 * transmission / completion of the abort task request.
1039 * @cb_state: This parameter specifies when this function was called - after
1040 * the TMF request has been started and after it has timed-out.
1041 * @tmf: This parameter specifies the TMF in progress.
1042 *
1043 *
1044 */
1045static void isci_abort_task_process_cb(
1046 enum isci_tmf_cb_state cb_state,
1047 struct isci_tmf *tmf,
1048 void *cb_data)
1049{
1050 struct isci_request *old_request;
1051
1052 old_request = (struct isci_request *)cb_data;
1053
1054 dev_dbg(&old_request->isci_host->pdev->dev,
1055 "%s: tmf=%p, old_request=%p\n",
1056 __func__, tmf, old_request);
1057
1058 switch (cb_state) {
1059
1060 case isci_tmf_started:
1061 /* The TMF has been started. Nothing to do here, since the
1062 * request state was already set to "aborted" by the abort
1063 * task function.
1064 */
1065 BUG_ON(old_request->status != aborted);
1066 break;
1067
1068 case isci_tmf_timed_out:
1069
1070 /* Set the task's state to "aborting", since the abort task
1071 * function thread set it to "aborted" (above) in anticipation
1072 * of the task management request working correctly. Since the
1073 * timeout has now fired, the TMF request failed. We set the
1074 * state such that the request completion will indicate the
1075 * device is no longer present.
1076 */
1077 isci_request_change_state(old_request, aborting);
1078 break;
1079
1080 default:
1081 dev_err(&old_request->isci_host->pdev->dev,
1082 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
1083 __func__, cb_state, tmf, old_request);
1084 break;
1085 }
1086}
1087
1088/**
1089 * isci_task_abort_task() - This function is one of the SAS Domain Template
1090 * functions. This function is called by libsas to abort a specified task.
1091 * @task: This parameter specifies the SAS task to abort.
1092 *
1093 * status, zero indicates success.
1094 */
1095int isci_task_abort_task(struct sas_task *task)
1096{
1097 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
1098 struct isci_request *old_request = NULL;
1099 struct isci_remote_device *isci_device = NULL;
1100 struct isci_host *isci_host = NULL;
1101 struct isci_tmf tmf;
1102 int ret = TMF_RESP_FUNC_FAILED;
1103 unsigned long flags;
1104 bool any_dev_reset, device_stopping;
1105
1106 /* Get the isci_request reference from the task. Note that
1107 * this check does not depend on the pending request list
1108 * in the device, because tasks driving resets may land here
1109 * after completion in the core.
1110 */
1111 old_request = isci_task_get_request_from_task(task, &isci_host,
1112 &isci_device);
1113
1114 dev_dbg(&isci_host->pdev->dev,
1115 "%s: task = %p\n", __func__, task);
1116
1117 /* Check if the device has been / is currently being removed.
1118 * If so, no task management will be done, and the I/O will
1119 * be terminated.
1120 */
1121 device_stopping = (isci_device->status == isci_stopping)
1122 || (isci_device->status == isci_stopped);
1123
1124#ifdef NOMORE
1125 /* This abort task function is the first stop of the libsas error
1126 * handler thread. Since libsas is executing in a thread with a
1127 * referernce to the "task" parameter, that task cannot be completed
1128 * directly back to the upper layers. In order to make sure that
1129 * the task is managed correctly if this abort task fails, set the
1130 * "SAS_TASK_STATE_ABORTED" bit now such that completions up the
1131 * stack will be intercepted and only allowed to happen in the
1132 * libsas SCSI error handler thread.
1133 */
1134 spin_lock_irqsave(&task->task_state_lock, flags);
1135 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1136 spin_unlock_irqrestore(&task->task_state_lock, flags);
1137#endif /* NOMORE */
1138
1139 /* This version of the driver will fail abort requests for
1140 * SATA/STP. Failing the abort request this way will cause the
1141 * SCSI error handler thread to escalate to LUN reset
1142 */
1143 if (sas_protocol_ata(task->task_proto) && !device_stopping) {
1144 dev_warn(&isci_host->pdev->dev,
1145 " task %p is for a STP/SATA device;"
1146 " returning TMF_RESP_FUNC_FAILED\n"
1147 " to cause a LUN reset...\n", task);
1148 return TMF_RESP_FUNC_FAILED;
1149 }
1150
1151 dev_dbg(&isci_host->pdev->dev,
1152 "%s: old_request == %p\n", __func__, old_request);
1153
1154 spin_lock_irqsave(&task->task_state_lock, flags);
1155
1156 /* Don't do resets to stopping devices. */
1157 if (device_stopping)
1158 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1159
1160 /* See if there is a pending device reset for this device. */
1161 any_dev_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
1162
1163 spin_unlock_irqrestore(&task->task_state_lock, flags);
1164
1165 if ((isci_device != NULL) && !device_stopping)
1166 any_dev_reset = any_dev_reset
1167 || isci_device_is_reset_pending(isci_host,
1168 isci_device
1169 );
1170
1171 /* If the extraction of the request reference from the task
1172 * failed, then the request has been completed (or if there is a
1173 * pending reset then this abort request function must be failed
1174 * in order to escalate to the target reset).
1175 */
1176 if ((old_request == NULL) ||
1177 ((old_request != NULL) &&
1178 (old_request->sci_request_handle == NULL) &&
1179 (old_request->complete_in_target)) ||
1180 any_dev_reset) {
1181
1182 spin_lock_irqsave(&task->task_state_lock, flags);
1183
1184 /* If the device reset task flag is set, fail the task
1185 * management request. Otherwise, the original request
1186 * has completed.
1187 */
1188 if (any_dev_reset) {
1189
1190 /* Turn off the task's DONE to make sure this
1191 * task is escalated to a target reset.
1192 */
1193 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1194
1195 /* Fail the task management request in order to
1196 * escalate to the target reset.
1197 */
1198 ret = TMF_RESP_FUNC_FAILED;
1199
1200 dev_dbg(&isci_host->pdev->dev,
1201 "%s: Failing task abort in order to "
1202 "escalate to target reset because\n"
1203 "SAS_TASK_NEED_DEV_RESET is set for "
1204 "task %p on dev %p\n",
1205 __func__, task, isci_device);
1206
1207 } else {
1208 ret = TMF_RESP_FUNC_COMPLETE;
1209
1210 dev_dbg(&isci_host->pdev->dev,
1211 "%s: abort task not needed for %p\n",
1212 __func__, task);
1213
1214 /* The request has already completed and there
1215 * is nothing to do here other than to set the task
1216 * done bit, and indicate that the task abort function
1217 * was sucessful.
1218 */
1219 isci_set_task_doneflags(task);
1220
1221 /* Set the abort bit to make sure that libsas sticks the
1222 * task in the completed task queue.
1223 */
1224/* task->task_state_flags |= SAS_TASK_STATE_ABORTED; */
1225
1226 /* Check for the situation where the request was
1227 * left around on the device list but the
1228 * request already completed.
1229 */
1230 if (old_request && !old_request->sci_request_handle) {
1231
1232 isci_request_cleanup_completed_loiterer(
1233 isci_host, isci_device, old_request
1234 );
1235 }
1236 }
1237 spin_unlock_irqrestore(&task->task_state_lock, flags);
1238
1239 return ret;
1240 }
1241
1242 spin_lock_irqsave(&isci_host->scic_lock, flags);
1243
1244 /* Sanity check the request status, and set the I/O kernel completion
1245 * struct that will be triggered when the request completes.
1246 */
1247 if (isci_task_validate_request_to_abort(
1248 old_request,
1249 isci_host,
1250 isci_device,
1251 &aborted_io_completion)
1252 == unallocated) {
1253 dev_dbg(&isci_host->pdev->dev,
1254 "%s: old_request not valid for device = %p\n",
1255 __func__,
1256 isci_device);
1257 old_request = NULL;
1258 }
1259
1260 if (!old_request) {
1261
1262 /* There is no isci_request attached to the sas_task.
1263 * It must have been completed and detached.
1264 */
1265 dev_dbg(&isci_host->pdev->dev,
1266 "%s: old_request == NULL\n",
1267 __func__);
1268
1269 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1270
1271 /* Set the state on the task. */
1272 isci_task_all_done(task);
1273
1274 return TMF_RESP_FUNC_COMPLETE;
1275 }
1276 if (task->task_proto == SAS_PROTOCOL_SMP || device_stopping) {
1277
1278 if (device_stopping)
1279 dev_dbg(&isci_host->pdev->dev,
1280 "%s: device is stopping, thus no TMF\n",
1281 __func__);
1282 else
1283 dev_dbg(&isci_host->pdev->dev,
1284 "%s: request is SMP, thus no TMF\n",
1285 __func__);
1286
1287 old_request->complete_in_target = true;
1288
1289 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1290
1291 /* Set the state on the task. */
1292 isci_task_all_done(task);
1293
1294 ret = TMF_RESP_FUNC_COMPLETE;
1295
1296 /* Stopping and SMP devices are not sent a TMF, and are not
1297 * reset, but the outstanding I/O request is terminated here.
1298 *
1299 * Clean up the request on our side, and wait for the aborted
1300 * I/O to complete.
1301 */
1302 isci_terminate_request_core(isci_host, isci_device, old_request,
1303 &aborted_io_completion);
1304 } else {
1305 /* Fill in the tmf stucture */
1306 isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_task_abort,
1307 isci_abort_task_process_cb, old_request);
1308
1309 tmf.io_tag = scic_io_request_get_io_tag(
1310 old_request->sci_request_handle
1311 );
1312
1313 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1314
1315 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
1316 ret = isci_task_execute_tmf(isci_host, &tmf,
1317 ISCI_ABORT_TASK_TIMEOUT_MS);
1318
1319 if (ret == TMF_RESP_FUNC_COMPLETE) {
1320 old_request->complete_in_target = true;
1321
1322 /* Clean up the request on our side, and wait for the aborted I/O to
1323 * complete.
1324 */
1325 isci_terminate_request_core(isci_host, isci_device, old_request,
1326 &aborted_io_completion);
1327
1328 /* Set the state on the task. */
1329 isci_task_all_done(task);
1330 } else
1331 dev_err(&isci_host->pdev->dev,
1332 "%s: isci_task_send_tmf failed\n",
1333 __func__);
1334 }
1335
1336 return ret;
1337}
1338
1339/**
1340 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1341 * functions. This is one of the Task Management functoins called by libsas,
1342 * to abort all task for the given lun.
1343 * @d_device: This parameter specifies the domain device associated with this
1344 * request.
1345 * @lun: This parameter specifies the lun associated with this request.
1346 *
1347 * status, zero indicates success.
1348 */
1349int isci_task_abort_task_set(
1350 struct domain_device *d_device,
1351 u8 *lun)
1352{
1353 return TMF_RESP_FUNC_FAILED;
1354}
1355
1356
1357/**
1358 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1359 * functions. This is one of the Task Management functoins called by libsas.
1360 * @d_device: This parameter specifies the domain device associated with this
1361 * request.
1362 * @lun: This parameter specifies the lun associated with this request.
1363 *
1364 * status, zero indicates success.
1365 */
1366int isci_task_clear_aca(
1367 struct domain_device *d_device,
1368 u8 *lun)
1369{
1370 return TMF_RESP_FUNC_FAILED;
1371}
1372
1373
1374
1375/**
1376 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1377 * functions. This is one of the Task Management functoins called by libsas.
1378 * @d_device: This parameter specifies the domain device associated with this
1379 * request.
1380 * @lun: This parameter specifies the lun associated with this request.
1381 *
1382 * status, zero indicates success.
1383 */
1384int isci_task_clear_task_set(
1385 struct domain_device *d_device,
1386 u8 *lun)
1387{
1388 return TMF_RESP_FUNC_FAILED;
1389}
1390
1391
1392/**
1393 * isci_task_query_task() - This function is implemented to cause libsas to
1394 * correctly escalate the failed abort to a LUN or target reset (this is
1395 * because sas_scsi_find_task libsas function does not correctly interpret
1396 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1397 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1398 * returned, libsas will turn this into a target reset
1399 * @task: This parameter specifies the sas task being queried.
1400 * @lun: This parameter specifies the lun associated with this request.
1401 *
1402 * status, zero indicates success.
1403 */
1404int isci_task_query_task(
1405 struct sas_task *task)
1406{
1407 /* See if there is a pending device reset for this device. */
1408 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1409 return TMF_RESP_FUNC_FAILED;
1410 else
1411 return TMF_RESP_FUNC_SUCC;
1412}
1413
1414/**
1415 * isci_task_request_complete() - This function is called by the sci core when
1416 * an task request completes.
1417 * @isci_host: This parameter specifies the ISCI host object
1418 * @request: This parameter is the completed isci_request object.
1419 * @completion_status: This parameter specifies the completion status from the
1420 * sci core.
1421 *
1422 * none.
1423 */
1424void isci_task_request_complete(
1425 struct isci_host *isci_host,
1426 struct isci_request *request,
1427 enum sci_task_status completion_status)
1428{
1429 struct isci_remote_device *isci_device = request->isci_device;
1430 enum isci_request_status old_state;
1431 struct isci_tmf *tmf = isci_request_access_tmf(request);
1432 struct completion *tmf_complete;
1433
1434 dev_dbg(&isci_host->pdev->dev,
1435 "%s: request = %p, status=%d\n",
1436 __func__, request, completion_status);
1437
1438 old_state = isci_request_change_state(request, completed);
1439
1440 tmf->status = completion_status;
1441 request->complete_in_target = true;
1442
1443 if (SAS_PROTOCOL_SSP == tmf->proto) {
1444
1445 memcpy(&tmf->resp.resp_iu,
1446 scic_io_request_get_response_iu_address(
1447 request->sci_request_handle
1448 ),
1449 sizeof(struct sci_ssp_response_iu));
1450
1451 } else if (SAS_PROTOCOL_SATA == tmf->proto) {
1452
1453 memcpy(&tmf->resp.d2h_fis,
1454 scic_stp_io_request_get_d2h_reg_address(
1455 request->sci_request_handle
1456 ),
1457 sizeof(struct sata_fis_reg_d2h)
1458 );
1459 }
1460
1461 /* Manage the timer if it is still running. */
1462 if (tmf->timeout_timer) {
7c40a803 1463 isci_del_timer(isci_host, tmf->timeout_timer);
6f231dda
DW
1464 tmf->timeout_timer = NULL;
1465 }
1466
1467 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1468 tmf_complete = tmf->complete;
1469
1470 scic_controller_complete_task(
1471 isci_host->core_controller,
3a97eec6 1472 to_sci_dev(isci_device),
6f231dda
DW
1473 request->sci_request_handle
1474 );
1475 /* NULL the request handle to make sure it cannot be terminated
1476 * or completed again.
1477 */
1478 request->sci_request_handle = NULL;
1479
1480 isci_request_change_state(request, unallocated);
1481 list_del_init(&request->dev_node);
1482
1483 /* The task management part completes last. */
1484 complete(tmf_complete);
1485}
1486
1487
1488/**
1489 * isci_task_ssp_request_get_lun() - This function is called by the sci core to
1490 * retrieve the lun for a given task request.
1491 * @request: This parameter is the isci_request object.
1492 *
1493 * lun for specified task request.
1494 */
1495u32 isci_task_ssp_request_get_lun(struct isci_request *request)
1496{
1497 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1498
1499 dev_dbg(&request->isci_host->pdev->dev,
1500 "%s: lun = %d\n", __func__, isci_tmf->lun[0]);
1501/* @todo: build lun from array of bytes to 32 bit */
1502 return isci_tmf->lun[0];
1503}
1504
1505/**
1506 * isci_task_ssp_request_get_function() - This function is called by the sci
1507 * core to retrieve the function for a given task request.
1508 * @request: This parameter is the isci_request object.
1509 *
1510 * function code for specified task request.
1511 */
1512u8 isci_task_ssp_request_get_function(struct isci_request *request)
1513{
1514 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1515
1516 dev_dbg(&request->isci_host->pdev->dev,
1517 "%s: func = %d\n", __func__, isci_tmf->tmf_code);
1518
1519 return isci_tmf->tmf_code;
1520}
1521
1522/**
1523 * isci_task_ssp_request_get_io_tag_to_manage() - This function is called by
1524 * the sci core to retrieve the io tag for a given task request.
1525 * @request: This parameter is the isci_request object.
1526 *
1527 * io tag for specified task request.
1528 */
1529u16 isci_task_ssp_request_get_io_tag_to_manage(struct isci_request *request)
1530{
1531 u16 io_tag = SCI_CONTROLLER_INVALID_IO_TAG;
1532
1533 if (tmf_task == request->ttype) {
1534 struct isci_tmf *tmf = isci_request_access_tmf(request);
1535 io_tag = tmf->io_tag;
1536 }
1537
1538 dev_dbg(&request->isci_host->pdev->dev,
1539 "%s: request = %p, io_tag = %d\n",
1540 __func__, request, io_tag);
1541
1542 return io_tag;
1543}
1544
1545/**
1546 * isci_task_ssp_request_get_response_data_address() - This function is called
1547 * by the sci core to retrieve the response data address for a given task
1548 * request.
1549 * @request: This parameter is the isci_request object.
1550 *
1551 * response data address for specified task request.
1552 */
1553void *isci_task_ssp_request_get_response_data_address(
1554 struct isci_request *request)
1555{
1556 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1557
1558 return &isci_tmf->resp.resp_iu;
1559}
1560
1561/**
1562 * isci_task_ssp_request_get_response_data_length() - This function is called
1563 * by the sci core to retrieve the response data length for a given task
1564 * request.
1565 * @request: This parameter is the isci_request object.
1566 *
1567 * response data length for specified task request.
1568 */
1569u32 isci_task_ssp_request_get_response_data_length(
1570 struct isci_request *request)
1571{
1572 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1573
1574 return sizeof(isci_tmf->resp.resp_iu);
1575}
1576
1577/**
1578 * isci_bus_reset_handler() - This function performs a target reset of the
1579 * device referenced by "cmd'. This function is exported through the
1580 * "struct scsi_host_template" structure such that it is called when an I/O
1581 * recovery process has escalated to a target reset. Note that this function
1582 * is called from the scsi error handler event thread, so may block on calls.
1583 * @scsi_cmd: This parameter specifies the target to be reset.
1584 *
1585 * SUCCESS if the reset process was successful, else FAILED.
1586 */
1587int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1588{
1589 unsigned long flags = 0;
1590 struct isci_host *isci_host = NULL;
1591 enum sci_status status;
1592 int base_status;
1593 struct isci_remote_device *isci_dev
1594 = isci_dev_from_domain_dev(
1595 sdev_to_domain_dev(cmd->device));
1596
1597 dev_dbg(&cmd->device->sdev_gendev,
1598 "%s: cmd %p, isci_dev %p\n",
1599 __func__, cmd, isci_dev);
1600
1601 if (!isci_dev) {
1602 dev_warn(&cmd->device->sdev_gendev,
1603 "%s: isci_dev is GONE!\n",
1604 __func__);
1605
1606 return TMF_RESP_FUNC_COMPLETE; /* Nothing to reset. */
1607 }
1608
1609 if (isci_dev->isci_port != NULL)
1610 isci_host = isci_dev->isci_port->isci_host;
1611
1612 if (isci_host != NULL)
1613 spin_lock_irqsave(&isci_host->scic_lock, flags);
1614
3a97eec6 1615 status = scic_remote_device_reset(to_sci_dev(isci_dev));
6f231dda
DW
1616 if (status != SCI_SUCCESS) {
1617
1618 if (isci_host != NULL)
1619 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1620
1621 scmd_printk(KERN_WARNING, cmd,
1622 "%s: scic_remote_device_reset(%p) returned %d!\n",
1623 __func__, isci_dev, status);
1624
1625 return TMF_RESP_FUNC_FAILED;
1626 }
1627 if (isci_host != NULL)
1628 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1629
1630 /* Stop I/O to the remote device. */
1631 isci_device_set_host_quiesce_lock_state(isci_dev, true);
1632
1633 /* Make sure all pending requests are able to be fully terminated. */
1634 isci_device_clear_reset_pending(isci_dev);
1635
1636 /* Terminate in-progress I/O now. */
1637 isci_remote_device_nuke_requests(isci_dev);
1638
1639 /* Call into the libsas default handler (which calls sas_phy_reset). */
1640 base_status = sas_eh_bus_reset_handler(cmd);
1641
1642 if (base_status != SUCCESS) {
1643
1644 /* There can be cases where the resets to individual devices
1645 * behind an expander will fail because of an unplug of the
1646 * expander itself.
1647 */
1648 scmd_printk(KERN_WARNING, cmd,
1649 "%s: sas_eh_bus_reset_handler(%p) returned %d!\n",
1650 __func__, cmd, base_status);
1651 }
1652
1653 /* WHAT TO DO HERE IF sas_phy_reset FAILS? */
1654
1655 if (isci_host != NULL)
1656 spin_lock_irqsave(&isci_host->scic_lock, flags);
3a97eec6 1657 status = scic_remote_device_reset_complete(to_sci_dev(isci_dev));
6f231dda
DW
1658
1659 if (isci_host != NULL)
1660 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1661
1662 if (status != SCI_SUCCESS) {
1663 scmd_printk(KERN_WARNING, cmd,
1664 "%s: scic_remote_device_reset_complete(%p) "
1665 "returned %d!\n",
1666 __func__, isci_dev, status);
1667 }
1668 /* WHAT TO DO HERE IF scic_remote_device_reset_complete FAILS? */
1669
1670 dev_dbg(&cmd->device->sdev_gendev,
1671 "%s: cmd %p, isci_dev %p complete.\n",
1672 __func__, cmd, isci_dev);
1673
1674 /* Resume I/O to the remote device. */
1675 isci_device_set_host_quiesce_lock_state(isci_dev, false);
1676
1677 return TMF_RESP_FUNC_COMPLETE;
1678}
This page took 0.234898 seconds and 5 git commands to generate.