]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/isci/request.c
isci: unify request start handlers
[mirror_ubuntu-artful-kernel.git] / drivers / scsi / isci / request.c
CommitLineData
6f231dda
DW
1/*
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"
6f231dda
DW
57#include "task.h"
58#include "request.h"
59#include "sata.h"
60#include "scu_completion_codes.h"
5dec6f4e 61#include "scu_event_codes.h"
2ec53eb4 62#include "sas.h"
6f231dda 63
f1f52e75
DW
64/**
65 * This method returns the sgl element pair for the specificed sgl_pair index.
66 * @sci_req: This parameter specifies the IO request for which to retrieve
67 * the Scatter-Gather List element pair.
68 * @sgl_pair_index: This parameter specifies the index into the SGL element
69 * pair to be retrieved.
70 *
71 * This method returns a pointer to an struct scu_sgl_element_pair.
72 */
73static struct scu_sgl_element_pair *scic_sds_request_get_sgl_element_pair(
74 struct scic_sds_request *sci_req,
75 u32 sgl_pair_index
76 ) {
77 struct scu_task_context *task_context;
78
79 task_context = (struct scu_task_context *)sci_req->task_context_buffer;
80
81 if (sgl_pair_index == 0) {
82 return &task_context->sgl_pair_ab;
83 } else if (sgl_pair_index == 1) {
84 return &task_context->sgl_pair_cd;
85 }
6f231dda 86
f1f52e75 87 return &sci_req->sg_table[sgl_pair_index - 2];
6f231dda
DW
88}
89
f1f52e75
DW
90/**
91 * This function will build the SGL list for an IO request.
92 * @sci_req: This parameter specifies the IO request for which to build
93 * the Scatter-Gather List.
94 *
95 */
5dec6f4e 96static void scic_sds_request_build_sgl(struct scic_sds_request *sds_request)
6f231dda 97{
f1f52e75
DW
98 struct isci_request *isci_request = sci_req_to_ireq(sds_request);
99 struct isci_host *isci_host = isci_request->isci_host;
100 struct sas_task *task = isci_request_access_task(isci_request);
101 struct scatterlist *sg = NULL;
102 dma_addr_t dma_addr;
103 u32 sg_idx = 0;
104 struct scu_sgl_element_pair *scu_sg = NULL;
105 struct scu_sgl_element_pair *prev_sg = NULL;
106
107 if (task->num_scatter > 0) {
108 sg = task->scatter;
109
110 while (sg) {
111 scu_sg = scic_sds_request_get_sgl_element_pair(
112 sds_request,
113 sg_idx);
114
115 SCU_SGL_COPY(scu_sg->A, sg);
116
117 sg = sg_next(sg);
118
119 if (sg) {
120 SCU_SGL_COPY(scu_sg->B, sg);
121 sg = sg_next(sg);
122 } else
123 SCU_SGL_ZERO(scu_sg->B);
124
125 if (prev_sg) {
126 dma_addr =
127 scic_io_request_get_dma_addr(
128 sds_request,
129 scu_sg);
130
131 prev_sg->next_pair_upper =
132 upper_32_bits(dma_addr);
133 prev_sg->next_pair_lower =
134 lower_32_bits(dma_addr);
135 }
136
137 prev_sg = scu_sg;
138 sg_idx++;
139 }
140 } else { /* handle when no sg */
141 scu_sg = scic_sds_request_get_sgl_element_pair(sds_request,
142 sg_idx);
6f231dda 143
f1f52e75
DW
144 dma_addr = dma_map_single(&isci_host->pdev->dev,
145 task->scatter,
146 task->total_xfer_len,
147 task->data_dir);
6f231dda 148
f1f52e75 149 isci_request->zero_scatter_daddr = dma_addr;
6f231dda 150
f1f52e75
DW
151 scu_sg->A.length = task->total_xfer_len;
152 scu_sg->A.address_upper = upper_32_bits(dma_addr);
153 scu_sg->A.address_lower = lower_32_bits(dma_addr);
154 }
6f231dda 155
f1f52e75
DW
156 if (scu_sg) {
157 scu_sg->next_pair_upper = 0;
158 scu_sg->next_pair_lower = 0;
6f231dda 159 }
f1f52e75 160}
6f231dda 161
f1f52e75 162static void scic_sds_io_request_build_ssp_command_iu(struct scic_sds_request *sci_req)
6f231dda 163{
f1f52e75
DW
164 struct ssp_cmd_iu *cmd_iu;
165 struct isci_request *ireq = sci_req_to_ireq(sci_req);
2ec53eb4 166 struct sas_task *task = isci_request_access_task(ireq);
6f231dda 167
f1f52e75 168 cmd_iu = &sci_req->ssp.cmd;
6f231dda 169
f1f52e75
DW
170 memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
171 cmd_iu->add_cdb_len = 0;
172 cmd_iu->_r_a = 0;
173 cmd_iu->_r_b = 0;
174 cmd_iu->en_fburst = 0; /* unsupported */
175 cmd_iu->task_prio = task->ssp_task.task_prio;
176 cmd_iu->task_attr = task->ssp_task.task_attr;
177 cmd_iu->_r_c = 0;
6f231dda 178
f1f52e75
DW
179 sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cdb,
180 sizeof(task->ssp_task.cdb) / sizeof(u32));
181}
6f231dda 182
f1f52e75
DW
183static void scic_sds_task_request_build_ssp_task_iu(struct scic_sds_request *sci_req)
184{
185 struct ssp_task_iu *task_iu;
186 struct isci_request *ireq = sci_req_to_ireq(sci_req);
187 struct sas_task *task = isci_request_access_task(ireq);
188 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
6f231dda 189
f1f52e75
DW
190 task_iu = &sci_req->ssp.tmf;
191
192 memset(task_iu, 0, sizeof(struct ssp_task_iu));
193
194 memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
195
196 task_iu->task_func = isci_tmf->tmf_code;
197 task_iu->task_tag =
198 (ireq->ttype == tmf_task) ?
199 isci_tmf->io_tag :
200 SCI_CONTROLLER_INVALID_IO_TAG;
6f231dda
DW
201}
202
203/**
f1f52e75
DW
204 * This method is will fill in the SCU Task Context for any type of SSP request.
205 * @sci_req:
206 * @task_context:
6f231dda 207 *
6f231dda 208 */
f1f52e75
DW
209static void scu_ssp_reqeust_construct_task_context(
210 struct scic_sds_request *sds_request,
211 struct scu_task_context *task_context)
6f231dda 212{
f1f52e75
DW
213 dma_addr_t dma_addr;
214 struct scic_sds_controller *controller;
215 struct scic_sds_remote_device *target_device;
216 struct scic_sds_port *target_port;
217
218 controller = scic_sds_request_get_controller(sds_request);
219 target_device = scic_sds_request_get_device(sds_request);
220 target_port = scic_sds_request_get_port(sds_request);
221
222 /* Fill in the TC with the its required data */
223 task_context->abort = 0;
224 task_context->priority = 0;
225 task_context->initiator_request = 1;
226 task_context->connection_rate = target_device->connection_rate;
227 task_context->protocol_engine_index =
228 scic_sds_controller_get_protocol_engine_group(controller);
229 task_context->logical_port_index =
230 scic_sds_port_get_index(target_port);
231 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
232 task_context->valid = SCU_TASK_CONTEXT_VALID;
233 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
234
235 task_context->remote_node_index =
236 scic_sds_remote_device_get_index(sds_request->target_device);
237 task_context->command_code = 0;
238
239 task_context->link_layer_control = 0;
240 task_context->do_not_dma_ssp_good_response = 1;
241 task_context->strict_ordering = 0;
242 task_context->control_frame = 0;
243 task_context->timeout_enable = 0;
244 task_context->block_guard_enable = 0;
245
246 task_context->address_modifier = 0;
247
248 /* task_context->type.ssp.tag = sci_req->io_tag; */
249 task_context->task_phase = 0x01;
250
251 if (sds_request->was_tag_assigned_by_user) {
252 /*
253 * Build the task context now since we have already read
254 * the data
255 */
256 sds_request->post_context =
257 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
258 (scic_sds_controller_get_protocol_engine_group(
259 controller) <<
260 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
261 (scic_sds_port_get_index(target_port) <<
262 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
263 scic_sds_io_tag_get_index(sds_request->io_tag));
264 } else {
265 /*
266 * Build the task context now since we have already read
267 * the data
268 *
269 * I/O tag index is not assigned because we have to wait
270 * until we get a TCi
271 */
272 sds_request->post_context =
273 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
274 (scic_sds_controller_get_protocol_engine_group(
275 owning_controller) <<
276 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
277 (scic_sds_port_get_index(target_port) <<
278 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
279 }
6f231dda 280
f1f52e75
DW
281 /*
282 * Copy the physical address for the command buffer to the
283 * SCU Task Context
284 */
285 dma_addr = scic_io_request_get_dma_addr(sds_request,
286 &sds_request->ssp.cmd);
6f231dda 287
f1f52e75
DW
288 task_context->command_iu_upper = upper_32_bits(dma_addr);
289 task_context->command_iu_lower = lower_32_bits(dma_addr);
290
291 /*
292 * Copy the physical address for the response buffer to the
293 * SCU Task Context
6f231dda 294 */
f1f52e75
DW
295 dma_addr = scic_io_request_get_dma_addr(sds_request,
296 &sds_request->ssp.rsp);
6f231dda 297
f1f52e75
DW
298 task_context->response_iu_upper = upper_32_bits(dma_addr);
299 task_context->response_iu_lower = lower_32_bits(dma_addr);
300}
6f231dda 301
f1f52e75
DW
302/**
303 * This method is will fill in the SCU Task Context for a SSP IO request.
304 * @sci_req:
305 *
306 */
307static void scu_ssp_io_request_construct_task_context(
308 struct scic_sds_request *sci_req,
309 enum dma_data_direction dir,
310 u32 len)
311{
312 struct scu_task_context *task_context;
6f231dda 313
f1f52e75 314 task_context = scic_sds_request_get_task_context(sci_req);
6f231dda 315
f1f52e75 316 scu_ssp_reqeust_construct_task_context(sci_req, task_context);
6f231dda 317
f1f52e75
DW
318 task_context->ssp_command_iu_length =
319 sizeof(struct ssp_cmd_iu) / sizeof(u32);
320 task_context->type.ssp.frame_type = SSP_COMMAND;
321
322 switch (dir) {
323 case DMA_FROM_DEVICE:
324 case DMA_NONE:
325 default:
326 task_context->task_type = SCU_TASK_TYPE_IOREAD;
a1a113b0 327 break;
f1f52e75
DW
328 case DMA_TO_DEVICE:
329 task_context->task_type = SCU_TASK_TYPE_IOWRITE;
a1a113b0 330 break;
6f231dda
DW
331 }
332
f1f52e75
DW
333 task_context->transfer_length_bytes = len;
334
335 if (task_context->transfer_length_bytes > 0)
336 scic_sds_request_build_sgl(sci_req);
6f231dda
DW
337}
338
6f231dda 339/**
f1f52e75
DW
340 * This method will fill in the SCU Task Context for a SSP Task request. The
341 * following important settings are utilized: -# priority ==
342 * SCU_TASK_PRIORITY_HIGH. This ensures that the task request is issued
343 * ahead of other task destined for the same Remote Node. -# task_type ==
344 * SCU_TASK_TYPE_IOREAD. This simply indicates that a normal request type
345 * (i.e. non-raw frame) is being utilized to perform task management. -#
346 * control_frame == 1. This ensures that the proper endianess is set so
347 * that the bytes are transmitted in the right order for a task frame.
348 * @sci_req: This parameter specifies the task request object being
349 * constructed.
6f231dda 350 *
6f231dda 351 */
f1f52e75
DW
352static void scu_ssp_task_request_construct_task_context(
353 struct scic_sds_request *sci_req)
6f231dda 354{
f1f52e75 355 struct scu_task_context *task_context;
6f231dda 356
f1f52e75 357 task_context = scic_sds_request_get_task_context(sci_req);
6f231dda 358
f1f52e75 359 scu_ssp_reqeust_construct_task_context(sci_req, task_context);
6f231dda 360
f1f52e75
DW
361 task_context->control_frame = 1;
362 task_context->priority = SCU_TASK_PRIORITY_HIGH;
363 task_context->task_type = SCU_TASK_TYPE_RAW_FRAME;
364 task_context->transfer_length_bytes = 0;
365 task_context->type.ssp.frame_type = SSP_TASK;
366 task_context->ssp_command_iu_length =
367 sizeof(struct ssp_task_iu) / sizeof(u32);
6f231dda
DW
368}
369
5dec6f4e
DW
370/**
371 * This method is will fill in the SCU Task Context for any type of SATA
372 * request. This is called from the various SATA constructors.
373 * @sci_req: The general IO request object which is to be used in
374 * constructing the SCU task context.
375 * @task_context: The buffer pointer for the SCU task context which is being
376 * constructed.
377 *
378 * The general io request construction is complete. The buffer assignment for
379 * the command buffer is complete. none Revisit task context construction to
380 * determine what is common for SSP/SMP/STP task context structures.
381 */
382static void scu_sata_reqeust_construct_task_context(
383 struct scic_sds_request *sci_req,
384 struct scu_task_context *task_context)
385{
386 dma_addr_t dma_addr;
387 struct scic_sds_controller *controller;
388 struct scic_sds_remote_device *target_device;
389 struct scic_sds_port *target_port;
390
391 controller = scic_sds_request_get_controller(sci_req);
392 target_device = scic_sds_request_get_device(sci_req);
393 target_port = scic_sds_request_get_port(sci_req);
394
395 /* Fill in the TC with the its required data */
396 task_context->abort = 0;
397 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
398 task_context->initiator_request = 1;
399 task_context->connection_rate = target_device->connection_rate;
400 task_context->protocol_engine_index =
401 scic_sds_controller_get_protocol_engine_group(controller);
402 task_context->logical_port_index =
403 scic_sds_port_get_index(target_port);
404 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
405 task_context->valid = SCU_TASK_CONTEXT_VALID;
406 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
407
408 task_context->remote_node_index =
409 scic_sds_remote_device_get_index(sci_req->target_device);
410 task_context->command_code = 0;
411
412 task_context->link_layer_control = 0;
413 task_context->do_not_dma_ssp_good_response = 1;
414 task_context->strict_ordering = 0;
415 task_context->control_frame = 0;
416 task_context->timeout_enable = 0;
417 task_context->block_guard_enable = 0;
418
419 task_context->address_modifier = 0;
420 task_context->task_phase = 0x01;
421
422 task_context->ssp_command_iu_length =
423 (sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);
424
425 /* Set the first word of the H2D REG FIS */
426 task_context->type.words[0] = *(u32 *)&sci_req->stp.cmd;
427
428 if (sci_req->was_tag_assigned_by_user) {
429 /*
430 * Build the task context now since we have already read
431 * the data
432 */
433 sci_req->post_context =
434 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
435 (scic_sds_controller_get_protocol_engine_group(
436 controller) <<
437 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
438 (scic_sds_port_get_index(target_port) <<
439 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
440 scic_sds_io_tag_get_index(sci_req->io_tag));
441 } else {
442 /*
443 * Build the task context now since we have already read
444 * the data.
445 * I/O tag index is not assigned because we have to wait
446 * until we get a TCi.
447 */
448 sci_req->post_context =
449 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
450 (scic_sds_controller_get_protocol_engine_group(
451 controller) <<
452 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
453 (scic_sds_port_get_index(target_port) <<
454 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
455 }
456
457 /*
458 * Copy the physical address for the command buffer to the SCU Task
459 * Context. We must offset the command buffer by 4 bytes because the
460 * first 4 bytes are transfered in the body of the TC.
461 */
462 dma_addr = scic_io_request_get_dma_addr(sci_req,
463 ((char *) &sci_req->stp.cmd) +
464 sizeof(u32));
465
466 task_context->command_iu_upper = upper_32_bits(dma_addr);
467 task_context->command_iu_lower = lower_32_bits(dma_addr);
468
469 /* SATA Requests do not have a response buffer */
470 task_context->response_iu_upper = 0;
471 task_context->response_iu_lower = 0;
472}
473
474
6f231dda 475
f1f52e75 476/**
5dec6f4e
DW
477 * scu_stp_raw_request_construct_task_context -
478 * @sci_req: This parameter specifies the STP request object for which to
479 * construct a RAW command frame task context.
480 * @task_context: This parameter specifies the SCU specific task context buffer
481 * to construct.
f1f52e75 482 *
5dec6f4e
DW
483 * This method performs the operations common to all SATA/STP requests
484 * utilizing the raw frame method. none
f1f52e75 485 */
5dec6f4e
DW
486static void scu_stp_raw_request_construct_task_context(struct scic_sds_stp_request *stp_req,
487 struct scu_task_context *task_context)
488{
489 struct scic_sds_request *sci_req = to_sci_req(stp_req);
490
491 scu_sata_reqeust_construct_task_context(sci_req, task_context);
492
493 task_context->control_frame = 0;
494 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
495 task_context->task_type = SCU_TASK_TYPE_SATA_RAW_FRAME;
496 task_context->type.stp.fis_type = FIS_REGH2D;
497 task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
498}
499
500static enum sci_status
501scic_sds_stp_pio_request_construct(struct scic_sds_request *sci_req,
502 bool copy_rx_frame)
503{
504 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
505 struct scic_sds_stp_pio_request *pio = &stp_req->type.pio;
506
507 scu_stp_raw_request_construct_task_context(stp_req,
508 sci_req->task_context_buffer);
509
510 pio->current_transfer_bytes = 0;
511 pio->ending_error = 0;
512 pio->ending_status = 0;
513
514 pio->request_current.sgl_offset = 0;
515 pio->request_current.sgl_set = SCU_SGL_ELEMENT_PAIR_A;
516
517 if (copy_rx_frame) {
518 scic_sds_request_build_sgl(sci_req);
519 /* Since the IO request copy of the TC contains the same data as
520 * the actual TC this pointer is vaild for either.
521 */
522 pio->request_current.sgl_pair = &sci_req->task_context_buffer->sgl_pair_ab;
523 } else {
524 /* The user does not want the data copied to the SGL buffer location */
525 pio->request_current.sgl_pair = NULL;
526 }
6f231dda 527
5dec6f4e
DW
528 return SCI_SUCCESS;
529}
6f231dda
DW
530
531/**
6f231dda 532 *
5dec6f4e
DW
533 * @sci_req: This parameter specifies the request to be constructed as an
534 * optimized request.
535 * @optimized_task_type: This parameter specifies whether the request is to be
536 * an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
537 * value of 1 indicates NCQ.
538 *
539 * This method will perform request construction common to all types of STP
540 * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
541 * returns an indication as to whether the construction was successful.
6f231dda 542 */
5dec6f4e
DW
543static void scic_sds_stp_optimized_request_construct(struct scic_sds_request *sci_req,
544 u8 optimized_task_type,
545 u32 len,
546 enum dma_data_direction dir)
547{
548 struct scu_task_context *task_context = sci_req->task_context_buffer;
549
550 /* Build the STP task context structure */
551 scu_sata_reqeust_construct_task_context(sci_req, task_context);
552
553 /* Copy over the SGL elements */
554 scic_sds_request_build_sgl(sci_req);
555
556 /* Copy over the number of bytes to be transfered */
557 task_context->transfer_length_bytes = len;
558
559 if (dir == DMA_TO_DEVICE) {
560 /*
561 * The difference between the DMA IN and DMA OUT request task type
562 * values are consistent with the difference between FPDMA READ
563 * and FPDMA WRITE values. Add the supplied task type parameter
564 * to this difference to set the task type properly for this
565 * DATA OUT (WRITE) case. */
566 task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
567 - SCU_TASK_TYPE_DMA_IN);
568 } else {
569 /*
570 * For the DATA IN (READ) case, simply save the supplied
571 * optimized task type. */
572 task_context->task_type = optimized_task_type;
573 }
574}
575
576
577
f1f52e75
DW
578static enum sci_status
579scic_io_request_construct_sata(struct scic_sds_request *sci_req,
580 u32 len,
581 enum dma_data_direction dir,
582 bool copy)
6f231dda 583{
f1f52e75
DW
584 enum sci_status status = SCI_SUCCESS;
585 struct isci_request *ireq = sci_req_to_ireq(sci_req);
586 struct sas_task *task = isci_request_access_task(ireq);
6f231dda 587
f1f52e75
DW
588 /* check for management protocols */
589 if (ireq->ttype == tmf_task) {
590 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
6f231dda 591
f1f52e75 592 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
5dec6f4e
DW
593 tmf->tmf_code == isci_tmf_sata_srst_low) {
594 scu_stp_raw_request_construct_task_context(&sci_req->stp.req,
595 sci_req->task_context_buffer);
596 return SCI_SUCCESS;
597 } else {
f1f52e75
DW
598 dev_err(scic_to_dev(sci_req->owning_controller),
599 "%s: Request 0x%p received un-handled SAT "
600 "management protocol 0x%x.\n",
601 __func__, sci_req, tmf->tmf_code);
602
603 return SCI_FAILURE;
604 }
6f231dda 605 }
6f231dda 606
f1f52e75
DW
607 if (!sas_protocol_ata(task->task_proto)) {
608 dev_err(scic_to_dev(sci_req->owning_controller),
609 "%s: Non-ATA protocol in SATA path: 0x%x\n",
610 __func__,
611 task->task_proto);
612 return SCI_FAILURE;
613
614 }
615
616 /* non data */
5dec6f4e
DW
617 if (task->data_dir == DMA_NONE) {
618 scu_stp_raw_request_construct_task_context(&sci_req->stp.req,
619 sci_req->task_context_buffer);
620 return SCI_SUCCESS;
621 }
f1f52e75
DW
622
623 /* NCQ */
5dec6f4e
DW
624 if (task->ata_task.use_ncq) {
625 scic_sds_stp_optimized_request_construct(sci_req,
626 SCU_TASK_TYPE_FPDMAQ_READ,
627 len, dir);
628 return SCI_SUCCESS;
629 }
f1f52e75
DW
630
631 /* DMA */
5dec6f4e
DW
632 if (task->ata_task.dma_xfer) {
633 scic_sds_stp_optimized_request_construct(sci_req,
634 SCU_TASK_TYPE_DMA_IN,
635 len, dir);
636 return SCI_SUCCESS;
637 } else /* PIO */
f1f52e75
DW
638 return scic_sds_stp_pio_request_construct(sci_req, copy);
639
640 return status;
641}
642
643static enum sci_status scic_io_request_construct_basic_ssp(struct scic_sds_request *sci_req)
6f231dda 644{
f1f52e75
DW
645 struct isci_request *ireq = sci_req_to_ireq(sci_req);
646 struct sas_task *task = isci_request_access_task(ireq);
6f231dda 647
f1f52e75 648 sci_req->protocol = SCIC_SSP_PROTOCOL;
6f231dda 649
f1f52e75
DW
650 scu_ssp_io_request_construct_task_context(sci_req,
651 task->data_dir,
652 task->total_xfer_len);
6f231dda 653
f1f52e75 654 scic_sds_io_request_build_ssp_command_iu(sci_req);
6f231dda 655
5dec6f4e
DW
656 sci_base_state_machine_change_state(&sci_req->state_machine,
657 SCI_BASE_REQUEST_STATE_CONSTRUCTED);
ce4f75de 658
f1f52e75
DW
659 return SCI_SUCCESS;
660}
6f231dda 661
f1f52e75
DW
662enum sci_status scic_task_request_construct_ssp(
663 struct scic_sds_request *sci_req)
664{
665 /* Construct the SSP Task SCU Task Context */
666 scu_ssp_task_request_construct_task_context(sci_req);
6f231dda 667
f1f52e75
DW
668 /* Fill in the SSP Task IU */
669 scic_sds_task_request_build_ssp_task_iu(sci_req);
c4b9e24c 670
f1f52e75 671 sci_base_state_machine_change_state(&sci_req->state_machine,
5dec6f4e 672 SCI_BASE_REQUEST_STATE_CONSTRUCTED);
67ea838d 673
f1f52e75
DW
674 return SCI_SUCCESS;
675}
67ea838d 676
f1f52e75
DW
677static enum sci_status scic_io_request_construct_basic_sata(struct scic_sds_request *sci_req)
678{
679 enum sci_status status;
680 struct scic_sds_stp_request *stp_req;
681 bool copy = false;
682 struct isci_request *isci_request = sci_req_to_ireq(sci_req);
683 struct sas_task *task = isci_request_access_task(isci_request);
6f231dda 684
f1f52e75
DW
685 stp_req = &sci_req->stp.req;
686 sci_req->protocol = SCIC_STP_PROTOCOL;
6f231dda 687
f1f52e75
DW
688 copy = (task->data_dir == DMA_NONE) ? false : true;
689
690 status = scic_io_request_construct_sata(sci_req,
691 task->total_xfer_len,
692 task->data_dir,
693 copy);
694
695 if (status == SCI_SUCCESS)
696 sci_base_state_machine_change_state(&sci_req->state_machine,
5dec6f4e 697 SCI_BASE_REQUEST_STATE_CONSTRUCTED);
f1f52e75
DW
698
699 return status;
6f231dda
DW
700}
701
f1f52e75
DW
702enum sci_status scic_task_request_construct_sata(struct scic_sds_request *sci_req)
703{
704 enum sci_status status = SCI_SUCCESS;
705 struct isci_request *ireq = sci_req_to_ireq(sci_req);
706
707 /* check for management protocols */
708 if (ireq->ttype == tmf_task) {
709 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
710
711 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
712 tmf->tmf_code == isci_tmf_sata_srst_low) {
5dec6f4e
DW
713 scu_stp_raw_request_construct_task_context(&sci_req->stp.req,
714 sci_req->task_context_buffer);
f1f52e75
DW
715 } else {
716 dev_err(scic_to_dev(sci_req->owning_controller),
717 "%s: Request 0x%p received un-handled SAT "
718 "Protocol 0x%x.\n",
719 __func__, sci_req, tmf->tmf_code);
720
721 return SCI_FAILURE;
722 }
723 }
724
5dec6f4e
DW
725 if (status != SCI_SUCCESS)
726 return status;
727 sci_base_state_machine_change_state(&sci_req->state_machine,
728 SCI_BASE_REQUEST_STATE_CONSTRUCTED);
f1f52e75
DW
729
730 return status;
731}
732
6f231dda 733/**
f1f52e75
DW
734 * sci_req_tx_bytes - bytes transferred when reply underruns request
735 * @sci_req: request that was terminated early
6f231dda 736 */
f1f52e75
DW
737#define SCU_TASK_CONTEXT_SRAM 0x200000
738static u32 sci_req_tx_bytes(struct scic_sds_request *sci_req)
6f231dda 739{
f1f52e75
DW
740 struct scic_sds_controller *scic = sci_req->owning_controller;
741 u32 ret_val = 0;
742
743 if (readl(&scic->smu_registers->address_modifier) == 0) {
744 void __iomem *scu_reg_base = scic->scu_registers;
745
746 /* get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
747 * BAR1 is the scu_registers
748 * 0x20002C = 0x200000 + 0x2c
749 * = start of task context SRAM + offset of (type.ssp.data_offset)
750 * TCi is the io_tag of struct scic_sds_request
751 */
752 ret_val = readl(scu_reg_base +
753 (SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
754 ((sizeof(struct scu_task_context)) * scic_sds_io_tag_get_index(sci_req->io_tag)));
755 }
756
757 return ret_val;
758}
759
f4636a7b 760enum sci_status scic_sds_request_start(struct scic_sds_request *sci_req)
f1f52e75 761{
f4636a7b
PS
762 struct scic_sds_controller *scic = sci_req->owning_controller;
763 struct scu_task_context *task_context;
764 enum sci_base_request_states state;
765
766 if (sci_req->device_sequence !=
767 scic_sds_remote_device_get_sequence(sci_req->target_device))
f1f52e75
DW
768 return SCI_FAILURE;
769
f4636a7b
PS
770 state = sci_req->state_machine.current_state_id;
771 if (state != SCI_BASE_REQUEST_STATE_CONSTRUCTED) {
772 dev_warn(scic_to_dev(scic),
773 "%s: SCIC IO Request requested to start while in wrong "
774 "state %d\n", __func__, state);
775 return SCI_FAILURE_INVALID_STATE;
776 }
f1f52e75 777
f4636a7b
PS
778 /* if necessary, allocate a TCi for the io request object and then will,
779 * if necessary, copy the constructed TC data into the actual TC buffer.
780 * If everything is successful the post context field is updated with
781 * the TCi so the controller can post the request to the hardware.
782 */
783 if (sci_req->io_tag == SCI_CONTROLLER_INVALID_IO_TAG)
784 sci_req->io_tag = scic_controller_allocate_io_tag(scic);
f1f52e75 785
f4636a7b
PS
786 /* Record the IO Tag in the request */
787 if (sci_req->io_tag != SCI_CONTROLLER_INVALID_IO_TAG) {
788 task_context = sci_req->task_context_buffer;
789
790 task_context->task_index = scic_sds_io_tag_get_index(sci_req->io_tag);
791
792 switch (task_context->protocol_type) {
793 case SCU_TASK_CONTEXT_PROTOCOL_SMP:
794 case SCU_TASK_CONTEXT_PROTOCOL_SSP:
795 /* SSP/SMP Frame */
796 task_context->type.ssp.tag = sci_req->io_tag;
797 task_context->type.ssp.target_port_transfer_tag =
798 0xFFFF;
799 break;
800
801 case SCU_TASK_CONTEXT_PROTOCOL_STP:
802 /* STP/SATA Frame
803 * task_context->type.stp.ncq_tag = sci_req->ncq_tag;
804 */
805 break;
806
807 case SCU_TASK_CONTEXT_PROTOCOL_NONE:
808 /* / @todo When do we set no protocol type? */
809 break;
810
811 default:
812 /* This should never happen since we build the IO
813 * requests */
814 break;
815 }
816
817 /*
818 * Check to see if we need to copy the task context buffer
819 * or have been building into the task context buffer */
820 if (sci_req->was_tag_assigned_by_user == false)
821 scic_sds_controller_copy_task_context(scic, sci_req);
822
823 /* Add to the post_context the io tag value */
824 sci_req->post_context |= scic_sds_io_tag_get_index(sci_req->io_tag);
825
826 /* Everything is good go ahead and change state */
827 sci_base_state_machine_change_state(&sci_req->state_machine,
828 SCI_BASE_REQUEST_STATE_STARTED);
829
830 return SCI_SUCCESS;
831 }
832
833 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
f1f52e75
DW
834}
835
836enum sci_status
f00e6ba4 837scic_sds_io_request_terminate(struct scic_sds_request *sci_req)
f1f52e75 838{
f00e6ba4 839 enum sci_base_request_states state;
f1f52e75 840
f00e6ba4
DW
841 state = sci_req->state_machine.current_state_id;
842
843 switch (state) {
844 case SCI_BASE_REQUEST_STATE_CONSTRUCTED:
845 scic_sds_request_set_status(sci_req,
846 SCU_TASK_DONE_TASK_ABORT,
847 SCI_FAILURE_IO_TERMINATED);
848
849 sci_base_state_machine_change_state(&sci_req->state_machine,
850 SCI_BASE_REQUEST_STATE_COMPLETED);
851 return SCI_SUCCESS;
852 case SCI_BASE_REQUEST_STATE_STARTED:
853 case SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION:
854 case SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE:
855 case SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION:
856 case SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE:
857 case SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE:
858 case SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE:
859 case SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE:
860 case SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE:
861 case SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE:
862 case SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE:
863 case SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE:
864 case SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE:
865 case SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE:
866 case SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE:
867 sci_base_state_machine_change_state(&sci_req->state_machine,
868 SCI_BASE_REQUEST_STATE_ABORTING);
869 return SCI_SUCCESS;
870 case SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE:
871 sci_base_state_machine_change_state(&sci_req->state_machine,
872 SCI_BASE_REQUEST_STATE_ABORTING);
873 sci_base_state_machine_change_state(&sci_req->state_machine,
874 SCI_BASE_REQUEST_STATE_COMPLETED);
875 return SCI_SUCCESS;
876 case SCI_BASE_REQUEST_STATE_ABORTING:
877 sci_base_state_machine_change_state(&sci_req->state_machine,
878 SCI_BASE_REQUEST_STATE_COMPLETED);
879 return SCI_SUCCESS;
880 case SCI_BASE_REQUEST_STATE_COMPLETED:
881 default:
882 dev_warn(scic_to_dev(sci_req->owning_controller),
883 "%s: SCIC IO Request requested to abort while in wrong "
884 "state %d\n",
885 __func__,
886 sci_base_state_machine_get_state(&sci_req->state_machine));
887 break;
888 }
6f231dda 889
f1f52e75
DW
890 return SCI_FAILURE_INVALID_STATE;
891}
6f231dda 892
f1f52e75
DW
893enum sci_status scic_sds_io_request_event_handler(
894 struct scic_sds_request *request,
895 u32 event_code)
896{
897 if (request->state_handlers->event_handler)
898 return request->state_handlers->event_handler(request, event_code);
899
900 dev_warn(scic_to_dev(request->owning_controller),
901 "%s: SCIC IO Request given event code notification %x while "
902 "in wrong state %d\n",
903 __func__,
904 event_code,
905 sci_base_state_machine_get_state(&request->state_machine));
906
907 return SCI_FAILURE_INVALID_STATE;
6f231dda
DW
908}
909
910/**
6f231dda 911 *
f1f52e75
DW
912 * @sci_req: The SCIC_SDS_IO_REQUEST_T object for which the start
913 * operation is to be executed.
914 * @frame_index: The frame index returned by the hardware for the reqeust
915 * object.
916 *
917 * This method invokes the core state frame handler for the
918 * SCIC_SDS_IO_REQUEST_T object. enum sci_status
6f231dda 919 */
f1f52e75
DW
920enum sci_status scic_sds_io_request_frame_handler(
921 struct scic_sds_request *request,
922 u32 frame_index)
6f231dda 923{
f1f52e75
DW
924 if (request->state_handlers->frame_handler)
925 return request->state_handlers->frame_handler(request, frame_index);
926
927 dev_warn(scic_to_dev(request->owning_controller),
928 "%s: SCIC IO Request given unexpected frame %x while in "
929 "state %d\n",
930 __func__,
931 frame_index,
932 sci_base_state_machine_get_state(&request->state_machine));
933
934 scic_sds_controller_release_frame(request->owning_controller, frame_index);
935 return SCI_FAILURE_INVALID_STATE;
6f231dda
DW
936}
937
f1f52e75
DW
938/*
939 * This function copies response data for requests returning response data
940 * instead of sense data.
941 * @sci_req: This parameter specifies the request object for which to copy
942 * the response data.
6f231dda 943 */
f139303d 944static void scic_sds_io_request_copy_response(struct scic_sds_request *sci_req)
6f231dda 945{
f1f52e75
DW
946 void *resp_buf;
947 u32 len;
948 struct ssp_response_iu *ssp_response;
949 struct isci_request *ireq = sci_req_to_ireq(sci_req);
950 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
6f231dda 951
f1f52e75 952 ssp_response = &sci_req->ssp.rsp;
6f231dda 953
f1f52e75 954 resp_buf = &isci_tmf->resp.resp_iu;
6f231dda 955
f1f52e75
DW
956 len = min_t(u32,
957 SSP_RESP_IU_MAX_SIZE,
958 be32_to_cpu(ssp_response->response_data_len));
6f231dda 959
f1f52e75
DW
960 memcpy(resp_buf, ssp_response->resp_data, len);
961}
6f231dda 962
f1f52e75
DW
963/*
964 * scic_sds_request_started_state_tc_completion_handler() - This method process
965 * TC (task context) completions for normal IO request (i.e. Task/Abort
966 * Completions of type 0). This method will update the
967 * SCIC_SDS_IO_REQUEST_T::status field.
968 * @sci_req: This parameter specifies the request for which a completion
969 * occurred.
970 * @completion_code: This parameter specifies the completion code received from
971 * the SCU.
972 *
973 */
974static enum sci_status
975scic_sds_request_started_state_tc_completion_handler(struct scic_sds_request *sci_req,
976 u32 completion_code)
977{
978 u8 datapres;
979 struct ssp_response_iu *resp_iu;
6f231dda 980
f1f52e75
DW
981 /*
982 * TODO: Any SDMA return code of other than 0 is bad
983 * decode 0x003C0000 to determine SDMA status
984 */
985 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
986 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
987 scic_sds_request_set_status(sci_req,
988 SCU_TASK_DONE_GOOD,
989 SCI_SUCCESS);
6f231dda
DW
990 break;
991
f1f52e75
DW
992 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP):
993 {
994 /*
995 * There are times when the SCU hardware will return an early
996 * response because the io request specified more data than is
997 * returned by the target device (mode pages, inquiry data,
998 * etc.). We must check the response stats to see if this is
999 * truly a failed request or a good request that just got
1000 * completed early.
1001 */
1002 struct ssp_response_iu *resp = &sci_req->ssp.rsp;
1003 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1004
1005 sci_swab32_cpy(&sci_req->ssp.rsp,
1006 &sci_req->ssp.rsp,
1007 word_cnt);
1008
1009 if (resp->status == 0) {
1010 scic_sds_request_set_status(
1011 sci_req,
1012 SCU_TASK_DONE_GOOD,
1013 SCI_SUCCESS_IO_DONE_EARLY);
1014 } else {
1015 scic_sds_request_set_status(
1016 sci_req,
1017 SCU_TASK_DONE_CHECK_RESPONSE,
1018 SCI_FAILURE_IO_RESPONSE_VALID);
1019 }
1020 }
1021 break;
6f231dda 1022
f1f52e75
DW
1023 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE):
1024 {
1025 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
6f231dda 1026
f1f52e75
DW
1027 sci_swab32_cpy(&sci_req->ssp.rsp,
1028 &sci_req->ssp.rsp,
1029 word_cnt);
6f231dda 1030
f1f52e75
DW
1031 scic_sds_request_set_status(sci_req,
1032 SCU_TASK_DONE_CHECK_RESPONSE,
1033 SCI_FAILURE_IO_RESPONSE_VALID);
6f231dda 1034 break;
f1f52e75 1035 }
6f231dda 1036
f1f52e75
DW
1037 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
1038 /*
1039 * / @todo With TASK_DONE_RESP_LEN_ERR is the response frame
1040 * guaranteed to be received before this completion status is
1041 * posted?
1042 */
1043 resp_iu = &sci_req->ssp.rsp;
1044 datapres = resp_iu->datapres;
1045
1046 if ((datapres == 0x01) || (datapres == 0x02)) {
1047 scic_sds_request_set_status(
1048 sci_req,
1049 SCU_TASK_DONE_CHECK_RESPONSE,
1050 SCI_FAILURE_IO_RESPONSE_VALID);
1051 } else
1052 scic_sds_request_set_status(
1053 sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS);
6f231dda
DW
1054 break;
1055
f1f52e75
DW
1056 /* only stp device gets suspended. */
1057 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
1058 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
1059 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
1060 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
1061 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
1062 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
1063 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
1064 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
1065 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
1066 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
1067 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
1068 if (sci_req->protocol == SCIC_STP_PROTOCOL) {
1069 scic_sds_request_set_status(
1070 sci_req,
1071 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1072 SCU_COMPLETION_TL_STATUS_SHIFT,
1073 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
1074 } else {
1075 scic_sds_request_set_status(
1076 sci_req,
1077 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1078 SCU_COMPLETION_TL_STATUS_SHIFT,
1079 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1080 }
6f231dda
DW
1081 break;
1082
f1f52e75
DW
1083 /* both stp/ssp device gets suspended */
1084 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
1085 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
1086 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
1087 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
1088 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
1089 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
1090 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
1091 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
1092 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
1093 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
1094 scic_sds_request_set_status(
1095 sci_req,
1096 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1097 SCU_COMPLETION_TL_STATUS_SHIFT,
1098 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
6f231dda
DW
1099 break;
1100
f1f52e75
DW
1101 /* neither ssp nor stp gets suspended. */
1102 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
1103 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
1104 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
1105 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
1106 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
1107 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
1108 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1109 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1110 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1111 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
1112 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
1113 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
1114 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
1115 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
1116 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
6f231dda 1117 default:
f1f52e75
DW
1118 scic_sds_request_set_status(
1119 sci_req,
1120 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1121 SCU_COMPLETION_TL_STATUS_SHIFT,
1122 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
6f231dda
DW
1123 break;
1124 }
f1f52e75
DW
1125
1126 /*
1127 * TODO: This is probably wrong for ACK/NAK timeout conditions
1128 */
1129
1130 /* In all cases we will treat this as the completion of the IO req. */
5dec6f4e
DW
1131 sci_base_state_machine_change_state(&sci_req->state_machine,
1132 SCI_BASE_REQUEST_STATE_COMPLETED);
f1f52e75 1133 return SCI_SUCCESS;
6f231dda
DW
1134}
1135
f1f52e75
DW
1136enum sci_status
1137scic_sds_io_request_tc_completion(struct scic_sds_request *request, u32 completion_code)
6f231dda 1138{
5dec6f4e 1139 if (request->state_handlers->tc_completion_handler)
f1f52e75
DW
1140 return request->state_handlers->tc_completion_handler(request, completion_code);
1141
1142 dev_warn(scic_to_dev(request->owning_controller),
1143 "%s: SCIC IO Request given task completion notification %x "
1144 "while in wrong state %d\n",
1145 __func__,
1146 completion_code,
1147 sci_base_state_machine_get_state(&request->state_machine));
6f231dda 1148
f1f52e75 1149 return SCI_FAILURE_INVALID_STATE;
f1f52e75 1150}
6f231dda 1151
f1f52e75
DW
1152/*
1153 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1154 * object receives a scic_sds_request_frame_handler() request. This method
1155 * first determines the frame type received. If this is a response frame then
1156 * the response data is copied to the io request response buffer for processing
1157 * at completion time. If the frame type is not a response buffer an error is
1158 * logged. enum sci_status SCI_SUCCESS SCI_FAILURE_INVALID_PARAMETER_VALUE
1159 */
1160static enum sci_status
1161scic_sds_request_started_state_frame_handler(struct scic_sds_request *sci_req,
1162 u32 frame_index)
1163{
1164 enum sci_status status;
1165 u32 *frame_header;
1166 struct ssp_frame_hdr ssp_hdr;
1167 ssize_t word_cnt;
1168
1169 status = scic_sds_unsolicited_frame_control_get_header(
1170 &(scic_sds_request_get_controller(sci_req)->uf_control),
1171 frame_index,
1172 (void **)&frame_header);
1173
1174 word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
1175 sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);
1176
1177 if (ssp_hdr.frame_type == SSP_RESPONSE) {
1178 struct ssp_response_iu *resp_iu;
1179 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1180
1181 status = scic_sds_unsolicited_frame_control_get_buffer(
1182 &(scic_sds_request_get_controller(sci_req)->uf_control),
1183 frame_index,
1184 (void **)&resp_iu);
1185
1186 sci_swab32_cpy(&sci_req->ssp.rsp,
1187 resp_iu, word_cnt);
1188
1189 resp_iu = &sci_req->ssp.rsp;
1190
1191 if ((resp_iu->datapres == 0x01) ||
1192 (resp_iu->datapres == 0x02)) {
1193 scic_sds_request_set_status(
1194 sci_req,
1195 SCU_TASK_DONE_CHECK_RESPONSE,
1196 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1197 } else
1198 scic_sds_request_set_status(
1199 sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS);
1200 } else {
1201 /* This was not a response frame why did it get forwarded? */
1202 dev_err(scic_to_dev(sci_req->owning_controller),
1203 "%s: SCIC IO Request 0x%p received unexpected "
1204 "frame %d type 0x%02x\n",
6f231dda 1205 __func__,
f1f52e75
DW
1206 sci_req,
1207 frame_index,
1208 ssp_hdr.frame_type);
1209 }
ec6c9638 1210
f1f52e75
DW
1211 /*
1212 * In any case we are done with this frame buffer return it to the
1213 * controller
1214 */
1215 scic_sds_controller_release_frame(
1216 sci_req->owning_controller, frame_index);
6f231dda 1217
f1f52e75
DW
1218 return SCI_SUCCESS;
1219}
a5fde225 1220
f1f52e75
DW
1221/*
1222 * *****************************************************************************
1223 * * COMPLETED STATE HANDLERS
1224 * ***************************************************************************** */
a5fde225 1225
a5fde225 1226
f1f52e75
DW
1227/*
1228 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1229 * object receives a scic_sds_request_complete() request. This method frees up
1230 * any io request resources that have been allocated and transitions the
1231 * request to its final state. Consider stopping the state machine instead of
1232 * transitioning to the final state? enum sci_status SCI_SUCCESS
1233 */
1234static enum sci_status scic_sds_request_completed_state_complete_handler(
1235 struct scic_sds_request *request)
1236{
1237 if (request->was_tag_assigned_by_user != true) {
1238 scic_controller_free_io_tag(
1239 request->owning_controller, request->io_tag);
1240 }
6f231dda 1241
f1f52e75
DW
1242 if (request->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX) {
1243 scic_sds_controller_release_frame(
1244 request->owning_controller, request->saved_rx_frame_index);
1245 }
6f231dda 1246
f1f52e75 1247 sci_base_state_machine_change_state(&request->state_machine,
5dec6f4e 1248 SCI_BASE_REQUEST_STATE_FINAL);
f1f52e75
DW
1249 return SCI_SUCCESS;
1250}
6f231dda 1251
f1f52e75
DW
1252/*
1253 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1254 * object receives a scic_sds_request_task_completion() request. This method
1255 * decodes the completion type waiting for the abort task complete
1256 * notification. When the abort task complete is received the io request
1257 * transitions to the completed state. enum sci_status SCI_SUCCESS
1258 */
1259static enum sci_status scic_sds_request_aborting_state_tc_completion_handler(
1260 struct scic_sds_request *sci_req,
1261 u32 completion_code)
1262{
1263 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1264 case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
1265 case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
1266 scic_sds_request_set_status(
1267 sci_req, SCU_TASK_DONE_TASK_ABORT, SCI_FAILURE_IO_TERMINATED
1268 );
1269
1270 sci_base_state_machine_change_state(&sci_req->state_machine,
5dec6f4e 1271 SCI_BASE_REQUEST_STATE_COMPLETED);
f1f52e75
DW
1272 break;
1273
1274 default:
1275 /*
1276 * Unless we get some strange error wait for the task abort to complete
1277 * TODO: Should there be a state change for this completion? */
6f231dda
DW
1278 break;
1279 }
f1f52e75
DW
1280
1281 return SCI_SUCCESS;
1282}
1283
1284/*
1285 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1286 * object receives a scic_sds_request_frame_handler() request. This method
1287 * discards the unsolicited frame since we are waiting for the abort task
1288 * completion. enum sci_status SCI_SUCCESS
1289 */
1290static enum sci_status scic_sds_request_aborting_state_frame_handler(
1291 struct scic_sds_request *sci_req,
1292 u32 frame_index)
1293{
1294 /* TODO: Is it even possible to get an unsolicited frame in the aborting state? */
1295
1296 scic_sds_controller_release_frame(
1297 sci_req->owning_controller, frame_index);
1298
1299 return SCI_SUCCESS;
6f231dda
DW
1300}
1301
f139303d
DW
1302/**
1303 * This method processes the completions transport layer (TL) status to
1304 * determine if the RAW task management frame was sent successfully. If the
1305 * raw frame was sent successfully, then the state for the task request
1306 * transitions to waiting for a response frame.
1307 * @sci_req: This parameter specifies the request for which the TC
1308 * completion was received.
1309 * @completion_code: This parameter indicates the completion status information
1310 * for the TC.
1311 *
1312 * Indicate if the tc completion handler was successful. SCI_SUCCESS currently
1313 * this method always returns success.
1314 */
1315static enum sci_status scic_sds_ssp_task_request_await_tc_completion_tc_completion_handler(
1316 struct scic_sds_request *sci_req,
1317 u32 completion_code)
1318{
1319 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1320 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1321 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
1322 SCI_SUCCESS);
1323
1324 sci_base_state_machine_change_state(&sci_req->state_machine,
5dec6f4e 1325 SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE);
f139303d
DW
1326 break;
1327
1328 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
1329 /*
1330 * Currently, the decision is to simply allow the task request to
1331 * timeout if the task IU wasn't received successfully.
1332 * There is a potential for receiving multiple task responses if we
1333 * decide to send the task IU again. */
1334 dev_warn(scic_to_dev(sci_req->owning_controller),
1335 "%s: TaskRequest:0x%p CompletionCode:%x - "
1336 "ACK/NAK timeout\n",
1337 __func__,
1338 sci_req,
1339 completion_code);
1340
1341 sci_base_state_machine_change_state(&sci_req->state_machine,
5dec6f4e 1342 SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE);
f139303d
DW
1343 break;
1344
1345 default:
1346 /*
1347 * All other completion status cause the IO to be complete. If a NAK
1348 * was received, then it is up to the user to retry the request. */
1349 scic_sds_request_set_status(
1350 sci_req,
1351 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1352 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
1353 );
1354
1355 sci_base_state_machine_change_state(&sci_req->state_machine,
5dec6f4e 1356 SCI_BASE_REQUEST_STATE_COMPLETED);
f139303d
DW
1357 break;
1358 }
1359
1360 return SCI_SUCCESS;
1361}
1362
f139303d
DW
1363/**
1364 * This method processes an unsolicited frame while the task mgmt request is
1365 * waiting for a response frame. It will copy the response data, release
1366 * the unsolicited frame, and transition the request to the
1367 * SCI_BASE_REQUEST_STATE_COMPLETED state.
1368 * @sci_req: This parameter specifies the request for which the
1369 * unsolicited frame was received.
1370 * @frame_index: This parameter indicates the unsolicited frame index that
1371 * should contain the response.
1372 *
1373 * This method returns an indication of whether the TC response frame was
1374 * handled successfully or not. SCI_SUCCESS Currently this value is always
1375 * returned and indicates successful processing of the TC response. Should
1376 * probably update to check frame type and make sure it is a response frame.
1377 */
1378static enum sci_status scic_sds_ssp_task_request_await_tc_response_frame_handler(
1379 struct scic_sds_request *request,
1380 u32 frame_index)
1381{
1382 scic_sds_io_request_copy_response(request);
1383
1384 sci_base_state_machine_change_state(&request->state_machine,
5dec6f4e 1385 SCI_BASE_REQUEST_STATE_COMPLETED);
f139303d
DW
1386 scic_sds_controller_release_frame(request->owning_controller,
1387 frame_index);
1388 return SCI_SUCCESS;
1389}
1390
c72086e3
DW
1391/**
1392 * This method processes an abnormal TC completion while the SMP request is
1393 * waiting for a response frame. It decides what happened to the IO based
1394 * on TC completion status.
1395 * @sci_req: This parameter specifies the request for which the TC
1396 * completion was received.
1397 * @completion_code: This parameter indicates the completion status information
1398 * for the TC.
1399 *
1400 * Indicate if the tc completion handler was successful. SCI_SUCCESS currently
1401 * this method always returns success.
1402 */
1403static enum sci_status scic_sds_smp_request_await_response_tc_completion_handler(
1404 struct scic_sds_request *sci_req,
1405 u32 completion_code)
1406{
1407 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1408 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1409 /*
1410 * In the AWAIT RESPONSE state, any TC completion is unexpected.
1411 * but if the TC has success status, we complete the IO anyway. */
5dec6f4e
DW
1412 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
1413 SCI_SUCCESS);
c72086e3 1414
5dec6f4e
DW
1415 sci_base_state_machine_change_state(&sci_req->state_machine,
1416 SCI_BASE_REQUEST_STATE_COMPLETED);
c72086e3
DW
1417 break;
1418
1419 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1420 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1421 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1422 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
1423 /*
1424 * These status has been seen in a specific LSI expander, which sometimes
1425 * is not able to send smp response within 2 ms. This causes our hardware
1426 * break the connection and set TC completion with one of these SMP_XXX_XX_ERR
1427 * status. For these type of error, we ask scic user to retry the request. */
5dec6f4e
DW
1428 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_SMP_RESP_TO_ERR,
1429 SCI_FAILURE_RETRY_REQUIRED);
c72086e3 1430
5dec6f4e
DW
1431 sci_base_state_machine_change_state(&sci_req->state_machine,
1432 SCI_BASE_REQUEST_STATE_COMPLETED);
c72086e3
DW
1433 break;
1434
1435 default:
1436 /*
1437 * All other completion status cause the IO to be complete. If a NAK
1438 * was received, then it is up to the user to retry the request. */
1439 scic_sds_request_set_status(
1440 sci_req,
1441 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1442 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
1443 );
1444
5dec6f4e
DW
1445 sci_base_state_machine_change_state(&sci_req->state_machine,
1446 SCI_BASE_REQUEST_STATE_COMPLETED);
c72086e3
DW
1447 break;
1448 }
1449
1450 return SCI_SUCCESS;
1451}
1452
1453/*
1454 * This function processes an unsolicited frame while the SMP request is waiting
1455 * for a response frame. It will copy the response data, release the
1456 * unsolicited frame, and transition the request to the
1457 * SCI_BASE_REQUEST_STATE_COMPLETED state.
1458 * @sci_req: This parameter specifies the request for which the
1459 * unsolicited frame was received.
1460 * @frame_index: This parameter indicates the unsolicited frame index that
1461 * should contain the response.
1462 *
1463 * This function returns an indication of whether the response frame was handled
1464 * successfully or not. SCI_SUCCESS Currently this value is always returned and
1465 * indicates successful processing of the TC response.
1466 */
1467static enum sci_status
1468scic_sds_smp_request_await_response_frame_handler(struct scic_sds_request *sci_req,
1469 u32 frame_index)
1470{
1471 enum sci_status status;
1472 void *frame_header;
1473 struct smp_resp *rsp_hdr = &sci_req->smp.rsp;
1474 ssize_t word_cnt = SMP_RESP_HDR_SZ / sizeof(u32);
1475
1476 status = scic_sds_unsolicited_frame_control_get_header(
1477 &(scic_sds_request_get_controller(sci_req)->uf_control),
1478 frame_index,
1479 &frame_header);
1480
1481 /* byte swap the header. */
1482 sci_swab32_cpy(rsp_hdr, frame_header, word_cnt);
1483
1484 if (rsp_hdr->frame_type == SMP_RESPONSE) {
1485 void *smp_resp;
1486
1487 status = scic_sds_unsolicited_frame_control_get_buffer(
1488 &(scic_sds_request_get_controller(sci_req)->uf_control),
1489 frame_index,
1490 &smp_resp);
1491
1492 word_cnt = (sizeof(struct smp_req) - SMP_RESP_HDR_SZ) /
1493 sizeof(u32);
1494
1495 sci_swab32_cpy(((u8 *) rsp_hdr) + SMP_RESP_HDR_SZ,
1496 smp_resp, word_cnt);
1497
1498 scic_sds_request_set_status(
1499 sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS);
1500
1501 sci_base_state_machine_change_state(&sci_req->state_machine,
5dec6f4e 1502 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION);
c72086e3
DW
1503 } else {
1504 /* This was not a response frame why did it get forwarded? */
1505 dev_err(scic_to_dev(sci_req->owning_controller),
1506 "%s: SCIC SMP Request 0x%p received unexpected frame "
1507 "%d type 0x%02x\n",
1508 __func__,
1509 sci_req,
1510 frame_index,
1511 rsp_hdr->frame_type);
1512
1513 scic_sds_request_set_status(
1514 sci_req,
5dec6f4e
DW
1515 SCU_TASK_DONE_SMP_FRM_TYPE_ERR,
1516 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1517
1518 sci_base_state_machine_change_state(&sci_req->state_machine,
1519 SCI_BASE_REQUEST_STATE_COMPLETED);
1520 }
1521
1522 scic_sds_controller_release_frame(sci_req->owning_controller,
1523 frame_index);
1524
1525 return SCI_SUCCESS;
1526}
1527
1528/**
1529 * This method processes the completions transport layer (TL) status to
1530 * determine if the SMP request was sent successfully. If the SMP request
1531 * was sent successfully, then the state for the SMP request transits to
1532 * waiting for a response frame.
1533 * @sci_req: This parameter specifies the request for which the TC
1534 * completion was received.
1535 * @completion_code: This parameter indicates the completion status information
1536 * for the TC.
1537 *
1538 * Indicate if the tc completion handler was successful. SCI_SUCCESS currently
1539 * this method always returns success.
1540 */
1541static enum sci_status scic_sds_smp_request_await_tc_completion_tc_completion_handler(
1542 struct scic_sds_request *sci_req,
1543 u32 completion_code)
1544{
1545 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1546 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1547 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
1548 SCI_SUCCESS);
1549
1550 sci_base_state_machine_change_state(&sci_req->state_machine,
1551 SCI_BASE_REQUEST_STATE_COMPLETED);
1552 break;
1553
1554 default:
1555 /*
1556 * All other completion status cause the IO to be complete. If a NAK
1557 * was received, then it is up to the user to retry the request. */
1558 scic_sds_request_set_status(
1559 sci_req,
1560 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1561 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
1562 );
1563
1564 sci_base_state_machine_change_state(
1565 &sci_req->state_machine,
1566 SCI_BASE_REQUEST_STATE_COMPLETED);
1567 break;
1568 }
1569
1570 return SCI_SUCCESS;
1571}
1572
1573void scic_stp_io_request_set_ncq_tag(struct scic_sds_request *req,
1574 u16 ncq_tag)
1575{
1576 /**
1577 * @note This could be made to return an error to the user if the user
1578 * attempts to set the NCQ tag in the wrong state.
1579 */
1580 req->task_context_buffer->type.stp.ncq_tag = ncq_tag;
1581}
1582
1583/**
1584 *
1585 * @sci_req:
1586 *
1587 * Get the next SGL element from the request. - Check on which SGL element pair
1588 * we are working - if working on SLG pair element A - advance to element B -
1589 * else - check to see if there are more SGL element pairs for this IO request
1590 * - if there are more SGL element pairs - advance to the next pair and return
1591 * element A struct scu_sgl_element*
1592 */
1593static struct scu_sgl_element *scic_sds_stp_request_pio_get_next_sgl(struct scic_sds_stp_request *stp_req)
1594{
1595 struct scu_sgl_element *current_sgl;
1596 struct scic_sds_request *sci_req = to_sci_req(stp_req);
1597 struct scic_sds_request_pio_sgl *pio_sgl = &stp_req->type.pio.request_current;
1598
1599 if (pio_sgl->sgl_set == SCU_SGL_ELEMENT_PAIR_A) {
1600 if (pio_sgl->sgl_pair->B.address_lower == 0 &&
1601 pio_sgl->sgl_pair->B.address_upper == 0) {
1602 current_sgl = NULL;
1603 } else {
1604 pio_sgl->sgl_set = SCU_SGL_ELEMENT_PAIR_B;
1605 current_sgl = &pio_sgl->sgl_pair->B;
1606 }
1607 } else {
1608 if (pio_sgl->sgl_pair->next_pair_lower == 0 &&
1609 pio_sgl->sgl_pair->next_pair_upper == 0) {
1610 current_sgl = NULL;
1611 } else {
1612 u64 phys_addr;
1613
1614 phys_addr = pio_sgl->sgl_pair->next_pair_upper;
1615 phys_addr <<= 32;
1616 phys_addr |= pio_sgl->sgl_pair->next_pair_lower;
1617
1618 pio_sgl->sgl_pair = scic_request_get_virt_addr(sci_req, phys_addr);
1619 pio_sgl->sgl_set = SCU_SGL_ELEMENT_PAIR_A;
1620 current_sgl = &pio_sgl->sgl_pair->A;
1621 }
1622 }
1623
1624 return current_sgl;
1625}
1626
1627/**
1628 *
1629 * @sci_req:
1630 * @completion_code:
1631 *
1632 * This method processes a TC completion. The expected TC completion is for
1633 * the transmission of the H2D register FIS containing the SATA/STP non-data
1634 * request. This method always successfully processes the TC completion.
1635 * SCI_SUCCESS This value is always returned.
1636 */
1637static enum sci_status scic_sds_stp_request_non_data_await_h2d_tc_completion_handler(
1638 struct scic_sds_request *sci_req,
1639 u32 completion_code)
1640{
1641 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1642 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1643 scic_sds_request_set_status(
1644 sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS
1645 );
1646
1647 sci_base_state_machine_change_state(
1648 &sci_req->state_machine,
1649 SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE
1650 );
1651 break;
1652
1653 default:
1654 /*
1655 * All other completion status cause the IO to be complete. If a NAK
1656 * was received, then it is up to the user to retry the request. */
1657 scic_sds_request_set_status(
1658 sci_req,
1659 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1660 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
1661 );
1662
1663 sci_base_state_machine_change_state(
1664 &sci_req->state_machine, SCI_BASE_REQUEST_STATE_COMPLETED);
1665 break;
1666 }
1667
1668 return SCI_SUCCESS;
1669}
1670
1671/**
1672 *
1673 * @request: This parameter specifies the request for which a frame has been
1674 * received.
1675 * @frame_index: This parameter specifies the index of the frame that has been
1676 * received.
1677 *
1678 * This method processes frames received from the target while waiting for a
1679 * device to host register FIS. If a non-register FIS is received during this
1680 * time, it is treated as a protocol violation from an IO perspective. Indicate
1681 * if the received frame was processed successfully.
1682 */
1683static enum sci_status scic_sds_stp_request_non_data_await_d2h_frame_handler(
1684 struct scic_sds_request *sci_req,
1685 u32 frame_index)
1686{
1687 enum sci_status status;
1688 struct dev_to_host_fis *frame_header;
1689 u32 *frame_buffer;
1690 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
1691 struct scic_sds_controller *scic = sci_req->owning_controller;
1692
1693 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1694 frame_index,
1695 (void **)&frame_header);
1696
1697 if (status != SCI_SUCCESS) {
1698 dev_err(scic_to_dev(sci_req->owning_controller),
1699 "%s: SCIC IO Request 0x%p could not get frame header "
1700 "for frame index %d, status %x\n",
1701 __func__, stp_req, frame_index, status);
1702
1703 return status;
1704 }
1705
1706 switch (frame_header->fis_type) {
1707 case FIS_REGD2H:
1708 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1709 frame_index,
1710 (void **)&frame_buffer);
1711
1712 scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
1713 frame_header,
1714 frame_buffer);
1715
1716 /* The command has completed with error */
1717 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_CHECK_RESPONSE,
1718 SCI_FAILURE_IO_RESPONSE_VALID);
1719 break;
1720
1721 default:
1722 dev_warn(scic_to_dev(scic),
1723 "%s: IO Request:0x%p Frame Id:%d protocol "
1724 "violation occurred\n", __func__, stp_req,
1725 frame_index);
1726
1727 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_UNEXP_FIS,
1728 SCI_FAILURE_PROTOCOL_VIOLATION);
1729 break;
1730 }
1731
1732 sci_base_state_machine_change_state(&sci_req->state_machine,
1733 SCI_BASE_REQUEST_STATE_COMPLETED);
1734
1735 /* Frame has been decoded return it to the controller */
1736 scic_sds_controller_release_frame(scic, frame_index);
1737
1738 return status;
1739}
1740
1741#define SCU_MAX_FRAME_BUFFER_SIZE 0x400 /* 1K is the maximum SCU frame data payload */
1742
1743/* transmit DATA_FIS from (current sgl + offset) for input
1744 * parameter length. current sgl and offset is alreay stored in the IO request
1745 */
1746static enum sci_status scic_sds_stp_request_pio_data_out_trasmit_data_frame(
1747 struct scic_sds_request *sci_req,
1748 u32 length)
1749{
1750 struct scic_sds_controller *scic = sci_req->owning_controller;
1751 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
1752 struct scu_task_context *task_context;
1753 struct scu_sgl_element *current_sgl;
1754
1755 /* Recycle the TC and reconstruct it for sending out DATA FIS containing
1756 * for the data from current_sgl+offset for the input length
1757 */
1758 task_context = scic_sds_controller_get_task_context_buffer(scic,
1759 sci_req->io_tag);
1760
1761 if (stp_req->type.pio.request_current.sgl_set == SCU_SGL_ELEMENT_PAIR_A)
1762 current_sgl = &stp_req->type.pio.request_current.sgl_pair->A;
1763 else
1764 current_sgl = &stp_req->type.pio.request_current.sgl_pair->B;
1765
1766 /* update the TC */
1767 task_context->command_iu_upper = current_sgl->address_upper;
1768 task_context->command_iu_lower = current_sgl->address_lower;
1769 task_context->transfer_length_bytes = length;
1770 task_context->type.stp.fis_type = FIS_DATA;
1771
1772 /* send the new TC out. */
1773 return scic_controller_continue_io(sci_req);
1774}
1775
1776static enum sci_status scic_sds_stp_request_pio_data_out_transmit_data(struct scic_sds_request *sci_req)
1777{
1778
1779 struct scu_sgl_element *current_sgl;
1780 u32 sgl_offset;
1781 u32 remaining_bytes_in_current_sgl = 0;
1782 enum sci_status status = SCI_SUCCESS;
1783 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
1784
1785 sgl_offset = stp_req->type.pio.request_current.sgl_offset;
1786
1787 if (stp_req->type.pio.request_current.sgl_set == SCU_SGL_ELEMENT_PAIR_A) {
1788 current_sgl = &(stp_req->type.pio.request_current.sgl_pair->A);
1789 remaining_bytes_in_current_sgl = stp_req->type.pio.request_current.sgl_pair->A.length - sgl_offset;
1790 } else {
1791 current_sgl = &(stp_req->type.pio.request_current.sgl_pair->B);
1792 remaining_bytes_in_current_sgl = stp_req->type.pio.request_current.sgl_pair->B.length - sgl_offset;
1793 }
1794
1795
1796 if (stp_req->type.pio.pio_transfer_bytes > 0) {
1797 if (stp_req->type.pio.pio_transfer_bytes >= remaining_bytes_in_current_sgl) {
1798 /* recycle the TC and send the H2D Data FIS from (current sgl + sgl_offset) and length = remaining_bytes_in_current_sgl */
1799 status = scic_sds_stp_request_pio_data_out_trasmit_data_frame(sci_req, remaining_bytes_in_current_sgl);
1800 if (status == SCI_SUCCESS) {
1801 stp_req->type.pio.pio_transfer_bytes -= remaining_bytes_in_current_sgl;
1802
1803 /* update the current sgl, sgl_offset and save for future */
1804 current_sgl = scic_sds_stp_request_pio_get_next_sgl(stp_req);
1805 sgl_offset = 0;
1806 }
1807 } else if (stp_req->type.pio.pio_transfer_bytes < remaining_bytes_in_current_sgl) {
1808 /* recycle the TC and send the H2D Data FIS from (current sgl + sgl_offset) and length = type.pio.pio_transfer_bytes */
1809 scic_sds_stp_request_pio_data_out_trasmit_data_frame(sci_req, stp_req->type.pio.pio_transfer_bytes);
1810
1811 if (status == SCI_SUCCESS) {
1812 /* Sgl offset will be adjusted and saved for future */
1813 sgl_offset += stp_req->type.pio.pio_transfer_bytes;
1814 current_sgl->address_lower += stp_req->type.pio.pio_transfer_bytes;
1815 stp_req->type.pio.pio_transfer_bytes = 0;
1816 }
1817 }
1818 }
1819
1820 if (status == SCI_SUCCESS) {
1821 stp_req->type.pio.request_current.sgl_offset = sgl_offset;
1822 }
1823
1824 return status;
1825}
1826
1827/**
1828 *
1829 * @stp_request: The request that is used for the SGL processing.
1830 * @data_buffer: The buffer of data to be copied.
1831 * @length: The length of the data transfer.
1832 *
1833 * Copy the data from the buffer for the length specified to the IO reqeust SGL
1834 * specified data region. enum sci_status
1835 */
1836static enum sci_status
1837scic_sds_stp_request_pio_data_in_copy_data_buffer(struct scic_sds_stp_request *stp_req,
1838 u8 *data_buf, u32 len)
1839{
1840 struct scic_sds_request *sci_req;
1841 struct isci_request *ireq;
1842 u8 *src_addr;
1843 int copy_len;
1844 struct sas_task *task;
1845 struct scatterlist *sg;
1846 void *kaddr;
1847 int total_len = len;
1848
1849 sci_req = to_sci_req(stp_req);
1850 ireq = sci_req_to_ireq(sci_req);
1851 task = isci_request_access_task(ireq);
1852 src_addr = data_buf;
1853
1854 if (task->num_scatter > 0) {
1855 sg = task->scatter;
1856
1857 while (total_len > 0) {
1858 struct page *page = sg_page(sg);
1859
1860 copy_len = min_t(int, total_len, sg_dma_len(sg));
1861 kaddr = kmap_atomic(page, KM_IRQ0);
1862 memcpy(kaddr + sg->offset, src_addr, copy_len);
1863 kunmap_atomic(kaddr, KM_IRQ0);
1864 total_len -= copy_len;
1865 src_addr += copy_len;
1866 sg = sg_next(sg);
1867 }
1868 } else {
1869 BUG_ON(task->total_xfer_len < total_len);
1870 memcpy(task->scatter, src_addr, total_len);
1871 }
1872
1873 return SCI_SUCCESS;
1874}
1875
1876/**
1877 *
1878 * @sci_req: The PIO DATA IN request that is to receive the data.
1879 * @data_buffer: The buffer to copy from.
1880 *
1881 * Copy the data buffer to the io request data region. enum sci_status
1882 */
1883static enum sci_status scic_sds_stp_request_pio_data_in_copy_data(
1884 struct scic_sds_stp_request *sci_req,
1885 u8 *data_buffer)
1886{
1887 enum sci_status status;
1888
1889 /*
1890 * If there is less than 1K remaining in the transfer request
1891 * copy just the data for the transfer */
1892 if (sci_req->type.pio.pio_transfer_bytes < SCU_MAX_FRAME_BUFFER_SIZE) {
1893 status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
1894 sci_req, data_buffer, sci_req->type.pio.pio_transfer_bytes);
1895
1896 if (status == SCI_SUCCESS)
1897 sci_req->type.pio.pio_transfer_bytes = 0;
1898 } else {
1899 /* We are transfering the whole frame so copy */
1900 status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
1901 sci_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
1902
1903 if (status == SCI_SUCCESS)
1904 sci_req->type.pio.pio_transfer_bytes -= SCU_MAX_FRAME_BUFFER_SIZE;
1905 }
1906
1907 return status;
1908}
1909
1910/**
1911 *
1912 * @sci_req:
1913 * @completion_code:
1914 *
1915 * enum sci_status
1916 */
1917static enum sci_status scic_sds_stp_request_pio_await_h2d_completion_tc_completion_handler(
1918 struct scic_sds_request *sci_req,
1919 u32 completion_code)
1920{
1921 enum sci_status status = SCI_SUCCESS;
1922
1923 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1924 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1925 scic_sds_request_set_status(
1926 sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS
1927 );
1928
1929 sci_base_state_machine_change_state(
1930 &sci_req->state_machine,
1931 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE
1932 );
1933 break;
1934
1935 default:
1936 /*
1937 * All other completion status cause the IO to be complete. If a NAK
1938 * was received, then it is up to the user to retry the request. */
1939 scic_sds_request_set_status(
1940 sci_req,
1941 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1942 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
1943 );
1944
1945 sci_base_state_machine_change_state(
1946 &sci_req->state_machine,
1947 SCI_BASE_REQUEST_STATE_COMPLETED
1948 );
1949 break;
1950 }
1951
1952 return status;
1953}
1954
1955static enum sci_status scic_sds_stp_request_pio_await_frame_frame_handler(struct scic_sds_request *sci_req,
1956 u32 frame_index)
1957{
1958 struct scic_sds_controller *scic = sci_req->owning_controller;
1959 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
1960 struct isci_request *ireq = sci_req_to_ireq(sci_req);
1961 struct sas_task *task = isci_request_access_task(ireq);
1962 struct dev_to_host_fis *frame_header;
1963 enum sci_status status;
1964 u32 *frame_buffer;
1965
1966 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1967 frame_index,
1968 (void **)&frame_header);
1969
1970 if (status != SCI_SUCCESS) {
1971 dev_err(scic_to_dev(scic),
1972 "%s: SCIC IO Request 0x%p could not get frame header "
1973 "for frame index %d, status %x\n",
1974 __func__, stp_req, frame_index, status);
1975 return status;
1976 }
1977
1978 switch (frame_header->fis_type) {
1979 case FIS_PIO_SETUP:
1980 /* Get from the frame buffer the PIO Setup Data */
1981 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1982 frame_index,
1983 (void **)&frame_buffer);
1984
1985 /* Get the data from the PIO Setup The SCU Hardware returns
1986 * first word in the frame_header and the rest of the data is in
1987 * the frame buffer so we need to back up one dword
1988 */
1989
1990 /* transfer_count: first 16bits in the 4th dword */
1991 stp_req->type.pio.pio_transfer_bytes = frame_buffer[3] & 0xffff;
1992
1993 /* ending_status: 4th byte in the 3rd dword */
1994 stp_req->type.pio.ending_status = (frame_buffer[2] >> 24) & 0xff;
1995
1996 scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
1997 frame_header,
1998 frame_buffer);
1999
2000 sci_req->stp.rsp.status = stp_req->type.pio.ending_status;
2001
2002 /* The next state is dependent on whether the
2003 * request was PIO Data-in or Data out
2004 */
2005 if (task->data_dir == DMA_FROM_DEVICE) {
2006 sci_base_state_machine_change_state(&sci_req->state_machine,
2007 SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE);
2008 } else if (task->data_dir == DMA_TO_DEVICE) {
2009 /* Transmit data */
2010 status = scic_sds_stp_request_pio_data_out_transmit_data(sci_req);
2011 if (status != SCI_SUCCESS)
2012 break;
2013 sci_base_state_machine_change_state(&sci_req->state_machine,
2014 SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE);
2015 }
2016 break;
2017 case FIS_SETDEVBITS:
2018 sci_base_state_machine_change_state(&sci_req->state_machine,
2019 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE);
2020 break;
2021 case FIS_REGD2H:
2022 if (frame_header->status & ATA_BUSY) {
2023 /* Now why is the drive sending a D2H Register FIS when
2024 * it is still busy? Do nothing since we are still in
2025 * the right state.
2026 */
2027 dev_dbg(scic_to_dev(scic),
2028 "%s: SCIC PIO Request 0x%p received "
2029 "D2H Register FIS with BSY status "
2030 "0x%x\n", __func__, stp_req,
2031 frame_header->status);
2032 break;
2033 }
2034
2035 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
2036 frame_index,
2037 (void **)&frame_buffer);
2038
2039 scic_sds_controller_copy_sata_response(&sci_req->stp.req,
2040 frame_header,
2041 frame_buffer);
2042
2043 scic_sds_request_set_status(sci_req,
2044 SCU_TASK_DONE_CHECK_RESPONSE,
2045 SCI_FAILURE_IO_RESPONSE_VALID);
2046
2047 sci_base_state_machine_change_state(&sci_req->state_machine,
2048 SCI_BASE_REQUEST_STATE_COMPLETED);
2049 break;
2050 default:
2051 /* FIXME: what do we do here? */
2052 break;
2053 }
2054
2055 /* Frame is decoded return it to the controller */
2056 scic_sds_controller_release_frame(scic, frame_index);
2057
2058 return status;
2059}
2060
2061static enum sci_status scic_sds_stp_request_pio_data_in_await_data_frame_handler(struct scic_sds_request *sci_req,
2062 u32 frame_index)
2063{
2064 enum sci_status status;
2065 struct dev_to_host_fis *frame_header;
2066 struct sata_fis_data *frame_buffer;
2067 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
2068 struct scic_sds_controller *scic = sci_req->owning_controller;
2069
2070 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
2071 frame_index,
2072 (void **)&frame_header);
2073
2074 if (status != SCI_SUCCESS) {
2075 dev_err(scic_to_dev(scic),
2076 "%s: SCIC IO Request 0x%p could not get frame header "
2077 "for frame index %d, status %x\n",
2078 __func__, stp_req, frame_index, status);
2079 return status;
2080 }
2081
2082 if (frame_header->fis_type == FIS_DATA) {
2083 if (stp_req->type.pio.request_current.sgl_pair == NULL) {
2084 sci_req->saved_rx_frame_index = frame_index;
2085 stp_req->type.pio.pio_transfer_bytes = 0;
2086 } else {
2087 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
2088 frame_index,
2089 (void **)&frame_buffer);
2090
2091 status = scic_sds_stp_request_pio_data_in_copy_data(stp_req,
2092 (u8 *)frame_buffer);
2093
2094 /* Frame is decoded return it to the controller */
2095 scic_sds_controller_release_frame(scic, frame_index);
2096 }
2097
2098 /* Check for the end of the transfer, are there more
2099 * bytes remaining for this data transfer
2100 */
2101 if (status != SCI_SUCCESS ||
2102 stp_req->type.pio.pio_transfer_bytes != 0)
2103 return status;
2104
2105 if ((stp_req->type.pio.ending_status & ATA_BUSY) == 0) {
2106 scic_sds_request_set_status(sci_req,
2107 SCU_TASK_DONE_CHECK_RESPONSE,
2108 SCI_FAILURE_IO_RESPONSE_VALID);
2109
2110 sci_base_state_machine_change_state(&sci_req->state_machine,
2111 SCI_BASE_REQUEST_STATE_COMPLETED);
2112 } else {
2113 sci_base_state_machine_change_state(&sci_req->state_machine,
2114 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE);
2115 }
2116 } else {
2117 dev_err(scic_to_dev(scic),
2118 "%s: SCIC PIO Request 0x%p received frame %d "
2119 "with fis type 0x%02x when expecting a data "
2120 "fis.\n", __func__, stp_req, frame_index,
2121 frame_header->fis_type);
2122
2123 scic_sds_request_set_status(sci_req,
2124 SCU_TASK_DONE_GOOD,
2125 SCI_FAILURE_IO_REQUIRES_SCSI_ABORT);
2126
2127 sci_base_state_machine_change_state(&sci_req->state_machine,
2128 SCI_BASE_REQUEST_STATE_COMPLETED);
2129
2130 /* Frame is decoded return it to the controller */
2131 scic_sds_controller_release_frame(scic, frame_index);
2132 }
2133
2134 return status;
2135}
2136
2137
2138/**
2139 *
2140 * @sci_req:
2141 * @completion_code:
2142 *
2143 * enum sci_status
2144 */
2145static enum sci_status scic_sds_stp_request_pio_data_out_await_data_transmit_completion_tc_completion_handler(
2146
2147 struct scic_sds_request *sci_req,
2148 u32 completion_code)
2149{
2150 enum sci_status status = SCI_SUCCESS;
2151 bool all_frames_transferred = false;
2152 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
2153
2154 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2155 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
2156 /* Transmit data */
2157 if (stp_req->type.pio.pio_transfer_bytes != 0) {
2158 status = scic_sds_stp_request_pio_data_out_transmit_data(sci_req);
2159 if (status == SCI_SUCCESS) {
2160 if (stp_req->type.pio.pio_transfer_bytes == 0)
2161 all_frames_transferred = true;
2162 }
2163 } else if (stp_req->type.pio.pio_transfer_bytes == 0) {
2164 /*
2165 * this will happen if the all data is written at the
2166 * first time after the pio setup fis is received
2167 */
2168 all_frames_transferred = true;
2169 }
2170
2171 /* all data transferred. */
2172 if (all_frames_transferred) {
2173 /*
2174 * Change the state to SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_FRAME_SUBSTATE
2175 * and wait for PIO_SETUP fis / or D2H REg fis. */
2176 sci_base_state_machine_change_state(
2177 &sci_req->state_machine,
2178 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE
2179 );
2180 }
2181 break;
2182
2183 default:
2184 /*
2185 * All other completion status cause the IO to be complete. If a NAK
2186 * was received, then it is up to the user to retry the request. */
2187 scic_sds_request_set_status(
2188 sci_req,
2189 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
2190 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
2191 );
2192
2193 sci_base_state_machine_change_state(
2194 &sci_req->state_machine,
2195 SCI_BASE_REQUEST_STATE_COMPLETED
2196 );
2197 break;
2198 }
2199
2200 return status;
2201}
2202
2203/**
2204 *
2205 * @request: This is the request which is receiving the event.
2206 * @event_code: This is the event code that the request on which the request is
2207 * expected to take action.
2208 *
2209 * This method will handle any link layer events while waiting for the data
2210 * frame. enum sci_status SCI_SUCCESS SCI_FAILURE
2211 */
2212static enum sci_status scic_sds_stp_request_pio_data_in_await_data_event_handler(
2213 struct scic_sds_request *request,
2214 u32 event_code)
2215{
2216 enum sci_status status;
2217
2218 switch (scu_get_event_specifier(event_code)) {
2219 case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
2220 /*
2221 * We are waiting for data and the SCU has R_ERR the data frame.
2222 * Go back to waiting for the D2H Register FIS */
2223 sci_base_state_machine_change_state(
2224 &request->state_machine,
2225 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE
2226 );
2227
2228 status = SCI_SUCCESS;
2229 break;
2230
2231 default:
2232 dev_err(scic_to_dev(request->owning_controller),
2233 "%s: SCIC PIO Request 0x%p received unexpected "
2234 "event 0x%08x\n",
2235 __func__, request, event_code);
2236
2237 /* / @todo Should we fail the PIO request when we get an unexpected event? */
2238 status = SCI_FAILURE;
2239 break;
2240 }
2241
2242 return status;
2243}
2244
2245static void scic_sds_stp_request_udma_complete_request(
2246 struct scic_sds_request *request,
2247 u32 scu_status,
2248 enum sci_status sci_status)
2249{
2250 scic_sds_request_set_status(request, scu_status, sci_status);
2251 sci_base_state_machine_change_state(&request->state_machine,
2252 SCI_BASE_REQUEST_STATE_COMPLETED);
2253}
2254
2255static enum sci_status scic_sds_stp_request_udma_general_frame_handler(struct scic_sds_request *sci_req,
2256 u32 frame_index)
2257{
2258 struct scic_sds_controller *scic = sci_req->owning_controller;
2259 struct dev_to_host_fis *frame_header;
2260 enum sci_status status;
2261 u32 *frame_buffer;
2262
2263 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
2264 frame_index,
2265 (void **)&frame_header);
2266
2267 if ((status == SCI_SUCCESS) &&
2268 (frame_header->fis_type == FIS_REGD2H)) {
2269 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
2270 frame_index,
2271 (void **)&frame_buffer);
2272
2273 scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
2274 frame_header,
2275 frame_buffer);
2276 }
2277
2278 scic_sds_controller_release_frame(scic, frame_index);
2279
2280 return status;
2281}
2282
2283static enum sci_status scic_sds_stp_request_udma_await_tc_completion_tc_completion_handler(
2284 struct scic_sds_request *sci_req,
2285 u32 completion_code)
2286{
2287 enum sci_status status = SCI_SUCCESS;
2288
2289 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2290 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
2291 scic_sds_stp_request_udma_complete_request(sci_req,
2292 SCU_TASK_DONE_GOOD,
2293 SCI_SUCCESS);
2294 break;
2295 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
2296 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
2297 /*
2298 * We must check ther response buffer to see if the D2H Register FIS was
2299 * received before we got the TC completion. */
2300 if (sci_req->stp.rsp.fis_type == FIS_REGD2H) {
2301 scic_sds_remote_device_suspend(sci_req->target_device,
2302 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
2303
2304 scic_sds_stp_request_udma_complete_request(sci_req,
2305 SCU_TASK_DONE_CHECK_RESPONSE,
2306 SCI_FAILURE_IO_RESPONSE_VALID);
2307 } else {
2308 /*
2309 * If we have an error completion status for the TC then we can expect a
2310 * D2H register FIS from the device so we must change state to wait for it */
2311 sci_base_state_machine_change_state(&sci_req->state_machine,
2312 SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE);
2313 }
2314 break;
2315
2316 /*
2317 * / @todo Check to see if any of these completion status need to wait for
2318 * / the device to host register fis. */
2319 /* / @todo We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR - this comes only for B0 */
2320 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_INV_FIS_LEN):
2321 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
2322 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_R_ERR):
2323 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CMD_LL_R_ERR):
2324 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CRC_ERR):
2325 scic_sds_remote_device_suspend(sci_req->target_device,
2326 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
2327 /* Fall through to the default case */
2328 default:
2329 /* All other completion status cause the IO to be complete. */
2330 scic_sds_stp_request_udma_complete_request(sci_req,
2331 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
2332 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
2333 break;
2334 }
2335
2336 return status;
2337}
2338
2339static enum sci_status scic_sds_stp_request_udma_await_d2h_reg_fis_frame_handler(
2340 struct scic_sds_request *sci_req,
2341 u32 frame_index)
2342{
2343 enum sci_status status;
2344
2345 /* Use the general frame handler to copy the resposne data */
2346 status = scic_sds_stp_request_udma_general_frame_handler(sci_req, frame_index);
2347
2348 if (status != SCI_SUCCESS)
2349 return status;
2350
2351 scic_sds_stp_request_udma_complete_request(sci_req,
2352 SCU_TASK_DONE_CHECK_RESPONSE,
2353 SCI_FAILURE_IO_RESPONSE_VALID);
2354
2355 return status;
2356}
2357
2358enum sci_status scic_sds_stp_udma_request_construct(struct scic_sds_request *sci_req,
2359 u32 len,
2360 enum dma_data_direction dir)
2361{
2362 return SCI_SUCCESS;
2363}
2364
2365/**
2366 *
2367 * @sci_req:
2368 * @completion_code:
2369 *
2370 * This method processes a TC completion. The expected TC completion is for
2371 * the transmission of the H2D register FIS containing the SATA/STP non-data
2372 * request. This method always successfully processes the TC completion.
2373 * SCI_SUCCESS This value is always returned.
2374 */
2375static enum sci_status scic_sds_stp_request_soft_reset_await_h2d_asserted_tc_completion_handler(
2376 struct scic_sds_request *sci_req,
2377 u32 completion_code)
2378{
2379 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2380 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
2381 scic_sds_request_set_status(
2382 sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS
2383 );
2384
2385 sci_base_state_machine_change_state(
2386 &sci_req->state_machine,
2387 SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE
2388 );
2389 break;
2390
2391 default:
2392 /*
2393 * All other completion status cause the IO to be complete. If a NAK
2394 * was received, then it is up to the user to retry the request. */
2395 scic_sds_request_set_status(
2396 sci_req,
2397 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
2398 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
2399 );
c72086e3
DW
2400
2401 sci_base_state_machine_change_state(
5dec6f4e
DW
2402 &sci_req->state_machine, SCI_BASE_REQUEST_STATE_COMPLETED);
2403 break;
c72086e3
DW
2404 }
2405
c72086e3
DW
2406 return SCI_SUCCESS;
2407}
2408
2409/**
c72086e3 2410 *
5dec6f4e
DW
2411 * @sci_req:
2412 * @completion_code:
2413 *
2414 * This method processes a TC completion. The expected TC completion is for
2415 * the transmission of the H2D register FIS containing the SATA/STP non-data
2416 * request. This method always successfully processes the TC completion.
2417 * SCI_SUCCESS This value is always returned.
c72086e3 2418 */
5dec6f4e 2419static enum sci_status scic_sds_stp_request_soft_reset_await_h2d_diagnostic_tc_completion_handler(
c72086e3
DW
2420 struct scic_sds_request *sci_req,
2421 u32 completion_code)
2422{
2423 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2424 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
5dec6f4e
DW
2425 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
2426 SCI_SUCCESS);
c72086e3 2427
5dec6f4e
DW
2428 sci_base_state_machine_change_state(&sci_req->state_machine,
2429 SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE);
c72086e3
DW
2430 break;
2431
2432 default:
2433 /*
2434 * All other completion status cause the IO to be complete. If a NAK
2435 * was received, then it is up to the user to retry the request. */
2436 scic_sds_request_set_status(
2437 sci_req,
2438 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
2439 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
2440 );
2441
5dec6f4e
DW
2442 sci_base_state_machine_change_state(&sci_req->state_machine,
2443 SCI_BASE_REQUEST_STATE_COMPLETED);
c72086e3
DW
2444 break;
2445 }
2446
2447 return SCI_SUCCESS;
2448}
2449
5dec6f4e
DW
2450/**
2451 *
2452 * @request: This parameter specifies the request for which a frame has been
2453 * received.
2454 * @frame_index: This parameter specifies the index of the frame that has been
2455 * received.
2456 *
2457 * This method processes frames received from the target while waiting for a
2458 * device to host register FIS. If a non-register FIS is received during this
2459 * time, it is treated as a protocol violation from an IO perspective. Indicate
2460 * if the received frame was processed successfully.
2461 */
2462static enum sci_status scic_sds_stp_request_soft_reset_await_d2h_frame_handler(
2463 struct scic_sds_request *sci_req,
2464 u32 frame_index)
2465{
2466 enum sci_status status;
2467 struct dev_to_host_fis *frame_header;
2468 u32 *frame_buffer;
2469 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
2470 struct scic_sds_controller *scic = sci_req->owning_controller;
2471
2472 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
2473 frame_index,
2474 (void **)&frame_header);
2475 if (status != SCI_SUCCESS) {
2476 dev_err(scic_to_dev(scic),
2477 "%s: SCIC IO Request 0x%p could not get frame header "
2478 "for frame index %d, status %x\n",
2479 __func__, stp_req, frame_index, status);
2480 return status;
2481 }
2482
2483 switch (frame_header->fis_type) {
2484 case FIS_REGD2H:
2485 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
2486 frame_index,
2487 (void **)&frame_buffer);
2488
2489 scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
2490 frame_header,
2491 frame_buffer);
2492
2493 /* The command has completed with error */
2494 scic_sds_request_set_status(sci_req,
2495 SCU_TASK_DONE_CHECK_RESPONSE,
2496 SCI_FAILURE_IO_RESPONSE_VALID);
2497 break;
2498
2499 default:
2500 dev_warn(scic_to_dev(scic),
2501 "%s: IO Request:0x%p Frame Id:%d protocol "
2502 "violation occurred\n", __func__, stp_req,
2503 frame_index);
2504
2505 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_UNEXP_FIS,
2506 SCI_FAILURE_PROTOCOL_VIOLATION);
2507 break;
2508 }
2509
2510 sci_base_state_machine_change_state(&sci_req->state_machine,
2511 SCI_BASE_REQUEST_STATE_COMPLETED);
2512
2513 /* Frame has been decoded return it to the controller */
2514 scic_sds_controller_release_frame(scic, frame_index);
2515
2516 return status;
2517}
2518
f1f52e75 2519static const struct scic_sds_io_request_state_handler scic_sds_request_state_handler_table[] = {
f4636a7b
PS
2520 [SCI_BASE_REQUEST_STATE_INITIAL] = {},
2521 [SCI_BASE_REQUEST_STATE_CONSTRUCTED] = {},
f1f52e75 2522 [SCI_BASE_REQUEST_STATE_STARTED] = {
f1f52e75
DW
2523 .tc_completion_handler = scic_sds_request_started_state_tc_completion_handler,
2524 .frame_handler = scic_sds_request_started_state_frame_handler,
2525 },
f139303d 2526 [SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION] = {
f139303d
DW
2527 .tc_completion_handler = scic_sds_ssp_task_request_await_tc_completion_tc_completion_handler,
2528 },
2529 [SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE] = {
f139303d
DW
2530 .frame_handler = scic_sds_ssp_task_request_await_tc_response_frame_handler,
2531 },
c72086e3 2532 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE] = {
c72086e3
DW
2533 .tc_completion_handler = scic_sds_smp_request_await_response_tc_completion_handler,
2534 .frame_handler = scic_sds_smp_request_await_response_frame_handler,
2535 },
2536 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION] = {
c72086e3
DW
2537 .tc_completion_handler = scic_sds_smp_request_await_tc_completion_tc_completion_handler,
2538 },
5dec6f4e 2539 [SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE] = {
5dec6f4e
DW
2540 .tc_completion_handler = scic_sds_stp_request_udma_await_tc_completion_tc_completion_handler,
2541 .frame_handler = scic_sds_stp_request_udma_general_frame_handler,
2542 },
2543 [SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE] = {
5dec6f4e
DW
2544 .frame_handler = scic_sds_stp_request_udma_await_d2h_reg_fis_frame_handler,
2545 },
2546 [SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE] = {
5dec6f4e
DW
2547 .tc_completion_handler = scic_sds_stp_request_non_data_await_h2d_tc_completion_handler,
2548 },
2549 [SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE] = {
5dec6f4e
DW
2550 .frame_handler = scic_sds_stp_request_non_data_await_d2h_frame_handler,
2551 },
2552 [SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE] = {
5dec6f4e
DW
2553 .tc_completion_handler = scic_sds_stp_request_pio_await_h2d_completion_tc_completion_handler,
2554 },
2555 [SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE] = {
5dec6f4e
DW
2556 .frame_handler = scic_sds_stp_request_pio_await_frame_frame_handler
2557 },
2558 [SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE] = {
5dec6f4e
DW
2559 .event_handler = scic_sds_stp_request_pio_data_in_await_data_event_handler,
2560 .frame_handler = scic_sds_stp_request_pio_data_in_await_data_frame_handler
2561 },
2562 [SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE] = {
5dec6f4e
DW
2563 .tc_completion_handler = scic_sds_stp_request_pio_data_out_await_data_transmit_completion_tc_completion_handler,
2564 },
2565 [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE] = {
5dec6f4e
DW
2566 .tc_completion_handler = scic_sds_stp_request_soft_reset_await_h2d_asserted_tc_completion_handler,
2567 },
2568 [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE] = {
5dec6f4e
DW
2569 .tc_completion_handler = scic_sds_stp_request_soft_reset_await_h2d_diagnostic_tc_completion_handler,
2570 },
2571 [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE] = {
5dec6f4e
DW
2572 .frame_handler = scic_sds_stp_request_soft_reset_await_d2h_frame_handler,
2573 },
f1f52e75
DW
2574 [SCI_BASE_REQUEST_STATE_COMPLETED] = {
2575 .complete_handler = scic_sds_request_completed_state_complete_handler,
2576 },
2577 [SCI_BASE_REQUEST_STATE_ABORTING] = {
f1f52e75
DW
2578 .tc_completion_handler = scic_sds_request_aborting_state_tc_completion_handler,
2579 .frame_handler = scic_sds_request_aborting_state_frame_handler,
2580 },
f139303d 2581 [SCI_BASE_REQUEST_STATE_FINAL] = { },
f1f52e75
DW
2582};
2583
2584
6f231dda 2585/**
f1f52e75
DW
2586 * isci_request_process_response_iu() - This function sets the status and
2587 * response iu, in the task struct, from the request object for the upper
2588 * layer driver.
2589 * @sas_task: This parameter is the task struct from the upper layer driver.
2590 * @resp_iu: This parameter points to the response iu of the completed request.
2591 * @dev: This parameter specifies the linux device struct.
6f231dda
DW
2592 *
2593 * none.
2594 */
f1f52e75
DW
2595static void isci_request_process_response_iu(
2596 struct sas_task *task,
2597 struct ssp_response_iu *resp_iu,
2598 struct device *dev)
6f231dda 2599{
f1f52e75
DW
2600 dev_dbg(dev,
2601 "%s: resp_iu = %p "
2602 "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
2603 "resp_iu->response_data_len = %x, "
2604 "resp_iu->sense_data_len = %x\nrepsonse data: ",
6f231dda 2605 __func__,
f1f52e75
DW
2606 resp_iu,
2607 resp_iu->status,
2608 resp_iu->datapres,
2609 resp_iu->response_data_len,
2610 resp_iu->sense_data_len);
6f231dda 2611
f1f52e75 2612 task->task_status.stat = resp_iu->status;
6f231dda 2613
f1f52e75
DW
2614 /* libsas updates the task status fields based on the response iu. */
2615 sas_ssp_task_response(dev, task, resp_iu);
2616}
6f231dda 2617
f1f52e75
DW
2618/**
2619 * isci_request_set_open_reject_status() - This function prepares the I/O
2620 * completion for OPEN_REJECT conditions.
2621 * @request: This parameter is the completed isci_request object.
2622 * @response_ptr: This parameter specifies the service response for the I/O.
2623 * @status_ptr: This parameter specifies the exec status for the I/O.
2624 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2625 * the LLDD with respect to completing this request or forcing an abort
2626 * condition on the I/O.
2627 * @open_rej_reason: This parameter specifies the encoded reason for the
2628 * abandon-class reject.
2629 *
2630 * none.
2631 */
2632static void isci_request_set_open_reject_status(
2633 struct isci_request *request,
2634 struct sas_task *task,
2635 enum service_response *response_ptr,
2636 enum exec_status *status_ptr,
2637 enum isci_completion_selection *complete_to_host_ptr,
2638 enum sas_open_rej_reason open_rej_reason)
2639{
2640 /* Task in the target is done. */
2641 request->complete_in_target = true;
2642 *response_ptr = SAS_TASK_UNDELIVERED;
2643 *status_ptr = SAS_OPEN_REJECT;
2644 *complete_to_host_ptr = isci_perform_normal_io_completion;
2645 task->task_status.open_rej_reason = open_rej_reason;
2646}
6f231dda 2647
f1f52e75
DW
2648/**
2649 * isci_request_handle_controller_specific_errors() - This function decodes
2650 * controller-specific I/O completion error conditions.
2651 * @request: This parameter is the completed isci_request object.
2652 * @response_ptr: This parameter specifies the service response for the I/O.
2653 * @status_ptr: This parameter specifies the exec status for the I/O.
2654 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2655 * the LLDD with respect to completing this request or forcing an abort
2656 * condition on the I/O.
2657 *
2658 * none.
2659 */
2660static void isci_request_handle_controller_specific_errors(
2661 struct isci_remote_device *isci_device,
2662 struct isci_request *request,
2663 struct sas_task *task,
2664 enum service_response *response_ptr,
2665 enum exec_status *status_ptr,
2666 enum isci_completion_selection *complete_to_host_ptr)
2667{
2668 unsigned int cstatus;
6f231dda 2669
f1f52e75 2670 cstatus = request->sci.scu_status;
a5fde225 2671
f1f52e75
DW
2672 dev_dbg(&request->isci_host->pdev->dev,
2673 "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
2674 "- controller status = 0x%x\n",
2675 __func__, request, cstatus);
6f231dda 2676
f1f52e75
DW
2677 /* Decode the controller-specific errors; most
2678 * important is to recognize those conditions in which
2679 * the target may still have a task outstanding that
2680 * must be aborted.
2681 *
2682 * Note that there are SCU completion codes being
2683 * named in the decode below for which SCIC has already
2684 * done work to handle them in a way other than as
2685 * a controller-specific completion code; these are left
2686 * in the decode below for completeness sake.
2687 */
2688 switch (cstatus) {
2689 case SCU_TASK_DONE_DMASETUP_DIRERR:
2690 /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
2691 case SCU_TASK_DONE_XFERCNT_ERR:
2692 /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
2693 if (task->task_proto == SAS_PROTOCOL_SMP) {
2694 /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
2695 *response_ptr = SAS_TASK_COMPLETE;
6f231dda 2696
f1f52e75
DW
2697 /* See if the device has been/is being stopped. Note
2698 * that we ignore the quiesce state, since we are
6f231dda
DW
2699 * concerned about the actual device state.
2700 */
f1f52e75
DW
2701 if ((isci_device->status == isci_stopping) ||
2702 (isci_device->status == isci_stopped))
2703 *status_ptr = SAS_DEVICE_UNKNOWN;
2704 else
2705 *status_ptr = SAS_ABORTED_TASK;
6f231dda 2706
f1f52e75 2707 request->complete_in_target = true;
6f231dda 2708
f1f52e75
DW
2709 *complete_to_host_ptr =
2710 isci_perform_normal_io_completion;
2711 } else {
2712 /* Task in the target is not done. */
2713 *response_ptr = SAS_TASK_UNDELIVERED;
a5fde225 2714
f1f52e75
DW
2715 if ((isci_device->status == isci_stopping) ||
2716 (isci_device->status == isci_stopped))
2717 *status_ptr = SAS_DEVICE_UNKNOWN;
2718 else
2719 *status_ptr = SAM_STAT_TASK_ABORTED;
6f231dda 2720
f1f52e75 2721 request->complete_in_target = false;
6f231dda 2722
f1f52e75
DW
2723 *complete_to_host_ptr =
2724 isci_perform_error_io_completion;
2725 }
2726
2727 break;
2728
2729 case SCU_TASK_DONE_CRC_ERR:
2730 case SCU_TASK_DONE_NAK_CMD_ERR:
2731 case SCU_TASK_DONE_EXCESS_DATA:
2732 case SCU_TASK_DONE_UNEXP_FIS:
2733 /* Also SCU_TASK_DONE_UNEXP_RESP: */
2734 case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
2735 case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
2736 case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
2737 /* These are conditions in which the target
2738 * has completed the task, so that no cleanup
2739 * is necessary.
6f231dda 2740 */
f1f52e75 2741 *response_ptr = SAS_TASK_COMPLETE;
6f231dda
DW
2742
2743 /* See if the device has been/is being stopped. Note
2744 * that we ignore the quiesce state, since we are
2745 * concerned about the actual device state.
2746 */
2747 if ((isci_device->status == isci_stopping) ||
2748 (isci_device->status == isci_stopped))
f1f52e75 2749 *status_ptr = SAS_DEVICE_UNKNOWN;
6f231dda 2750 else
f1f52e75 2751 *status_ptr = SAS_ABORTED_TASK;
6f231dda 2752
f1f52e75 2753 request->complete_in_target = true;
a5fde225 2754
f1f52e75 2755 *complete_to_host_ptr = isci_perform_normal_io_completion;
6f231dda
DW
2756 break;
2757
6f231dda 2758
f1f52e75
DW
2759 /* Note that the only open reject completion codes seen here will be
2760 * abandon-class codes; all others are automatically retried in the SCU.
2761 */
2762 case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
a5fde225 2763
f1f52e75
DW
2764 isci_request_set_open_reject_status(
2765 request, task, response_ptr, status_ptr,
2766 complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
2767 break;
a5fde225 2768
f1f52e75 2769 case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
6f231dda 2770
f1f52e75
DW
2771 /* Note - the return of AB0 will change when
2772 * libsas implements detection of zone violations.
2773 */
2774 isci_request_set_open_reject_status(
2775 request, task, response_ptr, status_ptr,
2776 complete_to_host_ptr, SAS_OREJ_RESV_AB0);
2777 break;
6f231dda 2778
f1f52e75 2779 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
6f231dda 2780
f1f52e75
DW
2781 isci_request_set_open_reject_status(
2782 request, task, response_ptr, status_ptr,
2783 complete_to_host_ptr, SAS_OREJ_RESV_AB1);
2784 break;
6f231dda 2785
f1f52e75 2786 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
6f231dda 2787
f1f52e75
DW
2788 isci_request_set_open_reject_status(
2789 request, task, response_ptr, status_ptr,
2790 complete_to_host_ptr, SAS_OREJ_RESV_AB2);
2791 break;
6f231dda 2792
f1f52e75 2793 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
6f231dda 2794
f1f52e75
DW
2795 isci_request_set_open_reject_status(
2796 request, task, response_ptr, status_ptr,
2797 complete_to_host_ptr, SAS_OREJ_RESV_AB3);
2798 break;
6f231dda 2799
f1f52e75 2800 case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
6f231dda 2801
f1f52e75
DW
2802 isci_request_set_open_reject_status(
2803 request, task, response_ptr, status_ptr,
2804 complete_to_host_ptr, SAS_OREJ_BAD_DEST);
2805 break;
6f231dda 2806
f1f52e75 2807 case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
6f231dda 2808
f1f52e75
DW
2809 isci_request_set_open_reject_status(
2810 request, task, response_ptr, status_ptr,
2811 complete_to_host_ptr, SAS_OREJ_STP_NORES);
2812 break;
6f231dda 2813
f1f52e75 2814 case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
6f231dda 2815
f1f52e75
DW
2816 isci_request_set_open_reject_status(
2817 request, task, response_ptr, status_ptr,
2818 complete_to_host_ptr, SAS_OREJ_EPROTO);
2819 break;
6f231dda 2820
f1f52e75 2821 case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
6f231dda 2822
f1f52e75
DW
2823 isci_request_set_open_reject_status(
2824 request, task, response_ptr, status_ptr,
2825 complete_to_host_ptr, SAS_OREJ_CONN_RATE);
2826 break;
6f231dda 2827
f1f52e75
DW
2828 case SCU_TASK_DONE_LL_R_ERR:
2829 /* Also SCU_TASK_DONE_ACK_NAK_TO: */
2830 case SCU_TASK_DONE_LL_PERR:
2831 case SCU_TASK_DONE_LL_SY_TERM:
2832 /* Also SCU_TASK_DONE_NAK_ERR:*/
2833 case SCU_TASK_DONE_LL_LF_TERM:
2834 /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
2835 case SCU_TASK_DONE_LL_ABORT_ERR:
2836 case SCU_TASK_DONE_SEQ_INV_TYPE:
2837 /* Also SCU_TASK_DONE_UNEXP_XR: */
2838 case SCU_TASK_DONE_XR_IU_LEN_ERR:
2839 case SCU_TASK_DONE_INV_FIS_LEN:
2840 /* Also SCU_TASK_DONE_XR_WD_LEN: */
2841 case SCU_TASK_DONE_SDMA_ERR:
2842 case SCU_TASK_DONE_OFFSET_ERR:
2843 case SCU_TASK_DONE_MAX_PLD_ERR:
2844 case SCU_TASK_DONE_LF_ERR:
2845 case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
2846 case SCU_TASK_DONE_SMP_LL_RX_ERR:
2847 case SCU_TASK_DONE_UNEXP_DATA:
2848 case SCU_TASK_DONE_UNEXP_SDBFIS:
2849 case SCU_TASK_DONE_REG_ERR:
2850 case SCU_TASK_DONE_SDB_ERR:
2851 case SCU_TASK_DONE_TASK_ABORT:
2852 default:
2853 /* Task in the target is not done. */
2854 *response_ptr = SAS_TASK_UNDELIVERED;
2855 *status_ptr = SAM_STAT_TASK_ABORTED;
2856 request->complete_in_target = false;
6f231dda 2857
f1f52e75
DW
2858 *complete_to_host_ptr = isci_perform_error_io_completion;
2859 break;
2860 }
2861}
6f231dda 2862
f1f52e75
DW
2863/**
2864 * isci_task_save_for_upper_layer_completion() - This function saves the
2865 * request for later completion to the upper layer driver.
2866 * @host: This parameter is a pointer to the host on which the the request
2867 * should be queued (either as an error or success).
2868 * @request: This parameter is the completed request.
2869 * @response: This parameter is the response code for the completed task.
2870 * @status: This parameter is the status code for the completed task.
2871 *
2872 * none.
2873 */
2874static void isci_task_save_for_upper_layer_completion(
2875 struct isci_host *host,
2876 struct isci_request *request,
2877 enum service_response response,
2878 enum exec_status status,
2879 enum isci_completion_selection task_notification_selection)
2880{
2881 struct sas_task *task = isci_request_access_task(request);
6f231dda 2882
f1f52e75
DW
2883 task_notification_selection
2884 = isci_task_set_completion_status(task, response, status,
2885 task_notification_selection);
6f231dda 2886
f1f52e75
DW
2887 /* Tasks aborted specifically by a call to the lldd_abort_task
2888 * function should not be completed to the host in the regular path.
2889 */
2890 switch (task_notification_selection) {
6f231dda 2891
f1f52e75 2892 case isci_perform_normal_io_completion:
6f231dda 2893
f1f52e75
DW
2894 /* Normal notification (task_done) */
2895 dev_dbg(&host->pdev->dev,
2896 "%s: Normal - task = %p, response=%d (%d), status=%d (%d)\n",
2897 __func__,
2898 task,
2899 task->task_status.resp, response,
2900 task->task_status.stat, status);
2901 /* Add to the completed list. */
2902 list_add(&request->completed_node,
2903 &host->requests_to_complete);
6f231dda 2904
f1f52e75
DW
2905 /* Take the request off the device's pending request list. */
2906 list_del_init(&request->dev_node);
2907 break;
6f231dda 2908
f1f52e75
DW
2909 case isci_perform_aborted_io_completion:
2910 /* No notification to libsas because this request is
2911 * already in the abort path.
2912 */
2913 dev_warn(&host->pdev->dev,
2914 "%s: Aborted - task = %p, response=%d (%d), status=%d (%d)\n",
2915 __func__,
2916 task,
2917 task->task_status.resp, response,
2918 task->task_status.stat, status);
6f231dda 2919
f1f52e75
DW
2920 /* Wake up whatever process was waiting for this
2921 * request to complete.
2922 */
2923 WARN_ON(request->io_request_completion == NULL);
6f231dda 2924
f1f52e75
DW
2925 if (request->io_request_completion != NULL) {
2926
2927 /* Signal whoever is waiting that this
2928 * request is complete.
2929 */
2930 complete(request->io_request_completion);
2931 }
2932 break;
2933
2934 case isci_perform_error_io_completion:
2935 /* Use sas_task_abort */
2936 dev_warn(&host->pdev->dev,
2937 "%s: Error - task = %p, response=%d (%d), status=%d (%d)\n",
2938 __func__,
2939 task,
2940 task->task_status.resp, response,
2941 task->task_status.stat, status);
2942 /* Add to the aborted list. */
2943 list_add(&request->completed_node,
2944 &host->requests_to_errorback);
2945 break;
2946
2947 default:
2948 dev_warn(&host->pdev->dev,
2949 "%s: Unknown - task = %p, response=%d (%d), status=%d (%d)\n",
2950 __func__,
2951 task,
2952 task->task_status.resp, response,
2953 task->task_status.stat, status);
2954
2955 /* Add to the error to libsas list. */
2956 list_add(&request->completed_node,
2957 &host->requests_to_errorback);
2958 break;
2959 }
2960}
2961
2962static void isci_request_io_request_complete(struct isci_host *isci_host,
2963 struct isci_request *request,
2964 enum sci_io_status completion_status)
2965{
2966 struct sas_task *task = isci_request_access_task(request);
2967 struct ssp_response_iu *resp_iu;
2968 void *resp_buf;
2969 unsigned long task_flags;
2970 struct isci_remote_device *isci_device = request->isci_device;
2971 enum service_response response = SAS_TASK_UNDELIVERED;
2972 enum exec_status status = SAS_ABORTED_TASK;
2973 enum isci_request_status request_status;
2974 enum isci_completion_selection complete_to_host
2975 = isci_perform_normal_io_completion;
2976
2977 dev_dbg(&isci_host->pdev->dev,
2978 "%s: request = %p, task = %p,\n"
2979 "task->data_dir = %d completion_status = 0x%x\n",
2980 __func__,
2981 request,
2982 task,
2983 task->data_dir,
2984 completion_status);
2985
2986 spin_lock(&request->state_lock);
2987 request_status = isci_request_get_state(request);
2988
2989 /* Decode the request status. Note that if the request has been
2990 * aborted by a task management function, we don't care
2991 * what the status is.
2992 */
2993 switch (request_status) {
2994
2995 case aborted:
2996 /* "aborted" indicates that the request was aborted by a task
2997 * management function, since once a task management request is
2998 * perfomed by the device, the request only completes because
2999 * of the subsequent driver terminate.
3000 *
3001 * Aborted also means an external thread is explicitly managing
3002 * this request, so that we do not complete it up the stack.
3003 *
3004 * The target is still there (since the TMF was successful).
3005 */
3006 request->complete_in_target = true;
3007 response = SAS_TASK_COMPLETE;
3008
3009 /* See if the device has been/is being stopped. Note
3010 * that we ignore the quiesce state, since we are
3011 * concerned about the actual device state.
3012 */
3013 if ((isci_device->status == isci_stopping)
3014 || (isci_device->status == isci_stopped)
3015 )
3016 status = SAS_DEVICE_UNKNOWN;
3017 else
3018 status = SAS_ABORTED_TASK;
3019
3020 complete_to_host = isci_perform_aborted_io_completion;
3021 /* This was an aborted request. */
3022
3023 spin_unlock(&request->state_lock);
3024 break;
3025
3026 case aborting:
3027 /* aborting means that the task management function tried and
3028 * failed to abort the request. We need to note the request
3029 * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
3030 * target as down.
3031 *
3032 * Aborting also means an external thread is explicitly managing
3033 * this request, so that we do not complete it up the stack.
3034 */
3035 request->complete_in_target = true;
3036 response = SAS_TASK_UNDELIVERED;
3037
3038 if ((isci_device->status == isci_stopping) ||
3039 (isci_device->status == isci_stopped))
3040 /* The device has been /is being stopped. Note that
3041 * we ignore the quiesce state, since we are
3042 * concerned about the actual device state.
3043 */
3044 status = SAS_DEVICE_UNKNOWN;
3045 else
3046 status = SAS_PHY_DOWN;
3047
3048 complete_to_host = isci_perform_aborted_io_completion;
3049
3050 /* This was an aborted request. */
3051
3052 spin_unlock(&request->state_lock);
3053 break;
3054
3055 case terminating:
3056
3057 /* This was an terminated request. This happens when
3058 * the I/O is being terminated because of an action on
3059 * the device (reset, tear down, etc.), and the I/O needs
3060 * to be completed up the stack.
3061 */
3062 request->complete_in_target = true;
3063 response = SAS_TASK_UNDELIVERED;
3064
3065 /* See if the device has been/is being stopped. Note
3066 * that we ignore the quiesce state, since we are
3067 * concerned about the actual device state.
3068 */
3069 if ((isci_device->status == isci_stopping) ||
3070 (isci_device->status == isci_stopped))
3071 status = SAS_DEVICE_UNKNOWN;
3072 else
3073 status = SAS_ABORTED_TASK;
3074
3075 complete_to_host = isci_perform_aborted_io_completion;
3076
3077 /* This was a terminated request. */
3078
3079 spin_unlock(&request->state_lock);
3080 break;
3081
3082 default:
3083
3084 /* The request is done from an SCU HW perspective. */
3085 request->status = completed;
3086
3087 spin_unlock(&request->state_lock);
3088
3089 /* This is an active request being completed from the core. */
3090 switch (completion_status) {
3091
3092 case SCI_IO_FAILURE_RESPONSE_VALID:
3093 dev_dbg(&isci_host->pdev->dev,
3094 "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
3095 __func__,
3096 request,
3097 task);
3098
3099 if (sas_protocol_ata(task->task_proto)) {
3100 resp_buf = &request->sci.stp.rsp;
3101 isci_request_process_stp_response(task,
3102 resp_buf);
3103 } else if (SAS_PROTOCOL_SSP == task->task_proto) {
3104
3105 /* crack the iu response buffer. */
3106 resp_iu = &request->sci.ssp.rsp;
3107 isci_request_process_response_iu(task, resp_iu,
3108 &isci_host->pdev->dev);
3109
3110 } else if (SAS_PROTOCOL_SMP == task->task_proto) {
3111
3112 dev_err(&isci_host->pdev->dev,
3113 "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
3114 "SAS_PROTOCOL_SMP protocol\n",
3115 __func__);
3116
3117 } else
3118 dev_err(&isci_host->pdev->dev,
3119 "%s: unknown protocol\n", __func__);
3120
3121 /* use the task status set in the task struct by the
3122 * isci_request_process_response_iu call.
3123 */
3124 request->complete_in_target = true;
3125 response = task->task_status.resp;
3126 status = task->task_status.stat;
3127 break;
3128
3129 case SCI_IO_SUCCESS:
3130 case SCI_IO_SUCCESS_IO_DONE_EARLY:
3131
3132 response = SAS_TASK_COMPLETE;
3133 status = SAM_STAT_GOOD;
3134 request->complete_in_target = true;
3135
3136 if (task->task_proto == SAS_PROTOCOL_SMP) {
3137 void *rsp = &request->sci.smp.rsp;
3138
3139 dev_dbg(&isci_host->pdev->dev,
3140 "%s: SMP protocol completion\n",
3141 __func__);
3142
3143 sg_copy_from_buffer(
3144 &task->smp_task.smp_resp, 1,
3145 rsp, sizeof(struct smp_resp));
3146 } else if (completion_status
3147 == SCI_IO_SUCCESS_IO_DONE_EARLY) {
3148
3149 /* This was an SSP / STP / SATA transfer.
3150 * There is a possibility that less data than
3151 * the maximum was transferred.
3152 */
3153 u32 transferred_length = sci_req_tx_bytes(&request->sci);
3154
3155 task->task_status.residual
3156 = task->total_xfer_len - transferred_length;
3157
3158 /* If there were residual bytes, call this an
3159 * underrun.
3160 */
3161 if (task->task_status.residual != 0)
3162 status = SAS_DATA_UNDERRUN;
3163
3164 dev_dbg(&isci_host->pdev->dev,
3165 "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
3166 __func__,
3167 status);
3168
3169 } else
3170 dev_dbg(&isci_host->pdev->dev,
3171 "%s: SCI_IO_SUCCESS\n",
3172 __func__);
3173
3174 break;
3175
3176 case SCI_IO_FAILURE_TERMINATED:
3177 dev_dbg(&isci_host->pdev->dev,
3178 "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
3179 __func__,
3180 request,
3181 task);
3182
3183 /* The request was terminated explicitly. No handling
3184 * is needed in the SCSI error handler path.
3185 */
3186 request->complete_in_target = true;
3187 response = SAS_TASK_UNDELIVERED;
3188
3189 /* See if the device has been/is being stopped. Note
3190 * that we ignore the quiesce state, since we are
3191 * concerned about the actual device state.
3192 */
3193 if ((isci_device->status == isci_stopping) ||
3194 (isci_device->status == isci_stopped))
3195 status = SAS_DEVICE_UNKNOWN;
3196 else
3197 status = SAS_ABORTED_TASK;
3198
3199 complete_to_host = isci_perform_normal_io_completion;
3200 break;
3201
3202 case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
3203
3204 isci_request_handle_controller_specific_errors(
3205 isci_device, request, task, &response, &status,
3206 &complete_to_host);
3207
3208 break;
3209
3210 case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
3211 /* This is a special case, in that the I/O completion
3212 * is telling us that the device needs a reset.
3213 * In order for the device reset condition to be
3214 * noticed, the I/O has to be handled in the error
3215 * handler. Set the reset flag and cause the
3216 * SCSI error thread to be scheduled.
3217 */
3218 spin_lock_irqsave(&task->task_state_lock, task_flags);
3219 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
6f231dda
DW
3220 spin_unlock_irqrestore(&task->task_state_lock, task_flags);
3221
f1f52e75
DW
3222 /* Fail the I/O. */
3223 response = SAS_TASK_UNDELIVERED;
3224 status = SAM_STAT_TASK_ABORTED;
3225
3226 complete_to_host = isci_perform_error_io_completion;
3227 request->complete_in_target = false;
3228 break;
3229
3230 default:
3231 /* Catch any otherwise unhandled error codes here. */
3232 dev_warn(&isci_host->pdev->dev,
3233 "%s: invalid completion code: 0x%x - "
3234 "isci_request = %p\n",
3235 __func__, completion_status, request);
3236
3237 response = SAS_TASK_UNDELIVERED;
3238
3239 /* See if the device has been/is being stopped. Note
3240 * that we ignore the quiesce state, since we are
3241 * concerned about the actual device state.
3242 */
3243 if ((isci_device->status == isci_stopping) ||
3244 (isci_device->status == isci_stopped))
3245 status = SAS_DEVICE_UNKNOWN;
3246 else
3247 status = SAS_ABORTED_TASK;
3248
3249 complete_to_host = isci_perform_error_io_completion;
3250 request->complete_in_target = false;
3251 break;
3252 }
3253 break;
3254 }
3255
3256 isci_request_unmap_sgl(request, isci_host->pdev);
3257
3258 /* Put the completed request on the correct list */
3259 isci_task_save_for_upper_layer_completion(isci_host, request, response,
3260 status, complete_to_host
3261 );
3262
3263 /* complete the io request to the core. */
3264 scic_controller_complete_io(&isci_host->sci,
3265 &isci_device->sci,
3266 &request->sci);
3267 /* set terminated handle so it cannot be completed or
3268 * terminated again, and to cause any calls into abort
3269 * task to recognize the already completed case.
3270 */
3271 request->terminated = true;
3272
3273 isci_host_can_dequeue(isci_host, 1);
3274}
3275
3276/**
3277 * scic_sds_request_initial_state_enter() -
3278 * @object: This parameter specifies the base object for which the state
3279 * transition is occurring.
3280 *
3281 * This method implements the actions taken when entering the
3282 * SCI_BASE_REQUEST_STATE_INITIAL state. This state is entered when the initial
3283 * base request is constructed. Entry into the initial state sets all handlers
3284 * for the io request object to their default handlers. none
3285 */
3286static void scic_sds_request_initial_state_enter(void *object)
3287{
3288 struct scic_sds_request *sci_req = object;
3289
3290 SET_STATE_HANDLER(
3291 sci_req,
3292 scic_sds_request_state_handler_table,
3293 SCI_BASE_REQUEST_STATE_INITIAL
3294 );
3295}
3296
3297/**
3298 * scic_sds_request_constructed_state_enter() -
3299 * @object: The io request object that is to enter the constructed state.
3300 *
3301 * This method implements the actions taken when entering the
3302 * SCI_BASE_REQUEST_STATE_CONSTRUCTED state. The method sets the state handlers
3303 * for the the constructed state. none
3304 */
3305static void scic_sds_request_constructed_state_enter(void *object)
3306{
3307 struct scic_sds_request *sci_req = object;
3308
3309 SET_STATE_HANDLER(
3310 sci_req,
3311 scic_sds_request_state_handler_table,
3312 SCI_BASE_REQUEST_STATE_CONSTRUCTED
3313 );
3314}
3315
f1f52e75
DW
3316static void scic_sds_request_started_state_enter(void *object)
3317{
3318 struct scic_sds_request *sci_req = object;
f139303d
DW
3319 struct sci_base_state_machine *sm = &sci_req->state_machine;
3320 struct isci_request *ireq = sci_req_to_ireq(sci_req);
3321 struct domain_device *dev = sci_dev_to_domain(sci_req->target_device);
c72086e3
DW
3322 struct sas_task *task;
3323
3324 /* XXX as hch said always creating an internal sas_task for tmf
3325 * requests would simplify the driver
3326 */
3327 task = ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL;
f1f52e75
DW
3328
3329 SET_STATE_HANDLER(
3330 sci_req,
3331 scic_sds_request_state_handler_table,
3332 SCI_BASE_REQUEST_STATE_STARTED
3333 );
3334
5dec6f4e
DW
3335 /* all unaccelerated request types (non ssp or ncq) handled with
3336 * substates
f139303d 3337 */
c72086e3
DW
3338 if (!task && dev->dev_type == SAS_END_DEV) {
3339 sci_base_state_machine_change_state(sm,
3340 SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION);
5dec6f4e
DW
3341 } else if (!task &&
3342 (isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_high ||
3343 isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_low)) {
3344 sci_base_state_machine_change_state(sm,
3345 SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE);
c72086e3
DW
3346 } else if (task && task->task_proto == SAS_PROTOCOL_SMP) {
3347 sci_base_state_machine_change_state(sm,
3348 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE);
5dec6f4e
DW
3349 } else if (task && sas_protocol_ata(task->task_proto) &&
3350 !task->ata_task.use_ncq) {
3351 u32 state;
3352
3353 if (task->data_dir == DMA_NONE)
3354 state = SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE;
3355 else if (task->ata_task.dma_xfer)
3356 state = SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE;
3357 else /* PIO */
3358 state = SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE;
3359
3360 sci_base_state_machine_change_state(sm, state);
c72086e3 3361 }
f1f52e75
DW
3362}
3363
f1f52e75
DW
3364/**
3365 * scic_sds_request_completed_state_enter() -
3366 * @object: This parameter specifies the base object for which the state
3367 * transition is occurring. This object is cast into a SCIC_SDS_IO_REQUEST
3368 * object.
3369 *
3370 * This method implements the actions taken when entering the
3371 * SCI_BASE_REQUEST_STATE_COMPLETED state. This state is entered when the
3372 * SCIC_SDS_IO_REQUEST has completed. The method will decode the request
3373 * completion status and convert it to an enum sci_status to return in the
3374 * completion callback function. none
3375 */
3376static void scic_sds_request_completed_state_enter(void *object)
3377{
3378 struct scic_sds_request *sci_req = object;
3379 struct scic_sds_controller *scic =
3380 scic_sds_request_get_controller(sci_req);
3381 struct isci_host *ihost = scic_to_ihost(scic);
3382 struct isci_request *ireq = sci_req_to_ireq(sci_req);
3383
3384 SET_STATE_HANDLER(sci_req,
3385 scic_sds_request_state_handler_table,
3386 SCI_BASE_REQUEST_STATE_COMPLETED);
3387
3388 /* Tell the SCI_USER that the IO request is complete */
3389 if (sci_req->is_task_management_request == false)
3390 isci_request_io_request_complete(ihost, ireq,
3391 sci_req->sci_status);
3392 else
3393 isci_task_request_complete(ihost, ireq, sci_req->sci_status);
3394}
3395
3396/**
3397 * scic_sds_request_aborting_state_enter() -
3398 * @object: This parameter specifies the base object for which the state
3399 * transition is occurring. This object is cast into a SCIC_SDS_IO_REQUEST
3400 * object.
3401 *
3402 * This method implements the actions taken when entering the
3403 * SCI_BASE_REQUEST_STATE_ABORTING state. none
3404 */
3405static void scic_sds_request_aborting_state_enter(void *object)
3406{
3407 struct scic_sds_request *sci_req = object;
3408
3409 /* Setting the abort bit in the Task Context is required by the silicon. */
3410 sci_req->task_context_buffer->abort = 1;
3411
3412 SET_STATE_HANDLER(
3413 sci_req,
3414 scic_sds_request_state_handler_table,
3415 SCI_BASE_REQUEST_STATE_ABORTING
3416 );
3417}
3418
3419/**
3420 * scic_sds_request_final_state_enter() -
3421 * @object: This parameter specifies the base object for which the state
3422 * transition is occurring. This is cast into a SCIC_SDS_IO_REQUEST object.
3423 *
3424 * This method implements the actions taken when entering the
3425 * SCI_BASE_REQUEST_STATE_FINAL state. The only action required is to put the
3426 * state handlers in place. none
3427 */
3428static void scic_sds_request_final_state_enter(void *object)
3429{
3430 struct scic_sds_request *sci_req = object;
3431
3432 SET_STATE_HANDLER(
3433 sci_req,
3434 scic_sds_request_state_handler_table,
3435 SCI_BASE_REQUEST_STATE_FINAL
3436 );
3437}
3438
f139303d
DW
3439static void scic_sds_io_request_started_task_mgmt_await_tc_completion_substate_enter(
3440 void *object)
3441{
3442 struct scic_sds_request *sci_req = object;
3443
3444 SET_STATE_HANDLER(
3445 sci_req,
3446 scic_sds_request_state_handler_table,
3447 SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION
3448 );
3449}
3450
3451static void scic_sds_io_request_started_task_mgmt_await_task_response_substate_enter(
3452 void *object)
3453{
3454 struct scic_sds_request *sci_req = object;
3455
3456 SET_STATE_HANDLER(
3457 sci_req,
3458 scic_sds_request_state_handler_table,
3459 SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE
3460 );
3461}
3462
c72086e3
DW
3463static void scic_sds_smp_request_started_await_response_substate_enter(void *object)
3464{
3465 struct scic_sds_request *sci_req = object;
3466
3467 SET_STATE_HANDLER(
3468 sci_req,
3469 scic_sds_request_state_handler_table,
3470 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE
3471 );
3472}
3473
3474static void scic_sds_smp_request_started_await_tc_completion_substate_enter(void *object)
3475{
3476 struct scic_sds_request *sci_req = object;
3477
3478 SET_STATE_HANDLER(
3479 sci_req,
3480 scic_sds_request_state_handler_table,
3481 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION
3482 );
3483}
3484
5dec6f4e
DW
3485static void scic_sds_stp_request_started_non_data_await_h2d_completion_enter(
3486 void *object)
3487{
3488 struct scic_sds_request *sci_req = object;
3489
3490 SET_STATE_HANDLER(
3491 sci_req,
3492 scic_sds_request_state_handler_table,
3493 SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE
3494 );
3495
3496 scic_sds_remote_device_set_working_request(
3497 sci_req->target_device, sci_req
3498 );
3499}
3500
3501static void scic_sds_stp_request_started_non_data_await_d2h_enter(void *object)
3502{
3503 struct scic_sds_request *sci_req = object;
3504
3505 SET_STATE_HANDLER(
3506 sci_req,
3507 scic_sds_request_state_handler_table,
3508 SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE
3509 );
3510}
3511
3512
3513
3514static void scic_sds_stp_request_started_pio_await_h2d_completion_enter(
3515 void *object)
3516{
3517 struct scic_sds_request *sci_req = object;
3518
3519 SET_STATE_HANDLER(
3520 sci_req,
3521 scic_sds_request_state_handler_table,
3522 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE
3523 );
3524
3525 scic_sds_remote_device_set_working_request(
3526 sci_req->target_device, sci_req);
3527}
3528
3529static void scic_sds_stp_request_started_pio_await_frame_enter(void *object)
3530{
3531 struct scic_sds_request *sci_req = object;
3532
3533 SET_STATE_HANDLER(
3534 sci_req,
3535 scic_sds_request_state_handler_table,
3536 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE
3537 );
3538}
3539
3540static void scic_sds_stp_request_started_pio_data_in_await_data_enter(
3541 void *object)
3542{
3543 struct scic_sds_request *sci_req = object;
3544
3545 SET_STATE_HANDLER(
3546 sci_req,
3547 scic_sds_request_state_handler_table,
3548 SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE
3549 );
3550}
3551
3552static void scic_sds_stp_request_started_pio_data_out_transmit_data_enter(
3553 void *object)
3554{
3555 struct scic_sds_request *sci_req = object;
3556
3557 SET_STATE_HANDLER(
3558 sci_req,
3559 scic_sds_request_state_handler_table,
3560 SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE
3561 );
3562}
3563
3564
3565
3566static void scic_sds_stp_request_started_udma_await_tc_completion_enter(
3567 void *object)
3568{
3569 struct scic_sds_request *sci_req = object;
3570
3571 SET_STATE_HANDLER(
3572 sci_req,
3573 scic_sds_request_state_handler_table,
3574 SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE
3575 );
3576}
3577
3578/**
3579 *
3580 *
3581 * This state is entered when there is an TC completion failure. The hardware
3582 * received an unexpected condition while processing the IO request and now
3583 * will UF the D2H register FIS to complete the IO.
3584 */
3585static void scic_sds_stp_request_started_udma_await_d2h_reg_fis_enter(
3586 void *object)
3587{
3588 struct scic_sds_request *sci_req = object;
3589
3590 SET_STATE_HANDLER(
3591 sci_req,
3592 scic_sds_request_state_handler_table,
3593 SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE
3594 );
3595}
3596
3597
3598
3599static void scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter(
3600 void *object)
3601{
3602 struct scic_sds_request *sci_req = object;
3603
3604 SET_STATE_HANDLER(
3605 sci_req,
3606 scic_sds_request_state_handler_table,
3607 SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE
3608 );
3609
3610 scic_sds_remote_device_set_working_request(
3611 sci_req->target_device, sci_req
3612 );
3613}
3614
3615static void scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter(
3616 void *object)
3617{
3618 struct scic_sds_request *sci_req = object;
3619 struct scu_task_context *task_context;
3620 struct host_to_dev_fis *h2d_fis;
3621 enum sci_status status;
3622
3623 /* Clear the SRST bit */
3624 h2d_fis = &sci_req->stp.cmd;
3625 h2d_fis->control = 0;
3626
3627 /* Clear the TC control bit */
3628 task_context = scic_sds_controller_get_task_context_buffer(
3629 sci_req->owning_controller, sci_req->io_tag);
3630 task_context->control_frame = 0;
3631
3632 status = scic_controller_continue_io(sci_req);
3633 if (status == SCI_SUCCESS) {
3634 SET_STATE_HANDLER(
3635 sci_req,
3636 scic_sds_request_state_handler_table,
3637 SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE
3638 );
3639 }
3640}
3641
3642static void scic_sds_stp_request_started_soft_reset_await_d2h_response_enter(
3643 void *object)
3644{
3645 struct scic_sds_request *sci_req = object;
3646
3647 SET_STATE_HANDLER(
3648 sci_req,
3649 scic_sds_request_state_handler_table,
3650 SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE
3651 );
3652}
3653
f1f52e75
DW
3654static const struct sci_base_state scic_sds_request_state_table[] = {
3655 [SCI_BASE_REQUEST_STATE_INITIAL] = {
3656 .enter_state = scic_sds_request_initial_state_enter,
3657 },
3658 [SCI_BASE_REQUEST_STATE_CONSTRUCTED] = {
3659 .enter_state = scic_sds_request_constructed_state_enter,
3660 },
3661 [SCI_BASE_REQUEST_STATE_STARTED] = {
3662 .enter_state = scic_sds_request_started_state_enter,
5dec6f4e
DW
3663 },
3664 [SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE] = {
3665 .enter_state = scic_sds_stp_request_started_non_data_await_h2d_completion_enter,
3666 },
3667 [SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE] = {
3668 .enter_state = scic_sds_stp_request_started_non_data_await_d2h_enter,
3669 },
3670 [SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE] = {
3671 .enter_state = scic_sds_stp_request_started_pio_await_h2d_completion_enter,
3672 },
3673 [SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE] = {
3674 .enter_state = scic_sds_stp_request_started_pio_await_frame_enter,
3675 },
3676 [SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE] = {
3677 .enter_state = scic_sds_stp_request_started_pio_data_in_await_data_enter,
3678 },
3679 [SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE] = {
3680 .enter_state = scic_sds_stp_request_started_pio_data_out_transmit_data_enter,
3681 },
3682 [SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE] = {
3683 .enter_state = scic_sds_stp_request_started_udma_await_tc_completion_enter,
3684 },
3685 [SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE] = {
3686 .enter_state = scic_sds_stp_request_started_udma_await_d2h_reg_fis_enter,
3687 },
3688 [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE] = {
3689 .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter,
3690 },
3691 [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE] = {
3692 .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter,
3693 },
3694 [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE] = {
3695 .enter_state = scic_sds_stp_request_started_soft_reset_await_d2h_response_enter,
f1f52e75 3696 },
f139303d
DW
3697 [SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION] = {
3698 .enter_state = scic_sds_io_request_started_task_mgmt_await_tc_completion_substate_enter,
3699 },
3700 [SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE] = {
3701 .enter_state = scic_sds_io_request_started_task_mgmt_await_task_response_substate_enter,
3702 },
c72086e3
DW
3703 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE] = {
3704 .enter_state = scic_sds_smp_request_started_await_response_substate_enter,
3705 },
3706 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION] = {
3707 .enter_state = scic_sds_smp_request_started_await_tc_completion_substate_enter,
3708 },
f1f52e75
DW
3709 [SCI_BASE_REQUEST_STATE_COMPLETED] = {
3710 .enter_state = scic_sds_request_completed_state_enter,
3711 },
3712 [SCI_BASE_REQUEST_STATE_ABORTING] = {
3713 .enter_state = scic_sds_request_aborting_state_enter,
3714 },
3715 [SCI_BASE_REQUEST_STATE_FINAL] = {
3716 .enter_state = scic_sds_request_final_state_enter,
3717 },
3718};
3719
3720static void scic_sds_general_request_construct(struct scic_sds_controller *scic,
3721 struct scic_sds_remote_device *sci_dev,
3722 u16 io_tag, struct scic_sds_request *sci_req)
3723{
3724 sci_base_state_machine_construct(&sci_req->state_machine, sci_req,
3725 scic_sds_request_state_table, SCI_BASE_REQUEST_STATE_INITIAL);
3726 sci_base_state_machine_start(&sci_req->state_machine);
3727
3728 sci_req->io_tag = io_tag;
3729 sci_req->owning_controller = scic;
3730 sci_req->target_device = sci_dev;
f1f52e75
DW
3731 sci_req->protocol = SCIC_NO_PROTOCOL;
3732 sci_req->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
3733 sci_req->device_sequence = scic_sds_remote_device_get_sequence(sci_dev);
3734
3735 sci_req->sci_status = SCI_SUCCESS;
3736 sci_req->scu_status = 0;
3737 sci_req->post_context = 0xFFFFFFFF;
3738
3739 sci_req->is_task_management_request = false;
3740
3741 if (io_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
3742 sci_req->was_tag_assigned_by_user = false;
c72086e3 3743 sci_req->task_context_buffer = &sci_req->tc;
f1f52e75
DW
3744 } else {
3745 sci_req->was_tag_assigned_by_user = true;
3746
3747 sci_req->task_context_buffer =
3748 scic_sds_controller_get_task_context_buffer(scic, io_tag);
3749 }
3750}
3751
3752static enum sci_status
3753scic_io_request_construct(struct scic_sds_controller *scic,
3754 struct scic_sds_remote_device *sci_dev,
3755 u16 io_tag, struct scic_sds_request *sci_req)
3756{
3757 struct domain_device *dev = sci_dev_to_domain(sci_dev);
3758 enum sci_status status = SCI_SUCCESS;
3759
3760 /* Build the common part of the request */
3761 scic_sds_general_request_construct(scic, sci_dev, io_tag, sci_req);
3762
c72086e3 3763 if (sci_dev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
f1f52e75
DW
3764 return SCI_FAILURE_INVALID_REMOTE_DEVICE;
3765
3766 if (dev->dev_type == SAS_END_DEV)
c72086e3
DW
3767 /* pass */;
3768 else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
f1f52e75 3769 memset(&sci_req->stp.cmd, 0, sizeof(sci_req->stp.cmd));
c72086e3 3770 else if (dev_is_expander(dev))
f1f52e75 3771 memset(&sci_req->smp.cmd, 0, sizeof(sci_req->smp.cmd));
c72086e3
DW
3772 else
3773 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
f1f52e75 3774
c72086e3
DW
3775 memset(sci_req->task_context_buffer, 0,
3776 offsetof(struct scu_task_context, sgl_pair_ab));
f1f52e75
DW
3777
3778 return status;
3779}
3780
3781enum sci_status scic_task_request_construct(struct scic_sds_controller *scic,
3782 struct scic_sds_remote_device *sci_dev,
3783 u16 io_tag, struct scic_sds_request *sci_req)
3784{
3785 struct domain_device *dev = sci_dev_to_domain(sci_dev);
3786 enum sci_status status = SCI_SUCCESS;
3787
3788 /* Build the common part of the request */
3789 scic_sds_general_request_construct(scic, sci_dev, io_tag, sci_req);
3790
c72086e3
DW
3791 if (dev->dev_type == SAS_END_DEV ||
3792 dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
f1f52e75
DW
3793 sci_req->is_task_management_request = true;
3794 memset(sci_req->task_context_buffer, 0, sizeof(struct scu_task_context));
c72086e3
DW
3795 } else
3796 status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
f1f52e75
DW
3797
3798 return status;
3799}
3800
3801static enum sci_status isci_request_ssp_request_construct(
3802 struct isci_request *request)
3803{
3804 enum sci_status status;
3805
3806 dev_dbg(&request->isci_host->pdev->dev,
3807 "%s: request = %p\n",
3808 __func__,
3809 request);
3810 status = scic_io_request_construct_basic_ssp(&request->sci);
3811 return status;
3812}
3813
3814static enum sci_status isci_request_stp_request_construct(
3815 struct isci_request *request)
3816{
3817 struct sas_task *task = isci_request_access_task(request);
3818 enum sci_status status;
3819 struct host_to_dev_fis *register_fis;
3820
3821 dev_dbg(&request->isci_host->pdev->dev,
3822 "%s: request = %p\n",
3823 __func__,
3824 request);
3825
3826 /* Get the host_to_dev_fis from the core and copy
3827 * the fis from the task into it.
3828 */
3829 register_fis = isci_sata_task_to_fis_copy(task);
3830
3831 status = scic_io_request_construct_basic_sata(&request->sci);
3832
3833 /* Set the ncq tag in the fis, from the queue
3834 * command in the task.
3835 */
3836 if (isci_sata_is_task_ncq(task)) {
3837
3838 isci_sata_set_ncq_tag(
3839 register_fis,
3840 task
3841 );
3842 }
3843
3844 return status;
3845}
3846
c72086e3
DW
3847/*
3848 * This function will fill in the SCU Task Context for a SMP request. The
3849 * following important settings are utilized: -# task_type ==
3850 * SCU_TASK_TYPE_SMP. This simply indicates that a normal request type
3851 * (i.e. non-raw frame) is being utilized to perform task management. -#
3852 * control_frame == 1. This ensures that the proper endianess is set so
3853 * that the bytes are transmitted in the right order for a smp request frame.
3854 * @sci_req: This parameter specifies the smp request object being
3855 * constructed.
3856 *
3857 */
3858static void
3859scu_smp_request_construct_task_context(struct scic_sds_request *sci_req,
3860 struct smp_req *smp_req)
3861{
3862 dma_addr_t dma_addr;
3863 struct scic_sds_controller *scic;
3864 struct scic_sds_remote_device *sci_dev;
3865 struct scic_sds_port *sci_port;
3866 struct scu_task_context *task_context;
3867 ssize_t word_cnt = sizeof(struct smp_req) / sizeof(u32);
3868
3869 /* byte swap the smp request. */
3870 sci_swab32_cpy(&sci_req->smp.cmd, smp_req,
3871 word_cnt);
3872
3873 task_context = scic_sds_request_get_task_context(sci_req);
3874
3875 scic = scic_sds_request_get_controller(sci_req);
3876 sci_dev = scic_sds_request_get_device(sci_req);
3877 sci_port = scic_sds_request_get_port(sci_req);
3878
3879 /*
3880 * Fill in the TC with the its required data
3881 * 00h
3882 */
3883 task_context->priority = 0;
3884 task_context->initiator_request = 1;
3885 task_context->connection_rate = sci_dev->connection_rate;
3886 task_context->protocol_engine_index =
3887 scic_sds_controller_get_protocol_engine_group(scic);
3888 task_context->logical_port_index = scic_sds_port_get_index(sci_port);
3889 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
3890 task_context->abort = 0;
3891 task_context->valid = SCU_TASK_CONTEXT_VALID;
3892 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
3893
3894 /* 04h */
3895 task_context->remote_node_index = sci_dev->rnc.remote_node_index;
3896 task_context->command_code = 0;
3897 task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;
3898
3899 /* 08h */
3900 task_context->link_layer_control = 0;
3901 task_context->do_not_dma_ssp_good_response = 1;
3902 task_context->strict_ordering = 0;
3903 task_context->control_frame = 1;
3904 task_context->timeout_enable = 0;
3905 task_context->block_guard_enable = 0;
3906
3907 /* 0ch */
3908 task_context->address_modifier = 0;
3909
3910 /* 10h */
3911 task_context->ssp_command_iu_length = smp_req->req_len;
3912
3913 /* 14h */
3914 task_context->transfer_length_bytes = 0;
3915
3916 /*
3917 * 18h ~ 30h, protocol specific
3918 * since commandIU has been build by framework at this point, we just
3919 * copy the frist DWord from command IU to this location. */
3920 memcpy(&task_context->type.smp, &sci_req->smp.cmd, sizeof(u32));
3921
3922 /*
3923 * 40h
3924 * "For SMP you could program it to zero. We would prefer that way
3925 * so that done code will be consistent." - Venki
3926 */
3927 task_context->task_phase = 0;
3928
3929 if (sci_req->was_tag_assigned_by_user) {
3930 /*
3931 * Build the task context now since we have already read
3932 * the data
3933 */
3934 sci_req->post_context =
3935 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
3936 (scic_sds_controller_get_protocol_engine_group(scic) <<
3937 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
3938 (scic_sds_port_get_index(sci_port) <<
3939 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
3940 scic_sds_io_tag_get_index(sci_req->io_tag));
3941 } else {
3942 /*
3943 * Build the task context now since we have already read
3944 * the data.
3945 * I/O tag index is not assigned because we have to wait
3946 * until we get a TCi.
3947 */
3948 sci_req->post_context =
3949 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
3950 (scic_sds_controller_get_protocol_engine_group(scic) <<
3951 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
3952 (scic_sds_port_get_index(sci_port) <<
3953 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
3954 }
3955
3956 /*
3957 * Copy the physical address for the command buffer to the SCU Task
3958 * Context command buffer should not contain command header.
3959 */
3960 dma_addr = scic_io_request_get_dma_addr(sci_req,
3961 ((char *) &sci_req->smp.cmd) +
3962 sizeof(u32));
3963
3964 task_context->command_iu_upper = upper_32_bits(dma_addr);
3965 task_context->command_iu_lower = lower_32_bits(dma_addr);
3966
3967 /* SMP response comes as UF, so no need to set response IU address. */
3968 task_context->response_iu_upper = 0;
3969 task_context->response_iu_lower = 0;
3970}
3971
3972static enum sci_status scic_io_request_construct_smp(struct scic_sds_request *sci_req)
3973{
3974 struct smp_req *smp_req = kmalloc(sizeof(*smp_req), GFP_KERNEL);
3975
3976 if (!smp_req)
3977 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
3978
3979 sci_req->protocol = SCIC_SMP_PROTOCOL;
3980
3981 /* Construct the SMP SCU Task Context */
3982 memcpy(smp_req, &sci_req->smp.cmd, sizeof(*smp_req));
3983
3984 /*
3985 * Look at the SMP requests' header fields; for certain SAS 1.x SMP
3986 * functions under SAS 2.0, a zero request length really indicates
3987 * a non-zero default length. */
3988 if (smp_req->req_len == 0) {
3989 switch (smp_req->func) {
3990 case SMP_DISCOVER:
3991 case SMP_REPORT_PHY_ERR_LOG:
3992 case SMP_REPORT_PHY_SATA:
3993 case SMP_REPORT_ROUTE_INFO:
3994 smp_req->req_len = 2;
3995 break;
3996 case SMP_CONF_ROUTE_INFO:
3997 case SMP_PHY_CONTROL:
3998 case SMP_PHY_TEST_FUNCTION:
3999 smp_req->req_len = 9;
4000 break;
4001 /* Default - zero is a valid default for 2.0. */
4002 }
4003 }
4004
4005 scu_smp_request_construct_task_context(sci_req, smp_req);
4006
4007 sci_base_state_machine_change_state(&sci_req->state_machine,
4008 SCI_BASE_REQUEST_STATE_CONSTRUCTED);
4009
4010 kfree(smp_req);
4011
4012 return SCI_SUCCESS;
4013}
4014
f1f52e75
DW
4015/*
4016 * isci_smp_request_build() - This function builds the smp request.
4017 * @ireq: This parameter points to the isci_request allocated in the
4018 * request construct function.
4019 *
4020 * SCI_SUCCESS on successfull completion, or specific failure code.
4021 */
4022static enum sci_status isci_smp_request_build(struct isci_request *ireq)
4023{
4024 enum sci_status status = SCI_FAILURE;
4025 struct sas_task *task = isci_request_access_task(ireq);
4026 struct scic_sds_request *sci_req = &ireq->sci;
4027
4028 dev_dbg(&ireq->isci_host->pdev->dev,
4029 "%s: request = %p\n", __func__, ireq);
4030
4031 dev_dbg(&ireq->isci_host->pdev->dev,
4032 "%s: smp_req len = %d\n",
4033 __func__,
4034 task->smp_task.smp_req.length);
4035
4036 /* copy the smp_command to the address; */
4037 sg_copy_to_buffer(&task->smp_task.smp_req, 1,
4038 &sci_req->smp.cmd,
4039 sizeof(struct smp_req));
4040
4041 status = scic_io_request_construct_smp(sci_req);
4042 if (status != SCI_SUCCESS)
4043 dev_warn(&ireq->isci_host->pdev->dev,
4044 "%s: failed with status = %d\n",
4045 __func__,
4046 status);
4047
4048 return status;
4049}
4050
4051/**
4052 * isci_io_request_build() - This function builds the io request object.
4053 * @isci_host: This parameter specifies the ISCI host object
4054 * @request: This parameter points to the isci_request object allocated in the
4055 * request construct function.
4056 * @sci_device: This parameter is the handle for the sci core's remote device
4057 * object that is the destination for this request.
4058 *
4059 * SCI_SUCCESS on successfull completion, or specific failure code.
4060 */
4061static enum sci_status isci_io_request_build(
4062 struct isci_host *isci_host,
4063 struct isci_request *request,
4064 struct isci_remote_device *isci_device)
4065{
4066 enum sci_status status = SCI_SUCCESS;
4067 struct sas_task *task = isci_request_access_task(request);
4068 struct scic_sds_remote_device *sci_device = &isci_device->sci;
4069
4070 dev_dbg(&isci_host->pdev->dev,
4071 "%s: isci_device = 0x%p; request = %p, "
4072 "num_scatter = %d\n",
4073 __func__,
4074 isci_device,
4075 request,
4076 task->num_scatter);
4077
4078 /* map the sgl addresses, if present.
4079 * libata does the mapping for sata devices
4080 * before we get the request.
4081 */
4082 if (task->num_scatter &&
4083 !sas_protocol_ata(task->task_proto) &&
4084 !(SAS_PROTOCOL_SMP & task->task_proto)) {
4085
4086 request->num_sg_entries = dma_map_sg(
4087 &isci_host->pdev->dev,
4088 task->scatter,
4089 task->num_scatter,
4090 task->data_dir
4091 );
4092
4093 if (request->num_sg_entries == 0)
4094 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
4095 }
4096
4097 /* build the common request object. For now,
4098 * we will let the core allocate the IO tag.
4099 */
4100 status = scic_io_request_construct(&isci_host->sci, sci_device,
4101 SCI_CONTROLLER_INVALID_IO_TAG,
4102 &request->sci);
4103
4104 if (status != SCI_SUCCESS) {
4105 dev_warn(&isci_host->pdev->dev,
4106 "%s: failed request construct\n",
4107 __func__);
4108 return SCI_FAILURE;
4109 }
4110
4111 switch (task->task_proto) {
4112 case SAS_PROTOCOL_SMP:
4113 status = isci_smp_request_build(request);
4114 break;
4115 case SAS_PROTOCOL_SSP:
4116 status = isci_request_ssp_request_construct(request);
4117 break;
4118 case SAS_PROTOCOL_SATA:
4119 case SAS_PROTOCOL_STP:
4120 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
4121 status = isci_request_stp_request_construct(request);
4122 break;
4123 default:
4124 dev_warn(&isci_host->pdev->dev,
4125 "%s: unknown protocol\n", __func__);
4126 return SCI_FAILURE;
4127 }
4128
4129 return SCI_SUCCESS;
4130}
4131
4132/**
4133 * isci_request_alloc_core() - This function gets the request object from the
4134 * isci_host dma cache.
4135 * @isci_host: This parameter specifies the ISCI host object
4136 * @isci_request: This parameter will contain the pointer to the new
4137 * isci_request object.
4138 * @isci_device: This parameter is the pointer to the isci remote device object
4139 * that is the destination for this request.
4140 * @gfp_flags: This parameter specifies the os allocation flags.
4141 *
4142 * SCI_SUCCESS on successfull completion, or specific failure code.
4143 */
4144static int isci_request_alloc_core(
4145 struct isci_host *isci_host,
4146 struct isci_request **isci_request,
4147 struct isci_remote_device *isci_device,
4148 gfp_t gfp_flags)
4149{
4150 int ret = 0;
4151 dma_addr_t handle;
4152 struct isci_request *request;
4153
aa145102 4154
f1f52e75
DW
4155 /* get pointer to dma memory. This actually points
4156 * to both the isci_remote_device object and the
4157 * sci object. The isci object is at the beginning
4158 * of the memory allocated here.
4159 */
4160 request = dma_pool_alloc(isci_host->dma_pool, gfp_flags, &handle);
4161 if (!request) {
4162 dev_warn(&isci_host->pdev->dev,
4163 "%s: dma_pool_alloc returned NULL\n", __func__);
4164 return -ENOMEM;
4165 }
6f231dda 4166
f1f52e75
DW
4167 /* initialize the request object. */
4168 spin_lock_init(&request->state_lock);
4169 request->request_daddr = handle;
4170 request->isci_host = isci_host;
4171 request->isci_device = isci_device;
4172 request->io_request_completion = NULL;
4173 request->terminated = false;
6f231dda 4174
f1f52e75 4175 request->num_sg_entries = 0;
6f231dda 4176
f1f52e75 4177 request->complete_in_target = false;
6f231dda 4178
f1f52e75
DW
4179 INIT_LIST_HEAD(&request->completed_node);
4180 INIT_LIST_HEAD(&request->dev_node);
4181
4182 *isci_request = request;
4183 isci_request_change_state(request, allocated);
4184
4185 return ret;
4186}
4187
4188static int isci_request_alloc_io(
4189 struct isci_host *isci_host,
4190 struct sas_task *task,
4191 struct isci_request **isci_request,
4192 struct isci_remote_device *isci_device,
4193 gfp_t gfp_flags)
4194{
4195 int retval = isci_request_alloc_core(isci_host, isci_request,
4196 isci_device, gfp_flags);
4197
4198 if (!retval) {
4199 (*isci_request)->ttype_ptr.io_task_ptr = task;
4200 (*isci_request)->ttype = io_task;
4201
4202 task->lldd_task = *isci_request;
6f231dda 4203 }
f1f52e75
DW
4204 return retval;
4205}
6f231dda 4206
f1f52e75
DW
4207/**
4208 * isci_request_alloc_tmf() - This function gets the request object from the
4209 * isci_host dma cache and initializes the relevant fields as a sas_task.
4210 * @isci_host: This parameter specifies the ISCI host object
4211 * @sas_task: This parameter is the task struct from the upper layer driver.
4212 * @isci_request: This parameter will contain the pointer to the new
4213 * isci_request object.
4214 * @isci_device: This parameter is the pointer to the isci remote device object
4215 * that is the destination for this request.
4216 * @gfp_flags: This parameter specifies the os allocation flags.
4217 *
4218 * SCI_SUCCESS on successfull completion, or specific failure code.
4219 */
4220int isci_request_alloc_tmf(
4221 struct isci_host *isci_host,
4222 struct isci_tmf *isci_tmf,
4223 struct isci_request **isci_request,
4224 struct isci_remote_device *isci_device,
4225 gfp_t gfp_flags)
4226{
4227 int retval = isci_request_alloc_core(isci_host, isci_request,
4228 isci_device, gfp_flags);
6f231dda 4229
f1f52e75 4230 if (!retval) {
6f231dda 4231
f1f52e75
DW
4232 (*isci_request)->ttype_ptr.tmf_task_ptr = isci_tmf;
4233 (*isci_request)->ttype = tmf_task;
4234 }
4235 return retval;
4236}
4237
4238/**
4239 * isci_request_execute() - This function allocates the isci_request object,
4240 * all fills in some common fields.
4241 * @isci_host: This parameter specifies the ISCI host object
4242 * @sas_task: This parameter is the task struct from the upper layer driver.
4243 * @isci_request: This parameter will contain the pointer to the new
4244 * isci_request object.
4245 * @gfp_flags: This parameter specifies the os allocation flags.
4246 *
4247 * SCI_SUCCESS on successfull completion, or specific failure code.
4248 */
4249int isci_request_execute(
4250 struct isci_host *isci_host,
4251 struct sas_task *task,
4252 struct isci_request **isci_request,
4253 gfp_t gfp_flags)
4254{
4255 int ret = 0;
4256 struct scic_sds_remote_device *sci_device;
4257 enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
4258 struct isci_remote_device *isci_device;
4259 struct isci_request *request;
4260 unsigned long flags;
4261
4262 isci_device = task->dev->lldd_dev;
4263 sci_device = &isci_device->sci;
4264
4265 /* do common allocation and init of request object. */
4266 ret = isci_request_alloc_io(
4267 isci_host,
4268 task,
4269 &request,
4270 isci_device,
4271 gfp_flags
4272 );
4273
4274 if (ret)
4275 goto out;
4276
4277 status = isci_io_request_build(isci_host, request, isci_device);
4278 if (status != SCI_SUCCESS) {
4279 dev_warn(&isci_host->pdev->dev,
4280 "%s: request_construct failed - status = 0x%x\n",
4281 __func__,
4282 status);
4283 goto out;
4284 }
4285
4286 spin_lock_irqsave(&isci_host->scic_lock, flags);
4287
4288 /* send the request, let the core assign the IO TAG. */
4289 status = scic_controller_start_io(&isci_host->sci, sci_device,
4290 &request->sci,
4291 SCI_CONTROLLER_INVALID_IO_TAG);
4292 if (status != SCI_SUCCESS &&
4293 status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
4294 dev_warn(&isci_host->pdev->dev,
4295 "%s: failed request start (0x%x)\n",
4296 __func__, status);
4297 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
4298 goto out;
4299 }
4300
4301 /* Either I/O started OK, or the core has signaled that
4302 * the device needs a target reset.
4303 *
4304 * In either case, hold onto the I/O for later.
4305 *
4306 * Update it's status and add it to the list in the
4307 * remote device object.
6f231dda 4308 */
f1f52e75
DW
4309 isci_request_change_state(request, started);
4310 list_add(&request->dev_node, &isci_device->reqs_in_process);
6f231dda 4311
f1f52e75
DW
4312 if (status == SCI_SUCCESS) {
4313 /* Save the tag for possible task mgmt later. */
4314 request->io_tag = request->sci.io_tag;
4315 } else {
4316 /* The request did not really start in the
4317 * hardware, so clear the request handle
4318 * here so no terminations will be done.
4319 */
4320 request->terminated = true;
4321 }
4322 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
4323
4324 if (status ==
4325 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
4326 /* Signal libsas that we need the SCSI error
4327 * handler thread to work on this I/O and that
4328 * we want a device reset.
4329 */
4330 spin_lock_irqsave(&task->task_state_lock, flags);
4331 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
4332 spin_unlock_irqrestore(&task->task_state_lock, flags);
4333
4334 /* Cause this task to be scheduled in the SCSI error
4335 * handler thread.
4336 */
4337 isci_execpath_callback(isci_host, task,
4338 sas_task_abort);
4339
4340 /* Change the status, since we are holding
4341 * the I/O until it is managed by the SCSI
4342 * error handler.
4343 */
4344 status = SCI_SUCCESS;
4345 }
4346
4347 out:
4348 if (status != SCI_SUCCESS) {
4349 /* release dma memory on failure. */
4350 isci_request_free(isci_host, request);
4351 request = NULL;
4352 ret = SCI_FAILURE;
4353 }
4354
4355 *isci_request = request;
4356 return ret;
6f231dda 4357}