File Index
Files and directories
Sign in
FILE

mpt3sas_transport.c

File preview with syntax highlighting.

linux-6.19.14/drivers/scsi/mpt3sas/mpt3sas_transport.c C
   1/*
   2 * SAS Transport Layer for MPT (Message Passing Technology) based controllers
   3 *
   4 * This code is based on drivers/scsi/mpt3sas/mpt3sas_transport.c
   5 * Copyright (C) 2012-2014  LSI Corporation
   6 * Copyright (C) 2013-2014 Avago Technologies
   7 *  (mailto: MPT-FusionLinux.pdl@avagotech.com)
   8 *
   9 * This program is free software; you can redistribute it and/or
  10 * modify it under the terms of the GNU General Public License
  11 * as published by the Free Software Foundation; either version 2
  12 * of the License, or (at your option) any later version.
  13 *
  14 * This program is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17 * GNU General Public License for more details.
  18 *
  19 * NO WARRANTY
  20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  24 * solely responsible for determining the appropriateness of using and
  25 * distributing the Program and assumes all risks associated with its
  26 * exercise of rights under this Agreement, including but not limited to
  27 * the risks and costs of program errors, damage to or loss of data,
  28 * programs or equipment, and unavailability or interruption of operations.
  29
  30 * DISCLAIMER OF LIABILITY
  31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  38
  39 * You should have received a copy of the GNU General Public License
  40 * along with this program; if not, write to the Free Software
  41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
  42 * USA.
  43 */
  44
  45#include <linux/module.h>
  46#include <linux/kernel.h>
  47#include <linux/init.h>
  48#include <linux/errno.h>
  49#include <linux/sched.h>
  50#include <linux/workqueue.h>
  51#include <linux/delay.h>
  52#include <linux/pci.h>
  53
  54#include <scsi/scsi.h>
  55#include <scsi/scsi_cmnd.h>
  56#include <scsi/scsi_device.h>
  57#include <scsi/scsi_host.h>
  58#include <scsi/scsi_transport_sas.h>
  59#include <scsi/scsi_dbg.h>
  60
  61#include "mpt3sas_base.h"
  62
  63/**
  64 * _transport_get_port_id_by_sas_phy - get zone's port id that Phy belong to
  65 * @phy: sas_phy object
  66 *
  67 * Return Port number
  68 */
  69static inline u8
  70_transport_get_port_id_by_sas_phy(struct sas_phy *phy)
  71{
  72	u8 port_id = 0xFF;
  73	struct hba_port *port = phy->hostdata;
  74
  75	if (port)
  76		port_id = port->port_id;
  77
  78	return port_id;
  79}
  80
  81/**
  82 * _transport_sas_node_find_by_sas_address - sas node search
  83 * @ioc: per adapter object
  84 * @sas_address: sas address of expander or sas host
  85 * @port: hba port entry
  86 * Context: Calling function should acquire ioc->sas_node_lock.
  87 *
  88 * Search for either hba phys or expander device based on handle, then returns
  89 * the sas_node object.
  90 */
  91static struct _sas_node *
  92_transport_sas_node_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
  93	u64 sas_address, struct hba_port *port)
  94{
  95	if (ioc->sas_hba.sas_address == sas_address)
  96		return &ioc->sas_hba;
  97	else
  98		return mpt3sas_scsih_expander_find_by_sas_address(ioc,
  99		    sas_address, port);
 100}
 101
 102/**
 103 * _transport_get_port_id_by_rphy - Get Port number from rphy object
 104 * @ioc: per adapter object
 105 * @rphy: sas_rphy object
 106 *
 107 * Returns Port number.
 108 */
 109static u8
 110_transport_get_port_id_by_rphy(struct MPT3SAS_ADAPTER *ioc,
 111	struct sas_rphy *rphy)
 112{
 113	struct _sas_node *sas_expander;
 114	struct _sas_device *sas_device;
 115	unsigned long flags;
 116	u8 port_id = 0xFF;
 117
 118	if (!rphy)
 119		return port_id;
 120
 121	if (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
 122	    rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE) {
 123		spin_lock_irqsave(&ioc->sas_node_lock, flags);
 124		list_for_each_entry(sas_expander,
 125		    &ioc->sas_expander_list, list) {
 126			if (sas_expander->rphy == rphy) {
 127				port_id = sas_expander->port->port_id;
 128				break;
 129			}
 130		}
 131		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
 132	} else if (rphy->identify.device_type == SAS_END_DEVICE) {
 133		spin_lock_irqsave(&ioc->sas_device_lock, flags);
 134		sas_device = __mpt3sas_get_sdev_by_rphy(ioc, rphy);
 135		if (sas_device) {
 136			port_id = sas_device->port->port_id;
 137			sas_device_put(sas_device);
 138		}
 139		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 140	}
 141
 142	return port_id;
 143}
 144
 145/**
 146 * _transport_convert_phy_link_rate -
 147 * @link_rate: link rate returned from mpt firmware
 148 *
 149 * Convert link_rate from mpi fusion into sas_transport form.
 150 */
 151static enum sas_linkrate
 152_transport_convert_phy_link_rate(u8 link_rate)
 153{
 154	enum sas_linkrate rc;
 155
 156	switch (link_rate) {
 157	case MPI2_SAS_NEG_LINK_RATE_1_5:
 158		rc = SAS_LINK_RATE_1_5_GBPS;
 159		break;
 160	case MPI2_SAS_NEG_LINK_RATE_3_0:
 161		rc = SAS_LINK_RATE_3_0_GBPS;
 162		break;
 163	case MPI2_SAS_NEG_LINK_RATE_6_0:
 164		rc = SAS_LINK_RATE_6_0_GBPS;
 165		break;
 166	case MPI25_SAS_NEG_LINK_RATE_12_0:
 167		rc = SAS_LINK_RATE_12_0_GBPS;
 168		break;
 169	case MPI26_SAS_NEG_LINK_RATE_22_5:
 170		rc = SAS_LINK_RATE_22_5_GBPS;
 171		break;
 172	case MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED:
 173		rc = SAS_PHY_DISABLED;
 174		break;
 175	case MPI2_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED:
 176		rc = SAS_LINK_RATE_FAILED;
 177		break;
 178	case MPI2_SAS_NEG_LINK_RATE_PORT_SELECTOR:
 179		rc = SAS_SATA_PORT_SELECTOR;
 180		break;
 181	case MPI2_SAS_NEG_LINK_RATE_SMP_RESET_IN_PROGRESS:
 182		rc = SAS_PHY_RESET_IN_PROGRESS;
 183		break;
 184
 185	default:
 186	case MPI2_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE:
 187	case MPI2_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE:
 188		rc = SAS_LINK_RATE_UNKNOWN;
 189		break;
 190	}
 191	return rc;
 192}
 193
 194/**
 195 * _transport_set_identify - set identify for phys and end devices
 196 * @ioc: per adapter object
 197 * @handle: device handle
 198 * @identify: sas identify info
 199 *
 200 * Populates sas identify info.
 201 *
 202 * Return: 0 for success, non-zero for failure.
 203 */
 204static int
 205_transport_set_identify(struct MPT3SAS_ADAPTER *ioc, u16 handle,
 206	struct sas_identify *identify)
 207{
 208	Mpi2SasDevicePage0_t sas_device_pg0;
 209	Mpi2ConfigReply_t mpi_reply;
 210	u32 device_info;
 211	u32 ioc_status;
 212
 213	if (ioc->shost_recovery || ioc->pci_error_recovery) {
 214		ioc_info(ioc, "%s: host reset in progress!\n", __func__);
 215		return -EFAULT;
 216	}
 217
 218	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
 219	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
 220		ioc_err(ioc, "failure at %s:%d/%s()!\n",
 221			__FILE__, __LINE__, __func__);
 222		return -ENXIO;
 223	}
 224
 225	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
 226	    MPI2_IOCSTATUS_MASK;
 227	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
 228		ioc_err(ioc, "handle(0x%04x), ioc_status(0x%04x) failure at %s:%d/%s()!\n",
 229			handle, ioc_status, __FILE__, __LINE__, __func__);
 230		return -EIO;
 231	}
 232
 233	memset(identify, 0, sizeof(struct sas_identify));
 234	device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
 235
 236	/* sas_address */
 237	identify->sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
 238
 239	/* phy number of the parent device this device is linked to */
 240	identify->phy_identifier = sas_device_pg0.PhyNum;
 241
 242	/* device_type */
 243	switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
 244	case MPI2_SAS_DEVICE_INFO_NO_DEVICE:
 245		identify->device_type = SAS_PHY_UNUSED;
 246		break;
 247	case MPI2_SAS_DEVICE_INFO_END_DEVICE:
 248		identify->device_type = SAS_END_DEVICE;
 249		break;
 250	case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER:
 251		identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
 252		break;
 253	case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER:
 254		identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
 255		break;
 256	}
 257
 258	/* initiator_port_protocols */
 259	if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR)
 260		identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
 261	if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR)
 262		identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
 263	if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR)
 264		identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
 265	if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST)
 266		identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
 267
 268	/* target_port_protocols */
 269	if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
 270		identify->target_port_protocols |= SAS_PROTOCOL_SSP;
 271	if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
 272		identify->target_port_protocols |= SAS_PROTOCOL_STP;
 273	if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET)
 274		identify->target_port_protocols |= SAS_PROTOCOL_SMP;
 275	if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
 276		identify->target_port_protocols |= SAS_PROTOCOL_SATA;
 277
 278	return 0;
 279}
 280
 281/**
 282 * mpt3sas_transport_done -  internal transport layer callback handler.
 283 * @ioc: per adapter object
 284 * @smid: system request message index
 285 * @msix_index: MSIX table index supplied by the OS
 286 * @reply: reply message frame(lower 32bit addr)
 287 *
 288 * Callback handler when sending internal generated transport cmds.
 289 * The callback index passed is `ioc->transport_cb_idx`
 290 *
 291 * Return: 1 meaning mf should be freed from _base_interrupt
 292 *         0 means the mf is freed from this function.
 293 */
 294u8
 295mpt3sas_transport_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
 296	u32 reply)
 297{
 298	MPI2DefaultReply_t *mpi_reply;
 299
 300	mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
 301	if (ioc->transport_cmds.status == MPT3_CMD_NOT_USED)
 302		return 1;
 303	if (ioc->transport_cmds.smid != smid)
 304		return 1;
 305	ioc->transport_cmds.status |= MPT3_CMD_COMPLETE;
 306	if (mpi_reply) {
 307		memcpy(ioc->transport_cmds.reply, mpi_reply,
 308		    mpi_reply->MsgLength*4);
 309		ioc->transport_cmds.status |= MPT3_CMD_REPLY_VALID;
 310	}
 311	ioc->transport_cmds.status &= ~MPT3_CMD_PENDING;
 312	complete(&ioc->transport_cmds.done);
 313	return 1;
 314}
 315
 316/* report manufacture request structure */
 317struct rep_manu_request {
 318	u8 smp_frame_type;
 319	u8 function;
 320	u8 reserved;
 321	u8 request_length;
 322};
 323
 324/* report manufacture reply structure */
 325struct rep_manu_reply {
 326	u8 smp_frame_type; /* 0x41 */
 327	u8 function; /* 0x01 */
 328	u8 function_result;
 329	u8 response_length;
 330	u16 expander_change_count;
 331	u8 reserved0[2];
 332	u8 sas_format;
 333	u8 reserved2[3];
 334	u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN] __nonstring;
 335	u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN] __nonstring;
 336	u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN] __nonstring;
 337	u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN] __nonstring;
 338	u16 component_id;
 339	u8 component_revision_id;
 340	u8 reserved3;
 341	u8 vendor_specific[8];
 342};
 343
 344/**
 345 * _transport_expander_report_manufacture - obtain SMP report_manufacture
 346 * @ioc: per adapter object
 347 * @sas_address: expander sas address
 348 * @edev: the sas_expander_device object
 349 * @port_id: Port ID number
 350 *
 351 * Fills in the sas_expander_device object when SMP port is created.
 352 *
 353 * Return: 0 for success, non-zero for failure.
 354 */
 355static int
 356_transport_expander_report_manufacture(struct MPT3SAS_ADAPTER *ioc,
 357	u64 sas_address, struct sas_expander_device *edev, u8 port_id)
 358{
 359	Mpi2SmpPassthroughRequest_t *mpi_request;
 360	Mpi2SmpPassthroughReply_t *mpi_reply;
 361	struct rep_manu_reply *manufacture_reply;
 362	struct rep_manu_request *manufacture_request;
 363	int rc;
 364	u16 smid;
 365	void *psge;
 366	u8 issue_reset = 0;
 367	void *data_out = NULL;
 368	dma_addr_t data_out_dma;
 369	dma_addr_t data_in_dma;
 370	size_t data_in_sz;
 371	size_t data_out_sz;
 372
 373	if (ioc->shost_recovery || ioc->pci_error_recovery) {
 374		ioc_info(ioc, "%s: host reset in progress!\n", __func__);
 375		return -EFAULT;
 376	}
 377
 378	mutex_lock(&ioc->transport_cmds.mutex);
 379
 380	if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
 381		ioc_err(ioc, "%s: transport_cmds in use\n", __func__);
 382		rc = -EAGAIN;
 383		goto out;
 384	}
 385	ioc->transport_cmds.status = MPT3_CMD_PENDING;
 386
 387	rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
 388	if (rc)
 389		goto out;
 390
 391	smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
 392	if (!smid) {
 393		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
 394		rc = -EAGAIN;
 395		goto out;
 396	}
 397
 398	rc = 0;
 399	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
 400	ioc->transport_cmds.smid = smid;
 401
 402	data_out_sz = sizeof(struct rep_manu_request);
 403	data_in_sz = sizeof(struct rep_manu_reply);
 404	data_out = dma_alloc_coherent(&ioc->pdev->dev, data_out_sz + data_in_sz,
 405			&data_out_dma, GFP_KERNEL);
 406	if (!data_out) {
 407		pr_err("failure at %s:%d/%s()!\n", __FILE__,
 408		    __LINE__, __func__);
 409		rc = -ENOMEM;
 410		mpt3sas_base_free_smid(ioc, smid);
 411		goto out;
 412	}
 413
 414	data_in_dma = data_out_dma + sizeof(struct rep_manu_request);
 415
 416	manufacture_request = data_out;
 417	manufacture_request->smp_frame_type = 0x40;
 418	manufacture_request->function = 1;
 419	manufacture_request->reserved = 0;
 420	manufacture_request->request_length = 0;
 421
 422	memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
 423	mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
 424	mpi_request->PhysicalPort = port_id;
 425	mpi_request->SASAddress = cpu_to_le64(sas_address);
 426	mpi_request->RequestDataLength = cpu_to_le16(data_out_sz);
 427	psge = &mpi_request->SGL;
 428
 429	ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
 430	    data_in_sz);
 431
 432	dtransportprintk(ioc,
 433			 ioc_info(ioc, "report_manufacture - send to sas_addr(0x%016llx)\n",
 434				  (u64)sas_address));
 435	init_completion(&ioc->transport_cmds.done);
 436	ioc->put_smid_default(ioc, smid);
 437	wait_for_completion_timeout(&ioc->transport_cmds.done, 10*HZ);
 438
 439	if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
 440		ioc_err(ioc, "%s: timeout\n", __func__);
 441		_debug_dump_mf(mpi_request,
 442		    sizeof(Mpi2SmpPassthroughRequest_t)/4);
 443		if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
 444			issue_reset = 1;
 445		goto issue_host_reset;
 446	}
 447
 448	dtransportprintk(ioc, ioc_info(ioc, "report_manufacture - complete\n"));
 449
 450	if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
 451		u8 *tmp;
 452
 453		mpi_reply = ioc->transport_cmds.reply;
 454
 455		dtransportprintk(ioc,
 456				 ioc_info(ioc, "report_manufacture - reply data transfer size(%d)\n",
 457					  le16_to_cpu(mpi_reply->ResponseDataLength)));
 458
 459		if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
 460		    sizeof(struct rep_manu_reply))
 461			goto out;
 462
 463		manufacture_reply = data_out + sizeof(struct rep_manu_request);
 464		memtostr(edev->vendor_id, manufacture_reply->vendor_id);
 465		memtostr(edev->product_id, manufacture_reply->product_id);
 466		memtostr(edev->product_rev, manufacture_reply->product_rev);
 467		edev->level = manufacture_reply->sas_format & 1;
 468		if (edev->level) {
 469			memtostr(edev->component_vendor_id,
 470				 manufacture_reply->component_vendor_id);
 471			tmp = (u8 *)&manufacture_reply->component_id;
 472			edev->component_id = tmp[0] << 8 | tmp[1];
 473			edev->component_revision_id =
 474			    manufacture_reply->component_revision_id;
 475		}
 476	} else
 477		dtransportprintk(ioc,
 478				 ioc_info(ioc, "report_manufacture - no reply\n"));
 479
 480 issue_host_reset:
 481	if (issue_reset)
 482		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
 483 out:
 484	ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
 485	if (data_out)
 486		dma_free_coherent(&ioc->pdev->dev, data_out_sz + data_in_sz,
 487		    data_out, data_out_dma);
 488
 489	mutex_unlock(&ioc->transport_cmds.mutex);
 490	return rc;
 491}
 492
 493
 494/**
 495 * _transport_delete_port - helper function to removing a port
 496 * @ioc: per adapter object
 497 * @mpt3sas_port: mpt3sas per port object
 498 */
 499static void
 500_transport_delete_port(struct MPT3SAS_ADAPTER *ioc,
 501	struct _sas_port *mpt3sas_port)
 502{
 503	u64 sas_address = mpt3sas_port->remote_identify.sas_address;
 504	struct hba_port *port = mpt3sas_port->hba_port;
 505	enum sas_device_type device_type =
 506	    mpt3sas_port->remote_identify.device_type;
 507
 508	dev_printk(KERN_INFO, &mpt3sas_port->port->dev,
 509	    "remove: sas_addr(0x%016llx)\n",
 510	    (unsigned long long) sas_address);
 511
 512	ioc->logging_level |= MPT_DEBUG_TRANSPORT;
 513	if (device_type == SAS_END_DEVICE)
 514		mpt3sas_device_remove_by_sas_address(ioc,
 515		    sas_address, port);
 516	else if (device_type == SAS_EDGE_EXPANDER_DEVICE ||
 517	    device_type == SAS_FANOUT_EXPANDER_DEVICE)
 518		mpt3sas_expander_remove(ioc, sas_address, port);
 519	ioc->logging_level &= ~MPT_DEBUG_TRANSPORT;
 520}
 521
 522/**
 523 * _transport_delete_phy - helper function to removing single phy from port
 524 * @ioc: per adapter object
 525 * @mpt3sas_port: mpt3sas per port object
 526 * @mpt3sas_phy: mpt3sas per phy object
 527 */
 528static void
 529_transport_delete_phy(struct MPT3SAS_ADAPTER *ioc,
 530	struct _sas_port *mpt3sas_port, struct _sas_phy *mpt3sas_phy)
 531{
 532	u64 sas_address = mpt3sas_port->remote_identify.sas_address;
 533
 534	dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
 535	    "remove: sas_addr(0x%016llx), phy(%d)\n",
 536	    (unsigned long long) sas_address, mpt3sas_phy->phy_id);
 537
 538	list_del(&mpt3sas_phy->port_siblings);
 539	mpt3sas_port->num_phys--;
 540	sas_port_delete_phy(mpt3sas_port->port, mpt3sas_phy->phy);
 541	mpt3sas_phy->phy_belongs_to_port = 0;
 542}
 543
 544/**
 545 * _transport_add_phy - helper function to adding single phy to port
 546 * @ioc: per adapter object
 547 * @mpt3sas_port: mpt3sas per port object
 548 * @mpt3sas_phy: mpt3sas per phy object
 549 */
 550static void
 551_transport_add_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_port *mpt3sas_port,
 552	struct _sas_phy *mpt3sas_phy)
 553{
 554	u64 sas_address = mpt3sas_port->remote_identify.sas_address;
 555
 556	dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
 557	    "add: sas_addr(0x%016llx), phy(%d)\n", (unsigned long long)
 558	    sas_address, mpt3sas_phy->phy_id);
 559
 560	list_add_tail(&mpt3sas_phy->port_siblings, &mpt3sas_port->phy_list);
 561	mpt3sas_port->num_phys++;
 562	sas_port_add_phy(mpt3sas_port->port, mpt3sas_phy->phy);
 563	mpt3sas_phy->phy_belongs_to_port = 1;
 564}
 565
 566/**
 567 * mpt3sas_transport_add_phy_to_an_existing_port - adding new phy to existing port
 568 * @ioc: per adapter object
 569 * @sas_node: sas node object (either expander or sas host)
 570 * @mpt3sas_phy: mpt3sas per phy object
 571 * @sas_address: sas address of device/expander were phy needs to be added to
 572 * @port: hba port entry
 573 */
 574void
 575mpt3sas_transport_add_phy_to_an_existing_port(struct MPT3SAS_ADAPTER *ioc,
 576	struct _sas_node *sas_node, struct _sas_phy *mpt3sas_phy,
 577	u64 sas_address, struct hba_port *port)
 578{
 579	struct _sas_port *mpt3sas_port;
 580	struct _sas_phy *phy_srch;
 581
 582	if (mpt3sas_phy->phy_belongs_to_port == 1)
 583		return;
 584
 585	if (!port)
 586		return;
 587
 588	list_for_each_entry(mpt3sas_port, &sas_node->sas_port_list,
 589	    port_list) {
 590		if (mpt3sas_port->remote_identify.sas_address !=
 591		    sas_address)
 592			continue;
 593		if (mpt3sas_port->hba_port != port)
 594			continue;
 595		list_for_each_entry(phy_srch, &mpt3sas_port->phy_list,
 596		    port_siblings) {
 597			if (phy_srch == mpt3sas_phy)
 598				return;
 599		}
 600		_transport_add_phy(ioc, mpt3sas_port, mpt3sas_phy);
 601		return;
 602	}
 603
 604}
 605
 606/**
 607 * mpt3sas_transport_del_phy_from_an_existing_port - delete phy from existing port
 608 * @ioc: per adapter object
 609 * @sas_node: sas node object (either expander or sas host)
 610 * @mpt3sas_phy: mpt3sas per phy object
 611 */
 612void
 613mpt3sas_transport_del_phy_from_an_existing_port(struct MPT3SAS_ADAPTER *ioc,
 614	struct _sas_node *sas_node, struct _sas_phy *mpt3sas_phy)
 615{
 616	struct _sas_port *mpt3sas_port, *next;
 617	struct _sas_phy *phy_srch;
 618
 619	if (mpt3sas_phy->phy_belongs_to_port == 0)
 620		return;
 621
 622	list_for_each_entry_safe(mpt3sas_port, next, &sas_node->sas_port_list,
 623	    port_list) {
 624		list_for_each_entry(phy_srch, &mpt3sas_port->phy_list,
 625		    port_siblings) {
 626			if (phy_srch != mpt3sas_phy)
 627				continue;
 628
 629			/*
 630			 * Don't delete port during host reset,
 631			 * just delete phy.
 632			 */
 633			if (mpt3sas_port->num_phys == 1 && !ioc->shost_recovery)
 634				_transport_delete_port(ioc, mpt3sas_port);
 635			else
 636				_transport_delete_phy(ioc, mpt3sas_port,
 637				    mpt3sas_phy);
 638			return;
 639		}
 640	}
 641}
 642
 643/**
 644 * _transport_sanity_check - sanity check when adding a new port
 645 * @ioc: per adapter object
 646 * @sas_node: sas node object (either expander or sas host)
 647 * @sas_address: sas address of device being added
 648 * @port: hba port entry
 649 *
 650 * See the explanation above from _transport_delete_duplicate_port
 651 */
 652static void
 653_transport_sanity_check(struct MPT3SAS_ADAPTER *ioc, struct _sas_node *sas_node,
 654	u64 sas_address, struct hba_port *port)
 655{
 656	int i;
 657
 658	for (i = 0; i < sas_node->num_phys; i++) {
 659		if (sas_node->phy[i].remote_identify.sas_address != sas_address)
 660			continue;
 661		if (sas_node->phy[i].port != port)
 662			continue;
 663		if (sas_node->phy[i].phy_belongs_to_port == 1)
 664			mpt3sas_transport_del_phy_from_an_existing_port(ioc,
 665			    sas_node, &sas_node->phy[i]);
 666	}
 667}
 668
 669/**
 670 * mpt3sas_transport_port_add - insert port to the list
 671 * @ioc: per adapter object
 672 * @handle: handle of attached device
 673 * @sas_address: sas address of parent expander or sas host
 674 * @hba_port: hba port entry
 675 * Context: This function will acquire ioc->sas_node_lock.
 676 *
 677 * Adding new port object to the sas_node->sas_port_list.
 678 *
 679 * Return: mpt3sas_port.
 680 */
 681struct _sas_port *
 682mpt3sas_transport_port_add(struct MPT3SAS_ADAPTER *ioc, u16 handle,
 683	u64 sas_address, struct hba_port *hba_port)
 684{
 685	struct _sas_phy *mpt3sas_phy, *next;
 686	struct _sas_port *mpt3sas_port;
 687	unsigned long flags;
 688	struct _sas_node *sas_node;
 689	struct sas_rphy *rphy;
 690	struct _sas_device *sas_device = NULL;
 691	int i;
 692	struct sas_port *port;
 693	struct virtual_phy *vphy = NULL;
 694
 695	if (!hba_port) {
 696		ioc_err(ioc, "failure at %s:%d/%s()!\n",
 697		    __FILE__, __LINE__, __func__);
 698		return NULL;
 699	}
 700
 701	mpt3sas_port = kzalloc(sizeof(struct _sas_port),
 702	    GFP_KERNEL);
 703	if (!mpt3sas_port) {
 704		ioc_err(ioc, "failure at %s:%d/%s()!\n",
 705			__FILE__, __LINE__, __func__);
 706		return NULL;
 707	}
 708
 709	INIT_LIST_HEAD(&mpt3sas_port->port_list);
 710	INIT_LIST_HEAD(&mpt3sas_port->phy_list);
 711	spin_lock_irqsave(&ioc->sas_node_lock, flags);
 712	sas_node = _transport_sas_node_find_by_sas_address(ioc,
 713	    sas_address, hba_port);
 714	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
 715
 716	if (!sas_node) {
 717		ioc_err(ioc, "%s: Could not find parent sas_address(0x%016llx)!\n",
 718			__func__, (u64)sas_address);
 719		goto out_fail;
 720	}
 721
 722	if ((_transport_set_identify(ioc, handle,
 723	    &mpt3sas_port->remote_identify))) {
 724		ioc_err(ioc, "failure at %s:%d/%s()!\n",
 725			__FILE__, __LINE__, __func__);
 726		goto out_fail;
 727	}
 728
 729	if (mpt3sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
 730		ioc_err(ioc, "failure at %s:%d/%s()!\n",
 731			__FILE__, __LINE__, __func__);
 732		goto out_fail;
 733	}
 734
 735	mpt3sas_port->hba_port = hba_port;
 736	_transport_sanity_check(ioc, sas_node,
 737	    mpt3sas_port->remote_identify.sas_address, hba_port);
 738
 739	for (i = 0; i < sas_node->num_phys; i++) {
 740		if (sas_node->phy[i].remote_identify.sas_address !=
 741		    mpt3sas_port->remote_identify.sas_address)
 742			continue;
 743		if (sas_node->phy[i].port != hba_port)
 744			continue;
 745		list_add_tail(&sas_node->phy[i].port_siblings,
 746		    &mpt3sas_port->phy_list);
 747		mpt3sas_port->num_phys++;
 748		if (sas_node->handle <= ioc->sas_hba.num_phys) {
 749			if (!sas_node->phy[i].hba_vphy) {
 750				hba_port->phy_mask |= (1 << i);
 751				continue;
 752			}
 753
 754			vphy = mpt3sas_get_vphy_by_phy(ioc, hba_port, i);
 755			if (!vphy) {
 756				ioc_err(ioc, "failure at %s:%d/%s()!\n",
 757				    __FILE__, __LINE__, __func__);
 758				goto out_fail;
 759			}
 760		}
 761	}
 762
 763	if (!mpt3sas_port->num_phys) {
 764		ioc_err(ioc, "failure at %s:%d/%s()!\n",
 765			__FILE__, __LINE__, __func__);
 766		goto out_fail;
 767	}
 768
 769	if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) {
 770		sas_device = mpt3sas_get_sdev_by_addr(ioc,
 771		    mpt3sas_port->remote_identify.sas_address,
 772		    mpt3sas_port->hba_port);
 773		if (!sas_device) {
 774			ioc_err(ioc, "failure at %s:%d/%s()!\n",
 775			    __FILE__, __LINE__, __func__);
 776			goto out_fail;
 777		}
 778		sas_device->pend_sas_rphy_add = 1;
 779	}
 780
 781	if (!sas_node->parent_dev) {
 782		ioc_err(ioc, "failure at %s:%d/%s()!\n",
 783			__FILE__, __LINE__, __func__);
 784		goto out_fail;
 785	}
 786	port = sas_port_alloc_num(sas_node->parent_dev);
 787	if (!port || (sas_port_add(port))) {
 788		ioc_err(ioc, "failure at %s:%d/%s()!\n",
 789			__FILE__, __LINE__, __func__);
 790		goto out_fail;
 791	}
 792
 793	list_for_each_entry(mpt3sas_phy, &mpt3sas_port->phy_list,
 794	    port_siblings) {
 795		if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
 796			dev_printk(KERN_INFO, &port->dev,
 797				"add: handle(0x%04x), sas_addr(0x%016llx), phy(%d)\n",
 798				handle, (unsigned long long)
 799			    mpt3sas_port->remote_identify.sas_address,
 800			    mpt3sas_phy->phy_id);
 801		sas_port_add_phy(port, mpt3sas_phy->phy);
 802		mpt3sas_phy->phy_belongs_to_port = 1;
 803		mpt3sas_phy->port = hba_port;
 804	}
 805
 806	mpt3sas_port->port = port;
 807	if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) {
 808		rphy = sas_end_device_alloc(port);
 809		sas_device->rphy = rphy;
 810		if (sas_node->handle <= ioc->sas_hba.num_phys) {
 811			if (!vphy)
 812				hba_port->sas_address =
 813				    sas_device->sas_address;
 814			else
 815				vphy->sas_address =
 816				    sas_device->sas_address;
 817		}
 818	} else {
 819		rphy = sas_expander_alloc(port,
 820		    mpt3sas_port->remote_identify.device_type);
 821		if (sas_node->handle <= ioc->sas_hba.num_phys)
 822			hba_port->sas_address =
 823			    mpt3sas_port->remote_identify.sas_address;
 824	}
 825
 826	if (!rphy) {
 827		ioc_err(ioc, "failure at %s:%d/%s()!\n",
 828			__FILE__, __LINE__, __func__);
 829		goto out_delete_port;
 830	}
 831
 832	rphy->identify = mpt3sas_port->remote_identify;
 833
 834	if ((sas_rphy_add(rphy))) {
 835		ioc_err(ioc, "failure at %s:%d/%s()!\n",
 836			__FILE__, __LINE__, __func__);
 837		sas_rphy_free(rphy);
 838		rphy = NULL;
 839		goto out_delete_port;
 840	}
 841
 842	if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) {
 843		sas_device->pend_sas_rphy_add = 0;
 844		sas_device_put(sas_device);
 845	}
 846
 847	dev_info(&rphy->dev,
 848	    "add: handle(0x%04x), sas_addr(0x%016llx)\n", handle,
 849	    (unsigned long long)mpt3sas_port->remote_identify.sas_address);
 850
 851	mpt3sas_port->rphy = rphy;
 852	spin_lock_irqsave(&ioc->sas_node_lock, flags);
 853	list_add_tail(&mpt3sas_port->port_list, &sas_node->sas_port_list);
 854	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
 855
 856	/* fill in report manufacture */
 857	if (mpt3sas_port->remote_identify.device_type ==
 858	    MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
 859	    mpt3sas_port->remote_identify.device_type ==
 860	    MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
 861		_transport_expander_report_manufacture(ioc,
 862		    mpt3sas_port->remote_identify.sas_address,
 863		    rphy_to_expander_device(rphy), hba_port->port_id);
 864	return mpt3sas_port;
 865
 866out_delete_port:
 867	sas_port_delete(port);
 868
 869out_fail:
 870	list_for_each_entry_safe(mpt3sas_phy, next, &mpt3sas_port->phy_list,
 871	    port_siblings)
 872		list_del(&mpt3sas_phy->port_siblings);
 873	kfree(mpt3sas_port);
 874	return NULL;
 875}
 876
 877/**
 878 * mpt3sas_transport_port_remove - remove port from the list
 879 * @ioc: per adapter object
 880 * @sas_address: sas address of attached device
 881 * @sas_address_parent: sas address of parent expander or sas host
 882 * @port: hba port entry
 883 * Context: This function will acquire ioc->sas_node_lock.
 884 *
 885 * Removing object and freeing associated memory from the
 886 * ioc->sas_port_list.
 887 */
 888void
 889mpt3sas_transport_port_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
 890	u64 sas_address_parent, struct hba_port *port)
 891{
 892	int i;
 893	unsigned long flags;
 894	struct _sas_port *mpt3sas_port, *next;
 895	struct _sas_node *sas_node;
 896	u8 found = 0;
 897	struct _sas_phy *mpt3sas_phy, *next_phy;
 898	struct hba_port *hba_port_next, *hba_port = NULL;
 899	struct virtual_phy *vphy, *vphy_next = NULL;
 900
 901	if (!port)
 902		return;
 903
 904	spin_lock_irqsave(&ioc->sas_node_lock, flags);
 905	sas_node = _transport_sas_node_find_by_sas_address(ioc,
 906	    sas_address_parent, port);
 907	if (!sas_node) {
 908		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
 909		return;
 910	}
 911	list_for_each_entry_safe(mpt3sas_port, next, &sas_node->sas_port_list,
 912	    port_list) {
 913		if (mpt3sas_port->remote_identify.sas_address != sas_address)
 914			continue;
 915		if (mpt3sas_port->hba_port != port)
 916			continue;
 917		found = 1;
 918		list_del(&mpt3sas_port->port_list);
 919		goto out;
 920	}
 921 out:
 922	if (!found) {
 923		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
 924		return;
 925	}
 926
 927	if (sas_node->handle <= ioc->sas_hba.num_phys &&
 928	    (ioc->multipath_on_hba)) {
 929		if (port->vphys_mask) {
 930			list_for_each_entry_safe(vphy, vphy_next,
 931			    &port->vphys_list, list) {
 932				if (vphy->sas_address != sas_address)
 933					continue;
 934				ioc_info(ioc,
 935				    "remove vphy entry: %p of port:%p,from %d port's vphys list\n",
 936				    vphy, port, port->port_id);
 937				port->vphys_mask &= ~vphy->phy_mask;
 938				list_del(&vphy->list);
 939				kfree(vphy);
 940			}
 941		}
 942
 943		list_for_each_entry_safe(hba_port, hba_port_next,
 944		    &ioc->port_table_list, list) {
 945			if (hba_port != port)
 946				continue;
 947			/*
 948			 * Delete hba_port object if
 949			 *  - hba_port object's sas address matches with current
 950			 *    removed device's sas address and no vphy's
 951			 *    associated with it.
 952			 *  - Current removed device is a vSES device and
 953			 *    none of the other direct attached device have
 954			 *    this vSES device's port number (hence hba_port
 955			 *    object sas_address field will be zero).
 956			 */
 957			if ((hba_port->sas_address == sas_address ||
 958			    !hba_port->sas_address) && !hba_port->vphys_mask) {
 959				ioc_info(ioc,
 960				    "remove hba_port entry: %p port: %d from hba_port list\n",
 961				    hba_port, hba_port->port_id);
 962				list_del(&hba_port->list);
 963				kfree(hba_port);
 964			} else if (hba_port->sas_address == sas_address &&
 965			    hba_port->vphys_mask) {
 966				/*
 967				 * Current removed device is a non vSES device
 968				 * and a vSES device has the same port number
 969				 * as of current device's port number. Hence
 970				 * only clear the sas_address filed, don't
 971				 * delete the hba_port object.
 972				 */
 973				ioc_info(ioc,
 974				    "clearing sas_address from hba_port entry: %p port: %d from hba_port list\n",
 975				    hba_port, hba_port->port_id);
 976				port->sas_address = 0;
 977			}
 978			break;
 979		}
 980	}
 981
 982	for (i = 0; i < sas_node->num_phys; i++) {
 983		if (sas_node->phy[i].remote_identify.sas_address == sas_address)
 984			memset(&sas_node->phy[i].remote_identify, 0 ,
 985			    sizeof(struct sas_identify));
 986	}
 987
 988	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
 989
 990	list_for_each_entry_safe(mpt3sas_phy, next_phy,
 991	    &mpt3sas_port->phy_list, port_siblings) {
 992		if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
 993			ioc_info(ioc, "remove: sas_addr(0x%016llx), phy(%d)\n",
 994				(unsigned long long) mpt3sas_port->remote_identify.sas_address,
 995					mpt3sas_phy->phy_id);
 996		mpt3sas_phy->phy_belongs_to_port = 0;
 997		if (!ioc->remove_host)
 998			sas_port_delete_phy(mpt3sas_port->port,
 999						mpt3sas_phy->phy);
1000		list_del(&mpt3sas_phy->port_siblings);
1001	}
1002	if (!ioc->remove_host)
1003		sas_port_delete(mpt3sas_port->port);
1004	ioc_info(ioc, "%s: removed: sas_addr(0x%016llx)\n",
1005	    __func__, (unsigned long long)sas_address);
1006	kfree(mpt3sas_port);
1007}
1008
1009/**
1010 * mpt3sas_transport_add_host_phy - report sas_host phy to transport
1011 * @ioc: per adapter object
1012 * @mpt3sas_phy: mpt3sas per phy object
1013 * @phy_pg0: sas phy page 0
1014 * @parent_dev: parent device class object
1015 *
1016 * Return: 0 for success, non-zero for failure.
1017 */
1018int
1019mpt3sas_transport_add_host_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy
1020	*mpt3sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
1021{
1022	struct sas_phy *phy;
1023	int phy_index = mpt3sas_phy->phy_id;
1024
1025
1026	INIT_LIST_HEAD(&mpt3sas_phy->port_siblings);
1027	phy = sas_phy_alloc(parent_dev, phy_index);
1028	if (!phy) {
1029		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1030			__FILE__, __LINE__, __func__);
1031		return -1;
1032	}
1033	if ((_transport_set_identify(ioc, mpt3sas_phy->handle,
1034	    &mpt3sas_phy->identify))) {
1035		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1036			__FILE__, __LINE__, __func__);
1037		sas_phy_free(phy);
1038		return -1;
1039	}
1040	phy->identify = mpt3sas_phy->identify;
1041	mpt3sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
1042	if (mpt3sas_phy->attached_handle)
1043		_transport_set_identify(ioc, mpt3sas_phy->attached_handle,
1044		    &mpt3sas_phy->remote_identify);
1045	phy->identify.phy_identifier = mpt3sas_phy->phy_id;
1046	phy->negotiated_linkrate = _transport_convert_phy_link_rate(
1047	    phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
1048	phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
1049	    phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
1050	phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
1051	    phy_pg0.HwLinkRate >> 4);
1052	phy->minimum_linkrate = _transport_convert_phy_link_rate(
1053	    phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
1054	phy->maximum_linkrate = _transport_convert_phy_link_rate(
1055	    phy_pg0.ProgrammedLinkRate >> 4);
1056	phy->hostdata = mpt3sas_phy->port;
1057
1058	if ((sas_phy_add(phy))) {
1059		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1060			__FILE__, __LINE__, __func__);
1061		sas_phy_free(phy);
1062		return -1;
1063	}
1064	if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
1065		dev_printk(KERN_INFO, &phy->dev,
1066		    "add: handle(0x%04x), sas_addr(0x%016llx)\n"
1067		    "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
1068		    mpt3sas_phy->handle, (unsigned long long)
1069		    mpt3sas_phy->identify.sas_address,
1070		    mpt3sas_phy->attached_handle,
1071		    (unsigned long long)
1072		    mpt3sas_phy->remote_identify.sas_address);
1073	mpt3sas_phy->phy = phy;
1074	return 0;
1075}
1076
1077
1078/**
1079 * mpt3sas_transport_add_expander_phy - report expander phy to transport
1080 * @ioc: per adapter object
1081 * @mpt3sas_phy: mpt3sas per phy object
1082 * @expander_pg1: expander page 1
1083 * @parent_dev: parent device class object
1084 *
1085 * Return: 0 for success, non-zero for failure.
1086 */
1087int
1088mpt3sas_transport_add_expander_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy
1089	*mpt3sas_phy, Mpi2ExpanderPage1_t expander_pg1,
1090	struct device *parent_dev)
1091{
1092	struct sas_phy *phy;
1093	int phy_index = mpt3sas_phy->phy_id;
1094
1095	INIT_LIST_HEAD(&mpt3sas_phy->port_siblings);
1096	phy = sas_phy_alloc(parent_dev, phy_index);
1097	if (!phy) {
1098		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1099			__FILE__, __LINE__, __func__);
1100		return -1;
1101	}
1102	if ((_transport_set_identify(ioc, mpt3sas_phy->handle,
1103	    &mpt3sas_phy->identify))) {
1104		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1105			__FILE__, __LINE__, __func__);
1106		sas_phy_free(phy);
1107		return -1;
1108	}
1109	phy->identify = mpt3sas_phy->identify;
1110	mpt3sas_phy->attached_handle =
1111	    le16_to_cpu(expander_pg1.AttachedDevHandle);
1112	if (mpt3sas_phy->attached_handle)
1113		_transport_set_identify(ioc, mpt3sas_phy->attached_handle,
1114		    &mpt3sas_phy->remote_identify);
1115	phy->identify.phy_identifier = mpt3sas_phy->phy_id;
1116	phy->negotiated_linkrate = _transport_convert_phy_link_rate(
1117	    expander_pg1.NegotiatedLinkRate &
1118	    MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
1119	phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
1120	    expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
1121	phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
1122	    expander_pg1.HwLinkRate >> 4);
1123	phy->minimum_linkrate = _transport_convert_phy_link_rate(
1124	    expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
1125	phy->maximum_linkrate = _transport_convert_phy_link_rate(
1126	    expander_pg1.ProgrammedLinkRate >> 4);
1127	phy->hostdata = mpt3sas_phy->port;
1128
1129	if ((sas_phy_add(phy))) {
1130		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1131			__FILE__, __LINE__, __func__);
1132		sas_phy_free(phy);
1133		return -1;
1134	}
1135	if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
1136		dev_printk(KERN_INFO, &phy->dev,
1137		    "add: handle(0x%04x), sas_addr(0x%016llx)\n"
1138		    "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
1139		    mpt3sas_phy->handle, (unsigned long long)
1140		    mpt3sas_phy->identify.sas_address,
1141		    mpt3sas_phy->attached_handle,
1142		    (unsigned long long)
1143		    mpt3sas_phy->remote_identify.sas_address);
1144	mpt3sas_phy->phy = phy;
1145	return 0;
1146}
1147
1148/**
1149 * mpt3sas_transport_update_links - refreshing phy link changes
1150 * @ioc: per adapter object
1151 * @sas_address: sas address of parent expander or sas host
1152 * @handle: attached device handle
1153 * @phy_number: phy number
1154 * @link_rate: new link rate
1155 * @port: hba port entry
1156 *
1157 * Return nothing.
1158 */
1159void
1160mpt3sas_transport_update_links(struct MPT3SAS_ADAPTER *ioc,
1161	u64 sas_address, u16 handle, u8 phy_number, u8 link_rate,
1162	struct hba_port *port)
1163{
1164	unsigned long flags;
1165	struct _sas_node *sas_node;
1166	struct _sas_phy *mpt3sas_phy;
1167	struct hba_port *hba_port = NULL;
1168
1169	if (ioc->shost_recovery || ioc->pci_error_recovery)
1170		return;
1171
1172	spin_lock_irqsave(&ioc->sas_node_lock, flags);
1173	sas_node = _transport_sas_node_find_by_sas_address(ioc,
1174	    sas_address, port);
1175	if (!sas_node) {
1176		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1177		return;
1178	}
1179
1180	mpt3sas_phy = &sas_node->phy[phy_number];
1181	mpt3sas_phy->attached_handle = handle;
1182	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1183	if (handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)) {
1184		_transport_set_identify(ioc, handle,
1185		    &mpt3sas_phy->remote_identify);
1186		if ((sas_node->handle <= ioc->sas_hba.num_phys) &&
1187		    (ioc->multipath_on_hba)) {
1188			list_for_each_entry(hba_port,
1189			    &ioc->port_table_list, list) {
1190				if (hba_port->sas_address == sas_address &&
1191				    hba_port == port)
1192					hba_port->phy_mask |=
1193					    (1 << mpt3sas_phy->phy_id);
1194			}
1195		}
1196		mpt3sas_transport_add_phy_to_an_existing_port(ioc, sas_node,
1197		    mpt3sas_phy, mpt3sas_phy->remote_identify.sas_address,
1198		    port);
1199	} else
1200		memset(&mpt3sas_phy->remote_identify, 0 , sizeof(struct
1201		    sas_identify));
1202
1203	if (mpt3sas_phy->phy)
1204		mpt3sas_phy->phy->negotiated_linkrate =
1205		    _transport_convert_phy_link_rate(link_rate);
1206
1207	if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
1208		dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
1209		    "refresh: parent sas_addr(0x%016llx),\n"
1210		    "\tlink_rate(0x%02x), phy(%d)\n"
1211		    "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
1212		    (unsigned long long)sas_address,
1213		    link_rate, phy_number, handle, (unsigned long long)
1214		    mpt3sas_phy->remote_identify.sas_address);
1215}
1216
1217static inline void *
1218phy_to_ioc(struct sas_phy *phy)
1219{
1220	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1221	return shost_priv(shost);
1222}
1223
1224static inline void *
1225rphy_to_ioc(struct sas_rphy *rphy)
1226{
1227	struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1228	return shost_priv(shost);
1229}
1230
1231/* report phy error log structure */
1232struct phy_error_log_request {
1233	u8 smp_frame_type; /* 0x40 */
1234	u8 function; /* 0x11 */
1235	u8 allocated_response_length;
1236	u8 request_length; /* 02 */
1237	u8 reserved_1[5];
1238	u8 phy_identifier;
1239	u8 reserved_2[2];
1240};
1241
1242/* report phy error log reply structure */
1243struct phy_error_log_reply {
1244	u8 smp_frame_type; /* 0x41 */
1245	u8 function; /* 0x11 */
1246	u8 function_result;
1247	u8 response_length;
1248	__be16 expander_change_count;
1249	u8 reserved_1[3];
1250	u8 phy_identifier;
1251	u8 reserved_2[2];
1252	__be32 invalid_dword;
1253	__be32 running_disparity_error;
1254	__be32 loss_of_dword_sync;
1255	__be32 phy_reset_problem;
1256};
1257
1258/**
1259 * _transport_get_expander_phy_error_log - return expander counters
1260 * @ioc: per adapter object
1261 * @phy: The sas phy object
1262 *
1263 * Return: 0 for success, non-zero for failure.
1264 *
1265 */
1266static int
1267_transport_get_expander_phy_error_log(struct MPT3SAS_ADAPTER *ioc,
1268	struct sas_phy *phy)
1269{
1270	Mpi2SmpPassthroughRequest_t *mpi_request;
1271	Mpi2SmpPassthroughReply_t *mpi_reply;
1272	struct phy_error_log_request *phy_error_log_request;
1273	struct phy_error_log_reply *phy_error_log_reply;
1274	int rc;
1275	u16 smid;
1276	void *psge;
1277	u8 issue_reset = 0;
1278	void *data_out = NULL;
1279	dma_addr_t data_out_dma;
1280	u32 sz;
1281
1282	if (ioc->shost_recovery || ioc->pci_error_recovery) {
1283		ioc_info(ioc, "%s: host reset in progress!\n", __func__);
1284		return -EFAULT;
1285	}
1286
1287	mutex_lock(&ioc->transport_cmds.mutex);
1288
1289	if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
1290		ioc_err(ioc, "%s: transport_cmds in use\n", __func__);
1291		rc = -EAGAIN;
1292		goto out;
1293	}
1294	ioc->transport_cmds.status = MPT3_CMD_PENDING;
1295
1296	rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
1297	if (rc)
1298		goto out;
1299
1300	smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
1301	if (!smid) {
1302		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
1303		rc = -EAGAIN;
1304		goto out;
1305	}
1306
1307	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1308	ioc->transport_cmds.smid = smid;
1309
1310	sz = sizeof(struct phy_error_log_request) +
1311	    sizeof(struct phy_error_log_reply);
1312	data_out = dma_alloc_coherent(&ioc->pdev->dev, sz, &data_out_dma,
1313			GFP_KERNEL);
1314	if (!data_out) {
1315		pr_err("failure at %s:%d/%s()!\n", __FILE__,
1316		    __LINE__, __func__);
1317		rc = -ENOMEM;
1318		mpt3sas_base_free_smid(ioc, smid);
1319		goto out;
1320	}
1321
1322	rc = -EINVAL;
1323	memset(data_out, 0, sz);
1324	phy_error_log_request = data_out;
1325	phy_error_log_request->smp_frame_type = 0x40;
1326	phy_error_log_request->function = 0x11;
1327	phy_error_log_request->request_length = 2;
1328	phy_error_log_request->allocated_response_length = 0;
1329	phy_error_log_request->phy_identifier = phy->number;
1330
1331	memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1332	mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1333	mpi_request->PhysicalPort = _transport_get_port_id_by_sas_phy(phy);
1334	mpi_request->VF_ID = 0; /* TODO */
1335	mpi_request->VP_ID = 0;
1336	mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
1337	mpi_request->RequestDataLength =
1338	    cpu_to_le16(sizeof(struct phy_error_log_request));
1339	psge = &mpi_request->SGL;
1340
1341	ioc->build_sg(ioc, psge, data_out_dma,
1342		sizeof(struct phy_error_log_request),
1343	    data_out_dma + sizeof(struct phy_error_log_request),
1344	    sizeof(struct phy_error_log_reply));
1345
1346	dtransportprintk(ioc,
1347			 ioc_info(ioc, "phy_error_log - send to sas_addr(0x%016llx), phy(%d)\n",
1348				  (u64)phy->identify.sas_address,
1349				  phy->number));
1350	init_completion(&ioc->transport_cmds.done);
1351	ioc->put_smid_default(ioc, smid);
1352	wait_for_completion_timeout(&ioc->transport_cmds.done, 10*HZ);
1353
1354	if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
1355		ioc_err(ioc, "%s: timeout\n", __func__);
1356		_debug_dump_mf(mpi_request,
1357		    sizeof(Mpi2SmpPassthroughRequest_t)/4);
1358		if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
1359			issue_reset = 1;
1360		goto issue_host_reset;
1361	}
1362
1363	dtransportprintk(ioc, ioc_info(ioc, "phy_error_log - complete\n"));
1364
1365	if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
1366
1367		mpi_reply = ioc->transport_cmds.reply;
1368
1369		dtransportprintk(ioc,
1370				 ioc_info(ioc, "phy_error_log - reply data transfer size(%d)\n",
1371					  le16_to_cpu(mpi_reply->ResponseDataLength)));
1372
1373		if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1374		    sizeof(struct phy_error_log_reply))
1375			goto out;
1376
1377		phy_error_log_reply = data_out +
1378		    sizeof(struct phy_error_log_request);
1379
1380		dtransportprintk(ioc,
1381				 ioc_info(ioc, "phy_error_log - function_result(%d)\n",
1382					  phy_error_log_reply->function_result));
1383
1384		phy->invalid_dword_count =
1385		    be32_to_cpu(phy_error_log_reply->invalid_dword);
1386		phy->running_disparity_error_count =
1387		    be32_to_cpu(phy_error_log_reply->running_disparity_error);
1388		phy->loss_of_dword_sync_count =
1389		    be32_to_cpu(phy_error_log_reply->loss_of_dword_sync);
1390		phy->phy_reset_problem_count =
1391		    be32_to_cpu(phy_error_log_reply->phy_reset_problem);
1392		rc = 0;
1393	} else
1394		dtransportprintk(ioc,
1395				 ioc_info(ioc, "phy_error_log - no reply\n"));
1396
1397 issue_host_reset:
1398	if (issue_reset)
1399		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1400 out:
1401	ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
1402	if (data_out)
1403		dma_free_coherent(&ioc->pdev->dev, sz, data_out, data_out_dma);
1404
1405	mutex_unlock(&ioc->transport_cmds.mutex);
1406	return rc;
1407}
1408
1409/**
1410 * _transport_get_linkerrors - return phy counters for both hba and expanders
1411 * @phy: The sas phy object
1412 *
1413 * Return: 0 for success, non-zero for failure.
1414 *
1415 */
1416static int
1417_transport_get_linkerrors(struct sas_phy *phy)
1418{
1419	struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
1420	unsigned long flags;
1421	Mpi2ConfigReply_t mpi_reply;
1422	Mpi2SasPhyPage1_t phy_pg1;
1423	struct hba_port *port = phy->hostdata;
1424	int port_id = port->port_id;
1425
1426	spin_lock_irqsave(&ioc->sas_node_lock, flags);
1427	if (_transport_sas_node_find_by_sas_address(ioc,
1428	    phy->identify.sas_address,
1429	    mpt3sas_get_port_by_id(ioc, port_id, 0)) == NULL) {
1430		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1431		return -EINVAL;
1432	}
1433	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1434
1435	if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1436		return _transport_get_expander_phy_error_log(ioc, phy);
1437
1438	/* get hba phy error logs */
1439	if ((mpt3sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
1440		    phy->number))) {
1441		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1442			__FILE__, __LINE__, __func__);
1443		return -ENXIO;
1444	}
1445
1446	if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1447		ioc_info(ioc, "phy(%d), ioc_status (0x%04x), loginfo(0x%08x)\n",
1448			 phy->number,
1449			 le16_to_cpu(mpi_reply.IOCStatus),
1450			 le32_to_cpu(mpi_reply.IOCLogInfo));
1451
1452	phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
1453	phy->running_disparity_error_count =
1454	    le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
1455	phy->loss_of_dword_sync_count =
1456	    le32_to_cpu(phy_pg1.LossDwordSynchCount);
1457	phy->phy_reset_problem_count =
1458	    le32_to_cpu(phy_pg1.PhyResetProblemCount);
1459	return 0;
1460}
1461
1462/**
1463 * _transport_get_enclosure_identifier -
1464 * @rphy: The sas phy object
1465 * @identifier: ?
1466 *
1467 * Obtain the enclosure logical id for an expander.
1468 * Return: 0 for success, non-zero for failure.
1469 */
1470static int
1471_transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
1472{
1473	struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1474	struct _sas_device *sas_device;
1475	unsigned long flags;
1476	int rc;
1477
1478	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1479	sas_device = __mpt3sas_get_sdev_by_rphy(ioc, rphy);
1480	if (sas_device) {
1481		*identifier = sas_device->enclosure_logical_id;
1482		rc = 0;
1483		sas_device_put(sas_device);
1484	} else {
1485		*identifier = 0;
1486		rc = -ENXIO;
1487	}
1488
1489	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1490	return rc;
1491}
1492
1493/**
1494 * _transport_get_bay_identifier -
1495 * @rphy: The sas phy object
1496 *
1497 * Return: the slot id for a device that resides inside an enclosure.
1498 */
1499static int
1500_transport_get_bay_identifier(struct sas_rphy *rphy)
1501{
1502	struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1503	struct _sas_device *sas_device;
1504	unsigned long flags;
1505	int rc;
1506
1507	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1508	sas_device = __mpt3sas_get_sdev_by_rphy(ioc, rphy);
1509	if (sas_device) {
1510		rc = sas_device->slot;
1511		sas_device_put(sas_device);
1512	} else {
1513		rc = -ENXIO;
1514	}
1515	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1516	return rc;
1517}
1518
1519/* phy control request structure */
1520struct phy_control_request {
1521	u8 smp_frame_type; /* 0x40 */
1522	u8 function; /* 0x91 */
1523	u8 allocated_response_length;
1524	u8 request_length; /* 0x09 */
1525	u16 expander_change_count;
1526	u8 reserved_1[3];
1527	u8 phy_identifier;
1528	u8 phy_operation;
1529	u8 reserved_2[13];
1530	u64 attached_device_name;
1531	u8 programmed_min_physical_link_rate;
1532	u8 programmed_max_physical_link_rate;
1533	u8 reserved_3[6];
1534};
1535
1536/* phy control reply structure */
1537struct phy_control_reply {
1538	u8 smp_frame_type; /* 0x41 */
1539	u8 function; /* 0x11 */
1540	u8 function_result;
1541	u8 response_length;
1542};
1543
1544#define SMP_PHY_CONTROL_LINK_RESET	(0x01)
1545#define SMP_PHY_CONTROL_HARD_RESET	(0x02)
1546#define SMP_PHY_CONTROL_DISABLE		(0x03)
1547
1548/**
1549 * _transport_expander_phy_control - expander phy control
1550 * @ioc: per adapter object
1551 * @phy: The sas phy object
1552 * @phy_operation: ?
1553 *
1554 * Return: 0 for success, non-zero for failure.
1555 *
1556 */
1557static int
1558_transport_expander_phy_control(struct MPT3SAS_ADAPTER *ioc,
1559	struct sas_phy *phy, u8 phy_operation)
1560{
1561	Mpi2SmpPassthroughRequest_t *mpi_request;
1562	Mpi2SmpPassthroughReply_t *mpi_reply;
1563	struct phy_control_request *phy_control_request;
1564	struct phy_control_reply *phy_control_reply;
1565	int rc;
1566	u16 smid;
1567	void *psge;
1568	u8 issue_reset = 0;
1569	void *data_out = NULL;
1570	dma_addr_t data_out_dma;
1571	u32 sz;
1572
1573	if (ioc->shost_recovery || ioc->pci_error_recovery) {
1574		ioc_info(ioc, "%s: host reset in progress!\n", __func__);
1575		return -EFAULT;
1576	}
1577
1578	mutex_lock(&ioc->transport_cmds.mutex);
1579
1580	if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
1581		ioc_err(ioc, "%s: transport_cmds in use\n", __func__);
1582		rc = -EAGAIN;
1583		goto out;
1584	}
1585	ioc->transport_cmds.status = MPT3_CMD_PENDING;
1586
1587	rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
1588	if (rc)
1589		goto out;
1590
1591	smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
1592	if (!smid) {
1593		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
1594		rc = -EAGAIN;
1595		goto out;
1596	}
1597
1598	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1599	ioc->transport_cmds.smid = smid;
1600
1601	sz = sizeof(struct phy_control_request) +
1602	    sizeof(struct phy_control_reply);
1603	data_out = dma_alloc_coherent(&ioc->pdev->dev, sz, &data_out_dma,
1604			GFP_KERNEL);
1605	if (!data_out) {
1606		pr_err("failure at %s:%d/%s()!\n", __FILE__,
1607		    __LINE__, __func__);
1608		rc = -ENOMEM;
1609		mpt3sas_base_free_smid(ioc, smid);
1610		goto out;
1611	}
1612
1613	rc = -EINVAL;
1614	memset(data_out, 0, sz);
1615	phy_control_request = data_out;
1616	phy_control_request->smp_frame_type = 0x40;
1617	phy_control_request->function = 0x91;
1618	phy_control_request->request_length = 9;
1619	phy_control_request->allocated_response_length = 0;
1620	phy_control_request->phy_identifier = phy->number;
1621	phy_control_request->phy_operation = phy_operation;
1622	phy_control_request->programmed_min_physical_link_rate =
1623	    phy->minimum_linkrate << 4;
1624	phy_control_request->programmed_max_physical_link_rate =
1625	    phy->maximum_linkrate << 4;
1626
1627	memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1628	mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1629	mpi_request->PhysicalPort = _transport_get_port_id_by_sas_phy(phy);
1630	mpi_request->VF_ID = 0; /* TODO */
1631	mpi_request->VP_ID = 0;
1632	mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
1633	mpi_request->RequestDataLength =
1634	    cpu_to_le16(sizeof(struct phy_error_log_request));
1635	psge = &mpi_request->SGL;
1636
1637	ioc->build_sg(ioc, psge, data_out_dma,
1638			    sizeof(struct phy_control_request),
1639	    data_out_dma + sizeof(struct phy_control_request),
1640	    sizeof(struct phy_control_reply));
1641
1642	dtransportprintk(ioc,
1643			 ioc_info(ioc, "phy_control - send to sas_addr(0x%016llx), phy(%d), opcode(%d)\n",
1644				  (u64)phy->identify.sas_address,
1645				  phy->number, phy_operation));
1646	init_completion(&ioc->transport_cmds.done);
1647	ioc->put_smid_default(ioc, smid);
1648	wait_for_completion_timeout(&ioc->transport_cmds.done, 10*HZ);
1649
1650	if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
1651		ioc_err(ioc, "%s: timeout\n", __func__);
1652		_debug_dump_mf(mpi_request,
1653		    sizeof(Mpi2SmpPassthroughRequest_t)/4);
1654		if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
1655			issue_reset = 1;
1656		goto issue_host_reset;
1657	}
1658
1659	dtransportprintk(ioc, ioc_info(ioc, "phy_control - complete\n"));
1660
1661	if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
1662
1663		mpi_reply = ioc->transport_cmds.reply;
1664
1665		dtransportprintk(ioc,
1666				 ioc_info(ioc, "phy_control - reply data transfer size(%d)\n",
1667					  le16_to_cpu(mpi_reply->ResponseDataLength)));
1668
1669		if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1670		    sizeof(struct phy_control_reply))
1671			goto out;
1672
1673		phy_control_reply = data_out +
1674		    sizeof(struct phy_control_request);
1675
1676		dtransportprintk(ioc,
1677				 ioc_info(ioc, "phy_control - function_result(%d)\n",
1678					  phy_control_reply->function_result));
1679
1680		rc = 0;
1681	} else
1682		dtransportprintk(ioc,
1683				 ioc_info(ioc, "phy_control - no reply\n"));
1684
1685 issue_host_reset:
1686	if (issue_reset)
1687		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1688 out:
1689	ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
1690	if (data_out)
1691		dma_free_coherent(&ioc->pdev->dev, sz, data_out,
1692				data_out_dma);
1693
1694	mutex_unlock(&ioc->transport_cmds.mutex);
1695	return rc;
1696}
1697
1698/**
1699 * _transport_phy_reset -
1700 * @phy: The sas phy object
1701 * @hard_reset:
1702 *
1703 * Return: 0 for success, non-zero for failure.
1704 */
1705static int
1706_transport_phy_reset(struct sas_phy *phy, int hard_reset)
1707{
1708	struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
1709	Mpi2SasIoUnitControlReply_t mpi_reply;
1710	Mpi2SasIoUnitControlRequest_t mpi_request;
1711	struct hba_port *port = phy->hostdata;
1712	int port_id = port->port_id;
1713	unsigned long flags;
1714
1715	spin_lock_irqsave(&ioc->sas_node_lock, flags);
1716	if (_transport_sas_node_find_by_sas_address(ioc,
1717	    phy->identify.sas_address,
1718	    mpt3sas_get_port_by_id(ioc, port_id, 0)) == NULL) {
1719		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1720		return -EINVAL;
1721	}
1722	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1723
1724	/* handle expander phys */
1725	if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1726		return _transport_expander_phy_control(ioc, phy,
1727		    (hard_reset == 1) ? SMP_PHY_CONTROL_HARD_RESET :
1728		    SMP_PHY_CONTROL_LINK_RESET);
1729
1730	/* handle hba phys */
1731	memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
1732	mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
1733	mpi_request.Operation = hard_reset ?
1734	    MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
1735	mpi_request.PhyNum = phy->number;
1736
1737	if ((mpt3sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
1738		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1739			__FILE__, __LINE__, __func__);
1740		return -ENXIO;
1741	}
1742
1743	if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1744		ioc_info(ioc, "phy(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
1745			 phy->number, le16_to_cpu(mpi_reply.IOCStatus),
1746			 le32_to_cpu(mpi_reply.IOCLogInfo));
1747
1748	return 0;
1749}
1750
1751/**
1752 * _transport_phy_enable - enable/disable phys
1753 * @phy: The sas phy object
1754 * @enable: enable phy when true
1755 *
1756 * Only support sas_host direct attached phys.
1757 * Return: 0 for success, non-zero for failure.
1758 */
1759static int
1760_transport_phy_enable(struct sas_phy *phy, int enable)
1761{
1762	struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
1763	Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1764	Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
1765	Mpi2ConfigReply_t mpi_reply;
1766	u16 ioc_status;
1767	u16 sz;
1768	int rc = 0;
1769	unsigned long flags;
1770	int i, discovery_active;
1771	struct hba_port *port = phy->hostdata;
1772	int port_id = port->port_id;
1773
1774	spin_lock_irqsave(&ioc->sas_node_lock, flags);
1775	if (_transport_sas_node_find_by_sas_address(ioc,
1776	    phy->identify.sas_address,
1777	    mpt3sas_get_port_by_id(ioc, port_id, 0)) == NULL) {
1778		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1779		return -EINVAL;
1780	}
1781	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1782
1783	/* handle expander phys */
1784	if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1785		return _transport_expander_phy_control(ioc, phy,
1786		    (enable == 1) ? SMP_PHY_CONTROL_LINK_RESET :
1787		    SMP_PHY_CONTROL_DISABLE);
1788
1789	/* handle hba phys */
1790
1791	/* read sas_iounit page 0 */
1792	sz = struct_size(sas_iounit_pg0, PhyData, ioc->sas_hba.num_phys);
1793	sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
1794	if (!sas_iounit_pg0) {
1795		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1796			__FILE__, __LINE__, __func__);
1797		rc = -ENOMEM;
1798		goto out;
1799	}
1800	if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
1801	    sas_iounit_pg0, sz))) {
1802		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1803			__FILE__, __LINE__, __func__);
1804		rc = -ENXIO;
1805		goto out;
1806	}
1807	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1808	    MPI2_IOCSTATUS_MASK;
1809	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1810		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1811			__FILE__, __LINE__, __func__);
1812		rc = -EIO;
1813		goto out;
1814	}
1815
1816	/* unable to enable/disable phys when when discovery is active */
1817	for (i = 0, discovery_active = 0; i < ioc->sas_hba.num_phys ; i++) {
1818		if (sas_iounit_pg0->PhyData[i].PortFlags &
1819		    MPI2_SASIOUNIT0_PORTFLAGS_DISCOVERY_IN_PROGRESS) {
1820			ioc_err(ioc, "discovery is active on port = %d, phy = %d: unable to enable/disable phys, try again later!\n",
1821				sas_iounit_pg0->PhyData[i].Port, i);
1822			discovery_active = 1;
1823		}
1824	}
1825
1826	if (discovery_active) {
1827		rc = -EAGAIN;
1828		goto out;
1829	}
1830
1831	/* read sas_iounit page 1 */
1832	sz = struct_size(sas_iounit_pg1, PhyData, ioc->sas_hba.num_phys);
1833	sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1834	if (!sas_iounit_pg1) {
1835		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1836			__FILE__, __LINE__, __func__);
1837		rc = -ENOMEM;
1838		goto out;
1839	}
1840	if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1841	    sas_iounit_pg1, sz))) {
1842		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1843			__FILE__, __LINE__, __func__);
1844		rc = -ENXIO;
1845		goto out;
1846	}
1847	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1848	    MPI2_IOCSTATUS_MASK;
1849	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1850		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1851			__FILE__, __LINE__, __func__);
1852		rc = -EIO;
1853		goto out;
1854	}
1855
1856	/* copy Port/PortFlags/PhyFlags from page 0 */
1857	for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
1858		sas_iounit_pg1->PhyData[i].Port =
1859		    sas_iounit_pg0->PhyData[i].Port;
1860		sas_iounit_pg1->PhyData[i].PortFlags =
1861		    (sas_iounit_pg0->PhyData[i].PortFlags &
1862		    MPI2_SASIOUNIT0_PORTFLAGS_AUTO_PORT_CONFIG);
1863		sas_iounit_pg1->PhyData[i].PhyFlags =
1864		    (sas_iounit_pg0->PhyData[i].PhyFlags &
1865		    (MPI2_SASIOUNIT0_PHYFLAGS_ZONING_ENABLED +
1866		    MPI2_SASIOUNIT0_PHYFLAGS_PHY_DISABLED));
1867	}
1868
1869	if (enable)
1870		sas_iounit_pg1->PhyData[phy->number].PhyFlags
1871		    &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1872	else
1873		sas_iounit_pg1->PhyData[phy->number].PhyFlags
1874		    |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1875
1876	mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
1877
1878	/* link reset */
1879	if (enable)
1880		_transport_phy_reset(phy, 0);
1881
1882 out:
1883	kfree(sas_iounit_pg1);
1884	kfree(sas_iounit_pg0);
1885	return rc;
1886}
1887
1888/**
1889 * _transport_phy_speed - set phy min/max link rates
1890 * @phy: The sas phy object
1891 * @rates: rates defined in sas_phy_linkrates
1892 *
1893 * Only support sas_host direct attached phys.
1894 *
1895 * Return: 0 for success, non-zero for failure.
1896 */
1897static int
1898_transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
1899{
1900	struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
1901	Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1902	Mpi2SasPhyPage0_t phy_pg0;
1903	Mpi2ConfigReply_t mpi_reply;
1904	u16 ioc_status;
1905	u16 sz;
1906	int i;
1907	int rc = 0;
1908	unsigned long flags;
1909	struct hba_port *port = phy->hostdata;
1910	int port_id = port->port_id;
1911
1912	spin_lock_irqsave(&ioc->sas_node_lock, flags);
1913	if (_transport_sas_node_find_by_sas_address(ioc,
1914	    phy->identify.sas_address,
1915	    mpt3sas_get_port_by_id(ioc, port_id, 0)) == NULL) {
1916		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1917		return -EINVAL;
1918	}
1919	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1920
1921	if (!rates->minimum_linkrate)
1922		rates->minimum_linkrate = phy->minimum_linkrate;
1923	else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
1924		rates->minimum_linkrate = phy->minimum_linkrate_hw;
1925
1926	if (!rates->maximum_linkrate)
1927		rates->maximum_linkrate = phy->maximum_linkrate;
1928	else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
1929		rates->maximum_linkrate = phy->maximum_linkrate_hw;
1930
1931	/* handle expander phys */
1932	if (phy->identify.sas_address != ioc->sas_hba.sas_address) {
1933		phy->minimum_linkrate = rates->minimum_linkrate;
1934		phy->maximum_linkrate = rates->maximum_linkrate;
1935		return _transport_expander_phy_control(ioc, phy,
1936		    SMP_PHY_CONTROL_LINK_RESET);
1937	}
1938
1939	/* handle hba phys */
1940
1941	/* sas_iounit page 1 */
1942	sz = struct_size(sas_iounit_pg1, PhyData, ioc->sas_hba.num_phys);
1943	sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1944	if (!sas_iounit_pg1) {
1945		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1946			__FILE__, __LINE__, __func__);
1947		rc = -ENOMEM;
1948		goto out;
1949	}
1950	if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1951	    sas_iounit_pg1, sz))) {
1952		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1953			__FILE__, __LINE__, __func__);
1954		rc = -ENXIO;
1955		goto out;
1956	}
1957	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1958	    MPI2_IOCSTATUS_MASK;
1959	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1960		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1961			__FILE__, __LINE__, __func__);
1962		rc = -EIO;
1963		goto out;
1964	}
1965
1966	for (i = 0; i < ioc->sas_hba.num_phys; i++) {
1967		if (phy->number != i) {
1968			sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1969			    (ioc->sas_hba.phy[i].phy->minimum_linkrate +
1970			    (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
1971		} else {
1972			sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1973			    (rates->minimum_linkrate +
1974			    (rates->maximum_linkrate << 4));
1975		}
1976	}
1977
1978	if (mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
1979	    sz)) {
1980		ioc_err(ioc, "failure at %s:%d/%s()!\n",
1981			__FILE__, __LINE__, __func__);
1982		rc = -ENXIO;
1983		goto out;
1984	}
1985
1986	/* link reset */
1987	_transport_phy_reset(phy, 0);
1988
1989	/* read phy page 0, then update the rates in the sas transport phy */
1990	if (!mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
1991	    phy->number)) {
1992		phy->minimum_linkrate = _transport_convert_phy_link_rate(
1993		    phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
1994		phy->maximum_linkrate = _transport_convert_phy_link_rate(
1995		    phy_pg0.ProgrammedLinkRate >> 4);
1996		phy->negotiated_linkrate = _transport_convert_phy_link_rate(
1997		    phy_pg0.NegotiatedLinkRate &
1998		    MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
1999	}
2000
2001 out:
2002	kfree(sas_iounit_pg1);
2003	return rc;
2004}
2005
2006static int
2007_transport_map_smp_buffer(struct device *dev, struct bsg_buffer *buf,
2008		dma_addr_t *dma_addr, size_t *dma_len, void **p)
2009{
2010	/* Check if the request is split across multiple segments */
2011	if (buf->sg_cnt > 1) {
2012		*p = dma_alloc_coherent(dev, buf->payload_len, dma_addr,
2013				GFP_KERNEL);
2014		if (!*p)
2015			return -ENOMEM;
2016		*dma_len = buf->payload_len;
2017	} else {
2018		if (!dma_map_sg(dev, buf->sg_list, 1, DMA_BIDIRECTIONAL))
2019			return -ENOMEM;
2020		*dma_addr = sg_dma_address(buf->sg_list);
2021		*dma_len = sg_dma_len(buf->sg_list);
2022		*p = NULL;
2023	}
2024
2025	return 0;
2026}
2027
2028static void
2029_transport_unmap_smp_buffer(struct device *dev, struct bsg_buffer *buf,
2030		dma_addr_t dma_addr, void *p)
2031{
2032	if (p)
2033		dma_free_coherent(dev, buf->payload_len, p, dma_addr);
2034	else
2035		dma_unmap_sg(dev, buf->sg_list, 1, DMA_BIDIRECTIONAL);
2036}
2037
2038/**
2039 * _transport_smp_handler - transport portal for smp passthru
2040 * @job: ?
2041 * @shost: shost object
2042 * @rphy: sas transport rphy object
2043 *
2044 * This used primarily for smp_utils.
2045 * Example:
2046 *           smp_rep_general /sys/class/bsg/expander-5:0
2047 */
2048static void
2049_transport_smp_handler(struct bsg_job *job, struct Scsi_Host *shost,
2050		struct sas_rphy *rphy)
2051{
2052	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2053	Mpi2SmpPassthroughRequest_t *mpi_request;
2054	Mpi2SmpPassthroughReply_t *mpi_reply;
2055	int rc;
2056	u16 smid;
2057	void *psge;
2058	dma_addr_t dma_addr_in;
2059	dma_addr_t dma_addr_out;
2060	void *addr_in = NULL;
2061	void *addr_out = NULL;
2062	size_t dma_len_in;
2063	size_t dma_len_out;
2064	unsigned int reslen = 0;
2065
2066	if (ioc->shost_recovery || ioc->pci_error_recovery) {
2067		ioc_info(ioc, "%s: host reset in progress!\n", __func__);
2068		rc = -EFAULT;
2069		goto job_done;
2070	}
2071
2072	rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
2073	if (rc)
2074		goto job_done;
2075
2076	if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
2077		ioc_err(ioc, "%s: transport_cmds in use\n",
2078			__func__);
2079		rc = -EAGAIN;
2080		goto out;
2081	}
2082	ioc->transport_cmds.status = MPT3_CMD_PENDING;
2083
2084	rc = _transport_map_smp_buffer(&ioc->pdev->dev, &job->request_payload,
2085			&dma_addr_out, &dma_len_out, &addr_out);
2086	if (rc)
2087		goto out;
2088	if (addr_out) {
2089		sg_copy_to_buffer(job->request_payload.sg_list,
2090				job->request_payload.sg_cnt, addr_out,
2091				job->request_payload.payload_len);
2092	}
2093
2094	rc = _transport_map_smp_buffer(&ioc->pdev->dev, &job->reply_payload,
2095			&dma_addr_in, &dma_len_in, &addr_in);
2096	if (rc)
2097		goto unmap_out;
2098
2099	rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
2100	if (rc)
2101		goto unmap_in;
2102
2103	smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
2104	if (!smid) {
2105		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
2106		rc = -EAGAIN;
2107		goto unmap_in;
2108	}
2109
2110	rc = 0;
2111	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2112	ioc->transport_cmds.smid = smid;
2113
2114	memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
2115	mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
2116	mpi_request->PhysicalPort = _transport_get_port_id_by_rphy(ioc, rphy);
2117	mpi_request->SASAddress = (rphy) ?
2118	    cpu_to_le64(rphy->identify.sas_address) :
2119	    cpu_to_le64(ioc->sas_hba.sas_address);
2120	mpi_request->RequestDataLength = cpu_to_le16(dma_len_out - 4);
2121	psge = &mpi_request->SGL;
2122
2123	ioc->build_sg(ioc, psge, dma_addr_out, dma_len_out - 4, dma_addr_in,
2124			dma_len_in - 4);
2125
2126	dtransportprintk(ioc,
2127			 ioc_info(ioc, "%s: sending smp request\n", __func__));
2128
2129	init_completion(&ioc->transport_cmds.done);
2130	ioc->put_smid_default(ioc, smid);
2131	wait_for_completion_timeout(&ioc->transport_cmds.done, 10*HZ);
2132
2133	if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
2134		ioc_err(ioc, "%s: timeout\n", __func__);
2135		_debug_dump_mf(mpi_request,
2136		    sizeof(Mpi2SmpPassthroughRequest_t)/4);
2137		if (!(ioc->transport_cmds.status & MPT3_CMD_RESET)) {
2138			mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2139			rc = -ETIMEDOUT;
2140			goto unmap_in;
2141		}
2142	}
2143
2144	dtransportprintk(ioc, ioc_info(ioc, "%s - complete\n", __func__));
2145
2146	if (!(ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID)) {
2147		dtransportprintk(ioc,
2148				 ioc_info(ioc, "%s: no reply\n", __func__));
2149		rc = -ENXIO;
2150		goto unmap_in;
2151	}
2152
2153	mpi_reply = ioc->transport_cmds.reply;
2154
2155	dtransportprintk(ioc,
2156			 ioc_info(ioc, "%s: reply data transfer size(%d)\n",
2157				  __func__,
2158				  le16_to_cpu(mpi_reply->ResponseDataLength)));
2159
2160	memcpy(job->reply, mpi_reply, sizeof(*mpi_reply));
2161	job->reply_len = sizeof(*mpi_reply);
2162	reslen = le16_to_cpu(mpi_reply->ResponseDataLength);
2163
2164	if (addr_in) {
2165		sg_copy_to_buffer(job->reply_payload.sg_list,
2166				job->reply_payload.sg_cnt, addr_in,
2167				job->reply_payload.payload_len);
2168	}
2169
2170	rc = 0;
2171 unmap_in:
2172	_transport_unmap_smp_buffer(&ioc->pdev->dev, &job->reply_payload,
2173			dma_addr_in, addr_in);
2174 unmap_out:
2175	_transport_unmap_smp_buffer(&ioc->pdev->dev, &job->request_payload,
2176			dma_addr_out, addr_out);
2177 out:
2178	ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
2179	mutex_unlock(&ioc->transport_cmds.mutex);
2180job_done:
2181	bsg_job_done(job, rc, reslen);
2182}
2183
2184struct sas_function_template mpt3sas_transport_functions = {
2185	.get_linkerrors		= _transport_get_linkerrors,
2186	.get_enclosure_identifier = _transport_get_enclosure_identifier,
2187	.get_bay_identifier	= _transport_get_bay_identifier,
2188	.phy_reset		= _transport_phy_reset,
2189	.phy_enable		= _transport_phy_enable,
2190	.set_phy_speed		= _transport_phy_speed,
2191	.smp_handler		= _transport_smp_handler,
2192};
2193
2194struct scsi_transport_template *mpt3sas_transport_template;
~/ Read-only file preview.