blob: d8968f32ad9af3198bdc605941435b673fa9b81c [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
249
250enum sci_status scic_remote_device_reset_complete(
251 struct scic_sds_remote_device *sci_dev)
252{
253 return sci_dev->state_handlers->reset_complete_handler(sci_dev);
254}
255
Dan Williams88f3b622011-04-22 19:18:03 -0700256#define SCIC_SDS_REMOTE_DEVICE_MINIMUM_TIMER_COUNT (0)
257#define SCIC_SDS_REMOTE_DEVICE_MAXIMUM_TIMER_COUNT (SCI_MAX_REMOTE_DEVICES)
258
Dan Williams88f3b622011-04-22 19:18:03 -0700259/**
260 *
261 * @sci_dev: The remote device for which the suspend is being requested.
262 *
263 * This method invokes the remote device suspend state handler. enum sci_status
264 */
265enum sci_status scic_sds_remote_device_suspend(
266 struct scic_sds_remote_device *sci_dev,
267 u32 suspend_type)
268{
269 return sci_dev->state_handlers->suspend_handler(sci_dev, suspend_type);
270}
271
272/**
273 *
274 * @sci_dev: The remote device for which the event handling is being
275 * requested.
276 * @frame_index: This is the frame index that is being processed.
277 *
278 * This method invokes the frame handler for the remote device state machine
279 * enum sci_status
280 */
281enum sci_status scic_sds_remote_device_frame_handler(
282 struct scic_sds_remote_device *sci_dev,
283 u32 frame_index)
284{
285 return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
286}
287
288/**
289 *
290 * @sci_dev: The remote device for which the event handling is being
291 * requested.
292 * @event_code: This is the event code that is to be processed.
293 *
294 * This method invokes the remote device event handler. enum sci_status
295 */
296enum sci_status scic_sds_remote_device_event_handler(
297 struct scic_sds_remote_device *sci_dev,
298 u32 event_code)
299{
300 return sci_dev->state_handlers->event_handler(sci_dev, event_code);
301}
302
303/**
304 *
305 * @controller: The controller that is starting the io request.
306 * @sci_dev: The remote device for which the start io handling is being
307 * requested.
308 * @io_request: The io request that is being started.
309 *
310 * This method invokes the remote device start io handler. enum sci_status
311 */
312enum sci_status scic_sds_remote_device_start_io(
313 struct scic_sds_controller *controller,
314 struct scic_sds_remote_device *sci_dev,
315 struct scic_sds_request *io_request)
316{
317 return sci_dev->state_handlers->start_io_handler(
318 sci_dev, io_request);
319}
320
321/**
322 *
323 * @controller: The controller that is completing the io request.
324 * @sci_dev: The remote device for which the complete io handling is being
325 * requested.
326 * @io_request: The io request that is being completed.
327 *
328 * This method invokes the remote device complete io handler. enum sci_status
329 */
330enum sci_status scic_sds_remote_device_complete_io(
331 struct scic_sds_controller *controller,
332 struct scic_sds_remote_device *sci_dev,
333 struct scic_sds_request *io_request)
334{
335 return sci_dev->state_handlers->complete_io_handler(
336 sci_dev, io_request);
337}
338
339/**
340 *
341 * @controller: The controller that is starting the task request.
342 * @sci_dev: The remote device for which the start task handling is being
343 * requested.
344 * @io_request: The task request that is being started.
345 *
346 * This method invokes the remote device start task handler. enum sci_status
347 */
348enum sci_status scic_sds_remote_device_start_task(
349 struct scic_sds_controller *controller,
350 struct scic_sds_remote_device *sci_dev,
351 struct scic_sds_request *io_request)
352{
353 return sci_dev->state_handlers->start_task_handler(
354 sci_dev, io_request);
355}
356
357/**
358 *
Dan Williams88f3b622011-04-22 19:18:03 -0700359 * @sci_dev:
360 * @request:
361 *
362 * This method takes the request and bulids an appropriate SCU context for the
363 * request and then requests the controller to post the request. none
364 */
365void scic_sds_remote_device_post_request(
366 struct scic_sds_remote_device *sci_dev,
367 u32 request)
368{
369 u32 context;
370
371 context = scic_sds_remote_device_build_command_context(sci_dev, request);
372
373 scic_sds_controller_post_request(
374 scic_sds_remote_device_get_controller(sci_dev),
375 context
376 );
377}
378
Dan Williamsab2e8f72011-04-27 16:32:45 -0700379/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700380 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700381 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700382 */
Dan Williamseb229672011-05-01 14:05:57 -0700383static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700384{
Dan Williamsab2e8f72011-04-27 16:32:45 -0700385 struct scic_sds_remote_device *sci_dev = _dev;
386 enum scic_sds_remote_device_states state;
Dan Williams88f3b622011-04-22 19:18:03 -0700387
Dan Williamsab2e8f72011-04-27 16:32:45 -0700388 state = sci_dev->state_machine.current_state_id;
389 switch (state) {
390 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
391 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
392 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
393 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
394 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
395 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
396 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
397 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
398 break;
399 default:
400 /* go 'ready' if we are not already in a ready state */
401 sci_base_state_machine_change_state(&sci_dev->state_machine,
402 SCI_BASE_REMOTE_DEVICE_STATE_READY);
403 break;
Dan Williams88f3b622011-04-22 19:18:03 -0700404 }
405}
406
407/**
408 *
409 * @device: This parameter specifies the device for which the request is being
410 * started.
411 * @request: This parameter specifies the request being started.
412 * @status: This parameter specifies the current start operation status.
413 *
414 * This method will perform the STP request start processing common to IO
415 * requests and task requests of all types. none
416 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700417static void scic_sds_remote_device_start_request(
Dan Williams88f3b622011-04-22 19:18:03 -0700418 struct scic_sds_remote_device *sci_dev,
419 struct scic_sds_request *sci_req,
420 enum sci_status status)
421{
422 /* We still have a fault in starting the io complete it on the port */
423 if (status == SCI_SUCCESS)
424 scic_sds_remote_device_increment_request_count(sci_dev);
425 else{
426 sci_dev->owning_port->state_handlers->complete_io_handler(
427 sci_dev->owning_port, sci_dev, sci_req
428 );
429 }
430}
431
432
433/**
434 *
435 * @request: This parameter specifies the request being continued.
436 *
437 * This method will continue to post tc for a STP request. This method usually
438 * serves as a callback when RNC gets resumed during a task management
439 * sequence. none
440 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700441static void scic_sds_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700442{
443 struct scic_sds_remote_device *sci_dev = dev;
444
445 /* we need to check if this request is still valid to continue. */
446 if (sci_dev->working_request)
447 scic_controller_continue_io(sci_dev->working_request);
448}
449
Dan Williams88f3b622011-04-22 19:18:03 -0700450static enum sci_status
451default_device_handler(struct scic_sds_remote_device *sci_dev,
452 const char *func)
453{
454 dev_warn(scirdev_to_dev(sci_dev),
455 "%s: in wrong state: %d\n", func,
456 sci_base_state_machine_get_state(&sci_dev->state_machine));
457 return SCI_FAILURE_INVALID_STATE;
458}
459
Dan Williamsab2e8f72011-04-27 16:32:45 -0700460static enum sci_status scic_sds_remote_device_default_reset_complete_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700461 struct scic_sds_remote_device *sci_dev)
462{
463 return default_device_handler(sci_dev, __func__);
464}
465
Dan Williamsab2e8f72011-04-27 16:32:45 -0700466static enum sci_status scic_sds_remote_device_default_suspend_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700467 struct scic_sds_remote_device *sci_dev, u32 suspend_type)
468{
469 return default_device_handler(sci_dev, __func__);
470}
471
Dan Williamsab2e8f72011-04-27 16:32:45 -0700472static enum sci_status scic_sds_remote_device_default_resume_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700473 struct scic_sds_remote_device *sci_dev)
474{
475 return default_device_handler(sci_dev, __func__);
476}
477
478/**
479 *
480 * @device: The struct scic_sds_remote_device which is then cast into a
481 * struct scic_sds_remote_device.
482 * @event_code: The event code that the struct scic_sds_controller wants the device
483 * object to process.
484 *
485 * This method is the default event handler. It will call the RNC state
486 * machine handler for any RNC events otherwise it will log a warning and
487 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
488 */
489static enum sci_status scic_sds_remote_device_core_event_handler(
490 struct scic_sds_remote_device *sci_dev,
491 u32 event_code,
492 bool is_ready_state)
493{
494 enum sci_status status;
495
496 switch (scu_get_event_type(event_code)) {
497 case SCU_EVENT_TYPE_RNC_OPS_MISC:
498 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
499 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
500 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
501 break;
502 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
503
504 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
505 status = SCI_SUCCESS;
506
507 /* Suspend the associated RNC */
508 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
509 SCI_SOFTWARE_SUSPENSION,
510 NULL, NULL);
511
512 dev_dbg(scirdev_to_dev(sci_dev),
513 "%s: device: %p event code: %x: %s\n",
514 __func__, sci_dev, event_code,
515 (is_ready_state)
516 ? "I_T_Nexus_Timeout event"
517 : "I_T_Nexus_Timeout event in wrong state");
518
519 break;
520 }
521 /* Else, fall through and treat as unhandled... */
522
523 default:
524 dev_dbg(scirdev_to_dev(sci_dev),
525 "%s: device: %p event code: %x: %s\n",
526 __func__, sci_dev, event_code,
527 (is_ready_state)
528 ? "unexpected event"
529 : "unexpected event in wrong state");
530 status = SCI_FAILURE_INVALID_STATE;
531 break;
532 }
533
534 return status;
535}
536/**
537 *
538 * @device: The struct scic_sds_remote_device which is then cast into a
539 * struct scic_sds_remote_device.
540 * @event_code: The event code that the struct scic_sds_controller wants the device
541 * object to process.
542 *
543 * This method is the default event handler. It will call the RNC state
544 * machine handler for any RNC events otherwise it will log a warning and
545 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
546 */
547static enum sci_status scic_sds_remote_device_default_event_handler(
548 struct scic_sds_remote_device *sci_dev,
549 u32 event_code)
550{
551 return scic_sds_remote_device_core_event_handler(sci_dev,
552 event_code,
553 false);
554}
555
556/**
557 *
558 * @device: The struct scic_sds_remote_device which is then cast into a
559 * struct scic_sds_remote_device.
560 * @frame_index: The frame index for which the struct scic_sds_controller wants this
561 * device object to process.
562 *
563 * This method is the default unsolicited frame handler. It logs a warning,
564 * releases the frame and returns a failure. enum sci_status
565 * SCI_FAILURE_INVALID_STATE
566 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700567static enum sci_status scic_sds_remote_device_default_frame_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700568 struct scic_sds_remote_device *sci_dev,
569 u32 frame_index)
570{
571 dev_warn(scirdev_to_dev(sci_dev),
572 "%s: SCIC Remote Device requested to handle frame %x "
573 "while in wrong state %d\n",
574 __func__,
575 frame_index,
576 sci_base_state_machine_get_state(
577 &sci_dev->state_machine));
578
579 /* Return the frame back to the controller */
580 scic_sds_controller_release_frame(
581 scic_sds_remote_device_get_controller(sci_dev), frame_index
582 );
583
584 return SCI_FAILURE_INVALID_STATE;
585}
586
Dan Williamsab2e8f72011-04-27 16:32:45 -0700587static enum sci_status scic_sds_remote_device_default_start_request_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700588 struct scic_sds_remote_device *sci_dev,
589 struct scic_sds_request *request)
590{
591 return default_device_handler(sci_dev, __func__);
592}
593
Dan Williamsab2e8f72011-04-27 16:32:45 -0700594static enum sci_status scic_sds_remote_device_default_complete_request_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700595 struct scic_sds_remote_device *sci_dev,
596 struct scic_sds_request *request)
597{
598 return default_device_handler(sci_dev, __func__);
599}
600
Dan Williamsab2e8f72011-04-27 16:32:45 -0700601static enum sci_status scic_sds_remote_device_default_continue_request_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700602 struct scic_sds_remote_device *sci_dev,
603 struct scic_sds_request *request)
604{
605 return default_device_handler(sci_dev, __func__);
606}
607
608/**
609 *
610 * @device: The struct scic_sds_remote_device which is then cast into a
611 * struct scic_sds_remote_device.
612 * @frame_index: The frame index for which the struct scic_sds_controller wants this
613 * device object to process.
614 *
615 * This method is a general ssp frame handler. In most cases the device object
616 * needs to route the unsolicited frame processing to the io request object.
617 * This method decodes the tag for the io request object and routes the
618 * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
619 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700620static enum sci_status scic_sds_remote_device_general_frame_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700621 struct scic_sds_remote_device *sci_dev,
622 u32 frame_index)
623{
624 enum sci_status result;
625 struct sci_ssp_frame_header *frame_header;
626 struct scic_sds_request *io_request;
627
628 result = scic_sds_unsolicited_frame_control_get_header(
629 &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
630 frame_index,
631 (void **)&frame_header
632 );
633
634 if (SCI_SUCCESS == result) {
635 io_request = scic_sds_controller_get_io_request_from_tag(
636 scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
637
638 if ((io_request == NULL)
639 || (io_request->target_device != sci_dev)) {
640 /*
641 * We could not map this tag to a valid IO request
642 * Just toss the frame and continue */
643 scic_sds_controller_release_frame(
644 scic_sds_remote_device_get_controller(sci_dev), frame_index
645 );
646 } else {
647 /* The IO request is now in charge of releasing the frame */
648 result = io_request->state_handlers->frame_handler(
649 io_request, frame_index);
650 }
651 }
652
653 return result;
654}
655
656/**
657 *
658 * @[in]: sci_dev This is the device object that is receiving the event.
659 * @[in]: event_code The event code to process.
660 *
661 * This is a common method for handling events reported to the remote device
662 * from the controller object. enum sci_status
663 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700664static enum sci_status scic_sds_remote_device_general_event_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700665 struct scic_sds_remote_device *sci_dev,
666 u32 event_code)
667{
668 return scic_sds_remote_device_core_event_handler(sci_dev,
669 event_code,
670 true);
671}
672
Dan Williams88f3b622011-04-22 19:18:03 -0700673/*
674 * This method will attempt to start a task request for this device object. The
675 * remote device object will issue the start request for the task and if
676 * successful it will start the request for the port object then increment its
677 * own requet count. enum sci_status SCI_SUCCESS if the task request is started for
678 * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
679 * object could not get the resources to start.
680 */
681static enum sci_status scic_sds_remote_device_ready_state_start_task_handler(
682 struct scic_sds_remote_device *sci_dev,
683 struct scic_sds_request *request)
684{
685 enum sci_status result;
686
687 /* See if the port is in a state where we can start the IO request */
688 result = scic_sds_port_start_io(
689 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
690
691 if (result == SCI_SUCCESS) {
692 result = scic_sds_remote_node_context_start_task(&sci_dev->rnc,
693 request);
694 if (result == SCI_SUCCESS)
695 result = scic_sds_request_start(request);
696
697 scic_sds_remote_device_start_request(sci_dev, request, result);
698 }
699
700 return result;
701}
702
703/*
704 * This method will attempt to start an io request for this device object. The
705 * remote device object will issue the start request for the io and if
706 * successful it will start the request for the port object then increment its
707 * own requet count. enum sci_status SCI_SUCCESS if the io request is started for
708 * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
709 * object could not get the resources to start.
710 */
711static enum sci_status scic_sds_remote_device_ready_state_start_io_handler(
712 struct scic_sds_remote_device *sci_dev,
713 struct scic_sds_request *request)
714{
715 enum sci_status result;
716
717 /* See if the port is in a state where we can start the IO request */
718 result = scic_sds_port_start_io(
719 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
720
721 if (result == SCI_SUCCESS) {
722 result = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
723 if (result == SCI_SUCCESS)
724 result = scic_sds_request_start(request);
725
726 scic_sds_remote_device_start_request(sci_dev, request, result);
727 }
728
729 return result;
730}
731
732/*
733 * This method will complete the request for the remote device object. The
734 * method will call the completion handler for the request object and if
735 * successful it will complete the request on the port object then decrement
736 * its own started_request_count. enum sci_status
737 */
738static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
739 struct scic_sds_remote_device *sci_dev,
740 struct scic_sds_request *request)
741{
742 enum sci_status result;
743
744 result = scic_sds_request_complete(request);
745
746 if (result != SCI_SUCCESS)
747 return result;
748
749 /* See if the port is in a state
750 * where we can start the IO request */
751 result = scic_sds_port_complete_io(
752 scic_sds_remote_device_get_port(sci_dev),
753 sci_dev, request);
754
755 if (result == SCI_SUCCESS)
756 scic_sds_remote_device_decrement_request_count(sci_dev);
757
758 return result;
759}
760
Dan Williams88f3b622011-04-22 19:18:03 -0700761/**
762 *
763 * @device: The device object for which the request is completing.
764 * @request: The task request that is being completed.
765 *
766 * This method completes requests for this struct scic_sds_remote_device while it is
767 * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
768 * complete method for the request object and if that is successful the port
769 * object is called to complete the task request. Then the device object itself
770 * completes the task request. If struct scic_sds_remote_device started_request_count
771 * goes to 0 and the invalidate RNC request has completed the device object can
772 * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
773 */
774static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
775 struct scic_sds_remote_device *sci_dev,
776 struct scic_sds_request *request)
777{
778 enum sci_status status = SCI_SUCCESS;
779
780 status = scic_sds_request_complete(request);
781
782 if (status != SCI_SUCCESS)
783 return status;
784
785 status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
786 sci_dev, request);
787 if (status != SCI_SUCCESS)
788 return status;
789
790 scic_sds_remote_device_decrement_request_count(sci_dev);
791
792 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
793 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
Dan Williamsec575662011-05-01 14:19:25 -0700794 rnc_destruct_done, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700795 return SCI_SUCCESS;
796}
797
Dan Williams88f3b622011-04-22 19:18:03 -0700798static enum sci_status scic_sds_remote_device_resetting_state_reset_complete_handler(
799 struct scic_sds_remote_device *sci_dev)
800{
Dan Williamsab2e8f72011-04-27 16:32:45 -0700801 sci_base_state_machine_change_state(&sci_dev->state_machine,
802 SCI_BASE_REMOTE_DEVICE_STATE_READY);
Dan Williams88f3b622011-04-22 19:18:03 -0700803
804 return SCI_SUCCESS;
805}
806
Dan Williamsab2e8f72011-04-27 16:32:45 -0700807/* complete requests for this device while it is in the
808 * SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the complete
809 * method for the request object and if that is successful the port object is
810 * called to complete the task request. Then the device object itself completes
811 * the task request. enum sci_status
Dan Williams88f3b622011-04-22 19:18:03 -0700812 */
813static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
814 struct scic_sds_remote_device *sci_dev,
815 struct scic_sds_request *request)
816{
817 enum sci_status status = SCI_SUCCESS;
818
819 status = scic_sds_request_complete(request);
820
821 if (status == SCI_SUCCESS) {
822 status = scic_sds_port_complete_io(
823 scic_sds_remote_device_get_port(sci_dev),
824 sci_dev, request);
825
826 if (status == SCI_SUCCESS) {
827 scic_sds_remote_device_decrement_request_count(sci_dev);
828 }
829 }
830
831 return status;
832}
833
Dan Williamsab2e8f72011-04-27 16:32:45 -0700834static enum sci_status scic_sds_stp_remote_device_complete_request(struct scic_sds_remote_device *sci_dev,
835 struct scic_sds_request *sci_req)
836{
837 enum sci_status status;
838
839 status = scic_sds_io_request_complete(sci_req);
840 if (status != SCI_SUCCESS)
841 goto out;
842
843 status = scic_sds_port_complete_io(sci_dev->owning_port, sci_dev, sci_req);
844 if (status != SCI_SUCCESS)
845 goto out;
846
847 scic_sds_remote_device_decrement_request_count(sci_dev);
848 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
849 /* This request causes hardware error, device needs to be Lun Reset.
850 * So here we force the state machine to IDLE state so the rest IOs
851 * can reach RNC state handler, these IOs will be completed by RNC with
852 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
853 */
854 sci_base_state_machine_change_state(&sci_dev->state_machine,
855 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
856 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
857 sci_base_state_machine_change_state(&sci_dev->state_machine,
858 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
859
860
861 out:
862 if (status != SCI_SUCCESS)
863 dev_err(scirdev_to_dev(sci_dev),
864 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
865 "could not complete\n", __func__, sci_dev->owning_port,
866 sci_dev, sci_req, status);
867
868 return status;
869}
870
871/* scic_sds_stp_remote_device_ready_substate_start_request_handler - start stp
872 * @device: The target device a task management request towards to.
873 * @request: The task request.
874 *
875 * This is the READY NCQ substate handler to start task management request. In
876 * this routine, we suspend and resume the RNC. enum sci_status Always return
877 * SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS status to let
878 * controller_start_task_handler know that the controller can't post TC for
879 * task request yet, instead, when RNC gets resumed, a controller_continue_task
880 * callback will be called.
881 */
882static enum sci_status scic_sds_stp_remote_device_ready_substate_start_request_handler(
883 struct scic_sds_remote_device *device,
884 struct scic_sds_request *request)
885{
886 enum sci_status status;
887
888 /* Will the port allow the io request to start? */
889 status = device->owning_port->state_handlers->start_io_handler(
890 device->owning_port, device, request);
891 if (status != SCI_SUCCESS)
892 return status;
893
894 status = scic_sds_remote_node_context_start_task(&device->rnc, request);
895 if (status != SCI_SUCCESS)
896 goto out;
897
898 status = request->state_handlers->start_handler(request);
899 if (status != SCI_SUCCESS)
900 goto out;
901
902 /*
903 * Note: If the remote device state is not IDLE this will replace
904 * the request that probably resulted in the task management request.
905 */
906 device->working_request = request;
907 sci_base_state_machine_change_state(&device->state_machine,
908 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
909
910 /*
911 * The remote node context must cleanup the TCi to NCQ mapping table.
912 * The only way to do this correctly is to either write to the TLCR
913 * register or to invalidate and repost the RNC. In either case the
914 * remote node context state machine will take the correct action when
915 * the remote node context is suspended and later resumed.
916 */
917 scic_sds_remote_node_context_suspend(&device->rnc,
918 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
919 scic_sds_remote_node_context_resume(&device->rnc,
920 scic_sds_remote_device_continue_request,
921 device);
922
923out:
924 scic_sds_remote_device_start_request(device, request, status);
925 /*
926 * We need to let the controller start request handler know that it can't
927 * post TC yet. We will provide a callback function to post TC when RNC gets
928 * resumed.
929 */
930 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
931}
932
933/* handle the start io operation for a sata device that is in the command idle
934 * state. - Evalute the type of IO request to be started - If its an NCQ
935 * request change to NCQ substate - If its any other command change to the CMD
936 * substate
937 *
938 * If this is a softreset we may want to have a different substate.
939 */
940static enum sci_status scic_sds_stp_remote_device_ready_idle_substate_start_io_handler(
941 struct scic_sds_remote_device *sci_dev,
942 struct scic_sds_request *request)
943{
944 enum sci_status status;
945 struct isci_request *isci_request = request->ireq;
946 enum scic_sds_remote_device_states new_state;
947
948 /* Will the port allow the io request to start? */
949 status = sci_dev->owning_port->state_handlers->start_io_handler(
950 sci_dev->owning_port, sci_dev, request);
951 if (status != SCI_SUCCESS)
952 return status;
953
954 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
955 if (status != SCI_SUCCESS)
956 goto out;
957
958 status = request->state_handlers->start_handler(request);
959 if (status != SCI_SUCCESS)
960 goto out;
961
962 if (isci_sata_get_sat_protocol(isci_request) == SAT_PROTOCOL_FPDMA)
963 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
964 else {
965 sci_dev->working_request = request;
966 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
967 }
968 sci_base_state_machine_change_state(&sci_dev->state_machine, new_state);
969out:
970 scic_sds_remote_device_start_request(sci_dev, request, status);
971 return status;
972}
973
974
975static enum sci_status scic_sds_stp_remote_device_ready_idle_substate_event_handler(
976 struct scic_sds_remote_device *sci_dev,
977 u32 event_code)
978{
979 enum sci_status status;
980
981 status = scic_sds_remote_device_general_event_handler(sci_dev, event_code);
982 if (status != SCI_SUCCESS)
983 return status;
984
985 /* We pick up suspension events to handle specifically to this state. We
986 * resume the RNC right away. enum sci_status
987 */
988 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
989 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
990 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
991
992 return status;
993}
994
995static enum sci_status scic_sds_stp_remote_device_ready_ncq_substate_start_io_handler(
996 struct scic_sds_remote_device *sci_dev,
997 struct scic_sds_request *request)
998{
999 enum sci_status status;
1000 struct isci_request *isci_request = request->ireq;
1001 scic_sds_port_io_request_handler_t start_io;
1002
1003 if (isci_sata_get_sat_protocol(isci_request) == SAT_PROTOCOL_FPDMA) {
1004 start_io = sci_dev->owning_port->state_handlers->start_io_handler;
1005 status = start_io(sci_dev->owning_port, sci_dev, request);
1006 if (status != SCI_SUCCESS)
1007 return status;
1008
1009 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
Dan Williamsf619fff2011-05-01 10:13:04 -07001010 if (status == SCI_SUCCESS)
1011 status = request->state_handlers->start_handler(request);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001012
1013 scic_sds_remote_device_start_request(sci_dev, request, status);
1014 } else
1015 status = SCI_FAILURE_INVALID_STATE;
1016
1017 return status;
1018}
1019
1020static enum sci_status scic_sds_stp_remote_device_ready_ncq_substate_frame_handler(struct scic_sds_remote_device *sci_dev,
1021 u32 frame_index)
1022{
1023 enum sci_status status;
1024 struct sata_fis_header *frame_header;
1025 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1026
1027 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1028 frame_index,
1029 (void **)&frame_header);
1030 if (status != SCI_SUCCESS)
1031 return status;
1032
1033 if (frame_header->fis_type == SATA_FIS_TYPE_SETDEVBITS &&
1034 (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
1035 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
1036
1037 /* TODO Check sactive and complete associated IO if any. */
1038
1039 sci_base_state_machine_change_state(&sci_dev->state_machine,
1040 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1041 } else if (frame_header->fis_type == SATA_FIS_TYPE_REGD2H &&
1042 (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
1043 /*
1044 * Some devices return D2H FIS when an NCQ error is detected.
1045 * Treat this like an SDB error FIS ready reason.
1046 */
1047 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
1048
1049 sci_base_state_machine_change_state(&sci_dev->state_machine,
1050 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1051 } else
1052 status = SCI_FAILURE;
1053
1054 scic_sds_controller_release_frame(scic, frame_index);
1055
1056 return status;
1057}
1058
1059static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_start_io_handler(
1060 struct scic_sds_remote_device *device,
1061 struct scic_sds_request *request)
1062{
1063 /* device is already handling a command it can not accept new commands
1064 * until this one is complete.
1065 */
1066 return SCI_FAILURE_INVALID_STATE;
1067}
1068
1069static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler(
1070 struct scic_sds_remote_device *sci_dev,
1071 u32 suspend_type)
1072{
1073 enum sci_status status;
1074
1075 status = scic_sds_remote_node_context_suspend(&sci_dev->rnc,
1076 suspend_type, NULL, NULL);
1077
1078 return status;
1079}
1080
1081static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_frame_handler(
1082 struct scic_sds_remote_device *sci_dev,
1083 u32 frame_index)
1084{
1085 /* The device doe not process any UF received from the hardware while
1086 * in this state. All unsolicited frames are forwarded to the io
1087 * request object.
1088 */
1089 return scic_sds_io_request_frame_handler(sci_dev->working_request,
1090 frame_index);
1091}
1092
1093static enum sci_status scic_sds_stp_remote_device_ready_await_reset_substate_start_io_handler(
1094 struct scic_sds_remote_device *device,
1095 struct scic_sds_request *request)
1096{
1097 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
1098}
1099
1100static enum sci_status scic_sds_stp_remote_device_ready_await_reset_substate_complete_request_handler(
1101 struct scic_sds_remote_device *device,
1102 struct scic_sds_request *request)
1103{
1104 struct scic_sds_request *sci_req = request;
1105 enum sci_status status;
1106
1107 status = scic_sds_io_request_complete(sci_req);
1108 if (status != SCI_SUCCESS)
1109 goto out;
1110
1111 status = scic_sds_port_complete_io(device->owning_port, device, sci_req);
1112 if (status != SCI_SUCCESS)
1113 goto out;
1114
1115 scic_sds_remote_device_decrement_request_count(device);
1116 out:
1117 if (status != SCI_SUCCESS)
1118 dev_err(scirdev_to_dev(device),
1119 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
1120 "could not complete\n",
1121 __func__, device->owning_port, device, sci_req, status);
1122
1123 return status;
1124}
1125
1126static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
1127{
1128 struct scic_sds_remote_device *sci_dev = _dev;
1129 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1130 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1131
1132 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
1133 * As a result, avoid sending the ready notification.
1134 */
1135 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
1136 isci_remote_device_ready(scic->ihost, idev);
1137}
1138
1139static enum sci_status scic_sds_smp_remote_device_ready_idle_substate_start_io_handler(
1140 struct scic_sds_remote_device *sci_dev,
1141 struct scic_sds_request *sci_req)
1142{
1143 enum sci_status status;
1144
1145 /* Will the port allow the io request to start? */
1146 status = sci_dev->owning_port->state_handlers->start_io_handler(
1147 sci_dev->owning_port, sci_dev, sci_req);
1148 if (status != SCI_SUCCESS)
1149 return status;
1150
1151 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
1152 if (status != SCI_SUCCESS)
Dan Williamsf619fff2011-05-01 10:13:04 -07001153 goto out;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001154
1155 status = scic_sds_request_start(sci_req);
1156 if (status != SCI_SUCCESS)
Dan Williamsf619fff2011-05-01 10:13:04 -07001157 goto out;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001158
1159 sci_dev->working_request = sci_req;
1160 sci_base_state_machine_change_state(&sci_dev->state_machine,
1161 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1162
Dan Williamsf619fff2011-05-01 10:13:04 -07001163 out:
Dan Williamsab2e8f72011-04-27 16:32:45 -07001164 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
1165
1166 return status;
1167}
1168
1169static enum sci_status scic_sds_smp_remote_device_ready_cmd_substate_start_io_handler(
1170 struct scic_sds_remote_device *device,
1171 struct scic_sds_request *request)
1172{
1173 /* device is already handling a command it can not accept new commands
1174 * until this one is complete.
1175 */
1176 return SCI_FAILURE_INVALID_STATE;
1177}
1178
1179static enum sci_status
1180scic_sds_smp_remote_device_ready_cmd_substate_complete_io_handler(struct scic_sds_remote_device *sci_dev,
1181 struct scic_sds_request *sci_req)
1182{
1183 enum sci_status status;
1184
1185 status = scic_sds_io_request_complete(sci_req);
1186 if (status != SCI_SUCCESS)
1187 return status;
1188
1189 status = scic_sds_port_complete_io(sci_dev->owning_port, sci_dev, sci_req);
1190 if (status != SCI_SUCCESS) {
1191 dev_err(scirdev_to_dev(sci_dev),
1192 "%s: SCIC SDS Remote Device 0x%p io request "
1193 "0x%p could not be completd on the port 0x%p "
1194 "failed with status %d.\n", __func__, sci_dev, sci_req,
1195 sci_dev->owning_port, status);
1196 return status;
1197 }
1198
1199 scic_sds_remote_device_decrement_request_count(sci_dev);
1200 sci_base_state_machine_change_state(&sci_dev->state_machine,
1201 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1202
1203 return status;
1204}
1205
1206static enum sci_status scic_sds_smp_remote_device_ready_cmd_substate_frame_handler(
1207 struct scic_sds_remote_device *sci_dev,
1208 u32 frame_index)
1209{
1210 enum sci_status status;
1211
1212 /* The device does not process any UF received from the hardware while
1213 * in this state. All unsolicited frames are forwarded to the io request
1214 * object.
1215 */
1216 status = scic_sds_io_request_frame_handler(
1217 sci_dev->working_request,
1218 frame_index
1219 );
1220
1221 return status;
1222}
1223
Dan Williams88f3b622011-04-22 19:18:03 -07001224static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
1225 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001226 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1227 .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_STOPPED] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001238 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1239 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1240 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1241 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1242 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1243 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1244 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1245 .resume_handler = scic_sds_remote_device_default_resume_handler,
1246 .event_handler = scic_sds_remote_device_default_event_handler,
1247 .frame_handler = scic_sds_remote_device_default_frame_handler
1248 },
1249 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001250 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1251 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1252 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1253 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1254 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1255 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1256 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1257 .resume_handler = scic_sds_remote_device_default_resume_handler,
1258 .event_handler = scic_sds_remote_device_general_event_handler,
1259 .frame_handler = scic_sds_remote_device_default_frame_handler
1260 },
1261 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001262 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1263 .start_io_handler = scic_sds_remote_device_ready_state_start_io_handler,
1264 .complete_io_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1265 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1266 .start_task_handler = scic_sds_remote_device_ready_state_start_task_handler,
1267 .complete_task_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1268 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1269 .resume_handler = scic_sds_remote_device_default_resume_handler,
1270 .event_handler = scic_sds_remote_device_general_event_handler,
1271 .frame_handler = scic_sds_remote_device_general_frame_handler,
1272 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001273 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001274 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1275 .start_io_handler = scic_sds_stp_remote_device_ready_idle_substate_start_io_handler,
1276 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1277 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1278 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1279 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1280 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1281 .resume_handler = scic_sds_remote_device_default_resume_handler,
1282 .event_handler = scic_sds_stp_remote_device_ready_idle_substate_event_handler,
1283 .frame_handler = scic_sds_remote_device_default_frame_handler
1284 },
1285 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001286 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1287 .start_io_handler = scic_sds_stp_remote_device_ready_cmd_substate_start_io_handler,
1288 .complete_io_handler = scic_sds_stp_remote_device_complete_request,
1289 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1290 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1291 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1292 .suspend_handler = scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler,
1293 .resume_handler = scic_sds_remote_device_default_resume_handler,
1294 .event_handler = scic_sds_remote_device_general_event_handler,
1295 .frame_handler = scic_sds_stp_remote_device_ready_cmd_substate_frame_handler
1296 },
1297 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001298 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1299 .start_io_handler = scic_sds_stp_remote_device_ready_ncq_substate_start_io_handler,
1300 .complete_io_handler = scic_sds_stp_remote_device_complete_request,
1301 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1302 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1303 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1304 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1305 .resume_handler = scic_sds_remote_device_default_resume_handler,
1306 .event_handler = scic_sds_remote_device_general_event_handler,
1307 .frame_handler = scic_sds_stp_remote_device_ready_ncq_substate_frame_handler
1308 },
1309 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001310 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1311 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1312 .complete_io_handler = scic_sds_stp_remote_device_complete_request,
1313 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1314 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1315 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1316 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1317 .resume_handler = scic_sds_remote_device_default_resume_handler,
1318 .event_handler = scic_sds_remote_device_general_event_handler,
1319 .frame_handler = scic_sds_remote_device_general_frame_handler
1320 },
1321 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001322 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1323 .start_io_handler = scic_sds_stp_remote_device_ready_await_reset_substate_start_io_handler,
1324 .complete_io_handler = scic_sds_stp_remote_device_ready_await_reset_substate_complete_request_handler,
1325 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1326 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1327 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1328 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1329 .resume_handler = scic_sds_remote_device_default_resume_handler,
1330 .event_handler = scic_sds_remote_device_general_event_handler,
1331 .frame_handler = scic_sds_remote_device_general_frame_handler
1332 },
1333 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001334 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1335 .start_io_handler = scic_sds_smp_remote_device_ready_idle_substate_start_io_handler,
1336 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1337 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1338 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1339 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1340 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1341 .resume_handler = scic_sds_remote_device_default_resume_handler,
1342 .event_handler = scic_sds_remote_device_general_event_handler,
1343 .frame_handler = scic_sds_remote_device_default_frame_handler
1344 },
1345 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001346 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1347 .start_io_handler = scic_sds_smp_remote_device_ready_cmd_substate_start_io_handler,
1348 .complete_io_handler = scic_sds_smp_remote_device_ready_cmd_substate_complete_io_handler,
1349 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1350 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1351 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1352 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1353 .resume_handler = scic_sds_remote_device_default_resume_handler,
1354 .event_handler = scic_sds_remote_device_general_event_handler,
1355 .frame_handler = scic_sds_smp_remote_device_ready_cmd_substate_frame_handler
1356 },
Dan Williams88f3b622011-04-22 19:18:03 -07001357 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001358 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1359 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1360 .complete_io_handler = scic_sds_remote_device_stopping_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_stopping_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_general_event_handler,
1367 .frame_handler = scic_sds_remote_device_general_frame_handler
1368 },
1369 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001370 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1371 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1372 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1373 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1374 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1375 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1376 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1377 .resume_handler = scic_sds_remote_device_default_resume_handler,
1378 .event_handler = scic_sds_remote_device_default_event_handler,
1379 .frame_handler = scic_sds_remote_device_general_frame_handler
1380 },
1381 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001382 .reset_complete_handler = scic_sds_remote_device_resetting_state_reset_complete_handler,
1383 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1384 .complete_io_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1385 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1386 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1387 .complete_task_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1388 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1389 .resume_handler = scic_sds_remote_device_default_resume_handler,
1390 .event_handler = scic_sds_remote_device_default_event_handler,
1391 .frame_handler = scic_sds_remote_device_general_frame_handler
1392 },
1393 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001394 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1395 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1396 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1397 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1398 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1399 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1400 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1401 .resume_handler = scic_sds_remote_device_default_resume_handler,
1402 .event_handler = scic_sds_remote_device_default_event_handler,
1403 .frame_handler = scic_sds_remote_device_default_frame_handler
1404 }
1405};
1406
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001407static void scic_sds_remote_device_initial_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001408{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001409 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001410
Dan Williams88f3b622011-04-22 19:18:03 -07001411 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1412 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1413
1414 /* Initial state is a transitional state to the stopped state */
1415 sci_base_state_machine_change_state(&sci_dev->state_machine,
1416 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1417}
1418
1419/**
Dan Williams88f3b622011-04-22 19:18:03 -07001420 * scic_remote_device_destruct() - free remote node context and destruct
1421 * @remote_device: This parameter specifies the remote device to be destructed.
1422 *
1423 * Remote device objects are a limited resource. As such, they must be
1424 * protected. Thus calls to construct and destruct are mutually exclusive and
1425 * non-reentrant. The return value shall indicate if the device was
1426 * successfully destructed or if some failure occurred. enum sci_status This value
1427 * is returned if the device is successfully destructed.
1428 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
1429 * device isn't valid (e.g. it's already been destoryed, the handle isn't
1430 * valid, etc.).
1431 */
1432static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
1433{
Dan Williamsb8d82f62011-05-01 14:38:26 -07001434 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1435 enum scic_sds_remote_device_states state = sm->current_state_id;
1436 struct scic_sds_controller *scic;
1437
1438 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1439 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1440 __func__, state);
1441 return SCI_FAILURE_INVALID_STATE;
1442 }
1443
1444 scic = sci_dev->owning_port->owning_controller;
1445 scic_sds_controller_free_remote_node_context(scic, sci_dev,
1446 sci_dev->rnc.remote_node_index);
1447 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
1448 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
1449
1450 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001451}
1452
Dan Williams6f231dd2011-07-02 22:56:22 -07001453/**
1454 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -08001455 * @ihost: This parameter specifies the isci host object.
1456 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -07001457 *
1458 */
Dan Williamsd9c37392011-03-03 17:59:32 -08001459static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001460{
Dan Williamsd9c37392011-03-03 17:59:32 -08001461 dev_dbg(&ihost->pdev->dev,
1462 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001463
1464 /* There should not be any outstanding io's. All paths to
1465 * here should go through isci_remote_device_nuke_requests.
1466 * If we hit this condition, we will need a way to complete
1467 * io requests in process */
Dan Williamsd9c37392011-03-03 17:59:32 -08001468 while (!list_empty(&idev->reqs_in_process)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001469
Dan Williamsd9c37392011-03-03 17:59:32 -08001470 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001471 "%s: ** request list not empty! **\n", __func__);
1472 BUG();
1473 }
1474
Dan Williams57f20f42011-04-21 18:14:45 -07001475 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -08001476 idev->domain_dev->lldd_dev = NULL;
1477 idev->domain_dev = NULL;
1478 idev->isci_port = NULL;
1479 list_del_init(&idev->node);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001480
Dan Williamsd9c37392011-03-03 17:59:32 -08001481 clear_bit(IDEV_START_PENDING, &idev->flags);
1482 clear_bit(IDEV_STOP_PENDING, &idev->flags);
1483 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001484}
1485
Dan Williams88f3b622011-04-22 19:18:03 -07001486/**
1487 * isci_remote_device_stop_complete() - This function is called by the scic
1488 * when the remote device stop has completed. We mark the isci device as not
1489 * ready and remove the isci remote device.
1490 * @ihost: This parameter specifies the isci host object.
1491 * @idev: This parameter specifies the remote device.
1492 * @status: This parameter specifies status of the completion.
1493 *
1494 */
1495static void isci_remote_device_stop_complete(struct isci_host *ihost,
1496 struct isci_remote_device *idev)
1497{
1498 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1499
1500 isci_remote_device_change_state(idev, isci_stopped);
1501
1502 /* after stop, we can tear down resources. */
1503 isci_remote_device_deconstruct(ihost, idev);
1504}
1505
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001506static void scic_sds_remote_device_stopped_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;
1510 struct isci_remote_device *idev;
1511 struct isci_host *ihost;
1512 u32 prev_state;
1513
Dan Williams88f3b622011-04-22 19:18:03 -07001514 scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +00001515 ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001516 idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001517
1518 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1519 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1520
1521 /* If we are entering from the stopping state let the SCI User know that
1522 * the stop operation has completed.
1523 */
1524 prev_state = sci_dev->state_machine.previous_state_id;
1525 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1526 isci_remote_device_stop_complete(ihost, idev);
1527
1528 scic_sds_controller_remote_device_stopped(scic, sci_dev);
1529}
1530
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001531static void scic_sds_remote_device_starting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001532{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001533 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001534 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +00001535 struct isci_host *ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001536 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001537
1538 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1539 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1540
1541 isci_remote_device_not_ready(ihost, idev,
1542 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1543}
1544
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001545static void scic_sds_remote_device_ready_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001546{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001547 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001548 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1549 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001550
1551 SET_STATE_HANDLER(sci_dev,
1552 scic_sds_remote_device_state_handler_table,
1553 SCI_BASE_REMOTE_DEVICE_STATE_READY);
1554
1555 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1556
Dan Williamsab2e8f72011-04-27 16:32:45 -07001557 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
1558 sci_base_state_machine_change_state(&sci_dev->state_machine,
1559 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1560 } else if (dev_is_expander(dev)) {
1561 sci_base_state_machine_change_state(&sci_dev->state_machine,
1562 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1563 } else
1564 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
Dan Williams88f3b622011-04-22 19:18:03 -07001565}
1566
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001567static void scic_sds_remote_device_ready_state_exit(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 Williamsab2e8f72011-04-27 16:32:45 -07001570 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1571
1572 if (dev->dev_type == SAS_END_DEV) {
1573 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001574 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001575
Dan Williamsab2e8f72011-04-27 16:32:45 -07001576 isci_remote_device_not_ready(scic->ihost, idev,
Dan Williams88f3b622011-04-22 19:18:03 -07001577 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1578 }
1579}
1580
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001581static void scic_sds_remote_device_stopping_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001582{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001583 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001584
1585 SET_STATE_HANDLER(
1586 sci_dev,
1587 scic_sds_remote_device_state_handler_table,
1588 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1589 );
1590}
1591
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001592static void scic_sds_remote_device_failed_state_enter(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 SET_STATE_HANDLER(
1597 sci_dev,
1598 scic_sds_remote_device_state_handler_table,
1599 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1600 );
1601}
1602
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001603static void scic_sds_remote_device_resetting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001604{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001605 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001606
1607 SET_STATE_HANDLER(
1608 sci_dev,
1609 scic_sds_remote_device_state_handler_table,
1610 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1611 );
1612
1613 scic_sds_remote_node_context_suspend(
1614 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1615}
1616
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001617static void scic_sds_remote_device_resetting_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001618{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001619 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001620
1621 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1622}
1623
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001624static void scic_sds_remote_device_final_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001625{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001626 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001627
1628 SET_STATE_HANDLER(
1629 sci_dev,
1630 scic_sds_remote_device_state_handler_table,
1631 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1632 );
1633}
1634
Dan Williamsab2e8f72011-04-27 16:32:45 -07001635static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
1636{
1637 struct scic_sds_remote_device *sci_dev = object;
1638
1639 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1640 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1641
1642 sci_dev->working_request = NULL;
1643 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
1644 /*
1645 * Since the RNC is ready, it's alright to finish completion
1646 * processing (e.g. signal the remote device is ready). */
1647 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
1648 } else {
1649 scic_sds_remote_node_context_resume(&sci_dev->rnc,
1650 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
1651 sci_dev);
1652 }
1653}
1654
1655static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1656{
1657 struct scic_sds_remote_device *sci_dev = object;
1658 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1659
1660 BUG_ON(sci_dev->working_request == NULL);
1661
1662 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1663 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1664
1665 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1666 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1667}
1668
1669static void scic_sds_stp_remote_device_ready_ncq_substate_enter(void *object)
1670{
1671 struct scic_sds_remote_device *sci_dev = object;
1672
1673 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1674 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ);
1675}
1676
1677static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1678{
1679 struct scic_sds_remote_device *sci_dev = object;
1680 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1681 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1682
1683 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1684 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1685
1686 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1687 isci_remote_device_not_ready(scic->ihost, idev,
1688 sci_dev->not_ready_reason);
1689}
1690
1691static void scic_sds_stp_remote_device_ready_await_reset_substate_enter(void *object)
1692{
1693 struct scic_sds_remote_device *sci_dev = object;
1694
1695 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1696 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
1697}
1698
1699static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1700{
1701 struct scic_sds_remote_device *sci_dev = object;
1702 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1703
1704 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1705 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1706
1707 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1708}
1709
1710static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1711{
1712 struct scic_sds_remote_device *sci_dev = object;
1713 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1714
1715 BUG_ON(sci_dev->working_request == NULL);
1716
1717 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1718 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1719
1720 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1721 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1722}
1723
1724static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1725{
1726 struct scic_sds_remote_device *sci_dev = object;
1727
1728 sci_dev->working_request = NULL;
1729}
Dan Williams88f3b622011-04-22 19:18:03 -07001730
1731static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1732 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1733 .enter_state = scic_sds_remote_device_initial_state_enter,
1734 },
1735 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1736 .enter_state = scic_sds_remote_device_stopped_state_enter,
1737 },
1738 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1739 .enter_state = scic_sds_remote_device_starting_state_enter,
1740 },
1741 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1742 .enter_state = scic_sds_remote_device_ready_state_enter,
1743 .exit_state = scic_sds_remote_device_ready_state_exit
1744 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001745 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1746 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1747 },
1748 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1749 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1750 },
1751 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
1752 .enter_state = scic_sds_stp_remote_device_ready_ncq_substate_enter,
1753 },
1754 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1755 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1756 },
1757 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
1758 .enter_state = scic_sds_stp_remote_device_ready_await_reset_substate_enter,
1759 },
1760 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1761 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1762 },
1763 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1764 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1765 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1766 },
Dan Williams88f3b622011-04-22 19:18:03 -07001767 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1768 .enter_state = scic_sds_remote_device_stopping_state_enter,
1769 },
1770 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1771 .enter_state = scic_sds_remote_device_failed_state_enter,
1772 },
1773 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1774 .enter_state = scic_sds_remote_device_resetting_state_enter,
1775 .exit_state = scic_sds_remote_device_resetting_state_exit
1776 },
1777 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1778 .enter_state = scic_sds_remote_device_final_state_enter,
1779 },
1780};
1781
1782/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001783 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001784 * @sci_port: SAS/SATA port through which this device is accessed.
1785 * @sci_dev: remote device to construct
1786 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001787 * This routine just performs benign initialization and does not
1788 * allocate the remote_node_context which is left to
1789 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1790 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001791 */
1792static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1793 struct scic_sds_remote_device *sci_dev)
1794{
1795 sci_dev->owning_port = sci_port;
1796 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001797
1798 sci_base_state_machine_construct(
1799 &sci_dev->state_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001800 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001801 scic_sds_remote_device_state_table,
1802 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1803 );
1804
1805 sci_base_state_machine_start(
1806 &sci_dev->state_machine
1807 );
1808
1809 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1810 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001811}
1812
1813/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001814 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001815 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001816 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1817 * the device is known to the SCI Core since it is contained in the
1818 * scic_phy object. Remote node context(s) is/are a global resource
1819 * allocated by this routine, freed by scic_remote_device_destruct().
1820 *
1821 * Returns:
1822 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1823 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1824 * sata-only controller instance.
1825 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001826 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001827static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1828 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001829{
1830 enum sci_status status;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001831 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001832
Dan Williamsb87ee302011-04-25 11:48:29 -07001833 scic_remote_device_construct(sci_port, sci_dev);
1834
Dan Williams88f3b622011-04-22 19:18:03 -07001835 /*
1836 * This information is request to determine how many remote node context
1837 * entries will be needed to store the remote node.
1838 */
Dan Williams88f3b622011-04-22 19:18:03 -07001839 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001840 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1841 sci_dev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001842 &sci_dev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001843
Dan Williamsa1a113b2011-04-21 18:44:45 -07001844 if (status != SCI_SUCCESS)
1845 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001846
Dan Williamsab2e8f72011-04-27 16:32:45 -07001847 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1848 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1849 /* pass */;
1850 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001851 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001852
Dan Williamsa1a113b2011-04-21 18:44:45 -07001853 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001854
Dan Williamsa1a113b2011-04-21 18:44:45 -07001855 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1856 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001857
Dan Williamsa1a113b2011-04-21 18:44:45 -07001858 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001859}
1860
Dan Williams88f3b622011-04-22 19:18:03 -07001861/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001862 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001863 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001864 * Remote node context(s) is/are a global resource allocated by this
1865 * routine, freed by scic_remote_device_destruct().
1866 *
1867 * Returns:
1868 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1869 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1870 * sata-only controller instance.
1871 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001872 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001873static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001874 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001875{
Dan Williamsa1a113b2011-04-21 18:44:45 -07001876 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001877 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001878
Dan Williamsb87ee302011-04-25 11:48:29 -07001879 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001880
Dan Williamsab2e8f72011-04-27 16:32:45 -07001881 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1882 sci_dev,
1883 &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001884 if (status != SCI_SUCCESS)
1885 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001886
Dan Williamsab2e8f72011-04-27 16:32:45 -07001887 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1888 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1889 /* pass */;
1890 else
1891 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001892
Dan Williamsa1a113b2011-04-21 18:44:45 -07001893 /*
1894 * For SAS-2 the physical link rate is actually a logical link
1895 * rate that incorporates multiplexing. The SCU doesn't
1896 * incorporate multiplexing and for the purposes of the
1897 * connection the logical link rate is that same as the
1898 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1899 * one another, so this code works for both situations. */
1900 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001901 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001902
Dan Williamsa1a113b2011-04-21 18:44:45 -07001903 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1904 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001905
Dan Williamsab2e8f72011-04-27 16:32:45 -07001906 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001907}
1908
1909/**
1910 * scic_remote_device_start() - This method will start the supplied remote
1911 * device. This method enables normal IO requests to flow through to the
1912 * remote device.
1913 * @remote_device: This parameter specifies the device to be started.
1914 * @timeout: This parameter specifies the number of milliseconds in which the
1915 * start operation should complete.
1916 *
1917 * An indication of whether the device was successfully started. SCI_SUCCESS
1918 * This value is returned if the device was successfully started.
1919 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1920 * the device when there have been no phys added to it.
1921 */
1922static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
Dan Williamseb229672011-05-01 14:05:57 -07001923 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001924{
Dan Williamseb229672011-05-01 14:05:57 -07001925 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1926 enum scic_sds_remote_device_states state = sm->current_state_id;
1927 enum sci_status status;
1928
1929 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1930 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1931 __func__, state);
1932 return SCI_FAILURE_INVALID_STATE;
1933 }
1934
1935 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1936 remote_device_resume_done,
1937 sci_dev);
1938 if (status != SCI_SUCCESS)
1939 return status;
1940
1941 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1942
1943 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001944}
Dan Williams6f231dd2011-07-02 22:56:22 -07001945
Dan Williams00d680e2011-04-25 14:29:29 -07001946static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1947 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001948{
Dan Williams00d680e2011-04-25 14:29:29 -07001949 struct scic_sds_port *sci_port = iport->sci_port_handle;
1950 struct isci_host *ihost = iport->isci_host;
1951 struct domain_device *dev = idev->domain_dev;
1952 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001953
Dan Williams00d680e2011-04-25 14:29:29 -07001954 if (dev->parent && dev_is_expander(dev->parent))
1955 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1956 else
1957 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001958
1959 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001960 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1961 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001962
1963 return status;
1964 }
1965
Dan Williams6f231dd2011-07-02 22:56:22 -07001966 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001967 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001968
Dan Williams00d680e2011-04-25 14:29:29 -07001969 if (status != SCI_SUCCESS)
1970 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1971 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001972
1973 return status;
1974}
1975
Dan Williams4393aa42011-03-31 13:10:44 -07001976void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001977{
1978 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001979
Dan Williams4393aa42011-03-31 13:10:44 -07001980 dev_dbg(&ihost->pdev->dev,
1981 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001982
1983 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -07001984 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -07001985
Dan Williams4393aa42011-03-31 13:10:44 -07001986 dev_dbg(&ihost->pdev->dev,
1987 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001988}
1989
Dan Williams6f231dd2011-07-02 22:56:22 -07001990/**
1991 * This function builds the isci_remote_device when a libsas dev_found message
1992 * is received.
1993 * @isci_host: This parameter specifies the isci host object.
1994 * @port: This parameter specifies the isci_port conected to this device.
1995 *
1996 * pointer to new isci_remote_device.
1997 */
1998static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001999isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07002000{
Dan Williamsd9c37392011-03-03 17:59:32 -08002001 struct isci_remote_device *idev;
2002 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07002003
Dan Williamsd9c37392011-03-03 17:59:32 -08002004 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07002005 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08002006 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
2007 break;
2008 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002009
Dan Williamsd9c37392011-03-03 17:59:32 -08002010 if (i >= SCI_MAX_REMOTE_DEVICES) {
2011 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07002012 return NULL;
2013 }
2014
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07002015 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
2016 return NULL;
2017
2018 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
2019 return NULL;
2020
Dan Williamsd9c37392011-03-03 17:59:32 -08002021 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07002022
Dan Williamsd9c37392011-03-03 17:59:32 -08002023 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07002024}
Dan Williams6f231dd2011-07-02 22:56:22 -07002025
2026/**
Dan Williams6f231dd2011-07-02 22:56:22 -07002027 * isci_remote_device_stop() - This function is called internally to stop the
2028 * remote device.
2029 * @isci_host: This parameter specifies the isci host object.
2030 * @isci_device: This parameter specifies the remote device.
2031 *
2032 * The status of the scic request to stop.
2033 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08002034enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002035{
2036 enum sci_status status;
2037 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07002038
Dan Williams6ad31fe2011-03-04 12:10:29 -08002039 dev_dbg(&ihost->pdev->dev,
2040 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002041
Dan Williams6ad31fe2011-03-04 12:10:29 -08002042 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07002043
2044 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07002045 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07002046
Dan Williams6ad31fe2011-03-04 12:10:29 -08002047 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002048
Dan Williams6ad31fe2011-03-04 12:10:29 -08002049 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07002050 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08002051 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002052
2053 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -08002054 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -08002055 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08002056 clear_bit(IDEV_ALLOCATED, &idev->flags);
2057 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002058
Dan Williams6ad31fe2011-03-04 12:10:29 -08002059 dev_dbg(&ihost->pdev->dev,
2060 "%s: idev = %p - after completion wait\n",
2061 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002062
Dan Williams6f231dd2011-07-02 22:56:22 -07002063 return status;
2064}
2065
2066/**
2067 * isci_remote_device_gone() - This function is called by libsas when a domain
2068 * device is removed.
2069 * @domain_device: This parameter specifies the libsas domain device.
2070 *
2071 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08002072void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002073{
Dan Williams4393aa42011-03-31 13:10:44 -07002074 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08002075 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07002076
Dan Williams6ad31fe2011-03-04 12:10:29 -08002077 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002078 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08002079 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07002080
Dan Williams6ad31fe2011-03-04 12:10:29 -08002081 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002082}
2083
2084
2085/**
2086 * isci_remote_device_found() - This function is called by libsas when a remote
2087 * device is discovered. A remote device object is created and started. the
2088 * function then sleeps until the sci core device started message is
2089 * received.
2090 * @domain_device: This parameter specifies the libsas domain device.
2091 *
2092 * status, zero indicates success.
2093 */
2094int isci_remote_device_found(struct domain_device *domain_dev)
2095{
Dan Williams4393aa42011-03-31 13:10:44 -07002096 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002097 struct isci_port *isci_port;
2098 struct isci_phy *isci_phy;
2099 struct asd_sas_port *sas_port;
2100 struct asd_sas_phy *sas_phy;
2101 struct isci_remote_device *isci_device;
2102 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07002103
Dan Williams6f231dd2011-07-02 22:56:22 -07002104 dev_dbg(&isci_host->pdev->dev,
2105 "%s: domain_device = %p\n", __func__, domain_dev);
2106
Dan Williams0cf89d12011-02-18 09:25:07 -08002107 wait_for_start(isci_host);
2108
Dan Williams6f231dd2011-07-02 22:56:22 -07002109 sas_port = domain_dev->port;
2110 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
2111 port_phy_el);
2112 isci_phy = to_isci_phy(sas_phy);
2113 isci_port = isci_phy->isci_port;
2114
2115 /* we are being called for a device on this port,
2116 * so it has to come up eventually
2117 */
2118 wait_for_completion(&isci_port->start_complete);
2119
2120 if ((isci_stopping == isci_port_get_state(isci_port)) ||
2121 (isci_stopped == isci_port_get_state(isci_port)))
2122 return -ENODEV;
2123
2124 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08002125 if (!isci_device)
2126 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07002127
2128 INIT_LIST_HEAD(&isci_device->node);
2129 domain_dev->lldd_dev = isci_device;
2130 isci_device->domain_dev = domain_dev;
2131 isci_device->isci_port = isci_port;
2132 isci_remote_device_change_state(isci_device, isci_starting);
2133
2134
Dan Williams1a380452011-03-03 18:01:43 -08002135 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002136 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
2137
Dan Williams6ad31fe2011-03-04 12:10:29 -08002138 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002139 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -08002140 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002141
Dan Williams6f231dd2011-07-02 22:56:22 -07002142 dev_dbg(&isci_host->pdev->dev,
2143 "%s: isci_device = %p\n",
2144 __func__, isci_device);
2145
2146 if (status != SCI_SUCCESS) {
2147
Dan Williams1a380452011-03-03 18:01:43 -08002148 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002149 isci_remote_device_deconstruct(
2150 isci_host,
2151 isci_device
2152 );
Dan Williams1a380452011-03-03 18:01:43 -08002153 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002154 return -ENODEV;
2155 }
2156
Dan Williams6ad31fe2011-03-04 12:10:29 -08002157 /* wait for the device ready callback. */
2158 wait_for_device_start(isci_host, isci_device);
2159
Dan Williams6f231dd2011-07-02 22:56:22 -07002160 return 0;
2161}
2162/**
2163 * isci_device_is_reset_pending() - This function will check if there is any
2164 * pending reset condition on the device.
2165 * @request: This parameter is the isci_device object.
2166 *
2167 * true if there is a reset pending for the device.
2168 */
2169bool isci_device_is_reset_pending(
2170 struct isci_host *isci_host,
2171 struct isci_remote_device *isci_device)
2172{
2173 struct isci_request *isci_request;
2174 struct isci_request *tmp_req;
2175 bool reset_is_pending = false;
2176 unsigned long flags;
2177
2178 dev_dbg(&isci_host->pdev->dev,
2179 "%s: isci_device = %p\n", __func__, isci_device);
2180
2181 spin_lock_irqsave(&isci_host->scic_lock, flags);
2182
2183 /* Check for reset on all pending requests. */
2184 list_for_each_entry_safe(isci_request, tmp_req,
2185 &isci_device->reqs_in_process, dev_node) {
2186 dev_dbg(&isci_host->pdev->dev,
2187 "%s: isci_device = %p request = %p\n",
2188 __func__, isci_device, isci_request);
2189
2190 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07002191 struct sas_task *task = isci_request_access_task(
2192 isci_request);
2193
Bartosz Barcinski467e8552011-04-12 17:28:41 -07002194 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002195 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
2196 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07002197 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002198 }
2199 }
2200
2201 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
2202
2203 dev_dbg(&isci_host->pdev->dev,
2204 "%s: isci_device = %p reset_is_pending = %d\n",
2205 __func__, isci_device, reset_is_pending);
2206
2207 return reset_is_pending;
2208}
2209
2210/**
2211 * isci_device_clear_reset_pending() - This function will clear if any pending
2212 * reset condition flags on the device.
2213 * @request: This parameter is the isci_device object.
2214 *
2215 * true if there is a reset pending for the device.
2216 */
Dan Williams4393aa42011-03-31 13:10:44 -07002217void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002218{
2219 struct isci_request *isci_request;
2220 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07002221 unsigned long flags = 0;
2222
Dan Williams4393aa42011-03-31 13:10:44 -07002223 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
2224 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07002225
Dan Williams4393aa42011-03-31 13:10:44 -07002226 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002227
2228 /* Clear reset pending on all pending requests. */
2229 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07002230 &idev->reqs_in_process, dev_node) {
2231 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
2232 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07002233
2234 if (isci_request->ttype == io_task) {
2235
2236 unsigned long flags2;
2237 struct sas_task *task = isci_request_access_task(
2238 isci_request);
2239
2240 spin_lock_irqsave(&task->task_state_lock, flags2);
2241 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
2242 spin_unlock_irqrestore(&task->task_state_lock, flags2);
2243 }
2244 }
Dan Williams4393aa42011-03-31 13:10:44 -07002245 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002246}