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