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