blob: 3e7e95200a1e1e859020aa5c0a3b5fac9f7b3dbb [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
Dan Williams88f3b622011-04-22 19:18:03 -070055#include "intel_sas.h"
Dan Williamsab2e8f72011-04-27 16:32:45 -070056#include "intel_ata.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070058#include "port.h"
59#include "remote_device.h"
60#include "request.h"
Dan Williams88f3b622011-04-22 19:18:03 -070061#include "scic_controller.h"
62#include "scic_io_request.h"
63#include "scic_phy.h"
64#include "scic_port.h"
65#include "scic_sds_controller.h"
66#include "scic_sds_phy.h"
67#include "scic_sds_port.h"
68#include "remote_node_context.h"
69#include "scic_sds_request.h"
70#include "sci_environment.h"
71#include "sci_util.h"
72#include "scu_event_codes.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070073#include "task.h"
74
Dan Williamsab2e8f72011-04-27 16:32:45 -070075/**
76 * isci_remote_device_change_state() - This function gets the status of the
77 * remote_device object.
78 * @isci_device: This parameter points to the isci_remote_device object
79 *
80 * status of the object as a isci_status enum.
81 */
82void isci_remote_device_change_state(
83 struct isci_remote_device *isci_device,
84 enum isci_status status)
85{
86 unsigned long flags;
87
88 spin_lock_irqsave(&isci_device->state_lock, flags);
89 isci_device->status = status;
90 spin_unlock_irqrestore(&isci_device->state_lock, flags);
91}
92
93/**
94 * isci_remote_device_not_ready() - This function is called by the scic when
95 * the remote device is not ready. We mark the isci device as ready (not
96 * "ready_for_io") and signal the waiting proccess.
97 * @isci_host: This parameter specifies the isci host object.
98 * @isci_device: This parameter specifies the remote device
99 *
100 */
101static void isci_remote_device_not_ready(struct isci_host *ihost,
102 struct isci_remote_device *idev, u32 reason)
103{
104 dev_dbg(&ihost->pdev->dev,
105 "%s: isci_device = %p\n", __func__, idev);
106
107 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
108 isci_remote_device_change_state(idev, isci_stopping);
109 else
110 /* device ready is actually a "not ready for io" state. */
111 isci_remote_device_change_state(idev, isci_ready);
112}
113
114/**
115 * isci_remote_device_ready() - This function is called by the scic when the
116 * remote device is ready. We mark the isci device as ready and signal the
117 * waiting proccess.
118 * @ihost: our valid isci_host
119 * @idev: remote device
120 *
121 */
122static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
123{
124 dev_dbg(&ihost->pdev->dev,
125 "%s: idev = %p\n", __func__, idev);
126
127 isci_remote_device_change_state(idev, isci_ready_for_io);
128 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
129 wake_up(&ihost->eventq);
130}
131
Dan Williamsec575662011-05-01 14:19:25 -0700132/* called once the remote node context is ready to be freed.
133 * The remote device can now report that its stop operation is complete. none
134 */
135static void rnc_destruct_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700136{
Dan Williamsec575662011-05-01 14:19:25 -0700137 struct scic_sds_remote_device *sci_dev = _dev;
138
139 BUG_ON(sci_dev->started_request_count != 0);
140 sci_base_state_machine_change_state(&sci_dev->state_machine,
141 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
142}
143
144static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
145{
146 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
147 u32 i, request_count = sci_dev->started_request_count;
148 enum sci_status status = SCI_SUCCESS;
149
150 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
151 struct scic_sds_request *sci_req;
152 enum sci_status s;
153
154 sci_req = scic->io_request_table[i];
155 if (!sci_req || sci_req->target_device != sci_dev)
156 continue;
157 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
158 if (s != SCI_SUCCESS)
159 status = s;
160 }
161
162 return status;
163}
164
165enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
166 u32 timeout)
167{
168 struct sci_base_state_machine *sm = &sci_dev->state_machine;
169 enum scic_sds_remote_device_states state = sm->current_state_id;
170
171 switch (state) {
172 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
173 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
174 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
175 default:
176 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
177 __func__, state);
178 return SCI_FAILURE_INVALID_STATE;
179 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
180 return SCI_SUCCESS;
181 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
182 /* device not started so there had better be no requests */
183 BUG_ON(sci_dev->started_request_count != 0);
184 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
185 rnc_destruct_done, sci_dev);
186 /* Transition to the stopping state and wait for the
187 * remote node to complete being posted and invalidated.
188 */
189 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
190 return SCI_SUCCESS;
191 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
192 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
193 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
194 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
195 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
196 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
197 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
198 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
199 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
200 if (sci_dev->started_request_count == 0) {
201 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
202 rnc_destruct_done, sci_dev);
203 return SCI_SUCCESS;
204 } else
205 return scic_sds_remote_device_terminate_requests(sci_dev);
206 break;
207 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
208 /* All requests should have been terminated, but if there is an
209 * attempt to stop a device already in the stopping state, then
210 * try again to terminate.
211 */
212 return scic_sds_remote_device_terminate_requests(sci_dev);
213 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
214 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
215 return SCI_SUCCESS;
216 }
Dan Williams88f3b622011-04-22 19:18:03 -0700217}
Dan Williams6f231dd2011-07-02 22:56:22 -0700218
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700219enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700220{
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700221 struct sci_base_state_machine *sm = &sci_dev->state_machine;
222 enum scic_sds_remote_device_states state = sm->current_state_id;
223
224 switch (state) {
225 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
226 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
227 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
228 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
229 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
230 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
231 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
232 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
233 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
234 default:
235 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
236 __func__, state);
237 return SCI_FAILURE_INVALID_STATE;
238 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
239 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
240 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
241 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
242 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
243 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
244 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
245 return SCI_SUCCESS;
246 }
Dan Williams88f3b622011-04-22 19:18:03 -0700247}
248
Dan Williams81515182011-05-01 14:53:00 -0700249enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700250{
Dan Williams81515182011-05-01 14:53:00 -0700251 struct sci_base_state_machine *sm = &sci_dev->state_machine;
252 enum scic_sds_remote_device_states state = sm->current_state_id;
Dan Williams88f3b622011-04-22 19:18:03 -0700253
Dan Williams81515182011-05-01 14:53:00 -0700254 if (state != SCI_BASE_REMOTE_DEVICE_STATE_RESETTING) {
255 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
256 __func__, state);
257 return SCI_FAILURE_INVALID_STATE;
258 }
259
260 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_READY);
261 return SCI_SUCCESS;
262}
Dan Williams88f3b622011-04-22 19:18:03 -0700263
Dan Williams88f3b622011-04-22 19:18:03 -0700264/**
265 *
266 * @sci_dev: The remote device for which the suspend is being requested.
267 *
268 * This method invokes the remote device suspend state handler. enum sci_status
269 */
270enum sci_status scic_sds_remote_device_suspend(
271 struct scic_sds_remote_device *sci_dev,
272 u32 suspend_type)
273{
274 return sci_dev->state_handlers->suspend_handler(sci_dev, suspend_type);
275}
276
277/**
278 *
279 * @sci_dev: The remote device for which the event handling is being
280 * requested.
281 * @frame_index: This is the frame index that is being processed.
282 *
283 * This method invokes the frame handler for the remote device state machine
284 * enum sci_status
285 */
286enum sci_status scic_sds_remote_device_frame_handler(
287 struct scic_sds_remote_device *sci_dev,
288 u32 frame_index)
289{
290 return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
291}
292
293/**
294 *
295 * @sci_dev: The remote device for which the event handling is being
296 * requested.
297 * @event_code: This is the event code that is to be processed.
298 *
299 * This method invokes the remote device event handler. enum sci_status
300 */
301enum sci_status scic_sds_remote_device_event_handler(
302 struct scic_sds_remote_device *sci_dev,
303 u32 event_code)
304{
305 return sci_dev->state_handlers->event_handler(sci_dev, event_code);
306}
307
Dan Williams18606552011-05-01 14:57:11 -0700308static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
309 struct scic_sds_request *sci_req,
310 enum sci_status status)
Dan Williams88f3b622011-04-22 19:18:03 -0700311{
Dan Williams18606552011-05-01 14:57:11 -0700312 struct scic_sds_port *sci_port = sci_dev->owning_port;
313
314 /* cleanup requests that failed after starting on the port */
315 if (status != SCI_SUCCESS)
316 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
317 else
318 scic_sds_remote_device_increment_request_count(sci_dev);
319}
320
321enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
322 struct scic_sds_remote_device *sci_dev,
323 struct scic_sds_request *sci_req)
324{
325 struct sci_base_state_machine *sm = &sci_dev->state_machine;
326 enum scic_sds_remote_device_states state = sm->current_state_id;
327 struct scic_sds_port *sci_port = sci_dev->owning_port;
328 enum sci_status status;
329
330 switch (state) {
331 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
332 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
333 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
334 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
335 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
336 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
337 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
338 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
339 default:
340 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
341 __func__, state);
342 return SCI_FAILURE_INVALID_STATE;
343 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
344 /* attempt to start an io request for this device object. The remote
345 * device object will issue the start request for the io and if
346 * successful it will start the request for the port object then
347 * increment its own request count.
348 */
349 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
350 if (status != SCI_SUCCESS)
351 return status;
352
353 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
354 if (status != SCI_SUCCESS)
355 break;
356
357 status = scic_sds_request_start(sci_req);
358 break;
359 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
360 /* handle the start io operation for a sata device that is in
361 * the command idle state. - Evalute the type of IO request to
362 * be started - If its an NCQ request change to NCQ substate -
363 * If its any other command change to the CMD substate
364 *
365 * If this is a softreset we may want to have a different
366 * substate.
367 */
368 enum scic_sds_remote_device_states new_state;
369
370 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
371 if (status != SCI_SUCCESS)
372 return status;
373
374 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
375 if (status != SCI_SUCCESS)
376 break;
377
378 status = sci_req->state_handlers->start_handler(sci_req);
379 if (status != SCI_SUCCESS)
380 break;
381
382 if (isci_sata_get_sat_protocol(sci_req->ireq) == SAT_PROTOCOL_FPDMA)
383 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
384 else {
385 sci_dev->working_request = sci_req;
386 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
387 }
388 sci_base_state_machine_change_state(sm, new_state);
389 break;
390 }
391 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
392 if (isci_sata_get_sat_protocol(sci_req->ireq) == SAT_PROTOCOL_FPDMA) {
393 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
394 if (status != SCI_SUCCESS)
395 return status;
396
397 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
398 if (status != SCI_SUCCESS)
399 break;
400
401 status = sci_req->state_handlers->start_handler(sci_req);
402 } else
403 return SCI_FAILURE_INVALID_STATE;
404 break;
405 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
406 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
407 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
408 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
409 if (status != SCI_SUCCESS)
410 return status;
411
412 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
413 if (status != SCI_SUCCESS)
414 break;
415
416 status = scic_sds_request_start(sci_req);
417 if (status != SCI_SUCCESS)
418 break;
419
420 sci_dev->working_request = sci_req;
421 sci_base_state_machine_change_state(&sci_dev->state_machine,
422 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
423 break;
424 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
425 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
426 /* device is already handling a command it can not accept new commands
427 * until this one is complete.
428 */
429 return SCI_FAILURE_INVALID_STATE;
430 }
431
432 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
433 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700434}
435
Dan Williams10a09e62011-05-01 15:33:43 -0700436static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
437 struct scic_sds_remote_device *sci_dev,
438 struct scic_sds_request *sci_req)
Dan Williams88f3b622011-04-22 19:18:03 -0700439{
Dan Williams10a09e62011-05-01 15:33:43 -0700440 enum sci_status status;
441
442 status = scic_sds_request_complete(sci_req);
443 if (status != SCI_SUCCESS)
444 return status;
445
446 status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
447 if (status != SCI_SUCCESS)
448 return status;
449
450 scic_sds_remote_device_decrement_request_count(sci_dev);
451 return status;
452}
453
454enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
455 struct scic_sds_remote_device *sci_dev,
456 struct scic_sds_request *sci_req)
457{
458 struct sci_base_state_machine *sm = &sci_dev->state_machine;
459 enum scic_sds_remote_device_states state = sm->current_state_id;
460 struct scic_sds_port *sci_port = sci_dev->owning_port;
461 enum sci_status status;
462
463 switch (state) {
464 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
465 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
466 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
467 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
468 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
469 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
470 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
471 default:
472 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
473 __func__, state);
474 return SCI_FAILURE_INVALID_STATE;
475 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
476 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
477 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
478 status = common_complete_io(sci_port, sci_dev, sci_req);
479 break;
480 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
481 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
482 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
483 status = common_complete_io(sci_port, sci_dev, sci_req);
484 if (status != SCI_SUCCESS)
485 break;
486
487 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
488 /* This request causes hardware error, device needs to be Lun Reset.
489 * So here we force the state machine to IDLE state so the rest IOs
490 * can reach RNC state handler, these IOs will be completed by RNC with
491 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
492 */
493 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
494 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
495 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
496 break;
497 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
498 status = common_complete_io(sci_port, sci_dev, sci_req);
499 if (status != SCI_SUCCESS)
500 break;
501 sci_base_state_machine_change_state(sm, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
502 break;
503 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
504 status = common_complete_io(sci_port, sci_dev, sci_req);
505 if (status != SCI_SUCCESS)
506 break;
507
508 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
509 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
510 rnc_destruct_done,
511 sci_dev);
512 break;
513 }
514
515 if (status != SCI_SUCCESS)
516 dev_err(scirdev_to_dev(sci_dev),
517 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
518 "could not complete\n", __func__, sci_port,
519 sci_dev, sci_req, status);
520
521 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700522}
523
Dan Williams84b9b022011-05-01 15:53:25 -0700524static void scic_sds_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700525{
Dan Williams84b9b022011-05-01 15:53:25 -0700526 struct scic_sds_remote_device *sci_dev = dev;
527
528 /* we need to check if this request is still valid to continue. */
529 if (sci_dev->working_request)
530 scic_controller_continue_io(sci_dev->working_request);
531}
532
533enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
534 struct scic_sds_remote_device *sci_dev,
535 struct scic_sds_request *sci_req)
536{
537 struct sci_base_state_machine *sm = &sci_dev->state_machine;
538 enum scic_sds_remote_device_states state = sm->current_state_id;
539 struct scic_sds_port *sci_port = sci_dev->owning_port;
540 enum sci_status status;
541
542 switch (state) {
543 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
544 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
545 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
546 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
547 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
548 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
549 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
550 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
551 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
552 default:
553 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
554 __func__, state);
555 return SCI_FAILURE_INVALID_STATE;
556 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
557 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
558 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
559 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
560 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
561 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
562 if (status != SCI_SUCCESS)
563 return status;
564
565 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
566 if (status != SCI_SUCCESS)
567 goto out;
568
569 status = sci_req->state_handlers->start_handler(sci_req);
570 if (status != SCI_SUCCESS)
571 goto out;
572
573 /* Note: If the remote device state is not IDLE this will
574 * replace the request that probably resulted in the task
575 * management request.
576 */
577 sci_dev->working_request = sci_req;
578 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
579
580 /* The remote node context must cleanup the TCi to NCQ mapping
581 * table. The only way to do this correctly is to either write
582 * to the TLCR register or to invalidate and repost the RNC. In
583 * either case the remote node context state machine will take
584 * the correct action when the remote node context is suspended
585 * and later resumed.
586 */
587 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
588 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
589 scic_sds_remote_node_context_resume(&sci_dev->rnc,
590 scic_sds_remote_device_continue_request,
591 sci_dev);
592
593 out:
594 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
595 /* We need to let the controller start request handler know that
596 * it can't post TC yet. We will provide a callback function to
597 * post TC when RNC gets resumed.
598 */
599 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
600 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
601 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
602 if (status != SCI_SUCCESS)
603 return status;
604
605 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
606 if (status != SCI_SUCCESS)
607 break;
608
609 status = scic_sds_request_start(sci_req);
610 break;
611 }
612 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
613
614 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700615}
616
617/**
618 *
Dan Williams88f3b622011-04-22 19:18:03 -0700619 * @sci_dev:
620 * @request:
621 *
622 * This method takes the request and bulids an appropriate SCU context for the
623 * request and then requests the controller to post the request. none
624 */
625void scic_sds_remote_device_post_request(
626 struct scic_sds_remote_device *sci_dev,
627 u32 request)
628{
629 u32 context;
630
631 context = scic_sds_remote_device_build_command_context(sci_dev, request);
632
633 scic_sds_controller_post_request(
634 scic_sds_remote_device_get_controller(sci_dev),
635 context
636 );
637}
638
Dan Williamsab2e8f72011-04-27 16:32:45 -0700639/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700640 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700641 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700642 */
Dan Williamseb229672011-05-01 14:05:57 -0700643static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700644{
Dan Williamsab2e8f72011-04-27 16:32:45 -0700645 struct scic_sds_remote_device *sci_dev = _dev;
646 enum scic_sds_remote_device_states state;
Dan Williams88f3b622011-04-22 19:18:03 -0700647
Dan Williamsab2e8f72011-04-27 16:32:45 -0700648 state = sci_dev->state_machine.current_state_id;
649 switch (state) {
650 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
651 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
652 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
653 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
654 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
655 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
656 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
657 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
658 break;
659 default:
660 /* go 'ready' if we are not already in a ready state */
661 sci_base_state_machine_change_state(&sci_dev->state_machine,
662 SCI_BASE_REMOTE_DEVICE_STATE_READY);
663 break;
Dan Williams88f3b622011-04-22 19:18:03 -0700664 }
665}
666
Dan Williams88f3b622011-04-22 19:18:03 -0700667static enum sci_status
668default_device_handler(struct scic_sds_remote_device *sci_dev,
669 const char *func)
670{
671 dev_warn(scirdev_to_dev(sci_dev),
672 "%s: in wrong state: %d\n", func,
673 sci_base_state_machine_get_state(&sci_dev->state_machine));
674 return SCI_FAILURE_INVALID_STATE;
675}
676
Dan Williamsab2e8f72011-04-27 16:32:45 -0700677static enum sci_status scic_sds_remote_device_default_suspend_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700678 struct scic_sds_remote_device *sci_dev, u32 suspend_type)
679{
680 return default_device_handler(sci_dev, __func__);
681}
682
Dan Williamsab2e8f72011-04-27 16:32:45 -0700683static enum sci_status scic_sds_remote_device_default_resume_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700684 struct scic_sds_remote_device *sci_dev)
685{
686 return default_device_handler(sci_dev, __func__);
687}
688
689/**
690 *
691 * @device: The struct scic_sds_remote_device which is then cast into a
692 * struct scic_sds_remote_device.
693 * @event_code: The event code that the struct scic_sds_controller wants the device
694 * object to process.
695 *
696 * This method is the default event handler. It will call the RNC state
697 * machine handler for any RNC events otherwise it will log a warning and
698 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
699 */
700static enum sci_status scic_sds_remote_device_core_event_handler(
701 struct scic_sds_remote_device *sci_dev,
702 u32 event_code,
703 bool is_ready_state)
704{
705 enum sci_status status;
706
707 switch (scu_get_event_type(event_code)) {
708 case SCU_EVENT_TYPE_RNC_OPS_MISC:
709 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
710 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
711 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
712 break;
713 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
714
715 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
716 status = SCI_SUCCESS;
717
718 /* Suspend the associated RNC */
719 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
720 SCI_SOFTWARE_SUSPENSION,
721 NULL, NULL);
722
723 dev_dbg(scirdev_to_dev(sci_dev),
724 "%s: device: %p event code: %x: %s\n",
725 __func__, sci_dev, event_code,
726 (is_ready_state)
727 ? "I_T_Nexus_Timeout event"
728 : "I_T_Nexus_Timeout event in wrong state");
729
730 break;
731 }
732 /* Else, fall through and treat as unhandled... */
733
734 default:
735 dev_dbg(scirdev_to_dev(sci_dev),
736 "%s: device: %p event code: %x: %s\n",
737 __func__, sci_dev, event_code,
738 (is_ready_state)
739 ? "unexpected event"
740 : "unexpected event in wrong state");
741 status = SCI_FAILURE_INVALID_STATE;
742 break;
743 }
744
745 return status;
746}
747/**
748 *
749 * @device: The struct scic_sds_remote_device which is then cast into a
750 * struct scic_sds_remote_device.
751 * @event_code: The event code that the struct scic_sds_controller wants the device
752 * object to process.
753 *
754 * This method is the default event handler. It will call the RNC state
755 * machine handler for any RNC events otherwise it will log a warning and
756 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
757 */
758static enum sci_status scic_sds_remote_device_default_event_handler(
759 struct scic_sds_remote_device *sci_dev,
760 u32 event_code)
761{
762 return scic_sds_remote_device_core_event_handler(sci_dev,
763 event_code,
764 false);
765}
766
767/**
768 *
769 * @device: The struct scic_sds_remote_device which is then cast into a
770 * struct scic_sds_remote_device.
771 * @frame_index: The frame index for which the struct scic_sds_controller wants this
772 * device object to process.
773 *
774 * This method is the default unsolicited frame handler. It logs a warning,
775 * releases the frame and returns a failure. enum sci_status
776 * SCI_FAILURE_INVALID_STATE
777 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700778static enum sci_status scic_sds_remote_device_default_frame_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700779 struct scic_sds_remote_device *sci_dev,
780 u32 frame_index)
781{
782 dev_warn(scirdev_to_dev(sci_dev),
783 "%s: SCIC Remote Device requested to handle frame %x "
784 "while in wrong state %d\n",
785 __func__,
786 frame_index,
787 sci_base_state_machine_get_state(
788 &sci_dev->state_machine));
789
790 /* Return the frame back to the controller */
791 scic_sds_controller_release_frame(
792 scic_sds_remote_device_get_controller(sci_dev), frame_index
793 );
794
795 return SCI_FAILURE_INVALID_STATE;
796}
797
Dan Williamsab2e8f72011-04-27 16:32:45 -0700798static enum sci_status scic_sds_remote_device_default_complete_request_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700799 struct scic_sds_remote_device *sci_dev,
800 struct scic_sds_request *request)
801{
802 return default_device_handler(sci_dev, __func__);
803}
804
Dan Williams88f3b622011-04-22 19:18:03 -0700805/**
806 *
807 * @device: The struct scic_sds_remote_device which is then cast into a
808 * struct scic_sds_remote_device.
809 * @frame_index: The frame index for which the struct scic_sds_controller wants this
810 * device object to process.
811 *
812 * This method is a general ssp frame handler. In most cases the device object
813 * needs to route the unsolicited frame processing to the io request object.
814 * This method decodes the tag for the io request object and routes the
815 * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
816 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700817static enum sci_status scic_sds_remote_device_general_frame_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700818 struct scic_sds_remote_device *sci_dev,
819 u32 frame_index)
820{
821 enum sci_status result;
822 struct sci_ssp_frame_header *frame_header;
823 struct scic_sds_request *io_request;
824
825 result = scic_sds_unsolicited_frame_control_get_header(
826 &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
827 frame_index,
828 (void **)&frame_header
829 );
830
831 if (SCI_SUCCESS == result) {
832 io_request = scic_sds_controller_get_io_request_from_tag(
833 scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
834
835 if ((io_request == NULL)
836 || (io_request->target_device != sci_dev)) {
837 /*
838 * We could not map this tag to a valid IO request
839 * Just toss the frame and continue */
840 scic_sds_controller_release_frame(
841 scic_sds_remote_device_get_controller(sci_dev), frame_index
842 );
843 } else {
844 /* The IO request is now in charge of releasing the frame */
845 result = io_request->state_handlers->frame_handler(
846 io_request, frame_index);
847 }
848 }
849
850 return result;
851}
852
853/**
854 *
855 * @[in]: sci_dev This is the device object that is receiving the event.
856 * @[in]: event_code The event code to process.
857 *
858 * This is a common method for handling events reported to the remote device
859 * from the controller object. enum sci_status
860 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700861static enum sci_status scic_sds_remote_device_general_event_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700862 struct scic_sds_remote_device *sci_dev,
863 u32 event_code)
864{
865 return scic_sds_remote_device_core_event_handler(sci_dev,
866 event_code,
867 true);
868}
869
Dan Williams88f3b622011-04-22 19:18:03 -0700870/*
Dan Williams88f3b622011-04-22 19:18:03 -0700871 * This method will complete the request for the remote device object. The
872 * method will call the completion handler for the request object and if
873 * successful it will complete the request on the port object then decrement
874 * its own started_request_count. enum sci_status
875 */
876static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
877 struct scic_sds_remote_device *sci_dev,
878 struct scic_sds_request *request)
879{
880 enum sci_status result;
881
882 result = scic_sds_request_complete(request);
883
884 if (result != SCI_SUCCESS)
885 return result;
886
887 /* See if the port is in a state
888 * where we can start the IO request */
889 result = scic_sds_port_complete_io(
890 scic_sds_remote_device_get_port(sci_dev),
891 sci_dev, request);
892
893 if (result == SCI_SUCCESS)
894 scic_sds_remote_device_decrement_request_count(sci_dev);
895
896 return result;
897}
898
Dan Williams88f3b622011-04-22 19:18:03 -0700899/**
900 *
901 * @device: The device object for which the request is completing.
902 * @request: The task request that is being completed.
903 *
904 * This method completes requests for this struct scic_sds_remote_device while it is
905 * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
906 * complete method for the request object and if that is successful the port
907 * object is called to complete the task request. Then the device object itself
908 * completes the task request. If struct scic_sds_remote_device started_request_count
909 * goes to 0 and the invalidate RNC request has completed the device object can
910 * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
911 */
912static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
913 struct scic_sds_remote_device *sci_dev,
914 struct scic_sds_request *request)
915{
916 enum sci_status status = SCI_SUCCESS;
917
918 status = scic_sds_request_complete(request);
919
920 if (status != SCI_SUCCESS)
921 return status;
922
923 status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
924 sci_dev, request);
925 if (status != SCI_SUCCESS)
926 return status;
927
928 scic_sds_remote_device_decrement_request_count(sci_dev);
929
930 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
931 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
Dan Williamsec575662011-05-01 14:19:25 -0700932 rnc_destruct_done, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700933 return SCI_SUCCESS;
934}
935
Dan Williamsab2e8f72011-04-27 16:32:45 -0700936/* complete requests for this device while it is in the
937 * SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the complete
938 * method for the request object and if that is successful the port object is
939 * called to complete the task request. Then the device object itself completes
940 * the task request. enum sci_status
Dan Williams88f3b622011-04-22 19:18:03 -0700941 */
942static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
943 struct scic_sds_remote_device *sci_dev,
944 struct scic_sds_request *request)
945{
946 enum sci_status status = SCI_SUCCESS;
947
948 status = scic_sds_request_complete(request);
949
950 if (status == SCI_SUCCESS) {
951 status = scic_sds_port_complete_io(
952 scic_sds_remote_device_get_port(sci_dev),
953 sci_dev, request);
954
955 if (status == SCI_SUCCESS) {
956 scic_sds_remote_device_decrement_request_count(sci_dev);
957 }
958 }
959
960 return status;
961}
962
Dan Williamsab2e8f72011-04-27 16:32:45 -0700963static enum sci_status scic_sds_stp_remote_device_complete_request(struct scic_sds_remote_device *sci_dev,
964 struct scic_sds_request *sci_req)
965{
966 enum sci_status status;
967
968 status = scic_sds_io_request_complete(sci_req);
969 if (status != SCI_SUCCESS)
970 goto out;
971
972 status = scic_sds_port_complete_io(sci_dev->owning_port, sci_dev, sci_req);
973 if (status != SCI_SUCCESS)
974 goto out;
975
976 scic_sds_remote_device_decrement_request_count(sci_dev);
977 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
978 /* This request causes hardware error, device needs to be Lun Reset.
979 * So here we force the state machine to IDLE state so the rest IOs
980 * can reach RNC state handler, these IOs will be completed by RNC with
981 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
982 */
983 sci_base_state_machine_change_state(&sci_dev->state_machine,
984 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
985 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
986 sci_base_state_machine_change_state(&sci_dev->state_machine,
987 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
988
989
990 out:
991 if (status != SCI_SUCCESS)
992 dev_err(scirdev_to_dev(sci_dev),
993 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
994 "could not complete\n", __func__, sci_dev->owning_port,
995 sci_dev, sci_req, status);
996
997 return status;
998}
999
Dan Williamsab2e8f72011-04-27 16:32:45 -07001000static enum sci_status scic_sds_stp_remote_device_ready_idle_substate_event_handler(
1001 struct scic_sds_remote_device *sci_dev,
1002 u32 event_code)
1003{
1004 enum sci_status status;
1005
1006 status = scic_sds_remote_device_general_event_handler(sci_dev, event_code);
1007 if (status != SCI_SUCCESS)
1008 return status;
1009
1010 /* We pick up suspension events to handle specifically to this state. We
1011 * resume the RNC right away. enum sci_status
1012 */
1013 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
1014 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
1015 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1016
1017 return status;
1018}
1019
Dan Williamsab2e8f72011-04-27 16:32:45 -07001020static 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
Dan Williamsab2e8f72011-04-27 16:32:45 -07001059static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler(
1060 struct scic_sds_remote_device *sci_dev,
1061 u32 suspend_type)
1062{
1063 enum sci_status status;
1064
1065 status = scic_sds_remote_node_context_suspend(&sci_dev->rnc,
1066 suspend_type, NULL, NULL);
1067
1068 return status;
1069}
1070
1071static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_frame_handler(
1072 struct scic_sds_remote_device *sci_dev,
1073 u32 frame_index)
1074{
1075 /* The device doe not process any UF received from the hardware while
1076 * in this state. All unsolicited frames are forwarded to the io
1077 * request object.
1078 */
1079 return scic_sds_io_request_frame_handler(sci_dev->working_request,
1080 frame_index);
1081}
1082
Dan Williamsab2e8f72011-04-27 16:32:45 -07001083static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
1084{
1085 struct scic_sds_remote_device *sci_dev = _dev;
1086 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1087 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1088
1089 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
1090 * As a result, avoid sending the ready notification.
1091 */
1092 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
1093 isci_remote_device_ready(scic->ihost, idev);
1094}
1095
Dan Williamsab2e8f72011-04-27 16:32:45 -07001096static enum sci_status scic_sds_smp_remote_device_ready_cmd_substate_frame_handler(
1097 struct scic_sds_remote_device *sci_dev,
1098 u32 frame_index)
1099{
1100 enum sci_status status;
1101
1102 /* The device does not process any UF received from the hardware while
1103 * in this state. All unsolicited frames are forwarded to the io request
1104 * object.
1105 */
1106 status = scic_sds_io_request_frame_handler(
1107 sci_dev->working_request,
1108 frame_index
1109 );
1110
1111 return status;
1112}
1113
Dan Williams88f3b622011-04-22 19:18:03 -07001114static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
1115 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001116 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1117 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1118 .resume_handler = scic_sds_remote_device_default_resume_handler,
1119 .event_handler = scic_sds_remote_device_default_event_handler,
1120 .frame_handler = scic_sds_remote_device_default_frame_handler
1121 },
1122 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001123 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1124 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1125 .resume_handler = scic_sds_remote_device_default_resume_handler,
1126 .event_handler = scic_sds_remote_device_default_event_handler,
1127 .frame_handler = scic_sds_remote_device_default_frame_handler
1128 },
1129 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001130 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1131 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1132 .resume_handler = scic_sds_remote_device_default_resume_handler,
1133 .event_handler = scic_sds_remote_device_general_event_handler,
1134 .frame_handler = scic_sds_remote_device_default_frame_handler
1135 },
1136 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001137 .complete_task_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1138 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1139 .resume_handler = scic_sds_remote_device_default_resume_handler,
1140 .event_handler = scic_sds_remote_device_general_event_handler,
1141 .frame_handler = scic_sds_remote_device_general_frame_handler,
1142 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001143 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001144 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1145 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1146 .resume_handler = scic_sds_remote_device_default_resume_handler,
1147 .event_handler = scic_sds_stp_remote_device_ready_idle_substate_event_handler,
1148 .frame_handler = scic_sds_remote_device_default_frame_handler
1149 },
1150 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001151 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1152 .suspend_handler = scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler,
1153 .resume_handler = scic_sds_remote_device_default_resume_handler,
1154 .event_handler = scic_sds_remote_device_general_event_handler,
1155 .frame_handler = scic_sds_stp_remote_device_ready_cmd_substate_frame_handler
1156 },
1157 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001158 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1159 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1160 .resume_handler = scic_sds_remote_device_default_resume_handler,
1161 .event_handler = scic_sds_remote_device_general_event_handler,
1162 .frame_handler = scic_sds_stp_remote_device_ready_ncq_substate_frame_handler
1163 },
1164 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001165 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1166 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1167 .resume_handler = scic_sds_remote_device_default_resume_handler,
1168 .event_handler = scic_sds_remote_device_general_event_handler,
1169 .frame_handler = scic_sds_remote_device_general_frame_handler
1170 },
1171 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001172 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1173 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1174 .resume_handler = scic_sds_remote_device_default_resume_handler,
1175 .event_handler = scic_sds_remote_device_general_event_handler,
1176 .frame_handler = scic_sds_remote_device_general_frame_handler
1177 },
1178 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001179 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1180 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1181 .resume_handler = scic_sds_remote_device_default_resume_handler,
1182 .event_handler = scic_sds_remote_device_general_event_handler,
1183 .frame_handler = scic_sds_remote_device_default_frame_handler
1184 },
1185 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001186 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1187 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1188 .resume_handler = scic_sds_remote_device_default_resume_handler,
1189 .event_handler = scic_sds_remote_device_general_event_handler,
1190 .frame_handler = scic_sds_smp_remote_device_ready_cmd_substate_frame_handler
1191 },
Dan Williams88f3b622011-04-22 19:18:03 -07001192 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001193 .complete_task_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
1194 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1195 .resume_handler = scic_sds_remote_device_default_resume_handler,
1196 .event_handler = scic_sds_remote_device_general_event_handler,
1197 .frame_handler = scic_sds_remote_device_general_frame_handler
1198 },
1199 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001200 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1201 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1202 .resume_handler = scic_sds_remote_device_default_resume_handler,
1203 .event_handler = scic_sds_remote_device_default_event_handler,
1204 .frame_handler = scic_sds_remote_device_general_frame_handler
1205 },
1206 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001207 .complete_task_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1208 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1209 .resume_handler = scic_sds_remote_device_default_resume_handler,
1210 .event_handler = scic_sds_remote_device_default_event_handler,
1211 .frame_handler = scic_sds_remote_device_general_frame_handler
1212 },
1213 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001214 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1215 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1216 .resume_handler = scic_sds_remote_device_default_resume_handler,
1217 .event_handler = scic_sds_remote_device_default_event_handler,
1218 .frame_handler = scic_sds_remote_device_default_frame_handler
1219 }
1220};
1221
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001222static void scic_sds_remote_device_initial_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001223{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001224 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001225
Dan Williams88f3b622011-04-22 19:18:03 -07001226 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1227 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1228
1229 /* Initial state is a transitional state to the stopped state */
1230 sci_base_state_machine_change_state(&sci_dev->state_machine,
1231 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1232}
1233
1234/**
Dan Williams88f3b622011-04-22 19:18:03 -07001235 * scic_remote_device_destruct() - free remote node context and destruct
1236 * @remote_device: This parameter specifies the remote device to be destructed.
1237 *
1238 * Remote device objects are a limited resource. As such, they must be
1239 * protected. Thus calls to construct and destruct are mutually exclusive and
1240 * non-reentrant. The return value shall indicate if the device was
1241 * successfully destructed or if some failure occurred. enum sci_status This value
1242 * is returned if the device is successfully destructed.
1243 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
1244 * device isn't valid (e.g. it's already been destoryed, the handle isn't
1245 * valid, etc.).
1246 */
1247static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
1248{
Dan Williamsb8d82f62011-05-01 14:38:26 -07001249 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1250 enum scic_sds_remote_device_states state = sm->current_state_id;
1251 struct scic_sds_controller *scic;
1252
1253 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1254 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1255 __func__, state);
1256 return SCI_FAILURE_INVALID_STATE;
1257 }
1258
1259 scic = sci_dev->owning_port->owning_controller;
1260 scic_sds_controller_free_remote_node_context(scic, sci_dev,
1261 sci_dev->rnc.remote_node_index);
1262 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
1263 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
1264
1265 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001266}
1267
Dan Williams6f231dd2011-07-02 22:56:22 -07001268/**
1269 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -08001270 * @ihost: This parameter specifies the isci host object.
1271 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -07001272 *
1273 */
Dan Williamsd9c37392011-03-03 17:59:32 -08001274static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001275{
Dan Williamsd9c37392011-03-03 17:59:32 -08001276 dev_dbg(&ihost->pdev->dev,
1277 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001278
1279 /* There should not be any outstanding io's. All paths to
1280 * here should go through isci_remote_device_nuke_requests.
1281 * If we hit this condition, we will need a way to complete
1282 * io requests in process */
Dan Williamsd9c37392011-03-03 17:59:32 -08001283 while (!list_empty(&idev->reqs_in_process)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001284
Dan Williamsd9c37392011-03-03 17:59:32 -08001285 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001286 "%s: ** request list not empty! **\n", __func__);
1287 BUG();
1288 }
1289
Dan Williams57f20f42011-04-21 18:14:45 -07001290 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -08001291 idev->domain_dev->lldd_dev = NULL;
1292 idev->domain_dev = NULL;
1293 idev->isci_port = NULL;
1294 list_del_init(&idev->node);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001295
Dan Williamsd9c37392011-03-03 17:59:32 -08001296 clear_bit(IDEV_START_PENDING, &idev->flags);
1297 clear_bit(IDEV_STOP_PENDING, &idev->flags);
1298 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001299}
1300
Dan Williams88f3b622011-04-22 19:18:03 -07001301/**
1302 * isci_remote_device_stop_complete() - This function is called by the scic
1303 * when the remote device stop has completed. We mark the isci device as not
1304 * ready and remove the isci remote device.
1305 * @ihost: This parameter specifies the isci host object.
1306 * @idev: This parameter specifies the remote device.
1307 * @status: This parameter specifies status of the completion.
1308 *
1309 */
1310static void isci_remote_device_stop_complete(struct isci_host *ihost,
1311 struct isci_remote_device *idev)
1312{
1313 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1314
1315 isci_remote_device_change_state(idev, isci_stopped);
1316
1317 /* after stop, we can tear down resources. */
1318 isci_remote_device_deconstruct(ihost, idev);
1319}
1320
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001321static void scic_sds_remote_device_stopped_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001322{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001323 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001324 struct scic_sds_controller *scic;
1325 struct isci_remote_device *idev;
1326 struct isci_host *ihost;
1327 u32 prev_state;
1328
Dan Williams88f3b622011-04-22 19:18:03 -07001329 scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +00001330 ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001331 idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001332
1333 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1334 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1335
1336 /* If we are entering from the stopping state let the SCI User know that
1337 * the stop operation has completed.
1338 */
1339 prev_state = sci_dev->state_machine.previous_state_id;
1340 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1341 isci_remote_device_stop_complete(ihost, idev);
1342
1343 scic_sds_controller_remote_device_stopped(scic, sci_dev);
1344}
1345
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001346static void scic_sds_remote_device_starting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001347{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001348 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001349 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +00001350 struct isci_host *ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001351 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001352
1353 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1354 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1355
1356 isci_remote_device_not_ready(ihost, idev,
1357 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1358}
1359
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001360static void scic_sds_remote_device_ready_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001361{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001362 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001363 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1364 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001365
1366 SET_STATE_HANDLER(sci_dev,
1367 scic_sds_remote_device_state_handler_table,
1368 SCI_BASE_REMOTE_DEVICE_STATE_READY);
1369
1370 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1371
Dan Williamsab2e8f72011-04-27 16:32:45 -07001372 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
1373 sci_base_state_machine_change_state(&sci_dev->state_machine,
1374 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1375 } else if (dev_is_expander(dev)) {
1376 sci_base_state_machine_change_state(&sci_dev->state_machine,
1377 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1378 } else
1379 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
Dan Williams88f3b622011-04-22 19:18:03 -07001380}
1381
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001382static void scic_sds_remote_device_ready_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001383{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001384 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001385 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1386
1387 if (dev->dev_type == SAS_END_DEV) {
1388 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001389 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001390
Dan Williamsab2e8f72011-04-27 16:32:45 -07001391 isci_remote_device_not_ready(scic->ihost, idev,
Dan Williams88f3b622011-04-22 19:18:03 -07001392 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1393 }
1394}
1395
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001396static void scic_sds_remote_device_stopping_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001397{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001398 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001399
1400 SET_STATE_HANDLER(
1401 sci_dev,
1402 scic_sds_remote_device_state_handler_table,
1403 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1404 );
1405}
1406
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001407static void scic_sds_remote_device_failed_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
1411 SET_STATE_HANDLER(
1412 sci_dev,
1413 scic_sds_remote_device_state_handler_table,
1414 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1415 );
1416}
1417
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001418static void scic_sds_remote_device_resetting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001419{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001420 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001421
1422 SET_STATE_HANDLER(
1423 sci_dev,
1424 scic_sds_remote_device_state_handler_table,
1425 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1426 );
1427
1428 scic_sds_remote_node_context_suspend(
1429 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1430}
1431
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001432static void scic_sds_remote_device_resetting_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001433{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001434 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001435
1436 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1437}
1438
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001439static void scic_sds_remote_device_final_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001440{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001441 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001442
1443 SET_STATE_HANDLER(
1444 sci_dev,
1445 scic_sds_remote_device_state_handler_table,
1446 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1447 );
1448}
1449
Dan Williamsab2e8f72011-04-27 16:32:45 -07001450static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
1451{
1452 struct scic_sds_remote_device *sci_dev = object;
1453
1454 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1455 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1456
1457 sci_dev->working_request = NULL;
1458 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
1459 /*
1460 * Since the RNC is ready, it's alright to finish completion
1461 * processing (e.g. signal the remote device is ready). */
1462 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
1463 } else {
1464 scic_sds_remote_node_context_resume(&sci_dev->rnc,
1465 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
1466 sci_dev);
1467 }
1468}
1469
1470static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1471{
1472 struct scic_sds_remote_device *sci_dev = object;
1473 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1474
1475 BUG_ON(sci_dev->working_request == NULL);
1476
1477 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1478 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1479
1480 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1481 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1482}
1483
1484static void scic_sds_stp_remote_device_ready_ncq_substate_enter(void *object)
1485{
1486 struct scic_sds_remote_device *sci_dev = object;
1487
1488 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1489 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ);
1490}
1491
1492static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1493{
1494 struct scic_sds_remote_device *sci_dev = object;
1495 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1496 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1497
1498 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1499 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1500
1501 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1502 isci_remote_device_not_ready(scic->ihost, idev,
1503 sci_dev->not_ready_reason);
1504}
1505
1506static void scic_sds_stp_remote_device_ready_await_reset_substate_enter(void *object)
1507{
1508 struct scic_sds_remote_device *sci_dev = object;
1509
1510 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1511 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
1512}
1513
1514static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1515{
1516 struct scic_sds_remote_device *sci_dev = object;
1517 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1518
1519 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1520 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1521
1522 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1523}
1524
1525static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1526{
1527 struct scic_sds_remote_device *sci_dev = object;
1528 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1529
1530 BUG_ON(sci_dev->working_request == NULL);
1531
1532 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1533 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1534
1535 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1536 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1537}
1538
1539static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1540{
1541 struct scic_sds_remote_device *sci_dev = object;
1542
1543 sci_dev->working_request = NULL;
1544}
Dan Williams88f3b622011-04-22 19:18:03 -07001545
1546static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1547 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1548 .enter_state = scic_sds_remote_device_initial_state_enter,
1549 },
1550 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1551 .enter_state = scic_sds_remote_device_stopped_state_enter,
1552 },
1553 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1554 .enter_state = scic_sds_remote_device_starting_state_enter,
1555 },
1556 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1557 .enter_state = scic_sds_remote_device_ready_state_enter,
1558 .exit_state = scic_sds_remote_device_ready_state_exit
1559 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001560 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1561 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1562 },
1563 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1564 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1565 },
1566 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
1567 .enter_state = scic_sds_stp_remote_device_ready_ncq_substate_enter,
1568 },
1569 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1570 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1571 },
1572 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
1573 .enter_state = scic_sds_stp_remote_device_ready_await_reset_substate_enter,
1574 },
1575 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1576 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1577 },
1578 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1579 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1580 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1581 },
Dan Williams88f3b622011-04-22 19:18:03 -07001582 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1583 .enter_state = scic_sds_remote_device_stopping_state_enter,
1584 },
1585 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1586 .enter_state = scic_sds_remote_device_failed_state_enter,
1587 },
1588 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1589 .enter_state = scic_sds_remote_device_resetting_state_enter,
1590 .exit_state = scic_sds_remote_device_resetting_state_exit
1591 },
1592 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1593 .enter_state = scic_sds_remote_device_final_state_enter,
1594 },
1595};
1596
1597/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001598 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001599 * @sci_port: SAS/SATA port through which this device is accessed.
1600 * @sci_dev: remote device to construct
1601 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001602 * This routine just performs benign initialization and does not
1603 * allocate the remote_node_context which is left to
1604 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1605 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001606 */
1607static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1608 struct scic_sds_remote_device *sci_dev)
1609{
1610 sci_dev->owning_port = sci_port;
1611 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001612
1613 sci_base_state_machine_construct(
1614 &sci_dev->state_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001615 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001616 scic_sds_remote_device_state_table,
1617 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1618 );
1619
1620 sci_base_state_machine_start(
1621 &sci_dev->state_machine
1622 );
1623
1624 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1625 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001626}
1627
1628/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001629 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001630 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001631 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1632 * the device is known to the SCI Core since it is contained in the
1633 * scic_phy object. Remote node context(s) is/are a global resource
1634 * allocated by this routine, freed by scic_remote_device_destruct().
1635 *
1636 * Returns:
1637 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1638 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1639 * sata-only controller instance.
1640 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001641 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001642static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1643 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001644{
1645 enum sci_status status;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001646 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001647
Dan Williamsb87ee302011-04-25 11:48:29 -07001648 scic_remote_device_construct(sci_port, sci_dev);
1649
Dan Williams88f3b622011-04-22 19:18:03 -07001650 /*
1651 * This information is request to determine how many remote node context
1652 * entries will be needed to store the remote node.
1653 */
Dan Williams88f3b622011-04-22 19:18:03 -07001654 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001655 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1656 sci_dev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001657 &sci_dev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001658
Dan Williamsa1a113b2011-04-21 18:44:45 -07001659 if (status != SCI_SUCCESS)
1660 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001661
Dan Williamsab2e8f72011-04-27 16:32:45 -07001662 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1663 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1664 /* pass */;
1665 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001666 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001667
Dan Williamsa1a113b2011-04-21 18:44:45 -07001668 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001669
Dan Williamsa1a113b2011-04-21 18:44:45 -07001670 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1671 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001672
Dan Williamsa1a113b2011-04-21 18:44:45 -07001673 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001674}
1675
Dan Williams88f3b622011-04-22 19:18:03 -07001676/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001677 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001678 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001679 * Remote node context(s) is/are a global resource allocated by this
1680 * routine, freed by scic_remote_device_destruct().
1681 *
1682 * Returns:
1683 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1684 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1685 * sata-only controller instance.
1686 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001687 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001688static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001689 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001690{
Dan Williamsa1a113b2011-04-21 18:44:45 -07001691 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001692 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001693
Dan Williamsb87ee302011-04-25 11:48:29 -07001694 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001695
Dan Williamsab2e8f72011-04-27 16:32:45 -07001696 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1697 sci_dev,
1698 &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001699 if (status != SCI_SUCCESS)
1700 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001701
Dan Williamsab2e8f72011-04-27 16:32:45 -07001702 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1703 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1704 /* pass */;
1705 else
1706 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001707
Dan Williamsa1a113b2011-04-21 18:44:45 -07001708 /*
1709 * For SAS-2 the physical link rate is actually a logical link
1710 * rate that incorporates multiplexing. The SCU doesn't
1711 * incorporate multiplexing and for the purposes of the
1712 * connection the logical link rate is that same as the
1713 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1714 * one another, so this code works for both situations. */
1715 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001716 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001717
Dan Williamsa1a113b2011-04-21 18:44:45 -07001718 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1719 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001720
Dan Williamsab2e8f72011-04-27 16:32:45 -07001721 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001722}
1723
1724/**
1725 * scic_remote_device_start() - This method will start the supplied remote
1726 * device. This method enables normal IO requests to flow through to the
1727 * remote device.
1728 * @remote_device: This parameter specifies the device to be started.
1729 * @timeout: This parameter specifies the number of milliseconds in which the
1730 * start operation should complete.
1731 *
1732 * An indication of whether the device was successfully started. SCI_SUCCESS
1733 * This value is returned if the device was successfully started.
1734 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1735 * the device when there have been no phys added to it.
1736 */
1737static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
Dan Williamseb229672011-05-01 14:05:57 -07001738 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001739{
Dan Williamseb229672011-05-01 14:05:57 -07001740 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1741 enum scic_sds_remote_device_states state = sm->current_state_id;
1742 enum sci_status status;
1743
1744 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1745 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1746 __func__, state);
1747 return SCI_FAILURE_INVALID_STATE;
1748 }
1749
1750 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1751 remote_device_resume_done,
1752 sci_dev);
1753 if (status != SCI_SUCCESS)
1754 return status;
1755
1756 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1757
1758 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001759}
Dan Williams6f231dd2011-07-02 22:56:22 -07001760
Dan Williams00d680e2011-04-25 14:29:29 -07001761static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1762 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001763{
Dan Williams00d680e2011-04-25 14:29:29 -07001764 struct scic_sds_port *sci_port = iport->sci_port_handle;
1765 struct isci_host *ihost = iport->isci_host;
1766 struct domain_device *dev = idev->domain_dev;
1767 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001768
Dan Williams00d680e2011-04-25 14:29:29 -07001769 if (dev->parent && dev_is_expander(dev->parent))
1770 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1771 else
1772 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001773
1774 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001775 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1776 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001777
1778 return status;
1779 }
1780
Dan Williams6f231dd2011-07-02 22:56:22 -07001781 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001782 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001783
Dan Williams00d680e2011-04-25 14:29:29 -07001784 if (status != SCI_SUCCESS)
1785 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1786 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001787
1788 return status;
1789}
1790
Dan Williams4393aa42011-03-31 13:10:44 -07001791void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001792{
1793 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001794
Dan Williams4393aa42011-03-31 13:10:44 -07001795 dev_dbg(&ihost->pdev->dev,
1796 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001797
1798 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -07001799 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -07001800
Dan Williams4393aa42011-03-31 13:10:44 -07001801 dev_dbg(&ihost->pdev->dev,
1802 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001803}
1804
Dan Williams6f231dd2011-07-02 22:56:22 -07001805/**
1806 * This function builds the isci_remote_device when a libsas dev_found message
1807 * is received.
1808 * @isci_host: This parameter specifies the isci host object.
1809 * @port: This parameter specifies the isci_port conected to this device.
1810 *
1811 * pointer to new isci_remote_device.
1812 */
1813static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001814isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001815{
Dan Williamsd9c37392011-03-03 17:59:32 -08001816 struct isci_remote_device *idev;
1817 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001818
Dan Williamsd9c37392011-03-03 17:59:32 -08001819 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001820 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001821 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1822 break;
1823 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001824
Dan Williamsd9c37392011-03-03 17:59:32 -08001825 if (i >= SCI_MAX_REMOTE_DEVICES) {
1826 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001827 return NULL;
1828 }
1829
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001830 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1831 return NULL;
1832
1833 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1834 return NULL;
1835
Dan Williamsd9c37392011-03-03 17:59:32 -08001836 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001837
Dan Williamsd9c37392011-03-03 17:59:32 -08001838 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001839}
Dan Williams6f231dd2011-07-02 22:56:22 -07001840
1841/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001842 * isci_remote_device_stop() - This function is called internally to stop the
1843 * remote device.
1844 * @isci_host: This parameter specifies the isci host object.
1845 * @isci_device: This parameter specifies the remote device.
1846 *
1847 * The status of the scic request to stop.
1848 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001849enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001850{
1851 enum sci_status status;
1852 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001853
Dan Williams6ad31fe2011-03-04 12:10:29 -08001854 dev_dbg(&ihost->pdev->dev,
1855 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001856
Dan Williams6ad31fe2011-03-04 12:10:29 -08001857 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001858
1859 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001860 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001861
Dan Williams6ad31fe2011-03-04 12:10:29 -08001862 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001863
Dan Williams6ad31fe2011-03-04 12:10:29 -08001864 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07001865 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001866 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001867
1868 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -08001869 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -08001870 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08001871 clear_bit(IDEV_ALLOCATED, &idev->flags);
1872 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001873
Dan Williams6ad31fe2011-03-04 12:10:29 -08001874 dev_dbg(&ihost->pdev->dev,
1875 "%s: idev = %p - after completion wait\n",
1876 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001877
Dan Williams6f231dd2011-07-02 22:56:22 -07001878 return status;
1879}
1880
1881/**
1882 * isci_remote_device_gone() - This function is called by libsas when a domain
1883 * device is removed.
1884 * @domain_device: This parameter specifies the libsas domain device.
1885 *
1886 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001887void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001888{
Dan Williams4393aa42011-03-31 13:10:44 -07001889 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001890 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001891
Dan Williams6ad31fe2011-03-04 12:10:29 -08001892 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001893 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001894 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001895
Dan Williams6ad31fe2011-03-04 12:10:29 -08001896 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001897}
1898
1899
1900/**
1901 * isci_remote_device_found() - This function is called by libsas when a remote
1902 * device is discovered. A remote device object is created and started. the
1903 * function then sleeps until the sci core device started message is
1904 * received.
1905 * @domain_device: This parameter specifies the libsas domain device.
1906 *
1907 * status, zero indicates success.
1908 */
1909int isci_remote_device_found(struct domain_device *domain_dev)
1910{
Dan Williams4393aa42011-03-31 13:10:44 -07001911 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001912 struct isci_port *isci_port;
1913 struct isci_phy *isci_phy;
1914 struct asd_sas_port *sas_port;
1915 struct asd_sas_phy *sas_phy;
1916 struct isci_remote_device *isci_device;
1917 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001918
Dan Williams6f231dd2011-07-02 22:56:22 -07001919 dev_dbg(&isci_host->pdev->dev,
1920 "%s: domain_device = %p\n", __func__, domain_dev);
1921
Dan Williams0cf89d12011-02-18 09:25:07 -08001922 wait_for_start(isci_host);
1923
Dan Williams6f231dd2011-07-02 22:56:22 -07001924 sas_port = domain_dev->port;
1925 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1926 port_phy_el);
1927 isci_phy = to_isci_phy(sas_phy);
1928 isci_port = isci_phy->isci_port;
1929
1930 /* we are being called for a device on this port,
1931 * so it has to come up eventually
1932 */
1933 wait_for_completion(&isci_port->start_complete);
1934
1935 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1936 (isci_stopped == isci_port_get_state(isci_port)))
1937 return -ENODEV;
1938
1939 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001940 if (!isci_device)
1941 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001942
1943 INIT_LIST_HEAD(&isci_device->node);
1944 domain_dev->lldd_dev = isci_device;
1945 isci_device->domain_dev = domain_dev;
1946 isci_device->isci_port = isci_port;
1947 isci_remote_device_change_state(isci_device, isci_starting);
1948
1949
Dan Williams1a380452011-03-03 18:01:43 -08001950 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001951 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1952
Dan Williams6ad31fe2011-03-04 12:10:29 -08001953 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001954 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -08001955 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001956
Dan Williams6f231dd2011-07-02 22:56:22 -07001957 dev_dbg(&isci_host->pdev->dev,
1958 "%s: isci_device = %p\n",
1959 __func__, isci_device);
1960
1961 if (status != SCI_SUCCESS) {
1962
Dan Williams1a380452011-03-03 18:01:43 -08001963 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001964 isci_remote_device_deconstruct(
1965 isci_host,
1966 isci_device
1967 );
Dan Williams1a380452011-03-03 18:01:43 -08001968 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001969 return -ENODEV;
1970 }
1971
Dan Williams6ad31fe2011-03-04 12:10:29 -08001972 /* wait for the device ready callback. */
1973 wait_for_device_start(isci_host, isci_device);
1974
Dan Williams6f231dd2011-07-02 22:56:22 -07001975 return 0;
1976}
1977/**
1978 * isci_device_is_reset_pending() - This function will check if there is any
1979 * pending reset condition on the device.
1980 * @request: This parameter is the isci_device object.
1981 *
1982 * true if there is a reset pending for the device.
1983 */
1984bool isci_device_is_reset_pending(
1985 struct isci_host *isci_host,
1986 struct isci_remote_device *isci_device)
1987{
1988 struct isci_request *isci_request;
1989 struct isci_request *tmp_req;
1990 bool reset_is_pending = false;
1991 unsigned long flags;
1992
1993 dev_dbg(&isci_host->pdev->dev,
1994 "%s: isci_device = %p\n", __func__, isci_device);
1995
1996 spin_lock_irqsave(&isci_host->scic_lock, flags);
1997
1998 /* Check for reset on all pending requests. */
1999 list_for_each_entry_safe(isci_request, tmp_req,
2000 &isci_device->reqs_in_process, dev_node) {
2001 dev_dbg(&isci_host->pdev->dev,
2002 "%s: isci_device = %p request = %p\n",
2003 __func__, isci_device, isci_request);
2004
2005 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07002006 struct sas_task *task = isci_request_access_task(
2007 isci_request);
2008
Bartosz Barcinski467e8552011-04-12 17:28:41 -07002009 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002010 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
2011 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07002012 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002013 }
2014 }
2015
2016 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
2017
2018 dev_dbg(&isci_host->pdev->dev,
2019 "%s: isci_device = %p reset_is_pending = %d\n",
2020 __func__, isci_device, reset_is_pending);
2021
2022 return reset_is_pending;
2023}
2024
2025/**
2026 * isci_device_clear_reset_pending() - This function will clear if any pending
2027 * reset condition flags on the device.
2028 * @request: This parameter is the isci_device object.
2029 *
2030 * true if there is a reset pending for the device.
2031 */
Dan Williams4393aa42011-03-31 13:10:44 -07002032void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002033{
2034 struct isci_request *isci_request;
2035 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07002036 unsigned long flags = 0;
2037
Dan Williams4393aa42011-03-31 13:10:44 -07002038 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
2039 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07002040
Dan Williams4393aa42011-03-31 13:10:44 -07002041 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002042
2043 /* Clear reset pending on all pending requests. */
2044 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07002045 &idev->reqs_in_process, dev_node) {
2046 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
2047 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07002048
2049 if (isci_request->ttype == io_task) {
2050
2051 unsigned long flags2;
2052 struct sas_task *task = isci_request_access_task(
2053 isci_request);
2054
2055 spin_lock_irqsave(&task->task_state_lock, flags2);
2056 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
2057 spin_unlock_irqrestore(&task->task_state_lock, flags2);
2058 }
2059 }
Dan Williams4393aa42011-03-31 13:10:44 -07002060 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002061}