blob: 36e674896bc5f47fcf758d9b3ed518c4d52ca509 [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55
56#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "task.h"
58#include "request.h"
59#include "sata.h"
60#include "scu_completion_codes.h"
Dan Williams5dec6f42011-05-10 02:28:49 -070061#include "scu_event_codes.h"
Dave Jiang2ec53eb2011-05-04 18:01:22 -070062#include "sas.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070063
Dan Williams5076a1a2011-06-27 14:57:03 -070064static struct scu_sgl_element_pair *to_sgl_element_pair(struct isci_request *ireq,
Dan Williams312e0c22011-06-28 13:47:09 -070065 int idx)
66{
67 if (idx == 0)
Dan Williams5076a1a2011-06-27 14:57:03 -070068 return &ireq->tc->sgl_pair_ab;
Dan Williams312e0c22011-06-28 13:47:09 -070069 else if (idx == 1)
Dan Williams5076a1a2011-06-27 14:57:03 -070070 return &ireq->tc->sgl_pair_cd;
Dan Williams312e0c22011-06-28 13:47:09 -070071 else if (idx < 0)
72 return NULL;
73 else
Dan Williams5076a1a2011-06-27 14:57:03 -070074 return &ireq->sg_table[idx - 2];
Dan Williams6f231dd2011-07-02 22:56:22 -070075}
76
Dan Williamsd9dcb4b2011-06-30 17:38:32 -070077static dma_addr_t to_sgl_element_pair_dma(struct isci_host *ihost,
Dan Williams5076a1a2011-06-27 14:57:03 -070078 struct isci_request *ireq, u32 idx)
Dan Williams312e0c22011-06-28 13:47:09 -070079{
80 u32 offset;
81
82 if (idx == 0) {
Dan Williams5076a1a2011-06-27 14:57:03 -070083 offset = (void *) &ireq->tc->sgl_pair_ab -
Dan Williamsd9dcb4b2011-06-30 17:38:32 -070084 (void *) &ihost->task_context_table[0];
85 return ihost->task_context_dma + offset;
Dan Williams312e0c22011-06-28 13:47:09 -070086 } else if (idx == 1) {
Dan Williams5076a1a2011-06-27 14:57:03 -070087 offset = (void *) &ireq->tc->sgl_pair_cd -
Dan Williamsd9dcb4b2011-06-30 17:38:32 -070088 (void *) &ihost->task_context_table[0];
89 return ihost->task_context_dma + offset;
Dan Williams312e0c22011-06-28 13:47:09 -070090 }
91
Dan Williams5076a1a2011-06-27 14:57:03 -070092 return scic_io_request_get_dma_addr(ireq, &ireq->sg_table[idx - 2]);
Dan Williams312e0c22011-06-28 13:47:09 -070093}
94
95static void init_sgl_element(struct scu_sgl_element *e, struct scatterlist *sg)
96{
97 e->length = sg_dma_len(sg);
98 e->address_upper = upper_32_bits(sg_dma_address(sg));
99 e->address_lower = lower_32_bits(sg_dma_address(sg));
100 e->address_modifier = 0;
101}
102
Dan Williams5076a1a2011-06-27 14:57:03 -0700103static void scic_sds_request_build_sgl(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700104{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700105 struct isci_host *ihost = ireq->isci_host;
Dan Williams5076a1a2011-06-27 14:57:03 -0700106 struct sas_task *task = isci_request_access_task(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700107 struct scatterlist *sg = NULL;
108 dma_addr_t dma_addr;
109 u32 sg_idx = 0;
110 struct scu_sgl_element_pair *scu_sg = NULL;
111 struct scu_sgl_element_pair *prev_sg = NULL;
112
113 if (task->num_scatter > 0) {
114 sg = task->scatter;
115
116 while (sg) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700117 scu_sg = to_sgl_element_pair(ireq, sg_idx);
Dan Williams312e0c22011-06-28 13:47:09 -0700118 init_sgl_element(&scu_sg->A, sg);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700119 sg = sg_next(sg);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700120 if (sg) {
Dan Williams312e0c22011-06-28 13:47:09 -0700121 init_sgl_element(&scu_sg->B, sg);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700122 sg = sg_next(sg);
123 } else
Dan Williams312e0c22011-06-28 13:47:09 -0700124 memset(&scu_sg->B, 0, sizeof(scu_sg->B));
Dan Williamsf1f52e72011-05-10 02:28:45 -0700125
126 if (prev_sg) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700127 dma_addr = to_sgl_element_pair_dma(ihost,
Dan Williams5076a1a2011-06-27 14:57:03 -0700128 ireq,
Dan Williams312e0c22011-06-28 13:47:09 -0700129 sg_idx);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700130
131 prev_sg->next_pair_upper =
132 upper_32_bits(dma_addr);
133 prev_sg->next_pair_lower =
134 lower_32_bits(dma_addr);
135 }
136
137 prev_sg = scu_sg;
138 sg_idx++;
139 }
140 } else { /* handle when no sg */
Dan Williams5076a1a2011-06-27 14:57:03 -0700141 scu_sg = to_sgl_element_pair(ireq, sg_idx);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700142
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700143 dma_addr = dma_map_single(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700144 task->scatter,
145 task->total_xfer_len,
146 task->data_dir);
147
Dan Williams5076a1a2011-06-27 14:57:03 -0700148 ireq->zero_scatter_daddr = dma_addr;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700149
150 scu_sg->A.length = task->total_xfer_len;
151 scu_sg->A.address_upper = upper_32_bits(dma_addr);
152 scu_sg->A.address_lower = lower_32_bits(dma_addr);
153 }
154
155 if (scu_sg) {
156 scu_sg->next_pair_upper = 0;
157 scu_sg->next_pair_lower = 0;
158 }
159}
160
Dan Williams5076a1a2011-06-27 14:57:03 -0700161static void scic_sds_io_request_build_ssp_command_iu(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700162{
163 struct ssp_cmd_iu *cmd_iu;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700164 struct sas_task *task = isci_request_access_task(ireq);
165
Dan Williams5076a1a2011-06-27 14:57:03 -0700166 cmd_iu = &ireq->ssp.cmd;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700167
168 memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
169 cmd_iu->add_cdb_len = 0;
170 cmd_iu->_r_a = 0;
171 cmd_iu->_r_b = 0;
172 cmd_iu->en_fburst = 0; /* unsupported */
173 cmd_iu->task_prio = task->ssp_task.task_prio;
174 cmd_iu->task_attr = task->ssp_task.task_attr;
175 cmd_iu->_r_c = 0;
176
177 sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cdb,
178 sizeof(task->ssp_task.cdb) / sizeof(u32));
179}
180
Dan Williams5076a1a2011-06-27 14:57:03 -0700181static void scic_sds_task_request_build_ssp_task_iu(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700182{
183 struct ssp_task_iu *task_iu;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700184 struct sas_task *task = isci_request_access_task(ireq);
185 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
186
Dan Williams5076a1a2011-06-27 14:57:03 -0700187 task_iu = &ireq->ssp.tmf;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700188
189 memset(task_iu, 0, sizeof(struct ssp_task_iu));
190
191 memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
192
193 task_iu->task_func = isci_tmf->tmf_code;
194 task_iu->task_tag =
195 (ireq->ttype == tmf_task) ?
196 isci_tmf->io_tag :
197 SCI_CONTROLLER_INVALID_IO_TAG;
198}
199
200/**
201 * This method is will fill in the SCU Task Context for any type of SSP request.
202 * @sci_req:
203 * @task_context:
204 *
205 */
206static void scu_ssp_reqeust_construct_task_context(
Dan Williams5076a1a2011-06-27 14:57:03 -0700207 struct isci_request *ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700208 struct scu_task_context *task_context)
209{
210 dma_addr_t dma_addr;
Dan Williams78a6f062011-06-30 16:31:37 -0700211 struct isci_remote_device *idev;
Dan Williamsffe191c2011-06-29 13:09:25 -0700212 struct isci_port *iport;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700213
Dan Williams78a6f062011-06-30 16:31:37 -0700214 idev = scic_sds_request_get_device(ireq);
Dan Williamsffe191c2011-06-29 13:09:25 -0700215 iport = scic_sds_request_get_port(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700216
217 /* Fill in the TC with the its required data */
218 task_context->abort = 0;
219 task_context->priority = 0;
220 task_context->initiator_request = 1;
Dan Williams78a6f062011-06-30 16:31:37 -0700221 task_context->connection_rate = idev->connection_rate;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700222 task_context->protocol_engine_index =
223 scic_sds_controller_get_protocol_engine_group(controller);
Dan Williamsffe191c2011-06-29 13:09:25 -0700224 task_context->logical_port_index = scic_sds_port_get_index(iport);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700225 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
226 task_context->valid = SCU_TASK_CONTEXT_VALID;
227 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
228
Dan Williams78a6f062011-06-30 16:31:37 -0700229 task_context->remote_node_index = scic_sds_remote_device_get_index(idev);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700230 task_context->command_code = 0;
231
232 task_context->link_layer_control = 0;
233 task_context->do_not_dma_ssp_good_response = 1;
234 task_context->strict_ordering = 0;
235 task_context->control_frame = 0;
236 task_context->timeout_enable = 0;
237 task_context->block_guard_enable = 0;
238
239 task_context->address_modifier = 0;
240
Dan Williams5076a1a2011-06-27 14:57:03 -0700241 /* task_context->type.ssp.tag = ireq->io_tag; */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700242 task_context->task_phase = 0x01;
243
Dan Williams5076a1a2011-06-27 14:57:03 -0700244 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williamsffe191c2011-06-29 13:09:25 -0700245 (scic_sds_controller_get_protocol_engine_group(controller) <<
246 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
247 (scic_sds_port_get_index(iport) <<
248 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
249 ISCI_TAG_TCI(ireq->io_tag));
Dan Williamsf1f52e72011-05-10 02:28:45 -0700250
251 /*
252 * Copy the physical address for the command buffer to the
253 * SCU Task Context
254 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700255 dma_addr = scic_io_request_get_dma_addr(ireq, &ireq->ssp.cmd);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700256
257 task_context->command_iu_upper = upper_32_bits(dma_addr);
258 task_context->command_iu_lower = lower_32_bits(dma_addr);
259
260 /*
261 * Copy the physical address for the response buffer to the
262 * SCU Task Context
263 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700264 dma_addr = scic_io_request_get_dma_addr(ireq, &ireq->ssp.rsp);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700265
266 task_context->response_iu_upper = upper_32_bits(dma_addr);
267 task_context->response_iu_lower = lower_32_bits(dma_addr);
268}
269
270/**
271 * This method is will fill in the SCU Task Context for a SSP IO request.
272 * @sci_req:
273 *
274 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700275static void scu_ssp_io_request_construct_task_context(struct isci_request *ireq,
Dan Williams312e0c22011-06-28 13:47:09 -0700276 enum dma_data_direction dir,
277 u32 len)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700278{
Dan Williams5076a1a2011-06-27 14:57:03 -0700279 struct scu_task_context *task_context = ireq->tc;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700280
Dan Williams5076a1a2011-06-27 14:57:03 -0700281 scu_ssp_reqeust_construct_task_context(ireq, task_context);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700282
283 task_context->ssp_command_iu_length =
284 sizeof(struct ssp_cmd_iu) / sizeof(u32);
285 task_context->type.ssp.frame_type = SSP_COMMAND;
286
287 switch (dir) {
288 case DMA_FROM_DEVICE:
289 case DMA_NONE:
290 default:
291 task_context->task_type = SCU_TASK_TYPE_IOREAD;
292 break;
293 case DMA_TO_DEVICE:
294 task_context->task_type = SCU_TASK_TYPE_IOWRITE;
295 break;
296 }
297
298 task_context->transfer_length_bytes = len;
299
300 if (task_context->transfer_length_bytes > 0)
Dan Williams5076a1a2011-06-27 14:57:03 -0700301 scic_sds_request_build_sgl(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700302}
303
Dan Williamsf1f52e72011-05-10 02:28:45 -0700304/**
305 * This method will fill in the SCU Task Context for a SSP Task request. The
306 * following important settings are utilized: -# priority ==
307 * SCU_TASK_PRIORITY_HIGH. This ensures that the task request is issued
308 * ahead of other task destined for the same Remote Node. -# task_type ==
309 * SCU_TASK_TYPE_IOREAD. This simply indicates that a normal request type
310 * (i.e. non-raw frame) is being utilized to perform task management. -#
311 * control_frame == 1. This ensures that the proper endianess is set so
312 * that the bytes are transmitted in the right order for a task frame.
313 * @sci_req: This parameter specifies the task request object being
314 * constructed.
315 *
316 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700317static void scu_ssp_task_request_construct_task_context(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700318{
Dan Williams5076a1a2011-06-27 14:57:03 -0700319 struct scu_task_context *task_context = ireq->tc;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700320
Dan Williams5076a1a2011-06-27 14:57:03 -0700321 scu_ssp_reqeust_construct_task_context(ireq, task_context);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700322
323 task_context->control_frame = 1;
324 task_context->priority = SCU_TASK_PRIORITY_HIGH;
325 task_context->task_type = SCU_TASK_TYPE_RAW_FRAME;
326 task_context->transfer_length_bytes = 0;
327 task_context->type.ssp.frame_type = SSP_TASK;
328 task_context->ssp_command_iu_length =
329 sizeof(struct ssp_task_iu) / sizeof(u32);
330}
331
Dan Williamsf1f52e72011-05-10 02:28:45 -0700332/**
Dan Williams5dec6f42011-05-10 02:28:49 -0700333 * This method is will fill in the SCU Task Context for any type of SATA
334 * request. This is called from the various SATA constructors.
335 * @sci_req: The general IO request object which is to be used in
336 * constructing the SCU task context.
337 * @task_context: The buffer pointer for the SCU task context which is being
338 * constructed.
Dan Williamsf1f52e72011-05-10 02:28:45 -0700339 *
Dan Williams5dec6f42011-05-10 02:28:49 -0700340 * The general io request construction is complete. The buffer assignment for
341 * the command buffer is complete. none Revisit task context construction to
342 * determine what is common for SSP/SMP/STP task context structures.
Dan Williamsf1f52e72011-05-10 02:28:45 -0700343 */
Dan Williams5dec6f42011-05-10 02:28:49 -0700344static void scu_sata_reqeust_construct_task_context(
Dan Williams5076a1a2011-06-27 14:57:03 -0700345 struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -0700346 struct scu_task_context *task_context)
347{
348 dma_addr_t dma_addr;
Dan Williams78a6f062011-06-30 16:31:37 -0700349 struct isci_remote_device *idev;
Dan Williamsffe191c2011-06-29 13:09:25 -0700350 struct isci_port *iport;
Dan Williams5dec6f42011-05-10 02:28:49 -0700351
Dan Williams78a6f062011-06-30 16:31:37 -0700352 idev = scic_sds_request_get_device(ireq);
Dan Williamsffe191c2011-06-29 13:09:25 -0700353 iport = scic_sds_request_get_port(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700354
355 /* Fill in the TC with the its required data */
356 task_context->abort = 0;
357 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
358 task_context->initiator_request = 1;
Dan Williams78a6f062011-06-30 16:31:37 -0700359 task_context->connection_rate = idev->connection_rate;
Dan Williams5dec6f42011-05-10 02:28:49 -0700360 task_context->protocol_engine_index =
361 scic_sds_controller_get_protocol_engine_group(controller);
362 task_context->logical_port_index =
Dan Williamsffe191c2011-06-29 13:09:25 -0700363 scic_sds_port_get_index(iport);
Dan Williams5dec6f42011-05-10 02:28:49 -0700364 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
365 task_context->valid = SCU_TASK_CONTEXT_VALID;
366 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
367
Dan Williams78a6f062011-06-30 16:31:37 -0700368 task_context->remote_node_index = scic_sds_remote_device_get_index(idev);
Dan Williams5dec6f42011-05-10 02:28:49 -0700369 task_context->command_code = 0;
370
371 task_context->link_layer_control = 0;
372 task_context->do_not_dma_ssp_good_response = 1;
373 task_context->strict_ordering = 0;
374 task_context->control_frame = 0;
375 task_context->timeout_enable = 0;
376 task_context->block_guard_enable = 0;
377
378 task_context->address_modifier = 0;
379 task_context->task_phase = 0x01;
380
381 task_context->ssp_command_iu_length =
382 (sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);
383
384 /* Set the first word of the H2D REG FIS */
Dan Williams5076a1a2011-06-27 14:57:03 -0700385 task_context->type.words[0] = *(u32 *)&ireq->stp.cmd;
Dan Williams5dec6f42011-05-10 02:28:49 -0700386
Dan Williams5076a1a2011-06-27 14:57:03 -0700387 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williams312e0c22011-06-28 13:47:09 -0700388 (scic_sds_controller_get_protocol_engine_group(controller) <<
389 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
Dan Williamsffe191c2011-06-29 13:09:25 -0700390 (scic_sds_port_get_index(iport) <<
Dan Williams312e0c22011-06-28 13:47:09 -0700391 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
Dan Williams5076a1a2011-06-27 14:57:03 -0700392 ISCI_TAG_TCI(ireq->io_tag));
Dan Williams5dec6f42011-05-10 02:28:49 -0700393 /*
394 * Copy the physical address for the command buffer to the SCU Task
395 * Context. We must offset the command buffer by 4 bytes because the
396 * first 4 bytes are transfered in the body of the TC.
397 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700398 dma_addr = scic_io_request_get_dma_addr(ireq,
399 ((char *) &ireq->stp.cmd) +
Dan Williams5dec6f42011-05-10 02:28:49 -0700400 sizeof(u32));
401
402 task_context->command_iu_upper = upper_32_bits(dma_addr);
403 task_context->command_iu_lower = lower_32_bits(dma_addr);
404
405 /* SATA Requests do not have a response buffer */
406 task_context->response_iu_upper = 0;
407 task_context->response_iu_lower = 0;
408}
409
Dan Williams5076a1a2011-06-27 14:57:03 -0700410static void scu_stp_raw_request_construct_task_context(struct isci_request *ireq)
Dan Williams5dec6f42011-05-10 02:28:49 -0700411{
Dan Williams5076a1a2011-06-27 14:57:03 -0700412 struct scu_task_context *task_context = ireq->tc;
Dan Williams5dec6f42011-05-10 02:28:49 -0700413
Dan Williams5076a1a2011-06-27 14:57:03 -0700414 scu_sata_reqeust_construct_task_context(ireq, task_context);
Dan Williams5dec6f42011-05-10 02:28:49 -0700415
416 task_context->control_frame = 0;
417 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
418 task_context->task_type = SCU_TASK_TYPE_SATA_RAW_FRAME;
419 task_context->type.stp.fis_type = FIS_REGH2D;
420 task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
421}
422
Dan Williams5076a1a2011-06-27 14:57:03 -0700423static enum sci_status scic_sds_stp_pio_request_construct(struct isci_request *ireq,
424 bool copy_rx_frame)
Dan Williams5dec6f42011-05-10 02:28:49 -0700425{
Dan Williams5076a1a2011-06-27 14:57:03 -0700426 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williams5dec6f42011-05-10 02:28:49 -0700427
Dan Williams5076a1a2011-06-27 14:57:03 -0700428 scu_stp_raw_request_construct_task_context(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700429
Dan Williamsba7cb222011-06-27 11:56:41 -0700430 stp_req->status = 0;
431 stp_req->sgl.offset = 0;
432 stp_req->sgl.set = SCU_SGL_ELEMENT_PAIR_A;
Dan Williams5dec6f42011-05-10 02:28:49 -0700433
434 if (copy_rx_frame) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700435 scic_sds_request_build_sgl(ireq);
Dan Williamsba7cb222011-06-27 11:56:41 -0700436 stp_req->sgl.index = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -0700437 } else {
438 /* The user does not want the data copied to the SGL buffer location */
Dan Williamsba7cb222011-06-27 11:56:41 -0700439 stp_req->sgl.index = -1;
Dan Williams5dec6f42011-05-10 02:28:49 -0700440 }
441
442 return SCI_SUCCESS;
443}
444
445/**
446 *
447 * @sci_req: This parameter specifies the request to be constructed as an
448 * optimized request.
449 * @optimized_task_type: This parameter specifies whether the request is to be
450 * an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
451 * value of 1 indicates NCQ.
452 *
453 * This method will perform request construction common to all types of STP
454 * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
455 * returns an indication as to whether the construction was successful.
456 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700457static void scic_sds_stp_optimized_request_construct(struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -0700458 u8 optimized_task_type,
459 u32 len,
460 enum dma_data_direction dir)
461{
Dan Williams5076a1a2011-06-27 14:57:03 -0700462 struct scu_task_context *task_context = ireq->tc;
Dan Williams5dec6f42011-05-10 02:28:49 -0700463
464 /* Build the STP task context structure */
Dan Williams5076a1a2011-06-27 14:57:03 -0700465 scu_sata_reqeust_construct_task_context(ireq, task_context);
Dan Williams5dec6f42011-05-10 02:28:49 -0700466
467 /* Copy over the SGL elements */
Dan Williams5076a1a2011-06-27 14:57:03 -0700468 scic_sds_request_build_sgl(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700469
470 /* Copy over the number of bytes to be transfered */
471 task_context->transfer_length_bytes = len;
472
473 if (dir == DMA_TO_DEVICE) {
474 /*
475 * The difference between the DMA IN and DMA OUT request task type
476 * values are consistent with the difference between FPDMA READ
477 * and FPDMA WRITE values. Add the supplied task type parameter
478 * to this difference to set the task type properly for this
479 * DATA OUT (WRITE) case. */
480 task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
481 - SCU_TASK_TYPE_DMA_IN);
482 } else {
483 /*
484 * For the DATA IN (READ) case, simply save the supplied
485 * optimized task type. */
486 task_context->task_type = optimized_task_type;
487 }
488}
489
490
491
Dan Williamsf1f52e72011-05-10 02:28:45 -0700492static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -0700493scic_io_request_construct_sata(struct isci_request *ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700494 u32 len,
495 enum dma_data_direction dir,
496 bool copy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700497{
Dan Williams6f231dd2011-07-02 22:56:22 -0700498 enum sci_status status = SCI_SUCCESS;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700499 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700500
Dan Williamsf1f52e72011-05-10 02:28:45 -0700501 /* check for management protocols */
502 if (ireq->ttype == tmf_task) {
503 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700504
Dan Williamsf1f52e72011-05-10 02:28:45 -0700505 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
Dan Williams5dec6f42011-05-10 02:28:49 -0700506 tmf->tmf_code == isci_tmf_sata_srst_low) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700507 scu_stp_raw_request_construct_task_context(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700508 return SCI_SUCCESS;
509 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700510 dev_err(&ireq->owning_controller->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700511 "%s: Request 0x%p received un-handled SAT "
512 "management protocol 0x%x.\n",
Dan Williams5076a1a2011-06-27 14:57:03 -0700513 __func__, ireq, tmf->tmf_code);
Dan Williams6f231dd2011-07-02 22:56:22 -0700514
Dan Williamsf1f52e72011-05-10 02:28:45 -0700515 return SCI_FAILURE;
516 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700517 }
518
Dan Williamsf1f52e72011-05-10 02:28:45 -0700519 if (!sas_protocol_ata(task->task_proto)) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700520 dev_err(&ireq->owning_controller->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700521 "%s: Non-ATA protocol in SATA path: 0x%x\n",
522 __func__,
523 task->task_proto);
Dan Williams6f231dd2011-07-02 22:56:22 -0700524 return SCI_FAILURE;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700525
Dan Williams6f231dd2011-07-02 22:56:22 -0700526 }
527
Dan Williamsf1f52e72011-05-10 02:28:45 -0700528 /* non data */
Dan Williams5dec6f42011-05-10 02:28:49 -0700529 if (task->data_dir == DMA_NONE) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700530 scu_stp_raw_request_construct_task_context(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700531 return SCI_SUCCESS;
532 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700533
534 /* NCQ */
Dan Williams5dec6f42011-05-10 02:28:49 -0700535 if (task->ata_task.use_ncq) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700536 scic_sds_stp_optimized_request_construct(ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -0700537 SCU_TASK_TYPE_FPDMAQ_READ,
538 len, dir);
539 return SCI_SUCCESS;
540 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700541
542 /* DMA */
Dan Williams5dec6f42011-05-10 02:28:49 -0700543 if (task->ata_task.dma_xfer) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700544 scic_sds_stp_optimized_request_construct(ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -0700545 SCU_TASK_TYPE_DMA_IN,
546 len, dir);
547 return SCI_SUCCESS;
548 } else /* PIO */
Dan Williams5076a1a2011-06-27 14:57:03 -0700549 return scic_sds_stp_pio_request_construct(ireq, copy);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700550
551 return status;
552}
553
Dan Williams5076a1a2011-06-27 14:57:03 -0700554static enum sci_status scic_io_request_construct_basic_ssp(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700555{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700556 struct sas_task *task = isci_request_access_task(ireq);
557
Dan Williams5076a1a2011-06-27 14:57:03 -0700558 ireq->protocol = SCIC_SSP_PROTOCOL;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700559
Dan Williams5076a1a2011-06-27 14:57:03 -0700560 scu_ssp_io_request_construct_task_context(ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700561 task->data_dir,
562 task->total_xfer_len);
563
Dan Williams5076a1a2011-06-27 14:57:03 -0700564 scic_sds_io_request_build_ssp_command_iu(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700565
Dan Williams5076a1a2011-06-27 14:57:03 -0700566 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700567
568 return SCI_SUCCESS;
569}
570
571enum sci_status scic_task_request_construct_ssp(
Dan Williams5076a1a2011-06-27 14:57:03 -0700572 struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700573{
574 /* Construct the SSP Task SCU Task Context */
Dan Williams5076a1a2011-06-27 14:57:03 -0700575 scu_ssp_task_request_construct_task_context(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700576
577 /* Fill in the SSP Task IU */
Dan Williams5076a1a2011-06-27 14:57:03 -0700578 scic_sds_task_request_build_ssp_task_iu(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700579
Dan Williams5076a1a2011-06-27 14:57:03 -0700580 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williams6f231dd2011-07-02 22:56:22 -0700581
582 return SCI_SUCCESS;
583}
584
Dan Williams5076a1a2011-06-27 14:57:03 -0700585static enum sci_status scic_io_request_construct_basic_sata(struct isci_request *ireq)
Dan Williams6f231dd2011-07-02 22:56:22 -0700586{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700587 enum sci_status status;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700588 bool copy = false;
Dan Williams5076a1a2011-06-27 14:57:03 -0700589 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700590
Dan Williams5076a1a2011-06-27 14:57:03 -0700591 ireq->protocol = SCIC_STP_PROTOCOL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700592
Dan Williamsf1f52e72011-05-10 02:28:45 -0700593 copy = (task->data_dir == DMA_NONE) ? false : true;
Dan Williams6f231dd2011-07-02 22:56:22 -0700594
Dan Williams5076a1a2011-06-27 14:57:03 -0700595 status = scic_io_request_construct_sata(ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700596 task->total_xfer_len,
597 task->data_dir,
598 copy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700599
Dan Williamsf1f52e72011-05-10 02:28:45 -0700600 if (status == SCI_SUCCESS)
Dan Williams5076a1a2011-06-27 14:57:03 -0700601 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williams6f231dd2011-07-02 22:56:22 -0700602
Dan Williamsf1f52e72011-05-10 02:28:45 -0700603 return status;
Dan Williams6f231dd2011-07-02 22:56:22 -0700604}
605
Dan Williams5076a1a2011-06-27 14:57:03 -0700606enum sci_status scic_task_request_construct_sata(struct isci_request *ireq)
Dan Williams6f231dd2011-07-02 22:56:22 -0700607{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700608 enum sci_status status = SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700609
Dan Williamsf1f52e72011-05-10 02:28:45 -0700610 /* check for management protocols */
611 if (ireq->ttype == tmf_task) {
612 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700613
Dan Williamsf1f52e72011-05-10 02:28:45 -0700614 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
615 tmf->tmf_code == isci_tmf_sata_srst_low) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700616 scu_stp_raw_request_construct_task_context(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700617 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700618 dev_err(&ireq->owning_controller->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700619 "%s: Request 0x%p received un-handled SAT "
620 "Protocol 0x%x.\n",
Dan Williams5076a1a2011-06-27 14:57:03 -0700621 __func__, ireq, tmf->tmf_code);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700622
623 return SCI_FAILURE;
624 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700625 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700626
Dan Williams5dec6f42011-05-10 02:28:49 -0700627 if (status != SCI_SUCCESS)
628 return status;
Dan Williams5076a1a2011-06-27 14:57:03 -0700629 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700630
631 return status;
Dan Williams6f231dd2011-07-02 22:56:22 -0700632}
633
634/**
Dan Williamsf1f52e72011-05-10 02:28:45 -0700635 * sci_req_tx_bytes - bytes transferred when reply underruns request
636 * @sci_req: request that was terminated early
Dan Williams6f231dd2011-07-02 22:56:22 -0700637 */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700638#define SCU_TASK_CONTEXT_SRAM 0x200000
Dan Williams5076a1a2011-06-27 14:57:03 -0700639static u32 sci_req_tx_bytes(struct isci_request *ireq)
Dan Williams6f231dd2011-07-02 22:56:22 -0700640{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700641 struct isci_host *ihost = ireq->owning_controller;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700642 u32 ret_val = 0;
Dan Williams6f231dd2011-07-02 22:56:22 -0700643
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700644 if (readl(&ihost->smu_registers->address_modifier) == 0) {
645 void __iomem *scu_reg_base = ihost->scu_registers;
Dan Williams6f231dd2011-07-02 22:56:22 -0700646
Dan Williamsf1f52e72011-05-10 02:28:45 -0700647 /* get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
648 * BAR1 is the scu_registers
649 * 0x20002C = 0x200000 + 0x2c
650 * = start of task context SRAM + offset of (type.ssp.data_offset)
651 * TCi is the io_tag of struct scic_sds_request
Dan Williams67ea8382011-05-08 11:47:15 -0700652 */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700653 ret_val = readl(scu_reg_base +
654 (SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
Dan Williams5076a1a2011-06-27 14:57:03 -0700655 ((sizeof(struct scu_task_context)) * ISCI_TAG_TCI(ireq->io_tag)));
Dan Williams67ea8382011-05-08 11:47:15 -0700656 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700657
Dan Williamsf1f52e72011-05-10 02:28:45 -0700658 return ret_val;
Dan Williams6f231dd2011-07-02 22:56:22 -0700659}
660
Dan Williams5076a1a2011-06-27 14:57:03 -0700661enum sci_status scic_sds_request_start(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700662{
Piotr Sawickif4636a72011-05-10 23:50:32 +0000663 enum sci_base_request_states state;
Dan Williams5076a1a2011-06-27 14:57:03 -0700664 struct scu_task_context *tc = ireq->tc;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700665 struct isci_host *ihost = ireq->owning_controller;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000666
Dan Williams5076a1a2011-06-27 14:57:03 -0700667 state = ireq->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000668 if (state != SCI_REQ_CONSTRUCTED) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700669 dev_warn(&ihost->pdev->dev,
Piotr Sawickif4636a72011-05-10 23:50:32 +0000670 "%s: SCIC IO Request requested to start while in wrong "
671 "state %d\n", __func__, state);
672 return SCI_FAILURE_INVALID_STATE;
673 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700674
Dan Williams5076a1a2011-06-27 14:57:03 -0700675 tc->task_index = ISCI_TAG_TCI(ireq->io_tag);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700676
Dan Williams312e0c22011-06-28 13:47:09 -0700677 switch (tc->protocol_type) {
678 case SCU_TASK_CONTEXT_PROTOCOL_SMP:
679 case SCU_TASK_CONTEXT_PROTOCOL_SSP:
680 /* SSP/SMP Frame */
Dan Williams5076a1a2011-06-27 14:57:03 -0700681 tc->type.ssp.tag = ireq->io_tag;
Dan Williams312e0c22011-06-28 13:47:09 -0700682 tc->type.ssp.target_port_transfer_tag = 0xFFFF;
683 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000684
Dan Williams312e0c22011-06-28 13:47:09 -0700685 case SCU_TASK_CONTEXT_PROTOCOL_STP:
686 /* STP/SATA Frame
Dan Williams5076a1a2011-06-27 14:57:03 -0700687 * tc->type.stp.ncq_tag = ireq->ncq_tag;
Dan Williams312e0c22011-06-28 13:47:09 -0700688 */
689 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000690
Dan Williams312e0c22011-06-28 13:47:09 -0700691 case SCU_TASK_CONTEXT_PROTOCOL_NONE:
692 /* / @todo When do we set no protocol type? */
693 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000694
Dan Williams312e0c22011-06-28 13:47:09 -0700695 default:
696 /* This should never happen since we build the IO
697 * requests */
698 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000699 }
700
Dan Williams312e0c22011-06-28 13:47:09 -0700701 /* Add to the post_context the io tag value */
Dan Williams5076a1a2011-06-27 14:57:03 -0700702 ireq->post_context |= ISCI_TAG_TCI(ireq->io_tag);
Dan Williams312e0c22011-06-28 13:47:09 -0700703
704 /* Everything is good go ahead and change state */
Dan Williams5076a1a2011-06-27 14:57:03 -0700705 sci_change_state(&ireq->sm, SCI_REQ_STARTED);
Dan Williams312e0c22011-06-28 13:47:09 -0700706
707 return SCI_SUCCESS;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700708}
709
710enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -0700711scic_sds_io_request_terminate(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700712{
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700713 enum sci_base_request_states state;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700714
Dan Williams5076a1a2011-06-27 14:57:03 -0700715 state = ireq->sm.current_state_id;
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700716
717 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000718 case SCI_REQ_CONSTRUCTED:
Dan Williams5076a1a2011-06-27 14:57:03 -0700719 scic_sds_request_set_status(ireq,
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700720 SCU_TASK_DONE_TASK_ABORT,
721 SCI_FAILURE_IO_TERMINATED);
722
Dan Williams5076a1a2011-06-27 14:57:03 -0700723 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700724 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000725 case SCI_REQ_STARTED:
726 case SCI_REQ_TASK_WAIT_TC_COMP:
727 case SCI_REQ_SMP_WAIT_RESP:
728 case SCI_REQ_SMP_WAIT_TC_COMP:
729 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
730 case SCI_REQ_STP_UDMA_WAIT_D2H:
731 case SCI_REQ_STP_NON_DATA_WAIT_H2D:
732 case SCI_REQ_STP_NON_DATA_WAIT_D2H:
733 case SCI_REQ_STP_PIO_WAIT_H2D:
734 case SCI_REQ_STP_PIO_WAIT_FRAME:
735 case SCI_REQ_STP_PIO_DATA_IN:
736 case SCI_REQ_STP_PIO_DATA_OUT:
737 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
738 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
739 case SCI_REQ_STP_SOFT_RESET_WAIT_D2H:
Dan Williams5076a1a2011-06-27 14:57:03 -0700740 sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700741 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000742 case SCI_REQ_TASK_WAIT_TC_RESP:
Dan Williams5076a1a2011-06-27 14:57:03 -0700743 sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
744 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700745 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000746 case SCI_REQ_ABORTING:
Dan Williams5076a1a2011-06-27 14:57:03 -0700747 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700748 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000749 case SCI_REQ_COMPLETED:
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700750 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700751 dev_warn(&ireq->owning_controller->pdev->dev,
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700752 "%s: SCIC IO Request requested to abort while in wrong "
753 "state %d\n",
754 __func__,
Dan Williams5076a1a2011-06-27 14:57:03 -0700755 ireq->sm.current_state_id);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700756 break;
757 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700758
759 return SCI_FAILURE_INVALID_STATE;
760}
761
Dan Williams5076a1a2011-06-27 14:57:03 -0700762enum sci_status scic_sds_request_complete(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700763{
Dan Williams79e2b6b2011-05-11 08:29:56 -0700764 enum sci_base_request_states state;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700765 struct isci_host *ihost = ireq->owning_controller;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700766
Dan Williams5076a1a2011-06-27 14:57:03 -0700767 state = ireq->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000768 if (WARN_ONCE(state != SCI_REQ_COMPLETED,
Dan Williams79e2b6b2011-05-11 08:29:56 -0700769 "isci: request completion from wrong state (%d)\n", state))
770 return SCI_FAILURE_INVALID_STATE;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700771
Dan Williams5076a1a2011-06-27 14:57:03 -0700772 if (ireq->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700773 scic_sds_controller_release_frame(ihost,
Dan Williams5076a1a2011-06-27 14:57:03 -0700774 ireq->saved_rx_frame_index);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700775
776 /* XXX can we just stop the machine and remove the 'final' state? */
Dan Williams5076a1a2011-06-27 14:57:03 -0700777 sci_change_state(&ireq->sm, SCI_REQ_FINAL);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700778 return SCI_SUCCESS;
779}
780
Dan Williams5076a1a2011-06-27 14:57:03 -0700781enum sci_status scic_sds_io_request_event_handler(struct isci_request *ireq,
Dan Williams79e2b6b2011-05-11 08:29:56 -0700782 u32 event_code)
783{
784 enum sci_base_request_states state;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700785 struct isci_host *ihost = ireq->owning_controller;
Dan Williams79e2b6b2011-05-11 08:29:56 -0700786
Dan Williams5076a1a2011-06-27 14:57:03 -0700787 state = ireq->sm.current_state_id;
Dan Williams79e2b6b2011-05-11 08:29:56 -0700788
Edmund Nadolskie3013702011-06-02 00:10:43 +0000789 if (state != SCI_REQ_STP_PIO_DATA_IN) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700790 dev_warn(&ihost->pdev->dev, "%s: (%x) in wrong state %d\n",
Dan Williams79e2b6b2011-05-11 08:29:56 -0700791 __func__, event_code, state);
792
793 return SCI_FAILURE_INVALID_STATE;
794 }
795
796 switch (scu_get_event_specifier(event_code)) {
797 case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
798 /* We are waiting for data and the SCU has R_ERR the data frame.
799 * Go back to waiting for the D2H Register FIS
800 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700801 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700802 return SCI_SUCCESS;
803 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700804 dev_err(&ihost->pdev->dev,
Dan Williams79e2b6b2011-05-11 08:29:56 -0700805 "%s: pio request unexpected event %#x\n",
806 __func__, event_code);
807
808 /* TODO Should we fail the PIO request when we get an
809 * unexpected event?
810 */
811 return SCI_FAILURE;
812 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700813}
814
Dan Williamsf1f52e72011-05-10 02:28:45 -0700815/*
816 * This function copies response data for requests returning response data
817 * instead of sense data.
818 * @sci_req: This parameter specifies the request object for which to copy
819 * the response data.
820 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700821static void scic_sds_io_request_copy_response(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700822{
823 void *resp_buf;
824 u32 len;
825 struct ssp_response_iu *ssp_response;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700826 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
827
Dan Williams5076a1a2011-06-27 14:57:03 -0700828 ssp_response = &ireq->ssp.rsp;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700829
830 resp_buf = &isci_tmf->resp.resp_iu;
831
832 len = min_t(u32,
833 SSP_RESP_IU_MAX_SIZE,
834 be32_to_cpu(ssp_response->response_data_len));
835
836 memcpy(resp_buf, ssp_response->resp_data, len);
837}
838
Edmund Nadolskie3013702011-06-02 00:10:43 +0000839static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -0700840request_started_state_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000841 u32 completion_code)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700842{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700843 struct ssp_response_iu *resp_iu;
Dan Williamsa7e255a2011-05-11 08:27:47 -0700844 u8 datapres;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700845
Dan Williamsa7e255a2011-05-11 08:27:47 -0700846 /* TODO: Any SDMA return code of other than 0 is bad decode 0x003C0000
847 * to determine SDMA status
Dan Williamsf1f52e72011-05-10 02:28:45 -0700848 */
849 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
850 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5076a1a2011-06-27 14:57:03 -0700851 scic_sds_request_set_status(ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700852 SCU_TASK_DONE_GOOD,
853 SCI_SUCCESS);
854 break;
Dan Williamsa7e255a2011-05-11 08:27:47 -0700855 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP): {
856 /* There are times when the SCU hardware will return an early
Dan Williamsf1f52e72011-05-10 02:28:45 -0700857 * response because the io request specified more data than is
858 * returned by the target device (mode pages, inquiry data,
859 * etc.). We must check the response stats to see if this is
860 * truly a failed request or a good request that just got
861 * completed early.
862 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700863 struct ssp_response_iu *resp = &ireq->ssp.rsp;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700864 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
865
Dan Williams5076a1a2011-06-27 14:57:03 -0700866 sci_swab32_cpy(&ireq->ssp.rsp,
867 &ireq->ssp.rsp,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700868 word_cnt);
869
870 if (resp->status == 0) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700871 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -0700872 SCU_TASK_DONE_GOOD,
873 SCI_SUCCESS_IO_DONE_EARLY);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700874 } else {
Dan Williams5076a1a2011-06-27 14:57:03 -0700875 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -0700876 SCU_TASK_DONE_CHECK_RESPONSE,
877 SCI_FAILURE_IO_RESPONSE_VALID);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700878 }
Dan Williamsa7e255a2011-05-11 08:27:47 -0700879 break;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700880 }
Dan Williamsa7e255a2011-05-11 08:27:47 -0700881 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE): {
Dan Williamsf1f52e72011-05-10 02:28:45 -0700882 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
883
Dan Williams5076a1a2011-06-27 14:57:03 -0700884 sci_swab32_cpy(&ireq->ssp.rsp,
885 &ireq->ssp.rsp,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700886 word_cnt);
887
Dan Williams5076a1a2011-06-27 14:57:03 -0700888 scic_sds_request_set_status(ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700889 SCU_TASK_DONE_CHECK_RESPONSE,
890 SCI_FAILURE_IO_RESPONSE_VALID);
891 break;
892 }
893
894 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -0700895 /* TODO With TASK_DONE_RESP_LEN_ERR is the response frame
Dan Williamsf1f52e72011-05-10 02:28:45 -0700896 * guaranteed to be received before this completion status is
897 * posted?
898 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700899 resp_iu = &ireq->ssp.rsp;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700900 datapres = resp_iu->datapres;
901
Dan Williamsa7e255a2011-05-11 08:27:47 -0700902 if (datapres == 1 || datapres == 2) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700903 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -0700904 SCU_TASK_DONE_CHECK_RESPONSE,
905 SCI_FAILURE_IO_RESPONSE_VALID);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700906 } else
Dan Williams5076a1a2011-06-27 14:57:03 -0700907 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -0700908 SCU_TASK_DONE_GOOD,
909 SCI_SUCCESS);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700910 break;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700911 /* only stp device gets suspended. */
912 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
913 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
914 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
915 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
916 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
917 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
918 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
919 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
920 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
921 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
922 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
Dan Williams5076a1a2011-06-27 14:57:03 -0700923 if (ireq->protocol == SCIC_STP_PROTOCOL) {
924 scic_sds_request_set_status(ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700925 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
926 SCU_COMPLETION_TL_STATUS_SHIFT,
927 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
928 } else {
Dan Williams5076a1a2011-06-27 14:57:03 -0700929 scic_sds_request_set_status(ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700930 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
931 SCU_COMPLETION_TL_STATUS_SHIFT,
932 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
933 }
934 break;
935
936 /* both stp/ssp device gets suspended */
937 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
938 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
939 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
940 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
941 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
942 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
943 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
944 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
945 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
946 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
Dan Williams5076a1a2011-06-27 14:57:03 -0700947 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -0700948 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
949 SCU_COMPLETION_TL_STATUS_SHIFT,
950 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700951 break;
952
953 /* neither ssp nor stp gets suspended. */
954 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
955 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
956 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
957 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
958 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
959 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
960 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
961 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
962 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
963 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
964 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
965 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
966 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
967 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
968 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
969 default:
970 scic_sds_request_set_status(
Dan Williams5076a1a2011-06-27 14:57:03 -0700971 ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700972 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
973 SCU_COMPLETION_TL_STATUS_SHIFT,
974 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
975 break;
976 }
977
978 /*
979 * TODO: This is probably wrong for ACK/NAK timeout conditions
980 */
981
982 /* In all cases we will treat this as the completion of the IO req. */
Dan Williams5076a1a2011-06-27 14:57:03 -0700983 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700984 return SCI_SUCCESS;
985}
986
Edmund Nadolskie3013702011-06-02 00:10:43 +0000987static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -0700988request_aborting_state_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000989 u32 completion_code)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700990{
991 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
992 case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
993 case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
Dan Williams5076a1a2011-06-27 14:57:03 -0700994 scic_sds_request_set_status(ireq, SCU_TASK_DONE_TASK_ABORT,
Dan Williamsa7e255a2011-05-11 08:27:47 -0700995 SCI_FAILURE_IO_TERMINATED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700996
Dan Williams5076a1a2011-06-27 14:57:03 -0700997 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700998 break;
999
1000 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001001 /* Unless we get some strange error wait for the task abort to complete
1002 * TODO: Should there be a state change for this completion?
1003 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07001004 break;
1005 }
1006
1007 return SCI_SUCCESS;
1008}
1009
Dan Williams5076a1a2011-06-27 14:57:03 -07001010static enum sci_status ssp_task_request_await_tc_event(struct isci_request *ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001011 u32 completion_code)
Dan Williamsf1393032011-05-10 02:28:47 -07001012{
1013 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1014 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5076a1a2011-06-27 14:57:03 -07001015 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
Dan Williamsf1393032011-05-10 02:28:47 -07001016 SCI_SUCCESS);
1017
Dan Williams5076a1a2011-06-27 14:57:03 -07001018 sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
Dan Williamsf1393032011-05-10 02:28:47 -07001019 break;
Dan Williamsf1393032011-05-10 02:28:47 -07001020 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001021 /* Currently, the decision is to simply allow the task request
1022 * to timeout if the task IU wasn't received successfully.
1023 * There is a potential for receiving multiple task responses if
1024 * we decide to send the task IU again.
1025 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001026 dev_warn(&ireq->owning_controller->pdev->dev,
Dan Williamsf1393032011-05-10 02:28:47 -07001027 "%s: TaskRequest:0x%p CompletionCode:%x - "
Dan Williams5076a1a2011-06-27 14:57:03 -07001028 "ACK/NAK timeout\n", __func__, ireq,
Dan Williamsf1393032011-05-10 02:28:47 -07001029 completion_code);
1030
Dan Williams5076a1a2011-06-27 14:57:03 -07001031 sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
Dan Williamsf1393032011-05-10 02:28:47 -07001032 break;
Dan Williamsf1393032011-05-10 02:28:47 -07001033 default:
Edmund Nadolskie3013702011-06-02 00:10:43 +00001034 /*
1035 * All other completion status cause the IO to be complete.
1036 * If a NAK was received, then it is up to the user to retry
1037 * the request.
Dan Williamsa7e255a2011-05-11 08:27:47 -07001038 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001039 scic_sds_request_set_status(ireq,
Dan Williamsf1393032011-05-10 02:28:47 -07001040 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
Dan Williamsa7e255a2011-05-11 08:27:47 -07001041 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williamsf1393032011-05-10 02:28:47 -07001042
Dan Williams5076a1a2011-06-27 14:57:03 -07001043 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf1393032011-05-10 02:28:47 -07001044 break;
1045 }
1046
1047 return SCI_SUCCESS;
1048}
1049
Edmund Nadolskie3013702011-06-02 00:10:43 +00001050static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001051smp_request_await_response_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001052 u32 completion_code)
Dan Williamsc72086e2011-05-10 02:28:48 -07001053{
1054 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1055 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001056 /* In the AWAIT RESPONSE state, any TC completion is
1057 * unexpected. but if the TC has success status, we
1058 * complete the IO anyway.
1059 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001060 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
Dan Williams5dec6f42011-05-10 02:28:49 -07001061 SCI_SUCCESS);
Dan Williamsc72086e2011-05-10 02:28:48 -07001062
Dan Williams5076a1a2011-06-27 14:57:03 -07001063 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001064 break;
1065
1066 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1067 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1068 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1069 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001070 /* These status has been seen in a specific LSI
1071 * expander, which sometimes is not able to send smp
1072 * response within 2 ms. This causes our hardware break
1073 * the connection and set TC completion with one of
1074 * these SMP_XXX_XX_ERR status. For these type of error,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001075 * we ask ihost user to retry the request.
Dan Williamsa7e255a2011-05-11 08:27:47 -07001076 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001077 scic_sds_request_set_status(ireq, SCU_TASK_DONE_SMP_RESP_TO_ERR,
Dan Williams5dec6f42011-05-10 02:28:49 -07001078 SCI_FAILURE_RETRY_REQUIRED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001079
Dan Williams5076a1a2011-06-27 14:57:03 -07001080 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001081 break;
1082
1083 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001084 /* All other completion status cause the IO to be complete. If a NAK
1085 * was received, then it is up to the user to retry the request
1086 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001087 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001088 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1089 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williamsc72086e2011-05-10 02:28:48 -07001090
Dan Williams5076a1a2011-06-27 14:57:03 -07001091 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001092 break;
1093 }
1094
1095 return SCI_SUCCESS;
1096}
1097
Edmund Nadolskie3013702011-06-02 00:10:43 +00001098static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001099smp_request_await_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001100 u32 completion_code)
Dan Williamsc72086e2011-05-10 02:28:48 -07001101{
1102 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1103 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5076a1a2011-06-27 14:57:03 -07001104 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
Dan Williams5dec6f42011-05-10 02:28:49 -07001105 SCI_SUCCESS);
Dan Williamsc72086e2011-05-10 02:28:48 -07001106
Dan Williams5076a1a2011-06-27 14:57:03 -07001107 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001108 break;
Dan Williamsc72086e2011-05-10 02:28:48 -07001109 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001110 /* All other completion status cause the IO to be
1111 * complete. If a NAK was received, then it is up to
1112 * the user to retry the request.
1113 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001114 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001115 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1116 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williamsc72086e2011-05-10 02:28:48 -07001117
Dan Williams5076a1a2011-06-27 14:57:03 -07001118 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001119 break;
1120 }
1121
1122 return SCI_SUCCESS;
1123}
1124
Dan Williams5076a1a2011-06-27 14:57:03 -07001125void scic_stp_io_request_set_ncq_tag(struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001126 u16 ncq_tag)
1127{
1128 /**
1129 * @note This could be made to return an error to the user if the user
1130 * attempts to set the NCQ tag in the wrong state.
1131 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001132 ireq->tc->type.stp.ncq_tag = ncq_tag;
Dan Williams5dec6f42011-05-10 02:28:49 -07001133}
1134
Dan Williamsba7cb222011-06-27 11:56:41 -07001135static struct scu_sgl_element *pio_sgl_next(struct isci_stp_request *stp_req)
Dan Williams5dec6f42011-05-10 02:28:49 -07001136{
Dan Williams312e0c22011-06-28 13:47:09 -07001137 struct scu_sgl_element *sgl;
1138 struct scu_sgl_element_pair *sgl_pair;
Dan Williams5076a1a2011-06-27 14:57:03 -07001139 struct isci_request *ireq = to_ireq(stp_req);
Dan Williamsba7cb222011-06-27 11:56:41 -07001140 struct isci_stp_pio_sgl *pio_sgl = &stp_req->sgl;
Dan Williams5dec6f42011-05-10 02:28:49 -07001141
Dan Williams5076a1a2011-06-27 14:57:03 -07001142 sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
Dan Williams312e0c22011-06-28 13:47:09 -07001143 if (!sgl_pair)
1144 sgl = NULL;
Dan Williamsba7cb222011-06-27 11:56:41 -07001145 else if (pio_sgl->set == SCU_SGL_ELEMENT_PAIR_A) {
Dan Williams312e0c22011-06-28 13:47:09 -07001146 if (sgl_pair->B.address_lower == 0 &&
1147 sgl_pair->B.address_upper == 0) {
1148 sgl = NULL;
Dan Williams5dec6f42011-05-10 02:28:49 -07001149 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001150 pio_sgl->set = SCU_SGL_ELEMENT_PAIR_B;
Dan Williams312e0c22011-06-28 13:47:09 -07001151 sgl = &sgl_pair->B;
Dan Williams5dec6f42011-05-10 02:28:49 -07001152 }
1153 } else {
Dan Williams312e0c22011-06-28 13:47:09 -07001154 if (sgl_pair->next_pair_lower == 0 &&
1155 sgl_pair->next_pair_upper == 0) {
1156 sgl = NULL;
Dan Williams5dec6f42011-05-10 02:28:49 -07001157 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001158 pio_sgl->index++;
1159 pio_sgl->set = SCU_SGL_ELEMENT_PAIR_A;
Dan Williams5076a1a2011-06-27 14:57:03 -07001160 sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
Dan Williams312e0c22011-06-28 13:47:09 -07001161 sgl = &sgl_pair->A;
Dan Williams5dec6f42011-05-10 02:28:49 -07001162 }
1163 }
1164
Dan Williams312e0c22011-06-28 13:47:09 -07001165 return sgl;
Dan Williams5dec6f42011-05-10 02:28:49 -07001166}
1167
Edmund Nadolskie3013702011-06-02 00:10:43 +00001168static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001169stp_request_non_data_await_h2d_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001170 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001171{
1172 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1173 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5076a1a2011-06-27 14:57:03 -07001174 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001175 SCI_SUCCESS);
Dan Williams5dec6f42011-05-10 02:28:49 -07001176
Dan Williams5076a1a2011-06-27 14:57:03 -07001177 sci_change_state(&ireq->sm, SCI_REQ_STP_NON_DATA_WAIT_D2H);
Dan Williams5dec6f42011-05-10 02:28:49 -07001178 break;
1179
1180 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001181 /* All other completion status cause the IO to be
1182 * complete. If a NAK was received, then it is up to
1183 * the user to retry the request.
1184 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001185 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001186 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1187 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williams5dec6f42011-05-10 02:28:49 -07001188
Dan Williams5076a1a2011-06-27 14:57:03 -07001189 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001190 break;
1191 }
1192
1193 return SCI_SUCCESS;
1194}
1195
Dan Williams5dec6f42011-05-10 02:28:49 -07001196#define SCU_MAX_FRAME_BUFFER_SIZE 0x400 /* 1K is the maximum SCU frame data payload */
1197
1198/* transmit DATA_FIS from (current sgl + offset) for input
1199 * parameter length. current sgl and offset is alreay stored in the IO request
1200 */
1201static enum sci_status scic_sds_stp_request_pio_data_out_trasmit_data_frame(
Dan Williams5076a1a2011-06-27 14:57:03 -07001202 struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001203 u32 length)
1204{
Dan Williams5076a1a2011-06-27 14:57:03 -07001205 struct isci_stp_request *stp_req = &ireq->stp.req;
1206 struct scu_task_context *task_context = ireq->tc;
Dan Williams312e0c22011-06-28 13:47:09 -07001207 struct scu_sgl_element_pair *sgl_pair;
Dan Williams5dec6f42011-05-10 02:28:49 -07001208 struct scu_sgl_element *current_sgl;
1209
1210 /* Recycle the TC and reconstruct it for sending out DATA FIS containing
1211 * for the data from current_sgl+offset for the input length
1212 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001213 sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
Dan Williamsba7cb222011-06-27 11:56:41 -07001214 if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A)
Dan Williams312e0c22011-06-28 13:47:09 -07001215 current_sgl = &sgl_pair->A;
Dan Williams5dec6f42011-05-10 02:28:49 -07001216 else
Dan Williams312e0c22011-06-28 13:47:09 -07001217 current_sgl = &sgl_pair->B;
Dan Williams5dec6f42011-05-10 02:28:49 -07001218
1219 /* update the TC */
1220 task_context->command_iu_upper = current_sgl->address_upper;
1221 task_context->command_iu_lower = current_sgl->address_lower;
1222 task_context->transfer_length_bytes = length;
1223 task_context->type.stp.fis_type = FIS_DATA;
1224
1225 /* send the new TC out. */
Dan Williams5076a1a2011-06-27 14:57:03 -07001226 return scic_controller_continue_io(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -07001227}
1228
Dan Williams5076a1a2011-06-27 14:57:03 -07001229static enum sci_status scic_sds_stp_request_pio_data_out_transmit_data(struct isci_request *ireq)
Dan Williams5dec6f42011-05-10 02:28:49 -07001230{
Dan Williams5076a1a2011-06-27 14:57:03 -07001231 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williams312e0c22011-06-28 13:47:09 -07001232 struct scu_sgl_element_pair *sgl_pair;
Dan Williamsba7cb222011-06-27 11:56:41 -07001233 struct scu_sgl_element *sgl;
1234 enum sci_status status;
1235 u32 offset;
1236 u32 len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001237
Dan Williamsba7cb222011-06-27 11:56:41 -07001238 offset = stp_req->sgl.offset;
Dan Williams5076a1a2011-06-27 14:57:03 -07001239 sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
Dan Williams312e0c22011-06-28 13:47:09 -07001240 if (WARN_ONCE(!sgl_pair, "%s: null sgl element", __func__))
1241 return SCI_FAILURE;
Dan Williams5dec6f42011-05-10 02:28:49 -07001242
Dan Williamsba7cb222011-06-27 11:56:41 -07001243 if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A) {
1244 sgl = &sgl_pair->A;
1245 len = sgl_pair->A.length - offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001246 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001247 sgl = &sgl_pair->B;
1248 len = sgl_pair->B.length - offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001249 }
1250
Dan Williamsba7cb222011-06-27 11:56:41 -07001251 if (stp_req->pio_len == 0)
1252 return SCI_SUCCESS;
Dan Williams5dec6f42011-05-10 02:28:49 -07001253
Dan Williamsba7cb222011-06-27 11:56:41 -07001254 if (stp_req->pio_len >= len) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001255 status = scic_sds_stp_request_pio_data_out_trasmit_data_frame(ireq, len);
Dan Williamsba7cb222011-06-27 11:56:41 -07001256 if (status != SCI_SUCCESS)
1257 return status;
1258 stp_req->pio_len -= len;
Dan Williams5dec6f42011-05-10 02:28:49 -07001259
Dan Williamsba7cb222011-06-27 11:56:41 -07001260 /* update the current sgl, offset and save for future */
1261 sgl = pio_sgl_next(stp_req);
1262 offset = 0;
1263 } else if (stp_req->pio_len < len) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001264 scic_sds_stp_request_pio_data_out_trasmit_data_frame(ireq, stp_req->pio_len);
Dan Williamsba7cb222011-06-27 11:56:41 -07001265
1266 /* Sgl offset will be adjusted and saved for future */
1267 offset += stp_req->pio_len;
1268 sgl->address_lower += stp_req->pio_len;
1269 stp_req->pio_len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001270 }
1271
Dan Williamsba7cb222011-06-27 11:56:41 -07001272 stp_req->sgl.offset = offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001273
1274 return status;
1275}
1276
1277/**
1278 *
1279 * @stp_request: The request that is used for the SGL processing.
1280 * @data_buffer: The buffer of data to be copied.
1281 * @length: The length of the data transfer.
1282 *
1283 * Copy the data from the buffer for the length specified to the IO reqeust SGL
1284 * specified data region. enum sci_status
1285 */
1286static enum sci_status
Dan Williamsba7cb222011-06-27 11:56:41 -07001287scic_sds_stp_request_pio_data_in_copy_data_buffer(struct isci_stp_request *stp_req,
Dan Williams5dec6f42011-05-10 02:28:49 -07001288 u8 *data_buf, u32 len)
1289{
Dan Williams5dec6f42011-05-10 02:28:49 -07001290 struct isci_request *ireq;
1291 u8 *src_addr;
1292 int copy_len;
1293 struct sas_task *task;
1294 struct scatterlist *sg;
1295 void *kaddr;
1296 int total_len = len;
1297
Dan Williams5076a1a2011-06-27 14:57:03 -07001298 ireq = to_ireq(stp_req);
Dan Williams5dec6f42011-05-10 02:28:49 -07001299 task = isci_request_access_task(ireq);
1300 src_addr = data_buf;
1301
1302 if (task->num_scatter > 0) {
1303 sg = task->scatter;
1304
1305 while (total_len > 0) {
1306 struct page *page = sg_page(sg);
1307
1308 copy_len = min_t(int, total_len, sg_dma_len(sg));
1309 kaddr = kmap_atomic(page, KM_IRQ0);
1310 memcpy(kaddr + sg->offset, src_addr, copy_len);
1311 kunmap_atomic(kaddr, KM_IRQ0);
1312 total_len -= copy_len;
1313 src_addr += copy_len;
1314 sg = sg_next(sg);
1315 }
1316 } else {
1317 BUG_ON(task->total_xfer_len < total_len);
1318 memcpy(task->scatter, src_addr, total_len);
1319 }
1320
1321 return SCI_SUCCESS;
1322}
1323
1324/**
1325 *
1326 * @sci_req: The PIO DATA IN request that is to receive the data.
1327 * @data_buffer: The buffer to copy from.
1328 *
1329 * Copy the data buffer to the io request data region. enum sci_status
1330 */
1331static enum sci_status scic_sds_stp_request_pio_data_in_copy_data(
Dan Williamsba7cb222011-06-27 11:56:41 -07001332 struct isci_stp_request *stp_req,
Dan Williams5dec6f42011-05-10 02:28:49 -07001333 u8 *data_buffer)
1334{
1335 enum sci_status status;
1336
1337 /*
1338 * If there is less than 1K remaining in the transfer request
1339 * copy just the data for the transfer */
Dan Williamsba7cb222011-06-27 11:56:41 -07001340 if (stp_req->pio_len < SCU_MAX_FRAME_BUFFER_SIZE) {
Dan Williams5dec6f42011-05-10 02:28:49 -07001341 status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
Dan Williamsba7cb222011-06-27 11:56:41 -07001342 stp_req, data_buffer, stp_req->pio_len);
Dan Williams5dec6f42011-05-10 02:28:49 -07001343
1344 if (status == SCI_SUCCESS)
Dan Williamsba7cb222011-06-27 11:56:41 -07001345 stp_req->pio_len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001346 } else {
1347 /* We are transfering the whole frame so copy */
1348 status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
Dan Williamsba7cb222011-06-27 11:56:41 -07001349 stp_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
Dan Williams5dec6f42011-05-10 02:28:49 -07001350
1351 if (status == SCI_SUCCESS)
Dan Williamsba7cb222011-06-27 11:56:41 -07001352 stp_req->pio_len -= SCU_MAX_FRAME_BUFFER_SIZE;
Dan Williams5dec6f42011-05-10 02:28:49 -07001353 }
1354
1355 return status;
1356}
1357
Edmund Nadolskie3013702011-06-02 00:10:43 +00001358static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001359stp_request_pio_await_h2d_completion_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001360 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001361{
1362 enum sci_status status = SCI_SUCCESS;
1363
1364 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1365 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5076a1a2011-06-27 14:57:03 -07001366 scic_sds_request_set_status(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001367 SCU_TASK_DONE_GOOD,
1368 SCI_SUCCESS);
Dan Williams5dec6f42011-05-10 02:28:49 -07001369
Dan Williams5076a1a2011-06-27 14:57:03 -07001370 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams5dec6f42011-05-10 02:28:49 -07001371 break;
1372
1373 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001374 /* All other completion status cause the IO to be
1375 * complete. If a NAK was received, then it is up to
1376 * the user to retry the request.
1377 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001378 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001379 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1380 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williams5dec6f42011-05-10 02:28:49 -07001381
Dan Williams5076a1a2011-06-27 14:57:03 -07001382 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001383 break;
1384 }
1385
1386 return status;
1387}
1388
Edmund Nadolskie3013702011-06-02 00:10:43 +00001389static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001390pio_data_out_tx_done_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001391 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001392{
1393 enum sci_status status = SCI_SUCCESS;
1394 bool all_frames_transferred = false;
Dan Williams5076a1a2011-06-27 14:57:03 -07001395 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williams5dec6f42011-05-10 02:28:49 -07001396
1397 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1398 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1399 /* Transmit data */
Dan Williamsba7cb222011-06-27 11:56:41 -07001400 if (stp_req->pio_len != 0) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001401 status = scic_sds_stp_request_pio_data_out_transmit_data(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -07001402 if (status == SCI_SUCCESS) {
Dan Williamsba7cb222011-06-27 11:56:41 -07001403 if (stp_req->pio_len == 0)
Dan Williams5dec6f42011-05-10 02:28:49 -07001404 all_frames_transferred = true;
1405 }
Dan Williamsba7cb222011-06-27 11:56:41 -07001406 } else if (stp_req->pio_len == 0) {
Dan Williams5dec6f42011-05-10 02:28:49 -07001407 /*
1408 * this will happen if the all data is written at the
1409 * first time after the pio setup fis is received
1410 */
1411 all_frames_transferred = true;
1412 }
1413
1414 /* all data transferred. */
1415 if (all_frames_transferred) {
1416 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001417 * Change the state to SCI_REQ_STP_PIO_DATA_IN
Dan Williams5dec6f42011-05-10 02:28:49 -07001418 * and wait for PIO_SETUP fis / or D2H REg fis. */
Dan Williams5076a1a2011-06-27 14:57:03 -07001419 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams5dec6f42011-05-10 02:28:49 -07001420 }
1421 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001422
Dan Williams5dec6f42011-05-10 02:28:49 -07001423 default:
1424 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001425 * All other completion status cause the IO to be complete.
1426 * If a NAK was received, then it is up to the user to retry
1427 * the request.
1428 */
Dan Williams5dec6f42011-05-10 02:28:49 -07001429 scic_sds_request_set_status(
Dan Williams5076a1a2011-06-27 14:57:03 -07001430 ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001431 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
Edmund Nadolskie3013702011-06-02 00:10:43 +00001432 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williams5dec6f42011-05-10 02:28:49 -07001433
Dan Williams5076a1a2011-06-27 14:57:03 -07001434 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001435 break;
1436 }
1437
1438 return status;
1439}
1440
Dan Williams5dec6f42011-05-10 02:28:49 -07001441static void scic_sds_stp_request_udma_complete_request(
Dan Williams5076a1a2011-06-27 14:57:03 -07001442 struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001443 u32 scu_status,
1444 enum sci_status sci_status)
1445{
Dan Williams5076a1a2011-06-27 14:57:03 -07001446 scic_sds_request_set_status(ireq, scu_status, sci_status);
1447 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001448}
1449
Dan Williams5076a1a2011-06-27 14:57:03 -07001450static enum sci_status scic_sds_stp_request_udma_general_frame_handler(struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001451 u32 frame_index)
1452{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001453 struct isci_host *ihost = ireq->owning_controller;
Dan Williams5dec6f42011-05-10 02:28:49 -07001454 struct dev_to_host_fis *frame_header;
1455 enum sci_status status;
1456 u32 *frame_buffer;
1457
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001458 status = scic_sds_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williams5dec6f42011-05-10 02:28:49 -07001459 frame_index,
1460 (void **)&frame_header);
1461
1462 if ((status == SCI_SUCCESS) &&
1463 (frame_header->fis_type == FIS_REGD2H)) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001464 scic_sds_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williams5dec6f42011-05-10 02:28:49 -07001465 frame_index,
1466 (void **)&frame_buffer);
1467
Dan Williams5076a1a2011-06-27 14:57:03 -07001468 scic_sds_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williams5dec6f42011-05-10 02:28:49 -07001469 frame_header,
1470 frame_buffer);
1471 }
1472
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001473 scic_sds_controller_release_frame(ihost, frame_index);
Dan Williams5dec6f42011-05-10 02:28:49 -07001474
1475 return status;
1476}
1477
Edmund Nadolskie3013702011-06-02 00:10:43 +00001478enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001479scic_sds_io_request_frame_handler(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001480 u32 frame_index)
Dan Williamsd1c637c32011-05-11 08:27:47 -07001481{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001482 struct isci_host *ihost = ireq->owning_controller;
Dan Williams5076a1a2011-06-27 14:57:03 -07001483 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001484 enum sci_base_request_states state;
1485 enum sci_status status;
1486 ssize_t word_cnt;
1487
Dan Williams5076a1a2011-06-27 14:57:03 -07001488 state = ireq->sm.current_state_id;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001489 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001490 case SCI_REQ_STARTED: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001491 struct ssp_frame_hdr ssp_hdr;
1492 void *frame_header;
1493
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001494 scic_sds_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001495 frame_index,
1496 &frame_header);
1497
1498 word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
1499 sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);
1500
1501 if (ssp_hdr.frame_type == SSP_RESPONSE) {
1502 struct ssp_response_iu *resp_iu;
1503 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1504
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001505 scic_sds_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001506 frame_index,
1507 (void **)&resp_iu);
1508
Dan Williams5076a1a2011-06-27 14:57:03 -07001509 sci_swab32_cpy(&ireq->ssp.rsp, resp_iu, word_cnt);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001510
Dan Williams5076a1a2011-06-27 14:57:03 -07001511 resp_iu = &ireq->ssp.rsp;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001512
1513 if (resp_iu->datapres == 0x01 ||
1514 resp_iu->datapres == 0x02) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001515 scic_sds_request_set_status(ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001516 SCU_TASK_DONE_CHECK_RESPONSE,
1517 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1518 } else
Dan Williams5076a1a2011-06-27 14:57:03 -07001519 scic_sds_request_set_status(ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001520 SCU_TASK_DONE_GOOD,
1521 SCI_SUCCESS);
1522 } else {
1523 /* not a response frame, why did it get forwarded? */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001524 dev_err(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001525 "%s: SCIC IO Request 0x%p received unexpected "
Dan Williams5076a1a2011-06-27 14:57:03 -07001526 "frame %d type 0x%02x\n", __func__, ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001527 frame_index, ssp_hdr.frame_type);
1528 }
1529
1530 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001531 * In any case we are done with this frame buffer return it to
1532 * the controller
Dan Williamsd1c637c32011-05-11 08:27:47 -07001533 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001534 scic_sds_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001535
1536 return SCI_SUCCESS;
1537 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001538
1539 case SCI_REQ_TASK_WAIT_TC_RESP:
Dan Williams5076a1a2011-06-27 14:57:03 -07001540 scic_sds_io_request_copy_response(ireq);
1541 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001542 scic_sds_controller_release_frame(ihost,frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001543 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001544
1545 case SCI_REQ_SMP_WAIT_RESP: {
Dan Williams5076a1a2011-06-27 14:57:03 -07001546 struct smp_resp *rsp_hdr = &ireq->smp.rsp;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001547 void *frame_header;
1548
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001549 scic_sds_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001550 frame_index,
1551 &frame_header);
1552
1553 /* byte swap the header. */
1554 word_cnt = SMP_RESP_HDR_SZ / sizeof(u32);
1555 sci_swab32_cpy(rsp_hdr, frame_header, word_cnt);
1556
1557 if (rsp_hdr->frame_type == SMP_RESPONSE) {
1558 void *smp_resp;
1559
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001560 scic_sds_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001561 frame_index,
1562 &smp_resp);
1563
Dan Williams5edc3342011-06-16 17:20:35 -07001564 word_cnt = (sizeof(struct smp_resp) - SMP_RESP_HDR_SZ) /
Dan Williamsd1c637c32011-05-11 08:27:47 -07001565 sizeof(u32);
1566
1567 sci_swab32_cpy(((u8 *) rsp_hdr) + SMP_RESP_HDR_SZ,
1568 smp_resp, word_cnt);
1569
Dan Williams5076a1a2011-06-27 14:57:03 -07001570 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001571 SCI_SUCCESS);
1572
Dan Williams5076a1a2011-06-27 14:57:03 -07001573 sci_change_state(&ireq->sm, SCI_REQ_SMP_WAIT_TC_COMP);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001574 } else {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001575 /*
1576 * This was not a response frame why did it get
1577 * forwarded?
1578 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001579 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001580 "%s: SCIC SMP Request 0x%p received unexpected "
1581 "frame %d type 0x%02x\n",
1582 __func__,
Dan Williams5076a1a2011-06-27 14:57:03 -07001583 ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001584 frame_index,
1585 rsp_hdr->frame_type);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001586
Dan Williams5076a1a2011-06-27 14:57:03 -07001587 scic_sds_request_set_status(ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001588 SCU_TASK_DONE_SMP_FRM_TYPE_ERR,
1589 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1590
Dan Williams5076a1a2011-06-27 14:57:03 -07001591 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001592 }
1593
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001594 scic_sds_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001595
1596 return SCI_SUCCESS;
1597 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001598
1599 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07001600 return scic_sds_stp_request_udma_general_frame_handler(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001601 frame_index);
1602
1603 case SCI_REQ_STP_UDMA_WAIT_D2H:
Dan Williamsd1c637c32011-05-11 08:27:47 -07001604 /* Use the general frame handler to copy the resposne data */
Dan Williams5076a1a2011-06-27 14:57:03 -07001605 status = scic_sds_stp_request_udma_general_frame_handler(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001606 frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001607
1608 if (status != SCI_SUCCESS)
1609 return status;
1610
Dan Williams5076a1a2011-06-27 14:57:03 -07001611 scic_sds_stp_request_udma_complete_request(ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001612 SCU_TASK_DONE_CHECK_RESPONSE,
1613 SCI_FAILURE_IO_RESPONSE_VALID);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001614
Dan Williamsd1c637c32011-05-11 08:27:47 -07001615 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001616
1617 case SCI_REQ_STP_NON_DATA_WAIT_D2H: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001618 struct dev_to_host_fis *frame_header;
1619 u32 *frame_buffer;
1620
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001621 status = scic_sds_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001622 frame_index,
1623 (void **)&frame_header);
1624
1625 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001626 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001627 "%s: SCIC IO Request 0x%p could not get frame "
1628 "header for frame index %d, status %x\n",
1629 __func__,
1630 stp_req,
1631 frame_index,
1632 status);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001633
1634 return status;
1635 }
1636
1637 switch (frame_header->fis_type) {
1638 case FIS_REGD2H:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001639 scic_sds_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001640 frame_index,
1641 (void **)&frame_buffer);
1642
Dan Williams5076a1a2011-06-27 14:57:03 -07001643 scic_sds_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001644 frame_header,
1645 frame_buffer);
1646
1647 /* The command has completed with error */
Dan Williams5076a1a2011-06-27 14:57:03 -07001648 scic_sds_request_set_status(ireq, SCU_TASK_DONE_CHECK_RESPONSE,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001649 SCI_FAILURE_IO_RESPONSE_VALID);
1650 break;
1651
1652 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001653 dev_warn(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001654 "%s: IO Request:0x%p Frame Id:%d protocol "
1655 "violation occurred\n", __func__, stp_req,
1656 frame_index);
1657
Dan Williams5076a1a2011-06-27 14:57:03 -07001658 scic_sds_request_set_status(ireq, SCU_TASK_DONE_UNEXP_FIS,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001659 SCI_FAILURE_PROTOCOL_VIOLATION);
1660 break;
1661 }
1662
Dan Williams5076a1a2011-06-27 14:57:03 -07001663 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001664
1665 /* Frame has been decoded return it to the controller */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001666 scic_sds_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001667
1668 return status;
1669 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001670
1671 case SCI_REQ_STP_PIO_WAIT_FRAME: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001672 struct sas_task *task = isci_request_access_task(ireq);
1673 struct dev_to_host_fis *frame_header;
1674 u32 *frame_buffer;
1675
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001676 status = scic_sds_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001677 frame_index,
1678 (void **)&frame_header);
1679
1680 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001681 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001682 "%s: SCIC IO Request 0x%p could not get frame "
1683 "header for frame index %d, status %x\n",
Dan Williamsd1c637c32011-05-11 08:27:47 -07001684 __func__, stp_req, frame_index, status);
1685 return status;
1686 }
1687
1688 switch (frame_header->fis_type) {
1689 case FIS_PIO_SETUP:
1690 /* Get from the frame buffer the PIO Setup Data */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001691 scic_sds_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001692 frame_index,
1693 (void **)&frame_buffer);
1694
Edmund Nadolskie3013702011-06-02 00:10:43 +00001695 /* Get the data from the PIO Setup The SCU Hardware
1696 * returns first word in the frame_header and the rest
1697 * of the data is in the frame buffer so we need to
1698 * back up one dword
Dan Williamsd1c637c32011-05-11 08:27:47 -07001699 */
1700
1701 /* transfer_count: first 16bits in the 4th dword */
Dan Williamsba7cb222011-06-27 11:56:41 -07001702 stp_req->pio_len = frame_buffer[3] & 0xffff;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001703
Dan Williamsba7cb222011-06-27 11:56:41 -07001704 /* status: 4th byte in the 3rd dword */
1705 stp_req->status = (frame_buffer[2] >> 24) & 0xff;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001706
Dan Williams5076a1a2011-06-27 14:57:03 -07001707 scic_sds_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001708 frame_header,
1709 frame_buffer);
1710
Dan Williams5076a1a2011-06-27 14:57:03 -07001711 ireq->stp.rsp.status = stp_req->status;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001712
1713 /* The next state is dependent on whether the
1714 * request was PIO Data-in or Data out
1715 */
1716 if (task->data_dir == DMA_FROM_DEVICE) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001717 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_IN);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001718 } else if (task->data_dir == DMA_TO_DEVICE) {
1719 /* Transmit data */
Dan Williams5076a1a2011-06-27 14:57:03 -07001720 status = scic_sds_stp_request_pio_data_out_transmit_data(ireq);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001721 if (status != SCI_SUCCESS)
1722 break;
Dan Williams5076a1a2011-06-27 14:57:03 -07001723 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_OUT);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001724 }
1725 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001726
Dan Williamsd1c637c32011-05-11 08:27:47 -07001727 case FIS_SETDEVBITS:
Dan Williams5076a1a2011-06-27 14:57:03 -07001728 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001729 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001730
Dan Williamsd1c637c32011-05-11 08:27:47 -07001731 case FIS_REGD2H:
1732 if (frame_header->status & ATA_BUSY) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001733 /*
1734 * Now why is the drive sending a D2H Register
1735 * FIS when it is still busy? Do nothing since
1736 * we are still in the right state.
Dan Williamsd1c637c32011-05-11 08:27:47 -07001737 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001738 dev_dbg(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001739 "%s: SCIC PIO Request 0x%p received "
1740 "D2H Register FIS with BSY status "
Edmund Nadolskie3013702011-06-02 00:10:43 +00001741 "0x%x\n",
1742 __func__,
1743 stp_req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001744 frame_header->status);
1745 break;
1746 }
1747
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001748 scic_sds_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001749 frame_index,
1750 (void **)&frame_buffer);
1751
Dan Williams5076a1a2011-06-27 14:57:03 -07001752 scic_sds_controller_copy_sata_response(&ireq->stp.req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001753 frame_header,
1754 frame_buffer);
1755
Dan Williams5076a1a2011-06-27 14:57:03 -07001756 scic_sds_request_set_status(ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001757 SCU_TASK_DONE_CHECK_RESPONSE,
1758 SCI_FAILURE_IO_RESPONSE_VALID);
1759
Dan Williams5076a1a2011-06-27 14:57:03 -07001760 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001761 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001762
Dan Williamsd1c637c32011-05-11 08:27:47 -07001763 default:
1764 /* FIXME: what do we do here? */
1765 break;
1766 }
1767
1768 /* Frame is decoded return it to the controller */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001769 scic_sds_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001770
1771 return status;
1772 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001773
1774 case SCI_REQ_STP_PIO_DATA_IN: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001775 struct dev_to_host_fis *frame_header;
1776 struct sata_fis_data *frame_buffer;
1777
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001778 status = scic_sds_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001779 frame_index,
1780 (void **)&frame_header);
1781
1782 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001783 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001784 "%s: SCIC IO Request 0x%p could not get frame "
1785 "header for frame index %d, status %x\n",
1786 __func__,
1787 stp_req,
1788 frame_index,
1789 status);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001790 return status;
1791 }
1792
1793 if (frame_header->fis_type != FIS_DATA) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001794 dev_err(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001795 "%s: SCIC PIO Request 0x%p received frame %d "
1796 "with fis type 0x%02x when expecting a data "
Edmund Nadolskie3013702011-06-02 00:10:43 +00001797 "fis.\n",
1798 __func__,
1799 stp_req,
1800 frame_index,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001801 frame_header->fis_type);
1802
Dan Williams5076a1a2011-06-27 14:57:03 -07001803 scic_sds_request_set_status(ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001804 SCU_TASK_DONE_GOOD,
1805 SCI_FAILURE_IO_REQUIRES_SCSI_ABORT);
1806
Dan Williams5076a1a2011-06-27 14:57:03 -07001807 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001808
1809 /* Frame is decoded return it to the controller */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001810 scic_sds_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001811 return status;
1812 }
1813
Dan Williamsba7cb222011-06-27 11:56:41 -07001814 if (stp_req->sgl.index < 0) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001815 ireq->saved_rx_frame_index = frame_index;
Dan Williamsba7cb222011-06-27 11:56:41 -07001816 stp_req->pio_len = 0;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001817 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001818 scic_sds_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001819 frame_index,
1820 (void **)&frame_buffer);
1821
1822 status = scic_sds_stp_request_pio_data_in_copy_data(stp_req,
1823 (u8 *)frame_buffer);
1824
1825 /* Frame is decoded return it to the controller */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001826 scic_sds_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001827 }
1828
1829 /* Check for the end of the transfer, are there more
1830 * bytes remaining for this data transfer
1831 */
Dan Williamsba7cb222011-06-27 11:56:41 -07001832 if (status != SCI_SUCCESS || stp_req->pio_len != 0)
Dan Williamsd1c637c32011-05-11 08:27:47 -07001833 return status;
1834
Dan Williamsba7cb222011-06-27 11:56:41 -07001835 if ((stp_req->status & ATA_BUSY) == 0) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001836 scic_sds_request_set_status(ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001837 SCU_TASK_DONE_CHECK_RESPONSE,
1838 SCI_FAILURE_IO_RESPONSE_VALID);
1839
Dan Williams5076a1a2011-06-27 14:57:03 -07001840 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001841 } else {
Dan Williams5076a1a2011-06-27 14:57:03 -07001842 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001843 }
1844 return status;
1845 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001846
1847 case SCI_REQ_STP_SOFT_RESET_WAIT_D2H: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001848 struct dev_to_host_fis *frame_header;
1849 u32 *frame_buffer;
1850
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001851 status = scic_sds_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001852 frame_index,
1853 (void **)&frame_header);
1854 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001855 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001856 "%s: SCIC IO Request 0x%p could not get frame "
1857 "header for frame index %d, status %x\n",
1858 __func__,
1859 stp_req,
1860 frame_index,
1861 status);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001862 return status;
1863 }
1864
1865 switch (frame_header->fis_type) {
1866 case FIS_REGD2H:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001867 scic_sds_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001868 frame_index,
1869 (void **)&frame_buffer);
1870
Dan Williams5076a1a2011-06-27 14:57:03 -07001871 scic_sds_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001872 frame_header,
1873 frame_buffer);
1874
1875 /* The command has completed with error */
Dan Williams5076a1a2011-06-27 14:57:03 -07001876 scic_sds_request_set_status(ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001877 SCU_TASK_DONE_CHECK_RESPONSE,
1878 SCI_FAILURE_IO_RESPONSE_VALID);
1879 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001880
Dan Williamsd1c637c32011-05-11 08:27:47 -07001881 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001882 dev_warn(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001883 "%s: IO Request:0x%p Frame Id:%d protocol "
Edmund Nadolskie3013702011-06-02 00:10:43 +00001884 "violation occurred\n",
1885 __func__,
1886 stp_req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001887 frame_index);
1888
Dan Williams5076a1a2011-06-27 14:57:03 -07001889 scic_sds_request_set_status(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001890 SCU_TASK_DONE_UNEXP_FIS,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001891 SCI_FAILURE_PROTOCOL_VIOLATION);
1892 break;
1893 }
1894
Dan Williams5076a1a2011-06-27 14:57:03 -07001895 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001896
1897 /* Frame has been decoded return it to the controller */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001898 scic_sds_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001899
1900 return status;
1901 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001902 case SCI_REQ_ABORTING:
1903 /*
1904 * TODO: Is it even possible to get an unsolicited frame in the
Dan Williamsd1c637c32011-05-11 08:27:47 -07001905 * aborting state?
1906 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001907 scic_sds_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001908 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001909
Dan Williamsd1c637c32011-05-11 08:27:47 -07001910 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001911 dev_warn(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001912 "%s: SCIC IO Request given unexpected frame %x while "
1913 "in state %d\n",
1914 __func__,
1915 frame_index,
1916 state);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001917
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001918 scic_sds_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001919 return SCI_FAILURE_INVALID_STATE;
1920 }
1921}
1922
Dan Williams5076a1a2011-06-27 14:57:03 -07001923static enum sci_status stp_request_udma_await_tc_event(struct isci_request *ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001924 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001925{
1926 enum sci_status status = SCI_SUCCESS;
1927
1928 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1929 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5076a1a2011-06-27 14:57:03 -07001930 scic_sds_stp_request_udma_complete_request(ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001931 SCU_TASK_DONE_GOOD,
1932 SCI_SUCCESS);
1933 break;
1934 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
1935 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001936 /* We must check ther response buffer to see if the D2H
1937 * Register FIS was received before we got the TC
1938 * completion.
1939 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001940 if (ireq->stp.rsp.fis_type == FIS_REGD2H) {
1941 scic_sds_remote_device_suspend(ireq->target_device,
Dan Williams5dec6f42011-05-10 02:28:49 -07001942 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
1943
Dan Williams5076a1a2011-06-27 14:57:03 -07001944 scic_sds_stp_request_udma_complete_request(ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001945 SCU_TASK_DONE_CHECK_RESPONSE,
1946 SCI_FAILURE_IO_RESPONSE_VALID);
1947 } else {
Dan Williamsa7e255a2011-05-11 08:27:47 -07001948 /* If we have an error completion status for the
1949 * TC then we can expect a D2H register FIS from
1950 * the device so we must change state to wait
1951 * for it
1952 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001953 sci_change_state(&ireq->sm, SCI_REQ_STP_UDMA_WAIT_D2H);
Dan Williams5dec6f42011-05-10 02:28:49 -07001954 }
1955 break;
1956
Dan Williamsa7e255a2011-05-11 08:27:47 -07001957 /* TODO Check to see if any of these completion status need to
1958 * wait for the device to host register fis.
1959 */
1960 /* TODO We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR
1961 * - this comes only for B0
1962 */
Dan Williams5dec6f42011-05-10 02:28:49 -07001963 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_INV_FIS_LEN):
1964 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
1965 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_R_ERR):
1966 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CMD_LL_R_ERR):
1967 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CRC_ERR):
Dan Williams5076a1a2011-06-27 14:57:03 -07001968 scic_sds_remote_device_suspend(ireq->target_device,
Dan Williams5dec6f42011-05-10 02:28:49 -07001969 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
1970 /* Fall through to the default case */
1971 default:
1972 /* All other completion status cause the IO to be complete. */
Dan Williams5076a1a2011-06-27 14:57:03 -07001973 scic_sds_stp_request_udma_complete_request(ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001974 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1975 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1976 break;
1977 }
1978
1979 return status;
1980}
1981
Edmund Nadolskie3013702011-06-02 00:10:43 +00001982static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001983stp_request_soft_reset_await_h2d_asserted_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001984 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001985{
1986 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1987 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5076a1a2011-06-27 14:57:03 -07001988 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001989 SCI_SUCCESS);
Dan Williams5dec6f42011-05-10 02:28:49 -07001990
Dan Williams5076a1a2011-06-27 14:57:03 -07001991 sci_change_state(&ireq->sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG);
Dan Williams5dec6f42011-05-10 02:28:49 -07001992 break;
1993
1994 default:
1995 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001996 * All other completion status cause the IO to be complete.
1997 * If a NAK was received, then it is up to the user to retry
1998 * the request.
1999 */
Dan Williams5076a1a2011-06-27 14:57:03 -07002000 scic_sds_request_set_status(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002001 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
2002 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williams5dec6f42011-05-10 02:28:49 -07002003
Dan Williams5076a1a2011-06-27 14:57:03 -07002004 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07002005 break;
2006 }
2007
2008 return SCI_SUCCESS;
2009}
2010
Edmund Nadolskie3013702011-06-02 00:10:43 +00002011static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07002012stp_request_soft_reset_await_h2d_diagnostic_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002013 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07002014{
2015 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2016 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5076a1a2011-06-27 14:57:03 -07002017 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
Dan Williams5dec6f42011-05-10 02:28:49 -07002018 SCI_SUCCESS);
2019
Dan Williams5076a1a2011-06-27 14:57:03 -07002020 sci_change_state(&ireq->sm, SCI_REQ_STP_SOFT_RESET_WAIT_D2H);
Dan Williams5dec6f42011-05-10 02:28:49 -07002021 break;
2022
2023 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07002024 /* All other completion status cause the IO to be complete. If
2025 * a NAK was received, then it is up to the user to retry the
2026 * request.
2027 */
Dan Williams5076a1a2011-06-27 14:57:03 -07002028 scic_sds_request_set_status(ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07002029 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
Dan Williamsa7e255a2011-05-11 08:27:47 -07002030 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williams5dec6f42011-05-10 02:28:49 -07002031
Dan Williams5076a1a2011-06-27 14:57:03 -07002032 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07002033 break;
2034 }
2035
2036 return SCI_SUCCESS;
2037}
2038
Dan Williamsa7e255a2011-05-11 08:27:47 -07002039enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07002040scic_sds_io_request_tc_completion(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002041 u32 completion_code)
Dan Williamsa7e255a2011-05-11 08:27:47 -07002042{
2043 enum sci_base_request_states state;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002044 struct isci_host *ihost = ireq->owning_controller;
Dan Williamsa7e255a2011-05-11 08:27:47 -07002045
Dan Williams5076a1a2011-06-27 14:57:03 -07002046 state = ireq->sm.current_state_id;
Dan Williamsa7e255a2011-05-11 08:27:47 -07002047
2048 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002049 case SCI_REQ_STARTED:
Dan Williams5076a1a2011-06-27 14:57:03 -07002050 return request_started_state_tc_event(ireq, completion_code);
Edmund Nadolskie3013702011-06-02 00:10:43 +00002051
2052 case SCI_REQ_TASK_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07002053 return ssp_task_request_await_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002054 completion_code);
2055
2056 case SCI_REQ_SMP_WAIT_RESP:
Dan Williams5076a1a2011-06-27 14:57:03 -07002057 return smp_request_await_response_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002058 completion_code);
2059
2060 case SCI_REQ_SMP_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07002061 return smp_request_await_tc_event(ireq, completion_code);
Edmund Nadolskie3013702011-06-02 00:10:43 +00002062
2063 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07002064 return stp_request_udma_await_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002065 completion_code);
2066
2067 case SCI_REQ_STP_NON_DATA_WAIT_H2D:
Dan Williams5076a1a2011-06-27 14:57:03 -07002068 return stp_request_non_data_await_h2d_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002069 completion_code);
2070
2071 case SCI_REQ_STP_PIO_WAIT_H2D:
Dan Williams5076a1a2011-06-27 14:57:03 -07002072 return stp_request_pio_await_h2d_completion_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002073 completion_code);
2074
2075 case SCI_REQ_STP_PIO_DATA_OUT:
Dan Williams5076a1a2011-06-27 14:57:03 -07002076 return pio_data_out_tx_done_tc_event(ireq, completion_code);
Edmund Nadolskie3013702011-06-02 00:10:43 +00002077
2078 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
Dan Williams5076a1a2011-06-27 14:57:03 -07002079 return stp_request_soft_reset_await_h2d_asserted_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002080 completion_code);
2081
2082 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
Dan Williams5076a1a2011-06-27 14:57:03 -07002083 return stp_request_soft_reset_await_h2d_diagnostic_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002084 completion_code);
2085
2086 case SCI_REQ_ABORTING:
Dan Williams5076a1a2011-06-27 14:57:03 -07002087 return request_aborting_state_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002088 completion_code);
2089
2090 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002091 dev_warn(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002092 "%s: SCIC IO Request given task completion "
2093 "notification %x while in wrong state %d\n",
2094 __func__,
2095 completion_code,
2096 state);
2097 return SCI_FAILURE_INVALID_STATE;
Dan Williamsa7e255a2011-05-11 08:27:47 -07002098 }
2099}
2100
Dan Williams6f231dd2011-07-02 22:56:22 -07002101/**
2102 * isci_request_process_response_iu() - This function sets the status and
2103 * response iu, in the task struct, from the request object for the upper
2104 * layer driver.
2105 * @sas_task: This parameter is the task struct from the upper layer driver.
2106 * @resp_iu: This parameter points to the response iu of the completed request.
2107 * @dev: This parameter specifies the linux device struct.
2108 *
2109 * none.
2110 */
2111static void isci_request_process_response_iu(
2112 struct sas_task *task,
2113 struct ssp_response_iu *resp_iu,
2114 struct device *dev)
2115{
2116 dev_dbg(dev,
2117 "%s: resp_iu = %p "
2118 "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
2119 "resp_iu->response_data_len = %x, "
2120 "resp_iu->sense_data_len = %x\nrepsonse data: ",
2121 __func__,
2122 resp_iu,
2123 resp_iu->status,
2124 resp_iu->datapres,
2125 resp_iu->response_data_len,
2126 resp_iu->sense_data_len);
2127
2128 task->task_status.stat = resp_iu->status;
2129
2130 /* libsas updates the task status fields based on the response iu. */
2131 sas_ssp_task_response(dev, task, resp_iu);
2132}
2133
2134/**
2135 * isci_request_set_open_reject_status() - This function prepares the I/O
2136 * completion for OPEN_REJECT conditions.
2137 * @request: This parameter is the completed isci_request object.
2138 * @response_ptr: This parameter specifies the service response for the I/O.
2139 * @status_ptr: This parameter specifies the exec status for the I/O.
2140 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2141 * the LLDD with respect to completing this request or forcing an abort
2142 * condition on the I/O.
2143 * @open_rej_reason: This parameter specifies the encoded reason for the
2144 * abandon-class reject.
2145 *
2146 * none.
2147 */
2148static void isci_request_set_open_reject_status(
2149 struct isci_request *request,
2150 struct sas_task *task,
2151 enum service_response *response_ptr,
2152 enum exec_status *status_ptr,
2153 enum isci_completion_selection *complete_to_host_ptr,
2154 enum sas_open_rej_reason open_rej_reason)
2155{
2156 /* Task in the target is done. */
Dan Williams38d88792011-06-23 14:33:48 -07002157 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002158 *response_ptr = SAS_TASK_UNDELIVERED;
2159 *status_ptr = SAS_OPEN_REJECT;
2160 *complete_to_host_ptr = isci_perform_normal_io_completion;
2161 task->task_status.open_rej_reason = open_rej_reason;
2162}
2163
2164/**
2165 * isci_request_handle_controller_specific_errors() - This function decodes
2166 * controller-specific I/O completion error conditions.
2167 * @request: This parameter is the completed isci_request object.
2168 * @response_ptr: This parameter specifies the service response for the I/O.
2169 * @status_ptr: This parameter specifies the exec status for the I/O.
2170 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2171 * the LLDD with respect to completing this request or forcing an abort
2172 * condition on the I/O.
2173 *
2174 * none.
2175 */
2176static void isci_request_handle_controller_specific_errors(
Dan Williams209fae12011-06-13 17:39:44 -07002177 struct isci_remote_device *idev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002178 struct isci_request *request,
2179 struct sas_task *task,
2180 enum service_response *response_ptr,
2181 enum exec_status *status_ptr,
2182 enum isci_completion_selection *complete_to_host_ptr)
2183{
2184 unsigned int cstatus;
2185
Dan Williams5076a1a2011-06-27 14:57:03 -07002186 cstatus = request->scu_status;
Dan Williams6f231dd2011-07-02 22:56:22 -07002187
2188 dev_dbg(&request->isci_host->pdev->dev,
2189 "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
2190 "- controller status = 0x%x\n",
2191 __func__, request, cstatus);
2192
2193 /* Decode the controller-specific errors; most
2194 * important is to recognize those conditions in which
2195 * the target may still have a task outstanding that
2196 * must be aborted.
2197 *
2198 * Note that there are SCU completion codes being
2199 * named in the decode below for which SCIC has already
2200 * done work to handle them in a way other than as
2201 * a controller-specific completion code; these are left
2202 * in the decode below for completeness sake.
2203 */
2204 switch (cstatus) {
2205 case SCU_TASK_DONE_DMASETUP_DIRERR:
2206 /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
2207 case SCU_TASK_DONE_XFERCNT_ERR:
2208 /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
2209 if (task->task_proto == SAS_PROTOCOL_SMP) {
2210 /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
2211 *response_ptr = SAS_TASK_COMPLETE;
2212
2213 /* See if the device has been/is being stopped. Note
2214 * that we ignore the quiesce state, since we are
2215 * concerned about the actual device state.
2216 */
Dan Williams209fae12011-06-13 17:39:44 -07002217 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002218 *status_ptr = SAS_DEVICE_UNKNOWN;
2219 else
2220 *status_ptr = SAS_ABORTED_TASK;
2221
Dan Williams38d88792011-06-23 14:33:48 -07002222 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002223
2224 *complete_to_host_ptr =
2225 isci_perform_normal_io_completion;
2226 } else {
2227 /* Task in the target is not done. */
2228 *response_ptr = SAS_TASK_UNDELIVERED;
2229
Dan Williams209fae12011-06-13 17:39:44 -07002230 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002231 *status_ptr = SAS_DEVICE_UNKNOWN;
2232 else
2233 *status_ptr = SAM_STAT_TASK_ABORTED;
2234
Dan Williams38d88792011-06-23 14:33:48 -07002235 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002236
2237 *complete_to_host_ptr =
2238 isci_perform_error_io_completion;
2239 }
2240
2241 break;
2242
2243 case SCU_TASK_DONE_CRC_ERR:
2244 case SCU_TASK_DONE_NAK_CMD_ERR:
2245 case SCU_TASK_DONE_EXCESS_DATA:
2246 case SCU_TASK_DONE_UNEXP_FIS:
2247 /* Also SCU_TASK_DONE_UNEXP_RESP: */
2248 case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
2249 case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
2250 case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
2251 /* These are conditions in which the target
2252 * has completed the task, so that no cleanup
2253 * is necessary.
2254 */
2255 *response_ptr = SAS_TASK_COMPLETE;
2256
2257 /* See if the device has been/is being stopped. Note
2258 * that we ignore the quiesce state, since we are
2259 * concerned about the actual device state.
2260 */
Dan Williams209fae12011-06-13 17:39:44 -07002261 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002262 *status_ptr = SAS_DEVICE_UNKNOWN;
2263 else
2264 *status_ptr = SAS_ABORTED_TASK;
2265
Dan Williams38d88792011-06-23 14:33:48 -07002266 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002267
2268 *complete_to_host_ptr = isci_perform_normal_io_completion;
2269 break;
2270
2271
2272 /* Note that the only open reject completion codes seen here will be
2273 * abandon-class codes; all others are automatically retried in the SCU.
2274 */
2275 case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2276
2277 isci_request_set_open_reject_status(
2278 request, task, response_ptr, status_ptr,
2279 complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
2280 break;
2281
2282 case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2283
2284 /* Note - the return of AB0 will change when
2285 * libsas implements detection of zone violations.
2286 */
2287 isci_request_set_open_reject_status(
2288 request, task, response_ptr, status_ptr,
2289 complete_to_host_ptr, SAS_OREJ_RESV_AB0);
2290 break;
2291
2292 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2293
2294 isci_request_set_open_reject_status(
2295 request, task, response_ptr, status_ptr,
2296 complete_to_host_ptr, SAS_OREJ_RESV_AB1);
2297 break;
2298
2299 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2300
2301 isci_request_set_open_reject_status(
2302 request, task, response_ptr, status_ptr,
2303 complete_to_host_ptr, SAS_OREJ_RESV_AB2);
2304 break;
2305
2306 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2307
2308 isci_request_set_open_reject_status(
2309 request, task, response_ptr, status_ptr,
2310 complete_to_host_ptr, SAS_OREJ_RESV_AB3);
2311 break;
2312
2313 case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2314
2315 isci_request_set_open_reject_status(
2316 request, task, response_ptr, status_ptr,
2317 complete_to_host_ptr, SAS_OREJ_BAD_DEST);
2318 break;
2319
2320 case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
2321
2322 isci_request_set_open_reject_status(
2323 request, task, response_ptr, status_ptr,
2324 complete_to_host_ptr, SAS_OREJ_STP_NORES);
2325 break;
2326
2327 case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
2328
2329 isci_request_set_open_reject_status(
2330 request, task, response_ptr, status_ptr,
2331 complete_to_host_ptr, SAS_OREJ_EPROTO);
2332 break;
2333
2334 case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
2335
2336 isci_request_set_open_reject_status(
2337 request, task, response_ptr, status_ptr,
2338 complete_to_host_ptr, SAS_OREJ_CONN_RATE);
2339 break;
2340
2341 case SCU_TASK_DONE_LL_R_ERR:
2342 /* Also SCU_TASK_DONE_ACK_NAK_TO: */
2343 case SCU_TASK_DONE_LL_PERR:
2344 case SCU_TASK_DONE_LL_SY_TERM:
2345 /* Also SCU_TASK_DONE_NAK_ERR:*/
2346 case SCU_TASK_DONE_LL_LF_TERM:
2347 /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
2348 case SCU_TASK_DONE_LL_ABORT_ERR:
2349 case SCU_TASK_DONE_SEQ_INV_TYPE:
2350 /* Also SCU_TASK_DONE_UNEXP_XR: */
2351 case SCU_TASK_DONE_XR_IU_LEN_ERR:
2352 case SCU_TASK_DONE_INV_FIS_LEN:
2353 /* Also SCU_TASK_DONE_XR_WD_LEN: */
2354 case SCU_TASK_DONE_SDMA_ERR:
2355 case SCU_TASK_DONE_OFFSET_ERR:
2356 case SCU_TASK_DONE_MAX_PLD_ERR:
2357 case SCU_TASK_DONE_LF_ERR:
2358 case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
2359 case SCU_TASK_DONE_SMP_LL_RX_ERR:
2360 case SCU_TASK_DONE_UNEXP_DATA:
2361 case SCU_TASK_DONE_UNEXP_SDBFIS:
2362 case SCU_TASK_DONE_REG_ERR:
2363 case SCU_TASK_DONE_SDB_ERR:
2364 case SCU_TASK_DONE_TASK_ABORT:
2365 default:
2366 /* Task in the target is not done. */
2367 *response_ptr = SAS_TASK_UNDELIVERED;
2368 *status_ptr = SAM_STAT_TASK_ABORTED;
Dan Williams6f231dd2011-07-02 22:56:22 -07002369
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002370 if (task->task_proto == SAS_PROTOCOL_SMP) {
Dan Williams38d88792011-06-23 14:33:48 -07002371 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002372
2373 *complete_to_host_ptr = isci_perform_normal_io_completion;
2374 } else {
Dan Williams38d88792011-06-23 14:33:48 -07002375 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002376
2377 *complete_to_host_ptr = isci_perform_error_io_completion;
2378 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002379 break;
2380 }
2381}
2382
2383/**
2384 * isci_task_save_for_upper_layer_completion() - This function saves the
2385 * request for later completion to the upper layer driver.
2386 * @host: This parameter is a pointer to the host on which the the request
2387 * should be queued (either as an error or success).
2388 * @request: This parameter is the completed request.
2389 * @response: This parameter is the response code for the completed task.
2390 * @status: This parameter is the status code for the completed task.
2391 *
2392 * none.
2393 */
2394static void isci_task_save_for_upper_layer_completion(
2395 struct isci_host *host,
2396 struct isci_request *request,
2397 enum service_response response,
2398 enum exec_status status,
2399 enum isci_completion_selection task_notification_selection)
2400{
2401 struct sas_task *task = isci_request_access_task(request);
2402
Jeff Skirvinec6c9632011-03-04 14:06:44 -08002403 task_notification_selection
2404 = isci_task_set_completion_status(task, response, status,
2405 task_notification_selection);
Dan Williams6f231dd2011-07-02 22:56:22 -07002406
2407 /* Tasks aborted specifically by a call to the lldd_abort_task
2408 * function should not be completed to the host in the regular path.
2409 */
2410 switch (task_notification_selection) {
2411
2412 case isci_perform_normal_io_completion:
2413
2414 /* Normal notification (task_done) */
2415 dev_dbg(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002416 "%s: Normal - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002417 __func__,
2418 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002419 task->task_status.resp, response,
2420 task->task_status.stat, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07002421 /* Add to the completed list. */
2422 list_add(&request->completed_node,
2423 &host->requests_to_complete);
Jeff Skirvinec6c9632011-03-04 14:06:44 -08002424
2425 /* Take the request off the device's pending request list. */
2426 list_del_init(&request->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -07002427 break;
2428
2429 case isci_perform_aborted_io_completion:
Jeff Skirvina5fde222011-03-04 14:06:42 -08002430 /* No notification to libsas because this request is
2431 * already in the abort path.
Dan Williams6f231dd2011-07-02 22:56:22 -07002432 */
2433 dev_warn(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002434 "%s: Aborted - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002435 __func__,
2436 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002437 task->task_status.resp, response,
2438 task->task_status.stat, status);
Jeff Skirvina5fde222011-03-04 14:06:42 -08002439
2440 /* Wake up whatever process was waiting for this
2441 * request to complete.
2442 */
2443 WARN_ON(request->io_request_completion == NULL);
2444
2445 if (request->io_request_completion != NULL) {
2446
2447 /* Signal whoever is waiting that this
2448 * request is complete.
2449 */
2450 complete(request->io_request_completion);
2451 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002452 break;
2453
2454 case isci_perform_error_io_completion:
2455 /* Use sas_task_abort */
2456 dev_warn(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002457 "%s: Error - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002458 __func__,
2459 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002460 task->task_status.resp, response,
2461 task->task_status.stat, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07002462 /* Add to the aborted list. */
2463 list_add(&request->completed_node,
Jeff Skirvin11b00c12011-03-04 14:06:40 -08002464 &host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002465 break;
2466
2467 default:
2468 dev_warn(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002469 "%s: Unknown - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002470 __func__,
2471 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002472 task->task_status.resp, response,
2473 task->task_status.stat, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07002474
Jeff Skirvina5fde222011-03-04 14:06:42 -08002475 /* Add to the error to libsas list. */
Dan Williams6f231dd2011-07-02 22:56:22 -07002476 list_add(&request->completed_node,
Jeff Skirvin11b00c12011-03-04 14:06:40 -08002477 &host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002478 break;
2479 }
2480}
2481
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002482static void isci_request_io_request_complete(struct isci_host *ihost,
Dan Williamsf1f52e72011-05-10 02:28:45 -07002483 struct isci_request *request,
2484 enum sci_io_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07002485{
2486 struct sas_task *task = isci_request_access_task(request);
2487 struct ssp_response_iu *resp_iu;
2488 void *resp_buf;
2489 unsigned long task_flags;
Dan Williams209fae12011-06-13 17:39:44 -07002490 struct isci_remote_device *idev = isci_lookup_device(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002491 enum service_response response = SAS_TASK_UNDELIVERED;
2492 enum exec_status status = SAS_ABORTED_TASK;
2493 enum isci_request_status request_status;
2494 enum isci_completion_selection complete_to_host
2495 = isci_perform_normal_io_completion;
2496
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002497 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002498 "%s: request = %p, task = %p,\n"
2499 "task->data_dir = %d completion_status = 0x%x\n",
2500 __func__,
2501 request,
2502 task,
2503 task->data_dir,
2504 completion_status);
2505
Jeff Skirvina5fde222011-03-04 14:06:42 -08002506 spin_lock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002507 request_status = isci_request_get_state(request);
Dan Williams6f231dd2011-07-02 22:56:22 -07002508
2509 /* Decode the request status. Note that if the request has been
2510 * aborted by a task management function, we don't care
2511 * what the status is.
2512 */
2513 switch (request_status) {
2514
2515 case aborted:
2516 /* "aborted" indicates that the request was aborted by a task
2517 * management function, since once a task management request is
2518 * perfomed by the device, the request only completes because
2519 * of the subsequent driver terminate.
2520 *
2521 * Aborted also means an external thread is explicitly managing
2522 * this request, so that we do not complete it up the stack.
2523 *
2524 * The target is still there (since the TMF was successful).
2525 */
Dan Williams38d88792011-06-23 14:33:48 -07002526 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002527 response = SAS_TASK_COMPLETE;
2528
2529 /* See if the device has been/is being stopped. Note
2530 * that we ignore the quiesce state, since we are
2531 * concerned about the actual device state.
2532 */
Dan Williams209fae12011-06-13 17:39:44 -07002533 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002534 status = SAS_DEVICE_UNKNOWN;
2535 else
2536 status = SAS_ABORTED_TASK;
2537
2538 complete_to_host = isci_perform_aborted_io_completion;
2539 /* This was an aborted request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002540
2541 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002542 break;
2543
2544 case aborting:
2545 /* aborting means that the task management function tried and
2546 * failed to abort the request. We need to note the request
2547 * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
2548 * target as down.
2549 *
2550 * Aborting also means an external thread is explicitly managing
2551 * this request, so that we do not complete it up the stack.
2552 */
Dan Williams38d88792011-06-23 14:33:48 -07002553 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002554 response = SAS_TASK_UNDELIVERED;
2555
Dan Williams209fae12011-06-13 17:39:44 -07002556 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002557 /* The device has been /is being stopped. Note that
2558 * we ignore the quiesce state, since we are
2559 * concerned about the actual device state.
2560 */
2561 status = SAS_DEVICE_UNKNOWN;
2562 else
2563 status = SAS_PHY_DOWN;
2564
2565 complete_to_host = isci_perform_aborted_io_completion;
2566
2567 /* This was an aborted request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002568
2569 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002570 break;
2571
2572 case terminating:
2573
2574 /* This was an terminated request. This happens when
2575 * the I/O is being terminated because of an action on
2576 * the device (reset, tear down, etc.), and the I/O needs
2577 * to be completed up the stack.
2578 */
Dan Williams38d88792011-06-23 14:33:48 -07002579 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002580 response = SAS_TASK_UNDELIVERED;
2581
2582 /* See if the device has been/is being stopped. Note
2583 * that we ignore the quiesce state, since we are
2584 * concerned about the actual device state.
2585 */
Dan Williams209fae12011-06-13 17:39:44 -07002586 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002587 status = SAS_DEVICE_UNKNOWN;
2588 else
2589 status = SAS_ABORTED_TASK;
2590
Jeff Skirvina5fde222011-03-04 14:06:42 -08002591 complete_to_host = isci_perform_aborted_io_completion;
Dan Williams6f231dd2011-07-02 22:56:22 -07002592
2593 /* This was a terminated request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002594
2595 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002596 break;
2597
Jeff Skirvin77c852f2011-06-20 14:09:16 -07002598 case dead:
2599 /* This was a terminated request that timed-out during the
2600 * termination process. There is no task to complete to
2601 * libsas.
2602 */
2603 complete_to_host = isci_perform_normal_io_completion;
2604 spin_unlock(&request->state_lock);
2605 break;
2606
Dan Williams6f231dd2011-07-02 22:56:22 -07002607 default:
2608
Jeff Skirvina5fde222011-03-04 14:06:42 -08002609 /* The request is done from an SCU HW perspective. */
2610 request->status = completed;
2611
2612 spin_unlock(&request->state_lock);
2613
Dan Williams6f231dd2011-07-02 22:56:22 -07002614 /* This is an active request being completed from the core. */
2615 switch (completion_status) {
2616
2617 case SCI_IO_FAILURE_RESPONSE_VALID:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002618 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002619 "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
2620 __func__,
2621 request,
2622 task);
2623
2624 if (sas_protocol_ata(task->task_proto)) {
Dan Williams5076a1a2011-06-27 14:57:03 -07002625 resp_buf = &request->stp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -07002626 isci_request_process_stp_response(task,
Dan Williamsb7645812011-05-08 02:35:32 -07002627 resp_buf);
Dan Williams6f231dd2011-07-02 22:56:22 -07002628 } else if (SAS_PROTOCOL_SSP == task->task_proto) {
2629
2630 /* crack the iu response buffer. */
Dan Williams5076a1a2011-06-27 14:57:03 -07002631 resp_iu = &request->ssp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -07002632 isci_request_process_response_iu(task, resp_iu,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002633 &ihost->pdev->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002634
2635 } else if (SAS_PROTOCOL_SMP == task->task_proto) {
2636
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002637 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002638 "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
2639 "SAS_PROTOCOL_SMP protocol\n",
2640 __func__);
2641
2642 } else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002643 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002644 "%s: unknown protocol\n", __func__);
2645
2646 /* use the task status set in the task struct by the
2647 * isci_request_process_response_iu call.
2648 */
Dan Williams38d88792011-06-23 14:33:48 -07002649 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002650 response = task->task_status.resp;
2651 status = task->task_status.stat;
2652 break;
2653
2654 case SCI_IO_SUCCESS:
2655 case SCI_IO_SUCCESS_IO_DONE_EARLY:
2656
2657 response = SAS_TASK_COMPLETE;
2658 status = SAM_STAT_GOOD;
Dan Williams38d88792011-06-23 14:33:48 -07002659 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002660
2661 if (task->task_proto == SAS_PROTOCOL_SMP) {
Dan Williams5076a1a2011-06-27 14:57:03 -07002662 void *rsp = &request->smp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -07002663
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002664 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002665 "%s: SMP protocol completion\n",
2666 __func__);
2667
2668 sg_copy_from_buffer(
2669 &task->smp_task.smp_resp, 1,
Dan Williamsb7645812011-05-08 02:35:32 -07002670 rsp, sizeof(struct smp_resp));
Dan Williams6f231dd2011-07-02 22:56:22 -07002671 } else if (completion_status
2672 == SCI_IO_SUCCESS_IO_DONE_EARLY) {
2673
2674 /* This was an SSP / STP / SATA transfer.
2675 * There is a possibility that less data than
2676 * the maximum was transferred.
2677 */
Dan Williams5076a1a2011-06-27 14:57:03 -07002678 u32 transferred_length = sci_req_tx_bytes(request);
Dan Williams6f231dd2011-07-02 22:56:22 -07002679
2680 task->task_status.residual
2681 = task->total_xfer_len - transferred_length;
2682
2683 /* If there were residual bytes, call this an
2684 * underrun.
2685 */
2686 if (task->task_status.residual != 0)
2687 status = SAS_DATA_UNDERRUN;
2688
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002689 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002690 "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
2691 __func__,
2692 status);
2693
2694 } else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002695 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002696 "%s: SCI_IO_SUCCESS\n",
2697 __func__);
2698
2699 break;
2700
2701 case SCI_IO_FAILURE_TERMINATED:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002702 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002703 "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
2704 __func__,
2705 request,
2706 task);
2707
2708 /* The request was terminated explicitly. No handling
2709 * is needed in the SCSI error handler path.
2710 */
Dan Williams38d88792011-06-23 14:33:48 -07002711 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002712 response = SAS_TASK_UNDELIVERED;
2713
2714 /* See if the device has been/is being stopped. Note
2715 * that we ignore the quiesce state, since we are
2716 * concerned about the actual device state.
2717 */
Dan Williams209fae12011-06-13 17:39:44 -07002718 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002719 status = SAS_DEVICE_UNKNOWN;
2720 else
2721 status = SAS_ABORTED_TASK;
2722
2723 complete_to_host = isci_perform_normal_io_completion;
2724 break;
2725
2726 case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
2727
2728 isci_request_handle_controller_specific_errors(
Dan Williams209fae12011-06-13 17:39:44 -07002729 idev, request, task, &response, &status,
Dan Williams6f231dd2011-07-02 22:56:22 -07002730 &complete_to_host);
2731
2732 break;
2733
2734 case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
2735 /* This is a special case, in that the I/O completion
2736 * is telling us that the device needs a reset.
2737 * In order for the device reset condition to be
2738 * noticed, the I/O has to be handled in the error
2739 * handler. Set the reset flag and cause the
2740 * SCSI error thread to be scheduled.
2741 */
2742 spin_lock_irqsave(&task->task_state_lock, task_flags);
2743 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
2744 spin_unlock_irqrestore(&task->task_state_lock, task_flags);
2745
Jeff Skirvinaa145102011-03-07 16:40:47 -07002746 /* Fail the I/O. */
2747 response = SAS_TASK_UNDELIVERED;
2748 status = SAM_STAT_TASK_ABORTED;
2749
Dan Williams6f231dd2011-07-02 22:56:22 -07002750 complete_to_host = isci_perform_error_io_completion;
Dan Williams38d88792011-06-23 14:33:48 -07002751 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002752 break;
2753
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002754 case SCI_FAILURE_RETRY_REQUIRED:
2755
2756 /* Fail the I/O so it can be retried. */
2757 response = SAS_TASK_UNDELIVERED;
Dan Williams209fae12011-06-13 17:39:44 -07002758 if (!idev)
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002759 status = SAS_DEVICE_UNKNOWN;
2760 else
2761 status = SAS_ABORTED_TASK;
2762
2763 complete_to_host = isci_perform_normal_io_completion;
Dan Williams38d88792011-06-23 14:33:48 -07002764 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002765 break;
2766
2767
Dan Williams6f231dd2011-07-02 22:56:22 -07002768 default:
2769 /* Catch any otherwise unhandled error codes here. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002770 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002771 "%s: invalid completion code: 0x%x - "
2772 "isci_request = %p\n",
2773 __func__, completion_status, request);
2774
2775 response = SAS_TASK_UNDELIVERED;
2776
2777 /* See if the device has been/is being stopped. Note
2778 * that we ignore the quiesce state, since we are
2779 * concerned about the actual device state.
2780 */
Dan Williams209fae12011-06-13 17:39:44 -07002781 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002782 status = SAS_DEVICE_UNKNOWN;
2783 else
2784 status = SAS_ABORTED_TASK;
2785
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002786 if (SAS_PROTOCOL_SMP == task->task_proto) {
Dan Williams38d88792011-06-23 14:33:48 -07002787 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002788 complete_to_host = isci_perform_normal_io_completion;
2789 } else {
Dan Williams38d88792011-06-23 14:33:48 -07002790 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002791 complete_to_host = isci_perform_error_io_completion;
2792 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002793 break;
2794 }
2795 break;
2796 }
2797
Dan Williamsddcc7e32011-06-17 10:40:43 -07002798 switch (task->task_proto) {
2799 case SAS_PROTOCOL_SSP:
2800 if (task->data_dir == DMA_NONE)
2801 break;
2802 if (task->num_scatter == 0)
2803 /* 0 indicates a single dma address */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002804 dma_unmap_single(&ihost->pdev->dev,
Dan Williamsddcc7e32011-06-17 10:40:43 -07002805 request->zero_scatter_daddr,
2806 task->total_xfer_len, task->data_dir);
2807 else /* unmap the sgl dma addresses */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002808 dma_unmap_sg(&ihost->pdev->dev, task->scatter,
Dan Williamsddcc7e32011-06-17 10:40:43 -07002809 request->num_sg_entries, task->data_dir);
2810 break;
Dan Williamse9bf7092011-06-16 16:59:56 -07002811 case SAS_PROTOCOL_SMP: {
2812 struct scatterlist *sg = &task->smp_task.smp_req;
2813 struct smp_req *smp_req;
2814 void *kaddr;
2815
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002816 dma_unmap_sg(&ihost->pdev->dev, sg, 1, DMA_TO_DEVICE);
Dan Williamse9bf7092011-06-16 16:59:56 -07002817
2818 /* need to swab it back in case the command buffer is re-used */
2819 kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
2820 smp_req = kaddr + sg->offset;
2821 sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
2822 kunmap_atomic(kaddr, KM_IRQ0);
2823 break;
2824 }
Dan Williamsddcc7e32011-06-17 10:40:43 -07002825 default:
2826 break;
2827 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002828
2829 /* Put the completed request on the correct list */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002830 isci_task_save_for_upper_layer_completion(ihost, request, response,
Dan Williams6f231dd2011-07-02 22:56:22 -07002831 status, complete_to_host
2832 );
2833
2834 /* complete the io request to the core. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002835 scic_controller_complete_io(ihost, request->target_device, request);
Dan Williams209fae12011-06-13 17:39:44 -07002836 isci_put_device(idev);
2837
Dan Williams67ea8382011-05-08 11:47:15 -07002838 /* set terminated handle so it cannot be completed or
Dan Williams6f231dd2011-07-02 22:56:22 -07002839 * terminated again, and to cause any calls into abort
2840 * task to recognize the already completed case.
2841 */
Dan Williams38d88792011-06-23 14:33:48 -07002842 set_bit(IREQ_TERMINATED, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002843}
Dan Williamsf1f52e72011-05-10 02:28:45 -07002844
Dan Williams9269e0e2011-05-12 07:42:17 -07002845static void scic_sds_request_started_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002846{
Dan Williams5076a1a2011-06-27 14:57:03 -07002847 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams78a6f062011-06-30 16:31:37 -07002848 struct domain_device *dev = ireq->target_device->domain_dev;
Dan Williamsc72086e2011-05-10 02:28:48 -07002849 struct sas_task *task;
2850
2851 /* XXX as hch said always creating an internal sas_task for tmf
2852 * requests would simplify the driver
2853 */
2854 task = ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL;
Dan Williamsf1f52e72011-05-10 02:28:45 -07002855
Dan Williams5dec6f42011-05-10 02:28:49 -07002856 /* all unaccelerated request types (non ssp or ncq) handled with
2857 * substates
Dan Williamsf1393032011-05-10 02:28:47 -07002858 */
Dan Williamsc72086e2011-05-10 02:28:48 -07002859 if (!task && dev->dev_type == SAS_END_DEV) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002860 sci_change_state(sm, SCI_REQ_TASK_WAIT_TC_COMP);
Dan Williams5dec6f42011-05-10 02:28:49 -07002861 } else if (!task &&
2862 (isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_high ||
2863 isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_low)) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002864 sci_change_state(sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED);
Dan Williamsc72086e2011-05-10 02:28:48 -07002865 } else if (task && task->task_proto == SAS_PROTOCOL_SMP) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002866 sci_change_state(sm, SCI_REQ_SMP_WAIT_RESP);
Dan Williams5dec6f42011-05-10 02:28:49 -07002867 } else if (task && sas_protocol_ata(task->task_proto) &&
2868 !task->ata_task.use_ncq) {
2869 u32 state;
2870
2871 if (task->data_dir == DMA_NONE)
Edmund Nadolskie3013702011-06-02 00:10:43 +00002872 state = SCI_REQ_STP_NON_DATA_WAIT_H2D;
Dan Williams5dec6f42011-05-10 02:28:49 -07002873 else if (task->ata_task.dma_xfer)
Edmund Nadolskie3013702011-06-02 00:10:43 +00002874 state = SCI_REQ_STP_UDMA_WAIT_TC_COMP;
Dan Williams5dec6f42011-05-10 02:28:49 -07002875 else /* PIO */
Edmund Nadolskie3013702011-06-02 00:10:43 +00002876 state = SCI_REQ_STP_PIO_WAIT_H2D;
Dan Williams5dec6f42011-05-10 02:28:49 -07002877
Edmund Nadolskie3013702011-06-02 00:10:43 +00002878 sci_change_state(sm, state);
Dan Williamsc72086e2011-05-10 02:28:48 -07002879 }
Dan Williamsf1f52e72011-05-10 02:28:45 -07002880}
2881
Dan Williams9269e0e2011-05-12 07:42:17 -07002882static void scic_sds_request_completed_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002883{
Dan Williams5076a1a2011-06-27 14:57:03 -07002884 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002885 struct isci_host *ihost = ireq->owning_controller;
Dan Williamsf1f52e72011-05-10 02:28:45 -07002886
Dan Williamsf1f52e72011-05-10 02:28:45 -07002887 /* Tell the SCI_USER that the IO request is complete */
Dan Williams38d88792011-06-23 14:33:48 -07002888 if (!test_bit(IREQ_TMF, &ireq->flags))
Dan Williamsf1f52e72011-05-10 02:28:45 -07002889 isci_request_io_request_complete(ihost, ireq,
Dan Williams5076a1a2011-06-27 14:57:03 -07002890 ireq->sci_status);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002891 else
Dan Williams5076a1a2011-06-27 14:57:03 -07002892 isci_task_request_complete(ihost, ireq, ireq->sci_status);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002893}
2894
Dan Williams9269e0e2011-05-12 07:42:17 -07002895static void scic_sds_request_aborting_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002896{
Dan Williams5076a1a2011-06-27 14:57:03 -07002897 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002898
2899 /* Setting the abort bit in the Task Context is required by the silicon. */
Dan Williams5076a1a2011-06-27 14:57:03 -07002900 ireq->tc->abort = 1;
Dan Williamsc72086e2011-05-10 02:28:48 -07002901}
2902
Dan Williams9269e0e2011-05-12 07:42:17 -07002903static void scic_sds_stp_request_started_non_data_await_h2d_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07002904{
Dan Williams5076a1a2011-06-27 14:57:03 -07002905 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams5dec6f42011-05-10 02:28:49 -07002906
Dan Williams5076a1a2011-06-27 14:57:03 -07002907 scic_sds_remote_device_set_working_request(ireq->target_device,
2908 ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -07002909}
2910
Dan Williams9269e0e2011-05-12 07:42:17 -07002911static void scic_sds_stp_request_started_pio_await_h2d_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07002912{
Dan Williams5076a1a2011-06-27 14:57:03 -07002913 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams5dec6f42011-05-10 02:28:49 -07002914
Dan Williams5076a1a2011-06-27 14:57:03 -07002915 scic_sds_remote_device_set_working_request(ireq->target_device,
2916 ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -07002917}
2918
Dan Williams9269e0e2011-05-12 07:42:17 -07002919static void scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07002920{
Dan Williams5076a1a2011-06-27 14:57:03 -07002921 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams5dec6f42011-05-10 02:28:49 -07002922
Dan Williams5076a1a2011-06-27 14:57:03 -07002923 scic_sds_remote_device_set_working_request(ireq->target_device,
2924 ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -07002925}
2926
Dan Williams9269e0e2011-05-12 07:42:17 -07002927static void scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07002928{
Dan Williams5076a1a2011-06-27 14:57:03 -07002929 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
2930 struct scu_task_context *tc = ireq->tc;
Dan Williams5dec6f42011-05-10 02:28:49 -07002931 struct host_to_dev_fis *h2d_fis;
2932 enum sci_status status;
2933
2934 /* Clear the SRST bit */
Dan Williams5076a1a2011-06-27 14:57:03 -07002935 h2d_fis = &ireq->stp.cmd;
Dan Williams5dec6f42011-05-10 02:28:49 -07002936 h2d_fis->control = 0;
2937
2938 /* Clear the TC control bit */
Dan Williams312e0c22011-06-28 13:47:09 -07002939 tc->control_frame = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07002940
Dan Williams5076a1a2011-06-27 14:57:03 -07002941 status = scic_controller_continue_io(ireq);
Dan Williams79e2b6b2011-05-11 08:29:56 -07002942 WARN_ONCE(status != SCI_SUCCESS, "isci: continue io failure\n");
Dan Williams5dec6f42011-05-10 02:28:49 -07002943}
2944
Dan Williamsf1f52e72011-05-10 02:28:45 -07002945static const struct sci_base_state scic_sds_request_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002946 [SCI_REQ_INIT] = { },
2947 [SCI_REQ_CONSTRUCTED] = { },
2948 [SCI_REQ_STARTED] = {
Dan Williamsf1f52e72011-05-10 02:28:45 -07002949 .enter_state = scic_sds_request_started_state_enter,
Dan Williams5dec6f42011-05-10 02:28:49 -07002950 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002951 [SCI_REQ_STP_NON_DATA_WAIT_H2D] = {
Dan Williams5dec6f42011-05-10 02:28:49 -07002952 .enter_state = scic_sds_stp_request_started_non_data_await_h2d_completion_enter,
2953 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002954 [SCI_REQ_STP_NON_DATA_WAIT_D2H] = { },
2955 [SCI_REQ_STP_PIO_WAIT_H2D] = {
Dan Williams5dec6f42011-05-10 02:28:49 -07002956 .enter_state = scic_sds_stp_request_started_pio_await_h2d_completion_enter,
2957 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002958 [SCI_REQ_STP_PIO_WAIT_FRAME] = { },
2959 [SCI_REQ_STP_PIO_DATA_IN] = { },
2960 [SCI_REQ_STP_PIO_DATA_OUT] = { },
2961 [SCI_REQ_STP_UDMA_WAIT_TC_COMP] = { },
2962 [SCI_REQ_STP_UDMA_WAIT_D2H] = { },
2963 [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED] = {
Dan Williams5dec6f42011-05-10 02:28:49 -07002964 .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter,
2965 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002966 [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG] = {
Dan Williams5dec6f42011-05-10 02:28:49 -07002967 .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter,
2968 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002969 [SCI_REQ_STP_SOFT_RESET_WAIT_D2H] = { },
2970 [SCI_REQ_TASK_WAIT_TC_COMP] = { },
2971 [SCI_REQ_TASK_WAIT_TC_RESP] = { },
2972 [SCI_REQ_SMP_WAIT_RESP] = { },
2973 [SCI_REQ_SMP_WAIT_TC_COMP] = { },
2974 [SCI_REQ_COMPLETED] = {
Dan Williamsf1f52e72011-05-10 02:28:45 -07002975 .enter_state = scic_sds_request_completed_state_enter,
2976 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002977 [SCI_REQ_ABORTING] = {
Dan Williamsf1f52e72011-05-10 02:28:45 -07002978 .enter_state = scic_sds_request_aborting_state_enter,
2979 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002980 [SCI_REQ_FINAL] = { },
Dan Williamsf1f52e72011-05-10 02:28:45 -07002981};
2982
Edmund Nadolskie3013702011-06-02 00:10:43 +00002983static void
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002984scic_sds_general_request_construct(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07002985 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07002986 struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002987{
Dan Williams5076a1a2011-06-27 14:57:03 -07002988 sci_init_sm(&ireq->sm, scic_sds_request_state_table, SCI_REQ_INIT);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002989
Dan Williams78a6f062011-06-30 16:31:37 -07002990 ireq->target_device = idev;
Dan Williams5076a1a2011-06-27 14:57:03 -07002991 ireq->protocol = SCIC_NO_PROTOCOL;
2992 ireq->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
Dan Williamsf1f52e72011-05-10 02:28:45 -07002993
Dan Williams5076a1a2011-06-27 14:57:03 -07002994 ireq->sci_status = SCI_SUCCESS;
2995 ireq->scu_status = 0;
2996 ireq->post_context = 0xFFFFFFFF;
Dan Williamsf1f52e72011-05-10 02:28:45 -07002997}
2998
2999static enum sci_status
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003000scic_io_request_construct(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07003001 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003002 struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003003{
Dan Williams78a6f062011-06-30 16:31:37 -07003004 struct domain_device *dev = idev->domain_dev;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003005 enum sci_status status = SCI_SUCCESS;
3006
3007 /* Build the common part of the request */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003008 scic_sds_general_request_construct(ihost, idev, ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003009
Dan Williams78a6f062011-06-30 16:31:37 -07003010 if (idev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003011 return SCI_FAILURE_INVALID_REMOTE_DEVICE;
3012
3013 if (dev->dev_type == SAS_END_DEV)
Dan Williamsc72086e2011-05-10 02:28:48 -07003014 /* pass */;
3015 else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
Dan Williams5076a1a2011-06-27 14:57:03 -07003016 memset(&ireq->stp.cmd, 0, sizeof(ireq->stp.cmd));
Dan Williamsc72086e2011-05-10 02:28:48 -07003017 else if (dev_is_expander(dev))
Dan Williamse9bf7092011-06-16 16:59:56 -07003018 /* pass */;
Dan Williamsc72086e2011-05-10 02:28:48 -07003019 else
3020 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003021
Dan Williams5076a1a2011-06-27 14:57:03 -07003022 memset(ireq->tc, 0, offsetof(struct scu_task_context, sgl_pair_ab));
Dan Williamsf1f52e72011-05-10 02:28:45 -07003023
3024 return status;
3025}
3026
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003027enum sci_status scic_task_request_construct(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07003028 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003029 u16 io_tag, struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003030{
Dan Williams78a6f062011-06-30 16:31:37 -07003031 struct domain_device *dev = idev->domain_dev;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003032 enum sci_status status = SCI_SUCCESS;
3033
3034 /* Build the common part of the request */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003035 scic_sds_general_request_construct(ihost, idev, ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003036
Dan Williamsc72086e2011-05-10 02:28:48 -07003037 if (dev->dev_type == SAS_END_DEV ||
3038 dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams5076a1a2011-06-27 14:57:03 -07003039 set_bit(IREQ_TMF, &ireq->flags);
3040 memset(ireq->tc, 0, sizeof(struct scu_task_context));
Dan Williamsc72086e2011-05-10 02:28:48 -07003041 } else
3042 status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003043
3044 return status;
3045}
3046
3047static enum sci_status isci_request_ssp_request_construct(
3048 struct isci_request *request)
3049{
3050 enum sci_status status;
3051
3052 dev_dbg(&request->isci_host->pdev->dev,
3053 "%s: request = %p\n",
3054 __func__,
3055 request);
Dan Williams5076a1a2011-06-27 14:57:03 -07003056 status = scic_io_request_construct_basic_ssp(request);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003057 return status;
3058}
3059
3060static enum sci_status isci_request_stp_request_construct(
3061 struct isci_request *request)
3062{
3063 struct sas_task *task = isci_request_access_task(request);
3064 enum sci_status status;
3065 struct host_to_dev_fis *register_fis;
3066
3067 dev_dbg(&request->isci_host->pdev->dev,
3068 "%s: request = %p\n",
3069 __func__,
3070 request);
3071
3072 /* Get the host_to_dev_fis from the core and copy
3073 * the fis from the task into it.
3074 */
3075 register_fis = isci_sata_task_to_fis_copy(task);
3076
Dan Williams5076a1a2011-06-27 14:57:03 -07003077 status = scic_io_request_construct_basic_sata(request);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003078
3079 /* Set the ncq tag in the fis, from the queue
3080 * command in the task.
3081 */
3082 if (isci_sata_is_task_ncq(task)) {
3083
3084 isci_sata_set_ncq_tag(
3085 register_fis,
3086 task
3087 );
3088 }
3089
3090 return status;
3091}
3092
Dan Williamse9bf7092011-06-16 16:59:56 -07003093static enum sci_status
3094scic_io_request_construct_smp(struct device *dev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003095 struct isci_request *ireq,
Dan Williamse9bf7092011-06-16 16:59:56 -07003096 struct sas_task *task)
Dan Williamsc72086e2011-05-10 02:28:48 -07003097{
Dan Williamse9bf7092011-06-16 16:59:56 -07003098 struct scatterlist *sg = &task->smp_task.smp_req;
Dan Williams78a6f062011-06-30 16:31:37 -07003099 struct isci_remote_device *idev;
Dan Williamsc72086e2011-05-10 02:28:48 -07003100 struct scu_task_context *task_context;
Dan Williamsffe191c2011-06-29 13:09:25 -07003101 struct isci_port *iport;
Dan Williamse9bf7092011-06-16 16:59:56 -07003102 struct smp_req *smp_req;
3103 void *kaddr;
3104 u8 req_len;
3105 u32 cmd;
3106
3107 kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
3108 smp_req = kaddr + sg->offset;
3109 /*
3110 * Look at the SMP requests' header fields; for certain SAS 1.x SMP
3111 * functions under SAS 2.0, a zero request length really indicates
3112 * a non-zero default length.
3113 */
3114 if (smp_req->req_len == 0) {
3115 switch (smp_req->func) {
3116 case SMP_DISCOVER:
3117 case SMP_REPORT_PHY_ERR_LOG:
3118 case SMP_REPORT_PHY_SATA:
3119 case SMP_REPORT_ROUTE_INFO:
3120 smp_req->req_len = 2;
3121 break;
3122 case SMP_CONF_ROUTE_INFO:
3123 case SMP_PHY_CONTROL:
3124 case SMP_PHY_TEST_FUNCTION:
3125 smp_req->req_len = 9;
3126 break;
3127 /* Default - zero is a valid default for 2.0. */
3128 }
3129 }
3130 req_len = smp_req->req_len;
3131 sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
3132 cmd = *(u32 *) smp_req;
3133 kunmap_atomic(kaddr, KM_IRQ0);
3134
3135 if (!dma_map_sg(dev, sg, 1, DMA_TO_DEVICE))
3136 return SCI_FAILURE;
3137
Dan Williams5076a1a2011-06-27 14:57:03 -07003138 ireq->protocol = SCIC_SMP_PROTOCOL;
Dan Williamsc72086e2011-05-10 02:28:48 -07003139
3140 /* byte swap the smp request. */
Dan Williamsc72086e2011-05-10 02:28:48 -07003141
Dan Williams5076a1a2011-06-27 14:57:03 -07003142 task_context = ireq->tc;
Dan Williamsc72086e2011-05-10 02:28:48 -07003143
Dan Williams78a6f062011-06-30 16:31:37 -07003144 idev = scic_sds_request_get_device(ireq);
Dan Williamsffe191c2011-06-29 13:09:25 -07003145 iport = scic_sds_request_get_port(ireq);
Dan Williamsc72086e2011-05-10 02:28:48 -07003146
3147 /*
3148 * Fill in the TC with the its required data
3149 * 00h
3150 */
3151 task_context->priority = 0;
3152 task_context->initiator_request = 1;
Dan Williams78a6f062011-06-30 16:31:37 -07003153 task_context->connection_rate = idev->connection_rate;
Dan Williamsc72086e2011-05-10 02:28:48 -07003154 task_context->protocol_engine_index =
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003155 scic_sds_controller_get_protocol_engine_group(ihost);
Dan Williamsffe191c2011-06-29 13:09:25 -07003156 task_context->logical_port_index = scic_sds_port_get_index(iport);
Dan Williamsc72086e2011-05-10 02:28:48 -07003157 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
3158 task_context->abort = 0;
3159 task_context->valid = SCU_TASK_CONTEXT_VALID;
3160 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
3161
3162 /* 04h */
Dan Williams78a6f062011-06-30 16:31:37 -07003163 task_context->remote_node_index = idev->rnc.remote_node_index;
Dan Williamsc72086e2011-05-10 02:28:48 -07003164 task_context->command_code = 0;
3165 task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;
3166
3167 /* 08h */
3168 task_context->link_layer_control = 0;
3169 task_context->do_not_dma_ssp_good_response = 1;
3170 task_context->strict_ordering = 0;
3171 task_context->control_frame = 1;
3172 task_context->timeout_enable = 0;
3173 task_context->block_guard_enable = 0;
3174
3175 /* 0ch */
3176 task_context->address_modifier = 0;
3177
3178 /* 10h */
Dave Jiang77d67382011-05-25 02:21:57 +00003179 task_context->ssp_command_iu_length = req_len;
Dan Williamsc72086e2011-05-10 02:28:48 -07003180
3181 /* 14h */
3182 task_context->transfer_length_bytes = 0;
3183
3184 /*
3185 * 18h ~ 30h, protocol specific
3186 * since commandIU has been build by framework at this point, we just
3187 * copy the frist DWord from command IU to this location. */
Dan Williamse9bf7092011-06-16 16:59:56 -07003188 memcpy(&task_context->type.smp, &cmd, sizeof(u32));
Dan Williamsc72086e2011-05-10 02:28:48 -07003189
3190 /*
3191 * 40h
3192 * "For SMP you could program it to zero. We would prefer that way
3193 * so that done code will be consistent." - Venki
3194 */
3195 task_context->task_phase = 0;
3196
Dan Williams5076a1a2011-06-27 14:57:03 -07003197 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003198 (scic_sds_controller_get_protocol_engine_group(ihost) <<
Dan Williams312e0c22011-06-28 13:47:09 -07003199 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
Dan Williamsffe191c2011-06-29 13:09:25 -07003200 (scic_sds_port_get_index(iport) <<
Dan Williams312e0c22011-06-28 13:47:09 -07003201 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
Dan Williams5076a1a2011-06-27 14:57:03 -07003202 ISCI_TAG_TCI(ireq->io_tag));
Dan Williamsc72086e2011-05-10 02:28:48 -07003203 /*
3204 * Copy the physical address for the command buffer to the SCU Task
3205 * Context command buffer should not contain command header.
3206 */
Dan Williamse9bf7092011-06-16 16:59:56 -07003207 task_context->command_iu_upper = upper_32_bits(sg_dma_address(sg));
3208 task_context->command_iu_lower = lower_32_bits(sg_dma_address(sg) + sizeof(u32));
Dan Williamsc72086e2011-05-10 02:28:48 -07003209
3210 /* SMP response comes as UF, so no need to set response IU address. */
3211 task_context->response_iu_upper = 0;
3212 task_context->response_iu_lower = 0;
Dan Williamsc72086e2011-05-10 02:28:48 -07003213
Dan Williams5076a1a2011-06-27 14:57:03 -07003214 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williamsc72086e2011-05-10 02:28:48 -07003215
3216 return SCI_SUCCESS;
3217}
3218
3219/*
Dan Williamsf1f52e72011-05-10 02:28:45 -07003220 * isci_smp_request_build() - This function builds the smp request.
3221 * @ireq: This parameter points to the isci_request allocated in the
3222 * request construct function.
3223 *
3224 * SCI_SUCCESS on successfull completion, or specific failure code.
3225 */
3226static enum sci_status isci_smp_request_build(struct isci_request *ireq)
3227{
Dan Williamsf1f52e72011-05-10 02:28:45 -07003228 struct sas_task *task = isci_request_access_task(ireq);
Dan Williamse9bf7092011-06-16 16:59:56 -07003229 struct device *dev = &ireq->isci_host->pdev->dev;
Dan Williamse9bf7092011-06-16 16:59:56 -07003230 enum sci_status status = SCI_FAILURE;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003231
Dan Williams5076a1a2011-06-27 14:57:03 -07003232 status = scic_io_request_construct_smp(dev, ireq, task);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003233 if (status != SCI_SUCCESS)
3234 dev_warn(&ireq->isci_host->pdev->dev,
3235 "%s: failed with status = %d\n",
3236 __func__,
3237 status);
3238
3239 return status;
3240}
3241
3242/**
3243 * isci_io_request_build() - This function builds the io request object.
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003244 * @ihost: This parameter specifies the ISCI host object
Dan Williamsf1f52e72011-05-10 02:28:45 -07003245 * @request: This parameter points to the isci_request object allocated in the
3246 * request construct function.
3247 * @sci_device: This parameter is the handle for the sci core's remote device
3248 * object that is the destination for this request.
3249 *
3250 * SCI_SUCCESS on successfull completion, or specific failure code.
3251 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003252static enum sci_status isci_io_request_build(struct isci_host *ihost,
Dan Williams312e0c22011-06-28 13:47:09 -07003253 struct isci_request *request,
Dan Williams78a6f062011-06-30 16:31:37 -07003254 struct isci_remote_device *idev)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003255{
3256 enum sci_status status = SCI_SUCCESS;
3257 struct sas_task *task = isci_request_access_task(request);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003258
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003259 dev_dbg(&ihost->pdev->dev,
Dan Williams78a6f062011-06-30 16:31:37 -07003260 "%s: idev = 0x%p; request = %p, "
Dan Williamsf1f52e72011-05-10 02:28:45 -07003261 "num_scatter = %d\n",
3262 __func__,
Dan Williams78a6f062011-06-30 16:31:37 -07003263 idev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003264 request,
3265 task->num_scatter);
3266
3267 /* map the sgl addresses, if present.
3268 * libata does the mapping for sata devices
3269 * before we get the request.
3270 */
3271 if (task->num_scatter &&
3272 !sas_protocol_ata(task->task_proto) &&
3273 !(SAS_PROTOCOL_SMP & task->task_proto)) {
3274
3275 request->num_sg_entries = dma_map_sg(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003276 &ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003277 task->scatter,
3278 task->num_scatter,
3279 task->data_dir
3280 );
3281
3282 if (request->num_sg_entries == 0)
3283 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
3284 }
3285
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003286 status = scic_io_request_construct(ihost, idev, request);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003287
3288 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003289 dev_warn(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003290 "%s: failed request construct\n",
3291 __func__);
3292 return SCI_FAILURE;
3293 }
3294
3295 switch (task->task_proto) {
3296 case SAS_PROTOCOL_SMP:
3297 status = isci_smp_request_build(request);
3298 break;
3299 case SAS_PROTOCOL_SSP:
3300 status = isci_request_ssp_request_construct(request);
3301 break;
3302 case SAS_PROTOCOL_SATA:
3303 case SAS_PROTOCOL_STP:
3304 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
3305 status = isci_request_stp_request_construct(request);
3306 break;
3307 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003308 dev_warn(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003309 "%s: unknown protocol\n", __func__);
3310 return SCI_FAILURE;
3311 }
3312
3313 return SCI_SUCCESS;
3314}
3315
Dan Williamsdb056252011-06-17 14:18:39 -07003316static struct isci_request *isci_request_from_tag(struct isci_host *ihost, u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003317{
Dan Williams0d0cf142011-06-13 00:51:30 -07003318 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003319
Dan Williamsdb056252011-06-17 14:18:39 -07003320 ireq = ihost->reqs[ISCI_TAG_TCI(tag)];
Dan Williams5076a1a2011-06-27 14:57:03 -07003321 ireq->io_tag = tag;
Dan Williams0d0cf142011-06-13 00:51:30 -07003322 ireq->io_request_completion = NULL;
Dan Williams38d88792011-06-23 14:33:48 -07003323 ireq->flags = 0;
Dan Williams0d0cf142011-06-13 00:51:30 -07003324 ireq->num_sg_entries = 0;
Dan Williams0d0cf142011-06-13 00:51:30 -07003325 INIT_LIST_HEAD(&ireq->completed_node);
3326 INIT_LIST_HEAD(&ireq->dev_node);
Dan Williams0d0cf142011-06-13 00:51:30 -07003327 isci_request_change_state(ireq, allocated);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003328
Dan Williams0d0cf142011-06-13 00:51:30 -07003329 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003330}
3331
Dan Williamsdb056252011-06-17 14:18:39 -07003332static struct isci_request *isci_io_request_from_tag(struct isci_host *ihost,
3333 struct sas_task *task,
3334 u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003335{
Dan Williams0d0cf142011-06-13 00:51:30 -07003336 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003337
Dan Williamsdb056252011-06-17 14:18:39 -07003338 ireq = isci_request_from_tag(ihost, tag);
3339 ireq->ttype_ptr.io_task_ptr = task;
3340 ireq->ttype = io_task;
3341 task->lldd_task = ireq;
3342
Dan Williams0d0cf142011-06-13 00:51:30 -07003343 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003344}
3345
Dan Williamsdb056252011-06-17 14:18:39 -07003346struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
3347 struct isci_tmf *isci_tmf,
3348 u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003349{
Dan Williams0d0cf142011-06-13 00:51:30 -07003350 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003351
Dan Williamsdb056252011-06-17 14:18:39 -07003352 ireq = isci_request_from_tag(ihost, tag);
3353 ireq->ttype_ptr.tmf_task_ptr = isci_tmf;
3354 ireq->ttype = tmf_task;
3355
Dan Williams0d0cf142011-06-13 00:51:30 -07003356 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003357}
3358
Dan Williams209fae12011-06-13 17:39:44 -07003359int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
Dan Williamsdb056252011-06-17 14:18:39 -07003360 struct sas_task *task, u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003361{
Dan Williamsf1f52e72011-05-10 02:28:45 -07003362 enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams0d0cf142011-06-13 00:51:30 -07003363 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003364 unsigned long flags;
Dan Williams0d0cf142011-06-13 00:51:30 -07003365 int ret = 0;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003366
Dan Williamsf1f52e72011-05-10 02:28:45 -07003367 /* do common allocation and init of request object. */
Dan Williamsdb056252011-06-17 14:18:39 -07003368 ireq = isci_io_request_from_tag(ihost, task, tag);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003369
Dan Williamsdb056252011-06-17 14:18:39 -07003370 status = isci_io_request_build(ihost, ireq, idev);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003371 if (status != SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -07003372 dev_warn(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003373 "%s: request_construct failed - status = 0x%x\n",
3374 __func__,
3375 status);
Dan Williamsdb056252011-06-17 14:18:39 -07003376 return status;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003377 }
3378
Dan Williams0d0cf142011-06-13 00:51:30 -07003379 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003380
Jeff Skirvin9274f452011-06-23 17:09:02 -07003381 if (test_bit(IDEV_IO_NCQERROR, &idev->flags)) {
3382
3383 if (isci_task_is_ncq_recovery(task)) {
3384
3385 /* The device is in an NCQ recovery state. Issue the
3386 * request on the task side. Note that it will
3387 * complete on the I/O request side because the
3388 * request was built that way (ie.
3389 * ireq->is_task_management_request is false).
3390 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003391 status = scic_controller_start_task(ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07003392 idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003393 ireq);
Jeff Skirvin9274f452011-06-23 17:09:02 -07003394 } else {
3395 status = SCI_FAILURE;
3396 }
3397 } else {
Jeff Skirvin9274f452011-06-23 17:09:02 -07003398 /* send the request, let the core assign the IO TAG. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003399 status = scic_controller_start_io(ihost, idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003400 ireq);
Jeff Skirvin9274f452011-06-23 17:09:02 -07003401 }
Dan Williams312e0c22011-06-28 13:47:09 -07003402
Dan Williamsf1f52e72011-05-10 02:28:45 -07003403 if (status != SCI_SUCCESS &&
3404 status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
Dan Williams0d0cf142011-06-13 00:51:30 -07003405 dev_warn(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003406 "%s: failed request start (0x%x)\n",
3407 __func__, status);
Dan Williams0d0cf142011-06-13 00:51:30 -07003408 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsdb056252011-06-17 14:18:39 -07003409 return status;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003410 }
3411
3412 /* Either I/O started OK, or the core has signaled that
3413 * the device needs a target reset.
3414 *
3415 * In either case, hold onto the I/O for later.
3416 *
3417 * Update it's status and add it to the list in the
3418 * remote device object.
3419 */
Dan Williams0d0cf142011-06-13 00:51:30 -07003420 list_add(&ireq->dev_node, &idev->reqs_in_process);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003421
3422 if (status == SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -07003423 isci_request_change_state(ireq, started);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003424 } else {
3425 /* The request did not really start in the
3426 * hardware, so clear the request handle
3427 * here so no terminations will be done.
3428 */
Dan Williams38d88792011-06-23 14:33:48 -07003429 set_bit(IREQ_TERMINATED, &ireq->flags);
Dan Williams0d0cf142011-06-13 00:51:30 -07003430 isci_request_change_state(ireq, completed);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003431 }
Dan Williams0d0cf142011-06-13 00:51:30 -07003432 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003433
3434 if (status ==
3435 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
3436 /* Signal libsas that we need the SCSI error
Dan Williams312e0c22011-06-28 13:47:09 -07003437 * handler thread to work on this I/O and that
3438 * we want a device reset.
3439 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07003440 spin_lock_irqsave(&task->task_state_lock, flags);
3441 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
3442 spin_unlock_irqrestore(&task->task_state_lock, flags);
3443
3444 /* Cause this task to be scheduled in the SCSI error
Dan Williams312e0c22011-06-28 13:47:09 -07003445 * handler thread.
3446 */
Dan Williams0d0cf142011-06-13 00:51:30 -07003447 isci_execpath_callback(ihost, task,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003448 sas_task_abort);
3449
3450 /* Change the status, since we are holding
Dan Williams312e0c22011-06-28 13:47:09 -07003451 * the I/O until it is managed by the SCSI
3452 * error handler.
3453 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07003454 status = SCI_SUCCESS;
3455 }
3456
Dan Williamsf1f52e72011-05-10 02:28:45 -07003457 return ret;
3458}