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