[SCSI] isci: debug, provide state-enum-to-string conversions
[deliverable/linux.git] / drivers / scsi / isci / remote_device.c
CommitLineData
6f231dda
DW
1/*
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 */
2d9c2240 55#include <scsi/sas.h>
7e629841 56#include <linux/bitops.h>
88f3b62a
DW
57#include "isci.h"
58#include "port.h"
59#include "remote_device.h"
60#include "request.h"
88f3b62a 61#include "remote_node_context.h"
88f3b62a
DW
62#include "scu_event_codes.h"
63#include "task.h"
64
d7a0ccdd
DW
65#undef C
66#define C(a) (#a)
67const char *dev_state_name(enum sci_remote_device_states state)
68{
69 static const char * const strings[] = REMOTE_DEV_STATES;
70
71 return strings[state];
72}
73#undef C
74
ab2e8f7d 75/**
d9dcb4ba 76 * isci_remote_device_not_ready() - This function is called by the ihost when
ab2e8f7d
DW
77 * the remote device is not ready. We mark the isci device as ready (not
78 * "ready_for_io") and signal the waiting proccess.
79 * @isci_host: This parameter specifies the isci host object.
80 * @isci_device: This parameter specifies the remote device
81 *
89a7301f 82 * sci_lock is held on entrance to this function.
ab2e8f7d
DW
83 */
84static void isci_remote_device_not_ready(struct isci_host *ihost,
85 struct isci_remote_device *idev, u32 reason)
86{
a5ec7f86 87 struct isci_request *ireq;
9274f45e 88
ab2e8f7d
DW
89 dev_dbg(&ihost->pdev->dev,
90 "%s: isci_device = %p\n", __func__, idev);
91
9274f45e
JS
92 switch (reason) {
93 case SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED:
209fae14 94 set_bit(IDEV_GONE, &idev->flags);
9274f45e
JS
95 break;
96 case SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED:
97 set_bit(IDEV_IO_NCQERROR, &idev->flags);
98
99 /* Kill all outstanding requests for the device. */
100 list_for_each_entry(ireq, &idev->reqs_in_process, dev_node) {
101
102 dev_dbg(&ihost->pdev->dev,
103 "%s: isci_device = %p request = %p\n",
104 __func__, idev, ireq);
105
89a7301f 106 sci_controller_terminate_request(ihost,
78a6f06e 107 idev,
5076a1a9 108 ireq);
9274f45e
JS
109 }
110 /* Fall through into the default case... */
111 default:
f2088267 112 clear_bit(IDEV_IO_READY, &idev->flags);
9274f45e
JS
113 break;
114 }
ab2e8f7d
DW
115}
116
117/**
d9dcb4ba 118 * isci_remote_device_ready() - This function is called by the ihost when the
ab2e8f7d
DW
119 * remote device is ready. We mark the isci device as ready and signal the
120 * waiting proccess.
121 * @ihost: our valid isci_host
122 * @idev: remote device
123 *
124 */
125static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
126{
127 dev_dbg(&ihost->pdev->dev,
128 "%s: idev = %p\n", __func__, idev);
129
9274f45e 130 clear_bit(IDEV_IO_NCQERROR, &idev->flags);
f2088267 131 set_bit(IDEV_IO_READY, &idev->flags);
ab2e8f7d
DW
132 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
133 wake_up(&ihost->eventq);
134}
135
ec575669
DW
136/* called once the remote node context is ready to be freed.
137 * The remote device can now report that its stop operation is complete. none
138 */
139static void rnc_destruct_done(void *_dev)
140{
78a6f06e 141 struct isci_remote_device *idev = _dev;
ab2e8f7d 142
78a6f06e
DW
143 BUG_ON(idev->started_request_count != 0);
144 sci_change_state(&idev->sm, SCI_DEV_STOPPED);
ec575669 145}
ab2e8f7d 146
89a7301f 147static enum sci_status sci_remote_device_terminate_requests(struct isci_remote_device *idev)
ec575669 148{
d9dcb4ba 149 struct isci_host *ihost = idev->owning_port->owning_controller;
ec575669 150 enum sci_status status = SCI_SUCCESS;
76802ce6 151 u32 i;
ec575669 152
76802ce6 153 for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
db056250 154 struct isci_request *ireq = ihost->reqs[i];
ec575669
DW
155 enum sci_status s;
156
db056250 157 if (!test_bit(IREQ_ACTIVE, &ireq->flags) ||
78a6f06e 158 ireq->target_device != idev)
ec575669 159 continue;
db056250 160
89a7301f 161 s = sci_controller_terminate_request(ihost, idev, ireq);
ec575669
DW
162 if (s != SCI_SUCCESS)
163 status = s;
164 }
165
166 return status;
167}
168
89a7301f 169enum sci_status sci_remote_device_stop(struct isci_remote_device *idev,
ec575669 170 u32 timeout)
88f3b62a 171{
78a6f06e 172 struct sci_base_state_machine *sm = &idev->sm;
89a7301f 173 enum sci_remote_device_states state = sm->current_state_id;
ec575669
DW
174
175 switch (state) {
e301370a
EN
176 case SCI_DEV_INITIAL:
177 case SCI_DEV_FAILED:
178 case SCI_DEV_FINAL:
ec575669 179 default:
d7a0ccdd
DW
180 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
181 __func__, dev_state_name(state));
ec575669 182 return SCI_FAILURE_INVALID_STATE;
e301370a 183 case SCI_DEV_STOPPED:
ec575669 184 return SCI_SUCCESS;
e301370a 185 case SCI_DEV_STARTING:
ec575669 186 /* device not started so there had better be no requests */
78a6f06e 187 BUG_ON(idev->started_request_count != 0);
89a7301f 188 sci_remote_node_context_destruct(&idev->rnc,
78a6f06e 189 rnc_destruct_done, idev);
ec575669
DW
190 /* Transition to the stopping state and wait for the
191 * remote node to complete being posted and invalidated.
192 */
e301370a 193 sci_change_state(sm, SCI_DEV_STOPPING);
ec575669 194 return SCI_SUCCESS;
e301370a
EN
195 case SCI_DEV_READY:
196 case SCI_STP_DEV_IDLE:
197 case SCI_STP_DEV_CMD:
198 case SCI_STP_DEV_NCQ:
199 case SCI_STP_DEV_NCQ_ERROR:
200 case SCI_STP_DEV_AWAIT_RESET:
201 case SCI_SMP_DEV_IDLE:
202 case SCI_SMP_DEV_CMD:
203 sci_change_state(sm, SCI_DEV_STOPPING);
78a6f06e 204 if (idev->started_request_count == 0) {
89a7301f 205 sci_remote_node_context_destruct(&idev->rnc,
78a6f06e 206 rnc_destruct_done, idev);
ec575669
DW
207 return SCI_SUCCESS;
208 } else
89a7301f 209 return sci_remote_device_terminate_requests(idev);
ec575669 210 break;
e301370a 211 case SCI_DEV_STOPPING:
ec575669
DW
212 /* All requests should have been terminated, but if there is an
213 * attempt to stop a device already in the stopping state, then
214 * try again to terminate.
215 */
89a7301f 216 return sci_remote_device_terminate_requests(idev);
e301370a
EN
217 case SCI_DEV_RESETTING:
218 sci_change_state(sm, SCI_DEV_STOPPING);
ec575669
DW
219 return SCI_SUCCESS;
220 }
88f3b62a
DW
221}
222
89a7301f 223enum sci_status sci_remote_device_reset(struct isci_remote_device *idev)
88f3b62a 224{
78a6f06e 225 struct sci_base_state_machine *sm = &idev->sm;
89a7301f 226 enum sci_remote_device_states state = sm->current_state_id;
4fd0d2e9
DW
227
228 switch (state) {
e301370a
EN
229 case SCI_DEV_INITIAL:
230 case SCI_DEV_STOPPED:
231 case SCI_DEV_STARTING:
232 case SCI_SMP_DEV_IDLE:
233 case SCI_SMP_DEV_CMD:
234 case SCI_DEV_STOPPING:
235 case SCI_DEV_FAILED:
236 case SCI_DEV_RESETTING:
237 case SCI_DEV_FINAL:
4fd0d2e9 238 default:
d7a0ccdd
DW
239 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
240 __func__, dev_state_name(state));
4fd0d2e9 241 return SCI_FAILURE_INVALID_STATE;
e301370a
EN
242 case SCI_DEV_READY:
243 case SCI_STP_DEV_IDLE:
244 case SCI_STP_DEV_CMD:
245 case SCI_STP_DEV_NCQ:
246 case SCI_STP_DEV_NCQ_ERROR:
247 case SCI_STP_DEV_AWAIT_RESET:
248 sci_change_state(sm, SCI_DEV_RESETTING);
4fd0d2e9
DW
249 return SCI_SUCCESS;
250 }
88f3b62a
DW
251}
252
89a7301f 253enum sci_status sci_remote_device_reset_complete(struct isci_remote_device *idev)
88f3b62a 254{
78a6f06e 255 struct sci_base_state_machine *sm = &idev->sm;
89a7301f 256 enum sci_remote_device_states state = sm->current_state_id;
81515182 257
e301370a 258 if (state != SCI_DEV_RESETTING) {
d7a0ccdd
DW
259 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
260 __func__, dev_state_name(state));
81515182
DW
261 return SCI_FAILURE_INVALID_STATE;
262 }
88f3b62a 263
e301370a 264 sci_change_state(sm, SCI_DEV_READY);
81515182
DW
265 return SCI_SUCCESS;
266}
88f3b62a 267
89a7301f 268enum sci_status sci_remote_device_suspend(struct isci_remote_device *idev,
323f0ec0 269 u32 suspend_type)
88f3b62a 270{
78a6f06e 271 struct sci_base_state_machine *sm = &idev->sm;
89a7301f 272 enum sci_remote_device_states state = sm->current_state_id;
323f0ec0 273
e301370a 274 if (state != SCI_STP_DEV_CMD) {
d7a0ccdd
DW
275 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
276 __func__, dev_state_name(state));
323f0ec0
DW
277 return SCI_FAILURE_INVALID_STATE;
278 }
279
89a7301f 280 return sci_remote_node_context_suspend(&idev->rnc,
323f0ec0 281 suspend_type, NULL, NULL);
88f3b62a
DW
282}
283
89a7301f 284enum sci_status sci_remote_device_frame_handler(struct isci_remote_device *idev,
01bec778 285 u32 frame_index)
88f3b62a 286{
78a6f06e 287 struct sci_base_state_machine *sm = &idev->sm;
89a7301f 288 enum sci_remote_device_states state = sm->current_state_id;
d9dcb4ba 289 struct isci_host *ihost = idev->owning_port->owning_controller;
01bec778
DW
290 enum sci_status status;
291
292 switch (state) {
e301370a
EN
293 case SCI_DEV_INITIAL:
294 case SCI_DEV_STOPPED:
295 case SCI_DEV_STARTING:
296 case SCI_STP_DEV_IDLE:
297 case SCI_SMP_DEV_IDLE:
298 case SCI_DEV_FINAL:
01bec778 299 default:
d7a0ccdd
DW
300 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
301 __func__, dev_state_name(state));
01bec778 302 /* Return the frame back to the controller */
89a7301f 303 sci_controller_release_frame(ihost, frame_index);
01bec778 304 return SCI_FAILURE_INVALID_STATE;
e301370a
EN
305 case SCI_DEV_READY:
306 case SCI_STP_DEV_NCQ_ERROR:
307 case SCI_STP_DEV_AWAIT_RESET:
308 case SCI_DEV_STOPPING:
309 case SCI_DEV_FAILED:
310 case SCI_DEV_RESETTING: {
5076a1a9 311 struct isci_request *ireq;
2d9c2240
DJ
312 struct ssp_frame_hdr hdr;
313 void *frame_header;
314 ssize_t word_cnt;
01bec778 315
89a7301f 316 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
01bec778 317 frame_index,
2d9c2240 318 &frame_header);
01bec778
DW
319 if (status != SCI_SUCCESS)
320 return status;
321
2d9c2240
DJ
322 word_cnt = sizeof(hdr) / sizeof(u32);
323 sci_swab32_cpy(&hdr, frame_header, word_cnt);
324
89a7301f 325 ireq = sci_request_by_tag(ihost, be16_to_cpu(hdr.tag));
78a6f06e 326 if (ireq && ireq->target_device == idev) {
01bec778 327 /* The IO request is now in charge of releasing the frame */
89a7301f 328 status = sci_io_request_frame_handler(ireq, frame_index);
01bec778
DW
329 } else {
330 /* We could not map this tag to a valid IO
331 * request Just toss the frame and continue
332 */
89a7301f 333 sci_controller_release_frame(ihost, frame_index);
01bec778
DW
334 }
335 break;
336 }
e301370a 337 case SCI_STP_DEV_NCQ: {
e76d6180 338 struct dev_to_host_fis *hdr;
01bec778 339
89a7301f 340 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
01bec778
DW
341 frame_index,
342 (void **)&hdr);
343 if (status != SCI_SUCCESS)
344 return status;
345
e76d6180
DJ
346 if (hdr->fis_type == FIS_SETDEVBITS &&
347 (hdr->status & ATA_ERR)) {
78a6f06e 348 idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
01bec778
DW
349
350 /* TODO Check sactive and complete associated IO if any. */
e301370a 351 sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
e76d6180
DJ
352 } else if (hdr->fis_type == FIS_REGD2H &&
353 (hdr->status & ATA_ERR)) {
01bec778
DW
354 /*
355 * Some devices return D2H FIS when an NCQ error is detected.
356 * Treat this like an SDB error FIS ready reason.
357 */
78a6f06e
DW
358 idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
359 sci_change_state(&idev->sm, SCI_STP_DEV_NCQ_ERROR);
01bec778
DW
360 } else
361 status = SCI_FAILURE;
362
89a7301f 363 sci_controller_release_frame(ihost, frame_index);
01bec778
DW
364 break;
365 }
e301370a
EN
366 case SCI_STP_DEV_CMD:
367 case SCI_SMP_DEV_CMD:
01bec778
DW
368 /* The device does not process any UF received from the hardware while
369 * in this state. All unsolicited frames are forwarded to the io request
370 * object.
371 */
89a7301f 372 status = sci_io_request_frame_handler(idev->working_request, frame_index);
01bec778
DW
373 break;
374 }
375
376 return status;
88f3b62a
DW
377}
378
78a6f06e 379static bool is_remote_device_ready(struct isci_remote_device *idev)
88f3b62a 380{
e622571f 381
78a6f06e 382 struct sci_base_state_machine *sm = &idev->sm;
89a7301f 383 enum sci_remote_device_states state = sm->current_state_id;
e622571f
DW
384
385 switch (state) {
e301370a
EN
386 case SCI_DEV_READY:
387 case SCI_STP_DEV_IDLE:
388 case SCI_STP_DEV_CMD:
389 case SCI_STP_DEV_NCQ:
390 case SCI_STP_DEV_NCQ_ERROR:
391 case SCI_STP_DEV_AWAIT_RESET:
392 case SCI_SMP_DEV_IDLE:
393 case SCI_SMP_DEV_CMD:
e622571f
DW
394 return true;
395 default:
396 return false;
397 }
398}
399
b50102d3
DW
400/*
401 * called once the remote node context has transisitioned to a ready
402 * state (after suspending RX and/or TX due to early D2H fis)
403 */
404static void atapi_remote_device_resume_done(void *_dev)
405{
406 struct isci_remote_device *idev = _dev;
407 struct isci_request *ireq = idev->working_request;
408
409 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
410}
411
89a7301f 412enum sci_status sci_remote_device_event_handler(struct isci_remote_device *idev,
e622571f
DW
413 u32 event_code)
414{
78a6f06e 415 struct sci_base_state_machine *sm = &idev->sm;
89a7301f 416 enum sci_remote_device_states state = sm->current_state_id;
e622571f
DW
417 enum sci_status status;
418
419 switch (scu_get_event_type(event_code)) {
420 case SCU_EVENT_TYPE_RNC_OPS_MISC:
421 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
422 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
89a7301f 423 status = sci_remote_node_context_event_handler(&idev->rnc, event_code);
e622571f
DW
424 break;
425 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
426 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
427 status = SCI_SUCCESS;
428
429 /* Suspend the associated RNC */
89a7301f 430 sci_remote_node_context_suspend(&idev->rnc,
e622571f
DW
431 SCI_SOFTWARE_SUSPENSION,
432 NULL, NULL);
433
78a6f06e 434 dev_dbg(scirdev_to_dev(idev),
e622571f 435 "%s: device: %p event code: %x: %s\n",
78a6f06e
DW
436 __func__, idev, event_code,
437 is_remote_device_ready(idev)
e622571f
DW
438 ? "I_T_Nexus_Timeout event"
439 : "I_T_Nexus_Timeout event in wrong state");
440
441 break;
442 }
443 /* Else, fall through and treat as unhandled... */
444 default:
78a6f06e 445 dev_dbg(scirdev_to_dev(idev),
e622571f 446 "%s: device: %p event code: %x: %s\n",
78a6f06e
DW
447 __func__, idev, event_code,
448 is_remote_device_ready(idev)
e622571f
DW
449 ? "unexpected event"
450 : "unexpected event in wrong state");
451 status = SCI_FAILURE_INVALID_STATE;
452 break;
453 }
454
455 if (status != SCI_SUCCESS)
456 return status;
457
b50102d3
DW
458 if (state == SCI_STP_DEV_ATAPI_ERROR) {
459 /* For ATAPI error state resume the RNC right away. */
460 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
461 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX) {
462 return sci_remote_node_context_resume(&idev->rnc,
463 atapi_remote_device_resume_done,
464 idev);
465 }
466 }
467
e301370a 468 if (state == SCI_STP_DEV_IDLE) {
e622571f
DW
469
470 /* We pick up suspension events to handle specifically to this
471 * state. We resume the RNC right away.
472 */
473 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
474 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
89a7301f 475 status = sci_remote_node_context_resume(&idev->rnc, NULL, NULL);
e622571f
DW
476 }
477
478 return status;
88f3b62a
DW
479}
480
89a7301f 481static void sci_remote_device_start_request(struct isci_remote_device *idev,
5076a1a9 482 struct isci_request *ireq,
18606557 483 enum sci_status status)
88f3b62a 484{
78a6f06e 485 struct isci_port *iport = idev->owning_port;
18606557
DW
486
487 /* cleanup requests that failed after starting on the port */
488 if (status != SCI_SUCCESS)
89a7301f 489 sci_port_complete_io(iport, idev, ireq);
209fae14 490 else {
78a6f06e 491 kref_get(&idev->kref);
34a99158 492 idev->started_request_count++;
209fae14 493 }
18606557
DW
494}
495
89a7301f 496enum sci_status sci_remote_device_start_io(struct isci_host *ihost,
78a6f06e 497 struct isci_remote_device *idev,
5076a1a9 498 struct isci_request *ireq)
18606557 499{
78a6f06e 500 struct sci_base_state_machine *sm = &idev->sm;
89a7301f 501 enum sci_remote_device_states state = sm->current_state_id;
78a6f06e 502 struct isci_port *iport = idev->owning_port;
18606557
DW
503 enum sci_status status;
504
505 switch (state) {
e301370a
EN
506 case SCI_DEV_INITIAL:
507 case SCI_DEV_STOPPED:
508 case SCI_DEV_STARTING:
509 case SCI_STP_DEV_NCQ_ERROR:
510 case SCI_DEV_STOPPING:
511 case SCI_DEV_FAILED:
512 case SCI_DEV_RESETTING:
513 case SCI_DEV_FINAL:
18606557 514 default:
d7a0ccdd
DW
515 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
516 __func__, dev_state_name(state));
18606557 517 return SCI_FAILURE_INVALID_STATE;
e301370a 518 case SCI_DEV_READY:
18606557
DW
519 /* attempt to start an io request for this device object. The remote
520 * device object will issue the start request for the io and if
521 * successful it will start the request for the port object then
522 * increment its own request count.
523 */
89a7301f 524 status = sci_port_start_io(iport, idev, ireq);
18606557
DW
525 if (status != SCI_SUCCESS)
526 return status;
527
89a7301f 528 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
18606557
DW
529 if (status != SCI_SUCCESS)
530 break;
531
89a7301f 532 status = sci_request_start(ireq);
18606557 533 break;
e301370a 534 case SCI_STP_DEV_IDLE: {
18606557
DW
535 /* handle the start io operation for a sata device that is in
536 * the command idle state. - Evalute the type of IO request to
537 * be started - If its an NCQ request change to NCQ substate -
538 * If its any other command change to the CMD substate
539 *
540 * If this is a softreset we may want to have a different
541 * substate.
542 */
89a7301f 543 enum sci_remote_device_states new_state;
e76d6180 544 struct sas_task *task = isci_request_access_task(ireq);
18606557 545
89a7301f 546 status = sci_port_start_io(iport, idev, ireq);
18606557
DW
547 if (status != SCI_SUCCESS)
548 return status;
549
89a7301f 550 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
18606557
DW
551 if (status != SCI_SUCCESS)
552 break;
553
89a7301f 554 status = sci_request_start(ireq);
18606557
DW
555 if (status != SCI_SUCCESS)
556 break;
557
e76d6180 558 if (task->ata_task.use_ncq)
e301370a 559 new_state = SCI_STP_DEV_NCQ;
18606557 560 else {
78a6f06e 561 idev->working_request = ireq;
e301370a 562 new_state = SCI_STP_DEV_CMD;
18606557 563 }
e301370a 564 sci_change_state(sm, new_state);
18606557
DW
565 break;
566 }
e301370a 567 case SCI_STP_DEV_NCQ: {
e76d6180
DJ
568 struct sas_task *task = isci_request_access_task(ireq);
569
570 if (task->ata_task.use_ncq) {
89a7301f 571 status = sci_port_start_io(iport, idev, ireq);
18606557
DW
572 if (status != SCI_SUCCESS)
573 return status;
574
89a7301f 575 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
18606557
DW
576 if (status != SCI_SUCCESS)
577 break;
578
89a7301f 579 status = sci_request_start(ireq);
18606557
DW
580 } else
581 return SCI_FAILURE_INVALID_STATE;
582 break;
e76d6180 583 }
e301370a 584 case SCI_STP_DEV_AWAIT_RESET:
18606557 585 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
e301370a 586 case SCI_SMP_DEV_IDLE:
89a7301f 587 status = sci_port_start_io(iport, idev, ireq);
18606557
DW
588 if (status != SCI_SUCCESS)
589 return status;
590
89a7301f 591 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
18606557
DW
592 if (status != SCI_SUCCESS)
593 break;
594
89a7301f 595 status = sci_request_start(ireq);
18606557
DW
596 if (status != SCI_SUCCESS)
597 break;
598
78a6f06e
DW
599 idev->working_request = ireq;
600 sci_change_state(&idev->sm, SCI_SMP_DEV_CMD);
18606557 601 break;
e301370a
EN
602 case SCI_STP_DEV_CMD:
603 case SCI_SMP_DEV_CMD:
18606557
DW
604 /* device is already handling a command it can not accept new commands
605 * until this one is complete.
606 */
607 return SCI_FAILURE_INVALID_STATE;
608 }
609
89a7301f 610 sci_remote_device_start_request(idev, ireq, status);
18606557 611 return status;
88f3b62a
DW
612}
613
ffe191c9 614static enum sci_status common_complete_io(struct isci_port *iport,
78a6f06e 615 struct isci_remote_device *idev,
5076a1a9 616 struct isci_request *ireq)
88f3b62a 617{
10a09e64
DW
618 enum sci_status status;
619
89a7301f 620 status = sci_request_complete(ireq);
10a09e64
DW
621 if (status != SCI_SUCCESS)
622 return status;
623
89a7301f 624 status = sci_port_complete_io(iport, idev, ireq);
10a09e64
DW
625 if (status != SCI_SUCCESS)
626 return status;
627
89a7301f 628 sci_remote_device_decrement_request_count(idev);
10a09e64
DW
629 return status;
630}
631
89a7301f 632enum sci_status sci_remote_device_complete_io(struct isci_host *ihost,
78a6f06e 633 struct isci_remote_device *idev,
5076a1a9 634 struct isci_request *ireq)
10a09e64 635{
78a6f06e 636 struct sci_base_state_machine *sm = &idev->sm;
89a7301f 637 enum sci_remote_device_states state = sm->current_state_id;
78a6f06e 638 struct isci_port *iport = idev->owning_port;
10a09e64
DW
639 enum sci_status status;
640
641 switch (state) {
e301370a
EN
642 case SCI_DEV_INITIAL:
643 case SCI_DEV_STOPPED:
644 case SCI_DEV_STARTING:
645 case SCI_STP_DEV_IDLE:
646 case SCI_SMP_DEV_IDLE:
647 case SCI_DEV_FAILED:
648 case SCI_DEV_FINAL:
10a09e64 649 default:
d7a0ccdd
DW
650 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
651 __func__, dev_state_name(state));
10a09e64 652 return SCI_FAILURE_INVALID_STATE;
e301370a
EN
653 case SCI_DEV_READY:
654 case SCI_STP_DEV_AWAIT_RESET:
655 case SCI_DEV_RESETTING:
78a6f06e 656 status = common_complete_io(iport, idev, ireq);
10a09e64 657 break;
e301370a
EN
658 case SCI_STP_DEV_CMD:
659 case SCI_STP_DEV_NCQ:
660 case SCI_STP_DEV_NCQ_ERROR:
b50102d3 661 case SCI_STP_DEV_ATAPI_ERROR:
78a6f06e 662 status = common_complete_io(iport, idev, ireq);
10a09e64
DW
663 if (status != SCI_SUCCESS)
664 break;
665
5076a1a9 666 if (ireq->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
10a09e64
DW
667 /* This request causes hardware error, device needs to be Lun Reset.
668 * So here we force the state machine to IDLE state so the rest IOs
669 * can reach RNC state handler, these IOs will be completed by RNC with
670 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
671 */
e301370a 672 sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
34a99158 673 } else if (idev->started_request_count == 0)
e301370a 674 sci_change_state(sm, SCI_STP_DEV_IDLE);
10a09e64 675 break;
e301370a 676 case SCI_SMP_DEV_CMD:
78a6f06e 677 status = common_complete_io(iport, idev, ireq);
10a09e64
DW
678 if (status != SCI_SUCCESS)
679 break;
e301370a 680 sci_change_state(sm, SCI_SMP_DEV_IDLE);
10a09e64 681 break;
e301370a 682 case SCI_DEV_STOPPING:
78a6f06e 683 status = common_complete_io(iport, idev, ireq);
10a09e64
DW
684 if (status != SCI_SUCCESS)
685 break;
686
34a99158 687 if (idev->started_request_count == 0)
89a7301f 688 sci_remote_node_context_destruct(&idev->rnc,
34a99158
DW
689 rnc_destruct_done,
690 idev);
10a09e64
DW
691 break;
692 }
693
694 if (status != SCI_SUCCESS)
78a6f06e 695 dev_err(scirdev_to_dev(idev),
10a09e64 696 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
ffe191c9 697 "could not complete\n", __func__, iport,
78a6f06e 698 idev, ireq, status);
209fae14 699 else
78a6f06e 700 isci_put_device(idev);
10a09e64
DW
701
702 return status;
88f3b62a
DW
703}
704
89a7301f 705static void sci_remote_device_continue_request(void *dev)
88f3b62a 706{
78a6f06e 707 struct isci_remote_device *idev = dev;
84b9b029
DW
708
709 /* we need to check if this request is still valid to continue. */
78a6f06e 710 if (idev->working_request)
89a7301f 711 sci_controller_continue_io(idev->working_request);
84b9b029
DW
712}
713
89a7301f 714enum sci_status sci_remote_device_start_task(struct isci_host *ihost,
78a6f06e 715 struct isci_remote_device *idev,
5076a1a9 716 struct isci_request *ireq)
84b9b029 717{
78a6f06e 718 struct sci_base_state_machine *sm = &idev->sm;
89a7301f 719 enum sci_remote_device_states state = sm->current_state_id;
78a6f06e 720 struct isci_port *iport = idev->owning_port;
84b9b029
DW
721 enum sci_status status;
722
723 switch (state) {
e301370a
EN
724 case SCI_DEV_INITIAL:
725 case SCI_DEV_STOPPED:
726 case SCI_DEV_STARTING:
727 case SCI_SMP_DEV_IDLE:
728 case SCI_SMP_DEV_CMD:
729 case SCI_DEV_STOPPING:
730 case SCI_DEV_FAILED:
731 case SCI_DEV_RESETTING:
732 case SCI_DEV_FINAL:
84b9b029 733 default:
d7a0ccdd
DW
734 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
735 __func__, dev_state_name(state));
84b9b029 736 return SCI_FAILURE_INVALID_STATE;
e301370a
EN
737 case SCI_STP_DEV_IDLE:
738 case SCI_STP_DEV_CMD:
739 case SCI_STP_DEV_NCQ:
740 case SCI_STP_DEV_NCQ_ERROR:
741 case SCI_STP_DEV_AWAIT_RESET:
89a7301f 742 status = sci_port_start_io(iport, idev, ireq);
84b9b029
DW
743 if (status != SCI_SUCCESS)
744 return status;
745
89a7301f 746 status = sci_remote_node_context_start_task(&idev->rnc, ireq);
84b9b029
DW
747 if (status != SCI_SUCCESS)
748 goto out;
749
89a7301f 750 status = sci_request_start(ireq);
84b9b029
DW
751 if (status != SCI_SUCCESS)
752 goto out;
753
754 /* Note: If the remote device state is not IDLE this will
755 * replace the request that probably resulted in the task
756 * management request.
757 */
78a6f06e 758 idev->working_request = ireq;
e301370a 759 sci_change_state(sm, SCI_STP_DEV_CMD);
84b9b029
DW
760
761 /* The remote node context must cleanup the TCi to NCQ mapping
762 * table. The only way to do this correctly is to either write
763 * to the TLCR register or to invalidate and repost the RNC. In
764 * either case the remote node context state machine will take
765 * the correct action when the remote node context is suspended
766 * and later resumed.
767 */
89a7301f 768 sci_remote_node_context_suspend(&idev->rnc,
84b9b029 769 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
89a7301f
DW
770 sci_remote_node_context_resume(&idev->rnc,
771 sci_remote_device_continue_request,
78a6f06e 772 idev);
84b9b029
DW
773
774 out:
89a7301f 775 sci_remote_device_start_request(idev, ireq, status);
84b9b029
DW
776 /* We need to let the controller start request handler know that
777 * it can't post TC yet. We will provide a callback function to
778 * post TC when RNC gets resumed.
779 */
780 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
e301370a 781 case SCI_DEV_READY:
89a7301f 782 status = sci_port_start_io(iport, idev, ireq);
84b9b029
DW
783 if (status != SCI_SUCCESS)
784 return status;
785
89a7301f 786 status = sci_remote_node_context_start_task(&idev->rnc, ireq);
84b9b029
DW
787 if (status != SCI_SUCCESS)
788 break;
789
89a7301f 790 status = sci_request_start(ireq);
84b9b029
DW
791 break;
792 }
89a7301f 793 sci_remote_device_start_request(idev, ireq, status);
84b9b029
DW
794
795 return status;
88f3b62a
DW
796}
797
34a99158 798void sci_remote_device_post_request(struct isci_remote_device *idev, u32 request)
88f3b62a 799{
34a99158 800 struct isci_port *iport = idev->owning_port;
88f3b62a
DW
801 u32 context;
802
34a99158
DW
803 context = request |
804 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
805 (iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
806 idev->rnc.remote_node_index;
88f3b62a 807
34a99158 808 sci_controller_post_request(iport->owning_controller, context);
88f3b62a
DW
809}
810
ab2e8f7d 811/* called once the remote node context has transisitioned to a
88f3b62a 812 * ready state. This is the indication that the remote device object can also
ab2e8f7d 813 * transition to ready.
88f3b62a 814 */
eb229671 815static void remote_device_resume_done(void *_dev)
ab2e8f7d 816{
78a6f06e 817 struct isci_remote_device *idev = _dev;
ab2e8f7d 818
78a6f06e 819 if (is_remote_device_ready(idev))
e622571f 820 return;
88f3b62a 821
e622571f 822 /* go 'ready' if we are not already in a ready state */
78a6f06e 823 sci_change_state(&idev->sm, SCI_DEV_READY);
88f3b62a
DW
824}
825
89a7301f 826static void sci_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
ab2e8f7d 827{
78a6f06e 828 struct isci_remote_device *idev = _dev;
d9dcb4ba 829 struct isci_host *ihost = idev->owning_port->owning_controller;
ab2e8f7d
DW
830
831 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
832 * As a result, avoid sending the ready notification.
833 */
78a6f06e 834 if (idev->sm.previous_state_id != SCI_STP_DEV_NCQ)
d9dcb4ba 835 isci_remote_device_ready(ihost, idev);
ab2e8f7d
DW
836}
837
89a7301f 838static void sci_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
88f3b62a 839{
78a6f06e 840 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
88f3b62a 841
ab2e8f7d 842 /* Initial state is a transitional state to the stopped state */
78a6f06e 843 sci_change_state(&idev->sm, SCI_DEV_STOPPED);
88f3b62a 844}
6f231dda 845
88f3b62a 846/**
89a7301f 847 * sci_remote_device_destruct() - free remote node context and destruct
88f3b62a
DW
848 * @remote_device: This parameter specifies the remote device to be destructed.
849 *
850 * Remote device objects are a limited resource. As such, they must be
851 * protected. Thus calls to construct and destruct are mutually exclusive and
852 * non-reentrant. The return value shall indicate if the device was
853 * successfully destructed or if some failure occurred. enum sci_status This value
854 * is returned if the device is successfully destructed.
855 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
856 * device isn't valid (e.g. it's already been destoryed, the handle isn't
857 * valid, etc.).
858 */
89a7301f 859static enum sci_status sci_remote_device_destruct(struct isci_remote_device *idev)
88f3b62a 860{
78a6f06e 861 struct sci_base_state_machine *sm = &idev->sm;
89a7301f 862 enum sci_remote_device_states state = sm->current_state_id;
d9dcb4ba 863 struct isci_host *ihost;
b8d82f6c 864
e301370a 865 if (state != SCI_DEV_STOPPED) {
d7a0ccdd
DW
866 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
867 __func__, dev_state_name(state));
b8d82f6c
DW
868 return SCI_FAILURE_INVALID_STATE;
869 }
870
d9dcb4ba 871 ihost = idev->owning_port->owning_controller;
89a7301f 872 sci_controller_free_remote_node_context(ihost, idev,
78a6f06e
DW
873 idev->rnc.remote_node_index);
874 idev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
e301370a 875 sci_change_state(sm, SCI_DEV_FINAL);
b8d82f6c
DW
876
877 return SCI_SUCCESS;
88f3b62a 878}
6f231dda
DW
879
880/**
881 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
d9c37390
DW
882 * @ihost: This parameter specifies the isci host object.
883 * @idev: This parameter specifies the remote device to be freed.
6f231dda
DW
884 *
885 */
d9c37390 886static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
6f231dda 887{
d9c37390
DW
888 dev_dbg(&ihost->pdev->dev,
889 "%s: isci_device = %p\n", __func__, idev);
6f231dda
DW
890
891 /* There should not be any outstanding io's. All paths to
892 * here should go through isci_remote_device_nuke_requests.
893 * If we hit this condition, we will need a way to complete
894 * io requests in process */
209fae14 895 BUG_ON(!list_empty(&idev->reqs_in_process));
6f231dda 896
89a7301f 897 sci_remote_device_destruct(idev);
d9c37390 898 list_del_init(&idev->node);
209fae14 899 isci_put_device(idev);
6f231dda
DW
900}
901
89a7301f 902static void sci_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
88f3b62a 903{
78a6f06e 904 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
d9dcb4ba 905 struct isci_host *ihost = idev->owning_port->owning_controller;
88f3b62a
DW
906 u32 prev_state;
907
88f3b62a
DW
908 /* If we are entering from the stopping state let the SCI User know that
909 * the stop operation has completed.
910 */
78a6f06e 911 prev_state = idev->sm.previous_state_id;
e301370a 912 if (prev_state == SCI_DEV_STOPPING)
d9dcb4ba 913 isci_remote_device_deconstruct(ihost, idev);
88f3b62a 914
89a7301f 915 sci_controller_remote_device_stopped(ihost, idev);
88f3b62a
DW
916}
917
89a7301f 918static void sci_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
88f3b62a 919{
78a6f06e 920 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
34a99158 921 struct isci_host *ihost = idev->owning_port->owning_controller;
88f3b62a 922
88f3b62a
DW
923 isci_remote_device_not_ready(ihost, idev,
924 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
925}
926
89a7301f 927static void sci_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
88f3b62a 928{
78a6f06e 929 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
d9dcb4ba 930 struct isci_host *ihost = idev->owning_port->owning_controller;
cc3dbd0a 931 struct domain_device *dev = idev->domain_dev;
88f3b62a 932
ab2e8f7d 933 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
78a6f06e 934 sci_change_state(&idev->sm, SCI_STP_DEV_IDLE);
ab2e8f7d 935 } else if (dev_is_expander(dev)) {
78a6f06e 936 sci_change_state(&idev->sm, SCI_SMP_DEV_IDLE);
ab2e8f7d 937 } else
d9dcb4ba 938 isci_remote_device_ready(ihost, idev);
88f3b62a
DW
939}
940
89a7301f 941static void sci_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
88f3b62a 942{
78a6f06e
DW
943 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
944 struct domain_device *dev = idev->domain_dev;
ab2e8f7d
DW
945
946 if (dev->dev_type == SAS_END_DEV) {
d9dcb4ba 947 struct isci_host *ihost = idev->owning_port->owning_controller;
88f3b62a 948
d9dcb4ba 949 isci_remote_device_not_ready(ihost, idev,
88f3b62a
DW
950 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
951 }
952}
953
89a7301f 954static void sci_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
88f3b62a 955{
78a6f06e 956 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
88f3b62a 957
89a7301f 958 sci_remote_node_context_suspend(
78a6f06e 959 &idev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
88f3b62a
DW
960}
961
89a7301f 962static void sci_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
88f3b62a 963{
78a6f06e 964 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
88f3b62a 965
89a7301f 966 sci_remote_node_context_resume(&idev->rnc, NULL, NULL);
88f3b62a
DW
967}
968
89a7301f 969static void sci_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
ab2e8f7d 970{
78a6f06e 971 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
ab2e8f7d 972
78a6f06e 973 idev->working_request = NULL;
89a7301f 974 if (sci_remote_node_context_is_ready(&idev->rnc)) {
ab2e8f7d
DW
975 /*
976 * Since the RNC is ready, it's alright to finish completion
977 * processing (e.g. signal the remote device is ready). */
89a7301f 978 sci_stp_remote_device_ready_idle_substate_resume_complete_handler(idev);
ab2e8f7d 979 } else {
89a7301f
DW
980 sci_remote_node_context_resume(&idev->rnc,
981 sci_stp_remote_device_ready_idle_substate_resume_complete_handler,
78a6f06e 982 idev);
ab2e8f7d
DW
983 }
984}
985
89a7301f 986static void sci_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
ab2e8f7d 987{
78a6f06e 988 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
34a99158 989 struct isci_host *ihost = idev->owning_port->owning_controller;
ab2e8f7d 990
78a6f06e 991 BUG_ON(idev->working_request == NULL);
ab2e8f7d 992
d9dcb4ba 993 isci_remote_device_not_ready(ihost, idev,
ab2e8f7d
DW
994 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
995}
996
89a7301f 997static void sci_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
ab2e8f7d 998{
78a6f06e 999 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
34a99158 1000 struct isci_host *ihost = idev->owning_port->owning_controller;
ab2e8f7d 1001
78a6f06e 1002 if (idev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
d9dcb4ba 1003 isci_remote_device_not_ready(ihost, idev,
78a6f06e 1004 idev->not_ready_reason);
ab2e8f7d
DW
1005}
1006
89a7301f 1007static void sci_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
ab2e8f7d 1008{
78a6f06e 1009 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
34a99158 1010 struct isci_host *ihost = idev->owning_port->owning_controller;
ab2e8f7d 1011
d9dcb4ba 1012 isci_remote_device_ready(ihost, idev);
ab2e8f7d
DW
1013}
1014
89a7301f 1015static void sci_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
ab2e8f7d 1016{
78a6f06e 1017 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
34a99158 1018 struct isci_host *ihost = idev->owning_port->owning_controller;
ab2e8f7d 1019
78a6f06e 1020 BUG_ON(idev->working_request == NULL);
ab2e8f7d 1021
d9dcb4ba 1022 isci_remote_device_not_ready(ihost, idev,
ab2e8f7d
DW
1023 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1024}
1025
89a7301f 1026static void sci_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
ab2e8f7d 1027{
78a6f06e 1028 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
ab2e8f7d 1029
78a6f06e 1030 idev->working_request = NULL;
ab2e8f7d 1031}
88f3b62a 1032
89a7301f 1033static const struct sci_base_state sci_remote_device_state_table[] = {
e301370a 1034 [SCI_DEV_INITIAL] = {
89a7301f 1035 .enter_state = sci_remote_device_initial_state_enter,
88f3b62a 1036 },
e301370a 1037 [SCI_DEV_STOPPED] = {
89a7301f 1038 .enter_state = sci_remote_device_stopped_state_enter,
88f3b62a 1039 },
e301370a 1040 [SCI_DEV_STARTING] = {
89a7301f 1041 .enter_state = sci_remote_device_starting_state_enter,
88f3b62a 1042 },
e301370a 1043 [SCI_DEV_READY] = {
89a7301f
DW
1044 .enter_state = sci_remote_device_ready_state_enter,
1045 .exit_state = sci_remote_device_ready_state_exit
88f3b62a 1046 },
e301370a 1047 [SCI_STP_DEV_IDLE] = {
89a7301f 1048 .enter_state = sci_stp_remote_device_ready_idle_substate_enter,
ab2e8f7d 1049 },
e301370a 1050 [SCI_STP_DEV_CMD] = {
89a7301f 1051 .enter_state = sci_stp_remote_device_ready_cmd_substate_enter,
ab2e8f7d 1052 },
e301370a
EN
1053 [SCI_STP_DEV_NCQ] = { },
1054 [SCI_STP_DEV_NCQ_ERROR] = {
89a7301f 1055 .enter_state = sci_stp_remote_device_ready_ncq_error_substate_enter,
ab2e8f7d 1056 },
b50102d3 1057 [SCI_STP_DEV_ATAPI_ERROR] = { },
e301370a
EN
1058 [SCI_STP_DEV_AWAIT_RESET] = { },
1059 [SCI_SMP_DEV_IDLE] = {
89a7301f 1060 .enter_state = sci_smp_remote_device_ready_idle_substate_enter,
ab2e8f7d 1061 },
e301370a 1062 [SCI_SMP_DEV_CMD] = {
89a7301f
DW
1063 .enter_state = sci_smp_remote_device_ready_cmd_substate_enter,
1064 .exit_state = sci_smp_remote_device_ready_cmd_substate_exit,
ab2e8f7d 1065 },
e301370a
EN
1066 [SCI_DEV_STOPPING] = { },
1067 [SCI_DEV_FAILED] = { },
1068 [SCI_DEV_RESETTING] = {
89a7301f
DW
1069 .enter_state = sci_remote_device_resetting_state_enter,
1070 .exit_state = sci_remote_device_resetting_state_exit
88f3b62a 1071 },
e301370a 1072 [SCI_DEV_FINAL] = { },
88f3b62a
DW
1073};
1074
1075/**
89a7301f 1076 * sci_remote_device_construct() - common construction
88f3b62a
DW
1077 * @sci_port: SAS/SATA port through which this device is accessed.
1078 * @sci_dev: remote device to construct
1079 *
b87ee307
DW
1080 * This routine just performs benign initialization and does not
1081 * allocate the remote_node_context which is left to
89a7301f 1082 * sci_remote_device_[de]a_construct(). sci_remote_device_destruct()
b87ee307 1083 * frees the remote_node_context(s) for the device.
88f3b62a 1084 */
89a7301f 1085static void sci_remote_device_construct(struct isci_port *iport,
78a6f06e 1086 struct isci_remote_device *idev)
88f3b62a 1087{
78a6f06e
DW
1088 idev->owning_port = iport;
1089 idev->started_request_count = 0;
88f3b62a 1090
89a7301f 1091 sci_init_sm(&idev->sm, sci_remote_device_state_table, SCI_DEV_INITIAL);
88f3b62a 1092
89a7301f 1093 sci_remote_node_context_construct(&idev->rnc,
88f3b62a 1094 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
88f3b62a
DW
1095}
1096
1097/**
89a7301f 1098 * sci_remote_device_da_construct() - construct direct attached device.
b87ee307
DW
1099 *
1100 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1101 * the device is known to the SCI Core since it is contained in the
89a7301f
DW
1102 * sci_phy object. Remote node context(s) is/are a global resource
1103 * allocated by this routine, freed by sci_remote_device_destruct().
b87ee307
DW
1104 *
1105 * Returns:
1106 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1107 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1108 * sata-only controller instance.
1109 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
88f3b62a 1110 */
89a7301f 1111static enum sci_status sci_remote_device_da_construct(struct isci_port *iport,
78a6f06e 1112 struct isci_remote_device *idev)
88f3b62a
DW
1113{
1114 enum sci_status status;
7e629841 1115 struct sci_port_properties properties;
78a6f06e 1116 struct domain_device *dev = idev->domain_dev;
88f3b62a 1117
89a7301f 1118 sci_remote_device_construct(iport, idev);
b87ee307 1119
88f3b62a
DW
1120 /*
1121 * This information is request to determine how many remote node context
1122 * entries will be needed to store the remote node.
1123 */
78a6f06e 1124 idev->is_direct_attached = true;
7e629841
BN
1125
1126 sci_port_get_properties(iport, &properties);
1127 /* Get accurate port width from port's phy mask for a DA device. */
1128 idev->device_port_width = hweight32(properties.phy_mask);
1129
89a7301f 1130 status = sci_controller_allocate_remote_node_context(iport->owning_controller,
78a6f06e
DW
1131 idev,
1132 &idev->rnc.remote_node_index);
88f3b62a 1133
a1a113b0
DW
1134 if (status != SCI_SUCCESS)
1135 return status;
88f3b62a 1136
ab2e8f7d
DW
1137 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1138 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1139 /* pass */;
1140 else
a1a113b0 1141 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
88f3b62a 1142
89a7301f 1143 idev->connection_rate = sci_port_get_max_allowed_speed(iport);
88f3b62a 1144
a1a113b0 1145 return SCI_SUCCESS;
88f3b62a
DW
1146}
1147
88f3b62a 1148/**
89a7301f 1149 * sci_remote_device_ea_construct() - construct expander attached device
b87ee307
DW
1150 *
1151 * Remote node context(s) is/are a global resource allocated by this
89a7301f 1152 * routine, freed by sci_remote_device_destruct().
b87ee307
DW
1153 *
1154 * Returns:
1155 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1156 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1157 * sata-only controller instance.
1158 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
88f3b62a 1159 */
89a7301f 1160static enum sci_status sci_remote_device_ea_construct(struct isci_port *iport,
78a6f06e 1161 struct isci_remote_device *idev)
88f3b62a 1162{
78a6f06e 1163 struct domain_device *dev = idev->domain_dev;
88f3b62a 1164 enum sci_status status;
88f3b62a 1165
89a7301f 1166 sci_remote_device_construct(iport, idev);
88f3b62a 1167
89a7301f 1168 status = sci_controller_allocate_remote_node_context(iport->owning_controller,
78a6f06e
DW
1169 idev,
1170 &idev->rnc.remote_node_index);
a1a113b0
DW
1171 if (status != SCI_SUCCESS)
1172 return status;
88f3b62a 1173
ab2e8f7d
DW
1174 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1175 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1176 /* pass */;
1177 else
1178 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
88f3b62a 1179
a1a113b0
DW
1180 /*
1181 * For SAS-2 the physical link rate is actually a logical link
1182 * rate that incorporates multiplexing. The SCU doesn't
1183 * incorporate multiplexing and for the purposes of the
1184 * connection the logical link rate is that same as the
1185 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1186 * one another, so this code works for both situations. */
89a7301f 1187 idev->connection_rate = min_t(u16, sci_port_get_max_allowed_speed(iport),
00d680ef 1188 dev->linkrate);
88f3b62a 1189
a1a113b0 1190 /* / @todo Should I assign the port width by reading all of the phys on the port? */
78a6f06e 1191 idev->device_port_width = 1;
88f3b62a 1192
ab2e8f7d 1193 return SCI_SUCCESS;
88f3b62a
DW
1194}
1195
1196/**
89a7301f 1197 * sci_remote_device_start() - This method will start the supplied remote
88f3b62a
DW
1198 * device. This method enables normal IO requests to flow through to the
1199 * remote device.
1200 * @remote_device: This parameter specifies the device to be started.
1201 * @timeout: This parameter specifies the number of milliseconds in which the
1202 * start operation should complete.
1203 *
1204 * An indication of whether the device was successfully started. SCI_SUCCESS
1205 * This value is returned if the device was successfully started.
1206 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1207 * the device when there have been no phys added to it.
1208 */
89a7301f 1209static enum sci_status sci_remote_device_start(struct isci_remote_device *idev,
eb229671 1210 u32 timeout)
88f3b62a 1211{
78a6f06e 1212 struct sci_base_state_machine *sm = &idev->sm;
89a7301f 1213 enum sci_remote_device_states state = sm->current_state_id;
eb229671
DW
1214 enum sci_status status;
1215
e301370a 1216 if (state != SCI_DEV_STOPPED) {
d7a0ccdd
DW
1217 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
1218 __func__, dev_state_name(state));
eb229671
DW
1219 return SCI_FAILURE_INVALID_STATE;
1220 }
1221
89a7301f 1222 status = sci_remote_node_context_resume(&idev->rnc,
eb229671 1223 remote_device_resume_done,
78a6f06e 1224 idev);
eb229671
DW
1225 if (status != SCI_SUCCESS)
1226 return status;
1227
e301370a 1228 sci_change_state(sm, SCI_DEV_STARTING);
eb229671
DW
1229
1230 return SCI_SUCCESS;
88f3b62a 1231}
6f231dda 1232
00d680ef
DW
1233static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1234 struct isci_remote_device *idev)
6f231dda 1235{
00d680ef
DW
1236 struct isci_host *ihost = iport->isci_host;
1237 struct domain_device *dev = idev->domain_dev;
1238 enum sci_status status;
6f231dda 1239
00d680ef 1240 if (dev->parent && dev_is_expander(dev->parent))
89a7301f 1241 status = sci_remote_device_ea_construct(iport, idev);
00d680ef 1242 else
89a7301f 1243 status = sci_remote_device_da_construct(iport, idev);
6f231dda
DW
1244
1245 if (status != SCI_SUCCESS) {
00d680ef
DW
1246 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1247 __func__, status);
6f231dda
DW
1248
1249 return status;
1250 }
1251
6f231dda 1252 /* start the device. */
89a7301f 1253 status = sci_remote_device_start(idev, ISCI_REMOTE_DEVICE_START_TIMEOUT);
6f231dda 1254
00d680ef
DW
1255 if (status != SCI_SUCCESS)
1256 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1257 status);
6f231dda
DW
1258
1259 return status;
1260}
1261
4393aa4e 1262void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
6f231dda
DW
1263{
1264 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
6f231dda 1265
4393aa4e
DW
1266 dev_dbg(&ihost->pdev->dev,
1267 "%s: idev = %p\n", __func__, idev);
6f231dda
DW
1268
1269 /* Cleanup all requests pending for this device. */
980d3aeb 1270 isci_terminate_pending_requests(ihost, idev);
6f231dda 1271
4393aa4e
DW
1272 dev_dbg(&ihost->pdev->dev,
1273 "%s: idev = %p, done\n", __func__, idev);
6f231dda
DW
1274}
1275
6f231dda
DW
1276/**
1277 * This function builds the isci_remote_device when a libsas dev_found message
1278 * is received.
1279 * @isci_host: This parameter specifies the isci host object.
1280 * @port: This parameter specifies the isci_port conected to this device.
1281 *
1282 * pointer to new isci_remote_device.
1283 */
1284static struct isci_remote_device *
d9c37390 1285isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
6f231dda 1286{
d9c37390
DW
1287 struct isci_remote_device *idev;
1288 int i;
6f231dda 1289
d9c37390 1290 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
57f20f4e 1291 idev = &ihost->devices[i];
d9c37390
DW
1292 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1293 break;
1294 }
6f231dda 1295
d9c37390
DW
1296 if (i >= SCI_MAX_REMOTE_DEVICES) {
1297 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
6f231dda
DW
1298 return NULL;
1299 }
1300
6cb4d6b3
BB
1301 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1302 return NULL;
1303
1304 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1305 return NULL;
1306
d9c37390 1307 return idev;
6f231dda 1308}
6f231dda 1309
209fae14
DW
1310void isci_remote_device_release(struct kref *kref)
1311{
1312 struct isci_remote_device *idev = container_of(kref, typeof(*idev), kref);
1313 struct isci_host *ihost = idev->isci_port->isci_host;
1314
1315 idev->domain_dev = NULL;
1316 idev->isci_port = NULL;
1317 clear_bit(IDEV_START_PENDING, &idev->flags);
1318 clear_bit(IDEV_STOP_PENDING, &idev->flags);
f2088267 1319 clear_bit(IDEV_IO_READY, &idev->flags);
209fae14 1320 clear_bit(IDEV_GONE, &idev->flags);
209fae14
DW
1321 smp_mb__before_clear_bit();
1322 clear_bit(IDEV_ALLOCATED, &idev->flags);
1323 wake_up(&ihost->eventq);
1324}
1325
6f231dda
DW
1326/**
1327 * isci_remote_device_stop() - This function is called internally to stop the
1328 * remote device.
1329 * @isci_host: This parameter specifies the isci host object.
1330 * @isci_device: This parameter specifies the remote device.
1331 *
d9dcb4ba 1332 * The status of the ihost request to stop.
6f231dda 1333 */
6ad31fec 1334enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
6f231dda
DW
1335{
1336 enum sci_status status;
1337 unsigned long flags;
6f231dda 1338
6ad31fec
DW
1339 dev_dbg(&ihost->pdev->dev,
1340 "%s: isci_device = %p\n", __func__, idev);
6f231dda 1341
209fae14
DW
1342 spin_lock_irqsave(&ihost->scic_lock, flags);
1343 idev->domain_dev->lldd_dev = NULL; /* disable new lookups */
1344 set_bit(IDEV_GONE, &idev->flags);
209fae14 1345 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6e2802a7
JS
1346
1347 /* Kill all outstanding requests. */
4393aa4e 1348 isci_remote_device_nuke_requests(ihost, idev);
6e2802a7 1349
6ad31fec 1350 set_bit(IDEV_STOP_PENDING, &idev->flags);
6f231dda 1351
6ad31fec 1352 spin_lock_irqsave(&ihost->scic_lock, flags);
89a7301f 1353 status = sci_remote_device_stop(idev, 50);
6ad31fec 1354 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda
DW
1355
1356 /* Wait for the stop complete callback. */
209fae14
DW
1357 if (WARN_ONCE(status != SCI_SUCCESS, "failed to stop device\n"))
1358 /* nothing to wait for */;
1359 else
6ad31fec 1360 wait_for_device_stop(ihost, idev);
6f231dda 1361
6f231dda
DW
1362 return status;
1363}
1364
1365/**
1366 * isci_remote_device_gone() - This function is called by libsas when a domain
1367 * device is removed.
1368 * @domain_device: This parameter specifies the libsas domain device.
1369 *
1370 */
6ad31fec 1371void isci_remote_device_gone(struct domain_device *dev)
6f231dda 1372{
4393aa4e 1373 struct isci_host *ihost = dev_to_ihost(dev);
6ad31fec 1374 struct isci_remote_device *idev = dev->lldd_dev;
6f231dda 1375
6ad31fec 1376 dev_dbg(&ihost->pdev->dev,
6f231dda 1377 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
6ad31fec 1378 __func__, dev, idev, idev->isci_port);
6f231dda 1379
6ad31fec 1380 isci_remote_device_stop(ihost, idev);
6f231dda
DW
1381}
1382
1383
1384/**
1385 * isci_remote_device_found() - This function is called by libsas when a remote
1386 * device is discovered. A remote device object is created and started. the
1387 * function then sleeps until the sci core device started message is
1388 * received.
1389 * @domain_device: This parameter specifies the libsas domain device.
1390 *
1391 * status, zero indicates success.
1392 */
c132f692 1393int isci_remote_device_found(struct domain_device *dev)
6f231dda 1394{
c132f692
DW
1395 struct isci_host *isci_host = dev_to_ihost(dev);
1396 struct isci_port *isci_port = dev->port->lldd_port;
6f231dda
DW
1397 struct isci_remote_device *isci_device;
1398 enum sci_status status;
6f231dda 1399
6f231dda 1400 dev_dbg(&isci_host->pdev->dev,
c132f692 1401 "%s: domain_device = %p\n", __func__, dev);
6f231dda 1402
c132f692
DW
1403 if (!isci_port)
1404 return -ENODEV;
6f231dda 1405
6f231dda 1406 isci_device = isci_remote_device_alloc(isci_host, isci_port);
d9c37390
DW
1407 if (!isci_device)
1408 return -ENODEV;
6f231dda 1409
209fae14 1410 kref_init(&isci_device->kref);
6f231dda 1411 INIT_LIST_HEAD(&isci_device->node);
209fae14
DW
1412
1413 spin_lock_irq(&isci_host->scic_lock);
c132f692 1414 isci_device->domain_dev = dev;
6f231dda 1415 isci_device->isci_port = isci_port;
6f231dda
DW
1416 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1417
6ad31fec 1418 set_bit(IDEV_START_PENDING, &isci_device->flags);
6f231dda 1419 status = isci_remote_device_construct(isci_port, isci_device);
6f231dda 1420
6f231dda
DW
1421 dev_dbg(&isci_host->pdev->dev,
1422 "%s: isci_device = %p\n",
1423 __func__, isci_device);
1424
209fae14
DW
1425 if (status == SCI_SUCCESS) {
1426 /* device came up, advertise it to the world */
c132f692 1427 dev->lldd_dev = isci_device;
209fae14
DW
1428 } else
1429 isci_put_device(isci_device);
1430 spin_unlock_irq(&isci_host->scic_lock);
6f231dda 1431
6ad31fec
DW
1432 /* wait for the device ready callback. */
1433 wait_for_device_start(isci_host, isci_device);
1434
209fae14 1435 return status == SCI_SUCCESS ? 0 : -ENODEV;
6f231dda 1436}
This page took 0.199125 seconds and 5 git commands to generate.