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 | */ | |
55 | ||
56 | #include <linux/completion.h> | |
50e7f9b5 | 57 | #include <linux/irqflags.h> |
af5ae893 | 58 | #include "sas.h" |
61aaff49 | 59 | #include <scsi/libsas.h> |
88f3b62a DW |
60 | #include "remote_device.h" |
61 | #include "remote_node_context.h" | |
6f231dda DW |
62 | #include "isci.h" |
63 | #include "request.h" | |
64 | #include "sata.h" | |
65 | #include "task.h" | |
af5ae893 | 66 | |
50e7f9b5 | 67 | /** |
1077a574 DW |
68 | * isci_task_refuse() - complete the request to the upper layer driver in |
69 | * the case where an I/O needs to be completed back in the submit path. | |
70 | * @ihost: host on which the the request was queued | |
71 | * @task: request to complete | |
72 | * @response: response code for the completed task. | |
73 | * @status: status code for the completed task. | |
50e7f9b5 | 74 | * |
50e7f9b5 | 75 | */ |
1077a574 DW |
76 | static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task, |
77 | enum service_response response, | |
78 | enum exec_status status) | |
79 | ||
50e7f9b5 | 80 | { |
1077a574 | 81 | enum isci_completion_selection disposition; |
50e7f9b5 | 82 | |
1077a574 DW |
83 | disposition = isci_perform_normal_io_completion; |
84 | disposition = isci_task_set_completion_status(task, response, status, | |
85 | disposition); | |
50e7f9b5 DW |
86 | |
87 | /* Tasks aborted specifically by a call to the lldd_abort_task | |
1077a574 DW |
88 | * function should not be completed to the host in the regular path. |
89 | */ | |
90 | switch (disposition) { | |
50e7f9b5 DW |
91 | case isci_perform_normal_io_completion: |
92 | /* Normal notification (task_done) */ | |
1077a574 | 93 | dev_dbg(&ihost->pdev->dev, |
ed8a72d1 JS |
94 | "%s: Normal - task = %p, response=%d, " |
95 | "status=%d\n", | |
50e7f9b5 DW |
96 | __func__, task, response, status); |
97 | ||
1077a574 | 98 | task->lldd_task = NULL; |
50e7f9b5 | 99 | |
ed8a72d1 | 100 | isci_execpath_callback(ihost, task, task->task_done); |
50e7f9b5 DW |
101 | break; |
102 | ||
103 | case isci_perform_aborted_io_completion: | |
104 | /* No notification because this request is already in the | |
105 | * abort path. | |
106 | */ | |
1077a574 | 107 | dev_warn(&ihost->pdev->dev, |
ed8a72d1 JS |
108 | "%s: Aborted - task = %p, response=%d, " |
109 | "status=%d\n", | |
50e7f9b5 DW |
110 | __func__, task, response, status); |
111 | break; | |
112 | ||
113 | case isci_perform_error_io_completion: | |
114 | /* Use sas_task_abort */ | |
1077a574 | 115 | dev_warn(&ihost->pdev->dev, |
ed8a72d1 JS |
116 | "%s: Error - task = %p, response=%d, " |
117 | "status=%d\n", | |
50e7f9b5 | 118 | __func__, task, response, status); |
ed8a72d1 JS |
119 | |
120 | isci_execpath_callback(ihost, task, sas_task_abort); | |
50e7f9b5 DW |
121 | break; |
122 | ||
123 | default: | |
1077a574 | 124 | dev_warn(&ihost->pdev->dev, |
50e7f9b5 DW |
125 | "%s: isci task notification default case!", |
126 | __func__); | |
127 | sas_task_abort(task); | |
128 | break; | |
129 | } | |
130 | } | |
6f231dda | 131 | |
1077a574 DW |
132 | #define for_each_sas_task(num, task) \ |
133 | for (; num > 0; num--,\ | |
134 | task = list_entry(task->list.next, struct sas_task, list)) | |
135 | ||
9274f45e JS |
136 | |
137 | static inline int isci_device_io_ready(struct isci_remote_device *idev, | |
138 | struct sas_task *task) | |
139 | { | |
140 | return idev ? test_bit(IDEV_IO_READY, &idev->flags) || | |
141 | (test_bit(IDEV_IO_NCQERROR, &idev->flags) && | |
142 | isci_task_is_ncq_recovery(task)) | |
143 | : 0; | |
144 | } | |
6f231dda DW |
145 | /** |
146 | * isci_task_execute_task() - This function is one of the SAS Domain Template | |
147 | * functions. This function is called by libsas to send a task down to | |
148 | * hardware. | |
149 | * @task: This parameter specifies the SAS task to send. | |
150 | * @num: This parameter specifies the number of tasks to queue. | |
151 | * @gfp_flags: This parameter specifies the context of this call. | |
152 | * | |
153 | * status, zero indicates success. | |
154 | */ | |
155 | int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags) | |
156 | { | |
4393aa4e | 157 | struct isci_host *ihost = dev_to_ihost(task->dev); |
209fae14 | 158 | struct isci_remote_device *idev; |
f2088267 | 159 | enum sci_status status; |
6f231dda | 160 | unsigned long flags; |
f2088267 | 161 | bool io_ready; |
6f231dda | 162 | int ret; |
6f231dda | 163 | |
1077a574 | 164 | dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num); |
6f231dda DW |
165 | |
166 | /* Check if we have room for more tasks */ | |
1077a574 | 167 | ret = isci_host_can_queue(ihost, num); |
6f231dda DW |
168 | |
169 | if (ret) { | |
1077a574 | 170 | dev_warn(&ihost->pdev->dev, "%s: queue full\n", __func__); |
6f231dda DW |
171 | return ret; |
172 | } | |
173 | ||
1077a574 | 174 | for_each_sas_task(num, task) { |
209fae14 DW |
175 | spin_lock_irqsave(&ihost->scic_lock, flags); |
176 | idev = isci_lookup_device(task->dev); | |
9274f45e | 177 | io_ready = isci_device_io_ready(idev, task); |
209fae14 | 178 | spin_unlock_irqrestore(&ihost->scic_lock, flags); |
6f231dda | 179 | |
f2088267 DW |
180 | dev_dbg(&ihost->pdev->dev, |
181 | "task: %p, num: %d dev: %p idev: %p:%#lx cmd = %p\n", | |
182 | task, num, task->dev, idev, idev ? idev->flags : 0, | |
183 | task->uldd_task); | |
6f231dda | 184 | |
f2088267 DW |
185 | if (!idev) { |
186 | isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED, | |
187 | SAS_DEVICE_UNKNOWN); | |
188 | isci_host_can_dequeue(ihost, 1); | |
189 | } else if (!io_ready) { | |
9274f45e | 190 | |
f2088267 DW |
191 | /* Indicate QUEUE_FULL so that the scsi midlayer |
192 | * retries. | |
193 | */ | |
194 | isci_task_refuse(ihost, task, SAS_TASK_COMPLETE, | |
195 | SAS_QUEUE_FULL); | |
1077a574 | 196 | isci_host_can_dequeue(ihost, 1); |
f0846c68 JS |
197 | } else { |
198 | /* There is a device and it's ready for I/O. */ | |
199 | spin_lock_irqsave(&task->task_state_lock, flags); | |
6f231dda | 200 | |
f0846c68 | 201 | if (task->task_state_flags & SAS_TASK_STATE_ABORTED) { |
f2088267 | 202 | /* The I/O was aborted. */ |
f0846c68 JS |
203 | spin_unlock_irqrestore(&task->task_state_lock, |
204 | flags); | |
6f231dda | 205 | |
1077a574 DW |
206 | isci_task_refuse(ihost, task, |
207 | SAS_TASK_UNDELIVERED, | |
208 | SAM_STAT_TASK_ABORTED); | |
f2088267 | 209 | isci_host_can_dequeue(ihost, 1); |
f0846c68 | 210 | } else { |
6f231dda DW |
211 | task->task_state_flags |= SAS_TASK_AT_INITIATOR; |
212 | spin_unlock_irqrestore(&task->task_state_lock, flags); | |
f0846c68 JS |
213 | |
214 | /* build and send the request. */ | |
209fae14 | 215 | status = isci_request_execute(ihost, idev, task, gfp_flags); |
f0846c68 JS |
216 | |
217 | if (status != SCI_SUCCESS) { | |
218 | ||
219 | spin_lock_irqsave(&task->task_state_lock, flags); | |
220 | /* Did not really start this command. */ | |
221 | task->task_state_flags &= ~SAS_TASK_AT_INITIATOR; | |
222 | spin_unlock_irqrestore(&task->task_state_lock, flags); | |
223 | ||
224 | /* Indicate QUEUE_FULL so that the scsi | |
225 | * midlayer retries. if the request | |
226 | * failed for remote device reasons, | |
227 | * it gets returned as | |
228 | * SAS_TASK_UNDELIVERED next time | |
229 | * through. | |
230 | */ | |
1077a574 DW |
231 | isci_task_refuse(ihost, task, |
232 | SAS_TASK_COMPLETE, | |
233 | SAS_QUEUE_FULL); | |
234 | isci_host_can_dequeue(ihost, 1); | |
f0846c68 | 235 | } |
6f231dda DW |
236 | } |
237 | } | |
209fae14 | 238 | isci_put_device(idev); |
1077a574 | 239 | } |
6f231dda DW |
240 | return 0; |
241 | } | |
242 | ||
0d0cf14c | 243 | static struct isci_request *isci_task_request_build(struct isci_host *ihost, |
209fae14 | 244 | struct isci_remote_device *idev, |
0d0cf14c | 245 | struct isci_tmf *isci_tmf) |
6f231dda | 246 | { |
6f231dda | 247 | enum sci_status status = SCI_FAILURE; |
0d0cf14c | 248 | struct isci_request *ireq = NULL; |
a1a113b0 | 249 | struct domain_device *dev; |
6f231dda | 250 | |
0d0cf14c | 251 | dev_dbg(&ihost->pdev->dev, |
6f231dda DW |
252 | "%s: isci_tmf = %p\n", __func__, isci_tmf); |
253 | ||
0d0cf14c | 254 | dev = idev->domain_dev; |
6f231dda DW |
255 | |
256 | /* do common allocation and init of request object. */ | |
209fae14 | 257 | ireq = isci_request_alloc_tmf(ihost, isci_tmf, GFP_ATOMIC); |
0d0cf14c DW |
258 | if (!ireq) |
259 | return NULL; | |
6f231dda DW |
260 | |
261 | /* let the core do it's construct. */ | |
209fae14 | 262 | status = scic_task_request_construct(&ihost->sci, &idev->sci, |
0d84366f | 263 | SCI_CONTROLLER_INVALID_IO_TAG, |
0d0cf14c | 264 | &ireq->sci); |
6f231dda DW |
265 | |
266 | if (status != SCI_SUCCESS) { | |
0d0cf14c | 267 | dev_warn(&ihost->pdev->dev, |
6f231dda DW |
268 | "%s: scic_task_request_construct failed - " |
269 | "status = 0x%x\n", | |
270 | __func__, | |
271 | status); | |
272 | goto errout; | |
273 | } | |
274 | ||
a1a113b0 DW |
275 | /* XXX convert to get this from task->tproto like other drivers */ |
276 | if (dev->dev_type == SAS_END_DEV) { | |
6f231dda | 277 | isci_tmf->proto = SAS_PROTOCOL_SSP; |
0d0cf14c | 278 | status = scic_task_request_construct_ssp(&ireq->sci); |
6f231dda DW |
279 | if (status != SCI_SUCCESS) |
280 | goto errout; | |
281 | } | |
282 | ||
a1a113b0 | 283 | if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) { |
6f231dda | 284 | isci_tmf->proto = SAS_PROTOCOL_SATA; |
0d0cf14c | 285 | status = isci_sata_management_task_request_build(ireq); |
6f231dda DW |
286 | |
287 | if (status != SCI_SUCCESS) | |
288 | goto errout; | |
289 | } | |
0d0cf14c | 290 | return ireq; |
6f231dda | 291 | errout: |
0d0cf14c DW |
292 | isci_request_free(ihost, ireq); |
293 | ireq = NULL; | |
294 | return ireq; | |
6f231dda DW |
295 | } |
296 | ||
209fae14 DW |
297 | int isci_task_execute_tmf(struct isci_host *ihost, |
298 | struct isci_remote_device *isci_device, | |
299 | struct isci_tmf *tmf, unsigned long timeout_ms) | |
6f231dda DW |
300 | { |
301 | DECLARE_COMPLETION_ONSTACK(completion); | |
467e855a | 302 | enum sci_task_status status = SCI_TASK_FAILURE; |
6f231dda | 303 | struct scic_sds_remote_device *sci_device; |
0d0cf14c | 304 | struct isci_request *ireq; |
6f231dda DW |
305 | int ret = TMF_RESP_FUNC_FAILED; |
306 | unsigned long flags; | |
fd18388b | 307 | unsigned long timeleft; |
6f231dda DW |
308 | |
309 | /* sanity check, return TMF_RESP_FUNC_FAILED | |
310 | * if the device is not there and ready. | |
311 | */ | |
9274f45e JS |
312 | if (!isci_device || |
313 | (!test_bit(IDEV_IO_READY, &isci_device->flags) && | |
314 | !test_bit(IDEV_IO_NCQERROR, &isci_device->flags))) { | |
0d0cf14c | 315 | dev_dbg(&ihost->pdev->dev, |
f2088267 | 316 | "%s: isci_device = %p not ready (%#lx)\n", |
6f231dda | 317 | __func__, |
f2088267 | 318 | isci_device, isci_device ? isci_device->flags : 0); |
6f231dda DW |
319 | return TMF_RESP_FUNC_FAILED; |
320 | } else | |
0d0cf14c | 321 | dev_dbg(&ihost->pdev->dev, |
6f231dda DW |
322 | "%s: isci_device = %p\n", |
323 | __func__, isci_device); | |
324 | ||
57f20f4e | 325 | sci_device = &isci_device->sci; |
6f231dda DW |
326 | |
327 | /* Assign the pointer to the TMF's completion kernel wait structure. */ | |
328 | tmf->complete = &completion; | |
329 | ||
209fae14 | 330 | ireq = isci_task_request_build(ihost, isci_device, tmf); |
0d0cf14c DW |
331 | if (!ireq) { |
332 | dev_warn(&ihost->pdev->dev, | |
6f231dda DW |
333 | "%s: isci_task_request_build failed\n", |
334 | __func__); | |
335 | return TMF_RESP_FUNC_FAILED; | |
336 | } | |
337 | ||
0d0cf14c | 338 | spin_lock_irqsave(&ihost->scic_lock, flags); |
6f231dda DW |
339 | |
340 | /* start the TMF io. */ | |
341 | status = scic_controller_start_task( | |
0d0cf14c | 342 | &ihost->sci, |
6f231dda | 343 | sci_device, |
0d0cf14c | 344 | &ireq->sci, |
67ea838d | 345 | SCI_CONTROLLER_INVALID_IO_TAG); |
6f231dda | 346 | |
467e855a | 347 | if (status != SCI_TASK_SUCCESS) { |
0d0cf14c | 348 | dev_warn(&ihost->pdev->dev, |
6f231dda DW |
349 | "%s: start_io failed - status = 0x%x, request = %p\n", |
350 | __func__, | |
351 | status, | |
0d0cf14c DW |
352 | ireq); |
353 | spin_unlock_irqrestore(&ihost->scic_lock, flags); | |
086a0dab DW |
354 | isci_request_free(ihost, ireq); |
355 | return ret; | |
6f231dda DW |
356 | } |
357 | ||
6f231dda DW |
358 | if (tmf->cb_state_func != NULL) |
359 | tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data); | |
360 | ||
0d0cf14c | 361 | isci_request_change_state(ireq, started); |
6f231dda DW |
362 | |
363 | /* add the request to the remote device request list. */ | |
0d0cf14c | 364 | list_add(&ireq->dev_node, &isci_device->reqs_in_process); |
6f231dda | 365 | |
0d0cf14c | 366 | spin_unlock_irqrestore(&ihost->scic_lock, flags); |
6f231dda DW |
367 | |
368 | /* Wait for the TMF to complete, or a timeout. */ | |
fd18388b | 369 | timeleft = wait_for_completion_timeout(&completion, |
086a0dab | 370 | msecs_to_jiffies(timeout_ms)); |
fd18388b EN |
371 | |
372 | if (timeleft == 0) { | |
0d0cf14c | 373 | spin_lock_irqsave(&ihost->scic_lock, flags); |
fd18388b EN |
374 | |
375 | if (tmf->cb_state_func != NULL) | |
376 | tmf->cb_state_func(isci_tmf_timed_out, tmf, tmf->cb_data); | |
377 | ||
086a0dab DW |
378 | scic_controller_terminate_request(&ihost->sci, |
379 | &isci_device->sci, | |
380 | &ireq->sci); | |
fd18388b | 381 | |
0d0cf14c | 382 | spin_unlock_irqrestore(&ihost->scic_lock, flags); |
086a0dab DW |
383 | |
384 | wait_for_completion(tmf->complete); | |
fd18388b | 385 | } |
6f231dda DW |
386 | |
387 | isci_print_tmf(tmf); | |
388 | ||
389 | if (tmf->status == SCI_SUCCESS) | |
390 | ret = TMF_RESP_FUNC_COMPLETE; | |
391 | else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) { | |
0d0cf14c | 392 | dev_dbg(&ihost->pdev->dev, |
6f231dda DW |
393 | "%s: tmf.status == " |
394 | "SCI_FAILURE_IO_RESPONSE_VALID\n", | |
395 | __func__); | |
396 | ret = TMF_RESP_FUNC_COMPLETE; | |
397 | } | |
398 | /* Else - leave the default "failed" status alone. */ | |
399 | ||
0d0cf14c | 400 | dev_dbg(&ihost->pdev->dev, |
6f231dda DW |
401 | "%s: completed request = %p\n", |
402 | __func__, | |
0d0cf14c | 403 | ireq); |
6f231dda | 404 | |
6f231dda DW |
405 | return ret; |
406 | } | |
407 | ||
408 | void isci_task_build_tmf( | |
409 | struct isci_tmf *tmf, | |
6f231dda DW |
410 | enum isci_tmf_function_codes code, |
411 | void (*tmf_sent_cb)(enum isci_tmf_cb_state, | |
412 | struct isci_tmf *, | |
413 | void *), | |
c3f42feb | 414 | void *cb_data) |
6f231dda | 415 | { |
6f231dda DW |
416 | memset(tmf, 0, sizeof(*tmf)); |
417 | ||
6f231dda | 418 | tmf->tmf_code = code; |
6f231dda | 419 | tmf->cb_state_func = tmf_sent_cb; |
c3f42feb JS |
420 | tmf->cb_data = cb_data; |
421 | } | |
1fad9e93 | 422 | |
35173d57 | 423 | static void isci_task_build_abort_task_tmf( |
c3f42feb | 424 | struct isci_tmf *tmf, |
c3f42feb JS |
425 | enum isci_tmf_function_codes code, |
426 | void (*tmf_sent_cb)(enum isci_tmf_cb_state, | |
427 | struct isci_tmf *, | |
428 | void *), | |
429 | struct isci_request *old_request) | |
430 | { | |
209fae14 | 431 | isci_task_build_tmf(tmf, code, tmf_sent_cb, |
c3f42feb JS |
432 | (void *)old_request); |
433 | tmf->io_tag = old_request->io_tag; | |
6f231dda DW |
434 | } |
435 | ||
6f231dda DW |
436 | /** |
437 | * isci_task_validate_request_to_abort() - This function checks the given I/O | |
438 | * against the "started" state. If the request is still "started", it's | |
439 | * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD | |
440 | * BEFORE CALLING THIS FUNCTION. | |
441 | * @isci_request: This parameter specifies the request object to control. | |
442 | * @isci_host: This parameter specifies the ISCI host object | |
443 | * @isci_device: This is the device to which the request is pending. | |
444 | * @aborted_io_completion: This is a completion structure that will be added to | |
445 | * the request in case it is changed to aborting; this completion is | |
446 | * triggered when the request is fully completed. | |
447 | * | |
448 | * Either "started" on successful change of the task status to "aborted", or | |
449 | * "unallocated" if the task cannot be controlled. | |
450 | */ | |
451 | static enum isci_request_status isci_task_validate_request_to_abort( | |
452 | struct isci_request *isci_request, | |
453 | struct isci_host *isci_host, | |
454 | struct isci_remote_device *isci_device, | |
455 | struct completion *aborted_io_completion) | |
456 | { | |
457 | enum isci_request_status old_state = unallocated; | |
458 | ||
459 | /* Only abort the task if it's in the | |
460 | * device's request_in_process list | |
461 | */ | |
462 | if (isci_request && !list_empty(&isci_request->dev_node)) { | |
463 | old_state = isci_request_change_started_to_aborted( | |
464 | isci_request, aborted_io_completion); | |
465 | ||
6f231dda DW |
466 | } |
467 | ||
468 | return old_state; | |
469 | } | |
470 | ||
77c852f3 JS |
471 | /** |
472 | * isci_request_cleanup_completed_loiterer() - This function will take care of | |
473 | * the final cleanup on any request which has been explicitly terminated. | |
474 | * @isci_host: This parameter specifies the ISCI host object | |
475 | * @isci_device: This is the device to which the request is pending. | |
476 | * @isci_request: This parameter specifies the terminated request object. | |
477 | * @task: This parameter is the libsas I/O request. | |
478 | */ | |
6f231dda | 479 | static void isci_request_cleanup_completed_loiterer( |
77c852f3 | 480 | struct isci_host *isci_host, |
6f231dda | 481 | struct isci_remote_device *isci_device, |
77c852f3 JS |
482 | struct isci_request *isci_request, |
483 | struct sas_task *task) | |
6f231dda | 484 | { |
77c852f3 | 485 | unsigned long flags; |
6f231dda DW |
486 | |
487 | dev_dbg(&isci_host->pdev->dev, | |
488 | "%s: isci_device=%p, request=%p, task=%p\n", | |
18d3d72a | 489 | __func__, isci_device, isci_request, task); |
6f231dda | 490 | |
a5fde225 JS |
491 | if (task != NULL) { |
492 | ||
493 | spin_lock_irqsave(&task->task_state_lock, flags); | |
494 | task->lldd_task = NULL; | |
495 | ||
77c852f3 JS |
496 | task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET; |
497 | ||
a5fde225 JS |
498 | isci_set_task_doneflags(task); |
499 | ||
500 | /* If this task is not in the abort path, call task_done. */ | |
501 | if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) { | |
502 | ||
503 | spin_unlock_irqrestore(&task->task_state_lock, flags); | |
504 | task->task_done(task); | |
505 | } else | |
506 | spin_unlock_irqrestore(&task->task_state_lock, flags); | |
507 | } | |
4dc043c4 | 508 | |
77c852f3 JS |
509 | if (isci_request != NULL) { |
510 | spin_lock_irqsave(&isci_host->scic_lock, flags); | |
511 | list_del_init(&isci_request->dev_node); | |
512 | spin_unlock_irqrestore(&isci_host->scic_lock, flags); | |
4dc043c4 | 513 | |
77c852f3 | 514 | isci_request_free(isci_host, isci_request); |
4dc043c4 | 515 | } |
4dc043c4 JS |
516 | } |
517 | ||
6f231dda DW |
518 | /** |
519 | * isci_terminate_request_core() - This function will terminate the given | |
520 | * request, and wait for it to complete. This function must only be called | |
521 | * from a thread that can wait. Note that the request is terminated and | |
522 | * completed (back to the host, if started there). | |
523 | * @isci_host: This SCU. | |
524 | * @isci_device: The target. | |
525 | * @isci_request: The I/O request to be terminated. | |
526 | * | |
6f231dda DW |
527 | */ |
528 | static void isci_terminate_request_core( | |
529 | struct isci_host *isci_host, | |
530 | struct isci_remote_device *isci_device, | |
cbb65c66 | 531 | struct isci_request *isci_request) |
6f231dda | 532 | { |
cbb65c66 | 533 | enum sci_status status = SCI_SUCCESS; |
6f231dda DW |
534 | bool was_terminated = false; |
535 | bool needs_cleanup_handling = false; | |
536 | enum isci_request_status request_status; | |
77c852f3 JS |
537 | unsigned long flags; |
538 | unsigned long termination_completed = 1; | |
539 | struct completion *io_request_completion; | |
540 | struct sas_task *task; | |
6f231dda DW |
541 | |
542 | dev_dbg(&isci_host->pdev->dev, | |
543 | "%s: device = %p; request = %p\n", | |
544 | __func__, isci_device, isci_request); | |
545 | ||
6f231dda | 546 | spin_lock_irqsave(&isci_host->scic_lock, flags); |
4dc043c4 | 547 | |
77c852f3 JS |
548 | io_request_completion = isci_request->io_request_completion; |
549 | ||
550 | task = (isci_request->ttype == io_task) | |
551 | ? isci_request_access_task(isci_request) | |
552 | : NULL; | |
553 | ||
4dc043c4 JS |
554 | /* Note that we are not going to control |
555 | * the target to abort the request. | |
556 | */ | |
557 | isci_request->complete_in_target = true; | |
558 | ||
6f231dda DW |
559 | /* Make sure the request wasn't just sitting around signalling |
560 | * device condition (if the request handle is NULL, then the | |
561 | * request completed but needed additional handling here). | |
562 | */ | |
67ea838d | 563 | if (!isci_request->terminated) { |
6f231dda | 564 | was_terminated = true; |
cbb65c66 | 565 | needs_cleanup_handling = true; |
6f231dda | 566 | status = scic_controller_terminate_request( |
cc3dbd0a | 567 | &isci_host->sci, |
57f20f4e | 568 | &isci_device->sci, |
67ea838d | 569 | &isci_request->sci); |
6f231dda DW |
570 | } |
571 | spin_unlock_irqrestore(&isci_host->scic_lock, flags); | |
572 | ||
573 | /* | |
574 | * The only time the request to terminate will | |
575 | * fail is when the io request is completed and | |
576 | * being aborted. | |
577 | */ | |
4dc043c4 | 578 | if (status != SCI_SUCCESS) { |
6f231dda DW |
579 | dev_err(&isci_host->pdev->dev, |
580 | "%s: scic_controller_terminate_request" | |
581 | " returned = 0x%x\n", | |
77c852f3 JS |
582 | __func__, status); |
583 | ||
4dc043c4 JS |
584 | isci_request->io_request_completion = NULL; |
585 | ||
586 | } else { | |
6f231dda DW |
587 | if (was_terminated) { |
588 | dev_dbg(&isci_host->pdev->dev, | |
77c852f3 JS |
589 | "%s: before completion wait (%p/%p)\n", |
590 | __func__, isci_request, io_request_completion); | |
6f231dda DW |
591 | |
592 | /* Wait here for the request to complete. */ | |
77c852f3 JS |
593 | #define TERMINATION_TIMEOUT_MSEC 500 |
594 | termination_completed | |
4dc043c4 | 595 | = wait_for_completion_timeout( |
77c852f3 | 596 | io_request_completion, |
4dc043c4 JS |
597 | msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC)); |
598 | ||
77c852f3 JS |
599 | if (!termination_completed) { |
600 | ||
601 | /* The request to terminate has timed out. */ | |
602 | spin_lock_irqsave(&isci_host->scic_lock, | |
603 | flags); | |
604 | ||
605 | /* Check for state changes. */ | |
606 | if (!isci_request->terminated) { | |
607 | ||
608 | /* The best we can do is to have the | |
609 | * request die a silent death if it | |
610 | * ever really completes. | |
611 | * | |
612 | * Set the request state to "dead", | |
613 | * and clear the task pointer so that | |
614 | * an actual completion event callback | |
615 | * doesn't do anything. | |
616 | */ | |
617 | isci_request->status = dead; | |
618 | isci_request->io_request_completion | |
619 | = NULL; | |
620 | ||
621 | if (isci_request->ttype == io_task) { | |
622 | ||
623 | /* Break links with the | |
624 | * sas_task. | |
625 | */ | |
626 | isci_request->ttype_ptr.io_task_ptr | |
627 | = NULL; | |
628 | } | |
629 | } else | |
630 | termination_completed = 1; | |
631 | ||
632 | spin_unlock_irqrestore(&isci_host->scic_lock, | |
633 | flags); | |
4dc043c4 | 634 | |
77c852f3 | 635 | if (!termination_completed) { |
4dc043c4 | 636 | |
77c852f3 JS |
637 | dev_err(&isci_host->pdev->dev, |
638 | "%s: *** Timeout waiting for " | |
639 | "termination(%p/%p)\n", | |
640 | __func__, io_request_completion, | |
641 | isci_request); | |
4dc043c4 | 642 | |
77c852f3 JS |
643 | /* The request can no longer be referenced |
644 | * safely since it may go away if the | |
645 | * termination every really does complete. | |
646 | */ | |
647 | isci_request = NULL; | |
648 | } | |
649 | } | |
650 | if (termination_completed) | |
4dc043c4 | 651 | dev_dbg(&isci_host->pdev->dev, |
77c852f3 JS |
652 | "%s: after completion wait (%p/%p)\n", |
653 | __func__, isci_request, io_request_completion); | |
4dc043c4 | 654 | } |
4dc043c4 | 655 | |
77c852f3 | 656 | if (termination_completed) { |
cbb65c66 | 657 | |
77c852f3 | 658 | isci_request->io_request_completion = NULL; |
6f231dda | 659 | |
77c852f3 JS |
660 | /* Peek at the status of the request. This will tell |
661 | * us if there was special handling on the request such that it | |
662 | * needs to be detached and freed here. | |
663 | */ | |
664 | spin_lock_irqsave(&isci_request->state_lock, flags); | |
665 | request_status = isci_request_get_state(isci_request); | |
666 | ||
667 | if ((isci_request->ttype == io_task) /* TMFs are in their own thread */ | |
668 | && ((request_status == aborted) | |
669 | || (request_status == aborting) | |
670 | || (request_status == terminating) | |
671 | || (request_status == completed) | |
672 | || (request_status == dead) | |
673 | ) | |
674 | ) { | |
675 | ||
676 | /* The completion routine won't free a request in | |
677 | * the aborted/aborting/etc. states, so we do | |
678 | * it here. | |
679 | */ | |
680 | needs_cleanup_handling = true; | |
681 | } | |
682 | spin_unlock_irqrestore(&isci_request->state_lock, flags); | |
6f231dda | 683 | |
77c852f3 JS |
684 | } |
685 | if (needs_cleanup_handling) | |
686 | isci_request_cleanup_completed_loiterer( | |
687 | isci_host, isci_device, isci_request, task); | |
a5fde225 | 688 | } |
6f231dda DW |
689 | } |
690 | ||
691 | /** | |
692 | * isci_terminate_pending_requests() - This function will change the all of the | |
693 | * requests on the given device's state to "aborting", will terminate the | |
694 | * requests, and wait for them to complete. This function must only be | |
695 | * called from a thread that can wait. Note that the requests are all | |
696 | * terminated and completed (back to the host, if started there). | |
697 | * @isci_host: This parameter specifies SCU. | |
698 | * @isci_device: This parameter specifies the target. | |
699 | * | |
6f231dda | 700 | */ |
980d3aeb DW |
701 | void isci_terminate_pending_requests(struct isci_host *ihost, |
702 | struct isci_remote_device *idev) | |
6f231dda | 703 | { |
980d3aeb | 704 | struct completion request_completion; |
77c852f3 | 705 | enum isci_request_status old_state; |
980d3aeb DW |
706 | unsigned long flags; |
707 | LIST_HEAD(list); | |
6f231dda | 708 | |
980d3aeb DW |
709 | spin_lock_irqsave(&ihost->scic_lock, flags); |
710 | list_splice_init(&idev->reqs_in_process, &list); | |
6f231dda | 711 | |
980d3aeb DW |
712 | /* assumes that isci_terminate_request_core deletes from the list */ |
713 | while (!list_empty(&list)) { | |
714 | struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node); | |
6f231dda | 715 | |
980d3aeb DW |
716 | /* Change state to "terminating" if it is currently |
717 | * "started". | |
718 | */ | |
719 | old_state = isci_request_change_started_to_newstate(ireq, | |
720 | &request_completion, | |
721 | terminating); | |
722 | switch (old_state) { | |
723 | case started: | |
724 | case completed: | |
725 | case aborting: | |
726 | break; | |
727 | default: | |
728 | /* termination in progress, or otherwise dispositioned. | |
729 | * We know the request was on 'list' so should be safe | |
730 | * to move it back to reqs_in_process | |
731 | */ | |
732 | list_move(&ireq->dev_node, &idev->reqs_in_process); | |
733 | ireq = NULL; | |
734 | break; | |
735 | } | |
4dc043c4 | 736 | |
980d3aeb DW |
737 | if (!ireq) |
738 | continue; | |
739 | spin_unlock_irqrestore(&ihost->scic_lock, flags); | |
77c852f3 | 740 | |
980d3aeb | 741 | init_completion(&request_completion); |
77c852f3 | 742 | |
980d3aeb DW |
743 | dev_dbg(&ihost->pdev->dev, |
744 | "%s: idev=%p request=%p; task=%p old_state=%d\n", | |
745 | __func__, idev, ireq, | |
746 | ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL, | |
747 | old_state); | |
748 | ||
749 | /* If the old_state is started: | |
750 | * This request was not already being aborted. If it had been, | |
751 | * then the aborting I/O (ie. the TMF request) would not be in | |
752 | * the aborting state, and thus would be terminated here. Note | |
753 | * that since the TMF completion's call to the kernel function | |
754 | * "complete()" does not happen until the pending I/O request | |
755 | * terminate fully completes, we do not have to implement a | |
756 | * special wait here for already aborting requests - the | |
757 | * termination of the TMF request will force the request | |
758 | * to finish it's already started terminate. | |
759 | * | |
760 | * If old_state == completed: | |
761 | * This request completed from the SCU hardware perspective | |
762 | * and now just needs cleaning up in terms of freeing the | |
763 | * request and potentially calling up to libsas. | |
764 | * | |
765 | * If old_state == aborting: | |
766 | * This request has already gone through a TMF timeout, but may | |
767 | * not have been terminated; needs cleaning up at least. | |
768 | */ | |
769 | isci_terminate_request_core(ihost, idev, ireq); | |
770 | spin_lock_irqsave(&ihost->scic_lock, flags); | |
4dc043c4 | 771 | } |
980d3aeb | 772 | spin_unlock_irqrestore(&ihost->scic_lock, flags); |
6f231dda DW |
773 | } |
774 | ||
775 | /** | |
776 | * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain | |
777 | * Template functions. | |
778 | * @lun: This parameter specifies the lun to be reset. | |
779 | * | |
780 | * status, zero indicates success. | |
781 | */ | |
782 | static int isci_task_send_lu_reset_sas( | |
783 | struct isci_host *isci_host, | |
784 | struct isci_remote_device *isci_device, | |
785 | u8 *lun) | |
786 | { | |
787 | struct isci_tmf tmf; | |
788 | int ret = TMF_RESP_FUNC_FAILED; | |
789 | ||
790 | dev_dbg(&isci_host->pdev->dev, | |
791 | "%s: isci_host = %p, isci_device = %p\n", | |
792 | __func__, isci_host, isci_device); | |
793 | /* Send the LUN reset to the target. By the time the call returns, | |
794 | * the TMF has fully exected in the target (in which case the return | |
795 | * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or | |
796 | * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED"). | |
797 | */ | |
209fae14 | 798 | isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset, NULL, NULL); |
6f231dda DW |
799 | |
800 | #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */ | |
209fae14 | 801 | ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS); |
6f231dda DW |
802 | |
803 | if (ret == TMF_RESP_FUNC_COMPLETE) | |
804 | dev_dbg(&isci_host->pdev->dev, | |
805 | "%s: %p: TMF_LU_RESET passed\n", | |
806 | __func__, isci_device); | |
807 | else | |
808 | dev_dbg(&isci_host->pdev->dev, | |
809 | "%s: %p: TMF_LU_RESET failed (%x)\n", | |
810 | __func__, isci_device, ret); | |
811 | ||
812 | return ret; | |
813 | } | |
814 | ||
815 | /** | |
816 | * isci_task_lu_reset() - This function is one of the SAS Domain Template | |
817 | * functions. This is one of the Task Management functoins called by libsas, | |
818 | * to reset the given lun. Note the assumption that while this call is | |
819 | * executing, no I/O will be sent by the host to the device. | |
820 | * @lun: This parameter specifies the lun to be reset. | |
821 | * | |
822 | * status, zero indicates success. | |
823 | */ | |
4393aa4e | 824 | int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun) |
6f231dda | 825 | { |
4393aa4e | 826 | struct isci_host *isci_host = dev_to_ihost(domain_device); |
209fae14 DW |
827 | struct isci_remote_device *isci_device; |
828 | unsigned long flags; | |
6f231dda | 829 | int ret; |
6f231dda | 830 | |
209fae14 DW |
831 | spin_lock_irqsave(&isci_host->scic_lock, flags); |
832 | isci_device = isci_lookup_device(domain_device); | |
833 | spin_unlock_irqrestore(&isci_host->scic_lock, flags); | |
6f231dda | 834 | |
4393aa4e DW |
835 | dev_dbg(&isci_host->pdev->dev, |
836 | "%s: domain_device=%p, isci_host=%p; isci_device=%p\n", | |
6f231dda DW |
837 | __func__, domain_device, isci_host, isci_device); |
838 | ||
209fae14 | 839 | if (isci_device) |
d06b487b | 840 | set_bit(IDEV_EH, &isci_device->flags); |
6f231dda DW |
841 | |
842 | /* If there is a device reset pending on any request in the | |
843 | * device's list, fail this LUN reset request in order to | |
844 | * escalate to the device reset. | |
845 | */ | |
209fae14 | 846 | if (!isci_device || |
4393aa4e | 847 | isci_device_is_reset_pending(isci_host, isci_device)) { |
6f231dda | 848 | dev_warn(&isci_host->pdev->dev, |
4393aa4e | 849 | "%s: No dev (%p), or " |
6f231dda | 850 | "RESET PENDING: domain_device=%p\n", |
4393aa4e | 851 | __func__, isci_device, domain_device); |
209fae14 DW |
852 | ret = TMF_RESP_FUNC_FAILED; |
853 | goto out; | |
6f231dda DW |
854 | } |
855 | ||
6f231dda DW |
856 | /* Send the task management part of the reset. */ |
857 | if (sas_protocol_ata(domain_device->tproto)) { | |
4393aa4e | 858 | ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun); |
6f231dda DW |
859 | } else |
860 | ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun); | |
861 | ||
862 | /* If the LUN reset worked, all the I/O can now be terminated. */ | |
863 | if (ret == TMF_RESP_FUNC_COMPLETE) | |
864 | /* Terminate all I/O now. */ | |
865 | isci_terminate_pending_requests(isci_host, | |
980d3aeb | 866 | isci_device); |
6f231dda | 867 | |
209fae14 DW |
868 | out: |
869 | isci_put_device(isci_device); | |
6f231dda DW |
870 | return ret; |
871 | } | |
872 | ||
873 | ||
874 | /* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */ | |
875 | int isci_task_clear_nexus_port(struct asd_sas_port *port) | |
876 | { | |
877 | return TMF_RESP_FUNC_FAILED; | |
878 | } | |
879 | ||
880 | ||
881 | ||
882 | int isci_task_clear_nexus_ha(struct sas_ha_struct *ha) | |
883 | { | |
884 | return TMF_RESP_FUNC_FAILED; | |
885 | } | |
886 | ||
6f231dda DW |
887 | /* Task Management Functions. Must be called from process context. */ |
888 | ||
889 | /** | |
890 | * isci_abort_task_process_cb() - This is a helper function for the abort task | |
891 | * TMF command. It manages the request state with respect to the successful | |
892 | * transmission / completion of the abort task request. | |
893 | * @cb_state: This parameter specifies when this function was called - after | |
894 | * the TMF request has been started and after it has timed-out. | |
895 | * @tmf: This parameter specifies the TMF in progress. | |
896 | * | |
897 | * | |
898 | */ | |
899 | static void isci_abort_task_process_cb( | |
900 | enum isci_tmf_cb_state cb_state, | |
901 | struct isci_tmf *tmf, | |
902 | void *cb_data) | |
903 | { | |
904 | struct isci_request *old_request; | |
905 | ||
906 | old_request = (struct isci_request *)cb_data; | |
907 | ||
908 | dev_dbg(&old_request->isci_host->pdev->dev, | |
909 | "%s: tmf=%p, old_request=%p\n", | |
910 | __func__, tmf, old_request); | |
911 | ||
912 | switch (cb_state) { | |
913 | ||
914 | case isci_tmf_started: | |
915 | /* The TMF has been started. Nothing to do here, since the | |
916 | * request state was already set to "aborted" by the abort | |
917 | * task function. | |
918 | */ | |
6cb4d6b3 BB |
919 | if ((old_request->status != aborted) |
920 | && (old_request->status != completed)) | |
921 | dev_err(&old_request->isci_host->pdev->dev, | |
922 | "%s: Bad request status (%d): tmf=%p, old_request=%p\n", | |
923 | __func__, old_request->status, tmf, old_request); | |
6f231dda DW |
924 | break; |
925 | ||
926 | case isci_tmf_timed_out: | |
927 | ||
928 | /* Set the task's state to "aborting", since the abort task | |
929 | * function thread set it to "aborted" (above) in anticipation | |
930 | * of the task management request working correctly. Since the | |
931 | * timeout has now fired, the TMF request failed. We set the | |
932 | * state such that the request completion will indicate the | |
933 | * device is no longer present. | |
934 | */ | |
935 | isci_request_change_state(old_request, aborting); | |
936 | break; | |
937 | ||
938 | default: | |
939 | dev_err(&old_request->isci_host->pdev->dev, | |
940 | "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n", | |
941 | __func__, cb_state, tmf, old_request); | |
942 | break; | |
943 | } | |
944 | } | |
945 | ||
946 | /** | |
947 | * isci_task_abort_task() - This function is one of the SAS Domain Template | |
948 | * functions. This function is called by libsas to abort a specified task. | |
949 | * @task: This parameter specifies the SAS task to abort. | |
950 | * | |
951 | * status, zero indicates success. | |
952 | */ | |
953 | int isci_task_abort_task(struct sas_task *task) | |
954 | { | |
4393aa4e | 955 | struct isci_host *isci_host = dev_to_ihost(task->dev); |
6f231dda | 956 | DECLARE_COMPLETION_ONSTACK(aborted_io_completion); |
a5fde225 JS |
957 | struct isci_request *old_request = NULL; |
958 | enum isci_request_status old_state; | |
6f231dda | 959 | struct isci_remote_device *isci_device = NULL; |
a5fde225 JS |
960 | struct isci_tmf tmf; |
961 | int ret = TMF_RESP_FUNC_FAILED; | |
962 | unsigned long flags; | |
963 | bool any_dev_reset = false; | |
6f231dda DW |
964 | |
965 | /* Get the isci_request reference from the task. Note that | |
966 | * this check does not depend on the pending request list | |
967 | * in the device, because tasks driving resets may land here | |
968 | * after completion in the core. | |
969 | */ | |
209fae14 DW |
970 | spin_lock_irqsave(&isci_host->scic_lock, flags); |
971 | spin_lock(&task->task_state_lock); | |
972 | ||
973 | old_request = task->lldd_task; | |
974 | ||
975 | /* If task is already done, the request isn't valid */ | |
976 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE) && | |
977 | (task->task_state_flags & SAS_TASK_AT_INITIATOR) && | |
978 | old_request) | |
979 | isci_device = isci_lookup_device(task->dev); | |
980 | ||
981 | spin_unlock(&task->task_state_lock); | |
982 | spin_unlock_irqrestore(&isci_host->scic_lock, flags); | |
6f231dda DW |
983 | |
984 | dev_dbg(&isci_host->pdev->dev, | |
985 | "%s: task = %p\n", __func__, task); | |
986 | ||
209fae14 DW |
987 | if (!isci_device || !old_request) |
988 | goto out; | |
6f231dda | 989 | |
d06b487b DW |
990 | set_bit(IDEV_EH, &isci_device->flags); |
991 | ||
6f231dda DW |
992 | /* This version of the driver will fail abort requests for |
993 | * SATA/STP. Failing the abort request this way will cause the | |
994 | * SCSI error handler thread to escalate to LUN reset | |
995 | */ | |
209fae14 | 996 | if (sas_protocol_ata(task->task_proto)) { |
6f231dda DW |
997 | dev_warn(&isci_host->pdev->dev, |
998 | " task %p is for a STP/SATA device;" | |
999 | " returning TMF_RESP_FUNC_FAILED\n" | |
1000 | " to cause a LUN reset...\n", task); | |
209fae14 | 1001 | goto out; |
6f231dda DW |
1002 | } |
1003 | ||
1004 | dev_dbg(&isci_host->pdev->dev, | |
1005 | "%s: old_request == %p\n", __func__, old_request); | |
1006 | ||
209fae14 | 1007 | any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device); |
a5fde225 | 1008 | |
6f231dda DW |
1009 | spin_lock_irqsave(&task->task_state_lock, flags); |
1010 | ||
209fae14 | 1011 | any_dev_reset = any_dev_reset || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET); |
6f231dda DW |
1012 | |
1013 | /* If the extraction of the request reference from the task | |
1014 | * failed, then the request has been completed (or if there is a | |
1015 | * pending reset then this abort request function must be failed | |
1016 | * in order to escalate to the target reset). | |
1017 | */ | |
a5fde225 | 1018 | if ((old_request == NULL) || any_dev_reset) { |
6f231dda DW |
1019 | |
1020 | /* If the device reset task flag is set, fail the task | |
1021 | * management request. Otherwise, the original request | |
1022 | * has completed. | |
1023 | */ | |
1024 | if (any_dev_reset) { | |
1025 | ||
1026 | /* Turn off the task's DONE to make sure this | |
1027 | * task is escalated to a target reset. | |
1028 | */ | |
1029 | task->task_state_flags &= ~SAS_TASK_STATE_DONE; | |
1030 | ||
a5fde225 JS |
1031 | /* Make the reset happen as soon as possible. */ |
1032 | task->task_state_flags |= SAS_TASK_NEED_DEV_RESET; | |
1033 | ||
1034 | spin_unlock_irqrestore(&task->task_state_lock, flags); | |
1035 | ||
6f231dda DW |
1036 | /* Fail the task management request in order to |
1037 | * escalate to the target reset. | |
1038 | */ | |
1039 | ret = TMF_RESP_FUNC_FAILED; | |
1040 | ||
1041 | dev_dbg(&isci_host->pdev->dev, | |
1042 | "%s: Failing task abort in order to " | |
1043 | "escalate to target reset because\n" | |
1044 | "SAS_TASK_NEED_DEV_RESET is set for " | |
1045 | "task %p on dev %p\n", | |
1046 | __func__, task, isci_device); | |
1047 | ||
6f231dda | 1048 | |
a5fde225 | 1049 | } else { |
6f231dda DW |
1050 | /* The request has already completed and there |
1051 | * is nothing to do here other than to set the task | |
1052 | * done bit, and indicate that the task abort function | |
1053 | * was sucessful. | |
1054 | */ | |
1055 | isci_set_task_doneflags(task); | |
1056 | ||
a5fde225 | 1057 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
6f231dda | 1058 | |
a5fde225 | 1059 | ret = TMF_RESP_FUNC_COMPLETE; |
6f231dda | 1060 | |
a5fde225 JS |
1061 | dev_dbg(&isci_host->pdev->dev, |
1062 | "%s: abort task not needed for %p\n", | |
1063 | __func__, task); | |
6f231dda | 1064 | } |
209fae14 | 1065 | goto out; |
6f231dda | 1066 | } |
a5fde225 JS |
1067 | else |
1068 | spin_unlock_irqrestore(&task->task_state_lock, flags); | |
6f231dda DW |
1069 | |
1070 | spin_lock_irqsave(&isci_host->scic_lock, flags); | |
1071 | ||
f219f010 | 1072 | /* Check the request status and change to "aborted" if currently |
a5fde225 | 1073 | * "starting"; if true then set the I/O kernel completion |
6f231dda DW |
1074 | * struct that will be triggered when the request completes. |
1075 | */ | |
a5fde225 JS |
1076 | old_state = isci_task_validate_request_to_abort( |
1077 | old_request, isci_host, isci_device, | |
1078 | &aborted_io_completion); | |
f219f010 JS |
1079 | if ((old_state != started) && |
1080 | (old_state != completed) && | |
1081 | (old_state != aborting)) { | |
6f231dda DW |
1082 | |
1083 | spin_unlock_irqrestore(&isci_host->scic_lock, flags); | |
1084 | ||
a5fde225 JS |
1085 | /* The request was already being handled by someone else (because |
1086 | * they got to set the state away from started). | |
1087 | */ | |
1088 | dev_dbg(&isci_host->pdev->dev, | |
1089 | "%s: device = %p; old_request %p already being aborted\n", | |
1090 | __func__, | |
1091 | isci_device, old_request); | |
209fae14 DW |
1092 | ret = TMF_RESP_FUNC_COMPLETE; |
1093 | goto out; | |
6f231dda | 1094 | } |
a5fde225 | 1095 | if ((task->task_proto == SAS_PROTOCOL_SMP) |
a5fde225 JS |
1096 | || old_request->complete_in_target |
1097 | ) { | |
6f231dda DW |
1098 | |
1099 | spin_unlock_irqrestore(&isci_host->scic_lock, flags); | |
1100 | ||
a5fde225 JS |
1101 | dev_dbg(&isci_host->pdev->dev, |
1102 | "%s: SMP request (%d)" | |
a5fde225 JS |
1103 | " or complete_in_target (%d), thus no TMF\n", |
1104 | __func__, (task->task_proto == SAS_PROTOCOL_SMP), | |
209fae14 | 1105 | old_request->complete_in_target); |
a5fde225 | 1106 | |
6f231dda DW |
1107 | /* Set the state on the task. */ |
1108 | isci_task_all_done(task); | |
1109 | ||
1110 | ret = TMF_RESP_FUNC_COMPLETE; | |
1111 | ||
1112 | /* Stopping and SMP devices are not sent a TMF, and are not | |
a5fde225 | 1113 | * reset, but the outstanding I/O request is terminated below. |
6f231dda | 1114 | */ |
6f231dda DW |
1115 | } else { |
1116 | /* Fill in the tmf stucture */ | |
209fae14 | 1117 | isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort, |
c3f42feb JS |
1118 | isci_abort_task_process_cb, |
1119 | old_request); | |
6f231dda | 1120 | |
6f231dda DW |
1121 | spin_unlock_irqrestore(&isci_host->scic_lock, flags); |
1122 | ||
1123 | #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */ | |
209fae14 | 1124 | ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, |
6f231dda DW |
1125 | ISCI_ABORT_TASK_TIMEOUT_MS); |
1126 | ||
a5fde225 | 1127 | if (ret != TMF_RESP_FUNC_COMPLETE) |
6f231dda DW |
1128 | dev_err(&isci_host->pdev->dev, |
1129 | "%s: isci_task_send_tmf failed\n", | |
1130 | __func__); | |
1131 | } | |
a5fde225 JS |
1132 | if (ret == TMF_RESP_FUNC_COMPLETE) { |
1133 | old_request->complete_in_target = true; | |
1134 | ||
77c852f3 JS |
1135 | /* Clean up the request on our side, and wait for the aborted |
1136 | * I/O to complete. | |
1137 | */ | |
cbb65c66 | 1138 | isci_terminate_request_core(isci_host, isci_device, old_request); |
a5fde225 | 1139 | } |
6f231dda | 1140 | |
a5fde225 JS |
1141 | /* Make sure we do not leave a reference to aborted_io_completion */ |
1142 | old_request->io_request_completion = NULL; | |
209fae14 DW |
1143 | out: |
1144 | isci_put_device(isci_device); | |
6f231dda DW |
1145 | return ret; |
1146 | } | |
1147 | ||
1148 | /** | |
1149 | * isci_task_abort_task_set() - This function is one of the SAS Domain Template | |
1150 | * functions. This is one of the Task Management functoins called by libsas, | |
1151 | * to abort all task for the given lun. | |
1152 | * @d_device: This parameter specifies the domain device associated with this | |
1153 | * request. | |
1154 | * @lun: This parameter specifies the lun associated with this request. | |
1155 | * | |
1156 | * status, zero indicates success. | |
1157 | */ | |
1158 | int isci_task_abort_task_set( | |
1159 | struct domain_device *d_device, | |
1160 | u8 *lun) | |
1161 | { | |
1162 | return TMF_RESP_FUNC_FAILED; | |
1163 | } | |
1164 | ||
1165 | ||
1166 | /** | |
1167 | * isci_task_clear_aca() - This function is one of the SAS Domain Template | |
1168 | * functions. This is one of the Task Management functoins called by libsas. | |
1169 | * @d_device: This parameter specifies the domain device associated with this | |
1170 | * request. | |
1171 | * @lun: This parameter specifies the lun associated with this request. | |
1172 | * | |
1173 | * status, zero indicates success. | |
1174 | */ | |
1175 | int isci_task_clear_aca( | |
1176 | struct domain_device *d_device, | |
1177 | u8 *lun) | |
1178 | { | |
1179 | return TMF_RESP_FUNC_FAILED; | |
1180 | } | |
1181 | ||
1182 | ||
1183 | ||
1184 | /** | |
1185 | * isci_task_clear_task_set() - This function is one of the SAS Domain Template | |
1186 | * functions. This is one of the Task Management functoins called by libsas. | |
1187 | * @d_device: This parameter specifies the domain device associated with this | |
1188 | * request. | |
1189 | * @lun: This parameter specifies the lun associated with this request. | |
1190 | * | |
1191 | * status, zero indicates success. | |
1192 | */ | |
1193 | int isci_task_clear_task_set( | |
1194 | struct domain_device *d_device, | |
1195 | u8 *lun) | |
1196 | { | |
1197 | return TMF_RESP_FUNC_FAILED; | |
1198 | } | |
1199 | ||
1200 | ||
1201 | /** | |
1202 | * isci_task_query_task() - This function is implemented to cause libsas to | |
1203 | * correctly escalate the failed abort to a LUN or target reset (this is | |
1204 | * because sas_scsi_find_task libsas function does not correctly interpret | |
1205 | * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is | |
1206 | * returned, libsas turns this into a LUN reset; when FUNC_FAILED is | |
1207 | * returned, libsas will turn this into a target reset | |
1208 | * @task: This parameter specifies the sas task being queried. | |
1209 | * @lun: This parameter specifies the lun associated with this request. | |
1210 | * | |
1211 | * status, zero indicates success. | |
1212 | */ | |
1213 | int isci_task_query_task( | |
1214 | struct sas_task *task) | |
1215 | { | |
1216 | /* See if there is a pending device reset for this device. */ | |
1217 | if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET) | |
1218 | return TMF_RESP_FUNC_FAILED; | |
1219 | else | |
1220 | return TMF_RESP_FUNC_SUCC; | |
1221 | } | |
1222 | ||
af5ae893 | 1223 | /* |
6f231dda DW |
1224 | * isci_task_request_complete() - This function is called by the sci core when |
1225 | * an task request completes. | |
af5ae893 DJ |
1226 | * @ihost: This parameter specifies the ISCI host object |
1227 | * @ireq: This parameter is the completed isci_request object. | |
6f231dda DW |
1228 | * @completion_status: This parameter specifies the completion status from the |
1229 | * sci core. | |
1230 | * | |
1231 | * none. | |
1232 | */ | |
af5ae893 DJ |
1233 | void |
1234 | isci_task_request_complete(struct isci_host *ihost, | |
1235 | struct isci_request *ireq, | |
1236 | enum sci_task_status completion_status) | |
6f231dda | 1237 | { |
af5ae893 | 1238 | struct isci_tmf *tmf = isci_request_access_tmf(ireq); |
6f231dda | 1239 | struct completion *tmf_complete; |
67ea838d | 1240 | struct scic_sds_request *sci_req = &ireq->sci; |
6f231dda | 1241 | |
af5ae893 | 1242 | dev_dbg(&ihost->pdev->dev, |
6f231dda | 1243 | "%s: request = %p, status=%d\n", |
af5ae893 | 1244 | __func__, ireq, completion_status); |
6f231dda | 1245 | |
8d2c65c0 | 1246 | isci_request_change_state(ireq, completed); |
6f231dda DW |
1247 | |
1248 | tmf->status = completion_status; | |
af5ae893 | 1249 | ireq->complete_in_target = true; |
6f231dda | 1250 | |
af5ae893 | 1251 | if (tmf->proto == SAS_PROTOCOL_SSP) { |
6f231dda | 1252 | memcpy(&tmf->resp.resp_iu, |
b7645818 | 1253 | &sci_req->ssp.rsp, |
af5ae893 DJ |
1254 | SSP_RESP_IU_MAX_SIZE); |
1255 | } else if (tmf->proto == SAS_PROTOCOL_SATA) { | |
6f231dda | 1256 | memcpy(&tmf->resp.d2h_fis, |
b7645818 | 1257 | &sci_req->stp.rsp, |
f2f30080 | 1258 | sizeof(struct dev_to_host_fis)); |
6f231dda DW |
1259 | } |
1260 | ||
6f231dda DW |
1261 | /* PRINT_TMF( ((struct isci_tmf *)request->task)); */ |
1262 | tmf_complete = tmf->complete; | |
1263 | ||
209fae14 | 1264 | scic_controller_complete_io(&ihost->sci, ireq->sci.target_device, &ireq->sci); |
67ea838d | 1265 | /* set the 'terminated' flag handle to make sure it cannot be terminated |
6f231dda DW |
1266 | * or completed again. |
1267 | */ | |
67ea838d | 1268 | ireq->terminated = true;; |
6f231dda | 1269 | |
af5ae893 DJ |
1270 | isci_request_change_state(ireq, unallocated); |
1271 | list_del_init(&ireq->dev_node); | |
6f231dda DW |
1272 | |
1273 | /* The task management part completes last. */ | |
1274 | complete(tmf_complete); | |
1275 | } | |
1276 | ||
61aaff49 JS |
1277 | static void isci_smp_task_timedout(unsigned long _task) |
1278 | { | |
1279 | struct sas_task *task = (void *) _task; | |
1280 | unsigned long flags; | |
1281 | ||
1282 | spin_lock_irqsave(&task->task_state_lock, flags); | |
1283 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) | |
1284 | task->task_state_flags |= SAS_TASK_STATE_ABORTED; | |
1285 | spin_unlock_irqrestore(&task->task_state_lock, flags); | |
1286 | ||
1287 | complete(&task->completion); | |
1288 | } | |
1289 | ||
1290 | static void isci_smp_task_done(struct sas_task *task) | |
1291 | { | |
1292 | if (!del_timer(&task->timer)) | |
1293 | return; | |
1294 | complete(&task->completion); | |
1295 | } | |
1296 | ||
1297 | static struct sas_task *isci_alloc_task(void) | |
1298 | { | |
1299 | struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL); | |
1300 | ||
1301 | if (task) { | |
1302 | INIT_LIST_HEAD(&task->list); | |
1303 | spin_lock_init(&task->task_state_lock); | |
1304 | task->task_state_flags = SAS_TASK_STATE_PENDING; | |
1305 | init_timer(&task->timer); | |
1306 | init_completion(&task->completion); | |
1307 | } | |
1308 | ||
1309 | return task; | |
1310 | } | |
1311 | ||
1312 | static void isci_free_task(struct isci_host *ihost, struct sas_task *task) | |
1313 | { | |
1314 | if (task) { | |
1315 | BUG_ON(!list_empty(&task->list)); | |
1316 | kfree(task); | |
1317 | } | |
1318 | } | |
1319 | ||
1320 | static int isci_smp_execute_task(struct isci_host *ihost, | |
1321 | struct domain_device *dev, void *req, | |
1322 | int req_size, void *resp, int resp_size) | |
1323 | { | |
1324 | int res, retry; | |
1325 | struct sas_task *task = NULL; | |
1326 | ||
1327 | for (retry = 0; retry < 3; retry++) { | |
1328 | task = isci_alloc_task(); | |
1329 | if (!task) | |
1330 | return -ENOMEM; | |
1331 | ||
1332 | task->dev = dev; | |
1333 | task->task_proto = dev->tproto; | |
1334 | sg_init_one(&task->smp_task.smp_req, req, req_size); | |
1335 | sg_init_one(&task->smp_task.smp_resp, resp, resp_size); | |
1336 | ||
1337 | task->task_done = isci_smp_task_done; | |
1338 | ||
1339 | task->timer.data = (unsigned long) task; | |
1340 | task->timer.function = isci_smp_task_timedout; | |
1341 | task->timer.expires = jiffies + 10*HZ; | |
1342 | add_timer(&task->timer); | |
1343 | ||
1344 | res = isci_task_execute_task(task, 1, GFP_KERNEL); | |
1345 | ||
1346 | if (res) { | |
1347 | del_timer(&task->timer); | |
1348 | dev_err(&ihost->pdev->dev, | |
1349 | "%s: executing SMP task failed:%d\n", | |
1350 | __func__, res); | |
1351 | goto ex_err; | |
1352 | } | |
1353 | ||
1354 | wait_for_completion(&task->completion); | |
1355 | res = -ECOMM; | |
1356 | if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { | |
1357 | dev_err(&ihost->pdev->dev, | |
1358 | "%s: smp task timed out or aborted\n", | |
1359 | __func__); | |
1360 | isci_task_abort_task(task); | |
1361 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { | |
1362 | dev_err(&ihost->pdev->dev, | |
1363 | "%s: SMP task aborted and not done\n", | |
1364 | __func__); | |
1365 | goto ex_err; | |
1366 | } | |
1367 | } | |
1368 | if (task->task_status.resp == SAS_TASK_COMPLETE && | |
1369 | task->task_status.stat == SAM_STAT_GOOD) { | |
1370 | res = 0; | |
1371 | break; | |
1372 | } | |
1373 | if (task->task_status.resp == SAS_TASK_COMPLETE && | |
1374 | task->task_status.stat == SAS_DATA_UNDERRUN) { | |
1375 | /* no error, but return the number of bytes of | |
1376 | * underrun */ | |
1377 | res = task->task_status.residual; | |
1378 | break; | |
1379 | } | |
1380 | if (task->task_status.resp == SAS_TASK_COMPLETE && | |
1381 | task->task_status.stat == SAS_DATA_OVERRUN) { | |
1382 | res = -EMSGSIZE; | |
1383 | break; | |
1384 | } else { | |
1385 | dev_err(&ihost->pdev->dev, | |
1386 | "%s: task to dev %016llx response: 0x%x " | |
1387 | "status 0x%x\n", __func__, | |
1388 | SAS_ADDR(dev->sas_addr), | |
1389 | task->task_status.resp, | |
1390 | task->task_status.stat); | |
1391 | isci_free_task(ihost, task); | |
1392 | task = NULL; | |
1393 | } | |
1394 | } | |
1395 | ex_err: | |
1396 | BUG_ON(retry == 3 && task != NULL); | |
1397 | isci_free_task(ihost, task); | |
1398 | return res; | |
1399 | } | |
1400 | ||
1401 | #define DISCOVER_REQ_SIZE 16 | |
1402 | #define DISCOVER_RESP_SIZE 56 | |
1403 | ||
1404 | int isci_smp_get_phy_attached_dev_type(struct isci_host *ihost, | |
1405 | struct domain_device *dev, | |
1406 | int phy_id, int *adt) | |
1407 | { | |
1408 | struct smp_resp *disc_resp; | |
1409 | u8 *disc_req; | |
1410 | int res; | |
1411 | ||
1412 | disc_resp = kzalloc(DISCOVER_RESP_SIZE, GFP_KERNEL); | |
1413 | if (!disc_resp) | |
1414 | return -ENOMEM; | |
1415 | ||
1416 | disc_req = kzalloc(DISCOVER_REQ_SIZE, GFP_KERNEL); | |
1417 | if (disc_req) { | |
1418 | disc_req[0] = SMP_REQUEST; | |
1419 | disc_req[1] = SMP_DISCOVER; | |
1420 | disc_req[9] = phy_id; | |
1421 | } else { | |
1422 | kfree(disc_resp); | |
1423 | return -ENOMEM; | |
1424 | } | |
1425 | res = isci_smp_execute_task(ihost, dev, disc_req, DISCOVER_REQ_SIZE, | |
1426 | disc_resp, DISCOVER_RESP_SIZE); | |
1427 | if (!res) { | |
1428 | if (disc_resp->result != SMP_RESP_FUNC_ACC) | |
1429 | res = disc_resp->result; | |
1430 | else | |
1431 | *adt = disc_resp->disc.attached_dev_type; | |
1432 | } | |
1433 | kfree(disc_req); | |
1434 | kfree(disc_resp); | |
1435 | ||
1436 | return res; | |
1437 | } | |
1438 | ||
1439 | static void isci_wait_for_smp_phy_reset(struct isci_remote_device *idev, int phy_num) | |
1440 | { | |
1441 | struct domain_device *dev = idev->domain_dev; | |
1442 | struct isci_port *iport = idev->isci_port; | |
1443 | struct isci_host *ihost = iport->isci_host; | |
1444 | int res, iteration = 0, attached_device_type; | |
1445 | #define STP_WAIT_MSECS 25000 | |
1446 | unsigned long tmo = msecs_to_jiffies(STP_WAIT_MSECS); | |
1447 | unsigned long deadline = jiffies + tmo; | |
1448 | enum { | |
1449 | SMP_PHYWAIT_PHYDOWN, | |
1450 | SMP_PHYWAIT_PHYUP, | |
1451 | SMP_PHYWAIT_DONE | |
1452 | } phy_state = SMP_PHYWAIT_PHYDOWN; | |
1453 | ||
1454 | /* While there is time, wait for the phy to go away and come back */ | |
1455 | while (time_is_after_jiffies(deadline) && phy_state != SMP_PHYWAIT_DONE) { | |
1456 | int event = atomic_read(&iport->event); | |
1457 | ||
1458 | ++iteration; | |
1459 | ||
1460 | tmo = wait_event_timeout(ihost->eventq, | |
1461 | event != atomic_read(&iport->event) || | |
1462 | !test_bit(IPORT_BCN_BLOCKED, &iport->flags), | |
1463 | tmo); | |
1464 | /* link down, stop polling */ | |
1465 | if (!test_bit(IPORT_BCN_BLOCKED, &iport->flags)) | |
1466 | break; | |
1467 | ||
1468 | dev_dbg(&ihost->pdev->dev, | |
1469 | "%s: iport %p, iteration %d," | |
1470 | " phase %d: time_remaining %lu, bcns = %d\n", | |
1471 | __func__, iport, iteration, phy_state, | |
1472 | tmo, test_bit(IPORT_BCN_PENDING, &iport->flags)); | |
1473 | ||
1474 | res = isci_smp_get_phy_attached_dev_type(ihost, dev, phy_num, | |
1475 | &attached_device_type); | |
1476 | tmo = deadline - jiffies; | |
1477 | ||
1478 | if (res) { | |
1479 | dev_warn(&ihost->pdev->dev, | |
1480 | "%s: iteration %d, phase %d:" | |
1481 | " SMP error=%d, time_remaining=%lu\n", | |
1482 | __func__, iteration, phy_state, res, tmo); | |
1483 | break; | |
1484 | } | |
1485 | dev_dbg(&ihost->pdev->dev, | |
1486 | "%s: iport %p, iteration %d," | |
1487 | " phase %d: time_remaining %lu, bcns = %d, " | |
1488 | "attdevtype = %x\n", | |
1489 | __func__, iport, iteration, phy_state, | |
1490 | tmo, test_bit(IPORT_BCN_PENDING, &iport->flags), | |
1491 | attached_device_type); | |
1492 | ||
1493 | switch (phy_state) { | |
1494 | case SMP_PHYWAIT_PHYDOWN: | |
1495 | /* Has the device gone away? */ | |
1496 | if (!attached_device_type) | |
1497 | phy_state = SMP_PHYWAIT_PHYUP; | |
1498 | ||
1499 | break; | |
1500 | ||
1501 | case SMP_PHYWAIT_PHYUP: | |
1502 | /* Has the device come back? */ | |
1503 | if (attached_device_type) | |
1504 | phy_state = SMP_PHYWAIT_DONE; | |
1505 | break; | |
1506 | ||
1507 | case SMP_PHYWAIT_DONE: | |
1508 | break; | |
1509 | } | |
1510 | ||
1511 | } | |
1512 | dev_dbg(&ihost->pdev->dev, "%s: done\n", __func__); | |
1513 | } | |
1514 | ||
209fae14 DW |
1515 | static int isci_reset_device(struct isci_host *ihost, |
1516 | struct isci_remote_device *idev, int hard_reset) | |
6f231dda | 1517 | { |
209fae14 | 1518 | struct sas_phy *phy = sas_find_local_phy(idev->domain_dev); |
61aaff49 | 1519 | struct isci_port *iport = idev->isci_port; |
6f231dda | 1520 | enum sci_status status; |
d06b487b DW |
1521 | unsigned long flags; |
1522 | int rc; | |
6f231dda | 1523 | |
d06b487b | 1524 | dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev); |
6f231dda | 1525 | |
d06b487b DW |
1526 | spin_lock_irqsave(&ihost->scic_lock, flags); |
1527 | status = scic_remote_device_reset(&idev->sci); | |
6f231dda | 1528 | if (status != SCI_SUCCESS) { |
d06b487b | 1529 | spin_unlock_irqrestore(&ihost->scic_lock, flags); |
6f231dda | 1530 | |
d06b487b DW |
1531 | dev_warn(&ihost->pdev->dev, |
1532 | "%s: scic_remote_device_reset(%p) returned %d!\n", | |
1533 | __func__, idev, status); | |
6f231dda DW |
1534 | |
1535 | return TMF_RESP_FUNC_FAILED; | |
1536 | } | |
d06b487b | 1537 | spin_unlock_irqrestore(&ihost->scic_lock, flags); |
6f231dda | 1538 | |
6f231dda | 1539 | /* Make sure all pending requests are able to be fully terminated. */ |
d06b487b | 1540 | isci_device_clear_reset_pending(ihost, idev); |
6f231dda | 1541 | |
61aaff49 JS |
1542 | /* If this is a device on an expander, disable BCN processing. */ |
1543 | if (!scsi_is_sas_phy_local(phy)) | |
1544 | set_bit(IPORT_BCN_BLOCKED, &iport->flags); | |
1545 | ||
d06b487b | 1546 | rc = sas_phy_reset(phy, hard_reset); |
6f231dda | 1547 | |
d06b487b DW |
1548 | /* Terminate in-progress I/O now. */ |
1549 | isci_remote_device_nuke_requests(ihost, idev); | |
6f231dda | 1550 | |
ff717ab0 | 1551 | /* Since all pending TCs have been cleaned, resume the RNC. */ |
d06b487b DW |
1552 | spin_lock_irqsave(&ihost->scic_lock, flags); |
1553 | status = scic_remote_device_reset_complete(&idev->sci); | |
1554 | spin_unlock_irqrestore(&ihost->scic_lock, flags); | |
6f231dda | 1555 | |
61aaff49 JS |
1556 | /* If this is a device on an expander, bring the phy back up. */ |
1557 | if (!scsi_is_sas_phy_local(phy)) { | |
1558 | /* A phy reset will cause the device to go away then reappear. | |
1559 | * Since libsas will take action on incoming BCNs (eg. remove | |
1560 | * a device going through an SMP phy-control driven reset), | |
1561 | * we need to wait until the phy comes back up before letting | |
1562 | * discovery proceed in libsas. | |
1563 | */ | |
1564 | isci_wait_for_smp_phy_reset(idev, phy->number); | |
1565 | ||
1566 | spin_lock_irqsave(&ihost->scic_lock, flags); | |
1567 | isci_port_bcn_enable(ihost, idev->isci_port); | |
1568 | spin_unlock_irqrestore(&ihost->scic_lock, flags); | |
1569 | } | |
ff717ab0 | 1570 | |
d06b487b DW |
1571 | if (status != SCI_SUCCESS) { |
1572 | dev_warn(&ihost->pdev->dev, | |
1573 | "%s: scic_remote_device_reset_complete(%p) " | |
1574 | "returned %d!\n", __func__, idev, status); | |
6f231dda DW |
1575 | } |
1576 | ||
d06b487b | 1577 | dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev); |
6f231dda | 1578 | |
d06b487b DW |
1579 | return rc; |
1580 | } | |
6f231dda | 1581 | |
d06b487b DW |
1582 | int isci_task_I_T_nexus_reset(struct domain_device *dev) |
1583 | { | |
1584 | struct isci_host *ihost = dev_to_ihost(dev); | |
d06b487b | 1585 | struct isci_remote_device *idev; |
209fae14 | 1586 | int ret, hard_reset = 1; |
d06b487b DW |
1587 | unsigned long flags; |
1588 | ||
d06b487b | 1589 | spin_lock_irqsave(&ihost->scic_lock, flags); |
209fae14 | 1590 | idev = isci_lookup_device(dev); |
d06b487b DW |
1591 | spin_unlock_irqrestore(&ihost->scic_lock, flags); |
1592 | ||
209fae14 DW |
1593 | if (!idev || !test_bit(IDEV_EH, &idev->flags)) { |
1594 | ret = TMF_RESP_FUNC_COMPLETE; | |
1595 | goto out; | |
1596 | } | |
d06b487b DW |
1597 | |
1598 | if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) | |
1599 | hard_reset = 0; | |
1600 | ||
209fae14 DW |
1601 | ret = isci_reset_device(ihost, idev, hard_reset); |
1602 | out: | |
1603 | isci_put_device(idev); | |
1604 | return ret; | |
d06b487b DW |
1605 | } |
1606 | ||
1607 | int isci_bus_reset_handler(struct scsi_cmnd *cmd) | |
1608 | { | |
1609 | struct domain_device *dev = sdev_to_domain_dev(cmd->device); | |
209fae14 DW |
1610 | struct isci_host *ihost = dev_to_ihost(dev); |
1611 | struct isci_remote_device *idev; | |
1612 | int ret, hard_reset = 1; | |
1613 | unsigned long flags; | |
d06b487b DW |
1614 | |
1615 | if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) | |
1616 | hard_reset = 0; | |
6f231dda | 1617 | |
209fae14 DW |
1618 | spin_lock_irqsave(&ihost->scic_lock, flags); |
1619 | idev = isci_lookup_device(dev); | |
1620 | spin_unlock_irqrestore(&ihost->scic_lock, flags); | |
1621 | ||
1622 | if (!idev) { | |
1623 | ret = TMF_RESP_FUNC_COMPLETE; | |
1624 | goto out; | |
1625 | } | |
1626 | ||
1627 | ret = isci_reset_device(ihost, idev, hard_reset); | |
1628 | out: | |
1629 | isci_put_device(idev); | |
1630 | return ret; | |
6f231dda | 1631 | } |