Linux Audio

Check our new training course

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