blob: b900e2c1b63ea6831ac7a2adc2a12bbe216bb161 [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 "remote_node_context.h"
Dan Williams88f3b622011-04-22 19:18:03 -070061#include "scu_event_codes.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070062#include "task.h"
63
Dan Williamsab2e8f72011-04-27 16:32:45 -070064/**
65 * isci_remote_device_change_state() - This function gets the status of the
66 * remote_device object.
67 * @isci_device: This parameter points to the isci_remote_device object
68 *
69 * status of the object as a isci_status enum.
70 */
71void isci_remote_device_change_state(
72 struct isci_remote_device *isci_device,
73 enum isci_status status)
74{
75 unsigned long flags;
76
77 spin_lock_irqsave(&isci_device->state_lock, flags);
78 isci_device->status = status;
79 spin_unlock_irqrestore(&isci_device->state_lock, flags);
80}
81
82/**
83 * isci_remote_device_not_ready() - This function is called by the scic when
84 * the remote device is not ready. We mark the isci device as ready (not
85 * "ready_for_io") and signal the waiting proccess.
86 * @isci_host: This parameter specifies the isci host object.
87 * @isci_device: This parameter specifies the remote device
88 *
89 */
90static void isci_remote_device_not_ready(struct isci_host *ihost,
91 struct isci_remote_device *idev, u32 reason)
92{
93 dev_dbg(&ihost->pdev->dev,
94 "%s: isci_device = %p\n", __func__, idev);
95
96 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
97 isci_remote_device_change_state(idev, isci_stopping);
98 else
99 /* device ready is actually a "not ready for io" state. */
100 isci_remote_device_change_state(idev, isci_ready);
101}
102
103/**
104 * isci_remote_device_ready() - This function is called by the scic when the
105 * remote device is ready. We mark the isci device as ready and signal the
106 * waiting proccess.
107 * @ihost: our valid isci_host
108 * @idev: remote device
109 *
110 */
111static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
112{
113 dev_dbg(&ihost->pdev->dev,
114 "%s: idev = %p\n", __func__, idev);
115
116 isci_remote_device_change_state(idev, isci_ready_for_io);
117 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
118 wake_up(&ihost->eventq);
119}
120
Dan Williamsec575662011-05-01 14:19:25 -0700121/* called once the remote node context is ready to be freed.
122 * The remote device can now report that its stop operation is complete. none
123 */
124static void rnc_destruct_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700125{
Dan Williamsec575662011-05-01 14:19:25 -0700126 struct scic_sds_remote_device *sci_dev = _dev;
127
128 BUG_ON(sci_dev->started_request_count != 0);
129 sci_base_state_machine_change_state(&sci_dev->state_machine,
130 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
131}
132
133static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
134{
135 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
136 u32 i, request_count = sci_dev->started_request_count;
137 enum sci_status status = SCI_SUCCESS;
138
139 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
140 struct scic_sds_request *sci_req;
141 enum sci_status s;
142
143 sci_req = scic->io_request_table[i];
144 if (!sci_req || sci_req->target_device != sci_dev)
145 continue;
146 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
147 if (s != SCI_SUCCESS)
148 status = s;
149 }
150
151 return status;
152}
153
154enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
155 u32 timeout)
156{
157 struct sci_base_state_machine *sm = &sci_dev->state_machine;
158 enum scic_sds_remote_device_states state = sm->current_state_id;
159
160 switch (state) {
161 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
162 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
163 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
164 default:
165 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
166 __func__, state);
167 return SCI_FAILURE_INVALID_STATE;
168 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
169 return SCI_SUCCESS;
170 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
171 /* device not started so there had better be no requests */
172 BUG_ON(sci_dev->started_request_count != 0);
173 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
174 rnc_destruct_done, sci_dev);
175 /* Transition to the stopping state and wait for the
176 * remote node to complete being posted and invalidated.
177 */
178 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
179 return SCI_SUCCESS;
180 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
181 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
182 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
183 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
184 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
185 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
186 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
187 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
188 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
189 if (sci_dev->started_request_count == 0) {
190 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
191 rnc_destruct_done, sci_dev);
192 return SCI_SUCCESS;
193 } else
194 return scic_sds_remote_device_terminate_requests(sci_dev);
195 break;
196 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
197 /* All requests should have been terminated, but if there is an
198 * attempt to stop a device already in the stopping state, then
199 * try again to terminate.
200 */
201 return scic_sds_remote_device_terminate_requests(sci_dev);
202 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
203 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
204 return SCI_SUCCESS;
205 }
Dan Williams88f3b622011-04-22 19:18:03 -0700206}
Dan Williams6f231dd2011-07-02 22:56:22 -0700207
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700208enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700209{
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700210 struct sci_base_state_machine *sm = &sci_dev->state_machine;
211 enum scic_sds_remote_device_states state = sm->current_state_id;
212
213 switch (state) {
214 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
215 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
216 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
217 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
218 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
219 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
220 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
221 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
222 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
223 default:
224 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
225 __func__, state);
226 return SCI_FAILURE_INVALID_STATE;
227 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
228 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
229 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
230 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
231 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
232 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
233 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
234 return SCI_SUCCESS;
235 }
Dan Williams88f3b622011-04-22 19:18:03 -0700236}
237
Dan Williams81515182011-05-01 14:53:00 -0700238enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700239{
Dan Williams81515182011-05-01 14:53:00 -0700240 struct sci_base_state_machine *sm = &sci_dev->state_machine;
241 enum scic_sds_remote_device_states state = sm->current_state_id;
Dan Williams88f3b622011-04-22 19:18:03 -0700242
Dan Williams81515182011-05-01 14:53:00 -0700243 if (state != SCI_BASE_REMOTE_DEVICE_STATE_RESETTING) {
244 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
245 __func__, state);
246 return SCI_FAILURE_INVALID_STATE;
247 }
248
249 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_READY);
250 return SCI_SUCCESS;
251}
Dan Williams88f3b622011-04-22 19:18:03 -0700252
Dan Williams323f0ec2011-05-01 16:15:47 -0700253enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
254 u32 suspend_type)
Dan Williams88f3b622011-04-22 19:18:03 -0700255{
Dan Williams323f0ec2011-05-01 16:15:47 -0700256 struct sci_base_state_machine *sm = &sci_dev->state_machine;
257 enum scic_sds_remote_device_states state = sm->current_state_id;
258
259 if (state != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD) {
260 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
261 __func__, state);
262 return SCI_FAILURE_INVALID_STATE;
263 }
264
265 return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
266 suspend_type, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700267}
268
Dan Williams01bec772011-05-01 16:51:11 -0700269enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
270 u32 frame_index)
Dan Williams88f3b622011-04-22 19:18:03 -0700271{
Dan Williams01bec772011-05-01 16:51:11 -0700272 struct sci_base_state_machine *sm = &sci_dev->state_machine;
273 enum scic_sds_remote_device_states state = sm->current_state_id;
274 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
275 enum sci_status status;
276
277 switch (state) {
278 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
279 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
280 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
281 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
282 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
283 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
284 default:
285 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
286 __func__, state);
287 /* Return the frame back to the controller */
288 scic_sds_controller_release_frame(scic, frame_index);
289 return SCI_FAILURE_INVALID_STATE;
290 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
291 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
292 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
293 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
294 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
295 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING: {
296 struct scic_sds_request *sci_req;
Dave Jiang2d9c2242011-05-04 18:45:05 -0700297 struct ssp_frame_hdr hdr;
298 void *frame_header;
299 ssize_t word_cnt;
Dan Williams01bec772011-05-01 16:51:11 -0700300
301 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
302 frame_index,
Dave Jiang2d9c2242011-05-04 18:45:05 -0700303 &frame_header);
Dan Williams01bec772011-05-01 16:51:11 -0700304 if (status != SCI_SUCCESS)
305 return status;
306
Dave Jiang2d9c2242011-05-04 18:45:05 -0700307 word_cnt = sizeof(hdr) / sizeof(u32);
308 sci_swab32_cpy(&hdr, frame_header, word_cnt);
309
310 sci_req = scic_request_by_tag(scic, be16_to_cpu(hdr.tag));
Dan Williams01bec772011-05-01 16:51:11 -0700311 if (sci_req && sci_req->target_device == sci_dev) {
312 /* The IO request is now in charge of releasing the frame */
Dan Williamsd1c637c32011-05-11 08:27:47 -0700313 status = scic_sds_io_request_frame_handler(sci_req, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700314 } else {
315 /* We could not map this tag to a valid IO
316 * request Just toss the frame and continue
317 */
318 scic_sds_controller_release_frame(scic, frame_index);
319 }
320 break;
321 }
322 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700323 struct dev_to_host_fis *hdr;
Dan Williams01bec772011-05-01 16:51:11 -0700324
325 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
326 frame_index,
327 (void **)&hdr);
328 if (status != SCI_SUCCESS)
329 return status;
330
Dave Jiange76d6182011-05-04 15:02:03 -0700331 if (hdr->fis_type == FIS_SETDEVBITS &&
332 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700333 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
334
335 /* TODO Check sactive and complete associated IO if any. */
336 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
Dave Jiange76d6182011-05-04 15:02:03 -0700337 } else if (hdr->fis_type == FIS_REGD2H &&
338 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700339 /*
340 * Some devices return D2H FIS when an NCQ error is detected.
341 * Treat this like an SDB error FIS ready reason.
342 */
343 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
344 sci_base_state_machine_change_state(&sci_dev->state_machine,
345 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
346 } else
347 status = SCI_FAILURE;
348
349 scic_sds_controller_release_frame(scic, frame_index);
350 break;
351 }
352 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
353 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
354 /* The device does not process any UF received from the hardware while
355 * in this state. All unsolicited frames are forwarded to the io request
356 * object.
357 */
358 status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
359 break;
360 }
361
362 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700363}
364
Dan Williamse6225712011-05-01 16:26:09 -0700365static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700366{
Dan Williamse6225712011-05-01 16:26:09 -0700367
368 struct sci_base_state_machine *sm = &sci_dev->state_machine;
369 enum scic_sds_remote_device_states state = sm->current_state_id;
370
371 switch (state) {
372 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
373 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
374 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
375 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
376 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
377 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
378 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
379 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
380 return true;
381 default:
382 return false;
383 }
384}
385
386enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
387 u32 event_code)
388{
389 struct sci_base_state_machine *sm = &sci_dev->state_machine;
390 enum scic_sds_remote_device_states state = sm->current_state_id;
391 enum sci_status status;
392
393 switch (scu_get_event_type(event_code)) {
394 case SCU_EVENT_TYPE_RNC_OPS_MISC:
395 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
396 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
397 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
398 break;
399 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
400 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
401 status = SCI_SUCCESS;
402
403 /* Suspend the associated RNC */
404 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
405 SCI_SOFTWARE_SUSPENSION,
406 NULL, NULL);
407
408 dev_dbg(scirdev_to_dev(sci_dev),
409 "%s: device: %p event code: %x: %s\n",
410 __func__, sci_dev, event_code,
411 is_remote_device_ready(sci_dev)
412 ? "I_T_Nexus_Timeout event"
413 : "I_T_Nexus_Timeout event in wrong state");
414
415 break;
416 }
417 /* Else, fall through and treat as unhandled... */
418 default:
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 ? "unexpected event"
424 : "unexpected event in wrong state");
425 status = SCI_FAILURE_INVALID_STATE;
426 break;
427 }
428
429 if (status != SCI_SUCCESS)
430 return status;
431
432 if (state == SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE) {
433
434 /* We pick up suspension events to handle specifically to this
435 * state. We resume the RNC right away.
436 */
437 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
438 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
439 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
440 }
441
442 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700443}
444
Dan Williams18606552011-05-01 14:57:11 -0700445static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
446 struct scic_sds_request *sci_req,
447 enum sci_status status)
Dan Williams88f3b622011-04-22 19:18:03 -0700448{
Dan Williams18606552011-05-01 14:57:11 -0700449 struct scic_sds_port *sci_port = sci_dev->owning_port;
450
451 /* cleanup requests that failed after starting on the port */
452 if (status != SCI_SUCCESS)
453 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
454 else
455 scic_sds_remote_device_increment_request_count(sci_dev);
456}
457
458enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
459 struct scic_sds_remote_device *sci_dev,
460 struct scic_sds_request *sci_req)
461{
462 struct sci_base_state_machine *sm = &sci_dev->state_machine;
463 enum scic_sds_remote_device_states state = sm->current_state_id;
464 struct scic_sds_port *sci_port = sci_dev->owning_port;
Dan Williams67ea8382011-05-08 11:47:15 -0700465 struct isci_request *ireq = sci_req_to_ireq(sci_req);
Dan Williams18606552011-05-01 14:57:11 -0700466 enum sci_status status;
467
468 switch (state) {
469 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
470 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
471 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
472 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
473 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
474 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
475 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
476 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
477 default:
478 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
479 __func__, state);
480 return SCI_FAILURE_INVALID_STATE;
481 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
482 /* attempt to start an io request for this device object. The remote
483 * device object will issue the start request for the io and if
484 * successful it will start the request for the port object then
485 * increment its own request count.
486 */
487 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
488 if (status != SCI_SUCCESS)
489 return status;
490
491 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
492 if (status != SCI_SUCCESS)
493 break;
494
495 status = scic_sds_request_start(sci_req);
496 break;
497 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
498 /* handle the start io operation for a sata device that is in
499 * the command idle state. - Evalute the type of IO request to
500 * be started - If its an NCQ request change to NCQ substate -
501 * If its any other command change to the CMD substate
502 *
503 * If this is a softreset we may want to have a different
504 * substate.
505 */
506 enum scic_sds_remote_device_states new_state;
Dave Jiange76d6182011-05-04 15:02:03 -0700507 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700508
509 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
510 if (status != SCI_SUCCESS)
511 return status;
512
513 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
514 if (status != SCI_SUCCESS)
515 break;
516
Piotr Sawickif4636a72011-05-10 23:50:32 +0000517 status = scic_sds_request_start(sci_req);
Dan Williams18606552011-05-01 14:57:11 -0700518 if (status != SCI_SUCCESS)
519 break;
520
Dave Jiange76d6182011-05-04 15:02:03 -0700521 if (task->ata_task.use_ncq)
Dan Williams18606552011-05-01 14:57:11 -0700522 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
523 else {
524 sci_dev->working_request = sci_req;
525 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
526 }
527 sci_base_state_machine_change_state(sm, new_state);
528 break;
529 }
Dave Jiange76d6182011-05-04 15:02:03 -0700530 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
531 struct sas_task *task = isci_request_access_task(ireq);
532
533 if (task->ata_task.use_ncq) {
Dan Williams18606552011-05-01 14:57:11 -0700534 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
535 if (status != SCI_SUCCESS)
536 return status;
537
538 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
539 if (status != SCI_SUCCESS)
540 break;
541
Piotr Sawickif4636a72011-05-10 23:50:32 +0000542 status = scic_sds_request_start(sci_req);
Dan Williams18606552011-05-01 14:57:11 -0700543 } else
544 return SCI_FAILURE_INVALID_STATE;
545 break;
Dave Jiange76d6182011-05-04 15:02:03 -0700546 }
Dan Williams18606552011-05-01 14:57:11 -0700547 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
548 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
549 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
550 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
551 if (status != SCI_SUCCESS)
552 return status;
553
554 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
555 if (status != SCI_SUCCESS)
556 break;
557
558 status = scic_sds_request_start(sci_req);
559 if (status != SCI_SUCCESS)
560 break;
561
562 sci_dev->working_request = sci_req;
563 sci_base_state_machine_change_state(&sci_dev->state_machine,
564 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
565 break;
566 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
567 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
568 /* device is already handling a command it can not accept new commands
569 * until this one is complete.
570 */
571 return SCI_FAILURE_INVALID_STATE;
572 }
573
574 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
575 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700576}
577
Dan Williams10a09e62011-05-01 15:33:43 -0700578static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
579 struct scic_sds_remote_device *sci_dev,
580 struct scic_sds_request *sci_req)
Dan Williams88f3b622011-04-22 19:18:03 -0700581{
Dan Williams10a09e62011-05-01 15:33:43 -0700582 enum sci_status status;
583
584 status = scic_sds_request_complete(sci_req);
585 if (status != SCI_SUCCESS)
586 return status;
587
588 status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
589 if (status != SCI_SUCCESS)
590 return status;
591
592 scic_sds_remote_device_decrement_request_count(sci_dev);
593 return status;
594}
595
596enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
597 struct scic_sds_remote_device *sci_dev,
598 struct scic_sds_request *sci_req)
599{
600 struct sci_base_state_machine *sm = &sci_dev->state_machine;
601 enum scic_sds_remote_device_states state = sm->current_state_id;
602 struct scic_sds_port *sci_port = sci_dev->owning_port;
603 enum sci_status status;
604
605 switch (state) {
606 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
607 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
608 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
609 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
610 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
611 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
612 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
613 default:
614 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
615 __func__, state);
616 return SCI_FAILURE_INVALID_STATE;
617 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
618 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
619 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
620 status = common_complete_io(sci_port, sci_dev, sci_req);
621 break;
622 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
623 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
624 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
625 status = common_complete_io(sci_port, sci_dev, sci_req);
626 if (status != SCI_SUCCESS)
627 break;
628
629 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
630 /* This request causes hardware error, device needs to be Lun Reset.
631 * So here we force the state machine to IDLE state so the rest IOs
632 * can reach RNC state handler, these IOs will be completed by RNC with
633 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
634 */
635 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
636 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
637 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
638 break;
639 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
640 status = common_complete_io(sci_port, sci_dev, sci_req);
641 if (status != SCI_SUCCESS)
642 break;
643 sci_base_state_machine_change_state(sm, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
644 break;
645 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
646 status = common_complete_io(sci_port, sci_dev, sci_req);
647 if (status != SCI_SUCCESS)
648 break;
649
650 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
651 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
652 rnc_destruct_done,
653 sci_dev);
654 break;
655 }
656
657 if (status != SCI_SUCCESS)
658 dev_err(scirdev_to_dev(sci_dev),
659 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
660 "could not complete\n", __func__, sci_port,
661 sci_dev, sci_req, status);
662
663 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700664}
665
Dan Williams84b9b022011-05-01 15:53:25 -0700666static void scic_sds_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700667{
Dan Williams84b9b022011-05-01 15:53:25 -0700668 struct scic_sds_remote_device *sci_dev = dev;
669
670 /* we need to check if this request is still valid to continue. */
671 if (sci_dev->working_request)
672 scic_controller_continue_io(sci_dev->working_request);
673}
674
675enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
676 struct scic_sds_remote_device *sci_dev,
677 struct scic_sds_request *sci_req)
678{
679 struct sci_base_state_machine *sm = &sci_dev->state_machine;
680 enum scic_sds_remote_device_states state = sm->current_state_id;
681 struct scic_sds_port *sci_port = sci_dev->owning_port;
682 enum sci_status status;
683
684 switch (state) {
685 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
686 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
687 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
688 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
689 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
690 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
691 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
692 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
693 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
694 default:
695 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
696 __func__, state);
697 return SCI_FAILURE_INVALID_STATE;
698 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
699 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
700 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
701 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
702 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
703 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
704 if (status != SCI_SUCCESS)
705 return status;
706
707 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
708 if (status != SCI_SUCCESS)
709 goto out;
710
Piotr Sawickif4636a72011-05-10 23:50:32 +0000711 status = scic_sds_request_start(sci_req);
Dan Williams84b9b022011-05-01 15:53:25 -0700712 if (status != SCI_SUCCESS)
713 goto out;
714
715 /* Note: If the remote device state is not IDLE this will
716 * replace the request that probably resulted in the task
717 * management request.
718 */
719 sci_dev->working_request = sci_req;
720 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
721
722 /* The remote node context must cleanup the TCi to NCQ mapping
723 * table. The only way to do this correctly is to either write
724 * to the TLCR register or to invalidate and repost the RNC. In
725 * either case the remote node context state machine will take
726 * the correct action when the remote node context is suspended
727 * and later resumed.
728 */
729 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
730 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
731 scic_sds_remote_node_context_resume(&sci_dev->rnc,
732 scic_sds_remote_device_continue_request,
733 sci_dev);
734
735 out:
736 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
737 /* We need to let the controller start request handler know that
738 * it can't post TC yet. We will provide a callback function to
739 * post TC when RNC gets resumed.
740 */
741 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
742 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
743 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
744 if (status != SCI_SUCCESS)
745 return status;
746
747 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
748 if (status != SCI_SUCCESS)
749 break;
750
751 status = scic_sds_request_start(sci_req);
752 break;
753 }
754 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
755
756 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700757}
758
759/**
760 *
Dan Williams88f3b622011-04-22 19:18:03 -0700761 * @sci_dev:
762 * @request:
763 *
764 * This method takes the request and bulids an appropriate SCU context for the
765 * request and then requests the controller to post the request. none
766 */
767void scic_sds_remote_device_post_request(
768 struct scic_sds_remote_device *sci_dev,
769 u32 request)
770{
771 u32 context;
772
773 context = scic_sds_remote_device_build_command_context(sci_dev, request);
774
775 scic_sds_controller_post_request(
776 scic_sds_remote_device_get_controller(sci_dev),
777 context
778 );
779}
780
Dan Williamsab2e8f72011-04-27 16:32:45 -0700781/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700782 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700783 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700784 */
Dan Williamseb229672011-05-01 14:05:57 -0700785static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700786{
Dan Williamsab2e8f72011-04-27 16:32:45 -0700787 struct scic_sds_remote_device *sci_dev = _dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700788
Dan Williamse6225712011-05-01 16:26:09 -0700789 if (is_remote_device_ready(sci_dev))
790 return;
Dan Williams88f3b622011-04-22 19:18:03 -0700791
Dan Williamse6225712011-05-01 16:26:09 -0700792 /* go 'ready' if we are not already in a ready state */
793 sci_base_state_machine_change_state(&sci_dev->state_machine,
794 SCI_BASE_REMOTE_DEVICE_STATE_READY);
Dan Williams88f3b622011-04-22 19:18:03 -0700795}
796
Dan Williamsab2e8f72011-04-27 16:32:45 -0700797static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
798{
799 struct scic_sds_remote_device *sci_dev = _dev;
800 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
801 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
802
803 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
804 * As a result, avoid sending the ready notification.
805 */
806 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000807 isci_remote_device_ready(scic_to_ihost(scic), idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700808}
809
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000810static void scic_sds_remote_device_initial_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700811{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000812 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700813
Dan Williams88f3b622011-04-22 19:18:03 -0700814 /* Initial state is a transitional state to the stopped state */
815 sci_base_state_machine_change_state(&sci_dev->state_machine,
816 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
817}
818
819/**
Dan Williams88f3b622011-04-22 19:18:03 -0700820 * scic_remote_device_destruct() - free remote node context and destruct
821 * @remote_device: This parameter specifies the remote device to be destructed.
822 *
823 * Remote device objects are a limited resource. As such, they must be
824 * protected. Thus calls to construct and destruct are mutually exclusive and
825 * non-reentrant. The return value shall indicate if the device was
826 * successfully destructed or if some failure occurred. enum sci_status This value
827 * is returned if the device is successfully destructed.
828 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
829 * device isn't valid (e.g. it's already been destoryed, the handle isn't
830 * valid, etc.).
831 */
832static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
833{
Dan Williamsb8d82f62011-05-01 14:38:26 -0700834 struct sci_base_state_machine *sm = &sci_dev->state_machine;
835 enum scic_sds_remote_device_states state = sm->current_state_id;
836 struct scic_sds_controller *scic;
837
838 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
839 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
840 __func__, state);
841 return SCI_FAILURE_INVALID_STATE;
842 }
843
844 scic = sci_dev->owning_port->owning_controller;
845 scic_sds_controller_free_remote_node_context(scic, sci_dev,
846 sci_dev->rnc.remote_node_index);
847 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
848 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
849
850 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -0700851}
852
Dan Williams6f231dd2011-07-02 22:56:22 -0700853/**
854 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -0800855 * @ihost: This parameter specifies the isci host object.
856 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -0700857 *
858 */
Dan Williamsd9c37392011-03-03 17:59:32 -0800859static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700860{
Dan Williamsd9c37392011-03-03 17:59:32 -0800861 dev_dbg(&ihost->pdev->dev,
862 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700863
864 /* There should not be any outstanding io's. All paths to
865 * here should go through isci_remote_device_nuke_requests.
866 * If we hit this condition, we will need a way to complete
867 * io requests in process */
Dan Williamsd9c37392011-03-03 17:59:32 -0800868 while (!list_empty(&idev->reqs_in_process)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700869
Dan Williamsd9c37392011-03-03 17:59:32 -0800870 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700871 "%s: ** request list not empty! **\n", __func__);
872 BUG();
873 }
874
Dan Williams57f20f42011-04-21 18:14:45 -0700875 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -0800876 idev->domain_dev->lldd_dev = NULL;
877 idev->domain_dev = NULL;
878 idev->isci_port = NULL;
879 list_del_init(&idev->node);
Dan Williams6ad31fe2011-03-04 12:10:29 -0800880
Dan Williamsd9c37392011-03-03 17:59:32 -0800881 clear_bit(IDEV_START_PENDING, &idev->flags);
882 clear_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williamsd06b4872011-05-02 13:59:25 -0700883 clear_bit(IDEV_EH, &idev->flags);
Dan Williamsd9c37392011-03-03 17:59:32 -0800884 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700885}
886
Dan Williams88f3b622011-04-22 19:18:03 -0700887/**
888 * isci_remote_device_stop_complete() - This function is called by the scic
889 * when the remote device stop has completed. We mark the isci device as not
890 * ready and remove the isci remote device.
891 * @ihost: This parameter specifies the isci host object.
892 * @idev: This parameter specifies the remote device.
893 * @status: This parameter specifies status of the completion.
894 *
895 */
896static void isci_remote_device_stop_complete(struct isci_host *ihost,
897 struct isci_remote_device *idev)
898{
899 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
900
901 isci_remote_device_change_state(idev, isci_stopped);
902
903 /* after stop, we can tear down resources. */
904 isci_remote_device_deconstruct(ihost, idev);
905}
906
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000907static void scic_sds_remote_device_stopped_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700908{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000909 struct scic_sds_remote_device *sci_dev = object;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000910 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
911 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700912 u32 prev_state;
913
Dan Williams88f3b622011-04-22 19:18:03 -0700914 /* If we are entering from the stopping state let the SCI User know that
915 * the stop operation has completed.
916 */
917 prev_state = sci_dev->state_machine.previous_state_id;
918 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000919 isci_remote_device_stop_complete(scic_to_ihost(scic), idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700920
921 scic_sds_controller_remote_device_stopped(scic, sci_dev);
922}
923
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000924static void scic_sds_remote_device_starting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700925{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000926 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700927 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000928 struct isci_host *ihost = scic_to_ihost(scic);
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000929 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700930
Dan Williams88f3b622011-04-22 19:18:03 -0700931 isci_remote_device_not_ready(ihost, idev,
932 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
933}
934
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000935static void scic_sds_remote_device_ready_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700936{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000937 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700938 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000939 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
940 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700941
Dan Williams88f3b622011-04-22 19:18:03 -0700942 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
943
Dan Williamsab2e8f72011-04-27 16:32:45 -0700944 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
945 sci_base_state_machine_change_state(&sci_dev->state_machine,
946 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
947 } else if (dev_is_expander(dev)) {
948 sci_base_state_machine_change_state(&sci_dev->state_machine,
949 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
950 } else
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000951 isci_remote_device_ready(scic_to_ihost(scic), idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700952}
953
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000954static void scic_sds_remote_device_ready_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700955{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000956 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700957 struct domain_device *dev = sci_dev_to_domain(sci_dev);
958
959 if (dev->dev_type == SAS_END_DEV) {
960 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000961 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700962
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000963 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
Dan Williams88f3b622011-04-22 19:18:03 -0700964 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
965 }
966}
967
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000968static void scic_sds_remote_device_resetting_state_enter(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 Williams88f3b622011-04-22 19:18:03 -0700971
Dan Williams88f3b622011-04-22 19:18:03 -0700972 scic_sds_remote_node_context_suspend(
973 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
974}
975
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000976static void scic_sds_remote_device_resetting_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700977{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000978 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700979
980 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
981}
982
Dan Williamsab2e8f72011-04-27 16:32:45 -0700983static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
984{
985 struct scic_sds_remote_device *sci_dev = object;
986
Dan Williamsab2e8f72011-04-27 16:32:45 -0700987 sci_dev->working_request = NULL;
988 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
989 /*
990 * Since the RNC is ready, it's alright to finish completion
991 * processing (e.g. signal the remote device is ready). */
992 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
993 } else {
994 scic_sds_remote_node_context_resume(&sci_dev->rnc,
995 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
996 sci_dev);
997 }
998}
999
1000static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1001{
1002 struct scic_sds_remote_device *sci_dev = object;
1003 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1004
1005 BUG_ON(sci_dev->working_request == NULL);
1006
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001007 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
Dan Williamsab2e8f72011-04-27 16:32:45 -07001008 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1009}
1010
Dan Williamsab2e8f72011-04-27 16:32:45 -07001011static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1012{
1013 struct scic_sds_remote_device *sci_dev = object;
1014 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1015 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1016
Dan Williamsab2e8f72011-04-27 16:32:45 -07001017 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001018 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001019 sci_dev->not_ready_reason);
1020}
1021
Dan Williamsab2e8f72011-04-27 16:32:45 -07001022static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1023{
1024 struct scic_sds_remote_device *sci_dev = object;
1025 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1026
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001027 isci_remote_device_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev));
Dan Williamsab2e8f72011-04-27 16:32:45 -07001028}
1029
1030static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1031{
1032 struct scic_sds_remote_device *sci_dev = object;
1033 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1034
1035 BUG_ON(sci_dev->working_request == NULL);
1036
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001037 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
Dan Williamsab2e8f72011-04-27 16:32:45 -07001038 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1039}
1040
1041static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1042{
1043 struct scic_sds_remote_device *sci_dev = object;
1044
1045 sci_dev->working_request = NULL;
1046}
Dan Williams88f3b622011-04-22 19:18:03 -07001047
1048static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1049 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1050 .enter_state = scic_sds_remote_device_initial_state_enter,
1051 },
1052 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1053 .enter_state = scic_sds_remote_device_stopped_state_enter,
1054 },
1055 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1056 .enter_state = scic_sds_remote_device_starting_state_enter,
1057 },
1058 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1059 .enter_state = scic_sds_remote_device_ready_state_enter,
1060 .exit_state = scic_sds_remote_device_ready_state_exit
1061 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001062 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1063 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1064 },
1065 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1066 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1067 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001068 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = { },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001069 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1070 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1071 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001072 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = { },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001073 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1074 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1075 },
1076 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1077 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1078 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1079 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001080 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = { },
1081 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001082 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1083 .enter_state = scic_sds_remote_device_resetting_state_enter,
1084 .exit_state = scic_sds_remote_device_resetting_state_exit
1085 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001086 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001087};
1088
1089/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001090 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001091 * @sci_port: SAS/SATA port through which this device is accessed.
1092 * @sci_dev: remote device to construct
1093 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001094 * This routine just performs benign initialization and does not
1095 * allocate the remote_node_context which is left to
1096 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1097 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001098 */
1099static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1100 struct scic_sds_remote_device *sci_dev)
1101{
1102 sci_dev->owning_port = sci_port;
1103 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001104
1105 sci_base_state_machine_construct(
1106 &sci_dev->state_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001107 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001108 scic_sds_remote_device_state_table,
1109 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1110 );
1111
1112 sci_base_state_machine_start(
1113 &sci_dev->state_machine
1114 );
1115
1116 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1117 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001118}
1119
1120/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001121 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001122 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001123 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1124 * the device is known to the SCI Core since it is contained in the
1125 * scic_phy object. Remote node context(s) is/are a global resource
1126 * allocated by this routine, freed by scic_remote_device_destruct().
1127 *
1128 * Returns:
1129 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1130 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1131 * sata-only controller instance.
1132 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001133 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001134static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1135 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001136{
1137 enum sci_status status;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001138 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001139
Dan Williamsb87ee302011-04-25 11:48:29 -07001140 scic_remote_device_construct(sci_port, sci_dev);
1141
Dan Williams88f3b622011-04-22 19:18:03 -07001142 /*
1143 * This information is request to determine how many remote node context
1144 * entries will be needed to store the remote node.
1145 */
Dan Williams88f3b622011-04-22 19:18:03 -07001146 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001147 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1148 sci_dev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001149 &sci_dev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001150
Dan Williamsa1a113b2011-04-21 18:44:45 -07001151 if (status != SCI_SUCCESS)
1152 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001153
Dan Williamsab2e8f72011-04-27 16:32:45 -07001154 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1155 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1156 /* pass */;
1157 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001158 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001159
Dan Williamsa1a113b2011-04-21 18:44:45 -07001160 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001161
Dan Williamsa1a113b2011-04-21 18:44:45 -07001162 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1163 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001164
Dan Williamsa1a113b2011-04-21 18:44:45 -07001165 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001166}
1167
Dan Williams88f3b622011-04-22 19:18:03 -07001168/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001169 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001170 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001171 * Remote node context(s) is/are a global resource allocated by this
1172 * routine, freed by scic_remote_device_destruct().
1173 *
1174 * Returns:
1175 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1176 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1177 * sata-only controller instance.
1178 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001179 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001180static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001181 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001182{
Dan Williamsa1a113b2011-04-21 18:44:45 -07001183 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001184 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001185
Dan Williamsb87ee302011-04-25 11:48:29 -07001186 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001187
Dan Williamsab2e8f72011-04-27 16:32:45 -07001188 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1189 sci_dev,
1190 &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001191 if (status != SCI_SUCCESS)
1192 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001193
Dan Williamsab2e8f72011-04-27 16:32:45 -07001194 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1195 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1196 /* pass */;
1197 else
1198 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001199
Dan Williamsa1a113b2011-04-21 18:44:45 -07001200 /*
1201 * For SAS-2 the physical link rate is actually a logical link
1202 * rate that incorporates multiplexing. The SCU doesn't
1203 * incorporate multiplexing and for the purposes of the
1204 * connection the logical link rate is that same as the
1205 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1206 * one another, so this code works for both situations. */
1207 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001208 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001209
Dan Williamsa1a113b2011-04-21 18:44:45 -07001210 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1211 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001212
Dan Williamsab2e8f72011-04-27 16:32:45 -07001213 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001214}
1215
1216/**
1217 * scic_remote_device_start() - This method will start the supplied remote
1218 * device. This method enables normal IO requests to flow through to the
1219 * remote device.
1220 * @remote_device: This parameter specifies the device to be started.
1221 * @timeout: This parameter specifies the number of milliseconds in which the
1222 * start operation should complete.
1223 *
1224 * An indication of whether the device was successfully started. SCI_SUCCESS
1225 * This value is returned if the device was successfully started.
1226 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1227 * the device when there have been no phys added to it.
1228 */
1229static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
Dan Williamseb229672011-05-01 14:05:57 -07001230 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001231{
Dan Williamseb229672011-05-01 14:05:57 -07001232 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1233 enum scic_sds_remote_device_states state = sm->current_state_id;
1234 enum sci_status status;
1235
1236 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1237 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1238 __func__, state);
1239 return SCI_FAILURE_INVALID_STATE;
1240 }
1241
1242 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1243 remote_device_resume_done,
1244 sci_dev);
1245 if (status != SCI_SUCCESS)
1246 return status;
1247
1248 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1249
1250 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001251}
Dan Williams6f231dd2011-07-02 22:56:22 -07001252
Dan Williams00d680e2011-04-25 14:29:29 -07001253static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1254 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001255{
Dan Williamse5313812011-05-07 10:11:43 -07001256 struct scic_sds_port *sci_port = &iport->sci;
Dan Williams00d680e2011-04-25 14:29:29 -07001257 struct isci_host *ihost = iport->isci_host;
1258 struct domain_device *dev = idev->domain_dev;
1259 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001260
Dan Williams00d680e2011-04-25 14:29:29 -07001261 if (dev->parent && dev_is_expander(dev->parent))
1262 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1263 else
1264 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001265
1266 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001267 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1268 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001269
1270 return status;
1271 }
1272
Dan Williams6f231dd2011-07-02 22:56:22 -07001273 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001274 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001275
Dan Williams00d680e2011-04-25 14:29:29 -07001276 if (status != SCI_SUCCESS)
1277 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1278 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001279
1280 return status;
1281}
1282
Dan Williams4393aa42011-03-31 13:10:44 -07001283void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001284{
1285 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001286
Dan Williams4393aa42011-03-31 13:10:44 -07001287 dev_dbg(&ihost->pdev->dev,
1288 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001289
1290 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -07001291 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -07001292
Dan Williams4393aa42011-03-31 13:10:44 -07001293 dev_dbg(&ihost->pdev->dev,
1294 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001295}
1296
Dan Williams6f231dd2011-07-02 22:56:22 -07001297/**
1298 * This function builds the isci_remote_device when a libsas dev_found message
1299 * is received.
1300 * @isci_host: This parameter specifies the isci host object.
1301 * @port: This parameter specifies the isci_port conected to this device.
1302 *
1303 * pointer to new isci_remote_device.
1304 */
1305static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001306isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001307{
Dan Williamsd9c37392011-03-03 17:59:32 -08001308 struct isci_remote_device *idev;
1309 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001310
Dan Williamsd9c37392011-03-03 17:59:32 -08001311 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001312 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001313 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1314 break;
1315 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001316
Dan Williamsd9c37392011-03-03 17:59:32 -08001317 if (i >= SCI_MAX_REMOTE_DEVICES) {
1318 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001319 return NULL;
1320 }
1321
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001322 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1323 return NULL;
1324
1325 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1326 return NULL;
1327
Dan Williamsd9c37392011-03-03 17:59:32 -08001328 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001329
Dan Williamsd9c37392011-03-03 17:59:32 -08001330 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001331}
Dan Williams6f231dd2011-07-02 22:56:22 -07001332
1333/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001334 * isci_remote_device_stop() - This function is called internally to stop the
1335 * remote device.
1336 * @isci_host: This parameter specifies the isci host object.
1337 * @isci_device: This parameter specifies the remote device.
1338 *
1339 * The status of the scic request to stop.
1340 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001341enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001342{
1343 enum sci_status status;
1344 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001345
Dan Williams6ad31fe2011-03-04 12:10:29 -08001346 dev_dbg(&ihost->pdev->dev,
1347 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001348
Dan Williams6ad31fe2011-03-04 12:10:29 -08001349 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001350
1351 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001352 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001353
Dan Williams6ad31fe2011-03-04 12:10:29 -08001354 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001355
Dan Williams6ad31fe2011-03-04 12:10:29 -08001356 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07001357 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001358 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001359
1360 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -08001361 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -08001362 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08001363 clear_bit(IDEV_ALLOCATED, &idev->flags);
1364 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001365
Dan Williams6ad31fe2011-03-04 12:10:29 -08001366 dev_dbg(&ihost->pdev->dev,
1367 "%s: idev = %p - after completion wait\n",
1368 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001369
Dan Williams6f231dd2011-07-02 22:56:22 -07001370 return status;
1371}
1372
1373/**
1374 * isci_remote_device_gone() - This function is called by libsas when a domain
1375 * device is removed.
1376 * @domain_device: This parameter specifies the libsas domain device.
1377 *
1378 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001379void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001380{
Dan Williams4393aa42011-03-31 13:10:44 -07001381 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001382 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001383
Dan Williams6ad31fe2011-03-04 12:10:29 -08001384 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001385 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001386 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001387
Dan Williams6ad31fe2011-03-04 12:10:29 -08001388 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001389}
1390
1391
1392/**
1393 * isci_remote_device_found() - This function is called by libsas when a remote
1394 * device is discovered. A remote device object is created and started. the
1395 * function then sleeps until the sci core device started message is
1396 * received.
1397 * @domain_device: This parameter specifies the libsas domain device.
1398 *
1399 * status, zero indicates success.
1400 */
1401int isci_remote_device_found(struct domain_device *domain_dev)
1402{
Dan Williams4393aa42011-03-31 13:10:44 -07001403 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001404 struct isci_port *isci_port;
1405 struct isci_phy *isci_phy;
1406 struct asd_sas_port *sas_port;
1407 struct asd_sas_phy *sas_phy;
1408 struct isci_remote_device *isci_device;
1409 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001410
Dan Williams6f231dd2011-07-02 22:56:22 -07001411 dev_dbg(&isci_host->pdev->dev,
1412 "%s: domain_device = %p\n", __func__, domain_dev);
1413
Dan Williams0cf89d12011-02-18 09:25:07 -08001414 wait_for_start(isci_host);
1415
Dan Williams6f231dd2011-07-02 22:56:22 -07001416 sas_port = domain_dev->port;
1417 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1418 port_phy_el);
1419 isci_phy = to_isci_phy(sas_phy);
1420 isci_port = isci_phy->isci_port;
1421
1422 /* we are being called for a device on this port,
1423 * so it has to come up eventually
1424 */
1425 wait_for_completion(&isci_port->start_complete);
1426
1427 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1428 (isci_stopped == isci_port_get_state(isci_port)))
1429 return -ENODEV;
1430
1431 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001432 if (!isci_device)
1433 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001434
1435 INIT_LIST_HEAD(&isci_device->node);
1436 domain_dev->lldd_dev = isci_device;
1437 isci_device->domain_dev = domain_dev;
1438 isci_device->isci_port = isci_port;
1439 isci_remote_device_change_state(isci_device, isci_starting);
1440
1441
Dan Williams1a380452011-03-03 18:01:43 -08001442 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001443 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1444
Dan Williams6ad31fe2011-03-04 12:10:29 -08001445 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001446 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -08001447 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001448
Dan Williams6f231dd2011-07-02 22:56:22 -07001449 dev_dbg(&isci_host->pdev->dev,
1450 "%s: isci_device = %p\n",
1451 __func__, isci_device);
1452
1453 if (status != SCI_SUCCESS) {
1454
Dan Williams1a380452011-03-03 18:01:43 -08001455 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001456 isci_remote_device_deconstruct(
1457 isci_host,
1458 isci_device
1459 );
Dan Williams1a380452011-03-03 18:01:43 -08001460 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001461 return -ENODEV;
1462 }
1463
Dan Williams6ad31fe2011-03-04 12:10:29 -08001464 /* wait for the device ready callback. */
1465 wait_for_device_start(isci_host, isci_device);
1466
Dan Williams6f231dd2011-07-02 22:56:22 -07001467 return 0;
1468}
1469/**
1470 * isci_device_is_reset_pending() - This function will check if there is any
1471 * pending reset condition on the device.
1472 * @request: This parameter is the isci_device object.
1473 *
1474 * true if there is a reset pending for the device.
1475 */
1476bool isci_device_is_reset_pending(
1477 struct isci_host *isci_host,
1478 struct isci_remote_device *isci_device)
1479{
1480 struct isci_request *isci_request;
1481 struct isci_request *tmp_req;
1482 bool reset_is_pending = false;
1483 unsigned long flags;
1484
1485 dev_dbg(&isci_host->pdev->dev,
1486 "%s: isci_device = %p\n", __func__, isci_device);
1487
1488 spin_lock_irqsave(&isci_host->scic_lock, flags);
1489
1490 /* Check for reset on all pending requests. */
1491 list_for_each_entry_safe(isci_request, tmp_req,
1492 &isci_device->reqs_in_process, dev_node) {
1493 dev_dbg(&isci_host->pdev->dev,
1494 "%s: isci_device = %p request = %p\n",
1495 __func__, isci_device, isci_request);
1496
1497 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001498 struct sas_task *task = isci_request_access_task(
1499 isci_request);
1500
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001501 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001502 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1503 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001504 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001505 }
1506 }
1507
1508 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1509
1510 dev_dbg(&isci_host->pdev->dev,
1511 "%s: isci_device = %p reset_is_pending = %d\n",
1512 __func__, isci_device, reset_is_pending);
1513
1514 return reset_is_pending;
1515}
1516
1517/**
1518 * isci_device_clear_reset_pending() - This function will clear if any pending
1519 * reset condition flags on the device.
1520 * @request: This parameter is the isci_device object.
1521 *
1522 * true if there is a reset pending for the device.
1523 */
Dan Williams4393aa42011-03-31 13:10:44 -07001524void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001525{
1526 struct isci_request *isci_request;
1527 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07001528 unsigned long flags = 0;
1529
Dan Williams4393aa42011-03-31 13:10:44 -07001530 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1531 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07001532
Dan Williams4393aa42011-03-31 13:10:44 -07001533 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001534
1535 /* Clear reset pending on all pending requests. */
1536 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07001537 &idev->reqs_in_process, dev_node) {
1538 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1539 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001540
1541 if (isci_request->ttype == io_task) {
1542
1543 unsigned long flags2;
1544 struct sas_task *task = isci_request_access_task(
1545 isci_request);
1546
1547 spin_lock_irqsave(&task->task_state_lock, flags2);
1548 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1549 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1550 }
1551 }
Dan Williams4393aa42011-03-31 13:10:44 -07001552 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001553}