isci: Make the driver copy data directly from and to sg for PIO
[pandora-kernel.git] / drivers / scsi / isci / core / scic_sds_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_sas.h"
58 #include "intel_sata.h"
59 #include "intel_sat.h"
60 #include "sci_base_request.h"
61 #include "scic_controller.h"
62 #include "scic_io_request.h"
63 #include "scic_remote_device.h"
64 #include "scic_sds_controller.h"
65 #include "scic_sds_controller_registers.h"
66 #include "scic_sds_pci.h"
67 #include "scic_sds_port.h"
68 #include "scic_sds_remote_device.h"
69 #include "scic_sds_request.h"
70 #include "scic_sds_smp_request.h"
71 #include "scic_sds_stp_request.h"
72 #include "scic_sds_unsolicited_frame_control.h"
73 #include "scic_user_callback.h"
74 #include "sci_environment.h"
75 #include "sci_util.h"
76 #include "scu_completion_codes.h"
77 #include "scu_constants.h"
78 #include "scu_task_context.h"
79
80 #if !defined(DISABLE_ATAPI)
81 #include "scic_sds_stp_packet_request.h"
82 #endif
83
84 /*
85  * ****************************************************************************
86  * * SCIC SDS IO REQUEST CONSTANTS
87  * **************************************************************************** */
88
89 /**
90  *
91  *
92  * We have no timer requirements for IO requests right now
93  */
94 #define SCIC_SDS_IO_REQUEST_MINIMUM_TIMER_COUNT (0)
95 #define SCIC_SDS_IO_REQUEST_MAXIMUM_TIMER_COUNT (0)
96
97 /*
98  * ****************************************************************************
99  * * SCIC SDS IO REQUEST MACROS
100  * **************************************************************************** */
101
102 /**
103  * scic_ssp_io_request_get_object_size() -
104  *
105  * This macro returns the sizeof memory required to store the an SSP IO
106  * request.  This does not include the size of the SGL or SCU Task Context
107  * memory.
108  */
109 #define scic_ssp_io_request_get_object_size() \
110         (\
111                 sizeof(struct sci_ssp_command_iu) \
112                 + sizeof(struct sci_ssp_response_iu)    \
113         )
114
115 /**
116  * scic_sds_ssp_request_get_command_buffer() -
117  *
118  * This macro returns the address of the ssp command buffer in the io request
119  * memory
120  */
121 #define scic_sds_ssp_request_get_command_buffer(memory) \
122         ((struct sci_ssp_command_iu *)(\
123                  ((char *)(memory)) + sizeof(struct scic_sds_request) \
124                  ))
125
126 /**
127  * scic_sds_ssp_request_get_response_buffer() -
128  *
129  * This macro returns the address of the ssp response buffer in the io request
130  * memory
131  */
132 #define scic_sds_ssp_request_get_response_buffer(memory) \
133         ((struct sci_ssp_response_iu *)(\
134                  ((char *)(scic_sds_ssp_request_get_command_buffer(memory))) \
135                  + sizeof(struct sci_ssp_command_iu)    \
136                  ))
137
138 /**
139  * scic_sds_ssp_request_get_task_context_buffer() -
140  *
141  * This macro returns the address of the task context buffer in the io request
142  * memory
143  */
144 #define scic_sds_ssp_request_get_task_context_buffer(memory) \
145         ((struct scu_task_context *)(\
146                  ((char *)(scic_sds_ssp_request_get_response_buffer(memory))) \
147                  + sizeof(struct sci_ssp_response_iu) \
148                  ))
149
150 /**
151  * scic_sds_ssp_request_get_sgl_element_buffer() -
152  *
153  * This macro returns the address of the sgl elment pairs in the io request
154  * memory buffer
155  */
156 #define scic_sds_ssp_request_get_sgl_element_buffer(memory) \
157         ((struct scu_sgl_element_pair *)(\
158                  ((char *)(scic_sds_ssp_request_get_task_context_buffer(memory))) \
159                  + sizeof(struct scu_task_context) \
160                  ))
161
162
163 /**
164  * scic_ssp_task_request_get_object_size() -
165  *
166  * This macro returns the sizeof of memory required to store an SSP Task
167  * request.  This does not include the size of the SCU Task Context memory.
168  */
169 #define scic_ssp_task_request_get_object_size() \
170         (\
171                 sizeof(struct sci_ssp_task_iu) \
172                 + sizeof(struct sci_ssp_response_iu)    \
173         )
174
175 /**
176  * scic_sds_ssp_task_request_get_command_buffer() -
177  *
178  * This macro returns the address of the ssp command buffer in the task request
179  * memory.  Yes its the same as the above macro except for the name.
180  */
181 #define scic_sds_ssp_task_request_get_command_buffer(memory) \
182         ((struct sci_ssp_task_iu *)(\
183                  ((char *)(memory)) + sizeof(struct scic_sds_request) \
184                  ))
185
186 /**
187  * scic_sds_ssp_task_request_get_response_buffer() -
188  *
189  * This macro returns the address of the ssp response buffer in the task
190  * request memory.
191  */
192 #define scic_sds_ssp_task_request_get_response_buffer(memory) \
193         ((struct sci_ssp_response_iu *)(\
194                  ((char *)(scic_sds_ssp_task_request_get_command_buffer(memory))) \
195                  + sizeof(struct sci_ssp_task_iu) \
196                  ))
197
198 /**
199  * scic_sds_ssp_task_request_get_task_context_buffer() -
200  *
201  * This macro returs the task context buffer for the SSP task request.
202  */
203 #define scic_sds_ssp_task_request_get_task_context_buffer(memory) \
204         ((struct scu_task_context *)(\
205                  ((char *)(scic_sds_ssp_task_request_get_response_buffer(memory))) \
206                  + sizeof(struct sci_ssp_response_iu) \
207                  ))
208
209
210
211 /*
212  * ****************************************************************************
213  * * SCIC SDS IO REQUEST PRIVATE METHODS
214  * **************************************************************************** */
215
216 /**
217  *
218  *
219  * This method returns the size required to store an SSP IO request object. u32
220  */
221 static u32 scic_sds_ssp_request_get_object_size(void)
222 {
223         return sizeof(struct scic_sds_request)
224                + scic_ssp_io_request_get_object_size()
225                + sizeof(struct scu_task_context)
226                + CACHE_LINE_SIZE
227                + sizeof(struct scu_sgl_element_pair) * SCU_MAX_SGL_ELEMENT_PAIRS;
228 }
229
230 /**
231  * This method returns the sgl element pair for the specificed sgl_pair index.
232  * @this_request: This parameter specifies the IO request for which to retrieve
233  *    the Scatter-Gather List element pair.
234  * @sgl_pair_index: This parameter specifies the index into the SGL element
235  *    pair to be retrieved.
236  *
237  * This method returns a pointer to an struct scu_sgl_element_pair.
238  */
239 static struct scu_sgl_element_pair *scic_sds_request_get_sgl_element_pair(
240         struct scic_sds_request *this_request,
241         u32 sgl_pair_index
242         ) {
243         struct scu_task_context *task_context;
244
245         task_context = (struct scu_task_context *)this_request->task_context_buffer;
246
247         if (sgl_pair_index == 0) {
248                 return &task_context->sgl_pair_ab;
249         } else if (sgl_pair_index == 1) {
250                 return &task_context->sgl_pair_cd;
251         }
252
253         return &this_request->sgl_element_pair_buffer[sgl_pair_index - 2];
254 }
255
256 /**
257  * This function will build the SGL list for an IO request.
258  * @this_request: This parameter specifies the IO request for which to build
259  *    the Scatter-Gather List.
260  *
261  */
262 void scic_sds_request_build_sgl(
263         struct scic_sds_request *this_request)
264 {
265         void *os_sge;
266         void *os_handle;
267         dma_addr_t physical_address;
268         u32 sgl_pair_index = 0;
269         struct scu_sgl_element_pair *scu_sgl_list   = NULL;
270         struct scu_sgl_element_pair *previous_pair  = NULL;
271
272         os_handle = scic_sds_request_get_user_request(this_request);
273         scic_cb_io_request_get_next_sge(os_handle, NULL, &os_sge);
274
275         while (os_sge != NULL) {
276                 scu_sgl_list =
277                         scic_sds_request_get_sgl_element_pair(this_request, sgl_pair_index);
278
279                 SCU_SGL_COPY(os_handle, scu_sgl_list->A, os_sge);
280
281                 scic_cb_io_request_get_next_sge(os_handle, os_sge, &os_sge);
282
283                 if (os_sge != NULL) {
284                         SCU_SGL_COPY(os_handle, scu_sgl_list->B, os_sge);
285
286                         scic_cb_io_request_get_next_sge(os_handle, os_sge, &os_sge);
287                 } else {
288                         SCU_SGL_ZERO(scu_sgl_list->B);
289                 }
290
291                 if (previous_pair != NULL) {
292                         scic_cb_io_request_get_physical_address(
293                                 scic_sds_request_get_controller(this_request),
294                                 this_request,
295                                 scu_sgl_list,
296                                 &physical_address
297                                 );
298
299                         previous_pair->next_pair_upper =
300                                 upper_32_bits(physical_address);
301                         previous_pair->next_pair_lower =
302                                 lower_32_bits(physical_address);
303                 }
304
305                 previous_pair = scu_sgl_list;
306                 sgl_pair_index++;
307         }
308
309         if (scu_sgl_list != NULL) {
310                 scu_sgl_list->next_pair_upper = 0;
311                 scu_sgl_list->next_pair_lower = 0;
312         }
313 }
314
315 /**
316  * This method initializes common portions of the io request object. This
317  *    includes construction of the struct sci_base_request parent.
318  * @the_controller: This parameter specifies the controller for which the
319  *    request is being constructed.
320  * @the_target: This parameter specifies the remote device for which the
321  *    request is being constructed.
322  * @io_tag: This parameter specifies the IO tag to be utilized for this
323  *    request.  This parameter can be set to SCI_CONTROLLER_INVALID_IO_TAG.
324  * @user_io_request_object: This parameter specifies the user request object
325  *    for which the request is being constructed.
326  * @this_request: This parameter specifies the request being constructed.
327  *
328  */
329 static void scic_sds_general_request_construct(
330         struct scic_sds_controller *the_controller,
331         struct scic_sds_remote_device *the_target,
332         u16 io_tag,
333         void *user_io_request_object,
334         struct scic_sds_request *this_request)
335 {
336         sci_base_request_construct(
337                 &this_request->parent,
338                 scic_sds_request_state_table
339                 );
340
341         this_request->io_tag = io_tag;
342         this_request->user_request = user_io_request_object;
343         this_request->owning_controller = the_controller;
344         this_request->target_device = the_target;
345         this_request->has_started_substate_machine = false;
346         this_request->protocol = SCIC_NO_PROTOCOL;
347         this_request->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
348         this_request->device_sequence = scic_sds_remote_device_get_sequence(the_target);
349
350         this_request->sci_status   = SCI_SUCCESS;
351         this_request->scu_status   = 0;
352         this_request->post_context = 0xFFFFFFFF;
353
354         this_request->is_task_management_request = false;
355
356         if (io_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
357                 this_request->was_tag_assigned_by_user = false;
358                 this_request->task_context_buffer = NULL;
359         } else {
360                 this_request->was_tag_assigned_by_user = true;
361
362                 this_request->task_context_buffer =
363                         scic_sds_controller_get_task_context_buffer(
364                                 this_request->owning_controller, io_tag);
365         }
366 }
367
368 /**
369  * This method build the remainder of the IO request object.
370  * @this_request: This parameter specifies the request object being constructed.
371  *
372  * The scic_sds_general_request_construct() must be called before this call is
373  * valid. none
374  */
375 static void scic_sds_ssp_io_request_assign_buffers(
376         struct scic_sds_request *this_request)
377 {
378         this_request->command_buffer =
379                 scic_sds_ssp_request_get_command_buffer(this_request);
380         this_request->response_buffer =
381                 scic_sds_ssp_request_get_response_buffer(this_request);
382         this_request->sgl_element_pair_buffer =
383                 scic_sds_ssp_request_get_sgl_element_buffer(this_request);
384         this_request->sgl_element_pair_buffer =
385                 scic_sds_request_align_sgl_element_buffer(this_request->sgl_element_pair_buffer);
386
387         if (this_request->was_tag_assigned_by_user == false) {
388                 this_request->task_context_buffer =
389                         scic_sds_ssp_request_get_task_context_buffer(this_request);
390                 this_request->task_context_buffer =
391                         scic_sds_request_align_task_context_buffer(this_request->task_context_buffer);
392         }
393 }
394
395 /**
396  * This method constructs the SSP Command IU data for this io request object.
397  * @this_request: This parameter specifies the request object for which the SSP
398  *    command information unit is being built.
399  *
400  */
401 static void scic_sds_io_request_build_ssp_command_iu(
402         struct scic_sds_request *this_request)
403 {
404         struct sci_ssp_command_iu *command_frame;
405         void *os_handle;
406         u32 cdb_length;
407         u32 *cdb_buffer;
408
409         command_frame =
410                 (struct sci_ssp_command_iu *)this_request->command_buffer;
411
412         os_handle = scic_sds_request_get_user_request(this_request);
413
414         command_frame->lun_upper = 0;
415         command_frame->lun_lower = scic_cb_ssp_io_request_get_lun(os_handle);
416
417         ((u32 *)command_frame)[2] = 0;
418
419         cdb_length = scic_cb_ssp_io_request_get_cdb_length(os_handle);
420         cdb_buffer = (u32 *)scic_cb_ssp_io_request_get_cdb_address(os_handle);
421
422         if (cdb_length > 16) {
423                 command_frame->additional_cdb_length = cdb_length - 16;
424         }
425
426         /* / @todo Is it ok to leave junk at the end of the cdb buffer? */
427         scic_word_copy_with_swap(
428                 (u32 *)(&command_frame->cdb),
429                 (u32 *)(cdb_buffer),
430                 (cdb_length + 3) / sizeof(u32)
431                 );
432
433         command_frame->enable_first_burst = 0;
434         command_frame->task_priority =
435                 scic_cb_ssp_io_request_get_command_priority(os_handle);
436         command_frame->task_attribute =
437                 scic_cb_ssp_io_request_get_task_attribute(os_handle);
438 }
439
440
441 /**
442  * This method constructs the SSP Task IU data for this io request object.
443  * @this_request:
444  *
445  */
446 static void scic_sds_task_request_build_ssp_task_iu(
447         struct scic_sds_request *this_request)
448 {
449         struct sci_ssp_task_iu *command_frame;
450         void *os_handle;
451
452         command_frame =
453                 (struct sci_ssp_task_iu *)this_request->command_buffer;
454
455         os_handle = scic_sds_request_get_user_request(this_request);
456
457         command_frame->lun_upper = 0;
458         command_frame->lun_lower = scic_cb_ssp_task_request_get_lun(os_handle);
459
460         ((u32 *)command_frame)[2] = 0;
461
462         command_frame->task_function =
463                 scic_cb_ssp_task_request_get_function(os_handle);
464         command_frame->task_tag =
465                 scic_cb_ssp_task_request_get_io_tag_to_manage(os_handle);
466 }
467
468
469 /**
470  * This method is will fill in the SCU Task Context for any type of SSP request.
471  * @this_request:
472  * @task_context:
473  *
474  */
475 static void scu_ssp_reqeust_construct_task_context(
476         struct scic_sds_request *this_request,
477         struct scu_task_context *task_context)
478 {
479         dma_addr_t physical_address;
480         struct scic_sds_controller *owning_controller;
481         struct scic_sds_remote_device *target_device;
482         struct scic_sds_port *target_port;
483
484         owning_controller = scic_sds_request_get_controller(this_request);
485         target_device = scic_sds_request_get_device(this_request);
486         target_port = scic_sds_request_get_port(this_request);
487
488         /* Fill in the TC with the its required data */
489         task_context->abort = 0;
490         task_context->priority = 0;
491         task_context->initiator_request = 1;
492         task_context->connection_rate =
493                 scic_remote_device_get_connection_rate(target_device);
494         task_context->protocol_engine_index =
495                 scic_sds_controller_get_protocol_engine_group(owning_controller);
496         task_context->logical_port_index =
497                 scic_sds_port_get_index(target_port);
498         task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
499         task_context->valid = SCU_TASK_CONTEXT_VALID;
500         task_context->context_type = SCU_TASK_CONTEXT_TYPE;
501
502         task_context->remote_node_index =
503                 scic_sds_remote_device_get_index(this_request->target_device);
504         task_context->command_code = 0;
505
506         task_context->link_layer_control = 0;
507         task_context->do_not_dma_ssp_good_response = 1;
508         task_context->strict_ordering = 0;
509         task_context->control_frame = 0;
510         task_context->timeout_enable = 0;
511         task_context->block_guard_enable = 0;
512
513         task_context->address_modifier = 0;
514
515         /* task_context->type.ssp.tag = this_request->io_tag; */
516         task_context->task_phase = 0x01;
517
518         if (this_request->was_tag_assigned_by_user) {
519                 /* Build the task context now since we have already read the data */
520                 this_request->post_context = (
521                         SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC
522                         | (
523                                 scic_sds_controller_get_protocol_engine_group(owning_controller)
524                                 << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT
525                                 )
526                         | (
527                                 scic_sds_port_get_index(target_port)
528                                 << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT
529                                 )
530                         | scic_sds_io_tag_get_index(this_request->io_tag)
531                         );
532         } else {
533                 /* Build the task context now since we have already read the data */
534                 this_request->post_context = (
535                         SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC
536                         | (
537                                 scic_sds_controller_get_protocol_engine_group(owning_controller)
538                                 << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT
539                                 )
540                         | (
541                                 scic_sds_port_get_index(target_port)
542                                 << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT
543                                 )
544                         /* This is not assigned because we have to wait until we get a TCi */
545                         );
546         }
547
548         /* Copy the physical address for the command buffer to the SCU Task Context */
549         scic_cb_io_request_get_physical_address(
550                 scic_sds_request_get_controller(this_request),
551                 this_request,
552                 this_request->command_buffer,
553                 &physical_address
554                 );
555
556         task_context->command_iu_upper =
557                 upper_32_bits(physical_address);
558         task_context->command_iu_lower =
559                 lower_32_bits(physical_address);
560
561         /* Copy the physical address for the response buffer to the SCU Task Context */
562         scic_cb_io_request_get_physical_address(
563                 scic_sds_request_get_controller(this_request),
564                 this_request,
565                 this_request->response_buffer,
566                 &physical_address
567                 );
568
569         task_context->response_iu_upper =
570                 upper_32_bits(physical_address);
571         task_context->response_iu_lower =
572                 lower_32_bits(physical_address);
573 }
574
575 /**
576  * This method is will fill in the SCU Task Context for a SSP IO request.
577  * @this_request:
578  *
579  */
580 static void scu_ssp_io_request_construct_task_context(
581         struct scic_sds_request *sci_req,
582         enum dma_data_direction dir,
583         u32 len)
584 {
585         struct scu_task_context *task_context;
586
587         task_context = scic_sds_request_get_task_context(sci_req);
588
589         scu_ssp_reqeust_construct_task_context(sci_req, task_context);
590
591         task_context->ssp_command_iu_length = sizeof(struct sci_ssp_command_iu) / sizeof(u32);
592         task_context->type.ssp.frame_type = SCI_SAS_COMMAND_FRAME;
593
594         switch (dir) {
595         case DMA_FROM_DEVICE:
596         case DMA_NONE:
597         default:
598                 task_context->task_type = SCU_TASK_TYPE_IOREAD;
599                 break;
600         case DMA_TO_DEVICE:
601                 task_context->task_type = SCU_TASK_TYPE_IOWRITE;
602                 break;
603         }
604
605         task_context->transfer_length_bytes = len;
606
607         if (task_context->transfer_length_bytes > 0)
608                 scic_sds_request_build_sgl(sci_req);
609 }
610
611
612 /**
613  * This method will fill in the remainder of the io request object for SSP Task
614  *    requests.
615  * @this_request:
616  *
617  */
618 static void scic_sds_ssp_task_request_assign_buffers(
619         struct scic_sds_request *this_request)
620 {
621         /* Assign all of the buffer pointers */
622         this_request->command_buffer =
623                 scic_sds_ssp_task_request_get_command_buffer(this_request);
624         this_request->response_buffer =
625                 scic_sds_ssp_task_request_get_response_buffer(this_request);
626         this_request->sgl_element_pair_buffer = NULL;
627
628         if (this_request->was_tag_assigned_by_user == false) {
629                 this_request->task_context_buffer =
630                         scic_sds_ssp_task_request_get_task_context_buffer(this_request);
631                 this_request->task_context_buffer =
632                         scic_sds_request_align_task_context_buffer(this_request->task_context_buffer);
633         }
634 }
635
636 /**
637  * This method will fill in the SCU Task Context for a SSP Task request.  The
638  *    following important settings are utilized: -# priority ==
639  *    SCU_TASK_PRIORITY_HIGH.  This ensures that the task request is issued
640  *    ahead of other task destined for the same Remote Node. -# task_type ==
641  *    SCU_TASK_TYPE_IOREAD.  This simply indicates that a normal request type
642  *    (i.e. non-raw frame) is being utilized to perform task management. -#
643  *    control_frame == 1.  This ensures that the proper endianess is set so
644  *    that the bytes are transmitted in the right order for a task frame.
645  * @this_request: This parameter specifies the task request object being
646  *    constructed.
647  *
648  */
649 static void scu_ssp_task_request_construct_task_context(
650         struct scic_sds_request *this_request)
651 {
652         struct scu_task_context *task_context;
653
654         task_context = scic_sds_request_get_task_context(this_request);
655
656         scu_ssp_reqeust_construct_task_context(this_request, task_context);
657
658         task_context->control_frame                = 1;
659         task_context->priority                     = SCU_TASK_PRIORITY_HIGH;
660         task_context->task_type                    = SCU_TASK_TYPE_RAW_FRAME;
661         task_context->transfer_length_bytes        = 0;
662         task_context->type.ssp.frame_type          = SCI_SAS_TASK_FRAME;
663         task_context->ssp_command_iu_length = sizeof(struct sci_ssp_task_iu) / sizeof(u32);
664 }
665
666
667 /**
668  * This method constructs the SSP Command IU data for this ssp passthrough
669  *    comand request object.
670  * @this_request: This parameter specifies the request object for which the SSP
671  *    command information unit is being built.
672  *
673  * enum sci_status, returns invalid parameter is cdb > 16
674  */
675
676
677 /**
678  * This method constructs the SATA request object.
679  * @this_request:
680  * @sat_protocol:
681  * @transfer_length:
682  * @data_direction:
683  * @copy_rx_frame:
684  *
685  * enum sci_status
686  */
687 static enum sci_status scic_io_request_construct_sata(struct scic_sds_request *sci_req,
688                                                       u8 proto, u32 len,
689                                                       enum dma_data_direction dir,
690                                                       bool copy)
691 {
692         enum sci_status status = SCI_SUCCESS;
693
694         switch (proto) {
695         case SAT_PROTOCOL_PIO_DATA_IN:
696         case SAT_PROTOCOL_PIO_DATA_OUT:
697                 status = scic_sds_stp_pio_request_construct(sci_req, proto, copy);
698                 break;
699
700         case SAT_PROTOCOL_UDMA_DATA_IN:
701         case SAT_PROTOCOL_UDMA_DATA_OUT:
702                 status = scic_sds_stp_udma_request_construct(sci_req, len, dir);
703                 break;
704
705         case SAT_PROTOCOL_ATA_HARD_RESET:
706         case SAT_PROTOCOL_SOFT_RESET:
707                 status = scic_sds_stp_soft_reset_request_construct(sci_req);
708                 break;
709
710         case SAT_PROTOCOL_NON_DATA:
711                 status = scic_sds_stp_non_data_request_construct(sci_req);
712                 break;
713
714         case SAT_PROTOCOL_FPDMA:
715                 status = scic_sds_stp_ncq_request_construct(sci_req, len, dir);
716                 break;
717
718 #if !defined(DISABLE_ATAPI)
719         case SAT_PROTOCOL_PACKET_NON_DATA:
720         case SAT_PROTOCOL_PACKET_DMA_DATA_IN:
721         case SAT_PROTOCOL_PACKET_DMA_DATA_OUT:
722         case SAT_PROTOCOL_PACKET_PIO_DATA_IN:
723         case SAT_PROTOCOL_PACKET_PIO_DATA_OUT:
724                 status = scic_sds_stp_packet_request_construct(sci_req);
725                 break;
726 #endif
727
728         case SAT_PROTOCOL_DMA_QUEUED:
729         case SAT_PROTOCOL_DMA:
730         case SAT_PROTOCOL_DEVICE_DIAGNOSTIC:
731         case SAT_PROTOCOL_DEVICE_RESET:
732         case SAT_PROTOCOL_RETURN_RESPONSE_INFO:
733         default:
734                 dev_err(scic_to_dev(sci_req->owning_controller),
735                         "%s: SCIC IO Request 0x%p received un-handled "
736                         "SAT Protocl %d.\n",
737                         __func__, sci_req, proto);
738
739                 status = SCI_FAILURE;
740                 break;
741         }
742
743         return status;
744 }
745
746 /*
747  * ****************************************************************************
748  * * SCIC Interface Implementation
749  * **************************************************************************** */
750
751
752
753
754 /* --------------------------------------------------------------------------- */
755
756 u32 scic_io_request_get_object_size(void)
757 {
758         u32 ssp_request_size;
759         u32 stp_request_size;
760         u32 smp_request_size;
761
762         ssp_request_size = scic_sds_ssp_request_get_object_size();
763         stp_request_size = scic_sds_stp_request_get_object_size();
764         smp_request_size = scic_sds_smp_request_get_object_size();
765
766         return max(ssp_request_size, max(stp_request_size, smp_request_size));
767 }
768
769 /* --------------------------------------------------------------------------- */
770
771
772 /* --------------------------------------------------------------------------- */
773
774
775 /* --------------------------------------------------------------------------- */
776
777
778 /* --------------------------------------------------------------------------- */
779
780 enum sci_status scic_io_request_construct(
781         struct scic_sds_controller *scic_controller,
782         struct scic_sds_remote_device *scic_remote_device,
783         u16 io_tag,
784         void *user_io_request_object,
785         void *scic_io_request_memory,
786         struct scic_sds_request **new_scic_io_request_handle)
787 {
788         enum sci_status status = SCI_SUCCESS;
789         struct scic_sds_request *this_request;
790         struct smp_discover_response_protocols device_protocol;
791
792         this_request = (struct scic_sds_request *)scic_io_request_memory;
793
794         /* Build the common part of the request */
795         scic_sds_general_request_construct(
796                 (struct scic_sds_controller *)scic_controller,
797                 (struct scic_sds_remote_device *)scic_remote_device,
798                 io_tag,
799                 user_io_request_object,
800                 this_request
801                 );
802
803         if (
804                 scic_sds_remote_device_get_index((struct scic_sds_remote_device *)scic_remote_device)
805                 == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX
806                 ) {
807                 return SCI_FAILURE_INVALID_REMOTE_DEVICE;
808         }
809
810         scic_remote_device_get_protocols(scic_remote_device, &device_protocol);
811
812         if (device_protocol.u.bits.attached_ssp_target) {
813                 scic_sds_ssp_io_request_assign_buffers(this_request);
814         } else if (device_protocol.u.bits.attached_stp_target) {
815                 scic_sds_stp_request_assign_buffers(this_request);
816                 memset(this_request->command_buffer, 0, sizeof(struct sata_fis_reg_h2d));
817         } else if (device_protocol.u.bits.attached_smp_target) {
818                 scic_sds_smp_request_assign_buffers(this_request);
819                 memset(this_request->command_buffer, 0, sizeof(struct smp_request));
820         } else {
821                 status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
822         }
823
824         if (status == SCI_SUCCESS) {
825                 memset(
826                         this_request->task_context_buffer,
827                         0,
828                         SCI_FIELD_OFFSET(struct scu_task_context, sgl_pair_ab)
829                         );
830                 *new_scic_io_request_handle = scic_io_request_memory;
831         }
832
833         return status;
834 }
835
836 /* --------------------------------------------------------------------------- */
837
838
839 enum sci_status scic_task_request_construct(
840         struct scic_sds_controller *controller,
841         struct scic_sds_remote_device *remote_device,
842         u16 io_tag,
843         void *user_io_request_object,
844         void *scic_task_request_memory,
845         struct scic_sds_request **new_scic_task_request_handle)
846 {
847         enum sci_status status = SCI_SUCCESS;
848         struct scic_sds_request *this_request = (struct scic_sds_request *)
849                                            scic_task_request_memory;
850         struct smp_discover_response_protocols device_protocol;
851
852         /* Build the common part of the request */
853         scic_sds_general_request_construct(
854                 (struct scic_sds_controller *)controller,
855                 (struct scic_sds_remote_device *)remote_device,
856                 io_tag,
857                 user_io_request_object,
858                 this_request
859                 );
860
861         scic_remote_device_get_protocols(remote_device, &device_protocol);
862
863         if (device_protocol.u.bits.attached_ssp_target) {
864                 scic_sds_ssp_task_request_assign_buffers(this_request);
865
866                 this_request->has_started_substate_machine = true;
867
868                 /* Construct the started sub-state machine. */
869                 sci_base_state_machine_construct(
870                         &this_request->started_substate_machine,
871                         &this_request->parent.parent,
872                         scic_sds_io_request_started_task_mgmt_substate_table,
873                         SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION
874                         );
875         } else if (device_protocol.u.bits.attached_stp_target) {
876                 scic_sds_stp_request_assign_buffers(this_request);
877         } else {
878                 status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
879         }
880
881         if (status == SCI_SUCCESS) {
882                 this_request->is_task_management_request = true;
883                 memset(this_request->task_context_buffer, 0x00, sizeof(struct scu_task_context));
884                 *new_scic_task_request_handle            = scic_task_request_memory;
885         }
886
887         return status;
888 }
889
890
891 enum sci_status scic_io_request_construct_basic_ssp(
892         struct scic_sds_request *sci_req)
893 {
894         void *os_handle;
895
896         sci_req->protocol = SCIC_SSP_PROTOCOL;
897
898         os_handle = scic_sds_request_get_user_request(sci_req);
899
900         scu_ssp_io_request_construct_task_context(
901                 sci_req,
902                 scic_cb_io_request_get_data_direction(os_handle),
903                 scic_cb_io_request_get_transfer_length(os_handle)
904                 );
905
906
907         scic_sds_io_request_build_ssp_command_iu(sci_req);
908
909         sci_base_state_machine_change_state(
910                 &sci_req->parent.state_machine,
911                 SCI_BASE_REQUEST_STATE_CONSTRUCTED
912                 );
913
914         return SCI_SUCCESS;
915 }
916
917
918 enum sci_status scic_task_request_construct_ssp(
919         struct scic_sds_request *sci_req)
920 {
921         /* Construct the SSP Task SCU Task Context */
922         scu_ssp_task_request_construct_task_context(sci_req);
923
924         /* Fill in the SSP Task IU */
925         scic_sds_task_request_build_ssp_task_iu(sci_req);
926
927         sci_base_state_machine_change_state(
928                 &sci_req->parent.state_machine,
929                 SCI_BASE_REQUEST_STATE_CONSTRUCTED
930                 );
931
932         return SCI_SUCCESS;
933 }
934
935
936 enum sci_status scic_io_request_construct_basic_sata(struct scic_sds_request *sci_req)
937 {
938         enum sci_status status;
939         struct scic_sds_stp_request *stp_req;
940         u8 proto;
941         u32 len;
942         enum dma_data_direction dir;
943         bool copy = false;
944
945         stp_req = container_of(sci_req, typeof(*stp_req), parent);
946
947         sci_req->protocol = SCIC_STP_PROTOCOL;
948
949         len = scic_cb_io_request_get_transfer_length(sci_req->user_request);
950         dir = scic_cb_io_request_get_data_direction(sci_req->user_request);
951         proto = scic_cb_request_get_sat_protocol(sci_req->user_request);
952         copy = scic_cb_io_request_do_copy_rx_frames(stp_req->parent.user_request);
953
954         status = scic_io_request_construct_sata(sci_req, proto, len, dir, copy);
955
956         if (status == SCI_SUCCESS)
957                 sci_base_state_machine_change_state(
958                         &sci_req->parent.state_machine,
959                         SCI_BASE_REQUEST_STATE_CONSTRUCTED
960                         );
961
962         return status;
963 }
964
965
966 enum sci_status scic_task_request_construct_sata(
967         struct scic_sds_request *sci_req)
968 {
969         enum sci_status status;
970         u8 sat_protocol = scic_cb_request_get_sat_protocol(sci_req->user_request);
971
972         switch (sat_protocol) {
973         case SAT_PROTOCOL_ATA_HARD_RESET:
974         case SAT_PROTOCOL_SOFT_RESET:
975                 status = scic_sds_stp_soft_reset_request_construct(sci_req);
976                 break;
977
978         default:
979                 dev_err(scic_to_dev(sci_req->owning_controller),
980                         "%s: SCIC IO Request 0x%p received un-handled SAT "
981                         "Protocl %d.\n",
982                         __func__,
983                         sci_req,
984                         sat_protocol);
985
986                 status = SCI_FAILURE;
987                 break;
988         }
989
990         if (status == SCI_SUCCESS)
991                 sci_base_state_machine_change_state(
992                         &sci_req->parent.state_machine,
993                         SCI_BASE_REQUEST_STATE_CONSTRUCTED
994                         );
995
996         return status;
997 }
998
999
1000 u16 scic_io_request_get_io_tag(
1001         struct scic_sds_request *sci_req)
1002 {
1003         return sci_req->io_tag;
1004 }
1005
1006
1007 u32 scic_request_get_controller_status(
1008         struct scic_sds_request *sci_req)
1009 {
1010         return sci_req->scu_status;
1011 }
1012
1013
1014 void *scic_io_request_get_command_iu_address(
1015         struct scic_sds_request *sci_req)
1016 {
1017         return sci_req->command_buffer;
1018 }
1019
1020
1021 void *scic_io_request_get_response_iu_address(
1022         struct scic_sds_request *sci_req)
1023 {
1024         return sci_req->response_buffer;
1025 }
1026
1027
1028 #define SCU_TASK_CONTEXT_SRAM 0x200000
1029 u32 scic_io_request_get_number_of_bytes_transferred(
1030         struct scic_sds_request *scic_sds_request)
1031 {
1032         u32 ret_val = 0;
1033
1034         if (SMU_AMR_READ(scic_sds_request->owning_controller) == 0) {
1035                 /*
1036                  * get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
1037                  *   BAR1 is the scu_registers
1038                  *   0x20002C = 0x200000 + 0x2c
1039                  *            = start of task context SRAM + offset of (type.ssp.data_offset)
1040                  *   TCi is the io_tag of struct scic_sds_request */
1041                 ret_val =  scic_sds_pci_read_scu_dword(
1042                         scic_sds_request->owning_controller,
1043                         (
1044                                 (u8 *)scic_sds_request->owning_controller->scu_registers +
1045                                 (SCU_TASK_CONTEXT_SRAM + SCI_FIELD_OFFSET(struct scu_task_context, type.ssp.data_offset)) +
1046                                 ((sizeof(struct scu_task_context)) * scic_sds_io_tag_get_index(scic_sds_request->io_tag))
1047                         )
1048                         );
1049         }
1050
1051         return ret_val;
1052 }
1053
1054
1055 /*
1056  * ****************************************************************************
1057  * * SCIC SDS Interface Implementation
1058  * **************************************************************************** */
1059
1060 /**
1061  *
1062  * @this_request: The SCIC_SDS_IO_REQUEST_T object for which the start
1063  *    operation is to be executed.
1064  *
1065  * This method invokes the base state start request handler for the
1066  * SCIC_SDS_IO_REQUEST_T object. enum sci_status
1067  */
1068 enum sci_status scic_sds_request_start(
1069         struct scic_sds_request *this_request)
1070 {
1071         if (
1072                 this_request->device_sequence
1073                 == scic_sds_remote_device_get_sequence(this_request->target_device)
1074                 ) {
1075                 return this_request->state_handlers->parent.start_handler(
1076                                &this_request->parent
1077                                );
1078         }
1079
1080         return SCI_FAILURE;
1081 }
1082
1083 /**
1084  *
1085  * @this_request: The SCIC_SDS_IO_REQUEST_T object for which the start
1086  *    operation is to be executed.
1087  *
1088  * This method invokes the base state terminate request handber for the
1089  * SCIC_SDS_IO_REQUEST_T object. enum sci_status
1090  */
1091 enum sci_status scic_sds_io_request_terminate(
1092         struct scic_sds_request *this_request)
1093 {
1094         return this_request->state_handlers->parent.abort_handler(
1095                        &this_request->parent);
1096 }
1097
1098 /**
1099  *
1100  * @this_request: The SCIC_SDS_IO_REQUEST_T object for which the start
1101  *    operation is to be executed.
1102  *
1103  * This method invokes the base state request completion handler for the
1104  * SCIC_SDS_IO_REQUEST_T object. enum sci_status
1105  */
1106 enum sci_status scic_sds_io_request_complete(
1107         struct scic_sds_request *this_request)
1108 {
1109         return this_request->state_handlers->parent.complete_handler(
1110                        &this_request->parent);
1111 }
1112
1113 /**
1114  *
1115  * @this_request: The SCIC_SDS_IO_REQUEST_T object for which the start
1116  *    operation is to be executed.
1117  * @event_code: The event code returned by the hardware for the task reqeust.
1118  *
1119  * This method invokes the core state handler for the SCIC_SDS_IO_REQUEST_T
1120  * object. enum sci_status
1121  */
1122 enum sci_status scic_sds_io_request_event_handler(
1123         struct scic_sds_request *this_request,
1124         u32 event_code)
1125 {
1126         return this_request->state_handlers->event_handler(this_request, event_code);
1127 }
1128
1129 /**
1130  *
1131  * @this_request: The SCIC_SDS_IO_REQUEST_T object for which the start
1132  *    operation is to be executed.
1133  * @frame_index: The frame index returned by the hardware for the reqeust
1134  *    object.
1135  *
1136  * This method invokes the core state frame handler for the
1137  * SCIC_SDS_IO_REQUEST_T object. enum sci_status
1138  */
1139 enum sci_status scic_sds_io_request_frame_handler(
1140         struct scic_sds_request *this_request,
1141         u32 frame_index)
1142 {
1143         return this_request->state_handlers->frame_handler(this_request, frame_index);
1144 }
1145
1146 /**
1147  *
1148  * @this_request: The SCIC_SDS_IO_REQUEST_T object for which the task start
1149  *    operation is to be executed.
1150  *
1151  * This method invokes the core state task complete handler for the
1152  * SCIC_SDS_IO_REQUEST_T object. enum sci_status
1153  */
1154
1155 /*
1156  * ****************************************************************************
1157  * * SCIC SDS PROTECTED METHODS
1158  * **************************************************************************** */
1159
1160 /**
1161  * This method copies response data for requests returning response data
1162  *    instead of sense data.
1163  * @this_request: This parameter specifies the request object for which to copy
1164  *    the response data.
1165  *
1166  */
1167 void scic_sds_io_request_copy_response(
1168         struct scic_sds_request *this_request)
1169 {
1170         void *response_buffer;
1171         u32 user_response_length;
1172         u32 core_response_length;
1173         struct sci_ssp_response_iu *ssp_response;
1174
1175         ssp_response = (struct sci_ssp_response_iu *)this_request->response_buffer;
1176
1177         response_buffer = scic_cb_ssp_task_request_get_response_data_address(
1178                 this_request->user_request
1179                 );
1180
1181         user_response_length = scic_cb_ssp_task_request_get_response_data_length(
1182                 this_request->user_request
1183                 );
1184
1185         core_response_length = sci_ssp_get_response_data_length(
1186                 ssp_response->response_data_length
1187                 );
1188
1189         user_response_length = min(user_response_length, core_response_length);
1190
1191         memcpy(response_buffer, ssp_response->data, user_response_length);
1192 }
1193
1194 /*
1195  * *****************************************************************************
1196  * *  DEFAULT STATE HANDLERS
1197  * ***************************************************************************** */
1198
1199 /**
1200  * scic_sds_request_default_start_handler() -
1201  * @request: This is the struct sci_base_request object that is cast to the
1202  *    SCIC_SDS_IO_REQUEST_T object for which the start operation is requested.
1203  *
1204  * This method is the default action to take when an SCIC_SDS_IO_REQUEST_T
1205  * object receives a scic_sds_request_start() request.  The default action is
1206  * to log a warning and return a failure status. enum sci_status
1207  * SCI_FAILURE_INVALID_STATE
1208  */
1209 enum sci_status scic_sds_request_default_start_handler(
1210         struct sci_base_request *request)
1211 {
1212         struct scic_sds_request *scic_request =
1213                 (struct scic_sds_request *)request;
1214
1215         dev_warn(scic_to_dev(scic_request->owning_controller),
1216                  "%s: SCIC IO Request requested to start while in wrong "
1217                  "state %d\n",
1218                  __func__,
1219                  sci_base_state_machine_get_state(
1220                          &((struct scic_sds_request *)request)->parent.state_machine));
1221
1222         return SCI_FAILURE_INVALID_STATE;
1223 }
1224
1225 static enum sci_status scic_sds_request_default_abort_handler(
1226         struct sci_base_request *request)
1227 {
1228         struct scic_sds_request *scic_request =
1229                 (struct scic_sds_request *)request;
1230
1231         dev_warn(scic_to_dev(scic_request->owning_controller),
1232                 "%s: SCIC IO Request requested to abort while in wrong "
1233                 "state %d\n",
1234                 __func__,
1235                 sci_base_state_machine_get_state(
1236                         &((struct scic_sds_request *)request)->parent.state_machine));
1237
1238         return SCI_FAILURE_INVALID_STATE;
1239 }
1240
1241 /**
1242  * scic_sds_request_default_complete_handler() -
1243  * @request: This is the struct sci_base_request object that is cast to the
1244  *    SCIC_SDS_IO_REQUEST_T object for which the start operation is requested.
1245  *
1246  * This method is the default action to take when an SCIC_SDS_IO_REQUEST_T
1247  * object receives a scic_sds_request_complete() request.  The default action
1248  * is to log a warning and return a failure status. enum sci_status
1249  * SCI_FAILURE_INVALID_STATE
1250  */
1251 enum sci_status scic_sds_request_default_complete_handler(
1252         struct sci_base_request *request)
1253 {
1254         struct scic_sds_request *scic_request =
1255                 (struct scic_sds_request *)request;
1256
1257         dev_warn(scic_to_dev(scic_request->owning_controller),
1258                 "%s: SCIC IO Request requested to complete while in wrong "
1259                 "state %d\n",
1260                 __func__,
1261                 sci_base_state_machine_get_state(
1262                         &((struct scic_sds_request *)request)->parent.state_machine));
1263
1264         return SCI_FAILURE_INVALID_STATE;
1265 }
1266
1267 /**
1268  * scic_sds_request_default_destruct_handler() -
1269  * @request: This is the struct sci_base_request object that is cast to the
1270  *    SCIC_SDS_IO_REQUEST_T object for which the start operation is requested.
1271  *
1272  * This method is the default action to take when an SCIC_SDS_IO_REQUEST_T
1273  * object receives a scic_sds_request_complete() request.  The default action
1274  * is to log a warning and return a failure status. enum sci_status
1275  * SCI_FAILURE_INVALID_STATE
1276  */
1277 enum sci_status scic_sds_request_default_destruct_handler(
1278         struct sci_base_request *request)
1279 {
1280         struct scic_sds_request *scic_request =
1281                 (struct scic_sds_request *)request;
1282
1283         dev_warn(scic_to_dev(scic_request->owning_controller),
1284                  "%s: SCIC IO Request requested to destroy while in wrong "
1285                  "state %d\n",
1286                  __func__,
1287                  sci_base_state_machine_get_state(
1288                          &((struct scic_sds_request *)request)->parent.state_machine));
1289
1290         return SCI_FAILURE_INVALID_STATE;
1291 }
1292
1293 /**
1294  * scic_sds_request_default_tc_completion_handler() -
1295  * @request: This is the struct sci_base_request object that is cast to the
1296  *    SCIC_SDS_IO_REQUEST_T object for which the start operation is requested.
1297  *
1298  * This method is the default action to take when an SCIC_SDS_IO_REQUEST_T
1299  * object receives a scic_sds_task_request_complete() request.  The default
1300  * action is to log a warning and return a failure status. enum sci_status
1301  * SCI_FAILURE_INVALID_STATE
1302  */
1303 enum sci_status scic_sds_request_default_tc_completion_handler(
1304         struct scic_sds_request *this_request,
1305         u32 completion_code)
1306 {
1307         dev_warn(scic_to_dev(this_request->owning_controller),
1308                 "%s: SCIC IO Request given task completion notification %x "
1309                 "while in wrong state %d\n",
1310                 __func__,
1311                 completion_code,
1312                 sci_base_state_machine_get_state(
1313                         &this_request->parent.state_machine));
1314
1315         return SCI_FAILURE_INVALID_STATE;
1316
1317 }
1318
1319 /**
1320  * scic_sds_request_default_event_handler() -
1321  * @request: This is the struct sci_base_request object that is cast to the
1322  *    SCIC_SDS_IO_REQUEST_T object for which the start operation is requested.
1323  *
1324  * This method is the default action to take when an SCIC_SDS_IO_REQUEST_T
1325  * object receives a scic_sds_request_event_handler() request.  The default
1326  * action is to log a warning and return a failure status. enum sci_status
1327  * SCI_FAILURE_INVALID_STATE
1328  */
1329 enum sci_status scic_sds_request_default_event_handler(
1330         struct scic_sds_request *this_request,
1331         u32 event_code)
1332 {
1333         dev_warn(scic_to_dev(this_request->owning_controller),
1334                  "%s: SCIC IO Request given event code notification %x while "
1335                  "in wrong state %d\n",
1336                  __func__,
1337                  event_code,
1338                  sci_base_state_machine_get_state(
1339                          &this_request->parent.state_machine));
1340
1341         return SCI_FAILURE_INVALID_STATE;
1342 }
1343
1344 /**
1345  * scic_sds_request_default_frame_handler() -
1346  * @request: This is the struct sci_base_request object that is cast to the
1347  *    SCIC_SDS_IO_REQUEST_T object for which the start operation is requested.
1348  *
1349  * This method is the default action to take when an SCIC_SDS_IO_REQUEST_T
1350  * object receives a scic_sds_request_event_handler() request.  The default
1351  * action is to log a warning and return a failure status. enum sci_status
1352  * SCI_FAILURE_INVALID_STATE
1353  */
1354 enum sci_status scic_sds_request_default_frame_handler(
1355         struct scic_sds_request *this_request,
1356         u32 frame_index)
1357 {
1358         dev_warn(scic_to_dev(this_request->owning_controller),
1359                  "%s: SCIC IO Request given unexpected frame %x while in "
1360                  "state %d\n",
1361                  __func__,
1362                  frame_index,
1363                  sci_base_state_machine_get_state(
1364                          &this_request->parent.state_machine));
1365
1366         scic_sds_controller_release_frame(
1367                 this_request->owning_controller, frame_index);
1368
1369         return SCI_FAILURE_INVALID_STATE;
1370 }
1371
1372 /*
1373  * *****************************************************************************
1374  * *  CONSTRUCTED STATE HANDLERS
1375  * ***************************************************************************** */
1376
1377 /**
1378  * scic_sds_request_constructed_state_start_handler() -
1379  * @request: This is the struct sci_base_request object that is cast to the
1380  *    SCIC_SDS_IO_REQUEST_T object for which the start operation is requested.
1381  *
1382  * This method implements the action taken when a constructed
1383  * SCIC_SDS_IO_REQUEST_T object receives a scic_sds_request_start() request.
1384  * This method will, if necessary, allocate a TCi for the io request object and
1385  * then will, if necessary, copy the constructed TC data into the actual TC
1386  * buffer.  If everything is successful the post context field is updated with
1387  * the TCi so the controller can post the request to the hardware. enum sci_status
1388  * SCI_SUCCESS SCI_FAILURE_INSUFFICIENT_RESOURCES
1389  */
1390 static enum sci_status scic_sds_request_constructed_state_start_handler(
1391         struct sci_base_request *request)
1392 {
1393         struct scu_task_context *task_context;
1394         struct scic_sds_request *this_request = (struct scic_sds_request *)request;
1395
1396         if (this_request->io_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
1397                 this_request->io_tag =
1398                         scic_controller_allocate_io_tag(this_request->owning_controller);
1399         }
1400
1401         /* Record the IO Tag in the request */
1402         if (this_request->io_tag != SCI_CONTROLLER_INVALID_IO_TAG) {
1403                 task_context = this_request->task_context_buffer;
1404
1405                 task_context->task_index = scic_sds_io_tag_get_index(this_request->io_tag);
1406
1407                 switch (task_context->protocol_type) {
1408                 case SCU_TASK_CONTEXT_PROTOCOL_SMP:
1409                 case SCU_TASK_CONTEXT_PROTOCOL_SSP:
1410                         /* SSP/SMP Frame */
1411                         task_context->type.ssp.tag = this_request->io_tag;
1412                         task_context->type.ssp.target_port_transfer_tag = 0xFFFF;
1413                         break;
1414
1415                 case SCU_TASK_CONTEXT_PROTOCOL_STP:
1416                         /*
1417                          * STP/SATA Frame
1418                          * task_context->type.stp.ncq_tag = this_request->ncq_tag; */
1419                         break;
1420
1421                 case SCU_TASK_CONTEXT_PROTOCOL_NONE:
1422                         /* / @todo When do we set no protocol type? */
1423                         break;
1424
1425                 default:
1426                         /* This should never happen since we build the IO requests */
1427                         break;
1428                 }
1429
1430                 /*
1431                  * Check to see if we need to copy the task context buffer
1432                  * or have been building into the task context buffer */
1433                 if (this_request->was_tag_assigned_by_user == false) {
1434                         scic_sds_controller_copy_task_context(
1435                                 this_request->owning_controller, this_request
1436                                 );
1437                 }
1438
1439                 /* Add to the post_context the io tag value */
1440                 this_request->post_context |= scic_sds_io_tag_get_index(this_request->io_tag);
1441
1442                 /* Everything is good go ahead and change state */
1443                 sci_base_state_machine_change_state(
1444                         &this_request->parent.state_machine,
1445                         SCI_BASE_REQUEST_STATE_STARTED
1446                         );
1447
1448                 return SCI_SUCCESS;
1449         }
1450
1451         return SCI_FAILURE_INSUFFICIENT_RESOURCES;
1452 }
1453
1454 /**
1455  * scic_sds_request_constructed_state_abort_handler() -
1456  * @request: This is the struct sci_base_request object that is cast to the
1457  *    SCIC_SDS_IO_REQUEST_T object for which the start operation is requested.
1458  *
1459  * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1460  * object receives a scic_sds_request_terminate() request. Since the request
1461  * has not yet been posted to the hardware the request transitions to the
1462  * completed state. enum sci_status SCI_SUCCESS
1463  */
1464 static enum sci_status scic_sds_request_constructed_state_abort_handler(
1465         struct sci_base_request *request)
1466 {
1467         struct scic_sds_request *this_request = (struct scic_sds_request *)request;
1468
1469         /*
1470          * This request has been terminated by the user make sure that the correct
1471          * status code is returned */
1472         scic_sds_request_set_status(
1473                 this_request,
1474                 SCU_TASK_DONE_TASK_ABORT,
1475                 SCI_FAILURE_IO_TERMINATED
1476                 );
1477
1478         sci_base_state_machine_change_state(
1479                 &this_request->parent.state_machine,
1480                 SCI_BASE_REQUEST_STATE_COMPLETED
1481                 );
1482
1483         return SCI_SUCCESS;
1484 }
1485
1486 /*
1487  * *****************************************************************************
1488  * *  STARTED STATE HANDLERS
1489  * ***************************************************************************** */
1490
1491 /**
1492  * scic_sds_request_started_state_abort_handler() -
1493  * @request: This is the struct sci_base_request object that is cast to the
1494  *    SCIC_SDS_IO_REQUEST_T object for which the start operation is requested.
1495  *
1496  * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1497  * object receives a scic_sds_request_terminate() request. Since the request
1498  * has been posted to the hardware the io request state is changed to the
1499  * aborting state. enum sci_status SCI_SUCCESS
1500  */
1501 enum sci_status scic_sds_request_started_state_abort_handler(
1502         struct sci_base_request *request)
1503 {
1504         struct scic_sds_request *this_request = (struct scic_sds_request *)request;
1505
1506         if (this_request->has_started_substate_machine) {
1507                 sci_base_state_machine_stop(&this_request->started_substate_machine);
1508         }
1509
1510         sci_base_state_machine_change_state(
1511                 &this_request->parent.state_machine,
1512                 SCI_BASE_REQUEST_STATE_ABORTING
1513                 );
1514
1515         return SCI_SUCCESS;
1516 }
1517
1518 /**
1519  * scic_sds_request_started_state_tc_completion_handler() - This method process
1520  *    TC (task context) completions for normal IO request (i.e. Task/Abort
1521  *    Completions of type 0).  This method will update the
1522  *    SCIC_SDS_IO_REQUEST_T::status field.
1523  * @this_request: This parameter specifies the request for which a completion
1524  *    occurred.
1525  * @completion_code: This parameter specifies the completion code received from
1526  *    the SCU.
1527  *
1528  */
1529 enum sci_status scic_sds_request_started_state_tc_completion_handler(
1530         struct scic_sds_request *this_request,
1531         u32 completion_code)
1532 {
1533         u8 data_present;
1534         struct sci_ssp_response_iu *response_buffer;
1535
1536         /**
1537          * @todo Any SDMA return code of other than 0 is bad
1538          *       decode 0x003C0000 to determine SDMA status
1539          */
1540         switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1541         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1542                 scic_sds_request_set_status(
1543                         this_request, SCU_TASK_DONE_GOOD, SCI_SUCCESS
1544                         );
1545                 break;
1546
1547         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP):
1548         {
1549                 /*
1550                  * There are times when the SCU hardware will return an early response
1551                  * because the io request specified more data than is returned by the
1552                  * target device (mode pages, inquiry data, etc.).  We must check the
1553                  * response stats to see if this is truly a failed request or a good
1554                  * request that just got completed early. */
1555                 struct sci_ssp_response_iu *response = (struct sci_ssp_response_iu *)
1556                                                   this_request->response_buffer;
1557                 scic_word_copy_with_swap(
1558                         this_request->response_buffer,
1559                         this_request->response_buffer,
1560                         sizeof(struct sci_ssp_response_iu) / sizeof(u32)
1561                         );
1562
1563                 if (response->status == 0) {
1564                         scic_sds_request_set_status(
1565                                 this_request, SCU_TASK_DONE_GOOD, SCI_SUCCESS_IO_DONE_EARLY
1566                                 );
1567                 } else {
1568                         scic_sds_request_set_status(
1569                                 this_request,
1570                                 SCU_TASK_DONE_CHECK_RESPONSE,
1571                                 SCI_FAILURE_IO_RESPONSE_VALID
1572                                 );
1573                 }
1574         }
1575         break;
1576
1577         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE):
1578                 scic_word_copy_with_swap(
1579                         this_request->response_buffer,
1580                         this_request->response_buffer,
1581                         sizeof(struct sci_ssp_response_iu) / sizeof(u32)
1582                         );
1583
1584                 scic_sds_request_set_status(
1585                         this_request,
1586                         SCU_TASK_DONE_CHECK_RESPONSE,
1587                         SCI_FAILURE_IO_RESPONSE_VALID
1588                         );
1589                 break;
1590
1591         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
1592                 /*
1593                  * / @todo With TASK_DONE_RESP_LEN_ERR is the response frame guaranteed
1594                  * /       to be received before this completion status is posted? */
1595                 response_buffer =
1596                         (struct sci_ssp_response_iu *)this_request->response_buffer;
1597                 data_present =
1598                         response_buffer->data_present & SCI_SSP_RESPONSE_IU_DATA_PRESENT_MASK;
1599
1600                 if ((data_present == 0x01) || (data_present == 0x02)) {
1601                         scic_sds_request_set_status(
1602                                 this_request,
1603                                 SCU_TASK_DONE_CHECK_RESPONSE,
1604                                 SCI_FAILURE_IO_RESPONSE_VALID
1605                                 );
1606                 } else {
1607                         scic_sds_request_set_status(
1608                                 this_request, SCU_TASK_DONE_GOOD, SCI_SUCCESS
1609                                 );
1610                 }
1611                 break;
1612
1613         /* only stp device gets suspended. */
1614         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
1615         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
1616         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
1617         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
1618         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
1619         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
1620         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
1621         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
1622         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
1623         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
1624         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
1625                 if (this_request->protocol == SCIC_STP_PROTOCOL) {
1626                         scic_sds_request_set_status(
1627                                 this_request,
1628                                 SCU_GET_COMPLETION_TL_STATUS(completion_code) >> SCU_COMPLETION_TL_STATUS_SHIFT,
1629                                 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED
1630                                 );
1631                 } else {
1632                         scic_sds_request_set_status(
1633                                 this_request,
1634                                 SCU_GET_COMPLETION_TL_STATUS(completion_code) >> SCU_COMPLETION_TL_STATUS_SHIFT,
1635                                 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
1636                                 );
1637                 }
1638                 break;
1639
1640         /* both stp/ssp device gets suspended */
1641         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
1642         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
1643         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
1644         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
1645         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
1646         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
1647         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
1648         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
1649         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
1650         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
1651                 scic_sds_request_set_status(
1652                         this_request,
1653                         SCU_GET_COMPLETION_TL_STATUS(completion_code) >> SCU_COMPLETION_TL_STATUS_SHIFT,
1654                         SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED
1655                         );
1656                 break;
1657
1658         /* neither ssp nor stp gets suspended. */
1659         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
1660         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
1661         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
1662         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
1663         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
1664         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
1665         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1666         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1667         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1668         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
1669         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
1670         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
1671         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
1672         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
1673         case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
1674         default:
1675                 scic_sds_request_set_status(
1676                         this_request,
1677                         SCU_GET_COMPLETION_TL_STATUS(completion_code) >> SCU_COMPLETION_TL_STATUS_SHIFT,
1678                         SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
1679                         );
1680                 break;
1681         }
1682
1683         /**
1684          * @todo This is probably wrong for ACK/NAK timeout conditions
1685          */
1686
1687         /* In all cases we will treat this as the completion of the IO request. */
1688         sci_base_state_machine_change_state(
1689                 &this_request->parent.state_machine,
1690                 SCI_BASE_REQUEST_STATE_COMPLETED
1691                 );
1692
1693         return SCI_SUCCESS;
1694 }
1695
1696 /**
1697  * scic_sds_request_started_state_frame_handler() -
1698  * @request: This is the struct sci_base_request object that is cast to the
1699  *    SCIC_SDS_IO_REQUEST_T object for which the start operation is requested.
1700  * @frame_index: This is the index of the unsolicited frame to be processed.
1701  *
1702  * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1703  * object receives a scic_sds_request_frame_handler() request. This method
1704  * first determines the frame type received.  If this is a response frame then
1705  * the response data is copied to the io request response buffer for processing
1706  * at completion time. If the frame type is not a response buffer an error is
1707  * logged. enum sci_status SCI_SUCCESS SCI_FAILURE_INVALID_PARAMETER_VALUE
1708  */
1709 static enum sci_status scic_sds_request_started_state_frame_handler(
1710         struct scic_sds_request *this_request,
1711         u32 frame_index)
1712 {
1713         enum sci_status status;
1714         struct sci_ssp_frame_header *frame_header;
1715
1716         /* / @todo If this is a response frame we must record that we received it */
1717         status = scic_sds_unsolicited_frame_control_get_header(
1718                 &(scic_sds_request_get_controller(this_request)->uf_control),
1719                 frame_index,
1720                 (void **)&frame_header
1721                 );
1722
1723         if (frame_header->frame_type == SCI_SAS_RESPONSE_FRAME) {
1724                 struct sci_ssp_response_iu *response_buffer;
1725
1726                 status = scic_sds_unsolicited_frame_control_get_buffer(
1727                         &(scic_sds_request_get_controller(this_request)->uf_control),
1728                         frame_index,
1729                         (void **)&response_buffer
1730                         );
1731
1732                 scic_word_copy_with_swap(
1733                         this_request->response_buffer,
1734                         (u32 *)response_buffer,
1735                         sizeof(struct sci_ssp_response_iu)
1736                         );
1737
1738                 response_buffer = (struct sci_ssp_response_iu *)this_request->response_buffer;
1739
1740                 if ((response_buffer->data_present == 0x01) ||
1741                     (response_buffer->data_present == 0x02)) {
1742                         scic_sds_request_set_status(
1743                                 this_request,
1744                                 SCU_TASK_DONE_CHECK_RESPONSE,
1745                                 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
1746                                 );
1747                 } else
1748                         scic_sds_request_set_status(
1749                                 this_request, SCU_TASK_DONE_GOOD, SCI_SUCCESS
1750                                 );
1751         } else
1752                 /* This was not a response frame why did it get forwarded? */
1753                 dev_err(scic_to_dev(this_request->owning_controller),
1754                         "%s: SCIC IO Request 0x%p received unexpected "
1755                         "frame %d type 0x%02x\n",
1756                         __func__,
1757                         this_request,
1758                         frame_index,
1759                         frame_header->frame_type);
1760
1761         /*
1762          * In any case we are done with this frame buffer return it to the
1763          * controller */
1764         scic_sds_controller_release_frame(
1765                 this_request->owning_controller, frame_index
1766                 );
1767
1768         return SCI_SUCCESS;
1769 }
1770
1771 /*
1772  * *****************************************************************************
1773  * *  COMPLETED STATE HANDLERS
1774  * ***************************************************************************** */
1775
1776
1777 /**
1778  * scic_sds_request_completed_state_complete_handler() -
1779  * @request: This is the struct sci_base_request object that is cast to the
1780  *    SCIC_SDS_IO_REQUEST_T object for which the start operation is requested.
1781  *
1782  * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1783  * object receives a scic_sds_request_complete() request. This method frees up
1784  * any io request resources that have been allocated and transitions the
1785  * request to its final state. Consider stopping the state machine instead of
1786  * transitioning to the final state? enum sci_status SCI_SUCCESS
1787  */
1788 static enum sci_status scic_sds_request_completed_state_complete_handler(
1789         struct sci_base_request *request)
1790 {
1791         struct scic_sds_request *this_request = (struct scic_sds_request *)request;
1792
1793         if (this_request->was_tag_assigned_by_user != true) {
1794                 scic_controller_free_io_tag(
1795                         this_request->owning_controller, this_request->io_tag
1796                         );
1797         }
1798
1799         if (this_request->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX) {
1800                 scic_sds_controller_release_frame(
1801                         this_request->owning_controller, this_request->saved_rx_frame_index);
1802         }
1803
1804         sci_base_state_machine_change_state(
1805                 &this_request->parent.state_machine,
1806                 SCI_BASE_REQUEST_STATE_FINAL
1807                 );
1808
1809         return SCI_SUCCESS;
1810 }
1811
1812 /*
1813  * *****************************************************************************
1814  * *  ABORTING STATE HANDLERS
1815  * ***************************************************************************** */
1816
1817 /**
1818  * scic_sds_request_aborting_state_abort_handler() -
1819  * @request: This is the struct sci_base_request object that is cast to the
1820  *    SCIC_SDS_IO_REQUEST_T object for which the start operation is requested.
1821  *
1822  * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1823  * object receives a scic_sds_request_terminate() request. This method is the
1824  * io request aborting state abort handlers.  On receipt of a multiple
1825  * terminate requests the io request will transition to the completed state.
1826  * This should not happen in normal operation. enum sci_status SCI_SUCCESS
1827  */
1828 static enum sci_status scic_sds_request_aborting_state_abort_handler(
1829         struct sci_base_request *request)
1830 {
1831         struct scic_sds_request *this_request = (struct scic_sds_request *)request;
1832
1833         sci_base_state_machine_change_state(
1834                 &this_request->parent.state_machine,
1835                 SCI_BASE_REQUEST_STATE_COMPLETED
1836                 );
1837
1838         return SCI_SUCCESS;
1839 }
1840
1841 /**
1842  * scic_sds_request_aborting_state_tc_completion_handler() -
1843  * @request: This is the struct sci_base_request object that is cast to the
1844  *    SCIC_SDS_IO_REQUEST_T object for which the start operation is requested.
1845  *
1846  * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1847  * object receives a scic_sds_request_task_completion() request. This method
1848  * decodes the completion type waiting for the abort task complete
1849  * notification. When the abort task complete is received the io request
1850  * transitions to the completed state. enum sci_status SCI_SUCCESS
1851  */
1852 static enum sci_status scic_sds_request_aborting_state_tc_completion_handler(
1853         struct scic_sds_request *this_request,
1854         u32 completion_code)
1855 {
1856         switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1857         case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
1858         case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
1859                 scic_sds_request_set_status(
1860                         this_request, SCU_TASK_DONE_TASK_ABORT, SCI_FAILURE_IO_TERMINATED
1861                         );
1862
1863                 sci_base_state_machine_change_state(
1864                         &this_request->parent.state_machine,
1865                         SCI_BASE_REQUEST_STATE_COMPLETED
1866                         );
1867                 break;
1868
1869         default:
1870                 /*
1871                  * Unless we get some strange error wait for the task abort to complete
1872                  * TODO: Should there be a state change for this completion? */
1873                 break;
1874         }
1875
1876         return SCI_SUCCESS;
1877 }
1878
1879 /**
1880  * scic_sds_request_aborting_state_frame_handler() -
1881  * @request: This is the struct sci_base_request object that is cast to the
1882  *    SCIC_SDS_IO_REQUEST_T object for which the start operation is requested.
1883  *
1884  * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1885  * object receives a scic_sds_request_frame_handler() request. This method
1886  * discards the unsolicited frame since we are waiting for the abort task
1887  * completion. enum sci_status SCI_SUCCESS
1888  */
1889 static enum sci_status scic_sds_request_aborting_state_frame_handler(
1890         struct scic_sds_request *this_request,
1891         u32 frame_index)
1892 {
1893         /* TODO: Is it even possible to get an unsolicited frame in the aborting state? */
1894
1895         scic_sds_controller_release_frame(
1896                 this_request->owning_controller, frame_index);
1897
1898         return SCI_SUCCESS;
1899 }
1900
1901 /* --------------------------------------------------------------------------- */
1902
1903 const struct scic_sds_io_request_state_handler scic_sds_request_state_handler_table[] = {
1904         [SCI_BASE_REQUEST_STATE_INITIAL] = {
1905                 .parent.start_handler    = scic_sds_request_default_start_handler,
1906                 .parent.abort_handler    = scic_sds_request_default_abort_handler,
1907                 .parent.complete_handler = scic_sds_request_default_complete_handler,
1908                 .parent.destruct_handler = scic_sds_request_default_destruct_handler,
1909                 .tc_completion_handler   = scic_sds_request_default_tc_completion_handler,
1910                 .event_handler           = scic_sds_request_default_event_handler,
1911                 .frame_handler           = scic_sds_request_default_frame_handler,
1912         },
1913         [SCI_BASE_REQUEST_STATE_CONSTRUCTED] = {
1914                 .parent.start_handler    = scic_sds_request_constructed_state_start_handler,
1915                 .parent.abort_handler    = scic_sds_request_constructed_state_abort_handler,
1916                 .parent.complete_handler = scic_sds_request_default_complete_handler,
1917                 .parent.destruct_handler = scic_sds_request_default_destruct_handler,
1918                 .tc_completion_handler   = scic_sds_request_default_tc_completion_handler,
1919                 .event_handler           = scic_sds_request_default_event_handler,
1920                 .frame_handler           = scic_sds_request_default_frame_handler,
1921         },
1922         [SCI_BASE_REQUEST_STATE_STARTED] = {
1923                 .parent.start_handler    = scic_sds_request_default_start_handler,
1924                 .parent.abort_handler    = scic_sds_request_started_state_abort_handler,
1925                 .parent.complete_handler = scic_sds_request_default_complete_handler,
1926                 .parent.destruct_handler = scic_sds_request_default_destruct_handler,
1927                 .tc_completion_handler   = scic_sds_request_started_state_tc_completion_handler,
1928                 .event_handler           = scic_sds_request_default_event_handler,
1929                 .frame_handler           = scic_sds_request_started_state_frame_handler,
1930         },
1931         [SCI_BASE_REQUEST_STATE_COMPLETED] = {
1932                 .parent.start_handler    = scic_sds_request_default_start_handler,
1933                 .parent.abort_handler    = scic_sds_request_default_abort_handler,
1934                 .parent.complete_handler = scic_sds_request_completed_state_complete_handler,
1935                 .parent.destruct_handler = scic_sds_request_default_destruct_handler,
1936                 .tc_completion_handler   = scic_sds_request_default_tc_completion_handler,
1937                 .event_handler           = scic_sds_request_default_event_handler,
1938                 .frame_handler           = scic_sds_request_default_frame_handler,
1939         },
1940         [SCI_BASE_REQUEST_STATE_ABORTING] = {
1941                 .parent.start_handler    = scic_sds_request_default_start_handler,
1942                 .parent.abort_handler    = scic_sds_request_aborting_state_abort_handler,
1943                 .parent.complete_handler = scic_sds_request_default_complete_handler,
1944                 .parent.destruct_handler = scic_sds_request_default_destruct_handler,
1945                 .tc_completion_handler   = scic_sds_request_aborting_state_tc_completion_handler,
1946                 .event_handler           = scic_sds_request_default_event_handler,
1947                 .frame_handler           = scic_sds_request_aborting_state_frame_handler,
1948         },
1949         [SCI_BASE_REQUEST_STATE_FINAL] = {
1950                 .parent.start_handler    = scic_sds_request_default_start_handler,
1951                 .parent.abort_handler    = scic_sds_request_default_abort_handler,
1952                 .parent.complete_handler = scic_sds_request_default_complete_handler,
1953                 .parent.destruct_handler = scic_sds_request_default_destruct_handler,
1954                 .tc_completion_handler   = scic_sds_request_default_tc_completion_handler,
1955                 .event_handler           = scic_sds_request_default_event_handler,
1956                 .frame_handler           = scic_sds_request_default_frame_handler,
1957         },
1958 };
1959
1960 /**
1961  * scic_sds_request_initial_state_enter() -
1962  * @object: This parameter specifies the base object for which the state
1963  *    transition is occurring.
1964  *
1965  * This method implements the actions taken when entering the
1966  * SCI_BASE_REQUEST_STATE_INITIAL state. This state is entered when the initial
1967  * base request is constructed. Entry into the initial state sets all handlers
1968  * for the io request object to their default handlers. none
1969  */
1970 static void scic_sds_request_initial_state_enter(
1971         struct sci_base_object *object)
1972 {
1973         struct scic_sds_request *this_request = (struct scic_sds_request *)object;
1974
1975         SET_STATE_HANDLER(
1976                 this_request,
1977                 scic_sds_request_state_handler_table,
1978                 SCI_BASE_REQUEST_STATE_INITIAL
1979                 );
1980 }
1981
1982 /**
1983  * scic_sds_request_constructed_state_enter() -
1984  * @object: The io request object that is to enter the constructed state.
1985  *
1986  * This method implements the actions taken when entering the
1987  * SCI_BASE_REQUEST_STATE_CONSTRUCTED state. The method sets the state handlers
1988  * for the the constructed state. none
1989  */
1990 static void scic_sds_request_constructed_state_enter(
1991         struct sci_base_object *object)
1992 {
1993         struct scic_sds_request *this_request = (struct scic_sds_request *)object;
1994
1995         SET_STATE_HANDLER(
1996                 this_request,
1997                 scic_sds_request_state_handler_table,
1998                 SCI_BASE_REQUEST_STATE_CONSTRUCTED
1999                 );
2000 }
2001
2002 /**
2003  * scic_sds_request_started_state_enter() -
2004  * @object: This parameter specifies the base object for which the state
2005  *    transition is occuring.  This is cast into a SCIC_SDS_IO_REQUEST object.
2006  *
2007  * This method implements the actions taken when entering the
2008  * SCI_BASE_REQUEST_STATE_STARTED state. If the io request object type is a
2009  * SCSI Task request we must enter the started substate machine. none
2010  */
2011 static void scic_sds_request_started_state_enter(
2012         struct sci_base_object *object)
2013 {
2014         struct scic_sds_request *this_request = (struct scic_sds_request *)object;
2015
2016         SET_STATE_HANDLER(
2017                 this_request,
2018                 scic_sds_request_state_handler_table,
2019                 SCI_BASE_REQUEST_STATE_STARTED
2020                 );
2021
2022         /*
2023          * Most of the request state machines have a started substate machine so
2024          * start its execution on the entry to the started state. */
2025         if (this_request->has_started_substate_machine == true)
2026                 sci_base_state_machine_start(&this_request->started_substate_machine);
2027 }
2028
2029 /**
2030  * scic_sds_request_started_state_exit() -
2031  * @object: This parameter specifies the base object for which the state
2032  *    transition is occuring.  This object is cast into a SCIC_SDS_IO_REQUEST
2033  *    object.
2034  *
2035  * This method implements the actions taken when exiting the
2036  * SCI_BASE_REQUEST_STATE_STARTED state. For task requests the action will be
2037  * to stop the started substate machine. none
2038  */
2039 static void scic_sds_request_started_state_exit(
2040         struct sci_base_object *object)
2041 {
2042         struct scic_sds_request *this_request = (struct scic_sds_request *)object;
2043
2044         if (this_request->has_started_substate_machine == true)
2045                 sci_base_state_machine_stop(&this_request->started_substate_machine);
2046 }
2047
2048 /**
2049  * scic_sds_request_completed_state_enter() -
2050  * @object: This parameter specifies the base object for which the state
2051  *    transition is occuring.  This object is cast into a SCIC_SDS_IO_REQUEST
2052  *    object.
2053  *
2054  * This method implements the actions taken when entering the
2055  * SCI_BASE_REQUEST_STATE_COMPLETED state.  This state is entered when the
2056  * SCIC_SDS_IO_REQUEST has completed.  The method will decode the request
2057  * completion status and convert it to an enum sci_status to return in the
2058  * completion callback function. none
2059  */
2060 static void scic_sds_request_completed_state_enter(
2061         struct sci_base_object *object)
2062 {
2063         struct scic_sds_request *this_request = (struct scic_sds_request *)object;
2064
2065         SET_STATE_HANDLER(
2066                 this_request,
2067                 scic_sds_request_state_handler_table,
2068                 SCI_BASE_REQUEST_STATE_COMPLETED
2069                 );
2070
2071         /* Tell the SCI_USER that the IO request is complete */
2072         if (this_request->is_task_management_request == false) {
2073                 scic_cb_io_request_complete(
2074                         scic_sds_request_get_controller(this_request),
2075                         scic_sds_request_get_device(this_request),
2076                         this_request,
2077                         this_request->sci_status
2078                         );
2079         } else {
2080                 scic_cb_task_request_complete(
2081                         scic_sds_request_get_controller(this_request),
2082                         scic_sds_request_get_device(this_request),
2083                         this_request,
2084                         this_request->sci_status
2085                         );
2086         }
2087 }
2088
2089 /**
2090  * scic_sds_request_aborting_state_enter() -
2091  * @object: This parameter specifies the base object for which the state
2092  *    transition is occuring.  This object is cast into a SCIC_SDS_IO_REQUEST
2093  *    object.
2094  *
2095  * This method implements the actions taken when entering the
2096  * SCI_BASE_REQUEST_STATE_ABORTING state. none
2097  */
2098 static void scic_sds_request_aborting_state_enter(
2099         struct sci_base_object *object)
2100 {
2101         struct scic_sds_request *this_request = (struct scic_sds_request *)object;
2102
2103         /* Setting the abort bit in the Task Context is required by the silicon. */
2104         this_request->task_context_buffer->abort = 1;
2105
2106         SET_STATE_HANDLER(
2107                 this_request,
2108                 scic_sds_request_state_handler_table,
2109                 SCI_BASE_REQUEST_STATE_ABORTING
2110                 );
2111 }
2112
2113 /**
2114  * scic_sds_request_final_state_enter() -
2115  * @object: This parameter specifies the base object for which the state
2116  *    transition is occuring.  This is cast into a SCIC_SDS_IO_REQUEST object.
2117  *
2118  * This method implements the actions taken when entering the
2119  * SCI_BASE_REQUEST_STATE_FINAL state. The only action required is to put the
2120  * state handlers in place. none
2121  */
2122 static void scic_sds_request_final_state_enter(
2123         struct sci_base_object *object)
2124 {
2125         struct scic_sds_request *this_request = (struct scic_sds_request *)object;
2126
2127         SET_STATE_HANDLER(
2128                 this_request,
2129                 scic_sds_request_state_handler_table,
2130                 SCI_BASE_REQUEST_STATE_FINAL
2131                 );
2132 }
2133
2134 /* --------------------------------------------------------------------------- */
2135
2136 const struct sci_base_state scic_sds_request_state_table[] = {
2137         [SCI_BASE_REQUEST_STATE_INITIAL] = {
2138                 .enter_state = scic_sds_request_initial_state_enter,
2139         },
2140         [SCI_BASE_REQUEST_STATE_CONSTRUCTED] = {
2141                 .enter_state = scic_sds_request_constructed_state_enter,
2142         },
2143         [SCI_BASE_REQUEST_STATE_STARTED] = {
2144                 .enter_state = scic_sds_request_started_state_enter,
2145                 .exit_state  = scic_sds_request_started_state_exit
2146         },
2147         [SCI_BASE_REQUEST_STATE_COMPLETED] = {
2148                 .enter_state = scic_sds_request_completed_state_enter,
2149         },
2150         [SCI_BASE_REQUEST_STATE_ABORTING] = {
2151                 .enter_state = scic_sds_request_aborting_state_enter,
2152         },
2153         [SCI_BASE_REQUEST_STATE_FINAL] = {
2154                 .enter_state = scic_sds_request_final_state_enter,
2155         },
2156 };
2157