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