task.c 48.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/*
 * This file is provided under a dual BSD/GPLv2 license.  When using or
 * redistributing this file, you may do so under either license.
 *
 * GPL LICENSE SUMMARY
 *
 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 * The full GNU General Public License is included in this distribution
 * in the file called LICENSE.GPL.
 *
 * BSD LICENSE
 *
 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *   * Neither the name of Intel Corporation nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <linux/completion.h>
57
#include <linux/irqflags.h>
58
#include "sas.h"
59
#include <scsi/libsas.h>
60 61
#include "remote_device.h"
#include "remote_node_context.h"
62 63 64
#include "isci.h"
#include "request.h"
#include "task.h"
65
#include "host.h"
66

67
/**
68 69 70 71 72 73
* isci_task_refuse() - complete the request to the upper layer driver in
*     the case where an I/O needs to be completed back in the submit path.
* @ihost: host on which the the request was queued
* @task: request to complete
* @response: response code for the completed task.
* @status: status code for the completed task.
74 75
*
*/
76 77 78 79
static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
			     enum service_response response,
			     enum exec_status status)

80
{
81
	enum isci_completion_selection disposition;
82

83 84 85
	disposition = isci_perform_normal_io_completion;
	disposition = isci_task_set_completion_status(task, response, status,
						      disposition);
86 87

	/* Tasks aborted specifically by a call to the lldd_abort_task
88 89 90
	 * function should not be completed to the host in the regular path.
	 */
	switch (disposition) {
91 92
		case isci_perform_normal_io_completion:
			/* Normal notification (task_done) */
93
			dev_dbg(&ihost->pdev->dev,
94 95
				"%s: Normal - task = %p, response=%d, "
				"status=%d\n",
96 97
				__func__, task, response, status);

98
			task->lldd_task = NULL;
99

100
			isci_execpath_callback(ihost, task, task->task_done);
101 102 103 104 105 106
			break;

		case isci_perform_aborted_io_completion:
			/* No notification because this request is already in the
			* abort path.
			*/
D
Dan Williams 已提交
107
			dev_dbg(&ihost->pdev->dev,
108 109
				 "%s: Aborted - task = %p, response=%d, "
				"status=%d\n",
110 111 112 113 114
				 __func__, task, response, status);
			break;

		case isci_perform_error_io_completion:
			/* Use sas_task_abort */
D
Dan Williams 已提交
115
			dev_dbg(&ihost->pdev->dev,
116 117
				 "%s: Error - task = %p, response=%d, "
				"status=%d\n",
118
				 __func__, task, response, status);
119 120

			isci_execpath_callback(ihost, task, sas_task_abort);
121 122 123
			break;

		default:
D
Dan Williams 已提交
124
			dev_dbg(&ihost->pdev->dev,
125 126 127 128 129 130
				 "%s: isci task notification default case!",
				 __func__);
			sas_task_abort(task);
			break;
	}
}
131

132 133 134 135
#define for_each_sas_task(num, task) \
	for (; num > 0; num--,\
	     task = list_entry(task->list.next, struct sas_task, list))

136 137 138 139 140 141 142 143 144

static inline int isci_device_io_ready(struct isci_remote_device *idev,
				       struct sas_task *task)
{
	return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
		      (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
		       isci_task_is_ncq_recovery(task))
		    : 0;
}
145 146 147 148 149 150 151 152 153 154 155 156
/**
 * isci_task_execute_task() - This function is one of the SAS Domain Template
 *    functions. This function is called by libsas to send a task down to
 *    hardware.
 * @task: This parameter specifies the SAS task to send.
 * @num: This parameter specifies the number of tasks to queue.
 * @gfp_flags: This parameter specifies the context of this call.
 *
 * status, zero indicates success.
 */
int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
{
157
	struct isci_host *ihost = dev_to_ihost(task->dev);
158
	struct isci_remote_device *idev;
159
	unsigned long flags;
160
	bool io_ready;
161
	u16 tag;
162

163
	dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
164

165
	for_each_sas_task(num, task) {
166 167
		enum sci_status status = SCI_FAILURE;

168 169
		spin_lock_irqsave(&ihost->scic_lock, flags);
		idev = isci_lookup_device(task->dev);
170
		io_ready = isci_device_io_ready(idev, task);
171
		tag = isci_alloc_tag(ihost);
172
		spin_unlock_irqrestore(&ihost->scic_lock, flags);
173

174 175 176 177
		dev_dbg(&ihost->pdev->dev,
			"task: %p, num: %d dev: %p idev: %p:%#lx cmd = %p\n",
			task, num, task->dev, idev, idev ? idev->flags : 0,
			task->uldd_task);
178

179 180 181
		if (!idev) {
			isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
					 SAS_DEVICE_UNKNOWN);
182
		} else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
183 184 185 186 187
			/* Indicate QUEUE_FULL so that the scsi midlayer
			 * retries.
			  */
			isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
					 SAS_QUEUE_FULL);
188 189 190
		} else {
			/* There is a device and it's ready for I/O. */
			spin_lock_irqsave(&task->task_state_lock, flags);
191

192
			if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
193
				/* The I/O was aborted. */
194 195
				spin_unlock_irqrestore(&task->task_state_lock,
						       flags);
196

197 198 199
				isci_task_refuse(ihost, task,
						 SAS_TASK_UNDELIVERED,
						 SAM_STAT_TASK_ABORTED);
200
			} else {
201 202
				task->task_state_flags |= SAS_TASK_AT_INITIATOR;
				spin_unlock_irqrestore(&task->task_state_lock, flags);
203 204

				/* build and send the request. */
D
Dan Williams 已提交
205
				status = isci_request_execute(ihost, idev, task, tag);
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

				if (status != SCI_SUCCESS) {

					spin_lock_irqsave(&task->task_state_lock, flags);
					/* Did not really start this command. */
					task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
					spin_unlock_irqrestore(&task->task_state_lock, flags);

					/* Indicate QUEUE_FULL so that the scsi
					* midlayer retries. if the request
					* failed for remote device reasons,
					* it gets returned as
					* SAS_TASK_UNDELIVERED next time
					* through.
					*/
221 222 223
					isci_task_refuse(ihost, task,
							 SAS_TASK_COMPLETE,
							 SAS_QUEUE_FULL);
224
				}
225 226
			}
		}
227 228 229 230 231 232 233 234
		if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
			spin_lock_irqsave(&ihost->scic_lock, flags);
			/* command never hit the device, so just free
			 * the tci and skip the sequence increment
			 */
			isci_tci_free(ihost, ISCI_TAG_TCI(tag));
			spin_unlock_irqrestore(&ihost->scic_lock, flags);
		}
235
		isci_put_device(idev);
236
	}
237 238 239
	return 0;
}

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
static enum sci_status isci_sata_management_task_request_build(struct isci_request *ireq)
{
	struct isci_tmf *isci_tmf;
	enum sci_status status;

	if (tmf_task != ireq->ttype)
		return SCI_FAILURE;

	isci_tmf = isci_request_access_tmf(ireq);

	switch (isci_tmf->tmf_code) {

	case isci_tmf_sata_srst_high:
	case isci_tmf_sata_srst_low: {
		struct host_to_dev_fis *fis = &ireq->stp.cmd;

		memset(fis, 0, sizeof(*fis));

		fis->fis_type  =  0x27;
		fis->flags     &= ~0x80;
		fis->flags     &= 0xF0;
		if (isci_tmf->tmf_code == isci_tmf_sata_srst_high)
			fis->control |= ATA_SRST;
		else
			fis->control &= ~ATA_SRST;
		break;
	}
	/* other management commnd go here... */
	default:
		return SCI_FAILURE;
	}

	/* core builds the protocol specific request
	 *  based on the h2d fis.
	 */
	status = sci_task_request_construct_sata(ireq);

	return status;
}

D
Dan Williams 已提交
280
static struct isci_request *isci_task_request_build(struct isci_host *ihost,
281
						    struct isci_remote_device *idev,
282
						    u16 tag, struct isci_tmf *isci_tmf)
283 284
{
	enum sci_status status = SCI_FAILURE;
D
Dan Williams 已提交
285
	struct isci_request *ireq = NULL;
286
	struct domain_device *dev;
287

D
Dan Williams 已提交
288
	dev_dbg(&ihost->pdev->dev,
289 290
		"%s: isci_tmf = %p\n", __func__, isci_tmf);

D
Dan Williams 已提交
291
	dev = idev->domain_dev;
292 293

	/* do common allocation and init of request object. */
D
Dan Williams 已提交
294
	ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
D
Dan Williams 已提交
295 296
	if (!ireq)
		return NULL;
297 298

	/* let the core do it's construct. */
299
	status = sci_task_request_construct(ihost, idev, tag,
300
					     ireq);
301 302

	if (status != SCI_SUCCESS) {
D
Dan Williams 已提交
303
		dev_warn(&ihost->pdev->dev,
304
			 "%s: sci_task_request_construct failed - "
305 306 307
			 "status = 0x%x\n",
			 __func__,
			 status);
D
Dan Williams 已提交
308
		return NULL;
309 310
	}

311 312
	/* XXX convert to get this from task->tproto like other drivers */
	if (dev->dev_type == SAS_END_DEV) {
313
		isci_tmf->proto = SAS_PROTOCOL_SSP;
314
		status = sci_task_request_construct_ssp(ireq);
315
		if (status != SCI_SUCCESS)
D
Dan Williams 已提交
316
			return NULL;
317 318
	}

319
	if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
320
		isci_tmf->proto = SAS_PROTOCOL_SATA;
D
Dan Williams 已提交
321
		status = isci_sata_management_task_request_build(ireq);
322 323

		if (status != SCI_SUCCESS)
D
Dan Williams 已提交
324
			return NULL;
325
	}
D
Dan Williams 已提交
326
	return ireq;
327 328
}

329 330 331
static int isci_task_execute_tmf(struct isci_host *ihost,
				 struct isci_remote_device *idev,
				 struct isci_tmf *tmf, unsigned long timeout_ms)
332 333
{
	DECLARE_COMPLETION_ONSTACK(completion);
334
	enum sci_task_status status = SCI_TASK_FAILURE;
D
Dan Williams 已提交
335
	struct isci_request *ireq;
336 337
	int ret = TMF_RESP_FUNC_FAILED;
	unsigned long flags;
E
Edmund Nadolski 已提交
338
	unsigned long timeleft;
339 340 341 342 343 344 345 346
	u16 tag;

	spin_lock_irqsave(&ihost->scic_lock, flags);
	tag = isci_alloc_tag(ihost);
	spin_unlock_irqrestore(&ihost->scic_lock, flags);

	if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
		return ret;
347 348 349 350

	/* sanity check, return TMF_RESP_FUNC_FAILED
	 * if the device is not there and ready.
	 */
351 352 353
	if (!idev ||
	    (!test_bit(IDEV_IO_READY, &idev->flags) &&
	     !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
D
Dan Williams 已提交
354
		dev_dbg(&ihost->pdev->dev,
355
			"%s: idev = %p not ready (%#lx)\n",
356
			__func__,
357
			idev, idev ? idev->flags : 0);
358
		goto err_tci;
359
	} else
D
Dan Williams 已提交
360
		dev_dbg(&ihost->pdev->dev,
361 362
			"%s: idev = %p\n",
			__func__, idev);
363 364 365 366

	/* Assign the pointer to the TMF's completion kernel wait structure. */
	tmf->complete = &completion;

367
	ireq = isci_task_request_build(ihost, idev, tag, tmf);
368 369
	if (!ireq)
		goto err_tci;
370

D
Dan Williams 已提交
371
	spin_lock_irqsave(&ihost->scic_lock, flags);
372 373

	/* start the TMF io. */
374
	status = sci_controller_start_task(ihost, idev, ireq);
375

376
	if (status != SCI_TASK_SUCCESS) {
D
Dan Williams 已提交
377
		dev_dbg(&ihost->pdev->dev,
378 379 380
			 "%s: start_io failed - status = 0x%x, request = %p\n",
			 __func__,
			 status,
D
Dan Williams 已提交
381 382
			 ireq);
		spin_unlock_irqrestore(&ihost->scic_lock, flags);
D
Dan Williams 已提交
383
		goto err_tci;
384 385 386 387 388
	}

	if (tmf->cb_state_func != NULL)
		tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);

D
Dan Williams 已提交
389
	isci_request_change_state(ireq, started);
390 391

	/* add the request to the remote device request list. */
392
	list_add(&ireq->dev_node, &idev->reqs_in_process);
393

D
Dan Williams 已提交
394
	spin_unlock_irqrestore(&ihost->scic_lock, flags);
395 396

	/* Wait for the TMF to complete, or a timeout. */
E
Edmund Nadolski 已提交
397
	timeleft = wait_for_completion_timeout(&completion,
398
					       msecs_to_jiffies(timeout_ms));
E
Edmund Nadolski 已提交
399 400

	if (timeleft == 0) {
D
Dan Williams 已提交
401
		spin_lock_irqsave(&ihost->scic_lock, flags);
E
Edmund Nadolski 已提交
402 403 404 405

		if (tmf->cb_state_func != NULL)
			tmf->cb_state_func(isci_tmf_timed_out, tmf, tmf->cb_data);

406
		sci_controller_terminate_request(ihost,
407
						  idev,
408
						  ireq);
E
Edmund Nadolski 已提交
409

D
Dan Williams 已提交
410
		spin_unlock_irqrestore(&ihost->scic_lock, flags);
411 412

		wait_for_completion(tmf->complete);
E
Edmund Nadolski 已提交
413
	}
414 415 416 417 418 419

	isci_print_tmf(tmf);

	if (tmf->status == SCI_SUCCESS)
		ret =  TMF_RESP_FUNC_COMPLETE;
	else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
D
Dan Williams 已提交
420
		dev_dbg(&ihost->pdev->dev,
421 422 423 424 425 426 427
			"%s: tmf.status == "
			"SCI_FAILURE_IO_RESPONSE_VALID\n",
			__func__);
		ret =  TMF_RESP_FUNC_COMPLETE;
	}
	/* Else - leave the default "failed" status alone. */

D
Dan Williams 已提交
428
	dev_dbg(&ihost->pdev->dev,
429 430
		"%s: completed request = %p\n",
		__func__,
D
Dan Williams 已提交
431
		ireq);
432 433

	return ret;
434 435 436 437 438 439 440

 err_tci:
	spin_lock_irqsave(&ihost->scic_lock, flags);
	isci_tci_free(ihost, ISCI_TAG_TCI(tag));
	spin_unlock_irqrestore(&ihost->scic_lock, flags);

	return ret;
441 442
}

443 444 445 446 447 448
static void isci_task_build_tmf(struct isci_tmf *tmf,
				enum isci_tmf_function_codes code,
				void (*tmf_sent_cb)(enum isci_tmf_cb_state,
						    struct isci_tmf *,
						    void *),
				void *cb_data)
449 450 451 452 453
{
	memset(tmf, 0, sizeof(*tmf));

	tmf->tmf_code      = code;
	tmf->cb_state_func = tmf_sent_cb;
454 455
	tmf->cb_data       = cb_data;
}
456

457 458 459 460 461 462
static void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
					   enum isci_tmf_function_codes code,
					   void (*tmf_sent_cb)(enum isci_tmf_cb_state,
							       struct isci_tmf *,
							       void *),
					   struct isci_request *old_request)
463
{
464
	isci_task_build_tmf(tmf, code, tmf_sent_cb, old_request);
465
	tmf->io_tag = old_request->io_tag;
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
}

/**
 * isci_task_validate_request_to_abort() - This function checks the given I/O
 *    against the "started" state.  If the request is still "started", it's
 *    state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
 *    BEFORE CALLING THIS FUNCTION.
 * @isci_request: This parameter specifies the request object to control.
 * @isci_host: This parameter specifies the ISCI host object
 * @isci_device: This is the device to which the request is pending.
 * @aborted_io_completion: This is a completion structure that will be added to
 *    the request in case it is changed to aborting; this completion is
 *    triggered when the request is fully completed.
 *
 * Either "started" on successful change of the task status to "aborted", or
 * "unallocated" if the task cannot be controlled.
 */
static enum isci_request_status isci_task_validate_request_to_abort(
	struct isci_request *isci_request,
	struct isci_host *isci_host,
	struct isci_remote_device *isci_device,
	struct completion *aborted_io_completion)
{
	enum isci_request_status old_state = unallocated;

	/* Only abort the task if it's in the
	 *  device's request_in_process list
	 */
	if (isci_request && !list_empty(&isci_request->dev_node)) {
		old_state = isci_request_change_started_to_aborted(
			isci_request, aborted_io_completion);

	}

	return old_state;
}

503 504 505 506 507 508 509 510
/**
* isci_request_cleanup_completed_loiterer() - This function will take care of
*    the final cleanup on any request which has been explicitly terminated.
* @isci_host: This parameter specifies the ISCI host object
* @isci_device: This is the device to which the request is pending.
* @isci_request: This parameter specifies the terminated request object.
* @task: This parameter is the libsas I/O request.
*/
511
static void isci_request_cleanup_completed_loiterer(
512
	struct isci_host          *isci_host,
513
	struct isci_remote_device *isci_device,
514 515
	struct isci_request       *isci_request,
	struct sas_task           *task)
516
{
517
	unsigned long flags;
518 519 520

	dev_dbg(&isci_host->pdev->dev,
		"%s: isci_device=%p, request=%p, task=%p\n",
521
		__func__, isci_device, isci_request, task);
522

523 524 525 526 527
	if (task != NULL) {

		spin_lock_irqsave(&task->task_state_lock, flags);
		task->lldd_task = NULL;

528 529
		task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;

530 531 532 533 534 535 536 537 538 539
		isci_set_task_doneflags(task);

		/* If this task is not in the abort path, call task_done. */
		if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {

			spin_unlock_irqrestore(&task->task_state_lock, flags);
			task->task_done(task);
		} else
			spin_unlock_irqrestore(&task->task_state_lock, flags);
	}
540

541 542 543 544
	if (isci_request != NULL) {
		spin_lock_irqsave(&isci_host->scic_lock, flags);
		list_del_init(&isci_request->dev_node);
		spin_unlock_irqrestore(&isci_host->scic_lock, flags);
545 546 547
	}
}

548 549 550 551 552
/**
 * isci_terminate_request_core() - This function will terminate the given
 *    request, and wait for it to complete.  This function must only be called
 *    from a thread that can wait.  Note that the request is terminated and
 *    completed (back to the host, if started there).
553
 * @ihost: This SCU.
554
 * @idev: The target.
555 556 557
 * @isci_request: The I/O request to be terminated.
 *
 */
558 559 560
static void isci_terminate_request_core(struct isci_host *ihost,
					struct isci_remote_device *idev,
					struct isci_request *isci_request)
561
{
562
	enum sci_status status      = SCI_SUCCESS;
563 564 565
	bool was_terminated         = false;
	bool needs_cleanup_handling = false;
	enum isci_request_status request_status;
566 567 568 569
	unsigned long     flags;
	unsigned long     termination_completed = 1;
	struct completion *io_request_completion;
	struct sas_task   *task;
570

571
	dev_dbg(&ihost->pdev->dev,
572
		"%s: device = %p; request = %p\n",
573
		__func__, idev, isci_request);
574

575
	spin_lock_irqsave(&ihost->scic_lock, flags);
576

577 578 579 580 581 582
	io_request_completion = isci_request->io_request_completion;

	task = (isci_request->ttype == io_task)
		? isci_request_access_task(isci_request)
		: NULL;

583
	/* Note that we are not going to control
D
Dan Williams 已提交
584 585 586
	 * the target to abort the request.
	 */
	set_bit(IREQ_COMPLETE_IN_TARGET, &isci_request->flags);
587

588 589 590 591
	/* Make sure the request wasn't just sitting around signalling
	 * device condition (if the request handle is NULL, then the
	 * request completed but needed additional handling here).
	 */
D
Dan Williams 已提交
592
	if (!test_bit(IREQ_TERMINATED, &isci_request->flags)) {
593
		was_terminated = true;
594
		needs_cleanup_handling = true;
595
		status = sci_controller_terminate_request(ihost,
596 597
							   idev,
							   isci_request);
598
	}
599
	spin_unlock_irqrestore(&ihost->scic_lock, flags);
600 601 602 603 604 605

	/*
	 * The only time the request to terminate will
	 * fail is when the io request is completed and
	 * being aborted.
	 */
606
	if (status != SCI_SUCCESS) {
D
Dan Williams 已提交
607
		dev_dbg(&ihost->pdev->dev,
608
			"%s: sci_controller_terminate_request"
609
			" returned = 0x%x\n",
610 611
			__func__, status);

612 613 614
		isci_request->io_request_completion = NULL;

	} else {
615
		if (was_terminated) {
616
			dev_dbg(&ihost->pdev->dev,
617 618
				"%s: before completion wait (%p/%p)\n",
				__func__, isci_request, io_request_completion);
619 620

			/* Wait here for the request to complete. */
621 622
			#define TERMINATION_TIMEOUT_MSEC 500
			termination_completed
623
				= wait_for_completion_timeout(
624
				   io_request_completion,
625 626
				   msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));

627 628 629
			if (!termination_completed) {

				/* The request to terminate has timed out.  */
630
				spin_lock_irqsave(&ihost->scic_lock,
631 632 633
						  flags);

				/* Check for state changes. */
D
Dan Williams 已提交
634
				if (!test_bit(IREQ_TERMINATED, &isci_request->flags)) {
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659

					/* The best we can do is to have the
					 * request die a silent death if it
					 * ever really completes.
					 *
					 * Set the request state to "dead",
					 * and clear the task pointer so that
					 * an actual completion event callback
					 * doesn't do anything.
					 */
					isci_request->status = dead;
					isci_request->io_request_completion
						= NULL;

					if (isci_request->ttype == io_task) {

						/* Break links with the
						* sas_task.
						*/
						isci_request->ttype_ptr.io_task_ptr
							= NULL;
					}
				} else
					termination_completed = 1;

660
				spin_unlock_irqrestore(&ihost->scic_lock,
661
						       flags);
662

663
				if (!termination_completed) {
664

D
Dan Williams 已提交
665
					dev_dbg(&ihost->pdev->dev,
666 667 668 669
						"%s: *** Timeout waiting for "
						"termination(%p/%p)\n",
						__func__, io_request_completion,
						isci_request);
670

671 672 673 674 675 676 677 678
					/* The request can no longer be referenced
					 * safely since it may go away if the
					 * termination every really does complete.
					 */
					isci_request = NULL;
				}
			}
			if (termination_completed)
679
				dev_dbg(&ihost->pdev->dev,
680 681
					"%s: after completion wait (%p/%p)\n",
					__func__, isci_request, io_request_completion);
682 683
		}

684
		if (termination_completed) {
685

686
			isci_request->io_request_completion = NULL;
687

688 689 690 691 692
			/* Peek at the status of the request.  This will tell
			 * us if there was special handling on the request such that it
			 * needs to be detached and freed here.
			 */
			spin_lock_irqsave(&isci_request->state_lock, flags);
D
Dan Williams 已提交
693
			request_status = isci_request->status;
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710

			if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
			    && ((request_status == aborted)
				|| (request_status == aborting)
				|| (request_status == terminating)
				|| (request_status == completed)
				|| (request_status == dead)
				)
			    ) {

				/* The completion routine won't free a request in
				 * the aborted/aborting/etc. states, so we do
				 * it here.
				 */
				needs_cleanup_handling = true;
			}
			spin_unlock_irqrestore(&isci_request->state_lock, flags);
711

712 713 714
		}
		if (needs_cleanup_handling)
			isci_request_cleanup_completed_loiterer(
715
				ihost, idev, isci_request, task);
716
	}
717 718 719 720 721 722 723 724 725
}

/**
 * isci_terminate_pending_requests() - This function will change the all of the
 *    requests on the given device's state to "aborting", will terminate the
 *    requests, and wait for them to complete.  This function must only be
 *    called from a thread that can wait.  Note that the requests are all
 *    terminated and completed (back to the host, if started there).
 * @isci_host: This parameter specifies SCU.
726
 * @idev: This parameter specifies the target.
727 728
 *
 */
729 730
void isci_terminate_pending_requests(struct isci_host *ihost,
				     struct isci_remote_device *idev)
731
{
732
	struct completion request_completion;
733
	enum isci_request_status old_state;
734 735
	unsigned long flags;
	LIST_HEAD(list);
736

737 738
	spin_lock_irqsave(&ihost->scic_lock, flags);
	list_splice_init(&idev->reqs_in_process, &list);
739

740 741 742
	/* assumes that isci_terminate_request_core deletes from the list */
	while (!list_empty(&list)) {
		struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
743

744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
		/* Change state to "terminating" if it is currently
		 * "started".
		 */
		old_state = isci_request_change_started_to_newstate(ireq,
								    &request_completion,
								    terminating);
		switch (old_state) {
		case started:
		case completed:
		case aborting:
			break;
		default:
			/* termination in progress, or otherwise dispositioned.
			 * We know the request was on 'list' so should be safe
			 * to move it back to reqs_in_process
			 */
			list_move(&ireq->dev_node, &idev->reqs_in_process);
			ireq = NULL;
			break;
		}
764

765 766 767
		if (!ireq)
			continue;
		spin_unlock_irqrestore(&ihost->scic_lock, flags);
768

769
		init_completion(&request_completion);
770

771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
		dev_dbg(&ihost->pdev->dev,
			 "%s: idev=%p request=%p; task=%p old_state=%d\n",
			 __func__, idev, ireq,
			ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL,
			old_state);

		/* If the old_state is started:
		 * This request was not already being aborted. If it had been,
		 * then the aborting I/O (ie. the TMF request) would not be in
		 * the aborting state, and thus would be terminated here.  Note
		 * that since the TMF completion's call to the kernel function
		 * "complete()" does not happen until the pending I/O request
		 * terminate fully completes, we do not have to implement a
		 * special wait here for already aborting requests - the
		 * termination of the TMF request will force the request
		 * to finish it's already started terminate.
		 *
		 * If old_state == completed:
		 * This request completed from the SCU hardware perspective
		 * and now just needs cleaning up in terms of freeing the
		 * request and potentially calling up to libsas.
		 *
		 * If old_state == aborting:
		 * This request has already gone through a TMF timeout, but may
		 * not have been terminated; needs cleaning up at least.
		 */
		isci_terminate_request_core(ihost, idev, ireq);
		spin_lock_irqsave(&ihost->scic_lock, flags);
799
	}
800
	spin_unlock_irqrestore(&ihost->scic_lock, flags);
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
}

/**
 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
 *    Template functions.
 * @lun: This parameter specifies the lun to be reset.
 *
 * status, zero indicates success.
 */
static int isci_task_send_lu_reset_sas(
	struct isci_host *isci_host,
	struct isci_remote_device *isci_device,
	u8 *lun)
{
	struct isci_tmf tmf;
	int ret = TMF_RESP_FUNC_FAILED;

	dev_dbg(&isci_host->pdev->dev,
		"%s: isci_host = %p, isci_device = %p\n",
		__func__, isci_host, isci_device);
	/* Send the LUN reset to the target.  By the time the call returns,
	 * the TMF has fully exected in the target (in which case the return
	 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
	 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
	 */
826
	isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset, NULL, NULL);
827 828

	#define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
829
	ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
830 831 832 833 834 835 836 837 838 839 840 841 842

	if (ret == TMF_RESP_FUNC_COMPLETE)
		dev_dbg(&isci_host->pdev->dev,
			"%s: %p: TMF_LU_RESET passed\n",
			__func__, isci_device);
	else
		dev_dbg(&isci_host->pdev->dev,
			"%s: %p: TMF_LU_RESET failed (%x)\n",
			__func__, isci_device, ret);

	return ret;
}

843 844 845 846 847 848 849 850 851 852 853 854 855
static int isci_task_send_lu_reset_sata(struct isci_host *ihost,
				 struct isci_remote_device *idev, u8 *lun)
{
	int ret = TMF_RESP_FUNC_FAILED;
	struct isci_tmf tmf;

	/* Send the soft reset to the target */
	#define ISCI_SRST_TIMEOUT_MS 25000 /* 25 second timeout. */
	isci_task_build_tmf(&tmf, isci_tmf_sata_srst_high, NULL, NULL);

	ret = isci_task_execute_tmf(ihost, idev, &tmf, ISCI_SRST_TIMEOUT_MS);

	if (ret != TMF_RESP_FUNC_COMPLETE) {
D
Dan Williams 已提交
856
		dev_dbg(&ihost->pdev->dev,
857 858 859 860 861 862 863 864 865 866
			 "%s: Assert SRST failed (%p) = %x",
			 __func__, idev, ret);

		/* Return the failure so that the LUN reset is escalated
		 * to a target reset.
		 */
	}
	return ret;
}

867 868 869 870 871 872 873 874 875
/**
 * isci_task_lu_reset() - This function is one of the SAS Domain Template
 *    functions. This is one of the Task Management functoins called by libsas,
 *    to reset the given lun. Note the assumption that while this call is
 *    executing, no I/O will be sent by the host to the device.
 * @lun: This parameter specifies the lun to be reset.
 *
 * status, zero indicates success.
 */
876
int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
877
{
878
	struct isci_host *isci_host = dev_to_ihost(domain_device);
879 880
	struct isci_remote_device *isci_device;
	unsigned long flags;
881 882
	int ret;

883 884 885
	spin_lock_irqsave(&isci_host->scic_lock, flags);
	isci_device = isci_lookup_device(domain_device);
	spin_unlock_irqrestore(&isci_host->scic_lock, flags);
886

887 888
	dev_dbg(&isci_host->pdev->dev,
		"%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
889 890
		 __func__, domain_device, isci_host, isci_device);

891
	if (isci_device)
D
Dan Williams 已提交
892
		set_bit(IDEV_EH, &isci_device->flags);
893 894 895 896 897

	/* If there is a device reset pending on any request in the
	 * device's list, fail this LUN reset request in order to
	 * escalate to the device reset.
	 */
898
	if (!isci_device ||
899
	    isci_device_is_reset_pending(isci_host, isci_device)) {
D
Dan Williams 已提交
900
		dev_dbg(&isci_host->pdev->dev,
901
			 "%s: No dev (%p), or "
902
			 "RESET PENDING: domain_device=%p\n",
903
			 __func__, isci_device, domain_device);
904 905
		ret = TMF_RESP_FUNC_FAILED;
		goto out;
906 907 908 909
	}

	/* Send the task management part of the reset. */
	if (sas_protocol_ata(domain_device->tproto)) {
910
		ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
911 912 913 914 915 916 917
	} else
		ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);

	/* If the LUN reset worked, all the I/O can now be terminated. */
	if (ret == TMF_RESP_FUNC_COMPLETE)
		/* Terminate all I/O now. */
		isci_terminate_pending_requests(isci_host,
918
						isci_device);
919

920 921
 out:
	isci_put_device(isci_device);
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
	return ret;
}


/*	 int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
int isci_task_clear_nexus_port(struct asd_sas_port *port)
{
	return TMF_RESP_FUNC_FAILED;
}



int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
{
	return TMF_RESP_FUNC_FAILED;
}

/* Task Management Functions. Must be called from process context.	 */

/**
 * isci_abort_task_process_cb() - This is a helper function for the abort task
 *    TMF command.  It manages the request state with respect to the successful
 *    transmission / completion of the abort task request.
 * @cb_state: This parameter specifies when this function was called - after
 *    the TMF request has been started and after it has timed-out.
 * @tmf: This parameter specifies the TMF in progress.
 *
 *
 */
static void isci_abort_task_process_cb(
	enum isci_tmf_cb_state cb_state,
	struct isci_tmf *tmf,
	void *cb_data)
{
	struct isci_request *old_request;

	old_request = (struct isci_request *)cb_data;

	dev_dbg(&old_request->isci_host->pdev->dev,
		"%s: tmf=%p, old_request=%p\n",
		__func__, tmf, old_request);

	switch (cb_state) {

	case isci_tmf_started:
		/* The TMF has been started.  Nothing to do here, since the
		 * request state was already set to "aborted" by the abort
		 * task function.
		 */
971 972
		if ((old_request->status != aborted)
			&& (old_request->status != completed))
D
Dan Williams 已提交
973
			dev_dbg(&old_request->isci_host->pdev->dev,
974 975
				"%s: Bad request status (%d): tmf=%p, old_request=%p\n",
				__func__, old_request->status, tmf, old_request);
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
		break;

	case isci_tmf_timed_out:

		/* Set the task's state to "aborting", since the abort task
		 * function thread set it to "aborted" (above) in anticipation
		 * of the task management request working correctly.  Since the
		 * timeout has now fired, the TMF request failed.  We set the
		 * state such that the request completion will indicate the
		 * device is no longer present.
		 */
		isci_request_change_state(old_request, aborting);
		break;

	default:
D
Dan Williams 已提交
991
		dev_dbg(&old_request->isci_host->pdev->dev,
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
			"%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
			__func__, cb_state, tmf, old_request);
		break;
	}
}

/**
 * isci_task_abort_task() - This function is one of the SAS Domain Template
 *    functions. This function is called by libsas to abort a specified task.
 * @task: This parameter specifies the SAS task to abort.
 *
 * status, zero indicates success.
 */
int isci_task_abort_task(struct sas_task *task)
{
1007
	struct isci_host *isci_host = dev_to_ihost(task->dev);
1008
	DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
1009 1010
	struct isci_request       *old_request = NULL;
	enum isci_request_status  old_state;
1011
	struct isci_remote_device *isci_device = NULL;
1012 1013 1014 1015
	struct isci_tmf           tmf;
	int                       ret = TMF_RESP_FUNC_FAILED;
	unsigned long             flags;
	bool                      any_dev_reset = false;
1016 1017 1018 1019 1020 1021

	/* Get the isci_request reference from the task.  Note that
	 * this check does not depend on the pending request list
	 * in the device, because tasks driving resets may land here
	 * after completion in the core.
	 */
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	spin_lock_irqsave(&isci_host->scic_lock, flags);
	spin_lock(&task->task_state_lock);

	old_request = task->lldd_task;

	/* If task is already done, the request isn't valid */
	if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
	    (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
	    old_request)
		isci_device = isci_lookup_device(task->dev);

	spin_unlock(&task->task_state_lock);
	spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1035 1036 1037 1038

	dev_dbg(&isci_host->pdev->dev,
		"%s: task = %p\n", __func__, task);

1039 1040
	if (!isci_device || !old_request)
		goto out;
1041

D
Dan Williams 已提交
1042 1043
	set_bit(IDEV_EH, &isci_device->flags);

1044 1045 1046 1047
	/* This version of the driver will fail abort requests for
	 * SATA/STP.  Failing the abort request this way will cause the
	 * SCSI error handler thread to escalate to LUN reset
	 */
1048
	if (sas_protocol_ata(task->task_proto)) {
D
Dan Williams 已提交
1049
		dev_dbg(&isci_host->pdev->dev,
1050 1051 1052
			    " task %p is for a STP/SATA device;"
			    " returning TMF_RESP_FUNC_FAILED\n"
			    " to cause a LUN reset...\n", task);
1053
		goto out;
1054 1055 1056 1057 1058
	}

	dev_dbg(&isci_host->pdev->dev,
		"%s: old_request == %p\n", __func__, old_request);

1059
	any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
1060

1061 1062
	spin_lock_irqsave(&task->task_state_lock, flags);

1063
	any_dev_reset = any_dev_reset || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
1064 1065 1066 1067 1068 1069

	/* If the extraction of the request reference from the task
	 * failed, then the request has been completed (or if there is a
	 * pending reset then this abort request function must be failed
	 * in order to escalate to the target reset).
	 */
1070
	if ((old_request == NULL) || any_dev_reset) {
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082

		/* If the device reset task flag is set, fail the task
		 * management request.  Otherwise, the original request
		 * has completed.
		 */
		if (any_dev_reset) {

			/* Turn off the task's DONE to make sure this
			 * task is escalated to a target reset.
			 */
			task->task_state_flags &= ~SAS_TASK_STATE_DONE;

1083 1084 1085 1086 1087
			/* Make the reset happen as soon as possible. */
			task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;

			spin_unlock_irqrestore(&task->task_state_lock, flags);

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
			/* Fail the task management request in order to
			 * escalate to the target reset.
			 */
			ret = TMF_RESP_FUNC_FAILED;

			dev_dbg(&isci_host->pdev->dev,
				"%s: Failing task abort in order to "
				"escalate to target reset because\n"
				"SAS_TASK_NEED_DEV_RESET is set for "
				"task %p on dev %p\n",
				__func__, task, isci_device);


1101
		} else {
1102 1103 1104 1105 1106 1107 1108
			/* The request has already completed and there
			 * is nothing to do here other than to set the task
			 * done bit, and indicate that the task abort function
			 * was sucessful.
			 */
			isci_set_task_doneflags(task);

1109
			spin_unlock_irqrestore(&task->task_state_lock, flags);
1110

1111
			ret = TMF_RESP_FUNC_COMPLETE;
1112

1113 1114 1115
			dev_dbg(&isci_host->pdev->dev,
				"%s: abort task not needed for %p\n",
				__func__, task);
1116
		}
1117
		goto out;
1118
	}
1119 1120
	else
		spin_unlock_irqrestore(&task->task_state_lock, flags);
1121 1122 1123

	spin_lock_irqsave(&isci_host->scic_lock, flags);

1124
	/* Check the request status and change to "aborted" if currently
1125
	 * "starting"; if true then set the I/O kernel completion
1126 1127
	 * struct that will be triggered when the request completes.
	 */
1128 1129 1130
	old_state = isci_task_validate_request_to_abort(
				old_request, isci_host, isci_device,
				&aborted_io_completion);
1131 1132 1133
	if ((old_state != started) &&
	    (old_state != completed) &&
	    (old_state != aborting)) {
1134 1135 1136

		spin_unlock_irqrestore(&isci_host->scic_lock, flags);

1137 1138 1139 1140 1141 1142 1143
		/* The request was already being handled by someone else (because
		* they got to set the state away from started).
		*/
		dev_dbg(&isci_host->pdev->dev,
			"%s:  device = %p; old_request %p already being aborted\n",
			__func__,
			isci_device, old_request);
1144 1145
		ret = TMF_RESP_FUNC_COMPLETE;
		goto out;
1146
	}
D
Dan Williams 已提交
1147 1148
	if (task->task_proto == SAS_PROTOCOL_SMP ||
	    test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
1149 1150 1151

		spin_unlock_irqrestore(&isci_host->scic_lock, flags);

1152 1153 1154 1155
		dev_dbg(&isci_host->pdev->dev,
			"%s: SMP request (%d)"
			" or complete_in_target (%d), thus no TMF\n",
			__func__, (task->task_proto == SAS_PROTOCOL_SMP),
D
Dan Williams 已提交
1156
			test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags));
1157

1158 1159 1160 1161 1162 1163
		/* Set the state on the task. */
		isci_task_all_done(task);

		ret = TMF_RESP_FUNC_COMPLETE;

		/* Stopping and SMP devices are not sent a TMF, and are not
1164
		 * reset, but the outstanding I/O request is terminated below.
1165 1166 1167
		 */
	} else {
		/* Fill in the tmf stucture */
1168
		isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
1169 1170
					       isci_abort_task_process_cb,
					       old_request);
1171 1172 1173 1174

		spin_unlock_irqrestore(&isci_host->scic_lock, flags);

		#define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
1175
		ret = isci_task_execute_tmf(isci_host, isci_device, &tmf,
1176 1177
					    ISCI_ABORT_TASK_TIMEOUT_MS);

1178
		if (ret != TMF_RESP_FUNC_COMPLETE)
D
Dan Williams 已提交
1179
			dev_dbg(&isci_host->pdev->dev,
1180 1181 1182
				"%s: isci_task_send_tmf failed\n",
				__func__);
	}
1183
	if (ret == TMF_RESP_FUNC_COMPLETE) {
D
Dan Williams 已提交
1184
		set_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags);
1185

1186 1187 1188
		/* Clean up the request on our side, and wait for the aborted
		 * I/O to complete.
		 */
1189
		isci_terminate_request_core(isci_host, isci_device, old_request);
1190
	}
1191

1192 1193
	/* Make sure we do not leave a reference to aborted_io_completion */
	old_request->io_request_completion = NULL;
1194 1195
 out:
	isci_put_device(isci_device);
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
	return ret;
}

/**
 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
 *    functions. This is one of the Task Management functoins called by libsas,
 *    to abort all task for the given lun.
 * @d_device: This parameter specifies the domain device associated with this
 *    request.
 * @lun: This parameter specifies the lun associated with this request.
 *
 * status, zero indicates success.
 */
int isci_task_abort_task_set(
	struct domain_device *d_device,
	u8 *lun)
{
	return TMF_RESP_FUNC_FAILED;
}


/**
 * isci_task_clear_aca() - This function is one of the SAS Domain Template
 *    functions. This is one of the Task Management functoins called by libsas.
 * @d_device: This parameter specifies the domain device associated with this
 *    request.
 * @lun: This parameter specifies the lun	 associated with this request.
 *
 * status, zero indicates success.
 */
int isci_task_clear_aca(
	struct domain_device *d_device,
	u8 *lun)
{
	return TMF_RESP_FUNC_FAILED;
}



/**
 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
 *    functions. This is one of the Task Management functoins called by libsas.
 * @d_device: This parameter specifies the domain device associated with this
 *    request.
 * @lun: This parameter specifies the lun	 associated with this request.
 *
 * status, zero indicates success.
 */
int isci_task_clear_task_set(
	struct domain_device *d_device,
	u8 *lun)
{
	return TMF_RESP_FUNC_FAILED;
}


/**
 * isci_task_query_task() - This function is implemented to cause libsas to
 *    correctly escalate the failed abort to a LUN or target reset (this is
 *    because sas_scsi_find_task libsas function does not correctly interpret
 *    all return codes from the abort task call).  When TMF_RESP_FUNC_SUCC is
 *    returned, libsas turns this into a LUN reset; when FUNC_FAILED is
 *    returned, libsas will turn this into a target reset
 * @task: This parameter specifies the sas task being queried.
 * @lun: This parameter specifies the lun associated with this request.
 *
 * status, zero indicates success.
 */
int isci_task_query_task(
	struct sas_task *task)
{
	/* See if there is a pending device reset for this device. */
	if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
		return TMF_RESP_FUNC_FAILED;
	else
		return TMF_RESP_FUNC_SUCC;
}

1274
/*
1275 1276
 * isci_task_request_complete() - This function is called by the sci core when
 *    an task request completes.
1277 1278
 * @ihost: This parameter specifies the ISCI host object
 * @ireq: This parameter is the completed isci_request object.
1279 1280 1281 1282 1283
 * @completion_status: This parameter specifies the completion status from the
 *    sci core.
 *
 * none.
 */
1284 1285 1286 1287
void
isci_task_request_complete(struct isci_host *ihost,
			   struct isci_request *ireq,
			   enum sci_task_status completion_status)
1288
{
1289
	struct isci_tmf *tmf = isci_request_access_tmf(ireq);
1290 1291
	struct completion *tmf_complete;

1292
	dev_dbg(&ihost->pdev->dev,
1293
		"%s: request = %p, status=%d\n",
1294
		__func__, ireq, completion_status);
1295

1296
	isci_request_change_state(ireq, completed);
1297 1298

	tmf->status = completion_status;
D
Dan Williams 已提交
1299
	set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
1300

1301
	if (tmf->proto == SAS_PROTOCOL_SSP) {
1302
		memcpy(&tmf->resp.resp_iu,
1303
		       &ireq->ssp.rsp,
1304 1305
		       SSP_RESP_IU_MAX_SIZE);
	} else if (tmf->proto == SAS_PROTOCOL_SATA) {
1306
		memcpy(&tmf->resp.d2h_fis,
1307
		       &ireq->stp.rsp,
1308
		       sizeof(struct dev_to_host_fis));
1309 1310 1311 1312 1313
	}

	/* PRINT_TMF( ((struct isci_tmf *)request->task)); */
	tmf_complete = tmf->complete;

1314
	sci_controller_complete_io(ihost, ireq->target_device, ireq);
1315
	/* set the 'terminated' flag handle to make sure it cannot be terminated
1316 1317
	 *  or completed again.
	 */
D
Dan Williams 已提交
1318
	set_bit(IREQ_TERMINATED, &ireq->flags);
1319

1320 1321
	isci_request_change_state(ireq, unallocated);
	list_del_init(&ireq->dev_node);
1322 1323 1324 1325 1326

	/* The task management part completes last. */
	complete(tmf_complete);
}

1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
static void isci_smp_task_timedout(unsigned long _task)
{
	struct sas_task *task = (void *) _task;
	unsigned long flags;

	spin_lock_irqsave(&task->task_state_lock, flags);
	if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
		task->task_state_flags |= SAS_TASK_STATE_ABORTED;
	spin_unlock_irqrestore(&task->task_state_lock, flags);

	complete(&task->completion);
}

static void isci_smp_task_done(struct sas_task *task)
{
	if (!del_timer(&task->timer))
		return;
	complete(&task->completion);
}

static struct sas_task *isci_alloc_task(void)
{
	struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL);

	if (task) {
		INIT_LIST_HEAD(&task->list);
		spin_lock_init(&task->task_state_lock);
		task->task_state_flags = SAS_TASK_STATE_PENDING;
		init_timer(&task->timer);
		init_completion(&task->completion);
	}

	return task;
}

static void isci_free_task(struct isci_host *ihost, struct sas_task  *task)
{
	if (task) {
		BUG_ON(!list_empty(&task->list));
		kfree(task);
	}
}

static int isci_smp_execute_task(struct isci_host *ihost,
				 struct domain_device *dev, void *req,
				 int req_size, void *resp, int resp_size)
{
	int res, retry;
	struct sas_task *task = NULL;

	for (retry = 0; retry < 3; retry++) {
		task = isci_alloc_task();
		if (!task)
			return -ENOMEM;

		task->dev = dev;
		task->task_proto = dev->tproto;
		sg_init_one(&task->smp_task.smp_req, req, req_size);
		sg_init_one(&task->smp_task.smp_resp, resp, resp_size);

		task->task_done = isci_smp_task_done;

		task->timer.data = (unsigned long) task;
		task->timer.function = isci_smp_task_timedout;
		task->timer.expires = jiffies + 10*HZ;
		add_timer(&task->timer);

		res = isci_task_execute_task(task, 1, GFP_KERNEL);

		if (res) {
			del_timer(&task->timer);
D
Dan Williams 已提交
1398
			dev_dbg(&ihost->pdev->dev,
1399 1400 1401 1402 1403 1404 1405 1406
				"%s: executing SMP task failed:%d\n",
				__func__, res);
			goto ex_err;
		}

		wait_for_completion(&task->completion);
		res = -ECOMM;
		if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
D
Dan Williams 已提交
1407
			dev_dbg(&ihost->pdev->dev,
1408 1409 1410 1411
				"%s: smp task timed out or aborted\n",
				__func__);
			isci_task_abort_task(task);
			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
D
Dan Williams 已提交
1412
				dev_dbg(&ihost->pdev->dev,
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
					"%s: SMP task aborted and not done\n",
					__func__);
				goto ex_err;
			}
		}
		if (task->task_status.resp == SAS_TASK_COMPLETE &&
		    task->task_status.stat == SAM_STAT_GOOD) {
			res = 0;
			break;
		}
		if (task->task_status.resp == SAS_TASK_COMPLETE &&
		      task->task_status.stat == SAS_DATA_UNDERRUN) {
			/* no error, but return the number of bytes of
			* underrun */
			res = task->task_status.residual;
			break;
		}
		if (task->task_status.resp == SAS_TASK_COMPLETE &&
		      task->task_status.stat == SAS_DATA_OVERRUN) {
			res = -EMSGSIZE;
			break;
		} else {
D
Dan Williams 已提交
1435
			dev_dbg(&ihost->pdev->dev,
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
				"%s: task to dev %016llx response: 0x%x "
				"status 0x%x\n", __func__,
				SAS_ADDR(dev->sas_addr),
				task->task_status.resp,
				task->task_status.stat);
			isci_free_task(ihost, task);
			task = NULL;
		}
	}
ex_err:
	BUG_ON(retry == 3 && task != NULL);
	isci_free_task(ihost, task);
	return res;
}

#define DISCOVER_REQ_SIZE  16
#define DISCOVER_RESP_SIZE 56

int isci_smp_get_phy_attached_dev_type(struct isci_host *ihost,
				       struct domain_device *dev,
				       int phy_id, int *adt)
{
	struct smp_resp *disc_resp;
	u8 *disc_req;
	int res;

	disc_resp = kzalloc(DISCOVER_RESP_SIZE, GFP_KERNEL);
	if (!disc_resp)
		return -ENOMEM;

	disc_req = kzalloc(DISCOVER_REQ_SIZE, GFP_KERNEL);
	if (disc_req) {
		disc_req[0] = SMP_REQUEST;
		disc_req[1] = SMP_DISCOVER;
		disc_req[9] = phy_id;
	} else {
		kfree(disc_resp);
		return -ENOMEM;
	}
	res = isci_smp_execute_task(ihost, dev, disc_req, DISCOVER_REQ_SIZE,
				    disc_resp, DISCOVER_RESP_SIZE);
	if (!res) {
		if (disc_resp->result != SMP_RESP_FUNC_ACC)
			res = disc_resp->result;
		else
			*adt = disc_resp->disc.attached_dev_type;
	}
	kfree(disc_req);
	kfree(disc_resp);

	return res;
}

static void isci_wait_for_smp_phy_reset(struct isci_remote_device *idev, int phy_num)
{
	struct domain_device *dev = idev->domain_dev;
	struct isci_port *iport = idev->isci_port;
	struct isci_host *ihost = iport->isci_host;
	int res, iteration = 0, attached_device_type;
	#define STP_WAIT_MSECS 25000
	unsigned long tmo = msecs_to_jiffies(STP_WAIT_MSECS);
	unsigned long deadline = jiffies + tmo;
	enum {
		SMP_PHYWAIT_PHYDOWN,
		SMP_PHYWAIT_PHYUP,
		SMP_PHYWAIT_DONE
	} phy_state = SMP_PHYWAIT_PHYDOWN;

	/* While there is time, wait for the phy to go away and come back */
	while (time_is_after_jiffies(deadline) && phy_state != SMP_PHYWAIT_DONE) {
		int event = atomic_read(&iport->event);

		++iteration;

		tmo = wait_event_timeout(ihost->eventq,
					 event != atomic_read(&iport->event) ||
					 !test_bit(IPORT_BCN_BLOCKED, &iport->flags),
					 tmo);
		/* link down, stop polling */
		if (!test_bit(IPORT_BCN_BLOCKED, &iport->flags))
			break;

		dev_dbg(&ihost->pdev->dev,
			"%s: iport %p, iteration %d,"
			" phase %d: time_remaining %lu, bcns = %d\n",
			__func__, iport, iteration, phy_state,
			tmo, test_bit(IPORT_BCN_PENDING, &iport->flags));

		res = isci_smp_get_phy_attached_dev_type(ihost, dev, phy_num,
							 &attached_device_type);
		tmo = deadline - jiffies;

		if (res) {
D
Dan Williams 已提交
1529
			dev_dbg(&ihost->pdev->dev,
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
				 "%s: iteration %d, phase %d:"
				 " SMP error=%d, time_remaining=%lu\n",
				 __func__, iteration, phy_state, res, tmo);
			break;
		}
		dev_dbg(&ihost->pdev->dev,
			"%s: iport %p, iteration %d,"
			" phase %d: time_remaining %lu, bcns = %d, "
			"attdevtype = %x\n",
			__func__, iport, iteration, phy_state,
			tmo, test_bit(IPORT_BCN_PENDING, &iport->flags),
			attached_device_type);

		switch (phy_state) {
		case SMP_PHYWAIT_PHYDOWN:
			/* Has the device gone away? */
			if (!attached_device_type)
				phy_state = SMP_PHYWAIT_PHYUP;

			break;

		case SMP_PHYWAIT_PHYUP:
			/* Has the device come back? */
			if (attached_device_type)
				phy_state = SMP_PHYWAIT_DONE;
			break;

		case SMP_PHYWAIT_DONE:
			break;
		}

	}
	dev_dbg(&ihost->pdev->dev, "%s: done\n",  __func__);
}

1565 1566
static int isci_reset_device(struct isci_host *ihost,
			     struct isci_remote_device *idev, int hard_reset)
1567
{
1568
	struct sas_phy *phy = sas_find_local_phy(idev->domain_dev);
1569
	struct isci_port *iport = idev->isci_port;
1570
	enum sci_status status;
D
Dan Williams 已提交
1571 1572
	unsigned long flags;
	int rc;
1573

D
Dan Williams 已提交
1574
	dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
1575

D
Dan Williams 已提交
1576
	spin_lock_irqsave(&ihost->scic_lock, flags);
1577
	status = sci_remote_device_reset(idev);
1578
	if (status != SCI_SUCCESS) {
D
Dan Williams 已提交
1579
		spin_unlock_irqrestore(&ihost->scic_lock, flags);
1580

D
Dan Williams 已提交
1581
		dev_dbg(&ihost->pdev->dev,
1582
			 "%s: sci_remote_device_reset(%p) returned %d!\n",
D
Dan Williams 已提交
1583
			 __func__, idev, status);
1584 1585 1586

		return TMF_RESP_FUNC_FAILED;
	}
D
Dan Williams 已提交
1587
	spin_unlock_irqrestore(&ihost->scic_lock, flags);
1588 1589

	/* Make sure all pending requests are able to be fully terminated. */
D
Dan Williams 已提交
1590
	isci_device_clear_reset_pending(ihost, idev);
1591

1592 1593 1594 1595
	/* If this is a device on an expander, disable BCN processing. */
	if (!scsi_is_sas_phy_local(phy))
		set_bit(IPORT_BCN_BLOCKED, &iport->flags);

D
Dan Williams 已提交
1596
	rc = sas_phy_reset(phy, hard_reset);
1597

D
Dan Williams 已提交
1598 1599
	/* Terminate in-progress I/O now. */
	isci_remote_device_nuke_requests(ihost, idev);
1600

1601
	/* Since all pending TCs have been cleaned, resume the RNC. */
D
Dan Williams 已提交
1602
	spin_lock_irqsave(&ihost->scic_lock, flags);
1603
	status = sci_remote_device_reset_complete(idev);
D
Dan Williams 已提交
1604
	spin_unlock_irqrestore(&ihost->scic_lock, flags);
1605

1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
	/* If this is a device on an expander, bring the phy back up. */
	if (!scsi_is_sas_phy_local(phy)) {
		/* A phy reset will cause the device to go away then reappear.
		 * Since libsas will take action on incoming BCNs (eg. remove
		 * a device going through an SMP phy-control driven reset),
		 * we need to wait until the phy comes back up before letting
		 * discovery proceed in libsas.
		 */
		isci_wait_for_smp_phy_reset(idev, phy->number);

		spin_lock_irqsave(&ihost->scic_lock, flags);
		isci_port_bcn_enable(ihost, idev->isci_port);
		spin_unlock_irqrestore(&ihost->scic_lock, flags);
	}
1620

D
Dan Williams 已提交
1621
	if (status != SCI_SUCCESS) {
D
Dan Williams 已提交
1622
		dev_dbg(&ihost->pdev->dev,
1623
			 "%s: sci_remote_device_reset_complete(%p) "
D
Dan Williams 已提交
1624
			 "returned %d!\n", __func__, idev, status);
1625 1626
	}

D
Dan Williams 已提交
1627
	dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
1628

D
Dan Williams 已提交
1629 1630
	return rc;
}
1631

D
Dan Williams 已提交
1632 1633 1634 1635
int isci_task_I_T_nexus_reset(struct domain_device *dev)
{
	struct isci_host *ihost = dev_to_ihost(dev);
	struct isci_remote_device *idev;
1636
	int ret, hard_reset = 1;
D
Dan Williams 已提交
1637 1638 1639
	unsigned long flags;

	spin_lock_irqsave(&ihost->scic_lock, flags);
1640
	idev = isci_lookup_device(dev);
D
Dan Williams 已提交
1641 1642
	spin_unlock_irqrestore(&ihost->scic_lock, flags);

1643 1644 1645 1646
	if (!idev || !test_bit(IDEV_EH, &idev->flags)) {
		ret = TMF_RESP_FUNC_COMPLETE;
		goto out;
	}
D
Dan Williams 已提交
1647 1648 1649 1650

	if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
		hard_reset = 0;

1651 1652 1653 1654
	ret = isci_reset_device(ihost, idev, hard_reset);
 out:
	isci_put_device(idev);
	return ret;
D
Dan Williams 已提交
1655 1656 1657 1658 1659
}

int isci_bus_reset_handler(struct scsi_cmnd *cmd)
{
	struct domain_device *dev = sdev_to_domain_dev(cmd->device);
1660 1661 1662 1663
	struct isci_host *ihost = dev_to_ihost(dev);
	struct isci_remote_device *idev;
	int ret, hard_reset = 1;
	unsigned long flags;
D
Dan Williams 已提交
1664 1665 1666

	if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
		hard_reset = 0;
1667

1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
	spin_lock_irqsave(&ihost->scic_lock, flags);
	idev = isci_lookup_device(dev);
	spin_unlock_irqrestore(&ihost->scic_lock, flags);

	if (!idev) {
		ret = TMF_RESP_FUNC_COMPLETE;
		goto out;
	}

	ret = isci_reset_device(ihost, idev, hard_reset);
 out:
	isci_put_device(idev);
	return ret;
1681
}