blob: 15c1e6c3b223682b4f0fd015cc04c75120d7070f [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 */
Dave Jiang2d9c2242011-05-04 18:45:05 -070055#include <scsi/sas.h>
Dan Williams6f231dd2011-07-02 22:56:22 -070056#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "port.h"
58#include "remote_device.h"
59#include "request.h"
Dan Williams88f3b622011-04-22 19:18:03 -070060#include "scic_controller.h"
61#include "scic_io_request.h"
62#include "scic_phy.h"
63#include "scic_port.h"
64#include "scic_sds_controller.h"
65#include "scic_sds_phy.h"
66#include "scic_sds_port.h"
67#include "remote_node_context.h"
68#include "scic_sds_request.h"
69#include "sci_environment.h"
70#include "sci_util.h"
71#include "scu_event_codes.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070072#include "task.h"
73
Dan Williamsab2e8f72011-04-27 16:32:45 -070074/**
75 * isci_remote_device_change_state() - This function gets the status of the
76 * remote_device object.
77 * @isci_device: This parameter points to the isci_remote_device object
78 *
79 * status of the object as a isci_status enum.
80 */
81void isci_remote_device_change_state(
82 struct isci_remote_device *isci_device,
83 enum isci_status status)
84{
85 unsigned long flags;
86
87 spin_lock_irqsave(&isci_device->state_lock, flags);
88 isci_device->status = status;
89 spin_unlock_irqrestore(&isci_device->state_lock, flags);
90}
91
92/**
93 * isci_remote_device_not_ready() - This function is called by the scic when
94 * the remote device is not ready. We mark the isci device as ready (not
95 * "ready_for_io") and signal the waiting proccess.
96 * @isci_host: This parameter specifies the isci host object.
97 * @isci_device: This parameter specifies the remote device
98 *
99 */
100static void isci_remote_device_not_ready(struct isci_host *ihost,
101 struct isci_remote_device *idev, u32 reason)
102{
103 dev_dbg(&ihost->pdev->dev,
104 "%s: isci_device = %p\n", __func__, idev);
105
106 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
107 isci_remote_device_change_state(idev, isci_stopping);
108 else
109 /* device ready is actually a "not ready for io" state. */
110 isci_remote_device_change_state(idev, isci_ready);
111}
112
113/**
114 * isci_remote_device_ready() - This function is called by the scic when the
115 * remote device is ready. We mark the isci device as ready and signal the
116 * waiting proccess.
117 * @ihost: our valid isci_host
118 * @idev: remote device
119 *
120 */
121static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
122{
123 dev_dbg(&ihost->pdev->dev,
124 "%s: idev = %p\n", __func__, idev);
125
126 isci_remote_device_change_state(idev, isci_ready_for_io);
127 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
128 wake_up(&ihost->eventq);
129}
130
Dan Williamsec575662011-05-01 14:19:25 -0700131/* called once the remote node context is ready to be freed.
132 * The remote device can now report that its stop operation is complete. none
133 */
134static void rnc_destruct_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700135{
Dan Williamsec575662011-05-01 14:19:25 -0700136 struct scic_sds_remote_device *sci_dev = _dev;
137
138 BUG_ON(sci_dev->started_request_count != 0);
139 sci_base_state_machine_change_state(&sci_dev->state_machine,
140 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
141}
142
143static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
144{
145 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
146 u32 i, request_count = sci_dev->started_request_count;
147 enum sci_status status = SCI_SUCCESS;
148
149 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
150 struct scic_sds_request *sci_req;
151 enum sci_status s;
152
153 sci_req = scic->io_request_table[i];
154 if (!sci_req || sci_req->target_device != sci_dev)
155 continue;
156 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
157 if (s != SCI_SUCCESS)
158 status = s;
159 }
160
161 return status;
162}
163
164enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
165 u32 timeout)
166{
167 struct sci_base_state_machine *sm = &sci_dev->state_machine;
168 enum scic_sds_remote_device_states state = sm->current_state_id;
169
170 switch (state) {
171 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
172 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
173 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
174 default:
175 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
176 __func__, state);
177 return SCI_FAILURE_INVALID_STATE;
178 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
179 return SCI_SUCCESS;
180 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
181 /* device not started so there had better be no requests */
182 BUG_ON(sci_dev->started_request_count != 0);
183 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
184 rnc_destruct_done, sci_dev);
185 /* Transition to the stopping state and wait for the
186 * remote node to complete being posted and invalidated.
187 */
188 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
189 return SCI_SUCCESS;
190 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
191 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
192 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
193 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
194 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
195 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
196 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
197 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
198 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
199 if (sci_dev->started_request_count == 0) {
200 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
201 rnc_destruct_done, sci_dev);
202 return SCI_SUCCESS;
203 } else
204 return scic_sds_remote_device_terminate_requests(sci_dev);
205 break;
206 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
207 /* All requests should have been terminated, but if there is an
208 * attempt to stop a device already in the stopping state, then
209 * try again to terminate.
210 */
211 return scic_sds_remote_device_terminate_requests(sci_dev);
212 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
213 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
214 return SCI_SUCCESS;
215 }
Dan Williams88f3b622011-04-22 19:18:03 -0700216}
Dan Williams6f231dd2011-07-02 22:56:22 -0700217
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700218enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700219{
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700220 struct sci_base_state_machine *sm = &sci_dev->state_machine;
221 enum scic_sds_remote_device_states state = sm->current_state_id;
222
223 switch (state) {
224 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
225 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
226 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
227 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
228 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
229 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
230 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
231 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
232 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
233 default:
234 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
235 __func__, state);
236 return SCI_FAILURE_INVALID_STATE;
237 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
238 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
239 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
240 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
241 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
242 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
243 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
244 return SCI_SUCCESS;
245 }
Dan Williams88f3b622011-04-22 19:18:03 -0700246}
247
Dan Williams81515182011-05-01 14:53:00 -0700248enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700249{
Dan Williams81515182011-05-01 14:53:00 -0700250 struct sci_base_state_machine *sm = &sci_dev->state_machine;
251 enum scic_sds_remote_device_states state = sm->current_state_id;
Dan Williams88f3b622011-04-22 19:18:03 -0700252
Dan Williams81515182011-05-01 14:53:00 -0700253 if (state != SCI_BASE_REMOTE_DEVICE_STATE_RESETTING) {
254 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
255 __func__, state);
256 return SCI_FAILURE_INVALID_STATE;
257 }
258
259 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_READY);
260 return SCI_SUCCESS;
261}
Dan Williams88f3b622011-04-22 19:18:03 -0700262
Dan Williams323f0ec2011-05-01 16:15:47 -0700263enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
264 u32 suspend_type)
Dan Williams88f3b622011-04-22 19:18:03 -0700265{
Dan Williams323f0ec2011-05-01 16:15:47 -0700266 struct sci_base_state_machine *sm = &sci_dev->state_machine;
267 enum scic_sds_remote_device_states state = sm->current_state_id;
268
269 if (state != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD) {
270 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
271 __func__, state);
272 return SCI_FAILURE_INVALID_STATE;
273 }
274
275 return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
276 suspend_type, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700277}
278
Dan Williams01bec772011-05-01 16:51:11 -0700279enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
280 u32 frame_index)
Dan Williams88f3b622011-04-22 19:18:03 -0700281{
Dan Williams01bec772011-05-01 16:51:11 -0700282 struct sci_base_state_machine *sm = &sci_dev->state_machine;
283 enum scic_sds_remote_device_states state = sm->current_state_id;
284 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
285 enum sci_status status;
286
287 switch (state) {
288 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
289 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
290 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
291 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
292 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
293 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
294 default:
295 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
296 __func__, state);
297 /* Return the frame back to the controller */
298 scic_sds_controller_release_frame(scic, frame_index);
299 return SCI_FAILURE_INVALID_STATE;
300 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
301 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
302 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
303 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
304 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
305 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING: {
306 struct scic_sds_request *sci_req;
Dave Jiang2d9c2242011-05-04 18:45:05 -0700307 struct ssp_frame_hdr hdr;
308 void *frame_header;
309 ssize_t word_cnt;
Dan Williams01bec772011-05-01 16:51:11 -0700310
311 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
312 frame_index,
Dave Jiang2d9c2242011-05-04 18:45:05 -0700313 &frame_header);
Dan Williams01bec772011-05-01 16:51:11 -0700314 if (status != SCI_SUCCESS)
315 return status;
316
Dave Jiang2d9c2242011-05-04 18:45:05 -0700317 word_cnt = sizeof(hdr) / sizeof(u32);
318 sci_swab32_cpy(&hdr, frame_header, word_cnt);
319
320 sci_req = scic_request_by_tag(scic, be16_to_cpu(hdr.tag));
Dan Williams01bec772011-05-01 16:51:11 -0700321 if (sci_req && sci_req->target_device == sci_dev) {
322 /* The IO request is now in charge of releasing the frame */
323 status = sci_req->state_handlers->frame_handler(sci_req,
324 frame_index);
325 } else {
326 /* We could not map this tag to a valid IO
327 * request Just toss the frame and continue
328 */
329 scic_sds_controller_release_frame(scic, frame_index);
330 }
331 break;
332 }
333 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700334 struct dev_to_host_fis *hdr;
Dan Williams01bec772011-05-01 16:51:11 -0700335
336 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
337 frame_index,
338 (void **)&hdr);
339 if (status != SCI_SUCCESS)
340 return status;
341
Dave Jiange76d6182011-05-04 15:02:03 -0700342 if (hdr->fis_type == FIS_SETDEVBITS &&
343 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700344 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
345
346 /* TODO Check sactive and complete associated IO if any. */
347 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
Dave Jiange76d6182011-05-04 15:02:03 -0700348 } else if (hdr->fis_type == FIS_REGD2H &&
349 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700350 /*
351 * Some devices return D2H FIS when an NCQ error is detected.
352 * Treat this like an SDB error FIS ready reason.
353 */
354 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
355 sci_base_state_machine_change_state(&sci_dev->state_machine,
356 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
357 } else
358 status = SCI_FAILURE;
359
360 scic_sds_controller_release_frame(scic, frame_index);
361 break;
362 }
363 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
364 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
365 /* The device does not process any UF received from the hardware while
366 * in this state. All unsolicited frames are forwarded to the io request
367 * object.
368 */
369 status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
370 break;
371 }
372
373 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700374}
375
Dan Williamse6225712011-05-01 16:26:09 -0700376static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700377{
Dan Williamse6225712011-05-01 16:26:09 -0700378
379 struct sci_base_state_machine *sm = &sci_dev->state_machine;
380 enum scic_sds_remote_device_states state = sm->current_state_id;
381
382 switch (state) {
383 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
384 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
385 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
386 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
387 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
388 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
389 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
390 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
391 return true;
392 default:
393 return false;
394 }
395}
396
397enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
398 u32 event_code)
399{
400 struct sci_base_state_machine *sm = &sci_dev->state_machine;
401 enum scic_sds_remote_device_states state = sm->current_state_id;
402 enum sci_status status;
403
404 switch (scu_get_event_type(event_code)) {
405 case SCU_EVENT_TYPE_RNC_OPS_MISC:
406 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
407 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
408 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
409 break;
410 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
411 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
412 status = SCI_SUCCESS;
413
414 /* Suspend the associated RNC */
415 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
416 SCI_SOFTWARE_SUSPENSION,
417 NULL, NULL);
418
419 dev_dbg(scirdev_to_dev(sci_dev),
420 "%s: device: %p event code: %x: %s\n",
421 __func__, sci_dev, event_code,
422 is_remote_device_ready(sci_dev)
423 ? "I_T_Nexus_Timeout event"
424 : "I_T_Nexus_Timeout event in wrong state");
425
426 break;
427 }
428 /* Else, fall through and treat as unhandled... */
429 default:
430 dev_dbg(scirdev_to_dev(sci_dev),
431 "%s: device: %p event code: %x: %s\n",
432 __func__, sci_dev, event_code,
433 is_remote_device_ready(sci_dev)
434 ? "unexpected event"
435 : "unexpected event in wrong state");
436 status = SCI_FAILURE_INVALID_STATE;
437 break;
438 }
439
440 if (status != SCI_SUCCESS)
441 return status;
442
443 if (state == SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE) {
444
445 /* We pick up suspension events to handle specifically to this
446 * state. We resume the RNC right away.
447 */
448 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
449 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
450 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
451 }
452
453 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700454}
455
Dan Williams18606552011-05-01 14:57:11 -0700456static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
457 struct scic_sds_request *sci_req,
458 enum sci_status status)
Dan Williams88f3b622011-04-22 19:18:03 -0700459{
Dan Williams18606552011-05-01 14:57:11 -0700460 struct scic_sds_port *sci_port = sci_dev->owning_port;
461
462 /* cleanup requests that failed after starting on the port */
463 if (status != SCI_SUCCESS)
464 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
465 else
466 scic_sds_remote_device_increment_request_count(sci_dev);
467}
468
469enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
470 struct scic_sds_remote_device *sci_dev,
471 struct scic_sds_request *sci_req)
472{
473 struct sci_base_state_machine *sm = &sci_dev->state_machine;
474 enum scic_sds_remote_device_states state = sm->current_state_id;
475 struct scic_sds_port *sci_port = sci_dev->owning_port;
Dave Jiange76d6182011-05-04 15:02:03 -0700476 struct isci_request *ireq = sci_req->ireq;
Dan Williams18606552011-05-01 14:57:11 -0700477 enum sci_status status;
478
479 switch (state) {
480 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
481 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
482 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
483 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
484 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
485 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
486 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
487 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
488 default:
489 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
490 __func__, state);
491 return SCI_FAILURE_INVALID_STATE;
492 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
493 /* attempt to start an io request for this device object. The remote
494 * device object will issue the start request for the io and if
495 * successful it will start the request for the port object then
496 * increment its own request count.
497 */
498 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
499 if (status != SCI_SUCCESS)
500 return status;
501
502 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
503 if (status != SCI_SUCCESS)
504 break;
505
506 status = scic_sds_request_start(sci_req);
507 break;
508 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
509 /* handle the start io operation for a sata device that is in
510 * the command idle state. - Evalute the type of IO request to
511 * be started - If its an NCQ request change to NCQ substate -
512 * If its any other command change to the CMD substate
513 *
514 * If this is a softreset we may want to have a different
515 * substate.
516 */
517 enum scic_sds_remote_device_states new_state;
Dave Jiange76d6182011-05-04 15:02:03 -0700518 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700519
520 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
521 if (status != SCI_SUCCESS)
522 return status;
523
524 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
525 if (status != SCI_SUCCESS)
526 break;
527
528 status = sci_req->state_handlers->start_handler(sci_req);
529 if (status != SCI_SUCCESS)
530 break;
531
Dave Jiange76d6182011-05-04 15:02:03 -0700532 if (task->ata_task.use_ncq)
Dan Williams18606552011-05-01 14:57:11 -0700533 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
534 else {
535 sci_dev->working_request = sci_req;
536 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
537 }
538 sci_base_state_machine_change_state(sm, new_state);
539 break;
540 }
Dave Jiange76d6182011-05-04 15:02:03 -0700541 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
542 struct sas_task *task = isci_request_access_task(ireq);
543
544 if (task->ata_task.use_ncq) {
Dan Williams18606552011-05-01 14:57:11 -0700545 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
546 if (status != SCI_SUCCESS)
547 return status;
548
549 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
550 if (status != SCI_SUCCESS)
551 break;
552
553 status = sci_req->state_handlers->start_handler(sci_req);
554 } else
555 return SCI_FAILURE_INVALID_STATE;
556 break;
Dave Jiange76d6182011-05-04 15:02:03 -0700557 }
Dan Williams18606552011-05-01 14:57:11 -0700558 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
559 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
560 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
561 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
562 if (status != SCI_SUCCESS)
563 return status;
564
565 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
566 if (status != SCI_SUCCESS)
567 break;
568
569 status = scic_sds_request_start(sci_req);
570 if (status != SCI_SUCCESS)
571 break;
572
573 sci_dev->working_request = sci_req;
574 sci_base_state_machine_change_state(&sci_dev->state_machine,
575 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
576 break;
577 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
578 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
579 /* device is already handling a command it can not accept new commands
580 * until this one is complete.
581 */
582 return SCI_FAILURE_INVALID_STATE;
583 }
584
585 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
586 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700587}
588
Dan Williams10a09e62011-05-01 15:33:43 -0700589static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
590 struct scic_sds_remote_device *sci_dev,
591 struct scic_sds_request *sci_req)
Dan Williams88f3b622011-04-22 19:18:03 -0700592{
Dan Williams10a09e62011-05-01 15:33:43 -0700593 enum sci_status status;
594
595 status = scic_sds_request_complete(sci_req);
596 if (status != SCI_SUCCESS)
597 return status;
598
599 status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
600 if (status != SCI_SUCCESS)
601 return status;
602
603 scic_sds_remote_device_decrement_request_count(sci_dev);
604 return status;
605}
606
607enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
608 struct scic_sds_remote_device *sci_dev,
609 struct scic_sds_request *sci_req)
610{
611 struct sci_base_state_machine *sm = &sci_dev->state_machine;
612 enum scic_sds_remote_device_states state = sm->current_state_id;
613 struct scic_sds_port *sci_port = sci_dev->owning_port;
614 enum sci_status status;
615
616 switch (state) {
617 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
618 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
619 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
620 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
621 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
622 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
623 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
624 default:
625 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
626 __func__, state);
627 return SCI_FAILURE_INVALID_STATE;
628 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
629 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
630 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
631 status = common_complete_io(sci_port, sci_dev, sci_req);
632 break;
633 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
634 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
635 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
636 status = common_complete_io(sci_port, sci_dev, sci_req);
637 if (status != SCI_SUCCESS)
638 break;
639
640 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
641 /* This request causes hardware error, device needs to be Lun Reset.
642 * So here we force the state machine to IDLE state so the rest IOs
643 * can reach RNC state handler, these IOs will be completed by RNC with
644 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
645 */
646 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
647 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
648 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
649 break;
650 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
651 status = common_complete_io(sci_port, sci_dev, sci_req);
652 if (status != SCI_SUCCESS)
653 break;
654 sci_base_state_machine_change_state(sm, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
655 break;
656 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
657 status = common_complete_io(sci_port, sci_dev, sci_req);
658 if (status != SCI_SUCCESS)
659 break;
660
661 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
662 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
663 rnc_destruct_done,
664 sci_dev);
665 break;
666 }
667
668 if (status != SCI_SUCCESS)
669 dev_err(scirdev_to_dev(sci_dev),
670 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
671 "could not complete\n", __func__, sci_port,
672 sci_dev, sci_req, status);
673
674 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700675}
676
Dan Williams84b9b022011-05-01 15:53:25 -0700677static void scic_sds_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700678{
Dan Williams84b9b022011-05-01 15:53:25 -0700679 struct scic_sds_remote_device *sci_dev = dev;
680
681 /* we need to check if this request is still valid to continue. */
682 if (sci_dev->working_request)
683 scic_controller_continue_io(sci_dev->working_request);
684}
685
686enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
687 struct scic_sds_remote_device *sci_dev,
688 struct scic_sds_request *sci_req)
689{
690 struct sci_base_state_machine *sm = &sci_dev->state_machine;
691 enum scic_sds_remote_device_states state = sm->current_state_id;
692 struct scic_sds_port *sci_port = sci_dev->owning_port;
693 enum sci_status status;
694
695 switch (state) {
696 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
697 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
698 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
699 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
700 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
701 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
702 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
703 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
704 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
705 default:
706 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
707 __func__, state);
708 return SCI_FAILURE_INVALID_STATE;
709 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
710 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
711 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
712 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
713 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
714 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
715 if (status != SCI_SUCCESS)
716 return status;
717
718 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
719 if (status != SCI_SUCCESS)
720 goto out;
721
722 status = sci_req->state_handlers->start_handler(sci_req);
723 if (status != SCI_SUCCESS)
724 goto out;
725
726 /* Note: If the remote device state is not IDLE this will
727 * replace the request that probably resulted in the task
728 * management request.
729 */
730 sci_dev->working_request = sci_req;
731 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
732
733 /* The remote node context must cleanup the TCi to NCQ mapping
734 * table. The only way to do this correctly is to either write
735 * to the TLCR register or to invalidate and repost the RNC. In
736 * either case the remote node context state machine will take
737 * the correct action when the remote node context is suspended
738 * and later resumed.
739 */
740 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
741 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
742 scic_sds_remote_node_context_resume(&sci_dev->rnc,
743 scic_sds_remote_device_continue_request,
744 sci_dev);
745
746 out:
747 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
748 /* We need to let the controller start request handler know that
749 * it can't post TC yet. We will provide a callback function to
750 * post TC when RNC gets resumed.
751 */
752 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
753 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
754 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
755 if (status != SCI_SUCCESS)
756 return status;
757
758 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
759 if (status != SCI_SUCCESS)
760 break;
761
762 status = scic_sds_request_start(sci_req);
763 break;
764 }
765 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
766
767 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700768}
769
770/**
771 *
Dan Williams88f3b622011-04-22 19:18:03 -0700772 * @sci_dev:
773 * @request:
774 *
775 * This method takes the request and bulids an appropriate SCU context for the
776 * request and then requests the controller to post the request. none
777 */
778void scic_sds_remote_device_post_request(
779 struct scic_sds_remote_device *sci_dev,
780 u32 request)
781{
782 u32 context;
783
784 context = scic_sds_remote_device_build_command_context(sci_dev, request);
785
786 scic_sds_controller_post_request(
787 scic_sds_remote_device_get_controller(sci_dev),
788 context
789 );
790}
791
Dan Williamsab2e8f72011-04-27 16:32:45 -0700792/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700793 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700794 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700795 */
Dan Williamseb229672011-05-01 14:05:57 -0700796static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700797{
Dan Williamsab2e8f72011-04-27 16:32:45 -0700798 struct scic_sds_remote_device *sci_dev = _dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700799
Dan Williamse6225712011-05-01 16:26:09 -0700800 if (is_remote_device_ready(sci_dev))
801 return;
Dan Williams88f3b622011-04-22 19:18:03 -0700802
Dan Williamse6225712011-05-01 16:26:09 -0700803 /* go 'ready' if we are not already in a ready state */
804 sci_base_state_machine_change_state(&sci_dev->state_machine,
805 SCI_BASE_REMOTE_DEVICE_STATE_READY);
Dan Williams88f3b622011-04-22 19:18:03 -0700806}
807
Dan Williamsab2e8f72011-04-27 16:32:45 -0700808static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
809{
810 struct scic_sds_remote_device *sci_dev = _dev;
811 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
812 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
813
814 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
815 * As a result, avoid sending the ready notification.
816 */
817 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
818 isci_remote_device_ready(scic->ihost, idev);
819}
820
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000821static void scic_sds_remote_device_initial_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700822{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000823 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700824
Dan Williams88f3b622011-04-22 19:18:03 -0700825 /* Initial state is a transitional state to the stopped state */
826 sci_base_state_machine_change_state(&sci_dev->state_machine,
827 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
828}
829
830/**
Dan Williams88f3b622011-04-22 19:18:03 -0700831 * scic_remote_device_destruct() - free remote node context and destruct
832 * @remote_device: This parameter specifies the remote device to be destructed.
833 *
834 * Remote device objects are a limited resource. As such, they must be
835 * protected. Thus calls to construct and destruct are mutually exclusive and
836 * non-reentrant. The return value shall indicate if the device was
837 * successfully destructed or if some failure occurred. enum sci_status This value
838 * is returned if the device is successfully destructed.
839 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
840 * device isn't valid (e.g. it's already been destoryed, the handle isn't
841 * valid, etc.).
842 */
843static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
844{
Dan Williamsb8d82f62011-05-01 14:38:26 -0700845 struct sci_base_state_machine *sm = &sci_dev->state_machine;
846 enum scic_sds_remote_device_states state = sm->current_state_id;
847 struct scic_sds_controller *scic;
848
849 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
850 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
851 __func__, state);
852 return SCI_FAILURE_INVALID_STATE;
853 }
854
855 scic = sci_dev->owning_port->owning_controller;
856 scic_sds_controller_free_remote_node_context(scic, sci_dev,
857 sci_dev->rnc.remote_node_index);
858 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
859 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
860
861 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -0700862}
863
Dan Williams6f231dd2011-07-02 22:56:22 -0700864/**
865 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -0800866 * @ihost: This parameter specifies the isci host object.
867 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -0700868 *
869 */
Dan Williamsd9c37392011-03-03 17:59:32 -0800870static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700871{
Dan Williamsd9c37392011-03-03 17:59:32 -0800872 dev_dbg(&ihost->pdev->dev,
873 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700874
875 /* There should not be any outstanding io's. All paths to
876 * here should go through isci_remote_device_nuke_requests.
877 * If we hit this condition, we will need a way to complete
878 * io requests in process */
Dan Williamsd9c37392011-03-03 17:59:32 -0800879 while (!list_empty(&idev->reqs_in_process)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700880
Dan Williamsd9c37392011-03-03 17:59:32 -0800881 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700882 "%s: ** request list not empty! **\n", __func__);
883 BUG();
884 }
885
Dan Williams57f20f42011-04-21 18:14:45 -0700886 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -0800887 idev->domain_dev->lldd_dev = NULL;
888 idev->domain_dev = NULL;
889 idev->isci_port = NULL;
890 list_del_init(&idev->node);
Dan Williams6ad31fe2011-03-04 12:10:29 -0800891
Dan Williamsd9c37392011-03-03 17:59:32 -0800892 clear_bit(IDEV_START_PENDING, &idev->flags);
893 clear_bit(IDEV_STOP_PENDING, &idev->flags);
894 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700895}
896
Dan Williams88f3b622011-04-22 19:18:03 -0700897/**
898 * isci_remote_device_stop_complete() - This function is called by the scic
899 * when the remote device stop has completed. We mark the isci device as not
900 * ready and remove the isci remote device.
901 * @ihost: This parameter specifies the isci host object.
902 * @idev: This parameter specifies the remote device.
903 * @status: This parameter specifies status of the completion.
904 *
905 */
906static void isci_remote_device_stop_complete(struct isci_host *ihost,
907 struct isci_remote_device *idev)
908{
909 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
910
911 isci_remote_device_change_state(idev, isci_stopped);
912
913 /* after stop, we can tear down resources. */
914 isci_remote_device_deconstruct(ihost, idev);
915}
916
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000917static void scic_sds_remote_device_stopped_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700918{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000919 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700920 struct scic_sds_controller *scic;
921 struct isci_remote_device *idev;
922 struct isci_host *ihost;
923 u32 prev_state;
924
Dan Williams88f3b622011-04-22 19:18:03 -0700925 scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +0000926 ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000927 idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700928
Dan Williams88f3b622011-04-22 19:18:03 -0700929 /* If we are entering from the stopping state let the SCI User know that
930 * the stop operation has completed.
931 */
932 prev_state = sci_dev->state_machine.previous_state_id;
933 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
934 isci_remote_device_stop_complete(ihost, idev);
935
936 scic_sds_controller_remote_device_stopped(scic, sci_dev);
937}
938
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000939static void scic_sds_remote_device_starting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700940{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000941 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700942 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +0000943 struct isci_host *ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000944 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700945
Dan Williams88f3b622011-04-22 19:18:03 -0700946 isci_remote_device_not_ready(ihost, idev,
947 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
948}
949
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000950static void scic_sds_remote_device_ready_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700951{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000952 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700953 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
954 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700955
Dan Williams88f3b622011-04-22 19:18:03 -0700956 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
957
Dan Williamsab2e8f72011-04-27 16:32:45 -0700958 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
959 sci_base_state_machine_change_state(&sci_dev->state_machine,
960 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
961 } else if (dev_is_expander(dev)) {
962 sci_base_state_machine_change_state(&sci_dev->state_machine,
963 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
964 } else
965 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
Dan Williams88f3b622011-04-22 19:18:03 -0700966}
967
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000968static void scic_sds_remote_device_ready_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700969{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000970 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700971 struct domain_device *dev = sci_dev_to_domain(sci_dev);
972
973 if (dev->dev_type == SAS_END_DEV) {
974 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000975 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700976
Dan Williamsab2e8f72011-04-27 16:32:45 -0700977 isci_remote_device_not_ready(scic->ihost, idev,
Dan Williams88f3b622011-04-22 19:18:03 -0700978 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
979 }
980}
981
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000982static void scic_sds_remote_device_resetting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700983{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000984 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700985
Dan Williams88f3b622011-04-22 19:18:03 -0700986 scic_sds_remote_node_context_suspend(
987 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
988}
989
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000990static void scic_sds_remote_device_resetting_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700991{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000992 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700993
994 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
995}
996
Dan Williamsab2e8f72011-04-27 16:32:45 -0700997static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
998{
999 struct scic_sds_remote_device *sci_dev = object;
1000
Dan Williamsab2e8f72011-04-27 16:32:45 -07001001 sci_dev->working_request = NULL;
1002 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
1003 /*
1004 * Since the RNC is ready, it's alright to finish completion
1005 * processing (e.g. signal the remote device is ready). */
1006 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
1007 } else {
1008 scic_sds_remote_node_context_resume(&sci_dev->rnc,
1009 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
1010 sci_dev);
1011 }
1012}
1013
1014static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1015{
1016 struct scic_sds_remote_device *sci_dev = object;
1017 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1018
1019 BUG_ON(sci_dev->working_request == NULL);
1020
Dan Williamsab2e8f72011-04-27 16:32:45 -07001021 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1022 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1023}
1024
Dan Williamsab2e8f72011-04-27 16:32:45 -07001025static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1026{
1027 struct scic_sds_remote_device *sci_dev = object;
1028 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1029 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1030
Dan Williamsab2e8f72011-04-27 16:32:45 -07001031 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1032 isci_remote_device_not_ready(scic->ihost, idev,
1033 sci_dev->not_ready_reason);
1034}
1035
Dan Williamsab2e8f72011-04-27 16:32:45 -07001036static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1037{
1038 struct scic_sds_remote_device *sci_dev = object;
1039 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1040
Dan Williamsab2e8f72011-04-27 16:32:45 -07001041 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1042}
1043
1044static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1045{
1046 struct scic_sds_remote_device *sci_dev = object;
1047 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1048
1049 BUG_ON(sci_dev->working_request == NULL);
1050
Dan Williamsab2e8f72011-04-27 16:32:45 -07001051 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1052 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1053}
1054
1055static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1056{
1057 struct scic_sds_remote_device *sci_dev = object;
1058
1059 sci_dev->working_request = NULL;
1060}
Dan Williams88f3b622011-04-22 19:18:03 -07001061
1062static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1063 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1064 .enter_state = scic_sds_remote_device_initial_state_enter,
1065 },
1066 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1067 .enter_state = scic_sds_remote_device_stopped_state_enter,
1068 },
1069 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1070 .enter_state = scic_sds_remote_device_starting_state_enter,
1071 },
1072 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1073 .enter_state = scic_sds_remote_device_ready_state_enter,
1074 .exit_state = scic_sds_remote_device_ready_state_exit
1075 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001076 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1077 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1078 },
1079 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1080 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1081 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001082 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = { },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001083 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1084 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1085 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001086 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = { },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001087 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1088 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1089 },
1090 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1091 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1092 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1093 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001094 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = { },
1095 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001096 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1097 .enter_state = scic_sds_remote_device_resetting_state_enter,
1098 .exit_state = scic_sds_remote_device_resetting_state_exit
1099 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001100 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001101};
1102
1103/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001104 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001105 * @sci_port: SAS/SATA port through which this device is accessed.
1106 * @sci_dev: remote device to construct
1107 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001108 * This routine just performs benign initialization and does not
1109 * allocate the remote_node_context which is left to
1110 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1111 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001112 */
1113static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1114 struct scic_sds_remote_device *sci_dev)
1115{
1116 sci_dev->owning_port = sci_port;
1117 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001118
1119 sci_base_state_machine_construct(
1120 &sci_dev->state_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001121 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001122 scic_sds_remote_device_state_table,
1123 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1124 );
1125
1126 sci_base_state_machine_start(
1127 &sci_dev->state_machine
1128 );
1129
1130 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1131 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001132}
1133
1134/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001135 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001136 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001137 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1138 * the device is known to the SCI Core since it is contained in the
1139 * scic_phy object. Remote node context(s) is/are a global resource
1140 * allocated by this routine, freed by scic_remote_device_destruct().
1141 *
1142 * Returns:
1143 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1144 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1145 * sata-only controller instance.
1146 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001147 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001148static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1149 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001150{
1151 enum sci_status status;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001152 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001153
Dan Williamsb87ee302011-04-25 11:48:29 -07001154 scic_remote_device_construct(sci_port, sci_dev);
1155
Dan Williams88f3b622011-04-22 19:18:03 -07001156 /*
1157 * This information is request to determine how many remote node context
1158 * entries will be needed to store the remote node.
1159 */
Dan Williams88f3b622011-04-22 19:18:03 -07001160 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001161 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1162 sci_dev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001163 &sci_dev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001164
Dan Williamsa1a113b2011-04-21 18:44:45 -07001165 if (status != SCI_SUCCESS)
1166 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001167
Dan Williamsab2e8f72011-04-27 16:32:45 -07001168 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1169 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1170 /* pass */;
1171 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001172 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001173
Dan Williamsa1a113b2011-04-21 18:44:45 -07001174 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001175
Dan Williamsa1a113b2011-04-21 18:44:45 -07001176 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1177 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001178
Dan Williamsa1a113b2011-04-21 18:44:45 -07001179 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001180}
1181
Dan Williams88f3b622011-04-22 19:18:03 -07001182/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001183 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001184 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001185 * Remote node context(s) is/are a global resource allocated by this
1186 * routine, freed by scic_remote_device_destruct().
1187 *
1188 * Returns:
1189 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1190 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1191 * sata-only controller instance.
1192 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001193 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001194static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001195 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001196{
Dan Williamsa1a113b2011-04-21 18:44:45 -07001197 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001198 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001199
Dan Williamsb87ee302011-04-25 11:48:29 -07001200 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001201
Dan Williamsab2e8f72011-04-27 16:32:45 -07001202 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1203 sci_dev,
1204 &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001205 if (status != SCI_SUCCESS)
1206 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001207
Dan Williamsab2e8f72011-04-27 16:32:45 -07001208 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1209 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1210 /* pass */;
1211 else
1212 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001213
Dan Williamsa1a113b2011-04-21 18:44:45 -07001214 /*
1215 * For SAS-2 the physical link rate is actually a logical link
1216 * rate that incorporates multiplexing. The SCU doesn't
1217 * incorporate multiplexing and for the purposes of the
1218 * connection the logical link rate is that same as the
1219 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1220 * one another, so this code works for both situations. */
1221 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001222 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001223
Dan Williamsa1a113b2011-04-21 18:44:45 -07001224 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1225 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001226
Dan Williamsab2e8f72011-04-27 16:32:45 -07001227 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001228}
1229
1230/**
1231 * scic_remote_device_start() - This method will start the supplied remote
1232 * device. This method enables normal IO requests to flow through to the
1233 * remote device.
1234 * @remote_device: This parameter specifies the device to be started.
1235 * @timeout: This parameter specifies the number of milliseconds in which the
1236 * start operation should complete.
1237 *
1238 * An indication of whether the device was successfully started. SCI_SUCCESS
1239 * This value is returned if the device was successfully started.
1240 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1241 * the device when there have been no phys added to it.
1242 */
1243static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
Dan Williamseb229672011-05-01 14:05:57 -07001244 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001245{
Dan Williamseb229672011-05-01 14:05:57 -07001246 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1247 enum scic_sds_remote_device_states state = sm->current_state_id;
1248 enum sci_status status;
1249
1250 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1251 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1252 __func__, state);
1253 return SCI_FAILURE_INVALID_STATE;
1254 }
1255
1256 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1257 remote_device_resume_done,
1258 sci_dev);
1259 if (status != SCI_SUCCESS)
1260 return status;
1261
1262 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1263
1264 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001265}
Dan Williams6f231dd2011-07-02 22:56:22 -07001266
Dan Williams00d680e2011-04-25 14:29:29 -07001267static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1268 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001269{
Dan Williams00d680e2011-04-25 14:29:29 -07001270 struct scic_sds_port *sci_port = iport->sci_port_handle;
1271 struct isci_host *ihost = iport->isci_host;
1272 struct domain_device *dev = idev->domain_dev;
1273 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001274
Dan Williams00d680e2011-04-25 14:29:29 -07001275 if (dev->parent && dev_is_expander(dev->parent))
1276 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1277 else
1278 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001279
1280 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001281 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1282 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001283
1284 return status;
1285 }
1286
Dan Williams6f231dd2011-07-02 22:56:22 -07001287 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001288 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001289
Dan Williams00d680e2011-04-25 14:29:29 -07001290 if (status != SCI_SUCCESS)
1291 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1292 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001293
1294 return status;
1295}
1296
Dan Williams4393aa42011-03-31 13:10:44 -07001297void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001298{
1299 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001300
Dan Williams4393aa42011-03-31 13:10:44 -07001301 dev_dbg(&ihost->pdev->dev,
1302 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001303
1304 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -07001305 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -07001306
Dan Williams4393aa42011-03-31 13:10:44 -07001307 dev_dbg(&ihost->pdev->dev,
1308 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001309}
1310
Dan Williams6f231dd2011-07-02 22:56:22 -07001311/**
1312 * This function builds the isci_remote_device when a libsas dev_found message
1313 * is received.
1314 * @isci_host: This parameter specifies the isci host object.
1315 * @port: This parameter specifies the isci_port conected to this device.
1316 *
1317 * pointer to new isci_remote_device.
1318 */
1319static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001320isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001321{
Dan Williamsd9c37392011-03-03 17:59:32 -08001322 struct isci_remote_device *idev;
1323 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001324
Dan Williamsd9c37392011-03-03 17:59:32 -08001325 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001326 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001327 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1328 break;
1329 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001330
Dan Williamsd9c37392011-03-03 17:59:32 -08001331 if (i >= SCI_MAX_REMOTE_DEVICES) {
1332 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001333 return NULL;
1334 }
1335
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001336 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1337 return NULL;
1338
1339 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1340 return NULL;
1341
Dan Williamsd9c37392011-03-03 17:59:32 -08001342 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001343
Dan Williamsd9c37392011-03-03 17:59:32 -08001344 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001345}
Dan Williams6f231dd2011-07-02 22:56:22 -07001346
1347/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001348 * isci_remote_device_stop() - This function is called internally to stop the
1349 * remote device.
1350 * @isci_host: This parameter specifies the isci host object.
1351 * @isci_device: This parameter specifies the remote device.
1352 *
1353 * The status of the scic request to stop.
1354 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001355enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001356{
1357 enum sci_status status;
1358 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001359
Dan Williams6ad31fe2011-03-04 12:10:29 -08001360 dev_dbg(&ihost->pdev->dev,
1361 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001362
Dan Williams6ad31fe2011-03-04 12:10:29 -08001363 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001364
1365 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001366 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001367
Dan Williams6ad31fe2011-03-04 12:10:29 -08001368 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001369
Dan Williams6ad31fe2011-03-04 12:10:29 -08001370 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07001371 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001372 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001373
1374 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -08001375 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -08001376 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08001377 clear_bit(IDEV_ALLOCATED, &idev->flags);
1378 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001379
Dan Williams6ad31fe2011-03-04 12:10:29 -08001380 dev_dbg(&ihost->pdev->dev,
1381 "%s: idev = %p - after completion wait\n",
1382 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001383
Dan Williams6f231dd2011-07-02 22:56:22 -07001384 return status;
1385}
1386
1387/**
1388 * isci_remote_device_gone() - This function is called by libsas when a domain
1389 * device is removed.
1390 * @domain_device: This parameter specifies the libsas domain device.
1391 *
1392 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001393void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001394{
Dan Williams4393aa42011-03-31 13:10:44 -07001395 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001396 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001397
Dan Williams6ad31fe2011-03-04 12:10:29 -08001398 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001399 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001400 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001401
Dan Williams6ad31fe2011-03-04 12:10:29 -08001402 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001403}
1404
1405
1406/**
1407 * isci_remote_device_found() - This function is called by libsas when a remote
1408 * device is discovered. A remote device object is created and started. the
1409 * function then sleeps until the sci core device started message is
1410 * received.
1411 * @domain_device: This parameter specifies the libsas domain device.
1412 *
1413 * status, zero indicates success.
1414 */
1415int isci_remote_device_found(struct domain_device *domain_dev)
1416{
Dan Williams4393aa42011-03-31 13:10:44 -07001417 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001418 struct isci_port *isci_port;
1419 struct isci_phy *isci_phy;
1420 struct asd_sas_port *sas_port;
1421 struct asd_sas_phy *sas_phy;
1422 struct isci_remote_device *isci_device;
1423 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001424
Dan Williams6f231dd2011-07-02 22:56:22 -07001425 dev_dbg(&isci_host->pdev->dev,
1426 "%s: domain_device = %p\n", __func__, domain_dev);
1427
Dan Williams0cf89d12011-02-18 09:25:07 -08001428 wait_for_start(isci_host);
1429
Dan Williams6f231dd2011-07-02 22:56:22 -07001430 sas_port = domain_dev->port;
1431 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1432 port_phy_el);
1433 isci_phy = to_isci_phy(sas_phy);
1434 isci_port = isci_phy->isci_port;
1435
1436 /* we are being called for a device on this port,
1437 * so it has to come up eventually
1438 */
1439 wait_for_completion(&isci_port->start_complete);
1440
1441 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1442 (isci_stopped == isci_port_get_state(isci_port)))
1443 return -ENODEV;
1444
1445 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001446 if (!isci_device)
1447 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001448
1449 INIT_LIST_HEAD(&isci_device->node);
1450 domain_dev->lldd_dev = isci_device;
1451 isci_device->domain_dev = domain_dev;
1452 isci_device->isci_port = isci_port;
1453 isci_remote_device_change_state(isci_device, isci_starting);
1454
1455
Dan Williams1a380452011-03-03 18:01:43 -08001456 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001457 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1458
Dan Williams6ad31fe2011-03-04 12:10:29 -08001459 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001460 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -08001461 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001462
Dan Williams6f231dd2011-07-02 22:56:22 -07001463 dev_dbg(&isci_host->pdev->dev,
1464 "%s: isci_device = %p\n",
1465 __func__, isci_device);
1466
1467 if (status != SCI_SUCCESS) {
1468
Dan Williams1a380452011-03-03 18:01:43 -08001469 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001470 isci_remote_device_deconstruct(
1471 isci_host,
1472 isci_device
1473 );
Dan Williams1a380452011-03-03 18:01:43 -08001474 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001475 return -ENODEV;
1476 }
1477
Dan Williams6ad31fe2011-03-04 12:10:29 -08001478 /* wait for the device ready callback. */
1479 wait_for_device_start(isci_host, isci_device);
1480
Dan Williams6f231dd2011-07-02 22:56:22 -07001481 return 0;
1482}
1483/**
1484 * isci_device_is_reset_pending() - This function will check if there is any
1485 * pending reset condition on the device.
1486 * @request: This parameter is the isci_device object.
1487 *
1488 * true if there is a reset pending for the device.
1489 */
1490bool isci_device_is_reset_pending(
1491 struct isci_host *isci_host,
1492 struct isci_remote_device *isci_device)
1493{
1494 struct isci_request *isci_request;
1495 struct isci_request *tmp_req;
1496 bool reset_is_pending = false;
1497 unsigned long flags;
1498
1499 dev_dbg(&isci_host->pdev->dev,
1500 "%s: isci_device = %p\n", __func__, isci_device);
1501
1502 spin_lock_irqsave(&isci_host->scic_lock, flags);
1503
1504 /* Check for reset on all pending requests. */
1505 list_for_each_entry_safe(isci_request, tmp_req,
1506 &isci_device->reqs_in_process, dev_node) {
1507 dev_dbg(&isci_host->pdev->dev,
1508 "%s: isci_device = %p request = %p\n",
1509 __func__, isci_device, isci_request);
1510
1511 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001512 struct sas_task *task = isci_request_access_task(
1513 isci_request);
1514
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001515 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001516 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1517 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001518 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001519 }
1520 }
1521
1522 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1523
1524 dev_dbg(&isci_host->pdev->dev,
1525 "%s: isci_device = %p reset_is_pending = %d\n",
1526 __func__, isci_device, reset_is_pending);
1527
1528 return reset_is_pending;
1529}
1530
1531/**
1532 * isci_device_clear_reset_pending() - This function will clear if any pending
1533 * reset condition flags on the device.
1534 * @request: This parameter is the isci_device object.
1535 *
1536 * true if there is a reset pending for the device.
1537 */
Dan Williams4393aa42011-03-31 13:10:44 -07001538void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001539{
1540 struct isci_request *isci_request;
1541 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07001542 unsigned long flags = 0;
1543
Dan Williams4393aa42011-03-31 13:10:44 -07001544 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1545 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07001546
Dan Williams4393aa42011-03-31 13:10:44 -07001547 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001548
1549 /* Clear reset pending on all pending requests. */
1550 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07001551 &idev->reqs_in_process, dev_node) {
1552 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1553 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001554
1555 if (isci_request->ttype == io_task) {
1556
1557 unsigned long flags2;
1558 struct sas_task *task = isci_request_access_task(
1559 isci_request);
1560
1561 spin_lock_irqsave(&task->task_state_lock, flags2);
1562 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1563 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1564 }
1565 }
Dan Williams4393aa42011-03-31 13:10:44 -07001566 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001567}