xref: /openbmc/linux/drivers/scsi/isci/request.c (revision ca48739e)
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 <scsi/scsi_cmnd.h>
57 #include "isci.h"
58 #include "task.h"
59 #include "request.h"
60 #include "scu_completion_codes.h"
61 #include "scu_event_codes.h"
62 #include "sas.h"
63 
64 #undef C
65 #define C(a) (#a)
66 const char *req_state_name(enum sci_base_request_states state)
67 {
68 	static const char * const strings[] = REQUEST_STATES;
69 
70 	return strings[state];
71 }
72 #undef C
73 
74 static struct scu_sgl_element_pair *to_sgl_element_pair(struct isci_request *ireq,
75 							int idx)
76 {
77 	if (idx == 0)
78 		return &ireq->tc->sgl_pair_ab;
79 	else if (idx == 1)
80 		return &ireq->tc->sgl_pair_cd;
81 	else if (idx < 0)
82 		return NULL;
83 	else
84 		return &ireq->sg_table[idx - 2];
85 }
86 
87 static dma_addr_t to_sgl_element_pair_dma(struct isci_host *ihost,
88 					  struct isci_request *ireq, u32 idx)
89 {
90 	u32 offset;
91 
92 	if (idx == 0) {
93 		offset = (void *) &ireq->tc->sgl_pair_ab -
94 			 (void *) &ihost->task_context_table[0];
95 		return ihost->tc_dma + offset;
96 	} else if (idx == 1) {
97 		offset = (void *) &ireq->tc->sgl_pair_cd -
98 			 (void *) &ihost->task_context_table[0];
99 		return ihost->tc_dma + offset;
100 	}
101 
102 	return sci_io_request_get_dma_addr(ireq, &ireq->sg_table[idx - 2]);
103 }
104 
105 static void init_sgl_element(struct scu_sgl_element *e, struct scatterlist *sg)
106 {
107 	e->length = sg_dma_len(sg);
108 	e->address_upper = upper_32_bits(sg_dma_address(sg));
109 	e->address_lower = lower_32_bits(sg_dma_address(sg));
110 	e->address_modifier = 0;
111 }
112 
113 static void sci_request_build_sgl(struct isci_request *ireq)
114 {
115 	struct isci_host *ihost = ireq->isci_host;
116 	struct sas_task *task = isci_request_access_task(ireq);
117 	struct scatterlist *sg = NULL;
118 	dma_addr_t dma_addr;
119 	u32 sg_idx = 0;
120 	struct scu_sgl_element_pair *scu_sg   = NULL;
121 	struct scu_sgl_element_pair *prev_sg  = NULL;
122 
123 	if (task->num_scatter > 0) {
124 		sg = task->scatter;
125 
126 		while (sg) {
127 			scu_sg = to_sgl_element_pair(ireq, sg_idx);
128 			init_sgl_element(&scu_sg->A, sg);
129 			sg = sg_next(sg);
130 			if (sg) {
131 				init_sgl_element(&scu_sg->B, sg);
132 				sg = sg_next(sg);
133 			} else
134 				memset(&scu_sg->B, 0, sizeof(scu_sg->B));
135 
136 			if (prev_sg) {
137 				dma_addr = to_sgl_element_pair_dma(ihost,
138 								   ireq,
139 								   sg_idx);
140 
141 				prev_sg->next_pair_upper =
142 					upper_32_bits(dma_addr);
143 				prev_sg->next_pair_lower =
144 					lower_32_bits(dma_addr);
145 			}
146 
147 			prev_sg = scu_sg;
148 			sg_idx++;
149 		}
150 	} else {	/* handle when no sg */
151 		scu_sg = to_sgl_element_pair(ireq, sg_idx);
152 
153 		dma_addr = dma_map_single(&ihost->pdev->dev,
154 					  task->scatter,
155 					  task->total_xfer_len,
156 					  task->data_dir);
157 
158 		ireq->zero_scatter_daddr = dma_addr;
159 
160 		scu_sg->A.length = task->total_xfer_len;
161 		scu_sg->A.address_upper = upper_32_bits(dma_addr);
162 		scu_sg->A.address_lower = lower_32_bits(dma_addr);
163 	}
164 
165 	if (scu_sg) {
166 		scu_sg->next_pair_upper = 0;
167 		scu_sg->next_pair_lower = 0;
168 	}
169 }
170 
171 static void sci_io_request_build_ssp_command_iu(struct isci_request *ireq)
172 {
173 	struct ssp_cmd_iu *cmd_iu;
174 	struct sas_task *task = isci_request_access_task(ireq);
175 
176 	cmd_iu = &ireq->ssp.cmd;
177 
178 	memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
179 	cmd_iu->add_cdb_len = 0;
180 	cmd_iu->_r_a = 0;
181 	cmd_iu->_r_b = 0;
182 	cmd_iu->en_fburst = 0; /* unsupported */
183 	cmd_iu->task_prio = task->ssp_task.task_prio;
184 	cmd_iu->task_attr = task->ssp_task.task_attr;
185 	cmd_iu->_r_c = 0;
186 
187 	sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cmd->cmnd,
188 		       (task->ssp_task.cmd->cmd_len+3) / sizeof(u32));
189 }
190 
191 static void sci_task_request_build_ssp_task_iu(struct isci_request *ireq)
192 {
193 	struct ssp_task_iu *task_iu;
194 	struct sas_task *task = isci_request_access_task(ireq);
195 	struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
196 
197 	task_iu = &ireq->ssp.tmf;
198 
199 	memset(task_iu, 0, sizeof(struct ssp_task_iu));
200 
201 	memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
202 
203 	task_iu->task_func = isci_tmf->tmf_code;
204 	task_iu->task_tag =
205 		(test_bit(IREQ_TMF, &ireq->flags)) ?
206 		isci_tmf->io_tag :
207 		SCI_CONTROLLER_INVALID_IO_TAG;
208 }
209 
210 /**
211  * This method is will fill in the SCU Task Context for any type of SSP request.
212  * @sci_req:
213  * @task_context:
214  *
215  */
216 static void scu_ssp_request_construct_task_context(
217 	struct isci_request *ireq,
218 	struct scu_task_context *task_context)
219 {
220 	dma_addr_t dma_addr;
221 	struct isci_remote_device *idev;
222 	struct isci_port *iport;
223 
224 	idev = ireq->target_device;
225 	iport = idev->owning_port;
226 
227 	/* Fill in the TC with its required data */
228 	task_context->abort = 0;
229 	task_context->priority = 0;
230 	task_context->initiator_request = 1;
231 	task_context->connection_rate = idev->connection_rate;
232 	task_context->protocol_engine_index = ISCI_PEG;
233 	task_context->logical_port_index = iport->physical_port_index;
234 	task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
235 	task_context->valid = SCU_TASK_CONTEXT_VALID;
236 	task_context->context_type = SCU_TASK_CONTEXT_TYPE;
237 
238 	task_context->remote_node_index = idev->rnc.remote_node_index;
239 	task_context->command_code = 0;
240 
241 	task_context->link_layer_control = 0;
242 	task_context->do_not_dma_ssp_good_response = 1;
243 	task_context->strict_ordering = 0;
244 	task_context->control_frame = 0;
245 	task_context->timeout_enable = 0;
246 	task_context->block_guard_enable = 0;
247 
248 	task_context->address_modifier = 0;
249 
250 	/* task_context->type.ssp.tag = ireq->io_tag; */
251 	task_context->task_phase = 0x01;
252 
253 	ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
254 			      (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
255 			      (iport->physical_port_index <<
256 			       SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
257 			      ISCI_TAG_TCI(ireq->io_tag));
258 
259 	/*
260 	 * Copy the physical address for the command buffer to the
261 	 * SCU Task Context
262 	 */
263 	dma_addr = sci_io_request_get_dma_addr(ireq, &ireq->ssp.cmd);
264 
265 	task_context->command_iu_upper = upper_32_bits(dma_addr);
266 	task_context->command_iu_lower = lower_32_bits(dma_addr);
267 
268 	/*
269 	 * Copy the physical address for the response buffer to the
270 	 * SCU Task Context
271 	 */
272 	dma_addr = sci_io_request_get_dma_addr(ireq, &ireq->ssp.rsp);
273 
274 	task_context->response_iu_upper = upper_32_bits(dma_addr);
275 	task_context->response_iu_lower = lower_32_bits(dma_addr);
276 }
277 
278 static u8 scu_bg_blk_size(struct scsi_device *sdp)
279 {
280 	switch (sdp->sector_size) {
281 	case 512:
282 		return 0;
283 	case 1024:
284 		return 1;
285 	case 4096:
286 		return 3;
287 	default:
288 		return 0xff;
289 	}
290 }
291 
292 static u32 scu_dif_bytes(u32 len, u32 sector_size)
293 {
294 	return (len >> ilog2(sector_size)) * 8;
295 }
296 
297 static void scu_ssp_ireq_dif_insert(struct isci_request *ireq, u8 type, u8 op)
298 {
299 	struct scu_task_context *tc = ireq->tc;
300 	struct scsi_cmnd *scmd = ireq->ttype_ptr.io_task_ptr->uldd_task;
301 	u8 blk_sz = scu_bg_blk_size(scmd->device);
302 
303 	tc->block_guard_enable = 1;
304 	tc->blk_prot_en = 1;
305 	tc->blk_sz = blk_sz;
306 	/* DIF write insert */
307 	tc->blk_prot_func = 0x2;
308 
309 	tc->transfer_length_bytes += scu_dif_bytes(tc->transfer_length_bytes,
310 						   scmd->device->sector_size);
311 
312 	/* always init to 0, used by hw */
313 	tc->interm_crc_val = 0;
314 
315 	tc->init_crc_seed = 0;
316 	tc->app_tag_verify = 0;
317 	tc->app_tag_gen = 0;
318 	tc->ref_tag_seed_verify = 0;
319 
320 	/* always init to same as bg_blk_sz */
321 	tc->UD_bytes_immed_val = scmd->device->sector_size;
322 
323 	tc->reserved_DC_0 = 0;
324 
325 	/* always init to 8 */
326 	tc->DIF_bytes_immed_val = 8;
327 
328 	tc->reserved_DC_1 = 0;
329 	tc->bgc_blk_sz = scmd->device->sector_size;
330 	tc->reserved_E0_0 = 0;
331 	tc->app_tag_gen_mask = 0;
332 
333 	/** setup block guard control **/
334 	tc->bgctl = 0;
335 
336 	/* DIF write insert */
337 	tc->bgctl_f.op = 0x2;
338 
339 	tc->app_tag_verify_mask = 0;
340 
341 	/* must init to 0 for hw */
342 	tc->blk_guard_err = 0;
343 
344 	tc->reserved_E8_0 = 0;
345 
346 	if ((type & SCSI_PROT_DIF_TYPE1) || (type & SCSI_PROT_DIF_TYPE2))
347 		tc->ref_tag_seed_gen = scsi_get_lba(scmd) & 0xffffffff;
348 	else if (type & SCSI_PROT_DIF_TYPE3)
349 		tc->ref_tag_seed_gen = 0;
350 }
351 
352 static void scu_ssp_ireq_dif_strip(struct isci_request *ireq, u8 type, u8 op)
353 {
354 	struct scu_task_context *tc = ireq->tc;
355 	struct scsi_cmnd *scmd = ireq->ttype_ptr.io_task_ptr->uldd_task;
356 	u8 blk_sz = scu_bg_blk_size(scmd->device);
357 
358 	tc->block_guard_enable = 1;
359 	tc->blk_prot_en = 1;
360 	tc->blk_sz = blk_sz;
361 	/* DIF read strip */
362 	tc->blk_prot_func = 0x1;
363 
364 	tc->transfer_length_bytes += scu_dif_bytes(tc->transfer_length_bytes,
365 						   scmd->device->sector_size);
366 
367 	/* always init to 0, used by hw */
368 	tc->interm_crc_val = 0;
369 
370 	tc->init_crc_seed = 0;
371 	tc->app_tag_verify = 0;
372 	tc->app_tag_gen = 0;
373 
374 	if ((type & SCSI_PROT_DIF_TYPE1) || (type & SCSI_PROT_DIF_TYPE2))
375 		tc->ref_tag_seed_verify = scsi_get_lba(scmd) & 0xffffffff;
376 	else if (type & SCSI_PROT_DIF_TYPE3)
377 		tc->ref_tag_seed_verify = 0;
378 
379 	/* always init to same as bg_blk_sz */
380 	tc->UD_bytes_immed_val = scmd->device->sector_size;
381 
382 	tc->reserved_DC_0 = 0;
383 
384 	/* always init to 8 */
385 	tc->DIF_bytes_immed_val = 8;
386 
387 	tc->reserved_DC_1 = 0;
388 	tc->bgc_blk_sz = scmd->device->sector_size;
389 	tc->reserved_E0_0 = 0;
390 	tc->app_tag_gen_mask = 0;
391 
392 	/** setup block guard control **/
393 	tc->bgctl = 0;
394 
395 	/* DIF read strip */
396 	tc->bgctl_f.crc_verify = 1;
397 	tc->bgctl_f.op = 0x1;
398 	if ((type & SCSI_PROT_DIF_TYPE1) || (type & SCSI_PROT_DIF_TYPE2)) {
399 		tc->bgctl_f.ref_tag_chk = 1;
400 		tc->bgctl_f.app_f_detect = 1;
401 	} else if (type & SCSI_PROT_DIF_TYPE3)
402 		tc->bgctl_f.app_ref_f_detect = 1;
403 
404 	tc->app_tag_verify_mask = 0;
405 
406 	/* must init to 0 for hw */
407 	tc->blk_guard_err = 0;
408 
409 	tc->reserved_E8_0 = 0;
410 	tc->ref_tag_seed_gen = 0;
411 }
412 
413 /**
414  * This method is will fill in the SCU Task Context for a SSP IO request.
415  * @sci_req:
416  *
417  */
418 static void scu_ssp_io_request_construct_task_context(struct isci_request *ireq,
419 						      enum dma_data_direction dir,
420 						      u32 len)
421 {
422 	struct scu_task_context *task_context = ireq->tc;
423 	struct sas_task *sas_task = ireq->ttype_ptr.io_task_ptr;
424 	struct scsi_cmnd *scmd = sas_task->uldd_task;
425 	u8 prot_type = scsi_get_prot_type(scmd);
426 	u8 prot_op = scsi_get_prot_op(scmd);
427 
428 	scu_ssp_request_construct_task_context(ireq, task_context);
429 
430 	task_context->ssp_command_iu_length =
431 		sizeof(struct ssp_cmd_iu) / sizeof(u32);
432 	task_context->type.ssp.frame_type = SSP_COMMAND;
433 
434 	switch (dir) {
435 	case DMA_FROM_DEVICE:
436 	case DMA_NONE:
437 	default:
438 		task_context->task_type = SCU_TASK_TYPE_IOREAD;
439 		break;
440 	case DMA_TO_DEVICE:
441 		task_context->task_type = SCU_TASK_TYPE_IOWRITE;
442 		break;
443 	}
444 
445 	task_context->transfer_length_bytes = len;
446 
447 	if (task_context->transfer_length_bytes > 0)
448 		sci_request_build_sgl(ireq);
449 
450 	if (prot_type != SCSI_PROT_DIF_TYPE0) {
451 		if (prot_op == SCSI_PROT_READ_STRIP)
452 			scu_ssp_ireq_dif_strip(ireq, prot_type, prot_op);
453 		else if (prot_op == SCSI_PROT_WRITE_INSERT)
454 			scu_ssp_ireq_dif_insert(ireq, prot_type, prot_op);
455 	}
456 }
457 
458 /**
459  * This method will fill in the SCU Task Context for a SSP Task request.  The
460  *    following important settings are utilized: -# priority ==
461  *    SCU_TASK_PRIORITY_HIGH.  This ensures that the task request is issued
462  *    ahead of other task destined for the same Remote Node. -# task_type ==
463  *    SCU_TASK_TYPE_IOREAD.  This simply indicates that a normal request type
464  *    (i.e. non-raw frame) is being utilized to perform task management. -#
465  *    control_frame == 1.  This ensures that the proper endianess is set so
466  *    that the bytes are transmitted in the right order for a task frame.
467  * @sci_req: This parameter specifies the task request object being
468  *    constructed.
469  *
470  */
471 static void scu_ssp_task_request_construct_task_context(struct isci_request *ireq)
472 {
473 	struct scu_task_context *task_context = ireq->tc;
474 
475 	scu_ssp_request_construct_task_context(ireq, task_context);
476 
477 	task_context->control_frame                = 1;
478 	task_context->priority                     = SCU_TASK_PRIORITY_HIGH;
479 	task_context->task_type                    = SCU_TASK_TYPE_RAW_FRAME;
480 	task_context->transfer_length_bytes        = 0;
481 	task_context->type.ssp.frame_type          = SSP_TASK;
482 	task_context->ssp_command_iu_length =
483 		sizeof(struct ssp_task_iu) / sizeof(u32);
484 }
485 
486 /**
487  * This method is will fill in the SCU Task Context for any type of SATA
488  *    request.  This is called from the various SATA constructors.
489  * @sci_req: The general IO request object which is to be used in
490  *    constructing the SCU task context.
491  * @task_context: The buffer pointer for the SCU task context which is being
492  *    constructed.
493  *
494  * The general io request construction is complete. The buffer assignment for
495  * the command buffer is complete. none Revisit task context construction to
496  * determine what is common for SSP/SMP/STP task context structures.
497  */
498 static void scu_sata_request_construct_task_context(
499 	struct isci_request *ireq,
500 	struct scu_task_context *task_context)
501 {
502 	dma_addr_t dma_addr;
503 	struct isci_remote_device *idev;
504 	struct isci_port *iport;
505 
506 	idev = ireq->target_device;
507 	iport = idev->owning_port;
508 
509 	/* Fill in the TC with its required data */
510 	task_context->abort = 0;
511 	task_context->priority = SCU_TASK_PRIORITY_NORMAL;
512 	task_context->initiator_request = 1;
513 	task_context->connection_rate = idev->connection_rate;
514 	task_context->protocol_engine_index = ISCI_PEG;
515 	task_context->logical_port_index = iport->physical_port_index;
516 	task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
517 	task_context->valid = SCU_TASK_CONTEXT_VALID;
518 	task_context->context_type = SCU_TASK_CONTEXT_TYPE;
519 
520 	task_context->remote_node_index = idev->rnc.remote_node_index;
521 	task_context->command_code = 0;
522 
523 	task_context->link_layer_control = 0;
524 	task_context->do_not_dma_ssp_good_response = 1;
525 	task_context->strict_ordering = 0;
526 	task_context->control_frame = 0;
527 	task_context->timeout_enable = 0;
528 	task_context->block_guard_enable = 0;
529 
530 	task_context->address_modifier = 0;
531 	task_context->task_phase = 0x01;
532 
533 	task_context->ssp_command_iu_length =
534 		(sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);
535 
536 	/* Set the first word of the H2D REG FIS */
537 	task_context->type.words[0] = *(u32 *)&ireq->stp.cmd;
538 
539 	ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
540 			      (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
541 			      (iport->physical_port_index <<
542 			       SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
543 			      ISCI_TAG_TCI(ireq->io_tag));
544 	/*
545 	 * Copy the physical address for the command buffer to the SCU Task
546 	 * Context. We must offset the command buffer by 4 bytes because the
547 	 * first 4 bytes are transfered in the body of the TC.
548 	 */
549 	dma_addr = sci_io_request_get_dma_addr(ireq,
550 						((char *) &ireq->stp.cmd) +
551 						sizeof(u32));
552 
553 	task_context->command_iu_upper = upper_32_bits(dma_addr);
554 	task_context->command_iu_lower = lower_32_bits(dma_addr);
555 
556 	/* SATA Requests do not have a response buffer */
557 	task_context->response_iu_upper = 0;
558 	task_context->response_iu_lower = 0;
559 }
560 
561 static void scu_stp_raw_request_construct_task_context(struct isci_request *ireq)
562 {
563 	struct scu_task_context *task_context = ireq->tc;
564 
565 	scu_sata_request_construct_task_context(ireq, task_context);
566 
567 	task_context->control_frame         = 0;
568 	task_context->priority              = SCU_TASK_PRIORITY_NORMAL;
569 	task_context->task_type             = SCU_TASK_TYPE_SATA_RAW_FRAME;
570 	task_context->type.stp.fis_type     = FIS_REGH2D;
571 	task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
572 }
573 
574 static enum sci_status sci_stp_pio_request_construct(struct isci_request *ireq,
575 							  bool copy_rx_frame)
576 {
577 	struct isci_stp_request *stp_req = &ireq->stp.req;
578 
579 	scu_stp_raw_request_construct_task_context(ireq);
580 
581 	stp_req->status = 0;
582 	stp_req->sgl.offset = 0;
583 	stp_req->sgl.set = SCU_SGL_ELEMENT_PAIR_A;
584 
585 	if (copy_rx_frame) {
586 		sci_request_build_sgl(ireq);
587 		stp_req->sgl.index = 0;
588 	} else {
589 		/* The user does not want the data copied to the SGL buffer location */
590 		stp_req->sgl.index = -1;
591 	}
592 
593 	return SCI_SUCCESS;
594 }
595 
596 /**
597  *
598  * @sci_req: This parameter specifies the request to be constructed as an
599  *    optimized request.
600  * @optimized_task_type: This parameter specifies whether the request is to be
601  *    an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
602  *    value of 1 indicates NCQ.
603  *
604  * This method will perform request construction common to all types of STP
605  * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
606  * returns an indication as to whether the construction was successful.
607  */
608 static void sci_stp_optimized_request_construct(struct isci_request *ireq,
609 						     u8 optimized_task_type,
610 						     u32 len,
611 						     enum dma_data_direction dir)
612 {
613 	struct scu_task_context *task_context = ireq->tc;
614 
615 	/* Build the STP task context structure */
616 	scu_sata_request_construct_task_context(ireq, task_context);
617 
618 	/* Copy over the SGL elements */
619 	sci_request_build_sgl(ireq);
620 
621 	/* Copy over the number of bytes to be transfered */
622 	task_context->transfer_length_bytes = len;
623 
624 	if (dir == DMA_TO_DEVICE) {
625 		/*
626 		 * The difference between the DMA IN and DMA OUT request task type
627 		 * values are consistent with the difference between FPDMA READ
628 		 * and FPDMA WRITE values.  Add the supplied task type parameter
629 		 * to this difference to set the task type properly for this
630 		 * DATA OUT (WRITE) case. */
631 		task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
632 								 - SCU_TASK_TYPE_DMA_IN);
633 	} else {
634 		/*
635 		 * For the DATA IN (READ) case, simply save the supplied
636 		 * optimized task type. */
637 		task_context->task_type = optimized_task_type;
638 	}
639 }
640 
641 static void sci_atapi_construct(struct isci_request *ireq)
642 {
643 	struct host_to_dev_fis *h2d_fis = &ireq->stp.cmd;
644 	struct sas_task *task;
645 
646 	/* To simplify the implementation we take advantage of the
647 	 * silicon's partial acceleration of atapi protocol (dma data
648 	 * transfers), so we promote all commands to dma protocol.  This
649 	 * breaks compatibility with ATA_HORKAGE_ATAPI_MOD16_DMA drives.
650 	 */
651 	h2d_fis->features |= ATAPI_PKT_DMA;
652 
653 	scu_stp_raw_request_construct_task_context(ireq);
654 
655 	task = isci_request_access_task(ireq);
656 	if (task->data_dir == DMA_NONE)
657 		task->total_xfer_len = 0;
658 
659 	/* clear the response so we can detect arrivial of an
660 	 * unsolicited h2d fis
661 	 */
662 	ireq->stp.rsp.fis_type = 0;
663 }
664 
665 static enum sci_status
666 sci_io_request_construct_sata(struct isci_request *ireq,
667 			       u32 len,
668 			       enum dma_data_direction dir,
669 			       bool copy)
670 {
671 	enum sci_status status = SCI_SUCCESS;
672 	struct sas_task *task = isci_request_access_task(ireq);
673 	struct domain_device *dev = ireq->target_device->domain_dev;
674 
675 	/* check for management protocols */
676 	if (test_bit(IREQ_TMF, &ireq->flags)) {
677 		struct isci_tmf *tmf = isci_request_access_tmf(ireq);
678 
679 		dev_err(&ireq->owning_controller->pdev->dev,
680 			"%s: Request 0x%p received un-handled SAT "
681 			"management protocol 0x%x.\n",
682 			__func__, ireq, tmf->tmf_code);
683 
684 		return SCI_FAILURE;
685 	}
686 
687 	if (!sas_protocol_ata(task->task_proto)) {
688 		dev_err(&ireq->owning_controller->pdev->dev,
689 			"%s: Non-ATA protocol in SATA path: 0x%x\n",
690 			__func__,
691 			task->task_proto);
692 		return SCI_FAILURE;
693 
694 	}
695 
696 	/* ATAPI */
697 	if (dev->sata_dev.class == ATA_DEV_ATAPI &&
698 	    task->ata_task.fis.command == ATA_CMD_PACKET) {
699 		sci_atapi_construct(ireq);
700 		return SCI_SUCCESS;
701 	}
702 
703 	/* non data */
704 	if (task->data_dir == DMA_NONE) {
705 		scu_stp_raw_request_construct_task_context(ireq);
706 		return SCI_SUCCESS;
707 	}
708 
709 	/* NCQ */
710 	if (task->ata_task.use_ncq) {
711 		sci_stp_optimized_request_construct(ireq,
712 							 SCU_TASK_TYPE_FPDMAQ_READ,
713 							 len, dir);
714 		return SCI_SUCCESS;
715 	}
716 
717 	/* DMA */
718 	if (task->ata_task.dma_xfer) {
719 		sci_stp_optimized_request_construct(ireq,
720 							 SCU_TASK_TYPE_DMA_IN,
721 							 len, dir);
722 		return SCI_SUCCESS;
723 	} else /* PIO */
724 		return sci_stp_pio_request_construct(ireq, copy);
725 
726 	return status;
727 }
728 
729 static enum sci_status sci_io_request_construct_basic_ssp(struct isci_request *ireq)
730 {
731 	struct sas_task *task = isci_request_access_task(ireq);
732 
733 	ireq->protocol = SAS_PROTOCOL_SSP;
734 
735 	scu_ssp_io_request_construct_task_context(ireq,
736 						  task->data_dir,
737 						  task->total_xfer_len);
738 
739 	sci_io_request_build_ssp_command_iu(ireq);
740 
741 	sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
742 
743 	return SCI_SUCCESS;
744 }
745 
746 enum sci_status sci_task_request_construct_ssp(
747 	struct isci_request *ireq)
748 {
749 	/* Construct the SSP Task SCU Task Context */
750 	scu_ssp_task_request_construct_task_context(ireq);
751 
752 	/* Fill in the SSP Task IU */
753 	sci_task_request_build_ssp_task_iu(ireq);
754 
755 	sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
756 
757 	return SCI_SUCCESS;
758 }
759 
760 static enum sci_status sci_io_request_construct_basic_sata(struct isci_request *ireq)
761 {
762 	enum sci_status status;
763 	bool copy = false;
764 	struct sas_task *task = isci_request_access_task(ireq);
765 
766 	ireq->protocol = SAS_PROTOCOL_STP;
767 
768 	copy = (task->data_dir == DMA_NONE) ? false : true;
769 
770 	status = sci_io_request_construct_sata(ireq,
771 						task->total_xfer_len,
772 						task->data_dir,
773 						copy);
774 
775 	if (status == SCI_SUCCESS)
776 		sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
777 
778 	return status;
779 }
780 
781 /**
782  * sci_req_tx_bytes - bytes transferred when reply underruns request
783  * @ireq: request that was terminated early
784  */
785 #define SCU_TASK_CONTEXT_SRAM 0x200000
786 static u32 sci_req_tx_bytes(struct isci_request *ireq)
787 {
788 	struct isci_host *ihost = ireq->owning_controller;
789 	u32 ret_val = 0;
790 
791 	if (readl(&ihost->smu_registers->address_modifier) == 0) {
792 		void __iomem *scu_reg_base = ihost->scu_registers;
793 
794 		/* get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
795 		 *   BAR1 is the scu_registers
796 		 *   0x20002C = 0x200000 + 0x2c
797 		 *            = start of task context SRAM + offset of (type.ssp.data_offset)
798 		 *   TCi is the io_tag of struct sci_request
799 		 */
800 		ret_val = readl(scu_reg_base +
801 				(SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
802 				((sizeof(struct scu_task_context)) * ISCI_TAG_TCI(ireq->io_tag)));
803 	}
804 
805 	return ret_val;
806 }
807 
808 enum sci_status sci_request_start(struct isci_request *ireq)
809 {
810 	enum sci_base_request_states state;
811 	struct scu_task_context *tc = ireq->tc;
812 	struct isci_host *ihost = ireq->owning_controller;
813 
814 	state = ireq->sm.current_state_id;
815 	if (state != SCI_REQ_CONSTRUCTED) {
816 		dev_warn(&ihost->pdev->dev,
817 			"%s: SCIC IO Request requested to start while in wrong "
818 			 "state %d\n", __func__, state);
819 		return SCI_FAILURE_INVALID_STATE;
820 	}
821 
822 	tc->task_index = ISCI_TAG_TCI(ireq->io_tag);
823 
824 	switch (tc->protocol_type) {
825 	case SCU_TASK_CONTEXT_PROTOCOL_SMP:
826 	case SCU_TASK_CONTEXT_PROTOCOL_SSP:
827 		/* SSP/SMP Frame */
828 		tc->type.ssp.tag = ireq->io_tag;
829 		tc->type.ssp.target_port_transfer_tag = 0xFFFF;
830 		break;
831 
832 	case SCU_TASK_CONTEXT_PROTOCOL_STP:
833 		/* STP/SATA Frame
834 		 * tc->type.stp.ncq_tag = ireq->ncq_tag;
835 		 */
836 		break;
837 
838 	case SCU_TASK_CONTEXT_PROTOCOL_NONE:
839 		/* / @todo When do we set no protocol type? */
840 		break;
841 
842 	default:
843 		/* This should never happen since we build the IO
844 		 * requests */
845 		break;
846 	}
847 
848 	/* Add to the post_context the io tag value */
849 	ireq->post_context |= ISCI_TAG_TCI(ireq->io_tag);
850 
851 	/* Everything is good go ahead and change state */
852 	sci_change_state(&ireq->sm, SCI_REQ_STARTED);
853 
854 	return SCI_SUCCESS;
855 }
856 
857 enum sci_status
858 sci_io_request_terminate(struct isci_request *ireq)
859 {
860 	enum sci_base_request_states state;
861 
862 	state = ireq->sm.current_state_id;
863 
864 	switch (state) {
865 	case SCI_REQ_CONSTRUCTED:
866 		/* Set to make sure no HW terminate posting is done: */
867 		set_bit(IREQ_TC_ABORT_POSTED, &ireq->flags);
868 		ireq->scu_status = SCU_TASK_DONE_TASK_ABORT;
869 		ireq->sci_status = SCI_FAILURE_IO_TERMINATED;
870 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
871 		return SCI_SUCCESS;
872 	case SCI_REQ_STARTED:
873 	case SCI_REQ_TASK_WAIT_TC_COMP:
874 	case SCI_REQ_SMP_WAIT_RESP:
875 	case SCI_REQ_SMP_WAIT_TC_COMP:
876 	case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
877 	case SCI_REQ_STP_UDMA_WAIT_D2H:
878 	case SCI_REQ_STP_NON_DATA_WAIT_H2D:
879 	case SCI_REQ_STP_NON_DATA_WAIT_D2H:
880 	case SCI_REQ_STP_PIO_WAIT_H2D:
881 	case SCI_REQ_STP_PIO_WAIT_FRAME:
882 	case SCI_REQ_STP_PIO_DATA_IN:
883 	case SCI_REQ_STP_PIO_DATA_OUT:
884 	case SCI_REQ_ATAPI_WAIT_H2D:
885 	case SCI_REQ_ATAPI_WAIT_PIO_SETUP:
886 	case SCI_REQ_ATAPI_WAIT_D2H:
887 	case SCI_REQ_ATAPI_WAIT_TC_COMP:
888 		/* Fall through and change state to ABORTING... */
889 	case SCI_REQ_TASK_WAIT_TC_RESP:
890 		/* The task frame was already confirmed to have been
891 		 * sent by the SCU HW.  Since the state machine is
892 		 * now only waiting for the task response itself,
893 		 * abort the request and complete it immediately
894 		 * and don't wait for the task response.
895 		 */
896 		sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
897 		fallthrough;	/* and handle like ABORTING */
898 	case SCI_REQ_ABORTING:
899 		if (!isci_remote_device_is_safe_to_abort(ireq->target_device))
900 			set_bit(IREQ_PENDING_ABORT, &ireq->flags);
901 		else
902 			clear_bit(IREQ_PENDING_ABORT, &ireq->flags);
903 		/* If the request is only waiting on the remote device
904 		 * suspension, return SUCCESS so the caller will wait too.
905 		 */
906 		return SCI_SUCCESS;
907 	case SCI_REQ_COMPLETED:
908 	default:
909 		dev_warn(&ireq->owning_controller->pdev->dev,
910 			 "%s: SCIC IO Request requested to abort while in wrong "
911 			 "state %d\n", __func__, ireq->sm.current_state_id);
912 		break;
913 	}
914 
915 	return SCI_FAILURE_INVALID_STATE;
916 }
917 
918 enum sci_status sci_request_complete(struct isci_request *ireq)
919 {
920 	enum sci_base_request_states state;
921 	struct isci_host *ihost = ireq->owning_controller;
922 
923 	state = ireq->sm.current_state_id;
924 	if (WARN_ONCE(state != SCI_REQ_COMPLETED,
925 		      "isci: request completion from wrong state (%s)\n",
926 		      req_state_name(state)))
927 		return SCI_FAILURE_INVALID_STATE;
928 
929 	if (ireq->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX)
930 		sci_controller_release_frame(ihost,
931 						  ireq->saved_rx_frame_index);
932 
933 	/* XXX can we just stop the machine and remove the 'final' state? */
934 	sci_change_state(&ireq->sm, SCI_REQ_FINAL);
935 	return SCI_SUCCESS;
936 }
937 
938 enum sci_status sci_io_request_event_handler(struct isci_request *ireq,
939 						  u32 event_code)
940 {
941 	enum sci_base_request_states state;
942 	struct isci_host *ihost = ireq->owning_controller;
943 
944 	state = ireq->sm.current_state_id;
945 
946 	if (state != SCI_REQ_STP_PIO_DATA_IN) {
947 		dev_warn(&ihost->pdev->dev, "%s: (%x) in wrong state %s\n",
948 			 __func__, event_code, req_state_name(state));
949 
950 		return SCI_FAILURE_INVALID_STATE;
951 	}
952 
953 	switch (scu_get_event_specifier(event_code)) {
954 	case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
955 		/* We are waiting for data and the SCU has R_ERR the data frame.
956 		 * Go back to waiting for the D2H Register FIS
957 		 */
958 		sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
959 		return SCI_SUCCESS;
960 	default:
961 		dev_err(&ihost->pdev->dev,
962 			"%s: pio request unexpected event %#x\n",
963 			__func__, event_code);
964 
965 		/* TODO Should we fail the PIO request when we get an
966 		 * unexpected event?
967 		 */
968 		return SCI_FAILURE;
969 	}
970 }
971 
972 /*
973  * This function copies response data for requests returning response data
974  *    instead of sense data.
975  * @sci_req: This parameter specifies the request object for which to copy
976  *    the response data.
977  */
978 static void sci_io_request_copy_response(struct isci_request *ireq)
979 {
980 	void *resp_buf;
981 	u32 len;
982 	struct ssp_response_iu *ssp_response;
983 	struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
984 
985 	ssp_response = &ireq->ssp.rsp;
986 
987 	resp_buf = &isci_tmf->resp.resp_iu;
988 
989 	len = min_t(u32,
990 		    SSP_RESP_IU_MAX_SIZE,
991 		    be32_to_cpu(ssp_response->response_data_len));
992 
993 	memcpy(resp_buf, ssp_response->resp_data, len);
994 }
995 
996 static enum sci_status
997 request_started_state_tc_event(struct isci_request *ireq,
998 			       u32 completion_code)
999 {
1000 	struct ssp_response_iu *resp_iu;
1001 	u8 datapres;
1002 
1003 	/* TODO: Any SDMA return code of other than 0 is bad decode 0x003C0000
1004 	 * to determine SDMA status
1005 	 */
1006 	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1007 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1008 		ireq->scu_status = SCU_TASK_DONE_GOOD;
1009 		ireq->sci_status = SCI_SUCCESS;
1010 		break;
1011 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP): {
1012 		/* There are times when the SCU hardware will return an early
1013 		 * response because the io request specified more data than is
1014 		 * returned by the target device (mode pages, inquiry data,
1015 		 * etc.).  We must check the response stats to see if this is
1016 		 * truly a failed request or a good request that just got
1017 		 * completed early.
1018 		 */
1019 		struct ssp_response_iu *resp = &ireq->ssp.rsp;
1020 		ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1021 
1022 		sci_swab32_cpy(&ireq->ssp.rsp,
1023 			       &ireq->ssp.rsp,
1024 			       word_cnt);
1025 
1026 		if (resp->status == 0) {
1027 			ireq->scu_status = SCU_TASK_DONE_GOOD;
1028 			ireq->sci_status = SCI_SUCCESS_IO_DONE_EARLY;
1029 		} else {
1030 			ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1031 			ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1032 		}
1033 		break;
1034 	}
1035 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE): {
1036 		ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1037 
1038 		sci_swab32_cpy(&ireq->ssp.rsp,
1039 			       &ireq->ssp.rsp,
1040 			       word_cnt);
1041 
1042 		ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1043 		ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1044 		break;
1045 	}
1046 
1047 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
1048 		/* TODO With TASK_DONE_RESP_LEN_ERR is the response frame
1049 		 * guaranteed to be received before this completion status is
1050 		 * posted?
1051 		 */
1052 		resp_iu = &ireq->ssp.rsp;
1053 		datapres = resp_iu->datapres;
1054 
1055 		if (datapres == 1 || datapres == 2) {
1056 			ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1057 			ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1058 		} else {
1059 			ireq->scu_status = SCU_TASK_DONE_GOOD;
1060 			ireq->sci_status = SCI_SUCCESS;
1061 		}
1062 		break;
1063 	/* only stp device gets suspended. */
1064 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
1065 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
1066 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
1067 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
1068 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
1069 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
1070 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
1071 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
1072 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
1073 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
1074 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
1075 		if (ireq->protocol == SAS_PROTOCOL_STP) {
1076 			ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1077 					   SCU_COMPLETION_TL_STATUS_SHIFT;
1078 			ireq->sci_status = SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
1079 		} else {
1080 			ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1081 					   SCU_COMPLETION_TL_STATUS_SHIFT;
1082 			ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1083 		}
1084 		break;
1085 
1086 	/* both stp/ssp device gets suspended */
1087 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
1088 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
1089 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
1090 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
1091 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
1092 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
1093 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
1094 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
1095 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
1096 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
1097 		ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1098 				   SCU_COMPLETION_TL_STATUS_SHIFT;
1099 		ireq->sci_status = SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
1100 		break;
1101 
1102 	/* neither ssp nor stp gets suspended. */
1103 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
1104 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
1105 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
1106 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
1107 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
1108 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
1109 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1110 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1111 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1112 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
1113 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
1114 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
1115 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
1116 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
1117 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
1118 	default:
1119 		ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1120 				   SCU_COMPLETION_TL_STATUS_SHIFT;
1121 		ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1122 		break;
1123 	}
1124 
1125 	/*
1126 	 * TODO: This is probably wrong for ACK/NAK timeout conditions
1127 	 */
1128 
1129 	/* In all cases we will treat this as the completion of the IO req. */
1130 	sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1131 	return SCI_SUCCESS;
1132 }
1133 
1134 static enum sci_status
1135 request_aborting_state_tc_event(struct isci_request *ireq,
1136 				u32 completion_code)
1137 {
1138 	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1139 	case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
1140 	case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
1141 		ireq->scu_status = SCU_TASK_DONE_TASK_ABORT;
1142 		ireq->sci_status = SCI_FAILURE_IO_TERMINATED;
1143 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1144 		break;
1145 
1146 	default:
1147 		/* Unless we get some strange error wait for the task abort to complete
1148 		 * TODO: Should there be a state change for this completion?
1149 		 */
1150 		break;
1151 	}
1152 
1153 	return SCI_SUCCESS;
1154 }
1155 
1156 static enum sci_status ssp_task_request_await_tc_event(struct isci_request *ireq,
1157 						       u32 completion_code)
1158 {
1159 	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1160 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1161 		ireq->scu_status = SCU_TASK_DONE_GOOD;
1162 		ireq->sci_status = SCI_SUCCESS;
1163 		sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
1164 		break;
1165 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
1166 		/* Currently, the decision is to simply allow the task request
1167 		 * to timeout if the task IU wasn't received successfully.
1168 		 * There is a potential for receiving multiple task responses if
1169 		 * we decide to send the task IU again.
1170 		 */
1171 		dev_warn(&ireq->owning_controller->pdev->dev,
1172 			 "%s: TaskRequest:0x%p CompletionCode:%x - "
1173 			 "ACK/NAK timeout\n", __func__, ireq,
1174 			 completion_code);
1175 
1176 		sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
1177 		break;
1178 	default:
1179 		/*
1180 		 * All other completion status cause the IO to be complete.
1181 		 * If a NAK was received, then it is up to the user to retry
1182 		 * the request.
1183 		 */
1184 		ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1185 		ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1186 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1187 		break;
1188 	}
1189 
1190 	return SCI_SUCCESS;
1191 }
1192 
1193 static enum sci_status
1194 smp_request_await_response_tc_event(struct isci_request *ireq,
1195 				    u32 completion_code)
1196 {
1197 	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1198 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1199 		/* In the AWAIT RESPONSE state, any TC completion is
1200 		 * unexpected.  but if the TC has success status, we
1201 		 * complete the IO anyway.
1202 		 */
1203 		ireq->scu_status = SCU_TASK_DONE_GOOD;
1204 		ireq->sci_status = SCI_SUCCESS;
1205 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1206 		break;
1207 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1208 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1209 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1210 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
1211 		/* These status has been seen in a specific LSI
1212 		 * expander, which sometimes is not able to send smp
1213 		 * response within 2 ms. This causes our hardware break
1214 		 * the connection and set TC completion with one of
1215 		 * these SMP_XXX_XX_ERR status. For these type of error,
1216 		 * we ask ihost user to retry the request.
1217 		 */
1218 		ireq->scu_status = SCU_TASK_DONE_SMP_RESP_TO_ERR;
1219 		ireq->sci_status = SCI_FAILURE_RETRY_REQUIRED;
1220 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1221 		break;
1222 	default:
1223 		/* All other completion status cause the IO to be complete.  If a NAK
1224 		 * was received, then it is up to the user to retry the request
1225 		 */
1226 		ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1227 		ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1228 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1229 		break;
1230 	}
1231 
1232 	return SCI_SUCCESS;
1233 }
1234 
1235 static enum sci_status
1236 smp_request_await_tc_event(struct isci_request *ireq,
1237 			   u32 completion_code)
1238 {
1239 	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1240 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1241 		ireq->scu_status = SCU_TASK_DONE_GOOD;
1242 		ireq->sci_status = SCI_SUCCESS;
1243 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1244 		break;
1245 	default:
1246 		/* All other completion status cause the IO to be
1247 		 * complete.  If a NAK was received, then it is up to
1248 		 * the user to retry the request.
1249 		 */
1250 		ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1251 		ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1252 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1253 		break;
1254 	}
1255 
1256 	return SCI_SUCCESS;
1257 }
1258 
1259 static struct scu_sgl_element *pio_sgl_next(struct isci_stp_request *stp_req)
1260 {
1261 	struct scu_sgl_element *sgl;
1262 	struct scu_sgl_element_pair *sgl_pair;
1263 	struct isci_request *ireq = to_ireq(stp_req);
1264 	struct isci_stp_pio_sgl *pio_sgl = &stp_req->sgl;
1265 
1266 	sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
1267 	if (!sgl_pair)
1268 		sgl = NULL;
1269 	else if (pio_sgl->set == SCU_SGL_ELEMENT_PAIR_A) {
1270 		if (sgl_pair->B.address_lower == 0 &&
1271 		    sgl_pair->B.address_upper == 0) {
1272 			sgl = NULL;
1273 		} else {
1274 			pio_sgl->set = SCU_SGL_ELEMENT_PAIR_B;
1275 			sgl = &sgl_pair->B;
1276 		}
1277 	} else {
1278 		if (sgl_pair->next_pair_lower == 0 &&
1279 		    sgl_pair->next_pair_upper == 0) {
1280 			sgl = NULL;
1281 		} else {
1282 			pio_sgl->index++;
1283 			pio_sgl->set = SCU_SGL_ELEMENT_PAIR_A;
1284 			sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
1285 			sgl = &sgl_pair->A;
1286 		}
1287 	}
1288 
1289 	return sgl;
1290 }
1291 
1292 static enum sci_status
1293 stp_request_non_data_await_h2d_tc_event(struct isci_request *ireq,
1294 					u32 completion_code)
1295 {
1296 	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1297 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1298 		ireq->scu_status = SCU_TASK_DONE_GOOD;
1299 		ireq->sci_status = SCI_SUCCESS;
1300 		sci_change_state(&ireq->sm, SCI_REQ_STP_NON_DATA_WAIT_D2H);
1301 		break;
1302 
1303 	default:
1304 		/* All other completion status cause the IO to be
1305 		 * complete.  If a NAK was received, then it is up to
1306 		 * the user to retry the request.
1307 		 */
1308 		ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1309 		ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1310 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1311 		break;
1312 	}
1313 
1314 	return SCI_SUCCESS;
1315 }
1316 
1317 #define SCU_MAX_FRAME_BUFFER_SIZE  0x400  /* 1K is the maximum SCU frame data payload */
1318 
1319 /* transmit DATA_FIS from (current sgl + offset) for input
1320  * parameter length. current sgl and offset is alreay stored in the IO request
1321  */
1322 static enum sci_status sci_stp_request_pio_data_out_trasmit_data_frame(
1323 	struct isci_request *ireq,
1324 	u32 length)
1325 {
1326 	struct isci_stp_request *stp_req = &ireq->stp.req;
1327 	struct scu_task_context *task_context = ireq->tc;
1328 	struct scu_sgl_element_pair *sgl_pair;
1329 	struct scu_sgl_element *current_sgl;
1330 
1331 	/* Recycle the TC and reconstruct it for sending out DATA FIS containing
1332 	 * for the data from current_sgl+offset for the input length
1333 	 */
1334 	sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
1335 	if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A)
1336 		current_sgl = &sgl_pair->A;
1337 	else
1338 		current_sgl = &sgl_pair->B;
1339 
1340 	/* update the TC */
1341 	task_context->command_iu_upper = current_sgl->address_upper;
1342 	task_context->command_iu_lower = current_sgl->address_lower;
1343 	task_context->transfer_length_bytes = length;
1344 	task_context->type.stp.fis_type = FIS_DATA;
1345 
1346 	/* send the new TC out. */
1347 	return sci_controller_continue_io(ireq);
1348 }
1349 
1350 static enum sci_status sci_stp_request_pio_data_out_transmit_data(struct isci_request *ireq)
1351 {
1352 	struct isci_stp_request *stp_req = &ireq->stp.req;
1353 	struct scu_sgl_element_pair *sgl_pair;
1354 	enum sci_status status = SCI_SUCCESS;
1355 	struct scu_sgl_element *sgl;
1356 	u32 offset;
1357 	u32 len = 0;
1358 
1359 	offset = stp_req->sgl.offset;
1360 	sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
1361 	if (WARN_ONCE(!sgl_pair, "%s: null sgl element", __func__))
1362 		return SCI_FAILURE;
1363 
1364 	if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A) {
1365 		sgl = &sgl_pair->A;
1366 		len = sgl_pair->A.length - offset;
1367 	} else {
1368 		sgl = &sgl_pair->B;
1369 		len = sgl_pair->B.length - offset;
1370 	}
1371 
1372 	if (stp_req->pio_len == 0)
1373 		return SCI_SUCCESS;
1374 
1375 	if (stp_req->pio_len >= len) {
1376 		status = sci_stp_request_pio_data_out_trasmit_data_frame(ireq, len);
1377 		if (status != SCI_SUCCESS)
1378 			return status;
1379 		stp_req->pio_len -= len;
1380 
1381 		/* update the current sgl, offset and save for future */
1382 		sgl = pio_sgl_next(stp_req);
1383 		offset = 0;
1384 	} else if (stp_req->pio_len < len) {
1385 		sci_stp_request_pio_data_out_trasmit_data_frame(ireq, stp_req->pio_len);
1386 
1387 		/* Sgl offset will be adjusted and saved for future */
1388 		offset += stp_req->pio_len;
1389 		sgl->address_lower += stp_req->pio_len;
1390 		stp_req->pio_len = 0;
1391 	}
1392 
1393 	stp_req->sgl.offset = offset;
1394 
1395 	return status;
1396 }
1397 
1398 /**
1399  *
1400  * @stp_request: The request that is used for the SGL processing.
1401  * @data_buffer: The buffer of data to be copied.
1402  * @length: The length of the data transfer.
1403  *
1404  * Copy the data from the buffer for the length specified to the IO request SGL
1405  * specified data region. enum sci_status
1406  */
1407 static enum sci_status
1408 sci_stp_request_pio_data_in_copy_data_buffer(struct isci_stp_request *stp_req,
1409 					     u8 *data_buf, u32 len)
1410 {
1411 	struct isci_request *ireq;
1412 	u8 *src_addr;
1413 	int copy_len;
1414 	struct sas_task *task;
1415 	struct scatterlist *sg;
1416 	void *kaddr;
1417 	int total_len = len;
1418 
1419 	ireq = to_ireq(stp_req);
1420 	task = isci_request_access_task(ireq);
1421 	src_addr = data_buf;
1422 
1423 	if (task->num_scatter > 0) {
1424 		sg = task->scatter;
1425 
1426 		while (total_len > 0) {
1427 			struct page *page = sg_page(sg);
1428 
1429 			copy_len = min_t(int, total_len, sg_dma_len(sg));
1430 			kaddr = kmap_atomic(page);
1431 			memcpy(kaddr + sg->offset, src_addr, copy_len);
1432 			kunmap_atomic(kaddr);
1433 			total_len -= copy_len;
1434 			src_addr += copy_len;
1435 			sg = sg_next(sg);
1436 		}
1437 	} else {
1438 		BUG_ON(task->total_xfer_len < total_len);
1439 		memcpy(task->scatter, src_addr, total_len);
1440 	}
1441 
1442 	return SCI_SUCCESS;
1443 }
1444 
1445 /**
1446  *
1447  * @sci_req: The PIO DATA IN request that is to receive the data.
1448  * @data_buffer: The buffer to copy from.
1449  *
1450  * Copy the data buffer to the io request data region. enum sci_status
1451  */
1452 static enum sci_status sci_stp_request_pio_data_in_copy_data(
1453 	struct isci_stp_request *stp_req,
1454 	u8 *data_buffer)
1455 {
1456 	enum sci_status status;
1457 
1458 	/*
1459 	 * If there is less than 1K remaining in the transfer request
1460 	 * copy just the data for the transfer */
1461 	if (stp_req->pio_len < SCU_MAX_FRAME_BUFFER_SIZE) {
1462 		status = sci_stp_request_pio_data_in_copy_data_buffer(
1463 			stp_req, data_buffer, stp_req->pio_len);
1464 
1465 		if (status == SCI_SUCCESS)
1466 			stp_req->pio_len = 0;
1467 	} else {
1468 		/* We are transfering the whole frame so copy */
1469 		status = sci_stp_request_pio_data_in_copy_data_buffer(
1470 			stp_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
1471 
1472 		if (status == SCI_SUCCESS)
1473 			stp_req->pio_len -= SCU_MAX_FRAME_BUFFER_SIZE;
1474 	}
1475 
1476 	return status;
1477 }
1478 
1479 static enum sci_status
1480 stp_request_pio_await_h2d_completion_tc_event(struct isci_request *ireq,
1481 					      u32 completion_code)
1482 {
1483 	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1484 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1485 		ireq->scu_status = SCU_TASK_DONE_GOOD;
1486 		ireq->sci_status = SCI_SUCCESS;
1487 		sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
1488 		break;
1489 
1490 	default:
1491 		/* All other completion status cause the IO to be
1492 		 * complete.  If a NAK was received, then it is up to
1493 		 * the user to retry the request.
1494 		 */
1495 		ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1496 		ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1497 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1498 		break;
1499 	}
1500 
1501 	return SCI_SUCCESS;
1502 }
1503 
1504 static enum sci_status
1505 pio_data_out_tx_done_tc_event(struct isci_request *ireq,
1506 			      u32 completion_code)
1507 {
1508 	enum sci_status status = SCI_SUCCESS;
1509 	bool all_frames_transferred = false;
1510 	struct isci_stp_request *stp_req = &ireq->stp.req;
1511 
1512 	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1513 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1514 		/* Transmit data */
1515 		if (stp_req->pio_len != 0) {
1516 			status = sci_stp_request_pio_data_out_transmit_data(ireq);
1517 			if (status == SCI_SUCCESS) {
1518 				if (stp_req->pio_len == 0)
1519 					all_frames_transferred = true;
1520 			}
1521 		} else if (stp_req->pio_len == 0) {
1522 			/*
1523 			 * this will happen if the all data is written at the
1524 			 * first time after the pio setup fis is received
1525 			 */
1526 			all_frames_transferred  = true;
1527 		}
1528 
1529 		/* all data transferred. */
1530 		if (all_frames_transferred) {
1531 			/*
1532 			 * Change the state to SCI_REQ_STP_PIO_DATA_IN
1533 			 * and wait for PIO_SETUP fis / or D2H REg fis. */
1534 			sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
1535 		}
1536 		break;
1537 
1538 	default:
1539 		/*
1540 		 * All other completion status cause the IO to be complete.
1541 		 * If a NAK was received, then it is up to the user to retry
1542 		 * the request.
1543 		 */
1544 		ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1545 		ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1546 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1547 		break;
1548 	}
1549 
1550 	return status;
1551 }
1552 
1553 static enum sci_status sci_stp_request_udma_general_frame_handler(struct isci_request *ireq,
1554 								       u32 frame_index)
1555 {
1556 	struct isci_host *ihost = ireq->owning_controller;
1557 	struct dev_to_host_fis *frame_header;
1558 	enum sci_status status;
1559 	u32 *frame_buffer;
1560 
1561 	status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1562 							       frame_index,
1563 							       (void **)&frame_header);
1564 
1565 	if ((status == SCI_SUCCESS) &&
1566 	    (frame_header->fis_type == FIS_REGD2H)) {
1567 		sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1568 							      frame_index,
1569 							      (void **)&frame_buffer);
1570 
1571 		sci_controller_copy_sata_response(&ireq->stp.rsp,
1572 						       frame_header,
1573 						       frame_buffer);
1574 	}
1575 
1576 	sci_controller_release_frame(ihost, frame_index);
1577 
1578 	return status;
1579 }
1580 
1581 static enum sci_status process_unsolicited_fis(struct isci_request *ireq,
1582 					       u32 frame_index)
1583 {
1584 	struct isci_host *ihost = ireq->owning_controller;
1585 	enum sci_status status;
1586 	struct dev_to_host_fis *frame_header;
1587 	u32 *frame_buffer;
1588 
1589 	status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1590 							  frame_index,
1591 							  (void **)&frame_header);
1592 
1593 	if (status != SCI_SUCCESS)
1594 		return status;
1595 
1596 	if (frame_header->fis_type != FIS_REGD2H) {
1597 		dev_err(&ireq->isci_host->pdev->dev,
1598 			"%s ERROR: invalid fis type 0x%X\n",
1599 			__func__, frame_header->fis_type);
1600 		return SCI_FAILURE;
1601 	}
1602 
1603 	sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1604 						 frame_index,
1605 						 (void **)&frame_buffer);
1606 
1607 	sci_controller_copy_sata_response(&ireq->stp.rsp,
1608 					  (u32 *)frame_header,
1609 					  frame_buffer);
1610 
1611 	/* Frame has been decoded return it to the controller */
1612 	sci_controller_release_frame(ihost, frame_index);
1613 
1614 	return status;
1615 }
1616 
1617 static enum sci_status atapi_d2h_reg_frame_handler(struct isci_request *ireq,
1618 						   u32 frame_index)
1619 {
1620 	struct sas_task *task = isci_request_access_task(ireq);
1621 	enum sci_status status;
1622 
1623 	status = process_unsolicited_fis(ireq, frame_index);
1624 
1625 	if (status == SCI_SUCCESS) {
1626 		if (ireq->stp.rsp.status & ATA_ERR)
1627 			status = SCI_FAILURE_IO_RESPONSE_VALID;
1628 	} else {
1629 		status = SCI_FAILURE_IO_RESPONSE_VALID;
1630 	}
1631 
1632 	if (status != SCI_SUCCESS) {
1633 		ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1634 		ireq->sci_status = status;
1635 	} else {
1636 		ireq->scu_status = SCU_TASK_DONE_GOOD;
1637 		ireq->sci_status = SCI_SUCCESS;
1638 	}
1639 
1640 	/* the d2h ufi is the end of non-data commands */
1641 	if (task->data_dir == DMA_NONE)
1642 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1643 
1644 	return status;
1645 }
1646 
1647 static void scu_atapi_reconstruct_raw_frame_task_context(struct isci_request *ireq)
1648 {
1649 	struct ata_device *dev = sas_to_ata_dev(ireq->target_device->domain_dev);
1650 	void *atapi_cdb = ireq->ttype_ptr.io_task_ptr->ata_task.atapi_packet;
1651 	struct scu_task_context *task_context = ireq->tc;
1652 
1653 	/* fill in the SCU Task Context for a DATA fis containing CDB in Raw Frame
1654 	 * type. The TC for previous Packet fis was already there, we only need to
1655 	 * change the H2D fis content.
1656 	 */
1657 	memset(&ireq->stp.cmd, 0, sizeof(struct host_to_dev_fis));
1658 	memcpy(((u8 *)&ireq->stp.cmd + sizeof(u32)), atapi_cdb, ATAPI_CDB_LEN);
1659 	memset(&(task_context->type.stp), 0, sizeof(struct stp_task_context));
1660 	task_context->type.stp.fis_type = FIS_DATA;
1661 	task_context->transfer_length_bytes = dev->cdb_len;
1662 }
1663 
1664 static void scu_atapi_construct_task_context(struct isci_request *ireq)
1665 {
1666 	struct ata_device *dev = sas_to_ata_dev(ireq->target_device->domain_dev);
1667 	struct sas_task *task = isci_request_access_task(ireq);
1668 	struct scu_task_context *task_context = ireq->tc;
1669 	int cdb_len = dev->cdb_len;
1670 
1671 	/* reference: SSTL 1.13.4.2
1672 	 * task_type, sata_direction
1673 	 */
1674 	if (task->data_dir == DMA_TO_DEVICE) {
1675 		task_context->task_type = SCU_TASK_TYPE_PACKET_DMA_OUT;
1676 		task_context->sata_direction = 0;
1677 	} else {
1678 		/* todo: for NO_DATA command, we need to send out raw frame. */
1679 		task_context->task_type = SCU_TASK_TYPE_PACKET_DMA_IN;
1680 		task_context->sata_direction = 1;
1681 	}
1682 
1683 	memset(&task_context->type.stp, 0, sizeof(task_context->type.stp));
1684 	task_context->type.stp.fis_type = FIS_DATA;
1685 
1686 	memset(&ireq->stp.cmd, 0, sizeof(ireq->stp.cmd));
1687 	memcpy(&ireq->stp.cmd.lbal, task->ata_task.atapi_packet, cdb_len);
1688 	task_context->ssp_command_iu_length = cdb_len / sizeof(u32);
1689 
1690 	/* task phase is set to TX_CMD */
1691 	task_context->task_phase = 0x1;
1692 
1693 	/* retry counter */
1694 	task_context->stp_retry_count = 0;
1695 
1696 	/* data transfer size. */
1697 	task_context->transfer_length_bytes = task->total_xfer_len;
1698 
1699 	/* setup sgl */
1700 	sci_request_build_sgl(ireq);
1701 }
1702 
1703 enum sci_status
1704 sci_io_request_frame_handler(struct isci_request *ireq,
1705 				  u32 frame_index)
1706 {
1707 	struct isci_host *ihost = ireq->owning_controller;
1708 	struct isci_stp_request *stp_req = &ireq->stp.req;
1709 	enum sci_base_request_states state;
1710 	enum sci_status status;
1711 	ssize_t word_cnt;
1712 
1713 	state = ireq->sm.current_state_id;
1714 	switch (state)  {
1715 	case SCI_REQ_STARTED: {
1716 		struct ssp_frame_hdr ssp_hdr;
1717 		void *frame_header;
1718 
1719 		sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1720 							      frame_index,
1721 							      &frame_header);
1722 
1723 		word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
1724 		sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);
1725 
1726 		if (ssp_hdr.frame_type == SSP_RESPONSE) {
1727 			struct ssp_response_iu *resp_iu;
1728 			ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1729 
1730 			sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1731 								      frame_index,
1732 								      (void **)&resp_iu);
1733 
1734 			sci_swab32_cpy(&ireq->ssp.rsp, resp_iu, word_cnt);
1735 
1736 			resp_iu = &ireq->ssp.rsp;
1737 
1738 			if (resp_iu->datapres == 0x01 ||
1739 			    resp_iu->datapres == 0x02) {
1740 				ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1741 				ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1742 			} else {
1743 				ireq->scu_status = SCU_TASK_DONE_GOOD;
1744 				ireq->sci_status = SCI_SUCCESS;
1745 			}
1746 		} else {
1747 			/* not a response frame, why did it get forwarded? */
1748 			dev_err(&ihost->pdev->dev,
1749 				"%s: SCIC IO Request 0x%p received unexpected "
1750 				"frame %d type 0x%02x\n", __func__, ireq,
1751 				frame_index, ssp_hdr.frame_type);
1752 		}
1753 
1754 		/*
1755 		 * In any case we are done with this frame buffer return it to
1756 		 * the controller
1757 		 */
1758 		sci_controller_release_frame(ihost, frame_index);
1759 
1760 		return SCI_SUCCESS;
1761 	}
1762 
1763 	case SCI_REQ_TASK_WAIT_TC_RESP:
1764 		sci_io_request_copy_response(ireq);
1765 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1766 		sci_controller_release_frame(ihost, frame_index);
1767 		return SCI_SUCCESS;
1768 
1769 	case SCI_REQ_SMP_WAIT_RESP: {
1770 		struct sas_task *task = isci_request_access_task(ireq);
1771 		struct scatterlist *sg = &task->smp_task.smp_resp;
1772 		void *frame_header, *kaddr;
1773 		u8 *rsp;
1774 
1775 		sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1776 							 frame_index,
1777 							 &frame_header);
1778 		kaddr = kmap_atomic(sg_page(sg));
1779 		rsp = kaddr + sg->offset;
1780 		sci_swab32_cpy(rsp, frame_header, 1);
1781 
1782 		if (rsp[0] == SMP_RESPONSE) {
1783 			void *smp_resp;
1784 
1785 			sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1786 								 frame_index,
1787 								 &smp_resp);
1788 
1789 			word_cnt = (sg->length/4)-1;
1790 			if (word_cnt > 0)
1791 				word_cnt = min_t(unsigned int, word_cnt,
1792 						 SCU_UNSOLICITED_FRAME_BUFFER_SIZE/4);
1793 			sci_swab32_cpy(rsp + 4, smp_resp, word_cnt);
1794 
1795 			ireq->scu_status = SCU_TASK_DONE_GOOD;
1796 			ireq->sci_status = SCI_SUCCESS;
1797 			sci_change_state(&ireq->sm, SCI_REQ_SMP_WAIT_TC_COMP);
1798 		} else {
1799 			/*
1800 			 * This was not a response frame why did it get
1801 			 * forwarded?
1802 			 */
1803 			dev_err(&ihost->pdev->dev,
1804 				"%s: SCIC SMP Request 0x%p received unexpected "
1805 				"frame %d type 0x%02x\n",
1806 				__func__,
1807 				ireq,
1808 				frame_index,
1809 				rsp[0]);
1810 
1811 			ireq->scu_status = SCU_TASK_DONE_SMP_FRM_TYPE_ERR;
1812 			ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1813 			sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1814 		}
1815 		kunmap_atomic(kaddr);
1816 
1817 		sci_controller_release_frame(ihost, frame_index);
1818 
1819 		return SCI_SUCCESS;
1820 	}
1821 
1822 	case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
1823 		return sci_stp_request_udma_general_frame_handler(ireq,
1824 								       frame_index);
1825 
1826 	case SCI_REQ_STP_UDMA_WAIT_D2H:
1827 		/* Use the general frame handler to copy the resposne data */
1828 		status = sci_stp_request_udma_general_frame_handler(ireq, frame_index);
1829 
1830 		if (status != SCI_SUCCESS)
1831 			return status;
1832 
1833 		ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1834 		ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1835 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1836 		return SCI_SUCCESS;
1837 
1838 	case SCI_REQ_STP_NON_DATA_WAIT_D2H: {
1839 		struct dev_to_host_fis *frame_header;
1840 		u32 *frame_buffer;
1841 
1842 		status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1843 								       frame_index,
1844 								       (void **)&frame_header);
1845 
1846 		if (status != SCI_SUCCESS) {
1847 			dev_err(&ihost->pdev->dev,
1848 				"%s: SCIC IO Request 0x%p could not get frame "
1849 				"header for frame index %d, status %x\n",
1850 				__func__,
1851 				stp_req,
1852 				frame_index,
1853 				status);
1854 
1855 			return status;
1856 		}
1857 
1858 		switch (frame_header->fis_type) {
1859 		case FIS_REGD2H:
1860 			sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1861 								      frame_index,
1862 								      (void **)&frame_buffer);
1863 
1864 			sci_controller_copy_sata_response(&ireq->stp.rsp,
1865 							       frame_header,
1866 							       frame_buffer);
1867 
1868 			/* The command has completed with error */
1869 			ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1870 			ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1871 			break;
1872 
1873 		default:
1874 			dev_warn(&ihost->pdev->dev,
1875 				 "%s: IO Request:0x%p Frame Id:%d protocol "
1876 				  "violation occurred\n", __func__, stp_req,
1877 				  frame_index);
1878 
1879 			ireq->scu_status = SCU_TASK_DONE_UNEXP_FIS;
1880 			ireq->sci_status = SCI_FAILURE_PROTOCOL_VIOLATION;
1881 			break;
1882 		}
1883 
1884 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1885 
1886 		/* Frame has been decoded return it to the controller */
1887 		sci_controller_release_frame(ihost, frame_index);
1888 
1889 		return status;
1890 	}
1891 
1892 	case SCI_REQ_STP_PIO_WAIT_FRAME: {
1893 		struct sas_task *task = isci_request_access_task(ireq);
1894 		struct dev_to_host_fis *frame_header;
1895 		u32 *frame_buffer;
1896 
1897 		status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1898 								       frame_index,
1899 								       (void **)&frame_header);
1900 
1901 		if (status != SCI_SUCCESS) {
1902 			dev_err(&ihost->pdev->dev,
1903 				"%s: SCIC IO Request 0x%p could not get frame "
1904 				"header for frame index %d, status %x\n",
1905 				__func__, stp_req, frame_index, status);
1906 			return status;
1907 		}
1908 
1909 		switch (frame_header->fis_type) {
1910 		case FIS_PIO_SETUP:
1911 			/* Get from the frame buffer the PIO Setup Data */
1912 			sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1913 								      frame_index,
1914 								      (void **)&frame_buffer);
1915 
1916 			/* Get the data from the PIO Setup The SCU Hardware
1917 			 * returns first word in the frame_header and the rest
1918 			 * of the data is in the frame buffer so we need to
1919 			 * back up one dword
1920 			 */
1921 
1922 			/* transfer_count: first 16bits in the 4th dword */
1923 			stp_req->pio_len = frame_buffer[3] & 0xffff;
1924 
1925 			/* status: 4th byte in the 3rd dword */
1926 			stp_req->status = (frame_buffer[2] >> 24) & 0xff;
1927 
1928 			sci_controller_copy_sata_response(&ireq->stp.rsp,
1929 							       frame_header,
1930 							       frame_buffer);
1931 
1932 			ireq->stp.rsp.status = stp_req->status;
1933 
1934 			/* The next state is dependent on whether the
1935 			 * request was PIO Data-in or Data out
1936 			 */
1937 			if (task->data_dir == DMA_FROM_DEVICE) {
1938 				sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_IN);
1939 			} else if (task->data_dir == DMA_TO_DEVICE) {
1940 				/* Transmit data */
1941 				status = sci_stp_request_pio_data_out_transmit_data(ireq);
1942 				if (status != SCI_SUCCESS)
1943 					break;
1944 				sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_OUT);
1945 			}
1946 			break;
1947 
1948 		case FIS_SETDEVBITS:
1949 			sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
1950 			break;
1951 
1952 		case FIS_REGD2H:
1953 			if (frame_header->status & ATA_BUSY) {
1954 				/*
1955 				 * Now why is the drive sending a D2H Register
1956 				 * FIS when it is still busy?  Do nothing since
1957 				 * we are still in the right state.
1958 				 */
1959 				dev_dbg(&ihost->pdev->dev,
1960 					"%s: SCIC PIO Request 0x%p received "
1961 					"D2H Register FIS with BSY status "
1962 					"0x%x\n",
1963 					__func__,
1964 					stp_req,
1965 					frame_header->status);
1966 				break;
1967 			}
1968 
1969 			sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1970 								      frame_index,
1971 								      (void **)&frame_buffer);
1972 
1973 			sci_controller_copy_sata_response(&ireq->stp.rsp,
1974 							       frame_header,
1975 							       frame_buffer);
1976 
1977 			ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1978 			ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1979 			sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1980 			break;
1981 
1982 		default:
1983 			/* FIXME: what do we do here? */
1984 			break;
1985 		}
1986 
1987 		/* Frame is decoded return it to the controller */
1988 		sci_controller_release_frame(ihost, frame_index);
1989 
1990 		return status;
1991 	}
1992 
1993 	case SCI_REQ_STP_PIO_DATA_IN: {
1994 		struct dev_to_host_fis *frame_header;
1995 		struct sata_fis_data *frame_buffer;
1996 
1997 		status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1998 								       frame_index,
1999 								       (void **)&frame_header);
2000 
2001 		if (status != SCI_SUCCESS) {
2002 			dev_err(&ihost->pdev->dev,
2003 				"%s: SCIC IO Request 0x%p could not get frame "
2004 				"header for frame index %d, status %x\n",
2005 				__func__,
2006 				stp_req,
2007 				frame_index,
2008 				status);
2009 			return status;
2010 		}
2011 
2012 		if (frame_header->fis_type != FIS_DATA) {
2013 			dev_err(&ihost->pdev->dev,
2014 				"%s: SCIC PIO Request 0x%p received frame %d "
2015 				"with fis type 0x%02x when expecting a data "
2016 				"fis.\n",
2017 				__func__,
2018 				stp_req,
2019 				frame_index,
2020 				frame_header->fis_type);
2021 
2022 			ireq->scu_status = SCU_TASK_DONE_GOOD;
2023 			ireq->sci_status = SCI_FAILURE_IO_REQUIRES_SCSI_ABORT;
2024 			sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2025 
2026 			/* Frame is decoded return it to the controller */
2027 			sci_controller_release_frame(ihost, frame_index);
2028 			return status;
2029 		}
2030 
2031 		if (stp_req->sgl.index < 0) {
2032 			ireq->saved_rx_frame_index = frame_index;
2033 			stp_req->pio_len = 0;
2034 		} else {
2035 			sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
2036 								      frame_index,
2037 								      (void **)&frame_buffer);
2038 
2039 			status = sci_stp_request_pio_data_in_copy_data(stp_req,
2040 									    (u8 *)frame_buffer);
2041 
2042 			/* Frame is decoded return it to the controller */
2043 			sci_controller_release_frame(ihost, frame_index);
2044 		}
2045 
2046 		/* Check for the end of the transfer, are there more
2047 		 * bytes remaining for this data transfer
2048 		 */
2049 		if (status != SCI_SUCCESS || stp_req->pio_len != 0)
2050 			return status;
2051 
2052 		if ((stp_req->status & ATA_BUSY) == 0) {
2053 			ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
2054 			ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
2055 			sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2056 		} else {
2057 			sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
2058 		}
2059 		return status;
2060 	}
2061 
2062 	case SCI_REQ_ATAPI_WAIT_PIO_SETUP: {
2063 		struct sas_task *task = isci_request_access_task(ireq);
2064 
2065 		sci_controller_release_frame(ihost, frame_index);
2066 		ireq->target_device->working_request = ireq;
2067 		if (task->data_dir == DMA_NONE) {
2068 			sci_change_state(&ireq->sm, SCI_REQ_ATAPI_WAIT_TC_COMP);
2069 			scu_atapi_reconstruct_raw_frame_task_context(ireq);
2070 		} else {
2071 			sci_change_state(&ireq->sm, SCI_REQ_ATAPI_WAIT_D2H);
2072 			scu_atapi_construct_task_context(ireq);
2073 		}
2074 
2075 		sci_controller_continue_io(ireq);
2076 		return SCI_SUCCESS;
2077 	}
2078 	case SCI_REQ_ATAPI_WAIT_D2H:
2079 		return atapi_d2h_reg_frame_handler(ireq, frame_index);
2080 	case SCI_REQ_ABORTING:
2081 		/*
2082 		 * TODO: Is it even possible to get an unsolicited frame in the
2083 		 * aborting state?
2084 		 */
2085 		sci_controller_release_frame(ihost, frame_index);
2086 		return SCI_SUCCESS;
2087 
2088 	default:
2089 		dev_warn(&ihost->pdev->dev,
2090 			 "%s: SCIC IO Request given unexpected frame %x while "
2091 			 "in state %d\n",
2092 			 __func__,
2093 			 frame_index,
2094 			 state);
2095 
2096 		sci_controller_release_frame(ihost, frame_index);
2097 		return SCI_FAILURE_INVALID_STATE;
2098 	}
2099 }
2100 
2101 static enum sci_status stp_request_udma_await_tc_event(struct isci_request *ireq,
2102 						       u32 completion_code)
2103 {
2104 	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2105 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
2106 		ireq->scu_status = SCU_TASK_DONE_GOOD;
2107 		ireq->sci_status = SCI_SUCCESS;
2108 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2109 		break;
2110 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
2111 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
2112 		/* We must check ther response buffer to see if the D2H
2113 		 * Register FIS was received before we got the TC
2114 		 * completion.
2115 		 */
2116 		if (ireq->stp.rsp.fis_type == FIS_REGD2H) {
2117 			sci_remote_device_suspend(ireq->target_device,
2118 						  SCI_SW_SUSPEND_NORMAL);
2119 
2120 			ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
2121 			ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
2122 			sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2123 		} else {
2124 			/* If we have an error completion status for the
2125 			 * TC then we can expect a D2H register FIS from
2126 			 * the device so we must change state to wait
2127 			 * for it
2128 			 */
2129 			sci_change_state(&ireq->sm, SCI_REQ_STP_UDMA_WAIT_D2H);
2130 		}
2131 		break;
2132 
2133 	/* TODO Check to see if any of these completion status need to
2134 	 * wait for the device to host register fis.
2135 	 */
2136 	/* TODO We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR
2137 	 * - this comes only for B0
2138 	 */
2139 	default:
2140 		/* All other completion status cause the IO to be complete. */
2141 		ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
2142 		ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
2143 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2144 		break;
2145 	}
2146 
2147 	return SCI_SUCCESS;
2148 }
2149 
2150 static enum sci_status atapi_raw_completion(struct isci_request *ireq, u32 completion_code,
2151 						  enum sci_base_request_states next)
2152 {
2153 	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2154 	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
2155 		ireq->scu_status = SCU_TASK_DONE_GOOD;
2156 		ireq->sci_status = SCI_SUCCESS;
2157 		sci_change_state(&ireq->sm, next);
2158 		break;
2159 	default:
2160 		/* All other completion status cause the IO to be complete.
2161 		 * If a NAK was received, then it is up to the user to retry
2162 		 * the request.
2163 		 */
2164 		ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
2165 		ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
2166 
2167 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2168 		break;
2169 	}
2170 
2171 	return SCI_SUCCESS;
2172 }
2173 
2174 static enum sci_status atapi_data_tc_completion_handler(struct isci_request *ireq,
2175 							u32 completion_code)
2176 {
2177 	struct isci_remote_device *idev = ireq->target_device;
2178 	struct dev_to_host_fis *d2h = &ireq->stp.rsp;
2179 	enum sci_status status = SCI_SUCCESS;
2180 
2181 	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2182 	case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
2183 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2184 		break;
2185 
2186 	case (SCU_TASK_DONE_UNEXP_FIS << SCU_COMPLETION_TL_STATUS_SHIFT): {
2187 		u16 len = sci_req_tx_bytes(ireq);
2188 
2189 		/* likely non-error data underrrun, workaround missing
2190 		 * d2h frame from the controller
2191 		 */
2192 		if (d2h->fis_type != FIS_REGD2H) {
2193 			d2h->fis_type = FIS_REGD2H;
2194 			d2h->flags = (1 << 6);
2195 			d2h->status = 0x50;
2196 			d2h->error = 0;
2197 			d2h->lbal = 0;
2198 			d2h->byte_count_low = len & 0xff;
2199 			d2h->byte_count_high = len >> 8;
2200 			d2h->device = 0xa0;
2201 			d2h->lbal_exp = 0;
2202 			d2h->lbam_exp = 0;
2203 			d2h->lbah_exp = 0;
2204 			d2h->_r_a = 0;
2205 			d2h->sector_count = 0x3;
2206 			d2h->sector_count_exp = 0;
2207 			d2h->_r_b = 0;
2208 			d2h->_r_c = 0;
2209 			d2h->_r_d = 0;
2210 		}
2211 
2212 		ireq->scu_status = SCU_TASK_DONE_GOOD;
2213 		ireq->sci_status = SCI_SUCCESS_IO_DONE_EARLY;
2214 		status = ireq->sci_status;
2215 
2216 		/* the hw will have suspended the rnc, so complete the
2217 		 * request upon pending resume
2218 		 */
2219 		sci_change_state(&idev->sm, SCI_STP_DEV_ATAPI_ERROR);
2220 		break;
2221 	}
2222 	case (SCU_TASK_DONE_EXCESS_DATA << SCU_COMPLETION_TL_STATUS_SHIFT):
2223 		/* In this case, there is no UF coming after.
2224 		 * compelte the IO now.
2225 		 */
2226 		ireq->scu_status = SCU_TASK_DONE_GOOD;
2227 		ireq->sci_status = SCI_SUCCESS;
2228 		sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2229 		break;
2230 
2231 	default:
2232 		if (d2h->fis_type == FIS_REGD2H) {
2233 			/* UF received change the device state to ATAPI_ERROR */
2234 			status = ireq->sci_status;
2235 			sci_change_state(&idev->sm, SCI_STP_DEV_ATAPI_ERROR);
2236 		} else {
2237 			/* If receiving any non-success TC status, no UF
2238 			 * received yet, then an UF for the status fis
2239 			 * is coming after (XXX: suspect this is
2240 			 * actually a protocol error or a bug like the
2241 			 * DONE_UNEXP_FIS case)
2242 			 */
2243 			ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
2244 			ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
2245 
2246 			sci_change_state(&ireq->sm, SCI_REQ_ATAPI_WAIT_D2H);
2247 		}
2248 		break;
2249 	}
2250 
2251 	return status;
2252 }
2253 
2254 static int sci_request_smp_completion_status_is_tx_suspend(
2255 	unsigned int completion_status)
2256 {
2257 	switch (completion_status) {
2258 	case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2259 	case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2260 	case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2261 	case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2262 	case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2263 	case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2264 		return 1;
2265 	}
2266 	return 0;
2267 }
2268 
2269 static int sci_request_smp_completion_status_is_tx_rx_suspend(
2270 	unsigned int completion_status)
2271 {
2272 	return 0; /* There are no Tx/Rx SMP suspend conditions. */
2273 }
2274 
2275 static int sci_request_ssp_completion_status_is_tx_suspend(
2276 	unsigned int completion_status)
2277 {
2278 	switch (completion_status) {
2279 	case SCU_TASK_DONE_TX_RAW_CMD_ERR:
2280 	case SCU_TASK_DONE_LF_ERR:
2281 	case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2282 	case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2283 	case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2284 	case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2285 	case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2286 	case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2287 	case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
2288 	case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
2289 	case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
2290 		return 1;
2291 	}
2292 	return 0;
2293 }
2294 
2295 static int sci_request_ssp_completion_status_is_tx_rx_suspend(
2296 	unsigned int completion_status)
2297 {
2298 	return 0; /* There are no Tx/Rx SSP suspend conditions. */
2299 }
2300 
2301 static int sci_request_stpsata_completion_status_is_tx_suspend(
2302 	unsigned int completion_status)
2303 {
2304 	switch (completion_status) {
2305 	case SCU_TASK_DONE_TX_RAW_CMD_ERR:
2306 	case SCU_TASK_DONE_LL_R_ERR:
2307 	case SCU_TASK_DONE_LL_PERR:
2308 	case SCU_TASK_DONE_REG_ERR:
2309 	case SCU_TASK_DONE_SDB_ERR:
2310 	case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2311 	case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2312 	case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2313 	case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2314 	case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2315 	case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2316 	case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
2317 	case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
2318 	case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
2319 		return 1;
2320 	}
2321 	return 0;
2322 }
2323 
2324 
2325 static int sci_request_stpsata_completion_status_is_tx_rx_suspend(
2326 	unsigned int completion_status)
2327 {
2328 	switch (completion_status) {
2329 	case SCU_TASK_DONE_LF_ERR:
2330 	case SCU_TASK_DONE_LL_SY_TERM:
2331 	case SCU_TASK_DONE_LL_LF_TERM:
2332 	case SCU_TASK_DONE_BREAK_RCVD:
2333 	case SCU_TASK_DONE_INV_FIS_LEN:
2334 	case SCU_TASK_DONE_UNEXP_FIS:
2335 	case SCU_TASK_DONE_UNEXP_SDBFIS:
2336 	case SCU_TASK_DONE_MAX_PLD_ERR:
2337 		return 1;
2338 	}
2339 	return 0;
2340 }
2341 
2342 static void sci_request_handle_suspending_completions(
2343 	struct isci_request *ireq,
2344 	u32 completion_code)
2345 {
2346 	int is_tx = 0;
2347 	int is_tx_rx = 0;
2348 
2349 	switch (ireq->protocol) {
2350 	case SAS_PROTOCOL_SMP:
2351 		is_tx = sci_request_smp_completion_status_is_tx_suspend(
2352 			completion_code);
2353 		is_tx_rx = sci_request_smp_completion_status_is_tx_rx_suspend(
2354 			completion_code);
2355 		break;
2356 	case SAS_PROTOCOL_SSP:
2357 		is_tx = sci_request_ssp_completion_status_is_tx_suspend(
2358 			completion_code);
2359 		is_tx_rx = sci_request_ssp_completion_status_is_tx_rx_suspend(
2360 			completion_code);
2361 		break;
2362 	case SAS_PROTOCOL_STP:
2363 		is_tx = sci_request_stpsata_completion_status_is_tx_suspend(
2364 			completion_code);
2365 		is_tx_rx =
2366 			sci_request_stpsata_completion_status_is_tx_rx_suspend(
2367 				completion_code);
2368 		break;
2369 	default:
2370 		dev_warn(&ireq->isci_host->pdev->dev,
2371 			 "%s: request %p has no valid protocol\n",
2372 			 __func__, ireq);
2373 		break;
2374 	}
2375 	if (is_tx || is_tx_rx) {
2376 		BUG_ON(is_tx && is_tx_rx);
2377 
2378 		sci_remote_node_context_suspend(
2379 			&ireq->target_device->rnc,
2380 			SCI_HW_SUSPEND,
2381 			(is_tx_rx) ? SCU_EVENT_TL_RNC_SUSPEND_TX_RX
2382 				   : SCU_EVENT_TL_RNC_SUSPEND_TX);
2383 	}
2384 }
2385 
2386 enum sci_status
2387 sci_io_request_tc_completion(struct isci_request *ireq,
2388 			     u32 completion_code)
2389 {
2390 	enum sci_base_request_states state;
2391 	struct isci_host *ihost = ireq->owning_controller;
2392 
2393 	state = ireq->sm.current_state_id;
2394 
2395 	/* Decode those completions that signal upcoming suspension events. */
2396 	sci_request_handle_suspending_completions(
2397 		ireq, SCU_GET_COMPLETION_TL_STATUS(completion_code));
2398 
2399 	switch (state) {
2400 	case SCI_REQ_STARTED:
2401 		return request_started_state_tc_event(ireq, completion_code);
2402 
2403 	case SCI_REQ_TASK_WAIT_TC_COMP:
2404 		return ssp_task_request_await_tc_event(ireq,
2405 						       completion_code);
2406 
2407 	case SCI_REQ_SMP_WAIT_RESP:
2408 		return smp_request_await_response_tc_event(ireq,
2409 							   completion_code);
2410 
2411 	case SCI_REQ_SMP_WAIT_TC_COMP:
2412 		return smp_request_await_tc_event(ireq, completion_code);
2413 
2414 	case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
2415 		return stp_request_udma_await_tc_event(ireq,
2416 						       completion_code);
2417 
2418 	case SCI_REQ_STP_NON_DATA_WAIT_H2D:
2419 		return stp_request_non_data_await_h2d_tc_event(ireq,
2420 							       completion_code);
2421 
2422 	case SCI_REQ_STP_PIO_WAIT_H2D:
2423 		return stp_request_pio_await_h2d_completion_tc_event(ireq,
2424 								     completion_code);
2425 
2426 	case SCI_REQ_STP_PIO_DATA_OUT:
2427 		return pio_data_out_tx_done_tc_event(ireq, completion_code);
2428 
2429 	case SCI_REQ_ABORTING:
2430 		return request_aborting_state_tc_event(ireq,
2431 						       completion_code);
2432 
2433 	case SCI_REQ_ATAPI_WAIT_H2D:
2434 		return atapi_raw_completion(ireq, completion_code,
2435 					    SCI_REQ_ATAPI_WAIT_PIO_SETUP);
2436 
2437 	case SCI_REQ_ATAPI_WAIT_TC_COMP:
2438 		return atapi_raw_completion(ireq, completion_code,
2439 					    SCI_REQ_ATAPI_WAIT_D2H);
2440 
2441 	case SCI_REQ_ATAPI_WAIT_D2H:
2442 		return atapi_data_tc_completion_handler(ireq, completion_code);
2443 
2444 	default:
2445 		dev_warn(&ihost->pdev->dev, "%s: %x in wrong state %s\n",
2446 			 __func__, completion_code, req_state_name(state));
2447 		return SCI_FAILURE_INVALID_STATE;
2448 	}
2449 }
2450 
2451 /**
2452  * isci_request_process_response_iu() - This function sets the status and
2453  *    response iu, in the task struct, from the request object for the upper
2454  *    layer driver.
2455  * @sas_task: This parameter is the task struct from the upper layer driver.
2456  * @resp_iu: This parameter points to the response iu of the completed request.
2457  * @dev: This parameter specifies the linux device struct.
2458  *
2459  * none.
2460  */
2461 static void isci_request_process_response_iu(
2462 	struct sas_task *task,
2463 	struct ssp_response_iu *resp_iu,
2464 	struct device *dev)
2465 {
2466 	dev_dbg(dev,
2467 		"%s: resp_iu = %p "
2468 		"resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
2469 		"resp_iu->response_data_len = %x, "
2470 		"resp_iu->sense_data_len = %x\nresponse data: ",
2471 		__func__,
2472 		resp_iu,
2473 		resp_iu->status,
2474 		resp_iu->datapres,
2475 		resp_iu->response_data_len,
2476 		resp_iu->sense_data_len);
2477 
2478 	task->task_status.stat = resp_iu->status;
2479 
2480 	/* libsas updates the task status fields based on the response iu. */
2481 	sas_ssp_task_response(dev, task, resp_iu);
2482 }
2483 
2484 /**
2485  * isci_request_set_open_reject_status() - This function prepares the I/O
2486  *    completion for OPEN_REJECT conditions.
2487  * @request: This parameter is the completed isci_request object.
2488  * @response_ptr: This parameter specifies the service response for the I/O.
2489  * @status_ptr: This parameter specifies the exec status for the I/O.
2490  * @open_rej_reason: This parameter specifies the encoded reason for the
2491  *    abandon-class reject.
2492  *
2493  * none.
2494  */
2495 static void isci_request_set_open_reject_status(
2496 	struct isci_request *request,
2497 	struct sas_task *task,
2498 	enum service_response *response_ptr,
2499 	enum exec_status *status_ptr,
2500 	enum sas_open_rej_reason open_rej_reason)
2501 {
2502 	/* Task in the target is done. */
2503 	set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2504 	*response_ptr                     = SAS_TASK_UNDELIVERED;
2505 	*status_ptr                       = SAS_OPEN_REJECT;
2506 	task->task_status.open_rej_reason = open_rej_reason;
2507 }
2508 
2509 /**
2510  * isci_request_handle_controller_specific_errors() - This function decodes
2511  *    controller-specific I/O completion error conditions.
2512  * @request: This parameter is the completed isci_request object.
2513  * @response_ptr: This parameter specifies the service response for the I/O.
2514  * @status_ptr: This parameter specifies the exec status for the I/O.
2515  *
2516  * none.
2517  */
2518 static void isci_request_handle_controller_specific_errors(
2519 	struct isci_remote_device *idev,
2520 	struct isci_request *request,
2521 	struct sas_task *task,
2522 	enum service_response *response_ptr,
2523 	enum exec_status *status_ptr)
2524 {
2525 	unsigned int cstatus;
2526 
2527 	cstatus = request->scu_status;
2528 
2529 	dev_dbg(&request->isci_host->pdev->dev,
2530 		"%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
2531 		"- controller status = 0x%x\n",
2532 		__func__, request, cstatus);
2533 
2534 	/* Decode the controller-specific errors; most
2535 	 * important is to recognize those conditions in which
2536 	 * the target may still have a task outstanding that
2537 	 * must be aborted.
2538 	 *
2539 	 * Note that there are SCU completion codes being
2540 	 * named in the decode below for which SCIC has already
2541 	 * done work to handle them in a way other than as
2542 	 * a controller-specific completion code; these are left
2543 	 * in the decode below for completeness sake.
2544 	 */
2545 	switch (cstatus) {
2546 	case SCU_TASK_DONE_DMASETUP_DIRERR:
2547 	/* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
2548 	case SCU_TASK_DONE_XFERCNT_ERR:
2549 		/* Also SCU_TASK_DONE_SMP_UFI_ERR: */
2550 		if (task->task_proto == SAS_PROTOCOL_SMP) {
2551 			/* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
2552 			*response_ptr = SAS_TASK_COMPLETE;
2553 
2554 			/* See if the device has been/is being stopped. Note
2555 			 * that we ignore the quiesce state, since we are
2556 			 * concerned about the actual device state.
2557 			 */
2558 			if (!idev)
2559 				*status_ptr = SAS_DEVICE_UNKNOWN;
2560 			else
2561 				*status_ptr = SAS_ABORTED_TASK;
2562 
2563 			set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2564 		} else {
2565 			/* Task in the target is not done. */
2566 			*response_ptr = SAS_TASK_UNDELIVERED;
2567 
2568 			if (!idev)
2569 				*status_ptr = SAS_DEVICE_UNKNOWN;
2570 			else
2571 				*status_ptr = SAM_STAT_TASK_ABORTED;
2572 
2573 			clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2574 		}
2575 
2576 		break;
2577 
2578 	case SCU_TASK_DONE_CRC_ERR:
2579 	case SCU_TASK_DONE_NAK_CMD_ERR:
2580 	case SCU_TASK_DONE_EXCESS_DATA:
2581 	case SCU_TASK_DONE_UNEXP_FIS:
2582 	/* Also SCU_TASK_DONE_UNEXP_RESP: */
2583 	case SCU_TASK_DONE_VIIT_ENTRY_NV:       /* TODO - conditions? */
2584 	case SCU_TASK_DONE_IIT_ENTRY_NV:        /* TODO - conditions? */
2585 	case SCU_TASK_DONE_RNCNV_OUTBOUND:      /* TODO - conditions? */
2586 		/* These are conditions in which the target
2587 		 * has completed the task, so that no cleanup
2588 		 * is necessary.
2589 		 */
2590 		*response_ptr = SAS_TASK_COMPLETE;
2591 
2592 		/* See if the device has been/is being stopped. Note
2593 		 * that we ignore the quiesce state, since we are
2594 		 * concerned about the actual device state.
2595 		 */
2596 		if (!idev)
2597 			*status_ptr = SAS_DEVICE_UNKNOWN;
2598 		else
2599 			*status_ptr = SAS_ABORTED_TASK;
2600 
2601 		set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2602 		break;
2603 
2604 
2605 	/* Note that the only open reject completion codes seen here will be
2606 	 * abandon-class codes; all others are automatically retried in the SCU.
2607 	 */
2608 	case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2609 
2610 		isci_request_set_open_reject_status(
2611 			request, task, response_ptr, status_ptr,
2612 			SAS_OREJ_WRONG_DEST);
2613 		break;
2614 
2615 	case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2616 
2617 		/* Note - the return of AB0 will change when
2618 		 * libsas implements detection of zone violations.
2619 		 */
2620 		isci_request_set_open_reject_status(
2621 			request, task, response_ptr, status_ptr,
2622 			SAS_OREJ_RESV_AB0);
2623 		break;
2624 
2625 	case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2626 
2627 		isci_request_set_open_reject_status(
2628 			request, task, response_ptr, status_ptr,
2629 			SAS_OREJ_RESV_AB1);
2630 		break;
2631 
2632 	case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2633 
2634 		isci_request_set_open_reject_status(
2635 			request, task, response_ptr, status_ptr,
2636 			SAS_OREJ_RESV_AB2);
2637 		break;
2638 
2639 	case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2640 
2641 		isci_request_set_open_reject_status(
2642 			request, task, response_ptr, status_ptr,
2643 			SAS_OREJ_RESV_AB3);
2644 		break;
2645 
2646 	case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2647 
2648 		isci_request_set_open_reject_status(
2649 			request, task, response_ptr, status_ptr,
2650 			SAS_OREJ_BAD_DEST);
2651 		break;
2652 
2653 	case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
2654 
2655 		isci_request_set_open_reject_status(
2656 			request, task, response_ptr, status_ptr,
2657 			SAS_OREJ_STP_NORES);
2658 		break;
2659 
2660 	case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
2661 
2662 		isci_request_set_open_reject_status(
2663 			request, task, response_ptr, status_ptr,
2664 			SAS_OREJ_EPROTO);
2665 		break;
2666 
2667 	case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
2668 
2669 		isci_request_set_open_reject_status(
2670 			request, task, response_ptr, status_ptr,
2671 			SAS_OREJ_CONN_RATE);
2672 		break;
2673 
2674 	case SCU_TASK_DONE_LL_R_ERR:
2675 	/* Also SCU_TASK_DONE_ACK_NAK_TO: */
2676 	case SCU_TASK_DONE_LL_PERR:
2677 	case SCU_TASK_DONE_LL_SY_TERM:
2678 	/* Also SCU_TASK_DONE_NAK_ERR:*/
2679 	case SCU_TASK_DONE_LL_LF_TERM:
2680 	/* Also SCU_TASK_DONE_DATA_LEN_ERR: */
2681 	case SCU_TASK_DONE_LL_ABORT_ERR:
2682 	case SCU_TASK_DONE_SEQ_INV_TYPE:
2683 	/* Also SCU_TASK_DONE_UNEXP_XR: */
2684 	case SCU_TASK_DONE_XR_IU_LEN_ERR:
2685 	case SCU_TASK_DONE_INV_FIS_LEN:
2686 	/* Also SCU_TASK_DONE_XR_WD_LEN: */
2687 	case SCU_TASK_DONE_SDMA_ERR:
2688 	case SCU_TASK_DONE_OFFSET_ERR:
2689 	case SCU_TASK_DONE_MAX_PLD_ERR:
2690 	case SCU_TASK_DONE_LF_ERR:
2691 	case SCU_TASK_DONE_SMP_RESP_TO_ERR:  /* Escalate to dev reset? */
2692 	case SCU_TASK_DONE_SMP_LL_RX_ERR:
2693 	case SCU_TASK_DONE_UNEXP_DATA:
2694 	case SCU_TASK_DONE_UNEXP_SDBFIS:
2695 	case SCU_TASK_DONE_REG_ERR:
2696 	case SCU_TASK_DONE_SDB_ERR:
2697 	case SCU_TASK_DONE_TASK_ABORT:
2698 	default:
2699 		/* Task in the target is not done. */
2700 		*response_ptr = SAS_TASK_UNDELIVERED;
2701 		*status_ptr = SAM_STAT_TASK_ABORTED;
2702 
2703 		if (task->task_proto == SAS_PROTOCOL_SMP)
2704 			set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2705 		else
2706 			clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2707 		break;
2708 	}
2709 }
2710 
2711 static void isci_process_stp_response(struct sas_task *task, struct dev_to_host_fis *fis)
2712 {
2713 	struct task_status_struct *ts = &task->task_status;
2714 	struct ata_task_resp *resp = (void *)&ts->buf[0];
2715 
2716 	resp->frame_len = sizeof(*fis);
2717 	memcpy(resp->ending_fis, fis, sizeof(*fis));
2718 	ts->buf_valid_size = sizeof(*resp);
2719 
2720 	/* If an error is flagged let libata decode the fis */
2721 	if (ac_err_mask(fis->status))
2722 		ts->stat = SAS_PROTO_RESPONSE;
2723 	else
2724 		ts->stat = SAM_STAT_GOOD;
2725 
2726 	ts->resp = SAS_TASK_COMPLETE;
2727 }
2728 
2729 static void isci_request_io_request_complete(struct isci_host *ihost,
2730 					     struct isci_request *request,
2731 					     enum sci_io_status completion_status)
2732 {
2733 	struct sas_task *task = isci_request_access_task(request);
2734 	struct ssp_response_iu *resp_iu;
2735 	unsigned long task_flags;
2736 	struct isci_remote_device *idev = request->target_device;
2737 	enum service_response response = SAS_TASK_UNDELIVERED;
2738 	enum exec_status status = SAS_ABORTED_TASK;
2739 
2740 	dev_dbg(&ihost->pdev->dev,
2741 		"%s: request = %p, task = %p, "
2742 		"task->data_dir = %d completion_status = 0x%x\n",
2743 		__func__, request, task, task->data_dir, completion_status);
2744 
2745 	/* The request is done from an SCU HW perspective. */
2746 
2747 	/* This is an active request being completed from the core. */
2748 	switch (completion_status) {
2749 
2750 	case SCI_IO_FAILURE_RESPONSE_VALID:
2751 		dev_dbg(&ihost->pdev->dev,
2752 			"%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
2753 			__func__, request, task);
2754 
2755 		if (sas_protocol_ata(task->task_proto)) {
2756 			isci_process_stp_response(task, &request->stp.rsp);
2757 		} else if (SAS_PROTOCOL_SSP == task->task_proto) {
2758 
2759 			/* crack the iu response buffer. */
2760 			resp_iu = &request->ssp.rsp;
2761 			isci_request_process_response_iu(task, resp_iu,
2762 							 &ihost->pdev->dev);
2763 
2764 		} else if (SAS_PROTOCOL_SMP == task->task_proto) {
2765 
2766 			dev_err(&ihost->pdev->dev,
2767 				"%s: SCI_IO_FAILURE_RESPONSE_VALID: "
2768 					"SAS_PROTOCOL_SMP protocol\n",
2769 				__func__);
2770 
2771 		} else
2772 			dev_err(&ihost->pdev->dev,
2773 				"%s: unknown protocol\n", __func__);
2774 
2775 		/* use the task status set in the task struct by the
2776 		* isci_request_process_response_iu call.
2777 		*/
2778 		set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2779 		response = task->task_status.resp;
2780 		status = task->task_status.stat;
2781 		break;
2782 
2783 	case SCI_IO_SUCCESS:
2784 	case SCI_IO_SUCCESS_IO_DONE_EARLY:
2785 
2786 		response = SAS_TASK_COMPLETE;
2787 		status   = SAM_STAT_GOOD;
2788 		set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2789 
2790 		if (completion_status == SCI_IO_SUCCESS_IO_DONE_EARLY) {
2791 
2792 			/* This was an SSP / STP / SATA transfer.
2793 			* There is a possibility that less data than
2794 			* the maximum was transferred.
2795 			*/
2796 			u32 transferred_length = sci_req_tx_bytes(request);
2797 
2798 			task->task_status.residual
2799 				= task->total_xfer_len - transferred_length;
2800 
2801 			/* If there were residual bytes, call this an
2802 			* underrun.
2803 			*/
2804 			if (task->task_status.residual != 0)
2805 				status = SAS_DATA_UNDERRUN;
2806 
2807 			dev_dbg(&ihost->pdev->dev,
2808 				"%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
2809 				__func__, status);
2810 
2811 		} else
2812 			dev_dbg(&ihost->pdev->dev, "%s: SCI_IO_SUCCESS\n",
2813 				__func__);
2814 		break;
2815 
2816 	case SCI_IO_FAILURE_TERMINATED:
2817 
2818 		dev_dbg(&ihost->pdev->dev,
2819 			"%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
2820 			__func__, request, task);
2821 
2822 		/* The request was terminated explicitly. */
2823 		set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2824 		response = SAS_TASK_UNDELIVERED;
2825 
2826 		/* See if the device has been/is being stopped. Note
2827 		* that we ignore the quiesce state, since we are
2828 		* concerned about the actual device state.
2829 		*/
2830 		if (!idev)
2831 			status = SAS_DEVICE_UNKNOWN;
2832 		else
2833 			status = SAS_ABORTED_TASK;
2834 		break;
2835 
2836 	case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
2837 
2838 		isci_request_handle_controller_specific_errors(idev, request,
2839 							       task, &response,
2840 							       &status);
2841 		break;
2842 
2843 	case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
2844 		/* This is a special case, in that the I/O completion
2845 		* is telling us that the device needs a reset.
2846 		* In order for the device reset condition to be
2847 		* noticed, the I/O has to be handled in the error
2848 		* handler.  Set the reset flag and cause the
2849 		* SCSI error thread to be scheduled.
2850 		*/
2851 		spin_lock_irqsave(&task->task_state_lock, task_flags);
2852 		task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
2853 		spin_unlock_irqrestore(&task->task_state_lock, task_flags);
2854 
2855 		/* Fail the I/O. */
2856 		response = SAS_TASK_UNDELIVERED;
2857 		status = SAM_STAT_TASK_ABORTED;
2858 
2859 		clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2860 		break;
2861 
2862 	case SCI_FAILURE_RETRY_REQUIRED:
2863 
2864 		/* Fail the I/O so it can be retried. */
2865 		response = SAS_TASK_UNDELIVERED;
2866 		if (!idev)
2867 			status = SAS_DEVICE_UNKNOWN;
2868 		else
2869 			status = SAS_ABORTED_TASK;
2870 
2871 		set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2872 		break;
2873 
2874 
2875 	default:
2876 		/* Catch any otherwise unhandled error codes here. */
2877 		dev_dbg(&ihost->pdev->dev,
2878 			"%s: invalid completion code: 0x%x - "
2879 				"isci_request = %p\n",
2880 			__func__, completion_status, request);
2881 
2882 		response = SAS_TASK_UNDELIVERED;
2883 
2884 		/* See if the device has been/is being stopped. Note
2885 		* that we ignore the quiesce state, since we are
2886 		* concerned about the actual device state.
2887 		*/
2888 		if (!idev)
2889 			status = SAS_DEVICE_UNKNOWN;
2890 		else
2891 			status = SAS_ABORTED_TASK;
2892 
2893 		if (SAS_PROTOCOL_SMP == task->task_proto)
2894 			set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2895 		else
2896 			clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2897 		break;
2898 	}
2899 
2900 	switch (task->task_proto) {
2901 	case SAS_PROTOCOL_SSP:
2902 		if (task->data_dir == DMA_NONE)
2903 			break;
2904 		if (task->num_scatter == 0)
2905 			/* 0 indicates a single dma address */
2906 			dma_unmap_single(&ihost->pdev->dev,
2907 					 request->zero_scatter_daddr,
2908 					 task->total_xfer_len, task->data_dir);
2909 		else  /* unmap the sgl dma addresses */
2910 			dma_unmap_sg(&ihost->pdev->dev, task->scatter,
2911 				     request->num_sg_entries, task->data_dir);
2912 		break;
2913 	case SAS_PROTOCOL_SMP: {
2914 		struct scatterlist *sg = &task->smp_task.smp_req;
2915 		struct smp_req *smp_req;
2916 		void *kaddr;
2917 
2918 		dma_unmap_sg(&ihost->pdev->dev, sg, 1, DMA_TO_DEVICE);
2919 
2920 		/* need to swab it back in case the command buffer is re-used */
2921 		kaddr = kmap_atomic(sg_page(sg));
2922 		smp_req = kaddr + sg->offset;
2923 		sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
2924 		kunmap_atomic(kaddr);
2925 		break;
2926 	}
2927 	default:
2928 		break;
2929 	}
2930 
2931 	spin_lock_irqsave(&task->task_state_lock, task_flags);
2932 
2933 	task->task_status.resp = response;
2934 	task->task_status.stat = status;
2935 
2936 	if (test_bit(IREQ_COMPLETE_IN_TARGET, &request->flags)) {
2937 		/* Normal notification (task_done) */
2938 		task->task_state_flags |= SAS_TASK_STATE_DONE;
2939 		task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
2940 					    SAS_TASK_STATE_PENDING);
2941 	}
2942 	spin_unlock_irqrestore(&task->task_state_lock, task_flags);
2943 
2944 	/* complete the io request to the core. */
2945 	sci_controller_complete_io(ihost, request->target_device, request);
2946 
2947 	/* set terminated handle so it cannot be completed or
2948 	 * terminated again, and to cause any calls into abort
2949 	 * task to recognize the already completed case.
2950 	 */
2951 	set_bit(IREQ_TERMINATED, &request->flags);
2952 
2953 	ireq_done(ihost, request, task);
2954 }
2955 
2956 static void sci_request_started_state_enter(struct sci_base_state_machine *sm)
2957 {
2958 	struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
2959 	struct domain_device *dev = ireq->target_device->domain_dev;
2960 	enum sci_base_request_states state;
2961 	struct sas_task *task;
2962 
2963 	/* XXX as hch said always creating an internal sas_task for tmf
2964 	 * requests would simplify the driver
2965 	 */
2966 	task = (test_bit(IREQ_TMF, &ireq->flags)) ? NULL : isci_request_access_task(ireq);
2967 
2968 	/* all unaccelerated request types (non ssp or ncq) handled with
2969 	 * substates
2970 	 */
2971 	if (!task && dev->dev_type == SAS_END_DEVICE) {
2972 		state = SCI_REQ_TASK_WAIT_TC_COMP;
2973 	} else if (task && task->task_proto == SAS_PROTOCOL_SMP) {
2974 		state = SCI_REQ_SMP_WAIT_RESP;
2975 	} else if (task && sas_protocol_ata(task->task_proto) &&
2976 		   !task->ata_task.use_ncq) {
2977 		if (dev->sata_dev.class == ATA_DEV_ATAPI &&
2978 			task->ata_task.fis.command == ATA_CMD_PACKET) {
2979 			state = SCI_REQ_ATAPI_WAIT_H2D;
2980 		} else if (task->data_dir == DMA_NONE) {
2981 			state = SCI_REQ_STP_NON_DATA_WAIT_H2D;
2982 		} else if (task->ata_task.dma_xfer) {
2983 			state = SCI_REQ_STP_UDMA_WAIT_TC_COMP;
2984 		} else /* PIO */ {
2985 			state = SCI_REQ_STP_PIO_WAIT_H2D;
2986 		}
2987 	} else {
2988 		/* SSP or NCQ are fully accelerated, no substates */
2989 		return;
2990 	}
2991 	sci_change_state(sm, state);
2992 }
2993 
2994 static void sci_request_completed_state_enter(struct sci_base_state_machine *sm)
2995 {
2996 	struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
2997 	struct isci_host *ihost = ireq->owning_controller;
2998 
2999 	/* Tell the SCI_USER that the IO request is complete */
3000 	if (!test_bit(IREQ_TMF, &ireq->flags))
3001 		isci_request_io_request_complete(ihost, ireq,
3002 						 ireq->sci_status);
3003 	else
3004 		isci_task_request_complete(ihost, ireq, ireq->sci_status);
3005 }
3006 
3007 static void sci_request_aborting_state_enter(struct sci_base_state_machine *sm)
3008 {
3009 	struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
3010 
3011 	/* Setting the abort bit in the Task Context is required by the silicon. */
3012 	ireq->tc->abort = 1;
3013 }
3014 
3015 static void sci_stp_request_started_non_data_await_h2d_completion_enter(struct sci_base_state_machine *sm)
3016 {
3017 	struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
3018 
3019 	ireq->target_device->working_request = ireq;
3020 }
3021 
3022 static void sci_stp_request_started_pio_await_h2d_completion_enter(struct sci_base_state_machine *sm)
3023 {
3024 	struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
3025 
3026 	ireq->target_device->working_request = ireq;
3027 }
3028 
3029 static const struct sci_base_state sci_request_state_table[] = {
3030 	[SCI_REQ_INIT] = { },
3031 	[SCI_REQ_CONSTRUCTED] = { },
3032 	[SCI_REQ_STARTED] = {
3033 		.enter_state = sci_request_started_state_enter,
3034 	},
3035 	[SCI_REQ_STP_NON_DATA_WAIT_H2D] = {
3036 		.enter_state = sci_stp_request_started_non_data_await_h2d_completion_enter,
3037 	},
3038 	[SCI_REQ_STP_NON_DATA_WAIT_D2H] = { },
3039 	[SCI_REQ_STP_PIO_WAIT_H2D] = {
3040 		.enter_state = sci_stp_request_started_pio_await_h2d_completion_enter,
3041 	},
3042 	[SCI_REQ_STP_PIO_WAIT_FRAME] = { },
3043 	[SCI_REQ_STP_PIO_DATA_IN] = { },
3044 	[SCI_REQ_STP_PIO_DATA_OUT] = { },
3045 	[SCI_REQ_STP_UDMA_WAIT_TC_COMP] = { },
3046 	[SCI_REQ_STP_UDMA_WAIT_D2H] = { },
3047 	[SCI_REQ_TASK_WAIT_TC_COMP] = { },
3048 	[SCI_REQ_TASK_WAIT_TC_RESP] = { },
3049 	[SCI_REQ_SMP_WAIT_RESP] = { },
3050 	[SCI_REQ_SMP_WAIT_TC_COMP] = { },
3051 	[SCI_REQ_ATAPI_WAIT_H2D] = { },
3052 	[SCI_REQ_ATAPI_WAIT_PIO_SETUP] = { },
3053 	[SCI_REQ_ATAPI_WAIT_D2H] = { },
3054 	[SCI_REQ_ATAPI_WAIT_TC_COMP] = { },
3055 	[SCI_REQ_COMPLETED] = {
3056 		.enter_state = sci_request_completed_state_enter,
3057 	},
3058 	[SCI_REQ_ABORTING] = {
3059 		.enter_state = sci_request_aborting_state_enter,
3060 	},
3061 	[SCI_REQ_FINAL] = { },
3062 };
3063 
3064 static void
3065 sci_general_request_construct(struct isci_host *ihost,
3066 				   struct isci_remote_device *idev,
3067 				   struct isci_request *ireq)
3068 {
3069 	sci_init_sm(&ireq->sm, sci_request_state_table, SCI_REQ_INIT);
3070 
3071 	ireq->target_device = idev;
3072 	ireq->protocol = SAS_PROTOCOL_NONE;
3073 	ireq->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
3074 
3075 	ireq->sci_status   = SCI_SUCCESS;
3076 	ireq->scu_status   = 0;
3077 	ireq->post_context = 0xFFFFFFFF;
3078 }
3079 
3080 static enum sci_status
3081 sci_io_request_construct(struct isci_host *ihost,
3082 			  struct isci_remote_device *idev,
3083 			  struct isci_request *ireq)
3084 {
3085 	struct domain_device *dev = idev->domain_dev;
3086 	enum sci_status status = SCI_SUCCESS;
3087 
3088 	/* Build the common part of the request */
3089 	sci_general_request_construct(ihost, idev, ireq);
3090 
3091 	if (idev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
3092 		return SCI_FAILURE_INVALID_REMOTE_DEVICE;
3093 
3094 	if (dev->dev_type == SAS_END_DEVICE)
3095 		/* pass */;
3096 	else if (dev_is_sata(dev))
3097 		memset(&ireq->stp.cmd, 0, sizeof(ireq->stp.cmd));
3098 	else if (dev_is_expander(dev->dev_type))
3099 		/* pass */;
3100 	else
3101 		return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
3102 
3103 	memset(ireq->tc, 0, offsetof(struct scu_task_context, sgl_pair_ab));
3104 
3105 	return status;
3106 }
3107 
3108 enum sci_status sci_task_request_construct(struct isci_host *ihost,
3109 					    struct isci_remote_device *idev,
3110 					    u16 io_tag, struct isci_request *ireq)
3111 {
3112 	struct domain_device *dev = idev->domain_dev;
3113 	enum sci_status status = SCI_SUCCESS;
3114 
3115 	/* Build the common part of the request */
3116 	sci_general_request_construct(ihost, idev, ireq);
3117 
3118 	if (dev->dev_type == SAS_END_DEVICE || dev_is_sata(dev)) {
3119 		set_bit(IREQ_TMF, &ireq->flags);
3120 		memset(ireq->tc, 0, sizeof(struct scu_task_context));
3121 
3122 		/* Set the protocol indicator. */
3123 		if (dev_is_sata(dev))
3124 			ireq->protocol = SAS_PROTOCOL_STP;
3125 		else
3126 			ireq->protocol = SAS_PROTOCOL_SSP;
3127 	} else
3128 		status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
3129 
3130 	return status;
3131 }
3132 
3133 static enum sci_status isci_request_ssp_request_construct(
3134 	struct isci_request *request)
3135 {
3136 	enum sci_status status;
3137 
3138 	dev_dbg(&request->isci_host->pdev->dev,
3139 		"%s: request = %p\n",
3140 		__func__,
3141 		request);
3142 	status = sci_io_request_construct_basic_ssp(request);
3143 	return status;
3144 }
3145 
3146 static enum sci_status isci_request_stp_request_construct(struct isci_request *ireq)
3147 {
3148 	struct sas_task *task = isci_request_access_task(ireq);
3149 	struct host_to_dev_fis *fis = &ireq->stp.cmd;
3150 	struct ata_queued_cmd *qc = task->uldd_task;
3151 	enum sci_status status;
3152 
3153 	dev_dbg(&ireq->isci_host->pdev->dev,
3154 		"%s: ireq = %p\n",
3155 		__func__,
3156 		ireq);
3157 
3158 	memcpy(fis, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
3159 	if (!task->ata_task.device_control_reg_update)
3160 		fis->flags |= 0x80;
3161 	fis->flags &= 0xF0;
3162 
3163 	status = sci_io_request_construct_basic_sata(ireq);
3164 
3165 	if (qc && (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
3166 		   qc->tf.command == ATA_CMD_FPDMA_READ ||
3167 		   qc->tf.command == ATA_CMD_FPDMA_RECV ||
3168 		   qc->tf.command == ATA_CMD_FPDMA_SEND ||
3169 		   qc->tf.command == ATA_CMD_NCQ_NON_DATA)) {
3170 		fis->sector_count = qc->tag << 3;
3171 		ireq->tc->type.stp.ncq_tag = qc->tag;
3172 	}
3173 
3174 	return status;
3175 }
3176 
3177 static enum sci_status
3178 sci_io_request_construct_smp(struct device *dev,
3179 			      struct isci_request *ireq,
3180 			      struct sas_task *task)
3181 {
3182 	struct scatterlist *sg = &task->smp_task.smp_req;
3183 	struct isci_remote_device *idev;
3184 	struct scu_task_context *task_context;
3185 	struct isci_port *iport;
3186 	struct smp_req *smp_req;
3187 	void *kaddr;
3188 	u8 req_len;
3189 	u32 cmd;
3190 
3191 	kaddr = kmap_atomic(sg_page(sg));
3192 	smp_req = kaddr + sg->offset;
3193 	/*
3194 	 * Look at the SMP requests' header fields; for certain SAS 1.x SMP
3195 	 * functions under SAS 2.0, a zero request length really indicates
3196 	 * a non-zero default length.
3197 	 */
3198 	if (smp_req->req_len == 0) {
3199 		switch (smp_req->func) {
3200 		case SMP_DISCOVER:
3201 		case SMP_REPORT_PHY_ERR_LOG:
3202 		case SMP_REPORT_PHY_SATA:
3203 		case SMP_REPORT_ROUTE_INFO:
3204 			smp_req->req_len = 2;
3205 			break;
3206 		case SMP_CONF_ROUTE_INFO:
3207 		case SMP_PHY_CONTROL:
3208 		case SMP_PHY_TEST_FUNCTION:
3209 			smp_req->req_len = 9;
3210 			break;
3211 			/* Default - zero is a valid default for 2.0. */
3212 		}
3213 	}
3214 	req_len = smp_req->req_len;
3215 	sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
3216 	cmd = *(u32 *) smp_req;
3217 	kunmap_atomic(kaddr);
3218 
3219 	if (!dma_map_sg(dev, sg, 1, DMA_TO_DEVICE))
3220 		return SCI_FAILURE;
3221 
3222 	ireq->protocol = SAS_PROTOCOL_SMP;
3223 
3224 	/* byte swap the smp request. */
3225 
3226 	task_context = ireq->tc;
3227 
3228 	idev = ireq->target_device;
3229 	iport = idev->owning_port;
3230 
3231 	/*
3232 	 * Fill in the TC with its required data
3233 	 * 00h
3234 	 */
3235 	task_context->priority = 0;
3236 	task_context->initiator_request = 1;
3237 	task_context->connection_rate = idev->connection_rate;
3238 	task_context->protocol_engine_index = ISCI_PEG;
3239 	task_context->logical_port_index = iport->physical_port_index;
3240 	task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
3241 	task_context->abort = 0;
3242 	task_context->valid = SCU_TASK_CONTEXT_VALID;
3243 	task_context->context_type = SCU_TASK_CONTEXT_TYPE;
3244 
3245 	/* 04h */
3246 	task_context->remote_node_index = idev->rnc.remote_node_index;
3247 	task_context->command_code = 0;
3248 	task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;
3249 
3250 	/* 08h */
3251 	task_context->link_layer_control = 0;
3252 	task_context->do_not_dma_ssp_good_response = 1;
3253 	task_context->strict_ordering = 0;
3254 	task_context->control_frame = 1;
3255 	task_context->timeout_enable = 0;
3256 	task_context->block_guard_enable = 0;
3257 
3258 	/* 0ch */
3259 	task_context->address_modifier = 0;
3260 
3261 	/* 10h */
3262 	task_context->ssp_command_iu_length = req_len;
3263 
3264 	/* 14h */
3265 	task_context->transfer_length_bytes = 0;
3266 
3267 	/*
3268 	 * 18h ~ 30h, protocol specific
3269 	 * since commandIU has been build by framework at this point, we just
3270 	 * copy the frist DWord from command IU to this location. */
3271 	memcpy(&task_context->type.smp, &cmd, sizeof(u32));
3272 
3273 	/*
3274 	 * 40h
3275 	 * "For SMP you could program it to zero. We would prefer that way
3276 	 * so that done code will be consistent." - Venki
3277 	 */
3278 	task_context->task_phase = 0;
3279 
3280 	ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
3281 			      (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
3282 			       (iport->physical_port_index <<
3283 				SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
3284 			      ISCI_TAG_TCI(ireq->io_tag));
3285 	/*
3286 	 * Copy the physical address for the command buffer to the SCU Task
3287 	 * Context command buffer should not contain command header.
3288 	 */
3289 	task_context->command_iu_upper = upper_32_bits(sg_dma_address(sg));
3290 	task_context->command_iu_lower = lower_32_bits(sg_dma_address(sg) + sizeof(u32));
3291 
3292 	/* SMP response comes as UF, so no need to set response IU address. */
3293 	task_context->response_iu_upper = 0;
3294 	task_context->response_iu_lower = 0;
3295 
3296 	sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
3297 
3298 	return SCI_SUCCESS;
3299 }
3300 
3301 /*
3302  * isci_smp_request_build() - This function builds the smp request.
3303  * @ireq: This parameter points to the isci_request allocated in the
3304  *    request construct function.
3305  *
3306  * SCI_SUCCESS on successfull completion, or specific failure code.
3307  */
3308 static enum sci_status isci_smp_request_build(struct isci_request *ireq)
3309 {
3310 	struct sas_task *task = isci_request_access_task(ireq);
3311 	struct device *dev = &ireq->isci_host->pdev->dev;
3312 	enum sci_status status = SCI_FAILURE;
3313 
3314 	status = sci_io_request_construct_smp(dev, ireq, task);
3315 	if (status != SCI_SUCCESS)
3316 		dev_dbg(&ireq->isci_host->pdev->dev,
3317 			 "%s: failed with status = %d\n",
3318 			 __func__,
3319 			 status);
3320 
3321 	return status;
3322 }
3323 
3324 /**
3325  * isci_io_request_build() - This function builds the io request object.
3326  * @ihost: This parameter specifies the ISCI host object
3327  * @request: This parameter points to the isci_request object allocated in the
3328  *    request construct function.
3329  * @sci_device: This parameter is the handle for the sci core's remote device
3330  *    object that is the destination for this request.
3331  *
3332  * SCI_SUCCESS on successfull completion, or specific failure code.
3333  */
3334 static enum sci_status isci_io_request_build(struct isci_host *ihost,
3335 					     struct isci_request *request,
3336 					     struct isci_remote_device *idev)
3337 {
3338 	enum sci_status status = SCI_SUCCESS;
3339 	struct sas_task *task = isci_request_access_task(request);
3340 
3341 	dev_dbg(&ihost->pdev->dev,
3342 		"%s: idev = 0x%p; request = %p, "
3343 		"num_scatter = %d\n",
3344 		__func__,
3345 		idev,
3346 		request,
3347 		task->num_scatter);
3348 
3349 	/* map the sgl addresses, if present.
3350 	 * libata does the mapping for sata devices
3351 	 * before we get the request.
3352 	 */
3353 	if (task->num_scatter &&
3354 	    !sas_protocol_ata(task->task_proto) &&
3355 	    !(SAS_PROTOCOL_SMP & task->task_proto)) {
3356 
3357 		request->num_sg_entries = dma_map_sg(
3358 			&ihost->pdev->dev,
3359 			task->scatter,
3360 			task->num_scatter,
3361 			task->data_dir
3362 			);
3363 
3364 		if (request->num_sg_entries == 0)
3365 			return SCI_FAILURE_INSUFFICIENT_RESOURCES;
3366 	}
3367 
3368 	status = sci_io_request_construct(ihost, idev, request);
3369 
3370 	if (status != SCI_SUCCESS) {
3371 		dev_dbg(&ihost->pdev->dev,
3372 			 "%s: failed request construct\n",
3373 			 __func__);
3374 		return SCI_FAILURE;
3375 	}
3376 
3377 	switch (task->task_proto) {
3378 	case SAS_PROTOCOL_SMP:
3379 		status = isci_smp_request_build(request);
3380 		break;
3381 	case SAS_PROTOCOL_SSP:
3382 		status = isci_request_ssp_request_construct(request);
3383 		break;
3384 	case SAS_PROTOCOL_SATA:
3385 	case SAS_PROTOCOL_STP:
3386 	case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
3387 		status = isci_request_stp_request_construct(request);
3388 		break;
3389 	default:
3390 		dev_dbg(&ihost->pdev->dev,
3391 			 "%s: unknown protocol\n", __func__);
3392 		return SCI_FAILURE;
3393 	}
3394 
3395 	return SCI_SUCCESS;
3396 }
3397 
3398 static struct isci_request *isci_request_from_tag(struct isci_host *ihost, u16 tag)
3399 {
3400 	struct isci_request *ireq;
3401 
3402 	ireq = ihost->reqs[ISCI_TAG_TCI(tag)];
3403 	ireq->io_tag = tag;
3404 	ireq->io_request_completion = NULL;
3405 	ireq->flags = 0;
3406 	ireq->num_sg_entries = 0;
3407 
3408 	return ireq;
3409 }
3410 
3411 static struct isci_request *isci_io_request_from_tag(struct isci_host *ihost,
3412 						     struct sas_task *task,
3413 						     u16 tag)
3414 {
3415 	struct isci_request *ireq;
3416 
3417 	ireq = isci_request_from_tag(ihost, tag);
3418 	ireq->ttype_ptr.io_task_ptr = task;
3419 	clear_bit(IREQ_TMF, &ireq->flags);
3420 	task->lldd_task = ireq;
3421 
3422 	return ireq;
3423 }
3424 
3425 struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
3426 					       struct isci_tmf *isci_tmf,
3427 					       u16 tag)
3428 {
3429 	struct isci_request *ireq;
3430 
3431 	ireq = isci_request_from_tag(ihost, tag);
3432 	ireq->ttype_ptr.tmf_task_ptr = isci_tmf;
3433 	set_bit(IREQ_TMF, &ireq->flags);
3434 
3435 	return ireq;
3436 }
3437 
3438 int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
3439 			 struct sas_task *task, u16 tag)
3440 {
3441 	enum sci_status status;
3442 	struct isci_request *ireq;
3443 	unsigned long flags;
3444 	int ret = 0;
3445 
3446 	/* do common allocation and init of request object. */
3447 	ireq = isci_io_request_from_tag(ihost, task, tag);
3448 
3449 	status = isci_io_request_build(ihost, ireq, idev);
3450 	if (status != SCI_SUCCESS) {
3451 		dev_dbg(&ihost->pdev->dev,
3452 			 "%s: request_construct failed - status = 0x%x\n",
3453 			 __func__,
3454 			 status);
3455 		return status;
3456 	}
3457 
3458 	spin_lock_irqsave(&ihost->scic_lock, flags);
3459 
3460 	if (test_bit(IDEV_IO_NCQERROR, &idev->flags)) {
3461 
3462 		if (isci_task_is_ncq_recovery(task)) {
3463 
3464 			/* The device is in an NCQ recovery state.  Issue the
3465 			 * request on the task side.  Note that it will
3466 			 * complete on the I/O request side because the
3467 			 * request was built that way (ie.
3468 			 * ireq->is_task_management_request is false).
3469 			 */
3470 			status = sci_controller_start_task(ihost,
3471 							    idev,
3472 							    ireq);
3473 		} else {
3474 			status = SCI_FAILURE;
3475 		}
3476 	} else {
3477 		/* send the request, let the core assign the IO TAG.	*/
3478 		status = sci_controller_start_io(ihost, idev,
3479 						  ireq);
3480 	}
3481 
3482 	if (status != SCI_SUCCESS &&
3483 	    status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
3484 		dev_dbg(&ihost->pdev->dev,
3485 			 "%s: failed request start (0x%x)\n",
3486 			 __func__, status);
3487 		spin_unlock_irqrestore(&ihost->scic_lock, flags);
3488 		return status;
3489 	}
3490 	/* Either I/O started OK, or the core has signaled that
3491 	 * the device needs a target reset.
3492 	 */
3493 	if (status != SCI_SUCCESS) {
3494 		/* The request did not really start in the
3495 		 * hardware, so clear the request handle
3496 		 * here so no terminations will be done.
3497 		 */
3498 		set_bit(IREQ_TERMINATED, &ireq->flags);
3499 	}
3500 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
3501 
3502 	if (status ==
3503 	    SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
3504 		/* Signal libsas that we need the SCSI error
3505 		 * handler thread to work on this I/O and that
3506 		 * we want a device reset.
3507 		 */
3508 		spin_lock_irqsave(&task->task_state_lock, flags);
3509 		task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
3510 		spin_unlock_irqrestore(&task->task_state_lock, flags);
3511 
3512 		/* Cause this task to be scheduled in the SCSI error
3513 		 * handler thread.
3514 		 */
3515 		sas_task_abort(task);
3516 
3517 		/* Change the status, since we are holding
3518 		 * the I/O until it is managed by the SCSI
3519 		 * error handler.
3520 		 */
3521 		status = SCI_SUCCESS;
3522 	}
3523 
3524 	return ret;
3525 }
3526