blob: ab5f9868e4efaee3a42c0dc397a14dbb93f6da06 [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)
Dan Williams209fae12011-06-13 17:39:44 -070097 set_bit(IDEV_GONE, &idev->flags);
Dan Williamsab2e8f72011-04-27 16:32:45 -070098 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);
Dan Williams209fae12011-06-13 17:39:44 -0700452 else {
453 kref_get(&sci_dev_to_idev(sci_dev)->kref);
Dan Williams18606552011-05-01 14:57:11 -0700454 scic_sds_remote_device_increment_request_count(sci_dev);
Dan Williams209fae12011-06-13 17:39:44 -0700455 }
Dan Williams18606552011-05-01 14:57:11 -0700456}
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{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000462 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams18606552011-05-01 14:57:11 -0700463 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) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000469 case SCI_DEV_INITIAL:
470 case SCI_DEV_STOPPED:
471 case SCI_DEV_STARTING:
472 case SCI_STP_DEV_NCQ_ERROR:
473 case SCI_DEV_STOPPING:
474 case SCI_DEV_FAILED:
475 case SCI_DEV_RESETTING:
476 case SCI_DEV_FINAL:
Dan Williams18606552011-05-01 14:57:11 -0700477 default:
478 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
479 __func__, state);
480 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000481 case SCI_DEV_READY:
Dan Williams18606552011-05-01 14:57:11 -0700482 /* 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;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000497 case SCI_STP_DEV_IDLE: {
Dan Williams18606552011-05-01 14:57:11 -0700498 /* 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)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000522 new_state = SCI_STP_DEV_NCQ;
Dan Williams18606552011-05-01 14:57:11 -0700523 else {
524 sci_dev->working_request = sci_req;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000525 new_state = SCI_STP_DEV_CMD;
Dan Williams18606552011-05-01 14:57:11 -0700526 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000527 sci_change_state(sm, new_state);
Dan Williams18606552011-05-01 14:57:11 -0700528 break;
529 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000530 case SCI_STP_DEV_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700531 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 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000547 case SCI_STP_DEV_AWAIT_RESET:
Dan Williams18606552011-05-01 14:57:11 -0700548 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000549 case SCI_SMP_DEV_IDLE:
Dan Williams18606552011-05-01 14:57:11 -0700550 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;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000563 sci_change_state(&sci_dev->sm, SCI_SMP_DEV_CMD);
Dan Williams18606552011-05-01 14:57:11 -0700564 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000565 case SCI_STP_DEV_CMD:
566 case SCI_SMP_DEV_CMD:
Dan Williams18606552011-05-01 14:57:11 -0700567 /* device is already handling a command it can not accept new commands
568 * until this one is complete.
569 */
570 return SCI_FAILURE_INVALID_STATE;
571 }
572
573 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
574 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700575}
576
Dan Williams10a09e62011-05-01 15:33:43 -0700577static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
578 struct scic_sds_remote_device *sci_dev,
579 struct scic_sds_request *sci_req)
Dan Williams88f3b622011-04-22 19:18:03 -0700580{
Dan Williams10a09e62011-05-01 15:33:43 -0700581 enum sci_status status;
582
583 status = scic_sds_request_complete(sci_req);
584 if (status != SCI_SUCCESS)
585 return status;
586
587 status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
588 if (status != SCI_SUCCESS)
589 return status;
590
591 scic_sds_remote_device_decrement_request_count(sci_dev);
592 return status;
593}
594
595enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
596 struct scic_sds_remote_device *sci_dev,
597 struct scic_sds_request *sci_req)
598{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000599 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams10a09e62011-05-01 15:33:43 -0700600 enum scic_sds_remote_device_states state = sm->current_state_id;
601 struct scic_sds_port *sci_port = sci_dev->owning_port;
602 enum sci_status status;
603
604 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000605 case SCI_DEV_INITIAL:
606 case SCI_DEV_STOPPED:
607 case SCI_DEV_STARTING:
608 case SCI_STP_DEV_IDLE:
609 case SCI_SMP_DEV_IDLE:
610 case SCI_DEV_FAILED:
611 case SCI_DEV_FINAL:
Dan Williams10a09e62011-05-01 15:33:43 -0700612 default:
613 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
614 __func__, state);
615 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000616 case SCI_DEV_READY:
617 case SCI_STP_DEV_AWAIT_RESET:
618 case SCI_DEV_RESETTING:
Dan Williams10a09e62011-05-01 15:33:43 -0700619 status = common_complete_io(sci_port, sci_dev, sci_req);
620 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000621 case SCI_STP_DEV_CMD:
622 case SCI_STP_DEV_NCQ:
623 case SCI_STP_DEV_NCQ_ERROR:
Dan Williams10a09e62011-05-01 15:33:43 -0700624 status = common_complete_io(sci_port, sci_dev, sci_req);
625 if (status != SCI_SUCCESS)
626 break;
627
628 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
629 /* This request causes hardware error, device needs to be Lun Reset.
630 * So here we force the state machine to IDLE state so the rest IOs
631 * can reach RNC state handler, these IOs will be completed by RNC with
632 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
633 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000634 sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
Dan Williams10a09e62011-05-01 15:33:43 -0700635 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000636 sci_change_state(sm, SCI_STP_DEV_IDLE);
Dan Williams10a09e62011-05-01 15:33:43 -0700637 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000638 case SCI_SMP_DEV_CMD:
Dan Williams10a09e62011-05-01 15:33:43 -0700639 status = common_complete_io(sci_port, sci_dev, sci_req);
640 if (status != SCI_SUCCESS)
641 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000642 sci_change_state(sm, SCI_SMP_DEV_IDLE);
Dan Williams10a09e62011-05-01 15:33:43 -0700643 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000644 case SCI_DEV_STOPPING:
Dan Williams10a09e62011-05-01 15:33:43 -0700645 status = common_complete_io(sci_port, sci_dev, sci_req);
646 if (status != SCI_SUCCESS)
647 break;
648
649 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
650 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
651 rnc_destruct_done,
652 sci_dev);
653 break;
654 }
655
656 if (status != SCI_SUCCESS)
657 dev_err(scirdev_to_dev(sci_dev),
658 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
659 "could not complete\n", __func__, sci_port,
660 sci_dev, sci_req, status);
Dan Williams209fae12011-06-13 17:39:44 -0700661 else
662 isci_put_device(sci_dev_to_idev(sci_dev));
Dan Williams10a09e62011-05-01 15:33:43 -0700663
664 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700665}
666
Dan Williams84b9b022011-05-01 15:53:25 -0700667static void scic_sds_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700668{
Dan Williams84b9b022011-05-01 15:53:25 -0700669 struct scic_sds_remote_device *sci_dev = dev;
670
671 /* we need to check if this request is still valid to continue. */
672 if (sci_dev->working_request)
673 scic_controller_continue_io(sci_dev->working_request);
674}
675
676enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
677 struct scic_sds_remote_device *sci_dev,
678 struct scic_sds_request *sci_req)
679{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000680 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams84b9b022011-05-01 15:53:25 -0700681 enum scic_sds_remote_device_states state = sm->current_state_id;
682 struct scic_sds_port *sci_port = sci_dev->owning_port;
683 enum sci_status status;
684
685 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000686 case SCI_DEV_INITIAL:
687 case SCI_DEV_STOPPED:
688 case SCI_DEV_STARTING:
689 case SCI_SMP_DEV_IDLE:
690 case SCI_SMP_DEV_CMD:
691 case SCI_DEV_STOPPING:
692 case SCI_DEV_FAILED:
693 case SCI_DEV_RESETTING:
694 case SCI_DEV_FINAL:
Dan Williams84b9b022011-05-01 15:53:25 -0700695 default:
696 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
697 __func__, state);
698 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000699 case SCI_STP_DEV_IDLE:
700 case SCI_STP_DEV_CMD:
701 case SCI_STP_DEV_NCQ:
702 case SCI_STP_DEV_NCQ_ERROR:
703 case SCI_STP_DEV_AWAIT_RESET:
Dan Williams84b9b022011-05-01 15:53:25 -0700704 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
705 if (status != SCI_SUCCESS)
706 return status;
707
708 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
709 if (status != SCI_SUCCESS)
710 goto out;
711
Piotr Sawickif4636a72011-05-10 23:50:32 +0000712 status = scic_sds_request_start(sci_req);
Dan Williams84b9b022011-05-01 15:53:25 -0700713 if (status != SCI_SUCCESS)
714 goto out;
715
716 /* Note: If the remote device state is not IDLE this will
717 * replace the request that probably resulted in the task
718 * management request.
719 */
720 sci_dev->working_request = sci_req;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000721 sci_change_state(sm, SCI_STP_DEV_CMD);
Dan Williams84b9b022011-05-01 15:53:25 -0700722
723 /* The remote node context must cleanup the TCi to NCQ mapping
724 * table. The only way to do this correctly is to either write
725 * to the TLCR register or to invalidate and repost the RNC. In
726 * either case the remote node context state machine will take
727 * the correct action when the remote node context is suspended
728 * and later resumed.
729 */
730 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
731 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
732 scic_sds_remote_node_context_resume(&sci_dev->rnc,
733 scic_sds_remote_device_continue_request,
734 sci_dev);
735
736 out:
737 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
738 /* We need to let the controller start request handler know that
739 * it can't post TC yet. We will provide a callback function to
740 * post TC when RNC gets resumed.
741 */
742 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000743 case SCI_DEV_READY:
Dan Williams84b9b022011-05-01 15:53:25 -0700744 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
745 if (status != SCI_SUCCESS)
746 return status;
747
748 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
749 if (status != SCI_SUCCESS)
750 break;
751
752 status = scic_sds_request_start(sci_req);
753 break;
754 }
755 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
756
757 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700758}
759
760/**
761 *
Dan Williams88f3b622011-04-22 19:18:03 -0700762 * @sci_dev:
763 * @request:
764 *
765 * This method takes the request and bulids an appropriate SCU context for the
766 * request and then requests the controller to post the request. none
767 */
768void scic_sds_remote_device_post_request(
769 struct scic_sds_remote_device *sci_dev,
770 u32 request)
771{
772 u32 context;
773
774 context = scic_sds_remote_device_build_command_context(sci_dev, request);
775
776 scic_sds_controller_post_request(
777 scic_sds_remote_device_get_controller(sci_dev),
778 context
779 );
780}
781
Dan Williamsab2e8f72011-04-27 16:32:45 -0700782/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700783 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700784 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700785 */
Dan Williamseb229672011-05-01 14:05:57 -0700786static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700787{
Dan Williamsab2e8f72011-04-27 16:32:45 -0700788 struct scic_sds_remote_device *sci_dev = _dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700789
Dan Williamse6225712011-05-01 16:26:09 -0700790 if (is_remote_device_ready(sci_dev))
791 return;
Dan Williams88f3b622011-04-22 19:18:03 -0700792
Dan Williamse6225712011-05-01 16:26:09 -0700793 /* go 'ready' if we are not already in a ready state */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000794 sci_change_state(&sci_dev->sm, SCI_DEV_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 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000806 if (sci_dev->sm.previous_state_id != SCI_STP_DEV_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
Dan Williams9269e0e2011-05-12 07:42:17 -0700810static void scic_sds_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700811{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000812 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
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 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000815 sci_change_state(&sci_dev->sm, SCI_DEV_STOPPED);
Dan Williams88f3b622011-04-22 19:18:03 -0700816}
817
818/**
Dan Williams88f3b622011-04-22 19:18:03 -0700819 * scic_remote_device_destruct() - free remote node context and destruct
820 * @remote_device: This parameter specifies the remote device to be destructed.
821 *
822 * Remote device objects are a limited resource. As such, they must be
823 * protected. Thus calls to construct and destruct are mutually exclusive and
824 * non-reentrant. The return value shall indicate if the device was
825 * successfully destructed or if some failure occurred. enum sci_status This value
826 * is returned if the device is successfully destructed.
827 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
828 * device isn't valid (e.g. it's already been destoryed, the handle isn't
829 * valid, etc.).
830 */
831static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
832{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000833 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williamsb8d82f62011-05-01 14:38:26 -0700834 enum scic_sds_remote_device_states state = sm->current_state_id;
835 struct scic_sds_controller *scic;
836
Edmund Nadolskie3013702011-06-02 00:10:43 +0000837 if (state != SCI_DEV_STOPPED) {
Dan Williamsb8d82f62011-05-01 14:38:26 -0700838 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
839 __func__, state);
840 return SCI_FAILURE_INVALID_STATE;
841 }
842
843 scic = sci_dev->owning_port->owning_controller;
844 scic_sds_controller_free_remote_node_context(scic, sci_dev,
845 sci_dev->rnc.remote_node_index);
846 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000847 sci_change_state(sm, SCI_DEV_FINAL);
Dan Williamsb8d82f62011-05-01 14:38:26 -0700848
849 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -0700850}
851
Dan Williams6f231dd2011-07-02 22:56:22 -0700852/**
853 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -0800854 * @ihost: This parameter specifies the isci host object.
855 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -0700856 *
857 */
Dan Williamsd9c37392011-03-03 17:59:32 -0800858static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700859{
Dan Williamsd9c37392011-03-03 17:59:32 -0800860 dev_dbg(&ihost->pdev->dev,
861 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700862
863 /* There should not be any outstanding io's. All paths to
864 * here should go through isci_remote_device_nuke_requests.
865 * If we hit this condition, we will need a way to complete
866 * io requests in process */
Dan Williams209fae12011-06-13 17:39:44 -0700867 BUG_ON(!list_empty(&idev->reqs_in_process));
Dan Williams6f231dd2011-07-02 22:56:22 -0700868
Dan Williams57f20f42011-04-21 18:14:45 -0700869 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -0800870 list_del_init(&idev->node);
Dan Williams209fae12011-06-13 17:39:44 -0700871 isci_put_device(idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700872}
873
Dan Williams88f3b622011-04-22 19:18:03 -0700874/**
875 * isci_remote_device_stop_complete() - This function is called by the scic
876 * when the remote device stop has completed. We mark the isci device as not
877 * ready and remove the isci remote device.
878 * @ihost: This parameter specifies the isci host object.
879 * @idev: This parameter specifies the remote device.
880 * @status: This parameter specifies status of the completion.
881 *
882 */
883static void isci_remote_device_stop_complete(struct isci_host *ihost,
884 struct isci_remote_device *idev)
885{
886 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
887
888 isci_remote_device_change_state(idev, isci_stopped);
889
890 /* after stop, we can tear down resources. */
891 isci_remote_device_deconstruct(ihost, idev);
892}
893
Dan Williams9269e0e2011-05-12 07:42:17 -0700894static void scic_sds_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700895{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000896 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000897 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
898 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700899 u32 prev_state;
900
Dan Williams88f3b622011-04-22 19:18:03 -0700901 /* If we are entering from the stopping state let the SCI User know that
902 * the stop operation has completed.
903 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000904 prev_state = sci_dev->sm.previous_state_id;
905 if (prev_state == SCI_DEV_STOPPING)
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000906 isci_remote_device_stop_complete(scic_to_ihost(scic), idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700907
908 scic_sds_controller_remote_device_stopped(scic, sci_dev);
909}
910
Dan Williams9269e0e2011-05-12 07:42:17 -0700911static void scic_sds_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700912{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000913 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700914 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000915 struct isci_host *ihost = scic_to_ihost(scic);
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000916 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700917
Dan Williams88f3b622011-04-22 19:18:03 -0700918 isci_remote_device_not_ready(ihost, idev,
919 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
920}
921
Dan Williams9269e0e2011-05-12 07:42:17 -0700922static void scic_sds_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700923{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000924 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700925 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000926 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
927 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700928
Dan Williams88f3b622011-04-22 19:18:03 -0700929 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
930
Dan Williamsab2e8f72011-04-27 16:32:45 -0700931 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000932 sci_change_state(&sci_dev->sm, SCI_STP_DEV_IDLE);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700933 } else if (dev_is_expander(dev)) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000934 sci_change_state(&sci_dev->sm, SCI_SMP_DEV_IDLE);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700935 } else
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000936 isci_remote_device_ready(scic_to_ihost(scic), idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700937}
938
Dan Williams9269e0e2011-05-12 07:42:17 -0700939static void scic_sds_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700940{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000941 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700942 struct domain_device *dev = sci_dev_to_domain(sci_dev);
943
944 if (dev->dev_type == SAS_END_DEV) {
945 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000946 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700947
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000948 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
Dan Williams88f3b622011-04-22 19:18:03 -0700949 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
950 }
951}
952
Dan Williams9269e0e2011-05-12 07:42:17 -0700953static void scic_sds_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700954{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000955 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700956
Dan Williams88f3b622011-04-22 19:18:03 -0700957 scic_sds_remote_node_context_suspend(
958 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
959}
960
Dan Williams9269e0e2011-05-12 07:42:17 -0700961static void scic_sds_remote_device_resetting_state_exit(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
965 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
966}
967
Dan Williams9269e0e2011-05-12 07:42:17 -0700968static void scic_sds_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700969{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000970 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700971
Dan Williamsab2e8f72011-04-27 16:32:45 -0700972 sci_dev->working_request = NULL;
973 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
974 /*
975 * Since the RNC is ready, it's alright to finish completion
976 * processing (e.g. signal the remote device is ready). */
977 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
978 } else {
979 scic_sds_remote_node_context_resume(&sci_dev->rnc,
980 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
981 sci_dev);
982 }
983}
984
Dan Williams9269e0e2011-05-12 07:42:17 -0700985static void scic_sds_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700986{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000987 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700988 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
989
990 BUG_ON(sci_dev->working_request == NULL);
991
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000992 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
Dan Williamsab2e8f72011-04-27 16:32:45 -0700993 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
994}
995
Dan Williams9269e0e2011-05-12 07:42:17 -0700996static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700997{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000998 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700999 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1000 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1001
Dan Williamsab2e8f72011-04-27 16:32:45 -07001002 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001003 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001004 sci_dev->not_ready_reason);
1005}
1006
Dan Williams9269e0e2011-05-12 07:42:17 -07001007static void scic_sds_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001008{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001009 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001010 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1011
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001012 isci_remote_device_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev));
Dan Williamsab2e8f72011-04-27 16:32:45 -07001013}
1014
Dan Williams9269e0e2011-05-12 07:42:17 -07001015static void scic_sds_smp_remote_device_ready_cmd_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
1020 BUG_ON(sci_dev->working_request == NULL);
1021
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001022 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
Dan Williamsab2e8f72011-04-27 16:32:45 -07001023 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1024}
1025
Dan Williams9269e0e2011-05-12 07:42:17 -07001026static void scic_sds_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001027{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001028 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001029
1030 sci_dev->working_request = NULL;
1031}
Dan Williams88f3b622011-04-22 19:18:03 -07001032
1033static const struct sci_base_state scic_sds_remote_device_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001034 [SCI_DEV_INITIAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001035 .enter_state = scic_sds_remote_device_initial_state_enter,
1036 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001037 [SCI_DEV_STOPPED] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001038 .enter_state = scic_sds_remote_device_stopped_state_enter,
1039 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001040 [SCI_DEV_STARTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001041 .enter_state = scic_sds_remote_device_starting_state_enter,
1042 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001043 [SCI_DEV_READY] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001044 .enter_state = scic_sds_remote_device_ready_state_enter,
1045 .exit_state = scic_sds_remote_device_ready_state_exit
1046 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001047 [SCI_STP_DEV_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001048 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1049 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001050 [SCI_STP_DEV_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001051 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1052 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001053 [SCI_STP_DEV_NCQ] = { },
1054 [SCI_STP_DEV_NCQ_ERROR] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001055 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1056 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001057 [SCI_STP_DEV_AWAIT_RESET] = { },
1058 [SCI_SMP_DEV_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001059 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1060 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001061 [SCI_SMP_DEV_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001062 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1063 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1064 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001065 [SCI_DEV_STOPPING] = { },
1066 [SCI_DEV_FAILED] = { },
1067 [SCI_DEV_RESETTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001068 .enter_state = scic_sds_remote_device_resetting_state_enter,
1069 .exit_state = scic_sds_remote_device_resetting_state_exit
1070 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001071 [SCI_DEV_FINAL] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001072};
1073
1074/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001075 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001076 * @sci_port: SAS/SATA port through which this device is accessed.
1077 * @sci_dev: remote device to construct
1078 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001079 * This routine just performs benign initialization and does not
1080 * allocate the remote_node_context which is left to
1081 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1082 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001083 */
1084static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1085 struct scic_sds_remote_device *sci_dev)
1086{
1087 sci_dev->owning_port = sci_port;
1088 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001089
Edmund Nadolski12ef6542011-06-02 00:10:50 +00001090 sci_init_sm(&sci_dev->sm, scic_sds_remote_device_state_table, SCI_DEV_INITIAL);
Dan Williams88f3b622011-04-22 19:18:03 -07001091
1092 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1093 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001094}
1095
1096/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001097 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001098 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001099 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1100 * the device is known to the SCI Core since it is contained in the
1101 * scic_phy object. Remote node context(s) is/are a global resource
1102 * allocated by this routine, freed by scic_remote_device_destruct().
1103 *
1104 * Returns:
1105 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1106 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1107 * sata-only controller instance.
1108 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001109 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001110static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1111 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001112{
1113 enum sci_status status;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001114 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001115
Dan Williamsb87ee302011-04-25 11:48:29 -07001116 scic_remote_device_construct(sci_port, sci_dev);
1117
Dan Williams88f3b622011-04-22 19:18:03 -07001118 /*
1119 * This information is request to determine how many remote node context
1120 * entries will be needed to store the remote node.
1121 */
Dan Williams88f3b622011-04-22 19:18:03 -07001122 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001123 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1124 sci_dev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001125 &sci_dev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001126
Dan Williamsa1a113b2011-04-21 18:44:45 -07001127 if (status != SCI_SUCCESS)
1128 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001129
Dan Williamsab2e8f72011-04-27 16:32:45 -07001130 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1131 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1132 /* pass */;
1133 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001134 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001135
Dan Williamsa1a113b2011-04-21 18:44:45 -07001136 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001137
Dan Williamsa1a113b2011-04-21 18:44:45 -07001138 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1139 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001140
Dan Williamsa1a113b2011-04-21 18:44:45 -07001141 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001142}
1143
Dan Williams88f3b622011-04-22 19:18:03 -07001144/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001145 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001146 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001147 * Remote node context(s) is/are a global resource allocated by this
1148 * routine, freed by scic_remote_device_destruct().
1149 *
1150 * Returns:
1151 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1152 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1153 * sata-only controller instance.
1154 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001155 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001156static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001157 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001158{
Dan Williamsa1a113b2011-04-21 18:44:45 -07001159 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001160 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001161
Dan Williamsb87ee302011-04-25 11:48:29 -07001162 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001163
Dan Williamsab2e8f72011-04-27 16:32:45 -07001164 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1165 sci_dev,
1166 &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001167 if (status != SCI_SUCCESS)
1168 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001169
Dan Williamsab2e8f72011-04-27 16:32:45 -07001170 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1171 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1172 /* pass */;
1173 else
1174 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001175
Dan Williamsa1a113b2011-04-21 18:44:45 -07001176 /*
1177 * For SAS-2 the physical link rate is actually a logical link
1178 * rate that incorporates multiplexing. The SCU doesn't
1179 * incorporate multiplexing and for the purposes of the
1180 * connection the logical link rate is that same as the
1181 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1182 * one another, so this code works for both situations. */
1183 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001184 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001185
Dan Williamsa1a113b2011-04-21 18:44:45 -07001186 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1187 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001188
Dan Williamsab2e8f72011-04-27 16:32:45 -07001189 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001190}
1191
1192/**
1193 * scic_remote_device_start() - This method will start the supplied remote
1194 * device. This method enables normal IO requests to flow through to the
1195 * remote device.
1196 * @remote_device: This parameter specifies the device to be started.
1197 * @timeout: This parameter specifies the number of milliseconds in which the
1198 * start operation should complete.
1199 *
1200 * An indication of whether the device was successfully started. SCI_SUCCESS
1201 * This value is returned if the device was successfully started.
1202 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1203 * the device when there have been no phys added to it.
1204 */
1205static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
Dan Williamseb229672011-05-01 14:05:57 -07001206 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001207{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001208 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williamseb229672011-05-01 14:05:57 -07001209 enum scic_sds_remote_device_states state = sm->current_state_id;
1210 enum sci_status status;
1211
Edmund Nadolskie3013702011-06-02 00:10:43 +00001212 if (state != SCI_DEV_STOPPED) {
Dan Williamseb229672011-05-01 14:05:57 -07001213 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1214 __func__, state);
1215 return SCI_FAILURE_INVALID_STATE;
1216 }
1217
1218 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1219 remote_device_resume_done,
1220 sci_dev);
1221 if (status != SCI_SUCCESS)
1222 return status;
1223
Edmund Nadolskie3013702011-06-02 00:10:43 +00001224 sci_change_state(sm, SCI_DEV_STARTING);
Dan Williamseb229672011-05-01 14:05:57 -07001225
1226 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001227}
Dan Williams6f231dd2011-07-02 22:56:22 -07001228
Dan Williams00d680e2011-04-25 14:29:29 -07001229static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1230 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001231{
Dan Williamse5313812011-05-07 10:11:43 -07001232 struct scic_sds_port *sci_port = &iport->sci;
Dan Williams00d680e2011-04-25 14:29:29 -07001233 struct isci_host *ihost = iport->isci_host;
1234 struct domain_device *dev = idev->domain_dev;
1235 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001236
Dan Williams00d680e2011-04-25 14:29:29 -07001237 if (dev->parent && dev_is_expander(dev->parent))
1238 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1239 else
1240 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001241
1242 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001243 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1244 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001245
1246 return status;
1247 }
1248
Dan Williams6f231dd2011-07-02 22:56:22 -07001249 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001250 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001251
Dan Williams00d680e2011-04-25 14:29:29 -07001252 if (status != SCI_SUCCESS)
1253 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1254 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001255
1256 return status;
1257}
1258
Dan Williams4393aa42011-03-31 13:10:44 -07001259void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001260{
1261 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001262
Dan Williams4393aa42011-03-31 13:10:44 -07001263 dev_dbg(&ihost->pdev->dev,
1264 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001265
1266 /* Cleanup all requests pending for this device. */
Dan Williams980d3ae2011-06-20 15:11:22 -07001267 isci_terminate_pending_requests(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001268
Dan Williams4393aa42011-03-31 13:10:44 -07001269 dev_dbg(&ihost->pdev->dev,
1270 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001271}
1272
Dan Williams6f231dd2011-07-02 22:56:22 -07001273/**
1274 * This function builds the isci_remote_device when a libsas dev_found message
1275 * is received.
1276 * @isci_host: This parameter specifies the isci host object.
1277 * @port: This parameter specifies the isci_port conected to this device.
1278 *
1279 * pointer to new isci_remote_device.
1280 */
1281static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001282isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001283{
Dan Williamsd9c37392011-03-03 17:59:32 -08001284 struct isci_remote_device *idev;
1285 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001286
Dan Williamsd9c37392011-03-03 17:59:32 -08001287 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001288 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001289 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1290 break;
1291 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001292
Dan Williamsd9c37392011-03-03 17:59:32 -08001293 if (i >= SCI_MAX_REMOTE_DEVICES) {
1294 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001295 return NULL;
1296 }
1297
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001298 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1299 return NULL;
1300
1301 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1302 return NULL;
1303
Dan Williamsd9c37392011-03-03 17:59:32 -08001304 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001305
Dan Williamsd9c37392011-03-03 17:59:32 -08001306 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001307}
Dan Williams6f231dd2011-07-02 22:56:22 -07001308
Dan Williams209fae12011-06-13 17:39:44 -07001309void isci_remote_device_release(struct kref *kref)
1310{
1311 struct isci_remote_device *idev = container_of(kref, typeof(*idev), kref);
1312 struct isci_host *ihost = idev->isci_port->isci_host;
1313
1314 idev->domain_dev = NULL;
1315 idev->isci_port = NULL;
1316 clear_bit(IDEV_START_PENDING, &idev->flags);
1317 clear_bit(IDEV_STOP_PENDING, &idev->flags);
1318 clear_bit(IDEV_GONE, &idev->flags);
1319 clear_bit(IDEV_EH, &idev->flags);
1320 smp_mb__before_clear_bit();
1321 clear_bit(IDEV_ALLOCATED, &idev->flags);
1322 wake_up(&ihost->eventq);
1323}
1324
Dan Williams6f231dd2011-07-02 22:56:22 -07001325/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001326 * isci_remote_device_stop() - This function is called internally to stop the
1327 * remote device.
1328 * @isci_host: This parameter specifies the isci host object.
1329 * @isci_device: This parameter specifies the remote device.
1330 *
1331 * The status of the scic request to stop.
1332 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001333enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001334{
1335 enum sci_status status;
1336 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001337
Dan Williams6ad31fe2011-03-04 12:10:29 -08001338 dev_dbg(&ihost->pdev->dev,
1339 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001340
Dan Williams209fae12011-06-13 17:39:44 -07001341 spin_lock_irqsave(&ihost->scic_lock, flags);
1342 idev->domain_dev->lldd_dev = NULL; /* disable new lookups */
1343 set_bit(IDEV_GONE, &idev->flags);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001344 isci_remote_device_change_state(idev, isci_stopping);
Dan Williams209fae12011-06-13 17:39:44 -07001345 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001346
1347 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001348 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001349
Dan Williams6ad31fe2011-03-04 12:10:29 -08001350 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001351
Dan Williams6ad31fe2011-03-04 12:10:29 -08001352 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07001353 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001354 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001355
1356 /* Wait for the stop complete callback. */
Dan Williams209fae12011-06-13 17:39:44 -07001357 if (WARN_ONCE(status != SCI_SUCCESS, "failed to stop device\n"))
1358 /* nothing to wait for */;
1359 else
Dan Williams6ad31fe2011-03-04 12:10:29 -08001360 wait_for_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001361
Dan Williams6f231dd2011-07-02 22:56:22 -07001362 return status;
1363}
1364
1365/**
1366 * isci_remote_device_gone() - This function is called by libsas when a domain
1367 * device is removed.
1368 * @domain_device: This parameter specifies the libsas domain device.
1369 *
1370 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001371void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001372{
Dan Williams4393aa42011-03-31 13:10:44 -07001373 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001374 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001375
Dan Williams6ad31fe2011-03-04 12:10:29 -08001376 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001377 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001378 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001379
Dan Williams6ad31fe2011-03-04 12:10:29 -08001380 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001381}
1382
1383
1384/**
1385 * isci_remote_device_found() - This function is called by libsas when a remote
1386 * device is discovered. A remote device object is created and started. the
1387 * function then sleeps until the sci core device started message is
1388 * received.
1389 * @domain_device: This parameter specifies the libsas domain device.
1390 *
1391 * status, zero indicates success.
1392 */
1393int isci_remote_device_found(struct domain_device *domain_dev)
1394{
Dan Williams4393aa42011-03-31 13:10:44 -07001395 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001396 struct isci_port *isci_port;
1397 struct isci_phy *isci_phy;
1398 struct asd_sas_port *sas_port;
1399 struct asd_sas_phy *sas_phy;
1400 struct isci_remote_device *isci_device;
1401 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001402
Dan Williams6f231dd2011-07-02 22:56:22 -07001403 dev_dbg(&isci_host->pdev->dev,
1404 "%s: domain_device = %p\n", __func__, domain_dev);
1405
Dan Williams0cf89d12011-02-18 09:25:07 -08001406 wait_for_start(isci_host);
1407
Dan Williams6f231dd2011-07-02 22:56:22 -07001408 sas_port = domain_dev->port;
1409 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1410 port_phy_el);
1411 isci_phy = to_isci_phy(sas_phy);
1412 isci_port = isci_phy->isci_port;
1413
1414 /* we are being called for a device on this port,
1415 * so it has to come up eventually
1416 */
1417 wait_for_completion(&isci_port->start_complete);
1418
1419 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1420 (isci_stopped == isci_port_get_state(isci_port)))
1421 return -ENODEV;
1422
1423 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001424 if (!isci_device)
1425 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001426
Dan Williams209fae12011-06-13 17:39:44 -07001427 kref_init(&isci_device->kref);
Dan Williams6f231dd2011-07-02 22:56:22 -07001428 INIT_LIST_HEAD(&isci_device->node);
Dan Williams209fae12011-06-13 17:39:44 -07001429
1430 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001431 isci_device->domain_dev = domain_dev;
1432 isci_device->isci_port = isci_port;
1433 isci_remote_device_change_state(isci_device, isci_starting);
Dan Williams6f231dd2011-07-02 22:56:22 -07001434 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1435
Dan Williams6ad31fe2011-03-04 12:10:29 -08001436 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001437 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001438
Dan Williams6f231dd2011-07-02 22:56:22 -07001439 dev_dbg(&isci_host->pdev->dev,
1440 "%s: isci_device = %p\n",
1441 __func__, isci_device);
1442
Dan Williams209fae12011-06-13 17:39:44 -07001443 if (status == SCI_SUCCESS) {
1444 /* device came up, advertise it to the world */
1445 domain_dev->lldd_dev = isci_device;
1446 } else
1447 isci_put_device(isci_device);
1448 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001449
Dan Williams6ad31fe2011-03-04 12:10:29 -08001450 /* wait for the device ready callback. */
1451 wait_for_device_start(isci_host, isci_device);
1452
Dan Williams209fae12011-06-13 17:39:44 -07001453 return status == SCI_SUCCESS ? 0 : -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001454}
1455/**
1456 * isci_device_is_reset_pending() - This function will check if there is any
1457 * pending reset condition on the device.
1458 * @request: This parameter is the isci_device object.
1459 *
1460 * true if there is a reset pending for the device.
1461 */
1462bool isci_device_is_reset_pending(
1463 struct isci_host *isci_host,
1464 struct isci_remote_device *isci_device)
1465{
1466 struct isci_request *isci_request;
1467 struct isci_request *tmp_req;
1468 bool reset_is_pending = false;
1469 unsigned long flags;
1470
1471 dev_dbg(&isci_host->pdev->dev,
1472 "%s: isci_device = %p\n", __func__, isci_device);
1473
1474 spin_lock_irqsave(&isci_host->scic_lock, flags);
1475
1476 /* Check for reset on all pending requests. */
1477 list_for_each_entry_safe(isci_request, tmp_req,
1478 &isci_device->reqs_in_process, dev_node) {
1479 dev_dbg(&isci_host->pdev->dev,
1480 "%s: isci_device = %p request = %p\n",
1481 __func__, isci_device, isci_request);
1482
1483 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001484 struct sas_task *task = isci_request_access_task(
1485 isci_request);
1486
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001487 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001488 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1489 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001490 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001491 }
1492 }
1493
1494 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1495
1496 dev_dbg(&isci_host->pdev->dev,
1497 "%s: isci_device = %p reset_is_pending = %d\n",
1498 __func__, isci_device, reset_is_pending);
1499
1500 return reset_is_pending;
1501}
1502
1503/**
1504 * isci_device_clear_reset_pending() - This function will clear if any pending
1505 * reset condition flags on the device.
1506 * @request: This parameter is the isci_device object.
1507 *
1508 * true if there is a reset pending for the device.
1509 */
Dan Williams4393aa42011-03-31 13:10:44 -07001510void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001511{
1512 struct isci_request *isci_request;
1513 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07001514 unsigned long flags = 0;
1515
Dan Williams4393aa42011-03-31 13:10:44 -07001516 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1517 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07001518
Dan Williams4393aa42011-03-31 13:10:44 -07001519 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001520
1521 /* Clear reset pending on all pending requests. */
1522 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07001523 &idev->reqs_in_process, dev_node) {
1524 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1525 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001526
1527 if (isci_request->ttype == io_task) {
1528
1529 unsigned long flags2;
1530 struct sas_task *task = isci_request_access_task(
1531 isci_request);
1532
1533 spin_lock_irqsave(&task->task_state_lock, flags2);
1534 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1535 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1536 }
1537 }
Dan Williams4393aa42011-03-31 13:10:44 -07001538 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001539}