blob: c544bc79ce17a752132d955451d987fc45fe3b80 [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55
56#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "task.h"
58#include "request.h"
59#include "sata.h"
60#include "scu_completion_codes.h"
Dan Williams5dec6f42011-05-10 02:28:49 -070061#include "scu_event_codes.h"
Dave Jiang2ec53eb2011-05-04 18:01:22 -070062#include "sas.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070063
Dan Williams5076a1a2011-06-27 14:57:03 -070064static struct scu_sgl_element_pair *to_sgl_element_pair(struct isci_request *ireq,
Dan Williams312e0c22011-06-28 13:47:09 -070065 int idx)
66{
67 if (idx == 0)
Dan Williams5076a1a2011-06-27 14:57:03 -070068 return &ireq->tc->sgl_pair_ab;
Dan Williams312e0c22011-06-28 13:47:09 -070069 else if (idx == 1)
Dan Williams5076a1a2011-06-27 14:57:03 -070070 return &ireq->tc->sgl_pair_cd;
Dan Williams312e0c22011-06-28 13:47:09 -070071 else if (idx < 0)
72 return NULL;
73 else
Dan Williams5076a1a2011-06-27 14:57:03 -070074 return &ireq->sg_table[idx - 2];
Dan Williams6f231dd2011-07-02 22:56:22 -070075}
76
Dan Williams312e0c22011-06-28 13:47:09 -070077static dma_addr_t to_sgl_element_pair_dma(struct scic_sds_controller *scic,
Dan Williams5076a1a2011-06-27 14:57:03 -070078 struct isci_request *ireq, u32 idx)
Dan Williams312e0c22011-06-28 13:47:09 -070079{
80 u32 offset;
81
82 if (idx == 0) {
Dan Williams5076a1a2011-06-27 14:57:03 -070083 offset = (void *) &ireq->tc->sgl_pair_ab -
Dan Williams312e0c22011-06-28 13:47:09 -070084 (void *) &scic->task_context_table[0];
85 return scic->task_context_dma + offset;
86 } else if (idx == 1) {
Dan Williams5076a1a2011-06-27 14:57:03 -070087 offset = (void *) &ireq->tc->sgl_pair_cd -
Dan Williams312e0c22011-06-28 13:47:09 -070088 (void *) &scic->task_context_table[0];
89 return scic->task_context_dma + offset;
90 }
91
Dan Williams5076a1a2011-06-27 14:57:03 -070092 return scic_io_request_get_dma_addr(ireq, &ireq->sg_table[idx - 2]);
Dan Williams312e0c22011-06-28 13:47:09 -070093}
94
95static void init_sgl_element(struct scu_sgl_element *e, struct scatterlist *sg)
96{
97 e->length = sg_dma_len(sg);
98 e->address_upper = upper_32_bits(sg_dma_address(sg));
99 e->address_lower = lower_32_bits(sg_dma_address(sg));
100 e->address_modifier = 0;
101}
102
Dan Williams5076a1a2011-06-27 14:57:03 -0700103static void scic_sds_request_build_sgl(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700104{
Dan Williams5076a1a2011-06-27 14:57:03 -0700105 struct isci_host *isci_host = ireq->isci_host;
Dan Williams312e0c22011-06-28 13:47:09 -0700106 struct scic_sds_controller *scic = &isci_host->sci;
Dan Williams5076a1a2011-06-27 14:57:03 -0700107 struct sas_task *task = isci_request_access_task(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700108 struct scatterlist *sg = NULL;
109 dma_addr_t dma_addr;
110 u32 sg_idx = 0;
111 struct scu_sgl_element_pair *scu_sg = NULL;
112 struct scu_sgl_element_pair *prev_sg = NULL;
113
114 if (task->num_scatter > 0) {
115 sg = task->scatter;
116
117 while (sg) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700118 scu_sg = to_sgl_element_pair(ireq, sg_idx);
Dan Williams312e0c22011-06-28 13:47:09 -0700119 init_sgl_element(&scu_sg->A, sg);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700120 sg = sg_next(sg);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700121 if (sg) {
Dan Williams312e0c22011-06-28 13:47:09 -0700122 init_sgl_element(&scu_sg->B, sg);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700123 sg = sg_next(sg);
124 } else
Dan Williams312e0c22011-06-28 13:47:09 -0700125 memset(&scu_sg->B, 0, sizeof(scu_sg->B));
Dan Williamsf1f52e72011-05-10 02:28:45 -0700126
127 if (prev_sg) {
Dan Williams312e0c22011-06-28 13:47:09 -0700128 dma_addr = to_sgl_element_pair_dma(scic,
Dan Williams5076a1a2011-06-27 14:57:03 -0700129 ireq,
Dan Williams312e0c22011-06-28 13:47:09 -0700130 sg_idx);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700131
132 prev_sg->next_pair_upper =
133 upper_32_bits(dma_addr);
134 prev_sg->next_pair_lower =
135 lower_32_bits(dma_addr);
136 }
137
138 prev_sg = scu_sg;
139 sg_idx++;
140 }
141 } else { /* handle when no sg */
Dan Williams5076a1a2011-06-27 14:57:03 -0700142 scu_sg = to_sgl_element_pair(ireq, sg_idx);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700143
144 dma_addr = dma_map_single(&isci_host->pdev->dev,
145 task->scatter,
146 task->total_xfer_len,
147 task->data_dir);
148
Dan Williams5076a1a2011-06-27 14:57:03 -0700149 ireq->zero_scatter_daddr = dma_addr;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700150
151 scu_sg->A.length = task->total_xfer_len;
152 scu_sg->A.address_upper = upper_32_bits(dma_addr);
153 scu_sg->A.address_lower = lower_32_bits(dma_addr);
154 }
155
156 if (scu_sg) {
157 scu_sg->next_pair_upper = 0;
158 scu_sg->next_pair_lower = 0;
159 }
160}
161
Dan Williams5076a1a2011-06-27 14:57:03 -0700162static void scic_sds_io_request_build_ssp_command_iu(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700163{
164 struct ssp_cmd_iu *cmd_iu;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700165 struct sas_task *task = isci_request_access_task(ireq);
166
Dan Williams5076a1a2011-06-27 14:57:03 -0700167 cmd_iu = &ireq->ssp.cmd;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700168
169 memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
170 cmd_iu->add_cdb_len = 0;
171 cmd_iu->_r_a = 0;
172 cmd_iu->_r_b = 0;
173 cmd_iu->en_fburst = 0; /* unsupported */
174 cmd_iu->task_prio = task->ssp_task.task_prio;
175 cmd_iu->task_attr = task->ssp_task.task_attr;
176 cmd_iu->_r_c = 0;
177
178 sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cdb,
179 sizeof(task->ssp_task.cdb) / sizeof(u32));
180}
181
Dan Williams5076a1a2011-06-27 14:57:03 -0700182static void scic_sds_task_request_build_ssp_task_iu(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700183{
184 struct ssp_task_iu *task_iu;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700185 struct sas_task *task = isci_request_access_task(ireq);
186 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
187
Dan Williams5076a1a2011-06-27 14:57:03 -0700188 task_iu = &ireq->ssp.tmf;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700189
190 memset(task_iu, 0, sizeof(struct ssp_task_iu));
191
192 memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
193
194 task_iu->task_func = isci_tmf->tmf_code;
195 task_iu->task_tag =
196 (ireq->ttype == tmf_task) ?
197 isci_tmf->io_tag :
198 SCI_CONTROLLER_INVALID_IO_TAG;
199}
200
201/**
202 * This method is will fill in the SCU Task Context for any type of SSP request.
203 * @sci_req:
204 * @task_context:
205 *
206 */
207static void scu_ssp_reqeust_construct_task_context(
Dan Williams5076a1a2011-06-27 14:57:03 -0700208 struct isci_request *ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700209 struct scu_task_context *task_context)
210{
211 dma_addr_t dma_addr;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700212 struct scic_sds_remote_device *target_device;
213 struct scic_sds_port *target_port;
214
Dan Williams5076a1a2011-06-27 14:57:03 -0700215 target_device = scic_sds_request_get_device(ireq);
216 target_port = scic_sds_request_get_port(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700217
218 /* Fill in the TC with the its required data */
219 task_context->abort = 0;
220 task_context->priority = 0;
221 task_context->initiator_request = 1;
222 task_context->connection_rate = target_device->connection_rate;
223 task_context->protocol_engine_index =
224 scic_sds_controller_get_protocol_engine_group(controller);
225 task_context->logical_port_index =
226 scic_sds_port_get_index(target_port);
227 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
228 task_context->valid = SCU_TASK_CONTEXT_VALID;
229 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
230
231 task_context->remote_node_index =
Dan Williams5076a1a2011-06-27 14:57:03 -0700232 scic_sds_remote_device_get_index(ireq->target_device);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700233 task_context->command_code = 0;
234
235 task_context->link_layer_control = 0;
236 task_context->do_not_dma_ssp_good_response = 1;
237 task_context->strict_ordering = 0;
238 task_context->control_frame = 0;
239 task_context->timeout_enable = 0;
240 task_context->block_guard_enable = 0;
241
242 task_context->address_modifier = 0;
243
Dan Williams5076a1a2011-06-27 14:57:03 -0700244 /* task_context->type.ssp.tag = ireq->io_tag; */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700245 task_context->task_phase = 0x01;
246
Dan Williams5076a1a2011-06-27 14:57:03 -0700247 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williams312e0c22011-06-28 13:47:09 -0700248 (scic_sds_controller_get_protocol_engine_group(controller) <<
249 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
250 (scic_sds_port_get_index(target_port) <<
251 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
Dan Williams5076a1a2011-06-27 14:57:03 -0700252 ISCI_TAG_TCI(ireq->io_tag));
Dan Williamsf1f52e72011-05-10 02:28:45 -0700253
254 /*
255 * Copy the physical address for the command buffer to the
256 * SCU Task Context
257 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700258 dma_addr = scic_io_request_get_dma_addr(ireq, &ireq->ssp.cmd);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700259
260 task_context->command_iu_upper = upper_32_bits(dma_addr);
261 task_context->command_iu_lower = lower_32_bits(dma_addr);
262
263 /*
264 * Copy the physical address for the response buffer to the
265 * SCU Task Context
266 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700267 dma_addr = scic_io_request_get_dma_addr(ireq, &ireq->ssp.rsp);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700268
269 task_context->response_iu_upper = upper_32_bits(dma_addr);
270 task_context->response_iu_lower = lower_32_bits(dma_addr);
271}
272
273/**
274 * This method is will fill in the SCU Task Context for a SSP IO request.
275 * @sci_req:
276 *
277 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700278static void scu_ssp_io_request_construct_task_context(struct isci_request *ireq,
Dan Williams312e0c22011-06-28 13:47:09 -0700279 enum dma_data_direction dir,
280 u32 len)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700281{
Dan Williams5076a1a2011-06-27 14:57:03 -0700282 struct scu_task_context *task_context = ireq->tc;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700283
Dan Williams5076a1a2011-06-27 14:57:03 -0700284 scu_ssp_reqeust_construct_task_context(ireq, task_context);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700285
286 task_context->ssp_command_iu_length =
287 sizeof(struct ssp_cmd_iu) / sizeof(u32);
288 task_context->type.ssp.frame_type = SSP_COMMAND;
289
290 switch (dir) {
291 case DMA_FROM_DEVICE:
292 case DMA_NONE:
293 default:
294 task_context->task_type = SCU_TASK_TYPE_IOREAD;
295 break;
296 case DMA_TO_DEVICE:
297 task_context->task_type = SCU_TASK_TYPE_IOWRITE;
298 break;
299 }
300
301 task_context->transfer_length_bytes = len;
302
303 if (task_context->transfer_length_bytes > 0)
Dan Williams5076a1a2011-06-27 14:57:03 -0700304 scic_sds_request_build_sgl(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700305}
306
Dan Williamsf1f52e72011-05-10 02:28:45 -0700307/**
308 * This method will fill in the SCU Task Context for a SSP Task request. The
309 * following important settings are utilized: -# priority ==
310 * SCU_TASK_PRIORITY_HIGH. This ensures that the task request is issued
311 * ahead of other task destined for the same Remote Node. -# task_type ==
312 * SCU_TASK_TYPE_IOREAD. This simply indicates that a normal request type
313 * (i.e. non-raw frame) is being utilized to perform task management. -#
314 * control_frame == 1. This ensures that the proper endianess is set so
315 * that the bytes are transmitted in the right order for a task frame.
316 * @sci_req: This parameter specifies the task request object being
317 * constructed.
318 *
319 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700320static void scu_ssp_task_request_construct_task_context(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700321{
Dan Williams5076a1a2011-06-27 14:57:03 -0700322 struct scu_task_context *task_context = ireq->tc;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700323
Dan Williams5076a1a2011-06-27 14:57:03 -0700324 scu_ssp_reqeust_construct_task_context(ireq, task_context);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700325
326 task_context->control_frame = 1;
327 task_context->priority = SCU_TASK_PRIORITY_HIGH;
328 task_context->task_type = SCU_TASK_TYPE_RAW_FRAME;
329 task_context->transfer_length_bytes = 0;
330 task_context->type.ssp.frame_type = SSP_TASK;
331 task_context->ssp_command_iu_length =
332 sizeof(struct ssp_task_iu) / sizeof(u32);
333}
334
Dan Williamsf1f52e72011-05-10 02:28:45 -0700335/**
Dan Williams5dec6f42011-05-10 02:28:49 -0700336 * This method is will fill in the SCU Task Context for any type of SATA
337 * request. This is called from the various SATA constructors.
338 * @sci_req: The general IO request object which is to be used in
339 * constructing the SCU task context.
340 * @task_context: The buffer pointer for the SCU task context which is being
341 * constructed.
Dan Williamsf1f52e72011-05-10 02:28:45 -0700342 *
Dan Williams5dec6f42011-05-10 02:28:49 -0700343 * The general io request construction is complete. The buffer assignment for
344 * the command buffer is complete. none Revisit task context construction to
345 * determine what is common for SSP/SMP/STP task context structures.
Dan Williamsf1f52e72011-05-10 02:28:45 -0700346 */
Dan Williams5dec6f42011-05-10 02:28:49 -0700347static void scu_sata_reqeust_construct_task_context(
Dan Williams5076a1a2011-06-27 14:57:03 -0700348 struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -0700349 struct scu_task_context *task_context)
350{
351 dma_addr_t dma_addr;
Dan Williams5dec6f42011-05-10 02:28:49 -0700352 struct scic_sds_remote_device *target_device;
353 struct scic_sds_port *target_port;
354
Dan Williams5076a1a2011-06-27 14:57:03 -0700355 target_device = scic_sds_request_get_device(ireq);
356 target_port = scic_sds_request_get_port(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700357
358 /* Fill in the TC with the its required data */
359 task_context->abort = 0;
360 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
361 task_context->initiator_request = 1;
362 task_context->connection_rate = target_device->connection_rate;
363 task_context->protocol_engine_index =
364 scic_sds_controller_get_protocol_engine_group(controller);
365 task_context->logical_port_index =
366 scic_sds_port_get_index(target_port);
367 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
368 task_context->valid = SCU_TASK_CONTEXT_VALID;
369 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
370
371 task_context->remote_node_index =
Dan Williams5076a1a2011-06-27 14:57:03 -0700372 scic_sds_remote_device_get_index(ireq->target_device);
Dan Williams5dec6f42011-05-10 02:28:49 -0700373 task_context->command_code = 0;
374
375 task_context->link_layer_control = 0;
376 task_context->do_not_dma_ssp_good_response = 1;
377 task_context->strict_ordering = 0;
378 task_context->control_frame = 0;
379 task_context->timeout_enable = 0;
380 task_context->block_guard_enable = 0;
381
382 task_context->address_modifier = 0;
383 task_context->task_phase = 0x01;
384
385 task_context->ssp_command_iu_length =
386 (sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);
387
388 /* Set the first word of the H2D REG FIS */
Dan Williams5076a1a2011-06-27 14:57:03 -0700389 task_context->type.words[0] = *(u32 *)&ireq->stp.cmd;
Dan Williams5dec6f42011-05-10 02:28:49 -0700390
Dan Williams5076a1a2011-06-27 14:57:03 -0700391 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williams312e0c22011-06-28 13:47:09 -0700392 (scic_sds_controller_get_protocol_engine_group(controller) <<
393 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
394 (scic_sds_port_get_index(target_port) <<
395 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
Dan Williams5076a1a2011-06-27 14:57:03 -0700396 ISCI_TAG_TCI(ireq->io_tag));
Dan Williams5dec6f42011-05-10 02:28:49 -0700397 /*
398 * Copy the physical address for the command buffer to the SCU Task
399 * Context. We must offset the command buffer by 4 bytes because the
400 * first 4 bytes are transfered in the body of the TC.
401 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700402 dma_addr = scic_io_request_get_dma_addr(ireq,
403 ((char *) &ireq->stp.cmd) +
Dan Williams5dec6f42011-05-10 02:28:49 -0700404 sizeof(u32));
405
406 task_context->command_iu_upper = upper_32_bits(dma_addr);
407 task_context->command_iu_lower = lower_32_bits(dma_addr);
408
409 /* SATA Requests do not have a response buffer */
410 task_context->response_iu_upper = 0;
411 task_context->response_iu_lower = 0;
412}
413
Dan Williams5076a1a2011-06-27 14:57:03 -0700414static void scu_stp_raw_request_construct_task_context(struct isci_request *ireq)
Dan Williams5dec6f42011-05-10 02:28:49 -0700415{
Dan Williams5076a1a2011-06-27 14:57:03 -0700416 struct scu_task_context *task_context = ireq->tc;
Dan Williams5dec6f42011-05-10 02:28:49 -0700417
Dan Williams5076a1a2011-06-27 14:57:03 -0700418 scu_sata_reqeust_construct_task_context(ireq, task_context);
Dan Williams5dec6f42011-05-10 02:28:49 -0700419
420 task_context->control_frame = 0;
421 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
422 task_context->task_type = SCU_TASK_TYPE_SATA_RAW_FRAME;
423 task_context->type.stp.fis_type = FIS_REGH2D;
424 task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
425}
426
Dan Williams5076a1a2011-06-27 14:57:03 -0700427static enum sci_status scic_sds_stp_pio_request_construct(struct isci_request *ireq,
428 bool copy_rx_frame)
Dan Williams5dec6f42011-05-10 02:28:49 -0700429{
Dan Williams5076a1a2011-06-27 14:57:03 -0700430 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williams5dec6f42011-05-10 02:28:49 -0700431
Dan Williams5076a1a2011-06-27 14:57:03 -0700432 scu_stp_raw_request_construct_task_context(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700433
Dan Williamsba7cb222011-06-27 11:56:41 -0700434 stp_req->status = 0;
435 stp_req->sgl.offset = 0;
436 stp_req->sgl.set = SCU_SGL_ELEMENT_PAIR_A;
Dan Williams5dec6f42011-05-10 02:28:49 -0700437
438 if (copy_rx_frame) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700439 scic_sds_request_build_sgl(ireq);
Dan Williamsba7cb222011-06-27 11:56:41 -0700440 stp_req->sgl.index = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -0700441 } else {
442 /* The user does not want the data copied to the SGL buffer location */
Dan Williamsba7cb222011-06-27 11:56:41 -0700443 stp_req->sgl.index = -1;
Dan Williams5dec6f42011-05-10 02:28:49 -0700444 }
445
446 return SCI_SUCCESS;
447}
448
449/**
450 *
451 * @sci_req: This parameter specifies the request to be constructed as an
452 * optimized request.
453 * @optimized_task_type: This parameter specifies whether the request is to be
454 * an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
455 * value of 1 indicates NCQ.
456 *
457 * This method will perform request construction common to all types of STP
458 * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
459 * returns an indication as to whether the construction was successful.
460 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700461static void scic_sds_stp_optimized_request_construct(struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -0700462 u8 optimized_task_type,
463 u32 len,
464 enum dma_data_direction dir)
465{
Dan Williams5076a1a2011-06-27 14:57:03 -0700466 struct scu_task_context *task_context = ireq->tc;
Dan Williams5dec6f42011-05-10 02:28:49 -0700467
468 /* Build the STP task context structure */
Dan Williams5076a1a2011-06-27 14:57:03 -0700469 scu_sata_reqeust_construct_task_context(ireq, task_context);
Dan Williams5dec6f42011-05-10 02:28:49 -0700470
471 /* Copy over the SGL elements */
Dan Williams5076a1a2011-06-27 14:57:03 -0700472 scic_sds_request_build_sgl(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700473
474 /* Copy over the number of bytes to be transfered */
475 task_context->transfer_length_bytes = len;
476
477 if (dir == DMA_TO_DEVICE) {
478 /*
479 * The difference between the DMA IN and DMA OUT request task type
480 * values are consistent with the difference between FPDMA READ
481 * and FPDMA WRITE values. Add the supplied task type parameter
482 * to this difference to set the task type properly for this
483 * DATA OUT (WRITE) case. */
484 task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
485 - SCU_TASK_TYPE_DMA_IN);
486 } else {
487 /*
488 * For the DATA IN (READ) case, simply save the supplied
489 * optimized task type. */
490 task_context->task_type = optimized_task_type;
491 }
492}
493
494
495
Dan Williamsf1f52e72011-05-10 02:28:45 -0700496static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -0700497scic_io_request_construct_sata(struct isci_request *ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700498 u32 len,
499 enum dma_data_direction dir,
500 bool copy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700501{
Dan Williams6f231dd2011-07-02 22:56:22 -0700502 enum sci_status status = SCI_SUCCESS;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700503 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700504
Dan Williamsf1f52e72011-05-10 02:28:45 -0700505 /* check for management protocols */
506 if (ireq->ttype == tmf_task) {
507 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700508
Dan Williamsf1f52e72011-05-10 02:28:45 -0700509 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
Dan Williams5dec6f42011-05-10 02:28:49 -0700510 tmf->tmf_code == isci_tmf_sata_srst_low) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700511 scu_stp_raw_request_construct_task_context(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700512 return SCI_SUCCESS;
513 } else {
Dan Williams5076a1a2011-06-27 14:57:03 -0700514 dev_err(scic_to_dev(ireq->owning_controller),
Dan Williamsf1f52e72011-05-10 02:28:45 -0700515 "%s: Request 0x%p received un-handled SAT "
516 "management protocol 0x%x.\n",
Dan Williams5076a1a2011-06-27 14:57:03 -0700517 __func__, ireq, tmf->tmf_code);
Dan Williams6f231dd2011-07-02 22:56:22 -0700518
Dan Williamsf1f52e72011-05-10 02:28:45 -0700519 return SCI_FAILURE;
520 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700521 }
522
Dan Williamsf1f52e72011-05-10 02:28:45 -0700523 if (!sas_protocol_ata(task->task_proto)) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700524 dev_err(scic_to_dev(ireq->owning_controller),
Dan Williamsf1f52e72011-05-10 02:28:45 -0700525 "%s: Non-ATA protocol in SATA path: 0x%x\n",
526 __func__,
527 task->task_proto);
Dan Williams6f231dd2011-07-02 22:56:22 -0700528 return SCI_FAILURE;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700529
Dan Williams6f231dd2011-07-02 22:56:22 -0700530 }
531
Dan Williamsf1f52e72011-05-10 02:28:45 -0700532 /* non data */
Dan Williams5dec6f42011-05-10 02:28:49 -0700533 if (task->data_dir == DMA_NONE) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700534 scu_stp_raw_request_construct_task_context(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700535 return SCI_SUCCESS;
536 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700537
538 /* NCQ */
Dan Williams5dec6f42011-05-10 02:28:49 -0700539 if (task->ata_task.use_ncq) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700540 scic_sds_stp_optimized_request_construct(ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -0700541 SCU_TASK_TYPE_FPDMAQ_READ,
542 len, dir);
543 return SCI_SUCCESS;
544 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700545
546 /* DMA */
Dan Williams5dec6f42011-05-10 02:28:49 -0700547 if (task->ata_task.dma_xfer) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700548 scic_sds_stp_optimized_request_construct(ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -0700549 SCU_TASK_TYPE_DMA_IN,
550 len, dir);
551 return SCI_SUCCESS;
552 } else /* PIO */
Dan Williams5076a1a2011-06-27 14:57:03 -0700553 return scic_sds_stp_pio_request_construct(ireq, copy);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700554
555 return status;
556}
557
Dan Williams5076a1a2011-06-27 14:57:03 -0700558static enum sci_status scic_io_request_construct_basic_ssp(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700559{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700560 struct sas_task *task = isci_request_access_task(ireq);
561
Dan Williams5076a1a2011-06-27 14:57:03 -0700562 ireq->protocol = SCIC_SSP_PROTOCOL;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700563
Dan Williams5076a1a2011-06-27 14:57:03 -0700564 scu_ssp_io_request_construct_task_context(ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700565 task->data_dir,
566 task->total_xfer_len);
567
Dan Williams5076a1a2011-06-27 14:57:03 -0700568 scic_sds_io_request_build_ssp_command_iu(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700569
Dan Williams5076a1a2011-06-27 14:57:03 -0700570 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700571
572 return SCI_SUCCESS;
573}
574
575enum sci_status scic_task_request_construct_ssp(
Dan Williams5076a1a2011-06-27 14:57:03 -0700576 struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700577{
578 /* Construct the SSP Task SCU Task Context */
Dan Williams5076a1a2011-06-27 14:57:03 -0700579 scu_ssp_task_request_construct_task_context(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700580
581 /* Fill in the SSP Task IU */
Dan Williams5076a1a2011-06-27 14:57:03 -0700582 scic_sds_task_request_build_ssp_task_iu(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700583
Dan Williams5076a1a2011-06-27 14:57:03 -0700584 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williams6f231dd2011-07-02 22:56:22 -0700585
586 return SCI_SUCCESS;
587}
588
Dan Williams5076a1a2011-06-27 14:57:03 -0700589static enum sci_status scic_io_request_construct_basic_sata(struct isci_request *ireq)
Dan Williams6f231dd2011-07-02 22:56:22 -0700590{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700591 enum sci_status status;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700592 bool copy = false;
Dan Williams5076a1a2011-06-27 14:57:03 -0700593 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700594
Dan Williams5076a1a2011-06-27 14:57:03 -0700595 ireq->protocol = SCIC_STP_PROTOCOL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700596
Dan Williamsf1f52e72011-05-10 02:28:45 -0700597 copy = (task->data_dir == DMA_NONE) ? false : true;
Dan Williams6f231dd2011-07-02 22:56:22 -0700598
Dan Williams5076a1a2011-06-27 14:57:03 -0700599 status = scic_io_request_construct_sata(ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700600 task->total_xfer_len,
601 task->data_dir,
602 copy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700603
Dan Williamsf1f52e72011-05-10 02:28:45 -0700604 if (status == SCI_SUCCESS)
Dan Williams5076a1a2011-06-27 14:57:03 -0700605 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williams6f231dd2011-07-02 22:56:22 -0700606
Dan Williamsf1f52e72011-05-10 02:28:45 -0700607 return status;
Dan Williams6f231dd2011-07-02 22:56:22 -0700608}
609
Dan Williams5076a1a2011-06-27 14:57:03 -0700610enum sci_status scic_task_request_construct_sata(struct isci_request *ireq)
Dan Williams6f231dd2011-07-02 22:56:22 -0700611{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700612 enum sci_status status = SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700613
Dan Williamsf1f52e72011-05-10 02:28:45 -0700614 /* check for management protocols */
615 if (ireq->ttype == tmf_task) {
616 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700617
Dan Williamsf1f52e72011-05-10 02:28:45 -0700618 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
619 tmf->tmf_code == isci_tmf_sata_srst_low) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700620 scu_stp_raw_request_construct_task_context(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700621 } else {
Dan Williams5076a1a2011-06-27 14:57:03 -0700622 dev_err(scic_to_dev(ireq->owning_controller),
Dan Williamsf1f52e72011-05-10 02:28:45 -0700623 "%s: Request 0x%p received un-handled SAT "
624 "Protocol 0x%x.\n",
Dan Williams5076a1a2011-06-27 14:57:03 -0700625 __func__, ireq, tmf->tmf_code);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700626
627 return SCI_FAILURE;
628 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700629 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700630
Dan Williams5dec6f42011-05-10 02:28:49 -0700631 if (status != SCI_SUCCESS)
632 return status;
Dan Williams5076a1a2011-06-27 14:57:03 -0700633 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700634
635 return status;
Dan Williams6f231dd2011-07-02 22:56:22 -0700636}
637
638/**
Dan Williamsf1f52e72011-05-10 02:28:45 -0700639 * sci_req_tx_bytes - bytes transferred when reply underruns request
640 * @sci_req: request that was terminated early
Dan Williams6f231dd2011-07-02 22:56:22 -0700641 */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700642#define SCU_TASK_CONTEXT_SRAM 0x200000
Dan Williams5076a1a2011-06-27 14:57:03 -0700643static u32 sci_req_tx_bytes(struct isci_request *ireq)
Dan Williams6f231dd2011-07-02 22:56:22 -0700644{
Dan Williams5076a1a2011-06-27 14:57:03 -0700645 struct scic_sds_controller *scic = ireq->owning_controller;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700646 u32 ret_val = 0;
Dan Williams6f231dd2011-07-02 22:56:22 -0700647
Dan Williamsf1f52e72011-05-10 02:28:45 -0700648 if (readl(&scic->smu_registers->address_modifier) == 0) {
649 void __iomem *scu_reg_base = scic->scu_registers;
Dan Williams6f231dd2011-07-02 22:56:22 -0700650
Dan Williamsf1f52e72011-05-10 02:28:45 -0700651 /* get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
652 * BAR1 is the scu_registers
653 * 0x20002C = 0x200000 + 0x2c
654 * = start of task context SRAM + offset of (type.ssp.data_offset)
655 * TCi is the io_tag of struct scic_sds_request
Dan Williams67ea8382011-05-08 11:47:15 -0700656 */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700657 ret_val = readl(scu_reg_base +
658 (SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
Dan Williams5076a1a2011-06-27 14:57:03 -0700659 ((sizeof(struct scu_task_context)) * ISCI_TAG_TCI(ireq->io_tag)));
Dan Williams67ea8382011-05-08 11:47:15 -0700660 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700661
Dan Williamsf1f52e72011-05-10 02:28:45 -0700662 return ret_val;
Dan Williams6f231dd2011-07-02 22:56:22 -0700663}
664
Dan Williams5076a1a2011-06-27 14:57:03 -0700665enum sci_status scic_sds_request_start(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700666{
Piotr Sawickif4636a72011-05-10 23:50:32 +0000667 enum sci_base_request_states state;
Dan Williams5076a1a2011-06-27 14:57:03 -0700668 struct scu_task_context *tc = ireq->tc;
669 struct scic_sds_controller *scic = ireq->owning_controller;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000670
Dan Williams5076a1a2011-06-27 14:57:03 -0700671 state = ireq->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000672 if (state != SCI_REQ_CONSTRUCTED) {
Piotr Sawickif4636a72011-05-10 23:50:32 +0000673 dev_warn(scic_to_dev(scic),
674 "%s: SCIC IO Request requested to start while in wrong "
675 "state %d\n", __func__, state);
676 return SCI_FAILURE_INVALID_STATE;
677 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700678
Dan Williams5076a1a2011-06-27 14:57:03 -0700679 tc->task_index = ISCI_TAG_TCI(ireq->io_tag);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700680
Dan Williams312e0c22011-06-28 13:47:09 -0700681 switch (tc->protocol_type) {
682 case SCU_TASK_CONTEXT_PROTOCOL_SMP:
683 case SCU_TASK_CONTEXT_PROTOCOL_SSP:
684 /* SSP/SMP Frame */
Dan Williams5076a1a2011-06-27 14:57:03 -0700685 tc->type.ssp.tag = ireq->io_tag;
Dan Williams312e0c22011-06-28 13:47:09 -0700686 tc->type.ssp.target_port_transfer_tag = 0xFFFF;
687 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000688
Dan Williams312e0c22011-06-28 13:47:09 -0700689 case SCU_TASK_CONTEXT_PROTOCOL_STP:
690 /* STP/SATA Frame
Dan Williams5076a1a2011-06-27 14:57:03 -0700691 * tc->type.stp.ncq_tag = ireq->ncq_tag;
Dan Williams312e0c22011-06-28 13:47:09 -0700692 */
693 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000694
Dan Williams312e0c22011-06-28 13:47:09 -0700695 case SCU_TASK_CONTEXT_PROTOCOL_NONE:
696 /* / @todo When do we set no protocol type? */
697 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000698
Dan Williams312e0c22011-06-28 13:47:09 -0700699 default:
700 /* This should never happen since we build the IO
701 * requests */
702 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000703 }
704
Dan Williams312e0c22011-06-28 13:47:09 -0700705 /* Add to the post_context the io tag value */
Dan Williams5076a1a2011-06-27 14:57:03 -0700706 ireq->post_context |= ISCI_TAG_TCI(ireq->io_tag);
Dan Williams312e0c22011-06-28 13:47:09 -0700707
708 /* Everything is good go ahead and change state */
Dan Williams5076a1a2011-06-27 14:57:03 -0700709 sci_change_state(&ireq->sm, SCI_REQ_STARTED);
Dan Williams312e0c22011-06-28 13:47:09 -0700710
711 return SCI_SUCCESS;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700712}
713
714enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -0700715scic_sds_io_request_terminate(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700716{
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700717 enum sci_base_request_states state;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700718
Dan Williams5076a1a2011-06-27 14:57:03 -0700719 state = ireq->sm.current_state_id;
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700720
721 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000722 case SCI_REQ_CONSTRUCTED:
Dan Williams5076a1a2011-06-27 14:57:03 -0700723 scic_sds_request_set_status(ireq,
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700724 SCU_TASK_DONE_TASK_ABORT,
725 SCI_FAILURE_IO_TERMINATED);
726
Dan Williams5076a1a2011-06-27 14:57:03 -0700727 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700728 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000729 case SCI_REQ_STARTED:
730 case SCI_REQ_TASK_WAIT_TC_COMP:
731 case SCI_REQ_SMP_WAIT_RESP:
732 case SCI_REQ_SMP_WAIT_TC_COMP:
733 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
734 case SCI_REQ_STP_UDMA_WAIT_D2H:
735 case SCI_REQ_STP_NON_DATA_WAIT_H2D:
736 case SCI_REQ_STP_NON_DATA_WAIT_D2H:
737 case SCI_REQ_STP_PIO_WAIT_H2D:
738 case SCI_REQ_STP_PIO_WAIT_FRAME:
739 case SCI_REQ_STP_PIO_DATA_IN:
740 case SCI_REQ_STP_PIO_DATA_OUT:
741 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
742 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
743 case SCI_REQ_STP_SOFT_RESET_WAIT_D2H:
Dan Williams5076a1a2011-06-27 14:57:03 -0700744 sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700745 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000746 case SCI_REQ_TASK_WAIT_TC_RESP:
Dan Williams5076a1a2011-06-27 14:57:03 -0700747 sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
748 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700749 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000750 case SCI_REQ_ABORTING:
Dan Williams5076a1a2011-06-27 14:57:03 -0700751 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700752 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000753 case SCI_REQ_COMPLETED:
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700754 default:
Dan Williams5076a1a2011-06-27 14:57:03 -0700755 dev_warn(scic_to_dev(ireq->owning_controller),
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700756 "%s: SCIC IO Request requested to abort while in wrong "
757 "state %d\n",
758 __func__,
Dan Williams5076a1a2011-06-27 14:57:03 -0700759 ireq->sm.current_state_id);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700760 break;
761 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700762
763 return SCI_FAILURE_INVALID_STATE;
764}
765
Dan Williams5076a1a2011-06-27 14:57:03 -0700766enum sci_status scic_sds_request_complete(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700767{
Dan Williams79e2b6b2011-05-11 08:29:56 -0700768 enum sci_base_request_states state;
Dan Williams5076a1a2011-06-27 14:57:03 -0700769 struct scic_sds_controller *scic = ireq->owning_controller;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700770
Dan Williams5076a1a2011-06-27 14:57:03 -0700771 state = ireq->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000772 if (WARN_ONCE(state != SCI_REQ_COMPLETED,
Dan Williams79e2b6b2011-05-11 08:29:56 -0700773 "isci: request completion from wrong state (%d)\n", state))
774 return SCI_FAILURE_INVALID_STATE;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700775
Dan Williams5076a1a2011-06-27 14:57:03 -0700776 if (ireq->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX)
Dan Williams79e2b6b2011-05-11 08:29:56 -0700777 scic_sds_controller_release_frame(scic,
Dan Williams5076a1a2011-06-27 14:57:03 -0700778 ireq->saved_rx_frame_index);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700779
780 /* XXX can we just stop the machine and remove the 'final' state? */
Dan Williams5076a1a2011-06-27 14:57:03 -0700781 sci_change_state(&ireq->sm, SCI_REQ_FINAL);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700782 return SCI_SUCCESS;
783}
784
Dan Williams5076a1a2011-06-27 14:57:03 -0700785enum sci_status scic_sds_io_request_event_handler(struct isci_request *ireq,
Dan Williams79e2b6b2011-05-11 08:29:56 -0700786 u32 event_code)
787{
788 enum sci_base_request_states state;
Dan Williams5076a1a2011-06-27 14:57:03 -0700789 struct scic_sds_controller *scic = ireq->owning_controller;
Dan Williams79e2b6b2011-05-11 08:29:56 -0700790
Dan Williams5076a1a2011-06-27 14:57:03 -0700791 state = ireq->sm.current_state_id;
Dan Williams79e2b6b2011-05-11 08:29:56 -0700792
Edmund Nadolskie3013702011-06-02 00:10:43 +0000793 if (state != SCI_REQ_STP_PIO_DATA_IN) {
Dan Williams79e2b6b2011-05-11 08:29:56 -0700794 dev_warn(scic_to_dev(scic), "%s: (%x) in wrong state %d\n",
795 __func__, event_code, state);
796
797 return SCI_FAILURE_INVALID_STATE;
798 }
799
800 switch (scu_get_event_specifier(event_code)) {
801 case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
802 /* We are waiting for data and the SCU has R_ERR the data frame.
803 * Go back to waiting for the D2H Register FIS
804 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700805 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700806 return SCI_SUCCESS;
807 default:
808 dev_err(scic_to_dev(scic),
809 "%s: pio request unexpected event %#x\n",
810 __func__, event_code);
811
812 /* TODO Should we fail the PIO request when we get an
813 * unexpected event?
814 */
815 return SCI_FAILURE;
816 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700817}
818
Dan Williamsf1f52e72011-05-10 02:28:45 -0700819/*
820 * This function copies response data for requests returning response data
821 * instead of sense data.
822 * @sci_req: This parameter specifies the request object for which to copy
823 * the response data.
824 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700825static void scic_sds_io_request_copy_response(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700826{
827 void *resp_buf;
828 u32 len;
829 struct ssp_response_iu *ssp_response;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700830 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
831
Dan Williams5076a1a2011-06-27 14:57:03 -0700832 ssp_response = &ireq->ssp.rsp;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700833
834 resp_buf = &isci_tmf->resp.resp_iu;
835
836 len = min_t(u32,
837 SSP_RESP_IU_MAX_SIZE,
838 be32_to_cpu(ssp_response->response_data_len));
839
840 memcpy(resp_buf, ssp_response->resp_data, len);
841}
842
Edmund Nadolskie3013702011-06-02 00:10:43 +0000843static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -0700844request_started_state_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000845 u32 completion_code)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700846{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700847 struct ssp_response_iu *resp_iu;
Dan Williamsa7e255a2011-05-11 08:27:47 -0700848 u8 datapres;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700849
Dan Williamsa7e255a2011-05-11 08:27:47 -0700850 /* TODO: Any SDMA return code of other than 0 is bad decode 0x003C0000
851 * to determine SDMA status
Dan Williamsf1f52e72011-05-10 02:28:45 -0700852 */
853 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
854 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5076a1a2011-06-27 14:57:03 -0700855 scic_sds_request_set_status(ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700856 SCU_TASK_DONE_GOOD,
857 SCI_SUCCESS);
858 break;
Dan Williamsa7e255a2011-05-11 08:27:47 -0700859 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP): {
860 /* There are times when the SCU hardware will return an early
Dan Williamsf1f52e72011-05-10 02:28:45 -0700861 * response because the io request specified more data than is
862 * returned by the target device (mode pages, inquiry data,
863 * etc.). We must check the response stats to see if this is
864 * truly a failed request or a good request that just got
865 * completed early.
866 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700867 struct ssp_response_iu *resp = &ireq->ssp.rsp;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700868 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
869
Dan Williams5076a1a2011-06-27 14:57:03 -0700870 sci_swab32_cpy(&ireq->ssp.rsp,
871 &ireq->ssp.rsp,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700872 word_cnt);
873
874 if (resp->status == 0) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700875 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -0700876 SCU_TASK_DONE_GOOD,
877 SCI_SUCCESS_IO_DONE_EARLY);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700878 } else {
Dan Williams5076a1a2011-06-27 14:57:03 -0700879 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -0700880 SCU_TASK_DONE_CHECK_RESPONSE,
881 SCI_FAILURE_IO_RESPONSE_VALID);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700882 }
Dan Williamsa7e255a2011-05-11 08:27:47 -0700883 break;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700884 }
Dan Williamsa7e255a2011-05-11 08:27:47 -0700885 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE): {
Dan Williamsf1f52e72011-05-10 02:28:45 -0700886 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
887
Dan Williams5076a1a2011-06-27 14:57:03 -0700888 sci_swab32_cpy(&ireq->ssp.rsp,
889 &ireq->ssp.rsp,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700890 word_cnt);
891
Dan Williams5076a1a2011-06-27 14:57:03 -0700892 scic_sds_request_set_status(ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700893 SCU_TASK_DONE_CHECK_RESPONSE,
894 SCI_FAILURE_IO_RESPONSE_VALID);
895 break;
896 }
897
898 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -0700899 /* TODO With TASK_DONE_RESP_LEN_ERR is the response frame
Dan Williamsf1f52e72011-05-10 02:28:45 -0700900 * guaranteed to be received before this completion status is
901 * posted?
902 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700903 resp_iu = &ireq->ssp.rsp;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700904 datapres = resp_iu->datapres;
905
Dan Williamsa7e255a2011-05-11 08:27:47 -0700906 if (datapres == 1 || datapres == 2) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700907 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -0700908 SCU_TASK_DONE_CHECK_RESPONSE,
909 SCI_FAILURE_IO_RESPONSE_VALID);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700910 } else
Dan Williams5076a1a2011-06-27 14:57:03 -0700911 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -0700912 SCU_TASK_DONE_GOOD,
913 SCI_SUCCESS);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700914 break;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700915 /* only stp device gets suspended. */
916 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
917 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
918 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
919 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
920 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
921 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
922 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
923 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
924 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
925 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
926 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
Dan Williams5076a1a2011-06-27 14:57:03 -0700927 if (ireq->protocol == SCIC_STP_PROTOCOL) {
928 scic_sds_request_set_status(ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700929 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
930 SCU_COMPLETION_TL_STATUS_SHIFT,
931 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
932 } else {
Dan Williams5076a1a2011-06-27 14:57:03 -0700933 scic_sds_request_set_status(ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700934 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
935 SCU_COMPLETION_TL_STATUS_SHIFT,
936 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
937 }
938 break;
939
940 /* both stp/ssp device gets suspended */
941 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
942 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
943 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
944 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
945 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
946 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
947 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
948 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
949 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
950 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
Dan Williams5076a1a2011-06-27 14:57:03 -0700951 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -0700952 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
953 SCU_COMPLETION_TL_STATUS_SHIFT,
954 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700955 break;
956
957 /* neither ssp nor stp gets suspended. */
958 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
959 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
960 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
961 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
962 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
963 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
964 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
965 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
966 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
967 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
968 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
969 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
970 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
971 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
972 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
973 default:
974 scic_sds_request_set_status(
Dan Williams5076a1a2011-06-27 14:57:03 -0700975 ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700976 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
977 SCU_COMPLETION_TL_STATUS_SHIFT,
978 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
979 break;
980 }
981
982 /*
983 * TODO: This is probably wrong for ACK/NAK timeout conditions
984 */
985
986 /* In all cases we will treat this as the completion of the IO req. */
Dan Williams5076a1a2011-06-27 14:57:03 -0700987 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700988 return SCI_SUCCESS;
989}
990
Edmund Nadolskie3013702011-06-02 00:10:43 +0000991static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -0700992request_aborting_state_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000993 u32 completion_code)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700994{
995 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
996 case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
997 case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
Dan Williams5076a1a2011-06-27 14:57:03 -0700998 scic_sds_request_set_status(ireq, SCU_TASK_DONE_TASK_ABORT,
Dan Williamsa7e255a2011-05-11 08:27:47 -0700999 SCI_FAILURE_IO_TERMINATED);
Dan Williamsf1f52e72011-05-10 02:28:45 -07001000
Dan Williams5076a1a2011-06-27 14:57:03 -07001001 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf1f52e72011-05-10 02:28:45 -07001002 break;
1003
1004 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001005 /* Unless we get some strange error wait for the task abort to complete
1006 * TODO: Should there be a state change for this completion?
1007 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07001008 break;
1009 }
1010
1011 return SCI_SUCCESS;
1012}
1013
Dan Williams5076a1a2011-06-27 14:57:03 -07001014static enum sci_status ssp_task_request_await_tc_event(struct isci_request *ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001015 u32 completion_code)
Dan Williamsf1393032011-05-10 02:28:47 -07001016{
1017 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1018 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5076a1a2011-06-27 14:57:03 -07001019 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
Dan Williamsf1393032011-05-10 02:28:47 -07001020 SCI_SUCCESS);
1021
Dan Williams5076a1a2011-06-27 14:57:03 -07001022 sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
Dan Williamsf1393032011-05-10 02:28:47 -07001023 break;
Dan Williamsf1393032011-05-10 02:28:47 -07001024 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001025 /* Currently, the decision is to simply allow the task request
1026 * to timeout if the task IU wasn't received successfully.
1027 * There is a potential for receiving multiple task responses if
1028 * we decide to send the task IU again.
1029 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001030 dev_warn(scic_to_dev(ireq->owning_controller),
Dan Williamsf1393032011-05-10 02:28:47 -07001031 "%s: TaskRequest:0x%p CompletionCode:%x - "
Dan Williams5076a1a2011-06-27 14:57:03 -07001032 "ACK/NAK timeout\n", __func__, ireq,
Dan Williamsf1393032011-05-10 02:28:47 -07001033 completion_code);
1034
Dan Williams5076a1a2011-06-27 14:57:03 -07001035 sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
Dan Williamsf1393032011-05-10 02:28:47 -07001036 break;
Dan Williamsf1393032011-05-10 02:28:47 -07001037 default:
Edmund Nadolskie3013702011-06-02 00:10:43 +00001038 /*
1039 * All other completion status cause the IO to be complete.
1040 * If a NAK was received, then it is up to the user to retry
1041 * the request.
Dan Williamsa7e255a2011-05-11 08:27:47 -07001042 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001043 scic_sds_request_set_status(ireq,
Dan Williamsf1393032011-05-10 02:28:47 -07001044 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
Dan Williamsa7e255a2011-05-11 08:27:47 -07001045 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williamsf1393032011-05-10 02:28:47 -07001046
Dan Williams5076a1a2011-06-27 14:57:03 -07001047 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf1393032011-05-10 02:28:47 -07001048 break;
1049 }
1050
1051 return SCI_SUCCESS;
1052}
1053
Edmund Nadolskie3013702011-06-02 00:10:43 +00001054static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001055smp_request_await_response_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001056 u32 completion_code)
Dan Williamsc72086e2011-05-10 02:28:48 -07001057{
1058 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1059 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001060 /* In the AWAIT RESPONSE state, any TC completion is
1061 * unexpected. but if the TC has success status, we
1062 * complete the IO anyway.
1063 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001064 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
Dan Williams5dec6f42011-05-10 02:28:49 -07001065 SCI_SUCCESS);
Dan Williamsc72086e2011-05-10 02:28:48 -07001066
Dan Williams5076a1a2011-06-27 14:57:03 -07001067 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001068 break;
1069
1070 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1071 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1072 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1073 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001074 /* These status has been seen in a specific LSI
1075 * expander, which sometimes is not able to send smp
1076 * response within 2 ms. This causes our hardware break
1077 * the connection and set TC completion with one of
1078 * these SMP_XXX_XX_ERR status. For these type of error,
1079 * we ask scic user to retry the request.
1080 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001081 scic_sds_request_set_status(ireq, SCU_TASK_DONE_SMP_RESP_TO_ERR,
Dan Williams5dec6f42011-05-10 02:28:49 -07001082 SCI_FAILURE_RETRY_REQUIRED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001083
Dan Williams5076a1a2011-06-27 14:57:03 -07001084 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001085 break;
1086
1087 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001088 /* All other completion status cause the IO to be complete. If a NAK
1089 * was received, then it is up to the user to retry the request
1090 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001091 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001092 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1093 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williamsc72086e2011-05-10 02:28:48 -07001094
Dan Williams5076a1a2011-06-27 14:57:03 -07001095 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001096 break;
1097 }
1098
1099 return SCI_SUCCESS;
1100}
1101
Edmund Nadolskie3013702011-06-02 00:10:43 +00001102static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001103smp_request_await_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001104 u32 completion_code)
Dan Williamsc72086e2011-05-10 02:28:48 -07001105{
1106 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1107 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5076a1a2011-06-27 14:57:03 -07001108 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
Dan Williams5dec6f42011-05-10 02:28:49 -07001109 SCI_SUCCESS);
Dan Williamsc72086e2011-05-10 02:28:48 -07001110
Dan Williams5076a1a2011-06-27 14:57:03 -07001111 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001112 break;
Dan Williamsc72086e2011-05-10 02:28:48 -07001113 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001114 /* All other completion status cause the IO to be
1115 * complete. If a NAK was received, then it is up to
1116 * the user to retry the request.
1117 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001118 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001119 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1120 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williamsc72086e2011-05-10 02:28:48 -07001121
Dan Williams5076a1a2011-06-27 14:57:03 -07001122 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001123 break;
1124 }
1125
1126 return SCI_SUCCESS;
1127}
1128
Dan Williams5076a1a2011-06-27 14:57:03 -07001129void scic_stp_io_request_set_ncq_tag(struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001130 u16 ncq_tag)
1131{
1132 /**
1133 * @note This could be made to return an error to the user if the user
1134 * attempts to set the NCQ tag in the wrong state.
1135 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001136 ireq->tc->type.stp.ncq_tag = ncq_tag;
Dan Williams5dec6f42011-05-10 02:28:49 -07001137}
1138
Dan Williamsba7cb222011-06-27 11:56:41 -07001139static struct scu_sgl_element *pio_sgl_next(struct isci_stp_request *stp_req)
Dan Williams5dec6f42011-05-10 02:28:49 -07001140{
Dan Williams312e0c22011-06-28 13:47:09 -07001141 struct scu_sgl_element *sgl;
1142 struct scu_sgl_element_pair *sgl_pair;
Dan Williams5076a1a2011-06-27 14:57:03 -07001143 struct isci_request *ireq = to_ireq(stp_req);
Dan Williamsba7cb222011-06-27 11:56:41 -07001144 struct isci_stp_pio_sgl *pio_sgl = &stp_req->sgl;
Dan Williams5dec6f42011-05-10 02:28:49 -07001145
Dan Williams5076a1a2011-06-27 14:57:03 -07001146 sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
Dan Williams312e0c22011-06-28 13:47:09 -07001147 if (!sgl_pair)
1148 sgl = NULL;
Dan Williamsba7cb222011-06-27 11:56:41 -07001149 else if (pio_sgl->set == SCU_SGL_ELEMENT_PAIR_A) {
Dan Williams312e0c22011-06-28 13:47:09 -07001150 if (sgl_pair->B.address_lower == 0 &&
1151 sgl_pair->B.address_upper == 0) {
1152 sgl = NULL;
Dan Williams5dec6f42011-05-10 02:28:49 -07001153 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001154 pio_sgl->set = SCU_SGL_ELEMENT_PAIR_B;
Dan Williams312e0c22011-06-28 13:47:09 -07001155 sgl = &sgl_pair->B;
Dan Williams5dec6f42011-05-10 02:28:49 -07001156 }
1157 } else {
Dan Williams312e0c22011-06-28 13:47:09 -07001158 if (sgl_pair->next_pair_lower == 0 &&
1159 sgl_pair->next_pair_upper == 0) {
1160 sgl = NULL;
Dan Williams5dec6f42011-05-10 02:28:49 -07001161 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001162 pio_sgl->index++;
1163 pio_sgl->set = SCU_SGL_ELEMENT_PAIR_A;
Dan Williams5076a1a2011-06-27 14:57:03 -07001164 sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
Dan Williams312e0c22011-06-28 13:47:09 -07001165 sgl = &sgl_pair->A;
Dan Williams5dec6f42011-05-10 02:28:49 -07001166 }
1167 }
1168
Dan Williams312e0c22011-06-28 13:47:09 -07001169 return sgl;
Dan Williams5dec6f42011-05-10 02:28:49 -07001170}
1171
Edmund Nadolskie3013702011-06-02 00:10:43 +00001172static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001173stp_request_non_data_await_h2d_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001174 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001175{
1176 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1177 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5076a1a2011-06-27 14:57:03 -07001178 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001179 SCI_SUCCESS);
Dan Williams5dec6f42011-05-10 02:28:49 -07001180
Dan Williams5076a1a2011-06-27 14:57:03 -07001181 sci_change_state(&ireq->sm, SCI_REQ_STP_NON_DATA_WAIT_D2H);
Dan Williams5dec6f42011-05-10 02:28:49 -07001182 break;
1183
1184 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001185 /* All other completion status cause the IO to be
1186 * complete. If a NAK was received, then it is up to
1187 * the user to retry the request.
1188 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001189 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001190 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1191 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williams5dec6f42011-05-10 02:28:49 -07001192
Dan Williams5076a1a2011-06-27 14:57:03 -07001193 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001194 break;
1195 }
1196
1197 return SCI_SUCCESS;
1198}
1199
Dan Williams5dec6f42011-05-10 02:28:49 -07001200#define SCU_MAX_FRAME_BUFFER_SIZE 0x400 /* 1K is the maximum SCU frame data payload */
1201
1202/* transmit DATA_FIS from (current sgl + offset) for input
1203 * parameter length. current sgl and offset is alreay stored in the IO request
1204 */
1205static enum sci_status scic_sds_stp_request_pio_data_out_trasmit_data_frame(
Dan Williams5076a1a2011-06-27 14:57:03 -07001206 struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001207 u32 length)
1208{
Dan Williams5076a1a2011-06-27 14:57:03 -07001209 struct isci_stp_request *stp_req = &ireq->stp.req;
1210 struct scu_task_context *task_context = ireq->tc;
Dan Williams312e0c22011-06-28 13:47:09 -07001211 struct scu_sgl_element_pair *sgl_pair;
Dan Williams5dec6f42011-05-10 02:28:49 -07001212 struct scu_sgl_element *current_sgl;
1213
1214 /* Recycle the TC and reconstruct it for sending out DATA FIS containing
1215 * for the data from current_sgl+offset for the input length
1216 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001217 sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
Dan Williamsba7cb222011-06-27 11:56:41 -07001218 if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A)
Dan Williams312e0c22011-06-28 13:47:09 -07001219 current_sgl = &sgl_pair->A;
Dan Williams5dec6f42011-05-10 02:28:49 -07001220 else
Dan Williams312e0c22011-06-28 13:47:09 -07001221 current_sgl = &sgl_pair->B;
Dan Williams5dec6f42011-05-10 02:28:49 -07001222
1223 /* update the TC */
1224 task_context->command_iu_upper = current_sgl->address_upper;
1225 task_context->command_iu_lower = current_sgl->address_lower;
1226 task_context->transfer_length_bytes = length;
1227 task_context->type.stp.fis_type = FIS_DATA;
1228
1229 /* send the new TC out. */
Dan Williams5076a1a2011-06-27 14:57:03 -07001230 return scic_controller_continue_io(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -07001231}
1232
Dan Williams5076a1a2011-06-27 14:57:03 -07001233static enum sci_status scic_sds_stp_request_pio_data_out_transmit_data(struct isci_request *ireq)
Dan Williams5dec6f42011-05-10 02:28:49 -07001234{
Dan Williams5076a1a2011-06-27 14:57:03 -07001235 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williams312e0c22011-06-28 13:47:09 -07001236 struct scu_sgl_element_pair *sgl_pair;
Dan Williamsba7cb222011-06-27 11:56:41 -07001237 struct scu_sgl_element *sgl;
1238 enum sci_status status;
1239 u32 offset;
1240 u32 len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001241
Dan Williamsba7cb222011-06-27 11:56:41 -07001242 offset = stp_req->sgl.offset;
Dan Williams5076a1a2011-06-27 14:57:03 -07001243 sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
Dan Williams312e0c22011-06-28 13:47:09 -07001244 if (WARN_ONCE(!sgl_pair, "%s: null sgl element", __func__))
1245 return SCI_FAILURE;
Dan Williams5dec6f42011-05-10 02:28:49 -07001246
Dan Williamsba7cb222011-06-27 11:56:41 -07001247 if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A) {
1248 sgl = &sgl_pair->A;
1249 len = sgl_pair->A.length - offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001250 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001251 sgl = &sgl_pair->B;
1252 len = sgl_pair->B.length - offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001253 }
1254
Dan Williamsba7cb222011-06-27 11:56:41 -07001255 if (stp_req->pio_len == 0)
1256 return SCI_SUCCESS;
Dan Williams5dec6f42011-05-10 02:28:49 -07001257
Dan Williamsba7cb222011-06-27 11:56:41 -07001258 if (stp_req->pio_len >= len) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001259 status = scic_sds_stp_request_pio_data_out_trasmit_data_frame(ireq, len);
Dan Williamsba7cb222011-06-27 11:56:41 -07001260 if (status != SCI_SUCCESS)
1261 return status;
1262 stp_req->pio_len -= len;
Dan Williams5dec6f42011-05-10 02:28:49 -07001263
Dan Williamsba7cb222011-06-27 11:56:41 -07001264 /* update the current sgl, offset and save for future */
1265 sgl = pio_sgl_next(stp_req);
1266 offset = 0;
1267 } else if (stp_req->pio_len < len) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001268 scic_sds_stp_request_pio_data_out_trasmit_data_frame(ireq, stp_req->pio_len);
Dan Williamsba7cb222011-06-27 11:56:41 -07001269
1270 /* Sgl offset will be adjusted and saved for future */
1271 offset += stp_req->pio_len;
1272 sgl->address_lower += stp_req->pio_len;
1273 stp_req->pio_len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001274 }
1275
Dan Williamsba7cb222011-06-27 11:56:41 -07001276 stp_req->sgl.offset = offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001277
1278 return status;
1279}
1280
1281/**
1282 *
1283 * @stp_request: The request that is used for the SGL processing.
1284 * @data_buffer: The buffer of data to be copied.
1285 * @length: The length of the data transfer.
1286 *
1287 * Copy the data from the buffer for the length specified to the IO reqeust SGL
1288 * specified data region. enum sci_status
1289 */
1290static enum sci_status
Dan Williamsba7cb222011-06-27 11:56:41 -07001291scic_sds_stp_request_pio_data_in_copy_data_buffer(struct isci_stp_request *stp_req,
Dan Williams5dec6f42011-05-10 02:28:49 -07001292 u8 *data_buf, u32 len)
1293{
Dan Williams5dec6f42011-05-10 02:28:49 -07001294 struct isci_request *ireq;
1295 u8 *src_addr;
1296 int copy_len;
1297 struct sas_task *task;
1298 struct scatterlist *sg;
1299 void *kaddr;
1300 int total_len = len;
1301
Dan Williams5076a1a2011-06-27 14:57:03 -07001302 ireq = to_ireq(stp_req);
Dan Williams5dec6f42011-05-10 02:28:49 -07001303 task = isci_request_access_task(ireq);
1304 src_addr = data_buf;
1305
1306 if (task->num_scatter > 0) {
1307 sg = task->scatter;
1308
1309 while (total_len > 0) {
1310 struct page *page = sg_page(sg);
1311
1312 copy_len = min_t(int, total_len, sg_dma_len(sg));
1313 kaddr = kmap_atomic(page, KM_IRQ0);
1314 memcpy(kaddr + sg->offset, src_addr, copy_len);
1315 kunmap_atomic(kaddr, KM_IRQ0);
1316 total_len -= copy_len;
1317 src_addr += copy_len;
1318 sg = sg_next(sg);
1319 }
1320 } else {
1321 BUG_ON(task->total_xfer_len < total_len);
1322 memcpy(task->scatter, src_addr, total_len);
1323 }
1324
1325 return SCI_SUCCESS;
1326}
1327
1328/**
1329 *
1330 * @sci_req: The PIO DATA IN request that is to receive the data.
1331 * @data_buffer: The buffer to copy from.
1332 *
1333 * Copy the data buffer to the io request data region. enum sci_status
1334 */
1335static enum sci_status scic_sds_stp_request_pio_data_in_copy_data(
Dan Williamsba7cb222011-06-27 11:56:41 -07001336 struct isci_stp_request *stp_req,
Dan Williams5dec6f42011-05-10 02:28:49 -07001337 u8 *data_buffer)
1338{
1339 enum sci_status status;
1340
1341 /*
1342 * If there is less than 1K remaining in the transfer request
1343 * copy just the data for the transfer */
Dan Williamsba7cb222011-06-27 11:56:41 -07001344 if (stp_req->pio_len < SCU_MAX_FRAME_BUFFER_SIZE) {
Dan Williams5dec6f42011-05-10 02:28:49 -07001345 status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
Dan Williamsba7cb222011-06-27 11:56:41 -07001346 stp_req, data_buffer, stp_req->pio_len);
Dan Williams5dec6f42011-05-10 02:28:49 -07001347
1348 if (status == SCI_SUCCESS)
Dan Williamsba7cb222011-06-27 11:56:41 -07001349 stp_req->pio_len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001350 } else {
1351 /* We are transfering the whole frame so copy */
1352 status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
Dan Williamsba7cb222011-06-27 11:56:41 -07001353 stp_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
Dan Williams5dec6f42011-05-10 02:28:49 -07001354
1355 if (status == SCI_SUCCESS)
Dan Williamsba7cb222011-06-27 11:56:41 -07001356 stp_req->pio_len -= SCU_MAX_FRAME_BUFFER_SIZE;
Dan Williams5dec6f42011-05-10 02:28:49 -07001357 }
1358
1359 return status;
1360}
1361
Edmund Nadolskie3013702011-06-02 00:10:43 +00001362static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001363stp_request_pio_await_h2d_completion_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001364 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001365{
1366 enum sci_status status = SCI_SUCCESS;
1367
1368 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1369 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5076a1a2011-06-27 14:57:03 -07001370 scic_sds_request_set_status(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001371 SCU_TASK_DONE_GOOD,
1372 SCI_SUCCESS);
Dan Williams5dec6f42011-05-10 02:28:49 -07001373
Dan Williams5076a1a2011-06-27 14:57:03 -07001374 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams5dec6f42011-05-10 02:28:49 -07001375 break;
1376
1377 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001378 /* All other completion status cause the IO to be
1379 * complete. If a NAK was received, then it is up to
1380 * the user to retry the request.
1381 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001382 scic_sds_request_set_status(ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001383 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1384 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williams5dec6f42011-05-10 02:28:49 -07001385
Dan Williams5076a1a2011-06-27 14:57:03 -07001386 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001387 break;
1388 }
1389
1390 return status;
1391}
1392
Edmund Nadolskie3013702011-06-02 00:10:43 +00001393static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001394pio_data_out_tx_done_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001395 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001396{
1397 enum sci_status status = SCI_SUCCESS;
1398 bool all_frames_transferred = false;
Dan Williams5076a1a2011-06-27 14:57:03 -07001399 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williams5dec6f42011-05-10 02:28:49 -07001400
1401 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1402 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1403 /* Transmit data */
Dan Williamsba7cb222011-06-27 11:56:41 -07001404 if (stp_req->pio_len != 0) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001405 status = scic_sds_stp_request_pio_data_out_transmit_data(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -07001406 if (status == SCI_SUCCESS) {
Dan Williamsba7cb222011-06-27 11:56:41 -07001407 if (stp_req->pio_len == 0)
Dan Williams5dec6f42011-05-10 02:28:49 -07001408 all_frames_transferred = true;
1409 }
Dan Williamsba7cb222011-06-27 11:56:41 -07001410 } else if (stp_req->pio_len == 0) {
Dan Williams5dec6f42011-05-10 02:28:49 -07001411 /*
1412 * this will happen if the all data is written at the
1413 * first time after the pio setup fis is received
1414 */
1415 all_frames_transferred = true;
1416 }
1417
1418 /* all data transferred. */
1419 if (all_frames_transferred) {
1420 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001421 * Change the state to SCI_REQ_STP_PIO_DATA_IN
Dan Williams5dec6f42011-05-10 02:28:49 -07001422 * and wait for PIO_SETUP fis / or D2H REg fis. */
Dan Williams5076a1a2011-06-27 14:57:03 -07001423 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams5dec6f42011-05-10 02:28:49 -07001424 }
1425 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001426
Dan Williams5dec6f42011-05-10 02:28:49 -07001427 default:
1428 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001429 * All other completion status cause the IO to be complete.
1430 * If a NAK was received, then it is up to the user to retry
1431 * the request.
1432 */
Dan Williams5dec6f42011-05-10 02:28:49 -07001433 scic_sds_request_set_status(
Dan Williams5076a1a2011-06-27 14:57:03 -07001434 ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001435 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
Edmund Nadolskie3013702011-06-02 00:10:43 +00001436 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williams5dec6f42011-05-10 02:28:49 -07001437
Dan Williams5076a1a2011-06-27 14:57:03 -07001438 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001439 break;
1440 }
1441
1442 return status;
1443}
1444
Dan Williams5dec6f42011-05-10 02:28:49 -07001445static void scic_sds_stp_request_udma_complete_request(
Dan Williams5076a1a2011-06-27 14:57:03 -07001446 struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001447 u32 scu_status,
1448 enum sci_status sci_status)
1449{
Dan Williams5076a1a2011-06-27 14:57:03 -07001450 scic_sds_request_set_status(ireq, scu_status, sci_status);
1451 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001452}
1453
Dan Williams5076a1a2011-06-27 14:57:03 -07001454static enum sci_status scic_sds_stp_request_udma_general_frame_handler(struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001455 u32 frame_index)
1456{
Dan Williams5076a1a2011-06-27 14:57:03 -07001457 struct scic_sds_controller *scic = ireq->owning_controller;
Dan Williams5dec6f42011-05-10 02:28:49 -07001458 struct dev_to_host_fis *frame_header;
1459 enum sci_status status;
1460 u32 *frame_buffer;
1461
1462 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1463 frame_index,
1464 (void **)&frame_header);
1465
1466 if ((status == SCI_SUCCESS) &&
1467 (frame_header->fis_type == FIS_REGD2H)) {
1468 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1469 frame_index,
1470 (void **)&frame_buffer);
1471
Dan Williams5076a1a2011-06-27 14:57:03 -07001472 scic_sds_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williams5dec6f42011-05-10 02:28:49 -07001473 frame_header,
1474 frame_buffer);
1475 }
1476
1477 scic_sds_controller_release_frame(scic, frame_index);
1478
1479 return status;
1480}
1481
Edmund Nadolskie3013702011-06-02 00:10:43 +00001482enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001483scic_sds_io_request_frame_handler(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001484 u32 frame_index)
Dan Williamsd1c637c32011-05-11 08:27:47 -07001485{
Dan Williams5076a1a2011-06-27 14:57:03 -07001486 struct scic_sds_controller *scic = ireq->owning_controller;
1487 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001488 enum sci_base_request_states state;
1489 enum sci_status status;
1490 ssize_t word_cnt;
1491
Dan Williams5076a1a2011-06-27 14:57:03 -07001492 state = ireq->sm.current_state_id;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001493 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001494 case SCI_REQ_STARTED: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001495 struct ssp_frame_hdr ssp_hdr;
1496 void *frame_header;
1497
1498 scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1499 frame_index,
1500 &frame_header);
1501
1502 word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
1503 sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);
1504
1505 if (ssp_hdr.frame_type == SSP_RESPONSE) {
1506 struct ssp_response_iu *resp_iu;
1507 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1508
1509 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1510 frame_index,
1511 (void **)&resp_iu);
1512
Dan Williams5076a1a2011-06-27 14:57:03 -07001513 sci_swab32_cpy(&ireq->ssp.rsp, resp_iu, word_cnt);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001514
Dan Williams5076a1a2011-06-27 14:57:03 -07001515 resp_iu = &ireq->ssp.rsp;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001516
1517 if (resp_iu->datapres == 0x01 ||
1518 resp_iu->datapres == 0x02) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001519 scic_sds_request_set_status(ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001520 SCU_TASK_DONE_CHECK_RESPONSE,
1521 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1522 } else
Dan Williams5076a1a2011-06-27 14:57:03 -07001523 scic_sds_request_set_status(ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001524 SCU_TASK_DONE_GOOD,
1525 SCI_SUCCESS);
1526 } else {
1527 /* not a response frame, why did it get forwarded? */
1528 dev_err(scic_to_dev(scic),
1529 "%s: SCIC IO Request 0x%p received unexpected "
Dan Williams5076a1a2011-06-27 14:57:03 -07001530 "frame %d type 0x%02x\n", __func__, ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001531 frame_index, ssp_hdr.frame_type);
1532 }
1533
1534 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001535 * In any case we are done with this frame buffer return it to
1536 * the controller
Dan Williamsd1c637c32011-05-11 08:27:47 -07001537 */
1538 scic_sds_controller_release_frame(scic, frame_index);
1539
1540 return SCI_SUCCESS;
1541 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001542
1543 case SCI_REQ_TASK_WAIT_TC_RESP:
Dan Williams5076a1a2011-06-27 14:57:03 -07001544 scic_sds_io_request_copy_response(ireq);
1545 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001546 scic_sds_controller_release_frame(scic,frame_index);
1547 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001548
1549 case SCI_REQ_SMP_WAIT_RESP: {
Dan Williams5076a1a2011-06-27 14:57:03 -07001550 struct smp_resp *rsp_hdr = &ireq->smp.rsp;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001551 void *frame_header;
1552
1553 scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1554 frame_index,
1555 &frame_header);
1556
1557 /* byte swap the header. */
1558 word_cnt = SMP_RESP_HDR_SZ / sizeof(u32);
1559 sci_swab32_cpy(rsp_hdr, frame_header, word_cnt);
1560
1561 if (rsp_hdr->frame_type == SMP_RESPONSE) {
1562 void *smp_resp;
1563
1564 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1565 frame_index,
1566 &smp_resp);
1567
Dan Williams5edc3342011-06-16 17:20:35 -07001568 word_cnt = (sizeof(struct smp_resp) - SMP_RESP_HDR_SZ) /
Dan Williamsd1c637c32011-05-11 08:27:47 -07001569 sizeof(u32);
1570
1571 sci_swab32_cpy(((u8 *) rsp_hdr) + SMP_RESP_HDR_SZ,
1572 smp_resp, word_cnt);
1573
Dan Williams5076a1a2011-06-27 14:57:03 -07001574 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001575 SCI_SUCCESS);
1576
Dan Williams5076a1a2011-06-27 14:57:03 -07001577 sci_change_state(&ireq->sm, SCI_REQ_SMP_WAIT_TC_COMP);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001578 } else {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001579 /*
1580 * This was not a response frame why did it get
1581 * forwarded?
1582 */
Dan Williamsd1c637c32011-05-11 08:27:47 -07001583 dev_err(scic_to_dev(scic),
Edmund Nadolskie3013702011-06-02 00:10:43 +00001584 "%s: SCIC SMP Request 0x%p received unexpected "
1585 "frame %d type 0x%02x\n",
1586 __func__,
Dan Williams5076a1a2011-06-27 14:57:03 -07001587 ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001588 frame_index,
1589 rsp_hdr->frame_type);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001590
Dan Williams5076a1a2011-06-27 14:57:03 -07001591 scic_sds_request_set_status(ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001592 SCU_TASK_DONE_SMP_FRM_TYPE_ERR,
1593 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1594
Dan Williams5076a1a2011-06-27 14:57:03 -07001595 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001596 }
1597
1598 scic_sds_controller_release_frame(scic, frame_index);
1599
1600 return SCI_SUCCESS;
1601 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001602
1603 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07001604 return scic_sds_stp_request_udma_general_frame_handler(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001605 frame_index);
1606
1607 case SCI_REQ_STP_UDMA_WAIT_D2H:
Dan Williamsd1c637c32011-05-11 08:27:47 -07001608 /* Use the general frame handler to copy the resposne data */
Dan Williams5076a1a2011-06-27 14:57:03 -07001609 status = scic_sds_stp_request_udma_general_frame_handler(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001610 frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001611
1612 if (status != SCI_SUCCESS)
1613 return status;
1614
Dan Williams5076a1a2011-06-27 14:57:03 -07001615 scic_sds_stp_request_udma_complete_request(ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001616 SCU_TASK_DONE_CHECK_RESPONSE,
1617 SCI_FAILURE_IO_RESPONSE_VALID);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001618
Dan Williamsd1c637c32011-05-11 08:27:47 -07001619 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001620
1621 case SCI_REQ_STP_NON_DATA_WAIT_D2H: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001622 struct dev_to_host_fis *frame_header;
1623 u32 *frame_buffer;
1624
1625 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1626 frame_index,
1627 (void **)&frame_header);
1628
1629 if (status != SCI_SUCCESS) {
1630 dev_err(scic_to_dev(scic),
Edmund Nadolskie3013702011-06-02 00:10:43 +00001631 "%s: SCIC IO Request 0x%p could not get frame "
1632 "header for frame index %d, status %x\n",
1633 __func__,
1634 stp_req,
1635 frame_index,
1636 status);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001637
1638 return status;
1639 }
1640
1641 switch (frame_header->fis_type) {
1642 case FIS_REGD2H:
1643 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1644 frame_index,
1645 (void **)&frame_buffer);
1646
Dan Williams5076a1a2011-06-27 14:57:03 -07001647 scic_sds_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001648 frame_header,
1649 frame_buffer);
1650
1651 /* The command has completed with error */
Dan Williams5076a1a2011-06-27 14:57:03 -07001652 scic_sds_request_set_status(ireq, SCU_TASK_DONE_CHECK_RESPONSE,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001653 SCI_FAILURE_IO_RESPONSE_VALID);
1654 break;
1655
1656 default:
1657 dev_warn(scic_to_dev(scic),
1658 "%s: IO Request:0x%p Frame Id:%d protocol "
1659 "violation occurred\n", __func__, stp_req,
1660 frame_index);
1661
Dan Williams5076a1a2011-06-27 14:57:03 -07001662 scic_sds_request_set_status(ireq, SCU_TASK_DONE_UNEXP_FIS,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001663 SCI_FAILURE_PROTOCOL_VIOLATION);
1664 break;
1665 }
1666
Dan Williams5076a1a2011-06-27 14:57:03 -07001667 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001668
1669 /* Frame has been decoded return it to the controller */
1670 scic_sds_controller_release_frame(scic, frame_index);
1671
1672 return status;
1673 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001674
1675 case SCI_REQ_STP_PIO_WAIT_FRAME: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001676 struct sas_task *task = isci_request_access_task(ireq);
1677 struct dev_to_host_fis *frame_header;
1678 u32 *frame_buffer;
1679
1680 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1681 frame_index,
1682 (void **)&frame_header);
1683
1684 if (status != SCI_SUCCESS) {
1685 dev_err(scic_to_dev(scic),
Edmund Nadolskie3013702011-06-02 00:10:43 +00001686 "%s: SCIC IO Request 0x%p could not get frame "
1687 "header for frame index %d, status %x\n",
Dan Williamsd1c637c32011-05-11 08:27:47 -07001688 __func__, stp_req, frame_index, status);
1689 return status;
1690 }
1691
1692 switch (frame_header->fis_type) {
1693 case FIS_PIO_SETUP:
1694 /* Get from the frame buffer the PIO Setup Data */
1695 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1696 frame_index,
1697 (void **)&frame_buffer);
1698
Edmund Nadolskie3013702011-06-02 00:10:43 +00001699 /* Get the data from the PIO Setup The SCU Hardware
1700 * returns first word in the frame_header and the rest
1701 * of the data is in the frame buffer so we need to
1702 * back up one dword
Dan Williamsd1c637c32011-05-11 08:27:47 -07001703 */
1704
1705 /* transfer_count: first 16bits in the 4th dword */
Dan Williamsba7cb222011-06-27 11:56:41 -07001706 stp_req->pio_len = frame_buffer[3] & 0xffff;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001707
Dan Williamsba7cb222011-06-27 11:56:41 -07001708 /* status: 4th byte in the 3rd dword */
1709 stp_req->status = (frame_buffer[2] >> 24) & 0xff;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001710
Dan Williams5076a1a2011-06-27 14:57:03 -07001711 scic_sds_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001712 frame_header,
1713 frame_buffer);
1714
Dan Williams5076a1a2011-06-27 14:57:03 -07001715 ireq->stp.rsp.status = stp_req->status;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001716
1717 /* The next state is dependent on whether the
1718 * request was PIO Data-in or Data out
1719 */
1720 if (task->data_dir == DMA_FROM_DEVICE) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001721 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_IN);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001722 } else if (task->data_dir == DMA_TO_DEVICE) {
1723 /* Transmit data */
Dan Williams5076a1a2011-06-27 14:57:03 -07001724 status = scic_sds_stp_request_pio_data_out_transmit_data(ireq);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001725 if (status != SCI_SUCCESS)
1726 break;
Dan Williams5076a1a2011-06-27 14:57:03 -07001727 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_OUT);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001728 }
1729 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001730
Dan Williamsd1c637c32011-05-11 08:27:47 -07001731 case FIS_SETDEVBITS:
Dan Williams5076a1a2011-06-27 14:57:03 -07001732 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001733 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001734
Dan Williamsd1c637c32011-05-11 08:27:47 -07001735 case FIS_REGD2H:
1736 if (frame_header->status & ATA_BUSY) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001737 /*
1738 * Now why is the drive sending a D2H Register
1739 * FIS when it is still busy? Do nothing since
1740 * we are still in the right state.
Dan Williamsd1c637c32011-05-11 08:27:47 -07001741 */
1742 dev_dbg(scic_to_dev(scic),
1743 "%s: SCIC PIO Request 0x%p received "
1744 "D2H Register FIS with BSY status "
Edmund Nadolskie3013702011-06-02 00:10:43 +00001745 "0x%x\n",
1746 __func__,
1747 stp_req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001748 frame_header->status);
1749 break;
1750 }
1751
1752 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1753 frame_index,
1754 (void **)&frame_buffer);
1755
Dan Williams5076a1a2011-06-27 14:57:03 -07001756 scic_sds_controller_copy_sata_response(&ireq->stp.req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001757 frame_header,
1758 frame_buffer);
1759
Dan Williams5076a1a2011-06-27 14:57:03 -07001760 scic_sds_request_set_status(ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001761 SCU_TASK_DONE_CHECK_RESPONSE,
1762 SCI_FAILURE_IO_RESPONSE_VALID);
1763
Dan Williams5076a1a2011-06-27 14:57:03 -07001764 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001765 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001766
Dan Williamsd1c637c32011-05-11 08:27:47 -07001767 default:
1768 /* FIXME: what do we do here? */
1769 break;
1770 }
1771
1772 /* Frame is decoded return it to the controller */
1773 scic_sds_controller_release_frame(scic, frame_index);
1774
1775 return status;
1776 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001777
1778 case SCI_REQ_STP_PIO_DATA_IN: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001779 struct dev_to_host_fis *frame_header;
1780 struct sata_fis_data *frame_buffer;
1781
1782 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1783 frame_index,
1784 (void **)&frame_header);
1785
1786 if (status != SCI_SUCCESS) {
1787 dev_err(scic_to_dev(scic),
Edmund Nadolskie3013702011-06-02 00:10:43 +00001788 "%s: SCIC IO Request 0x%p could not get frame "
1789 "header for frame index %d, status %x\n",
1790 __func__,
1791 stp_req,
1792 frame_index,
1793 status);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001794 return status;
1795 }
1796
1797 if (frame_header->fis_type != FIS_DATA) {
1798 dev_err(scic_to_dev(scic),
1799 "%s: SCIC PIO Request 0x%p received frame %d "
1800 "with fis type 0x%02x when expecting a data "
Edmund Nadolskie3013702011-06-02 00:10:43 +00001801 "fis.\n",
1802 __func__,
1803 stp_req,
1804 frame_index,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001805 frame_header->fis_type);
1806
Dan Williams5076a1a2011-06-27 14:57:03 -07001807 scic_sds_request_set_status(ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001808 SCU_TASK_DONE_GOOD,
1809 SCI_FAILURE_IO_REQUIRES_SCSI_ABORT);
1810
Dan Williams5076a1a2011-06-27 14:57:03 -07001811 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001812
1813 /* Frame is decoded return it to the controller */
1814 scic_sds_controller_release_frame(scic, frame_index);
1815 return status;
1816 }
1817
Dan Williamsba7cb222011-06-27 11:56:41 -07001818 if (stp_req->sgl.index < 0) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001819 ireq->saved_rx_frame_index = frame_index;
Dan Williamsba7cb222011-06-27 11:56:41 -07001820 stp_req->pio_len = 0;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001821 } else {
1822 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1823 frame_index,
1824 (void **)&frame_buffer);
1825
1826 status = scic_sds_stp_request_pio_data_in_copy_data(stp_req,
1827 (u8 *)frame_buffer);
1828
1829 /* Frame is decoded return it to the controller */
1830 scic_sds_controller_release_frame(scic, frame_index);
1831 }
1832
1833 /* Check for the end of the transfer, are there more
1834 * bytes remaining for this data transfer
1835 */
Dan Williamsba7cb222011-06-27 11:56:41 -07001836 if (status != SCI_SUCCESS || stp_req->pio_len != 0)
Dan Williamsd1c637c32011-05-11 08:27:47 -07001837 return status;
1838
Dan Williamsba7cb222011-06-27 11:56:41 -07001839 if ((stp_req->status & ATA_BUSY) == 0) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001840 scic_sds_request_set_status(ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001841 SCU_TASK_DONE_CHECK_RESPONSE,
1842 SCI_FAILURE_IO_RESPONSE_VALID);
1843
Dan Williams5076a1a2011-06-27 14:57:03 -07001844 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001845 } else {
Dan Williams5076a1a2011-06-27 14:57:03 -07001846 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001847 }
1848 return status;
1849 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001850
1851 case SCI_REQ_STP_SOFT_RESET_WAIT_D2H: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001852 struct dev_to_host_fis *frame_header;
1853 u32 *frame_buffer;
1854
1855 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1856 frame_index,
1857 (void **)&frame_header);
1858 if (status != SCI_SUCCESS) {
1859 dev_err(scic_to_dev(scic),
Edmund Nadolskie3013702011-06-02 00:10:43 +00001860 "%s: SCIC IO Request 0x%p could not get frame "
1861 "header for frame index %d, status %x\n",
1862 __func__,
1863 stp_req,
1864 frame_index,
1865 status);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001866 return status;
1867 }
1868
1869 switch (frame_header->fis_type) {
1870 case FIS_REGD2H:
1871 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1872 frame_index,
1873 (void **)&frame_buffer);
1874
Dan Williams5076a1a2011-06-27 14:57:03 -07001875 scic_sds_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001876 frame_header,
1877 frame_buffer);
1878
1879 /* The command has completed with error */
Dan Williams5076a1a2011-06-27 14:57:03 -07001880 scic_sds_request_set_status(ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001881 SCU_TASK_DONE_CHECK_RESPONSE,
1882 SCI_FAILURE_IO_RESPONSE_VALID);
1883 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001884
Dan Williamsd1c637c32011-05-11 08:27:47 -07001885 default:
1886 dev_warn(scic_to_dev(scic),
1887 "%s: IO Request:0x%p Frame Id:%d protocol "
Edmund Nadolskie3013702011-06-02 00:10:43 +00001888 "violation occurred\n",
1889 __func__,
1890 stp_req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001891 frame_index);
1892
Dan Williams5076a1a2011-06-27 14:57:03 -07001893 scic_sds_request_set_status(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001894 SCU_TASK_DONE_UNEXP_FIS,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001895 SCI_FAILURE_PROTOCOL_VIOLATION);
1896 break;
1897 }
1898
Dan Williams5076a1a2011-06-27 14:57:03 -07001899 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001900
1901 /* Frame has been decoded return it to the controller */
1902 scic_sds_controller_release_frame(scic, frame_index);
1903
1904 return status;
1905 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001906 case SCI_REQ_ABORTING:
1907 /*
1908 * TODO: Is it even possible to get an unsolicited frame in the
Dan Williamsd1c637c32011-05-11 08:27:47 -07001909 * aborting state?
1910 */
1911 scic_sds_controller_release_frame(scic, frame_index);
1912 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001913
Dan Williamsd1c637c32011-05-11 08:27:47 -07001914 default:
1915 dev_warn(scic_to_dev(scic),
Edmund Nadolskie3013702011-06-02 00:10:43 +00001916 "%s: SCIC IO Request given unexpected frame %x while "
1917 "in state %d\n",
1918 __func__,
1919 frame_index,
1920 state);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001921
1922 scic_sds_controller_release_frame(scic, frame_index);
1923 return SCI_FAILURE_INVALID_STATE;
1924 }
1925}
1926
Dan Williams5076a1a2011-06-27 14:57:03 -07001927static enum sci_status stp_request_udma_await_tc_event(struct isci_request *ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001928 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001929{
1930 enum sci_status status = SCI_SUCCESS;
1931
1932 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1933 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5076a1a2011-06-27 14:57:03 -07001934 scic_sds_stp_request_udma_complete_request(ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001935 SCU_TASK_DONE_GOOD,
1936 SCI_SUCCESS);
1937 break;
1938 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
1939 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001940 /* We must check ther response buffer to see if the D2H
1941 * Register FIS was received before we got the TC
1942 * completion.
1943 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001944 if (ireq->stp.rsp.fis_type == FIS_REGD2H) {
1945 scic_sds_remote_device_suspend(ireq->target_device,
Dan Williams5dec6f42011-05-10 02:28:49 -07001946 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
1947
Dan Williams5076a1a2011-06-27 14:57:03 -07001948 scic_sds_stp_request_udma_complete_request(ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001949 SCU_TASK_DONE_CHECK_RESPONSE,
1950 SCI_FAILURE_IO_RESPONSE_VALID);
1951 } else {
Dan Williamsa7e255a2011-05-11 08:27:47 -07001952 /* If we have an error completion status for the
1953 * TC then we can expect a D2H register FIS from
1954 * the device so we must change state to wait
1955 * for it
1956 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001957 sci_change_state(&ireq->sm, SCI_REQ_STP_UDMA_WAIT_D2H);
Dan Williams5dec6f42011-05-10 02:28:49 -07001958 }
1959 break;
1960
Dan Williamsa7e255a2011-05-11 08:27:47 -07001961 /* TODO Check to see if any of these completion status need to
1962 * wait for the device to host register fis.
1963 */
1964 /* TODO We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR
1965 * - this comes only for B0
1966 */
Dan Williams5dec6f42011-05-10 02:28:49 -07001967 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_INV_FIS_LEN):
1968 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
1969 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_R_ERR):
1970 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CMD_LL_R_ERR):
1971 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CRC_ERR):
Dan Williams5076a1a2011-06-27 14:57:03 -07001972 scic_sds_remote_device_suspend(ireq->target_device,
Dan Williams5dec6f42011-05-10 02:28:49 -07001973 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
1974 /* Fall through to the default case */
1975 default:
1976 /* All other completion status cause the IO to be complete. */
Dan Williams5076a1a2011-06-27 14:57:03 -07001977 scic_sds_stp_request_udma_complete_request(ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001978 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1979 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1980 break;
1981 }
1982
1983 return status;
1984}
1985
Edmund Nadolskie3013702011-06-02 00:10:43 +00001986static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001987stp_request_soft_reset_await_h2d_asserted_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001988 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001989{
1990 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1991 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5076a1a2011-06-27 14:57:03 -07001992 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001993 SCI_SUCCESS);
Dan Williams5dec6f42011-05-10 02:28:49 -07001994
Dan Williams5076a1a2011-06-27 14:57:03 -07001995 sci_change_state(&ireq->sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG);
Dan Williams5dec6f42011-05-10 02:28:49 -07001996 break;
1997
1998 default:
1999 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00002000 * All other completion status cause the IO to be complete.
2001 * If a NAK was received, then it is up to the user to retry
2002 * the request.
2003 */
Dan Williams5076a1a2011-06-27 14:57:03 -07002004 scic_sds_request_set_status(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002005 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
2006 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williams5dec6f42011-05-10 02:28:49 -07002007
Dan Williams5076a1a2011-06-27 14:57:03 -07002008 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07002009 break;
2010 }
2011
2012 return SCI_SUCCESS;
2013}
2014
Edmund Nadolskie3013702011-06-02 00:10:43 +00002015static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07002016stp_request_soft_reset_await_h2d_diagnostic_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002017 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07002018{
2019 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2020 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5076a1a2011-06-27 14:57:03 -07002021 scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
Dan Williams5dec6f42011-05-10 02:28:49 -07002022 SCI_SUCCESS);
2023
Dan Williams5076a1a2011-06-27 14:57:03 -07002024 sci_change_state(&ireq->sm, SCI_REQ_STP_SOFT_RESET_WAIT_D2H);
Dan Williams5dec6f42011-05-10 02:28:49 -07002025 break;
2026
2027 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07002028 /* All other completion status cause the IO to be complete. If
2029 * a NAK was received, then it is up to the user to retry the
2030 * request.
2031 */
Dan Williams5076a1a2011-06-27 14:57:03 -07002032 scic_sds_request_set_status(ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07002033 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
Dan Williamsa7e255a2011-05-11 08:27:47 -07002034 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williams5dec6f42011-05-10 02:28:49 -07002035
Dan Williams5076a1a2011-06-27 14:57:03 -07002036 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07002037 break;
2038 }
2039
2040 return SCI_SUCCESS;
2041}
2042
Dan Williamsa7e255a2011-05-11 08:27:47 -07002043enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07002044scic_sds_io_request_tc_completion(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002045 u32 completion_code)
Dan Williamsa7e255a2011-05-11 08:27:47 -07002046{
2047 enum sci_base_request_states state;
Dan Williams5076a1a2011-06-27 14:57:03 -07002048 struct scic_sds_controller *scic = ireq->owning_controller;
Dan Williamsa7e255a2011-05-11 08:27:47 -07002049
Dan Williams5076a1a2011-06-27 14:57:03 -07002050 state = ireq->sm.current_state_id;
Dan Williamsa7e255a2011-05-11 08:27:47 -07002051
2052 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002053 case SCI_REQ_STARTED:
Dan Williams5076a1a2011-06-27 14:57:03 -07002054 return request_started_state_tc_event(ireq, completion_code);
Edmund Nadolskie3013702011-06-02 00:10:43 +00002055
2056 case SCI_REQ_TASK_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07002057 return ssp_task_request_await_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002058 completion_code);
2059
2060 case SCI_REQ_SMP_WAIT_RESP:
Dan Williams5076a1a2011-06-27 14:57:03 -07002061 return smp_request_await_response_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002062 completion_code);
2063
2064 case SCI_REQ_SMP_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07002065 return smp_request_await_tc_event(ireq, completion_code);
Edmund Nadolskie3013702011-06-02 00:10:43 +00002066
2067 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07002068 return stp_request_udma_await_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002069 completion_code);
2070
2071 case SCI_REQ_STP_NON_DATA_WAIT_H2D:
Dan Williams5076a1a2011-06-27 14:57:03 -07002072 return stp_request_non_data_await_h2d_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002073 completion_code);
2074
2075 case SCI_REQ_STP_PIO_WAIT_H2D:
Dan Williams5076a1a2011-06-27 14:57:03 -07002076 return stp_request_pio_await_h2d_completion_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002077 completion_code);
2078
2079 case SCI_REQ_STP_PIO_DATA_OUT:
Dan Williams5076a1a2011-06-27 14:57:03 -07002080 return pio_data_out_tx_done_tc_event(ireq, completion_code);
Edmund Nadolskie3013702011-06-02 00:10:43 +00002081
2082 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
Dan Williams5076a1a2011-06-27 14:57:03 -07002083 return stp_request_soft_reset_await_h2d_asserted_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002084 completion_code);
2085
2086 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
Dan Williams5076a1a2011-06-27 14:57:03 -07002087 return stp_request_soft_reset_await_h2d_diagnostic_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002088 completion_code);
2089
2090 case SCI_REQ_ABORTING:
Dan Williams5076a1a2011-06-27 14:57:03 -07002091 return request_aborting_state_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002092 completion_code);
2093
2094 default:
2095 dev_warn(scic_to_dev(scic),
2096 "%s: SCIC IO Request given task completion "
2097 "notification %x while in wrong state %d\n",
2098 __func__,
2099 completion_code,
2100 state);
2101 return SCI_FAILURE_INVALID_STATE;
Dan Williamsa7e255a2011-05-11 08:27:47 -07002102 }
2103}
2104
Dan Williams6f231dd2011-07-02 22:56:22 -07002105/**
2106 * isci_request_process_response_iu() - This function sets the status and
2107 * response iu, in the task struct, from the request object for the upper
2108 * layer driver.
2109 * @sas_task: This parameter is the task struct from the upper layer driver.
2110 * @resp_iu: This parameter points to the response iu of the completed request.
2111 * @dev: This parameter specifies the linux device struct.
2112 *
2113 * none.
2114 */
2115static void isci_request_process_response_iu(
2116 struct sas_task *task,
2117 struct ssp_response_iu *resp_iu,
2118 struct device *dev)
2119{
2120 dev_dbg(dev,
2121 "%s: resp_iu = %p "
2122 "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
2123 "resp_iu->response_data_len = %x, "
2124 "resp_iu->sense_data_len = %x\nrepsonse data: ",
2125 __func__,
2126 resp_iu,
2127 resp_iu->status,
2128 resp_iu->datapres,
2129 resp_iu->response_data_len,
2130 resp_iu->sense_data_len);
2131
2132 task->task_status.stat = resp_iu->status;
2133
2134 /* libsas updates the task status fields based on the response iu. */
2135 sas_ssp_task_response(dev, task, resp_iu);
2136}
2137
2138/**
2139 * isci_request_set_open_reject_status() - This function prepares the I/O
2140 * completion for OPEN_REJECT conditions.
2141 * @request: This parameter is the completed isci_request object.
2142 * @response_ptr: This parameter specifies the service response for the I/O.
2143 * @status_ptr: This parameter specifies the exec status for the I/O.
2144 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2145 * the LLDD with respect to completing this request or forcing an abort
2146 * condition on the I/O.
2147 * @open_rej_reason: This parameter specifies the encoded reason for the
2148 * abandon-class reject.
2149 *
2150 * none.
2151 */
2152static void isci_request_set_open_reject_status(
2153 struct isci_request *request,
2154 struct sas_task *task,
2155 enum service_response *response_ptr,
2156 enum exec_status *status_ptr,
2157 enum isci_completion_selection *complete_to_host_ptr,
2158 enum sas_open_rej_reason open_rej_reason)
2159{
2160 /* Task in the target is done. */
Dan Williams38d88792011-06-23 14:33:48 -07002161 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002162 *response_ptr = SAS_TASK_UNDELIVERED;
2163 *status_ptr = SAS_OPEN_REJECT;
2164 *complete_to_host_ptr = isci_perform_normal_io_completion;
2165 task->task_status.open_rej_reason = open_rej_reason;
2166}
2167
2168/**
2169 * isci_request_handle_controller_specific_errors() - This function decodes
2170 * controller-specific I/O completion error conditions.
2171 * @request: This parameter is the completed isci_request object.
2172 * @response_ptr: This parameter specifies the service response for the I/O.
2173 * @status_ptr: This parameter specifies the exec status for the I/O.
2174 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2175 * the LLDD with respect to completing this request or forcing an abort
2176 * condition on the I/O.
2177 *
2178 * none.
2179 */
2180static void isci_request_handle_controller_specific_errors(
Dan Williams209fae12011-06-13 17:39:44 -07002181 struct isci_remote_device *idev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002182 struct isci_request *request,
2183 struct sas_task *task,
2184 enum service_response *response_ptr,
2185 enum exec_status *status_ptr,
2186 enum isci_completion_selection *complete_to_host_ptr)
2187{
2188 unsigned int cstatus;
2189
Dan Williams5076a1a2011-06-27 14:57:03 -07002190 cstatus = request->scu_status;
Dan Williams6f231dd2011-07-02 22:56:22 -07002191
2192 dev_dbg(&request->isci_host->pdev->dev,
2193 "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
2194 "- controller status = 0x%x\n",
2195 __func__, request, cstatus);
2196
2197 /* Decode the controller-specific errors; most
2198 * important is to recognize those conditions in which
2199 * the target may still have a task outstanding that
2200 * must be aborted.
2201 *
2202 * Note that there are SCU completion codes being
2203 * named in the decode below for which SCIC has already
2204 * done work to handle them in a way other than as
2205 * a controller-specific completion code; these are left
2206 * in the decode below for completeness sake.
2207 */
2208 switch (cstatus) {
2209 case SCU_TASK_DONE_DMASETUP_DIRERR:
2210 /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
2211 case SCU_TASK_DONE_XFERCNT_ERR:
2212 /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
2213 if (task->task_proto == SAS_PROTOCOL_SMP) {
2214 /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
2215 *response_ptr = SAS_TASK_COMPLETE;
2216
2217 /* See if the device has been/is being stopped. Note
2218 * that we ignore the quiesce state, since we are
2219 * concerned about the actual device state.
2220 */
Dan Williams209fae12011-06-13 17:39:44 -07002221 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002222 *status_ptr = SAS_DEVICE_UNKNOWN;
2223 else
2224 *status_ptr = SAS_ABORTED_TASK;
2225
Dan Williams38d88792011-06-23 14:33:48 -07002226 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002227
2228 *complete_to_host_ptr =
2229 isci_perform_normal_io_completion;
2230 } else {
2231 /* Task in the target is not done. */
2232 *response_ptr = SAS_TASK_UNDELIVERED;
2233
Dan Williams209fae12011-06-13 17:39:44 -07002234 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002235 *status_ptr = SAS_DEVICE_UNKNOWN;
2236 else
2237 *status_ptr = SAM_STAT_TASK_ABORTED;
2238
Dan Williams38d88792011-06-23 14:33:48 -07002239 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002240
2241 *complete_to_host_ptr =
2242 isci_perform_error_io_completion;
2243 }
2244
2245 break;
2246
2247 case SCU_TASK_DONE_CRC_ERR:
2248 case SCU_TASK_DONE_NAK_CMD_ERR:
2249 case SCU_TASK_DONE_EXCESS_DATA:
2250 case SCU_TASK_DONE_UNEXP_FIS:
2251 /* Also SCU_TASK_DONE_UNEXP_RESP: */
2252 case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
2253 case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
2254 case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
2255 /* These are conditions in which the target
2256 * has completed the task, so that no cleanup
2257 * is necessary.
2258 */
2259 *response_ptr = SAS_TASK_COMPLETE;
2260
2261 /* See if the device has been/is being stopped. Note
2262 * that we ignore the quiesce state, since we are
2263 * concerned about the actual device state.
2264 */
Dan Williams209fae12011-06-13 17:39:44 -07002265 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002266 *status_ptr = SAS_DEVICE_UNKNOWN;
2267 else
2268 *status_ptr = SAS_ABORTED_TASK;
2269
Dan Williams38d88792011-06-23 14:33:48 -07002270 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002271
2272 *complete_to_host_ptr = isci_perform_normal_io_completion;
2273 break;
2274
2275
2276 /* Note that the only open reject completion codes seen here will be
2277 * abandon-class codes; all others are automatically retried in the SCU.
2278 */
2279 case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2280
2281 isci_request_set_open_reject_status(
2282 request, task, response_ptr, status_ptr,
2283 complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
2284 break;
2285
2286 case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2287
2288 /* Note - the return of AB0 will change when
2289 * libsas implements detection of zone violations.
2290 */
2291 isci_request_set_open_reject_status(
2292 request, task, response_ptr, status_ptr,
2293 complete_to_host_ptr, SAS_OREJ_RESV_AB0);
2294 break;
2295
2296 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2297
2298 isci_request_set_open_reject_status(
2299 request, task, response_ptr, status_ptr,
2300 complete_to_host_ptr, SAS_OREJ_RESV_AB1);
2301 break;
2302
2303 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2304
2305 isci_request_set_open_reject_status(
2306 request, task, response_ptr, status_ptr,
2307 complete_to_host_ptr, SAS_OREJ_RESV_AB2);
2308 break;
2309
2310 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2311
2312 isci_request_set_open_reject_status(
2313 request, task, response_ptr, status_ptr,
2314 complete_to_host_ptr, SAS_OREJ_RESV_AB3);
2315 break;
2316
2317 case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2318
2319 isci_request_set_open_reject_status(
2320 request, task, response_ptr, status_ptr,
2321 complete_to_host_ptr, SAS_OREJ_BAD_DEST);
2322 break;
2323
2324 case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
2325
2326 isci_request_set_open_reject_status(
2327 request, task, response_ptr, status_ptr,
2328 complete_to_host_ptr, SAS_OREJ_STP_NORES);
2329 break;
2330
2331 case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
2332
2333 isci_request_set_open_reject_status(
2334 request, task, response_ptr, status_ptr,
2335 complete_to_host_ptr, SAS_OREJ_EPROTO);
2336 break;
2337
2338 case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
2339
2340 isci_request_set_open_reject_status(
2341 request, task, response_ptr, status_ptr,
2342 complete_to_host_ptr, SAS_OREJ_CONN_RATE);
2343 break;
2344
2345 case SCU_TASK_DONE_LL_R_ERR:
2346 /* Also SCU_TASK_DONE_ACK_NAK_TO: */
2347 case SCU_TASK_DONE_LL_PERR:
2348 case SCU_TASK_DONE_LL_SY_TERM:
2349 /* Also SCU_TASK_DONE_NAK_ERR:*/
2350 case SCU_TASK_DONE_LL_LF_TERM:
2351 /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
2352 case SCU_TASK_DONE_LL_ABORT_ERR:
2353 case SCU_TASK_DONE_SEQ_INV_TYPE:
2354 /* Also SCU_TASK_DONE_UNEXP_XR: */
2355 case SCU_TASK_DONE_XR_IU_LEN_ERR:
2356 case SCU_TASK_DONE_INV_FIS_LEN:
2357 /* Also SCU_TASK_DONE_XR_WD_LEN: */
2358 case SCU_TASK_DONE_SDMA_ERR:
2359 case SCU_TASK_DONE_OFFSET_ERR:
2360 case SCU_TASK_DONE_MAX_PLD_ERR:
2361 case SCU_TASK_DONE_LF_ERR:
2362 case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
2363 case SCU_TASK_DONE_SMP_LL_RX_ERR:
2364 case SCU_TASK_DONE_UNEXP_DATA:
2365 case SCU_TASK_DONE_UNEXP_SDBFIS:
2366 case SCU_TASK_DONE_REG_ERR:
2367 case SCU_TASK_DONE_SDB_ERR:
2368 case SCU_TASK_DONE_TASK_ABORT:
2369 default:
2370 /* Task in the target is not done. */
2371 *response_ptr = SAS_TASK_UNDELIVERED;
2372 *status_ptr = SAM_STAT_TASK_ABORTED;
Dan Williams6f231dd2011-07-02 22:56:22 -07002373
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002374 if (task->task_proto == SAS_PROTOCOL_SMP) {
Dan Williams38d88792011-06-23 14:33:48 -07002375 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002376
2377 *complete_to_host_ptr = isci_perform_normal_io_completion;
2378 } else {
Dan Williams38d88792011-06-23 14:33:48 -07002379 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002380
2381 *complete_to_host_ptr = isci_perform_error_io_completion;
2382 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002383 break;
2384 }
2385}
2386
2387/**
2388 * isci_task_save_for_upper_layer_completion() - This function saves the
2389 * request for later completion to the upper layer driver.
2390 * @host: This parameter is a pointer to the host on which the the request
2391 * should be queued (either as an error or success).
2392 * @request: This parameter is the completed request.
2393 * @response: This parameter is the response code for the completed task.
2394 * @status: This parameter is the status code for the completed task.
2395 *
2396 * none.
2397 */
2398static void isci_task_save_for_upper_layer_completion(
2399 struct isci_host *host,
2400 struct isci_request *request,
2401 enum service_response response,
2402 enum exec_status status,
2403 enum isci_completion_selection task_notification_selection)
2404{
2405 struct sas_task *task = isci_request_access_task(request);
2406
Jeff Skirvinec6c9632011-03-04 14:06:44 -08002407 task_notification_selection
2408 = isci_task_set_completion_status(task, response, status,
2409 task_notification_selection);
Dan Williams6f231dd2011-07-02 22:56:22 -07002410
2411 /* Tasks aborted specifically by a call to the lldd_abort_task
2412 * function should not be completed to the host in the regular path.
2413 */
2414 switch (task_notification_selection) {
2415
2416 case isci_perform_normal_io_completion:
2417
2418 /* Normal notification (task_done) */
2419 dev_dbg(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002420 "%s: Normal - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002421 __func__,
2422 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002423 task->task_status.resp, response,
2424 task->task_status.stat, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07002425 /* Add to the completed list. */
2426 list_add(&request->completed_node,
2427 &host->requests_to_complete);
Jeff Skirvinec6c9632011-03-04 14:06:44 -08002428
2429 /* Take the request off the device's pending request list. */
2430 list_del_init(&request->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -07002431 break;
2432
2433 case isci_perform_aborted_io_completion:
Jeff Skirvina5fde222011-03-04 14:06:42 -08002434 /* No notification to libsas because this request is
2435 * already in the abort path.
Dan Williams6f231dd2011-07-02 22:56:22 -07002436 */
2437 dev_warn(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002438 "%s: Aborted - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002439 __func__,
2440 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002441 task->task_status.resp, response,
2442 task->task_status.stat, status);
Jeff Skirvina5fde222011-03-04 14:06:42 -08002443
2444 /* Wake up whatever process was waiting for this
2445 * request to complete.
2446 */
2447 WARN_ON(request->io_request_completion == NULL);
2448
2449 if (request->io_request_completion != NULL) {
2450
2451 /* Signal whoever is waiting that this
2452 * request is complete.
2453 */
2454 complete(request->io_request_completion);
2455 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002456 break;
2457
2458 case isci_perform_error_io_completion:
2459 /* Use sas_task_abort */
2460 dev_warn(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002461 "%s: Error - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002462 __func__,
2463 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002464 task->task_status.resp, response,
2465 task->task_status.stat, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07002466 /* Add to the aborted list. */
2467 list_add(&request->completed_node,
Jeff Skirvin11b00c12011-03-04 14:06:40 -08002468 &host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002469 break;
2470
2471 default:
2472 dev_warn(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002473 "%s: Unknown - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002474 __func__,
2475 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002476 task->task_status.resp, response,
2477 task->task_status.stat, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07002478
Jeff Skirvina5fde222011-03-04 14:06:42 -08002479 /* Add to the error to libsas list. */
Dan Williams6f231dd2011-07-02 22:56:22 -07002480 list_add(&request->completed_node,
Jeff Skirvin11b00c12011-03-04 14:06:40 -08002481 &host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002482 break;
2483 }
2484}
2485
Dan Williamsf1f52e72011-05-10 02:28:45 -07002486static void isci_request_io_request_complete(struct isci_host *isci_host,
2487 struct isci_request *request,
2488 enum sci_io_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07002489{
2490 struct sas_task *task = isci_request_access_task(request);
2491 struct ssp_response_iu *resp_iu;
2492 void *resp_buf;
2493 unsigned long task_flags;
Dan Williams209fae12011-06-13 17:39:44 -07002494 struct isci_remote_device *idev = isci_lookup_device(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002495 enum service_response response = SAS_TASK_UNDELIVERED;
2496 enum exec_status status = SAS_ABORTED_TASK;
2497 enum isci_request_status request_status;
2498 enum isci_completion_selection complete_to_host
2499 = isci_perform_normal_io_completion;
2500
2501 dev_dbg(&isci_host->pdev->dev,
2502 "%s: request = %p, task = %p,\n"
2503 "task->data_dir = %d completion_status = 0x%x\n",
2504 __func__,
2505 request,
2506 task,
2507 task->data_dir,
2508 completion_status);
2509
Jeff Skirvina5fde222011-03-04 14:06:42 -08002510 spin_lock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002511 request_status = isci_request_get_state(request);
Dan Williams6f231dd2011-07-02 22:56:22 -07002512
2513 /* Decode the request status. Note that if the request has been
2514 * aborted by a task management function, we don't care
2515 * what the status is.
2516 */
2517 switch (request_status) {
2518
2519 case aborted:
2520 /* "aborted" indicates that the request was aborted by a task
2521 * management function, since once a task management request is
2522 * perfomed by the device, the request only completes because
2523 * of the subsequent driver terminate.
2524 *
2525 * Aborted also means an external thread is explicitly managing
2526 * this request, so that we do not complete it up the stack.
2527 *
2528 * The target is still there (since the TMF was successful).
2529 */
Dan Williams38d88792011-06-23 14:33:48 -07002530 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002531 response = SAS_TASK_COMPLETE;
2532
2533 /* See if the device has been/is being stopped. Note
2534 * that we ignore the quiesce state, since we are
2535 * concerned about the actual device state.
2536 */
Dan Williams209fae12011-06-13 17:39:44 -07002537 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002538 status = SAS_DEVICE_UNKNOWN;
2539 else
2540 status = SAS_ABORTED_TASK;
2541
2542 complete_to_host = isci_perform_aborted_io_completion;
2543 /* This was an aborted request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002544
2545 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002546 break;
2547
2548 case aborting:
2549 /* aborting means that the task management function tried and
2550 * failed to abort the request. We need to note the request
2551 * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
2552 * target as down.
2553 *
2554 * Aborting also means an external thread is explicitly managing
2555 * this request, so that we do not complete it up the stack.
2556 */
Dan Williams38d88792011-06-23 14:33:48 -07002557 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002558 response = SAS_TASK_UNDELIVERED;
2559
Dan Williams209fae12011-06-13 17:39:44 -07002560 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002561 /* The device has been /is being stopped. Note that
2562 * we ignore the quiesce state, since we are
2563 * concerned about the actual device state.
2564 */
2565 status = SAS_DEVICE_UNKNOWN;
2566 else
2567 status = SAS_PHY_DOWN;
2568
2569 complete_to_host = isci_perform_aborted_io_completion;
2570
2571 /* This was an aborted request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002572
2573 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002574 break;
2575
2576 case terminating:
2577
2578 /* This was an terminated request. This happens when
2579 * the I/O is being terminated because of an action on
2580 * the device (reset, tear down, etc.), and the I/O needs
2581 * to be completed up the stack.
2582 */
Dan Williams38d88792011-06-23 14:33:48 -07002583 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002584 response = SAS_TASK_UNDELIVERED;
2585
2586 /* See if the device has been/is being stopped. Note
2587 * that we ignore the quiesce state, since we are
2588 * concerned about the actual device state.
2589 */
Dan Williams209fae12011-06-13 17:39:44 -07002590 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002591 status = SAS_DEVICE_UNKNOWN;
2592 else
2593 status = SAS_ABORTED_TASK;
2594
Jeff Skirvina5fde222011-03-04 14:06:42 -08002595 complete_to_host = isci_perform_aborted_io_completion;
Dan Williams6f231dd2011-07-02 22:56:22 -07002596
2597 /* This was a terminated request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002598
2599 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002600 break;
2601
Jeff Skirvin77c852f2011-06-20 14:09:16 -07002602 case dead:
2603 /* This was a terminated request that timed-out during the
2604 * termination process. There is no task to complete to
2605 * libsas.
2606 */
2607 complete_to_host = isci_perform_normal_io_completion;
2608 spin_unlock(&request->state_lock);
2609 break;
2610
Dan Williams6f231dd2011-07-02 22:56:22 -07002611 default:
2612
Jeff Skirvina5fde222011-03-04 14:06:42 -08002613 /* The request is done from an SCU HW perspective. */
2614 request->status = completed;
2615
2616 spin_unlock(&request->state_lock);
2617
Dan Williams6f231dd2011-07-02 22:56:22 -07002618 /* This is an active request being completed from the core. */
2619 switch (completion_status) {
2620
2621 case SCI_IO_FAILURE_RESPONSE_VALID:
2622 dev_dbg(&isci_host->pdev->dev,
2623 "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
2624 __func__,
2625 request,
2626 task);
2627
2628 if (sas_protocol_ata(task->task_proto)) {
Dan Williams5076a1a2011-06-27 14:57:03 -07002629 resp_buf = &request->stp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -07002630 isci_request_process_stp_response(task,
Dan Williamsb7645812011-05-08 02:35:32 -07002631 resp_buf);
Dan Williams6f231dd2011-07-02 22:56:22 -07002632 } else if (SAS_PROTOCOL_SSP == task->task_proto) {
2633
2634 /* crack the iu response buffer. */
Dan Williams5076a1a2011-06-27 14:57:03 -07002635 resp_iu = &request->ssp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -07002636 isci_request_process_response_iu(task, resp_iu,
Dan Williamsb7645812011-05-08 02:35:32 -07002637 &isci_host->pdev->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002638
2639 } else if (SAS_PROTOCOL_SMP == task->task_proto) {
2640
2641 dev_err(&isci_host->pdev->dev,
2642 "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
2643 "SAS_PROTOCOL_SMP protocol\n",
2644 __func__);
2645
2646 } else
2647 dev_err(&isci_host->pdev->dev,
2648 "%s: unknown protocol\n", __func__);
2649
2650 /* use the task status set in the task struct by the
2651 * isci_request_process_response_iu call.
2652 */
Dan Williams38d88792011-06-23 14:33:48 -07002653 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002654 response = task->task_status.resp;
2655 status = task->task_status.stat;
2656 break;
2657
2658 case SCI_IO_SUCCESS:
2659 case SCI_IO_SUCCESS_IO_DONE_EARLY:
2660
2661 response = SAS_TASK_COMPLETE;
2662 status = SAM_STAT_GOOD;
Dan Williams38d88792011-06-23 14:33:48 -07002663 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002664
2665 if (task->task_proto == SAS_PROTOCOL_SMP) {
Dan Williams5076a1a2011-06-27 14:57:03 -07002666 void *rsp = &request->smp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -07002667
2668 dev_dbg(&isci_host->pdev->dev,
2669 "%s: SMP protocol completion\n",
2670 __func__);
2671
2672 sg_copy_from_buffer(
2673 &task->smp_task.smp_resp, 1,
Dan Williamsb7645812011-05-08 02:35:32 -07002674 rsp, sizeof(struct smp_resp));
Dan Williams6f231dd2011-07-02 22:56:22 -07002675 } else if (completion_status
2676 == SCI_IO_SUCCESS_IO_DONE_EARLY) {
2677
2678 /* This was an SSP / STP / SATA transfer.
2679 * There is a possibility that less data than
2680 * the maximum was transferred.
2681 */
Dan Williams5076a1a2011-06-27 14:57:03 -07002682 u32 transferred_length = sci_req_tx_bytes(request);
Dan Williams6f231dd2011-07-02 22:56:22 -07002683
2684 task->task_status.residual
2685 = task->total_xfer_len - transferred_length;
2686
2687 /* If there were residual bytes, call this an
2688 * underrun.
2689 */
2690 if (task->task_status.residual != 0)
2691 status = SAS_DATA_UNDERRUN;
2692
2693 dev_dbg(&isci_host->pdev->dev,
2694 "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
2695 __func__,
2696 status);
2697
2698 } else
2699 dev_dbg(&isci_host->pdev->dev,
2700 "%s: SCI_IO_SUCCESS\n",
2701 __func__);
2702
2703 break;
2704
2705 case SCI_IO_FAILURE_TERMINATED:
2706 dev_dbg(&isci_host->pdev->dev,
2707 "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
2708 __func__,
2709 request,
2710 task);
2711
2712 /* The request was terminated explicitly. No handling
2713 * is needed in the SCSI error handler path.
2714 */
Dan Williams38d88792011-06-23 14:33:48 -07002715 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002716 response = SAS_TASK_UNDELIVERED;
2717
2718 /* See if the device has been/is being stopped. Note
2719 * that we ignore the quiesce state, since we are
2720 * concerned about the actual device state.
2721 */
Dan Williams209fae12011-06-13 17:39:44 -07002722 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002723 status = SAS_DEVICE_UNKNOWN;
2724 else
2725 status = SAS_ABORTED_TASK;
2726
2727 complete_to_host = isci_perform_normal_io_completion;
2728 break;
2729
2730 case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
2731
2732 isci_request_handle_controller_specific_errors(
Dan Williams209fae12011-06-13 17:39:44 -07002733 idev, request, task, &response, &status,
Dan Williams6f231dd2011-07-02 22:56:22 -07002734 &complete_to_host);
2735
2736 break;
2737
2738 case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
2739 /* This is a special case, in that the I/O completion
2740 * is telling us that the device needs a reset.
2741 * In order for the device reset condition to be
2742 * noticed, the I/O has to be handled in the error
2743 * handler. Set the reset flag and cause the
2744 * SCSI error thread to be scheduled.
2745 */
2746 spin_lock_irqsave(&task->task_state_lock, task_flags);
2747 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
2748 spin_unlock_irqrestore(&task->task_state_lock, task_flags);
2749
Jeff Skirvinaa145102011-03-07 16:40:47 -07002750 /* Fail the I/O. */
2751 response = SAS_TASK_UNDELIVERED;
2752 status = SAM_STAT_TASK_ABORTED;
2753
Dan Williams6f231dd2011-07-02 22:56:22 -07002754 complete_to_host = isci_perform_error_io_completion;
Dan Williams38d88792011-06-23 14:33:48 -07002755 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002756 break;
2757
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002758 case SCI_FAILURE_RETRY_REQUIRED:
2759
2760 /* Fail the I/O so it can be retried. */
2761 response = SAS_TASK_UNDELIVERED;
Dan Williams209fae12011-06-13 17:39:44 -07002762 if (!idev)
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002763 status = SAS_DEVICE_UNKNOWN;
2764 else
2765 status = SAS_ABORTED_TASK;
2766
2767 complete_to_host = isci_perform_normal_io_completion;
Dan Williams38d88792011-06-23 14:33:48 -07002768 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002769 break;
2770
2771
Dan Williams6f231dd2011-07-02 22:56:22 -07002772 default:
2773 /* Catch any otherwise unhandled error codes here. */
2774 dev_warn(&isci_host->pdev->dev,
2775 "%s: invalid completion code: 0x%x - "
2776 "isci_request = %p\n",
2777 __func__, completion_status, request);
2778
2779 response = SAS_TASK_UNDELIVERED;
2780
2781 /* See if the device has been/is being stopped. Note
2782 * that we ignore the quiesce state, since we are
2783 * concerned about the actual device state.
2784 */
Dan Williams209fae12011-06-13 17:39:44 -07002785 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002786 status = SAS_DEVICE_UNKNOWN;
2787 else
2788 status = SAS_ABORTED_TASK;
2789
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002790 if (SAS_PROTOCOL_SMP == task->task_proto) {
Dan Williams38d88792011-06-23 14:33:48 -07002791 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002792 complete_to_host = isci_perform_normal_io_completion;
2793 } else {
Dan Williams38d88792011-06-23 14:33:48 -07002794 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002795 complete_to_host = isci_perform_error_io_completion;
2796 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002797 break;
2798 }
2799 break;
2800 }
2801
Dan Williamsddcc7e32011-06-17 10:40:43 -07002802 switch (task->task_proto) {
2803 case SAS_PROTOCOL_SSP:
2804 if (task->data_dir == DMA_NONE)
2805 break;
2806 if (task->num_scatter == 0)
2807 /* 0 indicates a single dma address */
2808 dma_unmap_single(&isci_host->pdev->dev,
2809 request->zero_scatter_daddr,
2810 task->total_xfer_len, task->data_dir);
2811 else /* unmap the sgl dma addresses */
2812 dma_unmap_sg(&isci_host->pdev->dev, task->scatter,
2813 request->num_sg_entries, task->data_dir);
2814 break;
Dan Williamse9bf7092011-06-16 16:59:56 -07002815 case SAS_PROTOCOL_SMP: {
2816 struct scatterlist *sg = &task->smp_task.smp_req;
2817 struct smp_req *smp_req;
2818 void *kaddr;
2819
2820 dma_unmap_sg(&isci_host->pdev->dev, sg, 1, DMA_TO_DEVICE);
2821
2822 /* need to swab it back in case the command buffer is re-used */
2823 kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
2824 smp_req = kaddr + sg->offset;
2825 sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
2826 kunmap_atomic(kaddr, KM_IRQ0);
2827 break;
2828 }
Dan Williamsddcc7e32011-06-17 10:40:43 -07002829 default:
2830 break;
2831 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002832
2833 /* Put the completed request on the correct list */
2834 isci_task_save_for_upper_layer_completion(isci_host, request, response,
2835 status, complete_to_host
2836 );
2837
2838 /* complete the io request to the core. */
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002839 scic_controller_complete_io(&isci_host->sci,
Dan Williams5076a1a2011-06-27 14:57:03 -07002840 request->target_device,
2841 request);
Dan Williams209fae12011-06-13 17:39:44 -07002842 isci_put_device(idev);
2843
Dan Williams67ea8382011-05-08 11:47:15 -07002844 /* set terminated handle so it cannot be completed or
Dan Williams6f231dd2011-07-02 22:56:22 -07002845 * terminated again, and to cause any calls into abort
2846 * task to recognize the already completed case.
2847 */
Dan Williams38d88792011-06-23 14:33:48 -07002848 set_bit(IREQ_TERMINATED, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002849}
Dan Williamsf1f52e72011-05-10 02:28:45 -07002850
Dan Williams9269e0e2011-05-12 07:42:17 -07002851static void scic_sds_request_started_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002852{
Dan Williams5076a1a2011-06-27 14:57:03 -07002853 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
2854 struct domain_device *dev = sci_dev_to_domain(ireq->target_device);
Dan Williamsc72086e2011-05-10 02:28:48 -07002855 struct sas_task *task;
2856
2857 /* XXX as hch said always creating an internal sas_task for tmf
2858 * requests would simplify the driver
2859 */
2860 task = ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL;
Dan Williamsf1f52e72011-05-10 02:28:45 -07002861
Dan Williams5dec6f42011-05-10 02:28:49 -07002862 /* all unaccelerated request types (non ssp or ncq) handled with
2863 * substates
Dan Williamsf1393032011-05-10 02:28:47 -07002864 */
Dan Williamsc72086e2011-05-10 02:28:48 -07002865 if (!task && dev->dev_type == SAS_END_DEV) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002866 sci_change_state(sm, SCI_REQ_TASK_WAIT_TC_COMP);
Dan Williams5dec6f42011-05-10 02:28:49 -07002867 } else if (!task &&
2868 (isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_high ||
2869 isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_low)) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002870 sci_change_state(sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED);
Dan Williamsc72086e2011-05-10 02:28:48 -07002871 } else if (task && task->task_proto == SAS_PROTOCOL_SMP) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002872 sci_change_state(sm, SCI_REQ_SMP_WAIT_RESP);
Dan Williams5dec6f42011-05-10 02:28:49 -07002873 } else if (task && sas_protocol_ata(task->task_proto) &&
2874 !task->ata_task.use_ncq) {
2875 u32 state;
2876
2877 if (task->data_dir == DMA_NONE)
Edmund Nadolskie3013702011-06-02 00:10:43 +00002878 state = SCI_REQ_STP_NON_DATA_WAIT_H2D;
Dan Williams5dec6f42011-05-10 02:28:49 -07002879 else if (task->ata_task.dma_xfer)
Edmund Nadolskie3013702011-06-02 00:10:43 +00002880 state = SCI_REQ_STP_UDMA_WAIT_TC_COMP;
Dan Williams5dec6f42011-05-10 02:28:49 -07002881 else /* PIO */
Edmund Nadolskie3013702011-06-02 00:10:43 +00002882 state = SCI_REQ_STP_PIO_WAIT_H2D;
Dan Williams5dec6f42011-05-10 02:28:49 -07002883
Edmund Nadolskie3013702011-06-02 00:10:43 +00002884 sci_change_state(sm, state);
Dan Williamsc72086e2011-05-10 02:28:48 -07002885 }
Dan Williamsf1f52e72011-05-10 02:28:45 -07002886}
2887
Dan Williams9269e0e2011-05-12 07:42:17 -07002888static void scic_sds_request_completed_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002889{
Dan Williams5076a1a2011-06-27 14:57:03 -07002890 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
2891 struct scic_sds_controller *scic = ireq->owning_controller;
Dan Williamsf1f52e72011-05-10 02:28:45 -07002892 struct isci_host *ihost = scic_to_ihost(scic);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002893
Dan Williamsf1f52e72011-05-10 02:28:45 -07002894 /* Tell the SCI_USER that the IO request is complete */
Dan Williams38d88792011-06-23 14:33:48 -07002895 if (!test_bit(IREQ_TMF, &ireq->flags))
Dan Williamsf1f52e72011-05-10 02:28:45 -07002896 isci_request_io_request_complete(ihost, ireq,
Dan Williams5076a1a2011-06-27 14:57:03 -07002897 ireq->sci_status);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002898 else
Dan Williams5076a1a2011-06-27 14:57:03 -07002899 isci_task_request_complete(ihost, ireq, ireq->sci_status);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002900}
2901
Dan Williams9269e0e2011-05-12 07:42:17 -07002902static void scic_sds_request_aborting_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002903{
Dan Williams5076a1a2011-06-27 14:57:03 -07002904 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002905
2906 /* Setting the abort bit in the Task Context is required by the silicon. */
Dan Williams5076a1a2011-06-27 14:57:03 -07002907 ireq->tc->abort = 1;
Dan Williamsc72086e2011-05-10 02:28:48 -07002908}
2909
Dan Williams9269e0e2011-05-12 07:42:17 -07002910static void scic_sds_stp_request_started_non_data_await_h2d_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07002911{
Dan Williams5076a1a2011-06-27 14:57:03 -07002912 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams5dec6f42011-05-10 02:28:49 -07002913
Dan Williams5076a1a2011-06-27 14:57:03 -07002914 scic_sds_remote_device_set_working_request(ireq->target_device,
2915 ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -07002916}
2917
Dan Williams9269e0e2011-05-12 07:42:17 -07002918static void scic_sds_stp_request_started_pio_await_h2d_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07002919{
Dan Williams5076a1a2011-06-27 14:57:03 -07002920 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams5dec6f42011-05-10 02:28:49 -07002921
Dan Williams5076a1a2011-06-27 14:57:03 -07002922 scic_sds_remote_device_set_working_request(ireq->target_device,
2923 ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -07002924}
2925
Dan Williams9269e0e2011-05-12 07:42:17 -07002926static void scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07002927{
Dan Williams5076a1a2011-06-27 14:57:03 -07002928 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams5dec6f42011-05-10 02:28:49 -07002929
Dan Williams5076a1a2011-06-27 14:57:03 -07002930 scic_sds_remote_device_set_working_request(ireq->target_device,
2931 ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -07002932}
2933
Dan Williams9269e0e2011-05-12 07:42:17 -07002934static void scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07002935{
Dan Williams5076a1a2011-06-27 14:57:03 -07002936 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
2937 struct scu_task_context *tc = ireq->tc;
Dan Williams5dec6f42011-05-10 02:28:49 -07002938 struct host_to_dev_fis *h2d_fis;
2939 enum sci_status status;
2940
2941 /* Clear the SRST bit */
Dan Williams5076a1a2011-06-27 14:57:03 -07002942 h2d_fis = &ireq->stp.cmd;
Dan Williams5dec6f42011-05-10 02:28:49 -07002943 h2d_fis->control = 0;
2944
2945 /* Clear the TC control bit */
Dan Williams312e0c22011-06-28 13:47:09 -07002946 tc->control_frame = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07002947
Dan Williams5076a1a2011-06-27 14:57:03 -07002948 status = scic_controller_continue_io(ireq);
Dan Williams79e2b6b2011-05-11 08:29:56 -07002949 WARN_ONCE(status != SCI_SUCCESS, "isci: continue io failure\n");
Dan Williams5dec6f42011-05-10 02:28:49 -07002950}
2951
Dan Williamsf1f52e72011-05-10 02:28:45 -07002952static const struct sci_base_state scic_sds_request_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002953 [SCI_REQ_INIT] = { },
2954 [SCI_REQ_CONSTRUCTED] = { },
2955 [SCI_REQ_STARTED] = {
Dan Williamsf1f52e72011-05-10 02:28:45 -07002956 .enter_state = scic_sds_request_started_state_enter,
Dan Williams5dec6f42011-05-10 02:28:49 -07002957 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002958 [SCI_REQ_STP_NON_DATA_WAIT_H2D] = {
Dan Williams5dec6f42011-05-10 02:28:49 -07002959 .enter_state = scic_sds_stp_request_started_non_data_await_h2d_completion_enter,
2960 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002961 [SCI_REQ_STP_NON_DATA_WAIT_D2H] = { },
2962 [SCI_REQ_STP_PIO_WAIT_H2D] = {
Dan Williams5dec6f42011-05-10 02:28:49 -07002963 .enter_state = scic_sds_stp_request_started_pio_await_h2d_completion_enter,
2964 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002965 [SCI_REQ_STP_PIO_WAIT_FRAME] = { },
2966 [SCI_REQ_STP_PIO_DATA_IN] = { },
2967 [SCI_REQ_STP_PIO_DATA_OUT] = { },
2968 [SCI_REQ_STP_UDMA_WAIT_TC_COMP] = { },
2969 [SCI_REQ_STP_UDMA_WAIT_D2H] = { },
2970 [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED] = {
Dan Williams5dec6f42011-05-10 02:28:49 -07002971 .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter,
2972 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002973 [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG] = {
Dan Williams5dec6f42011-05-10 02:28:49 -07002974 .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter,
2975 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002976 [SCI_REQ_STP_SOFT_RESET_WAIT_D2H] = { },
2977 [SCI_REQ_TASK_WAIT_TC_COMP] = { },
2978 [SCI_REQ_TASK_WAIT_TC_RESP] = { },
2979 [SCI_REQ_SMP_WAIT_RESP] = { },
2980 [SCI_REQ_SMP_WAIT_TC_COMP] = { },
2981 [SCI_REQ_COMPLETED] = {
Dan Williamsf1f52e72011-05-10 02:28:45 -07002982 .enter_state = scic_sds_request_completed_state_enter,
2983 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002984 [SCI_REQ_ABORTING] = {
Dan Williamsf1f52e72011-05-10 02:28:45 -07002985 .enter_state = scic_sds_request_aborting_state_enter,
2986 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002987 [SCI_REQ_FINAL] = { },
Dan Williamsf1f52e72011-05-10 02:28:45 -07002988};
2989
Edmund Nadolskie3013702011-06-02 00:10:43 +00002990static void
2991scic_sds_general_request_construct(struct scic_sds_controller *scic,
2992 struct scic_sds_remote_device *sci_dev,
Dan Williams5076a1a2011-06-27 14:57:03 -07002993 struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002994{
Dan Williams5076a1a2011-06-27 14:57:03 -07002995 sci_init_sm(&ireq->sm, scic_sds_request_state_table, SCI_REQ_INIT);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002996
Dan Williams5076a1a2011-06-27 14:57:03 -07002997 ireq->target_device = sci_dev;
2998 ireq->protocol = SCIC_NO_PROTOCOL;
2999 ireq->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003000
Dan Williams5076a1a2011-06-27 14:57:03 -07003001 ireq->sci_status = SCI_SUCCESS;
3002 ireq->scu_status = 0;
3003 ireq->post_context = 0xFFFFFFFF;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003004}
3005
3006static enum sci_status
3007scic_io_request_construct(struct scic_sds_controller *scic,
3008 struct scic_sds_remote_device *sci_dev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003009 struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003010{
3011 struct domain_device *dev = sci_dev_to_domain(sci_dev);
3012 enum sci_status status = SCI_SUCCESS;
3013
3014 /* Build the common part of the request */
Dan Williams5076a1a2011-06-27 14:57:03 -07003015 scic_sds_general_request_construct(scic, sci_dev, ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003016
Dan Williamsc72086e2011-05-10 02:28:48 -07003017 if (sci_dev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003018 return SCI_FAILURE_INVALID_REMOTE_DEVICE;
3019
3020 if (dev->dev_type == SAS_END_DEV)
Dan Williamsc72086e2011-05-10 02:28:48 -07003021 /* pass */;
3022 else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
Dan Williams5076a1a2011-06-27 14:57:03 -07003023 memset(&ireq->stp.cmd, 0, sizeof(ireq->stp.cmd));
Dan Williamsc72086e2011-05-10 02:28:48 -07003024 else if (dev_is_expander(dev))
Dan Williamse9bf7092011-06-16 16:59:56 -07003025 /* pass */;
Dan Williamsc72086e2011-05-10 02:28:48 -07003026 else
3027 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003028
Dan Williams5076a1a2011-06-27 14:57:03 -07003029 memset(ireq->tc, 0, offsetof(struct scu_task_context, sgl_pair_ab));
Dan Williamsf1f52e72011-05-10 02:28:45 -07003030
3031 return status;
3032}
3033
3034enum sci_status scic_task_request_construct(struct scic_sds_controller *scic,
3035 struct scic_sds_remote_device *sci_dev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003036 u16 io_tag, struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003037{
3038 struct domain_device *dev = sci_dev_to_domain(sci_dev);
3039 enum sci_status status = SCI_SUCCESS;
3040
3041 /* Build the common part of the request */
Dan Williams5076a1a2011-06-27 14:57:03 -07003042 scic_sds_general_request_construct(scic, sci_dev, ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003043
Dan Williamsc72086e2011-05-10 02:28:48 -07003044 if (dev->dev_type == SAS_END_DEV ||
3045 dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams5076a1a2011-06-27 14:57:03 -07003046 set_bit(IREQ_TMF, &ireq->flags);
3047 memset(ireq->tc, 0, sizeof(struct scu_task_context));
Dan Williamsc72086e2011-05-10 02:28:48 -07003048 } else
3049 status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003050
3051 return status;
3052}
3053
3054static enum sci_status isci_request_ssp_request_construct(
3055 struct isci_request *request)
3056{
3057 enum sci_status status;
3058
3059 dev_dbg(&request->isci_host->pdev->dev,
3060 "%s: request = %p\n",
3061 __func__,
3062 request);
Dan Williams5076a1a2011-06-27 14:57:03 -07003063 status = scic_io_request_construct_basic_ssp(request);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003064 return status;
3065}
3066
3067static enum sci_status isci_request_stp_request_construct(
3068 struct isci_request *request)
3069{
3070 struct sas_task *task = isci_request_access_task(request);
3071 enum sci_status status;
3072 struct host_to_dev_fis *register_fis;
3073
3074 dev_dbg(&request->isci_host->pdev->dev,
3075 "%s: request = %p\n",
3076 __func__,
3077 request);
3078
3079 /* Get the host_to_dev_fis from the core and copy
3080 * the fis from the task into it.
3081 */
3082 register_fis = isci_sata_task_to_fis_copy(task);
3083
Dan Williams5076a1a2011-06-27 14:57:03 -07003084 status = scic_io_request_construct_basic_sata(request);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003085
3086 /* Set the ncq tag in the fis, from the queue
3087 * command in the task.
3088 */
3089 if (isci_sata_is_task_ncq(task)) {
3090
3091 isci_sata_set_ncq_tag(
3092 register_fis,
3093 task
3094 );
3095 }
3096
3097 return status;
3098}
3099
Dan Williamse9bf7092011-06-16 16:59:56 -07003100static enum sci_status
3101scic_io_request_construct_smp(struct device *dev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003102 struct isci_request *ireq,
Dan Williamse9bf7092011-06-16 16:59:56 -07003103 struct sas_task *task)
Dan Williamsc72086e2011-05-10 02:28:48 -07003104{
Dan Williamse9bf7092011-06-16 16:59:56 -07003105 struct scatterlist *sg = &task->smp_task.smp_req;
Dan Williamsc72086e2011-05-10 02:28:48 -07003106 struct scic_sds_remote_device *sci_dev;
Dan Williamsc72086e2011-05-10 02:28:48 -07003107 struct scu_task_context *task_context;
Dan Williamse9bf7092011-06-16 16:59:56 -07003108 struct scic_sds_port *sci_port;
3109 struct smp_req *smp_req;
3110 void *kaddr;
3111 u8 req_len;
3112 u32 cmd;
3113
3114 kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
3115 smp_req = kaddr + sg->offset;
3116 /*
3117 * Look at the SMP requests' header fields; for certain SAS 1.x SMP
3118 * functions under SAS 2.0, a zero request length really indicates
3119 * a non-zero default length.
3120 */
3121 if (smp_req->req_len == 0) {
3122 switch (smp_req->func) {
3123 case SMP_DISCOVER:
3124 case SMP_REPORT_PHY_ERR_LOG:
3125 case SMP_REPORT_PHY_SATA:
3126 case SMP_REPORT_ROUTE_INFO:
3127 smp_req->req_len = 2;
3128 break;
3129 case SMP_CONF_ROUTE_INFO:
3130 case SMP_PHY_CONTROL:
3131 case SMP_PHY_TEST_FUNCTION:
3132 smp_req->req_len = 9;
3133 break;
3134 /* Default - zero is a valid default for 2.0. */
3135 }
3136 }
3137 req_len = smp_req->req_len;
3138 sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
3139 cmd = *(u32 *) smp_req;
3140 kunmap_atomic(kaddr, KM_IRQ0);
3141
3142 if (!dma_map_sg(dev, sg, 1, DMA_TO_DEVICE))
3143 return SCI_FAILURE;
3144
Dan Williams5076a1a2011-06-27 14:57:03 -07003145 ireq->protocol = SCIC_SMP_PROTOCOL;
Dan Williamsc72086e2011-05-10 02:28:48 -07003146
3147 /* byte swap the smp request. */
Dan Williamsc72086e2011-05-10 02:28:48 -07003148
Dan Williams5076a1a2011-06-27 14:57:03 -07003149 task_context = ireq->tc;
Dan Williamsc72086e2011-05-10 02:28:48 -07003150
Dan Williams5076a1a2011-06-27 14:57:03 -07003151 sci_dev = scic_sds_request_get_device(ireq);
3152 sci_port = scic_sds_request_get_port(ireq);
Dan Williamsc72086e2011-05-10 02:28:48 -07003153
3154 /*
3155 * Fill in the TC with the its required data
3156 * 00h
3157 */
3158 task_context->priority = 0;
3159 task_context->initiator_request = 1;
3160 task_context->connection_rate = sci_dev->connection_rate;
3161 task_context->protocol_engine_index =
3162 scic_sds_controller_get_protocol_engine_group(scic);
3163 task_context->logical_port_index = scic_sds_port_get_index(sci_port);
3164 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
3165 task_context->abort = 0;
3166 task_context->valid = SCU_TASK_CONTEXT_VALID;
3167 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
3168
3169 /* 04h */
3170 task_context->remote_node_index = sci_dev->rnc.remote_node_index;
3171 task_context->command_code = 0;
3172 task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;
3173
3174 /* 08h */
3175 task_context->link_layer_control = 0;
3176 task_context->do_not_dma_ssp_good_response = 1;
3177 task_context->strict_ordering = 0;
3178 task_context->control_frame = 1;
3179 task_context->timeout_enable = 0;
3180 task_context->block_guard_enable = 0;
3181
3182 /* 0ch */
3183 task_context->address_modifier = 0;
3184
3185 /* 10h */
Dave Jiang77d67382011-05-25 02:21:57 +00003186 task_context->ssp_command_iu_length = req_len;
Dan Williamsc72086e2011-05-10 02:28:48 -07003187
3188 /* 14h */
3189 task_context->transfer_length_bytes = 0;
3190
3191 /*
3192 * 18h ~ 30h, protocol specific
3193 * since commandIU has been build by framework at this point, we just
3194 * copy the frist DWord from command IU to this location. */
Dan Williamse9bf7092011-06-16 16:59:56 -07003195 memcpy(&task_context->type.smp, &cmd, sizeof(u32));
Dan Williamsc72086e2011-05-10 02:28:48 -07003196
3197 /*
3198 * 40h
3199 * "For SMP you could program it to zero. We would prefer that way
3200 * so that done code will be consistent." - Venki
3201 */
3202 task_context->task_phase = 0;
3203
Dan Williams5076a1a2011-06-27 14:57:03 -07003204 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williams312e0c22011-06-28 13:47:09 -07003205 (scic_sds_controller_get_protocol_engine_group(scic) <<
3206 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
3207 (scic_sds_port_get_index(sci_port) <<
3208 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
Dan Williams5076a1a2011-06-27 14:57:03 -07003209 ISCI_TAG_TCI(ireq->io_tag));
Dan Williamsc72086e2011-05-10 02:28:48 -07003210 /*
3211 * Copy the physical address for the command buffer to the SCU Task
3212 * Context command buffer should not contain command header.
3213 */
Dan Williamse9bf7092011-06-16 16:59:56 -07003214 task_context->command_iu_upper = upper_32_bits(sg_dma_address(sg));
3215 task_context->command_iu_lower = lower_32_bits(sg_dma_address(sg) + sizeof(u32));
Dan Williamsc72086e2011-05-10 02:28:48 -07003216
3217 /* SMP response comes as UF, so no need to set response IU address. */
3218 task_context->response_iu_upper = 0;
3219 task_context->response_iu_lower = 0;
Dan Williamsc72086e2011-05-10 02:28:48 -07003220
Dan Williams5076a1a2011-06-27 14:57:03 -07003221 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williamsc72086e2011-05-10 02:28:48 -07003222
3223 return SCI_SUCCESS;
3224}
3225
3226/*
Dan Williamsf1f52e72011-05-10 02:28:45 -07003227 * isci_smp_request_build() - This function builds the smp request.
3228 * @ireq: This parameter points to the isci_request allocated in the
3229 * request construct function.
3230 *
3231 * SCI_SUCCESS on successfull completion, or specific failure code.
3232 */
3233static enum sci_status isci_smp_request_build(struct isci_request *ireq)
3234{
Dan Williamsf1f52e72011-05-10 02:28:45 -07003235 struct sas_task *task = isci_request_access_task(ireq);
Dan Williamse9bf7092011-06-16 16:59:56 -07003236 struct device *dev = &ireq->isci_host->pdev->dev;
Dan Williamse9bf7092011-06-16 16:59:56 -07003237 enum sci_status status = SCI_FAILURE;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003238
Dan Williams5076a1a2011-06-27 14:57:03 -07003239 status = scic_io_request_construct_smp(dev, ireq, task);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003240 if (status != SCI_SUCCESS)
3241 dev_warn(&ireq->isci_host->pdev->dev,
3242 "%s: failed with status = %d\n",
3243 __func__,
3244 status);
3245
3246 return status;
3247}
3248
3249/**
3250 * isci_io_request_build() - This function builds the io request object.
3251 * @isci_host: This parameter specifies the ISCI host object
3252 * @request: This parameter points to the isci_request object allocated in the
3253 * request construct function.
3254 * @sci_device: This parameter is the handle for the sci core's remote device
3255 * object that is the destination for this request.
3256 *
3257 * SCI_SUCCESS on successfull completion, or specific failure code.
3258 */
Dan Williams312e0c22011-06-28 13:47:09 -07003259static enum sci_status isci_io_request_build(struct isci_host *isci_host,
3260 struct isci_request *request,
Dan Williamsdb056252011-06-17 14:18:39 -07003261 struct isci_remote_device *isci_device)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003262{
3263 enum sci_status status = SCI_SUCCESS;
3264 struct sas_task *task = isci_request_access_task(request);
3265 struct scic_sds_remote_device *sci_device = &isci_device->sci;
3266
3267 dev_dbg(&isci_host->pdev->dev,
3268 "%s: isci_device = 0x%p; request = %p, "
3269 "num_scatter = %d\n",
3270 __func__,
3271 isci_device,
3272 request,
3273 task->num_scatter);
3274
3275 /* map the sgl addresses, if present.
3276 * libata does the mapping for sata devices
3277 * before we get the request.
3278 */
3279 if (task->num_scatter &&
3280 !sas_protocol_ata(task->task_proto) &&
3281 !(SAS_PROTOCOL_SMP & task->task_proto)) {
3282
3283 request->num_sg_entries = dma_map_sg(
3284 &isci_host->pdev->dev,
3285 task->scatter,
3286 task->num_scatter,
3287 task->data_dir
3288 );
3289
3290 if (request->num_sg_entries == 0)
3291 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
3292 }
3293
Dan Williamsf1f52e72011-05-10 02:28:45 -07003294 status = scic_io_request_construct(&isci_host->sci, sci_device,
Dan Williams5076a1a2011-06-27 14:57:03 -07003295 request);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003296
3297 if (status != SCI_SUCCESS) {
3298 dev_warn(&isci_host->pdev->dev,
3299 "%s: failed request construct\n",
3300 __func__);
3301 return SCI_FAILURE;
3302 }
3303
3304 switch (task->task_proto) {
3305 case SAS_PROTOCOL_SMP:
3306 status = isci_smp_request_build(request);
3307 break;
3308 case SAS_PROTOCOL_SSP:
3309 status = isci_request_ssp_request_construct(request);
3310 break;
3311 case SAS_PROTOCOL_SATA:
3312 case SAS_PROTOCOL_STP:
3313 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
3314 status = isci_request_stp_request_construct(request);
3315 break;
3316 default:
3317 dev_warn(&isci_host->pdev->dev,
3318 "%s: unknown protocol\n", __func__);
3319 return SCI_FAILURE;
3320 }
3321
3322 return SCI_SUCCESS;
3323}
3324
Dan Williamsdb056252011-06-17 14:18:39 -07003325static struct isci_request *isci_request_from_tag(struct isci_host *ihost, u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003326{
Dan Williams0d0cf142011-06-13 00:51:30 -07003327 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003328
Dan Williamsdb056252011-06-17 14:18:39 -07003329 ireq = ihost->reqs[ISCI_TAG_TCI(tag)];
Dan Williams5076a1a2011-06-27 14:57:03 -07003330 ireq->io_tag = tag;
Dan Williams0d0cf142011-06-13 00:51:30 -07003331 ireq->io_request_completion = NULL;
Dan Williams38d88792011-06-23 14:33:48 -07003332 ireq->flags = 0;
Dan Williams0d0cf142011-06-13 00:51:30 -07003333 ireq->num_sg_entries = 0;
Dan Williams0d0cf142011-06-13 00:51:30 -07003334 INIT_LIST_HEAD(&ireq->completed_node);
3335 INIT_LIST_HEAD(&ireq->dev_node);
Dan Williams0d0cf142011-06-13 00:51:30 -07003336 isci_request_change_state(ireq, allocated);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003337
Dan Williams0d0cf142011-06-13 00:51:30 -07003338 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003339}
3340
Dan Williamsdb056252011-06-17 14:18:39 -07003341static struct isci_request *isci_io_request_from_tag(struct isci_host *ihost,
3342 struct sas_task *task,
3343 u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003344{
Dan Williams0d0cf142011-06-13 00:51:30 -07003345 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003346
Dan Williamsdb056252011-06-17 14:18:39 -07003347 ireq = isci_request_from_tag(ihost, tag);
3348 ireq->ttype_ptr.io_task_ptr = task;
3349 ireq->ttype = io_task;
3350 task->lldd_task = ireq;
3351
Dan Williams0d0cf142011-06-13 00:51:30 -07003352 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003353}
3354
Dan Williamsdb056252011-06-17 14:18:39 -07003355struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
3356 struct isci_tmf *isci_tmf,
3357 u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003358{
Dan Williams0d0cf142011-06-13 00:51:30 -07003359 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003360
Dan Williamsdb056252011-06-17 14:18:39 -07003361 ireq = isci_request_from_tag(ihost, tag);
3362 ireq->ttype_ptr.tmf_task_ptr = isci_tmf;
3363 ireq->ttype = tmf_task;
3364
Dan Williams0d0cf142011-06-13 00:51:30 -07003365 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003366}
3367
Dan Williams209fae12011-06-13 17:39:44 -07003368int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
Dan Williamsdb056252011-06-17 14:18:39 -07003369 struct sas_task *task, u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003370{
Dan Williamsf1f52e72011-05-10 02:28:45 -07003371 enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams0d0cf142011-06-13 00:51:30 -07003372 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003373 unsigned long flags;
Dan Williams0d0cf142011-06-13 00:51:30 -07003374 int ret = 0;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003375
Dan Williamsf1f52e72011-05-10 02:28:45 -07003376 /* do common allocation and init of request object. */
Dan Williamsdb056252011-06-17 14:18:39 -07003377 ireq = isci_io_request_from_tag(ihost, task, tag);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003378
Dan Williamsdb056252011-06-17 14:18:39 -07003379 status = isci_io_request_build(ihost, ireq, idev);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003380 if (status != SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -07003381 dev_warn(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003382 "%s: request_construct failed - status = 0x%x\n",
3383 __func__,
3384 status);
Dan Williamsdb056252011-06-17 14:18:39 -07003385 return status;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003386 }
3387
Dan Williams0d0cf142011-06-13 00:51:30 -07003388 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003389
Jeff Skirvin9274f452011-06-23 17:09:02 -07003390 if (test_bit(IDEV_IO_NCQERROR, &idev->flags)) {
3391
3392 if (isci_task_is_ncq_recovery(task)) {
3393
3394 /* The device is in an NCQ recovery state. Issue the
3395 * request on the task side. Note that it will
3396 * complete on the I/O request side because the
3397 * request was built that way (ie.
3398 * ireq->is_task_management_request is false).
3399 */
3400 status = scic_controller_start_task(&ihost->sci,
3401 &idev->sci,
Dan Williams5076a1a2011-06-27 14:57:03 -07003402 ireq);
Jeff Skirvin9274f452011-06-23 17:09:02 -07003403 } else {
3404 status = SCI_FAILURE;
3405 }
3406 } else {
Jeff Skirvin9274f452011-06-23 17:09:02 -07003407 /* send the request, let the core assign the IO TAG. */
3408 status = scic_controller_start_io(&ihost->sci, &idev->sci,
Dan Williams5076a1a2011-06-27 14:57:03 -07003409 ireq);
Jeff Skirvin9274f452011-06-23 17:09:02 -07003410 }
Dan Williams312e0c22011-06-28 13:47:09 -07003411
Dan Williamsf1f52e72011-05-10 02:28:45 -07003412 if (status != SCI_SUCCESS &&
3413 status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
Dan Williams0d0cf142011-06-13 00:51:30 -07003414 dev_warn(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003415 "%s: failed request start (0x%x)\n",
3416 __func__, status);
Dan Williams0d0cf142011-06-13 00:51:30 -07003417 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsdb056252011-06-17 14:18:39 -07003418 return status;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003419 }
3420
3421 /* Either I/O started OK, or the core has signaled that
3422 * the device needs a target reset.
3423 *
3424 * In either case, hold onto the I/O for later.
3425 *
3426 * Update it's status and add it to the list in the
3427 * remote device object.
3428 */
Dan Williams0d0cf142011-06-13 00:51:30 -07003429 list_add(&ireq->dev_node, &idev->reqs_in_process);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003430
3431 if (status == SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -07003432 isci_request_change_state(ireq, started);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003433 } else {
3434 /* The request did not really start in the
3435 * hardware, so clear the request handle
3436 * here so no terminations will be done.
3437 */
Dan Williams38d88792011-06-23 14:33:48 -07003438 set_bit(IREQ_TERMINATED, &ireq->flags);
Dan Williams0d0cf142011-06-13 00:51:30 -07003439 isci_request_change_state(ireq, completed);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003440 }
Dan Williams0d0cf142011-06-13 00:51:30 -07003441 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003442
3443 if (status ==
3444 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
3445 /* Signal libsas that we need the SCSI error
Dan Williams312e0c22011-06-28 13:47:09 -07003446 * handler thread to work on this I/O and that
3447 * we want a device reset.
3448 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07003449 spin_lock_irqsave(&task->task_state_lock, flags);
3450 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
3451 spin_unlock_irqrestore(&task->task_state_lock, flags);
3452
3453 /* Cause this task to be scheduled in the SCSI error
Dan Williams312e0c22011-06-28 13:47:09 -07003454 * handler thread.
3455 */
Dan Williams0d0cf142011-06-13 00:51:30 -07003456 isci_execpath_callback(ihost, task,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003457 sas_task_abort);
3458
3459 /* Change the status, since we are holding
Dan Williams312e0c22011-06-28 13:47:09 -07003460 * the I/O until it is managed by the SCSI
3461 * error handler.
3462 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07003463 status = SCI_SUCCESS;
3464 }
3465
Dan Williamsf1f52e72011-05-10 02:28:45 -07003466 return ret;
3467}