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