port.c 12.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/*
 * 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.
 */

/**
 * This file contains the isci port implementation.
 *
 *
 */


#include <linux/workqueue.h>
#include "isci.h"
#include "scic_io_request.h"
#include "scic_phy.h"
#include "scic_sds_phy.h"
#include "scic_port.h"
#include "port.h"
#include "request.h"
71
#include "core/scic_sds_controller.h"
72 73 74 75 76 77 78

static void isci_port_change_state(
	struct isci_port *isci_port,
	enum isci_status status);



79
void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
80
{
81
	struct scic_sds_port *sci_port;
82

83 84 85 86 87 88
	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);
89

90 91 92
	sci_port = &ihost->sci.port_table[index];
	iport->sci_port_handle = sci_port;
	sci_port->iport = iport;
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
}


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

static void isci_port_change_state(
	struct isci_port *isci_port,
	enum isci_status status)
{
	unsigned long flags;

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

	spin_lock_irqsave(&isci_port->state_lock, flags);
	isci_port->status = status;
	spin_unlock_irqrestore(&isci_port->state_lock, flags);
}

D
Dan Williams 已提交
123 124 125
void isci_port_bc_change_received(struct isci_host *ihost,
				  struct scic_sds_port *sci_port,
				  struct scic_sds_phy *sci_phy)
126
{
D
Dan Williams 已提交
127
	struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
128

D
Dan Williams 已提交
129 130
	dev_dbg(&ihost->pdev->dev, "%s: iphy = %p, sas_phy = %p\n",
		__func__, iphy, &iphy->sas_phy);
131

D
Dan Williams 已提交
132 133
	ihost->sas_ha.notify_port_event(&iphy->sas_phy, PORTE_BROADCAST_RCVD);
	scic_port_enable_broadcast_change_notification(sci_port);
134 135
}

D
Dan Williams 已提交
136 137 138
void isci_port_link_up(struct isci_host *isci_host,
		       struct scic_sds_port *port,
		       struct scic_sds_phy *phy)
139 140 141
{
	unsigned long flags;
	struct scic_port_properties properties;
D
Dan Williams 已提交
142
	struct isci_phy *isci_phy = sci_phy_to_iphy(phy);
143
	struct isci_port *isci_port = port->iport;
144 145 146
	unsigned long success = true;

	BUG_ON(isci_phy->isci_port != NULL);
147

148 149 150 151 152 153 154 155 156 157 158 159
	isci_phy->isci_port = isci_port;

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

	spin_lock_irqsave(&isci_phy->sas_phy.frame_rcvd_lock, flags);

	isci_port_change_state(isci_phy->isci_port, isci_starting);

	scic_port_get_properties(port, &properties);

160
	if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
D
Dan Williams 已提交
161
		u64 attached_sas_address;
162 163

		isci_phy->sas_phy.oob_mode = SATA_OOB_MODE;
164
		isci_phy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
165 166 167 168 169 170 171 172

		/*
		 * 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
		 * to be obtained from struct scic_port_properties properties.
		 */
D
Dan Williams 已提交
173 174 175 176
		attached_sas_address = properties.remote.sas_address.high;
		attached_sas_address <<= 32;
		attached_sas_address |= properties.remote.sas_address.low;
		swab64s(&attached_sas_address);
177

D
Dan Williams 已提交
178 179
		memcpy(&isci_phy->sas_phy.attached_sas_addr,
		       &attached_sas_address, sizeof(attached_sas_address));
180
	} else if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
181
		isci_phy->sas_phy.oob_mode = SAS_OOB_MODE;
182
		isci_phy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
183 184 185

		/* Copy the attached SAS address from the IAF */
		memcpy(isci_phy->sas_phy.attached_sas_addr,
186
		       isci_phy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
187 188 189 190 191
	} else {
		dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
		success = false;
	}

D
Dan Williams 已提交
192 193
	isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);

	/* Notify libsas that we have an address frame, if indeed
	 * we've found an SSP, SMP, or STP target */
	if (success)
		isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
						    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.
 *
 */
212 213
void isci_port_link_down(struct isci_host *isci_host, struct isci_phy *isci_phy,
			 struct isci_port *isci_port)
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
{
	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. */
		if (isci_phy->sas_phy.port
		    && isci_phy->sas_phy.port->num_phys == 1) {

			/* 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
			 */
			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);
				isci_remote_device_change_state(isci_device,
								isci_stopping);
			}
		}
		isci_port_change_state(isci_port, isci_stopping);
	}

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

/**
 * 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.
 *
 */
289
void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
{
	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.
 *
 */
307
void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
308 309 310 311 312 313 314 315 316 317 318 319 320
{
	dev_dbg(&isci_host->pdev->dev,
		"%s: isci_port = %p\n", __func__, isci_port);
}

/**
 * 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.
 *
 */
321 322
void isci_port_hard_reset_complete(struct isci_port *isci_port,
				   enum sci_status completion_status)
323 324 325 326 327 328 329 330 331 332
{
	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);
}
333 334 335

int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
				 struct isci_phy *iphy)
336
{
337
	unsigned long flags;
338 339 340
	enum sci_status status;
	int ret = TMF_RESP_FUNC_COMPLETE;

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

344
	init_completion(&iport->hard_reset_complete);
345

346
	spin_lock_irqsave(&ihost->scic_lock, flags);
347 348

	#define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
349
	status = scic_port_hard_reset(iport->sci_port_handle,
350 351
				      ISCI_PORT_RESET_TIMEOUT);

352
	spin_unlock_irqrestore(&ihost->scic_lock, flags);
353 354

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

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

361
		if (iport->hard_reset_status != SCI_SUCCESS)
362 363 364 365
			ret = TMF_RESP_FUNC_FAILED;
	} else {
		ret = TMF_RESP_FUNC_FAILED;

366 367
		dev_err(&ihost->pdev->dev,
			"%s: iport = %p; scic_port_hard_reset call"
368
			" failed 0x%x\n",
369
			__func__, iport, status);
370 371 372 373 374 375 376

	}

	/* 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) {
377 378
		dev_err(&ihost->pdev->dev,
			"%s: iport = %p; hard reset failed "
379
			"(0x%x) - sending link down to libsas for phy %p\n",
380 381 382
			__func__, iport, iport->hard_reset_status, iphy);

		isci_port_link_down(ihost, iphy, iport);
383 384 385 386
	}

	return ret;
}
387 388 389 390 391

void isci_port_stop_complete(struct scic_sds_controller *scic,
					  struct scic_sds_port *sci_port,
					  enum sci_status completion_status)
{
392
	dev_dbg(&scic_to_ihost(scic)->pdev->dev, "Port stop complete\n");
393
}