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