blob: c5ce0f0f3645d30eef41d9f1dc43aae46e520842 [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/**
Dan Williamsab2e8f72011-04-27 16:32:45 -070065 * isci_remote_device_not_ready() - This function is called by the scic when
66 * the remote device is not ready. We mark the isci device as ready (not
67 * "ready_for_io") and signal the waiting proccess.
68 * @isci_host: This parameter specifies the isci host object.
69 * @isci_device: This parameter specifies the remote device
70 *
Jeff Skirvin9274f452011-06-23 17:09:02 -070071 * scic_lock is held on entrance to this function.
Dan Williamsab2e8f72011-04-27 16:32:45 -070072 */
73static void isci_remote_device_not_ready(struct isci_host *ihost,
74 struct isci_remote_device *idev, u32 reason)
75{
Jeff Skirvin9274f452011-06-23 17:09:02 -070076 struct isci_request * ireq;
77
Dan Williamsab2e8f72011-04-27 16:32:45 -070078 dev_dbg(&ihost->pdev->dev,
79 "%s: isci_device = %p\n", __func__, idev);
80
Jeff Skirvin9274f452011-06-23 17:09:02 -070081 switch (reason) {
82 case SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED:
Dan Williams209fae12011-06-13 17:39:44 -070083 set_bit(IDEV_GONE, &idev->flags);
Jeff Skirvin9274f452011-06-23 17:09:02 -070084 break;
85 case SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED:
86 set_bit(IDEV_IO_NCQERROR, &idev->flags);
87
88 /* Kill all outstanding requests for the device. */
89 list_for_each_entry(ireq, &idev->reqs_in_process, dev_node) {
90
91 dev_dbg(&ihost->pdev->dev,
92 "%s: isci_device = %p request = %p\n",
93 __func__, idev, ireq);
94
95 scic_controller_terminate_request(&ihost->sci,
96 &idev->sci,
97 &ireq->sci);
98 }
99 /* Fall through into the default case... */
100 default:
Dan Williamsf2088262011-06-16 11:26:12 -0700101 clear_bit(IDEV_IO_READY, &idev->flags);
Jeff Skirvin9274f452011-06-23 17:09:02 -0700102 break;
103 }
Dan Williamsab2e8f72011-04-27 16:32:45 -0700104}
105
106/**
107 * isci_remote_device_ready() - This function is called by the scic when the
108 * remote device is ready. We mark the isci device as ready and signal the
109 * waiting proccess.
110 * @ihost: our valid isci_host
111 * @idev: remote device
112 *
113 */
114static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
115{
116 dev_dbg(&ihost->pdev->dev,
117 "%s: idev = %p\n", __func__, idev);
118
Jeff Skirvin9274f452011-06-23 17:09:02 -0700119 clear_bit(IDEV_IO_NCQERROR, &idev->flags);
Dan Williamsf2088262011-06-16 11:26:12 -0700120 set_bit(IDEV_IO_READY, &idev->flags);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700121 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
122 wake_up(&ihost->eventq);
123}
124
Dan Williamsec575662011-05-01 14:19:25 -0700125/* called once the remote node context is ready to be freed.
126 * The remote device can now report that its stop operation is complete. none
127 */
128static void rnc_destruct_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700129{
Dan Williamsec575662011-05-01 14:19:25 -0700130 struct scic_sds_remote_device *sci_dev = _dev;
131
132 BUG_ON(sci_dev->started_request_count != 0);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000133 sci_change_state(&sci_dev->sm, SCI_DEV_STOPPED);
Dan Williamsec575662011-05-01 14:19:25 -0700134}
135
136static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
137{
138 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
139 u32 i, request_count = sci_dev->started_request_count;
140 enum sci_status status = SCI_SUCCESS;
141
142 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
143 struct scic_sds_request *sci_req;
144 enum sci_status s;
145
146 sci_req = scic->io_request_table[i];
147 if (!sci_req || sci_req->target_device != sci_dev)
148 continue;
149 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
150 if (s != SCI_SUCCESS)
151 status = s;
152 }
153
154 return status;
155}
156
157enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
158 u32 timeout)
159{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000160 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williamsec575662011-05-01 14:19:25 -0700161 enum scic_sds_remote_device_states state = sm->current_state_id;
162
163 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000164 case SCI_DEV_INITIAL:
165 case SCI_DEV_FAILED:
166 case SCI_DEV_FINAL:
Dan Williamsec575662011-05-01 14:19:25 -0700167 default:
168 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
169 __func__, state);
170 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000171 case SCI_DEV_STOPPED:
Dan Williamsec575662011-05-01 14:19:25 -0700172 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000173 case SCI_DEV_STARTING:
Dan Williamsec575662011-05-01 14:19:25 -0700174 /* device not started so there had better be no requests */
175 BUG_ON(sci_dev->started_request_count != 0);
176 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
177 rnc_destruct_done, sci_dev);
178 /* Transition to the stopping state and wait for the
179 * remote node to complete being posted and invalidated.
180 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000181 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williamsec575662011-05-01 14:19:25 -0700182 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000183 case SCI_DEV_READY:
184 case SCI_STP_DEV_IDLE:
185 case SCI_STP_DEV_CMD:
186 case SCI_STP_DEV_NCQ:
187 case SCI_STP_DEV_NCQ_ERROR:
188 case SCI_STP_DEV_AWAIT_RESET:
189 case SCI_SMP_DEV_IDLE:
190 case SCI_SMP_DEV_CMD:
191 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williamsec575662011-05-01 14:19:25 -0700192 if (sci_dev->started_request_count == 0) {
193 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
194 rnc_destruct_done, sci_dev);
195 return SCI_SUCCESS;
196 } else
197 return scic_sds_remote_device_terminate_requests(sci_dev);
198 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000199 case SCI_DEV_STOPPING:
Dan Williamsec575662011-05-01 14:19:25 -0700200 /* All requests should have been terminated, but if there is an
201 * attempt to stop a device already in the stopping state, then
202 * try again to terminate.
203 */
204 return scic_sds_remote_device_terminate_requests(sci_dev);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000205 case SCI_DEV_RESETTING:
206 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williamsec575662011-05-01 14:19:25 -0700207 return SCI_SUCCESS;
208 }
Dan Williams88f3b622011-04-22 19:18:03 -0700209}
Dan Williams6f231dd2011-07-02 22:56:22 -0700210
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700211enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700212{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000213 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700214 enum scic_sds_remote_device_states state = sm->current_state_id;
215
216 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000217 case SCI_DEV_INITIAL:
218 case SCI_DEV_STOPPED:
219 case SCI_DEV_STARTING:
220 case SCI_SMP_DEV_IDLE:
221 case SCI_SMP_DEV_CMD:
222 case SCI_DEV_STOPPING:
223 case SCI_DEV_FAILED:
224 case SCI_DEV_RESETTING:
225 case SCI_DEV_FINAL:
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700226 default:
227 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
228 __func__, state);
229 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000230 case SCI_DEV_READY:
231 case SCI_STP_DEV_IDLE:
232 case SCI_STP_DEV_CMD:
233 case SCI_STP_DEV_NCQ:
234 case SCI_STP_DEV_NCQ_ERROR:
235 case SCI_STP_DEV_AWAIT_RESET:
236 sci_change_state(sm, SCI_DEV_RESETTING);
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700237 return SCI_SUCCESS;
238 }
Dan Williams88f3b622011-04-22 19:18:03 -0700239}
240
Dan Williams81515182011-05-01 14:53:00 -0700241enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700242{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000243 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams81515182011-05-01 14:53:00 -0700244 enum scic_sds_remote_device_states state = sm->current_state_id;
Dan Williams88f3b622011-04-22 19:18:03 -0700245
Edmund Nadolskie3013702011-06-02 00:10:43 +0000246 if (state != SCI_DEV_RESETTING) {
Dan Williams81515182011-05-01 14:53:00 -0700247 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
248 __func__, state);
249 return SCI_FAILURE_INVALID_STATE;
250 }
251
Edmund Nadolskie3013702011-06-02 00:10:43 +0000252 sci_change_state(sm, SCI_DEV_READY);
Dan Williams81515182011-05-01 14:53:00 -0700253 return SCI_SUCCESS;
254}
Dan Williams88f3b622011-04-22 19:18:03 -0700255
Dan Williams323f0ec2011-05-01 16:15:47 -0700256enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
257 u32 suspend_type)
Dan Williams88f3b622011-04-22 19:18:03 -0700258{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000259 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams323f0ec2011-05-01 16:15:47 -0700260 enum scic_sds_remote_device_states state = sm->current_state_id;
261
Edmund Nadolskie3013702011-06-02 00:10:43 +0000262 if (state != SCI_STP_DEV_CMD) {
Dan Williams323f0ec2011-05-01 16:15:47 -0700263 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
264 __func__, state);
265 return SCI_FAILURE_INVALID_STATE;
266 }
267
268 return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
269 suspend_type, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700270}
271
Dan Williams01bec772011-05-01 16:51:11 -0700272enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
273 u32 frame_index)
Dan Williams88f3b622011-04-22 19:18:03 -0700274{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000275 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams01bec772011-05-01 16:51:11 -0700276 enum scic_sds_remote_device_states state = sm->current_state_id;
277 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
278 enum sci_status status;
279
280 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000281 case SCI_DEV_INITIAL:
282 case SCI_DEV_STOPPED:
283 case SCI_DEV_STARTING:
284 case SCI_STP_DEV_IDLE:
285 case SCI_SMP_DEV_IDLE:
286 case SCI_DEV_FINAL:
Dan Williams01bec772011-05-01 16:51:11 -0700287 default:
288 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
289 __func__, state);
290 /* Return the frame back to the controller */
291 scic_sds_controller_release_frame(scic, frame_index);
292 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000293 case SCI_DEV_READY:
294 case SCI_STP_DEV_NCQ_ERROR:
295 case SCI_STP_DEV_AWAIT_RESET:
296 case SCI_DEV_STOPPING:
297 case SCI_DEV_FAILED:
298 case SCI_DEV_RESETTING: {
Dan Williams01bec772011-05-01 16:51:11 -0700299 struct scic_sds_request *sci_req;
Dave Jiang2d9c2242011-05-04 18:45:05 -0700300 struct ssp_frame_hdr hdr;
301 void *frame_header;
302 ssize_t word_cnt;
Dan Williams01bec772011-05-01 16:51:11 -0700303
304 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
305 frame_index,
Dave Jiang2d9c2242011-05-04 18:45:05 -0700306 &frame_header);
Dan Williams01bec772011-05-01 16:51:11 -0700307 if (status != SCI_SUCCESS)
308 return status;
309
Dave Jiang2d9c2242011-05-04 18:45:05 -0700310 word_cnt = sizeof(hdr) / sizeof(u32);
311 sci_swab32_cpy(&hdr, frame_header, word_cnt);
312
313 sci_req = scic_request_by_tag(scic, be16_to_cpu(hdr.tag));
Dan Williams01bec772011-05-01 16:51:11 -0700314 if (sci_req && sci_req->target_device == sci_dev) {
315 /* The IO request is now in charge of releasing the frame */
Dan Williamsd1c637c32011-05-11 08:27:47 -0700316 status = scic_sds_io_request_frame_handler(sci_req, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700317 } else {
318 /* We could not map this tag to a valid IO
319 * request Just toss the frame and continue
320 */
321 scic_sds_controller_release_frame(scic, frame_index);
322 }
323 break;
324 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000325 case SCI_STP_DEV_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700326 struct dev_to_host_fis *hdr;
Dan Williams01bec772011-05-01 16:51:11 -0700327
328 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
329 frame_index,
330 (void **)&hdr);
331 if (status != SCI_SUCCESS)
332 return status;
333
Dave Jiange76d6182011-05-04 15:02:03 -0700334 if (hdr->fis_type == FIS_SETDEVBITS &&
335 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700336 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
337
338 /* TODO Check sactive and complete associated IO if any. */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000339 sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
Dave Jiange76d6182011-05-04 15:02:03 -0700340 } else if (hdr->fis_type == FIS_REGD2H &&
341 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700342 /*
343 * Some devices return D2H FIS when an NCQ error is detected.
344 * Treat this like an SDB error FIS ready reason.
345 */
346 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000347 sci_change_state(&sci_dev->sm, SCI_STP_DEV_NCQ_ERROR);
Dan Williams01bec772011-05-01 16:51:11 -0700348 } else
349 status = SCI_FAILURE;
350
351 scic_sds_controller_release_frame(scic, frame_index);
352 break;
353 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000354 case SCI_STP_DEV_CMD:
355 case SCI_SMP_DEV_CMD:
Dan Williams01bec772011-05-01 16:51:11 -0700356 /* The device does not process any UF received from the hardware while
357 * in this state. All unsolicited frames are forwarded to the io request
358 * object.
359 */
360 status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
361 break;
362 }
363
364 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700365}
366
Dan Williamse6225712011-05-01 16:26:09 -0700367static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700368{
Dan Williamse6225712011-05-01 16:26:09 -0700369
Edmund Nadolskie3013702011-06-02 00:10:43 +0000370 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williamse6225712011-05-01 16:26:09 -0700371 enum scic_sds_remote_device_states state = sm->current_state_id;
372
373 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000374 case SCI_DEV_READY:
375 case SCI_STP_DEV_IDLE:
376 case SCI_STP_DEV_CMD:
377 case SCI_STP_DEV_NCQ:
378 case SCI_STP_DEV_NCQ_ERROR:
379 case SCI_STP_DEV_AWAIT_RESET:
380 case SCI_SMP_DEV_IDLE:
381 case SCI_SMP_DEV_CMD:
Dan Williamse6225712011-05-01 16:26:09 -0700382 return true;
383 default:
384 return false;
385 }
386}
387
388enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
389 u32 event_code)
390{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000391 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williamse6225712011-05-01 16:26:09 -0700392 enum scic_sds_remote_device_states state = sm->current_state_id;
393 enum sci_status status;
394
395 switch (scu_get_event_type(event_code)) {
396 case SCU_EVENT_TYPE_RNC_OPS_MISC:
397 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
398 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
399 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
400 break;
401 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
402 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
403 status = SCI_SUCCESS;
404
405 /* Suspend the associated RNC */
406 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
407 SCI_SOFTWARE_SUSPENSION,
408 NULL, NULL);
409
410 dev_dbg(scirdev_to_dev(sci_dev),
411 "%s: device: %p event code: %x: %s\n",
412 __func__, sci_dev, event_code,
413 is_remote_device_ready(sci_dev)
414 ? "I_T_Nexus_Timeout event"
415 : "I_T_Nexus_Timeout event in wrong state");
416
417 break;
418 }
419 /* Else, fall through and treat as unhandled... */
420 default:
421 dev_dbg(scirdev_to_dev(sci_dev),
422 "%s: device: %p event code: %x: %s\n",
423 __func__, sci_dev, event_code,
424 is_remote_device_ready(sci_dev)
425 ? "unexpected event"
426 : "unexpected event in wrong state");
427 status = SCI_FAILURE_INVALID_STATE;
428 break;
429 }
430
431 if (status != SCI_SUCCESS)
432 return status;
433
Edmund Nadolskie3013702011-06-02 00:10:43 +0000434 if (state == SCI_STP_DEV_IDLE) {
Dan Williamse6225712011-05-01 16:26:09 -0700435
436 /* We pick up suspension events to handle specifically to this
437 * state. We resume the RNC right away.
438 */
439 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
440 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
441 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
442 }
443
444 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700445}
446
Dan Williams18606552011-05-01 14:57:11 -0700447static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
448 struct scic_sds_request *sci_req,
449 enum sci_status status)
Dan Williams88f3b622011-04-22 19:18:03 -0700450{
Dan Williams18606552011-05-01 14:57:11 -0700451 struct scic_sds_port *sci_port = sci_dev->owning_port;
452
453 /* cleanup requests that failed after starting on the port */
454 if (status != SCI_SUCCESS)
455 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
Dan Williams209fae12011-06-13 17:39:44 -0700456 else {
457 kref_get(&sci_dev_to_idev(sci_dev)->kref);
Dan Williams18606552011-05-01 14:57:11 -0700458 scic_sds_remote_device_increment_request_count(sci_dev);
Dan Williams209fae12011-06-13 17:39:44 -0700459 }
Dan Williams18606552011-05-01 14:57:11 -0700460}
461
462enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
463 struct scic_sds_remote_device *sci_dev,
464 struct scic_sds_request *sci_req)
465{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000466 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams18606552011-05-01 14:57:11 -0700467 enum scic_sds_remote_device_states state = sm->current_state_id;
468 struct scic_sds_port *sci_port = sci_dev->owning_port;
Dan Williams67ea8382011-05-08 11:47:15 -0700469 struct isci_request *ireq = sci_req_to_ireq(sci_req);
Dan Williams18606552011-05-01 14:57:11 -0700470 enum sci_status status;
471
472 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000473 case SCI_DEV_INITIAL:
474 case SCI_DEV_STOPPED:
475 case SCI_DEV_STARTING:
476 case SCI_STP_DEV_NCQ_ERROR:
477 case SCI_DEV_STOPPING:
478 case SCI_DEV_FAILED:
479 case SCI_DEV_RESETTING:
480 case SCI_DEV_FINAL:
Dan Williams18606552011-05-01 14:57:11 -0700481 default:
482 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
483 __func__, state);
484 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000485 case SCI_DEV_READY:
Dan Williams18606552011-05-01 14:57:11 -0700486 /* attempt to start an io request for this device object. The remote
487 * device object will issue the start request for the io and if
488 * successful it will start the request for the port object then
489 * increment its own request count.
490 */
491 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
492 if (status != SCI_SUCCESS)
493 return status;
494
495 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
496 if (status != SCI_SUCCESS)
497 break;
498
499 status = scic_sds_request_start(sci_req);
500 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000501 case SCI_STP_DEV_IDLE: {
Dan Williams18606552011-05-01 14:57:11 -0700502 /* handle the start io operation for a sata device that is in
503 * the command idle state. - Evalute the type of IO request to
504 * be started - If its an NCQ request change to NCQ substate -
505 * If its any other command change to the CMD substate
506 *
507 * If this is a softreset we may want to have a different
508 * substate.
509 */
510 enum scic_sds_remote_device_states new_state;
Dave Jiange76d6182011-05-04 15:02:03 -0700511 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700512
513 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
514 if (status != SCI_SUCCESS)
515 return status;
516
517 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
518 if (status != SCI_SUCCESS)
519 break;
520
Piotr Sawickif4636a72011-05-10 23:50:32 +0000521 status = scic_sds_request_start(sci_req);
Dan Williams18606552011-05-01 14:57:11 -0700522 if (status != SCI_SUCCESS)
523 break;
524
Dave Jiange76d6182011-05-04 15:02:03 -0700525 if (task->ata_task.use_ncq)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000526 new_state = SCI_STP_DEV_NCQ;
Dan Williams18606552011-05-01 14:57:11 -0700527 else {
528 sci_dev->working_request = sci_req;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000529 new_state = SCI_STP_DEV_CMD;
Dan Williams18606552011-05-01 14:57:11 -0700530 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000531 sci_change_state(sm, new_state);
Dan Williams18606552011-05-01 14:57:11 -0700532 break;
533 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000534 case SCI_STP_DEV_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700535 struct sas_task *task = isci_request_access_task(ireq);
536
537 if (task->ata_task.use_ncq) {
Dan Williams18606552011-05-01 14:57:11 -0700538 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
539 if (status != SCI_SUCCESS)
540 return status;
541
542 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
543 if (status != SCI_SUCCESS)
544 break;
545
Piotr Sawickif4636a72011-05-10 23:50:32 +0000546 status = scic_sds_request_start(sci_req);
Dan Williams18606552011-05-01 14:57:11 -0700547 } else
548 return SCI_FAILURE_INVALID_STATE;
549 break;
Dave Jiange76d6182011-05-04 15:02:03 -0700550 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000551 case SCI_STP_DEV_AWAIT_RESET:
Dan Williams18606552011-05-01 14:57:11 -0700552 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000553 case SCI_SMP_DEV_IDLE:
Dan Williams18606552011-05-01 14:57:11 -0700554 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
555 if (status != SCI_SUCCESS)
556 return status;
557
558 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
559 if (status != SCI_SUCCESS)
560 break;
561
562 status = scic_sds_request_start(sci_req);
563 if (status != SCI_SUCCESS)
564 break;
565
566 sci_dev->working_request = sci_req;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000567 sci_change_state(&sci_dev->sm, SCI_SMP_DEV_CMD);
Dan Williams18606552011-05-01 14:57:11 -0700568 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000569 case SCI_STP_DEV_CMD:
570 case SCI_SMP_DEV_CMD:
Dan Williams18606552011-05-01 14:57:11 -0700571 /* device is already handling a command it can not accept new commands
572 * until this one is complete.
573 */
574 return SCI_FAILURE_INVALID_STATE;
575 }
576
577 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
578 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700579}
580
Dan Williams10a09e62011-05-01 15:33:43 -0700581static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
582 struct scic_sds_remote_device *sci_dev,
583 struct scic_sds_request *sci_req)
Dan Williams88f3b622011-04-22 19:18:03 -0700584{
Dan Williams10a09e62011-05-01 15:33:43 -0700585 enum sci_status status;
586
587 status = scic_sds_request_complete(sci_req);
588 if (status != SCI_SUCCESS)
589 return status;
590
591 status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
592 if (status != SCI_SUCCESS)
593 return status;
594
595 scic_sds_remote_device_decrement_request_count(sci_dev);
596 return status;
597}
598
599enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
600 struct scic_sds_remote_device *sci_dev,
601 struct scic_sds_request *sci_req)
602{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000603 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams10a09e62011-05-01 15:33:43 -0700604 enum scic_sds_remote_device_states state = sm->current_state_id;
605 struct scic_sds_port *sci_port = sci_dev->owning_port;
606 enum sci_status status;
607
608 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000609 case SCI_DEV_INITIAL:
610 case SCI_DEV_STOPPED:
611 case SCI_DEV_STARTING:
612 case SCI_STP_DEV_IDLE:
613 case SCI_SMP_DEV_IDLE:
614 case SCI_DEV_FAILED:
615 case SCI_DEV_FINAL:
Dan Williams10a09e62011-05-01 15:33:43 -0700616 default:
617 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
618 __func__, state);
619 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000620 case SCI_DEV_READY:
621 case SCI_STP_DEV_AWAIT_RESET:
622 case SCI_DEV_RESETTING:
Dan Williams10a09e62011-05-01 15:33:43 -0700623 status = common_complete_io(sci_port, sci_dev, sci_req);
624 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000625 case SCI_STP_DEV_CMD:
626 case SCI_STP_DEV_NCQ:
627 case SCI_STP_DEV_NCQ_ERROR:
Dan Williams10a09e62011-05-01 15:33:43 -0700628 status = common_complete_io(sci_port, sci_dev, sci_req);
629 if (status != SCI_SUCCESS)
630 break;
631
632 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
633 /* This request causes hardware error, device needs to be Lun Reset.
634 * So here we force the state machine to IDLE state so the rest IOs
635 * can reach RNC state handler, these IOs will be completed by RNC with
636 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
637 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000638 sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
Dan Williams10a09e62011-05-01 15:33:43 -0700639 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000640 sci_change_state(sm, SCI_STP_DEV_IDLE);
Dan Williams10a09e62011-05-01 15:33:43 -0700641 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000642 case SCI_SMP_DEV_CMD:
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;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000646 sci_change_state(sm, SCI_SMP_DEV_IDLE);
Dan Williams10a09e62011-05-01 15:33:43 -0700647 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000648 case SCI_DEV_STOPPING:
Dan Williams10a09e62011-05-01 15:33:43 -0700649 status = common_complete_io(sci_port, sci_dev, sci_req);
650 if (status != SCI_SUCCESS)
651 break;
652
653 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
654 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
655 rnc_destruct_done,
656 sci_dev);
657 break;
658 }
659
660 if (status != SCI_SUCCESS)
661 dev_err(scirdev_to_dev(sci_dev),
662 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
663 "could not complete\n", __func__, sci_port,
664 sci_dev, sci_req, status);
Dan Williams209fae12011-06-13 17:39:44 -0700665 else
666 isci_put_device(sci_dev_to_idev(sci_dev));
Dan Williams10a09e62011-05-01 15:33:43 -0700667
668 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700669}
670
Dan Williams84b9b022011-05-01 15:53:25 -0700671static void scic_sds_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700672{
Dan Williams84b9b022011-05-01 15:53:25 -0700673 struct scic_sds_remote_device *sci_dev = dev;
674
675 /* we need to check if this request is still valid to continue. */
676 if (sci_dev->working_request)
677 scic_controller_continue_io(sci_dev->working_request);
678}
679
680enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
681 struct scic_sds_remote_device *sci_dev,
682 struct scic_sds_request *sci_req)
683{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000684 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williams84b9b022011-05-01 15:53:25 -0700685 enum scic_sds_remote_device_states state = sm->current_state_id;
686 struct scic_sds_port *sci_port = sci_dev->owning_port;
687 enum sci_status status;
688
689 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000690 case SCI_DEV_INITIAL:
691 case SCI_DEV_STOPPED:
692 case SCI_DEV_STARTING:
693 case SCI_SMP_DEV_IDLE:
694 case SCI_SMP_DEV_CMD:
695 case SCI_DEV_STOPPING:
696 case SCI_DEV_FAILED:
697 case SCI_DEV_RESETTING:
698 case SCI_DEV_FINAL:
Dan Williams84b9b022011-05-01 15:53:25 -0700699 default:
700 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
701 __func__, state);
702 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000703 case SCI_STP_DEV_IDLE:
704 case SCI_STP_DEV_CMD:
705 case SCI_STP_DEV_NCQ:
706 case SCI_STP_DEV_NCQ_ERROR:
707 case SCI_STP_DEV_AWAIT_RESET:
Dan Williams84b9b022011-05-01 15:53:25 -0700708 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
709 if (status != SCI_SUCCESS)
710 return status;
711
712 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
713 if (status != SCI_SUCCESS)
714 goto out;
715
Piotr Sawickif4636a72011-05-10 23:50:32 +0000716 status = scic_sds_request_start(sci_req);
Dan Williams84b9b022011-05-01 15:53:25 -0700717 if (status != SCI_SUCCESS)
718 goto out;
719
720 /* Note: If the remote device state is not IDLE this will
721 * replace the request that probably resulted in the task
722 * management request.
723 */
724 sci_dev->working_request = sci_req;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000725 sci_change_state(sm, SCI_STP_DEV_CMD);
Dan Williams84b9b022011-05-01 15:53:25 -0700726
727 /* The remote node context must cleanup the TCi to NCQ mapping
728 * table. The only way to do this correctly is to either write
729 * to the TLCR register or to invalidate and repost the RNC. In
730 * either case the remote node context state machine will take
731 * the correct action when the remote node context is suspended
732 * and later resumed.
733 */
734 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
735 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
736 scic_sds_remote_node_context_resume(&sci_dev->rnc,
737 scic_sds_remote_device_continue_request,
738 sci_dev);
739
740 out:
741 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
742 /* We need to let the controller start request handler know that
743 * it can't post TC yet. We will provide a callback function to
744 * post TC when RNC gets resumed.
745 */
746 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000747 case SCI_DEV_READY:
Dan Williams84b9b022011-05-01 15:53:25 -0700748 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
749 if (status != SCI_SUCCESS)
750 return status;
751
752 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
753 if (status != SCI_SUCCESS)
754 break;
755
756 status = scic_sds_request_start(sci_req);
757 break;
758 }
759 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
760
761 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700762}
763
764/**
765 *
Dan Williams88f3b622011-04-22 19:18:03 -0700766 * @sci_dev:
767 * @request:
768 *
769 * This method takes the request and bulids an appropriate SCU context for the
770 * request and then requests the controller to post the request. none
771 */
772void scic_sds_remote_device_post_request(
773 struct scic_sds_remote_device *sci_dev,
774 u32 request)
775{
776 u32 context;
777
778 context = scic_sds_remote_device_build_command_context(sci_dev, request);
779
780 scic_sds_controller_post_request(
781 scic_sds_remote_device_get_controller(sci_dev),
782 context
783 );
784}
785
Dan Williamsab2e8f72011-04-27 16:32:45 -0700786/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700787 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700788 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700789 */
Dan Williamseb229672011-05-01 14:05:57 -0700790static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700791{
Dan Williamsab2e8f72011-04-27 16:32:45 -0700792 struct scic_sds_remote_device *sci_dev = _dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700793
Dan Williamse6225712011-05-01 16:26:09 -0700794 if (is_remote_device_ready(sci_dev))
795 return;
Dan Williams88f3b622011-04-22 19:18:03 -0700796
Dan Williamse6225712011-05-01 16:26:09 -0700797 /* go 'ready' if we are not already in a ready state */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000798 sci_change_state(&sci_dev->sm, SCI_DEV_READY);
Dan Williams88f3b622011-04-22 19:18:03 -0700799}
800
Dan Williamsab2e8f72011-04-27 16:32:45 -0700801static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
802{
803 struct scic_sds_remote_device *sci_dev = _dev;
804 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
805 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
806
807 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
808 * As a result, avoid sending the ready notification.
809 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000810 if (sci_dev->sm.previous_state_id != SCI_STP_DEV_NCQ)
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000811 isci_remote_device_ready(scic_to_ihost(scic), idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700812}
813
Dan Williams9269e0e2011-05-12 07:42:17 -0700814static void scic_sds_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700815{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000816 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700817
Dan Williams88f3b622011-04-22 19:18:03 -0700818 /* Initial state is a transitional state to the stopped state */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000819 sci_change_state(&sci_dev->sm, SCI_DEV_STOPPED);
Dan Williams88f3b622011-04-22 19:18:03 -0700820}
821
822/**
Dan Williams88f3b622011-04-22 19:18:03 -0700823 * scic_remote_device_destruct() - free remote node context and destruct
824 * @remote_device: This parameter specifies the remote device to be destructed.
825 *
826 * Remote device objects are a limited resource. As such, they must be
827 * protected. Thus calls to construct and destruct are mutually exclusive and
828 * non-reentrant. The return value shall indicate if the device was
829 * successfully destructed or if some failure occurred. enum sci_status This value
830 * is returned if the device is successfully destructed.
831 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
832 * device isn't valid (e.g. it's already been destoryed, the handle isn't
833 * valid, etc.).
834 */
835static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
836{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000837 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williamsb8d82f62011-05-01 14:38:26 -0700838 enum scic_sds_remote_device_states state = sm->current_state_id;
839 struct scic_sds_controller *scic;
840
Edmund Nadolskie3013702011-06-02 00:10:43 +0000841 if (state != SCI_DEV_STOPPED) {
Dan Williamsb8d82f62011-05-01 14:38:26 -0700842 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
843 __func__, state);
844 return SCI_FAILURE_INVALID_STATE;
845 }
846
847 scic = sci_dev->owning_port->owning_controller;
848 scic_sds_controller_free_remote_node_context(scic, sci_dev,
849 sci_dev->rnc.remote_node_index);
850 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000851 sci_change_state(sm, SCI_DEV_FINAL);
Dan Williamsb8d82f62011-05-01 14:38:26 -0700852
853 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -0700854}
855
Dan Williams6f231dd2011-07-02 22:56:22 -0700856/**
857 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -0800858 * @ihost: This parameter specifies the isci host object.
859 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -0700860 *
861 */
Dan Williamsd9c37392011-03-03 17:59:32 -0800862static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700863{
Dan Williamsd9c37392011-03-03 17:59:32 -0800864 dev_dbg(&ihost->pdev->dev,
865 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700866
867 /* There should not be any outstanding io's. All paths to
868 * here should go through isci_remote_device_nuke_requests.
869 * If we hit this condition, we will need a way to complete
870 * io requests in process */
Dan Williams209fae12011-06-13 17:39:44 -0700871 BUG_ON(!list_empty(&idev->reqs_in_process));
Dan Williams6f231dd2011-07-02 22:56:22 -0700872
Dan Williams57f20f42011-04-21 18:14:45 -0700873 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -0800874 list_del_init(&idev->node);
Dan Williams209fae12011-06-13 17:39:44 -0700875 isci_put_device(idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700876}
877
Dan Williams9269e0e2011-05-12 07:42:17 -0700878static void scic_sds_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700879{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000880 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000881 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
882 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700883 u32 prev_state;
884
Dan Williams88f3b622011-04-22 19:18:03 -0700885 /* If we are entering from the stopping state let the SCI User know that
886 * the stop operation has completed.
887 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000888 prev_state = sci_dev->sm.previous_state_id;
889 if (prev_state == SCI_DEV_STOPPING)
Dan Williamsf2088262011-06-16 11:26:12 -0700890 isci_remote_device_deconstruct(scic_to_ihost(scic), idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700891
892 scic_sds_controller_remote_device_stopped(scic, sci_dev);
893}
894
Dan Williams9269e0e2011-05-12 07:42:17 -0700895static void scic_sds_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700896{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000897 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700898 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000899 struct isci_host *ihost = scic_to_ihost(scic);
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000900 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700901
Dan Williams88f3b622011-04-22 19:18:03 -0700902 isci_remote_device_not_ready(ihost, idev,
903 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
904}
905
Dan Williams9269e0e2011-05-12 07:42:17 -0700906static void scic_sds_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700907{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000908 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700909 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000910 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
911 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700912
Dan Williamsab2e8f72011-04-27 16:32:45 -0700913 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000914 sci_change_state(&sci_dev->sm, SCI_STP_DEV_IDLE);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700915 } else if (dev_is_expander(dev)) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000916 sci_change_state(&sci_dev->sm, SCI_SMP_DEV_IDLE);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700917 } else
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000918 isci_remote_device_ready(scic_to_ihost(scic), idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700919}
920
Dan Williams9269e0e2011-05-12 07:42:17 -0700921static void scic_sds_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700922{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000923 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700924 struct domain_device *dev = sci_dev_to_domain(sci_dev);
925
926 if (dev->dev_type == SAS_END_DEV) {
927 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000928 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700929
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000930 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
Dan Williams88f3b622011-04-22 19:18:03 -0700931 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
932 }
933}
934
Dan Williams9269e0e2011-05-12 07:42:17 -0700935static void scic_sds_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700936{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000937 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700938
Dan Williams88f3b622011-04-22 19:18:03 -0700939 scic_sds_remote_node_context_suspend(
940 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
941}
942
Dan Williams9269e0e2011-05-12 07:42:17 -0700943static void scic_sds_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700944{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000945 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700946
947 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
948}
949
Dan Williams9269e0e2011-05-12 07:42:17 -0700950static void scic_sds_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700951{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000952 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700953
Dan Williamsab2e8f72011-04-27 16:32:45 -0700954 sci_dev->working_request = NULL;
955 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
956 /*
957 * Since the RNC is ready, it's alright to finish completion
958 * processing (e.g. signal the remote device is ready). */
959 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
960 } else {
961 scic_sds_remote_node_context_resume(&sci_dev->rnc,
962 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
963 sci_dev);
964 }
965}
966
Dan Williams9269e0e2011-05-12 07:42:17 -0700967static void scic_sds_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700968{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000969 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700970 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
971
972 BUG_ON(sci_dev->working_request == NULL);
973
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000974 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
Dan Williamsab2e8f72011-04-27 16:32:45 -0700975 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
976}
977
Dan Williams9269e0e2011-05-12 07:42:17 -0700978static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700979{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000980 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700981 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
982 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
983
Dan Williamsab2e8f72011-04-27 16:32:45 -0700984 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000985 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
Dan Williamsab2e8f72011-04-27 16:32:45 -0700986 sci_dev->not_ready_reason);
987}
988
Dan Williams9269e0e2011-05-12 07:42:17 -0700989static void scic_sds_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700990{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000991 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700992 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
993
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000994 isci_remote_device_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev));
Dan Williamsab2e8f72011-04-27 16:32:45 -0700995}
996
Dan Williams9269e0e2011-05-12 07:42:17 -0700997static void scic_sds_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700998{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000999 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001000 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1001
1002 BUG_ON(sci_dev->working_request == NULL);
1003
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001004 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
Dan Williamsab2e8f72011-04-27 16:32:45 -07001005 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1006}
1007
Dan Williams9269e0e2011-05-12 07:42:17 -07001008static void scic_sds_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001009{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001010 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001011
1012 sci_dev->working_request = NULL;
1013}
Dan Williams88f3b622011-04-22 19:18:03 -07001014
1015static const struct sci_base_state scic_sds_remote_device_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001016 [SCI_DEV_INITIAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001017 .enter_state = scic_sds_remote_device_initial_state_enter,
1018 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001019 [SCI_DEV_STOPPED] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001020 .enter_state = scic_sds_remote_device_stopped_state_enter,
1021 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001022 [SCI_DEV_STARTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001023 .enter_state = scic_sds_remote_device_starting_state_enter,
1024 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001025 [SCI_DEV_READY] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001026 .enter_state = scic_sds_remote_device_ready_state_enter,
1027 .exit_state = scic_sds_remote_device_ready_state_exit
1028 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001029 [SCI_STP_DEV_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001030 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1031 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001032 [SCI_STP_DEV_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001033 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1034 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001035 [SCI_STP_DEV_NCQ] = { },
1036 [SCI_STP_DEV_NCQ_ERROR] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001037 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1038 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001039 [SCI_STP_DEV_AWAIT_RESET] = { },
1040 [SCI_SMP_DEV_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001041 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1042 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001043 [SCI_SMP_DEV_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001044 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1045 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1046 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001047 [SCI_DEV_STOPPING] = { },
1048 [SCI_DEV_FAILED] = { },
1049 [SCI_DEV_RESETTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -07001050 .enter_state = scic_sds_remote_device_resetting_state_enter,
1051 .exit_state = scic_sds_remote_device_resetting_state_exit
1052 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001053 [SCI_DEV_FINAL] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001054};
1055
1056/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001057 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001058 * @sci_port: SAS/SATA port through which this device is accessed.
1059 * @sci_dev: remote device to construct
1060 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001061 * This routine just performs benign initialization and does not
1062 * allocate the remote_node_context which is left to
1063 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1064 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001065 */
1066static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1067 struct scic_sds_remote_device *sci_dev)
1068{
1069 sci_dev->owning_port = sci_port;
1070 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001071
Edmund Nadolski12ef6542011-06-02 00:10:50 +00001072 sci_init_sm(&sci_dev->sm, scic_sds_remote_device_state_table, SCI_DEV_INITIAL);
Dan Williams88f3b622011-04-22 19:18:03 -07001073
1074 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1075 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001076}
1077
1078/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001079 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001080 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001081 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1082 * the device is known to the SCI Core since it is contained in the
1083 * scic_phy object. Remote node context(s) is/are a global resource
1084 * allocated by this routine, freed by scic_remote_device_destruct().
1085 *
1086 * Returns:
1087 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1088 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1089 * sata-only controller instance.
1090 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001091 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001092static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1093 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001094{
1095 enum sci_status status;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001096 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001097
Dan Williamsb87ee302011-04-25 11:48:29 -07001098 scic_remote_device_construct(sci_port, sci_dev);
1099
Dan Williams88f3b622011-04-22 19:18:03 -07001100 /*
1101 * This information is request to determine how many remote node context
1102 * entries will be needed to store the remote node.
1103 */
Dan Williams88f3b622011-04-22 19:18:03 -07001104 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001105 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1106 sci_dev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001107 &sci_dev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001108
Dan Williamsa1a113b2011-04-21 18:44:45 -07001109 if (status != SCI_SUCCESS)
1110 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001111
Dan Williamsab2e8f72011-04-27 16:32:45 -07001112 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1113 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1114 /* pass */;
1115 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001116 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001117
Dan Williamsa1a113b2011-04-21 18:44:45 -07001118 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001119
Dan Williamsa1a113b2011-04-21 18:44:45 -07001120 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1121 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001122
Dan Williamsa1a113b2011-04-21 18:44:45 -07001123 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001124}
1125
Dan Williams88f3b622011-04-22 19:18:03 -07001126/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001127 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001128 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001129 * Remote node context(s) is/are a global resource allocated by this
1130 * routine, freed by scic_remote_device_destruct().
1131 *
1132 * Returns:
1133 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1134 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1135 * sata-only controller instance.
1136 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001137 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001138static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001139 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001140{
Dan Williamsa1a113b2011-04-21 18:44:45 -07001141 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001142 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001143
Dan Williamsb87ee302011-04-25 11:48:29 -07001144 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001145
Dan Williamsab2e8f72011-04-27 16:32:45 -07001146 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1147 sci_dev,
1148 &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001149 if (status != SCI_SUCCESS)
1150 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001151
Dan Williamsab2e8f72011-04-27 16:32:45 -07001152 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1153 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1154 /* pass */;
1155 else
1156 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001157
Dan Williamsa1a113b2011-04-21 18:44:45 -07001158 /*
1159 * For SAS-2 the physical link rate is actually a logical link
1160 * rate that incorporates multiplexing. The SCU doesn't
1161 * incorporate multiplexing and for the purposes of the
1162 * connection the logical link rate is that same as the
1163 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1164 * one another, so this code works for both situations. */
1165 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001166 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001167
Dan Williamsa1a113b2011-04-21 18:44:45 -07001168 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1169 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001170
Dan Williamsab2e8f72011-04-27 16:32:45 -07001171 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001172}
1173
1174/**
1175 * scic_remote_device_start() - This method will start the supplied remote
1176 * device. This method enables normal IO requests to flow through to the
1177 * remote device.
1178 * @remote_device: This parameter specifies the device to be started.
1179 * @timeout: This parameter specifies the number of milliseconds in which the
1180 * start operation should complete.
1181 *
1182 * An indication of whether the device was successfully started. SCI_SUCCESS
1183 * This value is returned if the device was successfully started.
1184 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1185 * the device when there have been no phys added to it.
1186 */
1187static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
Dan Williamseb229672011-05-01 14:05:57 -07001188 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001189{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001190 struct sci_base_state_machine *sm = &sci_dev->sm;
Dan Williamseb229672011-05-01 14:05:57 -07001191 enum scic_sds_remote_device_states state = sm->current_state_id;
1192 enum sci_status status;
1193
Edmund Nadolskie3013702011-06-02 00:10:43 +00001194 if (state != SCI_DEV_STOPPED) {
Dan Williamseb229672011-05-01 14:05:57 -07001195 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1196 __func__, state);
1197 return SCI_FAILURE_INVALID_STATE;
1198 }
1199
1200 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1201 remote_device_resume_done,
1202 sci_dev);
1203 if (status != SCI_SUCCESS)
1204 return status;
1205
Edmund Nadolskie3013702011-06-02 00:10:43 +00001206 sci_change_state(sm, SCI_DEV_STARTING);
Dan Williamseb229672011-05-01 14:05:57 -07001207
1208 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001209}
Dan Williams6f231dd2011-07-02 22:56:22 -07001210
Dan Williams00d680e2011-04-25 14:29:29 -07001211static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1212 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001213{
Dan Williamse5313812011-05-07 10:11:43 -07001214 struct scic_sds_port *sci_port = &iport->sci;
Dan Williams00d680e2011-04-25 14:29:29 -07001215 struct isci_host *ihost = iport->isci_host;
1216 struct domain_device *dev = idev->domain_dev;
1217 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001218
Dan Williams00d680e2011-04-25 14:29:29 -07001219 if (dev->parent && dev_is_expander(dev->parent))
1220 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1221 else
1222 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001223
1224 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001225 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1226 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001227
1228 return status;
1229 }
1230
Dan Williams6f231dd2011-07-02 22:56:22 -07001231 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001232 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001233
Dan Williams00d680e2011-04-25 14:29:29 -07001234 if (status != SCI_SUCCESS)
1235 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1236 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001237
1238 return status;
1239}
1240
Dan Williams4393aa42011-03-31 13:10:44 -07001241void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001242{
1243 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001244
Dan Williams4393aa42011-03-31 13:10:44 -07001245 dev_dbg(&ihost->pdev->dev,
1246 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001247
1248 /* Cleanup all requests pending for this device. */
Dan Williams980d3ae2011-06-20 15:11:22 -07001249 isci_terminate_pending_requests(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001250
Dan Williams4393aa42011-03-31 13:10:44 -07001251 dev_dbg(&ihost->pdev->dev,
1252 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001253}
1254
Dan Williams6f231dd2011-07-02 22:56:22 -07001255/**
1256 * This function builds the isci_remote_device when a libsas dev_found message
1257 * is received.
1258 * @isci_host: This parameter specifies the isci host object.
1259 * @port: This parameter specifies the isci_port conected to this device.
1260 *
1261 * pointer to new isci_remote_device.
1262 */
1263static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001264isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001265{
Dan Williamsd9c37392011-03-03 17:59:32 -08001266 struct isci_remote_device *idev;
1267 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001268
Dan Williamsd9c37392011-03-03 17:59:32 -08001269 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001270 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001271 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1272 break;
1273 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001274
Dan Williamsd9c37392011-03-03 17:59:32 -08001275 if (i >= SCI_MAX_REMOTE_DEVICES) {
1276 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001277 return NULL;
1278 }
1279
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001280 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1281 return NULL;
1282
1283 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1284 return NULL;
1285
Dan Williamsd9c37392011-03-03 17:59:32 -08001286 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001287}
Dan Williams6f231dd2011-07-02 22:56:22 -07001288
Dan Williams209fae12011-06-13 17:39:44 -07001289void isci_remote_device_release(struct kref *kref)
1290{
1291 struct isci_remote_device *idev = container_of(kref, typeof(*idev), kref);
1292 struct isci_host *ihost = idev->isci_port->isci_host;
1293
1294 idev->domain_dev = NULL;
1295 idev->isci_port = NULL;
1296 clear_bit(IDEV_START_PENDING, &idev->flags);
1297 clear_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williamsf2088262011-06-16 11:26:12 -07001298 clear_bit(IDEV_IO_READY, &idev->flags);
Dan Williams209fae12011-06-13 17:39:44 -07001299 clear_bit(IDEV_GONE, &idev->flags);
1300 clear_bit(IDEV_EH, &idev->flags);
1301 smp_mb__before_clear_bit();
1302 clear_bit(IDEV_ALLOCATED, &idev->flags);
1303 wake_up(&ihost->eventq);
1304}
1305
Dan Williams6f231dd2011-07-02 22:56:22 -07001306/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001307 * isci_remote_device_stop() - This function is called internally to stop the
1308 * remote device.
1309 * @isci_host: This parameter specifies the isci host object.
1310 * @isci_device: This parameter specifies the remote device.
1311 *
1312 * The status of the scic request to stop.
1313 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001314enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001315{
1316 enum sci_status status;
1317 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001318
Dan Williams6ad31fe2011-03-04 12:10:29 -08001319 dev_dbg(&ihost->pdev->dev,
1320 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001321
Dan Williams209fae12011-06-13 17:39:44 -07001322 spin_lock_irqsave(&ihost->scic_lock, flags);
1323 idev->domain_dev->lldd_dev = NULL; /* disable new lookups */
1324 set_bit(IDEV_GONE, &idev->flags);
Dan Williams209fae12011-06-13 17:39:44 -07001325 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001326
1327 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001328 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001329
Dan Williams6ad31fe2011-03-04 12:10:29 -08001330 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001331
Dan Williams6ad31fe2011-03-04 12:10:29 -08001332 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07001333 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001334 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001335
1336 /* Wait for the stop complete callback. */
Dan Williams209fae12011-06-13 17:39:44 -07001337 if (WARN_ONCE(status != SCI_SUCCESS, "failed to stop device\n"))
1338 /* nothing to wait for */;
1339 else
Dan Williams6ad31fe2011-03-04 12:10:29 -08001340 wait_for_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001341
Dan Williams6f231dd2011-07-02 22:56:22 -07001342 return status;
1343}
1344
1345/**
1346 * isci_remote_device_gone() - This function is called by libsas when a domain
1347 * device is removed.
1348 * @domain_device: This parameter specifies the libsas domain device.
1349 *
1350 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001351void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001352{
Dan Williams4393aa42011-03-31 13:10:44 -07001353 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001354 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001355
Dan Williams6ad31fe2011-03-04 12:10:29 -08001356 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001357 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001358 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001359
Dan Williams6ad31fe2011-03-04 12:10:29 -08001360 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001361}
1362
1363
1364/**
1365 * isci_remote_device_found() - This function is called by libsas when a remote
1366 * device is discovered. A remote device object is created and started. the
1367 * function then sleeps until the sci core device started message is
1368 * received.
1369 * @domain_device: This parameter specifies the libsas domain device.
1370 *
1371 * status, zero indicates success.
1372 */
1373int isci_remote_device_found(struct domain_device *domain_dev)
1374{
Dan Williams4393aa42011-03-31 13:10:44 -07001375 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001376 struct isci_port *isci_port;
1377 struct isci_phy *isci_phy;
1378 struct asd_sas_port *sas_port;
1379 struct asd_sas_phy *sas_phy;
1380 struct isci_remote_device *isci_device;
1381 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001382
Dan Williams6f231dd2011-07-02 22:56:22 -07001383 dev_dbg(&isci_host->pdev->dev,
1384 "%s: domain_device = %p\n", __func__, domain_dev);
1385
Dan Williams0cf89d12011-02-18 09:25:07 -08001386 wait_for_start(isci_host);
1387
Dan Williams6f231dd2011-07-02 22:56:22 -07001388 sas_port = domain_dev->port;
1389 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1390 port_phy_el);
1391 isci_phy = to_isci_phy(sas_phy);
1392 isci_port = isci_phy->isci_port;
1393
1394 /* we are being called for a device on this port,
1395 * so it has to come up eventually
1396 */
1397 wait_for_completion(&isci_port->start_complete);
1398
1399 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1400 (isci_stopped == isci_port_get_state(isci_port)))
1401 return -ENODEV;
1402
1403 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001404 if (!isci_device)
1405 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001406
Dan Williams209fae12011-06-13 17:39:44 -07001407 kref_init(&isci_device->kref);
Dan Williams6f231dd2011-07-02 22:56:22 -07001408 INIT_LIST_HEAD(&isci_device->node);
Dan Williams209fae12011-06-13 17:39:44 -07001409
1410 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001411 isci_device->domain_dev = domain_dev;
1412 isci_device->isci_port = isci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001413 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1414
Dan Williams6ad31fe2011-03-04 12:10:29 -08001415 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001416 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001417
Dan Williams6f231dd2011-07-02 22:56:22 -07001418 dev_dbg(&isci_host->pdev->dev,
1419 "%s: isci_device = %p\n",
1420 __func__, isci_device);
1421
Dan Williams209fae12011-06-13 17:39:44 -07001422 if (status == SCI_SUCCESS) {
1423 /* device came up, advertise it to the world */
1424 domain_dev->lldd_dev = isci_device;
1425 } else
1426 isci_put_device(isci_device);
1427 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001428
Dan Williams6ad31fe2011-03-04 12:10:29 -08001429 /* wait for the device ready callback. */
1430 wait_for_device_start(isci_host, isci_device);
1431
Dan Williams209fae12011-06-13 17:39:44 -07001432 return status == SCI_SUCCESS ? 0 : -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001433}
1434/**
1435 * isci_device_is_reset_pending() - This function will check if there is any
1436 * pending reset condition on the device.
1437 * @request: This parameter is the isci_device object.
1438 *
1439 * true if there is a reset pending for the device.
1440 */
1441bool isci_device_is_reset_pending(
1442 struct isci_host *isci_host,
1443 struct isci_remote_device *isci_device)
1444{
1445 struct isci_request *isci_request;
1446 struct isci_request *tmp_req;
1447 bool reset_is_pending = false;
1448 unsigned long flags;
1449
1450 dev_dbg(&isci_host->pdev->dev,
1451 "%s: isci_device = %p\n", __func__, isci_device);
1452
1453 spin_lock_irqsave(&isci_host->scic_lock, flags);
1454
1455 /* Check for reset on all pending requests. */
1456 list_for_each_entry_safe(isci_request, tmp_req,
1457 &isci_device->reqs_in_process, dev_node) {
1458 dev_dbg(&isci_host->pdev->dev,
1459 "%s: isci_device = %p request = %p\n",
1460 __func__, isci_device, isci_request);
1461
1462 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001463 struct sas_task *task = isci_request_access_task(
1464 isci_request);
1465
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001466 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001467 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1468 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001469 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001470 }
1471 }
1472
1473 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1474
1475 dev_dbg(&isci_host->pdev->dev,
1476 "%s: isci_device = %p reset_is_pending = %d\n",
1477 __func__, isci_device, reset_is_pending);
1478
1479 return reset_is_pending;
1480}
1481
1482/**
1483 * isci_device_clear_reset_pending() - This function will clear if any pending
1484 * reset condition flags on the device.
1485 * @request: This parameter is the isci_device object.
1486 *
1487 * true if there is a reset pending for the device.
1488 */
Dan Williams4393aa42011-03-31 13:10:44 -07001489void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001490{
1491 struct isci_request *isci_request;
1492 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07001493 unsigned long flags = 0;
1494
Dan Williams4393aa42011-03-31 13:10:44 -07001495 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1496 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07001497
Dan Williams4393aa42011-03-31 13:10:44 -07001498 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001499
1500 /* Clear reset pending on all pending requests. */
1501 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07001502 &idev->reqs_in_process, dev_node) {
1503 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1504 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001505
1506 if (isci_request->ttype == io_task) {
1507
1508 unsigned long flags2;
1509 struct sas_task *task = isci_request_access_task(
1510 isci_request);
1511
1512 spin_lock_irqsave(&task->task_state_lock, flags2);
1513 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1514 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1515 }
1516 }
Dan Williams4393aa42011-03-31 13:10:44 -07001517 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001518}