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