blob: e766b27bdc194c0996353177d7456717ee4b459f [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"
Dave Jiange76d6182011-05-04 15:02:03 -070056#include "sas.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
Dan Williams01bec772011-05-01 16:51:11 -0700280enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
281 u32 frame_index)
Dan Williams88f3b622011-04-22 19:18:03 -0700282{
Dan Williams01bec772011-05-01 16:51:11 -0700283 struct sci_base_state_machine *sm = &sci_dev->state_machine;
284 enum scic_sds_remote_device_states state = sm->current_state_id;
285 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
286 enum sci_status status;
287
288 switch (state) {
289 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
290 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
291 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
292 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
293 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
294 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
295 default:
296 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
297 __func__, state);
298 /* Return the frame back to the controller */
299 scic_sds_controller_release_frame(scic, frame_index);
300 return SCI_FAILURE_INVALID_STATE;
301 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
302 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
303 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
304 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
305 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
306 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING: {
307 struct scic_sds_request *sci_req;
308 struct sci_ssp_frame_header *hdr;
309
310 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
311 frame_index,
312 (void **)&hdr);
313 if (status != SCI_SUCCESS)
314 return status;
315
316 sci_req = scic_sds_controller_get_io_request_from_tag(scic, hdr->tag);
317 if (sci_req && sci_req->target_device == sci_dev) {
318 /* The IO request is now in charge of releasing the frame */
319 status = sci_req->state_handlers->frame_handler(sci_req,
320 frame_index);
321 } else {
322 /* We could not map this tag to a valid IO
323 * request Just toss the frame and continue
324 */
325 scic_sds_controller_release_frame(scic, frame_index);
326 }
327 break;
328 }
329 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700330 struct dev_to_host_fis *hdr;
Dan Williams01bec772011-05-01 16:51:11 -0700331
332 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
333 frame_index,
334 (void **)&hdr);
335 if (status != SCI_SUCCESS)
336 return status;
337
Dave Jiange76d6182011-05-04 15:02:03 -0700338 if (hdr->fis_type == FIS_SETDEVBITS &&
339 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700340 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
341
342 /* TODO Check sactive and complete associated IO if any. */
343 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
Dave Jiange76d6182011-05-04 15:02:03 -0700344 } else if (hdr->fis_type == FIS_REGD2H &&
345 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700346 /*
347 * Some devices return D2H FIS when an NCQ error is detected.
348 * Treat this like an SDB error FIS ready reason.
349 */
350 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
351 sci_base_state_machine_change_state(&sci_dev->state_machine,
352 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
353 } else
354 status = SCI_FAILURE;
355
356 scic_sds_controller_release_frame(scic, frame_index);
357 break;
358 }
359 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
360 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
361 /* The device does not process any UF received from the hardware while
362 * in this state. All unsolicited frames are forwarded to the io request
363 * object.
364 */
365 status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
366 break;
367 }
368
369 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700370}
371
Dan Williamse6225712011-05-01 16:26:09 -0700372static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700373{
Dan Williamse6225712011-05-01 16:26:09 -0700374
375 struct sci_base_state_machine *sm = &sci_dev->state_machine;
376 enum scic_sds_remote_device_states state = sm->current_state_id;
377
378 switch (state) {
379 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
380 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
381 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
382 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
383 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
384 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
385 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
386 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
387 return true;
388 default:
389 return false;
390 }
391}
392
393enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
394 u32 event_code)
395{
396 struct sci_base_state_machine *sm = &sci_dev->state_machine;
397 enum scic_sds_remote_device_states state = sm->current_state_id;
398 enum sci_status status;
399
400 switch (scu_get_event_type(event_code)) {
401 case SCU_EVENT_TYPE_RNC_OPS_MISC:
402 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
403 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
404 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
405 break;
406 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
407 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
408 status = SCI_SUCCESS;
409
410 /* Suspend the associated RNC */
411 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
412 SCI_SOFTWARE_SUSPENSION,
413 NULL, NULL);
414
415 dev_dbg(scirdev_to_dev(sci_dev),
416 "%s: device: %p event code: %x: %s\n",
417 __func__, sci_dev, event_code,
418 is_remote_device_ready(sci_dev)
419 ? "I_T_Nexus_Timeout event"
420 : "I_T_Nexus_Timeout event in wrong state");
421
422 break;
423 }
424 /* Else, fall through and treat as unhandled... */
425 default:
426 dev_dbg(scirdev_to_dev(sci_dev),
427 "%s: device: %p event code: %x: %s\n",
428 __func__, sci_dev, event_code,
429 is_remote_device_ready(sci_dev)
430 ? "unexpected event"
431 : "unexpected event in wrong state");
432 status = SCI_FAILURE_INVALID_STATE;
433 break;
434 }
435
436 if (status != SCI_SUCCESS)
437 return status;
438
439 if (state == SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE) {
440
441 /* We pick up suspension events to handle specifically to this
442 * state. We resume the RNC right away.
443 */
444 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
445 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
446 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
447 }
448
449 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700450}
451
Dan Williams18606552011-05-01 14:57:11 -0700452static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
453 struct scic_sds_request *sci_req,
454 enum sci_status status)
Dan Williams88f3b622011-04-22 19:18:03 -0700455{
Dan Williams18606552011-05-01 14:57:11 -0700456 struct scic_sds_port *sci_port = sci_dev->owning_port;
457
458 /* cleanup requests that failed after starting on the port */
459 if (status != SCI_SUCCESS)
460 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
461 else
462 scic_sds_remote_device_increment_request_count(sci_dev);
463}
464
465enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
466 struct scic_sds_remote_device *sci_dev,
467 struct scic_sds_request *sci_req)
468{
469 struct sci_base_state_machine *sm = &sci_dev->state_machine;
470 enum scic_sds_remote_device_states state = sm->current_state_id;
471 struct scic_sds_port *sci_port = sci_dev->owning_port;
Dave Jiange76d6182011-05-04 15:02:03 -0700472 struct isci_request *ireq = sci_req->ireq;
Dan Williams18606552011-05-01 14:57:11 -0700473 enum sci_status status;
474
475 switch (state) {
476 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
477 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
478 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
479 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
480 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
481 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
482 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
483 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
484 default:
485 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
486 __func__, state);
487 return SCI_FAILURE_INVALID_STATE;
488 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
489 /* attempt to start an io request for this device object. The remote
490 * device object will issue the start request for the io and if
491 * successful it will start the request for the port object then
492 * increment its own request count.
493 */
494 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
495 if (status != SCI_SUCCESS)
496 return status;
497
498 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
499 if (status != SCI_SUCCESS)
500 break;
501
502 status = scic_sds_request_start(sci_req);
503 break;
504 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
505 /* handle the start io operation for a sata device that is in
506 * the command idle state. - Evalute the type of IO request to
507 * be started - If its an NCQ request change to NCQ substate -
508 * If its any other command change to the CMD substate
509 *
510 * If this is a softreset we may want to have a different
511 * substate.
512 */
513 enum scic_sds_remote_device_states new_state;
Dave Jiange76d6182011-05-04 15:02:03 -0700514 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700515
516 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
517 if (status != SCI_SUCCESS)
518 return status;
519
520 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
521 if (status != SCI_SUCCESS)
522 break;
523
524 status = sci_req->state_handlers->start_handler(sci_req);
525 if (status != SCI_SUCCESS)
526 break;
527
Dave Jiange76d6182011-05-04 15:02:03 -0700528 if (task->ata_task.use_ncq)
Dan Williams18606552011-05-01 14:57:11 -0700529 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
530 else {
531 sci_dev->working_request = sci_req;
532 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
533 }
534 sci_base_state_machine_change_state(sm, new_state);
535 break;
536 }
Dave Jiange76d6182011-05-04 15:02:03 -0700537 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
538 struct sas_task *task = isci_request_access_task(ireq);
539
540 if (task->ata_task.use_ncq) {
Dan Williams18606552011-05-01 14:57:11 -0700541 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
542 if (status != SCI_SUCCESS)
543 return status;
544
545 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
546 if (status != SCI_SUCCESS)
547 break;
548
549 status = sci_req->state_handlers->start_handler(sci_req);
550 } else
551 return SCI_FAILURE_INVALID_STATE;
552 break;
Dave Jiange76d6182011-05-04 15:02:03 -0700553 }
Dan Williams18606552011-05-01 14:57:11 -0700554 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
555 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
556 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
557 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
558 if (status != SCI_SUCCESS)
559 return status;
560
561 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
562 if (status != SCI_SUCCESS)
563 break;
564
565 status = scic_sds_request_start(sci_req);
566 if (status != SCI_SUCCESS)
567 break;
568
569 sci_dev->working_request = sci_req;
570 sci_base_state_machine_change_state(&sci_dev->state_machine,
571 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
572 break;
573 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
574 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
575 /* device is already handling a command it can not accept new commands
576 * until this one is complete.
577 */
578 return SCI_FAILURE_INVALID_STATE;
579 }
580
581 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
582 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700583}
584
Dan Williams10a09e62011-05-01 15:33:43 -0700585static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
586 struct scic_sds_remote_device *sci_dev,
587 struct scic_sds_request *sci_req)
Dan Williams88f3b622011-04-22 19:18:03 -0700588{
Dan Williams10a09e62011-05-01 15:33:43 -0700589 enum sci_status status;
590
591 status = scic_sds_request_complete(sci_req);
592 if (status != SCI_SUCCESS)
593 return status;
594
595 status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
596 if (status != SCI_SUCCESS)
597 return status;
598
599 scic_sds_remote_device_decrement_request_count(sci_dev);
600 return status;
601}
602
603enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
604 struct scic_sds_remote_device *sci_dev,
605 struct scic_sds_request *sci_req)
606{
607 struct sci_base_state_machine *sm = &sci_dev->state_machine;
608 enum scic_sds_remote_device_states state = sm->current_state_id;
609 struct scic_sds_port *sci_port = sci_dev->owning_port;
610 enum sci_status status;
611
612 switch (state) {
613 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
614 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
615 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
616 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
617 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
618 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
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 SCI_BASE_REMOTE_DEVICE_STATE_READY:
625 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
626 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
627 status = common_complete_io(sci_port, sci_dev, sci_req);
628 break;
629 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
630 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
631 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
632 status = common_complete_io(sci_port, sci_dev, sci_req);
633 if (status != SCI_SUCCESS)
634 break;
635
636 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
637 /* This request causes hardware error, device needs to be Lun Reset.
638 * So here we force the state machine to IDLE state so the rest IOs
639 * can reach RNC state handler, these IOs will be completed by RNC with
640 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
641 */
642 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
643 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
644 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
645 break;
646 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
647 status = common_complete_io(sci_port, sci_dev, sci_req);
648 if (status != SCI_SUCCESS)
649 break;
650 sci_base_state_machine_change_state(sm, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
651 break;
652 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
653 status = common_complete_io(sci_port, sci_dev, sci_req);
654 if (status != SCI_SUCCESS)
655 break;
656
657 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
658 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
659 rnc_destruct_done,
660 sci_dev);
661 break;
662 }
663
664 if (status != SCI_SUCCESS)
665 dev_err(scirdev_to_dev(sci_dev),
666 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
667 "could not complete\n", __func__, sci_port,
668 sci_dev, sci_req, status);
669
670 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700671}
672
Dan Williams84b9b022011-05-01 15:53:25 -0700673static void scic_sds_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700674{
Dan Williams84b9b022011-05-01 15:53:25 -0700675 struct scic_sds_remote_device *sci_dev = dev;
676
677 /* we need to check if this request is still valid to continue. */
678 if (sci_dev->working_request)
679 scic_controller_continue_io(sci_dev->working_request);
680}
681
682enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
683 struct scic_sds_remote_device *sci_dev,
684 struct scic_sds_request *sci_req)
685{
686 struct sci_base_state_machine *sm = &sci_dev->state_machine;
687 enum scic_sds_remote_device_states state = sm->current_state_id;
688 struct scic_sds_port *sci_port = sci_dev->owning_port;
689 enum sci_status status;
690
691 switch (state) {
692 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
693 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
694 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
695 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
696 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
697 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
698 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
699 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
700 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
701 default:
702 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
703 __func__, state);
704 return SCI_FAILURE_INVALID_STATE;
705 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
706 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
707 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
708 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
709 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
710 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
711 if (status != SCI_SUCCESS)
712 return status;
713
714 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
715 if (status != SCI_SUCCESS)
716 goto out;
717
718 status = sci_req->state_handlers->start_handler(sci_req);
719 if (status != SCI_SUCCESS)
720 goto out;
721
722 /* Note: If the remote device state is not IDLE this will
723 * replace the request that probably resulted in the task
724 * management request.
725 */
726 sci_dev->working_request = sci_req;
727 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
728
729 /* The remote node context must cleanup the TCi to NCQ mapping
730 * table. The only way to do this correctly is to either write
731 * to the TLCR register or to invalidate and repost the RNC. In
732 * either case the remote node context state machine will take
733 * the correct action when the remote node context is suspended
734 * and later resumed.
735 */
736 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
737 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
738 scic_sds_remote_node_context_resume(&sci_dev->rnc,
739 scic_sds_remote_device_continue_request,
740 sci_dev);
741
742 out:
743 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
744 /* We need to let the controller start request handler know that
745 * it can't post TC yet. We will provide a callback function to
746 * post TC when RNC gets resumed.
747 */
748 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
749 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
750 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
751 if (status != SCI_SUCCESS)
752 return status;
753
754 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
755 if (status != SCI_SUCCESS)
756 break;
757
758 status = scic_sds_request_start(sci_req);
759 break;
760 }
761 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
762
763 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700764}
765
766/**
767 *
Dan Williams88f3b622011-04-22 19:18:03 -0700768 * @sci_dev:
769 * @request:
770 *
771 * This method takes the request and bulids an appropriate SCU context for the
772 * request and then requests the controller to post the request. none
773 */
774void scic_sds_remote_device_post_request(
775 struct scic_sds_remote_device *sci_dev,
776 u32 request)
777{
778 u32 context;
779
780 context = scic_sds_remote_device_build_command_context(sci_dev, request);
781
782 scic_sds_controller_post_request(
783 scic_sds_remote_device_get_controller(sci_dev),
784 context
785 );
786}
787
Dan Williamsab2e8f72011-04-27 16:32:45 -0700788/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700789 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700790 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700791 */
Dan Williamseb229672011-05-01 14:05:57 -0700792static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700793{
Dan Williamsab2e8f72011-04-27 16:32:45 -0700794 struct scic_sds_remote_device *sci_dev = _dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700795
Dan Williamse6225712011-05-01 16:26:09 -0700796 if (is_remote_device_ready(sci_dev))
797 return;
Dan Williams88f3b622011-04-22 19:18:03 -0700798
Dan Williamse6225712011-05-01 16:26:09 -0700799 /* go 'ready' if we are not already in a ready state */
800 sci_base_state_machine_change_state(&sci_dev->state_machine,
801 SCI_BASE_REMOTE_DEVICE_STATE_READY);
Dan Williams88f3b622011-04-22 19:18:03 -0700802}
803
Dan Williamsab2e8f72011-04-27 16:32:45 -0700804static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
805{
806 struct scic_sds_remote_device *sci_dev = _dev;
807 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
808 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
809
810 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
811 * As a result, avoid sending the ready notification.
812 */
813 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
814 isci_remote_device_ready(scic->ihost, idev);
815}
816
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000817static void scic_sds_remote_device_initial_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700818{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000819 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700820
Dan Williams88f3b622011-04-22 19:18:03 -0700821 /* Initial state is a transitional state to the stopped state */
822 sci_base_state_machine_change_state(&sci_dev->state_machine,
823 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
824}
825
826/**
Dan Williams88f3b622011-04-22 19:18:03 -0700827 * scic_remote_device_destruct() - free remote node context and destruct
828 * @remote_device: This parameter specifies the remote device to be destructed.
829 *
830 * Remote device objects are a limited resource. As such, they must be
831 * protected. Thus calls to construct and destruct are mutually exclusive and
832 * non-reentrant. The return value shall indicate if the device was
833 * successfully destructed or if some failure occurred. enum sci_status This value
834 * is returned if the device is successfully destructed.
835 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
836 * device isn't valid (e.g. it's already been destoryed, the handle isn't
837 * valid, etc.).
838 */
839static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
840{
Dan Williamsb8d82f62011-05-01 14:38:26 -0700841 struct sci_base_state_machine *sm = &sci_dev->state_machine;
842 enum scic_sds_remote_device_states state = sm->current_state_id;
843 struct scic_sds_controller *scic;
844
845 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
846 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
847 __func__, state);
848 return SCI_FAILURE_INVALID_STATE;
849 }
850
851 scic = sci_dev->owning_port->owning_controller;
852 scic_sds_controller_free_remote_node_context(scic, sci_dev,
853 sci_dev->rnc.remote_node_index);
854 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
855 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
856
857 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -0700858}
859
Dan Williams6f231dd2011-07-02 22:56:22 -0700860/**
861 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -0800862 * @ihost: This parameter specifies the isci host object.
863 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -0700864 *
865 */
Dan Williamsd9c37392011-03-03 17:59:32 -0800866static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700867{
Dan Williamsd9c37392011-03-03 17:59:32 -0800868 dev_dbg(&ihost->pdev->dev,
869 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700870
871 /* There should not be any outstanding io's. All paths to
872 * here should go through isci_remote_device_nuke_requests.
873 * If we hit this condition, we will need a way to complete
874 * io requests in process */
Dan Williamsd9c37392011-03-03 17:59:32 -0800875 while (!list_empty(&idev->reqs_in_process)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700876
Dan Williamsd9c37392011-03-03 17:59:32 -0800877 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700878 "%s: ** request list not empty! **\n", __func__);
879 BUG();
880 }
881
Dan Williams57f20f42011-04-21 18:14:45 -0700882 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -0800883 idev->domain_dev->lldd_dev = NULL;
884 idev->domain_dev = NULL;
885 idev->isci_port = NULL;
886 list_del_init(&idev->node);
Dan Williams6ad31fe2011-03-04 12:10:29 -0800887
Dan Williamsd9c37392011-03-03 17:59:32 -0800888 clear_bit(IDEV_START_PENDING, &idev->flags);
889 clear_bit(IDEV_STOP_PENDING, &idev->flags);
890 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700891}
892
Dan Williams88f3b622011-04-22 19:18:03 -0700893/**
894 * isci_remote_device_stop_complete() - This function is called by the scic
895 * when the remote device stop has completed. We mark the isci device as not
896 * ready and remove the isci remote device.
897 * @ihost: This parameter specifies the isci host object.
898 * @idev: This parameter specifies the remote device.
899 * @status: This parameter specifies status of the completion.
900 *
901 */
902static void isci_remote_device_stop_complete(struct isci_host *ihost,
903 struct isci_remote_device *idev)
904{
905 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
906
907 isci_remote_device_change_state(idev, isci_stopped);
908
909 /* after stop, we can tear down resources. */
910 isci_remote_device_deconstruct(ihost, idev);
911}
912
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000913static void scic_sds_remote_device_stopped_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700914{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000915 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700916 struct scic_sds_controller *scic;
917 struct isci_remote_device *idev;
918 struct isci_host *ihost;
919 u32 prev_state;
920
Dan Williams88f3b622011-04-22 19:18:03 -0700921 scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +0000922 ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000923 idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700924
Dan Williams88f3b622011-04-22 19:18:03 -0700925 /* If we are entering from the stopping state let the SCI User know that
926 * the stop operation has completed.
927 */
928 prev_state = sci_dev->state_machine.previous_state_id;
929 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
930 isci_remote_device_stop_complete(ihost, idev);
931
932 scic_sds_controller_remote_device_stopped(scic, sci_dev);
933}
934
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000935static void scic_sds_remote_device_starting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700936{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000937 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700938 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +0000939 struct isci_host *ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000940 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700941
Dan Williams88f3b622011-04-22 19:18:03 -0700942 isci_remote_device_not_ready(ihost, idev,
943 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
944}
945
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000946static void scic_sds_remote_device_ready_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700947{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000948 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700949 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
950 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700951
Dan Williams88f3b622011-04-22 19:18:03 -0700952 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
953
Dan Williamsab2e8f72011-04-27 16:32:45 -0700954 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
955 sci_base_state_machine_change_state(&sci_dev->state_machine,
956 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
957 } else if (dev_is_expander(dev)) {
958 sci_base_state_machine_change_state(&sci_dev->state_machine,
959 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
960 } else
961 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
Dan Williams88f3b622011-04-22 19:18:03 -0700962}
963
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000964static void scic_sds_remote_device_ready_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700965{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000966 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700967 struct domain_device *dev = sci_dev_to_domain(sci_dev);
968
969 if (dev->dev_type == SAS_END_DEV) {
970 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000971 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700972
Dan Williamsab2e8f72011-04-27 16:32:45 -0700973 isci_remote_device_not_ready(scic->ihost, idev,
Dan Williams88f3b622011-04-22 19:18:03 -0700974 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
975 }
976}
977
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000978static void scic_sds_remote_device_resetting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700979{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000980 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700981
Dan Williams88f3b622011-04-22 19:18:03 -0700982 scic_sds_remote_node_context_suspend(
983 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
984}
985
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000986static void scic_sds_remote_device_resetting_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700987{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000988 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700989
990 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
991}
992
Dan Williamsab2e8f72011-04-27 16:32:45 -0700993static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
994{
995 struct scic_sds_remote_device *sci_dev = object;
996
Dan Williamsab2e8f72011-04-27 16:32:45 -0700997 sci_dev->working_request = NULL;
998 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
999 /*
1000 * Since the RNC is ready, it's alright to finish completion
1001 * processing (e.g. signal the remote device is ready). */
1002 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
1003 } else {
1004 scic_sds_remote_node_context_resume(&sci_dev->rnc,
1005 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
1006 sci_dev);
1007 }
1008}
1009
1010static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1011{
1012 struct scic_sds_remote_device *sci_dev = object;
1013 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1014
1015 BUG_ON(sci_dev->working_request == NULL);
1016
Dan Williamsab2e8f72011-04-27 16:32:45 -07001017 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1018 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1019}
1020
Dan Williamsab2e8f72011-04-27 16:32:45 -07001021static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1022{
1023 struct scic_sds_remote_device *sci_dev = object;
1024 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1025 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1026
Dan Williamsab2e8f72011-04-27 16:32:45 -07001027 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1028 isci_remote_device_not_ready(scic->ihost, idev,
1029 sci_dev->not_ready_reason);
1030}
1031
Dan Williamsab2e8f72011-04-27 16:32:45 -07001032static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1033{
1034 struct scic_sds_remote_device *sci_dev = object;
1035 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1036
Dan Williamsab2e8f72011-04-27 16:32:45 -07001037 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1038}
1039
1040static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1041{
1042 struct scic_sds_remote_device *sci_dev = object;
1043 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1044
1045 BUG_ON(sci_dev->working_request == NULL);
1046
Dan Williamsab2e8f72011-04-27 16:32:45 -07001047 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1048 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1049}
1050
1051static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1052{
1053 struct scic_sds_remote_device *sci_dev = object;
1054
1055 sci_dev->working_request = NULL;
1056}
Dan Williams88f3b622011-04-22 19:18:03 -07001057
1058static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1059 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1060 .enter_state = scic_sds_remote_device_initial_state_enter,
1061 },
1062 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1063 .enter_state = scic_sds_remote_device_stopped_state_enter,
1064 },
1065 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1066 .enter_state = scic_sds_remote_device_starting_state_enter,
1067 },
1068 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1069 .enter_state = scic_sds_remote_device_ready_state_enter,
1070 .exit_state = scic_sds_remote_device_ready_state_exit
1071 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001072 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1073 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1074 },
1075 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1076 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1077 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001078 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = { },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001079 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1080 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1081 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001082 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = { },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001083 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1084 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1085 },
1086 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1087 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1088 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1089 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001090 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = { },
1091 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001092 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1093 .enter_state = scic_sds_remote_device_resetting_state_enter,
1094 .exit_state = scic_sds_remote_device_resetting_state_exit
1095 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001096 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001097};
1098
1099/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001100 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001101 * @sci_port: SAS/SATA port through which this device is accessed.
1102 * @sci_dev: remote device to construct
1103 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001104 * This routine just performs benign initialization and does not
1105 * allocate the remote_node_context which is left to
1106 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1107 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001108 */
1109static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1110 struct scic_sds_remote_device *sci_dev)
1111{
1112 sci_dev->owning_port = sci_port;
1113 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001114
1115 sci_base_state_machine_construct(
1116 &sci_dev->state_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001117 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001118 scic_sds_remote_device_state_table,
1119 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1120 );
1121
1122 sci_base_state_machine_start(
1123 &sci_dev->state_machine
1124 );
1125
1126 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1127 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001128}
1129
1130/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001131 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001132 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001133 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1134 * the device is known to the SCI Core since it is contained in the
1135 * scic_phy object. Remote node context(s) is/are a global resource
1136 * allocated by this routine, freed by scic_remote_device_destruct().
1137 *
1138 * Returns:
1139 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1140 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1141 * sata-only controller instance.
1142 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001143 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001144static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1145 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001146{
1147 enum sci_status status;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001148 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001149
Dan Williamsb87ee302011-04-25 11:48:29 -07001150 scic_remote_device_construct(sci_port, sci_dev);
1151
Dan Williams88f3b622011-04-22 19:18:03 -07001152 /*
1153 * This information is request to determine how many remote node context
1154 * entries will be needed to store the remote node.
1155 */
Dan Williams88f3b622011-04-22 19:18:03 -07001156 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001157 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1158 sci_dev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001159 &sci_dev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001160
Dan Williamsa1a113b2011-04-21 18:44:45 -07001161 if (status != SCI_SUCCESS)
1162 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001163
Dan Williamsab2e8f72011-04-27 16:32:45 -07001164 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1165 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1166 /* pass */;
1167 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001168 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001169
Dan Williamsa1a113b2011-04-21 18:44:45 -07001170 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001171
Dan Williamsa1a113b2011-04-21 18:44:45 -07001172 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1173 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001174
Dan Williamsa1a113b2011-04-21 18:44:45 -07001175 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001176}
1177
Dan Williams88f3b622011-04-22 19:18:03 -07001178/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001179 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001180 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001181 * Remote node context(s) is/are a global resource allocated by this
1182 * routine, freed by scic_remote_device_destruct().
1183 *
1184 * Returns:
1185 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1186 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1187 * sata-only controller instance.
1188 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001189 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001190static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001191 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001192{
Dan Williamsa1a113b2011-04-21 18:44:45 -07001193 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001194 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001195
Dan Williamsb87ee302011-04-25 11:48:29 -07001196 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001197
Dan Williamsab2e8f72011-04-27 16:32:45 -07001198 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1199 sci_dev,
1200 &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001201 if (status != SCI_SUCCESS)
1202 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001203
Dan Williamsab2e8f72011-04-27 16:32:45 -07001204 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1205 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1206 /* pass */;
1207 else
1208 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001209
Dan Williamsa1a113b2011-04-21 18:44:45 -07001210 /*
1211 * For SAS-2 the physical link rate is actually a logical link
1212 * rate that incorporates multiplexing. The SCU doesn't
1213 * incorporate multiplexing and for the purposes of the
1214 * connection the logical link rate is that same as the
1215 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1216 * one another, so this code works for both situations. */
1217 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001218 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001219
Dan Williamsa1a113b2011-04-21 18:44:45 -07001220 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1221 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001222
Dan Williamsab2e8f72011-04-27 16:32:45 -07001223 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001224}
1225
1226/**
1227 * scic_remote_device_start() - This method will start the supplied remote
1228 * device. This method enables normal IO requests to flow through to the
1229 * remote device.
1230 * @remote_device: This parameter specifies the device to be started.
1231 * @timeout: This parameter specifies the number of milliseconds in which the
1232 * start operation should complete.
1233 *
1234 * An indication of whether the device was successfully started. SCI_SUCCESS
1235 * This value is returned if the device was successfully started.
1236 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1237 * the device when there have been no phys added to it.
1238 */
1239static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
Dan Williamseb229672011-05-01 14:05:57 -07001240 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001241{
Dan Williamseb229672011-05-01 14:05:57 -07001242 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1243 enum scic_sds_remote_device_states state = sm->current_state_id;
1244 enum sci_status status;
1245
1246 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1247 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1248 __func__, state);
1249 return SCI_FAILURE_INVALID_STATE;
1250 }
1251
1252 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1253 remote_device_resume_done,
1254 sci_dev);
1255 if (status != SCI_SUCCESS)
1256 return status;
1257
1258 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1259
1260 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001261}
Dan Williams6f231dd2011-07-02 22:56:22 -07001262
Dan Williams00d680e2011-04-25 14:29:29 -07001263static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1264 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001265{
Dan Williams00d680e2011-04-25 14:29:29 -07001266 struct scic_sds_port *sci_port = iport->sci_port_handle;
1267 struct isci_host *ihost = iport->isci_host;
1268 struct domain_device *dev = idev->domain_dev;
1269 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001270
Dan Williams00d680e2011-04-25 14:29:29 -07001271 if (dev->parent && dev_is_expander(dev->parent))
1272 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1273 else
1274 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001275
1276 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001277 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1278 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001279
1280 return status;
1281 }
1282
Dan Williams6f231dd2011-07-02 22:56:22 -07001283 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001284 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001285
Dan Williams00d680e2011-04-25 14:29:29 -07001286 if (status != SCI_SUCCESS)
1287 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1288 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001289
1290 return status;
1291}
1292
Dan Williams4393aa42011-03-31 13:10:44 -07001293void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001294{
1295 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001296
Dan Williams4393aa42011-03-31 13:10:44 -07001297 dev_dbg(&ihost->pdev->dev,
1298 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001299
1300 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -07001301 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -07001302
Dan Williams4393aa42011-03-31 13:10:44 -07001303 dev_dbg(&ihost->pdev->dev,
1304 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001305}
1306
Dan Williams6f231dd2011-07-02 22:56:22 -07001307/**
1308 * This function builds the isci_remote_device when a libsas dev_found message
1309 * is received.
1310 * @isci_host: This parameter specifies the isci host object.
1311 * @port: This parameter specifies the isci_port conected to this device.
1312 *
1313 * pointer to new isci_remote_device.
1314 */
1315static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001316isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001317{
Dan Williamsd9c37392011-03-03 17:59:32 -08001318 struct isci_remote_device *idev;
1319 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001320
Dan Williamsd9c37392011-03-03 17:59:32 -08001321 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001322 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001323 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1324 break;
1325 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001326
Dan Williamsd9c37392011-03-03 17:59:32 -08001327 if (i >= SCI_MAX_REMOTE_DEVICES) {
1328 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001329 return NULL;
1330 }
1331
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001332 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1333 return NULL;
1334
1335 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1336 return NULL;
1337
Dan Williamsd9c37392011-03-03 17:59:32 -08001338 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001339
Dan Williamsd9c37392011-03-03 17:59:32 -08001340 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001341}
Dan Williams6f231dd2011-07-02 22:56:22 -07001342
1343/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001344 * isci_remote_device_stop() - This function is called internally to stop the
1345 * remote device.
1346 * @isci_host: This parameter specifies the isci host object.
1347 * @isci_device: This parameter specifies the remote device.
1348 *
1349 * The status of the scic request to stop.
1350 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001351enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001352{
1353 enum sci_status status;
1354 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001355
Dan Williams6ad31fe2011-03-04 12:10:29 -08001356 dev_dbg(&ihost->pdev->dev,
1357 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001358
Dan Williams6ad31fe2011-03-04 12:10:29 -08001359 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001360
1361 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001362 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001363
Dan Williams6ad31fe2011-03-04 12:10:29 -08001364 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001365
Dan Williams6ad31fe2011-03-04 12:10:29 -08001366 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07001367 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001368 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001369
1370 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -08001371 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -08001372 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08001373 clear_bit(IDEV_ALLOCATED, &idev->flags);
1374 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001375
Dan Williams6ad31fe2011-03-04 12:10:29 -08001376 dev_dbg(&ihost->pdev->dev,
1377 "%s: idev = %p - after completion wait\n",
1378 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001379
Dan Williams6f231dd2011-07-02 22:56:22 -07001380 return status;
1381}
1382
1383/**
1384 * isci_remote_device_gone() - This function is called by libsas when a domain
1385 * device is removed.
1386 * @domain_device: This parameter specifies the libsas domain device.
1387 *
1388 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001389void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001390{
Dan Williams4393aa42011-03-31 13:10:44 -07001391 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001392 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001393
Dan Williams6ad31fe2011-03-04 12:10:29 -08001394 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001395 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001396 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001397
Dan Williams6ad31fe2011-03-04 12:10:29 -08001398 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001399}
1400
1401
1402/**
1403 * isci_remote_device_found() - This function is called by libsas when a remote
1404 * device is discovered. A remote device object is created and started. the
1405 * function then sleeps until the sci core device started message is
1406 * received.
1407 * @domain_device: This parameter specifies the libsas domain device.
1408 *
1409 * status, zero indicates success.
1410 */
1411int isci_remote_device_found(struct domain_device *domain_dev)
1412{
Dan Williams4393aa42011-03-31 13:10:44 -07001413 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001414 struct isci_port *isci_port;
1415 struct isci_phy *isci_phy;
1416 struct asd_sas_port *sas_port;
1417 struct asd_sas_phy *sas_phy;
1418 struct isci_remote_device *isci_device;
1419 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001420
Dan Williams6f231dd2011-07-02 22:56:22 -07001421 dev_dbg(&isci_host->pdev->dev,
1422 "%s: domain_device = %p\n", __func__, domain_dev);
1423
Dan Williams0cf89d12011-02-18 09:25:07 -08001424 wait_for_start(isci_host);
1425
Dan Williams6f231dd2011-07-02 22:56:22 -07001426 sas_port = domain_dev->port;
1427 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1428 port_phy_el);
1429 isci_phy = to_isci_phy(sas_phy);
1430 isci_port = isci_phy->isci_port;
1431
1432 /* we are being called for a device on this port,
1433 * so it has to come up eventually
1434 */
1435 wait_for_completion(&isci_port->start_complete);
1436
1437 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1438 (isci_stopped == isci_port_get_state(isci_port)))
1439 return -ENODEV;
1440
1441 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001442 if (!isci_device)
1443 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001444
1445 INIT_LIST_HEAD(&isci_device->node);
1446 domain_dev->lldd_dev = isci_device;
1447 isci_device->domain_dev = domain_dev;
1448 isci_device->isci_port = isci_port;
1449 isci_remote_device_change_state(isci_device, isci_starting);
1450
1451
Dan Williams1a380452011-03-03 18:01:43 -08001452 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001453 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1454
Dan Williams6ad31fe2011-03-04 12:10:29 -08001455 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001456 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -08001457 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001458
Dan Williams6f231dd2011-07-02 22:56:22 -07001459 dev_dbg(&isci_host->pdev->dev,
1460 "%s: isci_device = %p\n",
1461 __func__, isci_device);
1462
1463 if (status != SCI_SUCCESS) {
1464
Dan Williams1a380452011-03-03 18:01:43 -08001465 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001466 isci_remote_device_deconstruct(
1467 isci_host,
1468 isci_device
1469 );
Dan Williams1a380452011-03-03 18:01:43 -08001470 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001471 return -ENODEV;
1472 }
1473
Dan Williams6ad31fe2011-03-04 12:10:29 -08001474 /* wait for the device ready callback. */
1475 wait_for_device_start(isci_host, isci_device);
1476
Dan Williams6f231dd2011-07-02 22:56:22 -07001477 return 0;
1478}
1479/**
1480 * isci_device_is_reset_pending() - This function will check if there is any
1481 * pending reset condition on the device.
1482 * @request: This parameter is the isci_device object.
1483 *
1484 * true if there is a reset pending for the device.
1485 */
1486bool isci_device_is_reset_pending(
1487 struct isci_host *isci_host,
1488 struct isci_remote_device *isci_device)
1489{
1490 struct isci_request *isci_request;
1491 struct isci_request *tmp_req;
1492 bool reset_is_pending = false;
1493 unsigned long flags;
1494
1495 dev_dbg(&isci_host->pdev->dev,
1496 "%s: isci_device = %p\n", __func__, isci_device);
1497
1498 spin_lock_irqsave(&isci_host->scic_lock, flags);
1499
1500 /* Check for reset on all pending requests. */
1501 list_for_each_entry_safe(isci_request, tmp_req,
1502 &isci_device->reqs_in_process, dev_node) {
1503 dev_dbg(&isci_host->pdev->dev,
1504 "%s: isci_device = %p request = %p\n",
1505 __func__, isci_device, isci_request);
1506
1507 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001508 struct sas_task *task = isci_request_access_task(
1509 isci_request);
1510
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001511 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001512 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1513 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001514 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001515 }
1516 }
1517
1518 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1519
1520 dev_dbg(&isci_host->pdev->dev,
1521 "%s: isci_device = %p reset_is_pending = %d\n",
1522 __func__, isci_device, reset_is_pending);
1523
1524 return reset_is_pending;
1525}
1526
1527/**
1528 * isci_device_clear_reset_pending() - This function will clear if any pending
1529 * reset condition flags on the device.
1530 * @request: This parameter is the isci_device object.
1531 *
1532 * true if there is a reset pending for the device.
1533 */
Dan Williams4393aa42011-03-31 13:10:44 -07001534void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001535{
1536 struct isci_request *isci_request;
1537 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07001538 unsigned long flags = 0;
1539
Dan Williams4393aa42011-03-31 13:10:44 -07001540 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1541 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07001542
Dan Williams4393aa42011-03-31 13:10:44 -07001543 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001544
1545 /* Clear reset pending on all pending requests. */
1546 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07001547 &idev->reqs_in_process, dev_node) {
1548 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1549 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001550
1551 if (isci_request->ttype == io_task) {
1552
1553 unsigned long flags2;
1554 struct sas_task *task = isci_request_access_task(
1555 isci_request);
1556
1557 spin_lock_irqsave(&task->task_state_lock, flags2);
1558 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1559 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1560 }
1561 }
Dan Williams4393aa42011-03-31 13:10:44 -07001562 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001563}