blob: a46e07ac789f64afcd2a6360ad2574fbda39d76b [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"
Dan Williams6f231dd2011-07-02 22:56:22 -070059#include "scu_completion_codes.h"
Dan Williams5dec6f42011-05-10 02:28:49 -070060#include "scu_event_codes.h"
Dave Jiang2ec53eb2011-05-04 18:01:22 -070061#include "sas.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070062
Dan Williams5076a1a2011-06-27 14:57:03 -070063static struct scu_sgl_element_pair *to_sgl_element_pair(struct isci_request *ireq,
Dan Williams312e0c22011-06-28 13:47:09 -070064 int idx)
65{
66 if (idx == 0)
Dan Williams5076a1a2011-06-27 14:57:03 -070067 return &ireq->tc->sgl_pair_ab;
Dan Williams312e0c22011-06-28 13:47:09 -070068 else if (idx == 1)
Dan Williams5076a1a2011-06-27 14:57:03 -070069 return &ireq->tc->sgl_pair_cd;
Dan Williams312e0c22011-06-28 13:47:09 -070070 else if (idx < 0)
71 return NULL;
72 else
Dan Williams5076a1a2011-06-27 14:57:03 -070073 return &ireq->sg_table[idx - 2];
Dan Williams6f231dd2011-07-02 22:56:22 -070074}
75
Dan Williamsd9dcb4b2011-06-30 17:38:32 -070076static dma_addr_t to_sgl_element_pair_dma(struct isci_host *ihost,
Dan Williams5076a1a2011-06-27 14:57:03 -070077 struct isci_request *ireq, u32 idx)
Dan Williams312e0c22011-06-28 13:47:09 -070078{
79 u32 offset;
80
81 if (idx == 0) {
Dan Williams5076a1a2011-06-27 14:57:03 -070082 offset = (void *) &ireq->tc->sgl_pair_ab -
Dan Williamsd9dcb4b2011-06-30 17:38:32 -070083 (void *) &ihost->task_context_table[0];
84 return ihost->task_context_dma + offset;
Dan Williams312e0c22011-06-28 13:47:09 -070085 } else if (idx == 1) {
Dan Williams5076a1a2011-06-27 14:57:03 -070086 offset = (void *) &ireq->tc->sgl_pair_cd -
Dan Williamsd9dcb4b2011-06-30 17:38:32 -070087 (void *) &ihost->task_context_table[0];
88 return ihost->task_context_dma + offset;
Dan Williams312e0c22011-06-28 13:47:09 -070089 }
90
Dan Williams89a73012011-06-30 19:14:33 -070091 return sci_io_request_get_dma_addr(ireq, &ireq->sg_table[idx - 2]);
Dan Williams312e0c22011-06-28 13:47:09 -070092}
93
94static void init_sgl_element(struct scu_sgl_element *e, struct scatterlist *sg)
95{
96 e->length = sg_dma_len(sg);
97 e->address_upper = upper_32_bits(sg_dma_address(sg));
98 e->address_lower = lower_32_bits(sg_dma_address(sg));
99 e->address_modifier = 0;
100}
101
Dan Williams89a73012011-06-30 19:14:33 -0700102static void sci_request_build_sgl(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700103{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700104 struct isci_host *ihost = ireq->isci_host;
Dan Williams5076a1a2011-06-27 14:57:03 -0700105 struct sas_task *task = isci_request_access_task(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700106 struct scatterlist *sg = NULL;
107 dma_addr_t dma_addr;
108 u32 sg_idx = 0;
109 struct scu_sgl_element_pair *scu_sg = NULL;
110 struct scu_sgl_element_pair *prev_sg = NULL;
111
112 if (task->num_scatter > 0) {
113 sg = task->scatter;
114
115 while (sg) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700116 scu_sg = to_sgl_element_pair(ireq, sg_idx);
Dan Williams312e0c22011-06-28 13:47:09 -0700117 init_sgl_element(&scu_sg->A, sg);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700118 sg = sg_next(sg);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700119 if (sg) {
Dan Williams312e0c22011-06-28 13:47:09 -0700120 init_sgl_element(&scu_sg->B, sg);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700121 sg = sg_next(sg);
122 } else
Dan Williams312e0c22011-06-28 13:47:09 -0700123 memset(&scu_sg->B, 0, sizeof(scu_sg->B));
Dan Williamsf1f52e72011-05-10 02:28:45 -0700124
125 if (prev_sg) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700126 dma_addr = to_sgl_element_pair_dma(ihost,
Dan Williams5076a1a2011-06-27 14:57:03 -0700127 ireq,
Dan Williams312e0c22011-06-28 13:47:09 -0700128 sg_idx);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700129
130 prev_sg->next_pair_upper =
131 upper_32_bits(dma_addr);
132 prev_sg->next_pair_lower =
133 lower_32_bits(dma_addr);
134 }
135
136 prev_sg = scu_sg;
137 sg_idx++;
138 }
139 } else { /* handle when no sg */
Dan Williams5076a1a2011-06-27 14:57:03 -0700140 scu_sg = to_sgl_element_pair(ireq, sg_idx);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700141
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700142 dma_addr = dma_map_single(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700143 task->scatter,
144 task->total_xfer_len,
145 task->data_dir);
146
Dan Williams5076a1a2011-06-27 14:57:03 -0700147 ireq->zero_scatter_daddr = dma_addr;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700148
149 scu_sg->A.length = task->total_xfer_len;
150 scu_sg->A.address_upper = upper_32_bits(dma_addr);
151 scu_sg->A.address_lower = lower_32_bits(dma_addr);
152 }
153
154 if (scu_sg) {
155 scu_sg->next_pair_upper = 0;
156 scu_sg->next_pair_lower = 0;
157 }
158}
159
Dan Williams89a73012011-06-30 19:14:33 -0700160static void sci_io_request_build_ssp_command_iu(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700161{
162 struct ssp_cmd_iu *cmd_iu;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700163 struct sas_task *task = isci_request_access_task(ireq);
164
Dan Williams5076a1a2011-06-27 14:57:03 -0700165 cmd_iu = &ireq->ssp.cmd;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700166
167 memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
168 cmd_iu->add_cdb_len = 0;
169 cmd_iu->_r_a = 0;
170 cmd_iu->_r_b = 0;
171 cmd_iu->en_fburst = 0; /* unsupported */
172 cmd_iu->task_prio = task->ssp_task.task_prio;
173 cmd_iu->task_attr = task->ssp_task.task_attr;
174 cmd_iu->_r_c = 0;
175
176 sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cdb,
177 sizeof(task->ssp_task.cdb) / sizeof(u32));
178}
179
Dan Williams89a73012011-06-30 19:14:33 -0700180static void sci_task_request_build_ssp_task_iu(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700181{
182 struct ssp_task_iu *task_iu;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700183 struct sas_task *task = isci_request_access_task(ireq);
184 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
185
Dan Williams5076a1a2011-06-27 14:57:03 -0700186 task_iu = &ireq->ssp.tmf;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700187
188 memset(task_iu, 0, sizeof(struct ssp_task_iu));
189
190 memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
191
192 task_iu->task_func = isci_tmf->tmf_code;
193 task_iu->task_tag =
194 (ireq->ttype == tmf_task) ?
195 isci_tmf->io_tag :
196 SCI_CONTROLLER_INVALID_IO_TAG;
197}
198
199/**
200 * This method is will fill in the SCU Task Context for any type of SSP request.
201 * @sci_req:
202 * @task_context:
203 *
204 */
205static void scu_ssp_reqeust_construct_task_context(
Dan Williams5076a1a2011-06-27 14:57:03 -0700206 struct isci_request *ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700207 struct scu_task_context *task_context)
208{
209 dma_addr_t dma_addr;
Dan Williams78a6f062011-06-30 16:31:37 -0700210 struct isci_remote_device *idev;
Dan Williamsffe191c2011-06-29 13:09:25 -0700211 struct isci_port *iport;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700212
Dan Williams34a99152011-07-01 02:25:15 -0700213 idev = ireq->target_device;
214 iport = idev->owning_port;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700215
216 /* Fill in the TC with the its required data */
217 task_context->abort = 0;
218 task_context->priority = 0;
219 task_context->initiator_request = 1;
Dan Williams78a6f062011-06-30 16:31:37 -0700220 task_context->connection_rate = idev->connection_rate;
Dan Williams34a99152011-07-01 02:25:15 -0700221 task_context->protocol_engine_index = ISCI_PEG;
222 task_context->logical_port_index = iport->physical_port_index;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700223 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
224 task_context->valid = SCU_TASK_CONTEXT_VALID;
225 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
226
Dan Williams34a99152011-07-01 02:25:15 -0700227 task_context->remote_node_index = idev->rnc.remote_node_index;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700228 task_context->command_code = 0;
229
230 task_context->link_layer_control = 0;
231 task_context->do_not_dma_ssp_good_response = 1;
232 task_context->strict_ordering = 0;
233 task_context->control_frame = 0;
234 task_context->timeout_enable = 0;
235 task_context->block_guard_enable = 0;
236
237 task_context->address_modifier = 0;
238
Dan Williams5076a1a2011-06-27 14:57:03 -0700239 /* task_context->type.ssp.tag = ireq->io_tag; */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700240 task_context->task_phase = 0x01;
241
Dan Williams5076a1a2011-06-27 14:57:03 -0700242 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williams34a99152011-07-01 02:25:15 -0700243 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
244 (iport->physical_port_index <<
Dan Williamsffe191c2011-06-29 13:09:25 -0700245 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
246 ISCI_TAG_TCI(ireq->io_tag));
Dan Williamsf1f52e72011-05-10 02:28:45 -0700247
248 /*
249 * Copy the physical address for the command buffer to the
250 * SCU Task Context
251 */
Dan Williams89a73012011-06-30 19:14:33 -0700252 dma_addr = sci_io_request_get_dma_addr(ireq, &ireq->ssp.cmd);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700253
254 task_context->command_iu_upper = upper_32_bits(dma_addr);
255 task_context->command_iu_lower = lower_32_bits(dma_addr);
256
257 /*
258 * Copy the physical address for the response buffer to the
259 * SCU Task Context
260 */
Dan Williams89a73012011-06-30 19:14:33 -0700261 dma_addr = sci_io_request_get_dma_addr(ireq, &ireq->ssp.rsp);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700262
263 task_context->response_iu_upper = upper_32_bits(dma_addr);
264 task_context->response_iu_lower = lower_32_bits(dma_addr);
265}
266
267/**
268 * This method is will fill in the SCU Task Context for a SSP IO request.
269 * @sci_req:
270 *
271 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700272static void scu_ssp_io_request_construct_task_context(struct isci_request *ireq,
Dan Williams312e0c22011-06-28 13:47:09 -0700273 enum dma_data_direction dir,
274 u32 len)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700275{
Dan Williams5076a1a2011-06-27 14:57:03 -0700276 struct scu_task_context *task_context = ireq->tc;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700277
Dan Williams5076a1a2011-06-27 14:57:03 -0700278 scu_ssp_reqeust_construct_task_context(ireq, task_context);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700279
280 task_context->ssp_command_iu_length =
281 sizeof(struct ssp_cmd_iu) / sizeof(u32);
282 task_context->type.ssp.frame_type = SSP_COMMAND;
283
284 switch (dir) {
285 case DMA_FROM_DEVICE:
286 case DMA_NONE:
287 default:
288 task_context->task_type = SCU_TASK_TYPE_IOREAD;
289 break;
290 case DMA_TO_DEVICE:
291 task_context->task_type = SCU_TASK_TYPE_IOWRITE;
292 break;
293 }
294
295 task_context->transfer_length_bytes = len;
296
297 if (task_context->transfer_length_bytes > 0)
Dan Williams89a73012011-06-30 19:14:33 -0700298 sci_request_build_sgl(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700299}
300
Dan Williamsf1f52e72011-05-10 02:28:45 -0700301/**
302 * This method will fill in the SCU Task Context for a SSP Task request. The
303 * following important settings are utilized: -# priority ==
304 * SCU_TASK_PRIORITY_HIGH. This ensures that the task request is issued
305 * ahead of other task destined for the same Remote Node. -# task_type ==
306 * SCU_TASK_TYPE_IOREAD. This simply indicates that a normal request type
307 * (i.e. non-raw frame) is being utilized to perform task management. -#
308 * control_frame == 1. This ensures that the proper endianess is set so
309 * that the bytes are transmitted in the right order for a task frame.
310 * @sci_req: This parameter specifies the task request object being
311 * constructed.
312 *
313 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700314static void scu_ssp_task_request_construct_task_context(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700315{
Dan Williams5076a1a2011-06-27 14:57:03 -0700316 struct scu_task_context *task_context = ireq->tc;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700317
Dan Williams5076a1a2011-06-27 14:57:03 -0700318 scu_ssp_reqeust_construct_task_context(ireq, task_context);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700319
320 task_context->control_frame = 1;
321 task_context->priority = SCU_TASK_PRIORITY_HIGH;
322 task_context->task_type = SCU_TASK_TYPE_RAW_FRAME;
323 task_context->transfer_length_bytes = 0;
324 task_context->type.ssp.frame_type = SSP_TASK;
325 task_context->ssp_command_iu_length =
326 sizeof(struct ssp_task_iu) / sizeof(u32);
327}
328
Dan Williamsf1f52e72011-05-10 02:28:45 -0700329/**
Dan Williams5dec6f42011-05-10 02:28:49 -0700330 * This method is will fill in the SCU Task Context for any type of SATA
331 * request. This is called from the various SATA constructors.
332 * @sci_req: The general IO request object which is to be used in
333 * constructing the SCU task context.
334 * @task_context: The buffer pointer for the SCU task context which is being
335 * constructed.
Dan Williamsf1f52e72011-05-10 02:28:45 -0700336 *
Dan Williams5dec6f42011-05-10 02:28:49 -0700337 * The general io request construction is complete. The buffer assignment for
338 * the command buffer is complete. none Revisit task context construction to
339 * determine what is common for SSP/SMP/STP task context structures.
Dan Williamsf1f52e72011-05-10 02:28:45 -0700340 */
Dan Williams5dec6f42011-05-10 02:28:49 -0700341static void scu_sata_reqeust_construct_task_context(
Dan Williams5076a1a2011-06-27 14:57:03 -0700342 struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -0700343 struct scu_task_context *task_context)
344{
345 dma_addr_t dma_addr;
Dan Williams78a6f062011-06-30 16:31:37 -0700346 struct isci_remote_device *idev;
Dan Williamsffe191c2011-06-29 13:09:25 -0700347 struct isci_port *iport;
Dan Williams5dec6f42011-05-10 02:28:49 -0700348
Dan Williams34a99152011-07-01 02:25:15 -0700349 idev = ireq->target_device;
350 iport = idev->owning_port;
Dan Williams5dec6f42011-05-10 02:28:49 -0700351
352 /* Fill in the TC with the its required data */
353 task_context->abort = 0;
354 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
355 task_context->initiator_request = 1;
Dan Williams78a6f062011-06-30 16:31:37 -0700356 task_context->connection_rate = idev->connection_rate;
Dan Williams34a99152011-07-01 02:25:15 -0700357 task_context->protocol_engine_index = ISCI_PEG;
358 task_context->logical_port_index = iport->physical_port_index;
Dan Williams5dec6f42011-05-10 02:28:49 -0700359 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
360 task_context->valid = SCU_TASK_CONTEXT_VALID;
361 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
362
Dan Williams34a99152011-07-01 02:25:15 -0700363 task_context->remote_node_index = idev->rnc.remote_node_index;
Dan Williams5dec6f42011-05-10 02:28:49 -0700364 task_context->command_code = 0;
365
366 task_context->link_layer_control = 0;
367 task_context->do_not_dma_ssp_good_response = 1;
368 task_context->strict_ordering = 0;
369 task_context->control_frame = 0;
370 task_context->timeout_enable = 0;
371 task_context->block_guard_enable = 0;
372
373 task_context->address_modifier = 0;
374 task_context->task_phase = 0x01;
375
376 task_context->ssp_command_iu_length =
377 (sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);
378
379 /* Set the first word of the H2D REG FIS */
Dan Williams5076a1a2011-06-27 14:57:03 -0700380 task_context->type.words[0] = *(u32 *)&ireq->stp.cmd;
Dan Williams5dec6f42011-05-10 02:28:49 -0700381
Dan Williams5076a1a2011-06-27 14:57:03 -0700382 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williams34a99152011-07-01 02:25:15 -0700383 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
384 (iport->physical_port_index <<
385 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
386 ISCI_TAG_TCI(ireq->io_tag));
Dan Williams5dec6f42011-05-10 02:28:49 -0700387 /*
388 * Copy the physical address for the command buffer to the SCU Task
389 * Context. We must offset the command buffer by 4 bytes because the
390 * first 4 bytes are transfered in the body of the TC.
391 */
Dan Williams89a73012011-06-30 19:14:33 -0700392 dma_addr = sci_io_request_get_dma_addr(ireq,
Dan Williams5076a1a2011-06-27 14:57:03 -0700393 ((char *) &ireq->stp.cmd) +
Dan Williams5dec6f42011-05-10 02:28:49 -0700394 sizeof(u32));
395
396 task_context->command_iu_upper = upper_32_bits(dma_addr);
397 task_context->command_iu_lower = lower_32_bits(dma_addr);
398
399 /* SATA Requests do not have a response buffer */
400 task_context->response_iu_upper = 0;
401 task_context->response_iu_lower = 0;
402}
403
Dan Williams5076a1a2011-06-27 14:57:03 -0700404static void scu_stp_raw_request_construct_task_context(struct isci_request *ireq)
Dan Williams5dec6f42011-05-10 02:28:49 -0700405{
Dan Williams5076a1a2011-06-27 14:57:03 -0700406 struct scu_task_context *task_context = ireq->tc;
Dan Williams5dec6f42011-05-10 02:28:49 -0700407
Dan Williams5076a1a2011-06-27 14:57:03 -0700408 scu_sata_reqeust_construct_task_context(ireq, task_context);
Dan Williams5dec6f42011-05-10 02:28:49 -0700409
410 task_context->control_frame = 0;
411 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
412 task_context->task_type = SCU_TASK_TYPE_SATA_RAW_FRAME;
413 task_context->type.stp.fis_type = FIS_REGH2D;
414 task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
415}
416
Dan Williams89a73012011-06-30 19:14:33 -0700417static enum sci_status sci_stp_pio_request_construct(struct isci_request *ireq,
Dan Williams5076a1a2011-06-27 14:57:03 -0700418 bool copy_rx_frame)
Dan Williams5dec6f42011-05-10 02:28:49 -0700419{
Dan Williams5076a1a2011-06-27 14:57:03 -0700420 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williams5dec6f42011-05-10 02:28:49 -0700421
Dan Williams5076a1a2011-06-27 14:57:03 -0700422 scu_stp_raw_request_construct_task_context(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700423
Dan Williamsba7cb222011-06-27 11:56:41 -0700424 stp_req->status = 0;
425 stp_req->sgl.offset = 0;
426 stp_req->sgl.set = SCU_SGL_ELEMENT_PAIR_A;
Dan Williams5dec6f42011-05-10 02:28:49 -0700427
428 if (copy_rx_frame) {
Dan Williams89a73012011-06-30 19:14:33 -0700429 sci_request_build_sgl(ireq);
Dan Williamsba7cb222011-06-27 11:56:41 -0700430 stp_req->sgl.index = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -0700431 } else {
432 /* The user does not want the data copied to the SGL buffer location */
Dan Williamsba7cb222011-06-27 11:56:41 -0700433 stp_req->sgl.index = -1;
Dan Williams5dec6f42011-05-10 02:28:49 -0700434 }
435
436 return SCI_SUCCESS;
437}
438
439/**
440 *
441 * @sci_req: This parameter specifies the request to be constructed as an
442 * optimized request.
443 * @optimized_task_type: This parameter specifies whether the request is to be
444 * an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
445 * value of 1 indicates NCQ.
446 *
447 * This method will perform request construction common to all types of STP
448 * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
449 * returns an indication as to whether the construction was successful.
450 */
Dan Williams89a73012011-06-30 19:14:33 -0700451static void sci_stp_optimized_request_construct(struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -0700452 u8 optimized_task_type,
453 u32 len,
454 enum dma_data_direction dir)
455{
Dan Williams5076a1a2011-06-27 14:57:03 -0700456 struct scu_task_context *task_context = ireq->tc;
Dan Williams5dec6f42011-05-10 02:28:49 -0700457
458 /* Build the STP task context structure */
Dan Williams5076a1a2011-06-27 14:57:03 -0700459 scu_sata_reqeust_construct_task_context(ireq, task_context);
Dan Williams5dec6f42011-05-10 02:28:49 -0700460
461 /* Copy over the SGL elements */
Dan Williams89a73012011-06-30 19:14:33 -0700462 sci_request_build_sgl(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700463
464 /* Copy over the number of bytes to be transfered */
465 task_context->transfer_length_bytes = len;
466
467 if (dir == DMA_TO_DEVICE) {
468 /*
469 * The difference between the DMA IN and DMA OUT request task type
470 * values are consistent with the difference between FPDMA READ
471 * and FPDMA WRITE values. Add the supplied task type parameter
472 * to this difference to set the task type properly for this
473 * DATA OUT (WRITE) case. */
474 task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
475 - SCU_TASK_TYPE_DMA_IN);
476 } else {
477 /*
478 * For the DATA IN (READ) case, simply save the supplied
479 * optimized task type. */
480 task_context->task_type = optimized_task_type;
481 }
482}
483
484
485
Dan Williamsf1f52e72011-05-10 02:28:45 -0700486static enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -0700487sci_io_request_construct_sata(struct isci_request *ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700488 u32 len,
489 enum dma_data_direction dir,
490 bool copy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700491{
Dan Williams6f231dd2011-07-02 22:56:22 -0700492 enum sci_status status = SCI_SUCCESS;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700493 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700494
Dan Williamsf1f52e72011-05-10 02:28:45 -0700495 /* check for management protocols */
496 if (ireq->ttype == tmf_task) {
497 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700498
Dan Williamsf1f52e72011-05-10 02:28:45 -0700499 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
Dan Williams5dec6f42011-05-10 02:28:49 -0700500 tmf->tmf_code == isci_tmf_sata_srst_low) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700501 scu_stp_raw_request_construct_task_context(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700502 return SCI_SUCCESS;
503 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700504 dev_err(&ireq->owning_controller->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700505 "%s: Request 0x%p received un-handled SAT "
506 "management protocol 0x%x.\n",
Dan Williams5076a1a2011-06-27 14:57:03 -0700507 __func__, ireq, tmf->tmf_code);
Dan Williams6f231dd2011-07-02 22:56:22 -0700508
Dan Williamsf1f52e72011-05-10 02:28:45 -0700509 return SCI_FAILURE;
510 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700511 }
512
Dan Williamsf1f52e72011-05-10 02:28:45 -0700513 if (!sas_protocol_ata(task->task_proto)) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700514 dev_err(&ireq->owning_controller->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700515 "%s: Non-ATA protocol in SATA path: 0x%x\n",
516 __func__,
517 task->task_proto);
Dan Williams6f231dd2011-07-02 22:56:22 -0700518 return SCI_FAILURE;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700519
Dan Williams6f231dd2011-07-02 22:56:22 -0700520 }
521
Dan Williamsf1f52e72011-05-10 02:28:45 -0700522 /* non data */
Dan Williams5dec6f42011-05-10 02:28:49 -0700523 if (task->data_dir == DMA_NONE) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700524 scu_stp_raw_request_construct_task_context(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700525 return SCI_SUCCESS;
526 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700527
528 /* NCQ */
Dan Williams5dec6f42011-05-10 02:28:49 -0700529 if (task->ata_task.use_ncq) {
Dan Williams89a73012011-06-30 19:14:33 -0700530 sci_stp_optimized_request_construct(ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -0700531 SCU_TASK_TYPE_FPDMAQ_READ,
532 len, dir);
533 return SCI_SUCCESS;
534 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700535
536 /* DMA */
Dan Williams5dec6f42011-05-10 02:28:49 -0700537 if (task->ata_task.dma_xfer) {
Dan Williams89a73012011-06-30 19:14:33 -0700538 sci_stp_optimized_request_construct(ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -0700539 SCU_TASK_TYPE_DMA_IN,
540 len, dir);
541 return SCI_SUCCESS;
542 } else /* PIO */
Dan Williams89a73012011-06-30 19:14:33 -0700543 return sci_stp_pio_request_construct(ireq, copy);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700544
545 return status;
546}
547
Dan Williams89a73012011-06-30 19:14:33 -0700548static enum sci_status sci_io_request_construct_basic_ssp(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700549{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700550 struct sas_task *task = isci_request_access_task(ireq);
551
Dan Williams5076a1a2011-06-27 14:57:03 -0700552 ireq->protocol = SCIC_SSP_PROTOCOL;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700553
Dan Williams5076a1a2011-06-27 14:57:03 -0700554 scu_ssp_io_request_construct_task_context(ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700555 task->data_dir,
556 task->total_xfer_len);
557
Dan Williams89a73012011-06-30 19:14:33 -0700558 sci_io_request_build_ssp_command_iu(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700559
Dan Williams5076a1a2011-06-27 14:57:03 -0700560 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700561
562 return SCI_SUCCESS;
563}
564
Dan Williams89a73012011-06-30 19:14:33 -0700565enum sci_status sci_task_request_construct_ssp(
Dan Williams5076a1a2011-06-27 14:57:03 -0700566 struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700567{
568 /* Construct the SSP Task SCU Task Context */
Dan Williams5076a1a2011-06-27 14:57:03 -0700569 scu_ssp_task_request_construct_task_context(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700570
571 /* Fill in the SSP Task IU */
Dan Williams89a73012011-06-30 19:14:33 -0700572 sci_task_request_build_ssp_task_iu(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700573
Dan Williams5076a1a2011-06-27 14:57:03 -0700574 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williams6f231dd2011-07-02 22:56:22 -0700575
576 return SCI_SUCCESS;
577}
578
Dan Williams89a73012011-06-30 19:14:33 -0700579static enum sci_status sci_io_request_construct_basic_sata(struct isci_request *ireq)
Dan Williams6f231dd2011-07-02 22:56:22 -0700580{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700581 enum sci_status status;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700582 bool copy = false;
Dan Williams5076a1a2011-06-27 14:57:03 -0700583 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700584
Dan Williams5076a1a2011-06-27 14:57:03 -0700585 ireq->protocol = SCIC_STP_PROTOCOL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700586
Dan Williamsf1f52e72011-05-10 02:28:45 -0700587 copy = (task->data_dir == DMA_NONE) ? false : true;
Dan Williams6f231dd2011-07-02 22:56:22 -0700588
Dan Williams89a73012011-06-30 19:14:33 -0700589 status = sci_io_request_construct_sata(ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700590 task->total_xfer_len,
591 task->data_dir,
592 copy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700593
Dan Williamsf1f52e72011-05-10 02:28:45 -0700594 if (status == SCI_SUCCESS)
Dan Williams5076a1a2011-06-27 14:57:03 -0700595 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williams6f231dd2011-07-02 22:56:22 -0700596
Dan Williamsf1f52e72011-05-10 02:28:45 -0700597 return status;
Dan Williams6f231dd2011-07-02 22:56:22 -0700598}
599
Dan Williams89a73012011-06-30 19:14:33 -0700600enum sci_status sci_task_request_construct_sata(struct isci_request *ireq)
Dan Williams6f231dd2011-07-02 22:56:22 -0700601{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700602 enum sci_status status = SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700603
Dan Williamsf1f52e72011-05-10 02:28:45 -0700604 /* check for management protocols */
605 if (ireq->ttype == tmf_task) {
606 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700607
Dan Williamsf1f52e72011-05-10 02:28:45 -0700608 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
609 tmf->tmf_code == isci_tmf_sata_srst_low) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700610 scu_stp_raw_request_construct_task_context(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700611 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700612 dev_err(&ireq->owning_controller->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700613 "%s: Request 0x%p received un-handled SAT "
614 "Protocol 0x%x.\n",
Dan Williams5076a1a2011-06-27 14:57:03 -0700615 __func__, ireq, tmf->tmf_code);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700616
617 return SCI_FAILURE;
618 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700619 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700620
Dan Williams5dec6f42011-05-10 02:28:49 -0700621 if (status != SCI_SUCCESS)
622 return status;
Dan Williams5076a1a2011-06-27 14:57:03 -0700623 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700624
625 return status;
Dan Williams6f231dd2011-07-02 22:56:22 -0700626}
627
628/**
Dan Williamsf1f52e72011-05-10 02:28:45 -0700629 * sci_req_tx_bytes - bytes transferred when reply underruns request
630 * @sci_req: request that was terminated early
Dan Williams6f231dd2011-07-02 22:56:22 -0700631 */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700632#define SCU_TASK_CONTEXT_SRAM 0x200000
Dan Williams5076a1a2011-06-27 14:57:03 -0700633static u32 sci_req_tx_bytes(struct isci_request *ireq)
Dan Williams6f231dd2011-07-02 22:56:22 -0700634{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700635 struct isci_host *ihost = ireq->owning_controller;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700636 u32 ret_val = 0;
Dan Williams6f231dd2011-07-02 22:56:22 -0700637
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700638 if (readl(&ihost->smu_registers->address_modifier) == 0) {
639 void __iomem *scu_reg_base = ihost->scu_registers;
Dan Williams6f231dd2011-07-02 22:56:22 -0700640
Dan Williamsf1f52e72011-05-10 02:28:45 -0700641 /* get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
642 * BAR1 is the scu_registers
643 * 0x20002C = 0x200000 + 0x2c
644 * = start of task context SRAM + offset of (type.ssp.data_offset)
Dan Williams89a73012011-06-30 19:14:33 -0700645 * TCi is the io_tag of struct sci_request
Dan Williams67ea8382011-05-08 11:47:15 -0700646 */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700647 ret_val = readl(scu_reg_base +
648 (SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
Dan Williams5076a1a2011-06-27 14:57:03 -0700649 ((sizeof(struct scu_task_context)) * ISCI_TAG_TCI(ireq->io_tag)));
Dan Williams67ea8382011-05-08 11:47:15 -0700650 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700651
Dan Williamsf1f52e72011-05-10 02:28:45 -0700652 return ret_val;
Dan Williams6f231dd2011-07-02 22:56:22 -0700653}
654
Dan Williams89a73012011-06-30 19:14:33 -0700655enum sci_status sci_request_start(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700656{
Piotr Sawickif4636a72011-05-10 23:50:32 +0000657 enum sci_base_request_states state;
Dan Williams5076a1a2011-06-27 14:57:03 -0700658 struct scu_task_context *tc = ireq->tc;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700659 struct isci_host *ihost = ireq->owning_controller;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000660
Dan Williams5076a1a2011-06-27 14:57:03 -0700661 state = ireq->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000662 if (state != SCI_REQ_CONSTRUCTED) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700663 dev_warn(&ihost->pdev->dev,
Piotr Sawickif4636a72011-05-10 23:50:32 +0000664 "%s: SCIC IO Request requested to start while in wrong "
665 "state %d\n", __func__, state);
666 return SCI_FAILURE_INVALID_STATE;
667 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700668
Dan Williams5076a1a2011-06-27 14:57:03 -0700669 tc->task_index = ISCI_TAG_TCI(ireq->io_tag);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700670
Dan Williams312e0c22011-06-28 13:47:09 -0700671 switch (tc->protocol_type) {
672 case SCU_TASK_CONTEXT_PROTOCOL_SMP:
673 case SCU_TASK_CONTEXT_PROTOCOL_SSP:
674 /* SSP/SMP Frame */
Dan Williams5076a1a2011-06-27 14:57:03 -0700675 tc->type.ssp.tag = ireq->io_tag;
Dan Williams312e0c22011-06-28 13:47:09 -0700676 tc->type.ssp.target_port_transfer_tag = 0xFFFF;
677 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000678
Dan Williams312e0c22011-06-28 13:47:09 -0700679 case SCU_TASK_CONTEXT_PROTOCOL_STP:
680 /* STP/SATA Frame
Dan Williams5076a1a2011-06-27 14:57:03 -0700681 * tc->type.stp.ncq_tag = ireq->ncq_tag;
Dan Williams312e0c22011-06-28 13:47:09 -0700682 */
683 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000684
Dan Williams312e0c22011-06-28 13:47:09 -0700685 case SCU_TASK_CONTEXT_PROTOCOL_NONE:
686 /* / @todo When do we set no protocol type? */
687 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000688
Dan Williams312e0c22011-06-28 13:47:09 -0700689 default:
690 /* This should never happen since we build the IO
691 * requests */
692 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000693 }
694
Dan Williams312e0c22011-06-28 13:47:09 -0700695 /* Add to the post_context the io tag value */
Dan Williams5076a1a2011-06-27 14:57:03 -0700696 ireq->post_context |= ISCI_TAG_TCI(ireq->io_tag);
Dan Williams312e0c22011-06-28 13:47:09 -0700697
698 /* Everything is good go ahead and change state */
Dan Williams5076a1a2011-06-27 14:57:03 -0700699 sci_change_state(&ireq->sm, SCI_REQ_STARTED);
Dan Williams312e0c22011-06-28 13:47:09 -0700700
701 return SCI_SUCCESS;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700702}
703
704enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -0700705sci_io_request_terminate(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700706{
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700707 enum sci_base_request_states state;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700708
Dan Williams5076a1a2011-06-27 14:57:03 -0700709 state = ireq->sm.current_state_id;
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700710
711 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000712 case SCI_REQ_CONSTRUCTED:
Dan Williams34a99152011-07-01 02:25:15 -0700713 ireq->scu_status = SCU_TASK_DONE_TASK_ABORT;
714 ireq->sci_status = SCI_FAILURE_IO_TERMINATED;
Dan Williams5076a1a2011-06-27 14:57:03 -0700715 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700716 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000717 case SCI_REQ_STARTED:
718 case SCI_REQ_TASK_WAIT_TC_COMP:
719 case SCI_REQ_SMP_WAIT_RESP:
720 case SCI_REQ_SMP_WAIT_TC_COMP:
721 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
722 case SCI_REQ_STP_UDMA_WAIT_D2H:
723 case SCI_REQ_STP_NON_DATA_WAIT_H2D:
724 case SCI_REQ_STP_NON_DATA_WAIT_D2H:
725 case SCI_REQ_STP_PIO_WAIT_H2D:
726 case SCI_REQ_STP_PIO_WAIT_FRAME:
727 case SCI_REQ_STP_PIO_DATA_IN:
728 case SCI_REQ_STP_PIO_DATA_OUT:
729 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
730 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
731 case SCI_REQ_STP_SOFT_RESET_WAIT_D2H:
Dan Williams5076a1a2011-06-27 14:57:03 -0700732 sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700733 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000734 case SCI_REQ_TASK_WAIT_TC_RESP:
Dan Williams5076a1a2011-06-27 14:57:03 -0700735 sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
736 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700737 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000738 case SCI_REQ_ABORTING:
Dan Williams5076a1a2011-06-27 14:57:03 -0700739 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700740 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000741 case SCI_REQ_COMPLETED:
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700742 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700743 dev_warn(&ireq->owning_controller->pdev->dev,
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700744 "%s: SCIC IO Request requested to abort while in wrong "
745 "state %d\n",
746 __func__,
Dan Williams5076a1a2011-06-27 14:57:03 -0700747 ireq->sm.current_state_id);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700748 break;
749 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700750
751 return SCI_FAILURE_INVALID_STATE;
752}
753
Dan Williams89a73012011-06-30 19:14:33 -0700754enum sci_status sci_request_complete(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700755{
Dan Williams79e2b6b2011-05-11 08:29:56 -0700756 enum sci_base_request_states state;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700757 struct isci_host *ihost = ireq->owning_controller;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700758
Dan Williams5076a1a2011-06-27 14:57:03 -0700759 state = ireq->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000760 if (WARN_ONCE(state != SCI_REQ_COMPLETED,
Dan Williams79e2b6b2011-05-11 08:29:56 -0700761 "isci: request completion from wrong state (%d)\n", state))
762 return SCI_FAILURE_INVALID_STATE;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700763
Dan Williams5076a1a2011-06-27 14:57:03 -0700764 if (ireq->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX)
Dan Williams89a73012011-06-30 19:14:33 -0700765 sci_controller_release_frame(ihost,
Dan Williams5076a1a2011-06-27 14:57:03 -0700766 ireq->saved_rx_frame_index);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700767
768 /* XXX can we just stop the machine and remove the 'final' state? */
Dan Williams5076a1a2011-06-27 14:57:03 -0700769 sci_change_state(&ireq->sm, SCI_REQ_FINAL);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700770 return SCI_SUCCESS;
771}
772
Dan Williams89a73012011-06-30 19:14:33 -0700773enum sci_status sci_io_request_event_handler(struct isci_request *ireq,
Dan Williams79e2b6b2011-05-11 08:29:56 -0700774 u32 event_code)
775{
776 enum sci_base_request_states state;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700777 struct isci_host *ihost = ireq->owning_controller;
Dan Williams79e2b6b2011-05-11 08:29:56 -0700778
Dan Williams5076a1a2011-06-27 14:57:03 -0700779 state = ireq->sm.current_state_id;
Dan Williams79e2b6b2011-05-11 08:29:56 -0700780
Edmund Nadolskie3013702011-06-02 00:10:43 +0000781 if (state != SCI_REQ_STP_PIO_DATA_IN) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700782 dev_warn(&ihost->pdev->dev, "%s: (%x) in wrong state %d\n",
Dan Williams79e2b6b2011-05-11 08:29:56 -0700783 __func__, event_code, state);
784
785 return SCI_FAILURE_INVALID_STATE;
786 }
787
788 switch (scu_get_event_specifier(event_code)) {
789 case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
790 /* We are waiting for data and the SCU has R_ERR the data frame.
791 * Go back to waiting for the D2H Register FIS
792 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700793 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700794 return SCI_SUCCESS;
795 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700796 dev_err(&ihost->pdev->dev,
Dan Williams79e2b6b2011-05-11 08:29:56 -0700797 "%s: pio request unexpected event %#x\n",
798 __func__, event_code);
799
800 /* TODO Should we fail the PIO request when we get an
801 * unexpected event?
802 */
803 return SCI_FAILURE;
804 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700805}
806
Dan Williamsf1f52e72011-05-10 02:28:45 -0700807/*
808 * This function copies response data for requests returning response data
809 * instead of sense data.
810 * @sci_req: This parameter specifies the request object for which to copy
811 * the response data.
812 */
Dan Williams89a73012011-06-30 19:14:33 -0700813static void sci_io_request_copy_response(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700814{
815 void *resp_buf;
816 u32 len;
817 struct ssp_response_iu *ssp_response;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700818 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
819
Dan Williams5076a1a2011-06-27 14:57:03 -0700820 ssp_response = &ireq->ssp.rsp;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700821
822 resp_buf = &isci_tmf->resp.resp_iu;
823
824 len = min_t(u32,
825 SSP_RESP_IU_MAX_SIZE,
826 be32_to_cpu(ssp_response->response_data_len));
827
828 memcpy(resp_buf, ssp_response->resp_data, len);
829}
830
Edmund Nadolskie3013702011-06-02 00:10:43 +0000831static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -0700832request_started_state_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000833 u32 completion_code)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700834{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700835 struct ssp_response_iu *resp_iu;
Dan Williamsa7e255a2011-05-11 08:27:47 -0700836 u8 datapres;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700837
Dan Williamsa7e255a2011-05-11 08:27:47 -0700838 /* TODO: Any SDMA return code of other than 0 is bad decode 0x003C0000
839 * to determine SDMA status
Dan Williamsf1f52e72011-05-10 02:28:45 -0700840 */
841 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
842 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -0700843 ireq->scu_status = SCU_TASK_DONE_GOOD;
844 ireq->sci_status = SCI_SUCCESS;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700845 break;
Dan Williamsa7e255a2011-05-11 08:27:47 -0700846 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP): {
847 /* There are times when the SCU hardware will return an early
Dan Williamsf1f52e72011-05-10 02:28:45 -0700848 * response because the io request specified more data than is
849 * returned by the target device (mode pages, inquiry data,
850 * etc.). We must check the response stats to see if this is
851 * truly a failed request or a good request that just got
852 * completed early.
853 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700854 struct ssp_response_iu *resp = &ireq->ssp.rsp;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700855 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
856
Dan Williams5076a1a2011-06-27 14:57:03 -0700857 sci_swab32_cpy(&ireq->ssp.rsp,
858 &ireq->ssp.rsp,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700859 word_cnt);
860
861 if (resp->status == 0) {
Dan Williams34a99152011-07-01 02:25:15 -0700862 ireq->scu_status = SCU_TASK_DONE_GOOD;
863 ireq->sci_status = SCI_SUCCESS_IO_DONE_EARLY;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700864 } else {
Dan Williams34a99152011-07-01 02:25:15 -0700865 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
866 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700867 }
Dan Williamsa7e255a2011-05-11 08:27:47 -0700868 break;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700869 }
Dan Williamsa7e255a2011-05-11 08:27:47 -0700870 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE): {
Dan Williamsf1f52e72011-05-10 02:28:45 -0700871 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
872
Dan Williams5076a1a2011-06-27 14:57:03 -0700873 sci_swab32_cpy(&ireq->ssp.rsp,
874 &ireq->ssp.rsp,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700875 word_cnt);
876
Dan Williams34a99152011-07-01 02:25:15 -0700877 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
878 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700879 break;
880 }
881
882 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -0700883 /* TODO With TASK_DONE_RESP_LEN_ERR is the response frame
Dan Williamsf1f52e72011-05-10 02:28:45 -0700884 * guaranteed to be received before this completion status is
885 * posted?
886 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700887 resp_iu = &ireq->ssp.rsp;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700888 datapres = resp_iu->datapres;
889
Dan Williamsa7e255a2011-05-11 08:27:47 -0700890 if (datapres == 1 || datapres == 2) {
Dan Williams34a99152011-07-01 02:25:15 -0700891 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
892 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
893 } else {
894 ireq->scu_status = SCU_TASK_DONE_GOOD;
895 ireq->sci_status = SCI_SUCCESS;
896 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700897 break;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700898 /* only stp device gets suspended. */
899 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
900 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
901 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
902 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
903 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
904 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
905 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
906 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
907 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
908 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
909 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
Dan Williams5076a1a2011-06-27 14:57:03 -0700910 if (ireq->protocol == SCIC_STP_PROTOCOL) {
Dan Williams34a99152011-07-01 02:25:15 -0700911 ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
912 SCU_COMPLETION_TL_STATUS_SHIFT;
913 ireq->sci_status = SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700914 } else {
Dan Williams34a99152011-07-01 02:25:15 -0700915 ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
916 SCU_COMPLETION_TL_STATUS_SHIFT;
917 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700918 }
919 break;
920
921 /* both stp/ssp device gets suspended */
922 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
923 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
924 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
925 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
926 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
927 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
928 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
929 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
930 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
931 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
Dan Williams34a99152011-07-01 02:25:15 -0700932 ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
933 SCU_COMPLETION_TL_STATUS_SHIFT;
934 ireq->sci_status = SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700935 break;
936
937 /* neither ssp nor stp gets suspended. */
938 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
939 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
940 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
941 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
942 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
943 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
944 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
945 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
946 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
947 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
948 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
949 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
950 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
951 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
952 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
953 default:
Dan Williams34a99152011-07-01 02:25:15 -0700954 ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
955 SCU_COMPLETION_TL_STATUS_SHIFT;
956 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700957 break;
958 }
959
960 /*
961 * TODO: This is probably wrong for ACK/NAK timeout conditions
962 */
963
964 /* In all cases we will treat this as the completion of the IO req. */
Dan Williams5076a1a2011-06-27 14:57:03 -0700965 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700966 return SCI_SUCCESS;
967}
968
Edmund Nadolskie3013702011-06-02 00:10:43 +0000969static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -0700970request_aborting_state_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000971 u32 completion_code)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700972{
973 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
974 case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
975 case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
Dan Williams34a99152011-07-01 02:25:15 -0700976 ireq->scu_status = SCU_TASK_DONE_TASK_ABORT;
977 ireq->sci_status = SCI_FAILURE_IO_TERMINATED;
Dan Williams5076a1a2011-06-27 14:57:03 -0700978 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700979 break;
980
981 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -0700982 /* Unless we get some strange error wait for the task abort to complete
983 * TODO: Should there be a state change for this completion?
984 */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700985 break;
986 }
987
988 return SCI_SUCCESS;
989}
990
Dan Williams5076a1a2011-06-27 14:57:03 -0700991static enum sci_status ssp_task_request_await_tc_event(struct isci_request *ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -0700992 u32 completion_code)
Dan Williamsf1393032011-05-10 02:28:47 -0700993{
994 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
995 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -0700996 ireq->scu_status = SCU_TASK_DONE_GOOD;
997 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -0700998 sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
Dan Williamsf1393032011-05-10 02:28:47 -0700999 break;
Dan Williamsf1393032011-05-10 02:28:47 -07001000 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001001 /* Currently, the decision is to simply allow the task request
1002 * to timeout if the task IU wasn't received successfully.
1003 * There is a potential for receiving multiple task responses if
1004 * we decide to send the task IU again.
1005 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001006 dev_warn(&ireq->owning_controller->pdev->dev,
Dan Williamsf1393032011-05-10 02:28:47 -07001007 "%s: TaskRequest:0x%p CompletionCode:%x - "
Dan Williams5076a1a2011-06-27 14:57:03 -07001008 "ACK/NAK timeout\n", __func__, ireq,
Dan Williamsf1393032011-05-10 02:28:47 -07001009 completion_code);
1010
Dan Williams5076a1a2011-06-27 14:57:03 -07001011 sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
Dan Williamsf1393032011-05-10 02:28:47 -07001012 break;
Dan Williamsf1393032011-05-10 02:28:47 -07001013 default:
Edmund Nadolskie3013702011-06-02 00:10:43 +00001014 /*
1015 * All other completion status cause the IO to be complete.
1016 * If a NAK was received, then it is up to the user to retry
1017 * the request.
Dan Williamsa7e255a2011-05-11 08:27:47 -07001018 */
Dan Williams34a99152011-07-01 02:25:15 -07001019 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1020 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001021 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf1393032011-05-10 02:28:47 -07001022 break;
1023 }
1024
1025 return SCI_SUCCESS;
1026}
1027
Edmund Nadolskie3013702011-06-02 00:10:43 +00001028static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001029smp_request_await_response_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001030 u32 completion_code)
Dan Williamsc72086e2011-05-10 02:28:48 -07001031{
1032 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1033 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001034 /* In the AWAIT RESPONSE state, any TC completion is
1035 * unexpected. but if the TC has success status, we
1036 * complete the IO anyway.
1037 */
Dan Williams34a99152011-07-01 02:25:15 -07001038 ireq->scu_status = SCU_TASK_DONE_GOOD;
1039 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001040 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001041 break;
Dan Williamsc72086e2011-05-10 02:28:48 -07001042 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1043 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1044 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1045 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001046 /* These status has been seen in a specific LSI
1047 * expander, which sometimes is not able to send smp
1048 * response within 2 ms. This causes our hardware break
1049 * the connection and set TC completion with one of
1050 * these SMP_XXX_XX_ERR status. For these type of error,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001051 * we ask ihost user to retry the request.
Dan Williamsa7e255a2011-05-11 08:27:47 -07001052 */
Dan Williams34a99152011-07-01 02:25:15 -07001053 ireq->scu_status = SCU_TASK_DONE_SMP_RESP_TO_ERR;
1054 ireq->sci_status = SCI_FAILURE_RETRY_REQUIRED;
Dan Williams5076a1a2011-06-27 14:57:03 -07001055 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001056 break;
Dan Williamsc72086e2011-05-10 02:28:48 -07001057 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001058 /* All other completion status cause the IO to be complete. If a NAK
1059 * was received, then it is up to the user to retry the request
1060 */
Dan Williams34a99152011-07-01 02:25:15 -07001061 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1062 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
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
1067 return SCI_SUCCESS;
1068}
1069
Edmund Nadolskie3013702011-06-02 00:10:43 +00001070static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001071smp_request_await_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001072 u32 completion_code)
Dan Williamsc72086e2011-05-10 02:28:48 -07001073{
1074 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1075 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07001076 ireq->scu_status = SCU_TASK_DONE_GOOD;
1077 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001078 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001079 break;
Dan Williamsc72086e2011-05-10 02:28:48 -07001080 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001081 /* All other completion status cause the IO to be
1082 * complete. If a NAK was received, then it is up to
1083 * the user to retry the request.
1084 */
Dan Williams34a99152011-07-01 02:25:15 -07001085 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1086 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001087 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001088 break;
1089 }
1090
1091 return SCI_SUCCESS;
1092}
1093
Dan Williamsba7cb222011-06-27 11:56:41 -07001094static struct scu_sgl_element *pio_sgl_next(struct isci_stp_request *stp_req)
Dan Williams5dec6f42011-05-10 02:28:49 -07001095{
Dan Williams312e0c22011-06-28 13:47:09 -07001096 struct scu_sgl_element *sgl;
1097 struct scu_sgl_element_pair *sgl_pair;
Dan Williams5076a1a2011-06-27 14:57:03 -07001098 struct isci_request *ireq = to_ireq(stp_req);
Dan Williamsba7cb222011-06-27 11:56:41 -07001099 struct isci_stp_pio_sgl *pio_sgl = &stp_req->sgl;
Dan Williams5dec6f42011-05-10 02:28:49 -07001100
Dan Williams5076a1a2011-06-27 14:57:03 -07001101 sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
Dan Williams312e0c22011-06-28 13:47:09 -07001102 if (!sgl_pair)
1103 sgl = NULL;
Dan Williamsba7cb222011-06-27 11:56:41 -07001104 else if (pio_sgl->set == SCU_SGL_ELEMENT_PAIR_A) {
Dan Williams312e0c22011-06-28 13:47:09 -07001105 if (sgl_pair->B.address_lower == 0 &&
1106 sgl_pair->B.address_upper == 0) {
1107 sgl = NULL;
Dan Williams5dec6f42011-05-10 02:28:49 -07001108 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001109 pio_sgl->set = SCU_SGL_ELEMENT_PAIR_B;
Dan Williams312e0c22011-06-28 13:47:09 -07001110 sgl = &sgl_pair->B;
Dan Williams5dec6f42011-05-10 02:28:49 -07001111 }
1112 } else {
Dan Williams312e0c22011-06-28 13:47:09 -07001113 if (sgl_pair->next_pair_lower == 0 &&
1114 sgl_pair->next_pair_upper == 0) {
1115 sgl = NULL;
Dan Williams5dec6f42011-05-10 02:28:49 -07001116 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001117 pio_sgl->index++;
1118 pio_sgl->set = SCU_SGL_ELEMENT_PAIR_A;
Dan Williams5076a1a2011-06-27 14:57:03 -07001119 sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
Dan Williams312e0c22011-06-28 13:47:09 -07001120 sgl = &sgl_pair->A;
Dan Williams5dec6f42011-05-10 02:28:49 -07001121 }
1122 }
1123
Dan Williams312e0c22011-06-28 13:47:09 -07001124 return sgl;
Dan Williams5dec6f42011-05-10 02:28:49 -07001125}
1126
Edmund Nadolskie3013702011-06-02 00:10:43 +00001127static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001128stp_request_non_data_await_h2d_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001129 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001130{
1131 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1132 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07001133 ireq->scu_status = SCU_TASK_DONE_GOOD;
1134 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001135 sci_change_state(&ireq->sm, SCI_REQ_STP_NON_DATA_WAIT_D2H);
Dan Williams5dec6f42011-05-10 02:28:49 -07001136 break;
1137
1138 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001139 /* All other completion status cause the IO to be
1140 * complete. If a NAK was received, then it is up to
1141 * the user to retry the request.
1142 */
Dan Williams34a99152011-07-01 02:25:15 -07001143 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1144 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001145 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001146 break;
1147 }
1148
1149 return SCI_SUCCESS;
1150}
1151
Dan Williams5dec6f42011-05-10 02:28:49 -07001152#define SCU_MAX_FRAME_BUFFER_SIZE 0x400 /* 1K is the maximum SCU frame data payload */
1153
1154/* transmit DATA_FIS from (current sgl + offset) for input
1155 * parameter length. current sgl and offset is alreay stored in the IO request
1156 */
Dan Williams89a73012011-06-30 19:14:33 -07001157static enum sci_status sci_stp_request_pio_data_out_trasmit_data_frame(
Dan Williams5076a1a2011-06-27 14:57:03 -07001158 struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001159 u32 length)
1160{
Dan Williams5076a1a2011-06-27 14:57:03 -07001161 struct isci_stp_request *stp_req = &ireq->stp.req;
1162 struct scu_task_context *task_context = ireq->tc;
Dan Williams312e0c22011-06-28 13:47:09 -07001163 struct scu_sgl_element_pair *sgl_pair;
Dan Williams5dec6f42011-05-10 02:28:49 -07001164 struct scu_sgl_element *current_sgl;
1165
1166 /* Recycle the TC and reconstruct it for sending out DATA FIS containing
1167 * for the data from current_sgl+offset for the input length
1168 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001169 sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
Dan Williamsba7cb222011-06-27 11:56:41 -07001170 if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A)
Dan Williams312e0c22011-06-28 13:47:09 -07001171 current_sgl = &sgl_pair->A;
Dan Williams5dec6f42011-05-10 02:28:49 -07001172 else
Dan Williams312e0c22011-06-28 13:47:09 -07001173 current_sgl = &sgl_pair->B;
Dan Williams5dec6f42011-05-10 02:28:49 -07001174
1175 /* update the TC */
1176 task_context->command_iu_upper = current_sgl->address_upper;
1177 task_context->command_iu_lower = current_sgl->address_lower;
1178 task_context->transfer_length_bytes = length;
1179 task_context->type.stp.fis_type = FIS_DATA;
1180
1181 /* send the new TC out. */
Dan Williams89a73012011-06-30 19:14:33 -07001182 return sci_controller_continue_io(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -07001183}
1184
Dan Williams89a73012011-06-30 19:14:33 -07001185static enum sci_status sci_stp_request_pio_data_out_transmit_data(struct isci_request *ireq)
Dan Williams5dec6f42011-05-10 02:28:49 -07001186{
Dan Williams5076a1a2011-06-27 14:57:03 -07001187 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williams312e0c22011-06-28 13:47:09 -07001188 struct scu_sgl_element_pair *sgl_pair;
Dan Williamsba7cb222011-06-27 11:56:41 -07001189 struct scu_sgl_element *sgl;
1190 enum sci_status status;
1191 u32 offset;
1192 u32 len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001193
Dan Williamsba7cb222011-06-27 11:56:41 -07001194 offset = stp_req->sgl.offset;
Dan Williams5076a1a2011-06-27 14:57:03 -07001195 sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
Dan Williams312e0c22011-06-28 13:47:09 -07001196 if (WARN_ONCE(!sgl_pair, "%s: null sgl element", __func__))
1197 return SCI_FAILURE;
Dan Williams5dec6f42011-05-10 02:28:49 -07001198
Dan Williamsba7cb222011-06-27 11:56:41 -07001199 if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A) {
1200 sgl = &sgl_pair->A;
1201 len = sgl_pair->A.length - offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001202 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001203 sgl = &sgl_pair->B;
1204 len = sgl_pair->B.length - offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001205 }
1206
Dan Williamsba7cb222011-06-27 11:56:41 -07001207 if (stp_req->pio_len == 0)
1208 return SCI_SUCCESS;
Dan Williams5dec6f42011-05-10 02:28:49 -07001209
Dan Williamsba7cb222011-06-27 11:56:41 -07001210 if (stp_req->pio_len >= len) {
Dan Williams89a73012011-06-30 19:14:33 -07001211 status = sci_stp_request_pio_data_out_trasmit_data_frame(ireq, len);
Dan Williamsba7cb222011-06-27 11:56:41 -07001212 if (status != SCI_SUCCESS)
1213 return status;
1214 stp_req->pio_len -= len;
Dan Williams5dec6f42011-05-10 02:28:49 -07001215
Dan Williamsba7cb222011-06-27 11:56:41 -07001216 /* update the current sgl, offset and save for future */
1217 sgl = pio_sgl_next(stp_req);
1218 offset = 0;
1219 } else if (stp_req->pio_len < len) {
Dan Williams89a73012011-06-30 19:14:33 -07001220 sci_stp_request_pio_data_out_trasmit_data_frame(ireq, stp_req->pio_len);
Dan Williamsba7cb222011-06-27 11:56:41 -07001221
1222 /* Sgl offset will be adjusted and saved for future */
1223 offset += stp_req->pio_len;
1224 sgl->address_lower += stp_req->pio_len;
1225 stp_req->pio_len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001226 }
1227
Dan Williamsba7cb222011-06-27 11:56:41 -07001228 stp_req->sgl.offset = offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001229
1230 return status;
1231}
1232
1233/**
1234 *
1235 * @stp_request: The request that is used for the SGL processing.
1236 * @data_buffer: The buffer of data to be copied.
1237 * @length: The length of the data transfer.
1238 *
1239 * Copy the data from the buffer for the length specified to the IO reqeust SGL
1240 * specified data region. enum sci_status
1241 */
1242static enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -07001243sci_stp_request_pio_data_in_copy_data_buffer(struct isci_stp_request *stp_req,
Dan Williams5dec6f42011-05-10 02:28:49 -07001244 u8 *data_buf, u32 len)
1245{
Dan Williams5dec6f42011-05-10 02:28:49 -07001246 struct isci_request *ireq;
1247 u8 *src_addr;
1248 int copy_len;
1249 struct sas_task *task;
1250 struct scatterlist *sg;
1251 void *kaddr;
1252 int total_len = len;
1253
Dan Williams5076a1a2011-06-27 14:57:03 -07001254 ireq = to_ireq(stp_req);
Dan Williams5dec6f42011-05-10 02:28:49 -07001255 task = isci_request_access_task(ireq);
1256 src_addr = data_buf;
1257
1258 if (task->num_scatter > 0) {
1259 sg = task->scatter;
1260
1261 while (total_len > 0) {
1262 struct page *page = sg_page(sg);
1263
1264 copy_len = min_t(int, total_len, sg_dma_len(sg));
1265 kaddr = kmap_atomic(page, KM_IRQ0);
1266 memcpy(kaddr + sg->offset, src_addr, copy_len);
1267 kunmap_atomic(kaddr, KM_IRQ0);
1268 total_len -= copy_len;
1269 src_addr += copy_len;
1270 sg = sg_next(sg);
1271 }
1272 } else {
1273 BUG_ON(task->total_xfer_len < total_len);
1274 memcpy(task->scatter, src_addr, total_len);
1275 }
1276
1277 return SCI_SUCCESS;
1278}
1279
1280/**
1281 *
1282 * @sci_req: The PIO DATA IN request that is to receive the data.
1283 * @data_buffer: The buffer to copy from.
1284 *
1285 * Copy the data buffer to the io request data region. enum sci_status
1286 */
Dan Williams89a73012011-06-30 19:14:33 -07001287static enum sci_status sci_stp_request_pio_data_in_copy_data(
Dan Williamsba7cb222011-06-27 11:56:41 -07001288 struct isci_stp_request *stp_req,
Dan Williams5dec6f42011-05-10 02:28:49 -07001289 u8 *data_buffer)
1290{
1291 enum sci_status status;
1292
1293 /*
1294 * If there is less than 1K remaining in the transfer request
1295 * copy just the data for the transfer */
Dan Williamsba7cb222011-06-27 11:56:41 -07001296 if (stp_req->pio_len < SCU_MAX_FRAME_BUFFER_SIZE) {
Dan Williams89a73012011-06-30 19:14:33 -07001297 status = sci_stp_request_pio_data_in_copy_data_buffer(
Dan Williamsba7cb222011-06-27 11:56:41 -07001298 stp_req, data_buffer, stp_req->pio_len);
Dan Williams5dec6f42011-05-10 02:28:49 -07001299
1300 if (status == SCI_SUCCESS)
Dan Williamsba7cb222011-06-27 11:56:41 -07001301 stp_req->pio_len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001302 } else {
1303 /* We are transfering the whole frame so copy */
Dan Williams89a73012011-06-30 19:14:33 -07001304 status = sci_stp_request_pio_data_in_copy_data_buffer(
Dan Williamsba7cb222011-06-27 11:56:41 -07001305 stp_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
Dan Williams5dec6f42011-05-10 02:28:49 -07001306
1307 if (status == SCI_SUCCESS)
Dan Williamsba7cb222011-06-27 11:56:41 -07001308 stp_req->pio_len -= SCU_MAX_FRAME_BUFFER_SIZE;
Dan Williams5dec6f42011-05-10 02:28:49 -07001309 }
1310
1311 return status;
1312}
1313
Edmund Nadolskie3013702011-06-02 00:10:43 +00001314static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001315stp_request_pio_await_h2d_completion_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001316 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001317{
1318 enum sci_status status = SCI_SUCCESS;
1319
1320 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1321 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07001322 ireq->scu_status = SCU_TASK_DONE_GOOD;
1323 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001324 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams5dec6f42011-05-10 02:28:49 -07001325 break;
1326
1327 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001328 /* All other completion status cause the IO to be
1329 * complete. If a NAK was received, then it is up to
1330 * the user to retry the request.
1331 */
Dan Williams34a99152011-07-01 02:25:15 -07001332 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1333 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001334 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001335 break;
1336 }
1337
1338 return status;
1339}
1340
Edmund Nadolskie3013702011-06-02 00:10:43 +00001341static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001342pio_data_out_tx_done_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001343 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001344{
1345 enum sci_status status = SCI_SUCCESS;
1346 bool all_frames_transferred = false;
Dan Williams5076a1a2011-06-27 14:57:03 -07001347 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williams5dec6f42011-05-10 02:28:49 -07001348
1349 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1350 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1351 /* Transmit data */
Dan Williamsba7cb222011-06-27 11:56:41 -07001352 if (stp_req->pio_len != 0) {
Dan Williams89a73012011-06-30 19:14:33 -07001353 status = sci_stp_request_pio_data_out_transmit_data(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -07001354 if (status == SCI_SUCCESS) {
Dan Williamsba7cb222011-06-27 11:56:41 -07001355 if (stp_req->pio_len == 0)
Dan Williams5dec6f42011-05-10 02:28:49 -07001356 all_frames_transferred = true;
1357 }
Dan Williamsba7cb222011-06-27 11:56:41 -07001358 } else if (stp_req->pio_len == 0) {
Dan Williams5dec6f42011-05-10 02:28:49 -07001359 /*
1360 * this will happen if the all data is written at the
1361 * first time after the pio setup fis is received
1362 */
1363 all_frames_transferred = true;
1364 }
1365
1366 /* all data transferred. */
1367 if (all_frames_transferred) {
1368 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001369 * Change the state to SCI_REQ_STP_PIO_DATA_IN
Dan Williams5dec6f42011-05-10 02:28:49 -07001370 * and wait for PIO_SETUP fis / or D2H REg fis. */
Dan Williams5076a1a2011-06-27 14:57:03 -07001371 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams5dec6f42011-05-10 02:28:49 -07001372 }
1373 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001374
Dan Williams5dec6f42011-05-10 02:28:49 -07001375 default:
1376 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001377 * All other completion status cause the IO to be complete.
1378 * If a NAK was received, then it is up to the user to retry
1379 * the request.
1380 */
Dan Williams34a99152011-07-01 02:25:15 -07001381 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1382 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001383 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001384 break;
1385 }
1386
1387 return status;
1388}
1389
Dan Williams89a73012011-06-30 19:14:33 -07001390static enum sci_status sci_stp_request_udma_general_frame_handler(struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001391 u32 frame_index)
1392{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001393 struct isci_host *ihost = ireq->owning_controller;
Dan Williams5dec6f42011-05-10 02:28:49 -07001394 struct dev_to_host_fis *frame_header;
1395 enum sci_status status;
1396 u32 *frame_buffer;
1397
Dan Williams89a73012011-06-30 19:14:33 -07001398 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williams5dec6f42011-05-10 02:28:49 -07001399 frame_index,
1400 (void **)&frame_header);
1401
1402 if ((status == SCI_SUCCESS) &&
1403 (frame_header->fis_type == FIS_REGD2H)) {
Dan Williams89a73012011-06-30 19:14:33 -07001404 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williams5dec6f42011-05-10 02:28:49 -07001405 frame_index,
1406 (void **)&frame_buffer);
1407
Dan Williams89a73012011-06-30 19:14:33 -07001408 sci_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williams5dec6f42011-05-10 02:28:49 -07001409 frame_header,
1410 frame_buffer);
1411 }
1412
Dan Williams89a73012011-06-30 19:14:33 -07001413 sci_controller_release_frame(ihost, frame_index);
Dan Williams5dec6f42011-05-10 02:28:49 -07001414
1415 return status;
1416}
1417
Edmund Nadolskie3013702011-06-02 00:10:43 +00001418enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -07001419sci_io_request_frame_handler(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001420 u32 frame_index)
Dan Williamsd1c637c32011-05-11 08:27:47 -07001421{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001422 struct isci_host *ihost = ireq->owning_controller;
Dan Williams5076a1a2011-06-27 14:57:03 -07001423 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001424 enum sci_base_request_states state;
1425 enum sci_status status;
1426 ssize_t word_cnt;
1427
Dan Williams5076a1a2011-06-27 14:57:03 -07001428 state = ireq->sm.current_state_id;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001429 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001430 case SCI_REQ_STARTED: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001431 struct ssp_frame_hdr ssp_hdr;
1432 void *frame_header;
1433
Dan Williams89a73012011-06-30 19:14:33 -07001434 sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001435 frame_index,
1436 &frame_header);
1437
1438 word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
1439 sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);
1440
1441 if (ssp_hdr.frame_type == SSP_RESPONSE) {
1442 struct ssp_response_iu *resp_iu;
1443 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1444
Dan Williams89a73012011-06-30 19:14:33 -07001445 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001446 frame_index,
1447 (void **)&resp_iu);
1448
Dan Williams5076a1a2011-06-27 14:57:03 -07001449 sci_swab32_cpy(&ireq->ssp.rsp, resp_iu, word_cnt);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001450
Dan Williams5076a1a2011-06-27 14:57:03 -07001451 resp_iu = &ireq->ssp.rsp;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001452
1453 if (resp_iu->datapres == 0x01 ||
1454 resp_iu->datapres == 0x02) {
Dan Williams34a99152011-07-01 02:25:15 -07001455 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1456 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1457 } else {
1458 ireq->scu_status = SCU_TASK_DONE_GOOD;
1459 ireq->sci_status = SCI_SUCCESS;
1460 }
Dan Williamsd1c637c32011-05-11 08:27:47 -07001461 } else {
1462 /* not a response frame, why did it get forwarded? */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001463 dev_err(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001464 "%s: SCIC IO Request 0x%p received unexpected "
Dan Williams5076a1a2011-06-27 14:57:03 -07001465 "frame %d type 0x%02x\n", __func__, ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001466 frame_index, ssp_hdr.frame_type);
1467 }
1468
1469 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001470 * In any case we are done with this frame buffer return it to
1471 * the controller
Dan Williamsd1c637c32011-05-11 08:27:47 -07001472 */
Dan Williams89a73012011-06-30 19:14:33 -07001473 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001474
1475 return SCI_SUCCESS;
1476 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001477
1478 case SCI_REQ_TASK_WAIT_TC_RESP:
Dan Williams89a73012011-06-30 19:14:33 -07001479 sci_io_request_copy_response(ireq);
Dan Williams5076a1a2011-06-27 14:57:03 -07001480 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams89a73012011-06-30 19:14:33 -07001481 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001482 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001483
1484 case SCI_REQ_SMP_WAIT_RESP: {
Dan Williams5076a1a2011-06-27 14:57:03 -07001485 struct smp_resp *rsp_hdr = &ireq->smp.rsp;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001486 void *frame_header;
1487
Dan Williams89a73012011-06-30 19:14:33 -07001488 sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001489 frame_index,
1490 &frame_header);
1491
1492 /* byte swap the header. */
1493 word_cnt = SMP_RESP_HDR_SZ / sizeof(u32);
1494 sci_swab32_cpy(rsp_hdr, frame_header, word_cnt);
1495
1496 if (rsp_hdr->frame_type == SMP_RESPONSE) {
1497 void *smp_resp;
1498
Dan Williams89a73012011-06-30 19:14:33 -07001499 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001500 frame_index,
1501 &smp_resp);
1502
Dan Williams5edc3342011-06-16 17:20:35 -07001503 word_cnt = (sizeof(struct smp_resp) - SMP_RESP_HDR_SZ) /
Dan Williamsd1c637c32011-05-11 08:27:47 -07001504 sizeof(u32);
1505
1506 sci_swab32_cpy(((u8 *) rsp_hdr) + SMP_RESP_HDR_SZ,
1507 smp_resp, word_cnt);
1508
Dan Williams34a99152011-07-01 02:25:15 -07001509 ireq->scu_status = SCU_TASK_DONE_GOOD;
1510 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001511 sci_change_state(&ireq->sm, SCI_REQ_SMP_WAIT_TC_COMP);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001512 } else {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001513 /*
1514 * This was not a response frame why did it get
1515 * forwarded?
1516 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001517 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001518 "%s: SCIC SMP Request 0x%p received unexpected "
1519 "frame %d type 0x%02x\n",
1520 __func__,
Dan Williams5076a1a2011-06-27 14:57:03 -07001521 ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001522 frame_index,
1523 rsp_hdr->frame_type);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001524
Dan Williams34a99152011-07-01 02:25:15 -07001525 ireq->scu_status = SCU_TASK_DONE_SMP_FRM_TYPE_ERR;
1526 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001527 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001528 }
1529
Dan Williams89a73012011-06-30 19:14:33 -07001530 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001531
1532 return SCI_SUCCESS;
1533 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001534
1535 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
Dan Williams89a73012011-06-30 19:14:33 -07001536 return sci_stp_request_udma_general_frame_handler(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001537 frame_index);
1538
1539 case SCI_REQ_STP_UDMA_WAIT_D2H:
Dan Williamsd1c637c32011-05-11 08:27:47 -07001540 /* Use the general frame handler to copy the resposne data */
Dan Williams34a99152011-07-01 02:25:15 -07001541 status = sci_stp_request_udma_general_frame_handler(ireq, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001542
1543 if (status != SCI_SUCCESS)
1544 return status;
1545
Dan Williams34a99152011-07-01 02:25:15 -07001546 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1547 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1548 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001549 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001550
1551 case SCI_REQ_STP_NON_DATA_WAIT_D2H: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001552 struct dev_to_host_fis *frame_header;
1553 u32 *frame_buffer;
1554
Dan Williams89a73012011-06-30 19:14:33 -07001555 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001556 frame_index,
1557 (void **)&frame_header);
1558
1559 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001560 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001561 "%s: SCIC IO Request 0x%p could not get frame "
1562 "header for frame index %d, status %x\n",
1563 __func__,
1564 stp_req,
1565 frame_index,
1566 status);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001567
1568 return status;
1569 }
1570
1571 switch (frame_header->fis_type) {
1572 case FIS_REGD2H:
Dan Williams89a73012011-06-30 19:14:33 -07001573 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001574 frame_index,
1575 (void **)&frame_buffer);
1576
Dan Williams89a73012011-06-30 19:14:33 -07001577 sci_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001578 frame_header,
1579 frame_buffer);
1580
1581 /* The command has completed with error */
Dan Williams34a99152011-07-01 02:25:15 -07001582 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1583 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001584 break;
1585
1586 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001587 dev_warn(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001588 "%s: IO Request:0x%p Frame Id:%d protocol "
1589 "violation occurred\n", __func__, stp_req,
1590 frame_index);
1591
Dan Williams34a99152011-07-01 02:25:15 -07001592 ireq->scu_status = SCU_TASK_DONE_UNEXP_FIS;
1593 ireq->sci_status = SCI_FAILURE_PROTOCOL_VIOLATION;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001594 break;
1595 }
1596
Dan Williams5076a1a2011-06-27 14:57:03 -07001597 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001598
1599 /* Frame has been decoded return it to the controller */
Dan Williams89a73012011-06-30 19:14:33 -07001600 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001601
1602 return status;
1603 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001604
1605 case SCI_REQ_STP_PIO_WAIT_FRAME: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001606 struct sas_task *task = isci_request_access_task(ireq);
1607 struct dev_to_host_fis *frame_header;
1608 u32 *frame_buffer;
1609
Dan Williams89a73012011-06-30 19:14:33 -07001610 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001611 frame_index,
1612 (void **)&frame_header);
1613
1614 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001615 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001616 "%s: SCIC IO Request 0x%p could not get frame "
1617 "header for frame index %d, status %x\n",
Dan Williamsd1c637c32011-05-11 08:27:47 -07001618 __func__, stp_req, frame_index, status);
1619 return status;
1620 }
1621
1622 switch (frame_header->fis_type) {
1623 case FIS_PIO_SETUP:
1624 /* Get from the frame buffer the PIO Setup Data */
Dan Williams89a73012011-06-30 19:14:33 -07001625 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001626 frame_index,
1627 (void **)&frame_buffer);
1628
Edmund Nadolskie3013702011-06-02 00:10:43 +00001629 /* Get the data from the PIO Setup The SCU Hardware
1630 * returns first word in the frame_header and the rest
1631 * of the data is in the frame buffer so we need to
1632 * back up one dword
Dan Williamsd1c637c32011-05-11 08:27:47 -07001633 */
1634
1635 /* transfer_count: first 16bits in the 4th dword */
Dan Williamsba7cb222011-06-27 11:56:41 -07001636 stp_req->pio_len = frame_buffer[3] & 0xffff;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001637
Dan Williamsba7cb222011-06-27 11:56:41 -07001638 /* status: 4th byte in the 3rd dword */
1639 stp_req->status = (frame_buffer[2] >> 24) & 0xff;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001640
Dan Williams89a73012011-06-30 19:14:33 -07001641 sci_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001642 frame_header,
1643 frame_buffer);
1644
Dan Williams5076a1a2011-06-27 14:57:03 -07001645 ireq->stp.rsp.status = stp_req->status;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001646
1647 /* The next state is dependent on whether the
1648 * request was PIO Data-in or Data out
1649 */
1650 if (task->data_dir == DMA_FROM_DEVICE) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001651 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_IN);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001652 } else if (task->data_dir == DMA_TO_DEVICE) {
1653 /* Transmit data */
Dan Williams89a73012011-06-30 19:14:33 -07001654 status = sci_stp_request_pio_data_out_transmit_data(ireq);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001655 if (status != SCI_SUCCESS)
1656 break;
Dan Williams5076a1a2011-06-27 14:57:03 -07001657 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_OUT);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001658 }
1659 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001660
Dan Williamsd1c637c32011-05-11 08:27:47 -07001661 case FIS_SETDEVBITS:
Dan Williams5076a1a2011-06-27 14:57:03 -07001662 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001663 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001664
Dan Williamsd1c637c32011-05-11 08:27:47 -07001665 case FIS_REGD2H:
1666 if (frame_header->status & ATA_BUSY) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001667 /*
1668 * Now why is the drive sending a D2H Register
1669 * FIS when it is still busy? Do nothing since
1670 * we are still in the right state.
Dan Williamsd1c637c32011-05-11 08:27:47 -07001671 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001672 dev_dbg(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001673 "%s: SCIC PIO Request 0x%p received "
1674 "D2H Register FIS with BSY status "
Edmund Nadolskie3013702011-06-02 00:10:43 +00001675 "0x%x\n",
1676 __func__,
1677 stp_req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001678 frame_header->status);
1679 break;
1680 }
1681
Dan Williams89a73012011-06-30 19:14:33 -07001682 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001683 frame_index,
1684 (void **)&frame_buffer);
1685
Dan Williams89a73012011-06-30 19:14:33 -07001686 sci_controller_copy_sata_response(&ireq->stp.req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001687 frame_header,
1688 frame_buffer);
1689
Dan Williams34a99152011-07-01 02:25:15 -07001690 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1691 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
Dan Williams5076a1a2011-06-27 14:57:03 -07001692 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001693 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001694
Dan Williamsd1c637c32011-05-11 08:27:47 -07001695 default:
1696 /* FIXME: what do we do here? */
1697 break;
1698 }
1699
1700 /* Frame is decoded return it to the controller */
Dan Williams89a73012011-06-30 19:14:33 -07001701 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001702
1703 return status;
1704 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001705
1706 case SCI_REQ_STP_PIO_DATA_IN: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001707 struct dev_to_host_fis *frame_header;
1708 struct sata_fis_data *frame_buffer;
1709
Dan Williams89a73012011-06-30 19:14:33 -07001710 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001711 frame_index,
1712 (void **)&frame_header);
1713
1714 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001715 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001716 "%s: SCIC IO Request 0x%p could not get frame "
1717 "header for frame index %d, status %x\n",
1718 __func__,
1719 stp_req,
1720 frame_index,
1721 status);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001722 return status;
1723 }
1724
1725 if (frame_header->fis_type != FIS_DATA) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001726 dev_err(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001727 "%s: SCIC PIO Request 0x%p received frame %d "
1728 "with fis type 0x%02x when expecting a data "
Edmund Nadolskie3013702011-06-02 00:10:43 +00001729 "fis.\n",
1730 __func__,
1731 stp_req,
1732 frame_index,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001733 frame_header->fis_type);
1734
Dan Williams34a99152011-07-01 02:25:15 -07001735 ireq->scu_status = SCU_TASK_DONE_GOOD;
1736 ireq->sci_status = SCI_FAILURE_IO_REQUIRES_SCSI_ABORT;
Dan Williams5076a1a2011-06-27 14:57:03 -07001737 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001738
1739 /* Frame is decoded return it to the controller */
Dan Williams89a73012011-06-30 19:14:33 -07001740 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001741 return status;
1742 }
1743
Dan Williamsba7cb222011-06-27 11:56:41 -07001744 if (stp_req->sgl.index < 0) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001745 ireq->saved_rx_frame_index = frame_index;
Dan Williamsba7cb222011-06-27 11:56:41 -07001746 stp_req->pio_len = 0;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001747 } else {
Dan Williams89a73012011-06-30 19:14:33 -07001748 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001749 frame_index,
1750 (void **)&frame_buffer);
1751
Dan Williams89a73012011-06-30 19:14:33 -07001752 status = sci_stp_request_pio_data_in_copy_data(stp_req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001753 (u8 *)frame_buffer);
1754
1755 /* Frame is decoded return it to the controller */
Dan Williams89a73012011-06-30 19:14:33 -07001756 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001757 }
1758
1759 /* Check for the end of the transfer, are there more
1760 * bytes remaining for this data transfer
1761 */
Dan Williamsba7cb222011-06-27 11:56:41 -07001762 if (status != SCI_SUCCESS || stp_req->pio_len != 0)
Dan Williamsd1c637c32011-05-11 08:27:47 -07001763 return status;
1764
Dan Williamsba7cb222011-06-27 11:56:41 -07001765 if ((stp_req->status & ATA_BUSY) == 0) {
Dan Williams34a99152011-07-01 02:25:15 -07001766 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1767 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
Dan Williams5076a1a2011-06-27 14:57:03 -07001768 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001769 } else {
Dan Williams5076a1a2011-06-27 14:57:03 -07001770 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001771 }
1772 return status;
1773 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001774
1775 case SCI_REQ_STP_SOFT_RESET_WAIT_D2H: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001776 struct dev_to_host_fis *frame_header;
1777 u32 *frame_buffer;
1778
Dan Williams89a73012011-06-30 19:14:33 -07001779 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001780 frame_index,
1781 (void **)&frame_header);
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 switch (frame_header->fis_type) {
1794 case FIS_REGD2H:
Dan Williams89a73012011-06-30 19:14:33 -07001795 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001796 frame_index,
1797 (void **)&frame_buffer);
1798
Dan Williams89a73012011-06-30 19:14:33 -07001799 sci_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001800 frame_header,
1801 frame_buffer);
1802
1803 /* The command has completed with error */
Dan Williams34a99152011-07-01 02:25:15 -07001804 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1805 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001806 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001807
Dan Williamsd1c637c32011-05-11 08:27:47 -07001808 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001809 dev_warn(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001810 "%s: IO Request:0x%p Frame Id:%d protocol "
Edmund Nadolskie3013702011-06-02 00:10:43 +00001811 "violation occurred\n",
1812 __func__,
1813 stp_req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001814 frame_index);
1815
Dan Williams34a99152011-07-01 02:25:15 -07001816 ireq->scu_status = SCU_TASK_DONE_UNEXP_FIS;
1817 ireq->sci_status = SCI_FAILURE_PROTOCOL_VIOLATION;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001818 break;
1819 }
1820
Dan Williams5076a1a2011-06-27 14:57:03 -07001821 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001822
1823 /* Frame has been decoded return it to the controller */
Dan Williams89a73012011-06-30 19:14:33 -07001824 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001825
1826 return status;
1827 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001828 case SCI_REQ_ABORTING:
1829 /*
1830 * TODO: Is it even possible to get an unsolicited frame in the
Dan Williamsd1c637c32011-05-11 08:27:47 -07001831 * aborting state?
1832 */
Dan Williams89a73012011-06-30 19:14:33 -07001833 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001834 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001835
Dan Williamsd1c637c32011-05-11 08:27:47 -07001836 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001837 dev_warn(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001838 "%s: SCIC IO Request given unexpected frame %x while "
1839 "in state %d\n",
1840 __func__,
1841 frame_index,
1842 state);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001843
Dan Williams89a73012011-06-30 19:14:33 -07001844 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001845 return SCI_FAILURE_INVALID_STATE;
1846 }
1847}
1848
Dan Williams5076a1a2011-06-27 14:57:03 -07001849static enum sci_status stp_request_udma_await_tc_event(struct isci_request *ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001850 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001851{
1852 enum sci_status status = SCI_SUCCESS;
1853
1854 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1855 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07001856 ireq->scu_status = SCU_TASK_DONE_GOOD;
1857 ireq->sci_status = SCI_SUCCESS;
1858 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001859 break;
1860 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
1861 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001862 /* We must check ther response buffer to see if the D2H
1863 * Register FIS was received before we got the TC
1864 * completion.
1865 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001866 if (ireq->stp.rsp.fis_type == FIS_REGD2H) {
Dan Williams89a73012011-06-30 19:14:33 -07001867 sci_remote_device_suspend(ireq->target_device,
Dan Williams5dec6f42011-05-10 02:28:49 -07001868 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
1869
Dan Williams34a99152011-07-01 02:25:15 -07001870 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1871 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1872 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001873 } else {
Dan Williamsa7e255a2011-05-11 08:27:47 -07001874 /* If we have an error completion status for the
1875 * TC then we can expect a D2H register FIS from
1876 * the device so we must change state to wait
1877 * for it
1878 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001879 sci_change_state(&ireq->sm, SCI_REQ_STP_UDMA_WAIT_D2H);
Dan Williams5dec6f42011-05-10 02:28:49 -07001880 }
1881 break;
1882
Dan Williamsa7e255a2011-05-11 08:27:47 -07001883 /* TODO Check to see if any of these completion status need to
1884 * wait for the device to host register fis.
1885 */
1886 /* TODO We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR
1887 * - this comes only for B0
1888 */
Dan Williams5dec6f42011-05-10 02:28:49 -07001889 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_INV_FIS_LEN):
1890 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
1891 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_R_ERR):
1892 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CMD_LL_R_ERR):
1893 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CRC_ERR):
Dan Williams89a73012011-06-30 19:14:33 -07001894 sci_remote_device_suspend(ireq->target_device,
Dan Williams5dec6f42011-05-10 02:28:49 -07001895 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
1896 /* Fall through to the default case */
1897 default:
1898 /* All other completion status cause the IO to be complete. */
Dan Williams34a99152011-07-01 02:25:15 -07001899 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1900 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1901 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001902 break;
1903 }
1904
1905 return status;
1906}
1907
Edmund Nadolskie3013702011-06-02 00:10:43 +00001908static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001909stp_request_soft_reset_await_h2d_asserted_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001910 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001911{
1912 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1913 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07001914 ireq->scu_status = SCU_TASK_DONE_GOOD;
1915 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001916 sci_change_state(&ireq->sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG);
Dan Williams5dec6f42011-05-10 02:28:49 -07001917 break;
1918
1919 default:
1920 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001921 * All other completion status cause the IO to be complete.
1922 * If a NAK was received, then it is up to the user to retry
1923 * the request.
1924 */
Dan Williams34a99152011-07-01 02:25:15 -07001925 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1926 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001927 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001928 break;
1929 }
1930
1931 return SCI_SUCCESS;
1932}
1933
Edmund Nadolskie3013702011-06-02 00:10:43 +00001934static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001935stp_request_soft_reset_await_h2d_diagnostic_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001936 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001937{
1938 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1939 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07001940 ireq->scu_status = SCU_TASK_DONE_GOOD;
1941 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001942 sci_change_state(&ireq->sm, SCI_REQ_STP_SOFT_RESET_WAIT_D2H);
Dan Williams5dec6f42011-05-10 02:28:49 -07001943 break;
1944
1945 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001946 /* All other completion status cause the IO to be complete. If
1947 * a NAK was received, then it is up to the user to retry the
1948 * request.
1949 */
Dan Williams34a99152011-07-01 02:25:15 -07001950 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1951 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001952 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001953 break;
1954 }
1955
1956 return SCI_SUCCESS;
1957}
1958
Dan Williamsa7e255a2011-05-11 08:27:47 -07001959enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -07001960sci_io_request_tc_completion(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001961 u32 completion_code)
Dan Williamsa7e255a2011-05-11 08:27:47 -07001962{
1963 enum sci_base_request_states state;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001964 struct isci_host *ihost = ireq->owning_controller;
Dan Williamsa7e255a2011-05-11 08:27:47 -07001965
Dan Williams5076a1a2011-06-27 14:57:03 -07001966 state = ireq->sm.current_state_id;
Dan Williamsa7e255a2011-05-11 08:27:47 -07001967
1968 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001969 case SCI_REQ_STARTED:
Dan Williams5076a1a2011-06-27 14:57:03 -07001970 return request_started_state_tc_event(ireq, completion_code);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001971
1972 case SCI_REQ_TASK_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07001973 return ssp_task_request_await_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001974 completion_code);
1975
1976 case SCI_REQ_SMP_WAIT_RESP:
Dan Williams5076a1a2011-06-27 14:57:03 -07001977 return smp_request_await_response_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001978 completion_code);
1979
1980 case SCI_REQ_SMP_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07001981 return smp_request_await_tc_event(ireq, completion_code);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001982
1983 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07001984 return stp_request_udma_await_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001985 completion_code);
1986
1987 case SCI_REQ_STP_NON_DATA_WAIT_H2D:
Dan Williams5076a1a2011-06-27 14:57:03 -07001988 return stp_request_non_data_await_h2d_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001989 completion_code);
1990
1991 case SCI_REQ_STP_PIO_WAIT_H2D:
Dan Williams5076a1a2011-06-27 14:57:03 -07001992 return stp_request_pio_await_h2d_completion_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001993 completion_code);
1994
1995 case SCI_REQ_STP_PIO_DATA_OUT:
Dan Williams5076a1a2011-06-27 14:57:03 -07001996 return pio_data_out_tx_done_tc_event(ireq, completion_code);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001997
1998 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
Dan Williams5076a1a2011-06-27 14:57:03 -07001999 return stp_request_soft_reset_await_h2d_asserted_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002000 completion_code);
2001
2002 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
Dan Williams5076a1a2011-06-27 14:57:03 -07002003 return stp_request_soft_reset_await_h2d_diagnostic_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002004 completion_code);
2005
2006 case SCI_REQ_ABORTING:
Dan Williams5076a1a2011-06-27 14:57:03 -07002007 return request_aborting_state_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002008 completion_code);
2009
2010 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002011 dev_warn(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002012 "%s: SCIC IO Request given task completion "
2013 "notification %x while in wrong state %d\n",
2014 __func__,
2015 completion_code,
2016 state);
2017 return SCI_FAILURE_INVALID_STATE;
Dan Williamsa7e255a2011-05-11 08:27:47 -07002018 }
2019}
2020
Dan Williams6f231dd2011-07-02 22:56:22 -07002021/**
2022 * isci_request_process_response_iu() - This function sets the status and
2023 * response iu, in the task struct, from the request object for the upper
2024 * layer driver.
2025 * @sas_task: This parameter is the task struct from the upper layer driver.
2026 * @resp_iu: This parameter points to the response iu of the completed request.
2027 * @dev: This parameter specifies the linux device struct.
2028 *
2029 * none.
2030 */
2031static void isci_request_process_response_iu(
2032 struct sas_task *task,
2033 struct ssp_response_iu *resp_iu,
2034 struct device *dev)
2035{
2036 dev_dbg(dev,
2037 "%s: resp_iu = %p "
2038 "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
2039 "resp_iu->response_data_len = %x, "
2040 "resp_iu->sense_data_len = %x\nrepsonse data: ",
2041 __func__,
2042 resp_iu,
2043 resp_iu->status,
2044 resp_iu->datapres,
2045 resp_iu->response_data_len,
2046 resp_iu->sense_data_len);
2047
2048 task->task_status.stat = resp_iu->status;
2049
2050 /* libsas updates the task status fields based on the response iu. */
2051 sas_ssp_task_response(dev, task, resp_iu);
2052}
2053
2054/**
2055 * isci_request_set_open_reject_status() - This function prepares the I/O
2056 * completion for OPEN_REJECT conditions.
2057 * @request: This parameter is the completed isci_request object.
2058 * @response_ptr: This parameter specifies the service response for the I/O.
2059 * @status_ptr: This parameter specifies the exec status for the I/O.
2060 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2061 * the LLDD with respect to completing this request or forcing an abort
2062 * condition on the I/O.
2063 * @open_rej_reason: This parameter specifies the encoded reason for the
2064 * abandon-class reject.
2065 *
2066 * none.
2067 */
2068static void isci_request_set_open_reject_status(
2069 struct isci_request *request,
2070 struct sas_task *task,
2071 enum service_response *response_ptr,
2072 enum exec_status *status_ptr,
2073 enum isci_completion_selection *complete_to_host_ptr,
2074 enum sas_open_rej_reason open_rej_reason)
2075{
2076 /* Task in the target is done. */
Dan Williams38d88792011-06-23 14:33:48 -07002077 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002078 *response_ptr = SAS_TASK_UNDELIVERED;
2079 *status_ptr = SAS_OPEN_REJECT;
2080 *complete_to_host_ptr = isci_perform_normal_io_completion;
2081 task->task_status.open_rej_reason = open_rej_reason;
2082}
2083
2084/**
2085 * isci_request_handle_controller_specific_errors() - This function decodes
2086 * controller-specific I/O completion error conditions.
2087 * @request: This parameter is the completed isci_request object.
2088 * @response_ptr: This parameter specifies the service response for the I/O.
2089 * @status_ptr: This parameter specifies the exec status for the I/O.
2090 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2091 * the LLDD with respect to completing this request or forcing an abort
2092 * condition on the I/O.
2093 *
2094 * none.
2095 */
2096static void isci_request_handle_controller_specific_errors(
Dan Williams209fae12011-06-13 17:39:44 -07002097 struct isci_remote_device *idev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002098 struct isci_request *request,
2099 struct sas_task *task,
2100 enum service_response *response_ptr,
2101 enum exec_status *status_ptr,
2102 enum isci_completion_selection *complete_to_host_ptr)
2103{
2104 unsigned int cstatus;
2105
Dan Williams5076a1a2011-06-27 14:57:03 -07002106 cstatus = request->scu_status;
Dan Williams6f231dd2011-07-02 22:56:22 -07002107
2108 dev_dbg(&request->isci_host->pdev->dev,
2109 "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
2110 "- controller status = 0x%x\n",
2111 __func__, request, cstatus);
2112
2113 /* Decode the controller-specific errors; most
2114 * important is to recognize those conditions in which
2115 * the target may still have a task outstanding that
2116 * must be aborted.
2117 *
2118 * Note that there are SCU completion codes being
2119 * named in the decode below for which SCIC has already
2120 * done work to handle them in a way other than as
2121 * a controller-specific completion code; these are left
2122 * in the decode below for completeness sake.
2123 */
2124 switch (cstatus) {
2125 case SCU_TASK_DONE_DMASETUP_DIRERR:
2126 /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
2127 case SCU_TASK_DONE_XFERCNT_ERR:
2128 /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
2129 if (task->task_proto == SAS_PROTOCOL_SMP) {
2130 /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
2131 *response_ptr = SAS_TASK_COMPLETE;
2132
2133 /* See if the device has been/is being stopped. Note
2134 * that we ignore the quiesce state, since we are
2135 * concerned about the actual device state.
2136 */
Dan Williams209fae12011-06-13 17:39:44 -07002137 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002138 *status_ptr = SAS_DEVICE_UNKNOWN;
2139 else
2140 *status_ptr = SAS_ABORTED_TASK;
2141
Dan Williams38d88792011-06-23 14:33:48 -07002142 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002143
2144 *complete_to_host_ptr =
2145 isci_perform_normal_io_completion;
2146 } else {
2147 /* Task in the target is not done. */
2148 *response_ptr = SAS_TASK_UNDELIVERED;
2149
Dan Williams209fae12011-06-13 17:39:44 -07002150 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002151 *status_ptr = SAS_DEVICE_UNKNOWN;
2152 else
2153 *status_ptr = SAM_STAT_TASK_ABORTED;
2154
Dan Williams38d88792011-06-23 14:33:48 -07002155 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002156
2157 *complete_to_host_ptr =
2158 isci_perform_error_io_completion;
2159 }
2160
2161 break;
2162
2163 case SCU_TASK_DONE_CRC_ERR:
2164 case SCU_TASK_DONE_NAK_CMD_ERR:
2165 case SCU_TASK_DONE_EXCESS_DATA:
2166 case SCU_TASK_DONE_UNEXP_FIS:
2167 /* Also SCU_TASK_DONE_UNEXP_RESP: */
2168 case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
2169 case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
2170 case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
2171 /* These are conditions in which the target
2172 * has completed the task, so that no cleanup
2173 * is necessary.
2174 */
2175 *response_ptr = SAS_TASK_COMPLETE;
2176
2177 /* See if the device has been/is being stopped. Note
2178 * that we ignore the quiesce state, since we are
2179 * concerned about the actual device state.
2180 */
Dan Williams209fae12011-06-13 17:39:44 -07002181 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002182 *status_ptr = SAS_DEVICE_UNKNOWN;
2183 else
2184 *status_ptr = SAS_ABORTED_TASK;
2185
Dan Williams38d88792011-06-23 14:33:48 -07002186 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002187
2188 *complete_to_host_ptr = isci_perform_normal_io_completion;
2189 break;
2190
2191
2192 /* Note that the only open reject completion codes seen here will be
2193 * abandon-class codes; all others are automatically retried in the SCU.
2194 */
2195 case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2196
2197 isci_request_set_open_reject_status(
2198 request, task, response_ptr, status_ptr,
2199 complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
2200 break;
2201
2202 case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2203
2204 /* Note - the return of AB0 will change when
2205 * libsas implements detection of zone violations.
2206 */
2207 isci_request_set_open_reject_status(
2208 request, task, response_ptr, status_ptr,
2209 complete_to_host_ptr, SAS_OREJ_RESV_AB0);
2210 break;
2211
2212 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2213
2214 isci_request_set_open_reject_status(
2215 request, task, response_ptr, status_ptr,
2216 complete_to_host_ptr, SAS_OREJ_RESV_AB1);
2217 break;
2218
2219 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2220
2221 isci_request_set_open_reject_status(
2222 request, task, response_ptr, status_ptr,
2223 complete_to_host_ptr, SAS_OREJ_RESV_AB2);
2224 break;
2225
2226 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2227
2228 isci_request_set_open_reject_status(
2229 request, task, response_ptr, status_ptr,
2230 complete_to_host_ptr, SAS_OREJ_RESV_AB3);
2231 break;
2232
2233 case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2234
2235 isci_request_set_open_reject_status(
2236 request, task, response_ptr, status_ptr,
2237 complete_to_host_ptr, SAS_OREJ_BAD_DEST);
2238 break;
2239
2240 case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
2241
2242 isci_request_set_open_reject_status(
2243 request, task, response_ptr, status_ptr,
2244 complete_to_host_ptr, SAS_OREJ_STP_NORES);
2245 break;
2246
2247 case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
2248
2249 isci_request_set_open_reject_status(
2250 request, task, response_ptr, status_ptr,
2251 complete_to_host_ptr, SAS_OREJ_EPROTO);
2252 break;
2253
2254 case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
2255
2256 isci_request_set_open_reject_status(
2257 request, task, response_ptr, status_ptr,
2258 complete_to_host_ptr, SAS_OREJ_CONN_RATE);
2259 break;
2260
2261 case SCU_TASK_DONE_LL_R_ERR:
2262 /* Also SCU_TASK_DONE_ACK_NAK_TO: */
2263 case SCU_TASK_DONE_LL_PERR:
2264 case SCU_TASK_DONE_LL_SY_TERM:
2265 /* Also SCU_TASK_DONE_NAK_ERR:*/
2266 case SCU_TASK_DONE_LL_LF_TERM:
2267 /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
2268 case SCU_TASK_DONE_LL_ABORT_ERR:
2269 case SCU_TASK_DONE_SEQ_INV_TYPE:
2270 /* Also SCU_TASK_DONE_UNEXP_XR: */
2271 case SCU_TASK_DONE_XR_IU_LEN_ERR:
2272 case SCU_TASK_DONE_INV_FIS_LEN:
2273 /* Also SCU_TASK_DONE_XR_WD_LEN: */
2274 case SCU_TASK_DONE_SDMA_ERR:
2275 case SCU_TASK_DONE_OFFSET_ERR:
2276 case SCU_TASK_DONE_MAX_PLD_ERR:
2277 case SCU_TASK_DONE_LF_ERR:
2278 case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
2279 case SCU_TASK_DONE_SMP_LL_RX_ERR:
2280 case SCU_TASK_DONE_UNEXP_DATA:
2281 case SCU_TASK_DONE_UNEXP_SDBFIS:
2282 case SCU_TASK_DONE_REG_ERR:
2283 case SCU_TASK_DONE_SDB_ERR:
2284 case SCU_TASK_DONE_TASK_ABORT:
2285 default:
2286 /* Task in the target is not done. */
2287 *response_ptr = SAS_TASK_UNDELIVERED;
2288 *status_ptr = SAM_STAT_TASK_ABORTED;
Dan Williams6f231dd2011-07-02 22:56:22 -07002289
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002290 if (task->task_proto == SAS_PROTOCOL_SMP) {
Dan Williams38d88792011-06-23 14:33:48 -07002291 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002292
2293 *complete_to_host_ptr = isci_perform_normal_io_completion;
2294 } else {
Dan Williams38d88792011-06-23 14:33:48 -07002295 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002296
2297 *complete_to_host_ptr = isci_perform_error_io_completion;
2298 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002299 break;
2300 }
2301}
2302
2303/**
2304 * isci_task_save_for_upper_layer_completion() - This function saves the
2305 * request for later completion to the upper layer driver.
2306 * @host: This parameter is a pointer to the host on which the the request
2307 * should be queued (either as an error or success).
2308 * @request: This parameter is the completed request.
2309 * @response: This parameter is the response code for the completed task.
2310 * @status: This parameter is the status code for the completed task.
2311 *
2312 * none.
2313 */
2314static void isci_task_save_for_upper_layer_completion(
2315 struct isci_host *host,
2316 struct isci_request *request,
2317 enum service_response response,
2318 enum exec_status status,
2319 enum isci_completion_selection task_notification_selection)
2320{
2321 struct sas_task *task = isci_request_access_task(request);
2322
Jeff Skirvinec6c9632011-03-04 14:06:44 -08002323 task_notification_selection
2324 = isci_task_set_completion_status(task, response, status,
2325 task_notification_selection);
Dan Williams6f231dd2011-07-02 22:56:22 -07002326
2327 /* Tasks aborted specifically by a call to the lldd_abort_task
2328 * function should not be completed to the host in the regular path.
2329 */
2330 switch (task_notification_selection) {
2331
2332 case isci_perform_normal_io_completion:
2333
2334 /* Normal notification (task_done) */
2335 dev_dbg(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002336 "%s: Normal - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002337 __func__,
2338 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002339 task->task_status.resp, response,
2340 task->task_status.stat, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07002341 /* Add to the completed list. */
2342 list_add(&request->completed_node,
2343 &host->requests_to_complete);
Jeff Skirvinec6c9632011-03-04 14:06:44 -08002344
2345 /* Take the request off the device's pending request list. */
2346 list_del_init(&request->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -07002347 break;
2348
2349 case isci_perform_aborted_io_completion:
Jeff Skirvina5fde222011-03-04 14:06:42 -08002350 /* No notification to libsas because this request is
2351 * already in the abort path.
Dan Williams6f231dd2011-07-02 22:56:22 -07002352 */
Dan Williamsa8a0a132011-07-01 12:07:25 -07002353 dev_dbg(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002354 "%s: Aborted - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002355 __func__,
2356 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002357 task->task_status.resp, response,
2358 task->task_status.stat, status);
Jeff Skirvina5fde222011-03-04 14:06:42 -08002359
2360 /* Wake up whatever process was waiting for this
2361 * request to complete.
2362 */
2363 WARN_ON(request->io_request_completion == NULL);
2364
2365 if (request->io_request_completion != NULL) {
2366
2367 /* Signal whoever is waiting that this
2368 * request is complete.
2369 */
2370 complete(request->io_request_completion);
2371 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002372 break;
2373
2374 case isci_perform_error_io_completion:
2375 /* Use sas_task_abort */
Dan Williamsa8a0a132011-07-01 12:07:25 -07002376 dev_dbg(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002377 "%s: Error - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002378 __func__,
2379 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002380 task->task_status.resp, response,
2381 task->task_status.stat, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07002382 /* Add to the aborted list. */
2383 list_add(&request->completed_node,
Jeff Skirvin11b00c12011-03-04 14:06:40 -08002384 &host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002385 break;
2386
2387 default:
Dan Williamsa8a0a132011-07-01 12:07:25 -07002388 dev_dbg(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002389 "%s: Unknown - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002390 __func__,
2391 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002392 task->task_status.resp, response,
2393 task->task_status.stat, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07002394
Jeff Skirvina5fde222011-03-04 14:06:42 -08002395 /* Add to the error to libsas list. */
Dan Williams6f231dd2011-07-02 22:56:22 -07002396 list_add(&request->completed_node,
Jeff Skirvin11b00c12011-03-04 14:06:40 -08002397 &host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002398 break;
2399 }
2400}
2401
Dan Williams16ba7702011-07-01 10:52:55 -07002402static void isci_request_process_stp_response(struct sas_task *task,
2403 void *response_buffer)
2404{
2405 struct dev_to_host_fis *d2h_reg_fis = response_buffer;
2406 struct task_status_struct *ts = &task->task_status;
2407 struct ata_task_resp *resp = (void *)&ts->buf[0];
2408
2409 resp->frame_len = le16_to_cpu(*(__le16 *)(response_buffer + 6));
2410 memcpy(&resp->ending_fis[0], response_buffer + 16, 24);
2411 ts->buf_valid_size = sizeof(*resp);
2412
2413 /**
2414 * If the device fault bit is set in the status register, then
2415 * set the sense data and return.
2416 */
2417 if (d2h_reg_fis->status & ATA_DF)
2418 ts->stat = SAS_PROTO_RESPONSE;
2419 else
2420 ts->stat = SAM_STAT_GOOD;
2421
2422 ts->resp = SAS_TASK_COMPLETE;
2423}
2424
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002425static void isci_request_io_request_complete(struct isci_host *ihost,
Dan Williamsf1f52e72011-05-10 02:28:45 -07002426 struct isci_request *request,
2427 enum sci_io_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07002428{
2429 struct sas_task *task = isci_request_access_task(request);
2430 struct ssp_response_iu *resp_iu;
2431 void *resp_buf;
2432 unsigned long task_flags;
Dan Williams209fae12011-06-13 17:39:44 -07002433 struct isci_remote_device *idev = isci_lookup_device(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002434 enum service_response response = SAS_TASK_UNDELIVERED;
2435 enum exec_status status = SAS_ABORTED_TASK;
2436 enum isci_request_status request_status;
2437 enum isci_completion_selection complete_to_host
2438 = isci_perform_normal_io_completion;
2439
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002440 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002441 "%s: request = %p, task = %p,\n"
2442 "task->data_dir = %d completion_status = 0x%x\n",
2443 __func__,
2444 request,
2445 task,
2446 task->data_dir,
2447 completion_status);
2448
Jeff Skirvina5fde222011-03-04 14:06:42 -08002449 spin_lock(&request->state_lock);
Dan Williams34a99152011-07-01 02:25:15 -07002450 request_status = request->status;
Dan Williams6f231dd2011-07-02 22:56:22 -07002451
2452 /* Decode the request status. Note that if the request has been
2453 * aborted by a task management function, we don't care
2454 * what the status is.
2455 */
2456 switch (request_status) {
2457
2458 case aborted:
2459 /* "aborted" indicates that the request was aborted by a task
2460 * management function, since once a task management request is
2461 * perfomed by the device, the request only completes because
2462 * of the subsequent driver terminate.
2463 *
2464 * Aborted also means an external thread is explicitly managing
2465 * this request, so that we do not complete it up the stack.
2466 *
2467 * The target is still there (since the TMF was successful).
2468 */
Dan Williams38d88792011-06-23 14:33:48 -07002469 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002470 response = SAS_TASK_COMPLETE;
2471
2472 /* See if the device has been/is being stopped. Note
2473 * that we ignore the quiesce state, since we are
2474 * concerned about the actual device state.
2475 */
Dan Williams209fae12011-06-13 17:39:44 -07002476 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002477 status = SAS_DEVICE_UNKNOWN;
2478 else
2479 status = SAS_ABORTED_TASK;
2480
2481 complete_to_host = isci_perform_aborted_io_completion;
2482 /* This was an aborted request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002483
2484 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002485 break;
2486
2487 case aborting:
2488 /* aborting means that the task management function tried and
2489 * failed to abort the request. We need to note the request
2490 * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
2491 * target as down.
2492 *
2493 * Aborting also means an external thread is explicitly managing
2494 * this request, so that we do not complete it up the stack.
2495 */
Dan Williams38d88792011-06-23 14:33:48 -07002496 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002497 response = SAS_TASK_UNDELIVERED;
2498
Dan Williams209fae12011-06-13 17:39:44 -07002499 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002500 /* The device has been /is being stopped. Note that
2501 * we ignore the quiesce state, since we are
2502 * concerned about the actual device state.
2503 */
2504 status = SAS_DEVICE_UNKNOWN;
2505 else
2506 status = SAS_PHY_DOWN;
2507
2508 complete_to_host = isci_perform_aborted_io_completion;
2509
2510 /* This was an aborted request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002511
2512 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002513 break;
2514
2515 case terminating:
2516
2517 /* This was an terminated request. This happens when
2518 * the I/O is being terminated because of an action on
2519 * the device (reset, tear down, etc.), and the I/O needs
2520 * to be completed up the stack.
2521 */
Dan Williams38d88792011-06-23 14:33:48 -07002522 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002523 response = SAS_TASK_UNDELIVERED;
2524
2525 /* See if the device has been/is being stopped. Note
2526 * that we ignore the quiesce state, since we are
2527 * concerned about the actual device state.
2528 */
Dan Williams209fae12011-06-13 17:39:44 -07002529 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002530 status = SAS_DEVICE_UNKNOWN;
2531 else
2532 status = SAS_ABORTED_TASK;
2533
Jeff Skirvina5fde222011-03-04 14:06:42 -08002534 complete_to_host = isci_perform_aborted_io_completion;
Dan Williams6f231dd2011-07-02 22:56:22 -07002535
2536 /* This was a terminated request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002537
2538 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002539 break;
2540
Jeff Skirvin77c852f2011-06-20 14:09:16 -07002541 case dead:
2542 /* This was a terminated request that timed-out during the
2543 * termination process. There is no task to complete to
2544 * libsas.
2545 */
2546 complete_to_host = isci_perform_normal_io_completion;
2547 spin_unlock(&request->state_lock);
2548 break;
2549
Dan Williams6f231dd2011-07-02 22:56:22 -07002550 default:
2551
Jeff Skirvina5fde222011-03-04 14:06:42 -08002552 /* The request is done from an SCU HW perspective. */
2553 request->status = completed;
2554
2555 spin_unlock(&request->state_lock);
2556
Dan Williams6f231dd2011-07-02 22:56:22 -07002557 /* This is an active request being completed from the core. */
2558 switch (completion_status) {
2559
2560 case SCI_IO_FAILURE_RESPONSE_VALID:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002561 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002562 "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
2563 __func__,
2564 request,
2565 task);
2566
2567 if (sas_protocol_ata(task->task_proto)) {
Dan Williams5076a1a2011-06-27 14:57:03 -07002568 resp_buf = &request->stp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -07002569 isci_request_process_stp_response(task,
Dan Williamsb7645812011-05-08 02:35:32 -07002570 resp_buf);
Dan Williams6f231dd2011-07-02 22:56:22 -07002571 } else if (SAS_PROTOCOL_SSP == task->task_proto) {
2572
2573 /* crack the iu response buffer. */
Dan Williams5076a1a2011-06-27 14:57:03 -07002574 resp_iu = &request->ssp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -07002575 isci_request_process_response_iu(task, resp_iu,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002576 &ihost->pdev->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002577
2578 } else if (SAS_PROTOCOL_SMP == task->task_proto) {
2579
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002580 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002581 "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
2582 "SAS_PROTOCOL_SMP protocol\n",
2583 __func__);
2584
2585 } else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002586 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002587 "%s: unknown protocol\n", __func__);
2588
2589 /* use the task status set in the task struct by the
2590 * isci_request_process_response_iu call.
2591 */
Dan Williams38d88792011-06-23 14:33:48 -07002592 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002593 response = task->task_status.resp;
2594 status = task->task_status.stat;
2595 break;
2596
2597 case SCI_IO_SUCCESS:
2598 case SCI_IO_SUCCESS_IO_DONE_EARLY:
2599
2600 response = SAS_TASK_COMPLETE;
2601 status = SAM_STAT_GOOD;
Dan Williams38d88792011-06-23 14:33:48 -07002602 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002603
2604 if (task->task_proto == SAS_PROTOCOL_SMP) {
Dan Williams5076a1a2011-06-27 14:57:03 -07002605 void *rsp = &request->smp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -07002606
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002607 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002608 "%s: SMP protocol completion\n",
2609 __func__);
2610
2611 sg_copy_from_buffer(
2612 &task->smp_task.smp_resp, 1,
Dan Williamsb7645812011-05-08 02:35:32 -07002613 rsp, sizeof(struct smp_resp));
Dan Williams6f231dd2011-07-02 22:56:22 -07002614 } else if (completion_status
2615 == SCI_IO_SUCCESS_IO_DONE_EARLY) {
2616
2617 /* This was an SSP / STP / SATA transfer.
2618 * There is a possibility that less data than
2619 * the maximum was transferred.
2620 */
Dan Williams5076a1a2011-06-27 14:57:03 -07002621 u32 transferred_length = sci_req_tx_bytes(request);
Dan Williams6f231dd2011-07-02 22:56:22 -07002622
2623 task->task_status.residual
2624 = task->total_xfer_len - transferred_length;
2625
2626 /* If there were residual bytes, call this an
2627 * underrun.
2628 */
2629 if (task->task_status.residual != 0)
2630 status = SAS_DATA_UNDERRUN;
2631
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002632 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002633 "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
2634 __func__,
2635 status);
2636
2637 } else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002638 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002639 "%s: SCI_IO_SUCCESS\n",
2640 __func__);
2641
2642 break;
2643
2644 case SCI_IO_FAILURE_TERMINATED:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002645 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002646 "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
2647 __func__,
2648 request,
2649 task);
2650
2651 /* The request was terminated explicitly. No handling
2652 * is needed in the SCSI error handler path.
2653 */
Dan Williams38d88792011-06-23 14:33:48 -07002654 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002655 response = SAS_TASK_UNDELIVERED;
2656
2657 /* See if the device has been/is being stopped. Note
2658 * that we ignore the quiesce state, since we are
2659 * concerned about the actual device state.
2660 */
Dan Williams209fae12011-06-13 17:39:44 -07002661 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002662 status = SAS_DEVICE_UNKNOWN;
2663 else
2664 status = SAS_ABORTED_TASK;
2665
2666 complete_to_host = isci_perform_normal_io_completion;
2667 break;
2668
2669 case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
2670
2671 isci_request_handle_controller_specific_errors(
Dan Williams209fae12011-06-13 17:39:44 -07002672 idev, request, task, &response, &status,
Dan Williams6f231dd2011-07-02 22:56:22 -07002673 &complete_to_host);
2674
2675 break;
2676
2677 case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
2678 /* This is a special case, in that the I/O completion
2679 * is telling us that the device needs a reset.
2680 * In order for the device reset condition to be
2681 * noticed, the I/O has to be handled in the error
2682 * handler. Set the reset flag and cause the
2683 * SCSI error thread to be scheduled.
2684 */
2685 spin_lock_irqsave(&task->task_state_lock, task_flags);
2686 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
2687 spin_unlock_irqrestore(&task->task_state_lock, task_flags);
2688
Jeff Skirvinaa145102011-03-07 16:40:47 -07002689 /* Fail the I/O. */
2690 response = SAS_TASK_UNDELIVERED;
2691 status = SAM_STAT_TASK_ABORTED;
2692
Dan Williams6f231dd2011-07-02 22:56:22 -07002693 complete_to_host = isci_perform_error_io_completion;
Dan Williams38d88792011-06-23 14:33:48 -07002694 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002695 break;
2696
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002697 case SCI_FAILURE_RETRY_REQUIRED:
2698
2699 /* Fail the I/O so it can be retried. */
2700 response = SAS_TASK_UNDELIVERED;
Dan Williams209fae12011-06-13 17:39:44 -07002701 if (!idev)
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002702 status = SAS_DEVICE_UNKNOWN;
2703 else
2704 status = SAS_ABORTED_TASK;
2705
2706 complete_to_host = isci_perform_normal_io_completion;
Dan Williams38d88792011-06-23 14:33:48 -07002707 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002708 break;
2709
2710
Dan Williams6f231dd2011-07-02 22:56:22 -07002711 default:
2712 /* Catch any otherwise unhandled error codes here. */
Dan Williamsa8a0a132011-07-01 12:07:25 -07002713 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002714 "%s: invalid completion code: 0x%x - "
2715 "isci_request = %p\n",
2716 __func__, completion_status, request);
2717
2718 response = SAS_TASK_UNDELIVERED;
2719
2720 /* See if the device has been/is being stopped. Note
2721 * that we ignore the quiesce state, since we are
2722 * concerned about the actual device state.
2723 */
Dan Williams209fae12011-06-13 17:39:44 -07002724 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002725 status = SAS_DEVICE_UNKNOWN;
2726 else
2727 status = SAS_ABORTED_TASK;
2728
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002729 if (SAS_PROTOCOL_SMP == task->task_proto) {
Dan Williams38d88792011-06-23 14:33:48 -07002730 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002731 complete_to_host = isci_perform_normal_io_completion;
2732 } else {
Dan Williams38d88792011-06-23 14:33:48 -07002733 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002734 complete_to_host = isci_perform_error_io_completion;
2735 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002736 break;
2737 }
2738 break;
2739 }
2740
Dan Williamsddcc7e32011-06-17 10:40:43 -07002741 switch (task->task_proto) {
2742 case SAS_PROTOCOL_SSP:
2743 if (task->data_dir == DMA_NONE)
2744 break;
2745 if (task->num_scatter == 0)
2746 /* 0 indicates a single dma address */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002747 dma_unmap_single(&ihost->pdev->dev,
Dan Williamsddcc7e32011-06-17 10:40:43 -07002748 request->zero_scatter_daddr,
2749 task->total_xfer_len, task->data_dir);
2750 else /* unmap the sgl dma addresses */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002751 dma_unmap_sg(&ihost->pdev->dev, task->scatter,
Dan Williamsddcc7e32011-06-17 10:40:43 -07002752 request->num_sg_entries, task->data_dir);
2753 break;
Dan Williamse9bf7092011-06-16 16:59:56 -07002754 case SAS_PROTOCOL_SMP: {
2755 struct scatterlist *sg = &task->smp_task.smp_req;
2756 struct smp_req *smp_req;
2757 void *kaddr;
2758
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002759 dma_unmap_sg(&ihost->pdev->dev, sg, 1, DMA_TO_DEVICE);
Dan Williamse9bf7092011-06-16 16:59:56 -07002760
2761 /* need to swab it back in case the command buffer is re-used */
2762 kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
2763 smp_req = kaddr + sg->offset;
2764 sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
2765 kunmap_atomic(kaddr, KM_IRQ0);
2766 break;
2767 }
Dan Williamsddcc7e32011-06-17 10:40:43 -07002768 default:
2769 break;
2770 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002771
2772 /* Put the completed request on the correct list */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002773 isci_task_save_for_upper_layer_completion(ihost, request, response,
Dan Williams6f231dd2011-07-02 22:56:22 -07002774 status, complete_to_host
2775 );
2776
2777 /* complete the io request to the core. */
Dan Williams89a73012011-06-30 19:14:33 -07002778 sci_controller_complete_io(ihost, request->target_device, request);
Dan Williams209fae12011-06-13 17:39:44 -07002779 isci_put_device(idev);
2780
Dan Williams67ea8382011-05-08 11:47:15 -07002781 /* set terminated handle so it cannot be completed or
Dan Williams6f231dd2011-07-02 22:56:22 -07002782 * terminated again, and to cause any calls into abort
2783 * task to recognize the already completed case.
2784 */
Dan Williams38d88792011-06-23 14:33:48 -07002785 set_bit(IREQ_TERMINATED, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002786}
Dan Williamsf1f52e72011-05-10 02:28:45 -07002787
Dan Williams89a73012011-06-30 19:14:33 -07002788static void sci_request_started_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002789{
Dan Williams5076a1a2011-06-27 14:57:03 -07002790 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams78a6f062011-06-30 16:31:37 -07002791 struct domain_device *dev = ireq->target_device->domain_dev;
Dan Williamsc72086e2011-05-10 02:28:48 -07002792 struct sas_task *task;
2793
2794 /* XXX as hch said always creating an internal sas_task for tmf
2795 * requests would simplify the driver
2796 */
2797 task = ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL;
Dan Williamsf1f52e72011-05-10 02:28:45 -07002798
Dan Williams5dec6f42011-05-10 02:28:49 -07002799 /* all unaccelerated request types (non ssp or ncq) handled with
2800 * substates
Dan Williamsf1393032011-05-10 02:28:47 -07002801 */
Dan Williamsc72086e2011-05-10 02:28:48 -07002802 if (!task && dev->dev_type == SAS_END_DEV) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002803 sci_change_state(sm, SCI_REQ_TASK_WAIT_TC_COMP);
Dan Williams5dec6f42011-05-10 02:28:49 -07002804 } else if (!task &&
2805 (isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_high ||
2806 isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_low)) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002807 sci_change_state(sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED);
Dan Williamsc72086e2011-05-10 02:28:48 -07002808 } else if (task && task->task_proto == SAS_PROTOCOL_SMP) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002809 sci_change_state(sm, SCI_REQ_SMP_WAIT_RESP);
Dan Williams5dec6f42011-05-10 02:28:49 -07002810 } else if (task && sas_protocol_ata(task->task_proto) &&
2811 !task->ata_task.use_ncq) {
2812 u32 state;
2813
2814 if (task->data_dir == DMA_NONE)
Edmund Nadolskie3013702011-06-02 00:10:43 +00002815 state = SCI_REQ_STP_NON_DATA_WAIT_H2D;
Dan Williams5dec6f42011-05-10 02:28:49 -07002816 else if (task->ata_task.dma_xfer)
Edmund Nadolskie3013702011-06-02 00:10:43 +00002817 state = SCI_REQ_STP_UDMA_WAIT_TC_COMP;
Dan Williams5dec6f42011-05-10 02:28:49 -07002818 else /* PIO */
Edmund Nadolskie3013702011-06-02 00:10:43 +00002819 state = SCI_REQ_STP_PIO_WAIT_H2D;
Dan Williams5dec6f42011-05-10 02:28:49 -07002820
Edmund Nadolskie3013702011-06-02 00:10:43 +00002821 sci_change_state(sm, state);
Dan Williamsc72086e2011-05-10 02:28:48 -07002822 }
Dan Williamsf1f52e72011-05-10 02:28:45 -07002823}
2824
Dan Williams89a73012011-06-30 19:14:33 -07002825static void sci_request_completed_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002826{
Dan Williams5076a1a2011-06-27 14:57:03 -07002827 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002828 struct isci_host *ihost = ireq->owning_controller;
Dan Williamsf1f52e72011-05-10 02:28:45 -07002829
Dan Williamsf1f52e72011-05-10 02:28:45 -07002830 /* Tell the SCI_USER that the IO request is complete */
Dan Williams38d88792011-06-23 14:33:48 -07002831 if (!test_bit(IREQ_TMF, &ireq->flags))
Dan Williamsf1f52e72011-05-10 02:28:45 -07002832 isci_request_io_request_complete(ihost, ireq,
Dan Williams5076a1a2011-06-27 14:57:03 -07002833 ireq->sci_status);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002834 else
Dan Williams5076a1a2011-06-27 14:57:03 -07002835 isci_task_request_complete(ihost, ireq, ireq->sci_status);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002836}
2837
Dan Williams89a73012011-06-30 19:14:33 -07002838static void sci_request_aborting_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002839{
Dan Williams5076a1a2011-06-27 14:57:03 -07002840 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002841
2842 /* Setting the abort bit in the Task Context is required by the silicon. */
Dan Williams5076a1a2011-06-27 14:57:03 -07002843 ireq->tc->abort = 1;
Dan Williamsc72086e2011-05-10 02:28:48 -07002844}
2845
Dan Williams89a73012011-06-30 19:14:33 -07002846static void sci_stp_request_started_non_data_await_h2d_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07002847{
Dan Williams5076a1a2011-06-27 14:57:03 -07002848 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams5dec6f42011-05-10 02:28:49 -07002849
Dan Williams34a99152011-07-01 02:25:15 -07002850 ireq->target_device->working_request = ireq;
Dan Williams5dec6f42011-05-10 02:28:49 -07002851}
2852
Dan Williams89a73012011-06-30 19:14:33 -07002853static void sci_stp_request_started_pio_await_h2d_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07002854{
Dan Williams5076a1a2011-06-27 14:57:03 -07002855 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams5dec6f42011-05-10 02:28:49 -07002856
Dan Williams34a99152011-07-01 02:25:15 -07002857 ireq->target_device->working_request = ireq;
Dan Williams5dec6f42011-05-10 02:28:49 -07002858}
2859
Dan Williams89a73012011-06-30 19:14:33 -07002860static void sci_stp_request_started_soft_reset_await_h2d_asserted_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07002861{
Dan Williams5076a1a2011-06-27 14:57:03 -07002862 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams5dec6f42011-05-10 02:28:49 -07002863
Dan Williams34a99152011-07-01 02:25:15 -07002864 ireq->target_device->working_request = ireq;
Dan Williams5dec6f42011-05-10 02:28:49 -07002865}
2866
Dan Williams89a73012011-06-30 19:14:33 -07002867static void sci_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07002868{
Dan Williams5076a1a2011-06-27 14:57:03 -07002869 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
2870 struct scu_task_context *tc = ireq->tc;
Dan Williams5dec6f42011-05-10 02:28:49 -07002871 struct host_to_dev_fis *h2d_fis;
2872 enum sci_status status;
2873
2874 /* Clear the SRST bit */
Dan Williams5076a1a2011-06-27 14:57:03 -07002875 h2d_fis = &ireq->stp.cmd;
Dan Williams5dec6f42011-05-10 02:28:49 -07002876 h2d_fis->control = 0;
2877
2878 /* Clear the TC control bit */
Dan Williams312e0c22011-06-28 13:47:09 -07002879 tc->control_frame = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07002880
Dan Williams89a73012011-06-30 19:14:33 -07002881 status = sci_controller_continue_io(ireq);
Dan Williams79e2b6b2011-05-11 08:29:56 -07002882 WARN_ONCE(status != SCI_SUCCESS, "isci: continue io failure\n");
Dan Williams5dec6f42011-05-10 02:28:49 -07002883}
2884
Dan Williams89a73012011-06-30 19:14:33 -07002885static const struct sci_base_state sci_request_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002886 [SCI_REQ_INIT] = { },
2887 [SCI_REQ_CONSTRUCTED] = { },
2888 [SCI_REQ_STARTED] = {
Dan Williams89a73012011-06-30 19:14:33 -07002889 .enter_state = sci_request_started_state_enter,
Dan Williams5dec6f42011-05-10 02:28:49 -07002890 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002891 [SCI_REQ_STP_NON_DATA_WAIT_H2D] = {
Dan Williams89a73012011-06-30 19:14:33 -07002892 .enter_state = sci_stp_request_started_non_data_await_h2d_completion_enter,
Dan Williams5dec6f42011-05-10 02:28:49 -07002893 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002894 [SCI_REQ_STP_NON_DATA_WAIT_D2H] = { },
2895 [SCI_REQ_STP_PIO_WAIT_H2D] = {
Dan Williams89a73012011-06-30 19:14:33 -07002896 .enter_state = sci_stp_request_started_pio_await_h2d_completion_enter,
Dan Williams5dec6f42011-05-10 02:28:49 -07002897 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002898 [SCI_REQ_STP_PIO_WAIT_FRAME] = { },
2899 [SCI_REQ_STP_PIO_DATA_IN] = { },
2900 [SCI_REQ_STP_PIO_DATA_OUT] = { },
2901 [SCI_REQ_STP_UDMA_WAIT_TC_COMP] = { },
2902 [SCI_REQ_STP_UDMA_WAIT_D2H] = { },
2903 [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED] = {
Dan Williams89a73012011-06-30 19:14:33 -07002904 .enter_state = sci_stp_request_started_soft_reset_await_h2d_asserted_completion_enter,
Dan Williams5dec6f42011-05-10 02:28:49 -07002905 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002906 [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG] = {
Dan Williams89a73012011-06-30 19:14:33 -07002907 .enter_state = sci_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter,
Dan Williams5dec6f42011-05-10 02:28:49 -07002908 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002909 [SCI_REQ_STP_SOFT_RESET_WAIT_D2H] = { },
2910 [SCI_REQ_TASK_WAIT_TC_COMP] = { },
2911 [SCI_REQ_TASK_WAIT_TC_RESP] = { },
2912 [SCI_REQ_SMP_WAIT_RESP] = { },
2913 [SCI_REQ_SMP_WAIT_TC_COMP] = { },
2914 [SCI_REQ_COMPLETED] = {
Dan Williams89a73012011-06-30 19:14:33 -07002915 .enter_state = sci_request_completed_state_enter,
Dan Williamsf1f52e72011-05-10 02:28:45 -07002916 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002917 [SCI_REQ_ABORTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07002918 .enter_state = sci_request_aborting_state_enter,
Dan Williamsf1f52e72011-05-10 02:28:45 -07002919 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002920 [SCI_REQ_FINAL] = { },
Dan Williamsf1f52e72011-05-10 02:28:45 -07002921};
2922
Edmund Nadolskie3013702011-06-02 00:10:43 +00002923static void
Dan Williams89a73012011-06-30 19:14:33 -07002924sci_general_request_construct(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07002925 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07002926 struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002927{
Dan Williams89a73012011-06-30 19:14:33 -07002928 sci_init_sm(&ireq->sm, sci_request_state_table, SCI_REQ_INIT);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002929
Dan Williams78a6f062011-06-30 16:31:37 -07002930 ireq->target_device = idev;
Dan Williams5076a1a2011-06-27 14:57:03 -07002931 ireq->protocol = SCIC_NO_PROTOCOL;
2932 ireq->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
Dan Williamsf1f52e72011-05-10 02:28:45 -07002933
Dan Williams5076a1a2011-06-27 14:57:03 -07002934 ireq->sci_status = SCI_SUCCESS;
2935 ireq->scu_status = 0;
2936 ireq->post_context = 0xFFFFFFFF;
Dan Williamsf1f52e72011-05-10 02:28:45 -07002937}
2938
2939static enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -07002940sci_io_request_construct(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07002941 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07002942 struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002943{
Dan Williams78a6f062011-06-30 16:31:37 -07002944 struct domain_device *dev = idev->domain_dev;
Dan Williamsf1f52e72011-05-10 02:28:45 -07002945 enum sci_status status = SCI_SUCCESS;
2946
2947 /* Build the common part of the request */
Dan Williams89a73012011-06-30 19:14:33 -07002948 sci_general_request_construct(ihost, idev, ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002949
Dan Williams78a6f062011-06-30 16:31:37 -07002950 if (idev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002951 return SCI_FAILURE_INVALID_REMOTE_DEVICE;
2952
2953 if (dev->dev_type == SAS_END_DEV)
Dan Williamsc72086e2011-05-10 02:28:48 -07002954 /* pass */;
2955 else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
Dan Williams5076a1a2011-06-27 14:57:03 -07002956 memset(&ireq->stp.cmd, 0, sizeof(ireq->stp.cmd));
Dan Williamsc72086e2011-05-10 02:28:48 -07002957 else if (dev_is_expander(dev))
Dan Williamse9bf7092011-06-16 16:59:56 -07002958 /* pass */;
Dan Williamsc72086e2011-05-10 02:28:48 -07002959 else
2960 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williamsf1f52e72011-05-10 02:28:45 -07002961
Dan Williams5076a1a2011-06-27 14:57:03 -07002962 memset(ireq->tc, 0, offsetof(struct scu_task_context, sgl_pair_ab));
Dan Williamsf1f52e72011-05-10 02:28:45 -07002963
2964 return status;
2965}
2966
Dan Williams89a73012011-06-30 19:14:33 -07002967enum sci_status sci_task_request_construct(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07002968 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07002969 u16 io_tag, struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002970{
Dan Williams78a6f062011-06-30 16:31:37 -07002971 struct domain_device *dev = idev->domain_dev;
Dan Williamsf1f52e72011-05-10 02:28:45 -07002972 enum sci_status status = SCI_SUCCESS;
2973
2974 /* Build the common part of the request */
Dan Williams89a73012011-06-30 19:14:33 -07002975 sci_general_request_construct(ihost, idev, ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002976
Dan Williamsc72086e2011-05-10 02:28:48 -07002977 if (dev->dev_type == SAS_END_DEV ||
2978 dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams5076a1a2011-06-27 14:57:03 -07002979 set_bit(IREQ_TMF, &ireq->flags);
2980 memset(ireq->tc, 0, sizeof(struct scu_task_context));
Dan Williamsc72086e2011-05-10 02:28:48 -07002981 } else
2982 status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williamsf1f52e72011-05-10 02:28:45 -07002983
2984 return status;
2985}
2986
2987static enum sci_status isci_request_ssp_request_construct(
2988 struct isci_request *request)
2989{
2990 enum sci_status status;
2991
2992 dev_dbg(&request->isci_host->pdev->dev,
2993 "%s: request = %p\n",
2994 __func__,
2995 request);
Dan Williams89a73012011-06-30 19:14:33 -07002996 status = sci_io_request_construct_basic_ssp(request);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002997 return status;
2998}
2999
Dan Williams16ba7702011-07-01 10:52:55 -07003000static enum sci_status isci_request_stp_request_construct(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003001{
Dan Williams16ba7702011-07-01 10:52:55 -07003002 struct sas_task *task = isci_request_access_task(ireq);
3003 struct host_to_dev_fis *fis = &ireq->stp.cmd;
3004 struct ata_queued_cmd *qc = task->uldd_task;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003005 enum sci_status status;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003006
Dan Williams16ba7702011-07-01 10:52:55 -07003007 dev_dbg(&ireq->isci_host->pdev->dev,
3008 "%s: ireq = %p\n",
Dan Williamsf1f52e72011-05-10 02:28:45 -07003009 __func__,
Dan Williams16ba7702011-07-01 10:52:55 -07003010 ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003011
Dan Williams16ba7702011-07-01 10:52:55 -07003012 memcpy(fis, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
3013 if (!task->ata_task.device_control_reg_update)
3014 fis->flags |= 0x80;
3015 fis->flags &= 0xF0;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003016
Dan Williams16ba7702011-07-01 10:52:55 -07003017 status = sci_io_request_construct_basic_sata(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003018
Dan Williams16ba7702011-07-01 10:52:55 -07003019 if (qc && (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
3020 qc->tf.command == ATA_CMD_FPDMA_READ)) {
3021 fis->sector_count = qc->tag << 3;
3022 ireq->tc->type.stp.ncq_tag = qc->tag;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003023 }
3024
3025 return status;
3026}
3027
Dan Williamse9bf7092011-06-16 16:59:56 -07003028static enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -07003029sci_io_request_construct_smp(struct device *dev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003030 struct isci_request *ireq,
Dan Williamse9bf7092011-06-16 16:59:56 -07003031 struct sas_task *task)
Dan Williamsc72086e2011-05-10 02:28:48 -07003032{
Dan Williamse9bf7092011-06-16 16:59:56 -07003033 struct scatterlist *sg = &task->smp_task.smp_req;
Dan Williams78a6f062011-06-30 16:31:37 -07003034 struct isci_remote_device *idev;
Dan Williamsc72086e2011-05-10 02:28:48 -07003035 struct scu_task_context *task_context;
Dan Williamsffe191c2011-06-29 13:09:25 -07003036 struct isci_port *iport;
Dan Williamse9bf7092011-06-16 16:59:56 -07003037 struct smp_req *smp_req;
3038 void *kaddr;
3039 u8 req_len;
3040 u32 cmd;
3041
3042 kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
3043 smp_req = kaddr + sg->offset;
3044 /*
3045 * Look at the SMP requests' header fields; for certain SAS 1.x SMP
3046 * functions under SAS 2.0, a zero request length really indicates
3047 * a non-zero default length.
3048 */
3049 if (smp_req->req_len == 0) {
3050 switch (smp_req->func) {
3051 case SMP_DISCOVER:
3052 case SMP_REPORT_PHY_ERR_LOG:
3053 case SMP_REPORT_PHY_SATA:
3054 case SMP_REPORT_ROUTE_INFO:
3055 smp_req->req_len = 2;
3056 break;
3057 case SMP_CONF_ROUTE_INFO:
3058 case SMP_PHY_CONTROL:
3059 case SMP_PHY_TEST_FUNCTION:
3060 smp_req->req_len = 9;
3061 break;
3062 /* Default - zero is a valid default for 2.0. */
3063 }
3064 }
3065 req_len = smp_req->req_len;
3066 sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
3067 cmd = *(u32 *) smp_req;
3068 kunmap_atomic(kaddr, KM_IRQ0);
3069
3070 if (!dma_map_sg(dev, sg, 1, DMA_TO_DEVICE))
3071 return SCI_FAILURE;
3072
Dan Williams5076a1a2011-06-27 14:57:03 -07003073 ireq->protocol = SCIC_SMP_PROTOCOL;
Dan Williamsc72086e2011-05-10 02:28:48 -07003074
3075 /* byte swap the smp request. */
Dan Williamsc72086e2011-05-10 02:28:48 -07003076
Dan Williams5076a1a2011-06-27 14:57:03 -07003077 task_context = ireq->tc;
Dan Williamsc72086e2011-05-10 02:28:48 -07003078
Dan Williams34a99152011-07-01 02:25:15 -07003079 idev = ireq->target_device;
3080 iport = idev->owning_port;
Dan Williamsc72086e2011-05-10 02:28:48 -07003081
3082 /*
3083 * Fill in the TC with the its required data
3084 * 00h
3085 */
3086 task_context->priority = 0;
3087 task_context->initiator_request = 1;
Dan Williams78a6f062011-06-30 16:31:37 -07003088 task_context->connection_rate = idev->connection_rate;
Dan Williams34a99152011-07-01 02:25:15 -07003089 task_context->protocol_engine_index = ISCI_PEG;
3090 task_context->logical_port_index = iport->physical_port_index;
Dan Williamsc72086e2011-05-10 02:28:48 -07003091 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
3092 task_context->abort = 0;
3093 task_context->valid = SCU_TASK_CONTEXT_VALID;
3094 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
3095
3096 /* 04h */
Dan Williams78a6f062011-06-30 16:31:37 -07003097 task_context->remote_node_index = idev->rnc.remote_node_index;
Dan Williamsc72086e2011-05-10 02:28:48 -07003098 task_context->command_code = 0;
3099 task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;
3100
3101 /* 08h */
3102 task_context->link_layer_control = 0;
3103 task_context->do_not_dma_ssp_good_response = 1;
3104 task_context->strict_ordering = 0;
3105 task_context->control_frame = 1;
3106 task_context->timeout_enable = 0;
3107 task_context->block_guard_enable = 0;
3108
3109 /* 0ch */
3110 task_context->address_modifier = 0;
3111
3112 /* 10h */
Dave Jiang77d67382011-05-25 02:21:57 +00003113 task_context->ssp_command_iu_length = req_len;
Dan Williamsc72086e2011-05-10 02:28:48 -07003114
3115 /* 14h */
3116 task_context->transfer_length_bytes = 0;
3117
3118 /*
3119 * 18h ~ 30h, protocol specific
3120 * since commandIU has been build by framework at this point, we just
3121 * copy the frist DWord from command IU to this location. */
Dan Williamse9bf7092011-06-16 16:59:56 -07003122 memcpy(&task_context->type.smp, &cmd, sizeof(u32));
Dan Williamsc72086e2011-05-10 02:28:48 -07003123
3124 /*
3125 * 40h
3126 * "For SMP you could program it to zero. We would prefer that way
3127 * so that done code will be consistent." - Venki
3128 */
3129 task_context->task_phase = 0;
3130
Dan Williams5076a1a2011-06-27 14:57:03 -07003131 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williams34a99152011-07-01 02:25:15 -07003132 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
3133 (iport->physical_port_index <<
3134 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
3135 ISCI_TAG_TCI(ireq->io_tag));
Dan Williamsc72086e2011-05-10 02:28:48 -07003136 /*
3137 * Copy the physical address for the command buffer to the SCU Task
3138 * Context command buffer should not contain command header.
3139 */
Dan Williamse9bf7092011-06-16 16:59:56 -07003140 task_context->command_iu_upper = upper_32_bits(sg_dma_address(sg));
3141 task_context->command_iu_lower = lower_32_bits(sg_dma_address(sg) + sizeof(u32));
Dan Williamsc72086e2011-05-10 02:28:48 -07003142
3143 /* SMP response comes as UF, so no need to set response IU address. */
3144 task_context->response_iu_upper = 0;
3145 task_context->response_iu_lower = 0;
Dan Williamsc72086e2011-05-10 02:28:48 -07003146
Dan Williams5076a1a2011-06-27 14:57:03 -07003147 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williamsc72086e2011-05-10 02:28:48 -07003148
3149 return SCI_SUCCESS;
3150}
3151
3152/*
Dan Williamsf1f52e72011-05-10 02:28:45 -07003153 * isci_smp_request_build() - This function builds the smp request.
3154 * @ireq: This parameter points to the isci_request allocated in the
3155 * request construct function.
3156 *
3157 * SCI_SUCCESS on successfull completion, or specific failure code.
3158 */
3159static enum sci_status isci_smp_request_build(struct isci_request *ireq)
3160{
Dan Williamsf1f52e72011-05-10 02:28:45 -07003161 struct sas_task *task = isci_request_access_task(ireq);
Dan Williamse9bf7092011-06-16 16:59:56 -07003162 struct device *dev = &ireq->isci_host->pdev->dev;
Dan Williamse9bf7092011-06-16 16:59:56 -07003163 enum sci_status status = SCI_FAILURE;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003164
Dan Williams89a73012011-06-30 19:14:33 -07003165 status = sci_io_request_construct_smp(dev, ireq, task);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003166 if (status != SCI_SUCCESS)
Dan Williamsa8a0a132011-07-01 12:07:25 -07003167 dev_dbg(&ireq->isci_host->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003168 "%s: failed with status = %d\n",
3169 __func__,
3170 status);
3171
3172 return status;
3173}
3174
3175/**
3176 * isci_io_request_build() - This function builds the io request object.
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003177 * @ihost: This parameter specifies the ISCI host object
Dan Williamsf1f52e72011-05-10 02:28:45 -07003178 * @request: This parameter points to the isci_request object allocated in the
3179 * request construct function.
3180 * @sci_device: This parameter is the handle for the sci core's remote device
3181 * object that is the destination for this request.
3182 *
3183 * SCI_SUCCESS on successfull completion, or specific failure code.
3184 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003185static enum sci_status isci_io_request_build(struct isci_host *ihost,
Dan Williams312e0c22011-06-28 13:47:09 -07003186 struct isci_request *request,
Dan Williams78a6f062011-06-30 16:31:37 -07003187 struct isci_remote_device *idev)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003188{
3189 enum sci_status status = SCI_SUCCESS;
3190 struct sas_task *task = isci_request_access_task(request);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003191
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003192 dev_dbg(&ihost->pdev->dev,
Dan Williams78a6f062011-06-30 16:31:37 -07003193 "%s: idev = 0x%p; request = %p, "
Dan Williamsf1f52e72011-05-10 02:28:45 -07003194 "num_scatter = %d\n",
3195 __func__,
Dan Williams78a6f062011-06-30 16:31:37 -07003196 idev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003197 request,
3198 task->num_scatter);
3199
3200 /* map the sgl addresses, if present.
3201 * libata does the mapping for sata devices
3202 * before we get the request.
3203 */
3204 if (task->num_scatter &&
3205 !sas_protocol_ata(task->task_proto) &&
3206 !(SAS_PROTOCOL_SMP & task->task_proto)) {
3207
3208 request->num_sg_entries = dma_map_sg(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003209 &ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003210 task->scatter,
3211 task->num_scatter,
3212 task->data_dir
3213 );
3214
3215 if (request->num_sg_entries == 0)
3216 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
3217 }
3218
Dan Williams89a73012011-06-30 19:14:33 -07003219 status = sci_io_request_construct(ihost, idev, request);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003220
3221 if (status != SCI_SUCCESS) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07003222 dev_dbg(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003223 "%s: failed request construct\n",
3224 __func__);
3225 return SCI_FAILURE;
3226 }
3227
3228 switch (task->task_proto) {
3229 case SAS_PROTOCOL_SMP:
3230 status = isci_smp_request_build(request);
3231 break;
3232 case SAS_PROTOCOL_SSP:
3233 status = isci_request_ssp_request_construct(request);
3234 break;
3235 case SAS_PROTOCOL_SATA:
3236 case SAS_PROTOCOL_STP:
3237 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
3238 status = isci_request_stp_request_construct(request);
3239 break;
3240 default:
Dan Williamsa8a0a132011-07-01 12:07:25 -07003241 dev_dbg(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003242 "%s: unknown protocol\n", __func__);
3243 return SCI_FAILURE;
3244 }
3245
3246 return SCI_SUCCESS;
3247}
3248
Dan Williamsdb056252011-06-17 14:18:39 -07003249static struct isci_request *isci_request_from_tag(struct isci_host *ihost, u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003250{
Dan Williams0d0cf142011-06-13 00:51:30 -07003251 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003252
Dan Williamsdb056252011-06-17 14:18:39 -07003253 ireq = ihost->reqs[ISCI_TAG_TCI(tag)];
Dan Williams5076a1a2011-06-27 14:57:03 -07003254 ireq->io_tag = tag;
Dan Williams0d0cf142011-06-13 00:51:30 -07003255 ireq->io_request_completion = NULL;
Dan Williams38d88792011-06-23 14:33:48 -07003256 ireq->flags = 0;
Dan Williams0d0cf142011-06-13 00:51:30 -07003257 ireq->num_sg_entries = 0;
Dan Williams0d0cf142011-06-13 00:51:30 -07003258 INIT_LIST_HEAD(&ireq->completed_node);
3259 INIT_LIST_HEAD(&ireq->dev_node);
Dan Williams0d0cf142011-06-13 00:51:30 -07003260 isci_request_change_state(ireq, allocated);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003261
Dan Williams0d0cf142011-06-13 00:51:30 -07003262 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003263}
3264
Dan Williamsdb056252011-06-17 14:18:39 -07003265static struct isci_request *isci_io_request_from_tag(struct isci_host *ihost,
3266 struct sas_task *task,
3267 u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003268{
Dan Williams0d0cf142011-06-13 00:51:30 -07003269 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003270
Dan Williamsdb056252011-06-17 14:18:39 -07003271 ireq = isci_request_from_tag(ihost, tag);
3272 ireq->ttype_ptr.io_task_ptr = task;
3273 ireq->ttype = io_task;
3274 task->lldd_task = ireq;
3275
Dan Williams0d0cf142011-06-13 00:51:30 -07003276 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003277}
3278
Dan Williamsdb056252011-06-17 14:18:39 -07003279struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
3280 struct isci_tmf *isci_tmf,
3281 u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003282{
Dan Williams0d0cf142011-06-13 00:51:30 -07003283 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003284
Dan Williamsdb056252011-06-17 14:18:39 -07003285 ireq = isci_request_from_tag(ihost, tag);
3286 ireq->ttype_ptr.tmf_task_ptr = isci_tmf;
3287 ireq->ttype = tmf_task;
3288
Dan Williams0d0cf142011-06-13 00:51:30 -07003289 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003290}
3291
Dan Williams209fae12011-06-13 17:39:44 -07003292int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
Dan Williamsdb056252011-06-17 14:18:39 -07003293 struct sas_task *task, u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003294{
Dan Williamsf1f52e72011-05-10 02:28:45 -07003295 enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams0d0cf142011-06-13 00:51:30 -07003296 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003297 unsigned long flags;
Dan Williams0d0cf142011-06-13 00:51:30 -07003298 int ret = 0;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003299
Dan Williamsf1f52e72011-05-10 02:28:45 -07003300 /* do common allocation and init of request object. */
Dan Williamsdb056252011-06-17 14:18:39 -07003301 ireq = isci_io_request_from_tag(ihost, task, tag);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003302
Dan Williamsdb056252011-06-17 14:18:39 -07003303 status = isci_io_request_build(ihost, ireq, idev);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003304 if (status != SCI_SUCCESS) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07003305 dev_dbg(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003306 "%s: request_construct failed - status = 0x%x\n",
3307 __func__,
3308 status);
Dan Williamsdb056252011-06-17 14:18:39 -07003309 return status;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003310 }
3311
Dan Williams0d0cf142011-06-13 00:51:30 -07003312 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003313
Jeff Skirvin9274f452011-06-23 17:09:02 -07003314 if (test_bit(IDEV_IO_NCQERROR, &idev->flags)) {
3315
3316 if (isci_task_is_ncq_recovery(task)) {
3317
3318 /* The device is in an NCQ recovery state. Issue the
3319 * request on the task side. Note that it will
3320 * complete on the I/O request side because the
3321 * request was built that way (ie.
3322 * ireq->is_task_management_request is false).
3323 */
Dan Williams89a73012011-06-30 19:14:33 -07003324 status = sci_controller_start_task(ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07003325 idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003326 ireq);
Jeff Skirvin9274f452011-06-23 17:09:02 -07003327 } else {
3328 status = SCI_FAILURE;
3329 }
3330 } else {
Jeff Skirvin9274f452011-06-23 17:09:02 -07003331 /* send the request, let the core assign the IO TAG. */
Dan Williams89a73012011-06-30 19:14:33 -07003332 status = sci_controller_start_io(ihost, idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003333 ireq);
Jeff Skirvin9274f452011-06-23 17:09:02 -07003334 }
Dan Williams312e0c22011-06-28 13:47:09 -07003335
Dan Williamsf1f52e72011-05-10 02:28:45 -07003336 if (status != SCI_SUCCESS &&
3337 status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07003338 dev_dbg(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003339 "%s: failed request start (0x%x)\n",
3340 __func__, status);
Dan Williams0d0cf142011-06-13 00:51:30 -07003341 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsdb056252011-06-17 14:18:39 -07003342 return status;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003343 }
3344
3345 /* Either I/O started OK, or the core has signaled that
3346 * the device needs a target reset.
3347 *
3348 * In either case, hold onto the I/O for later.
3349 *
3350 * Update it's status and add it to the list in the
3351 * remote device object.
3352 */
Dan Williams0d0cf142011-06-13 00:51:30 -07003353 list_add(&ireq->dev_node, &idev->reqs_in_process);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003354
3355 if (status == SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -07003356 isci_request_change_state(ireq, started);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003357 } else {
3358 /* The request did not really start in the
3359 * hardware, so clear the request handle
3360 * here so no terminations will be done.
3361 */
Dan Williams38d88792011-06-23 14:33:48 -07003362 set_bit(IREQ_TERMINATED, &ireq->flags);
Dan Williams0d0cf142011-06-13 00:51:30 -07003363 isci_request_change_state(ireq, completed);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003364 }
Dan Williams0d0cf142011-06-13 00:51:30 -07003365 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003366
3367 if (status ==
3368 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
3369 /* Signal libsas that we need the SCSI error
Dan Williams312e0c22011-06-28 13:47:09 -07003370 * handler thread to work on this I/O and that
3371 * we want a device reset.
3372 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07003373 spin_lock_irqsave(&task->task_state_lock, flags);
3374 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
3375 spin_unlock_irqrestore(&task->task_state_lock, flags);
3376
3377 /* Cause this task to be scheduled in the SCSI error
Dan Williams312e0c22011-06-28 13:47:09 -07003378 * handler thread.
3379 */
Dan Williams0d0cf142011-06-13 00:51:30 -07003380 isci_execpath_callback(ihost, task,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003381 sas_task_abort);
3382
3383 /* Change the status, since we are holding
Dan Williams312e0c22011-06-28 13:47:09 -07003384 * the I/O until it is managed by the SCSI
3385 * error handler.
3386 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07003387 status = SCI_SUCCESS;
3388 }
3389
Dan Williamsf1f52e72011-05-10 02:28:45 -07003390 return ret;
3391}