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