blob: 8520626b02fa4f09c29392435a50841224ef696b [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 Williams312e0c22011-06-28 13:47:09 -070064static struct scu_sgl_element_pair *to_sgl_element_pair(struct scic_sds_request *sci_req,
65 int idx)
66{
67 if (idx == 0)
68 return &sci_req->tc->sgl_pair_ab;
69 else if (idx == 1)
70 return &sci_req->tc->sgl_pair_cd;
71 else if (idx < 0)
72 return NULL;
73 else
74 return &sci_req->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,
78 struct scic_sds_request *sci_req, u32 idx)
79{
80 u32 offset;
81
82 if (idx == 0) {
83 offset = (void *) &sci_req->tc->sgl_pair_ab -
84 (void *) &scic->task_context_table[0];
85 return scic->task_context_dma + offset;
86 } else if (idx == 1) {
87 offset = (void *) &sci_req->tc->sgl_pair_cd -
88 (void *) &scic->task_context_table[0];
89 return scic->task_context_dma + offset;
90 }
91
92 return scic_io_request_get_dma_addr(sci_req, &sci_req->sg_table[idx - 2]);
93}
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 Williams5dec6f42011-05-10 02:28:49 -0700103static void scic_sds_request_build_sgl(struct scic_sds_request *sds_request)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700104{
105 struct isci_request *isci_request = sci_req_to_ireq(sds_request);
106 struct isci_host *isci_host = isci_request->isci_host;
Dan Williams312e0c22011-06-28 13:47:09 -0700107 struct scic_sds_controller *scic = &isci_host->sci;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700108 struct sas_task *task = isci_request_access_task(isci_request);
109 struct scatterlist *sg = NULL;
110 dma_addr_t dma_addr;
111 u32 sg_idx = 0;
112 struct scu_sgl_element_pair *scu_sg = NULL;
113 struct scu_sgl_element_pair *prev_sg = NULL;
114
115 if (task->num_scatter > 0) {
116 sg = task->scatter;
117
118 while (sg) {
Dan Williams312e0c22011-06-28 13:47:09 -0700119 scu_sg = to_sgl_element_pair(sds_request, sg_idx);
120 init_sgl_element(&scu_sg->A, sg);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700121 sg = sg_next(sg);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700122 if (sg) {
Dan Williams312e0c22011-06-28 13:47:09 -0700123 init_sgl_element(&scu_sg->B, sg);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700124 sg = sg_next(sg);
125 } else
Dan Williams312e0c22011-06-28 13:47:09 -0700126 memset(&scu_sg->B, 0, sizeof(scu_sg->B));
Dan Williamsf1f52e72011-05-10 02:28:45 -0700127
128 if (prev_sg) {
Dan Williams312e0c22011-06-28 13:47:09 -0700129 dma_addr = to_sgl_element_pair_dma(scic,
130 sds_request,
131 sg_idx);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700132
133 prev_sg->next_pair_upper =
134 upper_32_bits(dma_addr);
135 prev_sg->next_pair_lower =
136 lower_32_bits(dma_addr);
137 }
138
139 prev_sg = scu_sg;
140 sg_idx++;
141 }
142 } else { /* handle when no sg */
Dan Williams312e0c22011-06-28 13:47:09 -0700143 scu_sg = to_sgl_element_pair(sds_request, sg_idx);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700144
145 dma_addr = dma_map_single(&isci_host->pdev->dev,
146 task->scatter,
147 task->total_xfer_len,
148 task->data_dir);
149
150 isci_request->zero_scatter_daddr = dma_addr;
151
152 scu_sg->A.length = task->total_xfer_len;
153 scu_sg->A.address_upper = upper_32_bits(dma_addr);
154 scu_sg->A.address_lower = lower_32_bits(dma_addr);
155 }
156
157 if (scu_sg) {
158 scu_sg->next_pair_upper = 0;
159 scu_sg->next_pair_lower = 0;
160 }
161}
162
Dan Williamsf1f52e72011-05-10 02:28:45 -0700163static void scic_sds_io_request_build_ssp_command_iu(struct scic_sds_request *sci_req)
164{
165 struct ssp_cmd_iu *cmd_iu;
166 struct isci_request *ireq = sci_req_to_ireq(sci_req);
167 struct sas_task *task = isci_request_access_task(ireq);
168
169 cmd_iu = &sci_req->ssp.cmd;
170
171 memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
172 cmd_iu->add_cdb_len = 0;
173 cmd_iu->_r_a = 0;
174 cmd_iu->_r_b = 0;
175 cmd_iu->en_fburst = 0; /* unsupported */
176 cmd_iu->task_prio = task->ssp_task.task_prio;
177 cmd_iu->task_attr = task->ssp_task.task_attr;
178 cmd_iu->_r_c = 0;
179
180 sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cdb,
181 sizeof(task->ssp_task.cdb) / sizeof(u32));
182}
183
184static void scic_sds_task_request_build_ssp_task_iu(struct scic_sds_request *sci_req)
185{
186 struct ssp_task_iu *task_iu;
187 struct isci_request *ireq = sci_req_to_ireq(sci_req);
188 struct sas_task *task = isci_request_access_task(ireq);
189 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
190
191 task_iu = &sci_req->ssp.tmf;
192
193 memset(task_iu, 0, sizeof(struct ssp_task_iu));
194
195 memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
196
197 task_iu->task_func = isci_tmf->tmf_code;
198 task_iu->task_tag =
199 (ireq->ttype == tmf_task) ?
200 isci_tmf->io_tag :
201 SCI_CONTROLLER_INVALID_IO_TAG;
202}
203
204/**
205 * This method is will fill in the SCU Task Context for any type of SSP request.
206 * @sci_req:
207 * @task_context:
208 *
209 */
210static void scu_ssp_reqeust_construct_task_context(
211 struct scic_sds_request *sds_request,
212 struct scu_task_context *task_context)
213{
214 dma_addr_t dma_addr;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700215 struct scic_sds_remote_device *target_device;
216 struct scic_sds_port *target_port;
217
Dan Williamsf1f52e72011-05-10 02:28:45 -0700218 target_device = scic_sds_request_get_device(sds_request);
219 target_port = scic_sds_request_get_port(sds_request);
220
221 /* Fill in the TC with the its required data */
222 task_context->abort = 0;
223 task_context->priority = 0;
224 task_context->initiator_request = 1;
225 task_context->connection_rate = target_device->connection_rate;
226 task_context->protocol_engine_index =
227 scic_sds_controller_get_protocol_engine_group(controller);
228 task_context->logical_port_index =
229 scic_sds_port_get_index(target_port);
230 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
231 task_context->valid = SCU_TASK_CONTEXT_VALID;
232 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
233
234 task_context->remote_node_index =
235 scic_sds_remote_device_get_index(sds_request->target_device);
236 task_context->command_code = 0;
237
238 task_context->link_layer_control = 0;
239 task_context->do_not_dma_ssp_good_response = 1;
240 task_context->strict_ordering = 0;
241 task_context->control_frame = 0;
242 task_context->timeout_enable = 0;
243 task_context->block_guard_enable = 0;
244
245 task_context->address_modifier = 0;
246
247 /* task_context->type.ssp.tag = sci_req->io_tag; */
248 task_context->task_phase = 0x01;
249
Dan Williams312e0c22011-06-28 13:47:09 -0700250 sds_request->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
251 (scic_sds_controller_get_protocol_engine_group(controller) <<
252 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
253 (scic_sds_port_get_index(target_port) <<
254 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
255 ISCI_TAG_TCI(sds_request->io_tag));
Dan Williamsf1f52e72011-05-10 02:28:45 -0700256
257 /*
258 * Copy the physical address for the command buffer to the
259 * SCU Task Context
260 */
261 dma_addr = scic_io_request_get_dma_addr(sds_request,
262 &sds_request->ssp.cmd);
263
264 task_context->command_iu_upper = upper_32_bits(dma_addr);
265 task_context->command_iu_lower = lower_32_bits(dma_addr);
266
267 /*
268 * Copy the physical address for the response buffer to the
269 * SCU Task Context
270 */
271 dma_addr = scic_io_request_get_dma_addr(sds_request,
272 &sds_request->ssp.rsp);
273
274 task_context->response_iu_upper = upper_32_bits(dma_addr);
275 task_context->response_iu_lower = lower_32_bits(dma_addr);
276}
277
278/**
279 * This method is will fill in the SCU Task Context for a SSP IO request.
280 * @sci_req:
281 *
282 */
Dan Williams312e0c22011-06-28 13:47:09 -0700283static void scu_ssp_io_request_construct_task_context(struct scic_sds_request *sci_req,
284 enum dma_data_direction dir,
285 u32 len)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700286{
Dan Williams312e0c22011-06-28 13:47:09 -0700287 struct scu_task_context *task_context = sci_req->tc;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700288
289 scu_ssp_reqeust_construct_task_context(sci_req, task_context);
290
291 task_context->ssp_command_iu_length =
292 sizeof(struct ssp_cmd_iu) / sizeof(u32);
293 task_context->type.ssp.frame_type = SSP_COMMAND;
294
295 switch (dir) {
296 case DMA_FROM_DEVICE:
297 case DMA_NONE:
298 default:
299 task_context->task_type = SCU_TASK_TYPE_IOREAD;
300 break;
301 case DMA_TO_DEVICE:
302 task_context->task_type = SCU_TASK_TYPE_IOWRITE;
303 break;
304 }
305
306 task_context->transfer_length_bytes = len;
307
308 if (task_context->transfer_length_bytes > 0)
309 scic_sds_request_build_sgl(sci_req);
310}
311
Dan Williamsf1f52e72011-05-10 02:28:45 -0700312/**
313 * This method will fill in the SCU Task Context for a SSP Task request. The
314 * following important settings are utilized: -# priority ==
315 * SCU_TASK_PRIORITY_HIGH. This ensures that the task request is issued
316 * ahead of other task destined for the same Remote Node. -# task_type ==
317 * SCU_TASK_TYPE_IOREAD. This simply indicates that a normal request type
318 * (i.e. non-raw frame) is being utilized to perform task management. -#
319 * control_frame == 1. This ensures that the proper endianess is set so
320 * that the bytes are transmitted in the right order for a task frame.
321 * @sci_req: This parameter specifies the task request object being
322 * constructed.
323 *
324 */
Dan Williams312e0c22011-06-28 13:47:09 -0700325static void scu_ssp_task_request_construct_task_context(struct scic_sds_request *sci_req)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700326{
Dan Williams312e0c22011-06-28 13:47:09 -0700327 struct scu_task_context *task_context = sci_req->tc;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700328
329 scu_ssp_reqeust_construct_task_context(sci_req, task_context);
330
331 task_context->control_frame = 1;
332 task_context->priority = SCU_TASK_PRIORITY_HIGH;
333 task_context->task_type = SCU_TASK_TYPE_RAW_FRAME;
334 task_context->transfer_length_bytes = 0;
335 task_context->type.ssp.frame_type = SSP_TASK;
336 task_context->ssp_command_iu_length =
337 sizeof(struct ssp_task_iu) / sizeof(u32);
338}
339
Dan Williamsf1f52e72011-05-10 02:28:45 -0700340/**
Dan Williams5dec6f42011-05-10 02:28:49 -0700341 * This method is will fill in the SCU Task Context for any type of SATA
342 * request. This is called from the various SATA constructors.
343 * @sci_req: The general IO request object which is to be used in
344 * constructing the SCU task context.
345 * @task_context: The buffer pointer for the SCU task context which is being
346 * constructed.
Dan Williamsf1f52e72011-05-10 02:28:45 -0700347 *
Dan Williams5dec6f42011-05-10 02:28:49 -0700348 * The general io request construction is complete. The buffer assignment for
349 * the command buffer is complete. none Revisit task context construction to
350 * determine what is common for SSP/SMP/STP task context structures.
Dan Williamsf1f52e72011-05-10 02:28:45 -0700351 */
Dan Williams5dec6f42011-05-10 02:28:49 -0700352static void scu_sata_reqeust_construct_task_context(
353 struct scic_sds_request *sci_req,
354 struct scu_task_context *task_context)
355{
356 dma_addr_t dma_addr;
Dan Williams5dec6f42011-05-10 02:28:49 -0700357 struct scic_sds_remote_device *target_device;
358 struct scic_sds_port *target_port;
359
Dan Williams5dec6f42011-05-10 02:28:49 -0700360 target_device = scic_sds_request_get_device(sci_req);
361 target_port = scic_sds_request_get_port(sci_req);
362
363 /* Fill in the TC with the its required data */
364 task_context->abort = 0;
365 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
366 task_context->initiator_request = 1;
367 task_context->connection_rate = target_device->connection_rate;
368 task_context->protocol_engine_index =
369 scic_sds_controller_get_protocol_engine_group(controller);
370 task_context->logical_port_index =
371 scic_sds_port_get_index(target_port);
372 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
373 task_context->valid = SCU_TASK_CONTEXT_VALID;
374 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
375
376 task_context->remote_node_index =
377 scic_sds_remote_device_get_index(sci_req->target_device);
378 task_context->command_code = 0;
379
380 task_context->link_layer_control = 0;
381 task_context->do_not_dma_ssp_good_response = 1;
382 task_context->strict_ordering = 0;
383 task_context->control_frame = 0;
384 task_context->timeout_enable = 0;
385 task_context->block_guard_enable = 0;
386
387 task_context->address_modifier = 0;
388 task_context->task_phase = 0x01;
389
390 task_context->ssp_command_iu_length =
391 (sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);
392
393 /* Set the first word of the H2D REG FIS */
394 task_context->type.words[0] = *(u32 *)&sci_req->stp.cmd;
395
Dan Williams312e0c22011-06-28 13:47:09 -0700396 sci_req->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
397 (scic_sds_controller_get_protocol_engine_group(controller) <<
398 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
399 (scic_sds_port_get_index(target_port) <<
400 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
401 ISCI_TAG_TCI(sci_req->io_tag));
Dan Williams5dec6f42011-05-10 02:28:49 -0700402 /*
403 * Copy the physical address for the command buffer to the SCU Task
404 * Context. We must offset the command buffer by 4 bytes because the
405 * first 4 bytes are transfered in the body of the TC.
406 */
407 dma_addr = scic_io_request_get_dma_addr(sci_req,
408 ((char *) &sci_req->stp.cmd) +
409 sizeof(u32));
410
411 task_context->command_iu_upper = upper_32_bits(dma_addr);
412 task_context->command_iu_lower = lower_32_bits(dma_addr);
413
414 /* SATA Requests do not have a response buffer */
415 task_context->response_iu_upper = 0;
416 task_context->response_iu_lower = 0;
417}
418
Dan Williams312e0c22011-06-28 13:47:09 -0700419static void scu_stp_raw_request_construct_task_context(struct scic_sds_request *sci_req)
Dan Williams5dec6f42011-05-10 02:28:49 -0700420{
Dan Williams312e0c22011-06-28 13:47:09 -0700421 struct scu_task_context *task_context = sci_req->tc;
Dan Williams5dec6f42011-05-10 02:28:49 -0700422
423 scu_sata_reqeust_construct_task_context(sci_req, task_context);
424
425 task_context->control_frame = 0;
426 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
427 task_context->task_type = SCU_TASK_TYPE_SATA_RAW_FRAME;
428 task_context->type.stp.fis_type = FIS_REGH2D;
429 task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
430}
431
432static enum sci_status
433scic_sds_stp_pio_request_construct(struct scic_sds_request *sci_req,
434 bool copy_rx_frame)
435{
Dan Williamsba7cb222011-06-27 11:56:41 -0700436 struct isci_stp_request *stp_req = &sci_req->stp.req;
Dan Williams5dec6f42011-05-10 02:28:49 -0700437
Dan Williams312e0c22011-06-28 13:47:09 -0700438 scu_stp_raw_request_construct_task_context(sci_req);
Dan Williams5dec6f42011-05-10 02:28:49 -0700439
Dan Williamsba7cb222011-06-27 11:56:41 -0700440 stp_req->status = 0;
441 stp_req->sgl.offset = 0;
442 stp_req->sgl.set = SCU_SGL_ELEMENT_PAIR_A;
Dan Williams5dec6f42011-05-10 02:28:49 -0700443
444 if (copy_rx_frame) {
445 scic_sds_request_build_sgl(sci_req);
Dan Williamsba7cb222011-06-27 11:56:41 -0700446 stp_req->sgl.index = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -0700447 } else {
448 /* The user does not want the data copied to the SGL buffer location */
Dan Williamsba7cb222011-06-27 11:56:41 -0700449 stp_req->sgl.index = -1;
Dan Williams5dec6f42011-05-10 02:28:49 -0700450 }
451
452 return SCI_SUCCESS;
453}
454
455/**
456 *
457 * @sci_req: This parameter specifies the request to be constructed as an
458 * optimized request.
459 * @optimized_task_type: This parameter specifies whether the request is to be
460 * an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
461 * value of 1 indicates NCQ.
462 *
463 * This method will perform request construction common to all types of STP
464 * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
465 * returns an indication as to whether the construction was successful.
466 */
467static void scic_sds_stp_optimized_request_construct(struct scic_sds_request *sci_req,
468 u8 optimized_task_type,
469 u32 len,
470 enum dma_data_direction dir)
471{
Dan Williams312e0c22011-06-28 13:47:09 -0700472 struct scu_task_context *task_context = sci_req->tc;
Dan Williams5dec6f42011-05-10 02:28:49 -0700473
474 /* Build the STP task context structure */
475 scu_sata_reqeust_construct_task_context(sci_req, task_context);
476
477 /* Copy over the SGL elements */
478 scic_sds_request_build_sgl(sci_req);
479
480 /* Copy over the number of bytes to be transfered */
481 task_context->transfer_length_bytes = len;
482
483 if (dir == DMA_TO_DEVICE) {
484 /*
485 * The difference between the DMA IN and DMA OUT request task type
486 * values are consistent with the difference between FPDMA READ
487 * and FPDMA WRITE values. Add the supplied task type parameter
488 * to this difference to set the task type properly for this
489 * DATA OUT (WRITE) case. */
490 task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
491 - SCU_TASK_TYPE_DMA_IN);
492 } else {
493 /*
494 * For the DATA IN (READ) case, simply save the supplied
495 * optimized task type. */
496 task_context->task_type = optimized_task_type;
497 }
498}
499
500
501
Dan Williamsf1f52e72011-05-10 02:28:45 -0700502static enum sci_status
503scic_io_request_construct_sata(struct scic_sds_request *sci_req,
504 u32 len,
505 enum dma_data_direction dir,
506 bool copy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700507{
Dan Williams6f231dd2011-07-02 22:56:22 -0700508 enum sci_status status = SCI_SUCCESS;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700509 struct isci_request *ireq = sci_req_to_ireq(sci_req);
510 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700511
Dan Williamsf1f52e72011-05-10 02:28:45 -0700512 /* check for management protocols */
513 if (ireq->ttype == tmf_task) {
514 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700515
Dan Williamsf1f52e72011-05-10 02:28:45 -0700516 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
Dan Williams5dec6f42011-05-10 02:28:49 -0700517 tmf->tmf_code == isci_tmf_sata_srst_low) {
Dan Williams312e0c22011-06-28 13:47:09 -0700518 scu_stp_raw_request_construct_task_context(sci_req);
Dan Williams5dec6f42011-05-10 02:28:49 -0700519 return SCI_SUCCESS;
520 } else {
Dan Williamsf1f52e72011-05-10 02:28:45 -0700521 dev_err(scic_to_dev(sci_req->owning_controller),
522 "%s: Request 0x%p received un-handled SAT "
523 "management protocol 0x%x.\n",
524 __func__, sci_req, tmf->tmf_code);
Dan Williams6f231dd2011-07-02 22:56:22 -0700525
Dan Williamsf1f52e72011-05-10 02:28:45 -0700526 return SCI_FAILURE;
527 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700528 }
529
Dan Williamsf1f52e72011-05-10 02:28:45 -0700530 if (!sas_protocol_ata(task->task_proto)) {
531 dev_err(scic_to_dev(sci_req->owning_controller),
532 "%s: Non-ATA protocol in SATA path: 0x%x\n",
533 __func__,
534 task->task_proto);
Dan Williams6f231dd2011-07-02 22:56:22 -0700535 return SCI_FAILURE;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700536
Dan Williams6f231dd2011-07-02 22:56:22 -0700537 }
538
Dan Williamsf1f52e72011-05-10 02:28:45 -0700539 /* non data */
Dan Williams5dec6f42011-05-10 02:28:49 -0700540 if (task->data_dir == DMA_NONE) {
Dan Williams312e0c22011-06-28 13:47:09 -0700541 scu_stp_raw_request_construct_task_context(sci_req);
Dan Williams5dec6f42011-05-10 02:28:49 -0700542 return SCI_SUCCESS;
543 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700544
545 /* NCQ */
Dan Williams5dec6f42011-05-10 02:28:49 -0700546 if (task->ata_task.use_ncq) {
547 scic_sds_stp_optimized_request_construct(sci_req,
548 SCU_TASK_TYPE_FPDMAQ_READ,
549 len, dir);
550 return SCI_SUCCESS;
551 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700552
553 /* DMA */
Dan Williams5dec6f42011-05-10 02:28:49 -0700554 if (task->ata_task.dma_xfer) {
555 scic_sds_stp_optimized_request_construct(sci_req,
556 SCU_TASK_TYPE_DMA_IN,
557 len, dir);
558 return SCI_SUCCESS;
559 } else /* PIO */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700560 return scic_sds_stp_pio_request_construct(sci_req, copy);
561
562 return status;
563}
564
565static enum sci_status scic_io_request_construct_basic_ssp(struct scic_sds_request *sci_req)
566{
567 struct isci_request *ireq = sci_req_to_ireq(sci_req);
568 struct sas_task *task = isci_request_access_task(ireq);
569
570 sci_req->protocol = SCIC_SSP_PROTOCOL;
571
572 scu_ssp_io_request_construct_task_context(sci_req,
573 task->data_dir,
574 task->total_xfer_len);
575
576 scic_sds_io_request_build_ssp_command_iu(sci_req);
577
Edmund Nadolskie3013702011-06-02 00:10:43 +0000578 sci_change_state(&sci_req->sm, SCI_REQ_CONSTRUCTED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700579
580 return SCI_SUCCESS;
581}
582
583enum sci_status scic_task_request_construct_ssp(
584 struct scic_sds_request *sci_req)
585{
586 /* Construct the SSP Task SCU Task Context */
587 scu_ssp_task_request_construct_task_context(sci_req);
588
589 /* Fill in the SSP Task IU */
590 scic_sds_task_request_build_ssp_task_iu(sci_req);
591
Edmund Nadolskie3013702011-06-02 00:10:43 +0000592 sci_change_state(&sci_req->sm, SCI_REQ_CONSTRUCTED);
Dan Williams6f231dd2011-07-02 22:56:22 -0700593
594 return SCI_SUCCESS;
595}
596
Dan Williamsf1f52e72011-05-10 02:28:45 -0700597static enum sci_status scic_io_request_construct_basic_sata(struct scic_sds_request *sci_req)
Dan Williams6f231dd2011-07-02 22:56:22 -0700598{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700599 enum sci_status status;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700600 bool copy = false;
601 struct isci_request *isci_request = sci_req_to_ireq(sci_req);
602 struct sas_task *task = isci_request_access_task(isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700603
Dan Williamsf1f52e72011-05-10 02:28:45 -0700604 sci_req->protocol = SCIC_STP_PROTOCOL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700605
Dan Williamsf1f52e72011-05-10 02:28:45 -0700606 copy = (task->data_dir == DMA_NONE) ? false : true;
Dan Williams6f231dd2011-07-02 22:56:22 -0700607
Dan Williamsf1f52e72011-05-10 02:28:45 -0700608 status = scic_io_request_construct_sata(sci_req,
609 task->total_xfer_len,
610 task->data_dir,
611 copy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700612
Dan Williamsf1f52e72011-05-10 02:28:45 -0700613 if (status == SCI_SUCCESS)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000614 sci_change_state(&sci_req->sm, SCI_REQ_CONSTRUCTED);
Dan Williams6f231dd2011-07-02 22:56:22 -0700615
Dan Williamsf1f52e72011-05-10 02:28:45 -0700616 return status;
Dan Williams6f231dd2011-07-02 22:56:22 -0700617}
618
Dan Williamsf1f52e72011-05-10 02:28:45 -0700619enum sci_status scic_task_request_construct_sata(struct scic_sds_request *sci_req)
Dan Williams6f231dd2011-07-02 22:56:22 -0700620{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700621 enum sci_status status = SCI_SUCCESS;
622 struct isci_request *ireq = sci_req_to_ireq(sci_req);
Dan Williams6f231dd2011-07-02 22:56:22 -0700623
Dan Williamsf1f52e72011-05-10 02:28:45 -0700624 /* check for management protocols */
625 if (ireq->ttype == tmf_task) {
626 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700627
Dan Williamsf1f52e72011-05-10 02:28:45 -0700628 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
629 tmf->tmf_code == isci_tmf_sata_srst_low) {
Dan Williams312e0c22011-06-28 13:47:09 -0700630 scu_stp_raw_request_construct_task_context(sci_req);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700631 } else {
632 dev_err(scic_to_dev(sci_req->owning_controller),
633 "%s: Request 0x%p received un-handled SAT "
634 "Protocol 0x%x.\n",
635 __func__, sci_req, tmf->tmf_code);
636
637 return SCI_FAILURE;
638 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700639 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700640
Dan Williams5dec6f42011-05-10 02:28:49 -0700641 if (status != SCI_SUCCESS)
642 return status;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000643 sci_change_state(&sci_req->sm, SCI_REQ_CONSTRUCTED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700644
645 return status;
Dan Williams6f231dd2011-07-02 22:56:22 -0700646}
647
648/**
Dan Williamsf1f52e72011-05-10 02:28:45 -0700649 * sci_req_tx_bytes - bytes transferred when reply underruns request
650 * @sci_req: request that was terminated early
Dan Williams6f231dd2011-07-02 22:56:22 -0700651 */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700652#define SCU_TASK_CONTEXT_SRAM 0x200000
653static u32 sci_req_tx_bytes(struct scic_sds_request *sci_req)
Dan Williams6f231dd2011-07-02 22:56:22 -0700654{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700655 struct scic_sds_controller *scic = sci_req->owning_controller;
656 u32 ret_val = 0;
Dan Williams6f231dd2011-07-02 22:56:22 -0700657
Dan Williamsf1f52e72011-05-10 02:28:45 -0700658 if (readl(&scic->smu_registers->address_modifier) == 0) {
659 void __iomem *scu_reg_base = scic->scu_registers;
Dan Williams6f231dd2011-07-02 22:56:22 -0700660
Dan Williamsf1f52e72011-05-10 02:28:45 -0700661 /* get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
662 * BAR1 is the scu_registers
663 * 0x20002C = 0x200000 + 0x2c
664 * = start of task context SRAM + offset of (type.ssp.data_offset)
665 * TCi is the io_tag of struct scic_sds_request
Dan Williams67ea8382011-05-08 11:47:15 -0700666 */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700667 ret_val = readl(scu_reg_base +
668 (SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
Dan Williamsdd047c82011-06-09 11:06:58 -0700669 ((sizeof(struct scu_task_context)) * ISCI_TAG_TCI(sci_req->io_tag)));
Dan Williams67ea8382011-05-08 11:47:15 -0700670 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700671
Dan Williamsf1f52e72011-05-10 02:28:45 -0700672 return ret_val;
Dan Williams6f231dd2011-07-02 22:56:22 -0700673}
674
Piotr Sawickif4636a72011-05-10 23:50:32 +0000675enum sci_status scic_sds_request_start(struct scic_sds_request *sci_req)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700676{
Piotr Sawickif4636a72011-05-10 23:50:32 +0000677 enum sci_base_request_states state;
Dan Williams312e0c22011-06-28 13:47:09 -0700678 struct scu_task_context *tc = sci_req->tc;
679 struct scic_sds_controller *scic = sci_req->owning_controller;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000680
Edmund Nadolskie3013702011-06-02 00:10:43 +0000681 state = sci_req->sm.current_state_id;
682 if (state != SCI_REQ_CONSTRUCTED) {
Piotr Sawickif4636a72011-05-10 23:50:32 +0000683 dev_warn(scic_to_dev(scic),
684 "%s: SCIC IO Request requested to start while in wrong "
685 "state %d\n", __func__, state);
686 return SCI_FAILURE_INVALID_STATE;
687 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700688
Dan Williams312e0c22011-06-28 13:47:09 -0700689 tc->task_index = ISCI_TAG_TCI(sci_req->io_tag);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700690
Dan Williams312e0c22011-06-28 13:47:09 -0700691 switch (tc->protocol_type) {
692 case SCU_TASK_CONTEXT_PROTOCOL_SMP:
693 case SCU_TASK_CONTEXT_PROTOCOL_SSP:
694 /* SSP/SMP Frame */
695 tc->type.ssp.tag = sci_req->io_tag;
696 tc->type.ssp.target_port_transfer_tag = 0xFFFF;
697 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000698
Dan Williams312e0c22011-06-28 13:47:09 -0700699 case SCU_TASK_CONTEXT_PROTOCOL_STP:
700 /* STP/SATA Frame
701 * tc->type.stp.ncq_tag = sci_req->ncq_tag;
702 */
703 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000704
Dan Williams312e0c22011-06-28 13:47:09 -0700705 case SCU_TASK_CONTEXT_PROTOCOL_NONE:
706 /* / @todo When do we set no protocol type? */
707 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000708
Dan Williams312e0c22011-06-28 13:47:09 -0700709 default:
710 /* This should never happen since we build the IO
711 * requests */
712 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000713 }
714
Dan Williams312e0c22011-06-28 13:47:09 -0700715 /* Add to the post_context the io tag value */
716 sci_req->post_context |= ISCI_TAG_TCI(sci_req->io_tag);
717
718 /* Everything is good go ahead and change state */
719 sci_change_state(&sci_req->sm, SCI_REQ_STARTED);
720
721 return SCI_SUCCESS;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700722}
723
724enum sci_status
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700725scic_sds_io_request_terminate(struct scic_sds_request *sci_req)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700726{
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700727 enum sci_base_request_states state;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700728
Edmund Nadolskie3013702011-06-02 00:10:43 +0000729 state = sci_req->sm.current_state_id;
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700730
731 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000732 case SCI_REQ_CONSTRUCTED:
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700733 scic_sds_request_set_status(sci_req,
734 SCU_TASK_DONE_TASK_ABORT,
735 SCI_FAILURE_IO_TERMINATED);
736
Edmund Nadolskie3013702011-06-02 00:10:43 +0000737 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700738 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000739 case SCI_REQ_STARTED:
740 case SCI_REQ_TASK_WAIT_TC_COMP:
741 case SCI_REQ_SMP_WAIT_RESP:
742 case SCI_REQ_SMP_WAIT_TC_COMP:
743 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
744 case SCI_REQ_STP_UDMA_WAIT_D2H:
745 case SCI_REQ_STP_NON_DATA_WAIT_H2D:
746 case SCI_REQ_STP_NON_DATA_WAIT_D2H:
747 case SCI_REQ_STP_PIO_WAIT_H2D:
748 case SCI_REQ_STP_PIO_WAIT_FRAME:
749 case SCI_REQ_STP_PIO_DATA_IN:
750 case SCI_REQ_STP_PIO_DATA_OUT:
751 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
752 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
753 case SCI_REQ_STP_SOFT_RESET_WAIT_D2H:
754 sci_change_state(&sci_req->sm, SCI_REQ_ABORTING);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700755 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000756 case SCI_REQ_TASK_WAIT_TC_RESP:
757 sci_change_state(&sci_req->sm, SCI_REQ_ABORTING);
758 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700759 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000760 case SCI_REQ_ABORTING:
761 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700762 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000763 case SCI_REQ_COMPLETED:
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700764 default:
765 dev_warn(scic_to_dev(sci_req->owning_controller),
766 "%s: SCIC IO Request requested to abort while in wrong "
767 "state %d\n",
768 __func__,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000769 sci_req->sm.current_state_id);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700770 break;
771 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700772
773 return SCI_FAILURE_INVALID_STATE;
774}
775
Dan Williams79e2b6b2011-05-11 08:29:56 -0700776enum sci_status scic_sds_request_complete(struct scic_sds_request *sci_req)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700777{
Dan Williams79e2b6b2011-05-11 08:29:56 -0700778 enum sci_base_request_states state;
779 struct scic_sds_controller *scic = sci_req->owning_controller;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700780
Edmund Nadolskie3013702011-06-02 00:10:43 +0000781 state = sci_req->sm.current_state_id;
782 if (WARN_ONCE(state != SCI_REQ_COMPLETED,
Dan Williams79e2b6b2011-05-11 08:29:56 -0700783 "isci: request completion from wrong state (%d)\n", state))
784 return SCI_FAILURE_INVALID_STATE;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700785
Dan Williams79e2b6b2011-05-11 08:29:56 -0700786 if (sci_req->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX)
787 scic_sds_controller_release_frame(scic,
788 sci_req->saved_rx_frame_index);
789
790 /* XXX can we just stop the machine and remove the 'final' state? */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000791 sci_change_state(&sci_req->sm, SCI_REQ_FINAL);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700792 return SCI_SUCCESS;
793}
794
795enum sci_status scic_sds_io_request_event_handler(struct scic_sds_request *sci_req,
796 u32 event_code)
797{
798 enum sci_base_request_states state;
799 struct scic_sds_controller *scic = sci_req->owning_controller;
800
Edmund Nadolskie3013702011-06-02 00:10:43 +0000801 state = sci_req->sm.current_state_id;
Dan Williams79e2b6b2011-05-11 08:29:56 -0700802
Edmund Nadolskie3013702011-06-02 00:10:43 +0000803 if (state != SCI_REQ_STP_PIO_DATA_IN) {
Dan Williams79e2b6b2011-05-11 08:29:56 -0700804 dev_warn(scic_to_dev(scic), "%s: (%x) in wrong state %d\n",
805 __func__, event_code, state);
806
807 return SCI_FAILURE_INVALID_STATE;
808 }
809
810 switch (scu_get_event_specifier(event_code)) {
811 case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
812 /* We are waiting for data and the SCU has R_ERR the data frame.
813 * Go back to waiting for the D2H Register FIS
814 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000815 sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700816 return SCI_SUCCESS;
817 default:
818 dev_err(scic_to_dev(scic),
819 "%s: pio request unexpected event %#x\n",
820 __func__, event_code);
821
822 /* TODO Should we fail the PIO request when we get an
823 * unexpected event?
824 */
825 return SCI_FAILURE;
826 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700827}
828
Dan Williamsf1f52e72011-05-10 02:28:45 -0700829/*
830 * This function copies response data for requests returning response data
831 * instead of sense data.
832 * @sci_req: This parameter specifies the request object for which to copy
833 * the response data.
834 */
Dan Williamsf1393032011-05-10 02:28:47 -0700835static void scic_sds_io_request_copy_response(struct scic_sds_request *sci_req)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700836{
837 void *resp_buf;
838 u32 len;
839 struct ssp_response_iu *ssp_response;
840 struct isci_request *ireq = sci_req_to_ireq(sci_req);
841 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
842
843 ssp_response = &sci_req->ssp.rsp;
844
845 resp_buf = &isci_tmf->resp.resp_iu;
846
847 len = min_t(u32,
848 SSP_RESP_IU_MAX_SIZE,
849 be32_to_cpu(ssp_response->response_data_len));
850
851 memcpy(resp_buf, ssp_response->resp_data, len);
852}
853
Edmund Nadolskie3013702011-06-02 00:10:43 +0000854static enum sci_status
855request_started_state_tc_event(struct scic_sds_request *sci_req,
856 u32 completion_code)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700857{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700858 struct ssp_response_iu *resp_iu;
Dan Williamsa7e255a2011-05-11 08:27:47 -0700859 u8 datapres;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700860
Dan Williamsa7e255a2011-05-11 08:27:47 -0700861 /* TODO: Any SDMA return code of other than 0 is bad decode 0x003C0000
862 * to determine SDMA status
Dan Williamsf1f52e72011-05-10 02:28:45 -0700863 */
864 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
865 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
866 scic_sds_request_set_status(sci_req,
867 SCU_TASK_DONE_GOOD,
868 SCI_SUCCESS);
869 break;
Dan Williamsa7e255a2011-05-11 08:27:47 -0700870 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP): {
871 /* There are times when the SCU hardware will return an early
Dan Williamsf1f52e72011-05-10 02:28:45 -0700872 * response because the io request specified more data than is
873 * returned by the target device (mode pages, inquiry data,
874 * etc.). We must check the response stats to see if this is
875 * truly a failed request or a good request that just got
876 * completed early.
877 */
878 struct ssp_response_iu *resp = &sci_req->ssp.rsp;
879 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
880
881 sci_swab32_cpy(&sci_req->ssp.rsp,
882 &sci_req->ssp.rsp,
883 word_cnt);
884
885 if (resp->status == 0) {
Dan Williamsa7e255a2011-05-11 08:27:47 -0700886 scic_sds_request_set_status(sci_req,
887 SCU_TASK_DONE_GOOD,
888 SCI_SUCCESS_IO_DONE_EARLY);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700889 } else {
Dan Williamsa7e255a2011-05-11 08:27:47 -0700890 scic_sds_request_set_status(sci_req,
891 SCU_TASK_DONE_CHECK_RESPONSE,
892 SCI_FAILURE_IO_RESPONSE_VALID);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700893 }
Dan Williamsa7e255a2011-05-11 08:27:47 -0700894 break;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700895 }
Dan Williamsa7e255a2011-05-11 08:27:47 -0700896 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE): {
Dan Williamsf1f52e72011-05-10 02:28:45 -0700897 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
898
899 sci_swab32_cpy(&sci_req->ssp.rsp,
900 &sci_req->ssp.rsp,
901 word_cnt);
902
903 scic_sds_request_set_status(sci_req,
904 SCU_TASK_DONE_CHECK_RESPONSE,
905 SCI_FAILURE_IO_RESPONSE_VALID);
906 break;
907 }
908
909 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -0700910 /* TODO With TASK_DONE_RESP_LEN_ERR is the response frame
Dan Williamsf1f52e72011-05-10 02:28:45 -0700911 * guaranteed to be received before this completion status is
912 * posted?
913 */
914 resp_iu = &sci_req->ssp.rsp;
915 datapres = resp_iu->datapres;
916
Dan Williamsa7e255a2011-05-11 08:27:47 -0700917 if (datapres == 1 || datapres == 2) {
918 scic_sds_request_set_status(sci_req,
919 SCU_TASK_DONE_CHECK_RESPONSE,
920 SCI_FAILURE_IO_RESPONSE_VALID);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700921 } else
Dan Williamsa7e255a2011-05-11 08:27:47 -0700922 scic_sds_request_set_status(sci_req,
923 SCU_TASK_DONE_GOOD,
924 SCI_SUCCESS);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700925 break;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700926 /* only stp device gets suspended. */
927 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
928 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
929 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
930 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
931 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
932 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
933 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
934 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
935 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
936 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
937 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
938 if (sci_req->protocol == SCIC_STP_PROTOCOL) {
Dan Williamsa7e255a2011-05-11 08:27:47 -0700939 scic_sds_request_set_status(sci_req,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700940 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
941 SCU_COMPLETION_TL_STATUS_SHIFT,
942 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
943 } else {
Dan Williamsa7e255a2011-05-11 08:27:47 -0700944 scic_sds_request_set_status(sci_req,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700945 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
946 SCU_COMPLETION_TL_STATUS_SHIFT,
947 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
948 }
949 break;
950
951 /* both stp/ssp device gets suspended */
952 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
953 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
954 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
955 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
956 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
957 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
958 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
959 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
960 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
961 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
Dan Williamsa7e255a2011-05-11 08:27:47 -0700962 scic_sds_request_set_status(sci_req,
963 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
964 SCU_COMPLETION_TL_STATUS_SHIFT,
965 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700966 break;
967
968 /* neither ssp nor stp gets suspended. */
969 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
970 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
971 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
972 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
973 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
974 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
975 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
976 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
977 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
978 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
979 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
980 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
981 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
982 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
983 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
984 default:
985 scic_sds_request_set_status(
986 sci_req,
987 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
988 SCU_COMPLETION_TL_STATUS_SHIFT,
989 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
990 break;
991 }
992
993 /*
994 * TODO: This is probably wrong for ACK/NAK timeout conditions
995 */
996
997 /* In all cases we will treat this as the completion of the IO req. */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000998 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700999 return SCI_SUCCESS;
1000}
1001
Edmund Nadolskie3013702011-06-02 00:10:43 +00001002static enum sci_status
1003request_aborting_state_tc_event(struct scic_sds_request *sci_req,
1004 u32 completion_code)
Dan Williamsf1f52e72011-05-10 02:28:45 -07001005{
1006 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1007 case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
1008 case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001009 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_TASK_ABORT,
1010 SCI_FAILURE_IO_TERMINATED);
Dan Williamsf1f52e72011-05-10 02:28:45 -07001011
Edmund Nadolskie3013702011-06-02 00:10:43 +00001012 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsf1f52e72011-05-10 02:28:45 -07001013 break;
1014
1015 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001016 /* Unless we get some strange error wait for the task abort to complete
1017 * TODO: Should there be a state change for this completion?
1018 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07001019 break;
1020 }
1021
1022 return SCI_SUCCESS;
1023}
1024
Dan Williamsa7e255a2011-05-11 08:27:47 -07001025static enum sci_status ssp_task_request_await_tc_event(struct scic_sds_request *sci_req,
1026 u32 completion_code)
Dan Williamsf1393032011-05-10 02:28:47 -07001027{
1028 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1029 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1030 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
1031 SCI_SUCCESS);
1032
Edmund Nadolskie3013702011-06-02 00:10:43 +00001033 sci_change_state(&sci_req->sm, SCI_REQ_TASK_WAIT_TC_RESP);
Dan Williamsf1393032011-05-10 02:28:47 -07001034 break;
Dan Williamsf1393032011-05-10 02:28:47 -07001035 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001036 /* Currently, the decision is to simply allow the task request
1037 * to timeout if the task IU wasn't received successfully.
1038 * There is a potential for receiving multiple task responses if
1039 * we decide to send the task IU again.
1040 */
Dan Williamsf1393032011-05-10 02:28:47 -07001041 dev_warn(scic_to_dev(sci_req->owning_controller),
1042 "%s: TaskRequest:0x%p CompletionCode:%x - "
Dan Williamsa7e255a2011-05-11 08:27:47 -07001043 "ACK/NAK timeout\n", __func__, sci_req,
Dan Williamsf1393032011-05-10 02:28:47 -07001044 completion_code);
1045
Edmund Nadolskie3013702011-06-02 00:10:43 +00001046 sci_change_state(&sci_req->sm, SCI_REQ_TASK_WAIT_TC_RESP);
Dan Williamsf1393032011-05-10 02:28:47 -07001047 break;
Dan Williamsf1393032011-05-10 02:28:47 -07001048 default:
Edmund Nadolskie3013702011-06-02 00:10:43 +00001049 /*
1050 * All other completion status cause the IO to be complete.
1051 * If a NAK was received, then it is up to the user to retry
1052 * the request.
Dan Williamsa7e255a2011-05-11 08:27:47 -07001053 */
1054 scic_sds_request_set_status(sci_req,
Dan Williamsf1393032011-05-10 02:28:47 -07001055 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
Dan Williamsa7e255a2011-05-11 08:27:47 -07001056 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williamsf1393032011-05-10 02:28:47 -07001057
Edmund Nadolskie3013702011-06-02 00:10:43 +00001058 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsf1393032011-05-10 02:28:47 -07001059 break;
1060 }
1061
1062 return SCI_SUCCESS;
1063}
1064
Edmund Nadolskie3013702011-06-02 00:10:43 +00001065static enum sci_status
1066smp_request_await_response_tc_event(struct scic_sds_request *sci_req,
1067 u32 completion_code)
Dan Williamsc72086e2011-05-10 02:28:48 -07001068{
1069 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1070 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001071 /* In the AWAIT RESPONSE state, any TC completion is
1072 * unexpected. but if the TC has success status, we
1073 * complete the IO anyway.
1074 */
Dan Williams5dec6f42011-05-10 02:28:49 -07001075 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
1076 SCI_SUCCESS);
Dan Williamsc72086e2011-05-10 02:28:48 -07001077
Edmund Nadolskie3013702011-06-02 00:10:43 +00001078 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001079 break;
1080
1081 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1082 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1083 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1084 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001085 /* These status has been seen in a specific LSI
1086 * expander, which sometimes is not able to send smp
1087 * response within 2 ms. This causes our hardware break
1088 * the connection and set TC completion with one of
1089 * these SMP_XXX_XX_ERR status. For these type of error,
1090 * we ask scic user to retry the request.
1091 */
Dan Williams5dec6f42011-05-10 02:28:49 -07001092 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_SMP_RESP_TO_ERR,
1093 SCI_FAILURE_RETRY_REQUIRED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001094
Edmund Nadolskie3013702011-06-02 00:10:43 +00001095 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001096 break;
1097
1098 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001099 /* All other completion status cause the IO to be complete. If a NAK
1100 * was received, then it is up to the user to retry the request
1101 */
1102 scic_sds_request_set_status(sci_req,
1103 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1104 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williamsc72086e2011-05-10 02:28:48 -07001105
Edmund Nadolskie3013702011-06-02 00:10:43 +00001106 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001107 break;
1108 }
1109
1110 return SCI_SUCCESS;
1111}
1112
Edmund Nadolskie3013702011-06-02 00:10:43 +00001113static enum sci_status
1114smp_request_await_tc_event(struct scic_sds_request *sci_req,
1115 u32 completion_code)
Dan Williamsc72086e2011-05-10 02:28:48 -07001116{
1117 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1118 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams5dec6f42011-05-10 02:28:49 -07001119 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
1120 SCI_SUCCESS);
Dan Williamsc72086e2011-05-10 02:28:48 -07001121
Edmund Nadolskie3013702011-06-02 00:10:43 +00001122 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001123 break;
Dan Williamsc72086e2011-05-10 02:28:48 -07001124 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001125 /* All other completion status cause the IO to be
1126 * complete. If a NAK was received, then it is up to
1127 * the user to retry the request.
1128 */
1129 scic_sds_request_set_status(sci_req,
1130 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1131 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williamsc72086e2011-05-10 02:28:48 -07001132
Edmund Nadolskie3013702011-06-02 00:10:43 +00001133 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001134 break;
1135 }
1136
1137 return SCI_SUCCESS;
1138}
1139
Dan Williams5dec6f42011-05-10 02:28:49 -07001140void scic_stp_io_request_set_ncq_tag(struct scic_sds_request *req,
1141 u16 ncq_tag)
1142{
1143 /**
1144 * @note This could be made to return an error to the user if the user
1145 * attempts to set the NCQ tag in the wrong state.
1146 */
Dan Williams312e0c22011-06-28 13:47:09 -07001147 req->tc->type.stp.ncq_tag = ncq_tag;
Dan Williams5dec6f42011-05-10 02:28:49 -07001148}
1149
Dan Williamsba7cb222011-06-27 11:56:41 -07001150static struct scu_sgl_element *pio_sgl_next(struct isci_stp_request *stp_req)
Dan Williams5dec6f42011-05-10 02:28:49 -07001151{
Dan Williams312e0c22011-06-28 13:47:09 -07001152 struct scu_sgl_element *sgl;
1153 struct scu_sgl_element_pair *sgl_pair;
Dan Williams5dec6f42011-05-10 02:28:49 -07001154 struct scic_sds_request *sci_req = to_sci_req(stp_req);
Dan Williamsba7cb222011-06-27 11:56:41 -07001155 struct isci_stp_pio_sgl *pio_sgl = &stp_req->sgl;
Dan Williams5dec6f42011-05-10 02:28:49 -07001156
Dan Williamsba7cb222011-06-27 11:56:41 -07001157 sgl_pair = to_sgl_element_pair(sci_req, pio_sgl->index);
Dan Williams312e0c22011-06-28 13:47:09 -07001158 if (!sgl_pair)
1159 sgl = NULL;
Dan Williamsba7cb222011-06-27 11:56:41 -07001160 else if (pio_sgl->set == SCU_SGL_ELEMENT_PAIR_A) {
Dan Williams312e0c22011-06-28 13:47:09 -07001161 if (sgl_pair->B.address_lower == 0 &&
1162 sgl_pair->B.address_upper == 0) {
1163 sgl = NULL;
Dan Williams5dec6f42011-05-10 02:28:49 -07001164 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001165 pio_sgl->set = SCU_SGL_ELEMENT_PAIR_B;
Dan Williams312e0c22011-06-28 13:47:09 -07001166 sgl = &sgl_pair->B;
Dan Williams5dec6f42011-05-10 02:28:49 -07001167 }
1168 } else {
Dan Williams312e0c22011-06-28 13:47:09 -07001169 if (sgl_pair->next_pair_lower == 0 &&
1170 sgl_pair->next_pair_upper == 0) {
1171 sgl = NULL;
Dan Williams5dec6f42011-05-10 02:28:49 -07001172 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001173 pio_sgl->index++;
1174 pio_sgl->set = SCU_SGL_ELEMENT_PAIR_A;
1175 sgl_pair = to_sgl_element_pair(sci_req, pio_sgl->index);
Dan Williams312e0c22011-06-28 13:47:09 -07001176 sgl = &sgl_pair->A;
Dan Williams5dec6f42011-05-10 02:28:49 -07001177 }
1178 }
1179
Dan Williams312e0c22011-06-28 13:47:09 -07001180 return sgl;
Dan Williams5dec6f42011-05-10 02:28:49 -07001181}
1182
Edmund Nadolskie3013702011-06-02 00:10:43 +00001183static enum sci_status
1184stp_request_non_data_await_h2d_tc_event(struct scic_sds_request *sci_req,
1185 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001186{
1187 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1188 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001189 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
1190 SCI_SUCCESS);
Dan Williams5dec6f42011-05-10 02:28:49 -07001191
Edmund Nadolskie3013702011-06-02 00:10:43 +00001192 sci_change_state(&sci_req->sm, SCI_REQ_STP_NON_DATA_WAIT_D2H);
Dan Williams5dec6f42011-05-10 02:28:49 -07001193 break;
1194
1195 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001196 /* All other completion status cause the IO to be
1197 * complete. If a NAK was received, then it is up to
1198 * the user to retry the request.
1199 */
1200 scic_sds_request_set_status(sci_req,
1201 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1202 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williams5dec6f42011-05-10 02:28:49 -07001203
Edmund Nadolskie3013702011-06-02 00:10:43 +00001204 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001205 break;
1206 }
1207
1208 return SCI_SUCCESS;
1209}
1210
Dan Williams5dec6f42011-05-10 02:28:49 -07001211#define SCU_MAX_FRAME_BUFFER_SIZE 0x400 /* 1K is the maximum SCU frame data payload */
1212
1213/* transmit DATA_FIS from (current sgl + offset) for input
1214 * parameter length. current sgl and offset is alreay stored in the IO request
1215 */
1216static enum sci_status scic_sds_stp_request_pio_data_out_trasmit_data_frame(
1217 struct scic_sds_request *sci_req,
1218 u32 length)
1219{
Dan Williamsba7cb222011-06-27 11:56:41 -07001220 struct isci_stp_request *stp_req = &sci_req->stp.req;
Dan Williams312e0c22011-06-28 13:47:09 -07001221 struct scu_task_context *task_context = sci_req->tc;
1222 struct scu_sgl_element_pair *sgl_pair;
Dan Williams5dec6f42011-05-10 02:28:49 -07001223 struct scu_sgl_element *current_sgl;
1224
1225 /* Recycle the TC and reconstruct it for sending out DATA FIS containing
1226 * for the data from current_sgl+offset for the input length
1227 */
Dan Williamsba7cb222011-06-27 11:56:41 -07001228 sgl_pair = to_sgl_element_pair(sci_req, stp_req->sgl.index);
1229 if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A)
Dan Williams312e0c22011-06-28 13:47:09 -07001230 current_sgl = &sgl_pair->A;
Dan Williams5dec6f42011-05-10 02:28:49 -07001231 else
Dan Williams312e0c22011-06-28 13:47:09 -07001232 current_sgl = &sgl_pair->B;
Dan Williams5dec6f42011-05-10 02:28:49 -07001233
1234 /* update the TC */
1235 task_context->command_iu_upper = current_sgl->address_upper;
1236 task_context->command_iu_lower = current_sgl->address_lower;
1237 task_context->transfer_length_bytes = length;
1238 task_context->type.stp.fis_type = FIS_DATA;
1239
1240 /* send the new TC out. */
1241 return scic_controller_continue_io(sci_req);
1242}
1243
1244static enum sci_status scic_sds_stp_request_pio_data_out_transmit_data(struct scic_sds_request *sci_req)
1245{
Dan Williamsba7cb222011-06-27 11:56:41 -07001246 struct isci_stp_request *stp_req = &sci_req->stp.req;
Dan Williams312e0c22011-06-28 13:47:09 -07001247 struct scu_sgl_element_pair *sgl_pair;
Dan Williamsba7cb222011-06-27 11:56:41 -07001248 struct scu_sgl_element *sgl;
1249 enum sci_status status;
1250 u32 offset;
1251 u32 len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001252
Dan Williamsba7cb222011-06-27 11:56:41 -07001253 offset = stp_req->sgl.offset;
1254 sgl_pair = to_sgl_element_pair(sci_req, stp_req->sgl.index);
Dan Williams312e0c22011-06-28 13:47:09 -07001255 if (WARN_ONCE(!sgl_pair, "%s: null sgl element", __func__))
1256 return SCI_FAILURE;
Dan Williams5dec6f42011-05-10 02:28:49 -07001257
Dan Williamsba7cb222011-06-27 11:56:41 -07001258 if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A) {
1259 sgl = &sgl_pair->A;
1260 len = sgl_pair->A.length - offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001261 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001262 sgl = &sgl_pair->B;
1263 len = sgl_pair->B.length - offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001264 }
1265
Dan Williamsba7cb222011-06-27 11:56:41 -07001266 if (stp_req->pio_len == 0)
1267 return SCI_SUCCESS;
Dan Williams5dec6f42011-05-10 02:28:49 -07001268
Dan Williamsba7cb222011-06-27 11:56:41 -07001269 if (stp_req->pio_len >= len) {
1270 status = scic_sds_stp_request_pio_data_out_trasmit_data_frame(sci_req, len);
1271 if (status != SCI_SUCCESS)
1272 return status;
1273 stp_req->pio_len -= len;
Dan Williams5dec6f42011-05-10 02:28:49 -07001274
Dan Williamsba7cb222011-06-27 11:56:41 -07001275 /* update the current sgl, offset and save for future */
1276 sgl = pio_sgl_next(stp_req);
1277 offset = 0;
1278 } else if (stp_req->pio_len < len) {
1279 scic_sds_stp_request_pio_data_out_trasmit_data_frame(sci_req, stp_req->pio_len);
1280
1281 /* Sgl offset will be adjusted and saved for future */
1282 offset += stp_req->pio_len;
1283 sgl->address_lower += stp_req->pio_len;
1284 stp_req->pio_len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001285 }
1286
Dan Williamsba7cb222011-06-27 11:56:41 -07001287 stp_req->sgl.offset = offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001288
1289 return status;
1290}
1291
1292/**
1293 *
1294 * @stp_request: The request that is used for the SGL processing.
1295 * @data_buffer: The buffer of data to be copied.
1296 * @length: The length of the data transfer.
1297 *
1298 * Copy the data from the buffer for the length specified to the IO reqeust SGL
1299 * specified data region. enum sci_status
1300 */
1301static enum sci_status
Dan Williamsba7cb222011-06-27 11:56:41 -07001302scic_sds_stp_request_pio_data_in_copy_data_buffer(struct isci_stp_request *stp_req,
Dan Williams5dec6f42011-05-10 02:28:49 -07001303 u8 *data_buf, u32 len)
1304{
1305 struct scic_sds_request *sci_req;
1306 struct isci_request *ireq;
1307 u8 *src_addr;
1308 int copy_len;
1309 struct sas_task *task;
1310 struct scatterlist *sg;
1311 void *kaddr;
1312 int total_len = len;
1313
1314 sci_req = to_sci_req(stp_req);
1315 ireq = sci_req_to_ireq(sci_req);
1316 task = isci_request_access_task(ireq);
1317 src_addr = data_buf;
1318
1319 if (task->num_scatter > 0) {
1320 sg = task->scatter;
1321
1322 while (total_len > 0) {
1323 struct page *page = sg_page(sg);
1324
1325 copy_len = min_t(int, total_len, sg_dma_len(sg));
1326 kaddr = kmap_atomic(page, KM_IRQ0);
1327 memcpy(kaddr + sg->offset, src_addr, copy_len);
1328 kunmap_atomic(kaddr, KM_IRQ0);
1329 total_len -= copy_len;
1330 src_addr += copy_len;
1331 sg = sg_next(sg);
1332 }
1333 } else {
1334 BUG_ON(task->total_xfer_len < total_len);
1335 memcpy(task->scatter, src_addr, total_len);
1336 }
1337
1338 return SCI_SUCCESS;
1339}
1340
1341/**
1342 *
1343 * @sci_req: The PIO DATA IN request that is to receive the data.
1344 * @data_buffer: The buffer to copy from.
1345 *
1346 * Copy the data buffer to the io request data region. enum sci_status
1347 */
1348static enum sci_status scic_sds_stp_request_pio_data_in_copy_data(
Dan Williamsba7cb222011-06-27 11:56:41 -07001349 struct isci_stp_request *stp_req,
Dan Williams5dec6f42011-05-10 02:28:49 -07001350 u8 *data_buffer)
1351{
1352 enum sci_status status;
1353
1354 /*
1355 * If there is less than 1K remaining in the transfer request
1356 * copy just the data for the transfer */
Dan Williamsba7cb222011-06-27 11:56:41 -07001357 if (stp_req->pio_len < SCU_MAX_FRAME_BUFFER_SIZE) {
Dan Williams5dec6f42011-05-10 02:28:49 -07001358 status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
Dan Williamsba7cb222011-06-27 11:56:41 -07001359 stp_req, data_buffer, stp_req->pio_len);
Dan Williams5dec6f42011-05-10 02:28:49 -07001360
1361 if (status == SCI_SUCCESS)
Dan Williamsba7cb222011-06-27 11:56:41 -07001362 stp_req->pio_len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001363 } else {
1364 /* We are transfering the whole frame so copy */
1365 status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
Dan Williamsba7cb222011-06-27 11:56:41 -07001366 stp_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
Dan Williams5dec6f42011-05-10 02:28:49 -07001367
1368 if (status == SCI_SUCCESS)
Dan Williamsba7cb222011-06-27 11:56:41 -07001369 stp_req->pio_len -= SCU_MAX_FRAME_BUFFER_SIZE;
Dan Williams5dec6f42011-05-10 02:28:49 -07001370 }
1371
1372 return status;
1373}
1374
Edmund Nadolskie3013702011-06-02 00:10:43 +00001375static enum sci_status
1376stp_request_pio_await_h2d_completion_tc_event(struct scic_sds_request *sci_req,
1377 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001378{
1379 enum sci_status status = SCI_SUCCESS;
1380
1381 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1382 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Edmund Nadolskie3013702011-06-02 00:10:43 +00001383 scic_sds_request_set_status(sci_req,
1384 SCU_TASK_DONE_GOOD,
1385 SCI_SUCCESS);
Dan Williams5dec6f42011-05-10 02:28:49 -07001386
Edmund Nadolskie3013702011-06-02 00:10:43 +00001387 sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams5dec6f42011-05-10 02:28:49 -07001388 break;
1389
1390 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001391 /* All other completion status cause the IO to be
1392 * complete. If a NAK was received, then it is up to
1393 * the user to retry the request.
1394 */
1395 scic_sds_request_set_status(sci_req,
1396 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1397 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williams5dec6f42011-05-10 02:28:49 -07001398
Edmund Nadolskie3013702011-06-02 00:10:43 +00001399 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001400 break;
1401 }
1402
1403 return status;
1404}
1405
Edmund Nadolskie3013702011-06-02 00:10:43 +00001406static enum sci_status
1407pio_data_out_tx_done_tc_event(struct scic_sds_request *sci_req,
1408 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001409{
1410 enum sci_status status = SCI_SUCCESS;
1411 bool all_frames_transferred = false;
Dan Williamsba7cb222011-06-27 11:56:41 -07001412 struct isci_stp_request *stp_req = &sci_req->stp.req;
Dan Williams5dec6f42011-05-10 02:28:49 -07001413
1414 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1415 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1416 /* Transmit data */
Dan Williamsba7cb222011-06-27 11:56:41 -07001417 if (stp_req->pio_len != 0) {
Dan Williams5dec6f42011-05-10 02:28:49 -07001418 status = scic_sds_stp_request_pio_data_out_transmit_data(sci_req);
1419 if (status == SCI_SUCCESS) {
Dan Williamsba7cb222011-06-27 11:56:41 -07001420 if (stp_req->pio_len == 0)
Dan Williams5dec6f42011-05-10 02:28:49 -07001421 all_frames_transferred = true;
1422 }
Dan Williamsba7cb222011-06-27 11:56:41 -07001423 } else if (stp_req->pio_len == 0) {
Dan Williams5dec6f42011-05-10 02:28:49 -07001424 /*
1425 * this will happen if the all data is written at the
1426 * first time after the pio setup fis is received
1427 */
1428 all_frames_transferred = true;
1429 }
1430
1431 /* all data transferred. */
1432 if (all_frames_transferred) {
1433 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001434 * Change the state to SCI_REQ_STP_PIO_DATA_IN
Dan Williams5dec6f42011-05-10 02:28:49 -07001435 * and wait for PIO_SETUP fis / or D2H REg fis. */
Edmund Nadolskie3013702011-06-02 00:10:43 +00001436 sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams5dec6f42011-05-10 02:28:49 -07001437 }
1438 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001439
Dan Williams5dec6f42011-05-10 02:28:49 -07001440 default:
1441 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001442 * All other completion status cause the IO to be complete.
1443 * If a NAK was received, then it is up to the user to retry
1444 * the request.
1445 */
Dan Williams5dec6f42011-05-10 02:28:49 -07001446 scic_sds_request_set_status(
1447 sci_req,
1448 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
Edmund Nadolskie3013702011-06-02 00:10:43 +00001449 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williams5dec6f42011-05-10 02:28:49 -07001450
Edmund Nadolskie3013702011-06-02 00:10:43 +00001451 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001452 break;
1453 }
1454
1455 return status;
1456}
1457
Dan Williams5dec6f42011-05-10 02:28:49 -07001458static void scic_sds_stp_request_udma_complete_request(
1459 struct scic_sds_request *request,
1460 u32 scu_status,
1461 enum sci_status sci_status)
1462{
1463 scic_sds_request_set_status(request, scu_status, sci_status);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001464 sci_change_state(&request->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001465}
1466
1467static enum sci_status scic_sds_stp_request_udma_general_frame_handler(struct scic_sds_request *sci_req,
1468 u32 frame_index)
1469{
1470 struct scic_sds_controller *scic = sci_req->owning_controller;
1471 struct dev_to_host_fis *frame_header;
1472 enum sci_status status;
1473 u32 *frame_buffer;
1474
1475 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1476 frame_index,
1477 (void **)&frame_header);
1478
1479 if ((status == SCI_SUCCESS) &&
1480 (frame_header->fis_type == FIS_REGD2H)) {
1481 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1482 frame_index,
1483 (void **)&frame_buffer);
1484
1485 scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
1486 frame_header,
1487 frame_buffer);
1488 }
1489
1490 scic_sds_controller_release_frame(scic, frame_index);
1491
1492 return status;
1493}
1494
Edmund Nadolskie3013702011-06-02 00:10:43 +00001495enum sci_status
1496scic_sds_io_request_frame_handler(struct scic_sds_request *sci_req,
1497 u32 frame_index)
Dan Williamsd1c637c32011-05-11 08:27:47 -07001498{
1499 struct scic_sds_controller *scic = sci_req->owning_controller;
Dan Williamsba7cb222011-06-27 11:56:41 -07001500 struct isci_stp_request *stp_req = &sci_req->stp.req;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001501 enum sci_base_request_states state;
1502 enum sci_status status;
1503 ssize_t word_cnt;
1504
Edmund Nadolskie3013702011-06-02 00:10:43 +00001505 state = sci_req->sm.current_state_id;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001506 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001507 case SCI_REQ_STARTED: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001508 struct ssp_frame_hdr ssp_hdr;
1509 void *frame_header;
1510
1511 scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1512 frame_index,
1513 &frame_header);
1514
1515 word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
1516 sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);
1517
1518 if (ssp_hdr.frame_type == SSP_RESPONSE) {
1519 struct ssp_response_iu *resp_iu;
1520 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1521
1522 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1523 frame_index,
1524 (void **)&resp_iu);
1525
1526 sci_swab32_cpy(&sci_req->ssp.rsp, resp_iu, word_cnt);
1527
1528 resp_iu = &sci_req->ssp.rsp;
1529
1530 if (resp_iu->datapres == 0x01 ||
1531 resp_iu->datapres == 0x02) {
1532 scic_sds_request_set_status(sci_req,
1533 SCU_TASK_DONE_CHECK_RESPONSE,
1534 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1535 } else
1536 scic_sds_request_set_status(sci_req,
1537 SCU_TASK_DONE_GOOD,
1538 SCI_SUCCESS);
1539 } else {
1540 /* not a response frame, why did it get forwarded? */
1541 dev_err(scic_to_dev(scic),
1542 "%s: SCIC IO Request 0x%p received unexpected "
1543 "frame %d type 0x%02x\n", __func__, sci_req,
1544 frame_index, ssp_hdr.frame_type);
1545 }
1546
1547 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001548 * In any case we are done with this frame buffer return it to
1549 * the controller
Dan Williamsd1c637c32011-05-11 08:27:47 -07001550 */
1551 scic_sds_controller_release_frame(scic, frame_index);
1552
1553 return SCI_SUCCESS;
1554 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001555
1556 case SCI_REQ_TASK_WAIT_TC_RESP:
Dan Williamsd1c637c32011-05-11 08:27:47 -07001557 scic_sds_io_request_copy_response(sci_req);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001558 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001559 scic_sds_controller_release_frame(scic,frame_index);
1560 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001561
1562 case SCI_REQ_SMP_WAIT_RESP: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001563 struct smp_resp *rsp_hdr = &sci_req->smp.rsp;
1564 void *frame_header;
1565
1566 scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1567 frame_index,
1568 &frame_header);
1569
1570 /* byte swap the header. */
1571 word_cnt = SMP_RESP_HDR_SZ / sizeof(u32);
1572 sci_swab32_cpy(rsp_hdr, frame_header, word_cnt);
1573
1574 if (rsp_hdr->frame_type == SMP_RESPONSE) {
1575 void *smp_resp;
1576
1577 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1578 frame_index,
1579 &smp_resp);
1580
Dan Williams5edc3342011-06-16 17:20:35 -07001581 word_cnt = (sizeof(struct smp_resp) - SMP_RESP_HDR_SZ) /
Dan Williamsd1c637c32011-05-11 08:27:47 -07001582 sizeof(u32);
1583
1584 sci_swab32_cpy(((u8 *) rsp_hdr) + SMP_RESP_HDR_SZ,
1585 smp_resp, word_cnt);
1586
1587 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
1588 SCI_SUCCESS);
1589
Edmund Nadolskie3013702011-06-02 00:10:43 +00001590 sci_change_state(&sci_req->sm, SCI_REQ_SMP_WAIT_TC_COMP);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001591 } else {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001592 /*
1593 * This was not a response frame why did it get
1594 * forwarded?
1595 */
Dan Williamsd1c637c32011-05-11 08:27:47 -07001596 dev_err(scic_to_dev(scic),
Edmund Nadolskie3013702011-06-02 00:10:43 +00001597 "%s: SCIC SMP Request 0x%p received unexpected "
1598 "frame %d type 0x%02x\n",
1599 __func__,
1600 sci_req,
1601 frame_index,
1602 rsp_hdr->frame_type);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001603
1604 scic_sds_request_set_status(sci_req,
1605 SCU_TASK_DONE_SMP_FRM_TYPE_ERR,
1606 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1607
Edmund Nadolskie3013702011-06-02 00:10:43 +00001608 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001609 }
1610
1611 scic_sds_controller_release_frame(scic, frame_index);
1612
1613 return SCI_SUCCESS;
1614 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001615
1616 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
1617 return scic_sds_stp_request_udma_general_frame_handler(sci_req,
1618 frame_index);
1619
1620 case SCI_REQ_STP_UDMA_WAIT_D2H:
Dan Williamsd1c637c32011-05-11 08:27:47 -07001621 /* Use the general frame handler to copy the resposne data */
Edmund Nadolskie3013702011-06-02 00:10:43 +00001622 status = scic_sds_stp_request_udma_general_frame_handler(sci_req,
1623 frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001624
1625 if (status != SCI_SUCCESS)
1626 return status;
1627
1628 scic_sds_stp_request_udma_complete_request(sci_req,
1629 SCU_TASK_DONE_CHECK_RESPONSE,
1630 SCI_FAILURE_IO_RESPONSE_VALID);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001631
Dan Williamsd1c637c32011-05-11 08:27:47 -07001632 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001633
1634 case SCI_REQ_STP_NON_DATA_WAIT_D2H: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001635 struct dev_to_host_fis *frame_header;
1636 u32 *frame_buffer;
1637
1638 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1639 frame_index,
1640 (void **)&frame_header);
1641
1642 if (status != SCI_SUCCESS) {
1643 dev_err(scic_to_dev(scic),
Edmund Nadolskie3013702011-06-02 00:10:43 +00001644 "%s: SCIC IO Request 0x%p could not get frame "
1645 "header for frame index %d, status %x\n",
1646 __func__,
1647 stp_req,
1648 frame_index,
1649 status);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001650
1651 return status;
1652 }
1653
1654 switch (frame_header->fis_type) {
1655 case FIS_REGD2H:
1656 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1657 frame_index,
1658 (void **)&frame_buffer);
1659
1660 scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
1661 frame_header,
1662 frame_buffer);
1663
1664 /* The command has completed with error */
1665 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_CHECK_RESPONSE,
1666 SCI_FAILURE_IO_RESPONSE_VALID);
1667 break;
1668
1669 default:
1670 dev_warn(scic_to_dev(scic),
1671 "%s: IO Request:0x%p Frame Id:%d protocol "
1672 "violation occurred\n", __func__, stp_req,
1673 frame_index);
1674
1675 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_UNEXP_FIS,
1676 SCI_FAILURE_PROTOCOL_VIOLATION);
1677 break;
1678 }
1679
Edmund Nadolskie3013702011-06-02 00:10:43 +00001680 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001681
1682 /* Frame has been decoded return it to the controller */
1683 scic_sds_controller_release_frame(scic, frame_index);
1684
1685 return status;
1686 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001687
1688 case SCI_REQ_STP_PIO_WAIT_FRAME: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001689 struct isci_request *ireq = sci_req_to_ireq(sci_req);
1690 struct sas_task *task = isci_request_access_task(ireq);
1691 struct dev_to_host_fis *frame_header;
1692 u32 *frame_buffer;
1693
1694 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1695 frame_index,
1696 (void **)&frame_header);
1697
1698 if (status != SCI_SUCCESS) {
1699 dev_err(scic_to_dev(scic),
Edmund Nadolskie3013702011-06-02 00:10:43 +00001700 "%s: SCIC IO Request 0x%p could not get frame "
1701 "header for frame index %d, status %x\n",
Dan Williamsd1c637c32011-05-11 08:27:47 -07001702 __func__, stp_req, frame_index, status);
1703 return status;
1704 }
1705
1706 switch (frame_header->fis_type) {
1707 case FIS_PIO_SETUP:
1708 /* Get from the frame buffer the PIO Setup Data */
1709 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1710 frame_index,
1711 (void **)&frame_buffer);
1712
Edmund Nadolskie3013702011-06-02 00:10:43 +00001713 /* Get the data from the PIO Setup The SCU Hardware
1714 * returns first word in the frame_header and the rest
1715 * of the data is in the frame buffer so we need to
1716 * back up one dword
Dan Williamsd1c637c32011-05-11 08:27:47 -07001717 */
1718
1719 /* transfer_count: first 16bits in the 4th dword */
Dan Williamsba7cb222011-06-27 11:56:41 -07001720 stp_req->pio_len = frame_buffer[3] & 0xffff;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001721
Dan Williamsba7cb222011-06-27 11:56:41 -07001722 /* status: 4th byte in the 3rd dword */
1723 stp_req->status = (frame_buffer[2] >> 24) & 0xff;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001724
1725 scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
1726 frame_header,
1727 frame_buffer);
1728
Dan Williamsba7cb222011-06-27 11:56:41 -07001729 sci_req->stp.rsp.status = stp_req->status;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001730
1731 /* The next state is dependent on whether the
1732 * request was PIO Data-in or Data out
1733 */
1734 if (task->data_dir == DMA_FROM_DEVICE) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001735 sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_DATA_IN);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001736 } else if (task->data_dir == DMA_TO_DEVICE) {
1737 /* Transmit data */
1738 status = scic_sds_stp_request_pio_data_out_transmit_data(sci_req);
1739 if (status != SCI_SUCCESS)
1740 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001741 sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_DATA_OUT);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001742 }
1743 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001744
Dan Williamsd1c637c32011-05-11 08:27:47 -07001745 case FIS_SETDEVBITS:
Edmund Nadolskie3013702011-06-02 00:10:43 +00001746 sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001747 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001748
Dan Williamsd1c637c32011-05-11 08:27:47 -07001749 case FIS_REGD2H:
1750 if (frame_header->status & ATA_BUSY) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001751 /*
1752 * Now why is the drive sending a D2H Register
1753 * FIS when it is still busy? Do nothing since
1754 * we are still in the right state.
Dan Williamsd1c637c32011-05-11 08:27:47 -07001755 */
1756 dev_dbg(scic_to_dev(scic),
1757 "%s: SCIC PIO Request 0x%p received "
1758 "D2H Register FIS with BSY status "
Edmund Nadolskie3013702011-06-02 00:10:43 +00001759 "0x%x\n",
1760 __func__,
1761 stp_req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001762 frame_header->status);
1763 break;
1764 }
1765
1766 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1767 frame_index,
1768 (void **)&frame_buffer);
1769
1770 scic_sds_controller_copy_sata_response(&sci_req->stp.req,
1771 frame_header,
1772 frame_buffer);
1773
1774 scic_sds_request_set_status(sci_req,
1775 SCU_TASK_DONE_CHECK_RESPONSE,
1776 SCI_FAILURE_IO_RESPONSE_VALID);
1777
Edmund Nadolskie3013702011-06-02 00:10:43 +00001778 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001779 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001780
Dan Williamsd1c637c32011-05-11 08:27:47 -07001781 default:
1782 /* FIXME: what do we do here? */
1783 break;
1784 }
1785
1786 /* Frame is decoded return it to the controller */
1787 scic_sds_controller_release_frame(scic, frame_index);
1788
1789 return status;
1790 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001791
1792 case SCI_REQ_STP_PIO_DATA_IN: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001793 struct dev_to_host_fis *frame_header;
1794 struct sata_fis_data *frame_buffer;
1795
1796 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1797 frame_index,
1798 (void **)&frame_header);
1799
1800 if (status != SCI_SUCCESS) {
1801 dev_err(scic_to_dev(scic),
Edmund Nadolskie3013702011-06-02 00:10:43 +00001802 "%s: SCIC IO Request 0x%p could not get frame "
1803 "header for frame index %d, status %x\n",
1804 __func__,
1805 stp_req,
1806 frame_index,
1807 status);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001808 return status;
1809 }
1810
1811 if (frame_header->fis_type != FIS_DATA) {
1812 dev_err(scic_to_dev(scic),
1813 "%s: SCIC PIO Request 0x%p received frame %d "
1814 "with fis type 0x%02x when expecting a data "
Edmund Nadolskie3013702011-06-02 00:10:43 +00001815 "fis.\n",
1816 __func__,
1817 stp_req,
1818 frame_index,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001819 frame_header->fis_type);
1820
1821 scic_sds_request_set_status(sci_req,
1822 SCU_TASK_DONE_GOOD,
1823 SCI_FAILURE_IO_REQUIRES_SCSI_ABORT);
1824
Edmund Nadolskie3013702011-06-02 00:10:43 +00001825 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001826
1827 /* Frame is decoded return it to the controller */
1828 scic_sds_controller_release_frame(scic, frame_index);
1829 return status;
1830 }
1831
Dan Williamsba7cb222011-06-27 11:56:41 -07001832 if (stp_req->sgl.index < 0) {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001833 sci_req->saved_rx_frame_index = frame_index;
Dan Williamsba7cb222011-06-27 11:56:41 -07001834 stp_req->pio_len = 0;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001835 } else {
1836 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1837 frame_index,
1838 (void **)&frame_buffer);
1839
1840 status = scic_sds_stp_request_pio_data_in_copy_data(stp_req,
1841 (u8 *)frame_buffer);
1842
1843 /* Frame is decoded return it to the controller */
1844 scic_sds_controller_release_frame(scic, frame_index);
1845 }
1846
1847 /* Check for the end of the transfer, are there more
1848 * bytes remaining for this data transfer
1849 */
Dan Williamsba7cb222011-06-27 11:56:41 -07001850 if (status != SCI_SUCCESS || stp_req->pio_len != 0)
Dan Williamsd1c637c32011-05-11 08:27:47 -07001851 return status;
1852
Dan Williamsba7cb222011-06-27 11:56:41 -07001853 if ((stp_req->status & ATA_BUSY) == 0) {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001854 scic_sds_request_set_status(sci_req,
1855 SCU_TASK_DONE_CHECK_RESPONSE,
1856 SCI_FAILURE_IO_RESPONSE_VALID);
1857
Edmund Nadolskie3013702011-06-02 00:10:43 +00001858 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001859 } else {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001860 sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001861 }
1862 return status;
1863 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001864
1865 case SCI_REQ_STP_SOFT_RESET_WAIT_D2H: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001866 struct dev_to_host_fis *frame_header;
1867 u32 *frame_buffer;
1868
1869 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1870 frame_index,
1871 (void **)&frame_header);
1872 if (status != SCI_SUCCESS) {
1873 dev_err(scic_to_dev(scic),
Edmund Nadolskie3013702011-06-02 00:10:43 +00001874 "%s: SCIC IO Request 0x%p could not get frame "
1875 "header for frame index %d, status %x\n",
1876 __func__,
1877 stp_req,
1878 frame_index,
1879 status);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001880 return status;
1881 }
1882
1883 switch (frame_header->fis_type) {
1884 case FIS_REGD2H:
1885 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1886 frame_index,
1887 (void **)&frame_buffer);
1888
1889 scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
1890 frame_header,
1891 frame_buffer);
1892
1893 /* The command has completed with error */
1894 scic_sds_request_set_status(sci_req,
1895 SCU_TASK_DONE_CHECK_RESPONSE,
1896 SCI_FAILURE_IO_RESPONSE_VALID);
1897 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001898
Dan Williamsd1c637c32011-05-11 08:27:47 -07001899 default:
1900 dev_warn(scic_to_dev(scic),
1901 "%s: IO Request:0x%p Frame Id:%d protocol "
Edmund Nadolskie3013702011-06-02 00:10:43 +00001902 "violation occurred\n",
1903 __func__,
1904 stp_req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001905 frame_index);
1906
Edmund Nadolskie3013702011-06-02 00:10:43 +00001907 scic_sds_request_set_status(sci_req,
1908 SCU_TASK_DONE_UNEXP_FIS,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001909 SCI_FAILURE_PROTOCOL_VIOLATION);
1910 break;
1911 }
1912
Edmund Nadolskie3013702011-06-02 00:10:43 +00001913 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001914
1915 /* Frame has been decoded return it to the controller */
1916 scic_sds_controller_release_frame(scic, frame_index);
1917
1918 return status;
1919 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001920 case SCI_REQ_ABORTING:
1921 /*
1922 * TODO: Is it even possible to get an unsolicited frame in the
Dan Williamsd1c637c32011-05-11 08:27:47 -07001923 * aborting state?
1924 */
1925 scic_sds_controller_release_frame(scic, frame_index);
1926 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001927
Dan Williamsd1c637c32011-05-11 08:27:47 -07001928 default:
1929 dev_warn(scic_to_dev(scic),
Edmund Nadolskie3013702011-06-02 00:10:43 +00001930 "%s: SCIC IO Request given unexpected frame %x while "
1931 "in state %d\n",
1932 __func__,
1933 frame_index,
1934 state);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001935
1936 scic_sds_controller_release_frame(scic, frame_index);
1937 return SCI_FAILURE_INVALID_STATE;
1938 }
1939}
1940
Dan Williamsa7e255a2011-05-11 08:27:47 -07001941static enum sci_status stp_request_udma_await_tc_event(struct scic_sds_request *sci_req,
1942 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001943{
1944 enum sci_status status = SCI_SUCCESS;
1945
1946 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1947 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1948 scic_sds_stp_request_udma_complete_request(sci_req,
1949 SCU_TASK_DONE_GOOD,
1950 SCI_SUCCESS);
1951 break;
1952 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
1953 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001954 /* We must check ther response buffer to see if the D2H
1955 * Register FIS was received before we got the TC
1956 * completion.
1957 */
Dan Williams5dec6f42011-05-10 02:28:49 -07001958 if (sci_req->stp.rsp.fis_type == FIS_REGD2H) {
1959 scic_sds_remote_device_suspend(sci_req->target_device,
1960 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
1961
1962 scic_sds_stp_request_udma_complete_request(sci_req,
1963 SCU_TASK_DONE_CHECK_RESPONSE,
1964 SCI_FAILURE_IO_RESPONSE_VALID);
1965 } else {
Dan Williamsa7e255a2011-05-11 08:27:47 -07001966 /* If we have an error completion status for the
1967 * TC then we can expect a D2H register FIS from
1968 * the device so we must change state to wait
1969 * for it
1970 */
Edmund Nadolskie3013702011-06-02 00:10:43 +00001971 sci_change_state(&sci_req->sm, SCI_REQ_STP_UDMA_WAIT_D2H);
Dan Williams5dec6f42011-05-10 02:28:49 -07001972 }
1973 break;
1974
Dan Williamsa7e255a2011-05-11 08:27:47 -07001975 /* TODO Check to see if any of these completion status need to
1976 * wait for the device to host register fis.
1977 */
1978 /* TODO We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR
1979 * - this comes only for B0
1980 */
Dan Williams5dec6f42011-05-10 02:28:49 -07001981 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_INV_FIS_LEN):
1982 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
1983 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_R_ERR):
1984 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CMD_LL_R_ERR):
1985 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CRC_ERR):
1986 scic_sds_remote_device_suspend(sci_req->target_device,
1987 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
1988 /* Fall through to the default case */
1989 default:
1990 /* All other completion status cause the IO to be complete. */
1991 scic_sds_stp_request_udma_complete_request(sci_req,
1992 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1993 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1994 break;
1995 }
1996
1997 return status;
1998}
1999
Edmund Nadolskie3013702011-06-02 00:10:43 +00002000static enum sci_status
2001stp_request_soft_reset_await_h2d_asserted_tc_event(struct scic_sds_request *sci_req,
2002 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07002003{
2004 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2005 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williamsa7e255a2011-05-11 08:27:47 -07002006 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
2007 SCI_SUCCESS);
Dan Williams5dec6f42011-05-10 02:28:49 -07002008
Edmund Nadolskie3013702011-06-02 00:10:43 +00002009 sci_change_state(&sci_req->sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG);
Dan Williams5dec6f42011-05-10 02:28:49 -07002010 break;
2011
2012 default:
2013 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00002014 * All other completion status cause the IO to be complete.
2015 * If a NAK was received, then it is up to the user to retry
2016 * the request.
2017 */
Dan Williamsa7e255a2011-05-11 08:27:47 -07002018 scic_sds_request_set_status(sci_req,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002019 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
2020 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williams5dec6f42011-05-10 02:28:49 -07002021
Edmund Nadolskie3013702011-06-02 00:10:43 +00002022 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07002023 break;
2024 }
2025
2026 return SCI_SUCCESS;
2027}
2028
Edmund Nadolskie3013702011-06-02 00:10:43 +00002029static enum sci_status
2030stp_request_soft_reset_await_h2d_diagnostic_tc_event(struct scic_sds_request *sci_req,
2031 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07002032{
2033 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2034 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
2035 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
2036 SCI_SUCCESS);
2037
Edmund Nadolskie3013702011-06-02 00:10:43 +00002038 sci_change_state(&sci_req->sm, SCI_REQ_STP_SOFT_RESET_WAIT_D2H);
Dan Williams5dec6f42011-05-10 02:28:49 -07002039 break;
2040
2041 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07002042 /* All other completion status cause the IO to be complete. If
2043 * a NAK was received, then it is up to the user to retry the
2044 * request.
2045 */
2046 scic_sds_request_set_status(sci_req,
Dan Williams5dec6f42011-05-10 02:28:49 -07002047 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
Dan Williamsa7e255a2011-05-11 08:27:47 -07002048 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
Dan Williams5dec6f42011-05-10 02:28:49 -07002049
Edmund Nadolskie3013702011-06-02 00:10:43 +00002050 sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07002051 break;
2052 }
2053
2054 return SCI_SUCCESS;
2055}
2056
Dan Williamsa7e255a2011-05-11 08:27:47 -07002057enum sci_status
Edmund Nadolskie3013702011-06-02 00:10:43 +00002058scic_sds_io_request_tc_completion(struct scic_sds_request *sci_req,
2059 u32 completion_code)
Dan Williamsa7e255a2011-05-11 08:27:47 -07002060{
2061 enum sci_base_request_states state;
2062 struct scic_sds_controller *scic = sci_req->owning_controller;
2063
Edmund Nadolskie3013702011-06-02 00:10:43 +00002064 state = sci_req->sm.current_state_id;
Dan Williamsa7e255a2011-05-11 08:27:47 -07002065
2066 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002067 case SCI_REQ_STARTED:
2068 return request_started_state_tc_event(sci_req, completion_code);
2069
2070 case SCI_REQ_TASK_WAIT_TC_COMP:
2071 return ssp_task_request_await_tc_event(sci_req,
2072 completion_code);
2073
2074 case SCI_REQ_SMP_WAIT_RESP:
2075 return smp_request_await_response_tc_event(sci_req,
2076 completion_code);
2077
2078 case SCI_REQ_SMP_WAIT_TC_COMP:
2079 return smp_request_await_tc_event(sci_req, completion_code);
2080
2081 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
2082 return stp_request_udma_await_tc_event(sci_req,
2083 completion_code);
2084
2085 case SCI_REQ_STP_NON_DATA_WAIT_H2D:
2086 return stp_request_non_data_await_h2d_tc_event(sci_req,
2087 completion_code);
2088
2089 case SCI_REQ_STP_PIO_WAIT_H2D:
2090 return stp_request_pio_await_h2d_completion_tc_event(sci_req,
2091 completion_code);
2092
2093 case SCI_REQ_STP_PIO_DATA_OUT:
2094 return pio_data_out_tx_done_tc_event(sci_req, completion_code);
2095
2096 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
2097 return stp_request_soft_reset_await_h2d_asserted_tc_event(sci_req,
2098 completion_code);
2099
2100 case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
2101 return stp_request_soft_reset_await_h2d_diagnostic_tc_event(sci_req,
2102 completion_code);
2103
2104 case SCI_REQ_ABORTING:
2105 return request_aborting_state_tc_event(sci_req,
2106 completion_code);
2107
2108 default:
2109 dev_warn(scic_to_dev(scic),
2110 "%s: SCIC IO Request given task completion "
2111 "notification %x while in wrong state %d\n",
2112 __func__,
2113 completion_code,
2114 state);
2115 return SCI_FAILURE_INVALID_STATE;
Dan Williamsa7e255a2011-05-11 08:27:47 -07002116 }
2117}
2118
Dan Williams6f231dd2011-07-02 22:56:22 -07002119/**
2120 * isci_request_process_response_iu() - This function sets the status and
2121 * response iu, in the task struct, from the request object for the upper
2122 * layer driver.
2123 * @sas_task: This parameter is the task struct from the upper layer driver.
2124 * @resp_iu: This parameter points to the response iu of the completed request.
2125 * @dev: This parameter specifies the linux device struct.
2126 *
2127 * none.
2128 */
2129static void isci_request_process_response_iu(
2130 struct sas_task *task,
2131 struct ssp_response_iu *resp_iu,
2132 struct device *dev)
2133{
2134 dev_dbg(dev,
2135 "%s: resp_iu = %p "
2136 "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
2137 "resp_iu->response_data_len = %x, "
2138 "resp_iu->sense_data_len = %x\nrepsonse data: ",
2139 __func__,
2140 resp_iu,
2141 resp_iu->status,
2142 resp_iu->datapres,
2143 resp_iu->response_data_len,
2144 resp_iu->sense_data_len);
2145
2146 task->task_status.stat = resp_iu->status;
2147
2148 /* libsas updates the task status fields based on the response iu. */
2149 sas_ssp_task_response(dev, task, resp_iu);
2150}
2151
2152/**
2153 * isci_request_set_open_reject_status() - This function prepares the I/O
2154 * completion for OPEN_REJECT conditions.
2155 * @request: This parameter is the completed isci_request object.
2156 * @response_ptr: This parameter specifies the service response for the I/O.
2157 * @status_ptr: This parameter specifies the exec status for the I/O.
2158 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2159 * the LLDD with respect to completing this request or forcing an abort
2160 * condition on the I/O.
2161 * @open_rej_reason: This parameter specifies the encoded reason for the
2162 * abandon-class reject.
2163 *
2164 * none.
2165 */
2166static void isci_request_set_open_reject_status(
2167 struct isci_request *request,
2168 struct sas_task *task,
2169 enum service_response *response_ptr,
2170 enum exec_status *status_ptr,
2171 enum isci_completion_selection *complete_to_host_ptr,
2172 enum sas_open_rej_reason open_rej_reason)
2173{
2174 /* Task in the target is done. */
Dan Williams38d88792011-06-23 14:33:48 -07002175 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002176 *response_ptr = SAS_TASK_UNDELIVERED;
2177 *status_ptr = SAS_OPEN_REJECT;
2178 *complete_to_host_ptr = isci_perform_normal_io_completion;
2179 task->task_status.open_rej_reason = open_rej_reason;
2180}
2181
2182/**
2183 * isci_request_handle_controller_specific_errors() - This function decodes
2184 * controller-specific I/O completion error conditions.
2185 * @request: This parameter is the completed isci_request object.
2186 * @response_ptr: This parameter specifies the service response for the I/O.
2187 * @status_ptr: This parameter specifies the exec status for the I/O.
2188 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2189 * the LLDD with respect to completing this request or forcing an abort
2190 * condition on the I/O.
2191 *
2192 * none.
2193 */
2194static void isci_request_handle_controller_specific_errors(
Dan Williams209fae12011-06-13 17:39:44 -07002195 struct isci_remote_device *idev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002196 struct isci_request *request,
2197 struct sas_task *task,
2198 enum service_response *response_ptr,
2199 enum exec_status *status_ptr,
2200 enum isci_completion_selection *complete_to_host_ptr)
2201{
2202 unsigned int cstatus;
2203
Dan Williamsf1f52e72011-05-10 02:28:45 -07002204 cstatus = request->sci.scu_status;
Dan Williams6f231dd2011-07-02 22:56:22 -07002205
2206 dev_dbg(&request->isci_host->pdev->dev,
2207 "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
2208 "- controller status = 0x%x\n",
2209 __func__, request, cstatus);
2210
2211 /* Decode the controller-specific errors; most
2212 * important is to recognize those conditions in which
2213 * the target may still have a task outstanding that
2214 * must be aborted.
2215 *
2216 * Note that there are SCU completion codes being
2217 * named in the decode below for which SCIC has already
2218 * done work to handle them in a way other than as
2219 * a controller-specific completion code; these are left
2220 * in the decode below for completeness sake.
2221 */
2222 switch (cstatus) {
2223 case SCU_TASK_DONE_DMASETUP_DIRERR:
2224 /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
2225 case SCU_TASK_DONE_XFERCNT_ERR:
2226 /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
2227 if (task->task_proto == SAS_PROTOCOL_SMP) {
2228 /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
2229 *response_ptr = SAS_TASK_COMPLETE;
2230
2231 /* See if the device has been/is being stopped. Note
2232 * that we ignore the quiesce state, since we are
2233 * concerned about the actual device state.
2234 */
Dan Williams209fae12011-06-13 17:39:44 -07002235 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002236 *status_ptr = SAS_DEVICE_UNKNOWN;
2237 else
2238 *status_ptr = SAS_ABORTED_TASK;
2239
Dan Williams38d88792011-06-23 14:33:48 -07002240 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002241
2242 *complete_to_host_ptr =
2243 isci_perform_normal_io_completion;
2244 } else {
2245 /* Task in the target is not done. */
2246 *response_ptr = SAS_TASK_UNDELIVERED;
2247
Dan Williams209fae12011-06-13 17:39:44 -07002248 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002249 *status_ptr = SAS_DEVICE_UNKNOWN;
2250 else
2251 *status_ptr = SAM_STAT_TASK_ABORTED;
2252
Dan Williams38d88792011-06-23 14:33:48 -07002253 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002254
2255 *complete_to_host_ptr =
2256 isci_perform_error_io_completion;
2257 }
2258
2259 break;
2260
2261 case SCU_TASK_DONE_CRC_ERR:
2262 case SCU_TASK_DONE_NAK_CMD_ERR:
2263 case SCU_TASK_DONE_EXCESS_DATA:
2264 case SCU_TASK_DONE_UNEXP_FIS:
2265 /* Also SCU_TASK_DONE_UNEXP_RESP: */
2266 case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
2267 case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
2268 case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
2269 /* These are conditions in which the target
2270 * has completed the task, so that no cleanup
2271 * is necessary.
2272 */
2273 *response_ptr = SAS_TASK_COMPLETE;
2274
2275 /* See if the device has been/is being stopped. Note
2276 * that we ignore the quiesce state, since we are
2277 * concerned about the actual device state.
2278 */
Dan Williams209fae12011-06-13 17:39:44 -07002279 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002280 *status_ptr = SAS_DEVICE_UNKNOWN;
2281 else
2282 *status_ptr = SAS_ABORTED_TASK;
2283
Dan Williams38d88792011-06-23 14:33:48 -07002284 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002285
2286 *complete_to_host_ptr = isci_perform_normal_io_completion;
2287 break;
2288
2289
2290 /* Note that the only open reject completion codes seen here will be
2291 * abandon-class codes; all others are automatically retried in the SCU.
2292 */
2293 case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2294
2295 isci_request_set_open_reject_status(
2296 request, task, response_ptr, status_ptr,
2297 complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
2298 break;
2299
2300 case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2301
2302 /* Note - the return of AB0 will change when
2303 * libsas implements detection of zone violations.
2304 */
2305 isci_request_set_open_reject_status(
2306 request, task, response_ptr, status_ptr,
2307 complete_to_host_ptr, SAS_OREJ_RESV_AB0);
2308 break;
2309
2310 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2311
2312 isci_request_set_open_reject_status(
2313 request, task, response_ptr, status_ptr,
2314 complete_to_host_ptr, SAS_OREJ_RESV_AB1);
2315 break;
2316
2317 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2318
2319 isci_request_set_open_reject_status(
2320 request, task, response_ptr, status_ptr,
2321 complete_to_host_ptr, SAS_OREJ_RESV_AB2);
2322 break;
2323
2324 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2325
2326 isci_request_set_open_reject_status(
2327 request, task, response_ptr, status_ptr,
2328 complete_to_host_ptr, SAS_OREJ_RESV_AB3);
2329 break;
2330
2331 case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2332
2333 isci_request_set_open_reject_status(
2334 request, task, response_ptr, status_ptr,
2335 complete_to_host_ptr, SAS_OREJ_BAD_DEST);
2336 break;
2337
2338 case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
2339
2340 isci_request_set_open_reject_status(
2341 request, task, response_ptr, status_ptr,
2342 complete_to_host_ptr, SAS_OREJ_STP_NORES);
2343 break;
2344
2345 case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
2346
2347 isci_request_set_open_reject_status(
2348 request, task, response_ptr, status_ptr,
2349 complete_to_host_ptr, SAS_OREJ_EPROTO);
2350 break;
2351
2352 case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
2353
2354 isci_request_set_open_reject_status(
2355 request, task, response_ptr, status_ptr,
2356 complete_to_host_ptr, SAS_OREJ_CONN_RATE);
2357 break;
2358
2359 case SCU_TASK_DONE_LL_R_ERR:
2360 /* Also SCU_TASK_DONE_ACK_NAK_TO: */
2361 case SCU_TASK_DONE_LL_PERR:
2362 case SCU_TASK_DONE_LL_SY_TERM:
2363 /* Also SCU_TASK_DONE_NAK_ERR:*/
2364 case SCU_TASK_DONE_LL_LF_TERM:
2365 /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
2366 case SCU_TASK_DONE_LL_ABORT_ERR:
2367 case SCU_TASK_DONE_SEQ_INV_TYPE:
2368 /* Also SCU_TASK_DONE_UNEXP_XR: */
2369 case SCU_TASK_DONE_XR_IU_LEN_ERR:
2370 case SCU_TASK_DONE_INV_FIS_LEN:
2371 /* Also SCU_TASK_DONE_XR_WD_LEN: */
2372 case SCU_TASK_DONE_SDMA_ERR:
2373 case SCU_TASK_DONE_OFFSET_ERR:
2374 case SCU_TASK_DONE_MAX_PLD_ERR:
2375 case SCU_TASK_DONE_LF_ERR:
2376 case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
2377 case SCU_TASK_DONE_SMP_LL_RX_ERR:
2378 case SCU_TASK_DONE_UNEXP_DATA:
2379 case SCU_TASK_DONE_UNEXP_SDBFIS:
2380 case SCU_TASK_DONE_REG_ERR:
2381 case SCU_TASK_DONE_SDB_ERR:
2382 case SCU_TASK_DONE_TASK_ABORT:
2383 default:
2384 /* Task in the target is not done. */
2385 *response_ptr = SAS_TASK_UNDELIVERED;
2386 *status_ptr = SAM_STAT_TASK_ABORTED;
Dan Williams6f231dd2011-07-02 22:56:22 -07002387
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002388 if (task->task_proto == SAS_PROTOCOL_SMP) {
Dan Williams38d88792011-06-23 14:33:48 -07002389 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002390
2391 *complete_to_host_ptr = isci_perform_normal_io_completion;
2392 } else {
Dan Williams38d88792011-06-23 14:33:48 -07002393 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002394
2395 *complete_to_host_ptr = isci_perform_error_io_completion;
2396 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002397 break;
2398 }
2399}
2400
2401/**
2402 * isci_task_save_for_upper_layer_completion() - This function saves the
2403 * request for later completion to the upper layer driver.
2404 * @host: This parameter is a pointer to the host on which the the request
2405 * should be queued (either as an error or success).
2406 * @request: This parameter is the completed request.
2407 * @response: This parameter is the response code for the completed task.
2408 * @status: This parameter is the status code for the completed task.
2409 *
2410 * none.
2411 */
2412static void isci_task_save_for_upper_layer_completion(
2413 struct isci_host *host,
2414 struct isci_request *request,
2415 enum service_response response,
2416 enum exec_status status,
2417 enum isci_completion_selection task_notification_selection)
2418{
2419 struct sas_task *task = isci_request_access_task(request);
2420
Jeff Skirvinec6c9632011-03-04 14:06:44 -08002421 task_notification_selection
2422 = isci_task_set_completion_status(task, response, status,
2423 task_notification_selection);
Dan Williams6f231dd2011-07-02 22:56:22 -07002424
2425 /* Tasks aborted specifically by a call to the lldd_abort_task
2426 * function should not be completed to the host in the regular path.
2427 */
2428 switch (task_notification_selection) {
2429
2430 case isci_perform_normal_io_completion:
2431
2432 /* Normal notification (task_done) */
2433 dev_dbg(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002434 "%s: Normal - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002435 __func__,
2436 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002437 task->task_status.resp, response,
2438 task->task_status.stat, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07002439 /* Add to the completed list. */
2440 list_add(&request->completed_node,
2441 &host->requests_to_complete);
Jeff Skirvinec6c9632011-03-04 14:06:44 -08002442
2443 /* Take the request off the device's pending request list. */
2444 list_del_init(&request->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -07002445 break;
2446
2447 case isci_perform_aborted_io_completion:
Jeff Skirvina5fde222011-03-04 14:06:42 -08002448 /* No notification to libsas because this request is
2449 * already in the abort path.
Dan Williams6f231dd2011-07-02 22:56:22 -07002450 */
2451 dev_warn(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002452 "%s: Aborted - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002453 __func__,
2454 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002455 task->task_status.resp, response,
2456 task->task_status.stat, status);
Jeff Skirvina5fde222011-03-04 14:06:42 -08002457
2458 /* Wake up whatever process was waiting for this
2459 * request to complete.
2460 */
2461 WARN_ON(request->io_request_completion == NULL);
2462
2463 if (request->io_request_completion != NULL) {
2464
2465 /* Signal whoever is waiting that this
2466 * request is complete.
2467 */
2468 complete(request->io_request_completion);
2469 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002470 break;
2471
2472 case isci_perform_error_io_completion:
2473 /* Use sas_task_abort */
2474 dev_warn(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002475 "%s: Error - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002476 __func__,
2477 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002478 task->task_status.resp, response,
2479 task->task_status.stat, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07002480 /* Add to the aborted list. */
2481 list_add(&request->completed_node,
Jeff Skirvin11b00c12011-03-04 14:06:40 -08002482 &host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002483 break;
2484
2485 default:
2486 dev_warn(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002487 "%s: Unknown - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002488 __func__,
2489 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -07002490 task->task_status.resp, response,
2491 task->task_status.stat, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07002492
Jeff Skirvina5fde222011-03-04 14:06:42 -08002493 /* Add to the error to libsas list. */
Dan Williams6f231dd2011-07-02 22:56:22 -07002494 list_add(&request->completed_node,
Jeff Skirvin11b00c12011-03-04 14:06:40 -08002495 &host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002496 break;
2497 }
2498}
2499
Dan Williamsf1f52e72011-05-10 02:28:45 -07002500static void isci_request_io_request_complete(struct isci_host *isci_host,
2501 struct isci_request *request,
2502 enum sci_io_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07002503{
2504 struct sas_task *task = isci_request_access_task(request);
2505 struct ssp_response_iu *resp_iu;
2506 void *resp_buf;
2507 unsigned long task_flags;
Dan Williams209fae12011-06-13 17:39:44 -07002508 struct isci_remote_device *idev = isci_lookup_device(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002509 enum service_response response = SAS_TASK_UNDELIVERED;
2510 enum exec_status status = SAS_ABORTED_TASK;
2511 enum isci_request_status request_status;
2512 enum isci_completion_selection complete_to_host
2513 = isci_perform_normal_io_completion;
2514
2515 dev_dbg(&isci_host->pdev->dev,
2516 "%s: request = %p, task = %p,\n"
2517 "task->data_dir = %d completion_status = 0x%x\n",
2518 __func__,
2519 request,
2520 task,
2521 task->data_dir,
2522 completion_status);
2523
Jeff Skirvina5fde222011-03-04 14:06:42 -08002524 spin_lock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002525 request_status = isci_request_get_state(request);
Dan Williams6f231dd2011-07-02 22:56:22 -07002526
2527 /* Decode the request status. Note that if the request has been
2528 * aborted by a task management function, we don't care
2529 * what the status is.
2530 */
2531 switch (request_status) {
2532
2533 case aborted:
2534 /* "aborted" indicates that the request was aborted by a task
2535 * management function, since once a task management request is
2536 * perfomed by the device, the request only completes because
2537 * of the subsequent driver terminate.
2538 *
2539 * Aborted also means an external thread is explicitly managing
2540 * this request, so that we do not complete it up the stack.
2541 *
2542 * The target is still there (since the TMF was successful).
2543 */
Dan Williams38d88792011-06-23 14:33:48 -07002544 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002545 response = SAS_TASK_COMPLETE;
2546
2547 /* See if the device has been/is being stopped. Note
2548 * that we ignore the quiesce state, since we are
2549 * concerned about the actual device state.
2550 */
Dan Williams209fae12011-06-13 17:39:44 -07002551 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002552 status = SAS_DEVICE_UNKNOWN;
2553 else
2554 status = SAS_ABORTED_TASK;
2555
2556 complete_to_host = isci_perform_aborted_io_completion;
2557 /* This was an aborted request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002558
2559 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002560 break;
2561
2562 case aborting:
2563 /* aborting means that the task management function tried and
2564 * failed to abort the request. We need to note the request
2565 * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
2566 * target as down.
2567 *
2568 * Aborting also means an external thread is explicitly managing
2569 * this request, so that we do not complete it up the stack.
2570 */
Dan Williams38d88792011-06-23 14:33:48 -07002571 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002572 response = SAS_TASK_UNDELIVERED;
2573
Dan Williams209fae12011-06-13 17:39:44 -07002574 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002575 /* The device has been /is being stopped. Note that
2576 * we ignore the quiesce state, since we are
2577 * concerned about the actual device state.
2578 */
2579 status = SAS_DEVICE_UNKNOWN;
2580 else
2581 status = SAS_PHY_DOWN;
2582
2583 complete_to_host = isci_perform_aborted_io_completion;
2584
2585 /* This was an aborted request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002586
2587 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002588 break;
2589
2590 case terminating:
2591
2592 /* This was an terminated request. This happens when
2593 * the I/O is being terminated because of an action on
2594 * the device (reset, tear down, etc.), and the I/O needs
2595 * to be completed up the stack.
2596 */
Dan Williams38d88792011-06-23 14:33:48 -07002597 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002598 response = SAS_TASK_UNDELIVERED;
2599
2600 /* See if the device has been/is being stopped. Note
2601 * that we ignore the quiesce state, since we are
2602 * concerned about the actual device state.
2603 */
Dan Williams209fae12011-06-13 17:39:44 -07002604 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002605 status = SAS_DEVICE_UNKNOWN;
2606 else
2607 status = SAS_ABORTED_TASK;
2608
Jeff Skirvina5fde222011-03-04 14:06:42 -08002609 complete_to_host = isci_perform_aborted_io_completion;
Dan Williams6f231dd2011-07-02 22:56:22 -07002610
2611 /* This was a terminated request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002612
2613 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002614 break;
2615
Jeff Skirvin77c852f2011-06-20 14:09:16 -07002616 case dead:
2617 /* This was a terminated request that timed-out during the
2618 * termination process. There is no task to complete to
2619 * libsas.
2620 */
2621 complete_to_host = isci_perform_normal_io_completion;
2622 spin_unlock(&request->state_lock);
2623 break;
2624
Dan Williams6f231dd2011-07-02 22:56:22 -07002625 default:
2626
Jeff Skirvina5fde222011-03-04 14:06:42 -08002627 /* The request is done from an SCU HW perspective. */
2628 request->status = completed;
2629
2630 spin_unlock(&request->state_lock);
2631
Dan Williams6f231dd2011-07-02 22:56:22 -07002632 /* This is an active request being completed from the core. */
2633 switch (completion_status) {
2634
2635 case SCI_IO_FAILURE_RESPONSE_VALID:
2636 dev_dbg(&isci_host->pdev->dev,
2637 "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
2638 __func__,
2639 request,
2640 task);
2641
2642 if (sas_protocol_ata(task->task_proto)) {
Dan Williams67ea8382011-05-08 11:47:15 -07002643 resp_buf = &request->sci.stp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -07002644 isci_request_process_stp_response(task,
Dan Williamsb7645812011-05-08 02:35:32 -07002645 resp_buf);
Dan Williams6f231dd2011-07-02 22:56:22 -07002646 } else if (SAS_PROTOCOL_SSP == task->task_proto) {
2647
2648 /* crack the iu response buffer. */
Dan Williams67ea8382011-05-08 11:47:15 -07002649 resp_iu = &request->sci.ssp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -07002650 isci_request_process_response_iu(task, resp_iu,
Dan Williamsb7645812011-05-08 02:35:32 -07002651 &isci_host->pdev->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002652
2653 } else if (SAS_PROTOCOL_SMP == task->task_proto) {
2654
2655 dev_err(&isci_host->pdev->dev,
2656 "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
2657 "SAS_PROTOCOL_SMP protocol\n",
2658 __func__);
2659
2660 } else
2661 dev_err(&isci_host->pdev->dev,
2662 "%s: unknown protocol\n", __func__);
2663
2664 /* use the task status set in the task struct by the
2665 * isci_request_process_response_iu call.
2666 */
Dan Williams38d88792011-06-23 14:33:48 -07002667 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002668 response = task->task_status.resp;
2669 status = task->task_status.stat;
2670 break;
2671
2672 case SCI_IO_SUCCESS:
2673 case SCI_IO_SUCCESS_IO_DONE_EARLY:
2674
2675 response = SAS_TASK_COMPLETE;
2676 status = SAM_STAT_GOOD;
Dan Williams38d88792011-06-23 14:33:48 -07002677 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002678
2679 if (task->task_proto == SAS_PROTOCOL_SMP) {
Dan Williams67ea8382011-05-08 11:47:15 -07002680 void *rsp = &request->sci.smp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -07002681
2682 dev_dbg(&isci_host->pdev->dev,
2683 "%s: SMP protocol completion\n",
2684 __func__);
2685
2686 sg_copy_from_buffer(
2687 &task->smp_task.smp_resp, 1,
Dan Williamsb7645812011-05-08 02:35:32 -07002688 rsp, sizeof(struct smp_resp));
Dan Williams6f231dd2011-07-02 22:56:22 -07002689 } else if (completion_status
2690 == SCI_IO_SUCCESS_IO_DONE_EARLY) {
2691
2692 /* This was an SSP / STP / SATA transfer.
2693 * There is a possibility that less data than
2694 * the maximum was transferred.
2695 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07002696 u32 transferred_length = sci_req_tx_bytes(&request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07002697
2698 task->task_status.residual
2699 = task->total_xfer_len - transferred_length;
2700
2701 /* If there were residual bytes, call this an
2702 * underrun.
2703 */
2704 if (task->task_status.residual != 0)
2705 status = SAS_DATA_UNDERRUN;
2706
2707 dev_dbg(&isci_host->pdev->dev,
2708 "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
2709 __func__,
2710 status);
2711
2712 } else
2713 dev_dbg(&isci_host->pdev->dev,
2714 "%s: SCI_IO_SUCCESS\n",
2715 __func__);
2716
2717 break;
2718
2719 case SCI_IO_FAILURE_TERMINATED:
2720 dev_dbg(&isci_host->pdev->dev,
2721 "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
2722 __func__,
2723 request,
2724 task);
2725
2726 /* The request was terminated explicitly. No handling
2727 * is needed in the SCSI error handler path.
2728 */
Dan Williams38d88792011-06-23 14:33:48 -07002729 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002730 response = SAS_TASK_UNDELIVERED;
2731
2732 /* See if the device has been/is being stopped. Note
2733 * that we ignore the quiesce state, since we are
2734 * concerned about the actual device state.
2735 */
Dan Williams209fae12011-06-13 17:39:44 -07002736 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002737 status = SAS_DEVICE_UNKNOWN;
2738 else
2739 status = SAS_ABORTED_TASK;
2740
2741 complete_to_host = isci_perform_normal_io_completion;
2742 break;
2743
2744 case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
2745
2746 isci_request_handle_controller_specific_errors(
Dan Williams209fae12011-06-13 17:39:44 -07002747 idev, request, task, &response, &status,
Dan Williams6f231dd2011-07-02 22:56:22 -07002748 &complete_to_host);
2749
2750 break;
2751
2752 case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
2753 /* This is a special case, in that the I/O completion
2754 * is telling us that the device needs a reset.
2755 * In order for the device reset condition to be
2756 * noticed, the I/O has to be handled in the error
2757 * handler. Set the reset flag and cause the
2758 * SCSI error thread to be scheduled.
2759 */
2760 spin_lock_irqsave(&task->task_state_lock, task_flags);
2761 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
2762 spin_unlock_irqrestore(&task->task_state_lock, task_flags);
2763
Jeff Skirvinaa145102011-03-07 16:40:47 -07002764 /* Fail the I/O. */
2765 response = SAS_TASK_UNDELIVERED;
2766 status = SAM_STAT_TASK_ABORTED;
2767
Dan Williams6f231dd2011-07-02 22:56:22 -07002768 complete_to_host = isci_perform_error_io_completion;
Dan Williams38d88792011-06-23 14:33:48 -07002769 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002770 break;
2771
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002772 case SCI_FAILURE_RETRY_REQUIRED:
2773
2774 /* Fail the I/O so it can be retried. */
2775 response = SAS_TASK_UNDELIVERED;
Dan Williams209fae12011-06-13 17:39:44 -07002776 if (!idev)
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002777 status = SAS_DEVICE_UNKNOWN;
2778 else
2779 status = SAS_ABORTED_TASK;
2780
2781 complete_to_host = isci_perform_normal_io_completion;
Dan Williams38d88792011-06-23 14:33:48 -07002782 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002783 break;
2784
2785
Dan Williams6f231dd2011-07-02 22:56:22 -07002786 default:
2787 /* Catch any otherwise unhandled error codes here. */
2788 dev_warn(&isci_host->pdev->dev,
2789 "%s: invalid completion code: 0x%x - "
2790 "isci_request = %p\n",
2791 __func__, completion_status, request);
2792
2793 response = SAS_TASK_UNDELIVERED;
2794
2795 /* See if the device has been/is being stopped. Note
2796 * that we ignore the quiesce state, since we are
2797 * concerned about the actual device state.
2798 */
Dan Williams209fae12011-06-13 17:39:44 -07002799 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002800 status = SAS_DEVICE_UNKNOWN;
2801 else
2802 status = SAS_ABORTED_TASK;
2803
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002804 if (SAS_PROTOCOL_SMP == task->task_proto) {
Dan Williams38d88792011-06-23 14:33:48 -07002805 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002806 complete_to_host = isci_perform_normal_io_completion;
2807 } else {
Dan Williams38d88792011-06-23 14:33:48 -07002808 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002809 complete_to_host = isci_perform_error_io_completion;
2810 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002811 break;
2812 }
2813 break;
2814 }
2815
Dan Williamsddcc7e32011-06-17 10:40:43 -07002816 switch (task->task_proto) {
2817 case SAS_PROTOCOL_SSP:
2818 if (task->data_dir == DMA_NONE)
2819 break;
2820 if (task->num_scatter == 0)
2821 /* 0 indicates a single dma address */
2822 dma_unmap_single(&isci_host->pdev->dev,
2823 request->zero_scatter_daddr,
2824 task->total_xfer_len, task->data_dir);
2825 else /* unmap the sgl dma addresses */
2826 dma_unmap_sg(&isci_host->pdev->dev, task->scatter,
2827 request->num_sg_entries, task->data_dir);
2828 break;
Dan Williamse9bf7092011-06-16 16:59:56 -07002829 case SAS_PROTOCOL_SMP: {
2830 struct scatterlist *sg = &task->smp_task.smp_req;
2831 struct smp_req *smp_req;
2832 void *kaddr;
2833
2834 dma_unmap_sg(&isci_host->pdev->dev, sg, 1, DMA_TO_DEVICE);
2835
2836 /* need to swab it back in case the command buffer is re-used */
2837 kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
2838 smp_req = kaddr + sg->offset;
2839 sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
2840 kunmap_atomic(kaddr, KM_IRQ0);
2841 break;
2842 }
Dan Williamsddcc7e32011-06-17 10:40:43 -07002843 default:
2844 break;
2845 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002846
2847 /* Put the completed request on the correct list */
2848 isci_task_save_for_upper_layer_completion(isci_host, request, response,
2849 status, complete_to_host
2850 );
2851
2852 /* complete the io request to the core. */
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002853 scic_controller_complete_io(&isci_host->sci,
Dan Williams209fae12011-06-13 17:39:44 -07002854 request->sci.target_device,
Dan Williams67ea8382011-05-08 11:47:15 -07002855 &request->sci);
Dan Williams209fae12011-06-13 17:39:44 -07002856 isci_put_device(idev);
2857
Dan Williams67ea8382011-05-08 11:47:15 -07002858 /* set terminated handle so it cannot be completed or
Dan Williams6f231dd2011-07-02 22:56:22 -07002859 * terminated again, and to cause any calls into abort
2860 * task to recognize the already completed case.
2861 */
Dan Williams38d88792011-06-23 14:33:48 -07002862 set_bit(IREQ_TERMINATED, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002863}
Dan Williamsf1f52e72011-05-10 02:28:45 -07002864
Dan Williams9269e0e2011-05-12 07:42:17 -07002865static void scic_sds_request_started_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002866{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002867 struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
Dan Williamsf1393032011-05-10 02:28:47 -07002868 struct isci_request *ireq = sci_req_to_ireq(sci_req);
2869 struct domain_device *dev = sci_dev_to_domain(sci_req->target_device);
Dan Williamsc72086e2011-05-10 02:28:48 -07002870 struct sas_task *task;
2871
2872 /* XXX as hch said always creating an internal sas_task for tmf
2873 * requests would simplify the driver
2874 */
2875 task = ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL;
Dan Williamsf1f52e72011-05-10 02:28:45 -07002876
Dan Williams5dec6f42011-05-10 02:28:49 -07002877 /* all unaccelerated request types (non ssp or ncq) handled with
2878 * substates
Dan Williamsf1393032011-05-10 02:28:47 -07002879 */
Dan Williamsc72086e2011-05-10 02:28:48 -07002880 if (!task && dev->dev_type == SAS_END_DEV) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002881 sci_change_state(sm, SCI_REQ_TASK_WAIT_TC_COMP);
Dan Williams5dec6f42011-05-10 02:28:49 -07002882 } else if (!task &&
2883 (isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_high ||
2884 isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_low)) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002885 sci_change_state(sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED);
Dan Williamsc72086e2011-05-10 02:28:48 -07002886 } else if (task && task->task_proto == SAS_PROTOCOL_SMP) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002887 sci_change_state(sm, SCI_REQ_SMP_WAIT_RESP);
Dan Williams5dec6f42011-05-10 02:28:49 -07002888 } else if (task && sas_protocol_ata(task->task_proto) &&
2889 !task->ata_task.use_ncq) {
2890 u32 state;
2891
2892 if (task->data_dir == DMA_NONE)
Edmund Nadolskie3013702011-06-02 00:10:43 +00002893 state = SCI_REQ_STP_NON_DATA_WAIT_H2D;
Dan Williams5dec6f42011-05-10 02:28:49 -07002894 else if (task->ata_task.dma_xfer)
Edmund Nadolskie3013702011-06-02 00:10:43 +00002895 state = SCI_REQ_STP_UDMA_WAIT_TC_COMP;
Dan Williams5dec6f42011-05-10 02:28:49 -07002896 else /* PIO */
Edmund Nadolskie3013702011-06-02 00:10:43 +00002897 state = SCI_REQ_STP_PIO_WAIT_H2D;
Dan Williams5dec6f42011-05-10 02:28:49 -07002898
Edmund Nadolskie3013702011-06-02 00:10:43 +00002899 sci_change_state(sm, state);
Dan Williamsc72086e2011-05-10 02:28:48 -07002900 }
Dan Williamsf1f52e72011-05-10 02:28:45 -07002901}
2902
Dan Williams9269e0e2011-05-12 07:42:17 -07002903static void scic_sds_request_completed_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002904{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002905 struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
Dan Williams79e2b6b2011-05-11 08:29:56 -07002906 struct scic_sds_controller *scic = sci_req->owning_controller;
Dan Williamsf1f52e72011-05-10 02:28:45 -07002907 struct isci_host *ihost = scic_to_ihost(scic);
2908 struct isci_request *ireq = sci_req_to_ireq(sci_req);
2909
Dan Williamsf1f52e72011-05-10 02:28:45 -07002910 /* Tell the SCI_USER that the IO request is complete */
Dan Williams38d88792011-06-23 14:33:48 -07002911 if (!test_bit(IREQ_TMF, &ireq->flags))
Dan Williamsf1f52e72011-05-10 02:28:45 -07002912 isci_request_io_request_complete(ihost, ireq,
2913 sci_req->sci_status);
2914 else
2915 isci_task_request_complete(ihost, ireq, sci_req->sci_status);
2916}
2917
Dan Williams9269e0e2011-05-12 07:42:17 -07002918static void scic_sds_request_aborting_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07002919{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002920 struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
Dan Williamsf1f52e72011-05-10 02:28:45 -07002921
2922 /* Setting the abort bit in the Task Context is required by the silicon. */
Dan Williams312e0c22011-06-28 13:47:09 -07002923 sci_req->tc->abort = 1;
Dan Williamsc72086e2011-05-10 02:28:48 -07002924}
2925
Dan Williams9269e0e2011-05-12 07:42:17 -07002926static 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 -07002927{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002928 struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
Dan Williams5dec6f42011-05-10 02:28:49 -07002929
Dan Williams79e2b6b2011-05-11 08:29:56 -07002930 scic_sds_remote_device_set_working_request(sci_req->target_device,
2931 sci_req);
Dan Williams5dec6f42011-05-10 02:28:49 -07002932}
2933
Dan Williams9269e0e2011-05-12 07:42:17 -07002934static void scic_sds_stp_request_started_pio_await_h2d_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07002935{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002936 struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
Dan Williams5dec6f42011-05-10 02:28:49 -07002937
Dan Williams79e2b6b2011-05-11 08:29:56 -07002938 scic_sds_remote_device_set_working_request(sci_req->target_device,
2939 sci_req);
Dan Williams5dec6f42011-05-10 02:28:49 -07002940}
2941
Dan Williams9269e0e2011-05-12 07:42:17 -07002942static 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 -07002943{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002944 struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
Dan Williams5dec6f42011-05-10 02:28:49 -07002945
Dan Williams79e2b6b2011-05-11 08:29:56 -07002946 scic_sds_remote_device_set_working_request(sci_req->target_device,
2947 sci_req);
Dan Williams5dec6f42011-05-10 02:28:49 -07002948}
2949
Dan Williams9269e0e2011-05-12 07:42:17 -07002950static 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 -07002951{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002952 struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
Dan Williams312e0c22011-06-28 13:47:09 -07002953 struct scu_task_context *tc = sci_req->tc;
Dan Williams5dec6f42011-05-10 02:28:49 -07002954 struct host_to_dev_fis *h2d_fis;
2955 enum sci_status status;
2956
2957 /* Clear the SRST bit */
2958 h2d_fis = &sci_req->stp.cmd;
2959 h2d_fis->control = 0;
2960
2961 /* Clear the TC control bit */
Dan Williams312e0c22011-06-28 13:47:09 -07002962 tc->control_frame = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07002963
2964 status = scic_controller_continue_io(sci_req);
Dan Williams79e2b6b2011-05-11 08:29:56 -07002965 WARN_ONCE(status != SCI_SUCCESS, "isci: continue io failure\n");
Dan Williams5dec6f42011-05-10 02:28:49 -07002966}
2967
Dan Williamsf1f52e72011-05-10 02:28:45 -07002968static const struct sci_base_state scic_sds_request_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002969 [SCI_REQ_INIT] = { },
2970 [SCI_REQ_CONSTRUCTED] = { },
2971 [SCI_REQ_STARTED] = {
Dan Williamsf1f52e72011-05-10 02:28:45 -07002972 .enter_state = scic_sds_request_started_state_enter,
Dan Williams5dec6f42011-05-10 02:28:49 -07002973 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002974 [SCI_REQ_STP_NON_DATA_WAIT_H2D] = {
Dan Williams5dec6f42011-05-10 02:28:49 -07002975 .enter_state = scic_sds_stp_request_started_non_data_await_h2d_completion_enter,
2976 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002977 [SCI_REQ_STP_NON_DATA_WAIT_D2H] = { },
2978 [SCI_REQ_STP_PIO_WAIT_H2D] = {
Dan Williams5dec6f42011-05-10 02:28:49 -07002979 .enter_state = scic_sds_stp_request_started_pio_await_h2d_completion_enter,
2980 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002981 [SCI_REQ_STP_PIO_WAIT_FRAME] = { },
2982 [SCI_REQ_STP_PIO_DATA_IN] = { },
2983 [SCI_REQ_STP_PIO_DATA_OUT] = { },
2984 [SCI_REQ_STP_UDMA_WAIT_TC_COMP] = { },
2985 [SCI_REQ_STP_UDMA_WAIT_D2H] = { },
2986 [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED] = {
Dan Williams5dec6f42011-05-10 02:28:49 -07002987 .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter,
2988 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002989 [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG] = {
Dan Williams5dec6f42011-05-10 02:28:49 -07002990 .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter,
2991 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00002992 [SCI_REQ_STP_SOFT_RESET_WAIT_D2H] = { },
2993 [SCI_REQ_TASK_WAIT_TC_COMP] = { },
2994 [SCI_REQ_TASK_WAIT_TC_RESP] = { },
2995 [SCI_REQ_SMP_WAIT_RESP] = { },
2996 [SCI_REQ_SMP_WAIT_TC_COMP] = { },
2997 [SCI_REQ_COMPLETED] = {
Dan Williamsf1f52e72011-05-10 02:28:45 -07002998 .enter_state = scic_sds_request_completed_state_enter,
2999 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00003000 [SCI_REQ_ABORTING] = {
Dan Williamsf1f52e72011-05-10 02:28:45 -07003001 .enter_state = scic_sds_request_aborting_state_enter,
3002 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00003003 [SCI_REQ_FINAL] = { },
Dan Williamsf1f52e72011-05-10 02:28:45 -07003004};
3005
Edmund Nadolskie3013702011-06-02 00:10:43 +00003006static void
3007scic_sds_general_request_construct(struct scic_sds_controller *scic,
3008 struct scic_sds_remote_device *sci_dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00003009 struct scic_sds_request *sci_req)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003010{
Edmund Nadolski12ef6542011-06-02 00:10:50 +00003011 sci_init_sm(&sci_req->sm, scic_sds_request_state_table, SCI_REQ_INIT);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003012
Dan Williamsf1f52e72011-05-10 02:28:45 -07003013 sci_req->target_device = sci_dev;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003014 sci_req->protocol = SCIC_NO_PROTOCOL;
3015 sci_req->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003016
3017 sci_req->sci_status = SCI_SUCCESS;
3018 sci_req->scu_status = 0;
3019 sci_req->post_context = 0xFFFFFFFF;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003020}
3021
3022static enum sci_status
3023scic_io_request_construct(struct scic_sds_controller *scic,
3024 struct scic_sds_remote_device *sci_dev,
Dan Williamsdb056252011-06-17 14:18:39 -07003025 struct scic_sds_request *sci_req)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003026{
3027 struct domain_device *dev = sci_dev_to_domain(sci_dev);
3028 enum sci_status status = SCI_SUCCESS;
3029
3030 /* Build the common part of the request */
Dan Williamsdb056252011-06-17 14:18:39 -07003031 scic_sds_general_request_construct(scic, sci_dev, sci_req);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003032
Dan Williamsc72086e2011-05-10 02:28:48 -07003033 if (sci_dev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003034 return SCI_FAILURE_INVALID_REMOTE_DEVICE;
3035
3036 if (dev->dev_type == SAS_END_DEV)
Dan Williamsc72086e2011-05-10 02:28:48 -07003037 /* pass */;
3038 else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
Dan Williamsf1f52e72011-05-10 02:28:45 -07003039 memset(&sci_req->stp.cmd, 0, sizeof(sci_req->stp.cmd));
Dan Williamsc72086e2011-05-10 02:28:48 -07003040 else if (dev_is_expander(dev))
Dan Williamse9bf7092011-06-16 16:59:56 -07003041 /* pass */;
Dan Williamsc72086e2011-05-10 02:28:48 -07003042 else
3043 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003044
Dan Williams312e0c22011-06-28 13:47:09 -07003045 memset(sci_req->tc, 0, offsetof(struct scu_task_context, sgl_pair_ab));
Dan Williamsf1f52e72011-05-10 02:28:45 -07003046
3047 return status;
3048}
3049
3050enum sci_status scic_task_request_construct(struct scic_sds_controller *scic,
3051 struct scic_sds_remote_device *sci_dev,
3052 u16 io_tag, struct scic_sds_request *sci_req)
3053{
3054 struct domain_device *dev = sci_dev_to_domain(sci_dev);
3055 enum sci_status status = SCI_SUCCESS;
3056
3057 /* Build the common part of the request */
Dan Williamsdb056252011-06-17 14:18:39 -07003058 scic_sds_general_request_construct(scic, sci_dev, sci_req);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003059
Dan Williamsc72086e2011-05-10 02:28:48 -07003060 if (dev->dev_type == SAS_END_DEV ||
3061 dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams38d88792011-06-23 14:33:48 -07003062 set_bit(IREQ_TMF, &sci_req_to_ireq(sci_req)->flags);
Dan Williams312e0c22011-06-28 13:47:09 -07003063 memset(sci_req->tc, 0, sizeof(struct scu_task_context));
Dan Williamsc72086e2011-05-10 02:28:48 -07003064 } else
3065 status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003066
3067 return status;
3068}
3069
3070static enum sci_status isci_request_ssp_request_construct(
3071 struct isci_request *request)
3072{
3073 enum sci_status status;
3074
3075 dev_dbg(&request->isci_host->pdev->dev,
3076 "%s: request = %p\n",
3077 __func__,
3078 request);
3079 status = scic_io_request_construct_basic_ssp(&request->sci);
3080 return status;
3081}
3082
3083static enum sci_status isci_request_stp_request_construct(
3084 struct isci_request *request)
3085{
3086 struct sas_task *task = isci_request_access_task(request);
3087 enum sci_status status;
3088 struct host_to_dev_fis *register_fis;
3089
3090 dev_dbg(&request->isci_host->pdev->dev,
3091 "%s: request = %p\n",
3092 __func__,
3093 request);
3094
3095 /* Get the host_to_dev_fis from the core and copy
3096 * the fis from the task into it.
3097 */
3098 register_fis = isci_sata_task_to_fis_copy(task);
3099
3100 status = scic_io_request_construct_basic_sata(&request->sci);
3101
3102 /* Set the ncq tag in the fis, from the queue
3103 * command in the task.
3104 */
3105 if (isci_sata_is_task_ncq(task)) {
3106
3107 isci_sata_set_ncq_tag(
3108 register_fis,
3109 task
3110 );
3111 }
3112
3113 return status;
3114}
3115
Dan Williamse9bf7092011-06-16 16:59:56 -07003116static enum sci_status
3117scic_io_request_construct_smp(struct device *dev,
3118 struct scic_sds_request *sci_req,
3119 struct sas_task *task)
Dan Williamsc72086e2011-05-10 02:28:48 -07003120{
Dan Williamse9bf7092011-06-16 16:59:56 -07003121 struct scatterlist *sg = &task->smp_task.smp_req;
Dan Williamsc72086e2011-05-10 02:28:48 -07003122 struct scic_sds_remote_device *sci_dev;
Dan Williamsc72086e2011-05-10 02:28:48 -07003123 struct scu_task_context *task_context;
Dan Williamse9bf7092011-06-16 16:59:56 -07003124 struct scic_sds_port *sci_port;
3125 struct smp_req *smp_req;
3126 void *kaddr;
3127 u8 req_len;
3128 u32 cmd;
3129
3130 kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
3131 smp_req = kaddr + sg->offset;
3132 /*
3133 * Look at the SMP requests' header fields; for certain SAS 1.x SMP
3134 * functions under SAS 2.0, a zero request length really indicates
3135 * a non-zero default length.
3136 */
3137 if (smp_req->req_len == 0) {
3138 switch (smp_req->func) {
3139 case SMP_DISCOVER:
3140 case SMP_REPORT_PHY_ERR_LOG:
3141 case SMP_REPORT_PHY_SATA:
3142 case SMP_REPORT_ROUTE_INFO:
3143 smp_req->req_len = 2;
3144 break;
3145 case SMP_CONF_ROUTE_INFO:
3146 case SMP_PHY_CONTROL:
3147 case SMP_PHY_TEST_FUNCTION:
3148 smp_req->req_len = 9;
3149 break;
3150 /* Default - zero is a valid default for 2.0. */
3151 }
3152 }
3153 req_len = smp_req->req_len;
3154 sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
3155 cmd = *(u32 *) smp_req;
3156 kunmap_atomic(kaddr, KM_IRQ0);
3157
3158 if (!dma_map_sg(dev, sg, 1, DMA_TO_DEVICE))
3159 return SCI_FAILURE;
3160
3161 sci_req->protocol = SCIC_SMP_PROTOCOL;
Dan Williamsc72086e2011-05-10 02:28:48 -07003162
3163 /* byte swap the smp request. */
Dan Williamsc72086e2011-05-10 02:28:48 -07003164
Dan Williams312e0c22011-06-28 13:47:09 -07003165 task_context = sci_req->tc;
Dan Williamsc72086e2011-05-10 02:28:48 -07003166
Dan Williamsc72086e2011-05-10 02:28:48 -07003167 sci_dev = scic_sds_request_get_device(sci_req);
3168 sci_port = scic_sds_request_get_port(sci_req);
3169
3170 /*
3171 * Fill in the TC with the its required data
3172 * 00h
3173 */
3174 task_context->priority = 0;
3175 task_context->initiator_request = 1;
3176 task_context->connection_rate = sci_dev->connection_rate;
3177 task_context->protocol_engine_index =
3178 scic_sds_controller_get_protocol_engine_group(scic);
3179 task_context->logical_port_index = scic_sds_port_get_index(sci_port);
3180 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
3181 task_context->abort = 0;
3182 task_context->valid = SCU_TASK_CONTEXT_VALID;
3183 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
3184
3185 /* 04h */
3186 task_context->remote_node_index = sci_dev->rnc.remote_node_index;
3187 task_context->command_code = 0;
3188 task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;
3189
3190 /* 08h */
3191 task_context->link_layer_control = 0;
3192 task_context->do_not_dma_ssp_good_response = 1;
3193 task_context->strict_ordering = 0;
3194 task_context->control_frame = 1;
3195 task_context->timeout_enable = 0;
3196 task_context->block_guard_enable = 0;
3197
3198 /* 0ch */
3199 task_context->address_modifier = 0;
3200
3201 /* 10h */
Dave Jiang77d67382011-05-25 02:21:57 +00003202 task_context->ssp_command_iu_length = req_len;
Dan Williamsc72086e2011-05-10 02:28:48 -07003203
3204 /* 14h */
3205 task_context->transfer_length_bytes = 0;
3206
3207 /*
3208 * 18h ~ 30h, protocol specific
3209 * since commandIU has been build by framework at this point, we just
3210 * copy the frist DWord from command IU to this location. */
Dan Williamse9bf7092011-06-16 16:59:56 -07003211 memcpy(&task_context->type.smp, &cmd, sizeof(u32));
Dan Williamsc72086e2011-05-10 02:28:48 -07003212
3213 /*
3214 * 40h
3215 * "For SMP you could program it to zero. We would prefer that way
3216 * so that done code will be consistent." - Venki
3217 */
3218 task_context->task_phase = 0;
3219
Dan Williams312e0c22011-06-28 13:47:09 -07003220 sci_req->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
3221 (scic_sds_controller_get_protocol_engine_group(scic) <<
3222 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
3223 (scic_sds_port_get_index(sci_port) <<
3224 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
3225 ISCI_TAG_TCI(sci_req->io_tag));
Dan Williamsc72086e2011-05-10 02:28:48 -07003226 /*
3227 * Copy the physical address for the command buffer to the SCU Task
3228 * Context command buffer should not contain command header.
3229 */
Dan Williamse9bf7092011-06-16 16:59:56 -07003230 task_context->command_iu_upper = upper_32_bits(sg_dma_address(sg));
3231 task_context->command_iu_lower = lower_32_bits(sg_dma_address(sg) + sizeof(u32));
Dan Williamsc72086e2011-05-10 02:28:48 -07003232
3233 /* SMP response comes as UF, so no need to set response IU address. */
3234 task_context->response_iu_upper = 0;
3235 task_context->response_iu_lower = 0;
Dan Williamsc72086e2011-05-10 02:28:48 -07003236
Edmund Nadolskie3013702011-06-02 00:10:43 +00003237 sci_change_state(&sci_req->sm, SCI_REQ_CONSTRUCTED);
Dan Williamsc72086e2011-05-10 02:28:48 -07003238
3239 return SCI_SUCCESS;
3240}
3241
3242/*
Dan Williamsf1f52e72011-05-10 02:28:45 -07003243 * isci_smp_request_build() - This function builds the smp request.
3244 * @ireq: This parameter points to the isci_request allocated in the
3245 * request construct function.
3246 *
3247 * SCI_SUCCESS on successfull completion, or specific failure code.
3248 */
3249static enum sci_status isci_smp_request_build(struct isci_request *ireq)
3250{
Dan Williamsf1f52e72011-05-10 02:28:45 -07003251 struct sas_task *task = isci_request_access_task(ireq);
Dan Williamse9bf7092011-06-16 16:59:56 -07003252 struct device *dev = &ireq->isci_host->pdev->dev;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003253 struct scic_sds_request *sci_req = &ireq->sci;
Dan Williamse9bf7092011-06-16 16:59:56 -07003254 enum sci_status status = SCI_FAILURE;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003255
Dan Williamse9bf7092011-06-16 16:59:56 -07003256 status = scic_io_request_construct_smp(dev, sci_req, task);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003257 if (status != SCI_SUCCESS)
3258 dev_warn(&ireq->isci_host->pdev->dev,
3259 "%s: failed with status = %d\n",
3260 __func__,
3261 status);
3262
3263 return status;
3264}
3265
3266/**
3267 * isci_io_request_build() - This function builds the io request object.
3268 * @isci_host: This parameter specifies the ISCI host object
3269 * @request: This parameter points to the isci_request object allocated in the
3270 * request construct function.
3271 * @sci_device: This parameter is the handle for the sci core's remote device
3272 * object that is the destination for this request.
3273 *
3274 * SCI_SUCCESS on successfull completion, or specific failure code.
3275 */
Dan Williams312e0c22011-06-28 13:47:09 -07003276static enum sci_status isci_io_request_build(struct isci_host *isci_host,
3277 struct isci_request *request,
Dan Williamsdb056252011-06-17 14:18:39 -07003278 struct isci_remote_device *isci_device)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003279{
3280 enum sci_status status = SCI_SUCCESS;
3281 struct sas_task *task = isci_request_access_task(request);
3282 struct scic_sds_remote_device *sci_device = &isci_device->sci;
3283
3284 dev_dbg(&isci_host->pdev->dev,
3285 "%s: isci_device = 0x%p; request = %p, "
3286 "num_scatter = %d\n",
3287 __func__,
3288 isci_device,
3289 request,
3290 task->num_scatter);
3291
3292 /* map the sgl addresses, if present.
3293 * libata does the mapping for sata devices
3294 * before we get the request.
3295 */
3296 if (task->num_scatter &&
3297 !sas_protocol_ata(task->task_proto) &&
3298 !(SAS_PROTOCOL_SMP & task->task_proto)) {
3299
3300 request->num_sg_entries = dma_map_sg(
3301 &isci_host->pdev->dev,
3302 task->scatter,
3303 task->num_scatter,
3304 task->data_dir
3305 );
3306
3307 if (request->num_sg_entries == 0)
3308 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
3309 }
3310
Dan Williamsf1f52e72011-05-10 02:28:45 -07003311 status = scic_io_request_construct(&isci_host->sci, sci_device,
Dan Williamsdb056252011-06-17 14:18:39 -07003312 &request->sci);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003313
3314 if (status != SCI_SUCCESS) {
3315 dev_warn(&isci_host->pdev->dev,
3316 "%s: failed request construct\n",
3317 __func__);
3318 return SCI_FAILURE;
3319 }
3320
3321 switch (task->task_proto) {
3322 case SAS_PROTOCOL_SMP:
3323 status = isci_smp_request_build(request);
3324 break;
3325 case SAS_PROTOCOL_SSP:
3326 status = isci_request_ssp_request_construct(request);
3327 break;
3328 case SAS_PROTOCOL_SATA:
3329 case SAS_PROTOCOL_STP:
3330 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
3331 status = isci_request_stp_request_construct(request);
3332 break;
3333 default:
3334 dev_warn(&isci_host->pdev->dev,
3335 "%s: unknown protocol\n", __func__);
3336 return SCI_FAILURE;
3337 }
3338
3339 return SCI_SUCCESS;
3340}
3341
Dan Williamsdb056252011-06-17 14:18:39 -07003342static struct isci_request *isci_request_from_tag(struct isci_host *ihost, u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003343{
Dan Williams0d0cf142011-06-13 00:51:30 -07003344 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003345
Dan Williamsdb056252011-06-17 14:18:39 -07003346 ireq = ihost->reqs[ISCI_TAG_TCI(tag)];
3347 ireq->sci.io_tag = tag;
Dan Williams0d0cf142011-06-13 00:51:30 -07003348 ireq->io_request_completion = NULL;
Dan Williams38d88792011-06-23 14:33:48 -07003349 ireq->flags = 0;
Dan Williams0d0cf142011-06-13 00:51:30 -07003350 ireq->num_sg_entries = 0;
Dan Williams0d0cf142011-06-13 00:51:30 -07003351 INIT_LIST_HEAD(&ireq->completed_node);
3352 INIT_LIST_HEAD(&ireq->dev_node);
Dan Williams0d0cf142011-06-13 00:51:30 -07003353 isci_request_change_state(ireq, allocated);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003354
Dan Williams0d0cf142011-06-13 00:51:30 -07003355 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003356}
3357
Dan Williamsdb056252011-06-17 14:18:39 -07003358static struct isci_request *isci_io_request_from_tag(struct isci_host *ihost,
3359 struct sas_task *task,
3360 u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003361{
Dan Williams0d0cf142011-06-13 00:51:30 -07003362 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003363
Dan Williamsdb056252011-06-17 14:18:39 -07003364 ireq = isci_request_from_tag(ihost, tag);
3365 ireq->ttype_ptr.io_task_ptr = task;
3366 ireq->ttype = io_task;
3367 task->lldd_task = ireq;
3368
Dan Williams0d0cf142011-06-13 00:51:30 -07003369 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003370}
3371
Dan Williamsdb056252011-06-17 14:18:39 -07003372struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
3373 struct isci_tmf *isci_tmf,
3374 u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003375{
Dan Williams0d0cf142011-06-13 00:51:30 -07003376 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003377
Dan Williamsdb056252011-06-17 14:18:39 -07003378 ireq = isci_request_from_tag(ihost, tag);
3379 ireq->ttype_ptr.tmf_task_ptr = isci_tmf;
3380 ireq->ttype = tmf_task;
3381
Dan Williams0d0cf142011-06-13 00:51:30 -07003382 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003383}
3384
Dan Williams209fae12011-06-13 17:39:44 -07003385int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
Dan Williamsdb056252011-06-17 14:18:39 -07003386 struct sas_task *task, u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003387{
Dan Williamsf1f52e72011-05-10 02:28:45 -07003388 enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams0d0cf142011-06-13 00:51:30 -07003389 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003390 unsigned long flags;
Dan Williams0d0cf142011-06-13 00:51:30 -07003391 int ret = 0;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003392
Dan Williamsf1f52e72011-05-10 02:28:45 -07003393 /* do common allocation and init of request object. */
Dan Williamsdb056252011-06-17 14:18:39 -07003394 ireq = isci_io_request_from_tag(ihost, task, tag);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003395
Dan Williamsdb056252011-06-17 14:18:39 -07003396 status = isci_io_request_build(ihost, ireq, idev);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003397 if (status != SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -07003398 dev_warn(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003399 "%s: request_construct failed - status = 0x%x\n",
3400 __func__,
3401 status);
Dan Williamsdb056252011-06-17 14:18:39 -07003402 return status;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003403 }
3404
Dan Williams0d0cf142011-06-13 00:51:30 -07003405 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003406
Jeff Skirvin9274f452011-06-23 17:09:02 -07003407 if (test_bit(IDEV_IO_NCQERROR, &idev->flags)) {
3408
3409 if (isci_task_is_ncq_recovery(task)) {
3410
3411 /* The device is in an NCQ recovery state. Issue the
3412 * request on the task side. Note that it will
3413 * complete on the I/O request side because the
3414 * request was built that way (ie.
3415 * ireq->is_task_management_request is false).
3416 */
3417 status = scic_controller_start_task(&ihost->sci,
3418 &idev->sci,
Dan Williams312e0c22011-06-28 13:47:09 -07003419 &ireq->sci);
Jeff Skirvin9274f452011-06-23 17:09:02 -07003420 } else {
3421 status = SCI_FAILURE;
3422 }
3423 } else {
Jeff Skirvin9274f452011-06-23 17:09:02 -07003424 /* send the request, let the core assign the IO TAG. */
3425 status = scic_controller_start_io(&ihost->sci, &idev->sci,
Dan Williams312e0c22011-06-28 13:47:09 -07003426 &ireq->sci);
Jeff Skirvin9274f452011-06-23 17:09:02 -07003427 }
Dan Williams312e0c22011-06-28 13:47:09 -07003428
Dan Williamsf1f52e72011-05-10 02:28:45 -07003429 if (status != SCI_SUCCESS &&
3430 status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
Dan Williams0d0cf142011-06-13 00:51:30 -07003431 dev_warn(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003432 "%s: failed request start (0x%x)\n",
3433 __func__, status);
Dan Williams0d0cf142011-06-13 00:51:30 -07003434 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsdb056252011-06-17 14:18:39 -07003435 return status;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003436 }
3437
3438 /* Either I/O started OK, or the core has signaled that
3439 * the device needs a target reset.
3440 *
3441 * In either case, hold onto the I/O for later.
3442 *
3443 * Update it's status and add it to the list in the
3444 * remote device object.
3445 */
Dan Williams0d0cf142011-06-13 00:51:30 -07003446 list_add(&ireq->dev_node, &idev->reqs_in_process);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003447
3448 if (status == SCI_SUCCESS) {
3449 /* Save the tag for possible task mgmt later. */
Dan Williams0d0cf142011-06-13 00:51:30 -07003450 ireq->io_tag = ireq->sci.io_tag;
3451 isci_request_change_state(ireq, started);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003452 } else {
3453 /* The request did not really start in the
3454 * hardware, so clear the request handle
3455 * here so no terminations will be done.
3456 */
Dan Williams38d88792011-06-23 14:33:48 -07003457 set_bit(IREQ_TERMINATED, &ireq->flags);
Dan Williams0d0cf142011-06-13 00:51:30 -07003458 isci_request_change_state(ireq, completed);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003459 }
Dan Williams0d0cf142011-06-13 00:51:30 -07003460 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003461
3462 if (status ==
3463 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
3464 /* Signal libsas that we need the SCSI error
Dan Williams312e0c22011-06-28 13:47:09 -07003465 * handler thread to work on this I/O and that
3466 * we want a device reset.
3467 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07003468 spin_lock_irqsave(&task->task_state_lock, flags);
3469 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
3470 spin_unlock_irqrestore(&task->task_state_lock, flags);
3471
3472 /* Cause this task to be scheduled in the SCSI error
Dan Williams312e0c22011-06-28 13:47:09 -07003473 * handler thread.
3474 */
Dan Williams0d0cf142011-06-13 00:51:30 -07003475 isci_execpath_callback(ihost, task,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003476 sas_task_abort);
3477
3478 /* Change the status, since we are holding
Dan Williams312e0c22011-06-28 13:47:09 -07003479 * the I/O until it is managed by the SCSI
3480 * error handler.
3481 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07003482 status = SCI_SUCCESS;
3483 }
3484
Dan Williamsf1f52e72011-05-10 02:28:45 -07003485 return ret;
3486}