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

#include "isci.h"
D
Dan Williams 已提交
57
#include "host.h"
58
#include "phy.h"
D
Dan Williams 已提交
59
#include "scu_event_codes.h"
D
Dan Williams 已提交
60
#include "probe_roms.h"
61

D
Dan Williams 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
/* Maximum arbitration wait time in micro-seconds */
#define SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME  (700)

enum sas_linkrate sci_phy_linkrate(struct scic_sds_phy *sci_phy)
{
	return sci_phy->max_negotiated_speed;
}

/*
 * *****************************************************************************
 * * SCIC SDS PHY Internal Methods
 * ***************************************************************************** */

/**
 * This method will initialize the phy transport layer registers
 * @sci_phy:
 * @transport_layer_registers
 *
 * enum sci_status
 */
static enum sci_status scic_sds_phy_transport_layer_initialization(
	struct scic_sds_phy *sci_phy,
	struct scu_transport_layer_registers __iomem *transport_layer_registers)
{
	u32 tl_control;

	sci_phy->transport_layer_registers = transport_layer_registers;

	writel(SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX,
		&sci_phy->transport_layer_registers->stp_rni);

	/*
	 * Hardware team recommends that we enable the STP prefetch for all
	 * transports
	 */
	tl_control = readl(&sci_phy->transport_layer_registers->control);
	tl_control |= SCU_TLCR_GEN_BIT(STP_WRITE_DATA_PREFETCH);
	writel(tl_control, &sci_phy->transport_layer_registers->control);

	return SCI_SUCCESS;
}

/**
 * This method will initialize the phy link layer registers
 * @sci_phy:
 * @link_layer_registers:
 *
 * enum sci_status
 */
static enum sci_status
scic_sds_phy_link_layer_initialization(struct scic_sds_phy *sci_phy,
				       struct scu_link_layer_registers __iomem *link_layer_registers)
{
	struct scic_sds_controller *scic =
		sci_phy->owning_port->owning_controller;
	int phy_idx = sci_phy->phy_index;
	struct sci_phy_user_params *phy_user =
		&scic->user_parameters.sds1.phys[phy_idx];
	struct sci_phy_oem_params *phy_oem =
		&scic->oem_parameters.sds1.phys[phy_idx];
	u32 phy_configuration;
	struct scic_phy_cap phy_cap;
	u32 parity_check = 0;
	u32 parity_count = 0;
	u32 llctl, link_rate;
	u32 clksm_value = 0;

	sci_phy->link_layer_registers = link_layer_registers;

	/* Set our IDENTIFY frame data */
	#define SCI_END_DEVICE 0x01

	writel(SCU_SAS_TIID_GEN_BIT(SMP_INITIATOR) |
	       SCU_SAS_TIID_GEN_BIT(SSP_INITIATOR) |
	       SCU_SAS_TIID_GEN_BIT(STP_INITIATOR) |
	       SCU_SAS_TIID_GEN_BIT(DA_SATA_HOST) |
	       SCU_SAS_TIID_GEN_VAL(DEVICE_TYPE, SCI_END_DEVICE),
	       &sci_phy->link_layer_registers->transmit_identification);

	/* Write the device SAS Address */
	writel(0xFEDCBA98,
	       &sci_phy->link_layer_registers->sas_device_name_high);
	writel(phy_idx, &sci_phy->link_layer_registers->sas_device_name_low);

	/* Write the source SAS Address */
	writel(phy_oem->sas_address.high,
		&sci_phy->link_layer_registers->source_sas_address_high);
	writel(phy_oem->sas_address.low,
		&sci_phy->link_layer_registers->source_sas_address_low);

	/* Clear and Set the PHY Identifier */
	writel(0, &sci_phy->link_layer_registers->identify_frame_phy_id);
	writel(SCU_SAS_TIPID_GEN_VALUE(ID, phy_idx),
		&sci_phy->link_layer_registers->identify_frame_phy_id);

	/* Change the initial state of the phy configuration register */
	phy_configuration =
		readl(&sci_phy->link_layer_registers->phy_configuration);

	/* Hold OOB state machine in reset */
	phy_configuration |=  SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
	writel(phy_configuration,
		&sci_phy->link_layer_registers->phy_configuration);

	/* Configure the SNW capabilities */
	phy_cap.all = 0;
	phy_cap.start = 1;
	phy_cap.gen3_no_ssc = 1;
	phy_cap.gen2_no_ssc = 1;
	phy_cap.gen1_no_ssc = 1;
	if (scic->oem_parameters.sds1.controller.do_enable_ssc == true) {
		phy_cap.gen3_ssc = 1;
		phy_cap.gen2_ssc = 1;
		phy_cap.gen1_ssc = 1;
	}

	/*
	 * The SAS specification indicates that the phy_capabilities that
	 * are transmitted shall have an even parity.  Calculate the parity. */
	parity_check = phy_cap.all;
	while (parity_check != 0) {
		if (parity_check & 0x1)
			parity_count++;
		parity_check >>= 1;
	}

	/*
	 * If parity indicates there are an odd number of bits set, then
	 * set the parity bit to 1 in the phy capabilities. */
	if ((parity_count % 2) != 0)
		phy_cap.parity = 1;

	writel(phy_cap.all, &sci_phy->link_layer_registers->phy_capabilities);

	/* Set the enable spinup period but disable the ability to send
	 * notify enable spinup
	 */
	writel(SCU_ENSPINUP_GEN_VAL(COUNT,
			phy_user->notify_enable_spin_up_insertion_frequency),
		&sci_phy->link_layer_registers->notify_enable_spinup_control);

	/* Write the ALIGN Insertion Ferequency for connected phy and
	 * inpendent of connected state
	 */
	clksm_value = SCU_ALIGN_INSERTION_FREQUENCY_GEN_VAL(CONNECTED,
			phy_user->in_connection_align_insertion_frequency);

	clksm_value |= SCU_ALIGN_INSERTION_FREQUENCY_GEN_VAL(GENERAL,
			phy_user->align_insertion_frequency);

	writel(clksm_value, &sci_phy->link_layer_registers->clock_skew_management);

	/* @todo Provide a way to write this register correctly */
	writel(0x02108421,
		&sci_phy->link_layer_registers->afe_lookup_table_control);

	llctl = SCU_SAS_LLCTL_GEN_VAL(NO_OUTBOUND_TASK_TIMEOUT,
		(u8)scic->user_parameters.sds1.no_outbound_task_timeout);

	switch(phy_user->max_speed_generation) {
	case SCIC_SDS_PARM_GEN3_SPEED:
		link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN3;
		break;
	case SCIC_SDS_PARM_GEN2_SPEED:
		link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN2;
		break;
	default:
		link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN1;
		break;
	}
	llctl |= SCU_SAS_LLCTL_GEN_VAL(MAX_LINK_RATE, link_rate);
	writel(llctl, &sci_phy->link_layer_registers->link_layer_control);

	if (is_a0() || is_a2()) {
		/* Program the max ARB time for the PHY to 700us so we inter-operate with
		 * the PMC expander which shuts down PHYs if the expander PHY generates too
		 * many breaks.  This time value will guarantee that the initiator PHY will
		 * generate the break.
		 */
		writel(SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME,
			&sci_phy->link_layer_registers->maximum_arbitration_wait_timer_timeout);
	}

245 246
	/* Disable link layer hang detection, rely on the OS timeout for I/O timeouts. */
	writel(0, &sci_phy->link_layer_registers->link_layer_hang_detection_timeout);
D
Dan Williams 已提交
247 248

	/* We can exit the initial state to the stopped state */
E
Edmund Nadolski 已提交
249
	sci_change_state(&sci_phy->sm, SCI_PHY_STOPPED);
D
Dan Williams 已提交
250 251 252 253

	return SCI_SUCCESS;
}

254
static void phy_sata_timeout(unsigned long data)
D
Dan Williams 已提交
255
{
256 257 258 259 260 261 262 263 264
	struct sci_timer *tmr = (struct sci_timer *)data;
	struct scic_sds_phy *sci_phy = container_of(tmr, typeof(*sci_phy), sata_timer);
	struct isci_host *ihost = scic_to_ihost(sci_phy->owning_port->owning_controller);
	unsigned long flags;

	spin_lock_irqsave(&ihost->scic_lock, flags);

	if (tmr->cancel)
		goto done;
D
Dan Williams 已提交
265 266 267 268 269 270 271

	dev_dbg(sciphy_to_dev(sci_phy),
		 "%s: SCIC SDS Phy 0x%p did not receive signature fis before "
		 "timeout.\n",
		 __func__,
		 sci_phy);

E
Edmund Nadolski 已提交
272
	sci_change_state(&sci_phy->sm, SCI_PHY_STARTING);
273 274
done:
	spin_unlock_irqrestore(&ihost->scic_lock, flags);
D
Dan Williams 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288
}

/**
 * This method returns the port currently containing this phy. If the phy is
 *    currently contained by the dummy port, then the phy is considered to not
 *    be part of a port.
 * @sci_phy: This parameter specifies the phy for which to retrieve the
 *    containing port.
 *
 * This method returns a handle to a port that contains the supplied phy.
 * NULL This value is returned if the phy is not part of a real
 * port (i.e. it's contained in the dummy port). !NULL All other
 * values indicate a handle/pointer to the port containing the phy.
 */
D
Dan Williams 已提交
289
struct scic_sds_port *phy_get_non_dummy_port(
D
Dan Williams 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
	struct scic_sds_phy *sci_phy)
{
	if (scic_sds_port_get_index(sci_phy->owning_port) == SCIC_SDS_DUMMY_PORT)
		return NULL;

	return sci_phy->owning_port;
}

/**
 * This method will assign a port to the phy object.
 * @out]: sci_phy This parameter specifies the phy for which to assign a port
 *    object.
 *
 *
 */
void scic_sds_phy_set_port(
	struct scic_sds_phy *sci_phy,
	struct scic_sds_port *sci_port)
{
	sci_phy->owning_port = sci_port;

	if (sci_phy->bcn_received_while_port_unassigned) {
		sci_phy->bcn_received_while_port_unassigned = false;
		scic_sds_port_broadcast_change_received(sci_phy->owning_port, sci_phy);
	}
}

/**
 * This method will initialize the constructed phy
 * @sci_phy:
 * @link_layer_registers:
 *
 * enum sci_status
 */
enum sci_status scic_sds_phy_initialize(
	struct scic_sds_phy *sci_phy,
	struct scu_transport_layer_registers __iomem *transport_layer_registers,
	struct scu_link_layer_registers __iomem *link_layer_registers)
{
	/* Perfrom the initialization of the TL hardware */
	scic_sds_phy_transport_layer_initialization(
			sci_phy,
			transport_layer_registers);

	/* Perofrm the initialization of the PE hardware */
	scic_sds_phy_link_layer_initialization(sci_phy, link_layer_registers);

	/*
	 * There is nothing that needs to be done in this state just
	 * transition to the stopped state. */
E
Edmund Nadolski 已提交
340
	sci_change_state(&sci_phy->sm, SCI_PHY_STOPPED);
D
Dan Williams 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432

	return SCI_SUCCESS;
}

/**
 * This method assigns the direct attached device ID for this phy.
 *
 * @sci_phy The phy for which the direct attached device id is to
 *       be assigned.
 * @device_id The direct attached device ID to assign to the phy.
 *       This will either be the RNi for the device or an invalid RNi if there
 *       is no current device assigned to the phy.
 */
void scic_sds_phy_setup_transport(
	struct scic_sds_phy *sci_phy,
	u32 device_id)
{
	u32 tl_control;

	writel(device_id, &sci_phy->transport_layer_registers->stp_rni);

	/*
	 * The read should guarantee that the first write gets posted
	 * before the next write
	 */
	tl_control = readl(&sci_phy->transport_layer_registers->control);
	tl_control |= SCU_TLCR_GEN_BIT(CLEAR_TCI_NCQ_MAPPING_TABLE);
	writel(tl_control, &sci_phy->transport_layer_registers->control);
}

/**
 *
 * @sci_phy: The phy object to be suspended.
 *
 * This function will perform the register reads/writes to suspend the SCU
 * hardware protocol engine. none
 */
static void scic_sds_phy_suspend(
	struct scic_sds_phy *sci_phy)
{
	u32 scu_sas_pcfg_value;

	scu_sas_pcfg_value =
		readl(&sci_phy->link_layer_registers->phy_configuration);
	scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
	writel(scu_sas_pcfg_value,
		&sci_phy->link_layer_registers->phy_configuration);

	scic_sds_phy_setup_transport(
			sci_phy,
			SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
}

void scic_sds_phy_resume(struct scic_sds_phy *sci_phy)
{
	u32 scu_sas_pcfg_value;

	scu_sas_pcfg_value =
		readl(&sci_phy->link_layer_registers->phy_configuration);
	scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
	writel(scu_sas_pcfg_value,
		&sci_phy->link_layer_registers->phy_configuration);
}

void scic_sds_phy_get_sas_address(struct scic_sds_phy *sci_phy,
				  struct sci_sas_address *sas_address)
{
	sas_address->high = readl(&sci_phy->link_layer_registers->source_sas_address_high);
	sas_address->low = readl(&sci_phy->link_layer_registers->source_sas_address_low);
}

void scic_sds_phy_get_attached_sas_address(struct scic_sds_phy *sci_phy,
					   struct sci_sas_address *sas_address)
{
	struct sas_identify_frame *iaf;
	struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);

	iaf = &iphy->frame_rcvd.iaf;
	memcpy(sas_address, iaf->sas_addr, SAS_ADDR_SIZE);
}

void scic_sds_phy_get_protocols(struct scic_sds_phy *sci_phy,
				struct scic_phy_proto *protocols)
{
	protocols->all =
		(u16)(readl(&sci_phy->
			link_layer_registers->transmit_identification) &
				0x0000FFFF);
}

enum sci_status scic_sds_phy_start(struct scic_sds_phy *sci_phy)
{
E
Edmund Nadolski 已提交
433
	enum scic_sds_phy_states state = sci_phy->sm.current_state_id;
D
Dan Williams 已提交
434

E
Edmund Nadolski 已提交
435
	if (state != SCI_PHY_STOPPED) {
D
Dan Williams 已提交
436 437 438 439 440
		dev_dbg(sciphy_to_dev(sci_phy),
			 "%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}

E
Edmund Nadolski 已提交
441
	sci_change_state(&sci_phy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
442
	return SCI_SUCCESS;
D
Dan Williams 已提交
443 444 445 446
}

enum sci_status scic_sds_phy_stop(struct scic_sds_phy *sci_phy)
{
E
Edmund Nadolski 已提交
447
	enum scic_sds_phy_states state = sci_phy->sm.current_state_id;
D
Dan Williams 已提交
448 449

	switch (state) {
E
Edmund Nadolski 已提交
450 451 452 453 454 455 456 457 458 459
	case SCI_PHY_SUB_INITIAL:
	case SCI_PHY_SUB_AWAIT_OSSP_EN:
	case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
	case SCI_PHY_SUB_AWAIT_SAS_POWER:
	case SCI_PHY_SUB_AWAIT_SATA_POWER:
	case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
	case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
	case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
	case SCI_PHY_SUB_FINAL:
	case SCI_PHY_READY:
D
Dan Williams 已提交
460 461 462 463 464 465 466
		break;
	default:
		dev_dbg(sciphy_to_dev(sci_phy),
			"%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}

E
Edmund Nadolski 已提交
467
	sci_change_state(&sci_phy->sm, SCI_PHY_STOPPED);
D
Dan Williams 已提交
468
	return SCI_SUCCESS;
D
Dan Williams 已提交
469 470
}

D
Dan Williams 已提交
471
enum sci_status scic_sds_phy_reset(struct scic_sds_phy *sci_phy)
D
Dan Williams 已提交
472
{
E
Edmund Nadolski 已提交
473
	enum scic_sds_phy_states state = sci_phy->sm.current_state_id;
D
Dan Williams 已提交
474

E
Edmund Nadolski 已提交
475
	if (state != SCI_PHY_READY) {
D
Dan Williams 已提交
476 477 478 479 480
		dev_dbg(sciphy_to_dev(sci_phy),
			"%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}

E
Edmund Nadolski 已提交
481
	sci_change_state(&sci_phy->sm, SCI_PHY_RESETTING);
D
Dan Williams 已提交
482
	return SCI_SUCCESS;
D
Dan Williams 已提交
483 484
}

485
enum sci_status scic_sds_phy_consume_power_handler(struct scic_sds_phy *sci_phy)
D
Dan Williams 已提交
486
{
E
Edmund Nadolski 已提交
487
	enum scic_sds_phy_states state = sci_phy->sm.current_state_id;
488 489

	switch (state) {
E
Edmund Nadolski 已提交
490
	case SCI_PHY_SUB_AWAIT_SAS_POWER: {
491 492 493 494 495 496 497
		u32 enable_spinup;

		enable_spinup = readl(&sci_phy->link_layer_registers->notify_enable_spinup_control);
		enable_spinup |= SCU_ENSPINUP_GEN_BIT(ENABLE);
		writel(enable_spinup, &sci_phy->link_layer_registers->notify_enable_spinup_control);

		/* Change state to the final state this substate machine has run to completion */
E
Edmund Nadolski 已提交
498
		sci_change_state(&sci_phy->sm, SCI_PHY_SUB_FINAL);
499 500 501

		return SCI_SUCCESS;
	}
E
Edmund Nadolski 已提交
502
	case SCI_PHY_SUB_AWAIT_SATA_POWER: {
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
		u32 scu_sas_pcfg_value;

		/* Release the spinup hold state and reset the OOB state machine */
		scu_sas_pcfg_value =
			readl(&sci_phy->link_layer_registers->phy_configuration);
		scu_sas_pcfg_value &=
			~(SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD) | SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE));
		scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
		writel(scu_sas_pcfg_value,
			&sci_phy->link_layer_registers->phy_configuration);

		/* Now restart the OOB operation */
		scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
		scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
		writel(scu_sas_pcfg_value,
			&sci_phy->link_layer_registers->phy_configuration);

		/* Change state to the final state this substate machine has run to completion */
E
Edmund Nadolski 已提交
521
		sci_change_state(&sci_phy->sm, SCI_PHY_SUB_AWAIT_SATA_PHY_EN);
522 523 524 525 526 527 528 529

		return SCI_SUCCESS;
	}
	default:
		dev_dbg(sciphy_to_dev(sci_phy),
			"%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}
D
Dan Williams 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
}

/*
 * *****************************************************************************
 * * SCIC SDS PHY HELPER FUNCTIONS
 * ***************************************************************************** */


/**
 *
 * @sci_phy: The phy object that received SAS PHY DETECTED.
 *
 * This method continues the link training for the phy as if it were a SAS PHY
 * instead of a SATA PHY. This is done because the completion queue had a SAS
 * PHY DETECTED event when the state machine was expecting a SATA PHY event.
 * none
 */
static void scic_sds_phy_start_sas_link_training(
	struct scic_sds_phy *sci_phy)
{
	u32 phy_control;

	phy_control =
		readl(&sci_phy->link_layer_registers->phy_configuration);
	phy_control |= SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD);
	writel(phy_control,
		&sci_phy->link_layer_registers->phy_configuration);

E
Edmund Nadolski 已提交
558
	sci_change_state(&sci_phy->sm, SCI_PHY_SUB_AWAIT_SAS_SPEED_EN);
D
Dan Williams 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573

	sci_phy->protocol = SCIC_SDS_PHY_PROTOCOL_SAS;
}

/**
 *
 * @sci_phy: The phy object that received a SATA SPINUP HOLD event
 *
 * This method continues the link training for the phy as if it were a SATA PHY
 * instead of a SAS PHY.  This is done because the completion queue had a SATA
 * SPINUP HOLD event when the state machine was expecting a SAS PHY event. none
 */
static void scic_sds_phy_start_sata_link_training(
	struct scic_sds_phy *sci_phy)
{
E
Edmund Nadolski 已提交
574
	sci_change_state(&sci_phy->sm, SCI_PHY_SUB_AWAIT_SATA_POWER);
D
Dan Williams 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596

	sci_phy->protocol = SCIC_SDS_PHY_PROTOCOL_SATA;
}

/**
 * scic_sds_phy_complete_link_training - perform processing common to
 *    all protocols upon completion of link training.
 * @sci_phy: This parameter specifies the phy object for which link training
 *    has completed.
 * @max_link_rate: This parameter specifies the maximum link rate to be
 *    associated with this phy.
 * @next_state: This parameter specifies the next state for the phy's starting
 *    sub-state machine.
 *
 */
static void scic_sds_phy_complete_link_training(
	struct scic_sds_phy *sci_phy,
	enum sas_linkrate max_link_rate,
	u32 next_state)
{
	sci_phy->max_negotiated_speed = max_link_rate;

E
Edmund Nadolski 已提交
597
	sci_change_state(&sci_phy->sm, next_state);
D
Dan Williams 已提交
598 599
}

D
Dan Williams 已提交
600 601
enum sci_status scic_sds_phy_event_handler(struct scic_sds_phy *sci_phy,
					   u32 event_code)
A
Adam Gruchala 已提交
602
{
E
Edmund Nadolski 已提交
603
	enum scic_sds_phy_states state = sci_phy->sm.current_state_id;
A
Adam Gruchala 已提交
604

D
Dan Williams 已提交
605
	switch (state) {
E
Edmund Nadolski 已提交
606
	case SCI_PHY_SUB_AWAIT_OSSP_EN:
D
Dan Williams 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_SAS_PHY_DETECTED:
			scic_sds_phy_start_sas_link_training(sci_phy);
			sci_phy->is_in_link_training = true;
			break;
		case SCU_EVENT_SATA_SPINUP_HOLD:
			scic_sds_phy_start_sata_link_training(sci_phy);
			sci_phy->is_in_link_training = true;
			break;
		default:
			dev_dbg(sciphy_to_dev(sci_phy),
				"%s: PHY starting substate machine received "
				"unexpected event_code %x\n",
				__func__,
				event_code);
			return SCI_FAILURE;
		}
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
625
	case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
D
Dan Williams 已提交
626 627 628 629 630 631 632 633 634 635 636
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_SAS_PHY_DETECTED:
			/*
			 * Why is this being reported again by the controller?
			 * We would re-enter this state so just stay here */
			break;
		case SCU_EVENT_SAS_15:
		case SCU_EVENT_SAS_15_SSC:
			scic_sds_phy_complete_link_training(
				sci_phy,
				SAS_LINK_RATE_1_5_GBPS,
E
Edmund Nadolski 已提交
637
				SCI_PHY_SUB_AWAIT_IAF_UF);
D
Dan Williams 已提交
638 639 640 641 642 643
			break;
		case SCU_EVENT_SAS_30:
		case SCU_EVENT_SAS_30_SSC:
			scic_sds_phy_complete_link_training(
				sci_phy,
				SAS_LINK_RATE_3_0_GBPS,
E
Edmund Nadolski 已提交
644
				SCI_PHY_SUB_AWAIT_IAF_UF);
D
Dan Williams 已提交
645 646 647 648 649 650
			break;
		case SCU_EVENT_SAS_60:
		case SCU_EVENT_SAS_60_SSC:
			scic_sds_phy_complete_link_training(
				sci_phy,
				SAS_LINK_RATE_6_0_GBPS,
E
Edmund Nadolski 已提交
651
				SCI_PHY_SUB_AWAIT_IAF_UF);
D
Dan Williams 已提交
652 653 654 655 656 657 658 659 660
			break;
		case SCU_EVENT_SATA_SPINUP_HOLD:
			/*
			 * We were doing SAS PHY link training and received a SATA PHY event
			 * continue OOB/SN as if this were a SATA PHY */
			scic_sds_phy_start_sata_link_training(sci_phy);
			break;
		case SCU_EVENT_LINK_FAILURE:
			/* Link failure change state back to the starting state */
E
Edmund Nadolski 已提交
661
			sci_change_state(&sci_phy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
662 663 664 665 666 667 668 669 670 671 672
			break;
		default:
			dev_warn(sciphy_to_dev(sci_phy),
				 "%s: PHY starting substate machine received "
				 "unexpected event_code %x\n",
				 __func__, event_code);

			return SCI_FAILURE;
			break;
		}
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
673
	case SCI_PHY_SUB_AWAIT_IAF_UF:
D
Dan Williams 已提交
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_SAS_PHY_DETECTED:
			/* Backup the state machine */
			scic_sds_phy_start_sas_link_training(sci_phy);
			break;
		case SCU_EVENT_SATA_SPINUP_HOLD:
			/* We were doing SAS PHY link training and received a
			 * SATA PHY event continue OOB/SN as if this were a
			 * SATA PHY
			 */
			scic_sds_phy_start_sata_link_training(sci_phy);
			break;
		case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
		case SCU_EVENT_LINK_FAILURE:
		case SCU_EVENT_HARD_RESET_RECEIVED:
			/* Start the oob/sn state machine over again */
E
Edmund Nadolski 已提交
690
			sci_change_state(&sci_phy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
691 692 693 694 695 696 697 698 699
			break;
		default:
			dev_warn(sciphy_to_dev(sci_phy),
				 "%s: PHY starting substate machine received "
				 "unexpected event_code %x\n",
				 __func__, event_code);
			return SCI_FAILURE;
		}
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
700
	case SCI_PHY_SUB_AWAIT_SAS_POWER:
D
Dan Williams 已提交
701 702 703
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_LINK_FAILURE:
			/* Link failure change state back to the starting state */
E
Edmund Nadolski 已提交
704
			sci_change_state(&sci_phy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
705 706 707 708 709 710 711 712 713 714
			break;
		default:
			dev_warn(sciphy_to_dev(sci_phy),
				"%s: PHY starting substate machine received unexpected "
				"event_code %x\n",
				__func__,
				event_code);
			return SCI_FAILURE;
		}
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
715
	case SCI_PHY_SUB_AWAIT_SATA_POWER:
D
Dan Williams 已提交
716 717 718
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_LINK_FAILURE:
			/* Link failure change state back to the starting state */
E
Edmund Nadolski 已提交
719
			sci_change_state(&sci_phy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
			break;
		case SCU_EVENT_SATA_SPINUP_HOLD:
			/* These events are received every 10ms and are
			 * expected while in this state
			 */
			break;

		case SCU_EVENT_SAS_PHY_DETECTED:
			/* There has been a change in the phy type before OOB/SN for the
			 * SATA finished start down the SAS link traning path.
			 */
			scic_sds_phy_start_sas_link_training(sci_phy);
			break;

		default:
			dev_warn(sciphy_to_dev(sci_phy),
				 "%s: PHY starting substate machine received "
				 "unexpected event_code %x\n",
				 __func__, event_code);
A
Adam Gruchala 已提交
739

D
Dan Williams 已提交
740 741 742
			return SCI_FAILURE;
		}
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
743
	case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
D
Dan Williams 已提交
744 745 746
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_LINK_FAILURE:
			/* Link failure change state back to the starting state */
E
Edmund Nadolski 已提交
747
			sci_change_state(&sci_phy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
748 749 750 751 752 753 754 755 756 757
			break;
		case SCU_EVENT_SATA_SPINUP_HOLD:
			/* These events might be received since we dont know how many may be in
			 * the completion queue while waiting for power
			 */
			break;
		case SCU_EVENT_SATA_PHY_DETECTED:
			sci_phy->protocol = SCIC_SDS_PHY_PROTOCOL_SATA;

			/* We have received the SATA PHY notification change state */
E
Edmund Nadolski 已提交
758
			sci_change_state(&sci_phy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
D
Dan Williams 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771
			break;
		case SCU_EVENT_SAS_PHY_DETECTED:
			/* There has been a change in the phy type before OOB/SN for the
			 * SATA finished start down the SAS link traning path.
			 */
			scic_sds_phy_start_sas_link_training(sci_phy);
			break;
		default:
			dev_warn(sciphy_to_dev(sci_phy),
				 "%s: PHY starting substate machine received "
				 "unexpected event_code %x\n",
				 __func__,
				 event_code);
A
Adam Gruchala 已提交
772

D
Dan Williams 已提交
773 774 775
			return SCI_FAILURE;;
		}
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
776
	case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
D
Dan Williams 已提交
777 778 779 780 781 782 783 784 785 786 787
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_SATA_PHY_DETECTED:
			/*
			 * The hardware reports multiple SATA PHY detected events
			 * ignore the extras */
			break;
		case SCU_EVENT_SATA_15:
		case SCU_EVENT_SATA_15_SSC:
			scic_sds_phy_complete_link_training(
				sci_phy,
				SAS_LINK_RATE_1_5_GBPS,
E
Edmund Nadolski 已提交
788
				SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
D
Dan Williams 已提交
789 790 791 792 793 794
			break;
		case SCU_EVENT_SATA_30:
		case SCU_EVENT_SATA_30_SSC:
			scic_sds_phy_complete_link_training(
				sci_phy,
				SAS_LINK_RATE_3_0_GBPS,
E
Edmund Nadolski 已提交
795
				SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
D
Dan Williams 已提交
796 797 798 799 800 801
			break;
		case SCU_EVENT_SATA_60:
		case SCU_EVENT_SATA_60_SSC:
			scic_sds_phy_complete_link_training(
				sci_phy,
				SAS_LINK_RATE_6_0_GBPS,
E
Edmund Nadolski 已提交
802
				SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
D
Dan Williams 已提交
803 804 805
			break;
		case SCU_EVENT_LINK_FAILURE:
			/* Link failure change state back to the starting state */
E
Edmund Nadolski 已提交
806
			sci_change_state(&sci_phy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
807 808 809 810 811 812 813 814 815 816 817 818
			break;
		case SCU_EVENT_SAS_PHY_DETECTED:
			/*
			 * There has been a change in the phy type before OOB/SN for the
			 * SATA finished start down the SAS link traning path. */
			scic_sds_phy_start_sas_link_training(sci_phy);
			break;
		default:
			dev_warn(sciphy_to_dev(sci_phy),
				 "%s: PHY starting substate machine received "
				 "unexpected event_code %x\n",
				 __func__, event_code);
A
Adam Gruchala 已提交
819

D
Dan Williams 已提交
820 821 822 823
			return SCI_FAILURE;
		}

		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
824
	case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
D
Dan Williams 已提交
825 826 827
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_SATA_PHY_DETECTED:
			/* Backup the state machine */
E
Edmund Nadolski 已提交
828
			sci_change_state(&sci_phy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
D
Dan Williams 已提交
829
			break;
A
Adam Gruchala 已提交
830

D
Dan Williams 已提交
831 832
		case SCU_EVENT_LINK_FAILURE:
			/* Link failure change state back to the starting state */
E
Edmund Nadolski 已提交
833
			sci_change_state(&sci_phy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
834 835 836 837 838 839 840 841 842 843 844 845
			break;

		default:
			dev_warn(sciphy_to_dev(sci_phy),
				 "%s: PHY starting substate machine received "
				 "unexpected event_code %x\n",
				 __func__,
				 event_code);

			return SCI_FAILURE;
		}
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
846
	case SCI_PHY_READY:
D
Dan Williams 已提交
847 848 849
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_LINK_FAILURE:
			/* Link failure change state back to the starting state */
E
Edmund Nadolski 已提交
850
			sci_change_state(&sci_phy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
851 852 853
			break;
		case SCU_EVENT_BROADCAST_CHANGE:
			/* Broadcast change received. Notify the port. */
D
Dan Williams 已提交
854
			if (phy_get_non_dummy_port(sci_phy) != NULL)
D
Dan Williams 已提交
855 856 857 858 859 860 861 862 863 864 865 866
				scic_sds_port_broadcast_change_received(sci_phy->owning_port, sci_phy);
			else
				sci_phy->bcn_received_while_port_unassigned = true;
			break;
		default:
			dev_warn(sciphy_to_dev(sci_phy),
				 "%sP SCIC PHY 0x%p ready state machine received "
				 "unexpected event_code %x\n",
				 __func__, sci_phy, event_code);
			return SCI_FAILURE_INVALID_STATE;
		}
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
867
	case SCI_PHY_RESETTING:
D
Dan Williams 已提交
868 869 870
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_HARD_RESET_TRANSMITTED:
			/* Link failure change state back to the starting state */
E
Edmund Nadolski 已提交
871
			sci_change_state(&sci_phy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
			break;
		default:
			dev_warn(sciphy_to_dev(sci_phy),
				 "%s: SCIC PHY 0x%p resetting state machine received "
				 "unexpected event_code %x\n",
				 __func__, sci_phy, event_code);

			return SCI_FAILURE_INVALID_STATE;
			break;
		}
		return SCI_SUCCESS;
	default:
		dev_dbg(sciphy_to_dev(sci_phy),
			"%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}
A
Adam Gruchala 已提交
888 889
}

D
Dan Williams 已提交
890 891
enum sci_status scic_sds_phy_frame_handler(struct scic_sds_phy *sci_phy,
					   u32 frame_index)
A
Adam Gruchala 已提交
892
{
E
Edmund Nadolski 已提交
893
	enum scic_sds_phy_states state = sci_phy->sm.current_state_id;
D
Dan Williams 已提交
894 895
	struct scic_sds_controller *scic = sci_phy->owning_port->owning_controller;
	enum sci_status result;
D
Dan Williams 已提交
896
	unsigned long flags;
A
Adam Gruchala 已提交
897

D
Dan Williams 已提交
898
	switch (state) {
E
Edmund Nadolski 已提交
899
	case SCI_PHY_SUB_AWAIT_IAF_UF: {
D
Dan Williams 已提交
900 901 902
		u32 *frame_words;
		struct sas_identify_frame iaf;
		struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
A
Adam Gruchala 已提交
903

D
Dan Williams 已提交
904 905 906
		result = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
								       frame_index,
								       (void **)&frame_words);
A
Adam Gruchala 已提交
907

D
Dan Williams 已提交
908 909 910 911 912 913 914
		if (result != SCI_SUCCESS)
			return result;

		sci_swab32_cpy(&iaf, frame_words, sizeof(iaf) / sizeof(u32));
		if (iaf.frame_type == 0) {
			u32 state;

D
Dan Williams 已提交
915
			spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
D
Dan Williams 已提交
916
			memcpy(&iphy->frame_rcvd.iaf, &iaf, sizeof(iaf));
D
Dan Williams 已提交
917
			spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
D
Dan Williams 已提交
918 919 920 921 922
			if (iaf.smp_tport) {
				/* We got the IAF for an expander PHY go to the final
				 * state since there are no power requirements for
				 * expander phys.
				 */
E
Edmund Nadolski 已提交
923
				state = SCI_PHY_SUB_FINAL;
D
Dan Williams 已提交
924 925 926 927
			} else {
				/* We got the IAF we can now go to the await spinup
				 * semaphore state
				 */
E
Edmund Nadolski 已提交
928
				state = SCI_PHY_SUB_AWAIT_SAS_POWER;
D
Dan Williams 已提交
929
			}
E
Edmund Nadolski 已提交
930
			sci_change_state(&sci_phy->sm, state);
D
Dan Williams 已提交
931 932 933 934 935 936 937 938 939
			result = SCI_SUCCESS;
		} else
			dev_warn(sciphy_to_dev(sci_phy),
				"%s: PHY starting substate machine received "
				"unexpected frame id %x\n",
				__func__, frame_index);

		scic_sds_controller_release_frame(scic, frame_index);
		return result;
A
Adam Gruchala 已提交
940
	}
E
Edmund Nadolski 已提交
941
	case SCI_PHY_SUB_AWAIT_SIG_FIS_UF: {
D
Dan Williams 已提交
942 943 944 945 946 947 948 949 950 951 952
		struct dev_to_host_fis *frame_header;
		u32 *fis_frame_data;
		struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);

		result = scic_sds_unsolicited_frame_control_get_header(
			&(scic_sds_phy_get_controller(sci_phy)->uf_control),
			frame_index,
			(void **)&frame_header);

		if (result != SCI_SUCCESS)
			return result;
A
Adam Gruchala 已提交
953

D
Dan Williams 已提交
954 955 956 957 958 959
		if ((frame_header->fis_type == FIS_REGD2H) &&
		    !(frame_header->status & ATA_BUSY)) {
			scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
								      frame_index,
								      (void **)&fis_frame_data);

D
Dan Williams 已提交
960
			spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
D
Dan Williams 已提交
961 962 963
			scic_sds_controller_copy_sata_response(&iphy->frame_rcvd.fis,
							       frame_header,
							       fis_frame_data);
D
Dan Williams 已提交
964
			spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
D
Dan Williams 已提交
965 966

			/* got IAF we can now go to the await spinup semaphore state */
E
Edmund Nadolski 已提交
967
			sci_change_state(&sci_phy->sm, SCI_PHY_SUB_FINAL);
D
Dan Williams 已提交
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985

			result = SCI_SUCCESS;
		} else
			dev_warn(sciphy_to_dev(sci_phy),
				 "%s: PHY starting substate machine received "
				 "unexpected frame id %x\n",
				 __func__, frame_index);

		/* Regardless of the result we are done with this frame with it */
		scic_sds_controller_release_frame(scic, frame_index);

		return result;
	}
	default:
		dev_dbg(sciphy_to_dev(sci_phy),
			"%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}
986

A
Adam Gruchala 已提交
987 988
}

989
static void scic_sds_phy_starting_initial_substate_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
990
{
E
Edmund Nadolski 已提交
991
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
D
Dan Williams 已提交
992 993

	/* This is just an temporary state go off to the starting state */
E
Edmund Nadolski 已提交
994
	sci_change_state(&sci_phy->sm, SCI_PHY_SUB_AWAIT_OSSP_EN);
D
Dan Williams 已提交
995 996
}

997
static void scic_sds_phy_starting_await_sas_power_substate_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
998
{
E
Edmund Nadolski 已提交
999
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
1000
	struct scic_sds_controller *scic = sci_phy->owning_port->owning_controller;
D
Dan Williams 已提交
1001

1002
	scic_sds_controller_power_control_queue_insert(scic, sci_phy);
D
Dan Williams 已提交
1003 1004
}

1005
static void scic_sds_phy_starting_await_sas_power_substate_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1006
{
E
Edmund Nadolski 已提交
1007
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
1008
	struct scic_sds_controller *scic = sci_phy->owning_port->owning_controller;
D
Dan Williams 已提交
1009

1010
	scic_sds_controller_power_control_queue_remove(scic, sci_phy);
D
Dan Williams 已提交
1011 1012
}

1013
static void scic_sds_phy_starting_await_sata_power_substate_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1014
{
E
Edmund Nadolski 已提交
1015
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
1016
	struct scic_sds_controller *scic = sci_phy->owning_port->owning_controller;
D
Dan Williams 已提交
1017

1018
	scic_sds_controller_power_control_queue_insert(scic, sci_phy);
D
Dan Williams 已提交
1019 1020
}

1021
static void scic_sds_phy_starting_await_sata_power_substate_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1022
{
E
Edmund Nadolski 已提交
1023
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
1024
	struct scic_sds_controller *scic = sci_phy->owning_port->owning_controller;
D
Dan Williams 已提交
1025

1026
	scic_sds_controller_power_control_queue_remove(scic, sci_phy);
D
Dan Williams 已提交
1027 1028
}

1029
static void scic_sds_phy_starting_await_sata_phy_substate_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1030
{
E
Edmund Nadolski 已提交
1031
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
D
Dan Williams 已提交
1032

1033
	sci_mod_timer(&sci_phy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
D
Dan Williams 已提交
1034 1035
}

1036
static void scic_sds_phy_starting_await_sata_phy_substate_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1037
{
E
Edmund Nadolski 已提交
1038
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
D
Dan Williams 已提交
1039

1040
	sci_del_timer(&sci_phy->sata_timer);
D
Dan Williams 已提交
1041 1042
}

1043
static void scic_sds_phy_starting_await_sata_speed_substate_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1044
{
E
Edmund Nadolski 已提交
1045
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
D
Dan Williams 已提交
1046

1047
	sci_mod_timer(&sci_phy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
D
Dan Williams 已提交
1048 1049
}

1050
static void scic_sds_phy_starting_await_sata_speed_substate_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1051
{
E
Edmund Nadolski 已提交
1052
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
D
Dan Williams 已提交
1053

1054
	sci_del_timer(&sci_phy->sata_timer);
D
Dan Williams 已提交
1055 1056
}

1057
static void scic_sds_phy_starting_await_sig_fis_uf_substate_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1058
{
E
Edmund Nadolski 已提交
1059
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
D
Dan Williams 已提交
1060

1061
	if (scic_sds_port_link_detected(sci_phy->owning_port, sci_phy)) {
D
Dan Williams 已提交
1062 1063 1064 1065 1066 1067 1068 1069 1070

		/*
		 * Clear the PE suspend condition so we can actually
		 * receive SIG FIS
		 * The hardware will not respond to the XRDY until the PE
		 * suspend condition is cleared.
		 */
		scic_sds_phy_resume(sci_phy);

1071 1072
		sci_mod_timer(&sci_phy->sata_timer,
			      SCIC_SDS_SIGNATURE_FIS_TIMEOUT);
D
Dan Williams 已提交
1073 1074 1075 1076
	} else
		sci_phy->is_in_link_training = false;
}

1077
static void scic_sds_phy_starting_await_sig_fis_uf_substate_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1078
{
E
Edmund Nadolski 已提交
1079
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
D
Dan Williams 已提交
1080

1081
	sci_del_timer(&sci_phy->sata_timer);
D
Dan Williams 已提交
1082 1083
}

1084
static void scic_sds_phy_starting_final_substate_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1085
{
E
Edmund Nadolski 已提交
1086
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
D
Dan Williams 已提交
1087 1088 1089 1090

	/* State machine has run to completion so exit out and change
	 * the base state machine to the ready state
	 */
E
Edmund Nadolski 已提交
1091
	sci_change_state(&sci_phy->sm, SCI_PHY_READY);
D
Dan Williams 已提交
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
}

/**
 *
 * @sci_phy: This is the struct scic_sds_phy object to stop.
 *
 * This method will stop the struct scic_sds_phy object. This does not reset the
 * protocol engine it just suspends it and places it in a state where it will
 * not cause the end device to power up. none
 */
static void scu_link_layer_stop_protocol_engine(
	struct scic_sds_phy *sci_phy)
{
	u32 scu_sas_pcfg_value;
	u32 enable_spinup_value;

	/* Suspend the protocol engine and place it in a sata spinup hold state */
	scu_sas_pcfg_value =
		readl(&sci_phy->link_layer_registers->phy_configuration);
	scu_sas_pcfg_value |=
		(SCU_SAS_PCFG_GEN_BIT(OOB_RESET) |
		 SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE) |
		 SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD));
	writel(scu_sas_pcfg_value,
	       &sci_phy->link_layer_registers->phy_configuration);

	/* Disable the notify enable spinup primitives */
	enable_spinup_value = readl(&sci_phy->link_layer_registers->notify_enable_spinup_control);
	enable_spinup_value &= ~SCU_ENSPINUP_GEN_BIT(ENABLE);
	writel(enable_spinup_value, &sci_phy->link_layer_registers->notify_enable_spinup_control);
}

/**
 *
 *
 * This method will start the OOB/SN state machine for this struct scic_sds_phy object.
 */
static void scu_link_layer_start_oob(
	struct scic_sds_phy *sci_phy)
{
	u32 scu_sas_pcfg_value;

	scu_sas_pcfg_value =
		readl(&sci_phy->link_layer_registers->phy_configuration);
	scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
	scu_sas_pcfg_value &=
		~(SCU_SAS_PCFG_GEN_BIT(OOB_RESET) |
		SCU_SAS_PCFG_GEN_BIT(HARD_RESET));
	writel(scu_sas_pcfg_value,
	       &sci_phy->link_layer_registers->phy_configuration);
}

/**
 *
 *
 * This method will transmit a hard reset request on the specified phy. The SCU
 * hardware requires that we reset the OOB state machine and set the hard reset
 * bit in the phy configuration register. We then must start OOB over with the
 * hard reset bit set.
 */
static void scu_link_layer_tx_hard_reset(
	struct scic_sds_phy *sci_phy)
{
	u32 phy_configuration_value;

	/*
	 * SAS Phys must wait for the HARD_RESET_TX event notification to transition
	 * to the starting state. */
	phy_configuration_value =
		readl(&sci_phy->link_layer_registers->phy_configuration);
	phy_configuration_value |=
		(SCU_SAS_PCFG_GEN_BIT(HARD_RESET) |
		 SCU_SAS_PCFG_GEN_BIT(OOB_RESET));
	writel(phy_configuration_value,
	       &sci_phy->link_layer_registers->phy_configuration);

	/* Now take the OOB state machine out of reset */
	phy_configuration_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
	phy_configuration_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
	writel(phy_configuration_value,
	       &sci_phy->link_layer_registers->phy_configuration);
}

1175
static void scic_sds_phy_stopped_state_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1176
{
E
Edmund Nadolski 已提交
1177
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
D
Dan Williams 已提交
1178 1179 1180 1181 1182

	/*
	 * @todo We need to get to the controller to place this PE in a
	 * reset state
	 */
1183
	sci_del_timer(&sci_phy->sata_timer);
D
Dan Williams 已提交
1184 1185 1186

	scu_link_layer_stop_protocol_engine(sci_phy);

E
Edmund Nadolski 已提交
1187
	if (sci_phy->sm.previous_state_id != SCI_PHY_INITIAL)
1188
		scic_sds_controller_link_down(scic_sds_phy_get_controller(sci_phy),
D
Dan Williams 已提交
1189
					      phy_get_non_dummy_port(sci_phy),
1190
					      sci_phy);
D
Dan Williams 已提交
1191 1192
}

1193
static void scic_sds_phy_starting_state_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1194
{
E
Edmund Nadolski 已提交
1195
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
D
Dan Williams 已提交
1196 1197 1198 1199 1200 1201 1202 1203

	scu_link_layer_stop_protocol_engine(sci_phy);
	scu_link_layer_start_oob(sci_phy);

	/* We don't know what kind of phy we are going to be just yet */
	sci_phy->protocol = SCIC_SDS_PHY_PROTOCOL_UNKNOWN;
	sci_phy->bcn_received_while_port_unassigned = false;

E
Edmund Nadolski 已提交
1204
	if (sci_phy->sm.previous_state_id == SCI_PHY_READY)
1205
		scic_sds_controller_link_down(scic_sds_phy_get_controller(sci_phy),
D
Dan Williams 已提交
1206
					      phy_get_non_dummy_port(sci_phy),
1207
					      sci_phy);
A
Adam Gruchala 已提交
1208

E
Edmund Nadolski 已提交
1209
	sci_change_state(&sci_phy->sm, SCI_PHY_SUB_INITIAL);
D
Dan Williams 已提交
1210 1211
}

1212
static void scic_sds_phy_ready_state_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1213
{
E
Edmund Nadolski 已提交
1214
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
D
Dan Williams 已提交
1215

1216
	scic_sds_controller_link_up(scic_sds_phy_get_controller(sci_phy),
D
Dan Williams 已提交
1217
				    phy_get_non_dummy_port(sci_phy),
1218
				    sci_phy);
D
Dan Williams 已提交
1219 1220 1221

}

1222
static void scic_sds_phy_ready_state_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1223
{
E
Edmund Nadolski 已提交
1224
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
D
Dan Williams 已提交
1225 1226 1227 1228

	scic_sds_phy_suspend(sci_phy);
}

1229
static void scic_sds_phy_resetting_state_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1230
{
E
Edmund Nadolski 已提交
1231
	struct scic_sds_phy *sci_phy = container_of(sm, typeof(*sci_phy), sm);
D
Dan Williams 已提交
1232

1233 1234 1235 1236
	/* The phy is being reset, therefore deactivate it from the port.  In
	 * the resetting state we don't notify the user regarding link up and
	 * link down notifications
	 */
D
Dan Williams 已提交
1237 1238 1239 1240 1241
	scic_sds_port_deactivate_phy(sci_phy->owning_port, sci_phy, false);

	if (sci_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
		scu_link_layer_tx_hard_reset(sci_phy);
	} else {
1242
		/* The SCU does not need to have a discrete reset state so
D
Dan Williams 已提交
1243 1244
		 * just go back to the starting state.
		 */
E
Edmund Nadolski 已提交
1245
		sci_change_state(&sci_phy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
1246 1247 1248 1249
	}
}

static const struct sci_base_state scic_sds_phy_state_table[] = {
E
Edmund Nadolski 已提交
1250 1251
	[SCI_PHY_INITIAL] = { },
	[SCI_PHY_STOPPED] = {
D
Dan Williams 已提交
1252 1253
		.enter_state = scic_sds_phy_stopped_state_enter,
	},
E
Edmund Nadolski 已提交
1254
	[SCI_PHY_STARTING] = {
D
Dan Williams 已提交
1255 1256
		.enter_state = scic_sds_phy_starting_state_enter,
	},
E
Edmund Nadolski 已提交
1257
	[SCI_PHY_SUB_INITIAL] = {
A
Adam Gruchala 已提交
1258 1259
		.enter_state = scic_sds_phy_starting_initial_substate_enter,
	},
E
Edmund Nadolski 已提交
1260 1261 1262 1263
	[SCI_PHY_SUB_AWAIT_OSSP_EN] = { },
	[SCI_PHY_SUB_AWAIT_SAS_SPEED_EN] = { },
	[SCI_PHY_SUB_AWAIT_IAF_UF] = { },
	[SCI_PHY_SUB_AWAIT_SAS_POWER] = {
A
Adam Gruchala 已提交
1264 1265 1266
		.enter_state = scic_sds_phy_starting_await_sas_power_substate_enter,
		.exit_state  = scic_sds_phy_starting_await_sas_power_substate_exit,
	},
E
Edmund Nadolski 已提交
1267
	[SCI_PHY_SUB_AWAIT_SATA_POWER] = {
A
Adam Gruchala 已提交
1268 1269 1270
		.enter_state = scic_sds_phy_starting_await_sata_power_substate_enter,
		.exit_state  = scic_sds_phy_starting_await_sata_power_substate_exit
	},
E
Edmund Nadolski 已提交
1271
	[SCI_PHY_SUB_AWAIT_SATA_PHY_EN] = {
A
Adam Gruchala 已提交
1272 1273 1274
		.enter_state = scic_sds_phy_starting_await_sata_phy_substate_enter,
		.exit_state  = scic_sds_phy_starting_await_sata_phy_substate_exit
	},
E
Edmund Nadolski 已提交
1275
	[SCI_PHY_SUB_AWAIT_SATA_SPEED_EN] = {
A
Adam Gruchala 已提交
1276 1277 1278
		.enter_state = scic_sds_phy_starting_await_sata_speed_substate_enter,
		.exit_state  = scic_sds_phy_starting_await_sata_speed_substate_exit
	},
E
Edmund Nadolski 已提交
1279
	[SCI_PHY_SUB_AWAIT_SIG_FIS_UF] = {
A
Adam Gruchala 已提交
1280 1281 1282
		.enter_state = scic_sds_phy_starting_await_sig_fis_uf_substate_enter,
		.exit_state  = scic_sds_phy_starting_await_sig_fis_uf_substate_exit
	},
E
Edmund Nadolski 已提交
1283
	[SCI_PHY_SUB_FINAL] = {
A
Adam Gruchala 已提交
1284 1285
		.enter_state = scic_sds_phy_starting_final_substate_enter,
	},
E
Edmund Nadolski 已提交
1286
	[SCI_PHY_READY] = {
D
Dan Williams 已提交
1287 1288 1289
		.enter_state = scic_sds_phy_ready_state_enter,
		.exit_state = scic_sds_phy_ready_state_exit,
	},
E
Edmund Nadolski 已提交
1290
	[SCI_PHY_RESETTING] = {
D
Dan Williams 已提交
1291 1292
		.enter_state = scic_sds_phy_resetting_state_enter,
	},
E
Edmund Nadolski 已提交
1293
	[SCI_PHY_FINAL] = { },
D
Dan Williams 已提交
1294 1295 1296 1297 1298
};

void scic_sds_phy_construct(struct scic_sds_phy *sci_phy,
			    struct scic_sds_port *owning_port, u8 phy_index)
{
1299
	sci_init_sm(&sci_phy->sm, scic_sds_phy_state_table, SCI_PHY_INITIAL);
D
Dan Williams 已提交
1300 1301 1302 1303 1304 1305 1306 1307

	/* Copy the rest of the input data to our locals */
	sci_phy->owning_port = owning_port;
	sci_phy->phy_index = phy_index;
	sci_phy->bcn_received_while_port_unassigned = false;
	sci_phy->protocol = SCIC_SDS_PHY_PROTOCOL_UNKNOWN;
	sci_phy->link_layer_registers = NULL;
	sci_phy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
1308 1309 1310

	/* Create the SIGNATURE FIS Timeout timer for this phy */
	sci_init_timer(&sci_phy->sata_timer, phy_sata_timeout);
D
Dan Williams 已提交
1311
}
1312

D
Dan Williams 已提交
1313
void isci_phy_init(struct isci_phy *iphy, struct isci_host *ihost, int index)
1314
{
D
Dan Williams 已提交
1315
	union scic_oem_parameters oem;
D
Dan Williams 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
	u64 sci_sas_addr;
	__be64 sas_addr;

	scic_oem_parameters_get(&ihost->sci, &oem);
	sci_sas_addr = oem.sds1.phys[index].sas_address.high;
	sci_sas_addr <<= 32;
	sci_sas_addr |= oem.sds1.phys[index].sas_address.low;
	sas_addr = cpu_to_be64(sci_sas_addr);
	memcpy(iphy->sas_addr, &sas_addr, sizeof(sas_addr));

	iphy->isci_port = NULL;
	iphy->sas_phy.enabled = 0;
	iphy->sas_phy.id = index;
	iphy->sas_phy.sas_addr = &iphy->sas_addr[0];
	iphy->sas_phy.frame_rcvd = (u8 *)&iphy->frame_rcvd;
	iphy->sas_phy.ha = &ihost->sas_ha;
	iphy->sas_phy.lldd_phy = iphy;
	iphy->sas_phy.enabled = 1;
	iphy->sas_phy.class = SAS;
	iphy->sas_phy.iproto = SAS_PROTOCOL_ALL;
	iphy->sas_phy.tproto = 0;
	iphy->sas_phy.type = PHY_TYPE_PHYSICAL;
	iphy->sas_phy.role = PHY_ROLE_INITIATOR;
	iphy->sas_phy.oob_mode = OOB_NOT_CONNECTED;
	iphy->sas_phy.linkrate = SAS_LINK_RATE_UNKNOWN;
	memset(&iphy->frame_rcvd, 0, sizeof(iphy->frame_rcvd));
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
}


/**
 * isci_phy_control() - This function is one of the SAS Domain Template
 *    functions. This is a phy management function.
 * @phy: This parameter specifies the sphy being controlled.
 * @func: This parameter specifies the phy control function being invoked.
 * @buf: This parameter is specific to the phy function being invoked.
 *
 * status, zero indicates success.
 */
1354 1355 1356
int isci_phy_control(struct asd_sas_phy *sas_phy,
		     enum phy_func func,
		     void *buf)
1357
{
1358 1359 1360 1361 1362
	int ret = 0;
	struct isci_phy *iphy = sas_phy->lldd_phy;
	struct isci_port *iport = iphy->isci_port;
	struct isci_host *ihost = sas_phy->ha->lldd_ha;
	unsigned long flags;
1363

1364 1365 1366
	dev_dbg(&ihost->pdev->dev,
		"%s: phy %p; func %d; buf %p; isci phy %p, port %p\n",
		__func__, sas_phy, func, buf, iphy, iport);
1367 1368

	switch (func) {
1369 1370
	case PHY_FUNC_DISABLE:
		spin_lock_irqsave(&ihost->scic_lock, flags);
D
Dan Williams 已提交
1371
		scic_sds_phy_stop(&iphy->sci);
1372 1373 1374
		spin_unlock_irqrestore(&ihost->scic_lock, flags);
		break;

1375
	case PHY_FUNC_LINK_RESET:
1376
		spin_lock_irqsave(&ihost->scic_lock, flags);
D
Dan Williams 已提交
1377 1378
		scic_sds_phy_stop(&iphy->sci);
		scic_sds_phy_start(&iphy->sci);
1379 1380 1381 1382 1383 1384
		spin_unlock_irqrestore(&ihost->scic_lock, flags);
		break;

	case PHY_FUNC_HARD_RESET:
		if (!iport)
			return -ENODEV;
1385 1386

		/* Perform the port reset. */
1387
		ret = isci_port_perform_hard_reset(ihost, iport, iphy);
1388 1389 1390 1391

		break;

	default:
1392 1393 1394 1395
		dev_dbg(&ihost->pdev->dev,
			   "%s: phy %p; func %d NOT IMPLEMENTED!\n",
			   __func__, sas_phy, func);
		ret = -ENOSYS;
1396 1397 1398 1399
		break;
	}
	return ret;
}