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