blob: a6247675c5c790baf21fdf07b6381b1ec4bc0532 [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
524/**
525 *
526 * @controller: The controller that is starting the task request.
527 * @sci_dev: The remote device for which the start task handling is being
528 * requested.
529 * @io_request: The task request that is being started.
530 *
531 * This method invokes the remote device start task handler. enum sci_status
532 */
533enum sci_status scic_sds_remote_device_start_task(
534 struct scic_sds_controller *controller,
535 struct scic_sds_remote_device *sci_dev,
536 struct scic_sds_request *io_request)
537{
538 return sci_dev->state_handlers->start_task_handler(
539 sci_dev, io_request);
540}
541
542/**
543 *
Dan Williams88f3b622011-04-22 19:18:03 -0700544 * @sci_dev:
545 * @request:
546 *
547 * This method takes the request and bulids an appropriate SCU context for the
548 * request and then requests the controller to post the request. none
549 */
550void scic_sds_remote_device_post_request(
551 struct scic_sds_remote_device *sci_dev,
552 u32 request)
553{
554 u32 context;
555
556 context = scic_sds_remote_device_build_command_context(sci_dev, request);
557
558 scic_sds_controller_post_request(
559 scic_sds_remote_device_get_controller(sci_dev),
560 context
561 );
562}
563
Dan Williamsab2e8f72011-04-27 16:32:45 -0700564/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700565 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700566 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700567 */
Dan Williamseb229672011-05-01 14:05:57 -0700568static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700569{
Dan Williamsab2e8f72011-04-27 16:32:45 -0700570 struct scic_sds_remote_device *sci_dev = _dev;
571 enum scic_sds_remote_device_states state;
Dan Williams88f3b622011-04-22 19:18:03 -0700572
Dan Williamsab2e8f72011-04-27 16:32:45 -0700573 state = sci_dev->state_machine.current_state_id;
574 switch (state) {
575 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
576 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
577 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
578 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
579 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
580 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
581 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
582 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
583 break;
584 default:
585 /* go 'ready' if we are not already in a ready state */
586 sci_base_state_machine_change_state(&sci_dev->state_machine,
587 SCI_BASE_REMOTE_DEVICE_STATE_READY);
588 break;
Dan Williams88f3b622011-04-22 19:18:03 -0700589 }
590}
591
592/**
593 *
Dan Williams88f3b622011-04-22 19:18:03 -0700594 * @request: This parameter specifies the request being continued.
595 *
596 * This method will continue to post tc for a STP request. This method usually
597 * serves as a callback when RNC gets resumed during a task management
598 * sequence. none
599 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700600static void scic_sds_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700601{
602 struct scic_sds_remote_device *sci_dev = dev;
603
604 /* we need to check if this request is still valid to continue. */
605 if (sci_dev->working_request)
606 scic_controller_continue_io(sci_dev->working_request);
607}
608
Dan Williams88f3b622011-04-22 19:18:03 -0700609static enum sci_status
610default_device_handler(struct scic_sds_remote_device *sci_dev,
611 const char *func)
612{
613 dev_warn(scirdev_to_dev(sci_dev),
614 "%s: in wrong state: %d\n", func,
615 sci_base_state_machine_get_state(&sci_dev->state_machine));
616 return SCI_FAILURE_INVALID_STATE;
617}
618
Dan Williamsab2e8f72011-04-27 16:32:45 -0700619static enum sci_status scic_sds_remote_device_default_suspend_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700620 struct scic_sds_remote_device *sci_dev, u32 suspend_type)
621{
622 return default_device_handler(sci_dev, __func__);
623}
624
Dan Williamsab2e8f72011-04-27 16:32:45 -0700625static enum sci_status scic_sds_remote_device_default_resume_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700626 struct scic_sds_remote_device *sci_dev)
627{
628 return default_device_handler(sci_dev, __func__);
629}
630
631/**
632 *
633 * @device: The struct scic_sds_remote_device which is then cast into a
634 * struct scic_sds_remote_device.
635 * @event_code: The event code that the struct scic_sds_controller wants the device
636 * object to process.
637 *
638 * This method is the default event handler. It will call the RNC state
639 * machine handler for any RNC events otherwise it will log a warning and
640 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
641 */
642static enum sci_status scic_sds_remote_device_core_event_handler(
643 struct scic_sds_remote_device *sci_dev,
644 u32 event_code,
645 bool is_ready_state)
646{
647 enum sci_status status;
648
649 switch (scu_get_event_type(event_code)) {
650 case SCU_EVENT_TYPE_RNC_OPS_MISC:
651 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
652 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
653 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
654 break;
655 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
656
657 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
658 status = SCI_SUCCESS;
659
660 /* Suspend the associated RNC */
661 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
662 SCI_SOFTWARE_SUSPENSION,
663 NULL, NULL);
664
665 dev_dbg(scirdev_to_dev(sci_dev),
666 "%s: device: %p event code: %x: %s\n",
667 __func__, sci_dev, event_code,
668 (is_ready_state)
669 ? "I_T_Nexus_Timeout event"
670 : "I_T_Nexus_Timeout event in wrong state");
671
672 break;
673 }
674 /* Else, fall through and treat as unhandled... */
675
676 default:
677 dev_dbg(scirdev_to_dev(sci_dev),
678 "%s: device: %p event code: %x: %s\n",
679 __func__, sci_dev, event_code,
680 (is_ready_state)
681 ? "unexpected event"
682 : "unexpected event in wrong state");
683 status = SCI_FAILURE_INVALID_STATE;
684 break;
685 }
686
687 return status;
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_default_event_handler(
701 struct scic_sds_remote_device *sci_dev,
702 u32 event_code)
703{
704 return scic_sds_remote_device_core_event_handler(sci_dev,
705 event_code,
706 false);
707}
708
709/**
710 *
711 * @device: The struct scic_sds_remote_device which is then cast into a
712 * struct scic_sds_remote_device.
713 * @frame_index: The frame index for which the struct scic_sds_controller wants this
714 * device object to process.
715 *
716 * This method is the default unsolicited frame handler. It logs a warning,
717 * releases the frame and returns a failure. enum sci_status
718 * SCI_FAILURE_INVALID_STATE
719 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700720static enum sci_status scic_sds_remote_device_default_frame_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700721 struct scic_sds_remote_device *sci_dev,
722 u32 frame_index)
723{
724 dev_warn(scirdev_to_dev(sci_dev),
725 "%s: SCIC Remote Device requested to handle frame %x "
726 "while in wrong state %d\n",
727 __func__,
728 frame_index,
729 sci_base_state_machine_get_state(
730 &sci_dev->state_machine));
731
732 /* Return the frame back to the controller */
733 scic_sds_controller_release_frame(
734 scic_sds_remote_device_get_controller(sci_dev), frame_index
735 );
736
737 return SCI_FAILURE_INVALID_STATE;
738}
739
Dan Williamsab2e8f72011-04-27 16:32:45 -0700740static enum sci_status scic_sds_remote_device_default_start_request_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700741 struct scic_sds_remote_device *sci_dev,
742 struct scic_sds_request *request)
743{
744 return default_device_handler(sci_dev, __func__);
745}
746
Dan Williamsab2e8f72011-04-27 16:32:45 -0700747static enum sci_status scic_sds_remote_device_default_complete_request_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700748 struct scic_sds_remote_device *sci_dev,
749 struct scic_sds_request *request)
750{
751 return default_device_handler(sci_dev, __func__);
752}
753
Dan Williamsab2e8f72011-04-27 16:32:45 -0700754static enum sci_status scic_sds_remote_device_default_continue_request_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700755 struct scic_sds_remote_device *sci_dev,
756 struct scic_sds_request *request)
757{
758 return default_device_handler(sci_dev, __func__);
759}
760
761/**
762 *
763 * @device: The struct scic_sds_remote_device which is then cast into a
764 * struct scic_sds_remote_device.
765 * @frame_index: The frame index for which the struct scic_sds_controller wants this
766 * device object to process.
767 *
768 * This method is a general ssp frame handler. In most cases the device object
769 * needs to route the unsolicited frame processing to the io request object.
770 * This method decodes the tag for the io request object and routes the
771 * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
772 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700773static enum sci_status scic_sds_remote_device_general_frame_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700774 struct scic_sds_remote_device *sci_dev,
775 u32 frame_index)
776{
777 enum sci_status result;
778 struct sci_ssp_frame_header *frame_header;
779 struct scic_sds_request *io_request;
780
781 result = scic_sds_unsolicited_frame_control_get_header(
782 &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
783 frame_index,
784 (void **)&frame_header
785 );
786
787 if (SCI_SUCCESS == result) {
788 io_request = scic_sds_controller_get_io_request_from_tag(
789 scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
790
791 if ((io_request == NULL)
792 || (io_request->target_device != sci_dev)) {
793 /*
794 * We could not map this tag to a valid IO request
795 * Just toss the frame and continue */
796 scic_sds_controller_release_frame(
797 scic_sds_remote_device_get_controller(sci_dev), frame_index
798 );
799 } else {
800 /* The IO request is now in charge of releasing the frame */
801 result = io_request->state_handlers->frame_handler(
802 io_request, frame_index);
803 }
804 }
805
806 return result;
807}
808
809/**
810 *
811 * @[in]: sci_dev This is the device object that is receiving the event.
812 * @[in]: event_code The event code to process.
813 *
814 * This is a common method for handling events reported to the remote device
815 * from the controller object. enum sci_status
816 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700817static enum sci_status scic_sds_remote_device_general_event_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700818 struct scic_sds_remote_device *sci_dev,
819 u32 event_code)
820{
821 return scic_sds_remote_device_core_event_handler(sci_dev,
822 event_code,
823 true);
824}
825
Dan Williams88f3b622011-04-22 19:18:03 -0700826/*
827 * This method will attempt to start a task request for this device object. The
828 * remote device object will issue the start request for the task and if
829 * successful it will start the request for the port object then increment its
830 * own requet count. enum sci_status SCI_SUCCESS if the task request is started for
831 * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
832 * object could not get the resources to start.
833 */
834static enum sci_status scic_sds_remote_device_ready_state_start_task_handler(
835 struct scic_sds_remote_device *sci_dev,
836 struct scic_sds_request *request)
837{
838 enum sci_status result;
839
840 /* See if the port is in a state where we can start the IO request */
841 result = scic_sds_port_start_io(
842 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
843
844 if (result == SCI_SUCCESS) {
845 result = scic_sds_remote_node_context_start_task(&sci_dev->rnc,
846 request);
847 if (result == SCI_SUCCESS)
848 result = scic_sds_request_start(request);
849
850 scic_sds_remote_device_start_request(sci_dev, request, result);
851 }
852
853 return result;
854}
855
856/*
Dan Williams88f3b622011-04-22 19:18:03 -0700857 * This method will complete the request for the remote device object. The
858 * method will call the completion handler for the request object and if
859 * successful it will complete the request on the port object then decrement
860 * its own started_request_count. enum sci_status
861 */
862static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
863 struct scic_sds_remote_device *sci_dev,
864 struct scic_sds_request *request)
865{
866 enum sci_status result;
867
868 result = scic_sds_request_complete(request);
869
870 if (result != SCI_SUCCESS)
871 return result;
872
873 /* See if the port is in a state
874 * where we can start the IO request */
875 result = scic_sds_port_complete_io(
876 scic_sds_remote_device_get_port(sci_dev),
877 sci_dev, request);
878
879 if (result == SCI_SUCCESS)
880 scic_sds_remote_device_decrement_request_count(sci_dev);
881
882 return result;
883}
884
Dan Williams88f3b622011-04-22 19:18:03 -0700885/**
886 *
887 * @device: The device object for which the request is completing.
888 * @request: The task request that is being completed.
889 *
890 * This method completes requests for this struct scic_sds_remote_device while it is
891 * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
892 * complete method for the request object and if that is successful the port
893 * object is called to complete the task request. Then the device object itself
894 * completes the task request. If struct scic_sds_remote_device started_request_count
895 * goes to 0 and the invalidate RNC request has completed the device object can
896 * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
897 */
898static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
899 struct scic_sds_remote_device *sci_dev,
900 struct scic_sds_request *request)
901{
902 enum sci_status status = SCI_SUCCESS;
903
904 status = scic_sds_request_complete(request);
905
906 if (status != SCI_SUCCESS)
907 return status;
908
909 status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
910 sci_dev, request);
911 if (status != SCI_SUCCESS)
912 return status;
913
914 scic_sds_remote_device_decrement_request_count(sci_dev);
915
916 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
917 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
Dan Williamsec575662011-05-01 14:19:25 -0700918 rnc_destruct_done, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700919 return SCI_SUCCESS;
920}
921
Dan Williamsab2e8f72011-04-27 16:32:45 -0700922/* complete requests for this device while it is in the
923 * SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the complete
924 * method for the request object and if that is successful the port object is
925 * called to complete the task request. Then the device object itself completes
926 * the task request. enum sci_status
Dan Williams88f3b622011-04-22 19:18:03 -0700927 */
928static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
929 struct scic_sds_remote_device *sci_dev,
930 struct scic_sds_request *request)
931{
932 enum sci_status status = SCI_SUCCESS;
933
934 status = scic_sds_request_complete(request);
935
936 if (status == SCI_SUCCESS) {
937 status = scic_sds_port_complete_io(
938 scic_sds_remote_device_get_port(sci_dev),
939 sci_dev, request);
940
941 if (status == SCI_SUCCESS) {
942 scic_sds_remote_device_decrement_request_count(sci_dev);
943 }
944 }
945
946 return status;
947}
948
Dan Williamsab2e8f72011-04-27 16:32:45 -0700949static enum sci_status scic_sds_stp_remote_device_complete_request(struct scic_sds_remote_device *sci_dev,
950 struct scic_sds_request *sci_req)
951{
952 enum sci_status status;
953
954 status = scic_sds_io_request_complete(sci_req);
955 if (status != SCI_SUCCESS)
956 goto out;
957
958 status = scic_sds_port_complete_io(sci_dev->owning_port, sci_dev, sci_req);
959 if (status != SCI_SUCCESS)
960 goto out;
961
962 scic_sds_remote_device_decrement_request_count(sci_dev);
963 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
964 /* This request causes hardware error, device needs to be Lun Reset.
965 * So here we force the state machine to IDLE state so the rest IOs
966 * can reach RNC state handler, these IOs will be completed by RNC with
967 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
968 */
969 sci_base_state_machine_change_state(&sci_dev->state_machine,
970 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
971 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
972 sci_base_state_machine_change_state(&sci_dev->state_machine,
973 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
974
975
976 out:
977 if (status != SCI_SUCCESS)
978 dev_err(scirdev_to_dev(sci_dev),
979 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
980 "could not complete\n", __func__, sci_dev->owning_port,
981 sci_dev, sci_req, status);
982
983 return status;
984}
985
986/* scic_sds_stp_remote_device_ready_substate_start_request_handler - start stp
987 * @device: The target device a task management request towards to.
988 * @request: The task request.
989 *
990 * This is the READY NCQ substate handler to start task management request. In
991 * this routine, we suspend and resume the RNC. enum sci_status Always return
992 * SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS status to let
993 * controller_start_task_handler know that the controller can't post TC for
994 * task request yet, instead, when RNC gets resumed, a controller_continue_task
995 * callback will be called.
996 */
997static enum sci_status scic_sds_stp_remote_device_ready_substate_start_request_handler(
998 struct scic_sds_remote_device *device,
999 struct scic_sds_request *request)
1000{
1001 enum sci_status status;
1002
1003 /* Will the port allow the io request to start? */
1004 status = device->owning_port->state_handlers->start_io_handler(
1005 device->owning_port, device, request);
1006 if (status != SCI_SUCCESS)
1007 return status;
1008
1009 status = scic_sds_remote_node_context_start_task(&device->rnc, request);
1010 if (status != SCI_SUCCESS)
1011 goto out;
1012
1013 status = request->state_handlers->start_handler(request);
1014 if (status != SCI_SUCCESS)
1015 goto out;
1016
1017 /*
1018 * Note: If the remote device state is not IDLE this will replace
1019 * the request that probably resulted in the task management request.
1020 */
1021 device->working_request = request;
1022 sci_base_state_machine_change_state(&device->state_machine,
1023 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1024
1025 /*
1026 * The remote node context must cleanup the TCi to NCQ mapping table.
1027 * The only way to do this correctly is to either write to the TLCR
1028 * register or to invalidate and repost the RNC. In either case the
1029 * remote node context state machine will take the correct action when
1030 * the remote node context is suspended and later resumed.
1031 */
1032 scic_sds_remote_node_context_suspend(&device->rnc,
1033 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1034 scic_sds_remote_node_context_resume(&device->rnc,
1035 scic_sds_remote_device_continue_request,
1036 device);
1037
1038out:
1039 scic_sds_remote_device_start_request(device, request, status);
1040 /*
1041 * We need to let the controller start request handler know that it can't
1042 * post TC yet. We will provide a callback function to post TC when RNC gets
1043 * resumed.
1044 */
1045 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
1046}
1047
Dan Williamsab2e8f72011-04-27 16:32:45 -07001048static enum sci_status scic_sds_stp_remote_device_ready_idle_substate_event_handler(
1049 struct scic_sds_remote_device *sci_dev,
1050 u32 event_code)
1051{
1052 enum sci_status status;
1053
1054 status = scic_sds_remote_device_general_event_handler(sci_dev, event_code);
1055 if (status != SCI_SUCCESS)
1056 return status;
1057
1058 /* We pick up suspension events to handle specifically to this state. We
1059 * resume the RNC right away. enum sci_status
1060 */
1061 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
1062 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
1063 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1064
1065 return status;
1066}
1067
Dan Williamsab2e8f72011-04-27 16:32:45 -07001068static enum sci_status scic_sds_stp_remote_device_ready_ncq_substate_frame_handler(struct scic_sds_remote_device *sci_dev,
1069 u32 frame_index)
1070{
1071 enum sci_status status;
1072 struct sata_fis_header *frame_header;
1073 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1074
1075 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1076 frame_index,
1077 (void **)&frame_header);
1078 if (status != SCI_SUCCESS)
1079 return status;
1080
1081 if (frame_header->fis_type == SATA_FIS_TYPE_SETDEVBITS &&
1082 (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
1083 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
1084
1085 /* TODO Check sactive and complete associated IO if any. */
1086
1087 sci_base_state_machine_change_state(&sci_dev->state_machine,
1088 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1089 } else if (frame_header->fis_type == SATA_FIS_TYPE_REGD2H &&
1090 (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
1091 /*
1092 * Some devices return D2H FIS when an NCQ error is detected.
1093 * Treat this like an SDB error FIS ready reason.
1094 */
1095 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
1096
1097 sci_base_state_machine_change_state(&sci_dev->state_machine,
1098 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1099 } else
1100 status = SCI_FAILURE;
1101
1102 scic_sds_controller_release_frame(scic, frame_index);
1103
1104 return status;
1105}
1106
Dan Williamsab2e8f72011-04-27 16:32:45 -07001107static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler(
1108 struct scic_sds_remote_device *sci_dev,
1109 u32 suspend_type)
1110{
1111 enum sci_status status;
1112
1113 status = scic_sds_remote_node_context_suspend(&sci_dev->rnc,
1114 suspend_type, NULL, NULL);
1115
1116 return status;
1117}
1118
1119static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_frame_handler(
1120 struct scic_sds_remote_device *sci_dev,
1121 u32 frame_index)
1122{
1123 /* The device doe not process any UF received from the hardware while
1124 * in this state. All unsolicited frames are forwarded to the io
1125 * request object.
1126 */
1127 return scic_sds_io_request_frame_handler(sci_dev->working_request,
1128 frame_index);
1129}
1130
Dan Williamsab2e8f72011-04-27 16:32:45 -07001131static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
1132{
1133 struct scic_sds_remote_device *sci_dev = _dev;
1134 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1135 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1136
1137 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
1138 * As a result, avoid sending the ready notification.
1139 */
1140 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
1141 isci_remote_device_ready(scic->ihost, idev);
1142}
1143
Dan Williamsab2e8f72011-04-27 16:32:45 -07001144static enum sci_status scic_sds_smp_remote_device_ready_cmd_substate_frame_handler(
1145 struct scic_sds_remote_device *sci_dev,
1146 u32 frame_index)
1147{
1148 enum sci_status status;
1149
1150 /* The device does not process any UF received from the hardware while
1151 * in this state. All unsolicited frames are forwarded to the io request
1152 * object.
1153 */
1154 status = scic_sds_io_request_frame_handler(
1155 sci_dev->working_request,
1156 frame_index
1157 );
1158
1159 return status;
1160}
1161
Dan Williams88f3b622011-04-22 19:18:03 -07001162static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
1163 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001164 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1165 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1166 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1167 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1168 .resume_handler = scic_sds_remote_device_default_resume_handler,
1169 .event_handler = scic_sds_remote_device_default_event_handler,
1170 .frame_handler = scic_sds_remote_device_default_frame_handler
1171 },
1172 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001173 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1174 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1175 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1176 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1177 .resume_handler = scic_sds_remote_device_default_resume_handler,
1178 .event_handler = scic_sds_remote_device_default_event_handler,
1179 .frame_handler = scic_sds_remote_device_default_frame_handler
1180 },
1181 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001182 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1183 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1184 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1185 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1186 .resume_handler = scic_sds_remote_device_default_resume_handler,
1187 .event_handler = scic_sds_remote_device_general_event_handler,
1188 .frame_handler = scic_sds_remote_device_default_frame_handler
1189 },
1190 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001191 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1192 .start_task_handler = scic_sds_remote_device_ready_state_start_task_handler,
1193 .complete_task_handler = scic_sds_remote_device_ready_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 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001199 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001200 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1201 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1202 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1203 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1204 .resume_handler = scic_sds_remote_device_default_resume_handler,
1205 .event_handler = scic_sds_stp_remote_device_ready_idle_substate_event_handler,
1206 .frame_handler = scic_sds_remote_device_default_frame_handler
1207 },
1208 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001209 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1210 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1211 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1212 .suspend_handler = scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler,
1213 .resume_handler = scic_sds_remote_device_default_resume_handler,
1214 .event_handler = scic_sds_remote_device_general_event_handler,
1215 .frame_handler = scic_sds_stp_remote_device_ready_cmd_substate_frame_handler
1216 },
1217 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001218 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1219 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1220 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1221 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1222 .resume_handler = scic_sds_remote_device_default_resume_handler,
1223 .event_handler = scic_sds_remote_device_general_event_handler,
1224 .frame_handler = scic_sds_stp_remote_device_ready_ncq_substate_frame_handler
1225 },
1226 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001227 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1228 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1229 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1230 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1231 .resume_handler = scic_sds_remote_device_default_resume_handler,
1232 .event_handler = scic_sds_remote_device_general_event_handler,
1233 .frame_handler = scic_sds_remote_device_general_frame_handler
1234 },
1235 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001236 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1237 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1238 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1239 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1240 .resume_handler = scic_sds_remote_device_default_resume_handler,
1241 .event_handler = scic_sds_remote_device_general_event_handler,
1242 .frame_handler = scic_sds_remote_device_general_frame_handler
1243 },
1244 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001245 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1246 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1247 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1248 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1249 .resume_handler = scic_sds_remote_device_default_resume_handler,
1250 .event_handler = scic_sds_remote_device_general_event_handler,
1251 .frame_handler = scic_sds_remote_device_default_frame_handler
1252 },
1253 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001254 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1255 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1256 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1257 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1258 .resume_handler = scic_sds_remote_device_default_resume_handler,
1259 .event_handler = scic_sds_remote_device_general_event_handler,
1260 .frame_handler = scic_sds_smp_remote_device_ready_cmd_substate_frame_handler
1261 },
Dan Williams88f3b622011-04-22 19:18:03 -07001262 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001263 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1264 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1265 .complete_task_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
1266 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1267 .resume_handler = scic_sds_remote_device_default_resume_handler,
1268 .event_handler = scic_sds_remote_device_general_event_handler,
1269 .frame_handler = scic_sds_remote_device_general_frame_handler
1270 },
1271 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001272 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1273 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1274 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1275 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1276 .resume_handler = scic_sds_remote_device_default_resume_handler,
1277 .event_handler = scic_sds_remote_device_default_event_handler,
1278 .frame_handler = scic_sds_remote_device_general_frame_handler
1279 },
1280 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001281 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1282 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1283 .complete_task_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1284 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1285 .resume_handler = scic_sds_remote_device_default_resume_handler,
1286 .event_handler = scic_sds_remote_device_default_event_handler,
1287 .frame_handler = scic_sds_remote_device_general_frame_handler
1288 },
1289 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001290 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1291 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1292 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1293 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1294 .resume_handler = scic_sds_remote_device_default_resume_handler,
1295 .event_handler = scic_sds_remote_device_default_event_handler,
1296 .frame_handler = scic_sds_remote_device_default_frame_handler
1297 }
1298};
1299
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001300static void scic_sds_remote_device_initial_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001301{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001302 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001303
Dan Williams88f3b622011-04-22 19:18:03 -07001304 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1305 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1306
1307 /* Initial state is a transitional state to the stopped state */
1308 sci_base_state_machine_change_state(&sci_dev->state_machine,
1309 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1310}
1311
1312/**
Dan Williams88f3b622011-04-22 19:18:03 -07001313 * scic_remote_device_destruct() - free remote node context and destruct
1314 * @remote_device: This parameter specifies the remote device to be destructed.
1315 *
1316 * Remote device objects are a limited resource. As such, they must be
1317 * protected. Thus calls to construct and destruct are mutually exclusive and
1318 * non-reentrant. The return value shall indicate if the device was
1319 * successfully destructed or if some failure occurred. enum sci_status This value
1320 * is returned if the device is successfully destructed.
1321 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
1322 * device isn't valid (e.g. it's already been destoryed, the handle isn't
1323 * valid, etc.).
1324 */
1325static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
1326{
Dan Williamsb8d82f62011-05-01 14:38:26 -07001327 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1328 enum scic_sds_remote_device_states state = sm->current_state_id;
1329 struct scic_sds_controller *scic;
1330
1331 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1332 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1333 __func__, state);
1334 return SCI_FAILURE_INVALID_STATE;
1335 }
1336
1337 scic = sci_dev->owning_port->owning_controller;
1338 scic_sds_controller_free_remote_node_context(scic, sci_dev,
1339 sci_dev->rnc.remote_node_index);
1340 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
1341 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
1342
1343 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001344}
1345
Dan Williams6f231dd2011-07-02 22:56:22 -07001346/**
1347 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -08001348 * @ihost: This parameter specifies the isci host object.
1349 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -07001350 *
1351 */
Dan Williamsd9c37392011-03-03 17:59:32 -08001352static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001353{
Dan Williamsd9c37392011-03-03 17:59:32 -08001354 dev_dbg(&ihost->pdev->dev,
1355 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001356
1357 /* There should not be any outstanding io's. All paths to
1358 * here should go through isci_remote_device_nuke_requests.
1359 * If we hit this condition, we will need a way to complete
1360 * io requests in process */
Dan Williamsd9c37392011-03-03 17:59:32 -08001361 while (!list_empty(&idev->reqs_in_process)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001362
Dan Williamsd9c37392011-03-03 17:59:32 -08001363 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001364 "%s: ** request list not empty! **\n", __func__);
1365 BUG();
1366 }
1367
Dan Williams57f20f42011-04-21 18:14:45 -07001368 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -08001369 idev->domain_dev->lldd_dev = NULL;
1370 idev->domain_dev = NULL;
1371 idev->isci_port = NULL;
1372 list_del_init(&idev->node);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001373
Dan Williamsd9c37392011-03-03 17:59:32 -08001374 clear_bit(IDEV_START_PENDING, &idev->flags);
1375 clear_bit(IDEV_STOP_PENDING, &idev->flags);
1376 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001377}
1378
Dan Williams88f3b622011-04-22 19:18:03 -07001379/**
1380 * isci_remote_device_stop_complete() - This function is called by the scic
1381 * when the remote device stop has completed. We mark the isci device as not
1382 * ready and remove the isci remote device.
1383 * @ihost: This parameter specifies the isci host object.
1384 * @idev: This parameter specifies the remote device.
1385 * @status: This parameter specifies status of the completion.
1386 *
1387 */
1388static void isci_remote_device_stop_complete(struct isci_host *ihost,
1389 struct isci_remote_device *idev)
1390{
1391 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1392
1393 isci_remote_device_change_state(idev, isci_stopped);
1394
1395 /* after stop, we can tear down resources. */
1396 isci_remote_device_deconstruct(ihost, idev);
1397}
1398
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001399static void scic_sds_remote_device_stopped_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001400{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001401 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001402 struct scic_sds_controller *scic;
1403 struct isci_remote_device *idev;
1404 struct isci_host *ihost;
1405 u32 prev_state;
1406
Dan Williams88f3b622011-04-22 19:18:03 -07001407 scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +00001408 ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001409 idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001410
1411 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1412 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1413
1414 /* If we are entering from the stopping state let the SCI User know that
1415 * the stop operation has completed.
1416 */
1417 prev_state = sci_dev->state_machine.previous_state_id;
1418 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1419 isci_remote_device_stop_complete(ihost, idev);
1420
1421 scic_sds_controller_remote_device_stopped(scic, sci_dev);
1422}
1423
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001424static void scic_sds_remote_device_starting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001425{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001426 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001427 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +00001428 struct isci_host *ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001429 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001430
1431 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1432 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1433
1434 isci_remote_device_not_ready(ihost, idev,
1435 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1436}
1437
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001438static void scic_sds_remote_device_ready_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001439{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001440 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001441 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1442 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001443
1444 SET_STATE_HANDLER(sci_dev,
1445 scic_sds_remote_device_state_handler_table,
1446 SCI_BASE_REMOTE_DEVICE_STATE_READY);
1447
1448 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1449
Dan Williamsab2e8f72011-04-27 16:32:45 -07001450 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
1451 sci_base_state_machine_change_state(&sci_dev->state_machine,
1452 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1453 } else if (dev_is_expander(dev)) {
1454 sci_base_state_machine_change_state(&sci_dev->state_machine,
1455 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1456 } else
1457 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
Dan Williams88f3b622011-04-22 19:18:03 -07001458}
1459
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001460static void scic_sds_remote_device_ready_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001461{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001462 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001463 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1464
1465 if (dev->dev_type == SAS_END_DEV) {
1466 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001467 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001468
Dan Williamsab2e8f72011-04-27 16:32:45 -07001469 isci_remote_device_not_ready(scic->ihost, idev,
Dan Williams88f3b622011-04-22 19:18:03 -07001470 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1471 }
1472}
1473
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001474static void scic_sds_remote_device_stopping_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001475{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001476 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001477
1478 SET_STATE_HANDLER(
1479 sci_dev,
1480 scic_sds_remote_device_state_handler_table,
1481 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1482 );
1483}
1484
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001485static void scic_sds_remote_device_failed_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001486{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001487 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001488
1489 SET_STATE_HANDLER(
1490 sci_dev,
1491 scic_sds_remote_device_state_handler_table,
1492 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1493 );
1494}
1495
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001496static void scic_sds_remote_device_resetting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001497{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001498 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001499
1500 SET_STATE_HANDLER(
1501 sci_dev,
1502 scic_sds_remote_device_state_handler_table,
1503 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1504 );
1505
1506 scic_sds_remote_node_context_suspend(
1507 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1508}
1509
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001510static void scic_sds_remote_device_resetting_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001511{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001512 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001513
1514 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1515}
1516
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001517static void scic_sds_remote_device_final_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001518{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001519 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001520
1521 SET_STATE_HANDLER(
1522 sci_dev,
1523 scic_sds_remote_device_state_handler_table,
1524 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1525 );
1526}
1527
Dan Williamsab2e8f72011-04-27 16:32:45 -07001528static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
1529{
1530 struct scic_sds_remote_device *sci_dev = object;
1531
1532 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1533 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1534
1535 sci_dev->working_request = NULL;
1536 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
1537 /*
1538 * Since the RNC is ready, it's alright to finish completion
1539 * processing (e.g. signal the remote device is ready). */
1540 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
1541 } else {
1542 scic_sds_remote_node_context_resume(&sci_dev->rnc,
1543 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
1544 sci_dev);
1545 }
1546}
1547
1548static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1549{
1550 struct scic_sds_remote_device *sci_dev = object;
1551 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1552
1553 BUG_ON(sci_dev->working_request == NULL);
1554
1555 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1556 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1557
1558 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1559 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1560}
1561
1562static void scic_sds_stp_remote_device_ready_ncq_substate_enter(void *object)
1563{
1564 struct scic_sds_remote_device *sci_dev = object;
1565
1566 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1567 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ);
1568}
1569
1570static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1571{
1572 struct scic_sds_remote_device *sci_dev = object;
1573 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1574 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1575
1576 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1577 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1578
1579 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1580 isci_remote_device_not_ready(scic->ihost, idev,
1581 sci_dev->not_ready_reason);
1582}
1583
1584static void scic_sds_stp_remote_device_ready_await_reset_substate_enter(void *object)
1585{
1586 struct scic_sds_remote_device *sci_dev = object;
1587
1588 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1589 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
1590}
1591
1592static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1593{
1594 struct scic_sds_remote_device *sci_dev = object;
1595 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1596
1597 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1598 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1599
1600 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1601}
1602
1603static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1604{
1605 struct scic_sds_remote_device *sci_dev = object;
1606 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1607
1608 BUG_ON(sci_dev->working_request == NULL);
1609
1610 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1611 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1612
1613 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1614 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1615}
1616
1617static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1618{
1619 struct scic_sds_remote_device *sci_dev = object;
1620
1621 sci_dev->working_request = NULL;
1622}
Dan Williams88f3b622011-04-22 19:18:03 -07001623
1624static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1625 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1626 .enter_state = scic_sds_remote_device_initial_state_enter,
1627 },
1628 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1629 .enter_state = scic_sds_remote_device_stopped_state_enter,
1630 },
1631 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1632 .enter_state = scic_sds_remote_device_starting_state_enter,
1633 },
1634 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1635 .enter_state = scic_sds_remote_device_ready_state_enter,
1636 .exit_state = scic_sds_remote_device_ready_state_exit
1637 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001638 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1639 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1640 },
1641 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1642 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1643 },
1644 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
1645 .enter_state = scic_sds_stp_remote_device_ready_ncq_substate_enter,
1646 },
1647 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1648 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1649 },
1650 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
1651 .enter_state = scic_sds_stp_remote_device_ready_await_reset_substate_enter,
1652 },
1653 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1654 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1655 },
1656 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1657 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1658 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1659 },
Dan Williams88f3b622011-04-22 19:18:03 -07001660 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1661 .enter_state = scic_sds_remote_device_stopping_state_enter,
1662 },
1663 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1664 .enter_state = scic_sds_remote_device_failed_state_enter,
1665 },
1666 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1667 .enter_state = scic_sds_remote_device_resetting_state_enter,
1668 .exit_state = scic_sds_remote_device_resetting_state_exit
1669 },
1670 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1671 .enter_state = scic_sds_remote_device_final_state_enter,
1672 },
1673};
1674
1675/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001676 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001677 * @sci_port: SAS/SATA port through which this device is accessed.
1678 * @sci_dev: remote device to construct
1679 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001680 * This routine just performs benign initialization and does not
1681 * allocate the remote_node_context which is left to
1682 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1683 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001684 */
1685static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1686 struct scic_sds_remote_device *sci_dev)
1687{
1688 sci_dev->owning_port = sci_port;
1689 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001690
1691 sci_base_state_machine_construct(
1692 &sci_dev->state_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001693 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001694 scic_sds_remote_device_state_table,
1695 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1696 );
1697
1698 sci_base_state_machine_start(
1699 &sci_dev->state_machine
1700 );
1701
1702 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1703 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001704}
1705
1706/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001707 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001708 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001709 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1710 * the device is known to the SCI Core since it is contained in the
1711 * scic_phy object. Remote node context(s) is/are a global resource
1712 * allocated by this routine, freed by scic_remote_device_destruct().
1713 *
1714 * Returns:
1715 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1716 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1717 * sata-only controller instance.
1718 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001719 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001720static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1721 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001722{
1723 enum sci_status status;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001724 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001725
Dan Williamsb87ee302011-04-25 11:48:29 -07001726 scic_remote_device_construct(sci_port, sci_dev);
1727
Dan Williams88f3b622011-04-22 19:18:03 -07001728 /*
1729 * This information is request to determine how many remote node context
1730 * entries will be needed to store the remote node.
1731 */
Dan Williams88f3b622011-04-22 19:18:03 -07001732 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001733 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1734 sci_dev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001735 &sci_dev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001736
Dan Williamsa1a113b2011-04-21 18:44:45 -07001737 if (status != SCI_SUCCESS)
1738 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001739
Dan Williamsab2e8f72011-04-27 16:32:45 -07001740 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1741 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1742 /* pass */;
1743 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001744 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001745
Dan Williamsa1a113b2011-04-21 18:44:45 -07001746 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001747
Dan Williamsa1a113b2011-04-21 18:44:45 -07001748 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1749 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001750
Dan Williamsa1a113b2011-04-21 18:44:45 -07001751 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001752}
1753
Dan Williams88f3b622011-04-22 19:18:03 -07001754/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001755 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001756 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001757 * Remote node context(s) is/are a global resource allocated by this
1758 * routine, freed by scic_remote_device_destruct().
1759 *
1760 * Returns:
1761 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1762 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1763 * sata-only controller instance.
1764 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001765 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001766static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001767 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001768{
Dan Williamsa1a113b2011-04-21 18:44:45 -07001769 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001770 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001771
Dan Williamsb87ee302011-04-25 11:48:29 -07001772 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001773
Dan Williamsab2e8f72011-04-27 16:32:45 -07001774 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1775 sci_dev,
1776 &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001777 if (status != SCI_SUCCESS)
1778 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001779
Dan Williamsab2e8f72011-04-27 16:32:45 -07001780 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1781 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1782 /* pass */;
1783 else
1784 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001785
Dan Williamsa1a113b2011-04-21 18:44:45 -07001786 /*
1787 * For SAS-2 the physical link rate is actually a logical link
1788 * rate that incorporates multiplexing. The SCU doesn't
1789 * incorporate multiplexing and for the purposes of the
1790 * connection the logical link rate is that same as the
1791 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1792 * one another, so this code works for both situations. */
1793 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001794 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001795
Dan Williamsa1a113b2011-04-21 18:44:45 -07001796 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1797 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001798
Dan Williamsab2e8f72011-04-27 16:32:45 -07001799 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001800}
1801
1802/**
1803 * scic_remote_device_start() - This method will start the supplied remote
1804 * device. This method enables normal IO requests to flow through to the
1805 * remote device.
1806 * @remote_device: This parameter specifies the device to be started.
1807 * @timeout: This parameter specifies the number of milliseconds in which the
1808 * start operation should complete.
1809 *
1810 * An indication of whether the device was successfully started. SCI_SUCCESS
1811 * This value is returned if the device was successfully started.
1812 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1813 * the device when there have been no phys added to it.
1814 */
1815static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
Dan Williamseb229672011-05-01 14:05:57 -07001816 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001817{
Dan Williamseb229672011-05-01 14:05:57 -07001818 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1819 enum scic_sds_remote_device_states state = sm->current_state_id;
1820 enum sci_status status;
1821
1822 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1823 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1824 __func__, state);
1825 return SCI_FAILURE_INVALID_STATE;
1826 }
1827
1828 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1829 remote_device_resume_done,
1830 sci_dev);
1831 if (status != SCI_SUCCESS)
1832 return status;
1833
1834 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1835
1836 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001837}
Dan Williams6f231dd2011-07-02 22:56:22 -07001838
Dan Williams00d680e2011-04-25 14:29:29 -07001839static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1840 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001841{
Dan Williams00d680e2011-04-25 14:29:29 -07001842 struct scic_sds_port *sci_port = iport->sci_port_handle;
1843 struct isci_host *ihost = iport->isci_host;
1844 struct domain_device *dev = idev->domain_dev;
1845 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001846
Dan Williams00d680e2011-04-25 14:29:29 -07001847 if (dev->parent && dev_is_expander(dev->parent))
1848 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1849 else
1850 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001851
1852 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001853 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1854 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001855
1856 return status;
1857 }
1858
Dan Williams6f231dd2011-07-02 22:56:22 -07001859 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001860 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001861
Dan Williams00d680e2011-04-25 14:29:29 -07001862 if (status != SCI_SUCCESS)
1863 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1864 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001865
1866 return status;
1867}
1868
Dan Williams4393aa42011-03-31 13:10:44 -07001869void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001870{
1871 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001872
Dan Williams4393aa42011-03-31 13:10:44 -07001873 dev_dbg(&ihost->pdev->dev,
1874 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001875
1876 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -07001877 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -07001878
Dan Williams4393aa42011-03-31 13:10:44 -07001879 dev_dbg(&ihost->pdev->dev,
1880 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001881}
1882
Dan Williams6f231dd2011-07-02 22:56:22 -07001883/**
1884 * This function builds the isci_remote_device when a libsas dev_found message
1885 * is received.
1886 * @isci_host: This parameter specifies the isci host object.
1887 * @port: This parameter specifies the isci_port conected to this device.
1888 *
1889 * pointer to new isci_remote_device.
1890 */
1891static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001892isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001893{
Dan Williamsd9c37392011-03-03 17:59:32 -08001894 struct isci_remote_device *idev;
1895 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001896
Dan Williamsd9c37392011-03-03 17:59:32 -08001897 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001898 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001899 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1900 break;
1901 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001902
Dan Williamsd9c37392011-03-03 17:59:32 -08001903 if (i >= SCI_MAX_REMOTE_DEVICES) {
1904 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001905 return NULL;
1906 }
1907
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001908 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1909 return NULL;
1910
1911 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1912 return NULL;
1913
Dan Williamsd9c37392011-03-03 17:59:32 -08001914 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001915
Dan Williamsd9c37392011-03-03 17:59:32 -08001916 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001917}
Dan Williams6f231dd2011-07-02 22:56:22 -07001918
1919/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001920 * isci_remote_device_stop() - This function is called internally to stop the
1921 * remote device.
1922 * @isci_host: This parameter specifies the isci host object.
1923 * @isci_device: This parameter specifies the remote device.
1924 *
1925 * The status of the scic request to stop.
1926 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001927enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001928{
1929 enum sci_status status;
1930 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001931
Dan Williams6ad31fe2011-03-04 12:10:29 -08001932 dev_dbg(&ihost->pdev->dev,
1933 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001934
Dan Williams6ad31fe2011-03-04 12:10:29 -08001935 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001936
1937 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001938 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001939
Dan Williams6ad31fe2011-03-04 12:10:29 -08001940 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001941
Dan Williams6ad31fe2011-03-04 12:10:29 -08001942 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07001943 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001944 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001945
1946 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -08001947 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -08001948 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08001949 clear_bit(IDEV_ALLOCATED, &idev->flags);
1950 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001951
Dan Williams6ad31fe2011-03-04 12:10:29 -08001952 dev_dbg(&ihost->pdev->dev,
1953 "%s: idev = %p - after completion wait\n",
1954 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001955
Dan Williams6f231dd2011-07-02 22:56:22 -07001956 return status;
1957}
1958
1959/**
1960 * isci_remote_device_gone() - This function is called by libsas when a domain
1961 * device is removed.
1962 * @domain_device: This parameter specifies the libsas domain device.
1963 *
1964 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001965void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001966{
Dan Williams4393aa42011-03-31 13:10:44 -07001967 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001968 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001969
Dan Williams6ad31fe2011-03-04 12:10:29 -08001970 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001971 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001972 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001973
Dan Williams6ad31fe2011-03-04 12:10:29 -08001974 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001975}
1976
1977
1978/**
1979 * isci_remote_device_found() - This function is called by libsas when a remote
1980 * device is discovered. A remote device object is created and started. the
1981 * function then sleeps until the sci core device started message is
1982 * received.
1983 * @domain_device: This parameter specifies the libsas domain device.
1984 *
1985 * status, zero indicates success.
1986 */
1987int isci_remote_device_found(struct domain_device *domain_dev)
1988{
Dan Williams4393aa42011-03-31 13:10:44 -07001989 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001990 struct isci_port *isci_port;
1991 struct isci_phy *isci_phy;
1992 struct asd_sas_port *sas_port;
1993 struct asd_sas_phy *sas_phy;
1994 struct isci_remote_device *isci_device;
1995 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001996
Dan Williams6f231dd2011-07-02 22:56:22 -07001997 dev_dbg(&isci_host->pdev->dev,
1998 "%s: domain_device = %p\n", __func__, domain_dev);
1999
Dan Williams0cf89d12011-02-18 09:25:07 -08002000 wait_for_start(isci_host);
2001
Dan Williams6f231dd2011-07-02 22:56:22 -07002002 sas_port = domain_dev->port;
2003 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
2004 port_phy_el);
2005 isci_phy = to_isci_phy(sas_phy);
2006 isci_port = isci_phy->isci_port;
2007
2008 /* we are being called for a device on this port,
2009 * so it has to come up eventually
2010 */
2011 wait_for_completion(&isci_port->start_complete);
2012
2013 if ((isci_stopping == isci_port_get_state(isci_port)) ||
2014 (isci_stopped == isci_port_get_state(isci_port)))
2015 return -ENODEV;
2016
2017 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08002018 if (!isci_device)
2019 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07002020
2021 INIT_LIST_HEAD(&isci_device->node);
2022 domain_dev->lldd_dev = isci_device;
2023 isci_device->domain_dev = domain_dev;
2024 isci_device->isci_port = isci_port;
2025 isci_remote_device_change_state(isci_device, isci_starting);
2026
2027
Dan Williams1a380452011-03-03 18:01:43 -08002028 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002029 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
2030
Dan Williams6ad31fe2011-03-04 12:10:29 -08002031 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002032 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -08002033 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002034
Dan Williams6f231dd2011-07-02 22:56:22 -07002035 dev_dbg(&isci_host->pdev->dev,
2036 "%s: isci_device = %p\n",
2037 __func__, isci_device);
2038
2039 if (status != SCI_SUCCESS) {
2040
Dan Williams1a380452011-03-03 18:01:43 -08002041 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002042 isci_remote_device_deconstruct(
2043 isci_host,
2044 isci_device
2045 );
Dan Williams1a380452011-03-03 18:01:43 -08002046 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002047 return -ENODEV;
2048 }
2049
Dan Williams6ad31fe2011-03-04 12:10:29 -08002050 /* wait for the device ready callback. */
2051 wait_for_device_start(isci_host, isci_device);
2052
Dan Williams6f231dd2011-07-02 22:56:22 -07002053 return 0;
2054}
2055/**
2056 * isci_device_is_reset_pending() - This function will check if there is any
2057 * pending reset condition on the device.
2058 * @request: This parameter is the isci_device object.
2059 *
2060 * true if there is a reset pending for the device.
2061 */
2062bool isci_device_is_reset_pending(
2063 struct isci_host *isci_host,
2064 struct isci_remote_device *isci_device)
2065{
2066 struct isci_request *isci_request;
2067 struct isci_request *tmp_req;
2068 bool reset_is_pending = false;
2069 unsigned long flags;
2070
2071 dev_dbg(&isci_host->pdev->dev,
2072 "%s: isci_device = %p\n", __func__, isci_device);
2073
2074 spin_lock_irqsave(&isci_host->scic_lock, flags);
2075
2076 /* Check for reset on all pending requests. */
2077 list_for_each_entry_safe(isci_request, tmp_req,
2078 &isci_device->reqs_in_process, dev_node) {
2079 dev_dbg(&isci_host->pdev->dev,
2080 "%s: isci_device = %p request = %p\n",
2081 __func__, isci_device, isci_request);
2082
2083 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07002084 struct sas_task *task = isci_request_access_task(
2085 isci_request);
2086
Bartosz Barcinski467e8552011-04-12 17:28:41 -07002087 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002088 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
2089 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07002090 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002091 }
2092 }
2093
2094 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
2095
2096 dev_dbg(&isci_host->pdev->dev,
2097 "%s: isci_device = %p reset_is_pending = %d\n",
2098 __func__, isci_device, reset_is_pending);
2099
2100 return reset_is_pending;
2101}
2102
2103/**
2104 * isci_device_clear_reset_pending() - This function will clear if any pending
2105 * reset condition flags on the device.
2106 * @request: This parameter is the isci_device object.
2107 *
2108 * true if there is a reset pending for the device.
2109 */
Dan Williams4393aa42011-03-31 13:10:44 -07002110void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002111{
2112 struct isci_request *isci_request;
2113 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07002114 unsigned long flags = 0;
2115
Dan Williams4393aa42011-03-31 13:10:44 -07002116 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
2117 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07002118
Dan Williams4393aa42011-03-31 13:10:44 -07002119 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002120
2121 /* Clear reset pending on all pending requests. */
2122 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07002123 &idev->reqs_in_process, dev_node) {
2124 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
2125 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07002126
2127 if (isci_request->ttype == io_task) {
2128
2129 unsigned long flags2;
2130 struct sas_task *task = isci_request_access_task(
2131 isci_request);
2132
2133 spin_lock_irqsave(&task->task_state_lock, flags2);
2134 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
2135 spin_unlock_irqrestore(&task->task_state_lock, flags2);
2136 }
2137 }
Dan Williams4393aa42011-03-31 13:10:44 -07002138 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002139}