blob: 89cdd0a05abc12c6e20679f7be16c88aebe716f8 [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 Williams88f3b622011-04-22 19:18:03 -070056#include "intel_sas.h"
Dave Jiange76d6182011-05-04 15:02:03 -070057#include "sas.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070058#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070059#include "port.h"
60#include "remote_device.h"
61#include "request.h"
Dan Williams88f3b622011-04-22 19:18:03 -070062#include "scic_controller.h"
63#include "scic_io_request.h"
64#include "scic_phy.h"
65#include "scic_port.h"
66#include "scic_sds_controller.h"
67#include "scic_sds_phy.h"
68#include "scic_sds_port.h"
69#include "remote_node_context.h"
70#include "scic_sds_request.h"
71#include "sci_environment.h"
72#include "sci_util.h"
73#include "scu_event_codes.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070074#include "task.h"
75
Dan Williamsab2e8f72011-04-27 16:32:45 -070076/**
77 * isci_remote_device_change_state() - This function gets the status of the
78 * remote_device object.
79 * @isci_device: This parameter points to the isci_remote_device object
80 *
81 * status of the object as a isci_status enum.
82 */
83void isci_remote_device_change_state(
84 struct isci_remote_device *isci_device,
85 enum isci_status status)
86{
87 unsigned long flags;
88
89 spin_lock_irqsave(&isci_device->state_lock, flags);
90 isci_device->status = status;
91 spin_unlock_irqrestore(&isci_device->state_lock, flags);
92}
93
94/**
95 * isci_remote_device_not_ready() - This function is called by the scic when
96 * the remote device is not ready. We mark the isci device as ready (not
97 * "ready_for_io") and signal the waiting proccess.
98 * @isci_host: This parameter specifies the isci host object.
99 * @isci_device: This parameter specifies the remote device
100 *
101 */
102static void isci_remote_device_not_ready(struct isci_host *ihost,
103 struct isci_remote_device *idev, u32 reason)
104{
105 dev_dbg(&ihost->pdev->dev,
106 "%s: isci_device = %p\n", __func__, idev);
107
108 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
109 isci_remote_device_change_state(idev, isci_stopping);
110 else
111 /* device ready is actually a "not ready for io" state. */
112 isci_remote_device_change_state(idev, isci_ready);
113}
114
115/**
116 * isci_remote_device_ready() - This function is called by the scic when the
117 * remote device is ready. We mark the isci device as ready and signal the
118 * waiting proccess.
119 * @ihost: our valid isci_host
120 * @idev: remote device
121 *
122 */
123static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
124{
125 dev_dbg(&ihost->pdev->dev,
126 "%s: idev = %p\n", __func__, idev);
127
128 isci_remote_device_change_state(idev, isci_ready_for_io);
129 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
130 wake_up(&ihost->eventq);
131}
132
Dan Williamsec575662011-05-01 14:19:25 -0700133/* called once the remote node context is ready to be freed.
134 * The remote device can now report that its stop operation is complete. none
135 */
136static void rnc_destruct_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700137{
Dan Williamsec575662011-05-01 14:19:25 -0700138 struct scic_sds_remote_device *sci_dev = _dev;
139
140 BUG_ON(sci_dev->started_request_count != 0);
141 sci_base_state_machine_change_state(&sci_dev->state_machine,
142 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
143}
144
145static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
146{
147 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
148 u32 i, request_count = sci_dev->started_request_count;
149 enum sci_status status = SCI_SUCCESS;
150
151 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
152 struct scic_sds_request *sci_req;
153 enum sci_status s;
154
155 sci_req = scic->io_request_table[i];
156 if (!sci_req || sci_req->target_device != sci_dev)
157 continue;
158 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
159 if (s != SCI_SUCCESS)
160 status = s;
161 }
162
163 return status;
164}
165
166enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
167 u32 timeout)
168{
169 struct sci_base_state_machine *sm = &sci_dev->state_machine;
170 enum scic_sds_remote_device_states state = sm->current_state_id;
171
172 switch (state) {
173 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
174 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
175 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
176 default:
177 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
178 __func__, state);
179 return SCI_FAILURE_INVALID_STATE;
180 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
181 return SCI_SUCCESS;
182 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
183 /* device not started so there had better be no requests */
184 BUG_ON(sci_dev->started_request_count != 0);
185 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
186 rnc_destruct_done, sci_dev);
187 /* Transition to the stopping state and wait for the
188 * remote node to complete being posted and invalidated.
189 */
190 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
191 return SCI_SUCCESS;
192 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
193 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
194 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
195 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
196 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
197 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
198 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
199 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
200 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
201 if (sci_dev->started_request_count == 0) {
202 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
203 rnc_destruct_done, sci_dev);
204 return SCI_SUCCESS;
205 } else
206 return scic_sds_remote_device_terminate_requests(sci_dev);
207 break;
208 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
209 /* All requests should have been terminated, but if there is an
210 * attempt to stop a device already in the stopping state, then
211 * try again to terminate.
212 */
213 return scic_sds_remote_device_terminate_requests(sci_dev);
214 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
215 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
216 return SCI_SUCCESS;
217 }
Dan Williams88f3b622011-04-22 19:18:03 -0700218}
Dan Williams6f231dd2011-07-02 22:56:22 -0700219
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700220enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700221{
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700222 struct sci_base_state_machine *sm = &sci_dev->state_machine;
223 enum scic_sds_remote_device_states state = sm->current_state_id;
224
225 switch (state) {
226 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
227 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
228 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
229 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
230 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
231 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
232 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
233 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
234 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
235 default:
236 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
237 __func__, state);
238 return SCI_FAILURE_INVALID_STATE;
239 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
240 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
241 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
242 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
243 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
244 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
245 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
246 return SCI_SUCCESS;
247 }
Dan Williams88f3b622011-04-22 19:18:03 -0700248}
249
Dan Williams81515182011-05-01 14:53:00 -0700250enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700251{
Dan Williams81515182011-05-01 14:53:00 -0700252 struct sci_base_state_machine *sm = &sci_dev->state_machine;
253 enum scic_sds_remote_device_states state = sm->current_state_id;
Dan Williams88f3b622011-04-22 19:18:03 -0700254
Dan Williams81515182011-05-01 14:53:00 -0700255 if (state != SCI_BASE_REMOTE_DEVICE_STATE_RESETTING) {
256 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
257 __func__, state);
258 return SCI_FAILURE_INVALID_STATE;
259 }
260
261 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_READY);
262 return SCI_SUCCESS;
263}
Dan Williams88f3b622011-04-22 19:18:03 -0700264
Dan Williams323f0ec2011-05-01 16:15:47 -0700265enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
266 u32 suspend_type)
Dan Williams88f3b622011-04-22 19:18:03 -0700267{
Dan Williams323f0ec2011-05-01 16:15:47 -0700268 struct sci_base_state_machine *sm = &sci_dev->state_machine;
269 enum scic_sds_remote_device_states state = sm->current_state_id;
270
271 if (state != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD) {
272 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
273 __func__, state);
274 return SCI_FAILURE_INVALID_STATE;
275 }
276
277 return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
278 suspend_type, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700279}
280
Dan Williams01bec772011-05-01 16:51:11 -0700281enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
282 u32 frame_index)
Dan Williams88f3b622011-04-22 19:18:03 -0700283{
Dan Williams01bec772011-05-01 16:51:11 -0700284 struct sci_base_state_machine *sm = &sci_dev->state_machine;
285 enum scic_sds_remote_device_states state = sm->current_state_id;
286 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
287 enum sci_status status;
288
289 switch (state) {
290 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
291 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
292 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
293 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
294 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
295 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
296 default:
297 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
298 __func__, state);
299 /* Return the frame back to the controller */
300 scic_sds_controller_release_frame(scic, frame_index);
301 return SCI_FAILURE_INVALID_STATE;
302 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
303 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
304 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
305 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
306 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
307 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING: {
308 struct scic_sds_request *sci_req;
Dave Jiang2d9c2242011-05-04 18:45:05 -0700309 struct ssp_frame_hdr hdr;
310 void *frame_header;
311 ssize_t word_cnt;
Dan Williams01bec772011-05-01 16:51:11 -0700312
313 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
314 frame_index,
Dave Jiang2d9c2242011-05-04 18:45:05 -0700315 &frame_header);
Dan Williams01bec772011-05-01 16:51:11 -0700316 if (status != SCI_SUCCESS)
317 return status;
318
Dave Jiang2d9c2242011-05-04 18:45:05 -0700319 word_cnt = sizeof(hdr) / sizeof(u32);
320 sci_swab32_cpy(&hdr, frame_header, word_cnt);
321
322 sci_req = scic_request_by_tag(scic, be16_to_cpu(hdr.tag));
Dan Williams01bec772011-05-01 16:51:11 -0700323 if (sci_req && sci_req->target_device == sci_dev) {
324 /* The IO request is now in charge of releasing the frame */
325 status = sci_req->state_handlers->frame_handler(sci_req,
326 frame_index);
327 } else {
328 /* We could not map this tag to a valid IO
329 * request Just toss the frame and continue
330 */
331 scic_sds_controller_release_frame(scic, frame_index);
332 }
333 break;
334 }
335 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700336 struct dev_to_host_fis *hdr;
Dan Williams01bec772011-05-01 16:51:11 -0700337
338 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
339 frame_index,
340 (void **)&hdr);
341 if (status != SCI_SUCCESS)
342 return status;
343
Dave Jiange76d6182011-05-04 15:02:03 -0700344 if (hdr->fis_type == FIS_SETDEVBITS &&
345 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700346 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
347
348 /* TODO Check sactive and complete associated IO if any. */
349 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
Dave Jiange76d6182011-05-04 15:02:03 -0700350 } else if (hdr->fis_type == FIS_REGD2H &&
351 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700352 /*
353 * Some devices return D2H FIS when an NCQ error is detected.
354 * Treat this like an SDB error FIS ready reason.
355 */
356 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
357 sci_base_state_machine_change_state(&sci_dev->state_machine,
358 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
359 } else
360 status = SCI_FAILURE;
361
362 scic_sds_controller_release_frame(scic, frame_index);
363 break;
364 }
365 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
366 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
367 /* The device does not process any UF received from the hardware while
368 * in this state. All unsolicited frames are forwarded to the io request
369 * object.
370 */
371 status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
372 break;
373 }
374
375 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700376}
377
Dan Williamse6225712011-05-01 16:26:09 -0700378static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700379{
Dan Williamse6225712011-05-01 16:26:09 -0700380
381 struct sci_base_state_machine *sm = &sci_dev->state_machine;
382 enum scic_sds_remote_device_states state = sm->current_state_id;
383
384 switch (state) {
385 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
386 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
387 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
388 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
389 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
390 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
391 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
392 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
393 return true;
394 default:
395 return false;
396 }
397}
398
399enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
400 u32 event_code)
401{
402 struct sci_base_state_machine *sm = &sci_dev->state_machine;
403 enum scic_sds_remote_device_states state = sm->current_state_id;
404 enum sci_status status;
405
406 switch (scu_get_event_type(event_code)) {
407 case SCU_EVENT_TYPE_RNC_OPS_MISC:
408 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
409 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
410 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
411 break;
412 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
413 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
414 status = SCI_SUCCESS;
415
416 /* Suspend the associated RNC */
417 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
418 SCI_SOFTWARE_SUSPENSION,
419 NULL, NULL);
420
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 ? "I_T_Nexus_Timeout event"
426 : "I_T_Nexus_Timeout event in wrong state");
427
428 break;
429 }
430 /* Else, fall through and treat as unhandled... */
431 default:
432 dev_dbg(scirdev_to_dev(sci_dev),
433 "%s: device: %p event code: %x: %s\n",
434 __func__, sci_dev, event_code,
435 is_remote_device_ready(sci_dev)
436 ? "unexpected event"
437 : "unexpected event in wrong state");
438 status = SCI_FAILURE_INVALID_STATE;
439 break;
440 }
441
442 if (status != SCI_SUCCESS)
443 return status;
444
445 if (state == SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE) {
446
447 /* We pick up suspension events to handle specifically to this
448 * state. We resume the RNC right away.
449 */
450 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
451 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
452 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
453 }
454
455 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700456}
457
Dan Williams18606552011-05-01 14:57:11 -0700458static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
459 struct scic_sds_request *sci_req,
460 enum sci_status status)
Dan Williams88f3b622011-04-22 19:18:03 -0700461{
Dan Williams18606552011-05-01 14:57:11 -0700462 struct scic_sds_port *sci_port = sci_dev->owning_port;
463
464 /* cleanup requests that failed after starting on the port */
465 if (status != SCI_SUCCESS)
466 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
467 else
468 scic_sds_remote_device_increment_request_count(sci_dev);
469}
470
471enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
472 struct scic_sds_remote_device *sci_dev,
473 struct scic_sds_request *sci_req)
474{
475 struct sci_base_state_machine *sm = &sci_dev->state_machine;
476 enum scic_sds_remote_device_states state = sm->current_state_id;
477 struct scic_sds_port *sci_port = sci_dev->owning_port;
Dave Jiange76d6182011-05-04 15:02:03 -0700478 struct isci_request *ireq = sci_req->ireq;
Dan Williams18606552011-05-01 14:57:11 -0700479 enum sci_status status;
480
481 switch (state) {
482 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
483 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
484 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
485 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
486 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
487 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
488 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
489 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
490 default:
491 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
492 __func__, state);
493 return SCI_FAILURE_INVALID_STATE;
494 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
495 /* attempt to start an io request for this device object. The remote
496 * device object will issue the start request for the io and if
497 * successful it will start the request for the port object then
498 * increment its own request count.
499 */
500 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
501 if (status != SCI_SUCCESS)
502 return status;
503
504 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
505 if (status != SCI_SUCCESS)
506 break;
507
508 status = scic_sds_request_start(sci_req);
509 break;
510 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
511 /* handle the start io operation for a sata device that is in
512 * the command idle state. - Evalute the type of IO request to
513 * be started - If its an NCQ request change to NCQ substate -
514 * If its any other command change to the CMD substate
515 *
516 * If this is a softreset we may want to have a different
517 * substate.
518 */
519 enum scic_sds_remote_device_states new_state;
Dave Jiange76d6182011-05-04 15:02:03 -0700520 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700521
522 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
523 if (status != SCI_SUCCESS)
524 return status;
525
526 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
527 if (status != SCI_SUCCESS)
528 break;
529
530 status = sci_req->state_handlers->start_handler(sci_req);
531 if (status != SCI_SUCCESS)
532 break;
533
Dave Jiange76d6182011-05-04 15:02:03 -0700534 if (task->ata_task.use_ncq)
Dan Williams18606552011-05-01 14:57:11 -0700535 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
536 else {
537 sci_dev->working_request = sci_req;
538 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
539 }
540 sci_base_state_machine_change_state(sm, new_state);
541 break;
542 }
Dave Jiange76d6182011-05-04 15:02:03 -0700543 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
544 struct sas_task *task = isci_request_access_task(ireq);
545
546 if (task->ata_task.use_ncq) {
Dan Williams18606552011-05-01 14:57:11 -0700547 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
548 if (status != SCI_SUCCESS)
549 return status;
550
551 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
552 if (status != SCI_SUCCESS)
553 break;
554
555 status = sci_req->state_handlers->start_handler(sci_req);
556 } else
557 return SCI_FAILURE_INVALID_STATE;
558 break;
Dave Jiange76d6182011-05-04 15:02:03 -0700559 }
Dan Williams18606552011-05-01 14:57:11 -0700560 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
561 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
562 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
563 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
564 if (status != SCI_SUCCESS)
565 return status;
566
567 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
568 if (status != SCI_SUCCESS)
569 break;
570
571 status = scic_sds_request_start(sci_req);
572 if (status != SCI_SUCCESS)
573 break;
574
575 sci_dev->working_request = sci_req;
576 sci_base_state_machine_change_state(&sci_dev->state_machine,
577 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
578 break;
579 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
580 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
581 /* device is already handling a command it can not accept new commands
582 * until this one is complete.
583 */
584 return SCI_FAILURE_INVALID_STATE;
585 }
586
587 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
588 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700589}
590
Dan Williams10a09e62011-05-01 15:33:43 -0700591static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
592 struct scic_sds_remote_device *sci_dev,
593 struct scic_sds_request *sci_req)
Dan Williams88f3b622011-04-22 19:18:03 -0700594{
Dan Williams10a09e62011-05-01 15:33:43 -0700595 enum sci_status status;
596
597 status = scic_sds_request_complete(sci_req);
598 if (status != SCI_SUCCESS)
599 return status;
600
601 status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
602 if (status != SCI_SUCCESS)
603 return status;
604
605 scic_sds_remote_device_decrement_request_count(sci_dev);
606 return status;
607}
608
609enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
610 struct scic_sds_remote_device *sci_dev,
611 struct scic_sds_request *sci_req)
612{
613 struct sci_base_state_machine *sm = &sci_dev->state_machine;
614 enum scic_sds_remote_device_states state = sm->current_state_id;
615 struct scic_sds_port *sci_port = sci_dev->owning_port;
616 enum sci_status status;
617
618 switch (state) {
619 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
620 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
621 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
622 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
623 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
624 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
625 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
626 default:
627 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
628 __func__, state);
629 return SCI_FAILURE_INVALID_STATE;
630 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
631 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
632 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
633 status = common_complete_io(sci_port, sci_dev, sci_req);
634 break;
635 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
636 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
637 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
638 status = common_complete_io(sci_port, sci_dev, sci_req);
639 if (status != SCI_SUCCESS)
640 break;
641
642 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
643 /* This request causes hardware error, device needs to be Lun Reset.
644 * So here we force the state machine to IDLE state so the rest IOs
645 * can reach RNC state handler, these IOs will be completed by RNC with
646 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
647 */
648 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
649 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
650 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
651 break;
652 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
653 status = common_complete_io(sci_port, sci_dev, sci_req);
654 if (status != SCI_SUCCESS)
655 break;
656 sci_base_state_machine_change_state(sm, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
657 break;
658 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
659 status = common_complete_io(sci_port, sci_dev, sci_req);
660 if (status != SCI_SUCCESS)
661 break;
662
663 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
664 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
665 rnc_destruct_done,
666 sci_dev);
667 break;
668 }
669
670 if (status != SCI_SUCCESS)
671 dev_err(scirdev_to_dev(sci_dev),
672 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
673 "could not complete\n", __func__, sci_port,
674 sci_dev, sci_req, status);
675
676 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700677}
678
Dan Williams84b9b022011-05-01 15:53:25 -0700679static void scic_sds_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700680{
Dan Williams84b9b022011-05-01 15:53:25 -0700681 struct scic_sds_remote_device *sci_dev = dev;
682
683 /* we need to check if this request is still valid to continue. */
684 if (sci_dev->working_request)
685 scic_controller_continue_io(sci_dev->working_request);
686}
687
688enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
689 struct scic_sds_remote_device *sci_dev,
690 struct scic_sds_request *sci_req)
691{
692 struct sci_base_state_machine *sm = &sci_dev->state_machine;
693 enum scic_sds_remote_device_states state = sm->current_state_id;
694 struct scic_sds_port *sci_port = sci_dev->owning_port;
695 enum sci_status status;
696
697 switch (state) {
698 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
699 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
700 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
701 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
702 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
703 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
704 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
705 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
706 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
707 default:
708 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
709 __func__, state);
710 return SCI_FAILURE_INVALID_STATE;
711 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
712 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
713 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
714 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
715 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
716 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
717 if (status != SCI_SUCCESS)
718 return status;
719
720 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
721 if (status != SCI_SUCCESS)
722 goto out;
723
724 status = sci_req->state_handlers->start_handler(sci_req);
725 if (status != SCI_SUCCESS)
726 goto out;
727
728 /* Note: If the remote device state is not IDLE this will
729 * replace the request that probably resulted in the task
730 * management request.
731 */
732 sci_dev->working_request = sci_req;
733 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
734
735 /* The remote node context must cleanup the TCi to NCQ mapping
736 * table. The only way to do this correctly is to either write
737 * to the TLCR register or to invalidate and repost the RNC. In
738 * either case the remote node context state machine will take
739 * the correct action when the remote node context is suspended
740 * and later resumed.
741 */
742 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
743 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
744 scic_sds_remote_node_context_resume(&sci_dev->rnc,
745 scic_sds_remote_device_continue_request,
746 sci_dev);
747
748 out:
749 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
750 /* We need to let the controller start request handler know that
751 * it can't post TC yet. We will provide a callback function to
752 * post TC when RNC gets resumed.
753 */
754 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
755 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
756 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
757 if (status != SCI_SUCCESS)
758 return status;
759
760 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
761 if (status != SCI_SUCCESS)
762 break;
763
764 status = scic_sds_request_start(sci_req);
765 break;
766 }
767 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
768
769 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700770}
771
772/**
773 *
Dan Williams88f3b622011-04-22 19:18:03 -0700774 * @sci_dev:
775 * @request:
776 *
777 * This method takes the request and bulids an appropriate SCU context for the
778 * request and then requests the controller to post the request. none
779 */
780void scic_sds_remote_device_post_request(
781 struct scic_sds_remote_device *sci_dev,
782 u32 request)
783{
784 u32 context;
785
786 context = scic_sds_remote_device_build_command_context(sci_dev, request);
787
788 scic_sds_controller_post_request(
789 scic_sds_remote_device_get_controller(sci_dev),
790 context
791 );
792}
793
Dan Williamsab2e8f72011-04-27 16:32:45 -0700794/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700795 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700796 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700797 */
Dan Williamseb229672011-05-01 14:05:57 -0700798static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700799{
Dan Williamsab2e8f72011-04-27 16:32:45 -0700800 struct scic_sds_remote_device *sci_dev = _dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700801
Dan Williamse6225712011-05-01 16:26:09 -0700802 if (is_remote_device_ready(sci_dev))
803 return;
Dan Williams88f3b622011-04-22 19:18:03 -0700804
Dan Williamse6225712011-05-01 16:26:09 -0700805 /* go 'ready' if we are not already in a ready state */
806 sci_base_state_machine_change_state(&sci_dev->state_machine,
807 SCI_BASE_REMOTE_DEVICE_STATE_READY);
Dan Williams88f3b622011-04-22 19:18:03 -0700808}
809
Dan Williamsab2e8f72011-04-27 16:32:45 -0700810static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
811{
812 struct scic_sds_remote_device *sci_dev = _dev;
813 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
814 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
815
816 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
817 * As a result, avoid sending the ready notification.
818 */
819 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
820 isci_remote_device_ready(scic->ihost, idev);
821}
822
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000823static void scic_sds_remote_device_initial_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700824{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000825 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700826
Dan Williams88f3b622011-04-22 19:18:03 -0700827 /* Initial state is a transitional state to the stopped state */
828 sci_base_state_machine_change_state(&sci_dev->state_machine,
829 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
830}
831
832/**
Dan Williams88f3b622011-04-22 19:18:03 -0700833 * scic_remote_device_destruct() - free remote node context and destruct
834 * @remote_device: This parameter specifies the remote device to be destructed.
835 *
836 * Remote device objects are a limited resource. As such, they must be
837 * protected. Thus calls to construct and destruct are mutually exclusive and
838 * non-reentrant. The return value shall indicate if the device was
839 * successfully destructed or if some failure occurred. enum sci_status This value
840 * is returned if the device is successfully destructed.
841 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
842 * device isn't valid (e.g. it's already been destoryed, the handle isn't
843 * valid, etc.).
844 */
845static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
846{
Dan Williamsb8d82f62011-05-01 14:38:26 -0700847 struct sci_base_state_machine *sm = &sci_dev->state_machine;
848 enum scic_sds_remote_device_states state = sm->current_state_id;
849 struct scic_sds_controller *scic;
850
851 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
852 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
853 __func__, state);
854 return SCI_FAILURE_INVALID_STATE;
855 }
856
857 scic = sci_dev->owning_port->owning_controller;
858 scic_sds_controller_free_remote_node_context(scic, sci_dev,
859 sci_dev->rnc.remote_node_index);
860 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
861 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
862
863 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -0700864}
865
Dan Williams6f231dd2011-07-02 22:56:22 -0700866/**
867 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -0800868 * @ihost: This parameter specifies the isci host object.
869 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -0700870 *
871 */
Dan Williamsd9c37392011-03-03 17:59:32 -0800872static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700873{
Dan Williamsd9c37392011-03-03 17:59:32 -0800874 dev_dbg(&ihost->pdev->dev,
875 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700876
877 /* There should not be any outstanding io's. All paths to
878 * here should go through isci_remote_device_nuke_requests.
879 * If we hit this condition, we will need a way to complete
880 * io requests in process */
Dan Williamsd9c37392011-03-03 17:59:32 -0800881 while (!list_empty(&idev->reqs_in_process)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700882
Dan Williamsd9c37392011-03-03 17:59:32 -0800883 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700884 "%s: ** request list not empty! **\n", __func__);
885 BUG();
886 }
887
Dan Williams57f20f42011-04-21 18:14:45 -0700888 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -0800889 idev->domain_dev->lldd_dev = NULL;
890 idev->domain_dev = NULL;
891 idev->isci_port = NULL;
892 list_del_init(&idev->node);
Dan Williams6ad31fe2011-03-04 12:10:29 -0800893
Dan Williamsd9c37392011-03-03 17:59:32 -0800894 clear_bit(IDEV_START_PENDING, &idev->flags);
895 clear_bit(IDEV_STOP_PENDING, &idev->flags);
896 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700897}
898
Dan Williams88f3b622011-04-22 19:18:03 -0700899/**
900 * isci_remote_device_stop_complete() - This function is called by the scic
901 * when the remote device stop has completed. We mark the isci device as not
902 * ready and remove the isci remote device.
903 * @ihost: This parameter specifies the isci host object.
904 * @idev: This parameter specifies the remote device.
905 * @status: This parameter specifies status of the completion.
906 *
907 */
908static void isci_remote_device_stop_complete(struct isci_host *ihost,
909 struct isci_remote_device *idev)
910{
911 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
912
913 isci_remote_device_change_state(idev, isci_stopped);
914
915 /* after stop, we can tear down resources. */
916 isci_remote_device_deconstruct(ihost, idev);
917}
918
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000919static void scic_sds_remote_device_stopped_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700920{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000921 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700922 struct scic_sds_controller *scic;
923 struct isci_remote_device *idev;
924 struct isci_host *ihost;
925 u32 prev_state;
926
Dan Williams88f3b622011-04-22 19:18:03 -0700927 scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +0000928 ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000929 idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700930
Dan Williams88f3b622011-04-22 19:18:03 -0700931 /* If we are entering from the stopping state let the SCI User know that
932 * the stop operation has completed.
933 */
934 prev_state = sci_dev->state_machine.previous_state_id;
935 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
936 isci_remote_device_stop_complete(ihost, idev);
937
938 scic_sds_controller_remote_device_stopped(scic, sci_dev);
939}
940
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000941static void scic_sds_remote_device_starting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700942{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000943 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700944 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +0000945 struct isci_host *ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000946 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700947
Dan Williams88f3b622011-04-22 19:18:03 -0700948 isci_remote_device_not_ready(ihost, idev,
949 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
950}
951
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000952static void scic_sds_remote_device_ready_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700953{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000954 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700955 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
956 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700957
Dan Williams88f3b622011-04-22 19:18:03 -0700958 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
959
Dan Williamsab2e8f72011-04-27 16:32:45 -0700960 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
961 sci_base_state_machine_change_state(&sci_dev->state_machine,
962 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
963 } else if (dev_is_expander(dev)) {
964 sci_base_state_machine_change_state(&sci_dev->state_machine,
965 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
966 } else
967 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
Dan Williams88f3b622011-04-22 19:18:03 -0700968}
969
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000970static void scic_sds_remote_device_ready_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700971{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000972 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700973 struct domain_device *dev = sci_dev_to_domain(sci_dev);
974
975 if (dev->dev_type == SAS_END_DEV) {
976 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000977 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700978
Dan Williamsab2e8f72011-04-27 16:32:45 -0700979 isci_remote_device_not_ready(scic->ihost, idev,
Dan Williams88f3b622011-04-22 19:18:03 -0700980 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
981 }
982}
983
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000984static void scic_sds_remote_device_resetting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700985{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000986 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700987
Dan Williams88f3b622011-04-22 19:18:03 -0700988 scic_sds_remote_node_context_suspend(
989 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
990}
991
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000992static void scic_sds_remote_device_resetting_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700993{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000994 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700995
996 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
997}
998
Dan Williamsab2e8f72011-04-27 16:32:45 -0700999static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
1000{
1001 struct scic_sds_remote_device *sci_dev = object;
1002
Dan Williamsab2e8f72011-04-27 16:32:45 -07001003 sci_dev->working_request = NULL;
1004 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
1005 /*
1006 * Since the RNC is ready, it's alright to finish completion
1007 * processing (e.g. signal the remote device is ready). */
1008 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
1009 } else {
1010 scic_sds_remote_node_context_resume(&sci_dev->rnc,
1011 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
1012 sci_dev);
1013 }
1014}
1015
1016static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1017{
1018 struct scic_sds_remote_device *sci_dev = object;
1019 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1020
1021 BUG_ON(sci_dev->working_request == NULL);
1022
Dan Williamsab2e8f72011-04-27 16:32:45 -07001023 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1024 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1025}
1026
Dan Williamsab2e8f72011-04-27 16:32:45 -07001027static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1028{
1029 struct scic_sds_remote_device *sci_dev = object;
1030 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1031 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1032
Dan Williamsab2e8f72011-04-27 16:32:45 -07001033 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1034 isci_remote_device_not_ready(scic->ihost, idev,
1035 sci_dev->not_ready_reason);
1036}
1037
Dan Williamsab2e8f72011-04-27 16:32:45 -07001038static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1039{
1040 struct scic_sds_remote_device *sci_dev = object;
1041 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1042
Dan Williamsab2e8f72011-04-27 16:32:45 -07001043 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1044}
1045
1046static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1047{
1048 struct scic_sds_remote_device *sci_dev = object;
1049 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1050
1051 BUG_ON(sci_dev->working_request == NULL);
1052
Dan Williamsab2e8f72011-04-27 16:32:45 -07001053 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1054 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1055}
1056
1057static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1058{
1059 struct scic_sds_remote_device *sci_dev = object;
1060
1061 sci_dev->working_request = NULL;
1062}
Dan Williams88f3b622011-04-22 19:18:03 -07001063
1064static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1065 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1066 .enter_state = scic_sds_remote_device_initial_state_enter,
1067 },
1068 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1069 .enter_state = scic_sds_remote_device_stopped_state_enter,
1070 },
1071 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1072 .enter_state = scic_sds_remote_device_starting_state_enter,
1073 },
1074 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1075 .enter_state = scic_sds_remote_device_ready_state_enter,
1076 .exit_state = scic_sds_remote_device_ready_state_exit
1077 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001078 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1079 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1080 },
1081 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1082 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1083 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001084 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = { },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001085 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1086 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1087 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001088 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = { },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001089 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1090 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1091 },
1092 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1093 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1094 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1095 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001096 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = { },
1097 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001098 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1099 .enter_state = scic_sds_remote_device_resetting_state_enter,
1100 .exit_state = scic_sds_remote_device_resetting_state_exit
1101 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001102 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001103};
1104
1105/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001106 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001107 * @sci_port: SAS/SATA port through which this device is accessed.
1108 * @sci_dev: remote device to construct
1109 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001110 * This routine just performs benign initialization and does not
1111 * allocate the remote_node_context which is left to
1112 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1113 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001114 */
1115static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1116 struct scic_sds_remote_device *sci_dev)
1117{
1118 sci_dev->owning_port = sci_port;
1119 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001120
1121 sci_base_state_machine_construct(
1122 &sci_dev->state_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001123 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001124 scic_sds_remote_device_state_table,
1125 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1126 );
1127
1128 sci_base_state_machine_start(
1129 &sci_dev->state_machine
1130 );
1131
1132 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1133 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001134}
1135
1136/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001137 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001138 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001139 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1140 * the device is known to the SCI Core since it is contained in the
1141 * scic_phy object. Remote node context(s) is/are a global resource
1142 * allocated by this routine, freed by scic_remote_device_destruct().
1143 *
1144 * Returns:
1145 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1146 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1147 * sata-only controller instance.
1148 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001149 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001150static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1151 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001152{
1153 enum sci_status status;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001154 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001155
Dan Williamsb87ee302011-04-25 11:48:29 -07001156 scic_remote_device_construct(sci_port, sci_dev);
1157
Dan Williams88f3b622011-04-22 19:18:03 -07001158 /*
1159 * This information is request to determine how many remote node context
1160 * entries will be needed to store the remote node.
1161 */
Dan Williams88f3b622011-04-22 19:18:03 -07001162 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001163 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1164 sci_dev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001165 &sci_dev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001166
Dan Williamsa1a113b2011-04-21 18:44:45 -07001167 if (status != SCI_SUCCESS)
1168 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001169
Dan Williamsab2e8f72011-04-27 16:32:45 -07001170 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1171 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1172 /* pass */;
1173 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001174 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001175
Dan Williamsa1a113b2011-04-21 18:44:45 -07001176 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001177
Dan Williamsa1a113b2011-04-21 18:44:45 -07001178 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1179 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001180
Dan Williamsa1a113b2011-04-21 18:44:45 -07001181 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001182}
1183
Dan Williams88f3b622011-04-22 19:18:03 -07001184/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001185 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001186 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001187 * Remote node context(s) is/are a global resource allocated by this
1188 * routine, freed by scic_remote_device_destruct().
1189 *
1190 * Returns:
1191 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1192 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1193 * sata-only controller instance.
1194 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001195 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001196static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001197 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001198{
Dan Williamsa1a113b2011-04-21 18:44:45 -07001199 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001200 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001201
Dan Williamsb87ee302011-04-25 11:48:29 -07001202 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001203
Dan Williamsab2e8f72011-04-27 16:32:45 -07001204 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1205 sci_dev,
1206 &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001207 if (status != SCI_SUCCESS)
1208 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001209
Dan Williamsab2e8f72011-04-27 16:32:45 -07001210 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1211 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1212 /* pass */;
1213 else
1214 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001215
Dan Williamsa1a113b2011-04-21 18:44:45 -07001216 /*
1217 * For SAS-2 the physical link rate is actually a logical link
1218 * rate that incorporates multiplexing. The SCU doesn't
1219 * incorporate multiplexing and for the purposes of the
1220 * connection the logical link rate is that same as the
1221 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1222 * one another, so this code works for both situations. */
1223 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001224 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001225
Dan Williamsa1a113b2011-04-21 18:44:45 -07001226 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1227 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001228
Dan Williamsab2e8f72011-04-27 16:32:45 -07001229 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001230}
1231
1232/**
1233 * scic_remote_device_start() - This method will start the supplied remote
1234 * device. This method enables normal IO requests to flow through to the
1235 * remote device.
1236 * @remote_device: This parameter specifies the device to be started.
1237 * @timeout: This parameter specifies the number of milliseconds in which the
1238 * start operation should complete.
1239 *
1240 * An indication of whether the device was successfully started. SCI_SUCCESS
1241 * This value is returned if the device was successfully started.
1242 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1243 * the device when there have been no phys added to it.
1244 */
1245static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
Dan Williamseb229672011-05-01 14:05:57 -07001246 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001247{
Dan Williamseb229672011-05-01 14:05:57 -07001248 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1249 enum scic_sds_remote_device_states state = sm->current_state_id;
1250 enum sci_status status;
1251
1252 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1253 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1254 __func__, state);
1255 return SCI_FAILURE_INVALID_STATE;
1256 }
1257
1258 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1259 remote_device_resume_done,
1260 sci_dev);
1261 if (status != SCI_SUCCESS)
1262 return status;
1263
1264 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1265
1266 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001267}
Dan Williams6f231dd2011-07-02 22:56:22 -07001268
Dan Williams00d680e2011-04-25 14:29:29 -07001269static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1270 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001271{
Dan Williams00d680e2011-04-25 14:29:29 -07001272 struct scic_sds_port *sci_port = iport->sci_port_handle;
1273 struct isci_host *ihost = iport->isci_host;
1274 struct domain_device *dev = idev->domain_dev;
1275 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001276
Dan Williams00d680e2011-04-25 14:29:29 -07001277 if (dev->parent && dev_is_expander(dev->parent))
1278 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1279 else
1280 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001281
1282 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001283 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1284 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001285
1286 return status;
1287 }
1288
Dan Williams6f231dd2011-07-02 22:56:22 -07001289 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001290 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001291
Dan Williams00d680e2011-04-25 14:29:29 -07001292 if (status != SCI_SUCCESS)
1293 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1294 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001295
1296 return status;
1297}
1298
Dan Williams4393aa42011-03-31 13:10:44 -07001299void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001300{
1301 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001302
Dan Williams4393aa42011-03-31 13:10:44 -07001303 dev_dbg(&ihost->pdev->dev,
1304 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001305
1306 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -07001307 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -07001308
Dan Williams4393aa42011-03-31 13:10:44 -07001309 dev_dbg(&ihost->pdev->dev,
1310 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001311}
1312
Dan Williams6f231dd2011-07-02 22:56:22 -07001313/**
1314 * This function builds the isci_remote_device when a libsas dev_found message
1315 * is received.
1316 * @isci_host: This parameter specifies the isci host object.
1317 * @port: This parameter specifies the isci_port conected to this device.
1318 *
1319 * pointer to new isci_remote_device.
1320 */
1321static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001322isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001323{
Dan Williamsd9c37392011-03-03 17:59:32 -08001324 struct isci_remote_device *idev;
1325 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001326
Dan Williamsd9c37392011-03-03 17:59:32 -08001327 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001328 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001329 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1330 break;
1331 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001332
Dan Williamsd9c37392011-03-03 17:59:32 -08001333 if (i >= SCI_MAX_REMOTE_DEVICES) {
1334 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001335 return NULL;
1336 }
1337
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001338 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1339 return NULL;
1340
1341 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1342 return NULL;
1343
Dan Williamsd9c37392011-03-03 17:59:32 -08001344 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001345
Dan Williamsd9c37392011-03-03 17:59:32 -08001346 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001347}
Dan Williams6f231dd2011-07-02 22:56:22 -07001348
1349/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001350 * isci_remote_device_stop() - This function is called internally to stop the
1351 * remote device.
1352 * @isci_host: This parameter specifies the isci host object.
1353 * @isci_device: This parameter specifies the remote device.
1354 *
1355 * The status of the scic request to stop.
1356 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001357enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001358{
1359 enum sci_status status;
1360 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001361
Dan Williams6ad31fe2011-03-04 12:10:29 -08001362 dev_dbg(&ihost->pdev->dev,
1363 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001364
Dan Williams6ad31fe2011-03-04 12:10:29 -08001365 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001366
1367 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001368 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001369
Dan Williams6ad31fe2011-03-04 12:10:29 -08001370 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001371
Dan Williams6ad31fe2011-03-04 12:10:29 -08001372 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07001373 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001374 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001375
1376 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -08001377 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -08001378 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08001379 clear_bit(IDEV_ALLOCATED, &idev->flags);
1380 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001381
Dan Williams6ad31fe2011-03-04 12:10:29 -08001382 dev_dbg(&ihost->pdev->dev,
1383 "%s: idev = %p - after completion wait\n",
1384 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001385
Dan Williams6f231dd2011-07-02 22:56:22 -07001386 return status;
1387}
1388
1389/**
1390 * isci_remote_device_gone() - This function is called by libsas when a domain
1391 * device is removed.
1392 * @domain_device: This parameter specifies the libsas domain device.
1393 *
1394 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001395void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001396{
Dan Williams4393aa42011-03-31 13:10:44 -07001397 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001398 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001399
Dan Williams6ad31fe2011-03-04 12:10:29 -08001400 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001401 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001402 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001403
Dan Williams6ad31fe2011-03-04 12:10:29 -08001404 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001405}
1406
1407
1408/**
1409 * isci_remote_device_found() - This function is called by libsas when a remote
1410 * device is discovered. A remote device object is created and started. the
1411 * function then sleeps until the sci core device started message is
1412 * received.
1413 * @domain_device: This parameter specifies the libsas domain device.
1414 *
1415 * status, zero indicates success.
1416 */
1417int isci_remote_device_found(struct domain_device *domain_dev)
1418{
Dan Williams4393aa42011-03-31 13:10:44 -07001419 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001420 struct isci_port *isci_port;
1421 struct isci_phy *isci_phy;
1422 struct asd_sas_port *sas_port;
1423 struct asd_sas_phy *sas_phy;
1424 struct isci_remote_device *isci_device;
1425 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001426
Dan Williams6f231dd2011-07-02 22:56:22 -07001427 dev_dbg(&isci_host->pdev->dev,
1428 "%s: domain_device = %p\n", __func__, domain_dev);
1429
Dan Williams0cf89d12011-02-18 09:25:07 -08001430 wait_for_start(isci_host);
1431
Dan Williams6f231dd2011-07-02 22:56:22 -07001432 sas_port = domain_dev->port;
1433 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1434 port_phy_el);
1435 isci_phy = to_isci_phy(sas_phy);
1436 isci_port = isci_phy->isci_port;
1437
1438 /* we are being called for a device on this port,
1439 * so it has to come up eventually
1440 */
1441 wait_for_completion(&isci_port->start_complete);
1442
1443 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1444 (isci_stopped == isci_port_get_state(isci_port)))
1445 return -ENODEV;
1446
1447 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001448 if (!isci_device)
1449 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001450
1451 INIT_LIST_HEAD(&isci_device->node);
1452 domain_dev->lldd_dev = isci_device;
1453 isci_device->domain_dev = domain_dev;
1454 isci_device->isci_port = isci_port;
1455 isci_remote_device_change_state(isci_device, isci_starting);
1456
1457
Dan Williams1a380452011-03-03 18:01:43 -08001458 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001459 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1460
Dan Williams6ad31fe2011-03-04 12:10:29 -08001461 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001462 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -08001463 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001464
Dan Williams6f231dd2011-07-02 22:56:22 -07001465 dev_dbg(&isci_host->pdev->dev,
1466 "%s: isci_device = %p\n",
1467 __func__, isci_device);
1468
1469 if (status != SCI_SUCCESS) {
1470
Dan Williams1a380452011-03-03 18:01:43 -08001471 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001472 isci_remote_device_deconstruct(
1473 isci_host,
1474 isci_device
1475 );
Dan Williams1a380452011-03-03 18:01:43 -08001476 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001477 return -ENODEV;
1478 }
1479
Dan Williams6ad31fe2011-03-04 12:10:29 -08001480 /* wait for the device ready callback. */
1481 wait_for_device_start(isci_host, isci_device);
1482
Dan Williams6f231dd2011-07-02 22:56:22 -07001483 return 0;
1484}
1485/**
1486 * isci_device_is_reset_pending() - This function will check if there is any
1487 * pending reset condition on the device.
1488 * @request: This parameter is the isci_device object.
1489 *
1490 * true if there is a reset pending for the device.
1491 */
1492bool isci_device_is_reset_pending(
1493 struct isci_host *isci_host,
1494 struct isci_remote_device *isci_device)
1495{
1496 struct isci_request *isci_request;
1497 struct isci_request *tmp_req;
1498 bool reset_is_pending = false;
1499 unsigned long flags;
1500
1501 dev_dbg(&isci_host->pdev->dev,
1502 "%s: isci_device = %p\n", __func__, isci_device);
1503
1504 spin_lock_irqsave(&isci_host->scic_lock, flags);
1505
1506 /* Check for reset on all pending requests. */
1507 list_for_each_entry_safe(isci_request, tmp_req,
1508 &isci_device->reqs_in_process, dev_node) {
1509 dev_dbg(&isci_host->pdev->dev,
1510 "%s: isci_device = %p request = %p\n",
1511 __func__, isci_device, isci_request);
1512
1513 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001514 struct sas_task *task = isci_request_access_task(
1515 isci_request);
1516
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001517 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001518 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1519 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001520 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001521 }
1522 }
1523
1524 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1525
1526 dev_dbg(&isci_host->pdev->dev,
1527 "%s: isci_device = %p reset_is_pending = %d\n",
1528 __func__, isci_device, reset_is_pending);
1529
1530 return reset_is_pending;
1531}
1532
1533/**
1534 * isci_device_clear_reset_pending() - This function will clear if any pending
1535 * reset condition flags on the device.
1536 * @request: This parameter is the isci_device object.
1537 *
1538 * true if there is a reset pending for the device.
1539 */
Dan Williams4393aa42011-03-31 13:10:44 -07001540void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001541{
1542 struct isci_request *isci_request;
1543 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07001544 unsigned long flags = 0;
1545
Dan Williams4393aa42011-03-31 13:10:44 -07001546 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1547 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07001548
Dan Williams4393aa42011-03-31 13:10:44 -07001549 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001550
1551 /* Clear reset pending on all pending requests. */
1552 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07001553 &idev->reqs_in_process, dev_node) {
1554 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1555 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001556
1557 if (isci_request->ttype == io_task) {
1558
1559 unsigned long flags2;
1560 struct sas_task *task = isci_request_access_task(
1561 isci_request);
1562
1563 spin_lock_irqsave(&task->task_state_lock, flags2);
1564 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1565 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1566 }
1567 }
Dan Williams4393aa42011-03-31 13:10:44 -07001568 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001569}