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