isci: Intel(R) C600 Series Chipset Storage Control Unit Driver
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / scsi / isci / core / scic_sds_remote_device.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  * This file contains the implementation of remote device methods.
58  *
59  *
60  */
61
62 #include "intel_sas.h"
63 #include "sci_util.h"
64 #include "sci_environment.h"
65 #include "scic_port.h"
66 #include "scic_phy.h"
67 #include "scic_remote_device.h"
68 #include "scic_sds_port.h"
69 #include "scic_sds_phy.h"
70 #include "scic_sds_remote_device.h"
71 #include "scic_sds_request.h"
72 #include "scic_sds_controller.h"
73
74 #define SCIC_SDS_REMOTE_DEVICE_RESET_TIMEOUT  (1000)
75
76 /*
77  * *****************************************************************************
78  * *  CORE REMOTE DEVICE PRIVATE METHODS
79  * ***************************************************************************** */
80
81 /*
82  * *****************************************************************************
83  * *  CORE REMOTE DEVICE PUBLIC METHODS
84  * ***************************************************************************** */
85
86 u32 scic_remote_device_get_object_size(void)
87 {
88         return sizeof(struct scic_sds_remote_device)
89                + sizeof(struct scic_sds_remote_node_context);
90 }
91
92 /* --------------------------------------------------------------------------- */
93
94 void scic_remote_device_construct(struct scic_sds_port *sci_port,
95                                   struct scic_sds_remote_device *sci_dev)
96 {
97         sci_dev->owning_port = sci_port;
98         sci_dev->started_request_count = 0;
99         sci_dev->rnc = (struct scic_sds_remote_node_context *) &sci_dev[1];
100
101         sci_base_remote_device_construct(
102                 &sci_dev->parent,
103                 scic_sds_remote_device_state_table
104                 );
105
106         scic_sds_remote_node_context_construct(
107                 sci_dev,
108                 sci_dev->rnc,
109                 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX
110                 );
111
112         sci_object_set_association(sci_dev->rnc, sci_dev);
113 }
114
115
116 enum sci_status scic_remote_device_da_construct(
117         struct scic_sds_remote_device *sci_dev)
118 {
119         enum sci_status status;
120         u16 remote_node_index;
121         struct sci_sas_identify_address_frame_protocols protocols;
122
123         /*
124          * This information is request to determine how many remote node context
125          * entries will be needed to store the remote node.
126          */
127         scic_sds_port_get_attached_protocols(sci_dev->owning_port, &protocols);
128         sci_dev->target_protocols.u.all = protocols.u.all;
129         sci_dev->is_direct_attached = true;
130 #if !defined(DISABLE_ATAPI)
131         sci_dev->is_atapi = scic_sds_remote_device_is_atapi(sci_dev);
132 #endif
133
134         status = scic_sds_controller_allocate_remote_node_context(
135                 sci_dev->owning_port->owning_controller,
136                 sci_dev,
137                 &remote_node_index);
138
139         if (status == SCI_SUCCESS) {
140                 scic_sds_remote_node_context_set_remote_node_index(
141                         sci_dev->rnc, remote_node_index);
142
143                 scic_sds_port_get_attached_sas_address(
144                         sci_dev->owning_port, &sci_dev->device_address);
145
146                 if (sci_dev->target_protocols.u.bits.attached_ssp_target) {
147                         sci_dev->has_ready_substate_machine = false;
148                 } else if (sci_dev->target_protocols.u.bits.attached_stp_target) {
149                         sci_dev->has_ready_substate_machine = true;
150
151                         sci_base_state_machine_construct(
152                                 &sci_dev->ready_substate_machine,
153                                 &sci_dev->parent.parent,
154                                 scic_sds_stp_remote_device_ready_substate_table,
155                                 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
156                 } else if (sci_dev->target_protocols.u.bits.attached_smp_target) {
157                         sci_dev->has_ready_substate_machine = true;
158
159                         /* add the SMP ready substate machine construction here */
160                         sci_base_state_machine_construct(
161                                 &sci_dev->ready_substate_machine,
162                                 &sci_dev->parent.parent,
163                                 scic_sds_smp_remote_device_ready_substate_table,
164                                 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
165                 }
166
167                 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(
168                         sci_dev->owning_port);
169
170                 /* / @todo Should I assign the port width by reading all of the phys on the port? */
171                 sci_dev->device_port_width = 1;
172         }
173
174         return status;
175 }
176
177
178 static void scic_sds_remote_device_get_info_from_smp_discover_response(
179         struct scic_sds_remote_device *this_device,
180         struct smp_response_discover *discover_response)
181 {
182         /* decode discover_response to set sas_address to this_device. */
183         this_device->device_address.high =
184                 discover_response->attached_sas_address.high;
185
186         this_device->device_address.low =
187                 discover_response->attached_sas_address.low;
188
189         this_device->target_protocols.u.all = discover_response->protocols.u.all;
190 }
191
192
193 enum sci_status scic_remote_device_ea_construct(
194         struct scic_sds_remote_device *sci_dev,
195         struct smp_response_discover *discover_response)
196 {
197         enum sci_status status;
198         struct scic_sds_controller *the_controller;
199
200         the_controller = scic_sds_port_get_controller(sci_dev->owning_port);
201
202         scic_sds_remote_device_get_info_from_smp_discover_response(
203                 sci_dev, discover_response);
204
205         status = scic_sds_controller_allocate_remote_node_context(
206                 the_controller, sci_dev, &sci_dev->rnc->remote_node_index);
207
208         if (status == SCI_SUCCESS) {
209                 if (sci_dev->target_protocols.u.bits.attached_ssp_target) {
210                         sci_dev->has_ready_substate_machine = false;
211                 } else if (sci_dev->target_protocols.u.bits.attached_smp_target) {
212                         sci_dev->has_ready_substate_machine = true;
213
214                         /* add the SMP ready substate machine construction here */
215                         sci_base_state_machine_construct(
216                                 &sci_dev->ready_substate_machine,
217                                 &sci_dev->parent.parent,
218                                 scic_sds_smp_remote_device_ready_substate_table,
219                                 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
220                 } else if (sci_dev->target_protocols.u.bits.attached_stp_target) {
221                         sci_dev->has_ready_substate_machine = true;
222
223                         sci_base_state_machine_construct(
224                                 &sci_dev->ready_substate_machine,
225                                 &sci_dev->parent.parent,
226                                 scic_sds_stp_remote_device_ready_substate_table,
227                                 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
228                 }
229
230                 /*
231                  * For SAS-2 the physical link rate is actually a logical link
232                  * rate that incorporates multiplexing.  The SCU doesn't
233                  * incorporate multiplexing and for the purposes of the
234                  * connection the logical link rate is that same as the
235                  * physical.  Furthermore, the SAS-2 and SAS-1.1 fields overlay
236                  * one another, so this code works for both situations. */
237                 sci_dev->connection_rate = min_t(u16,
238                         scic_sds_port_get_max_allowed_speed(sci_dev->owning_port),
239                         discover_response->u2.sas1_1.negotiated_physical_link_rate
240                         );
241
242                 /* / @todo Should I assign the port width by reading all of the phys on the port? */
243                 sci_dev->device_port_width = 1;
244         }
245
246         return status;
247 }
248
249 enum sci_status scic_remote_device_destruct(
250         struct scic_sds_remote_device *sci_dev)
251 {
252         return sci_dev->state_handlers->parent.destruct_handler(&sci_dev->parent);
253 }
254
255
256 enum sci_status scic_remote_device_start(
257         struct scic_sds_remote_device *sci_dev,
258         u32 timeout)
259 {
260         return sci_dev->state_handlers->parent.start_handler(&sci_dev->parent);
261 }
262
263
264 enum sci_status scic_remote_device_stop(
265         struct scic_sds_remote_device *sci_dev,
266         u32 timeout)
267 {
268         return sci_dev->state_handlers->parent.stop_handler(&sci_dev->parent);
269 }
270
271
272 enum sci_status scic_remote_device_reset(
273         struct scic_sds_remote_device *sci_dev)
274 {
275         return sci_dev->state_handlers->parent.reset_handler(&sci_dev->parent);
276 }
277
278
279 enum sci_status scic_remote_device_reset_complete(
280         struct scic_sds_remote_device *sci_dev)
281 {
282         return sci_dev->state_handlers->parent.reset_complete_handler(&sci_dev->parent);
283 }
284
285
286 enum sci_sas_link_rate scic_remote_device_get_connection_rate(
287         struct scic_sds_remote_device *sci_dev)
288 {
289         return sci_dev->connection_rate;
290 }
291
292
293 void scic_remote_device_get_protocols(
294         struct scic_sds_remote_device *sci_dev,
295         struct smp_discover_response_protocols *pr)
296 {
297         pr->u.all = sci_dev->target_protocols.u.all;
298 }
299
300 #if !defined(DISABLE_ATAPI)
301 bool scic_remote_device_is_atapi(struct scic_sds_remote_device *sci_dev)
302 {
303         return sci_dev->is_atapi;
304 }
305 #endif
306
307
308 /*
309  * *****************************************************************************
310  * *  SCU DRIVER STANDARD (SDS) REMOTE DEVICE IMPLEMENTATIONS
311  * ***************************************************************************** */
312
313 /**
314  *
315  *
316  * Remote device timer requirements
317  */
318 #define SCIC_SDS_REMOTE_DEVICE_MINIMUM_TIMER_COUNT (0)
319 #define SCIC_SDS_REMOTE_DEVICE_MAXIMUM_TIMER_COUNT (SCI_MAX_REMOTE_DEVICES)
320
321
322 /**
323  *
324  * @this_device: The remote device for which the suspend is being requested.
325  *
326  * This method invokes the remote device suspend state handler. enum sci_status
327  */
328 enum sci_status scic_sds_remote_device_suspend(
329         struct scic_sds_remote_device *this_device,
330         u32 suspend_type)
331 {
332         return this_device->state_handlers->suspend_handler(this_device, suspend_type);
333 }
334
335 /**
336  *
337  * @this_device: The remote device for which the resume is being requested.
338  *
339  * This method invokes the remote device resume state handler. enum sci_status
340  */
341 enum sci_status scic_sds_remote_device_resume(
342         struct scic_sds_remote_device *this_device)
343 {
344         return this_device->state_handlers->resume_handler(this_device);
345 }
346
347 /**
348  *
349  * @this_device: The remote device for which the event handling is being
350  *    requested.
351  * @frame_index: This is the frame index that is being processed.
352  *
353  * This method invokes the frame handler for the remote device state machine
354  * enum sci_status
355  */
356 enum sci_status scic_sds_remote_device_frame_handler(
357         struct scic_sds_remote_device *this_device,
358         u32 frame_index)
359 {
360         return this_device->state_handlers->frame_handler(this_device, frame_index);
361 }
362
363 /**
364  *
365  * @this_device: The remote device for which the event handling is being
366  *    requested.
367  * @event_code: This is the event code that is to be processed.
368  *
369  * This method invokes the remote device event handler. enum sci_status
370  */
371 enum sci_status scic_sds_remote_device_event_handler(
372         struct scic_sds_remote_device *this_device,
373         u32 event_code)
374 {
375         return this_device->state_handlers->event_handler(this_device, event_code);
376 }
377
378 /**
379  *
380  * @controller: The controller that is starting the io request.
381  * @this_device: The remote device for which the start io handling is being
382  *    requested.
383  * @io_request: The io request that is being started.
384  *
385  * This method invokes the remote device start io handler. enum sci_status
386  */
387 enum sci_status scic_sds_remote_device_start_io(
388         struct scic_sds_controller *controller,
389         struct scic_sds_remote_device *this_device,
390         struct scic_sds_request *io_request)
391 {
392         return this_device->state_handlers->parent.start_io_handler(
393                        &this_device->parent, &io_request->parent);
394 }
395
396 /**
397  *
398  * @controller: The controller that is completing the io request.
399  * @this_device: The remote device for which the complete io handling is being
400  *    requested.
401  * @io_request: The io request that is being completed.
402  *
403  * This method invokes the remote device complete io handler. enum sci_status
404  */
405 enum sci_status scic_sds_remote_device_complete_io(
406         struct scic_sds_controller *controller,
407         struct scic_sds_remote_device *this_device,
408         struct scic_sds_request *io_request)
409 {
410         return this_device->state_handlers->parent.complete_io_handler(
411                        &this_device->parent, &io_request->parent);
412 }
413
414 /**
415  *
416  * @controller: The controller that is starting the task request.
417  * @this_device: The remote device for which the start task handling is being
418  *    requested.
419  * @io_request: The task request that is being started.
420  *
421  * This method invokes the remote device start task handler. enum sci_status
422  */
423 enum sci_status scic_sds_remote_device_start_task(
424         struct scic_sds_controller *controller,
425         struct scic_sds_remote_device *this_device,
426         struct scic_sds_request *io_request)
427 {
428         return this_device->state_handlers->parent.start_task_handler(
429                        &this_device->parent, &io_request->parent);
430 }
431
432 /**
433  *
434  * @controller: The controller that is completing the task request.
435  * @this_device: The remote device for which the complete task handling is
436  *    being requested.
437  * @io_request: The task request that is being completed.
438  *
439  * This method invokes the remote device complete task handler. enum sci_status
440  */
441
442 /**
443  *
444  * @this_device:
445  * @request:
446  *
447  * This method takes the request and bulids an appropriate SCU context for the
448  * request and then requests the controller to post the request. none
449  */
450 void scic_sds_remote_device_post_request(
451         struct scic_sds_remote_device *this_device,
452         u32 request)
453 {
454         u32 context;
455
456         context = scic_sds_remote_device_build_command_context(this_device, request);
457
458         scic_sds_controller_post_request(
459                 scic_sds_remote_device_get_controller(this_device),
460                 context
461                 );
462 }
463
464 #if !defined(DISABLE_ATAPI)
465 /**
466  *
467  * @this_device: The device to be checked.
468  *
469  * This method check the signature fis of a stp device to decide whether a
470  * device is atapi or not. true if a device is atapi device. False if a device
471  * is not atapi.
472  */
473 bool scic_sds_remote_device_is_atapi(
474         struct scic_sds_remote_device *this_device)
475 {
476         if (!this_device->target_protocols.u.bits.attached_stp_target)
477                 return false;
478         else if (this_device->is_direct_attached) {
479                 struct scic_sds_phy *phy;
480                 struct scic_sata_phy_properties properties;
481                 struct sata_fis_reg_d2h *signature_fis;
482                 phy = scic_sds_port_get_a_connected_phy(this_device->owning_port);
483                 scic_sata_phy_get_properties(phy, &properties);
484
485                 /* decode the signature fis. */
486                 signature_fis = &(properties.signature_fis);
487
488                 if ((signature_fis->sector_count  == 0x01)
489                     && (signature_fis->lba_low       == 0x01)
490                     && (signature_fis->lba_mid       == 0x14)
491                     && (signature_fis->lba_high      == 0xEB)
492                     && ((signature_fis->device & 0x5F) == 0x00)
493                     ) {
494                         /* An ATA device supporting the PACKET command set. */
495                         return true;
496                 } else
497                         return false;
498         } else {
499                 /* Expander supported ATAPI device is not currently supported. */
500                 return false;
501         }
502 }
503 #endif
504 /*
505  * This file is provided under a dual BSD/GPLv2 license.  When using or
506  * redistributing this file, you may do so under either license.
507  *
508  * GPL LICENSE SUMMARY
509  *
510  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
511  *
512  * This program is free software; you can redistribute it and/or modify
513  * it under the terms of version 2 of the GNU General Public License as
514  * published by the Free Software Foundation.
515  *
516  * This program is distributed in the hope that it will be useful, but
517  * WITHOUT ANY WARRANTY; without even the implied warranty of
518  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
519  * General Public License for more details.
520  *
521  * You should have received a copy of the GNU General Public License
522  * along with this program; if not, write to the Free Software
523  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
524  * The full GNU General Public License is included in this distribution
525  * in the file called LICENSE.GPL.
526  *
527  * BSD LICENSE
528  *
529  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
530  * All rights reserved.
531  *
532  * Redistribution and use in source and binary forms, with or without
533  * modification, are permitted provided that the following conditions
534  * are met:
535  *
536  *   * Redistributions of source code must retain the above copyright
537  *     notice, this list of conditions and the following disclaimer.
538  *   * Redistributions in binary form must reproduce the above copyright
539  *     notice, this list of conditions and the following disclaimer in
540  *     the documentation and/or other materials provided with the
541  *     distribution.
542  *   * Neither the name of Intel Corporation nor the names of its
543  *     contributors may be used to endorse or promote products derived
544  *     from this software without specific prior written permission.
545  *
546  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
547  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
548  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
549  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
550  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
551  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
552  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
553  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
554  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
555  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
556  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
557  */
558
559 /**
560  * This file contains the state handlers for the struct scic_sds_remote_device for the
561  *    base state machine.
562  *
563  *
564  */
565
566 #include "sci_environment.h"
567 #include "scic_user_callback.h"
568 #include "scic_controller.h"
569 #include "scic_sds_remote_device.h"
570 #include "scic_sds_controller.h"
571 #include "scic_sds_port.h"
572 #include "scic_sds_request.h"
573 #include "scic_sds_remote_node_context.h"
574 #include "scu_event_codes.h"
575
576 /*
577  * *****************************************************************************
578  * *  PROTECTED METHODS
579  * ***************************************************************************** */
580
581 /**
582  *
583  * @user_parameter: This is cast to a remote device object.
584  *
585  * This method is called once the remote node context is ready to be freed.
586  * The remote device can now report that its stop operation is complete. none
587  */
588 static void scic_sds_cb_remote_device_rnc_destruct_complete(
589         void *user_parameter)
590 {
591         struct scic_sds_remote_device *this_device;
592
593         this_device = (struct scic_sds_remote_device *)user_parameter;
594
595         BUG_ON(this_device->started_request_count != 0);
596
597         sci_base_state_machine_change_state(
598                 scic_sds_remote_device_get_base_state_machine(this_device),
599                 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
600                 );
601 }
602
603 /**
604  *
605  * @user_parameter: This is cast to a remote device object.
606  *
607  * This method is called once the remote node context has transisitioned to a
608  * ready state.  This is the indication that the remote device object can also
609  * transition to ready. none
610  */
611 static void scic_sds_remote_device_resume_complete_handler(
612         void *user_parameter)
613 {
614         struct scic_sds_remote_device *this_device;
615
616         this_device = (struct scic_sds_remote_device *)user_parameter;
617
618         if (
619                 sci_base_state_machine_get_state(&this_device->parent.state_machine)
620                 != SCI_BASE_REMOTE_DEVICE_STATE_READY
621                 ) {
622                 sci_base_state_machine_change_state(
623                         &this_device->parent.state_machine,
624                         SCI_BASE_REMOTE_DEVICE_STATE_READY
625                         );
626         }
627 }
628
629 /**
630  *
631  * @device: This parameter specifies the device for which the request is being
632  *    started.
633  * @request: This parameter specifies the request being started.
634  * @status: This parameter specifies the current start operation status.
635  *
636  * This method will perform the STP request start processing common to IO
637  * requests and task requests of all types. none
638  */
639 void scic_sds_remote_device_start_request(
640         struct scic_sds_remote_device *this_device,
641         struct scic_sds_request *the_request,
642         enum sci_status status)
643 {
644         /* We still have a fault in starting the io complete it on the port */
645         if (status == SCI_SUCCESS)
646                 scic_sds_remote_device_increment_request_count(this_device);
647         else{
648                 this_device->owning_port->state_handlers->complete_io_handler(
649                         this_device->owning_port, this_device, the_request
650                         );
651         }
652 }
653
654
655 /**
656  *
657  * @request: This parameter specifies the request being continued.
658  *
659  * This method will continue to post tc for a STP request. This method usually
660  * serves as a callback when RNC gets resumed during a task management
661  * sequence. none
662  */
663 void scic_sds_remote_device_continue_request(
664         struct scic_sds_remote_device *this_device)
665 {
666         /* we need to check if this request is still valid to continue. */
667         if (this_device->working_request != NULL) {
668                 struct scic_sds_request *this_request = this_device->working_request;
669                 struct scic_sds_controller *scic = this_request->owning_controller;
670                 u32 state = scic->parent.state_machine.current_state_id;
671                 sci_base_controller_request_handler_t continue_io;
672
673                 continue_io = scic_sds_controller_state_handler_table[state].base.continue_io;
674                 continue_io(&scic->parent, &this_request->target_device->parent,
675                             &this_request->parent);
676         }
677 }
678
679 /**
680  *
681  * @user_parameter: This is cast to a remote device object.
682  *
683  * This method is called once the remote node context has reached a suspended
684  * state. The remote device can now report that its suspend operation is
685  * complete. none
686  */
687
688 /**
689  * This method will terminate all of the IO requests in the controllers IO
690  *    request table that were targeted for this device.
691  * @this_device: This parameter specifies the remote device for which to
692  *    attempt to terminate all requests.
693  *
694  * This method returns an indication as to whether all requests were
695  * successfully terminated.  If a single request fails to be terminated, then
696  * this method will return the failure.
697  */
698 static enum sci_status scic_sds_remote_device_terminate_requests(
699         struct scic_sds_remote_device *this_device)
700 {
701         enum sci_status status           = SCI_SUCCESS;
702         enum sci_status terminate_status = SCI_SUCCESS;
703         struct scic_sds_request *the_request;
704         u32 index;
705         u32 request_count    = this_device->started_request_count;
706
707         for (index = 0;
708              (index < SCI_MAX_IO_REQUESTS) && (request_count > 0);
709              index++) {
710                 the_request = this_device->owning_port->owning_controller->io_request_table[index];
711
712                 if ((the_request != NULL) && (the_request->target_device == this_device)) {
713                         terminate_status = scic_controller_terminate_request(
714                                 this_device->owning_port->owning_controller,
715                                 this_device,
716                                 the_request
717                                 );
718
719                         if (terminate_status != SCI_SUCCESS)
720                                 status = terminate_status;
721
722                         request_count--;
723                 }
724         }
725
726         return status;
727 }
728
729 /*
730  * *****************************************************************************
731  * *  DEFAULT STATE HANDLERS
732  * ***************************************************************************** */
733
734 /**
735  *
736  * @device: The struct sci_base_remote_device which is then cast into a
737  *    struct scic_sds_remote_device.
738  *
739  * This method is the default start handler.  It logs a warning and returns a
740  * failure. enum sci_status SCI_FAILURE_INVALID_STATE
741  */
742 enum sci_status scic_sds_remote_device_default_start_handler(
743         struct sci_base_remote_device *device)
744 {
745         struct scic_sds_remote_device *sds_device =
746                 (struct scic_sds_remote_device *)device;
747
748         dev_warn(scirdev_to_dev(sds_device),
749                  "%s: SCIC Remote Device requested to start while in wrong "
750                  "state %d\n",
751                  __func__,
752                  sci_base_state_machine_get_state(
753                          scic_sds_remote_device_get_base_state_machine(
754                                  sds_device)));
755
756         return SCI_FAILURE_INVALID_STATE;
757 }
758
759 /**
760  *
761  * @device: The struct sci_base_remote_device which is then cast into a
762  *    struct scic_sds_remote_device.
763  *
764  * This method is the default stop handler.  It logs a warning and returns a
765  * failure. enum sci_status SCI_FAILURE_INVALID_STATE
766  */
767 static enum sci_status scic_sds_remote_device_default_stop_handler(
768         struct sci_base_remote_device *device)
769 {
770         struct scic_sds_remote_device *sds_device =
771                 (struct scic_sds_remote_device *)device;
772
773         dev_warn(scirdev_to_dev(sds_device),
774                  "%s: SCIC Remote Device requested to stop while in wrong "
775                  "state %d\n",
776                  __func__,
777                  sci_base_state_machine_get_state(
778                          scic_sds_remote_device_get_base_state_machine(
779                                  sds_device)));
780
781         return SCI_FAILURE_INVALID_STATE;
782 }
783
784 /**
785  *
786  * @device: The struct sci_base_remote_device which is then cast into a
787  *    struct scic_sds_remote_device.
788  *
789  * This method is the default fail handler.  It logs a warning and returns a
790  * failure. enum sci_status SCI_FAILURE_INVALID_STATE
791  */
792 enum sci_status scic_sds_remote_device_default_fail_handler(
793         struct sci_base_remote_device *device)
794 {
795         struct scic_sds_remote_device *sds_device =
796                 (struct scic_sds_remote_device *)device;
797
798         dev_warn(scirdev_to_dev(sds_device),
799                  "%s: SCIC Remote Device requested to fail while in wrong "
800                  "state %d\n",
801                  __func__,
802                  sci_base_state_machine_get_state(
803                          scic_sds_remote_device_get_base_state_machine(
804                                  sds_device)));
805
806         return SCI_FAILURE_INVALID_STATE;
807 }
808
809 /**
810  *
811  * @device: The struct sci_base_remote_device which is then cast into a
812  *    struct scic_sds_remote_device.
813  *
814  * This method is the default destruct handler.  It logs a warning and returns
815  * a failure. enum sci_status SCI_FAILURE_INVALID_STATE
816  */
817 enum sci_status scic_sds_remote_device_default_destruct_handler(
818         struct sci_base_remote_device *device)
819 {
820         struct scic_sds_remote_device *sds_device =
821                 (struct scic_sds_remote_device *)device;
822
823         dev_warn(scirdev_to_dev(sds_device),
824                  "%s: SCIC Remote Device requested to destroy while in "
825                  "wrong state %d\n",
826                  __func__,
827                  sci_base_state_machine_get_state(
828                          scic_sds_remote_device_get_base_state_machine(
829                                  sds_device)));
830
831         return SCI_FAILURE_INVALID_STATE;
832 }
833
834 /**
835  *
836  * @device: The struct sci_base_remote_device which is then cast into a
837  *    struct scic_sds_remote_device.
838  *
839  * This method is the default reset handler.  It logs a warning and returns a
840  * failure. enum sci_status SCI_FAILURE_INVALID_STATE
841  */
842 enum sci_status scic_sds_remote_device_default_reset_handler(
843         struct sci_base_remote_device *device)
844 {
845         struct scic_sds_remote_device *sds_device =
846                 (struct scic_sds_remote_device *)device;
847
848         dev_warn(scirdev_to_dev(sds_device),
849                  "%s: SCIC Remote Device requested to reset while in wrong "
850                  "state %d\n",
851                  __func__,
852                  sci_base_state_machine_get_state(
853                          scic_sds_remote_device_get_base_state_machine(
854                                  sds_device)));
855
856         return SCI_FAILURE_INVALID_STATE;
857 }
858
859 /**
860  *
861  * @device: The struct sci_base_remote_device which is then cast into a
862  *    struct scic_sds_remote_device.
863  *
864  * This method is the default reset complete handler.  It logs a warning and
865  * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
866  */
867 enum sci_status scic_sds_remote_device_default_reset_complete_handler(
868         struct sci_base_remote_device *device)
869 {
870         struct scic_sds_remote_device *sds_device =
871                 (struct scic_sds_remote_device *)device;
872
873         dev_warn(scirdev_to_dev(sds_device),
874                  "%s: SCIC Remote Device requested to complete reset while "
875                  "in wrong state %d\n",
876                  __func__,
877                  sci_base_state_machine_get_state(
878                          scic_sds_remote_device_get_base_state_machine(
879                                  sds_device)));
880
881         return SCI_FAILURE_INVALID_STATE;
882 }
883
884 /**
885  *
886  * @device: The struct sci_base_remote_device which is then cast into a
887  *    struct scic_sds_remote_device.
888  *
889  * This method is the default suspend handler.  It logs a warning and returns a
890  * failure. enum sci_status SCI_FAILURE_INVALID_STATE
891  */
892 enum sci_status scic_sds_remote_device_default_suspend_handler(
893         struct scic_sds_remote_device *this_device,
894         u32 suspend_type)
895 {
896         dev_warn(scirdev_to_dev(this_device),
897                  "%s: SCIC Remote Device 0x%p requested to suspend %d while "
898                  "in wrong state %d\n",
899                  __func__,
900                  this_device,
901                  suspend_type,
902                  sci_base_state_machine_get_state(
903                          scic_sds_remote_device_get_base_state_machine(
904                                  this_device)));
905
906         return SCI_FAILURE_INVALID_STATE;
907 }
908
909 /**
910  *
911  * @device: The struct sci_base_remote_device which is then cast into a
912  *    struct scic_sds_remote_device.
913  *
914  * This method is the default resume handler.  It logs a warning and returns a
915  * failure. enum sci_status SCI_FAILURE_INVALID_STATE
916  */
917 enum sci_status scic_sds_remote_device_default_resume_handler(
918         struct scic_sds_remote_device *this_device)
919 {
920         dev_warn(scirdev_to_dev(this_device),
921                  "%s: SCIC Remote Device requested to resume while in "
922                  "wrong state %d\n",
923                  __func__,
924                  sci_base_state_machine_get_state(
925                          scic_sds_remote_device_get_base_state_machine(
926                                  this_device)));
927
928         return SCI_FAILURE_INVALID_STATE;
929 }
930
931 /**
932  *
933  * @device: The struct sci_base_remote_device which is then cast into a
934  *    struct scic_sds_remote_device.
935  * @event_code: The event code that the struct scic_sds_controller wants the device
936  *    object to process.
937  *
938  * This method is the default event handler.  It will call the RNC state
939  * machine handler for any RNC events otherwise it will log a warning and
940  * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
941  */
942 static enum sci_status  scic_sds_remote_device_core_event_handler(
943         struct scic_sds_remote_device *this_device,
944         u32 event_code,
945         bool is_ready_state)
946 {
947         enum sci_status status;
948
949         switch (scu_get_event_type(event_code)) {
950         case SCU_EVENT_TYPE_RNC_OPS_MISC:
951         case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
952         case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
953                 status = scic_sds_remote_node_context_event_handler(this_device->rnc, event_code);
954                 break;
955         case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
956
957                 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
958                         status = SCI_SUCCESS;
959
960                         /* Suspend the associated RNC */
961                         scic_sds_remote_node_context_suspend(this_device->rnc,
962                                                               SCI_SOFTWARE_SUSPENSION,
963                                                               NULL, NULL);
964
965                         dev_dbg(scirdev_to_dev(this_device),
966                                 "%s: device: %p event code: %x: %s\n",
967                                 __func__, this_device, event_code,
968                                 (is_ready_state)
969                                 ? "I_T_Nexus_Timeout event"
970                                 : "I_T_Nexus_Timeout event in wrong state");
971
972                         break;
973                 }
974         /* Else, fall through and treat as unhandled... */
975
976         default:
977                 dev_dbg(scirdev_to_dev(this_device),
978                         "%s: device: %p event code: %x: %s\n",
979                         __func__, this_device, event_code,
980                         (is_ready_state)
981                         ? "unexpected event"
982                         : "unexpected event in wrong state");
983                 status = SCI_FAILURE_INVALID_STATE;
984                 break;
985         }
986
987         return status;
988 }
989 /**
990  *
991  * @device: The struct sci_base_remote_device which is then cast into a
992  *    struct scic_sds_remote_device.
993  * @event_code: The event code that the struct scic_sds_controller wants the device
994  *    object to process.
995  *
996  * This method is the default event handler.  It will call the RNC state
997  * machine handler for any RNC events otherwise it will log a warning and
998  * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
999  */
1000 static enum sci_status  scic_sds_remote_device_default_event_handler(
1001         struct scic_sds_remote_device *this_device,
1002         u32 event_code)
1003 {
1004         return scic_sds_remote_device_core_event_handler(this_device,
1005                                                           event_code,
1006                                                           false);
1007 }
1008
1009 /**
1010  *
1011  * @device: The struct sci_base_remote_device which is then cast into a
1012  *    struct scic_sds_remote_device.
1013  * @frame_index: The frame index for which the struct scic_sds_controller wants this
1014  *    device object to process.
1015  *
1016  * This method is the default unsolicited frame handler.  It logs a warning,
1017  * releases the frame and returns a failure. enum sci_status
1018  * SCI_FAILURE_INVALID_STATE
1019  */
1020 enum sci_status scic_sds_remote_device_default_frame_handler(
1021         struct scic_sds_remote_device *this_device,
1022         u32 frame_index)
1023 {
1024         dev_warn(scirdev_to_dev(this_device),
1025                  "%s: SCIC Remote Device requested to handle frame %x "
1026                  "while in wrong state %d\n",
1027                  __func__,
1028                  frame_index,
1029                  sci_base_state_machine_get_state(
1030                          &this_device->parent.state_machine));
1031
1032         /* Return the frame back to the controller */
1033         scic_sds_controller_release_frame(
1034                 scic_sds_remote_device_get_controller(this_device), frame_index
1035                 );
1036
1037         return SCI_FAILURE_INVALID_STATE;
1038 }
1039
1040 /**
1041  *
1042  * @device: The struct sci_base_remote_device which is then cast into a
1043  *    struct scic_sds_remote_device.
1044  * @request: The struct sci_base_request which is then cast into a SCIC_SDS_IO_REQUEST
1045  *    to start.
1046  *
1047  * This method is the default start io handler.  It logs a warning and returns
1048  * a failure. enum sci_status SCI_FAILURE_INVALID_STATE
1049  */
1050 enum sci_status scic_sds_remote_device_default_start_request_handler(
1051         struct sci_base_remote_device *device,
1052         struct sci_base_request *request)
1053 {
1054         struct scic_sds_remote_device *sds_device =
1055                 (struct scic_sds_remote_device *)device;
1056
1057         dev_warn(scirdev_to_dev(sds_device),
1058                  "%s: SCIC Remote Device requested to start io request %p "
1059                  "while in wrong state %d\n",
1060                  __func__,
1061                  request,
1062                  sci_base_state_machine_get_state(
1063                          scic_sds_remote_device_get_base_state_machine(
1064                                  (struct scic_sds_remote_device *)device)));
1065
1066         return SCI_FAILURE_INVALID_STATE;
1067 }
1068
1069 /**
1070  *
1071  * @device: The struct sci_base_remote_device which is then cast into a
1072  *    struct scic_sds_remote_device.
1073  * @request: The struct sci_base_request which is then cast into a SCIC_SDS_IO_REQUEST
1074  *    to complete.
1075  *
1076  * This method is the default complete io handler.  It logs a warning and
1077  * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
1078  */
1079 enum sci_status scic_sds_remote_device_default_complete_request_handler(
1080         struct sci_base_remote_device *device,
1081         struct sci_base_request *request)
1082 {
1083         struct scic_sds_remote_device *sds_device =
1084                 (struct scic_sds_remote_device *)device;
1085
1086         dev_warn(scirdev_to_dev(sds_device),
1087                  "%s: SCIC Remote Device requested to complete io_request %p "
1088                  "while in wrong state %d\n",
1089                  __func__,
1090                  request,
1091                  sci_base_state_machine_get_state(
1092                          scic_sds_remote_device_get_base_state_machine(
1093                                  sds_device)));
1094
1095         return SCI_FAILURE_INVALID_STATE;
1096 }
1097
1098 /**
1099  *
1100  * @device: The struct sci_base_remote_device which is then cast into a
1101  *    struct scic_sds_remote_device.
1102  * @request: The struct sci_base_request which is then cast into a SCIC_SDS_IO_REQUEST
1103  *    to continue.
1104  *
1105  * This method is the default continue io handler.  It logs a warning and
1106  * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
1107  */
1108 enum sci_status scic_sds_remote_device_default_continue_request_handler(
1109         struct sci_base_remote_device *device,
1110         struct sci_base_request *request)
1111 {
1112         struct scic_sds_remote_device *sds_device =
1113                 (struct scic_sds_remote_device *)device;
1114
1115         dev_warn(scirdev_to_dev(sds_device),
1116                  "%s: SCIC Remote Device requested to continue io request %p "
1117                  "while in wrong state %d\n",
1118                  __func__,
1119                  request,
1120                  sci_base_state_machine_get_state(
1121                          scic_sds_remote_device_get_base_state_machine(
1122                          sds_device)));
1123
1124         return SCI_FAILURE_INVALID_STATE;
1125 }
1126
1127 /**
1128  *
1129  * @device: The struct sci_base_remote_device which is then cast into a
1130  *    struct scic_sds_remote_device.
1131  * @request: The struct sci_base_request which is then cast into a SCIC_SDS_IO_REQUEST
1132  *    to complete.
1133  *
1134  * This method is the default complete task handler.  It logs a warning and
1135  * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
1136  */
1137
1138 /*
1139  * *****************************************************************************
1140  * *  NORMAL STATE HANDLERS
1141  * ***************************************************************************** */
1142
1143 /**
1144  *
1145  * @device: The struct sci_base_remote_device which is then cast into a
1146  *    struct scic_sds_remote_device.
1147  * @frame_index: The frame index for which the struct scic_sds_controller wants this
1148  *    device object to process.
1149  *
1150  * This method is a general ssp frame handler.  In most cases the device object
1151  * needs to route the unsolicited frame processing to the io request object.
1152  * This method decodes the tag for the io request object and routes the
1153  * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
1154  */
1155 enum sci_status scic_sds_remote_device_general_frame_handler(
1156         struct scic_sds_remote_device *this_device,
1157         u32 frame_index)
1158 {
1159         enum sci_status result;
1160         struct sci_ssp_frame_header *frame_header;
1161         struct scic_sds_request *io_request;
1162
1163         result = scic_sds_unsolicited_frame_control_get_header(
1164                 &(scic_sds_remote_device_get_controller(this_device)->uf_control),
1165                 frame_index,
1166                 (void **)&frame_header
1167                 );
1168
1169         if (SCI_SUCCESS == result) {
1170                 io_request = scic_sds_controller_get_io_request_from_tag(
1171                         scic_sds_remote_device_get_controller(this_device), frame_header->tag);
1172
1173                 if ((io_request == SCI_INVALID_HANDLE)
1174                     || (io_request->target_device != this_device)) {
1175                         /*
1176                          * We could not map this tag to a valid IO request
1177                          * Just toss the frame and continue */
1178                         scic_sds_controller_release_frame(
1179                                 scic_sds_remote_device_get_controller(this_device), frame_index
1180                                 );
1181                 } else {
1182                         /* The IO request is now in charge of releasing the frame */
1183                         result = io_request->state_handlers->frame_handler(
1184                                 io_request, frame_index);
1185                 }
1186         }
1187
1188         return result;
1189 }
1190
1191 /**
1192  *
1193  * @[in]: this_device This is the device object that is receiving the event.
1194  * @[in]: event_code The event code to process.
1195  *
1196  * This is a common method for handling events reported to the remote device
1197  * from the controller object. enum sci_status
1198  */
1199 enum sci_status scic_sds_remote_device_general_event_handler(
1200         struct scic_sds_remote_device *this_device,
1201         u32 event_code)
1202 {
1203         return scic_sds_remote_device_core_event_handler(this_device,
1204                                                           event_code,
1205                                                           true);
1206 }
1207
1208 /*
1209  * *****************************************************************************
1210  * *  STOPPED STATE HANDLERS
1211  * ***************************************************************************** */
1212
1213 /**
1214  *
1215  * @device:
1216  *
1217  * This method takes the struct scic_sds_remote_device from a stopped state and
1218  * attempts to start it.   The RNC buffer for the device is constructed and the
1219  * device state machine is transitioned to the
1220  * SCIC_BASE_REMOTE_DEVICE_STATE_STARTING. enum sci_status SCI_SUCCESS if there is
1221  * an RNC buffer available to construct the remote device.
1222  * SCI_FAILURE_INSUFFICIENT_RESOURCES if there is no RNC buffer available in
1223  * which to construct the remote device.
1224  */
1225 static enum sci_status scic_sds_remote_device_stopped_state_start_handler(
1226         struct sci_base_remote_device *device)
1227 {
1228         enum sci_status status;
1229         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)device;
1230
1231         status = scic_sds_remote_node_context_resume(
1232                 this_device->rnc,
1233                 scic_sds_remote_device_resume_complete_handler,
1234                 this_device
1235                 );
1236
1237         if (status == SCI_SUCCESS) {
1238                 sci_base_state_machine_change_state(
1239                         scic_sds_remote_device_get_base_state_machine(this_device),
1240                         SCI_BASE_REMOTE_DEVICE_STATE_STARTING
1241                         );
1242         }
1243
1244         return status;
1245 }
1246
1247 /**
1248  *
1249  * @this_device: The struct sci_base_remote_device which is cast into a
1250  *    struct scic_sds_remote_device.
1251  *
1252  * This method will stop a struct scic_sds_remote_device that is already in a stopped
1253  * state.  This is not considered an error since the device is already stopped.
1254  * enum sci_status SCI_SUCCESS
1255  */
1256 static enum sci_status scic_sds_remote_device_stopped_state_stop_handler(
1257         struct sci_base_remote_device *this_device)
1258 {
1259         return SCI_SUCCESS;
1260 }
1261
1262 /**
1263  *
1264  * @this_device: The struct sci_base_remote_device which is cast into a
1265  *    struct scic_sds_remote_device.
1266  *
1267  * This method will destruct a struct scic_sds_remote_device that is in a stopped
1268  * state.  This is the only state from which a destruct request will succeed.
1269  * The RNi for this struct scic_sds_remote_device is returned to the free pool and the
1270  * device object transitions to the SCI_BASE_REMOTE_DEVICE_STATE_FINAL.
1271  * enum sci_status SCI_SUCCESS
1272  */
1273 static enum sci_status scic_sds_remote_device_stopped_state_destruct_handler(
1274         struct sci_base_remote_device *device)
1275 {
1276         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)device;
1277
1278         scic_sds_controller_free_remote_node_context(
1279                 scic_sds_remote_device_get_controller(this_device),
1280                 this_device,
1281                 this_device->rnc->remote_node_index
1282                 );
1283
1284         scic_sds_remote_node_context_set_remote_node_index(
1285                 this_device->rnc,
1286                 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX
1287                 );
1288
1289         scic_sds_port_set_direct_attached_device_id(
1290                 this_device->owning_port,
1291                 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX
1292                 );
1293
1294         sci_base_state_machine_change_state(
1295                 scic_sds_remote_device_get_base_state_machine(this_device),
1296                 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1297                 );
1298
1299         return SCI_SUCCESS;
1300 }
1301
1302 /*
1303  * *****************************************************************************
1304  * *  STARTING STATE HANDLERS
1305  * ***************************************************************************** */
1306
1307 static enum sci_status scic_sds_remote_device_starting_state_stop_handler(
1308         struct sci_base_remote_device *device)
1309 {
1310         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)device;
1311
1312         /*
1313          * This device has not yet started so there had better be no IO requests
1314          */
1315         BUG_ON(this_device->started_request_count != 0);
1316
1317         /*
1318          * Destroy the remote node context
1319          */
1320         scic_sds_remote_node_context_destruct(
1321                 this_device->rnc,
1322                 scic_sds_cb_remote_device_rnc_destruct_complete,
1323                 this_device
1324                 );
1325
1326         /*
1327          * Transition to the stopping state and wait for the remote node to
1328          * complete being posted and invalidated.
1329          */
1330         sci_base_state_machine_change_state(
1331                 scic_sds_remote_device_get_base_state_machine(this_device),
1332                 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1333                 );
1334
1335         return SCI_SUCCESS;
1336 }
1337
1338 /*
1339  * *****************************************************************************
1340  * *  INITIALIZING STATE HANDLERS
1341  * ***************************************************************************** */
1342
1343 /* There is nothing to do here for SSP devices */
1344
1345 /*
1346  * *****************************************************************************
1347  * *  READY STATE HANDLERS
1348  * ***************************************************************************** */
1349
1350 /**
1351  *
1352  * @this_device: The struct scic_sds_remote_device object to be suspended.
1353  *
1354  * This method is the resume handler for the struct scic_sds_remote_device object. It
1355  * will post an RNC resume to the SCU hardware. enum sci_status SCI_SUCCESS
1356  */
1357
1358 /**
1359  *
1360  * @device: The struct sci_base_remote_device object which is cast to a
1361  *    struct scic_sds_remote_device object.
1362  *
1363  * This method is the default stop handler for the struct scic_sds_remote_device ready
1364  * substate machine. It will stop the current substate machine and transition
1365  * the base state machine to SCI_BASE_REMOTE_DEVICE_STATE_STOPPING. enum sci_status
1366  * SCI_SUCCESS
1367  */
1368 enum sci_status scic_sds_remote_device_ready_state_stop_handler(
1369         struct sci_base_remote_device *device)
1370 {
1371         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)device;
1372         enum sci_status status      = SCI_SUCCESS;
1373
1374         /* Request the parent state machine to transition to the stopping state */
1375         sci_base_state_machine_change_state(
1376                 scic_sds_remote_device_get_base_state_machine(this_device),
1377                 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1378                 );
1379
1380         if (this_device->started_request_count == 0) {
1381                 scic_sds_remote_node_context_destruct(
1382                         this_device->rnc,
1383                         scic_sds_cb_remote_device_rnc_destruct_complete,
1384                         this_device
1385                         );
1386         } else
1387                 status = scic_sds_remote_device_terminate_requests(this_device);
1388
1389         return status;
1390 }
1391
1392 /**
1393  *
1394  * @device: The struct sci_base_remote_device object which is cast to a
1395  *    struct scic_sds_remote_device object.
1396  *
1397  * This is the ready state device reset handler enum sci_status
1398  */
1399 enum sci_status scic_sds_remote_device_ready_state_reset_handler(
1400         struct sci_base_remote_device *device)
1401 {
1402         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)device;
1403
1404         /* Request the parent state machine to transition to the stopping state */
1405         sci_base_state_machine_change_state(
1406                 scic_sds_remote_device_get_base_state_machine(this_device),
1407                 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1408                 );
1409
1410         return SCI_SUCCESS;
1411 }
1412
1413 /**
1414  *
1415  * @device: The struct sci_base_remote_device object which is cast to a
1416  *    struct scic_sds_remote_device object.
1417  *
1418  * This is the default fail handler for the struct scic_sds_remote_device ready
1419  * substate machine.  It will stop the current ready substate and transition
1420  * the remote device object to the SCI_BASE_REMOTE_DEVICE_STATE_FAILED.
1421  * enum sci_status SCI_SUCCESS
1422  */
1423
1424 /**
1425  *
1426  * @device: The struct sci_base_remote_device which is cast to a
1427  *    struct scic_sds_remote_device for which the request is to be started.
1428  * @request: The struct sci_base_request which is cast to a SCIC_SDS_IO_REQUEST that
1429  *    is to be started.
1430  *
1431  * This method will attempt to start a task request for this device object. The
1432  * remote device object will issue the start request for the task and if
1433  * successful it will start the request for the port object then increment its
1434  * own requet count. enum sci_status SCI_SUCCESS if the task request is started for
1435  * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
1436  * object could not get the resources to start.
1437  */
1438 static enum sci_status scic_sds_remote_device_ready_state_start_task_handler(
1439         struct sci_base_remote_device *device,
1440         struct sci_base_request *request)
1441 {
1442         enum sci_status result;
1443         struct scic_sds_remote_device *this_device  = (struct scic_sds_remote_device *)device;
1444         struct scic_sds_request *task_request = (struct scic_sds_request *)request;
1445
1446         /* See if the port is in a state where we can start the IO request */
1447         result = scic_sds_port_start_io(
1448                 scic_sds_remote_device_get_port(this_device), this_device, task_request);
1449
1450         if (result == SCI_SUCCESS) {
1451                 result = scic_sds_remote_node_context_start_task(
1452                         this_device->rnc, task_request
1453                         );
1454
1455                 if (result == SCI_SUCCESS) {
1456                         result = scic_sds_request_start(task_request);
1457                 }
1458
1459                 scic_sds_remote_device_start_request(this_device, task_request, result);
1460         }
1461
1462         return result;
1463 }
1464
1465 /**
1466  *
1467  * @device: The struct sci_base_remote_device which is cast to a
1468  *    struct scic_sds_remote_device for which the request is to be started.
1469  * @request: The struct sci_base_request which is cast to a SCIC_SDS_IO_REQUEST that
1470  *    is to be started.
1471  *
1472  * This method will attempt to start an io request for this device object. The
1473  * remote device object will issue the start request for the io and if
1474  * successful it will start the request for the port object then increment its
1475  * own requet count. enum sci_status SCI_SUCCESS if the io request is started for
1476  * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
1477  * object could not get the resources to start.
1478  */
1479 static enum sci_status scic_sds_remote_device_ready_state_start_io_handler(
1480         struct sci_base_remote_device *device,
1481         struct sci_base_request *request)
1482 {
1483         enum sci_status result;
1484         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)device;
1485         struct scic_sds_request *io_request  = (struct scic_sds_request *)request;
1486
1487         /* See if the port is in a state where we can start the IO request */
1488         result = scic_sds_port_start_io(
1489                 scic_sds_remote_device_get_port(this_device), this_device, io_request);
1490
1491         if (result == SCI_SUCCESS) {
1492                 result = scic_sds_remote_node_context_start_io(
1493                         this_device->rnc, io_request
1494                         );
1495
1496                 if (result == SCI_SUCCESS) {
1497                         result = scic_sds_request_start(io_request);
1498                 }
1499
1500                 scic_sds_remote_device_start_request(this_device, io_request, result);
1501         }
1502
1503         return result;
1504 }
1505
1506 /**
1507  *
1508  * @device: The struct sci_base_remote_device which is cast to a
1509  *    struct scic_sds_remote_device for which the request is to be completed.
1510  * @request: The struct sci_base_request which is cast to a SCIC_SDS_IO_REQUEST that
1511  *    is to be completed.
1512  *
1513  * This method will complete the request for the remote device object.  The
1514  * method will call the completion handler for the request object and if
1515  * successful it will complete the request on the port object then decrement
1516  * its own started_request_count. enum sci_status
1517  */
1518 static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
1519         struct sci_base_remote_device *device,
1520         struct sci_base_request *request)
1521 {
1522         enum sci_status result;
1523         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)device;
1524         struct scic_sds_request *the_request = (struct scic_sds_request *)request;
1525
1526         result = scic_sds_request_complete(the_request);
1527
1528         if (result == SCI_SUCCESS) {
1529                 /* See if the port is in a state where we can start the IO request */
1530                 result = scic_sds_port_complete_io(
1531                         scic_sds_remote_device_get_port(this_device), this_device, the_request);
1532
1533                 if (result == SCI_SUCCESS) {
1534                         scic_sds_remote_device_decrement_request_count(this_device);
1535                 }
1536         }
1537
1538         return result;
1539 }
1540
1541 /*
1542  * *****************************************************************************
1543  * *  STOPPING STATE HANDLERS
1544  * ***************************************************************************** */
1545
1546 /**
1547  *
1548  * @this_device: The struct sci_base_remote_device which is cast into a
1549  *    struct scic_sds_remote_device.
1550  *
1551  * This method will stop a struct scic_sds_remote_device that is already in the
1552  * SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This is not considered an error
1553  * since we allow a stop request on a device that is alreay stopping or
1554  * stopped. enum sci_status SCI_SUCCESS
1555  */
1556 static enum sci_status scic_sds_remote_device_stopping_state_stop_handler(
1557         struct sci_base_remote_device *device)
1558 {
1559         /*
1560          * All requests should have been terminated, but if there is an
1561          * attempt to stop a device already in the stopping state, then
1562          * try again to terminate. */
1563         return scic_sds_remote_device_terminate_requests(
1564                        (struct scic_sds_remote_device *)device);
1565 }
1566
1567
1568 /**
1569  *
1570  * @device: The device object for which the request is completing.
1571  * @request: The task request that is being completed.
1572  *
1573  * This method completes requests for this struct scic_sds_remote_device while it is
1574  * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
1575  * complete method for the request object and if that is successful the port
1576  * object is called to complete the task request. Then the device object itself
1577  * completes the task request. If struct scic_sds_remote_device started_request_count
1578  * goes to 0 and the invalidate RNC request has completed the device object can
1579  * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
1580  */
1581 static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
1582         struct sci_base_remote_device *device,
1583         struct sci_base_request *request)
1584 {
1585         enum sci_status status = SCI_SUCCESS;
1586         struct scic_sds_request *this_request = (struct scic_sds_request *)request;
1587         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)device;
1588
1589         status = scic_sds_request_complete(this_request);
1590         if (status == SCI_SUCCESS) {
1591                 status = scic_sds_port_complete_io(
1592                         scic_sds_remote_device_get_port(this_device),
1593                         this_device,
1594                         this_request
1595                         );
1596
1597                 if (status == SCI_SUCCESS) {
1598                         scic_sds_remote_device_decrement_request_count(this_device);
1599
1600                         if (scic_sds_remote_device_get_request_count(this_device) == 0) {
1601                                 scic_sds_remote_node_context_destruct(
1602                                         this_device->rnc,
1603                                         scic_sds_cb_remote_device_rnc_destruct_complete,
1604                                         this_device
1605                                         );
1606                         }
1607                 }
1608         }
1609
1610         return status;
1611 }
1612
1613 /*
1614  * *****************************************************************************
1615  * *  RESETTING STATE HANDLERS
1616  * ***************************************************************************** */
1617
1618 /**
1619  *
1620  * @device: The struct sci_base_remote_device which is to be cast into a
1621  *    struct scic_sds_remote_device object.
1622  *
1623  * This method will complete the reset operation when the device is in the
1624  * resetting state. enum sci_status
1625  */
1626 static enum sci_status scic_sds_remote_device_resetting_state_reset_complete_handler(
1627         struct sci_base_remote_device *device)
1628 {
1629         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)device;
1630
1631         sci_base_state_machine_change_state(
1632                 &this_device->parent.state_machine,
1633                 SCI_BASE_REMOTE_DEVICE_STATE_READY
1634                 );
1635
1636         return SCI_SUCCESS;
1637 }
1638
1639 /**
1640  *
1641  * @device: The struct sci_base_remote_device which is to be cast into a
1642  *    struct scic_sds_remote_device object.
1643  *
1644  * This method will stop the remote device while in the resetting state.
1645  * enum sci_status
1646  */
1647 static enum sci_status scic_sds_remote_device_resetting_state_stop_handler(
1648         struct sci_base_remote_device *device)
1649 {
1650         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)device;
1651
1652         sci_base_state_machine_change_state(
1653                 &this_device->parent.state_machine,
1654                 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1655                 );
1656
1657         return SCI_SUCCESS;
1658 }
1659
1660 /**
1661  *
1662  * @device: The device object for which the request is completing.
1663  * @request: The task request that is being completed.
1664  *
1665  * This method completes requests for this struct scic_sds_remote_device while it is
1666  * in the SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the
1667  * complete method for the request object and if that is successful the port
1668  * object is called to complete the task request. Then the device object itself
1669  * completes the task request. enum sci_status
1670  */
1671 static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
1672         struct sci_base_remote_device *device,
1673         struct sci_base_request *request)
1674 {
1675         enum sci_status status = SCI_SUCCESS;
1676         struct scic_sds_request *this_request = (struct scic_sds_request *)request;
1677         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)device;
1678
1679         status = scic_sds_request_complete(this_request);
1680
1681         if (status == SCI_SUCCESS) {
1682                 status = scic_sds_port_complete_io(
1683                         scic_sds_remote_device_get_port(this_device), this_device, this_request);
1684
1685                 if (status == SCI_SUCCESS) {
1686                         scic_sds_remote_device_decrement_request_count(this_device);
1687                 }
1688         }
1689
1690         return status;
1691 }
1692
1693 /*
1694  * *****************************************************************************
1695  * *  FAILED STATE HANDLERS
1696  * ***************************************************************************** */
1697
1698 /**
1699  *
1700  * @device: The struct sci_base_remote_device which is to be cast into a
1701  *    struct scic_sds_remote_device object.
1702  *
1703  * This method handles the remove request for a failed struct scic_sds_remote_device
1704  * object. The method will transition the device object to the
1705  * SCIC_BASE_REMOTE_DEVICE_STATE_STOPPING. enum sci_status SCI_SUCCESS
1706  */
1707
1708 /* --------------------------------------------------------------------------- */
1709
1710 struct scic_sds_remote_device_state_handler
1711 scic_sds_remote_device_state_handler_table[SCI_BASE_REMOTE_DEVICE_MAX_STATES] =
1712 {
1713         /* SCI_BASE_REMOTE_DEVICE_STATE_INITIAL */
1714         {
1715                 {
1716                         scic_sds_remote_device_default_start_handler,
1717                         scic_sds_remote_device_default_stop_handler,
1718                         scic_sds_remote_device_default_fail_handler,
1719                         scic_sds_remote_device_default_destruct_handler,
1720                         scic_sds_remote_device_default_reset_handler,
1721                         scic_sds_remote_device_default_reset_complete_handler,
1722                         scic_sds_remote_device_default_start_request_handler,
1723                         scic_sds_remote_device_default_complete_request_handler,
1724                         scic_sds_remote_device_default_continue_request_handler,
1725                         scic_sds_remote_device_default_start_request_handler,
1726                         scic_sds_remote_device_default_complete_request_handler
1727                 },
1728                 scic_sds_remote_device_default_suspend_handler,
1729                 scic_sds_remote_device_default_resume_handler,
1730                 scic_sds_remote_device_default_event_handler,
1731                 scic_sds_remote_device_default_frame_handler
1732         },
1733         /* SCI_BASE_REMOTE_DEVICE_STATE_STOPPED */
1734         {
1735                 {
1736                         scic_sds_remote_device_stopped_state_start_handler,
1737                         scic_sds_remote_device_stopped_state_stop_handler,
1738                         scic_sds_remote_device_default_fail_handler,
1739                         scic_sds_remote_device_stopped_state_destruct_handler,
1740                         scic_sds_remote_device_default_reset_handler,
1741                         scic_sds_remote_device_default_reset_complete_handler,
1742                         scic_sds_remote_device_default_start_request_handler,
1743                         scic_sds_remote_device_default_complete_request_handler,
1744                         scic_sds_remote_device_default_continue_request_handler,
1745                         scic_sds_remote_device_default_start_request_handler,
1746                         scic_sds_remote_device_default_complete_request_handler
1747                 },
1748                 scic_sds_remote_device_default_suspend_handler,
1749                 scic_sds_remote_device_default_resume_handler,
1750                 scic_sds_remote_device_default_event_handler,
1751                 scic_sds_remote_device_default_frame_handler
1752         },
1753         /* SCI_BASE_REMOTE_DEVICE_STATE_STARTING */
1754         {
1755                 {
1756                         scic_sds_remote_device_default_start_handler,
1757                         scic_sds_remote_device_starting_state_stop_handler,
1758                         scic_sds_remote_device_default_fail_handler,
1759                         scic_sds_remote_device_default_destruct_handler,
1760                         scic_sds_remote_device_default_reset_handler,
1761                         scic_sds_remote_device_default_reset_complete_handler,
1762                         scic_sds_remote_device_default_start_request_handler,
1763                         scic_sds_remote_device_default_complete_request_handler,
1764                         scic_sds_remote_device_default_continue_request_handler,
1765                         scic_sds_remote_device_default_start_request_handler,
1766                         scic_sds_remote_device_default_complete_request_handler
1767                 },
1768                 scic_sds_remote_device_default_suspend_handler,
1769                 scic_sds_remote_device_default_resume_handler,
1770                 scic_sds_remote_device_general_event_handler,
1771                 scic_sds_remote_device_default_frame_handler
1772         },
1773         /* SCI_BASE_REMOTE_DEVICE_STATE_READY */
1774         {
1775                 {
1776                         scic_sds_remote_device_default_start_handler,
1777                         scic_sds_remote_device_ready_state_stop_handler,
1778                         scic_sds_remote_device_default_fail_handler,
1779                         scic_sds_remote_device_default_destruct_handler,
1780                         scic_sds_remote_device_ready_state_reset_handler,
1781                         scic_sds_remote_device_default_reset_complete_handler,
1782                         scic_sds_remote_device_ready_state_start_io_handler,
1783                         scic_sds_remote_device_ready_state_complete_request_handler,
1784                         scic_sds_remote_device_default_continue_request_handler,
1785                         scic_sds_remote_device_ready_state_start_task_handler,
1786                         scic_sds_remote_device_ready_state_complete_request_handler
1787                 },
1788                 scic_sds_remote_device_default_suspend_handler,
1789                 scic_sds_remote_device_default_resume_handler,
1790                 scic_sds_remote_device_general_event_handler,
1791                 scic_sds_remote_device_general_frame_handler,
1792         },
1793         /* SCI_BASE_REMOTE_DEVICE_STATE_STOPPING */
1794         {
1795                 {
1796                         scic_sds_remote_device_default_start_handler,
1797                         scic_sds_remote_device_stopping_state_stop_handler,
1798                         scic_sds_remote_device_default_fail_handler,
1799                         scic_sds_remote_device_default_destruct_handler,
1800                         scic_sds_remote_device_default_reset_handler,
1801                         scic_sds_remote_device_default_reset_complete_handler,
1802                         scic_sds_remote_device_default_start_request_handler,
1803                         scic_sds_remote_device_stopping_state_complete_request_handler,
1804                         scic_sds_remote_device_default_continue_request_handler,
1805                         scic_sds_remote_device_default_start_request_handler,
1806                         scic_sds_remote_device_stopping_state_complete_request_handler
1807                 },
1808                 scic_sds_remote_device_default_suspend_handler,
1809                 scic_sds_remote_device_default_resume_handler,
1810                 scic_sds_remote_device_general_event_handler,
1811                 scic_sds_remote_device_general_frame_handler
1812         },
1813         /* SCI_BASE_REMOTE_DEVICE_STATE_FAILED */
1814         {
1815                 {
1816                         scic_sds_remote_device_default_start_handler,
1817                         scic_sds_remote_device_default_stop_handler,
1818                         scic_sds_remote_device_default_fail_handler,
1819                         scic_sds_remote_device_default_destruct_handler,
1820                         scic_sds_remote_device_default_reset_handler,
1821                         scic_sds_remote_device_default_reset_complete_handler,
1822                         scic_sds_remote_device_default_start_request_handler,
1823                         scic_sds_remote_device_default_complete_request_handler,
1824                         scic_sds_remote_device_default_continue_request_handler,
1825                         scic_sds_remote_device_default_start_request_handler,
1826                         scic_sds_remote_device_default_complete_request_handler
1827                 },
1828                 scic_sds_remote_device_default_suspend_handler,
1829                 scic_sds_remote_device_default_resume_handler,
1830                 scic_sds_remote_device_default_event_handler,
1831                 scic_sds_remote_device_general_frame_handler
1832         },
1833         /* SCI_BASE_REMOTE_DEVICE_STATE_RESETTING */
1834         {
1835                 {
1836                         scic_sds_remote_device_default_start_handler,
1837                         scic_sds_remote_device_resetting_state_stop_handler,
1838                         scic_sds_remote_device_default_fail_handler,
1839                         scic_sds_remote_device_default_destruct_handler,
1840                         scic_sds_remote_device_default_reset_handler,
1841                         scic_sds_remote_device_resetting_state_reset_complete_handler,
1842                         scic_sds_remote_device_default_start_request_handler,
1843                         scic_sds_remote_device_resetting_state_complete_request_handler,
1844                         scic_sds_remote_device_default_continue_request_handler,
1845                         scic_sds_remote_device_default_start_request_handler,
1846                         scic_sds_remote_device_resetting_state_complete_request_handler
1847                 },
1848                 scic_sds_remote_device_default_suspend_handler,
1849                 scic_sds_remote_device_default_resume_handler,
1850                 scic_sds_remote_device_default_event_handler,
1851                 scic_sds_remote_device_general_frame_handler
1852         },
1853         /* SCI_BASE_REMOTE_DEVICE_STATE_FINAL */
1854         {
1855                 {
1856                         scic_sds_remote_device_default_start_handler,
1857                         scic_sds_remote_device_default_stop_handler,
1858                         scic_sds_remote_device_default_fail_handler,
1859                         scic_sds_remote_device_default_destruct_handler,
1860                         scic_sds_remote_device_default_reset_handler,
1861                         scic_sds_remote_device_default_reset_complete_handler,
1862                         scic_sds_remote_device_default_start_request_handler,
1863                         scic_sds_remote_device_default_complete_request_handler,
1864                         scic_sds_remote_device_default_continue_request_handler,
1865                         scic_sds_remote_device_default_start_request_handler,
1866                         scic_sds_remote_device_default_complete_request_handler
1867                 },
1868                 scic_sds_remote_device_default_suspend_handler,
1869                 scic_sds_remote_device_default_resume_handler,
1870                 scic_sds_remote_device_default_event_handler,
1871                 scic_sds_remote_device_default_frame_handler
1872         }
1873 };
1874
1875 /*
1876  * This file is provided under a dual BSD/GPLv2 license.  When using or
1877  * redistributing this file, you may do so under either license.
1878  *
1879  * GPL LICENSE SUMMARY
1880  *
1881  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
1882  *
1883  * This program is free software; you can redistribute it and/or modify
1884  * it under the terms of version 2 of the GNU General Public License as
1885  * published by the Free Software Foundation.
1886  *
1887  * This program is distributed in the hope that it will be useful, but
1888  * WITHOUT ANY WARRANTY; without even the implied warranty of
1889  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1890  * General Public License for more details.
1891  *
1892  * You should have received a copy of the GNU General Public License
1893  * along with this program; if not, write to the Free Software
1894  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
1895  * The full GNU General Public License is included in this distribution
1896  * in the file called LICENSE.GPL.
1897  *
1898  * BSD LICENSE
1899  *
1900  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
1901  * All rights reserved.
1902  *
1903  * Redistribution and use in source and binary forms, with or without
1904  * modification, are permitted provided that the following conditions
1905  * are met:
1906  *
1907  *   * Redistributions of source code must retain the above copyright
1908  *     notice, this list of conditions and the following disclaimer.
1909  *   * Redistributions in binary form must reproduce the above copyright
1910  *     notice, this list of conditions and the following disclaimer in
1911  *     the documentation and/or other materials provided with the
1912  *     distribution.
1913  *   * Neither the name of Intel Corporation nor the names of its
1914  *     contributors may be used to endorse or promote products derived
1915  *     from this software without specific prior written permission.
1916  *
1917  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1918  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1919  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1920  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1921  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1922  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1923  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1924  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1925  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1926  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1927  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1928  */
1929
1930 /**
1931  * This file contains the operations that are taken on the enter and exit state
1932  *    transitions for the struct sci_base_remote_device state machine.
1933  *
1934  *
1935  */
1936
1937 #include "scic_remote_device.h"
1938 #include "scic_user_callback.h"
1939 #include "scic_sds_controller.h"
1940 #include "scic_sds_remote_device.h"
1941 #include "scic_sds_request.h"
1942 #include "scic_sds_controller.h"
1943 #include "scic_sds_port.h"
1944
1945 /**
1946  *
1947  * @object: This is the struct sci_base_object that is cast into a
1948  *    struct scic_sds_remote_device.
1949  *
1950  * This is the enter method for the SCI_BASE_REMOTE_DEVICE_STATE_INITIAL it
1951  * immediatly transitions the remote device object to the stopped state. none
1952  */
1953 static void scic_sds_remote_device_initial_state_enter(
1954         struct sci_base_object *object)
1955 {
1956         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)object;
1957
1958         SET_STATE_HANDLER(
1959                 this_device,
1960                 scic_sds_remote_device_state_handler_table,
1961                 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1962                 );
1963
1964         /* Initial state is a transitional state to the stopped state */
1965         sci_base_state_machine_change_state(
1966                 scic_sds_remote_device_get_base_state_machine(this_device),
1967                 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
1968                 );
1969 }
1970
1971 /**
1972  *
1973  * @object: This is the struct sci_base_object that is cast into a
1974  *    struct scic_sds_remote_device.
1975  *
1976  * This is the enter method for the SCI_BASE_REMOTE_DEVICE_STATE_INITIAL it
1977  * sets the stopped state handlers and if this state is entered from the
1978  * SCI_BASE_REMOTE_DEVICE_STATE_STOPPING then the SCI User is informed that the
1979  * device stop is complete. none
1980  */
1981 static void scic_sds_remote_device_stopped_state_enter(
1982         struct sci_base_object *object)
1983 {
1984         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)object;
1985
1986         SET_STATE_HANDLER(
1987                 this_device,
1988                 scic_sds_remote_device_state_handler_table,
1989                 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
1990                 );
1991
1992         /*
1993          * If we are entering from the stopping state let the SCI User know that
1994          * the stop operation has completed. */
1995         if (this_device->parent.state_machine.previous_state_id
1996             == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING) {
1997                 scic_cb_remote_device_stop_complete(
1998                         scic_sds_remote_device_get_controller(this_device),
1999                         this_device,
2000                         SCI_SUCCESS
2001                         );
2002         }
2003 }
2004
2005 /**
2006  *
2007  * @object: This is the struct sci_base_object that is cast into a
2008  *    struct scic_sds_remote_device.
2009  *
2010  * This is the enter method for the SCI_BASE_REMOTE_DEVICE_STATE_STARTING it
2011  * sets the starting state handlers, sets the device not ready, and posts the
2012  * remote node context to the hardware. none
2013  */
2014 static void scic_sds_remote_device_starting_state_enter(
2015         struct sci_base_object *object)
2016 {
2017         struct scic_sds_controller *the_controller;
2018         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)object;
2019
2020         the_controller = scic_sds_remote_device_get_controller(this_device);
2021
2022         SET_STATE_HANDLER(
2023                 this_device,
2024                 scic_sds_remote_device_state_handler_table,
2025                 SCI_BASE_REMOTE_DEVICE_STATE_STARTING
2026                 );
2027
2028         scic_cb_remote_device_not_ready(
2029                 the_controller,
2030                 this_device,
2031                 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED
2032                 );
2033 }
2034
2035 /**
2036  *
2037  * @object: This is the struct sci_base_object that is cast into a
2038  *    struct scic_sds_remote_device.
2039  *
2040  * This is the exit method for the SCI_BASE_REMOTE_DEVICE_STATE_STARTING it
2041  * reports that the device start is complete. none
2042  */
2043 static void scic_sds_remote_device_starting_state_exit(
2044         struct sci_base_object *object)
2045 {
2046         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)object;
2047
2048         /*
2049          * / @todo Check the device object for the proper return code for this
2050          * /       callback */
2051         scic_cb_remote_device_start_complete(
2052                 scic_sds_remote_device_get_controller(this_device),
2053                 this_device,
2054                 SCI_SUCCESS
2055                 );
2056 }
2057
2058 /**
2059  *
2060  * @object: This is the struct sci_base_object that is cast into a
2061  *    struct scic_sds_remote_device.
2062  *
2063  * This is the enter method for the SCI_BASE_REMOTE_DEVICE_STATE_READY it sets
2064  * the ready state handlers, and starts the ready substate machine. none
2065  */
2066 static void scic_sds_remote_device_ready_state_enter(
2067         struct sci_base_object *object)
2068 {
2069         struct scic_sds_controller *the_controller;
2070         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)object;
2071
2072         the_controller = scic_sds_remote_device_get_controller(this_device);
2073
2074         SET_STATE_HANDLER(
2075                 this_device,
2076                 scic_sds_remote_device_state_handler_table,
2077                 SCI_BASE_REMOTE_DEVICE_STATE_READY
2078                 );
2079
2080         the_controller->remote_device_sequence[this_device->rnc->remote_node_index]++;
2081
2082         if (this_device->has_ready_substate_machine) {
2083                 sci_base_state_machine_start(&this_device->ready_substate_machine);
2084         } else {
2085                 scic_cb_remote_device_ready(the_controller, this_device);
2086         }
2087 }
2088
2089 /**
2090  *
2091  * @object: This is the struct sci_base_object that is cast into a
2092  *    struct scic_sds_remote_device.
2093  *
2094  * This is the exit method for the SCI_BASE_REMOTE_DEVICE_STATE_READY it does
2095  * nothing. none
2096  */
2097 static void scic_sds_remote_device_ready_state_exit(
2098         struct sci_base_object *object)
2099 {
2100         struct scic_sds_controller *the_controller;
2101         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)object;
2102
2103         the_controller = scic_sds_remote_device_get_controller(this_device);
2104
2105         if (this_device->has_ready_substate_machine) {
2106                 sci_base_state_machine_stop(&this_device->ready_substate_machine);
2107         } else {
2108                 scic_cb_remote_device_not_ready(
2109                         the_controller,
2110                         this_device,
2111                         SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED
2112                         );
2113         }
2114 }
2115
2116 /**
2117  *
2118  * @object: This is the struct sci_base_object that is cast into a
2119  *    struct scic_sds_remote_device.
2120  *
2121  * This is the enter method for the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING it
2122  * sets the stopping state handlers and posts an RNC invalidate request to the
2123  * SCU hardware. none
2124  */
2125 static void scic_sds_remote_device_stopping_state_enter(
2126         struct sci_base_object *object)
2127 {
2128         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)object;
2129
2130         SET_STATE_HANDLER(
2131                 this_device,
2132                 scic_sds_remote_device_state_handler_table,
2133                 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
2134                 );
2135 }
2136
2137 /**
2138  *
2139  * @object: This is the struct sci_base_object that is cast into a
2140  *    struct scic_sds_remote_device.
2141  *
2142  * This is the enter method for the SCI_BASE_REMOTE_DEVICE_STATE_FAILED it sets
2143  * the stopping state handlers. none
2144  */
2145 static void scic_sds_remote_device_failed_state_enter(
2146         struct sci_base_object *object)
2147 {
2148         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)object;
2149
2150         SET_STATE_HANDLER(
2151                 this_device,
2152                 scic_sds_remote_device_state_handler_table,
2153                 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
2154                 );
2155 }
2156
2157 /**
2158  *
2159  * @object: This is the struct sci_base_object that is cast into a
2160  *    struct scic_sds_remote_device.
2161  *
2162  * This is the enter method for the SCI_BASE_REMOTE_DEVICE_STATE_RESETTING it
2163  * sets the resetting state handlers. none
2164  */
2165 static void scic_sds_remote_device_resetting_state_enter(
2166         struct sci_base_object *object)
2167 {
2168         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)object;
2169
2170         SET_STATE_HANDLER(
2171                 this_device,
2172                 scic_sds_remote_device_state_handler_table,
2173                 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
2174                 );
2175
2176         scic_sds_remote_node_context_suspend(
2177                 this_device->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
2178 }
2179
2180 /**
2181  *
2182  * @object: This is the struct sci_base_object that is cast into a
2183  *    struct scic_sds_remote_device.
2184  *
2185  * This is the exit method for the SCI_BASE_REMOTE_DEVICE_STATE_RESETTING it
2186  * does nothing. none
2187  */
2188 static void scic_sds_remote_device_resetting_state_exit(
2189         struct sci_base_object *object)
2190 {
2191         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)object;
2192
2193         scic_sds_port_set_direct_attached_device_id(
2194                 scic_sds_remote_device_get_port(this_device),
2195                 this_device->rnc->remote_node_index
2196                 );
2197
2198         scic_sds_remote_node_context_resume(this_device->rnc, NULL, NULL);
2199 }
2200
2201 /**
2202  *
2203  * @object: This is the struct sci_base_object that is cast into a
2204  *    struct scic_sds_remote_device.
2205  *
2206  * This is the enter method for the SCI_BASE_REMOTE_DEVICE_STATE_FINAL it sets
2207  * the final state handlers. none
2208  */
2209 static void scic_sds_remote_device_final_state_enter(
2210         struct sci_base_object *object)
2211 {
2212         struct scic_sds_remote_device *this_device = (struct scic_sds_remote_device *)object;
2213
2214         SET_STATE_HANDLER(
2215                 this_device,
2216                 scic_sds_remote_device_state_handler_table,
2217                 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
2218                 );
2219 }
2220
2221 /* --------------------------------------------------------------------------- */
2222
2223 const struct sci_base_state scic_sds_remote_device_state_table[] = {
2224         [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
2225                 .enter_state = scic_sds_remote_device_initial_state_enter,
2226         },
2227         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
2228                 .enter_state = scic_sds_remote_device_stopped_state_enter,
2229         },
2230         [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
2231                 .enter_state = scic_sds_remote_device_starting_state_enter,
2232                 .exit_state  = scic_sds_remote_device_starting_state_exit
2233         },
2234         [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
2235                 .enter_state = scic_sds_remote_device_ready_state_enter,
2236                 .exit_state  = scic_sds_remote_device_ready_state_exit
2237         },
2238         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
2239                 .enter_state = scic_sds_remote_device_stopping_state_enter,
2240         },
2241         [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
2242                 .enter_state = scic_sds_remote_device_failed_state_enter,
2243         },
2244         [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
2245                 .enter_state = scic_sds_remote_device_resetting_state_enter,
2246                 .exit_state  = scic_sds_remote_device_resetting_state_exit
2247         },
2248         [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
2249                 .enter_state = scic_sds_remote_device_final_state_enter,
2250         },
2251 };
2252