blob: ee6fe1db8f995aaee8a30f39ed43959a3abf6b7e [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);
Dan Williamsd06b4872011-05-02 13:59:25 -0700894 clear_bit(IDEV_EH, &idev->flags);
Dan Williamsd9c37392011-03-03 17:59:32 -0800895 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700896}
897
Dan Williams88f3b622011-04-22 19:18:03 -0700898/**
899 * isci_remote_device_stop_complete() - This function is called by the scic
900 * when the remote device stop has completed. We mark the isci device as not
901 * ready and remove the isci remote device.
902 * @ihost: This parameter specifies the isci host object.
903 * @idev: This parameter specifies the remote device.
904 * @status: This parameter specifies status of the completion.
905 *
906 */
907static void isci_remote_device_stop_complete(struct isci_host *ihost,
908 struct isci_remote_device *idev)
909{
910 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
911
912 isci_remote_device_change_state(idev, isci_stopped);
913
914 /* after stop, we can tear down resources. */
915 isci_remote_device_deconstruct(ihost, idev);
916}
917
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000918static void scic_sds_remote_device_stopped_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700919{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000920 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700921 struct scic_sds_controller *scic;
922 struct isci_remote_device *idev;
923 struct isci_host *ihost;
924 u32 prev_state;
925
Dan Williams88f3b622011-04-22 19:18:03 -0700926 scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +0000927 ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000928 idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700929
Dan Williams88f3b622011-04-22 19:18:03 -0700930 /* If we are entering from the stopping state let the SCI User know that
931 * the stop operation has completed.
932 */
933 prev_state = sci_dev->state_machine.previous_state_id;
934 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
935 isci_remote_device_stop_complete(ihost, idev);
936
937 scic_sds_controller_remote_device_stopped(scic, sci_dev);
938}
939
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000940static void scic_sds_remote_device_starting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700941{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000942 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700943 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +0000944 struct isci_host *ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000945 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700946
Dan Williams88f3b622011-04-22 19:18:03 -0700947 isci_remote_device_not_ready(ihost, idev,
948 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
949}
950
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000951static void scic_sds_remote_device_ready_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700952{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000953 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700954 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
955 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700956
Dan Williams88f3b622011-04-22 19:18:03 -0700957 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
958
Dan Williamsab2e8f72011-04-27 16:32:45 -0700959 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
960 sci_base_state_machine_change_state(&sci_dev->state_machine,
961 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
962 } else if (dev_is_expander(dev)) {
963 sci_base_state_machine_change_state(&sci_dev->state_machine,
964 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
965 } else
966 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
Dan Williams88f3b622011-04-22 19:18:03 -0700967}
968
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000969static void scic_sds_remote_device_ready_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700970{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000971 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700972 struct domain_device *dev = sci_dev_to_domain(sci_dev);
973
974 if (dev->dev_type == SAS_END_DEV) {
975 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000976 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700977
Dan Williamsab2e8f72011-04-27 16:32:45 -0700978 isci_remote_device_not_ready(scic->ihost, idev,
Dan Williams88f3b622011-04-22 19:18:03 -0700979 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
980 }
981}
982
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000983static void scic_sds_remote_device_resetting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700984{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000985 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700986
Dan Williams88f3b622011-04-22 19:18:03 -0700987 scic_sds_remote_node_context_suspend(
988 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
989}
990
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000991static void scic_sds_remote_device_resetting_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700992{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000993 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700994
995 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
996}
997
Dan Williamsab2e8f72011-04-27 16:32:45 -0700998static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
999{
1000 struct scic_sds_remote_device *sci_dev = object;
1001
Dan Williamsab2e8f72011-04-27 16:32:45 -07001002 sci_dev->working_request = NULL;
1003 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
1004 /*
1005 * Since the RNC is ready, it's alright to finish completion
1006 * processing (e.g. signal the remote device is ready). */
1007 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
1008 } else {
1009 scic_sds_remote_node_context_resume(&sci_dev->rnc,
1010 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
1011 sci_dev);
1012 }
1013}
1014
1015static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1016{
1017 struct scic_sds_remote_device *sci_dev = object;
1018 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1019
1020 BUG_ON(sci_dev->working_request == NULL);
1021
Dan Williamsab2e8f72011-04-27 16:32:45 -07001022 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1023 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1024}
1025
Dan Williamsab2e8f72011-04-27 16:32:45 -07001026static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1027{
1028 struct scic_sds_remote_device *sci_dev = object;
1029 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1030 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1031
Dan Williamsab2e8f72011-04-27 16:32:45 -07001032 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1033 isci_remote_device_not_ready(scic->ihost, idev,
1034 sci_dev->not_ready_reason);
1035}
1036
Dan Williamsab2e8f72011-04-27 16:32:45 -07001037static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1038{
1039 struct scic_sds_remote_device *sci_dev = object;
1040 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1041
Dan Williamsab2e8f72011-04-27 16:32:45 -07001042 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1043}
1044
1045static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1046{
1047 struct scic_sds_remote_device *sci_dev = object;
1048 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1049
1050 BUG_ON(sci_dev->working_request == NULL);
1051
Dan Williamsab2e8f72011-04-27 16:32:45 -07001052 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1053 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1054}
1055
1056static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1057{
1058 struct scic_sds_remote_device *sci_dev = object;
1059
1060 sci_dev->working_request = NULL;
1061}
Dan Williams88f3b622011-04-22 19:18:03 -07001062
1063static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1064 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1065 .enter_state = scic_sds_remote_device_initial_state_enter,
1066 },
1067 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1068 .enter_state = scic_sds_remote_device_stopped_state_enter,
1069 },
1070 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1071 .enter_state = scic_sds_remote_device_starting_state_enter,
1072 },
1073 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1074 .enter_state = scic_sds_remote_device_ready_state_enter,
1075 .exit_state = scic_sds_remote_device_ready_state_exit
1076 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001077 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1078 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1079 },
1080 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1081 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1082 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001083 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = { },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001084 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1085 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1086 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001087 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = { },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001088 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1089 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1090 },
1091 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1092 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1093 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1094 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001095 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = { },
1096 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001097 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1098 .enter_state = scic_sds_remote_device_resetting_state_enter,
1099 .exit_state = scic_sds_remote_device_resetting_state_exit
1100 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001101 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001102};
1103
1104/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001105 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001106 * @sci_port: SAS/SATA port through which this device is accessed.
1107 * @sci_dev: remote device to construct
1108 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001109 * This routine just performs benign initialization and does not
1110 * allocate the remote_node_context which is left to
1111 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1112 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001113 */
1114static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1115 struct scic_sds_remote_device *sci_dev)
1116{
1117 sci_dev->owning_port = sci_port;
1118 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001119
1120 sci_base_state_machine_construct(
1121 &sci_dev->state_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001122 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001123 scic_sds_remote_device_state_table,
1124 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1125 );
1126
1127 sci_base_state_machine_start(
1128 &sci_dev->state_machine
1129 );
1130
1131 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1132 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001133}
1134
1135/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001136 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001137 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001138 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1139 * the device is known to the SCI Core since it is contained in the
1140 * scic_phy object. Remote node context(s) is/are a global resource
1141 * allocated by this routine, freed by scic_remote_device_destruct().
1142 *
1143 * Returns:
1144 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1145 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1146 * sata-only controller instance.
1147 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001148 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001149static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1150 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001151{
1152 enum sci_status status;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001153 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001154
Dan Williamsb87ee302011-04-25 11:48:29 -07001155 scic_remote_device_construct(sci_port, sci_dev);
1156
Dan Williams88f3b622011-04-22 19:18:03 -07001157 /*
1158 * This information is request to determine how many remote node context
1159 * entries will be needed to store the remote node.
1160 */
Dan Williams88f3b622011-04-22 19:18:03 -07001161 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001162 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1163 sci_dev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001164 &sci_dev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001165
Dan Williamsa1a113b2011-04-21 18:44:45 -07001166 if (status != SCI_SUCCESS)
1167 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001168
Dan Williamsab2e8f72011-04-27 16:32:45 -07001169 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1170 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1171 /* pass */;
1172 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001173 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001174
Dan Williamsa1a113b2011-04-21 18:44:45 -07001175 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001176
Dan Williamsa1a113b2011-04-21 18:44:45 -07001177 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1178 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001179
Dan Williamsa1a113b2011-04-21 18:44:45 -07001180 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001181}
1182
Dan Williams88f3b622011-04-22 19:18:03 -07001183/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001184 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001185 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001186 * Remote node context(s) is/are a global resource allocated by this
1187 * routine, freed by scic_remote_device_destruct().
1188 *
1189 * Returns:
1190 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1191 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1192 * sata-only controller instance.
1193 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001194 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001195static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001196 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001197{
Dan Williamsa1a113b2011-04-21 18:44:45 -07001198 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001199 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001200
Dan Williamsb87ee302011-04-25 11:48:29 -07001201 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001202
Dan Williamsab2e8f72011-04-27 16:32:45 -07001203 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1204 sci_dev,
1205 &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001206 if (status != SCI_SUCCESS)
1207 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001208
Dan Williamsab2e8f72011-04-27 16:32:45 -07001209 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1210 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1211 /* pass */;
1212 else
1213 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001214
Dan Williamsa1a113b2011-04-21 18:44:45 -07001215 /*
1216 * For SAS-2 the physical link rate is actually a logical link
1217 * rate that incorporates multiplexing. The SCU doesn't
1218 * incorporate multiplexing and for the purposes of the
1219 * connection the logical link rate is that same as the
1220 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1221 * one another, so this code works for both situations. */
1222 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001223 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001224
Dan Williamsa1a113b2011-04-21 18:44:45 -07001225 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1226 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001227
Dan Williamsab2e8f72011-04-27 16:32:45 -07001228 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001229}
1230
1231/**
1232 * scic_remote_device_start() - This method will start the supplied remote
1233 * device. This method enables normal IO requests to flow through to the
1234 * remote device.
1235 * @remote_device: This parameter specifies the device to be started.
1236 * @timeout: This parameter specifies the number of milliseconds in which the
1237 * start operation should complete.
1238 *
1239 * An indication of whether the device was successfully started. SCI_SUCCESS
1240 * This value is returned if the device was successfully started.
1241 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1242 * the device when there have been no phys added to it.
1243 */
1244static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
Dan Williamseb229672011-05-01 14:05:57 -07001245 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001246{
Dan Williamseb229672011-05-01 14:05:57 -07001247 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1248 enum scic_sds_remote_device_states state = sm->current_state_id;
1249 enum sci_status status;
1250
1251 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1252 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1253 __func__, state);
1254 return SCI_FAILURE_INVALID_STATE;
1255 }
1256
1257 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1258 remote_device_resume_done,
1259 sci_dev);
1260 if (status != SCI_SUCCESS)
1261 return status;
1262
1263 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1264
1265 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001266}
Dan Williams6f231dd2011-07-02 22:56:22 -07001267
Dan Williams00d680e2011-04-25 14:29:29 -07001268static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1269 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001270{
Dan Williams00d680e2011-04-25 14:29:29 -07001271 struct scic_sds_port *sci_port = iport->sci_port_handle;
1272 struct isci_host *ihost = iport->isci_host;
1273 struct domain_device *dev = idev->domain_dev;
1274 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001275
Dan Williams00d680e2011-04-25 14:29:29 -07001276 if (dev->parent && dev_is_expander(dev->parent))
1277 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1278 else
1279 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001280
1281 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001282 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1283 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001284
1285 return status;
1286 }
1287
Dan Williams6f231dd2011-07-02 22:56:22 -07001288 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001289 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001290
Dan Williams00d680e2011-04-25 14:29:29 -07001291 if (status != SCI_SUCCESS)
1292 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1293 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001294
1295 return status;
1296}
1297
Dan Williams4393aa42011-03-31 13:10:44 -07001298void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001299{
1300 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001301
Dan Williams4393aa42011-03-31 13:10:44 -07001302 dev_dbg(&ihost->pdev->dev,
1303 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001304
1305 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -07001306 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -07001307
Dan Williams4393aa42011-03-31 13:10:44 -07001308 dev_dbg(&ihost->pdev->dev,
1309 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001310}
1311
Dan Williams6f231dd2011-07-02 22:56:22 -07001312/**
1313 * This function builds the isci_remote_device when a libsas dev_found message
1314 * is received.
1315 * @isci_host: This parameter specifies the isci host object.
1316 * @port: This parameter specifies the isci_port conected to this device.
1317 *
1318 * pointer to new isci_remote_device.
1319 */
1320static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001321isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001322{
Dan Williamsd9c37392011-03-03 17:59:32 -08001323 struct isci_remote_device *idev;
1324 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001325
Dan Williamsd9c37392011-03-03 17:59:32 -08001326 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001327 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001328 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1329 break;
1330 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001331
Dan Williamsd9c37392011-03-03 17:59:32 -08001332 if (i >= SCI_MAX_REMOTE_DEVICES) {
1333 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001334 return NULL;
1335 }
1336
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001337 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1338 return NULL;
1339
1340 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1341 return NULL;
1342
Dan Williamsd9c37392011-03-03 17:59:32 -08001343 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001344
Dan Williamsd9c37392011-03-03 17:59:32 -08001345 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001346}
Dan Williams6f231dd2011-07-02 22:56:22 -07001347
1348/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001349 * isci_remote_device_stop() - This function is called internally to stop the
1350 * remote device.
1351 * @isci_host: This parameter specifies the isci host object.
1352 * @isci_device: This parameter specifies the remote device.
1353 *
1354 * The status of the scic request to stop.
1355 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001356enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001357{
1358 enum sci_status status;
1359 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001360
Dan Williams6ad31fe2011-03-04 12:10:29 -08001361 dev_dbg(&ihost->pdev->dev,
1362 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001363
Dan Williams6ad31fe2011-03-04 12:10:29 -08001364 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001365
1366 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001367 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001368
Dan Williams6ad31fe2011-03-04 12:10:29 -08001369 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001370
Dan Williams6ad31fe2011-03-04 12:10:29 -08001371 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07001372 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001373 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001374
1375 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -08001376 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -08001377 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08001378 clear_bit(IDEV_ALLOCATED, &idev->flags);
1379 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001380
Dan Williams6ad31fe2011-03-04 12:10:29 -08001381 dev_dbg(&ihost->pdev->dev,
1382 "%s: idev = %p - after completion wait\n",
1383 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001384
Dan Williams6f231dd2011-07-02 22:56:22 -07001385 return status;
1386}
1387
1388/**
1389 * isci_remote_device_gone() - This function is called by libsas when a domain
1390 * device is removed.
1391 * @domain_device: This parameter specifies the libsas domain device.
1392 *
1393 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001394void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001395{
Dan Williams4393aa42011-03-31 13:10:44 -07001396 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001397 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001398
Dan Williams6ad31fe2011-03-04 12:10:29 -08001399 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001400 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001401 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001402
Dan Williams6ad31fe2011-03-04 12:10:29 -08001403 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001404}
1405
1406
1407/**
1408 * isci_remote_device_found() - This function is called by libsas when a remote
1409 * device is discovered. A remote device object is created and started. the
1410 * function then sleeps until the sci core device started message is
1411 * received.
1412 * @domain_device: This parameter specifies the libsas domain device.
1413 *
1414 * status, zero indicates success.
1415 */
1416int isci_remote_device_found(struct domain_device *domain_dev)
1417{
Dan Williams4393aa42011-03-31 13:10:44 -07001418 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001419 struct isci_port *isci_port;
1420 struct isci_phy *isci_phy;
1421 struct asd_sas_port *sas_port;
1422 struct asd_sas_phy *sas_phy;
1423 struct isci_remote_device *isci_device;
1424 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001425
Dan Williams6f231dd2011-07-02 22:56:22 -07001426 dev_dbg(&isci_host->pdev->dev,
1427 "%s: domain_device = %p\n", __func__, domain_dev);
1428
Dan Williams0cf89d12011-02-18 09:25:07 -08001429 wait_for_start(isci_host);
1430
Dan Williams6f231dd2011-07-02 22:56:22 -07001431 sas_port = domain_dev->port;
1432 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1433 port_phy_el);
1434 isci_phy = to_isci_phy(sas_phy);
1435 isci_port = isci_phy->isci_port;
1436
1437 /* we are being called for a device on this port,
1438 * so it has to come up eventually
1439 */
1440 wait_for_completion(&isci_port->start_complete);
1441
1442 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1443 (isci_stopped == isci_port_get_state(isci_port)))
1444 return -ENODEV;
1445
1446 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001447 if (!isci_device)
1448 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001449
1450 INIT_LIST_HEAD(&isci_device->node);
1451 domain_dev->lldd_dev = isci_device;
1452 isci_device->domain_dev = domain_dev;
1453 isci_device->isci_port = isci_port;
1454 isci_remote_device_change_state(isci_device, isci_starting);
1455
1456
Dan Williams1a380452011-03-03 18:01:43 -08001457 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001458 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1459
Dan Williams6ad31fe2011-03-04 12:10:29 -08001460 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001461 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -08001462 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001463
Dan Williams6f231dd2011-07-02 22:56:22 -07001464 dev_dbg(&isci_host->pdev->dev,
1465 "%s: isci_device = %p\n",
1466 __func__, isci_device);
1467
1468 if (status != SCI_SUCCESS) {
1469
Dan Williams1a380452011-03-03 18:01:43 -08001470 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001471 isci_remote_device_deconstruct(
1472 isci_host,
1473 isci_device
1474 );
Dan Williams1a380452011-03-03 18:01:43 -08001475 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001476 return -ENODEV;
1477 }
1478
Dan Williams6ad31fe2011-03-04 12:10:29 -08001479 /* wait for the device ready callback. */
1480 wait_for_device_start(isci_host, isci_device);
1481
Dan Williams6f231dd2011-07-02 22:56:22 -07001482 return 0;
1483}
1484/**
1485 * isci_device_is_reset_pending() - This function will check if there is any
1486 * pending reset condition on the device.
1487 * @request: This parameter is the isci_device object.
1488 *
1489 * true if there is a reset pending for the device.
1490 */
1491bool isci_device_is_reset_pending(
1492 struct isci_host *isci_host,
1493 struct isci_remote_device *isci_device)
1494{
1495 struct isci_request *isci_request;
1496 struct isci_request *tmp_req;
1497 bool reset_is_pending = false;
1498 unsigned long flags;
1499
1500 dev_dbg(&isci_host->pdev->dev,
1501 "%s: isci_device = %p\n", __func__, isci_device);
1502
1503 spin_lock_irqsave(&isci_host->scic_lock, flags);
1504
1505 /* Check for reset on all pending requests. */
1506 list_for_each_entry_safe(isci_request, tmp_req,
1507 &isci_device->reqs_in_process, dev_node) {
1508 dev_dbg(&isci_host->pdev->dev,
1509 "%s: isci_device = %p request = %p\n",
1510 __func__, isci_device, isci_request);
1511
1512 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001513 struct sas_task *task = isci_request_access_task(
1514 isci_request);
1515
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001516 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001517 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1518 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001519 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001520 }
1521 }
1522
1523 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1524
1525 dev_dbg(&isci_host->pdev->dev,
1526 "%s: isci_device = %p reset_is_pending = %d\n",
1527 __func__, isci_device, reset_is_pending);
1528
1529 return reset_is_pending;
1530}
1531
1532/**
1533 * isci_device_clear_reset_pending() - This function will clear if any pending
1534 * reset condition flags on the device.
1535 * @request: This parameter is the isci_device object.
1536 *
1537 * true if there is a reset pending for the device.
1538 */
Dan Williams4393aa42011-03-31 13:10:44 -07001539void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001540{
1541 struct isci_request *isci_request;
1542 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07001543 unsigned long flags = 0;
1544
Dan Williams4393aa42011-03-31 13:10:44 -07001545 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1546 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07001547
Dan Williams4393aa42011-03-31 13:10:44 -07001548 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001549
1550 /* Clear reset pending on all pending requests. */
1551 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07001552 &idev->reqs_in_process, dev_node) {
1553 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1554 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001555
1556 if (isci_request->ttype == io_task) {
1557
1558 unsigned long flags2;
1559 struct sas_task *task = isci_request_access_task(
1560 isci_request);
1561
1562 spin_lock_irqsave(&task->task_state_lock, flags2);
1563 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1564 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1565 }
1566 }
Dan Williams4393aa42011-03-31 13:10:44 -07001567 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001568}