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