]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/scsi/isci/core/scic_sds_remote_device.c
isci: unify remote_device data structures
[karo-tx-linux.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 #include "intel_sas.h"
57 #include "scic_controller.h"
58 #include "scic_phy.h"
59 #include "scic_port.h"
60 #include "scic_remote_device.h"
61 #include "scic_sds_controller.h"
62 #include "scic_sds_phy.h"
63 #include "scic_sds_port.h"
64 #include "scic_sds_remote_device.h"
65 #include "scic_sds_remote_node_context.h"
66 #include "scic_sds_request.h"
67 #include "sci_environment.h"
68 #include "sci_util.h"
69 #include "scu_event_codes.h"
70
71
72 #define SCIC_SDS_REMOTE_DEVICE_RESET_TIMEOUT  (1000)
73
74 enum sci_status scic_remote_device_da_construct(
75         struct scic_sds_remote_device *sci_dev)
76 {
77         enum sci_status status;
78         u16 remote_node_index;
79         struct sci_sas_identify_address_frame_protocols protocols;
80
81         /*
82          * This information is request to determine how many remote node context
83          * entries will be needed to store the remote node.
84          */
85         scic_sds_port_get_attached_protocols(sci_dev->owning_port, &protocols);
86         sci_dev->target_protocols.u.all = protocols.u.all;
87         sci_dev->is_direct_attached = true;
88 #if !defined(DISABLE_ATAPI)
89         sci_dev->is_atapi = scic_sds_remote_device_is_atapi(sci_dev);
90 #endif
91
92         status = scic_sds_controller_allocate_remote_node_context(
93                 sci_dev->owning_port->owning_controller,
94                 sci_dev,
95                 &remote_node_index);
96
97         if (status == SCI_SUCCESS) {
98                 sci_dev->rnc.remote_node_index = remote_node_index;
99
100                 scic_sds_port_get_attached_sas_address(
101                         sci_dev->owning_port, &sci_dev->device_address);
102
103                 if (sci_dev->target_protocols.u.bits.attached_ssp_target) {
104                         sci_dev->has_ready_substate_machine = false;
105                 } else if (sci_dev->target_protocols.u.bits.attached_stp_target) {
106                         sci_dev->has_ready_substate_machine = true;
107
108                         sci_base_state_machine_construct(
109                                 &sci_dev->ready_substate_machine,
110                                 &sci_dev->parent,
111                                 scic_sds_stp_remote_device_ready_substate_table,
112                                 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
113                 } else if (sci_dev->target_protocols.u.bits.attached_smp_target) {
114                         sci_dev->has_ready_substate_machine = true;
115
116                         /* add the SMP ready substate machine construction here */
117                         sci_base_state_machine_construct(
118                                 &sci_dev->ready_substate_machine,
119                                 &sci_dev->parent,
120                                 scic_sds_smp_remote_device_ready_substate_table,
121                                 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
122                 }
123
124                 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(
125                         sci_dev->owning_port);
126
127                 /* / @todo Should I assign the port width by reading all of the phys on the port? */
128                 sci_dev->device_port_width = 1;
129         }
130
131         return status;
132 }
133
134
135 static void scic_sds_remote_device_get_info_from_smp_discover_response(
136         struct scic_sds_remote_device *sci_dev,
137         struct smp_response_discover *discover_response)
138 {
139         /* decode discover_response to set sas_address to sci_dev. */
140         sci_dev->device_address.high =
141                 discover_response->attached_sas_address.high;
142
143         sci_dev->device_address.low =
144                 discover_response->attached_sas_address.low;
145
146         sci_dev->target_protocols.u.all = discover_response->protocols.u.all;
147 }
148
149
150 enum sci_status scic_remote_device_ea_construct(
151         struct scic_sds_remote_device *sci_dev,
152         struct smp_response_discover *discover_response)
153 {
154         enum sci_status status;
155         struct scic_sds_controller *scic;
156
157         scic = scic_sds_port_get_controller(sci_dev->owning_port);
158
159         scic_sds_remote_device_get_info_from_smp_discover_response(
160                 sci_dev, discover_response);
161
162         status = scic_sds_controller_allocate_remote_node_context(
163                 scic, sci_dev, &sci_dev->rnc.remote_node_index);
164
165         if (status == SCI_SUCCESS) {
166                 if (sci_dev->target_protocols.u.bits.attached_ssp_target) {
167                         sci_dev->has_ready_substate_machine = false;
168                 } else if (sci_dev->target_protocols.u.bits.attached_smp_target) {
169                         sci_dev->has_ready_substate_machine = true;
170
171                         /* add the SMP ready substate machine construction here */
172                         sci_base_state_machine_construct(
173                                 &sci_dev->ready_substate_machine,
174                                 &sci_dev->parent,
175                                 scic_sds_smp_remote_device_ready_substate_table,
176                                 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
177                 } else if (sci_dev->target_protocols.u.bits.attached_stp_target) {
178                         sci_dev->has_ready_substate_machine = true;
179
180                         sci_base_state_machine_construct(
181                                 &sci_dev->ready_substate_machine,
182                                 &sci_dev->parent,
183                                 scic_sds_stp_remote_device_ready_substate_table,
184                                 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
185                 }
186
187                 /*
188                  * For SAS-2 the physical link rate is actually a logical link
189                  * rate that incorporates multiplexing.  The SCU doesn't
190                  * incorporate multiplexing and for the purposes of the
191                  * connection the logical link rate is that same as the
192                  * physical.  Furthermore, the SAS-2 and SAS-1.1 fields overlay
193                  * one another, so this code works for both situations. */
194                 sci_dev->connection_rate = min_t(u16,
195                         scic_sds_port_get_max_allowed_speed(sci_dev->owning_port),
196                         discover_response->u2.sas1_1.negotiated_physical_link_rate
197                         );
198
199                 /* / @todo Should I assign the port width by reading all of the phys on the port? */
200                 sci_dev->device_port_width = 1;
201         }
202
203         return status;
204 }
205
206 enum sci_status scic_remote_device_destruct(
207         struct scic_sds_remote_device *sci_dev)
208 {
209         return sci_dev->state_handlers->destruct_handler(sci_dev);
210 }
211
212
213 enum sci_status scic_remote_device_start(
214         struct scic_sds_remote_device *sci_dev,
215         u32 timeout)
216 {
217         return sci_dev->state_handlers->start_handler(sci_dev);
218 }
219
220
221 enum sci_status scic_remote_device_stop(
222         struct scic_sds_remote_device *sci_dev,
223         u32 timeout)
224 {
225         return sci_dev->state_handlers->stop_handler(sci_dev);
226 }
227
228
229 enum sci_status scic_remote_device_reset(
230         struct scic_sds_remote_device *sci_dev)
231 {
232         return sci_dev->state_handlers->reset_handler(sci_dev);
233 }
234
235
236 enum sci_status scic_remote_device_reset_complete(
237         struct scic_sds_remote_device *sci_dev)
238 {
239         return sci_dev->state_handlers->reset_complete_handler(sci_dev);
240 }
241
242
243 enum sas_linkrate scic_remote_device_get_connection_rate(
244         struct scic_sds_remote_device *sci_dev)
245 {
246         return sci_dev->connection_rate;
247 }
248
249
250 void scic_remote_device_get_protocols(
251         struct scic_sds_remote_device *sci_dev,
252         struct smp_discover_response_protocols *pr)
253 {
254         pr->u.all = sci_dev->target_protocols.u.all;
255 }
256
257 #if !defined(DISABLE_ATAPI)
258 bool scic_remote_device_is_atapi(struct scic_sds_remote_device *sci_dev)
259 {
260         return sci_dev->is_atapi;
261 }
262 #endif
263
264
265 /*
266  * *****************************************************************************
267  * *  SCU DRIVER STANDARD (SDS) REMOTE DEVICE IMPLEMENTATIONS
268  * ***************************************************************************** */
269
270 /**
271  *
272  *
273  * Remote device timer requirements
274  */
275 #define SCIC_SDS_REMOTE_DEVICE_MINIMUM_TIMER_COUNT (0)
276 #define SCIC_SDS_REMOTE_DEVICE_MAXIMUM_TIMER_COUNT (SCI_MAX_REMOTE_DEVICES)
277
278
279 /**
280  *
281  * @sci_dev: The remote device for which the suspend is being requested.
282  *
283  * This method invokes the remote device suspend state handler. enum sci_status
284  */
285 enum sci_status scic_sds_remote_device_suspend(
286         struct scic_sds_remote_device *sci_dev,
287         u32 suspend_type)
288 {
289         return sci_dev->state_handlers->suspend_handler(sci_dev, suspend_type);
290 }
291
292 /**
293  *
294  * @sci_dev: The remote device for which the resume is being requested.
295  *
296  * This method invokes the remote device resume state handler. enum sci_status
297  */
298 enum sci_status scic_sds_remote_device_resume(
299         struct scic_sds_remote_device *sci_dev)
300 {
301         return sci_dev->state_handlers->resume_handler(sci_dev);
302 }
303
304 /**
305  *
306  * @sci_dev: The remote device for which the event handling is being
307  *    requested.
308  * @frame_index: This is the frame index that is being processed.
309  *
310  * This method invokes the frame handler for the remote device state machine
311  * enum sci_status
312  */
313 enum sci_status scic_sds_remote_device_frame_handler(
314         struct scic_sds_remote_device *sci_dev,
315         u32 frame_index)
316 {
317         return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
318 }
319
320 /**
321  *
322  * @sci_dev: The remote device for which the event handling is being
323  *    requested.
324  * @event_code: This is the event code that is to be processed.
325  *
326  * This method invokes the remote device event handler. enum sci_status
327  */
328 enum sci_status scic_sds_remote_device_event_handler(
329         struct scic_sds_remote_device *sci_dev,
330         u32 event_code)
331 {
332         return sci_dev->state_handlers->event_handler(sci_dev, event_code);
333 }
334
335 /**
336  *
337  * @controller: The controller that is starting the io request.
338  * @sci_dev: The remote device for which the start io handling is being
339  *    requested.
340  * @io_request: The io request that is being started.
341  *
342  * This method invokes the remote device start io handler. enum sci_status
343  */
344 enum sci_status scic_sds_remote_device_start_io(
345         struct scic_sds_controller *controller,
346         struct scic_sds_remote_device *sci_dev,
347         struct scic_sds_request *io_request)
348 {
349         return sci_dev->state_handlers->start_io_handler(
350                        sci_dev, io_request);
351 }
352
353 /**
354  *
355  * @controller: The controller that is completing the io request.
356  * @sci_dev: The remote device for which the complete io handling is being
357  *    requested.
358  * @io_request: The io request that is being completed.
359  *
360  * This method invokes the remote device complete io handler. enum sci_status
361  */
362 enum sci_status scic_sds_remote_device_complete_io(
363         struct scic_sds_controller *controller,
364         struct scic_sds_remote_device *sci_dev,
365         struct scic_sds_request *io_request)
366 {
367         return sci_dev->state_handlers->complete_io_handler(
368                        sci_dev, io_request);
369 }
370
371 /**
372  *
373  * @controller: The controller that is starting the task request.
374  * @sci_dev: The remote device for which the start task handling is being
375  *    requested.
376  * @io_request: The task request that is being started.
377  *
378  * This method invokes the remote device start task handler. enum sci_status
379  */
380 enum sci_status scic_sds_remote_device_start_task(
381         struct scic_sds_controller *controller,
382         struct scic_sds_remote_device *sci_dev,
383         struct scic_sds_request *io_request)
384 {
385         return sci_dev->state_handlers->start_task_handler(
386                        sci_dev, io_request);
387 }
388
389 /**
390  *
391  * @controller: The controller that is completing the task request.
392  * @sci_dev: The remote device for which the complete task handling is
393  *    being requested.
394  * @io_request: The task request that is being completed.
395  *
396  * This method invokes the remote device complete task handler. enum sci_status
397  */
398
399 /**
400  *
401  * @sci_dev:
402  * @request:
403  *
404  * This method takes the request and bulids an appropriate SCU context for the
405  * request and then requests the controller to post the request. none
406  */
407 void scic_sds_remote_device_post_request(
408         struct scic_sds_remote_device *sci_dev,
409         u32 request)
410 {
411         u32 context;
412
413         context = scic_sds_remote_device_build_command_context(sci_dev, request);
414
415         scic_sds_controller_post_request(
416                 scic_sds_remote_device_get_controller(sci_dev),
417                 context
418                 );
419 }
420
421 #if !defined(DISABLE_ATAPI)
422 /**
423  *
424  * @sci_dev: The device to be checked.
425  *
426  * This method check the signature fis of a stp device to decide whether a
427  * device is atapi or not. true if a device is atapi device. False if a device
428  * is not atapi.
429  */
430 bool scic_sds_remote_device_is_atapi(
431         struct scic_sds_remote_device *sci_dev)
432 {
433         if (!sci_dev->target_protocols.u.bits.attached_stp_target)
434                 return false;
435         else if (sci_dev->is_direct_attached) {
436                 struct scic_sds_phy *phy;
437                 struct scic_sata_phy_properties properties;
438                 struct sata_fis_reg_d2h *signature_fis;
439                 phy = scic_sds_port_get_a_connected_phy(sci_dev->owning_port);
440                 scic_sata_phy_get_properties(phy, &properties);
441
442                 /* decode the signature fis. */
443                 signature_fis = &(properties.signature_fis);
444
445                 if ((signature_fis->sector_count  == 0x01)
446                     && (signature_fis->lba_low       == 0x01)
447                     && (signature_fis->lba_mid       == 0x14)
448                     && (signature_fis->lba_high      == 0xEB)
449                     && ((signature_fis->device & 0x5F) == 0x00)
450                     ) {
451                         /* An ATA device supporting the PACKET command set. */
452                         return true;
453                 } else
454                         return false;
455         } else {
456                 /* Expander supported ATAPI device is not currently supported. */
457                 return false;
458         }
459 }
460 #endif
461
462 /**
463  *
464  * @user_parameter: This is cast to a remote device object.
465  *
466  * This method is called once the remote node context is ready to be freed.
467  * The remote device can now report that its stop operation is complete. none
468  */
469 static void scic_sds_cb_remote_device_rnc_destruct_complete(
470         void *user_parameter)
471 {
472         struct scic_sds_remote_device *sci_dev;
473
474         sci_dev = (struct scic_sds_remote_device *)user_parameter;
475
476         BUG_ON(sci_dev->started_request_count != 0);
477
478         sci_base_state_machine_change_state(&sci_dev->state_machine,
479                                             SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
480 }
481
482 /**
483  *
484  * @user_parameter: This is cast to a remote device object.
485  *
486  * This method is called once the remote node context has transisitioned to a
487  * ready state.  This is the indication that the remote device object can also
488  * transition to ready. none
489  */
490 static void scic_sds_remote_device_resume_complete_handler(
491         void *user_parameter)
492 {
493         struct scic_sds_remote_device *sci_dev;
494
495         sci_dev = (struct scic_sds_remote_device *)user_parameter;
496
497         if (
498                 sci_base_state_machine_get_state(&sci_dev->state_machine)
499                 != SCI_BASE_REMOTE_DEVICE_STATE_READY
500                 ) {
501                 sci_base_state_machine_change_state(
502                         &sci_dev->state_machine,
503                         SCI_BASE_REMOTE_DEVICE_STATE_READY
504                         );
505         }
506 }
507
508 /**
509  *
510  * @device: This parameter specifies the device for which the request is being
511  *    started.
512  * @request: This parameter specifies the request being started.
513  * @status: This parameter specifies the current start operation status.
514  *
515  * This method will perform the STP request start processing common to IO
516  * requests and task requests of all types. none
517  */
518 void scic_sds_remote_device_start_request(
519         struct scic_sds_remote_device *sci_dev,
520         struct scic_sds_request *sci_req,
521         enum sci_status status)
522 {
523         /* We still have a fault in starting the io complete it on the port */
524         if (status == SCI_SUCCESS)
525                 scic_sds_remote_device_increment_request_count(sci_dev);
526         else{
527                 sci_dev->owning_port->state_handlers->complete_io_handler(
528                         sci_dev->owning_port, sci_dev, sci_req
529                         );
530         }
531 }
532
533
534 /**
535  *
536  * @request: This parameter specifies the request being continued.
537  *
538  * This method will continue to post tc for a STP request. This method usually
539  * serves as a callback when RNC gets resumed during a task management
540  * sequence. none
541  */
542 void scic_sds_remote_device_continue_request(void *dev)
543 {
544         struct scic_sds_remote_device *sci_dev = dev;
545
546         /* we need to check if this request is still valid to continue. */
547         if (sci_dev->working_request)
548                 scic_controller_continue_io(sci_dev->working_request);
549 }
550
551 /**
552  *
553  * @user_parameter: This is cast to a remote device object.
554  *
555  * This method is called once the remote node context has reached a suspended
556  * state. The remote device can now report that its suspend operation is
557  * complete. none
558  */
559
560 /**
561  * This method will terminate all of the IO requests in the controllers IO
562  *    request table that were targeted for this device.
563  * @sci_dev: This parameter specifies the remote device for which to
564  *    attempt to terminate all requests.
565  *
566  * This method returns an indication as to whether all requests were
567  * successfully terminated.  If a single request fails to be terminated, then
568  * this method will return the failure.
569  */
570 static enum sci_status scic_sds_remote_device_terminate_requests(
571         struct scic_sds_remote_device *sci_dev)
572 {
573         enum sci_status status           = SCI_SUCCESS;
574         enum sci_status terminate_status = SCI_SUCCESS;
575         struct scic_sds_request *sci_req;
576         u32 index;
577         u32 request_count    = sci_dev->started_request_count;
578
579         for (index = 0;
580              (index < SCI_MAX_IO_REQUESTS) && (request_count > 0);
581              index++) {
582                 sci_req = sci_dev->owning_port->owning_controller->io_request_table[index];
583
584                 if ((sci_req != NULL) && (sci_req->target_device == sci_dev)) {
585                         terminate_status = scic_controller_terminate_request(
586                                 sci_dev->owning_port->owning_controller,
587                                 sci_dev,
588                                 sci_req
589                                 );
590
591                         if (terminate_status != SCI_SUCCESS)
592                                 status = terminate_status;
593
594                         request_count--;
595                 }
596         }
597
598         return status;
599 }
600
601 static enum sci_status
602 default_device_handler(struct scic_sds_remote_device *sci_dev,
603                        const char *func)
604 {
605         dev_warn(scirdev_to_dev(sci_dev),
606                  "%s: in wrong state: %d\n", func,
607                  sci_base_state_machine_get_state(&sci_dev->state_machine));
608         return SCI_FAILURE_INVALID_STATE;
609 }
610
611 enum sci_status scic_sds_remote_device_default_start_handler(
612         struct scic_sds_remote_device *sci_dev)
613 {
614         return default_device_handler(sci_dev, __func__);
615 }
616
617 static enum sci_status scic_sds_remote_device_default_stop_handler(
618         struct scic_sds_remote_device *sci_dev)
619 {
620         return default_device_handler(sci_dev, __func__);
621 }
622
623 enum sci_status scic_sds_remote_device_default_fail_handler(
624         struct scic_sds_remote_device *sci_dev)
625 {
626         return default_device_handler(sci_dev, __func__);
627 }
628
629 enum sci_status scic_sds_remote_device_default_destruct_handler(
630         struct scic_sds_remote_device *sci_dev)
631 {
632         return default_device_handler(sci_dev, __func__);
633 }
634
635 enum sci_status scic_sds_remote_device_default_reset_handler(
636         struct scic_sds_remote_device *sci_dev)
637 {
638         return default_device_handler(sci_dev, __func__);
639 }
640
641 enum sci_status scic_sds_remote_device_default_reset_complete_handler(
642         struct scic_sds_remote_device *sci_dev)
643 {
644         return default_device_handler(sci_dev, __func__);
645 }
646
647 enum sci_status scic_sds_remote_device_default_suspend_handler(
648         struct scic_sds_remote_device *sci_dev, u32 suspend_type)
649 {
650         return default_device_handler(sci_dev, __func__);
651 }
652
653 enum sci_status scic_sds_remote_device_default_resume_handler(
654         struct scic_sds_remote_device *sci_dev)
655 {
656         return default_device_handler(sci_dev, __func__);
657 }
658
659 /**
660  *
661  * @device: The struct scic_sds_remote_device which is then cast into a
662  *    struct scic_sds_remote_device.
663  * @event_code: The event code that the struct scic_sds_controller wants the device
664  *    object to process.
665  *
666  * This method is the default event handler.  It will call the RNC state
667  * machine handler for any RNC events otherwise it will log a warning and
668  * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
669  */
670 static enum sci_status  scic_sds_remote_device_core_event_handler(
671         struct scic_sds_remote_device *sci_dev,
672         u32 event_code,
673         bool is_ready_state)
674 {
675         enum sci_status status;
676
677         switch (scu_get_event_type(event_code)) {
678         case SCU_EVENT_TYPE_RNC_OPS_MISC:
679         case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
680         case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
681                 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
682                 break;
683         case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
684
685                 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
686                         status = SCI_SUCCESS;
687
688                         /* Suspend the associated RNC */
689                         scic_sds_remote_node_context_suspend(&sci_dev->rnc,
690                                                               SCI_SOFTWARE_SUSPENSION,
691                                                               NULL, NULL);
692
693                         dev_dbg(scirdev_to_dev(sci_dev),
694                                 "%s: device: %p event code: %x: %s\n",
695                                 __func__, sci_dev, event_code,
696                                 (is_ready_state)
697                                 ? "I_T_Nexus_Timeout event"
698                                 : "I_T_Nexus_Timeout event in wrong state");
699
700                         break;
701                 }
702         /* Else, fall through and treat as unhandled... */
703
704         default:
705                 dev_dbg(scirdev_to_dev(sci_dev),
706                         "%s: device: %p event code: %x: %s\n",
707                         __func__, sci_dev, event_code,
708                         (is_ready_state)
709                         ? "unexpected event"
710                         : "unexpected event in wrong state");
711                 status = SCI_FAILURE_INVALID_STATE;
712                 break;
713         }
714
715         return status;
716 }
717 /**
718  *
719  * @device: The struct scic_sds_remote_device which is then cast into a
720  *    struct scic_sds_remote_device.
721  * @event_code: The event code that the struct scic_sds_controller wants the device
722  *    object to process.
723  *
724  * This method is the default event handler.  It will call the RNC state
725  * machine handler for any RNC events otherwise it will log a warning and
726  * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
727  */
728 static enum sci_status  scic_sds_remote_device_default_event_handler(
729         struct scic_sds_remote_device *sci_dev,
730         u32 event_code)
731 {
732         return scic_sds_remote_device_core_event_handler(sci_dev,
733                                                           event_code,
734                                                           false);
735 }
736
737 /**
738  *
739  * @device: The struct scic_sds_remote_device which is then cast into a
740  *    struct scic_sds_remote_device.
741  * @frame_index: The frame index for which the struct scic_sds_controller wants this
742  *    device object to process.
743  *
744  * This method is the default unsolicited frame handler.  It logs a warning,
745  * releases the frame and returns a failure. enum sci_status
746  * SCI_FAILURE_INVALID_STATE
747  */
748 enum sci_status scic_sds_remote_device_default_frame_handler(
749         struct scic_sds_remote_device *sci_dev,
750         u32 frame_index)
751 {
752         dev_warn(scirdev_to_dev(sci_dev),
753                  "%s: SCIC Remote Device requested to handle frame %x "
754                  "while in wrong state %d\n",
755                  __func__,
756                  frame_index,
757                  sci_base_state_machine_get_state(
758                          &sci_dev->state_machine));
759
760         /* Return the frame back to the controller */
761         scic_sds_controller_release_frame(
762                 scic_sds_remote_device_get_controller(sci_dev), frame_index
763                 );
764
765         return SCI_FAILURE_INVALID_STATE;
766 }
767
768 enum sci_status scic_sds_remote_device_default_start_request_handler(
769         struct scic_sds_remote_device *sci_dev,
770         struct scic_sds_request *request)
771 {
772         return default_device_handler(sci_dev, __func__);
773 }
774
775 enum sci_status scic_sds_remote_device_default_complete_request_handler(
776         struct scic_sds_remote_device *sci_dev,
777         struct scic_sds_request *request)
778 {
779         return default_device_handler(sci_dev, __func__);
780 }
781
782 enum sci_status scic_sds_remote_device_default_continue_request_handler(
783         struct scic_sds_remote_device *sci_dev,
784         struct scic_sds_request *request)
785 {
786         return default_device_handler(sci_dev, __func__);
787 }
788
789 /**
790  *
791  * @device: The struct scic_sds_remote_device which is then cast into a
792  *    struct scic_sds_remote_device.
793  * @frame_index: The frame index for which the struct scic_sds_controller wants this
794  *    device object to process.
795  *
796  * This method is a general ssp frame handler.  In most cases the device object
797  * needs to route the unsolicited frame processing to the io request object.
798  * This method decodes the tag for the io request object and routes the
799  * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
800  */
801 enum sci_status scic_sds_remote_device_general_frame_handler(
802         struct scic_sds_remote_device *sci_dev,
803         u32 frame_index)
804 {
805         enum sci_status result;
806         struct sci_ssp_frame_header *frame_header;
807         struct scic_sds_request *io_request;
808
809         result = scic_sds_unsolicited_frame_control_get_header(
810                 &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
811                 frame_index,
812                 (void **)&frame_header
813                 );
814
815         if (SCI_SUCCESS == result) {
816                 io_request = scic_sds_controller_get_io_request_from_tag(
817                         scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
818
819                 if ((io_request == NULL)
820                     || (io_request->target_device != sci_dev)) {
821                         /*
822                          * We could not map this tag to a valid IO request
823                          * Just toss the frame and continue */
824                         scic_sds_controller_release_frame(
825                                 scic_sds_remote_device_get_controller(sci_dev), frame_index
826                                 );
827                 } else {
828                         /* The IO request is now in charge of releasing the frame */
829                         result = io_request->state_handlers->frame_handler(
830                                 io_request, frame_index);
831                 }
832         }
833
834         return result;
835 }
836
837 /**
838  *
839  * @[in]: sci_dev This is the device object that is receiving the event.
840  * @[in]: event_code The event code to process.
841  *
842  * This is a common method for handling events reported to the remote device
843  * from the controller object. enum sci_status
844  */
845 enum sci_status scic_sds_remote_device_general_event_handler(
846         struct scic_sds_remote_device *sci_dev,
847         u32 event_code)
848 {
849         return scic_sds_remote_device_core_event_handler(sci_dev,
850                                                           event_code,
851                                                           true);
852 }
853
854 /*
855  * *****************************************************************************
856  * *  STOPPED STATE HANDLERS
857  * ***************************************************************************** */
858
859 /**
860  *
861  * @device:
862  *
863  * This method takes the struct scic_sds_remote_device from a stopped state and
864  * attempts to start it.   The RNC buffer for the device is constructed and the
865  * device state machine is transitioned to the
866  * SCIC_BASE_REMOTE_DEVICE_STATE_STARTING. enum sci_status SCI_SUCCESS if there is
867  * an RNC buffer available to construct the remote device.
868  * SCI_FAILURE_INSUFFICIENT_RESOURCES if there is no RNC buffer available in
869  * which to construct the remote device.
870  */
871 static enum sci_status scic_sds_remote_device_stopped_state_start_handler(
872         struct scic_sds_remote_device *sci_dev)
873 {
874         enum sci_status status;
875
876         status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
877                         scic_sds_remote_device_resume_complete_handler, sci_dev);
878
879         if (status == SCI_SUCCESS)
880                 sci_base_state_machine_change_state(&sci_dev->state_machine,
881                                                     SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
882
883         return status;
884 }
885
886 static enum sci_status scic_sds_remote_device_stopped_state_stop_handler(
887         struct scic_sds_remote_device *sci_dev)
888 {
889         return SCI_SUCCESS;
890 }
891
892 /**
893  *
894  * @sci_dev: The struct scic_sds_remote_device which is cast into a
895  *    struct scic_sds_remote_device.
896  *
897  * This method will destruct a struct scic_sds_remote_device that is in a stopped
898  * state.  This is the only state from which a destruct request will succeed.
899  * The RNi for this struct scic_sds_remote_device is returned to the free pool and the
900  * device object transitions to the SCI_BASE_REMOTE_DEVICE_STATE_FINAL.
901  * enum sci_status SCI_SUCCESS
902  */
903 static enum sci_status scic_sds_remote_device_stopped_state_destruct_handler(
904         struct scic_sds_remote_device *sci_dev)
905 {
906         struct scic_sds_controller *scic;
907
908         scic = scic_sds_remote_device_get_controller(sci_dev);
909         scic_sds_controller_free_remote_node_context(scic, sci_dev,
910                                                      sci_dev->rnc.remote_node_index);
911         sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
912
913         sci_base_state_machine_change_state(&sci_dev->state_machine,
914                                             SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
915
916         return SCI_SUCCESS;
917 }
918
919 /*
920  * *****************************************************************************
921  * *  STARTING STATE HANDLERS
922  * ***************************************************************************** */
923
924 static enum sci_status scic_sds_remote_device_starting_state_stop_handler(
925         struct scic_sds_remote_device *sci_dev)
926 {
927         /*
928          * This device has not yet started so there had better be no IO requests
929          */
930         BUG_ON(sci_dev->started_request_count != 0);
931
932         /*
933          * Destroy the remote node context
934          */
935         scic_sds_remote_node_context_destruct(&sci_dev->rnc,
936                 scic_sds_cb_remote_device_rnc_destruct_complete, sci_dev);
937
938         /*
939          * Transition to the stopping state and wait for the remote node to
940          * complete being posted and invalidated.
941          */
942         sci_base_state_machine_change_state(&sci_dev->state_machine,
943                                             SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
944
945         return SCI_SUCCESS;
946 }
947
948 enum sci_status scic_sds_remote_device_ready_state_stop_handler(
949         struct scic_sds_remote_device *sci_dev)
950 {
951         enum sci_status status = SCI_SUCCESS;
952
953         /* Request the parent state machine to transition to the stopping state */
954         sci_base_state_machine_change_state(&sci_dev->state_machine,
955                                             SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
956
957         if (sci_dev->started_request_count == 0) {
958                 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
959                         scic_sds_cb_remote_device_rnc_destruct_complete,
960                         sci_dev);
961         } else
962                 status = scic_sds_remote_device_terminate_requests(sci_dev);
963
964         return status;
965 }
966
967 /**
968  *
969  * @device: The struct scic_sds_remote_device object which is cast to a
970  *    struct scic_sds_remote_device object.
971  *
972  * This is the ready state device reset handler enum sci_status
973  */
974 enum sci_status scic_sds_remote_device_ready_state_reset_handler(
975         struct scic_sds_remote_device *sci_dev)
976 {
977         /* Request the parent state machine to transition to the stopping state */
978         sci_base_state_machine_change_state(&sci_dev->state_machine,
979                                             SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
980
981         return SCI_SUCCESS;
982 }
983
984 /*
985  * This method will attempt to start a task request for this device object. The
986  * remote device object will issue the start request for the task and if
987  * successful it will start the request for the port object then increment its
988  * own requet count. enum sci_status SCI_SUCCESS if the task request is started for
989  * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
990  * object could not get the resources to start.
991  */
992 static enum sci_status scic_sds_remote_device_ready_state_start_task_handler(
993         struct scic_sds_remote_device *sci_dev,
994         struct scic_sds_request *request)
995 {
996         enum sci_status result;
997
998         /* See if the port is in a state where we can start the IO request */
999         result = scic_sds_port_start_io(
1000                 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
1001
1002         if (result == SCI_SUCCESS) {
1003                 result = scic_sds_remote_node_context_start_task(&sci_dev->rnc,
1004                                                                  request);
1005                 if (result == SCI_SUCCESS)
1006                         result = scic_sds_request_start(request);
1007
1008                 scic_sds_remote_device_start_request(sci_dev, request, result);
1009         }
1010
1011         return result;
1012 }
1013
1014 /*
1015  * This method will attempt to start an io request for this device object. The
1016  * remote device object will issue the start request for the io and if
1017  * successful it will start the request for the port object then increment its
1018  * own requet count. enum sci_status SCI_SUCCESS if the io request is started for
1019  * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
1020  * object could not get the resources to start.
1021  */
1022 static enum sci_status scic_sds_remote_device_ready_state_start_io_handler(
1023         struct scic_sds_remote_device *sci_dev,
1024         struct scic_sds_request *request)
1025 {
1026         enum sci_status result;
1027
1028         /* See if the port is in a state where we can start the IO request */
1029         result = scic_sds_port_start_io(
1030                 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
1031
1032         if (result == SCI_SUCCESS) {
1033                 result = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
1034                 if (result == SCI_SUCCESS)
1035                         result = scic_sds_request_start(request);
1036
1037                 scic_sds_remote_device_start_request(sci_dev, request, result);
1038         }
1039
1040         return result;
1041 }
1042
1043 /*
1044  * This method will complete the request for the remote device object.  The
1045  * method will call the completion handler for the request object and if
1046  * successful it will complete the request on the port object then decrement
1047  * its own started_request_count. enum sci_status
1048  */
1049 static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
1050         struct scic_sds_remote_device *sci_dev,
1051         struct scic_sds_request *request)
1052 {
1053         enum sci_status result;
1054
1055         result = scic_sds_request_complete(request);
1056
1057         if (result != SCI_SUCCESS)
1058                 return result;
1059
1060         /* See if the port is in a state
1061          * where we can start the IO request */
1062         result = scic_sds_port_complete_io(
1063                         scic_sds_remote_device_get_port(sci_dev),
1064                         sci_dev, request);
1065
1066         if (result == SCI_SUCCESS)
1067                 scic_sds_remote_device_decrement_request_count(sci_dev);
1068
1069         return result;
1070 }
1071
1072 /*
1073  * *****************************************************************************
1074  * *  STOPPING STATE HANDLERS
1075  * ***************************************************************************** */
1076
1077 /**
1078  *
1079  * @sci_dev: The struct scic_sds_remote_device which is cast into a
1080  *    struct scic_sds_remote_device.
1081  *
1082  * This method will stop a struct scic_sds_remote_device that is already in the
1083  * SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This is not considered an error
1084  * since we allow a stop request on a device that is alreay stopping or
1085  * stopped. enum sci_status SCI_SUCCESS
1086  */
1087 static enum sci_status scic_sds_remote_device_stopping_state_stop_handler(
1088         struct scic_sds_remote_device *device)
1089 {
1090         /*
1091          * All requests should have been terminated, but if there is an
1092          * attempt to stop a device already in the stopping state, then
1093          * try again to terminate. */
1094         return scic_sds_remote_device_terminate_requests(device);
1095 }
1096
1097
1098 /**
1099  *
1100  * @device: The device object for which the request is completing.
1101  * @request: The task request that is being completed.
1102  *
1103  * This method completes requests for this struct scic_sds_remote_device while it is
1104  * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
1105  * complete method for the request object and if that is successful the port
1106  * object is called to complete the task request. Then the device object itself
1107  * completes the task request. If struct scic_sds_remote_device started_request_count
1108  * goes to 0 and the invalidate RNC request has completed the device object can
1109  * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
1110  */
1111 static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
1112         struct scic_sds_remote_device *sci_dev,
1113         struct scic_sds_request *request)
1114 {
1115         enum sci_status status = SCI_SUCCESS;
1116
1117         status = scic_sds_request_complete(request);
1118
1119         if (status != SCI_SUCCESS)
1120                 return status;
1121
1122         status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
1123                                            sci_dev, request);
1124         if (status != SCI_SUCCESS)
1125                 return status;
1126
1127         scic_sds_remote_device_decrement_request_count(sci_dev);
1128
1129         if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
1130                 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
1131                                                       scic_sds_cb_remote_device_rnc_destruct_complete,
1132                                                       sci_dev);
1133         return SCI_SUCCESS;
1134 }
1135
1136 /**
1137  *
1138  * @device: The struct scic_sds_remote_device which is to be cast into a
1139  *    struct scic_sds_remote_device object.
1140  *
1141  * This method will complete the reset operation when the device is in the
1142  * resetting state. enum sci_status
1143  */
1144 static enum sci_status scic_sds_remote_device_resetting_state_reset_complete_handler(
1145         struct scic_sds_remote_device *sci_dev)
1146 {
1147
1148         sci_base_state_machine_change_state(
1149                 &sci_dev->state_machine,
1150                 SCI_BASE_REMOTE_DEVICE_STATE_READY
1151                 );
1152
1153         return SCI_SUCCESS;
1154 }
1155
1156 /**
1157  *
1158  * @device: The struct scic_sds_remote_device which is to be cast into a
1159  *    struct scic_sds_remote_device object.
1160  *
1161  * This method will stop the remote device while in the resetting state.
1162  * enum sci_status
1163  */
1164 static enum sci_status scic_sds_remote_device_resetting_state_stop_handler(
1165         struct scic_sds_remote_device *sci_dev)
1166 {
1167         sci_base_state_machine_change_state(
1168                 &sci_dev->state_machine,
1169                 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1170                 );
1171
1172         return SCI_SUCCESS;
1173 }
1174
1175 /*
1176  * This method completes requests for this struct scic_sds_remote_device while it is
1177  * in the SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the
1178  * complete method for the request object and if that is successful the port
1179  * object is called to complete the task request. Then the device object itself
1180  * completes the task request. enum sci_status
1181  */
1182 static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
1183         struct scic_sds_remote_device *sci_dev,
1184         struct scic_sds_request *request)
1185 {
1186         enum sci_status status = SCI_SUCCESS;
1187
1188         status = scic_sds_request_complete(request);
1189
1190         if (status == SCI_SUCCESS) {
1191                 status = scic_sds_port_complete_io(
1192                                 scic_sds_remote_device_get_port(sci_dev),
1193                                 sci_dev, request);
1194
1195                 if (status == SCI_SUCCESS) {
1196                         scic_sds_remote_device_decrement_request_count(sci_dev);
1197                 }
1198         }
1199
1200         return status;
1201 }
1202
1203 /*
1204  * *****************************************************************************
1205  * *  FAILED STATE HANDLERS
1206  * ***************************************************************************** */
1207
1208 /**
1209  *
1210  * @device: The struct scic_sds_remote_device which is to be cast into a
1211  *    struct scic_sds_remote_device object.
1212  *
1213  * This method handles the remove request for a failed struct scic_sds_remote_device
1214  * object. The method will transition the device object to the
1215  * SCIC_BASE_REMOTE_DEVICE_STATE_STOPPING. enum sci_status SCI_SUCCESS
1216  */
1217
1218 /* --------------------------------------------------------------------------- */
1219
1220 static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
1221         [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1222                 .start_handler          = scic_sds_remote_device_default_start_handler,
1223                 .stop_handler           = scic_sds_remote_device_default_stop_handler,
1224                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1225                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1226                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1227                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1228                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1229                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
1230                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1231                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1232                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
1233                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1234                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1235                 .event_handler          = scic_sds_remote_device_default_event_handler,
1236                 .frame_handler          = scic_sds_remote_device_default_frame_handler
1237         },
1238         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1239                 .start_handler          = scic_sds_remote_device_stopped_state_start_handler,
1240                 .stop_handler           = scic_sds_remote_device_stopped_state_stop_handler,
1241                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1242                 .destruct_handler       = scic_sds_remote_device_stopped_state_destruct_handler,
1243                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1244                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1245                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1246                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
1247                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1248                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1249                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
1250                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1251                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1252                 .event_handler          = scic_sds_remote_device_default_event_handler,
1253                 .frame_handler          = scic_sds_remote_device_default_frame_handler
1254         },
1255         [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1256                 .start_handler          = scic_sds_remote_device_default_start_handler,
1257                 .stop_handler           = scic_sds_remote_device_starting_state_stop_handler,
1258                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1259                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1260                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1261                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1262                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1263                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
1264                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1265                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1266                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
1267                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1268                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1269                 .event_handler          = scic_sds_remote_device_general_event_handler,
1270                 .frame_handler          = scic_sds_remote_device_default_frame_handler
1271         },
1272         [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1273                 .start_handler          = scic_sds_remote_device_default_start_handler,
1274                 .stop_handler           = scic_sds_remote_device_ready_state_stop_handler,
1275                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1276                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1277                 .reset_handler          = scic_sds_remote_device_ready_state_reset_handler,
1278                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1279                 .start_io_handler       = scic_sds_remote_device_ready_state_start_io_handler,
1280                 .complete_io_handler    = scic_sds_remote_device_ready_state_complete_request_handler,
1281                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1282                 .start_task_handler     = scic_sds_remote_device_ready_state_start_task_handler,
1283                 .complete_task_handler  = scic_sds_remote_device_ready_state_complete_request_handler,
1284                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1285                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1286                 .event_handler          = scic_sds_remote_device_general_event_handler,
1287                 .frame_handler          = scic_sds_remote_device_general_frame_handler,
1288         },
1289         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1290                 .start_handler          = scic_sds_remote_device_default_start_handler,
1291                 .stop_handler           = scic_sds_remote_device_stopping_state_stop_handler,
1292                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1293                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1294                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1295                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1296                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1297                 .complete_io_handler    = scic_sds_remote_device_stopping_state_complete_request_handler,
1298                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1299                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1300                 .complete_task_handler  = scic_sds_remote_device_stopping_state_complete_request_handler,
1301                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1302                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1303                 .event_handler          = scic_sds_remote_device_general_event_handler,
1304                 .frame_handler          = scic_sds_remote_device_general_frame_handler
1305         },
1306         [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1307                 .start_handler          = scic_sds_remote_device_default_start_handler,
1308                 .stop_handler           = scic_sds_remote_device_default_stop_handler,
1309                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1310                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1311                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1312                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1313                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1314                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
1315                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1316                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1317                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
1318                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1319                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1320                 .event_handler          = scic_sds_remote_device_default_event_handler,
1321                 .frame_handler          = scic_sds_remote_device_general_frame_handler
1322         },
1323         [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1324                 .start_handler          = scic_sds_remote_device_default_start_handler,
1325                 .stop_handler           = scic_sds_remote_device_resetting_state_stop_handler,
1326                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1327                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1328                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1329                 .reset_complete_handler = scic_sds_remote_device_resetting_state_reset_complete_handler,
1330                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1331                 .complete_io_handler    = scic_sds_remote_device_resetting_state_complete_request_handler,
1332                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1333                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1334                 .complete_task_handler  = scic_sds_remote_device_resetting_state_complete_request_handler,
1335                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1336                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1337                 .event_handler          = scic_sds_remote_device_default_event_handler,
1338                 .frame_handler          = scic_sds_remote_device_general_frame_handler
1339         },
1340         [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1341                 .start_handler          = scic_sds_remote_device_default_start_handler,
1342                 .stop_handler           = scic_sds_remote_device_default_stop_handler,
1343                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1344                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1345                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1346                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1347                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1348                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
1349                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1350                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1351                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
1352                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1353                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1354                 .event_handler          = scic_sds_remote_device_default_event_handler,
1355                 .frame_handler          = scic_sds_remote_device_default_frame_handler
1356         }
1357 };
1358
1359 /**
1360  *
1361  * @object: This is the struct sci_base_object that is cast into a
1362  *    struct scic_sds_remote_device.
1363  *
1364  * This is the enter method for the SCI_BASE_REMOTE_DEVICE_STATE_INITIAL it
1365  * immediatly transitions the remote device object to the stopped state. none
1366  */
1367 static void scic_sds_remote_device_initial_state_enter(
1368         struct sci_base_object *object)
1369 {
1370         struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1371
1372         sci_dev = container_of(object, typeof(*sci_dev), parent);
1373         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1374                           SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1375
1376         /* Initial state is a transitional state to the stopped state */
1377         sci_base_state_machine_change_state(&sci_dev->state_machine,
1378                                             SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1379 }
1380
1381 /**
1382  *
1383  * @object: This is the struct sci_base_object that is cast into a
1384  *    struct scic_sds_remote_device.
1385  *
1386  * This is the enter function for the SCI_BASE_REMOTE_DEVICE_STATE_INITIAL it
1387  * sets the stopped state handlers and if this state is entered from the
1388  * SCI_BASE_REMOTE_DEVICE_STATE_STOPPING then the SCI User is informed that the
1389  * device stop is complete. none
1390  */
1391 static void scic_sds_remote_device_stopped_state_enter(
1392         struct sci_base_object *object)
1393 {
1394         struct scic_sds_remote_device *sci_dev;
1395         struct scic_sds_controller *scic;
1396         struct isci_remote_device *idev;
1397         struct isci_host *ihost;
1398         u32 prev_state;
1399
1400         sci_dev = container_of(object, typeof(*sci_dev), parent);
1401         scic = scic_sds_remote_device_get_controller(sci_dev);
1402         ihost = sci_object_get_association(scic);
1403         idev = sci_object_get_association(sci_dev);
1404
1405         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1406                           SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1407
1408         /* If we are entering from the stopping state let the SCI User know that
1409          * the stop operation has completed.
1410          */
1411         prev_state = sci_dev->state_machine.previous_state_id;
1412         if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1413                 isci_remote_device_stop_complete(ihost, idev, SCI_SUCCESS);
1414
1415         scic_sds_controller_remote_device_stopped(scic, sci_dev);
1416 }
1417
1418 /**
1419  *
1420  * @object: This is the struct sci_base_object that is cast into a
1421  *    struct scic_sds_remote_device.
1422  *
1423  * This is the enter function for the SCI_BASE_REMOTE_DEVICE_STATE_STARTING it
1424  * sets the starting state handlers, sets the device not ready, and posts the
1425  * remote node context to the hardware. none
1426  */
1427 static void scic_sds_remote_device_starting_state_enter(struct sci_base_object *object)
1428 {
1429         struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1430                                                               parent);
1431         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1432         struct isci_host *ihost = sci_object_get_association(scic);
1433         struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1434
1435         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1436                           SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1437
1438         isci_remote_device_not_ready(ihost, idev,
1439                                      SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1440 }
1441
1442 static void scic_sds_remote_device_starting_state_exit(struct sci_base_object *object)
1443 {
1444         struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1445                                                               parent);
1446         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1447         struct isci_host *ihost = sci_object_get_association(scic);
1448         struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1449
1450         /*
1451          * @todo Check the device object for the proper return code for this
1452          * callback
1453          */
1454         isci_remote_device_start_complete(ihost, idev, SCI_SUCCESS);
1455 }
1456
1457 /**
1458  *
1459  * @object: This is the struct sci_base_object that is cast into a
1460  *    struct scic_sds_remote_device.
1461  *
1462  * This is the enter function for the SCI_BASE_REMOTE_DEVICE_STATE_READY it sets
1463  * the ready state handlers, and starts the ready substate machine. none
1464  */
1465 static void scic_sds_remote_device_ready_state_enter(struct sci_base_object *object)
1466 {
1467         struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1468                                                               parent);
1469         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1470         struct isci_host *ihost = sci_object_get_association(scic);
1471         struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1472
1473         SET_STATE_HANDLER(sci_dev,
1474                           scic_sds_remote_device_state_handler_table,
1475                           SCI_BASE_REMOTE_DEVICE_STATE_READY);
1476
1477         scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1478
1479         if (sci_dev->has_ready_substate_machine)
1480                 sci_base_state_machine_start(&sci_dev->ready_substate_machine);
1481         else
1482                 isci_remote_device_ready(ihost, idev);
1483 }
1484
1485 /**
1486  *
1487  * @object: This is the struct sci_base_object that is cast into a
1488  *    struct scic_sds_remote_device.
1489  *
1490  * This is the exit function for the SCI_BASE_REMOTE_DEVICE_STATE_READY it does
1491  * nothing. none
1492  */
1493 static void scic_sds_remote_device_ready_state_exit(
1494         struct sci_base_object *object)
1495 {
1496         struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1497                                                               parent);
1498         if (sci_dev->has_ready_substate_machine)
1499                 sci_base_state_machine_stop(&sci_dev->ready_substate_machine);
1500         else {
1501                 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1502                 struct isci_host *ihost = sci_object_get_association(scic);
1503                 struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1504
1505                 isci_remote_device_not_ready(ihost, idev,
1506                                              SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1507         }
1508 }
1509
1510 /**
1511  *
1512  * @object: This is the struct sci_base_object that is cast into a
1513  *    struct scic_sds_remote_device.
1514  *
1515  * This is the enter method for the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING it
1516  * sets the stopping state handlers and posts an RNC invalidate request to the
1517  * SCU hardware. none
1518  */
1519 static void scic_sds_remote_device_stopping_state_enter(
1520         struct sci_base_object *object)
1521 {
1522         struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1523
1524         SET_STATE_HANDLER(
1525                 sci_dev,
1526                 scic_sds_remote_device_state_handler_table,
1527                 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1528                 );
1529 }
1530
1531 /**
1532  *
1533  * @object: This is the struct sci_base_object that is cast into a
1534  *    struct scic_sds_remote_device.
1535  *
1536  * This is the enter method for the SCI_BASE_REMOTE_DEVICE_STATE_FAILED it sets
1537  * the stopping state handlers. none
1538  */
1539 static void scic_sds_remote_device_failed_state_enter(
1540         struct sci_base_object *object)
1541 {
1542         struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1543
1544         SET_STATE_HANDLER(
1545                 sci_dev,
1546                 scic_sds_remote_device_state_handler_table,
1547                 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1548                 );
1549 }
1550
1551 /**
1552  *
1553  * @object: This is the struct sci_base_object that is cast into a
1554  *    struct scic_sds_remote_device.
1555  *
1556  * This is the enter method for the SCI_BASE_REMOTE_DEVICE_STATE_RESETTING it
1557  * sets the resetting state handlers. none
1558  */
1559 static void scic_sds_remote_device_resetting_state_enter(
1560         struct sci_base_object *object)
1561 {
1562         struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1563
1564         SET_STATE_HANDLER(
1565                 sci_dev,
1566                 scic_sds_remote_device_state_handler_table,
1567                 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1568                 );
1569
1570         scic_sds_remote_node_context_suspend(
1571                 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1572 }
1573
1574 /**
1575  *
1576  * @object: This is the struct sci_base_object that is cast into a
1577  *    struct scic_sds_remote_device.
1578  *
1579  * This is the exit method for the SCI_BASE_REMOTE_DEVICE_STATE_RESETTING it
1580  * does nothing. none
1581  */
1582 static void scic_sds_remote_device_resetting_state_exit(
1583         struct sci_base_object *object)
1584 {
1585         struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1586
1587         scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1588 }
1589
1590 /**
1591  *
1592  * @object: This is the struct sci_base_object that is cast into a
1593  *    struct scic_sds_remote_device.
1594  *
1595  * This is the enter method for the SCI_BASE_REMOTE_DEVICE_STATE_FINAL it sets
1596  * the final state handlers. none
1597  */
1598 static void scic_sds_remote_device_final_state_enter(
1599         struct sci_base_object *object)
1600 {
1601         struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1602
1603         SET_STATE_HANDLER(
1604                 sci_dev,
1605                 scic_sds_remote_device_state_handler_table,
1606                 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1607                 );
1608 }
1609
1610 /* --------------------------------------------------------------------------- */
1611
1612 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1613         [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1614                 .enter_state = scic_sds_remote_device_initial_state_enter,
1615         },
1616         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1617                 .enter_state = scic_sds_remote_device_stopped_state_enter,
1618         },
1619         [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1620                 .enter_state = scic_sds_remote_device_starting_state_enter,
1621                 .exit_state  = scic_sds_remote_device_starting_state_exit
1622         },
1623         [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1624                 .enter_state = scic_sds_remote_device_ready_state_enter,
1625                 .exit_state  = scic_sds_remote_device_ready_state_exit
1626         },
1627         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1628                 .enter_state = scic_sds_remote_device_stopping_state_enter,
1629         },
1630         [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1631                 .enter_state = scic_sds_remote_device_failed_state_enter,
1632         },
1633         [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1634                 .enter_state = scic_sds_remote_device_resetting_state_enter,
1635                 .exit_state  = scic_sds_remote_device_resetting_state_exit
1636         },
1637         [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1638                 .enter_state = scic_sds_remote_device_final_state_enter,
1639         },
1640 };
1641
1642 void scic_remote_device_construct(struct scic_sds_port *sci_port,
1643                                   struct scic_sds_remote_device *sci_dev)
1644 {
1645         sci_dev->owning_port = sci_port;
1646         sci_dev->started_request_count = 0;
1647         sci_dev->parent.private = NULL;
1648
1649         sci_base_state_machine_construct(
1650                 &sci_dev->state_machine,
1651                 &sci_dev->parent,
1652                 scic_sds_remote_device_state_table,
1653                 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1654                 );
1655
1656         sci_base_state_machine_start(
1657                 &sci_dev->state_machine
1658                 );
1659
1660         scic_sds_remote_node_context_construct(&sci_dev->rnc,
1661                                                SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1662
1663         sci_object_set_association(&sci_dev->rnc, sci_dev);
1664 }