blob: 6c93f20f3dd3ffe9c58388268642c9f7f0700ead [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);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000129 sci_change_state(&sci_dev->sm, SCI_DEV_STOPPED);
Dan Williamsec575662011-05-01 14:19:25 -0700130}
131
132static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
133{
134 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
135 u32 i, request_count = sci_dev->started_request_count;
136 enum sci_status status = SCI_SUCCESS;
137
138 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
139 struct scic_sds_request *sci_req;
140 enum sci_status s;
141
142 sci_req = scic->io_request_table[i];
143 if (!sci_req || sci_req->target_device != sci_dev)
144 continue;
145 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
146 if (s != SCI_SUCCESS)
147 status = s;
148 }
149
150 return status;
151}
152
153enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
154 u32 timeout)
155{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000156 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williamsec575662011-05-01 14:19:25 -0700157 enum scic_sds_remote_device_states state = sm->current_state_id;
158
159 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000160 case SCI_DEV_INITIAL:
161 case SCI_DEV_FAILED:
162 case SCI_DEV_FINAL:
Dan Williamsec575662011-05-01 14:19:25 -0700163 default:
164 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
165 __func__, state);
166 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000167 case SCI_DEV_STOPPED:
Dan Williamsec575662011-05-01 14:19:25 -0700168 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000169 case SCI_DEV_STARTING:
Dan Williamsec575662011-05-01 14:19:25 -0700170 /* device not started so there had better be no requests */
171 BUG_ON(sci_dev->started_request_count != 0);
172 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
173 rnc_destruct_done, sci_dev);
174 /* Transition to the stopping state and wait for the
175 * remote node to complete being posted and invalidated.
176 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000177 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williamsec575662011-05-01 14:19:25 -0700178 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000179 case SCI_DEV_READY:
180 case SCI_STP_DEV_IDLE:
181 case SCI_STP_DEV_CMD:
182 case SCI_STP_DEV_NCQ:
183 case SCI_STP_DEV_NCQ_ERROR:
184 case SCI_STP_DEV_AWAIT_RESET:
185 case SCI_SMP_DEV_IDLE:
186 case SCI_SMP_DEV_CMD:
187 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williamsec575662011-05-01 14:19:25 -0700188 if (sci_dev->started_request_count == 0) {
189 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
190 rnc_destruct_done, sci_dev);
191 return SCI_SUCCESS;
192 } else
193 return scic_sds_remote_device_terminate_requests(sci_dev);
194 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000195 case SCI_DEV_STOPPING:
Dan Williamsec575662011-05-01 14:19:25 -0700196 /* All requests should have been terminated, but if there is an
197 * attempt to stop a device already in the stopping state, then
198 * try again to terminate.
199 */
200 return scic_sds_remote_device_terminate_requests(sci_dev);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000201 case SCI_DEV_RESETTING:
202 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williamsec575662011-05-01 14:19:25 -0700203 return SCI_SUCCESS;
204 }
Dan Williams88f3b622011-04-22 19:18:03 -0700205}
Dan Williams6f231dd2011-07-02 22:56:22 -0700206
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700207enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700208{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000209 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700210 enum scic_sds_remote_device_states state = sm->current_state_id;
211
212 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000213 case SCI_DEV_INITIAL:
214 case SCI_DEV_STOPPED:
215 case SCI_DEV_STARTING:
216 case SCI_SMP_DEV_IDLE:
217 case SCI_SMP_DEV_CMD:
218 case SCI_DEV_STOPPING:
219 case SCI_DEV_FAILED:
220 case SCI_DEV_RESETTING:
221 case SCI_DEV_FINAL:
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700222 default:
223 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
224 __func__, state);
225 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000226 case SCI_DEV_READY:
227 case SCI_STP_DEV_IDLE:
228 case SCI_STP_DEV_CMD:
229 case SCI_STP_DEV_NCQ:
230 case SCI_STP_DEV_NCQ_ERROR:
231 case SCI_STP_DEV_AWAIT_RESET:
232 sci_change_state(sm, SCI_DEV_RESETTING);
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700233 return SCI_SUCCESS;
234 }
Dan Williams88f3b622011-04-22 19:18:03 -0700235}
236
Dan Williams81515182011-05-01 14:53:00 -0700237enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700238{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000239 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams81515182011-05-01 14:53:00 -0700240 enum scic_sds_remote_device_states state = sm->current_state_id;
Dan Williams88f3b622011-04-22 19:18:03 -0700241
Edmund Nadolskie3013702011-06-02 00:10:43 +0000242 if (state != SCI_DEV_RESETTING) {
Dan Williams81515182011-05-01 14:53:00 -0700243 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
244 __func__, state);
245 return SCI_FAILURE_INVALID_STATE;
246 }
247
Edmund Nadolskie3013702011-06-02 00:10:43 +0000248 sci_change_state(sm, SCI_DEV_READY);
Dan Williams81515182011-05-01 14:53:00 -0700249 return SCI_SUCCESS;
250}
Dan Williams88f3b622011-04-22 19:18:03 -0700251
Dan Williams323f0ec2011-05-01 16:15:47 -0700252enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
253 u32 suspend_type)
Dan Williams88f3b622011-04-22 19:18:03 -0700254{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000255 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams323f0ec2011-05-01 16:15:47 -0700256 enum scic_sds_remote_device_states state = sm->current_state_id;
257
Edmund Nadolskie3013702011-06-02 00:10:43 +0000258 if (state != SCI_STP_DEV_CMD) {
Dan Williams323f0ec2011-05-01 16:15:47 -0700259 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
260 __func__, state);
261 return SCI_FAILURE_INVALID_STATE;
262 }
263
264 return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
265 suspend_type, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700266}
267
Dan Williams01bec772011-05-01 16:51:11 -0700268enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
269 u32 frame_index)
Dan Williams88f3b622011-04-22 19:18:03 -0700270{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000271 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams01bec772011-05-01 16:51:11 -0700272 enum scic_sds_remote_device_states state = sm->current_state_id;
273 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
274 enum sci_status status;
275
276 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000277 case SCI_DEV_INITIAL:
278 case SCI_DEV_STOPPED:
279 case SCI_DEV_STARTING:
280 case SCI_STP_DEV_IDLE:
281 case SCI_SMP_DEV_IDLE:
282 case SCI_DEV_FINAL:
Dan Williams01bec772011-05-01 16:51:11 -0700283 default:
284 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
285 __func__, state);
286 /* Return the frame back to the controller */
287 scic_sds_controller_release_frame(scic, frame_index);
288 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000289 case SCI_DEV_READY:
290 case SCI_STP_DEV_NCQ_ERROR:
291 case SCI_STP_DEV_AWAIT_RESET:
292 case SCI_DEV_STOPPING:
293 case SCI_DEV_FAILED:
294 case SCI_DEV_RESETTING: {
Dan Williams01bec772011-05-01 16:51:11 -0700295 struct scic_sds_request *sci_req;
Dave Jiang2d9c2242011-05-04 18:45:05 -0700296 struct ssp_frame_hdr hdr;
297 void *frame_header;
298 ssize_t word_cnt;
Dan Williams01bec772011-05-01 16:51:11 -0700299
300 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
301 frame_index,
Dave Jiang2d9c2242011-05-04 18:45:05 -0700302 &frame_header);
Dan Williams01bec772011-05-01 16:51:11 -0700303 if (status != SCI_SUCCESS)
304 return status;
305
Dave Jiang2d9c2242011-05-04 18:45:05 -0700306 word_cnt = sizeof(hdr) / sizeof(u32);
307 sci_swab32_cpy(&hdr, frame_header, word_cnt);
308
309 sci_req = scic_request_by_tag(scic, be16_to_cpu(hdr.tag));
Dan Williams01bec772011-05-01 16:51:11 -0700310 if (sci_req && sci_req->target_device == sci_dev) {
311 /* The IO request is now in charge of releasing the frame */
Dan Williamsd1c637c32011-05-11 08:27:47 -0700312 status = scic_sds_io_request_frame_handler(sci_req, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700313 } else {
314 /* We could not map this tag to a valid IO
315 * request Just toss the frame and continue
316 */
317 scic_sds_controller_release_frame(scic, frame_index);
318 }
319 break;
320 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000321 case SCI_STP_DEV_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700322 struct dev_to_host_fis *hdr;
Dan Williams01bec772011-05-01 16:51:11 -0700323
324 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
325 frame_index,
326 (void **)&hdr);
327 if (status != SCI_SUCCESS)
328 return status;
329
Dave Jiange76d6182011-05-04 15:02:03 -0700330 if (hdr->fis_type == FIS_SETDEVBITS &&
331 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700332 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
333
334 /* TODO Check sactive and complete associated IO if any. */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000335 sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
Dave Jiange76d6182011-05-04 15:02:03 -0700336 } else if (hdr->fis_type == FIS_REGD2H &&
337 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700338 /*
339 * Some devices return D2H FIS when an NCQ error is detected.
340 * Treat this like an SDB error FIS ready reason.
341 */
342 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000343 sci_change_state(&sci_dev->sm, SCI_STP_DEV_NCQ_ERROR);
Dan Williams01bec772011-05-01 16:51:11 -0700344 } else
345 status = SCI_FAILURE;
346
347 scic_sds_controller_release_frame(scic, frame_index);
348 break;
349 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000350 case SCI_STP_DEV_CMD:
351 case SCI_SMP_DEV_CMD:
Dan Williams01bec772011-05-01 16:51:11 -0700352 /* The device does not process any UF received from the hardware while
353 * in this state. All unsolicited frames are forwarded to the io request
354 * object.
355 */
356 status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
357 break;
358 }
359
360 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700361}
362
Dan Williamse6225712011-05-01 16:26:09 -0700363static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700364{
Dan Williamse6225712011-05-01 16:26:09 -0700365
Edmund Nadolskie3013702011-06-02 00:10:43 +0000366 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williamse6225712011-05-01 16:26:09 -0700367 enum scic_sds_remote_device_states state = sm->current_state_id;
368
369 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000370 case SCI_DEV_READY:
371 case SCI_STP_DEV_IDLE:
372 case SCI_STP_DEV_CMD:
373 case SCI_STP_DEV_NCQ:
374 case SCI_STP_DEV_NCQ_ERROR:
375 case SCI_STP_DEV_AWAIT_RESET:
376 case SCI_SMP_DEV_IDLE:
377 case SCI_SMP_DEV_CMD:
Dan Williamse6225712011-05-01 16:26:09 -0700378 return true;
379 default:
380 return false;
381 }
382}
383
384enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
385 u32 event_code)
386{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000387 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williamse6225712011-05-01 16:26:09 -0700388 enum scic_sds_remote_device_states state = sm->current_state_id;
389 enum sci_status status;
390
391 switch (scu_get_event_type(event_code)) {
392 case SCU_EVENT_TYPE_RNC_OPS_MISC:
393 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
394 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
395 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
396 break;
397 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
398 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
399 status = SCI_SUCCESS;
400
401 /* Suspend the associated RNC */
402 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
403 SCI_SOFTWARE_SUSPENSION,
404 NULL, NULL);
405
406 dev_dbg(scirdev_to_dev(sci_dev),
407 "%s: device: %p event code: %x: %s\n",
408 __func__, sci_dev, event_code,
409 is_remote_device_ready(sci_dev)
410 ? "I_T_Nexus_Timeout event"
411 : "I_T_Nexus_Timeout event in wrong state");
412
413 break;
414 }
415 /* Else, fall through and treat as unhandled... */
416 default:
417 dev_dbg(scirdev_to_dev(sci_dev),
418 "%s: device: %p event code: %x: %s\n",
419 __func__, sci_dev, event_code,
420 is_remote_device_ready(sci_dev)
421 ? "unexpected event"
422 : "unexpected event in wrong state");
423 status = SCI_FAILURE_INVALID_STATE;
424 break;
425 }
426
427 if (status != SCI_SUCCESS)
428 return status;
429
Edmund Nadolskie3013702011-06-02 00:10:43 +0000430 if (state == SCI_STP_DEV_IDLE) {
Dan Williamse6225712011-05-01 16:26:09 -0700431
432 /* We pick up suspension events to handle specifically to this
433 * state. We resume the RNC right away.
434 */
435 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
436 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
437 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
438 }
439
440 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700441}
442
Dan Williams18606552011-05-01 14:57:11 -0700443static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
444 struct scic_sds_request *sci_req,
445 enum sci_status status)
Dan Williams88f3b622011-04-22 19:18:03 -0700446{
Dan Williams18606552011-05-01 14:57:11 -0700447 struct scic_sds_port *sci_port = sci_dev->owning_port;
448
449 /* cleanup requests that failed after starting on the port */
450 if (status != SCI_SUCCESS)
451 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
452 else
453 scic_sds_remote_device_increment_request_count(sci_dev);
454}
455
456enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
457 struct scic_sds_remote_device *sci_dev,
458 struct scic_sds_request *sci_req)
459{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000460 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams18606552011-05-01 14:57:11 -0700461 enum scic_sds_remote_device_states state = sm->current_state_id;
462 struct scic_sds_port *sci_port = sci_dev->owning_port;
Dan Williams67ea8382011-05-08 11:47:15 -0700463 struct isci_request *ireq = sci_req_to_ireq(sci_req);
Dan Williams18606552011-05-01 14:57:11 -0700464 enum sci_status status;
465
466 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000467 case SCI_DEV_INITIAL:
468 case SCI_DEV_STOPPED:
469 case SCI_DEV_STARTING:
470 case SCI_STP_DEV_NCQ_ERROR:
471 case SCI_DEV_STOPPING:
472 case SCI_DEV_FAILED:
473 case SCI_DEV_RESETTING:
474 case SCI_DEV_FINAL:
Dan Williams18606552011-05-01 14:57:11 -0700475 default:
476 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
477 __func__, state);
478 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000479 case SCI_DEV_READY:
Dan Williams18606552011-05-01 14:57:11 -0700480 /* attempt to start an io request for this device object. The remote
481 * device object will issue the start request for the io and if
482 * successful it will start the request for the port object then
483 * increment its own request count.
484 */
485 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
486 if (status != SCI_SUCCESS)
487 return status;
488
489 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
490 if (status != SCI_SUCCESS)
491 break;
492
493 status = scic_sds_request_start(sci_req);
494 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000495 case SCI_STP_DEV_IDLE: {
Dan Williams18606552011-05-01 14:57:11 -0700496 /* handle the start io operation for a sata device that is in
497 * the command idle state. - Evalute the type of IO request to
498 * be started - If its an NCQ request change to NCQ substate -
499 * If its any other command change to the CMD substate
500 *
501 * If this is a softreset we may want to have a different
502 * substate.
503 */
504 enum scic_sds_remote_device_states new_state;
Dave Jiange76d6182011-05-04 15:02:03 -0700505 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700506
507 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
508 if (status != SCI_SUCCESS)
509 return status;
510
511 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
512 if (status != SCI_SUCCESS)
513 break;
514
Piotr Sawickif4636a72011-05-10 23:50:32 +0000515 status = scic_sds_request_start(sci_req);
Dan Williams18606552011-05-01 14:57:11 -0700516 if (status != SCI_SUCCESS)
517 break;
518
Dave Jiange76d6182011-05-04 15:02:03 -0700519 if (task->ata_task.use_ncq)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000520 new_state = SCI_STP_DEV_NCQ;
Dan Williams18606552011-05-01 14:57:11 -0700521 else {
522 sci_dev->working_request = sci_req;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000523 new_state = SCI_STP_DEV_CMD;
Dan Williams18606552011-05-01 14:57:11 -0700524 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000525 sci_change_state(sm, new_state);
Dan Williams18606552011-05-01 14:57:11 -0700526 break;
527 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000528 case SCI_STP_DEV_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700529 struct sas_task *task = isci_request_access_task(ireq);
530
531 if (task->ata_task.use_ncq) {
Dan Williams18606552011-05-01 14:57:11 -0700532 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
533 if (status != SCI_SUCCESS)
534 return status;
535
536 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
537 if (status != SCI_SUCCESS)
538 break;
539
Piotr Sawickif4636a72011-05-10 23:50:32 +0000540 status = scic_sds_request_start(sci_req);
Dan Williams18606552011-05-01 14:57:11 -0700541 } else
542 return SCI_FAILURE_INVALID_STATE;
543 break;
Dave Jiange76d6182011-05-04 15:02:03 -0700544 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000545 case SCI_STP_DEV_AWAIT_RESET:
Dan Williams18606552011-05-01 14:57:11 -0700546 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000547 case SCI_SMP_DEV_IDLE:
Dan Williams18606552011-05-01 14:57:11 -0700548 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
549 if (status != SCI_SUCCESS)
550 return status;
551
552 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
553 if (status != SCI_SUCCESS)
554 break;
555
556 status = scic_sds_request_start(sci_req);
557 if (status != SCI_SUCCESS)
558 break;
559
560 sci_dev->working_request = sci_req;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000561 sci_change_state(&sci_dev->sm, SCI_SMP_DEV_CMD);
Dan Williams18606552011-05-01 14:57:11 -0700562 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000563 case SCI_STP_DEV_CMD:
564 case SCI_SMP_DEV_CMD:
Dan Williams18606552011-05-01 14:57:11 -0700565 /* device is already handling a command it can not accept new commands
566 * until this one is complete.
567 */
568 return SCI_FAILURE_INVALID_STATE;
569 }
570
571 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
572 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700573}
574
Dan Williams10a09e62011-05-01 15:33:43 -0700575static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
576 struct scic_sds_remote_device *sci_dev,
577 struct scic_sds_request *sci_req)
Dan Williams88f3b622011-04-22 19:18:03 -0700578{
Dan Williams10a09e62011-05-01 15:33:43 -0700579 enum sci_status status;
580
581 status = scic_sds_request_complete(sci_req);
582 if (status != SCI_SUCCESS)
583 return status;
584
585 status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
586 if (status != SCI_SUCCESS)
587 return status;
588
589 scic_sds_remote_device_decrement_request_count(sci_dev);
590 return status;
591}
592
593enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
594 struct scic_sds_remote_device *sci_dev,
595 struct scic_sds_request *sci_req)
596{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000597 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams10a09e62011-05-01 15:33:43 -0700598 enum scic_sds_remote_device_states state = sm->current_state_id;
599 struct scic_sds_port *sci_port = sci_dev->owning_port;
600 enum sci_status status;
601
602 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000603 case SCI_DEV_INITIAL:
604 case SCI_DEV_STOPPED:
605 case SCI_DEV_STARTING:
606 case SCI_STP_DEV_IDLE:
607 case SCI_SMP_DEV_IDLE:
608 case SCI_DEV_FAILED:
609 case SCI_DEV_FINAL:
Dan Williams10a09e62011-05-01 15:33:43 -0700610 default:
611 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
612 __func__, state);
613 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000614 case SCI_DEV_READY:
615 case SCI_STP_DEV_AWAIT_RESET:
616 case SCI_DEV_RESETTING:
Dan Williams10a09e62011-05-01 15:33:43 -0700617 status = common_complete_io(sci_port, sci_dev, sci_req);
618 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000619 case SCI_STP_DEV_CMD:
620 case SCI_STP_DEV_NCQ:
621 case SCI_STP_DEV_NCQ_ERROR:
Dan Williams10a09e62011-05-01 15:33:43 -0700622 status = common_complete_io(sci_port, sci_dev, sci_req);
623 if (status != SCI_SUCCESS)
624 break;
625
626 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
627 /* This request causes hardware error, device needs to be Lun Reset.
628 * So here we force the state machine to IDLE state so the rest IOs
629 * can reach RNC state handler, these IOs will be completed by RNC with
630 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
631 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000632 sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
Dan Williams10a09e62011-05-01 15:33:43 -0700633 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000634 sci_change_state(sm, SCI_STP_DEV_IDLE);
Dan Williams10a09e62011-05-01 15:33:43 -0700635 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000636 case SCI_SMP_DEV_CMD:
Dan Williams10a09e62011-05-01 15:33:43 -0700637 status = common_complete_io(sci_port, sci_dev, sci_req);
638 if (status != SCI_SUCCESS)
639 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000640 sci_change_state(sm, SCI_SMP_DEV_IDLE);
Dan Williams10a09e62011-05-01 15:33:43 -0700641 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000642 case SCI_DEV_STOPPING:
Dan Williams10a09e62011-05-01 15:33:43 -0700643 status = common_complete_io(sci_port, sci_dev, sci_req);
644 if (status != SCI_SUCCESS)
645 break;
646
647 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
648 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
649 rnc_destruct_done,
650 sci_dev);
651 break;
652 }
653
654 if (status != SCI_SUCCESS)
655 dev_err(scirdev_to_dev(sci_dev),
656 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
657 "could not complete\n", __func__, sci_port,
658 sci_dev, sci_req, status);
659
660 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700661}
662
Dan Williams84b9b022011-05-01 15:53:25 -0700663static void scic_sds_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700664{
Dan Williams84b9b022011-05-01 15:53:25 -0700665 struct scic_sds_remote_device *sci_dev = dev;
666
667 /* we need to check if this request is still valid to continue. */
668 if (sci_dev->working_request)
669 scic_controller_continue_io(sci_dev->working_request);
670}
671
672enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
673 struct scic_sds_remote_device *sci_dev,
674 struct scic_sds_request *sci_req)
675{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000676 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams84b9b022011-05-01 15:53:25 -0700677 enum scic_sds_remote_device_states state = sm->current_state_id;
678 struct scic_sds_port *sci_port = sci_dev->owning_port;
679 enum sci_status status;
680
681 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000682 case SCI_DEV_INITIAL:
683 case SCI_DEV_STOPPED:
684 case SCI_DEV_STARTING:
685 case SCI_SMP_DEV_IDLE:
686 case SCI_SMP_DEV_CMD:
687 case SCI_DEV_STOPPING:
688 case SCI_DEV_FAILED:
689 case SCI_DEV_RESETTING:
690 case SCI_DEV_FINAL:
Dan Williams84b9b022011-05-01 15:53:25 -0700691 default:
692 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
693 __func__, state);
694 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000695 case SCI_STP_DEV_IDLE:
696 case SCI_STP_DEV_CMD:
697 case SCI_STP_DEV_NCQ:
698 case SCI_STP_DEV_NCQ_ERROR:
699 case SCI_STP_DEV_AWAIT_RESET:
Dan Williams84b9b022011-05-01 15:53:25 -0700700 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
701 if (status != SCI_SUCCESS)
702 return status;
703
704 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
705 if (status != SCI_SUCCESS)
706 goto out;
707
Piotr Sawickif4636a72011-05-10 23:50:32 +0000708 status = scic_sds_request_start(sci_req);
Dan Williams84b9b022011-05-01 15:53:25 -0700709 if (status != SCI_SUCCESS)
710 goto out;
711
712 /* Note: If the remote device state is not IDLE this will
713 * replace the request that probably resulted in the task
714 * management request.
715 */
716 sci_dev->working_request = sci_req;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000717 sci_change_state(sm, SCI_STP_DEV_CMD);
Dan Williams84b9b022011-05-01 15:53:25 -0700718
719 /* The remote node context must cleanup the TCi to NCQ mapping
720 * table. The only way to do this correctly is to either write
721 * to the TLCR register or to invalidate and repost the RNC. In
722 * either case the remote node context state machine will take
723 * the correct action when the remote node context is suspended
724 * and later resumed.
725 */
726 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
727 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
728 scic_sds_remote_node_context_resume(&sci_dev->rnc,
729 scic_sds_remote_device_continue_request,
730 sci_dev);
731
732 out:
733 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
734 /* We need to let the controller start request handler know that
735 * it can't post TC yet. We will provide a callback function to
736 * post TC when RNC gets resumed.
737 */
738 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000739 case SCI_DEV_READY:
Dan Williams84b9b022011-05-01 15:53:25 -0700740 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
741 if (status != SCI_SUCCESS)
742 return status;
743
744 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
745 if (status != SCI_SUCCESS)
746 break;
747
748 status = scic_sds_request_start(sci_req);
749 break;
750 }
751 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
752
753 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700754}
755
756/**
757 *
Dan Williams88f3b622011-04-22 19:18:03 -0700758 * @sci_dev:
759 * @request:
760 *
761 * This method takes the request and bulids an appropriate SCU context for the
762 * request and then requests the controller to post the request. none
763 */
764void scic_sds_remote_device_post_request(
765 struct scic_sds_remote_device *sci_dev,
766 u32 request)
767{
768 u32 context;
769
770 context = scic_sds_remote_device_build_command_context(sci_dev, request);
771
772 scic_sds_controller_post_request(
773 scic_sds_remote_device_get_controller(sci_dev),
774 context
775 );
776}
777
Dan Williamsab2e8f72011-04-27 16:32:45 -0700778/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700779 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700780 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700781 */
Dan Williamseb229672011-05-01 14:05:57 -0700782static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700783{
Dan Williamsab2e8f72011-04-27 16:32:45 -0700784 struct scic_sds_remote_device *sci_dev = _dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700785
Dan Williamse6225712011-05-01 16:26:09 -0700786 if (is_remote_device_ready(sci_dev))
787 return;
Dan Williams88f3b622011-04-22 19:18:03 -0700788
Dan Williamse6225712011-05-01 16:26:09 -0700789 /* go 'ready' if we are not already in a ready state */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000790 sci_change_state(&sci_dev->sm, SCI_DEV_READY);
Dan Williams88f3b622011-04-22 19:18:03 -0700791}
792
Dan Williamsab2e8f72011-04-27 16:32:45 -0700793static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
794{
795 struct scic_sds_remote_device *sci_dev = _dev;
796 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
797 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
798
799 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
800 * As a result, avoid sending the ready notification.
801 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000802 if (sci_dev->sm.previous_state_id != SCI_STP_DEV_NCQ)
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000803 isci_remote_device_ready(scic_to_ihost(scic), idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700804}
805
Dan Williams9269e0e2011-05-12 07:42:17 -0700806static void scic_sds_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700807{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000808 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700809
Dan Williams88f3b622011-04-22 19:18:03 -0700810 /* Initial state is a transitional state to the stopped state */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000811 sci_change_state(&sci_dev->sm, SCI_DEV_STOPPED);
Dan Williams88f3b622011-04-22 19:18:03 -0700812}
813
814/**
Dan Williams88f3b622011-04-22 19:18:03 -0700815 * scic_remote_device_destruct() - free remote node context and destruct
816 * @remote_device: This parameter specifies the remote device to be destructed.
817 *
818 * Remote device objects are a limited resource. As such, they must be
819 * protected. Thus calls to construct and destruct are mutually exclusive and
820 * non-reentrant. The return value shall indicate if the device was
821 * successfully destructed or if some failure occurred. enum sci_status This value
822 * is returned if the device is successfully destructed.
823 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
824 * device isn't valid (e.g. it's already been destoryed, the handle isn't
825 * valid, etc.).
826 */
827static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
828{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000829 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williamsb8d82f62011-05-01 14:38:26 -0700830 enum scic_sds_remote_device_states state = sm->current_state_id;
831 struct scic_sds_controller *scic;
832
Edmund Nadolskie3013702011-06-02 00:10:43 +0000833 if (state != SCI_DEV_STOPPED) {
Dan Williamsb8d82f62011-05-01 14:38:26 -0700834 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
835 __func__, state);
836 return SCI_FAILURE_INVALID_STATE;
837 }
838
839 scic = sci_dev->owning_port->owning_controller;
840 scic_sds_controller_free_remote_node_context(scic, sci_dev,
841 sci_dev->rnc.remote_node_index);
842 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000843 sci_change_state(sm, SCI_DEV_FINAL);
Dan Williamsb8d82f62011-05-01 14:38:26 -0700844
845 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -0700846}
847
Dan Williams6f231dd2011-07-02 22:56:22 -0700848/**
849 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -0800850 * @ihost: This parameter specifies the isci host object.
851 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -0700852 *
853 */
Dan Williamsd9c37392011-03-03 17:59:32 -0800854static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700855{
Dan Williamsd9c37392011-03-03 17:59:32 -0800856 dev_dbg(&ihost->pdev->dev,
857 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700858
859 /* There should not be any outstanding io's. All paths to
860 * here should go through isci_remote_device_nuke_requests.
861 * If we hit this condition, we will need a way to complete
862 * io requests in process */
Dan Williamsd9c37392011-03-03 17:59:32 -0800863 while (!list_empty(&idev->reqs_in_process)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700864
Dan Williamsd9c37392011-03-03 17:59:32 -0800865 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700866 "%s: ** request list not empty! **\n", __func__);
867 BUG();
868 }
869
Dan Williams57f20f42011-04-21 18:14:45 -0700870 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -0800871 idev->domain_dev->lldd_dev = NULL;
872 idev->domain_dev = NULL;
873 idev->isci_port = NULL;
874 list_del_init(&idev->node);
Dan Williams6ad31fe2011-03-04 12:10:29 -0800875
Dan Williamsd9c37392011-03-03 17:59:32 -0800876 clear_bit(IDEV_START_PENDING, &idev->flags);
877 clear_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williamsd06b4872011-05-02 13:59:25 -0700878 clear_bit(IDEV_EH, &idev->flags);
Dan Williamsd9c37392011-03-03 17:59:32 -0800879 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700880}
881
Dan Williams88f3b622011-04-22 19:18:03 -0700882/**
883 * isci_remote_device_stop_complete() - This function is called by the scic
884 * when the remote device stop has completed. We mark the isci device as not
885 * ready and remove the isci remote device.
886 * @ihost: This parameter specifies the isci host object.
887 * @idev: This parameter specifies the remote device.
888 * @status: This parameter specifies status of the completion.
889 *
890 */
891static void isci_remote_device_stop_complete(struct isci_host *ihost,
892 struct isci_remote_device *idev)
893{
894 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
895
896 isci_remote_device_change_state(idev, isci_stopped);
897
898 /* after stop, we can tear down resources. */
899 isci_remote_device_deconstruct(ihost, idev);
900}
901
Dan Williams9269e0e2011-05-12 07:42:17 -0700902static void scic_sds_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700903{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000904 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000905 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
906 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700907 u32 prev_state;
908
Dan Williams88f3b622011-04-22 19:18:03 -0700909 /* If we are entering from the stopping state let the SCI User know that
910 * the stop operation has completed.
911 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000912 prev_state = sci_dev->sm.previous_state_id;
913 if (prev_state == SCI_DEV_STOPPING)
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000914 isci_remote_device_stop_complete(scic_to_ihost(scic), idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700915
916 scic_sds_controller_remote_device_stopped(scic, sci_dev);
917}
918
Dan Williams9269e0e2011-05-12 07:42:17 -0700919static void scic_sds_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700920{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000921 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700922 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000923 struct isci_host *ihost = scic_to_ihost(scic);
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000924 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700925
Dan Williams88f3b622011-04-22 19:18:03 -0700926 isci_remote_device_not_ready(ihost, idev,
927 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
928}
929
Dan Williams9269e0e2011-05-12 07:42:17 -0700930static void scic_sds_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700931{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000932 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700933 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000934 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
935 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700936
Dan Williams88f3b622011-04-22 19:18:03 -0700937 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
938
Dan Williamsab2e8f72011-04-27 16:32:45 -0700939 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000940 sci_change_state(&sci_dev->sm, SCI_STP_DEV_IDLE);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700941 } else if (dev_is_expander(dev)) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000942 sci_change_state(&sci_dev->sm, SCI_SMP_DEV_IDLE);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700943 } else
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000944 isci_remote_device_ready(scic_to_ihost(scic), idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700945}
946
Dan Williams9269e0e2011-05-12 07:42:17 -0700947static void scic_sds_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700948{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000949 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700950 struct domain_device *dev = sci_dev_to_domain(sci_dev);
951
952 if (dev->dev_type == SAS_END_DEV) {
953 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000954 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700955
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000956 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
Dan Williams88f3b622011-04-22 19:18:03 -0700957 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
958 }
959}
960
Dan Williams9269e0e2011-05-12 07:42:17 -0700961static void scic_sds_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700962{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000963 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700964
Dan Williams88f3b622011-04-22 19:18:03 -0700965 scic_sds_remote_node_context_suspend(
966 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
967}
968
Dan Williams9269e0e2011-05-12 07:42:17 -0700969static void scic_sds_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700970{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000971 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700972
973 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
974}
975
Dan Williams9269e0e2011-05-12 07:42:17 -0700976static void scic_sds_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700977{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000978 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700979
Dan Williamsab2e8f72011-04-27 16:32:45 -0700980 sci_dev->working_request = NULL;
981 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
982 /*
983 * Since the RNC is ready, it's alright to finish completion
984 * processing (e.g. signal the remote device is ready). */
985 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
986 } else {
987 scic_sds_remote_node_context_resume(&sci_dev->rnc,
988 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
989 sci_dev);
990 }
991}
992
Dan Williams9269e0e2011-05-12 07:42:17 -0700993static void scic_sds_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700994{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000995 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700996 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
997
998 BUG_ON(sci_dev->working_request == NULL);
999
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001000 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
Dan Williamsab2e8f72011-04-27 16:32:45 -07001001 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1002}
1003
Dan Williams9269e0e2011-05-12 07:42:17 -07001004static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001005{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001006 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001007 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1008 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1009
Dan Williamsab2e8f72011-04-27 16:32:45 -07001010 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001011 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001012 sci_dev->not_ready_reason);
1013}
1014
Dan Williams9269e0e2011-05-12 07:42:17 -07001015static void scic_sds_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001016{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001017 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001018 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1019
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001020 isci_remote_device_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev));
Dan Williamsab2e8f72011-04-27 16:32:45 -07001021}
1022
Dan Williams9269e0e2011-05-12 07:42:17 -07001023static void scic_sds_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001024{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001025 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001026 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1027
1028 BUG_ON(sci_dev->working_request == NULL);
1029
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001030 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
Dan Williamsab2e8f72011-04-27 16:32:45 -07001031 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1032}
1033
Dan Williams9269e0e2011-05-12 07:42:17 -07001034static void scic_sds_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001035{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001036 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001037
1038 sci_dev->working_request = NULL;
1039}
Dan Williams88f3b622011-04-22 19:18:03 -07001040
1041static const struct sci_base_state scic_sds_remote_device_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001042 [SCI_DEV_INITIAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001043 .enter_state = scic_sds_remote_device_initial_state_enter,
1044 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001045 [SCI_DEV_STOPPED] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001046 .enter_state = scic_sds_remote_device_stopped_state_enter,
1047 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001048 [SCI_DEV_STARTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001049 .enter_state = scic_sds_remote_device_starting_state_enter,
1050 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001051 [SCI_DEV_READY] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001052 .enter_state = scic_sds_remote_device_ready_state_enter,
1053 .exit_state = scic_sds_remote_device_ready_state_exit
1054 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001055 [SCI_STP_DEV_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001056 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1057 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001058 [SCI_STP_DEV_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001059 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1060 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001061 [SCI_STP_DEV_NCQ] = { },
1062 [SCI_STP_DEV_NCQ_ERROR] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001063 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1064 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001065 [SCI_STP_DEV_AWAIT_RESET] = { },
1066 [SCI_SMP_DEV_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001067 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1068 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001069 [SCI_SMP_DEV_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001070 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1071 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1072 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001073 [SCI_DEV_STOPPING] = { },
1074 [SCI_DEV_FAILED] = { },
1075 [SCI_DEV_RESETTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001076 .enter_state = scic_sds_remote_device_resetting_state_enter,
1077 .exit_state = scic_sds_remote_device_resetting_state_exit
1078 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001079 [SCI_DEV_FINAL] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001080};
1081
1082/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001083 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001084 * @sci_port: SAS/SATA port through which this device is accessed.
1085 * @sci_dev: remote device to construct
1086 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001087 * This routine just performs benign initialization and does not
1088 * allocate the remote_node_context which is left to
1089 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1090 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001091 */
1092static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1093 struct scic_sds_remote_device *sci_dev)
1094{
1095 sci_dev->owning_port = sci_port;
1096 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001097
Edmund Nadolskie3013702011-06-02 00:10:43 +00001098 sci_base_state_machine_construct(&sci_dev->sm,
Dan Williams9269e0e2011-05-12 07:42:17 -07001099 scic_sds_remote_device_state_table,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001100 SCI_DEV_INITIAL);
Dan Williams88f3b622011-04-22 19:18:03 -07001101
Edmund Nadolskie3013702011-06-02 00:10:43 +00001102 sci_base_state_machine_start(&sci_dev->sm);
Dan Williams88f3b622011-04-22 19:18:03 -07001103
1104 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1105 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001106}
1107
1108/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001109 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001110 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001111 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1112 * the device is known to the SCI Core since it is contained in the
1113 * scic_phy object. Remote node context(s) is/are a global resource
1114 * allocated by this routine, freed by scic_remote_device_destruct().
1115 *
1116 * Returns:
1117 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1118 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1119 * sata-only controller instance.
1120 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001121 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001122static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1123 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001124{
1125 enum sci_status status;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001126 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001127
Dan Williamsb87ee302011-04-25 11:48:29 -07001128 scic_remote_device_construct(sci_port, sci_dev);
1129
Dan Williams88f3b622011-04-22 19:18:03 -07001130 /*
1131 * This information is request to determine how many remote node context
1132 * entries will be needed to store the remote node.
1133 */
Dan Williams88f3b622011-04-22 19:18:03 -07001134 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001135 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1136 sci_dev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001137 &sci_dev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001138
Dan Williamsa1a113b2011-04-21 18:44:45 -07001139 if (status != SCI_SUCCESS)
1140 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001141
Dan Williamsab2e8f72011-04-27 16:32:45 -07001142 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1143 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1144 /* pass */;
1145 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001146 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001147
Dan Williamsa1a113b2011-04-21 18:44:45 -07001148 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001149
Dan Williamsa1a113b2011-04-21 18:44:45 -07001150 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1151 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001152
Dan Williamsa1a113b2011-04-21 18:44:45 -07001153 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001154}
1155
Dan Williams88f3b622011-04-22 19:18:03 -07001156/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001157 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001158 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001159 * Remote node context(s) is/are a global resource allocated by this
1160 * routine, freed by scic_remote_device_destruct().
1161 *
1162 * Returns:
1163 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1164 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1165 * sata-only controller instance.
1166 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001167 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001168static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001169 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001170{
Dan Williamsa1a113b2011-04-21 18:44:45 -07001171 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001172 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001173
Dan Williamsb87ee302011-04-25 11:48:29 -07001174 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001175
Dan Williamsab2e8f72011-04-27 16:32:45 -07001176 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1177 sci_dev,
1178 &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001179 if (status != SCI_SUCCESS)
1180 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001181
Dan Williamsab2e8f72011-04-27 16:32:45 -07001182 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1183 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1184 /* pass */;
1185 else
1186 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001187
Dan Williamsa1a113b2011-04-21 18:44:45 -07001188 /*
1189 * For SAS-2 the physical link rate is actually a logical link
1190 * rate that incorporates multiplexing. The SCU doesn't
1191 * incorporate multiplexing and for the purposes of the
1192 * connection the logical link rate is that same as the
1193 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1194 * one another, so this code works for both situations. */
1195 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001196 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001197
Dan Williamsa1a113b2011-04-21 18:44:45 -07001198 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1199 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001200
Dan Williamsab2e8f72011-04-27 16:32:45 -07001201 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001202}
1203
1204/**
1205 * scic_remote_device_start() - This method will start the supplied remote
1206 * device. This method enables normal IO requests to flow through to the
1207 * remote device.
1208 * @remote_device: This parameter specifies the device to be started.
1209 * @timeout: This parameter specifies the number of milliseconds in which the
1210 * start operation should complete.
1211 *
1212 * An indication of whether the device was successfully started. SCI_SUCCESS
1213 * This value is returned if the device was successfully started.
1214 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1215 * the device when there have been no phys added to it.
1216 */
1217static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
Dan Williamseb229672011-05-01 14:05:57 -07001218 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001219{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001220 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williamseb229672011-05-01 14:05:57 -07001221 enum scic_sds_remote_device_states state = sm->current_state_id;
1222 enum sci_status status;
1223
Edmund Nadolskie3013702011-06-02 00:10:43 +00001224 if (state != SCI_DEV_STOPPED) {
Dan Williamseb229672011-05-01 14:05:57 -07001225 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1226 __func__, state);
1227 return SCI_FAILURE_INVALID_STATE;
1228 }
1229
1230 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1231 remote_device_resume_done,
1232 sci_dev);
1233 if (status != SCI_SUCCESS)
1234 return status;
1235
Edmund Nadolskie3013702011-06-02 00:10:43 +00001236 sci_change_state(sm, SCI_DEV_STARTING);
Dan Williamseb229672011-05-01 14:05:57 -07001237
1238 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001239}
Dan Williams6f231dd2011-07-02 22:56:22 -07001240
Dan Williams00d680e2011-04-25 14:29:29 -07001241static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1242 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001243{
Dan Williamse5313812011-05-07 10:11:43 -07001244 struct scic_sds_port *sci_port = &iport->sci;
Dan Williams00d680e2011-04-25 14:29:29 -07001245 struct isci_host *ihost = iport->isci_host;
1246 struct domain_device *dev = idev->domain_dev;
1247 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001248
Dan Williams00d680e2011-04-25 14:29:29 -07001249 if (dev->parent && dev_is_expander(dev->parent))
1250 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1251 else
1252 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001253
1254 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001255 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1256 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001257
1258 return status;
1259 }
1260
Dan Williams6f231dd2011-07-02 22:56:22 -07001261 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001262 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001263
Dan Williams00d680e2011-04-25 14:29:29 -07001264 if (status != SCI_SUCCESS)
1265 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1266 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001267
1268 return status;
1269}
1270
Dan Williams4393aa42011-03-31 13:10:44 -07001271void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001272{
1273 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001274
Dan Williams4393aa42011-03-31 13:10:44 -07001275 dev_dbg(&ihost->pdev->dev,
1276 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001277
1278 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -07001279 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -07001280
Dan Williams4393aa42011-03-31 13:10:44 -07001281 dev_dbg(&ihost->pdev->dev,
1282 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001283}
1284
Dan Williams6f231dd2011-07-02 22:56:22 -07001285/**
1286 * This function builds the isci_remote_device when a libsas dev_found message
1287 * is received.
1288 * @isci_host: This parameter specifies the isci host object.
1289 * @port: This parameter specifies the isci_port conected to this device.
1290 *
1291 * pointer to new isci_remote_device.
1292 */
1293static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001294isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001295{
Dan Williamsd9c37392011-03-03 17:59:32 -08001296 struct isci_remote_device *idev;
1297 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001298
Dan Williamsd9c37392011-03-03 17:59:32 -08001299 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001300 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001301 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1302 break;
1303 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001304
Dan Williamsd9c37392011-03-03 17:59:32 -08001305 if (i >= SCI_MAX_REMOTE_DEVICES) {
1306 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001307 return NULL;
1308 }
1309
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001310 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1311 return NULL;
1312
1313 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1314 return NULL;
1315
Dan Williamsd9c37392011-03-03 17:59:32 -08001316 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001317
Dan Williamsd9c37392011-03-03 17:59:32 -08001318 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001319}
Dan Williams6f231dd2011-07-02 22:56:22 -07001320
1321/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001322 * isci_remote_device_stop() - This function is called internally to stop the
1323 * remote device.
1324 * @isci_host: This parameter specifies the isci host object.
1325 * @isci_device: This parameter specifies the remote device.
1326 *
1327 * The status of the scic request to stop.
1328 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001329enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001330{
1331 enum sci_status status;
1332 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001333
Dan Williams6ad31fe2011-03-04 12:10:29 -08001334 dev_dbg(&ihost->pdev->dev,
1335 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001336
Dan Williams6ad31fe2011-03-04 12:10:29 -08001337 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001338
1339 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001340 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001341
Dan Williams6ad31fe2011-03-04 12:10:29 -08001342 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001343
Dan Williams6ad31fe2011-03-04 12:10:29 -08001344 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07001345 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001346 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001347
1348 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -08001349 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -08001350 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08001351 clear_bit(IDEV_ALLOCATED, &idev->flags);
1352 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001353
Dan Williams6ad31fe2011-03-04 12:10:29 -08001354 dev_dbg(&ihost->pdev->dev,
1355 "%s: idev = %p - after completion wait\n",
1356 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001357
Dan Williams6f231dd2011-07-02 22:56:22 -07001358 return status;
1359}
1360
1361/**
1362 * isci_remote_device_gone() - This function is called by libsas when a domain
1363 * device is removed.
1364 * @domain_device: This parameter specifies the libsas domain device.
1365 *
1366 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001367void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001368{
Dan Williams4393aa42011-03-31 13:10:44 -07001369 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001370 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001371
Dan Williams6ad31fe2011-03-04 12:10:29 -08001372 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001373 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001374 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001375
Dan Williams6ad31fe2011-03-04 12:10:29 -08001376 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001377}
1378
1379
1380/**
1381 * isci_remote_device_found() - This function is called by libsas when a remote
1382 * device is discovered. A remote device object is created and started. the
1383 * function then sleeps until the sci core device started message is
1384 * received.
1385 * @domain_device: This parameter specifies the libsas domain device.
1386 *
1387 * status, zero indicates success.
1388 */
1389int isci_remote_device_found(struct domain_device *domain_dev)
1390{
Dan Williams4393aa42011-03-31 13:10:44 -07001391 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001392 struct isci_port *isci_port;
1393 struct isci_phy *isci_phy;
1394 struct asd_sas_port *sas_port;
1395 struct asd_sas_phy *sas_phy;
1396 struct isci_remote_device *isci_device;
1397 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001398
Dan Williams6f231dd2011-07-02 22:56:22 -07001399 dev_dbg(&isci_host->pdev->dev,
1400 "%s: domain_device = %p\n", __func__, domain_dev);
1401
Dan Williams0cf89d12011-02-18 09:25:07 -08001402 wait_for_start(isci_host);
1403
Dan Williams6f231dd2011-07-02 22:56:22 -07001404 sas_port = domain_dev->port;
1405 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1406 port_phy_el);
1407 isci_phy = to_isci_phy(sas_phy);
1408 isci_port = isci_phy->isci_port;
1409
1410 /* we are being called for a device on this port,
1411 * so it has to come up eventually
1412 */
1413 wait_for_completion(&isci_port->start_complete);
1414
1415 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1416 (isci_stopped == isci_port_get_state(isci_port)))
1417 return -ENODEV;
1418
1419 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001420 if (!isci_device)
1421 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001422
1423 INIT_LIST_HEAD(&isci_device->node);
1424 domain_dev->lldd_dev = isci_device;
1425 isci_device->domain_dev = domain_dev;
1426 isci_device->isci_port = isci_port;
1427 isci_remote_device_change_state(isci_device, isci_starting);
1428
1429
Dan Williams1a380452011-03-03 18:01:43 -08001430 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001431 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1432
Dan Williams6ad31fe2011-03-04 12:10:29 -08001433 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001434 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -08001435 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001436
Dan Williams6f231dd2011-07-02 22:56:22 -07001437 dev_dbg(&isci_host->pdev->dev,
1438 "%s: isci_device = %p\n",
1439 __func__, isci_device);
1440
1441 if (status != SCI_SUCCESS) {
1442
Dan Williams1a380452011-03-03 18:01:43 -08001443 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001444 isci_remote_device_deconstruct(
1445 isci_host,
1446 isci_device
1447 );
Dan Williams1a380452011-03-03 18:01:43 -08001448 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001449 return -ENODEV;
1450 }
1451
Dan Williams6ad31fe2011-03-04 12:10:29 -08001452 /* wait for the device ready callback. */
1453 wait_for_device_start(isci_host, isci_device);
1454
Dan Williams6f231dd2011-07-02 22:56:22 -07001455 return 0;
1456}
1457/**
1458 * isci_device_is_reset_pending() - This function will check if there is any
1459 * pending reset condition on the device.
1460 * @request: This parameter is the isci_device object.
1461 *
1462 * true if there is a reset pending for the device.
1463 */
1464bool isci_device_is_reset_pending(
1465 struct isci_host *isci_host,
1466 struct isci_remote_device *isci_device)
1467{
1468 struct isci_request *isci_request;
1469 struct isci_request *tmp_req;
1470 bool reset_is_pending = false;
1471 unsigned long flags;
1472
1473 dev_dbg(&isci_host->pdev->dev,
1474 "%s: isci_device = %p\n", __func__, isci_device);
1475
1476 spin_lock_irqsave(&isci_host->scic_lock, flags);
1477
1478 /* Check for reset on all pending requests. */
1479 list_for_each_entry_safe(isci_request, tmp_req,
1480 &isci_device->reqs_in_process, dev_node) {
1481 dev_dbg(&isci_host->pdev->dev,
1482 "%s: isci_device = %p request = %p\n",
1483 __func__, isci_device, isci_request);
1484
1485 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001486 struct sas_task *task = isci_request_access_task(
1487 isci_request);
1488
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001489 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001490 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1491 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001492 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001493 }
1494 }
1495
1496 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1497
1498 dev_dbg(&isci_host->pdev->dev,
1499 "%s: isci_device = %p reset_is_pending = %d\n",
1500 __func__, isci_device, reset_is_pending);
1501
1502 return reset_is_pending;
1503}
1504
1505/**
1506 * isci_device_clear_reset_pending() - This function will clear if any pending
1507 * reset condition flags on the device.
1508 * @request: This parameter is the isci_device object.
1509 *
1510 * true if there is a reset pending for the device.
1511 */
Dan Williams4393aa42011-03-31 13:10:44 -07001512void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001513{
1514 struct isci_request *isci_request;
1515 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07001516 unsigned long flags = 0;
1517
Dan Williams4393aa42011-03-31 13:10:44 -07001518 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1519 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07001520
Dan Williams4393aa42011-03-31 13:10:44 -07001521 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001522
1523 /* Clear reset pending on all pending requests. */
1524 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07001525 &idev->reqs_in_process, dev_node) {
1526 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1527 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001528
1529 if (isci_request->ttype == io_task) {
1530
1531 unsigned long flags2;
1532 struct sas_task *task = isci_request_access_task(
1533 isci_request);
1534
1535 spin_lock_irqsave(&task->task_state_lock, flags2);
1536 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1537 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1538 }
1539 }
Dan Williams4393aa42011-03-31 13:10:44 -07001540 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001541}