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