162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license.  When using or
362306a36Sopenharmony_ci * redistributing this file, you may do so under either license.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * GPL LICENSE SUMMARY
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
1062306a36Sopenharmony_ci * it under the terms of version 2 of the GNU General Public License as
1162306a36Sopenharmony_ci * published by the Free Software Foundation.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * This program is distributed in the hope that it will be useful, but
1462306a36Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
1562306a36Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1662306a36Sopenharmony_ci * General Public License for more details.
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci * You should have received a copy of the GNU General Public License
1962306a36Sopenharmony_ci * along with this program; if not, write to the Free Software
2062306a36Sopenharmony_ci * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
2162306a36Sopenharmony_ci * The full GNU General Public License is included in this distribution
2262306a36Sopenharmony_ci * in the file called LICENSE.GPL.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * BSD LICENSE
2562306a36Sopenharmony_ci *
2662306a36Sopenharmony_ci * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
2762306a36Sopenharmony_ci * All rights reserved.
2862306a36Sopenharmony_ci *
2962306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
3062306a36Sopenharmony_ci * modification, are permitted provided that the following conditions
3162306a36Sopenharmony_ci * are met:
3262306a36Sopenharmony_ci *
3362306a36Sopenharmony_ci *   * Redistributions of source code must retain the above copyright
3462306a36Sopenharmony_ci *     notice, this list of conditions and the following disclaimer.
3562306a36Sopenharmony_ci *   * Redistributions in binary form must reproduce the above copyright
3662306a36Sopenharmony_ci *     notice, this list of conditions and the following disclaimer in
3762306a36Sopenharmony_ci *     the documentation and/or other materials provided with the
3862306a36Sopenharmony_ci *     distribution.
3962306a36Sopenharmony_ci *   * Neither the name of Intel Corporation nor the names of its
4062306a36Sopenharmony_ci *     contributors may be used to endorse or promote products derived
4162306a36Sopenharmony_ci *     from this software without specific prior written permission.
4262306a36Sopenharmony_ci *
4362306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4462306a36Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
4562306a36Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
4662306a36Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
4762306a36Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4862306a36Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
4962306a36Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5062306a36Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5162306a36Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5262306a36Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
5362306a36Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5462306a36Sopenharmony_ci */
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci#include "isci.h"
5762306a36Sopenharmony_ci#include "host.h"
5862306a36Sopenharmony_ci#include "phy.h"
5962306a36Sopenharmony_ci#include "scu_event_codes.h"
6062306a36Sopenharmony_ci#include "probe_roms.h"
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci#undef C
6362306a36Sopenharmony_ci#define C(a) (#a)
6462306a36Sopenharmony_cistatic const char *phy_state_name(enum sci_phy_states state)
6562306a36Sopenharmony_ci{
6662306a36Sopenharmony_ci	static const char * const strings[] = PHY_STATES;
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	return strings[state];
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_ci#undef C
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci/* Maximum arbitration wait time in micro-seconds */
7362306a36Sopenharmony_ci#define SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME  (700)
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_cienum sas_linkrate sci_phy_linkrate(struct isci_phy *iphy)
7662306a36Sopenharmony_ci{
7762306a36Sopenharmony_ci	return iphy->max_negotiated_speed;
7862306a36Sopenharmony_ci}
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_cistatic struct isci_host *phy_to_host(struct isci_phy *iphy)
8162306a36Sopenharmony_ci{
8262306a36Sopenharmony_ci	struct isci_phy *table = iphy - iphy->phy_index;
8362306a36Sopenharmony_ci	struct isci_host *ihost = container_of(table, typeof(*ihost), phys[0]);
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	return ihost;
8662306a36Sopenharmony_ci}
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_cistatic struct device *sciphy_to_dev(struct isci_phy *iphy)
8962306a36Sopenharmony_ci{
9062306a36Sopenharmony_ci	return &phy_to_host(iphy)->pdev->dev;
9162306a36Sopenharmony_ci}
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_cistatic enum sci_status
9462306a36Sopenharmony_cisci_phy_transport_layer_initialization(struct isci_phy *iphy,
9562306a36Sopenharmony_ci				       struct scu_transport_layer_registers __iomem *reg)
9662306a36Sopenharmony_ci{
9762306a36Sopenharmony_ci	u32 tl_control;
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci	iphy->transport_layer_registers = reg;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	writel(SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX,
10262306a36Sopenharmony_ci		&iphy->transport_layer_registers->stp_rni);
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	/*
10562306a36Sopenharmony_ci	 * Hardware team recommends that we enable the STP prefetch for all
10662306a36Sopenharmony_ci	 * transports
10762306a36Sopenharmony_ci	 */
10862306a36Sopenharmony_ci	tl_control = readl(&iphy->transport_layer_registers->control);
10962306a36Sopenharmony_ci	tl_control |= SCU_TLCR_GEN_BIT(STP_WRITE_DATA_PREFETCH);
11062306a36Sopenharmony_ci	writel(tl_control, &iphy->transport_layer_registers->control);
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	return SCI_SUCCESS;
11362306a36Sopenharmony_ci}
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_cistatic enum sci_status
11662306a36Sopenharmony_cisci_phy_link_layer_initialization(struct isci_phy *iphy,
11762306a36Sopenharmony_ci				  struct scu_link_layer_registers __iomem *llr)
11862306a36Sopenharmony_ci{
11962306a36Sopenharmony_ci	struct isci_host *ihost = iphy->owning_port->owning_controller;
12062306a36Sopenharmony_ci	struct sci_phy_user_params *phy_user;
12162306a36Sopenharmony_ci	struct sci_phy_oem_params *phy_oem;
12262306a36Sopenharmony_ci	int phy_idx = iphy->phy_index;
12362306a36Sopenharmony_ci	struct sci_phy_cap phy_cap;
12462306a36Sopenharmony_ci	u32 phy_configuration;
12562306a36Sopenharmony_ci	u32 parity_check = 0;
12662306a36Sopenharmony_ci	u32 parity_count = 0;
12762306a36Sopenharmony_ci	u32 llctl, link_rate;
12862306a36Sopenharmony_ci	u32 clksm_value = 0;
12962306a36Sopenharmony_ci	u32 sp_timeouts = 0;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	phy_user = &ihost->user_parameters.phys[phy_idx];
13262306a36Sopenharmony_ci	phy_oem = &ihost->oem_parameters.phys[phy_idx];
13362306a36Sopenharmony_ci	iphy->link_layer_registers = llr;
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	/* Set our IDENTIFY frame data */
13662306a36Sopenharmony_ci	#define SCI_END_DEVICE 0x01
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	writel(SCU_SAS_TIID_GEN_BIT(SMP_INITIATOR) |
13962306a36Sopenharmony_ci	       SCU_SAS_TIID_GEN_BIT(SSP_INITIATOR) |
14062306a36Sopenharmony_ci	       SCU_SAS_TIID_GEN_BIT(STP_INITIATOR) |
14162306a36Sopenharmony_ci	       SCU_SAS_TIID_GEN_BIT(DA_SATA_HOST) |
14262306a36Sopenharmony_ci	       SCU_SAS_TIID_GEN_VAL(DEVICE_TYPE, SCI_END_DEVICE),
14362306a36Sopenharmony_ci	       &llr->transmit_identification);
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	/* Write the device SAS Address */
14662306a36Sopenharmony_ci	writel(0xFEDCBA98, &llr->sas_device_name_high);
14762306a36Sopenharmony_ci	writel(phy_idx, &llr->sas_device_name_low);
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	/* Write the source SAS Address */
15062306a36Sopenharmony_ci	writel(phy_oem->sas_address.high, &llr->source_sas_address_high);
15162306a36Sopenharmony_ci	writel(phy_oem->sas_address.low, &llr->source_sas_address_low);
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	/* Clear and Set the PHY Identifier */
15462306a36Sopenharmony_ci	writel(0, &llr->identify_frame_phy_id);
15562306a36Sopenharmony_ci	writel(SCU_SAS_TIPID_GEN_VALUE(ID, phy_idx), &llr->identify_frame_phy_id);
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	/* Change the initial state of the phy configuration register */
15862306a36Sopenharmony_ci	phy_configuration = readl(&llr->phy_configuration);
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	/* Hold OOB state machine in reset */
16162306a36Sopenharmony_ci	phy_configuration |=  SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
16262306a36Sopenharmony_ci	writel(phy_configuration, &llr->phy_configuration);
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	/* Configure the SNW capabilities */
16562306a36Sopenharmony_ci	phy_cap.all = 0;
16662306a36Sopenharmony_ci	phy_cap.start = 1;
16762306a36Sopenharmony_ci	phy_cap.gen3_no_ssc = 1;
16862306a36Sopenharmony_ci	phy_cap.gen2_no_ssc = 1;
16962306a36Sopenharmony_ci	phy_cap.gen1_no_ssc = 1;
17062306a36Sopenharmony_ci	if (ihost->oem_parameters.controller.do_enable_ssc) {
17162306a36Sopenharmony_ci		struct scu_afe_registers __iomem *afe = &ihost->scu_registers->afe;
17262306a36Sopenharmony_ci		struct scu_afe_transceiver __iomem *xcvr = &afe->scu_afe_xcvr[phy_idx];
17362306a36Sopenharmony_ci		struct isci_pci_info *pci_info = to_pci_info(ihost->pdev);
17462306a36Sopenharmony_ci		bool en_sas = false;
17562306a36Sopenharmony_ci		bool en_sata = false;
17662306a36Sopenharmony_ci		u32 sas_type = 0;
17762306a36Sopenharmony_ci		u32 sata_spread = 0x2;
17862306a36Sopenharmony_ci		u32 sas_spread = 0x2;
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci		phy_cap.gen3_ssc = 1;
18162306a36Sopenharmony_ci		phy_cap.gen2_ssc = 1;
18262306a36Sopenharmony_ci		phy_cap.gen1_ssc = 1;
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci		if (pci_info->orom->hdr.version < ISCI_ROM_VER_1_1)
18562306a36Sopenharmony_ci			en_sas = en_sata = true;
18662306a36Sopenharmony_ci		else {
18762306a36Sopenharmony_ci			sata_spread = ihost->oem_parameters.controller.ssc_sata_tx_spread_level;
18862306a36Sopenharmony_ci			sas_spread = ihost->oem_parameters.controller.ssc_sas_tx_spread_level;
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci			if (sata_spread)
19162306a36Sopenharmony_ci				en_sata = true;
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci			if (sas_spread) {
19462306a36Sopenharmony_ci				en_sas = true;
19562306a36Sopenharmony_ci				sas_type = ihost->oem_parameters.controller.ssc_sas_tx_type;
19662306a36Sopenharmony_ci			}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci		}
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci		if (en_sas) {
20162306a36Sopenharmony_ci			u32 reg;
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci			reg = readl(&xcvr->afe_xcvr_control0);
20462306a36Sopenharmony_ci			reg |= (0x00100000 | (sas_type << 19));
20562306a36Sopenharmony_ci			writel(reg, &xcvr->afe_xcvr_control0);
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci			reg = readl(&xcvr->afe_tx_ssc_control);
20862306a36Sopenharmony_ci			reg |= sas_spread << 8;
20962306a36Sopenharmony_ci			writel(reg, &xcvr->afe_tx_ssc_control);
21062306a36Sopenharmony_ci		}
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci		if (en_sata) {
21362306a36Sopenharmony_ci			u32 reg;
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci			reg = readl(&xcvr->afe_tx_ssc_control);
21662306a36Sopenharmony_ci			reg |= sata_spread;
21762306a36Sopenharmony_ci			writel(reg, &xcvr->afe_tx_ssc_control);
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci			reg = readl(&llr->stp_control);
22062306a36Sopenharmony_ci			reg |= 1 << 12;
22162306a36Sopenharmony_ci			writel(reg, &llr->stp_control);
22262306a36Sopenharmony_ci		}
22362306a36Sopenharmony_ci	}
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	/* The SAS specification indicates that the phy_capabilities that
22662306a36Sopenharmony_ci	 * are transmitted shall have an even parity.  Calculate the parity.
22762306a36Sopenharmony_ci	 */
22862306a36Sopenharmony_ci	parity_check = phy_cap.all;
22962306a36Sopenharmony_ci	while (parity_check != 0) {
23062306a36Sopenharmony_ci		if (parity_check & 0x1)
23162306a36Sopenharmony_ci			parity_count++;
23262306a36Sopenharmony_ci		parity_check >>= 1;
23362306a36Sopenharmony_ci	}
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci	/* If parity indicates there are an odd number of bits set, then
23662306a36Sopenharmony_ci	 * set the parity bit to 1 in the phy capabilities.
23762306a36Sopenharmony_ci	 */
23862306a36Sopenharmony_ci	if ((parity_count % 2) != 0)
23962306a36Sopenharmony_ci		phy_cap.parity = 1;
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci	writel(phy_cap.all, &llr->phy_capabilities);
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci	/* Set the enable spinup period but disable the ability to send
24462306a36Sopenharmony_ci	 * notify enable spinup
24562306a36Sopenharmony_ci	 */
24662306a36Sopenharmony_ci	writel(SCU_ENSPINUP_GEN_VAL(COUNT,
24762306a36Sopenharmony_ci			phy_user->notify_enable_spin_up_insertion_frequency),
24862306a36Sopenharmony_ci		&llr->notify_enable_spinup_control);
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	/* Write the ALIGN Insertion Ferequency for connected phy and
25162306a36Sopenharmony_ci	 * inpendent of connected state
25262306a36Sopenharmony_ci	 */
25362306a36Sopenharmony_ci	clksm_value = SCU_ALIGN_INSERTION_FREQUENCY_GEN_VAL(CONNECTED,
25462306a36Sopenharmony_ci			phy_user->in_connection_align_insertion_frequency);
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci	clksm_value |= SCU_ALIGN_INSERTION_FREQUENCY_GEN_VAL(GENERAL,
25762306a36Sopenharmony_ci			phy_user->align_insertion_frequency);
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci	writel(clksm_value, &llr->clock_skew_management);
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci	if (is_c0(ihost->pdev) || is_c1(ihost->pdev)) {
26262306a36Sopenharmony_ci		writel(0x04210400, &llr->afe_lookup_table_control);
26362306a36Sopenharmony_ci		writel(0x020A7C05, &llr->sas_primitive_timeout);
26462306a36Sopenharmony_ci	} else
26562306a36Sopenharmony_ci		writel(0x02108421, &llr->afe_lookup_table_control);
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci	llctl = SCU_SAS_LLCTL_GEN_VAL(NO_OUTBOUND_TASK_TIMEOUT,
26862306a36Sopenharmony_ci		(u8)ihost->user_parameters.no_outbound_task_timeout);
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	switch (phy_user->max_speed_generation) {
27162306a36Sopenharmony_ci	case SCIC_SDS_PARM_GEN3_SPEED:
27262306a36Sopenharmony_ci		link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN3;
27362306a36Sopenharmony_ci		break;
27462306a36Sopenharmony_ci	case SCIC_SDS_PARM_GEN2_SPEED:
27562306a36Sopenharmony_ci		link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN2;
27662306a36Sopenharmony_ci		break;
27762306a36Sopenharmony_ci	default:
27862306a36Sopenharmony_ci		link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN1;
27962306a36Sopenharmony_ci		break;
28062306a36Sopenharmony_ci	}
28162306a36Sopenharmony_ci	llctl |= SCU_SAS_LLCTL_GEN_VAL(MAX_LINK_RATE, link_rate);
28262306a36Sopenharmony_ci	writel(llctl, &llr->link_layer_control);
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci	sp_timeouts = readl(&llr->sas_phy_timeouts);
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci	/* Clear the default 0x36 (54us) RATE_CHANGE timeout value. */
28762306a36Sopenharmony_ci	sp_timeouts &= ~SCU_SAS_PHYTOV_GEN_VAL(RATE_CHANGE, 0xFF);
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci	/* Set RATE_CHANGE timeout value to 0x3B (59us).  This ensures SCU can
29062306a36Sopenharmony_ci	 * lock with 3Gb drive when SCU max rate is set to 1.5Gb.
29162306a36Sopenharmony_ci	 */
29262306a36Sopenharmony_ci	sp_timeouts |= SCU_SAS_PHYTOV_GEN_VAL(RATE_CHANGE, 0x3B);
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ci	writel(sp_timeouts, &llr->sas_phy_timeouts);
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci	if (is_a2(ihost->pdev)) {
29762306a36Sopenharmony_ci		/* Program the max ARB time for the PHY to 700us so we
29862306a36Sopenharmony_ci		 * inter-operate with the PMC expander which shuts down
29962306a36Sopenharmony_ci		 * PHYs if the expander PHY generates too many breaks.
30062306a36Sopenharmony_ci		 * This time value will guarantee that the initiator PHY
30162306a36Sopenharmony_ci		 * will generate the break.
30262306a36Sopenharmony_ci		 */
30362306a36Sopenharmony_ci		writel(SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME,
30462306a36Sopenharmony_ci		       &llr->maximum_arbitration_wait_timer_timeout);
30562306a36Sopenharmony_ci	}
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci	/* Disable link layer hang detection, rely on the OS timeout for
30862306a36Sopenharmony_ci	 * I/O timeouts.
30962306a36Sopenharmony_ci	 */
31062306a36Sopenharmony_ci	writel(0, &llr->link_layer_hang_detection_timeout);
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci	/* We can exit the initial state to the stopped state */
31362306a36Sopenharmony_ci	sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci	return SCI_SUCCESS;
31662306a36Sopenharmony_ci}
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_cistatic void phy_sata_timeout(struct timer_list *t)
31962306a36Sopenharmony_ci{
32062306a36Sopenharmony_ci	struct sci_timer *tmr = from_timer(tmr, t, timer);
32162306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(tmr, typeof(*iphy), sata_timer);
32262306a36Sopenharmony_ci	struct isci_host *ihost = iphy->owning_port->owning_controller;
32362306a36Sopenharmony_ci	unsigned long flags;
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci	spin_lock_irqsave(&ihost->scic_lock, flags);
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci	if (tmr->cancel)
32862306a36Sopenharmony_ci		goto done;
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci	dev_dbg(sciphy_to_dev(iphy),
33162306a36Sopenharmony_ci		 "%s: SCIC SDS Phy 0x%p did not receive signature fis before "
33262306a36Sopenharmony_ci		 "timeout.\n",
33362306a36Sopenharmony_ci		 __func__,
33462306a36Sopenharmony_ci		 iphy);
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci	sci_change_state(&iphy->sm, SCI_PHY_STARTING);
33762306a36Sopenharmony_cidone:
33862306a36Sopenharmony_ci	spin_unlock_irqrestore(&ihost->scic_lock, flags);
33962306a36Sopenharmony_ci}
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci/**
34262306a36Sopenharmony_ci * phy_get_non_dummy_port() - This method returns the port currently containing
34362306a36Sopenharmony_ci * this phy. If the phy is currently contained by the dummy port, then the phy
34462306a36Sopenharmony_ci * is considered to not be part of a port.
34562306a36Sopenharmony_ci *
34662306a36Sopenharmony_ci * @iphy: This parameter specifies the phy for which to retrieve the
34762306a36Sopenharmony_ci *    containing port.
34862306a36Sopenharmony_ci *
34962306a36Sopenharmony_ci * This method returns a handle to a port that contains the supplied phy.
35062306a36Sopenharmony_ci * NULL This value is returned if the phy is not part of a real
35162306a36Sopenharmony_ci * port (i.e. it's contained in the dummy port). !NULL All other
35262306a36Sopenharmony_ci * values indicate a handle/pointer to the port containing the phy.
35362306a36Sopenharmony_ci */
35462306a36Sopenharmony_cistruct isci_port *phy_get_non_dummy_port(struct isci_phy *iphy)
35562306a36Sopenharmony_ci{
35662306a36Sopenharmony_ci	struct isci_port *iport = iphy->owning_port;
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci	if (iport->physical_port_index == SCIC_SDS_DUMMY_PORT)
35962306a36Sopenharmony_ci		return NULL;
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_ci	return iphy->owning_port;
36262306a36Sopenharmony_ci}
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_ci/*
36562306a36Sopenharmony_ci * sci_phy_set_port() - This method will assign a port to the phy object.
36662306a36Sopenharmony_ci */
36762306a36Sopenharmony_civoid sci_phy_set_port(
36862306a36Sopenharmony_ci	struct isci_phy *iphy,
36962306a36Sopenharmony_ci	struct isci_port *iport)
37062306a36Sopenharmony_ci{
37162306a36Sopenharmony_ci	iphy->owning_port = iport;
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci	if (iphy->bcn_received_while_port_unassigned) {
37462306a36Sopenharmony_ci		iphy->bcn_received_while_port_unassigned = false;
37562306a36Sopenharmony_ci		sci_port_broadcast_change_received(iphy->owning_port, iphy);
37662306a36Sopenharmony_ci	}
37762306a36Sopenharmony_ci}
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_cienum sci_status sci_phy_initialize(struct isci_phy *iphy,
38062306a36Sopenharmony_ci				   struct scu_transport_layer_registers __iomem *tl,
38162306a36Sopenharmony_ci				   struct scu_link_layer_registers __iomem *ll)
38262306a36Sopenharmony_ci{
38362306a36Sopenharmony_ci	/* Perfrom the initialization of the TL hardware */
38462306a36Sopenharmony_ci	sci_phy_transport_layer_initialization(iphy, tl);
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	/* Perofrm the initialization of the PE hardware */
38762306a36Sopenharmony_ci	sci_phy_link_layer_initialization(iphy, ll);
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci	/* There is nothing that needs to be done in this state just
39062306a36Sopenharmony_ci	 * transition to the stopped state
39162306a36Sopenharmony_ci	 */
39262306a36Sopenharmony_ci	sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci	return SCI_SUCCESS;
39562306a36Sopenharmony_ci}
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ci/**
39862306a36Sopenharmony_ci * sci_phy_setup_transport() - This method assigns the direct attached device ID for this phy.
39962306a36Sopenharmony_ci *
40062306a36Sopenharmony_ci * @iphy: The phy for which the direct attached device id is to
40162306a36Sopenharmony_ci *       be assigned.
40262306a36Sopenharmony_ci * @device_id: The direct attached device ID to assign to the phy.
40362306a36Sopenharmony_ci *       This will either be the RNi for the device or an invalid RNi if there
40462306a36Sopenharmony_ci *       is no current device assigned to the phy.
40562306a36Sopenharmony_ci */
40662306a36Sopenharmony_civoid sci_phy_setup_transport(struct isci_phy *iphy, u32 device_id)
40762306a36Sopenharmony_ci{
40862306a36Sopenharmony_ci	u32 tl_control;
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci	writel(device_id, &iphy->transport_layer_registers->stp_rni);
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci	/*
41362306a36Sopenharmony_ci	 * The read should guarantee that the first write gets posted
41462306a36Sopenharmony_ci	 * before the next write
41562306a36Sopenharmony_ci	 */
41662306a36Sopenharmony_ci	tl_control = readl(&iphy->transport_layer_registers->control);
41762306a36Sopenharmony_ci	tl_control |= SCU_TLCR_GEN_BIT(CLEAR_TCI_NCQ_MAPPING_TABLE);
41862306a36Sopenharmony_ci	writel(tl_control, &iphy->transport_layer_registers->control);
41962306a36Sopenharmony_ci}
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_cistatic void sci_phy_suspend(struct isci_phy *iphy)
42262306a36Sopenharmony_ci{
42362306a36Sopenharmony_ci	u32 scu_sas_pcfg_value;
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci	scu_sas_pcfg_value =
42662306a36Sopenharmony_ci		readl(&iphy->link_layer_registers->phy_configuration);
42762306a36Sopenharmony_ci	scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
42862306a36Sopenharmony_ci	writel(scu_sas_pcfg_value,
42962306a36Sopenharmony_ci		&iphy->link_layer_registers->phy_configuration);
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_ci	sci_phy_setup_transport(iphy, SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
43262306a36Sopenharmony_ci}
43362306a36Sopenharmony_ci
43462306a36Sopenharmony_civoid sci_phy_resume(struct isci_phy *iphy)
43562306a36Sopenharmony_ci{
43662306a36Sopenharmony_ci	u32 scu_sas_pcfg_value;
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci	scu_sas_pcfg_value =
43962306a36Sopenharmony_ci		readl(&iphy->link_layer_registers->phy_configuration);
44062306a36Sopenharmony_ci	scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
44162306a36Sopenharmony_ci	writel(scu_sas_pcfg_value,
44262306a36Sopenharmony_ci		&iphy->link_layer_registers->phy_configuration);
44362306a36Sopenharmony_ci}
44462306a36Sopenharmony_ci
44562306a36Sopenharmony_civoid sci_phy_get_sas_address(struct isci_phy *iphy, struct sci_sas_address *sas)
44662306a36Sopenharmony_ci{
44762306a36Sopenharmony_ci	sas->high = readl(&iphy->link_layer_registers->source_sas_address_high);
44862306a36Sopenharmony_ci	sas->low = readl(&iphy->link_layer_registers->source_sas_address_low);
44962306a36Sopenharmony_ci}
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_civoid sci_phy_get_attached_sas_address(struct isci_phy *iphy, struct sci_sas_address *sas)
45262306a36Sopenharmony_ci{
45362306a36Sopenharmony_ci	struct sas_identify_frame *iaf;
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_ci	iaf = &iphy->frame_rcvd.iaf;
45662306a36Sopenharmony_ci	memcpy(sas, iaf->sas_addr, SAS_ADDR_SIZE);
45762306a36Sopenharmony_ci}
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_civoid sci_phy_get_protocols(struct isci_phy *iphy, struct sci_phy_proto *proto)
46062306a36Sopenharmony_ci{
46162306a36Sopenharmony_ci	proto->all = readl(&iphy->link_layer_registers->transmit_identification);
46262306a36Sopenharmony_ci}
46362306a36Sopenharmony_ci
46462306a36Sopenharmony_cienum sci_status sci_phy_start(struct isci_phy *iphy)
46562306a36Sopenharmony_ci{
46662306a36Sopenharmony_ci	enum sci_phy_states state = iphy->sm.current_state_id;
46762306a36Sopenharmony_ci
46862306a36Sopenharmony_ci	if (state != SCI_PHY_STOPPED) {
46962306a36Sopenharmony_ci		dev_dbg(sciphy_to_dev(iphy), "%s: in wrong state: %s\n",
47062306a36Sopenharmony_ci			__func__, phy_state_name(state));
47162306a36Sopenharmony_ci		return SCI_FAILURE_INVALID_STATE;
47262306a36Sopenharmony_ci	}
47362306a36Sopenharmony_ci
47462306a36Sopenharmony_ci	sci_change_state(&iphy->sm, SCI_PHY_STARTING);
47562306a36Sopenharmony_ci	return SCI_SUCCESS;
47662306a36Sopenharmony_ci}
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_cienum sci_status sci_phy_stop(struct isci_phy *iphy)
47962306a36Sopenharmony_ci{
48062306a36Sopenharmony_ci	enum sci_phy_states state = iphy->sm.current_state_id;
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_ci	switch (state) {
48362306a36Sopenharmony_ci	case SCI_PHY_SUB_INITIAL:
48462306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_OSSP_EN:
48562306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
48662306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_SAS_POWER:
48762306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_SATA_POWER:
48862306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
48962306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
49062306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
49162306a36Sopenharmony_ci	case SCI_PHY_SUB_FINAL:
49262306a36Sopenharmony_ci	case SCI_PHY_READY:
49362306a36Sopenharmony_ci		break;
49462306a36Sopenharmony_ci	default:
49562306a36Sopenharmony_ci		dev_dbg(sciphy_to_dev(iphy), "%s: in wrong state: %s\n",
49662306a36Sopenharmony_ci			__func__, phy_state_name(state));
49762306a36Sopenharmony_ci		return SCI_FAILURE_INVALID_STATE;
49862306a36Sopenharmony_ci	}
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_ci	sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
50162306a36Sopenharmony_ci	return SCI_SUCCESS;
50262306a36Sopenharmony_ci}
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_cienum sci_status sci_phy_reset(struct isci_phy *iphy)
50562306a36Sopenharmony_ci{
50662306a36Sopenharmony_ci	enum sci_phy_states state = iphy->sm.current_state_id;
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ci	if (state != SCI_PHY_READY) {
50962306a36Sopenharmony_ci		dev_dbg(sciphy_to_dev(iphy), "%s: in wrong state: %s\n",
51062306a36Sopenharmony_ci			__func__, phy_state_name(state));
51162306a36Sopenharmony_ci		return SCI_FAILURE_INVALID_STATE;
51262306a36Sopenharmony_ci	}
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_ci	sci_change_state(&iphy->sm, SCI_PHY_RESETTING);
51562306a36Sopenharmony_ci	return SCI_SUCCESS;
51662306a36Sopenharmony_ci}
51762306a36Sopenharmony_ci
51862306a36Sopenharmony_cienum sci_status sci_phy_consume_power_handler(struct isci_phy *iphy)
51962306a36Sopenharmony_ci{
52062306a36Sopenharmony_ci	enum sci_phy_states state = iphy->sm.current_state_id;
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci	switch (state) {
52362306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_SAS_POWER: {
52462306a36Sopenharmony_ci		u32 enable_spinup;
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ci		enable_spinup = readl(&iphy->link_layer_registers->notify_enable_spinup_control);
52762306a36Sopenharmony_ci		enable_spinup |= SCU_ENSPINUP_GEN_BIT(ENABLE);
52862306a36Sopenharmony_ci		writel(enable_spinup, &iphy->link_layer_registers->notify_enable_spinup_control);
52962306a36Sopenharmony_ci
53062306a36Sopenharmony_ci		/* Change state to the final state this substate machine has run to completion */
53162306a36Sopenharmony_ci		sci_change_state(&iphy->sm, SCI_PHY_SUB_FINAL);
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_ci		return SCI_SUCCESS;
53462306a36Sopenharmony_ci	}
53562306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_SATA_POWER: {
53662306a36Sopenharmony_ci		u32 scu_sas_pcfg_value;
53762306a36Sopenharmony_ci
53862306a36Sopenharmony_ci		/* Release the spinup hold state and reset the OOB state machine */
53962306a36Sopenharmony_ci		scu_sas_pcfg_value =
54062306a36Sopenharmony_ci			readl(&iphy->link_layer_registers->phy_configuration);
54162306a36Sopenharmony_ci		scu_sas_pcfg_value &=
54262306a36Sopenharmony_ci			~(SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD) | SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE));
54362306a36Sopenharmony_ci		scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
54462306a36Sopenharmony_ci		writel(scu_sas_pcfg_value,
54562306a36Sopenharmony_ci			&iphy->link_layer_registers->phy_configuration);
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci		/* Now restart the OOB operation */
54862306a36Sopenharmony_ci		scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
54962306a36Sopenharmony_ci		scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
55062306a36Sopenharmony_ci		writel(scu_sas_pcfg_value,
55162306a36Sopenharmony_ci			&iphy->link_layer_registers->phy_configuration);
55262306a36Sopenharmony_ci
55362306a36Sopenharmony_ci		/* Change state to the final state this substate machine has run to completion */
55462306a36Sopenharmony_ci		sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_PHY_EN);
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci		return SCI_SUCCESS;
55762306a36Sopenharmony_ci	}
55862306a36Sopenharmony_ci	default:
55962306a36Sopenharmony_ci		dev_dbg(sciphy_to_dev(iphy), "%s: in wrong state: %s\n",
56062306a36Sopenharmony_ci			__func__, phy_state_name(state));
56162306a36Sopenharmony_ci		return SCI_FAILURE_INVALID_STATE;
56262306a36Sopenharmony_ci	}
56362306a36Sopenharmony_ci}
56462306a36Sopenharmony_ci
56562306a36Sopenharmony_cistatic void sci_phy_start_sas_link_training(struct isci_phy *iphy)
56662306a36Sopenharmony_ci{
56762306a36Sopenharmony_ci	/* continue the link training for the phy as if it were a SAS PHY
56862306a36Sopenharmony_ci	 * instead of a SATA PHY. This is done because the completion queue had a SAS
56962306a36Sopenharmony_ci	 * PHY DETECTED event when the state machine was expecting a SATA PHY event.
57062306a36Sopenharmony_ci	 */
57162306a36Sopenharmony_ci	u32 phy_control;
57262306a36Sopenharmony_ci
57362306a36Sopenharmony_ci	phy_control = readl(&iphy->link_layer_registers->phy_configuration);
57462306a36Sopenharmony_ci	phy_control |= SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD);
57562306a36Sopenharmony_ci	writel(phy_control,
57662306a36Sopenharmony_ci	       &iphy->link_layer_registers->phy_configuration);
57762306a36Sopenharmony_ci
57862306a36Sopenharmony_ci	sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SAS_SPEED_EN);
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_ci	iphy->protocol = SAS_PROTOCOL_SSP;
58162306a36Sopenharmony_ci}
58262306a36Sopenharmony_ci
58362306a36Sopenharmony_cistatic void sci_phy_start_sata_link_training(struct isci_phy *iphy)
58462306a36Sopenharmony_ci{
58562306a36Sopenharmony_ci	/* This method continues the link training for the phy as if it were a SATA PHY
58662306a36Sopenharmony_ci	 * instead of a SAS PHY.  This is done because the completion queue had a SATA
58762306a36Sopenharmony_ci	 * SPINUP HOLD event when the state machine was expecting a SAS PHY event. none
58862306a36Sopenharmony_ci	 */
58962306a36Sopenharmony_ci	sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_POWER);
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci	iphy->protocol = SAS_PROTOCOL_SATA;
59262306a36Sopenharmony_ci}
59362306a36Sopenharmony_ci
59462306a36Sopenharmony_ci/**
59562306a36Sopenharmony_ci * sci_phy_complete_link_training - perform processing common to
59662306a36Sopenharmony_ci *    all protocols upon completion of link training.
59762306a36Sopenharmony_ci * @iphy: This parameter specifies the phy object for which link training
59862306a36Sopenharmony_ci *    has completed.
59962306a36Sopenharmony_ci * @max_link_rate: This parameter specifies the maximum link rate to be
60062306a36Sopenharmony_ci *    associated with this phy.
60162306a36Sopenharmony_ci * @next_state: This parameter specifies the next state for the phy's starting
60262306a36Sopenharmony_ci *    sub-state machine.
60362306a36Sopenharmony_ci *
60462306a36Sopenharmony_ci */
60562306a36Sopenharmony_cistatic void sci_phy_complete_link_training(struct isci_phy *iphy,
60662306a36Sopenharmony_ci					   enum sas_linkrate max_link_rate,
60762306a36Sopenharmony_ci					   u32 next_state)
60862306a36Sopenharmony_ci{
60962306a36Sopenharmony_ci	iphy->max_negotiated_speed = max_link_rate;
61062306a36Sopenharmony_ci
61162306a36Sopenharmony_ci	sci_change_state(&iphy->sm, next_state);
61262306a36Sopenharmony_ci}
61362306a36Sopenharmony_ci
61462306a36Sopenharmony_cistatic const char *phy_event_name(u32 event_code)
61562306a36Sopenharmony_ci{
61662306a36Sopenharmony_ci	switch (scu_get_event_code(event_code)) {
61762306a36Sopenharmony_ci	case SCU_EVENT_PORT_SELECTOR_DETECTED:
61862306a36Sopenharmony_ci		return "port selector";
61962306a36Sopenharmony_ci	case SCU_EVENT_SENT_PORT_SELECTION:
62062306a36Sopenharmony_ci		return "port selection";
62162306a36Sopenharmony_ci	case SCU_EVENT_HARD_RESET_TRANSMITTED:
62262306a36Sopenharmony_ci		return "tx hard reset";
62362306a36Sopenharmony_ci	case SCU_EVENT_HARD_RESET_RECEIVED:
62462306a36Sopenharmony_ci		return "rx hard reset";
62562306a36Sopenharmony_ci	case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
62662306a36Sopenharmony_ci		return "identify timeout";
62762306a36Sopenharmony_ci	case SCU_EVENT_LINK_FAILURE:
62862306a36Sopenharmony_ci		return "link fail";
62962306a36Sopenharmony_ci	case SCU_EVENT_SATA_SPINUP_HOLD:
63062306a36Sopenharmony_ci		return "sata spinup hold";
63162306a36Sopenharmony_ci	case SCU_EVENT_SAS_15_SSC:
63262306a36Sopenharmony_ci	case SCU_EVENT_SAS_15:
63362306a36Sopenharmony_ci		return "sas 1.5";
63462306a36Sopenharmony_ci	case SCU_EVENT_SAS_30_SSC:
63562306a36Sopenharmony_ci	case SCU_EVENT_SAS_30:
63662306a36Sopenharmony_ci		return "sas 3.0";
63762306a36Sopenharmony_ci	case SCU_EVENT_SAS_60_SSC:
63862306a36Sopenharmony_ci	case SCU_EVENT_SAS_60:
63962306a36Sopenharmony_ci		return "sas 6.0";
64062306a36Sopenharmony_ci	case SCU_EVENT_SATA_15_SSC:
64162306a36Sopenharmony_ci	case SCU_EVENT_SATA_15:
64262306a36Sopenharmony_ci		return "sata 1.5";
64362306a36Sopenharmony_ci	case SCU_EVENT_SATA_30_SSC:
64462306a36Sopenharmony_ci	case SCU_EVENT_SATA_30:
64562306a36Sopenharmony_ci		return "sata 3.0";
64662306a36Sopenharmony_ci	case SCU_EVENT_SATA_60_SSC:
64762306a36Sopenharmony_ci	case SCU_EVENT_SATA_60:
64862306a36Sopenharmony_ci		return "sata 6.0";
64962306a36Sopenharmony_ci	case SCU_EVENT_SAS_PHY_DETECTED:
65062306a36Sopenharmony_ci		return "sas detect";
65162306a36Sopenharmony_ci	case SCU_EVENT_SATA_PHY_DETECTED:
65262306a36Sopenharmony_ci		return "sata detect";
65362306a36Sopenharmony_ci	default:
65462306a36Sopenharmony_ci		return "unknown";
65562306a36Sopenharmony_ci	}
65662306a36Sopenharmony_ci}
65762306a36Sopenharmony_ci
65862306a36Sopenharmony_ci#define phy_event_dbg(iphy, state, code) \
65962306a36Sopenharmony_ci	dev_dbg(sciphy_to_dev(iphy), "phy-%d:%d: %s event: %s (%x)\n", \
66062306a36Sopenharmony_ci		phy_to_host(iphy)->id, iphy->phy_index, \
66162306a36Sopenharmony_ci		phy_state_name(state), phy_event_name(code), code)
66262306a36Sopenharmony_ci
66362306a36Sopenharmony_ci#define phy_event_warn(iphy, state, code) \
66462306a36Sopenharmony_ci	dev_warn(sciphy_to_dev(iphy), "phy-%d:%d: %s event: %s (%x)\n", \
66562306a36Sopenharmony_ci		phy_to_host(iphy)->id, iphy->phy_index, \
66662306a36Sopenharmony_ci		phy_state_name(state), phy_event_name(code), code)
66762306a36Sopenharmony_ci
66862306a36Sopenharmony_ci
66962306a36Sopenharmony_cistatic void scu_link_layer_set_txcomsas_timeout(struct isci_phy *iphy, u32 timeout)
67062306a36Sopenharmony_ci{
67162306a36Sopenharmony_ci	u32 val;
67262306a36Sopenharmony_ci
67362306a36Sopenharmony_ci	/* Extend timeout */
67462306a36Sopenharmony_ci	val = readl(&iphy->link_layer_registers->transmit_comsas_signal);
67562306a36Sopenharmony_ci	val &= ~SCU_SAS_LLTXCOMSAS_GEN_VAL(NEGTIME, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_MASK);
67662306a36Sopenharmony_ci	val |= SCU_SAS_LLTXCOMSAS_GEN_VAL(NEGTIME, timeout);
67762306a36Sopenharmony_ci
67862306a36Sopenharmony_ci	writel(val, &iphy->link_layer_registers->transmit_comsas_signal);
67962306a36Sopenharmony_ci}
68062306a36Sopenharmony_ci
68162306a36Sopenharmony_cienum sci_status sci_phy_event_handler(struct isci_phy *iphy, u32 event_code)
68262306a36Sopenharmony_ci{
68362306a36Sopenharmony_ci	enum sci_phy_states state = iphy->sm.current_state_id;
68462306a36Sopenharmony_ci
68562306a36Sopenharmony_ci	switch (state) {
68662306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_OSSP_EN:
68762306a36Sopenharmony_ci		switch (scu_get_event_code(event_code)) {
68862306a36Sopenharmony_ci		case SCU_EVENT_SAS_PHY_DETECTED:
68962306a36Sopenharmony_ci			sci_phy_start_sas_link_training(iphy);
69062306a36Sopenharmony_ci			iphy->is_in_link_training = true;
69162306a36Sopenharmony_ci			break;
69262306a36Sopenharmony_ci		case SCU_EVENT_SATA_SPINUP_HOLD:
69362306a36Sopenharmony_ci			sci_phy_start_sata_link_training(iphy);
69462306a36Sopenharmony_ci			iphy->is_in_link_training = true;
69562306a36Sopenharmony_ci			break;
69662306a36Sopenharmony_ci		case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
69762306a36Sopenharmony_ci		       /* Extend timeout value */
69862306a36Sopenharmony_ci		       scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_EXTENDED);
69962306a36Sopenharmony_ci
70062306a36Sopenharmony_ci		       /* Start the oob/sn state machine over again */
70162306a36Sopenharmony_ci		       sci_change_state(&iphy->sm, SCI_PHY_STARTING);
70262306a36Sopenharmony_ci		       break;
70362306a36Sopenharmony_ci		default:
70462306a36Sopenharmony_ci			phy_event_dbg(iphy, state, event_code);
70562306a36Sopenharmony_ci			return SCI_FAILURE;
70662306a36Sopenharmony_ci		}
70762306a36Sopenharmony_ci		return SCI_SUCCESS;
70862306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
70962306a36Sopenharmony_ci		switch (scu_get_event_code(event_code)) {
71062306a36Sopenharmony_ci		case SCU_EVENT_SAS_PHY_DETECTED:
71162306a36Sopenharmony_ci			/*
71262306a36Sopenharmony_ci			 * Why is this being reported again by the controller?
71362306a36Sopenharmony_ci			 * We would re-enter this state so just stay here */
71462306a36Sopenharmony_ci			break;
71562306a36Sopenharmony_ci		case SCU_EVENT_SAS_15:
71662306a36Sopenharmony_ci		case SCU_EVENT_SAS_15_SSC:
71762306a36Sopenharmony_ci			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_1_5_GBPS,
71862306a36Sopenharmony_ci						       SCI_PHY_SUB_AWAIT_IAF_UF);
71962306a36Sopenharmony_ci			break;
72062306a36Sopenharmony_ci		case SCU_EVENT_SAS_30:
72162306a36Sopenharmony_ci		case SCU_EVENT_SAS_30_SSC:
72262306a36Sopenharmony_ci			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_3_0_GBPS,
72362306a36Sopenharmony_ci						       SCI_PHY_SUB_AWAIT_IAF_UF);
72462306a36Sopenharmony_ci			break;
72562306a36Sopenharmony_ci		case SCU_EVENT_SAS_60:
72662306a36Sopenharmony_ci		case SCU_EVENT_SAS_60_SSC:
72762306a36Sopenharmony_ci			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_6_0_GBPS,
72862306a36Sopenharmony_ci						       SCI_PHY_SUB_AWAIT_IAF_UF);
72962306a36Sopenharmony_ci			break;
73062306a36Sopenharmony_ci		case SCU_EVENT_SATA_SPINUP_HOLD:
73162306a36Sopenharmony_ci			/*
73262306a36Sopenharmony_ci			 * We were doing SAS PHY link training and received a SATA PHY event
73362306a36Sopenharmony_ci			 * continue OOB/SN as if this were a SATA PHY */
73462306a36Sopenharmony_ci			sci_phy_start_sata_link_training(iphy);
73562306a36Sopenharmony_ci			break;
73662306a36Sopenharmony_ci		case SCU_EVENT_LINK_FAILURE:
73762306a36Sopenharmony_ci			/* Change the timeout value to default */
73862306a36Sopenharmony_ci			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
73962306a36Sopenharmony_ci
74062306a36Sopenharmony_ci			/* Link failure change state back to the starting state */
74162306a36Sopenharmony_ci			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
74262306a36Sopenharmony_ci			break;
74362306a36Sopenharmony_ci		case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
74462306a36Sopenharmony_ci		       /* Extend the timeout value */
74562306a36Sopenharmony_ci		       scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_EXTENDED);
74662306a36Sopenharmony_ci
74762306a36Sopenharmony_ci		       /* Start the oob/sn state machine over again */
74862306a36Sopenharmony_ci		       sci_change_state(&iphy->sm, SCI_PHY_STARTING);
74962306a36Sopenharmony_ci		       break;
75062306a36Sopenharmony_ci		default:
75162306a36Sopenharmony_ci			phy_event_warn(iphy, state, event_code);
75262306a36Sopenharmony_ci			return SCI_FAILURE;
75362306a36Sopenharmony_ci		}
75462306a36Sopenharmony_ci		return SCI_SUCCESS;
75562306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_IAF_UF:
75662306a36Sopenharmony_ci		switch (scu_get_event_code(event_code)) {
75762306a36Sopenharmony_ci		case SCU_EVENT_SAS_PHY_DETECTED:
75862306a36Sopenharmony_ci			/* Backup the state machine */
75962306a36Sopenharmony_ci			sci_phy_start_sas_link_training(iphy);
76062306a36Sopenharmony_ci			break;
76162306a36Sopenharmony_ci		case SCU_EVENT_SATA_SPINUP_HOLD:
76262306a36Sopenharmony_ci			/* We were doing SAS PHY link training and received a
76362306a36Sopenharmony_ci			 * SATA PHY event continue OOB/SN as if this were a
76462306a36Sopenharmony_ci			 * SATA PHY
76562306a36Sopenharmony_ci			 */
76662306a36Sopenharmony_ci			sci_phy_start_sata_link_training(iphy);
76762306a36Sopenharmony_ci			break;
76862306a36Sopenharmony_ci		case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
76962306a36Sopenharmony_ci			/* Extend the timeout value */
77062306a36Sopenharmony_ci			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_EXTENDED);
77162306a36Sopenharmony_ci
77262306a36Sopenharmony_ci			/* Start the oob/sn state machine over again */
77362306a36Sopenharmony_ci			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
77462306a36Sopenharmony_ci			break;
77562306a36Sopenharmony_ci		case SCU_EVENT_LINK_FAILURE:
77662306a36Sopenharmony_ci			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
77762306a36Sopenharmony_ci			fallthrough;
77862306a36Sopenharmony_ci		case SCU_EVENT_HARD_RESET_RECEIVED:
77962306a36Sopenharmony_ci			/* Start the oob/sn state machine over again */
78062306a36Sopenharmony_ci			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
78162306a36Sopenharmony_ci			break;
78262306a36Sopenharmony_ci		default:
78362306a36Sopenharmony_ci			phy_event_warn(iphy, state, event_code);
78462306a36Sopenharmony_ci			return SCI_FAILURE;
78562306a36Sopenharmony_ci		}
78662306a36Sopenharmony_ci		return SCI_SUCCESS;
78762306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_SAS_POWER:
78862306a36Sopenharmony_ci		switch (scu_get_event_code(event_code)) {
78962306a36Sopenharmony_ci		case SCU_EVENT_LINK_FAILURE:
79062306a36Sopenharmony_ci			/* Change the timeout value to default */
79162306a36Sopenharmony_ci			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
79262306a36Sopenharmony_ci
79362306a36Sopenharmony_ci			/* Link failure change state back to the starting state */
79462306a36Sopenharmony_ci			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
79562306a36Sopenharmony_ci			break;
79662306a36Sopenharmony_ci		default:
79762306a36Sopenharmony_ci			phy_event_warn(iphy, state, event_code);
79862306a36Sopenharmony_ci			return SCI_FAILURE;
79962306a36Sopenharmony_ci		}
80062306a36Sopenharmony_ci		return SCI_SUCCESS;
80162306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_SATA_POWER:
80262306a36Sopenharmony_ci		switch (scu_get_event_code(event_code)) {
80362306a36Sopenharmony_ci		case SCU_EVENT_LINK_FAILURE:
80462306a36Sopenharmony_ci			/* Change the timeout value to default */
80562306a36Sopenharmony_ci			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
80662306a36Sopenharmony_ci
80762306a36Sopenharmony_ci			/* Link failure change state back to the starting state */
80862306a36Sopenharmony_ci			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
80962306a36Sopenharmony_ci			break;
81062306a36Sopenharmony_ci		case SCU_EVENT_SATA_SPINUP_HOLD:
81162306a36Sopenharmony_ci			/* These events are received every 10ms and are
81262306a36Sopenharmony_ci			 * expected while in this state
81362306a36Sopenharmony_ci			 */
81462306a36Sopenharmony_ci			break;
81562306a36Sopenharmony_ci
81662306a36Sopenharmony_ci		case SCU_EVENT_SAS_PHY_DETECTED:
81762306a36Sopenharmony_ci			/* There has been a change in the phy type before OOB/SN for the
81862306a36Sopenharmony_ci			 * SATA finished start down the SAS link traning path.
81962306a36Sopenharmony_ci			 */
82062306a36Sopenharmony_ci			sci_phy_start_sas_link_training(iphy);
82162306a36Sopenharmony_ci			break;
82262306a36Sopenharmony_ci
82362306a36Sopenharmony_ci		default:
82462306a36Sopenharmony_ci			phy_event_warn(iphy, state, event_code);
82562306a36Sopenharmony_ci			return SCI_FAILURE;
82662306a36Sopenharmony_ci		}
82762306a36Sopenharmony_ci		return SCI_SUCCESS;
82862306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
82962306a36Sopenharmony_ci		switch (scu_get_event_code(event_code)) {
83062306a36Sopenharmony_ci		case SCU_EVENT_LINK_FAILURE:
83162306a36Sopenharmony_ci			/* Change the timeout value to default */
83262306a36Sopenharmony_ci			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
83362306a36Sopenharmony_ci
83462306a36Sopenharmony_ci			/* Link failure change state back to the starting state */
83562306a36Sopenharmony_ci			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
83662306a36Sopenharmony_ci			break;
83762306a36Sopenharmony_ci		case SCU_EVENT_SATA_SPINUP_HOLD:
83862306a36Sopenharmony_ci			/* These events might be received since we dont know how many may be in
83962306a36Sopenharmony_ci			 * the completion queue while waiting for power
84062306a36Sopenharmony_ci			 */
84162306a36Sopenharmony_ci			break;
84262306a36Sopenharmony_ci		case SCU_EVENT_SATA_PHY_DETECTED:
84362306a36Sopenharmony_ci			iphy->protocol = SAS_PROTOCOL_SATA;
84462306a36Sopenharmony_ci
84562306a36Sopenharmony_ci			/* We have received the SATA PHY notification change state */
84662306a36Sopenharmony_ci			sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
84762306a36Sopenharmony_ci			break;
84862306a36Sopenharmony_ci		case SCU_EVENT_SAS_PHY_DETECTED:
84962306a36Sopenharmony_ci			/* There has been a change in the phy type before OOB/SN for the
85062306a36Sopenharmony_ci			 * SATA finished start down the SAS link traning path.
85162306a36Sopenharmony_ci			 */
85262306a36Sopenharmony_ci			sci_phy_start_sas_link_training(iphy);
85362306a36Sopenharmony_ci			break;
85462306a36Sopenharmony_ci		default:
85562306a36Sopenharmony_ci			phy_event_warn(iphy, state, event_code);
85662306a36Sopenharmony_ci			return SCI_FAILURE;
85762306a36Sopenharmony_ci		}
85862306a36Sopenharmony_ci		return SCI_SUCCESS;
85962306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
86062306a36Sopenharmony_ci		switch (scu_get_event_code(event_code)) {
86162306a36Sopenharmony_ci		case SCU_EVENT_SATA_PHY_DETECTED:
86262306a36Sopenharmony_ci			/*
86362306a36Sopenharmony_ci			 * The hardware reports multiple SATA PHY detected events
86462306a36Sopenharmony_ci			 * ignore the extras */
86562306a36Sopenharmony_ci			break;
86662306a36Sopenharmony_ci		case SCU_EVENT_SATA_15:
86762306a36Sopenharmony_ci		case SCU_EVENT_SATA_15_SSC:
86862306a36Sopenharmony_ci			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_1_5_GBPS,
86962306a36Sopenharmony_ci						       SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
87062306a36Sopenharmony_ci			break;
87162306a36Sopenharmony_ci		case SCU_EVENT_SATA_30:
87262306a36Sopenharmony_ci		case SCU_EVENT_SATA_30_SSC:
87362306a36Sopenharmony_ci			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_3_0_GBPS,
87462306a36Sopenharmony_ci						       SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
87562306a36Sopenharmony_ci			break;
87662306a36Sopenharmony_ci		case SCU_EVENT_SATA_60:
87762306a36Sopenharmony_ci		case SCU_EVENT_SATA_60_SSC:
87862306a36Sopenharmony_ci			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_6_0_GBPS,
87962306a36Sopenharmony_ci						       SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
88062306a36Sopenharmony_ci			break;
88162306a36Sopenharmony_ci		case SCU_EVENT_LINK_FAILURE:
88262306a36Sopenharmony_ci			/* Change the timeout value to default */
88362306a36Sopenharmony_ci			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
88462306a36Sopenharmony_ci
88562306a36Sopenharmony_ci			/* Link failure change state back to the starting state */
88662306a36Sopenharmony_ci			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
88762306a36Sopenharmony_ci			break;
88862306a36Sopenharmony_ci		case SCU_EVENT_SAS_PHY_DETECTED:
88962306a36Sopenharmony_ci			/*
89062306a36Sopenharmony_ci			 * There has been a change in the phy type before OOB/SN for the
89162306a36Sopenharmony_ci			 * SATA finished start down the SAS link traning path. */
89262306a36Sopenharmony_ci			sci_phy_start_sas_link_training(iphy);
89362306a36Sopenharmony_ci			break;
89462306a36Sopenharmony_ci		default:
89562306a36Sopenharmony_ci			phy_event_warn(iphy, state, event_code);
89662306a36Sopenharmony_ci			return SCI_FAILURE;
89762306a36Sopenharmony_ci		}
89862306a36Sopenharmony_ci
89962306a36Sopenharmony_ci		return SCI_SUCCESS;
90062306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
90162306a36Sopenharmony_ci		switch (scu_get_event_code(event_code)) {
90262306a36Sopenharmony_ci		case SCU_EVENT_SATA_PHY_DETECTED:
90362306a36Sopenharmony_ci			/* Backup the state machine */
90462306a36Sopenharmony_ci			sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
90562306a36Sopenharmony_ci			break;
90662306a36Sopenharmony_ci
90762306a36Sopenharmony_ci		case SCU_EVENT_LINK_FAILURE:
90862306a36Sopenharmony_ci			/* Change the timeout value to default */
90962306a36Sopenharmony_ci			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
91062306a36Sopenharmony_ci
91162306a36Sopenharmony_ci			/* Link failure change state back to the starting state */
91262306a36Sopenharmony_ci			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
91362306a36Sopenharmony_ci			break;
91462306a36Sopenharmony_ci
91562306a36Sopenharmony_ci		default:
91662306a36Sopenharmony_ci			phy_event_warn(iphy, state, event_code);
91762306a36Sopenharmony_ci			return SCI_FAILURE;
91862306a36Sopenharmony_ci		}
91962306a36Sopenharmony_ci		return SCI_SUCCESS;
92062306a36Sopenharmony_ci	case SCI_PHY_READY:
92162306a36Sopenharmony_ci		switch (scu_get_event_code(event_code)) {
92262306a36Sopenharmony_ci		case SCU_EVENT_LINK_FAILURE:
92362306a36Sopenharmony_ci			/* Set default timeout */
92462306a36Sopenharmony_ci			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
92562306a36Sopenharmony_ci
92662306a36Sopenharmony_ci			/* Link failure change state back to the starting state */
92762306a36Sopenharmony_ci			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
92862306a36Sopenharmony_ci			break;
92962306a36Sopenharmony_ci		case SCU_EVENT_BROADCAST_CHANGE:
93062306a36Sopenharmony_ci		case SCU_EVENT_BROADCAST_SES:
93162306a36Sopenharmony_ci		case SCU_EVENT_BROADCAST_RESERVED0:
93262306a36Sopenharmony_ci		case SCU_EVENT_BROADCAST_RESERVED1:
93362306a36Sopenharmony_ci		case SCU_EVENT_BROADCAST_EXPANDER:
93462306a36Sopenharmony_ci		case SCU_EVENT_BROADCAST_AEN:
93562306a36Sopenharmony_ci			/* Broadcast change received. Notify the port. */
93662306a36Sopenharmony_ci			if (phy_get_non_dummy_port(iphy) != NULL)
93762306a36Sopenharmony_ci				sci_port_broadcast_change_received(iphy->owning_port, iphy);
93862306a36Sopenharmony_ci			else
93962306a36Sopenharmony_ci				iphy->bcn_received_while_port_unassigned = true;
94062306a36Sopenharmony_ci			break;
94162306a36Sopenharmony_ci		case SCU_EVENT_BROADCAST_RESERVED3:
94262306a36Sopenharmony_ci		case SCU_EVENT_BROADCAST_RESERVED4:
94362306a36Sopenharmony_ci		default:
94462306a36Sopenharmony_ci			phy_event_warn(iphy, state, event_code);
94562306a36Sopenharmony_ci			return SCI_FAILURE_INVALID_STATE;
94662306a36Sopenharmony_ci		}
94762306a36Sopenharmony_ci		return SCI_SUCCESS;
94862306a36Sopenharmony_ci	case SCI_PHY_RESETTING:
94962306a36Sopenharmony_ci		switch (scu_get_event_code(event_code)) {
95062306a36Sopenharmony_ci		case SCU_EVENT_HARD_RESET_TRANSMITTED:
95162306a36Sopenharmony_ci			/* Link failure change state back to the starting state */
95262306a36Sopenharmony_ci			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
95362306a36Sopenharmony_ci			break;
95462306a36Sopenharmony_ci		default:
95562306a36Sopenharmony_ci			phy_event_warn(iphy, state, event_code);
95662306a36Sopenharmony_ci			return SCI_FAILURE_INVALID_STATE;
95762306a36Sopenharmony_ci		}
95862306a36Sopenharmony_ci		return SCI_SUCCESS;
95962306a36Sopenharmony_ci	default:
96062306a36Sopenharmony_ci		dev_dbg(sciphy_to_dev(iphy), "%s: in wrong state: %s\n",
96162306a36Sopenharmony_ci			__func__, phy_state_name(state));
96262306a36Sopenharmony_ci		return SCI_FAILURE_INVALID_STATE;
96362306a36Sopenharmony_ci	}
96462306a36Sopenharmony_ci}
96562306a36Sopenharmony_ci
96662306a36Sopenharmony_cienum sci_status sci_phy_frame_handler(struct isci_phy *iphy, u32 frame_index)
96762306a36Sopenharmony_ci{
96862306a36Sopenharmony_ci	enum sci_phy_states state = iphy->sm.current_state_id;
96962306a36Sopenharmony_ci	struct isci_host *ihost = iphy->owning_port->owning_controller;
97062306a36Sopenharmony_ci	enum sci_status result;
97162306a36Sopenharmony_ci	unsigned long flags;
97262306a36Sopenharmony_ci
97362306a36Sopenharmony_ci	switch (state) {
97462306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_IAF_UF: {
97562306a36Sopenharmony_ci		u32 *frame_words;
97662306a36Sopenharmony_ci		struct sas_identify_frame iaf;
97762306a36Sopenharmony_ci
97862306a36Sopenharmony_ci		result = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
97962306a36Sopenharmony_ci								  frame_index,
98062306a36Sopenharmony_ci								  (void **)&frame_words);
98162306a36Sopenharmony_ci
98262306a36Sopenharmony_ci		if (result != SCI_SUCCESS)
98362306a36Sopenharmony_ci			return result;
98462306a36Sopenharmony_ci
98562306a36Sopenharmony_ci		sci_swab32_cpy(&iaf, frame_words, sizeof(iaf) / sizeof(u32));
98662306a36Sopenharmony_ci		if (iaf.frame_type == 0) {
98762306a36Sopenharmony_ci			u32 state;
98862306a36Sopenharmony_ci
98962306a36Sopenharmony_ci			spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
99062306a36Sopenharmony_ci			memcpy(&iphy->frame_rcvd.iaf, &iaf, sizeof(iaf));
99162306a36Sopenharmony_ci			spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
99262306a36Sopenharmony_ci			if (iaf.smp_tport) {
99362306a36Sopenharmony_ci				/* We got the IAF for an expander PHY go to the final
99462306a36Sopenharmony_ci				 * state since there are no power requirements for
99562306a36Sopenharmony_ci				 * expander phys.
99662306a36Sopenharmony_ci				 */
99762306a36Sopenharmony_ci				state = SCI_PHY_SUB_FINAL;
99862306a36Sopenharmony_ci			} else {
99962306a36Sopenharmony_ci				/* We got the IAF we can now go to the await spinup
100062306a36Sopenharmony_ci				 * semaphore state
100162306a36Sopenharmony_ci				 */
100262306a36Sopenharmony_ci				state = SCI_PHY_SUB_AWAIT_SAS_POWER;
100362306a36Sopenharmony_ci			}
100462306a36Sopenharmony_ci			sci_change_state(&iphy->sm, state);
100562306a36Sopenharmony_ci			result = SCI_SUCCESS;
100662306a36Sopenharmony_ci		} else
100762306a36Sopenharmony_ci			dev_warn(sciphy_to_dev(iphy),
100862306a36Sopenharmony_ci				"%s: PHY starting substate machine received "
100962306a36Sopenharmony_ci				"unexpected frame id %x\n",
101062306a36Sopenharmony_ci				__func__, frame_index);
101162306a36Sopenharmony_ci
101262306a36Sopenharmony_ci		sci_controller_release_frame(ihost, frame_index);
101362306a36Sopenharmony_ci		return result;
101462306a36Sopenharmony_ci	}
101562306a36Sopenharmony_ci	case SCI_PHY_SUB_AWAIT_SIG_FIS_UF: {
101662306a36Sopenharmony_ci		struct dev_to_host_fis *frame_header;
101762306a36Sopenharmony_ci		u32 *fis_frame_data;
101862306a36Sopenharmony_ci
101962306a36Sopenharmony_ci		result = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
102062306a36Sopenharmony_ci								  frame_index,
102162306a36Sopenharmony_ci								  (void **)&frame_header);
102262306a36Sopenharmony_ci
102362306a36Sopenharmony_ci		if (result != SCI_SUCCESS)
102462306a36Sopenharmony_ci			return result;
102562306a36Sopenharmony_ci
102662306a36Sopenharmony_ci		if ((frame_header->fis_type == FIS_REGD2H) &&
102762306a36Sopenharmony_ci		    !(frame_header->status & ATA_BUSY)) {
102862306a36Sopenharmony_ci			sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
102962306a36Sopenharmony_ci								 frame_index,
103062306a36Sopenharmony_ci								 (void **)&fis_frame_data);
103162306a36Sopenharmony_ci
103262306a36Sopenharmony_ci			spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
103362306a36Sopenharmony_ci			sci_controller_copy_sata_response(&iphy->frame_rcvd.fis,
103462306a36Sopenharmony_ci							  frame_header,
103562306a36Sopenharmony_ci							  fis_frame_data);
103662306a36Sopenharmony_ci			spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
103762306a36Sopenharmony_ci
103862306a36Sopenharmony_ci			/* got IAF we can now go to the await spinup semaphore state */
103962306a36Sopenharmony_ci			sci_change_state(&iphy->sm, SCI_PHY_SUB_FINAL);
104062306a36Sopenharmony_ci
104162306a36Sopenharmony_ci			result = SCI_SUCCESS;
104262306a36Sopenharmony_ci		} else
104362306a36Sopenharmony_ci			dev_warn(sciphy_to_dev(iphy),
104462306a36Sopenharmony_ci				 "%s: PHY starting substate machine received "
104562306a36Sopenharmony_ci				 "unexpected frame id %x\n",
104662306a36Sopenharmony_ci				 __func__, frame_index);
104762306a36Sopenharmony_ci
104862306a36Sopenharmony_ci		/* Regardless of the result we are done with this frame with it */
104962306a36Sopenharmony_ci		sci_controller_release_frame(ihost, frame_index);
105062306a36Sopenharmony_ci
105162306a36Sopenharmony_ci		return result;
105262306a36Sopenharmony_ci	}
105362306a36Sopenharmony_ci	default:
105462306a36Sopenharmony_ci		dev_dbg(sciphy_to_dev(iphy), "%s: in wrong state: %s\n",
105562306a36Sopenharmony_ci			__func__, phy_state_name(state));
105662306a36Sopenharmony_ci		return SCI_FAILURE_INVALID_STATE;
105762306a36Sopenharmony_ci	}
105862306a36Sopenharmony_ci
105962306a36Sopenharmony_ci}
106062306a36Sopenharmony_ci
106162306a36Sopenharmony_cistatic void sci_phy_starting_initial_substate_enter(struct sci_base_state_machine *sm)
106262306a36Sopenharmony_ci{
106362306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
106462306a36Sopenharmony_ci
106562306a36Sopenharmony_ci	/* This is just an temporary state go off to the starting state */
106662306a36Sopenharmony_ci	sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_OSSP_EN);
106762306a36Sopenharmony_ci}
106862306a36Sopenharmony_ci
106962306a36Sopenharmony_cistatic void sci_phy_starting_await_sas_power_substate_enter(struct sci_base_state_machine *sm)
107062306a36Sopenharmony_ci{
107162306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
107262306a36Sopenharmony_ci	struct isci_host *ihost = iphy->owning_port->owning_controller;
107362306a36Sopenharmony_ci
107462306a36Sopenharmony_ci	sci_controller_power_control_queue_insert(ihost, iphy);
107562306a36Sopenharmony_ci}
107662306a36Sopenharmony_ci
107762306a36Sopenharmony_cistatic void sci_phy_starting_await_sas_power_substate_exit(struct sci_base_state_machine *sm)
107862306a36Sopenharmony_ci{
107962306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
108062306a36Sopenharmony_ci	struct isci_host *ihost = iphy->owning_port->owning_controller;
108162306a36Sopenharmony_ci
108262306a36Sopenharmony_ci	sci_controller_power_control_queue_remove(ihost, iphy);
108362306a36Sopenharmony_ci}
108462306a36Sopenharmony_ci
108562306a36Sopenharmony_cistatic void sci_phy_starting_await_sata_power_substate_enter(struct sci_base_state_machine *sm)
108662306a36Sopenharmony_ci{
108762306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
108862306a36Sopenharmony_ci	struct isci_host *ihost = iphy->owning_port->owning_controller;
108962306a36Sopenharmony_ci
109062306a36Sopenharmony_ci	sci_controller_power_control_queue_insert(ihost, iphy);
109162306a36Sopenharmony_ci}
109262306a36Sopenharmony_ci
109362306a36Sopenharmony_cistatic void sci_phy_starting_await_sata_power_substate_exit(struct sci_base_state_machine *sm)
109462306a36Sopenharmony_ci{
109562306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
109662306a36Sopenharmony_ci	struct isci_host *ihost = iphy->owning_port->owning_controller;
109762306a36Sopenharmony_ci
109862306a36Sopenharmony_ci	sci_controller_power_control_queue_remove(ihost, iphy);
109962306a36Sopenharmony_ci}
110062306a36Sopenharmony_ci
110162306a36Sopenharmony_cistatic void sci_phy_starting_await_sata_phy_substate_enter(struct sci_base_state_machine *sm)
110262306a36Sopenharmony_ci{
110362306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
110462306a36Sopenharmony_ci
110562306a36Sopenharmony_ci	sci_mod_timer(&iphy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
110662306a36Sopenharmony_ci}
110762306a36Sopenharmony_ci
110862306a36Sopenharmony_cistatic void sci_phy_starting_await_sata_phy_substate_exit(struct sci_base_state_machine *sm)
110962306a36Sopenharmony_ci{
111062306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
111162306a36Sopenharmony_ci
111262306a36Sopenharmony_ci	sci_del_timer(&iphy->sata_timer);
111362306a36Sopenharmony_ci}
111462306a36Sopenharmony_ci
111562306a36Sopenharmony_cistatic void sci_phy_starting_await_sata_speed_substate_enter(struct sci_base_state_machine *sm)
111662306a36Sopenharmony_ci{
111762306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
111862306a36Sopenharmony_ci
111962306a36Sopenharmony_ci	sci_mod_timer(&iphy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
112062306a36Sopenharmony_ci}
112162306a36Sopenharmony_ci
112262306a36Sopenharmony_cistatic void sci_phy_starting_await_sata_speed_substate_exit(struct sci_base_state_machine *sm)
112362306a36Sopenharmony_ci{
112462306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
112562306a36Sopenharmony_ci
112662306a36Sopenharmony_ci	sci_del_timer(&iphy->sata_timer);
112762306a36Sopenharmony_ci}
112862306a36Sopenharmony_ci
112962306a36Sopenharmony_cistatic void sci_phy_starting_await_sig_fis_uf_substate_enter(struct sci_base_state_machine *sm)
113062306a36Sopenharmony_ci{
113162306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
113262306a36Sopenharmony_ci
113362306a36Sopenharmony_ci	if (sci_port_link_detected(iphy->owning_port, iphy)) {
113462306a36Sopenharmony_ci
113562306a36Sopenharmony_ci		/*
113662306a36Sopenharmony_ci		 * Clear the PE suspend condition so we can actually
113762306a36Sopenharmony_ci		 * receive SIG FIS
113862306a36Sopenharmony_ci		 * The hardware will not respond to the XRDY until the PE
113962306a36Sopenharmony_ci		 * suspend condition is cleared.
114062306a36Sopenharmony_ci		 */
114162306a36Sopenharmony_ci		sci_phy_resume(iphy);
114262306a36Sopenharmony_ci
114362306a36Sopenharmony_ci		sci_mod_timer(&iphy->sata_timer,
114462306a36Sopenharmony_ci			      SCIC_SDS_SIGNATURE_FIS_TIMEOUT);
114562306a36Sopenharmony_ci	} else
114662306a36Sopenharmony_ci		iphy->is_in_link_training = false;
114762306a36Sopenharmony_ci}
114862306a36Sopenharmony_ci
114962306a36Sopenharmony_cistatic void sci_phy_starting_await_sig_fis_uf_substate_exit(struct sci_base_state_machine *sm)
115062306a36Sopenharmony_ci{
115162306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
115262306a36Sopenharmony_ci
115362306a36Sopenharmony_ci	sci_del_timer(&iphy->sata_timer);
115462306a36Sopenharmony_ci}
115562306a36Sopenharmony_ci
115662306a36Sopenharmony_cistatic void sci_phy_starting_final_substate_enter(struct sci_base_state_machine *sm)
115762306a36Sopenharmony_ci{
115862306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
115962306a36Sopenharmony_ci
116062306a36Sopenharmony_ci	/* State machine has run to completion so exit out and change
116162306a36Sopenharmony_ci	 * the base state machine to the ready state
116262306a36Sopenharmony_ci	 */
116362306a36Sopenharmony_ci	sci_change_state(&iphy->sm, SCI_PHY_READY);
116462306a36Sopenharmony_ci}
116562306a36Sopenharmony_ci
116662306a36Sopenharmony_ci/**
116762306a36Sopenharmony_ci * scu_link_layer_stop_protocol_engine()
116862306a36Sopenharmony_ci * @iphy: This is the struct isci_phy object to stop.
116962306a36Sopenharmony_ci *
117062306a36Sopenharmony_ci * This method will stop the struct isci_phy object. This does not reset the
117162306a36Sopenharmony_ci * protocol engine it just suspends it and places it in a state where it will
117262306a36Sopenharmony_ci * not cause the end device to power up. none
117362306a36Sopenharmony_ci */
117462306a36Sopenharmony_cistatic void scu_link_layer_stop_protocol_engine(
117562306a36Sopenharmony_ci	struct isci_phy *iphy)
117662306a36Sopenharmony_ci{
117762306a36Sopenharmony_ci	u32 scu_sas_pcfg_value;
117862306a36Sopenharmony_ci	u32 enable_spinup_value;
117962306a36Sopenharmony_ci
118062306a36Sopenharmony_ci	/* Suspend the protocol engine and place it in a sata spinup hold state */
118162306a36Sopenharmony_ci	scu_sas_pcfg_value =
118262306a36Sopenharmony_ci		readl(&iphy->link_layer_registers->phy_configuration);
118362306a36Sopenharmony_ci	scu_sas_pcfg_value |=
118462306a36Sopenharmony_ci		(SCU_SAS_PCFG_GEN_BIT(OOB_RESET) |
118562306a36Sopenharmony_ci		 SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE) |
118662306a36Sopenharmony_ci		 SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD));
118762306a36Sopenharmony_ci	writel(scu_sas_pcfg_value,
118862306a36Sopenharmony_ci	       &iphy->link_layer_registers->phy_configuration);
118962306a36Sopenharmony_ci
119062306a36Sopenharmony_ci	/* Disable the notify enable spinup primitives */
119162306a36Sopenharmony_ci	enable_spinup_value = readl(&iphy->link_layer_registers->notify_enable_spinup_control);
119262306a36Sopenharmony_ci	enable_spinup_value &= ~SCU_ENSPINUP_GEN_BIT(ENABLE);
119362306a36Sopenharmony_ci	writel(enable_spinup_value, &iphy->link_layer_registers->notify_enable_spinup_control);
119462306a36Sopenharmony_ci}
119562306a36Sopenharmony_ci
119662306a36Sopenharmony_cistatic void scu_link_layer_start_oob(struct isci_phy *iphy)
119762306a36Sopenharmony_ci{
119862306a36Sopenharmony_ci	struct scu_link_layer_registers __iomem *ll = iphy->link_layer_registers;
119962306a36Sopenharmony_ci	u32 val;
120062306a36Sopenharmony_ci
120162306a36Sopenharmony_ci	/** Reset OOB sequence - start */
120262306a36Sopenharmony_ci	val = readl(&ll->phy_configuration);
120362306a36Sopenharmony_ci	val &= ~(SCU_SAS_PCFG_GEN_BIT(OOB_RESET) |
120462306a36Sopenharmony_ci		 SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE) |
120562306a36Sopenharmony_ci		 SCU_SAS_PCFG_GEN_BIT(HARD_RESET));
120662306a36Sopenharmony_ci	writel(val, &ll->phy_configuration);
120762306a36Sopenharmony_ci	readl(&ll->phy_configuration); /* flush */
120862306a36Sopenharmony_ci	/** Reset OOB sequence - end */
120962306a36Sopenharmony_ci
121062306a36Sopenharmony_ci	/** Start OOB sequence - start */
121162306a36Sopenharmony_ci	val = readl(&ll->phy_configuration);
121262306a36Sopenharmony_ci	val |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
121362306a36Sopenharmony_ci	writel(val, &ll->phy_configuration);
121462306a36Sopenharmony_ci	readl(&ll->phy_configuration); /* flush */
121562306a36Sopenharmony_ci	/** Start OOB sequence - end */
121662306a36Sopenharmony_ci}
121762306a36Sopenharmony_ci
121862306a36Sopenharmony_ci/**
121962306a36Sopenharmony_ci * scu_link_layer_tx_hard_reset()
122062306a36Sopenharmony_ci * @iphy: This is the struct isci_phy object to stop.
122162306a36Sopenharmony_ci *
122262306a36Sopenharmony_ci * This method will transmit a hard reset request on the specified phy. The SCU
122362306a36Sopenharmony_ci * hardware requires that we reset the OOB state machine and set the hard reset
122462306a36Sopenharmony_ci * bit in the phy configuration register. We then must start OOB over with the
122562306a36Sopenharmony_ci * hard reset bit set.
122662306a36Sopenharmony_ci */
122762306a36Sopenharmony_cistatic void scu_link_layer_tx_hard_reset(
122862306a36Sopenharmony_ci	struct isci_phy *iphy)
122962306a36Sopenharmony_ci{
123062306a36Sopenharmony_ci	u32 phy_configuration_value;
123162306a36Sopenharmony_ci
123262306a36Sopenharmony_ci	/*
123362306a36Sopenharmony_ci	 * SAS Phys must wait for the HARD_RESET_TX event notification to transition
123462306a36Sopenharmony_ci	 * to the starting state. */
123562306a36Sopenharmony_ci	phy_configuration_value =
123662306a36Sopenharmony_ci		readl(&iphy->link_layer_registers->phy_configuration);
123762306a36Sopenharmony_ci	phy_configuration_value &= ~(SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE));
123862306a36Sopenharmony_ci	phy_configuration_value |=
123962306a36Sopenharmony_ci		(SCU_SAS_PCFG_GEN_BIT(HARD_RESET) |
124062306a36Sopenharmony_ci		 SCU_SAS_PCFG_GEN_BIT(OOB_RESET));
124162306a36Sopenharmony_ci	writel(phy_configuration_value,
124262306a36Sopenharmony_ci	       &iphy->link_layer_registers->phy_configuration);
124362306a36Sopenharmony_ci
124462306a36Sopenharmony_ci	/* Now take the OOB state machine out of reset */
124562306a36Sopenharmony_ci	phy_configuration_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
124662306a36Sopenharmony_ci	phy_configuration_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
124762306a36Sopenharmony_ci	writel(phy_configuration_value,
124862306a36Sopenharmony_ci	       &iphy->link_layer_registers->phy_configuration);
124962306a36Sopenharmony_ci}
125062306a36Sopenharmony_ci
125162306a36Sopenharmony_cistatic void sci_phy_stopped_state_enter(struct sci_base_state_machine *sm)
125262306a36Sopenharmony_ci{
125362306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
125462306a36Sopenharmony_ci	struct isci_port *iport = iphy->owning_port;
125562306a36Sopenharmony_ci	struct isci_host *ihost = iport->owning_controller;
125662306a36Sopenharmony_ci
125762306a36Sopenharmony_ci	/*
125862306a36Sopenharmony_ci	 * @todo We need to get to the controller to place this PE in a
125962306a36Sopenharmony_ci	 * reset state
126062306a36Sopenharmony_ci	 */
126162306a36Sopenharmony_ci	sci_del_timer(&iphy->sata_timer);
126262306a36Sopenharmony_ci
126362306a36Sopenharmony_ci	scu_link_layer_stop_protocol_engine(iphy);
126462306a36Sopenharmony_ci
126562306a36Sopenharmony_ci	if (iphy->sm.previous_state_id != SCI_PHY_INITIAL)
126662306a36Sopenharmony_ci		sci_controller_link_down(ihost, phy_get_non_dummy_port(iphy), iphy);
126762306a36Sopenharmony_ci}
126862306a36Sopenharmony_ci
126962306a36Sopenharmony_cistatic void sci_phy_starting_state_enter(struct sci_base_state_machine *sm)
127062306a36Sopenharmony_ci{
127162306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
127262306a36Sopenharmony_ci	struct isci_port *iport = iphy->owning_port;
127362306a36Sopenharmony_ci	struct isci_host *ihost = iport->owning_controller;
127462306a36Sopenharmony_ci
127562306a36Sopenharmony_ci	scu_link_layer_stop_protocol_engine(iphy);
127662306a36Sopenharmony_ci	scu_link_layer_start_oob(iphy);
127762306a36Sopenharmony_ci
127862306a36Sopenharmony_ci	/* We don't know what kind of phy we are going to be just yet */
127962306a36Sopenharmony_ci	iphy->protocol = SAS_PROTOCOL_NONE;
128062306a36Sopenharmony_ci	iphy->bcn_received_while_port_unassigned = false;
128162306a36Sopenharmony_ci
128262306a36Sopenharmony_ci	if (iphy->sm.previous_state_id == SCI_PHY_READY)
128362306a36Sopenharmony_ci		sci_controller_link_down(ihost, phy_get_non_dummy_port(iphy), iphy);
128462306a36Sopenharmony_ci
128562306a36Sopenharmony_ci	sci_change_state(&iphy->sm, SCI_PHY_SUB_INITIAL);
128662306a36Sopenharmony_ci}
128762306a36Sopenharmony_ci
128862306a36Sopenharmony_cistatic void sci_phy_ready_state_enter(struct sci_base_state_machine *sm)
128962306a36Sopenharmony_ci{
129062306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
129162306a36Sopenharmony_ci	struct isci_port *iport = iphy->owning_port;
129262306a36Sopenharmony_ci	struct isci_host *ihost = iport->owning_controller;
129362306a36Sopenharmony_ci
129462306a36Sopenharmony_ci	sci_controller_link_up(ihost, phy_get_non_dummy_port(iphy), iphy);
129562306a36Sopenharmony_ci}
129662306a36Sopenharmony_ci
129762306a36Sopenharmony_cistatic void sci_phy_ready_state_exit(struct sci_base_state_machine *sm)
129862306a36Sopenharmony_ci{
129962306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
130062306a36Sopenharmony_ci
130162306a36Sopenharmony_ci	sci_phy_suspend(iphy);
130262306a36Sopenharmony_ci}
130362306a36Sopenharmony_ci
130462306a36Sopenharmony_cistatic void sci_phy_resetting_state_enter(struct sci_base_state_machine *sm)
130562306a36Sopenharmony_ci{
130662306a36Sopenharmony_ci	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
130762306a36Sopenharmony_ci
130862306a36Sopenharmony_ci	/* The phy is being reset, therefore deactivate it from the port.  In
130962306a36Sopenharmony_ci	 * the resetting state we don't notify the user regarding link up and
131062306a36Sopenharmony_ci	 * link down notifications
131162306a36Sopenharmony_ci	 */
131262306a36Sopenharmony_ci	sci_port_deactivate_phy(iphy->owning_port, iphy, false);
131362306a36Sopenharmony_ci
131462306a36Sopenharmony_ci	if (iphy->protocol == SAS_PROTOCOL_SSP) {
131562306a36Sopenharmony_ci		scu_link_layer_tx_hard_reset(iphy);
131662306a36Sopenharmony_ci	} else {
131762306a36Sopenharmony_ci		/* The SCU does not need to have a discrete reset state so
131862306a36Sopenharmony_ci		 * just go back to the starting state.
131962306a36Sopenharmony_ci		 */
132062306a36Sopenharmony_ci		sci_change_state(&iphy->sm, SCI_PHY_STARTING);
132162306a36Sopenharmony_ci	}
132262306a36Sopenharmony_ci}
132362306a36Sopenharmony_ci
132462306a36Sopenharmony_cistatic const struct sci_base_state sci_phy_state_table[] = {
132562306a36Sopenharmony_ci	[SCI_PHY_INITIAL] = { },
132662306a36Sopenharmony_ci	[SCI_PHY_STOPPED] = {
132762306a36Sopenharmony_ci		.enter_state = sci_phy_stopped_state_enter,
132862306a36Sopenharmony_ci	},
132962306a36Sopenharmony_ci	[SCI_PHY_STARTING] = {
133062306a36Sopenharmony_ci		.enter_state = sci_phy_starting_state_enter,
133162306a36Sopenharmony_ci	},
133262306a36Sopenharmony_ci	[SCI_PHY_SUB_INITIAL] = {
133362306a36Sopenharmony_ci		.enter_state = sci_phy_starting_initial_substate_enter,
133462306a36Sopenharmony_ci	},
133562306a36Sopenharmony_ci	[SCI_PHY_SUB_AWAIT_OSSP_EN] = { },
133662306a36Sopenharmony_ci	[SCI_PHY_SUB_AWAIT_SAS_SPEED_EN] = { },
133762306a36Sopenharmony_ci	[SCI_PHY_SUB_AWAIT_IAF_UF] = { },
133862306a36Sopenharmony_ci	[SCI_PHY_SUB_AWAIT_SAS_POWER] = {
133962306a36Sopenharmony_ci		.enter_state = sci_phy_starting_await_sas_power_substate_enter,
134062306a36Sopenharmony_ci		.exit_state  = sci_phy_starting_await_sas_power_substate_exit,
134162306a36Sopenharmony_ci	},
134262306a36Sopenharmony_ci	[SCI_PHY_SUB_AWAIT_SATA_POWER] = {
134362306a36Sopenharmony_ci		.enter_state = sci_phy_starting_await_sata_power_substate_enter,
134462306a36Sopenharmony_ci		.exit_state  = sci_phy_starting_await_sata_power_substate_exit
134562306a36Sopenharmony_ci	},
134662306a36Sopenharmony_ci	[SCI_PHY_SUB_AWAIT_SATA_PHY_EN] = {
134762306a36Sopenharmony_ci		.enter_state = sci_phy_starting_await_sata_phy_substate_enter,
134862306a36Sopenharmony_ci		.exit_state  = sci_phy_starting_await_sata_phy_substate_exit
134962306a36Sopenharmony_ci	},
135062306a36Sopenharmony_ci	[SCI_PHY_SUB_AWAIT_SATA_SPEED_EN] = {
135162306a36Sopenharmony_ci		.enter_state = sci_phy_starting_await_sata_speed_substate_enter,
135262306a36Sopenharmony_ci		.exit_state  = sci_phy_starting_await_sata_speed_substate_exit
135362306a36Sopenharmony_ci	},
135462306a36Sopenharmony_ci	[SCI_PHY_SUB_AWAIT_SIG_FIS_UF] = {
135562306a36Sopenharmony_ci		.enter_state = sci_phy_starting_await_sig_fis_uf_substate_enter,
135662306a36Sopenharmony_ci		.exit_state  = sci_phy_starting_await_sig_fis_uf_substate_exit
135762306a36Sopenharmony_ci	},
135862306a36Sopenharmony_ci	[SCI_PHY_SUB_FINAL] = {
135962306a36Sopenharmony_ci		.enter_state = sci_phy_starting_final_substate_enter,
136062306a36Sopenharmony_ci	},
136162306a36Sopenharmony_ci	[SCI_PHY_READY] = {
136262306a36Sopenharmony_ci		.enter_state = sci_phy_ready_state_enter,
136362306a36Sopenharmony_ci		.exit_state = sci_phy_ready_state_exit,
136462306a36Sopenharmony_ci	},
136562306a36Sopenharmony_ci	[SCI_PHY_RESETTING] = {
136662306a36Sopenharmony_ci		.enter_state = sci_phy_resetting_state_enter,
136762306a36Sopenharmony_ci	},
136862306a36Sopenharmony_ci	[SCI_PHY_FINAL] = { },
136962306a36Sopenharmony_ci};
137062306a36Sopenharmony_ci
137162306a36Sopenharmony_civoid sci_phy_construct(struct isci_phy *iphy,
137262306a36Sopenharmony_ci			    struct isci_port *iport, u8 phy_index)
137362306a36Sopenharmony_ci{
137462306a36Sopenharmony_ci	sci_init_sm(&iphy->sm, sci_phy_state_table, SCI_PHY_INITIAL);
137562306a36Sopenharmony_ci
137662306a36Sopenharmony_ci	/* Copy the rest of the input data to our locals */
137762306a36Sopenharmony_ci	iphy->owning_port = iport;
137862306a36Sopenharmony_ci	iphy->phy_index = phy_index;
137962306a36Sopenharmony_ci	iphy->bcn_received_while_port_unassigned = false;
138062306a36Sopenharmony_ci	iphy->protocol = SAS_PROTOCOL_NONE;
138162306a36Sopenharmony_ci	iphy->link_layer_registers = NULL;
138262306a36Sopenharmony_ci	iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
138362306a36Sopenharmony_ci
138462306a36Sopenharmony_ci	/* Create the SIGNATURE FIS Timeout timer for this phy */
138562306a36Sopenharmony_ci	sci_init_timer(&iphy->sata_timer, phy_sata_timeout);
138662306a36Sopenharmony_ci}
138762306a36Sopenharmony_ci
138862306a36Sopenharmony_civoid isci_phy_init(struct isci_phy *iphy, struct isci_host *ihost, int index)
138962306a36Sopenharmony_ci{
139062306a36Sopenharmony_ci	struct sci_oem_params *oem = &ihost->oem_parameters;
139162306a36Sopenharmony_ci	u64 sci_sas_addr;
139262306a36Sopenharmony_ci	__be64 sas_addr;
139362306a36Sopenharmony_ci
139462306a36Sopenharmony_ci	sci_sas_addr = oem->phys[index].sas_address.high;
139562306a36Sopenharmony_ci	sci_sas_addr <<= 32;
139662306a36Sopenharmony_ci	sci_sas_addr |= oem->phys[index].sas_address.low;
139762306a36Sopenharmony_ci	sas_addr = cpu_to_be64(sci_sas_addr);
139862306a36Sopenharmony_ci	memcpy(iphy->sas_addr, &sas_addr, sizeof(sas_addr));
139962306a36Sopenharmony_ci
140062306a36Sopenharmony_ci	iphy->sas_phy.enabled = 0;
140162306a36Sopenharmony_ci	iphy->sas_phy.id = index;
140262306a36Sopenharmony_ci	iphy->sas_phy.sas_addr = &iphy->sas_addr[0];
140362306a36Sopenharmony_ci	iphy->sas_phy.frame_rcvd = (u8 *)&iphy->frame_rcvd;
140462306a36Sopenharmony_ci	iphy->sas_phy.ha = &ihost->sas_ha;
140562306a36Sopenharmony_ci	iphy->sas_phy.lldd_phy = iphy;
140662306a36Sopenharmony_ci	iphy->sas_phy.enabled = 1;
140762306a36Sopenharmony_ci	iphy->sas_phy.iproto = SAS_PROTOCOL_ALL;
140862306a36Sopenharmony_ci	iphy->sas_phy.tproto = 0;
140962306a36Sopenharmony_ci	iphy->sas_phy.role = PHY_ROLE_INITIATOR;
141062306a36Sopenharmony_ci	iphy->sas_phy.oob_mode = OOB_NOT_CONNECTED;
141162306a36Sopenharmony_ci	iphy->sas_phy.linkrate = SAS_LINK_RATE_UNKNOWN;
141262306a36Sopenharmony_ci	memset(&iphy->frame_rcvd, 0, sizeof(iphy->frame_rcvd));
141362306a36Sopenharmony_ci}
141462306a36Sopenharmony_ci
141562306a36Sopenharmony_ci
141662306a36Sopenharmony_ci/**
141762306a36Sopenharmony_ci * isci_phy_control() - This function is one of the SAS Domain Template
141862306a36Sopenharmony_ci *    functions. This is a phy management function.
141962306a36Sopenharmony_ci * @sas_phy: This parameter specifies the sphy being controlled.
142062306a36Sopenharmony_ci * @func: This parameter specifies the phy control function being invoked.
142162306a36Sopenharmony_ci * @buf: This parameter is specific to the phy function being invoked.
142262306a36Sopenharmony_ci *
142362306a36Sopenharmony_ci * status, zero indicates success.
142462306a36Sopenharmony_ci */
142562306a36Sopenharmony_ciint isci_phy_control(struct asd_sas_phy *sas_phy,
142662306a36Sopenharmony_ci		     enum phy_func func,
142762306a36Sopenharmony_ci		     void *buf)
142862306a36Sopenharmony_ci{
142962306a36Sopenharmony_ci	int ret = 0;
143062306a36Sopenharmony_ci	struct isci_phy *iphy = sas_phy->lldd_phy;
143162306a36Sopenharmony_ci	struct asd_sas_port *port = sas_phy->port;
143262306a36Sopenharmony_ci	struct isci_host *ihost = sas_phy->ha->lldd_ha;
143362306a36Sopenharmony_ci	unsigned long flags;
143462306a36Sopenharmony_ci
143562306a36Sopenharmony_ci	dev_dbg(&ihost->pdev->dev,
143662306a36Sopenharmony_ci		"%s: phy %p; func %d; buf %p; isci phy %p, port %p\n",
143762306a36Sopenharmony_ci		__func__, sas_phy, func, buf, iphy, port);
143862306a36Sopenharmony_ci
143962306a36Sopenharmony_ci	switch (func) {
144062306a36Sopenharmony_ci	case PHY_FUNC_DISABLE:
144162306a36Sopenharmony_ci		spin_lock_irqsave(&ihost->scic_lock, flags);
144262306a36Sopenharmony_ci		scu_link_layer_start_oob(iphy);
144362306a36Sopenharmony_ci		sci_phy_stop(iphy);
144462306a36Sopenharmony_ci		spin_unlock_irqrestore(&ihost->scic_lock, flags);
144562306a36Sopenharmony_ci		break;
144662306a36Sopenharmony_ci
144762306a36Sopenharmony_ci	case PHY_FUNC_LINK_RESET:
144862306a36Sopenharmony_ci		spin_lock_irqsave(&ihost->scic_lock, flags);
144962306a36Sopenharmony_ci		scu_link_layer_start_oob(iphy);
145062306a36Sopenharmony_ci		sci_phy_stop(iphy);
145162306a36Sopenharmony_ci		sci_phy_start(iphy);
145262306a36Sopenharmony_ci		spin_unlock_irqrestore(&ihost->scic_lock, flags);
145362306a36Sopenharmony_ci		break;
145462306a36Sopenharmony_ci
145562306a36Sopenharmony_ci	case PHY_FUNC_HARD_RESET:
145662306a36Sopenharmony_ci		if (!port)
145762306a36Sopenharmony_ci			return -ENODEV;
145862306a36Sopenharmony_ci
145962306a36Sopenharmony_ci		ret = isci_port_perform_hard_reset(ihost, port->lldd_port, iphy);
146062306a36Sopenharmony_ci
146162306a36Sopenharmony_ci		break;
146262306a36Sopenharmony_ci	case PHY_FUNC_GET_EVENTS: {
146362306a36Sopenharmony_ci		struct scu_link_layer_registers __iomem *r;
146462306a36Sopenharmony_ci		struct sas_phy *phy = sas_phy->phy;
146562306a36Sopenharmony_ci
146662306a36Sopenharmony_ci		r = iphy->link_layer_registers;
146762306a36Sopenharmony_ci		phy->running_disparity_error_count = readl(&r->running_disparity_error_count);
146862306a36Sopenharmony_ci		phy->loss_of_dword_sync_count = readl(&r->loss_of_sync_error_count);
146962306a36Sopenharmony_ci		phy->phy_reset_problem_count = readl(&r->phy_reset_problem_count);
147062306a36Sopenharmony_ci		phy->invalid_dword_count = readl(&r->invalid_dword_counter);
147162306a36Sopenharmony_ci		break;
147262306a36Sopenharmony_ci	}
147362306a36Sopenharmony_ci
147462306a36Sopenharmony_ci	default:
147562306a36Sopenharmony_ci		dev_dbg(&ihost->pdev->dev,
147662306a36Sopenharmony_ci			   "%s: phy %p; func %d NOT IMPLEMENTED!\n",
147762306a36Sopenharmony_ci			   __func__, sas_phy, func);
147862306a36Sopenharmony_ci		ret = -ENOSYS;
147962306a36Sopenharmony_ci		break;
148062306a36Sopenharmony_ci	}
148162306a36Sopenharmony_ci	return ret;
148262306a36Sopenharmony_ci}
1483