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