scic_sds_port.c 78.4 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 61 62 63 64 65 66
/*
 * 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 "intel_sas.h"
#include "scic_controller.h"
#include "scic_phy.h"
#include "scic_port.h"
#include "scic_sds_controller.h"
#include "scic_sds_phy.h"
#include "scic_sds_port.h"
#include "scic_sds_remote_device.h"
#include "scic_sds_remote_node_context.h"
#include "scic_sds_request.h"
#include "sci_environment.h"
C
Christoph Hellwig 已提交
67
#include "scu_registers.h"
68 69 70 71 72

#define SCIC_SDS_PORT_MIN_TIMER_COUNT  (SCI_MAX_PORTS)
#define SCIC_SDS_PORT_MAX_TIMER_COUNT  (SCI_MAX_PORTS)

#define SCIC_SDS_PORT_HARD_RESET_TIMEOUT  (1000)
73
#define SCU_DUMMY_INDEX    (0xFFFF)
74 75 76 77


/**
 *
78
 * @sci_port: This is the port object to which the phy is being assigned.
79 80 81 82 83 84 85 86 87 88 89 90 91 92
 * @phy_index: This is the phy index that is being assigned to the port.
 *
 * This method will return a true value if the specified phy can be assigned to
 * this port The following is a list of phys for each port that are allowed: -
 * Port 0 - 3 2 1 0 - Port 1 -     1 - Port 2 - 3 2 - Port 3 - 3 This method
 * doesn't preclude all configurations.  It merely ensures that a phy is part
 * of the allowable set of phy identifiers for that port.  For example, one
 * could assign phy 3 to port 0 and no other phys.  Please refer to
 * scic_sds_port_is_phy_mask_valid() for information regarding whether the
 * phy_mask for a port can be supported. bool true if this is a valid phy
 * assignment for the port false if this is not a valid phy assignment for the
 * port
 */
bool scic_sds_port_is_valid_phy_assignment(
93
	struct scic_sds_port *sci_port,
94 95 96 97 98 99
	u32 phy_index)
{
	/* Initialize to invalid value. */
	u32 existing_phy_index = SCI_MAX_PHYS;
	u32 index;

100
	if ((sci_port->physical_port_index == 1) && (phy_index != 1)) {
101 102 103
		return false;
	}

104
	if (sci_port->physical_port_index == 3 && phy_index != 3) {
105 106 107 108
		return false;
	}

	if (
109
		(sci_port->physical_port_index == 2)
110 111 112 113 114 115
		&& ((phy_index == 0) || (phy_index == 1))
		) {
		return false;
	}

	for (index = 0; index < SCI_MAX_PHYS; index++) {
116
		if ((sci_port->phy_table[index] != NULL)
117 118 119 120 121 122 123 124 125 126
		    && (index != phy_index)) {
			existing_phy_index = index;
		}
	}

	/*
	 * Ensure that all of the phys in the port are capable of
	 * operating at the same maximum link rate. */
	if (
		(existing_phy_index < SCI_MAX_PHYS)
127
		&& (sci_port->owning_controller->user_parameters.sds1.phys[
128
			    phy_index].max_speed_generation !=
129
		    sci_port->owning_controller->user_parameters.sds1.phys[
130 131 132 133 134 135 136 137 138 139
			    existing_phy_index].max_speed_generation)
		)
		return false;

	return true;
}

/**
 * This method requests a list (mask) of the phys contained in the supplied SAS
 *    port.
140
 * @sci_port: a handle corresponding to the SAS port for which to return the
141 142 143 144 145
 *    phy mask.
 *
 * Return a bit mask indicating which phys are a part of this port. Each bit
 * corresponds to a phy identifier (e.g. bit 0 = phy id 0).
 */
146
static u32 scic_sds_port_get_phys(struct scic_sds_port *sci_port)
147 148 149 150 151 152 153
{
	u32 index;
	u32 mask;

	mask = 0;

	for (index = 0; index < SCI_MAX_PHYS; index++) {
154
		if (sci_port->phy_table[index] != NULL) {
155 156 157 158 159 160 161 162 163
			mask |= (1 << index);
		}
	}

	return mask;
}

/**
 *
164
 * @sci_port: This is the port object for which to determine if the phy mask
165 166 167 168 169 170 171 172 173
 *    can be supported.
 *
 * This method will return a true value if the port's phy mask can be supported
 * by the SCU. The following is a list of valid PHY mask configurations for
 * each port: - Port 0 - [[3  2] 1] 0 - Port 1 -        [1] - Port 2 - [[3] 2]
 * - Port 3 -  [3] This method returns a boolean indication specifying if the
 * phy mask can be supported. true if this is a valid phy assignment for the
 * port false if this is not a valid phy assignment for the port
 */
D
Dan Williams 已提交
174
static bool scic_sds_port_is_phy_mask_valid(
175
	struct scic_sds_port *sci_port,
176 177
	u32 phy_mask)
{
178
	if (sci_port->physical_port_index == 0) {
179 180 181 182 183
		if (((phy_mask & 0x0F) == 0x0F)
		    || ((phy_mask & 0x03) == 0x03)
		    || ((phy_mask & 0x01) == 0x01)
		    || (phy_mask == 0))
			return true;
184
	} else if (sci_port->physical_port_index == 1) {
185 186 187
		if (((phy_mask & 0x02) == 0x02)
		    || (phy_mask == 0))
			return true;
188
	} else if (sci_port->physical_port_index == 2) {
189 190 191 192
		if (((phy_mask & 0x0C) == 0x0C)
		    || ((phy_mask & 0x04) == 0x04)
		    || (phy_mask == 0))
			return true;
193
	} else if (sci_port->physical_port_index == 3) {
194 195 196 197 198 199 200 201 202 203
		if (((phy_mask & 0x08) == 0x08)
		    || (phy_mask == 0))
			return true;
	}

	return false;
}

/**
 *
204
 * @sci_port: This parameter specifies the port from which to return a
205 206 207 208 209 210 211 212 213 214
 *    connected phy.
 *
 * This method retrieves a currently active (i.e. connected) phy contained in
 * the port.  Currently, the lowest order phy that is connected is returned.
 * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
 * returned if there are no currently active (i.e. connected to a remote end
 * point) phys contained in the port. All other values specify a struct scic_sds_phy
 * object that is active in the port.
 */
static struct scic_sds_phy *scic_sds_port_get_a_connected_phy(
215
	struct scic_sds_port *sci_port
216 217 218 219 220 221 222 223
	) {
	u32 index;
	struct scic_sds_phy *phy;

	for (index = 0; index < SCI_MAX_PHYS; index++) {
		/*
		 * Ensure that the phy is both part of the port and currently
		 * connected to the remote end-point. */
224
		phy = sci_port->phy_table[index];
225 226
		if (
			(phy != NULL)
227
			&& scic_sds_port_active_phy(sci_port, phy)
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
			) {
			return phy;
		}
	}

	return NULL;
}

/**
 * scic_sds_port_set_phy() -
 * @out]: port The port object to which the phy assignement is being made.
 * @out]: phy The phy which is being assigned to the port.
 *
 * This method attempts to make the assignment of the phy to the port. If
 * successful the phy is assigned to the ports phy table. bool true if the phy
 * assignment can be made. false if the phy assignement can not be made. This
 * is a functional test that only fails if the phy is currently assigned to a
 * different port.
 */
D
Dan Williams 已提交
247
static enum sci_status scic_sds_port_set_phy(
248 249 250 251 252 253 254 255
	struct scic_sds_port *port,
	struct scic_sds_phy *phy)
{
	/*
	 * Check to see if we can add this phy to a port
	 * that means that the phy is not part of a port and that the port does
	 * not already have a phy assinged to the phy index. */
	if (
256 257
		(port->phy_table[phy->phy_index] == NULL)
		&& (scic_sds_phy_get_port(phy) == NULL)
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
		&& scic_sds_port_is_valid_phy_assignment(port, phy->phy_index)
		) {
		/*
		 * Phy is being added in the stopped state so we are in MPC mode
		 * make logical port index = physical port index */
		port->logical_port_index = port->physical_port_index;
		port->phy_table[phy->phy_index] = phy;
		scic_sds_phy_set_port(phy, port);

		return SCI_SUCCESS;
	}

	return SCI_FAILURE;
}

/**
 * scic_sds_port_clear_phy() -
 * @out]: port The port from which the phy is being cleared.
 * @out]: phy The phy being cleared from the port.
 *
 * This method will clear the phy assigned to this port.  This method fails if
 * this phy is not currently assinged to this port. bool true if the phy is
 * removed from the port. false if this phy is not assined to this port.
 */
D
Dan Williams 已提交
282
static enum sci_status scic_sds_port_clear_phy(
283 284 285 286 287 288 289 290 291 292 293 294 295 296
	struct scic_sds_port *port,
	struct scic_sds_phy *phy)
{
	/* Make sure that this phy is part of this port */
	if (
		(port->phy_table[phy->phy_index] == phy)
		&& (scic_sds_phy_get_port(phy) == port)
		) {
		/* Yep it is assigned to this port so remove it */
		scic_sds_phy_set_port(
			phy,
			&scic_sds_port_get_controller(port)->port_table[SCI_MAX_PORTS]
			);

297
		port->phy_table[phy->phy_index] = NULL;
298 299 300 301 302 303 304 305 306

		return SCI_SUCCESS;
	}

	return SCI_FAILURE;
}

/**
 * scic_sds_port_add_phy() -
307 308
 * @sci_port: This parameter specifies the port in which the phy will be added.
 * @sci_phy: This parameter is the phy which is to be added to the port.
309 310 311 312 313 314
 *
 * This method will add a PHY to the selected port. This method returns an
 * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other status
 * is failre to add the phy to the port.
 */
enum sci_status scic_sds_port_add_phy(
315 316
	struct scic_sds_port *sci_port,
	struct scic_sds_phy *sci_phy)
317
{
318 319
	return sci_port->state_handlers->add_phy_handler(
		       sci_port, sci_phy);
320 321 322 323 324
}


/**
 * scic_sds_port_remove_phy() -
325 326
 * @sci_port: This parameter specifies the port in which the phy will be added.
 * @sci_phy: This parameter is the phy which is to be added to the port.
327 328 329 330 331 332
 *
 * This method will remove the PHY from the selected PORT. This method returns
 * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any other
 * status is failre to add the phy to the port.
 */
enum sci_status scic_sds_port_remove_phy(
333 334
	struct scic_sds_port *sci_port,
	struct scic_sds_phy *sci_phy)
335
{
336 337
	return sci_port->state_handlers->remove_phy_handler(
		       sci_port, sci_phy);
338 339 340 341 342
}

/**
 * This method requests the SAS address for the supplied SAS port from the SCI
 *    implementation.
343
 * @sci_port: a handle corresponding to the SAS port for which to return the
344 345 346 347 348 349
 *    SAS address.
 * @sas_address: This parameter specifies a pointer to a SAS address structure
 *    into which the core will copy the SAS address for the port.
 *
 */
void scic_sds_port_get_sas_address(
350
	struct scic_sds_port *sci_port,
351 352 353 354 355 356 357 358
	struct sci_sas_address *sas_address)
{
	u32 index;

	sas_address->high = 0;
	sas_address->low  = 0;

	for (index = 0; index < SCI_MAX_PHYS; index++) {
359 360
		if (sci_port->phy_table[index] != NULL) {
			scic_sds_phy_get_sas_address(sci_port->phy_table[index], sas_address);
361 362 363 364 365 366
		}
	}
}

/**
 * This method will indicate which protocols are supported by this port.
367
 * @sci_port: a handle corresponding to the SAS port for which to return the
368 369 370 371 372 373 374 375
 *    supported protocols.
 * @protocols: This parameter specifies a pointer to an IAF protocol field
 *    structure into which the core will copy the protocol values for the port.
 *     The values are returned as part of a bit mask in order to allow for
 *    multi-protocol support.
 *
 */
static void scic_sds_port_get_protocols(
376
	struct scic_sds_port *sci_port,
377 378 379 380 381 382 383
	struct sci_sas_identify_address_frame_protocols *protocols)
{
	u8 index;

	protocols->u.all = 0;

	for (index = 0; index < SCI_MAX_PHYS; index++) {
384 385
		if (sci_port->phy_table[index] != NULL) {
			scic_sds_phy_get_protocols(sci_port->phy_table[index], protocols);
386 387 388 389 390 391 392
		}
	}
}

/**
 * This method requests the SAS address for the device directly attached to
 *    this SAS port.
393
 * @sci_port: a handle corresponding to the SAS port for which to return the
394 395 396 397 398 399 400
 *    SAS address.
 * @sas_address: This parameter specifies a pointer to a SAS address structure
 *    into which the core will copy the SAS address for the device directly
 *    attached to the port.
 *
 */
void scic_sds_port_get_attached_sas_address(
401
	struct scic_sds_port *sci_port,
402 403 404 405 406 407 408 409
	struct sci_sas_address *sas_address)
{
	struct sci_sas_identify_address_frame_protocols protocols;
	struct scic_sds_phy *phy;

	/*
	 * Ensure that the phy is both part of the port and currently
	 * connected to the remote end-point. */
410
	phy = scic_sds_port_get_a_connected_phy(sci_port);
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
	if (phy != NULL) {
		scic_sds_phy_get_attached_phy_protocols(phy, &protocols);

		if (!protocols.u.bits.stp_target) {
			scic_sds_phy_get_attached_sas_address(phy, sas_address);
		} else {
			scic_sds_phy_get_sas_address(phy, sas_address);
			sas_address->low += phy->phy_index;
		}
	} else {
		sas_address->high = 0;
		sas_address->low  = 0;
	}
}

/**
 * This method will indicate which protocols are supported by this remote
 *    device.
429
 * @sci_port: a handle corresponding to the SAS port for which to return the
430 431 432 433 434 435 436 437
 *    supported protocols.
 * @protocols: This parameter specifies a pointer to an IAF protocol field
 *    structure into which the core will copy the protocol values for the port.
 *     The values are returned as part of a bit mask in order to allow for
 *    multi-protocol support.
 *
 */
void scic_sds_port_get_attached_protocols(
438
	struct scic_sds_port *sci_port,
439 440 441 442 443 444 445
	struct sci_sas_identify_address_frame_protocols *protocols)
{
	struct scic_sds_phy *phy;

	/*
	 * Ensure that the phy is both part of the port and currently
	 * connected to the remote end-point. */
446
	phy = scic_sds_port_get_a_connected_phy(sci_port);
447 448 449 450 451 452 453
	if (phy != NULL)
		scic_sds_phy_get_attached_phy_protocols(phy, protocols);
	else
		protocols->u.all = 0;
}

/**
454
 * scic_sds_port_construct_dummy_rnc() - create dummy rnc for si workaround
455
 *
456 457
 * @sci_port: logical port on which we need to create the remote node context
 * @rni: remote node index for this remote node context.
458
 *
459 460 461
 * This routine will construct a dummy remote node context data structure
 * This structure will be posted to the hardware to work around a scheduler
 * error in the hardware.
462
 */
D
Dan Williams 已提交
463
static void scic_sds_port_construct_dummy_rnc(struct scic_sds_port *sci_port, u16 rni)
464 465
{
	union scu_remote_node_context *rnc;
466

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	rnc = &sci_port->owning_controller->remote_node_context_table[rni];

	memset(rnc, 0, sizeof(union scu_remote_node_context));

	rnc->ssp.remote_sas_address_hi = 0;
	rnc->ssp.remote_sas_address_lo = 0;

	rnc->ssp.remote_node_index = rni;
	rnc->ssp.remote_node_port_width = 1;
	rnc->ssp.logical_port_index = sci_port->physical_port_index;

	rnc->ssp.nexus_loss_timer_enable = false;
	rnc->ssp.check_bit = false;
	rnc->ssp.is_valid = true;
	rnc->ssp.is_remote_node_context = true;
	rnc->ssp.function_number = 0;
	rnc->ssp.arbitration_wait_time = 0;
}
485 486

/**
487 488 489 490 491
 * scic_sds_port_construct_dummy_task() - create dummy task for si workaround
 * @sci_port The logical port on which we need to create the
 *            remote node context.
 *            context.
 * @tci The remote node index for this remote node context.
492
 *
493 494 495
 * This routine will construct a dummy task context data structure.  This
 * structure will be posted to the hardwre to work around a scheduler error
 * in the hardware.
496 497
 *
 */
D
Dan Williams 已提交
498
static void scic_sds_port_construct_dummy_task(struct scic_sds_port *sci_port, u16 tci)
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 527 528 529 530 531
{
	struct scu_task_context *task_context;

	task_context = scic_sds_controller_get_task_context_buffer(sci_port->owning_controller, tci);

	memset(task_context, 0, sizeof(struct scu_task_context));

	task_context->abort = 0;
	task_context->priority = 0;
	task_context->initiator_request = 1;
	task_context->connection_rate = 1;
	task_context->protocol_engine_index = 0;
	task_context->logical_port_index = sci_port->physical_port_index;
	task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
	task_context->task_index = scic_sds_io_tag_get_index(tci);
	task_context->valid = SCU_TASK_CONTEXT_VALID;
	task_context->context_type = SCU_TASK_CONTEXT_TYPE;

	task_context->remote_node_index = sci_port->reserved_rni;
	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;
}

D
Dan Williams 已提交
532
static void scic_sds_port_destroy_dummy_resources(struct scic_sds_port *sci_port)
533 534 535 536 537 538 539 540 541 542 543 544 545 546
{
	struct scic_sds_controller *scic = sci_port->owning_controller;

	if (sci_port->reserved_tci != SCU_DUMMY_INDEX)
		scic_controller_free_io_tag(scic, sci_port->reserved_tci);

	if (sci_port->reserved_rni != SCU_DUMMY_INDEX)
		scic_sds_remote_node_table_release_remote_node_index(&scic->available_remote_nodes,
								     1, sci_port->reserved_rni);

	sci_port->reserved_rni = SCU_DUMMY_INDEX;
	sci_port->reserved_tci = SCU_DUMMY_INDEX;
}

547 548 549 550
/**
 * This method performs initialization of the supplied port. Initialization
 *    includes: - state machine initialization - member variable initialization
 *    - configuring the phy_mask
551
 * @sci_port:
552 553 554 555 556 557 558 559
 * @transport_layer_registers:
 * @port_task_scheduler_registers:
 * @port_configuration_regsiter:
 *
 * enum sci_status SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION This value is returned
 * if the phy being added to the port
 */
enum sci_status scic_sds_port_initialize(
560
	struct scic_sds_port *sci_port,
561 562 563
	void __iomem *port_task_scheduler_registers,
	void __iomem *port_configuration_regsiter,
	void __iomem *viit_registers)
564
{
565 566 567
	sci_port->port_task_scheduler_registers  = port_task_scheduler_registers;
	sci_port->port_pe_configuration_register = port_configuration_regsiter;
	sci_port->viit_registers                 = viit_registers;
568 569 570 571 572

	return SCI_SUCCESS;
}

/**
D
Dan Williams 已提交
573 574 575 576 577 578
 * scic_port_get_properties() - This method simply returns the properties
 *    regarding the port, such as: physical index, protocols, sas address, etc.
 * @port: this parameter specifies the port for which to retrieve the physical
 *    index.
 * @properties: This parameter specifies the properties structure into which to
 *    copy the requested information.
579
 *
D
Dan Williams 已提交
580 581 582 583
 * Indicate if the user specified a valid port. SCI_SUCCESS This value is
 * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
 * value is returned if the specified port is not valid.  When this value is
 * returned, no data is copied to the properties output parameter.
584 585 586 587 588
 */
enum sci_status scic_port_get_properties(
	struct scic_sds_port *port,
	struct scic_port_properties *prop)
{
589
	if ((port == NULL) ||
590 591 592 593 594 595 596 597 598 599 600 601 602
	    (port->logical_port_index == SCIC_SDS_DUMMY_PORT))
		return SCI_FAILURE_INVALID_PORT;

	prop->index    = port->logical_port_index;
	prop->phy_mask = scic_sds_port_get_phys(port);
	scic_sds_port_get_sas_address(port, &prop->local.sas_address);
	scic_sds_port_get_protocols(port, &prop->local.protocols);
	scic_sds_port_get_attached_sas_address(port, &prop->remote.sas_address);
	scic_sds_port_get_attached_protocols(port, &prop->remote.protocols);

	return SCI_SUCCESS;
}

D
Dan Williams 已提交
603
/**
D
Dave Jiang 已提交
604
 * scic_port_hard_reset() - perform port hard reset
D
Dan Williams 已提交
605 606 607 608
 * @port: a handle corresponding to the SAS port to be hard reset.
 * @reset_timeout: This parameter specifies the number of milliseconds in which
 *    the port reset operation should complete.
 *
D
Dave Jiang 已提交
609
 * The SCI User callback in scic_user_callbacks_t will only be called once for
D
Dan Williams 已提交
610 611 612 613
 * each phy in the SAS Port at completion of the hard reset sequence. Return a
 * status indicating whether the hard reset started successfully. SCI_SUCCESS
 * This value is returned if the hard reset operation started successfully.
 */
614 615 616 617
enum sci_status scic_port_hard_reset(
	struct scic_sds_port *port,
	u32 reset_timeout)
{
618 619
	return port->state_handlers->reset_handler(
		       port, reset_timeout);
620 621 622 623
}

/**
 * This method assigns the direct attached device ID for this port.
624
 *
625
 * @param[in] sci_port The port for which the direct attached device id is to
626 627 628
 *       be assigned.
 * @param[in] device_id The direct attached device ID to assign to the port.
 *       This will be the RNi for the device
629
 */
630
void scic_sds_port_setup_transports(
631
	struct scic_sds_port *sci_port,
632 633
	u32 device_id)
{
634
	u8 index;
635

636
	for (index = 0; index < SCI_MAX_PHYS; index++) {
637 638
		if (sci_port->active_phy_mask & (1 << index))
			scic_sds_phy_setup_transport(sci_port->phy_table[index], device_id);
639
	}
640 641 642 643
}

/**
 *
644 645
 * @sci_port: This is the port on which the phy should be enabled.
 * @sci_phy: This is the specific phy which to enable.
646 647 648
 * @do_notify_user: This parameter specifies whether to inform the user (via
 *    scic_cb_port_link_up()) as to the fact that a new phy as become ready.
 *
649 650
 * This function will activate the phy in the port.
 * Activation includes: - adding
651 652 653
 * the phy to the port - enabling the Protocol Engine in the silicon. -
 * notifying the user that the link is up. none
 */
D
Dan Williams 已提交
654 655 656
static void scic_sds_port_activate_phy(struct scic_sds_port *sci_port,
				       struct scic_sds_phy *sci_phy,
				       bool do_notify_user)
657
{
D
Dan Williams 已提交
658
	struct scic_sds_controller *scic = scic_sds_port_get_controller(sci_port);
659
	struct sci_sas_identify_address_frame_protocols protocols;
660
	struct isci_host *ihost = sci_object_get_association(scic);
661

662
	scic_sds_phy_get_attached_phy_protocols(sci_phy, &protocols);
663 664

	/* If this is sata port then the phy has already been resumed */
665 666
	if (!protocols.u.bits.stp_target)
		scic_sds_phy_resume(sci_phy);
667

668
	sci_port->active_phy_mask |= 1 << sci_phy->phy_index;
669

670
	scic_sds_controller_clear_invalid_phy(scic, sci_phy);
671 672

	if (do_notify_user == true)
673
		isci_port_link_up(ihost, sci_port, sci_phy);
674 675
}

676 677 678
void scic_sds_port_deactivate_phy(struct scic_sds_port *sci_port,
				  struct scic_sds_phy *sci_phy,
				  bool do_notify_user)
679
{
D
Dan Williams 已提交
680
	struct scic_sds_controller *scic = scic_sds_port_get_controller(sci_port);
681 682 683 684 685
	struct isci_port *iport = sci_object_get_association(sci_port);
	struct isci_host *ihost = sci_object_get_association(scic);
	struct isci_phy *iphy = sci_object_get_association(sci_phy);

	sci_port->active_phy_mask &= ~(1 << sci_phy->phy_index);
686

687
	sci_phy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
688 689

	/* Re-assign the phy back to the LP as if it were a narrow port */
C
Christoph Hellwig 已提交
690 691
	writel(sci_phy->phy_index,
		&sci_port->port_pe_configuration_register[sci_phy->phy_index]);
692 693

	if (do_notify_user == true)
694
		isci_port_link_down(ihost, iphy, iport);
695 696 697 698
}

/**
 *
699 700
 * @sci_port: This is the port on which the phy should be disabled.
 * @sci_phy: This is the specific phy which to disabled.
701
 *
702
 * This function will disable the phy and report that the phy is not valid for
703 704 705
 * this port object. None
 */
static void scic_sds_port_invalid_link_up(
706 707
	struct scic_sds_port *sci_port,
	struct scic_sds_phy *sci_phy)
708
{
709 710
	struct scic_sds_controller *scic =
		scic_sds_port_get_controller(sci_port);
711 712

	/*
713 714 715 716 717 718 719
	 * Check to see if we have alreay reported this link as bad and if
	 * not go ahead and tell the SCI_USER that we have discovered an
	 * invalid link.
	 */
	if ((scic->invalid_phy_mask & (1 << sci_phy->phy_index)) == 0) {
		scic_sds_controller_set_invalid_phy(scic, sci_phy);
		isci_port_invalid_link_up(scic, sci_port, sci_phy);
720 721 722
	}
}

D
Dan Williams 已提交
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
/**
 * scic_sds_port_general_link_up_handler - phy can be assigned to port?
 * @sci_port: scic_sds_port object for which has a phy that has gone link up.
 * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
 * @do_notify_user: This parameter specifies whether to inform the user (via
 *    scic_cb_port_link_up()) as to the fact that a new phy as become ready.
 *
 * Determine if this phy can be assigned to this
 * port . If the phy is not a valid PHY for
 * this port then the function will notify the user. A PHY can only be
 * part of a port if it's attached SAS ADDRESS is the same as all other PHYs in
 * the same port. none
 */
static void scic_sds_port_general_link_up_handler(struct scic_sds_port *sci_port,
						  struct scic_sds_phy *sci_phy,
						  bool do_notify_user)
{
	struct sci_sas_address port_sas_address;
	struct sci_sas_address phy_sas_address;

	scic_sds_port_get_attached_sas_address(sci_port, &port_sas_address);
	scic_sds_phy_get_attached_sas_address(sci_phy, &phy_sas_address);

	/* If the SAS address of the new phy matches the SAS address of
	 * other phys in the port OR this is the first phy in the port,
	 * then activate the phy and allow it to be used for operations
	 * in this port.
	 */
	if ((phy_sas_address.high == port_sas_address.high &&
	     phy_sas_address.low  == port_sas_address.low) ||
	    sci_port->active_phy_mask == 0) {
754
		struct sci_base_state_machine *sm = &sci_port->state_machine;
D
Dan Williams 已提交
755 756 757 758 759 760 761 762 763 764

		scic_sds_port_activate_phy(sci_port, sci_phy, do_notify_user);
		if (sm->current_state_id == SCI_BASE_PORT_STATE_RESETTING)
			sci_base_state_machine_change_state(sm, SCI_BASE_PORT_STATE_READY);
	} else
		scic_sds_port_invalid_link_up(sci_port, sci_phy);
}



765 766 767 768
/**
 * This method returns false if the port only has a single phy object assigned.
 *     If there are no phys or more than one phy then the method will return
 *    true.
769
 * @sci_port: The port for which the wide port condition is to be checked.
770 771 772 773
 *
 * bool true Is returned if this is a wide ported port. false Is returned if
 * this is a narrow port.
 */
774
static bool scic_sds_port_is_wide(struct scic_sds_port *sci_port)
775 776 777 778 779
{
	u32 index;
	u32 phy_count = 0;

	for (index = 0; index < SCI_MAX_PHYS; index++) {
780
		if (sci_port->phy_table[index] != NULL) {
781 782 783 784 785 786 787 788 789 790 791 792
			phy_count++;
		}
	}

	return phy_count != 1;
}

/**
 * This method is called by the PHY object when the link is detected. if the
 *    port wants the PHY to continue on to the link up state then the port
 *    layer must return true.  If the port object returns false the phy object
 *    must halt its attempt to go link up.
793 794
 * @sci_port: The port associated with the phy object.
 * @sci_phy: The phy object that is trying to go link up.
795 796 797 798 799 800 801 802
 *
 * true if the phy object can continue to the link up condition. true Is
 * returned if this phy can continue to the ready state. false Is returned if
 * can not continue on to the ready state. This notification is in place for
 * wide ports and direct attached phys.  Since there are no wide ported SATA
 * devices this could become an invalid port configuration.
 */
bool scic_sds_port_link_detected(
803 804
	struct scic_sds_port *sci_port,
	struct scic_sds_phy *sci_phy)
805 806 807
{
	struct sci_sas_identify_address_frame_protocols protocols;

808
	scic_sds_phy_get_attached_phy_protocols(sci_phy, &protocols);
809 810

	if (
811
		(sci_port->logical_port_index != SCIC_SDS_DUMMY_PORT)
812
		&& (protocols.u.bits.stp_target)
813
		&& scic_sds_port_is_wide(sci_port)
814
		) {
815
		scic_sds_port_invalid_link_up(sci_port, sci_phy);
816 817 818 819 820 821 822 823 824 825

		return false;
	}

	return true;
}

/**
 * This method is the entry point for the phy to inform the port that it is now
 *    in a ready state
826
 * @sci_port:
827 828 829 830
 *
 *
 */
void scic_sds_port_link_up(
831 832
	struct scic_sds_port *sci_port,
	struct scic_sds_phy *sci_phy)
833
{
834
	sci_phy->is_in_link_training = false;
835

836
	sci_port->state_handlers->link_up_handler(sci_port, sci_phy);
837 838 839 840 841
}

/**
 * This method is the entry point for the phy to inform the port that it is no
 *    longer in a ready state
842
 * @sci_port:
843 844 845 846
 *
 *
 */
void scic_sds_port_link_down(
847 848
	struct scic_sds_port *sci_port,
	struct scic_sds_phy *sci_phy)
849
{
850
	sci_port->state_handlers->link_down_handler(sci_port, sci_phy);
851 852 853 854
}

/**
 * This method is called to start an IO request on this port.
855 856 857
 * @sci_port:
 * @sci_dev:
 * @sci_req:
858 859 860 861
 *
 * enum sci_status
 */
enum sci_status scic_sds_port_start_io(
862 863 864
	struct scic_sds_port *sci_port,
	struct scic_sds_remote_device *sci_dev,
	struct scic_sds_request *sci_req)
865
{
866 867
	return sci_port->state_handlers->start_io_handler(
		       sci_port, sci_dev, sci_req);
868 869 870 871
}

/**
 * This method is called to complete an IO request to the port.
872 873 874
 * @sci_port:
 * @sci_dev:
 * @sci_req:
875 876 877 878
 *
 * enum sci_status
 */
enum sci_status scic_sds_port_complete_io(
879 880 881
	struct scic_sds_port *sci_port,
	struct scic_sds_remote_device *sci_dev,
	struct scic_sds_request *sci_req)
882
{
883 884
	return sci_port->state_handlers->complete_io_handler(
		       sci_port, sci_dev, sci_req);
885 886 887 888 889 890 891 892 893 894
}

/**
 * This method is provided to timeout requests for port operations. Mostly its
 *    for the port reset operation.
 *
 *
 */
static void scic_sds_port_timeout_handler(void *port)
{
895
	struct scic_sds_port *sci_port = port;
896 897 898
	u32 current_state;

	current_state = sci_base_state_machine_get_state(
899
		&sci_port->state_machine);
900 901 902

	if (current_state == SCI_BASE_PORT_STATE_RESETTING) {
		/*
903 904 905
		 * if the port is still in the resetting state then the
		 * timeout fired before the reset completed.
		 */
906
		sci_base_state_machine_change_state(
907
			&sci_port->state_machine,
908
			SCI_BASE_PORT_STATE_FAILED);
909 910 911
	} else if (current_state == SCI_BASE_PORT_STATE_STOPPED) {
		/*
		 * if the port is stopped then the start request failed
912 913 914
		 * In this case stay in the stopped state.
		 */
		dev_err(sciport_to_dev(sci_port),
915 916
			"%s: SCIC Port 0x%p failed to stop before tiemout.\n",
			__func__,
917
			sci_port);
918
	} else if (current_state == SCI_BASE_PORT_STATE_STOPPING) {
919 920 921 922 923 924 925 926
		/*
		 * if the port is still stopping then the stop has not
		 * completed
		 */
		isci_port_stop_complete(
				scic_sds_port_get_controller(sci_port),
				sci_port,
				SCI_FAILURE_TIMEOUT);
927 928
	} else {
		/*
929 930 931 932
		 * The port is in the ready state and we have a timer
		 * reporting a timeout this should not happen.
		 */
		dev_err(sciport_to_dev(sci_port),
933 934 935
			"%s: SCIC Port 0x%p is processing a timeout operation "
			"in state %d.\n",
			__func__,
936
			sci_port,
937 938 939 940 941 942 943 944 945 946 947
			current_state);
	}
}

/* --------------------------------------------------------------------------- */

/**
 * This function updates the hardwares VIIT entry for this port.
 *
 *
 */
948
static void scic_sds_port_update_viit_entry(struct scic_sds_port *sci_port)
949 950 951
{
	struct sci_sas_address sas_address;

952
	scic_sds_port_get_sas_address(sci_port, &sas_address);
953

C
Christoph Hellwig 已提交
954
	writel(sas_address.high,
955
		&sci_port->viit_registers->initiator_sas_address_hi);
C
Christoph Hellwig 已提交
956
	writel(sas_address.low,
957
		&sci_port->viit_registers->initiator_sas_address_lo);
958 959

	/* This value get cleared just in case its not already cleared */
960
	writel(0, &sci_port->viit_registers->reserved);
961 962

	/* We are required to update the status register last */
C
Christoph Hellwig 已提交
963 964
	writel(SCU_VIIT_ENTRY_ID_VIIT |
	       SCU_VIIT_IPPT_INITIATOR |
965
	       ((1 << sci_port->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
C
Christoph Hellwig 已提交
966
	       SCU_VIIT_STATUS_ALL_VALID,
967
	       &sci_port->viit_registers->status);
968 969 970 971 972 973
}

/**
 * This method returns the maximum allowed speed for data transfers on this
 *    port.  This maximum allowed speed evaluates to the maximum speed of the
 *    slowest phy in the port.
974
 * @sci_port: This parameter specifies the port for which to retrieve the
975 976 977 978 979
 *    maximum allowed speed.
 *
 * This method returns the maximum negotiated speed of the slowest phy in the
 * port.
 */
980
enum sas_linkrate scic_sds_port_get_max_allowed_speed(
981
	struct scic_sds_port *sci_port)
982
{
983 984 985
	u16 index;
	enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
	struct scic_sds_phy *phy = NULL;
986 987 988 989 990

	/*
	 * Loop through all of the phys in this port and find the phy with the
	 * lowest maximum link rate. */
	for (index = 0; index < SCI_MAX_PHYS; index++) {
991
		phy = sci_port->phy_table[index];
992 993
		if (
			(phy != NULL)
994
			&& (scic_sds_port_active_phy(sci_port, phy) == true)
995 996 997 998 999 1000 1001 1002 1003 1004 1005
			&& (phy->max_negotiated_speed < max_allowed_speed)
			)
			max_allowed_speed = phy->max_negotiated_speed;
	}

	return max_allowed_speed;
}


/**
 * This method passes the event to core user.
1006 1007
 * @sci_port: The port that a BCN happens.
 * @sci_phy: The phy that receives BCN.
1008 1009 1010
 *
 */
void scic_sds_port_broadcast_change_received(
1011 1012
	struct scic_sds_port *sci_port,
	struct scic_sds_phy *sci_phy)
1013
{
1014 1015 1016
	struct scic_sds_controller *scic = sci_port->owning_controller;
	struct isci_host *ihost = sci_object_get_association(scic);

1017
	/* notify the user. */
1018
	isci_port_bc_change_received(ihost, sci_port, sci_phy);
1019 1020 1021 1022 1023 1024
}


/**
 * This API methhod enables the broadcast change notification from underneath
 *    hardware.
1025
 * @sci_port: The port that a BCN had been disabled from.
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
 *
 */
void scic_port_enable_broadcast_change_notification(
	struct scic_sds_port *port)
{
	struct scic_sds_phy *phy;
	u32 register_value;
	u8 index;

	/* Loop through all of the phys to enable BCN. */
	for (index = 0; index < SCI_MAX_PHYS; index++) {
		phy = port->phy_table[index];
		if (phy != NULL) {
C
Christoph Hellwig 已提交
1039 1040
			register_value =
				readl(&phy->link_layer_registers->link_layer_control);
1041 1042

			/* clear the bit by writing 1. */
C
Christoph Hellwig 已提交
1043 1044
			writel(register_value,
				&phy->link_layer_registers->link_layer_control);
1045 1046 1047 1048 1049 1050 1051 1052 1053
		}
	}
}

/*
 * ****************************************************************************
 * *  READY SUBSTATE HANDLERS
 * **************************************************************************** */

1054
/*
1055 1056 1057 1058 1059
 * This method is the general ready state stop handler for the struct scic_sds_port
 * object.  This function will transition the ready substate machine to its
 * final state. enum sci_status SCI_SUCCESS
 */
static enum sci_status scic_sds_port_ready_substate_stop_handler(
1060
	struct scic_sds_port *port)
1061 1062
{
	sci_base_state_machine_change_state(
1063
		&port->state_machine,
1064 1065 1066 1067 1068 1069
		SCI_BASE_PORT_STATE_STOPPING
		);

	return SCI_SUCCESS;
}

1070
/*
1071 1072 1073 1074 1075 1076 1077 1078 1079
 * This method is the general ready substate complete io handler for the
 * struct scic_sds_port object.  This function decrments the outstanding request count
 * for this port object. enum sci_status SCI_SUCCESS
 */
static enum sci_status scic_sds_port_ready_substate_complete_io_handler(
	struct scic_sds_port *port,
	struct scic_sds_remote_device *device,
	struct scic_sds_request *io_request)
{
1080
	scic_sds_port_decrement_request_count(port);
1081 1082 1083 1084 1085

	return SCI_SUCCESS;
}

static enum sci_status scic_sds_port_ready_substate_add_phy_handler(
1086
	struct scic_sds_port *port,
M
Maciej Trela 已提交
1087
	struct scic_sds_phy *phy)
1088 1089 1090
{
	enum sci_status status;

M
Maciej Trela 已提交
1091
	status = scic_sds_port_set_phy(port, phy);
1092 1093

	if (status == SCI_SUCCESS) {
M
Maciej Trela 已提交
1094
		scic_sds_port_general_link_up_handler(port, phy, true);
1095

1096
		port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1097 1098

		sci_base_state_machine_change_state(
1099
			&port->ready_substate_machine,
1100 1101 1102 1103 1104 1105 1106 1107 1108
			SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING
			);
	}

	return status;
}


static enum sci_status scic_sds_port_ready_substate_remove_phy_handler(
1109
	struct scic_sds_port *port,
M
Maciej Trela 已提交
1110
	struct scic_sds_phy *phy)
1111 1112 1113
{
	enum sci_status status;

M
Maciej Trela 已提交
1114
	status = scic_sds_port_clear_phy(port, phy);
1115 1116

	if (status == SCI_SUCCESS) {
M
Maciej Trela 已提交
1117
		scic_sds_port_deactivate_phy(port, phy, true);
1118

1119
		port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1120 1121

		sci_base_state_machine_change_state(
1122
			&port->ready_substate_machine,
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
			SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING
			);
	}

	return status;
}

/*
 * ****************************************************************************
 * *  READY SUBSTATE WAITING HANDLERS
 * **************************************************************************** */

/**
 *
1137
 * @sci_port: This is the struct scic_sds_port object that which has a phy that has
1138
 *    gone link up.
1139
 * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
1140 1141 1142 1143 1144 1145
 *
 * This method is the ready waiting substate link up handler for the
 * struct scic_sds_port object.  This methos will report the link up condition for
 * this port and will transition to the ready operational substate. none
 */
static void scic_sds_port_ready_waiting_substate_link_up_handler(
1146 1147
	struct scic_sds_port *sci_port,
	struct scic_sds_phy *sci_phy)
1148 1149 1150 1151
{
	/*
	 * Since this is the first phy going link up for the port we can just enable
	 * it and continue. */
1152
	scic_sds_port_activate_phy(sci_port, sci_phy, true);
1153 1154

	sci_base_state_machine_change_state(
1155
		&sci_port->ready_substate_machine,
1156 1157 1158 1159
		SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL
		);
}

1160
/*
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
 * This method is the ready waiting substate start io handler for the
 * struct scic_sds_port object. The port object can not accept new requests so the
 * request is failed. enum sci_status SCI_FAILURE_INVALID_STATE
 */
static enum sci_status scic_sds_port_ready_waiting_substate_start_io_handler(
	struct scic_sds_port *port,
	struct scic_sds_remote_device *device,
	struct scic_sds_request *io_request)
{
	return SCI_FAILURE_INVALID_STATE;
}

/*
 * ****************************************************************************
 * *  READY SUBSTATE OPERATIONAL HANDLERS
 * **************************************************************************** */

1178
/*
1179 1180
 * This method will casue the port to reset. enum sci_status SCI_SUCCESS
 */
1181 1182
static enum
sci_status scic_sds_port_ready_operational_substate_reset_handler(
1183
		struct scic_sds_port *port,
1184
		u32 timeout)
1185 1186 1187
{
	enum sci_status status = SCI_FAILURE_INVALID_PHY;
	u32 phy_index;
1188
	struct scic_sds_phy *selected_phy = NULL;
1189 1190 1191


	/* Select a phy on which we can send the hard reset request. */
1192 1193 1194
	for (phy_index = 0;
	     (phy_index < SCI_MAX_PHYS) && (selected_phy == NULL);
	     phy_index++) {
1195
		selected_phy = port->phy_table[phy_index];
1196 1197

		if ((selected_phy != NULL) &&
1198
		    !scic_sds_port_active_phy(port, selected_phy)) {
1199 1200 1201 1202
			/*
			 * We found a phy but it is not ready select
			 * different phy
			 */
1203
			selected_phy = NULL;
1204 1205 1206 1207
		}
	}

	/* If we have a phy then go ahead and start the reset procedure */
1208
	if (selected_phy != NULL) {
1209 1210 1211
		status = scic_sds_phy_reset(selected_phy);

		if (status == SCI_SUCCESS) {
1212 1213
			isci_timer_start(port->timer_handle, timeout);
			port->not_ready_reason =
1214
				SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
1215 1216

			sci_base_state_machine_change_state(
1217
					&port->state_machine,
1218
					SCI_BASE_PORT_STATE_RESETTING);
1219 1220 1221 1222 1223 1224 1225 1226
		}
	}

	return status;
}

/**
 * scic_sds_port_ready_operational_substate_link_up_handler() -
1227
 * @sci_port: This is the struct scic_sds_port object that which has a phy that has
1228
 *    gone link up.
1229
 * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
1230 1231 1232 1233 1234 1235
 *
 * This method is the ready operational substate link up handler for the
 * struct scic_sds_port object. This function notifies the SCI User that the phy has
 * gone link up. none
 */
static void scic_sds_port_ready_operational_substate_link_up_handler(
1236 1237
	struct scic_sds_port *sci_port,
	struct scic_sds_phy *sci_phy)
1238
{
1239
	scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1240 1241 1242 1243
}

/**
 * scic_sds_port_ready_operational_substate_link_down_handler() -
D
Dan Williams 已提交
1244
 * @sci_port: This is the struct scic_sds_port object that which has a phy that has
1245
 *    gone link down.
D
Dan Williams 已提交
1246
 * @sci_phy: This is the struct scic_sds_phy object that has gone link down.
1247 1248 1249 1250 1251 1252 1253
 *
 * This method is the ready operational substate link down handler for the
 * struct scic_sds_port object. This function notifies the SCI User that the phy has
 * gone link down and if this is the last phy in the port the port will change
 * state to the ready waiting substate. none
 */
static void scic_sds_port_ready_operational_substate_link_down_handler(
D
Dan Williams 已提交
1254 1255
	struct scic_sds_port *sci_port,
	struct scic_sds_phy *sci_phy)
1256
{
D
Dan Williams 已提交
1257
	scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
1258 1259 1260 1261 1262

	/*
	 * If there are no active phys left in the port, then transition
	 * the port to the WAITING state until such time as a phy goes
	 * link up. */
D
Dan Williams 已提交
1263 1264 1265
	if (sci_port->active_phy_mask == 0)
		sci_base_state_machine_change_state(&sci_port->ready_substate_machine,
						    SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
1266 1267
}

1268
/*
1269 1270 1271 1272 1273 1274 1275 1276 1277
 * This method is the ready operational substate start io handler for the
 * struct scic_sds_port object.  This function incremetns the outstanding request
 * count for this port object. enum sci_status SCI_SUCCESS
 */
static enum sci_status scic_sds_port_ready_operational_substate_start_io_handler(
	struct scic_sds_port *port,
	struct scic_sds_remote_device *device,
	struct scic_sds_request *io_request)
{
1278
	scic_sds_port_increment_request_count(port);
1279 1280 1281 1282 1283 1284 1285 1286 1287

	return SCI_SUCCESS;
}

/*
 * ****************************************************************************
 * *  READY SUBSTATE OPERATIONAL HANDLERS
 * **************************************************************************** */

1288
/*
1289 1290 1291 1292
 * This is the default method for a port add phy request.  It will report a
 * warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
 */
static enum sci_status scic_sds_port_ready_configuring_substate_add_phy_handler(
1293
	struct scic_sds_port *port,
M
Maciej Trela 已提交
1294
	struct scic_sds_phy *phy)
1295 1296 1297
{
	enum sci_status status;

M
Maciej Trela 已提交
1298
	status = scic_sds_port_set_phy(port, phy);
1299 1300

	if (status == SCI_SUCCESS) {
M
Maciej Trela 已提交
1301
		scic_sds_port_general_link_up_handler(port, phy, true);
1302 1303 1304 1305 1306

		/*
		 * Re-enter the configuring state since this may be the last phy in
		 * the port. */
		sci_base_state_machine_change_state(
1307
			&port->ready_substate_machine,
1308 1309 1310 1311 1312 1313 1314
			SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING
			);
	}

	return status;
}

1315
/*
1316 1317 1318 1319
 * This is the default method for a port remove phy request.  It will report a
 * warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
 */
static enum sci_status scic_sds_port_ready_configuring_substate_remove_phy_handler(
1320
	struct scic_sds_port *port,
M
Maciej Trela 已提交
1321
	struct scic_sds_phy *phy)
1322 1323 1324
{
	enum sci_status status;

M
Maciej Trela 已提交
1325
	status = scic_sds_port_clear_phy(port, phy);
1326 1327

	if (status == SCI_SUCCESS) {
M
Maciej Trela 已提交
1328
		scic_sds_port_deactivate_phy(port, phy, true);
1329 1330 1331 1332 1333

		/*
		 * Re-enter the configuring state since this may be the last phy in
		 * the port. */
		sci_base_state_machine_change_state(
1334
			&port->ready_substate_machine,
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
			SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING
			);
	}

	return status;
}

/**
 * scic_sds_port_ready_configuring_substate_complete_io_handler() -
 * @port: This is the port that is being requested to complete the io request.
 * @device: This is the device on which the io is completing.
 *
 * This method will decrement the outstanding request count for this port. If
 * the request count goes to 0 then the port can be reprogrammed with its new
 * phy data.
 */
1351 1352
static enum sci_status
scic_sds_port_ready_configuring_substate_complete_io_handler(
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
	struct scic_sds_port *port,
	struct scic_sds_remote_device *device,
	struct scic_sds_request *io_request)
{
	scic_sds_port_decrement_request_count(port);

	if (port->started_request_count == 0) {
		sci_base_state_machine_change_state(
			&port->ready_substate_machine,
			SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL
			);
	}

	return SCI_SUCCESS;
}

1369 1370
static enum sci_status default_port_handler(struct scic_sds_port *sci_port,
					    const char *func)
D
Dan Williams 已提交
1371 1372 1373
{
	dev_warn(sciport_to_dev(sci_port),
		 "%s: in wrong state: %d\n", func,
1374
		 sci_base_state_machine_get_state(&sci_port->state_machine));
D
Dan Williams 已提交
1375 1376 1377
	return SCI_FAILURE_INVALID_STATE;
}

1378 1379
static enum sci_status
scic_sds_port_default_start_handler(struct scic_sds_port *sci_port)
D
Dan Williams 已提交
1380
{
1381
	return default_port_handler(sci_port, __func__);
D
Dan Williams 已提交
1382 1383
}

1384 1385
static enum sci_status
scic_sds_port_default_stop_handler(struct scic_sds_port *sci_port)
D
Dan Williams 已提交
1386
{
1387
	return default_port_handler(sci_port, __func__);
D
Dan Williams 已提交
1388 1389
}

1390 1391
static enum sci_status
scic_sds_port_default_destruct_handler(struct scic_sds_port *sci_port)
D
Dan Williams 已提交
1392
{
1393
	return default_port_handler(sci_port, __func__);
D
Dan Williams 已提交
1394 1395
}

1396 1397 1398
static enum sci_status
scic_sds_port_default_reset_handler(struct scic_sds_port *sci_port,
				    u32 timeout)
D
Dan Williams 已提交
1399
{
1400
	return default_port_handler(sci_port, __func__);
D
Dan Williams 已提交
1401 1402
}

1403 1404
static enum sci_status
scic_sds_port_default_add_phy_handler(struct scic_sds_port *sci_port,
M
Maciej Trela 已提交
1405
				      struct scic_sds_phy *base_phy)
D
Dan Williams 已提交
1406
{
1407
	return default_port_handler(sci_port, __func__);
D
Dan Williams 已提交
1408 1409
}

1410 1411
static enum sci_status
scic_sds_port_default_remove_phy_handler(struct scic_sds_port *sci_port,
M
Maciej Trela 已提交
1412
					 struct scic_sds_phy *base_phy)
D
Dan Williams 已提交
1413
{
1414
	return default_port_handler(sci_port, __func__);
D
Dan Williams 已提交
1415 1416
}

1417
/*
D
Dan Williams 已提交
1418 1419 1420 1421 1422
 * This is the default method for a port unsolicited frame request.  It will
 * report a warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE Is it even
 * possible to receive an unsolicited frame directed to a port object?  It
 * seems possible if we implementing virtual functions but until then?
 */
1423 1424 1425
static enum sci_status
scic_sds_port_default_frame_handler(struct scic_sds_port *sci_port,
				    u32 frame_index)
D
Dan Williams 已提交
1426 1427 1428
{
	struct scic_sds_controller *scic = scic_sds_port_get_controller(sci_port);

1429
	default_port_handler(sci_port, __func__);
D
Dan Williams 已提交
1430 1431 1432 1433 1434 1435 1436 1437
	scic_sds_controller_release_frame(scic, frame_index);

	return SCI_FAILURE_INVALID_STATE;
}

static enum sci_status scic_sds_port_default_event_handler(struct scic_sds_port *sci_port,
						    u32 event_code)
{
1438
	return default_port_handler(sci_port, __func__);
D
Dan Williams 已提交
1439 1440 1441 1442 1443
}

static void scic_sds_port_default_link_up_handler(struct scic_sds_port *sci_port,
					   struct scic_sds_phy *sci_phy)
{
1444
	default_port_handler(sci_port, __func__);
D
Dan Williams 已提交
1445 1446 1447 1448 1449
}

static void scic_sds_port_default_link_down_handler(struct scic_sds_port *sci_port,
					     struct scic_sds_phy *sci_phy)
{
1450
	default_port_handler(sci_port, __func__);
D
Dan Williams 已提交
1451
}
1452

D
Dan Williams 已提交
1453 1454 1455 1456
static enum sci_status scic_sds_port_default_start_io_handler(struct scic_sds_port *sci_port,
						       struct scic_sds_remote_device *sci_dev,
						       struct scic_sds_request *sci_req)
{
1457
	return default_port_handler(sci_port, __func__);
D
Dan Williams 已提交
1458 1459 1460 1461 1462 1463
}

static enum sci_status scic_sds_port_default_complete_io_handler(struct scic_sds_port *sci_port,
								 struct scic_sds_remote_device *sci_dev,
								 struct scic_sds_request *sci_req)
{
1464
	return default_port_handler(sci_port, __func__);
D
Dan Williams 已提交
1465 1466 1467 1468 1469
}



static struct scic_sds_port_state_handler
1470
scic_sds_port_ready_substate_handler_table[SCIC_SDS_PORT_READY_MAX_SUBSTATES] = {
1471
	{
1472 1473 1474 1475 1476 1477 1478
		/* SCIC_SDS_PORT_READY_SUBSTATE_WAITING */
		scic_sds_port_default_start_handler,
		scic_sds_port_ready_substate_stop_handler,
		scic_sds_port_default_destruct_handler,
		scic_sds_port_default_reset_handler,
		scic_sds_port_ready_substate_add_phy_handler,
		scic_sds_port_default_remove_phy_handler,
1479 1480 1481 1482 1483 1484 1485
		scic_sds_port_default_frame_handler,
		scic_sds_port_default_event_handler,
		scic_sds_port_ready_waiting_substate_link_up_handler,
		scic_sds_port_default_link_down_handler,
		scic_sds_port_ready_waiting_substate_start_io_handler,
		scic_sds_port_ready_substate_complete_io_handler,
	},
1486

1487
	{
1488 1489 1490 1491 1492 1493 1494
		/* SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL */
		scic_sds_port_default_start_handler,
		scic_sds_port_ready_substate_stop_handler,
		scic_sds_port_default_destruct_handler,
		scic_sds_port_ready_operational_substate_reset_handler,
		scic_sds_port_ready_substate_add_phy_handler,
		scic_sds_port_ready_substate_remove_phy_handler,
1495 1496 1497 1498 1499
		scic_sds_port_default_frame_handler,
		scic_sds_port_default_event_handler,
		scic_sds_port_ready_operational_substate_link_up_handler,
		scic_sds_port_ready_operational_substate_link_down_handler,
		scic_sds_port_ready_operational_substate_start_io_handler,
1500
		scic_sds_port_ready_substate_complete_io_handler,
1501
	},
1502

1503
	{
1504 1505 1506 1507 1508 1509 1510
		/* SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING */
		scic_sds_port_default_start_handler,
		scic_sds_port_ready_substate_stop_handler,
		scic_sds_port_default_destruct_handler,
		scic_sds_port_default_reset_handler,
		scic_sds_port_ready_configuring_substate_add_phy_handler,
		scic_sds_port_ready_configuring_substate_remove_phy_handler,
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
		scic_sds_port_default_frame_handler,
		scic_sds_port_default_event_handler,
		scic_sds_port_default_link_up_handler,
		scic_sds_port_default_link_down_handler,
		scic_sds_port_default_start_io_handler,
		scic_sds_port_ready_configuring_substate_complete_io_handler
	}
};

/**
 * scic_sds_port_set_ready_state_handlers() -
 *
 * This macro sets the port ready substate handlers.
 */
#define scic_sds_port_set_ready_state_handlers(port, state_id) \
	scic_sds_port_set_state_handlers(\
		port, &scic_sds_port_ready_substate_handler_table[(state_id)] \
		)

/*
 * ******************************************************************************
 * *  PORT STATE PRIVATE METHODS
 * ****************************************************************************** */

/**
 *
1537
 * @sci_port: This is the struct scic_sds_port object to suspend.
1538 1539 1540
 *
 * This method will susped the port task scheduler for this port object. none
 */
C
Christoph Hellwig 已提交
1541 1542
static void
scic_sds_port_suspend_port_task_scheduler(struct scic_sds_port *port)
1543 1544 1545
{
	u32 pts_control_value;

C
Christoph Hellwig 已提交
1546
	pts_control_value = readl(&port->port_task_scheduler_registers->control);
1547
	pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
C
Christoph Hellwig 已提交
1548
	writel(pts_control_value, &port->port_task_scheduler_registers->control);
1549 1550
}

1551 1552 1553 1554 1555 1556 1557 1558 1559
/**
 * scic_sds_port_post_dummy_request() - post dummy/workaround request
 * @sci_port: port to post task
 *
 * Prevent the hardware scheduler from posting new requests to the front
 * of the scheduler queue causing a starvation problem for currently
 * ongoing requests.
 *
 */
D
Dan Williams 已提交
1560
static void scic_sds_port_post_dummy_request(struct scic_sds_port *sci_port)
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
{
	u32 command;
	struct scu_task_context *task_context;
	struct scic_sds_controller *scic = sci_port->owning_controller;
	u16 tci = sci_port->reserved_tci;

	task_context = scic_sds_controller_get_task_context_buffer(scic, tci);

	task_context->abort = 0;

	command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
		  sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
		  tci;

	scic_sds_controller_post_request(scic, command);
}

/**
 * This routine will abort the dummy request.  This will alow the hardware to
 * power down parts of the silicon to save power.
 *
 * @sci_port: The port on which the task must be aborted.
 *
 */
D
Dan Williams 已提交
1585
static void scic_sds_port_abort_dummy_request(struct scic_sds_port *sci_port)
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
{
	struct scic_sds_controller *scic = sci_port->owning_controller;
	u16 tci = sci_port->reserved_tci;
	struct scu_task_context *tc;
	u32 command;

	tc = scic_sds_controller_get_task_context_buffer(scic, tci);

	tc->abort = 1;

	command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
		  sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
		  tci;

	scic_sds_controller_post_request(scic, command);
}

1603 1604
/**
 *
1605
 * @sci_port: This is the struct scic_sds_port object to resume.
1606 1607 1608
 *
 * This method will resume the port task scheduler for this port object. none
 */
C
Christoph Hellwig 已提交
1609 1610
static void
scic_sds_port_resume_port_task_scheduler(struct scic_sds_port *port)
1611 1612 1613
{
	u32 pts_control_value;

C
Christoph Hellwig 已提交
1614
	pts_control_value = readl(&port->port_task_scheduler_registers->control);
1615
	pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
C
Christoph Hellwig 已提交
1616
	writel(pts_control_value, &port->port_task_scheduler_registers->control);
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
}

/*
 * ******************************************************************************
 * *  PORT READY SUBSTATE METHODS
 * ****************************************************************************** */

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_port object.
 *
 * This method will perform the actions required by the struct scic_sds_port on
 * entering the SCIC_SDS_PORT_READY_SUBSTATE_WAITING. This function checks the
 * port for any ready phys.  If there is at least one phy in a ready state then
 * the port transitions to the ready operational substate. none
 */
static void scic_sds_port_ready_substate_waiting_enter(
	struct sci_base_object *object)
{
1636
	struct scic_sds_port *sci_port = (struct scic_sds_port *)object;
1637 1638

	scic_sds_port_set_ready_state_handlers(
1639
		sci_port, SCIC_SDS_PORT_READY_SUBSTATE_WAITING
1640 1641
		);

1642
	scic_sds_port_suspend_port_task_scheduler(sci_port);
1643

1644
	sci_port->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
1645

1646
	if (sci_port->active_phy_mask != 0) {
1647 1648
		/* At least one of the phys on the port is ready */
		sci_base_state_machine_change_state(
1649
			&sci_port->ready_substate_machine,
1650 1651 1652 1653 1654 1655 1656
			SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL
			);
	}
}

/**
 *
1657 1658
 * @object: This is the struct sci_base_object which is cast to a
 * struct scic_sds_port object.
1659
 *
1660 1661
 * This function will perform the actions required by the struct scic_sds_port
 * on entering the SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL. This function sets
1662 1663 1664 1665 1666 1667 1668
 * the state handlers for the port object, notifies the SCI User that the port
 * is ready, and resumes port operations. none
 */
static void scic_sds_port_ready_substate_operational_enter(
	struct sci_base_object *object)
{
	u32 index;
1669 1670 1671 1672 1673
	struct scic_sds_port *sci_port = (struct scic_sds_port *)object;
	struct scic_sds_controller *scic =
		scic_sds_port_get_controller(sci_port);
	struct isci_host *ihost = sci_object_get_association(scic);
	struct isci_port *iport = sci_object_get_association(sci_port);
1674 1675

	scic_sds_port_set_ready_state_handlers(
1676 1677
			sci_port,
			SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1678

1679
	isci_port_ready(ihost, iport);
1680 1681

	for (index = 0; index < SCI_MAX_PHYS; index++) {
C
Christoph Hellwig 已提交
1682 1683 1684 1685 1686
		if (sci_port->phy_table[index]) {
			writel(sci_port->physical_port_index,
				&sci_port->port_pe_configuration_register[
					sci_port->phy_table[index]->phy_index]);
		}
1687 1688
	}

1689
	scic_sds_port_update_viit_entry(sci_port);
1690

1691
	scic_sds_port_resume_port_task_scheduler(sci_port);
1692

1693 1694
	/*
	 * Post the dummy task for the port so the hardware can schedule
1695 1696
	 * io correctly
	 */
1697
	scic_sds_port_post_dummy_request(sci_port);
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_port object.
 *
 * This method will perform the actions required by the struct scic_sds_port on
 * exiting the SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL. This function reports
 * the port not ready and suspends the port task scheduler. none
 */
static void scic_sds_port_ready_substate_operational_exit(
	struct sci_base_object *object)
{
1711 1712 1713 1714 1715
	struct scic_sds_port *sci_port = (struct scic_sds_port *)object;
	struct scic_sds_controller *scic =
		scic_sds_port_get_controller(sci_port);
	struct isci_host *ihost = sci_object_get_association(scic);
	struct isci_port *iport = sci_object_get_association(sci_port);
1716

1717 1718 1719 1720 1721 1722
	/*
	 * Kill the dummy task for this port if it has not yet posted
	 * the hardware will treat this as a NOP and just return abort
	 * complete.
	 */
	scic_sds_port_abort_dummy_request(sci_port);
D
Dan Williams 已提交
1723

1724
	isci_port_not_ready(ihost, iport);
1725 1726 1727 1728 1729 1730 1731 1732 1733
}

/*
 * ******************************************************************************
 * *  PORT READY CONFIGURING METHODS
 * ****************************************************************************** */

/**
 * scic_sds_port_ready_substate_configuring_enter() -
1734 1735
 * @object: This is the struct sci_base_object which is cast to a
 * struct scic_sds_port object.
1736 1737 1738 1739 1740 1741 1742 1743
 *
 * This method will perform the actions required by the struct scic_sds_port on
 * exiting the SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL. This function reports
 * the port not ready and suspends the port task scheduler. none
 */
static void scic_sds_port_ready_substate_configuring_enter(
	struct sci_base_object *object)
{
1744 1745 1746 1747 1748
	struct scic_sds_port *sci_port = (struct scic_sds_port *)object;
	struct scic_sds_controller *scic =
		scic_sds_port_get_controller(sci_port);
	struct isci_host *ihost = sci_object_get_association(scic);
	struct isci_port *iport = sci_object_get_association(sci_port);
1749 1750

	scic_sds_port_set_ready_state_handlers(
1751 1752
			sci_port,
			SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING);
1753

1754 1755
	if (sci_port->active_phy_mask == 0) {
		isci_port_not_ready(ihost, iport);
1756 1757

		sci_base_state_machine_change_state(
1758 1759 1760
				&sci_port->ready_substate_machine,
				SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
	} else if (sci_port->started_request_count == 0)
1761
		sci_base_state_machine_change_state(
1762 1763
				&sci_port->ready_substate_machine,
				SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1764 1765 1766 1767 1768
}

static void scic_sds_port_ready_substate_configuring_exit(
	struct sci_base_object *object)
{
1769
	struct scic_sds_port *sci_port = (struct scic_sds_port *)object;
1770

1771
	scic_sds_port_suspend_port_task_scheduler(sci_port);
1772 1773 1774 1775
}

/* --------------------------------------------------------------------------- */

D
Dan Williams 已提交
1776
static const struct sci_base_state scic_sds_port_ready_substate_table[] = {
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
	[SCIC_SDS_PORT_READY_SUBSTATE_WAITING] = {
		.enter_state = scic_sds_port_ready_substate_waiting_enter,
	},
	[SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL] = {
		.enter_state = scic_sds_port_ready_substate_operational_enter,
		.exit_state  = scic_sds_port_ready_substate_operational_exit
	},
	[SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING] = {
		.enter_state = scic_sds_port_ready_substate_configuring_enter,
		.exit_state  = scic_sds_port_ready_substate_configuring_exit
	},
};

/**
 *
 * @port: This is the struct scic_sds_port object on which the io request count will
 *    be decremented.
 * @device: This is the struct scic_sds_remote_device object to which the io request
 *    is being directed.  This parameter is not required to complete this
 *    operation.
 * @io_request: This is the request that is being completed on this port
 *    object.  This parameter is not required to complete this operation.
 *
 * This is a general complete io request handler for the struct scic_sds_port object.
 * enum sci_status SCI_SUCCESS
 */
static enum sci_status scic_sds_port_general_complete_io_handler(
	struct scic_sds_port *port,
	struct scic_sds_remote_device *device,
	struct scic_sds_request *io_request)
{
1808
	scic_sds_port_decrement_request_count(port);
1809 1810 1811 1812 1813

	return SCI_SUCCESS;
}

/**
1814
 * scic_sds_port_stopped_state_start_handler() - stop a port from "started"
1815
 *
1816
 * @port: This is the struct scic_sds_port object which is cast into a
1817
 * struct scic_sds_port object.
1818
 *
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
 * This function takes the struct scic_sds_port from a stopped state and
 * attempts to start it.  To start a port it must have no assiged devices and
 * it must have at least one phy assigned to it.  If those conditions are
 * met then the port can transition to the ready state.
 * enum sci_status
 * SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION
 * This struct scic_sds_port object could not be started because the port
 * configuration is not valid.
 * SCI_SUCCESS
 * the start request is successful and the struct scic_sds_port object
 * has transitioned to the SCI_BASE_PORT_STATE_READY.
1830
 */
1831
static enum sci_status
1832
scic_sds_port_stopped_state_start_handler(struct scic_sds_port *sci_port)
1833
{
1834
	struct scic_sds_controller *scic = sci_port->owning_controller;
1835
	struct isci_host *ihost = sci_object_get_association(scic);
1836
	enum sci_status status = SCI_SUCCESS;
1837 1838
	u32 phy_mask;

1839
	if (sci_port->assigned_device_count > 0) {
1840
		/*
1841 1842 1843 1844 1845
		 * @todo This is a start failure operation because
		 * there are still devices assigned to this port.
		 * There must be no devices assigned to a port on a
		 * start operation.
		 */
1846 1847 1848
		return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
	}

1849 1850 1851 1852
	sci_port->timer_handle =
		isci_timer_create(ihost,
				  sci_port,
				  scic_sds_port_timeout_handler);
1853

1854 1855
	if (!sci_port->timer_handle)
		return SCI_FAILURE_INSUFFICIENT_RESOURCES;
1856

1857
	if (sci_port->reserved_rni == SCU_DUMMY_INDEX) {
1858 1859
		u16 rni = scic_sds_remote_node_table_allocate_remote_node(
				&scic->available_remote_nodes, 1);
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

		if (rni != SCU_DUMMY_INDEX)
			scic_sds_port_construct_dummy_rnc(sci_port, rni);
		else
			status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
		sci_port->reserved_rni = rni;
	}

	if (sci_port->reserved_tci == SCU_DUMMY_INDEX) {
		/* Allocate a TCI and remove the sequence nibble */
		u16 tci = scic_controller_allocate_io_tag(scic);

		if (tci != SCU_DUMMY_INDEX)
			scic_sds_port_construct_dummy_task(sci_port, tci);
		else
			status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
		sci_port->reserved_tci = tci;
	}

	if (status == SCI_SUCCESS) {
		phy_mask = scic_sds_port_get_phys(sci_port);

		/*
		 * There are one or more phys assigned to this port.  Make sure
		 * the port's phy mask is in fact legal and supported by the
		 * silicon.
		 */
		if (scic_sds_port_is_phy_mask_valid(sci_port, phy_mask) == true) {
1888 1889 1890
			sci_base_state_machine_change_state(
				&sci_port->state_machine,
				SCI_BASE_PORT_STATE_READY);
1891 1892 1893 1894

			return SCI_SUCCESS;
		} else
			status = SCI_FAILURE;
1895 1896
	}

1897 1898 1899 1900
	if (status != SCI_SUCCESS)
		scic_sds_port_destroy_dummy_resources(sci_port);

	return status;
1901 1902
}

1903
/*
1904 1905 1906 1907 1908
 * This method takes the struct scic_sds_port that is in a stopped state and handles a
 * stop request.  This function takes no action. enum sci_status SCI_SUCCESS the
 * stop request is successful as the struct scic_sds_port object is already stopped.
 */
static enum sci_status scic_sds_port_stopped_state_stop_handler(
1909
	struct scic_sds_port *port)
1910 1911 1912 1913 1914
{
	/* We are already stopped so there is nothing to do here */
	return SCI_SUCCESS;
}

1915
/*
1916 1917 1918 1919 1920 1921
 * This method takes the struct scic_sds_port that is in a stopped state and handles
 * the destruct request.  The stopped state is the only state in which the
 * struct scic_sds_port can be destroyed.  This function causes the port object to
 * transition to the SCI_BASE_PORT_STATE_FINAL. enum sci_status SCI_SUCCESS
 */
static enum sci_status scic_sds_port_stopped_state_destruct_handler(
1922
	struct scic_sds_port *port)
1923
{
1924
	sci_base_state_machine_stop(&port->state_machine);
1925 1926 1927 1928

	return SCI_SUCCESS;
}

1929
/*
1930 1931 1932 1933 1934 1935 1936
 * This method takes the struct scic_sds_port that is in a stopped state and handles
 * the add phy request.  In MPC mode the only time a phy can be added to a port
 * is in the SCI_BASE_PORT_STATE_STOPPED. enum sci_status
 * SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION is returned when the phy can not
 * be added to the port. SCI_SUCCESS if the phy is added to the port.
 */
static enum sci_status scic_sds_port_stopped_state_add_phy_handler(
1937
	struct scic_sds_port *port,
M
Maciej Trela 已提交
1938
	struct scic_sds_phy *phy)
1939 1940 1941 1942
{
	struct sci_sas_address port_sas_address;

	/* Read the port assigned SAS Address if there is one */
1943
	scic_sds_port_get_sas_address(port, &port_sas_address);
1944 1945 1946 1947 1948 1949 1950

	if (port_sas_address.high != 0 && port_sas_address.low != 0) {
		struct sci_sas_address phy_sas_address;

		/*
		 * Make sure that the PHY SAS Address matches the SAS Address
		 * for this port. */
M
Maciej Trela 已提交
1951
		scic_sds_phy_get_sas_address(phy, &phy_sas_address);
1952 1953 1954 1955 1956 1957 1958 1959 1960

		if (
			(port_sas_address.high != phy_sas_address.high)
			|| (port_sas_address.low  != phy_sas_address.low)
			) {
			return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
		}
	}

M
Maciej Trela 已提交
1961
	return scic_sds_port_set_phy(port, phy);
1962 1963
}

1964
/*
1965 1966 1967 1968 1969 1970 1971
 * This method takes the struct scic_sds_port that is in a stopped state and handles
 * the remove phy request.  In MPC mode the only time a phy can be removed from
 * a port is in the SCI_BASE_PORT_STATE_STOPPED. enum sci_status
 * SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION is returned when the phy can not
 * be added to the port. SCI_SUCCESS if the phy is added to the port.
 */
static enum sci_status scic_sds_port_stopped_state_remove_phy_handler(
1972
	struct scic_sds_port *port,
M
Maciej Trela 已提交
1973
	struct scic_sds_phy *phy)
1974
{
M
Maciej Trela 已提交
1975
	return scic_sds_port_clear_phy(port, phy);
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
}

/*
 * ****************************************************************************
 * *  READY STATE HANDLERS
 * **************************************************************************** */

/*
 * ****************************************************************************
 * *  RESETTING STATE HANDLERS
 * **************************************************************************** */

/*
 * ****************************************************************************
 * *  STOPPING STATE HANDLERS
 * **************************************************************************** */

1993
/*
1994 1995 1996 1997 1998
 * This method takes the struct scic_sds_port that is in a stopping state and handles
 * the complete io request. Should the request count reach 0 then the port
 * object will transition to the stopped state. enum sci_status SCI_SUCCESS
 */
static enum sci_status scic_sds_port_stopping_state_complete_io_handler(
D
Dan Williams 已提交
1999
	struct scic_sds_port *sci_port,
2000 2001 2002
	struct scic_sds_remote_device *device,
	struct scic_sds_request *io_request)
{
D
Dan Williams 已提交
2003
	scic_sds_port_decrement_request_count(sci_port);
2004

D
Dan Williams 已提交
2005
	if (sci_port->started_request_count == 0) {
2006
		sci_base_state_machine_change_state(&sci_port->state_machine,
D
Dan Williams 已提交
2007
						    SCI_BASE_PORT_STATE_STOPPED);
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
	}

	return SCI_SUCCESS;
}

/*
 * ****************************************************************************
 * *  RESETTING STATE HANDLERS
 * **************************************************************************** */

/**
 *
 * @port: This is the port object which is being requested to stop.
 *
 * This method will stop a failed port.  This causes a transition to the
 * stopping state. enum sci_status SCI_SUCCESS
 */
static enum sci_status scic_sds_port_reset_state_stop_handler(
2026
	struct scic_sds_port *port)
2027 2028
{
	sci_base_state_machine_change_state(
2029
		&port->state_machine,
2030 2031 2032 2033 2034 2035
		SCI_BASE_PORT_STATE_STOPPING
		);

	return SCI_SUCCESS;
}

2036
/*
2037 2038 2039 2040 2041
 * This method will transition a failed port to its ready state.  The port
 * failed because a hard reset request timed out but at some time later one or
 * more phys in the port became ready. enum sci_status SCI_SUCCESS
 */
static void scic_sds_port_reset_state_link_up_handler(
2042
	struct scic_sds_port *port,
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
	struct scic_sds_phy *phy)
{
	/*
	 * / @todo We should make sure that the phy that has gone link up is the same
	 * /       one on which we sent the reset.  It is possible that the phy on
	 * /       which we sent the reset is not the one that has gone link up and we
	 * /       want to make sure that phy being reset comes back.  Consider the
	 * /       case where a reset is sent but before the hardware processes the
	 * /       reset it get a link up on the port because of a hot plug event.
	 * /       because of the reset request this phy will go link down almost
	 * /       immediately. */

	/*
	 * In the resetting state we don't notify the user regarding
	 * link up and link down notifications. */
2058
	scic_sds_port_general_link_up_handler(port, phy, false);
2059 2060
}

2061
/*
2062 2063 2064 2065 2066
 * This method process link down notifications that occur during a port reset
 * operation. Link downs can occur during the reset operation. enum sci_status
 * SCI_SUCCESS
 */
static void scic_sds_port_reset_state_link_down_handler(
2067
	struct scic_sds_port *port,
2068 2069 2070 2071 2072
	struct scic_sds_phy *phy)
{
	/*
	 * In the resetting state we don't notify the user regarding
	 * link up and link down notifications. */
2073
	scic_sds_port_deactivate_phy(port, phy, false);
2074 2075
}

D
Dan Williams 已提交
2076
static struct scic_sds_port_state_handler
2077 2078 2079 2080
scic_sds_port_state_handler_table[SCI_BASE_PORT_MAX_STATES] =
{
	/* SCI_BASE_PORT_STATE_STOPPED */
	{
2081 2082 2083 2084 2085 2086
		scic_sds_port_stopped_state_start_handler,
		scic_sds_port_stopped_state_stop_handler,
		scic_sds_port_stopped_state_destruct_handler,
		scic_sds_port_default_reset_handler,
		scic_sds_port_stopped_state_add_phy_handler,
		scic_sds_port_stopped_state_remove_phy_handler,
2087 2088 2089 2090 2091 2092 2093 2094 2095
		scic_sds_port_default_frame_handler,
		scic_sds_port_default_event_handler,
		scic_sds_port_default_link_up_handler,
		scic_sds_port_default_link_down_handler,
		scic_sds_port_default_start_io_handler,
		scic_sds_port_default_complete_io_handler
	},
	/* SCI_BASE_PORT_STATE_STOPPING */
	{
2096 2097 2098 2099 2100 2101
		scic_sds_port_default_start_handler,
		scic_sds_port_default_stop_handler,
		scic_sds_port_default_destruct_handler,
		scic_sds_port_default_reset_handler,
		scic_sds_port_default_add_phy_handler,
		scic_sds_port_default_remove_phy_handler,
2102 2103 2104 2105 2106 2107 2108 2109 2110
		scic_sds_port_default_frame_handler,
		scic_sds_port_default_event_handler,
		scic_sds_port_default_link_up_handler,
		scic_sds_port_default_link_down_handler,
		scic_sds_port_default_start_io_handler,
		scic_sds_port_stopping_state_complete_io_handler
	},
	/* SCI_BASE_PORT_STATE_READY */
	{
2111 2112 2113 2114 2115 2116
		scic_sds_port_default_start_handler,
		scic_sds_port_default_stop_handler,
		scic_sds_port_default_destruct_handler,
		scic_sds_port_default_reset_handler,
		scic_sds_port_default_add_phy_handler,
		scic_sds_port_default_remove_phy_handler,
2117 2118 2119 2120 2121 2122 2123 2124 2125
		scic_sds_port_default_frame_handler,
		scic_sds_port_default_event_handler,
		scic_sds_port_default_link_up_handler,
		scic_sds_port_default_link_down_handler,
		scic_sds_port_default_start_io_handler,
		scic_sds_port_general_complete_io_handler
	},
	/* SCI_BASE_PORT_STATE_RESETTING */
	{
2126 2127 2128 2129 2130 2131
		scic_sds_port_default_start_handler,
		scic_sds_port_reset_state_stop_handler,
		scic_sds_port_default_destruct_handler,
		scic_sds_port_default_reset_handler,
		scic_sds_port_default_add_phy_handler,
		scic_sds_port_default_remove_phy_handler,
2132 2133 2134 2135 2136 2137 2138 2139 2140
		scic_sds_port_default_frame_handler,
		scic_sds_port_default_event_handler,
		scic_sds_port_reset_state_link_up_handler,
		scic_sds_port_reset_state_link_down_handler,
		scic_sds_port_default_start_io_handler,
		scic_sds_port_general_complete_io_handler
	},
	/* SCI_BASE_PORT_STATE_FAILED */
	{
2141 2142 2143 2144 2145 2146
		scic_sds_port_default_start_handler,
		scic_sds_port_default_stop_handler,
		scic_sds_port_default_destruct_handler,
		scic_sds_port_default_reset_handler,
		scic_sds_port_default_add_phy_handler,
		scic_sds_port_default_remove_phy_handler,
2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
		scic_sds_port_default_frame_handler,
		scic_sds_port_default_event_handler,
		scic_sds_port_default_link_up_handler,
		scic_sds_port_default_link_down_handler,
		scic_sds_port_default_start_io_handler,
		scic_sds_port_general_complete_io_handler
	}
};

/*
 * ******************************************************************************
 * *  PORT STATE PRIVATE METHODS
 * ****************************************************************************** */

/**
 *
2163
 * @sci_port: This is the port object which to suspend.
2164 2165 2166 2167
 *
 * This method will enable the SCU Port Task Scheduler for this port object but
 * will leave the port task scheduler in a suspended state. none
 */
C
Christoph Hellwig 已提交
2168 2169
static void
scic_sds_port_enable_port_task_scheduler(struct scic_sds_port *port)
2170 2171 2172
{
	u32 pts_control_value;

C
Christoph Hellwig 已提交
2173
	pts_control_value = readl(&port->port_task_scheduler_registers->control);
2174
	pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
C
Christoph Hellwig 已提交
2175
	writel(pts_control_value, &port->port_task_scheduler_registers->control);
2176 2177 2178 2179
}

/**
 *
2180
 * @sci_port: This is the port object which to resume.
2181 2182 2183 2184
 *
 * This method will disable the SCU port task scheduler for this port object.
 * none
 */
C
Christoph Hellwig 已提交
2185 2186
static void
scic_sds_port_disable_port_task_scheduler(struct scic_sds_port *port)
2187 2188 2189
{
	u32 pts_control_value;

C
Christoph Hellwig 已提交
2190 2191 2192 2193
	pts_control_value = readl(&port->port_task_scheduler_registers->control);
	pts_control_value &=
		~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
	writel(pts_control_value, &port->port_task_scheduler_registers->control);
2194 2195
}

D
Dan Williams 已提交
2196
static void scic_sds_port_post_dummy_remote_node(struct scic_sds_port *sci_port)
2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
{
	struct scic_sds_controller *scic = sci_port->owning_controller;
	u8 phys_index = sci_port->physical_port_index;
	union scu_remote_node_context *rnc;
	u16 rni = sci_port->reserved_rni;
	u32 command;

	rnc = &scic->remote_node_context_table[rni];
	rnc->ssp.is_valid = true;

	command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
		  phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;

	scic_sds_controller_post_request(scic, command);

	/* ensure hardware has seen the post rnc command and give it
	 * ample time to act before sending the suspend
	 */
C
Christoph Hellwig 已提交
2215
	readl(&scic->smu_registers->interrupt_status); /* flush */
2216 2217 2218 2219 2220 2221 2222 2223
	udelay(10);

	command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
		  phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;

	scic_sds_controller_post_request(scic, command);
}

D
Dan Williams 已提交
2224
static void scic_sds_port_invalidate_dummy_remote_node(struct scic_sds_port *sci_port)
2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235
{
	struct scic_sds_controller *scic = sci_port->owning_controller;
	u8 phys_index = sci_port->physical_port_index;
	union scu_remote_node_context *rnc;
	u16 rni = sci_port->reserved_rni;
	u32 command;

	rnc = &scic->remote_node_context_table[rni];

	rnc->ssp.is_valid = false;

D
Dan Williams 已提交
2236 2237 2238 2239
	/* ensure the preceding tc abort request has reached the
	 * controller and give it ample time to act before posting the rnc
	 * invalidate
	 */
C
Christoph Hellwig 已提交
2240
	readl(&scic->smu_registers->interrupt_status); /* flush */
D
Dan Williams 已提交
2241 2242
	udelay(10);

2243 2244 2245 2246 2247 2248
	command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
		  phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;

	scic_sds_controller_post_request(scic, command);
}

2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265
/*
 * ******************************************************************************
 * *  PORT STATE METHODS
 * ****************************************************************************** */

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_port object.
 *
 * This method will perform the actions required by the struct scic_sds_port on
 * entering the SCI_BASE_PORT_STATE_STOPPED. This function sets the stopped
 * state handlers for the struct scic_sds_port object and disables the port task
 * scheduler in the hardware. none
 */
static void scic_sds_port_stopped_state_enter(
	struct sci_base_object *object)
{
2266
	struct scic_sds_port *sci_port;
2267

2268
	sci_port = (struct scic_sds_port *)object;
2269 2270

	scic_sds_port_set_base_state_handlers(
2271
		sci_port, SCI_BASE_PORT_STATE_STOPPED
2272 2273 2274 2275
		);

	if (
		SCI_BASE_PORT_STATE_STOPPING
2276
		== sci_port->state_machine.previous_state_id
2277 2278 2279 2280 2281
		) {
		/*
		 * If we enter this state becasuse of a request to stop
		 * the port then we want to disable the hardwares port
		 * task scheduler. */
2282
		scic_sds_port_disable_port_task_scheduler(sci_port);
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
	}
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_port object.
 *
 * This method will perform the actions required by the struct scic_sds_port on
 * exiting the SCI_BASE_STATE_STOPPED. This function enables the SCU hardware
 * port task scheduler. none
 */
static void scic_sds_port_stopped_state_exit(
	struct sci_base_object *object)
{
2297
	struct scic_sds_port *sci_port;
2298

2299
	sci_port = (struct scic_sds_port *)object;
2300 2301

	/* Enable and suspend the port task scheduler */
2302
	scic_sds_port_enable_port_task_scheduler(sci_port);
2303 2304 2305
}

/**
D
Dan Williams 已提交
2306 2307
 * scic_sds_port_ready_state_enter -
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_port object.
2308 2309 2310
 *
 * This method will perform the actions required by the struct scic_sds_port on
 * entering the SCI_BASE_PORT_STATE_READY. This function sets the ready state
2311 2312
 * handlers for the struct scic_sds_port object, reports the port object as
 * not ready and starts the ready substate machine. none
2313
 */
2314
static void scic_sds_port_ready_state_enter(struct sci_base_object *object)
2315
{
D
Dan Williams 已提交
2316 2317 2318 2319 2320
	struct scic_sds_controller *scic;
	struct scic_sds_port *sci_port;
	struct isci_port *iport;
	struct isci_host *ihost;
	u32 prev_state;
2321

2322
	sci_port = container_of(object, typeof(*sci_port), parent);
D
Dan Williams 已提交
2323 2324 2325
	scic = scic_sds_port_get_controller(sci_port);
	ihost = sci_object_get_association(scic);
	iport = sci_object_get_association(sci_port);
2326

D
Dan Williams 已提交
2327 2328 2329
	/* Put the ready state handlers in place though they will not be there long */
	scic_sds_port_set_base_state_handlers(sci_port, SCI_BASE_PORT_STATE_READY);

2330
	prev_state = sci_port->state_machine.previous_state_id;
D
Dan Williams 已提交
2331
	if (prev_state  == SCI_BASE_PORT_STATE_RESETTING)
2332
		isci_port_hard_reset_complete(iport, SCI_SUCCESS);
D
Dan Williams 已提交
2333
	else
2334
		isci_port_not_ready(ihost, iport);
2335

2336
	/* Post and suspend the dummy remote node context for this port. */
2337
	scic_sds_port_post_dummy_remote_node(sci_port);
2338

2339
	/* Start the ready substate machine */
D
Dan Williams 已提交
2340
	sci_base_state_machine_start(&sci_port->ready_substate_machine);
2341 2342
}

D
Dan Williams 已提交
2343
static void scic_sds_port_ready_state_exit(struct sci_base_object *object)
2344
{
D
Dan Williams 已提交
2345
	struct scic_sds_port *sci_port;
2346

2347
	sci_port = container_of(object, typeof(*sci_port), parent);
D
Dan Williams 已提交
2348 2349
	sci_base_state_machine_stop(&sci_port->ready_substate_machine);
	scic_sds_port_invalidate_dummy_remote_node(sci_port);
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_port object.
 *
 * This method will perform the actions required by the struct scic_sds_port on
 * entering the SCI_BASE_PORT_STATE_RESETTING. This function sets the resetting
 * state handlers for the struct scic_sds_port object. none
 */
static void scic_sds_port_resetting_state_enter(
	struct sci_base_object *object)
{
2363
	struct scic_sds_port *sci_port;
2364

2365
	sci_port = (struct scic_sds_port *)object;
2366 2367

	scic_sds_port_set_base_state_handlers(
2368
		sci_port, SCI_BASE_PORT_STATE_RESETTING
2369 2370 2371 2372 2373
		);
}

/**
 *
2374 2375
 * @object: This is the struct sci_base_object which is cast to a
 * struct scic_sds_port object.
2376
 *
2377 2378
 * This function will perform the actions required by the
 * struct scic_sds_port on
2379 2380
 * exiting the SCI_BASE_STATE_RESETTING. This function does nothing. none
 */
2381
static inline void scic_sds_port_resetting_state_exit(
2382 2383
	struct sci_base_object *object)
{
2384
	struct scic_sds_port *sci_port = (struct scic_sds_port *)object;
2385

2386
	isci_timer_stop(sci_port->timer_handle);
2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_port object.
 *
 * This method will perform the actions required by the struct scic_sds_port on
 * entering the SCI_BASE_PORT_STATE_STOPPING. This function sets the stopping
 * state handlers for the struct scic_sds_port object. none
 */
static void scic_sds_port_stopping_state_enter(
	struct sci_base_object *object)
{
2400
	struct scic_sds_port *sci_port;
2401

2402
	sci_port = (struct scic_sds_port *)object;
2403 2404

	scic_sds_port_set_base_state_handlers(
2405
		sci_port, SCI_BASE_PORT_STATE_STOPPING
2406 2407 2408 2409 2410
		);
}

/**
 *
2411 2412
 * @object: This is the struct sci_base_object which is cast to a
 * struct scic_sds_port object.
2413
 *
2414 2415
 * This function will perform the actions required by the
 * struct scic_sds_port on
2416 2417
 * exiting the SCI_BASE_STATE_STOPPING. This function does nothing. none
 */
2418 2419
static inline void
scic_sds_port_stopping_state_exit(struct sci_base_object *object)
2420
{
2421
	struct scic_sds_port *sci_port = (struct scic_sds_port *)object;
2422

2423
	isci_timer_stop(sci_port->timer_handle);
2424

2425
	scic_sds_port_destroy_dummy_resources(sci_port);
2426 2427 2428 2429
}

/**
 *
2430 2431
 * @object: This is the struct sci_base_object which is cast to a
 * struct scic_sds_port object.
2432
 *
2433 2434
 * This function will perform the actions required by the
 * struct scic_sds_port on
2435 2436 2437
 * entering the SCI_BASE_PORT_STATE_STOPPING. This function sets the stopping
 * state handlers for the struct scic_sds_port object. none
 */
2438
static void scic_sds_port_failed_state_enter(struct sci_base_object *object)
2439
{
2440 2441
	struct scic_sds_port *sci_port = (struct scic_sds_port *)object;
	struct isci_port *iport = sci_object_get_association(sci_port);
2442

2443 2444
	scic_sds_port_set_base_state_handlers(sci_port,
					      SCI_BASE_PORT_STATE_FAILED);
2445

2446
	isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
2447 2448 2449 2450
}

/* --------------------------------------------------------------------------- */

D
Dan Williams 已提交
2451
static const struct sci_base_state scic_sds_port_state_table[] = {
2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472
	[SCI_BASE_PORT_STATE_STOPPED] = {
		.enter_state = scic_sds_port_stopped_state_enter,
		.exit_state  = scic_sds_port_stopped_state_exit
	},
	[SCI_BASE_PORT_STATE_STOPPING] = {
		.enter_state = scic_sds_port_stopping_state_enter,
		.exit_state  = scic_sds_port_stopping_state_exit
	},
	[SCI_BASE_PORT_STATE_READY] = {
		.enter_state = scic_sds_port_ready_state_enter,
		.exit_state  = scic_sds_port_ready_state_exit
	},
	[SCI_BASE_PORT_STATE_RESETTING] = {
		.enter_state = scic_sds_port_resetting_state_enter,
		.exit_state  = scic_sds_port_resetting_state_exit
	},
	[SCI_BASE_PORT_STATE_FAILED] = {
		.enter_state = scic_sds_port_failed_state_enter,
	}
};

D
Dan Williams 已提交
2473 2474 2475 2476 2477
void scic_sds_port_construct(struct scic_sds_port *sci_port, u8 port_index,
			     struct scic_sds_controller *scic)
{
	u32 index;

2478 2479 2480 2481 2482 2483 2484
	sci_port->parent.private = NULL;
	sci_base_state_machine_construct(&sci_port->state_machine,
					 &sci_port->parent,
					 scic_sds_port_state_table,
					 SCI_BASE_PORT_STATE_STOPPED);

	sci_base_state_machine_start(&sci_port->state_machine);
D
Dan Williams 已提交
2485 2486

	sci_base_state_machine_construct(&sci_port->ready_substate_machine,
2487
					 &sci_port->parent,
D
Dan Williams 已提交
2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
					 scic_sds_port_ready_substate_table,
					 SCIC_SDS_PORT_READY_SUBSTATE_WAITING);

	sci_port->logical_port_index  = SCIC_SDS_DUMMY_PORT;
	sci_port->physical_port_index = port_index;
	sci_port->active_phy_mask     = 0;

	sci_port->owning_controller = scic;

	sci_port->started_request_count = 0;
	sci_port->assigned_device_count = 0;

	sci_port->reserved_rni = SCU_DUMMY_INDEX;
	sci_port->reserved_tci = SCU_DUMMY_INDEX;

	sci_port->timer_handle = NULL;

	sci_port->port_task_scheduler_registers = NULL;

	for (index = 0; index < SCI_MAX_PHYS; index++)
		sci_port->phy_table[index] = NULL;
}