blob: 97ea8748335d5e525e6218fc9ec15ea5356d1d6b [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
Dan Williams88f3b622011-04-22 19:18:03 -070055#include "intel_sas.h"
Dan Williamsab2e8f72011-04-27 16:32:45 -070056#include "intel_ata.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070058#include "port.h"
59#include "remote_device.h"
60#include "request.h"
Dan Williams88f3b622011-04-22 19:18:03 -070061#include "scic_controller.h"
62#include "scic_io_request.h"
63#include "scic_phy.h"
64#include "scic_port.h"
65#include "scic_sds_controller.h"
66#include "scic_sds_phy.h"
67#include "scic_sds_port.h"
68#include "remote_node_context.h"
69#include "scic_sds_request.h"
70#include "sci_environment.h"
71#include "sci_util.h"
72#include "scu_event_codes.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070073#include "task.h"
74
Dan Williamsab2e8f72011-04-27 16:32:45 -070075/**
76 * isci_remote_device_change_state() - This function gets the status of the
77 * remote_device object.
78 * @isci_device: This parameter points to the isci_remote_device object
79 *
80 * status of the object as a isci_status enum.
81 */
82void isci_remote_device_change_state(
83 struct isci_remote_device *isci_device,
84 enum isci_status status)
85{
86 unsigned long flags;
87
88 spin_lock_irqsave(&isci_device->state_lock, flags);
89 isci_device->status = status;
90 spin_unlock_irqrestore(&isci_device->state_lock, flags);
91}
92
93/**
94 * isci_remote_device_not_ready() - This function is called by the scic when
95 * the remote device is not ready. We mark the isci device as ready (not
96 * "ready_for_io") and signal the waiting proccess.
97 * @isci_host: This parameter specifies the isci host object.
98 * @isci_device: This parameter specifies the remote device
99 *
100 */
101static void isci_remote_device_not_ready(struct isci_host *ihost,
102 struct isci_remote_device *idev, u32 reason)
103{
104 dev_dbg(&ihost->pdev->dev,
105 "%s: isci_device = %p\n", __func__, idev);
106
107 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
108 isci_remote_device_change_state(idev, isci_stopping);
109 else
110 /* device ready is actually a "not ready for io" state. */
111 isci_remote_device_change_state(idev, isci_ready);
112}
113
114/**
115 * isci_remote_device_ready() - This function is called by the scic when the
116 * remote device is ready. We mark the isci device as ready and signal the
117 * waiting proccess.
118 * @ihost: our valid isci_host
119 * @idev: remote device
120 *
121 */
122static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
123{
124 dev_dbg(&ihost->pdev->dev,
125 "%s: idev = %p\n", __func__, idev);
126
127 isci_remote_device_change_state(idev, isci_ready_for_io);
128 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
129 wake_up(&ihost->eventq);
130}
131
Dan Williamsec575662011-05-01 14:19:25 -0700132/* called once the remote node context is ready to be freed.
133 * The remote device can now report that its stop operation is complete. none
134 */
135static void rnc_destruct_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700136{
Dan Williamsec575662011-05-01 14:19:25 -0700137 struct scic_sds_remote_device *sci_dev = _dev;
138
139 BUG_ON(sci_dev->started_request_count != 0);
140 sci_base_state_machine_change_state(&sci_dev->state_machine,
141 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
142}
143
144static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
145{
146 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
147 u32 i, request_count = sci_dev->started_request_count;
148 enum sci_status status = SCI_SUCCESS;
149
150 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
151 struct scic_sds_request *sci_req;
152 enum sci_status s;
153
154 sci_req = scic->io_request_table[i];
155 if (!sci_req || sci_req->target_device != sci_dev)
156 continue;
157 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
158 if (s != SCI_SUCCESS)
159 status = s;
160 }
161
162 return status;
163}
164
165enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
166 u32 timeout)
167{
168 struct sci_base_state_machine *sm = &sci_dev->state_machine;
169 enum scic_sds_remote_device_states state = sm->current_state_id;
170
171 switch (state) {
172 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
173 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
174 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
175 default:
176 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
177 __func__, state);
178 return SCI_FAILURE_INVALID_STATE;
179 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
180 return SCI_SUCCESS;
181 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
182 /* device not started so there had better be no requests */
183 BUG_ON(sci_dev->started_request_count != 0);
184 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
185 rnc_destruct_done, sci_dev);
186 /* Transition to the stopping state and wait for the
187 * remote node to complete being posted and invalidated.
188 */
189 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
190 return SCI_SUCCESS;
191 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
192 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
193 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
194 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
195 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
196 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
197 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
198 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
199 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
200 if (sci_dev->started_request_count == 0) {
201 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
202 rnc_destruct_done, sci_dev);
203 return SCI_SUCCESS;
204 } else
205 return scic_sds_remote_device_terminate_requests(sci_dev);
206 break;
207 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
208 /* All requests should have been terminated, but if there is an
209 * attempt to stop a device already in the stopping state, then
210 * try again to terminate.
211 */
212 return scic_sds_remote_device_terminate_requests(sci_dev);
213 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
214 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
215 return SCI_SUCCESS;
216 }
Dan Williams88f3b622011-04-22 19:18:03 -0700217}
Dan Williams6f231dd2011-07-02 22:56:22 -0700218
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700219enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700220{
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700221 struct sci_base_state_machine *sm = &sci_dev->state_machine;
222 enum scic_sds_remote_device_states state = sm->current_state_id;
223
224 switch (state) {
225 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
226 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
227 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
228 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
229 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
230 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
231 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
232 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
233 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
234 default:
235 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
236 __func__, state);
237 return SCI_FAILURE_INVALID_STATE;
238 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
239 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
240 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
241 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
242 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
243 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
244 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
245 return SCI_SUCCESS;
246 }
Dan Williams88f3b622011-04-22 19:18:03 -0700247}
248
Dan Williams81515182011-05-01 14:53:00 -0700249enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700250{
Dan Williams81515182011-05-01 14:53:00 -0700251 struct sci_base_state_machine *sm = &sci_dev->state_machine;
252 enum scic_sds_remote_device_states state = sm->current_state_id;
Dan Williams88f3b622011-04-22 19:18:03 -0700253
Dan Williams81515182011-05-01 14:53:00 -0700254 if (state != SCI_BASE_REMOTE_DEVICE_STATE_RESETTING) {
255 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
256 __func__, state);
257 return SCI_FAILURE_INVALID_STATE;
258 }
259
260 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_READY);
261 return SCI_SUCCESS;
262}
Dan Williams88f3b622011-04-22 19:18:03 -0700263
Dan Williams323f0ec2011-05-01 16:15:47 -0700264enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
265 u32 suspend_type)
Dan Williams88f3b622011-04-22 19:18:03 -0700266{
Dan Williams323f0ec2011-05-01 16:15:47 -0700267 struct sci_base_state_machine *sm = &sci_dev->state_machine;
268 enum scic_sds_remote_device_states state = sm->current_state_id;
269
270 if (state != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD) {
271 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
272 __func__, state);
273 return SCI_FAILURE_INVALID_STATE;
274 }
275
276 return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
277 suspend_type, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700278}
279
280/**
281 *
282 * @sci_dev: The remote device for which the event handling is being
283 * requested.
284 * @frame_index: This is the frame index that is being processed.
285 *
286 * This method invokes the frame handler for the remote device state machine
287 * enum sci_status
288 */
289enum sci_status scic_sds_remote_device_frame_handler(
290 struct scic_sds_remote_device *sci_dev,
291 u32 frame_index)
292{
293 return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
294}
295
296/**
297 *
298 * @sci_dev: The remote device for which the event handling is being
299 * requested.
300 * @event_code: This is the event code that is to be processed.
301 *
302 * This method invokes the remote device event handler. enum sci_status
303 */
304enum sci_status scic_sds_remote_device_event_handler(
305 struct scic_sds_remote_device *sci_dev,
306 u32 event_code)
307{
308 return sci_dev->state_handlers->event_handler(sci_dev, event_code);
309}
310
Dan Williams18606552011-05-01 14:57:11 -0700311static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
312 struct scic_sds_request *sci_req,
313 enum sci_status status)
Dan Williams88f3b622011-04-22 19:18:03 -0700314{
Dan Williams18606552011-05-01 14:57:11 -0700315 struct scic_sds_port *sci_port = sci_dev->owning_port;
316
317 /* cleanup requests that failed after starting on the port */
318 if (status != SCI_SUCCESS)
319 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
320 else
321 scic_sds_remote_device_increment_request_count(sci_dev);
322}
323
324enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
325 struct scic_sds_remote_device *sci_dev,
326 struct scic_sds_request *sci_req)
327{
328 struct sci_base_state_machine *sm = &sci_dev->state_machine;
329 enum scic_sds_remote_device_states state = sm->current_state_id;
330 struct scic_sds_port *sci_port = sci_dev->owning_port;
331 enum sci_status status;
332
333 switch (state) {
334 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
335 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
336 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
337 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
338 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
339 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
340 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
341 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
342 default:
343 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
344 __func__, state);
345 return SCI_FAILURE_INVALID_STATE;
346 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
347 /* attempt to start an io request for this device object. The remote
348 * device object will issue the start request for the io and if
349 * successful it will start the request for the port object then
350 * increment its own request count.
351 */
352 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
353 if (status != SCI_SUCCESS)
354 return status;
355
356 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
357 if (status != SCI_SUCCESS)
358 break;
359
360 status = scic_sds_request_start(sci_req);
361 break;
362 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
363 /* handle the start io operation for a sata device that is in
364 * the command idle state. - Evalute the type of IO request to
365 * be started - If its an NCQ request change to NCQ substate -
366 * If its any other command change to the CMD substate
367 *
368 * If this is a softreset we may want to have a different
369 * substate.
370 */
371 enum scic_sds_remote_device_states new_state;
372
373 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
374 if (status != SCI_SUCCESS)
375 return status;
376
377 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
378 if (status != SCI_SUCCESS)
379 break;
380
381 status = sci_req->state_handlers->start_handler(sci_req);
382 if (status != SCI_SUCCESS)
383 break;
384
385 if (isci_sata_get_sat_protocol(sci_req->ireq) == SAT_PROTOCOL_FPDMA)
386 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
387 else {
388 sci_dev->working_request = sci_req;
389 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
390 }
391 sci_base_state_machine_change_state(sm, new_state);
392 break;
393 }
394 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
395 if (isci_sata_get_sat_protocol(sci_req->ireq) == SAT_PROTOCOL_FPDMA) {
396 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
397 if (status != SCI_SUCCESS)
398 return status;
399
400 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
401 if (status != SCI_SUCCESS)
402 break;
403
404 status = sci_req->state_handlers->start_handler(sci_req);
405 } else
406 return SCI_FAILURE_INVALID_STATE;
407 break;
408 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
409 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
410 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
411 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
412 if (status != SCI_SUCCESS)
413 return status;
414
415 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
416 if (status != SCI_SUCCESS)
417 break;
418
419 status = scic_sds_request_start(sci_req);
420 if (status != SCI_SUCCESS)
421 break;
422
423 sci_dev->working_request = sci_req;
424 sci_base_state_machine_change_state(&sci_dev->state_machine,
425 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
426 break;
427 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
428 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
429 /* device is already handling a command it can not accept new commands
430 * until this one is complete.
431 */
432 return SCI_FAILURE_INVALID_STATE;
433 }
434
435 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
436 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700437}
438
Dan Williams10a09e62011-05-01 15:33:43 -0700439static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
440 struct scic_sds_remote_device *sci_dev,
441 struct scic_sds_request *sci_req)
Dan Williams88f3b622011-04-22 19:18:03 -0700442{
Dan Williams10a09e62011-05-01 15:33:43 -0700443 enum sci_status status;
444
445 status = scic_sds_request_complete(sci_req);
446 if (status != SCI_SUCCESS)
447 return status;
448
449 status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
450 if (status != SCI_SUCCESS)
451 return status;
452
453 scic_sds_remote_device_decrement_request_count(sci_dev);
454 return status;
455}
456
457enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
458 struct scic_sds_remote_device *sci_dev,
459 struct scic_sds_request *sci_req)
460{
461 struct sci_base_state_machine *sm = &sci_dev->state_machine;
462 enum scic_sds_remote_device_states state = sm->current_state_id;
463 struct scic_sds_port *sci_port = sci_dev->owning_port;
464 enum sci_status status;
465
466 switch (state) {
467 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
468 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
469 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
470 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
471 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
472 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
473 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
474 default:
475 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
476 __func__, state);
477 return SCI_FAILURE_INVALID_STATE;
478 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
479 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
480 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
481 status = common_complete_io(sci_port, sci_dev, sci_req);
482 break;
483 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
484 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
485 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
486 status = common_complete_io(sci_port, sci_dev, sci_req);
487 if (status != SCI_SUCCESS)
488 break;
489
490 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
491 /* This request causes hardware error, device needs to be Lun Reset.
492 * So here we force the state machine to IDLE state so the rest IOs
493 * can reach RNC state handler, these IOs will be completed by RNC with
494 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
495 */
496 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
497 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
498 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
499 break;
500 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
501 status = common_complete_io(sci_port, sci_dev, sci_req);
502 if (status != SCI_SUCCESS)
503 break;
504 sci_base_state_machine_change_state(sm, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
505 break;
506 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
507 status = common_complete_io(sci_port, sci_dev, sci_req);
508 if (status != SCI_SUCCESS)
509 break;
510
511 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
512 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
513 rnc_destruct_done,
514 sci_dev);
515 break;
516 }
517
518 if (status != SCI_SUCCESS)
519 dev_err(scirdev_to_dev(sci_dev),
520 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
521 "could not complete\n", __func__, sci_port,
522 sci_dev, sci_req, status);
523
524 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700525}
526
Dan Williams84b9b022011-05-01 15:53:25 -0700527static void scic_sds_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700528{
Dan Williams84b9b022011-05-01 15:53:25 -0700529 struct scic_sds_remote_device *sci_dev = dev;
530
531 /* we need to check if this request is still valid to continue. */
532 if (sci_dev->working_request)
533 scic_controller_continue_io(sci_dev->working_request);
534}
535
536enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
537 struct scic_sds_remote_device *sci_dev,
538 struct scic_sds_request *sci_req)
539{
540 struct sci_base_state_machine *sm = &sci_dev->state_machine;
541 enum scic_sds_remote_device_states state = sm->current_state_id;
542 struct scic_sds_port *sci_port = sci_dev->owning_port;
543 enum sci_status status;
544
545 switch (state) {
546 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
547 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
548 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
549 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
550 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
551 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
552 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
553 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
554 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
555 default:
556 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
557 __func__, state);
558 return SCI_FAILURE_INVALID_STATE;
559 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
560 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
561 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
562 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
563 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
564 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
565 if (status != SCI_SUCCESS)
566 return status;
567
568 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
569 if (status != SCI_SUCCESS)
570 goto out;
571
572 status = sci_req->state_handlers->start_handler(sci_req);
573 if (status != SCI_SUCCESS)
574 goto out;
575
576 /* Note: If the remote device state is not IDLE this will
577 * replace the request that probably resulted in the task
578 * management request.
579 */
580 sci_dev->working_request = sci_req;
581 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
582
583 /* The remote node context must cleanup the TCi to NCQ mapping
584 * table. The only way to do this correctly is to either write
585 * to the TLCR register or to invalidate and repost the RNC. In
586 * either case the remote node context state machine will take
587 * the correct action when the remote node context is suspended
588 * and later resumed.
589 */
590 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
591 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
592 scic_sds_remote_node_context_resume(&sci_dev->rnc,
593 scic_sds_remote_device_continue_request,
594 sci_dev);
595
596 out:
597 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
598 /* We need to let the controller start request handler know that
599 * it can't post TC yet. We will provide a callback function to
600 * post TC when RNC gets resumed.
601 */
602 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
603 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
604 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
605 if (status != SCI_SUCCESS)
606 return status;
607
608 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
609 if (status != SCI_SUCCESS)
610 break;
611
612 status = scic_sds_request_start(sci_req);
613 break;
614 }
615 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
616
617 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700618}
619
620/**
621 *
Dan Williams88f3b622011-04-22 19:18:03 -0700622 * @sci_dev:
623 * @request:
624 *
625 * This method takes the request and bulids an appropriate SCU context for the
626 * request and then requests the controller to post the request. none
627 */
628void scic_sds_remote_device_post_request(
629 struct scic_sds_remote_device *sci_dev,
630 u32 request)
631{
632 u32 context;
633
634 context = scic_sds_remote_device_build_command_context(sci_dev, request);
635
636 scic_sds_controller_post_request(
637 scic_sds_remote_device_get_controller(sci_dev),
638 context
639 );
640}
641
Dan Williamsab2e8f72011-04-27 16:32:45 -0700642/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700643 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700644 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700645 */
Dan Williamseb229672011-05-01 14:05:57 -0700646static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700647{
Dan Williamsab2e8f72011-04-27 16:32:45 -0700648 struct scic_sds_remote_device *sci_dev = _dev;
649 enum scic_sds_remote_device_states state;
Dan Williams88f3b622011-04-22 19:18:03 -0700650
Dan Williamsab2e8f72011-04-27 16:32:45 -0700651 state = sci_dev->state_machine.current_state_id;
652 switch (state) {
653 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
654 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
655 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
656 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
657 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
658 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
659 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
660 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
661 break;
662 default:
663 /* go 'ready' if we are not already in a ready state */
664 sci_base_state_machine_change_state(&sci_dev->state_machine,
665 SCI_BASE_REMOTE_DEVICE_STATE_READY);
666 break;
Dan Williams88f3b622011-04-22 19:18:03 -0700667 }
668}
669
Dan Williams88f3b622011-04-22 19:18:03 -0700670static enum sci_status
671default_device_handler(struct scic_sds_remote_device *sci_dev,
672 const char *func)
673{
674 dev_warn(scirdev_to_dev(sci_dev),
675 "%s: in wrong state: %d\n", func,
676 sci_base_state_machine_get_state(&sci_dev->state_machine));
677 return SCI_FAILURE_INVALID_STATE;
678}
679
Dan Williamsab2e8f72011-04-27 16:32:45 -0700680static enum sci_status scic_sds_remote_device_default_resume_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700681 struct scic_sds_remote_device *sci_dev)
682{
683 return default_device_handler(sci_dev, __func__);
684}
685
686/**
687 *
688 * @device: The struct scic_sds_remote_device which is then cast into a
689 * struct scic_sds_remote_device.
690 * @event_code: The event code that the struct scic_sds_controller wants the device
691 * object to process.
692 *
693 * This method is the default event handler. It will call the RNC state
694 * machine handler for any RNC events otherwise it will log a warning and
695 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
696 */
697static enum sci_status scic_sds_remote_device_core_event_handler(
698 struct scic_sds_remote_device *sci_dev,
699 u32 event_code,
700 bool is_ready_state)
701{
702 enum sci_status status;
703
704 switch (scu_get_event_type(event_code)) {
705 case SCU_EVENT_TYPE_RNC_OPS_MISC:
706 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
707 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
708 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
709 break;
710 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
711
712 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
713 status = SCI_SUCCESS;
714
715 /* Suspend the associated RNC */
716 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
717 SCI_SOFTWARE_SUSPENSION,
718 NULL, NULL);
719
720 dev_dbg(scirdev_to_dev(sci_dev),
721 "%s: device: %p event code: %x: %s\n",
722 __func__, sci_dev, event_code,
723 (is_ready_state)
724 ? "I_T_Nexus_Timeout event"
725 : "I_T_Nexus_Timeout event in wrong state");
726
727 break;
728 }
729 /* Else, fall through and treat as unhandled... */
730
731 default:
732 dev_dbg(scirdev_to_dev(sci_dev),
733 "%s: device: %p event code: %x: %s\n",
734 __func__, sci_dev, event_code,
735 (is_ready_state)
736 ? "unexpected event"
737 : "unexpected event in wrong state");
738 status = SCI_FAILURE_INVALID_STATE;
739 break;
740 }
741
742 return status;
743}
744/**
745 *
746 * @device: The struct scic_sds_remote_device which is then cast into a
747 * struct scic_sds_remote_device.
748 * @event_code: The event code that the struct scic_sds_controller wants the device
749 * object to process.
750 *
751 * This method is the default event handler. It will call the RNC state
752 * machine handler for any RNC events otherwise it will log a warning and
753 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
754 */
755static enum sci_status scic_sds_remote_device_default_event_handler(
756 struct scic_sds_remote_device *sci_dev,
757 u32 event_code)
758{
759 return scic_sds_remote_device_core_event_handler(sci_dev,
760 event_code,
761 false);
762}
763
764/**
765 *
766 * @device: The struct scic_sds_remote_device which is then cast into a
767 * struct scic_sds_remote_device.
768 * @frame_index: The frame index for which the struct scic_sds_controller wants this
769 * device object to process.
770 *
771 * This method is the default unsolicited frame handler. It logs a warning,
772 * releases the frame and returns a failure. enum sci_status
773 * SCI_FAILURE_INVALID_STATE
774 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700775static enum sci_status scic_sds_remote_device_default_frame_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700776 struct scic_sds_remote_device *sci_dev,
777 u32 frame_index)
778{
779 dev_warn(scirdev_to_dev(sci_dev),
780 "%s: SCIC Remote Device requested to handle frame %x "
781 "while in wrong state %d\n",
782 __func__,
783 frame_index,
784 sci_base_state_machine_get_state(
785 &sci_dev->state_machine));
786
787 /* Return the frame back to the controller */
788 scic_sds_controller_release_frame(
789 scic_sds_remote_device_get_controller(sci_dev), frame_index
790 );
791
792 return SCI_FAILURE_INVALID_STATE;
793}
794
Dan Williams88f3b622011-04-22 19:18:03 -0700795/**
796 *
797 * @device: The struct scic_sds_remote_device which is then cast into a
798 * struct scic_sds_remote_device.
799 * @frame_index: The frame index for which the struct scic_sds_controller wants this
800 * device object to process.
801 *
802 * This method is a general ssp frame handler. In most cases the device object
803 * needs to route the unsolicited frame processing to the io request object.
804 * This method decodes the tag for the io request object and routes the
805 * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
806 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700807static enum sci_status scic_sds_remote_device_general_frame_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700808 struct scic_sds_remote_device *sci_dev,
809 u32 frame_index)
810{
811 enum sci_status result;
812 struct sci_ssp_frame_header *frame_header;
813 struct scic_sds_request *io_request;
814
815 result = scic_sds_unsolicited_frame_control_get_header(
816 &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
817 frame_index,
818 (void **)&frame_header
819 );
820
821 if (SCI_SUCCESS == result) {
822 io_request = scic_sds_controller_get_io_request_from_tag(
823 scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
824
825 if ((io_request == NULL)
826 || (io_request->target_device != sci_dev)) {
827 /*
828 * We could not map this tag to a valid IO request
829 * Just toss the frame and continue */
830 scic_sds_controller_release_frame(
831 scic_sds_remote_device_get_controller(sci_dev), frame_index
832 );
833 } else {
834 /* The IO request is now in charge of releasing the frame */
835 result = io_request->state_handlers->frame_handler(
836 io_request, frame_index);
837 }
838 }
839
840 return result;
841}
842
843/**
844 *
845 * @[in]: sci_dev This is the device object that is receiving the event.
846 * @[in]: event_code The event code to process.
847 *
848 * This is a common method for handling events reported to the remote device
849 * from the controller object. enum sci_status
850 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700851static enum sci_status scic_sds_remote_device_general_event_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700852 struct scic_sds_remote_device *sci_dev,
853 u32 event_code)
854{
855 return scic_sds_remote_device_core_event_handler(sci_dev,
856 event_code,
857 true);
858}
859
Dan Williamsab2e8f72011-04-27 16:32:45 -0700860static enum sci_status scic_sds_stp_remote_device_ready_idle_substate_event_handler(
861 struct scic_sds_remote_device *sci_dev,
862 u32 event_code)
863{
864 enum sci_status status;
865
866 status = scic_sds_remote_device_general_event_handler(sci_dev, event_code);
867 if (status != SCI_SUCCESS)
868 return status;
869
870 /* We pick up suspension events to handle specifically to this state. We
871 * resume the RNC right away. enum sci_status
872 */
873 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
874 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
875 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
876
877 return status;
878}
879
Dan Williamsab2e8f72011-04-27 16:32:45 -0700880static enum sci_status scic_sds_stp_remote_device_ready_ncq_substate_frame_handler(struct scic_sds_remote_device *sci_dev,
881 u32 frame_index)
882{
883 enum sci_status status;
884 struct sata_fis_header *frame_header;
885 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
886
887 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
888 frame_index,
889 (void **)&frame_header);
890 if (status != SCI_SUCCESS)
891 return status;
892
893 if (frame_header->fis_type == SATA_FIS_TYPE_SETDEVBITS &&
894 (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
895 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
896
897 /* TODO Check sactive and complete associated IO if any. */
898
899 sci_base_state_machine_change_state(&sci_dev->state_machine,
900 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
901 } else if (frame_header->fis_type == SATA_FIS_TYPE_REGD2H &&
902 (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
903 /*
904 * Some devices return D2H FIS when an NCQ error is detected.
905 * Treat this like an SDB error FIS ready reason.
906 */
907 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
908
909 sci_base_state_machine_change_state(&sci_dev->state_machine,
910 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
911 } else
912 status = SCI_FAILURE;
913
914 scic_sds_controller_release_frame(scic, frame_index);
915
916 return status;
917}
918
Dan Williamsab2e8f72011-04-27 16:32:45 -0700919static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_frame_handler(
920 struct scic_sds_remote_device *sci_dev,
921 u32 frame_index)
922{
923 /* The device doe not process any UF received from the hardware while
924 * in this state. All unsolicited frames are forwarded to the io
925 * request object.
926 */
927 return scic_sds_io_request_frame_handler(sci_dev->working_request,
928 frame_index);
929}
930
Dan Williamsab2e8f72011-04-27 16:32:45 -0700931static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
932{
933 struct scic_sds_remote_device *sci_dev = _dev;
934 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
935 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
936
937 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
938 * As a result, avoid sending the ready notification.
939 */
940 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
941 isci_remote_device_ready(scic->ihost, idev);
942}
943
Dan Williamsab2e8f72011-04-27 16:32:45 -0700944static enum sci_status scic_sds_smp_remote_device_ready_cmd_substate_frame_handler(
945 struct scic_sds_remote_device *sci_dev,
946 u32 frame_index)
947{
948 enum sci_status status;
949
950 /* The device does not process any UF received from the hardware while
951 * in this state. All unsolicited frames are forwarded to the io request
952 * object.
953 */
954 status = scic_sds_io_request_frame_handler(
955 sci_dev->working_request,
956 frame_index
957 );
958
959 return status;
960}
961
Dan Williams88f3b622011-04-22 19:18:03 -0700962static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
963 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700964 .resume_handler = scic_sds_remote_device_default_resume_handler,
965 .event_handler = scic_sds_remote_device_default_event_handler,
966 .frame_handler = scic_sds_remote_device_default_frame_handler
967 },
968 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700969 .resume_handler = scic_sds_remote_device_default_resume_handler,
970 .event_handler = scic_sds_remote_device_default_event_handler,
971 .frame_handler = scic_sds_remote_device_default_frame_handler
972 },
973 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700974 .resume_handler = scic_sds_remote_device_default_resume_handler,
975 .event_handler = scic_sds_remote_device_general_event_handler,
976 .frame_handler = scic_sds_remote_device_default_frame_handler
977 },
978 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700979 .resume_handler = scic_sds_remote_device_default_resume_handler,
980 .event_handler = scic_sds_remote_device_general_event_handler,
981 .frame_handler = scic_sds_remote_device_general_frame_handler,
982 },
Dan Williamsab2e8f72011-04-27 16:32:45 -0700983 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700984 .resume_handler = scic_sds_remote_device_default_resume_handler,
985 .event_handler = scic_sds_stp_remote_device_ready_idle_substate_event_handler,
986 .frame_handler = scic_sds_remote_device_default_frame_handler
987 },
988 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700989 .resume_handler = scic_sds_remote_device_default_resume_handler,
990 .event_handler = scic_sds_remote_device_general_event_handler,
991 .frame_handler = scic_sds_stp_remote_device_ready_cmd_substate_frame_handler
992 },
993 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700994 .resume_handler = scic_sds_remote_device_default_resume_handler,
995 .event_handler = scic_sds_remote_device_general_event_handler,
996 .frame_handler = scic_sds_stp_remote_device_ready_ncq_substate_frame_handler
997 },
998 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700999 .resume_handler = scic_sds_remote_device_default_resume_handler,
1000 .event_handler = scic_sds_remote_device_general_event_handler,
1001 .frame_handler = scic_sds_remote_device_general_frame_handler
1002 },
1003 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001004 .resume_handler = scic_sds_remote_device_default_resume_handler,
1005 .event_handler = scic_sds_remote_device_general_event_handler,
1006 .frame_handler = scic_sds_remote_device_general_frame_handler
1007 },
1008 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001009 .resume_handler = scic_sds_remote_device_default_resume_handler,
1010 .event_handler = scic_sds_remote_device_general_event_handler,
1011 .frame_handler = scic_sds_remote_device_default_frame_handler
1012 },
1013 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001014 .resume_handler = scic_sds_remote_device_default_resume_handler,
1015 .event_handler = scic_sds_remote_device_general_event_handler,
1016 .frame_handler = scic_sds_smp_remote_device_ready_cmd_substate_frame_handler
1017 },
Dan Williams88f3b622011-04-22 19:18:03 -07001018 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001019 .resume_handler = scic_sds_remote_device_default_resume_handler,
1020 .event_handler = scic_sds_remote_device_general_event_handler,
1021 .frame_handler = scic_sds_remote_device_general_frame_handler
1022 },
1023 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001024 .resume_handler = scic_sds_remote_device_default_resume_handler,
1025 .event_handler = scic_sds_remote_device_default_event_handler,
1026 .frame_handler = scic_sds_remote_device_general_frame_handler
1027 },
1028 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001029 .resume_handler = scic_sds_remote_device_default_resume_handler,
1030 .event_handler = scic_sds_remote_device_default_event_handler,
1031 .frame_handler = scic_sds_remote_device_general_frame_handler
1032 },
1033 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001034 .resume_handler = scic_sds_remote_device_default_resume_handler,
1035 .event_handler = scic_sds_remote_device_default_event_handler,
1036 .frame_handler = scic_sds_remote_device_default_frame_handler
1037 }
1038};
1039
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001040static void scic_sds_remote_device_initial_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001041{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001042 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001043
Dan Williams88f3b622011-04-22 19:18:03 -07001044 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1045 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1046
1047 /* Initial state is a transitional state to the stopped state */
1048 sci_base_state_machine_change_state(&sci_dev->state_machine,
1049 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1050}
1051
1052/**
Dan Williams88f3b622011-04-22 19:18:03 -07001053 * scic_remote_device_destruct() - free remote node context and destruct
1054 * @remote_device: This parameter specifies the remote device to be destructed.
1055 *
1056 * Remote device objects are a limited resource. As such, they must be
1057 * protected. Thus calls to construct and destruct are mutually exclusive and
1058 * non-reentrant. The return value shall indicate if the device was
1059 * successfully destructed or if some failure occurred. enum sci_status This value
1060 * is returned if the device is successfully destructed.
1061 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
1062 * device isn't valid (e.g. it's already been destoryed, the handle isn't
1063 * valid, etc.).
1064 */
1065static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
1066{
Dan Williamsb8d82f62011-05-01 14:38:26 -07001067 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1068 enum scic_sds_remote_device_states state = sm->current_state_id;
1069 struct scic_sds_controller *scic;
1070
1071 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1072 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1073 __func__, state);
1074 return SCI_FAILURE_INVALID_STATE;
1075 }
1076
1077 scic = sci_dev->owning_port->owning_controller;
1078 scic_sds_controller_free_remote_node_context(scic, sci_dev,
1079 sci_dev->rnc.remote_node_index);
1080 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
1081 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
1082
1083 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001084}
1085
Dan Williams6f231dd2011-07-02 22:56:22 -07001086/**
1087 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -08001088 * @ihost: This parameter specifies the isci host object.
1089 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -07001090 *
1091 */
Dan Williamsd9c37392011-03-03 17:59:32 -08001092static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001093{
Dan Williamsd9c37392011-03-03 17:59:32 -08001094 dev_dbg(&ihost->pdev->dev,
1095 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001096
1097 /* There should not be any outstanding io's. All paths to
1098 * here should go through isci_remote_device_nuke_requests.
1099 * If we hit this condition, we will need a way to complete
1100 * io requests in process */
Dan Williamsd9c37392011-03-03 17:59:32 -08001101 while (!list_empty(&idev->reqs_in_process)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001102
Dan Williamsd9c37392011-03-03 17:59:32 -08001103 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001104 "%s: ** request list not empty! **\n", __func__);
1105 BUG();
1106 }
1107
Dan Williams57f20f42011-04-21 18:14:45 -07001108 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -08001109 idev->domain_dev->lldd_dev = NULL;
1110 idev->domain_dev = NULL;
1111 idev->isci_port = NULL;
1112 list_del_init(&idev->node);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001113
Dan Williamsd9c37392011-03-03 17:59:32 -08001114 clear_bit(IDEV_START_PENDING, &idev->flags);
1115 clear_bit(IDEV_STOP_PENDING, &idev->flags);
1116 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001117}
1118
Dan Williams88f3b622011-04-22 19:18:03 -07001119/**
1120 * isci_remote_device_stop_complete() - This function is called by the scic
1121 * when the remote device stop has completed. We mark the isci device as not
1122 * ready and remove the isci remote device.
1123 * @ihost: This parameter specifies the isci host object.
1124 * @idev: This parameter specifies the remote device.
1125 * @status: This parameter specifies status of the completion.
1126 *
1127 */
1128static void isci_remote_device_stop_complete(struct isci_host *ihost,
1129 struct isci_remote_device *idev)
1130{
1131 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1132
1133 isci_remote_device_change_state(idev, isci_stopped);
1134
1135 /* after stop, we can tear down resources. */
1136 isci_remote_device_deconstruct(ihost, idev);
1137}
1138
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001139static void scic_sds_remote_device_stopped_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001140{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001141 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001142 struct scic_sds_controller *scic;
1143 struct isci_remote_device *idev;
1144 struct isci_host *ihost;
1145 u32 prev_state;
1146
Dan Williams88f3b622011-04-22 19:18:03 -07001147 scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +00001148 ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001149 idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001150
1151 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1152 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1153
1154 /* If we are entering from the stopping state let the SCI User know that
1155 * the stop operation has completed.
1156 */
1157 prev_state = sci_dev->state_machine.previous_state_id;
1158 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1159 isci_remote_device_stop_complete(ihost, idev);
1160
1161 scic_sds_controller_remote_device_stopped(scic, sci_dev);
1162}
1163
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001164static void scic_sds_remote_device_starting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001165{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001166 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001167 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +00001168 struct isci_host *ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001169 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001170
1171 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1172 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1173
1174 isci_remote_device_not_ready(ihost, idev,
1175 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1176}
1177
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001178static void scic_sds_remote_device_ready_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001179{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001180 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001181 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1182 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001183
1184 SET_STATE_HANDLER(sci_dev,
1185 scic_sds_remote_device_state_handler_table,
1186 SCI_BASE_REMOTE_DEVICE_STATE_READY);
1187
1188 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1189
Dan Williamsab2e8f72011-04-27 16:32:45 -07001190 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
1191 sci_base_state_machine_change_state(&sci_dev->state_machine,
1192 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1193 } else if (dev_is_expander(dev)) {
1194 sci_base_state_machine_change_state(&sci_dev->state_machine,
1195 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1196 } else
1197 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
Dan Williams88f3b622011-04-22 19:18:03 -07001198}
1199
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001200static void scic_sds_remote_device_ready_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001201{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001202 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001203 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1204
1205 if (dev->dev_type == SAS_END_DEV) {
1206 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001207 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001208
Dan Williamsab2e8f72011-04-27 16:32:45 -07001209 isci_remote_device_not_ready(scic->ihost, idev,
Dan Williams88f3b622011-04-22 19:18:03 -07001210 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1211 }
1212}
1213
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001214static void scic_sds_remote_device_stopping_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001215{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001216 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001217
1218 SET_STATE_HANDLER(
1219 sci_dev,
1220 scic_sds_remote_device_state_handler_table,
1221 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1222 );
1223}
1224
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001225static void scic_sds_remote_device_failed_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001226{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001227 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001228
1229 SET_STATE_HANDLER(
1230 sci_dev,
1231 scic_sds_remote_device_state_handler_table,
1232 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1233 );
1234}
1235
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001236static void scic_sds_remote_device_resetting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001237{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001238 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001239
1240 SET_STATE_HANDLER(
1241 sci_dev,
1242 scic_sds_remote_device_state_handler_table,
1243 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1244 );
1245
1246 scic_sds_remote_node_context_suspend(
1247 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1248}
1249
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001250static void scic_sds_remote_device_resetting_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001251{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001252 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001253
1254 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1255}
1256
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001257static void scic_sds_remote_device_final_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001258{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001259 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001260
1261 SET_STATE_HANDLER(
1262 sci_dev,
1263 scic_sds_remote_device_state_handler_table,
1264 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1265 );
1266}
1267
Dan Williamsab2e8f72011-04-27 16:32:45 -07001268static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
1269{
1270 struct scic_sds_remote_device *sci_dev = object;
1271
1272 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1273 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1274
1275 sci_dev->working_request = NULL;
1276 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
1277 /*
1278 * Since the RNC is ready, it's alright to finish completion
1279 * processing (e.g. signal the remote device is ready). */
1280 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
1281 } else {
1282 scic_sds_remote_node_context_resume(&sci_dev->rnc,
1283 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
1284 sci_dev);
1285 }
1286}
1287
1288static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1289{
1290 struct scic_sds_remote_device *sci_dev = object;
1291 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1292
1293 BUG_ON(sci_dev->working_request == NULL);
1294
1295 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1296 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1297
1298 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1299 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1300}
1301
1302static void scic_sds_stp_remote_device_ready_ncq_substate_enter(void *object)
1303{
1304 struct scic_sds_remote_device *sci_dev = object;
1305
1306 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1307 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ);
1308}
1309
1310static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1311{
1312 struct scic_sds_remote_device *sci_dev = object;
1313 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1314 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1315
1316 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1317 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1318
1319 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1320 isci_remote_device_not_ready(scic->ihost, idev,
1321 sci_dev->not_ready_reason);
1322}
1323
1324static void scic_sds_stp_remote_device_ready_await_reset_substate_enter(void *object)
1325{
1326 struct scic_sds_remote_device *sci_dev = object;
1327
1328 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1329 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
1330}
1331
1332static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1333{
1334 struct scic_sds_remote_device *sci_dev = object;
1335 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1336
1337 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1338 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1339
1340 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1341}
1342
1343static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1344{
1345 struct scic_sds_remote_device *sci_dev = object;
1346 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1347
1348 BUG_ON(sci_dev->working_request == NULL);
1349
1350 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1351 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1352
1353 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1354 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1355}
1356
1357static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1358{
1359 struct scic_sds_remote_device *sci_dev = object;
1360
1361 sci_dev->working_request = NULL;
1362}
Dan Williams88f3b622011-04-22 19:18:03 -07001363
1364static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1365 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1366 .enter_state = scic_sds_remote_device_initial_state_enter,
1367 },
1368 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1369 .enter_state = scic_sds_remote_device_stopped_state_enter,
1370 },
1371 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1372 .enter_state = scic_sds_remote_device_starting_state_enter,
1373 },
1374 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1375 .enter_state = scic_sds_remote_device_ready_state_enter,
1376 .exit_state = scic_sds_remote_device_ready_state_exit
1377 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001378 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1379 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1380 },
1381 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1382 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1383 },
1384 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
1385 .enter_state = scic_sds_stp_remote_device_ready_ncq_substate_enter,
1386 },
1387 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1388 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1389 },
1390 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
1391 .enter_state = scic_sds_stp_remote_device_ready_await_reset_substate_enter,
1392 },
1393 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1394 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1395 },
1396 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1397 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1398 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1399 },
Dan Williams88f3b622011-04-22 19:18:03 -07001400 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1401 .enter_state = scic_sds_remote_device_stopping_state_enter,
1402 },
1403 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1404 .enter_state = scic_sds_remote_device_failed_state_enter,
1405 },
1406 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1407 .enter_state = scic_sds_remote_device_resetting_state_enter,
1408 .exit_state = scic_sds_remote_device_resetting_state_exit
1409 },
1410 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1411 .enter_state = scic_sds_remote_device_final_state_enter,
1412 },
1413};
1414
1415/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001416 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001417 * @sci_port: SAS/SATA port through which this device is accessed.
1418 * @sci_dev: remote device to construct
1419 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001420 * This routine just performs benign initialization and does not
1421 * allocate the remote_node_context which is left to
1422 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1423 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001424 */
1425static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1426 struct scic_sds_remote_device *sci_dev)
1427{
1428 sci_dev->owning_port = sci_port;
1429 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001430
1431 sci_base_state_machine_construct(
1432 &sci_dev->state_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001433 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001434 scic_sds_remote_device_state_table,
1435 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1436 );
1437
1438 sci_base_state_machine_start(
1439 &sci_dev->state_machine
1440 );
1441
1442 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1443 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001444}
1445
1446/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001447 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001448 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001449 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1450 * the device is known to the SCI Core since it is contained in the
1451 * scic_phy object. Remote node context(s) is/are a global resource
1452 * allocated by this routine, freed by scic_remote_device_destruct().
1453 *
1454 * Returns:
1455 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1456 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1457 * sata-only controller instance.
1458 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001459 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001460static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1461 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001462{
1463 enum sci_status status;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001464 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001465
Dan Williamsb87ee302011-04-25 11:48:29 -07001466 scic_remote_device_construct(sci_port, sci_dev);
1467
Dan Williams88f3b622011-04-22 19:18:03 -07001468 /*
1469 * This information is request to determine how many remote node context
1470 * entries will be needed to store the remote node.
1471 */
Dan Williams88f3b622011-04-22 19:18:03 -07001472 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001473 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1474 sci_dev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001475 &sci_dev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001476
Dan Williamsa1a113b2011-04-21 18:44:45 -07001477 if (status != SCI_SUCCESS)
1478 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001479
Dan Williamsab2e8f72011-04-27 16:32:45 -07001480 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1481 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1482 /* pass */;
1483 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001484 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001485
Dan Williamsa1a113b2011-04-21 18:44:45 -07001486 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001487
Dan Williamsa1a113b2011-04-21 18:44:45 -07001488 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1489 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001490
Dan Williamsa1a113b2011-04-21 18:44:45 -07001491 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001492}
1493
Dan Williams88f3b622011-04-22 19:18:03 -07001494/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001495 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001496 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001497 * Remote node context(s) is/are a global resource allocated by this
1498 * routine, freed by scic_remote_device_destruct().
1499 *
1500 * Returns:
1501 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1502 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1503 * sata-only controller instance.
1504 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001505 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001506static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001507 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001508{
Dan Williamsa1a113b2011-04-21 18:44:45 -07001509 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001510 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001511
Dan Williamsb87ee302011-04-25 11:48:29 -07001512 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001513
Dan Williamsab2e8f72011-04-27 16:32:45 -07001514 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1515 sci_dev,
1516 &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001517 if (status != SCI_SUCCESS)
1518 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001519
Dan Williamsab2e8f72011-04-27 16:32:45 -07001520 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1521 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1522 /* pass */;
1523 else
1524 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001525
Dan Williamsa1a113b2011-04-21 18:44:45 -07001526 /*
1527 * For SAS-2 the physical link rate is actually a logical link
1528 * rate that incorporates multiplexing. The SCU doesn't
1529 * incorporate multiplexing and for the purposes of the
1530 * connection the logical link rate is that same as the
1531 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1532 * one another, so this code works for both situations. */
1533 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001534 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001535
Dan Williamsa1a113b2011-04-21 18:44:45 -07001536 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1537 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001538
Dan Williamsab2e8f72011-04-27 16:32:45 -07001539 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001540}
1541
1542/**
1543 * scic_remote_device_start() - This method will start the supplied remote
1544 * device. This method enables normal IO requests to flow through to the
1545 * remote device.
1546 * @remote_device: This parameter specifies the device to be started.
1547 * @timeout: This parameter specifies the number of milliseconds in which the
1548 * start operation should complete.
1549 *
1550 * An indication of whether the device was successfully started. SCI_SUCCESS
1551 * This value is returned if the device was successfully started.
1552 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1553 * the device when there have been no phys added to it.
1554 */
1555static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
Dan Williamseb229672011-05-01 14:05:57 -07001556 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001557{
Dan Williamseb229672011-05-01 14:05:57 -07001558 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1559 enum scic_sds_remote_device_states state = sm->current_state_id;
1560 enum sci_status status;
1561
1562 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1563 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1564 __func__, state);
1565 return SCI_FAILURE_INVALID_STATE;
1566 }
1567
1568 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1569 remote_device_resume_done,
1570 sci_dev);
1571 if (status != SCI_SUCCESS)
1572 return status;
1573
1574 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1575
1576 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001577}
Dan Williams6f231dd2011-07-02 22:56:22 -07001578
Dan Williams00d680e2011-04-25 14:29:29 -07001579static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1580 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001581{
Dan Williams00d680e2011-04-25 14:29:29 -07001582 struct scic_sds_port *sci_port = iport->sci_port_handle;
1583 struct isci_host *ihost = iport->isci_host;
1584 struct domain_device *dev = idev->domain_dev;
1585 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001586
Dan Williams00d680e2011-04-25 14:29:29 -07001587 if (dev->parent && dev_is_expander(dev->parent))
1588 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1589 else
1590 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001591
1592 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001593 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1594 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001595
1596 return status;
1597 }
1598
Dan Williams6f231dd2011-07-02 22:56:22 -07001599 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001600 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001601
Dan Williams00d680e2011-04-25 14:29:29 -07001602 if (status != SCI_SUCCESS)
1603 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1604 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001605
1606 return status;
1607}
1608
Dan Williams4393aa42011-03-31 13:10:44 -07001609void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001610{
1611 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001612
Dan Williams4393aa42011-03-31 13:10:44 -07001613 dev_dbg(&ihost->pdev->dev,
1614 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001615
1616 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -07001617 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -07001618
Dan Williams4393aa42011-03-31 13:10:44 -07001619 dev_dbg(&ihost->pdev->dev,
1620 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001621}
1622
Dan Williams6f231dd2011-07-02 22:56:22 -07001623/**
1624 * This function builds the isci_remote_device when a libsas dev_found message
1625 * is received.
1626 * @isci_host: This parameter specifies the isci host object.
1627 * @port: This parameter specifies the isci_port conected to this device.
1628 *
1629 * pointer to new isci_remote_device.
1630 */
1631static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001632isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001633{
Dan Williamsd9c37392011-03-03 17:59:32 -08001634 struct isci_remote_device *idev;
1635 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001636
Dan Williamsd9c37392011-03-03 17:59:32 -08001637 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001638 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001639 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1640 break;
1641 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001642
Dan Williamsd9c37392011-03-03 17:59:32 -08001643 if (i >= SCI_MAX_REMOTE_DEVICES) {
1644 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001645 return NULL;
1646 }
1647
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001648 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1649 return NULL;
1650
1651 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1652 return NULL;
1653
Dan Williamsd9c37392011-03-03 17:59:32 -08001654 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001655
Dan Williamsd9c37392011-03-03 17:59:32 -08001656 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001657}
Dan Williams6f231dd2011-07-02 22:56:22 -07001658
1659/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001660 * isci_remote_device_stop() - This function is called internally to stop the
1661 * remote device.
1662 * @isci_host: This parameter specifies the isci host object.
1663 * @isci_device: This parameter specifies the remote device.
1664 *
1665 * The status of the scic request to stop.
1666 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001667enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001668{
1669 enum sci_status status;
1670 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001671
Dan Williams6ad31fe2011-03-04 12:10:29 -08001672 dev_dbg(&ihost->pdev->dev,
1673 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001674
Dan Williams6ad31fe2011-03-04 12:10:29 -08001675 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001676
1677 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001678 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001679
Dan Williams6ad31fe2011-03-04 12:10:29 -08001680 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001681
Dan Williams6ad31fe2011-03-04 12:10:29 -08001682 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07001683 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001684 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001685
1686 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -08001687 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -08001688 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08001689 clear_bit(IDEV_ALLOCATED, &idev->flags);
1690 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001691
Dan Williams6ad31fe2011-03-04 12:10:29 -08001692 dev_dbg(&ihost->pdev->dev,
1693 "%s: idev = %p - after completion wait\n",
1694 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001695
Dan Williams6f231dd2011-07-02 22:56:22 -07001696 return status;
1697}
1698
1699/**
1700 * isci_remote_device_gone() - This function is called by libsas when a domain
1701 * device is removed.
1702 * @domain_device: This parameter specifies the libsas domain device.
1703 *
1704 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001705void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001706{
Dan Williams4393aa42011-03-31 13:10:44 -07001707 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001708 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001709
Dan Williams6ad31fe2011-03-04 12:10:29 -08001710 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001711 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001712 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001713
Dan Williams6ad31fe2011-03-04 12:10:29 -08001714 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001715}
1716
1717
1718/**
1719 * isci_remote_device_found() - This function is called by libsas when a remote
1720 * device is discovered. A remote device object is created and started. the
1721 * function then sleeps until the sci core device started message is
1722 * received.
1723 * @domain_device: This parameter specifies the libsas domain device.
1724 *
1725 * status, zero indicates success.
1726 */
1727int isci_remote_device_found(struct domain_device *domain_dev)
1728{
Dan Williams4393aa42011-03-31 13:10:44 -07001729 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001730 struct isci_port *isci_port;
1731 struct isci_phy *isci_phy;
1732 struct asd_sas_port *sas_port;
1733 struct asd_sas_phy *sas_phy;
1734 struct isci_remote_device *isci_device;
1735 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001736
Dan Williams6f231dd2011-07-02 22:56:22 -07001737 dev_dbg(&isci_host->pdev->dev,
1738 "%s: domain_device = %p\n", __func__, domain_dev);
1739
Dan Williams0cf89d12011-02-18 09:25:07 -08001740 wait_for_start(isci_host);
1741
Dan Williams6f231dd2011-07-02 22:56:22 -07001742 sas_port = domain_dev->port;
1743 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1744 port_phy_el);
1745 isci_phy = to_isci_phy(sas_phy);
1746 isci_port = isci_phy->isci_port;
1747
1748 /* we are being called for a device on this port,
1749 * so it has to come up eventually
1750 */
1751 wait_for_completion(&isci_port->start_complete);
1752
1753 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1754 (isci_stopped == isci_port_get_state(isci_port)))
1755 return -ENODEV;
1756
1757 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001758 if (!isci_device)
1759 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001760
1761 INIT_LIST_HEAD(&isci_device->node);
1762 domain_dev->lldd_dev = isci_device;
1763 isci_device->domain_dev = domain_dev;
1764 isci_device->isci_port = isci_port;
1765 isci_remote_device_change_state(isci_device, isci_starting);
1766
1767
Dan Williams1a380452011-03-03 18:01:43 -08001768 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001769 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1770
Dan Williams6ad31fe2011-03-04 12:10:29 -08001771 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001772 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -08001773 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001774
Dan Williams6f231dd2011-07-02 22:56:22 -07001775 dev_dbg(&isci_host->pdev->dev,
1776 "%s: isci_device = %p\n",
1777 __func__, isci_device);
1778
1779 if (status != SCI_SUCCESS) {
1780
Dan Williams1a380452011-03-03 18:01:43 -08001781 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001782 isci_remote_device_deconstruct(
1783 isci_host,
1784 isci_device
1785 );
Dan Williams1a380452011-03-03 18:01:43 -08001786 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001787 return -ENODEV;
1788 }
1789
Dan Williams6ad31fe2011-03-04 12:10:29 -08001790 /* wait for the device ready callback. */
1791 wait_for_device_start(isci_host, isci_device);
1792
Dan Williams6f231dd2011-07-02 22:56:22 -07001793 return 0;
1794}
1795/**
1796 * isci_device_is_reset_pending() - This function will check if there is any
1797 * pending reset condition on the device.
1798 * @request: This parameter is the isci_device object.
1799 *
1800 * true if there is a reset pending for the device.
1801 */
1802bool isci_device_is_reset_pending(
1803 struct isci_host *isci_host,
1804 struct isci_remote_device *isci_device)
1805{
1806 struct isci_request *isci_request;
1807 struct isci_request *tmp_req;
1808 bool reset_is_pending = false;
1809 unsigned long flags;
1810
1811 dev_dbg(&isci_host->pdev->dev,
1812 "%s: isci_device = %p\n", __func__, isci_device);
1813
1814 spin_lock_irqsave(&isci_host->scic_lock, flags);
1815
1816 /* Check for reset on all pending requests. */
1817 list_for_each_entry_safe(isci_request, tmp_req,
1818 &isci_device->reqs_in_process, dev_node) {
1819 dev_dbg(&isci_host->pdev->dev,
1820 "%s: isci_device = %p request = %p\n",
1821 __func__, isci_device, isci_request);
1822
1823 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001824 struct sas_task *task = isci_request_access_task(
1825 isci_request);
1826
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001827 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001828 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1829 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001830 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001831 }
1832 }
1833
1834 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1835
1836 dev_dbg(&isci_host->pdev->dev,
1837 "%s: isci_device = %p reset_is_pending = %d\n",
1838 __func__, isci_device, reset_is_pending);
1839
1840 return reset_is_pending;
1841}
1842
1843/**
1844 * isci_device_clear_reset_pending() - This function will clear if any pending
1845 * reset condition flags on the device.
1846 * @request: This parameter is the isci_device object.
1847 *
1848 * true if there is a reset pending for the device.
1849 */
Dan Williams4393aa42011-03-31 13:10:44 -07001850void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001851{
1852 struct isci_request *isci_request;
1853 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07001854 unsigned long flags = 0;
1855
Dan Williams4393aa42011-03-31 13:10:44 -07001856 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1857 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07001858
Dan Williams4393aa42011-03-31 13:10:44 -07001859 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001860
1861 /* Clear reset pending on all pending requests. */
1862 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07001863 &idev->reqs_in_process, dev_node) {
1864 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1865 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001866
1867 if (isci_request->ttype == io_task) {
1868
1869 unsigned long flags2;
1870 struct sas_task *task = isci_request_access_task(
1871 isci_request);
1872
1873 spin_lock_irqsave(&task->task_state_lock, flags2);
1874 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1875 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1876 }
1877 }
Dan Williams4393aa42011-03-31 13:10:44 -07001878 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001879}