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.
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.
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
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
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.
59 #include "scu_completion_codes.h"
60 #include "scu_event_codes.h"
63 static struct scu_sgl_element_pair *to_sgl_element_pair(struct isci_request *ireq,
67 return &ireq->tc->sgl_pair_ab;
69 return &ireq->tc->sgl_pair_cd;
73 return &ireq->sg_table[idx - 2];
76 static dma_addr_t to_sgl_element_pair_dma(struct isci_host *ihost,
77 struct isci_request *ireq, u32 idx)
82 offset = (void *) &ireq->tc->sgl_pair_ab -
83 (void *) &ihost->task_context_table[0];
84 return ihost->task_context_dma + offset;
85 } else if (idx == 1) {
86 offset = (void *) &ireq->tc->sgl_pair_cd -
87 (void *) &ihost->task_context_table[0];
88 return ihost->task_context_dma + offset;
91 return sci_io_request_get_dma_addr(ireq, &ireq->sg_table[idx - 2]);
94 static void init_sgl_element(struct scu_sgl_element *e, struct scatterlist *sg)
96 e->length = sg_dma_len(sg);
97 e->address_upper = upper_32_bits(sg_dma_address(sg));
98 e->address_lower = lower_32_bits(sg_dma_address(sg));
99 e->address_modifier = 0;
102 static void sci_request_build_sgl(struct isci_request *ireq)
104 struct isci_host *ihost = ireq->isci_host;
105 struct sas_task *task = isci_request_access_task(ireq);
106 struct scatterlist *sg = NULL;
109 struct scu_sgl_element_pair *scu_sg = NULL;
110 struct scu_sgl_element_pair *prev_sg = NULL;
112 if (task->num_scatter > 0) {
116 scu_sg = to_sgl_element_pair(ireq, sg_idx);
117 init_sgl_element(&scu_sg->A, sg);
120 init_sgl_element(&scu_sg->B, sg);
123 memset(&scu_sg->B, 0, sizeof(scu_sg->B));
126 dma_addr = to_sgl_element_pair_dma(ihost,
130 prev_sg->next_pair_upper =
131 upper_32_bits(dma_addr);
132 prev_sg->next_pair_lower =
133 lower_32_bits(dma_addr);
139 } else { /* handle when no sg */
140 scu_sg = to_sgl_element_pair(ireq, sg_idx);
142 dma_addr = dma_map_single(&ihost->pdev->dev,
144 task->total_xfer_len,
147 ireq->zero_scatter_daddr = dma_addr;
149 scu_sg->A.length = task->total_xfer_len;
150 scu_sg->A.address_upper = upper_32_bits(dma_addr);
151 scu_sg->A.address_lower = lower_32_bits(dma_addr);
155 scu_sg->next_pair_upper = 0;
156 scu_sg->next_pair_lower = 0;
160 static void sci_io_request_build_ssp_command_iu(struct isci_request *ireq)
162 struct ssp_cmd_iu *cmd_iu;
163 struct sas_task *task = isci_request_access_task(ireq);
165 cmd_iu = &ireq->ssp.cmd;
167 memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
168 cmd_iu->add_cdb_len = 0;
171 cmd_iu->en_fburst = 0; /* unsupported */
172 cmd_iu->task_prio = task->ssp_task.task_prio;
173 cmd_iu->task_attr = task->ssp_task.task_attr;
176 sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cdb,
177 sizeof(task->ssp_task.cdb) / sizeof(u32));
180 static void sci_task_request_build_ssp_task_iu(struct isci_request *ireq)
182 struct ssp_task_iu *task_iu;
183 struct sas_task *task = isci_request_access_task(ireq);
184 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
186 task_iu = &ireq->ssp.tmf;
188 memset(task_iu, 0, sizeof(struct ssp_task_iu));
190 memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
192 task_iu->task_func = isci_tmf->tmf_code;
194 (ireq->ttype == tmf_task) ?
196 SCI_CONTROLLER_INVALID_IO_TAG;
200 * This method is will fill in the SCU Task Context for any type of SSP request.
205 static void scu_ssp_reqeust_construct_task_context(
206 struct isci_request *ireq,
207 struct scu_task_context *task_context)
210 struct isci_remote_device *idev;
211 struct isci_port *iport;
213 idev = ireq->target_device;
214 iport = idev->owning_port;
216 /* Fill in the TC with the its required data */
217 task_context->abort = 0;
218 task_context->priority = 0;
219 task_context->initiator_request = 1;
220 task_context->connection_rate = idev->connection_rate;
221 task_context->protocol_engine_index = ISCI_PEG;
222 task_context->logical_port_index = iport->physical_port_index;
223 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
224 task_context->valid = SCU_TASK_CONTEXT_VALID;
225 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
227 task_context->remote_node_index = idev->rnc.remote_node_index;
228 task_context->command_code = 0;
230 task_context->link_layer_control = 0;
231 task_context->do_not_dma_ssp_good_response = 1;
232 task_context->strict_ordering = 0;
233 task_context->control_frame = 0;
234 task_context->timeout_enable = 0;
235 task_context->block_guard_enable = 0;
237 task_context->address_modifier = 0;
239 /* task_context->type.ssp.tag = ireq->io_tag; */
240 task_context->task_phase = 0x01;
242 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
243 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
244 (iport->physical_port_index <<
245 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
246 ISCI_TAG_TCI(ireq->io_tag));
249 * Copy the physical address for the command buffer to the
252 dma_addr = sci_io_request_get_dma_addr(ireq, &ireq->ssp.cmd);
254 task_context->command_iu_upper = upper_32_bits(dma_addr);
255 task_context->command_iu_lower = lower_32_bits(dma_addr);
258 * Copy the physical address for the response buffer to the
261 dma_addr = sci_io_request_get_dma_addr(ireq, &ireq->ssp.rsp);
263 task_context->response_iu_upper = upper_32_bits(dma_addr);
264 task_context->response_iu_lower = lower_32_bits(dma_addr);
268 * This method is will fill in the SCU Task Context for a SSP IO request.
272 static void scu_ssp_io_request_construct_task_context(struct isci_request *ireq,
273 enum dma_data_direction dir,
276 struct scu_task_context *task_context = ireq->tc;
278 scu_ssp_reqeust_construct_task_context(ireq, task_context);
280 task_context->ssp_command_iu_length =
281 sizeof(struct ssp_cmd_iu) / sizeof(u32);
282 task_context->type.ssp.frame_type = SSP_COMMAND;
285 case DMA_FROM_DEVICE:
288 task_context->task_type = SCU_TASK_TYPE_IOREAD;
291 task_context->task_type = SCU_TASK_TYPE_IOWRITE;
295 task_context->transfer_length_bytes = len;
297 if (task_context->transfer_length_bytes > 0)
298 sci_request_build_sgl(ireq);
302 * This method will fill in the SCU Task Context for a SSP Task request. The
303 * following important settings are utilized: -# priority ==
304 * SCU_TASK_PRIORITY_HIGH. This ensures that the task request is issued
305 * ahead of other task destined for the same Remote Node. -# task_type ==
306 * SCU_TASK_TYPE_IOREAD. This simply indicates that a normal request type
307 * (i.e. non-raw frame) is being utilized to perform task management. -#
308 * control_frame == 1. This ensures that the proper endianess is set so
309 * that the bytes are transmitted in the right order for a task frame.
310 * @sci_req: This parameter specifies the task request object being
314 static void scu_ssp_task_request_construct_task_context(struct isci_request *ireq)
316 struct scu_task_context *task_context = ireq->tc;
318 scu_ssp_reqeust_construct_task_context(ireq, task_context);
320 task_context->control_frame = 1;
321 task_context->priority = SCU_TASK_PRIORITY_HIGH;
322 task_context->task_type = SCU_TASK_TYPE_RAW_FRAME;
323 task_context->transfer_length_bytes = 0;
324 task_context->type.ssp.frame_type = SSP_TASK;
325 task_context->ssp_command_iu_length =
326 sizeof(struct ssp_task_iu) / sizeof(u32);
330 * This method is will fill in the SCU Task Context for any type of SATA
331 * request. This is called from the various SATA constructors.
332 * @sci_req: The general IO request object which is to be used in
333 * constructing the SCU task context.
334 * @task_context: The buffer pointer for the SCU task context which is being
337 * The general io request construction is complete. The buffer assignment for
338 * the command buffer is complete. none Revisit task context construction to
339 * determine what is common for SSP/SMP/STP task context structures.
341 static void scu_sata_reqeust_construct_task_context(
342 struct isci_request *ireq,
343 struct scu_task_context *task_context)
346 struct isci_remote_device *idev;
347 struct isci_port *iport;
349 idev = ireq->target_device;
350 iport = idev->owning_port;
352 /* Fill in the TC with the its required data */
353 task_context->abort = 0;
354 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
355 task_context->initiator_request = 1;
356 task_context->connection_rate = idev->connection_rate;
357 task_context->protocol_engine_index = ISCI_PEG;
358 task_context->logical_port_index = iport->physical_port_index;
359 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
360 task_context->valid = SCU_TASK_CONTEXT_VALID;
361 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
363 task_context->remote_node_index = idev->rnc.remote_node_index;
364 task_context->command_code = 0;
366 task_context->link_layer_control = 0;
367 task_context->do_not_dma_ssp_good_response = 1;
368 task_context->strict_ordering = 0;
369 task_context->control_frame = 0;
370 task_context->timeout_enable = 0;
371 task_context->block_guard_enable = 0;
373 task_context->address_modifier = 0;
374 task_context->task_phase = 0x01;
376 task_context->ssp_command_iu_length =
377 (sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);
379 /* Set the first word of the H2D REG FIS */
380 task_context->type.words[0] = *(u32 *)&ireq->stp.cmd;
382 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
383 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
384 (iport->physical_port_index <<
385 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
386 ISCI_TAG_TCI(ireq->io_tag));
388 * Copy the physical address for the command buffer to the SCU Task
389 * Context. We must offset the command buffer by 4 bytes because the
390 * first 4 bytes are transfered in the body of the TC.
392 dma_addr = sci_io_request_get_dma_addr(ireq,
393 ((char *) &ireq->stp.cmd) +
396 task_context->command_iu_upper = upper_32_bits(dma_addr);
397 task_context->command_iu_lower = lower_32_bits(dma_addr);
399 /* SATA Requests do not have a response buffer */
400 task_context->response_iu_upper = 0;
401 task_context->response_iu_lower = 0;
404 static void scu_stp_raw_request_construct_task_context(struct isci_request *ireq)
406 struct scu_task_context *task_context = ireq->tc;
408 scu_sata_reqeust_construct_task_context(ireq, task_context);
410 task_context->control_frame = 0;
411 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
412 task_context->task_type = SCU_TASK_TYPE_SATA_RAW_FRAME;
413 task_context->type.stp.fis_type = FIS_REGH2D;
414 task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
417 static enum sci_status sci_stp_pio_request_construct(struct isci_request *ireq,
420 struct isci_stp_request *stp_req = &ireq->stp.req;
422 scu_stp_raw_request_construct_task_context(ireq);
425 stp_req->sgl.offset = 0;
426 stp_req->sgl.set = SCU_SGL_ELEMENT_PAIR_A;
429 sci_request_build_sgl(ireq);
430 stp_req->sgl.index = 0;
432 /* The user does not want the data copied to the SGL buffer location */
433 stp_req->sgl.index = -1;
441 * @sci_req: This parameter specifies the request to be constructed as an
443 * @optimized_task_type: This parameter specifies whether the request is to be
444 * an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
445 * value of 1 indicates NCQ.
447 * This method will perform request construction common to all types of STP
448 * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
449 * returns an indication as to whether the construction was successful.
451 static void sci_stp_optimized_request_construct(struct isci_request *ireq,
452 u8 optimized_task_type,
454 enum dma_data_direction dir)
456 struct scu_task_context *task_context = ireq->tc;
458 /* Build the STP task context structure */
459 scu_sata_reqeust_construct_task_context(ireq, task_context);
461 /* Copy over the SGL elements */
462 sci_request_build_sgl(ireq);
464 /* Copy over the number of bytes to be transfered */
465 task_context->transfer_length_bytes = len;
467 if (dir == DMA_TO_DEVICE) {
469 * The difference between the DMA IN and DMA OUT request task type
470 * values are consistent with the difference between FPDMA READ
471 * and FPDMA WRITE values. Add the supplied task type parameter
472 * to this difference to set the task type properly for this
473 * DATA OUT (WRITE) case. */
474 task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
475 - SCU_TASK_TYPE_DMA_IN);
478 * For the DATA IN (READ) case, simply save the supplied
479 * optimized task type. */
480 task_context->task_type = optimized_task_type;
484 static void sci_atapi_construct(struct isci_request *ireq)
486 struct host_to_dev_fis *h2d_fis = &ireq->stp.cmd;
487 struct sas_task *task;
489 /* To simplify the implementation we take advantage of the
490 * silicon's partial acceleration of atapi protocol (dma data
491 * transfers), so we promote all commands to dma protocol. This
492 * breaks compatibility with ATA_HORKAGE_ATAPI_MOD16_DMA drives.
494 h2d_fis->features |= ATAPI_PKT_DMA;
496 scu_stp_raw_request_construct_task_context(ireq);
498 task = isci_request_access_task(ireq);
499 if (task->data_dir == DMA_NONE)
500 task->total_xfer_len = 0;
502 /* clear the response so we can detect arrivial of an
503 * unsolicited h2d fis
505 ireq->stp.rsp.fis_type = 0;
508 static enum sci_status
509 sci_io_request_construct_sata(struct isci_request *ireq,
511 enum dma_data_direction dir,
514 enum sci_status status = SCI_SUCCESS;
515 struct sas_task *task = isci_request_access_task(ireq);
516 struct domain_device *dev = ireq->target_device->domain_dev;
518 /* check for management protocols */
519 if (ireq->ttype == tmf_task) {
520 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
522 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
523 tmf->tmf_code == isci_tmf_sata_srst_low) {
524 scu_stp_raw_request_construct_task_context(ireq);
527 dev_err(&ireq->owning_controller->pdev->dev,
528 "%s: Request 0x%p received un-handled SAT "
529 "management protocol 0x%x.\n",
530 __func__, ireq, tmf->tmf_code);
536 if (!sas_protocol_ata(task->task_proto)) {
537 dev_err(&ireq->owning_controller->pdev->dev,
538 "%s: Non-ATA protocol in SATA path: 0x%x\n",
546 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET &&
547 task->ata_task.fis.command == ATA_CMD_PACKET) {
548 sci_atapi_construct(ireq);
553 if (task->data_dir == DMA_NONE) {
554 scu_stp_raw_request_construct_task_context(ireq);
559 if (task->ata_task.use_ncq) {
560 sci_stp_optimized_request_construct(ireq,
561 SCU_TASK_TYPE_FPDMAQ_READ,
567 if (task->ata_task.dma_xfer) {
568 sci_stp_optimized_request_construct(ireq,
569 SCU_TASK_TYPE_DMA_IN,
573 return sci_stp_pio_request_construct(ireq, copy);
578 static enum sci_status sci_io_request_construct_basic_ssp(struct isci_request *ireq)
580 struct sas_task *task = isci_request_access_task(ireq);
582 ireq->protocol = SCIC_SSP_PROTOCOL;
584 scu_ssp_io_request_construct_task_context(ireq,
586 task->total_xfer_len);
588 sci_io_request_build_ssp_command_iu(ireq);
590 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
595 enum sci_status sci_task_request_construct_ssp(
596 struct isci_request *ireq)
598 /* Construct the SSP Task SCU Task Context */
599 scu_ssp_task_request_construct_task_context(ireq);
601 /* Fill in the SSP Task IU */
602 sci_task_request_build_ssp_task_iu(ireq);
604 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
609 static enum sci_status sci_io_request_construct_basic_sata(struct isci_request *ireq)
611 enum sci_status status;
613 struct sas_task *task = isci_request_access_task(ireq);
615 ireq->protocol = SCIC_STP_PROTOCOL;
617 copy = (task->data_dir == DMA_NONE) ? false : true;
619 status = sci_io_request_construct_sata(ireq,
620 task->total_xfer_len,
624 if (status == SCI_SUCCESS)
625 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
630 enum sci_status sci_task_request_construct_sata(struct isci_request *ireq)
632 enum sci_status status = SCI_SUCCESS;
634 /* check for management protocols */
635 if (ireq->ttype == tmf_task) {
636 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
638 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
639 tmf->tmf_code == isci_tmf_sata_srst_low) {
640 scu_stp_raw_request_construct_task_context(ireq);
642 dev_err(&ireq->owning_controller->pdev->dev,
643 "%s: Request 0x%p received un-handled SAT "
645 __func__, ireq, tmf->tmf_code);
651 if (status != SCI_SUCCESS)
653 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
659 * sci_req_tx_bytes - bytes transferred when reply underruns request
660 * @ireq: request that was terminated early
662 #define SCU_TASK_CONTEXT_SRAM 0x200000
663 static u32 sci_req_tx_bytes(struct isci_request *ireq)
665 struct isci_host *ihost = ireq->owning_controller;
668 if (readl(&ihost->smu_registers->address_modifier) == 0) {
669 void __iomem *scu_reg_base = ihost->scu_registers;
671 /* get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
672 * BAR1 is the scu_registers
673 * 0x20002C = 0x200000 + 0x2c
674 * = start of task context SRAM + offset of (type.ssp.data_offset)
675 * TCi is the io_tag of struct sci_request
677 ret_val = readl(scu_reg_base +
678 (SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
679 ((sizeof(struct scu_task_context)) * ISCI_TAG_TCI(ireq->io_tag)));
685 enum sci_status sci_request_start(struct isci_request *ireq)
687 enum sci_base_request_states state;
688 struct scu_task_context *tc = ireq->tc;
689 struct isci_host *ihost = ireq->owning_controller;
691 state = ireq->sm.current_state_id;
692 if (state != SCI_REQ_CONSTRUCTED) {
693 dev_warn(&ihost->pdev->dev,
694 "%s: SCIC IO Request requested to start while in wrong "
695 "state %d\n", __func__, state);
696 return SCI_FAILURE_INVALID_STATE;
699 tc->task_index = ISCI_TAG_TCI(ireq->io_tag);
701 switch (tc->protocol_type) {
702 case SCU_TASK_CONTEXT_PROTOCOL_SMP:
703 case SCU_TASK_CONTEXT_PROTOCOL_SSP:
705 tc->type.ssp.tag = ireq->io_tag;
706 tc->type.ssp.target_port_transfer_tag = 0xFFFF;
709 case SCU_TASK_CONTEXT_PROTOCOL_STP:
711 * tc->type.stp.ncq_tag = ireq->ncq_tag;
715 case SCU_TASK_CONTEXT_PROTOCOL_NONE:
716 /* / @todo When do we set no protocol type? */
720 /* This should never happen since we build the IO
725 /* Add to the post_context the io tag value */
726 ireq->post_context |= ISCI_TAG_TCI(ireq->io_tag);
728 /* Everything is good go ahead and change state */
729 sci_change_state(&ireq->sm, SCI_REQ_STARTED);
735 sci_io_request_terminate(struct isci_request *ireq)
737 enum sci_base_request_states state;
739 state = ireq->sm.current_state_id;
742 case SCI_REQ_CONSTRUCTED:
743 ireq->scu_status = SCU_TASK_DONE_TASK_ABORT;
744 ireq->sci_status = SCI_FAILURE_IO_TERMINATED;
745 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
747 case SCI_REQ_STARTED:
748 case SCI_REQ_TASK_WAIT_TC_COMP:
749 case SCI_REQ_SMP_WAIT_RESP:
750 case SCI_REQ_SMP_WAIT_TC_COMP:
751 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
752 case SCI_REQ_STP_UDMA_WAIT_D2H:
753 case SCI_REQ_STP_NON_DATA_WAIT_H2D:
754 case SCI_REQ_STP_NON_DATA_WAIT_D2H:
755 case SCI_REQ_STP_PIO_WAIT_H2D:
756 case SCI_REQ_STP_PIO_WAIT_FRAME:
757 case SCI_REQ_STP_PIO_DATA_IN:
758 case SCI_REQ_STP_PIO_DATA_OUT:
759 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
760 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
761 case SCI_REQ_STP_SOFT_RESET_WAIT_D2H:
762 case SCI_REQ_ATAPI_WAIT_H2D:
763 case SCI_REQ_ATAPI_WAIT_PIO_SETUP:
764 case SCI_REQ_ATAPI_WAIT_D2H:
765 case SCI_REQ_ATAPI_WAIT_TC_COMP:
766 sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
768 case SCI_REQ_TASK_WAIT_TC_RESP:
769 /* The task frame was already confirmed to have been
770 * sent by the SCU HW. Since the state machine is
771 * now only waiting for the task response itself,
772 * abort the request and complete it immediately
773 * and don't wait for the task response.
775 sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
776 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
778 case SCI_REQ_ABORTING:
779 /* If a request has a termination requested twice, return
780 * a failure indication, since HW confirmation of the first
781 * abort is still outstanding.
783 case SCI_REQ_COMPLETED:
785 dev_warn(&ireq->owning_controller->pdev->dev,
786 "%s: SCIC IO Request requested to abort while in wrong "
789 ireq->sm.current_state_id);
793 return SCI_FAILURE_INVALID_STATE;
796 enum sci_status sci_request_complete(struct isci_request *ireq)
798 enum sci_base_request_states state;
799 struct isci_host *ihost = ireq->owning_controller;
801 state = ireq->sm.current_state_id;
802 if (WARN_ONCE(state != SCI_REQ_COMPLETED,
803 "isci: request completion from wrong state (%d)\n", state))
804 return SCI_FAILURE_INVALID_STATE;
806 if (ireq->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX)
807 sci_controller_release_frame(ihost,
808 ireq->saved_rx_frame_index);
810 /* XXX can we just stop the machine and remove the 'final' state? */
811 sci_change_state(&ireq->sm, SCI_REQ_FINAL);
815 enum sci_status sci_io_request_event_handler(struct isci_request *ireq,
818 enum sci_base_request_states state;
819 struct isci_host *ihost = ireq->owning_controller;
821 state = ireq->sm.current_state_id;
823 if (state != SCI_REQ_STP_PIO_DATA_IN) {
824 dev_warn(&ihost->pdev->dev, "%s: (%x) in wrong state %d\n",
825 __func__, event_code, state);
827 return SCI_FAILURE_INVALID_STATE;
830 switch (scu_get_event_specifier(event_code)) {
831 case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
832 /* We are waiting for data and the SCU has R_ERR the data frame.
833 * Go back to waiting for the D2H Register FIS
835 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
838 dev_err(&ihost->pdev->dev,
839 "%s: pio request unexpected event %#x\n",
840 __func__, event_code);
842 /* TODO Should we fail the PIO request when we get an
850 * This function copies response data for requests returning response data
851 * instead of sense data.
852 * @sci_req: This parameter specifies the request object for which to copy
855 static void sci_io_request_copy_response(struct isci_request *ireq)
859 struct ssp_response_iu *ssp_response;
860 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
862 ssp_response = &ireq->ssp.rsp;
864 resp_buf = &isci_tmf->resp.resp_iu;
867 SSP_RESP_IU_MAX_SIZE,
868 be32_to_cpu(ssp_response->response_data_len));
870 memcpy(resp_buf, ssp_response->resp_data, len);
873 static enum sci_status
874 request_started_state_tc_event(struct isci_request *ireq,
877 struct ssp_response_iu *resp_iu;
880 /* TODO: Any SDMA return code of other than 0 is bad decode 0x003C0000
881 * to determine SDMA status
883 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
884 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
885 ireq->scu_status = SCU_TASK_DONE_GOOD;
886 ireq->sci_status = SCI_SUCCESS;
888 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP): {
889 /* There are times when the SCU hardware will return an early
890 * response because the io request specified more data than is
891 * returned by the target device (mode pages, inquiry data,
892 * etc.). We must check the response stats to see if this is
893 * truly a failed request or a good request that just got
896 struct ssp_response_iu *resp = &ireq->ssp.rsp;
897 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
899 sci_swab32_cpy(&ireq->ssp.rsp,
903 if (resp->status == 0) {
904 ireq->scu_status = SCU_TASK_DONE_GOOD;
905 ireq->sci_status = SCI_SUCCESS_IO_DONE_EARLY;
907 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
908 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
912 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE): {
913 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
915 sci_swab32_cpy(&ireq->ssp.rsp,
919 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
920 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
924 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
925 /* TODO With TASK_DONE_RESP_LEN_ERR is the response frame
926 * guaranteed to be received before this completion status is
929 resp_iu = &ireq->ssp.rsp;
930 datapres = resp_iu->datapres;
932 if (datapres == 1 || datapres == 2) {
933 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
934 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
936 ireq->scu_status = SCU_TASK_DONE_GOOD;
937 ireq->sci_status = SCI_SUCCESS;
940 /* only stp device gets suspended. */
941 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
942 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
943 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
944 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
945 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
946 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
947 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
948 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
949 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
950 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
951 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
952 if (ireq->protocol == SCIC_STP_PROTOCOL) {
953 ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
954 SCU_COMPLETION_TL_STATUS_SHIFT;
955 ireq->sci_status = SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
957 ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
958 SCU_COMPLETION_TL_STATUS_SHIFT;
959 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
963 /* both stp/ssp device gets suspended */
964 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
965 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
966 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
967 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
968 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
969 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
970 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
971 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
972 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
973 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
974 ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
975 SCU_COMPLETION_TL_STATUS_SHIFT;
976 ireq->sci_status = SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
979 /* neither ssp nor stp gets suspended. */
980 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
981 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
982 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
983 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
984 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
985 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
986 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
987 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
988 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
989 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
990 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
991 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
992 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
993 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
994 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
996 ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
997 SCU_COMPLETION_TL_STATUS_SHIFT;
998 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1003 * TODO: This is probably wrong for ACK/NAK timeout conditions
1006 /* In all cases we will treat this as the completion of the IO req. */
1007 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1011 static enum sci_status
1012 request_aborting_state_tc_event(struct isci_request *ireq,
1013 u32 completion_code)
1015 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1016 case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
1017 case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
1018 ireq->scu_status = SCU_TASK_DONE_TASK_ABORT;
1019 ireq->sci_status = SCI_FAILURE_IO_TERMINATED;
1020 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1024 /* Unless we get some strange error wait for the task abort to complete
1025 * TODO: Should there be a state change for this completion?
1033 static enum sci_status ssp_task_request_await_tc_event(struct isci_request *ireq,
1034 u32 completion_code)
1036 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1037 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1038 ireq->scu_status = SCU_TASK_DONE_GOOD;
1039 ireq->sci_status = SCI_SUCCESS;
1040 sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
1042 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
1043 /* Currently, the decision is to simply allow the task request
1044 * to timeout if the task IU wasn't received successfully.
1045 * There is a potential for receiving multiple task responses if
1046 * we decide to send the task IU again.
1048 dev_warn(&ireq->owning_controller->pdev->dev,
1049 "%s: TaskRequest:0x%p CompletionCode:%x - "
1050 "ACK/NAK timeout\n", __func__, ireq,
1053 sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
1057 * All other completion status cause the IO to be complete.
1058 * If a NAK was received, then it is up to the user to retry
1061 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1062 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1063 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1070 static enum sci_status
1071 smp_request_await_response_tc_event(struct isci_request *ireq,
1072 u32 completion_code)
1074 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1075 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1076 /* In the AWAIT RESPONSE state, any TC completion is
1077 * unexpected. but if the TC has success status, we
1078 * complete the IO anyway.
1080 ireq->scu_status = SCU_TASK_DONE_GOOD;
1081 ireq->sci_status = SCI_SUCCESS;
1082 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1084 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1085 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1086 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1087 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
1088 /* These status has been seen in a specific LSI
1089 * expander, which sometimes is not able to send smp
1090 * response within 2 ms. This causes our hardware break
1091 * the connection and set TC completion with one of
1092 * these SMP_XXX_XX_ERR status. For these type of error,
1093 * we ask ihost user to retry the request.
1095 ireq->scu_status = SCU_TASK_DONE_SMP_RESP_TO_ERR;
1096 ireq->sci_status = SCI_FAILURE_RETRY_REQUIRED;
1097 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1100 /* All other completion status cause the IO to be complete. If a NAK
1101 * was received, then it is up to the user to retry the request
1103 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1104 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1105 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1112 static enum sci_status
1113 smp_request_await_tc_event(struct isci_request *ireq,
1114 u32 completion_code)
1116 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1117 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1118 ireq->scu_status = SCU_TASK_DONE_GOOD;
1119 ireq->sci_status = SCI_SUCCESS;
1120 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1123 /* All other completion status cause the IO to be
1124 * complete. If a NAK was received, then it is up to
1125 * the user to retry the request.
1127 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1128 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1129 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1136 static struct scu_sgl_element *pio_sgl_next(struct isci_stp_request *stp_req)
1138 struct scu_sgl_element *sgl;
1139 struct scu_sgl_element_pair *sgl_pair;
1140 struct isci_request *ireq = to_ireq(stp_req);
1141 struct isci_stp_pio_sgl *pio_sgl = &stp_req->sgl;
1143 sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
1146 else if (pio_sgl->set == SCU_SGL_ELEMENT_PAIR_A) {
1147 if (sgl_pair->B.address_lower == 0 &&
1148 sgl_pair->B.address_upper == 0) {
1151 pio_sgl->set = SCU_SGL_ELEMENT_PAIR_B;
1155 if (sgl_pair->next_pair_lower == 0 &&
1156 sgl_pair->next_pair_upper == 0) {
1160 pio_sgl->set = SCU_SGL_ELEMENT_PAIR_A;
1161 sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
1169 static enum sci_status
1170 stp_request_non_data_await_h2d_tc_event(struct isci_request *ireq,
1171 u32 completion_code)
1173 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1174 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1175 ireq->scu_status = SCU_TASK_DONE_GOOD;
1176 ireq->sci_status = SCI_SUCCESS;
1177 sci_change_state(&ireq->sm, SCI_REQ_STP_NON_DATA_WAIT_D2H);
1181 /* All other completion status cause the IO to be
1182 * complete. If a NAK was received, then it is up to
1183 * the user to retry the request.
1185 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1186 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1187 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1194 #define SCU_MAX_FRAME_BUFFER_SIZE 0x400 /* 1K is the maximum SCU frame data payload */
1196 /* transmit DATA_FIS from (current sgl + offset) for input
1197 * parameter length. current sgl and offset is alreay stored in the IO request
1199 static enum sci_status sci_stp_request_pio_data_out_trasmit_data_frame(
1200 struct isci_request *ireq,
1203 struct isci_stp_request *stp_req = &ireq->stp.req;
1204 struct scu_task_context *task_context = ireq->tc;
1205 struct scu_sgl_element_pair *sgl_pair;
1206 struct scu_sgl_element *current_sgl;
1208 /* Recycle the TC and reconstruct it for sending out DATA FIS containing
1209 * for the data from current_sgl+offset for the input length
1211 sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
1212 if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A)
1213 current_sgl = &sgl_pair->A;
1215 current_sgl = &sgl_pair->B;
1218 task_context->command_iu_upper = current_sgl->address_upper;
1219 task_context->command_iu_lower = current_sgl->address_lower;
1220 task_context->transfer_length_bytes = length;
1221 task_context->type.stp.fis_type = FIS_DATA;
1223 /* send the new TC out. */
1224 return sci_controller_continue_io(ireq);
1227 static enum sci_status sci_stp_request_pio_data_out_transmit_data(struct isci_request *ireq)
1229 struct isci_stp_request *stp_req = &ireq->stp.req;
1230 struct scu_sgl_element_pair *sgl_pair;
1231 enum sci_status status = SCI_SUCCESS;
1232 struct scu_sgl_element *sgl;
1236 offset = stp_req->sgl.offset;
1237 sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
1238 if (WARN_ONCE(!sgl_pair, "%s: null sgl element", __func__))
1241 if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A) {
1243 len = sgl_pair->A.length - offset;
1246 len = sgl_pair->B.length - offset;
1249 if (stp_req->pio_len == 0)
1252 if (stp_req->pio_len >= len) {
1253 status = sci_stp_request_pio_data_out_trasmit_data_frame(ireq, len);
1254 if (status != SCI_SUCCESS)
1256 stp_req->pio_len -= len;
1258 /* update the current sgl, offset and save for future */
1259 sgl = pio_sgl_next(stp_req);
1261 } else if (stp_req->pio_len < len) {
1262 sci_stp_request_pio_data_out_trasmit_data_frame(ireq, stp_req->pio_len);
1264 /* Sgl offset will be adjusted and saved for future */
1265 offset += stp_req->pio_len;
1266 sgl->address_lower += stp_req->pio_len;
1267 stp_req->pio_len = 0;
1270 stp_req->sgl.offset = offset;
1277 * @stp_request: The request that is used for the SGL processing.
1278 * @data_buffer: The buffer of data to be copied.
1279 * @length: The length of the data transfer.
1281 * Copy the data from the buffer for the length specified to the IO reqeust SGL
1282 * specified data region. enum sci_status
1284 static enum sci_status
1285 sci_stp_request_pio_data_in_copy_data_buffer(struct isci_stp_request *stp_req,
1286 u8 *data_buf, u32 len)
1288 struct isci_request *ireq;
1291 struct sas_task *task;
1292 struct scatterlist *sg;
1294 int total_len = len;
1296 ireq = to_ireq(stp_req);
1297 task = isci_request_access_task(ireq);
1298 src_addr = data_buf;
1300 if (task->num_scatter > 0) {
1303 while (total_len > 0) {
1304 struct page *page = sg_page(sg);
1306 copy_len = min_t(int, total_len, sg_dma_len(sg));
1307 kaddr = kmap_atomic(page, KM_IRQ0);
1308 memcpy(kaddr + sg->offset, src_addr, copy_len);
1309 kunmap_atomic(kaddr, KM_IRQ0);
1310 total_len -= copy_len;
1311 src_addr += copy_len;
1315 BUG_ON(task->total_xfer_len < total_len);
1316 memcpy(task->scatter, src_addr, total_len);
1324 * @sci_req: The PIO DATA IN request that is to receive the data.
1325 * @data_buffer: The buffer to copy from.
1327 * Copy the data buffer to the io request data region. enum sci_status
1329 static enum sci_status sci_stp_request_pio_data_in_copy_data(
1330 struct isci_stp_request *stp_req,
1333 enum sci_status status;
1336 * If there is less than 1K remaining in the transfer request
1337 * copy just the data for the transfer */
1338 if (stp_req->pio_len < SCU_MAX_FRAME_BUFFER_SIZE) {
1339 status = sci_stp_request_pio_data_in_copy_data_buffer(
1340 stp_req, data_buffer, stp_req->pio_len);
1342 if (status == SCI_SUCCESS)
1343 stp_req->pio_len = 0;
1345 /* We are transfering the whole frame so copy */
1346 status = sci_stp_request_pio_data_in_copy_data_buffer(
1347 stp_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
1349 if (status == SCI_SUCCESS)
1350 stp_req->pio_len -= SCU_MAX_FRAME_BUFFER_SIZE;
1356 static enum sci_status
1357 stp_request_pio_await_h2d_completion_tc_event(struct isci_request *ireq,
1358 u32 completion_code)
1360 enum sci_status status = SCI_SUCCESS;
1362 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1363 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1364 ireq->scu_status = SCU_TASK_DONE_GOOD;
1365 ireq->sci_status = SCI_SUCCESS;
1366 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
1370 /* All other completion status cause the IO to be
1371 * complete. If a NAK was received, then it is up to
1372 * the user to retry the request.
1374 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1375 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1376 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1383 static enum sci_status
1384 pio_data_out_tx_done_tc_event(struct isci_request *ireq,
1385 u32 completion_code)
1387 enum sci_status status = SCI_SUCCESS;
1388 bool all_frames_transferred = false;
1389 struct isci_stp_request *stp_req = &ireq->stp.req;
1391 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1392 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1394 if (stp_req->pio_len != 0) {
1395 status = sci_stp_request_pio_data_out_transmit_data(ireq);
1396 if (status == SCI_SUCCESS) {
1397 if (stp_req->pio_len == 0)
1398 all_frames_transferred = true;
1400 } else if (stp_req->pio_len == 0) {
1402 * this will happen if the all data is written at the
1403 * first time after the pio setup fis is received
1405 all_frames_transferred = true;
1408 /* all data transferred. */
1409 if (all_frames_transferred) {
1411 * Change the state to SCI_REQ_STP_PIO_DATA_IN
1412 * and wait for PIO_SETUP fis / or D2H REg fis. */
1413 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
1419 * All other completion status cause the IO to be complete.
1420 * If a NAK was received, then it is up to the user to retry
1423 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1424 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1425 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1432 static enum sci_status sci_stp_request_udma_general_frame_handler(struct isci_request *ireq,
1435 struct isci_host *ihost = ireq->owning_controller;
1436 struct dev_to_host_fis *frame_header;
1437 enum sci_status status;
1440 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1442 (void **)&frame_header);
1444 if ((status == SCI_SUCCESS) &&
1445 (frame_header->fis_type == FIS_REGD2H)) {
1446 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1448 (void **)&frame_buffer);
1450 sci_controller_copy_sata_response(&ireq->stp.rsp,
1455 sci_controller_release_frame(ihost, frame_index);
1460 static enum sci_status process_unsolicited_fis(struct isci_request *ireq,
1463 struct isci_host *ihost = ireq->owning_controller;
1464 enum sci_status status;
1465 struct dev_to_host_fis *frame_header;
1468 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1470 (void **)&frame_header);
1472 if (status != SCI_SUCCESS)
1475 if (frame_header->fis_type != FIS_REGD2H) {
1476 dev_err(&ireq->isci_host->pdev->dev,
1477 "%s ERROR: invalid fis type 0x%X\n",
1478 __func__, frame_header->fis_type);
1482 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1484 (void **)&frame_buffer);
1486 sci_controller_copy_sata_response(&ireq->stp.rsp,
1487 (u32 *)frame_header,
1490 /* Frame has been decoded return it to the controller */
1491 sci_controller_release_frame(ihost, frame_index);
1496 static enum sci_status atapi_d2h_reg_frame_handler(struct isci_request *ireq,
1499 struct sas_task *task = isci_request_access_task(ireq);
1500 enum sci_status status;
1502 status = process_unsolicited_fis(ireq, frame_index);
1504 if (status == SCI_SUCCESS) {
1505 if (ireq->stp.rsp.status & ATA_ERR)
1506 status = SCI_IO_FAILURE_RESPONSE_VALID;
1508 status = SCI_IO_FAILURE_RESPONSE_VALID;
1511 if (status != SCI_SUCCESS) {
1512 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1513 ireq->sci_status = status;
1515 ireq->scu_status = SCU_TASK_DONE_GOOD;
1516 ireq->sci_status = SCI_SUCCESS;
1519 /* the d2h ufi is the end of non-data commands */
1520 if (task->data_dir == DMA_NONE)
1521 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1526 static void scu_atapi_reconstruct_raw_frame_task_context(struct isci_request *ireq)
1528 struct ata_device *dev = sas_to_ata_dev(ireq->target_device->domain_dev);
1529 void *atapi_cdb = ireq->ttype_ptr.io_task_ptr->ata_task.atapi_packet;
1530 struct scu_task_context *task_context = ireq->tc;
1532 /* fill in the SCU Task Context for a DATA fis containing CDB in Raw Frame
1533 * type. The TC for previous Packet fis was already there, we only need to
1534 * change the H2D fis content.
1536 memset(&ireq->stp.cmd, 0, sizeof(struct host_to_dev_fis));
1537 memcpy(((u8 *)&ireq->stp.cmd + sizeof(u32)), atapi_cdb, ATAPI_CDB_LEN);
1538 memset(&(task_context->type.stp), 0, sizeof(struct stp_task_context));
1539 task_context->type.stp.fis_type = FIS_DATA;
1540 task_context->transfer_length_bytes = dev->cdb_len;
1543 static void scu_atapi_construct_task_context(struct isci_request *ireq)
1545 struct ata_device *dev = sas_to_ata_dev(ireq->target_device->domain_dev);
1546 struct sas_task *task = isci_request_access_task(ireq);
1547 struct scu_task_context *task_context = ireq->tc;
1548 int cdb_len = dev->cdb_len;
1550 /* reference: SSTL 1.13.4.2
1551 * task_type, sata_direction
1553 if (task->data_dir == DMA_TO_DEVICE) {
1554 task_context->task_type = SCU_TASK_TYPE_PACKET_DMA_OUT;
1555 task_context->sata_direction = 0;
1557 /* todo: for NO_DATA command, we need to send out raw frame. */
1558 task_context->task_type = SCU_TASK_TYPE_PACKET_DMA_IN;
1559 task_context->sata_direction = 1;
1562 memset(&task_context->type.stp, 0, sizeof(task_context->type.stp));
1563 task_context->type.stp.fis_type = FIS_DATA;
1565 memset(&ireq->stp.cmd, 0, sizeof(ireq->stp.cmd));
1566 memcpy(&ireq->stp.cmd.lbal, task->ata_task.atapi_packet, cdb_len);
1567 task_context->ssp_command_iu_length = cdb_len / sizeof(u32);
1569 /* task phase is set to TX_CMD */
1570 task_context->task_phase = 0x1;
1573 task_context->stp_retry_count = 0;
1575 /* data transfer size. */
1576 task_context->transfer_length_bytes = task->total_xfer_len;
1579 sci_request_build_sgl(ireq);
1583 sci_io_request_frame_handler(struct isci_request *ireq,
1586 struct isci_host *ihost = ireq->owning_controller;
1587 struct isci_stp_request *stp_req = &ireq->stp.req;
1588 enum sci_base_request_states state;
1589 enum sci_status status;
1592 state = ireq->sm.current_state_id;
1594 case SCI_REQ_STARTED: {
1595 struct ssp_frame_hdr ssp_hdr;
1598 sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1602 word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
1603 sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);
1605 if (ssp_hdr.frame_type == SSP_RESPONSE) {
1606 struct ssp_response_iu *resp_iu;
1607 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1609 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1613 sci_swab32_cpy(&ireq->ssp.rsp, resp_iu, word_cnt);
1615 resp_iu = &ireq->ssp.rsp;
1617 if (resp_iu->datapres == 0x01 ||
1618 resp_iu->datapres == 0x02) {
1619 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1620 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1622 ireq->scu_status = SCU_TASK_DONE_GOOD;
1623 ireq->sci_status = SCI_SUCCESS;
1626 /* not a response frame, why did it get forwarded? */
1627 dev_err(&ihost->pdev->dev,
1628 "%s: SCIC IO Request 0x%p received unexpected "
1629 "frame %d type 0x%02x\n", __func__, ireq,
1630 frame_index, ssp_hdr.frame_type);
1634 * In any case we are done with this frame buffer return it to
1637 sci_controller_release_frame(ihost, frame_index);
1642 case SCI_REQ_TASK_WAIT_TC_RESP:
1643 sci_io_request_copy_response(ireq);
1644 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1645 sci_controller_release_frame(ihost, frame_index);
1648 case SCI_REQ_SMP_WAIT_RESP: {
1649 struct sas_task *task = isci_request_access_task(ireq);
1650 struct scatterlist *sg = &task->smp_task.smp_resp;
1651 void *frame_header, *kaddr;
1654 sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1657 kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
1658 rsp = kaddr + sg->offset;
1659 sci_swab32_cpy(rsp, frame_header, 1);
1661 if (rsp[0] == SMP_RESPONSE) {
1664 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1668 word_cnt = (sg->length/4)-1;
1670 word_cnt = min_t(unsigned int, word_cnt,
1671 SCU_UNSOLICITED_FRAME_BUFFER_SIZE/4);
1672 sci_swab32_cpy(rsp + 4, smp_resp, word_cnt);
1674 ireq->scu_status = SCU_TASK_DONE_GOOD;
1675 ireq->sci_status = SCI_SUCCESS;
1676 sci_change_state(&ireq->sm, SCI_REQ_SMP_WAIT_TC_COMP);
1679 * This was not a response frame why did it get
1682 dev_err(&ihost->pdev->dev,
1683 "%s: SCIC SMP Request 0x%p received unexpected "
1684 "frame %d type 0x%02x\n",
1690 ireq->scu_status = SCU_TASK_DONE_SMP_FRM_TYPE_ERR;
1691 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1692 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1694 kunmap_atomic(kaddr, KM_IRQ0);
1696 sci_controller_release_frame(ihost, frame_index);
1701 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
1702 return sci_stp_request_udma_general_frame_handler(ireq,
1705 case SCI_REQ_STP_UDMA_WAIT_D2H:
1706 /* Use the general frame handler to copy the resposne data */
1707 status = sci_stp_request_udma_general_frame_handler(ireq, frame_index);
1709 if (status != SCI_SUCCESS)
1712 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1713 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1714 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1717 case SCI_REQ_STP_NON_DATA_WAIT_D2H: {
1718 struct dev_to_host_fis *frame_header;
1721 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1723 (void **)&frame_header);
1725 if (status != SCI_SUCCESS) {
1726 dev_err(&ihost->pdev->dev,
1727 "%s: SCIC IO Request 0x%p could not get frame "
1728 "header for frame index %d, status %x\n",
1737 switch (frame_header->fis_type) {
1739 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1741 (void **)&frame_buffer);
1743 sci_controller_copy_sata_response(&ireq->stp.rsp,
1747 /* The command has completed with error */
1748 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1749 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1753 dev_warn(&ihost->pdev->dev,
1754 "%s: IO Request:0x%p Frame Id:%d protocol "
1755 "violation occurred\n", __func__, stp_req,
1758 ireq->scu_status = SCU_TASK_DONE_UNEXP_FIS;
1759 ireq->sci_status = SCI_FAILURE_PROTOCOL_VIOLATION;
1763 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1765 /* Frame has been decoded return it to the controller */
1766 sci_controller_release_frame(ihost, frame_index);
1771 case SCI_REQ_STP_PIO_WAIT_FRAME: {
1772 struct sas_task *task = isci_request_access_task(ireq);
1773 struct dev_to_host_fis *frame_header;
1776 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1778 (void **)&frame_header);
1780 if (status != SCI_SUCCESS) {
1781 dev_err(&ihost->pdev->dev,
1782 "%s: SCIC IO Request 0x%p could not get frame "
1783 "header for frame index %d, status %x\n",
1784 __func__, stp_req, frame_index, status);
1788 switch (frame_header->fis_type) {
1790 /* Get from the frame buffer the PIO Setup Data */
1791 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1793 (void **)&frame_buffer);
1795 /* Get the data from the PIO Setup The SCU Hardware
1796 * returns first word in the frame_header and the rest
1797 * of the data is in the frame buffer so we need to
1801 /* transfer_count: first 16bits in the 4th dword */
1802 stp_req->pio_len = frame_buffer[3] & 0xffff;
1804 /* status: 4th byte in the 3rd dword */
1805 stp_req->status = (frame_buffer[2] >> 24) & 0xff;
1807 sci_controller_copy_sata_response(&ireq->stp.rsp,
1811 ireq->stp.rsp.status = stp_req->status;
1813 /* The next state is dependent on whether the
1814 * request was PIO Data-in or Data out
1816 if (task->data_dir == DMA_FROM_DEVICE) {
1817 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_IN);
1818 } else if (task->data_dir == DMA_TO_DEVICE) {
1820 status = sci_stp_request_pio_data_out_transmit_data(ireq);
1821 if (status != SCI_SUCCESS)
1823 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_OUT);
1827 case FIS_SETDEVBITS:
1828 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
1832 if (frame_header->status & ATA_BUSY) {
1834 * Now why is the drive sending a D2H Register
1835 * FIS when it is still busy? Do nothing since
1836 * we are still in the right state.
1838 dev_dbg(&ihost->pdev->dev,
1839 "%s: SCIC PIO Request 0x%p received "
1840 "D2H Register FIS with BSY status "
1844 frame_header->status);
1848 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1850 (void **)&frame_buffer);
1852 sci_controller_copy_sata_response(&ireq->stp.req,
1856 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1857 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1858 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1862 /* FIXME: what do we do here? */
1866 /* Frame is decoded return it to the controller */
1867 sci_controller_release_frame(ihost, frame_index);
1872 case SCI_REQ_STP_PIO_DATA_IN: {
1873 struct dev_to_host_fis *frame_header;
1874 struct sata_fis_data *frame_buffer;
1876 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1878 (void **)&frame_header);
1880 if (status != SCI_SUCCESS) {
1881 dev_err(&ihost->pdev->dev,
1882 "%s: SCIC IO Request 0x%p could not get frame "
1883 "header for frame index %d, status %x\n",
1891 if (frame_header->fis_type != FIS_DATA) {
1892 dev_err(&ihost->pdev->dev,
1893 "%s: SCIC PIO Request 0x%p received frame %d "
1894 "with fis type 0x%02x when expecting a data "
1899 frame_header->fis_type);
1901 ireq->scu_status = SCU_TASK_DONE_GOOD;
1902 ireq->sci_status = SCI_FAILURE_IO_REQUIRES_SCSI_ABORT;
1903 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1905 /* Frame is decoded return it to the controller */
1906 sci_controller_release_frame(ihost, frame_index);
1910 if (stp_req->sgl.index < 0) {
1911 ireq->saved_rx_frame_index = frame_index;
1912 stp_req->pio_len = 0;
1914 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1916 (void **)&frame_buffer);
1918 status = sci_stp_request_pio_data_in_copy_data(stp_req,
1919 (u8 *)frame_buffer);
1921 /* Frame is decoded return it to the controller */
1922 sci_controller_release_frame(ihost, frame_index);
1925 /* Check for the end of the transfer, are there more
1926 * bytes remaining for this data transfer
1928 if (status != SCI_SUCCESS || stp_req->pio_len != 0)
1931 if ((stp_req->status & ATA_BUSY) == 0) {
1932 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1933 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1934 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1936 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
1941 case SCI_REQ_STP_SOFT_RESET_WAIT_D2H: {
1942 struct dev_to_host_fis *frame_header;
1945 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1947 (void **)&frame_header);
1948 if (status != SCI_SUCCESS) {
1949 dev_err(&ihost->pdev->dev,
1950 "%s: SCIC IO Request 0x%p could not get frame "
1951 "header for frame index %d, status %x\n",
1959 switch (frame_header->fis_type) {
1961 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1963 (void **)&frame_buffer);
1965 sci_controller_copy_sata_response(&ireq->stp.rsp,
1969 /* The command has completed with error */
1970 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1971 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1975 dev_warn(&ihost->pdev->dev,
1976 "%s: IO Request:0x%p Frame Id:%d protocol "
1977 "violation occurred\n",
1982 ireq->scu_status = SCU_TASK_DONE_UNEXP_FIS;
1983 ireq->sci_status = SCI_FAILURE_PROTOCOL_VIOLATION;
1987 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1989 /* Frame has been decoded return it to the controller */
1990 sci_controller_release_frame(ihost, frame_index);
1994 case SCI_REQ_ATAPI_WAIT_PIO_SETUP: {
1995 struct sas_task *task = isci_request_access_task(ireq);
1997 sci_controller_release_frame(ihost, frame_index);
1998 ireq->target_device->working_request = ireq;
1999 if (task->data_dir == DMA_NONE) {
2000 sci_change_state(&ireq->sm, SCI_REQ_ATAPI_WAIT_TC_COMP);
2001 scu_atapi_reconstruct_raw_frame_task_context(ireq);
2003 sci_change_state(&ireq->sm, SCI_REQ_ATAPI_WAIT_D2H);
2004 scu_atapi_construct_task_context(ireq);
2007 sci_controller_continue_io(ireq);
2010 case SCI_REQ_ATAPI_WAIT_D2H:
2011 return atapi_d2h_reg_frame_handler(ireq, frame_index);
2012 case SCI_REQ_ABORTING:
2014 * TODO: Is it even possible to get an unsolicited frame in the
2017 sci_controller_release_frame(ihost, frame_index);
2021 dev_warn(&ihost->pdev->dev,
2022 "%s: SCIC IO Request given unexpected frame %x while "
2028 sci_controller_release_frame(ihost, frame_index);
2029 return SCI_FAILURE_INVALID_STATE;
2033 static enum sci_status stp_request_udma_await_tc_event(struct isci_request *ireq,
2034 u32 completion_code)
2036 enum sci_status status = SCI_SUCCESS;
2038 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2039 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
2040 ireq->scu_status = SCU_TASK_DONE_GOOD;
2041 ireq->sci_status = SCI_SUCCESS;
2042 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2044 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
2045 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
2046 /* We must check ther response buffer to see if the D2H
2047 * Register FIS was received before we got the TC
2050 if (ireq->stp.rsp.fis_type == FIS_REGD2H) {
2051 sci_remote_device_suspend(ireq->target_device,
2052 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
2054 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
2055 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
2056 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2058 /* If we have an error completion status for the
2059 * TC then we can expect a D2H register FIS from
2060 * the device so we must change state to wait
2063 sci_change_state(&ireq->sm, SCI_REQ_STP_UDMA_WAIT_D2H);
2067 /* TODO Check to see if any of these completion status need to
2068 * wait for the device to host register fis.
2070 /* TODO We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR
2071 * - this comes only for B0
2073 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_INV_FIS_LEN):
2074 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
2075 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_R_ERR):
2076 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CMD_LL_R_ERR):
2077 sci_remote_device_suspend(ireq->target_device,
2078 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
2079 /* Fall through to the default case */
2081 /* All other completion status cause the IO to be complete. */
2082 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
2083 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
2084 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2091 static enum sci_status
2092 stp_request_soft_reset_await_h2d_asserted_tc_event(struct isci_request *ireq,
2093 u32 completion_code)
2095 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2096 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
2097 ireq->scu_status = SCU_TASK_DONE_GOOD;
2098 ireq->sci_status = SCI_SUCCESS;
2099 sci_change_state(&ireq->sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG);
2104 * All other completion status cause the IO to be complete.
2105 * If a NAK was received, then it is up to the user to retry
2108 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
2109 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
2110 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2117 static enum sci_status
2118 stp_request_soft_reset_await_h2d_diagnostic_tc_event(struct isci_request *ireq,
2119 u32 completion_code)
2121 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2122 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
2123 ireq->scu_status = SCU_TASK_DONE_GOOD;
2124 ireq->sci_status = SCI_SUCCESS;
2125 sci_change_state(&ireq->sm, SCI_REQ_STP_SOFT_RESET_WAIT_D2H);
2129 /* All other completion status cause the IO to be complete. If
2130 * a NAK was received, then it is up to the user to retry the
2133 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
2134 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
2135 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2142 static enum sci_status atapi_raw_completion(struct isci_request *ireq, u32 completion_code,
2143 enum sci_base_request_states next)
2145 enum sci_status status = SCI_SUCCESS;
2147 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2148 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
2149 ireq->scu_status = SCU_TASK_DONE_GOOD;
2150 ireq->sci_status = SCI_SUCCESS;
2151 sci_change_state(&ireq->sm, next);
2154 /* All other completion status cause the IO to be complete.
2155 * If a NAK was received, then it is up to the user to retry
2158 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
2159 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
2161 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2168 static enum sci_status atapi_data_tc_completion_handler(struct isci_request *ireq,
2169 u32 completion_code)
2171 struct isci_remote_device *idev = ireq->target_device;
2172 struct dev_to_host_fis *d2h = &ireq->stp.rsp;
2173 enum sci_status status = SCI_SUCCESS;
2175 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2176 case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
2177 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2180 case (SCU_TASK_DONE_UNEXP_FIS << SCU_COMPLETION_TL_STATUS_SHIFT): {
2181 u16 len = sci_req_tx_bytes(ireq);
2183 /* likely non-error data underrrun, workaround missing
2184 * d2h frame from the controller
2186 if (d2h->fis_type != FIS_REGD2H) {
2187 d2h->fis_type = FIS_REGD2H;
2188 d2h->flags = (1 << 6);
2192 d2h->byte_count_low = len & 0xff;
2193 d2h->byte_count_high = len >> 8;
2199 d2h->sector_count = 0x3;
2200 d2h->sector_count_exp = 0;
2206 ireq->scu_status = SCU_TASK_DONE_GOOD;
2207 ireq->sci_status = SCI_SUCCESS_IO_DONE_EARLY;
2208 status = ireq->sci_status;
2210 /* the hw will have suspended the rnc, so complete the
2211 * request upon pending resume
2213 sci_change_state(&idev->sm, SCI_STP_DEV_ATAPI_ERROR);
2216 case (SCU_TASK_DONE_EXCESS_DATA << SCU_COMPLETION_TL_STATUS_SHIFT):
2217 /* In this case, there is no UF coming after.
2218 * compelte the IO now.
2220 ireq->scu_status = SCU_TASK_DONE_GOOD;
2221 ireq->sci_status = SCI_SUCCESS;
2222 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2226 if (d2h->fis_type == FIS_REGD2H) {
2227 /* UF received change the device state to ATAPI_ERROR */
2228 status = ireq->sci_status;
2229 sci_change_state(&idev->sm, SCI_STP_DEV_ATAPI_ERROR);
2231 /* If receiving any non-sucess TC status, no UF
2232 * received yet, then an UF for the status fis
2233 * is coming after (XXX: suspect this is
2234 * actually a protocol error or a bug like the
2235 * DONE_UNEXP_FIS case)
2237 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
2238 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
2240 sci_change_state(&ireq->sm, SCI_REQ_ATAPI_WAIT_D2H);
2249 sci_io_request_tc_completion(struct isci_request *ireq,
2250 u32 completion_code)
2252 enum sci_base_request_states state;
2253 struct isci_host *ihost = ireq->owning_controller;
2255 state = ireq->sm.current_state_id;
2258 case SCI_REQ_STARTED:
2259 return request_started_state_tc_event(ireq, completion_code);
2261 case SCI_REQ_TASK_WAIT_TC_COMP:
2262 return ssp_task_request_await_tc_event(ireq,
2265 case SCI_REQ_SMP_WAIT_RESP:
2266 return smp_request_await_response_tc_event(ireq,
2269 case SCI_REQ_SMP_WAIT_TC_COMP:
2270 return smp_request_await_tc_event(ireq, completion_code);
2272 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
2273 return stp_request_udma_await_tc_event(ireq,
2276 case SCI_REQ_STP_NON_DATA_WAIT_H2D:
2277 return stp_request_non_data_await_h2d_tc_event(ireq,
2280 case SCI_REQ_STP_PIO_WAIT_H2D:
2281 return stp_request_pio_await_h2d_completion_tc_event(ireq,
2284 case SCI_REQ_STP_PIO_DATA_OUT:
2285 return pio_data_out_tx_done_tc_event(ireq, completion_code);
2287 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
2288 return stp_request_soft_reset_await_h2d_asserted_tc_event(ireq,
2291 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
2292 return stp_request_soft_reset_await_h2d_diagnostic_tc_event(ireq,
2295 case SCI_REQ_ABORTING:
2296 return request_aborting_state_tc_event(ireq,
2299 case SCI_REQ_ATAPI_WAIT_H2D:
2300 return atapi_raw_completion(ireq, completion_code,
2301 SCI_REQ_ATAPI_WAIT_PIO_SETUP);
2303 case SCI_REQ_ATAPI_WAIT_TC_COMP:
2304 return atapi_raw_completion(ireq, completion_code,
2305 SCI_REQ_ATAPI_WAIT_D2H);
2307 case SCI_REQ_ATAPI_WAIT_D2H:
2308 return atapi_data_tc_completion_handler(ireq, completion_code);
2311 dev_warn(&ihost->pdev->dev,
2312 "%s: SCIC IO Request given task completion "
2313 "notification %x while in wrong state %d\n",
2317 return SCI_FAILURE_INVALID_STATE;
2322 * isci_request_process_response_iu() - This function sets the status and
2323 * response iu, in the task struct, from the request object for the upper
2325 * @sas_task: This parameter is the task struct from the upper layer driver.
2326 * @resp_iu: This parameter points to the response iu of the completed request.
2327 * @dev: This parameter specifies the linux device struct.
2331 static void isci_request_process_response_iu(
2332 struct sas_task *task,
2333 struct ssp_response_iu *resp_iu,
2338 "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
2339 "resp_iu->response_data_len = %x, "
2340 "resp_iu->sense_data_len = %x\nrepsonse data: ",
2345 resp_iu->response_data_len,
2346 resp_iu->sense_data_len);
2348 task->task_status.stat = resp_iu->status;
2350 /* libsas updates the task status fields based on the response iu. */
2351 sas_ssp_task_response(dev, task, resp_iu);
2355 * isci_request_set_open_reject_status() - This function prepares the I/O
2356 * completion for OPEN_REJECT conditions.
2357 * @request: This parameter is the completed isci_request object.
2358 * @response_ptr: This parameter specifies the service response for the I/O.
2359 * @status_ptr: This parameter specifies the exec status for the I/O.
2360 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2361 * the LLDD with respect to completing this request or forcing an abort
2362 * condition on the I/O.
2363 * @open_rej_reason: This parameter specifies the encoded reason for the
2364 * abandon-class reject.
2368 static void isci_request_set_open_reject_status(
2369 struct isci_request *request,
2370 struct sas_task *task,
2371 enum service_response *response_ptr,
2372 enum exec_status *status_ptr,
2373 enum isci_completion_selection *complete_to_host_ptr,
2374 enum sas_open_rej_reason open_rej_reason)
2376 /* Task in the target is done. */
2377 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2378 *response_ptr = SAS_TASK_UNDELIVERED;
2379 *status_ptr = SAS_OPEN_REJECT;
2380 *complete_to_host_ptr = isci_perform_normal_io_completion;
2381 task->task_status.open_rej_reason = open_rej_reason;
2385 * isci_request_handle_controller_specific_errors() - This function decodes
2386 * controller-specific I/O completion error conditions.
2387 * @request: This parameter is the completed isci_request object.
2388 * @response_ptr: This parameter specifies the service response for the I/O.
2389 * @status_ptr: This parameter specifies the exec status for the I/O.
2390 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2391 * the LLDD with respect to completing this request or forcing an abort
2392 * condition on the I/O.
2396 static void isci_request_handle_controller_specific_errors(
2397 struct isci_remote_device *idev,
2398 struct isci_request *request,
2399 struct sas_task *task,
2400 enum service_response *response_ptr,
2401 enum exec_status *status_ptr,
2402 enum isci_completion_selection *complete_to_host_ptr)
2404 unsigned int cstatus;
2406 cstatus = request->scu_status;
2408 dev_dbg(&request->isci_host->pdev->dev,
2409 "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
2410 "- controller status = 0x%x\n",
2411 __func__, request, cstatus);
2413 /* Decode the controller-specific errors; most
2414 * important is to recognize those conditions in which
2415 * the target may still have a task outstanding that
2418 * Note that there are SCU completion codes being
2419 * named in the decode below for which SCIC has already
2420 * done work to handle them in a way other than as
2421 * a controller-specific completion code; these are left
2422 * in the decode below for completeness sake.
2425 case SCU_TASK_DONE_DMASETUP_DIRERR:
2426 /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
2427 case SCU_TASK_DONE_XFERCNT_ERR:
2428 /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
2429 if (task->task_proto == SAS_PROTOCOL_SMP) {
2430 /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
2431 *response_ptr = SAS_TASK_COMPLETE;
2433 /* See if the device has been/is being stopped. Note
2434 * that we ignore the quiesce state, since we are
2435 * concerned about the actual device state.
2438 *status_ptr = SAS_DEVICE_UNKNOWN;
2440 *status_ptr = SAS_ABORTED_TASK;
2442 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2444 *complete_to_host_ptr =
2445 isci_perform_normal_io_completion;
2447 /* Task in the target is not done. */
2448 *response_ptr = SAS_TASK_UNDELIVERED;
2451 *status_ptr = SAS_DEVICE_UNKNOWN;
2453 *status_ptr = SAM_STAT_TASK_ABORTED;
2455 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2457 *complete_to_host_ptr =
2458 isci_perform_error_io_completion;
2463 case SCU_TASK_DONE_CRC_ERR:
2464 case SCU_TASK_DONE_NAK_CMD_ERR:
2465 case SCU_TASK_DONE_EXCESS_DATA:
2466 case SCU_TASK_DONE_UNEXP_FIS:
2467 /* Also SCU_TASK_DONE_UNEXP_RESP: */
2468 case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
2469 case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
2470 case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
2471 /* These are conditions in which the target
2472 * has completed the task, so that no cleanup
2475 *response_ptr = SAS_TASK_COMPLETE;
2477 /* See if the device has been/is being stopped. Note
2478 * that we ignore the quiesce state, since we are
2479 * concerned about the actual device state.
2482 *status_ptr = SAS_DEVICE_UNKNOWN;
2484 *status_ptr = SAS_ABORTED_TASK;
2486 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2488 *complete_to_host_ptr = isci_perform_normal_io_completion;
2492 /* Note that the only open reject completion codes seen here will be
2493 * abandon-class codes; all others are automatically retried in the SCU.
2495 case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2497 isci_request_set_open_reject_status(
2498 request, task, response_ptr, status_ptr,
2499 complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
2502 case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2504 /* Note - the return of AB0 will change when
2505 * libsas implements detection of zone violations.
2507 isci_request_set_open_reject_status(
2508 request, task, response_ptr, status_ptr,
2509 complete_to_host_ptr, SAS_OREJ_RESV_AB0);
2512 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2514 isci_request_set_open_reject_status(
2515 request, task, response_ptr, status_ptr,
2516 complete_to_host_ptr, SAS_OREJ_RESV_AB1);
2519 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2521 isci_request_set_open_reject_status(
2522 request, task, response_ptr, status_ptr,
2523 complete_to_host_ptr, SAS_OREJ_RESV_AB2);
2526 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2528 isci_request_set_open_reject_status(
2529 request, task, response_ptr, status_ptr,
2530 complete_to_host_ptr, SAS_OREJ_RESV_AB3);
2533 case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2535 isci_request_set_open_reject_status(
2536 request, task, response_ptr, status_ptr,
2537 complete_to_host_ptr, SAS_OREJ_BAD_DEST);
2540 case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
2542 isci_request_set_open_reject_status(
2543 request, task, response_ptr, status_ptr,
2544 complete_to_host_ptr, SAS_OREJ_STP_NORES);
2547 case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
2549 isci_request_set_open_reject_status(
2550 request, task, response_ptr, status_ptr,
2551 complete_to_host_ptr, SAS_OREJ_EPROTO);
2554 case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
2556 isci_request_set_open_reject_status(
2557 request, task, response_ptr, status_ptr,
2558 complete_to_host_ptr, SAS_OREJ_CONN_RATE);
2561 case SCU_TASK_DONE_LL_R_ERR:
2562 /* Also SCU_TASK_DONE_ACK_NAK_TO: */
2563 case SCU_TASK_DONE_LL_PERR:
2564 case SCU_TASK_DONE_LL_SY_TERM:
2565 /* Also SCU_TASK_DONE_NAK_ERR:*/
2566 case SCU_TASK_DONE_LL_LF_TERM:
2567 /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
2568 case SCU_TASK_DONE_LL_ABORT_ERR:
2569 case SCU_TASK_DONE_SEQ_INV_TYPE:
2570 /* Also SCU_TASK_DONE_UNEXP_XR: */
2571 case SCU_TASK_DONE_XR_IU_LEN_ERR:
2572 case SCU_TASK_DONE_INV_FIS_LEN:
2573 /* Also SCU_TASK_DONE_XR_WD_LEN: */
2574 case SCU_TASK_DONE_SDMA_ERR:
2575 case SCU_TASK_DONE_OFFSET_ERR:
2576 case SCU_TASK_DONE_MAX_PLD_ERR:
2577 case SCU_TASK_DONE_LF_ERR:
2578 case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
2579 case SCU_TASK_DONE_SMP_LL_RX_ERR:
2580 case SCU_TASK_DONE_UNEXP_DATA:
2581 case SCU_TASK_DONE_UNEXP_SDBFIS:
2582 case SCU_TASK_DONE_REG_ERR:
2583 case SCU_TASK_DONE_SDB_ERR:
2584 case SCU_TASK_DONE_TASK_ABORT:
2586 /* Task in the target is not done. */
2587 *response_ptr = SAS_TASK_UNDELIVERED;
2588 *status_ptr = SAM_STAT_TASK_ABORTED;
2590 if (task->task_proto == SAS_PROTOCOL_SMP) {
2591 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2593 *complete_to_host_ptr = isci_perform_normal_io_completion;
2595 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2597 *complete_to_host_ptr = isci_perform_error_io_completion;
2604 * isci_task_save_for_upper_layer_completion() - This function saves the
2605 * request for later completion to the upper layer driver.
2606 * @host: This parameter is a pointer to the host on which the the request
2607 * should be queued (either as an error or success).
2608 * @request: This parameter is the completed request.
2609 * @response: This parameter is the response code for the completed task.
2610 * @status: This parameter is the status code for the completed task.
2614 static void isci_task_save_for_upper_layer_completion(
2615 struct isci_host *host,
2616 struct isci_request *request,
2617 enum service_response response,
2618 enum exec_status status,
2619 enum isci_completion_selection task_notification_selection)
2621 struct sas_task *task = isci_request_access_task(request);
2623 task_notification_selection
2624 = isci_task_set_completion_status(task, response, status,
2625 task_notification_selection);
2627 /* Tasks aborted specifically by a call to the lldd_abort_task
2628 * function should not be completed to the host in the regular path.
2630 switch (task_notification_selection) {
2632 case isci_perform_normal_io_completion:
2634 /* Normal notification (task_done) */
2635 dev_dbg(&host->pdev->dev,
2636 "%s: Normal - task = %p, response=%d (%d), status=%d (%d)\n",
2639 task->task_status.resp, response,
2640 task->task_status.stat, status);
2641 /* Add to the completed list. */
2642 list_add(&request->completed_node,
2643 &host->requests_to_complete);
2645 /* Take the request off the device's pending request list. */
2646 list_del_init(&request->dev_node);
2649 case isci_perform_aborted_io_completion:
2650 /* No notification to libsas because this request is
2651 * already in the abort path.
2653 dev_dbg(&host->pdev->dev,
2654 "%s: Aborted - task = %p, response=%d (%d), status=%d (%d)\n",
2657 task->task_status.resp, response,
2658 task->task_status.stat, status);
2660 /* Wake up whatever process was waiting for this
2661 * request to complete.
2663 WARN_ON(request->io_request_completion == NULL);
2665 if (request->io_request_completion != NULL) {
2667 /* Signal whoever is waiting that this
2668 * request is complete.
2670 complete(request->io_request_completion);
2674 case isci_perform_error_io_completion:
2675 /* Use sas_task_abort */
2676 dev_dbg(&host->pdev->dev,
2677 "%s: Error - task = %p, response=%d (%d), status=%d (%d)\n",
2680 task->task_status.resp, response,
2681 task->task_status.stat, status);
2682 /* Add to the aborted list. */
2683 list_add(&request->completed_node,
2684 &host->requests_to_errorback);
2688 dev_dbg(&host->pdev->dev,
2689 "%s: Unknown - task = %p, response=%d (%d), status=%d (%d)\n",
2692 task->task_status.resp, response,
2693 task->task_status.stat, status);
2695 /* Add to the error to libsas list. */
2696 list_add(&request->completed_node,
2697 &host->requests_to_errorback);
2702 static void isci_process_stp_response(struct sas_task *task, struct dev_to_host_fis *fis)
2704 struct task_status_struct *ts = &task->task_status;
2705 struct ata_task_resp *resp = (void *)&ts->buf[0];
2707 resp->frame_len = sizeof(*fis);
2708 memcpy(resp->ending_fis, fis, sizeof(*fis));
2709 ts->buf_valid_size = sizeof(*resp);
2711 /* If the device fault bit is set in the status register, then
2712 * set the sense data and return.
2714 if (fis->status & ATA_DF)
2715 ts->stat = SAS_PROTO_RESPONSE;
2716 else if (fis->status & ATA_ERR)
2717 ts->stat = SAM_STAT_CHECK_CONDITION;
2719 ts->stat = SAM_STAT_GOOD;
2721 ts->resp = SAS_TASK_COMPLETE;
2724 static void isci_request_io_request_complete(struct isci_host *ihost,
2725 struct isci_request *request,
2726 enum sci_io_status completion_status)
2728 struct sas_task *task = isci_request_access_task(request);
2729 struct ssp_response_iu *resp_iu;
2730 unsigned long task_flags;
2731 struct isci_remote_device *idev = isci_lookup_device(task->dev);
2732 enum service_response response = SAS_TASK_UNDELIVERED;
2733 enum exec_status status = SAS_ABORTED_TASK;
2734 enum isci_request_status request_status;
2735 enum isci_completion_selection complete_to_host
2736 = isci_perform_normal_io_completion;
2738 dev_dbg(&ihost->pdev->dev,
2739 "%s: request = %p, task = %p,\n"
2740 "task->data_dir = %d completion_status = 0x%x\n",
2747 spin_lock(&request->state_lock);
2748 request_status = request->status;
2750 /* Decode the request status. Note that if the request has been
2751 * aborted by a task management function, we don't care
2752 * what the status is.
2754 switch (request_status) {
2757 /* "aborted" indicates that the request was aborted by a task
2758 * management function, since once a task management request is
2759 * perfomed by the device, the request only completes because
2760 * of the subsequent driver terminate.
2762 * Aborted also means an external thread is explicitly managing
2763 * this request, so that we do not complete it up the stack.
2765 * The target is still there (since the TMF was successful).
2767 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2768 response = SAS_TASK_COMPLETE;
2770 /* See if the device has been/is being stopped. Note
2771 * that we ignore the quiesce state, since we are
2772 * concerned about the actual device state.
2775 status = SAS_DEVICE_UNKNOWN;
2777 status = SAS_ABORTED_TASK;
2779 complete_to_host = isci_perform_aborted_io_completion;
2780 /* This was an aborted request. */
2782 spin_unlock(&request->state_lock);
2786 /* aborting means that the task management function tried and
2787 * failed to abort the request. We need to note the request
2788 * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
2791 * Aborting also means an external thread is explicitly managing
2792 * this request, so that we do not complete it up the stack.
2794 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2795 response = SAS_TASK_UNDELIVERED;
2798 /* The device has been /is being stopped. Note that
2799 * we ignore the quiesce state, since we are
2800 * concerned about the actual device state.
2802 status = SAS_DEVICE_UNKNOWN;
2804 status = SAS_PHY_DOWN;
2806 complete_to_host = isci_perform_aborted_io_completion;
2808 /* This was an aborted request. */
2810 spin_unlock(&request->state_lock);
2815 /* This was an terminated request. This happens when
2816 * the I/O is being terminated because of an action on
2817 * the device (reset, tear down, etc.), and the I/O needs
2818 * to be completed up the stack.
2820 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2821 response = SAS_TASK_UNDELIVERED;
2823 /* See if the device has been/is being stopped. Note
2824 * that we ignore the quiesce state, since we are
2825 * concerned about the actual device state.
2828 status = SAS_DEVICE_UNKNOWN;
2830 status = SAS_ABORTED_TASK;
2832 complete_to_host = isci_perform_aborted_io_completion;
2834 /* This was a terminated request. */
2836 spin_unlock(&request->state_lock);
2840 /* This was a terminated request that timed-out during the
2841 * termination process. There is no task to complete to
2844 complete_to_host = isci_perform_normal_io_completion;
2845 spin_unlock(&request->state_lock);
2850 /* The request is done from an SCU HW perspective. */
2851 request->status = completed;
2853 spin_unlock(&request->state_lock);
2855 /* This is an active request being completed from the core. */
2856 switch (completion_status) {
2858 case SCI_IO_FAILURE_RESPONSE_VALID:
2859 dev_dbg(&ihost->pdev->dev,
2860 "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
2865 if (sas_protocol_ata(task->task_proto)) {
2866 isci_process_stp_response(task, &request->stp.rsp);
2867 } else if (SAS_PROTOCOL_SSP == task->task_proto) {
2869 /* crack the iu response buffer. */
2870 resp_iu = &request->ssp.rsp;
2871 isci_request_process_response_iu(task, resp_iu,
2874 } else if (SAS_PROTOCOL_SMP == task->task_proto) {
2876 dev_err(&ihost->pdev->dev,
2877 "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
2878 "SAS_PROTOCOL_SMP protocol\n",
2882 dev_err(&ihost->pdev->dev,
2883 "%s: unknown protocol\n", __func__);
2885 /* use the task status set in the task struct by the
2886 * isci_request_process_response_iu call.
2888 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2889 response = task->task_status.resp;
2890 status = task->task_status.stat;
2893 case SCI_IO_SUCCESS:
2894 case SCI_IO_SUCCESS_IO_DONE_EARLY:
2896 response = SAS_TASK_COMPLETE;
2897 status = SAM_STAT_GOOD;
2898 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2900 if (completion_status == SCI_IO_SUCCESS_IO_DONE_EARLY) {
2902 /* This was an SSP / STP / SATA transfer.
2903 * There is a possibility that less data than
2904 * the maximum was transferred.
2906 u32 transferred_length = sci_req_tx_bytes(request);
2908 task->task_status.residual
2909 = task->total_xfer_len - transferred_length;
2911 /* If there were residual bytes, call this an
2914 if (task->task_status.residual != 0)
2915 status = SAS_DATA_UNDERRUN;
2917 dev_dbg(&ihost->pdev->dev,
2918 "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
2923 dev_dbg(&ihost->pdev->dev,
2924 "%s: SCI_IO_SUCCESS\n",
2929 case SCI_IO_FAILURE_TERMINATED:
2930 dev_dbg(&ihost->pdev->dev,
2931 "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
2936 /* The request was terminated explicitly. No handling
2937 * is needed in the SCSI error handler path.
2939 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2940 response = SAS_TASK_UNDELIVERED;
2942 /* See if the device has been/is being stopped. Note
2943 * that we ignore the quiesce state, since we are
2944 * concerned about the actual device state.
2947 status = SAS_DEVICE_UNKNOWN;
2949 status = SAS_ABORTED_TASK;
2951 complete_to_host = isci_perform_normal_io_completion;
2954 case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
2956 isci_request_handle_controller_specific_errors(
2957 idev, request, task, &response, &status,
2962 case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
2963 /* This is a special case, in that the I/O completion
2964 * is telling us that the device needs a reset.
2965 * In order for the device reset condition to be
2966 * noticed, the I/O has to be handled in the error
2967 * handler. Set the reset flag and cause the
2968 * SCSI error thread to be scheduled.
2970 spin_lock_irqsave(&task->task_state_lock, task_flags);
2971 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
2972 spin_unlock_irqrestore(&task->task_state_lock, task_flags);
2975 response = SAS_TASK_UNDELIVERED;
2976 status = SAM_STAT_TASK_ABORTED;
2978 complete_to_host = isci_perform_error_io_completion;
2979 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2982 case SCI_FAILURE_RETRY_REQUIRED:
2984 /* Fail the I/O so it can be retried. */
2985 response = SAS_TASK_UNDELIVERED;
2987 status = SAS_DEVICE_UNKNOWN;
2989 status = SAS_ABORTED_TASK;
2991 complete_to_host = isci_perform_normal_io_completion;
2992 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
2997 /* Catch any otherwise unhandled error codes here. */
2998 dev_dbg(&ihost->pdev->dev,
2999 "%s: invalid completion code: 0x%x - "
3000 "isci_request = %p\n",
3001 __func__, completion_status, request);
3003 response = SAS_TASK_UNDELIVERED;
3005 /* See if the device has been/is being stopped. Note
3006 * that we ignore the quiesce state, since we are
3007 * concerned about the actual device state.
3010 status = SAS_DEVICE_UNKNOWN;
3012 status = SAS_ABORTED_TASK;
3014 if (SAS_PROTOCOL_SMP == task->task_proto) {
3015 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
3016 complete_to_host = isci_perform_normal_io_completion;
3018 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
3019 complete_to_host = isci_perform_error_io_completion;
3026 switch (task->task_proto) {
3027 case SAS_PROTOCOL_SSP:
3028 if (task->data_dir == DMA_NONE)
3030 if (task->num_scatter == 0)
3031 /* 0 indicates a single dma address */
3032 dma_unmap_single(&ihost->pdev->dev,
3033 request->zero_scatter_daddr,
3034 task->total_xfer_len, task->data_dir);
3035 else /* unmap the sgl dma addresses */
3036 dma_unmap_sg(&ihost->pdev->dev, task->scatter,
3037 request->num_sg_entries, task->data_dir);
3039 case SAS_PROTOCOL_SMP: {
3040 struct scatterlist *sg = &task->smp_task.smp_req;
3041 struct smp_req *smp_req;
3044 dma_unmap_sg(&ihost->pdev->dev, sg, 1, DMA_TO_DEVICE);
3046 /* need to swab it back in case the command buffer is re-used */
3047 kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
3048 smp_req = kaddr + sg->offset;
3049 sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
3050 kunmap_atomic(kaddr, KM_IRQ0);
3057 /* Put the completed request on the correct list */
3058 isci_task_save_for_upper_layer_completion(ihost, request, response,
3059 status, complete_to_host
3062 /* complete the io request to the core. */
3063 sci_controller_complete_io(ihost, request->target_device, request);
3064 isci_put_device(idev);
3066 /* set terminated handle so it cannot be completed or
3067 * terminated again, and to cause any calls into abort
3068 * task to recognize the already completed case.
3070 set_bit(IREQ_TERMINATED, &request->flags);
3073 static void sci_request_started_state_enter(struct sci_base_state_machine *sm)
3075 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
3076 struct domain_device *dev = ireq->target_device->domain_dev;
3077 enum sci_base_request_states state;
3078 struct sas_task *task;
3080 /* XXX as hch said always creating an internal sas_task for tmf
3081 * requests would simplify the driver
3083 task = ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL;
3085 /* all unaccelerated request types (non ssp or ncq) handled with
3088 if (!task && dev->dev_type == SAS_END_DEV) {
3089 state = SCI_REQ_TASK_WAIT_TC_COMP;
3091 (isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_high ||
3092 isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_low)) {
3093 state = SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED;
3094 } else if (task && task->task_proto == SAS_PROTOCOL_SMP) {
3095 state = SCI_REQ_SMP_WAIT_RESP;
3096 } else if (task && sas_protocol_ata(task->task_proto) &&
3097 !task->ata_task.use_ncq) {
3098 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET &&
3099 task->ata_task.fis.command == ATA_CMD_PACKET) {
3100 state = SCI_REQ_ATAPI_WAIT_H2D;
3101 } else if (task->data_dir == DMA_NONE) {
3102 state = SCI_REQ_STP_NON_DATA_WAIT_H2D;
3103 } else if (task->ata_task.dma_xfer) {
3104 state = SCI_REQ_STP_UDMA_WAIT_TC_COMP;
3106 state = SCI_REQ_STP_PIO_WAIT_H2D;
3109 /* SSP or NCQ are fully accelerated, no substates */
3112 sci_change_state(sm, state);
3115 static void sci_request_completed_state_enter(struct sci_base_state_machine *sm)
3117 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
3118 struct isci_host *ihost = ireq->owning_controller;
3120 /* Tell the SCI_USER that the IO request is complete */
3121 if (!test_bit(IREQ_TMF, &ireq->flags))
3122 isci_request_io_request_complete(ihost, ireq,
3125 isci_task_request_complete(ihost, ireq, ireq->sci_status);
3128 static void sci_request_aborting_state_enter(struct sci_base_state_machine *sm)
3130 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
3132 /* Setting the abort bit in the Task Context is required by the silicon. */
3133 ireq->tc->abort = 1;
3136 static void sci_stp_request_started_non_data_await_h2d_completion_enter(struct sci_base_state_machine *sm)
3138 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
3140 ireq->target_device->working_request = ireq;
3143 static void sci_stp_request_started_pio_await_h2d_completion_enter(struct sci_base_state_machine *sm)
3145 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
3147 ireq->target_device->working_request = ireq;
3150 static void sci_stp_request_started_soft_reset_await_h2d_asserted_completion_enter(struct sci_base_state_machine *sm)
3152 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
3154 ireq->target_device->working_request = ireq;
3157 static void sci_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter(struct sci_base_state_machine *sm)
3159 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
3160 struct scu_task_context *tc = ireq->tc;
3161 struct host_to_dev_fis *h2d_fis;
3162 enum sci_status status;
3164 /* Clear the SRST bit */
3165 h2d_fis = &ireq->stp.cmd;
3166 h2d_fis->control = 0;
3168 /* Clear the TC control bit */
3169 tc->control_frame = 0;
3171 status = sci_controller_continue_io(ireq);
3172 WARN_ONCE(status != SCI_SUCCESS, "isci: continue io failure\n");
3175 static const struct sci_base_state sci_request_state_table[] = {
3176 [SCI_REQ_INIT] = { },
3177 [SCI_REQ_CONSTRUCTED] = { },
3178 [SCI_REQ_STARTED] = {
3179 .enter_state = sci_request_started_state_enter,
3181 [SCI_REQ_STP_NON_DATA_WAIT_H2D] = {
3182 .enter_state = sci_stp_request_started_non_data_await_h2d_completion_enter,
3184 [SCI_REQ_STP_NON_DATA_WAIT_D2H] = { },
3185 [SCI_REQ_STP_PIO_WAIT_H2D] = {
3186 .enter_state = sci_stp_request_started_pio_await_h2d_completion_enter,
3188 [SCI_REQ_STP_PIO_WAIT_FRAME] = { },
3189 [SCI_REQ_STP_PIO_DATA_IN] = { },
3190 [SCI_REQ_STP_PIO_DATA_OUT] = { },
3191 [SCI_REQ_STP_UDMA_WAIT_TC_COMP] = { },
3192 [SCI_REQ_STP_UDMA_WAIT_D2H] = { },
3193 [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED] = {
3194 .enter_state = sci_stp_request_started_soft_reset_await_h2d_asserted_completion_enter,
3196 [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG] = {
3197 .enter_state = sci_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter,
3199 [SCI_REQ_STP_SOFT_RESET_WAIT_D2H] = { },
3200 [SCI_REQ_TASK_WAIT_TC_COMP] = { },
3201 [SCI_REQ_TASK_WAIT_TC_RESP] = { },
3202 [SCI_REQ_SMP_WAIT_RESP] = { },
3203 [SCI_REQ_SMP_WAIT_TC_COMP] = { },
3204 [SCI_REQ_ATAPI_WAIT_H2D] = { },
3205 [SCI_REQ_ATAPI_WAIT_PIO_SETUP] = { },
3206 [SCI_REQ_ATAPI_WAIT_D2H] = { },
3207 [SCI_REQ_ATAPI_WAIT_TC_COMP] = { },
3208 [SCI_REQ_COMPLETED] = {
3209 .enter_state = sci_request_completed_state_enter,
3211 [SCI_REQ_ABORTING] = {
3212 .enter_state = sci_request_aborting_state_enter,
3214 [SCI_REQ_FINAL] = { },
3218 sci_general_request_construct(struct isci_host *ihost,
3219 struct isci_remote_device *idev,
3220 struct isci_request *ireq)
3222 sci_init_sm(&ireq->sm, sci_request_state_table, SCI_REQ_INIT);
3224 ireq->target_device = idev;
3225 ireq->protocol = SCIC_NO_PROTOCOL;
3226 ireq->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
3228 ireq->sci_status = SCI_SUCCESS;
3229 ireq->scu_status = 0;
3230 ireq->post_context = 0xFFFFFFFF;
3233 static enum sci_status
3234 sci_io_request_construct(struct isci_host *ihost,
3235 struct isci_remote_device *idev,
3236 struct isci_request *ireq)
3238 struct domain_device *dev = idev->domain_dev;
3239 enum sci_status status = SCI_SUCCESS;
3241 /* Build the common part of the request */
3242 sci_general_request_construct(ihost, idev, ireq);
3244 if (idev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
3245 return SCI_FAILURE_INVALID_REMOTE_DEVICE;
3247 if (dev->dev_type == SAS_END_DEV)
3249 else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
3250 memset(&ireq->stp.cmd, 0, sizeof(ireq->stp.cmd));
3251 else if (dev_is_expander(dev))
3254 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
3256 memset(ireq->tc, 0, offsetof(struct scu_task_context, sgl_pair_ab));
3261 enum sci_status sci_task_request_construct(struct isci_host *ihost,
3262 struct isci_remote_device *idev,
3263 u16 io_tag, struct isci_request *ireq)
3265 struct domain_device *dev = idev->domain_dev;
3266 enum sci_status status = SCI_SUCCESS;
3268 /* Build the common part of the request */
3269 sci_general_request_construct(ihost, idev, ireq);
3271 if (dev->dev_type == SAS_END_DEV ||
3272 dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
3273 set_bit(IREQ_TMF, &ireq->flags);
3274 memset(ireq->tc, 0, sizeof(struct scu_task_context));
3276 status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
3281 static enum sci_status isci_request_ssp_request_construct(
3282 struct isci_request *request)
3284 enum sci_status status;
3286 dev_dbg(&request->isci_host->pdev->dev,
3287 "%s: request = %p\n",
3290 status = sci_io_request_construct_basic_ssp(request);
3294 static enum sci_status isci_request_stp_request_construct(struct isci_request *ireq)
3296 struct sas_task *task = isci_request_access_task(ireq);
3297 struct host_to_dev_fis *fis = &ireq->stp.cmd;
3298 struct ata_queued_cmd *qc = task->uldd_task;
3299 enum sci_status status;
3301 dev_dbg(&ireq->isci_host->pdev->dev,
3306 memcpy(fis, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
3307 if (!task->ata_task.device_control_reg_update)
3311 status = sci_io_request_construct_basic_sata(ireq);
3313 if (qc && (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
3314 qc->tf.command == ATA_CMD_FPDMA_READ)) {
3315 fis->sector_count = qc->tag << 3;
3316 ireq->tc->type.stp.ncq_tag = qc->tag;
3322 static enum sci_status
3323 sci_io_request_construct_smp(struct device *dev,
3324 struct isci_request *ireq,
3325 struct sas_task *task)
3327 struct scatterlist *sg = &task->smp_task.smp_req;
3328 struct isci_remote_device *idev;
3329 struct scu_task_context *task_context;
3330 struct isci_port *iport;
3331 struct smp_req *smp_req;
3336 kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
3337 smp_req = kaddr + sg->offset;
3339 * Look at the SMP requests' header fields; for certain SAS 1.x SMP
3340 * functions under SAS 2.0, a zero request length really indicates
3341 * a non-zero default length.
3343 if (smp_req->req_len == 0) {
3344 switch (smp_req->func) {
3346 case SMP_REPORT_PHY_ERR_LOG:
3347 case SMP_REPORT_PHY_SATA:
3348 case SMP_REPORT_ROUTE_INFO:
3349 smp_req->req_len = 2;
3351 case SMP_CONF_ROUTE_INFO:
3352 case SMP_PHY_CONTROL:
3353 case SMP_PHY_TEST_FUNCTION:
3354 smp_req->req_len = 9;
3356 /* Default - zero is a valid default for 2.0. */
3359 req_len = smp_req->req_len;
3360 sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
3361 cmd = *(u32 *) smp_req;
3362 kunmap_atomic(kaddr, KM_IRQ0);
3364 if (!dma_map_sg(dev, sg, 1, DMA_TO_DEVICE))
3367 ireq->protocol = SCIC_SMP_PROTOCOL;
3369 /* byte swap the smp request. */
3371 task_context = ireq->tc;
3373 idev = ireq->target_device;
3374 iport = idev->owning_port;
3377 * Fill in the TC with the its required data
3380 task_context->priority = 0;
3381 task_context->initiator_request = 1;
3382 task_context->connection_rate = idev->connection_rate;
3383 task_context->protocol_engine_index = ISCI_PEG;
3384 task_context->logical_port_index = iport->physical_port_index;
3385 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
3386 task_context->abort = 0;
3387 task_context->valid = SCU_TASK_CONTEXT_VALID;
3388 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
3391 task_context->remote_node_index = idev->rnc.remote_node_index;
3392 task_context->command_code = 0;
3393 task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;
3396 task_context->link_layer_control = 0;
3397 task_context->do_not_dma_ssp_good_response = 1;
3398 task_context->strict_ordering = 0;
3399 task_context->control_frame = 1;
3400 task_context->timeout_enable = 0;
3401 task_context->block_guard_enable = 0;
3404 task_context->address_modifier = 0;
3407 task_context->ssp_command_iu_length = req_len;
3410 task_context->transfer_length_bytes = 0;
3413 * 18h ~ 30h, protocol specific
3414 * since commandIU has been build by framework at this point, we just
3415 * copy the frist DWord from command IU to this location. */
3416 memcpy(&task_context->type.smp, &cmd, sizeof(u32));
3420 * "For SMP you could program it to zero. We would prefer that way
3421 * so that done code will be consistent." - Venki
3423 task_context->task_phase = 0;
3425 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
3426 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
3427 (iport->physical_port_index <<
3428 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
3429 ISCI_TAG_TCI(ireq->io_tag));
3431 * Copy the physical address for the command buffer to the SCU Task
3432 * Context command buffer should not contain command header.
3434 task_context->command_iu_upper = upper_32_bits(sg_dma_address(sg));
3435 task_context->command_iu_lower = lower_32_bits(sg_dma_address(sg) + sizeof(u32));
3437 /* SMP response comes as UF, so no need to set response IU address. */
3438 task_context->response_iu_upper = 0;
3439 task_context->response_iu_lower = 0;
3441 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
3447 * isci_smp_request_build() - This function builds the smp request.
3448 * @ireq: This parameter points to the isci_request allocated in the
3449 * request construct function.
3451 * SCI_SUCCESS on successfull completion, or specific failure code.
3453 static enum sci_status isci_smp_request_build(struct isci_request *ireq)
3455 struct sas_task *task = isci_request_access_task(ireq);
3456 struct device *dev = &ireq->isci_host->pdev->dev;
3457 enum sci_status status = SCI_FAILURE;
3459 status = sci_io_request_construct_smp(dev, ireq, task);
3460 if (status != SCI_SUCCESS)
3461 dev_dbg(&ireq->isci_host->pdev->dev,
3462 "%s: failed with status = %d\n",
3470 * isci_io_request_build() - This function builds the io request object.
3471 * @ihost: This parameter specifies the ISCI host object
3472 * @request: This parameter points to the isci_request object allocated in the
3473 * request construct function.
3474 * @sci_device: This parameter is the handle for the sci core's remote device
3475 * object that is the destination for this request.
3477 * SCI_SUCCESS on successfull completion, or specific failure code.
3479 static enum sci_status isci_io_request_build(struct isci_host *ihost,
3480 struct isci_request *request,
3481 struct isci_remote_device *idev)
3483 enum sci_status status = SCI_SUCCESS;
3484 struct sas_task *task = isci_request_access_task(request);
3486 dev_dbg(&ihost->pdev->dev,
3487 "%s: idev = 0x%p; request = %p, "
3488 "num_scatter = %d\n",
3494 /* map the sgl addresses, if present.
3495 * libata does the mapping for sata devices
3496 * before we get the request.
3498 if (task->num_scatter &&
3499 !sas_protocol_ata(task->task_proto) &&
3500 !(SAS_PROTOCOL_SMP & task->task_proto)) {
3502 request->num_sg_entries = dma_map_sg(
3509 if (request->num_sg_entries == 0)
3510 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
3513 status = sci_io_request_construct(ihost, idev, request);
3515 if (status != SCI_SUCCESS) {
3516 dev_dbg(&ihost->pdev->dev,
3517 "%s: failed request construct\n",
3522 switch (task->task_proto) {
3523 case SAS_PROTOCOL_SMP:
3524 status = isci_smp_request_build(request);
3526 case SAS_PROTOCOL_SSP:
3527 status = isci_request_ssp_request_construct(request);
3529 case SAS_PROTOCOL_SATA:
3530 case SAS_PROTOCOL_STP:
3531 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
3532 status = isci_request_stp_request_construct(request);
3535 dev_dbg(&ihost->pdev->dev,
3536 "%s: unknown protocol\n", __func__);
3543 static struct isci_request *isci_request_from_tag(struct isci_host *ihost, u16 tag)
3545 struct isci_request *ireq;
3547 ireq = ihost->reqs[ISCI_TAG_TCI(tag)];
3549 ireq->io_request_completion = NULL;
3551 ireq->num_sg_entries = 0;
3552 INIT_LIST_HEAD(&ireq->completed_node);
3553 INIT_LIST_HEAD(&ireq->dev_node);
3554 isci_request_change_state(ireq, allocated);
3559 static struct isci_request *isci_io_request_from_tag(struct isci_host *ihost,
3560 struct sas_task *task,
3563 struct isci_request *ireq;
3565 ireq = isci_request_from_tag(ihost, tag);
3566 ireq->ttype_ptr.io_task_ptr = task;
3567 ireq->ttype = io_task;
3568 task->lldd_task = ireq;
3573 struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
3574 struct isci_tmf *isci_tmf,
3577 struct isci_request *ireq;
3579 ireq = isci_request_from_tag(ihost, tag);
3580 ireq->ttype_ptr.tmf_task_ptr = isci_tmf;
3581 ireq->ttype = tmf_task;
3586 int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
3587 struct sas_task *task, u16 tag)
3589 enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
3590 struct isci_request *ireq;
3591 unsigned long flags;
3594 /* do common allocation and init of request object. */
3595 ireq = isci_io_request_from_tag(ihost, task, tag);
3597 status = isci_io_request_build(ihost, ireq, idev);
3598 if (status != SCI_SUCCESS) {
3599 dev_dbg(&ihost->pdev->dev,
3600 "%s: request_construct failed - status = 0x%x\n",
3606 spin_lock_irqsave(&ihost->scic_lock, flags);
3608 if (test_bit(IDEV_IO_NCQERROR, &idev->flags)) {
3610 if (isci_task_is_ncq_recovery(task)) {
3612 /* The device is in an NCQ recovery state. Issue the
3613 * request on the task side. Note that it will
3614 * complete on the I/O request side because the
3615 * request was built that way (ie.
3616 * ireq->is_task_management_request is false).
3618 status = sci_controller_start_task(ihost,
3622 status = SCI_FAILURE;
3625 /* send the request, let the core assign the IO TAG. */
3626 status = sci_controller_start_io(ihost, idev,
3630 if (status != SCI_SUCCESS &&
3631 status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
3632 dev_dbg(&ihost->pdev->dev,
3633 "%s: failed request start (0x%x)\n",
3635 spin_unlock_irqrestore(&ihost->scic_lock, flags);
3639 /* Either I/O started OK, or the core has signaled that
3640 * the device needs a target reset.
3642 * In either case, hold onto the I/O for later.
3644 * Update it's status and add it to the list in the
3645 * remote device object.
3647 list_add(&ireq->dev_node, &idev->reqs_in_process);
3649 if (status == SCI_SUCCESS) {
3650 isci_request_change_state(ireq, started);
3652 /* The request did not really start in the
3653 * hardware, so clear the request handle
3654 * here so no terminations will be done.
3656 set_bit(IREQ_TERMINATED, &ireq->flags);
3657 isci_request_change_state(ireq, completed);
3659 spin_unlock_irqrestore(&ihost->scic_lock, flags);
3662 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
3663 /* Signal libsas that we need the SCSI error
3664 * handler thread to work on this I/O and that
3665 * we want a device reset.
3667 spin_lock_irqsave(&task->task_state_lock, flags);
3668 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
3669 spin_unlock_irqrestore(&task->task_state_lock, flags);
3671 /* Cause this task to be scheduled in the SCSI error
3674 isci_execpath_callback(ihost, task,
3677 /* Change the status, since we are holding
3678 * the I/O until it is managed by the SCSI
3681 status = SCI_SUCCESS;