isci: unify isci_request and scic_sds_request
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / isci / request.c
blobc544bc79ce17a752132d955451d987fc45fe3b80
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.
5 * GPL LICENSE SUMMARY
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
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.
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.
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.
24 * BSD LICENSE
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
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.
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.
56 #include "isci.h"
57 #include "task.h"
58 #include "request.h"
59 #include "sata.h"
60 #include "scu_completion_codes.h"
61 #include "scu_event_codes.h"
62 #include "sas.h"
64 static struct scu_sgl_element_pair *to_sgl_element_pair(struct isci_request *ireq,
65 int idx)
67 if (idx == 0)
68 return &ireq->tc->sgl_pair_ab;
69 else if (idx == 1)
70 return &ireq->tc->sgl_pair_cd;
71 else if (idx < 0)
72 return NULL;
73 else
74 return &ireq->sg_table[idx - 2];
77 static dma_addr_t to_sgl_element_pair_dma(struct scic_sds_controller *scic,
78 struct isci_request *ireq, u32 idx)
80 u32 offset;
82 if (idx == 0) {
83 offset = (void *) &ireq->tc->sgl_pair_ab -
84 (void *) &scic->task_context_table[0];
85 return scic->task_context_dma + offset;
86 } else if (idx == 1) {
87 offset = (void *) &ireq->tc->sgl_pair_cd -
88 (void *) &scic->task_context_table[0];
89 return scic->task_context_dma + offset;
92 return scic_io_request_get_dma_addr(ireq, &ireq->sg_table[idx - 2]);
95 static void init_sgl_element(struct scu_sgl_element *e, struct scatterlist *sg)
97 e->length = sg_dma_len(sg);
98 e->address_upper = upper_32_bits(sg_dma_address(sg));
99 e->address_lower = lower_32_bits(sg_dma_address(sg));
100 e->address_modifier = 0;
103 static void scic_sds_request_build_sgl(struct isci_request *ireq)
105 struct isci_host *isci_host = ireq->isci_host;
106 struct scic_sds_controller *scic = &isci_host->sci;
107 struct sas_task *task = isci_request_access_task(ireq);
108 struct scatterlist *sg = NULL;
109 dma_addr_t dma_addr;
110 u32 sg_idx = 0;
111 struct scu_sgl_element_pair *scu_sg = NULL;
112 struct scu_sgl_element_pair *prev_sg = NULL;
114 if (task->num_scatter > 0) {
115 sg = task->scatter;
117 while (sg) {
118 scu_sg = to_sgl_element_pair(ireq, sg_idx);
119 init_sgl_element(&scu_sg->A, sg);
120 sg = sg_next(sg);
121 if (sg) {
122 init_sgl_element(&scu_sg->B, sg);
123 sg = sg_next(sg);
124 } else
125 memset(&scu_sg->B, 0, sizeof(scu_sg->B));
127 if (prev_sg) {
128 dma_addr = to_sgl_element_pair_dma(scic,
129 ireq,
130 sg_idx);
132 prev_sg->next_pair_upper =
133 upper_32_bits(dma_addr);
134 prev_sg->next_pair_lower =
135 lower_32_bits(dma_addr);
138 prev_sg = scu_sg;
139 sg_idx++;
141 } else { /* handle when no sg */
142 scu_sg = to_sgl_element_pair(ireq, sg_idx);
144 dma_addr = dma_map_single(&isci_host->pdev->dev,
145 task->scatter,
146 task->total_xfer_len,
147 task->data_dir);
149 ireq->zero_scatter_daddr = dma_addr;
151 scu_sg->A.length = task->total_xfer_len;
152 scu_sg->A.address_upper = upper_32_bits(dma_addr);
153 scu_sg->A.address_lower = lower_32_bits(dma_addr);
156 if (scu_sg) {
157 scu_sg->next_pair_upper = 0;
158 scu_sg->next_pair_lower = 0;
162 static void scic_sds_io_request_build_ssp_command_iu(struct isci_request *ireq)
164 struct ssp_cmd_iu *cmd_iu;
165 struct sas_task *task = isci_request_access_task(ireq);
167 cmd_iu = &ireq->ssp.cmd;
169 memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
170 cmd_iu->add_cdb_len = 0;
171 cmd_iu->_r_a = 0;
172 cmd_iu->_r_b = 0;
173 cmd_iu->en_fburst = 0; /* unsupported */
174 cmd_iu->task_prio = task->ssp_task.task_prio;
175 cmd_iu->task_attr = task->ssp_task.task_attr;
176 cmd_iu->_r_c = 0;
178 sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cdb,
179 sizeof(task->ssp_task.cdb) / sizeof(u32));
182 static void scic_sds_task_request_build_ssp_task_iu(struct isci_request *ireq)
184 struct ssp_task_iu *task_iu;
185 struct sas_task *task = isci_request_access_task(ireq);
186 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
188 task_iu = &ireq->ssp.tmf;
190 memset(task_iu, 0, sizeof(struct ssp_task_iu));
192 memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
194 task_iu->task_func = isci_tmf->tmf_code;
195 task_iu->task_tag =
196 (ireq->ttype == tmf_task) ?
197 isci_tmf->io_tag :
198 SCI_CONTROLLER_INVALID_IO_TAG;
202 * This method is will fill in the SCU Task Context for any type of SSP request.
203 * @sci_req:
204 * @task_context:
207 static void scu_ssp_reqeust_construct_task_context(
208 struct isci_request *ireq,
209 struct scu_task_context *task_context)
211 dma_addr_t dma_addr;
212 struct scic_sds_remote_device *target_device;
213 struct scic_sds_port *target_port;
215 target_device = scic_sds_request_get_device(ireq);
216 target_port = scic_sds_request_get_port(ireq);
218 /* Fill in the TC with the its required data */
219 task_context->abort = 0;
220 task_context->priority = 0;
221 task_context->initiator_request = 1;
222 task_context->connection_rate = target_device->connection_rate;
223 task_context->protocol_engine_index =
224 scic_sds_controller_get_protocol_engine_group(controller);
225 task_context->logical_port_index =
226 scic_sds_port_get_index(target_port);
227 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
228 task_context->valid = SCU_TASK_CONTEXT_VALID;
229 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
231 task_context->remote_node_index =
232 scic_sds_remote_device_get_index(ireq->target_device);
233 task_context->command_code = 0;
235 task_context->link_layer_control = 0;
236 task_context->do_not_dma_ssp_good_response = 1;
237 task_context->strict_ordering = 0;
238 task_context->control_frame = 0;
239 task_context->timeout_enable = 0;
240 task_context->block_guard_enable = 0;
242 task_context->address_modifier = 0;
244 /* task_context->type.ssp.tag = ireq->io_tag; */
245 task_context->task_phase = 0x01;
247 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
248 (scic_sds_controller_get_protocol_engine_group(controller) <<
249 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
250 (scic_sds_port_get_index(target_port) <<
251 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
252 ISCI_TAG_TCI(ireq->io_tag));
255 * Copy the physical address for the command buffer to the
256 * SCU Task Context
258 dma_addr = scic_io_request_get_dma_addr(ireq, &ireq->ssp.cmd);
260 task_context->command_iu_upper = upper_32_bits(dma_addr);
261 task_context->command_iu_lower = lower_32_bits(dma_addr);
264 * Copy the physical address for the response buffer to the
265 * SCU Task Context
267 dma_addr = scic_io_request_get_dma_addr(ireq, &ireq->ssp.rsp);
269 task_context->response_iu_upper = upper_32_bits(dma_addr);
270 task_context->response_iu_lower = lower_32_bits(dma_addr);
274 * This method is will fill in the SCU Task Context for a SSP IO request.
275 * @sci_req:
278 static void scu_ssp_io_request_construct_task_context(struct isci_request *ireq,
279 enum dma_data_direction dir,
280 u32 len)
282 struct scu_task_context *task_context = ireq->tc;
284 scu_ssp_reqeust_construct_task_context(ireq, task_context);
286 task_context->ssp_command_iu_length =
287 sizeof(struct ssp_cmd_iu) / sizeof(u32);
288 task_context->type.ssp.frame_type = SSP_COMMAND;
290 switch (dir) {
291 case DMA_FROM_DEVICE:
292 case DMA_NONE:
293 default:
294 task_context->task_type = SCU_TASK_TYPE_IOREAD;
295 break;
296 case DMA_TO_DEVICE:
297 task_context->task_type = SCU_TASK_TYPE_IOWRITE;
298 break;
301 task_context->transfer_length_bytes = len;
303 if (task_context->transfer_length_bytes > 0)
304 scic_sds_request_build_sgl(ireq);
308 * This method will fill in the SCU Task Context for a SSP Task request. The
309 * following important settings are utilized: -# priority ==
310 * SCU_TASK_PRIORITY_HIGH. This ensures that the task request is issued
311 * ahead of other task destined for the same Remote Node. -# task_type ==
312 * SCU_TASK_TYPE_IOREAD. This simply indicates that a normal request type
313 * (i.e. non-raw frame) is being utilized to perform task management. -#
314 * control_frame == 1. This ensures that the proper endianess is set so
315 * that the bytes are transmitted in the right order for a task frame.
316 * @sci_req: This parameter specifies the task request object being
317 * constructed.
320 static void scu_ssp_task_request_construct_task_context(struct isci_request *ireq)
322 struct scu_task_context *task_context = ireq->tc;
324 scu_ssp_reqeust_construct_task_context(ireq, task_context);
326 task_context->control_frame = 1;
327 task_context->priority = SCU_TASK_PRIORITY_HIGH;
328 task_context->task_type = SCU_TASK_TYPE_RAW_FRAME;
329 task_context->transfer_length_bytes = 0;
330 task_context->type.ssp.frame_type = SSP_TASK;
331 task_context->ssp_command_iu_length =
332 sizeof(struct ssp_task_iu) / sizeof(u32);
336 * This method is will fill in the SCU Task Context for any type of SATA
337 * request. This is called from the various SATA constructors.
338 * @sci_req: The general IO request object which is to be used in
339 * constructing the SCU task context.
340 * @task_context: The buffer pointer for the SCU task context which is being
341 * constructed.
343 * The general io request construction is complete. The buffer assignment for
344 * the command buffer is complete. none Revisit task context construction to
345 * determine what is common for SSP/SMP/STP task context structures.
347 static void scu_sata_reqeust_construct_task_context(
348 struct isci_request *ireq,
349 struct scu_task_context *task_context)
351 dma_addr_t dma_addr;
352 struct scic_sds_remote_device *target_device;
353 struct scic_sds_port *target_port;
355 target_device = scic_sds_request_get_device(ireq);
356 target_port = scic_sds_request_get_port(ireq);
358 /* Fill in the TC with the its required data */
359 task_context->abort = 0;
360 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
361 task_context->initiator_request = 1;
362 task_context->connection_rate = target_device->connection_rate;
363 task_context->protocol_engine_index =
364 scic_sds_controller_get_protocol_engine_group(controller);
365 task_context->logical_port_index =
366 scic_sds_port_get_index(target_port);
367 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
368 task_context->valid = SCU_TASK_CONTEXT_VALID;
369 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
371 task_context->remote_node_index =
372 scic_sds_remote_device_get_index(ireq->target_device);
373 task_context->command_code = 0;
375 task_context->link_layer_control = 0;
376 task_context->do_not_dma_ssp_good_response = 1;
377 task_context->strict_ordering = 0;
378 task_context->control_frame = 0;
379 task_context->timeout_enable = 0;
380 task_context->block_guard_enable = 0;
382 task_context->address_modifier = 0;
383 task_context->task_phase = 0x01;
385 task_context->ssp_command_iu_length =
386 (sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);
388 /* Set the first word of the H2D REG FIS */
389 task_context->type.words[0] = *(u32 *)&ireq->stp.cmd;
391 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
392 (scic_sds_controller_get_protocol_engine_group(controller) <<
393 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
394 (scic_sds_port_get_index(target_port) <<
395 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
396 ISCI_TAG_TCI(ireq->io_tag));
398 * Copy the physical address for the command buffer to the SCU Task
399 * Context. We must offset the command buffer by 4 bytes because the
400 * first 4 bytes are transfered in the body of the TC.
402 dma_addr = scic_io_request_get_dma_addr(ireq,
403 ((char *) &ireq->stp.cmd) +
404 sizeof(u32));
406 task_context->command_iu_upper = upper_32_bits(dma_addr);
407 task_context->command_iu_lower = lower_32_bits(dma_addr);
409 /* SATA Requests do not have a response buffer */
410 task_context->response_iu_upper = 0;
411 task_context->response_iu_lower = 0;
414 static void scu_stp_raw_request_construct_task_context(struct isci_request *ireq)
416 struct scu_task_context *task_context = ireq->tc;
418 scu_sata_reqeust_construct_task_context(ireq, task_context);
420 task_context->control_frame = 0;
421 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
422 task_context->task_type = SCU_TASK_TYPE_SATA_RAW_FRAME;
423 task_context->type.stp.fis_type = FIS_REGH2D;
424 task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
427 static enum sci_status scic_sds_stp_pio_request_construct(struct isci_request *ireq,
428 bool copy_rx_frame)
430 struct isci_stp_request *stp_req = &ireq->stp.req;
432 scu_stp_raw_request_construct_task_context(ireq);
434 stp_req->status = 0;
435 stp_req->sgl.offset = 0;
436 stp_req->sgl.set = SCU_SGL_ELEMENT_PAIR_A;
438 if (copy_rx_frame) {
439 scic_sds_request_build_sgl(ireq);
440 stp_req->sgl.index = 0;
441 } else {
442 /* The user does not want the data copied to the SGL buffer location */
443 stp_req->sgl.index = -1;
446 return SCI_SUCCESS;
451 * @sci_req: This parameter specifies the request to be constructed as an
452 * optimized request.
453 * @optimized_task_type: This parameter specifies whether the request is to be
454 * an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
455 * value of 1 indicates NCQ.
457 * This method will perform request construction common to all types of STP
458 * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
459 * returns an indication as to whether the construction was successful.
461 static void scic_sds_stp_optimized_request_construct(struct isci_request *ireq,
462 u8 optimized_task_type,
463 u32 len,
464 enum dma_data_direction dir)
466 struct scu_task_context *task_context = ireq->tc;
468 /* Build the STP task context structure */
469 scu_sata_reqeust_construct_task_context(ireq, task_context);
471 /* Copy over the SGL elements */
472 scic_sds_request_build_sgl(ireq);
474 /* Copy over the number of bytes to be transfered */
475 task_context->transfer_length_bytes = len;
477 if (dir == DMA_TO_DEVICE) {
479 * The difference between the DMA IN and DMA OUT request task type
480 * values are consistent with the difference between FPDMA READ
481 * and FPDMA WRITE values. Add the supplied task type parameter
482 * to this difference to set the task type properly for this
483 * DATA OUT (WRITE) case. */
484 task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
485 - SCU_TASK_TYPE_DMA_IN);
486 } else {
488 * For the DATA IN (READ) case, simply save the supplied
489 * optimized task type. */
490 task_context->task_type = optimized_task_type;
496 static enum sci_status
497 scic_io_request_construct_sata(struct isci_request *ireq,
498 u32 len,
499 enum dma_data_direction dir,
500 bool copy)
502 enum sci_status status = SCI_SUCCESS;
503 struct sas_task *task = isci_request_access_task(ireq);
505 /* check for management protocols */
506 if (ireq->ttype == tmf_task) {
507 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
509 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
510 tmf->tmf_code == isci_tmf_sata_srst_low) {
511 scu_stp_raw_request_construct_task_context(ireq);
512 return SCI_SUCCESS;
513 } else {
514 dev_err(scic_to_dev(ireq->owning_controller),
515 "%s: Request 0x%p received un-handled SAT "
516 "management protocol 0x%x.\n",
517 __func__, ireq, tmf->tmf_code);
519 return SCI_FAILURE;
523 if (!sas_protocol_ata(task->task_proto)) {
524 dev_err(scic_to_dev(ireq->owning_controller),
525 "%s: Non-ATA protocol in SATA path: 0x%x\n",
526 __func__,
527 task->task_proto);
528 return SCI_FAILURE;
532 /* non data */
533 if (task->data_dir == DMA_NONE) {
534 scu_stp_raw_request_construct_task_context(ireq);
535 return SCI_SUCCESS;
538 /* NCQ */
539 if (task->ata_task.use_ncq) {
540 scic_sds_stp_optimized_request_construct(ireq,
541 SCU_TASK_TYPE_FPDMAQ_READ,
542 len, dir);
543 return SCI_SUCCESS;
546 /* DMA */
547 if (task->ata_task.dma_xfer) {
548 scic_sds_stp_optimized_request_construct(ireq,
549 SCU_TASK_TYPE_DMA_IN,
550 len, dir);
551 return SCI_SUCCESS;
552 } else /* PIO */
553 return scic_sds_stp_pio_request_construct(ireq, copy);
555 return status;
558 static enum sci_status scic_io_request_construct_basic_ssp(struct isci_request *ireq)
560 struct sas_task *task = isci_request_access_task(ireq);
562 ireq->protocol = SCIC_SSP_PROTOCOL;
564 scu_ssp_io_request_construct_task_context(ireq,
565 task->data_dir,
566 task->total_xfer_len);
568 scic_sds_io_request_build_ssp_command_iu(ireq);
570 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
572 return SCI_SUCCESS;
575 enum sci_status scic_task_request_construct_ssp(
576 struct isci_request *ireq)
578 /* Construct the SSP Task SCU Task Context */
579 scu_ssp_task_request_construct_task_context(ireq);
581 /* Fill in the SSP Task IU */
582 scic_sds_task_request_build_ssp_task_iu(ireq);
584 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
586 return SCI_SUCCESS;
589 static enum sci_status scic_io_request_construct_basic_sata(struct isci_request *ireq)
591 enum sci_status status;
592 bool copy = false;
593 struct sas_task *task = isci_request_access_task(ireq);
595 ireq->protocol = SCIC_STP_PROTOCOL;
597 copy = (task->data_dir == DMA_NONE) ? false : true;
599 status = scic_io_request_construct_sata(ireq,
600 task->total_xfer_len,
601 task->data_dir,
602 copy);
604 if (status == SCI_SUCCESS)
605 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
607 return status;
610 enum sci_status scic_task_request_construct_sata(struct isci_request *ireq)
612 enum sci_status status = SCI_SUCCESS;
614 /* check for management protocols */
615 if (ireq->ttype == tmf_task) {
616 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
618 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
619 tmf->tmf_code == isci_tmf_sata_srst_low) {
620 scu_stp_raw_request_construct_task_context(ireq);
621 } else {
622 dev_err(scic_to_dev(ireq->owning_controller),
623 "%s: Request 0x%p received un-handled SAT "
624 "Protocol 0x%x.\n",
625 __func__, ireq, tmf->tmf_code);
627 return SCI_FAILURE;
631 if (status != SCI_SUCCESS)
632 return status;
633 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
635 return status;
639 * sci_req_tx_bytes - bytes transferred when reply underruns request
640 * @sci_req: request that was terminated early
642 #define SCU_TASK_CONTEXT_SRAM 0x200000
643 static u32 sci_req_tx_bytes(struct isci_request *ireq)
645 struct scic_sds_controller *scic = ireq->owning_controller;
646 u32 ret_val = 0;
648 if (readl(&scic->smu_registers->address_modifier) == 0) {
649 void __iomem *scu_reg_base = scic->scu_registers;
651 /* get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
652 * BAR1 is the scu_registers
653 * 0x20002C = 0x200000 + 0x2c
654 * = start of task context SRAM + offset of (type.ssp.data_offset)
655 * TCi is the io_tag of struct scic_sds_request
657 ret_val = readl(scu_reg_base +
658 (SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
659 ((sizeof(struct scu_task_context)) * ISCI_TAG_TCI(ireq->io_tag)));
662 return ret_val;
665 enum sci_status scic_sds_request_start(struct isci_request *ireq)
667 enum sci_base_request_states state;
668 struct scu_task_context *tc = ireq->tc;
669 struct scic_sds_controller *scic = ireq->owning_controller;
671 state = ireq->sm.current_state_id;
672 if (state != SCI_REQ_CONSTRUCTED) {
673 dev_warn(scic_to_dev(scic),
674 "%s: SCIC IO Request requested to start while in wrong "
675 "state %d\n", __func__, state);
676 return SCI_FAILURE_INVALID_STATE;
679 tc->task_index = ISCI_TAG_TCI(ireq->io_tag);
681 switch (tc->protocol_type) {
682 case SCU_TASK_CONTEXT_PROTOCOL_SMP:
683 case SCU_TASK_CONTEXT_PROTOCOL_SSP:
684 /* SSP/SMP Frame */
685 tc->type.ssp.tag = ireq->io_tag;
686 tc->type.ssp.target_port_transfer_tag = 0xFFFF;
687 break;
689 case SCU_TASK_CONTEXT_PROTOCOL_STP:
690 /* STP/SATA Frame
691 * tc->type.stp.ncq_tag = ireq->ncq_tag;
693 break;
695 case SCU_TASK_CONTEXT_PROTOCOL_NONE:
696 /* / @todo When do we set no protocol type? */
697 break;
699 default:
700 /* This should never happen since we build the IO
701 * requests */
702 break;
705 /* Add to the post_context the io tag value */
706 ireq->post_context |= ISCI_TAG_TCI(ireq->io_tag);
708 /* Everything is good go ahead and change state */
709 sci_change_state(&ireq->sm, SCI_REQ_STARTED);
711 return SCI_SUCCESS;
714 enum sci_status
715 scic_sds_io_request_terminate(struct isci_request *ireq)
717 enum sci_base_request_states state;
719 state = ireq->sm.current_state_id;
721 switch (state) {
722 case SCI_REQ_CONSTRUCTED:
723 scic_sds_request_set_status(ireq,
724 SCU_TASK_DONE_TASK_ABORT,
725 SCI_FAILURE_IO_TERMINATED);
727 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
728 return SCI_SUCCESS;
729 case SCI_REQ_STARTED:
730 case SCI_REQ_TASK_WAIT_TC_COMP:
731 case SCI_REQ_SMP_WAIT_RESP:
732 case SCI_REQ_SMP_WAIT_TC_COMP:
733 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
734 case SCI_REQ_STP_UDMA_WAIT_D2H:
735 case SCI_REQ_STP_NON_DATA_WAIT_H2D:
736 case SCI_REQ_STP_NON_DATA_WAIT_D2H:
737 case SCI_REQ_STP_PIO_WAIT_H2D:
738 case SCI_REQ_STP_PIO_WAIT_FRAME:
739 case SCI_REQ_STP_PIO_DATA_IN:
740 case SCI_REQ_STP_PIO_DATA_OUT:
741 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
742 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
743 case SCI_REQ_STP_SOFT_RESET_WAIT_D2H:
744 sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
745 return SCI_SUCCESS;
746 case SCI_REQ_TASK_WAIT_TC_RESP:
747 sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
748 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
749 return SCI_SUCCESS;
750 case SCI_REQ_ABORTING:
751 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
752 return SCI_SUCCESS;
753 case SCI_REQ_COMPLETED:
754 default:
755 dev_warn(scic_to_dev(ireq->owning_controller),
756 "%s: SCIC IO Request requested to abort while in wrong "
757 "state %d\n",
758 __func__,
759 ireq->sm.current_state_id);
760 break;
763 return SCI_FAILURE_INVALID_STATE;
766 enum sci_status scic_sds_request_complete(struct isci_request *ireq)
768 enum sci_base_request_states state;
769 struct scic_sds_controller *scic = ireq->owning_controller;
771 state = ireq->sm.current_state_id;
772 if (WARN_ONCE(state != SCI_REQ_COMPLETED,
773 "isci: request completion from wrong state (%d)\n", state))
774 return SCI_FAILURE_INVALID_STATE;
776 if (ireq->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX)
777 scic_sds_controller_release_frame(scic,
778 ireq->saved_rx_frame_index);
780 /* XXX can we just stop the machine and remove the 'final' state? */
781 sci_change_state(&ireq->sm, SCI_REQ_FINAL);
782 return SCI_SUCCESS;
785 enum sci_status scic_sds_io_request_event_handler(struct isci_request *ireq,
786 u32 event_code)
788 enum sci_base_request_states state;
789 struct scic_sds_controller *scic = ireq->owning_controller;
791 state = ireq->sm.current_state_id;
793 if (state != SCI_REQ_STP_PIO_DATA_IN) {
794 dev_warn(scic_to_dev(scic), "%s: (%x) in wrong state %d\n",
795 __func__, event_code, state);
797 return SCI_FAILURE_INVALID_STATE;
800 switch (scu_get_event_specifier(event_code)) {
801 case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
802 /* We are waiting for data and the SCU has R_ERR the data frame.
803 * Go back to waiting for the D2H Register FIS
805 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
806 return SCI_SUCCESS;
807 default:
808 dev_err(scic_to_dev(scic),
809 "%s: pio request unexpected event %#x\n",
810 __func__, event_code);
812 /* TODO Should we fail the PIO request when we get an
813 * unexpected event?
815 return SCI_FAILURE;
820 * This function copies response data for requests returning response data
821 * instead of sense data.
822 * @sci_req: This parameter specifies the request object for which to copy
823 * the response data.
825 static void scic_sds_io_request_copy_response(struct isci_request *ireq)
827 void *resp_buf;
828 u32 len;
829 struct ssp_response_iu *ssp_response;
830 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
832 ssp_response = &ireq->ssp.rsp;
834 resp_buf = &isci_tmf->resp.resp_iu;
836 len = min_t(u32,
837 SSP_RESP_IU_MAX_SIZE,
838 be32_to_cpu(ssp_response->response_data_len));
840 memcpy(resp_buf, ssp_response->resp_data, len);
843 static enum sci_status
844 request_started_state_tc_event(struct isci_request *ireq,
845 u32 completion_code)
847 struct ssp_response_iu *resp_iu;
848 u8 datapres;
850 /* TODO: Any SDMA return code of other than 0 is bad decode 0x003C0000
851 * to determine SDMA status
853 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
854 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
855 scic_sds_request_set_status(ireq,
856 SCU_TASK_DONE_GOOD,
857 SCI_SUCCESS);
858 break;
859 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP): {
860 /* There are times when the SCU hardware will return an early
861 * response because the io request specified more data than is
862 * returned by the target device (mode pages, inquiry data,
863 * etc.). We must check the response stats to see if this is
864 * truly a failed request or a good request that just got
865 * completed early.
867 struct ssp_response_iu *resp = &ireq->ssp.rsp;
868 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
870 sci_swab32_cpy(&ireq->ssp.rsp,
871 &ireq->ssp.rsp,
872 word_cnt);
874 if (resp->status == 0) {
875 scic_sds_request_set_status(ireq,
876 SCU_TASK_DONE_GOOD,
877 SCI_SUCCESS_IO_DONE_EARLY);
878 } else {
879 scic_sds_request_set_status(ireq,
880 SCU_TASK_DONE_CHECK_RESPONSE,
881 SCI_FAILURE_IO_RESPONSE_VALID);
883 break;
885 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE): {
886 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
888 sci_swab32_cpy(&ireq->ssp.rsp,
889 &ireq->ssp.rsp,
890 word_cnt);
892 scic_sds_request_set_status(ireq,
893 SCU_TASK_DONE_CHECK_RESPONSE,
894 SCI_FAILURE_IO_RESPONSE_VALID);
895 break;
898 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
899 /* TODO With TASK_DONE_RESP_LEN_ERR is the response frame
900 * guaranteed to be received before this completion status is
901 * posted?
903 resp_iu = &ireq->ssp.rsp;
904 datapres = resp_iu->datapres;
906 if (datapres == 1 || datapres == 2) {
907 scic_sds_request_set_status(ireq,
908 SCU_TASK_DONE_CHECK_RESPONSE,
909 SCI_FAILURE_IO_RESPONSE_VALID);
910 } else
911 scic_sds_request_set_status(ireq,
912 SCU_TASK_DONE_GOOD,
913 SCI_SUCCESS);
914 break;
915 /* only stp device gets suspended. */
916 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
917 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
918 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
919 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
920 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
921 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
922 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
923 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
924 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
925 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
926 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
927 if (ireq->protocol == SCIC_STP_PROTOCOL) {
928 scic_sds_request_set_status(ireq,
929 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
930 SCU_COMPLETION_TL_STATUS_SHIFT,
931 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
932 } else {
933 scic_sds_request_set_status(ireq,
934 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
935 SCU_COMPLETION_TL_STATUS_SHIFT,
936 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
938 break;
940 /* both stp/ssp device gets suspended */
941 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
942 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
943 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
944 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
945 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
946 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
947 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
948 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
949 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
950 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
951 scic_sds_request_set_status(ireq,
952 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
953 SCU_COMPLETION_TL_STATUS_SHIFT,
954 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
955 break;
957 /* neither ssp nor stp gets suspended. */
958 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
959 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
960 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
961 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
962 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
963 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
964 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
965 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
966 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
967 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
968 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
969 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
970 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
971 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
972 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
973 default:
974 scic_sds_request_set_status(
975 ireq,
976 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
977 SCU_COMPLETION_TL_STATUS_SHIFT,
978 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
979 break;
983 * TODO: This is probably wrong for ACK/NAK timeout conditions
986 /* In all cases we will treat this as the completion of the IO req. */
987 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
988 return SCI_SUCCESS;
991 static enum sci_status
992 request_aborting_state_tc_event(struct isci_request *ireq,
993 u32 completion_code)
995 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
996 case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
997 case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
998 scic_sds_request_set_status(ireq, SCU_TASK_DONE_TASK_ABORT,
999 SCI_FAILURE_IO_TERMINATED);
1001 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1002 break;
1004 default:
1005 /* Unless we get some strange error wait for the task abort to complete
1006 * TODO: Should there be a state change for this completion?
1008 break;
1011 return SCI_SUCCESS;
1014 static enum sci_status ssp_task_request_await_tc_event(struct isci_request *ireq,
1015 u32 completion_code)
1017 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1018 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1019 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
1020 SCI_SUCCESS);
1022 sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
1023 break;
1024 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
1025 /* Currently, the decision is to simply allow the task request
1026 * to timeout if the task IU wasn't received successfully.
1027 * There is a potential for receiving multiple task responses if
1028 * we decide to send the task IU again.
1030 dev_warn(scic_to_dev(ireq->owning_controller),
1031 "%s: TaskRequest:0x%p CompletionCode:%x - "
1032 "ACK/NAK timeout\n", __func__, ireq,
1033 completion_code);
1035 sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
1036 break;
1037 default:
1039 * All other completion status cause the IO to be complete.
1040 * If a NAK was received, then it is up to the user to retry
1041 * the request.
1043 scic_sds_request_set_status(ireq,
1044 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1045 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1047 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1048 break;
1051 return SCI_SUCCESS;
1054 static enum sci_status
1055 smp_request_await_response_tc_event(struct isci_request *ireq,
1056 u32 completion_code)
1058 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1059 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1060 /* In the AWAIT RESPONSE state, any TC completion is
1061 * unexpected. but if the TC has success status, we
1062 * complete the IO anyway.
1064 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
1065 SCI_SUCCESS);
1067 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1068 break;
1070 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1071 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1072 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1073 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
1074 /* These status has been seen in a specific LSI
1075 * expander, which sometimes is not able to send smp
1076 * response within 2 ms. This causes our hardware break
1077 * the connection and set TC completion with one of
1078 * these SMP_XXX_XX_ERR status. For these type of error,
1079 * we ask scic user to retry the request.
1081 scic_sds_request_set_status(ireq, SCU_TASK_DONE_SMP_RESP_TO_ERR,
1082 SCI_FAILURE_RETRY_REQUIRED);
1084 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1085 break;
1087 default:
1088 /* All other completion status cause the IO to be complete. If a NAK
1089 * was received, then it is up to the user to retry the request
1091 scic_sds_request_set_status(ireq,
1092 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1093 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1095 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1096 break;
1099 return SCI_SUCCESS;
1102 static enum sci_status
1103 smp_request_await_tc_event(struct isci_request *ireq,
1104 u32 completion_code)
1106 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1107 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1108 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
1109 SCI_SUCCESS);
1111 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1112 break;
1113 default:
1114 /* All other completion status cause the IO to be
1115 * complete. If a NAK was received, then it is up to
1116 * the user to retry the request.
1118 scic_sds_request_set_status(ireq,
1119 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1120 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1122 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1123 break;
1126 return SCI_SUCCESS;
1129 void scic_stp_io_request_set_ncq_tag(struct isci_request *ireq,
1130 u16 ncq_tag)
1133 * @note This could be made to return an error to the user if the user
1134 * attempts to set the NCQ tag in the wrong state.
1136 ireq->tc->type.stp.ncq_tag = ncq_tag;
1139 static struct scu_sgl_element *pio_sgl_next(struct isci_stp_request *stp_req)
1141 struct scu_sgl_element *sgl;
1142 struct scu_sgl_element_pair *sgl_pair;
1143 struct isci_request *ireq = to_ireq(stp_req);
1144 struct isci_stp_pio_sgl *pio_sgl = &stp_req->sgl;
1146 sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
1147 if (!sgl_pair)
1148 sgl = NULL;
1149 else if (pio_sgl->set == SCU_SGL_ELEMENT_PAIR_A) {
1150 if (sgl_pair->B.address_lower == 0 &&
1151 sgl_pair->B.address_upper == 0) {
1152 sgl = NULL;
1153 } else {
1154 pio_sgl->set = SCU_SGL_ELEMENT_PAIR_B;
1155 sgl = &sgl_pair->B;
1157 } else {
1158 if (sgl_pair->next_pair_lower == 0 &&
1159 sgl_pair->next_pair_upper == 0) {
1160 sgl = NULL;
1161 } else {
1162 pio_sgl->index++;
1163 pio_sgl->set = SCU_SGL_ELEMENT_PAIR_A;
1164 sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
1165 sgl = &sgl_pair->A;
1169 return sgl;
1172 static enum sci_status
1173 stp_request_non_data_await_h2d_tc_event(struct isci_request *ireq,
1174 u32 completion_code)
1176 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1177 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1178 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
1179 SCI_SUCCESS);
1181 sci_change_state(&ireq->sm, SCI_REQ_STP_NON_DATA_WAIT_D2H);
1182 break;
1184 default:
1185 /* All other completion status cause the IO to be
1186 * complete. If a NAK was received, then it is up to
1187 * the user to retry the request.
1189 scic_sds_request_set_status(ireq,
1190 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1191 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1193 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1194 break;
1197 return SCI_SUCCESS;
1200 #define SCU_MAX_FRAME_BUFFER_SIZE 0x400 /* 1K is the maximum SCU frame data payload */
1202 /* transmit DATA_FIS from (current sgl + offset) for input
1203 * parameter length. current sgl and offset is alreay stored in the IO request
1205 static enum sci_status scic_sds_stp_request_pio_data_out_trasmit_data_frame(
1206 struct isci_request *ireq,
1207 u32 length)
1209 struct isci_stp_request *stp_req = &ireq->stp.req;
1210 struct scu_task_context *task_context = ireq->tc;
1211 struct scu_sgl_element_pair *sgl_pair;
1212 struct scu_sgl_element *current_sgl;
1214 /* Recycle the TC and reconstruct it for sending out DATA FIS containing
1215 * for the data from current_sgl+offset for the input length
1217 sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
1218 if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A)
1219 current_sgl = &sgl_pair->A;
1220 else
1221 current_sgl = &sgl_pair->B;
1223 /* update the TC */
1224 task_context->command_iu_upper = current_sgl->address_upper;
1225 task_context->command_iu_lower = current_sgl->address_lower;
1226 task_context->transfer_length_bytes = length;
1227 task_context->type.stp.fis_type = FIS_DATA;
1229 /* send the new TC out. */
1230 return scic_controller_continue_io(ireq);
1233 static enum sci_status scic_sds_stp_request_pio_data_out_transmit_data(struct isci_request *ireq)
1235 struct isci_stp_request *stp_req = &ireq->stp.req;
1236 struct scu_sgl_element_pair *sgl_pair;
1237 struct scu_sgl_element *sgl;
1238 enum sci_status status;
1239 u32 offset;
1240 u32 len = 0;
1242 offset = stp_req->sgl.offset;
1243 sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
1244 if (WARN_ONCE(!sgl_pair, "%s: null sgl element", __func__))
1245 return SCI_FAILURE;
1247 if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A) {
1248 sgl = &sgl_pair->A;
1249 len = sgl_pair->A.length - offset;
1250 } else {
1251 sgl = &sgl_pair->B;
1252 len = sgl_pair->B.length - offset;
1255 if (stp_req->pio_len == 0)
1256 return SCI_SUCCESS;
1258 if (stp_req->pio_len >= len) {
1259 status = scic_sds_stp_request_pio_data_out_trasmit_data_frame(ireq, len);
1260 if (status != SCI_SUCCESS)
1261 return status;
1262 stp_req->pio_len -= len;
1264 /* update the current sgl, offset and save for future */
1265 sgl = pio_sgl_next(stp_req);
1266 offset = 0;
1267 } else if (stp_req->pio_len < len) {
1268 scic_sds_stp_request_pio_data_out_trasmit_data_frame(ireq, stp_req->pio_len);
1270 /* Sgl offset will be adjusted and saved for future */
1271 offset += stp_req->pio_len;
1272 sgl->address_lower += stp_req->pio_len;
1273 stp_req->pio_len = 0;
1276 stp_req->sgl.offset = offset;
1278 return status;
1283 * @stp_request: The request that is used for the SGL processing.
1284 * @data_buffer: The buffer of data to be copied.
1285 * @length: The length of the data transfer.
1287 * Copy the data from the buffer for the length specified to the IO reqeust SGL
1288 * specified data region. enum sci_status
1290 static enum sci_status
1291 scic_sds_stp_request_pio_data_in_copy_data_buffer(struct isci_stp_request *stp_req,
1292 u8 *data_buf, u32 len)
1294 struct isci_request *ireq;
1295 u8 *src_addr;
1296 int copy_len;
1297 struct sas_task *task;
1298 struct scatterlist *sg;
1299 void *kaddr;
1300 int total_len = len;
1302 ireq = to_ireq(stp_req);
1303 task = isci_request_access_task(ireq);
1304 src_addr = data_buf;
1306 if (task->num_scatter > 0) {
1307 sg = task->scatter;
1309 while (total_len > 0) {
1310 struct page *page = sg_page(sg);
1312 copy_len = min_t(int, total_len, sg_dma_len(sg));
1313 kaddr = kmap_atomic(page, KM_IRQ0);
1314 memcpy(kaddr + sg->offset, src_addr, copy_len);
1315 kunmap_atomic(kaddr, KM_IRQ0);
1316 total_len -= copy_len;
1317 src_addr += copy_len;
1318 sg = sg_next(sg);
1320 } else {
1321 BUG_ON(task->total_xfer_len < total_len);
1322 memcpy(task->scatter, src_addr, total_len);
1325 return SCI_SUCCESS;
1330 * @sci_req: The PIO DATA IN request that is to receive the data.
1331 * @data_buffer: The buffer to copy from.
1333 * Copy the data buffer to the io request data region. enum sci_status
1335 static enum sci_status scic_sds_stp_request_pio_data_in_copy_data(
1336 struct isci_stp_request *stp_req,
1337 u8 *data_buffer)
1339 enum sci_status status;
1342 * If there is less than 1K remaining in the transfer request
1343 * copy just the data for the transfer */
1344 if (stp_req->pio_len < SCU_MAX_FRAME_BUFFER_SIZE) {
1345 status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
1346 stp_req, data_buffer, stp_req->pio_len);
1348 if (status == SCI_SUCCESS)
1349 stp_req->pio_len = 0;
1350 } else {
1351 /* We are transfering the whole frame so copy */
1352 status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
1353 stp_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
1355 if (status == SCI_SUCCESS)
1356 stp_req->pio_len -= SCU_MAX_FRAME_BUFFER_SIZE;
1359 return status;
1362 static enum sci_status
1363 stp_request_pio_await_h2d_completion_tc_event(struct isci_request *ireq,
1364 u32 completion_code)
1366 enum sci_status status = SCI_SUCCESS;
1368 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1369 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1370 scic_sds_request_set_status(ireq,
1371 SCU_TASK_DONE_GOOD,
1372 SCI_SUCCESS);
1374 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
1375 break;
1377 default:
1378 /* All other completion status cause the IO to be
1379 * complete. If a NAK was received, then it is up to
1380 * the user to retry the request.
1382 scic_sds_request_set_status(ireq,
1383 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1384 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1386 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1387 break;
1390 return status;
1393 static enum sci_status
1394 pio_data_out_tx_done_tc_event(struct isci_request *ireq,
1395 u32 completion_code)
1397 enum sci_status status = SCI_SUCCESS;
1398 bool all_frames_transferred = false;
1399 struct isci_stp_request *stp_req = &ireq->stp.req;
1401 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1402 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1403 /* Transmit data */
1404 if (stp_req->pio_len != 0) {
1405 status = scic_sds_stp_request_pio_data_out_transmit_data(ireq);
1406 if (status == SCI_SUCCESS) {
1407 if (stp_req->pio_len == 0)
1408 all_frames_transferred = true;
1410 } else if (stp_req->pio_len == 0) {
1412 * this will happen if the all data is written at the
1413 * first time after the pio setup fis is received
1415 all_frames_transferred = true;
1418 /* all data transferred. */
1419 if (all_frames_transferred) {
1421 * Change the state to SCI_REQ_STP_PIO_DATA_IN
1422 * and wait for PIO_SETUP fis / or D2H REg fis. */
1423 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
1425 break;
1427 default:
1429 * All other completion status cause the IO to be complete.
1430 * If a NAK was received, then it is up to the user to retry
1431 * the request.
1433 scic_sds_request_set_status(
1434 ireq,
1435 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1436 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1438 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1439 break;
1442 return status;
1445 static void scic_sds_stp_request_udma_complete_request(
1446 struct isci_request *ireq,
1447 u32 scu_status,
1448 enum sci_status sci_status)
1450 scic_sds_request_set_status(ireq, scu_status, sci_status);
1451 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1454 static enum sci_status scic_sds_stp_request_udma_general_frame_handler(struct isci_request *ireq,
1455 u32 frame_index)
1457 struct scic_sds_controller *scic = ireq->owning_controller;
1458 struct dev_to_host_fis *frame_header;
1459 enum sci_status status;
1460 u32 *frame_buffer;
1462 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1463 frame_index,
1464 (void **)&frame_header);
1466 if ((status == SCI_SUCCESS) &&
1467 (frame_header->fis_type == FIS_REGD2H)) {
1468 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1469 frame_index,
1470 (void **)&frame_buffer);
1472 scic_sds_controller_copy_sata_response(&ireq->stp.rsp,
1473 frame_header,
1474 frame_buffer);
1477 scic_sds_controller_release_frame(scic, frame_index);
1479 return status;
1482 enum sci_status
1483 scic_sds_io_request_frame_handler(struct isci_request *ireq,
1484 u32 frame_index)
1486 struct scic_sds_controller *scic = ireq->owning_controller;
1487 struct isci_stp_request *stp_req = &ireq->stp.req;
1488 enum sci_base_request_states state;
1489 enum sci_status status;
1490 ssize_t word_cnt;
1492 state = ireq->sm.current_state_id;
1493 switch (state) {
1494 case SCI_REQ_STARTED: {
1495 struct ssp_frame_hdr ssp_hdr;
1496 void *frame_header;
1498 scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1499 frame_index,
1500 &frame_header);
1502 word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
1503 sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);
1505 if (ssp_hdr.frame_type == SSP_RESPONSE) {
1506 struct ssp_response_iu *resp_iu;
1507 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1509 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1510 frame_index,
1511 (void **)&resp_iu);
1513 sci_swab32_cpy(&ireq->ssp.rsp, resp_iu, word_cnt);
1515 resp_iu = &ireq->ssp.rsp;
1517 if (resp_iu->datapres == 0x01 ||
1518 resp_iu->datapres == 0x02) {
1519 scic_sds_request_set_status(ireq,
1520 SCU_TASK_DONE_CHECK_RESPONSE,
1521 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1522 } else
1523 scic_sds_request_set_status(ireq,
1524 SCU_TASK_DONE_GOOD,
1525 SCI_SUCCESS);
1526 } else {
1527 /* not a response frame, why did it get forwarded? */
1528 dev_err(scic_to_dev(scic),
1529 "%s: SCIC IO Request 0x%p received unexpected "
1530 "frame %d type 0x%02x\n", __func__, ireq,
1531 frame_index, ssp_hdr.frame_type);
1535 * In any case we are done with this frame buffer return it to
1536 * the controller
1538 scic_sds_controller_release_frame(scic, frame_index);
1540 return SCI_SUCCESS;
1543 case SCI_REQ_TASK_WAIT_TC_RESP:
1544 scic_sds_io_request_copy_response(ireq);
1545 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1546 scic_sds_controller_release_frame(scic,frame_index);
1547 return SCI_SUCCESS;
1549 case SCI_REQ_SMP_WAIT_RESP: {
1550 struct smp_resp *rsp_hdr = &ireq->smp.rsp;
1551 void *frame_header;
1553 scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1554 frame_index,
1555 &frame_header);
1557 /* byte swap the header. */
1558 word_cnt = SMP_RESP_HDR_SZ / sizeof(u32);
1559 sci_swab32_cpy(rsp_hdr, frame_header, word_cnt);
1561 if (rsp_hdr->frame_type == SMP_RESPONSE) {
1562 void *smp_resp;
1564 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1565 frame_index,
1566 &smp_resp);
1568 word_cnt = (sizeof(struct smp_resp) - SMP_RESP_HDR_SZ) /
1569 sizeof(u32);
1571 sci_swab32_cpy(((u8 *) rsp_hdr) + SMP_RESP_HDR_SZ,
1572 smp_resp, word_cnt);
1574 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
1575 SCI_SUCCESS);
1577 sci_change_state(&ireq->sm, SCI_REQ_SMP_WAIT_TC_COMP);
1578 } else {
1580 * This was not a response frame why did it get
1581 * forwarded?
1583 dev_err(scic_to_dev(scic),
1584 "%s: SCIC SMP Request 0x%p received unexpected "
1585 "frame %d type 0x%02x\n",
1586 __func__,
1587 ireq,
1588 frame_index,
1589 rsp_hdr->frame_type);
1591 scic_sds_request_set_status(ireq,
1592 SCU_TASK_DONE_SMP_FRM_TYPE_ERR,
1593 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1595 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1598 scic_sds_controller_release_frame(scic, frame_index);
1600 return SCI_SUCCESS;
1603 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
1604 return scic_sds_stp_request_udma_general_frame_handler(ireq,
1605 frame_index);
1607 case SCI_REQ_STP_UDMA_WAIT_D2H:
1608 /* Use the general frame handler to copy the resposne data */
1609 status = scic_sds_stp_request_udma_general_frame_handler(ireq,
1610 frame_index);
1612 if (status != SCI_SUCCESS)
1613 return status;
1615 scic_sds_stp_request_udma_complete_request(ireq,
1616 SCU_TASK_DONE_CHECK_RESPONSE,
1617 SCI_FAILURE_IO_RESPONSE_VALID);
1619 return SCI_SUCCESS;
1621 case SCI_REQ_STP_NON_DATA_WAIT_D2H: {
1622 struct dev_to_host_fis *frame_header;
1623 u32 *frame_buffer;
1625 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1626 frame_index,
1627 (void **)&frame_header);
1629 if (status != SCI_SUCCESS) {
1630 dev_err(scic_to_dev(scic),
1631 "%s: SCIC IO Request 0x%p could not get frame "
1632 "header for frame index %d, status %x\n",
1633 __func__,
1634 stp_req,
1635 frame_index,
1636 status);
1638 return status;
1641 switch (frame_header->fis_type) {
1642 case FIS_REGD2H:
1643 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1644 frame_index,
1645 (void **)&frame_buffer);
1647 scic_sds_controller_copy_sata_response(&ireq->stp.rsp,
1648 frame_header,
1649 frame_buffer);
1651 /* The command has completed with error */
1652 scic_sds_request_set_status(ireq, SCU_TASK_DONE_CHECK_RESPONSE,
1653 SCI_FAILURE_IO_RESPONSE_VALID);
1654 break;
1656 default:
1657 dev_warn(scic_to_dev(scic),
1658 "%s: IO Request:0x%p Frame Id:%d protocol "
1659 "violation occurred\n", __func__, stp_req,
1660 frame_index);
1662 scic_sds_request_set_status(ireq, SCU_TASK_DONE_UNEXP_FIS,
1663 SCI_FAILURE_PROTOCOL_VIOLATION);
1664 break;
1667 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1669 /* Frame has been decoded return it to the controller */
1670 scic_sds_controller_release_frame(scic, frame_index);
1672 return status;
1675 case SCI_REQ_STP_PIO_WAIT_FRAME: {
1676 struct sas_task *task = isci_request_access_task(ireq);
1677 struct dev_to_host_fis *frame_header;
1678 u32 *frame_buffer;
1680 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1681 frame_index,
1682 (void **)&frame_header);
1684 if (status != SCI_SUCCESS) {
1685 dev_err(scic_to_dev(scic),
1686 "%s: SCIC IO Request 0x%p could not get frame "
1687 "header for frame index %d, status %x\n",
1688 __func__, stp_req, frame_index, status);
1689 return status;
1692 switch (frame_header->fis_type) {
1693 case FIS_PIO_SETUP:
1694 /* Get from the frame buffer the PIO Setup Data */
1695 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1696 frame_index,
1697 (void **)&frame_buffer);
1699 /* Get the data from the PIO Setup The SCU Hardware
1700 * returns first word in the frame_header and the rest
1701 * of the data is in the frame buffer so we need to
1702 * back up one dword
1705 /* transfer_count: first 16bits in the 4th dword */
1706 stp_req->pio_len = frame_buffer[3] & 0xffff;
1708 /* status: 4th byte in the 3rd dword */
1709 stp_req->status = (frame_buffer[2] >> 24) & 0xff;
1711 scic_sds_controller_copy_sata_response(&ireq->stp.rsp,
1712 frame_header,
1713 frame_buffer);
1715 ireq->stp.rsp.status = stp_req->status;
1717 /* The next state is dependent on whether the
1718 * request was PIO Data-in or Data out
1720 if (task->data_dir == DMA_FROM_DEVICE) {
1721 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_IN);
1722 } else if (task->data_dir == DMA_TO_DEVICE) {
1723 /* Transmit data */
1724 status = scic_sds_stp_request_pio_data_out_transmit_data(ireq);
1725 if (status != SCI_SUCCESS)
1726 break;
1727 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_OUT);
1729 break;
1731 case FIS_SETDEVBITS:
1732 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
1733 break;
1735 case FIS_REGD2H:
1736 if (frame_header->status & ATA_BUSY) {
1738 * Now why is the drive sending a D2H Register
1739 * FIS when it is still busy? Do nothing since
1740 * we are still in the right state.
1742 dev_dbg(scic_to_dev(scic),
1743 "%s: SCIC PIO Request 0x%p received "
1744 "D2H Register FIS with BSY status "
1745 "0x%x\n",
1746 __func__,
1747 stp_req,
1748 frame_header->status);
1749 break;
1752 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1753 frame_index,
1754 (void **)&frame_buffer);
1756 scic_sds_controller_copy_sata_response(&ireq->stp.req,
1757 frame_header,
1758 frame_buffer);
1760 scic_sds_request_set_status(ireq,
1761 SCU_TASK_DONE_CHECK_RESPONSE,
1762 SCI_FAILURE_IO_RESPONSE_VALID);
1764 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1765 break;
1767 default:
1768 /* FIXME: what do we do here? */
1769 break;
1772 /* Frame is decoded return it to the controller */
1773 scic_sds_controller_release_frame(scic, frame_index);
1775 return status;
1778 case SCI_REQ_STP_PIO_DATA_IN: {
1779 struct dev_to_host_fis *frame_header;
1780 struct sata_fis_data *frame_buffer;
1782 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1783 frame_index,
1784 (void **)&frame_header);
1786 if (status != SCI_SUCCESS) {
1787 dev_err(scic_to_dev(scic),
1788 "%s: SCIC IO Request 0x%p could not get frame "
1789 "header for frame index %d, status %x\n",
1790 __func__,
1791 stp_req,
1792 frame_index,
1793 status);
1794 return status;
1797 if (frame_header->fis_type != FIS_DATA) {
1798 dev_err(scic_to_dev(scic),
1799 "%s: SCIC PIO Request 0x%p received frame %d "
1800 "with fis type 0x%02x when expecting a data "
1801 "fis.\n",
1802 __func__,
1803 stp_req,
1804 frame_index,
1805 frame_header->fis_type);
1807 scic_sds_request_set_status(ireq,
1808 SCU_TASK_DONE_GOOD,
1809 SCI_FAILURE_IO_REQUIRES_SCSI_ABORT);
1811 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1813 /* Frame is decoded return it to the controller */
1814 scic_sds_controller_release_frame(scic, frame_index);
1815 return status;
1818 if (stp_req->sgl.index < 0) {
1819 ireq->saved_rx_frame_index = frame_index;
1820 stp_req->pio_len = 0;
1821 } else {
1822 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1823 frame_index,
1824 (void **)&frame_buffer);
1826 status = scic_sds_stp_request_pio_data_in_copy_data(stp_req,
1827 (u8 *)frame_buffer);
1829 /* Frame is decoded return it to the controller */
1830 scic_sds_controller_release_frame(scic, frame_index);
1833 /* Check for the end of the transfer, are there more
1834 * bytes remaining for this data transfer
1836 if (status != SCI_SUCCESS || stp_req->pio_len != 0)
1837 return status;
1839 if ((stp_req->status & ATA_BUSY) == 0) {
1840 scic_sds_request_set_status(ireq,
1841 SCU_TASK_DONE_CHECK_RESPONSE,
1842 SCI_FAILURE_IO_RESPONSE_VALID);
1844 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1845 } else {
1846 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
1848 return status;
1851 case SCI_REQ_STP_SOFT_RESET_WAIT_D2H: {
1852 struct dev_to_host_fis *frame_header;
1853 u32 *frame_buffer;
1855 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1856 frame_index,
1857 (void **)&frame_header);
1858 if (status != SCI_SUCCESS) {
1859 dev_err(scic_to_dev(scic),
1860 "%s: SCIC IO Request 0x%p could not get frame "
1861 "header for frame index %d, status %x\n",
1862 __func__,
1863 stp_req,
1864 frame_index,
1865 status);
1866 return status;
1869 switch (frame_header->fis_type) {
1870 case FIS_REGD2H:
1871 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1872 frame_index,
1873 (void **)&frame_buffer);
1875 scic_sds_controller_copy_sata_response(&ireq->stp.rsp,
1876 frame_header,
1877 frame_buffer);
1879 /* The command has completed with error */
1880 scic_sds_request_set_status(ireq,
1881 SCU_TASK_DONE_CHECK_RESPONSE,
1882 SCI_FAILURE_IO_RESPONSE_VALID);
1883 break;
1885 default:
1886 dev_warn(scic_to_dev(scic),
1887 "%s: IO Request:0x%p Frame Id:%d protocol "
1888 "violation occurred\n",
1889 __func__,
1890 stp_req,
1891 frame_index);
1893 scic_sds_request_set_status(ireq,
1894 SCU_TASK_DONE_UNEXP_FIS,
1895 SCI_FAILURE_PROTOCOL_VIOLATION);
1896 break;
1899 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1901 /* Frame has been decoded return it to the controller */
1902 scic_sds_controller_release_frame(scic, frame_index);
1904 return status;
1906 case SCI_REQ_ABORTING:
1908 * TODO: Is it even possible to get an unsolicited frame in the
1909 * aborting state?
1911 scic_sds_controller_release_frame(scic, frame_index);
1912 return SCI_SUCCESS;
1914 default:
1915 dev_warn(scic_to_dev(scic),
1916 "%s: SCIC IO Request given unexpected frame %x while "
1917 "in state %d\n",
1918 __func__,
1919 frame_index,
1920 state);
1922 scic_sds_controller_release_frame(scic, frame_index);
1923 return SCI_FAILURE_INVALID_STATE;
1927 static enum sci_status stp_request_udma_await_tc_event(struct isci_request *ireq,
1928 u32 completion_code)
1930 enum sci_status status = SCI_SUCCESS;
1932 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1933 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1934 scic_sds_stp_request_udma_complete_request(ireq,
1935 SCU_TASK_DONE_GOOD,
1936 SCI_SUCCESS);
1937 break;
1938 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
1939 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
1940 /* We must check ther response buffer to see if the D2H
1941 * Register FIS was received before we got the TC
1942 * completion.
1944 if (ireq->stp.rsp.fis_type == FIS_REGD2H) {
1945 scic_sds_remote_device_suspend(ireq->target_device,
1946 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
1948 scic_sds_stp_request_udma_complete_request(ireq,
1949 SCU_TASK_DONE_CHECK_RESPONSE,
1950 SCI_FAILURE_IO_RESPONSE_VALID);
1951 } else {
1952 /* If we have an error completion status for the
1953 * TC then we can expect a D2H register FIS from
1954 * the device so we must change state to wait
1955 * for it
1957 sci_change_state(&ireq->sm, SCI_REQ_STP_UDMA_WAIT_D2H);
1959 break;
1961 /* TODO Check to see if any of these completion status need to
1962 * wait for the device to host register fis.
1964 /* TODO We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR
1965 * - this comes only for B0
1967 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_INV_FIS_LEN):
1968 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
1969 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_R_ERR):
1970 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CMD_LL_R_ERR):
1971 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CRC_ERR):
1972 scic_sds_remote_device_suspend(ireq->target_device,
1973 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
1974 /* Fall through to the default case */
1975 default:
1976 /* All other completion status cause the IO to be complete. */
1977 scic_sds_stp_request_udma_complete_request(ireq,
1978 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1979 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1980 break;
1983 return status;
1986 static enum sci_status
1987 stp_request_soft_reset_await_h2d_asserted_tc_event(struct isci_request *ireq,
1988 u32 completion_code)
1990 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1991 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1992 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
1993 SCI_SUCCESS);
1995 sci_change_state(&ireq->sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG);
1996 break;
1998 default:
2000 * All other completion status cause the IO to be complete.
2001 * If a NAK was received, then it is up to the user to retry
2002 * the request.
2004 scic_sds_request_set_status(ireq,
2005 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
2006 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
2008 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2009 break;
2012 return SCI_SUCCESS;
2015 static enum sci_status
2016 stp_request_soft_reset_await_h2d_diagnostic_tc_event(struct isci_request *ireq,
2017 u32 completion_code)
2019 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2020 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
2021 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
2022 SCI_SUCCESS);
2024 sci_change_state(&ireq->sm, SCI_REQ_STP_SOFT_RESET_WAIT_D2H);
2025 break;
2027 default:
2028 /* All other completion status cause the IO to be complete. If
2029 * a NAK was received, then it is up to the user to retry the
2030 * request.
2032 scic_sds_request_set_status(ireq,
2033 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
2034 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
2036 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2037 break;
2040 return SCI_SUCCESS;
2043 enum sci_status
2044 scic_sds_io_request_tc_completion(struct isci_request *ireq,
2045 u32 completion_code)
2047 enum sci_base_request_states state;
2048 struct scic_sds_controller *scic = ireq->owning_controller;
2050 state = ireq->sm.current_state_id;
2052 switch (state) {
2053 case SCI_REQ_STARTED:
2054 return request_started_state_tc_event(ireq, completion_code);
2056 case SCI_REQ_TASK_WAIT_TC_COMP:
2057 return ssp_task_request_await_tc_event(ireq,
2058 completion_code);
2060 case SCI_REQ_SMP_WAIT_RESP:
2061 return smp_request_await_response_tc_event(ireq,
2062 completion_code);
2064 case SCI_REQ_SMP_WAIT_TC_COMP:
2065 return smp_request_await_tc_event(ireq, completion_code);
2067 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
2068 return stp_request_udma_await_tc_event(ireq,
2069 completion_code);
2071 case SCI_REQ_STP_NON_DATA_WAIT_H2D:
2072 return stp_request_non_data_await_h2d_tc_event(ireq,
2073 completion_code);
2075 case SCI_REQ_STP_PIO_WAIT_H2D:
2076 return stp_request_pio_await_h2d_completion_tc_event(ireq,
2077 completion_code);
2079 case SCI_REQ_STP_PIO_DATA_OUT:
2080 return pio_data_out_tx_done_tc_event(ireq, completion_code);
2082 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
2083 return stp_request_soft_reset_await_h2d_asserted_tc_event(ireq,
2084 completion_code);
2086 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
2087 return stp_request_soft_reset_await_h2d_diagnostic_tc_event(ireq,
2088 completion_code);
2090 case SCI_REQ_ABORTING:
2091 return request_aborting_state_tc_event(ireq,
2092 completion_code);
2094 default:
2095 dev_warn(scic_to_dev(scic),
2096 "%s: SCIC IO Request given task completion "
2097 "notification %x while in wrong state %d\n",
2098 __func__,
2099 completion_code,
2100 state);
2101 return SCI_FAILURE_INVALID_STATE;
2106 * isci_request_process_response_iu() - This function sets the status and
2107 * response iu, in the task struct, from the request object for the upper
2108 * layer driver.
2109 * @sas_task: This parameter is the task struct from the upper layer driver.
2110 * @resp_iu: This parameter points to the response iu of the completed request.
2111 * @dev: This parameter specifies the linux device struct.
2113 * none.
2115 static void isci_request_process_response_iu(
2116 struct sas_task *task,
2117 struct ssp_response_iu *resp_iu,
2118 struct device *dev)
2120 dev_dbg(dev,
2121 "%s: resp_iu = %p "
2122 "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
2123 "resp_iu->response_data_len = %x, "
2124 "resp_iu->sense_data_len = %x\nrepsonse data: ",
2125 __func__,
2126 resp_iu,
2127 resp_iu->status,
2128 resp_iu->datapres,
2129 resp_iu->response_data_len,
2130 resp_iu->sense_data_len);
2132 task->task_status.stat = resp_iu->status;
2134 /* libsas updates the task status fields based on the response iu. */
2135 sas_ssp_task_response(dev, task, resp_iu);
2139 * isci_request_set_open_reject_status() - This function prepares the I/O
2140 * completion for OPEN_REJECT conditions.
2141 * @request: This parameter is the completed isci_request object.
2142 * @response_ptr: This parameter specifies the service response for the I/O.
2143 * @status_ptr: This parameter specifies the exec status for the I/O.
2144 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2145 * the LLDD with respect to completing this request or forcing an abort
2146 * condition on the I/O.
2147 * @open_rej_reason: This parameter specifies the encoded reason for the
2148 * abandon-class reject.
2150 * none.
2152 static void isci_request_set_open_reject_status(
2153 struct isci_request *request,
2154 struct sas_task *task,
2155 enum service_response *response_ptr,
2156 enum exec_status *status_ptr,
2157 enum isci_completion_selection *complete_to_host_ptr,
2158 enum sas_open_rej_reason open_rej_reason)
2160 /* Task in the target is done. */
2161 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2162 *response_ptr = SAS_TASK_UNDELIVERED;
2163 *status_ptr = SAS_OPEN_REJECT;
2164 *complete_to_host_ptr = isci_perform_normal_io_completion;
2165 task->task_status.open_rej_reason = open_rej_reason;
2169 * isci_request_handle_controller_specific_errors() - This function decodes
2170 * controller-specific I/O completion error conditions.
2171 * @request: This parameter is the completed isci_request object.
2172 * @response_ptr: This parameter specifies the service response for the I/O.
2173 * @status_ptr: This parameter specifies the exec status for the I/O.
2174 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2175 * the LLDD with respect to completing this request or forcing an abort
2176 * condition on the I/O.
2178 * none.
2180 static void isci_request_handle_controller_specific_errors(
2181 struct isci_remote_device *idev,
2182 struct isci_request *request,
2183 struct sas_task *task,
2184 enum service_response *response_ptr,
2185 enum exec_status *status_ptr,
2186 enum isci_completion_selection *complete_to_host_ptr)
2188 unsigned int cstatus;
2190 cstatus = request->scu_status;
2192 dev_dbg(&request->isci_host->pdev->dev,
2193 "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
2194 "- controller status = 0x%x\n",
2195 __func__, request, cstatus);
2197 /* Decode the controller-specific errors; most
2198 * important is to recognize those conditions in which
2199 * the target may still have a task outstanding that
2200 * must be aborted.
2202 * Note that there are SCU completion codes being
2203 * named in the decode below for which SCIC has already
2204 * done work to handle them in a way other than as
2205 * a controller-specific completion code; these are left
2206 * in the decode below for completeness sake.
2208 switch (cstatus) {
2209 case SCU_TASK_DONE_DMASETUP_DIRERR:
2210 /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
2211 case SCU_TASK_DONE_XFERCNT_ERR:
2212 /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
2213 if (task->task_proto == SAS_PROTOCOL_SMP) {
2214 /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
2215 *response_ptr = SAS_TASK_COMPLETE;
2217 /* See if the device has been/is being stopped. Note
2218 * that we ignore the quiesce state, since we are
2219 * concerned about the actual device state.
2221 if (!idev)
2222 *status_ptr = SAS_DEVICE_UNKNOWN;
2223 else
2224 *status_ptr = SAS_ABORTED_TASK;
2226 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2228 *complete_to_host_ptr =
2229 isci_perform_normal_io_completion;
2230 } else {
2231 /* Task in the target is not done. */
2232 *response_ptr = SAS_TASK_UNDELIVERED;
2234 if (!idev)
2235 *status_ptr = SAS_DEVICE_UNKNOWN;
2236 else
2237 *status_ptr = SAM_STAT_TASK_ABORTED;
2239 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2241 *complete_to_host_ptr =
2242 isci_perform_error_io_completion;
2245 break;
2247 case SCU_TASK_DONE_CRC_ERR:
2248 case SCU_TASK_DONE_NAK_CMD_ERR:
2249 case SCU_TASK_DONE_EXCESS_DATA:
2250 case SCU_TASK_DONE_UNEXP_FIS:
2251 /* Also SCU_TASK_DONE_UNEXP_RESP: */
2252 case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
2253 case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
2254 case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
2255 /* These are conditions in which the target
2256 * has completed the task, so that no cleanup
2257 * is necessary.
2259 *response_ptr = SAS_TASK_COMPLETE;
2261 /* See if the device has been/is being stopped. Note
2262 * that we ignore the quiesce state, since we are
2263 * concerned about the actual device state.
2265 if (!idev)
2266 *status_ptr = SAS_DEVICE_UNKNOWN;
2267 else
2268 *status_ptr = SAS_ABORTED_TASK;
2270 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2272 *complete_to_host_ptr = isci_perform_normal_io_completion;
2273 break;
2276 /* Note that the only open reject completion codes seen here will be
2277 * abandon-class codes; all others are automatically retried in the SCU.
2279 case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2281 isci_request_set_open_reject_status(
2282 request, task, response_ptr, status_ptr,
2283 complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
2284 break;
2286 case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2288 /* Note - the return of AB0 will change when
2289 * libsas implements detection of zone violations.
2291 isci_request_set_open_reject_status(
2292 request, task, response_ptr, status_ptr,
2293 complete_to_host_ptr, SAS_OREJ_RESV_AB0);
2294 break;
2296 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2298 isci_request_set_open_reject_status(
2299 request, task, response_ptr, status_ptr,
2300 complete_to_host_ptr, SAS_OREJ_RESV_AB1);
2301 break;
2303 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2305 isci_request_set_open_reject_status(
2306 request, task, response_ptr, status_ptr,
2307 complete_to_host_ptr, SAS_OREJ_RESV_AB2);
2308 break;
2310 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2312 isci_request_set_open_reject_status(
2313 request, task, response_ptr, status_ptr,
2314 complete_to_host_ptr, SAS_OREJ_RESV_AB3);
2315 break;
2317 case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2319 isci_request_set_open_reject_status(
2320 request, task, response_ptr, status_ptr,
2321 complete_to_host_ptr, SAS_OREJ_BAD_DEST);
2322 break;
2324 case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
2326 isci_request_set_open_reject_status(
2327 request, task, response_ptr, status_ptr,
2328 complete_to_host_ptr, SAS_OREJ_STP_NORES);
2329 break;
2331 case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
2333 isci_request_set_open_reject_status(
2334 request, task, response_ptr, status_ptr,
2335 complete_to_host_ptr, SAS_OREJ_EPROTO);
2336 break;
2338 case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
2340 isci_request_set_open_reject_status(
2341 request, task, response_ptr, status_ptr,
2342 complete_to_host_ptr, SAS_OREJ_CONN_RATE);
2343 break;
2345 case SCU_TASK_DONE_LL_R_ERR:
2346 /* Also SCU_TASK_DONE_ACK_NAK_TO: */
2347 case SCU_TASK_DONE_LL_PERR:
2348 case SCU_TASK_DONE_LL_SY_TERM:
2349 /* Also SCU_TASK_DONE_NAK_ERR:*/
2350 case SCU_TASK_DONE_LL_LF_TERM:
2351 /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
2352 case SCU_TASK_DONE_LL_ABORT_ERR:
2353 case SCU_TASK_DONE_SEQ_INV_TYPE:
2354 /* Also SCU_TASK_DONE_UNEXP_XR: */
2355 case SCU_TASK_DONE_XR_IU_LEN_ERR:
2356 case SCU_TASK_DONE_INV_FIS_LEN:
2357 /* Also SCU_TASK_DONE_XR_WD_LEN: */
2358 case SCU_TASK_DONE_SDMA_ERR:
2359 case SCU_TASK_DONE_OFFSET_ERR:
2360 case SCU_TASK_DONE_MAX_PLD_ERR:
2361 case SCU_TASK_DONE_LF_ERR:
2362 case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
2363 case SCU_TASK_DONE_SMP_LL_RX_ERR:
2364 case SCU_TASK_DONE_UNEXP_DATA:
2365 case SCU_TASK_DONE_UNEXP_SDBFIS:
2366 case SCU_TASK_DONE_REG_ERR:
2367 case SCU_TASK_DONE_SDB_ERR:
2368 case SCU_TASK_DONE_TASK_ABORT:
2369 default:
2370 /* Task in the target is not done. */
2371 *response_ptr = SAS_TASK_UNDELIVERED;
2372 *status_ptr = SAM_STAT_TASK_ABORTED;
2374 if (task->task_proto == SAS_PROTOCOL_SMP) {
2375 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2377 *complete_to_host_ptr = isci_perform_normal_io_completion;
2378 } else {
2379 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2381 *complete_to_host_ptr = isci_perform_error_io_completion;
2383 break;
2388 * isci_task_save_for_upper_layer_completion() - This function saves the
2389 * request for later completion to the upper layer driver.
2390 * @host: This parameter is a pointer to the host on which the the request
2391 * should be queued (either as an error or success).
2392 * @request: This parameter is the completed request.
2393 * @response: This parameter is the response code for the completed task.
2394 * @status: This parameter is the status code for the completed task.
2396 * none.
2398 static void isci_task_save_for_upper_layer_completion(
2399 struct isci_host *host,
2400 struct isci_request *request,
2401 enum service_response response,
2402 enum exec_status status,
2403 enum isci_completion_selection task_notification_selection)
2405 struct sas_task *task = isci_request_access_task(request);
2407 task_notification_selection
2408 = isci_task_set_completion_status(task, response, status,
2409 task_notification_selection);
2411 /* Tasks aborted specifically by a call to the lldd_abort_task
2412 * function should not be completed to the host in the regular path.
2414 switch (task_notification_selection) {
2416 case isci_perform_normal_io_completion:
2418 /* Normal notification (task_done) */
2419 dev_dbg(&host->pdev->dev,
2420 "%s: Normal - task = %p, response=%d (%d), status=%d (%d)\n",
2421 __func__,
2422 task,
2423 task->task_status.resp, response,
2424 task->task_status.stat, status);
2425 /* Add to the completed list. */
2426 list_add(&request->completed_node,
2427 &host->requests_to_complete);
2429 /* Take the request off the device's pending request list. */
2430 list_del_init(&request->dev_node);
2431 break;
2433 case isci_perform_aborted_io_completion:
2434 /* No notification to libsas because this request is
2435 * already in the abort path.
2437 dev_warn(&host->pdev->dev,
2438 "%s: Aborted - task = %p, response=%d (%d), status=%d (%d)\n",
2439 __func__,
2440 task,
2441 task->task_status.resp, response,
2442 task->task_status.stat, status);
2444 /* Wake up whatever process was waiting for this
2445 * request to complete.
2447 WARN_ON(request->io_request_completion == NULL);
2449 if (request->io_request_completion != NULL) {
2451 /* Signal whoever is waiting that this
2452 * request is complete.
2454 complete(request->io_request_completion);
2456 break;
2458 case isci_perform_error_io_completion:
2459 /* Use sas_task_abort */
2460 dev_warn(&host->pdev->dev,
2461 "%s: Error - task = %p, response=%d (%d), status=%d (%d)\n",
2462 __func__,
2463 task,
2464 task->task_status.resp, response,
2465 task->task_status.stat, status);
2466 /* Add to the aborted list. */
2467 list_add(&request->completed_node,
2468 &host->requests_to_errorback);
2469 break;
2471 default:
2472 dev_warn(&host->pdev->dev,
2473 "%s: Unknown - task = %p, response=%d (%d), status=%d (%d)\n",
2474 __func__,
2475 task,
2476 task->task_status.resp, response,
2477 task->task_status.stat, status);
2479 /* Add to the error to libsas list. */
2480 list_add(&request->completed_node,
2481 &host->requests_to_errorback);
2482 break;
2486 static void isci_request_io_request_complete(struct isci_host *isci_host,
2487 struct isci_request *request,
2488 enum sci_io_status completion_status)
2490 struct sas_task *task = isci_request_access_task(request);
2491 struct ssp_response_iu *resp_iu;
2492 void *resp_buf;
2493 unsigned long task_flags;
2494 struct isci_remote_device *idev = isci_lookup_device(task->dev);
2495 enum service_response response = SAS_TASK_UNDELIVERED;
2496 enum exec_status status = SAS_ABORTED_TASK;
2497 enum isci_request_status request_status;
2498 enum isci_completion_selection complete_to_host
2499 = isci_perform_normal_io_completion;
2501 dev_dbg(&isci_host->pdev->dev,
2502 "%s: request = %p, task = %p,\n"
2503 "task->data_dir = %d completion_status = 0x%x\n",
2504 __func__,
2505 request,
2506 task,
2507 task->data_dir,
2508 completion_status);
2510 spin_lock(&request->state_lock);
2511 request_status = isci_request_get_state(request);
2513 /* Decode the request status. Note that if the request has been
2514 * aborted by a task management function, we don't care
2515 * what the status is.
2517 switch (request_status) {
2519 case aborted:
2520 /* "aborted" indicates that the request was aborted by a task
2521 * management function, since once a task management request is
2522 * perfomed by the device, the request only completes because
2523 * of the subsequent driver terminate.
2525 * Aborted also means an external thread is explicitly managing
2526 * this request, so that we do not complete it up the stack.
2528 * The target is still there (since the TMF was successful).
2530 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2531 response = SAS_TASK_COMPLETE;
2533 /* See if the device has been/is being stopped. Note
2534 * that we ignore the quiesce state, since we are
2535 * concerned about the actual device state.
2537 if (!idev)
2538 status = SAS_DEVICE_UNKNOWN;
2539 else
2540 status = SAS_ABORTED_TASK;
2542 complete_to_host = isci_perform_aborted_io_completion;
2543 /* This was an aborted request. */
2545 spin_unlock(&request->state_lock);
2546 break;
2548 case aborting:
2549 /* aborting means that the task management function tried and
2550 * failed to abort the request. We need to note the request
2551 * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
2552 * target as down.
2554 * Aborting also means an external thread is explicitly managing
2555 * this request, so that we do not complete it up the stack.
2557 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2558 response = SAS_TASK_UNDELIVERED;
2560 if (!idev)
2561 /* The device has been /is being stopped. Note that
2562 * we ignore the quiesce state, since we are
2563 * concerned about the actual device state.
2565 status = SAS_DEVICE_UNKNOWN;
2566 else
2567 status = SAS_PHY_DOWN;
2569 complete_to_host = isci_perform_aborted_io_completion;
2571 /* This was an aborted request. */
2573 spin_unlock(&request->state_lock);
2574 break;
2576 case terminating:
2578 /* This was an terminated request. This happens when
2579 * the I/O is being terminated because of an action on
2580 * the device (reset, tear down, etc.), and the I/O needs
2581 * to be completed up the stack.
2583 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2584 response = SAS_TASK_UNDELIVERED;
2586 /* See if the device has been/is being stopped. Note
2587 * that we ignore the quiesce state, since we are
2588 * concerned about the actual device state.
2590 if (!idev)
2591 status = SAS_DEVICE_UNKNOWN;
2592 else
2593 status = SAS_ABORTED_TASK;
2595 complete_to_host = isci_perform_aborted_io_completion;
2597 /* This was a terminated request. */
2599 spin_unlock(&request->state_lock);
2600 break;
2602 case dead:
2603 /* This was a terminated request that timed-out during the
2604 * termination process. There is no task to complete to
2605 * libsas.
2607 complete_to_host = isci_perform_normal_io_completion;
2608 spin_unlock(&request->state_lock);
2609 break;
2611 default:
2613 /* The request is done from an SCU HW perspective. */
2614 request->status = completed;
2616 spin_unlock(&request->state_lock);
2618 /* This is an active request being completed from the core. */
2619 switch (completion_status) {
2621 case SCI_IO_FAILURE_RESPONSE_VALID:
2622 dev_dbg(&isci_host->pdev->dev,
2623 "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
2624 __func__,
2625 request,
2626 task);
2628 if (sas_protocol_ata(task->task_proto)) {
2629 resp_buf = &request->stp.rsp;
2630 isci_request_process_stp_response(task,
2631 resp_buf);
2632 } else if (SAS_PROTOCOL_SSP == task->task_proto) {
2634 /* crack the iu response buffer. */
2635 resp_iu = &request->ssp.rsp;
2636 isci_request_process_response_iu(task, resp_iu,
2637 &isci_host->pdev->dev);
2639 } else if (SAS_PROTOCOL_SMP == task->task_proto) {
2641 dev_err(&isci_host->pdev->dev,
2642 "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
2643 "SAS_PROTOCOL_SMP protocol\n",
2644 __func__);
2646 } else
2647 dev_err(&isci_host->pdev->dev,
2648 "%s: unknown protocol\n", __func__);
2650 /* use the task status set in the task struct by the
2651 * isci_request_process_response_iu call.
2653 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2654 response = task->task_status.resp;
2655 status = task->task_status.stat;
2656 break;
2658 case SCI_IO_SUCCESS:
2659 case SCI_IO_SUCCESS_IO_DONE_EARLY:
2661 response = SAS_TASK_COMPLETE;
2662 status = SAM_STAT_GOOD;
2663 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2665 if (task->task_proto == SAS_PROTOCOL_SMP) {
2666 void *rsp = &request->smp.rsp;
2668 dev_dbg(&isci_host->pdev->dev,
2669 "%s: SMP protocol completion\n",
2670 __func__);
2672 sg_copy_from_buffer(
2673 &task->smp_task.smp_resp, 1,
2674 rsp, sizeof(struct smp_resp));
2675 } else if (completion_status
2676 == SCI_IO_SUCCESS_IO_DONE_EARLY) {
2678 /* This was an SSP / STP / SATA transfer.
2679 * There is a possibility that less data than
2680 * the maximum was transferred.
2682 u32 transferred_length = sci_req_tx_bytes(request);
2684 task->task_status.residual
2685 = task->total_xfer_len - transferred_length;
2687 /* If there were residual bytes, call this an
2688 * underrun.
2690 if (task->task_status.residual != 0)
2691 status = SAS_DATA_UNDERRUN;
2693 dev_dbg(&isci_host->pdev->dev,
2694 "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
2695 __func__,
2696 status);
2698 } else
2699 dev_dbg(&isci_host->pdev->dev,
2700 "%s: SCI_IO_SUCCESS\n",
2701 __func__);
2703 break;
2705 case SCI_IO_FAILURE_TERMINATED:
2706 dev_dbg(&isci_host->pdev->dev,
2707 "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
2708 __func__,
2709 request,
2710 task);
2712 /* The request was terminated explicitly. No handling
2713 * is needed in the SCSI error handler path.
2715 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2716 response = SAS_TASK_UNDELIVERED;
2718 /* See if the device has been/is being stopped. Note
2719 * that we ignore the quiesce state, since we are
2720 * concerned about the actual device state.
2722 if (!idev)
2723 status = SAS_DEVICE_UNKNOWN;
2724 else
2725 status = SAS_ABORTED_TASK;
2727 complete_to_host = isci_perform_normal_io_completion;
2728 break;
2730 case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
2732 isci_request_handle_controller_specific_errors(
2733 idev, request, task, &response, &status,
2734 &complete_to_host);
2736 break;
2738 case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
2739 /* This is a special case, in that the I/O completion
2740 * is telling us that the device needs a reset.
2741 * In order for the device reset condition to be
2742 * noticed, the I/O has to be handled in the error
2743 * handler. Set the reset flag and cause the
2744 * SCSI error thread to be scheduled.
2746 spin_lock_irqsave(&task->task_state_lock, task_flags);
2747 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
2748 spin_unlock_irqrestore(&task->task_state_lock, task_flags);
2750 /* Fail the I/O. */
2751 response = SAS_TASK_UNDELIVERED;
2752 status = SAM_STAT_TASK_ABORTED;
2754 complete_to_host = isci_perform_error_io_completion;
2755 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2756 break;
2758 case SCI_FAILURE_RETRY_REQUIRED:
2760 /* Fail the I/O so it can be retried. */
2761 response = SAS_TASK_UNDELIVERED;
2762 if (!idev)
2763 status = SAS_DEVICE_UNKNOWN;
2764 else
2765 status = SAS_ABORTED_TASK;
2767 complete_to_host = isci_perform_normal_io_completion;
2768 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2769 break;
2772 default:
2773 /* Catch any otherwise unhandled error codes here. */
2774 dev_warn(&isci_host->pdev->dev,
2775 "%s: invalid completion code: 0x%x - "
2776 "isci_request = %p\n",
2777 __func__, completion_status, request);
2779 response = SAS_TASK_UNDELIVERED;
2781 /* See if the device has been/is being stopped. Note
2782 * that we ignore the quiesce state, since we are
2783 * concerned about the actual device state.
2785 if (!idev)
2786 status = SAS_DEVICE_UNKNOWN;
2787 else
2788 status = SAS_ABORTED_TASK;
2790 if (SAS_PROTOCOL_SMP == task->task_proto) {
2791 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2792 complete_to_host = isci_perform_normal_io_completion;
2793 } else {
2794 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2795 complete_to_host = isci_perform_error_io_completion;
2797 break;
2799 break;
2802 switch (task->task_proto) {
2803 case SAS_PROTOCOL_SSP:
2804 if (task->data_dir == DMA_NONE)
2805 break;
2806 if (task->num_scatter == 0)
2807 /* 0 indicates a single dma address */
2808 dma_unmap_single(&isci_host->pdev->dev,
2809 request->zero_scatter_daddr,
2810 task->total_xfer_len, task->data_dir);
2811 else /* unmap the sgl dma addresses */
2812 dma_unmap_sg(&isci_host->pdev->dev, task->scatter,
2813 request->num_sg_entries, task->data_dir);
2814 break;
2815 case SAS_PROTOCOL_SMP: {
2816 struct scatterlist *sg = &task->smp_task.smp_req;
2817 struct smp_req *smp_req;
2818 void *kaddr;
2820 dma_unmap_sg(&isci_host->pdev->dev, sg, 1, DMA_TO_DEVICE);
2822 /* need to swab it back in case the command buffer is re-used */
2823 kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
2824 smp_req = kaddr + sg->offset;
2825 sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
2826 kunmap_atomic(kaddr, KM_IRQ0);
2827 break;
2829 default:
2830 break;
2833 /* Put the completed request on the correct list */
2834 isci_task_save_for_upper_layer_completion(isci_host, request, response,
2835 status, complete_to_host
2838 /* complete the io request to the core. */
2839 scic_controller_complete_io(&isci_host->sci,
2840 request->target_device,
2841 request);
2842 isci_put_device(idev);
2844 /* set terminated handle so it cannot be completed or
2845 * terminated again, and to cause any calls into abort
2846 * task to recognize the already completed case.
2848 set_bit(IREQ_TERMINATED, &request->flags);
2851 static void scic_sds_request_started_state_enter(struct sci_base_state_machine *sm)
2853 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
2854 struct domain_device *dev = sci_dev_to_domain(ireq->target_device);
2855 struct sas_task *task;
2857 /* XXX as hch said always creating an internal sas_task for tmf
2858 * requests would simplify the driver
2860 task = ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL;
2862 /* all unaccelerated request types (non ssp or ncq) handled with
2863 * substates
2865 if (!task && dev->dev_type == SAS_END_DEV) {
2866 sci_change_state(sm, SCI_REQ_TASK_WAIT_TC_COMP);
2867 } else if (!task &&
2868 (isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_high ||
2869 isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_low)) {
2870 sci_change_state(sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED);
2871 } else if (task && task->task_proto == SAS_PROTOCOL_SMP) {
2872 sci_change_state(sm, SCI_REQ_SMP_WAIT_RESP);
2873 } else if (task && sas_protocol_ata(task->task_proto) &&
2874 !task->ata_task.use_ncq) {
2875 u32 state;
2877 if (task->data_dir == DMA_NONE)
2878 state = SCI_REQ_STP_NON_DATA_WAIT_H2D;
2879 else if (task->ata_task.dma_xfer)
2880 state = SCI_REQ_STP_UDMA_WAIT_TC_COMP;
2881 else /* PIO */
2882 state = SCI_REQ_STP_PIO_WAIT_H2D;
2884 sci_change_state(sm, state);
2888 static void scic_sds_request_completed_state_enter(struct sci_base_state_machine *sm)
2890 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
2891 struct scic_sds_controller *scic = ireq->owning_controller;
2892 struct isci_host *ihost = scic_to_ihost(scic);
2894 /* Tell the SCI_USER that the IO request is complete */
2895 if (!test_bit(IREQ_TMF, &ireq->flags))
2896 isci_request_io_request_complete(ihost, ireq,
2897 ireq->sci_status);
2898 else
2899 isci_task_request_complete(ihost, ireq, ireq->sci_status);
2902 static void scic_sds_request_aborting_state_enter(struct sci_base_state_machine *sm)
2904 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
2906 /* Setting the abort bit in the Task Context is required by the silicon. */
2907 ireq->tc->abort = 1;
2910 static void scic_sds_stp_request_started_non_data_await_h2d_completion_enter(struct sci_base_state_machine *sm)
2912 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
2914 scic_sds_remote_device_set_working_request(ireq->target_device,
2915 ireq);
2918 static void scic_sds_stp_request_started_pio_await_h2d_completion_enter(struct sci_base_state_machine *sm)
2920 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
2922 scic_sds_remote_device_set_working_request(ireq->target_device,
2923 ireq);
2926 static void scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter(struct sci_base_state_machine *sm)
2928 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
2930 scic_sds_remote_device_set_working_request(ireq->target_device,
2931 ireq);
2934 static void scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter(struct sci_base_state_machine *sm)
2936 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
2937 struct scu_task_context *tc = ireq->tc;
2938 struct host_to_dev_fis *h2d_fis;
2939 enum sci_status status;
2941 /* Clear the SRST bit */
2942 h2d_fis = &ireq->stp.cmd;
2943 h2d_fis->control = 0;
2945 /* Clear the TC control bit */
2946 tc->control_frame = 0;
2948 status = scic_controller_continue_io(ireq);
2949 WARN_ONCE(status != SCI_SUCCESS, "isci: continue io failure\n");
2952 static const struct sci_base_state scic_sds_request_state_table[] = {
2953 [SCI_REQ_INIT] = { },
2954 [SCI_REQ_CONSTRUCTED] = { },
2955 [SCI_REQ_STARTED] = {
2956 .enter_state = scic_sds_request_started_state_enter,
2958 [SCI_REQ_STP_NON_DATA_WAIT_H2D] = {
2959 .enter_state = scic_sds_stp_request_started_non_data_await_h2d_completion_enter,
2961 [SCI_REQ_STP_NON_DATA_WAIT_D2H] = { },
2962 [SCI_REQ_STP_PIO_WAIT_H2D] = {
2963 .enter_state = scic_sds_stp_request_started_pio_await_h2d_completion_enter,
2965 [SCI_REQ_STP_PIO_WAIT_FRAME] = { },
2966 [SCI_REQ_STP_PIO_DATA_IN] = { },
2967 [SCI_REQ_STP_PIO_DATA_OUT] = { },
2968 [SCI_REQ_STP_UDMA_WAIT_TC_COMP] = { },
2969 [SCI_REQ_STP_UDMA_WAIT_D2H] = { },
2970 [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED] = {
2971 .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter,
2973 [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG] = {
2974 .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter,
2976 [SCI_REQ_STP_SOFT_RESET_WAIT_D2H] = { },
2977 [SCI_REQ_TASK_WAIT_TC_COMP] = { },
2978 [SCI_REQ_TASK_WAIT_TC_RESP] = { },
2979 [SCI_REQ_SMP_WAIT_RESP] = { },
2980 [SCI_REQ_SMP_WAIT_TC_COMP] = { },
2981 [SCI_REQ_COMPLETED] = {
2982 .enter_state = scic_sds_request_completed_state_enter,
2984 [SCI_REQ_ABORTING] = {
2985 .enter_state = scic_sds_request_aborting_state_enter,
2987 [SCI_REQ_FINAL] = { },
2990 static void
2991 scic_sds_general_request_construct(struct scic_sds_controller *scic,
2992 struct scic_sds_remote_device *sci_dev,
2993 struct isci_request *ireq)
2995 sci_init_sm(&ireq->sm, scic_sds_request_state_table, SCI_REQ_INIT);
2997 ireq->target_device = sci_dev;
2998 ireq->protocol = SCIC_NO_PROTOCOL;
2999 ireq->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
3001 ireq->sci_status = SCI_SUCCESS;
3002 ireq->scu_status = 0;
3003 ireq->post_context = 0xFFFFFFFF;
3006 static enum sci_status
3007 scic_io_request_construct(struct scic_sds_controller *scic,
3008 struct scic_sds_remote_device *sci_dev,
3009 struct isci_request *ireq)
3011 struct domain_device *dev = sci_dev_to_domain(sci_dev);
3012 enum sci_status status = SCI_SUCCESS;
3014 /* Build the common part of the request */
3015 scic_sds_general_request_construct(scic, sci_dev, ireq);
3017 if (sci_dev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
3018 return SCI_FAILURE_INVALID_REMOTE_DEVICE;
3020 if (dev->dev_type == SAS_END_DEV)
3021 /* pass */;
3022 else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
3023 memset(&ireq->stp.cmd, 0, sizeof(ireq->stp.cmd));
3024 else if (dev_is_expander(dev))
3025 /* pass */;
3026 else
3027 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
3029 memset(ireq->tc, 0, offsetof(struct scu_task_context, sgl_pair_ab));
3031 return status;
3034 enum sci_status scic_task_request_construct(struct scic_sds_controller *scic,
3035 struct scic_sds_remote_device *sci_dev,
3036 u16 io_tag, struct isci_request *ireq)
3038 struct domain_device *dev = sci_dev_to_domain(sci_dev);
3039 enum sci_status status = SCI_SUCCESS;
3041 /* Build the common part of the request */
3042 scic_sds_general_request_construct(scic, sci_dev, ireq);
3044 if (dev->dev_type == SAS_END_DEV ||
3045 dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
3046 set_bit(IREQ_TMF, &ireq->flags);
3047 memset(ireq->tc, 0, sizeof(struct scu_task_context));
3048 } else
3049 status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
3051 return status;
3054 static enum sci_status isci_request_ssp_request_construct(
3055 struct isci_request *request)
3057 enum sci_status status;
3059 dev_dbg(&request->isci_host->pdev->dev,
3060 "%s: request = %p\n",
3061 __func__,
3062 request);
3063 status = scic_io_request_construct_basic_ssp(request);
3064 return status;
3067 static enum sci_status isci_request_stp_request_construct(
3068 struct isci_request *request)
3070 struct sas_task *task = isci_request_access_task(request);
3071 enum sci_status status;
3072 struct host_to_dev_fis *register_fis;
3074 dev_dbg(&request->isci_host->pdev->dev,
3075 "%s: request = %p\n",
3076 __func__,
3077 request);
3079 /* Get the host_to_dev_fis from the core and copy
3080 * the fis from the task into it.
3082 register_fis = isci_sata_task_to_fis_copy(task);
3084 status = scic_io_request_construct_basic_sata(request);
3086 /* Set the ncq tag in the fis, from the queue
3087 * command in the task.
3089 if (isci_sata_is_task_ncq(task)) {
3091 isci_sata_set_ncq_tag(
3092 register_fis,
3093 task
3097 return status;
3100 static enum sci_status
3101 scic_io_request_construct_smp(struct device *dev,
3102 struct isci_request *ireq,
3103 struct sas_task *task)
3105 struct scatterlist *sg = &task->smp_task.smp_req;
3106 struct scic_sds_remote_device *sci_dev;
3107 struct scu_task_context *task_context;
3108 struct scic_sds_port *sci_port;
3109 struct smp_req *smp_req;
3110 void *kaddr;
3111 u8 req_len;
3112 u32 cmd;
3114 kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
3115 smp_req = kaddr + sg->offset;
3117 * Look at the SMP requests' header fields; for certain SAS 1.x SMP
3118 * functions under SAS 2.0, a zero request length really indicates
3119 * a non-zero default length.
3121 if (smp_req->req_len == 0) {
3122 switch (smp_req->func) {
3123 case SMP_DISCOVER:
3124 case SMP_REPORT_PHY_ERR_LOG:
3125 case SMP_REPORT_PHY_SATA:
3126 case SMP_REPORT_ROUTE_INFO:
3127 smp_req->req_len = 2;
3128 break;
3129 case SMP_CONF_ROUTE_INFO:
3130 case SMP_PHY_CONTROL:
3131 case SMP_PHY_TEST_FUNCTION:
3132 smp_req->req_len = 9;
3133 break;
3134 /* Default - zero is a valid default for 2.0. */
3137 req_len = smp_req->req_len;
3138 sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
3139 cmd = *(u32 *) smp_req;
3140 kunmap_atomic(kaddr, KM_IRQ0);
3142 if (!dma_map_sg(dev, sg, 1, DMA_TO_DEVICE))
3143 return SCI_FAILURE;
3145 ireq->protocol = SCIC_SMP_PROTOCOL;
3147 /* byte swap the smp request. */
3149 task_context = ireq->tc;
3151 sci_dev = scic_sds_request_get_device(ireq);
3152 sci_port = scic_sds_request_get_port(ireq);
3155 * Fill in the TC with the its required data
3156 * 00h
3158 task_context->priority = 0;
3159 task_context->initiator_request = 1;
3160 task_context->connection_rate = sci_dev->connection_rate;
3161 task_context->protocol_engine_index =
3162 scic_sds_controller_get_protocol_engine_group(scic);
3163 task_context->logical_port_index = scic_sds_port_get_index(sci_port);
3164 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
3165 task_context->abort = 0;
3166 task_context->valid = SCU_TASK_CONTEXT_VALID;
3167 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
3169 /* 04h */
3170 task_context->remote_node_index = sci_dev->rnc.remote_node_index;
3171 task_context->command_code = 0;
3172 task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;
3174 /* 08h */
3175 task_context->link_layer_control = 0;
3176 task_context->do_not_dma_ssp_good_response = 1;
3177 task_context->strict_ordering = 0;
3178 task_context->control_frame = 1;
3179 task_context->timeout_enable = 0;
3180 task_context->block_guard_enable = 0;
3182 /* 0ch */
3183 task_context->address_modifier = 0;
3185 /* 10h */
3186 task_context->ssp_command_iu_length = req_len;
3188 /* 14h */
3189 task_context->transfer_length_bytes = 0;
3192 * 18h ~ 30h, protocol specific
3193 * since commandIU has been build by framework at this point, we just
3194 * copy the frist DWord from command IU to this location. */
3195 memcpy(&task_context->type.smp, &cmd, sizeof(u32));
3198 * 40h
3199 * "For SMP you could program it to zero. We would prefer that way
3200 * so that done code will be consistent." - Venki
3202 task_context->task_phase = 0;
3204 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
3205 (scic_sds_controller_get_protocol_engine_group(scic) <<
3206 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
3207 (scic_sds_port_get_index(sci_port) <<
3208 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
3209 ISCI_TAG_TCI(ireq->io_tag));
3211 * Copy the physical address for the command buffer to the SCU Task
3212 * Context command buffer should not contain command header.
3214 task_context->command_iu_upper = upper_32_bits(sg_dma_address(sg));
3215 task_context->command_iu_lower = lower_32_bits(sg_dma_address(sg) + sizeof(u32));
3217 /* SMP response comes as UF, so no need to set response IU address. */
3218 task_context->response_iu_upper = 0;
3219 task_context->response_iu_lower = 0;
3221 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
3223 return SCI_SUCCESS;
3227 * isci_smp_request_build() - This function builds the smp request.
3228 * @ireq: This parameter points to the isci_request allocated in the
3229 * request construct function.
3231 * SCI_SUCCESS on successfull completion, or specific failure code.
3233 static enum sci_status isci_smp_request_build(struct isci_request *ireq)
3235 struct sas_task *task = isci_request_access_task(ireq);
3236 struct device *dev = &ireq->isci_host->pdev->dev;
3237 enum sci_status status = SCI_FAILURE;
3239 status = scic_io_request_construct_smp(dev, ireq, task);
3240 if (status != SCI_SUCCESS)
3241 dev_warn(&ireq->isci_host->pdev->dev,
3242 "%s: failed with status = %d\n",
3243 __func__,
3244 status);
3246 return status;
3250 * isci_io_request_build() - This function builds the io request object.
3251 * @isci_host: This parameter specifies the ISCI host object
3252 * @request: This parameter points to the isci_request object allocated in the
3253 * request construct function.
3254 * @sci_device: This parameter is the handle for the sci core's remote device
3255 * object that is the destination for this request.
3257 * SCI_SUCCESS on successfull completion, or specific failure code.
3259 static enum sci_status isci_io_request_build(struct isci_host *isci_host,
3260 struct isci_request *request,
3261 struct isci_remote_device *isci_device)
3263 enum sci_status status = SCI_SUCCESS;
3264 struct sas_task *task = isci_request_access_task(request);
3265 struct scic_sds_remote_device *sci_device = &isci_device->sci;
3267 dev_dbg(&isci_host->pdev->dev,
3268 "%s: isci_device = 0x%p; request = %p, "
3269 "num_scatter = %d\n",
3270 __func__,
3271 isci_device,
3272 request,
3273 task->num_scatter);
3275 /* map the sgl addresses, if present.
3276 * libata does the mapping for sata devices
3277 * before we get the request.
3279 if (task->num_scatter &&
3280 !sas_protocol_ata(task->task_proto) &&
3281 !(SAS_PROTOCOL_SMP & task->task_proto)) {
3283 request->num_sg_entries = dma_map_sg(
3284 &isci_host->pdev->dev,
3285 task->scatter,
3286 task->num_scatter,
3287 task->data_dir
3290 if (request->num_sg_entries == 0)
3291 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
3294 status = scic_io_request_construct(&isci_host->sci, sci_device,
3295 request);
3297 if (status != SCI_SUCCESS) {
3298 dev_warn(&isci_host->pdev->dev,
3299 "%s: failed request construct\n",
3300 __func__);
3301 return SCI_FAILURE;
3304 switch (task->task_proto) {
3305 case SAS_PROTOCOL_SMP:
3306 status = isci_smp_request_build(request);
3307 break;
3308 case SAS_PROTOCOL_SSP:
3309 status = isci_request_ssp_request_construct(request);
3310 break;
3311 case SAS_PROTOCOL_SATA:
3312 case SAS_PROTOCOL_STP:
3313 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
3314 status = isci_request_stp_request_construct(request);
3315 break;
3316 default:
3317 dev_warn(&isci_host->pdev->dev,
3318 "%s: unknown protocol\n", __func__);
3319 return SCI_FAILURE;
3322 return SCI_SUCCESS;
3325 static struct isci_request *isci_request_from_tag(struct isci_host *ihost, u16 tag)
3327 struct isci_request *ireq;
3329 ireq = ihost->reqs[ISCI_TAG_TCI(tag)];
3330 ireq->io_tag = tag;
3331 ireq->io_request_completion = NULL;
3332 ireq->flags = 0;
3333 ireq->num_sg_entries = 0;
3334 INIT_LIST_HEAD(&ireq->completed_node);
3335 INIT_LIST_HEAD(&ireq->dev_node);
3336 isci_request_change_state(ireq, allocated);
3338 return ireq;
3341 static struct isci_request *isci_io_request_from_tag(struct isci_host *ihost,
3342 struct sas_task *task,
3343 u16 tag)
3345 struct isci_request *ireq;
3347 ireq = isci_request_from_tag(ihost, tag);
3348 ireq->ttype_ptr.io_task_ptr = task;
3349 ireq->ttype = io_task;
3350 task->lldd_task = ireq;
3352 return ireq;
3355 struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
3356 struct isci_tmf *isci_tmf,
3357 u16 tag)
3359 struct isci_request *ireq;
3361 ireq = isci_request_from_tag(ihost, tag);
3362 ireq->ttype_ptr.tmf_task_ptr = isci_tmf;
3363 ireq->ttype = tmf_task;
3365 return ireq;
3368 int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
3369 struct sas_task *task, u16 tag)
3371 enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
3372 struct isci_request *ireq;
3373 unsigned long flags;
3374 int ret = 0;
3376 /* do common allocation and init of request object. */
3377 ireq = isci_io_request_from_tag(ihost, task, tag);
3379 status = isci_io_request_build(ihost, ireq, idev);
3380 if (status != SCI_SUCCESS) {
3381 dev_warn(&ihost->pdev->dev,
3382 "%s: request_construct failed - status = 0x%x\n",
3383 __func__,
3384 status);
3385 return status;
3388 spin_lock_irqsave(&ihost->scic_lock, flags);
3390 if (test_bit(IDEV_IO_NCQERROR, &idev->flags)) {
3392 if (isci_task_is_ncq_recovery(task)) {
3394 /* The device is in an NCQ recovery state. Issue the
3395 * request on the task side. Note that it will
3396 * complete on the I/O request side because the
3397 * request was built that way (ie.
3398 * ireq->is_task_management_request is false).
3400 status = scic_controller_start_task(&ihost->sci,
3401 &idev->sci,
3402 ireq);
3403 } else {
3404 status = SCI_FAILURE;
3406 } else {
3407 /* send the request, let the core assign the IO TAG. */
3408 status = scic_controller_start_io(&ihost->sci, &idev->sci,
3409 ireq);
3412 if (status != SCI_SUCCESS &&
3413 status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
3414 dev_warn(&ihost->pdev->dev,
3415 "%s: failed request start (0x%x)\n",
3416 __func__, status);
3417 spin_unlock_irqrestore(&ihost->scic_lock, flags);
3418 return status;
3421 /* Either I/O started OK, or the core has signaled that
3422 * the device needs a target reset.
3424 * In either case, hold onto the I/O for later.
3426 * Update it's status and add it to the list in the
3427 * remote device object.
3429 list_add(&ireq->dev_node, &idev->reqs_in_process);
3431 if (status == SCI_SUCCESS) {
3432 isci_request_change_state(ireq, started);
3433 } else {
3434 /* The request did not really start in the
3435 * hardware, so clear the request handle
3436 * here so no terminations will be done.
3438 set_bit(IREQ_TERMINATED, &ireq->flags);
3439 isci_request_change_state(ireq, completed);
3441 spin_unlock_irqrestore(&ihost->scic_lock, flags);
3443 if (status ==
3444 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
3445 /* Signal libsas that we need the SCSI error
3446 * handler thread to work on this I/O and that
3447 * we want a device reset.
3449 spin_lock_irqsave(&task->task_state_lock, flags);
3450 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
3451 spin_unlock_irqrestore(&task->task_state_lock, flags);
3453 /* Cause this task to be scheduled in the SCSI error
3454 * handler thread.
3456 isci_execpath_callback(ihost, task,
3457 sas_task_abort);
3459 /* Change the status, since we are holding
3460 * the I/O until it is managed by the SCSI
3461 * error handler.
3463 status = SCI_SUCCESS;
3466 return ret;