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