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