blob: 0295349b40e5f7e7382029942e8d857cacc6af7c [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 */
Dan Williams88f3b622011-04-22 19:18:03 -070055#include "intel_sas.h"
Dan Williamsab2e8f72011-04-27 16:32:45 -070056#include "intel_ata.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070058#include "port.h"
59#include "remote_device.h"
60#include "request.h"
Dan Williams88f3b622011-04-22 19:18:03 -070061#include "scic_controller.h"
62#include "scic_io_request.h"
63#include "scic_phy.h"
64#include "scic_port.h"
65#include "scic_sds_controller.h"
66#include "scic_sds_phy.h"
67#include "scic_sds_port.h"
68#include "remote_node_context.h"
69#include "scic_sds_request.h"
70#include "sci_environment.h"
71#include "sci_util.h"
72#include "scu_event_codes.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070073#include "task.h"
74
Dan Williamsab2e8f72011-04-27 16:32:45 -070075/**
76 * isci_remote_device_change_state() - This function gets the status of the
77 * remote_device object.
78 * @isci_device: This parameter points to the isci_remote_device object
79 *
80 * status of the object as a isci_status enum.
81 */
82void isci_remote_device_change_state(
83 struct isci_remote_device *isci_device,
84 enum isci_status status)
85{
86 unsigned long flags;
87
88 spin_lock_irqsave(&isci_device->state_lock, flags);
89 isci_device->status = status;
90 spin_unlock_irqrestore(&isci_device->state_lock, flags);
91}
92
93/**
94 * isci_remote_device_not_ready() - This function is called by the scic when
95 * the remote device is not ready. We mark the isci device as ready (not
96 * "ready_for_io") and signal the waiting proccess.
97 * @isci_host: This parameter specifies the isci host object.
98 * @isci_device: This parameter specifies the remote device
99 *
100 */
101static void isci_remote_device_not_ready(struct isci_host *ihost,
102 struct isci_remote_device *idev, u32 reason)
103{
104 dev_dbg(&ihost->pdev->dev,
105 "%s: isci_device = %p\n", __func__, idev);
106
107 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
108 isci_remote_device_change_state(idev, isci_stopping);
109 else
110 /* device ready is actually a "not ready for io" state. */
111 isci_remote_device_change_state(idev, isci_ready);
112}
113
114/**
115 * isci_remote_device_ready() - This function is called by the scic when the
116 * remote device is ready. We mark the isci device as ready and signal the
117 * waiting proccess.
118 * @ihost: our valid isci_host
119 * @idev: remote device
120 *
121 */
122static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
123{
124 dev_dbg(&ihost->pdev->dev,
125 "%s: idev = %p\n", __func__, idev);
126
127 isci_remote_device_change_state(idev, isci_ready_for_io);
128 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
129 wake_up(&ihost->eventq);
130}
131
Dan Williamsec575662011-05-01 14:19:25 -0700132/* called once the remote node context is ready to be freed.
133 * The remote device can now report that its stop operation is complete. none
134 */
135static void rnc_destruct_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700136{
Dan Williamsec575662011-05-01 14:19:25 -0700137 struct scic_sds_remote_device *sci_dev = _dev;
138
139 BUG_ON(sci_dev->started_request_count != 0);
140 sci_base_state_machine_change_state(&sci_dev->state_machine,
141 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
142}
143
144static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
145{
146 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
147 u32 i, request_count = sci_dev->started_request_count;
148 enum sci_status status = SCI_SUCCESS;
149
150 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
151 struct scic_sds_request *sci_req;
152 enum sci_status s;
153
154 sci_req = scic->io_request_table[i];
155 if (!sci_req || sci_req->target_device != sci_dev)
156 continue;
157 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
158 if (s != SCI_SUCCESS)
159 status = s;
160 }
161
162 return status;
163}
164
165enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
166 u32 timeout)
167{
168 struct sci_base_state_machine *sm = &sci_dev->state_machine;
169 enum scic_sds_remote_device_states state = sm->current_state_id;
170
171 switch (state) {
172 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
173 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
174 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
175 default:
176 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
177 __func__, state);
178 return SCI_FAILURE_INVALID_STATE;
179 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
180 return SCI_SUCCESS;
181 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
182 /* device not started so there had better be no requests */
183 BUG_ON(sci_dev->started_request_count != 0);
184 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
185 rnc_destruct_done, sci_dev);
186 /* Transition to the stopping state and wait for the
187 * remote node to complete being posted and invalidated.
188 */
189 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
190 return SCI_SUCCESS;
191 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
192 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
193 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
194 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
195 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
196 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
197 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
198 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
199 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
200 if (sci_dev->started_request_count == 0) {
201 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
202 rnc_destruct_done, sci_dev);
203 return SCI_SUCCESS;
204 } else
205 return scic_sds_remote_device_terminate_requests(sci_dev);
206 break;
207 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
208 /* All requests should have been terminated, but if there is an
209 * attempt to stop a device already in the stopping state, then
210 * try again to terminate.
211 */
212 return scic_sds_remote_device_terminate_requests(sci_dev);
213 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
214 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
215 return SCI_SUCCESS;
216 }
Dan Williams88f3b622011-04-22 19:18:03 -0700217}
Dan Williams6f231dd2011-07-02 22:56:22 -0700218
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700219enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700220{
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700221 struct sci_base_state_machine *sm = &sci_dev->state_machine;
222 enum scic_sds_remote_device_states state = sm->current_state_id;
223
224 switch (state) {
225 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
226 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
227 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
228 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
229 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
230 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
231 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
232 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
233 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
234 default:
235 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
236 __func__, state);
237 return SCI_FAILURE_INVALID_STATE;
238 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
239 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
240 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
241 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
242 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
243 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
244 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
245 return SCI_SUCCESS;
246 }
Dan Williams88f3b622011-04-22 19:18:03 -0700247}
248
Dan Williams81515182011-05-01 14:53:00 -0700249enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700250{
Dan Williams81515182011-05-01 14:53:00 -0700251 struct sci_base_state_machine *sm = &sci_dev->state_machine;
252 enum scic_sds_remote_device_states state = sm->current_state_id;
Dan Williams88f3b622011-04-22 19:18:03 -0700253
Dan Williams81515182011-05-01 14:53:00 -0700254 if (state != SCI_BASE_REMOTE_DEVICE_STATE_RESETTING) {
255 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
256 __func__, state);
257 return SCI_FAILURE_INVALID_STATE;
258 }
259
260 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_READY);
261 return SCI_SUCCESS;
262}
Dan Williams88f3b622011-04-22 19:18:03 -0700263
Dan Williams323f0ec2011-05-01 16:15:47 -0700264enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
265 u32 suspend_type)
Dan Williams88f3b622011-04-22 19:18:03 -0700266{
Dan Williams323f0ec2011-05-01 16:15:47 -0700267 struct sci_base_state_machine *sm = &sci_dev->state_machine;
268 enum scic_sds_remote_device_states state = sm->current_state_id;
269
270 if (state != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD) {
271 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
272 __func__, state);
273 return SCI_FAILURE_INVALID_STATE;
274 }
275
276 return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
277 suspend_type, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700278}
279
Dan Williams01bec772011-05-01 16:51:11 -0700280enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
281 u32 frame_index)
Dan Williams88f3b622011-04-22 19:18:03 -0700282{
Dan Williams01bec772011-05-01 16:51:11 -0700283 struct sci_base_state_machine *sm = &sci_dev->state_machine;
284 enum scic_sds_remote_device_states state = sm->current_state_id;
285 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
286 enum sci_status status;
287
288 switch (state) {
289 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
290 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
291 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
292 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
293 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
294 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
295 default:
296 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
297 __func__, state);
298 /* Return the frame back to the controller */
299 scic_sds_controller_release_frame(scic, frame_index);
300 return SCI_FAILURE_INVALID_STATE;
301 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
302 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
303 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
304 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
305 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
306 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING: {
307 struct scic_sds_request *sci_req;
308 struct sci_ssp_frame_header *hdr;
309
310 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
311 frame_index,
312 (void **)&hdr);
313 if (status != SCI_SUCCESS)
314 return status;
315
316 sci_req = scic_sds_controller_get_io_request_from_tag(scic, hdr->tag);
317 if (sci_req && sci_req->target_device == sci_dev) {
318 /* The IO request is now in charge of releasing the frame */
319 status = sci_req->state_handlers->frame_handler(sci_req,
320 frame_index);
321 } else {
322 /* We could not map this tag to a valid IO
323 * request Just toss the frame and continue
324 */
325 scic_sds_controller_release_frame(scic, frame_index);
326 }
327 break;
328 }
329 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
330 struct sata_fis_header *hdr;
331
332 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
333 frame_index,
334 (void **)&hdr);
335 if (status != SCI_SUCCESS)
336 return status;
337
338 if (hdr->fis_type == SATA_FIS_TYPE_SETDEVBITS &&
339 (hdr->status & ATA_STATUS_REG_ERROR_BIT)) {
340 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
341
342 /* TODO Check sactive and complete associated IO if any. */
343 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
344 } else if (hdr->fis_type == SATA_FIS_TYPE_REGD2H &&
345 (hdr->status & ATA_STATUS_REG_ERROR_BIT)) {
346 /*
347 * Some devices return D2H FIS when an NCQ error is detected.
348 * Treat this like an SDB error FIS ready reason.
349 */
350 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
351 sci_base_state_machine_change_state(&sci_dev->state_machine,
352 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
353 } else
354 status = SCI_FAILURE;
355
356 scic_sds_controller_release_frame(scic, frame_index);
357 break;
358 }
359 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
360 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
361 /* The device does not process any UF received from the hardware while
362 * in this state. All unsolicited frames are forwarded to the io request
363 * object.
364 */
365 status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
366 break;
367 }
368
369 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700370}
371
Dan Williamse6225712011-05-01 16:26:09 -0700372static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700373{
Dan Williamse6225712011-05-01 16:26:09 -0700374
375 struct sci_base_state_machine *sm = &sci_dev->state_machine;
376 enum scic_sds_remote_device_states state = sm->current_state_id;
377
378 switch (state) {
379 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
380 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
381 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
382 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
383 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
384 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
385 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
386 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
387 return true;
388 default:
389 return false;
390 }
391}
392
393enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
394 u32 event_code)
395{
396 struct sci_base_state_machine *sm = &sci_dev->state_machine;
397 enum scic_sds_remote_device_states state = sm->current_state_id;
398 enum sci_status status;
399
400 switch (scu_get_event_type(event_code)) {
401 case SCU_EVENT_TYPE_RNC_OPS_MISC:
402 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
403 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
404 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
405 break;
406 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
407 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
408 status = SCI_SUCCESS;
409
410 /* Suspend the associated RNC */
411 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
412 SCI_SOFTWARE_SUSPENSION,
413 NULL, NULL);
414
415 dev_dbg(scirdev_to_dev(sci_dev),
416 "%s: device: %p event code: %x: %s\n",
417 __func__, sci_dev, event_code,
418 is_remote_device_ready(sci_dev)
419 ? "I_T_Nexus_Timeout event"
420 : "I_T_Nexus_Timeout event in wrong state");
421
422 break;
423 }
424 /* Else, fall through and treat as unhandled... */
425 default:
426 dev_dbg(scirdev_to_dev(sci_dev),
427 "%s: device: %p event code: %x: %s\n",
428 __func__, sci_dev, event_code,
429 is_remote_device_ready(sci_dev)
430 ? "unexpected event"
431 : "unexpected event in wrong state");
432 status = SCI_FAILURE_INVALID_STATE;
433 break;
434 }
435
436 if (status != SCI_SUCCESS)
437 return status;
438
439 if (state == SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE) {
440
441 /* We pick up suspension events to handle specifically to this
442 * state. We resume the RNC right away.
443 */
444 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
445 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
446 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
447 }
448
449 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700450}
451
Dan Williams18606552011-05-01 14:57:11 -0700452static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
453 struct scic_sds_request *sci_req,
454 enum sci_status status)
Dan Williams88f3b622011-04-22 19:18:03 -0700455{
Dan Williams18606552011-05-01 14:57:11 -0700456 struct scic_sds_port *sci_port = sci_dev->owning_port;
457
458 /* cleanup requests that failed after starting on the port */
459 if (status != SCI_SUCCESS)
460 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
461 else
462 scic_sds_remote_device_increment_request_count(sci_dev);
463}
464
465enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
466 struct scic_sds_remote_device *sci_dev,
467 struct scic_sds_request *sci_req)
468{
469 struct sci_base_state_machine *sm = &sci_dev->state_machine;
470 enum scic_sds_remote_device_states state = sm->current_state_id;
471 struct scic_sds_port *sci_port = sci_dev->owning_port;
472 enum sci_status status;
473
474 switch (state) {
475 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
476 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
477 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
478 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
479 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
480 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
481 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
482 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
483 default:
484 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
485 __func__, state);
486 return SCI_FAILURE_INVALID_STATE;
487 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
488 /* attempt to start an io request for this device object. The remote
489 * device object will issue the start request for the io and if
490 * successful it will start the request for the port object then
491 * increment its own request count.
492 */
493 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
494 if (status != SCI_SUCCESS)
495 return status;
496
497 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
498 if (status != SCI_SUCCESS)
499 break;
500
501 status = scic_sds_request_start(sci_req);
502 break;
503 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
504 /* handle the start io operation for a sata device that is in
505 * the command idle state. - Evalute the type of IO request to
506 * be started - If its an NCQ request change to NCQ substate -
507 * If its any other command change to the CMD substate
508 *
509 * If this is a softreset we may want to have a different
510 * substate.
511 */
512 enum scic_sds_remote_device_states new_state;
513
514 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
515 if (status != SCI_SUCCESS)
516 return status;
517
518 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
519 if (status != SCI_SUCCESS)
520 break;
521
522 status = sci_req->state_handlers->start_handler(sci_req);
523 if (status != SCI_SUCCESS)
524 break;
525
526 if (isci_sata_get_sat_protocol(sci_req->ireq) == SAT_PROTOCOL_FPDMA)
527 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
528 else {
529 sci_dev->working_request = sci_req;
530 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
531 }
532 sci_base_state_machine_change_state(sm, new_state);
533 break;
534 }
535 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
536 if (isci_sata_get_sat_protocol(sci_req->ireq) == SAT_PROTOCOL_FPDMA) {
537 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
538 if (status != SCI_SUCCESS)
539 return status;
540
541 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
542 if (status != SCI_SUCCESS)
543 break;
544
545 status = sci_req->state_handlers->start_handler(sci_req);
546 } else
547 return SCI_FAILURE_INVALID_STATE;
548 break;
549 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
550 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
551 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
552 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
553 if (status != SCI_SUCCESS)
554 return status;
555
556 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
557 if (status != SCI_SUCCESS)
558 break;
559
560 status = scic_sds_request_start(sci_req);
561 if (status != SCI_SUCCESS)
562 break;
563
564 sci_dev->working_request = sci_req;
565 sci_base_state_machine_change_state(&sci_dev->state_machine,
566 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
567 break;
568 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
569 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
570 /* device is already handling a command it can not accept new commands
571 * until this one is complete.
572 */
573 return SCI_FAILURE_INVALID_STATE;
574 }
575
576 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
577 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700578}
579
Dan Williams10a09e62011-05-01 15:33:43 -0700580static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
581 struct scic_sds_remote_device *sci_dev,
582 struct scic_sds_request *sci_req)
Dan Williams88f3b622011-04-22 19:18:03 -0700583{
Dan Williams10a09e62011-05-01 15:33:43 -0700584 enum sci_status status;
585
586 status = scic_sds_request_complete(sci_req);
587 if (status != SCI_SUCCESS)
588 return status;
589
590 status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
591 if (status != SCI_SUCCESS)
592 return status;
593
594 scic_sds_remote_device_decrement_request_count(sci_dev);
595 return status;
596}
597
598enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
599 struct scic_sds_remote_device *sci_dev,
600 struct scic_sds_request *sci_req)
601{
602 struct sci_base_state_machine *sm = &sci_dev->state_machine;
603 enum scic_sds_remote_device_states state = sm->current_state_id;
604 struct scic_sds_port *sci_port = sci_dev->owning_port;
605 enum sci_status status;
606
607 switch (state) {
608 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
609 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
610 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
611 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
612 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
613 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
614 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
615 default:
616 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
617 __func__, state);
618 return SCI_FAILURE_INVALID_STATE;
619 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
620 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
621 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
622 status = common_complete_io(sci_port, sci_dev, sci_req);
623 break;
624 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
625 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
626 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
627 status = common_complete_io(sci_port, sci_dev, sci_req);
628 if (status != SCI_SUCCESS)
629 break;
630
631 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
632 /* This request causes hardware error, device needs to be Lun Reset.
633 * So here we force the state machine to IDLE state so the rest IOs
634 * can reach RNC state handler, these IOs will be completed by RNC with
635 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
636 */
637 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
638 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
639 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
640 break;
641 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
642 status = common_complete_io(sci_port, sci_dev, sci_req);
643 if (status != SCI_SUCCESS)
644 break;
645 sci_base_state_machine_change_state(sm, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
646 break;
647 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
648 status = common_complete_io(sci_port, sci_dev, sci_req);
649 if (status != SCI_SUCCESS)
650 break;
651
652 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
653 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
654 rnc_destruct_done,
655 sci_dev);
656 break;
657 }
658
659 if (status != SCI_SUCCESS)
660 dev_err(scirdev_to_dev(sci_dev),
661 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
662 "could not complete\n", __func__, sci_port,
663 sci_dev, sci_req, status);
664
665 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700666}
667
Dan Williams84b9b022011-05-01 15:53:25 -0700668static void scic_sds_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700669{
Dan Williams84b9b022011-05-01 15:53:25 -0700670 struct scic_sds_remote_device *sci_dev = dev;
671
672 /* we need to check if this request is still valid to continue. */
673 if (sci_dev->working_request)
674 scic_controller_continue_io(sci_dev->working_request);
675}
676
677enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
678 struct scic_sds_remote_device *sci_dev,
679 struct scic_sds_request *sci_req)
680{
681 struct sci_base_state_machine *sm = &sci_dev->state_machine;
682 enum scic_sds_remote_device_states state = sm->current_state_id;
683 struct scic_sds_port *sci_port = sci_dev->owning_port;
684 enum sci_status status;
685
686 switch (state) {
687 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
688 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
689 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
690 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
691 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
692 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
693 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
694 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
695 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
696 default:
697 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
698 __func__, state);
699 return SCI_FAILURE_INVALID_STATE;
700 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
701 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
702 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
703 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
704 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
705 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
706 if (status != SCI_SUCCESS)
707 return status;
708
709 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
710 if (status != SCI_SUCCESS)
711 goto out;
712
713 status = sci_req->state_handlers->start_handler(sci_req);
714 if (status != SCI_SUCCESS)
715 goto out;
716
717 /* Note: If the remote device state is not IDLE this will
718 * replace the request that probably resulted in the task
719 * management request.
720 */
721 sci_dev->working_request = sci_req;
722 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
723
724 /* The remote node context must cleanup the TCi to NCQ mapping
725 * table. The only way to do this correctly is to either write
726 * to the TLCR register or to invalidate and repost the RNC. In
727 * either case the remote node context state machine will take
728 * the correct action when the remote node context is suspended
729 * and later resumed.
730 */
731 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
732 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
733 scic_sds_remote_node_context_resume(&sci_dev->rnc,
734 scic_sds_remote_device_continue_request,
735 sci_dev);
736
737 out:
738 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
739 /* We need to let the controller start request handler know that
740 * it can't post TC yet. We will provide a callback function to
741 * post TC when RNC gets resumed.
742 */
743 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
744 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
745 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
746 if (status != SCI_SUCCESS)
747 return status;
748
749 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
750 if (status != SCI_SUCCESS)
751 break;
752
753 status = scic_sds_request_start(sci_req);
754 break;
755 }
756 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
757
758 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700759}
760
761/**
762 *
Dan Williams88f3b622011-04-22 19:18:03 -0700763 * @sci_dev:
764 * @request:
765 *
766 * This method takes the request and bulids an appropriate SCU context for the
767 * request and then requests the controller to post the request. none
768 */
769void scic_sds_remote_device_post_request(
770 struct scic_sds_remote_device *sci_dev,
771 u32 request)
772{
773 u32 context;
774
775 context = scic_sds_remote_device_build_command_context(sci_dev, request);
776
777 scic_sds_controller_post_request(
778 scic_sds_remote_device_get_controller(sci_dev),
779 context
780 );
781}
782
Dan Williamsab2e8f72011-04-27 16:32:45 -0700783/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700784 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700785 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700786 */
Dan Williamseb229672011-05-01 14:05:57 -0700787static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700788{
Dan Williamsab2e8f72011-04-27 16:32:45 -0700789 struct scic_sds_remote_device *sci_dev = _dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700790
Dan Williamse6225712011-05-01 16:26:09 -0700791 if (is_remote_device_ready(sci_dev))
792 return;
Dan Williams88f3b622011-04-22 19:18:03 -0700793
Dan Williamse6225712011-05-01 16:26:09 -0700794 /* go 'ready' if we are not already in a ready state */
795 sci_base_state_machine_change_state(&sci_dev->state_machine,
796 SCI_BASE_REMOTE_DEVICE_STATE_READY);
Dan Williams88f3b622011-04-22 19:18:03 -0700797}
798
Dan Williamsab2e8f72011-04-27 16:32:45 -0700799static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
800{
801 struct scic_sds_remote_device *sci_dev = _dev;
802 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
803 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
804
805 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
806 * As a result, avoid sending the ready notification.
807 */
808 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
809 isci_remote_device_ready(scic->ihost, idev);
810}
811
Dan Williams88f3b622011-04-22 19:18:03 -0700812static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
813 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700814 },
815 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700816 },
817 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700818 },
819 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700820 },
Dan Williamsab2e8f72011-04-27 16:32:45 -0700821 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700822 },
823 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700824 },
825 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700826 },
827 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700828 },
829 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700830 },
831 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700832 },
833 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700834 },
Dan Williams88f3b622011-04-22 19:18:03 -0700835 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700836 },
837 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700838 },
839 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700840 },
841 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
Dan Williams88f3b622011-04-22 19:18:03 -0700842 }
843};
844
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000845static void scic_sds_remote_device_initial_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700846{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000847 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700848
Dan Williams88f3b622011-04-22 19:18:03 -0700849 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
850 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
851
852 /* Initial state is a transitional state to the stopped state */
853 sci_base_state_machine_change_state(&sci_dev->state_machine,
854 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
855}
856
857/**
Dan Williams88f3b622011-04-22 19:18:03 -0700858 * scic_remote_device_destruct() - free remote node context and destruct
859 * @remote_device: This parameter specifies the remote device to be destructed.
860 *
861 * Remote device objects are a limited resource. As such, they must be
862 * protected. Thus calls to construct and destruct are mutually exclusive and
863 * non-reentrant. The return value shall indicate if the device was
864 * successfully destructed or if some failure occurred. enum sci_status This value
865 * is returned if the device is successfully destructed.
866 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
867 * device isn't valid (e.g. it's already been destoryed, the handle isn't
868 * valid, etc.).
869 */
870static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
871{
Dan Williamsb8d82f62011-05-01 14:38:26 -0700872 struct sci_base_state_machine *sm = &sci_dev->state_machine;
873 enum scic_sds_remote_device_states state = sm->current_state_id;
874 struct scic_sds_controller *scic;
875
876 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
877 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
878 __func__, state);
879 return SCI_FAILURE_INVALID_STATE;
880 }
881
882 scic = sci_dev->owning_port->owning_controller;
883 scic_sds_controller_free_remote_node_context(scic, sci_dev,
884 sci_dev->rnc.remote_node_index);
885 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
886 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
887
888 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -0700889}
890
Dan Williams6f231dd2011-07-02 22:56:22 -0700891/**
892 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -0800893 * @ihost: This parameter specifies the isci host object.
894 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -0700895 *
896 */
Dan Williamsd9c37392011-03-03 17:59:32 -0800897static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700898{
Dan Williamsd9c37392011-03-03 17:59:32 -0800899 dev_dbg(&ihost->pdev->dev,
900 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700901
902 /* There should not be any outstanding io's. All paths to
903 * here should go through isci_remote_device_nuke_requests.
904 * If we hit this condition, we will need a way to complete
905 * io requests in process */
Dan Williamsd9c37392011-03-03 17:59:32 -0800906 while (!list_empty(&idev->reqs_in_process)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700907
Dan Williamsd9c37392011-03-03 17:59:32 -0800908 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700909 "%s: ** request list not empty! **\n", __func__);
910 BUG();
911 }
912
Dan Williams57f20f42011-04-21 18:14:45 -0700913 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -0800914 idev->domain_dev->lldd_dev = NULL;
915 idev->domain_dev = NULL;
916 idev->isci_port = NULL;
917 list_del_init(&idev->node);
Dan Williams6ad31fe2011-03-04 12:10:29 -0800918
Dan Williamsd9c37392011-03-03 17:59:32 -0800919 clear_bit(IDEV_START_PENDING, &idev->flags);
920 clear_bit(IDEV_STOP_PENDING, &idev->flags);
921 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700922}
923
Dan Williams88f3b622011-04-22 19:18:03 -0700924/**
925 * isci_remote_device_stop_complete() - This function is called by the scic
926 * when the remote device stop has completed. We mark the isci device as not
927 * ready and remove the isci remote device.
928 * @ihost: This parameter specifies the isci host object.
929 * @idev: This parameter specifies the remote device.
930 * @status: This parameter specifies status of the completion.
931 *
932 */
933static void isci_remote_device_stop_complete(struct isci_host *ihost,
934 struct isci_remote_device *idev)
935{
936 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
937
938 isci_remote_device_change_state(idev, isci_stopped);
939
940 /* after stop, we can tear down resources. */
941 isci_remote_device_deconstruct(ihost, idev);
942}
943
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000944static void scic_sds_remote_device_stopped_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700945{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000946 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700947 struct scic_sds_controller *scic;
948 struct isci_remote_device *idev;
949 struct isci_host *ihost;
950 u32 prev_state;
951
Dan Williams88f3b622011-04-22 19:18:03 -0700952 scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +0000953 ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000954 idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700955
956 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
957 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
958
959 /* If we are entering from the stopping state let the SCI User know that
960 * the stop operation has completed.
961 */
962 prev_state = sci_dev->state_machine.previous_state_id;
963 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
964 isci_remote_device_stop_complete(ihost, idev);
965
966 scic_sds_controller_remote_device_stopped(scic, sci_dev);
967}
968
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000969static void scic_sds_remote_device_starting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700970{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000971 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -0700972 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
Maciej Patelczykd3757c32011-04-28 22:06:06 +0000973 struct isci_host *ihost = scic->ihost;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000974 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700975
976 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
977 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
978
979 isci_remote_device_not_ready(ihost, idev,
980 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
981}
982
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +0000983static void scic_sds_remote_device_ready_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -0700984{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000985 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700986 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
987 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -0700988
989 SET_STATE_HANDLER(sci_dev,
990 scic_sds_remote_device_state_handler_table,
991 SCI_BASE_REMOTE_DEVICE_STATE_READY);
992
993 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
994
Dan Williamsab2e8f72011-04-27 16:32:45 -0700995 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
996 sci_base_state_machine_change_state(&sci_dev->state_machine,
997 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
998 } else if (dev_is_expander(dev)) {
999 sci_base_state_machine_change_state(&sci_dev->state_machine,
1000 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1001 } else
1002 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
Dan Williams88f3b622011-04-22 19:18:03 -07001003}
1004
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001005static void scic_sds_remote_device_ready_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001006{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001007 struct scic_sds_remote_device *sci_dev = object;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001008 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1009
1010 if (dev->dev_type == SAS_END_DEV) {
1011 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001012 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001013
Dan Williamsab2e8f72011-04-27 16:32:45 -07001014 isci_remote_device_not_ready(scic->ihost, idev,
Dan Williams88f3b622011-04-22 19:18:03 -07001015 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1016 }
1017}
1018
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001019static void scic_sds_remote_device_stopping_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001020{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001021 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001022
1023 SET_STATE_HANDLER(
1024 sci_dev,
1025 scic_sds_remote_device_state_handler_table,
1026 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1027 );
1028}
1029
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001030static void scic_sds_remote_device_failed_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001031{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001032 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001033
1034 SET_STATE_HANDLER(
1035 sci_dev,
1036 scic_sds_remote_device_state_handler_table,
1037 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1038 );
1039}
1040
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001041static void scic_sds_remote_device_resetting_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001042{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001043 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001044
1045 SET_STATE_HANDLER(
1046 sci_dev,
1047 scic_sds_remote_device_state_handler_table,
1048 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1049 );
1050
1051 scic_sds_remote_node_context_suspend(
1052 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1053}
1054
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001055static void scic_sds_remote_device_resetting_state_exit(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001056{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001057 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001058
1059 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1060}
1061
Maciej Patelczyk9a0fff72011-04-28 22:06:01 +00001062static void scic_sds_remote_device_final_state_enter(void *object)
Dan Williams88f3b622011-04-22 19:18:03 -07001063{
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001064 struct scic_sds_remote_device *sci_dev = object;
Dan Williams88f3b622011-04-22 19:18:03 -07001065
1066 SET_STATE_HANDLER(
1067 sci_dev,
1068 scic_sds_remote_device_state_handler_table,
1069 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1070 );
1071}
1072
Dan Williamsab2e8f72011-04-27 16:32:45 -07001073static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
1074{
1075 struct scic_sds_remote_device *sci_dev = object;
1076
1077 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1078 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1079
1080 sci_dev->working_request = NULL;
1081 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
1082 /*
1083 * Since the RNC is ready, it's alright to finish completion
1084 * processing (e.g. signal the remote device is ready). */
1085 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
1086 } else {
1087 scic_sds_remote_node_context_resume(&sci_dev->rnc,
1088 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
1089 sci_dev);
1090 }
1091}
1092
1093static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1094{
1095 struct scic_sds_remote_device *sci_dev = object;
1096 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1097
1098 BUG_ON(sci_dev->working_request == NULL);
1099
1100 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1101 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1102
1103 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1104 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1105}
1106
1107static void scic_sds_stp_remote_device_ready_ncq_substate_enter(void *object)
1108{
1109 struct scic_sds_remote_device *sci_dev = object;
1110
1111 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1112 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ);
1113}
1114
1115static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1116{
1117 struct scic_sds_remote_device *sci_dev = object;
1118 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1119 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1120
1121 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1122 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1123
1124 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1125 isci_remote_device_not_ready(scic->ihost, idev,
1126 sci_dev->not_ready_reason);
1127}
1128
1129static void scic_sds_stp_remote_device_ready_await_reset_substate_enter(void *object)
1130{
1131 struct scic_sds_remote_device *sci_dev = object;
1132
1133 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1134 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
1135}
1136
1137static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1138{
1139 struct scic_sds_remote_device *sci_dev = object;
1140 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1141
1142 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1143 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1144
1145 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1146}
1147
1148static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1149{
1150 struct scic_sds_remote_device *sci_dev = object;
1151 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1152
1153 BUG_ON(sci_dev->working_request == NULL);
1154
1155 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1156 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1157
1158 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1159 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1160}
1161
1162static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1163{
1164 struct scic_sds_remote_device *sci_dev = object;
1165
1166 sci_dev->working_request = NULL;
1167}
Dan Williams88f3b622011-04-22 19:18:03 -07001168
1169static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1170 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1171 .enter_state = scic_sds_remote_device_initial_state_enter,
1172 },
1173 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1174 .enter_state = scic_sds_remote_device_stopped_state_enter,
1175 },
1176 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1177 .enter_state = scic_sds_remote_device_starting_state_enter,
1178 },
1179 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1180 .enter_state = scic_sds_remote_device_ready_state_enter,
1181 .exit_state = scic_sds_remote_device_ready_state_exit
1182 },
Dan Williamsab2e8f72011-04-27 16:32:45 -07001183 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1184 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1185 },
1186 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1187 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1188 },
1189 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
1190 .enter_state = scic_sds_stp_remote_device_ready_ncq_substate_enter,
1191 },
1192 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1193 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1194 },
1195 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
1196 .enter_state = scic_sds_stp_remote_device_ready_await_reset_substate_enter,
1197 },
1198 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1199 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1200 },
1201 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1202 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1203 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1204 },
Dan Williams88f3b622011-04-22 19:18:03 -07001205 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1206 .enter_state = scic_sds_remote_device_stopping_state_enter,
1207 },
1208 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1209 .enter_state = scic_sds_remote_device_failed_state_enter,
1210 },
1211 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1212 .enter_state = scic_sds_remote_device_resetting_state_enter,
1213 .exit_state = scic_sds_remote_device_resetting_state_exit
1214 },
1215 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1216 .enter_state = scic_sds_remote_device_final_state_enter,
1217 },
1218};
1219
1220/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001221 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001222 * @sci_port: SAS/SATA port through which this device is accessed.
1223 * @sci_dev: remote device to construct
1224 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001225 * This routine just performs benign initialization and does not
1226 * allocate the remote_node_context which is left to
1227 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1228 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001229 */
1230static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1231 struct scic_sds_remote_device *sci_dev)
1232{
1233 sci_dev->owning_port = sci_port;
1234 sci_dev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001235
1236 sci_base_state_machine_construct(
1237 &sci_dev->state_machine,
Maciej Patelczyk5d937e92011-04-28 22:06:21 +00001238 sci_dev,
Dan Williams88f3b622011-04-22 19:18:03 -07001239 scic_sds_remote_device_state_table,
1240 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1241 );
1242
1243 sci_base_state_machine_start(
1244 &sci_dev->state_machine
1245 );
1246
1247 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1248 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001249}
1250
1251/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001252 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001253 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001254 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1255 * the device is known to the SCI Core since it is contained in the
1256 * scic_phy object. Remote node context(s) is/are a global resource
1257 * allocated by this routine, freed by scic_remote_device_destruct().
1258 *
1259 * Returns:
1260 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1261 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1262 * sata-only controller instance.
1263 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001264 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001265static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1266 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001267{
1268 enum sci_status status;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001269 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001270
Dan Williamsb87ee302011-04-25 11:48:29 -07001271 scic_remote_device_construct(sci_port, sci_dev);
1272
Dan Williams88f3b622011-04-22 19:18:03 -07001273 /*
1274 * This information is request to determine how many remote node context
1275 * entries will be needed to store the remote node.
1276 */
Dan Williams88f3b622011-04-22 19:18:03 -07001277 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001278 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1279 sci_dev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001280 &sci_dev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001281
Dan Williamsa1a113b2011-04-21 18:44:45 -07001282 if (status != SCI_SUCCESS)
1283 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001284
Dan Williamsab2e8f72011-04-27 16:32:45 -07001285 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1286 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1287 /* pass */;
1288 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001289 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001290
Dan Williamsa1a113b2011-04-21 18:44:45 -07001291 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001292
Dan Williamsa1a113b2011-04-21 18:44:45 -07001293 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1294 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001295
Dan Williamsa1a113b2011-04-21 18:44:45 -07001296 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001297}
1298
Dan Williams88f3b622011-04-22 19:18:03 -07001299/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001300 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001301 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001302 * Remote node context(s) is/are a global resource allocated by this
1303 * routine, freed by scic_remote_device_destruct().
1304 *
1305 * Returns:
1306 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1307 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1308 * sata-only controller instance.
1309 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001310 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001311static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001312 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001313{
Dan Williamsa1a113b2011-04-21 18:44:45 -07001314 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001315 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001316
Dan Williamsb87ee302011-04-25 11:48:29 -07001317 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001318
Dan Williamsab2e8f72011-04-27 16:32:45 -07001319 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1320 sci_dev,
1321 &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001322 if (status != SCI_SUCCESS)
1323 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001324
Dan Williamsab2e8f72011-04-27 16:32:45 -07001325 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1326 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1327 /* pass */;
1328 else
1329 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001330
Dan Williamsa1a113b2011-04-21 18:44:45 -07001331 /*
1332 * For SAS-2 the physical link rate is actually a logical link
1333 * rate that incorporates multiplexing. The SCU doesn't
1334 * incorporate multiplexing and for the purposes of the
1335 * connection the logical link rate is that same as the
1336 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1337 * one another, so this code works for both situations. */
1338 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001339 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001340
Dan Williamsa1a113b2011-04-21 18:44:45 -07001341 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1342 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001343
Dan Williamsab2e8f72011-04-27 16:32:45 -07001344 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001345}
1346
1347/**
1348 * scic_remote_device_start() - This method will start the supplied remote
1349 * device. This method enables normal IO requests to flow through to the
1350 * remote device.
1351 * @remote_device: This parameter specifies the device to be started.
1352 * @timeout: This parameter specifies the number of milliseconds in which the
1353 * start operation should complete.
1354 *
1355 * An indication of whether the device was successfully started. SCI_SUCCESS
1356 * This value is returned if the device was successfully started.
1357 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1358 * the device when there have been no phys added to it.
1359 */
1360static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
Dan Williamseb229672011-05-01 14:05:57 -07001361 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001362{
Dan Williamseb229672011-05-01 14:05:57 -07001363 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1364 enum scic_sds_remote_device_states state = sm->current_state_id;
1365 enum sci_status status;
1366
1367 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1368 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1369 __func__, state);
1370 return SCI_FAILURE_INVALID_STATE;
1371 }
1372
1373 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1374 remote_device_resume_done,
1375 sci_dev);
1376 if (status != SCI_SUCCESS)
1377 return status;
1378
1379 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1380
1381 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001382}
Dan Williams6f231dd2011-07-02 22:56:22 -07001383
Dan Williams00d680e2011-04-25 14:29:29 -07001384static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1385 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001386{
Dan Williams00d680e2011-04-25 14:29:29 -07001387 struct scic_sds_port *sci_port = iport->sci_port_handle;
1388 struct isci_host *ihost = iport->isci_host;
1389 struct domain_device *dev = idev->domain_dev;
1390 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001391
Dan Williams00d680e2011-04-25 14:29:29 -07001392 if (dev->parent && dev_is_expander(dev->parent))
1393 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1394 else
1395 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001396
1397 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001398 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1399 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001400
1401 return status;
1402 }
1403
Dan Williams6f231dd2011-07-02 22:56:22 -07001404 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001405 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001406
Dan Williams00d680e2011-04-25 14:29:29 -07001407 if (status != SCI_SUCCESS)
1408 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1409 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001410
1411 return status;
1412}
1413
Dan Williams4393aa42011-03-31 13:10:44 -07001414void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001415{
1416 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001417
Dan Williams4393aa42011-03-31 13:10:44 -07001418 dev_dbg(&ihost->pdev->dev,
1419 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001420
1421 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -07001422 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -07001423
Dan Williams4393aa42011-03-31 13:10:44 -07001424 dev_dbg(&ihost->pdev->dev,
1425 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001426}
1427
Dan Williams6f231dd2011-07-02 22:56:22 -07001428/**
1429 * This function builds the isci_remote_device when a libsas dev_found message
1430 * is received.
1431 * @isci_host: This parameter specifies the isci host object.
1432 * @port: This parameter specifies the isci_port conected to this device.
1433 *
1434 * pointer to new isci_remote_device.
1435 */
1436static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001437isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001438{
Dan Williamsd9c37392011-03-03 17:59:32 -08001439 struct isci_remote_device *idev;
1440 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001441
Dan Williamsd9c37392011-03-03 17:59:32 -08001442 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001443 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001444 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1445 break;
1446 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001447
Dan Williamsd9c37392011-03-03 17:59:32 -08001448 if (i >= SCI_MAX_REMOTE_DEVICES) {
1449 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001450 return NULL;
1451 }
1452
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001453 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1454 return NULL;
1455
1456 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1457 return NULL;
1458
Dan Williamsd9c37392011-03-03 17:59:32 -08001459 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001460
Dan Williamsd9c37392011-03-03 17:59:32 -08001461 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001462}
Dan Williams6f231dd2011-07-02 22:56:22 -07001463
1464/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001465 * isci_remote_device_stop() - This function is called internally to stop the
1466 * remote device.
1467 * @isci_host: This parameter specifies the isci host object.
1468 * @isci_device: This parameter specifies the remote device.
1469 *
1470 * The status of the scic request to stop.
1471 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001472enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001473{
1474 enum sci_status status;
1475 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001476
Dan Williams6ad31fe2011-03-04 12:10:29 -08001477 dev_dbg(&ihost->pdev->dev,
1478 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001479
Dan Williams6ad31fe2011-03-04 12:10:29 -08001480 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001481
1482 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001483 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001484
Dan Williams6ad31fe2011-03-04 12:10:29 -08001485 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001486
Dan Williams6ad31fe2011-03-04 12:10:29 -08001487 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07001488 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001489 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001490
1491 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -08001492 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -08001493 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08001494 clear_bit(IDEV_ALLOCATED, &idev->flags);
1495 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001496
Dan Williams6ad31fe2011-03-04 12:10:29 -08001497 dev_dbg(&ihost->pdev->dev,
1498 "%s: idev = %p - after completion wait\n",
1499 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001500
Dan Williams6f231dd2011-07-02 22:56:22 -07001501 return status;
1502}
1503
1504/**
1505 * isci_remote_device_gone() - This function is called by libsas when a domain
1506 * device is removed.
1507 * @domain_device: This parameter specifies the libsas domain device.
1508 *
1509 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001510void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001511{
Dan Williams4393aa42011-03-31 13:10:44 -07001512 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001513 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001514
Dan Williams6ad31fe2011-03-04 12:10:29 -08001515 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001516 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001517 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001518
Dan Williams6ad31fe2011-03-04 12:10:29 -08001519 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001520}
1521
1522
1523/**
1524 * isci_remote_device_found() - This function is called by libsas when a remote
1525 * device is discovered. A remote device object is created and started. the
1526 * function then sleeps until the sci core device started message is
1527 * received.
1528 * @domain_device: This parameter specifies the libsas domain device.
1529 *
1530 * status, zero indicates success.
1531 */
1532int isci_remote_device_found(struct domain_device *domain_dev)
1533{
Dan Williams4393aa42011-03-31 13:10:44 -07001534 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001535 struct isci_port *isci_port;
1536 struct isci_phy *isci_phy;
1537 struct asd_sas_port *sas_port;
1538 struct asd_sas_phy *sas_phy;
1539 struct isci_remote_device *isci_device;
1540 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001541
Dan Williams6f231dd2011-07-02 22:56:22 -07001542 dev_dbg(&isci_host->pdev->dev,
1543 "%s: domain_device = %p\n", __func__, domain_dev);
1544
Dan Williams0cf89d12011-02-18 09:25:07 -08001545 wait_for_start(isci_host);
1546
Dan Williams6f231dd2011-07-02 22:56:22 -07001547 sas_port = domain_dev->port;
1548 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1549 port_phy_el);
1550 isci_phy = to_isci_phy(sas_phy);
1551 isci_port = isci_phy->isci_port;
1552
1553 /* we are being called for a device on this port,
1554 * so it has to come up eventually
1555 */
1556 wait_for_completion(&isci_port->start_complete);
1557
1558 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1559 (isci_stopped == isci_port_get_state(isci_port)))
1560 return -ENODEV;
1561
1562 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001563 if (!isci_device)
1564 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001565
1566 INIT_LIST_HEAD(&isci_device->node);
1567 domain_dev->lldd_dev = isci_device;
1568 isci_device->domain_dev = domain_dev;
1569 isci_device->isci_port = isci_port;
1570 isci_remote_device_change_state(isci_device, isci_starting);
1571
1572
Dan Williams1a380452011-03-03 18:01:43 -08001573 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001574 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1575
Dan Williams6ad31fe2011-03-04 12:10:29 -08001576 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001577 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -08001578 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001579
Dan Williams6f231dd2011-07-02 22:56:22 -07001580 dev_dbg(&isci_host->pdev->dev,
1581 "%s: isci_device = %p\n",
1582 __func__, isci_device);
1583
1584 if (status != SCI_SUCCESS) {
1585
Dan Williams1a380452011-03-03 18:01:43 -08001586 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001587 isci_remote_device_deconstruct(
1588 isci_host,
1589 isci_device
1590 );
Dan Williams1a380452011-03-03 18:01:43 -08001591 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001592 return -ENODEV;
1593 }
1594
Dan Williams6ad31fe2011-03-04 12:10:29 -08001595 /* wait for the device ready callback. */
1596 wait_for_device_start(isci_host, isci_device);
1597
Dan Williams6f231dd2011-07-02 22:56:22 -07001598 return 0;
1599}
1600/**
1601 * isci_device_is_reset_pending() - This function will check if there is any
1602 * pending reset condition on the device.
1603 * @request: This parameter is the isci_device object.
1604 *
1605 * true if there is a reset pending for the device.
1606 */
1607bool isci_device_is_reset_pending(
1608 struct isci_host *isci_host,
1609 struct isci_remote_device *isci_device)
1610{
1611 struct isci_request *isci_request;
1612 struct isci_request *tmp_req;
1613 bool reset_is_pending = false;
1614 unsigned long flags;
1615
1616 dev_dbg(&isci_host->pdev->dev,
1617 "%s: isci_device = %p\n", __func__, isci_device);
1618
1619 spin_lock_irqsave(&isci_host->scic_lock, flags);
1620
1621 /* Check for reset on all pending requests. */
1622 list_for_each_entry_safe(isci_request, tmp_req,
1623 &isci_device->reqs_in_process, dev_node) {
1624 dev_dbg(&isci_host->pdev->dev,
1625 "%s: isci_device = %p request = %p\n",
1626 __func__, isci_device, isci_request);
1627
1628 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001629 struct sas_task *task = isci_request_access_task(
1630 isci_request);
1631
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001632 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001633 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1634 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001635 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001636 }
1637 }
1638
1639 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1640
1641 dev_dbg(&isci_host->pdev->dev,
1642 "%s: isci_device = %p reset_is_pending = %d\n",
1643 __func__, isci_device, reset_is_pending);
1644
1645 return reset_is_pending;
1646}
1647
1648/**
1649 * isci_device_clear_reset_pending() - This function will clear if any pending
1650 * reset condition flags on the device.
1651 * @request: This parameter is the isci_device object.
1652 *
1653 * true if there is a reset pending for the device.
1654 */
Dan Williams4393aa42011-03-31 13:10:44 -07001655void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001656{
1657 struct isci_request *isci_request;
1658 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07001659 unsigned long flags = 0;
1660
Dan Williams4393aa42011-03-31 13:10:44 -07001661 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1662 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07001663
Dan Williams4393aa42011-03-31 13:10:44 -07001664 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001665
1666 /* Clear reset pending on all pending requests. */
1667 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07001668 &idev->reqs_in_process, dev_node) {
1669 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1670 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001671
1672 if (isci_request->ttype == io_task) {
1673
1674 unsigned long flags2;
1675 struct sas_task *task = isci_request_access_task(
1676 isci_request);
1677
1678 spin_lock_irqsave(&task->task_state_lock, flags2);
1679 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1680 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1681 }
1682 }
Dan Williams4393aa42011-03-31 13:10:44 -07001683 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001684}