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