blob: 3fc66b2a5992ac1ed15563b5ac4aed38e1dc04b6 [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
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 */
Dan Williams88f3b622011-04-22 19:18:03 -070055#include "intel_sas.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070056#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "port.h"
58#include "remote_device.h"
59#include "request.h"
Dan Williams88f3b622011-04-22 19:18:03 -070060#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"
Dan Williams6f231dd2011-07-02 22:56:22 -070072#include "task.h"
73
Dan Williams88f3b622011-04-22 19:18:03 -070074enum 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}
Dan Williams6f231dd2011-07-02 22:56:22 -070080
81
Dan Williams88f3b622011-04-22 19:18:03 -070082enum 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
89enum 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
96enum 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
Dan Williams88f3b622011-04-22 19:18:03 -0700102/**
103 *
104 *
105 * Remote device timer requirements
106 */
107#define SCIC_SDS_REMOTE_DEVICE_MINIMUM_TIMER_COUNT (0)
108#define SCIC_SDS_REMOTE_DEVICE_MAXIMUM_TIMER_COUNT (SCI_MAX_REMOTE_DEVICES)
109
110
111/**
112 *
113 * @sci_dev: The remote device for which the suspend is being requested.
114 *
115 * This method invokes the remote device suspend state handler. enum sci_status
116 */
117enum sci_status scic_sds_remote_device_suspend(
118 struct scic_sds_remote_device *sci_dev,
119 u32 suspend_type)
120{
121 return sci_dev->state_handlers->suspend_handler(sci_dev, suspend_type);
122}
123
124/**
125 *
126 * @sci_dev: The remote device for which the event handling is being
127 * requested.
128 * @frame_index: This is the frame index that is being processed.
129 *
130 * This method invokes the frame handler for the remote device state machine
131 * enum sci_status
132 */
133enum sci_status scic_sds_remote_device_frame_handler(
134 struct scic_sds_remote_device *sci_dev,
135 u32 frame_index)
136{
137 return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
138}
139
140/**
141 *
142 * @sci_dev: The remote device for which the event handling is being
143 * requested.
144 * @event_code: This is the event code that is to be processed.
145 *
146 * This method invokes the remote device event handler. enum sci_status
147 */
148enum sci_status scic_sds_remote_device_event_handler(
149 struct scic_sds_remote_device *sci_dev,
150 u32 event_code)
151{
152 return sci_dev->state_handlers->event_handler(sci_dev, event_code);
153}
154
155/**
156 *
157 * @controller: The controller that is starting the io request.
158 * @sci_dev: The remote device for which the start io handling is being
159 * requested.
160 * @io_request: The io request that is being started.
161 *
162 * This method invokes the remote device start io handler. enum sci_status
163 */
164enum sci_status scic_sds_remote_device_start_io(
165 struct scic_sds_controller *controller,
166 struct scic_sds_remote_device *sci_dev,
167 struct scic_sds_request *io_request)
168{
169 return sci_dev->state_handlers->start_io_handler(
170 sci_dev, io_request);
171}
172
173/**
174 *
175 * @controller: The controller that is completing the io request.
176 * @sci_dev: The remote device for which the complete io handling is being
177 * requested.
178 * @io_request: The io request that is being completed.
179 *
180 * This method invokes the remote device complete io handler. enum sci_status
181 */
182enum sci_status scic_sds_remote_device_complete_io(
183 struct scic_sds_controller *controller,
184 struct scic_sds_remote_device *sci_dev,
185 struct scic_sds_request *io_request)
186{
187 return sci_dev->state_handlers->complete_io_handler(
188 sci_dev, io_request);
189}
190
191/**
192 *
193 * @controller: The controller that is starting the task request.
194 * @sci_dev: The remote device for which the start task handling is being
195 * requested.
196 * @io_request: The task request that is being started.
197 *
198 * This method invokes the remote device start task handler. enum sci_status
199 */
200enum sci_status scic_sds_remote_device_start_task(
201 struct scic_sds_controller *controller,
202 struct scic_sds_remote_device *sci_dev,
203 struct scic_sds_request *io_request)
204{
205 return sci_dev->state_handlers->start_task_handler(
206 sci_dev, io_request);
207}
208
209/**
210 *
Dan Williams88f3b622011-04-22 19:18:03 -0700211 * @sci_dev:
212 * @request:
213 *
214 * This method takes the request and bulids an appropriate SCU context for the
215 * request and then requests the controller to post the request. none
216 */
217void scic_sds_remote_device_post_request(
218 struct scic_sds_remote_device *sci_dev,
219 u32 request)
220{
221 u32 context;
222
223 context = scic_sds_remote_device_build_command_context(sci_dev, request);
224
225 scic_sds_controller_post_request(
226 scic_sds_remote_device_get_controller(sci_dev),
227 context
228 );
229}
230
Dan Williams88f3b622011-04-22 19:18:03 -0700231/**
232 *
233 * @user_parameter: This is cast to a remote device object.
234 *
235 * This method is called once the remote node context is ready to be freed.
236 * The remote device can now report that its stop operation is complete. none
237 */
238static void scic_sds_cb_remote_device_rnc_destruct_complete(
239 void *user_parameter)
240{
241 struct scic_sds_remote_device *sci_dev;
242
243 sci_dev = (struct scic_sds_remote_device *)user_parameter;
244
245 BUG_ON(sci_dev->started_request_count != 0);
246
247 sci_base_state_machine_change_state(&sci_dev->state_machine,
248 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
249}
250
251/**
252 *
253 * @user_parameter: This is cast to a remote device object.
254 *
255 * This method is called once the remote node context has transisitioned to a
256 * ready state. This is the indication that the remote device object can also
257 * transition to ready. none
258 */
259static void scic_sds_remote_device_resume_complete_handler(
260 void *user_parameter)
261{
262 struct scic_sds_remote_device *sci_dev;
263
264 sci_dev = (struct scic_sds_remote_device *)user_parameter;
265
266 if (
267 sci_base_state_machine_get_state(&sci_dev->state_machine)
268 != SCI_BASE_REMOTE_DEVICE_STATE_READY
269 ) {
270 sci_base_state_machine_change_state(
271 &sci_dev->state_machine,
272 SCI_BASE_REMOTE_DEVICE_STATE_READY
273 );
274 }
275}
276
277/**
278 *
279 * @device: This parameter specifies the device for which the request is being
280 * started.
281 * @request: This parameter specifies the request being started.
282 * @status: This parameter specifies the current start operation status.
283 *
284 * This method will perform the STP request start processing common to IO
285 * requests and task requests of all types. none
286 */
287void scic_sds_remote_device_start_request(
288 struct scic_sds_remote_device *sci_dev,
289 struct scic_sds_request *sci_req,
290 enum sci_status status)
291{
292 /* We still have a fault in starting the io complete it on the port */
293 if (status == SCI_SUCCESS)
294 scic_sds_remote_device_increment_request_count(sci_dev);
295 else{
296 sci_dev->owning_port->state_handlers->complete_io_handler(
297 sci_dev->owning_port, sci_dev, sci_req
298 );
299 }
300}
301
302
303/**
304 *
305 * @request: This parameter specifies the request being continued.
306 *
307 * This method will continue to post tc for a STP request. This method usually
308 * serves as a callback when RNC gets resumed during a task management
309 * sequence. none
310 */
311void scic_sds_remote_device_continue_request(void *dev)
312{
313 struct scic_sds_remote_device *sci_dev = dev;
314
315 /* we need to check if this request is still valid to continue. */
316 if (sci_dev->working_request)
317 scic_controller_continue_io(sci_dev->working_request);
318}
319
320/**
321 * This method will terminate all of the IO requests in the controllers IO
322 * request table that were targeted for this device.
323 * @sci_dev: This parameter specifies the remote device for which to
324 * attempt to terminate all requests.
325 *
326 * This method returns an indication as to whether all requests were
327 * successfully terminated. If a single request fails to be terminated, then
328 * this method will return the failure.
329 */
330static enum sci_status scic_sds_remote_device_terminate_requests(
331 struct scic_sds_remote_device *sci_dev)
332{
333 enum sci_status status = SCI_SUCCESS;
334 enum sci_status terminate_status = SCI_SUCCESS;
335 struct scic_sds_request *sci_req;
336 u32 index;
337 u32 request_count = sci_dev->started_request_count;
338
339 for (index = 0;
340 (index < SCI_MAX_IO_REQUESTS) && (request_count > 0);
341 index++) {
342 sci_req = sci_dev->owning_port->owning_controller->io_request_table[index];
343
344 if ((sci_req != NULL) && (sci_req->target_device == sci_dev)) {
345 terminate_status = scic_controller_terminate_request(
346 sci_dev->owning_port->owning_controller,
347 sci_dev,
348 sci_req
349 );
350
351 if (terminate_status != SCI_SUCCESS)
352 status = terminate_status;
353
354 request_count--;
355 }
356 }
357
358 return status;
359}
360
361static enum sci_status
362default_device_handler(struct scic_sds_remote_device *sci_dev,
363 const char *func)
364{
365 dev_warn(scirdev_to_dev(sci_dev),
366 "%s: in wrong state: %d\n", func,
367 sci_base_state_machine_get_state(&sci_dev->state_machine));
368 return SCI_FAILURE_INVALID_STATE;
369}
370
371enum sci_status scic_sds_remote_device_default_start_handler(
372 struct scic_sds_remote_device *sci_dev)
373{
374 return default_device_handler(sci_dev, __func__);
375}
376
377static enum sci_status scic_sds_remote_device_default_stop_handler(
378 struct scic_sds_remote_device *sci_dev)
379{
380 return default_device_handler(sci_dev, __func__);
381}
382
383enum sci_status scic_sds_remote_device_default_fail_handler(
384 struct scic_sds_remote_device *sci_dev)
385{
386 return default_device_handler(sci_dev, __func__);
387}
388
389enum sci_status scic_sds_remote_device_default_destruct_handler(
390 struct scic_sds_remote_device *sci_dev)
391{
392 return default_device_handler(sci_dev, __func__);
393}
394
395enum sci_status scic_sds_remote_device_default_reset_handler(
396 struct scic_sds_remote_device *sci_dev)
397{
398 return default_device_handler(sci_dev, __func__);
399}
400
401enum sci_status scic_sds_remote_device_default_reset_complete_handler(
402 struct scic_sds_remote_device *sci_dev)
403{
404 return default_device_handler(sci_dev, __func__);
405}
406
407enum sci_status scic_sds_remote_device_default_suspend_handler(
408 struct scic_sds_remote_device *sci_dev, u32 suspend_type)
409{
410 return default_device_handler(sci_dev, __func__);
411}
412
413enum sci_status scic_sds_remote_device_default_resume_handler(
414 struct scic_sds_remote_device *sci_dev)
415{
416 return default_device_handler(sci_dev, __func__);
417}
418
419/**
420 *
421 * @device: The struct scic_sds_remote_device which is then cast into a
422 * struct scic_sds_remote_device.
423 * @event_code: The event code that the struct scic_sds_controller wants the device
424 * object to process.
425 *
426 * This method is the default event handler. It will call the RNC state
427 * machine handler for any RNC events otherwise it will log a warning and
428 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
429 */
430static enum sci_status scic_sds_remote_device_core_event_handler(
431 struct scic_sds_remote_device *sci_dev,
432 u32 event_code,
433 bool is_ready_state)
434{
435 enum sci_status status;
436
437 switch (scu_get_event_type(event_code)) {
438 case SCU_EVENT_TYPE_RNC_OPS_MISC:
439 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
440 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
441 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
442 break;
443 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
444
445 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
446 status = SCI_SUCCESS;
447
448 /* Suspend the associated RNC */
449 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
450 SCI_SOFTWARE_SUSPENSION,
451 NULL, NULL);
452
453 dev_dbg(scirdev_to_dev(sci_dev),
454 "%s: device: %p event code: %x: %s\n",
455 __func__, sci_dev, event_code,
456 (is_ready_state)
457 ? "I_T_Nexus_Timeout event"
458 : "I_T_Nexus_Timeout event in wrong state");
459
460 break;
461 }
462 /* Else, fall through and treat as unhandled... */
463
464 default:
465 dev_dbg(scirdev_to_dev(sci_dev),
466 "%s: device: %p event code: %x: %s\n",
467 __func__, sci_dev, event_code,
468 (is_ready_state)
469 ? "unexpected event"
470 : "unexpected event in wrong state");
471 status = SCI_FAILURE_INVALID_STATE;
472 break;
473 }
474
475 return status;
476}
477/**
478 *
479 * @device: The struct scic_sds_remote_device which is then cast into a
480 * struct scic_sds_remote_device.
481 * @event_code: The event code that the struct scic_sds_controller wants the device
482 * object to process.
483 *
484 * This method is the default event handler. It will call the RNC state
485 * machine handler for any RNC events otherwise it will log a warning and
486 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
487 */
488static enum sci_status scic_sds_remote_device_default_event_handler(
489 struct scic_sds_remote_device *sci_dev,
490 u32 event_code)
491{
492 return scic_sds_remote_device_core_event_handler(sci_dev,
493 event_code,
494 false);
495}
496
497/**
498 *
499 * @device: The struct scic_sds_remote_device which is then cast into a
500 * struct scic_sds_remote_device.
501 * @frame_index: The frame index for which the struct scic_sds_controller wants this
502 * device object to process.
503 *
504 * This method is the default unsolicited frame handler. It logs a warning,
505 * releases the frame and returns a failure. enum sci_status
506 * SCI_FAILURE_INVALID_STATE
507 */
508enum sci_status scic_sds_remote_device_default_frame_handler(
509 struct scic_sds_remote_device *sci_dev,
510 u32 frame_index)
511{
512 dev_warn(scirdev_to_dev(sci_dev),
513 "%s: SCIC Remote Device requested to handle frame %x "
514 "while in wrong state %d\n",
515 __func__,
516 frame_index,
517 sci_base_state_machine_get_state(
518 &sci_dev->state_machine));
519
520 /* Return the frame back to the controller */
521 scic_sds_controller_release_frame(
522 scic_sds_remote_device_get_controller(sci_dev), frame_index
523 );
524
525 return SCI_FAILURE_INVALID_STATE;
526}
527
528enum sci_status scic_sds_remote_device_default_start_request_handler(
529 struct scic_sds_remote_device *sci_dev,
530 struct scic_sds_request *request)
531{
532 return default_device_handler(sci_dev, __func__);
533}
534
535enum sci_status scic_sds_remote_device_default_complete_request_handler(
536 struct scic_sds_remote_device *sci_dev,
537 struct scic_sds_request *request)
538{
539 return default_device_handler(sci_dev, __func__);
540}
541
542enum sci_status scic_sds_remote_device_default_continue_request_handler(
543 struct scic_sds_remote_device *sci_dev,
544 struct scic_sds_request *request)
545{
546 return default_device_handler(sci_dev, __func__);
547}
548
549/**
550 *
551 * @device: The struct scic_sds_remote_device which is then cast into a
552 * struct scic_sds_remote_device.
553 * @frame_index: The frame index for which the struct scic_sds_controller wants this
554 * device object to process.
555 *
556 * This method is a general ssp frame handler. In most cases the device object
557 * needs to route the unsolicited frame processing to the io request object.
558 * This method decodes the tag for the io request object and routes the
559 * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
560 */
561enum sci_status scic_sds_remote_device_general_frame_handler(
562 struct scic_sds_remote_device *sci_dev,
563 u32 frame_index)
564{
565 enum sci_status result;
566 struct sci_ssp_frame_header *frame_header;
567 struct scic_sds_request *io_request;
568
569 result = scic_sds_unsolicited_frame_control_get_header(
570 &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
571 frame_index,
572 (void **)&frame_header
573 );
574
575 if (SCI_SUCCESS == result) {
576 io_request = scic_sds_controller_get_io_request_from_tag(
577 scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
578
579 if ((io_request == NULL)
580 || (io_request->target_device != sci_dev)) {
581 /*
582 * We could not map this tag to a valid IO request
583 * Just toss the frame and continue */
584 scic_sds_controller_release_frame(
585 scic_sds_remote_device_get_controller(sci_dev), frame_index
586 );
587 } else {
588 /* The IO request is now in charge of releasing the frame */
589 result = io_request->state_handlers->frame_handler(
590 io_request, frame_index);
591 }
592 }
593
594 return result;
595}
596
597/**
598 *
599 * @[in]: sci_dev This is the device object that is receiving the event.
600 * @[in]: event_code The event code to process.
601 *
602 * This is a common method for handling events reported to the remote device
603 * from the controller object. enum sci_status
604 */
605enum sci_status scic_sds_remote_device_general_event_handler(
606 struct scic_sds_remote_device *sci_dev,
607 u32 event_code)
608{
609 return scic_sds_remote_device_core_event_handler(sci_dev,
610 event_code,
611 true);
612}
613
614/*
615 * *****************************************************************************
616 * * STOPPED STATE HANDLERS
617 * ***************************************************************************** */
618
619/**
620 *
621 * @device:
622 *
623 * This method takes the struct scic_sds_remote_device from a stopped state and
624 * attempts to start it. The RNC buffer for the device is constructed and the
625 * device state machine is transitioned to the
626 * SCIC_BASE_REMOTE_DEVICE_STATE_STARTING. enum sci_status SCI_SUCCESS if there is
627 * an RNC buffer available to construct the remote device.
628 * SCI_FAILURE_INSUFFICIENT_RESOURCES if there is no RNC buffer available in
629 * which to construct the remote device.
630 */
631static enum sci_status scic_sds_remote_device_stopped_state_start_handler(
632 struct scic_sds_remote_device *sci_dev)
633{
634 enum sci_status status;
635
636 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
637 scic_sds_remote_device_resume_complete_handler, sci_dev);
638
639 if (status == SCI_SUCCESS)
640 sci_base_state_machine_change_state(&sci_dev->state_machine,
641 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
642
643 return status;
644}
645
646static enum sci_status scic_sds_remote_device_stopped_state_stop_handler(
647 struct scic_sds_remote_device *sci_dev)
648{
649 return SCI_SUCCESS;
650}
651
652/**
653 *
654 * @sci_dev: The struct scic_sds_remote_device which is cast into a
655 * struct scic_sds_remote_device.
656 *
657 * This method will destruct a struct scic_sds_remote_device that is in a stopped
658 * state. This is the only state from which a destruct request will succeed.
659 * The RNi for this struct scic_sds_remote_device is returned to the free pool and the
660 * device object transitions to the SCI_BASE_REMOTE_DEVICE_STATE_FINAL.
661 * enum sci_status SCI_SUCCESS
662 */
663static enum sci_status scic_sds_remote_device_stopped_state_destruct_handler(
664 struct scic_sds_remote_device *sci_dev)
665{
666 struct scic_sds_controller *scic;
667
668 scic = scic_sds_remote_device_get_controller(sci_dev);
669 scic_sds_controller_free_remote_node_context(scic, sci_dev,
670 sci_dev->rnc.remote_node_index);
671 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
672
673 sci_base_state_machine_change_state(&sci_dev->state_machine,
674 SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
675
676 return SCI_SUCCESS;
677}
678
679/*
680 * *****************************************************************************
681 * * STARTING STATE HANDLERS
682 * ***************************************************************************** */
683
684static enum sci_status scic_sds_remote_device_starting_state_stop_handler(
685 struct scic_sds_remote_device *sci_dev)
686{
687 /*
688 * This device has not yet started so there had better be no IO requests
689 */
690 BUG_ON(sci_dev->started_request_count != 0);
691
692 /*
693 * Destroy the remote node context
694 */
695 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
696 scic_sds_cb_remote_device_rnc_destruct_complete, sci_dev);
697
698 /*
699 * Transition to the stopping state and wait for the remote node to
700 * complete being posted and invalidated.
701 */
702 sci_base_state_machine_change_state(&sci_dev->state_machine,
703 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
704
705 return SCI_SUCCESS;
706}
707
708enum sci_status scic_sds_remote_device_ready_state_stop_handler(
709 struct scic_sds_remote_device *sci_dev)
710{
711 enum sci_status status = SCI_SUCCESS;
712
713 /* Request the parent state machine to transition to the stopping state */
714 sci_base_state_machine_change_state(&sci_dev->state_machine,
715 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
716
717 if (sci_dev->started_request_count == 0) {
718 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
719 scic_sds_cb_remote_device_rnc_destruct_complete,
720 sci_dev);
721 } else
722 status = scic_sds_remote_device_terminate_requests(sci_dev);
723
724 return status;
725}
726
727/**
728 *
729 * @device: The struct scic_sds_remote_device object which is cast to a
730 * struct scic_sds_remote_device object.
731 *
732 * This is the ready state device reset handler enum sci_status
733 */
734enum sci_status scic_sds_remote_device_ready_state_reset_handler(
735 struct scic_sds_remote_device *sci_dev)
736{
737 /* Request the parent state machine to transition to the stopping state */
738 sci_base_state_machine_change_state(&sci_dev->state_machine,
739 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
740
741 return SCI_SUCCESS;
742}
743
744/*
745 * This method will attempt to start a task request for this device object. The
746 * remote device object will issue the start request for the task and if
747 * successful it will start the request for the port object then increment its
748 * own requet count. enum sci_status SCI_SUCCESS if the task request is started for
749 * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
750 * object could not get the resources to start.
751 */
752static enum sci_status scic_sds_remote_device_ready_state_start_task_handler(
753 struct scic_sds_remote_device *sci_dev,
754 struct scic_sds_request *request)
755{
756 enum sci_status result;
757
758 /* See if the port is in a state where we can start the IO request */
759 result = scic_sds_port_start_io(
760 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
761
762 if (result == SCI_SUCCESS) {
763 result = scic_sds_remote_node_context_start_task(&sci_dev->rnc,
764 request);
765 if (result == SCI_SUCCESS)
766 result = scic_sds_request_start(request);
767
768 scic_sds_remote_device_start_request(sci_dev, request, result);
769 }
770
771 return result;
772}
773
774/*
775 * This method will attempt to start an io request for this device object. The
776 * remote device object will issue the start request for the io and if
777 * successful it will start the request for the port object then increment its
778 * own requet count. enum sci_status SCI_SUCCESS if the io request is started for
779 * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
780 * object could not get the resources to start.
781 */
782static enum sci_status scic_sds_remote_device_ready_state_start_io_handler(
783 struct scic_sds_remote_device *sci_dev,
784 struct scic_sds_request *request)
785{
786 enum sci_status result;
787
788 /* See if the port is in a state where we can start the IO request */
789 result = scic_sds_port_start_io(
790 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
791
792 if (result == SCI_SUCCESS) {
793 result = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
794 if (result == SCI_SUCCESS)
795 result = scic_sds_request_start(request);
796
797 scic_sds_remote_device_start_request(sci_dev, request, result);
798 }
799
800 return result;
801}
802
803/*
804 * This method will complete the request for the remote device object. The
805 * method will call the completion handler for the request object and if
806 * successful it will complete the request on the port object then decrement
807 * its own started_request_count. enum sci_status
808 */
809static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
810 struct scic_sds_remote_device *sci_dev,
811 struct scic_sds_request *request)
812{
813 enum sci_status result;
814
815 result = scic_sds_request_complete(request);
816
817 if (result != SCI_SUCCESS)
818 return result;
819
820 /* See if the port is in a state
821 * where we can start the IO request */
822 result = scic_sds_port_complete_io(
823 scic_sds_remote_device_get_port(sci_dev),
824 sci_dev, request);
825
826 if (result == SCI_SUCCESS)
827 scic_sds_remote_device_decrement_request_count(sci_dev);
828
829 return result;
830}
831
832/*
833 * *****************************************************************************
834 * * STOPPING STATE HANDLERS
835 * ***************************************************************************** */
836
837/**
838 *
839 * @sci_dev: The struct scic_sds_remote_device which is cast into a
840 * struct scic_sds_remote_device.
841 *
842 * This method will stop a struct scic_sds_remote_device that is already in the
843 * SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This is not considered an error
844 * since we allow a stop request on a device that is alreay stopping or
845 * stopped. enum sci_status SCI_SUCCESS
846 */
847static enum sci_status scic_sds_remote_device_stopping_state_stop_handler(
848 struct scic_sds_remote_device *device)
849{
850 /*
851 * All requests should have been terminated, but if there is an
852 * attempt to stop a device already in the stopping state, then
853 * try again to terminate. */
854 return scic_sds_remote_device_terminate_requests(device);
855}
856
857
858/**
859 *
860 * @device: The device object for which the request is completing.
861 * @request: The task request that is being completed.
862 *
863 * This method completes requests for this struct scic_sds_remote_device while it is
864 * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
865 * complete method for the request object and if that is successful the port
866 * object is called to complete the task request. Then the device object itself
867 * completes the task request. If struct scic_sds_remote_device started_request_count
868 * goes to 0 and the invalidate RNC request has completed the device object can
869 * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
870 */
871static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
872 struct scic_sds_remote_device *sci_dev,
873 struct scic_sds_request *request)
874{
875 enum sci_status status = SCI_SUCCESS;
876
877 status = scic_sds_request_complete(request);
878
879 if (status != SCI_SUCCESS)
880 return status;
881
882 status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
883 sci_dev, request);
884 if (status != SCI_SUCCESS)
885 return status;
886
887 scic_sds_remote_device_decrement_request_count(sci_dev);
888
889 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
890 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
891 scic_sds_cb_remote_device_rnc_destruct_complete,
892 sci_dev);
893 return SCI_SUCCESS;
894}
895
896/**
897 *
898 * @device: The struct scic_sds_remote_device which is to be cast into a
899 * struct scic_sds_remote_device object.
900 *
901 * This method will complete the reset operation when the device is in the
902 * resetting state. enum sci_status
903 */
904static enum sci_status scic_sds_remote_device_resetting_state_reset_complete_handler(
905 struct scic_sds_remote_device *sci_dev)
906{
907
908 sci_base_state_machine_change_state(
909 &sci_dev->state_machine,
910 SCI_BASE_REMOTE_DEVICE_STATE_READY
911 );
912
913 return SCI_SUCCESS;
914}
915
916/**
917 *
918 * @device: The struct scic_sds_remote_device which is to be cast into a
919 * struct scic_sds_remote_device object.
920 *
921 * This method will stop the remote device while in the resetting state.
922 * enum sci_status
923 */
924static enum sci_status scic_sds_remote_device_resetting_state_stop_handler(
925 struct scic_sds_remote_device *sci_dev)
926{
927 sci_base_state_machine_change_state(
928 &sci_dev->state_machine,
929 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
930 );
931
932 return SCI_SUCCESS;
933}
934
935/*
936 * This method completes requests for this struct scic_sds_remote_device while it is
937 * in the SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the
938 * complete method for the request object and if that is successful the port
939 * object is called to complete the task request. Then the device object itself
940 * completes the task request. enum sci_status
941 */
942static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
943 struct scic_sds_remote_device *sci_dev,
944 struct scic_sds_request *request)
945{
946 enum sci_status status = SCI_SUCCESS;
947
948 status = scic_sds_request_complete(request);
949
950 if (status == SCI_SUCCESS) {
951 status = scic_sds_port_complete_io(
952 scic_sds_remote_device_get_port(sci_dev),
953 sci_dev, request);
954
955 if (status == SCI_SUCCESS) {
956 scic_sds_remote_device_decrement_request_count(sci_dev);
957 }
958 }
959
960 return status;
961}
962
963static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
964 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
965 .start_handler = scic_sds_remote_device_default_start_handler,
966 .stop_handler = scic_sds_remote_device_default_stop_handler,
967 .fail_handler = scic_sds_remote_device_default_fail_handler,
968 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
969 .reset_handler = scic_sds_remote_device_default_reset_handler,
970 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
971 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
972 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
973 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
974 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
975 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
976 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
977 .resume_handler = scic_sds_remote_device_default_resume_handler,
978 .event_handler = scic_sds_remote_device_default_event_handler,
979 .frame_handler = scic_sds_remote_device_default_frame_handler
980 },
981 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
982 .start_handler = scic_sds_remote_device_stopped_state_start_handler,
983 .stop_handler = scic_sds_remote_device_stopped_state_stop_handler,
984 .fail_handler = scic_sds_remote_device_default_fail_handler,
985 .destruct_handler = scic_sds_remote_device_stopped_state_destruct_handler,
986 .reset_handler = scic_sds_remote_device_default_reset_handler,
987 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
988 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
989 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
990 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
991 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
992 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
993 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
994 .resume_handler = scic_sds_remote_device_default_resume_handler,
995 .event_handler = scic_sds_remote_device_default_event_handler,
996 .frame_handler = scic_sds_remote_device_default_frame_handler
997 },
998 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
999 .start_handler = scic_sds_remote_device_default_start_handler,
1000 .stop_handler = scic_sds_remote_device_starting_state_stop_handler,
1001 .fail_handler = scic_sds_remote_device_default_fail_handler,
1002 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1003 .reset_handler = scic_sds_remote_device_default_reset_handler,
1004 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1005 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1006 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1007 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1008 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1009 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1010 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1011 .resume_handler = scic_sds_remote_device_default_resume_handler,
1012 .event_handler = scic_sds_remote_device_general_event_handler,
1013 .frame_handler = scic_sds_remote_device_default_frame_handler
1014 },
1015 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1016 .start_handler = scic_sds_remote_device_default_start_handler,
1017 .stop_handler = scic_sds_remote_device_ready_state_stop_handler,
1018 .fail_handler = scic_sds_remote_device_default_fail_handler,
1019 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1020 .reset_handler = scic_sds_remote_device_ready_state_reset_handler,
1021 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1022 .start_io_handler = scic_sds_remote_device_ready_state_start_io_handler,
1023 .complete_io_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1024 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1025 .start_task_handler = scic_sds_remote_device_ready_state_start_task_handler,
1026 .complete_task_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1027 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1028 .resume_handler = scic_sds_remote_device_default_resume_handler,
1029 .event_handler = scic_sds_remote_device_general_event_handler,
1030 .frame_handler = scic_sds_remote_device_general_frame_handler,
1031 },
1032 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1033 .start_handler = scic_sds_remote_device_default_start_handler,
1034 .stop_handler = scic_sds_remote_device_stopping_state_stop_handler,
1035 .fail_handler = scic_sds_remote_device_default_fail_handler,
1036 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1037 .reset_handler = scic_sds_remote_device_default_reset_handler,
1038 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1039 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1040 .complete_io_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
1041 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1042 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1043 .complete_task_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
1044 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1045 .resume_handler = scic_sds_remote_device_default_resume_handler,
1046 .event_handler = scic_sds_remote_device_general_event_handler,
1047 .frame_handler = scic_sds_remote_device_general_frame_handler
1048 },
1049 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1050 .start_handler = scic_sds_remote_device_default_start_handler,
1051 .stop_handler = scic_sds_remote_device_default_stop_handler,
1052 .fail_handler = scic_sds_remote_device_default_fail_handler,
1053 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1054 .reset_handler = scic_sds_remote_device_default_reset_handler,
1055 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1056 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1057 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1058 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1059 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1060 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1061 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1062 .resume_handler = scic_sds_remote_device_default_resume_handler,
1063 .event_handler = scic_sds_remote_device_default_event_handler,
1064 .frame_handler = scic_sds_remote_device_general_frame_handler
1065 },
1066 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1067 .start_handler = scic_sds_remote_device_default_start_handler,
1068 .stop_handler = scic_sds_remote_device_resetting_state_stop_handler,
1069 .fail_handler = scic_sds_remote_device_default_fail_handler,
1070 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1071 .reset_handler = scic_sds_remote_device_default_reset_handler,
1072 .reset_complete_handler = scic_sds_remote_device_resetting_state_reset_complete_handler,
1073 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1074 .complete_io_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1075 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1076 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1077 .complete_task_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1078 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1079 .resume_handler = scic_sds_remote_device_default_resume_handler,
1080 .event_handler = scic_sds_remote_device_default_event_handler,
1081 .frame_handler = scic_sds_remote_device_general_frame_handler
1082 },
1083 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1084 .start_handler = scic_sds_remote_device_default_start_handler,
1085 .stop_handler = scic_sds_remote_device_default_stop_handler,
1086 .fail_handler = scic_sds_remote_device_default_fail_handler,
1087 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1088 .reset_handler = scic_sds_remote_device_default_reset_handler,
1089 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1090 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1091 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1092 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1093 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1094 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1095 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1096 .resume_handler = scic_sds_remote_device_default_resume_handler,
1097 .event_handler = scic_sds_remote_device_default_event_handler,
1098 .frame_handler = scic_sds_remote_device_default_frame_handler
1099 }
1100};
1101
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001102static void scic_sds_remote_device_initial_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001103{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001104 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001105
Dan Williams88f3b622011-04-22 19:18:03 -07001106 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1107 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1108
1109 /* Initial state is a transitional state to the stopped state */
1110 sci_base_state_machine_change_state(&sci_dev->state_machine,
1111 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1112}
1113
1114/**
1115 * isci_remote_device_change_state() - This function gets the status of the
1116 * remote_device object.
1117 * @isci_device: This parameter points to the isci_remote_device object
1118 *
1119 * status of the object as a isci_status enum.
1120 */
1121void isci_remote_device_change_state(
1122 struct isci_remote_device *isci_device,
1123 enum isci_status status)
1124{
1125 unsigned long flags;
1126
1127 spin_lock_irqsave(&isci_device->state_lock, flags);
1128 isci_device->status = status;
1129 spin_unlock_irqrestore(&isci_device->state_lock, flags);
1130}
1131
1132/**
1133 * scic_remote_device_destruct() - free remote node context and destruct
1134 * @remote_device: This parameter specifies the remote device to be destructed.
1135 *
1136 * Remote device objects are a limited resource. As such, they must be
1137 * protected. Thus calls to construct and destruct are mutually exclusive and
1138 * non-reentrant. The return value shall indicate if the device was
1139 * successfully destructed or if some failure occurred. enum sci_status This value
1140 * is returned if the device is successfully destructed.
1141 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
1142 * device isn't valid (e.g. it's already been destoryed, the handle isn't
1143 * valid, etc.).
1144 */
1145static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
1146{
1147 return sci_dev->state_handlers->destruct_handler(sci_dev);
1148}
1149
Dan Williams6f231dd2011-07-02 22:56:22 -07001150/**
1151 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -08001152 * @ihost: This parameter specifies the isci host object.
1153 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -07001154 *
1155 */
Dan Williamsd9c37392011-03-03 17:59:32 -08001156static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001157{
Dan Williamsd9c37392011-03-03 17:59:32 -08001158 dev_dbg(&ihost->pdev->dev,
1159 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001160
1161 /* There should not be any outstanding io's. All paths to
1162 * here should go through isci_remote_device_nuke_requests.
1163 * If we hit this condition, we will need a way to complete
1164 * io requests in process */
Dan Williamsd9c37392011-03-03 17:59:32 -08001165 while (!list_empty(&idev->reqs_in_process)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001166
Dan Williamsd9c37392011-03-03 17:59:32 -08001167 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001168 "%s: ** request list not empty! **\n", __func__);
1169 BUG();
1170 }
1171
Dan Williams57f20f42011-04-21 18:14:45 -07001172 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -08001173 idev->domain_dev->lldd_dev = NULL;
1174 idev->domain_dev = NULL;
1175 idev->isci_port = NULL;
1176 list_del_init(&idev->node);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001177
Dan Williamsd9c37392011-03-03 17:59:32 -08001178 clear_bit(IDEV_START_PENDING, &idev->flags);
1179 clear_bit(IDEV_STOP_PENDING, &idev->flags);
1180 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001181}
1182
Dan Williams88f3b622011-04-22 19:18:03 -07001183/**
1184 * isci_remote_device_stop_complete() - This function is called by the scic
1185 * when the remote device stop has completed. We mark the isci device as not
1186 * ready and remove the isci remote device.
1187 * @ihost: This parameter specifies the isci host object.
1188 * @idev: This parameter specifies the remote device.
1189 * @status: This parameter specifies status of the completion.
1190 *
1191 */
1192static void isci_remote_device_stop_complete(struct isci_host *ihost,
1193 struct isci_remote_device *idev)
1194{
1195 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1196
1197 isci_remote_device_change_state(idev, isci_stopped);
1198
1199 /* after stop, we can tear down resources. */
1200 isci_remote_device_deconstruct(ihost, idev);
1201}
1202
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001203static void scic_sds_remote_device_stopped_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001204{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001205 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001206 struct scic_sds_controller *scic;
1207 struct isci_remote_device *idev;
1208 struct isci_host *ihost;
1209 u32 prev_state;
1210
Dan Williams88f3b622011-04-22 19:18:03 -07001211 scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +00001212 ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001213 idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001214
1215 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1216 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1217
1218 /* If we are entering from the stopping state let the SCI User know that
1219 * the stop operation has completed.
1220 */
1221 prev_state = sci_dev->state_machine.previous_state_id;
1222 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1223 isci_remote_device_stop_complete(ihost, idev);
1224
1225 scic_sds_controller_remote_device_stopped(scic, sci_dev);
1226}
1227
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001228static void scic_sds_remote_device_starting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001229{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001230 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001231 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +00001232 struct isci_host *ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001233 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001234
1235 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1236 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1237
1238 isci_remote_device_not_ready(ihost, idev,
1239 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1240}
1241
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001242static void scic_sds_remote_device_ready_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001243{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001244 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001245 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +00001246 struct isci_host *ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001247 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001248
1249 SET_STATE_HANDLER(sci_dev,
1250 scic_sds_remote_device_state_handler_table,
1251 SCI_BASE_REMOTE_DEVICE_STATE_READY);
1252
1253 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1254
1255 if (sci_dev->has_ready_substate_machine)
1256 sci_base_state_machine_start(&sci_dev->ready_substate_machine);
1257 else
1258 isci_remote_device_ready(ihost, idev);
1259}
1260
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001261static void scic_sds_remote_device_ready_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001262{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001263 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001264 if (sci_dev->has_ready_substate_machine)
1265 sci_base_state_machine_stop(&sci_dev->ready_substate_machine);
1266 else {
1267 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +00001268 struct isci_host *ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001269 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001270
1271 isci_remote_device_not_ready(ihost, idev,
1272 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1273 }
1274}
1275
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001276static void scic_sds_remote_device_stopping_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001277{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001278 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001279
1280 SET_STATE_HANDLER(
1281 sci_dev,
1282 scic_sds_remote_device_state_handler_table,
1283 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1284 );
1285}
1286
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001287static void scic_sds_remote_device_failed_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001288{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001289 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001290
1291 SET_STATE_HANDLER(
1292 sci_dev,
1293 scic_sds_remote_device_state_handler_table,
1294 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1295 );
1296}
1297
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001298static void scic_sds_remote_device_resetting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001299{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001300 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001301
1302 SET_STATE_HANDLER(
1303 sci_dev,
1304 scic_sds_remote_device_state_handler_table,
1305 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1306 );
1307
1308 scic_sds_remote_node_context_suspend(
1309 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1310}
1311
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001312static void scic_sds_remote_device_resetting_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001313{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001314 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001315
1316 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1317}
1318
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001319static void scic_sds_remote_device_final_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001320{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001321 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001322
1323 SET_STATE_HANDLER(
1324 sci_dev,
1325 scic_sds_remote_device_state_handler_table,
1326 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1327 );
1328}
1329
1330
1331static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1332 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1333 .enter_state = scic_sds_remote_device_initial_state_enter,
1334 },
1335 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1336 .enter_state = scic_sds_remote_device_stopped_state_enter,
1337 },
1338 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1339 .enter_state = scic_sds_remote_device_starting_state_enter,
1340 },
1341 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1342 .enter_state = scic_sds_remote_device_ready_state_enter,
1343 .exit_state = scic_sds_remote_device_ready_state_exit
1344 },
1345 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1346 .enter_state = scic_sds_remote_device_stopping_state_enter,
1347 },
1348 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1349 .enter_state = scic_sds_remote_device_failed_state_enter,
1350 },
1351 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1352 .enter_state = scic_sds_remote_device_resetting_state_enter,
1353 .exit_state = scic_sds_remote_device_resetting_state_exit
1354 },
1355 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1356 .enter_state = scic_sds_remote_device_final_state_enter,
1357 },
1358};
1359
1360/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001361 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001362 * @sci_port: SAS/SATA port through which this device is accessed.
1363 * @sci_dev: remote device to construct
1364 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001365 * This routine just performs benign initialization and does not
1366 * allocate the remote_node_context which is left to
1367 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1368 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001369 */
1370static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1371 struct scic_sds_remote_device *sci_dev)
1372{
1373 sci_dev->owning_port = sci_port;
1374 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001375
1376 sci_base_state_machine_construct(
1377 &sci_dev->state_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001378 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001379 scic_sds_remote_device_state_table,
1380 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1381 );
1382
1383 sci_base_state_machine_start(
1384 &sci_dev->state_machine
1385 );
1386
1387 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1388 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1389
1390 sci_object_set_association(&sci_dev->rnc, sci_dev);
1391}
1392
1393/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001394 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001395 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001396 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1397 * the device is known to the SCI Core since it is contained in the
1398 * scic_phy object. Remote node context(s) is/are a global resource
1399 * allocated by this routine, freed by scic_remote_device_destruct().
1400 *
1401 * Returns:
1402 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1403 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1404 * sata-only controller instance.
1405 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001406 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001407static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1408 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001409{
1410 enum sci_status status;
1411 u16 remote_node_index;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001412 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001413
Dan Williamsb87ee302011-04-25 11:48:29 -07001414 scic_remote_device_construct(sci_port, sci_dev);
1415
Dan Williams88f3b622011-04-22 19:18:03 -07001416 /*
1417 * This information is request to determine how many remote node context
1418 * entries will be needed to store the remote node.
1419 */
Dan Williams88f3b622011-04-22 19:18:03 -07001420 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001421 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1422 sci_dev,
1423 &remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001424
Dan Williamsa1a113b2011-04-21 18:44:45 -07001425 if (status != SCI_SUCCESS)
1426 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001427
Dan Williamsa1a113b2011-04-21 18:44:45 -07001428 sci_dev->rnc.remote_node_index = remote_node_index;
Dan Williams88f3b622011-04-22 19:18:03 -07001429
Dan Williamsa1a113b2011-04-21 18:44:45 -07001430 if (dev->dev_type == SAS_END_DEV)
1431 sci_dev->has_ready_substate_machine = false;
1432 else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
1433 sci_dev->has_ready_substate_machine = true;
Dan Williams88f3b622011-04-22 19:18:03 -07001434
Dan Williamsa1a113b2011-04-21 18:44:45 -07001435 sci_base_state_machine_construct(
Dan Williams88f3b622011-04-22 19:18:03 -07001436 &sci_dev->ready_substate_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001437 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001438 scic_sds_stp_remote_device_ready_substate_table,
1439 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001440 } else if (dev_is_expander(dev)) {
1441 sci_dev->has_ready_substate_machine = true;
Dan Williams88f3b622011-04-22 19:18:03 -07001442
Dan Williamsa1a113b2011-04-21 18:44:45 -07001443 /* add the SMP ready substate machine construction here */
1444 sci_base_state_machine_construct(
Dan Williams88f3b622011-04-22 19:18:03 -07001445 &sci_dev->ready_substate_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001446 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001447 scic_sds_smp_remote_device_ready_substate_table,
1448 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001449 } else
1450 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001451
Dan Williamsa1a113b2011-04-21 18:44:45 -07001452 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001453
Dan Williamsa1a113b2011-04-21 18:44:45 -07001454 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1455 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001456
Dan Williamsa1a113b2011-04-21 18:44:45 -07001457 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001458}
1459
Dan Williams88f3b622011-04-22 19:18:03 -07001460/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001461 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001462 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001463 * Remote node context(s) is/are a global resource allocated by this
1464 * routine, freed by scic_remote_device_destruct().
1465 *
1466 * Returns:
1467 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1468 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1469 * sata-only controller instance.
1470 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001471 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001472static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001473 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001474{
Dan Williamsb87ee302011-04-25 11:48:29 -07001475 struct scic_sds_controller *scic = sci_port->owning_controller;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001476 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001477 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001478
Dan Williamsb87ee302011-04-25 11:48:29 -07001479 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001480
1481 status = scic_sds_controller_allocate_remote_node_context(
1482 scic, sci_dev, &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001483 if (status != SCI_SUCCESS)
1484 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001485
Dan Williamsa1a113b2011-04-21 18:44:45 -07001486 if (dev->dev_type == SAS_END_DEV)
1487 sci_dev->has_ready_substate_machine = false;
1488 else if (dev_is_expander(dev)) {
1489 sci_dev->has_ready_substate_machine = true;
Dan Williams88f3b622011-04-22 19:18:03 -07001490
Dan Williamsa1a113b2011-04-21 18:44:45 -07001491 /* add the SMP ready substate machine construction here */
1492 sci_base_state_machine_construct(
Dan Williams88f3b622011-04-22 19:18:03 -07001493 &sci_dev->ready_substate_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001494 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001495 scic_sds_smp_remote_device_ready_substate_table,
1496 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001497 } else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
1498 sci_dev->has_ready_substate_machine = true;
Dan Williams88f3b622011-04-22 19:18:03 -07001499
Dan Williamsa1a113b2011-04-21 18:44:45 -07001500 sci_base_state_machine_construct(
Dan Williams88f3b622011-04-22 19:18:03 -07001501 &sci_dev->ready_substate_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001502 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001503 scic_sds_stp_remote_device_ready_substate_table,
1504 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001505 }
Dan Williams88f3b622011-04-22 19:18:03 -07001506
Dan Williamsa1a113b2011-04-21 18:44:45 -07001507 /*
1508 * For SAS-2 the physical link rate is actually a logical link
1509 * rate that incorporates multiplexing. The SCU doesn't
1510 * incorporate multiplexing and for the purposes of the
1511 * connection the logical link rate is that same as the
1512 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1513 * one another, so this code works for both situations. */
1514 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001515 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001516
Dan Williamsa1a113b2011-04-21 18:44:45 -07001517 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1518 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001519
1520 return status;
1521}
1522
1523/**
1524 * scic_remote_device_start() - This method will start the supplied remote
1525 * device. This method enables normal IO requests to flow through to the
1526 * remote device.
1527 * @remote_device: This parameter specifies the device to be started.
1528 * @timeout: This parameter specifies the number of milliseconds in which the
1529 * start operation should complete.
1530 *
1531 * An indication of whether the device was successfully started. SCI_SUCCESS
1532 * This value is returned if the device was successfully started.
1533 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1534 * the device when there have been no phys added to it.
1535 */
1536static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
1537 u32 timeout)
1538{
1539 return sci_dev->state_handlers->start_handler(sci_dev);
1540}
Dan Williams6f231dd2011-07-02 22:56:22 -07001541
Dan Williams00d680e2011-04-25 14:29:29 -07001542static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1543 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001544{
Dan Williams00d680e2011-04-25 14:29:29 -07001545 struct scic_sds_port *sci_port = iport->sci_port_handle;
1546 struct isci_host *ihost = iport->isci_host;
1547 struct domain_device *dev = idev->domain_dev;
1548 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001549
Dan Williams00d680e2011-04-25 14:29:29 -07001550 if (dev->parent && dev_is_expander(dev->parent))
1551 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1552 else
1553 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001554
1555 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001556 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1557 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001558
1559 return status;
1560 }
1561
Dan Williams6f231dd2011-07-02 22:56:22 -07001562 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001563 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001564
Dan Williams00d680e2011-04-25 14:29:29 -07001565 if (status != SCI_SUCCESS)
1566 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1567 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001568
1569 return status;
1570}
1571
Dan Williams4393aa42011-03-31 13:10:44 -07001572void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001573{
1574 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001575
Dan Williams4393aa42011-03-31 13:10:44 -07001576 dev_dbg(&ihost->pdev->dev,
1577 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001578
1579 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -07001580 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -07001581
Dan Williams4393aa42011-03-31 13:10:44 -07001582 dev_dbg(&ihost->pdev->dev,
1583 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001584}
1585
Dan Williams6f231dd2011-07-02 22:56:22 -07001586/**
1587 * This function builds the isci_remote_device when a libsas dev_found message
1588 * is received.
1589 * @isci_host: This parameter specifies the isci host object.
1590 * @port: This parameter specifies the isci_port conected to this device.
1591 *
1592 * pointer to new isci_remote_device.
1593 */
1594static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001595isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001596{
Dan Williamsd9c37392011-03-03 17:59:32 -08001597 struct isci_remote_device *idev;
1598 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001599
Dan Williamsd9c37392011-03-03 17:59:32 -08001600 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001601 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001602 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1603 break;
1604 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001605
Dan Williamsd9c37392011-03-03 17:59:32 -08001606 if (i >= SCI_MAX_REMOTE_DEVICES) {
1607 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001608 return NULL;
1609 }
1610
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001611 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1612 return NULL;
1613
1614 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1615 return NULL;
1616
Dan Williamsd9c37392011-03-03 17:59:32 -08001617 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001618
Dan Williamsd9c37392011-03-03 17:59:32 -08001619 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001620}
Dan Williams6f231dd2011-07-02 22:56:22 -07001621
1622/**
1623 * isci_remote_device_ready() - This function is called by the scic when the
1624 * remote device is ready. We mark the isci device as ready and signal the
1625 * waiting proccess.
Dan Williams037afc72011-03-31 13:10:42 -07001626 * @ihost: our valid isci_host
1627 * @idev: remote device
Dan Williams6f231dd2011-07-02 22:56:22 -07001628 *
1629 */
Dan Williams037afc72011-03-31 13:10:42 -07001630void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001631{
Dan Williams6ad31fe2011-03-04 12:10:29 -08001632 dev_dbg(&ihost->pdev->dev,
Dan Williams1a380452011-03-03 18:01:43 -08001633 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001634
Dan Williams6ad31fe2011-03-04 12:10:29 -08001635 isci_remote_device_change_state(idev, isci_ready_for_io);
1636 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
1637 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001638}
1639
1640/**
1641 * isci_remote_device_not_ready() - This function is called by the scic when
1642 * the remote device is not ready. We mark the isci device as ready (not
1643 * "ready_for_io") and signal the waiting proccess.
1644 * @isci_host: This parameter specifies the isci host object.
1645 * @isci_device: This parameter specifies the remote device
1646 *
1647 */
Dan Williams037afc72011-03-31 13:10:42 -07001648void isci_remote_device_not_ready(struct isci_host *ihost,
1649 struct isci_remote_device *idev, u32 reason)
Dan Williams6f231dd2011-07-02 22:56:22 -07001650{
Dan Williams037afc72011-03-31 13:10:42 -07001651 dev_dbg(&ihost->pdev->dev,
1652 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001653
Dan Williams037afc72011-03-31 13:10:42 -07001654 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
1655 isci_remote_device_change_state(idev, isci_stopping);
Dan Williams6f231dd2011-07-02 22:56:22 -07001656 else
1657 /* device ready is actually a "not ready for io" state. */
Dan Williams037afc72011-03-31 13:10:42 -07001658 isci_remote_device_change_state(idev, isci_ready);
Dan Williams6f231dd2011-07-02 22:56:22 -07001659}
1660
1661/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001662 * isci_remote_device_stop() - This function is called internally to stop the
1663 * remote device.
1664 * @isci_host: This parameter specifies the isci host object.
1665 * @isci_device: This parameter specifies the remote device.
1666 *
1667 * The status of the scic request to stop.
1668 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001669enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001670{
1671 enum sci_status status;
1672 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001673
Dan Williams6ad31fe2011-03-04 12:10:29 -08001674 dev_dbg(&ihost->pdev->dev,
1675 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001676
Dan Williams6ad31fe2011-03-04 12:10:29 -08001677 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001678
1679 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001680 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001681
Dan Williams6ad31fe2011-03-04 12:10:29 -08001682 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001683
Dan Williams6ad31fe2011-03-04 12:10:29 -08001684 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07001685 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001686 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001687
1688 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -08001689 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -08001690 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08001691 clear_bit(IDEV_ALLOCATED, &idev->flags);
1692 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001693
Dan Williams6ad31fe2011-03-04 12:10:29 -08001694 dev_dbg(&ihost->pdev->dev,
1695 "%s: idev = %p - after completion wait\n",
1696 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001697
Dan Williams6f231dd2011-07-02 22:56:22 -07001698 return status;
1699}
1700
1701/**
1702 * isci_remote_device_gone() - This function is called by libsas when a domain
1703 * device is removed.
1704 * @domain_device: This parameter specifies the libsas domain device.
1705 *
1706 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001707void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001708{
Dan Williams4393aa42011-03-31 13:10:44 -07001709 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001710 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001711
Dan Williams6ad31fe2011-03-04 12:10:29 -08001712 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001713 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001714 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001715
Dan Williams6ad31fe2011-03-04 12:10:29 -08001716 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001717}
1718
1719
1720/**
1721 * isci_remote_device_found() - This function is called by libsas when a remote
1722 * device is discovered. A remote device object is created and started. the
1723 * function then sleeps until the sci core device started message is
1724 * received.
1725 * @domain_device: This parameter specifies the libsas domain device.
1726 *
1727 * status, zero indicates success.
1728 */
1729int isci_remote_device_found(struct domain_device *domain_dev)
1730{
Dan Williams4393aa42011-03-31 13:10:44 -07001731 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001732 struct isci_port *isci_port;
1733 struct isci_phy *isci_phy;
1734 struct asd_sas_port *sas_port;
1735 struct asd_sas_phy *sas_phy;
1736 struct isci_remote_device *isci_device;
1737 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001738
Dan Williams6f231dd2011-07-02 22:56:22 -07001739 dev_dbg(&isci_host->pdev->dev,
1740 "%s: domain_device = %p\n", __func__, domain_dev);
1741
Dan Williams0cf89d12011-02-18 09:25:07 -08001742 wait_for_start(isci_host);
1743
Dan Williams6f231dd2011-07-02 22:56:22 -07001744 sas_port = domain_dev->port;
1745 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1746 port_phy_el);
1747 isci_phy = to_isci_phy(sas_phy);
1748 isci_port = isci_phy->isci_port;
1749
1750 /* we are being called for a device on this port,
1751 * so it has to come up eventually
1752 */
1753 wait_for_completion(&isci_port->start_complete);
1754
1755 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1756 (isci_stopped == isci_port_get_state(isci_port)))
1757 return -ENODEV;
1758
1759 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001760 if (!isci_device)
1761 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001762
1763 INIT_LIST_HEAD(&isci_device->node);
1764 domain_dev->lldd_dev = isci_device;
1765 isci_device->domain_dev = domain_dev;
1766 isci_device->isci_port = isci_port;
1767 isci_remote_device_change_state(isci_device, isci_starting);
1768
1769
Dan Williams1a380452011-03-03 18:01:43 -08001770 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001771 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1772
Dan Williams6ad31fe2011-03-04 12:10:29 -08001773 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001774 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -08001775 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001776
Dan Williams6f231dd2011-07-02 22:56:22 -07001777 dev_dbg(&isci_host->pdev->dev,
1778 "%s: isci_device = %p\n",
1779 __func__, isci_device);
1780
1781 if (status != SCI_SUCCESS) {
1782
Dan Williams1a380452011-03-03 18:01:43 -08001783 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001784 isci_remote_device_deconstruct(
1785 isci_host,
1786 isci_device
1787 );
Dan Williams1a380452011-03-03 18:01:43 -08001788 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001789 return -ENODEV;
1790 }
1791
Dan Williams6ad31fe2011-03-04 12:10:29 -08001792 /* wait for the device ready callback. */
1793 wait_for_device_start(isci_host, isci_device);
1794
Dan Williams6f231dd2011-07-02 22:56:22 -07001795 return 0;
1796}
1797/**
1798 * isci_device_is_reset_pending() - This function will check if there is any
1799 * pending reset condition on the device.
1800 * @request: This parameter is the isci_device object.
1801 *
1802 * true if there is a reset pending for the device.
1803 */
1804bool isci_device_is_reset_pending(
1805 struct isci_host *isci_host,
1806 struct isci_remote_device *isci_device)
1807{
1808 struct isci_request *isci_request;
1809 struct isci_request *tmp_req;
1810 bool reset_is_pending = false;
1811 unsigned long flags;
1812
1813 dev_dbg(&isci_host->pdev->dev,
1814 "%s: isci_device = %p\n", __func__, isci_device);
1815
1816 spin_lock_irqsave(&isci_host->scic_lock, flags);
1817
1818 /* Check for reset on all pending requests. */
1819 list_for_each_entry_safe(isci_request, tmp_req,
1820 &isci_device->reqs_in_process, dev_node) {
1821 dev_dbg(&isci_host->pdev->dev,
1822 "%s: isci_device = %p request = %p\n",
1823 __func__, isci_device, isci_request);
1824
1825 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001826 struct sas_task *task = isci_request_access_task(
1827 isci_request);
1828
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001829 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001830 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1831 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001832 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001833 }
1834 }
1835
1836 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1837
1838 dev_dbg(&isci_host->pdev->dev,
1839 "%s: isci_device = %p reset_is_pending = %d\n",
1840 __func__, isci_device, reset_is_pending);
1841
1842 return reset_is_pending;
1843}
1844
1845/**
1846 * isci_device_clear_reset_pending() - This function will clear if any pending
1847 * reset condition flags on the device.
1848 * @request: This parameter is the isci_device object.
1849 *
1850 * true if there is a reset pending for the device.
1851 */
Dan Williams4393aa42011-03-31 13:10:44 -07001852void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001853{
1854 struct isci_request *isci_request;
1855 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07001856 unsigned long flags = 0;
1857
Dan Williams4393aa42011-03-31 13:10:44 -07001858 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1859 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07001860
Dan Williams4393aa42011-03-31 13:10:44 -07001861 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001862
1863 /* Clear reset pending on all pending requests. */
1864 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07001865 &idev->reqs_in_process, dev_node) {
1866 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1867 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001868
1869 if (isci_request->ttype == io_task) {
1870
1871 unsigned long flags2;
1872 struct sas_task *task = isci_request_access_task(
1873 isci_request);
1874
1875 spin_lock_irqsave(&task->task_state_lock, flags2);
1876 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1877 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1878 }
1879 }
Dan Williams4393aa42011-03-31 13:10:44 -07001880 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001881}