blob: 658781dd65e66705441018231b9018aa3c7b4876 [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 Williamsab2e8f72011-04-27 16:32:45 -070056#include "intel_ata.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070058#include "port.h"
59#include "remote_device.h"
60#include "request.h"
Dan Williams88f3b622011-04-22 19:18:03 -070061#include "scic_controller.h"
62#include "scic_io_request.h"
63#include "scic_phy.h"
64#include "scic_port.h"
65#include "scic_sds_controller.h"
66#include "scic_sds_phy.h"
67#include "scic_sds_port.h"
68#include "remote_node_context.h"
69#include "scic_sds_request.h"
70#include "sci_environment.h"
71#include "sci_util.h"
72#include "scu_event_codes.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070073#include "task.h"
74
Dan Williamsab2e8f72011-04-27 16:32:45 -070075/**
76 * isci_remote_device_change_state() - This function gets the status of the
77 * remote_device object.
78 * @isci_device: This parameter points to the isci_remote_device object
79 *
80 * status of the object as a isci_status enum.
81 */
82void isci_remote_device_change_state(
83 struct isci_remote_device *isci_device,
84 enum isci_status status)
85{
86 unsigned long flags;
87
88 spin_lock_irqsave(&isci_device->state_lock, flags);
89 isci_device->status = status;
90 spin_unlock_irqrestore(&isci_device->state_lock, flags);
91}
92
93/**
94 * isci_remote_device_not_ready() - This function is called by the scic when
95 * the remote device is not ready. We mark the isci device as ready (not
96 * "ready_for_io") and signal the waiting proccess.
97 * @isci_host: This parameter specifies the isci host object.
98 * @isci_device: This parameter specifies the remote device
99 *
100 */
101static void isci_remote_device_not_ready(struct isci_host *ihost,
102 struct isci_remote_device *idev, u32 reason)
103{
104 dev_dbg(&ihost->pdev->dev,
105 "%s: isci_device = %p\n", __func__, idev);
106
107 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
108 isci_remote_device_change_state(idev, isci_stopping);
109 else
110 /* device ready is actually a "not ready for io" state. */
111 isci_remote_device_change_state(idev, isci_ready);
112}
113
114/**
115 * isci_remote_device_ready() - This function is called by the scic when the
116 * remote device is ready. We mark the isci device as ready and signal the
117 * waiting proccess.
118 * @ihost: our valid isci_host
119 * @idev: remote device
120 *
121 */
122static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
123{
124 dev_dbg(&ihost->pdev->dev,
125 "%s: idev = %p\n", __func__, idev);
126
127 isci_remote_device_change_state(idev, isci_ready_for_io);
128 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
129 wake_up(&ihost->eventq);
130}
131
Dan Williamsec575662011-05-01 14:19:25 -0700132/* called once the remote node context is ready to be freed.
133 * The remote device can now report that its stop operation is complete. none
134 */
135static void rnc_destruct_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700136{
Dan Williamsec575662011-05-01 14:19:25 -0700137 struct scic_sds_remote_device *sci_dev = _dev;
138
139 BUG_ON(sci_dev->started_request_count != 0);
140 sci_base_state_machine_change_state(&sci_dev->state_machine,
141 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
142}
143
144static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
145{
146 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
147 u32 i, request_count = sci_dev->started_request_count;
148 enum sci_status status = SCI_SUCCESS;
149
150 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
151 struct scic_sds_request *sci_req;
152 enum sci_status s;
153
154 sci_req = scic->io_request_table[i];
155 if (!sci_req || sci_req->target_device != sci_dev)
156 continue;
157 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
158 if (s != SCI_SUCCESS)
159 status = s;
160 }
161
162 return status;
163}
164
165enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
166 u32 timeout)
167{
168 struct sci_base_state_machine *sm = &sci_dev->state_machine;
169 enum scic_sds_remote_device_states state = sm->current_state_id;
170
171 switch (state) {
172 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
173 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
174 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
175 default:
176 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
177 __func__, state);
178 return SCI_FAILURE_INVALID_STATE;
179 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
180 return SCI_SUCCESS;
181 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
182 /* device not started so there had better be no requests */
183 BUG_ON(sci_dev->started_request_count != 0);
184 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
185 rnc_destruct_done, sci_dev);
186 /* Transition to the stopping state and wait for the
187 * remote node to complete being posted and invalidated.
188 */
189 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
190 return SCI_SUCCESS;
191 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
192 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
193 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
194 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
195 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
196 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
197 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
198 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
199 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
200 if (sci_dev->started_request_count == 0) {
201 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
202 rnc_destruct_done, sci_dev);
203 return SCI_SUCCESS;
204 } else
205 return scic_sds_remote_device_terminate_requests(sci_dev);
206 break;
207 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
208 /* All requests should have been terminated, but if there is an
209 * attempt to stop a device already in the stopping state, then
210 * try again to terminate.
211 */
212 return scic_sds_remote_device_terminate_requests(sci_dev);
213 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
214 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
215 return SCI_SUCCESS;
216 }
Dan Williams88f3b622011-04-22 19:18:03 -0700217}
Dan Williams6f231dd2011-07-02 22:56:22 -0700218
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700219enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700220{
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700221 struct sci_base_state_machine *sm = &sci_dev->state_machine;
222 enum scic_sds_remote_device_states state = sm->current_state_id;
223
224 switch (state) {
225 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
226 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
227 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
228 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
229 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
230 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
231 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
232 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
233 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
234 default:
235 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
236 __func__, state);
237 return SCI_FAILURE_INVALID_STATE;
238 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
239 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
240 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
241 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
242 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
243 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
244 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
245 return SCI_SUCCESS;
246 }
Dan Williams88f3b622011-04-22 19:18:03 -0700247}
248
Dan Williams81515182011-05-01 14:53:00 -0700249enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700250{
Dan Williams81515182011-05-01 14:53:00 -0700251 struct sci_base_state_machine *sm = &sci_dev->state_machine;
252 enum scic_sds_remote_device_states state = sm->current_state_id;
Dan Williams88f3b622011-04-22 19:18:03 -0700253
Dan Williams81515182011-05-01 14:53:00 -0700254 if (state != SCI_BASE_REMOTE_DEVICE_STATE_RESETTING) {
255 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
256 __func__, state);
257 return SCI_FAILURE_INVALID_STATE;
258 }
259
260 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_READY);
261 return SCI_SUCCESS;
262}
Dan Williams88f3b622011-04-22 19:18:03 -0700263
Dan Williams88f3b622011-04-22 19:18:03 -0700264/**
265 *
266 * @sci_dev: The remote device for which the suspend is being requested.
267 *
268 * This method invokes the remote device suspend state handler. enum sci_status
269 */
270enum sci_status scic_sds_remote_device_suspend(
271 struct scic_sds_remote_device *sci_dev,
272 u32 suspend_type)
273{
274 return sci_dev->state_handlers->suspend_handler(sci_dev, suspend_type);
275}
276
277/**
278 *
279 * @sci_dev: The remote device for which the event handling is being
280 * requested.
281 * @frame_index: This is the frame index that is being processed.
282 *
283 * This method invokes the frame handler for the remote device state machine
284 * enum sci_status
285 */
286enum sci_status scic_sds_remote_device_frame_handler(
287 struct scic_sds_remote_device *sci_dev,
288 u32 frame_index)
289{
290 return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
291}
292
293/**
294 *
295 * @sci_dev: The remote device for which the event handling is being
296 * requested.
297 * @event_code: This is the event code that is to be processed.
298 *
299 * This method invokes the remote device event handler. enum sci_status
300 */
301enum sci_status scic_sds_remote_device_event_handler(
302 struct scic_sds_remote_device *sci_dev,
303 u32 event_code)
304{
305 return sci_dev->state_handlers->event_handler(sci_dev, event_code);
306}
307
308/**
309 *
310 * @controller: The controller that is starting the io request.
311 * @sci_dev: The remote device for which the start io handling is being
312 * requested.
313 * @io_request: The io request that is being started.
314 *
315 * This method invokes the remote device start io handler. enum sci_status
316 */
317enum sci_status scic_sds_remote_device_start_io(
318 struct scic_sds_controller *controller,
319 struct scic_sds_remote_device *sci_dev,
320 struct scic_sds_request *io_request)
321{
322 return sci_dev->state_handlers->start_io_handler(
323 sci_dev, io_request);
324}
325
326/**
327 *
328 * @controller: The controller that is completing the io request.
329 * @sci_dev: The remote device for which the complete io handling is being
330 * requested.
331 * @io_request: The io request that is being completed.
332 *
333 * This method invokes the remote device complete io handler. enum sci_status
334 */
335enum sci_status scic_sds_remote_device_complete_io(
336 struct scic_sds_controller *controller,
337 struct scic_sds_remote_device *sci_dev,
338 struct scic_sds_request *io_request)
339{
340 return sci_dev->state_handlers->complete_io_handler(
341 sci_dev, io_request);
342}
343
344/**
345 *
346 * @controller: The controller that is starting the task request.
347 * @sci_dev: The remote device for which the start task handling is being
348 * requested.
349 * @io_request: The task request that is being started.
350 *
351 * This method invokes the remote device start task handler. enum sci_status
352 */
353enum sci_status scic_sds_remote_device_start_task(
354 struct scic_sds_controller *controller,
355 struct scic_sds_remote_device *sci_dev,
356 struct scic_sds_request *io_request)
357{
358 return sci_dev->state_handlers->start_task_handler(
359 sci_dev, io_request);
360}
361
362/**
363 *
Dan Williams88f3b622011-04-22 19:18:03 -0700364 * @sci_dev:
365 * @request:
366 *
367 * This method takes the request and bulids an appropriate SCU context for the
368 * request and then requests the controller to post the request. none
369 */
370void scic_sds_remote_device_post_request(
371 struct scic_sds_remote_device *sci_dev,
372 u32 request)
373{
374 u32 context;
375
376 context = scic_sds_remote_device_build_command_context(sci_dev, request);
377
378 scic_sds_controller_post_request(
379 scic_sds_remote_device_get_controller(sci_dev),
380 context
381 );
382}
383
Dan Williamsab2e8f72011-04-27 16:32:45 -0700384/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700385 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700386 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700387 */
Dan Williamseb229672011-05-01 14:05:57 -0700388static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700389{
Dan Williamsab2e8f72011-04-27 16:32:45 -0700390 struct scic_sds_remote_device *sci_dev = _dev;
391 enum scic_sds_remote_device_states state;
Dan Williams88f3b622011-04-22 19:18:03 -0700392
Dan Williamsab2e8f72011-04-27 16:32:45 -0700393 state = sci_dev->state_machine.current_state_id;
394 switch (state) {
395 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
396 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
397 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
398 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
399 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
400 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
401 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
402 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
403 break;
404 default:
405 /* go 'ready' if we are not already in a ready state */
406 sci_base_state_machine_change_state(&sci_dev->state_machine,
407 SCI_BASE_REMOTE_DEVICE_STATE_READY);
408 break;
Dan Williams88f3b622011-04-22 19:18:03 -0700409 }
410}
411
412/**
413 *
414 * @device: This parameter specifies the device for which the request is being
415 * started.
416 * @request: This parameter specifies the request being started.
417 * @status: This parameter specifies the current start operation status.
418 *
419 * This method will perform the STP request start processing common to IO
420 * requests and task requests of all types. none
421 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700422static void scic_sds_remote_device_start_request(
Dan Williams88f3b622011-04-22 19:18:03 -0700423 struct scic_sds_remote_device *sci_dev,
424 struct scic_sds_request *sci_req,
425 enum sci_status status)
426{
427 /* We still have a fault in starting the io complete it on the port */
428 if (status == SCI_SUCCESS)
429 scic_sds_remote_device_increment_request_count(sci_dev);
430 else{
431 sci_dev->owning_port->state_handlers->complete_io_handler(
432 sci_dev->owning_port, sci_dev, sci_req
433 );
434 }
435}
436
437
438/**
439 *
440 * @request: This parameter specifies the request being continued.
441 *
442 * This method will continue to post tc for a STP request. This method usually
443 * serves as a callback when RNC gets resumed during a task management
444 * sequence. none
445 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700446static void scic_sds_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700447{
448 struct scic_sds_remote_device *sci_dev = dev;
449
450 /* we need to check if this request is still valid to continue. */
451 if (sci_dev->working_request)
452 scic_controller_continue_io(sci_dev->working_request);
453}
454
Dan Williams88f3b622011-04-22 19:18:03 -0700455static enum sci_status
456default_device_handler(struct scic_sds_remote_device *sci_dev,
457 const char *func)
458{
459 dev_warn(scirdev_to_dev(sci_dev),
460 "%s: in wrong state: %d\n", func,
461 sci_base_state_machine_get_state(&sci_dev->state_machine));
462 return SCI_FAILURE_INVALID_STATE;
463}
464
Dan Williamsab2e8f72011-04-27 16:32:45 -0700465static enum sci_status scic_sds_remote_device_default_suspend_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700466 struct scic_sds_remote_device *sci_dev, u32 suspend_type)
467{
468 return default_device_handler(sci_dev, __func__);
469}
470
Dan Williamsab2e8f72011-04-27 16:32:45 -0700471static enum sci_status scic_sds_remote_device_default_resume_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700472 struct scic_sds_remote_device *sci_dev)
473{
474 return default_device_handler(sci_dev, __func__);
475}
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_core_event_handler(
489 struct scic_sds_remote_device *sci_dev,
490 u32 event_code,
491 bool is_ready_state)
492{
493 enum sci_status status;
494
495 switch (scu_get_event_type(event_code)) {
496 case SCU_EVENT_TYPE_RNC_OPS_MISC:
497 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
498 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
499 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
500 break;
501 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
502
503 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
504 status = SCI_SUCCESS;
505
506 /* Suspend the associated RNC */
507 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
508 SCI_SOFTWARE_SUSPENSION,
509 NULL, NULL);
510
511 dev_dbg(scirdev_to_dev(sci_dev),
512 "%s: device: %p event code: %x: %s\n",
513 __func__, sci_dev, event_code,
514 (is_ready_state)
515 ? "I_T_Nexus_Timeout event"
516 : "I_T_Nexus_Timeout event in wrong state");
517
518 break;
519 }
520 /* Else, fall through and treat as unhandled... */
521
522 default:
523 dev_dbg(scirdev_to_dev(sci_dev),
524 "%s: device: %p event code: %x: %s\n",
525 __func__, sci_dev, event_code,
526 (is_ready_state)
527 ? "unexpected event"
528 : "unexpected event in wrong state");
529 status = SCI_FAILURE_INVALID_STATE;
530 break;
531 }
532
533 return status;
534}
535/**
536 *
537 * @device: The struct scic_sds_remote_device which is then cast into a
538 * struct scic_sds_remote_device.
539 * @event_code: The event code that the struct scic_sds_controller wants the device
540 * object to process.
541 *
542 * This method is the default event handler. It will call the RNC state
543 * machine handler for any RNC events otherwise it will log a warning and
544 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
545 */
546static enum sci_status scic_sds_remote_device_default_event_handler(
547 struct scic_sds_remote_device *sci_dev,
548 u32 event_code)
549{
550 return scic_sds_remote_device_core_event_handler(sci_dev,
551 event_code,
552 false);
553}
554
555/**
556 *
557 * @device: The struct scic_sds_remote_device which is then cast into a
558 * struct scic_sds_remote_device.
559 * @frame_index: The frame index for which the struct scic_sds_controller wants this
560 * device object to process.
561 *
562 * This method is the default unsolicited frame handler. It logs a warning,
563 * releases the frame and returns a failure. enum sci_status
564 * SCI_FAILURE_INVALID_STATE
565 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700566static enum sci_status scic_sds_remote_device_default_frame_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700567 struct scic_sds_remote_device *sci_dev,
568 u32 frame_index)
569{
570 dev_warn(scirdev_to_dev(sci_dev),
571 "%s: SCIC Remote Device requested to handle frame %x "
572 "while in wrong state %d\n",
573 __func__,
574 frame_index,
575 sci_base_state_machine_get_state(
576 &sci_dev->state_machine));
577
578 /* Return the frame back to the controller */
579 scic_sds_controller_release_frame(
580 scic_sds_remote_device_get_controller(sci_dev), frame_index
581 );
582
583 return SCI_FAILURE_INVALID_STATE;
584}
585
Dan Williamsab2e8f72011-04-27 16:32:45 -0700586static enum sci_status scic_sds_remote_device_default_start_request_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700587 struct scic_sds_remote_device *sci_dev,
588 struct scic_sds_request *request)
589{
590 return default_device_handler(sci_dev, __func__);
591}
592
Dan Williamsab2e8f72011-04-27 16:32:45 -0700593static enum sci_status scic_sds_remote_device_default_complete_request_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700594 struct scic_sds_remote_device *sci_dev,
595 struct scic_sds_request *request)
596{
597 return default_device_handler(sci_dev, __func__);
598}
599
Dan Williamsab2e8f72011-04-27 16:32:45 -0700600static enum sci_status scic_sds_remote_device_default_continue_request_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700601 struct scic_sds_remote_device *sci_dev,
602 struct scic_sds_request *request)
603{
604 return default_device_handler(sci_dev, __func__);
605}
606
607/**
608 *
609 * @device: The struct scic_sds_remote_device which is then cast into a
610 * struct scic_sds_remote_device.
611 * @frame_index: The frame index for which the struct scic_sds_controller wants this
612 * device object to process.
613 *
614 * This method is a general ssp frame handler. In most cases the device object
615 * needs to route the unsolicited frame processing to the io request object.
616 * This method decodes the tag for the io request object and routes the
617 * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
618 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700619static enum sci_status scic_sds_remote_device_general_frame_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700620 struct scic_sds_remote_device *sci_dev,
621 u32 frame_index)
622{
623 enum sci_status result;
624 struct sci_ssp_frame_header *frame_header;
625 struct scic_sds_request *io_request;
626
627 result = scic_sds_unsolicited_frame_control_get_header(
628 &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
629 frame_index,
630 (void **)&frame_header
631 );
632
633 if (SCI_SUCCESS == result) {
634 io_request = scic_sds_controller_get_io_request_from_tag(
635 scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
636
637 if ((io_request == NULL)
638 || (io_request->target_device != sci_dev)) {
639 /*
640 * We could not map this tag to a valid IO request
641 * Just toss the frame and continue */
642 scic_sds_controller_release_frame(
643 scic_sds_remote_device_get_controller(sci_dev), frame_index
644 );
645 } else {
646 /* The IO request is now in charge of releasing the frame */
647 result = io_request->state_handlers->frame_handler(
648 io_request, frame_index);
649 }
650 }
651
652 return result;
653}
654
655/**
656 *
657 * @[in]: sci_dev This is the device object that is receiving the event.
658 * @[in]: event_code The event code to process.
659 *
660 * This is a common method for handling events reported to the remote device
661 * from the controller object. enum sci_status
662 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700663static enum sci_status scic_sds_remote_device_general_event_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700664 struct scic_sds_remote_device *sci_dev,
665 u32 event_code)
666{
667 return scic_sds_remote_device_core_event_handler(sci_dev,
668 event_code,
669 true);
670}
671
Dan Williams88f3b622011-04-22 19:18:03 -0700672/*
673 * This method will attempt to start a task request for this device object. The
674 * remote device object will issue the start request for the task and if
675 * successful it will start the request for the port object then increment its
676 * own requet count. enum sci_status SCI_SUCCESS if the task request is started for
677 * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
678 * object could not get the resources to start.
679 */
680static enum sci_status scic_sds_remote_device_ready_state_start_task_handler(
681 struct scic_sds_remote_device *sci_dev,
682 struct scic_sds_request *request)
683{
684 enum sci_status result;
685
686 /* See if the port is in a state where we can start the IO request */
687 result = scic_sds_port_start_io(
688 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
689
690 if (result == SCI_SUCCESS) {
691 result = scic_sds_remote_node_context_start_task(&sci_dev->rnc,
692 request);
693 if (result == SCI_SUCCESS)
694 result = scic_sds_request_start(request);
695
696 scic_sds_remote_device_start_request(sci_dev, request, result);
697 }
698
699 return result;
700}
701
702/*
703 * This method will attempt to start an io request for this device object. The
704 * remote device object will issue the start request for the io and if
705 * successful it will start the request for the port object then increment its
706 * own requet count. enum sci_status SCI_SUCCESS if the io request is started for
707 * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
708 * object could not get the resources to start.
709 */
710static enum sci_status scic_sds_remote_device_ready_state_start_io_handler(
711 struct scic_sds_remote_device *sci_dev,
712 struct scic_sds_request *request)
713{
714 enum sci_status result;
715
716 /* See if the port is in a state where we can start the IO request */
717 result = scic_sds_port_start_io(
718 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
719
720 if (result == SCI_SUCCESS) {
721 result = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
722 if (result == SCI_SUCCESS)
723 result = scic_sds_request_start(request);
724
725 scic_sds_remote_device_start_request(sci_dev, request, result);
726 }
727
728 return result;
729}
730
731/*
732 * This method will complete the request for the remote device object. The
733 * method will call the completion handler for the request object and if
734 * successful it will complete the request on the port object then decrement
735 * its own started_request_count. enum sci_status
736 */
737static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
738 struct scic_sds_remote_device *sci_dev,
739 struct scic_sds_request *request)
740{
741 enum sci_status result;
742
743 result = scic_sds_request_complete(request);
744
745 if (result != SCI_SUCCESS)
746 return result;
747
748 /* See if the port is in a state
749 * where we can start the IO request */
750 result = scic_sds_port_complete_io(
751 scic_sds_remote_device_get_port(sci_dev),
752 sci_dev, request);
753
754 if (result == SCI_SUCCESS)
755 scic_sds_remote_device_decrement_request_count(sci_dev);
756
757 return result;
758}
759
Dan Williams88f3b622011-04-22 19:18:03 -0700760/**
761 *
762 * @device: The device object for which the request is completing.
763 * @request: The task request that is being completed.
764 *
765 * This method completes requests for this struct scic_sds_remote_device while it is
766 * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
767 * complete method for the request object and if that is successful the port
768 * object is called to complete the task request. Then the device object itself
769 * completes the task request. If struct scic_sds_remote_device started_request_count
770 * goes to 0 and the invalidate RNC request has completed the device object can
771 * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
772 */
773static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
774 struct scic_sds_remote_device *sci_dev,
775 struct scic_sds_request *request)
776{
777 enum sci_status status = SCI_SUCCESS;
778
779 status = scic_sds_request_complete(request);
780
781 if (status != SCI_SUCCESS)
782 return status;
783
784 status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
785 sci_dev, request);
786 if (status != SCI_SUCCESS)
787 return status;
788
789 scic_sds_remote_device_decrement_request_count(sci_dev);
790
791 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
792 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
Dan Williamsec575662011-05-01 14:19:25 -0700793 rnc_destruct_done, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700794 return SCI_SUCCESS;
795}
796
Dan Williamsab2e8f72011-04-27 16:32:45 -0700797/* complete requests for this device while it is in the
798 * SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the complete
799 * method for the request object and if that is successful the port object is
800 * called to complete the task request. Then the device object itself completes
801 * the task request. enum sci_status
Dan Williams88f3b622011-04-22 19:18:03 -0700802 */
803static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
804 struct scic_sds_remote_device *sci_dev,
805 struct scic_sds_request *request)
806{
807 enum sci_status status = SCI_SUCCESS;
808
809 status = scic_sds_request_complete(request);
810
811 if (status == SCI_SUCCESS) {
812 status = scic_sds_port_complete_io(
813 scic_sds_remote_device_get_port(sci_dev),
814 sci_dev, request);
815
816 if (status == SCI_SUCCESS) {
817 scic_sds_remote_device_decrement_request_count(sci_dev);
818 }
819 }
820
821 return status;
822}
823
Dan Williamsab2e8f72011-04-27 16:32:45 -0700824static enum sci_status scic_sds_stp_remote_device_complete_request(struct scic_sds_remote_device *sci_dev,
825 struct scic_sds_request *sci_req)
826{
827 enum sci_status status;
828
829 status = scic_sds_io_request_complete(sci_req);
830 if (status != SCI_SUCCESS)
831 goto out;
832
833 status = scic_sds_port_complete_io(sci_dev->owning_port, sci_dev, sci_req);
834 if (status != SCI_SUCCESS)
835 goto out;
836
837 scic_sds_remote_device_decrement_request_count(sci_dev);
838 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
839 /* This request causes hardware error, device needs to be Lun Reset.
840 * So here we force the state machine to IDLE state so the rest IOs
841 * can reach RNC state handler, these IOs will be completed by RNC with
842 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
843 */
844 sci_base_state_machine_change_state(&sci_dev->state_machine,
845 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
846 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
847 sci_base_state_machine_change_state(&sci_dev->state_machine,
848 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
849
850
851 out:
852 if (status != SCI_SUCCESS)
853 dev_err(scirdev_to_dev(sci_dev),
854 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
855 "could not complete\n", __func__, sci_dev->owning_port,
856 sci_dev, sci_req, status);
857
858 return status;
859}
860
861/* scic_sds_stp_remote_device_ready_substate_start_request_handler - start stp
862 * @device: The target device a task management request towards to.
863 * @request: The task request.
864 *
865 * This is the READY NCQ substate handler to start task management request. In
866 * this routine, we suspend and resume the RNC. enum sci_status Always return
867 * SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS status to let
868 * controller_start_task_handler know that the controller can't post TC for
869 * task request yet, instead, when RNC gets resumed, a controller_continue_task
870 * callback will be called.
871 */
872static enum sci_status scic_sds_stp_remote_device_ready_substate_start_request_handler(
873 struct scic_sds_remote_device *device,
874 struct scic_sds_request *request)
875{
876 enum sci_status status;
877
878 /* Will the port allow the io request to start? */
879 status = device->owning_port->state_handlers->start_io_handler(
880 device->owning_port, device, request);
881 if (status != SCI_SUCCESS)
882 return status;
883
884 status = scic_sds_remote_node_context_start_task(&device->rnc, request);
885 if (status != SCI_SUCCESS)
886 goto out;
887
888 status = request->state_handlers->start_handler(request);
889 if (status != SCI_SUCCESS)
890 goto out;
891
892 /*
893 * Note: If the remote device state is not IDLE this will replace
894 * the request that probably resulted in the task management request.
895 */
896 device->working_request = request;
897 sci_base_state_machine_change_state(&device->state_machine,
898 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
899
900 /*
901 * The remote node context must cleanup the TCi to NCQ mapping table.
902 * The only way to do this correctly is to either write to the TLCR
903 * register or to invalidate and repost the RNC. In either case the
904 * remote node context state machine will take the correct action when
905 * the remote node context is suspended and later resumed.
906 */
907 scic_sds_remote_node_context_suspend(&device->rnc,
908 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
909 scic_sds_remote_node_context_resume(&device->rnc,
910 scic_sds_remote_device_continue_request,
911 device);
912
913out:
914 scic_sds_remote_device_start_request(device, request, status);
915 /*
916 * We need to let the controller start request handler know that it can't
917 * post TC yet. We will provide a callback function to post TC when RNC gets
918 * resumed.
919 */
920 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
921}
922
923/* handle the start io operation for a sata device that is in the command idle
924 * state. - Evalute the type of IO request to be started - If its an NCQ
925 * request change to NCQ substate - If its any other command change to the CMD
926 * substate
927 *
928 * If this is a softreset we may want to have a different substate.
929 */
930static enum sci_status scic_sds_stp_remote_device_ready_idle_substate_start_io_handler(
931 struct scic_sds_remote_device *sci_dev,
932 struct scic_sds_request *request)
933{
934 enum sci_status status;
935 struct isci_request *isci_request = request->ireq;
936 enum scic_sds_remote_device_states new_state;
937
938 /* Will the port allow the io request to start? */
939 status = sci_dev->owning_port->state_handlers->start_io_handler(
940 sci_dev->owning_port, sci_dev, request);
941 if (status != SCI_SUCCESS)
942 return status;
943
944 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
945 if (status != SCI_SUCCESS)
946 goto out;
947
948 status = request->state_handlers->start_handler(request);
949 if (status != SCI_SUCCESS)
950 goto out;
951
952 if (isci_sata_get_sat_protocol(isci_request) == SAT_PROTOCOL_FPDMA)
953 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
954 else {
955 sci_dev->working_request = request;
956 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
957 }
958 sci_base_state_machine_change_state(&sci_dev->state_machine, new_state);
959out:
960 scic_sds_remote_device_start_request(sci_dev, request, status);
961 return status;
962}
963
964
965static enum sci_status scic_sds_stp_remote_device_ready_idle_substate_event_handler(
966 struct scic_sds_remote_device *sci_dev,
967 u32 event_code)
968{
969 enum sci_status status;
970
971 status = scic_sds_remote_device_general_event_handler(sci_dev, event_code);
972 if (status != SCI_SUCCESS)
973 return status;
974
975 /* We pick up suspension events to handle specifically to this state. We
976 * resume the RNC right away. enum sci_status
977 */
978 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
979 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
980 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
981
982 return status;
983}
984
985static enum sci_status scic_sds_stp_remote_device_ready_ncq_substate_start_io_handler(
986 struct scic_sds_remote_device *sci_dev,
987 struct scic_sds_request *request)
988{
989 enum sci_status status;
990 struct isci_request *isci_request = request->ireq;
991 scic_sds_port_io_request_handler_t start_io;
992
993 if (isci_sata_get_sat_protocol(isci_request) == SAT_PROTOCOL_FPDMA) {
994 start_io = sci_dev->owning_port->state_handlers->start_io_handler;
995 status = start_io(sci_dev->owning_port, sci_dev, request);
996 if (status != SCI_SUCCESS)
997 return status;
998
999 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
Dan Williamsf619fff2011-05-01 10:13:04 -07001000 if (status == SCI_SUCCESS)
1001 status = request->state_handlers->start_handler(request);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001002
1003 scic_sds_remote_device_start_request(sci_dev, request, status);
1004 } else
1005 status = SCI_FAILURE_INVALID_STATE;
1006
1007 return status;
1008}
1009
1010static enum sci_status scic_sds_stp_remote_device_ready_ncq_substate_frame_handler(struct scic_sds_remote_device *sci_dev,
1011 u32 frame_index)
1012{
1013 enum sci_status status;
1014 struct sata_fis_header *frame_header;
1015 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1016
1017 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1018 frame_index,
1019 (void **)&frame_header);
1020 if (status != SCI_SUCCESS)
1021 return status;
1022
1023 if (frame_header->fis_type == SATA_FIS_TYPE_SETDEVBITS &&
1024 (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
1025 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
1026
1027 /* TODO Check sactive and complete associated IO if any. */
1028
1029 sci_base_state_machine_change_state(&sci_dev->state_machine,
1030 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1031 } else if (frame_header->fis_type == SATA_FIS_TYPE_REGD2H &&
1032 (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
1033 /*
1034 * Some devices return D2H FIS when an NCQ error is detected.
1035 * Treat this like an SDB error FIS ready reason.
1036 */
1037 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
1038
1039 sci_base_state_machine_change_state(&sci_dev->state_machine,
1040 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1041 } else
1042 status = SCI_FAILURE;
1043
1044 scic_sds_controller_release_frame(scic, frame_index);
1045
1046 return status;
1047}
1048
1049static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_start_io_handler(
1050 struct scic_sds_remote_device *device,
1051 struct scic_sds_request *request)
1052{
1053 /* device is already handling a command it can not accept new commands
1054 * until this one is complete.
1055 */
1056 return SCI_FAILURE_INVALID_STATE;
1057}
1058
1059static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler(
1060 struct scic_sds_remote_device *sci_dev,
1061 u32 suspend_type)
1062{
1063 enum sci_status status;
1064
1065 status = scic_sds_remote_node_context_suspend(&sci_dev->rnc,
1066 suspend_type, NULL, NULL);
1067
1068 return status;
1069}
1070
1071static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_frame_handler(
1072 struct scic_sds_remote_device *sci_dev,
1073 u32 frame_index)
1074{
1075 /* The device doe not process any UF received from the hardware while
1076 * in this state. All unsolicited frames are forwarded to the io
1077 * request object.
1078 */
1079 return scic_sds_io_request_frame_handler(sci_dev->working_request,
1080 frame_index);
1081}
1082
1083static enum sci_status scic_sds_stp_remote_device_ready_await_reset_substate_start_io_handler(
1084 struct scic_sds_remote_device *device,
1085 struct scic_sds_request *request)
1086{
1087 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
1088}
1089
1090static enum sci_status scic_sds_stp_remote_device_ready_await_reset_substate_complete_request_handler(
1091 struct scic_sds_remote_device *device,
1092 struct scic_sds_request *request)
1093{
1094 struct scic_sds_request *sci_req = request;
1095 enum sci_status status;
1096
1097 status = scic_sds_io_request_complete(sci_req);
1098 if (status != SCI_SUCCESS)
1099 goto out;
1100
1101 status = scic_sds_port_complete_io(device->owning_port, device, sci_req);
1102 if (status != SCI_SUCCESS)
1103 goto out;
1104
1105 scic_sds_remote_device_decrement_request_count(device);
1106 out:
1107 if (status != SCI_SUCCESS)
1108 dev_err(scirdev_to_dev(device),
1109 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
1110 "could not complete\n",
1111 __func__, device->owning_port, device, sci_req, status);
1112
1113 return status;
1114}
1115
1116static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
1117{
1118 struct scic_sds_remote_device *sci_dev = _dev;
1119 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1120 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1121
1122 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
1123 * As a result, avoid sending the ready notification.
1124 */
1125 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
1126 isci_remote_device_ready(scic->ihost, idev);
1127}
1128
1129static enum sci_status scic_sds_smp_remote_device_ready_idle_substate_start_io_handler(
1130 struct scic_sds_remote_device *sci_dev,
1131 struct scic_sds_request *sci_req)
1132{
1133 enum sci_status status;
1134
1135 /* Will the port allow the io request to start? */
1136 status = sci_dev->owning_port->state_handlers->start_io_handler(
1137 sci_dev->owning_port, sci_dev, sci_req);
1138 if (status != SCI_SUCCESS)
1139 return status;
1140
1141 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
1142 if (status != SCI_SUCCESS)
Dan Williamsf619fff2011-05-01 10:13:04 -07001143 goto out;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001144
1145 status = scic_sds_request_start(sci_req);
1146 if (status != SCI_SUCCESS)
Dan Williamsf619fff2011-05-01 10:13:04 -07001147 goto out;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001148
1149 sci_dev->working_request = sci_req;
1150 sci_base_state_machine_change_state(&sci_dev->state_machine,
1151 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1152
Dan Williamsf619fff2011-05-01 10:13:04 -07001153 out:
Dan Williamsab2e8f72011-04-27 16:32:45 -07001154 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
1155
1156 return status;
1157}
1158
1159static enum sci_status scic_sds_smp_remote_device_ready_cmd_substate_start_io_handler(
1160 struct scic_sds_remote_device *device,
1161 struct scic_sds_request *request)
1162{
1163 /* device is already handling a command it can not accept new commands
1164 * until this one is complete.
1165 */
1166 return SCI_FAILURE_INVALID_STATE;
1167}
1168
1169static enum sci_status
1170scic_sds_smp_remote_device_ready_cmd_substate_complete_io_handler(struct scic_sds_remote_device *sci_dev,
1171 struct scic_sds_request *sci_req)
1172{
1173 enum sci_status status;
1174
1175 status = scic_sds_io_request_complete(sci_req);
1176 if (status != SCI_SUCCESS)
1177 return status;
1178
1179 status = scic_sds_port_complete_io(sci_dev->owning_port, sci_dev, sci_req);
1180 if (status != SCI_SUCCESS) {
1181 dev_err(scirdev_to_dev(sci_dev),
1182 "%s: SCIC SDS Remote Device 0x%p io request "
1183 "0x%p could not be completd on the port 0x%p "
1184 "failed with status %d.\n", __func__, sci_dev, sci_req,
1185 sci_dev->owning_port, status);
1186 return status;
1187 }
1188
1189 scic_sds_remote_device_decrement_request_count(sci_dev);
1190 sci_base_state_machine_change_state(&sci_dev->state_machine,
1191 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1192
1193 return status;
1194}
1195
1196static enum sci_status scic_sds_smp_remote_device_ready_cmd_substate_frame_handler(
1197 struct scic_sds_remote_device *sci_dev,
1198 u32 frame_index)
1199{
1200 enum sci_status status;
1201
1202 /* The device does not process any UF received from the hardware while
1203 * in this state. All unsolicited frames are forwarded to the io request
1204 * object.
1205 */
1206 status = scic_sds_io_request_frame_handler(
1207 sci_dev->working_request,
1208 frame_index
1209 );
1210
1211 return status;
1212}
1213
Dan Williams88f3b622011-04-22 19:18:03 -07001214static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
1215 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001216 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1217 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1218 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1219 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1220 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1221 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1222 .resume_handler = scic_sds_remote_device_default_resume_handler,
1223 .event_handler = scic_sds_remote_device_default_event_handler,
1224 .frame_handler = scic_sds_remote_device_default_frame_handler
1225 },
1226 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001227 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1228 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1229 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1230 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1231 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1232 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1233 .resume_handler = scic_sds_remote_device_default_resume_handler,
1234 .event_handler = scic_sds_remote_device_default_event_handler,
1235 .frame_handler = scic_sds_remote_device_default_frame_handler
1236 },
1237 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001238 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1239 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1240 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1241 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1242 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1243 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1244 .resume_handler = scic_sds_remote_device_default_resume_handler,
1245 .event_handler = scic_sds_remote_device_general_event_handler,
1246 .frame_handler = scic_sds_remote_device_default_frame_handler
1247 },
1248 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001249 .start_io_handler = scic_sds_remote_device_ready_state_start_io_handler,
1250 .complete_io_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1251 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1252 .start_task_handler = scic_sds_remote_device_ready_state_start_task_handler,
1253 .complete_task_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1254 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1255 .resume_handler = scic_sds_remote_device_default_resume_handler,
1256 .event_handler = scic_sds_remote_device_general_event_handler,
1257 .frame_handler = scic_sds_remote_device_general_frame_handler,
1258 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001259 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001260 .start_io_handler = scic_sds_stp_remote_device_ready_idle_substate_start_io_handler,
1261 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1262 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1263 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1264 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1265 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1266 .resume_handler = scic_sds_remote_device_default_resume_handler,
1267 .event_handler = scic_sds_stp_remote_device_ready_idle_substate_event_handler,
1268 .frame_handler = scic_sds_remote_device_default_frame_handler
1269 },
1270 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001271 .start_io_handler = scic_sds_stp_remote_device_ready_cmd_substate_start_io_handler,
1272 .complete_io_handler = scic_sds_stp_remote_device_complete_request,
1273 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1274 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1275 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1276 .suspend_handler = scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler,
1277 .resume_handler = scic_sds_remote_device_default_resume_handler,
1278 .event_handler = scic_sds_remote_device_general_event_handler,
1279 .frame_handler = scic_sds_stp_remote_device_ready_cmd_substate_frame_handler
1280 },
1281 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001282 .start_io_handler = scic_sds_stp_remote_device_ready_ncq_substate_start_io_handler,
1283 .complete_io_handler = scic_sds_stp_remote_device_complete_request,
1284 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1285 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1286 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1287 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1288 .resume_handler = scic_sds_remote_device_default_resume_handler,
1289 .event_handler = scic_sds_remote_device_general_event_handler,
1290 .frame_handler = scic_sds_stp_remote_device_ready_ncq_substate_frame_handler
1291 },
1292 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001293 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1294 .complete_io_handler = scic_sds_stp_remote_device_complete_request,
1295 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1296 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1297 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1298 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1299 .resume_handler = scic_sds_remote_device_default_resume_handler,
1300 .event_handler = scic_sds_remote_device_general_event_handler,
1301 .frame_handler = scic_sds_remote_device_general_frame_handler
1302 },
1303 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001304 .start_io_handler = scic_sds_stp_remote_device_ready_await_reset_substate_start_io_handler,
1305 .complete_io_handler = scic_sds_stp_remote_device_ready_await_reset_substate_complete_request_handler,
1306 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1307 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1308 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1309 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1310 .resume_handler = scic_sds_remote_device_default_resume_handler,
1311 .event_handler = scic_sds_remote_device_general_event_handler,
1312 .frame_handler = scic_sds_remote_device_general_frame_handler
1313 },
1314 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001315 .start_io_handler = scic_sds_smp_remote_device_ready_idle_substate_start_io_handler,
1316 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1317 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1318 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1319 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1320 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1321 .resume_handler = scic_sds_remote_device_default_resume_handler,
1322 .event_handler = scic_sds_remote_device_general_event_handler,
1323 .frame_handler = scic_sds_remote_device_default_frame_handler
1324 },
1325 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001326 .start_io_handler = scic_sds_smp_remote_device_ready_cmd_substate_start_io_handler,
1327 .complete_io_handler = scic_sds_smp_remote_device_ready_cmd_substate_complete_io_handler,
1328 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1329 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1330 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1331 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1332 .resume_handler = scic_sds_remote_device_default_resume_handler,
1333 .event_handler = scic_sds_remote_device_general_event_handler,
1334 .frame_handler = scic_sds_smp_remote_device_ready_cmd_substate_frame_handler
1335 },
Dan Williams88f3b622011-04-22 19:18:03 -07001336 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001337 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1338 .complete_io_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
1339 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1340 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1341 .complete_task_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
1342 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1343 .resume_handler = scic_sds_remote_device_default_resume_handler,
1344 .event_handler = scic_sds_remote_device_general_event_handler,
1345 .frame_handler = scic_sds_remote_device_general_frame_handler
1346 },
1347 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001348 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1349 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1350 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1351 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1352 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1353 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1354 .resume_handler = scic_sds_remote_device_default_resume_handler,
1355 .event_handler = scic_sds_remote_device_default_event_handler,
1356 .frame_handler = scic_sds_remote_device_general_frame_handler
1357 },
1358 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001359 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1360 .complete_io_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1361 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1362 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1363 .complete_task_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1364 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1365 .resume_handler = scic_sds_remote_device_default_resume_handler,
1366 .event_handler = scic_sds_remote_device_default_event_handler,
1367 .frame_handler = scic_sds_remote_device_general_frame_handler
1368 },
1369 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001370 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1371 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1372 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1373 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1374 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1375 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1376 .resume_handler = scic_sds_remote_device_default_resume_handler,
1377 .event_handler = scic_sds_remote_device_default_event_handler,
1378 .frame_handler = scic_sds_remote_device_default_frame_handler
1379 }
1380};
1381
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001382static void scic_sds_remote_device_initial_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001383{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001384 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001385
Dan Williams88f3b622011-04-22 19:18:03 -07001386 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1387 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1388
1389 /* Initial state is a transitional state to the stopped state */
1390 sci_base_state_machine_change_state(&sci_dev->state_machine,
1391 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1392}
1393
1394/**
Dan Williams88f3b622011-04-22 19:18:03 -07001395 * scic_remote_device_destruct() - free remote node context and destruct
1396 * @remote_device: This parameter specifies the remote device to be destructed.
1397 *
1398 * Remote device objects are a limited resource. As such, they must be
1399 * protected. Thus calls to construct and destruct are mutually exclusive and
1400 * non-reentrant. The return value shall indicate if the device was
1401 * successfully destructed or if some failure occurred. enum sci_status This value
1402 * is returned if the device is successfully destructed.
1403 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
1404 * device isn't valid (e.g. it's already been destoryed, the handle isn't
1405 * valid, etc.).
1406 */
1407static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
1408{
Dan Williamsb8d82f62011-05-01 14:38:26 -07001409 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1410 enum scic_sds_remote_device_states state = sm->current_state_id;
1411 struct scic_sds_controller *scic;
1412
1413 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1414 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1415 __func__, state);
1416 return SCI_FAILURE_INVALID_STATE;
1417 }
1418
1419 scic = sci_dev->owning_port->owning_controller;
1420 scic_sds_controller_free_remote_node_context(scic, sci_dev,
1421 sci_dev->rnc.remote_node_index);
1422 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
1423 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
1424
1425 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001426}
1427
Dan Williams6f231dd2011-07-02 22:56:22 -07001428/**
1429 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -08001430 * @ihost: This parameter specifies the isci host object.
1431 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -07001432 *
1433 */
Dan Williamsd9c37392011-03-03 17:59:32 -08001434static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001435{
Dan Williamsd9c37392011-03-03 17:59:32 -08001436 dev_dbg(&ihost->pdev->dev,
1437 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001438
1439 /* There should not be any outstanding io's. All paths to
1440 * here should go through isci_remote_device_nuke_requests.
1441 * If we hit this condition, we will need a way to complete
1442 * io requests in process */
Dan Williamsd9c37392011-03-03 17:59:32 -08001443 while (!list_empty(&idev->reqs_in_process)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001444
Dan Williamsd9c37392011-03-03 17:59:32 -08001445 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001446 "%s: ** request list not empty! **\n", __func__);
1447 BUG();
1448 }
1449
Dan Williams57f20f42011-04-21 18:14:45 -07001450 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -08001451 idev->domain_dev->lldd_dev = NULL;
1452 idev->domain_dev = NULL;
1453 idev->isci_port = NULL;
1454 list_del_init(&idev->node);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001455
Dan Williamsd9c37392011-03-03 17:59:32 -08001456 clear_bit(IDEV_START_PENDING, &idev->flags);
1457 clear_bit(IDEV_STOP_PENDING, &idev->flags);
1458 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001459}
1460
Dan Williams88f3b622011-04-22 19:18:03 -07001461/**
1462 * isci_remote_device_stop_complete() - This function is called by the scic
1463 * when the remote device stop has completed. We mark the isci device as not
1464 * ready and remove the isci remote device.
1465 * @ihost: This parameter specifies the isci host object.
1466 * @idev: This parameter specifies the remote device.
1467 * @status: This parameter specifies status of the completion.
1468 *
1469 */
1470static void isci_remote_device_stop_complete(struct isci_host *ihost,
1471 struct isci_remote_device *idev)
1472{
1473 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1474
1475 isci_remote_device_change_state(idev, isci_stopped);
1476
1477 /* after stop, we can tear down resources. */
1478 isci_remote_device_deconstruct(ihost, idev);
1479}
1480
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001481static void scic_sds_remote_device_stopped_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001482{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001483 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001484 struct scic_sds_controller *scic;
1485 struct isci_remote_device *idev;
1486 struct isci_host *ihost;
1487 u32 prev_state;
1488
Dan Williams88f3b622011-04-22 19:18:03 -07001489 scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +00001490 ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001491 idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001492
1493 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1494 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1495
1496 /* If we are entering from the stopping state let the SCI User know that
1497 * the stop operation has completed.
1498 */
1499 prev_state = sci_dev->state_machine.previous_state_id;
1500 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1501 isci_remote_device_stop_complete(ihost, idev);
1502
1503 scic_sds_controller_remote_device_stopped(scic, sci_dev);
1504}
1505
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001506static void scic_sds_remote_device_starting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001507{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001508 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001509 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +00001510 struct isci_host *ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001511 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001512
1513 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1514 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1515
1516 isci_remote_device_not_ready(ihost, idev,
1517 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1518}
1519
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001520static void scic_sds_remote_device_ready_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001521{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001522 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001523 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1524 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001525
1526 SET_STATE_HANDLER(sci_dev,
1527 scic_sds_remote_device_state_handler_table,
1528 SCI_BASE_REMOTE_DEVICE_STATE_READY);
1529
1530 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1531
Dan Williamsab2e8f72011-04-27 16:32:45 -07001532 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
1533 sci_base_state_machine_change_state(&sci_dev->state_machine,
1534 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1535 } else if (dev_is_expander(dev)) {
1536 sci_base_state_machine_change_state(&sci_dev->state_machine,
1537 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1538 } else
1539 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
Dan Williams88f3b622011-04-22 19:18:03 -07001540}
1541
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001542static void scic_sds_remote_device_ready_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001543{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001544 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001545 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1546
1547 if (dev->dev_type == SAS_END_DEV) {
1548 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001549 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001550
Dan Williamsab2e8f72011-04-27 16:32:45 -07001551 isci_remote_device_not_ready(scic->ihost, idev,
Dan Williams88f3b622011-04-22 19:18:03 -07001552 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1553 }
1554}
1555
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001556static void scic_sds_remote_device_stopping_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001557{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001558 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001559
1560 SET_STATE_HANDLER(
1561 sci_dev,
1562 scic_sds_remote_device_state_handler_table,
1563 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1564 );
1565}
1566
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001567static void scic_sds_remote_device_failed_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001568{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001569 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001570
1571 SET_STATE_HANDLER(
1572 sci_dev,
1573 scic_sds_remote_device_state_handler_table,
1574 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1575 );
1576}
1577
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001578static void scic_sds_remote_device_resetting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001579{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001580 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001581
1582 SET_STATE_HANDLER(
1583 sci_dev,
1584 scic_sds_remote_device_state_handler_table,
1585 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1586 );
1587
1588 scic_sds_remote_node_context_suspend(
1589 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1590}
1591
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001592static void scic_sds_remote_device_resetting_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001593{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001594 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001595
1596 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1597}
1598
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001599static void scic_sds_remote_device_final_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001600{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001601 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001602
1603 SET_STATE_HANDLER(
1604 sci_dev,
1605 scic_sds_remote_device_state_handler_table,
1606 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1607 );
1608}
1609
Dan Williamsab2e8f72011-04-27 16:32:45 -07001610static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
1611{
1612 struct scic_sds_remote_device *sci_dev = object;
1613
1614 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1615 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1616
1617 sci_dev->working_request = NULL;
1618 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
1619 /*
1620 * Since the RNC is ready, it's alright to finish completion
1621 * processing (e.g. signal the remote device is ready). */
1622 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
1623 } else {
1624 scic_sds_remote_node_context_resume(&sci_dev->rnc,
1625 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
1626 sci_dev);
1627 }
1628}
1629
1630static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1631{
1632 struct scic_sds_remote_device *sci_dev = object;
1633 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1634
1635 BUG_ON(sci_dev->working_request == NULL);
1636
1637 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1638 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1639
1640 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1641 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1642}
1643
1644static void scic_sds_stp_remote_device_ready_ncq_substate_enter(void *object)
1645{
1646 struct scic_sds_remote_device *sci_dev = object;
1647
1648 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1649 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ);
1650}
1651
1652static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1653{
1654 struct scic_sds_remote_device *sci_dev = object;
1655 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1656 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1657
1658 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1659 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1660
1661 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1662 isci_remote_device_not_ready(scic->ihost, idev,
1663 sci_dev->not_ready_reason);
1664}
1665
1666static void scic_sds_stp_remote_device_ready_await_reset_substate_enter(void *object)
1667{
1668 struct scic_sds_remote_device *sci_dev = object;
1669
1670 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1671 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
1672}
1673
1674static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1675{
1676 struct scic_sds_remote_device *sci_dev = object;
1677 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1678
1679 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1680 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1681
1682 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1683}
1684
1685static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1686{
1687 struct scic_sds_remote_device *sci_dev = object;
1688 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1689
1690 BUG_ON(sci_dev->working_request == NULL);
1691
1692 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1693 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1694
1695 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1696 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1697}
1698
1699static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1700{
1701 struct scic_sds_remote_device *sci_dev = object;
1702
1703 sci_dev->working_request = NULL;
1704}
Dan Williams88f3b622011-04-22 19:18:03 -07001705
1706static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1707 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1708 .enter_state = scic_sds_remote_device_initial_state_enter,
1709 },
1710 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1711 .enter_state = scic_sds_remote_device_stopped_state_enter,
1712 },
1713 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1714 .enter_state = scic_sds_remote_device_starting_state_enter,
1715 },
1716 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1717 .enter_state = scic_sds_remote_device_ready_state_enter,
1718 .exit_state = scic_sds_remote_device_ready_state_exit
1719 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001720 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1721 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1722 },
1723 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1724 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1725 },
1726 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
1727 .enter_state = scic_sds_stp_remote_device_ready_ncq_substate_enter,
1728 },
1729 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1730 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1731 },
1732 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
1733 .enter_state = scic_sds_stp_remote_device_ready_await_reset_substate_enter,
1734 },
1735 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1736 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1737 },
1738 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1739 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1740 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1741 },
Dan Williams88f3b622011-04-22 19:18:03 -07001742 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1743 .enter_state = scic_sds_remote_device_stopping_state_enter,
1744 },
1745 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1746 .enter_state = scic_sds_remote_device_failed_state_enter,
1747 },
1748 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1749 .enter_state = scic_sds_remote_device_resetting_state_enter,
1750 .exit_state = scic_sds_remote_device_resetting_state_exit
1751 },
1752 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1753 .enter_state = scic_sds_remote_device_final_state_enter,
1754 },
1755};
1756
1757/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001758 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001759 * @sci_port: SAS/SATA port through which this device is accessed.
1760 * @sci_dev: remote device to construct
1761 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001762 * This routine just performs benign initialization and does not
1763 * allocate the remote_node_context which is left to
1764 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1765 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001766 */
1767static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1768 struct scic_sds_remote_device *sci_dev)
1769{
1770 sci_dev->owning_port = sci_port;
1771 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001772
1773 sci_base_state_machine_construct(
1774 &sci_dev->state_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001775 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001776 scic_sds_remote_device_state_table,
1777 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1778 );
1779
1780 sci_base_state_machine_start(
1781 &sci_dev->state_machine
1782 );
1783
1784 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1785 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001786}
1787
1788/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001789 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001790 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001791 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1792 * the device is known to the SCI Core since it is contained in the
1793 * scic_phy object. Remote node context(s) is/are a global resource
1794 * allocated by this routine, freed by scic_remote_device_destruct().
1795 *
1796 * Returns:
1797 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1798 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1799 * sata-only controller instance.
1800 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001801 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001802static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1803 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001804{
1805 enum sci_status status;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001806 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001807
Dan Williamsb87ee302011-04-25 11:48:29 -07001808 scic_remote_device_construct(sci_port, sci_dev);
1809
Dan Williams88f3b622011-04-22 19:18:03 -07001810 /*
1811 * This information is request to determine how many remote node context
1812 * entries will be needed to store the remote node.
1813 */
Dan Williams88f3b622011-04-22 19:18:03 -07001814 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001815 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1816 sci_dev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001817 &sci_dev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001818
Dan Williamsa1a113b2011-04-21 18:44:45 -07001819 if (status != SCI_SUCCESS)
1820 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001821
Dan Williamsab2e8f72011-04-27 16:32:45 -07001822 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1823 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1824 /* pass */;
1825 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001826 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001827
Dan Williamsa1a113b2011-04-21 18:44:45 -07001828 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001829
Dan Williamsa1a113b2011-04-21 18:44:45 -07001830 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1831 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001832
Dan Williamsa1a113b2011-04-21 18:44:45 -07001833 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001834}
1835
Dan Williams88f3b622011-04-22 19:18:03 -07001836/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001837 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001838 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001839 * Remote node context(s) is/are a global resource allocated by this
1840 * routine, freed by scic_remote_device_destruct().
1841 *
1842 * Returns:
1843 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1844 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1845 * sata-only controller instance.
1846 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001847 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001848static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001849 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001850{
Dan Williamsa1a113b2011-04-21 18:44:45 -07001851 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001852 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001853
Dan Williamsb87ee302011-04-25 11:48:29 -07001854 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001855
Dan Williamsab2e8f72011-04-27 16:32:45 -07001856 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1857 sci_dev,
1858 &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001859 if (status != SCI_SUCCESS)
1860 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001861
Dan Williamsab2e8f72011-04-27 16:32:45 -07001862 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1863 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1864 /* pass */;
1865 else
1866 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001867
Dan Williamsa1a113b2011-04-21 18:44:45 -07001868 /*
1869 * For SAS-2 the physical link rate is actually a logical link
1870 * rate that incorporates multiplexing. The SCU doesn't
1871 * incorporate multiplexing and for the purposes of the
1872 * connection the logical link rate is that same as the
1873 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1874 * one another, so this code works for both situations. */
1875 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001876 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001877
Dan Williamsa1a113b2011-04-21 18:44:45 -07001878 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1879 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001880
Dan Williamsab2e8f72011-04-27 16:32:45 -07001881 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001882}
1883
1884/**
1885 * scic_remote_device_start() - This method will start the supplied remote
1886 * device. This method enables normal IO requests to flow through to the
1887 * remote device.
1888 * @remote_device: This parameter specifies the device to be started.
1889 * @timeout: This parameter specifies the number of milliseconds in which the
1890 * start operation should complete.
1891 *
1892 * An indication of whether the device was successfully started. SCI_SUCCESS
1893 * This value is returned if the device was successfully started.
1894 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1895 * the device when there have been no phys added to it.
1896 */
1897static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
Dan Williamseb229672011-05-01 14:05:57 -07001898 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001899{
Dan Williamseb229672011-05-01 14:05:57 -07001900 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1901 enum scic_sds_remote_device_states state = sm->current_state_id;
1902 enum sci_status status;
1903
1904 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1905 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1906 __func__, state);
1907 return SCI_FAILURE_INVALID_STATE;
1908 }
1909
1910 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1911 remote_device_resume_done,
1912 sci_dev);
1913 if (status != SCI_SUCCESS)
1914 return status;
1915
1916 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1917
1918 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001919}
Dan Williams6f231dd2011-07-02 22:56:22 -07001920
Dan Williams00d680e2011-04-25 14:29:29 -07001921static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1922 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001923{
Dan Williams00d680e2011-04-25 14:29:29 -07001924 struct scic_sds_port *sci_port = iport->sci_port_handle;
1925 struct isci_host *ihost = iport->isci_host;
1926 struct domain_device *dev = idev->domain_dev;
1927 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001928
Dan Williams00d680e2011-04-25 14:29:29 -07001929 if (dev->parent && dev_is_expander(dev->parent))
1930 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1931 else
1932 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001933
1934 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001935 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1936 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001937
1938 return status;
1939 }
1940
Dan Williams6f231dd2011-07-02 22:56:22 -07001941 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001942 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001943
Dan Williams00d680e2011-04-25 14:29:29 -07001944 if (status != SCI_SUCCESS)
1945 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1946 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001947
1948 return status;
1949}
1950
Dan Williams4393aa42011-03-31 13:10:44 -07001951void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001952{
1953 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001954
Dan Williams4393aa42011-03-31 13:10:44 -07001955 dev_dbg(&ihost->pdev->dev,
1956 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001957
1958 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -07001959 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -07001960
Dan Williams4393aa42011-03-31 13:10:44 -07001961 dev_dbg(&ihost->pdev->dev,
1962 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001963}
1964
Dan Williams6f231dd2011-07-02 22:56:22 -07001965/**
1966 * This function builds the isci_remote_device when a libsas dev_found message
1967 * is received.
1968 * @isci_host: This parameter specifies the isci host object.
1969 * @port: This parameter specifies the isci_port conected to this device.
1970 *
1971 * pointer to new isci_remote_device.
1972 */
1973static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001974isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001975{
Dan Williamsd9c37392011-03-03 17:59:32 -08001976 struct isci_remote_device *idev;
1977 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001978
Dan Williamsd9c37392011-03-03 17:59:32 -08001979 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001980 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001981 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1982 break;
1983 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001984
Dan Williamsd9c37392011-03-03 17:59:32 -08001985 if (i >= SCI_MAX_REMOTE_DEVICES) {
1986 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001987 return NULL;
1988 }
1989
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001990 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1991 return NULL;
1992
1993 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1994 return NULL;
1995
Dan Williamsd9c37392011-03-03 17:59:32 -08001996 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001997
Dan Williamsd9c37392011-03-03 17:59:32 -08001998 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001999}
Dan Williams6f231dd2011-07-02 22:56:22 -07002000
2001/**
Dan Williams6f231dd2011-07-02 22:56:22 -07002002 * isci_remote_device_stop() - This function is called internally to stop the
2003 * remote device.
2004 * @isci_host: This parameter specifies the isci host object.
2005 * @isci_device: This parameter specifies the remote device.
2006 *
2007 * The status of the scic request to stop.
2008 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08002009enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002010{
2011 enum sci_status status;
2012 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07002013
Dan Williams6ad31fe2011-03-04 12:10:29 -08002014 dev_dbg(&ihost->pdev->dev,
2015 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002016
Dan Williams6ad31fe2011-03-04 12:10:29 -08002017 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07002018
2019 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07002020 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07002021
Dan Williams6ad31fe2011-03-04 12:10:29 -08002022 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002023
Dan Williams6ad31fe2011-03-04 12:10:29 -08002024 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07002025 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08002026 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002027
2028 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -08002029 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -08002030 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08002031 clear_bit(IDEV_ALLOCATED, &idev->flags);
2032 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002033
Dan Williams6ad31fe2011-03-04 12:10:29 -08002034 dev_dbg(&ihost->pdev->dev,
2035 "%s: idev = %p - after completion wait\n",
2036 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002037
Dan Williams6f231dd2011-07-02 22:56:22 -07002038 return status;
2039}
2040
2041/**
2042 * isci_remote_device_gone() - This function is called by libsas when a domain
2043 * device is removed.
2044 * @domain_device: This parameter specifies the libsas domain device.
2045 *
2046 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08002047void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002048{
Dan Williams4393aa42011-03-31 13:10:44 -07002049 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08002050 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07002051
Dan Williams6ad31fe2011-03-04 12:10:29 -08002052 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002053 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08002054 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07002055
Dan Williams6ad31fe2011-03-04 12:10:29 -08002056 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002057}
2058
2059
2060/**
2061 * isci_remote_device_found() - This function is called by libsas when a remote
2062 * device is discovered. A remote device object is created and started. the
2063 * function then sleeps until the sci core device started message is
2064 * received.
2065 * @domain_device: This parameter specifies the libsas domain device.
2066 *
2067 * status, zero indicates success.
2068 */
2069int isci_remote_device_found(struct domain_device *domain_dev)
2070{
Dan Williams4393aa42011-03-31 13:10:44 -07002071 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002072 struct isci_port *isci_port;
2073 struct isci_phy *isci_phy;
2074 struct asd_sas_port *sas_port;
2075 struct asd_sas_phy *sas_phy;
2076 struct isci_remote_device *isci_device;
2077 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07002078
Dan Williams6f231dd2011-07-02 22:56:22 -07002079 dev_dbg(&isci_host->pdev->dev,
2080 "%s: domain_device = %p\n", __func__, domain_dev);
2081
Dan Williams0cf89d12011-02-18 09:25:07 -08002082 wait_for_start(isci_host);
2083
Dan Williams6f231dd2011-07-02 22:56:22 -07002084 sas_port = domain_dev->port;
2085 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
2086 port_phy_el);
2087 isci_phy = to_isci_phy(sas_phy);
2088 isci_port = isci_phy->isci_port;
2089
2090 /* we are being called for a device on this port,
2091 * so it has to come up eventually
2092 */
2093 wait_for_completion(&isci_port->start_complete);
2094
2095 if ((isci_stopping == isci_port_get_state(isci_port)) ||
2096 (isci_stopped == isci_port_get_state(isci_port)))
2097 return -ENODEV;
2098
2099 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08002100 if (!isci_device)
2101 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07002102
2103 INIT_LIST_HEAD(&isci_device->node);
2104 domain_dev->lldd_dev = isci_device;
2105 isci_device->domain_dev = domain_dev;
2106 isci_device->isci_port = isci_port;
2107 isci_remote_device_change_state(isci_device, isci_starting);
2108
2109
Dan Williams1a380452011-03-03 18:01:43 -08002110 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002111 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
2112
Dan Williams6ad31fe2011-03-04 12:10:29 -08002113 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002114 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -08002115 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002116
Dan Williams6f231dd2011-07-02 22:56:22 -07002117 dev_dbg(&isci_host->pdev->dev,
2118 "%s: isci_device = %p\n",
2119 __func__, isci_device);
2120
2121 if (status != SCI_SUCCESS) {
2122
Dan Williams1a380452011-03-03 18:01:43 -08002123 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002124 isci_remote_device_deconstruct(
2125 isci_host,
2126 isci_device
2127 );
Dan Williams1a380452011-03-03 18:01:43 -08002128 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002129 return -ENODEV;
2130 }
2131
Dan Williams6ad31fe2011-03-04 12:10:29 -08002132 /* wait for the device ready callback. */
2133 wait_for_device_start(isci_host, isci_device);
2134
Dan Williams6f231dd2011-07-02 22:56:22 -07002135 return 0;
2136}
2137/**
2138 * isci_device_is_reset_pending() - This function will check if there is any
2139 * pending reset condition on the device.
2140 * @request: This parameter is the isci_device object.
2141 *
2142 * true if there is a reset pending for the device.
2143 */
2144bool isci_device_is_reset_pending(
2145 struct isci_host *isci_host,
2146 struct isci_remote_device *isci_device)
2147{
2148 struct isci_request *isci_request;
2149 struct isci_request *tmp_req;
2150 bool reset_is_pending = false;
2151 unsigned long flags;
2152
2153 dev_dbg(&isci_host->pdev->dev,
2154 "%s: isci_device = %p\n", __func__, isci_device);
2155
2156 spin_lock_irqsave(&isci_host->scic_lock, flags);
2157
2158 /* Check for reset on all pending requests. */
2159 list_for_each_entry_safe(isci_request, tmp_req,
2160 &isci_device->reqs_in_process, dev_node) {
2161 dev_dbg(&isci_host->pdev->dev,
2162 "%s: isci_device = %p request = %p\n",
2163 __func__, isci_device, isci_request);
2164
2165 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07002166 struct sas_task *task = isci_request_access_task(
2167 isci_request);
2168
Bartosz Barcinski467e8552011-04-12 17:28:41 -07002169 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002170 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
2171 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07002172 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002173 }
2174 }
2175
2176 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
2177
2178 dev_dbg(&isci_host->pdev->dev,
2179 "%s: isci_device = %p reset_is_pending = %d\n",
2180 __func__, isci_device, reset_is_pending);
2181
2182 return reset_is_pending;
2183}
2184
2185/**
2186 * isci_device_clear_reset_pending() - This function will clear if any pending
2187 * reset condition flags on the device.
2188 * @request: This parameter is the isci_device object.
2189 *
2190 * true if there is a reset pending for the device.
2191 */
Dan Williams4393aa42011-03-31 13:10:44 -07002192void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002193{
2194 struct isci_request *isci_request;
2195 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07002196 unsigned long flags = 0;
2197
Dan Williams4393aa42011-03-31 13:10:44 -07002198 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
2199 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07002200
Dan Williams4393aa42011-03-31 13:10:44 -07002201 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002202
2203 /* Clear reset pending on all pending requests. */
2204 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07002205 &idev->reqs_in_process, dev_node) {
2206 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
2207 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07002208
2209 if (isci_request->ttype == io_task) {
2210
2211 unsigned long flags2;
2212 struct sas_task *task = isci_request_access_task(
2213 isci_request);
2214
2215 spin_lock_irqsave(&task->task_state_lock, flags2);
2216 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
2217 spin_unlock_irqrestore(&task->task_state_lock, flags2);
2218 }
2219 }
Dan Williams4393aa42011-03-31 13:10:44 -07002220 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002221}