blob: 53f4ecfddce41e79bf5a72eb32594f8691f20feb [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
Dan Williams88f3b622011-04-22 19:18:03 -070055#include "intel_sas.h"
Dan Williamsab2e8f72011-04-27 16:32:45 -070056#include "intel_ata.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070058#include "port.h"
59#include "remote_device.h"
60#include "request.h"
Dan Williams88f3b622011-04-22 19:18:03 -070061#include "scic_controller.h"
62#include "scic_io_request.h"
63#include "scic_phy.h"
64#include "scic_port.h"
65#include "scic_sds_controller.h"
66#include "scic_sds_phy.h"
67#include "scic_sds_port.h"
68#include "remote_node_context.h"
69#include "scic_sds_request.h"
70#include "sci_environment.h"
71#include "sci_util.h"
72#include "scu_event_codes.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070073#include "task.h"
74
Dan Williamsab2e8f72011-04-27 16:32:45 -070075/**
76 * isci_remote_device_change_state() - This function gets the status of the
77 * remote_device object.
78 * @isci_device: This parameter points to the isci_remote_device object
79 *
80 * status of the object as a isci_status enum.
81 */
82void isci_remote_device_change_state(
83 struct isci_remote_device *isci_device,
84 enum isci_status status)
85{
86 unsigned long flags;
87
88 spin_lock_irqsave(&isci_device->state_lock, flags);
89 isci_device->status = status;
90 spin_unlock_irqrestore(&isci_device->state_lock, flags);
91}
92
93/**
94 * isci_remote_device_not_ready() - This function is called by the scic when
95 * the remote device is not ready. We mark the isci device as ready (not
96 * "ready_for_io") and signal the waiting proccess.
97 * @isci_host: This parameter specifies the isci host object.
98 * @isci_device: This parameter specifies the remote device
99 *
100 */
101static void isci_remote_device_not_ready(struct isci_host *ihost,
102 struct isci_remote_device *idev, u32 reason)
103{
104 dev_dbg(&ihost->pdev->dev,
105 "%s: isci_device = %p\n", __func__, idev);
106
107 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
108 isci_remote_device_change_state(idev, isci_stopping);
109 else
110 /* device ready is actually a "not ready for io" state. */
111 isci_remote_device_change_state(idev, isci_ready);
112}
113
114/**
115 * isci_remote_device_ready() - This function is called by the scic when the
116 * remote device is ready. We mark the isci device as ready and signal the
117 * waiting proccess.
118 * @ihost: our valid isci_host
119 * @idev: remote device
120 *
121 */
122static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
123{
124 dev_dbg(&ihost->pdev->dev,
125 "%s: idev = %p\n", __func__, idev);
126
127 isci_remote_device_change_state(idev, isci_ready_for_io);
128 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
129 wake_up(&ihost->eventq);
130}
131
Dan Williamsec575662011-05-01 14:19:25 -0700132/* called once the remote node context is ready to be freed.
133 * The remote device can now report that its stop operation is complete. none
134 */
135static void rnc_destruct_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700136{
Dan Williamsec575662011-05-01 14:19:25 -0700137 struct scic_sds_remote_device *sci_dev = _dev;
138
139 BUG_ON(sci_dev->started_request_count != 0);
140 sci_base_state_machine_change_state(&sci_dev->state_machine,
141 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
142}
143
144static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
145{
146 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
147 u32 i, request_count = sci_dev->started_request_count;
148 enum sci_status status = SCI_SUCCESS;
149
150 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
151 struct scic_sds_request *sci_req;
152 enum sci_status s;
153
154 sci_req = scic->io_request_table[i];
155 if (!sci_req || sci_req->target_device != sci_dev)
156 continue;
157 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
158 if (s != SCI_SUCCESS)
159 status = s;
160 }
161
162 return status;
163}
164
165enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
166 u32 timeout)
167{
168 struct sci_base_state_machine *sm = &sci_dev->state_machine;
169 enum scic_sds_remote_device_states state = sm->current_state_id;
170
171 switch (state) {
172 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
173 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
174 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
175 default:
176 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
177 __func__, state);
178 return SCI_FAILURE_INVALID_STATE;
179 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
180 return SCI_SUCCESS;
181 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
182 /* device not started so there had better be no requests */
183 BUG_ON(sci_dev->started_request_count != 0);
184 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
185 rnc_destruct_done, sci_dev);
186 /* Transition to the stopping state and wait for the
187 * remote node to complete being posted and invalidated.
188 */
189 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
190 return SCI_SUCCESS;
191 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
192 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
193 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
194 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
195 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
196 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
197 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
198 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
199 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
200 if (sci_dev->started_request_count == 0) {
201 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
202 rnc_destruct_done, sci_dev);
203 return SCI_SUCCESS;
204 } else
205 return scic_sds_remote_device_terminate_requests(sci_dev);
206 break;
207 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
208 /* All requests should have been terminated, but if there is an
209 * attempt to stop a device already in the stopping state, then
210 * try again to terminate.
211 */
212 return scic_sds_remote_device_terminate_requests(sci_dev);
213 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
214 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
215 return SCI_SUCCESS;
216 }
Dan Williams88f3b622011-04-22 19:18:03 -0700217}
Dan Williams6f231dd2011-07-02 22:56:22 -0700218
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700219enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700220{
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700221 struct sci_base_state_machine *sm = &sci_dev->state_machine;
222 enum scic_sds_remote_device_states state = sm->current_state_id;
223
224 switch (state) {
225 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
226 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
227 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
228 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
229 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
230 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
231 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
232 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
233 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
234 default:
235 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
236 __func__, state);
237 return SCI_FAILURE_INVALID_STATE;
238 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
239 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
240 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
241 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
242 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
243 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
244 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
245 return SCI_SUCCESS;
246 }
Dan Williams88f3b622011-04-22 19:18:03 -0700247}
248
Dan Williams81515182011-05-01 14:53:00 -0700249enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700250{
Dan Williams81515182011-05-01 14:53:00 -0700251 struct sci_base_state_machine *sm = &sci_dev->state_machine;
252 enum scic_sds_remote_device_states state = sm->current_state_id;
Dan Williams88f3b622011-04-22 19:18:03 -0700253
Dan Williams81515182011-05-01 14:53:00 -0700254 if (state != SCI_BASE_REMOTE_DEVICE_STATE_RESETTING) {
255 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
256 __func__, state);
257 return SCI_FAILURE_INVALID_STATE;
258 }
259
260 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_READY);
261 return SCI_SUCCESS;
262}
Dan Williams88f3b622011-04-22 19:18:03 -0700263
Dan Williams323f0ec2011-05-01 16:15:47 -0700264enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
265 u32 suspend_type)
Dan Williams88f3b622011-04-22 19:18:03 -0700266{
Dan Williams323f0ec2011-05-01 16:15:47 -0700267 struct sci_base_state_machine *sm = &sci_dev->state_machine;
268 enum scic_sds_remote_device_states state = sm->current_state_id;
269
270 if (state != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD) {
271 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
272 __func__, state);
273 return SCI_FAILURE_INVALID_STATE;
274 }
275
276 return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
277 suspend_type, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700278}
279
280/**
281 *
282 * @sci_dev: The remote device for which the event handling is being
283 * requested.
284 * @frame_index: This is the frame index that is being processed.
285 *
286 * This method invokes the frame handler for the remote device state machine
287 * enum sci_status
288 */
289enum sci_status scic_sds_remote_device_frame_handler(
290 struct scic_sds_remote_device *sci_dev,
291 u32 frame_index)
292{
293 return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
294}
295
Dan Williamse6225712011-05-01 16:26:09 -0700296static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700297{
Dan Williamse6225712011-05-01 16:26:09 -0700298
299 struct sci_base_state_machine *sm = &sci_dev->state_machine;
300 enum scic_sds_remote_device_states state = sm->current_state_id;
301
302 switch (state) {
303 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
304 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
305 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
306 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
307 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
308 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
309 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
310 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
311 return true;
312 default:
313 return false;
314 }
315}
316
317enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
318 u32 event_code)
319{
320 struct sci_base_state_machine *sm = &sci_dev->state_machine;
321 enum scic_sds_remote_device_states state = sm->current_state_id;
322 enum sci_status status;
323
324 switch (scu_get_event_type(event_code)) {
325 case SCU_EVENT_TYPE_RNC_OPS_MISC:
326 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
327 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
328 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
329 break;
330 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
331 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
332 status = SCI_SUCCESS;
333
334 /* Suspend the associated RNC */
335 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
336 SCI_SOFTWARE_SUSPENSION,
337 NULL, NULL);
338
339 dev_dbg(scirdev_to_dev(sci_dev),
340 "%s: device: %p event code: %x: %s\n",
341 __func__, sci_dev, event_code,
342 is_remote_device_ready(sci_dev)
343 ? "I_T_Nexus_Timeout event"
344 : "I_T_Nexus_Timeout event in wrong state");
345
346 break;
347 }
348 /* Else, fall through and treat as unhandled... */
349 default:
350 dev_dbg(scirdev_to_dev(sci_dev),
351 "%s: device: %p event code: %x: %s\n",
352 __func__, sci_dev, event_code,
353 is_remote_device_ready(sci_dev)
354 ? "unexpected event"
355 : "unexpected event in wrong state");
356 status = SCI_FAILURE_INVALID_STATE;
357 break;
358 }
359
360 if (status != SCI_SUCCESS)
361 return status;
362
363 if (state == SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE) {
364
365 /* We pick up suspension events to handle specifically to this
366 * state. We resume the RNC right away.
367 */
368 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
369 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
370 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
371 }
372
373 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700374}
375
Dan Williams18606552011-05-01 14:57:11 -0700376static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
377 struct scic_sds_request *sci_req,
378 enum sci_status status)
Dan Williams88f3b622011-04-22 19:18:03 -0700379{
Dan Williams18606552011-05-01 14:57:11 -0700380 struct scic_sds_port *sci_port = sci_dev->owning_port;
381
382 /* cleanup requests that failed after starting on the port */
383 if (status != SCI_SUCCESS)
384 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
385 else
386 scic_sds_remote_device_increment_request_count(sci_dev);
387}
388
389enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
390 struct scic_sds_remote_device *sci_dev,
391 struct scic_sds_request *sci_req)
392{
393 struct sci_base_state_machine *sm = &sci_dev->state_machine;
394 enum scic_sds_remote_device_states state = sm->current_state_id;
395 struct scic_sds_port *sci_port = sci_dev->owning_port;
396 enum sci_status status;
397
398 switch (state) {
399 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
400 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
401 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
402 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
403 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
404 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
405 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
406 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
407 default:
408 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
409 __func__, state);
410 return SCI_FAILURE_INVALID_STATE;
411 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
412 /* attempt to start an io request for this device object. The remote
413 * device object will issue the start request for the io and if
414 * successful it will start the request for the port object then
415 * increment its own request count.
416 */
417 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
418 if (status != SCI_SUCCESS)
419 return status;
420
421 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
422 if (status != SCI_SUCCESS)
423 break;
424
425 status = scic_sds_request_start(sci_req);
426 break;
427 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
428 /* handle the start io operation for a sata device that is in
429 * the command idle state. - Evalute the type of IO request to
430 * be started - If its an NCQ request change to NCQ substate -
431 * If its any other command change to the CMD substate
432 *
433 * If this is a softreset we may want to have a different
434 * substate.
435 */
436 enum scic_sds_remote_device_states new_state;
437
438 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
439 if (status != SCI_SUCCESS)
440 return status;
441
442 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
443 if (status != SCI_SUCCESS)
444 break;
445
446 status = sci_req->state_handlers->start_handler(sci_req);
447 if (status != SCI_SUCCESS)
448 break;
449
450 if (isci_sata_get_sat_protocol(sci_req->ireq) == SAT_PROTOCOL_FPDMA)
451 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
452 else {
453 sci_dev->working_request = sci_req;
454 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
455 }
456 sci_base_state_machine_change_state(sm, new_state);
457 break;
458 }
459 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
460 if (isci_sata_get_sat_protocol(sci_req->ireq) == SAT_PROTOCOL_FPDMA) {
461 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
462 if (status != SCI_SUCCESS)
463 return status;
464
465 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
466 if (status != SCI_SUCCESS)
467 break;
468
469 status = sci_req->state_handlers->start_handler(sci_req);
470 } else
471 return SCI_FAILURE_INVALID_STATE;
472 break;
473 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
474 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
475 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
476 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
477 if (status != SCI_SUCCESS)
478 return status;
479
480 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
481 if (status != SCI_SUCCESS)
482 break;
483
484 status = scic_sds_request_start(sci_req);
485 if (status != SCI_SUCCESS)
486 break;
487
488 sci_dev->working_request = sci_req;
489 sci_base_state_machine_change_state(&sci_dev->state_machine,
490 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
491 break;
492 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
493 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
494 /* device is already handling a command it can not accept new commands
495 * until this one is complete.
496 */
497 return SCI_FAILURE_INVALID_STATE;
498 }
499
500 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
501 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700502}
503
Dan Williams10a09e62011-05-01 15:33:43 -0700504static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
505 struct scic_sds_remote_device *sci_dev,
506 struct scic_sds_request *sci_req)
Dan Williams88f3b622011-04-22 19:18:03 -0700507{
Dan Williams10a09e62011-05-01 15:33:43 -0700508 enum sci_status status;
509
510 status = scic_sds_request_complete(sci_req);
511 if (status != SCI_SUCCESS)
512 return status;
513
514 status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
515 if (status != SCI_SUCCESS)
516 return status;
517
518 scic_sds_remote_device_decrement_request_count(sci_dev);
519 return status;
520}
521
522enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
523 struct scic_sds_remote_device *sci_dev,
524 struct scic_sds_request *sci_req)
525{
526 struct sci_base_state_machine *sm = &sci_dev->state_machine;
527 enum scic_sds_remote_device_states state = sm->current_state_id;
528 struct scic_sds_port *sci_port = sci_dev->owning_port;
529 enum sci_status status;
530
531 switch (state) {
532 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
533 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
534 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
535 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
536 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
537 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
538 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
539 default:
540 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
541 __func__, state);
542 return SCI_FAILURE_INVALID_STATE;
543 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
544 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
545 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
546 status = common_complete_io(sci_port, sci_dev, sci_req);
547 break;
548 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
549 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
550 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
551 status = common_complete_io(sci_port, sci_dev, sci_req);
552 if (status != SCI_SUCCESS)
553 break;
554
555 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
556 /* This request causes hardware error, device needs to be Lun Reset.
557 * So here we force the state machine to IDLE state so the rest IOs
558 * can reach RNC state handler, these IOs will be completed by RNC with
559 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
560 */
561 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
562 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
563 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
564 break;
565 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
566 status = common_complete_io(sci_port, sci_dev, sci_req);
567 if (status != SCI_SUCCESS)
568 break;
569 sci_base_state_machine_change_state(sm, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
570 break;
571 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
572 status = common_complete_io(sci_port, sci_dev, sci_req);
573 if (status != SCI_SUCCESS)
574 break;
575
576 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
577 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
578 rnc_destruct_done,
579 sci_dev);
580 break;
581 }
582
583 if (status != SCI_SUCCESS)
584 dev_err(scirdev_to_dev(sci_dev),
585 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
586 "could not complete\n", __func__, sci_port,
587 sci_dev, sci_req, status);
588
589 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700590}
591
Dan Williams84b9b022011-05-01 15:53:25 -0700592static void scic_sds_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700593{
Dan Williams84b9b022011-05-01 15:53:25 -0700594 struct scic_sds_remote_device *sci_dev = dev;
595
596 /* we need to check if this request is still valid to continue. */
597 if (sci_dev->working_request)
598 scic_controller_continue_io(sci_dev->working_request);
599}
600
601enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
602 struct scic_sds_remote_device *sci_dev,
603 struct scic_sds_request *sci_req)
604{
605 struct sci_base_state_machine *sm = &sci_dev->state_machine;
606 enum scic_sds_remote_device_states state = sm->current_state_id;
607 struct scic_sds_port *sci_port = sci_dev->owning_port;
608 enum sci_status status;
609
610 switch (state) {
611 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
612 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
613 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
614 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
615 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
616 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
617 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
618 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
619 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
620 default:
621 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
622 __func__, state);
623 return SCI_FAILURE_INVALID_STATE;
624 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
625 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
626 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
627 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
628 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
629 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
630 if (status != SCI_SUCCESS)
631 return status;
632
633 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
634 if (status != SCI_SUCCESS)
635 goto out;
636
637 status = sci_req->state_handlers->start_handler(sci_req);
638 if (status != SCI_SUCCESS)
639 goto out;
640
641 /* Note: If the remote device state is not IDLE this will
642 * replace the request that probably resulted in the task
643 * management request.
644 */
645 sci_dev->working_request = sci_req;
646 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
647
648 /* The remote node context must cleanup the TCi to NCQ mapping
649 * table. The only way to do this correctly is to either write
650 * to the TLCR register or to invalidate and repost the RNC. In
651 * either case the remote node context state machine will take
652 * the correct action when the remote node context is suspended
653 * and later resumed.
654 */
655 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
656 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
657 scic_sds_remote_node_context_resume(&sci_dev->rnc,
658 scic_sds_remote_device_continue_request,
659 sci_dev);
660
661 out:
662 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
663 /* We need to let the controller start request handler know that
664 * it can't post TC yet. We will provide a callback function to
665 * post TC when RNC gets resumed.
666 */
667 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
668 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
669 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
670 if (status != SCI_SUCCESS)
671 return status;
672
673 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
674 if (status != SCI_SUCCESS)
675 break;
676
677 status = scic_sds_request_start(sci_req);
678 break;
679 }
680 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
681
682 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700683}
684
685/**
686 *
Dan Williams88f3b622011-04-22 19:18:03 -0700687 * @sci_dev:
688 * @request:
689 *
690 * This method takes the request and bulids an appropriate SCU context for the
691 * request and then requests the controller to post the request. none
692 */
693void scic_sds_remote_device_post_request(
694 struct scic_sds_remote_device *sci_dev,
695 u32 request)
696{
697 u32 context;
698
699 context = scic_sds_remote_device_build_command_context(sci_dev, request);
700
701 scic_sds_controller_post_request(
702 scic_sds_remote_device_get_controller(sci_dev),
703 context
704 );
705}
706
Dan Williamsab2e8f72011-04-27 16:32:45 -0700707/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700708 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700709 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700710 */
Dan Williamseb229672011-05-01 14:05:57 -0700711static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700712{
Dan Williamsab2e8f72011-04-27 16:32:45 -0700713 struct scic_sds_remote_device *sci_dev = _dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700714
Dan Williamse6225712011-05-01 16:26:09 -0700715 if (is_remote_device_ready(sci_dev))
716 return;
Dan Williams88f3b622011-04-22 19:18:03 -0700717
Dan Williamse6225712011-05-01 16:26:09 -0700718 /* go 'ready' if we are not already in a ready state */
719 sci_base_state_machine_change_state(&sci_dev->state_machine,
720 SCI_BASE_REMOTE_DEVICE_STATE_READY);
Dan Williams88f3b622011-04-22 19:18:03 -0700721}
722
723/**
724 *
725 * @device: The struct scic_sds_remote_device which is then cast into a
726 * struct scic_sds_remote_device.
727 * @frame_index: The frame index for which the struct scic_sds_controller wants this
728 * device object to process.
729 *
730 * This method is the default unsolicited frame handler. It logs a warning,
731 * releases the frame and returns a failure. enum sci_status
732 * SCI_FAILURE_INVALID_STATE
733 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700734static enum sci_status scic_sds_remote_device_default_frame_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700735 struct scic_sds_remote_device *sci_dev,
736 u32 frame_index)
737{
738 dev_warn(scirdev_to_dev(sci_dev),
739 "%s: SCIC Remote Device requested to handle frame %x "
740 "while in wrong state %d\n",
741 __func__,
742 frame_index,
743 sci_base_state_machine_get_state(
744 &sci_dev->state_machine));
745
746 /* Return the frame back to the controller */
747 scic_sds_controller_release_frame(
748 scic_sds_remote_device_get_controller(sci_dev), frame_index
749 );
750
751 return SCI_FAILURE_INVALID_STATE;
752}
753
Dan Williams88f3b622011-04-22 19:18:03 -0700754/**
755 *
756 * @device: The struct scic_sds_remote_device which is then cast into a
757 * struct scic_sds_remote_device.
758 * @frame_index: The frame index for which the struct scic_sds_controller wants this
759 * device object to process.
760 *
761 * This method is a general ssp frame handler. In most cases the device object
762 * needs to route the unsolicited frame processing to the io request object.
763 * This method decodes the tag for the io request object and routes the
764 * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
765 */
Dan Williamsab2e8f72011-04-27 16:32:45 -0700766static enum sci_status scic_sds_remote_device_general_frame_handler(
Dan Williams88f3b622011-04-22 19:18:03 -0700767 struct scic_sds_remote_device *sci_dev,
768 u32 frame_index)
769{
770 enum sci_status result;
771 struct sci_ssp_frame_header *frame_header;
772 struct scic_sds_request *io_request;
773
774 result = scic_sds_unsolicited_frame_control_get_header(
775 &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
776 frame_index,
777 (void **)&frame_header
778 );
779
780 if (SCI_SUCCESS == result) {
781 io_request = scic_sds_controller_get_io_request_from_tag(
782 scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
783
784 if ((io_request == NULL)
785 || (io_request->target_device != sci_dev)) {
786 /*
787 * We could not map this tag to a valid IO request
788 * Just toss the frame and continue */
789 scic_sds_controller_release_frame(
790 scic_sds_remote_device_get_controller(sci_dev), frame_index
791 );
792 } else {
793 /* The IO request is now in charge of releasing the frame */
794 result = io_request->state_handlers->frame_handler(
795 io_request, frame_index);
796 }
797 }
798
799 return result;
800}
801
Dan Williamsab2e8f72011-04-27 16:32:45 -0700802static enum sci_status scic_sds_stp_remote_device_ready_ncq_substate_frame_handler(struct scic_sds_remote_device *sci_dev,
803 u32 frame_index)
804{
805 enum sci_status status;
806 struct sata_fis_header *frame_header;
807 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
808
809 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
810 frame_index,
811 (void **)&frame_header);
812 if (status != SCI_SUCCESS)
813 return status;
814
815 if (frame_header->fis_type == SATA_FIS_TYPE_SETDEVBITS &&
816 (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
817 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
818
819 /* TODO Check sactive and complete associated IO if any. */
820
821 sci_base_state_machine_change_state(&sci_dev->state_machine,
822 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
823 } else if (frame_header->fis_type == SATA_FIS_TYPE_REGD2H &&
824 (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
825 /*
826 * Some devices return D2H FIS when an NCQ error is detected.
827 * Treat this like an SDB error FIS ready reason.
828 */
829 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
830
831 sci_base_state_machine_change_state(&sci_dev->state_machine,
832 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
833 } else
834 status = SCI_FAILURE;
835
836 scic_sds_controller_release_frame(scic, frame_index);
837
838 return status;
839}
840
Dan Williamsab2e8f72011-04-27 16:32:45 -0700841static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_frame_handler(
842 struct scic_sds_remote_device *sci_dev,
843 u32 frame_index)
844{
845 /* The device doe not process any UF received from the hardware while
846 * in this state. All unsolicited frames are forwarded to the io
847 * request object.
848 */
849 return scic_sds_io_request_frame_handler(sci_dev->working_request,
850 frame_index);
851}
852
Dan Williamsab2e8f72011-04-27 16:32:45 -0700853static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
854{
855 struct scic_sds_remote_device *sci_dev = _dev;
856 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
857 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
858
859 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
860 * As a result, avoid sending the ready notification.
861 */
862 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
863 isci_remote_device_ready(scic->ihost, idev);
864}
865
Dan Williamsab2e8f72011-04-27 16:32:45 -0700866static enum sci_status scic_sds_smp_remote_device_ready_cmd_substate_frame_handler(
867 struct scic_sds_remote_device *sci_dev,
868 u32 frame_index)
869{
870 enum sci_status status;
871
872 /* The device does not process any UF received from the hardware while
873 * in this state. All unsolicited frames are forwarded to the io request
874 * object.
875 */
876 status = scic_sds_io_request_frame_handler(
877 sci_dev->working_request,
878 frame_index
879 );
880
881 return status;
882}
883
Dan Williams88f3b622011-04-22 19:18:03 -0700884static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
885 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700886 .frame_handler = scic_sds_remote_device_default_frame_handler
887 },
888 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700889 .frame_handler = scic_sds_remote_device_default_frame_handler
890 },
891 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700892 .frame_handler = scic_sds_remote_device_default_frame_handler
893 },
894 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700895 .frame_handler = scic_sds_remote_device_general_frame_handler,
896 },
Dan Williamsab2e8f72011-04-27 16:32:45 -0700897 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700898 .frame_handler = scic_sds_remote_device_default_frame_handler
899 },
900 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700901 .frame_handler = scic_sds_stp_remote_device_ready_cmd_substate_frame_handler
902 },
903 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700904 .frame_handler = scic_sds_stp_remote_device_ready_ncq_substate_frame_handler
905 },
906 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700907 .frame_handler = scic_sds_remote_device_general_frame_handler
908 },
909 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700910 .frame_handler = scic_sds_remote_device_general_frame_handler
911 },
912 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700913 .frame_handler = scic_sds_remote_device_default_frame_handler
914 },
915 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700916 .frame_handler = scic_sds_smp_remote_device_ready_cmd_substate_frame_handler
917 },
Dan Williams88f3b622011-04-22 19:18:03 -0700918 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700919 .frame_handler = scic_sds_remote_device_general_frame_handler
920 },
921 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700922 .frame_handler = scic_sds_remote_device_general_frame_handler
923 },
924 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700925 .frame_handler = scic_sds_remote_device_general_frame_handler
926 },
927 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700928 .frame_handler = scic_sds_remote_device_default_frame_handler
929 }
930};
931
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000932static void scic_sds_remote_device_initial_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700933{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000934 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700935
Dan Williams88f3b622011-04-22 19:18:03 -0700936 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
937 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
938
939 /* Initial state is a transitional state to the stopped state */
940 sci_base_state_machine_change_state(&sci_dev->state_machine,
941 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
942}
943
944/**
Dan Williams88f3b622011-04-22 19:18:03 -0700945 * scic_remote_device_destruct() - free remote node context and destruct
946 * @remote_device: This parameter specifies the remote device to be destructed.
947 *
948 * Remote device objects are a limited resource. As such, they must be
949 * protected. Thus calls to construct and destruct are mutually exclusive and
950 * non-reentrant. The return value shall indicate if the device was
951 * successfully destructed or if some failure occurred. enum sci_status This value
952 * is returned if the device is successfully destructed.
953 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
954 * device isn't valid (e.g. it's already been destoryed, the handle isn't
955 * valid, etc.).
956 */
957static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
958{
Dan Williamsb8d82f62011-05-01 14:38:26 -0700959 struct sci_base_state_machine *sm = &sci_dev->state_machine;
960 enum scic_sds_remote_device_states state = sm->current_state_id;
961 struct scic_sds_controller *scic;
962
963 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
964 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
965 __func__, state);
966 return SCI_FAILURE_INVALID_STATE;
967 }
968
969 scic = sci_dev->owning_port->owning_controller;
970 scic_sds_controller_free_remote_node_context(scic, sci_dev,
971 sci_dev->rnc.remote_node_index);
972 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
973 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
974
975 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -0700976}
977
Dan Williams6f231dd2011-07-02 22:56:22 -0700978/**
979 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -0800980 * @ihost: This parameter specifies the isci host object.
981 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -0700982 *
983 */
Dan Williamsd9c37392011-03-03 17:59:32 -0800984static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700985{
Dan Williamsd9c37392011-03-03 17:59:32 -0800986 dev_dbg(&ihost->pdev->dev,
987 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700988
989 /* There should not be any outstanding io's. All paths to
990 * here should go through isci_remote_device_nuke_requests.
991 * If we hit this condition, we will need a way to complete
992 * io requests in process */
Dan Williamsd9c37392011-03-03 17:59:32 -0800993 while (!list_empty(&idev->reqs_in_process)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700994
Dan Williamsd9c37392011-03-03 17:59:32 -0800995 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700996 "%s: ** request list not empty! **\n", __func__);
997 BUG();
998 }
999
Dan Williams57f20f42011-04-21 18:14:45 -07001000 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -08001001 idev->domain_dev->lldd_dev = NULL;
1002 idev->domain_dev = NULL;
1003 idev->isci_port = NULL;
1004 list_del_init(&idev->node);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001005
Dan Williamsd9c37392011-03-03 17:59:32 -08001006 clear_bit(IDEV_START_PENDING, &idev->flags);
1007 clear_bit(IDEV_STOP_PENDING, &idev->flags);
1008 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001009}
1010
Dan Williams88f3b622011-04-22 19:18:03 -07001011/**
1012 * isci_remote_device_stop_complete() - This function is called by the scic
1013 * when the remote device stop has completed. We mark the isci device as not
1014 * ready and remove the isci remote device.
1015 * @ihost: This parameter specifies the isci host object.
1016 * @idev: This parameter specifies the remote device.
1017 * @status: This parameter specifies status of the completion.
1018 *
1019 */
1020static void isci_remote_device_stop_complete(struct isci_host *ihost,
1021 struct isci_remote_device *idev)
1022{
1023 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1024
1025 isci_remote_device_change_state(idev, isci_stopped);
1026
1027 /* after stop, we can tear down resources. */
1028 isci_remote_device_deconstruct(ihost, idev);
1029}
1030
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001031static void scic_sds_remote_device_stopped_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001032{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001033 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001034 struct scic_sds_controller *scic;
1035 struct isci_remote_device *idev;
1036 struct isci_host *ihost;
1037 u32 prev_state;
1038
Dan Williams88f3b622011-04-22 19:18:03 -07001039 scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +00001040 ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001041 idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001042
1043 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1044 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1045
1046 /* If we are entering from the stopping state let the SCI User know that
1047 * the stop operation has completed.
1048 */
1049 prev_state = sci_dev->state_machine.previous_state_id;
1050 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1051 isci_remote_device_stop_complete(ihost, idev);
1052
1053 scic_sds_controller_remote_device_stopped(scic, sci_dev);
1054}
1055
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001056static void scic_sds_remote_device_starting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001057{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001058 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001059 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +00001060 struct isci_host *ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001061 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001062
1063 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1064 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1065
1066 isci_remote_device_not_ready(ihost, idev,
1067 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1068}
1069
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001070static void scic_sds_remote_device_ready_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001071{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001072 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001073 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1074 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001075
1076 SET_STATE_HANDLER(sci_dev,
1077 scic_sds_remote_device_state_handler_table,
1078 SCI_BASE_REMOTE_DEVICE_STATE_READY);
1079
1080 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1081
Dan Williamsab2e8f72011-04-27 16:32:45 -07001082 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
1083 sci_base_state_machine_change_state(&sci_dev->state_machine,
1084 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1085 } else if (dev_is_expander(dev)) {
1086 sci_base_state_machine_change_state(&sci_dev->state_machine,
1087 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1088 } else
1089 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
Dan Williams88f3b622011-04-22 19:18:03 -07001090}
1091
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001092static void scic_sds_remote_device_ready_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001093{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001094 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001095 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1096
1097 if (dev->dev_type == SAS_END_DEV) {
1098 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001099 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001100
Dan Williamsab2e8f72011-04-27 16:32:45 -07001101 isci_remote_device_not_ready(scic->ihost, idev,
Dan Williams88f3b622011-04-22 19:18:03 -07001102 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1103 }
1104}
1105
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001106static void scic_sds_remote_device_stopping_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001107{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001108 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001109
1110 SET_STATE_HANDLER(
1111 sci_dev,
1112 scic_sds_remote_device_state_handler_table,
1113 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1114 );
1115}
1116
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001117static void scic_sds_remote_device_failed_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001118{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001119 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001120
1121 SET_STATE_HANDLER(
1122 sci_dev,
1123 scic_sds_remote_device_state_handler_table,
1124 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1125 );
1126}
1127
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001128static void scic_sds_remote_device_resetting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001129{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001130 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001131
1132 SET_STATE_HANDLER(
1133 sci_dev,
1134 scic_sds_remote_device_state_handler_table,
1135 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1136 );
1137
1138 scic_sds_remote_node_context_suspend(
1139 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1140}
1141
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001142static void scic_sds_remote_device_resetting_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001143{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001144 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001145
1146 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1147}
1148
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001149static void scic_sds_remote_device_final_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001150{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001151 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001152
1153 SET_STATE_HANDLER(
1154 sci_dev,
1155 scic_sds_remote_device_state_handler_table,
1156 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1157 );
1158}
1159
Dan Williamsab2e8f72011-04-27 16:32:45 -07001160static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
1161{
1162 struct scic_sds_remote_device *sci_dev = object;
1163
1164 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1165 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1166
1167 sci_dev->working_request = NULL;
1168 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
1169 /*
1170 * Since the RNC is ready, it's alright to finish completion
1171 * processing (e.g. signal the remote device is ready). */
1172 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
1173 } else {
1174 scic_sds_remote_node_context_resume(&sci_dev->rnc,
1175 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
1176 sci_dev);
1177 }
1178}
1179
1180static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1181{
1182 struct scic_sds_remote_device *sci_dev = object;
1183 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1184
1185 BUG_ON(sci_dev->working_request == NULL);
1186
1187 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1188 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1189
1190 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1191 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1192}
1193
1194static void scic_sds_stp_remote_device_ready_ncq_substate_enter(void *object)
1195{
1196 struct scic_sds_remote_device *sci_dev = object;
1197
1198 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1199 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ);
1200}
1201
1202static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1203{
1204 struct scic_sds_remote_device *sci_dev = object;
1205 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1206 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1207
1208 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1209 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1210
1211 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1212 isci_remote_device_not_ready(scic->ihost, idev,
1213 sci_dev->not_ready_reason);
1214}
1215
1216static void scic_sds_stp_remote_device_ready_await_reset_substate_enter(void *object)
1217{
1218 struct scic_sds_remote_device *sci_dev = object;
1219
1220 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1221 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
1222}
1223
1224static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1225{
1226 struct scic_sds_remote_device *sci_dev = object;
1227 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1228
1229 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1230 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1231
1232 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1233}
1234
1235static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1236{
1237 struct scic_sds_remote_device *sci_dev = object;
1238 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1239
1240 BUG_ON(sci_dev->working_request == NULL);
1241
1242 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1243 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1244
1245 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1246 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1247}
1248
1249static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1250{
1251 struct scic_sds_remote_device *sci_dev = object;
1252
1253 sci_dev->working_request = NULL;
1254}
Dan Williams88f3b622011-04-22 19:18:03 -07001255
1256static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1257 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1258 .enter_state = scic_sds_remote_device_initial_state_enter,
1259 },
1260 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1261 .enter_state = scic_sds_remote_device_stopped_state_enter,
1262 },
1263 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1264 .enter_state = scic_sds_remote_device_starting_state_enter,
1265 },
1266 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1267 .enter_state = scic_sds_remote_device_ready_state_enter,
1268 .exit_state = scic_sds_remote_device_ready_state_exit
1269 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001270 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1271 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1272 },
1273 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1274 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1275 },
1276 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
1277 .enter_state = scic_sds_stp_remote_device_ready_ncq_substate_enter,
1278 },
1279 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1280 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1281 },
1282 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
1283 .enter_state = scic_sds_stp_remote_device_ready_await_reset_substate_enter,
1284 },
1285 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1286 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1287 },
1288 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1289 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1290 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1291 },
Dan Williams88f3b622011-04-22 19:18:03 -07001292 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1293 .enter_state = scic_sds_remote_device_stopping_state_enter,
1294 },
1295 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1296 .enter_state = scic_sds_remote_device_failed_state_enter,
1297 },
1298 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1299 .enter_state = scic_sds_remote_device_resetting_state_enter,
1300 .exit_state = scic_sds_remote_device_resetting_state_exit
1301 },
1302 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1303 .enter_state = scic_sds_remote_device_final_state_enter,
1304 },
1305};
1306
1307/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001308 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001309 * @sci_port: SAS/SATA port through which this device is accessed.
1310 * @sci_dev: remote device to construct
1311 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001312 * This routine just performs benign initialization and does not
1313 * allocate the remote_node_context which is left to
1314 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1315 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001316 */
1317static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1318 struct scic_sds_remote_device *sci_dev)
1319{
1320 sci_dev->owning_port = sci_port;
1321 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001322
1323 sci_base_state_machine_construct(
1324 &sci_dev->state_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001325 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001326 scic_sds_remote_device_state_table,
1327 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1328 );
1329
1330 sci_base_state_machine_start(
1331 &sci_dev->state_machine
1332 );
1333
1334 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1335 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001336}
1337
1338/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001339 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001340 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001341 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1342 * the device is known to the SCI Core since it is contained in the
1343 * scic_phy object. Remote node context(s) is/are a global resource
1344 * allocated by this routine, freed by scic_remote_device_destruct().
1345 *
1346 * Returns:
1347 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1348 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1349 * sata-only controller instance.
1350 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001351 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001352static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1353 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001354{
1355 enum sci_status status;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001356 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001357
Dan Williamsb87ee302011-04-25 11:48:29 -07001358 scic_remote_device_construct(sci_port, sci_dev);
1359
Dan Williams88f3b622011-04-22 19:18:03 -07001360 /*
1361 * This information is request to determine how many remote node context
1362 * entries will be needed to store the remote node.
1363 */
Dan Williams88f3b622011-04-22 19:18:03 -07001364 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001365 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1366 sci_dev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001367 &sci_dev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001368
Dan Williamsa1a113b2011-04-21 18:44:45 -07001369 if (status != SCI_SUCCESS)
1370 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001371
Dan Williamsab2e8f72011-04-27 16:32:45 -07001372 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1373 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1374 /* pass */;
1375 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001376 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001377
Dan Williamsa1a113b2011-04-21 18:44:45 -07001378 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001379
Dan Williamsa1a113b2011-04-21 18:44:45 -07001380 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1381 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001382
Dan Williamsa1a113b2011-04-21 18:44:45 -07001383 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001384}
1385
Dan Williams88f3b622011-04-22 19:18:03 -07001386/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001387 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001388 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001389 * Remote node context(s) is/are a global resource allocated by this
1390 * routine, freed by scic_remote_device_destruct().
1391 *
1392 * Returns:
1393 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1394 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1395 * sata-only controller instance.
1396 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001397 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001398static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001399 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001400{
Dan Williamsa1a113b2011-04-21 18:44:45 -07001401 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001402 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001403
Dan Williamsb87ee302011-04-25 11:48:29 -07001404 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001405
Dan Williamsab2e8f72011-04-27 16:32:45 -07001406 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1407 sci_dev,
1408 &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001409 if (status != SCI_SUCCESS)
1410 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001411
Dan Williamsab2e8f72011-04-27 16:32:45 -07001412 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1413 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1414 /* pass */;
1415 else
1416 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001417
Dan Williamsa1a113b2011-04-21 18:44:45 -07001418 /*
1419 * For SAS-2 the physical link rate is actually a logical link
1420 * rate that incorporates multiplexing. The SCU doesn't
1421 * incorporate multiplexing and for the purposes of the
1422 * connection the logical link rate is that same as the
1423 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1424 * one another, so this code works for both situations. */
1425 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001426 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001427
Dan Williamsa1a113b2011-04-21 18:44:45 -07001428 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1429 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001430
Dan Williamsab2e8f72011-04-27 16:32:45 -07001431 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001432}
1433
1434/**
1435 * scic_remote_device_start() - This method will start the supplied remote
1436 * device. This method enables normal IO requests to flow through to the
1437 * remote device.
1438 * @remote_device: This parameter specifies the device to be started.
1439 * @timeout: This parameter specifies the number of milliseconds in which the
1440 * start operation should complete.
1441 *
1442 * An indication of whether the device was successfully started. SCI_SUCCESS
1443 * This value is returned if the device was successfully started.
1444 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1445 * the device when there have been no phys added to it.
1446 */
1447static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
Dan Williamseb229672011-05-01 14:05:57 -07001448 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001449{
Dan Williamseb229672011-05-01 14:05:57 -07001450 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1451 enum scic_sds_remote_device_states state = sm->current_state_id;
1452 enum sci_status status;
1453
1454 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1455 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1456 __func__, state);
1457 return SCI_FAILURE_INVALID_STATE;
1458 }
1459
1460 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1461 remote_device_resume_done,
1462 sci_dev);
1463 if (status != SCI_SUCCESS)
1464 return status;
1465
1466 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1467
1468 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001469}
Dan Williams6f231dd2011-07-02 22:56:22 -07001470
Dan Williams00d680e2011-04-25 14:29:29 -07001471static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1472 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001473{
Dan Williams00d680e2011-04-25 14:29:29 -07001474 struct scic_sds_port *sci_port = iport->sci_port_handle;
1475 struct isci_host *ihost = iport->isci_host;
1476 struct domain_device *dev = idev->domain_dev;
1477 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001478
Dan Williams00d680e2011-04-25 14:29:29 -07001479 if (dev->parent && dev_is_expander(dev->parent))
1480 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1481 else
1482 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001483
1484 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001485 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1486 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001487
1488 return status;
1489 }
1490
Dan Williams6f231dd2011-07-02 22:56:22 -07001491 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001492 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001493
Dan Williams00d680e2011-04-25 14:29:29 -07001494 if (status != SCI_SUCCESS)
1495 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1496 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001497
1498 return status;
1499}
1500
Dan Williams4393aa42011-03-31 13:10:44 -07001501void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001502{
1503 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001504
Dan Williams4393aa42011-03-31 13:10:44 -07001505 dev_dbg(&ihost->pdev->dev,
1506 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001507
1508 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -07001509 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -07001510
Dan Williams4393aa42011-03-31 13:10:44 -07001511 dev_dbg(&ihost->pdev->dev,
1512 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001513}
1514
Dan Williams6f231dd2011-07-02 22:56:22 -07001515/**
1516 * This function builds the isci_remote_device when a libsas dev_found message
1517 * is received.
1518 * @isci_host: This parameter specifies the isci host object.
1519 * @port: This parameter specifies the isci_port conected to this device.
1520 *
1521 * pointer to new isci_remote_device.
1522 */
1523static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001524isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001525{
Dan Williamsd9c37392011-03-03 17:59:32 -08001526 struct isci_remote_device *idev;
1527 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001528
Dan Williamsd9c37392011-03-03 17:59:32 -08001529 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001530 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001531 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1532 break;
1533 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001534
Dan Williamsd9c37392011-03-03 17:59:32 -08001535 if (i >= SCI_MAX_REMOTE_DEVICES) {
1536 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001537 return NULL;
1538 }
1539
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001540 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1541 return NULL;
1542
1543 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1544 return NULL;
1545
Dan Williamsd9c37392011-03-03 17:59:32 -08001546 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001547
Dan Williamsd9c37392011-03-03 17:59:32 -08001548 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001549}
Dan Williams6f231dd2011-07-02 22:56:22 -07001550
1551/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001552 * isci_remote_device_stop() - This function is called internally to stop the
1553 * remote device.
1554 * @isci_host: This parameter specifies the isci host object.
1555 * @isci_device: This parameter specifies the remote device.
1556 *
1557 * The status of the scic request to stop.
1558 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001559enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001560{
1561 enum sci_status status;
1562 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001563
Dan Williams6ad31fe2011-03-04 12:10:29 -08001564 dev_dbg(&ihost->pdev->dev,
1565 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001566
Dan Williams6ad31fe2011-03-04 12:10:29 -08001567 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001568
1569 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001570 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001571
Dan Williams6ad31fe2011-03-04 12:10:29 -08001572 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001573
Dan Williams6ad31fe2011-03-04 12:10:29 -08001574 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07001575 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001576 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001577
1578 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -08001579 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -08001580 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08001581 clear_bit(IDEV_ALLOCATED, &idev->flags);
1582 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001583
Dan Williams6ad31fe2011-03-04 12:10:29 -08001584 dev_dbg(&ihost->pdev->dev,
1585 "%s: idev = %p - after completion wait\n",
1586 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001587
Dan Williams6f231dd2011-07-02 22:56:22 -07001588 return status;
1589}
1590
1591/**
1592 * isci_remote_device_gone() - This function is called by libsas when a domain
1593 * device is removed.
1594 * @domain_device: This parameter specifies the libsas domain device.
1595 *
1596 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001597void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001598{
Dan Williams4393aa42011-03-31 13:10:44 -07001599 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001600 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001601
Dan Williams6ad31fe2011-03-04 12:10:29 -08001602 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001603 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001604 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001605
Dan Williams6ad31fe2011-03-04 12:10:29 -08001606 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001607}
1608
1609
1610/**
1611 * isci_remote_device_found() - This function is called by libsas when a remote
1612 * device is discovered. A remote device object is created and started. the
1613 * function then sleeps until the sci core device started message is
1614 * received.
1615 * @domain_device: This parameter specifies the libsas domain device.
1616 *
1617 * status, zero indicates success.
1618 */
1619int isci_remote_device_found(struct domain_device *domain_dev)
1620{
Dan Williams4393aa42011-03-31 13:10:44 -07001621 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001622 struct isci_port *isci_port;
1623 struct isci_phy *isci_phy;
1624 struct asd_sas_port *sas_port;
1625 struct asd_sas_phy *sas_phy;
1626 struct isci_remote_device *isci_device;
1627 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001628
Dan Williams6f231dd2011-07-02 22:56:22 -07001629 dev_dbg(&isci_host->pdev->dev,
1630 "%s: domain_device = %p\n", __func__, domain_dev);
1631
Dan Williams0cf89d12011-02-18 09:25:07 -08001632 wait_for_start(isci_host);
1633
Dan Williams6f231dd2011-07-02 22:56:22 -07001634 sas_port = domain_dev->port;
1635 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1636 port_phy_el);
1637 isci_phy = to_isci_phy(sas_phy);
1638 isci_port = isci_phy->isci_port;
1639
1640 /* we are being called for a device on this port,
1641 * so it has to come up eventually
1642 */
1643 wait_for_completion(&isci_port->start_complete);
1644
1645 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1646 (isci_stopped == isci_port_get_state(isci_port)))
1647 return -ENODEV;
1648
1649 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001650 if (!isci_device)
1651 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001652
1653 INIT_LIST_HEAD(&isci_device->node);
1654 domain_dev->lldd_dev = isci_device;
1655 isci_device->domain_dev = domain_dev;
1656 isci_device->isci_port = isci_port;
1657 isci_remote_device_change_state(isci_device, isci_starting);
1658
1659
Dan Williams1a380452011-03-03 18:01:43 -08001660 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001661 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1662
Dan Williams6ad31fe2011-03-04 12:10:29 -08001663 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001664 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -08001665 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001666
Dan Williams6f231dd2011-07-02 22:56:22 -07001667 dev_dbg(&isci_host->pdev->dev,
1668 "%s: isci_device = %p\n",
1669 __func__, isci_device);
1670
1671 if (status != SCI_SUCCESS) {
1672
Dan Williams1a380452011-03-03 18:01:43 -08001673 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001674 isci_remote_device_deconstruct(
1675 isci_host,
1676 isci_device
1677 );
Dan Williams1a380452011-03-03 18:01:43 -08001678 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001679 return -ENODEV;
1680 }
1681
Dan Williams6ad31fe2011-03-04 12:10:29 -08001682 /* wait for the device ready callback. */
1683 wait_for_device_start(isci_host, isci_device);
1684
Dan Williams6f231dd2011-07-02 22:56:22 -07001685 return 0;
1686}
1687/**
1688 * isci_device_is_reset_pending() - This function will check if there is any
1689 * pending reset condition on the device.
1690 * @request: This parameter is the isci_device object.
1691 *
1692 * true if there is a reset pending for the device.
1693 */
1694bool isci_device_is_reset_pending(
1695 struct isci_host *isci_host,
1696 struct isci_remote_device *isci_device)
1697{
1698 struct isci_request *isci_request;
1699 struct isci_request *tmp_req;
1700 bool reset_is_pending = false;
1701 unsigned long flags;
1702
1703 dev_dbg(&isci_host->pdev->dev,
1704 "%s: isci_device = %p\n", __func__, isci_device);
1705
1706 spin_lock_irqsave(&isci_host->scic_lock, flags);
1707
1708 /* Check for reset on all pending requests. */
1709 list_for_each_entry_safe(isci_request, tmp_req,
1710 &isci_device->reqs_in_process, dev_node) {
1711 dev_dbg(&isci_host->pdev->dev,
1712 "%s: isci_device = %p request = %p\n",
1713 __func__, isci_device, isci_request);
1714
1715 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001716 struct sas_task *task = isci_request_access_task(
1717 isci_request);
1718
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001719 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001720 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1721 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001722 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001723 }
1724 }
1725
1726 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1727
1728 dev_dbg(&isci_host->pdev->dev,
1729 "%s: isci_device = %p reset_is_pending = %d\n",
1730 __func__, isci_device, reset_is_pending);
1731
1732 return reset_is_pending;
1733}
1734
1735/**
1736 * isci_device_clear_reset_pending() - This function will clear if any pending
1737 * reset condition flags on the device.
1738 * @request: This parameter is the isci_device object.
1739 *
1740 * true if there is a reset pending for the device.
1741 */
Dan Williams4393aa42011-03-31 13:10:44 -07001742void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001743{
1744 struct isci_request *isci_request;
1745 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07001746 unsigned long flags = 0;
1747
Dan Williams4393aa42011-03-31 13:10:44 -07001748 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1749 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07001750
Dan Williams4393aa42011-03-31 13:10:44 -07001751 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001752
1753 /* Clear reset pending on all pending requests. */
1754 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07001755 &idev->reqs_in_process, dev_node) {
1756 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1757 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001758
1759 if (isci_request->ttype == io_task) {
1760
1761 unsigned long flags2;
1762 struct sas_task *task = isci_request_access_task(
1763 isci_request);
1764
1765 spin_lock_irqsave(&task->task_state_lock, flags2);
1766 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1767 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1768 }
1769 }
Dan Williams4393aa42011-03-31 13:10:44 -07001770 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001771}