]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/scsi/isci/request.c
isci: Removing unnecessary functions in request.c
[linux-2.6.git] / drivers / scsi / isci / request.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 "isci.h"
57 #include "scic_io_request.h"
58 #include "scic_task_request.h"
59 #include "scic_port.h"
60 #include "task.h"
61 #include "request.h"
62 #include "sata.h"
63 #include "scu_completion_codes.h"
64 #include "scic_sds_request.h"
65 #include "sas.h"
66
67 static enum sci_status isci_request_ssp_request_construct(
68         struct isci_request *request)
69 {
70         enum sci_status status;
71
72         dev_dbg(&request->isci_host->pdev->dev,
73                 "%s: request = %p\n",
74                 __func__,
75                 request);
76         status = scic_io_request_construct_basic_ssp(
77                 request->sci_request_handle
78                 );
79         return status;
80 }
81
82 static enum sci_status isci_request_stp_request_construct(
83         struct isci_request *request)
84 {
85         struct sas_task *task = isci_request_access_task(request);
86         enum sci_status status;
87         struct host_to_dev_fis *register_fis;
88
89         dev_dbg(&request->isci_host->pdev->dev,
90                 "%s: request = %p\n",
91                 __func__,
92                 request);
93
94         /* Get the host_to_dev_fis from the core and copy
95          * the fis from the task into it.
96          */
97         register_fis = isci_sata_task_to_fis_copy(task);
98
99         status = scic_io_request_construct_basic_sata(
100                 request->sci_request_handle
101                 );
102
103         /* Set the ncq tag in the fis, from the queue
104          * command in the task.
105          */
106         if (isci_sata_is_task_ncq(task)) {
107
108                 isci_sata_set_ncq_tag(
109                         register_fis,
110                         task
111                         );
112         }
113
114         return status;
115 }
116
117 /*
118  * isci_smp_request_build() - This function builds the smp request.
119  * @ireq: This parameter points to the isci_request allocated in the
120  *    request construct function.
121  *
122  * SCI_SUCCESS on successfull completion, or specific failure code.
123  */
124 static enum sci_status isci_smp_request_build(struct isci_request *ireq)
125 {
126         enum sci_status status = SCI_FAILURE;
127         struct sas_task *task = isci_request_access_task(ireq);
128         struct scic_sds_request *sci_req = ireq->sci_request_handle;
129         void *cmd_iu = sci_req->command_buffer;
130
131         dev_dbg(&ireq->isci_host->pdev->dev,
132                 "%s: request = %p\n", __func__, ireq);
133
134         dev_dbg(&ireq->isci_host->pdev->dev,
135                 "%s: smp_req len = %d\n",
136                 __func__,
137                 task->smp_task.smp_req.length);
138
139         /* copy the smp_command to the address; */
140         sg_copy_to_buffer(&task->smp_task.smp_req, 1,
141                           (char *)cmd_iu,
142                           sizeof(struct smp_req));
143
144         status = scic_io_request_construct_smp(sci_req);
145         if (status != SCI_SUCCESS)
146                 dev_warn(&ireq->isci_host->pdev->dev,
147                          "%s: failed with status = %d\n",
148                          __func__,
149                          status);
150
151         return status;
152 }
153
154 /**
155  * isci_io_request_build() - This function builds the io request object.
156  * @isci_host: This parameter specifies the ISCI host object
157  * @request: This parameter points to the isci_request object allocated in the
158  *    request construct function.
159  * @sci_device: This parameter is the handle for the sci core's remote device
160  *    object that is the destination for this request.
161  *
162  * SCI_SUCCESS on successfull completion, or specific failure code.
163  */
164 static enum sci_status isci_io_request_build(
165         struct isci_host *isci_host,
166         struct isci_request *request,
167         struct isci_remote_device *isci_device)
168 {
169         enum sci_status status = SCI_SUCCESS;
170         struct sas_task *task = isci_request_access_task(request);
171         struct scic_sds_remote_device *sci_device = &isci_device->sci;
172
173         dev_dbg(&isci_host->pdev->dev,
174                 "%s: isci_device = 0x%p; request = %p, "
175                 "num_scatter = %d\n",
176                 __func__,
177                 isci_device,
178                 request,
179                 task->num_scatter);
180
181         /* map the sgl addresses, if present.
182          * libata does the mapping for sata devices
183          * before we get the request.
184          */
185         if (task->num_scatter &&
186             !sas_protocol_ata(task->task_proto) &&
187             !(SAS_PROTOCOL_SMP & task->task_proto)) {
188
189                 request->num_sg_entries = dma_map_sg(
190                         &isci_host->pdev->dev,
191                         task->scatter,
192                         task->num_scatter,
193                         task->data_dir
194                         );
195
196                 if (request->num_sg_entries == 0)
197                         return SCI_FAILURE_INSUFFICIENT_RESOURCES;
198         }
199
200         /* build the common request object. For now,
201          * we will let the core allocate the IO tag.
202          */
203         status = scic_io_request_construct(
204                 &isci_host->sci,
205                 sci_device,
206                 SCI_CONTROLLER_INVALID_IO_TAG,
207                 request,
208                 request->sci_request_mem_ptr,
209                 (struct scic_sds_request **)&request->sci_request_handle
210                 );
211
212         if (status != SCI_SUCCESS) {
213                 dev_warn(&isci_host->pdev->dev,
214                          "%s: failed request construct\n",
215                          __func__);
216                 return SCI_FAILURE;
217         }
218
219         request->sci_request_handle->ireq = request;
220
221         switch (task->task_proto) {
222         case SAS_PROTOCOL_SMP:
223                 status = isci_smp_request_build(request);
224                 break;
225         case SAS_PROTOCOL_SSP:
226                 status = isci_request_ssp_request_construct(request);
227                 break;
228         case SAS_PROTOCOL_SATA:
229         case SAS_PROTOCOL_STP:
230         case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
231                 status = isci_request_stp_request_construct(request);
232                 break;
233         default:
234                 dev_warn(&isci_host->pdev->dev,
235                          "%s: unknown protocol\n", __func__);
236                 return SCI_FAILURE;
237         }
238
239         return SCI_SUCCESS;
240 }
241
242
243 /**
244  * isci_request_alloc_core() - This function gets the request object from the
245  *    isci_host dma cache.
246  * @isci_host: This parameter specifies the ISCI host object
247  * @isci_request: This parameter will contain the pointer to the new
248  *    isci_request object.
249  * @isci_device: This parameter is the pointer to the isci remote device object
250  *    that is the destination for this request.
251  * @gfp_flags: This parameter specifies the os allocation flags.
252  *
253  * SCI_SUCCESS on successfull completion, or specific failure code.
254  */
255 static int isci_request_alloc_core(
256         struct isci_host *isci_host,
257         struct isci_request **isci_request,
258         struct isci_remote_device *isci_device,
259         gfp_t gfp_flags)
260 {
261         int ret = 0;
262         dma_addr_t handle;
263         struct isci_request *request;
264
265
266         /* get pointer to dma memory. This actually points
267          * to both the isci_remote_device object and the
268          * sci object. The isci object is at the beginning
269          * of the memory allocated here.
270          */
271         request = dma_pool_alloc(isci_host->dma_pool, gfp_flags, &handle);
272         if (!request) {
273                 dev_warn(&isci_host->pdev->dev,
274                          "%s: dma_pool_alloc returned NULL\n", __func__);
275                 return -ENOMEM;
276         }
277
278         /* initialize the request object.       */
279         spin_lock_init(&request->state_lock);
280         request->sci_request_mem_ptr = ((u8 *)request) +
281                                        sizeof(struct isci_request);
282         request->request_daddr = handle;
283         request->isci_host = isci_host;
284         request->isci_device = isci_device;
285         request->io_request_completion = NULL;
286
287         request->request_alloc_size = isci_host->dma_pool_alloc_size;
288         request->num_sg_entries = 0;
289
290         request->complete_in_target = false;
291
292         INIT_LIST_HEAD(&request->completed_node);
293         INIT_LIST_HEAD(&request->dev_node);
294
295         *isci_request = request;
296         isci_request_change_state(request, allocated);
297
298         return ret;
299 }
300
301 static int isci_request_alloc_io(
302         struct isci_host *isci_host,
303         struct sas_task *task,
304         struct isci_request **isci_request,
305         struct isci_remote_device *isci_device,
306         gfp_t gfp_flags)
307 {
308         int retval = isci_request_alloc_core(isci_host, isci_request,
309                                              isci_device, gfp_flags);
310
311         if (!retval) {
312                 (*isci_request)->ttype_ptr.io_task_ptr = task;
313                 (*isci_request)->ttype                 = io_task;
314
315                 task->lldd_task = *isci_request;
316         }
317         return retval;
318 }
319
320 /**
321  * isci_request_alloc_tmf() - This function gets the request object from the
322  *    isci_host dma cache and initializes the relevant fields as a sas_task.
323  * @isci_host: This parameter specifies the ISCI host object
324  * @sas_task: This parameter is the task struct from the upper layer driver.
325  * @isci_request: This parameter will contain the pointer to the new
326  *    isci_request object.
327  * @isci_device: This parameter is the pointer to the isci remote device object
328  *    that is the destination for this request.
329  * @gfp_flags: This parameter specifies the os allocation flags.
330  *
331  * SCI_SUCCESS on successfull completion, or specific failure code.
332  */
333 int isci_request_alloc_tmf(
334         struct isci_host *isci_host,
335         struct isci_tmf *isci_tmf,
336         struct isci_request **isci_request,
337         struct isci_remote_device *isci_device,
338         gfp_t gfp_flags)
339 {
340         int retval = isci_request_alloc_core(isci_host, isci_request,
341                                              isci_device, gfp_flags);
342
343         if (!retval) {
344
345                 (*isci_request)->ttype_ptr.tmf_task_ptr = isci_tmf;
346                 (*isci_request)->ttype = tmf_task;
347         }
348         return retval;
349 }
350
351 /**
352  * isci_request_execute() - This function allocates the isci_request object,
353  *    all fills in some common fields.
354  * @isci_host: This parameter specifies the ISCI host object
355  * @sas_task: This parameter is the task struct from the upper layer driver.
356  * @isci_request: This parameter will contain the pointer to the new
357  *    isci_request object.
358  * @gfp_flags: This parameter specifies the os allocation flags.
359  *
360  * SCI_SUCCESS on successfull completion, or specific failure code.
361  */
362 int isci_request_execute(
363         struct isci_host *isci_host,
364         struct sas_task *task,
365         struct isci_request **isci_request,
366         gfp_t gfp_flags)
367 {
368         int ret = 0;
369         struct scic_sds_remote_device *sci_device;
370         enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
371         struct isci_remote_device *isci_device;
372         struct isci_request *request;
373         unsigned long flags;
374
375         isci_device = task->dev->lldd_dev;
376         sci_device = &isci_device->sci;
377
378         /* do common allocation and init of request object. */
379         ret = isci_request_alloc_io(
380                 isci_host,
381                 task,
382                 &request,
383                 isci_device,
384                 gfp_flags
385                 );
386
387         if (ret)
388                 goto out;
389
390         status = isci_io_request_build(isci_host, request, isci_device);
391         if (status == SCI_SUCCESS) {
392
393                 spin_lock_irqsave(&isci_host->scic_lock, flags);
394
395                 /* send the request, let the core assign the IO TAG.    */
396                 status = scic_controller_start_io(
397                         &isci_host->sci,
398                         sci_device,
399                         request->sci_request_handle,
400                         SCI_CONTROLLER_INVALID_IO_TAG
401                         );
402
403                 if (status == SCI_SUCCESS ||
404                     status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
405
406                         /* Either I/O started OK, or the core has signaled that
407                          * the device needs a target reset.
408                          *
409                          * In either case, hold onto the I/O for later.
410                          *
411                          * Update it's status and add it to the list in the
412                          * remote device object.
413                          */
414                         isci_request_change_state(request, started);
415                         list_add(&request->dev_node,
416                                  &isci_device->reqs_in_process);
417
418                         if (status == SCI_SUCCESS) {
419                                 /* Save the tag for possible task mgmt later. */
420                                 request->io_tag = scic_io_request_get_io_tag(
421                                                      request->sci_request_handle);
422                         } else {
423                                 /* The request did not really start in the
424                                  * hardware, so clear the request handle
425                                  * here so no terminations will be done.
426                                  */
427                                 request->sci_request_handle = NULL;
428                         }
429
430                 } else
431                         dev_warn(&isci_host->pdev->dev,
432                                  "%s: failed request start (0x%x)\n",
433                                  __func__, status);
434
435                 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
436
437                 if (status ==
438                     SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
439                         /* Signal libsas that we need the SCSI error
440                         * handler thread to work on this I/O and that
441                         * we want a device reset.
442                         */
443                         spin_lock_irqsave(&task->task_state_lock, flags);
444                         task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
445                         spin_unlock_irqrestore(&task->task_state_lock, flags);
446
447                         /* Cause this task to be scheduled in the SCSI error
448                         * handler thread.
449                         */
450                         isci_execpath_callback(isci_host, task,
451                                                sas_task_abort);
452
453                         /* Change the status, since we are holding
454                         * the I/O until it is managed by the SCSI
455                         * error handler.
456                         */
457                         status = SCI_SUCCESS;
458                 }
459
460         } else
461                 dev_warn(&isci_host->pdev->dev,
462                          "%s: request_construct failed - status = 0x%x\n",
463                          __func__,
464                          status);
465
466  out:
467         if (status != SCI_SUCCESS) {
468                 /* release dma memory on failure. */
469                 isci_request_free(isci_host, request);
470                 request = NULL;
471                 ret = SCI_FAILURE;
472         }
473
474         *isci_request = request;
475         return ret;
476 }
477
478
479 /**
480  * isci_request_process_response_iu() - This function sets the status and
481  *    response iu, in the task struct, from the request object for the upper
482  *    layer driver.
483  * @sas_task: This parameter is the task struct from the upper layer driver.
484  * @resp_iu: This parameter points to the response iu of the completed request.
485  * @dev: This parameter specifies the linux device struct.
486  *
487  * none.
488  */
489 static void isci_request_process_response_iu(
490         struct sas_task *task,
491         struct ssp_response_iu *resp_iu,
492         struct device *dev)
493 {
494         dev_dbg(dev,
495                 "%s: resp_iu = %p "
496                 "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
497                 "resp_iu->response_data_len = %x, "
498                 "resp_iu->sense_data_len = %x\nrepsonse data: ",
499                 __func__,
500                 resp_iu,
501                 resp_iu->status,
502                 resp_iu->datapres,
503                 resp_iu->response_data_len,
504                 resp_iu->sense_data_len);
505
506         task->task_status.stat = resp_iu->status;
507
508         /* libsas updates the task status fields based on the response iu. */
509         sas_ssp_task_response(dev, task, resp_iu);
510 }
511
512 /**
513  * isci_request_set_open_reject_status() - This function prepares the I/O
514  *    completion for OPEN_REJECT conditions.
515  * @request: This parameter is the completed isci_request object.
516  * @response_ptr: This parameter specifies the service response for the I/O.
517  * @status_ptr: This parameter specifies the exec status for the I/O.
518  * @complete_to_host_ptr: This parameter specifies the action to be taken by
519  *    the LLDD with respect to completing this request or forcing an abort
520  *    condition on the I/O.
521  * @open_rej_reason: This parameter specifies the encoded reason for the
522  *    abandon-class reject.
523  *
524  * none.
525  */
526 static void isci_request_set_open_reject_status(
527         struct isci_request *request,
528         struct sas_task *task,
529         enum service_response *response_ptr,
530         enum exec_status *status_ptr,
531         enum isci_completion_selection *complete_to_host_ptr,
532         enum sas_open_rej_reason open_rej_reason)
533 {
534         /* Task in the target is done. */
535         request->complete_in_target       = true;
536         *response_ptr                     = SAS_TASK_UNDELIVERED;
537         *status_ptr                       = SAS_OPEN_REJECT;
538         *complete_to_host_ptr             = isci_perform_normal_io_completion;
539         task->task_status.open_rej_reason = open_rej_reason;
540 }
541
542 /**
543  * isci_request_handle_controller_specific_errors() - This function decodes
544  *    controller-specific I/O completion error conditions.
545  * @request: This parameter is the completed isci_request object.
546  * @response_ptr: This parameter specifies the service response for the I/O.
547  * @status_ptr: This parameter specifies the exec status for the I/O.
548  * @complete_to_host_ptr: This parameter specifies the action to be taken by
549  *    the LLDD with respect to completing this request or forcing an abort
550  *    condition on the I/O.
551  *
552  * none.
553  */
554 static void isci_request_handle_controller_specific_errors(
555         struct isci_remote_device *isci_device,
556         struct isci_request *request,
557         struct sas_task *task,
558         enum service_response *response_ptr,
559         enum exec_status *status_ptr,
560         enum isci_completion_selection *complete_to_host_ptr)
561 {
562         unsigned int cstatus;
563
564         cstatus = scic_request_get_controller_status(
565                 request->sci_request_handle
566                 );
567
568         dev_dbg(&request->isci_host->pdev->dev,
569                 "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
570                 "- controller status = 0x%x\n",
571                 __func__, request, cstatus);
572
573         /* Decode the controller-specific errors; most
574          * important is to recognize those conditions in which
575          * the target may still have a task outstanding that
576          * must be aborted.
577          *
578          * Note that there are SCU completion codes being
579          * named in the decode below for which SCIC has already
580          * done work to handle them in a way other than as
581          * a controller-specific completion code; these are left
582          * in the decode below for completeness sake.
583          */
584         switch (cstatus) {
585         case SCU_TASK_DONE_DMASETUP_DIRERR:
586         /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
587         case SCU_TASK_DONE_XFERCNT_ERR:
588                 /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
589                 if (task->task_proto == SAS_PROTOCOL_SMP) {
590                         /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
591                         *response_ptr = SAS_TASK_COMPLETE;
592
593                         /* See if the device has been/is being stopped. Note
594                          * that we ignore the quiesce state, since we are
595                          * concerned about the actual device state.
596                          */
597                         if ((isci_device->status == isci_stopping) ||
598                             (isci_device->status == isci_stopped))
599                                 *status_ptr = SAS_DEVICE_UNKNOWN;
600                         else
601                                 *status_ptr = SAS_ABORTED_TASK;
602
603                         request->complete_in_target = true;
604
605                         *complete_to_host_ptr =
606                                 isci_perform_normal_io_completion;
607                 } else {
608                         /* Task in the target is not done. */
609                         *response_ptr = SAS_TASK_UNDELIVERED;
610
611                         if ((isci_device->status == isci_stopping) ||
612                             (isci_device->status == isci_stopped))
613                                 *status_ptr = SAS_DEVICE_UNKNOWN;
614                         else
615                                 *status_ptr = SAM_STAT_TASK_ABORTED;
616
617                         request->complete_in_target = false;
618
619                         *complete_to_host_ptr =
620                                 isci_perform_error_io_completion;
621                 }
622
623                 break;
624
625         case SCU_TASK_DONE_CRC_ERR:
626         case SCU_TASK_DONE_NAK_CMD_ERR:
627         case SCU_TASK_DONE_EXCESS_DATA:
628         case SCU_TASK_DONE_UNEXP_FIS:
629         /* Also SCU_TASK_DONE_UNEXP_RESP: */
630         case SCU_TASK_DONE_VIIT_ENTRY_NV:       /* TODO - conditions? */
631         case SCU_TASK_DONE_IIT_ENTRY_NV:        /* TODO - conditions? */
632         case SCU_TASK_DONE_RNCNV_OUTBOUND:      /* TODO - conditions? */
633                 /* These are conditions in which the target
634                  * has completed the task, so that no cleanup
635                  * is necessary.
636                  */
637                 *response_ptr = SAS_TASK_COMPLETE;
638
639                 /* See if the device has been/is being stopped. Note
640                  * that we ignore the quiesce state, since we are
641                  * concerned about the actual device state.
642                  */
643                 if ((isci_device->status == isci_stopping) ||
644                     (isci_device->status == isci_stopped))
645                         *status_ptr = SAS_DEVICE_UNKNOWN;
646                 else
647                         *status_ptr = SAS_ABORTED_TASK;
648
649                 request->complete_in_target = true;
650
651                 *complete_to_host_ptr = isci_perform_normal_io_completion;
652                 break;
653
654
655         /* Note that the only open reject completion codes seen here will be
656          * abandon-class codes; all others are automatically retried in the SCU.
657          */
658         case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
659
660                 isci_request_set_open_reject_status(
661                         request, task, response_ptr, status_ptr,
662                         complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
663                 break;
664
665         case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
666
667                 /* Note - the return of AB0 will change when
668                  * libsas implements detection of zone violations.
669                  */
670                 isci_request_set_open_reject_status(
671                         request, task, response_ptr, status_ptr,
672                         complete_to_host_ptr, SAS_OREJ_RESV_AB0);
673                 break;
674
675         case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
676
677                 isci_request_set_open_reject_status(
678                         request, task, response_ptr, status_ptr,
679                         complete_to_host_ptr, SAS_OREJ_RESV_AB1);
680                 break;
681
682         case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
683
684                 isci_request_set_open_reject_status(
685                         request, task, response_ptr, status_ptr,
686                         complete_to_host_ptr, SAS_OREJ_RESV_AB2);
687                 break;
688
689         case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
690
691                 isci_request_set_open_reject_status(
692                         request, task, response_ptr, status_ptr,
693                         complete_to_host_ptr, SAS_OREJ_RESV_AB3);
694                 break;
695
696         case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
697
698                 isci_request_set_open_reject_status(
699                         request, task, response_ptr, status_ptr,
700                         complete_to_host_ptr, SAS_OREJ_BAD_DEST);
701                 break;
702
703         case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
704
705                 isci_request_set_open_reject_status(
706                         request, task, response_ptr, status_ptr,
707                         complete_to_host_ptr, SAS_OREJ_STP_NORES);
708                 break;
709
710         case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
711
712                 isci_request_set_open_reject_status(
713                         request, task, response_ptr, status_ptr,
714                         complete_to_host_ptr, SAS_OREJ_EPROTO);
715                 break;
716
717         case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
718
719                 isci_request_set_open_reject_status(
720                         request, task, response_ptr, status_ptr,
721                         complete_to_host_ptr, SAS_OREJ_CONN_RATE);
722                 break;
723
724         case SCU_TASK_DONE_LL_R_ERR:
725         /* Also SCU_TASK_DONE_ACK_NAK_TO: */
726         case SCU_TASK_DONE_LL_PERR:
727         case SCU_TASK_DONE_LL_SY_TERM:
728         /* Also SCU_TASK_DONE_NAK_ERR:*/
729         case SCU_TASK_DONE_LL_LF_TERM:
730         /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
731         case SCU_TASK_DONE_LL_ABORT_ERR:
732         case SCU_TASK_DONE_SEQ_INV_TYPE:
733         /* Also SCU_TASK_DONE_UNEXP_XR: */
734         case SCU_TASK_DONE_XR_IU_LEN_ERR:
735         case SCU_TASK_DONE_INV_FIS_LEN:
736         /* Also SCU_TASK_DONE_XR_WD_LEN: */
737         case SCU_TASK_DONE_SDMA_ERR:
738         case SCU_TASK_DONE_OFFSET_ERR:
739         case SCU_TASK_DONE_MAX_PLD_ERR:
740         case SCU_TASK_DONE_LF_ERR:
741         case SCU_TASK_DONE_SMP_RESP_TO_ERR:  /* Escalate to dev reset? */
742         case SCU_TASK_DONE_SMP_LL_RX_ERR:
743         case SCU_TASK_DONE_UNEXP_DATA:
744         case SCU_TASK_DONE_UNEXP_SDBFIS:
745         case SCU_TASK_DONE_REG_ERR:
746         case SCU_TASK_DONE_SDB_ERR:
747         case SCU_TASK_DONE_TASK_ABORT:
748         default:
749                 /* Task in the target is not done. */
750                 *response_ptr = SAS_TASK_UNDELIVERED;
751                 *status_ptr = SAM_STAT_TASK_ABORTED;
752                 request->complete_in_target = false;
753
754                 *complete_to_host_ptr = isci_perform_error_io_completion;
755                 break;
756         }
757 }
758
759 /**
760  * isci_task_save_for_upper_layer_completion() - This function saves the
761  *    request for later completion to the upper layer driver.
762  * @host: This parameter is a pointer to the host on which the the request
763  *    should be queued (either as an error or success).
764  * @request: This parameter is the completed request.
765  * @response: This parameter is the response code for the completed task.
766  * @status: This parameter is the status code for the completed task.
767  *
768  * none.
769  */
770 static void isci_task_save_for_upper_layer_completion(
771         struct isci_host *host,
772         struct isci_request *request,
773         enum service_response response,
774         enum exec_status status,
775         enum isci_completion_selection task_notification_selection)
776 {
777         struct sas_task *task = isci_request_access_task(request);
778
779         task_notification_selection
780                 = isci_task_set_completion_status(task, response, status,
781                                                   task_notification_selection);
782
783         /* Tasks aborted specifically by a call to the lldd_abort_task
784          * function should not be completed to the host in the regular path.
785          */
786         switch (task_notification_selection) {
787
788         case isci_perform_normal_io_completion:
789
790                 /* Normal notification (task_done) */
791                 dev_dbg(&host->pdev->dev,
792                         "%s: Normal - task = %p, response=%d (%d), status=%d (%d)\n",
793                         __func__,
794                         task,
795                         task->task_status.resp, response,
796                         task->task_status.stat, status);
797                 /* Add to the completed list. */
798                 list_add(&request->completed_node,
799                          &host->requests_to_complete);
800
801                 /* Take the request off the device's pending request list. */
802                 list_del_init(&request->dev_node);
803                 break;
804
805         case isci_perform_aborted_io_completion:
806                 /* No notification to libsas because this request is
807                  * already in the abort path.
808                  */
809                 dev_warn(&host->pdev->dev,
810                          "%s: Aborted - task = %p, response=%d (%d), status=%d (%d)\n",
811                          __func__,
812                          task,
813                          task->task_status.resp, response,
814                          task->task_status.stat, status);
815
816                 /* Wake up whatever process was waiting for this
817                  * request to complete.
818                  */
819                 WARN_ON(request->io_request_completion == NULL);
820
821                 if (request->io_request_completion != NULL) {
822
823                         /* Signal whoever is waiting that this
824                         * request is complete.
825                         */
826                         complete(request->io_request_completion);
827                 }
828                 break;
829
830         case isci_perform_error_io_completion:
831                 /* Use sas_task_abort */
832                 dev_warn(&host->pdev->dev,
833                          "%s: Error - task = %p, response=%d (%d), status=%d (%d)\n",
834                          __func__,
835                          task,
836                          task->task_status.resp, response,
837                          task->task_status.stat, status);
838                 /* Add to the aborted list. */
839                 list_add(&request->completed_node,
840                          &host->requests_to_errorback);
841                 break;
842
843         default:
844                 dev_warn(&host->pdev->dev,
845                          "%s: Unknown - task = %p, response=%d (%d), status=%d (%d)\n",
846                          __func__,
847                          task,
848                          task->task_status.resp, response,
849                          task->task_status.stat, status);
850
851                 /* Add to the error to libsas list. */
852                 list_add(&request->completed_node,
853                          &host->requests_to_errorback);
854                 break;
855         }
856 }
857
858 /**
859  * isci_request_io_request_complete() - This function is called by the sci core
860  *    when an io request completes.
861  * @isci_host: This parameter specifies the ISCI host object
862  * @request: This parameter is the completed isci_request object.
863  * @completion_status: This parameter specifies the completion status from the
864  *    sci core.
865  *
866  * none.
867  */
868 void isci_request_io_request_complete(
869         struct        isci_host *isci_host,
870         struct        isci_request *request,
871         enum sci_io_status completion_status)
872 {
873         struct sas_task *task = isci_request_access_task(request);
874         struct ssp_response_iu *resp_iu;
875         void *resp_buf;
876         unsigned long task_flags;
877         struct isci_remote_device *isci_device   = request->isci_device;
878         enum service_response response       = SAS_TASK_UNDELIVERED;
879         enum exec_status status         = SAS_ABORTED_TASK;
880         enum isci_request_status request_status;
881         enum isci_completion_selection complete_to_host
882                 = isci_perform_normal_io_completion;
883
884         dev_dbg(&isci_host->pdev->dev,
885                 "%s: request = %p, task = %p,\n"
886                 "task->data_dir = %d completion_status = 0x%x\n",
887                 __func__,
888                 request,
889                 task,
890                 task->data_dir,
891                 completion_status);
892
893         spin_lock(&request->state_lock);
894         request_status = isci_request_get_state(request);
895
896         /* Decode the request status.  Note that if the request has been
897          * aborted by a task management function, we don't care
898          * what the status is.
899          */
900         switch (request_status) {
901
902         case aborted:
903                 /* "aborted" indicates that the request was aborted by a task
904                  * management function, since once a task management request is
905                  * perfomed by the device, the request only completes because
906                  * of the subsequent driver terminate.
907                  *
908                  * Aborted also means an external thread is explicitly managing
909                  * this request, so that we do not complete it up the stack.
910                  *
911                  * The target is still there (since the TMF was successful).
912                  */
913                 request->complete_in_target = true;
914                 response = SAS_TASK_COMPLETE;
915
916                 /* See if the device has been/is being stopped. Note
917                  * that we ignore the quiesce state, since we are
918                  * concerned about the actual device state.
919                  */
920                 if ((isci_device->status == isci_stopping)
921                     || (isci_device->status == isci_stopped)
922                     )
923                         status = SAS_DEVICE_UNKNOWN;
924                 else
925                         status = SAS_ABORTED_TASK;
926
927                 complete_to_host = isci_perform_aborted_io_completion;
928                 /* This was an aborted request. */
929
930                 spin_unlock(&request->state_lock);
931                 break;
932
933         case aborting:
934                 /* aborting means that the task management function tried and
935                  * failed to abort the request. We need to note the request
936                  * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
937                  * target as down.
938                  *
939                  * Aborting also means an external thread is explicitly managing
940                  * this request, so that we do not complete it up the stack.
941                  */
942                 request->complete_in_target = true;
943                 response = SAS_TASK_UNDELIVERED;
944
945                 if ((isci_device->status == isci_stopping) ||
946                     (isci_device->status == isci_stopped))
947                         /* The device has been /is being stopped. Note that
948                          * we ignore the quiesce state, since we are
949                          * concerned about the actual device state.
950                          */
951                         status = SAS_DEVICE_UNKNOWN;
952                 else
953                         status = SAS_PHY_DOWN;
954
955                 complete_to_host = isci_perform_aborted_io_completion;
956
957                 /* This was an aborted request. */
958
959                 spin_unlock(&request->state_lock);
960                 break;
961
962         case terminating:
963
964                 /* This was an terminated request.  This happens when
965                  * the I/O is being terminated because of an action on
966                  * the device (reset, tear down, etc.), and the I/O needs
967                  * to be completed up the stack.
968                  */
969                 request->complete_in_target = true;
970                 response = SAS_TASK_UNDELIVERED;
971
972                 /* See if the device has been/is being stopped. Note
973                  * that we ignore the quiesce state, since we are
974                  * concerned about the actual device state.
975                  */
976                 if ((isci_device->status == isci_stopping) ||
977                     (isci_device->status == isci_stopped))
978                         status = SAS_DEVICE_UNKNOWN;
979                 else
980                         status = SAS_ABORTED_TASK;
981
982                 complete_to_host = isci_perform_aborted_io_completion;
983
984                 /* This was a terminated request. */
985
986                 spin_unlock(&request->state_lock);
987                 break;
988
989         default:
990
991                 /* The request is done from an SCU HW perspective. */
992                 request->status = completed;
993
994                 spin_unlock(&request->state_lock);
995
996                 /* This is an active request being completed from the core. */
997                 switch (completion_status) {
998
999                 case SCI_IO_FAILURE_RESPONSE_VALID:
1000                         dev_dbg(&isci_host->pdev->dev,
1001                                 "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
1002                                 __func__,
1003                                 request,
1004                                 task);
1005
1006                         if (sas_protocol_ata(task->task_proto)) {
1007                                 resp_buf
1008                                         = scic_stp_io_request_get_d2h_reg_address(
1009                                         request->sci_request_handle
1010                                         );
1011                                 isci_request_process_stp_response(task,
1012                                                                   resp_buf
1013                                                                   );
1014
1015                         } else if (SAS_PROTOCOL_SSP == task->task_proto) {
1016
1017                                 /* crack the iu response buffer. */
1018                                 resp_iu
1019                                         = scic_io_request_get_response_iu_address(
1020                                         request->sci_request_handle
1021                                         );
1022
1023                                 isci_request_process_response_iu(task, resp_iu,
1024                                                                  &isci_host->pdev->dev
1025                                                                  );
1026
1027                         } else if (SAS_PROTOCOL_SMP == task->task_proto) {
1028
1029                                 dev_err(&isci_host->pdev->dev,
1030                                         "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
1031                                         "SAS_PROTOCOL_SMP protocol\n",
1032                                         __func__);
1033
1034                         } else
1035                                 dev_err(&isci_host->pdev->dev,
1036                                         "%s: unknown protocol\n", __func__);
1037
1038                         /* use the task status set in the task struct by the
1039                          * isci_request_process_response_iu call.
1040                          */
1041                         request->complete_in_target = true;
1042                         response = task->task_status.resp;
1043                         status = task->task_status.stat;
1044                         break;
1045
1046                 case SCI_IO_SUCCESS:
1047                 case SCI_IO_SUCCESS_IO_DONE_EARLY:
1048
1049                         response = SAS_TASK_COMPLETE;
1050                         status   = SAM_STAT_GOOD;
1051                         request->complete_in_target = true;
1052
1053                         if (task->task_proto == SAS_PROTOCOL_SMP) {
1054
1055                                 u8 *command_iu_address
1056                                         = scic_io_request_get_command_iu_address(
1057                                         request->sci_request_handle
1058                                         );
1059
1060                                 dev_dbg(&isci_host->pdev->dev,
1061                                         "%s: SMP protocol completion\n",
1062                                         __func__);
1063
1064                                 sg_copy_from_buffer(
1065                                         &task->smp_task.smp_resp, 1,
1066                                         command_iu_address
1067                                         + sizeof(struct smp_req),
1068                                         sizeof(struct smp_resp));
1069                         } else if (completion_status
1070                                    == SCI_IO_SUCCESS_IO_DONE_EARLY) {
1071
1072                                 /* This was an SSP / STP / SATA transfer.
1073                                  * There is a possibility that less data than
1074                                  * the maximum was transferred.
1075                                  */
1076                                 u32 transferred_length
1077                                         = scic_io_request_get_number_of_bytes_transferred(
1078                                         request->sci_request_handle);
1079
1080                                 task->task_status.residual
1081                                         = task->total_xfer_len - transferred_length;
1082
1083                                 /* If there were residual bytes, call this an
1084                                  * underrun.
1085                                  */
1086                                 if (task->task_status.residual != 0)
1087                                         status = SAS_DATA_UNDERRUN;
1088
1089                                 dev_dbg(&isci_host->pdev->dev,
1090                                         "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
1091                                         __func__,
1092                                         status);
1093
1094                         } else
1095                                 dev_dbg(&isci_host->pdev->dev,
1096                                         "%s: SCI_IO_SUCCESS\n",
1097                                         __func__);
1098
1099                         break;
1100
1101                 case SCI_IO_FAILURE_TERMINATED:
1102                         dev_dbg(&isci_host->pdev->dev,
1103                                 "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
1104                                 __func__,
1105                                 request,
1106                                 task);
1107
1108                         /* The request was terminated explicitly.  No handling
1109                          * is needed in the SCSI error handler path.
1110                          */
1111                         request->complete_in_target = true;
1112                         response = SAS_TASK_UNDELIVERED;
1113
1114                         /* See if the device has been/is being stopped. Note
1115                          * that we ignore the quiesce state, since we are
1116                          * concerned about the actual device state.
1117                          */
1118                         if ((isci_device->status == isci_stopping) ||
1119                             (isci_device->status == isci_stopped))
1120                                 status = SAS_DEVICE_UNKNOWN;
1121                         else
1122                                 status = SAS_ABORTED_TASK;
1123
1124                         complete_to_host = isci_perform_normal_io_completion;
1125                         break;
1126
1127                 case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
1128
1129                         isci_request_handle_controller_specific_errors(
1130                                 isci_device, request, task, &response, &status,
1131                                 &complete_to_host);
1132
1133                         break;
1134
1135                 case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
1136                         /* This is a special case, in that the I/O completion
1137                          * is telling us that the device needs a reset.
1138                          * In order for the device reset condition to be
1139                          * noticed, the I/O has to be handled in the error
1140                          * handler.  Set the reset flag and cause the
1141                          * SCSI error thread to be scheduled.
1142                          */
1143                         spin_lock_irqsave(&task->task_state_lock, task_flags);
1144                         task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1145                         spin_unlock_irqrestore(&task->task_state_lock, task_flags);
1146
1147                         /* Fail the I/O. */
1148                         response = SAS_TASK_UNDELIVERED;
1149                         status = SAM_STAT_TASK_ABORTED;
1150
1151                         complete_to_host = isci_perform_error_io_completion;
1152                         request->complete_in_target = false;
1153                         break;
1154
1155                 default:
1156                         /* Catch any otherwise unhandled error codes here. */
1157                         dev_warn(&isci_host->pdev->dev,
1158                                  "%s: invalid completion code: 0x%x - "
1159                                  "isci_request = %p\n",
1160                                  __func__, completion_status, request);
1161
1162                         response = SAS_TASK_UNDELIVERED;
1163
1164                         /* See if the device has been/is being stopped. Note
1165                          * that we ignore the quiesce state, since we are
1166                          * concerned about the actual device state.
1167                          */
1168                         if ((isci_device->status == isci_stopping) ||
1169                             (isci_device->status == isci_stopped))
1170                                 status = SAS_DEVICE_UNKNOWN;
1171                         else
1172                                 status = SAS_ABORTED_TASK;
1173
1174                         complete_to_host = isci_perform_error_io_completion;
1175                         request->complete_in_target = false;
1176                         break;
1177                 }
1178                 break;
1179         }
1180
1181         isci_request_unmap_sgl(request, isci_host->pdev);
1182
1183         /* Put the completed request on the correct list */
1184         isci_task_save_for_upper_layer_completion(isci_host, request, response,
1185                                                   status, complete_to_host
1186                                                   );
1187
1188         /* complete the io request to the core. */
1189         scic_controller_complete_io(&isci_host->sci,
1190                                     &isci_device->sci,
1191                                     request->sci_request_handle);
1192         /* NULL the request handle so it cannot be completed or
1193          * terminated again, and to cause any calls into abort
1194          * task to recognize the already completed case.
1195          */
1196         request->sci_request_handle = NULL;
1197
1198         isci_host_can_dequeue(isci_host, 1);
1199 }