phy.c 40.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
/* Maximum arbitration wait time in micro-seconds */
#define SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME  (700)

65
enum sas_linkrate sci_phy_linkrate(struct isci_phy *iphy)
D
Dan Williams 已提交
66
{
67
	return iphy->max_negotiated_speed;
D
Dan Williams 已提交
68 69
}

70 71 72
static enum sci_status
sci_phy_transport_layer_initialization(struct isci_phy *iphy,
				       struct scu_transport_layer_registers __iomem *reg)
D
Dan Williams 已提交
73 74 75
{
	u32 tl_control;

76
	iphy->transport_layer_registers = reg;
D
Dan Williams 已提交
77 78

	writel(SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX,
79
		&iphy->transport_layer_registers->stp_rni);
D
Dan Williams 已提交
80 81 82 83 84

	/*
	 * Hardware team recommends that we enable the STP prefetch for all
	 * transports
	 */
85
	tl_control = readl(&iphy->transport_layer_registers->control);
D
Dan Williams 已提交
86
	tl_control |= SCU_TLCR_GEN_BIT(STP_WRITE_DATA_PREFETCH);
87
	writel(tl_control, &iphy->transport_layer_registers->control);
D
Dan Williams 已提交
88 89 90 91 92

	return SCI_SUCCESS;
}

static enum sci_status
93 94
sci_phy_link_layer_initialization(struct isci_phy *iphy,
				  struct scu_link_layer_registers __iomem *reg)
D
Dan Williams 已提交
95
{
96
	struct isci_host *ihost = iphy->owning_port->owning_controller;
97
	int phy_idx = iphy->phy_index;
98
	struct sci_phy_user_params *phy_user = &ihost->user_parameters.phys[phy_idx];
D
Dan Williams 已提交
99
	struct sci_phy_oem_params *phy_oem =
100
		&ihost->oem_parameters.phys[phy_idx];
D
Dan Williams 已提交
101
	u32 phy_configuration;
102
	struct sci_phy_cap phy_cap;
D
Dan Williams 已提交
103 104 105 106 107
	u32 parity_check = 0;
	u32 parity_count = 0;
	u32 llctl, link_rate;
	u32 clksm_value = 0;

108
	iphy->link_layer_registers = reg;
D
Dan Williams 已提交
109 110 111 112 113 114 115 116 117

	/* 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),
118
	       &iphy->link_layer_registers->transmit_identification);
D
Dan Williams 已提交
119 120 121

	/* Write the device SAS Address */
	writel(0xFEDCBA98,
122 123
	       &iphy->link_layer_registers->sas_device_name_high);
	writel(phy_idx, &iphy->link_layer_registers->sas_device_name_low);
D
Dan Williams 已提交
124 125 126

	/* Write the source SAS Address */
	writel(phy_oem->sas_address.high,
127
		&iphy->link_layer_registers->source_sas_address_high);
D
Dan Williams 已提交
128
	writel(phy_oem->sas_address.low,
129
		&iphy->link_layer_registers->source_sas_address_low);
D
Dan Williams 已提交
130 131

	/* Clear and Set the PHY Identifier */
132
	writel(0, &iphy->link_layer_registers->identify_frame_phy_id);
D
Dan Williams 已提交
133
	writel(SCU_SAS_TIPID_GEN_VALUE(ID, phy_idx),
134
		&iphy->link_layer_registers->identify_frame_phy_id);
D
Dan Williams 已提交
135 136 137

	/* Change the initial state of the phy configuration register */
	phy_configuration =
138
		readl(&iphy->link_layer_registers->phy_configuration);
D
Dan Williams 已提交
139 140 141 142

	/* Hold OOB state machine in reset */
	phy_configuration |=  SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
	writel(phy_configuration,
143
		&iphy->link_layer_registers->phy_configuration);
D
Dan Williams 已提交
144 145 146 147 148 149 150

	/* 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;
151
	if (ihost->oem_parameters.controller.do_enable_ssc == true) {
D
Dan Williams 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
		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;

173
	writel(phy_cap.all, &iphy->link_layer_registers->phy_capabilities);
D
Dan Williams 已提交
174 175 176 177 178 179

	/* 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),
180
		&iphy->link_layer_registers->notify_enable_spinup_control);
D
Dan Williams 已提交
181 182 183 184 185 186 187 188 189 190

	/* 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);

191
	writel(clksm_value, &iphy->link_layer_registers->clock_skew_management);
D
Dan Williams 已提交
192 193 194

	/* @todo Provide a way to write this register correctly */
	writel(0x02108421,
195
		&iphy->link_layer_registers->afe_lookup_table_control);
D
Dan Williams 已提交
196 197

	llctl = SCU_SAS_LLCTL_GEN_VAL(NO_OUTBOUND_TASK_TIMEOUT,
198
		(u8)ihost->user_parameters.no_outbound_task_timeout);
D
Dan Williams 已提交
199

200
	switch (phy_user->max_speed_generation) {
D
Dan Williams 已提交
201 202 203 204 205 206 207 208 209 210 211
	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);
212
	writel(llctl, &iphy->link_layer_registers->link_layer_control);
D
Dan Williams 已提交
213

214
	if (is_a2(ihost->pdev)) {
D
Dan Williams 已提交
215 216 217 218 219 220
		/* 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,
221
			&iphy->link_layer_registers->maximum_arbitration_wait_timer_timeout);
D
Dan Williams 已提交
222 223
	}

224
	/* Disable link layer hang detection, rely on the OS timeout for I/O timeouts. */
225
	writel(0, &iphy->link_layer_registers->link_layer_hang_detection_timeout);
D
Dan Williams 已提交
226 227

	/* We can exit the initial state to the stopped state */
228
	sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
D
Dan Williams 已提交
229 230 231 232

	return SCI_SUCCESS;
}

233
static void phy_sata_timeout(unsigned long data)
D
Dan Williams 已提交
234
{
235
	struct sci_timer *tmr = (struct sci_timer *)data;
236
	struct isci_phy *iphy = container_of(tmr, typeof(*iphy), sata_timer);
237
	struct isci_host *ihost = iphy->owning_port->owning_controller;
238 239 240 241 242 243
	unsigned long flags;

	spin_lock_irqsave(&ihost->scic_lock, flags);

	if (tmr->cancel)
		goto done;
D
Dan Williams 已提交
244

245
	dev_dbg(sciphy_to_dev(iphy),
D
Dan Williams 已提交
246 247 248
		 "%s: SCIC SDS Phy 0x%p did not receive signature fis before "
		 "timeout.\n",
		 __func__,
249
		 iphy);
D
Dan Williams 已提交
250

251
	sci_change_state(&iphy->sm, SCI_PHY_STARTING);
252 253
done:
	spin_unlock_irqrestore(&ihost->scic_lock, flags);
D
Dan Williams 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267
}

/**
 * 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 已提交
268
struct isci_port *phy_get_non_dummy_port(struct isci_phy *iphy)
D
Dan Williams 已提交
269
{
D
Dan Williams 已提交
270 271 272
	struct isci_port *iport = iphy->owning_port;

	if (iport->physical_port_index == SCIC_SDS_DUMMY_PORT)
D
Dan Williams 已提交
273 274
		return NULL;

275
	return iphy->owning_port;
D
Dan Williams 已提交
276 277 278 279
}

/**
 * This method will assign a port to the phy object.
280
 * @out]: iphy This parameter specifies the phy for which to assign a port
D
Dan Williams 已提交
281 282 283 284
 *    object.
 *
 *
 */
285
void sci_phy_set_port(
286
	struct isci_phy *iphy,
287
	struct isci_port *iport)
D
Dan Williams 已提交
288
{
289
	iphy->owning_port = iport;
D
Dan Williams 已提交
290

291 292
	if (iphy->bcn_received_while_port_unassigned) {
		iphy->bcn_received_while_port_unassigned = false;
293
		sci_port_broadcast_change_received(iphy->owning_port, iphy);
D
Dan Williams 已提交
294 295 296
	}
}

297 298 299
enum sci_status sci_phy_initialize(struct isci_phy *iphy,
				   struct scu_transport_layer_registers __iomem *tl,
				   struct scu_link_layer_registers __iomem *ll)
D
Dan Williams 已提交
300 301
{
	/* Perfrom the initialization of the TL hardware */
302
	sci_phy_transport_layer_initialization(iphy, tl);
D
Dan Williams 已提交
303 304

	/* Perofrm the initialization of the PE hardware */
305
	sci_phy_link_layer_initialization(iphy, ll);
D
Dan Williams 已提交
306

307 308 309
	/* There is nothing that needs to be done in this state just
	 * transition to the stopped state
	 */
310
	sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
D
Dan Williams 已提交
311 312 313 314 315 316 317

	return SCI_SUCCESS;
}

/**
 * This method assigns the direct attached device ID for this phy.
 *
318
 * @iphy The phy for which the direct attached device id is to
D
Dan Williams 已提交
319 320 321 322 323
 *       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.
 */
324
void sci_phy_setup_transport(struct isci_phy *iphy, u32 device_id)
D
Dan Williams 已提交
325 326 327
{
	u32 tl_control;

328
	writel(device_id, &iphy->transport_layer_registers->stp_rni);
D
Dan Williams 已提交
329 330 331 332 333

	/*
	 * The read should guarantee that the first write gets posted
	 * before the next write
	 */
334
	tl_control = readl(&iphy->transport_layer_registers->control);
D
Dan Williams 已提交
335
	tl_control |= SCU_TLCR_GEN_BIT(CLEAR_TCI_NCQ_MAPPING_TABLE);
336
	writel(tl_control, &iphy->transport_layer_registers->control);
D
Dan Williams 已提交
337 338
}

339
static void sci_phy_suspend(struct isci_phy *iphy)
D
Dan Williams 已提交
340 341 342 343
{
	u32 scu_sas_pcfg_value;

	scu_sas_pcfg_value =
344
		readl(&iphy->link_layer_registers->phy_configuration);
D
Dan Williams 已提交
345 346
	scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
	writel(scu_sas_pcfg_value,
347
		&iphy->link_layer_registers->phy_configuration);
D
Dan Williams 已提交
348

349
	sci_phy_setup_transport(iphy, SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
D
Dan Williams 已提交
350 351
}

352
void sci_phy_resume(struct isci_phy *iphy)
D
Dan Williams 已提交
353 354 355 356
{
	u32 scu_sas_pcfg_value;

	scu_sas_pcfg_value =
357
		readl(&iphy->link_layer_registers->phy_configuration);
D
Dan Williams 已提交
358 359
	scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
	writel(scu_sas_pcfg_value,
360
		&iphy->link_layer_registers->phy_configuration);
D
Dan Williams 已提交
361 362
}

363
void sci_phy_get_sas_address(struct isci_phy *iphy, struct sci_sas_address *sas)
D
Dan Williams 已提交
364
{
365 366
	sas->high = readl(&iphy->link_layer_registers->source_sas_address_high);
	sas->low = readl(&iphy->link_layer_registers->source_sas_address_low);
D
Dan Williams 已提交
367 368
}

369
void sci_phy_get_attached_sas_address(struct isci_phy *iphy, struct sci_sas_address *sas)
D
Dan Williams 已提交
370 371 372 373
{
	struct sas_identify_frame *iaf;

	iaf = &iphy->frame_rcvd.iaf;
374
	memcpy(sas, iaf->sas_addr, SAS_ADDR_SIZE);
D
Dan Williams 已提交
375 376
}

377
void sci_phy_get_protocols(struct isci_phy *iphy, struct sci_phy_proto *proto)
D
Dan Williams 已提交
378
{
379
	proto->all = readl(&iphy->link_layer_registers->transmit_identification);
D
Dan Williams 已提交
380 381
}

382
enum sci_status sci_phy_start(struct isci_phy *iphy)
D
Dan Williams 已提交
383
{
384
	enum sci_phy_states state = iphy->sm.current_state_id;
D
Dan Williams 已提交
385

E
Edmund Nadolski 已提交
386
	if (state != SCI_PHY_STOPPED) {
387
		dev_dbg(sciphy_to_dev(iphy),
D
Dan Williams 已提交
388 389 390 391
			 "%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}

392
	sci_change_state(&iphy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
393
	return SCI_SUCCESS;
D
Dan Williams 已提交
394 395
}

396
enum sci_status sci_phy_stop(struct isci_phy *iphy)
D
Dan Williams 已提交
397
{
398
	enum sci_phy_states state = iphy->sm.current_state_id;
D
Dan Williams 已提交
399 400

	switch (state) {
E
Edmund Nadolski 已提交
401 402 403 404 405 406 407 408 409 410
	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 已提交
411 412
		break;
	default:
413
		dev_dbg(sciphy_to_dev(iphy),
D
Dan Williams 已提交
414 415 416 417
			"%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}

418
	sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
D
Dan Williams 已提交
419
	return SCI_SUCCESS;
D
Dan Williams 已提交
420 421
}

422
enum sci_status sci_phy_reset(struct isci_phy *iphy)
D
Dan Williams 已提交
423
{
424
	enum sci_phy_states state = iphy->sm.current_state_id;
D
Dan Williams 已提交
425

E
Edmund Nadolski 已提交
426
	if (state != SCI_PHY_READY) {
427
		dev_dbg(sciphy_to_dev(iphy),
D
Dan Williams 已提交
428 429 430 431
			"%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}

432
	sci_change_state(&iphy->sm, SCI_PHY_RESETTING);
D
Dan Williams 已提交
433
	return SCI_SUCCESS;
D
Dan Williams 已提交
434 435
}

436
enum sci_status sci_phy_consume_power_handler(struct isci_phy *iphy)
D
Dan Williams 已提交
437
{
438
	enum sci_phy_states state = iphy->sm.current_state_id;
439 440

	switch (state) {
E
Edmund Nadolski 已提交
441
	case SCI_PHY_SUB_AWAIT_SAS_POWER: {
442 443
		u32 enable_spinup;

444
		enable_spinup = readl(&iphy->link_layer_registers->notify_enable_spinup_control);
445
		enable_spinup |= SCU_ENSPINUP_GEN_BIT(ENABLE);
446
		writel(enable_spinup, &iphy->link_layer_registers->notify_enable_spinup_control);
447 448

		/* Change state to the final state this substate machine has run to completion */
449
		sci_change_state(&iphy->sm, SCI_PHY_SUB_FINAL);
450 451 452

		return SCI_SUCCESS;
	}
E
Edmund Nadolski 已提交
453
	case SCI_PHY_SUB_AWAIT_SATA_POWER: {
454 455 456 457
		u32 scu_sas_pcfg_value;

		/* Release the spinup hold state and reset the OOB state machine */
		scu_sas_pcfg_value =
458
			readl(&iphy->link_layer_registers->phy_configuration);
459 460 461 462
		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,
463
			&iphy->link_layer_registers->phy_configuration);
464 465 466 467 468

		/* 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,
469
			&iphy->link_layer_registers->phy_configuration);
470 471

		/* Change state to the final state this substate machine has run to completion */
472
		sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_PHY_EN);
473 474 475 476

		return SCI_SUCCESS;
	}
	default:
477
		dev_dbg(sciphy_to_dev(iphy),
478 479 480
			"%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}
D
Dan Williams 已提交
481 482
}

483
static void sci_phy_start_sas_link_training(struct isci_phy *iphy)
D
Dan Williams 已提交
484
{
485 486 487 488
	/* continue 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.
	 */
D
Dan Williams 已提交
489 490
	u32 phy_control;

491
	phy_control = readl(&iphy->link_layer_registers->phy_configuration);
D
Dan Williams 已提交
492 493
	phy_control |= SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD);
	writel(phy_control,
494
	       &iphy->link_layer_registers->phy_configuration);
D
Dan Williams 已提交
495

496
	sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SAS_SPEED_EN);
D
Dan Williams 已提交
497

498
	iphy->protocol = SCIC_SDS_PHY_PROTOCOL_SAS;
D
Dan Williams 已提交
499 500
}

501
static void sci_phy_start_sata_link_training(struct isci_phy *iphy)
D
Dan Williams 已提交
502
{
503 504 505 506
	/* 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
	 */
507
	sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_POWER);
D
Dan Williams 已提交
508

509
	iphy->protocol = SCIC_SDS_PHY_PROTOCOL_SATA;
D
Dan Williams 已提交
510 511 512
}

/**
513
 * sci_phy_complete_link_training - perform processing common to
D
Dan Williams 已提交
514 515 516 517 518 519 520 521 522
 *    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.
 *
 */
523 524 525
static void sci_phy_complete_link_training(struct isci_phy *iphy,
					   enum sas_linkrate max_link_rate,
					   u32 next_state)
D
Dan Williams 已提交
526
{
527
	iphy->max_negotiated_speed = max_link_rate;
D
Dan Williams 已提交
528

529
	sci_change_state(&iphy->sm, next_state);
D
Dan Williams 已提交
530 531
}

532
enum sci_status sci_phy_event_handler(struct isci_phy *iphy, u32 event_code)
A
Adam Gruchala 已提交
533
{
534
	enum sci_phy_states state = iphy->sm.current_state_id;
A
Adam Gruchala 已提交
535

D
Dan Williams 已提交
536
	switch (state) {
E
Edmund Nadolski 已提交
537
	case SCI_PHY_SUB_AWAIT_OSSP_EN:
D
Dan Williams 已提交
538 539
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_SAS_PHY_DETECTED:
540
			sci_phy_start_sas_link_training(iphy);
541
			iphy->is_in_link_training = true;
D
Dan Williams 已提交
542 543
			break;
		case SCU_EVENT_SATA_SPINUP_HOLD:
544
			sci_phy_start_sata_link_training(iphy);
545
			iphy->is_in_link_training = true;
D
Dan Williams 已提交
546 547
			break;
		default:
548
			dev_dbg(sciphy_to_dev(iphy),
D
Dan Williams 已提交
549 550 551 552 553 554 555
				"%s: PHY starting substate machine received "
				"unexpected event_code %x\n",
				__func__,
				event_code);
			return SCI_FAILURE;
		}
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
556
	case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
D
Dan Williams 已提交
557 558 559 560 561 562 563 564
		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:
565 566
			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_1_5_GBPS,
						       SCI_PHY_SUB_AWAIT_IAF_UF);
D
Dan Williams 已提交
567 568 569
			break;
		case SCU_EVENT_SAS_30:
		case SCU_EVENT_SAS_30_SSC:
570 571
			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_3_0_GBPS,
						       SCI_PHY_SUB_AWAIT_IAF_UF);
D
Dan Williams 已提交
572 573 574
			break;
		case SCU_EVENT_SAS_60:
		case SCU_EVENT_SAS_60_SSC:
575 576
			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_6_0_GBPS,
						       SCI_PHY_SUB_AWAIT_IAF_UF);
D
Dan Williams 已提交
577 578 579 580 581
			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 */
582
			sci_phy_start_sata_link_training(iphy);
D
Dan Williams 已提交
583 584 585
			break;
		case SCU_EVENT_LINK_FAILURE:
			/* Link failure change state back to the starting state */
586
			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
587 588
			break;
		default:
589
			dev_warn(sciphy_to_dev(iphy),
D
Dan Williams 已提交
590 591 592 593 594 595 596 597
				 "%s: PHY starting substate machine received "
				 "unexpected event_code %x\n",
				 __func__, event_code);

			return SCI_FAILURE;
			break;
		}
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
598
	case SCI_PHY_SUB_AWAIT_IAF_UF:
D
Dan Williams 已提交
599 600 601
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_SAS_PHY_DETECTED:
			/* Backup the state machine */
602
			sci_phy_start_sas_link_training(iphy);
D
Dan Williams 已提交
603 604 605 606 607 608
			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
			 */
609
			sci_phy_start_sata_link_training(iphy);
D
Dan Williams 已提交
610 611 612 613 614
			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 */
615
			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
616 617
			break;
		default:
618
			dev_warn(sciphy_to_dev(iphy),
D
Dan Williams 已提交
619 620 621 622 623 624
				 "%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_POWER:
D
Dan Williams 已提交
626 627 628
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_LINK_FAILURE:
			/* Link failure change state back to the starting state */
629
			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
630 631
			break;
		default:
632
			dev_warn(sciphy_to_dev(iphy),
D
Dan Williams 已提交
633 634 635 636 637 638 639
				"%s: PHY starting substate machine received unexpected "
				"event_code %x\n",
				__func__,
				event_code);
			return SCI_FAILURE;
		}
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
640
	case SCI_PHY_SUB_AWAIT_SATA_POWER:
D
Dan Williams 已提交
641 642 643
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_LINK_FAILURE:
			/* Link failure change state back to the starting state */
644
			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
645 646 647 648 649 650 651 652 653 654 655
			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.
			 */
656
			sci_phy_start_sas_link_training(iphy);
D
Dan Williams 已提交
657 658 659
			break;

		default:
660
			dev_warn(sciphy_to_dev(iphy),
D
Dan Williams 已提交
661 662 663
				 "%s: PHY starting substate machine received "
				 "unexpected event_code %x\n",
				 __func__, event_code);
A
Adam Gruchala 已提交
664

D
Dan Williams 已提交
665 666 667
			return SCI_FAILURE;
		}
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
668
	case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
D
Dan Williams 已提交
669 670 671
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_LINK_FAILURE:
			/* Link failure change state back to the starting state */
672
			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
673 674 675 676 677 678 679
			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:
680
			iphy->protocol = SCIC_SDS_PHY_PROTOCOL_SATA;
D
Dan Williams 已提交
681 682

			/* We have received the SATA PHY notification change state */
683
			sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
D
Dan Williams 已提交
684 685 686 687 688
			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.
			 */
689
			sci_phy_start_sas_link_training(iphy);
D
Dan Williams 已提交
690 691
			break;
		default:
692
			dev_warn(sciphy_to_dev(iphy),
D
Dan Williams 已提交
693 694 695 696
				 "%s: PHY starting substate machine received "
				 "unexpected event_code %x\n",
				 __func__,
				 event_code);
A
Adam Gruchala 已提交
697

D
Dan Williams 已提交
698 699 700
			return SCI_FAILURE;;
		}
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
701
	case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
D
Dan Williams 已提交
702 703 704 705 706 707 708 709
		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:
710 711
			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_1_5_GBPS,
						       SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
D
Dan Williams 已提交
712 713 714
			break;
		case SCU_EVENT_SATA_30:
		case SCU_EVENT_SATA_30_SSC:
715 716
			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_3_0_GBPS,
						       SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
D
Dan Williams 已提交
717 718 719
			break;
		case SCU_EVENT_SATA_60:
		case SCU_EVENT_SATA_60_SSC:
720 721
			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_6_0_GBPS,
						       SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
D
Dan Williams 已提交
722 723 724
			break;
		case SCU_EVENT_LINK_FAILURE:
			/* Link failure change state back to the starting state */
725
			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
726 727 728 729 730
			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. */
731
			sci_phy_start_sas_link_training(iphy);
D
Dan Williams 已提交
732 733
			break;
		default:
734
			dev_warn(sciphy_to_dev(iphy),
D
Dan Williams 已提交
735 736 737
				 "%s: PHY starting substate machine received "
				 "unexpected event_code %x\n",
				 __func__, event_code);
A
Adam Gruchala 已提交
738

D
Dan Williams 已提交
739 740 741 742
			return SCI_FAILURE;
		}

		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
743
	case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
D
Dan Williams 已提交
744 745 746
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_SATA_PHY_DETECTED:
			/* Backup the state machine */
747
			sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
D
Dan Williams 已提交
748
			break;
A
Adam Gruchala 已提交
749

D
Dan Williams 已提交
750 751
		case SCU_EVENT_LINK_FAILURE:
			/* Link failure change state back to the starting state */
752
			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
753 754 755
			break;

		default:
756
			dev_warn(sciphy_to_dev(iphy),
D
Dan Williams 已提交
757 758 759 760 761 762 763 764
				 "%s: PHY starting substate machine received "
				 "unexpected event_code %x\n",
				 __func__,
				 event_code);

			return SCI_FAILURE;
		}
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
765
	case SCI_PHY_READY:
D
Dan Williams 已提交
766 767 768
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_LINK_FAILURE:
			/* Link failure change state back to the starting state */
769
			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
770 771 772
			break;
		case SCU_EVENT_BROADCAST_CHANGE:
			/* Broadcast change received. Notify the port. */
773
			if (phy_get_non_dummy_port(iphy) != NULL)
774
				sci_port_broadcast_change_received(iphy->owning_port, iphy);
D
Dan Williams 已提交
775
			else
776
				iphy->bcn_received_while_port_unassigned = true;
D
Dan Williams 已提交
777 778
			break;
		default:
779
			dev_warn(sciphy_to_dev(iphy),
D
Dan Williams 已提交
780 781
				 "%sP SCIC PHY 0x%p ready state machine received "
				 "unexpected event_code %x\n",
782
				 __func__, iphy, event_code);
D
Dan Williams 已提交
783 784 785
			return SCI_FAILURE_INVALID_STATE;
		}
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
786
	case SCI_PHY_RESETTING:
D
Dan Williams 已提交
787 788 789
		switch (scu_get_event_code(event_code)) {
		case SCU_EVENT_HARD_RESET_TRANSMITTED:
			/* Link failure change state back to the starting state */
790
			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
791 792
			break;
		default:
793
			dev_warn(sciphy_to_dev(iphy),
D
Dan Williams 已提交
794 795
				 "%s: SCIC PHY 0x%p resetting state machine received "
				 "unexpected event_code %x\n",
796
				 __func__, iphy, event_code);
D
Dan Williams 已提交
797 798 799 800 801 802

			return SCI_FAILURE_INVALID_STATE;
			break;
		}
		return SCI_SUCCESS;
	default:
803
		dev_dbg(sciphy_to_dev(iphy),
D
Dan Williams 已提交
804 805 806
			"%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}
A
Adam Gruchala 已提交
807 808
}

809
enum sci_status sci_phy_frame_handler(struct isci_phy *iphy, u32 frame_index)
A
Adam Gruchala 已提交
810
{
811
	enum sci_phy_states state = iphy->sm.current_state_id;
812
	struct isci_host *ihost = iphy->owning_port->owning_controller;
D
Dan Williams 已提交
813
	enum sci_status result;
D
Dan Williams 已提交
814
	unsigned long flags;
A
Adam Gruchala 已提交
815

D
Dan Williams 已提交
816
	switch (state) {
E
Edmund Nadolski 已提交
817
	case SCI_PHY_SUB_AWAIT_IAF_UF: {
D
Dan Williams 已提交
818 819
		u32 *frame_words;
		struct sas_identify_frame iaf;
A
Adam Gruchala 已提交
820

821 822 823
		result = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
								  frame_index,
								  (void **)&frame_words);
A
Adam Gruchala 已提交
824

D
Dan Williams 已提交
825 826 827 828 829 830 831
		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 已提交
832
			spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
D
Dan Williams 已提交
833
			memcpy(&iphy->frame_rcvd.iaf, &iaf, sizeof(iaf));
D
Dan Williams 已提交
834
			spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
D
Dan Williams 已提交
835 836 837 838 839
			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 已提交
840
				state = SCI_PHY_SUB_FINAL;
D
Dan Williams 已提交
841 842 843 844
			} else {
				/* We got the IAF we can now go to the await spinup
				 * semaphore state
				 */
E
Edmund Nadolski 已提交
845
				state = SCI_PHY_SUB_AWAIT_SAS_POWER;
D
Dan Williams 已提交
846
			}
847
			sci_change_state(&iphy->sm, state);
D
Dan Williams 已提交
848 849
			result = SCI_SUCCESS;
		} else
850
			dev_warn(sciphy_to_dev(iphy),
D
Dan Williams 已提交
851 852 853 854
				"%s: PHY starting substate machine received "
				"unexpected frame id %x\n",
				__func__, frame_index);

855
		sci_controller_release_frame(ihost, frame_index);
D
Dan Williams 已提交
856
		return result;
A
Adam Gruchala 已提交
857
	}
E
Edmund Nadolski 已提交
858
	case SCI_PHY_SUB_AWAIT_SIG_FIS_UF: {
D
Dan Williams 已提交
859 860 861
		struct dev_to_host_fis *frame_header;
		u32 *fis_frame_data;

D
Dan Williams 已提交
862 863 864
		result = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
								  frame_index,
								  (void **)&frame_header);
D
Dan Williams 已提交
865 866 867

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

D
Dan Williams 已提交
869 870
		if ((frame_header->fis_type == FIS_REGD2H) &&
		    !(frame_header->status & ATA_BUSY)) {
871 872 873
			sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
								 frame_index,
								 (void **)&fis_frame_data);
D
Dan Williams 已提交
874

D
Dan Williams 已提交
875
			spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
876 877 878
			sci_controller_copy_sata_response(&iphy->frame_rcvd.fis,
							  frame_header,
							  fis_frame_data);
D
Dan Williams 已提交
879
			spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
D
Dan Williams 已提交
880 881

			/* got IAF we can now go to the await spinup semaphore state */
882
			sci_change_state(&iphy->sm, SCI_PHY_SUB_FINAL);
D
Dan Williams 已提交
883 884 885

			result = SCI_SUCCESS;
		} else
886
			dev_warn(sciphy_to_dev(iphy),
D
Dan Williams 已提交
887 888 889 890 891
				 "%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 */
892
		sci_controller_release_frame(ihost, frame_index);
D
Dan Williams 已提交
893 894 895 896

		return result;
	}
	default:
897
		dev_dbg(sciphy_to_dev(iphy),
D
Dan Williams 已提交
898 899 900
			"%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}
901

A
Adam Gruchala 已提交
902 903
}

904
static void sci_phy_starting_initial_substate_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
905
{
906
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
D
Dan Williams 已提交
907 908

	/* This is just an temporary state go off to the starting state */
909
	sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_OSSP_EN);
D
Dan Williams 已提交
910 911
}

912
static void sci_phy_starting_await_sas_power_substate_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
913
{
914
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
915
	struct isci_host *ihost = iphy->owning_port->owning_controller;
D
Dan Williams 已提交
916

917
	sci_controller_power_control_queue_insert(ihost, iphy);
D
Dan Williams 已提交
918 919
}

920
static void sci_phy_starting_await_sas_power_substate_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
921
{
922
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
923
	struct isci_host *ihost = iphy->owning_port->owning_controller;
D
Dan Williams 已提交
924

925
	sci_controller_power_control_queue_remove(ihost, iphy);
D
Dan Williams 已提交
926 927
}

928
static void sci_phy_starting_await_sata_power_substate_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
929
{
930
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
931
	struct isci_host *ihost = iphy->owning_port->owning_controller;
D
Dan Williams 已提交
932

933
	sci_controller_power_control_queue_insert(ihost, iphy);
D
Dan Williams 已提交
934 935
}

936
static void sci_phy_starting_await_sata_power_substate_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
937
{
938
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
939
	struct isci_host *ihost = iphy->owning_port->owning_controller;
D
Dan Williams 已提交
940

941
	sci_controller_power_control_queue_remove(ihost, iphy);
D
Dan Williams 已提交
942 943
}

944
static void sci_phy_starting_await_sata_phy_substate_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
945
{
946
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
D
Dan Williams 已提交
947

948
	sci_mod_timer(&iphy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
D
Dan Williams 已提交
949 950
}

951
static void sci_phy_starting_await_sata_phy_substate_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
952
{
953
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
D
Dan Williams 已提交
954

955
	sci_del_timer(&iphy->sata_timer);
D
Dan Williams 已提交
956 957
}

958
static void sci_phy_starting_await_sata_speed_substate_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
959
{
960
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
D
Dan Williams 已提交
961

962
	sci_mod_timer(&iphy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
D
Dan Williams 已提交
963 964
}

965
static void sci_phy_starting_await_sata_speed_substate_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
966
{
967
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
D
Dan Williams 已提交
968

969
	sci_del_timer(&iphy->sata_timer);
D
Dan Williams 已提交
970 971
}

972
static void sci_phy_starting_await_sig_fis_uf_substate_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
973
{
974
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
D
Dan Williams 已提交
975

976
	if (sci_port_link_detected(iphy->owning_port, iphy)) {
D
Dan Williams 已提交
977 978 979 980 981 982 983

		/*
		 * 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.
		 */
984
		sci_phy_resume(iphy);
D
Dan Williams 已提交
985

986
		sci_mod_timer(&iphy->sata_timer,
987
			      SCIC_SDS_SIGNATURE_FIS_TIMEOUT);
D
Dan Williams 已提交
988
	} else
989
		iphy->is_in_link_training = false;
D
Dan Williams 已提交
990 991
}

992
static void sci_phy_starting_await_sig_fis_uf_substate_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
993
{
994
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
D
Dan Williams 已提交
995

996
	sci_del_timer(&iphy->sata_timer);
D
Dan Williams 已提交
997 998
}

999
static void sci_phy_starting_final_substate_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1000
{
1001
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
D
Dan Williams 已提交
1002 1003 1004 1005

	/* State machine has run to completion so exit out and change
	 * the base state machine to the ready state
	 */
1006
	sci_change_state(&iphy->sm, SCI_PHY_READY);
D
Dan Williams 已提交
1007 1008 1009 1010
}

/**
 *
1011
 * @sci_phy: This is the struct isci_phy object to stop.
D
Dan Williams 已提交
1012
 *
1013
 * This method will stop the struct isci_phy object. This does not reset the
D
Dan Williams 已提交
1014 1015 1016 1017
 * 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(
1018
	struct isci_phy *iphy)
D
Dan Williams 已提交
1019 1020 1021 1022 1023 1024
{
	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 =
1025
		readl(&iphy->link_layer_registers->phy_configuration);
D
Dan Williams 已提交
1026 1027 1028 1029 1030
	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,
1031
	       &iphy->link_layer_registers->phy_configuration);
D
Dan Williams 已提交
1032 1033

	/* Disable the notify enable spinup primitives */
1034
	enable_spinup_value = readl(&iphy->link_layer_registers->notify_enable_spinup_control);
D
Dan Williams 已提交
1035
	enable_spinup_value &= ~SCU_ENSPINUP_GEN_BIT(ENABLE);
1036
	writel(enable_spinup_value, &iphy->link_layer_registers->notify_enable_spinup_control);
D
Dan Williams 已提交
1037 1038 1039 1040 1041
}

/**
 *
 *
1042
 * This method will start the OOB/SN state machine for this struct isci_phy object.
D
Dan Williams 已提交
1043 1044
 */
static void scu_link_layer_start_oob(
1045
	struct isci_phy *iphy)
D
Dan Williams 已提交
1046 1047 1048 1049
{
	u32 scu_sas_pcfg_value;

	scu_sas_pcfg_value =
1050
		readl(&iphy->link_layer_registers->phy_configuration);
D
Dan Williams 已提交
1051 1052 1053 1054 1055
	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,
1056
	       &iphy->link_layer_registers->phy_configuration);
D
Dan Williams 已提交
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
}

/**
 *
 *
 * 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(
1068
	struct isci_phy *iphy)
D
Dan Williams 已提交
1069 1070 1071 1072 1073 1074 1075
{
	u32 phy_configuration_value;

	/*
	 * SAS Phys must wait for the HARD_RESET_TX event notification to transition
	 * to the starting state. */
	phy_configuration_value =
1076
		readl(&iphy->link_layer_registers->phy_configuration);
D
Dan Williams 已提交
1077 1078 1079 1080
	phy_configuration_value |=
		(SCU_SAS_PCFG_GEN_BIT(HARD_RESET) |
		 SCU_SAS_PCFG_GEN_BIT(OOB_RESET));
	writel(phy_configuration_value,
1081
	       &iphy->link_layer_registers->phy_configuration);
D
Dan Williams 已提交
1082 1083 1084 1085 1086

	/* 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,
1087
	       &iphy->link_layer_registers->phy_configuration);
D
Dan Williams 已提交
1088 1089
}

1090
static void sci_phy_stopped_state_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1091
{
1092
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
D
Dan Williams 已提交
1093 1094
	struct isci_port *iport = iphy->owning_port;
	struct isci_host *ihost = iport->owning_controller;
D
Dan Williams 已提交
1095 1096 1097 1098 1099

	/*
	 * @todo We need to get to the controller to place this PE in a
	 * reset state
	 */
1100
	sci_del_timer(&iphy->sata_timer);
D
Dan Williams 已提交
1101

1102
	scu_link_layer_stop_protocol_engine(iphy);
D
Dan Williams 已提交
1103

1104
	if (iphy->sm.previous_state_id != SCI_PHY_INITIAL)
D
Dan Williams 已提交
1105
		sci_controller_link_down(ihost, phy_get_non_dummy_port(iphy), iphy);
D
Dan Williams 已提交
1106 1107
}

1108
static void sci_phy_starting_state_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1109
{
1110
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
D
Dan Williams 已提交
1111 1112
	struct isci_port *iport = iphy->owning_port;
	struct isci_host *ihost = iport->owning_controller;
D
Dan Williams 已提交
1113

1114 1115
	scu_link_layer_stop_protocol_engine(iphy);
	scu_link_layer_start_oob(iphy);
D
Dan Williams 已提交
1116 1117

	/* We don't know what kind of phy we are going to be just yet */
1118 1119
	iphy->protocol = SCIC_SDS_PHY_PROTOCOL_UNKNOWN;
	iphy->bcn_received_while_port_unassigned = false;
D
Dan Williams 已提交
1120

1121
	if (iphy->sm.previous_state_id == SCI_PHY_READY)
D
Dan Williams 已提交
1122
		sci_controller_link_down(ihost, phy_get_non_dummy_port(iphy), iphy);
A
Adam Gruchala 已提交
1123

1124
	sci_change_state(&iphy->sm, SCI_PHY_SUB_INITIAL);
D
Dan Williams 已提交
1125 1126
}

1127
static void sci_phy_ready_state_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1128
{
1129
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
D
Dan Williams 已提交
1130 1131
	struct isci_port *iport = iphy->owning_port;
	struct isci_host *ihost = iport->owning_controller;
D
Dan Williams 已提交
1132

D
Dan Williams 已提交
1133
	sci_controller_link_up(ihost, phy_get_non_dummy_port(iphy), iphy);
D
Dan Williams 已提交
1134 1135
}

1136
static void sci_phy_ready_state_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1137
{
1138
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
D
Dan Williams 已提交
1139

1140
	sci_phy_suspend(iphy);
D
Dan Williams 已提交
1141 1142
}

1143
static void sci_phy_resetting_state_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1144
{
1145
	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
D
Dan Williams 已提交
1146

1147 1148 1149 1150
	/* 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
	 */
1151
	sci_port_deactivate_phy(iphy->owning_port, iphy, false);
D
Dan Williams 已提交
1152

1153 1154
	if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
		scu_link_layer_tx_hard_reset(iphy);
D
Dan Williams 已提交
1155
	} else {
1156
		/* The SCU does not need to have a discrete reset state so
D
Dan Williams 已提交
1157 1158
		 * just go back to the starting state.
		 */
1159
		sci_change_state(&iphy->sm, SCI_PHY_STARTING);
D
Dan Williams 已提交
1160 1161 1162
	}
}

1163
static const struct sci_base_state sci_phy_state_table[] = {
E
Edmund Nadolski 已提交
1164 1165
	[SCI_PHY_INITIAL] = { },
	[SCI_PHY_STOPPED] = {
1166
		.enter_state = sci_phy_stopped_state_enter,
D
Dan Williams 已提交
1167
	},
E
Edmund Nadolski 已提交
1168
	[SCI_PHY_STARTING] = {
1169
		.enter_state = sci_phy_starting_state_enter,
D
Dan Williams 已提交
1170
	},
E
Edmund Nadolski 已提交
1171
	[SCI_PHY_SUB_INITIAL] = {
1172
		.enter_state = sci_phy_starting_initial_substate_enter,
A
Adam Gruchala 已提交
1173
	},
E
Edmund Nadolski 已提交
1174 1175 1176 1177
	[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] = {
1178 1179
		.enter_state = sci_phy_starting_await_sas_power_substate_enter,
		.exit_state  = sci_phy_starting_await_sas_power_substate_exit,
A
Adam Gruchala 已提交
1180
	},
E
Edmund Nadolski 已提交
1181
	[SCI_PHY_SUB_AWAIT_SATA_POWER] = {
1182 1183
		.enter_state = sci_phy_starting_await_sata_power_substate_enter,
		.exit_state  = sci_phy_starting_await_sata_power_substate_exit
A
Adam Gruchala 已提交
1184
	},
E
Edmund Nadolski 已提交
1185
	[SCI_PHY_SUB_AWAIT_SATA_PHY_EN] = {
1186 1187
		.enter_state = sci_phy_starting_await_sata_phy_substate_enter,
		.exit_state  = sci_phy_starting_await_sata_phy_substate_exit
A
Adam Gruchala 已提交
1188
	},
E
Edmund Nadolski 已提交
1189
	[SCI_PHY_SUB_AWAIT_SATA_SPEED_EN] = {
1190 1191
		.enter_state = sci_phy_starting_await_sata_speed_substate_enter,
		.exit_state  = sci_phy_starting_await_sata_speed_substate_exit
A
Adam Gruchala 已提交
1192
	},
E
Edmund Nadolski 已提交
1193
	[SCI_PHY_SUB_AWAIT_SIG_FIS_UF] = {
1194 1195
		.enter_state = sci_phy_starting_await_sig_fis_uf_substate_enter,
		.exit_state  = sci_phy_starting_await_sig_fis_uf_substate_exit
A
Adam Gruchala 已提交
1196
	},
E
Edmund Nadolski 已提交
1197
	[SCI_PHY_SUB_FINAL] = {
1198
		.enter_state = sci_phy_starting_final_substate_enter,
A
Adam Gruchala 已提交
1199
	},
E
Edmund Nadolski 已提交
1200
	[SCI_PHY_READY] = {
1201 1202
		.enter_state = sci_phy_ready_state_enter,
		.exit_state = sci_phy_ready_state_exit,
D
Dan Williams 已提交
1203
	},
E
Edmund Nadolski 已提交
1204
	[SCI_PHY_RESETTING] = {
1205
		.enter_state = sci_phy_resetting_state_enter,
D
Dan Williams 已提交
1206
	},
E
Edmund Nadolski 已提交
1207
	[SCI_PHY_FINAL] = { },
D
Dan Williams 已提交
1208 1209
};

1210
void sci_phy_construct(struct isci_phy *iphy,
1211
			    struct isci_port *iport, u8 phy_index)
D
Dan Williams 已提交
1212
{
1213
	sci_init_sm(&iphy->sm, sci_phy_state_table, SCI_PHY_INITIAL);
D
Dan Williams 已提交
1214 1215

	/* Copy the rest of the input data to our locals */
1216
	iphy->owning_port = iport;
1217 1218 1219 1220 1221
	iphy->phy_index = phy_index;
	iphy->bcn_received_while_port_unassigned = false;
	iphy->protocol = SCIC_SDS_PHY_PROTOCOL_UNKNOWN;
	iphy->link_layer_registers = NULL;
	iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
1222 1223

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

D
Dan Williams 已提交
1227
void isci_phy_init(struct isci_phy *iphy, struct isci_host *ihost, int index)
1228
{
1229
	struct sci_oem_params *oem = &ihost->oem_parameters;
D
Dan Williams 已提交
1230 1231 1232
	u64 sci_sas_addr;
	__be64 sas_addr;

1233
	sci_sas_addr = oem->phys[index].sas_address.high;
D
Dan Williams 已提交
1234
	sci_sas_addr <<= 32;
1235
	sci_sas_addr |= oem->phys[index].sas_address.low;
D
Dan Williams 已提交
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
	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));
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
}


/**
 * 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.
 */
1267 1268 1269
int isci_phy_control(struct asd_sas_phy *sas_phy,
		     enum phy_func func,
		     void *buf)
1270
{
1271 1272 1273 1274 1275
	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;
1276

1277 1278 1279
	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);
1280 1281

	switch (func) {
1282 1283
	case PHY_FUNC_DISABLE:
		spin_lock_irqsave(&ihost->scic_lock, flags);
1284
		sci_phy_stop(iphy);
1285 1286 1287
		spin_unlock_irqrestore(&ihost->scic_lock, flags);
		break;

1288
	case PHY_FUNC_LINK_RESET:
1289
		spin_lock_irqsave(&ihost->scic_lock, flags);
1290 1291
		sci_phy_stop(iphy);
		sci_phy_start(iphy);
1292 1293 1294 1295 1296 1297
		spin_unlock_irqrestore(&ihost->scic_lock, flags);
		break;

	case PHY_FUNC_HARD_RESET:
		if (!iport)
			return -ENODEV;
1298 1299

		/* Perform the port reset. */
1300
		ret = isci_port_perform_hard_reset(ihost, iport, iphy);
1301 1302 1303 1304

		break;

	default:
1305 1306 1307 1308
		dev_dbg(&ihost->pdev->dev,
			   "%s: phy %p; func %d NOT IMPLEMENTED!\n",
			   __func__, sas_phy, func);
		ret = -ENOSYS;
1309 1310 1311 1312
		break;
	}
	return ret;
}