isci: unify constants
[pandora-kernel.git] / drivers / scsi / isci / core / scic_sds_stp_request.c
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 <scsi/sas.h>
57 #include "sas.h"
58 #include "sci_base_state.h"
59 #include "sci_base_state_machine.h"
60 #include "scic_io_request.h"
61 #include "scic_sds_controller.h"
62 #include "remote_device.h"
63 #include "scic_sds_request.h"
64 #include "scic_sds_stp_pio_request.h"
65 #include "scic_sds_stp_request.h"
66 #include "scic_sds_unsolicited_frame_control.h"
67 #include "sci_environment.h"
68 #include "sci_util.h"
69 #include "scu_completion_codes.h"
70 #include "scu_event_codes.h"
71 #include "scu_task_context.h"
72 #include "request.h"
73
74 void scic_sds_stp_request_assign_buffers(struct scic_sds_request *sci_req)
75 {
76         if (sci_req->was_tag_assigned_by_user == false)
77                 sci_req->task_context_buffer = &sci_req->tc;
78 }
79
80 /**
81  * This method is will fill in the SCU Task Context for any type of SATA
82  *    request.  This is called from the various SATA constructors.
83  * @sci_req: The general IO request object which is to be used in
84  *    constructing the SCU task context.
85  * @task_context: The buffer pointer for the SCU task context which is being
86  *    constructed.
87  *
88  * The general io request construction is complete. The buffer assignment for
89  * the command buffer is complete. none Revisit task context construction to
90  * determine what is common for SSP/SMP/STP task context structures.
91  */
92 static void scu_sata_reqeust_construct_task_context(
93         struct scic_sds_request *sci_req,
94         struct scu_task_context *task_context)
95 {
96         dma_addr_t dma_addr;
97         struct scic_sds_controller *controller;
98         struct scic_sds_remote_device *target_device;
99         struct scic_sds_port *target_port;
100
101         controller = scic_sds_request_get_controller(sci_req);
102         target_device = scic_sds_request_get_device(sci_req);
103         target_port = scic_sds_request_get_port(sci_req);
104
105         /* Fill in the TC with the its required data */
106         task_context->abort = 0;
107         task_context->priority = SCU_TASK_PRIORITY_NORMAL;
108         task_context->initiator_request = 1;
109         task_context->connection_rate = target_device->connection_rate;
110         task_context->protocol_engine_index =
111                 scic_sds_controller_get_protocol_engine_group(controller);
112         task_context->logical_port_index =
113                 scic_sds_port_get_index(target_port);
114         task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
115         task_context->valid = SCU_TASK_CONTEXT_VALID;
116         task_context->context_type = SCU_TASK_CONTEXT_TYPE;
117
118         task_context->remote_node_index =
119                 scic_sds_remote_device_get_index(sci_req->target_device);
120         task_context->command_code = 0;
121
122         task_context->link_layer_control = 0;
123         task_context->do_not_dma_ssp_good_response = 1;
124         task_context->strict_ordering = 0;
125         task_context->control_frame = 0;
126         task_context->timeout_enable = 0;
127         task_context->block_guard_enable = 0;
128
129         task_context->address_modifier = 0;
130         task_context->task_phase = 0x01;
131
132         task_context->ssp_command_iu_length =
133                 (sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);
134
135         /* Set the first word of the H2D REG FIS */
136         task_context->type.words[0] = *(u32 *)&sci_req->stp.cmd;
137
138         if (sci_req->was_tag_assigned_by_user) {
139                 /*
140                  * Build the task context now since we have already read
141                  * the data
142                  */
143                 sci_req->post_context =
144                         (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
145                          (scic_sds_controller_get_protocol_engine_group(
146                                                         controller) <<
147                           SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
148                          (scic_sds_port_get_index(target_port) <<
149                           SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
150                          scic_sds_io_tag_get_index(sci_req->io_tag));
151         } else {
152                 /*
153                  * Build the task context now since we have already read
154                  * the data.
155                  * I/O tag index is not assigned because we have to wait
156                  * until we get a TCi.
157                  */
158                 sci_req->post_context =
159                         (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
160                          (scic_sds_controller_get_protocol_engine_group(
161                                                         controller) <<
162                           SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
163                          (scic_sds_port_get_index(target_port) <<
164                           SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
165         }
166
167         /*
168          * Copy the physical address for the command buffer to the SCU Task
169          * Context. We must offset the command buffer by 4 bytes because the
170          * first 4 bytes are transfered in the body of the TC.
171          */
172         dma_addr = scic_io_request_get_dma_addr(sci_req,
173                                                 ((char *) &sci_req->stp.cmd) +
174                                                 sizeof(u32));
175
176         task_context->command_iu_upper = upper_32_bits(dma_addr);
177         task_context->command_iu_lower = lower_32_bits(dma_addr);
178
179         /* SATA Requests do not have a response buffer */
180         task_context->response_iu_upper = 0;
181         task_context->response_iu_lower = 0;
182 }
183
184 /**
185  *
186  * @sci_req:
187  *
188  * This method will perform any general sata request construction. What part of
189  * SATA IO request construction is general? none
190  */
191 static void scic_sds_stp_non_ncq_request_construct(
192         struct scic_sds_request *sci_req)
193 {
194         sci_req->has_started_substate_machine = true;
195 }
196
197 /**
198  *
199  * @sci_req: This parameter specifies the request to be constructed as an
200  *    optimized request.
201  * @optimized_task_type: This parameter specifies whether the request is to be
202  *    an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
203  *    value of 1 indicates NCQ.
204  *
205  * This method will perform request construction common to all types of STP
206  * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
207  * returns an indication as to whether the construction was successful.
208  */
209 static void scic_sds_stp_optimized_request_construct(struct scic_sds_request *sci_req,
210                                                      u8 optimized_task_type,
211                                                      u32 len,
212                                                      enum dma_data_direction dir)
213 {
214         struct scu_task_context *task_context = sci_req->task_context_buffer;
215
216         /* Build the STP task context structure */
217         scu_sata_reqeust_construct_task_context(sci_req, task_context);
218
219         /* Copy over the SGL elements */
220         scic_sds_request_build_sgl(sci_req);
221
222         /* Copy over the number of bytes to be transfered */
223         task_context->transfer_length_bytes = len;
224
225         if (dir == DMA_TO_DEVICE) {
226                 /*
227                  * The difference between the DMA IN and DMA OUT request task type
228                  * values are consistent with the difference between FPDMA READ
229                  * and FPDMA WRITE values.  Add the supplied task type parameter
230                  * to this difference to set the task type properly for this
231                  * DATA OUT (WRITE) case. */
232                 task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
233                                                                  - SCU_TASK_TYPE_DMA_IN);
234         } else {
235                 /*
236                  * For the DATA IN (READ) case, simply save the supplied
237                  * optimized task type. */
238                 task_context->task_type = optimized_task_type;
239         }
240 }
241
242 /**
243  *
244  * @sci_req: This parameter specifies the request to be constructed.
245  *
246  * This method will construct the STP UDMA request and its associated TC data.
247  * This method returns an indication as to whether the construction was
248  * successful. SCI_SUCCESS Currently this method always returns this value.
249  */
250 enum sci_status scic_sds_stp_ncq_request_construct(struct scic_sds_request *sci_req,
251                                                    u32 len,
252                                                    enum dma_data_direction dir)
253 {
254         scic_sds_stp_optimized_request_construct(sci_req,
255                                                  SCU_TASK_TYPE_FPDMAQ_READ,
256                                                  len, dir);
257         return SCI_SUCCESS;
258 }
259
260 /**
261  * scu_stp_raw_request_construct_task_context -
262  * @sci_req: This parameter specifies the STP request object for which to
263  *    construct a RAW command frame task context.
264  * @task_context: This parameter specifies the SCU specific task context buffer
265  *    to construct.
266  *
267  * This method performs the operations common to all SATA/STP requests
268  * utilizing the raw frame method. none
269  */
270 static void scu_stp_raw_request_construct_task_context(
271         struct scic_sds_stp_request *stp_req,
272         struct scu_task_context *task_context)
273 {
274         struct scic_sds_request *sci_req = to_sci_req(stp_req);
275
276         scu_sata_reqeust_construct_task_context(sci_req, task_context);
277
278         task_context->control_frame         = 0;
279         task_context->priority              = SCU_TASK_PRIORITY_NORMAL;
280         task_context->task_type             = SCU_TASK_TYPE_SATA_RAW_FRAME;
281         task_context->type.stp.fis_type     = FIS_REGH2D;
282         task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
283 }
284
285 void scic_stp_io_request_set_ncq_tag(
286         struct scic_sds_request *req,
287         u16 ncq_tag)
288 {
289         /**
290          * @note This could be made to return an error to the user if the user
291          *       attempts to set the NCQ tag in the wrong state.
292          */
293         req->task_context_buffer->type.stp.ncq_tag = ncq_tag;
294 }
295
296 /**
297  *
298  * @sci_req:
299  *
300  * Get the next SGL element from the request. - Check on which SGL element pair
301  * we are working - if working on SLG pair element A - advance to element B -
302  * else - check to see if there are more SGL element pairs for this IO request
303  * - if there are more SGL element pairs - advance to the next pair and return
304  * element A struct scu_sgl_element*
305  */
306 static struct scu_sgl_element *scic_sds_stp_request_pio_get_next_sgl(struct scic_sds_stp_request *stp_req)
307 {
308         struct scu_sgl_element *current_sgl;
309         struct scic_sds_request *sci_req = to_sci_req(stp_req);
310         struct scic_sds_request_pio_sgl *pio_sgl = &stp_req->type.pio.request_current;
311
312         if (pio_sgl->sgl_set == SCU_SGL_ELEMENT_PAIR_A) {
313                 if (pio_sgl->sgl_pair->B.address_lower == 0 &&
314                     pio_sgl->sgl_pair->B.address_upper == 0) {
315                         current_sgl = NULL;
316                 } else {
317                         pio_sgl->sgl_set = SCU_SGL_ELEMENT_PAIR_B;
318                         current_sgl = &pio_sgl->sgl_pair->B;
319                 }
320         } else {
321                 if (pio_sgl->sgl_pair->next_pair_lower == 0 &&
322                     pio_sgl->sgl_pair->next_pair_upper == 0) {
323                         current_sgl = NULL;
324                 } else {
325                         u64 phys_addr;
326
327                         phys_addr = pio_sgl->sgl_pair->next_pair_upper;
328                         phys_addr <<= 32;
329                         phys_addr |= pio_sgl->sgl_pair->next_pair_lower;
330
331                         pio_sgl->sgl_pair = scic_request_get_virt_addr(sci_req, phys_addr);
332                         pio_sgl->sgl_set = SCU_SGL_ELEMENT_PAIR_A;
333                         current_sgl = &pio_sgl->sgl_pair->A;
334                 }
335         }
336
337         return current_sgl;
338 }
339
340 /**
341  *
342  * @sci_req:
343  * @completion_code:
344  *
345  * This method processes a TC completion.  The expected TC completion is for
346  * the transmission of the H2D register FIS containing the SATA/STP non-data
347  * request. This method always successfully processes the TC completion.
348  * SCI_SUCCESS This value is always returned.
349  */
350 static enum sci_status scic_sds_stp_request_non_data_await_h2d_tc_completion_handler(
351         struct scic_sds_request *sci_req,
352         u32 completion_code)
353 {
354         switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
355         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
356                 scic_sds_request_set_status(
357                         sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS
358                         );
359
360                 sci_base_state_machine_change_state(
361                         &sci_req->started_substate_machine,
362                         SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE
363                         );
364                 break;
365
366         default:
367                 /*
368                  * All other completion status cause the IO to be complete.  If a NAK
369                  * was received, then it is up to the user to retry the request. */
370                 scic_sds_request_set_status(
371                         sci_req,
372                         SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
373                         SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
374                         );
375
376                 sci_base_state_machine_change_state(
377                         &sci_req->state_machine, SCI_BASE_REQUEST_STATE_COMPLETED);
378                 break;
379         }
380
381         return SCI_SUCCESS;
382 }
383
384 /**
385  *
386  * @request: This parameter specifies the request for which a frame has been
387  *    received.
388  * @frame_index: This parameter specifies the index of the frame that has been
389  *    received.
390  *
391  * This method processes frames received from the target while waiting for a
392  * device to host register FIS.  If a non-register FIS is received during this
393  * time, it is treated as a protocol violation from an IO perspective. Indicate
394  * if the received frame was processed successfully.
395  */
396 static enum sci_status scic_sds_stp_request_non_data_await_d2h_frame_handler(
397         struct scic_sds_request *sci_req,
398         u32 frame_index)
399 {
400         enum sci_status status;
401         struct dev_to_host_fis *frame_header;
402         u32 *frame_buffer;
403         struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
404         struct scic_sds_controller *scic = sci_req->owning_controller;
405
406         status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
407                                                                frame_index,
408                                                                (void **)&frame_header);
409
410         if (status != SCI_SUCCESS) {
411                 dev_err(scic_to_dev(sci_req->owning_controller),
412                         "%s: SCIC IO Request 0x%p could not get frame header "
413                         "for frame index %d, status %x\n",
414                         __func__, stp_req, frame_index, status);
415
416                 return status;
417         }
418
419         switch (frame_header->fis_type) {
420         case FIS_REGD2H:
421                 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
422                                                               frame_index,
423                                                               (void **)&frame_buffer);
424
425                 scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
426                                                        frame_header,
427                                                        frame_buffer);
428
429                 /* The command has completed with error */
430                 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_CHECK_RESPONSE,
431                                             SCI_FAILURE_IO_RESPONSE_VALID);
432                 break;
433
434         default:
435                 dev_warn(scic_to_dev(scic),
436                          "%s: IO Request:0x%p Frame Id:%d protocol "
437                           "violation occurred\n", __func__, stp_req,
438                           frame_index);
439
440                 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_UNEXP_FIS,
441                                             SCI_FAILURE_PROTOCOL_VIOLATION);
442                 break;
443         }
444
445         sci_base_state_machine_change_state(&sci_req->state_machine,
446                                             SCI_BASE_REQUEST_STATE_COMPLETED);
447
448         /* Frame has been decoded return it to the controller */
449         scic_sds_controller_release_frame(scic, frame_index);
450
451         return status;
452 }
453
454 /* --------------------------------------------------------------------------- */
455
456 static const struct scic_sds_io_request_state_handler scic_sds_stp_request_started_non_data_substate_handler_table[] = {
457         [SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE] = {
458                 .abort_handler          = scic_sds_request_started_state_abort_handler,
459                 .tc_completion_handler  = scic_sds_stp_request_non_data_await_h2d_tc_completion_handler,
460         },
461         [SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE] = {
462                 .abort_handler          = scic_sds_request_started_state_abort_handler,
463                 .frame_handler          = scic_sds_stp_request_non_data_await_d2h_frame_handler,
464         }
465 };
466
467 static void scic_sds_stp_request_started_non_data_await_h2d_completion_enter(
468         void *object)
469 {
470         struct scic_sds_request *sci_req = object;
471
472         SET_STATE_HANDLER(
473                 sci_req,
474                 scic_sds_stp_request_started_non_data_substate_handler_table,
475                 SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE
476                 );
477
478         scic_sds_remote_device_set_working_request(
479                 sci_req->target_device, sci_req
480                 );
481 }
482
483 static void scic_sds_stp_request_started_non_data_await_d2h_enter(void *object)
484 {
485         struct scic_sds_request *sci_req = object;
486
487         SET_STATE_HANDLER(
488                 sci_req,
489                 scic_sds_stp_request_started_non_data_substate_handler_table,
490                 SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE
491                 );
492 }
493
494 /* --------------------------------------------------------------------------- */
495
496 static const struct sci_base_state scic_sds_stp_request_started_non_data_substate_table[] = {
497         [SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE] = {
498                 .enter_state = scic_sds_stp_request_started_non_data_await_h2d_completion_enter,
499         },
500         [SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE] = {
501                 .enter_state = scic_sds_stp_request_started_non_data_await_d2h_enter,
502         },
503 };
504
505 enum sci_status scic_sds_stp_non_data_request_construct(struct scic_sds_request *sci_req)
506 {
507         struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
508
509         scic_sds_stp_non_ncq_request_construct(sci_req);
510
511         /* Build the STP task context structure */
512         scu_stp_raw_request_construct_task_context(stp_req, sci_req->task_context_buffer);
513
514         sci_base_state_machine_construct(&sci_req->started_substate_machine,
515                                          sci_req,
516                                          scic_sds_stp_request_started_non_data_substate_table,
517                                          SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE);
518
519         return SCI_SUCCESS;
520 }
521
522 #define SCU_MAX_FRAME_BUFFER_SIZE  0x400  /* 1K is the maximum SCU frame data payload */
523
524 /* transmit DATA_FIS from (current sgl + offset) for input
525  * parameter length. current sgl and offset is alreay stored in the IO request
526  */
527 static enum sci_status scic_sds_stp_request_pio_data_out_trasmit_data_frame(
528         struct scic_sds_request *sci_req,
529         u32 length)
530 {
531         struct scic_sds_controller *scic = sci_req->owning_controller;
532         struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
533         struct scu_task_context *task_context;
534         struct scu_sgl_element *current_sgl;
535
536         /* Recycle the TC and reconstruct it for sending out DATA FIS containing
537          * for the data from current_sgl+offset for the input length
538          */
539         task_context = scic_sds_controller_get_task_context_buffer(scic,
540                                                                    sci_req->io_tag);
541
542         if (stp_req->type.pio.request_current.sgl_set == SCU_SGL_ELEMENT_PAIR_A)
543                 current_sgl = &stp_req->type.pio.request_current.sgl_pair->A;
544         else
545                 current_sgl = &stp_req->type.pio.request_current.sgl_pair->B;
546
547         /* update the TC */
548         task_context->command_iu_upper = current_sgl->address_upper;
549         task_context->command_iu_lower = current_sgl->address_lower;
550         task_context->transfer_length_bytes = length;
551         task_context->type.stp.fis_type = FIS_DATA;
552
553         /* send the new TC out. */
554         return scic_controller_continue_io(sci_req);
555 }
556
557 static enum sci_status scic_sds_stp_request_pio_data_out_transmit_data(struct scic_sds_request *sci_req)
558 {
559
560         struct scu_sgl_element *current_sgl;
561         u32 sgl_offset;
562         u32 remaining_bytes_in_current_sgl = 0;
563         enum sci_status status = SCI_SUCCESS;
564         struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
565
566         sgl_offset = stp_req->type.pio.request_current.sgl_offset;
567
568         if (stp_req->type.pio.request_current.sgl_set == SCU_SGL_ELEMENT_PAIR_A) {
569                 current_sgl = &(stp_req->type.pio.request_current.sgl_pair->A);
570                 remaining_bytes_in_current_sgl = stp_req->type.pio.request_current.sgl_pair->A.length - sgl_offset;
571         } else {
572                 current_sgl = &(stp_req->type.pio.request_current.sgl_pair->B);
573                 remaining_bytes_in_current_sgl = stp_req->type.pio.request_current.sgl_pair->B.length - sgl_offset;
574         }
575
576
577         if (stp_req->type.pio.pio_transfer_bytes > 0) {
578                 if (stp_req->type.pio.pio_transfer_bytes >= remaining_bytes_in_current_sgl) {
579                         /* recycle the TC and send the H2D Data FIS from (current sgl + sgl_offset) and length = remaining_bytes_in_current_sgl */
580                         status = scic_sds_stp_request_pio_data_out_trasmit_data_frame(sci_req, remaining_bytes_in_current_sgl);
581                         if (status == SCI_SUCCESS) {
582                                 stp_req->type.pio.pio_transfer_bytes -= remaining_bytes_in_current_sgl;
583
584                                 /* update the current sgl, sgl_offset and save for future */
585                                 current_sgl = scic_sds_stp_request_pio_get_next_sgl(stp_req);
586                                 sgl_offset = 0;
587                         }
588                 } else if (stp_req->type.pio.pio_transfer_bytes < remaining_bytes_in_current_sgl) {
589                         /* recycle the TC and send the H2D Data FIS from (current sgl + sgl_offset) and length = type.pio.pio_transfer_bytes */
590                         scic_sds_stp_request_pio_data_out_trasmit_data_frame(sci_req, stp_req->type.pio.pio_transfer_bytes);
591
592                         if (status == SCI_SUCCESS) {
593                                 /* Sgl offset will be adjusted and saved for future */
594                                 sgl_offset += stp_req->type.pio.pio_transfer_bytes;
595                                 current_sgl->address_lower += stp_req->type.pio.pio_transfer_bytes;
596                                 stp_req->type.pio.pio_transfer_bytes = 0;
597                         }
598                 }
599         }
600
601         if (status == SCI_SUCCESS) {
602                 stp_req->type.pio.request_current.sgl_offset = sgl_offset;
603         }
604
605         return status;
606 }
607
608 /**
609  *
610  * @stp_request: The request that is used for the SGL processing.
611  * @data_buffer: The buffer of data to be copied.
612  * @length: The length of the data transfer.
613  *
614  * Copy the data from the buffer for the length specified to the IO reqeust SGL
615  * specified data region. enum sci_status
616  */
617 static enum sci_status
618 scic_sds_stp_request_pio_data_in_copy_data_buffer(struct scic_sds_stp_request *stp_req,
619                                                   u8 *data_buf, u32 len)
620 {
621         struct scic_sds_request *sci_req;
622         struct isci_request *ireq;
623         u8 *src_addr;
624         int copy_len;
625         struct sas_task *task;
626         struct scatterlist *sg;
627         void *kaddr;
628         int total_len = len;
629
630         sci_req = to_sci_req(stp_req);
631         ireq = sci_req_to_ireq(sci_req);
632         task = isci_request_access_task(ireq);
633         src_addr = data_buf;
634
635         if (task->num_scatter > 0) {
636                 sg = task->scatter;
637
638                 while (total_len > 0) {
639                         struct page *page = sg_page(sg);
640
641                         copy_len = min_t(int, total_len, sg_dma_len(sg));
642                         kaddr = kmap_atomic(page, KM_IRQ0);
643                         memcpy(kaddr + sg->offset, src_addr, copy_len);
644                         kunmap_atomic(kaddr, KM_IRQ0);
645                         total_len -= copy_len;
646                         src_addr += copy_len;
647                         sg = sg_next(sg);
648                 }
649         } else {
650                 BUG_ON(task->total_xfer_len < total_len);
651                 memcpy(task->scatter, src_addr, total_len);
652         }
653
654         return SCI_SUCCESS;
655 }
656
657 /**
658  *
659  * @sci_req: The PIO DATA IN request that is to receive the data.
660  * @data_buffer: The buffer to copy from.
661  *
662  * Copy the data buffer to the io request data region. enum sci_status
663  */
664 static enum sci_status scic_sds_stp_request_pio_data_in_copy_data(
665         struct scic_sds_stp_request *sci_req,
666         u8 *data_buffer)
667 {
668         enum sci_status status;
669
670         /*
671          * If there is less than 1K remaining in the transfer request
672          * copy just the data for the transfer */
673         if (sci_req->type.pio.pio_transfer_bytes < SCU_MAX_FRAME_BUFFER_SIZE) {
674                 status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
675                         sci_req, data_buffer, sci_req->type.pio.pio_transfer_bytes);
676
677                 if (status == SCI_SUCCESS)
678                         sci_req->type.pio.pio_transfer_bytes = 0;
679         } else {
680                 /* We are transfering the whole frame so copy */
681                 status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
682                         sci_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
683
684                 if (status == SCI_SUCCESS)
685                         sci_req->type.pio.pio_transfer_bytes -= SCU_MAX_FRAME_BUFFER_SIZE;
686         }
687
688         return status;
689 }
690
691 /**
692  *
693  * @sci_req:
694  * @completion_code:
695  *
696  * enum sci_status
697  */
698 static enum sci_status scic_sds_stp_request_pio_await_h2d_completion_tc_completion_handler(
699         struct scic_sds_request *sci_req,
700         u32 completion_code)
701 {
702         enum sci_status status = SCI_SUCCESS;
703
704         switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
705         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
706                 scic_sds_request_set_status(
707                         sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS
708                         );
709
710                 sci_base_state_machine_change_state(
711                         &sci_req->started_substate_machine,
712                         SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE
713                         );
714                 break;
715
716         default:
717                 /*
718                  * All other completion status cause the IO to be complete.  If a NAK
719                  * was received, then it is up to the user to retry the request. */
720                 scic_sds_request_set_status(
721                         sci_req,
722                         SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
723                         SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
724                         );
725
726                 sci_base_state_machine_change_state(
727                         &sci_req->state_machine,
728                         SCI_BASE_REQUEST_STATE_COMPLETED
729                         );
730                 break;
731         }
732
733         return status;
734 }
735
736 static enum sci_status scic_sds_stp_request_pio_await_frame_frame_handler(struct scic_sds_request *sci_req,
737                                                                           u32 frame_index)
738 {
739         struct scic_sds_controller *scic = sci_req->owning_controller;
740         struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
741         struct isci_request *ireq = sci_req_to_ireq(sci_req);
742         struct sas_task *task = isci_request_access_task(ireq);
743         struct dev_to_host_fis *frame_header;
744         enum sci_status status;
745         u32 *frame_buffer;
746
747         status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
748                                                                frame_index,
749                                                                (void **)&frame_header);
750
751         if (status != SCI_SUCCESS) {
752                 dev_err(scic_to_dev(scic),
753                         "%s: SCIC IO Request 0x%p could not get frame header "
754                         "for frame index %d, status %x\n",
755                         __func__, stp_req, frame_index, status);
756                 return status;
757         }
758
759         switch (frame_header->fis_type) {
760         case FIS_PIO_SETUP:
761                 /* Get from the frame buffer the PIO Setup Data */
762                 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
763                                                               frame_index,
764                                                               (void **)&frame_buffer);
765
766                 /* Get the data from the PIO Setup The SCU Hardware returns
767                  * first word in the frame_header and the rest of the data is in
768                  * the frame buffer so we need to back up one dword
769                  */
770
771                 /* transfer_count: first 16bits in the 4th dword */
772                 stp_req->type.pio.pio_transfer_bytes = frame_buffer[3] & 0xffff;
773
774                 /* ending_status: 4th byte in the 3rd dword */
775                 stp_req->type.pio.ending_status = (frame_buffer[2] >> 24) & 0xff;
776
777                 scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
778                                                        frame_header,
779                                                        frame_buffer);
780
781                 sci_req->stp.rsp.status = stp_req->type.pio.ending_status;
782
783                 /* The next state is dependent on whether the
784                  * request was PIO Data-in or Data out
785                  */
786                 if (task->data_dir == DMA_FROM_DEVICE) {
787                         sci_base_state_machine_change_state(&sci_req->started_substate_machine,
788                                                             SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE);
789                 } else if (task->data_dir == DMA_TO_DEVICE) {
790                         /* Transmit data */
791                         status = scic_sds_stp_request_pio_data_out_transmit_data(sci_req);
792                         if (status != SCI_SUCCESS)
793                                 break;
794                         sci_base_state_machine_change_state(&sci_req->started_substate_machine,
795                                                             SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE);
796                 }
797                 break;
798         case FIS_SETDEVBITS:
799                 sci_base_state_machine_change_state(&sci_req->started_substate_machine,
800                                                     SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE);
801                 break;
802         case FIS_REGD2H:
803                 if (frame_header->status & ATA_BUSY) {
804                         /* Now why is the drive sending a D2H Register FIS when
805                          * it is still busy?  Do nothing since we are still in
806                          * the right state.
807                          */
808                         dev_dbg(scic_to_dev(scic),
809                                 "%s: SCIC PIO Request 0x%p received "
810                                 "D2H Register FIS with BSY status "
811                                 "0x%x\n", __func__, stp_req,
812                                 frame_header->status);
813                         break;
814                 }
815
816                 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
817                                                               frame_index,
818                                                               (void **)&frame_buffer);
819
820                 scic_sds_controller_copy_sata_response(&sci_req->stp.req,
821                                                        frame_header,
822                                                        frame_buffer);
823
824                 scic_sds_request_set_status(sci_req,
825                                             SCU_TASK_DONE_CHECK_RESPONSE,
826                                             SCI_FAILURE_IO_RESPONSE_VALID);
827
828                 sci_base_state_machine_change_state(&sci_req->state_machine,
829                                                     SCI_BASE_REQUEST_STATE_COMPLETED);
830                 break;
831         default:
832                 /* FIXME: what do we do here? */
833                 break;
834         }
835
836         /* Frame is decoded return it to the controller */
837         scic_sds_controller_release_frame(scic, frame_index);
838
839         return status;
840 }
841
842 static enum sci_status scic_sds_stp_request_pio_data_in_await_data_frame_handler(struct scic_sds_request *sci_req,
843                                                                                  u32 frame_index)
844 {
845         enum sci_status status;
846         struct dev_to_host_fis *frame_header;
847         struct sata_fis_data *frame_buffer;
848         struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
849         struct scic_sds_controller *scic = sci_req->owning_controller;
850
851         status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
852                                                                frame_index,
853                                                                (void **)&frame_header);
854
855         if (status != SCI_SUCCESS) {
856                 dev_err(scic_to_dev(scic),
857                         "%s: SCIC IO Request 0x%p could not get frame header "
858                         "for frame index %d, status %x\n",
859                         __func__, stp_req, frame_index, status);
860                 return status;
861         }
862
863         if (frame_header->fis_type == FIS_DATA) {
864                 if (stp_req->type.pio.request_current.sgl_pair == NULL) {
865                         sci_req->saved_rx_frame_index = frame_index;
866                         stp_req->type.pio.pio_transfer_bytes = 0;
867                 } else {
868                         scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
869                                                                       frame_index,
870                                                                       (void **)&frame_buffer);
871
872                         status = scic_sds_stp_request_pio_data_in_copy_data(stp_req,
873                                                                             (u8 *)frame_buffer);
874
875                         /* Frame is decoded return it to the controller */
876                         scic_sds_controller_release_frame(scic, frame_index);
877                 }
878
879                 /* Check for the end of the transfer, are there more
880                  * bytes remaining for this data transfer
881                  */
882                 if (status != SCI_SUCCESS ||
883                     stp_req->type.pio.pio_transfer_bytes != 0)
884                         return status;
885
886                 if ((stp_req->type.pio.ending_status & ATA_BUSY) == 0) {
887                         scic_sds_request_set_status(sci_req,
888                                                     SCU_TASK_DONE_CHECK_RESPONSE,
889                                                     SCI_FAILURE_IO_RESPONSE_VALID);
890
891                         sci_base_state_machine_change_state(&sci_req->state_machine,
892                                                             SCI_BASE_REQUEST_STATE_COMPLETED);
893                 } else {
894                         sci_base_state_machine_change_state(&sci_req->started_substate_machine,
895                                                             SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE);
896                 }
897         } else {
898                 dev_err(scic_to_dev(scic),
899                         "%s: SCIC PIO Request 0x%p received frame %d "
900                         "with fis type 0x%02x when expecting a data "
901                         "fis.\n", __func__, stp_req, frame_index,
902                         frame_header->fis_type);
903
904                 scic_sds_request_set_status(sci_req,
905                                             SCU_TASK_DONE_GOOD,
906                                             SCI_FAILURE_IO_REQUIRES_SCSI_ABORT);
907
908                 sci_base_state_machine_change_state(&sci_req->state_machine,
909                                                     SCI_BASE_REQUEST_STATE_COMPLETED);
910
911                 /* Frame is decoded return it to the controller */
912                 scic_sds_controller_release_frame(scic, frame_index);
913         }
914
915         return status;
916 }
917
918
919 /**
920  *
921  * @sci_req:
922  * @completion_code:
923  *
924  * enum sci_status
925  */
926 static enum sci_status scic_sds_stp_request_pio_data_out_await_data_transmit_completion_tc_completion_handler(
927
928         struct scic_sds_request *sci_req,
929         u32 completion_code)
930 {
931         enum sci_status status = SCI_SUCCESS;
932         bool all_frames_transferred = false;
933         struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
934
935         switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
936         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
937                 /* Transmit data */
938                 if (stp_req->type.pio.pio_transfer_bytes != 0) {
939                         status = scic_sds_stp_request_pio_data_out_transmit_data(sci_req);
940                         if (status == SCI_SUCCESS) {
941                                 if (stp_req->type.pio.pio_transfer_bytes == 0)
942                                         all_frames_transferred = true;
943                         }
944                 } else if (stp_req->type.pio.pio_transfer_bytes == 0) {
945                         /*
946                          * this will happen if the all data is written at the
947                          * first time after the pio setup fis is received
948                          */
949                         all_frames_transferred  = true;
950                 }
951
952                 /* all data transferred. */
953                 if (all_frames_transferred) {
954                         /*
955                          * Change the state to SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_FRAME_SUBSTATE
956                          * and wait for PIO_SETUP fis / or D2H REg fis. */
957                         sci_base_state_machine_change_state(
958                                 &sci_req->started_substate_machine,
959                                 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE
960                                 );
961                 }
962                 break;
963
964         default:
965                 /*
966                  * All other completion status cause the IO to be complete.  If a NAK
967                  * was received, then it is up to the user to retry the request. */
968                 scic_sds_request_set_status(
969                         sci_req,
970                         SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
971                         SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
972                         );
973
974                 sci_base_state_machine_change_state(
975                         &sci_req->state_machine,
976                         SCI_BASE_REQUEST_STATE_COMPLETED
977                         );
978                 break;
979         }
980
981         return status;
982 }
983
984 /**
985  *
986  * @request: This is the request which is receiving the event.
987  * @event_code: This is the event code that the request on which the request is
988  *    expected to take action.
989  *
990  * This method will handle any link layer events while waiting for the data
991  * frame. enum sci_status SCI_SUCCESS SCI_FAILURE
992  */
993 static enum sci_status scic_sds_stp_request_pio_data_in_await_data_event_handler(
994         struct scic_sds_request *request,
995         u32 event_code)
996 {
997         enum sci_status status;
998
999         switch (scu_get_event_specifier(event_code)) {
1000         case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
1001                 /*
1002                  * We are waiting for data and the SCU has R_ERR the data frame.
1003                  * Go back to waiting for the D2H Register FIS */
1004                 sci_base_state_machine_change_state(
1005                         &request->started_substate_machine,
1006                         SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE
1007                         );
1008
1009                 status = SCI_SUCCESS;
1010                 break;
1011
1012         default:
1013                 dev_err(scic_to_dev(request->owning_controller),
1014                         "%s: SCIC PIO Request 0x%p received unexpected "
1015                         "event 0x%08x\n",
1016                         __func__, request, event_code);
1017
1018                 /* / @todo Should we fail the PIO request when we get an unexpected event? */
1019                 status = SCI_FAILURE;
1020                 break;
1021         }
1022
1023         return status;
1024 }
1025
1026 /* --------------------------------------------------------------------------- */
1027
1028 static const struct scic_sds_io_request_state_handler scic_sds_stp_request_started_pio_substate_handler_table[] = {
1029         [SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE] = {
1030                 .abort_handler          = scic_sds_request_started_state_abort_handler,
1031                 .tc_completion_handler  = scic_sds_stp_request_pio_await_h2d_completion_tc_completion_handler,
1032         },
1033         [SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE] = {
1034                 .abort_handler          = scic_sds_request_started_state_abort_handler,
1035                 .frame_handler          = scic_sds_stp_request_pio_await_frame_frame_handler
1036         },
1037         [SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE] = {
1038                 .abort_handler          = scic_sds_request_started_state_abort_handler,
1039                 .event_handler          = scic_sds_stp_request_pio_data_in_await_data_event_handler,
1040                 .frame_handler          = scic_sds_stp_request_pio_data_in_await_data_frame_handler
1041         },
1042         [SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE] = {
1043                 .abort_handler          = scic_sds_request_started_state_abort_handler,
1044                 .tc_completion_handler  = scic_sds_stp_request_pio_data_out_await_data_transmit_completion_tc_completion_handler,
1045         }
1046 };
1047
1048 static void scic_sds_stp_request_started_pio_await_h2d_completion_enter(
1049         void *object)
1050 {
1051         struct scic_sds_request *sci_req = object;
1052
1053         SET_STATE_HANDLER(
1054                 sci_req,
1055                 scic_sds_stp_request_started_pio_substate_handler_table,
1056                 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE
1057                 );
1058
1059         scic_sds_remote_device_set_working_request(
1060                 sci_req->target_device, sci_req);
1061 }
1062
1063 static void scic_sds_stp_request_started_pio_await_frame_enter(void *object)
1064 {
1065         struct scic_sds_request *sci_req = object;
1066
1067         SET_STATE_HANDLER(
1068                 sci_req,
1069                 scic_sds_stp_request_started_pio_substate_handler_table,
1070                 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE
1071                 );
1072 }
1073
1074 static void scic_sds_stp_request_started_pio_data_in_await_data_enter(
1075         void *object)
1076 {
1077         struct scic_sds_request *sci_req = object;
1078
1079         SET_STATE_HANDLER(
1080                 sci_req,
1081                 scic_sds_stp_request_started_pio_substate_handler_table,
1082                 SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE
1083                 );
1084 }
1085
1086 static void scic_sds_stp_request_started_pio_data_out_transmit_data_enter(
1087         void *object)
1088 {
1089         struct scic_sds_request *sci_req = object;
1090
1091         SET_STATE_HANDLER(
1092                 sci_req,
1093                 scic_sds_stp_request_started_pio_substate_handler_table,
1094                 SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE
1095                 );
1096 }
1097
1098 /* --------------------------------------------------------------------------- */
1099
1100 static const struct sci_base_state scic_sds_stp_request_started_pio_substate_table[] = {
1101         [SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE] = {
1102                 .enter_state = scic_sds_stp_request_started_pio_await_h2d_completion_enter,
1103         },
1104         [SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE] = {
1105                 .enter_state = scic_sds_stp_request_started_pio_await_frame_enter,
1106         },
1107         [SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE] = {
1108                 .enter_state = scic_sds_stp_request_started_pio_data_in_await_data_enter,
1109         },
1110         [SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE] = {
1111                 .enter_state = scic_sds_stp_request_started_pio_data_out_transmit_data_enter,
1112         }
1113 };
1114
1115 enum sci_status
1116 scic_sds_stp_pio_request_construct(struct scic_sds_request *sci_req,
1117                                    bool copy_rx_frame)
1118 {
1119         struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
1120         struct scic_sds_stp_pio_request *pio = &stp_req->type.pio;
1121
1122         scic_sds_stp_non_ncq_request_construct(sci_req);
1123
1124         scu_stp_raw_request_construct_task_context(stp_req,
1125                                                    sci_req->task_context_buffer);
1126
1127         pio->current_transfer_bytes = 0;
1128         pio->ending_error = 0;
1129         pio->ending_status = 0;
1130
1131         pio->request_current.sgl_offset = 0;
1132         pio->request_current.sgl_set = SCU_SGL_ELEMENT_PAIR_A;
1133
1134         if (copy_rx_frame) {
1135                 scic_sds_request_build_sgl(sci_req);
1136                 /* Since the IO request copy of the TC contains the same data as
1137                  * the actual TC this pointer is vaild for either.
1138                  */
1139                 pio->request_current.sgl_pair = &sci_req->task_context_buffer->sgl_pair_ab;
1140         } else {
1141                 /* The user does not want the data copied to the SGL buffer location */
1142                 pio->request_current.sgl_pair = NULL;
1143         }
1144
1145         sci_base_state_machine_construct(&sci_req->started_substate_machine,
1146                                          sci_req,
1147                                          scic_sds_stp_request_started_pio_substate_table,
1148                                          SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE);
1149
1150         return SCI_SUCCESS;
1151 }
1152
1153 static void scic_sds_stp_request_udma_complete_request(
1154         struct scic_sds_request *request,
1155         u32 scu_status,
1156         enum sci_status sci_status)
1157 {
1158         scic_sds_request_set_status(request, scu_status, sci_status);
1159         sci_base_state_machine_change_state(&request->state_machine,
1160                 SCI_BASE_REQUEST_STATE_COMPLETED);
1161 }
1162
1163 static enum sci_status scic_sds_stp_request_udma_general_frame_handler(struct scic_sds_request *sci_req,
1164                                                                        u32 frame_index)
1165 {
1166         struct scic_sds_controller *scic = sci_req->owning_controller;
1167         struct dev_to_host_fis *frame_header;
1168         enum sci_status status;
1169         u32 *frame_buffer;
1170
1171         status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1172                                                                frame_index,
1173                                                                (void **)&frame_header);
1174
1175         if ((status == SCI_SUCCESS) &&
1176             (frame_header->fis_type == FIS_REGD2H)) {
1177                 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1178                                                               frame_index,
1179                                                               (void **)&frame_buffer);
1180
1181                 scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
1182                                                        frame_header,
1183                                                        frame_buffer);
1184         }
1185
1186         scic_sds_controller_release_frame(scic, frame_index);
1187
1188         return status;
1189 }
1190
1191 static enum sci_status scic_sds_stp_request_udma_await_tc_completion_tc_completion_handler(
1192         struct scic_sds_request *sci_req,
1193         u32 completion_code)
1194 {
1195         enum sci_status status = SCI_SUCCESS;
1196
1197         switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1198         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1199                 scic_sds_stp_request_udma_complete_request(sci_req,
1200                                                            SCU_TASK_DONE_GOOD,
1201                                                            SCI_SUCCESS);
1202                 break;
1203         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
1204         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
1205                 /*
1206                  * We must check ther response buffer to see if the D2H Register FIS was
1207                  * received before we got the TC completion. */
1208                 if (sci_req->stp.rsp.fis_type == FIS_REGD2H) {
1209                         scic_sds_remote_device_suspend(sci_req->target_device,
1210                                 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
1211
1212                         scic_sds_stp_request_udma_complete_request(sci_req,
1213                                                                    SCU_TASK_DONE_CHECK_RESPONSE,
1214                                                                    SCI_FAILURE_IO_RESPONSE_VALID);
1215                 } else {
1216                         /*
1217                          * If we have an error completion status for the TC then we can expect a
1218                          * D2H register FIS from the device so we must change state to wait for it */
1219                         sci_base_state_machine_change_state(&sci_req->started_substate_machine,
1220                                 SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE);
1221                 }
1222                 break;
1223
1224         /*
1225          * / @todo Check to see if any of these completion status need to wait for
1226          * /       the device to host register fis. */
1227         /* / @todo We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR - this comes only for B0 */
1228         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_INV_FIS_LEN):
1229         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
1230         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_R_ERR):
1231         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CMD_LL_R_ERR):
1232         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CRC_ERR):
1233                 scic_sds_remote_device_suspend(sci_req->target_device,
1234                         SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
1235         /* Fall through to the default case */
1236         default:
1237                 /* All other completion status cause the IO to be complete. */
1238                 scic_sds_stp_request_udma_complete_request(sci_req,
1239                                         SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1240                                         SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1241                 break;
1242         }
1243
1244         return status;
1245 }
1246
1247 static enum sci_status scic_sds_stp_request_udma_await_d2h_reg_fis_frame_handler(
1248         struct scic_sds_request *sci_req,
1249         u32 frame_index)
1250 {
1251         enum sci_status status;
1252
1253         /* Use the general frame handler to copy the resposne data */
1254         status = scic_sds_stp_request_udma_general_frame_handler(sci_req, frame_index);
1255
1256         if (status != SCI_SUCCESS)
1257                 return status;
1258
1259         scic_sds_stp_request_udma_complete_request(sci_req,
1260                                                    SCU_TASK_DONE_CHECK_RESPONSE,
1261                                                    SCI_FAILURE_IO_RESPONSE_VALID);
1262
1263         return status;
1264 }
1265
1266 /* --------------------------------------------------------------------------- */
1267
1268 static const struct scic_sds_io_request_state_handler scic_sds_stp_request_started_udma_substate_handler_table[] = {
1269         [SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE] = {
1270                 .abort_handler          = scic_sds_request_started_state_abort_handler,
1271                 .tc_completion_handler  = scic_sds_stp_request_udma_await_tc_completion_tc_completion_handler,
1272                 .frame_handler          = scic_sds_stp_request_udma_general_frame_handler,
1273         },
1274         [SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE] = {
1275                 .abort_handler          = scic_sds_request_started_state_abort_handler,
1276                 .frame_handler          = scic_sds_stp_request_udma_await_d2h_reg_fis_frame_handler,
1277         },
1278 };
1279
1280 static void scic_sds_stp_request_started_udma_await_tc_completion_enter(
1281         void *object)
1282 {
1283         struct scic_sds_request *sci_req = object;
1284
1285         SET_STATE_HANDLER(
1286                 sci_req,
1287                 scic_sds_stp_request_started_udma_substate_handler_table,
1288                 SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE
1289                 );
1290 }
1291
1292 /**
1293  *
1294  *
1295  * This state is entered when there is an TC completion failure.  The hardware
1296  * received an unexpected condition while processing the IO request and now
1297  * will UF the D2H register FIS to complete the IO.
1298  */
1299 static void scic_sds_stp_request_started_udma_await_d2h_reg_fis_enter(
1300         void *object)
1301 {
1302         struct scic_sds_request *sci_req = object;
1303
1304         SET_STATE_HANDLER(
1305                 sci_req,
1306                 scic_sds_stp_request_started_udma_substate_handler_table,
1307                 SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE
1308                 );
1309 }
1310
1311 /* --------------------------------------------------------------------------- */
1312
1313 static const struct sci_base_state scic_sds_stp_request_started_udma_substate_table[] = {
1314         [SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE] = {
1315                 .enter_state = scic_sds_stp_request_started_udma_await_tc_completion_enter,
1316         },
1317         [SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE] = {
1318                 .enter_state = scic_sds_stp_request_started_udma_await_d2h_reg_fis_enter,
1319         },
1320 };
1321
1322 enum sci_status scic_sds_stp_udma_request_construct(struct scic_sds_request *sci_req,
1323                                                     u32 len,
1324                                                     enum dma_data_direction dir)
1325 {
1326         scic_sds_stp_non_ncq_request_construct(sci_req);
1327
1328         scic_sds_stp_optimized_request_construct(sci_req, SCU_TASK_TYPE_DMA_IN,
1329                                                  len, dir);
1330
1331         sci_base_state_machine_construct(
1332                 &sci_req->started_substate_machine,
1333                 sci_req,
1334                 scic_sds_stp_request_started_udma_substate_table,
1335                 SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE
1336                 );
1337
1338         return SCI_SUCCESS;
1339 }
1340
1341 /**
1342  *
1343  * @sci_req:
1344  * @completion_code:
1345  *
1346  * This method processes a TC completion.  The expected TC completion is for
1347  * the transmission of the H2D register FIS containing the SATA/STP non-data
1348  * request. This method always successfully processes the TC completion.
1349  * SCI_SUCCESS This value is always returned.
1350  */
1351 static enum sci_status scic_sds_stp_request_soft_reset_await_h2d_asserted_tc_completion_handler(
1352         struct scic_sds_request *sci_req,
1353         u32 completion_code)
1354 {
1355         switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1356         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1357                 scic_sds_request_set_status(
1358                         sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS
1359                         );
1360
1361                 sci_base_state_machine_change_state(
1362                         &sci_req->started_substate_machine,
1363                         SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE
1364                         );
1365                 break;
1366
1367         default:
1368                 /*
1369                  * All other completion status cause the IO to be complete.  If a NAK
1370                  * was received, then it is up to the user to retry the request. */
1371                 scic_sds_request_set_status(
1372                         sci_req,
1373                         SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1374                         SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
1375                         );
1376
1377                 sci_base_state_machine_change_state(
1378                         &sci_req->state_machine, SCI_BASE_REQUEST_STATE_COMPLETED);
1379                 break;
1380         }
1381
1382         return SCI_SUCCESS;
1383 }
1384
1385 /**
1386  *
1387  * @sci_req:
1388  * @completion_code:
1389  *
1390  * This method processes a TC completion.  The expected TC completion is for
1391  * the transmission of the H2D register FIS containing the SATA/STP non-data
1392  * request. This method always successfully processes the TC completion.
1393  * SCI_SUCCESS This value is always returned.
1394  */
1395 static enum sci_status scic_sds_stp_request_soft_reset_await_h2d_diagnostic_tc_completion_handler(
1396         struct scic_sds_request *sci_req,
1397         u32 completion_code)
1398 {
1399         switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1400         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1401                 scic_sds_request_set_status(
1402                         sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS
1403                         );
1404
1405                 sci_base_state_machine_change_state(
1406                         &sci_req->started_substate_machine,
1407                         SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE
1408                         );
1409                 break;
1410
1411         default:
1412                 /*
1413                  * All other completion status cause the IO to be complete.  If a NAK
1414                  * was received, then it is up to the user to retry the request. */
1415                 scic_sds_request_set_status(
1416                         sci_req,
1417                         SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1418                         SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
1419                         );
1420
1421                 sci_base_state_machine_change_state(&sci_req->state_machine,
1422                                 SCI_BASE_REQUEST_STATE_COMPLETED);
1423                 break;
1424         }
1425
1426         return SCI_SUCCESS;
1427 }
1428
1429 /**
1430  *
1431  * @request: This parameter specifies the request for which a frame has been
1432  *    received.
1433  * @frame_index: This parameter specifies the index of the frame that has been
1434  *    received.
1435  *
1436  * This method processes frames received from the target while waiting for a
1437  * device to host register FIS.  If a non-register FIS is received during this
1438  * time, it is treated as a protocol violation from an IO perspective. Indicate
1439  * if the received frame was processed successfully.
1440  */
1441 static enum sci_status scic_sds_stp_request_soft_reset_await_d2h_frame_handler(
1442         struct scic_sds_request *sci_req,
1443         u32 frame_index)
1444 {
1445         enum sci_status status;
1446         struct dev_to_host_fis *frame_header;
1447         u32 *frame_buffer;
1448         struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
1449         struct scic_sds_controller *scic = sci_req->owning_controller;
1450
1451         status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1452                                                                frame_index,
1453                                                                (void **)&frame_header);
1454         if (status != SCI_SUCCESS) {
1455                 dev_err(scic_to_dev(scic),
1456                         "%s: SCIC IO Request 0x%p could not get frame header "
1457                         "for frame index %d, status %x\n",
1458                         __func__, stp_req, frame_index, status);
1459                 return status;
1460         }
1461
1462         switch (frame_header->fis_type) {
1463         case FIS_REGD2H:
1464                 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1465                                                               frame_index,
1466                                                               (void **)&frame_buffer);
1467
1468                 scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
1469                                                        frame_header,
1470                                                        frame_buffer);
1471
1472                 /* The command has completed with error */
1473                 scic_sds_request_set_status(sci_req,
1474                                             SCU_TASK_DONE_CHECK_RESPONSE,
1475                                             SCI_FAILURE_IO_RESPONSE_VALID);
1476                 break;
1477
1478         default:
1479                 dev_warn(scic_to_dev(scic),
1480                          "%s: IO Request:0x%p Frame Id:%d protocol "
1481                          "violation occurred\n", __func__, stp_req,
1482                          frame_index);
1483
1484                 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_UNEXP_FIS,
1485                                             SCI_FAILURE_PROTOCOL_VIOLATION);
1486                 break;
1487         }
1488
1489         sci_base_state_machine_change_state(&sci_req->state_machine,
1490                                             SCI_BASE_REQUEST_STATE_COMPLETED);
1491
1492         /* Frame has been decoded return it to the controller */
1493         scic_sds_controller_release_frame(scic, frame_index);
1494
1495         return status;
1496 }
1497
1498 /* --------------------------------------------------------------------------- */
1499
1500 static const struct scic_sds_io_request_state_handler scic_sds_stp_request_started_soft_reset_substate_handler_table[] = {
1501         [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE] = {
1502                 .abort_handler          = scic_sds_request_started_state_abort_handler,
1503                 .tc_completion_handler  = scic_sds_stp_request_soft_reset_await_h2d_asserted_tc_completion_handler,
1504         },
1505         [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE] = {
1506                 .abort_handler          = scic_sds_request_started_state_abort_handler,
1507                 .tc_completion_handler  = scic_sds_stp_request_soft_reset_await_h2d_diagnostic_tc_completion_handler,
1508         },
1509         [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE] = {
1510                 .abort_handler          = scic_sds_request_started_state_abort_handler,
1511                 .frame_handler          = scic_sds_stp_request_soft_reset_await_d2h_frame_handler,
1512         },
1513 };
1514
1515 static void scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter(
1516         void *object)
1517 {
1518         struct scic_sds_request *sci_req = object;
1519
1520         SET_STATE_HANDLER(
1521                 sci_req,
1522                 scic_sds_stp_request_started_soft_reset_substate_handler_table,
1523                 SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE
1524                 );
1525
1526         scic_sds_remote_device_set_working_request(
1527                 sci_req->target_device, sci_req
1528                 );
1529 }
1530
1531 static void scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter(
1532         void *object)
1533 {
1534         struct scic_sds_request *sci_req = object;
1535         struct scu_task_context *task_context;
1536         struct host_to_dev_fis *h2d_fis;
1537         enum sci_status status;
1538
1539         /* Clear the SRST bit */
1540         h2d_fis = &sci_req->stp.cmd;
1541         h2d_fis->control = 0;
1542
1543         /* Clear the TC control bit */
1544         task_context = scic_sds_controller_get_task_context_buffer(
1545                 sci_req->owning_controller, sci_req->io_tag);
1546         task_context->control_frame = 0;
1547
1548         status = scic_controller_continue_io(sci_req);
1549         if (status == SCI_SUCCESS) {
1550                 SET_STATE_HANDLER(
1551                         sci_req,
1552                         scic_sds_stp_request_started_soft_reset_substate_handler_table,
1553                         SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE
1554                         );
1555         }
1556 }
1557
1558 static void scic_sds_stp_request_started_soft_reset_await_d2h_response_enter(
1559         void *object)
1560 {
1561         struct scic_sds_request *sci_req = object;
1562
1563         SET_STATE_HANDLER(
1564                 sci_req,
1565                 scic_sds_stp_request_started_soft_reset_substate_handler_table,
1566                 SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE
1567                 );
1568 }
1569
1570 static const struct sci_base_state scic_sds_stp_request_started_soft_reset_substate_table[] = {
1571         [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE] = {
1572                 .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter,
1573         },
1574         [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE] = {
1575                 .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter,
1576         },
1577         [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE] = {
1578                 .enter_state = scic_sds_stp_request_started_soft_reset_await_d2h_response_enter,
1579         },
1580 };
1581
1582 enum sci_status scic_sds_stp_soft_reset_request_construct(struct scic_sds_request *sci_req)
1583 {
1584         struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
1585
1586         scic_sds_stp_non_ncq_request_construct(sci_req);
1587
1588         /* Build the STP task context structure */
1589         scu_stp_raw_request_construct_task_context(stp_req, sci_req->task_context_buffer);
1590
1591         sci_base_state_machine_construct(&sci_req->started_substate_machine,
1592                                          sci_req,
1593                                          scic_sds_stp_request_started_soft_reset_substate_table,
1594                                          SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE);
1595
1596         return SCI_SUCCESS;
1597 }