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