blob: b1a8000a5ef86f87467de71ed89878c917ee7ade [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>
Bartek Nowakowski7e629842012-01-04 01:33:20 -080056#include <linux/bitops.h>
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070058#include "port.h"
59#include "remote_device.h"
60#include "request.h"
Dan Williams88f3b622011-04-22 19:18:03 -070061#include "remote_node_context.h"
Dan Williams88f3b622011-04-22 19:18:03 -070062#include "scu_event_codes.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070063#include "task.h"
64
Dan Williamsd7a0ccd2012-02-10 01:18:44 -080065#undef C
66#define C(a) (#a)
67const char *dev_state_name(enum sci_remote_device_states state)
68{
69 static const char * const strings[] = REMOTE_DEV_STATES;
70
71 return strings[state];
72}
73#undef C
74
Dan Williamsab2e8f72011-04-27 16:32:45 -070075/**
Dan Williamsd9dcb4b2011-06-30 17:38:32 -070076 * isci_remote_device_not_ready() - This function is called by the ihost when
Dan Williamsab2e8f72011-04-27 16:32:45 -070077 * the remote device is not ready. We mark the isci device as ready (not
78 * "ready_for_io") and signal the waiting proccess.
79 * @isci_host: This parameter specifies the isci host object.
80 * @isci_device: This parameter specifies the remote device
81 *
Dan Williams89a73012011-06-30 19:14:33 -070082 * sci_lock is held on entrance to this function.
Dan Williamsab2e8f72011-04-27 16:32:45 -070083 */
84static void isci_remote_device_not_ready(struct isci_host *ihost,
85 struct isci_remote_device *idev, u32 reason)
86{
James Bottomleya5ec7f862011-07-03 14:14:45 -050087 struct isci_request *ireq;
Jeff Skirvin9274f452011-06-23 17:09:02 -070088
Dan Williamsab2e8f72011-04-27 16:32:45 -070089 dev_dbg(&ihost->pdev->dev,
90 "%s: isci_device = %p\n", __func__, idev);
91
Jeff Skirvin9274f452011-06-23 17:09:02 -070092 switch (reason) {
93 case SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED:
Dan Williams209fae12011-06-13 17:39:44 -070094 set_bit(IDEV_GONE, &idev->flags);
Jeff Skirvin9274f452011-06-23 17:09:02 -070095 break;
96 case SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED:
97 set_bit(IDEV_IO_NCQERROR, &idev->flags);
98
99 /* Kill all outstanding requests for the device. */
100 list_for_each_entry(ireq, &idev->reqs_in_process, dev_node) {
101
102 dev_dbg(&ihost->pdev->dev,
103 "%s: isci_device = %p request = %p\n",
104 __func__, idev, ireq);
105
Dan Williams89a73012011-06-30 19:14:33 -0700106 sci_controller_terminate_request(ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700107 idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700108 ireq);
Jeff Skirvin9274f452011-06-23 17:09:02 -0700109 }
110 /* Fall through into the default case... */
111 default:
Dan Williamsf2088262011-06-16 11:26:12 -0700112 clear_bit(IDEV_IO_READY, &idev->flags);
Jeff Skirvin9274f452011-06-23 17:09:02 -0700113 break;
114 }
Dan Williamsab2e8f72011-04-27 16:32:45 -0700115}
116
117/**
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700118 * isci_remote_device_ready() - This function is called by the ihost when the
Dan Williamsab2e8f72011-04-27 16:32:45 -0700119 * remote device is ready. We mark the isci device as ready and signal the
120 * waiting proccess.
121 * @ihost: our valid isci_host
122 * @idev: remote device
123 *
124 */
125static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
126{
127 dev_dbg(&ihost->pdev->dev,
128 "%s: idev = %p\n", __func__, idev);
129
Jeff Skirvin9274f452011-06-23 17:09:02 -0700130 clear_bit(IDEV_IO_NCQERROR, &idev->flags);
Dan Williamsf2088262011-06-16 11:26:12 -0700131 set_bit(IDEV_IO_READY, &idev->flags);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700132 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
133 wake_up(&ihost->eventq);
134}
135
Dan Williamsec575662011-05-01 14:19:25 -0700136/* called once the remote node context is ready to be freed.
137 * The remote device can now report that its stop operation is complete. none
138 */
139static void rnc_destruct_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700140{
Dan Williams78a6f062011-06-30 16:31:37 -0700141 struct isci_remote_device *idev = _dev;
Dan Williamsec575662011-05-01 14:19:25 -0700142
Dan Williams78a6f062011-06-30 16:31:37 -0700143 BUG_ON(idev->started_request_count != 0);
144 sci_change_state(&idev->sm, SCI_DEV_STOPPED);
Dan Williamsec575662011-05-01 14:19:25 -0700145}
146
Dan Williams89a73012011-06-30 19:14:33 -0700147static enum sci_status sci_remote_device_terminate_requests(struct isci_remote_device *idev)
Dan Williamsec575662011-05-01 14:19:25 -0700148{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700149 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsec575662011-05-01 14:19:25 -0700150 enum sci_status status = SCI_SUCCESS;
Dan Williams76802ce2011-06-29 09:45:48 -0700151 u32 i;
Dan Williamsec575662011-05-01 14:19:25 -0700152
Dan Williams76802ce2011-06-29 09:45:48 -0700153 for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
Dan Williamsdb056252011-06-17 14:18:39 -0700154 struct isci_request *ireq = ihost->reqs[i];
Dan Williamsec575662011-05-01 14:19:25 -0700155 enum sci_status s;
156
Dan Williamsdb056252011-06-17 14:18:39 -0700157 if (!test_bit(IREQ_ACTIVE, &ireq->flags) ||
Dan Williams78a6f062011-06-30 16:31:37 -0700158 ireq->target_device != idev)
Dan Williamsec575662011-05-01 14:19:25 -0700159 continue;
Dan Williamsdb056252011-06-17 14:18:39 -0700160
Dan Williams89a73012011-06-30 19:14:33 -0700161 s = sci_controller_terminate_request(ihost, idev, ireq);
Dan Williamsec575662011-05-01 14:19:25 -0700162 if (s != SCI_SUCCESS)
163 status = s;
164 }
165
166 return status;
167}
168
Dan Williams89a73012011-06-30 19:14:33 -0700169enum sci_status sci_remote_device_stop(struct isci_remote_device *idev,
Dan Williamsec575662011-05-01 14:19:25 -0700170 u32 timeout)
171{
Dan Williams78a6f062011-06-30 16:31:37 -0700172 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700173 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamsec575662011-05-01 14:19:25 -0700174
175 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000176 case SCI_DEV_INITIAL:
177 case SCI_DEV_FAILED:
178 case SCI_DEV_FINAL:
Dan Williamsec575662011-05-01 14:19:25 -0700179 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800180 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
181 __func__, dev_state_name(state));
Dan Williamsec575662011-05-01 14:19:25 -0700182 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000183 case SCI_DEV_STOPPED:
Dan Williamsec575662011-05-01 14:19:25 -0700184 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000185 case SCI_DEV_STARTING:
Dan Williamsec575662011-05-01 14:19:25 -0700186 /* device not started so there had better be no requests */
Dan Williams78a6f062011-06-30 16:31:37 -0700187 BUG_ON(idev->started_request_count != 0);
Dan Williams89a73012011-06-30 19:14:33 -0700188 sci_remote_node_context_destruct(&idev->rnc,
Dan Williams78a6f062011-06-30 16:31:37 -0700189 rnc_destruct_done, idev);
Dan Williamsec575662011-05-01 14:19:25 -0700190 /* Transition to the stopping state and wait for the
191 * remote node to complete being posted and invalidated.
192 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000193 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williamsec575662011-05-01 14:19:25 -0700194 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000195 case SCI_DEV_READY:
196 case SCI_STP_DEV_IDLE:
197 case SCI_STP_DEV_CMD:
198 case SCI_STP_DEV_NCQ:
199 case SCI_STP_DEV_NCQ_ERROR:
200 case SCI_STP_DEV_AWAIT_RESET:
201 case SCI_SMP_DEV_IDLE:
202 case SCI_SMP_DEV_CMD:
203 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williams78a6f062011-06-30 16:31:37 -0700204 if (idev->started_request_count == 0) {
Dan Williams89a73012011-06-30 19:14:33 -0700205 sci_remote_node_context_destruct(&idev->rnc,
Dan Williams78a6f062011-06-30 16:31:37 -0700206 rnc_destruct_done, idev);
Dan Williamsec575662011-05-01 14:19:25 -0700207 return SCI_SUCCESS;
208 } else
Dan Williams89a73012011-06-30 19:14:33 -0700209 return sci_remote_device_terminate_requests(idev);
Dan Williamsec575662011-05-01 14:19:25 -0700210 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000211 case SCI_DEV_STOPPING:
Dan Williamsec575662011-05-01 14:19:25 -0700212 /* All requests should have been terminated, but if there is an
213 * attempt to stop a device already in the stopping state, then
214 * try again to terminate.
215 */
Dan Williams89a73012011-06-30 19:14:33 -0700216 return sci_remote_device_terminate_requests(idev);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000217 case SCI_DEV_RESETTING:
218 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williamsec575662011-05-01 14:19:25 -0700219 return SCI_SUCCESS;
220 }
Dan Williams88f3b622011-04-22 19:18:03 -0700221}
Dan Williams6f231dd2011-07-02 22:56:22 -0700222
Dan Williams89a73012011-06-30 19:14:33 -0700223enum sci_status sci_remote_device_reset(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700224{
Dan Williams78a6f062011-06-30 16:31:37 -0700225 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700226 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700227
228 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000229 case SCI_DEV_INITIAL:
230 case SCI_DEV_STOPPED:
231 case SCI_DEV_STARTING:
232 case SCI_SMP_DEV_IDLE:
233 case SCI_SMP_DEV_CMD:
234 case SCI_DEV_STOPPING:
235 case SCI_DEV_FAILED:
236 case SCI_DEV_RESETTING:
237 case SCI_DEV_FINAL:
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700238 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800239 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
240 __func__, dev_state_name(state));
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700241 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000242 case SCI_DEV_READY:
243 case SCI_STP_DEV_IDLE:
244 case SCI_STP_DEV_CMD:
245 case SCI_STP_DEV_NCQ:
246 case SCI_STP_DEV_NCQ_ERROR:
247 case SCI_STP_DEV_AWAIT_RESET:
248 sci_change_state(sm, SCI_DEV_RESETTING);
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700249 return SCI_SUCCESS;
250 }
Dan Williams88f3b622011-04-22 19:18:03 -0700251}
252
Dan Williams89a73012011-06-30 19:14:33 -0700253enum sci_status sci_remote_device_reset_complete(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700254{
Dan Williams78a6f062011-06-30 16:31:37 -0700255 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700256 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams88f3b622011-04-22 19:18:03 -0700257
Edmund Nadolskie3013702011-06-02 00:10:43 +0000258 if (state != SCI_DEV_RESETTING) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800259 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
260 __func__, dev_state_name(state));
Dan Williams81515182011-05-01 14:53:00 -0700261 return SCI_FAILURE_INVALID_STATE;
262 }
263
Edmund Nadolskie3013702011-06-02 00:10:43 +0000264 sci_change_state(sm, SCI_DEV_READY);
Dan Williams81515182011-05-01 14:53:00 -0700265 return SCI_SUCCESS;
266}
Dan Williams88f3b622011-04-22 19:18:03 -0700267
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800268enum sci_status sci_remote_device_suspend(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700269{
Dan Williams89a73012011-06-30 19:14:33 -0700270 return sci_remote_node_context_suspend(&idev->rnc,
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800271 SCI_SOFTWARE_SUSPENSION,
272 SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT,
273 NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700274}
275
Dan Williams89a73012011-06-30 19:14:33 -0700276enum sci_status sci_remote_device_frame_handler(struct isci_remote_device *idev,
Dan Williams01bec772011-05-01 16:51:11 -0700277 u32 frame_index)
Dan Williams88f3b622011-04-22 19:18:03 -0700278{
Dan Williams78a6f062011-06-30 16:31:37 -0700279 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700280 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700281 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams01bec772011-05-01 16:51:11 -0700282 enum sci_status status;
283
284 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000285 case SCI_DEV_INITIAL:
286 case SCI_DEV_STOPPED:
287 case SCI_DEV_STARTING:
288 case SCI_STP_DEV_IDLE:
289 case SCI_SMP_DEV_IDLE:
290 case SCI_DEV_FINAL:
Dan Williams01bec772011-05-01 16:51:11 -0700291 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800292 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
293 __func__, dev_state_name(state));
Dan Williams01bec772011-05-01 16:51:11 -0700294 /* Return the frame back to the controller */
Dan Williams89a73012011-06-30 19:14:33 -0700295 sci_controller_release_frame(ihost, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700296 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000297 case SCI_DEV_READY:
298 case SCI_STP_DEV_NCQ_ERROR:
299 case SCI_STP_DEV_AWAIT_RESET:
300 case SCI_DEV_STOPPING:
301 case SCI_DEV_FAILED:
302 case SCI_DEV_RESETTING: {
Dan Williams5076a1a2011-06-27 14:57:03 -0700303 struct isci_request *ireq;
Dave Jiang2d9c2242011-05-04 18:45:05 -0700304 struct ssp_frame_hdr hdr;
305 void *frame_header;
306 ssize_t word_cnt;
Dan Williams01bec772011-05-01 16:51:11 -0700307
Dan Williams89a73012011-06-30 19:14:33 -0700308 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williams01bec772011-05-01 16:51:11 -0700309 frame_index,
Dave Jiang2d9c2242011-05-04 18:45:05 -0700310 &frame_header);
Dan Williams01bec772011-05-01 16:51:11 -0700311 if (status != SCI_SUCCESS)
312 return status;
313
Dave Jiang2d9c2242011-05-04 18:45:05 -0700314 word_cnt = sizeof(hdr) / sizeof(u32);
315 sci_swab32_cpy(&hdr, frame_header, word_cnt);
316
Dan Williams89a73012011-06-30 19:14:33 -0700317 ireq = sci_request_by_tag(ihost, be16_to_cpu(hdr.tag));
Dan Williams78a6f062011-06-30 16:31:37 -0700318 if (ireq && ireq->target_device == idev) {
Dan Williams01bec772011-05-01 16:51:11 -0700319 /* The IO request is now in charge of releasing the frame */
Dan Williams89a73012011-06-30 19:14:33 -0700320 status = sci_io_request_frame_handler(ireq, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700321 } else {
322 /* We could not map this tag to a valid IO
323 * request Just toss the frame and continue
324 */
Dan Williams89a73012011-06-30 19:14:33 -0700325 sci_controller_release_frame(ihost, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700326 }
327 break;
328 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000329 case SCI_STP_DEV_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700330 struct dev_to_host_fis *hdr;
Dan Williams01bec772011-05-01 16:51:11 -0700331
Dan Williams89a73012011-06-30 19:14:33 -0700332 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williams01bec772011-05-01 16:51:11 -0700333 frame_index,
334 (void **)&hdr);
335 if (status != SCI_SUCCESS)
336 return status;
337
Dave Jiange76d6182011-05-04 15:02:03 -0700338 if (hdr->fis_type == FIS_SETDEVBITS &&
339 (hdr->status & ATA_ERR)) {
Dan Williams78a6f062011-06-30 16:31:37 -0700340 idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
Dan Williams01bec772011-05-01 16:51:11 -0700341
342 /* TODO Check sactive and complete associated IO if any. */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000343 sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
Dave Jiange76d6182011-05-04 15:02:03 -0700344 } else if (hdr->fis_type == FIS_REGD2H &&
345 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700346 /*
347 * Some devices return D2H FIS when an NCQ error is detected.
348 * Treat this like an SDB error FIS ready reason.
349 */
Dan Williams78a6f062011-06-30 16:31:37 -0700350 idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
351 sci_change_state(&idev->sm, SCI_STP_DEV_NCQ_ERROR);
Dan Williams01bec772011-05-01 16:51:11 -0700352 } else
353 status = SCI_FAILURE;
354
Dan Williams89a73012011-06-30 19:14:33 -0700355 sci_controller_release_frame(ihost, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700356 break;
357 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000358 case SCI_STP_DEV_CMD:
359 case SCI_SMP_DEV_CMD:
Dan Williams01bec772011-05-01 16:51:11 -0700360 /* The device does not process any UF received from the hardware while
361 * in this state. All unsolicited frames are forwarded to the io request
362 * object.
363 */
Dan Williams89a73012011-06-30 19:14:33 -0700364 status = sci_io_request_frame_handler(idev->working_request, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700365 break;
366 }
367
368 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700369}
370
Dan Williams78a6f062011-06-30 16:31:37 -0700371static bool is_remote_device_ready(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700372{
Dan Williamse6225712011-05-01 16:26:09 -0700373
Dan Williams78a6f062011-06-30 16:31:37 -0700374 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700375 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamse6225712011-05-01 16:26:09 -0700376
377 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000378 case SCI_DEV_READY:
379 case SCI_STP_DEV_IDLE:
380 case SCI_STP_DEV_CMD:
381 case SCI_STP_DEV_NCQ:
382 case SCI_STP_DEV_NCQ_ERROR:
383 case SCI_STP_DEV_AWAIT_RESET:
384 case SCI_SMP_DEV_IDLE:
385 case SCI_SMP_DEV_CMD:
Dan Williamse6225712011-05-01 16:26:09 -0700386 return true;
387 default:
388 return false;
389 }
390}
391
Dan Williamsb50102d2011-09-30 18:52:19 -0700392/*
393 * called once the remote node context has transisitioned to a ready
394 * state (after suspending RX and/or TX due to early D2H fis)
395 */
396static void atapi_remote_device_resume_done(void *_dev)
397{
398 struct isci_remote_device *idev = _dev;
399 struct isci_request *ireq = idev->working_request;
400
401 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
402}
403
Dan Williams89a73012011-06-30 19:14:33 -0700404enum sci_status sci_remote_device_event_handler(struct isci_remote_device *idev,
Dan Williamse6225712011-05-01 16:26:09 -0700405 u32 event_code)
406{
Dan Williamse6225712011-05-01 16:26:09 -0700407 enum sci_status status;
408
409 switch (scu_get_event_type(event_code)) {
410 case SCU_EVENT_TYPE_RNC_OPS_MISC:
411 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
412 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
Dan Williams89a73012011-06-30 19:14:33 -0700413 status = sci_remote_node_context_event_handler(&idev->rnc, event_code);
Dan Williamse6225712011-05-01 16:26:09 -0700414 break;
415 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
416 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
417 status = SCI_SUCCESS;
418
419 /* Suspend the associated RNC */
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800420 sci_remote_node_context_suspend(
421 &idev->rnc,
422 SCI_SOFTWARE_SUSPENSION,
423 SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT,
424 NULL, NULL);
Dan Williamse6225712011-05-01 16:26:09 -0700425
Dan Williams78a6f062011-06-30 16:31:37 -0700426 dev_dbg(scirdev_to_dev(idev),
Dan Williamse6225712011-05-01 16:26:09 -0700427 "%s: device: %p event code: %x: %s\n",
Dan Williams78a6f062011-06-30 16:31:37 -0700428 __func__, idev, event_code,
429 is_remote_device_ready(idev)
Dan Williamse6225712011-05-01 16:26:09 -0700430 ? "I_T_Nexus_Timeout event"
431 : "I_T_Nexus_Timeout event in wrong state");
432
433 break;
434 }
435 /* Else, fall through and treat as unhandled... */
436 default:
Dan Williams78a6f062011-06-30 16:31:37 -0700437 dev_dbg(scirdev_to_dev(idev),
Dan Williamse6225712011-05-01 16:26:09 -0700438 "%s: device: %p event code: %x: %s\n",
Dan Williams78a6f062011-06-30 16:31:37 -0700439 __func__, idev, event_code,
440 is_remote_device_ready(idev)
Dan Williamse6225712011-05-01 16:26:09 -0700441 ? "unexpected event"
442 : "unexpected event in wrong state");
443 status = SCI_FAILURE_INVALID_STATE;
444 break;
445 }
446
447 if (status != SCI_SUCCESS)
448 return status;
449
Dan Williamse6225712011-05-01 16:26:09 -0700450 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700451}
452
Dan Williams89a73012011-06-30 19:14:33 -0700453static void sci_remote_device_start_request(struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700454 struct isci_request *ireq,
Dan Williams18606552011-05-01 14:57:11 -0700455 enum sci_status status)
Dan Williams88f3b622011-04-22 19:18:03 -0700456{
Dan Williams78a6f062011-06-30 16:31:37 -0700457 struct isci_port *iport = idev->owning_port;
Dan Williams18606552011-05-01 14:57:11 -0700458
459 /* cleanup requests that failed after starting on the port */
460 if (status != SCI_SUCCESS)
Dan Williams89a73012011-06-30 19:14:33 -0700461 sci_port_complete_io(iport, idev, ireq);
Dan Williams209fae12011-06-13 17:39:44 -0700462 else {
Dan Williams78a6f062011-06-30 16:31:37 -0700463 kref_get(&idev->kref);
Dan Williams34a99152011-07-01 02:25:15 -0700464 idev->started_request_count++;
Dan Williams209fae12011-06-13 17:39:44 -0700465 }
Dan Williams18606552011-05-01 14:57:11 -0700466}
467
Dan Williams89a73012011-06-30 19:14:33 -0700468enum sci_status sci_remote_device_start_io(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700469 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700470 struct isci_request *ireq)
Dan Williams18606552011-05-01 14:57:11 -0700471{
Dan Williams78a6f062011-06-30 16:31:37 -0700472 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700473 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams78a6f062011-06-30 16:31:37 -0700474 struct isci_port *iport = idev->owning_port;
Dan Williams18606552011-05-01 14:57:11 -0700475 enum sci_status status;
476
477 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000478 case SCI_DEV_INITIAL:
479 case SCI_DEV_STOPPED:
480 case SCI_DEV_STARTING:
481 case SCI_STP_DEV_NCQ_ERROR:
482 case SCI_DEV_STOPPING:
483 case SCI_DEV_FAILED:
484 case SCI_DEV_RESETTING:
485 case SCI_DEV_FINAL:
Dan Williams18606552011-05-01 14:57:11 -0700486 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800487 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
488 __func__, dev_state_name(state));
Dan Williams18606552011-05-01 14:57:11 -0700489 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000490 case SCI_DEV_READY:
Dan Williams18606552011-05-01 14:57:11 -0700491 /* attempt to start an io request for this device object. The remote
492 * device object will issue the start request for the io and if
493 * successful it will start the request for the port object then
494 * increment its own request count.
495 */
Dan Williams89a73012011-06-30 19:14:33 -0700496 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700497 if (status != SCI_SUCCESS)
498 return status;
499
Dan Williams89a73012011-06-30 19:14:33 -0700500 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700501 if (status != SCI_SUCCESS)
502 break;
503
Dan Williams89a73012011-06-30 19:14:33 -0700504 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700505 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000506 case SCI_STP_DEV_IDLE: {
Dan Williams18606552011-05-01 14:57:11 -0700507 /* handle the start io operation for a sata device that is in
508 * the command idle state. - Evalute the type of IO request to
509 * be started - If its an NCQ request change to NCQ substate -
510 * If its any other command change to the CMD substate
511 *
512 * If this is a softreset we may want to have a different
513 * substate.
514 */
Dan Williams89a73012011-06-30 19:14:33 -0700515 enum sci_remote_device_states new_state;
Dave Jiange76d6182011-05-04 15:02:03 -0700516 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700517
Dan Williams89a73012011-06-30 19:14:33 -0700518 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700519 if (status != SCI_SUCCESS)
520 return status;
521
Dan Williams89a73012011-06-30 19:14:33 -0700522 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700523 if (status != SCI_SUCCESS)
524 break;
525
Dan Williams89a73012011-06-30 19:14:33 -0700526 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700527 if (status != SCI_SUCCESS)
528 break;
529
Dave Jiange76d6182011-05-04 15:02:03 -0700530 if (task->ata_task.use_ncq)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000531 new_state = SCI_STP_DEV_NCQ;
Dan Williams18606552011-05-01 14:57:11 -0700532 else {
Dan Williams78a6f062011-06-30 16:31:37 -0700533 idev->working_request = ireq;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000534 new_state = SCI_STP_DEV_CMD;
Dan Williams18606552011-05-01 14:57:11 -0700535 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000536 sci_change_state(sm, new_state);
Dan Williams18606552011-05-01 14:57:11 -0700537 break;
538 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000539 case SCI_STP_DEV_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700540 struct sas_task *task = isci_request_access_task(ireq);
541
542 if (task->ata_task.use_ncq) {
Dan Williams89a73012011-06-30 19:14:33 -0700543 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700544 if (status != SCI_SUCCESS)
545 return status;
546
Dan Williams89a73012011-06-30 19:14:33 -0700547 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700548 if (status != SCI_SUCCESS)
549 break;
550
Dan Williams89a73012011-06-30 19:14:33 -0700551 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700552 } else
553 return SCI_FAILURE_INVALID_STATE;
554 break;
Dave Jiange76d6182011-05-04 15:02:03 -0700555 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000556 case SCI_STP_DEV_AWAIT_RESET:
Dan Williams18606552011-05-01 14:57:11 -0700557 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000558 case SCI_SMP_DEV_IDLE:
Dan Williams89a73012011-06-30 19:14:33 -0700559 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700560 if (status != SCI_SUCCESS)
561 return status;
562
Dan Williams89a73012011-06-30 19:14:33 -0700563 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700564 if (status != SCI_SUCCESS)
565 break;
566
Dan Williams89a73012011-06-30 19:14:33 -0700567 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700568 if (status != SCI_SUCCESS)
569 break;
570
Dan Williams78a6f062011-06-30 16:31:37 -0700571 idev->working_request = ireq;
572 sci_change_state(&idev->sm, SCI_SMP_DEV_CMD);
Dan Williams18606552011-05-01 14:57:11 -0700573 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000574 case SCI_STP_DEV_CMD:
575 case SCI_SMP_DEV_CMD:
Dan Williams18606552011-05-01 14:57:11 -0700576 /* device is already handling a command it can not accept new commands
577 * until this one is complete.
578 */
579 return SCI_FAILURE_INVALID_STATE;
580 }
581
Dan Williams89a73012011-06-30 19:14:33 -0700582 sci_remote_device_start_request(idev, ireq, status);
Dan Williams18606552011-05-01 14:57:11 -0700583 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700584}
585
Dan Williamsffe191c2011-06-29 13:09:25 -0700586static enum sci_status common_complete_io(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -0700587 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700588 struct isci_request *ireq)
Dan Williams88f3b622011-04-22 19:18:03 -0700589{
Dan Williams10a09e62011-05-01 15:33:43 -0700590 enum sci_status status;
591
Dan Williams89a73012011-06-30 19:14:33 -0700592 status = sci_request_complete(ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700593 if (status != SCI_SUCCESS)
594 return status;
595
Dan Williams89a73012011-06-30 19:14:33 -0700596 status = sci_port_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700597 if (status != SCI_SUCCESS)
598 return status;
599
Dan Williams89a73012011-06-30 19:14:33 -0700600 sci_remote_device_decrement_request_count(idev);
Dan Williams10a09e62011-05-01 15:33:43 -0700601 return status;
602}
603
Dan Williams89a73012011-06-30 19:14:33 -0700604enum sci_status sci_remote_device_complete_io(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700605 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700606 struct isci_request *ireq)
Dan Williams10a09e62011-05-01 15:33:43 -0700607{
Dan Williams78a6f062011-06-30 16:31:37 -0700608 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700609 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams78a6f062011-06-30 16:31:37 -0700610 struct isci_port *iport = idev->owning_port;
Dan Williams10a09e62011-05-01 15:33:43 -0700611 enum sci_status status;
612
613 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000614 case SCI_DEV_INITIAL:
615 case SCI_DEV_STOPPED:
616 case SCI_DEV_STARTING:
617 case SCI_STP_DEV_IDLE:
618 case SCI_SMP_DEV_IDLE:
619 case SCI_DEV_FAILED:
620 case SCI_DEV_FINAL:
Dan Williams10a09e62011-05-01 15:33:43 -0700621 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800622 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
623 __func__, dev_state_name(state));
Dan Williams10a09e62011-05-01 15:33:43 -0700624 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000625 case SCI_DEV_READY:
626 case SCI_STP_DEV_AWAIT_RESET:
627 case SCI_DEV_RESETTING:
Dan Williams78a6f062011-06-30 16:31:37 -0700628 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700629 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000630 case SCI_STP_DEV_CMD:
631 case SCI_STP_DEV_NCQ:
632 case SCI_STP_DEV_NCQ_ERROR:
Dan Williamsb50102d2011-09-30 18:52:19 -0700633 case SCI_STP_DEV_ATAPI_ERROR:
Dan Williams78a6f062011-06-30 16:31:37 -0700634 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700635 if (status != SCI_SUCCESS)
636 break;
637
Dan Williams5076a1a2011-06-27 14:57:03 -0700638 if (ireq->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
Dan Williams10a09e62011-05-01 15:33:43 -0700639 /* This request causes hardware error, device needs to be Lun Reset.
640 * So here we force the state machine to IDLE state so the rest IOs
641 * can reach RNC state handler, these IOs will be completed by RNC with
642 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
643 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000644 sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
Dan Williams34a99152011-07-01 02:25:15 -0700645 } else if (idev->started_request_count == 0)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000646 sci_change_state(sm, SCI_STP_DEV_IDLE);
Dan Williams10a09e62011-05-01 15:33:43 -0700647 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000648 case SCI_SMP_DEV_CMD:
Dan Williams78a6f062011-06-30 16:31:37 -0700649 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700650 if (status != SCI_SUCCESS)
651 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000652 sci_change_state(sm, SCI_SMP_DEV_IDLE);
Dan Williams10a09e62011-05-01 15:33:43 -0700653 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000654 case SCI_DEV_STOPPING:
Dan Williams78a6f062011-06-30 16:31:37 -0700655 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700656 if (status != SCI_SUCCESS)
657 break;
658
Dan Williams34a99152011-07-01 02:25:15 -0700659 if (idev->started_request_count == 0)
Dan Williams89a73012011-06-30 19:14:33 -0700660 sci_remote_node_context_destruct(&idev->rnc,
Dan Williams34a99152011-07-01 02:25:15 -0700661 rnc_destruct_done,
662 idev);
Dan Williams10a09e62011-05-01 15:33:43 -0700663 break;
664 }
665
666 if (status != SCI_SUCCESS)
Dan Williams78a6f062011-06-30 16:31:37 -0700667 dev_err(scirdev_to_dev(idev),
Dan Williams10a09e62011-05-01 15:33:43 -0700668 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
Dan Williamsffe191c2011-06-29 13:09:25 -0700669 "could not complete\n", __func__, iport,
Dan Williams78a6f062011-06-30 16:31:37 -0700670 idev, ireq, status);
Dan Williams209fae12011-06-13 17:39:44 -0700671 else
Dan Williams78a6f062011-06-30 16:31:37 -0700672 isci_put_device(idev);
Dan Williams10a09e62011-05-01 15:33:43 -0700673
674 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700675}
676
Dan Williams89a73012011-06-30 19:14:33 -0700677static void sci_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700678{
Dan Williams78a6f062011-06-30 16:31:37 -0700679 struct isci_remote_device *idev = dev;
Dan Williams84b9b022011-05-01 15:53:25 -0700680
681 /* we need to check if this request is still valid to continue. */
Dan Williams78a6f062011-06-30 16:31:37 -0700682 if (idev->working_request)
Dan Williams89a73012011-06-30 19:14:33 -0700683 sci_controller_continue_io(idev->working_request);
Dan Williams84b9b022011-05-01 15:53:25 -0700684}
685
Dan Williams89a73012011-06-30 19:14:33 -0700686enum sci_status sci_remote_device_start_task(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700687 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700688 struct isci_request *ireq)
Dan Williams84b9b022011-05-01 15:53:25 -0700689{
Dan Williams78a6f062011-06-30 16:31:37 -0700690 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700691 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams78a6f062011-06-30 16:31:37 -0700692 struct isci_port *iport = idev->owning_port;
Dan Williams84b9b022011-05-01 15:53:25 -0700693 enum sci_status status;
694
695 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000696 case SCI_DEV_INITIAL:
697 case SCI_DEV_STOPPED:
698 case SCI_DEV_STARTING:
699 case SCI_SMP_DEV_IDLE:
700 case SCI_SMP_DEV_CMD:
701 case SCI_DEV_STOPPING:
702 case SCI_DEV_FAILED:
703 case SCI_DEV_RESETTING:
704 case SCI_DEV_FINAL:
Dan Williams84b9b022011-05-01 15:53:25 -0700705 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800706 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
707 __func__, dev_state_name(state));
Dan Williams84b9b022011-05-01 15:53:25 -0700708 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000709 case SCI_STP_DEV_IDLE:
710 case SCI_STP_DEV_CMD:
711 case SCI_STP_DEV_NCQ:
712 case SCI_STP_DEV_NCQ_ERROR:
713 case SCI_STP_DEV_AWAIT_RESET:
Dan Williams89a73012011-06-30 19:14:33 -0700714 status = sci_port_start_io(iport, idev, ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700715 if (status != SCI_SUCCESS)
716 return status;
717
Dan Williams89a73012011-06-30 19:14:33 -0700718 status = sci_remote_node_context_start_task(&idev->rnc, ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700719 if (status != SCI_SUCCESS)
720 goto out;
721
Dan Williams89a73012011-06-30 19:14:33 -0700722 status = sci_request_start(ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700723 if (status != SCI_SUCCESS)
724 goto out;
725
726 /* Note: If the remote device state is not IDLE this will
727 * replace the request that probably resulted in the task
728 * management request.
729 */
Dan Williams78a6f062011-06-30 16:31:37 -0700730 idev->working_request = ireq;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000731 sci_change_state(sm, SCI_STP_DEV_CMD);
Dan Williams84b9b022011-05-01 15:53:25 -0700732
733 /* The remote node context must cleanup the TCi to NCQ mapping
734 * table. The only way to do this correctly is to either write
735 * to the TLCR register or to invalidate and repost the RNC. In
736 * either case the remote node context state machine will take
737 * the correct action when the remote node context is suspended
738 * and later resumed.
739 */
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800740 sci_remote_node_context_suspend(
741 &idev->rnc, SCI_SOFTWARE_SUSPENSION,
742 SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT, NULL, NULL);
743 sci_remote_node_context_resume(
744 &idev->rnc, sci_remote_device_continue_request, idev);
Dan Williams84b9b022011-05-01 15:53:25 -0700745
746 out:
Dan Williams89a73012011-06-30 19:14:33 -0700747 sci_remote_device_start_request(idev, ireq, status);
Dan Williams84b9b022011-05-01 15:53:25 -0700748 /* We need to let the controller start request handler know that
749 * it can't post TC yet. We will provide a callback function to
750 * post TC when RNC gets resumed.
751 */
752 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000753 case SCI_DEV_READY:
Dan Williams89a73012011-06-30 19:14:33 -0700754 status = sci_port_start_io(iport, idev, ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700755 if (status != SCI_SUCCESS)
756 return status;
757
Dan Williams89a73012011-06-30 19:14:33 -0700758 status = sci_remote_node_context_start_task(&idev->rnc, ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700759 if (status != SCI_SUCCESS)
760 break;
761
Dan Williams89a73012011-06-30 19:14:33 -0700762 status = sci_request_start(ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700763 break;
764 }
Dan Williams89a73012011-06-30 19:14:33 -0700765 sci_remote_device_start_request(idev, ireq, status);
Dan Williams84b9b022011-05-01 15:53:25 -0700766
767 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700768}
769
Dan Williams34a99152011-07-01 02:25:15 -0700770void sci_remote_device_post_request(struct isci_remote_device *idev, u32 request)
Dan Williams88f3b622011-04-22 19:18:03 -0700771{
Dan Williams34a99152011-07-01 02:25:15 -0700772 struct isci_port *iport = idev->owning_port;
Dan Williams88f3b622011-04-22 19:18:03 -0700773 u32 context;
774
Dan Williams34a99152011-07-01 02:25:15 -0700775 context = request |
776 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
777 (iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
778 idev->rnc.remote_node_index;
Dan Williams88f3b622011-04-22 19:18:03 -0700779
Dan Williams34a99152011-07-01 02:25:15 -0700780 sci_controller_post_request(iport->owning_controller, context);
Dan Williams88f3b622011-04-22 19:18:03 -0700781}
782
Dan Williamsab2e8f72011-04-27 16:32:45 -0700783/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700784 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700785 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700786 */
Dan Williamseb229672011-05-01 14:05:57 -0700787static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700788{
Dan Williams78a6f062011-06-30 16:31:37 -0700789 struct isci_remote_device *idev = _dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700790
Dan Williams78a6f062011-06-30 16:31:37 -0700791 if (is_remote_device_ready(idev))
Dan Williamse6225712011-05-01 16:26:09 -0700792 return;
Dan Williams88f3b622011-04-22 19:18:03 -0700793
Dan Williamse6225712011-05-01 16:26:09 -0700794 /* go 'ready' if we are not already in a ready state */
Dan Williams78a6f062011-06-30 16:31:37 -0700795 sci_change_state(&idev->sm, SCI_DEV_READY);
Dan Williams88f3b622011-04-22 19:18:03 -0700796}
797
Dan Williams89a73012011-06-30 19:14:33 -0700798static void sci_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700799{
Dan Williams78a6f062011-06-30 16:31:37 -0700800 struct isci_remote_device *idev = _dev;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700801 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700802
803 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
804 * As a result, avoid sending the ready notification.
805 */
Dan Williams78a6f062011-06-30 16:31:37 -0700806 if (idev->sm.previous_state_id != SCI_STP_DEV_NCQ)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700807 isci_remote_device_ready(ihost, idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700808}
809
Dan Williams89a73012011-06-30 19:14:33 -0700810static void sci_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700811{
Dan Williams78a6f062011-06-30 16:31:37 -0700812 struct isci_remote_device *idev = container_of(sm, typeof(*idev), 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 */
Dan Williams78a6f062011-06-30 16:31:37 -0700815 sci_change_state(&idev->sm, SCI_DEV_STOPPED);
Dan Williams88f3b622011-04-22 19:18:03 -0700816}
817
818/**
Dan Williams89a73012011-06-30 19:14:33 -0700819 * sci_remote_device_destruct() - free remote node context and destruct
Dan Williams88f3b622011-04-22 19:18:03 -0700820 * @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 */
Dan Williams89a73012011-06-30 19:14:33 -0700831static enum sci_status sci_remote_device_destruct(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700832{
Dan Williams78a6f062011-06-30 16:31:37 -0700833 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700834 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700835 struct isci_host *ihost;
Dan Williamsb8d82f62011-05-01 14:38:26 -0700836
Edmund Nadolskie3013702011-06-02 00:10:43 +0000837 if (state != SCI_DEV_STOPPED) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800838 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
839 __func__, dev_state_name(state));
Dan Williamsb8d82f62011-05-01 14:38:26 -0700840 return SCI_FAILURE_INVALID_STATE;
841 }
842
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700843 ihost = idev->owning_port->owning_controller;
Dan Williams89a73012011-06-30 19:14:33 -0700844 sci_controller_free_remote_node_context(ihost, idev,
Dan Williams78a6f062011-06-30 16:31:37 -0700845 idev->rnc.remote_node_index);
846 idev->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 Williams89a73012011-06-30 19:14:33 -0700869 sci_remote_device_destruct(idev);
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 Williams89a73012011-06-30 19:14:33 -0700874static void sci_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700875{
Dan Williams78a6f062011-06-30 16:31:37 -0700876 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700877 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams88f3b622011-04-22 19:18:03 -0700878 u32 prev_state;
879
Dan Williams88f3b622011-04-22 19:18:03 -0700880 /* If we are entering from the stopping state let the SCI User know that
881 * the stop operation has completed.
882 */
Dan Williams78a6f062011-06-30 16:31:37 -0700883 prev_state = idev->sm.previous_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000884 if (prev_state == SCI_DEV_STOPPING)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700885 isci_remote_device_deconstruct(ihost, idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700886
Dan Williams89a73012011-06-30 19:14:33 -0700887 sci_controller_remote_device_stopped(ihost, idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700888}
889
Dan Williams89a73012011-06-30 19:14:33 -0700890static void sci_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700891{
Dan Williams78a6f062011-06-30 16:31:37 -0700892 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -0700893 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams88f3b622011-04-22 19:18:03 -0700894
Dan Williams88f3b622011-04-22 19:18:03 -0700895 isci_remote_device_not_ready(ihost, idev,
896 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
897}
898
Dan Williams89a73012011-06-30 19:14:33 -0700899static void sci_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700900{
Dan Williams78a6f062011-06-30 16:31:37 -0700901 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700902 struct isci_host *ihost = idev->owning_port->owning_controller;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000903 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700904
Dan Williamsab2e8f72011-04-27 16:32:45 -0700905 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
Dan Williams78a6f062011-06-30 16:31:37 -0700906 sci_change_state(&idev->sm, SCI_STP_DEV_IDLE);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700907 } else if (dev_is_expander(dev)) {
Dan Williams78a6f062011-06-30 16:31:37 -0700908 sci_change_state(&idev->sm, SCI_SMP_DEV_IDLE);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700909 } else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700910 isci_remote_device_ready(ihost, idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700911}
912
Dan Williams89a73012011-06-30 19:14:33 -0700913static void sci_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700914{
Dan Williams78a6f062011-06-30 16:31:37 -0700915 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
916 struct domain_device *dev = idev->domain_dev;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700917
918 if (dev->dev_type == SAS_END_DEV) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700919 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams88f3b622011-04-22 19:18:03 -0700920
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700921 isci_remote_device_not_ready(ihost, idev,
Dan Williams88f3b622011-04-22 19:18:03 -0700922 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
923 }
924}
925
Dan Williams89a73012011-06-30 19:14:33 -0700926static void sci_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700927{
Dan Williams78a6f062011-06-30 16:31:37 -0700928 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800929 struct isci_host *ihost = idev->owning_port->owning_controller;
930
931 dev_dbg(&ihost->pdev->dev,
932 "%s: isci_device = %p\n", __func__, idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700933
Dan Williams89a73012011-06-30 19:14:33 -0700934 sci_remote_node_context_suspend(
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800935 &idev->rnc, SCI_SOFTWARE_SUSPENSION,
936 SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700937}
938
Dan Williams89a73012011-06-30 19:14:33 -0700939static void sci_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700940{
Dan Williams78a6f062011-06-30 16:31:37 -0700941 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800942 struct isci_host *ihost = idev->owning_port->owning_controller;
943
944 dev_dbg(&ihost->pdev->dev,
945 "%s: isci_device = %p\n", __func__, idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700946
Dan Williams89a73012011-06-30 19:14:33 -0700947 sci_remote_node_context_resume(&idev->rnc, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700948}
949
Dan Williams89a73012011-06-30 19:14:33 -0700950static void sci_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700951{
Dan Williams78a6f062011-06-30 16:31:37 -0700952 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700953
Dan Williams78a6f062011-06-30 16:31:37 -0700954 idev->working_request = NULL;
Dan Williams89a73012011-06-30 19:14:33 -0700955 if (sci_remote_node_context_is_ready(&idev->rnc)) {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700956 /*
957 * Since the RNC is ready, it's alright to finish completion
958 * processing (e.g. signal the remote device is ready). */
Dan Williams89a73012011-06-30 19:14:33 -0700959 sci_stp_remote_device_ready_idle_substate_resume_complete_handler(idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700960 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700961 sci_remote_node_context_resume(&idev->rnc,
962 sci_stp_remote_device_ready_idle_substate_resume_complete_handler,
Dan Williams78a6f062011-06-30 16:31:37 -0700963 idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700964 }
965}
966
Dan Williams89a73012011-06-30 19:14:33 -0700967static void sci_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700968{
Dan Williams78a6f062011-06-30 16:31:37 -0700969 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -0700970 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700971
Dan Williams78a6f062011-06-30 16:31:37 -0700972 BUG_ON(idev->working_request == NULL);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700973
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700974 isci_remote_device_not_ready(ihost, idev,
Dan Williamsab2e8f72011-04-27 16:32:45 -0700975 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
976}
977
Dan Williams89a73012011-06-30 19:14:33 -0700978static void sci_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700979{
Dan Williams78a6f062011-06-30 16:31:37 -0700980 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -0700981 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700982
Dan Williams78a6f062011-06-30 16:31:37 -0700983 if (idev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700984 isci_remote_device_not_ready(ihost, idev,
Dan Williams78a6f062011-06-30 16:31:37 -0700985 idev->not_ready_reason);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700986}
987
Jeff Skirvinac78ed02012-03-08 22:41:50 -0800988static void sci_stp_remote_device_atapi_error_substate_enter(
989 struct sci_base_state_machine *sm)
990{
991 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
992
993 /* This state is entered when an I/O is decoded with an error
994 * condition. By this point the RNC expected suspension state is set.
995 * The error conditions suspend the device, so unsuspend here if
996 * possible.
997 */
998 sci_remote_node_context_resume(&idev->rnc,
999 atapi_remote_device_resume_done,
1000 idev);
1001}
1002
Dan Williams89a73012011-06-30 19:14:33 -07001003static void sci_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001004{
Dan Williams78a6f062011-06-30 16:31:37 -07001005 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001006 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001007
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001008 isci_remote_device_ready(ihost, idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001009}
1010
Dan Williams89a73012011-06-30 19:14:33 -07001011static void sci_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001012{
Dan Williams78a6f062011-06-30 16:31:37 -07001013 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001014 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001015
Dan Williams78a6f062011-06-30 16:31:37 -07001016 BUG_ON(idev->working_request == NULL);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001017
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001018 isci_remote_device_not_ready(ihost, idev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001019 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1020}
1021
Dan Williams89a73012011-06-30 19:14:33 -07001022static void sci_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001023{
Dan Williams78a6f062011-06-30 16:31:37 -07001024 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001025
Dan Williams78a6f062011-06-30 16:31:37 -07001026 idev->working_request = NULL;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001027}
Dan Williams88f3b622011-04-22 19:18:03 -07001028
Dan Williams89a73012011-06-30 19:14:33 -07001029static const struct sci_base_state sci_remote_device_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001030 [SCI_DEV_INITIAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001031 .enter_state = sci_remote_device_initial_state_enter,
Dan Williams88f3b622011-04-22 19:18:03 -07001032 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001033 [SCI_DEV_STOPPED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001034 .enter_state = sci_remote_device_stopped_state_enter,
Dan Williams88f3b622011-04-22 19:18:03 -07001035 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001036 [SCI_DEV_STARTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001037 .enter_state = sci_remote_device_starting_state_enter,
Dan Williams88f3b622011-04-22 19:18:03 -07001038 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001039 [SCI_DEV_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -07001040 .enter_state = sci_remote_device_ready_state_enter,
1041 .exit_state = sci_remote_device_ready_state_exit
Dan Williams88f3b622011-04-22 19:18:03 -07001042 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001043 [SCI_STP_DEV_IDLE] = {
Dan Williams89a73012011-06-30 19:14:33 -07001044 .enter_state = sci_stp_remote_device_ready_idle_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001045 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001046 [SCI_STP_DEV_CMD] = {
Dan Williams89a73012011-06-30 19:14:33 -07001047 .enter_state = sci_stp_remote_device_ready_cmd_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001048 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001049 [SCI_STP_DEV_NCQ] = { },
1050 [SCI_STP_DEV_NCQ_ERROR] = {
Dan Williams89a73012011-06-30 19:14:33 -07001051 .enter_state = sci_stp_remote_device_ready_ncq_error_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001052 },
Jeff Skirvinac78ed02012-03-08 22:41:50 -08001053 [SCI_STP_DEV_ATAPI_ERROR] = {
1054 .enter_state = sci_stp_remote_device_atapi_error_substate_enter,
1055 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001056 [SCI_STP_DEV_AWAIT_RESET] = { },
1057 [SCI_SMP_DEV_IDLE] = {
Dan Williams89a73012011-06-30 19:14:33 -07001058 .enter_state = sci_smp_remote_device_ready_idle_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001059 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001060 [SCI_SMP_DEV_CMD] = {
Dan Williams89a73012011-06-30 19:14:33 -07001061 .enter_state = sci_smp_remote_device_ready_cmd_substate_enter,
1062 .exit_state = sci_smp_remote_device_ready_cmd_substate_exit,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001063 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001064 [SCI_DEV_STOPPING] = { },
1065 [SCI_DEV_FAILED] = { },
1066 [SCI_DEV_RESETTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001067 .enter_state = sci_remote_device_resetting_state_enter,
1068 .exit_state = sci_remote_device_resetting_state_exit
Dan Williams88f3b622011-04-22 19:18:03 -07001069 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001070 [SCI_DEV_FINAL] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001071};
1072
1073/**
Dan Williams89a73012011-06-30 19:14:33 -07001074 * sci_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001075 * @sci_port: SAS/SATA port through which this device is accessed.
1076 * @sci_dev: remote device to construct
1077 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001078 * This routine just performs benign initialization and does not
1079 * allocate the remote_node_context which is left to
Dan Williams89a73012011-06-30 19:14:33 -07001080 * sci_remote_device_[de]a_construct(). sci_remote_device_destruct()
Dan Williamsb87ee302011-04-25 11:48:29 -07001081 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001082 */
Dan Williams89a73012011-06-30 19:14:33 -07001083static void sci_remote_device_construct(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -07001084 struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -07001085{
Dan Williams78a6f062011-06-30 16:31:37 -07001086 idev->owning_port = iport;
1087 idev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001088
Dan Williams89a73012011-06-30 19:14:33 -07001089 sci_init_sm(&idev->sm, sci_remote_device_state_table, SCI_DEV_INITIAL);
Dan Williams88f3b622011-04-22 19:18:03 -07001090
Dan Williams89a73012011-06-30 19:14:33 -07001091 sci_remote_node_context_construct(&idev->rnc,
Dan Williams88f3b622011-04-22 19:18:03 -07001092 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001093}
1094
1095/**
Dan Williams89a73012011-06-30 19:14:33 -07001096 * sci_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001097 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001098 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1099 * the device is known to the SCI Core since it is contained in the
Dan Williams89a73012011-06-30 19:14:33 -07001100 * sci_phy object. Remote node context(s) is/are a global resource
1101 * allocated by this routine, freed by sci_remote_device_destruct().
Dan Williamsb87ee302011-04-25 11:48:29 -07001102 *
1103 * Returns:
1104 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1105 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1106 * sata-only controller instance.
1107 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001108 */
Dan Williams89a73012011-06-30 19:14:33 -07001109static enum sci_status sci_remote_device_da_construct(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -07001110 struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -07001111{
1112 enum sci_status status;
Bartek Nowakowski7e629842012-01-04 01:33:20 -08001113 struct sci_port_properties properties;
Dan Williams88f3b622011-04-22 19:18:03 -07001114
Dan Williams89a73012011-06-30 19:14:33 -07001115 sci_remote_device_construct(iport, idev);
Dan Williamsb87ee302011-04-25 11:48:29 -07001116
Bartek Nowakowski7e629842012-01-04 01:33:20 -08001117 sci_port_get_properties(iport, &properties);
1118 /* Get accurate port width from port's phy mask for a DA device. */
1119 idev->device_port_width = hweight32(properties.phy_mask);
1120
Dan Williams89a73012011-06-30 19:14:33 -07001121 status = sci_controller_allocate_remote_node_context(iport->owning_controller,
Dan Williams11cc5182012-02-01 00:23:10 -08001122 idev,
1123 &idev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001124
Dan Williamsa1a113b2011-04-21 18:44:45 -07001125 if (status != SCI_SUCCESS)
1126 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001127
Dan Williams89a73012011-06-30 19:14:33 -07001128 idev->connection_rate = sci_port_get_max_allowed_speed(iport);
Dan Williams88f3b622011-04-22 19:18:03 -07001129
Dan Williamsa1a113b2011-04-21 18:44:45 -07001130 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001131}
1132
Dan Williams88f3b622011-04-22 19:18:03 -07001133/**
Dan Williams89a73012011-06-30 19:14:33 -07001134 * sci_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001135 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001136 * Remote node context(s) is/are a global resource allocated by this
Dan Williams89a73012011-06-30 19:14:33 -07001137 * routine, freed by sci_remote_device_destruct().
Dan Williamsb87ee302011-04-25 11:48:29 -07001138 *
1139 * Returns:
1140 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1141 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1142 * sata-only controller instance.
1143 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001144 */
Dan Williams89a73012011-06-30 19:14:33 -07001145static enum sci_status sci_remote_device_ea_construct(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -07001146 struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -07001147{
Dan Williams78a6f062011-06-30 16:31:37 -07001148 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -07001149 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001150
Dan Williams89a73012011-06-30 19:14:33 -07001151 sci_remote_device_construct(iport, idev);
Dan Williams88f3b622011-04-22 19:18:03 -07001152
Dan Williams89a73012011-06-30 19:14:33 -07001153 status = sci_controller_allocate_remote_node_context(iport->owning_controller,
Dan Williams78a6f062011-06-30 16:31:37 -07001154 idev,
1155 &idev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001156 if (status != SCI_SUCCESS)
1157 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001158
Dan Williams11cc5182012-02-01 00:23:10 -08001159 /* For SAS-2 the physical link rate is actually a logical link
Dan Williamsa1a113b2011-04-21 18:44:45 -07001160 * 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
Dan Williams11cc5182012-02-01 00:23:10 -08001164 * one another, so this code works for both situations.
1165 */
Dan Williams89a73012011-06-30 19:14:33 -07001166 idev->connection_rate = min_t(u16, sci_port_get_max_allowed_speed(iport),
Dan Williams00d680e2011-04-25 14:29:29 -07001167 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001168
Dan Williamsa1a113b2011-04-21 18:44:45 -07001169 /* / @todo Should I assign the port width by reading all of the phys on the port? */
Dan Williams78a6f062011-06-30 16:31:37 -07001170 idev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001171
Dan Williamsab2e8f72011-04-27 16:32:45 -07001172 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001173}
1174
1175/**
Dan Williams89a73012011-06-30 19:14:33 -07001176 * sci_remote_device_start() - This method will start the supplied remote
Dan Williams88f3b622011-04-22 19:18:03 -07001177 * device. This method enables normal IO requests to flow through to the
1178 * remote device.
1179 * @remote_device: This parameter specifies the device to be started.
1180 * @timeout: This parameter specifies the number of milliseconds in which the
1181 * start operation should complete.
1182 *
1183 * An indication of whether the device was successfully started. SCI_SUCCESS
1184 * This value is returned if the device was successfully started.
1185 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1186 * the device when there have been no phys added to it.
1187 */
Dan Williams89a73012011-06-30 19:14:33 -07001188static enum sci_status sci_remote_device_start(struct isci_remote_device *idev,
Dan Williamseb229672011-05-01 14:05:57 -07001189 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001190{
Dan Williams78a6f062011-06-30 16:31:37 -07001191 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -07001192 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamseb229672011-05-01 14:05:57 -07001193 enum sci_status status;
1194
Edmund Nadolskie3013702011-06-02 00:10:43 +00001195 if (state != SCI_DEV_STOPPED) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -08001196 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
1197 __func__, dev_state_name(state));
Dan Williamseb229672011-05-01 14:05:57 -07001198 return SCI_FAILURE_INVALID_STATE;
1199 }
1200
Dan Williams89a73012011-06-30 19:14:33 -07001201 status = sci_remote_node_context_resume(&idev->rnc,
Dan Williamseb229672011-05-01 14:05:57 -07001202 remote_device_resume_done,
Dan Williams78a6f062011-06-30 16:31:37 -07001203 idev);
Dan Williamseb229672011-05-01 14:05:57 -07001204 if (status != SCI_SUCCESS)
1205 return status;
1206
Edmund Nadolskie3013702011-06-02 00:10:43 +00001207 sci_change_state(sm, SCI_DEV_STARTING);
Dan Williamseb229672011-05-01 14:05:57 -07001208
1209 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001210}
Dan Williams6f231dd2011-07-02 22:56:22 -07001211
Dan Williams00d680e2011-04-25 14:29:29 -07001212static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1213 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001214{
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))
Dan Williams89a73012011-06-30 19:14:33 -07001220 status = sci_remote_device_ea_construct(iport, idev);
Dan Williams00d680e2011-04-25 14:29:29 -07001221 else
Dan Williams89a73012011-06-30 19:14:33 -07001222 status = sci_remote_device_da_construct(iport, idev);
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 Williams89a73012011-06-30 19:14:33 -07001232 status = sci_remote_device_start(idev, 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);
Dan Williams209fae12011-06-13 17:39:44 -07001300 smp_mb__before_clear_bit();
1301 clear_bit(IDEV_ALLOCATED, &idev->flags);
1302 wake_up(&ihost->eventq);
1303}
1304
Dan Williams6f231dd2011-07-02 22:56:22 -07001305/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001306 * isci_remote_device_stop() - This function is called internally to stop the
1307 * remote device.
1308 * @isci_host: This parameter specifies the isci host object.
1309 * @isci_device: This parameter specifies the remote device.
1310 *
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001311 * The status of the ihost request to stop.
Dan Williams6f231dd2011-07-02 22:56:22 -07001312 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001313enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001314{
1315 enum sci_status status;
1316 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001317
Dan Williams6ad31fe2011-03-04 12:10:29 -08001318 dev_dbg(&ihost->pdev->dev,
1319 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001320
Dan Williams209fae12011-06-13 17:39:44 -07001321 spin_lock_irqsave(&ihost->scic_lock, flags);
1322 idev->domain_dev->lldd_dev = NULL; /* disable new lookups */
1323 set_bit(IDEV_GONE, &idev->flags);
Dan Williams209fae12011-06-13 17:39:44 -07001324 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001325
1326 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001327 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001328
Dan Williams6ad31fe2011-03-04 12:10:29 -08001329 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001330
Dan Williams6ad31fe2011-03-04 12:10:29 -08001331 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -07001332 status = sci_remote_device_stop(idev, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001333 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001334
1335 /* Wait for the stop complete callback. */
Dan Williams209fae12011-06-13 17:39:44 -07001336 if (WARN_ONCE(status != SCI_SUCCESS, "failed to stop device\n"))
1337 /* nothing to wait for */;
1338 else
Dan Williams6ad31fe2011-03-04 12:10:29 -08001339 wait_for_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001340
Dan Williams6f231dd2011-07-02 22:56:22 -07001341 return status;
1342}
1343
1344/**
1345 * isci_remote_device_gone() - This function is called by libsas when a domain
1346 * device is removed.
1347 * @domain_device: This parameter specifies the libsas domain device.
1348 *
1349 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001350void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001351{
Dan Williams4393aa42011-03-31 13:10:44 -07001352 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001353 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001354
Dan Williams6ad31fe2011-03-04 12:10:29 -08001355 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001356 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001357 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001358
Dan Williams6ad31fe2011-03-04 12:10:29 -08001359 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001360}
1361
1362
1363/**
1364 * isci_remote_device_found() - This function is called by libsas when a remote
1365 * device is discovered. A remote device object is created and started. the
1366 * function then sleeps until the sci core device started message is
1367 * received.
1368 * @domain_device: This parameter specifies the libsas domain device.
1369 *
1370 * status, zero indicates success.
1371 */
Dan Williamsc132f692012-01-03 23:26:08 -08001372int isci_remote_device_found(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001373{
Dan Williamsc132f692012-01-03 23:26:08 -08001374 struct isci_host *isci_host = dev_to_ihost(dev);
1375 struct isci_port *isci_port = dev->port->lldd_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001376 struct isci_remote_device *isci_device;
1377 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001378
Dan Williams6f231dd2011-07-02 22:56:22 -07001379 dev_dbg(&isci_host->pdev->dev,
Dan Williamsc132f692012-01-03 23:26:08 -08001380 "%s: domain_device = %p\n", __func__, dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001381
Dan Williamsc132f692012-01-03 23:26:08 -08001382 if (!isci_port)
1383 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001384
Dan Williams6f231dd2011-07-02 22:56:22 -07001385 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001386 if (!isci_device)
1387 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001388
Dan Williams209fae12011-06-13 17:39:44 -07001389 kref_init(&isci_device->kref);
Dan Williams6f231dd2011-07-02 22:56:22 -07001390 INIT_LIST_HEAD(&isci_device->node);
Dan Williams209fae12011-06-13 17:39:44 -07001391
1392 spin_lock_irq(&isci_host->scic_lock);
Dan Williamsc132f692012-01-03 23:26:08 -08001393 isci_device->domain_dev = dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001394 isci_device->isci_port = isci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001395 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1396
Dan Williams6ad31fe2011-03-04 12:10:29 -08001397 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001398 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001399
Dan Williams6f231dd2011-07-02 22:56:22 -07001400 dev_dbg(&isci_host->pdev->dev,
1401 "%s: isci_device = %p\n",
1402 __func__, isci_device);
1403
Dan Williams209fae12011-06-13 17:39:44 -07001404 if (status == SCI_SUCCESS) {
1405 /* device came up, advertise it to the world */
Dan Williamsc132f692012-01-03 23:26:08 -08001406 dev->lldd_dev = isci_device;
Dan Williams209fae12011-06-13 17:39:44 -07001407 } else
1408 isci_put_device(isci_device);
1409 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001410
Dan Williams6ad31fe2011-03-04 12:10:29 -08001411 /* wait for the device ready callback. */
1412 wait_for_device_start(isci_host, isci_device);
1413
Dan Williams209fae12011-06-13 17:39:44 -07001414 return status == SCI_SUCCESS ? 0 : -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001415}