blob: 68b63b04be198ea0d6dea12a17b2a0b414014622 [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
Dave Jiang2d9c2242011-05-04 18:45:05 -070055#include <scsi/sas.h>
Dan Williams6f231dd2011-07-02 22:56:22 -070056#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "port.h"
58#include "remote_device.h"
59#include "request.h"
Dan Williams88f3b622011-04-22 19:18:03 -070060#include "remote_node_context.h"
Dan Williams88f3b622011-04-22 19:18:03 -070061#include "scu_event_codes.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070062#include "task.h"
63
Dan Williamsab2e8f72011-04-27 16:32:45 -070064/**
65 * isci_remote_device_change_state() - This function gets the status of the
66 * remote_device object.
67 * @isci_device: This parameter points to the isci_remote_device object
68 *
69 * status of the object as a isci_status enum.
70 */
71void isci_remote_device_change_state(
72 struct isci_remote_device *isci_device,
73 enum isci_status status)
74{
75 unsigned long flags;
76
77 spin_lock_irqsave(&isci_device->state_lock, flags);
78 isci_device->status = status;
79 spin_unlock_irqrestore(&isci_device->state_lock, flags);
80}
81
82/**
83 * isci_remote_device_not_ready() - This function is called by the scic when
84 * the remote device is not ready. We mark the isci device as ready (not
85 * "ready_for_io") and signal the waiting proccess.
86 * @isci_host: This parameter specifies the isci host object.
87 * @isci_device: This parameter specifies the remote device
88 *
89 */
90static void isci_remote_device_not_ready(struct isci_host *ihost,
91 struct isci_remote_device *idev, u32 reason)
92{
93 dev_dbg(&ihost->pdev->dev,
94 "%s: isci_device = %p\n", __func__, idev);
95
96 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
97 isci_remote_device_change_state(idev, isci_stopping);
98 else
99 /* device ready is actually a "not ready for io" state. */
100 isci_remote_device_change_state(idev, isci_ready);
101}
102
103/**
104 * isci_remote_device_ready() - This function is called by the scic when the
105 * remote device is ready. We mark the isci device as ready and signal the
106 * waiting proccess.
107 * @ihost: our valid isci_host
108 * @idev: remote device
109 *
110 */
111static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
112{
113 dev_dbg(&ihost->pdev->dev,
114 "%s: idev = %p\n", __func__, idev);
115
116 isci_remote_device_change_state(idev, isci_ready_for_io);
117 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
118 wake_up(&ihost->eventq);
119}
120
Dan Williamsec575662011-05-01 14:19:25 -0700121/* called once the remote node context is ready to be freed.
122 * The remote device can now report that its stop operation is complete. none
123 */
124static void rnc_destruct_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700125{
Dan Williamsec575662011-05-01 14:19:25 -0700126 struct scic_sds_remote_device *sci_dev = _dev;
127
128 BUG_ON(sci_dev->started_request_count != 0);
129 sci_base_state_machine_change_state(&sci_dev->state_machine,
130 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
131}
132
133static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
134{
135 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
136 u32 i, request_count = sci_dev->started_request_count;
137 enum sci_status status = SCI_SUCCESS;
138
139 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
140 struct scic_sds_request *sci_req;
141 enum sci_status s;
142
143 sci_req = scic->io_request_table[i];
144 if (!sci_req || sci_req->target_device != sci_dev)
145 continue;
146 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
147 if (s != SCI_SUCCESS)
148 status = s;
149 }
150
151 return status;
152}
153
154enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
155 u32 timeout)
156{
157 struct sci_base_state_machine *sm = &sci_dev->state_machine;
158 enum scic_sds_remote_device_states state = sm->current_state_id;
159
160 switch (state) {
161 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
162 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
163 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
164 default:
165 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
166 __func__, state);
167 return SCI_FAILURE_INVALID_STATE;
168 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
169 return SCI_SUCCESS;
170 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
171 /* device not started so there had better be no requests */
172 BUG_ON(sci_dev->started_request_count != 0);
173 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
174 rnc_destruct_done, sci_dev);
175 /* Transition to the stopping state and wait for the
176 * remote node to complete being posted and invalidated.
177 */
178 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
179 return SCI_SUCCESS;
180 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
181 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
182 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
183 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
184 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
185 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
186 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
187 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
188 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
189 if (sci_dev->started_request_count == 0) {
190 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
191 rnc_destruct_done, sci_dev);
192 return SCI_SUCCESS;
193 } else
194 return scic_sds_remote_device_terminate_requests(sci_dev);
195 break;
196 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
197 /* All requests should have been terminated, but if there is an
198 * attempt to stop a device already in the stopping state, then
199 * try again to terminate.
200 */
201 return scic_sds_remote_device_terminate_requests(sci_dev);
202 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
203 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
204 return SCI_SUCCESS;
205 }
Dan Williams88f3b622011-04-22 19:18:03 -0700206}
Dan Williams6f231dd2011-07-02 22:56:22 -0700207
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700208enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700209{
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700210 struct sci_base_state_machine *sm = &sci_dev->state_machine;
211 enum scic_sds_remote_device_states state = sm->current_state_id;
212
213 switch (state) {
214 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
215 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
216 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
217 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
218 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
219 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
220 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
221 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
222 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
223 default:
224 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
225 __func__, state);
226 return SCI_FAILURE_INVALID_STATE;
227 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
228 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
229 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
230 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
231 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
232 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
233 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
234 return SCI_SUCCESS;
235 }
Dan Williams88f3b622011-04-22 19:18:03 -0700236}
237
Dan Williams81515182011-05-01 14:53:00 -0700238enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700239{
Dan Williams81515182011-05-01 14:53:00 -0700240 struct sci_base_state_machine *sm = &sci_dev->state_machine;
241 enum scic_sds_remote_device_states state = sm->current_state_id;
Dan Williams88f3b622011-04-22 19:18:03 -0700242
Dan Williams81515182011-05-01 14:53:00 -0700243 if (state != SCI_BASE_REMOTE_DEVICE_STATE_RESETTING) {
244 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
245 __func__, state);
246 return SCI_FAILURE_INVALID_STATE;
247 }
248
249 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_READY);
250 return SCI_SUCCESS;
251}
Dan Williams88f3b622011-04-22 19:18:03 -0700252
Dan Williams323f0ec2011-05-01 16:15:47 -0700253enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
254 u32 suspend_type)
Dan Williams88f3b622011-04-22 19:18:03 -0700255{
Dan Williams323f0ec2011-05-01 16:15:47 -0700256 struct sci_base_state_machine *sm = &sci_dev->state_machine;
257 enum scic_sds_remote_device_states state = sm->current_state_id;
258
259 if (state != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD) {
260 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
261 __func__, state);
262 return SCI_FAILURE_INVALID_STATE;
263 }
264
265 return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
266 suspend_type, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700267}
268
Dan Williams01bec772011-05-01 16:51:11 -0700269enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
270 u32 frame_index)
Dan Williams88f3b622011-04-22 19:18:03 -0700271{
Dan Williams01bec772011-05-01 16:51:11 -0700272 struct sci_base_state_machine *sm = &sci_dev->state_machine;
273 enum scic_sds_remote_device_states state = sm->current_state_id;
274 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
275 enum sci_status status;
276
277 switch (state) {
278 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
279 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
280 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
281 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
282 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
283 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
284 default:
285 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
286 __func__, state);
287 /* Return the frame back to the controller */
288 scic_sds_controller_release_frame(scic, frame_index);
289 return SCI_FAILURE_INVALID_STATE;
290 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
291 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
292 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
293 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
294 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
295 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING: {
296 struct scic_sds_request *sci_req;
Dave Jiang2d9c2242011-05-04 18:45:05 -0700297 struct ssp_frame_hdr hdr;
298 void *frame_header;
299 ssize_t word_cnt;
Dan Williams01bec772011-05-01 16:51:11 -0700300
301 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
302 frame_index,
Dave Jiang2d9c2242011-05-04 18:45:05 -0700303 &frame_header);
Dan Williams01bec772011-05-01 16:51:11 -0700304 if (status != SCI_SUCCESS)
305 return status;
306
Dave Jiang2d9c2242011-05-04 18:45:05 -0700307 word_cnt = sizeof(hdr) / sizeof(u32);
308 sci_swab32_cpy(&hdr, frame_header, word_cnt);
309
310 sci_req = scic_request_by_tag(scic, be16_to_cpu(hdr.tag));
Dan Williams01bec772011-05-01 16:51:11 -0700311 if (sci_req && sci_req->target_device == sci_dev) {
312 /* The IO request is now in charge of releasing the frame */
Dan Williamsd1c637c2011-05-11 08:27:47 -0700313 status = scic_sds_io_request_frame_handler(sci_req, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700314 } else {
315 /* We could not map this tag to a valid IO
316 * request Just toss the frame and continue
317 */
318 scic_sds_controller_release_frame(scic, frame_index);
319 }
320 break;
321 }
322 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700323 struct dev_to_host_fis *hdr;
Dan Williams01bec772011-05-01 16:51:11 -0700324
325 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
326 frame_index,
327 (void **)&hdr);
328 if (status != SCI_SUCCESS)
329 return status;
330
Dave Jiange76d6182011-05-04 15:02:03 -0700331 if (hdr->fis_type == FIS_SETDEVBITS &&
332 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700333 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
334
335 /* TODO Check sactive and complete associated IO if any. */
336 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
Dave Jiange76d6182011-05-04 15:02:03 -0700337 } else if (hdr->fis_type == FIS_REGD2H &&
338 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700339 /*
340 * Some devices return D2H FIS when an NCQ error is detected.
341 * Treat this like an SDB error FIS ready reason.
342 */
343 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
344 sci_base_state_machine_change_state(&sci_dev->state_machine,
345 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
346 } else
347 status = SCI_FAILURE;
348
349 scic_sds_controller_release_frame(scic, frame_index);
350 break;
351 }
352 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
353 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
354 /* The device does not process any UF received from the hardware while
355 * in this state. All unsolicited frames are forwarded to the io request
356 * object.
357 */
358 status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
359 break;
360 }
361
362 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700363}
364
Dan Williamse6225712011-05-01 16:26:09 -0700365static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700366{
Dan Williamse6225712011-05-01 16:26:09 -0700367
368 struct sci_base_state_machine *sm = &sci_dev->state_machine;
369 enum scic_sds_remote_device_states state = sm->current_state_id;
370
371 switch (state) {
372 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
373 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
374 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
375 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
376 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
377 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
378 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
379 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
380 return true;
381 default:
382 return false;
383 }
384}
385
386enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
387 u32 event_code)
388{
389 struct sci_base_state_machine *sm = &sci_dev->state_machine;
390 enum scic_sds_remote_device_states state = sm->current_state_id;
391 enum sci_status status;
392
393 switch (scu_get_event_type(event_code)) {
394 case SCU_EVENT_TYPE_RNC_OPS_MISC:
395 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
396 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
397 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
398 break;
399 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
400 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
401 status = SCI_SUCCESS;
402
403 /* Suspend the associated RNC */
404 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
405 SCI_SOFTWARE_SUSPENSION,
406 NULL, NULL);
407
408 dev_dbg(scirdev_to_dev(sci_dev),
409 "%s: device: %p event code: %x: %s\n",
410 __func__, sci_dev, event_code,
411 is_remote_device_ready(sci_dev)
412 ? "I_T_Nexus_Timeout event"
413 : "I_T_Nexus_Timeout event in wrong state");
414
415 break;
416 }
417 /* Else, fall through and treat as unhandled... */
418 default:
419 dev_dbg(scirdev_to_dev(sci_dev),
420 "%s: device: %p event code: %x: %s\n",
421 __func__, sci_dev, event_code,
422 is_remote_device_ready(sci_dev)
423 ? "unexpected event"
424 : "unexpected event in wrong state");
425 status = SCI_FAILURE_INVALID_STATE;
426 break;
427 }
428
429 if (status != SCI_SUCCESS)
430 return status;
431
432 if (state == SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE) {
433
434 /* We pick up suspension events to handle specifically to this
435 * state. We resume the RNC right away.
436 */
437 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
438 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
439 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
440 }
441
442 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700443}
444
Dan Williams18606552011-05-01 14:57:11 -0700445static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
446 struct scic_sds_request *sci_req,
447 enum sci_status status)
Dan Williams88f3b622011-04-22 19:18:03 -0700448{
Dan Williams18606552011-05-01 14:57:11 -0700449 struct scic_sds_port *sci_port = sci_dev->owning_port;
450
451 /* cleanup requests that failed after starting on the port */
452 if (status != SCI_SUCCESS)
453 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
454 else
455 scic_sds_remote_device_increment_request_count(sci_dev);
456}
457
458enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
459 struct scic_sds_remote_device *sci_dev,
460 struct scic_sds_request *sci_req)
461{
462 struct sci_base_state_machine *sm = &sci_dev->state_machine;
463 enum scic_sds_remote_device_states state = sm->current_state_id;
464 struct scic_sds_port *sci_port = sci_dev->owning_port;
Dan Williams67ea8382011-05-08 11:47:15 -0700465 struct isci_request *ireq = sci_req_to_ireq(sci_req);
Dan Williams18606552011-05-01 14:57:11 -0700466 enum sci_status status;
467
468 switch (state) {
469 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
470 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
471 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
472 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
473 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
474 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
475 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
476 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
477 default:
478 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
479 __func__, state);
480 return SCI_FAILURE_INVALID_STATE;
481 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
482 /* attempt to start an io request for this device object. The remote
483 * device object will issue the start request for the io and if
484 * successful it will start the request for the port object then
485 * increment its own request count.
486 */
487 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
488 if (status != SCI_SUCCESS)
489 return status;
490
491 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
492 if (status != SCI_SUCCESS)
493 break;
494
495 status = scic_sds_request_start(sci_req);
496 break;
497 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
498 /* handle the start io operation for a sata device that is in
499 * the command idle state. - Evalute the type of IO request to
500 * be started - If its an NCQ request change to NCQ substate -
501 * If its any other command change to the CMD substate
502 *
503 * If this is a softreset we may want to have a different
504 * substate.
505 */
506 enum scic_sds_remote_device_states new_state;
Dave Jiange76d6182011-05-04 15:02:03 -0700507 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700508
509 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
510 if (status != SCI_SUCCESS)
511 return status;
512
513 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
514 if (status != SCI_SUCCESS)
515 break;
516
Piotr Sawickif4636a72011-05-10 23:50:32 +0000517 status = scic_sds_request_start(sci_req);
Dan Williams18606552011-05-01 14:57:11 -0700518 if (status != SCI_SUCCESS)
519 break;
520
Dave Jiange76d6182011-05-04 15:02:03 -0700521 if (task->ata_task.use_ncq)
Dan Williams18606552011-05-01 14:57:11 -0700522 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
523 else {
524 sci_dev->working_request = sci_req;
525 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
526 }
527 sci_base_state_machine_change_state(sm, new_state);
528 break;
529 }
Dave Jiange76d6182011-05-04 15:02:03 -0700530 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
531 struct sas_task *task = isci_request_access_task(ireq);
532
533 if (task->ata_task.use_ncq) {
Dan Williams18606552011-05-01 14:57:11 -0700534 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
535 if (status != SCI_SUCCESS)
536 return status;
537
538 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
539 if (status != SCI_SUCCESS)
540 break;
541
Piotr Sawickif4636a72011-05-10 23:50:32 +0000542 status = scic_sds_request_start(sci_req);
Dan Williams18606552011-05-01 14:57:11 -0700543 } else
544 return SCI_FAILURE_INVALID_STATE;
545 break;
Dave Jiange76d6182011-05-04 15:02:03 -0700546 }
Dan Williams18606552011-05-01 14:57:11 -0700547 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
548 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
549 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
550 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
551 if (status != SCI_SUCCESS)
552 return status;
553
554 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
555 if (status != SCI_SUCCESS)
556 break;
557
558 status = scic_sds_request_start(sci_req);
559 if (status != SCI_SUCCESS)
560 break;
561
562 sci_dev->working_request = sci_req;
563 sci_base_state_machine_change_state(&sci_dev->state_machine,
564 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
565 break;
566 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
567 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
568 /* device is already handling a command it can not accept new commands
569 * until this one is complete.
570 */
571 return SCI_FAILURE_INVALID_STATE;
572 }
573
574 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
575 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700576}
577
Dan Williams10a09e62011-05-01 15:33:43 -0700578static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
579 struct scic_sds_remote_device *sci_dev,
580 struct scic_sds_request *sci_req)
Dan Williams88f3b622011-04-22 19:18:03 -0700581{
Dan Williams10a09e62011-05-01 15:33:43 -0700582 enum sci_status status;
583
584 status = scic_sds_request_complete(sci_req);
585 if (status != SCI_SUCCESS)
586 return status;
587
588 status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
589 if (status != SCI_SUCCESS)
590 return status;
591
592 scic_sds_remote_device_decrement_request_count(sci_dev);
593 return status;
594}
595
596enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
597 struct scic_sds_remote_device *sci_dev,
598 struct scic_sds_request *sci_req)
599{
600 struct sci_base_state_machine *sm = &sci_dev->state_machine;
601 enum scic_sds_remote_device_states state = sm->current_state_id;
602 struct scic_sds_port *sci_port = sci_dev->owning_port;
603 enum sci_status status;
604
605 switch (state) {
606 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
607 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
608 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
609 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
610 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
611 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
612 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
613 default:
614 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
615 __func__, state);
616 return SCI_FAILURE_INVALID_STATE;
617 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
618 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
619 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
620 status = common_complete_io(sci_port, sci_dev, sci_req);
621 break;
622 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
623 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
624 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
625 status = common_complete_io(sci_port, sci_dev, sci_req);
626 if (status != SCI_SUCCESS)
627 break;
628
629 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
630 /* This request causes hardware error, device needs to be Lun Reset.
631 * So here we force the state machine to IDLE state so the rest IOs
632 * can reach RNC state handler, these IOs will be completed by RNC with
633 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
634 */
635 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
636 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
637 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
638 break;
639 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
640 status = common_complete_io(sci_port, sci_dev, sci_req);
641 if (status != SCI_SUCCESS)
642 break;
643 sci_base_state_machine_change_state(sm, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
644 break;
645 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
646 status = common_complete_io(sci_port, sci_dev, sci_req);
647 if (status != SCI_SUCCESS)
648 break;
649
650 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
651 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
652 rnc_destruct_done,
653 sci_dev);
654 break;
655 }
656
657 if (status != SCI_SUCCESS)
658 dev_err(scirdev_to_dev(sci_dev),
659 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
660 "could not complete\n", __func__, sci_port,
661 sci_dev, sci_req, status);
662
663 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700664}
665
Dan Williams84b9b022011-05-01 15:53:25 -0700666static void scic_sds_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700667{
Dan Williams84b9b022011-05-01 15:53:25 -0700668 struct scic_sds_remote_device *sci_dev = dev;
669
670 /* we need to check if this request is still valid to continue. */
671 if (sci_dev->working_request)
672 scic_controller_continue_io(sci_dev->working_request);
673}
674
675enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
676 struct scic_sds_remote_device *sci_dev,
677 struct scic_sds_request *sci_req)
678{
679 struct sci_base_state_machine *sm = &sci_dev->state_machine;
680 enum scic_sds_remote_device_states state = sm->current_state_id;
681 struct scic_sds_port *sci_port = sci_dev->owning_port;
682 enum sci_status status;
683
684 switch (state) {
685 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
686 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
687 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
688 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
689 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
690 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
691 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
692 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
693 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
694 default:
695 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
696 __func__, state);
697 return SCI_FAILURE_INVALID_STATE;
698 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
699 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
700 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
701 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
702 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
703 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
704 if (status != SCI_SUCCESS)
705 return status;
706
707 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
708 if (status != SCI_SUCCESS)
709 goto out;
710
Piotr Sawickif4636a72011-05-10 23:50:32 +0000711 status = scic_sds_request_start(sci_req);
Dan Williams84b9b022011-05-01 15:53:25 -0700712 if (status != SCI_SUCCESS)
713 goto out;
714
715 /* Note: If the remote device state is not IDLE this will
716 * replace the request that probably resulted in the task
717 * management request.
718 */
719 sci_dev->working_request = sci_req;
720 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
721
722 /* The remote node context must cleanup the TCi to NCQ mapping
723 * table. The only way to do this correctly is to either write
724 * to the TLCR register or to invalidate and repost the RNC. In
725 * either case the remote node context state machine will take
726 * the correct action when the remote node context is suspended
727 * and later resumed.
728 */
729 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
730 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
731 scic_sds_remote_node_context_resume(&sci_dev->rnc,
732 scic_sds_remote_device_continue_request,
733 sci_dev);
734
735 out:
736 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
737 /* We need to let the controller start request handler know that
738 * it can't post TC yet. We will provide a callback function to
739 * post TC when RNC gets resumed.
740 */
741 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
742 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
743 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
744 if (status != SCI_SUCCESS)
745 return status;
746
747 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
748 if (status != SCI_SUCCESS)
749 break;
750
751 status = scic_sds_request_start(sci_req);
752 break;
753 }
754 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
755
756 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700757}
758
759/**
760 *
Dan Williams88f3b622011-04-22 19:18:03 -0700761 * @sci_dev:
762 * @request:
763 *
764 * This method takes the request and bulids an appropriate SCU context for the
765 * request and then requests the controller to post the request. none
766 */
767void scic_sds_remote_device_post_request(
768 struct scic_sds_remote_device *sci_dev,
769 u32 request)
770{
771 u32 context;
772
773 context = scic_sds_remote_device_build_command_context(sci_dev, request);
774
775 scic_sds_controller_post_request(
776 scic_sds_remote_device_get_controller(sci_dev),
777 context
778 );
779}
780
Dan Williamsab2e8f72011-04-27 16:32:45 -0700781/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700782 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700783 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700784 */
Dan Williamseb229672011-05-01 14:05:57 -0700785static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700786{
Dan Williamsab2e8f72011-04-27 16:32:45 -0700787 struct scic_sds_remote_device *sci_dev = _dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700788
Dan Williamse6225712011-05-01 16:26:09 -0700789 if (is_remote_device_ready(sci_dev))
790 return;
Dan Williams88f3b622011-04-22 19:18:03 -0700791
Dan Williamse6225712011-05-01 16:26:09 -0700792 /* go 'ready' if we are not already in a ready state */
793 sci_base_state_machine_change_state(&sci_dev->state_machine,
794 SCI_BASE_REMOTE_DEVICE_STATE_READY);
Dan Williams88f3b622011-04-22 19:18:03 -0700795}
796
Dan Williamsab2e8f72011-04-27 16:32:45 -0700797static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
798{
799 struct scic_sds_remote_device *sci_dev = _dev;
800 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
801 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
802
803 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
804 * As a result, avoid sending the ready notification.
805 */
806 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000807 isci_remote_device_ready(scic_to_ihost(scic), idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700808}
809
Dan Williams9269e0e2011-05-12 07:42:17 -0700810static void scic_sds_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700811{
Dan Williams9269e0e2011-05-12 07:42:17 -0700812 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), state_machine);
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 */
815 sci_base_state_machine_change_state(&sci_dev->state_machine,
816 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
817}
818
819/**
Dan Williams88f3b622011-04-22 19:18:03 -0700820 * scic_remote_device_destruct() - free remote node context and destruct
821 * @remote_device: This parameter specifies the remote device to be destructed.
822 *
823 * Remote device objects are a limited resource. As such, they must be
824 * protected. Thus calls to construct and destruct are mutually exclusive and
825 * non-reentrant. The return value shall indicate if the device was
826 * successfully destructed or if some failure occurred. enum sci_status This value
827 * is returned if the device is successfully destructed.
828 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
829 * device isn't valid (e.g. it's already been destoryed, the handle isn't
830 * valid, etc.).
831 */
832static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
833{
Dan Williamsb8d82f62011-05-01 14:38:26 -0700834 struct sci_base_state_machine *sm = &sci_dev->state_machine;
835 enum scic_sds_remote_device_states state = sm->current_state_id;
836 struct scic_sds_controller *scic;
837
838 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
839 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
840 __func__, state);
841 return SCI_FAILURE_INVALID_STATE;
842 }
843
844 scic = sci_dev->owning_port->owning_controller;
845 scic_sds_controller_free_remote_node_context(scic, sci_dev,
846 sci_dev->rnc.remote_node_index);
847 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
848 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
849
850 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -0700851}
852
Dan Williams6f231dd2011-07-02 22:56:22 -0700853/**
854 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -0800855 * @ihost: This parameter specifies the isci host object.
856 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -0700857 *
858 */
Dan Williamsd9c37392011-03-03 17:59:32 -0800859static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700860{
Dan Williamsd9c37392011-03-03 17:59:32 -0800861 dev_dbg(&ihost->pdev->dev,
862 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700863
864 /* There should not be any outstanding io's. All paths to
865 * here should go through isci_remote_device_nuke_requests.
866 * If we hit this condition, we will need a way to complete
867 * io requests in process */
Dan Williamsd9c37392011-03-03 17:59:32 -0800868 while (!list_empty(&idev->reqs_in_process)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700869
Dan Williamsd9c37392011-03-03 17:59:32 -0800870 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700871 "%s: ** request list not empty! **\n", __func__);
872 BUG();
873 }
874
Dan Williams57f20f42011-04-21 18:14:45 -0700875 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -0800876 idev->domain_dev->lldd_dev = NULL;
877 idev->domain_dev = NULL;
878 idev->isci_port = NULL;
879 list_del_init(&idev->node);
Dan Williams6ad31fe2011-03-04 12:10:29 -0800880
Dan Williamsd9c37392011-03-03 17:59:32 -0800881 clear_bit(IDEV_START_PENDING, &idev->flags);
882 clear_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williamsd06b4872011-05-02 13:59:25 -0700883 clear_bit(IDEV_EH, &idev->flags);
Dan Williamsd9c37392011-03-03 17:59:32 -0800884 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700885}
886
Dan Williams88f3b622011-04-22 19:18:03 -0700887/**
888 * isci_remote_device_stop_complete() - This function is called by the scic
889 * when the remote device stop has completed. We mark the isci device as not
890 * ready and remove the isci remote device.
891 * @ihost: This parameter specifies the isci host object.
892 * @idev: This parameter specifies the remote device.
893 * @status: This parameter specifies status of the completion.
894 *
895 */
896static void isci_remote_device_stop_complete(struct isci_host *ihost,
897 struct isci_remote_device *idev)
898{
899 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
900
901 isci_remote_device_change_state(idev, isci_stopped);
902
903 /* after stop, we can tear down resources. */
904 isci_remote_device_deconstruct(ihost, idev);
905}
906
Dan Williams9269e0e2011-05-12 07:42:17 -0700907static void scic_sds_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700908{
Dan Williams9269e0e2011-05-12 07:42:17 -0700909 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), state_machine);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000910 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
911 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700912 u32 prev_state;
913
Dan Williams88f3b622011-04-22 19:18:03 -0700914 /* If we are entering from the stopping state let the SCI User know that
915 * the stop operation has completed.
916 */
917 prev_state = sci_dev->state_machine.previous_state_id;
918 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000919 isci_remote_device_stop_complete(scic_to_ihost(scic), idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700920
921 scic_sds_controller_remote_device_stopped(scic, sci_dev);
922}
923
Dan Williams9269e0e2011-05-12 07:42:17 -0700924static void scic_sds_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700925{
Dan Williams9269e0e2011-05-12 07:42:17 -0700926 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), state_machine);
Dan Williams88f3b622011-04-22 19:18:03 -0700927 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000928 struct isci_host *ihost = scic_to_ihost(scic);
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000929 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700930
Dan Williams88f3b622011-04-22 19:18:03 -0700931 isci_remote_device_not_ready(ihost, idev,
932 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
933}
934
Dan Williams9269e0e2011-05-12 07:42:17 -0700935static void scic_sds_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700936{
Dan Williams9269e0e2011-05-12 07:42:17 -0700937 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), state_machine);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700938 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000939 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
940 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700941
Dan Williams88f3b622011-04-22 19:18:03 -0700942 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
943
Dan Williamsab2e8f72011-04-27 16:32:45 -0700944 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
945 sci_base_state_machine_change_state(&sci_dev->state_machine,
946 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
947 } else if (dev_is_expander(dev)) {
948 sci_base_state_machine_change_state(&sci_dev->state_machine,
949 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
950 } else
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000951 isci_remote_device_ready(scic_to_ihost(scic), idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700952}
953
Dan Williams9269e0e2011-05-12 07:42:17 -0700954static void scic_sds_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700955{
Dan Williams9269e0e2011-05-12 07:42:17 -0700956 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), state_machine);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700957 struct domain_device *dev = sci_dev_to_domain(sci_dev);
958
959 if (dev->dev_type == SAS_END_DEV) {
960 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000961 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700962
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000963 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
Dan Williams88f3b622011-04-22 19:18:03 -0700964 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
965 }
966}
967
Dan Williams9269e0e2011-05-12 07:42:17 -0700968static void scic_sds_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700969{
Dan Williams9269e0e2011-05-12 07:42:17 -0700970 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), state_machine);
Dan Williams88f3b622011-04-22 19:18:03 -0700971
Dan Williams88f3b622011-04-22 19:18:03 -0700972 scic_sds_remote_node_context_suspend(
973 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
974}
975
Dan Williams9269e0e2011-05-12 07:42:17 -0700976static void scic_sds_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700977{
Dan Williams9269e0e2011-05-12 07:42:17 -0700978 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), state_machine);
Dan Williams88f3b622011-04-22 19:18:03 -0700979
980 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
981}
982
Dan Williams9269e0e2011-05-12 07:42:17 -0700983static void scic_sds_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700984{
Dan Williams9269e0e2011-05-12 07:42:17 -0700985 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), state_machine);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700986
Dan Williamsab2e8f72011-04-27 16:32:45 -0700987 sci_dev->working_request = NULL;
988 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
989 /*
990 * Since the RNC is ready, it's alright to finish completion
991 * processing (e.g. signal the remote device is ready). */
992 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
993 } else {
994 scic_sds_remote_node_context_resume(&sci_dev->rnc,
995 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
996 sci_dev);
997 }
998}
999
Dan Williams9269e0e2011-05-12 07:42:17 -07001000static void scic_sds_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001001{
Dan Williams9269e0e2011-05-12 07:42:17 -07001002 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), state_machine);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001003 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1004
1005 BUG_ON(sci_dev->working_request == NULL);
1006
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001007 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
Dan Williamsab2e8f72011-04-27 16:32:45 -07001008 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1009}
1010
Dan Williams9269e0e2011-05-12 07:42:17 -07001011static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001012{
Dan Williams9269e0e2011-05-12 07:42:17 -07001013 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), state_machine);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001014 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1015 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1016
Dan Williamsab2e8f72011-04-27 16:32:45 -07001017 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001018 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001019 sci_dev->not_ready_reason);
1020}
1021
Dan Williams9269e0e2011-05-12 07:42:17 -07001022static void scic_sds_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001023{
Dan Williams9269e0e2011-05-12 07:42:17 -07001024 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), state_machine);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001025 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1026
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001027 isci_remote_device_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev));
Dan Williamsab2e8f72011-04-27 16:32:45 -07001028}
1029
Dan Williams9269e0e2011-05-12 07:42:17 -07001030static void scic_sds_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001031{
Dan Williams9269e0e2011-05-12 07:42:17 -07001032 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), state_machine);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001033 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1034
1035 BUG_ON(sci_dev->working_request == NULL);
1036
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001037 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
Dan Williamsab2e8f72011-04-27 16:32:45 -07001038 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1039}
1040
Dan Williams9269e0e2011-05-12 07:42:17 -07001041static void scic_sds_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001042{
Dan Williams9269e0e2011-05-12 07:42:17 -07001043 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), state_machine);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001044
1045 sci_dev->working_request = NULL;
1046}
Dan Williams88f3b622011-04-22 19:18:03 -07001047
1048static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1049 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1050 .enter_state = scic_sds_remote_device_initial_state_enter,
1051 },
1052 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1053 .enter_state = scic_sds_remote_device_stopped_state_enter,
1054 },
1055 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1056 .enter_state = scic_sds_remote_device_starting_state_enter,
1057 },
1058 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1059 .enter_state = scic_sds_remote_device_ready_state_enter,
1060 .exit_state = scic_sds_remote_device_ready_state_exit
1061 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001062 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1063 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1064 },
1065 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1066 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1067 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001068 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = { },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001069 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1070 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1071 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001072 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = { },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001073 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1074 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1075 },
1076 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1077 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1078 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1079 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001080 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = { },
1081 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001082 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1083 .enter_state = scic_sds_remote_device_resetting_state_enter,
1084 .exit_state = scic_sds_remote_device_resetting_state_exit
1085 },
Dan Williams971cc2f2011-05-01 16:58:46 -07001086 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001087};
1088
1089/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001090 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001091 * @sci_port: SAS/SATA port through which this device is accessed.
1092 * @sci_dev: remote device to construct
1093 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001094 * This routine just performs benign initialization and does not
1095 * allocate the remote_node_context which is left to
1096 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1097 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001098 */
1099static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1100 struct scic_sds_remote_device *sci_dev)
1101{
1102 sci_dev->owning_port = sci_port;
1103 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001104
Dan Williams9269e0e2011-05-12 07:42:17 -07001105 sci_base_state_machine_construct(&sci_dev->state_machine,
1106 scic_sds_remote_device_state_table,
1107 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
Dan Williams88f3b622011-04-22 19:18:03 -07001108
Dan Williams9269e0e2011-05-12 07:42:17 -07001109 sci_base_state_machine_start(&sci_dev->state_machine);
Dan Williams88f3b622011-04-22 19:18:03 -07001110
1111 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1112 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001113}
1114
1115/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001116 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001117 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001118 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1119 * the device is known to the SCI Core since it is contained in the
1120 * scic_phy object. Remote node context(s) is/are a global resource
1121 * allocated by this routine, freed by scic_remote_device_destruct().
1122 *
1123 * Returns:
1124 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1125 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1126 * sata-only controller instance.
1127 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001128 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001129static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1130 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001131{
1132 enum sci_status status;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001133 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001134
Dan Williamsb87ee302011-04-25 11:48:29 -07001135 scic_remote_device_construct(sci_port, sci_dev);
1136
Dan Williams88f3b622011-04-22 19:18:03 -07001137 /*
1138 * This information is request to determine how many remote node context
1139 * entries will be needed to store the remote node.
1140 */
Dan Williams88f3b622011-04-22 19:18:03 -07001141 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001142 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1143 sci_dev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001144 &sci_dev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001145
Dan Williamsa1a113b2011-04-21 18:44:45 -07001146 if (status != SCI_SUCCESS)
1147 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001148
Dan Williamsab2e8f72011-04-27 16:32:45 -07001149 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1150 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1151 /* pass */;
1152 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001153 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001154
Dan Williamsa1a113b2011-04-21 18:44:45 -07001155 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001156
Dan Williamsa1a113b2011-04-21 18:44:45 -07001157 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1158 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001159
Dan Williamsa1a113b2011-04-21 18:44:45 -07001160 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001161}
1162
Dan Williams88f3b622011-04-22 19:18:03 -07001163/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001164 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001165 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001166 * Remote node context(s) is/are a global resource allocated by this
1167 * routine, freed by scic_remote_device_destruct().
1168 *
1169 * Returns:
1170 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1171 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1172 * sata-only controller instance.
1173 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001174 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001175static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001176 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001177{
Dan Williamsa1a113b2011-04-21 18:44:45 -07001178 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001179 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001180
Dan Williamsb87ee302011-04-25 11:48:29 -07001181 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001182
Dan Williamsab2e8f72011-04-27 16:32:45 -07001183 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1184 sci_dev,
1185 &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001186 if (status != SCI_SUCCESS)
1187 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001188
Dan Williamsab2e8f72011-04-27 16:32:45 -07001189 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1190 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1191 /* pass */;
1192 else
1193 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001194
Dan Williamsa1a113b2011-04-21 18:44:45 -07001195 /*
1196 * For SAS-2 the physical link rate is actually a logical link
1197 * rate that incorporates multiplexing. The SCU doesn't
1198 * incorporate multiplexing and for the purposes of the
1199 * connection the logical link rate is that same as the
1200 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1201 * one another, so this code works for both situations. */
1202 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001203 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001204
Dan Williamsa1a113b2011-04-21 18:44:45 -07001205 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1206 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001207
Dan Williamsab2e8f72011-04-27 16:32:45 -07001208 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001209}
1210
1211/**
1212 * scic_remote_device_start() - This method will start the supplied remote
1213 * device. This method enables normal IO requests to flow through to the
1214 * remote device.
1215 * @remote_device: This parameter specifies the device to be started.
1216 * @timeout: This parameter specifies the number of milliseconds in which the
1217 * start operation should complete.
1218 *
1219 * An indication of whether the device was successfully started. SCI_SUCCESS
1220 * This value is returned if the device was successfully started.
1221 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1222 * the device when there have been no phys added to it.
1223 */
1224static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
Dan Williamseb229672011-05-01 14:05:57 -07001225 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001226{
Dan Williamseb229672011-05-01 14:05:57 -07001227 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1228 enum scic_sds_remote_device_states state = sm->current_state_id;
1229 enum sci_status status;
1230
1231 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1232 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1233 __func__, state);
1234 return SCI_FAILURE_INVALID_STATE;
1235 }
1236
1237 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1238 remote_device_resume_done,
1239 sci_dev);
1240 if (status != SCI_SUCCESS)
1241 return status;
1242
1243 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1244
1245 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001246}
Dan Williams6f231dd2011-07-02 22:56:22 -07001247
Dan Williams00d680e2011-04-25 14:29:29 -07001248static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1249 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001250{
Dan Williamse5313812011-05-07 10:11:43 -07001251 struct scic_sds_port *sci_port = &iport->sci;
Dan Williams00d680e2011-04-25 14:29:29 -07001252 struct isci_host *ihost = iport->isci_host;
1253 struct domain_device *dev = idev->domain_dev;
1254 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001255
Dan Williams00d680e2011-04-25 14:29:29 -07001256 if (dev->parent && dev_is_expander(dev->parent))
1257 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1258 else
1259 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001260
1261 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001262 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1263 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001264
1265 return status;
1266 }
1267
Dan Williams6f231dd2011-07-02 22:56:22 -07001268 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001269 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001270
Dan Williams00d680e2011-04-25 14:29:29 -07001271 if (status != SCI_SUCCESS)
1272 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1273 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001274
1275 return status;
1276}
1277
Dan Williams4393aa42011-03-31 13:10:44 -07001278void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001279{
1280 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001281
Dan Williams4393aa42011-03-31 13:10:44 -07001282 dev_dbg(&ihost->pdev->dev,
1283 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001284
1285 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -07001286 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -07001287
Dan Williams4393aa42011-03-31 13:10:44 -07001288 dev_dbg(&ihost->pdev->dev,
1289 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001290}
1291
Dan Williams6f231dd2011-07-02 22:56:22 -07001292/**
1293 * This function builds the isci_remote_device when a libsas dev_found message
1294 * is received.
1295 * @isci_host: This parameter specifies the isci host object.
1296 * @port: This parameter specifies the isci_port conected to this device.
1297 *
1298 * pointer to new isci_remote_device.
1299 */
1300static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001301isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001302{
Dan Williamsd9c37392011-03-03 17:59:32 -08001303 struct isci_remote_device *idev;
1304 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001305
Dan Williamsd9c37392011-03-03 17:59:32 -08001306 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001307 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001308 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1309 break;
1310 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001311
Dan Williamsd9c37392011-03-03 17:59:32 -08001312 if (i >= SCI_MAX_REMOTE_DEVICES) {
1313 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001314 return NULL;
1315 }
1316
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001317 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1318 return NULL;
1319
1320 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1321 return NULL;
1322
Dan Williamsd9c37392011-03-03 17:59:32 -08001323 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001324
Dan Williamsd9c37392011-03-03 17:59:32 -08001325 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001326}
Dan Williams6f231dd2011-07-02 22:56:22 -07001327
1328/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001329 * isci_remote_device_stop() - This function is called internally to stop the
1330 * remote device.
1331 * @isci_host: This parameter specifies the isci host object.
1332 * @isci_device: This parameter specifies the remote device.
1333 *
1334 * The status of the scic request to stop.
1335 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001336enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001337{
1338 enum sci_status status;
1339 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001340
Dan Williams6ad31fe2011-03-04 12:10:29 -08001341 dev_dbg(&ihost->pdev->dev,
1342 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001343
Dan Williams6ad31fe2011-03-04 12:10:29 -08001344 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001345
1346 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001347 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001348
Dan Williams6ad31fe2011-03-04 12:10:29 -08001349 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001350
Dan Williams6ad31fe2011-03-04 12:10:29 -08001351 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07001352 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001353 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001354
1355 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -08001356 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -08001357 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08001358 clear_bit(IDEV_ALLOCATED, &idev->flags);
1359 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001360
Dan Williams6ad31fe2011-03-04 12:10:29 -08001361 dev_dbg(&ihost->pdev->dev,
1362 "%s: idev = %p - after completion wait\n",
1363 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001364
Dan Williams6f231dd2011-07-02 22:56:22 -07001365 return status;
1366}
1367
1368/**
1369 * isci_remote_device_gone() - This function is called by libsas when a domain
1370 * device is removed.
1371 * @domain_device: This parameter specifies the libsas domain device.
1372 *
1373 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001374void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001375{
Dan Williams4393aa42011-03-31 13:10:44 -07001376 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001377 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001378
Dan Williams6ad31fe2011-03-04 12:10:29 -08001379 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001380 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001381 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001382
Dan Williams6ad31fe2011-03-04 12:10:29 -08001383 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001384}
1385
1386
1387/**
1388 * isci_remote_device_found() - This function is called by libsas when a remote
1389 * device is discovered. A remote device object is created and started. the
1390 * function then sleeps until the sci core device started message is
1391 * received.
1392 * @domain_device: This parameter specifies the libsas domain device.
1393 *
1394 * status, zero indicates success.
1395 */
1396int isci_remote_device_found(struct domain_device *domain_dev)
1397{
Dan Williams4393aa42011-03-31 13:10:44 -07001398 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001399 struct isci_port *isci_port;
1400 struct isci_phy *isci_phy;
1401 struct asd_sas_port *sas_port;
1402 struct asd_sas_phy *sas_phy;
1403 struct isci_remote_device *isci_device;
1404 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001405
Dan Williams6f231dd2011-07-02 22:56:22 -07001406 dev_dbg(&isci_host->pdev->dev,
1407 "%s: domain_device = %p\n", __func__, domain_dev);
1408
Dan Williams0cf89d12011-02-18 09:25:07 -08001409 wait_for_start(isci_host);
1410
Dan Williams6f231dd2011-07-02 22:56:22 -07001411 sas_port = domain_dev->port;
1412 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1413 port_phy_el);
1414 isci_phy = to_isci_phy(sas_phy);
1415 isci_port = isci_phy->isci_port;
1416
1417 /* we are being called for a device on this port,
1418 * so it has to come up eventually
1419 */
1420 wait_for_completion(&isci_port->start_complete);
1421
1422 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1423 (isci_stopped == isci_port_get_state(isci_port)))
1424 return -ENODEV;
1425
1426 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001427 if (!isci_device)
1428 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001429
1430 INIT_LIST_HEAD(&isci_device->node);
1431 domain_dev->lldd_dev = isci_device;
1432 isci_device->domain_dev = domain_dev;
1433 isci_device->isci_port = isci_port;
1434 isci_remote_device_change_state(isci_device, isci_starting);
1435
1436
Dan Williams1a380452011-03-03 18:01:43 -08001437 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001438 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1439
Dan Williams6ad31fe2011-03-04 12:10:29 -08001440 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001441 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -08001442 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001443
Dan Williams6f231dd2011-07-02 22:56:22 -07001444 dev_dbg(&isci_host->pdev->dev,
1445 "%s: isci_device = %p\n",
1446 __func__, isci_device);
1447
1448 if (status != SCI_SUCCESS) {
1449
Dan Williams1a380452011-03-03 18:01:43 -08001450 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001451 isci_remote_device_deconstruct(
1452 isci_host,
1453 isci_device
1454 );
Dan Williams1a380452011-03-03 18:01:43 -08001455 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001456 return -ENODEV;
1457 }
1458
Dan Williams6ad31fe2011-03-04 12:10:29 -08001459 /* wait for the device ready callback. */
1460 wait_for_device_start(isci_host, isci_device);
1461
Dan Williams6f231dd2011-07-02 22:56:22 -07001462 return 0;
1463}
1464/**
1465 * isci_device_is_reset_pending() - This function will check if there is any
1466 * pending reset condition on the device.
1467 * @request: This parameter is the isci_device object.
1468 *
1469 * true if there is a reset pending for the device.
1470 */
1471bool isci_device_is_reset_pending(
1472 struct isci_host *isci_host,
1473 struct isci_remote_device *isci_device)
1474{
1475 struct isci_request *isci_request;
1476 struct isci_request *tmp_req;
1477 bool reset_is_pending = false;
1478 unsigned long flags;
1479
1480 dev_dbg(&isci_host->pdev->dev,
1481 "%s: isci_device = %p\n", __func__, isci_device);
1482
1483 spin_lock_irqsave(&isci_host->scic_lock, flags);
1484
1485 /* Check for reset on all pending requests. */
1486 list_for_each_entry_safe(isci_request, tmp_req,
1487 &isci_device->reqs_in_process, dev_node) {
1488 dev_dbg(&isci_host->pdev->dev,
1489 "%s: isci_device = %p request = %p\n",
1490 __func__, isci_device, isci_request);
1491
1492 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001493 struct sas_task *task = isci_request_access_task(
1494 isci_request);
1495
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001496 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001497 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1498 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001499 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001500 }
1501 }
1502
1503 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1504
1505 dev_dbg(&isci_host->pdev->dev,
1506 "%s: isci_device = %p reset_is_pending = %d\n",
1507 __func__, isci_device, reset_is_pending);
1508
1509 return reset_is_pending;
1510}
1511
1512/**
1513 * isci_device_clear_reset_pending() - This function will clear if any pending
1514 * reset condition flags on the device.
1515 * @request: This parameter is the isci_device object.
1516 *
1517 * true if there is a reset pending for the device.
1518 */
Dan Williams4393aa42011-03-31 13:10:44 -07001519void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001520{
1521 struct isci_request *isci_request;
1522 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07001523 unsigned long flags = 0;
1524
Dan Williams4393aa42011-03-31 13:10:44 -07001525 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1526 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07001527
Dan Williams4393aa42011-03-31 13:10:44 -07001528 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001529
1530 /* Clear reset pending on all pending requests. */
1531 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07001532 &idev->reqs_in_process, dev_node) {
1533 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1534 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001535
1536 if (isci_request->ttype == io_task) {
1537
1538 unsigned long flags2;
1539 struct sas_task *task = isci_request_access_task(
1540 isci_request);
1541
1542 spin_lock_irqsave(&task->task_state_lock, flags2);
1543 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1544 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1545 }
1546 }
Dan Williams4393aa42011-03-31 13:10:44 -07001547 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001548}