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