request.c 117.0 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 57 58 59 60
/*
 * 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 "isci.h"
#include "task.h"
#include "request.h"
#include "sata.h"
#include "scu_completion_codes.h"
61
#include "scu_event_codes.h"
D
Dave Jiang 已提交
62
#include "sas.h"
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/**
 * This method returns the sgl element pair for the specificed sgl_pair index.
 * @sci_req: This parameter specifies the IO request for which to retrieve
 *    the Scatter-Gather List element pair.
 * @sgl_pair_index: This parameter specifies the index into the SGL element
 *    pair to be retrieved.
 *
 * This method returns a pointer to an struct scu_sgl_element_pair.
 */
static struct scu_sgl_element_pair *scic_sds_request_get_sgl_element_pair(
	struct scic_sds_request *sci_req,
	u32 sgl_pair_index
	) {
	struct scu_task_context *task_context;

	task_context = (struct scu_task_context *)sci_req->task_context_buffer;

	if (sgl_pair_index == 0) {
		return &task_context->sgl_pair_ab;
	} else if (sgl_pair_index == 1) {
		return &task_context->sgl_pair_cd;
	}
86

87
	return &sci_req->sg_table[sgl_pair_index - 2];
88 89
}

90 91 92 93 94 95
/**
 * This function will build the SGL list for an IO request.
 * @sci_req: This parameter specifies the IO request for which to build
 *    the Scatter-Gather List.
 *
 */
96
static void scic_sds_request_build_sgl(struct scic_sds_request *sds_request)
97
{
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	struct isci_request *isci_request = sci_req_to_ireq(sds_request);
	struct isci_host *isci_host = isci_request->isci_host;
	struct sas_task *task = isci_request_access_task(isci_request);
	struct scatterlist *sg = NULL;
	dma_addr_t dma_addr;
	u32 sg_idx = 0;
	struct scu_sgl_element_pair *scu_sg   = NULL;
	struct scu_sgl_element_pair *prev_sg  = NULL;

	if (task->num_scatter > 0) {
		sg = task->scatter;

		while (sg) {
			scu_sg = scic_sds_request_get_sgl_element_pair(
					sds_request,
					sg_idx);

			SCU_SGL_COPY(scu_sg->A, sg);

			sg = sg_next(sg);

			if (sg) {
				SCU_SGL_COPY(scu_sg->B, sg);
				sg = sg_next(sg);
			} else
				SCU_SGL_ZERO(scu_sg->B);

			if (prev_sg) {
				dma_addr =
					scic_io_request_get_dma_addr(
							sds_request,
							scu_sg);

				prev_sg->next_pair_upper =
					upper_32_bits(dma_addr);
				prev_sg->next_pair_lower =
					lower_32_bits(dma_addr);
			}

			prev_sg = scu_sg;
			sg_idx++;
		}
	} else {	/* handle when no sg */
		scu_sg = scic_sds_request_get_sgl_element_pair(sds_request,
							       sg_idx);
143

144 145 146 147
		dma_addr = dma_map_single(&isci_host->pdev->dev,
					  task->scatter,
					  task->total_xfer_len,
					  task->data_dir);
148

149
		isci_request->zero_scatter_daddr = dma_addr;
150

151 152 153 154
		scu_sg->A.length = task->total_xfer_len;
		scu_sg->A.address_upper = upper_32_bits(dma_addr);
		scu_sg->A.address_lower = lower_32_bits(dma_addr);
	}
155

156 157 158
	if (scu_sg) {
		scu_sg->next_pair_upper = 0;
		scu_sg->next_pair_lower = 0;
159
	}
160
}
161

162
static void scic_sds_io_request_build_ssp_command_iu(struct scic_sds_request *sci_req)
163
{
164 165
	struct ssp_cmd_iu *cmd_iu;
	struct isci_request *ireq = sci_req_to_ireq(sci_req);
D
Dave Jiang 已提交
166
	struct sas_task *task = isci_request_access_task(ireq);
167

168
	cmd_iu = &sci_req->ssp.cmd;
169

170 171 172 173 174 175 176 177
	memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
	cmd_iu->add_cdb_len = 0;
	cmd_iu->_r_a = 0;
	cmd_iu->_r_b = 0;
	cmd_iu->en_fburst = 0; /* unsupported */
	cmd_iu->task_prio = task->ssp_task.task_prio;
	cmd_iu->task_attr = task->ssp_task.task_attr;
	cmd_iu->_r_c = 0;
178

179 180 181
	sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cdb,
		       sizeof(task->ssp_task.cdb) / sizeof(u32));
}
182

183 184 185 186 187 188
static void scic_sds_task_request_build_ssp_task_iu(struct scic_sds_request *sci_req)
{
	struct ssp_task_iu *task_iu;
	struct isci_request *ireq = sci_req_to_ireq(sci_req);
	struct sas_task *task = isci_request_access_task(ireq);
	struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
189

190 191 192 193 194 195 196 197 198 199 200
	task_iu = &sci_req->ssp.tmf;

	memset(task_iu, 0, sizeof(struct ssp_task_iu));

	memcpy(task_iu->LUN, task->ssp_task.LUN, 8);

	task_iu->task_func = isci_tmf->tmf_code;
	task_iu->task_tag =
		(ireq->ttype == tmf_task) ?
		isci_tmf->io_tag :
		SCI_CONTROLLER_INVALID_IO_TAG;
201 202 203
}

/**
204 205 206
 * This method is will fill in the SCU Task Context for any type of SSP request.
 * @sci_req:
 * @task_context:
207 208
 *
 */
209 210 211
static void scu_ssp_reqeust_construct_task_context(
	struct scic_sds_request *sds_request,
	struct scu_task_context *task_context)
212
{
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 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
	dma_addr_t dma_addr;
	struct scic_sds_controller *controller;
	struct scic_sds_remote_device *target_device;
	struct scic_sds_port *target_port;

	controller = scic_sds_request_get_controller(sds_request);
	target_device = scic_sds_request_get_device(sds_request);
	target_port = scic_sds_request_get_port(sds_request);

	/* Fill in the TC with the its required data */
	task_context->abort = 0;
	task_context->priority = 0;
	task_context->initiator_request = 1;
	task_context->connection_rate = target_device->connection_rate;
	task_context->protocol_engine_index =
		scic_sds_controller_get_protocol_engine_group(controller);
	task_context->logical_port_index =
		scic_sds_port_get_index(target_port);
	task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
	task_context->valid = SCU_TASK_CONTEXT_VALID;
	task_context->context_type = SCU_TASK_CONTEXT_TYPE;

	task_context->remote_node_index =
		scic_sds_remote_device_get_index(sds_request->target_device);
	task_context->command_code = 0;

	task_context->link_layer_control = 0;
	task_context->do_not_dma_ssp_good_response = 1;
	task_context->strict_ordering = 0;
	task_context->control_frame = 0;
	task_context->timeout_enable = 0;
	task_context->block_guard_enable = 0;

	task_context->address_modifier = 0;

	/* task_context->type.ssp.tag = sci_req->io_tag; */
	task_context->task_phase = 0x01;

	if (sds_request->was_tag_assigned_by_user) {
		/*
		 * Build the task context now since we have already read
		 * the data
		 */
		sds_request->post_context =
			(SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
			 (scic_sds_controller_get_protocol_engine_group(
							controller) <<
			  SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
			 (scic_sds_port_get_index(target_port) <<
			  SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
			 scic_sds_io_tag_get_index(sds_request->io_tag));
	} else {
		/*
		 * Build the task context now since we have already read
		 * the data
		 *
		 * I/O tag index is not assigned because we have to wait
		 * until we get a TCi
		 */
		sds_request->post_context =
			(SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
			 (scic_sds_controller_get_protocol_engine_group(
							owning_controller) <<
			  SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
			 (scic_sds_port_get_index(target_port) <<
			  SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
	}
280

281 282 283 284 285 286
	/*
	 * Copy the physical address for the command buffer to the
	 * SCU Task Context
	 */
	dma_addr = scic_io_request_get_dma_addr(sds_request,
						&sds_request->ssp.cmd);
287

288 289 290 291 292 293
	task_context->command_iu_upper = upper_32_bits(dma_addr);
	task_context->command_iu_lower = lower_32_bits(dma_addr);

	/*
	 * Copy the physical address for the response buffer to the
	 * SCU Task Context
294
	 */
295 296
	dma_addr = scic_io_request_get_dma_addr(sds_request,
						&sds_request->ssp.rsp);
297

298 299 300
	task_context->response_iu_upper = upper_32_bits(dma_addr);
	task_context->response_iu_lower = lower_32_bits(dma_addr);
}
301

302 303 304 305 306 307 308 309 310 311 312
/**
 * This method is will fill in the SCU Task Context for a SSP IO request.
 * @sci_req:
 *
 */
static void scu_ssp_io_request_construct_task_context(
	struct scic_sds_request *sci_req,
	enum dma_data_direction dir,
	u32 len)
{
	struct scu_task_context *task_context;
313

314
	task_context = scic_sds_request_get_task_context(sci_req);
315

316
	scu_ssp_reqeust_construct_task_context(sci_req, task_context);
317

318 319 320 321 322 323 324 325 326
	task_context->ssp_command_iu_length =
		sizeof(struct ssp_cmd_iu) / sizeof(u32);
	task_context->type.ssp.frame_type = SSP_COMMAND;

	switch (dir) {
	case DMA_FROM_DEVICE:
	case DMA_NONE:
	default:
		task_context->task_type = SCU_TASK_TYPE_IOREAD;
327
		break;
328 329
	case DMA_TO_DEVICE:
		task_context->task_type = SCU_TASK_TYPE_IOWRITE;
330
		break;
331 332
	}

333 334 335 336
	task_context->transfer_length_bytes = len;

	if (task_context->transfer_length_bytes > 0)
		scic_sds_request_build_sgl(sci_req);
337 338 339
}

/**
340 341 342 343 344 345 346 347 348 349
 * This method will fill in the SCU Task Context for a SSP Task request.  The
 *    following important settings are utilized: -# priority ==
 *    SCU_TASK_PRIORITY_HIGH.  This ensures that the task request is issued
 *    ahead of other task destined for the same Remote Node. -# task_type ==
 *    SCU_TASK_TYPE_IOREAD.  This simply indicates that a normal request type
 *    (i.e. non-raw frame) is being utilized to perform task management. -#
 *    control_frame == 1.  This ensures that the proper endianess is set so
 *    that the bytes are transmitted in the right order for a task frame.
 * @sci_req: This parameter specifies the task request object being
 *    constructed.
350 351
 *
 */
352 353
static void scu_ssp_task_request_construct_task_context(
	struct scic_sds_request *sci_req)
354
{
355
	struct scu_task_context *task_context;
356

357
	task_context = scic_sds_request_get_task_context(sci_req);
358

359
	scu_ssp_reqeust_construct_task_context(sci_req, task_context);
360

361 362 363 364 365 366 367
	task_context->control_frame                = 1;
	task_context->priority                     = SCU_TASK_PRIORITY_HIGH;
	task_context->task_type                    = SCU_TASK_TYPE_RAW_FRAME;
	task_context->transfer_length_bytes        = 0;
	task_context->type.ssp.frame_type          = SSP_TASK;
	task_context->ssp_command_iu_length =
		sizeof(struct ssp_task_iu) / sizeof(u32);
368 369
}

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
/**
 * This method is will fill in the SCU Task Context for any type of SATA
 *    request.  This is called from the various SATA constructors.
 * @sci_req: The general IO request object which is to be used in
 *    constructing the SCU task context.
 * @task_context: The buffer pointer for the SCU task context which is being
 *    constructed.
 *
 * The general io request construction is complete. The buffer assignment for
 * the command buffer is complete. none Revisit task context construction to
 * determine what is common for SSP/SMP/STP task context structures.
 */
static void scu_sata_reqeust_construct_task_context(
	struct scic_sds_request *sci_req,
	struct scu_task_context *task_context)
{
	dma_addr_t dma_addr;
	struct scic_sds_controller *controller;
	struct scic_sds_remote_device *target_device;
	struct scic_sds_port *target_port;

	controller = scic_sds_request_get_controller(sci_req);
	target_device = scic_sds_request_get_device(sci_req);
	target_port = scic_sds_request_get_port(sci_req);

	/* Fill in the TC with the its required data */
	task_context->abort = 0;
	task_context->priority = SCU_TASK_PRIORITY_NORMAL;
	task_context->initiator_request = 1;
	task_context->connection_rate = target_device->connection_rate;
	task_context->protocol_engine_index =
		scic_sds_controller_get_protocol_engine_group(controller);
	task_context->logical_port_index =
		scic_sds_port_get_index(target_port);
	task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
	task_context->valid = SCU_TASK_CONTEXT_VALID;
	task_context->context_type = SCU_TASK_CONTEXT_TYPE;

	task_context->remote_node_index =
		scic_sds_remote_device_get_index(sci_req->target_device);
	task_context->command_code = 0;

	task_context->link_layer_control = 0;
	task_context->do_not_dma_ssp_good_response = 1;
	task_context->strict_ordering = 0;
	task_context->control_frame = 0;
	task_context->timeout_enable = 0;
	task_context->block_guard_enable = 0;

	task_context->address_modifier = 0;
	task_context->task_phase = 0x01;

	task_context->ssp_command_iu_length =
		(sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);

	/* Set the first word of the H2D REG FIS */
	task_context->type.words[0] = *(u32 *)&sci_req->stp.cmd;

	if (sci_req->was_tag_assigned_by_user) {
		/*
		 * Build the task context now since we have already read
		 * the data
		 */
		sci_req->post_context =
			(SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
			 (scic_sds_controller_get_protocol_engine_group(
							controller) <<
			  SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
			 (scic_sds_port_get_index(target_port) <<
			  SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
			 scic_sds_io_tag_get_index(sci_req->io_tag));
	} else {
		/*
		 * Build the task context now since we have already read
		 * the data.
		 * I/O tag index is not assigned because we have to wait
		 * until we get a TCi.
		 */
		sci_req->post_context =
			(SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
			 (scic_sds_controller_get_protocol_engine_group(
							controller) <<
			  SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
			 (scic_sds_port_get_index(target_port) <<
			  SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
	}

	/*
	 * Copy the physical address for the command buffer to the SCU Task
	 * Context. We must offset the command buffer by 4 bytes because the
	 * first 4 bytes are transfered in the body of the TC.
	 */
	dma_addr = scic_io_request_get_dma_addr(sci_req,
						((char *) &sci_req->stp.cmd) +
						sizeof(u32));

	task_context->command_iu_upper = upper_32_bits(dma_addr);
	task_context->command_iu_lower = lower_32_bits(dma_addr);

	/* SATA Requests do not have a response buffer */
	task_context->response_iu_upper = 0;
	task_context->response_iu_lower = 0;
}


475

476
/**
477 478 479 480 481
 * scu_stp_raw_request_construct_task_context -
 * @sci_req: This parameter specifies the STP request object for which to
 *    construct a RAW command frame task context.
 * @task_context: This parameter specifies the SCU specific task context buffer
 *    to construct.
482
 *
483 484
 * This method performs the operations common to all SATA/STP requests
 * utilizing the raw frame method. none
485
 */
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
static void scu_stp_raw_request_construct_task_context(struct scic_sds_stp_request *stp_req,
						       struct scu_task_context *task_context)
{
	struct scic_sds_request *sci_req = to_sci_req(stp_req);

	scu_sata_reqeust_construct_task_context(sci_req, task_context);

	task_context->control_frame         = 0;
	task_context->priority              = SCU_TASK_PRIORITY_NORMAL;
	task_context->task_type             = SCU_TASK_TYPE_SATA_RAW_FRAME;
	task_context->type.stp.fis_type     = FIS_REGH2D;
	task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
}

static enum sci_status
scic_sds_stp_pio_request_construct(struct scic_sds_request *sci_req,
				   bool copy_rx_frame)
{
	struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
	struct scic_sds_stp_pio_request *pio = &stp_req->type.pio;

	scu_stp_raw_request_construct_task_context(stp_req,
						   sci_req->task_context_buffer);

	pio->current_transfer_bytes = 0;
	pio->ending_error = 0;
	pio->ending_status = 0;

	pio->request_current.sgl_offset = 0;
	pio->request_current.sgl_set = SCU_SGL_ELEMENT_PAIR_A;

	if (copy_rx_frame) {
		scic_sds_request_build_sgl(sci_req);
		/* Since the IO request copy of the TC contains the same data as
		 * the actual TC this pointer is vaild for either.
		 */
		pio->request_current.sgl_pair = &sci_req->task_context_buffer->sgl_pair_ab;
	} else {
		/* The user does not want the data copied to the SGL buffer location */
		pio->request_current.sgl_pair = NULL;
	}
527

528 529
	return SCI_SUCCESS;
}
530 531 532

/**
 *
533 534 535 536 537 538 539 540 541
 * @sci_req: This parameter specifies the request to be constructed as an
 *    optimized request.
 * @optimized_task_type: This parameter specifies whether the request is to be
 *    an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
 *    value of 1 indicates NCQ.
 *
 * This method will perform request construction common to all types of STP
 * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
 * returns an indication as to whether the construction was successful.
542
 */
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
static void scic_sds_stp_optimized_request_construct(struct scic_sds_request *sci_req,
						     u8 optimized_task_type,
						     u32 len,
						     enum dma_data_direction dir)
{
	struct scu_task_context *task_context = sci_req->task_context_buffer;

	/* Build the STP task context structure */
	scu_sata_reqeust_construct_task_context(sci_req, task_context);

	/* Copy over the SGL elements */
	scic_sds_request_build_sgl(sci_req);

	/* Copy over the number of bytes to be transfered */
	task_context->transfer_length_bytes = len;

	if (dir == DMA_TO_DEVICE) {
		/*
		 * The difference between the DMA IN and DMA OUT request task type
		 * values are consistent with the difference between FPDMA READ
		 * and FPDMA WRITE values.  Add the supplied task type parameter
		 * to this difference to set the task type properly for this
		 * DATA OUT (WRITE) case. */
		task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
								 - SCU_TASK_TYPE_DMA_IN);
	} else {
		/*
		 * For the DATA IN (READ) case, simply save the supplied
		 * optimized task type. */
		task_context->task_type = optimized_task_type;
	}
}



578 579 580 581 582
static enum sci_status
scic_io_request_construct_sata(struct scic_sds_request *sci_req,
			       u32 len,
			       enum dma_data_direction dir,
			       bool copy)
583
{
584 585 586
	enum sci_status status = SCI_SUCCESS;
	struct isci_request *ireq = sci_req_to_ireq(sci_req);
	struct sas_task *task = isci_request_access_task(ireq);
587

588 589 590
	/* check for management protocols */
	if (ireq->ttype == tmf_task) {
		struct isci_tmf *tmf = isci_request_access_tmf(ireq);
591

592
		if (tmf->tmf_code == isci_tmf_sata_srst_high ||
593 594 595 596 597
		    tmf->tmf_code == isci_tmf_sata_srst_low) {
			scu_stp_raw_request_construct_task_context(&sci_req->stp.req,
								   sci_req->task_context_buffer);
			return SCI_SUCCESS;
		} else {
598 599 600 601 602 603 604
			dev_err(scic_to_dev(sci_req->owning_controller),
				"%s: Request 0x%p received un-handled SAT "
				"management protocol 0x%x.\n",
				__func__, sci_req, tmf->tmf_code);

			return SCI_FAILURE;
		}
605 606
	}

607 608 609 610 611 612 613 614 615 616
	if (!sas_protocol_ata(task->task_proto)) {
		dev_err(scic_to_dev(sci_req->owning_controller),
			"%s: Non-ATA protocol in SATA path: 0x%x\n",
			__func__,
			task->task_proto);
		return SCI_FAILURE;

	}

	/* non data */
617 618 619 620 621
	if (task->data_dir == DMA_NONE) {
		scu_stp_raw_request_construct_task_context(&sci_req->stp.req,
							   sci_req->task_context_buffer);
		return SCI_SUCCESS;
	}
622 623

	/* NCQ */
624 625 626 627 628 629
	if (task->ata_task.use_ncq) {
		scic_sds_stp_optimized_request_construct(sci_req,
							 SCU_TASK_TYPE_FPDMAQ_READ,
							 len, dir);
		return SCI_SUCCESS;
	}
630 631

	/* DMA */
632 633 634 635 636 637
	if (task->ata_task.dma_xfer) {
		scic_sds_stp_optimized_request_construct(sci_req,
							 SCU_TASK_TYPE_DMA_IN,
							 len, dir);
		return SCI_SUCCESS;
	} else /* PIO */
638 639 640 641 642 643
		return scic_sds_stp_pio_request_construct(sci_req, copy);

	return status;
}

static enum sci_status scic_io_request_construct_basic_ssp(struct scic_sds_request *sci_req)
644
{
645 646
	struct isci_request *ireq = sci_req_to_ireq(sci_req);
	struct sas_task *task = isci_request_access_task(ireq);
647

648
	sci_req->protocol = SCIC_SSP_PROTOCOL;
649

650 651 652
	scu_ssp_io_request_construct_task_context(sci_req,
						  task->data_dir,
						  task->total_xfer_len);
653

654
	scic_sds_io_request_build_ssp_command_iu(sci_req);
655

656 657
	sci_base_state_machine_change_state(&sci_req->state_machine,
					    SCI_BASE_REQUEST_STATE_CONSTRUCTED);
658

659 660
	return SCI_SUCCESS;
}
661

662 663 664 665 666
enum sci_status scic_task_request_construct_ssp(
	struct scic_sds_request *sci_req)
{
	/* Construct the SSP Task SCU Task Context */
	scu_ssp_task_request_construct_task_context(sci_req);
667

668 669
	/* Fill in the SSP Task IU */
	scic_sds_task_request_build_ssp_task_iu(sci_req);
670

671
	sci_base_state_machine_change_state(&sci_req->state_machine,
672
					    SCI_BASE_REQUEST_STATE_CONSTRUCTED);
673

674 675
	return SCI_SUCCESS;
}
676

677 678 679 680 681 682 683
static enum sci_status scic_io_request_construct_basic_sata(struct scic_sds_request *sci_req)
{
	enum sci_status status;
	struct scic_sds_stp_request *stp_req;
	bool copy = false;
	struct isci_request *isci_request = sci_req_to_ireq(sci_req);
	struct sas_task *task = isci_request_access_task(isci_request);
684

685 686
	stp_req = &sci_req->stp.req;
	sci_req->protocol = SCIC_STP_PROTOCOL;
687

688 689 690 691 692 693 694 695 696
	copy = (task->data_dir == DMA_NONE) ? false : true;

	status = scic_io_request_construct_sata(sci_req,
						task->total_xfer_len,
						task->data_dir,
						copy);

	if (status == SCI_SUCCESS)
		sci_base_state_machine_change_state(&sci_req->state_machine,
697
						    SCI_BASE_REQUEST_STATE_CONSTRUCTED);
698 699

	return status;
700 701
}

702 703 704 705 706 707 708 709 710 711 712
enum sci_status scic_task_request_construct_sata(struct scic_sds_request *sci_req)
{
	enum sci_status status = SCI_SUCCESS;
	struct isci_request *ireq = sci_req_to_ireq(sci_req);

	/* check for management protocols */
	if (ireq->ttype == tmf_task) {
		struct isci_tmf *tmf = isci_request_access_tmf(ireq);

		if (tmf->tmf_code == isci_tmf_sata_srst_high ||
		    tmf->tmf_code == isci_tmf_sata_srst_low) {
713 714
			scu_stp_raw_request_construct_task_context(&sci_req->stp.req,
								   sci_req->task_context_buffer);
715 716 717 718 719 720 721 722 723 724
		} else {
			dev_err(scic_to_dev(sci_req->owning_controller),
				"%s: Request 0x%p received un-handled SAT "
				"Protocol 0x%x.\n",
				__func__, sci_req, tmf->tmf_code);

			return SCI_FAILURE;
		}
	}

725 726 727 728
	if (status != SCI_SUCCESS)
		return status;
	sci_base_state_machine_change_state(&sci_req->state_machine,
					    SCI_BASE_REQUEST_STATE_CONSTRUCTED);
729 730 731 732

	return status;
}

733
/**
734 735
 * sci_req_tx_bytes - bytes transferred when reply underruns request
 * @sci_req: request that was terminated early
736
 */
737 738
#define SCU_TASK_CONTEXT_SRAM 0x200000
static u32 sci_req_tx_bytes(struct scic_sds_request *sci_req)
739
{
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
	struct scic_sds_controller *scic = sci_req->owning_controller;
	u32 ret_val = 0;

	if (readl(&scic->smu_registers->address_modifier) == 0) {
		void __iomem *scu_reg_base = scic->scu_registers;

		/* get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
		 *   BAR1 is the scu_registers
		 *   0x20002C = 0x200000 + 0x2c
		 *            = start of task context SRAM + offset of (type.ssp.data_offset)
		 *   TCi is the io_tag of struct scic_sds_request
		 */
		ret_val = readl(scu_reg_base +
				(SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
				((sizeof(struct scu_task_context)) * scic_sds_io_tag_get_index(sci_req->io_tag)));
	}

	return ret_val;
}

760
enum sci_status scic_sds_request_start(struct scic_sds_request *sci_req)
761
{
762 763 764 765 766 767
	struct scic_sds_controller *scic = sci_req->owning_controller;
	struct scu_task_context *task_context;
	enum sci_base_request_states state;

	if (sci_req->device_sequence !=
	    scic_sds_remote_device_get_sequence(sci_req->target_device))
768 769
		return SCI_FAILURE;

770 771 772 773 774 775 776
	state = sci_req->state_machine.current_state_id;
	if (state != SCI_BASE_REQUEST_STATE_CONSTRUCTED) {
		dev_warn(scic_to_dev(scic),
			"%s: SCIC IO Request requested to start while in wrong "
			 "state %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}
777

778 779 780 781 782 783 784
	/* if necessary, allocate a TCi for the io request object and then will,
	 * if necessary, copy the constructed TC data into the actual TC buffer.
	 * If everything is successful the post context field is updated with
	 * the TCi so the controller can post the request to the hardware.
	 */
	if (sci_req->io_tag == SCI_CONTROLLER_INVALID_IO_TAG)
		sci_req->io_tag = scic_controller_allocate_io_tag(scic);
785

786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 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 826 827 828 829 830 831 832 833
	/* Record the IO Tag in the request */
	if (sci_req->io_tag != SCI_CONTROLLER_INVALID_IO_TAG) {
		task_context = sci_req->task_context_buffer;

		task_context->task_index = scic_sds_io_tag_get_index(sci_req->io_tag);

		switch (task_context->protocol_type) {
		case SCU_TASK_CONTEXT_PROTOCOL_SMP:
		case SCU_TASK_CONTEXT_PROTOCOL_SSP:
			/* SSP/SMP Frame */
			task_context->type.ssp.tag = sci_req->io_tag;
			task_context->type.ssp.target_port_transfer_tag =
				0xFFFF;
			break;

		case SCU_TASK_CONTEXT_PROTOCOL_STP:
			/* STP/SATA Frame
			 * task_context->type.stp.ncq_tag = sci_req->ncq_tag;
			 */
			break;

		case SCU_TASK_CONTEXT_PROTOCOL_NONE:
			/* / @todo When do we set no protocol type? */
			break;

		default:
			/* This should never happen since we build the IO
			 * requests */
			break;
		}

		/*
		 * Check to see if we need to copy the task context buffer
		 * or have been building into the task context buffer */
		if (sci_req->was_tag_assigned_by_user == false)
			scic_sds_controller_copy_task_context(scic, sci_req);

		/* Add to the post_context the io tag value */
		sci_req->post_context |= scic_sds_io_tag_get_index(sci_req->io_tag);

		/* Everything is good go ahead and change state */
		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_STARTED);

		return SCI_SUCCESS;
	}

	return SCI_FAILURE_INSUFFICIENT_RESOURCES;
834 835 836
}

enum sci_status
837
scic_sds_io_request_terminate(struct scic_sds_request *sci_req)
838
{
839
	enum sci_base_request_states state;
840

841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
	state = sci_req->state_machine.current_state_id;

	switch (state) {
	case SCI_BASE_REQUEST_STATE_CONSTRUCTED:
		scic_sds_request_set_status(sci_req,
			SCU_TASK_DONE_TASK_ABORT,
			SCI_FAILURE_IO_TERMINATED);

		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_COMPLETED);
		return SCI_SUCCESS;
	case SCI_BASE_REQUEST_STATE_STARTED:
	case SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION:
	case SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE:
	case SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION:
	case SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE:
	case SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE:
	case SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE:
	case SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE:
	case SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE:
	case SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE:
	case SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE:
	case SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE:
	case SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE:
	case SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE:
	case SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE:
		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_ABORTING);
		return SCI_SUCCESS;
	case SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE:
		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_ABORTING);
		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_COMPLETED);
		return SCI_SUCCESS;
	case SCI_BASE_REQUEST_STATE_ABORTING:
		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_COMPLETED);
		return SCI_SUCCESS;
	case SCI_BASE_REQUEST_STATE_COMPLETED:
	default:
		dev_warn(scic_to_dev(sci_req->owning_controller),
			 "%s: SCIC IO Request requested to abort while in wrong "
			 "state %d\n",
			 __func__,
			 sci_base_state_machine_get_state(&sci_req->state_machine));
		break;
	}
889

890 891
	return SCI_FAILURE_INVALID_STATE;
}
892

893
enum sci_status scic_sds_request_complete(struct scic_sds_request *sci_req)
894
{
895 896 897 898 899 900 901
	enum sci_base_request_states state;
	struct scic_sds_controller *scic = sci_req->owning_controller;

	state = sci_req->state_machine.current_state_id;
	if (WARN_ONCE(state != SCI_BASE_REQUEST_STATE_COMPLETED,
		      "isci: request completion from wrong state (%d)\n", state))
		return SCI_FAILURE_INVALID_STATE;
902

903 904
	if (!sci_req->was_tag_assigned_by_user)
		scic_controller_free_io_tag(scic, sci_req->io_tag);
905

906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 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
	if (sci_req->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX)
		scic_sds_controller_release_frame(scic,
						  sci_req->saved_rx_frame_index);

	/* XXX can we just stop the machine and remove the 'final' state? */
	sci_base_state_machine_change_state(&sci_req->state_machine,
					    SCI_BASE_REQUEST_STATE_FINAL);
	return SCI_SUCCESS;
}

enum sci_status scic_sds_io_request_event_handler(struct scic_sds_request *sci_req,
						  u32 event_code)
{
	enum sci_base_request_states state;
	struct scic_sds_controller *scic = sci_req->owning_controller;

	state = sci_req->state_machine.current_state_id;

	if (state != SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE) {
		dev_warn(scic_to_dev(scic), "%s: (%x) in wrong state %d\n",
			 __func__, event_code, state);

		return SCI_FAILURE_INVALID_STATE;
	}

	switch (scu_get_event_specifier(event_code)) {
	case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
		/* We are waiting for data and the SCU has R_ERR the data frame.
		 * Go back to waiting for the D2H Register FIS
		 */
		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE);
		return SCI_SUCCESS;
	default:
		dev_err(scic_to_dev(scic),
			"%s: pio request unexpected event %#x\n",
			__func__, event_code);

		/* TODO Should we fail the PIO request when we get an
		 * unexpected event?
		 */
		return SCI_FAILURE;
	}
949 950
}

951 952 953 954 955
/*
 * This function copies response data for requests returning response data
 *    instead of sense data.
 * @sci_req: This parameter specifies the request object for which to copy
 *    the response data.
956
 */
957
static void scic_sds_io_request_copy_response(struct scic_sds_request *sci_req)
958
{
959 960 961 962 963
	void *resp_buf;
	u32 len;
	struct ssp_response_iu *ssp_response;
	struct isci_request *ireq = sci_req_to_ireq(sci_req);
	struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
964

965
	ssp_response = &sci_req->ssp.rsp;
966

967
	resp_buf = &isci_tmf->resp.resp_iu;
968

969 970 971
	len = min_t(u32,
		    SSP_RESP_IU_MAX_SIZE,
		    be32_to_cpu(ssp_response->response_data_len));
972

973 974
	memcpy(resp_buf, ssp_response->resp_data, len);
}
975

976 977
static enum sci_status request_started_state_tc_event(struct scic_sds_request *sci_req,
						      u32 completion_code)
978 979
{
	struct ssp_response_iu *resp_iu;
980
	u8 datapres;
981

982 983
	/* TODO: Any SDMA return code of other than 0 is bad decode 0x003C0000
	 * to determine SDMA status
984 985 986 987 988 989
	 */
	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
		scic_sds_request_set_status(sci_req,
					    SCU_TASK_DONE_GOOD,
					    SCI_SUCCESS);
990
		break;
991 992
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP): {
		/* There are times when the SCU hardware will return an early
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
		 * response because the io request specified more data than is
		 * returned by the target device (mode pages, inquiry data,
		 * etc.).  We must check the response stats to see if this is
		 * truly a failed request or a good request that just got
		 * completed early.
		 */
		struct ssp_response_iu *resp = &sci_req->ssp.rsp;
		ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);

		sci_swab32_cpy(&sci_req->ssp.rsp,
			       &sci_req->ssp.rsp,
			       word_cnt);

		if (resp->status == 0) {
1007 1008 1009
			scic_sds_request_set_status(sci_req,
						    SCU_TASK_DONE_GOOD,
						    SCI_SUCCESS_IO_DONE_EARLY);
1010
		} else {
1011 1012 1013
			scic_sds_request_set_status(sci_req,
						    SCU_TASK_DONE_CHECK_RESPONSE,
						    SCI_FAILURE_IO_RESPONSE_VALID);
1014
		}
1015
		break;
1016
	}
1017
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE): {
1018
		ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1019

1020 1021 1022
		sci_swab32_cpy(&sci_req->ssp.rsp,
			       &sci_req->ssp.rsp,
			       word_cnt);
1023

1024 1025 1026
		scic_sds_request_set_status(sci_req,
					    SCU_TASK_DONE_CHECK_RESPONSE,
					    SCI_FAILURE_IO_RESPONSE_VALID);
1027
		break;
1028
	}
1029

1030
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
1031
		/* TODO With TASK_DONE_RESP_LEN_ERR is the response frame
1032 1033 1034 1035 1036 1037
		 * guaranteed to be received before this completion status is
		 * posted?
		 */
		resp_iu = &sci_req->ssp.rsp;
		datapres = resp_iu->datapres;

1038 1039 1040 1041
		if (datapres == 1 || datapres == 2) {
			scic_sds_request_set_status(sci_req,
						    SCU_TASK_DONE_CHECK_RESPONSE,
						    SCI_FAILURE_IO_RESPONSE_VALID);
1042
		} else
1043 1044 1045
			scic_sds_request_set_status(sci_req,
						    SCU_TASK_DONE_GOOD,
						    SCI_SUCCESS);
1046
		break;
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
	/* only stp device gets suspended. */
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
		if (sci_req->protocol == SCIC_STP_PROTOCOL) {
1060
			scic_sds_request_set_status(sci_req,
1061 1062 1063 1064
				SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
				SCU_COMPLETION_TL_STATUS_SHIFT,
				SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
		} else {
1065
			scic_sds_request_set_status(sci_req,
1066 1067 1068 1069
				SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
				SCU_COMPLETION_TL_STATUS_SHIFT,
				SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
		}
1070 1071
		break;

1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
	/* both stp/ssp device gets suspended */
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
1083 1084 1085 1086
		scic_sds_request_set_status(sci_req,
					    SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
					    SCU_COMPLETION_TL_STATUS_SHIFT,
					    SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
1087 1088
		break;

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
	/* neither ssp nor stp gets suspended. */
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
1105
	default:
1106 1107 1108 1109 1110
		scic_sds_request_set_status(
			sci_req,
			SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
			SCU_COMPLETION_TL_STATUS_SHIFT,
			SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1111 1112
		break;
	}
1113 1114 1115 1116 1117 1118

	/*
	 * TODO: This is probably wrong for ACK/NAK timeout conditions
	 */

	/* In all cases we will treat this as the completion of the IO req. */
1119 1120
	sci_base_state_machine_change_state(&sci_req->state_machine,
					    SCI_BASE_REQUEST_STATE_COMPLETED);
1121
	return SCI_SUCCESS;
1122 1123
}

1124 1125
static enum sci_status request_aborting_state_tc_event(struct scic_sds_request *sci_req,
						       u32 completion_code)
1126 1127 1128 1129
{
	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
	case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
	case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
1130 1131
		scic_sds_request_set_status(sci_req, SCU_TASK_DONE_TASK_ABORT,
					    SCI_FAILURE_IO_TERMINATED);
1132 1133

		sci_base_state_machine_change_state(&sci_req->state_machine,
1134
						    SCI_BASE_REQUEST_STATE_COMPLETED);
1135 1136 1137
		break;

	default:
1138 1139 1140
		/* Unless we get some strange error wait for the task abort to complete
		 * TODO: Should there be a state change for this completion?
		 */
1141 1142
		break;
	}
1143 1144 1145 1146

	return SCI_SUCCESS;
}

1147 1148
static enum sci_status ssp_task_request_await_tc_event(struct scic_sds_request *sci_req,
						       u32 completion_code)
1149 1150 1151 1152 1153 1154 1155
{
	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
		scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
					    SCI_SUCCESS);

		sci_base_state_machine_change_state(&sci_req->state_machine,
1156
						    SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE);
1157 1158
		break;
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
1159 1160 1161 1162 1163
		/* Currently, the decision is to simply allow the task request
		 * to timeout if the task IU wasn't received successfully.
		 * There is a potential for receiving multiple task responses if
		 * we decide to send the task IU again.
		 */
1164 1165
		dev_warn(scic_to_dev(sci_req->owning_controller),
			 "%s: TaskRequest:0x%p CompletionCode:%x - "
1166
			 "ACK/NAK timeout\n", __func__, sci_req,
1167 1168 1169
			 completion_code);

		sci_base_state_machine_change_state(&sci_req->state_machine,
1170
						    SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE);
1171 1172
		break;
	default:
1173 1174 1175 1176
		/* All other completion status cause the IO to be complete.  If a NAK
		 * was received, then it is up to the user to retry the request.
		 */
		scic_sds_request_set_status(sci_req,
1177
			SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1178
			SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1179 1180

		sci_base_state_machine_change_state(&sci_req->state_machine,
1181
						    SCI_BASE_REQUEST_STATE_COMPLETED);
1182 1183 1184 1185 1186 1187
		break;
	}

	return SCI_SUCCESS;
}

1188 1189
static enum sci_status smp_request_await_response_tc_event(struct scic_sds_request *sci_req,
							   u32 completion_code)
1190 1191 1192
{
	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1193 1194 1195 1196
		/* In the AWAIT RESPONSE state, any TC completion is
		 * unexpected.  but if the TC has success status, we
		 * complete the IO anyway.
		 */
1197 1198
		scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
					    SCI_SUCCESS);
1199

1200 1201
		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_COMPLETED);
1202 1203 1204 1205 1206 1207
		break;

	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
1208 1209 1210 1211 1212 1213 1214
		/* These status has been seen in a specific LSI
		 * expander, which sometimes is not able to send smp
		 * response within 2 ms. This causes our hardware break
		 * the connection and set TC completion with one of
		 * these SMP_XXX_XX_ERR status. For these type of error,
		 * we ask scic user to retry the request.
		 */
1215 1216
		scic_sds_request_set_status(sci_req, SCU_TASK_DONE_SMP_RESP_TO_ERR,
					    SCI_FAILURE_RETRY_REQUIRED);
1217

1218 1219
		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_COMPLETED);
1220 1221 1222
		break;

	default:
1223 1224 1225 1226 1227 1228
		/* All other completion status cause the IO to be complete.  If a NAK
		 * was received, then it is up to the user to retry the request
		 */
		scic_sds_request_set_status(sci_req,
					    SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
					    SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1229

1230 1231
		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_COMPLETED);
1232 1233 1234 1235 1236 1237
		break;
	}

	return SCI_SUCCESS;
}

1238 1239
static enum sci_status smp_request_await_tc_event(struct scic_sds_request *sci_req,
						  u32 completion_code)
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
{
	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
		scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
					    SCI_SUCCESS);

		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_COMPLETED);
		break;
	default:
1250 1251 1252 1253 1254 1255 1256
		/* All other completion status cause the IO to be
		 * complete.  If a NAK was received, then it is up to
		 * the user to retry the request.
		 */
		scic_sds_request_set_status(sci_req,
					    SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
					    SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1257

1258 1259
		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_COMPLETED);
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
		break;
	}

	return SCI_SUCCESS;
}

void scic_stp_io_request_set_ncq_tag(struct scic_sds_request *req,
				     u16 ncq_tag)
{
	/**
	 * @note This could be made to return an error to the user if the user
	 *       attempts to set the NCQ tag in the wrong state.
	 */
	req->task_context_buffer->type.stp.ncq_tag = ncq_tag;
}

/**
 *
 * @sci_req:
 *
 * Get the next SGL element from the request. - Check on which SGL element pair
 * we are working - if working on SLG pair element A - advance to element B -
 * else - check to see if there are more SGL element pairs for this IO request
 * - if there are more SGL element pairs - advance to the next pair and return
 * element A struct scu_sgl_element*
 */
static struct scu_sgl_element *scic_sds_stp_request_pio_get_next_sgl(struct scic_sds_stp_request *stp_req)
{
	struct scu_sgl_element *current_sgl;
	struct scic_sds_request *sci_req = to_sci_req(stp_req);
	struct scic_sds_request_pio_sgl *pio_sgl = &stp_req->type.pio.request_current;

	if (pio_sgl->sgl_set == SCU_SGL_ELEMENT_PAIR_A) {
		if (pio_sgl->sgl_pair->B.address_lower == 0 &&
		    pio_sgl->sgl_pair->B.address_upper == 0) {
			current_sgl = NULL;
		} else {
			pio_sgl->sgl_set = SCU_SGL_ELEMENT_PAIR_B;
			current_sgl = &pio_sgl->sgl_pair->B;
		}
	} else {
		if (pio_sgl->sgl_pair->next_pair_lower == 0 &&
		    pio_sgl->sgl_pair->next_pair_upper == 0) {
			current_sgl = NULL;
		} else {
			u64 phys_addr;

			phys_addr = pio_sgl->sgl_pair->next_pair_upper;
			phys_addr <<= 32;
			phys_addr |= pio_sgl->sgl_pair->next_pair_lower;

			pio_sgl->sgl_pair = scic_request_get_virt_addr(sci_req, phys_addr);
			pio_sgl->sgl_set = SCU_SGL_ELEMENT_PAIR_A;
			current_sgl = &pio_sgl->sgl_pair->A;
		}
	}

	return current_sgl;
}

1320 1321
static enum sci_status stp_request_non_data_await_h2d_tc_event(struct scic_sds_request *sci_req,
							       u32 completion_code)
1322 1323 1324
{
	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1325 1326
		scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
					    SCI_SUCCESS);
1327

1328 1329
		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE);
1330 1331 1332
		break;

	default:
1333 1334 1335 1336 1337 1338 1339
		/* All other completion status cause the IO to be
		 * complete.  If a NAK was received, then it is up to
		 * the user to retry the request.
		 */
		scic_sds_request_set_status(sci_req,
					    SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
					    SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1340

1341 1342
		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_COMPLETED);
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 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 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
		break;
	}

	return SCI_SUCCESS;
}

#define SCU_MAX_FRAME_BUFFER_SIZE  0x400  /* 1K is the maximum SCU frame data payload */

/* transmit DATA_FIS from (current sgl + offset) for input
 * parameter length. current sgl and offset is alreay stored in the IO request
 */
static enum sci_status scic_sds_stp_request_pio_data_out_trasmit_data_frame(
	struct scic_sds_request *sci_req,
	u32 length)
{
	struct scic_sds_controller *scic = sci_req->owning_controller;
	struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
	struct scu_task_context *task_context;
	struct scu_sgl_element *current_sgl;

	/* Recycle the TC and reconstruct it for sending out DATA FIS containing
	 * for the data from current_sgl+offset for the input length
	 */
	task_context = scic_sds_controller_get_task_context_buffer(scic,
								   sci_req->io_tag);

	if (stp_req->type.pio.request_current.sgl_set == SCU_SGL_ELEMENT_PAIR_A)
		current_sgl = &stp_req->type.pio.request_current.sgl_pair->A;
	else
		current_sgl = &stp_req->type.pio.request_current.sgl_pair->B;

	/* update the TC */
	task_context->command_iu_upper = current_sgl->address_upper;
	task_context->command_iu_lower = current_sgl->address_lower;
	task_context->transfer_length_bytes = length;
	task_context->type.stp.fis_type = FIS_DATA;

	/* send the new TC out. */
	return scic_controller_continue_io(sci_req);
}

static enum sci_status scic_sds_stp_request_pio_data_out_transmit_data(struct scic_sds_request *sci_req)
{

	struct scu_sgl_element *current_sgl;
	u32 sgl_offset;
	u32 remaining_bytes_in_current_sgl = 0;
	enum sci_status status = SCI_SUCCESS;
	struct scic_sds_stp_request *stp_req = &sci_req->stp.req;

	sgl_offset = stp_req->type.pio.request_current.sgl_offset;

	if (stp_req->type.pio.request_current.sgl_set == SCU_SGL_ELEMENT_PAIR_A) {
		current_sgl = &(stp_req->type.pio.request_current.sgl_pair->A);
		remaining_bytes_in_current_sgl = stp_req->type.pio.request_current.sgl_pair->A.length - sgl_offset;
	} else {
		current_sgl = &(stp_req->type.pio.request_current.sgl_pair->B);
		remaining_bytes_in_current_sgl = stp_req->type.pio.request_current.sgl_pair->B.length - sgl_offset;
	}


	if (stp_req->type.pio.pio_transfer_bytes > 0) {
		if (stp_req->type.pio.pio_transfer_bytes >= remaining_bytes_in_current_sgl) {
			/* recycle the TC and send the H2D Data FIS from (current sgl + sgl_offset) and length = remaining_bytes_in_current_sgl */
			status = scic_sds_stp_request_pio_data_out_trasmit_data_frame(sci_req, remaining_bytes_in_current_sgl);
			if (status == SCI_SUCCESS) {
				stp_req->type.pio.pio_transfer_bytes -= remaining_bytes_in_current_sgl;

				/* update the current sgl, sgl_offset and save for future */
				current_sgl = scic_sds_stp_request_pio_get_next_sgl(stp_req);
				sgl_offset = 0;
			}
		} else if (stp_req->type.pio.pio_transfer_bytes < remaining_bytes_in_current_sgl) {
			/* recycle the TC and send the H2D Data FIS from (current sgl + sgl_offset) and length = type.pio.pio_transfer_bytes */
			scic_sds_stp_request_pio_data_out_trasmit_data_frame(sci_req, stp_req->type.pio.pio_transfer_bytes);

			if (status == SCI_SUCCESS) {
				/* Sgl offset will be adjusted and saved for future */
				sgl_offset += stp_req->type.pio.pio_transfer_bytes;
				current_sgl->address_lower += stp_req->type.pio.pio_transfer_bytes;
				stp_req->type.pio.pio_transfer_bytes = 0;
			}
		}
	}

	if (status == SCI_SUCCESS) {
		stp_req->type.pio.request_current.sgl_offset = sgl_offset;
	}

	return status;
}

/**
 *
 * @stp_request: The request that is used for the SGL processing.
 * @data_buffer: The buffer of data to be copied.
 * @length: The length of the data transfer.
 *
 * Copy the data from the buffer for the length specified to the IO reqeust SGL
 * specified data region. enum sci_status
 */
static enum sci_status
scic_sds_stp_request_pio_data_in_copy_data_buffer(struct scic_sds_stp_request *stp_req,
						  u8 *data_buf, u32 len)
{
	struct scic_sds_request *sci_req;
	struct isci_request *ireq;
	u8 *src_addr;
	int copy_len;
	struct sas_task *task;
	struct scatterlist *sg;
	void *kaddr;
	int total_len = len;

	sci_req = to_sci_req(stp_req);
	ireq = sci_req_to_ireq(sci_req);
	task = isci_request_access_task(ireq);
	src_addr = data_buf;

	if (task->num_scatter > 0) {
		sg = task->scatter;

		while (total_len > 0) {
			struct page *page = sg_page(sg);

			copy_len = min_t(int, total_len, sg_dma_len(sg));
			kaddr = kmap_atomic(page, KM_IRQ0);
			memcpy(kaddr + sg->offset, src_addr, copy_len);
			kunmap_atomic(kaddr, KM_IRQ0);
			total_len -= copy_len;
			src_addr += copy_len;
			sg = sg_next(sg);
		}
	} else {
		BUG_ON(task->total_xfer_len < total_len);
		memcpy(task->scatter, src_addr, total_len);
	}

	return SCI_SUCCESS;
}

/**
 *
 * @sci_req: The PIO DATA IN request that is to receive the data.
 * @data_buffer: The buffer to copy from.
 *
 * Copy the data buffer to the io request data region. enum sci_status
 */
static enum sci_status scic_sds_stp_request_pio_data_in_copy_data(
	struct scic_sds_stp_request *sci_req,
	u8 *data_buffer)
{
	enum sci_status status;

	/*
	 * If there is less than 1K remaining in the transfer request
	 * copy just the data for the transfer */
	if (sci_req->type.pio.pio_transfer_bytes < SCU_MAX_FRAME_BUFFER_SIZE) {
		status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
			sci_req, data_buffer, sci_req->type.pio.pio_transfer_bytes);

		if (status == SCI_SUCCESS)
			sci_req->type.pio.pio_transfer_bytes = 0;
	} else {
		/* We are transfering the whole frame so copy */
		status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
			sci_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);

		if (status == SCI_SUCCESS)
			sci_req->type.pio.pio_transfer_bytes -= SCU_MAX_FRAME_BUFFER_SIZE;
	}

	return status;
}

1518 1519
static enum sci_status stp_request_pio_await_h2d_completion_tc_event(struct scic_sds_request *sci_req,
								     u32 completion_code)
1520 1521 1522 1523 1524
{
	enum sci_status status = SCI_SUCCESS;

	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1525
		scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS);
1526

1527 1528
		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE);
1529 1530 1531
		break;

	default:
1532 1533 1534 1535 1536 1537 1538
		/* All other completion status cause the IO to be
		 * complete.  If a NAK was received, then it is up to
		 * the user to retry the request.
		 */
		scic_sds_request_set_status(sci_req,
					    SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
					    SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1539

1540 1541
		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_COMPLETED);
1542 1543 1544 1545 1546 1547
		break;
	}

	return status;
}

1548 1549
static enum sci_status pio_data_out_tx_done_tc_event(struct scic_sds_request *sci_req,
						     u32 completion_code)
1550
{
1551 1552
	enum sci_status status = SCI_SUCCESS;
	bool all_frames_transferred = false;
1553 1554
	struct scic_sds_stp_request *stp_req = &sci_req->stp.req;

1555 1556 1557 1558
	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
		/* Transmit data */
		if (stp_req->type.pio.pio_transfer_bytes != 0) {
1559
			status = scic_sds_stp_request_pio_data_out_transmit_data(sci_req);
1560 1561 1562 1563 1564 1565 1566 1567
			if (status == SCI_SUCCESS) {
				if (stp_req->type.pio.pio_transfer_bytes == 0)
					all_frames_transferred = true;
			}
		} else if (stp_req->type.pio.pio_transfer_bytes == 0) {
			/*
			 * this will happen if the all data is written at the
			 * first time after the pio setup fis is received
1568
			 */
1569
			all_frames_transferred  = true;
1570 1571
		}

1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
		/* all data transferred. */
		if (all_frames_transferred) {
			/*
			 * Change the state to SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_FRAME_SUBSTATE
			 * and wait for PIO_SETUP fis / or D2H REg fis. */
			sci_base_state_machine_change_state(
				&sci_req->state_machine,
				SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE
				);
		}
1582 1583
		break;
	default:
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
		/*
		 * All other completion status cause the IO to be complete.  If a NAK
		 * was received, then it is up to the user to retry the request. */
		scic_sds_request_set_status(
			sci_req,
			SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
			SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
			);

		sci_base_state_machine_change_state(
			&sci_req->state_machine,
			SCI_BASE_REQUEST_STATE_COMPLETED
			);
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
		break;
	}

	return status;
}

static void scic_sds_stp_request_udma_complete_request(
	struct scic_sds_request *request,
	u32 scu_status,
	enum sci_status sci_status)
{
	scic_sds_request_set_status(request, scu_status, sci_status);
	sci_base_state_machine_change_state(&request->state_machine,
		SCI_BASE_REQUEST_STATE_COMPLETED);
}

static enum sci_status scic_sds_stp_request_udma_general_frame_handler(struct scic_sds_request *sci_req,
								       u32 frame_index)
{
	struct scic_sds_controller *scic = sci_req->owning_controller;
	struct dev_to_host_fis *frame_header;
	enum sci_status status;
	u32 *frame_buffer;

	status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
							       frame_index,
							       (void **)&frame_header);

	if ((status == SCI_SUCCESS) &&
	    (frame_header->fis_type == FIS_REGD2H)) {
		scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
							      frame_index,
							      (void **)&frame_buffer);

		scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
						       frame_header,
						       frame_buffer);
	}

	scic_sds_controller_release_frame(scic, frame_index);

	return status;
}

1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
enum sci_status scic_sds_io_request_frame_handler(struct scic_sds_request *sci_req,
						  u32 frame_index)
{
	struct scic_sds_controller *scic = sci_req->owning_controller;
	struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
	enum sci_base_request_states state;
	enum sci_status status;
	ssize_t word_cnt;

	state = sci_req->state_machine.current_state_id;
	switch (state)  {
	case SCI_BASE_REQUEST_STATE_STARTED: {
		struct ssp_frame_hdr ssp_hdr;
		void *frame_header;

		scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
							      frame_index,
							      &frame_header);

		word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
		sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);

		if (ssp_hdr.frame_type == SSP_RESPONSE) {
			struct ssp_response_iu *resp_iu;
			ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);

			scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
								      frame_index,
								      (void **)&resp_iu);

			sci_swab32_cpy(&sci_req->ssp.rsp, resp_iu, word_cnt);

			resp_iu = &sci_req->ssp.rsp;

			if (resp_iu->datapres == 0x01 ||
			    resp_iu->datapres == 0x02) {
				scic_sds_request_set_status(sci_req,
							    SCU_TASK_DONE_CHECK_RESPONSE,
							    SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
			} else
				scic_sds_request_set_status(sci_req,
							    SCU_TASK_DONE_GOOD,
							    SCI_SUCCESS);
		} else {
			/* not a response frame, why did it get forwarded? */
			dev_err(scic_to_dev(scic),
				"%s: SCIC IO Request 0x%p received unexpected "
				"frame %d type 0x%02x\n", __func__, sci_req,
				frame_index, ssp_hdr.frame_type);
		}

		/*
		 * In any case we are done with this frame buffer return it to the
		 * controller
		 */
		scic_sds_controller_release_frame(scic, frame_index);

		return SCI_SUCCESS;
	}
	case SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE:
		scic_sds_io_request_copy_response(sci_req);
		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_COMPLETED);
		scic_sds_controller_release_frame(scic,frame_index);
		return SCI_SUCCESS;
	case SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE: {
		struct smp_resp *rsp_hdr = &sci_req->smp.rsp;
		void *frame_header;

		scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
							      frame_index,
							      &frame_header);

		/* byte swap the header. */
		word_cnt = SMP_RESP_HDR_SZ / sizeof(u32);
		sci_swab32_cpy(rsp_hdr, frame_header, word_cnt);

		if (rsp_hdr->frame_type == SMP_RESPONSE) {
			void *smp_resp;

			scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
								      frame_index,
								      &smp_resp);

			word_cnt = (sizeof(struct smp_req) - SMP_RESP_HDR_SZ) /
				sizeof(u32);

			sci_swab32_cpy(((u8 *) rsp_hdr) + SMP_RESP_HDR_SZ,
				       smp_resp, word_cnt);

			scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
						    SCI_SUCCESS);

			sci_base_state_machine_change_state(&sci_req->state_machine,
							    SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION);
		} else {
			/* This was not a response frame why did it get forwarded? */
			dev_err(scic_to_dev(scic),
				"%s: SCIC SMP Request 0x%p received unexpected frame "
				"%d type 0x%02x\n", __func__, sci_req,
				frame_index, rsp_hdr->frame_type);

			scic_sds_request_set_status(sci_req,
						    SCU_TASK_DONE_SMP_FRM_TYPE_ERR,
						    SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);

			sci_base_state_machine_change_state(&sci_req->state_machine,
							    SCI_BASE_REQUEST_STATE_COMPLETED);
		}

		scic_sds_controller_release_frame(scic, frame_index);

		return SCI_SUCCESS;
	}
	case SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE:
		return scic_sds_stp_request_udma_general_frame_handler(sci_req, frame_index);
	case SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE:
		/* Use the general frame handler to copy the resposne data */
		status = scic_sds_stp_request_udma_general_frame_handler(sci_req, frame_index);

		if (status != SCI_SUCCESS)
			return status;

		scic_sds_stp_request_udma_complete_request(sci_req,
							   SCU_TASK_DONE_CHECK_RESPONSE,
							   SCI_FAILURE_IO_RESPONSE_VALID);
		return SCI_SUCCESS;
	case SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE: {
		struct dev_to_host_fis *frame_header;
		u32 *frame_buffer;

		status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
								       frame_index,
								       (void **)&frame_header);

		if (status != SCI_SUCCESS) {
			dev_err(scic_to_dev(scic),
				"%s: SCIC IO Request 0x%p could not get frame header "
				"for frame index %d, status %x\n",
				__func__, stp_req, frame_index, status);

			return status;
		}

		switch (frame_header->fis_type) {
		case FIS_REGD2H:
			scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
								      frame_index,
								      (void **)&frame_buffer);

			scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
							       frame_header,
							       frame_buffer);

			/* The command has completed with error */
			scic_sds_request_set_status(sci_req, SCU_TASK_DONE_CHECK_RESPONSE,
						    SCI_FAILURE_IO_RESPONSE_VALID);
			break;

		default:
			dev_warn(scic_to_dev(scic),
				 "%s: IO Request:0x%p Frame Id:%d protocol "
				  "violation occurred\n", __func__, stp_req,
				  frame_index);

			scic_sds_request_set_status(sci_req, SCU_TASK_DONE_UNEXP_FIS,
						    SCI_FAILURE_PROTOCOL_VIOLATION);
			break;
		}

		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_COMPLETED);

		/* Frame has been decoded return it to the controller */
		scic_sds_controller_release_frame(scic, frame_index);

		return status;
	}
	case SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE: {
		struct isci_request *ireq = sci_req_to_ireq(sci_req);
		struct sas_task *task = isci_request_access_task(ireq);
		struct dev_to_host_fis *frame_header;
		u32 *frame_buffer;

		status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
								       frame_index,
								       (void **)&frame_header);

		if (status != SCI_SUCCESS) {
			dev_err(scic_to_dev(scic),
				"%s: SCIC IO Request 0x%p could not get frame header "
				"for frame index %d, status %x\n",
				__func__, stp_req, frame_index, status);
			return status;
		}

		switch (frame_header->fis_type) {
		case FIS_PIO_SETUP:
			/* Get from the frame buffer the PIO Setup Data */
			scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
								      frame_index,
								      (void **)&frame_buffer);

			/* Get the data from the PIO Setup The SCU Hardware returns
			 * first word in the frame_header and the rest of the data is in
			 * the frame buffer so we need to back up one dword
			 */

			/* transfer_count: first 16bits in the 4th dword */
			stp_req->type.pio.pio_transfer_bytes = frame_buffer[3] & 0xffff;

			/* ending_status: 4th byte in the 3rd dword */
			stp_req->type.pio.ending_status = (frame_buffer[2] >> 24) & 0xff;

			scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
							       frame_header,
							       frame_buffer);

			sci_req->stp.rsp.status = stp_req->type.pio.ending_status;

			/* The next state is dependent on whether the
			 * request was PIO Data-in or Data out
			 */
			if (task->data_dir == DMA_FROM_DEVICE) {
				sci_base_state_machine_change_state(&sci_req->state_machine,
								    SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE);
			} else if (task->data_dir == DMA_TO_DEVICE) {
				/* Transmit data */
				status = scic_sds_stp_request_pio_data_out_transmit_data(sci_req);
				if (status != SCI_SUCCESS)
					break;
				sci_base_state_machine_change_state(&sci_req->state_machine,
								    SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE);
			}
			break;
		case FIS_SETDEVBITS:
			sci_base_state_machine_change_state(&sci_req->state_machine,
							    SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE);
			break;
		case FIS_REGD2H:
			if (frame_header->status & ATA_BUSY) {
				/* Now why is the drive sending a D2H Register FIS when
				 * it is still busy?  Do nothing since we are still in
				 * the right state.
				 */
				dev_dbg(scic_to_dev(scic),
					"%s: SCIC PIO Request 0x%p received "
					"D2H Register FIS with BSY status "
					"0x%x\n", __func__, stp_req,
					frame_header->status);
				break;
			}

			scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
								      frame_index,
								      (void **)&frame_buffer);

			scic_sds_controller_copy_sata_response(&sci_req->stp.req,
							       frame_header,
							       frame_buffer);

			scic_sds_request_set_status(sci_req,
						    SCU_TASK_DONE_CHECK_RESPONSE,
						    SCI_FAILURE_IO_RESPONSE_VALID);

			sci_base_state_machine_change_state(&sci_req->state_machine,
							    SCI_BASE_REQUEST_STATE_COMPLETED);
			break;
		default:
			/* FIXME: what do we do here? */
			break;
		}

		/* Frame is decoded return it to the controller */
		scic_sds_controller_release_frame(scic, frame_index);

		return status;
	}
	case SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE: {
		struct dev_to_host_fis *frame_header;
		struct sata_fis_data *frame_buffer;

		status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
								       frame_index,
								       (void **)&frame_header);

		if (status != SCI_SUCCESS) {
			dev_err(scic_to_dev(scic),
				"%s: SCIC IO Request 0x%p could not get frame header "
				"for frame index %d, status %x\n",
				__func__, stp_req, frame_index, status);
			return status;
		}

		if (frame_header->fis_type != FIS_DATA) {
			dev_err(scic_to_dev(scic),
				"%s: SCIC PIO Request 0x%p received frame %d "
				"with fis type 0x%02x when expecting a data "
				"fis.\n", __func__, stp_req, frame_index,
				frame_header->fis_type);

			scic_sds_request_set_status(sci_req,
						    SCU_TASK_DONE_GOOD,
						    SCI_FAILURE_IO_REQUIRES_SCSI_ABORT);

			sci_base_state_machine_change_state(&sci_req->state_machine,
							    SCI_BASE_REQUEST_STATE_COMPLETED);

			/* Frame is decoded return it to the controller */
			scic_sds_controller_release_frame(scic, frame_index);
			return status;
		}

		if (stp_req->type.pio.request_current.sgl_pair == NULL) {
			sci_req->saved_rx_frame_index = frame_index;
			stp_req->type.pio.pio_transfer_bytes = 0;
		} else {
			scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
								      frame_index,
								      (void **)&frame_buffer);

			status = scic_sds_stp_request_pio_data_in_copy_data(stp_req,
									    (u8 *)frame_buffer);

			/* Frame is decoded return it to the controller */
			scic_sds_controller_release_frame(scic, frame_index);
		}

		/* Check for the end of the transfer, are there more
		 * bytes remaining for this data transfer
		 */
		if (status != SCI_SUCCESS ||
		    stp_req->type.pio.pio_transfer_bytes != 0)
			return status;

		if ((stp_req->type.pio.ending_status & ATA_BUSY) == 0) {
			scic_sds_request_set_status(sci_req,
						    SCU_TASK_DONE_CHECK_RESPONSE,
						    SCI_FAILURE_IO_RESPONSE_VALID);

			sci_base_state_machine_change_state(&sci_req->state_machine,
							    SCI_BASE_REQUEST_STATE_COMPLETED);
		} else {
			sci_base_state_machine_change_state(&sci_req->state_machine,
							    SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE);
		}
		return status;
	}
	case SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE: {
		struct dev_to_host_fis *frame_header;
		u32 *frame_buffer;

		status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
								       frame_index,
								       (void **)&frame_header);
		if (status != SCI_SUCCESS) {
			dev_err(scic_to_dev(scic),
				"%s: SCIC IO Request 0x%p could not get frame header "
				"for frame index %d, status %x\n",
				__func__, stp_req, frame_index, status);
			return status;
		}

		switch (frame_header->fis_type) {
		case FIS_REGD2H:
			scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
								      frame_index,
								      (void **)&frame_buffer);

			scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
							       frame_header,
							       frame_buffer);

			/* The command has completed with error */
			scic_sds_request_set_status(sci_req,
						    SCU_TASK_DONE_CHECK_RESPONSE,
						    SCI_FAILURE_IO_RESPONSE_VALID);
			break;
		default:
			dev_warn(scic_to_dev(scic),
				 "%s: IO Request:0x%p Frame Id:%d protocol "
				 "violation occurred\n", __func__, stp_req,
				 frame_index);

			scic_sds_request_set_status(sci_req, SCU_TASK_DONE_UNEXP_FIS,
						    SCI_FAILURE_PROTOCOL_VIOLATION);
			break;
		}

		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_COMPLETED);

		/* Frame has been decoded return it to the controller */
		scic_sds_controller_release_frame(scic, frame_index);

		return status;
	}
	case SCI_BASE_REQUEST_STATE_ABORTING:
		/* TODO: Is it even possible to get an unsolicited frame in the
		 * aborting state?
		 */
		scic_sds_controller_release_frame(scic, frame_index);
		return SCI_SUCCESS;
	default:
		dev_warn(scic_to_dev(scic),
			 "%s: SCIC IO Request given unexpected frame %x while in "
			 "state %d\n", __func__, frame_index, state);

		scic_sds_controller_release_frame(scic, frame_index);
		return SCI_FAILURE_INVALID_STATE;
	}
}

2054 2055
static enum sci_status stp_request_udma_await_tc_event(struct scic_sds_request *sci_req,
						       u32 completion_code)
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
{
	enum sci_status status = SCI_SUCCESS;

	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
		scic_sds_stp_request_udma_complete_request(sci_req,
							   SCU_TASK_DONE_GOOD,
							   SCI_SUCCESS);
		break;
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
2067 2068 2069 2070
		/* We must check ther response buffer to see if the D2H
		 * Register FIS was received before we got the TC
		 * completion.
		 */
2071 2072 2073 2074 2075 2076 2077 2078
		if (sci_req->stp.rsp.fis_type == FIS_REGD2H) {
			scic_sds_remote_device_suspend(sci_req->target_device,
				SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));

			scic_sds_stp_request_udma_complete_request(sci_req,
								   SCU_TASK_DONE_CHECK_RESPONSE,
								   SCI_FAILURE_IO_RESPONSE_VALID);
		} else {
2079 2080 2081 2082 2083
			/* If we have an error completion status for the
			 * TC then we can expect a D2H register FIS from
			 * the device so we must change state to wait
			 * for it
			 */
2084 2085 2086 2087 2088
			sci_base_state_machine_change_state(&sci_req->state_machine,
				SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE);
		}
		break;

2089 2090 2091 2092 2093 2094
	/* TODO Check to see if any of these completion status need to
	 * wait for the device to host register fis.
	 */
	/* TODO We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR
	 * - this comes only for B0
	 */
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_INV_FIS_LEN):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_R_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CMD_LL_R_ERR):
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CRC_ERR):
		scic_sds_remote_device_suspend(sci_req->target_device,
			SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
	/* Fall through to the default case */
	default:
		/* All other completion status cause the IO to be complete. */
		scic_sds_stp_request_udma_complete_request(sci_req,
					SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
					SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
		break;
	}

	return status;
}

2114 2115
static enum sci_status stp_request_soft_reset_await_h2d_asserted_tc_event(struct scic_sds_request *sci_req,
									  u32 completion_code)
2116 2117 2118
{
	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
2119 2120
		scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
					    SCI_SUCCESS);
2121

2122 2123
		sci_base_state_machine_change_state(&sci_req->state_machine,
			SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE);
2124 2125 2126 2127 2128 2129
		break;

	default:
		/*
		 * All other completion status cause the IO to be complete.  If a NAK
		 * was received, then it is up to the user to retry the request. */
2130
		scic_sds_request_set_status(sci_req,
2131
			SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
2132
			SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
2133

2134 2135
		sci_base_state_machine_change_state(&sci_req->state_machine,
						    SCI_BASE_REQUEST_STATE_COMPLETED);
2136
		break;
2137 2138 2139 2140 2141
	}

	return SCI_SUCCESS;
}

2142
static enum sci_status stp_request_soft_reset_await_h2d_diagnostic_tc_event(
2143 2144 2145 2146 2147
	struct scic_sds_request *sci_req,
	u32 completion_code)
{
	switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
	case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
2148 2149
		scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
					    SCI_SUCCESS);
2150

2151 2152
		sci_base_state_machine_change_state(&sci_req->state_machine,
			SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE);
2153 2154 2155
		break;

	default:
2156 2157 2158 2159 2160
		/* All other completion status cause the IO to be complete.  If
		 * a NAK was received, then it is up to the user to retry the
		 * request.
		 */
		scic_sds_request_set_status(sci_req,
2161
			SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
2162
			SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
2163

2164
		sci_base_state_machine_change_state(&sci_req->state_machine,
2165
						    SCI_BASE_REQUEST_STATE_COMPLETED);
2166 2167 2168 2169 2170 2171
		break;
	}

	return SCI_SUCCESS;
}

2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
enum sci_status
scic_sds_io_request_tc_completion(struct scic_sds_request *sci_req, u32 completion_code)
{
	enum sci_base_request_states state;
	struct scic_sds_controller *scic = sci_req->owning_controller;

	state = sci_req->state_machine.current_state_id;

	switch (state) {
		case SCI_BASE_REQUEST_STATE_STARTED:
			return request_started_state_tc_event(sci_req, completion_code);
		case SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION:
			return ssp_task_request_await_tc_event(sci_req, completion_code);
		case SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE:
			return smp_request_await_response_tc_event(sci_req, completion_code);
		case SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION:
			return smp_request_await_tc_event(sci_req, completion_code);
		case SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE:
			return stp_request_udma_await_tc_event(sci_req, completion_code);
		case SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE:
			return stp_request_non_data_await_h2d_tc_event(sci_req, completion_code);
		case SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE:
			return stp_request_pio_await_h2d_completion_tc_event(sci_req, completion_code);
		case SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE:
			return pio_data_out_tx_done_tc_event(sci_req, completion_code);
		case SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE:
			return stp_request_soft_reset_await_h2d_asserted_tc_event(sci_req, completion_code);
		case SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE:
			return stp_request_soft_reset_await_h2d_diagnostic_tc_event(sci_req, completion_code);
		case SCI_BASE_REQUEST_STATE_ABORTING:
			return request_aborting_state_tc_event(sci_req, completion_code);
		default:
			dev_warn(scic_to_dev(scic),
				"%s: SCIC IO Request given task completion notification %x "
				"while in wrong state %d\n", __func__, completion_code,
				state);
			return SCI_FAILURE_INVALID_STATE;
	}
}

2212
/**
2213 2214 2215 2216 2217 2218
 * isci_request_process_response_iu() - This function sets the status and
 *    response iu, in the task struct, from the request object for the upper
 *    layer driver.
 * @sas_task: This parameter is the task struct from the upper layer driver.
 * @resp_iu: This parameter points to the response iu of the completed request.
 * @dev: This parameter specifies the linux device struct.
2219 2220 2221
 *
 * none.
 */
2222 2223 2224 2225
static void isci_request_process_response_iu(
	struct sas_task *task,
	struct ssp_response_iu *resp_iu,
	struct device *dev)
2226
{
2227 2228 2229 2230 2231
	dev_dbg(dev,
		"%s: resp_iu = %p "
		"resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
		"resp_iu->response_data_len = %x, "
		"resp_iu->sense_data_len = %x\nrepsonse data: ",
2232
		__func__,
2233 2234 2235 2236 2237
		resp_iu,
		resp_iu->status,
		resp_iu->datapres,
		resp_iu->response_data_len,
		resp_iu->sense_data_len);
2238

2239
	task->task_status.stat = resp_iu->status;
2240

2241 2242 2243
	/* libsas updates the task status fields based on the response iu. */
	sas_ssp_task_response(dev, task, resp_iu);
}
2244

2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
/**
 * isci_request_set_open_reject_status() - This function prepares the I/O
 *    completion for OPEN_REJECT conditions.
 * @request: This parameter is the completed isci_request object.
 * @response_ptr: This parameter specifies the service response for the I/O.
 * @status_ptr: This parameter specifies the exec status for the I/O.
 * @complete_to_host_ptr: This parameter specifies the action to be taken by
 *    the LLDD with respect to completing this request or forcing an abort
 *    condition on the I/O.
 * @open_rej_reason: This parameter specifies the encoded reason for the
 *    abandon-class reject.
 *
 * none.
 */
static void isci_request_set_open_reject_status(
	struct isci_request *request,
	struct sas_task *task,
	enum service_response *response_ptr,
	enum exec_status *status_ptr,
	enum isci_completion_selection *complete_to_host_ptr,
	enum sas_open_rej_reason open_rej_reason)
{
	/* Task in the target is done. */
	request->complete_in_target       = true;
	*response_ptr                     = SAS_TASK_UNDELIVERED;
	*status_ptr                       = SAS_OPEN_REJECT;
	*complete_to_host_ptr             = isci_perform_normal_io_completion;
	task->task_status.open_rej_reason = open_rej_reason;
}
2274

2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
/**
 * isci_request_handle_controller_specific_errors() - This function decodes
 *    controller-specific I/O completion error conditions.
 * @request: This parameter is the completed isci_request object.
 * @response_ptr: This parameter specifies the service response for the I/O.
 * @status_ptr: This parameter specifies the exec status for the I/O.
 * @complete_to_host_ptr: This parameter specifies the action to be taken by
 *    the LLDD with respect to completing this request or forcing an abort
 *    condition on the I/O.
 *
 * none.
 */
static void isci_request_handle_controller_specific_errors(
	struct isci_remote_device *isci_device,
	struct isci_request *request,
	struct sas_task *task,
	enum service_response *response_ptr,
	enum exec_status *status_ptr,
	enum isci_completion_selection *complete_to_host_ptr)
{
	unsigned int cstatus;
2296

2297
	cstatus = request->sci.scu_status;
2298

2299 2300 2301 2302
	dev_dbg(&request->isci_host->pdev->dev,
		"%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
		"- controller status = 0x%x\n",
		__func__, request, cstatus);
2303

2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322
	/* Decode the controller-specific errors; most
	 * important is to recognize those conditions in which
	 * the target may still have a task outstanding that
	 * must be aborted.
	 *
	 * Note that there are SCU completion codes being
	 * named in the decode below for which SCIC has already
	 * done work to handle them in a way other than as
	 * a controller-specific completion code; these are left
	 * in the decode below for completeness sake.
	 */
	switch (cstatus) {
	case SCU_TASK_DONE_DMASETUP_DIRERR:
	/* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
	case SCU_TASK_DONE_XFERCNT_ERR:
		/* Also SCU_TASK_DONE_SMP_UFI_ERR: */
		if (task->task_proto == SAS_PROTOCOL_SMP) {
			/* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
			*response_ptr = SAS_TASK_COMPLETE;
2323

2324 2325
			/* See if the device has been/is being stopped. Note
			 * that we ignore the quiesce state, since we are
2326 2327
			 * concerned about the actual device state.
			 */
2328 2329 2330 2331 2332
			if ((isci_device->status == isci_stopping) ||
			    (isci_device->status == isci_stopped))
				*status_ptr = SAS_DEVICE_UNKNOWN;
			else
				*status_ptr = SAS_ABORTED_TASK;
2333

2334
			request->complete_in_target = true;
2335

2336 2337 2338 2339 2340
			*complete_to_host_ptr =
				isci_perform_normal_io_completion;
		} else {
			/* Task in the target is not done. */
			*response_ptr = SAS_TASK_UNDELIVERED;
2341

2342 2343 2344 2345 2346
			if ((isci_device->status == isci_stopping) ||
			    (isci_device->status == isci_stopped))
				*status_ptr = SAS_DEVICE_UNKNOWN;
			else
				*status_ptr = SAM_STAT_TASK_ABORTED;
2347

2348
			request->complete_in_target = false;
2349

2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366
			*complete_to_host_ptr =
				isci_perform_error_io_completion;
		}

		break;

	case SCU_TASK_DONE_CRC_ERR:
	case SCU_TASK_DONE_NAK_CMD_ERR:
	case SCU_TASK_DONE_EXCESS_DATA:
	case SCU_TASK_DONE_UNEXP_FIS:
	/* Also SCU_TASK_DONE_UNEXP_RESP: */
	case SCU_TASK_DONE_VIIT_ENTRY_NV:       /* TODO - conditions? */
	case SCU_TASK_DONE_IIT_ENTRY_NV:        /* TODO - conditions? */
	case SCU_TASK_DONE_RNCNV_OUTBOUND:      /* TODO - conditions? */
		/* These are conditions in which the target
		 * has completed the task, so that no cleanup
		 * is necessary.
2367
		 */
2368
		*response_ptr = SAS_TASK_COMPLETE;
2369 2370 2371 2372 2373 2374 2375

		/* See if the device has been/is being stopped. Note
		 * that we ignore the quiesce state, since we are
		 * concerned about the actual device state.
		 */
		if ((isci_device->status == isci_stopping) ||
		    (isci_device->status == isci_stopped))
2376
			*status_ptr = SAS_DEVICE_UNKNOWN;
2377
		else
2378
			*status_ptr = SAS_ABORTED_TASK;
2379

2380
		request->complete_in_target = true;
2381

2382
		*complete_to_host_ptr = isci_perform_normal_io_completion;
2383 2384 2385
		break;


2386 2387 2388 2389
	/* Note that the only open reject completion codes seen here will be
	 * abandon-class codes; all others are automatically retried in the SCU.
	 */
	case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2390

2391 2392 2393 2394
		isci_request_set_open_reject_status(
			request, task, response_ptr, status_ptr,
			complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
		break;
2395

2396
	case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2397

2398 2399 2400 2401 2402 2403 2404
		/* Note - the return of AB0 will change when
		 * libsas implements detection of zone violations.
		 */
		isci_request_set_open_reject_status(
			request, task, response_ptr, status_ptr,
			complete_to_host_ptr, SAS_OREJ_RESV_AB0);
		break;
2405

2406
	case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2407

2408 2409 2410 2411
		isci_request_set_open_reject_status(
			request, task, response_ptr, status_ptr,
			complete_to_host_ptr, SAS_OREJ_RESV_AB1);
		break;
2412

2413
	case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2414

2415 2416 2417 2418
		isci_request_set_open_reject_status(
			request, task, response_ptr, status_ptr,
			complete_to_host_ptr, SAS_OREJ_RESV_AB2);
		break;
2419

2420
	case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2421

2422 2423 2424 2425
		isci_request_set_open_reject_status(
			request, task, response_ptr, status_ptr,
			complete_to_host_ptr, SAS_OREJ_RESV_AB3);
		break;
2426

2427
	case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2428

2429 2430 2431 2432
		isci_request_set_open_reject_status(
			request, task, response_ptr, status_ptr,
			complete_to_host_ptr, SAS_OREJ_BAD_DEST);
		break;
2433

2434
	case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
2435

2436 2437 2438 2439
		isci_request_set_open_reject_status(
			request, task, response_ptr, status_ptr,
			complete_to_host_ptr, SAS_OREJ_STP_NORES);
		break;
2440

2441
	case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
2442

2443 2444 2445 2446
		isci_request_set_open_reject_status(
			request, task, response_ptr, status_ptr,
			complete_to_host_ptr, SAS_OREJ_EPROTO);
		break;
2447

2448
	case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
2449

2450 2451 2452 2453
		isci_request_set_open_reject_status(
			request, task, response_ptr, status_ptr,
			complete_to_host_ptr, SAS_OREJ_CONN_RATE);
		break;
2454

2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483
	case SCU_TASK_DONE_LL_R_ERR:
	/* Also SCU_TASK_DONE_ACK_NAK_TO: */
	case SCU_TASK_DONE_LL_PERR:
	case SCU_TASK_DONE_LL_SY_TERM:
	/* Also SCU_TASK_DONE_NAK_ERR:*/
	case SCU_TASK_DONE_LL_LF_TERM:
	/* Also SCU_TASK_DONE_DATA_LEN_ERR: */
	case SCU_TASK_DONE_LL_ABORT_ERR:
	case SCU_TASK_DONE_SEQ_INV_TYPE:
	/* Also SCU_TASK_DONE_UNEXP_XR: */
	case SCU_TASK_DONE_XR_IU_LEN_ERR:
	case SCU_TASK_DONE_INV_FIS_LEN:
	/* Also SCU_TASK_DONE_XR_WD_LEN: */
	case SCU_TASK_DONE_SDMA_ERR:
	case SCU_TASK_DONE_OFFSET_ERR:
	case SCU_TASK_DONE_MAX_PLD_ERR:
	case SCU_TASK_DONE_LF_ERR:
	case SCU_TASK_DONE_SMP_RESP_TO_ERR:  /* Escalate to dev reset? */
	case SCU_TASK_DONE_SMP_LL_RX_ERR:
	case SCU_TASK_DONE_UNEXP_DATA:
	case SCU_TASK_DONE_UNEXP_SDBFIS:
	case SCU_TASK_DONE_REG_ERR:
	case SCU_TASK_DONE_SDB_ERR:
	case SCU_TASK_DONE_TASK_ABORT:
	default:
		/* Task in the target is not done. */
		*response_ptr = SAS_TASK_UNDELIVERED;
		*status_ptr = SAM_STAT_TASK_ABORTED;
		request->complete_in_target = false;
2484

2485 2486 2487 2488
		*complete_to_host_ptr = isci_perform_error_io_completion;
		break;
	}
}
2489

2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508
/**
 * isci_task_save_for_upper_layer_completion() - This function saves the
 *    request for later completion to the upper layer driver.
 * @host: This parameter is a pointer to the host on which the the request
 *    should be queued (either as an error or success).
 * @request: This parameter is the completed request.
 * @response: This parameter is the response code for the completed task.
 * @status: This parameter is the status code for the completed task.
 *
 * none.
 */
static void isci_task_save_for_upper_layer_completion(
	struct isci_host *host,
	struct isci_request *request,
	enum service_response response,
	enum exec_status status,
	enum isci_completion_selection task_notification_selection)
{
	struct sas_task *task = isci_request_access_task(request);
2509

2510 2511 2512
	task_notification_selection
		= isci_task_set_completion_status(task, response, status,
						  task_notification_selection);
2513

2514 2515 2516 2517
	/* Tasks aborted specifically by a call to the lldd_abort_task
	 * function should not be completed to the host in the regular path.
	 */
	switch (task_notification_selection) {
2518

2519
	case isci_perform_normal_io_completion:
2520

2521 2522 2523 2524 2525 2526 2527 2528 2529 2530
		/* Normal notification (task_done) */
		dev_dbg(&host->pdev->dev,
			"%s: Normal - task = %p, response=%d (%d), status=%d (%d)\n",
			__func__,
			task,
			task->task_status.resp, response,
			task->task_status.stat, status);
		/* Add to the completed list. */
		list_add(&request->completed_node,
			 &host->requests_to_complete);
2531

2532 2533 2534
		/* Take the request off the device's pending request list. */
		list_del_init(&request->dev_node);
		break;
2535

2536 2537 2538 2539 2540 2541 2542 2543 2544 2545
	case isci_perform_aborted_io_completion:
		/* No notification to libsas because this request is
		 * already in the abort path.
		 */
		dev_warn(&host->pdev->dev,
			 "%s: Aborted - task = %p, response=%d (%d), status=%d (%d)\n",
			 __func__,
			 task,
			 task->task_status.resp, response,
			 task->task_status.stat, status);
2546

2547 2548 2549 2550
		/* Wake up whatever process was waiting for this
		 * request to complete.
		 */
		WARN_ON(request->io_request_completion == NULL);
2551

2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
		if (request->io_request_completion != NULL) {

			/* Signal whoever is waiting that this
			* request is complete.
			*/
			complete(request->io_request_completion);
		}
		break;

	case isci_perform_error_io_completion:
		/* Use sas_task_abort */
		dev_warn(&host->pdev->dev,
			 "%s: Error - task = %p, response=%d (%d), status=%d (%d)\n",
			 __func__,
			 task,
			 task->task_status.resp, response,
			 task->task_status.stat, status);
		/* Add to the aborted list. */
		list_add(&request->completed_node,
			 &host->requests_to_errorback);
		break;

	default:
		dev_warn(&host->pdev->dev,
			 "%s: Unknown - task = %p, response=%d (%d), status=%d (%d)\n",
			 __func__,
			 task,
			 task->task_status.resp, response,
			 task->task_status.stat, status);

		/* Add to the error to libsas list. */
		list_add(&request->completed_node,
			 &host->requests_to_errorback);
		break;
	}
}

static void isci_request_io_request_complete(struct isci_host *isci_host,
					     struct isci_request *request,
					     enum sci_io_status completion_status)
{
	struct sas_task *task = isci_request_access_task(request);
	struct ssp_response_iu *resp_iu;
	void *resp_buf;
	unsigned long task_flags;
	struct isci_remote_device *isci_device   = request->isci_device;
	enum service_response response       = SAS_TASK_UNDELIVERED;
	enum exec_status status         = SAS_ABORTED_TASK;
	enum isci_request_status request_status;
	enum isci_completion_selection complete_to_host
		= isci_perform_normal_io_completion;

	dev_dbg(&isci_host->pdev->dev,
		"%s: request = %p, task = %p,\n"
		"task->data_dir = %d completion_status = 0x%x\n",
		__func__,
		request,
		task,
		task->data_dir,
		completion_status);

	spin_lock(&request->state_lock);
	request_status = isci_request_get_state(request);

	/* Decode the request status.  Note that if the request has been
	 * aborted by a task management function, we don't care
	 * what the status is.
	 */
	switch (request_status) {

	case aborted:
		/* "aborted" indicates that the request was aborted by a task
		 * management function, since once a task management request is
		 * perfomed by the device, the request only completes because
		 * of the subsequent driver terminate.
		 *
		 * Aborted also means an external thread is explicitly managing
		 * this request, so that we do not complete it up the stack.
		 *
		 * The target is still there (since the TMF was successful).
		 */
		request->complete_in_target = true;
		response = SAS_TASK_COMPLETE;

		/* See if the device has been/is being stopped. Note
		 * that we ignore the quiesce state, since we are
		 * concerned about the actual device state.
		 */
		if ((isci_device->status == isci_stopping)
		    || (isci_device->status == isci_stopped)
		    )
			status = SAS_DEVICE_UNKNOWN;
		else
			status = SAS_ABORTED_TASK;

		complete_to_host = isci_perform_aborted_io_completion;
		/* This was an aborted request. */

		spin_unlock(&request->state_lock);
		break;

	case aborting:
		/* aborting means that the task management function tried and
		 * failed to abort the request. We need to note the request
		 * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
		 * target as down.
		 *
		 * Aborting also means an external thread is explicitly managing
		 * this request, so that we do not complete it up the stack.
		 */
		request->complete_in_target = true;
		response = SAS_TASK_UNDELIVERED;

		if ((isci_device->status == isci_stopping) ||
		    (isci_device->status == isci_stopped))
			/* The device has been /is being stopped. Note that
			 * we ignore the quiesce state, since we are
			 * concerned about the actual device state.
			 */
			status = SAS_DEVICE_UNKNOWN;
		else
			status = SAS_PHY_DOWN;

		complete_to_host = isci_perform_aborted_io_completion;

		/* This was an aborted request. */

		spin_unlock(&request->state_lock);
		break;

	case terminating:

		/* This was an terminated request.  This happens when
		 * the I/O is being terminated because of an action on
		 * the device (reset, tear down, etc.), and the I/O needs
		 * to be completed up the stack.
		 */
		request->complete_in_target = true;
		response = SAS_TASK_UNDELIVERED;

		/* See if the device has been/is being stopped. Note
		 * that we ignore the quiesce state, since we are
		 * concerned about the actual device state.
		 */
		if ((isci_device->status == isci_stopping) ||
		    (isci_device->status == isci_stopped))
			status = SAS_DEVICE_UNKNOWN;
		else
			status = SAS_ABORTED_TASK;

		complete_to_host = isci_perform_aborted_io_completion;

		/* This was a terminated request. */

		spin_unlock(&request->state_lock);
		break;

	default:

		/* The request is done from an SCU HW perspective. */
		request->status = completed;

		spin_unlock(&request->state_lock);

		/* This is an active request being completed from the core. */
		switch (completion_status) {

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

			if (sas_protocol_ata(task->task_proto)) {
				resp_buf = &request->sci.stp.rsp;
				isci_request_process_stp_response(task,
								  resp_buf);
			} else if (SAS_PROTOCOL_SSP == task->task_proto) {

				/* crack the iu response buffer. */
				resp_iu = &request->sci.ssp.rsp;
				isci_request_process_response_iu(task, resp_iu,
								 &isci_host->pdev->dev);

			} else if (SAS_PROTOCOL_SMP == task->task_proto) {

				dev_err(&isci_host->pdev->dev,
					"%s: SCI_IO_FAILURE_RESPONSE_VALID: "
					"SAS_PROTOCOL_SMP protocol\n",
					__func__);

			} else
				dev_err(&isci_host->pdev->dev,
					"%s: unknown protocol\n", __func__);

			/* use the task status set in the task struct by the
			 * isci_request_process_response_iu call.
			 */
			request->complete_in_target = true;
			response = task->task_status.resp;
			status = task->task_status.stat;
			break;

		case SCI_IO_SUCCESS:
		case SCI_IO_SUCCESS_IO_DONE_EARLY:

			response = SAS_TASK_COMPLETE;
			status   = SAM_STAT_GOOD;
			request->complete_in_target = true;

			if (task->task_proto == SAS_PROTOCOL_SMP) {
				void *rsp = &request->sci.smp.rsp;

				dev_dbg(&isci_host->pdev->dev,
					"%s: SMP protocol completion\n",
					__func__);

				sg_copy_from_buffer(
					&task->smp_task.smp_resp, 1,
					rsp, sizeof(struct smp_resp));
			} else if (completion_status
				   == SCI_IO_SUCCESS_IO_DONE_EARLY) {

				/* This was an SSP / STP / SATA transfer.
				 * There is a possibility that less data than
				 * the maximum was transferred.
				 */
				u32 transferred_length = sci_req_tx_bytes(&request->sci);

				task->task_status.residual
					= task->total_xfer_len - transferred_length;

				/* If there were residual bytes, call this an
				 * underrun.
				 */
				if (task->task_status.residual != 0)
					status = SAS_DATA_UNDERRUN;

				dev_dbg(&isci_host->pdev->dev,
					"%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
					__func__,
					status);

			} else
				dev_dbg(&isci_host->pdev->dev,
					"%s: SCI_IO_SUCCESS\n",
					__func__);

			break;

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

			/* The request was terminated explicitly.  No handling
			 * is needed in the SCSI error handler path.
			 */
			request->complete_in_target = true;
			response = SAS_TASK_UNDELIVERED;

			/* See if the device has been/is being stopped. Note
			 * that we ignore the quiesce state, since we are
			 * concerned about the actual device state.
			 */
			if ((isci_device->status == isci_stopping) ||
			    (isci_device->status == isci_stopped))
				status = SAS_DEVICE_UNKNOWN;
			else
				status = SAS_ABORTED_TASK;

			complete_to_host = isci_perform_normal_io_completion;
			break;

		case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:

			isci_request_handle_controller_specific_errors(
				isci_device, request, task, &response, &status,
				&complete_to_host);

			break;

		case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
			/* This is a special case, in that the I/O completion
			 * is telling us that the device needs a reset.
			 * In order for the device reset condition to be
			 * noticed, the I/O has to be handled in the error
			 * handler.  Set the reset flag and cause the
			 * SCSI error thread to be scheduled.
			 */
			spin_lock_irqsave(&task->task_state_lock, task_flags);
			task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
2847 2848
			spin_unlock_irqrestore(&task->task_state_lock, task_flags);

2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905
			/* Fail the I/O. */
			response = SAS_TASK_UNDELIVERED;
			status = SAM_STAT_TASK_ABORTED;

			complete_to_host = isci_perform_error_io_completion;
			request->complete_in_target = false;
			break;

		default:
			/* Catch any otherwise unhandled error codes here. */
			dev_warn(&isci_host->pdev->dev,
				 "%s: invalid completion code: 0x%x - "
				 "isci_request = %p\n",
				 __func__, completion_status, request);

			response = SAS_TASK_UNDELIVERED;

			/* See if the device has been/is being stopped. Note
			 * that we ignore the quiesce state, since we are
			 * concerned about the actual device state.
			 */
			if ((isci_device->status == isci_stopping) ||
			    (isci_device->status == isci_stopped))
				status = SAS_DEVICE_UNKNOWN;
			else
				status = SAS_ABORTED_TASK;

			complete_to_host = isci_perform_error_io_completion;
			request->complete_in_target = false;
			break;
		}
		break;
	}

	isci_request_unmap_sgl(request, isci_host->pdev);

	/* Put the completed request on the correct list */
	isci_task_save_for_upper_layer_completion(isci_host, request, response,
						  status, complete_to_host
						  );

	/* complete the io request to the core. */
	scic_controller_complete_io(&isci_host->sci,
				    &isci_device->sci,
				    &request->sci);
	/* set terminated handle so it cannot be completed or
	 * terminated again, and to cause any calls into abort
	 * task to recognize the already completed case.
	 */
	request->terminated = true;

	isci_host_can_dequeue(isci_host, 1);
}

static void scic_sds_request_started_state_enter(void *object)
{
	struct scic_sds_request *sci_req = object;
2906 2907 2908
	struct sci_base_state_machine *sm = &sci_req->state_machine;
	struct isci_request *ireq = sci_req_to_ireq(sci_req);
	struct domain_device *dev = sci_dev_to_domain(sci_req->target_device);
2909 2910 2911 2912 2913 2914
	struct sas_task *task;

	/* XXX as hch said always creating an internal sas_task for tmf
	 * requests would simplify the driver
	 */
	task = ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL;
2915

2916 2917
	/* all unaccelerated request types (non ssp or ncq) handled with
	 * substates
2918
	 */
2919 2920 2921
	if (!task && dev->dev_type == SAS_END_DEV) {
		sci_base_state_machine_change_state(sm,
			SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION);
2922 2923 2924 2925 2926
	} else if (!task &&
		   (isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_high ||
		    isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_low)) {
		sci_base_state_machine_change_state(sm,
			SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE);
2927 2928 2929
	} else if (task && task->task_proto == SAS_PROTOCOL_SMP) {
		sci_base_state_machine_change_state(sm,
			SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE);
2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941
	} else if (task && sas_protocol_ata(task->task_proto) &&
		   !task->ata_task.use_ncq) {
		u32 state;

		if (task->data_dir == DMA_NONE)
			 state = SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE;
		else if (task->ata_task.dma_xfer)
			state = SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE;
		else /* PIO */
			state = SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE;

		sci_base_state_machine_change_state(sm, state);
2942
	}
2943 2944 2945 2946 2947
}

static void scic_sds_request_completed_state_enter(void *object)
{
	struct scic_sds_request *sci_req = object;
2948
	struct scic_sds_controller *scic = sci_req->owning_controller;
2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965
	struct isci_host *ihost = scic_to_ihost(scic);
	struct isci_request *ireq = sci_req_to_ireq(sci_req);

	/* Tell the SCI_USER that the IO request is complete */
	if (sci_req->is_task_management_request == false)
		isci_request_io_request_complete(ihost, ireq,
						 sci_req->sci_status);
	else
		isci_task_request_complete(ihost, ireq, sci_req->sci_status);
}

static void scic_sds_request_aborting_state_enter(void *object)
{
	struct scic_sds_request *sci_req = object;

	/* Setting the abort bit in the Task Context is required by the silicon. */
	sci_req->task_context_buffer->abort = 1;
2966 2967
}

2968 2969 2970 2971 2972
static void scic_sds_stp_request_started_non_data_await_h2d_completion_enter(
	void *object)
{
	struct scic_sds_request *sci_req = object;

2973 2974
	scic_sds_remote_device_set_working_request(sci_req->target_device,
						   sci_req);
2975 2976
}

2977
static void scic_sds_stp_request_started_pio_await_h2d_completion_enter(void *object)
2978 2979 2980
{
	struct scic_sds_request *sci_req = object;

2981 2982
	scic_sds_remote_device_set_working_request(sci_req->target_device,
						   sci_req);
2983 2984
}

2985
static void scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter(void *object)
2986 2987 2988
{
	struct scic_sds_request *sci_req = object;

2989 2990
	scic_sds_remote_device_set_working_request(sci_req->target_device,
						   sci_req);
2991 2992
}

2993
static void scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter(void *object)
2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009
{
	struct scic_sds_request *sci_req = object;
	struct scu_task_context *task_context;
	struct host_to_dev_fis *h2d_fis;
	enum sci_status status;

	/* Clear the SRST bit */
	h2d_fis = &sci_req->stp.cmd;
	h2d_fis->control = 0;

	/* Clear the TC control bit */
	task_context = scic_sds_controller_get_task_context_buffer(
		sci_req->owning_controller, sci_req->io_tag);
	task_context->control_frame = 0;

	status = scic_controller_continue_io(sci_req);
3010
	WARN_ONCE(status != SCI_SUCCESS, "isci: continue io failure\n");
3011 3012
}

3013
static const struct sci_base_state scic_sds_request_state_table[] = {
3014 3015
	[SCI_BASE_REQUEST_STATE_INITIAL] = { },
	[SCI_BASE_REQUEST_STATE_CONSTRUCTED] = { },
3016 3017
	[SCI_BASE_REQUEST_STATE_STARTED] = {
		.enter_state = scic_sds_request_started_state_enter,
3018 3019 3020 3021
	},
	[SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE] = {
		.enter_state = scic_sds_stp_request_started_non_data_await_h2d_completion_enter,
	},
3022
	[SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE] = { },
3023 3024 3025
	[SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE] = {
		.enter_state = scic_sds_stp_request_started_pio_await_h2d_completion_enter,
	},
3026 3027 3028 3029 3030
	[SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE] = { },
	[SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE] = { },
	[SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE] = { },
	[SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE] = { },
	[SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE] = { },
3031 3032 3033 3034 3035 3036
	[SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE] = {
		.enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter,
	},
	[SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE] = {
		.enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter,
	},
3037 3038 3039 3040 3041
	[SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE] = { },
	[SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION] = { },
	[SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE] = { },
	[SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE] = { },
	[SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION] = { },
3042 3043 3044 3045 3046 3047
	[SCI_BASE_REQUEST_STATE_COMPLETED] = {
		.enter_state = scic_sds_request_completed_state_enter,
	},
	[SCI_BASE_REQUEST_STATE_ABORTING] = {
		.enter_state = scic_sds_request_aborting_state_enter,
	},
3048
	[SCI_BASE_REQUEST_STATE_FINAL] = { },
3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073
};

static void scic_sds_general_request_construct(struct scic_sds_controller *scic,
					       struct scic_sds_remote_device *sci_dev,
					       u16 io_tag, struct scic_sds_request *sci_req)
{
	sci_base_state_machine_construct(&sci_req->state_machine, sci_req,
			scic_sds_request_state_table, SCI_BASE_REQUEST_STATE_INITIAL);
	sci_base_state_machine_start(&sci_req->state_machine);

	sci_req->io_tag = io_tag;
	sci_req->owning_controller = scic;
	sci_req->target_device = sci_dev;
	sci_req->protocol = SCIC_NO_PROTOCOL;
	sci_req->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
	sci_req->device_sequence = scic_sds_remote_device_get_sequence(sci_dev);

	sci_req->sci_status   = SCI_SUCCESS;
	sci_req->scu_status   = 0;
	sci_req->post_context = 0xFFFFFFFF;

	sci_req->is_task_management_request = false;

	if (io_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
		sci_req->was_tag_assigned_by_user = false;
3074
		sci_req->task_context_buffer = &sci_req->tc;
3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093
	} else {
		sci_req->was_tag_assigned_by_user = true;

		sci_req->task_context_buffer =
			scic_sds_controller_get_task_context_buffer(scic, io_tag);
	}
}

static enum sci_status
scic_io_request_construct(struct scic_sds_controller *scic,
			  struct scic_sds_remote_device *sci_dev,
			  u16 io_tag, struct scic_sds_request *sci_req)
{
	struct domain_device *dev = sci_dev_to_domain(sci_dev);
	enum sci_status status = SCI_SUCCESS;

	/* Build the common part of the request */
	scic_sds_general_request_construct(scic, sci_dev, io_tag, sci_req);

3094
	if (sci_dev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
3095 3096 3097
		return SCI_FAILURE_INVALID_REMOTE_DEVICE;

	if (dev->dev_type == SAS_END_DEV)
3098 3099
		/* pass */;
	else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
3100
		memset(&sci_req->stp.cmd, 0, sizeof(sci_req->stp.cmd));
3101
	else if (dev_is_expander(dev))
3102
		memset(&sci_req->smp.cmd, 0, sizeof(sci_req->smp.cmd));
3103 3104
	else
		return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
3105

3106 3107
	memset(sci_req->task_context_buffer, 0,
	       offsetof(struct scu_task_context, sgl_pair_ab));
3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121

	return status;
}

enum sci_status scic_task_request_construct(struct scic_sds_controller *scic,
					    struct scic_sds_remote_device *sci_dev,
					    u16 io_tag, struct scic_sds_request *sci_req)
{
	struct domain_device *dev = sci_dev_to_domain(sci_dev);
	enum sci_status status = SCI_SUCCESS;

	/* Build the common part of the request */
	scic_sds_general_request_construct(scic, sci_dev, io_tag, sci_req);

3122 3123
	if (dev->dev_type == SAS_END_DEV ||
	    dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
3124 3125
		sci_req->is_task_management_request = true;
		memset(sci_req->task_context_buffer, 0, sizeof(struct scu_task_context));
3126 3127
	} else
		status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177

	return status;
}

static enum sci_status isci_request_ssp_request_construct(
	struct isci_request *request)
{
	enum sci_status status;

	dev_dbg(&request->isci_host->pdev->dev,
		"%s: request = %p\n",
		__func__,
		request);
	status = scic_io_request_construct_basic_ssp(&request->sci);
	return status;
}

static enum sci_status isci_request_stp_request_construct(
	struct isci_request *request)
{
	struct sas_task *task = isci_request_access_task(request);
	enum sci_status status;
	struct host_to_dev_fis *register_fis;

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

	/* Get the host_to_dev_fis from the core and copy
	 * the fis from the task into it.
	 */
	register_fis = isci_sata_task_to_fis_copy(task);

	status = scic_io_request_construct_basic_sata(&request->sci);

	/* Set the ncq tag in the fis, from the queue
	 * command in the task.
	 */
	if (isci_sata_is_task_ncq(task)) {

		isci_sata_set_ncq_tag(
			register_fis,
			task
			);
	}

	return status;
}

3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345
/*
 * This function will fill in the SCU Task Context for a SMP request. The
 *    following important settings are utilized: -# task_type ==
 *    SCU_TASK_TYPE_SMP.  This simply indicates that a normal request type
 *    (i.e. non-raw frame) is being utilized to perform task management. -#
 *    control_frame == 1.  This ensures that the proper endianess is set so
 *    that the bytes are transmitted in the right order for a smp request frame.
 * @sci_req: This parameter specifies the smp request object being
 *    constructed.
 *
 */
static void
scu_smp_request_construct_task_context(struct scic_sds_request *sci_req,
				       struct smp_req *smp_req)
{
	dma_addr_t dma_addr;
	struct scic_sds_controller *scic;
	struct scic_sds_remote_device *sci_dev;
	struct scic_sds_port *sci_port;
	struct scu_task_context *task_context;
	ssize_t word_cnt = sizeof(struct smp_req) / sizeof(u32);

	/* byte swap the smp request. */
	sci_swab32_cpy(&sci_req->smp.cmd, smp_req,
		       word_cnt);

	task_context = scic_sds_request_get_task_context(sci_req);

	scic = scic_sds_request_get_controller(sci_req);
	sci_dev = scic_sds_request_get_device(sci_req);
	sci_port = scic_sds_request_get_port(sci_req);

	/*
	 * Fill in the TC with the its required data
	 * 00h
	 */
	task_context->priority = 0;
	task_context->initiator_request = 1;
	task_context->connection_rate = sci_dev->connection_rate;
	task_context->protocol_engine_index =
		scic_sds_controller_get_protocol_engine_group(scic);
	task_context->logical_port_index = scic_sds_port_get_index(sci_port);
	task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
	task_context->abort = 0;
	task_context->valid = SCU_TASK_CONTEXT_VALID;
	task_context->context_type = SCU_TASK_CONTEXT_TYPE;

	/* 04h */
	task_context->remote_node_index = sci_dev->rnc.remote_node_index;
	task_context->command_code = 0;
	task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;

	/* 08h */
	task_context->link_layer_control = 0;
	task_context->do_not_dma_ssp_good_response = 1;
	task_context->strict_ordering = 0;
	task_context->control_frame = 1;
	task_context->timeout_enable = 0;
	task_context->block_guard_enable = 0;

	/* 0ch */
	task_context->address_modifier = 0;

	/* 10h */
	task_context->ssp_command_iu_length = smp_req->req_len;

	/* 14h */
	task_context->transfer_length_bytes = 0;

	/*
	 * 18h ~ 30h, protocol specific
	 * since commandIU has been build by framework at this point, we just
	 * copy the frist DWord from command IU to this location. */
	memcpy(&task_context->type.smp, &sci_req->smp.cmd, sizeof(u32));

	/*
	 * 40h
	 * "For SMP you could program it to zero. We would prefer that way
	 * so that done code will be consistent." - Venki
	 */
	task_context->task_phase = 0;

	if (sci_req->was_tag_assigned_by_user) {
		/*
		 * Build the task context now since we have already read
		 * the data
		 */
		sci_req->post_context =
			(SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
			 (scic_sds_controller_get_protocol_engine_group(scic) <<
			  SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
			 (scic_sds_port_get_index(sci_port) <<
			  SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
			 scic_sds_io_tag_get_index(sci_req->io_tag));
	} else {
		/*
		 * Build the task context now since we have already read
		 * the data.
		 * I/O tag index is not assigned because we have to wait
		 * until we get a TCi.
		 */
		sci_req->post_context =
			(SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
			 (scic_sds_controller_get_protocol_engine_group(scic) <<
			  SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
			 (scic_sds_port_get_index(sci_port) <<
			  SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
	}

	/*
	 * Copy the physical address for the command buffer to the SCU Task
	 * Context command buffer should not contain command header.
	 */
	dma_addr = scic_io_request_get_dma_addr(sci_req,
						((char *) &sci_req->smp.cmd) +
						sizeof(u32));

	task_context->command_iu_upper = upper_32_bits(dma_addr);
	task_context->command_iu_lower = lower_32_bits(dma_addr);

	/* SMP response comes as UF, so no need to set response IU address. */
	task_context->response_iu_upper = 0;
	task_context->response_iu_lower = 0;
}

static enum sci_status scic_io_request_construct_smp(struct scic_sds_request *sci_req)
{
	struct smp_req *smp_req = kmalloc(sizeof(*smp_req), GFP_KERNEL);

	if (!smp_req)
		return SCI_FAILURE_INSUFFICIENT_RESOURCES;

	sci_req->protocol = SCIC_SMP_PROTOCOL;

	/* Construct the SMP SCU Task Context */
	memcpy(smp_req, &sci_req->smp.cmd, sizeof(*smp_req));

	/*
	 * Look at the SMP requests' header fields; for certain SAS 1.x SMP
	 * functions under SAS 2.0, a zero request length really indicates
	 * a non-zero default length. */
	if (smp_req->req_len == 0) {
		switch (smp_req->func) {
		case SMP_DISCOVER:
		case SMP_REPORT_PHY_ERR_LOG:
		case SMP_REPORT_PHY_SATA:
		case SMP_REPORT_ROUTE_INFO:
			smp_req->req_len = 2;
			break;
		case SMP_CONF_ROUTE_INFO:
		case SMP_PHY_CONTROL:
		case SMP_PHY_TEST_FUNCTION:
			smp_req->req_len = 9;
			break;
			/* Default - zero is a valid default for 2.0. */
		}
	}

	scu_smp_request_construct_task_context(sci_req, smp_req);

	sci_base_state_machine_change_state(&sci_req->state_machine,
		SCI_BASE_REQUEST_STATE_CONSTRUCTED);

	kfree(smp_req);

	return SCI_SUCCESS;
}

3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484
/*
 * isci_smp_request_build() - This function builds the smp request.
 * @ireq: This parameter points to the isci_request allocated in the
 *    request construct function.
 *
 * SCI_SUCCESS on successfull completion, or specific failure code.
 */
static enum sci_status isci_smp_request_build(struct isci_request *ireq)
{
	enum sci_status status = SCI_FAILURE;
	struct sas_task *task = isci_request_access_task(ireq);
	struct scic_sds_request *sci_req = &ireq->sci;

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

	dev_dbg(&ireq->isci_host->pdev->dev,
		"%s: smp_req len = %d\n",
		__func__,
		task->smp_task.smp_req.length);

	/* copy the smp_command to the address; */
	sg_copy_to_buffer(&task->smp_task.smp_req, 1,
			  &sci_req->smp.cmd,
			  sizeof(struct smp_req));

	status = scic_io_request_construct_smp(sci_req);
	if (status != SCI_SUCCESS)
		dev_warn(&ireq->isci_host->pdev->dev,
			 "%s: failed with status = %d\n",
			 __func__,
			 status);

	return status;
}

/**
 * isci_io_request_build() - This function builds the io request object.
 * @isci_host: This parameter specifies the ISCI host object
 * @request: This parameter points to the isci_request object allocated in the
 *    request construct function.
 * @sci_device: This parameter is the handle for the sci core's remote device
 *    object that is the destination for this request.
 *
 * SCI_SUCCESS on successfull completion, or specific failure code.
 */
static enum sci_status isci_io_request_build(
	struct isci_host *isci_host,
	struct isci_request *request,
	struct isci_remote_device *isci_device)
{
	enum sci_status status = SCI_SUCCESS;
	struct sas_task *task = isci_request_access_task(request);
	struct scic_sds_remote_device *sci_device = &isci_device->sci;

	dev_dbg(&isci_host->pdev->dev,
		"%s: isci_device = 0x%p; request = %p, "
		"num_scatter = %d\n",
		__func__,
		isci_device,
		request,
		task->num_scatter);

	/* map the sgl addresses, if present.
	 * libata does the mapping for sata devices
	 * before we get the request.
	 */
	if (task->num_scatter &&
	    !sas_protocol_ata(task->task_proto) &&
	    !(SAS_PROTOCOL_SMP & task->task_proto)) {

		request->num_sg_entries = dma_map_sg(
			&isci_host->pdev->dev,
			task->scatter,
			task->num_scatter,
			task->data_dir
			);

		if (request->num_sg_entries == 0)
			return SCI_FAILURE_INSUFFICIENT_RESOURCES;
	}

	/* build the common request object. For now,
	 * we will let the core allocate the IO tag.
	 */
	status = scic_io_request_construct(&isci_host->sci, sci_device,
					   SCI_CONTROLLER_INVALID_IO_TAG,
					   &request->sci);

	if (status != SCI_SUCCESS) {
		dev_warn(&isci_host->pdev->dev,
			 "%s: failed request construct\n",
			 __func__);
		return SCI_FAILURE;
	}

	switch (task->task_proto) {
	case SAS_PROTOCOL_SMP:
		status = isci_smp_request_build(request);
		break;
	case SAS_PROTOCOL_SSP:
		status = isci_request_ssp_request_construct(request);
		break;
	case SAS_PROTOCOL_SATA:
	case SAS_PROTOCOL_STP:
	case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
		status = isci_request_stp_request_construct(request);
		break;
	default:
		dev_warn(&isci_host->pdev->dev,
			 "%s: unknown protocol\n", __func__);
		return SCI_FAILURE;
	}

	return SCI_SUCCESS;
}

/**
 * isci_request_alloc_core() - This function gets the request object from the
 *    isci_host dma cache.
 * @isci_host: This parameter specifies the ISCI host object
 * @isci_request: This parameter will contain the pointer to the new
 *    isci_request object.
 * @isci_device: This parameter is the pointer to the isci remote device object
 *    that is the destination for this request.
 * @gfp_flags: This parameter specifies the os allocation flags.
 *
 * SCI_SUCCESS on successfull completion, or specific failure code.
 */
static int isci_request_alloc_core(
	struct isci_host *isci_host,
	struct isci_request **isci_request,
	struct isci_remote_device *isci_device,
	gfp_t gfp_flags)
{
	int ret = 0;
	dma_addr_t handle;
	struct isci_request *request;

3485

3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496
	/* get pointer to dma memory. This actually points
	 * to both the isci_remote_device object and the
	 * sci object. The isci object is at the beginning
	 * of the memory allocated here.
	 */
	request = dma_pool_alloc(isci_host->dma_pool, gfp_flags, &handle);
	if (!request) {
		dev_warn(&isci_host->pdev->dev,
			 "%s: dma_pool_alloc returned NULL\n", __func__);
		return -ENOMEM;
	}
3497

3498 3499 3500 3501 3502 3503 3504
	/* initialize the request object.	*/
	spin_lock_init(&request->state_lock);
	request->request_daddr = handle;
	request->isci_host = isci_host;
	request->isci_device = isci_device;
	request->io_request_completion = NULL;
	request->terminated = false;
3505

3506
	request->num_sg_entries = 0;
3507

3508
	request->complete_in_target = false;
3509

3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533
	INIT_LIST_HEAD(&request->completed_node);
	INIT_LIST_HEAD(&request->dev_node);

	*isci_request = request;
	isci_request_change_state(request, allocated);

	return ret;
}

static int isci_request_alloc_io(
	struct isci_host *isci_host,
	struct sas_task *task,
	struct isci_request **isci_request,
	struct isci_remote_device *isci_device,
	gfp_t gfp_flags)
{
	int retval = isci_request_alloc_core(isci_host, isci_request,
					     isci_device, gfp_flags);

	if (!retval) {
		(*isci_request)->ttype_ptr.io_task_ptr = task;
		(*isci_request)->ttype                 = io_task;

		task->lldd_task = *isci_request;
3534
	}
3535 3536
	return retval;
}
3537

3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559
/**
 * isci_request_alloc_tmf() - This function gets the request object from the
 *    isci_host dma cache and initializes the relevant fields as a sas_task.
 * @isci_host: This parameter specifies the ISCI host object
 * @sas_task: This parameter is the task struct from the upper layer driver.
 * @isci_request: This parameter will contain the pointer to the new
 *    isci_request object.
 * @isci_device: This parameter is the pointer to the isci remote device object
 *    that is the destination for this request.
 * @gfp_flags: This parameter specifies the os allocation flags.
 *
 * SCI_SUCCESS on successfull completion, or specific failure code.
 */
int isci_request_alloc_tmf(
	struct isci_host *isci_host,
	struct isci_tmf *isci_tmf,
	struct isci_request **isci_request,
	struct isci_remote_device *isci_device,
	gfp_t gfp_flags)
{
	int retval = isci_request_alloc_core(isci_host, isci_request,
					     isci_device, gfp_flags);
3560

3561
	if (!retval) {
3562

3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638
		(*isci_request)->ttype_ptr.tmf_task_ptr = isci_tmf;
		(*isci_request)->ttype = tmf_task;
	}
	return retval;
}

/**
 * isci_request_execute() - This function allocates the isci_request object,
 *    all fills in some common fields.
 * @isci_host: This parameter specifies the ISCI host object
 * @sas_task: This parameter is the task struct from the upper layer driver.
 * @isci_request: This parameter will contain the pointer to the new
 *    isci_request object.
 * @gfp_flags: This parameter specifies the os allocation flags.
 *
 * SCI_SUCCESS on successfull completion, or specific failure code.
 */
int isci_request_execute(
	struct isci_host *isci_host,
	struct sas_task *task,
	struct isci_request **isci_request,
	gfp_t gfp_flags)
{
	int ret = 0;
	struct scic_sds_remote_device *sci_device;
	enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
	struct isci_remote_device *isci_device;
	struct isci_request *request;
	unsigned long flags;

	isci_device = task->dev->lldd_dev;
	sci_device = &isci_device->sci;

	/* do common allocation and init of request object. */
	ret = isci_request_alloc_io(
		isci_host,
		task,
		&request,
		isci_device,
		gfp_flags
		);

	if (ret)
		goto out;

	status = isci_io_request_build(isci_host, request, isci_device);
	if (status != SCI_SUCCESS) {
		dev_warn(&isci_host->pdev->dev,
			 "%s: request_construct failed - status = 0x%x\n",
			 __func__,
			 status);
		goto out;
	}

	spin_lock_irqsave(&isci_host->scic_lock, flags);

	/* send the request, let the core assign the IO TAG.	*/
	status = scic_controller_start_io(&isci_host->sci, sci_device,
					  &request->sci,
					  SCI_CONTROLLER_INVALID_IO_TAG);
	if (status != SCI_SUCCESS &&
	    status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
		dev_warn(&isci_host->pdev->dev,
			 "%s: failed request start (0x%x)\n",
			 __func__, status);
		spin_unlock_irqrestore(&isci_host->scic_lock, flags);
		goto out;
	}

	/* Either I/O started OK, or the core has signaled that
	 * the device needs a target reset.
	 *
	 * In either case, hold onto the I/O for later.
	 *
	 * Update it's status and add it to the list in the
	 * remote device object.
3639
	 */
3640 3641
	isci_request_change_state(request, started);
	list_add(&request->dev_node, &isci_device->reqs_in_process);
3642

3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687
	if (status == SCI_SUCCESS) {
		/* Save the tag for possible task mgmt later. */
		request->io_tag = request->sci.io_tag;
	} else {
		/* The request did not really start in the
		 * hardware, so clear the request handle
		 * here so no terminations will be done.
		 */
		request->terminated = true;
	}
	spin_unlock_irqrestore(&isci_host->scic_lock, flags);

	if (status ==
	    SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
		/* Signal libsas that we need the SCSI error
		* handler thread to work on this I/O and that
		* we want a device reset.
		*/
		spin_lock_irqsave(&task->task_state_lock, flags);
		task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
		spin_unlock_irqrestore(&task->task_state_lock, flags);

		/* Cause this task to be scheduled in the SCSI error
		* handler thread.
		*/
		isci_execpath_callback(isci_host, task,
				       sas_task_abort);

		/* Change the status, since we are holding
		* the I/O until it is managed by the SCSI
		* error handler.
		*/
		status = SCI_SUCCESS;
	}

 out:
	if (status != SCI_SUCCESS) {
		/* release dma memory on failure. */
		isci_request_free(isci_host, request);
		request = NULL;
		ret = SCI_FAILURE;
	}

	*isci_request = request;
	return ret;
3688
}