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