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

#include "isci.h"
#include "port.h"
#include "request.h"
D
Dan Williams 已提交
59 60 61

#define SCIC_SDS_PORT_HARD_RESET_TIMEOUT  (1000)
#define SCU_DUMMY_INDEX    (0xFFFF)
62

D
Dan Williams 已提交
63 64 65
static void isci_port_change_state(struct isci_port *iport, enum isci_status status)
{
	unsigned long flags;
66

D
Dan Williams 已提交
67 68 69
	dev_dbg(&iport->isci_host->pdev->dev,
		"%s: iport = %p, state = 0x%x\n",
		__func__, iport, status);
70

D
Dan Williams 已提交
71 72 73 74 75
	/* XXX pointless lock */
	spin_lock_irqsave(&iport->state_lock, flags);
	iport->status = status;
	spin_unlock_irqrestore(&iport->state_lock, flags);
}
76

77
static void sci_port_get_protocols(struct isci_port *iport, struct sci_phy_proto *proto)
78
{
D
Dan Williams 已提交
79 80
	u8 index;

81
	proto->all = 0;
D
Dan Williams 已提交
82
	for (index = 0; index < SCI_MAX_PHYS; index++) {
83 84 85 86 87
		struct isci_phy *iphy = iport->phy_table[index];

		if (!iphy)
			continue;
		sci_phy_get_protocols(iphy, proto);
D
Dan Williams 已提交
88
	}
89 90
}

91
static u32 sci_port_get_phys(struct isci_port *iport)
92
{
D
Dan Williams 已提交
93 94 95 96
	u32 index;
	u32 mask;

	mask = 0;
97 98
	for (index = 0; index < SCI_MAX_PHYS; index++)
		if (iport->phy_table[index])
D
Dan Williams 已提交
99 100 101
			mask |= (1 << index);

	return mask;
102 103
}

D
Dan Williams 已提交
104
/**
105
 * sci_port_get_properties() - This method simply returns the properties
D
Dan Williams 已提交
106 107 108 109 110 111 112 113 114 115 116
 *    regarding the port, such as: physical index, protocols, sas address, etc.
 * @port: this parameter specifies the port for which to retrieve the physical
 *    index.
 * @properties: This parameter specifies the properties structure into which to
 *    copy the requested information.
 *
 * Indicate if the user specified a valid port. SCI_SUCCESS This value is
 * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
 * value is returned if the specified port is not valid.  When this value is
 * returned, no data is copied to the properties output parameter.
 */
117 118
static enum sci_status sci_port_get_properties(struct isci_port *iport,
						struct sci_port_properties *prop)
119
{
120
	if (!iport || iport->logical_port_index == SCIC_SDS_DUMMY_PORT)
D
Dan Williams 已提交
121
		return SCI_FAILURE_INVALID_PORT;
122

123 124 125 126 127
	prop->index = iport->logical_port_index;
	prop->phy_mask = sci_port_get_phys(iport);
	sci_port_get_sas_address(iport, &prop->local.sas_address);
	sci_port_get_protocols(iport, &prop->local.protocols);
	sci_port_get_attached_sas_address(iport, &prop->remote.sas_address);
128

D
Dan Williams 已提交
129
	return SCI_SUCCESS;
130 131
}

132
static void sci_port_bcn_enable(struct isci_port *iport)
133
{
134
	struct isci_phy *iphy;
135 136 137
	u32 val;
	int i;

138 139
	for (i = 0; i < ARRAY_SIZE(iport->phy_table); i++) {
		iphy = iport->phy_table[i];
140
		if (!iphy)
141
			continue;
142
		val = readl(&iphy->link_layer_registers->link_layer_control);
143
		/* clear the bit by writing 1. */
144
		writel(val, &iphy->link_layer_registers->link_layer_control);
145 146 147
	}
}

148
/* called under sci_lock to stabilize phy:port associations */
149 150 151 152 153 154 155 156 157 158
void isci_port_bcn_enable(struct isci_host *ihost, struct isci_port *iport)
{
	int i;

	clear_bit(IPORT_BCN_BLOCKED, &iport->flags);
	wake_up(&ihost->eventq);

	if (!test_and_clear_bit(IPORT_BCN_PENDING, &iport->flags))
		return;

159 160
	for (i = 0; i < ARRAY_SIZE(iport->phy_table); i++) {
		struct isci_phy *iphy = iport->phy_table[i];
161

162
		if (!iphy)
163 164 165 166 167 168 169 170
			continue;

		ihost->sas_ha.notify_port_event(&iphy->sas_phy,
						PORTE_BROADCAST_RCVD);
		break;
	}
}

171 172 173
static void isci_port_bc_change_received(struct isci_host *ihost,
					 struct isci_port *iport,
					 struct isci_phy *iphy)
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
{
	if (iport && test_bit(IPORT_BCN_BLOCKED, &iport->flags)) {
		dev_dbg(&ihost->pdev->dev,
			"%s: disabled BCN; isci_phy = %p, sas_phy = %p\n",
			__func__, iphy, &iphy->sas_phy);
		set_bit(IPORT_BCN_PENDING, &iport->flags);
		atomic_inc(&iport->event);
		wake_up(&ihost->eventq);
	} else {
		dev_dbg(&ihost->pdev->dev,
			"%s: isci_phy = %p, sas_phy = %p\n",
			__func__, iphy, &iphy->sas_phy);

		ihost->sas_ha.notify_port_event(&iphy->sas_phy,
						PORTE_BROADCAST_RCVD);
	}
190
	sci_port_bcn_enable(iport);
191 192
}

D
Dan Williams 已提交
193
static void isci_port_link_up(struct isci_host *isci_host,
194
			      struct isci_port *iport,
195
			      struct isci_phy *iphy)
196 197
{
	unsigned long flags;
198
	struct sci_port_properties properties;
199 200
	unsigned long success = true;

201
	BUG_ON(iphy->isci_port != NULL);
202

203
	iphy->isci_port = iport;
204 205 206

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

209
	spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
210

211
	isci_port_change_state(iphy->isci_port, isci_starting);
212

213
	sci_port_get_properties(iport, &properties);
214

215
	if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
D
Dan Williams 已提交
216
		u64 attached_sas_address;
217

218 219
		iphy->sas_phy.oob_mode = SATA_OOB_MODE;
		iphy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
220 221 222 223 224 225

		/*
		 * For direct-attached SATA devices, the SCI core will
		 * automagically assign a SAS address to the end device
		 * for the purpose of creating a port. This SAS address
		 * will not be the same as assigned to the PHY and needs
226
		 * to be obtained from struct sci_port_properties properties.
227
		 */
D
Dan Williams 已提交
228 229 230 231
		attached_sas_address = properties.remote.sas_address.high;
		attached_sas_address <<= 32;
		attached_sas_address |= properties.remote.sas_address.low;
		swab64s(&attached_sas_address);
232

233
		memcpy(&iphy->sas_phy.attached_sas_addr,
D
Dan Williams 已提交
234
		       &attached_sas_address, sizeof(attached_sas_address));
235 236 237
	} else if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
		iphy->sas_phy.oob_mode = SAS_OOB_MODE;
		iphy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
238 239

		/* Copy the attached SAS address from the IAF */
240 241
		memcpy(iphy->sas_phy.attached_sas_addr,
		       iphy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
242 243 244 245 246
	} else {
		dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
		success = false;
	}

247
	iphy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(iphy);
D
Dan Williams 已提交
248

249
	spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
250 251 252 253

	/* Notify libsas that we have an address frame, if indeed
	 * we've found an SSP, SMP, or STP target */
	if (success)
254
		isci_host->sas_ha.notify_port_event(&iphy->sas_phy,
255 256 257 258 259 260 261 262 263 264 265 266
						    PORTE_BYTES_DMAED);
}


/**
 * isci_port_link_down() - This function is called by the sci core when a link
 *    becomes inactive.
 * @isci_host: This parameter specifies the isci host object.
 * @phy: This parameter specifies the isci phy with the active link.
 * @port: This parameter specifies the isci port with the active link.
 *
 */
D
Dan Williams 已提交
267 268 269
static void isci_port_link_down(struct isci_host *isci_host,
				struct isci_phy *isci_phy,
				struct isci_port *isci_port)
270 271 272 273 274 275 276 277 278
{
	struct isci_remote_device *isci_device;

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

	if (isci_port) {

		/* check to see if this is the last phy on this port. */
279 280 281 282 283 284 285 286 287
		if (isci_phy->sas_phy.port &&
		    isci_phy->sas_phy.port->num_phys == 1) {
			atomic_inc(&isci_port->event);
			isci_port_bcn_enable(isci_host, isci_port);

			/* change the state for all devices on this port.  The
			 * next task sent to this device will be returned as
			 * SAS_TASK_UNDELIVERED, and the scsi mid layer will
			 * remove the target
288 289 290 291 292 293 294
			 */
			list_for_each_entry(isci_device,
					    &isci_port->remote_dev_list,
					    node) {
				dev_dbg(&isci_host->pdev->dev,
					"%s: isci_device = %p\n",
					__func__, isci_device);
295
				set_bit(IDEV_GONE, &isci_device->flags);
296
			}
297
			isci_port_change_state(isci_port, isci_stopping);
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
		}
	}

	/* Notify libsas of the borken link, this will trigger calls to our
	 * isci_port_deformed and isci_dev_gone functions.
	 */
	sas_phy_disconnected(&isci_phy->sas_phy);
	isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
					   PHYE_LOSS_OF_SIGNAL);

	isci_phy->isci_port = NULL;

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


/**
 * isci_port_ready() - This function is called by the sci core when a link
 *    becomes ready.
 * @isci_host: This parameter specifies the isci host object.
 * @port: This parameter specifies the sci port with the active link.
 *
 */
D
Dan Williams 已提交
322
static void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
{
	dev_dbg(&isci_host->pdev->dev,
		"%s: isci_port = %p\n", __func__, isci_port);

	complete_all(&isci_port->start_complete);
	isci_port_change_state(isci_port, isci_ready);
	return;
}

/**
 * isci_port_not_ready() - This function is called by the sci core when a link
 *    is not ready. All remote devices on this link will be removed if they are
 *    in the stopping state.
 * @isci_host: This parameter specifies the isci host object.
 * @port: This parameter specifies the sci port with the active link.
 *
 */
D
Dan Williams 已提交
340
static void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
341 342 343 344 345
{
	dev_dbg(&isci_host->pdev->dev,
		"%s: isci_port = %p\n", __func__, isci_port);
}

346
static void isci_port_stop_complete(struct isci_host *ihost,
347
				    struct isci_port *iport,
D
Dan Williams 已提交
348 349
				    enum sci_status completion_status)
{
350
	dev_dbg(&ihost->pdev->dev, "Port stop complete\n");
D
Dan Williams 已提交
351 352
}

353 354 355 356 357 358 359 360
/**
 * isci_port_hard_reset_complete() - This function is called by the sci core
 *    when the hard reset complete notification has been received.
 * @port: This parameter specifies the sci port with the active link.
 * @completion_status: This parameter specifies the core status for the reset
 *    process.
 *
 */
D
Dan Williams 已提交
361 362
static void isci_port_hard_reset_complete(struct isci_port *isci_port,
					  enum sci_status completion_status)
363 364 365 366 367 368 369 370 371 372
{
	dev_dbg(&isci_port->isci_host->pdev->dev,
		"%s: isci_port = %p, completion_status=%x\n",
		     __func__, isci_port, completion_status);

	/* Save the status of the hard reset from the port. */
	isci_port->hard_reset_status = completion_status;

	complete_all(&isci_port->hard_reset_complete);
}
373

D
Dan Williams 已提交
374 375 376 377 378 379
/* This method will return a true value if the specified phy can be assigned to
 * this port The following is a list of phys for each port that are allowed: -
 * Port 0 - 3 2 1 0 - Port 1 -     1 - Port 2 - 3 2 - Port 3 - 3 This method
 * doesn't preclude all configurations.  It merely ensures that a phy is part
 * of the allowable set of phy identifiers for that port.  For example, one
 * could assign phy 3 to port 0 and no other phys.  Please refer to
380
 * sci_port_is_phy_mask_valid() for information regarding whether the
D
Dan Williams 已提交
381 382 383 384
 * phy_mask for a port can be supported. bool true if this is a valid phy
 * assignment for the port false if this is not a valid phy assignment for the
 * port
 */
385
bool sci_port_is_valid_phy_assignment(struct isci_port *iport, u32 phy_index)
386
{
387 388 389
	struct isci_host *ihost = iport->owning_controller;
	struct sci_user_parameters *user = &ihost->user_parameters;

D
Dan Williams 已提交
390 391 392
	/* Initialize to invalid value. */
	u32 existing_phy_index = SCI_MAX_PHYS;
	u32 index;
393

394
	if ((iport->physical_port_index == 1) && (phy_index != 1))
D
Dan Williams 已提交
395
		return false;
396

397
	if (iport->physical_port_index == 3 && phy_index != 3)
D
Dan Williams 已提交
398
		return false;
399

400 401
	if (iport->physical_port_index == 2 &&
	    (phy_index == 0 || phy_index == 1))
D
Dan Williams 已提交
402
		return false;
403

404 405
	for (index = 0; index < SCI_MAX_PHYS; index++)
		if (iport->phy_table[index] && index != phy_index)
D
Dan Williams 已提交
406
			existing_phy_index = index;
407

408 409 410 411 412 413
	/* Ensure that all of the phys in the port are capable of
	 * operating at the same maximum link rate.
	 */
	if (existing_phy_index < SCI_MAX_PHYS &&
	    user->phys[phy_index].max_speed_generation !=
	    user->phys[existing_phy_index].max_speed_generation)
D
Dan Williams 已提交
414
		return false;
415

D
Dan Williams 已提交
416 417
	return true;
}
418

D
Dan Williams 已提交
419 420 421 422 423 424 425 426 427 428 429 430
/**
 *
 * @sci_port: This is the port object for which to determine if the phy mask
 *    can be supported.
 *
 * This method will return a true value if the port's phy mask can be supported
 * by the SCU. The following is a list of valid PHY mask configurations for
 * each port: - Port 0 - [[3  2] 1] 0 - Port 1 -        [1] - Port 2 - [[3] 2]
 * - Port 3 -  [3] This method returns a boolean indication specifying if the
 * phy mask can be supported. true if this is a valid phy assignment for the
 * port false if this is not a valid phy assignment for the port
 */
431
static bool sci_port_is_phy_mask_valid(
432
	struct isci_port *iport,
D
Dan Williams 已提交
433 434
	u32 phy_mask)
{
435
	if (iport->physical_port_index == 0) {
D
Dan Williams 已提交
436 437 438 439 440
		if (((phy_mask & 0x0F) == 0x0F)
		    || ((phy_mask & 0x03) == 0x03)
		    || ((phy_mask & 0x01) == 0x01)
		    || (phy_mask == 0))
			return true;
441
	} else if (iport->physical_port_index == 1) {
D
Dan Williams 已提交
442 443 444
		if (((phy_mask & 0x02) == 0x02)
		    || (phy_mask == 0))
			return true;
445
	} else if (iport->physical_port_index == 2) {
D
Dan Williams 已提交
446 447 448 449
		if (((phy_mask & 0x0C) == 0x0C)
		    || ((phy_mask & 0x04) == 0x04)
		    || (phy_mask == 0))
			return true;
450
	} else if (iport->physical_port_index == 3) {
D
Dan Williams 已提交
451 452 453 454
		if (((phy_mask & 0x08) == 0x08)
		    || (phy_mask == 0))
			return true;
	}
455

D
Dan Williams 已提交
456 457
	return false;
}
458

459
/*
D
Dan Williams 已提交
460 461 462 463
 * This method retrieves a currently active (i.e. connected) phy contained in
 * the port.  Currently, the lowest order phy that is connected is returned.
 * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
 * returned if there are no currently active (i.e. connected to a remote end
464
 * point) phys contained in the port. All other values specify a struct sci_phy
D
Dan Williams 已提交
465 466
 * object that is active in the port.
 */
467
static struct isci_phy *sci_port_get_a_connected_phy(struct isci_port *iport)
468
{
D
Dan Williams 已提交
469
	u32 index;
470
	struct isci_phy *iphy;
471

D
Dan Williams 已提交
472
	for (index = 0; index < SCI_MAX_PHYS; index++) {
473 474 475
		/* Ensure that the phy is both part of the port and currently
		 * connected to the remote end-point.
		 */
476
		iphy = iport->phy_table[index];
477
		if (iphy && sci_port_active_phy(iport, iphy))
478
			return iphy;
479 480
	}

D
Dan Williams 已提交
481 482 483
	return NULL;
}

484
static enum sci_status sci_port_set_phy(struct isci_port *iport, struct isci_phy *iphy)
D
Dan Williams 已提交
485
{
486
	/* Check to see if we can add this phy to a port
D
Dan Williams 已提交
487
	 * that means that the phy is not part of a port and that the port does
488 489
	 * not already have a phy assinged to the phy index.
	 */
490
	if (!iport->phy_table[iphy->phy_index] &&
491
	    !phy_get_non_dummy_port(iphy) &&
492
	    sci_port_is_valid_phy_assignment(iport, iphy->phy_index)) {
493 494 495
		/* Phy is being added in the stopped state so we are in MPC mode
		 * make logical port index = physical port index
		 */
496 497
		iport->logical_port_index = iport->physical_port_index;
		iport->phy_table[iphy->phy_index] = iphy;
498
		sci_phy_set_port(iphy, iport);
D
Dan Williams 已提交
499 500 501 502 503 504 505

		return SCI_SUCCESS;
	}

	return SCI_FAILURE;
}

506
static enum sci_status sci_port_clear_phy(struct isci_port *iport, struct isci_phy *iphy)
D
Dan Williams 已提交
507 508
{
	/* Make sure that this phy is part of this port */
509 510
	if (iport->phy_table[iphy->phy_index] == iphy &&
	    phy_get_non_dummy_port(iphy) == iport) {
511
		struct isci_host *ihost = iport->owning_controller;
D
Dan Williams 已提交
512 513

		/* Yep it is assigned to this port so remove it */
514
		sci_phy_set_port(iphy, &ihost->ports[SCI_MAX_PORTS]);
515
		iport->phy_table[iphy->phy_index] = NULL;
D
Dan Williams 已提交
516 517 518 519 520 521
		return SCI_SUCCESS;
	}

	return SCI_FAILURE;
}

522
void sci_port_get_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
D
Dan Williams 已提交
523 524 525
{
	u32 index;

526 527 528 529 530
	sas->high = 0;
	sas->low  = 0;
	for (index = 0; index < SCI_MAX_PHYS; index++)
		if (iport->phy_table[index])
			sci_phy_get_sas_address(iport->phy_table[index], sas);
D
Dan Williams 已提交
531 532
}

533
void sci_port_get_attached_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
D
Dan Williams 已提交
534
{
535
	struct isci_phy *iphy;
D
Dan Williams 已提交
536 537 538 539

	/*
	 * Ensure that the phy is both part of the port and currently
	 * connected to the remote end-point.
540
	 */
541
	iphy = sci_port_get_a_connected_phy(iport);
542 543
	if (iphy) {
		if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
544
			sci_phy_get_attached_sas_address(iphy, sas);
D
Dan Williams 已提交
545
		} else {
546 547
			sci_phy_get_sas_address(iphy, sas);
			sas->low += iphy->phy_index;
D
Dan Williams 已提交
548 549
		}
	} else {
550 551
		sas->high = 0;
		sas->low  = 0;
D
Dan Williams 已提交
552 553
	}
}
554

D
Dan Williams 已提交
555
/**
556
 * sci_port_construct_dummy_rnc() - create dummy rnc for si workaround
D
Dan Williams 已提交
557 558 559 560 561 562 563 564
 *
 * @sci_port: logical port on which we need to create the remote node context
 * @rni: remote node index for this remote node context.
 *
 * This routine will construct a dummy remote node context data structure
 * This structure will be posted to the hardware to work around a scheduler
 * error in the hardware.
 */
565
static void sci_port_construct_dummy_rnc(struct isci_port *iport, u16 rni)
D
Dan Williams 已提交
566 567 568
{
	union scu_remote_node_context *rnc;

569
	rnc = &iport->owning_controller->remote_node_context_table[rni];
D
Dan Williams 已提交
570 571 572 573 574 575 576 577

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

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

	rnc->ssp.remote_node_index = rni;
	rnc->ssp.remote_node_port_width = 1;
578
	rnc->ssp.logical_port_index = iport->physical_port_index;
D
Dan Williams 已提交
579 580 581 582 583 584 585 586 587

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

D
Dan Williams 已提交
588 589
/*
 * construct a dummy task context data structure.  This
D
Dan Williams 已提交
590 591 592
 * structure will be posted to the hardwre to work around a scheduler error
 * in the hardware.
 */
593
static void sci_port_construct_dummy_task(struct isci_port *iport, u16 tag)
D
Dan Williams 已提交
594
{
595
	struct isci_host *ihost = iport->owning_controller;
D
Dan Williams 已提交
596 597
	struct scu_task_context *task_context;

598
	task_context = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
D
Dan Williams 已提交
599 600 601 602
	memset(task_context, 0, sizeof(struct scu_task_context));

	task_context->initiator_request = 1;
	task_context->connection_rate = 1;
603
	task_context->logical_port_index = iport->physical_port_index;
D
Dan Williams 已提交
604
	task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
D
Dan Williams 已提交
605
	task_context->task_index = ISCI_TAG_TCI(tag);
D
Dan Williams 已提交
606 607
	task_context->valid = SCU_TASK_CONTEXT_VALID;
	task_context->context_type = SCU_TASK_CONTEXT_TYPE;
608
	task_context->remote_node_index = iport->reserved_rni;
D
Dan Williams 已提交
609 610 611 612
	task_context->do_not_dma_ssp_good_response = 1;
	task_context->task_phase = 0x01;
}

613
static void sci_port_destroy_dummy_resources(struct isci_port *iport)
D
Dan Williams 已提交
614
{
615
	struct isci_host *ihost = iport->owning_controller;
D
Dan Williams 已提交
616

617
	if (iport->reserved_tag != SCI_CONTROLLER_INVALID_IO_TAG)
618
		isci_free_tag(ihost, iport->reserved_tag);
D
Dan Williams 已提交
619

620
	if (iport->reserved_rni != SCU_DUMMY_INDEX)
621
		sci_remote_node_table_release_remote_node_index(&ihost->available_remote_nodes,
622
								     1, iport->reserved_rni);
D
Dan Williams 已提交
623

624 625
	iport->reserved_rni = SCU_DUMMY_INDEX;
	iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
D
Dan Williams 已提交
626 627
}

628
void sci_port_setup_transports(struct isci_port *iport, u32 device_id)
D
Dan Williams 已提交
629 630 631 632
{
	u8 index;

	for (index = 0; index < SCI_MAX_PHYS; index++) {
633
		if (iport->active_phy_mask & (1 << index))
634
			sci_phy_setup_transport(iport->phy_table[index], device_id);
635
	}
D
Dan Williams 已提交
636
}
637

638 639
static void sci_port_activate_phy(struct isci_port *iport, struct isci_phy *iphy,
				  bool do_notify_user)
D
Dan Williams 已提交
640
{
641
	struct isci_host *ihost = iport->owning_controller;
D
Dan Williams 已提交
642

643
	if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA)
644
		sci_phy_resume(iphy);
D
Dan Williams 已提交
645

646
	iport->active_phy_mask |= 1 << iphy->phy_index;
D
Dan Williams 已提交
647

648
	sci_controller_clear_invalid_phy(ihost, iphy);
D
Dan Williams 已提交
649 650

	if (do_notify_user == true)
651
		isci_port_link_up(ihost, iport, iphy);
652
}
653

654 655
void sci_port_deactivate_phy(struct isci_port *iport, struct isci_phy *iphy,
			     bool do_notify_user)
656
{
D
Dan Williams 已提交
657
	struct isci_host *ihost = iport->owning_controller;
D
Dan Williams 已提交
658

659
	iport->active_phy_mask &= ~(1 << iphy->phy_index);
D
Dan Williams 已提交
660

661
	iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
D
Dan Williams 已提交
662 663

	/* Re-assign the phy back to the LP as if it were a narrow port */
664
	writel(iphy->phy_index,
665
		&iport->port_pe_configuration_register[iphy->phy_index]);
D
Dan Williams 已提交
666 667 668 669 670

	if (do_notify_user == true)
		isci_port_link_down(ihost, iphy, iport);
}

671
static void sci_port_invalid_link_up(struct isci_port *iport, struct isci_phy *iphy)
D
Dan Williams 已提交
672
{
673
	struct isci_host *ihost = iport->owning_controller;
D
Dan Williams 已提交
674 675 676 677 678 679

	/*
	 * Check to see if we have alreay reported this link as bad and if
	 * not go ahead and tell the SCI_USER that we have discovered an
	 * invalid link.
	 */
680
	if ((ihost->invalid_phy_mask & (1 << iphy->phy_index)) == 0) {
D
Dan Williams 已提交
681
		ihost->invalid_phy_mask |= 1 << iphy->phy_index;
682
		dev_warn(&ihost->pdev->dev, "Invalid link up!\n");
D
Dan Williams 已提交
683 684 685
	}
}

686
static bool is_port_ready_state(enum sci_port_states state)
687 688
{
	switch (state) {
E
Edmund Nadolski 已提交
689 690 691 692
	case SCI_PORT_READY:
	case SCI_PORT_SUB_WAITING:
	case SCI_PORT_SUB_OPERATIONAL:
	case SCI_PORT_SUB_CONFIGURING:
693 694 695 696 697 698 699
		return true;
	default:
		return false;
	}
}

/* flag dummy rnc hanling when exiting a ready state */
700
static void port_state_machine_change(struct isci_port *iport,
701
				      enum sci_port_states state)
702
{
703
	struct sci_base_state_machine *sm = &iport->sm;
704
	enum sci_port_states old_state = sm->current_state_id;
705 706

	if (is_port_ready_state(old_state) && !is_port_ready_state(state))
707
		iport->ready_exit = true;
708

E
Edmund Nadolski 已提交
709
	sci_change_state(sm, state);
710
	iport->ready_exit = false;
711 712
}

D
Dan Williams 已提交
713
/**
714 715
 * sci_port_general_link_up_handler - phy can be assigned to port?
 * @sci_port: sci_port object for which has a phy that has gone link up.
716
 * @sci_phy: This is the struct isci_phy object that has gone link up.
D
Dan Williams 已提交
717
 * @do_notify_user: This parameter specifies whether to inform the user (via
718
 *    sci_port_link_up()) as to the fact that a new phy as become ready.
D
Dan Williams 已提交
719 720 721 722 723 724 725
 *
 * Determine if this phy can be assigned to this
 * port . If the phy is not a valid PHY for
 * this port then the function will notify the user. A PHY can only be
 * part of a port if it's attached SAS ADDRESS is the same as all other PHYs in
 * the same port. none
 */
726
static void sci_port_general_link_up_handler(struct isci_port *iport,
727
						  struct isci_phy *iphy,
D
Dan Williams 已提交
728 729 730 731 732
						  bool do_notify_user)
{
	struct sci_sas_address port_sas_address;
	struct sci_sas_address phy_sas_address;

733 734
	sci_port_get_attached_sas_address(iport, &port_sas_address);
	sci_phy_get_attached_sas_address(iphy, &phy_sas_address);
D
Dan Williams 已提交
735 736 737 738 739 740 741 742

	/* If the SAS address of the new phy matches the SAS address of
	 * other phys in the port OR this is the first phy in the port,
	 * then activate the phy and allow it to be used for operations
	 * in this port.
	 */
	if ((phy_sas_address.high == port_sas_address.high &&
	     phy_sas_address.low  == port_sas_address.low) ||
743 744
	    iport->active_phy_mask == 0) {
		struct sci_base_state_machine *sm = &iport->sm;
D
Dan Williams 已提交
745

746
		sci_port_activate_phy(iport, iphy, do_notify_user);
E
Edmund Nadolski 已提交
747
		if (sm->current_state_id == SCI_PORT_RESETTING)
748
			port_state_machine_change(iport, SCI_PORT_READY);
D
Dan Williams 已提交
749
	} else
750
		sci_port_invalid_link_up(iport, iphy);
D
Dan Williams 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763
}



/**
 * This method returns false if the port only has a single phy object assigned.
 *     If there are no phys or more than one phy then the method will return
 *    true.
 * @sci_port: The port for which the wide port condition is to be checked.
 *
 * bool true Is returned if this is a wide ported port. false Is returned if
 * this is a narrow port.
 */
764
static bool sci_port_is_wide(struct isci_port *iport)
D
Dan Williams 已提交
765 766 767 768 769
{
	u32 index;
	u32 phy_count = 0;

	for (index = 0; index < SCI_MAX_PHYS; index++) {
770
		if (iport->phy_table[index] != NULL) {
D
Dan Williams 已提交
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
			phy_count++;
		}
	}

	return phy_count != 1;
}

/**
 * This method is called by the PHY object when the link is detected. if the
 *    port wants the PHY to continue on to the link up state then the port
 *    layer must return true.  If the port object returns false the phy object
 *    must halt its attempt to go link up.
 * @sci_port: The port associated with the phy object.
 * @sci_phy: The phy object that is trying to go link up.
 *
 * true if the phy object can continue to the link up condition. true Is
 * returned if this phy can continue to the ready state. false Is returned if
 * can not continue on to the ready state. This notification is in place for
 * wide ports and direct attached phys.  Since there are no wide ported SATA
 * devices this could become an invalid port configuration.
 */
792
bool sci_port_link_detected(
793
	struct isci_port *iport,
794
	struct isci_phy *iphy)
D
Dan Williams 已提交
795
{
796
	if ((iport->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
797
	    (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) &&
798 799
	    sci_port_is_wide(iport)) {
		sci_port_invalid_link_up(iport, iphy);
D
Dan Williams 已提交
800 801 802 803 804 805 806

		return false;
	}

	return true;
}

807
static void port_timeout(unsigned long data)
D
Dan Williams 已提交
808
{
809
	struct sci_timer *tmr = (struct sci_timer *)data;
810
	struct isci_port *iport = container_of(tmr, typeof(*iport), timer);
811
	struct isci_host *ihost = iport->owning_controller;
812
	unsigned long flags;
D
Dan Williams 已提交
813 814
	u32 current_state;

815 816 817 818 819
	spin_lock_irqsave(&ihost->scic_lock, flags);

	if (tmr->cancel)
		goto done;

820
	current_state = iport->sm.current_state_id;
D
Dan Williams 已提交
821

E
Edmund Nadolski 已提交
822
	if (current_state == SCI_PORT_RESETTING) {
823 824
		/* if the port is still in the resetting state then the timeout
		 * fired before the reset completed.
D
Dan Williams 已提交
825
		 */
826
		port_state_machine_change(iport, SCI_PORT_FAILED);
E
Edmund Nadolski 已提交
827
	} else if (current_state == SCI_PORT_STOPPED) {
828 829
		/* if the port is stopped then the start request failed In this
		 * case stay in the stopped state.
D
Dan Williams 已提交
830
		 */
831
		dev_err(sciport_to_dev(iport),
D
Dan Williams 已提交
832 833
			"%s: SCIC Port 0x%p failed to stop before tiemout.\n",
			__func__,
834
			iport);
E
Edmund Nadolski 已提交
835
	} else if (current_state == SCI_PORT_STOPPING) {
836
		/* if the port is still stopping then the stop has not completed */
837 838
		isci_port_stop_complete(iport->owning_controller,
					iport,
839
					SCI_FAILURE_TIMEOUT);
D
Dan Williams 已提交
840
	} else {
841
		/* The port is in the ready state and we have a timer
D
Dan Williams 已提交
842 843
		 * reporting a timeout this should not happen.
		 */
844
		dev_err(sciport_to_dev(iport),
D
Dan Williams 已提交
845
			"%s: SCIC Port 0x%p is processing a timeout operation "
846
			"in state %d.\n", __func__, iport, current_state);
D
Dan Williams 已提交
847
	}
848 849 850

done:
	spin_unlock_irqrestore(&ihost->scic_lock, flags);
D
Dan Williams 已提交
851 852 853 854 855 856 857 858 859
}

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

/**
 * This function updates the hardwares VIIT entry for this port.
 *
 *
 */
860
static void sci_port_update_viit_entry(struct isci_port *iport)
D
Dan Williams 已提交
861 862 863
{
	struct sci_sas_address sas_address;

864
	sci_port_get_sas_address(iport, &sas_address);
D
Dan Williams 已提交
865 866

	writel(sas_address.high,
867
		&iport->viit_registers->initiator_sas_address_hi);
D
Dan Williams 已提交
868
	writel(sas_address.low,
869
		&iport->viit_registers->initiator_sas_address_lo);
D
Dan Williams 已提交
870 871

	/* This value get cleared just in case its not already cleared */
872
	writel(0, &iport->viit_registers->reserved);
D
Dan Williams 已提交
873 874 875 876

	/* We are required to update the status register last */
	writel(SCU_VIIT_ENTRY_ID_VIIT |
	       SCU_VIIT_IPPT_INITIATOR |
877
	       ((1 << iport->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
D
Dan Williams 已提交
878
	       SCU_VIIT_STATUS_ALL_VALID,
879
	       &iport->viit_registers->status);
D
Dan Williams 已提交
880 881
}

882
enum sas_linkrate sci_port_get_max_allowed_speed(struct isci_port *iport)
D
Dan Williams 已提交
883 884
{
	u16 index;
885
	struct isci_phy *iphy;
D
Dan Williams 已提交
886 887 888 889 890 891
	enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;

	/*
	 * Loop through all of the phys in this port and find the phy with the
	 * lowest maximum link rate. */
	for (index = 0; index < SCI_MAX_PHYS; index++) {
892
		iphy = iport->phy_table[index];
893
		if (iphy && sci_port_active_phy(iport, iphy) &&
894 895
		    iphy->max_negotiated_speed < max_allowed_speed)
			max_allowed_speed = iphy->max_negotiated_speed;
D
Dan Williams 已提交
896 897 898 899 900
	}

	return max_allowed_speed;
}

901
static void sci_port_suspend_port_task_scheduler(struct isci_port *iport)
D
Dan Williams 已提交
902 903 904
{
	u32 pts_control_value;

905
	pts_control_value = readl(&iport->port_task_scheduler_registers->control);
D
Dan Williams 已提交
906
	pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
907
	writel(pts_control_value, &iport->port_task_scheduler_registers->control);
D
Dan Williams 已提交
908 909 910
}

/**
911
 * sci_port_post_dummy_request() - post dummy/workaround request
D
Dan Williams 已提交
912 913 914 915 916 917 918
 * @sci_port: port to post task
 *
 * Prevent the hardware scheduler from posting new requests to the front
 * of the scheduler queue causing a starvation problem for currently
 * ongoing requests.
 *
 */
919
static void sci_port_post_dummy_request(struct isci_port *iport)
D
Dan Williams 已提交
920
{
921
	struct isci_host *ihost = iport->owning_controller;
922
	u16 tag = iport->reserved_tag;
923 924
	struct scu_task_context *tc;
	u32 command;
D
Dan Williams 已提交
925

926
	tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
927
	tc->abort = 0;
D
Dan Williams 已提交
928 929

	command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
930
		  iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
931
		  ISCI_TAG_TCI(tag);
D
Dan Williams 已提交
932

933
	sci_controller_post_request(ihost, command);
D
Dan Williams 已提交
934 935 936 937 938 939 940 941 942
}

/**
 * This routine will abort the dummy request.  This will alow the hardware to
 * power down parts of the silicon to save power.
 *
 * @sci_port: The port on which the task must be aborted.
 *
 */
943
static void sci_port_abort_dummy_request(struct isci_port *iport)
D
Dan Williams 已提交
944
{
945
	struct isci_host *ihost = iport->owning_controller;
946
	u16 tag = iport->reserved_tag;
D
Dan Williams 已提交
947 948 949
	struct scu_task_context *tc;
	u32 command;

950
	tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
D
Dan Williams 已提交
951 952 953
	tc->abort = 1;

	command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
954
		  iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
955
		  ISCI_TAG_TCI(tag);
D
Dan Williams 已提交
956

957
	sci_controller_post_request(ihost, command);
D
Dan Williams 已提交
958 959 960 961
}

/**
 *
962
 * @sci_port: This is the struct isci_port object to resume.
D
Dan Williams 已提交
963 964 965 966
 *
 * This method will resume the port task scheduler for this port object. none
 */
static void
967
sci_port_resume_port_task_scheduler(struct isci_port *iport)
D
Dan Williams 已提交
968 969 970
{
	u32 pts_control_value;

971
	pts_control_value = readl(&iport->port_task_scheduler_registers->control);
D
Dan Williams 已提交
972
	pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
973
	writel(pts_control_value, &iport->port_task_scheduler_registers->control);
D
Dan Williams 已提交
974 975
}

976
static void sci_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
977
{
978
	struct isci_port *iport = container_of(sm, typeof(*iport), sm);
D
Dan Williams 已提交
979

980
	sci_port_suspend_port_task_scheduler(iport);
D
Dan Williams 已提交
981

982
	iport->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
D
Dan Williams 已提交
983

984
	if (iport->active_phy_mask != 0) {
D
Dan Williams 已提交
985
		/* At least one of the phys on the port is ready */
986
		port_state_machine_change(iport,
E
Edmund Nadolski 已提交
987
					  SCI_PORT_SUB_OPERATIONAL);
D
Dan Williams 已提交
988 989 990
	}
}

991
static void sci_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
992 993
{
	u32 index;
994
	struct isci_port *iport = container_of(sm, typeof(*iport), sm);
995
	struct isci_host *ihost = iport->owning_controller;
D
Dan Williams 已提交
996 997 998 999

	isci_port_ready(ihost, iport);

	for (index = 0; index < SCI_MAX_PHYS; index++) {
1000 1001 1002 1003
		if (iport->phy_table[index]) {
			writel(iport->physical_port_index,
				&iport->port_pe_configuration_register[
					iport->phy_table[index]->phy_index]);
D
Dan Williams 已提交
1004 1005 1006
		}
	}

1007
	sci_port_update_viit_entry(iport);
D
Dan Williams 已提交
1008

1009
	sci_port_resume_port_task_scheduler(iport);
D
Dan Williams 已提交
1010 1011 1012 1013 1014

	/*
	 * Post the dummy task for the port so the hardware can schedule
	 * io correctly
	 */
1015
	sci_port_post_dummy_request(iport);
D
Dan Williams 已提交
1016 1017
}

1018
static void sci_port_invalidate_dummy_remote_node(struct isci_port *iport)
1019
{
1020
	struct isci_host *ihost = iport->owning_controller;
1021
	u8 phys_index = iport->physical_port_index;
1022
	union scu_remote_node_context *rnc;
1023
	u16 rni = iport->reserved_rni;
1024 1025
	u32 command;

1026
	rnc = &ihost->remote_node_context_table[rni];
1027 1028 1029 1030 1031 1032 1033

	rnc->ssp.is_valid = false;

	/* ensure the preceding tc abort request has reached the
	 * controller and give it ample time to act before posting the rnc
	 * invalidate
	 */
1034
	readl(&ihost->smu_registers->interrupt_status); /* flush */
1035 1036 1037 1038 1039
	udelay(10);

	command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
		  phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;

1040
	sci_controller_post_request(ihost, command);
1041 1042
}

D
Dan Williams 已提交
1043 1044
/**
 *
1045
 * @object: This is the object which is cast to a struct isci_port object.
D
Dan Williams 已提交
1046
 *
1047
 * This method will perform the actions required by the struct isci_port on
E
Edmund Nadolski 已提交
1048
 * exiting the SCI_PORT_SUB_OPERATIONAL. This function reports
D
Dan Williams 已提交
1049 1050
 * the port not ready and suspends the port task scheduler. none
 */
1051
static void sci_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1052
{
1053
	struct isci_port *iport = container_of(sm, typeof(*iport), sm);
1054
	struct isci_host *ihost = iport->owning_controller;
D
Dan Williams 已提交
1055 1056 1057 1058 1059 1060

	/*
	 * Kill the dummy task for this port if it has not yet posted
	 * the hardware will treat this as a NOP and just return abort
	 * complete.
	 */
1061
	sci_port_abort_dummy_request(iport);
D
Dan Williams 已提交
1062 1063

	isci_port_not_ready(ihost, iport);
1064

1065
	if (iport->ready_exit)
1066
		sci_port_invalidate_dummy_remote_node(iport);
D
Dan Williams 已提交
1067 1068
}

1069
static void sci_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1070
{
1071
	struct isci_port *iport = container_of(sm, typeof(*iport), sm);
1072
	struct isci_host *ihost = iport->owning_controller;
D
Dan Williams 已提交
1073

1074
	if (iport->active_phy_mask == 0) {
D
Dan Williams 已提交
1075 1076
		isci_port_not_ready(ihost, iport);

1077
		port_state_machine_change(iport,
E
Edmund Nadolski 已提交
1078
					  SCI_PORT_SUB_WAITING);
1079 1080
	} else if (iport->started_request_count == 0)
		port_state_machine_change(iport,
E
Edmund Nadolski 已提交
1081
					  SCI_PORT_SUB_OPERATIONAL);
D
Dan Williams 已提交
1082 1083
}

1084
static void sci_port_ready_substate_configuring_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1085
{
1086
	struct isci_port *iport = container_of(sm, typeof(*iport), sm);
D
Dan Williams 已提交
1087

1088
	sci_port_suspend_port_task_scheduler(iport);
1089
	if (iport->ready_exit)
1090
		sci_port_invalidate_dummy_remote_node(iport);
D
Dan Williams 已提交
1091 1092
}

1093
enum sci_status sci_port_start(struct isci_port *iport)
P
Piotr Sawicki 已提交
1094
{
1095
	struct isci_host *ihost = iport->owning_controller;
P
Piotr Sawicki 已提交
1096
	enum sci_status status = SCI_SUCCESS;
1097
	enum sci_port_states state;
P
Piotr Sawicki 已提交
1098 1099
	u32 phy_mask;

1100
	state = iport->sm.current_state_id;
E
Edmund Nadolski 已提交
1101
	if (state != SCI_PORT_STOPPED) {
1102
		dev_warn(sciport_to_dev(iport),
P
Piotr Sawicki 已提交
1103 1104 1105 1106
			 "%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}

1107
	if (iport->assigned_device_count > 0) {
P
Piotr Sawicki 已提交
1108 1109 1110 1111 1112 1113 1114 1115
		/* TODO This is a start failure operation because
		 * there are still devices assigned to this port.
		 * There must be no devices assigned to a port on a
		 * start operation.
		 */
		return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
	}

1116
	if (iport->reserved_rni == SCU_DUMMY_INDEX) {
1117
		u16 rni = sci_remote_node_table_allocate_remote_node(
1118
				&ihost->available_remote_nodes, 1);
P
Piotr Sawicki 已提交
1119 1120

		if (rni != SCU_DUMMY_INDEX)
1121
			sci_port_construct_dummy_rnc(iport, rni);
P
Piotr Sawicki 已提交
1122 1123
		else
			status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1124
		iport->reserved_rni = rni;
P
Piotr Sawicki 已提交
1125 1126
	}

1127
	if (iport->reserved_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
1128
		u16 tag;
P
Piotr Sawicki 已提交
1129

1130 1131
		tag = isci_alloc_tag(ihost);
		if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
P
Piotr Sawicki 已提交
1132
			status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1133
		else
1134
			sci_port_construct_dummy_task(iport, tag);
1135
		iport->reserved_tag = tag;
P
Piotr Sawicki 已提交
1136 1137 1138
	}

	if (status == SCI_SUCCESS) {
1139
		phy_mask = sci_port_get_phys(iport);
P
Piotr Sawicki 已提交
1140 1141 1142 1143 1144 1145

		/*
		 * There are one or more phys assigned to this port.  Make sure
		 * the port's phy mask is in fact legal and supported by the
		 * silicon.
		 */
1146
		if (sci_port_is_phy_mask_valid(iport, phy_mask) == true) {
1147
			port_state_machine_change(iport,
E
Edmund Nadolski 已提交
1148
						  SCI_PORT_READY);
P
Piotr Sawicki 已提交
1149 1150 1151 1152 1153 1154 1155

			return SCI_SUCCESS;
		}
		status = SCI_FAILURE;
	}

	if (status != SCI_SUCCESS)
1156
		sci_port_destroy_dummy_resources(iport);
P
Piotr Sawicki 已提交
1157 1158 1159 1160

	return status;
}

1161
enum sci_status sci_port_stop(struct isci_port *iport)
P
Piotr Sawicki 已提交
1162
{
1163
	enum sci_port_states state;
P
Piotr Sawicki 已提交
1164

1165
	state = iport->sm.current_state_id;
P
Piotr Sawicki 已提交
1166
	switch (state) {
E
Edmund Nadolski 已提交
1167
	case SCI_PORT_STOPPED:
P
Piotr Sawicki 已提交
1168
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
1169 1170 1171 1172
	case SCI_PORT_SUB_WAITING:
	case SCI_PORT_SUB_OPERATIONAL:
	case SCI_PORT_SUB_CONFIGURING:
	case SCI_PORT_RESETTING:
1173
		port_state_machine_change(iport,
E
Edmund Nadolski 已提交
1174
					  SCI_PORT_STOPPING);
P
Piotr Sawicki 已提交
1175 1176
		return SCI_SUCCESS;
	default:
1177
		dev_warn(sciport_to_dev(iport),
P
Piotr Sawicki 已提交
1178 1179 1180 1181 1182
			 "%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}
}

1183
static enum sci_status sci_port_hard_reset(struct isci_port *iport, u32 timeout)
1184 1185
{
	enum sci_status status = SCI_FAILURE_INVALID_PHY;
1186
	struct isci_phy *iphy = NULL;
1187
	enum sci_port_states state;
1188 1189
	u32 phy_index;

1190
	state = iport->sm.current_state_id;
E
Edmund Nadolski 已提交
1191
	if (state != SCI_PORT_SUB_OPERATIONAL) {
1192
		dev_warn(sciport_to_dev(iport),
1193 1194 1195 1196 1197
			 "%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}

	/* Select a phy on which we can send the hard reset request. */
1198
	for (phy_index = 0; phy_index < SCI_MAX_PHYS && !iphy; phy_index++) {
1199
		iphy = iport->phy_table[phy_index];
1200
		if (iphy && !sci_port_active_phy(iport, iphy)) {
1201 1202 1203 1204
			/*
			 * We found a phy but it is not ready select
			 * different phy
			 */
1205
			iphy = NULL;
1206 1207 1208 1209
		}
	}

	/* If we have a phy then go ahead and start the reset procedure */
1210
	if (!iphy)
1211
		return status;
1212
	status = sci_phy_reset(iphy);
1213 1214 1215 1216

	if (status != SCI_SUCCESS)
		return status;

1217 1218
	sci_mod_timer(&iport->timer, timeout);
	iport->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
1219

1220
	port_state_machine_change(iport, SCI_PORT_RESETTING);
1221 1222 1223 1224
	return SCI_SUCCESS;
}

/**
1225
 * sci_port_add_phy() -
1226 1227 1228 1229 1230 1231 1232
 * @sci_port: This parameter specifies the port in which the phy will be added.
 * @sci_phy: This parameter is the phy which is to be added to the port.
 *
 * This method will add a PHY to the selected port. This method returns an
 * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
 * status is a failure to add the phy to the port.
 */
1233
enum sci_status sci_port_add_phy(struct isci_port *iport,
1234
				      struct isci_phy *iphy)
1235 1236
{
	enum sci_status status;
1237
	enum sci_port_states state;
1238

1239
	state = iport->sm.current_state_id;
1240
	switch (state) {
E
Edmund Nadolski 已提交
1241
	case SCI_PORT_STOPPED: {
1242 1243 1244
		struct sci_sas_address port_sas_address;

		/* Read the port assigned SAS Address if there is one */
1245
		sci_port_get_sas_address(iport, &port_sas_address);
1246 1247 1248 1249 1250 1251 1252

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

			/* Make sure that the PHY SAS Address matches the SAS Address
			 * for this port
			 */
1253
			sci_phy_get_sas_address(iphy, &phy_sas_address);
1254 1255 1256 1257 1258

			if (port_sas_address.high != phy_sas_address.high ||
			    port_sas_address.low  != phy_sas_address.low)
				return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
		}
1259
		return sci_port_set_phy(iport, iphy);
1260
	}
E
Edmund Nadolski 已提交
1261 1262
	case SCI_PORT_SUB_WAITING:
	case SCI_PORT_SUB_OPERATIONAL:
1263
		status = sci_port_set_phy(iport, iphy);
1264 1265 1266 1267

		if (status != SCI_SUCCESS)
			return status;

1268
		sci_port_general_link_up_handler(iport, iphy, true);
1269 1270
		iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
		port_state_machine_change(iport, SCI_PORT_SUB_CONFIGURING);
1271 1272

		return status;
E
Edmund Nadolski 已提交
1273
	case SCI_PORT_SUB_CONFIGURING:
1274
		status = sci_port_set_phy(iport, iphy);
1275 1276 1277

		if (status != SCI_SUCCESS)
			return status;
1278
		sci_port_general_link_up_handler(iport, iphy, true);
1279 1280 1281 1282

		/* Re-enter the configuring state since this may be the last phy in
		 * the port.
		 */
1283
		port_state_machine_change(iport,
E
Edmund Nadolski 已提交
1284
					  SCI_PORT_SUB_CONFIGURING);
1285 1286
		return SCI_SUCCESS;
	default:
1287
		dev_warn(sciport_to_dev(iport),
1288 1289 1290 1291 1292 1293
			 "%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}
}

/**
1294
 * sci_port_remove_phy() -
1295 1296 1297 1298 1299 1300 1301
 * @sci_port: This parameter specifies the port in which the phy will be added.
 * @sci_phy: This parameter is the phy which is to be added to the port.
 *
 * This method will remove the PHY from the selected PORT. This method returns
 * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
 * other status is a failure to add the phy to the port.
 */
1302
enum sci_status sci_port_remove_phy(struct isci_port *iport,
1303
					 struct isci_phy *iphy)
1304 1305
{
	enum sci_status status;
1306
	enum sci_port_states state;
1307

1308
	state = iport->sm.current_state_id;
1309 1310

	switch (state) {
E
Edmund Nadolski 已提交
1311
	case SCI_PORT_STOPPED:
1312
		return sci_port_clear_phy(iport, iphy);
E
Edmund Nadolski 已提交
1313
	case SCI_PORT_SUB_OPERATIONAL:
1314
		status = sci_port_clear_phy(iport, iphy);
1315 1316 1317
		if (status != SCI_SUCCESS)
			return status;

1318
		sci_port_deactivate_phy(iport, iphy, true);
1319 1320
		iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
		port_state_machine_change(iport,
E
Edmund Nadolski 已提交
1321
					  SCI_PORT_SUB_CONFIGURING);
1322
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
1323
	case SCI_PORT_SUB_CONFIGURING:
1324
		status = sci_port_clear_phy(iport, iphy);
1325 1326 1327

		if (status != SCI_SUCCESS)
			return status;
1328
		sci_port_deactivate_phy(iport, iphy, true);
1329 1330 1331 1332

		/* Re-enter the configuring state since this may be the last phy in
		 * the port
		 */
1333
		port_state_machine_change(iport,
E
Edmund Nadolski 已提交
1334
					  SCI_PORT_SUB_CONFIGURING);
1335 1336
		return SCI_SUCCESS;
	default:
1337
		dev_warn(sciport_to_dev(iport),
1338 1339 1340 1341
			 "%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}
}
1342

1343
enum sci_status sci_port_link_up(struct isci_port *iport,
1344
				      struct isci_phy *iphy)
1345
{
1346
	enum sci_port_states state;
1347

1348
	state = iport->sm.current_state_id;
1349
	switch (state) {
E
Edmund Nadolski 已提交
1350
	case SCI_PORT_SUB_WAITING:
1351 1352 1353
		/* Since this is the first phy going link up for the port we
		 * can just enable it and continue
		 */
1354
		sci_port_activate_phy(iport, iphy, true);
1355

1356
		port_state_machine_change(iport,
E
Edmund Nadolski 已提交
1357
					  SCI_PORT_SUB_OPERATIONAL);
1358
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
1359
	case SCI_PORT_SUB_OPERATIONAL:
1360
		sci_port_general_link_up_handler(iport, iphy, true);
1361
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
1362
	case SCI_PORT_RESETTING:
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
		/* TODO We should  make  sure  that  the phy  that  has gone
		 * link up is the same one on which we sent the reset.  It is
		 * possible that the phy on which we sent  the reset is not the
		 * one that has  gone  link up  and we  want to make sure that
		 * phy being reset  comes  back.  Consider the case where a
		 * reset is sent but before the hardware processes the reset it
		 * get a link up on  the  port because of a hot plug event.
		 * because  of  the reset request this phy will go link down
		 * almost immediately.
		 */

		/* In the resetting state we don't notify the user regarding
		 * link up and link down notifications.
		 */
1377
		sci_port_general_link_up_handler(iport, iphy, false);
1378 1379
		return SCI_SUCCESS;
	default:
1380
		dev_warn(sciport_to_dev(iport),
1381 1382 1383 1384 1385
			 "%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}
}

1386
enum sci_status sci_port_link_down(struct isci_port *iport,
1387
					struct isci_phy *iphy)
1388
{
1389
	enum sci_port_states state;
1390

1391
	state = iport->sm.current_state_id;
1392
	switch (state) {
E
Edmund Nadolski 已提交
1393
	case SCI_PORT_SUB_OPERATIONAL:
1394
		sci_port_deactivate_phy(iport, iphy, true);
1395 1396 1397 1398 1399

		/* If there are no active phys left in the port, then
		 * transition the port to the WAITING state until such time
		 * as a phy goes link up
		 */
1400 1401
		if (iport->active_phy_mask == 0)
			port_state_machine_change(iport,
E
Edmund Nadolski 已提交
1402
						  SCI_PORT_SUB_WAITING);
1403
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
1404
	case SCI_PORT_RESETTING:
1405 1406
		/* In the resetting state we don't notify the user regarding
		 * link up and link down notifications. */
1407
		sci_port_deactivate_phy(iport, iphy, false);
1408 1409
		return SCI_SUCCESS;
	default:
1410
		dev_warn(sciport_to_dev(iport),
1411 1412 1413 1414 1415
			 "%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}
}

1416 1417 1418
enum sci_status sci_port_start_io(struct isci_port *iport,
				  struct isci_remote_device *idev,
				  struct isci_request *ireq)
1419
{
1420
	enum sci_port_states state;
1421

1422
	state = iport->sm.current_state_id;
1423
	switch (state) {
E
Edmund Nadolski 已提交
1424
	case SCI_PORT_SUB_WAITING:
1425
		return SCI_FAILURE_INVALID_STATE;
E
Edmund Nadolski 已提交
1426
	case SCI_PORT_SUB_OPERATIONAL:
1427
		iport->started_request_count++;
1428 1429
		return SCI_SUCCESS;
	default:
1430
		dev_warn(sciport_to_dev(iport),
1431 1432
			 "%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
D
Dan Williams 已提交
1433
	}
1434
}
D
Dan Williams 已提交
1435

1436 1437 1438
enum sci_status sci_port_complete_io(struct isci_port *iport,
				     struct isci_remote_device *idev,
				     struct isci_request *ireq)
1439
{
1440
	enum sci_port_states state;
1441

1442
	state = iport->sm.current_state_id;
1443
	switch (state) {
E
Edmund Nadolski 已提交
1444
	case SCI_PORT_STOPPED:
1445
		dev_warn(sciport_to_dev(iport),
1446 1447
			 "%s: in wrong state: %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
E
Edmund Nadolski 已提交
1448
	case SCI_PORT_STOPPING:
1449
		sci_port_decrement_request_count(iport);
1450

1451 1452
		if (iport->started_request_count == 0)
			port_state_machine_change(iport,
E
Edmund Nadolski 已提交
1453
						  SCI_PORT_STOPPED);
1454
		break;
E
Edmund Nadolski 已提交
1455 1456 1457 1458 1459
	case SCI_PORT_READY:
	case SCI_PORT_RESETTING:
	case SCI_PORT_FAILED:
	case SCI_PORT_SUB_WAITING:
	case SCI_PORT_SUB_OPERATIONAL:
1460
		sci_port_decrement_request_count(iport);
1461
		break;
E
Edmund Nadolski 已提交
1462
	case SCI_PORT_SUB_CONFIGURING:
1463
		sci_port_decrement_request_count(iport);
1464 1465
		if (iport->started_request_count == 0) {
			port_state_machine_change(iport,
E
Edmund Nadolski 已提交
1466
						  SCI_PORT_SUB_OPERATIONAL);
1467 1468 1469 1470 1471
		}
		break;
	}
	return SCI_SUCCESS;
}
D
Dan Williams 已提交
1472

1473
static void sci_port_enable_port_task_scheduler(struct isci_port *iport)
D
Dan Williams 已提交
1474 1475 1476
{
	u32 pts_control_value;

1477
	 /* enable the port task scheduler in a suspended state */
1478
	pts_control_value = readl(&iport->port_task_scheduler_registers->control);
D
Dan Williams 已提交
1479
	pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
1480
	writel(pts_control_value, &iport->port_task_scheduler_registers->control);
D
Dan Williams 已提交
1481 1482
}

1483
static void sci_port_disable_port_task_scheduler(struct isci_port *iport)
D
Dan Williams 已提交
1484 1485 1486
{
	u32 pts_control_value;

1487
	pts_control_value = readl(&iport->port_task_scheduler_registers->control);
D
Dan Williams 已提交
1488 1489
	pts_control_value &=
		~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
1490
	writel(pts_control_value, &iport->port_task_scheduler_registers->control);
D
Dan Williams 已提交
1491 1492
}

1493
static void sci_port_post_dummy_remote_node(struct isci_port *iport)
D
Dan Williams 已提交
1494
{
1495
	struct isci_host *ihost = iport->owning_controller;
1496
	u8 phys_index = iport->physical_port_index;
D
Dan Williams 已提交
1497
	union scu_remote_node_context *rnc;
1498
	u16 rni = iport->reserved_rni;
D
Dan Williams 已提交
1499 1500
	u32 command;

1501
	rnc = &ihost->remote_node_context_table[rni];
D
Dan Williams 已提交
1502 1503 1504 1505 1506
	rnc->ssp.is_valid = true;

	command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
		  phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;

1507
	sci_controller_post_request(ihost, command);
D
Dan Williams 已提交
1508 1509 1510 1511

	/* ensure hardware has seen the post rnc command and give it
	 * ample time to act before sending the suspend
	 */
1512
	readl(&ihost->smu_registers->interrupt_status); /* flush */
D
Dan Williams 已提交
1513 1514 1515 1516 1517
	udelay(10);

	command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
		  phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;

1518
	sci_controller_post_request(ihost, command);
D
Dan Williams 已提交
1519 1520
}

1521
static void sci_port_stopped_state_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1522
{
1523
	struct isci_port *iport = container_of(sm, typeof(*iport), sm);
D
Dan Williams 已提交
1524

1525
	if (iport->sm.previous_state_id == SCI_PORT_STOPPING) {
D
Dan Williams 已提交
1526 1527 1528 1529
		/*
		 * If we enter this state becasuse of a request to stop
		 * the port then we want to disable the hardwares port
		 * task scheduler. */
1530
		sci_port_disable_port_task_scheduler(iport);
D
Dan Williams 已提交
1531 1532 1533
	}
}

1534
static void sci_port_stopped_state_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1535
{
1536
	struct isci_port *iport = container_of(sm, typeof(*iport), sm);
D
Dan Williams 已提交
1537 1538

	/* Enable and suspend the port task scheduler */
1539
	sci_port_enable_port_task_scheduler(iport);
D
Dan Williams 已提交
1540 1541
}

1542
static void sci_port_ready_state_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1543
{
1544
	struct isci_port *iport = container_of(sm, typeof(*iport), sm);
1545
	struct isci_host *ihost = iport->owning_controller;
D
Dan Williams 已提交
1546 1547
	u32 prev_state;

1548
	prev_state = iport->sm.previous_state_id;
E
Edmund Nadolski 已提交
1549
	if (prev_state  == SCI_PORT_RESETTING)
D
Dan Williams 已提交
1550 1551 1552 1553 1554
		isci_port_hard_reset_complete(iport, SCI_SUCCESS);
	else
		isci_port_not_ready(ihost, iport);

	/* Post and suspend the dummy remote node context for this port. */
1555
	sci_port_post_dummy_remote_node(iport);
D
Dan Williams 已提交
1556 1557

	/* Start the ready substate machine */
1558
	port_state_machine_change(iport,
E
Edmund Nadolski 已提交
1559
				  SCI_PORT_SUB_WAITING);
D
Dan Williams 已提交
1560 1561
}

1562
static void sci_port_resetting_state_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1563
{
1564
	struct isci_port *iport = container_of(sm, typeof(*iport), sm);
D
Dan Williams 已提交
1565

1566
	sci_del_timer(&iport->timer);
D
Dan Williams 已提交
1567 1568
}

1569
static void sci_port_stopping_state_exit(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1570
{
1571
	struct isci_port *iport = container_of(sm, typeof(*iport), sm);
D
Dan Williams 已提交
1572

1573
	sci_del_timer(&iport->timer);
D
Dan Williams 已提交
1574

1575
	sci_port_destroy_dummy_resources(iport);
D
Dan Williams 已提交
1576 1577
}

1578
static void sci_port_failed_state_enter(struct sci_base_state_machine *sm)
D
Dan Williams 已提交
1579
{
1580
	struct isci_port *iport = container_of(sm, typeof(*iport), sm);
D
Dan Williams 已提交
1581 1582 1583 1584 1585 1586

	isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
}

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

1587
static const struct sci_base_state sci_port_state_table[] = {
E
Edmund Nadolski 已提交
1588
	[SCI_PORT_STOPPED] = {
1589 1590
		.enter_state = sci_port_stopped_state_enter,
		.exit_state  = sci_port_stopped_state_exit
D
Dan Williams 已提交
1591
	},
E
Edmund Nadolski 已提交
1592
	[SCI_PORT_STOPPING] = {
1593
		.exit_state  = sci_port_stopping_state_exit
D
Dan Williams 已提交
1594
	},
E
Edmund Nadolski 已提交
1595
	[SCI_PORT_READY] = {
1596
		.enter_state = sci_port_ready_state_enter,
1597
	},
E
Edmund Nadolski 已提交
1598
	[SCI_PORT_SUB_WAITING] = {
1599
		.enter_state = sci_port_ready_substate_waiting_enter,
1600
	},
E
Edmund Nadolski 已提交
1601
	[SCI_PORT_SUB_OPERATIONAL] = {
1602 1603
		.enter_state = sci_port_ready_substate_operational_enter,
		.exit_state  = sci_port_ready_substate_operational_exit
1604
	},
E
Edmund Nadolski 已提交
1605
	[SCI_PORT_SUB_CONFIGURING] = {
1606 1607
		.enter_state = sci_port_ready_substate_configuring_enter,
		.exit_state  = sci_port_ready_substate_configuring_exit
D
Dan Williams 已提交
1608
	},
E
Edmund Nadolski 已提交
1609
	[SCI_PORT_RESETTING] = {
1610
		.exit_state  = sci_port_resetting_state_exit
D
Dan Williams 已提交
1611
	},
E
Edmund Nadolski 已提交
1612
	[SCI_PORT_FAILED] = {
1613
		.enter_state = sci_port_failed_state_enter,
D
Dan Williams 已提交
1614 1615 1616
	}
};

1617
void sci_port_construct(struct isci_port *iport, u8 index,
1618
			     struct isci_host *ihost)
D
Dan Williams 已提交
1619
{
1620
	sci_init_sm(&iport->sm, sci_port_state_table, SCI_PORT_STOPPED);
D
Dan Williams 已提交
1621

1622 1623 1624 1625
	iport->logical_port_index  = SCIC_SDS_DUMMY_PORT;
	iport->physical_port_index = index;
	iport->active_phy_mask     = 0;
	iport->ready_exit	      = false;
D
Dan Williams 已提交
1626

1627
	iport->owning_controller = ihost;
D
Dan Williams 已提交
1628

1629 1630
	iport->started_request_count = 0;
	iport->assigned_device_count = 0;
D
Dan Williams 已提交
1631

1632 1633
	iport->reserved_rni = SCU_DUMMY_INDEX;
	iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
D
Dan Williams 已提交
1634

1635
	sci_init_timer(&iport->timer, port_timeout);
1636

1637
	iport->port_task_scheduler_registers = NULL;
D
Dan Williams 已提交
1638 1639

	for (index = 0; index < SCI_MAX_PHYS; index++)
1640
		iport->phy_table[index] = NULL;
D
Dan Williams 已提交
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
}

void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
{
	INIT_LIST_HEAD(&iport->remote_dev_list);
	INIT_LIST_HEAD(&iport->domain_dev_list);
	spin_lock_init(&iport->state_lock);
	init_completion(&iport->start_complete);
	iport->isci_host = ihost;
	isci_port_change_state(iport, isci_freed);
1651
	atomic_set(&iport->event, 0);
D
Dan Williams 已提交
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
}

/**
 * isci_port_get_state() - This function gets the status of the port object.
 * @isci_port: This parameter points to the isci_port object
 *
 * status of the object as a isci_status enum.
 */
enum isci_status isci_port_get_state(
	struct isci_port *isci_port)
{
	return isci_port->status;
}

1666
void sci_port_broadcast_change_received(struct isci_port *iport, struct isci_phy *iphy)
D
Dan Williams 已提交
1667
{
1668
	struct isci_host *ihost = iport->owning_controller;
D
Dan Williams 已提交
1669 1670

	/* notify the user. */
1671
	isci_port_bc_change_received(ihost, iport, iphy);
D
Dan Williams 已提交
1672 1673 1674 1675 1676 1677 1678
}

int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
				 struct isci_phy *iphy)
{
	unsigned long flags;
	enum sci_status status;
1679
	int idx, ret = TMF_RESP_FUNC_COMPLETE;
D
Dan Williams 已提交
1680 1681 1682 1683 1684 1685 1686 1687 1688

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

	init_completion(&iport->hard_reset_complete);

	spin_lock_irqsave(&ihost->scic_lock, flags);

	#define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
1689
	status = sci_port_hard_reset(iport, ISCI_PORT_RESET_TIMEOUT);
D
Dan Williams 已提交
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705

	spin_unlock_irqrestore(&ihost->scic_lock, flags);

	if (status == SCI_SUCCESS) {
		wait_for_completion(&iport->hard_reset_complete);

		dev_dbg(&ihost->pdev->dev,
			"%s: iport = %p; hard reset completion\n",
			__func__, iport);

		if (iport->hard_reset_status != SCI_SUCCESS)
			ret = TMF_RESP_FUNC_FAILED;
	} else {
		ret = TMF_RESP_FUNC_FAILED;

		dev_err(&ihost->pdev->dev,
1706
			"%s: iport = %p; sci_port_hard_reset call"
D
Dan Williams 已提交
1707 1708 1709 1710 1711 1712 1713 1714 1715
			" failed 0x%x\n",
			__func__, iport, status);

	}

	/* If the hard reset for the port has failed, consider this
	 * the same as link failures on all phys in the port.
	 */
	if (ret != TMF_RESP_FUNC_COMPLETE) {
1716

D
Dan Williams 已提交
1717 1718
		dev_err(&ihost->pdev->dev,
			"%s: iport = %p; hard reset failed "
1719 1720
			"(0x%x) - driving explicit link fail for all phys\n",
			__func__, iport, iport->hard_reset_status);
D
Dan Williams 已提交
1721

1722 1723 1724
		/* Down all phys in the port. */
		spin_lock_irqsave(&ihost->scic_lock, flags);
		for (idx = 0; idx < SCI_MAX_PHYS; ++idx) {
1725
			struct isci_phy *iphy = iport->phy_table[idx];
1726

1727 1728
			if (!iphy)
				continue;
1729 1730
			sci_phy_stop(iphy);
			sci_phy_start(iphy);
1731 1732 1733
		}
		spin_unlock_irqrestore(&ihost->scic_lock, flags);
	}
D
Dan Williams 已提交
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
	return ret;
}

/**
 * isci_port_deformed() - This function is called by libsas when a port becomes
 *    inactive.
 * @phy: This parameter specifies the libsas phy with the inactive port.
 *
 */
void isci_port_deformed(struct asd_sas_phy *phy)
{
	pr_debug("%s: sas_phy = %p\n", __func__, phy);
}

/**
 * isci_port_formed() - This function is called by libsas when a port becomes
 *    active.
 * @phy: This parameter specifies the libsas phy with the active port.
 *
 */
void isci_port_formed(struct asd_sas_phy *phy)
{
	pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
1757
}