mpt3sas_scsih.c 358.5 KB
Newer Older
1 2 3 4
/*
 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
 *
 * This code is based on drivers/scsi/mpt3sas/mpt3sas_scsih.c
5
 * Copyright (C) 2012-2014  LSI Corporation
6 7
 * Copyright (C) 2013-2014 Avago Technologies
 *  (mailto: MPT-FusionLinux.pdl@avagotech.com)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * NO WARRANTY
 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
 * solely responsible for determining the appropriateness of using and
 * distributing the Program and assumes all risks associated with its
 * exercise of rights under this Agreement, including but not limited to
 * the risks and costs of program errors, damage to or loss of data,
 * programs or equipment, and unavailability or interruption of operations.

 * DISCLAIMER OF LIABILITY
 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/blkdev.h>
#include <linux/sched.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
#include <linux/pci.h>
#include <linux/interrupt.h>
#include <linux/aer.h>
#include <linux/raid_class.h>
57
#include <asm/unaligned.h>
58 59 60 61

#include "mpt3sas_base.h"

#define RAID_CHANNEL 1
62 63 64

#define PCIE_CHANNEL 2

65 66 67 68 69 70 71 72 73
/* forward proto's */
static void _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
	struct _sas_node *sas_expander);
static void _firmware_event_work(struct work_struct *work);

static void _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
	struct _sas_device *sas_device);
static int _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
	u8 retry_count, u8 is_pd);
74
static int _scsih_pcie_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle);
75 76
static void _scsih_pcie_device_remove_from_sml(struct MPT3SAS_ADAPTER *ioc,
	struct _pcie_device *pcie_device);
77 78
static void
_scsih_pcie_check_device(struct MPT3SAS_ADAPTER *ioc, u16 handle);
79 80 81 82
static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);

/* global parameters */
LIST_HEAD(mpt3sas_ioc_list);
83 84
/* global ioc lock for list operations */
DEFINE_SPINLOCK(gioc_lock);
85

86 87 88 89 90 91
MODULE_AUTHOR(MPT3SAS_AUTHOR);
MODULE_DESCRIPTION(MPT3SAS_DESCRIPTION);
MODULE_LICENSE("GPL");
MODULE_VERSION(MPT3SAS_DRIVER_VERSION);
MODULE_ALIAS("mpt2sas");

92 93 94 95 96 97 98 99 100
/* local parameters */
static u8 scsi_io_cb_idx = -1;
static u8 tm_cb_idx = -1;
static u8 ctl_cb_idx = -1;
static u8 base_cb_idx = -1;
static u8 port_enable_cb_idx = -1;
static u8 transport_cb_idx = -1;
static u8 scsih_cb_idx = -1;
static u8 config_cb_idx = -1;
101 102
static int mpt2_ids;
static int mpt3_ids;
103 104 105 106 107 108 109 110 111 112 113 114

static u8 tm_tr_cb_idx = -1 ;
static u8 tm_tr_volume_cb_idx = -1 ;
static u8 tm_sas_control_cb_idx = -1;

/* command line options */
static u32 logging_level;
MODULE_PARM_DESC(logging_level,
	" bits for enabling additional logging info (default=0)");


static ushort max_sectors = 0xFFFF;
115
module_param(max_sectors, ushort, 0444);
116 117 118 119
MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767  default=32767");


static int missing_delay[2] = {-1, -1};
120
module_param_array(missing_delay, int, NULL, 0444);
121 122 123 124
MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");

/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
#define MPT3SAS_MAX_LUN (16895)
125
static u64 max_lun = MPT3SAS_MAX_LUN;
126
module_param(max_lun, ullong, 0444);
127 128
MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");

129
static ushort hbas_to_enumerate;
130
module_param(hbas_to_enumerate, ushort, 0444);
131 132 133 134
MODULE_PARM_DESC(hbas_to_enumerate,
		" 0 - enumerates both SAS 2.0 & SAS 3.0 generation HBAs\n \
		  1 - enumerates only SAS 2.0 generation HBAs\n \
		  2 - enumerates only SAS 3.0 generation HBAs (default=0)");
135 136 137 138 139 140 141 142 143

/* diag_buffer_enable is bitwise
 * bit 0 set = TRACE
 * bit 1 set = SNAPSHOT
 * bit 2 set = EXTENDED
 *
 * Either bit can be set, or both
 */
static int diag_buffer_enable = -1;
144
module_param(diag_buffer_enable, int, 0444);
145 146 147
MODULE_PARM_DESC(diag_buffer_enable,
	" post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
static int disable_discovery = -1;
148
module_param(disable_discovery, int, 0444);
149 150 151 152 153
MODULE_PARM_DESC(disable_discovery, " disable discovery ");


/* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
static int prot_mask = -1;
154
module_param(prot_mask, int, 0444);
155 156
MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");

157 158 159 160
static bool enable_sdev_max_qd;
module_param(enable_sdev_max_qd, bool, 0444);
MODULE_PARM_DESC(enable_sdev_max_qd,
	"Enable sdev max qd as can_queue, def=disabled(0)");
161 162

/* raid transport support */
163 164
static struct raid_template *mpt3sas_raid_template;
static struct raid_template *mpt2sas_raid_template;
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179


/**
 * struct sense_info - common structure for obtaining sense keys
 * @skey: sense key
 * @asc: additional sense code
 * @ascq: additional sense code qualifier
 */
struct sense_info {
	u8 skey;
	u8 asc;
	u8 ascq;
};

#define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
180
#define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
181 182 183 184 185 186 187 188 189 190 191 192
#define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
#define MPT3SAS_ABRT_TASK_SET (0xFFFE)
#define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
/**
 * struct fw_event_work - firmware event struct
 * @list: link list framework
 * @work: work object (ioc->fault_reset_work_q)
 * @ioc: per adapter object
 * @device_handle: device handle
 * @VF_ID: virtual function id
 * @VP_ID: virtual port id
 * @ignore: flag meaning this event has been marked to ignore
193 194
 * @event: firmware event MPI2_EVENT_XXX defined in mpi2_ioc.h
 * @refcount: kref for this event
195 196 197 198 199 200 201 202 203 204 205 206 207 208
 * @event_data: reply event data payload follows
 *
 * This object stored on ioc->fw_event_list.
 */
struct fw_event_work {
	struct list_head	list;
	struct work_struct	work;

	struct MPT3SAS_ADAPTER *ioc;
	u16			device_handle;
	u8			VF_ID;
	u8			VP_ID;
	u8			ignore;
	u16			event;
209
	struct kref		refcount;
210
	char			event_data[] __aligned(4);
211 212
};

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
static void fw_event_work_free(struct kref *r)
{
	kfree(container_of(r, struct fw_event_work, refcount));
}

static void fw_event_work_get(struct fw_event_work *fw_work)
{
	kref_get(&fw_work->refcount);
}

static void fw_event_work_put(struct fw_event_work *fw_work)
{
	kref_put(&fw_work->refcount, fw_event_work_free);
}

static struct fw_event_work *alloc_fw_event_work(int len)
{
	struct fw_event_work *fw_event;

	fw_event = kzalloc(sizeof(*fw_event) + len, GFP_ATOMIC);
	if (!fw_event)
		return NULL;

	kref_init(&fw_event->refcount);
	return fw_event;
}

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
/**
 * struct _scsi_io_transfer - scsi io transfer
 * @handle: sas device handle (assigned by firmware)
 * @is_raid: flag set for hidden raid components
 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
 * @data_length: data transfer length
 * @data_dma: dma pointer to data
 * @sense: sense data
 * @lun: lun number
 * @cdb_length: cdb length
 * @cdb: cdb contents
 * @timeout: timeout for this command
 * @VF_ID: virtual function id
 * @VP_ID: virtual port id
 * @valid_reply: flag set for reply message
 * @sense_length: sense length
 * @ioc_status: ioc status
 * @scsi_state: scsi state
 * @scsi_status: scsi staus
 * @log_info: log information
 * @transfer_length: data length transfer when there is a reply message
 *
 * Used for sending internal scsi commands to devices within this module.
 * Refer to _scsi_send_scsi_io().
 */
struct _scsi_io_transfer {
	u16	handle;
	u8	is_raid;
	enum dma_data_direction dir;
	u32	data_length;
	dma_addr_t data_dma;
	u8	sense[SCSI_SENSE_BUFFERSIZE];
	u32	lun;
	u8	cdb_length;
	u8	cdb[32];
	u8	timeout;
	u8	VF_ID;
	u8	VP_ID;
	u8	valid_reply;
  /* the following bits are only valid when 'valid_reply = 1' */
	u32	sense_length;
	u16	ioc_status;
	u8	scsi_state;
	u8	scsi_status;
	u32	log_info;
	u32	transfer_length;
};

/**
 * _scsih_set_debug_level - global setting of ioc->logging_level.
290 291
 * @val: ?
 * @kp: ?
292 293 294 295
 *
 * Note: The logging levels are defined in mpt3sas_debug.h.
 */
static int
296
_scsih_set_debug_level(const char *val, const struct kernel_param *kp)
297 298 299 300 301 302 303 304
{
	int ret = param_set_int(val, kp);
	struct MPT3SAS_ADAPTER *ioc;

	if (ret)
		return ret;

	pr_info("setting logging_level(0x%08x)\n", logging_level);
305
	spin_lock(&gioc_lock);
306 307
	list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
		ioc->logging_level = logging_level;
308
	spin_unlock(&gioc_lock);
309 310 311 312 313 314 315 316 317 318
	return 0;
}
module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
	&logging_level, 0644);

/**
 * _scsih_srch_boot_sas_address - search based on sas_address
 * @sas_address: sas address
 * @boot_device: boot device object from bios page 2
 *
319
 * Return: 1 when there's a match, 0 means no match.
320 321 322 323 324 325 326 327 328 329 330 331 332
 */
static inline int
_scsih_srch_boot_sas_address(u64 sas_address,
	Mpi2BootDeviceSasWwid_t *boot_device)
{
	return (sas_address == le64_to_cpu(boot_device->SASAddress)) ?  1 : 0;
}

/**
 * _scsih_srch_boot_device_name - search based on device name
 * @device_name: device name specified in INDENTIFY fram
 * @boot_device: boot device object from bios page 2
 *
333
 * Return: 1 when there's a match, 0 means no match.
334 335 336 337 338 339 340 341 342 343 344 345 346 347
 */
static inline int
_scsih_srch_boot_device_name(u64 device_name,
	Mpi2BootDeviceDeviceName_t *boot_device)
{
	return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
}

/**
 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
 * @enclosure_logical_id: enclosure logical id
 * @slot_number: slot number
 * @boot_device: boot device object from bios page 2
 *
348
 * Return: 1 when there's a match, 0 means no match.
349 350 351 352 353 354 355 356 357 358
 */
static inline int
_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
	Mpi2BootDeviceEnclosureSlot_t *boot_device)
{
	return (enclosure_logical_id == le64_to_cpu(boot_device->
	    EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
	    SlotNumber)) ? 1 : 0;
}

359 360 361 362 363
/**
 * mpt3sas_get_port_by_id - get hba port entry corresponding to provided
 *			  port number from port list
 * @ioc: per adapter object
 * @port_id: port number
364 365
 * @bypass_dirty_port_flag: when set look the matching hba port entry even
 *			if hba port entry is marked as dirty.
366 367 368 369 370
 *
 * Search for hba port entry corresponding to provided port number,
 * if available return port object otherwise return NULL.
 */
struct hba_port *
371 372
mpt3sas_get_port_by_id(struct MPT3SAS_ADAPTER *ioc,
	u8 port_id, u8 bypass_dirty_port_flag)
373 374 375 376 377
{
	struct hba_port *port, *port_next;

	list_for_each_entry_safe(port, port_next,
	    &ioc->port_table_list, list) {
378 379 380
		if (port->port_id != port_id)
			continue;
		if (bypass_dirty_port_flag)
381
			return port;
382 383 384
		if (port->flags & HBA_PORT_FLAG_DIRTY_PORT)
			continue;
		return port;
385 386 387 388 389
	}

	return NULL;
}

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
/**
 * mpt3sas_get_vphy_by_phy - get virtual_phy object corresponding to phy number
 * @ioc: per adapter object
 * @port: hba_port object
 * @phy: phy number
 *
 * Return virtual_phy object corresponding to phy number.
 */
struct virtual_phy *
mpt3sas_get_vphy_by_phy(struct MPT3SAS_ADAPTER *ioc,
	struct hba_port *port, u32 phy)
{
	struct virtual_phy *vphy, *vphy_next;

	if (!port->vphys_mask)
		return NULL;

	list_for_each_entry_safe(vphy, vphy_next, &port->vphys_list, list) {
		if (vphy->phy_mask & (1 << phy))
			return vphy;
	}
	return NULL;
}

414 415 416 417 418
/**
 * _scsih_is_boot_device - search for matching boot device.
 * @sas_address: sas address
 * @device_name: device name specified in INDENTIFY fram
 * @enclosure_logical_id: enclosure logical id
419
 * @slot: slot number
420 421 422
 * @form: specifies boot device form
 * @boot_device: boot device object from bios page 2
 *
423
 * Return: 1 when there's a match, 0 means no match.
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
 */
static int
_scsih_is_boot_device(u64 sas_address, u64 device_name,
	u64 enclosure_logical_id, u16 slot, u8 form,
	Mpi2BiosPage2BootDevice_t *boot_device)
{
	int rc = 0;

	switch (form) {
	case MPI2_BIOSPAGE2_FORM_SAS_WWID:
		if (!sas_address)
			break;
		rc = _scsih_srch_boot_sas_address(
		    sas_address, &boot_device->SasWwid);
		break;
	case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
		if (!enclosure_logical_id)
			break;
		rc = _scsih_srch_boot_encl_slot(
		    enclosure_logical_id,
		    slot, &boot_device->EnclosureSlot);
		break;
	case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
		if (!device_name)
			break;
		rc = _scsih_srch_boot_device_name(
		    device_name, &boot_device->DeviceName);
		break;
	case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
		break;
	}

	return rc;
}

/**
 * _scsih_get_sas_address - set the sas_address for given device handle
461
 * @ioc: ?
462 463 464
 * @handle: device handle
 * @sas_address: sas address
 *
465
 * Return: 0 success, non-zero when failure
466 467 468 469 470 471 472 473 474 475 476 477 478
 */
static int
_scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
	u64 *sas_address)
{
	Mpi2SasDevicePage0_t sas_device_pg0;
	Mpi2ConfigReply_t mpi_reply;
	u32 ioc_status;

	*sas_address = 0;

	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
479 480
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
481 482 483 484 485
		return -ENXIO;
	}

	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
486 487 488 489 490 491 492 493 494
		/* For HBA, vSES doesn't return HBA SAS address. Instead return
		 * vSES's sas address.
		 */
		if ((handle <= ioc->sas_hba.num_phys) &&
		   (!(le32_to_cpu(sas_device_pg0.DeviceInfo) &
		   MPI2_SAS_DEVICE_INFO_SEP)))
			*sas_address = ioc->sas_hba.sas_address;
		else
			*sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
495 496 497
		return 0;
	}

498
	/* we hit this because the given parent handle doesn't exist */
499 500 501 502
	if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
		return -ENXIO;

	/* else error case */
503 504
	ioc_err(ioc, "handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
		handle, ioc_status, __FILE__, __LINE__, __func__);
505 506 507 508 509 510
	return -EIO;
}

/**
 * _scsih_determine_boot_device - determine boot device.
 * @ioc: per adapter object
511 512
 * @device: sas_device or pcie_device object
 * @channel: SAS or PCIe channel
513 514 515 516 517
 *
 * Determines whether this device should be first reported device to
 * to scsi-ml or sas transport, this purpose is for persistent boot device.
 * There are primary, alternate, and current entries in bios page 2. The order
 * priority is primary, alternate, then current.  This routine saves
518
 * the corresponding device object.
519 520 521
 * The saved data to be used later in _scsih_probe_boot_devices().
 */
static void
522 523
_scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc, void *device,
	u32 channel)
524 525
{
	struct _sas_device *sas_device;
526
	struct _pcie_device *pcie_device;
527 528 529 530 531 532 533 534 535 536 537 538 539 540
	struct _raid_device *raid_device;
	u64 sas_address;
	u64 device_name;
	u64 enclosure_logical_id;
	u16 slot;

	 /* only process this function when driver loads */
	if (!ioc->is_driver_loading)
		return;

	 /* no Bios, return immediately */
	if (!ioc->bios_pg3.BiosVersion)
		return;

541
	if (channel == RAID_CHANNEL) {
542 543 544 545 546
		raid_device = device;
		sas_address = raid_device->wwid;
		device_name = 0;
		enclosure_logical_id = 0;
		slot = 0;
547 548 549 550 551 552 553 554 555 556 557 558
	} else if (channel == PCIE_CHANNEL) {
		pcie_device = device;
		sas_address = pcie_device->wwid;
		device_name = 0;
		enclosure_logical_id = 0;
		slot = 0;
	} else {
		sas_device = device;
		sas_address = sas_device->sas_address;
		device_name = sas_device->device_name;
		enclosure_logical_id = sas_device->enclosure_logical_id;
		slot = sas_device->slot;
559 560 561 562 563 564 565 566
	}

	if (!ioc->req_boot_device.device) {
		if (_scsih_is_boot_device(sas_address, device_name,
		    enclosure_logical_id, slot,
		    (ioc->bios_pg2.ReqBootDeviceForm &
		    MPI2_BIOSPAGE2_FORM_MASK),
		    &ioc->bios_pg2.RequestedBootDevice)) {
567 568 569
			dinitprintk(ioc,
				    ioc_info(ioc, "%s: req_boot_device(0x%016llx)\n",
					     __func__, (u64)sas_address));
570
			ioc->req_boot_device.device = device;
571
			ioc->req_boot_device.channel = channel;
572 573 574 575 576 577 578 579 580
		}
	}

	if (!ioc->req_alt_boot_device.device) {
		if (_scsih_is_boot_device(sas_address, device_name,
		    enclosure_logical_id, slot,
		    (ioc->bios_pg2.ReqAltBootDeviceForm &
		    MPI2_BIOSPAGE2_FORM_MASK),
		    &ioc->bios_pg2.RequestedAltBootDevice)) {
581 582 583
			dinitprintk(ioc,
				    ioc_info(ioc, "%s: req_alt_boot_device(0x%016llx)\n",
					     __func__, (u64)sas_address));
584
			ioc->req_alt_boot_device.device = device;
585
			ioc->req_alt_boot_device.channel = channel;
586 587 588 589 590 591 592 593 594
		}
	}

	if (!ioc->current_boot_device.device) {
		if (_scsih_is_boot_device(sas_address, device_name,
		    enclosure_logical_id, slot,
		    (ioc->bios_pg2.CurrentBootDeviceForm &
		    MPI2_BIOSPAGE2_FORM_MASK),
		    &ioc->bios_pg2.CurrentBootDevice)) {
595 596 597
			dinitprintk(ioc,
				    ioc_info(ioc, "%s: current_boot_device(0x%016llx)\n",
					     __func__, (u64)sas_address));
598
			ioc->current_boot_device.device = device;
599
			ioc->current_boot_device.channel = channel;
600 601 602 603
		}
	}
}

604 605 606 607 608 609 610 611
static struct _sas_device *
__mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
		struct MPT3SAS_TARGET *tgt_priv)
{
	struct _sas_device *ret;

	assert_spin_locked(&ioc->sas_device_lock);

612
	ret = tgt_priv->sas_dev;
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
	if (ret)
		sas_device_get(ret);

	return ret;
}

static struct _sas_device *
mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
		struct MPT3SAS_TARGET *tgt_priv)
{
	struct _sas_device *ret;
	unsigned long flags;

	spin_lock_irqsave(&ioc->sas_device_lock, flags);
	ret = __mpt3sas_get_sdev_from_target(ioc, tgt_priv);
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);

	return ret;
}

633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
static struct _pcie_device *
__mpt3sas_get_pdev_from_target(struct MPT3SAS_ADAPTER *ioc,
	struct MPT3SAS_TARGET *tgt_priv)
{
	struct _pcie_device *ret;

	assert_spin_locked(&ioc->pcie_device_lock);

	ret = tgt_priv->pcie_dev;
	if (ret)
		pcie_device_get(ret);

	return ret;
}

/**
 * mpt3sas_get_pdev_from_target - pcie device search
 * @ioc: per adapter object
 * @tgt_priv: starget private object
 *
 * Context: This function will acquire ioc->pcie_device_lock and will release
 * before returning the pcie_device object.
 *
 * This searches for pcie_device from target, then return pcie_device object.
 */
658
static struct _pcie_device *
659 660 661 662 663 664 665 666 667 668 669 670
mpt3sas_get_pdev_from_target(struct MPT3SAS_ADAPTER *ioc,
	struct MPT3SAS_TARGET *tgt_priv)
{
	struct _pcie_device *ret;
	unsigned long flags;

	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
	ret = __mpt3sas_get_pdev_from_target(ioc, tgt_priv);
	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);

	return ret;
}
671

672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709

/**
 * __mpt3sas_get_sdev_by_rphy - sas device search
 * @ioc: per adapter object
 * @rphy: sas_rphy pointer
 *
 * Context: This function will acquire ioc->sas_device_lock and will release
 * before returning the sas_device object.
 *
 * This searches for sas_device from rphy object
 * then return sas_device object.
 */
struct _sas_device *
__mpt3sas_get_sdev_by_rphy(struct MPT3SAS_ADAPTER *ioc,
	struct sas_rphy *rphy)
{
	struct _sas_device *sas_device;

	assert_spin_locked(&ioc->sas_device_lock);

	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
		if (sas_device->rphy != rphy)
			continue;
		sas_device_get(sas_device);
		return sas_device;
	}

	sas_device = NULL;
	list_for_each_entry(sas_device, &ioc->sas_device_init_list, list) {
		if (sas_device->rphy != rphy)
			continue;
		sas_device_get(sas_device);
		return sas_device;
	}

	return NULL;
}

710 711 712 713 714 715 716 717 718
/**
 * mpt3sas_get_sdev_by_addr - get _sas_device object corresponding to provided
 *				sas address from sas_device_list list
 * @ioc: per adapter object
 * @port: port number
 *
 * Search for _sas_device object corresponding to provided sas address,
 * if available return _sas_device object address otherwise return NULL.
 */
719 720
struct _sas_device *
__mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
721
	u64 sas_address, struct hba_port *port)
722 723 724
{
	struct _sas_device *sas_device;

725 726 727
	if (!port)
		return NULL;

728 729
	assert_spin_locked(&ioc->sas_device_lock);

730 731 732 733 734 735 736 737
	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
		if (sas_device->sas_address != sas_address)
			continue;
		if (sas_device->port != port)
			continue;
		sas_device_get(sas_device);
		return sas_device;
	}
738

739 740 741 742 743 744 745 746
	list_for_each_entry(sas_device, &ioc->sas_device_init_list, list) {
		if (sas_device->sas_address != sas_address)
			continue;
		if (sas_device->port != port)
			continue;
		sas_device_get(sas_device);
		return sas_device;
	}
747 748 749 750

	return NULL;
}

751
/**
752
 * mpt3sas_get_sdev_by_addr - sas device search
753 754
 * @ioc: per adapter object
 * @sas_address: sas address
755
 * @port: hba port entry
756 757
 * Context: Calling function should acquire ioc->sas_device_lock
 *
758 759
 * This searches for sas_device based on sas_address & port number,
 * then return sas_device object.
760 761
 */
struct _sas_device *
762
mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
763
	u64 sas_address, struct hba_port *port)
764 765
{
	struct _sas_device *sas_device;
766 767 768 769
	unsigned long flags;

	spin_lock_irqsave(&ioc->sas_device_lock, flags);
	sas_device = __mpt3sas_get_sdev_by_addr(ioc,
770
	    sas_address, port);
771 772 773 774 775 776 777 778 779 780 781
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);

	return sas_device;
}

static struct _sas_device *
__mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	struct _sas_device *sas_device;

	assert_spin_locked(&ioc->sas_device_lock);
782 783

	list_for_each_entry(sas_device, &ioc->sas_device_list, list)
784 785
		if (sas_device->handle == handle)
			goto found_device;
786 787

	list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
788 789
		if (sas_device->handle == handle)
			goto found_device;
790 791

	return NULL;
792 793 794 795

found_device:
	sas_device_get(sas_device);
	return sas_device;
796 797 798
}

/**
799
 * mpt3sas_get_sdev_by_handle - sas device search
800 801 802 803 804 805 806
 * @ioc: per adapter object
 * @handle: sas device handle (assigned by firmware)
 * Context: Calling function should acquire ioc->sas_device_lock
 *
 * This searches for sas_device based on sas_address, then return sas_device
 * object.
 */
807
struct _sas_device *
808
mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
809 810
{
	struct _sas_device *sas_device;
811
	unsigned long flags;
812

813 814 815
	spin_lock_irqsave(&ioc->sas_device_lock, flags);
	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
816

817
	return sas_device;
818 819
}

820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
/**
 * _scsih_display_enclosure_chassis_info - display device location info
 * @ioc: per adapter object
 * @sas_device: per sas device object
 * @sdev: scsi device struct
 * @starget: scsi target struct
 */
static void
_scsih_display_enclosure_chassis_info(struct MPT3SAS_ADAPTER *ioc,
	struct _sas_device *sas_device, struct scsi_device *sdev,
	struct scsi_target *starget)
{
	if (sdev) {
		if (sas_device->enclosure_handle != 0)
			sdev_printk(KERN_INFO, sdev,
			    "enclosure logical id (0x%016llx), slot(%d) \n",
			    (unsigned long long)
			    sas_device->enclosure_logical_id,
			    sas_device->slot);
		if (sas_device->connector_name[0] != '\0')
			sdev_printk(KERN_INFO, sdev,
			    "enclosure level(0x%04x), connector name( %s)\n",
			    sas_device->enclosure_level,
			    sas_device->connector_name);
		if (sas_device->is_chassis_slot_valid)
			sdev_printk(KERN_INFO, sdev, "chassis slot(0x%04x)\n",
			    sas_device->chassis_slot);
	} else if (starget) {
		if (sas_device->enclosure_handle != 0)
			starget_printk(KERN_INFO, starget,
			    "enclosure logical id(0x%016llx), slot(%d) \n",
			    (unsigned long long)
			    sas_device->enclosure_logical_id,
			    sas_device->slot);
		if (sas_device->connector_name[0] != '\0')
			starget_printk(KERN_INFO, starget,
			    "enclosure level(0x%04x), connector name( %s)\n",
			    sas_device->enclosure_level,
			    sas_device->connector_name);
		if (sas_device->is_chassis_slot_valid)
			starget_printk(KERN_INFO, starget,
			    "chassis slot(0x%04x)\n",
			    sas_device->chassis_slot);
	} else {
		if (sas_device->enclosure_handle != 0)
865 866 867
			ioc_info(ioc, "enclosure logical id(0x%016llx), slot(%d)\n",
				 (u64)sas_device->enclosure_logical_id,
				 sas_device->slot);
868
		if (sas_device->connector_name[0] != '\0')
869 870 871
			ioc_info(ioc, "enclosure level(0x%04x), connector name( %s)\n",
				 sas_device->enclosure_level,
				 sas_device->connector_name);
872
		if (sas_device->is_chassis_slot_valid)
873 874
			ioc_info(ioc, "chassis slot(0x%04x)\n",
				 sas_device->chassis_slot);
875 876 877
	}
}

878 879 880 881 882 883
/**
 * _scsih_sas_device_remove - remove sas_device from list.
 * @ioc: per adapter object
 * @sas_device: the sas_device object
 * Context: This function will acquire ioc->sas_device_lock.
 *
884
 * If sas_device is on the list, remove it and decrement its reference count.
885 886 887 888 889 890 891 892 893
 */
static void
_scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
	struct _sas_device *sas_device)
{
	unsigned long flags;

	if (!sas_device)
		return;
894 895
	ioc_info(ioc, "removing handle(0x%04x), sas_addr(0x%016llx)\n",
		 sas_device->handle, (u64)sas_device->sas_address);
896

897
	_scsih_display_enclosure_chassis_info(ioc, sas_device, NULL, NULL);
898

899 900 901 902
	/*
	 * The lock serializes access to the list, but we still need to verify
	 * that nobody removed the entry while we were waiting on the lock.
	 */
903
	spin_lock_irqsave(&ioc->sas_device_lock, flags);
904 905 906 907
	if (!list_empty(&sas_device->list)) {
		list_del_init(&sas_device->list);
		sas_device_put(sas_device);
	}
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
}

/**
 * _scsih_device_remove_by_handle - removing device object by handle
 * @ioc: per adapter object
 * @handle: device handle
 */
static void
_scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	struct _sas_device *sas_device;
	unsigned long flags;

	if (ioc->shost_recovery)
		return;

	spin_lock_irqsave(&ioc->sas_device_lock, flags);
926 927 928 929 930
	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
	if (sas_device) {
		list_del_init(&sas_device->list);
		sas_device_put(sas_device);
	}
931
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
932
	if (sas_device) {
933
		_scsih_remove_device(ioc, sas_device);
934 935
		sas_device_put(sas_device);
	}
936 937 938
}

/**
939 940
 * mpt3sas_device_remove_by_sas_address - removing device object by
 *					sas address & port number
941 942
 * @ioc: per adapter object
 * @sas_address: device sas_address
943 944 945
 * @port: hba port entry
 *
 * Return nothing.
946 947 948
 */
void
mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
949
	u64 sas_address, struct hba_port *port)
950 951 952 953 954 955 956 957
{
	struct _sas_device *sas_device;
	unsigned long flags;

	if (ioc->shost_recovery)
		return;

	spin_lock_irqsave(&ioc->sas_device_lock, flags);
958
	sas_device = __mpt3sas_get_sdev_by_addr(ioc, sas_address, port);
959 960 961 962
	if (sas_device) {
		list_del_init(&sas_device->list);
		sas_device_put(sas_device);
	}
963
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
964
	if (sas_device) {
965
		_scsih_remove_device(ioc, sas_device);
966 967
		sas_device_put(sas_device);
	}
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
}

/**
 * _scsih_sas_device_add - insert sas_device to the list.
 * @ioc: per adapter object
 * @sas_device: the sas_device object
 * Context: This function will acquire ioc->sas_device_lock.
 *
 * Adding new object to the ioc->sas_device_list.
 */
static void
_scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
	struct _sas_device *sas_device)
{
	unsigned long flags;

984 985 986 987
	dewtprintk(ioc,
		   ioc_info(ioc, "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
			    __func__, sas_device->handle,
			    (u64)sas_device->sas_address));
988

989 990
	dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
	    NULL, NULL));
991

992
	spin_lock_irqsave(&ioc->sas_device_lock, flags);
993
	sas_device_get(sas_device);
994 995 996
	list_add_tail(&sas_device->list, &ioc->sas_device_list);
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);

997 998 999 1000 1001
	if (ioc->hide_drives) {
		clear_bit(sas_device->handle, ioc->pend_os_device_add);
		return;
	}

1002
	if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
1003
	     sas_device->sas_address_parent, sas_device->port)) {
1004 1005 1006 1007 1008 1009 1010
		_scsih_sas_device_remove(ioc, sas_device);
	} else if (!sas_device->starget) {
		/*
		 * When asyn scanning is enabled, its not possible to remove
		 * devices while scanning is turned on due to an oops in
		 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
		 */
1011
		if (!ioc->is_driver_loading) {
1012 1013
			mpt3sas_transport_port_remove(ioc,
			    sas_device->sas_address,
1014 1015
			    sas_device->sas_address_parent,
			    sas_device->port);
1016 1017
			_scsih_sas_device_remove(ioc, sas_device);
		}
1018 1019
	} else
		clear_bit(sas_device->handle, ioc->pend_os_device_add);
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
}

/**
 * _scsih_sas_device_init_add - insert sas_device to the list.
 * @ioc: per adapter object
 * @sas_device: the sas_device object
 * Context: This function will acquire ioc->sas_device_lock.
 *
 * Adding new object at driver load time to the ioc->sas_device_init_list.
 */
static void
_scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
	struct _sas_device *sas_device)
{
	unsigned long flags;

1036 1037 1038 1039
	dewtprintk(ioc,
		   ioc_info(ioc, "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
			    __func__, sas_device->handle,
			    (u64)sas_device->sas_address));
1040

1041 1042
	dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
	    NULL, NULL));
1043

1044
	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1045
	sas_device_get(sas_device);
1046 1047 1048 1049 1050
	list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
	_scsih_determine_boot_device(ioc, sas_device, 0);
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
}

1051

1052
static struct _pcie_device *
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
__mpt3sas_get_pdev_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
{
	struct _pcie_device *pcie_device;

	assert_spin_locked(&ioc->pcie_device_lock);

	list_for_each_entry(pcie_device, &ioc->pcie_device_list, list)
		if (pcie_device->wwid == wwid)
			goto found_device;

	list_for_each_entry(pcie_device, &ioc->pcie_device_init_list, list)
		if (pcie_device->wwid == wwid)
			goto found_device;

	return NULL;

found_device:
	pcie_device_get(pcie_device);
	return pcie_device;
}


/**
 * mpt3sas_get_pdev_by_wwid - pcie device search
 * @ioc: per adapter object
 * @wwid: wwid
 *
 * Context: This function will acquire ioc->pcie_device_lock and will release
 * before returning the pcie_device object.
 *
 * This searches for pcie_device based on wwid, then return pcie_device object.
 */
1085
static struct _pcie_device *
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
mpt3sas_get_pdev_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
{
	struct _pcie_device *pcie_device;
	unsigned long flags;

	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
	pcie_device = __mpt3sas_get_pdev_by_wwid(ioc, wwid);
	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);

	return pcie_device;
}


1099
static struct _pcie_device *
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
__mpt3sas_get_pdev_by_idchannel(struct MPT3SAS_ADAPTER *ioc, int id,
	int channel)
{
	struct _pcie_device *pcie_device;

	assert_spin_locked(&ioc->pcie_device_lock);

	list_for_each_entry(pcie_device, &ioc->pcie_device_list, list)
		if (pcie_device->id == id && pcie_device->channel == channel)
			goto found_device;

	list_for_each_entry(pcie_device, &ioc->pcie_device_init_list, list)
		if (pcie_device->id == id && pcie_device->channel == channel)
			goto found_device;

	return NULL;

found_device:
	pcie_device_get(pcie_device);
	return pcie_device;
}

1122
static struct _pcie_device *
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
__mpt3sas_get_pdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	struct _pcie_device *pcie_device;

	assert_spin_locked(&ioc->pcie_device_lock);

	list_for_each_entry(pcie_device, &ioc->pcie_device_list, list)
		if (pcie_device->handle == handle)
			goto found_device;

	list_for_each_entry(pcie_device, &ioc->pcie_device_init_list, list)
		if (pcie_device->handle == handle)
			goto found_device;

	return NULL;

found_device:
	pcie_device_get(pcie_device);
	return pcie_device;
}


/**
 * mpt3sas_get_pdev_by_handle - pcie device search
 * @ioc: per adapter object
 * @handle: Firmware device handle
 *
 * Context: This function will acquire ioc->pcie_device_lock and will release
 * before returning the pcie_device object.
 *
 * This searches for pcie_device based on handle, then return pcie_device
 * object.
 */
struct _pcie_device *
mpt3sas_get_pdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	struct _pcie_device *pcie_device;
	unsigned long flags;

	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
	pcie_device = __mpt3sas_get_pdev_by_handle(ioc, handle);
	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);

	return pcie_device;
}

1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
/**
 * _scsih_set_nvme_max_shutdown_latency - Update max_shutdown_latency.
 * @ioc: per adapter object
 * Context: This function will acquire ioc->pcie_device_lock
 *
 * Update ioc->max_shutdown_latency to that NVMe drives RTD3 Entry Latency
 * which has reported maximum among all available NVMe drives.
 * Minimum max_shutdown_latency will be six seconds.
 */
static void
_scsih_set_nvme_max_shutdown_latency(struct MPT3SAS_ADAPTER *ioc)
{
	struct _pcie_device *pcie_device;
	unsigned long flags;
	u16 shutdown_latency = IO_UNIT_CONTROL_SHUTDOWN_TIMEOUT;

	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
	list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
		if (pcie_device->shutdown_latency) {
			if (shutdown_latency < pcie_device->shutdown_latency)
				shutdown_latency =
					pcie_device->shutdown_latency;
		}
	}
	ioc->max_shutdown_latency = shutdown_latency;
	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
}

1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
/**
 * _scsih_pcie_device_remove - remove pcie_device from list.
 * @ioc: per adapter object
 * @pcie_device: the pcie_device object
 * Context: This function will acquire ioc->pcie_device_lock.
 *
 * If pcie_device is on the list, remove it and decrement its reference count.
 */
static void
_scsih_pcie_device_remove(struct MPT3SAS_ADAPTER *ioc,
	struct _pcie_device *pcie_device)
{
	unsigned long flags;
	int was_on_pcie_device_list = 0;
1211
	u8 update_latency = 0;
1212 1213 1214

	if (!pcie_device)
		return;
1215 1216
	ioc_info(ioc, "removing handle(0x%04x), wwid(0x%016llx)\n",
		 pcie_device->handle, (u64)pcie_device->wwid);
1217
	if (pcie_device->enclosure_handle != 0)
1218 1219 1220
		ioc_info(ioc, "removing enclosure logical id(0x%016llx), slot(%d)\n",
			 (u64)pcie_device->enclosure_logical_id,
			 pcie_device->slot);
1221
	if (pcie_device->connector_name[0] != '\0')
1222 1223 1224
		ioc_info(ioc, "removing enclosure level(0x%04x), connector name( %s)\n",
			 pcie_device->enclosure_level,
			 pcie_device->connector_name);
1225 1226 1227 1228 1229 1230

	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
	if (!list_empty(&pcie_device->list)) {
		list_del_init(&pcie_device->list);
		was_on_pcie_device_list = 1;
	}
1231 1232
	if (pcie_device->shutdown_latency == ioc->max_shutdown_latency)
		update_latency = 1;
1233 1234 1235 1236 1237
	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
	if (was_on_pcie_device_list) {
		kfree(pcie_device->serial_number);
		pcie_device_put(pcie_device);
	}
1238 1239 1240 1241 1242 1243 1244 1245

	/*
	 * This device's RTD3 Entry Latency matches IOC's
	 * max_shutdown_latency. Recalculate IOC's max_shutdown_latency
	 * from the available drives as current drive is getting removed.
	 */
	if (update_latency)
		_scsih_set_nvme_max_shutdown_latency(ioc);
1246
}
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259


/**
 * _scsih_pcie_device_remove_by_handle - removing pcie device object by handle
 * @ioc: per adapter object
 * @handle: device handle
 */
static void
_scsih_pcie_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	struct _pcie_device *pcie_device;
	unsigned long flags;
	int was_on_pcie_device_list = 0;
1260
	u8 update_latency = 0;
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272

	if (ioc->shost_recovery)
		return;

	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
	pcie_device = __mpt3sas_get_pdev_by_handle(ioc, handle);
	if (pcie_device) {
		if (!list_empty(&pcie_device->list)) {
			list_del_init(&pcie_device->list);
			was_on_pcie_device_list = 1;
			pcie_device_put(pcie_device);
		}
1273 1274
		if (pcie_device->shutdown_latency == ioc->max_shutdown_latency)
			update_latency = 1;
1275 1276 1277 1278 1279 1280
	}
	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
	if (was_on_pcie_device_list) {
		_scsih_pcie_device_remove_from_sml(ioc, pcie_device);
		pcie_device_put(pcie_device);
	}
1281 1282 1283 1284 1285 1286 1287 1288

	/*
	 * This device's RTD3 Entry Latency matches IOC's
	 * max_shutdown_latency. Recalculate IOC's max_shutdown_latency
	 * from the available drives as current drive is getting removed.
	 */
	if (update_latency)
		_scsih_set_nvme_max_shutdown_latency(ioc);
1289 1290
}

1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
/**
 * _scsih_pcie_device_add - add pcie_device object
 * @ioc: per adapter object
 * @pcie_device: pcie_device object
 *
 * This is added to the pcie_device_list link list.
 */
static void
_scsih_pcie_device_add(struct MPT3SAS_ADAPTER *ioc,
	struct _pcie_device *pcie_device)
{
	unsigned long flags;

1304 1305 1306 1307
	dewtprintk(ioc,
		   ioc_info(ioc, "%s: handle (0x%04x), wwid(0x%016llx)\n",
			    __func__,
			    pcie_device->handle, (u64)pcie_device->wwid));
1308
	if (pcie_device->enclosure_handle != 0)
1309 1310 1311 1312 1313
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: enclosure logical id(0x%016llx), slot( %d)\n",
				    __func__,
				    (u64)pcie_device->enclosure_logical_id,
				    pcie_device->slot));
1314
	if (pcie_device->connector_name[0] != '\0')
1315 1316 1317 1318
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: enclosure level(0x%04x), connector name( %s)\n",
				    __func__, pcie_device->enclosure_level,
				    pcie_device->connector_name));
1319 1320 1321 1322 1323 1324

	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
	pcie_device_get(pcie_device);
	list_add_tail(&pcie_device->list, &ioc->pcie_device_list);
	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);

1325 1326 1327 1328 1329
	if (pcie_device->access_status ==
	    MPI26_PCIEDEV0_ASTATUS_DEVICE_BLOCKED) {
		clear_bit(pcie_device->handle, ioc->pend_os_device_add);
		return;
	}
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
	if (scsi_add_device(ioc->shost, PCIE_CHANNEL, pcie_device->id, 0)) {
		_scsih_pcie_device_remove(ioc, pcie_device);
	} else if (!pcie_device->starget) {
		if (!ioc->is_driver_loading) {
/*TODO-- Need to find out whether this condition will occur or not*/
			clear_bit(pcie_device->handle, ioc->pend_os_device_add);
		}
	} else
		clear_bit(pcie_device->handle, ioc->pend_os_device_add);
}

/*
 * _scsih_pcie_device_init_add - insert pcie_device to the init list.
 * @ioc: per adapter object
 * @pcie_device: the pcie_device object
 * Context: This function will acquire ioc->pcie_device_lock.
 *
 * Adding new object at driver load time to the ioc->pcie_device_init_list.
 */
static void
_scsih_pcie_device_init_add(struct MPT3SAS_ADAPTER *ioc,
				struct _pcie_device *pcie_device)
{
	unsigned long flags;

1355 1356 1357 1358
	dewtprintk(ioc,
		   ioc_info(ioc, "%s: handle (0x%04x), wwid(0x%016llx)\n",
			    __func__,
			    pcie_device->handle, (u64)pcie_device->wwid));
1359
	if (pcie_device->enclosure_handle != 0)
1360 1361 1362 1363 1364
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: enclosure logical id(0x%016llx), slot( %d)\n",
				    __func__,
				    (u64)pcie_device->enclosure_logical_id,
				    pcie_device->slot));
1365
	if (pcie_device->connector_name[0] != '\0')
1366 1367 1368 1369
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: enclosure level(0x%04x), connector name( %s)\n",
				    __func__, pcie_device->enclosure_level,
				    pcie_device->connector_name));
1370 1371 1372 1373

	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
	pcie_device_get(pcie_device);
	list_add_tail(&pcie_device->list, &ioc->pcie_device_init_list);
1374 1375 1376
	if (pcie_device->access_status !=
	    MPI26_PCIEDEV0_ASTATUS_DEVICE_BLOCKED)
		_scsih_determine_boot_device(ioc, pcie_device, PCIE_CHANNEL);
1377 1378
	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
}
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
/**
 * _scsih_raid_device_find_by_id - raid device search
 * @ioc: per adapter object
 * @id: sas device target id
 * @channel: sas device channel
 * Context: Calling function should acquire ioc->raid_device_lock
 *
 * This searches for raid_device based on target id, then return raid_device
 * object.
 */
static struct _raid_device *
_scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
{
	struct _raid_device *raid_device, *r;

	r = NULL;
	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
		if (raid_device->id == id && raid_device->channel == channel) {
			r = raid_device;
			goto out;
		}
	}

 out:
	return r;
}

/**
1407
 * mpt3sas_raid_device_find_by_handle - raid device search
1408 1409 1410 1411 1412 1413 1414
 * @ioc: per adapter object
 * @handle: sas device handle (assigned by firmware)
 * Context: Calling function should acquire ioc->raid_device_lock
 *
 * This searches for raid_device based on handle, then return raid_device
 * object.
 */
1415 1416
struct _raid_device *
mpt3sas_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
{
	struct _raid_device *raid_device, *r;

	r = NULL;
	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
		if (raid_device->handle != handle)
			continue;
		r = raid_device;
		goto out;
	}

 out:
	return r;
}

/**
 * _scsih_raid_device_find_by_wwid - raid device search
 * @ioc: per adapter object
1435
 * @wwid: ?
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
 * Context: Calling function should acquire ioc->raid_device_lock
 *
 * This searches for raid_device based on wwid, then return raid_device
 * object.
 */
static struct _raid_device *
_scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
{
	struct _raid_device *raid_device, *r;

	r = NULL;
	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
		if (raid_device->wwid != wwid)
			continue;
		r = raid_device;
		goto out;
	}

 out:
	return r;
}

/**
 * _scsih_raid_device_add - add raid_device object
 * @ioc: per adapter object
 * @raid_device: raid_device object
 *
 * This is added to the raid_device_list link list.
 */
static void
_scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
	struct _raid_device *raid_device)
{
	unsigned long flags;

1471 1472 1473 1474
	dewtprintk(ioc,
		   ioc_info(ioc, "%s: handle(0x%04x), wwid(0x%016llx)\n",
			    __func__,
			    raid_device->handle, (u64)raid_device->wwid));
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523

	spin_lock_irqsave(&ioc->raid_device_lock, flags);
	list_add_tail(&raid_device->list, &ioc->raid_device_list);
	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
}

/**
 * _scsih_raid_device_remove - delete raid_device object
 * @ioc: per adapter object
 * @raid_device: raid_device object
 *
 */
static void
_scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
	struct _raid_device *raid_device)
{
	unsigned long flags;

	spin_lock_irqsave(&ioc->raid_device_lock, flags);
	list_del(&raid_device->list);
	kfree(raid_device);
	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
}

/**
 * mpt3sas_scsih_expander_find_by_handle - expander device search
 * @ioc: per adapter object
 * @handle: expander handle (assigned by firmware)
 * Context: Calling function should acquire ioc->sas_device_lock
 *
 * This searches for expander device based on handle, then returns the
 * sas_node object.
 */
struct _sas_node *
mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	struct _sas_node *sas_expander, *r;

	r = NULL;
	list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
		if (sas_expander->handle != handle)
			continue;
		r = sas_expander;
		goto out;
	}
 out:
	return r;
}

1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
/**
 * mpt3sas_scsih_enclosure_find_by_handle - exclosure device search
 * @ioc: per adapter object
 * @handle: enclosure handle (assigned by firmware)
 * Context: Calling function should acquire ioc->sas_device_lock
 *
 * This searches for enclosure device based on handle, then returns the
 * enclosure object.
 */
static struct _enclosure_node *
mpt3sas_scsih_enclosure_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	struct _enclosure_node *enclosure_dev, *r;

	r = NULL;
	list_for_each_entry(enclosure_dev, &ioc->enclosure_list, list) {
		if (le16_to_cpu(enclosure_dev->pg0.EnclosureHandle) != handle)
			continue;
		r = enclosure_dev;
		goto out;
	}
out:
	return r;
}
1548 1549 1550 1551
/**
 * mpt3sas_scsih_expander_find_by_sas_address - expander device search
 * @ioc: per adapter object
 * @sas_address: sas address
1552
 * @port: hba port entry
1553 1554
 * Context: Calling function should acquire ioc->sas_node_lock.
 *
1555 1556
 * This searches for expander device based on sas_address & port number,
 * then returns the sas_node object.
1557 1558 1559
 */
struct _sas_node *
mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
1560
	u64 sas_address, struct hba_port *port)
1561
{
1562 1563 1564 1565
	struct _sas_node *sas_expander, *r = NULL;

	if (!port)
		return r;
1566 1567 1568 1569

	list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
		if (sas_expander->sas_address != sas_address)
			continue;
1570 1571
		if (sas_expander->port != port)
			continue;
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
		r = sas_expander;
		goto out;
	}
 out:
	return r;
}

/**
 * _scsih_expander_node_add - insert expander device to the list.
 * @ioc: per adapter object
 * @sas_expander: the sas_device object
 * Context: This function will acquire ioc->sas_node_lock.
 *
 * Adding new object to the ioc->sas_expander_list.
 */
static void
_scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
	struct _sas_node *sas_expander)
{
	unsigned long flags;

	spin_lock_irqsave(&ioc->sas_node_lock, flags);
	list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
}

/**
 * _scsih_is_end_device - determines if device is an end device
 * @device_info: bitfield providing information about the device.
 * Context: none
 *
1603
 * Return: 1 if end device.
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
 */
static int
_scsih_is_end_device(u32 device_info)
{
	if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
		((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
		(device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
		(device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
		return 1;
	else
		return 0;
}

1617
/**
1618 1619
 * _scsih_is_nvme_pciescsi_device - determines if
 *			device is an pcie nvme/scsi device
1620 1621 1622
 * @device_info: bitfield providing information about the device.
 * Context: none
 *
1623
 * Returns 1 if device is pcie device type nvme/scsi.
1624 1625
 */
static int
1626
_scsih_is_nvme_pciescsi_device(u32 device_info)
1627
{
1628 1629 1630 1631
	if (((device_info & MPI26_PCIE_DEVINFO_MASK_DEVICE_TYPE)
	    == MPI26_PCIE_DEVINFO_NVME) ||
	    ((device_info & MPI26_PCIE_DEVINFO_MASK_DEVICE_TYPE)
	    == MPI26_PCIE_DEVINFO_SCSI))
1632 1633 1634 1635 1636
		return 1;
	else
		return 0;
}

1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
/**
 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
 * @ioc: per adapter object
 * @id: target id
 * @channel: channel
 * Context: This function will acquire ioc->scsi_lookup_lock.
 *
 * This will search for a matching channel:id in the scsi_lookup array,
 * returning 1 if found.
 */
static u8
_scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
	int channel)
{
	int smid;
	struct scsi_cmnd *scmd;

	for (smid = 1;
	     smid <= ioc->shost->can_queue; smid++) {
		scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
		if (!scmd)
			continue;
		if (scmd->device->id == id &&
		    scmd->device->channel == channel)
			return 1;
	}
	return 0;
}

/**
 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
 * @ioc: per adapter object
 * @id: target id
 * @lun: lun number
 * @channel: channel
 * Context: This function will acquire ioc->scsi_lookup_lock.
 *
 * This will search for a matching channel:id:lun in the scsi_lookup array,
 * returning 1 if found.
 */
static u8
_scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
	unsigned int lun, int channel)
{
	int smid;
	struct scsi_cmnd *scmd;

	for (smid = 1; smid <= ioc->shost->can_queue; smid++) {

		scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
		if (!scmd)
			continue;
		if (scmd->device->id == id &&
		    scmd->device->channel == channel &&
		    scmd->device->lun == lun)
			return 1;
	}
	return 0;
}

1697
/**
1698
 * mpt3sas_scsih_scsi_lookup_get - returns scmd entry
1699 1700 1701
 * @ioc: per adapter object
 * @smid: system request message index
 *
1702
 * Return: the smid stored scmd pointer.
1703 1704
 * Then will dereference the stored scmd pointer.
 */
1705 1706
struct scsi_cmnd *
mpt3sas_scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1707 1708
{
	struct scsi_cmnd *scmd = NULL;
1709
	struct scsiio_tracker *st;
1710
	Mpi25SCSIIORequest_t *mpi_request;
1711

1712 1713 1714
	if (smid > 0  &&
	    smid <= ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT) {
		u32 unique_tag = smid - 1;
1715

1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
		mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);

		/*
		 * If SCSI IO request is outstanding at driver level then
		 * DevHandle filed must be non-zero. If DevHandle is zero
		 * then it means that this smid is free at driver level,
		 * so return NULL.
		 */
		if (!mpi_request->DevHandle)
			return scmd;

1727 1728 1729
		scmd = scsi_host_find_tag(ioc->shost, unique_tag);
		if (scmd) {
			st = scsi_cmd_priv(scmd);
1730
			if (st->cb_idx == 0xFF || st->smid == 0)
1731
				scmd = NULL;
1732 1733
		}
	}
1734
	return scmd;
1735 1736
}

1737
/**
1738
 * scsih_change_queue_depth - setting device queue depth
1739 1740 1741
 * @sdev: scsi device struct
 * @qdepth: requested queue depth
 *
1742
 * Return: queue depth.
1743
 */
1744
static int
1745
scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
{
	struct Scsi_Host *shost = sdev->host;
	int max_depth;
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	struct MPT3SAS_DEVICE *sas_device_priv_data;
	struct MPT3SAS_TARGET *sas_target_priv_data;
	struct _sas_device *sas_device;
	unsigned long flags;

	max_depth = shost->can_queue;

1757 1758 1759 1760 1761 1762 1763
	/*
	 * limit max device queue for SATA to 32 if enable_sdev_max_qd
	 * is disabled.
	 */
	if (ioc->enable_sdev_max_qd)
		goto not_sata;

1764 1765 1766 1767 1768 1769 1770 1771
	sas_device_priv_data = sdev->hostdata;
	if (!sas_device_priv_data)
		goto not_sata;
	sas_target_priv_data = sas_device_priv_data->sas_target;
	if (!sas_target_priv_data)
		goto not_sata;
	if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
		goto not_sata;
1772

1773
	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1774 1775 1776 1777 1778 1779 1780
	sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
	if (sas_device) {
		if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
			max_depth = MPT3SAS_SATA_QUEUE_DEPTH;

		sas_device_put(sas_device);
	}
1781 1782 1783 1784 1785 1786 1787 1788
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);

 not_sata:

	if (!sdev->tagged_supported)
		max_depth = 1;
	if (qdepth > max_depth)
		qdepth = max_depth;
1789 1790 1791 1792 1793 1794
	scsi_change_queue_depth(sdev, qdepth);
	sdev_printk(KERN_INFO, sdev,
	    "qdepth(%d), tagged(%d), scsi_level(%d), cmd_que(%d)\n",
	    sdev->queue_depth, sdev->tagged_supported,
	    sdev->scsi_level, ((sdev->inquiry[7] & 2) >> 1));
	return sdev->queue_depth;
1795 1796
}

1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
/**
 * mpt3sas_scsih_change_queue_depth - setting device queue depth
 * @sdev: scsi device struct
 * @qdepth: requested queue depth
 *
 * Returns nothing.
 */
void
mpt3sas_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
{
	struct Scsi_Host *shost = sdev->host;
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	if (ioc->enable_sdev_max_qd)
		qdepth = shost->can_queue;

	scsih_change_queue_depth(sdev, qdepth);
}

1816
/**
1817
 * scsih_target_alloc - target add routine
1818 1819
 * @starget: scsi target struct
 *
1820
 * Return: 0 if ok. Any other return is assumed to be an error and
1821 1822
 * the device is ignored.
 */
1823
static int
1824
scsih_target_alloc(struct scsi_target *starget)
1825 1826 1827 1828 1829 1830
{
	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	struct MPT3SAS_TARGET *sas_target_priv_data;
	struct _sas_device *sas_device;
	struct _raid_device *raid_device;
1831
	struct _pcie_device *pcie_device;
1832 1833 1834
	unsigned long flags;
	struct sas_rphy *rphy;

1835 1836
	sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
				       GFP_KERNEL);
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
	if (!sas_target_priv_data)
		return -ENOMEM;

	starget->hostdata = sas_target_priv_data;
	sas_target_priv_data->starget = starget;
	sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;

	/* RAID volumes */
	if (starget->channel == RAID_CHANNEL) {
		spin_lock_irqsave(&ioc->raid_device_lock, flags);
		raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
		    starget->channel);
		if (raid_device) {
			sas_target_priv_data->handle = raid_device->handle;
			sas_target_priv_data->sas_address = raid_device->wwid;
			sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1853
			if (ioc->is_warpdrive)
1854 1855
				sas_target_priv_data->raid_device = raid_device;
			raid_device->starget = starget;
1856 1857 1858 1859 1860
		}
		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
		return 0;
	}

1861 1862 1863 1864 1865 1866 1867 1868
	/* PCIe devices */
	if (starget->channel == PCIE_CHANNEL) {
		spin_lock_irqsave(&ioc->pcie_device_lock, flags);
		pcie_device = __mpt3sas_get_pdev_by_idchannel(ioc, starget->id,
			starget->channel);
		if (pcie_device) {
			sas_target_priv_data->handle = pcie_device->handle;
			sas_target_priv_data->sas_address = pcie_device->wwid;
1869
			sas_target_priv_data->port = NULL;
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
			sas_target_priv_data->pcie_dev = pcie_device;
			pcie_device->starget = starget;
			pcie_device->id = starget->id;
			pcie_device->channel = starget->channel;
			sas_target_priv_data->flags |=
				MPT_TARGET_FLAGS_PCIE_DEVICE;
			if (pcie_device->fast_path)
				sas_target_priv_data->flags |=
					MPT_TARGET_FASTPATH_IO;
		}
		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
		return 0;
	}

1884 1885 1886
	/* sas/sata devices */
	spin_lock_irqsave(&ioc->sas_device_lock, flags);
	rphy = dev_to_rphy(starget->dev.parent);
1887
	sas_device = __mpt3sas_get_sdev_by_rphy(ioc, rphy);
1888 1889 1890 1891

	if (sas_device) {
		sas_target_priv_data->handle = sas_device->handle;
		sas_target_priv_data->sas_address = sas_device->sas_address;
1892
		sas_target_priv_data->port = sas_device->port;
1893
		sas_target_priv_data->sas_dev = sas_device;
1894 1895 1896 1897 1898 1899 1900
		sas_device->starget = starget;
		sas_device->id = starget->id;
		sas_device->channel = starget->channel;
		if (test_bit(sas_device->handle, ioc->pd_handles))
			sas_target_priv_data->flags |=
			    MPT_TARGET_FLAGS_RAID_COMPONENT;
		if (sas_device->fast_path)
1901 1902
			sas_target_priv_data->flags |=
					MPT_TARGET_FASTPATH_IO;
1903 1904 1905 1906 1907 1908 1909
	}
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);

	return 0;
}

/**
1910
 * scsih_target_destroy - target destroy routine
1911 1912
 * @starget: scsi target struct
 */
1913
static void
1914
scsih_target_destroy(struct scsi_target *starget)
1915 1916 1917 1918 1919 1920
{
	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	struct MPT3SAS_TARGET *sas_target_priv_data;
	struct _sas_device *sas_device;
	struct _raid_device *raid_device;
1921
	struct _pcie_device *pcie_device;
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
	unsigned long flags;

	sas_target_priv_data = starget->hostdata;
	if (!sas_target_priv_data)
		return;

	if (starget->channel == RAID_CHANNEL) {
		spin_lock_irqsave(&ioc->raid_device_lock, flags);
		raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
		    starget->channel);
		if (raid_device) {
			raid_device->starget = NULL;
			raid_device->sdev = NULL;
		}
		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
		goto out;
	}

1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
	if (starget->channel == PCIE_CHANNEL) {
		spin_lock_irqsave(&ioc->pcie_device_lock, flags);
		pcie_device = __mpt3sas_get_pdev_from_target(ioc,
							sas_target_priv_data);
		if (pcie_device && (pcie_device->starget == starget) &&
			(pcie_device->id == starget->id) &&
			(pcie_device->channel == starget->channel))
			pcie_device->starget = NULL;

		if (pcie_device) {
			/*
			 * Corresponding get() is in _scsih_target_alloc()
			 */
			sas_target_priv_data->pcie_dev = NULL;
			pcie_device_put(pcie_device);
			pcie_device_put(pcie_device);
		}
		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
		goto out;
	}

1961
	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1962
	sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1963 1964 1965 1966 1967
	if (sas_device && (sas_device->starget == starget) &&
	    (sas_device->id == starget->id) &&
	    (sas_device->channel == starget->channel))
		sas_device->starget = NULL;

1968 1969 1970 1971
	if (sas_device) {
		/*
		 * Corresponding get() is in _scsih_target_alloc()
		 */
1972
		sas_target_priv_data->sas_dev = NULL;
1973 1974 1975 1976
		sas_device_put(sas_device);

		sas_device_put(sas_device);
	}
1977 1978 1979 1980 1981 1982 1983 1984
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);

 out:
	kfree(sas_target_priv_data);
	starget->hostdata = NULL;
}

/**
1985
 * scsih_slave_alloc - device add routine
1986 1987
 * @sdev: scsi device struct
 *
1988
 * Return: 0 if ok. Any other return is assumed to be an error and
1989 1990
 * the device is ignored.
 */
1991
static int
1992
scsih_slave_alloc(struct scsi_device *sdev)
1993 1994 1995 1996 1997 1998 1999
{
	struct Scsi_Host *shost;
	struct MPT3SAS_ADAPTER *ioc;
	struct MPT3SAS_TARGET *sas_target_priv_data;
	struct MPT3SAS_DEVICE *sas_device_priv_data;
	struct scsi_target *starget;
	struct _raid_device *raid_device;
2000
	struct _sas_device *sas_device;
2001
	struct _pcie_device *pcie_device;
2002 2003
	unsigned long flags;

2004 2005
	sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
				       GFP_KERNEL);
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
	if (!sas_device_priv_data)
		return -ENOMEM;

	sas_device_priv_data->lun = sdev->lun;
	sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;

	starget = scsi_target(sdev);
	sas_target_priv_data = starget->hostdata;
	sas_target_priv_data->num_luns++;
	sas_device_priv_data->sas_target = sas_target_priv_data;
	sdev->hostdata = sas_device_priv_data;
	if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
		sdev->no_uld_attach = 1;

	shost = dev_to_shost(&starget->dev);
	ioc = shost_priv(shost);
	if (starget->channel == RAID_CHANNEL) {
		spin_lock_irqsave(&ioc->raid_device_lock, flags);
		raid_device = _scsih_raid_device_find_by_id(ioc,
		    starget->id, starget->channel);
		if (raid_device)
			raid_device->sdev = sdev; /* raid is single lun */
		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
	}
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
	if (starget->channel == PCIE_CHANNEL) {
		spin_lock_irqsave(&ioc->pcie_device_lock, flags);
		pcie_device = __mpt3sas_get_pdev_by_wwid(ioc,
				sas_target_priv_data->sas_address);
		if (pcie_device && (pcie_device->starget == NULL)) {
			sdev_printk(KERN_INFO, sdev,
			    "%s : pcie_device->starget set to starget @ %d\n",
			    __func__, __LINE__);
			pcie_device->starget = starget;
		}

		if (pcie_device)
			pcie_device_put(pcie_device);
		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
2044

2045
	} else  if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
2046
		spin_lock_irqsave(&ioc->sas_device_lock, flags);
2047
		sas_device = __mpt3sas_get_sdev_by_addr(ioc,
2048 2049
		    sas_target_priv_data->sas_address,
		    sas_target_priv_data->port);
2050 2051 2052
		if (sas_device && (sas_device->starget == NULL)) {
			sdev_printk(KERN_INFO, sdev,
			"%s : sas_device->starget set to starget @ %d\n",
2053
			     __func__, __LINE__);
2054 2055
			sas_device->starget = starget;
		}
2056 2057 2058 2059

		if (sas_device)
			sas_device_put(sas_device);

2060 2061 2062
		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
	}

2063 2064 2065 2066
	return 0;
}

/**
2067
 * scsih_slave_destroy - device destroy routine
2068 2069
 * @sdev: scsi device struct
 */
2070
static void
2071
scsih_slave_destroy(struct scsi_device *sdev)
2072 2073 2074 2075 2076 2077
{
	struct MPT3SAS_TARGET *sas_target_priv_data;
	struct scsi_target *starget;
	struct Scsi_Host *shost;
	struct MPT3SAS_ADAPTER *ioc;
	struct _sas_device *sas_device;
2078
	struct _pcie_device *pcie_device;
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
	unsigned long flags;

	if (!sdev->hostdata)
		return;

	starget = scsi_target(sdev);
	sas_target_priv_data = starget->hostdata;
	sas_target_priv_data->num_luns--;

	shost = dev_to_shost(&starget->dev);
	ioc = shost_priv(shost);

2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
	if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_PCIE_DEVICE) {
		spin_lock_irqsave(&ioc->pcie_device_lock, flags);
		pcie_device = __mpt3sas_get_pdev_from_target(ioc,
				sas_target_priv_data);
		if (pcie_device && !sas_target_priv_data->num_luns)
			pcie_device->starget = NULL;

		if (pcie_device)
			pcie_device_put(pcie_device);

		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);

	} else if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
2104
		spin_lock_irqsave(&ioc->sas_device_lock, flags);
2105 2106
		sas_device = __mpt3sas_get_sdev_from_target(ioc,
				sas_target_priv_data);
2107 2108
		if (sas_device && !sas_target_priv_data->num_luns)
			sas_device->starget = NULL;
2109 2110 2111

		if (sas_device)
			sas_device_put(sas_device);
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
	}

	kfree(sdev->hostdata);
	sdev->hostdata = NULL;
}

/**
 * _scsih_display_sata_capabilities - sata capabilities
 * @ioc: per adapter object
 * @handle: device handle
 * @sdev: scsi device struct
 */
static void
_scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
	u16 handle, struct scsi_device *sdev)
{
	Mpi2ConfigReply_t mpi_reply;
	Mpi2SasDevicePage0_t sas_device_pg0;
	u32 ioc_status;
	u16 flags;
	u32 device_info;

	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
2137 2138
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
2139 2140 2141 2142 2143 2144
		return;
	}

	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
	    MPI2_IOCSTATUS_MASK;
	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2145 2146
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
		return;
	}

	flags = le16_to_cpu(sas_device_pg0.Flags);
	device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);

	sdev_printk(KERN_INFO, sdev,
	    "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
	    "sw_preserve(%s)\n",
	    (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
	    "n",
	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
}

/*
 * raid transport support -
 * Enabled for SLES11 and newer, in older kernels the driver will panic when
2168
 * unloading the driver followed by a load - I believe that the subroutine
2169 2170 2171 2172
 * raid_class_release() is not cleaning up properly.
 */

/**
2173
 * scsih_is_raid - return boolean indicating device is raid volume
2174
 * @dev: the device struct object
2175
 */
2176
static int
2177
scsih_is_raid(struct device *dev)
2178 2179
{
	struct scsi_device *sdev = to_scsi_device(dev);
2180
	struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
2181

2182 2183
	if (ioc->is_warpdrive)
		return 0;
2184 2185 2186
	return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
}

2187 2188 2189 2190 2191 2192 2193 2194
static int
scsih_is_nvme(struct device *dev)
{
	struct scsi_device *sdev = to_scsi_device(dev);

	return (sdev->channel == PCIE_CHANNEL) ? 1 : 0;
}

2195
/**
2196
 * scsih_get_resync - get raid volume resync percent complete
2197
 * @dev: the device struct object
2198
 */
2199
static void
2200
scsih_get_resync(struct device *dev)
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213
{
	struct scsi_device *sdev = to_scsi_device(dev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
	static struct _raid_device *raid_device;
	unsigned long flags;
	Mpi2RaidVolPage0_t vol_pg0;
	Mpi2ConfigReply_t mpi_reply;
	u32 volume_status_flags;
	u8 percent_complete;
	u16 handle;

	percent_complete = 0;
	handle = 0;
2214 2215 2216
	if (ioc->is_warpdrive)
		goto out;

2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
	spin_lock_irqsave(&ioc->raid_device_lock, flags);
	raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
	    sdev->channel);
	if (raid_device) {
		handle = raid_device->handle;
		percent_complete = raid_device->percent_complete;
	}
	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);

	if (!handle)
		goto out;

	if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
	     MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
	     sizeof(Mpi2RaidVolPage0_t))) {
2232 2233
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243
		percent_complete = 0;
		goto out;
	}

	volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
	if (!(volume_status_flags &
	    MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
		percent_complete = 0;

 out:
2244 2245 2246

	switch (ioc->hba_mpi_version_belonged) {
	case MPI2_VERSION:
2247
		raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
2248 2249 2250
		break;
	case MPI25_VERSION:
	case MPI26_VERSION:
2251
		raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
2252 2253
		break;
	}
2254 2255 2256
}

/**
2257
 * scsih_get_state - get raid volume level
2258
 * @dev: the device struct object
2259
 */
2260
static void
2261
scsih_get_state(struct device *dev)
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
{
	struct scsi_device *sdev = to_scsi_device(dev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
	static struct _raid_device *raid_device;
	unsigned long flags;
	Mpi2RaidVolPage0_t vol_pg0;
	Mpi2ConfigReply_t mpi_reply;
	u32 volstate;
	enum raid_state state = RAID_STATE_UNKNOWN;
	u16 handle = 0;

	spin_lock_irqsave(&ioc->raid_device_lock, flags);
	raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
	    sdev->channel);
	if (raid_device)
		handle = raid_device->handle;
	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);

	if (!raid_device)
		goto out;

	if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
	     MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
	     sizeof(Mpi2RaidVolPage0_t))) {
2286 2287
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
		goto out;
	}

	volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
	if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
		state = RAID_STATE_RESYNCING;
		goto out;
	}

	switch (vol_pg0.VolumeState) {
	case MPI2_RAID_VOL_STATE_OPTIMAL:
	case MPI2_RAID_VOL_STATE_ONLINE:
		state = RAID_STATE_ACTIVE;
		break;
	case  MPI2_RAID_VOL_STATE_DEGRADED:
		state = RAID_STATE_DEGRADED;
		break;
	case MPI2_RAID_VOL_STATE_FAILED:
	case MPI2_RAID_VOL_STATE_MISSING:
		state = RAID_STATE_OFFLINE;
		break;
	}
 out:
2311 2312
	switch (ioc->hba_mpi_version_belonged) {
	case MPI2_VERSION:
2313
		raid_set_state(mpt2sas_raid_template, dev, state);
2314 2315 2316
		break;
	case MPI25_VERSION:
	case MPI26_VERSION:
2317
		raid_set_state(mpt3sas_raid_template, dev, state);
2318 2319
		break;
	}
2320 2321 2322 2323
}

/**
 * _scsih_set_level - set raid level
2324
 * @ioc: ?
2325 2326 2327 2328
 * @sdev: scsi device struct
 * @volume_type: volume type
 */
static void
2329 2330
_scsih_set_level(struct MPT3SAS_ADAPTER *ioc,
	struct scsi_device *sdev, u8 volume_type)
2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
{
	enum raid_level level = RAID_LEVEL_UNKNOWN;

	switch (volume_type) {
	case MPI2_RAID_VOL_TYPE_RAID0:
		level = RAID_LEVEL_0;
		break;
	case MPI2_RAID_VOL_TYPE_RAID10:
		level = RAID_LEVEL_10;
		break;
	case MPI2_RAID_VOL_TYPE_RAID1E:
		level = RAID_LEVEL_1E;
		break;
	case MPI2_RAID_VOL_TYPE_RAID1:
		level = RAID_LEVEL_1;
		break;
	}

2349 2350
	switch (ioc->hba_mpi_version_belonged) {
	case MPI2_VERSION:
2351
		raid_set_level(mpt2sas_raid_template,
2352 2353 2354 2355
			&sdev->sdev_gendev, level);
		break;
	case MPI25_VERSION:
	case MPI26_VERSION:
2356
		raid_set_level(mpt3sas_raid_template,
2357 2358 2359
			&sdev->sdev_gendev, level);
		break;
	}
2360 2361 2362 2363 2364 2365
}


/**
 * _scsih_get_volume_capabilities - volume capabilities
 * @ioc: per adapter object
2366
 * @raid_device: the raid_device object
2367
 *
2368
 * Return: 0 for success, else 1
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382
 */
static int
_scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
	struct _raid_device *raid_device)
{
	Mpi2RaidVolPage0_t *vol_pg0;
	Mpi2RaidPhysDiskPage0_t pd_pg0;
	Mpi2SasDevicePage0_t sas_device_pg0;
	Mpi2ConfigReply_t mpi_reply;
	u16 sz;
	u8 num_pds;

	if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
	    &num_pds)) || !num_pds) {
2383 2384 2385
		dfailprintk(ioc,
			    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
				     __FILE__, __LINE__, __func__));
2386 2387 2388 2389 2390 2391 2392 2393
		return 1;
	}

	raid_device->num_pds = num_pds;
	sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
	    sizeof(Mpi2RaidVol0PhysDisk_t));
	vol_pg0 = kzalloc(sz, GFP_KERNEL);
	if (!vol_pg0) {
2394 2395 2396
		dfailprintk(ioc,
			    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
				     __FILE__, __LINE__, __func__));
2397 2398 2399 2400 2401
		return 1;
	}

	if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
	     MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
2402 2403 2404
		dfailprintk(ioc,
			    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
				     __FILE__, __LINE__, __func__));
2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456
		kfree(vol_pg0);
		return 1;
	}

	raid_device->volume_type = vol_pg0->VolumeType;

	/* figure out what the underlying devices are by
	 * obtaining the device_info bits for the 1st device
	 */
	if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
	    &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
	    vol_pg0->PhysDisk[0].PhysDiskNum))) {
		if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
		    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
		    le16_to_cpu(pd_pg0.DevHandle)))) {
			raid_device->device_info =
			    le32_to_cpu(sas_device_pg0.DeviceInfo);
		}
	}

	kfree(vol_pg0);
	return 0;
}

/**
 * _scsih_enable_tlr - setting TLR flags
 * @ioc: per adapter object
 * @sdev: scsi device struct
 *
 * Enabling Transaction Layer Retries for tape devices when
 * vpd page 0x90 is present
 *
 */
static void
_scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
{

	/* only for TAPE */
	if (sdev->type != TYPE_TAPE)
		return;

	if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
		return;

	sas_enable_tlr(sdev);
	sdev_printk(KERN_INFO, sdev, "TLR %s\n",
	    sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
	return;

}

/**
2457
 * scsih_slave_configure - device configure routine.
2458 2459
 * @sdev: scsi device struct
 *
2460
 * Return: 0 if ok. Any other return is assumed to be an error and
2461 2462
 * the device is ignored.
 */
2463
static int
2464
scsih_slave_configure(struct scsi_device *sdev)
2465 2466 2467 2468 2469 2470
{
	struct Scsi_Host *shost = sdev->host;
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	struct MPT3SAS_DEVICE *sas_device_priv_data;
	struct MPT3SAS_TARGET *sas_target_priv_data;
	struct _sas_device *sas_device;
2471
	struct _pcie_device *pcie_device;
2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
	struct _raid_device *raid_device;
	unsigned long flags;
	int qdepth;
	u8 ssp_target = 0;
	char *ds = "";
	char *r_level = "";
	u16 handle, volume_handle = 0;
	u64 volume_wwid = 0;

	qdepth = 1;
	sas_device_priv_data = sdev->hostdata;
	sas_device_priv_data->configured_lun = 1;
	sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
	sas_target_priv_data = sas_device_priv_data->sas_target;
	handle = sas_target_priv_data->handle;

	/* raid volume handling */
	if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {

		spin_lock_irqsave(&ioc->raid_device_lock, flags);
2492
		raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
2493 2494
		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
		if (!raid_device) {
2495 2496 2497
			dfailprintk(ioc,
				    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
					     __FILE__, __LINE__, __func__));
2498 2499 2500 2501
			return 1;
		}

		if (_scsih_get_volume_capabilities(ioc, raid_device)) {
2502 2503 2504
			dfailprintk(ioc,
				    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
					     __FILE__, __LINE__, __func__));
2505 2506 2507
			return 1;
		}

2508 2509 2510
		/*
		 * WARPDRIVE: Initialize the required data for Direct IO
		 */
2511
		mpt3sas_init_warpdrive_properties(ioc, raid_device);
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523

		/* RAID Queue Depth Support
		 * IS volume = underlying qdepth of drive type, either
		 *    MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
		 * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
		 */
		if (raid_device->device_info &
		    MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
			qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
			ds = "SSP";
		} else {
			qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
B
Bart Van Assche 已提交
2524
			if (raid_device->device_info &
2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559
			    MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
				ds = "SATA";
			else
				ds = "STP";
		}

		switch (raid_device->volume_type) {
		case MPI2_RAID_VOL_TYPE_RAID0:
			r_level = "RAID0";
			break;
		case MPI2_RAID_VOL_TYPE_RAID1E:
			qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
			if (ioc->manu_pg10.OEMIdentifier &&
			    (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
			    MFG10_GF0_R10_DISPLAY) &&
			    !(raid_device->num_pds % 2))
				r_level = "RAID10";
			else
				r_level = "RAID1E";
			break;
		case MPI2_RAID_VOL_TYPE_RAID1:
			qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
			r_level = "RAID1";
			break;
		case MPI2_RAID_VOL_TYPE_RAID10:
			qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
			r_level = "RAID10";
			break;
		case MPI2_RAID_VOL_TYPE_UNKNOWN:
		default:
			qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
			r_level = "RAIDX";
			break;
		}

2560 2561 2562 2563 2564 2565 2566
		if (!ioc->hide_ir_msg)
			sdev_printk(KERN_INFO, sdev,
			   "%s: handle(0x%04x), wwid(0x%016llx),"
			    " pd_count(%d), type(%s)\n",
			    r_level, raid_device->handle,
			    (unsigned long long)raid_device->wwid,
			    raid_device->num_pds, ds);
2567

2568 2569 2570 2571 2572 2573 2574 2575
		if (shost->max_sectors > MPT3SAS_RAID_MAX_SECTORS) {
			blk_queue_max_hw_sectors(sdev->request_queue,
						MPT3SAS_RAID_MAX_SECTORS);
			sdev_printk(KERN_INFO, sdev,
					"Set queue's max_sector to: %u\n",
						MPT3SAS_RAID_MAX_SECTORS);
		}

2576
		mpt3sas_scsih_change_queue_depth(sdev, qdepth);
2577

2578 2579
		/* raid transport support */
		if (!ioc->is_warpdrive)
2580
			_scsih_set_level(ioc, sdev, raid_device->volume_type);
2581 2582 2583 2584 2585 2586 2587
		return 0;
	}

	/* non-raid handling */
	if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
		if (mpt3sas_config_get_volume_handle(ioc, handle,
		    &volume_handle)) {
2588 2589 2590
			dfailprintk(ioc,
				    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
					     __FILE__, __LINE__, __func__));
2591 2592 2593 2594
			return 1;
		}
		if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
		    volume_handle, &volume_wwid)) {
2595 2596 2597
			dfailprintk(ioc,
				    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
					     __FILE__, __LINE__, __func__));
2598 2599 2600 2601
			return 1;
		}
	}

2602 2603 2604 2605 2606 2607 2608
	/* PCIe handling */
	if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_PCIE_DEVICE) {
		spin_lock_irqsave(&ioc->pcie_device_lock, flags);
		pcie_device = __mpt3sas_get_pdev_by_wwid(ioc,
				sas_device_priv_data->sas_target->sas_address);
		if (!pcie_device) {
			spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
2609 2610 2611
			dfailprintk(ioc,
				    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
					     __FILE__, __LINE__, __func__));
2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636
			return 1;
		}

		qdepth = MPT3SAS_NVME_QUEUE_DEPTH;
		ds = "NVMe";
		sdev_printk(KERN_INFO, sdev,
			"%s: handle(0x%04x), wwid(0x%016llx), port(%d)\n",
			ds, handle, (unsigned long long)pcie_device->wwid,
			pcie_device->port_num);
		if (pcie_device->enclosure_handle != 0)
			sdev_printk(KERN_INFO, sdev,
			"%s: enclosure logical id(0x%016llx), slot(%d)\n",
			ds,
			(unsigned long long)pcie_device->enclosure_logical_id,
			pcie_device->slot);
		if (pcie_device->connector_name[0] != '\0')
			sdev_printk(KERN_INFO, sdev,
				"%s: enclosure level(0x%04x),"
				"connector name( %s)\n", ds,
				pcie_device->enclosure_level,
				pcie_device->connector_name);

		if (pcie_device->nvme_mdts)
			blk_queue_max_hw_sectors(sdev->request_queue,
					pcie_device->nvme_mdts/512);
2637 2638 2639

		pcie_device_put(pcie_device);
		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
2640
		mpt3sas_scsih_change_queue_depth(sdev, qdepth);
2641 2642 2643 2644
		/* Enable QUEUE_FLAG_NOMERGES flag, so that IOs won't be
		 ** merged and can eliminate holes created during merging
		 ** operation.
		 **/
2645
		blk_queue_flag_set(QUEUE_FLAG_NOMERGES,
2646 2647 2648 2649 2650 2651
				sdev->request_queue);
		blk_queue_virt_boundary(sdev->request_queue,
				ioc->page_size - 1);
		return 0;
	}

2652
	spin_lock_irqsave(&ioc->sas_device_lock, flags);
2653
	sas_device = __mpt3sas_get_sdev_by_addr(ioc,
2654 2655
	   sas_device_priv_data->sas_target->sas_address,
	   sas_device_priv_data->sas_target->port);
2656 2657
	if (!sas_device) {
		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2658 2659 2660
		dfailprintk(ioc,
			    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
				     __FILE__, __LINE__, __func__));
2661 2662 2663 2664 2665 2666 2667 2668
		return 1;
	}

	sas_device->volume_handle = volume_handle;
	sas_device->volume_wwid = volume_wwid;
	if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
		qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
		ssp_target = 1;
2669 2670 2671 2672 2673 2674 2675 2676 2677
		if (sas_device->device_info &
				MPI2_SAS_DEVICE_INFO_SEP) {
			sdev_printk(KERN_WARNING, sdev,
			"set ignore_delay_remove for handle(0x%04x)\n",
			sas_device_priv_data->sas_target->handle);
			sas_device_priv_data->ignore_delay_remove = 1;
			ds = "SES";
		} else
			ds = "SSP";
2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690
	} else {
		qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
		if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
			ds = "STP";
		else if (sas_device->device_info &
		    MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
			ds = "SATA";
	}

	sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
	    "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
	    ds, handle, (unsigned long long)sas_device->sas_address,
	    sas_device->phy, (unsigned long long)sas_device->device_name);
2691 2692

	_scsih_display_enclosure_chassis_info(NULL, sas_device, sdev, NULL);
2693

2694
	sas_device_put(sas_device);
2695 2696 2697 2698 2699 2700
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);

	if (!ssp_target)
		_scsih_display_sata_capabilities(ioc, handle, sdev);


2701
	mpt3sas_scsih_change_queue_depth(sdev, qdepth);
2702 2703 2704 2705 2706 2707 2708 2709 2710 2711

	if (ssp_target) {
		sas_read_port_mode_page(sdev);
		_scsih_enable_tlr(ioc, sdev);
	}

	return 0;
}

/**
2712
 * scsih_bios_param - fetch head, sector, cylinder info for a disk
2713 2714 2715 2716 2717 2718 2719 2720
 * @sdev: scsi device struct
 * @bdev: pointer to block device context
 * @capacity: device size (in 512 byte sectors)
 * @params: three element array to place output:
 *              params[0] number of heads (max 255)
 *              params[1] number of sectors (max 63)
 *              params[2] number of cylinders
 */
2721
static int
2722
scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
	sector_t capacity, int params[])
{
	int		heads;
	int		sectors;
	sector_t	cylinders;
	ulong		dummy;

	heads = 64;
	sectors = 32;

	dummy = heads * sectors;
	cylinders = capacity;
	sector_div(cylinders, dummy);

	/*
	 * Handle extended translation size for logical drives
	 * > 1Gb
	 */
	if ((ulong)capacity >= 0x200000) {
		heads = 255;
		sectors = 63;
		dummy = heads * sectors;
		cylinders = capacity;
		sector_div(cylinders, dummy);
	}

	/* return result */
	params[0] = heads;
	params[1] = sectors;
	params[2] = cylinders;

	return 0;
}

/**
 * _scsih_response_code - translation of device response code
 * @ioc: per adapter object
 * @response_code: response code returned by the device
 */
static void
_scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
{
	char *desc;

	switch (response_code) {
	case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
		desc = "task management request completed";
		break;
	case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
		desc = "invalid frame";
		break;
	case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
		desc = "task management request not supported";
		break;
	case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
		desc = "task management request failed";
		break;
	case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
		desc = "task management request succeeded";
		break;
	case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
		desc = "invalid lun";
		break;
	case 0xA:
		desc = "overlapped tag attempted";
		break;
	case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
		desc = "task queued, however not sent to target";
		break;
	default:
		desc = "unknown";
		break;
	}
2796
	ioc_warn(ioc, "response_code(0x%01x): %s\n", response_code, desc);
2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808
}

/**
 * _scsih_tm_done - tm completion routine
 * @ioc: per adapter object
 * @smid: system request message index
 * @msix_index: MSIX table index supplied by the OS
 * @reply: reply message frame(lower 32bit addr)
 * Context: none.
 *
 * The callback handler when using scsih_issue_tm.
 *
2809 2810
 * Return: 1 meaning mf should be freed from _base_interrupt
 *         0 means the mf is freed from this function.
2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887
 */
static u8
_scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
{
	MPI2DefaultReply_t *mpi_reply;

	if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
		return 1;
	if (ioc->tm_cmds.smid != smid)
		return 1;
	ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
	mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
	if (mpi_reply) {
		memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
		ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
	}
	ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
	complete(&ioc->tm_cmds.done);
	return 1;
}

/**
 * mpt3sas_scsih_set_tm_flag - set per target tm_busy
 * @ioc: per adapter object
 * @handle: device handle
 *
 * During taskmangement request, we need to freeze the device queue.
 */
void
mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	struct MPT3SAS_DEVICE *sas_device_priv_data;
	struct scsi_device *sdev;
	u8 skip = 0;

	shost_for_each_device(sdev, ioc->shost) {
		if (skip)
			continue;
		sas_device_priv_data = sdev->hostdata;
		if (!sas_device_priv_data)
			continue;
		if (sas_device_priv_data->sas_target->handle == handle) {
			sas_device_priv_data->sas_target->tm_busy = 1;
			skip = 1;
			ioc->ignore_loginfos = 1;
		}
	}
}

/**
 * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
 * @ioc: per adapter object
 * @handle: device handle
 *
 * During taskmangement request, we need to freeze the device queue.
 */
void
mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	struct MPT3SAS_DEVICE *sas_device_priv_data;
	struct scsi_device *sdev;
	u8 skip = 0;

	shost_for_each_device(sdev, ioc->shost) {
		if (skip)
			continue;
		sas_device_priv_data = sdev->hostdata;
		if (!sas_device_priv_data)
			continue;
		if (sas_device_priv_data->sas_target->handle == handle) {
			sas_device_priv_data->sas_target->tm_busy = 0;
			skip = 1;
			ioc->ignore_loginfos = 0;
		}
	}
}

2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977
/**
 * scsih_tm_cmd_map_status - map the target reset & LUN reset TM status
 * @ioc - per adapter object
 * @channel - the channel assigned by the OS
 * @id: the id assigned by the OS
 * @lun: lun number
 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
 * @smid_task: smid assigned to the task
 *
 * Look whether TM has aborted the timed out SCSI command, if
 * TM has aborted the IO then return SUCCESS else return FAILED.
 */
static int
scsih_tm_cmd_map_status(struct MPT3SAS_ADAPTER *ioc, uint channel,
	uint id, uint lun, u8 type, u16 smid_task)
{

	if (smid_task <= ioc->shost->can_queue) {
		switch (type) {
		case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
			if (!(_scsih_scsi_lookup_find_by_target(ioc,
			    id, channel)))
				return SUCCESS;
			break;
		case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
		case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
			if (!(_scsih_scsi_lookup_find_by_lun(ioc, id,
			    lun, channel)))
				return SUCCESS;
			break;
		default:
			return SUCCESS;
		}
	} else if (smid_task == ioc->scsih_cmds.smid) {
		if ((ioc->scsih_cmds.status & MPT3_CMD_COMPLETE) ||
		    (ioc->scsih_cmds.status & MPT3_CMD_NOT_USED))
			return SUCCESS;
	} else if (smid_task == ioc->ctl_cmds.smid) {
		if ((ioc->ctl_cmds.status & MPT3_CMD_COMPLETE) ||
		    (ioc->ctl_cmds.status & MPT3_CMD_NOT_USED))
			return SUCCESS;
	}

	return FAILED;
}

/**
 * scsih_tm_post_processing - post processing of target & LUN reset
 * @ioc - per adapter object
 * @handle: device handle
 * @channel - the channel assigned by the OS
 * @id: the id assigned by the OS
 * @lun: lun number
 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
 * @smid_task: smid assigned to the task
 *
 * Post processing of target & LUN reset. Due to interrupt latency
 * issue it possible that interrupt for aborted IO might not be
 * received yet. So before returning failure status, poll the
 * reply descriptor pools for the reply of timed out SCSI command.
 * Return FAILED status if reply for timed out is not received
 * otherwise return SUCCESS.
 */
static int
scsih_tm_post_processing(struct MPT3SAS_ADAPTER *ioc, u16 handle,
	uint channel, uint id, uint lun, u8 type, u16 smid_task)
{
	int rc;

	rc = scsih_tm_cmd_map_status(ioc, channel, id, lun, type, smid_task);
	if (rc == SUCCESS)
		return rc;

	ioc_info(ioc,
	    "Poll ReplyDescriptor queues for completion of"
	    " smid(%d), task_type(0x%02x), handle(0x%04x)\n",
	    smid_task, type, handle);

	/*
	 * Due to interrupt latency issues, driver may receive interrupt for
	 * TM first and then for aborted SCSI IO command. So, poll all the
	 * ReplyDescriptor pools before returning the FAILED status to SML.
	 */
	mpt3sas_base_mask_interrupts(ioc);
	mpt3sas_base_sync_reply_irqs(ioc, 1);
	mpt3sas_base_unmask_interrupts(ioc);

	return scsih_tm_cmd_map_status(ioc, channel, id, lun, type, smid_task);
}

2978 2979 2980
/**
 * mpt3sas_scsih_issue_tm - main routine for sending tm requests
 * @ioc: per adapter struct
2981
 * @handle: device handle
2982 2983
 * @channel: the channel assigned by the OS
 * @id: the id assigned by the OS
2984 2985 2986
 * @lun: lun number
 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
 * @smid_task: smid assigned to the task
2987
 * @msix_task: MSIX table index supplied by the OS
2988
 * @timeout: timeout in seconds
2989
 * @tr_method: Target Reset Method
2990 2991 2992 2993 2994
 * Context: user
 *
 * A generic API for sending task management requests to firmware.
 *
 * The callback index is set inside `ioc->tm_cb_idx`.
2995
 * The caller is responsible to check for outstanding commands.
2996
 *
2997
 * Return: SUCCESS or FAILED.
2998 2999
 */
int
3000 3001 3002
mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
	uint id, u64 lun, u8 type, u16 smid_task, u16 msix_task,
	u8 timeout, u8 tr_method)
3003 3004 3005
{
	Mpi2SCSITaskManagementRequest_t *mpi_request;
	Mpi2SCSITaskManagementReply_t *mpi_reply;
3006
	Mpi25SCSIIORequest_t *request;
3007 3008 3009
	u16 smid = 0;
	u32 ioc_state;
	int rc;
3010
	u8 issue_reset = 0;
3011

3012 3013
	lockdep_assert_held(&ioc->tm_cmds.mutex);

3014
	if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
3015
		ioc_info(ioc, "%s: tm_cmd busy!!!\n", __func__);
3016
		return FAILED;
3017 3018 3019 3020
	}

	if (ioc->shost_recovery || ioc->remove_host ||
	    ioc->pci_error_recovery) {
3021
		ioc_info(ioc, "%s: host reset in progress!\n", __func__);
3022
		return FAILED;
3023 3024 3025 3026
	}

	ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
	if (ioc_state & MPI2_DOORBELL_USED) {
3027
		dhsprintk(ioc, ioc_info(ioc, "unexpected doorbell active!\n"));
3028
		rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
3029
		return (!rc) ? SUCCESS : FAILED;
3030 3031 3032
	}

	if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
3033
		mpt3sas_print_fault_code(ioc, ioc_state &
3034
		    MPI2_DOORBELL_DATA_MASK);
3035
		rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
3036
		return (!rc) ? SUCCESS : FAILED;
3037 3038
	} else if ((ioc_state & MPI2_IOC_STATE_MASK) ==
	    MPI2_IOC_STATE_COREDUMP) {
3039
		mpt3sas_print_coredump_info(ioc, ioc_state &
3040 3041 3042
		    MPI2_DOORBELL_DATA_MASK);
		rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
		return (!rc) ? SUCCESS : FAILED;
3043 3044 3045 3046
	}

	smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
	if (!smid) {
3047
		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
3048
		return FAILED;
3049 3050
	}

3051 3052 3053
	dtmprintk(ioc,
		  ioc_info(ioc, "sending tm: handle(0x%04x), task_type(0x%02x), smid(%d), timeout(%d), tr_method(0x%x)\n",
			   handle, type, smid_task, timeout, tr_method));
3054 3055 3056 3057 3058 3059 3060 3061
	ioc->tm_cmds.status = MPT3_CMD_PENDING;
	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
	ioc->tm_cmds.smid = smid;
	memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
	memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
	mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
	mpi_request->DevHandle = cpu_to_le16(handle);
	mpi_request->TaskType = type;
3062 3063 3064
	if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
	    type == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
		mpi_request->MsgFlags = tr_method;
3065 3066 3067 3068
	mpi_request->TaskMID = cpu_to_le16(smid_task);
	int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
	mpt3sas_scsih_set_tm_flag(ioc, handle);
	init_completion(&ioc->tm_cmds.done);
3069
	ioc->put_smid_hi_priority(ioc, smid, msix_task);
3070
	wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
3071
	if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
3072 3073 3074 3075
		mpt3sas_check_cmd_timeout(ioc,
		    ioc->tm_cmds.status, mpi_request,
		    sizeof(Mpi2SCSITaskManagementRequest_t)/4, issue_reset);
		if (issue_reset) {
3076 3077
			rc = mpt3sas_base_hard_reset_handler(ioc,
					FORCE_BIG_HAMMER);
3078
			rc = (!rc) ? SUCCESS : FAILED;
3079
			goto out;
3080 3081 3082
		}
	}

3083
	/* sync IRQs in case those were busy during flush. */
3084
	mpt3sas_base_sync_reply_irqs(ioc, 0);
3085

3086 3087 3088
	if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
		mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
		mpi_reply = ioc->tm_cmds.reply;
3089 3090 3091 3092 3093
		dtmprintk(ioc,
			  ioc_info(ioc, "complete tm: ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
				   le16_to_cpu(mpi_reply->IOCStatus),
				   le32_to_cpu(mpi_reply->IOCLogInfo),
				   le32_to_cpu(mpi_reply->TerminationCount)));
3094 3095 3096 3097 3098 3099 3100
		if (ioc->logging_level & MPT_DEBUG_TM) {
			_scsih_response_code(ioc, mpi_reply->ResponseCode);
			if (mpi_reply->IOCStatus)
				_debug_dump_mf(mpi_request,
				    sizeof(Mpi2SCSITaskManagementRequest_t)/4);
		}
	}
3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138

	switch (type) {
	case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
		rc = SUCCESS;
		/*
		 * If DevHandle filed in smid_task's entry of request pool
		 * doesn't match with device handle on which this task abort
		 * TM is received then it means that TM has successfully
		 * aborted the timed out command. Since smid_task's entry in
		 * request pool will be memset to zero once the timed out
		 * command is returned to the SML. If the command is not
		 * aborted then smid_task’s entry won’t be cleared and it
		 * will have same DevHandle value on which this task abort TM
		 * is received and driver will return the TM status as FAILED.
		 */
		request = mpt3sas_base_get_msg_frame(ioc, smid_task);
		if (le16_to_cpu(request->DevHandle) != handle)
			break;

		ioc_info(ioc, "Task abort tm failed: handle(0x%04x),"
		    "timeout(%d) tr_method(0x%x) smid(%d) msix_index(%d)\n",
		    handle, timeout, tr_method, smid_task, msix_task);
		rc = FAILED;
		break;

	case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
	case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
	case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
		rc = scsih_tm_post_processing(ioc, handle, channel, id, lun,
		    type, smid_task);
		break;
	case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
		rc = SUCCESS;
		break;
	default:
		rc = FAILED;
		break;
	}
3139

3140
out:
3141 3142 3143
	mpt3sas_scsih_clear_tm_flag(ioc, handle);
	ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
	return rc;
3144
}
3145

3146
int mpt3sas_scsih_issue_locked_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle,
3147 3148
		uint channel, uint id, u64 lun, u8 type, u16 smid_task,
		u16 msix_task, u8 timeout, u8 tr_method)
3149 3150 3151 3152
{
	int ret;

	mutex_lock(&ioc->tm_cmds.mutex);
3153 3154
	ret = mpt3sas_scsih_issue_tm(ioc, handle, channel, id, lun, type,
			smid_task, msix_task, timeout, tr_method);
3155 3156 3157
	mutex_unlock(&ioc->tm_cmds.mutex);

	return ret;
3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172
}

/**
 * _scsih_tm_display_info - displays info about the device
 * @ioc: per adapter struct
 * @scmd: pointer to scsi command object
 *
 * Called by task management callback handlers.
 */
static void
_scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
{
	struct scsi_target *starget = scmd->device->sdev_target;
	struct MPT3SAS_TARGET *priv_target = starget->hostdata;
	struct _sas_device *sas_device = NULL;
3173
	struct _pcie_device *pcie_device = NULL;
3174 3175 3176 3177 3178
	unsigned long flags;
	char *device_str = NULL;

	if (!priv_target)
		return;
3179 3180 3181 3182
	if (ioc->hide_ir_msg)
		device_str = "WarpDrive";
	else
		device_str = "volume";
3183 3184 3185 3186 3187 3188 3189

	scsi_print_command(scmd);
	if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
		starget_printk(KERN_INFO, starget,
			"%s handle(0x%04x), %s wwid(0x%016llx)\n",
			device_str, priv_target->handle,
		    device_str, (unsigned long long)priv_target->sas_address);
3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214

	} else if (priv_target->flags & MPT_TARGET_FLAGS_PCIE_DEVICE) {
		spin_lock_irqsave(&ioc->pcie_device_lock, flags);
		pcie_device = __mpt3sas_get_pdev_from_target(ioc, priv_target);
		if (pcie_device) {
			starget_printk(KERN_INFO, starget,
				"handle(0x%04x), wwid(0x%016llx), port(%d)\n",
				pcie_device->handle,
				(unsigned long long)pcie_device->wwid,
				pcie_device->port_num);
			if (pcie_device->enclosure_handle != 0)
				starget_printk(KERN_INFO, starget,
					"enclosure logical id(0x%016llx), slot(%d)\n",
					(unsigned long long)
					pcie_device->enclosure_logical_id,
					pcie_device->slot);
			if (pcie_device->connector_name[0] != '\0')
				starget_printk(KERN_INFO, starget,
					"enclosure level(0x%04x), connector name( %s)\n",
					pcie_device->enclosure_level,
					pcie_device->connector_name);
			pcie_device_put(pcie_device);
		}
		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);

3215 3216
	} else {
		spin_lock_irqsave(&ioc->sas_device_lock, flags);
3217
		sas_device = __mpt3sas_get_sdev_from_target(ioc, priv_target);
3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231
		if (sas_device) {
			if (priv_target->flags &
			    MPT_TARGET_FLAGS_RAID_COMPONENT) {
				starget_printk(KERN_INFO, starget,
				    "volume handle(0x%04x), "
				    "volume wwid(0x%016llx)\n",
				    sas_device->volume_handle,
				   (unsigned long long)sas_device->volume_wwid);
			}
			starget_printk(KERN_INFO, starget,
			    "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
			    sas_device->handle,
			    (unsigned long long)sas_device->sas_address,
			    sas_device->phy);
3232 3233 3234

			_scsih_display_enclosure_chassis_info(NULL, sas_device,
			    NULL, starget);
3235 3236

			sas_device_put(sas_device);
3237 3238 3239 3240 3241 3242
		}
		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
	}
}

/**
3243
 * scsih_abort - eh threads main abort routine
3244 3245
 * @scmd: pointer to scsi command object
 *
3246
 * Return: SUCCESS if command aborted else FAILED
3247
 */
3248
static int
3249
scsih_abort(struct scsi_cmnd *scmd)
3250 3251 3252
{
	struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
	struct MPT3SAS_DEVICE *sas_device_priv_data;
3253
	struct scsiio_tracker *st = scsi_cmd_priv(scmd);
3254 3255 3256
	u16 handle;
	int r;

3257 3258
	u8 timeout = 30;
	struct _pcie_device *pcie_device = NULL;
3259 3260 3261 3262
	sdev_printk(KERN_INFO, scmd->device, "attempting task abort!"
	    "scmd(0x%p), outstanding for %u ms & timeout %u ms\n",
	    scmd, jiffies_to_msecs(jiffies - scmd->jiffies_at_alloc),
	    (scmd->request->timeout / HZ) * 1000);
3263 3264 3265
	_scsih_tm_display_info(ioc, scmd);

	sas_device_priv_data = scmd->device->hostdata;
3266 3267
	if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
	    ioc->remove_host) {
3268
		sdev_printk(KERN_INFO, scmd->device,
3269
		    "device been deleted! scmd(0x%p)\n", scmd);
3270 3271 3272 3273 3274 3275
		scmd->result = DID_NO_CONNECT << 16;
		scmd->scsi_done(scmd);
		r = SUCCESS;
		goto out;
	}

3276 3277
	/* check for completed command */
	if (st == NULL || st->cb_idx == 0xFF) {
3278 3279
		sdev_printk(KERN_INFO, scmd->device, "No reference found at "
		    "driver, assuming scmd(0x%p) might have completed\n", scmd);
3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296
		scmd->result = DID_RESET << 16;
		r = SUCCESS;
		goto out;
	}

	/* for hidden raid components and volumes this is not supported */
	if (sas_device_priv_data->sas_target->flags &
	    MPT_TARGET_FLAGS_RAID_COMPONENT ||
	    sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
		scmd->result = DID_RESET << 16;
		r = FAILED;
		goto out;
	}

	mpt3sas_halt_firmware(ioc);

	handle = sas_device_priv_data->sas_target->handle;
3297
	pcie_device = mpt3sas_get_pdev_by_handle(ioc, handle);
3298 3299
	if (pcie_device && (!ioc->tm_custom_handling) &&
	    (!(mpt3sas_scsih_is_pcie_scsi_device(pcie_device->device_info))))
3300
		timeout = ioc->nvme_abort_timeout;
3301 3302
	r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
		scmd->device->id, scmd->device->lun,
3303
		MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
3304
		st->smid, st->msix_io, timeout, 0);
3305
	/* Command must be cleared after abort */
3306
	if (r == SUCCESS && st->cb_idx != 0xFF)
3307
		r = FAILED;
3308
 out:
3309
	sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(0x%p)\n",
3310
	    ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3311 3312
	if (pcie_device)
		pcie_device_put(pcie_device);
3313 3314 3315 3316
	return r;
}

/**
3317
 * scsih_dev_reset - eh threads main device reset routine
3318 3319
 * @scmd: pointer to scsi command object
 *
3320
 * Return: SUCCESS if command aborted else FAILED
3321
 */
3322
static int
3323
scsih_dev_reset(struct scsi_cmnd *scmd)
3324 3325 3326
{
	struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
	struct MPT3SAS_DEVICE *sas_device_priv_data;
3327
	struct _sas_device *sas_device = NULL;
3328
	struct _pcie_device *pcie_device = NULL;
3329
	u16	handle;
3330 3331
	u8	tr_method = 0;
	u8	tr_timeout = 30;
3332 3333
	int r;

3334 3335 3336
	struct scsi_target *starget = scmd->device->sdev_target;
	struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;

3337
	sdev_printk(KERN_INFO, scmd->device,
3338
	    "attempting device reset! scmd(0x%p)\n", scmd);
3339 3340 3341
	_scsih_tm_display_info(ioc, scmd);

	sas_device_priv_data = scmd->device->hostdata;
3342 3343
	if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
	    ioc->remove_host) {
3344
		sdev_printk(KERN_INFO, scmd->device,
3345
		    "device been deleted! scmd(0x%p)\n", scmd);
3346 3347 3348 3349 3350 3351 3352 3353 3354 3355
		scmd->result = DID_NO_CONNECT << 16;
		scmd->scsi_done(scmd);
		r = SUCCESS;
		goto out;
	}

	/* for hidden raid components obtain the volume_handle */
	handle = 0;
	if (sas_device_priv_data->sas_target->flags &
	    MPT_TARGET_FLAGS_RAID_COMPONENT) {
3356 3357
		sas_device = mpt3sas_get_sdev_from_target(ioc,
				target_priv_data);
3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368
		if (sas_device)
			handle = sas_device->volume_handle;
	} else
		handle = sas_device_priv_data->sas_target->handle;

	if (!handle) {
		scmd->result = DID_RESET << 16;
		r = FAILED;
		goto out;
	}

3369 3370
	pcie_device = mpt3sas_get_pdev_by_handle(ioc, handle);

3371 3372
	if (pcie_device && (!ioc->tm_custom_handling) &&
	    (!(mpt3sas_scsih_is_pcie_scsi_device(pcie_device->device_info)))) {
3373 3374 3375 3376
		tr_timeout = pcie_device->reset_timeout;
		tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
	} else
		tr_method = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
3377

3378 3379
	r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
		scmd->device->id, scmd->device->lun,
3380 3381
		MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 0,
		tr_timeout, tr_method);
3382 3383 3384
	/* Check for busy commands after reset */
	if (r == SUCCESS && atomic_read(&scmd->device->device_busy))
		r = FAILED;
3385
 out:
3386
	sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(0x%p)\n",
3387
	    ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3388 3389 3390

	if (sas_device)
		sas_device_put(sas_device);
3391 3392
	if (pcie_device)
		pcie_device_put(pcie_device);
3393

3394 3395 3396 3397
	return r;
}

/**
3398
 * scsih_target_reset - eh threads main target reset routine
3399 3400
 * @scmd: pointer to scsi command object
 *
3401
 * Return: SUCCESS if command aborted else FAILED
3402
 */
3403
static int
3404
scsih_target_reset(struct scsi_cmnd *scmd)
3405 3406 3407
{
	struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
	struct MPT3SAS_DEVICE *sas_device_priv_data;
3408
	struct _sas_device *sas_device = NULL;
3409
	struct _pcie_device *pcie_device = NULL;
3410
	u16	handle;
3411 3412
	u8	tr_method = 0;
	u8	tr_timeout = 30;
3413 3414
	int r;
	struct scsi_target *starget = scmd->device->sdev_target;
3415
	struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
3416

3417 3418
	starget_printk(KERN_INFO, starget,
	    "attempting target reset! scmd(0x%p)\n", scmd);
3419 3420 3421
	_scsih_tm_display_info(ioc, scmd);

	sas_device_priv_data = scmd->device->hostdata;
3422 3423
	if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
	    ioc->remove_host) {
3424 3425
		starget_printk(KERN_INFO, starget,
		    "target been deleted! scmd(0x%p)\n", scmd);
3426 3427 3428 3429 3430 3431 3432 3433 3434 3435
		scmd->result = DID_NO_CONNECT << 16;
		scmd->scsi_done(scmd);
		r = SUCCESS;
		goto out;
	}

	/* for hidden raid components obtain the volume_handle */
	handle = 0;
	if (sas_device_priv_data->sas_target->flags &
	    MPT_TARGET_FLAGS_RAID_COMPONENT) {
3436 3437
		sas_device = mpt3sas_get_sdev_from_target(ioc,
				target_priv_data);
3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448
		if (sas_device)
			handle = sas_device->volume_handle;
	} else
		handle = sas_device_priv_data->sas_target->handle;

	if (!handle) {
		scmd->result = DID_RESET << 16;
		r = FAILED;
		goto out;
	}

3449 3450
	pcie_device = mpt3sas_get_pdev_by_handle(ioc, handle);

3451 3452
	if (pcie_device && (!ioc->tm_custom_handling) &&
	    (!(mpt3sas_scsih_is_pcie_scsi_device(pcie_device->device_info)))) {
3453 3454 3455 3456
		tr_timeout = pcie_device->reset_timeout;
		tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
	} else
		tr_method = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
3457 3458
	r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
		scmd->device->id, 0,
3459 3460
		MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 0,
	    tr_timeout, tr_method);
3461 3462 3463
	/* Check for busy commands after reset */
	if (r == SUCCESS && atomic_read(&starget->target_busy))
		r = FAILED;
3464
 out:
3465
	starget_printk(KERN_INFO, starget, "target reset: %s scmd(0x%p)\n",
3466
	    ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3467 3468 3469

	if (sas_device)
		sas_device_put(sas_device);
3470 3471
	if (pcie_device)
		pcie_device_put(pcie_device);
3472 3473 3474 3475 3476
	return r;
}


/**
3477
 * scsih_host_reset - eh threads main host reset routine
3478 3479
 * @scmd: pointer to scsi command object
 *
3480
 * Return: SUCCESS if command aborted else FAILED
3481
 */
3482
static int
3483
scsih_host_reset(struct scsi_cmnd *scmd)
3484 3485 3486 3487
{
	struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
	int r, retval;

3488
	ioc_info(ioc, "attempting host reset! scmd(0x%p)\n", scmd);
3489 3490
	scsi_print_command(scmd);

3491
	if (ioc->is_driver_loading || ioc->remove_host) {
3492
		ioc_info(ioc, "Blocking the host reset\n");
3493 3494 3495 3496
		r = FAILED;
		goto out;
	}

3497
	retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
3498
	r = (retval < 0) ? FAILED : SUCCESS;
3499
out:
3500
	ioc_info(ioc, "host reset: %s scmd(0x%p)\n",
3501
		 r == SUCCESS ? "SUCCESS" : "FAILED", scmd);
3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523

	return r;
}

/**
 * _scsih_fw_event_add - insert and queue up fw_event
 * @ioc: per adapter object
 * @fw_event: object describing the event
 * Context: This function will acquire ioc->fw_event_lock.
 *
 * This adds the firmware event object into link list, then queues it up to
 * be processed from user context.
 */
static void
_scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
{
	unsigned long flags;

	if (ioc->firmware_event_thread == NULL)
		return;

	spin_lock_irqsave(&ioc->fw_event_lock, flags);
3524
	fw_event_work_get(fw_event);
3525 3526 3527
	INIT_LIST_HEAD(&fw_event->list);
	list_add_tail(&fw_event->list, &ioc->fw_event_list);
	INIT_WORK(&fw_event->work, _firmware_event_work);
3528
	fw_event_work_get(fw_event);
3529 3530 3531 3532 3533
	queue_work(ioc->firmware_event_thread, &fw_event->work);
	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
}

/**
3534
 * _scsih_fw_event_del_from_list - delete fw_event from the list
3535 3536 3537 3538
 * @ioc: per adapter object
 * @fw_event: object describing the event
 * Context: This function will acquire ioc->fw_event_lock.
 *
3539
 * If the fw_event is on the fw_event_list, remove it and do a put.
3540 3541
 */
static void
3542
_scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
3543 3544 3545 3546 3547
	*fw_event)
{
	unsigned long flags;

	spin_lock_irqsave(&ioc->fw_event_lock, flags);
3548 3549 3550 3551
	if (!list_empty(&fw_event->list)) {
		list_del_init(&fw_event->list);
		fw_event_work_put(fw_event);
	}
3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565
	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
}


 /**
 * mpt3sas_send_trigger_data_event - send event for processing trigger data
 * @ioc: per adapter object
 * @event_data: trigger event data
 */
void
mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
	struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
{
	struct fw_event_work *fw_event;
3566
	u16 sz;
3567 3568 3569

	if (ioc->is_driver_loading)
		return;
3570 3571
	sz = sizeof(*event_data);
	fw_event = alloc_fw_event_work(sz);
3572 3573 3574 3575 3576 3577
	if (!fw_event)
		return;
	fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
	fw_event->ioc = ioc;
	memcpy(fw_event->event_data, event_data, sizeof(*event_data));
	_scsih_fw_event_add(ioc, fw_event);
3578
	fw_event_work_put(fw_event);
3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591
}

/**
 * _scsih_error_recovery_delete_devices - remove devices not responding
 * @ioc: per adapter object
 */
static void
_scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
{
	struct fw_event_work *fw_event;

	if (ioc->is_driver_loading)
		return;
3592
	fw_event = alloc_fw_event_work(0);
3593 3594 3595 3596 3597
	if (!fw_event)
		return;
	fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
	fw_event->ioc = ioc;
	_scsih_fw_event_add(ioc, fw_event);
3598
	fw_event_work_put(fw_event);
3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609
}

/**
 * mpt3sas_port_enable_complete - port enable completed (fake event)
 * @ioc: per adapter object
 */
void
mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
{
	struct fw_event_work *fw_event;

3610
	fw_event = alloc_fw_event_work(0);
3611 3612 3613 3614 3615
	if (!fw_event)
		return;
	fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
	fw_event->ioc = ioc;
	_scsih_fw_event_add(ioc, fw_event);
3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632
	fw_event_work_put(fw_event);
}

static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc)
{
	unsigned long flags;
	struct fw_event_work *fw_event = NULL;

	spin_lock_irqsave(&ioc->fw_event_lock, flags);
	if (!list_empty(&ioc->fw_event_list)) {
		fw_event = list_first_entry(&ioc->fw_event_list,
				struct fw_event_work, list);
		list_del_init(&fw_event->list);
	}
	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);

	return fw_event;
3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644
}

/**
 * _scsih_fw_event_cleanup_queue - cleanup event queue
 * @ioc: per adapter object
 *
 * Walk the firmware event queue, either killing timers, or waiting
 * for outstanding events to complete
 */
static void
_scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
{
3645
	struct fw_event_work *fw_event;
3646

3647
	if ((list_empty(&ioc->fw_event_list) && !ioc->current_event) ||
3648 3649 3650
	     !ioc->firmware_event_thread || in_interrupt())
		return;

3651 3652 3653
	ioc->fw_events_cleanup = 1;
	while ((fw_event = dequeue_next_fw_event(ioc)) ||
	     (fw_event = ioc->current_event)) {
3654 3655 3656
		/*
		 * Wait on the fw_event to complete. If this returns 1, then
		 * the event was never executed, and we need a put for the
3657
		 * reference the work had on the fw_event.
3658 3659 3660 3661
		 *
		 * If it did execute, we wait for it to finish, and the put will
		 * happen from _firmware_event_work()
		 */
3662
		if (cancel_work_sync(&fw_event->work))
3663 3664 3665
			fw_event_work_put(fw_event);

		fw_event_work_put(fw_event);
3666
	}
3667
	ioc->fw_events_cleanup = 0;
3668 3669
}

3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687
/**
 * _scsih_internal_device_block - block the sdev device
 * @sdev: per device object
 * @sas_device_priv_data : per device driver private data
 *
 * make sure device is blocked without error, if not
 * print an error
 */
static void
_scsih_internal_device_block(struct scsi_device *sdev,
			struct MPT3SAS_DEVICE *sas_device_priv_data)
{
	int r = 0;

	sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
	    sas_device_priv_data->sas_target->handle);
	sas_device_priv_data->block = 1;

3688
	r = scsi_internal_device_block_nowait(sdev);
3689 3690 3691
	if (r == -EINVAL)
		sdev_printk(KERN_WARNING, sdev,
		    "device_block failed with return(%d) for handle(0x%04x)\n",
3692
		    r, sas_device_priv_data->sas_target->handle);
3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711
}

/**
 * _scsih_internal_device_unblock - unblock the sdev device
 * @sdev: per device object
 * @sas_device_priv_data : per device driver private data
 * make sure device is unblocked without error, if not retry
 * by blocking and then unblocking
 */

static void
_scsih_internal_device_unblock(struct scsi_device *sdev,
			struct MPT3SAS_DEVICE *sas_device_priv_data)
{
	int r = 0;

	sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
	    "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
	sas_device_priv_data->block = 0;
3712
	r = scsi_internal_device_unblock_nowait(sdev, SDEV_RUNNING);
3713 3714 3715 3716 3717 3718 3719 3720 3721
	if (r == -EINVAL) {
		/* The device has been set to SDEV_RUNNING by SD layer during
		 * device addition but the request queue is still stopped by
		 * our earlier block call. We need to perform a block again
		 * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */

		sdev_printk(KERN_WARNING, sdev,
		    "device_unblock failed with return(%d) for handle(0x%04x) "
		    "performing a block followed by an unblock\n",
3722
		    r, sas_device_priv_data->sas_target->handle);
3723
		sas_device_priv_data->block = 1;
3724
		r = scsi_internal_device_block_nowait(sdev);
3725 3726 3727
		if (r)
			sdev_printk(KERN_WARNING, sdev, "retried device_block "
			    "failed with return(%d) for handle(0x%04x)\n",
3728
			    r, sas_device_priv_data->sas_target->handle);
3729 3730

		sas_device_priv_data->block = 0;
3731
		r = scsi_internal_device_unblock_nowait(sdev, SDEV_RUNNING);
3732 3733 3734
		if (r)
			sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
			    " failed with return(%d) for handle(0x%04x)\n",
3735
			    r, sas_device_priv_data->sas_target->handle);
3736 3737 3738
	}
}

3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760
/**
 * _scsih_ublock_io_all_device - unblock every device
 * @ioc: per adapter object
 *
 * change the device state from block to running
 */
static void
_scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
{
	struct MPT3SAS_DEVICE *sas_device_priv_data;
	struct scsi_device *sdev;

	shost_for_each_device(sdev, ioc->shost) {
		sas_device_priv_data = sdev->hostdata;
		if (!sas_device_priv_data)
			continue;
		if (!sas_device_priv_data->block)
			continue;

		dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
			"device_running, handle(0x%04x)\n",
		    sas_device_priv_data->sas_target->handle));
3761
		_scsih_internal_device_unblock(sdev, sas_device_priv_data);
3762 3763 3764 3765 3766 3767 3768
	}
}


/**
 * _scsih_ublock_io_device - prepare device to be deleted
 * @ioc: per adapter object
3769
 * @sas_address: sas address
3770
 * @port: hba port entry
3771 3772 3773 3774
 *
 * unblock then put device in offline state
 */
static void
3775 3776
_scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc,
	u64 sas_address, struct hba_port *port)
3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787
{
	struct MPT3SAS_DEVICE *sas_device_priv_data;
	struct scsi_device *sdev;

	shost_for_each_device(sdev, ioc->shost) {
		sas_device_priv_data = sdev->hostdata;
		if (!sas_device_priv_data)
			continue;
		if (sas_device_priv_data->sas_target->sas_address
		    != sas_address)
			continue;
3788 3789
		if (sas_device_priv_data->sas_target->port != port)
			continue;
3790 3791 3792
		if (sas_device_priv_data->block)
			_scsih_internal_device_unblock(sdev,
				sas_device_priv_data);
3793 3794 3795 3796 3797 3798 3799
	}
}

/**
 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
 * @ioc: per adapter object
 *
3800
 * During device pull we need to appropriately set the sdev state.
3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813
 */
static void
_scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
{
	struct MPT3SAS_DEVICE *sas_device_priv_data;
	struct scsi_device *sdev;

	shost_for_each_device(sdev, ioc->shost) {
		sas_device_priv_data = sdev->hostdata;
		if (!sas_device_priv_data)
			continue;
		if (sas_device_priv_data->block)
			continue;
3814 3815 3816 3817 3818 3819
		if (sas_device_priv_data->ignore_delay_remove) {
			sdev_printk(KERN_INFO, sdev,
			"%s skip device_block for SES handle(0x%04x)\n",
			__func__, sas_device_priv_data->sas_target->handle);
			continue;
		}
3820
		_scsih_internal_device_block(sdev, sas_device_priv_data);
3821 3822 3823 3824 3825 3826 3827 3828
	}
}

/**
 * _scsih_block_io_device - set the device state to SDEV_BLOCK
 * @ioc: per adapter object
 * @handle: device handle
 *
3829
 * During device pull we need to appropriately set the sdev state.
3830 3831 3832 3833 3834 3835
 */
static void
_scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	struct MPT3SAS_DEVICE *sas_device_priv_data;
	struct scsi_device *sdev;
3836 3837
	struct _sas_device *sas_device;

3838
	sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
3839 3840 3841 3842 3843 3844 3845 3846 3847

	shost_for_each_device(sdev, ioc->shost) {
		sas_device_priv_data = sdev->hostdata;
		if (!sas_device_priv_data)
			continue;
		if (sas_device_priv_data->sas_target->handle != handle)
			continue;
		if (sas_device_priv_data->block)
			continue;
3848
		if (sas_device && sas_device->pend_sas_rphy_add)
3849
			continue;
3850 3851 3852 3853 3854 3855
		if (sas_device_priv_data->ignore_delay_remove) {
			sdev_printk(KERN_INFO, sdev,
			"%s skip device_block for SES handle(0x%04x)\n",
			__func__, sas_device_priv_data->sas_target->handle);
			continue;
		}
3856
		_scsih_internal_device_block(sdev, sas_device_priv_data);
3857
	}
3858

3859 3860
	if (sas_device)
		sas_device_put(sas_device);
3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888
}

/**
 * _scsih_block_io_to_children_attached_to_ex
 * @ioc: per adapter object
 * @sas_expander: the sas_device object
 *
 * This routine set sdev state to SDEV_BLOCK for all devices
 * attached to this expander. This function called when expander is
 * pulled.
 */
static void
_scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
	struct _sas_node *sas_expander)
{
	struct _sas_port *mpt3sas_port;
	struct _sas_device *sas_device;
	struct _sas_node *expander_sibling;
	unsigned long flags;

	if (!sas_expander)
		return;

	list_for_each_entry(mpt3sas_port,
	   &sas_expander->sas_port_list, port_list) {
		if (mpt3sas_port->remote_identify.device_type ==
		    SAS_END_DEVICE) {
			spin_lock_irqsave(&ioc->sas_device_lock, flags);
3889
			sas_device = __mpt3sas_get_sdev_by_addr(ioc,
3890 3891
			    mpt3sas_port->remote_identify.sas_address,
			    mpt3sas_port->hba_port);
3892
			if (sas_device) {
3893
				set_bit(sas_device->handle,
3894 3895 3896
						ioc->blocking_handles);
				sas_device_put(sas_device);
			}
3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909
			spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
		}
	}

	list_for_each_entry(mpt3sas_port,
	   &sas_expander->sas_port_list, port_list) {

		if (mpt3sas_port->remote_identify.device_type ==
		    SAS_EDGE_EXPANDER_DEVICE ||
		    mpt3sas_port->remote_identify.device_type ==
		    SAS_FANOUT_EXPANDER_DEVICE) {
			expander_sibling =
			    mpt3sas_scsih_expander_find_by_sas_address(
3910 3911
			    ioc, mpt3sas_port->remote_identify.sas_address,
			    mpt3sas_port->hba_port);
3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944
			_scsih_block_io_to_children_attached_to_ex(ioc,
			    expander_sibling);
		}
	}
}

/**
 * _scsih_block_io_to_children_attached_directly
 * @ioc: per adapter object
 * @event_data: topology change event data
 *
 * This routine set sdev state to SDEV_BLOCK for all devices
 * direct attached during device pull.
 */
static void
_scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
	Mpi2EventDataSasTopologyChangeList_t *event_data)
{
	int i;
	u16 handle;
	u16 reason_code;

	for (i = 0; i < event_data->NumEntries; i++) {
		handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
		if (!handle)
			continue;
		reason_code = event_data->PHY[i].PhyStatus &
		    MPI2_EVENT_SAS_TOPO_RC_MASK;
		if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
			_scsih_block_io_device(ioc, handle);
	}
}

3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971
/**
 * _scsih_block_io_to_pcie_children_attached_directly
 * @ioc: per adapter object
 * @event_data: topology change event data
 *
 * This routine set sdev state to SDEV_BLOCK for all devices
 * direct attached during device pull/reconnect.
 */
static void
_scsih_block_io_to_pcie_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
		Mpi26EventDataPCIeTopologyChangeList_t *event_data)
{
	int i;
	u16 handle;
	u16 reason_code;

	for (i = 0; i < event_data->NumEntries; i++) {
		handle =
			le16_to_cpu(event_data->PortEntry[i].AttachedDevHandle);
		if (!handle)
			continue;
		reason_code = event_data->PortEntry[i].PortStatus;
		if (reason_code ==
				MPI26_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING)
			_scsih_block_io_device(ioc, handle);
	}
}
3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991
/**
 * _scsih_tm_tr_send - send task management request
 * @ioc: per adapter object
 * @handle: device handle
 * Context: interrupt time.
 *
 * This code is to initiate the device removal handshake protocol
 * with controller firmware.  This function will issue target reset
 * using high priority request queue.  It will send a sas iounit
 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
 *
 * This is designed to send muliple task management request at the same
 * time to the fifo. If the fifo is full, we will append the request,
 * and process it in a future completion.
 */
static void
_scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	Mpi2SCSITaskManagementRequest_t *mpi_request;
	u16 smid;
3992
	struct _sas_device *sas_device = NULL;
3993
	struct _pcie_device *pcie_device = NULL;
3994 3995 3996 3997 3998
	struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
	u64 sas_address = 0;
	unsigned long flags;
	struct _tr_list *delayed_tr;
	u32 ioc_state;
3999
	u8 tr_method = 0;
4000
	struct hba_port *port = NULL;
4001

4002
	if (ioc->pci_error_recovery) {
4003 4004 4005
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: host in pci error recovery: handle(0x%04x)\n",
				    __func__, handle));
4006 4007 4008 4009
		return;
	}
	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
4010 4011 4012
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: host is not operational: handle(0x%04x)\n",
				    __func__, handle));
4013 4014 4015 4016 4017 4018 4019
		return;
	}

	/* if PD, then return */
	if (test_bit(handle, ioc->pd_handles))
		return;

4020 4021
	clear_bit(handle, ioc->pend_os_device_add);

4022
	spin_lock_irqsave(&ioc->sas_device_lock, flags);
4023
	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
4024 4025 4026 4027 4028
	if (sas_device && sas_device->starget &&
	    sas_device->starget->hostdata) {
		sas_target_priv_data = sas_device->starget->hostdata;
		sas_target_priv_data->deleted = 1;
		sas_address = sas_device->sas_address;
4029
		port = sas_device->port;
4030 4031
	}
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4032 4033 4034 4035 4036 4037 4038 4039 4040 4041
	if (!sas_device) {
		spin_lock_irqsave(&ioc->pcie_device_lock, flags);
		pcie_device = __mpt3sas_get_pdev_by_handle(ioc, handle);
		if (pcie_device && pcie_device->starget &&
			pcie_device->starget->hostdata) {
			sas_target_priv_data = pcie_device->starget->hostdata;
			sas_target_priv_data->deleted = 1;
			sas_address = pcie_device->wwid;
		}
		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
4042 4043 4044
		if (pcie_device && (!ioc->tm_custom_handling) &&
		    (!(mpt3sas_scsih_is_pcie_scsi_device(
		    pcie_device->device_info))))
4045 4046 4047 4048
			tr_method =
			    MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
		else
			tr_method = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
4049
	}
4050
	if (sas_target_priv_data) {
4051 4052 4053
		dewtprintk(ioc,
			   ioc_info(ioc, "setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
				    handle, (u64)sas_address));
4054 4055
		if (sas_device) {
			if (sas_device->enclosure_handle != 0)
4056 4057 4058 4059
				dewtprintk(ioc,
					   ioc_info(ioc, "setting delete flag:enclosure logical id(0x%016llx), slot(%d)\n",
						    (u64)sas_device->enclosure_logical_id,
						    sas_device->slot));
4060
			if (sas_device->connector_name[0] != '\0')
4061 4062 4063 4064
				dewtprintk(ioc,
					   ioc_info(ioc, "setting delete flag: enclosure level(0x%04x), connector name( %s)\n",
						    sas_device->enclosure_level,
						    sas_device->connector_name));
4065 4066
		} else if (pcie_device) {
			if (pcie_device->enclosure_handle != 0)
4067 4068 4069 4070
				dewtprintk(ioc,
					   ioc_info(ioc, "setting delete flag: logical id(0x%016llx), slot(%d)\n",
						    (u64)pcie_device->enclosure_logical_id,
						    pcie_device->slot));
4071
			if (pcie_device->connector_name[0] != '\0')
4072 4073 4074 4075
				dewtprintk(ioc,
					   ioc_info(ioc, "setting delete flag:, enclosure level(0x%04x), connector name( %s)\n",
						    pcie_device->enclosure_level,
						    pcie_device->connector_name));
4076
		}
4077
		_scsih_ublock_io_device(ioc, sas_address, port);
4078 4079 4080 4081 4082 4083 4084
		sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
	}

	smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
	if (!smid) {
		delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
		if (!delayed_tr)
4085
			goto out;
4086 4087 4088
		INIT_LIST_HEAD(&delayed_tr->list);
		delayed_tr->handle = handle;
		list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
4089 4090 4091
		dewtprintk(ioc,
			   ioc_info(ioc, "DELAYED:tr:handle(0x%04x), (open)\n",
				    handle));
4092
		goto out;
4093 4094
	}

4095 4096 4097
	dewtprintk(ioc,
		   ioc_info(ioc, "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
			    handle, smid, ioc->tm_tr_cb_idx));
4098 4099 4100 4101 4102
	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
	memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
	mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
	mpi_request->DevHandle = cpu_to_le16(handle);
	mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
4103
	mpi_request->MsgFlags = tr_method;
4104
	set_bit(handle, ioc->device_remove_in_progress);
4105
	ioc->put_smid_hi_priority(ioc, smid, 0);
4106
	mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
4107 4108 4109 4110

out:
	if (sas_device)
		sas_device_put(sas_device);
4111 4112
	if (pcie_device)
		pcie_device_put(pcie_device);
4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127
}

/**
 * _scsih_tm_tr_complete -
 * @ioc: per adapter object
 * @smid: system request message index
 * @msix_index: MSIX table index supplied by the OS
 * @reply: reply message frame(lower 32bit addr)
 * Context: interrupt time.
 *
 * This is the target reset completion routine.
 * This code is part of the code to initiate the device removal
 * handshake protocol with controller firmware.
 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
 *
4128 4129
 * Return: 1 meaning mf should be freed from _base_interrupt
 *         0 means the mf is freed from this function.
4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141
 */
static u8
_scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
	u32 reply)
{
	u16 handle;
	Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
	Mpi2SCSITaskManagementReply_t *mpi_reply =
	    mpt3sas_base_get_reply_virt_addr(ioc, reply);
	Mpi2SasIoUnitControlRequest_t *mpi_request;
	u16 smid_sas_ctrl;
	u32 ioc_state;
4142
	struct _sc_list *delayed_sc;
4143

4144
	if (ioc->pci_error_recovery) {
4145 4146 4147
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: host in pci error recovery\n",
				    __func__));
4148 4149 4150 4151
		return 1;
	}
	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
4152 4153 4154
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: host is not operational\n",
				    __func__));
4155 4156 4157
		return 1;
	}
	if (unlikely(!mpi_reply)) {
4158 4159
		ioc_err(ioc, "mpi_reply not valid at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
4160 4161 4162 4163 4164
		return 1;
	}
	mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
	handle = le16_to_cpu(mpi_request_tm->DevHandle);
	if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
4165 4166 4167 4168
		dewtprintk(ioc,
			   ioc_err(ioc, "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
				   handle,
				   le16_to_cpu(mpi_reply->DevHandle), smid));
4169 4170 4171 4172
		return 0;
	}

	mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
4173 4174 4175 4176 4177
	dewtprintk(ioc,
		   ioc_info(ioc, "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), loginfo(0x%08x), completed(%d)\n",
			    handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
			    le32_to_cpu(mpi_reply->IOCLogInfo),
			    le32_to_cpu(mpi_reply->TerminationCount)));
4178 4179 4180

	smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
	if (!smid_sas_ctrl) {
4181 4182 4183 4184
		delayed_sc = kzalloc(sizeof(*delayed_sc), GFP_ATOMIC);
		if (!delayed_sc)
			return _scsih_check_for_pending_tm(ioc, smid);
		INIT_LIST_HEAD(&delayed_sc->list);
4185
		delayed_sc->handle = le16_to_cpu(mpi_request_tm->DevHandle);
4186
		list_add_tail(&delayed_sc->list, &ioc->delayed_sc_list);
4187 4188 4189
		dewtprintk(ioc,
			   ioc_info(ioc, "DELAYED:sc:handle(0x%04x), (open)\n",
				    handle));
4190
		return _scsih_check_for_pending_tm(ioc, smid);
4191 4192
	}

4193 4194 4195
	dewtprintk(ioc,
		   ioc_info(ioc, "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
			    handle, smid_sas_ctrl, ioc->tm_sas_control_cb_idx));
4196 4197 4198 4199 4200
	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
	memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
	mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
	mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
	mpi_request->DevHandle = mpi_request_tm->DevHandle;
4201
	ioc->put_smid_default(ioc, smid_sas_ctrl);
4202 4203 4204 4205

	return _scsih_check_for_pending_tm(ioc, smid);
}

4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239
/** _scsih_allow_scmd_to_device - check whether scmd needs to
 *				 issue to IOC or not.
 * @ioc: per adapter object
 * @scmd: pointer to scsi command object
 *
 * Returns true if scmd can be issued to IOC otherwise returns false.
 */
inline bool _scsih_allow_scmd_to_device(struct MPT3SAS_ADAPTER *ioc,
	struct scsi_cmnd *scmd)
{

	if (ioc->pci_error_recovery)
		return false;

	if (ioc->hba_mpi_version_belonged == MPI2_VERSION) {
		if (ioc->remove_host)
			return false;

		return true;
	}

	if (ioc->remove_host) {

		switch (scmd->cmnd[0]) {
		case SYNCHRONIZE_CACHE:
		case START_STOP:
			return true;
		default:
			return false;
		}
	}

	return true;
}
4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252

/**
 * _scsih_sas_control_complete - completion routine
 * @ioc: per adapter object
 * @smid: system request message index
 * @msix_index: MSIX table index supplied by the OS
 * @reply: reply message frame(lower 32bit addr)
 * Context: interrupt time.
 *
 * This is the sas iounit control completion routine.
 * This code is part of the code to initiate the device removal
 * handshake protocol with controller firmware.
 *
4253 4254
 * Return: 1 meaning mf should be freed from _base_interrupt
 *         0 means the mf is freed from this function.
4255 4256 4257 4258 4259 4260 4261 4262 4263
 */
static u8
_scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
	u8 msix_index, u32 reply)
{
	Mpi2SasIoUnitControlReply_t *mpi_reply =
	    mpt3sas_base_get_reply_virt_addr(ioc, reply);

	if (likely(mpi_reply)) {
4264 4265 4266 4267 4268
		dewtprintk(ioc,
			   ioc_info(ioc, "sc_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
				    le16_to_cpu(mpi_reply->DevHandle), smid,
				    le16_to_cpu(mpi_reply->IOCStatus),
				    le32_to_cpu(mpi_reply->IOCLogInfo)));
4269 4270 4271 4272 4273
		if (le16_to_cpu(mpi_reply->IOCStatus) ==
		     MPI2_IOCSTATUS_SUCCESS) {
			clear_bit(le16_to_cpu(mpi_reply->DevHandle),
			    ioc->device_remove_in_progress);
		}
4274
	} else {
4275 4276
		ioc_err(ioc, "mpi_reply not valid at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
4277
	}
4278
	return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297
}

/**
 * _scsih_tm_tr_volume_send - send target reset request for volumes
 * @ioc: per adapter object
 * @handle: device handle
 * Context: interrupt time.
 *
 * This is designed to send muliple task management request at the same
 * time to the fifo. If the fifo is full, we will append the request,
 * and process it in a future completion.
 */
static void
_scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	Mpi2SCSITaskManagementRequest_t *mpi_request;
	u16 smid;
	struct _tr_list *delayed_tr;

4298
	if (ioc->pci_error_recovery) {
4299 4300 4301
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: host reset in progress!\n",
				    __func__));
4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312
		return;
	}

	smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
	if (!smid) {
		delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
		if (!delayed_tr)
			return;
		INIT_LIST_HEAD(&delayed_tr->list);
		delayed_tr->handle = handle;
		list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
4313 4314 4315
		dewtprintk(ioc,
			   ioc_info(ioc, "DELAYED:tr:handle(0x%04x), (open)\n",
				    handle));
4316 4317 4318
		return;
	}

4319 4320 4321
	dewtprintk(ioc,
		   ioc_info(ioc, "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
			    handle, smid, ioc->tm_tr_volume_cb_idx));
4322 4323 4324 4325 4326
	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
	memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
	mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
	mpi_request->DevHandle = cpu_to_le16(handle);
	mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
4327
	ioc->put_smid_hi_priority(ioc, smid, 0);
4328 4329 4330 4331 4332 4333 4334 4335 4336 4337
}

/**
 * _scsih_tm_volume_tr_complete - target reset completion
 * @ioc: per adapter object
 * @smid: system request message index
 * @msix_index: MSIX table index supplied by the OS
 * @reply: reply message frame(lower 32bit addr)
 * Context: interrupt time.
 *
4338 4339
 * Return: 1 meaning mf should be freed from _base_interrupt
 *         0 means the mf is freed from this function.
4340 4341 4342 4343 4344 4345 4346 4347 4348 4349
 */
static u8
_scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
	u8 msix_index, u32 reply)
{
	u16 handle;
	Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
	Mpi2SCSITaskManagementReply_t *mpi_reply =
	    mpt3sas_base_get_reply_virt_addr(ioc, reply);

4350
	if (ioc->shost_recovery || ioc->pci_error_recovery) {
4351 4352 4353
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: host reset in progress!\n",
				    __func__));
4354 4355 4356
		return 1;
	}
	if (unlikely(!mpi_reply)) {
4357 4358
		ioc_err(ioc, "mpi_reply not valid at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
4359 4360 4361 4362 4363 4364
		return 1;
	}

	mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
	handle = le16_to_cpu(mpi_request_tm->DevHandle);
	if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
4365 4366 4367 4368
		dewtprintk(ioc,
			   ioc_err(ioc, "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
				   handle, le16_to_cpu(mpi_reply->DevHandle),
				   smid));
4369 4370 4371
		return 0;
	}

4372 4373 4374 4375 4376
	dewtprintk(ioc,
		   ioc_info(ioc, "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), loginfo(0x%08x), completed(%d)\n",
			    handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
			    le32_to_cpu(mpi_reply->IOCLogInfo),
			    le32_to_cpu(mpi_reply->TerminationCount)));
4377 4378 4379 4380

	return _scsih_check_for_pending_tm(ioc, smid);
}

4381 4382 4383 4384 4385 4386 4387 4388 4389
/**
 * _scsih_issue_delayed_event_ack - issue delayed Event ACK messages
 * @ioc: per adapter object
 * @smid: system request message index
 * @event: Event ID
 * @event_context: used to track events uniquely
 *
 * Context - processed in interrupt context.
 */
4390
static void
4391 4392
_scsih_issue_delayed_event_ack(struct MPT3SAS_ADAPTER *ioc, u16 smid, U16 event,
				U32 event_context)
4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405
{
	Mpi2EventAckRequest_t *ack_request;
	int i = smid - ioc->internal_smid;
	unsigned long flags;

	/* Without releasing the smid just update the
	 * call back index and reuse the same smid for
	 * processing this delayed request
	 */
	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
	ioc->internal_lookup[i].cb_idx = ioc->base_cb_idx;
	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);

4406 4407 4408
	dewtprintk(ioc,
		   ioc_info(ioc, "EVENT ACK: event(0x%04x), smid(%d), cb(%d)\n",
			    le16_to_cpu(event), smid, ioc->base_cb_idx));
4409 4410 4411 4412 4413 4414 4415
	ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
	memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
	ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
	ack_request->Event = event;
	ack_request->EventContext = event_context;
	ack_request->VF_ID = 0;  /* TODO */
	ack_request->VP_ID = 0;
4416
	ioc->put_smid_default(ioc, smid);
4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427
}

/**
 * _scsih_issue_delayed_sas_io_unit_ctrl - issue delayed
 *				sas_io_unit_ctrl messages
 * @ioc: per adapter object
 * @smid: system request message index
 * @handle: device handle
 *
 * Context - processed in interrupt context.
 */
4428
static void
4429 4430
_scsih_issue_delayed_sas_io_unit_ctrl(struct MPT3SAS_ADAPTER *ioc,
					u16 smid, u16 handle)
B
Bart Van Assche 已提交
4431 4432 4433 4434 4435
{
	Mpi2SasIoUnitControlRequest_t *mpi_request;
	u32 ioc_state;
	int i = smid - ioc->internal_smid;
	unsigned long flags;
4436

B
Bart Van Assche 已提交
4437
	if (ioc->remove_host) {
4438 4439 4440
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: host has been removed\n",
				    __func__));
B
Bart Van Assche 已提交
4441 4442
		return;
	} else if (ioc->pci_error_recovery) {
4443 4444 4445
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: host in pci error recovery\n",
				    __func__));
4446 4447 4448 4449
		return;
	}
	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
4450 4451 4452
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: host is not operational\n",
				    __func__));
4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463
		return;
	}

	/* Without releasing the smid just update the
	 * call back index and reuse the same smid for
	 * processing this delayed request
	 */
	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
	ioc->internal_lookup[i].cb_idx = ioc->tm_sas_control_cb_idx;
	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);

4464 4465 4466
	dewtprintk(ioc,
		   ioc_info(ioc, "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
			    handle, smid, ioc->tm_sas_control_cb_idx));
4467 4468 4469 4470
	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
	memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
	mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
	mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
4471
	mpi_request->DevHandle = cpu_to_le16(handle);
4472
	ioc->put_smid_default(ioc, smid);
4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484
}

/**
 * _scsih_check_for_pending_internal_cmds - check for pending internal messages
 * @ioc: per adapter object
 * @smid: system request message index
 *
 * Context: Executed in interrupt context
 *
 * This will check delayed internal messages list, and process the
 * next request.
 *
4485 4486
 * Return: 1 meaning mf should be freed from _base_interrupt
 *         0 means the mf is freed from this function.
4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514
 */
u8
mpt3sas_check_for_pending_internal_cmds(struct MPT3SAS_ADAPTER *ioc, u16 smid)
{
	struct _sc_list *delayed_sc;
	struct _event_ack_list *delayed_event_ack;

	if (!list_empty(&ioc->delayed_event_ack_list)) {
		delayed_event_ack = list_entry(ioc->delayed_event_ack_list.next,
						struct _event_ack_list, list);
		_scsih_issue_delayed_event_ack(ioc, smid,
		  delayed_event_ack->Event, delayed_event_ack->EventContext);
		list_del(&delayed_event_ack->list);
		kfree(delayed_event_ack);
		return 0;
	}

	if (!list_empty(&ioc->delayed_sc_list)) {
		delayed_sc = list_entry(ioc->delayed_sc_list.next,
						struct _sc_list, list);
		_scsih_issue_delayed_sas_io_unit_ctrl(ioc, smid,
						 delayed_sc->handle);
		list_del(&delayed_sc->list);
		kfree(delayed_sc);
		return 0;
	}
	return 1;
}
4515 4516 4517 4518 4519 4520 4521 4522 4523

/**
 * _scsih_check_for_pending_tm - check for pending task management
 * @ioc: per adapter object
 * @smid: system request message index
 *
 * This will check delayed target reset list, and feed the
 * next reqeust.
 *
4524 4525
 * Return: 1 meaning mf should be freed from _base_interrupt
 *         0 means the mf is freed from this function.
4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618
 */
static u8
_scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
{
	struct _tr_list *delayed_tr;

	if (!list_empty(&ioc->delayed_tr_volume_list)) {
		delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
		    struct _tr_list, list);
		mpt3sas_base_free_smid(ioc, smid);
		_scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
		list_del(&delayed_tr->list);
		kfree(delayed_tr);
		return 0;
	}

	if (!list_empty(&ioc->delayed_tr_list)) {
		delayed_tr = list_entry(ioc->delayed_tr_list.next,
		    struct _tr_list, list);
		mpt3sas_base_free_smid(ioc, smid);
		_scsih_tm_tr_send(ioc, delayed_tr->handle);
		list_del(&delayed_tr->list);
		kfree(delayed_tr);
		return 0;
	}

	return 1;
}

/**
 * _scsih_check_topo_delete_events - sanity check on topo events
 * @ioc: per adapter object
 * @event_data: the event data payload
 *
 * This routine added to better handle cable breaker.
 *
 * This handles the case where driver receives multiple expander
 * add and delete events in a single shot.  When there is a delete event
 * the routine will void any pending add events waiting in the event queue.
 */
static void
_scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
	Mpi2EventDataSasTopologyChangeList_t *event_data)
{
	struct fw_event_work *fw_event;
	Mpi2EventDataSasTopologyChangeList_t *local_event_data;
	u16 expander_handle;
	struct _sas_node *sas_expander;
	unsigned long flags;
	int i, reason_code;
	u16 handle;

	for (i = 0 ; i < event_data->NumEntries; i++) {
		handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
		if (!handle)
			continue;
		reason_code = event_data->PHY[i].PhyStatus &
		    MPI2_EVENT_SAS_TOPO_RC_MASK;
		if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
			_scsih_tm_tr_send(ioc, handle);
	}

	expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
	if (expander_handle < ioc->sas_hba.num_phys) {
		_scsih_block_io_to_children_attached_directly(ioc, event_data);
		return;
	}
	if (event_data->ExpStatus ==
	    MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
		/* put expander attached devices into blocking state */
		spin_lock_irqsave(&ioc->sas_node_lock, flags);
		sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
		    expander_handle);
		_scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
		do {
			handle = find_first_bit(ioc->blocking_handles,
			    ioc->facts.MaxDevHandle);
			if (handle < ioc->facts.MaxDevHandle)
				_scsih_block_io_device(ioc, handle);
		} while (test_and_clear_bit(handle, ioc->blocking_handles));
	} else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
		_scsih_block_io_to_children_attached_directly(ioc, event_data);

	if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
		return;

	/* mark ignore flag for pending events */
	spin_lock_irqsave(&ioc->fw_event_lock, flags);
	list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
		if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
		    fw_event->ignore)
			continue;
4619 4620
		local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
				   fw_event->event_data;
4621 4622 4623 4624 4625 4626
		if (local_event_data->ExpStatus ==
		    MPI2_EVENT_SAS_TOPO_ES_ADDED ||
		    local_event_data->ExpStatus ==
		    MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
			if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
			    expander_handle) {
4627 4628
				dewtprintk(ioc,
					   ioc_info(ioc, "setting ignoring flag\n"));
4629 4630 4631 4632 4633 4634 4635
				fw_event->ignore = 1;
			}
		}
	}
	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
}

4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698
/**
 * _scsih_check_pcie_topo_remove_events - sanity check on topo
 * events
 * @ioc: per adapter object
 * @event_data: the event data payload
 *
 * This handles the case where driver receives multiple switch
 * or device add and delete events in a single shot.  When there
 * is a delete event the routine will void any pending add
 * events waiting in the event queue.
 */
static void
_scsih_check_pcie_topo_remove_events(struct MPT3SAS_ADAPTER *ioc,
	Mpi26EventDataPCIeTopologyChangeList_t *event_data)
{
	struct fw_event_work *fw_event;
	Mpi26EventDataPCIeTopologyChangeList_t *local_event_data;
	unsigned long flags;
	int i, reason_code;
	u16 handle, switch_handle;

	for (i = 0; i < event_data->NumEntries; i++) {
		handle =
			le16_to_cpu(event_data->PortEntry[i].AttachedDevHandle);
		if (!handle)
			continue;
		reason_code = event_data->PortEntry[i].PortStatus;
		if (reason_code == MPI26_EVENT_PCIE_TOPO_PS_NOT_RESPONDING)
			_scsih_tm_tr_send(ioc, handle);
	}

	switch_handle = le16_to_cpu(event_data->SwitchDevHandle);
	if (!switch_handle) {
		_scsih_block_io_to_pcie_children_attached_directly(
							ioc, event_data);
		return;
	}
    /* TODO We are not supporting cascaded PCIe Switch removal yet*/
	if ((event_data->SwitchStatus
		== MPI26_EVENT_PCIE_TOPO_SS_DELAY_NOT_RESPONDING) ||
		(event_data->SwitchStatus ==
					MPI26_EVENT_PCIE_TOPO_SS_RESPONDING))
		_scsih_block_io_to_pcie_children_attached_directly(
							ioc, event_data);

	if (event_data->SwitchStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
		return;

	/* mark ignore flag for pending events */
	spin_lock_irqsave(&ioc->fw_event_lock, flags);
	list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
		if (fw_event->event != MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
			fw_event->ignore)
			continue;
		local_event_data =
			(Mpi26EventDataPCIeTopologyChangeList_t *)
			fw_event->event_data;
		if (local_event_data->SwitchStatus ==
		    MPI2_EVENT_SAS_TOPO_ES_ADDED ||
		    local_event_data->SwitchStatus ==
		    MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
			if (le16_to_cpu(local_event_data->SwitchDevHandle) ==
				switch_handle) {
4699 4700
				dewtprintk(ioc,
					   ioc_info(ioc, "setting ignoring flag for switch event\n"));
4701 4702 4703 4704 4705 4706 4707
				fw_event->ignore = 1;
			}
		}
	}
	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
}

4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722
/**
 * _scsih_set_volume_delete_flag - setting volume delete flag
 * @ioc: per adapter object
 * @handle: device handle
 *
 * This returns nothing.
 */
static void
_scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	struct _raid_device *raid_device;
	struct MPT3SAS_TARGET *sas_target_priv_data;
	unsigned long flags;

	spin_lock_irqsave(&ioc->raid_device_lock, flags);
4723
	raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
4724 4725 4726 4727 4728
	if (raid_device && raid_device->starget &&
	    raid_device->starget->hostdata) {
		sas_target_priv_data =
		    raid_device->starget->hostdata;
		sas_target_priv_data->deleted = 1;
4729 4730 4731
		dewtprintk(ioc,
			   ioc_info(ioc, "setting delete flag: handle(0x%04x), wwid(0x%016llx)\n",
				    handle, (u64)raid_device->wwid));
4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780
	}
	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
}

/**
 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
 * @handle: input handle
 * @a: handle for volume a
 * @b: handle for volume b
 *
 * IR firmware only supports two raid volumes.  The purpose of this
 * routine is to set the volume handle in either a or b. When the given
 * input handle is non-zero, or when a and b have not been set before.
 */
static void
_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
{
	if (!handle || handle == *a || handle == *b)
		return;
	if (!*a)
		*a = handle;
	else if (!*b)
		*b = handle;
}

/**
 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
 * @ioc: per adapter object
 * @event_data: the event data payload
 * Context: interrupt time.
 *
 * This routine will send target reset to volume, followed by target
 * resets to the PDs. This is called when a PD has been removed, or
 * volume has been deleted or removed. When the target reset is sent
 * to volume, the PD target resets need to be queued to start upon
 * completion of the volume target reset.
 */
static void
_scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
	Mpi2EventDataIrConfigChangeList_t *event_data)
{
	Mpi2EventIrConfigElement_t *element;
	int i;
	u16 handle, volume_handle, a, b;
	struct _tr_list *delayed_tr;

	a = 0;
	b = 0;

4781 4782 4783
	if (ioc->is_warpdrive)
		return;

4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832
	/* Volume Resets for Deleted or Removed */
	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
	for (i = 0; i < event_data->NumElements; i++, element++) {
		if (le32_to_cpu(event_data->Flags) &
		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
			continue;
		if (element->ReasonCode ==
		    MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
		    element->ReasonCode ==
		    MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
			volume_handle = le16_to_cpu(element->VolDevHandle);
			_scsih_set_volume_delete_flag(ioc, volume_handle);
			_scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
		}
	}

	/* Volume Resets for UNHIDE events */
	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
	for (i = 0; i < event_data->NumElements; i++, element++) {
		if (le32_to_cpu(event_data->Flags) &
		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
			continue;
		if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
			volume_handle = le16_to_cpu(element->VolDevHandle);
			_scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
		}
	}

	if (a)
		_scsih_tm_tr_volume_send(ioc, a);
	if (b)
		_scsih_tm_tr_volume_send(ioc, b);

	/* PD target resets */
	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
	for (i = 0; i < event_data->NumElements; i++, element++) {
		if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
			continue;
		handle = le16_to_cpu(element->PhysDiskDevHandle);
		volume_handle = le16_to_cpu(element->VolDevHandle);
		clear_bit(handle, ioc->pd_handles);
		if (!volume_handle)
			_scsih_tm_tr_send(ioc, handle);
		else if (volume_handle == a || volume_handle == b) {
			delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
			BUG_ON(!delayed_tr);
			INIT_LIST_HEAD(&delayed_tr->list);
			delayed_tr->handle = handle;
			list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
4833 4834 4835
			dewtprintk(ioc,
				   ioc_info(ioc, "DELAYED:tr:handle(0x%04x), (open)\n",
					    handle));
4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866
		} else
			_scsih_tm_tr_send(ioc, handle);
	}
}


/**
 * _scsih_check_volume_delete_events - set delete flag for volumes
 * @ioc: per adapter object
 * @event_data: the event data payload
 * Context: interrupt time.
 *
 * This will handle the case when the cable connected to entire volume is
 * pulled. We will take care of setting the deleted flag so normal IO will
 * not be sent.
 */
static void
_scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
	Mpi2EventDataIrVolume_t *event_data)
{
	u32 state;

	if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
		return;
	state = le32_to_cpu(event_data->NewValue);
	if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
	    MPI2_RAID_VOL_STATE_FAILED)
		_scsih_set_volume_delete_flag(ioc,
		    le16_to_cpu(event_data->VolDevHandle));
}

4867 4868 4869 4870 4871 4872 4873 4874 4875 4876
/**
 * _scsih_temp_threshold_events - display temperature threshold exceeded events
 * @ioc: per adapter object
 * @event_data: the temp threshold event data
 * Context: interrupt time.
 */
static void
_scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
	Mpi2EventDataTemperature_t *event_data)
{
4877
	u32 doorbell;
4878
	if (ioc->temp_sensors_count >= event_data->SensorNum) {
4879 4880 4881 4882 4883 4884 4885 4886
		ioc_err(ioc, "Temperature Threshold flags %s%s%s%s exceeded for Sensor: %d !!!\n",
			le16_to_cpu(event_data->Status) & 0x1 ? "0 " : " ",
			le16_to_cpu(event_data->Status) & 0x2 ? "1 " : " ",
			le16_to_cpu(event_data->Status) & 0x4 ? "2 " : " ",
			le16_to_cpu(event_data->Status) & 0x8 ? "3 " : " ",
			event_data->SensorNum);
		ioc_err(ioc, "Current Temp In Celsius: %d\n",
			event_data->CurrentTemperature);
4887 4888 4889 4890
		if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
			doorbell = mpt3sas_base_get_iocstate(ioc, 0);
			if ((doorbell & MPI2_IOC_STATE_MASK) ==
			    MPI2_IOC_STATE_FAULT) {
4891
				mpt3sas_print_fault_code(ioc,
4892 4893 4894
				    doorbell & MPI2_DOORBELL_DATA_MASK);
			} else if ((doorbell & MPI2_IOC_STATE_MASK) ==
			    MPI2_IOC_STATE_COREDUMP) {
4895
				mpt3sas_print_coredump_info(ioc,
4896 4897 4898
				    doorbell & MPI2_DOORBELL_DATA_MASK);
			}
		}
4899 4900 4901
	}
}

4902
static int _scsih_set_satl_pending(struct scsi_cmnd *scmd, bool pending)
4903
{
4904 4905 4906 4907 4908 4909 4910 4911 4912 4913
	struct MPT3SAS_DEVICE *priv = scmd->device->hostdata;

	if (scmd->cmnd[0] != ATA_12 && scmd->cmnd[0] != ATA_16)
		return 0;

	if (pending)
		return test_and_set_bit(0, &priv->ata_command_pending);

	clear_bit(0, &priv->ata_command_pending);
	return 0;
4914 4915
}

4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926
/**
 * _scsih_flush_running_cmds - completing outstanding commands.
 * @ioc: per adapter object
 *
 * The flushing out of all pending scmd commands following host reset,
 * where all IO is dropped to the floor.
 */
static void
_scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
{
	struct scsi_cmnd *scmd;
4927
	struct scsiio_tracker *st;
4928
	u16 smid;
4929
	int count = 0;
4930 4931

	for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
4932
		scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
4933 4934 4935
		if (!scmd)
			continue;
		count++;
4936
		_scsih_set_satl_pending(scmd, false);
4937 4938
		st = scsi_cmd_priv(scmd);
		mpt3sas_base_clear_st(ioc, st);
4939
		scsi_dma_unmap(scmd);
4940
		if (ioc->pci_error_recovery || ioc->remove_host)
4941 4942 4943 4944 4945
			scmd->result = DID_NO_CONNECT << 16;
		else
			scmd->result = DID_RESET << 16;
		scmd->scsi_done(scmd);
	}
4946
	dtmprintk(ioc, ioc_info(ioc, "completing %d cmds\n", count));
4947 4948 4949 4950 4951 4952
}

/**
 * _scsih_setup_eedp - setup MPI request for EEDP transfer
 * @ioc: per adapter object
 * @scmd: pointer to scsi command object
4953
 * @mpi_request: pointer to the SCSI_IO request message frame
4954 4955 4956 4957 4958
 *
 * Supporting protection 1 and 3.
 */
static void
_scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4959
	Mpi25SCSIIORequest_t *mpi_request)
4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988
{
	u16 eedp_flags;
	unsigned char prot_op = scsi_get_prot_op(scmd);
	unsigned char prot_type = scsi_get_prot_type(scmd);
	Mpi25SCSIIORequest_t *mpi_request_3v =
	   (Mpi25SCSIIORequest_t *)mpi_request;

	if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
		return;

	if (prot_op ==  SCSI_PROT_READ_STRIP)
		eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
	else if (prot_op ==  SCSI_PROT_WRITE_INSERT)
		eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
	else
		return;

	switch (prot_type) {
	case SCSI_PROT_DIF_TYPE1:
	case SCSI_PROT_DIF_TYPE2:

		/*
		* enable ref/guard checking
		* auto increment ref tag
		*/
		eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
		    MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
		    MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
		mpi_request->CDB.EEDP32.PrimaryReferenceTag =
4989
		    cpu_to_be32(t10_pi_ref_tag(scmd->request));
4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003
		break;

	case SCSI_PROT_DIF_TYPE3:

		/*
		* enable guard checking
		*/
		eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;

		break;
	}

	mpi_request_3v->EEDPBlockSize =
	    cpu_to_le16(scmd->device->sector_size);
5004 5005 5006

	if (ioc->is_gen35_ioc)
		eedp_flags |= MPI25_SCSIIO_EEDPFLAGS_APPTAG_DISABLE_MODE;
5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040
	mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
}

/**
 * _scsih_eedp_error_handling - return sense code for EEDP errors
 * @scmd: pointer to scsi command object
 * @ioc_status: ioc status
 */
static void
_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
{
	u8 ascq;

	switch (ioc_status) {
	case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
		ascq = 0x01;
		break;
	case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
		ascq = 0x02;
		break;
	case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
		ascq = 0x03;
		break;
	default:
		ascq = 0x00;
		break;
	}
	scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
	    ascq);
	scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
	    SAM_STAT_CHECK_CONDITION;
}

/**
5041
 * scsih_qcmd - main scsi request entry point
5042
 * @shost: SCSI host pointer
5043 5044 5045 5046
 * @scmd: pointer to scsi command object
 *
 * The callback index is set inside `ioc->scsi_io_cb_idx`.
 *
5047
 * Return: 0 on success.  If there's a failure, return either:
5048 5049 5050
 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
 */
5051
static int
5052
scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
5053
{
5054
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
5055 5056
	struct MPT3SAS_DEVICE *sas_device_priv_data;
	struct MPT3SAS_TARGET *sas_target_priv_data;
5057
	struct _raid_device *raid_device;
5058 5059
	struct request *rq = scmd->request;
	int class;
5060 5061
	Mpi25SCSIIORequest_t *mpi_request;
	struct _pcie_device *pcie_device = NULL;
5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075
	u32 mpi_control;
	u16 smid;
	u16 handle;

	if (ioc->logging_level & MPT_DEBUG_SCSI)
		scsi_print_command(scmd);

	sas_device_priv_data = scmd->device->hostdata;
	if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
		scmd->result = DID_NO_CONNECT << 16;
		scmd->scsi_done(scmd);
		return 0;
	}

5076
	if (!(_scsih_allow_scmd_to_device(ioc, scmd))) {
5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092
		scmd->result = DID_NO_CONNECT << 16;
		scmd->scsi_done(scmd);
		return 0;
	}

	sas_target_priv_data = sas_device_priv_data->sas_target;

	/* invalid device handle */
	handle = sas_target_priv_data->handle;
	if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
		scmd->result = DID_NO_CONNECT << 16;
		scmd->scsi_done(scmd);
		return 0;
	}


B
Bart Van Assche 已提交
5093 5094
	if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress) {
		/* host recovery or link resets sent via IOCTLs */
5095
		return SCSI_MLQUEUE_HOST_BUSY;
B
Bart Van Assche 已提交
5096 5097
	} else if (sas_target_priv_data->deleted) {
		/* device has been deleted */
5098 5099 5100 5101
		scmd->result = DID_NO_CONNECT << 16;
		scmd->scsi_done(scmd);
		return 0;
	} else if (sas_target_priv_data->tm_busy ||
B
Bart Van Assche 已提交
5102 5103
		   sas_device_priv_data->block) {
		/* device busy with task management */
5104
		return SCSI_MLQUEUE_DEVICE_BUSY;
B
Bart Van Assche 已提交
5105
	}
5106

5107 5108 5109 5110 5111 5112
	/*
	 * Bug work around for firmware SATL handling.  The loop
	 * is based on atomic operations and ensures consistency
	 * since we're lockless at this point
	 */
	do {
5113 5114
		if (test_bit(0, &sas_device_priv_data->ata_command_pending))
			return SCSI_MLQUEUE_DEVICE_BUSY;
5115 5116
	} while (_scsih_set_satl_pending(scmd, true));

5117 5118 5119 5120 5121 5122 5123 5124
	if (scmd->sc_data_direction == DMA_FROM_DEVICE)
		mpi_control = MPI2_SCSIIO_CONTROL_READ;
	else if (scmd->sc_data_direction == DMA_TO_DEVICE)
		mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
	else
		mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;

	/* set tags */
5125
	mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
5126 5127 5128 5129 5130 5131
	/* NCQ Prio supported, make sure control indicated high priority */
	if (sas_device_priv_data->ncq_prio_enable) {
		class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
		if (class == IOPRIO_CLASS_RT)
			mpi_control |= 1 << MPI2_SCSIIO_CONTROL_CMDPRI_SHIFT;
	}
5132 5133 5134
	/* Make sure Device is not raid volume.
	 * We do not expose raid functionality to upper layer for warpdrive.
	 */
5135 5136 5137
	if (((!ioc->is_warpdrive && !scsih_is_raid(&scmd->device->sdev_gendev))
		&& !scsih_is_nvme(&scmd->device->sdev_gendev))
		&& sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
5138 5139 5140 5141
		mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;

	smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
	if (!smid) {
5142
		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
5143
		_scsih_set_satl_pending(scmd, false);
5144 5145 5146
		goto out;
	}
	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
5147
	memset(mpi_request, 0, ioc->request_sz);
5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165
	_scsih_setup_eedp(ioc, scmd, mpi_request);

	if (scmd->cmd_len == 32)
		mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
	mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
	if (sas_device_priv_data->sas_target->flags &
	    MPT_TARGET_FLAGS_RAID_COMPONENT)
		mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
	else
		mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
	mpi_request->DevHandle = cpu_to_le16(handle);
	mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
	mpi_request->Control = cpu_to_le32(mpi_control);
	mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
	mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
	mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
	mpi_request->SenseBufferLowAddress =
	    mpt3sas_base_get_sense_buffer_dma(ioc, smid);
5166
	mpi_request->SGLOffset0 = offsetof(Mpi25SCSIIORequest_t, SGL) / 4;
5167 5168 5169 5170 5171
	int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
	    mpi_request->LUN);
	memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);

	if (mpi_request->DataLength) {
5172 5173
		pcie_device = sas_target_priv_data->pcie_dev;
		if (ioc->build_sg_scmd(ioc, scmd, smid, pcie_device)) {
5174
			mpt3sas_base_free_smid(ioc, smid);
5175
			_scsih_set_satl_pending(scmd, false);
5176 5177 5178 5179 5180
			goto out;
		}
	} else
		ioc->build_zero_len_sge(ioc, &mpi_request->SGL);

5181 5182
	raid_device = sas_target_priv_data->raid_device;
	if (raid_device && raid_device->direct_io_enabled)
5183
		mpt3sas_setup_direct_io(ioc, scmd,
5184
			raid_device, mpi_request);
5185

5186 5187 5188 5189
	if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
		if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
			mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
			    MPI25_SCSIIO_IOFLAGS_FAST_PATH);
5190
			ioc->put_smid_fast_path(ioc, smid, handle);
5191
		} else
5192
			ioc->put_smid_scsi_io(ioc, smid,
5193
			    le16_to_cpu(mpi_request->DevHandle));
5194
	} else
5195
		ioc->put_smid_default(ioc, smid);
5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227
	return 0;

 out:
	return SCSI_MLQUEUE_HOST_BUSY;
}

/**
 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
 * @sense_buffer: sense data returned by target
 * @data: normalized skey/asc/ascq
 */
static void
_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
{
	if ((sense_buffer[0] & 0x7F) >= 0x72) {
		/* descriptor format */
		data->skey = sense_buffer[1] & 0x0F;
		data->asc = sense_buffer[2];
		data->ascq = sense_buffer[3];
	} else {
		/* fixed format */
		data->skey = sense_buffer[2] & 0x0F;
		data->asc = sense_buffer[12];
		data->ascq = sense_buffer[13];
	}
}

/**
 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
 * @ioc: per adapter object
 * @scmd: pointer to scsi command object
 * @mpi_reply: reply mf payload returned from firmware
5228
 * @smid: ?
5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248
 *
 * scsi_status - SCSI Status code returned from target device
 * scsi_state - state info associated with SCSI_IO determined by ioc
 * ioc_status - ioc supplied status info
 */
static void
_scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
	Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
{
	u32 response_info;
	u8 *response_bytes;
	u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
	    MPI2_IOCSTATUS_MASK;
	u8 scsi_state = mpi_reply->SCSIState;
	u8 scsi_status = mpi_reply->SCSIStatus;
	char *desc_ioc_state = NULL;
	char *desc_scsi_status = NULL;
	char *desc_scsi_state = ioc->tmp_string;
	u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
	struct _sas_device *sas_device = NULL;
5249
	struct _pcie_device *pcie_device = NULL;
5250 5251 5252 5253 5254 5255
	struct scsi_target *starget = scmd->device->sdev_target;
	struct MPT3SAS_TARGET *priv_target = starget->hostdata;
	char *device_str = NULL;

	if (!priv_target)
		return;
5256 5257 5258 5259
	if (ioc->hide_ir_msg)
		device_str = "WarpDrive";
	else
		device_str = "volume";
5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315

	if (log_info == 0x31170000)
		return;

	switch (ioc_status) {
	case MPI2_IOCSTATUS_SUCCESS:
		desc_ioc_state = "success";
		break;
	case MPI2_IOCSTATUS_INVALID_FUNCTION:
		desc_ioc_state = "invalid function";
		break;
	case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
		desc_ioc_state = "scsi recovered error";
		break;
	case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
		desc_ioc_state = "scsi invalid dev handle";
		break;
	case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
		desc_ioc_state = "scsi device not there";
		break;
	case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
		desc_ioc_state = "scsi data overrun";
		break;
	case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
		desc_ioc_state = "scsi data underrun";
		break;
	case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
		desc_ioc_state = "scsi io data error";
		break;
	case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
		desc_ioc_state = "scsi protocol error";
		break;
	case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
		desc_ioc_state = "scsi task terminated";
		break;
	case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
		desc_ioc_state = "scsi residual mismatch";
		break;
	case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
		desc_ioc_state = "scsi task mgmt failed";
		break;
	case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
		desc_ioc_state = "scsi ioc terminated";
		break;
	case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
		desc_ioc_state = "scsi ext terminated";
		break;
	case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
		desc_ioc_state = "eedp guard error";
		break;
	case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
		desc_ioc_state = "eedp ref tag error";
		break;
	case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
		desc_ioc_state = "eedp app tag error";
		break;
5316 5317 5318
	case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
		desc_ioc_state = "insufficient power";
		break;
5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379
	default:
		desc_ioc_state = "unknown";
		break;
	}

	switch (scsi_status) {
	case MPI2_SCSI_STATUS_GOOD:
		desc_scsi_status = "good";
		break;
	case MPI2_SCSI_STATUS_CHECK_CONDITION:
		desc_scsi_status = "check condition";
		break;
	case MPI2_SCSI_STATUS_CONDITION_MET:
		desc_scsi_status = "condition met";
		break;
	case MPI2_SCSI_STATUS_BUSY:
		desc_scsi_status = "busy";
		break;
	case MPI2_SCSI_STATUS_INTERMEDIATE:
		desc_scsi_status = "intermediate";
		break;
	case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
		desc_scsi_status = "intermediate condmet";
		break;
	case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
		desc_scsi_status = "reservation conflict";
		break;
	case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
		desc_scsi_status = "command terminated";
		break;
	case MPI2_SCSI_STATUS_TASK_SET_FULL:
		desc_scsi_status = "task set full";
		break;
	case MPI2_SCSI_STATUS_ACA_ACTIVE:
		desc_scsi_status = "aca active";
		break;
	case MPI2_SCSI_STATUS_TASK_ABORTED:
		desc_scsi_status = "task aborted";
		break;
	default:
		desc_scsi_status = "unknown";
		break;
	}

	desc_scsi_state[0] = '\0';
	if (!scsi_state)
		desc_scsi_state = " ";
	if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
		strcat(desc_scsi_state, "response info ");
	if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
		strcat(desc_scsi_state, "state terminated ");
	if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
		strcat(desc_scsi_state, "no status ");
	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
		strcat(desc_scsi_state, "autosense failed ");
	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
		strcat(desc_scsi_state, "autosense valid ");

	scsi_print_command(scmd);

	if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
5380 5381
		ioc_warn(ioc, "\t%s wwid(0x%016llx)\n",
			 device_str, (u64)priv_target->sas_address);
5382 5383 5384
	} else if (priv_target->flags & MPT_TARGET_FLAGS_PCIE_DEVICE) {
		pcie_device = mpt3sas_get_pdev_from_target(ioc, priv_target);
		if (pcie_device) {
5385 5386
			ioc_info(ioc, "\twwid(0x%016llx), port(%d)\n",
				 (u64)pcie_device->wwid, pcie_device->port_num);
5387
			if (pcie_device->enclosure_handle != 0)
5388 5389 5390
				ioc_info(ioc, "\tenclosure logical id(0x%016llx), slot(%d)\n",
					 (u64)pcie_device->enclosure_logical_id,
					 pcie_device->slot);
5391
			if (pcie_device->connector_name[0])
5392 5393 5394
				ioc_info(ioc, "\tenclosure level(0x%04x), connector name( %s)\n",
					 pcie_device->enclosure_level,
					 pcie_device->connector_name);
5395 5396
			pcie_device_put(pcie_device);
		}
5397
	} else {
5398
		sas_device = mpt3sas_get_sdev_from_target(ioc, priv_target);
5399
		if (sas_device) {
5400 5401
			ioc_warn(ioc, "\tsas_address(0x%016llx), phy(%d)\n",
				 (u64)sas_device->sas_address, sas_device->phy);
5402 5403 5404

			_scsih_display_enclosure_chassis_info(ioc, sas_device,
			    NULL, NULL);
5405 5406

			sas_device_put(sas_device);
5407 5408 5409
		}
	}

5410 5411 5412 5413 5414 5415 5416 5417 5418 5419
	ioc_warn(ioc, "\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
		 le16_to_cpu(mpi_reply->DevHandle),
		 desc_ioc_state, ioc_status, smid);
	ioc_warn(ioc, "\trequest_len(%d), underflow(%d), resid(%d)\n",
		 scsi_bufflen(scmd), scmd->underflow, scsi_get_resid(scmd));
	ioc_warn(ioc, "\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
		 le16_to_cpu(mpi_reply->TaskTag),
		 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
	ioc_warn(ioc, "\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
		 desc_scsi_status, scsi_status, desc_scsi_state, scsi_state);
5420 5421 5422 5423

	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
		struct sense_info data;
		_scsih_normalize_sense(scmd->sense_buffer, &data);
5424 5425 5426
		ioc_warn(ioc, "\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
			 data.skey, data.asc, data.ascq,
			 le32_to_cpu(mpi_reply->SenseCount));
5427 5428 5429 5430 5431 5432 5433 5434 5435
	}
	if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
		response_info = le32_to_cpu(mpi_reply->ResponseInfo);
		response_bytes = (u8 *)&response_info;
		_scsih_response_code(ioc, response_bytes[0]);
	}
}

/**
5436
 * _scsih_turn_on_pfa_led - illuminate PFA LED
5437 5438 5439 5440 5441
 * @ioc: per adapter object
 * @handle: device handle
 * Context: process
 */
static void
5442
_scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
5443 5444 5445
{
	Mpi2SepReply_t mpi_reply;
	Mpi2SepRequest_t mpi_request;
5446 5447
	struct _sas_device *sas_device;

5448
	sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
5449 5450
	if (!sas_device)
		return;
5451 5452 5453 5454 5455 5456 5457 5458 5459 5460

	memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
	mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
	mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
	mpi_request.SlotStatus =
	    cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
	mpi_request.DevHandle = cpu_to_le16(handle);
	mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
	if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
	    &mpi_request)) != 0) {
5461 5462
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
5463
		goto out;
5464
	}
5465
	sas_device->pfa_led_on = 1;
5466 5467

	if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
5468 5469 5470 5471
		dewtprintk(ioc,
			   ioc_info(ioc, "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
				    le16_to_cpu(mpi_reply.IOCStatus),
				    le32_to_cpu(mpi_reply.IOCLogInfo)));
5472
		goto out;
5473
	}
5474 5475
out:
	sas_device_put(sas_device);
5476
}
5477

5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489
/**
 * _scsih_turn_off_pfa_led - turn off Fault LED
 * @ioc: per adapter object
 * @sas_device: sas device whose PFA LED has to turned off
 * Context: process
 */
static void
_scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
	struct _sas_device *sas_device)
{
	Mpi2SepReply_t mpi_reply;
	Mpi2SepRequest_t mpi_request;
5490

5491 5492 5493 5494 5495 5496 5497 5498 5499 5500
	memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
	mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
	mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
	mpi_request.SlotStatus = 0;
	mpi_request.Slot = cpu_to_le16(sas_device->slot);
	mpi_request.DevHandle = 0;
	mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
	mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
	if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
		&mpi_request)) != 0) {
5501 5502
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
5503 5504 5505 5506
		return;
	}

	if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
5507 5508 5509 5510
		dewtprintk(ioc,
			   ioc_info(ioc, "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
				    le16_to_cpu(mpi_reply.IOCStatus),
				    le32_to_cpu(mpi_reply.IOCLogInfo)));
5511 5512 5513
		return;
	}
}
5514

5515
/**
5516
 * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
5517 5518 5519 5520 5521
 * @ioc: per adapter object
 * @handle: device handle
 * Context: interrupt.
 */
static void
5522
_scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
5523 5524 5525
{
	struct fw_event_work *fw_event;

5526
	fw_event = alloc_fw_event_work(0);
5527 5528
	if (!fw_event)
		return;
5529
	fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
5530 5531 5532
	fw_event->device_handle = handle;
	fw_event->ioc = ioc;
	_scsih_fw_event_add(ioc, fw_event);
5533
	fw_event_work_put(fw_event);
5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554
}

/**
 * _scsih_smart_predicted_fault - process smart errors
 * @ioc: per adapter object
 * @handle: device handle
 * Context: interrupt.
 */
static void
_scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	struct scsi_target *starget;
	struct MPT3SAS_TARGET *sas_target_priv_data;
	Mpi2EventNotificationReply_t *event_reply;
	Mpi2EventDataSasDeviceStatusChange_t *event_data;
	struct _sas_device *sas_device;
	ssize_t sz;
	unsigned long flags;

	/* only handle non-raid devices */
	spin_lock_irqsave(&ioc->sas_device_lock, flags);
5555 5556 5557 5558
	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
	if (!sas_device)
		goto out_unlock;

5559 5560 5561 5562
	starget = sas_device->starget;
	sas_target_priv_data = starget->hostdata;

	if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
5563 5564 5565
	   ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)))
		goto out_unlock;

5566 5567
	_scsih_display_enclosure_chassis_info(NULL, sas_device, NULL, starget);

5568 5569 5570
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);

	if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
5571
		_scsih_send_event_to_turn_on_pfa_led(ioc, handle);
5572 5573 5574 5575

	/* insert into event log */
	sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
	     sizeof(Mpi2EventDataSasDeviceStatusChange_t);
5576
	event_reply = kzalloc(sz, GFP_ATOMIC);
5577
	if (!event_reply) {
5578 5579
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
5580
		goto out;
5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596
	}

	event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
	event_reply->Event =
	    cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
	event_reply->MsgLength = sz/4;
	event_reply->EventDataLength =
	    cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
	event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
	    event_reply->EventData;
	event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
	event_data->ASC = 0x5D;
	event_data->DevHandle = cpu_to_le16(handle);
	event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
	mpt3sas_ctl_add_to_event_log(ioc, event_reply);
	kfree(event_reply);
5597 5598 5599 5600 5601 5602 5603 5604
out:
	if (sas_device)
		sas_device_put(sas_device);
	return;

out_unlock:
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
	goto out;
5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615
}

/**
 * _scsih_io_done - scsi request callback
 * @ioc: per adapter object
 * @smid: system request message index
 * @msix_index: MSIX table index supplied by the OS
 * @reply: reply message frame(lower 32bit addr)
 *
 * Callback handler when using _scsih_qcmd.
 *
5616 5617
 * Return: 1 meaning mf should be freed from _base_interrupt
 *         0 means the mf is freed from this function.
5618 5619 5620 5621
 */
static u8
_scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
{
5622
	Mpi25SCSIIORequest_t *mpi_request;
5623 5624
	Mpi2SCSIIOReply_t *mpi_reply;
	struct scsi_cmnd *scmd;
5625
	struct scsiio_tracker *st;
5626 5627 5628 5629 5630 5631 5632 5633 5634
	u16 ioc_status;
	u32 xfer_cnt;
	u8 scsi_state;
	u8 scsi_status;
	u32 log_info;
	struct MPT3SAS_DEVICE *sas_device_priv_data;
	u32 response_code = 0;

	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
5635

5636
	scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
5637 5638 5639
	if (scmd == NULL)
		return 1;

5640
	_scsih_set_satl_pending(scmd, false);
5641

5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656
	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);

	if (mpi_reply == NULL) {
		scmd->result = DID_OK << 16;
		goto out;
	}

	sas_device_priv_data = scmd->device->hostdata;
	if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
	     sas_device_priv_data->sas_target->deleted) {
		scmd->result = DID_NO_CONNECT << 16;
		goto out;
	}
	ioc_status = le16_to_cpu(mpi_reply->IOCStatus);

5657 5658 5659 5660
	/*
	 * WARPDRIVE: If direct_io is set then it is directIO,
	 * the failed direct I/O should be redirected to volume
	 */
5661 5662
	st = scsi_cmd_priv(scmd);
	if (st->direct_io &&
5663 5664
	     ((ioc_status & MPI2_IOCSTATUS_MASK)
	      != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
5665
		st->direct_io = 0;
5666
		st->scmd = scmd;
5667 5668 5669
		memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
		mpi_request->DevHandle =
		    cpu_to_le16(sas_device_priv_data->sas_target->handle);
5670
		ioc->put_smid_scsi_io(ioc, smid,
5671 5672 5673
		    sas_device_priv_data->sas_target->handle);
		return 0;
	}
5674 5675 5676 5677 5678 5679 5680
	/* turning off TLR */
	scsi_state = mpi_reply->SCSIState;
	if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
		response_code =
		    le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
	if (!sas_device_priv_data->tlr_snoop_check) {
		sas_device_priv_data->tlr_snoop_check++;
5681
		if ((!ioc->is_warpdrive &&
5682
		    !scsih_is_raid(&scmd->device->sdev_gendev) &&
5683 5684
		    !scsih_is_nvme(&scmd->device->sdev_gendev))
		    && sas_is_tlr_enabled(scmd->device) &&
5685 5686 5687 5688
		    response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
			sas_disable_tlr(scmd->device);
			sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
		}
5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720
	}

	xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
	scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
	if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
		log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
	else
		log_info = 0;
	ioc_status &= MPI2_IOCSTATUS_MASK;
	scsi_status = mpi_reply->SCSIStatus;

	if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
	    (scsi_status == MPI2_SCSI_STATUS_BUSY ||
	     scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
	     scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
		ioc_status = MPI2_IOCSTATUS_SUCCESS;
	}

	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
		struct sense_info data;
		const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
		    smid);
		u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
		    le32_to_cpu(mpi_reply->SenseCount));
		memcpy(scmd->sense_buffer, sense_data, sz);
		_scsih_normalize_sense(scmd->sense_buffer, &data);
		/* failure prediction threshold exceeded */
		if (data.asc == 0x5D)
			_scsih_smart_predicted_fault(ioc,
			    le16_to_cpu(mpi_reply->DevHandle));
		mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);

5721
		if ((ioc->logging_level & MPT_DEBUG_REPLY) &&
5722 5723 5724 5725 5726
		     ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
		     (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
		     (scmd->sense_buffer[2] == HARDWARE_ERROR)))
			_scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
	}
5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751
	switch (ioc_status) {
	case MPI2_IOCSTATUS_BUSY:
	case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
		scmd->result = SAM_STAT_BUSY;
		break;

	case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
		scmd->result = DID_NO_CONNECT << 16;
		break;

	case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
		if (sas_device_priv_data->block) {
			scmd->result = DID_TRANSPORT_DISRUPTED << 16;
			goto out;
		}
		if (log_info == 0x31110630) {
			if (scmd->retries > 2) {
				scmd->result = DID_NO_CONNECT << 16;
				scsi_device_set_state(scmd->device,
				    SDEV_OFFLINE);
			} else {
				scmd->result = DID_SOFT_ERROR << 16;
				scmd->device->expecting_cc_ua = 1;
			}
			break;
5752 5753 5754
		} else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
			scmd->result = DID_RESET << 16;
			break;
5755 5756 5757 5758 5759
		} else if ((scmd->device->channel == RAID_CHANNEL) &&
		   (scsi_state == (MPI2_SCSI_STATE_TERMINATED |
		   MPI2_SCSI_STATE_NO_SCSI_STATUS))) {
			scmd->result = DID_RESET << 16;
			break;
5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804
		}
		scmd->result = DID_SOFT_ERROR << 16;
		break;
	case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
	case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
		scmd->result = DID_RESET << 16;
		break;

	case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
		if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
			scmd->result = DID_SOFT_ERROR << 16;
		else
			scmd->result = (DID_OK << 16) | scsi_status;
		break;

	case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
		scmd->result = (DID_OK << 16) | scsi_status;

		if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
			break;

		if (xfer_cnt < scmd->underflow) {
			if (scsi_status == SAM_STAT_BUSY)
				scmd->result = SAM_STAT_BUSY;
			else
				scmd->result = DID_SOFT_ERROR << 16;
		} else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
		     MPI2_SCSI_STATE_NO_SCSI_STATUS))
			scmd->result = DID_SOFT_ERROR << 16;
		else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
			scmd->result = DID_RESET << 16;
		else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
			mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
			mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
			scmd->result = (DRIVER_SENSE << 24) |
			    SAM_STAT_CHECK_CONDITION;
			scmd->sense_buffer[0] = 0x70;
			scmd->sense_buffer[2] = ILLEGAL_REQUEST;
			scmd->sense_buffer[12] = 0x20;
			scmd->sense_buffer[13] = 0;
		}
		break;

	case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
		scsi_set_resid(scmd, 0);
5805
		fallthrough;
5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831
	case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
	case MPI2_IOCSTATUS_SUCCESS:
		scmd->result = (DID_OK << 16) | scsi_status;
		if (response_code ==
		    MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
		    (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
		     MPI2_SCSI_STATE_NO_SCSI_STATUS)))
			scmd->result = DID_SOFT_ERROR << 16;
		else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
			scmd->result = DID_RESET << 16;
		break;

	case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
	case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
	case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
		_scsih_eedp_error_handling(scmd, ioc_status);
		break;

	case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
	case MPI2_IOCSTATUS_INVALID_FUNCTION:
	case MPI2_IOCSTATUS_INVALID_SGL:
	case MPI2_IOCSTATUS_INTERNAL_ERROR:
	case MPI2_IOCSTATUS_INVALID_FIELD:
	case MPI2_IOCSTATUS_INVALID_STATE:
	case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
	case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
5832
	case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844
	default:
		scmd->result = DID_SOFT_ERROR << 16;
		break;

	}

	if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
		_scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);

 out:

	scsi_dma_unmap(scmd);
5845
	mpt3sas_base_free_smid(ioc, smid);
5846
	scmd->scsi_done(scmd);
5847
	return 0;
5848 5849
}

5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047
/**
 * _scsih_update_vphys_after_reset - update the Port's
 *			vphys_list after reset
 * @ioc: per adapter object
 *
 * Returns nothing.
 */
static void
_scsih_update_vphys_after_reset(struct MPT3SAS_ADAPTER *ioc)
{
	u16 sz, ioc_status;
	int i;
	Mpi2ConfigReply_t mpi_reply;
	Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
	u16 attached_handle;
	u64 attached_sas_addr;
	u8 found = 0, port_id;
	Mpi2SasPhyPage0_t phy_pg0;
	struct hba_port *port, *port_next, *mport;
	struct virtual_phy *vphy, *vphy_next;
	struct _sas_device *sas_device;

	/*
	 * Mark all the vphys objects as dirty.
	 */
	list_for_each_entry_safe(port, port_next,
	    &ioc->port_table_list, list) {
		if (!port->vphys_mask)
			continue;
		list_for_each_entry_safe(vphy, vphy_next,
		    &port->vphys_list, list) {
			vphy->flags |= MPT_VPHY_FLAG_DIRTY_PHY;
		}
	}

	/*
	 * Read SASIOUnitPage0 to get each HBA Phy's data.
	 */
	sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) +
	    (ioc->sas_hba.num_phys * sizeof(Mpi2SasIOUnit0PhyData_t));
	sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
	if (!sas_iounit_pg0) {
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return;
	}
	if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
	    sas_iounit_pg0, sz)) != 0)
		goto out;
	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
		goto out;
	/*
	 * Loop over each HBA Phy.
	 */
	for (i = 0; i < ioc->sas_hba.num_phys; i++) {
		/*
		 * Check whether Phy's Negotiation Link Rate is > 1.5G or not.
		 */
		if ((sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4) <
		    MPI2_SAS_NEG_LINK_RATE_1_5)
			continue;
		/*
		 * Check whether Phy is connected to SEP device or not,
		 * if it is SEP device then read the Phy's SASPHYPage0 data to
		 * determine whether Phy is a virtual Phy or not. if it is
		 * virtual phy then it is conformed that the attached remote
		 * device is a HBA's vSES device.
		 */
		if (!(le32_to_cpu(
		    sas_iounit_pg0->PhyData[i].ControllerPhyDeviceInfo) &
		    MPI2_SAS_DEVICE_INFO_SEP))
			continue;

		if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
		    i))) {
			ioc_err(ioc, "failure at %s:%d/%s()!\n",
			    __FILE__, __LINE__, __func__);
			continue;
		}

		if (!(le32_to_cpu(phy_pg0.PhyInfo) &
		    MPI2_SAS_PHYINFO_VIRTUAL_PHY))
			continue;
		/*
		 * Get the vSES device's SAS Address.
		 */
		attached_handle = le16_to_cpu(
		    sas_iounit_pg0->PhyData[i].AttachedDevHandle);
		if (_scsih_get_sas_address(ioc, attached_handle,
		    &attached_sas_addr) != 0) {
			ioc_err(ioc, "failure at %s:%d/%s()!\n",
			    __FILE__, __LINE__, __func__);
			continue;
		}

		found = 0;
		port = port_next = NULL;
		/*
		 * Loop over each virtual_phy object from
		 * each port's vphys_list.
		 */
		list_for_each_entry_safe(port,
		    port_next, &ioc->port_table_list, list) {
			if (!port->vphys_mask)
				continue;
			list_for_each_entry_safe(vphy, vphy_next,
			    &port->vphys_list, list) {
				/*
				 * Continue with next virtual_phy object
				 * if the object is not marked as dirty.
				 */
				if (!(vphy->flags & MPT_VPHY_FLAG_DIRTY_PHY))
					continue;

				/*
				 * Continue with next virtual_phy object
				 * if the object's SAS Address is not equals
				 * to current Phy's vSES device SAS Address.
				 */
				if (vphy->sas_address != attached_sas_addr)
					continue;
				/*
				 * Enable current Phy number bit in object's
				 * phy_mask field.
				 */
				if (!(vphy->phy_mask & (1 << i)))
					vphy->phy_mask = (1 << i);
				/*
				 * Get hba_port object from hba_port table
				 * corresponding to current phy's Port ID.
				 * if there is no hba_port object corresponding
				 * to Phy's Port ID then create a new hba_port
				 * object & add to hba_port table.
				 */
				port_id = sas_iounit_pg0->PhyData[i].Port;
				mport = mpt3sas_get_port_by_id(ioc, port_id, 1);
				if (!mport) {
					mport = kzalloc(
					    sizeof(struct hba_port), GFP_KERNEL);
					if (!mport)
						break;
					mport->port_id = port_id;
					ioc_info(ioc,
					    "%s: hba_port entry: %p, port: %d is added to hba_port list\n",
					    __func__, mport, mport->port_id);
					list_add_tail(&mport->list,
						&ioc->port_table_list);
				}
				/*
				 * If mport & port pointers are not pointing to
				 * same hba_port object then it means that vSES
				 * device's Port ID got changed after reset and
				 * hence move current virtual_phy object from
				 * port's vphys_list to mport's vphys_list.
				 */
				if (port != mport) {
					if (!mport->vphys_mask)
						INIT_LIST_HEAD(
						    &mport->vphys_list);
					mport->vphys_mask |= (1 << i);
					port->vphys_mask &= ~(1 << i);
					list_move(&vphy->list,
					    &mport->vphys_list);
					sas_device = mpt3sas_get_sdev_by_addr(
					    ioc, attached_sas_addr, port);
					if (sas_device)
						sas_device->port = mport;
				}
				/*
				 * Earlier while updating the hba_port table,
				 * it is determined that there is no other
				 * direct attached device with mport's Port ID,
				 * Hence mport was marked as dirty. Only vSES
				 * device has this Port ID, so unmark the mport
				 * as dirt.
				 */
				if (mport->flags & HBA_PORT_FLAG_DIRTY_PORT) {
					mport->sas_address = 0;
					mport->phy_mask = 0;
					mport->flags &=
					    ~HBA_PORT_FLAG_DIRTY_PORT;
				}
				/*
				 * Unmark current virtual_phy object as dirty.
				 */
				vphy->flags &= ~MPT_VPHY_FLAG_DIRTY_PHY;
				found = 1;
				break;
			}
			if (found)
				break;
		}
	}
out:
	kfree(sas_iounit_pg0);
}

6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267
/**
 * _scsih_get_port_table_after_reset - Construct temporary port table
 * @ioc: per adapter object
 * @port_table: address where port table needs to be constructed
 *
 * return number of HBA port entries available after reset.
 */
static int
_scsih_get_port_table_after_reset(struct MPT3SAS_ADAPTER *ioc,
	struct hba_port *port_table)
{
	u16 sz, ioc_status;
	int i, j;
	Mpi2ConfigReply_t mpi_reply;
	Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
	u16 attached_handle;
	u64 attached_sas_addr;
	u8 found = 0, port_count = 0, port_id;

	sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
	    * sizeof(Mpi2SasIOUnit0PhyData_t));
	sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
	if (!sas_iounit_pg0) {
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return port_count;
	}

	if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
	    sas_iounit_pg0, sz)) != 0)
		goto out;
	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
		goto out;
	for (i = 0; i < ioc->sas_hba.num_phys; i++) {
		found = 0;
		if ((sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4) <
		    MPI2_SAS_NEG_LINK_RATE_1_5)
			continue;
		attached_handle =
		    le16_to_cpu(sas_iounit_pg0->PhyData[i].AttachedDevHandle);
		if (_scsih_get_sas_address(
		    ioc, attached_handle, &attached_sas_addr) != 0) {
			ioc_err(ioc, "failure at %s:%d/%s()!\n",
			    __FILE__, __LINE__, __func__);
			continue;
		}

		for (j = 0; j < port_count; j++) {
			port_id = sas_iounit_pg0->PhyData[i].Port;
			if (port_table[j].port_id == port_id &&
			    port_table[j].sas_address == attached_sas_addr) {
				port_table[j].phy_mask |= (1 << i);
				found = 1;
				break;
			}
		}

		if (found)
			continue;

		port_id = sas_iounit_pg0->PhyData[i].Port;
		port_table[port_count].port_id = port_id;
		port_table[port_count].phy_mask = (1 << i);
		port_table[port_count].sas_address = attached_sas_addr;
		port_count++;
	}
out:
	kfree(sas_iounit_pg0);
	return port_count;
}

enum hba_port_matched_codes {
	NOT_MATCHED = 0,
	MATCHED_WITH_ADDR_AND_PHYMASK,
	MATCHED_WITH_ADDR_SUBPHYMASK_AND_PORT,
	MATCHED_WITH_ADDR_AND_SUBPHYMASK,
	MATCHED_WITH_ADDR,
};

/**
 * _scsih_look_and_get_matched_port_entry - Get matched hba port entry
 *					from HBA port table
 * @ioc: per adapter object
 * @port_entry - hba port entry from temporary port table which needs to be
 *		searched for matched entry in the HBA port table
 * @matched_port_entry - save matched hba port entry here
 * @count - count of matched entries
 *
 * return type of matched entry found.
 */
static enum hba_port_matched_codes
_scsih_look_and_get_matched_port_entry(struct MPT3SAS_ADAPTER *ioc,
	struct hba_port *port_entry,
	struct hba_port **matched_port_entry, int *count)
{
	struct hba_port *port_table_entry, *matched_port = NULL;
	enum hba_port_matched_codes matched_code = NOT_MATCHED;
	int lcount = 0;
	*matched_port_entry = NULL;

	list_for_each_entry(port_table_entry, &ioc->port_table_list, list) {
		if (!(port_table_entry->flags & HBA_PORT_FLAG_DIRTY_PORT))
			continue;

		if ((port_table_entry->sas_address == port_entry->sas_address)
		    && (port_table_entry->phy_mask == port_entry->phy_mask)) {
			matched_code = MATCHED_WITH_ADDR_AND_PHYMASK;
			matched_port = port_table_entry;
			break;
		}

		if ((port_table_entry->sas_address == port_entry->sas_address)
		    && (port_table_entry->phy_mask & port_entry->phy_mask)
		    && (port_table_entry->port_id == port_entry->port_id)) {
			matched_code = MATCHED_WITH_ADDR_SUBPHYMASK_AND_PORT;
			matched_port = port_table_entry;
			continue;
		}

		if ((port_table_entry->sas_address == port_entry->sas_address)
		    && (port_table_entry->phy_mask & port_entry->phy_mask)) {
			if (matched_code ==
			    MATCHED_WITH_ADDR_SUBPHYMASK_AND_PORT)
				continue;
			matched_code = MATCHED_WITH_ADDR_AND_SUBPHYMASK;
			matched_port = port_table_entry;
			continue;
		}

		if (port_table_entry->sas_address == port_entry->sas_address) {
			if (matched_code ==
			    MATCHED_WITH_ADDR_SUBPHYMASK_AND_PORT)
				continue;
			if (matched_code == MATCHED_WITH_ADDR_AND_SUBPHYMASK)
				continue;
			matched_code = MATCHED_WITH_ADDR;
			matched_port = port_table_entry;
			lcount++;
		}
	}

	*matched_port_entry = matched_port;
	if (matched_code ==  MATCHED_WITH_ADDR)
		*count = lcount;
	return matched_code;
}

/**
 * _scsih_del_phy_part_of_anther_port - remove phy if it
 *				is a part of anther port
 *@ioc: per adapter object
 *@port_table: port table after reset
 *@index: hba port entry index
 *@port_count: number of ports available after host reset
 *@offset: HBA phy bit offset
 *
 */
static void
_scsih_del_phy_part_of_anther_port(struct MPT3SAS_ADAPTER *ioc,
	struct hba_port *port_table,
	int index, u8 port_count, int offset)
{
	struct _sas_node *sas_node = &ioc->sas_hba;
	u32 i, found = 0;

	for (i = 0; i < port_count; i++) {
		if (i == index)
			continue;

		if (port_table[i].phy_mask & (1 << offset)) {
			mpt3sas_transport_del_phy_from_an_existing_port(
			    ioc, sas_node, &sas_node->phy[offset]);
			found = 1;
			break;
		}
	}
	if (!found)
		port_table[index].phy_mask |= (1 << offset);
}

/**
 * _scsih_add_or_del_phys_from_existing_port - add/remove phy to/from
 *						right port
 *@ioc: per adapter object
 *@hba_port_entry: hba port table entry
 *@port_table: temporary port table
 *@index: hba port entry index
 *@port_count: number of ports available after host reset
 *
 */
static void
_scsih_add_or_del_phys_from_existing_port(struct MPT3SAS_ADAPTER *ioc,
	struct hba_port *hba_port_entry, struct hba_port *port_table,
	int index, int port_count)
{
	u32 phy_mask, offset = 0;
	struct _sas_node *sas_node = &ioc->sas_hba;

	phy_mask = hba_port_entry->phy_mask ^ port_table[index].phy_mask;

	for (offset = 0; offset < ioc->sas_hba.num_phys; offset++) {
		if (phy_mask & (1 << offset)) {
			if (!(port_table[index].phy_mask & (1 << offset))) {
				_scsih_del_phy_part_of_anther_port(
				    ioc, port_table, index, port_count,
				    offset);
				continue;
			}
			if (sas_node->phy[offset].phy_belongs_to_port)
				mpt3sas_transport_del_phy_from_an_existing_port(
				    ioc, sas_node, &sas_node->phy[offset]);
			mpt3sas_transport_add_phy_to_an_existing_port(
			    ioc, sas_node, &sas_node->phy[offset],
			    hba_port_entry->sas_address,
			    hba_port_entry);
		}
	}
}

6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300
/**
 * _scsih_del_dirty_vphy - delete virtual_phy objects marked as dirty.
 * @ioc: per adapter object
 *
 * Returns nothing.
 */
static void
_scsih_del_dirty_vphy(struct MPT3SAS_ADAPTER *ioc)
{
	struct hba_port *port, *port_next;
	struct virtual_phy *vphy, *vphy_next;

	list_for_each_entry_safe(port, port_next,
	    &ioc->port_table_list, list) {
		if (!port->vphys_mask)
			continue;
		list_for_each_entry_safe(vphy, vphy_next,
		    &port->vphys_list, list) {
			if (vphy->flags & MPT_VPHY_FLAG_DIRTY_PHY) {
				drsprintk(ioc, ioc_info(ioc,
				    "Deleting vphy %p entry from port id: %d\t, Phy_mask 0x%08x\n",
				    vphy, port->port_id,
				    vphy->phy_mask));
				port->vphys_mask &= ~vphy->phy_mask;
				list_del(&vphy->list);
				kfree(vphy);
			}
		}
		if (!port->vphys_mask && !port->sas_address)
			port->flags |= HBA_PORT_FLAG_DIRTY_PORT;
	}
}

6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416
/**
 * _scsih_del_dirty_port_entries - delete dirty port entries from port list
 *					after host reset
 *@ioc: per adapter object
 *
 */
static void
_scsih_del_dirty_port_entries(struct MPT3SAS_ADAPTER *ioc)
{
	struct hba_port *port, *port_next;

	list_for_each_entry_safe(port, port_next,
	    &ioc->port_table_list, list) {
		if (!(port->flags & HBA_PORT_FLAG_DIRTY_PORT) ||
		    port->flags & HBA_PORT_FLAG_NEW_PORT)
			continue;

		drsprintk(ioc, ioc_info(ioc,
		    "Deleting port table entry %p having Port: %d\t Phy_mask 0x%08x\n",
		    port, port->port_id, port->phy_mask));
		list_del(&port->list);
		kfree(port);
	}
}

/**
 * _scsih_sas_port_refresh - Update HBA port table after host reset
 * @ioc: per adapter object
 */
static void
_scsih_sas_port_refresh(struct MPT3SAS_ADAPTER *ioc)
{
	u32 port_count = 0;
	struct hba_port *port_table;
	struct hba_port *port_table_entry;
	struct hba_port *port_entry = NULL;
	int i, j, count = 0, lcount = 0;
	int ret;
	u64 sas_addr;

	drsprintk(ioc, ioc_info(ioc,
	    "updating ports for sas_host(0x%016llx)\n",
	    (unsigned long long)ioc->sas_hba.sas_address));

	port_table = kcalloc(ioc->sas_hba.num_phys,
	    sizeof(struct hba_port), GFP_KERNEL);
	if (!port_table)
		return;

	port_count = _scsih_get_port_table_after_reset(ioc, port_table);
	if (!port_count)
		return;

	drsprintk(ioc, ioc_info(ioc, "New Port table\n"));
	for (j = 0; j < port_count; j++)
		drsprintk(ioc, ioc_info(ioc,
		    "Port: %d\t Phy_mask 0x%08x\t sas_addr(0x%016llx)\n",
		    port_table[j].port_id,
		    port_table[j].phy_mask, port_table[j].sas_address));

	list_for_each_entry(port_table_entry, &ioc->port_table_list, list)
		port_table_entry->flags |= HBA_PORT_FLAG_DIRTY_PORT;

	drsprintk(ioc, ioc_info(ioc, "Old Port table\n"));
	port_table_entry = NULL;
	list_for_each_entry(port_table_entry, &ioc->port_table_list, list) {
		drsprintk(ioc, ioc_info(ioc,
		    "Port: %d\t Phy_mask 0x%08x\t sas_addr(0x%016llx)\n",
		    port_table_entry->port_id,
		    port_table_entry->phy_mask,
		    port_table_entry->sas_address));
	}

	for (j = 0; j < port_count; j++) {
		ret = _scsih_look_and_get_matched_port_entry(ioc,
		    &port_table[j], &port_entry, &count);
		if (!port_entry) {
			drsprintk(ioc, ioc_info(ioc,
			    "No Matched entry for sas_addr(0x%16llx), Port:%d\n",
			    port_table[j].sas_address,
			    port_table[j].port_id));
			continue;
		}

		switch (ret) {
		case MATCHED_WITH_ADDR_SUBPHYMASK_AND_PORT:
		case MATCHED_WITH_ADDR_AND_SUBPHYMASK:
			_scsih_add_or_del_phys_from_existing_port(ioc,
			    port_entry, port_table, j, port_count);
			break;
		case MATCHED_WITH_ADDR:
			sas_addr = port_table[j].sas_address;
			for (i = 0; i < port_count; i++) {
				if (port_table[i].sas_address == sas_addr)
					lcount++;
			}

			if (count > 1 || lcount > 1)
				port_entry = NULL;
			else
				_scsih_add_or_del_phys_from_existing_port(ioc,
				    port_entry, port_table, j, port_count);
		}

		if (!port_entry)
			continue;

		if (port_entry->port_id != port_table[j].port_id)
			port_entry->port_id = port_table[j].port_id;
		port_entry->flags &= ~HBA_PORT_FLAG_DIRTY_PORT;
		port_entry->phy_mask = port_table[j].phy_mask;
	}

	port_table_entry = NULL;
}

6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430
/**
 * _scsih_alloc_vphy - allocate virtual_phy object
 * @ioc: per adapter object
 * @port_id: Port ID number
 * @phy_num: HBA Phy number
 *
 * Returns allocated virtual_phy object.
 */
static struct virtual_phy *
_scsih_alloc_vphy(struct MPT3SAS_ADAPTER *ioc, u8 port_id, u8 phy_num)
{
	struct virtual_phy *vphy;
	struct hba_port *port;

6431
	port = mpt3sas_get_port_by_id(ioc, port_id, 0);
6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457
	if (!port)
		return NULL;

	vphy = mpt3sas_get_vphy_by_phy(ioc, port, phy_num);
	if (!vphy) {
		vphy = kzalloc(sizeof(struct virtual_phy), GFP_KERNEL);
		if (!vphy)
			return NULL;

		/*
		 * Enable bit corresponding to HBA phy number on its
		 * parent hba_port object's vphys_mask field.
		 */
		port->vphys_mask |= (1 << phy_num);
		vphy->phy_mask |= (1 << phy_num);

		INIT_LIST_HEAD(&port->vphys_list);
		list_add_tail(&vphy->list, &port->vphys_list);

		ioc_info(ioc,
		    "vphy entry: %p, port id: %d, phy:%d is added to port's vphys_list\n",
		    vphy, port->port_id, phy_num);
	}
	return vphy;
}

6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475
/**
 * _scsih_sas_host_refresh - refreshing sas host object contents
 * @ioc: per adapter object
 * Context: user
 *
 * During port enable, fw will send topology events for every device. Its
 * possible that the handles may change from the previous setting, so this
 * code keeping handles updating if changed.
 */
static void
_scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
{
	u16 sz;
	u16 ioc_status;
	int i;
	Mpi2ConfigReply_t mpi_reply;
	Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
	u16 attached_handle;
6476 6477
	u8 link_rate, port_id;
	struct hba_port *port;
6478
	Mpi2SasPhyPage0_t phy_pg0;
6479

6480 6481 6482
	dtmprintk(ioc,
		  ioc_info(ioc, "updating handles for sas_host(0x%016llx)\n",
			   (u64)ioc->sas_hba.sas_address));
6483 6484 6485 6486 6487

	sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
	    * sizeof(Mpi2SasIOUnit0PhyData_t));
	sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
	if (!sas_iounit_pg0) {
6488 6489
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501
		return;
	}

	if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
	    sas_iounit_pg0, sz)) != 0)
		goto out;
	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
		goto out;
	for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
		link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
		if (i == 0)
6502 6503 6504
			ioc->sas_hba.handle = le16_to_cpu(
			    sas_iounit_pg0->PhyData[0].ControllerDevHandle);
		port_id = sas_iounit_pg0->PhyData[i].Port;
6505
		if (!(mpt3sas_get_port_by_id(ioc, port_id, 0))) {
6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517
			port = kzalloc(sizeof(struct hba_port), GFP_KERNEL);
			if (!port)
				goto out;

			port->port_id = port_id;
			ioc_info(ioc,
			    "hba_port entry: %p, port: %d is added to hba_port list\n",
			    port, port->port_id);
			if (ioc->shost_recovery)
				port->flags = HBA_PORT_FLAG_NEW_PORT;
			list_add_tail(&port->list, &ioc->port_table_list);
		}
6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542
		/*
		 * Check whether current Phy belongs to HBA vSES device or not.
		 */
		if (le32_to_cpu(sas_iounit_pg0->PhyData[i].ControllerPhyDeviceInfo) &
		    MPI2_SAS_DEVICE_INFO_SEP &&
		    (link_rate >=  MPI2_SAS_NEG_LINK_RATE_1_5)) {
			if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply,
			    &phy_pg0, i))) {
				ioc_err(ioc,
				    "failure at %s:%d/%s()!\n",
				     __FILE__, __LINE__, __func__);
				goto out;
			}
			if (!(le32_to_cpu(phy_pg0.PhyInfo) &
			    MPI2_SAS_PHYINFO_VIRTUAL_PHY))
				continue;
			/*
			 * Allocate a virtual_phy object for vSES device, if
			 * this vSES device is hot added.
			 */
			if (!_scsih_alloc_vphy(ioc, port_id, i))
				goto out;
			ioc->sas_hba.phy[i].hba_vphy = 1;
		}

6543 6544 6545 6546 6547
		ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
		attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
		    AttachedDevHandle);
		if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
			link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
6548 6549
		ioc->sas_hba.phy[i].port =
		    mpt3sas_get_port_by_id(ioc, port_id, 0);
6550
		mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
6551 6552
		    attached_handle, i, link_rate,
		    ioc->sas_hba.phy[i].port);
6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576
	}
 out:
	kfree(sas_iounit_pg0);
}

/**
 * _scsih_sas_host_add - create sas host object
 * @ioc: per adapter object
 *
 * Creating host side data object, stored in ioc->sas_hba
 */
static void
_scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
{
	int i;
	Mpi2ConfigReply_t mpi_reply;
	Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
	Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
	Mpi2SasPhyPage0_t phy_pg0;
	Mpi2SasDevicePage0_t sas_device_pg0;
	Mpi2SasEnclosurePage0_t enclosure_pg0;
	u16 ioc_status;
	u16 sz;
	u8 device_missing_delay;
6577 6578
	u8 num_phys, port_id;
	struct hba_port *port;
6579

6580 6581
	mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
	if (!num_phys) {
6582 6583
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
6584 6585
		return;
	}
6586 6587 6588
	ioc->sas_hba.phy = kcalloc(num_phys,
	    sizeof(struct _sas_phy), GFP_KERNEL);
	if (!ioc->sas_hba.phy) {
6589 6590
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
6591 6592 6593
		goto out;
	}
	ioc->sas_hba.num_phys = num_phys;
6594 6595 6596 6597 6598 6599

	/* sas_iounit page 0 */
	sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
	    sizeof(Mpi2SasIOUnit0PhyData_t));
	sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
	if (!sas_iounit_pg0) {
6600 6601
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
6602 6603 6604 6605
		return;
	}
	if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
	    sas_iounit_pg0, sz))) {
6606 6607
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
6608 6609 6610 6611 6612
		goto out;
	}
	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
	    MPI2_IOCSTATUS_MASK;
	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6613 6614
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
6615 6616 6617 6618 6619 6620 6621 6622
		goto out;
	}

	/* sas_iounit page 1 */
	sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
	    sizeof(Mpi2SasIOUnit1PhyData_t));
	sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
	if (!sas_iounit_pg1) {
6623 6624
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
6625 6626 6627 6628
		goto out;
	}
	if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
	    sas_iounit_pg1, sz))) {
6629 6630
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
6631 6632 6633 6634 6635
		goto out;
	}
	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
	    MPI2_IOCSTATUS_MASK;
	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6636 6637
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655
		goto out;
	}

	ioc->io_missing_delay =
	    sas_iounit_pg1->IODeviceMissingDelay;
	device_missing_delay =
	    sas_iounit_pg1->ReportDeviceMissingDelay;
	if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
		ioc->device_missing_delay = (device_missing_delay &
		    MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
	else
		ioc->device_missing_delay = device_missing_delay &
		    MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;

	ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
	for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
		if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
		    i))) {
6656 6657
			ioc_err(ioc, "failure at %s:%d/%s()!\n",
				__FILE__, __LINE__, __func__);
6658 6659 6660 6661 6662
			goto out;
		}
		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
		    MPI2_IOCSTATUS_MASK;
		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6663 6664
			ioc_err(ioc, "failure at %s:%d/%s()!\n",
				__FILE__, __LINE__, __func__);
6665 6666 6667 6668 6669 6670
			goto out;
		}

		if (i == 0)
			ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
			    PhyData[0].ControllerDevHandle);
6671 6672

		port_id = sas_iounit_pg0->PhyData[i].Port;
6673
		if (!(mpt3sas_get_port_by_id(ioc, port_id, 0))) {
6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685
			port = kzalloc(sizeof(struct hba_port), GFP_KERNEL);
			if (!port)
				goto out;

			port->port_id = port_id;
			ioc_info(ioc,
			   "hba_port entry: %p, port: %d is added to hba_port list\n",
			   port, port->port_id);
			list_add_tail(&port->list,
			    &ioc->port_table_list);
		}

6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700
		/*
		 * Check whether current Phy belongs to HBA vSES device or not.
		 */
		if ((le32_to_cpu(phy_pg0.PhyInfo) &
		    MPI2_SAS_PHYINFO_VIRTUAL_PHY) &&
		    (phy_pg0.NegotiatedLinkRate >> 4) >=
		    MPI2_SAS_NEG_LINK_RATE_1_5) {
			/*
			 * Allocate a virtual_phy object for vSES device.
			 */
			if (!_scsih_alloc_vphy(ioc, port_id, i))
				goto out;
			ioc->sas_hba.phy[i].hba_vphy = 1;
		}

6701 6702
		ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
		ioc->sas_hba.phy[i].phy_id = i;
6703 6704
		ioc->sas_hba.phy[i].port =
		    mpt3sas_get_port_by_id(ioc, port_id, 0);
6705 6706 6707 6708 6709
		mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
		    phy_pg0, ioc->sas_hba.parent_dev);
	}
	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
6710 6711
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
6712 6713 6714 6715 6716
		goto out;
	}
	ioc->sas_hba.enclosure_handle =
	    le16_to_cpu(sas_device_pg0.EnclosureHandle);
	ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6717 6718 6719 6720
	ioc_info(ioc, "host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
		 ioc->sas_hba.handle,
		 (u64)ioc->sas_hba.sas_address,
		 ioc->sas_hba.num_phys);
6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741

	if (ioc->sas_hba.enclosure_handle) {
		if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
		    &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
		   ioc->sas_hba.enclosure_handle)))
			ioc->sas_hba.enclosure_logical_id =
			    le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
	}

 out:
	kfree(sas_iounit_pg1);
	kfree(sas_iounit_pg0);
}

/**
 * _scsih_expander_add -  creating expander object
 * @ioc: per adapter object
 * @handle: expander handle
 *
 * Creating expander object, stored in ioc->sas_expander_list.
 *
6742
 * Return: 0 for success, else error.
6743 6744 6745 6746 6747
 */
static int
_scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	struct _sas_node *sas_expander;
6748
	struct _enclosure_node *enclosure_dev;
6749 6750 6751 6752 6753 6754 6755 6756 6757
	Mpi2ConfigReply_t mpi_reply;
	Mpi2ExpanderPage0_t expander_pg0;
	Mpi2ExpanderPage1_t expander_pg1;
	u32 ioc_status;
	u16 parent_handle;
	u64 sas_address, sas_address_parent = 0;
	int i;
	unsigned long flags;
	struct _sas_port *mpt3sas_port = NULL;
6758
	u8 port_id;
6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769

	int rc = 0;

	if (!handle)
		return -1;

	if (ioc->shost_recovery || ioc->pci_error_recovery)
		return -1;

	if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
	    MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
6770 6771
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
6772 6773 6774 6775 6776 6777
		return -1;
	}

	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
	    MPI2_IOCSTATUS_MASK;
	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6778 6779
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
6780 6781 6782 6783 6784 6785 6786
		return -1;
	}

	/* handle out of order topology events */
	parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
	if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
	    != 0) {
6787 6788
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
6789 6790
		return -1;
	}
6791 6792

	port_id = expander_pg0.PhysicalPort;
6793 6794 6795
	if (sas_address_parent != ioc->sas_hba.sas_address) {
		spin_lock_irqsave(&ioc->sas_node_lock, flags);
		sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
6796 6797
		    sas_address_parent,
		    mpt3sas_get_port_by_id(ioc, port_id, 0));
6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808
		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
		if (!sas_expander) {
			rc = _scsih_expander_add(ioc, parent_handle);
			if (rc != 0)
				return rc;
		}
	}

	spin_lock_irqsave(&ioc->sas_node_lock, flags);
	sas_address = le64_to_cpu(expander_pg0.SASAddress);
	sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
6809
	    sas_address, mpt3sas_get_port_by_id(ioc, port_id, 0));
6810 6811 6812 6813 6814 6815 6816 6817
	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);

	if (sas_expander)
		return 0;

	sas_expander = kzalloc(sizeof(struct _sas_node),
	    GFP_KERNEL);
	if (!sas_expander) {
6818 6819
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
6820 6821 6822 6823 6824 6825 6826
		return -1;
	}

	sas_expander->handle = handle;
	sas_expander->num_phys = expander_pg0.NumPhys;
	sas_expander->sas_address_parent = sas_address_parent;
	sas_expander->sas_address = sas_address;
6827
	sas_expander->port = mpt3sas_get_port_by_id(ioc, port_id, 0);
6828 6829 6830 6831 6832 6833
	if (!sas_expander->port) {
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		rc = -1;
		goto out_fail;
	}
6834

6835 6836 6837
	ioc_info(ioc, "expander_add: handle(0x%04x), parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
		 handle, parent_handle,
		 (u64)sas_expander->sas_address, sas_expander->num_phys);
6838 6839 6840 6841 6842 6843

	if (!sas_expander->num_phys)
		goto out_fail;
	sas_expander->phy = kcalloc(sas_expander->num_phys,
	    sizeof(struct _sas_phy), GFP_KERNEL);
	if (!sas_expander->phy) {
6844 6845
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
6846 6847 6848 6849 6850 6851
		rc = -1;
		goto out_fail;
	}

	INIT_LIST_HEAD(&sas_expander->sas_port_list);
	mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
6852
	    sas_address_parent, sas_expander->port);
6853
	if (!mpt3sas_port) {
6854 6855
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
6856 6857 6858 6859
		rc = -1;
		goto out_fail;
	}
	sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
6860
	sas_expander->rphy = mpt3sas_port->rphy;
6861 6862 6863 6864

	for (i = 0 ; i < sas_expander->num_phys ; i++) {
		if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
		    &expander_pg1, i, handle))) {
6865 6866
			ioc_err(ioc, "failure at %s:%d/%s()!\n",
				__FILE__, __LINE__, __func__);
6867 6868 6869 6870 6871
			rc = -1;
			goto out_fail;
		}
		sas_expander->phy[i].handle = handle;
		sas_expander->phy[i].phy_id = i;
6872 6873
		sas_expander->phy[i].port =
		    mpt3sas_get_port_by_id(ioc, port_id, 0);
6874 6875 6876 6877

		if ((mpt3sas_transport_add_expander_phy(ioc,
		    &sas_expander->phy[i], expander_pg1,
		    sas_expander->parent_dev))) {
6878 6879
			ioc_err(ioc, "failure at %s:%d/%s()!\n",
				__FILE__, __LINE__, __func__);
6880 6881 6882 6883 6884 6885
			rc = -1;
			goto out_fail;
		}
	}

	if (sas_expander->enclosure_handle) {
6886 6887 6888 6889
		enclosure_dev =
			mpt3sas_scsih_enclosure_find_by_handle(ioc,
						sas_expander->enclosure_handle);
		if (enclosure_dev)
6890
			sas_expander->enclosure_logical_id =
6891
			    le64_to_cpu(enclosure_dev->pg0.EnclosureLogicalID);
6892 6893 6894
	}

	_scsih_expander_node_add(ioc, sas_expander);
B
Bart Van Assche 已提交
6895
	return 0;
6896 6897 6898 6899 6900

 out_fail:

	if (mpt3sas_port)
		mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
6901
		    sas_address_parent, sas_expander->port);
6902 6903 6904 6905 6906 6907 6908 6909 6910 6911
	kfree(sas_expander);
	return rc;
}

/**
 * mpt3sas_expander_remove - removing expander object
 * @ioc: per adapter object
 * @sas_address: expander sas_address
 */
void
6912 6913
mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
	struct hba_port *port)
6914 6915 6916 6917 6918 6919 6920
{
	struct _sas_node *sas_expander;
	unsigned long flags;

	if (ioc->shost_recovery)
		return;

6921 6922 6923
	if (!port)
		return;

6924 6925
	spin_lock_irqsave(&ioc->sas_node_lock, flags);
	sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
6926
	    sas_address, port);
6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941
	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
	if (sas_expander)
		_scsih_expander_node_remove(ioc, sas_expander);
}

/**
 * _scsih_done -  internal SCSI_IO callback handler.
 * @ioc: per adapter object
 * @smid: system request message index
 * @msix_index: MSIX table index supplied by the OS
 * @reply: reply message frame(lower 32bit addr)
 *
 * Callback handler when sending internal generated SCSI_IO.
 * The callback index passed is `ioc->scsih_cb_idx`
 *
6942 6943
 * Return: 1 meaning mf should be freed from _base_interrupt
 *         0 means the mf is freed from this function.
6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976
 */
static u8
_scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
{
	MPI2DefaultReply_t *mpi_reply;

	mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
	if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
		return 1;
	if (ioc->scsih_cmds.smid != smid)
		return 1;
	ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
	if (mpi_reply) {
		memcpy(ioc->scsih_cmds.reply, mpi_reply,
		    mpi_reply->MsgLength*4);
		ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
	}
	ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
	complete(&ioc->scsih_cmds.done);
	return 1;
}




#define MPT3_MAX_LUNS (255)


/**
 * _scsih_check_access_status - check access flags
 * @ioc: per adapter object
 * @sas_address: sas address
 * @handle: sas device handle
6977
 * @access_status: errors returned during discovery of the device
6978
 *
6979
 * Return: 0 for success, else failure
6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029
 */
static u8
_scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
	u16 handle, u8 access_status)
{
	u8 rc = 1;
	char *desc = NULL;

	switch (access_status) {
	case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
	case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
		rc = 0;
		break;
	case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
		desc = "sata capability failed";
		break;
	case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
		desc = "sata affiliation conflict";
		break;
	case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
		desc = "route not addressable";
		break;
	case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
		desc = "smp error not addressable";
		break;
	case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
		desc = "device blocked";
		break;
	case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
	case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
	case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
	case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
	case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
	case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
	case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
	case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
	case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
	case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
	case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
	case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
		desc = "sata initialization failed";
		break;
	default:
		desc = "unknown";
		break;
	}

	if (!rc)
		return 0;

7030 7031
	ioc_err(ioc, "discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
		desc, (u64)sas_address, handle);
7032 7033 7034 7035 7036 7037 7038 7039
	return rc;
}

/**
 * _scsih_check_device - checking device responsiveness
 * @ioc: per adapter object
 * @parent_sas_address: sas address of parent expander or sas host
 * @handle: attached device handle
7040
 * @phy_number: phy number
7041 7042 7043 7044 7045 7046 7047 7048
 * @link_rate: new link rate
 */
static void
_scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
	u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
{
	Mpi2ConfigReply_t mpi_reply;
	Mpi2SasDevicePage0_t sas_device_pg0;
7049
	struct _sas_device *sas_device = NULL;
7050
	struct _enclosure_node *enclosure_dev = NULL;
7051 7052 7053 7054 7055 7056
	u32 ioc_status;
	unsigned long flags;
	u64 sas_address;
	struct scsi_target *starget;
	struct MPT3SAS_TARGET *sas_target_priv_data;
	u32 device_info;
7057
	struct hba_port *port;
7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079

	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
		return;

	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
		return;

	/* wide port handling ~ we need only handle device once for the phy that
	 * is matched in sas device page zero
	 */
	if (phy_number != sas_device_pg0.PhyNum)
		return;

	/* check if this is end device */
	device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
	if (!(_scsih_is_end_device(device_info)))
		return;

	spin_lock_irqsave(&ioc->sas_device_lock, flags);
	sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
7080
	port = mpt3sas_get_port_by_id(ioc, sas_device_pg0.PhysicalPort, 0);
7081 7082
	if (!port)
		goto out_unlock;
7083
	sas_device = __mpt3sas_get_sdev_by_addr(ioc,
7084
	    sas_address, port);
7085

7086 7087
	if (!sas_device)
		goto out_unlock;
7088 7089 7090 7091 7092 7093 7094 7095 7096

	if (unlikely(sas_device->handle != handle)) {
		starget = sas_device->starget;
		sas_target_priv_data = starget->hostdata;
		starget_printk(KERN_INFO, starget,
			"handle changed from(0x%04x) to (0x%04x)!!!\n",
			sas_device->handle, handle);
		sas_target_priv_data->handle = handle;
		sas_device->handle = handle;
7097
		if (le16_to_cpu(sas_device_pg0.Flags) &
7098 7099
		     MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
			sas_device->enclosure_level =
7100
				sas_device_pg0.EnclosureLevel;
7101 7102 7103
			memcpy(sas_device->connector_name,
				sas_device_pg0.ConnectorName, 4);
			sas_device->connector_name[4] = '\0';
7104 7105 7106 7107
		} else {
			sas_device->enclosure_level = 0;
			sas_device->connector_name[0] = '\0';
		}
7108

7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123
		sas_device->enclosure_handle =
				le16_to_cpu(sas_device_pg0.EnclosureHandle);
		sas_device->is_chassis_slot_valid = 0;
		enclosure_dev = mpt3sas_scsih_enclosure_find_by_handle(ioc,
						sas_device->enclosure_handle);
		if (enclosure_dev) {
			sas_device->enclosure_logical_id =
			    le64_to_cpu(enclosure_dev->pg0.EnclosureLogicalID);
			if (le16_to_cpu(enclosure_dev->pg0.Flags) &
			    MPI2_SAS_ENCLS0_FLAGS_CHASSIS_SLOT_VALID) {
				sas_device->is_chassis_slot_valid = 1;
				sas_device->chassis_slot =
					enclosure_dev->pg0.ChassisSlot;
			}
		}
7124 7125 7126 7127 7128
	}

	/* check if device is present */
	if (!(le16_to_cpu(sas_device_pg0.Flags) &
	    MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
7129 7130
		ioc_err(ioc, "device is not present handle(0x%04x), flags!!!\n",
			handle);
7131
		goto out_unlock;
7132 7133 7134 7135
	}

	/* check if there were any issues with discovery */
	if (_scsih_check_access_status(ioc, sas_address, handle,
7136 7137
	    sas_device_pg0.AccessStatus))
		goto out_unlock;
7138 7139

	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7140
	_scsih_ublock_io_device(ioc, sas_address, port);
7141

7142 7143 7144 7145 7146 7147 7148 7149
	if (sas_device)
		sas_device_put(sas_device);
	return;

out_unlock:
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
	if (sas_device)
		sas_device_put(sas_device);
7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160
}

/**
 * _scsih_add_device -  creating sas device object
 * @ioc: per adapter object
 * @handle: sas device handle
 * @phy_num: phy number end device attached to
 * @is_pd: is this hidden raid component
 *
 * Creating end device object, stored in ioc->sas_device_list.
 *
7161
 * Return: 0 for success, non-zero for failure.
7162 7163 7164 7165 7166 7167 7168 7169
 */
static int
_scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
	u8 is_pd)
{
	Mpi2ConfigReply_t mpi_reply;
	Mpi2SasDevicePage0_t sas_device_pg0;
	struct _sas_device *sas_device;
7170
	struct _enclosure_node *enclosure_dev = NULL;
7171 7172 7173
	u32 ioc_status;
	u64 sas_address;
	u32 device_info;
7174
	u8 port_id;
7175 7176 7177

	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
7178 7179
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
7180 7181 7182 7183 7184 7185
		return -1;
	}

	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
	    MPI2_IOCSTATUS_MASK;
	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7186 7187
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
7188 7189 7190 7191 7192 7193 7194
		return -1;
	}

	/* check if this is end device */
	device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
	if (!(_scsih_is_end_device(device_info)))
		return -1;
7195
	set_bit(handle, ioc->pend_os_device_add);
7196 7197 7198 7199 7200
	sas_address = le64_to_cpu(sas_device_pg0.SASAddress);

	/* check if device is present */
	if (!(le16_to_cpu(sas_device_pg0.Flags) &
	    MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
7201 7202
		ioc_err(ioc, "device is not present handle(0x04%x)!!!\n",
			handle);
7203 7204 7205 7206 7207 7208 7209 7210
		return -1;
	}

	/* check if there were any issues with discovery */
	if (_scsih_check_access_status(ioc, sas_address, handle,
	    sas_device_pg0.AccessStatus))
		return -1;

7211
	port_id = sas_device_pg0.PhysicalPort;
7212
	sas_device = mpt3sas_get_sdev_by_addr(ioc,
7213
	    sas_address, mpt3sas_get_port_by_id(ioc, port_id, 0));
7214
	if (sas_device) {
7215
		clear_bit(handle, ioc->pend_os_device_add);
7216
		sas_device_put(sas_device);
7217
		return -1;
7218
	}
7219

7220
	if (sas_device_pg0.EnclosureHandle) {
7221 7222 7223 7224
		enclosure_dev =
			mpt3sas_scsih_enclosure_find_by_handle(ioc,
			    le16_to_cpu(sas_device_pg0.EnclosureHandle));
		if (enclosure_dev == NULL)
7225 7226
			ioc_info(ioc, "Enclosure handle(0x%04x) doesn't match with enclosure device!\n",
				 sas_device_pg0.EnclosureHandle);
7227 7228
	}

7229 7230 7231
	sas_device = kzalloc(sizeof(struct _sas_device),
	    GFP_KERNEL);
	if (!sas_device) {
7232 7233
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
7234 7235 7236
		return 0;
	}

7237
	kref_init(&sas_device->refcount);
7238 7239 7240 7241
	sas_device->handle = handle;
	if (_scsih_get_sas_address(ioc,
	    le16_to_cpu(sas_device_pg0.ParentDevHandle),
	    &sas_device->sas_address_parent) != 0)
7242 7243
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
7244 7245
	sas_device->enclosure_handle =
	    le16_to_cpu(sas_device_pg0.EnclosureHandle);
7246 7247 7248
	if (sas_device->enclosure_handle != 0)
		sas_device->slot =
		    le16_to_cpu(sas_device_pg0.Slot);
7249 7250 7251 7252 7253
	sas_device->device_info = device_info;
	sas_device->sas_address = sas_address;
	sas_device->phy = sas_device_pg0.PhyNum;
	sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
	    MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
7254
	sas_device->port = mpt3sas_get_port_by_id(ioc, port_id, 0);
7255 7256 7257 7258 7259
	if (!sas_device->port) {
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		goto out;
	}
7260

7261 7262
	if (le16_to_cpu(sas_device_pg0.Flags)
		& MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
7263
		sas_device->enclosure_level =
7264
			sas_device_pg0.EnclosureLevel;
7265 7266 7267
		memcpy(sas_device->connector_name,
			sas_device_pg0.ConnectorName, 4);
		sas_device->connector_name[4] = '\0';
7268 7269 7270 7271
	} else {
		sas_device->enclosure_level = 0;
		sas_device->connector_name[0] = '\0';
	}
7272
	/* get enclosure_logical_id & chassis_slot*/
7273
	sas_device->is_chassis_slot_valid = 0;
7274
	if (enclosure_dev) {
7275
		sas_device->enclosure_logical_id =
7276 7277
		    le64_to_cpu(enclosure_dev->pg0.EnclosureLogicalID);
		if (le16_to_cpu(enclosure_dev->pg0.Flags) &
7278 7279 7280
		    MPI2_SAS_ENCLS0_FLAGS_CHASSIS_SLOT_VALID) {
			sas_device->is_chassis_slot_valid = 1;
			sas_device->chassis_slot =
7281
					enclosure_dev->pg0.ChassisSlot;
7282 7283 7284
		}
	}

7285 7286 7287 7288 7289 7290 7291 7292
	/* get device name */
	sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);

	if (ioc->wait_for_discovery_to_complete)
		_scsih_sas_device_init_add(ioc, sas_device);
	else
		_scsih_sas_device_add(ioc, sas_device);

7293
out:
7294
	sas_device_put(sas_device);
7295 7296 7297 7298 7299 7300
	return 0;
}

/**
 * _scsih_remove_device -  removing sas device object
 * @ioc: per adapter object
7301
 * @sas_device: the sas_device object
7302 7303 7304 7305 7306 7307 7308
 */
static void
_scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
	struct _sas_device *sas_device)
{
	struct MPT3SAS_TARGET *sas_target_priv_data;

7309 7310 7311 7312 7313
	if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
	     (sas_device->pfa_led_on)) {
		_scsih_turn_off_pfa_led(ioc, sas_device);
		sas_device->pfa_led_on = 0;
	}
7314

7315 7316 7317 7318
	dewtprintk(ioc,
		   ioc_info(ioc, "%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
			    __func__,
			    sas_device->handle, (u64)sas_device->sas_address));
7319 7320 7321

	dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
	    NULL, NULL));
7322 7323 7324 7325

	if (sas_device->starget && sas_device->starget->hostdata) {
		sas_target_priv_data = sas_device->starget->hostdata;
		sas_target_priv_data->deleted = 1;
7326 7327
		_scsih_ublock_io_device(ioc, sas_device->sas_address,
		    sas_device->port);
7328 7329 7330
		sas_target_priv_data->handle =
		     MPT3SAS_INVALID_DEVICE_HANDLE;
	}
7331 7332 7333

	if (!ioc->hide_drives)
		mpt3sas_transport_port_remove(ioc,
7334
		    sas_device->sas_address,
7335 7336
		    sas_device->sas_address_parent,
		    sas_device->port);
7337

7338 7339
	ioc_info(ioc, "removing handle(0x%04x), sas_addr(0x%016llx)\n",
		 sas_device->handle, (u64)sas_device->sas_address);
7340 7341

	_scsih_display_enclosure_chassis_info(ioc, sas_device, NULL, NULL);
7342

7343 7344 7345 7346
	dewtprintk(ioc,
		   ioc_info(ioc, "%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
			    __func__,
			    sas_device->handle, (u64)sas_device->sas_address));
7347 7348
	dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
	    NULL, NULL));
7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385
}

/**
 * _scsih_sas_topology_change_event_debug - debug for topology event
 * @ioc: per adapter object
 * @event_data: event data payload
 * Context: user.
 */
static void
_scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
	Mpi2EventDataSasTopologyChangeList_t *event_data)
{
	int i;
	u16 handle;
	u16 reason_code;
	u8 phy_number;
	char *status_str = NULL;
	u8 link_rate, prev_link_rate;

	switch (event_data->ExpStatus) {
	case MPI2_EVENT_SAS_TOPO_ES_ADDED:
		status_str = "add";
		break;
	case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
		status_str = "remove";
		break;
	case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
	case 0:
		status_str =  "responding";
		break;
	case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
		status_str = "remove delay";
		break;
	default:
		status_str = "unknown status";
		break;
	}
7386
	ioc_info(ioc, "sas topology change: (%s)\n", status_str);
7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446
	pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
	    "start_phy(%02d), count(%d)\n",
	    le16_to_cpu(event_data->ExpanderDevHandle),
	    le16_to_cpu(event_data->EnclosureHandle),
	    event_data->StartPhyNum, event_data->NumEntries);
	for (i = 0; i < event_data->NumEntries; i++) {
		handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
		if (!handle)
			continue;
		phy_number = event_data->StartPhyNum + i;
		reason_code = event_data->PHY[i].PhyStatus &
		    MPI2_EVENT_SAS_TOPO_RC_MASK;
		switch (reason_code) {
		case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
			status_str = "target add";
			break;
		case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
			status_str = "target remove";
			break;
		case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
			status_str = "delay target remove";
			break;
		case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
			status_str = "link rate change";
			break;
		case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
			status_str = "target responding";
			break;
		default:
			status_str = "unknown";
			break;
		}
		link_rate = event_data->PHY[i].LinkRate >> 4;
		prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
		pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
		    " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
		    handle, status_str, link_rate, prev_link_rate);

	}
}

/**
 * _scsih_sas_topology_change_event - handle topology changes
 * @ioc: per adapter object
 * @fw_event: The fw_event_work object
 * Context: user.
 *
 */
static int
_scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
	struct fw_event_work *fw_event)
{
	int i;
	u16 parent_handle, handle;
	u16 reason_code;
	u8 phy_number, max_phys;
	struct _sas_node *sas_expander;
	u64 sas_address;
	unsigned long flags;
	u8 link_rate, prev_link_rate;
7447
	struct hba_port *port;
7448 7449 7450
	Mpi2EventDataSasTopologyChangeList_t *event_data =
		(Mpi2EventDataSasTopologyChangeList_t *)
		fw_event->event_data;
7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463

	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
		_scsih_sas_topology_change_event_debug(ioc, event_data);

	if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
		return 0;

	if (!ioc->sas_hba.num_phys)
		_scsih_sas_host_add(ioc);
	else
		_scsih_sas_host_refresh(ioc);

	if (fw_event->ignore) {
7464
		dewtprintk(ioc, ioc_info(ioc, "ignoring expander event\n"));
7465 7466 7467 7468
		return 0;
	}

	parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
7469
	port = mpt3sas_get_port_by_id(ioc, event_data->PhysicalPort, 0);
7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481

	/* handle expander add */
	if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
		if (_scsih_expander_add(ioc, parent_handle) != 0)
			return 0;

	spin_lock_irqsave(&ioc->sas_node_lock, flags);
	sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
	    parent_handle);
	if (sas_expander) {
		sas_address = sas_expander->sas_address;
		max_phys = sas_expander->num_phys;
7482
		port = sas_expander->port;
7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494
	} else if (parent_handle < ioc->sas_hba.num_phys) {
		sas_address = ioc->sas_hba.sas_address;
		max_phys = ioc->sas_hba.num_phys;
	} else {
		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
		return 0;
	}
	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);

	/* handle siblings events */
	for (i = 0; i < event_data->NumEntries; i++) {
		if (fw_event->ignore) {
7495 7496
			dewtprintk(ioc,
				   ioc_info(ioc, "ignoring expander event\n"));
7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524
			return 0;
		}
		if (ioc->remove_host || ioc->pci_error_recovery)
			return 0;
		phy_number = event_data->StartPhyNum + i;
		if (phy_number >= max_phys)
			continue;
		reason_code = event_data->PHY[i].PhyStatus &
		    MPI2_EVENT_SAS_TOPO_RC_MASK;
		if ((event_data->PHY[i].PhyStatus &
		    MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
		    MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
				continue;
		handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
		if (!handle)
			continue;
		link_rate = event_data->PHY[i].LinkRate >> 4;
		prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
		switch (reason_code) {
		case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:

			if (ioc->shost_recovery)
				break;

			if (link_rate == prev_link_rate)
				break;

			mpt3sas_transport_update_links(ioc, sas_address,
7525
			    handle, phy_number, link_rate, port);
7526 7527 7528 7529 7530 7531 7532

			if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
				break;

			_scsih_check_device(ioc, sas_address, handle,
			    phy_number, link_rate);

7533 7534 7535
			if (!test_bit(handle, ioc->pend_os_device_add))
				break;

7536
			fallthrough;
7537 7538 7539 7540 7541 7542 7543

		case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:

			if (ioc->shost_recovery)
				break;

			mpt3sas_transport_update_links(ioc, sas_address,
7544
			    handle, phy_number, link_rate, port);
7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558

			_scsih_add_device(ioc, handle, phy_number, 0);

			break;
		case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:

			_scsih_device_remove_by_handle(ioc, handle);
			break;
		}
	}

	/* handle expander removal */
	if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
	    sas_expander)
7559
		mpt3sas_expander_remove(ioc, sas_address, port);
7560 7561 7562 7563 7564 7565

	return 0;
}

/**
 * _scsih_sas_device_status_change_event_debug - debug for device event
7566
 * @ioc: ?
7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619
 * @event_data: event data payload
 * Context: user.
 */
static void
_scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
	Mpi2EventDataSasDeviceStatusChange_t *event_data)
{
	char *reason_str = NULL;

	switch (event_data->ReasonCode) {
	case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
		reason_str = "smart data";
		break;
	case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
		reason_str = "unsupported device discovered";
		break;
	case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
		reason_str = "internal device reset";
		break;
	case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
		reason_str = "internal task abort";
		break;
	case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
		reason_str = "internal task abort set";
		break;
	case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
		reason_str = "internal clear task set";
		break;
	case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
		reason_str = "internal query task";
		break;
	case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
		reason_str = "sata init failure";
		break;
	case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
		reason_str = "internal device reset complete";
		break;
	case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
		reason_str = "internal task abort complete";
		break;
	case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
		reason_str = "internal async notification";
		break;
	case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
		reason_str = "expander reduced functionality";
		break;
	case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
		reason_str = "expander reduced functionality complete";
		break;
	default:
		reason_str = "unknown reason";
		break;
	}
7620 7621 7622 7623
	ioc_info(ioc, "device status change: (%s)\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
		 reason_str, le16_to_cpu(event_data->DevHandle),
		 (u64)le64_to_cpu(event_data->SASAddress),
		 le16_to_cpu(event_data->TaskTag));
7624
	if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
7625 7626 7627
		pr_cont(", ASC(0x%x), ASCQ(0x%x)\n",
			event_data->ASC, event_data->ASCQ);
	pr_cont("\n");
7628 7629 7630 7631 7632
}

/**
 * _scsih_sas_device_status_change_event - handle device status change
 * @ioc: per adapter object
7633
 * @event_data: The fw event
7634 7635 7636 7637
 * Context: user.
 */
static void
_scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
7638
	Mpi2EventDataSasDeviceStatusChange_t *event_data)
7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658
{
	struct MPT3SAS_TARGET *target_priv_data;
	struct _sas_device *sas_device;
	u64 sas_address;
	unsigned long flags;

	/* In MPI Revision K (0xC), the internal device reset complete was
	 * implemented, so avoid setting tm_busy flag for older firmware.
	 */
	if ((ioc->facts.HeaderVersion >> 8) < 0xC)
		return;

	if (event_data->ReasonCode !=
	    MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
	   event_data->ReasonCode !=
	    MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
		return;

	spin_lock_irqsave(&ioc->sas_device_lock, flags);
	sas_address = le64_to_cpu(event_data->SASAddress);
7659
	sas_device = __mpt3sas_get_sdev_by_addr(ioc,
7660 7661
	    sas_address,
	    mpt3sas_get_port_by_id(ioc, event_data->PhysicalPort, 0));
7662

7663 7664
	if (!sas_device || !sas_device->starget)
		goto out;
7665 7666

	target_priv_data = sas_device->starget->hostdata;
7667 7668
	if (!target_priv_data)
		goto out;
7669 7670 7671 7672 7673 7674

	if (event_data->ReasonCode ==
	    MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
		target_priv_data->tm_busy = 1;
	else
		target_priv_data->tm_busy = 0;
7675

7676 7677 7678 7679 7680 7681
	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
		ioc_info(ioc,
		    "%s tm_busy flag for handle(0x%04x)\n",
		    (target_priv_data->tm_busy == 1) ? "Enable" : "Disable",
		    target_priv_data->handle);

7682 7683 7684 7685
out:
	if (sas_device)
		sas_device_put(sas_device);

7686 7687 7688
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
}

7689

7690 7691 7692 7693 7694
/**
 * _scsih_check_pcie_access_status - check access flags
 * @ioc: per adapter object
 * @wwid: wwid
 * @handle: sas device handle
7695
 * @access_status: errors returned during discovery of the device
7696
 *
7697
 * Return: 0 for success, else failure
7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715
 */
static u8
_scsih_check_pcie_access_status(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
	u16 handle, u8 access_status)
{
	u8 rc = 1;
	char *desc = NULL;

	switch (access_status) {
	case MPI26_PCIEDEV0_ASTATUS_NO_ERRORS:
	case MPI26_PCIEDEV0_ASTATUS_NEEDS_INITIALIZATION:
		rc = 0;
		break;
	case MPI26_PCIEDEV0_ASTATUS_CAPABILITY_FAILED:
		desc = "PCIe device capability failed";
		break;
	case MPI26_PCIEDEV0_ASTATUS_DEVICE_BLOCKED:
		desc = "PCIe device blocked";
7716 7717 7718 7719 7720
		ioc_info(ioc,
		    "Device with Access Status (%s): wwid(0x%016llx), "
		    "handle(0x%04x)\n ll only be added to the internal list",
		    desc, (u64)wwid, handle);
		rc = 0;
7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764
		break;
	case MPI26_PCIEDEV0_ASTATUS_MEMORY_SPACE_ACCESS_FAILED:
		desc = "PCIe device mem space access failed";
		break;
	case MPI26_PCIEDEV0_ASTATUS_UNSUPPORTED_DEVICE:
		desc = "PCIe device unsupported";
		break;
	case MPI26_PCIEDEV0_ASTATUS_MSIX_REQUIRED:
		desc = "PCIe device MSIx Required";
		break;
	case MPI26_PCIEDEV0_ASTATUS_INIT_FAIL_MAX:
		desc = "PCIe device init fail max";
		break;
	case MPI26_PCIEDEV0_ASTATUS_UNKNOWN:
		desc = "PCIe device status unknown";
		break;
	case MPI26_PCIEDEV0_ASTATUS_NVME_READY_TIMEOUT:
		desc = "nvme ready timeout";
		break;
	case MPI26_PCIEDEV0_ASTATUS_NVME_DEVCFG_UNSUPPORTED:
		desc = "nvme device configuration unsupported";
		break;
	case MPI26_PCIEDEV0_ASTATUS_NVME_IDENTIFY_FAILED:
		desc = "nvme identify failed";
		break;
	case MPI26_PCIEDEV0_ASTATUS_NVME_QCONFIG_FAILED:
		desc = "nvme qconfig failed";
		break;
	case MPI26_PCIEDEV0_ASTATUS_NVME_QCREATION_FAILED:
		desc = "nvme qcreation failed";
		break;
	case MPI26_PCIEDEV0_ASTATUS_NVME_EVENTCFG_FAILED:
		desc = "nvme eventcfg failed";
		break;
	case MPI26_PCIEDEV0_ASTATUS_NVME_GET_FEATURE_STAT_FAILED:
		desc = "nvme get feature stat failed";
		break;
	case MPI26_PCIEDEV0_ASTATUS_NVME_IDLE_TIMEOUT:
		desc = "nvme idle timeout";
		break;
	case MPI26_PCIEDEV0_ASTATUS_NVME_FAILURE_STATUS:
		desc = "nvme failure status";
		break;
	default:
7765 7766
		ioc_err(ioc, "NVMe discovery error(0x%02x): wwid(0x%016llx), handle(0x%04x)\n",
			access_status, (u64)wwid, handle);
7767 7768 7769 7770 7771 7772
		return rc;
	}

	if (!rc)
		return rc;

7773 7774
	ioc_info(ioc, "NVMe discovery error(%s): wwid(0x%016llx), handle(0x%04x)\n",
		 desc, (u64)wwid, handle);
7775 7776
	return rc;
}
7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789

/**
 * _scsih_pcie_device_remove_from_sml -  removing pcie device
 * from SML and free up associated memory
 * @ioc: per adapter object
 * @pcie_device: the pcie_device object
 */
static void
_scsih_pcie_device_remove_from_sml(struct MPT3SAS_ADAPTER *ioc,
	struct _pcie_device *pcie_device)
{
	struct MPT3SAS_TARGET *sas_target_priv_data;

7790 7791 7792 7793
	dewtprintk(ioc,
		   ioc_info(ioc, "%s: enter: handle(0x%04x), wwid(0x%016llx)\n",
			    __func__,
			    pcie_device->handle, (u64)pcie_device->wwid));
7794
	if (pcie_device->enclosure_handle != 0)
7795 7796 7797 7798 7799
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
				    __func__,
				    (u64)pcie_device->enclosure_logical_id,
				    pcie_device->slot));
7800
	if (pcie_device->connector_name[0] != '\0')
7801 7802 7803 7804 7805
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: enter: enclosure level(0x%04x), connector name(%s)\n",
				    __func__,
				    pcie_device->enclosure_level,
				    pcie_device->connector_name));
7806 7807 7808 7809

	if (pcie_device->starget && pcie_device->starget->hostdata) {
		sas_target_priv_data = pcie_device->starget->hostdata;
		sas_target_priv_data->deleted = 1;
7810
		_scsih_ublock_io_device(ioc, pcie_device->wwid, NULL);
7811 7812 7813
		sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
	}

7814 7815
	ioc_info(ioc, "removing handle(0x%04x), wwid(0x%016llx)\n",
		 pcie_device->handle, (u64)pcie_device->wwid);
7816
	if (pcie_device->enclosure_handle != 0)
7817 7818 7819
		ioc_info(ioc, "removing : enclosure logical id(0x%016llx), slot(%d)\n",
			 (u64)pcie_device->enclosure_logical_id,
			 pcie_device->slot);
7820
	if (pcie_device->connector_name[0] != '\0')
7821 7822 7823
		ioc_info(ioc, "removing: enclosure level(0x%04x), connector name( %s)\n",
			 pcie_device->enclosure_level,
			 pcie_device->connector_name);
7824

7825 7826
	if (pcie_device->starget && (pcie_device->access_status !=
				MPI26_PCIEDEV0_ASTATUS_DEVICE_BLOCKED))
7827
		scsi_remove_target(&pcie_device->starget->dev);
7828 7829 7830 7831
	dewtprintk(ioc,
		   ioc_info(ioc, "%s: exit: handle(0x%04x), wwid(0x%016llx)\n",
			    __func__,
			    pcie_device->handle, (u64)pcie_device->wwid));
7832
	if (pcie_device->enclosure_handle != 0)
7833 7834 7835 7836 7837
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
				    __func__,
				    (u64)pcie_device->enclosure_logical_id,
				    pcie_device->slot));
7838
	if (pcie_device->connector_name[0] != '\0')
7839 7840 7841 7842 7843
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: exit: enclosure level(0x%04x), connector name( %s)\n",
				    __func__,
				    pcie_device->enclosure_level,
				    pcie_device->connector_name));
7844 7845 7846 7847 7848

	kfree(pcie_device->serial_number);
}


7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876
/**
 * _scsih_pcie_check_device - checking device responsiveness
 * @ioc: per adapter object
 * @handle: attached device handle
 */
static void
_scsih_pcie_check_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	Mpi2ConfigReply_t mpi_reply;
	Mpi26PCIeDevicePage0_t pcie_device_pg0;
	u32 ioc_status;
	struct _pcie_device *pcie_device;
	u64 wwid;
	unsigned long flags;
	struct scsi_target *starget;
	struct MPT3SAS_TARGET *sas_target_priv_data;
	u32 device_info;

	if ((mpt3sas_config_get_pcie_device_pg0(ioc, &mpi_reply,
		&pcie_device_pg0, MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle)))
		return;

	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
		return;

	/* check if this is end device */
	device_info = le32_to_cpu(pcie_device_pg0.DeviceInfo);
7877
	if (!(_scsih_is_nvme_pciescsi_device(device_info)))
7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891
		return;

	wwid = le64_to_cpu(pcie_device_pg0.WWID);
	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
	pcie_device = __mpt3sas_get_pdev_by_wwid(ioc, wwid);

	if (!pcie_device) {
		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
		return;
	}

	if (unlikely(pcie_device->handle != handle)) {
		starget = pcie_device->starget;
		sas_target_priv_data = starget->hostdata;
7892
		pcie_device->access_status = pcie_device_pg0.AccessStatus;
7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913
		starget_printk(KERN_INFO, starget,
		    "handle changed from(0x%04x) to (0x%04x)!!!\n",
		    pcie_device->handle, handle);
		sas_target_priv_data->handle = handle;
		pcie_device->handle = handle;

		if (le32_to_cpu(pcie_device_pg0.Flags) &
		    MPI26_PCIEDEV0_FLAGS_ENCL_LEVEL_VALID) {
			pcie_device->enclosure_level =
			    pcie_device_pg0.EnclosureLevel;
			memcpy(&pcie_device->connector_name[0],
			    &pcie_device_pg0.ConnectorName[0], 4);
		} else {
			pcie_device->enclosure_level = 0;
			pcie_device->connector_name[0] = '\0';
		}
	}

	/* check if device is present */
	if (!(le32_to_cpu(pcie_device_pg0.Flags) &
	    MPI26_PCIEDEV0_FLAGS_DEVICE_PRESENT)) {
7914 7915
		ioc_info(ioc, "device is not present handle(0x%04x), flags!!!\n",
			 handle);
7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931
		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
		pcie_device_put(pcie_device);
		return;
	}

	/* check if there were any issues with discovery */
	if (_scsih_check_pcie_access_status(ioc, wwid, handle,
	    pcie_device_pg0.AccessStatus)) {
		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
		pcie_device_put(pcie_device);
		return;
	}

	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
	pcie_device_put(pcie_device);

7932
	_scsih_ublock_io_device(ioc, wwid, NULL);
7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943

	return;
}

/**
 * _scsih_pcie_add_device -  creating pcie device object
 * @ioc: per adapter object
 * @handle: pcie device handle
 *
 * Creating end device object, stored in ioc->pcie_device_list.
 *
7944
 * Return: 1 means queue the event later, 0 means complete the event
7945 7946 7947 7948 7949 7950 7951 7952
 */
static int
_scsih_pcie_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	Mpi26PCIeDevicePage0_t pcie_device_pg0;
	Mpi26PCIeDevicePage2_t pcie_device_pg2;
	Mpi2ConfigReply_t mpi_reply;
	struct _pcie_device *pcie_device;
7953
	struct _enclosure_node *enclosure_dev;
7954 7955 7956 7957 7958
	u32 ioc_status;
	u64 wwid;

	if ((mpt3sas_config_get_pcie_device_pg0(ioc, &mpi_reply,
	    &pcie_device_pg0, MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
7959 7960
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
7961 7962 7963 7964 7965
		return 0;
	}
	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
	    MPI2_IOCSTATUS_MASK;
	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7966 7967
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
7968 7969 7970 7971 7972 7973 7974 7975 7976
		return 0;
	}

	set_bit(handle, ioc->pend_os_device_add);
	wwid = le64_to_cpu(pcie_device_pg0.WWID);

	/* check if device is present */
	if (!(le32_to_cpu(pcie_device_pg0.Flags) &
		MPI26_PCIEDEV0_FLAGS_DEVICE_PRESENT)) {
7977 7978
		ioc_err(ioc, "device is not present handle(0x04%x)!!!\n",
			handle);
7979 7980 7981 7982 7983 7984 7985 7986
		return 0;
	}

	/* check if there were any issues with discovery */
	if (_scsih_check_pcie_access_status(ioc, wwid, handle,
	    pcie_device_pg0.AccessStatus))
		return 0;

7987 7988
	if (!(_scsih_is_nvme_pciescsi_device(le32_to_cpu
	    (pcie_device_pg0.DeviceInfo))))
7989 7990 7991 7992 7993 7994 7995 7996 7997
		return 0;

	pcie_device = mpt3sas_get_pdev_by_wwid(ioc, wwid);
	if (pcie_device) {
		clear_bit(handle, ioc->pend_os_device_add);
		pcie_device_put(pcie_device);
		return 0;
	}

7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022
	/* PCIe Device Page 2 contains read-only information about a
	 * specific NVMe device; therefore, this page is only
	 * valid for NVMe devices and skip for pcie devices of type scsi.
	 */
	if (!(mpt3sas_scsih_is_pcie_scsi_device(
		le32_to_cpu(pcie_device_pg0.DeviceInfo)))) {
		if (mpt3sas_config_get_pcie_device_pg2(ioc, &mpi_reply,
		    &pcie_device_pg2, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
		    handle)) {
			ioc_err(ioc,
			    "failure at %s:%d/%s()!\n", __FILE__,
			    __LINE__, __func__);
			return 0;
		}

		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
					MPI2_IOCSTATUS_MASK;
		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
			ioc_err(ioc,
			    "failure at %s:%d/%s()!\n", __FILE__,
			    __LINE__, __func__);
			return 0;
		}
	}

8023 8024
	pcie_device = kzalloc(sizeof(struct _pcie_device), GFP_KERNEL);
	if (!pcie_device) {
8025 8026
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
8027 8028 8029 8030 8031 8032 8033
		return 0;
	}

	kref_init(&pcie_device->refcount);
	pcie_device->id = ioc->pcie_target_id++;
	pcie_device->channel = PCIE_CHANNEL;
	pcie_device->handle = handle;
8034
	pcie_device->access_status = pcie_device_pg0.AccessStatus;
8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045
	pcie_device->device_info = le32_to_cpu(pcie_device_pg0.DeviceInfo);
	pcie_device->wwid = wwid;
	pcie_device->port_num = pcie_device_pg0.PortNum;
	pcie_device->fast_path = (le32_to_cpu(pcie_device_pg0.Flags) &
	    MPI26_PCIEDEV0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;

	pcie_device->enclosure_handle =
	    le16_to_cpu(pcie_device_pg0.EnclosureHandle);
	if (pcie_device->enclosure_handle != 0)
		pcie_device->slot = le16_to_cpu(pcie_device_pg0.Slot);

8046
	if (le32_to_cpu(pcie_device_pg0.Flags) &
8047 8048 8049 8050 8051 8052 8053 8054 8055 8056
	    MPI26_PCIEDEV0_FLAGS_ENCL_LEVEL_VALID) {
		pcie_device->enclosure_level = pcie_device_pg0.EnclosureLevel;
		memcpy(&pcie_device->connector_name[0],
		    &pcie_device_pg0.ConnectorName[0], 4);
	} else {
		pcie_device->enclosure_level = 0;
		pcie_device->connector_name[0] = '\0';
	}

	/* get enclosure_logical_id */
8057 8058 8059 8060 8061 8062 8063 8064
	if (pcie_device->enclosure_handle) {
		enclosure_dev =
			mpt3sas_scsih_enclosure_find_by_handle(ioc,
						pcie_device->enclosure_handle);
		if (enclosure_dev)
			pcie_device->enclosure_logical_id =
			    le64_to_cpu(enclosure_dev->pg0.EnclosureLogicalID);
	}
8065
	/* TODO -- Add device name once FW supports it */
8066 8067 8068 8069
	if (!(mpt3sas_scsih_is_pcie_scsi_device(
	    le32_to_cpu(pcie_device_pg0.DeviceInfo)))) {
		pcie_device->nvme_mdts =
		    le32_to_cpu(pcie_device_pg2.MaximumDataTransferSize);
8070 8071 8072 8073 8074 8075 8076 8077 8078 8079
		pcie_device->shutdown_latency =
			le16_to_cpu(pcie_device_pg2.ShutdownLatency);
		/*
		 * Set IOC's max_shutdown_latency to drive's RTD3 Entry Latency
		 * if drive's RTD3 Entry Latency is greater then IOC's
		 * max_shutdown_latency.
		 */
		if (pcie_device->shutdown_latency > ioc->max_shutdown_latency)
			ioc->max_shutdown_latency =
				pcie_device->shutdown_latency;
8080 8081 8082 8083 8084 8085
		if (pcie_device_pg2.ControllerResetTO)
			pcie_device->reset_timeout =
			    pcie_device_pg2.ControllerResetTO;
		else
			pcie_device->reset_timeout = 30;
	} else
8086
		pcie_device->reset_timeout = 30;
8087 8088 8089 8090 8091 8092 8093 8094 8095

	if (ioc->wait_for_discovery_to_complete)
		_scsih_pcie_device_init_add(ioc, pcie_device);
	else
		_scsih_pcie_device_add(ioc, pcie_device);

	pcie_device_put(pcie_device);
	return 0;
}
8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132

/**
 * _scsih_pcie_topology_change_event_debug - debug for topology
 * event
 * @ioc: per adapter object
 * @event_data: event data payload
 * Context: user.
 */
static void
_scsih_pcie_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
	Mpi26EventDataPCIeTopologyChangeList_t *event_data)
{
	int i;
	u16 handle;
	u16 reason_code;
	u8 port_number;
	char *status_str = NULL;
	u8 link_rate, prev_link_rate;

	switch (event_data->SwitchStatus) {
	case MPI26_EVENT_PCIE_TOPO_SS_ADDED:
		status_str = "add";
		break;
	case MPI26_EVENT_PCIE_TOPO_SS_NOT_RESPONDING:
		status_str = "remove";
		break;
	case MPI26_EVENT_PCIE_TOPO_SS_RESPONDING:
	case 0:
		status_str =  "responding";
		break;
	case MPI26_EVENT_PCIE_TOPO_SS_DELAY_NOT_RESPONDING:
		status_str = "remove delay";
		break;
	default:
		status_str = "unknown status";
		break;
	}
8133
	ioc_info(ioc, "pcie topology change: (%s)\n", status_str);
8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183
	pr_info("\tswitch_handle(0x%04x), enclosure_handle(0x%04x)"
		"start_port(%02d), count(%d)\n",
		le16_to_cpu(event_data->SwitchDevHandle),
		le16_to_cpu(event_data->EnclosureHandle),
		event_data->StartPortNum, event_data->NumEntries);
	for (i = 0; i < event_data->NumEntries; i++) {
		handle =
			le16_to_cpu(event_data->PortEntry[i].AttachedDevHandle);
		if (!handle)
			continue;
		port_number = event_data->StartPortNum + i;
		reason_code = event_data->PortEntry[i].PortStatus;
		switch (reason_code) {
		case MPI26_EVENT_PCIE_TOPO_PS_DEV_ADDED:
			status_str = "target add";
			break;
		case MPI26_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
			status_str = "target remove";
			break;
		case MPI26_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
			status_str = "delay target remove";
			break;
		case MPI26_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
			status_str = "link rate change";
			break;
		case MPI26_EVENT_PCIE_TOPO_PS_NO_CHANGE:
			status_str = "target responding";
			break;
		default:
			status_str = "unknown";
			break;
		}
		link_rate = event_data->PortEntry[i].CurrentPortInfo &
			MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
		prev_link_rate = event_data->PortEntry[i].PreviousPortInfo &
			MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
		pr_info("\tport(%02d), attached_handle(0x%04x): %s:"
			" link rate: new(0x%02x), old(0x%02x)\n", port_number,
			handle, status_str, link_rate, prev_link_rate);
	}
}

/**
 * _scsih_pcie_topology_change_event - handle PCIe topology
 *  changes
 * @ioc: per adapter object
 * @fw_event: The fw_event_work object
 * Context: user.
 *
 */
8184
static void
8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202
_scsih_pcie_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
	struct fw_event_work *fw_event)
{
	int i;
	u16 handle;
	u16 reason_code;
	u8 link_rate, prev_link_rate;
	unsigned long flags;
	int rc;
	Mpi26EventDataPCIeTopologyChangeList_t *event_data =
		(Mpi26EventDataPCIeTopologyChangeList_t *) fw_event->event_data;
	struct _pcie_device *pcie_device;

	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
		_scsih_pcie_topology_change_event_debug(ioc, event_data);

	if (ioc->shost_recovery || ioc->remove_host ||
		ioc->pci_error_recovery)
8203
		return;
8204 8205

	if (fw_event->ignore) {
8206
		dewtprintk(ioc, ioc_info(ioc, "ignoring switch event\n"));
8207
		return;
8208 8209 8210 8211 8212
	}

	/* handle siblings events */
	for (i = 0; i < event_data->NumEntries; i++) {
		if (fw_event->ignore) {
8213 8214
			dewtprintk(ioc,
				   ioc_info(ioc, "ignoring switch event\n"));
8215
			return;
8216 8217
		}
		if (ioc->remove_host || ioc->pci_error_recovery)
8218
			return;
8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258
		reason_code = event_data->PortEntry[i].PortStatus;
		handle =
			le16_to_cpu(event_data->PortEntry[i].AttachedDevHandle);
		if (!handle)
			continue;

		link_rate = event_data->PortEntry[i].CurrentPortInfo
			& MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
		prev_link_rate = event_data->PortEntry[i].PreviousPortInfo
			& MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;

		switch (reason_code) {
		case MPI26_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
			if (ioc->shost_recovery)
				break;
			if (link_rate == prev_link_rate)
				break;
			if (link_rate < MPI26_EVENT_PCIE_TOPO_PI_RATE_2_5)
				break;

			_scsih_pcie_check_device(ioc, handle);

			/* This code after this point handles the test case
			 * where a device has been added, however its returning
			 * BUSY for sometime.  Then before the Device Missing
			 * Delay expires and the device becomes READY, the
			 * device is removed and added back.
			 */
			spin_lock_irqsave(&ioc->pcie_device_lock, flags);
			pcie_device = __mpt3sas_get_pdev_by_handle(ioc, handle);
			spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);

			if (pcie_device) {
				pcie_device_put(pcie_device);
				break;
			}

			if (!test_bit(handle, ioc->pend_os_device_add))
				break;

8259 8260 8261
			dewtprintk(ioc,
				   ioc_info(ioc, "handle(0x%04x) device not found: convert event to a device add\n",
					    handle));
8262 8263 8264
			event_data->PortEntry[i].PortStatus &= 0xF0;
			event_data->PortEntry[i].PortStatus |=
				MPI26_EVENT_PCIE_TOPO_PS_DEV_ADDED;
8265
			fallthrough;
8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290
		case MPI26_EVENT_PCIE_TOPO_PS_DEV_ADDED:
			if (ioc->shost_recovery)
				break;
			if (link_rate < MPI26_EVENT_PCIE_TOPO_PI_RATE_2_5)
				break;

			rc = _scsih_pcie_add_device(ioc, handle);
			if (!rc) {
				/* mark entry vacant */
				/* TODO This needs to be reviewed and fixed,
				 * we dont have an entry
				 * to make an event void like vacant
				 */
				event_data->PortEntry[i].PortStatus |=
					MPI26_EVENT_PCIE_TOPO_PS_NO_CHANGE;
			}
			break;
		case MPI26_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
			_scsih_pcie_device_remove_by_handle(ioc, handle);
			break;
		}
	}
}

/**
8291 8292
 * _scsih_pcie_device_status_change_event_debug - debug for device event
 * @ioc: ?
8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335
 * @event_data: event data payload
 * Context: user.
 */
static void
_scsih_pcie_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
	Mpi26EventDataPCIeDeviceStatusChange_t *event_data)
{
	char *reason_str = NULL;

	switch (event_data->ReasonCode) {
	case MPI26_EVENT_PCIDEV_STAT_RC_SMART_DATA:
		reason_str = "smart data";
		break;
	case MPI26_EVENT_PCIDEV_STAT_RC_UNSUPPORTED:
		reason_str = "unsupported device discovered";
		break;
	case MPI26_EVENT_PCIDEV_STAT_RC_INTERNAL_DEVICE_RESET:
		reason_str = "internal device reset";
		break;
	case MPI26_EVENT_PCIDEV_STAT_RC_TASK_ABORT_INTERNAL:
		reason_str = "internal task abort";
		break;
	case MPI26_EVENT_PCIDEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
		reason_str = "internal task abort set";
		break;
	case MPI26_EVENT_PCIDEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
		reason_str = "internal clear task set";
		break;
	case MPI26_EVENT_PCIDEV_STAT_RC_QUERY_TASK_INTERNAL:
		reason_str = "internal query task";
		break;
	case MPI26_EVENT_PCIDEV_STAT_RC_DEV_INIT_FAILURE:
		reason_str = "device init failure";
		break;
	case MPI26_EVENT_PCIDEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
		reason_str = "internal device reset complete";
		break;
	case MPI26_EVENT_PCIDEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
		reason_str = "internal task abort complete";
		break;
	case MPI26_EVENT_PCIDEV_STAT_RC_ASYNC_NOTIFICATION:
		reason_str = "internal async notification";
		break;
8336 8337 8338
	case MPI26_EVENT_PCIDEV_STAT_RC_PCIE_HOT_RESET_FAILED:
		reason_str = "pcie hot reset failed";
		break;
8339 8340 8341 8342 8343
	default:
		reason_str = "unknown reason";
		break;
	}

8344 8345 8346 8347 8348
	ioc_info(ioc, "PCIE device status change: (%s)\n"
		 "\thandle(0x%04x), WWID(0x%016llx), tag(%d)",
		 reason_str, le16_to_cpu(event_data->DevHandle),
		 (u64)le64_to_cpu(event_data->WWID),
		 le16_to_cpu(event_data->TaskTag));
8349
	if (event_data->ReasonCode == MPI26_EVENT_PCIDEV_STAT_RC_SMART_DATA)
8350
		pr_cont(", ASC(0x%x), ASCQ(0x%x)\n",
8351
			event_data->ASC, event_data->ASCQ);
8352
	pr_cont("\n");
8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404
}

/**
 * _scsih_pcie_device_status_change_event - handle device status
 * change
 * @ioc: per adapter object
 * @fw_event: The fw_event_work object
 * Context: user.
 */
static void
_scsih_pcie_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
	struct fw_event_work *fw_event)
{
	struct MPT3SAS_TARGET *target_priv_data;
	struct _pcie_device *pcie_device;
	u64 wwid;
	unsigned long flags;
	Mpi26EventDataPCIeDeviceStatusChange_t *event_data =
		(Mpi26EventDataPCIeDeviceStatusChange_t *)fw_event->event_data;
	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
		_scsih_pcie_device_status_change_event_debug(ioc,
			event_data);

	if (event_data->ReasonCode !=
		MPI26_EVENT_PCIDEV_STAT_RC_INTERNAL_DEVICE_RESET &&
		event_data->ReasonCode !=
		MPI26_EVENT_PCIDEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
		return;

	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
	wwid = le64_to_cpu(event_data->WWID);
	pcie_device = __mpt3sas_get_pdev_by_wwid(ioc, wwid);

	if (!pcie_device || !pcie_device->starget)
		goto out;

	target_priv_data = pcie_device->starget->hostdata;
	if (!target_priv_data)
		goto out;

	if (event_data->ReasonCode ==
		MPI26_EVENT_PCIDEV_STAT_RC_INTERNAL_DEVICE_RESET)
		target_priv_data->tm_busy = 1;
	else
		target_priv_data->tm_busy = 0;
out:
	if (pcie_device)
		pcie_device_put(pcie_device);

	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
}

8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429
/**
 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
 * event
 * @ioc: per adapter object
 * @event_data: event data payload
 * Context: user.
 */
static void
_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
	Mpi2EventDataSasEnclDevStatusChange_t *event_data)
{
	char *reason_str = NULL;

	switch (event_data->ReasonCode) {
	case MPI2_EVENT_SAS_ENCL_RC_ADDED:
		reason_str = "enclosure add";
		break;
	case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
		reason_str = "enclosure remove";
		break;
	default:
		reason_str = "unknown reason";
		break;
	}

8430 8431 8432 8433 8434 8435
	ioc_info(ioc, "enclosure status change: (%s)\n"
		 "\thandle(0x%04x), enclosure logical id(0x%016llx) number slots(%d)\n",
		 reason_str,
		 le16_to_cpu(event_data->EnclosureHandle),
		 (u64)le64_to_cpu(event_data->EnclosureLogicalID),
		 le16_to_cpu(event_data->StartSlot));
8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447
}

/**
 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
 * @ioc: per adapter object
 * @fw_event: The fw_event_work object
 * Context: user.
 */
static void
_scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
	struct fw_event_work *fw_event)
{
8448 8449 8450 8451 8452 8453 8454
	Mpi2ConfigReply_t mpi_reply;
	struct _enclosure_node *enclosure_dev = NULL;
	Mpi2EventDataSasEnclDevStatusChange_t *event_data =
		(Mpi2EventDataSasEnclDevStatusChange_t *)fw_event->event_data;
	int rc;
	u16 enclosure_handle = le16_to_cpu(event_data->EnclosureHandle);

8455 8456
	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
		_scsih_sas_enclosure_dev_status_change_event_debug(ioc,
8457
		     (Mpi2EventDataSasEnclDevStatusChange_t *)
8458
		     fw_event->event_data);
8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472
	if (ioc->shost_recovery)
		return;

	if (enclosure_handle)
		enclosure_dev =
			mpt3sas_scsih_enclosure_find_by_handle(ioc,
						enclosure_handle);
	switch (event_data->ReasonCode) {
	case MPI2_EVENT_SAS_ENCL_RC_ADDED:
		if (!enclosure_dev) {
			enclosure_dev =
				kzalloc(sizeof(struct _enclosure_node),
					GFP_KERNEL);
			if (!enclosure_dev) {
8473 8474
				ioc_info(ioc, "failure at %s:%d/%s()!\n",
					 __FILE__, __LINE__, __func__);
8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500
				return;
			}
			rc = mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
				&enclosure_dev->pg0,
				MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
				enclosure_handle);

			if (rc || (le16_to_cpu(mpi_reply.IOCStatus) &
						MPI2_IOCSTATUS_MASK)) {
				kfree(enclosure_dev);
				return;
			}

			list_add_tail(&enclosure_dev->list,
							&ioc->enclosure_list);
		}
		break;
	case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
		if (enclosure_dev) {
			list_del(&enclosure_dev->list);
			kfree(enclosure_dev);
		}
		break;
	default:
		break;
	}
8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514
}

/**
 * _scsih_sas_broadcast_primitive_event - handle broadcast events
 * @ioc: per adapter object
 * @fw_event: The fw_event_work object
 * Context: user.
 */
static void
_scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
	struct fw_event_work *fw_event)
{
	struct scsi_cmnd *scmd;
	struct scsi_device *sdev;
8515
	struct scsiio_tracker *st;
8516 8517 8518 8519 8520 8521
	u16 smid, handle;
	u32 lun;
	struct MPT3SAS_DEVICE *sas_device_priv_data;
	u32 termination_count;
	u32 query_count;
	Mpi2SCSITaskManagementReply_t *mpi_reply;
8522 8523 8524
	Mpi2EventDataSasBroadcastPrimitive_t *event_data =
		(Mpi2EventDataSasBroadcastPrimitive_t *)
		fw_event->event_data;
8525 8526 8527 8528 8529 8530 8531
	u16 ioc_status;
	unsigned long flags;
	int r;
	u8 max_retries = 0;
	u8 task_abort_retries;

	mutex_lock(&ioc->tm_cmds.mutex);
8532 8533
	ioc_info(ioc, "%s: enter: phy number(%d), width(%d)\n",
		 __func__, event_data->PhyNum, event_data->PortWidth);
8534 8535 8536 8537 8538 8539 8540 8541 8542

	_scsih_block_io_all_device(ioc);

	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
	mpi_reply = ioc->tm_cmds.reply;
 broadcast_aen_retry:

	/* sanity checks for retrying this loop */
	if (max_retries++ == 5) {
8543
		dewtprintk(ioc, ioc_info(ioc, "%s: giving up\n", __func__));
8544 8545
		goto out;
	} else if (max_retries > 1)
8546 8547 8548
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: %d retry\n",
				    __func__, max_retries - 1));
8549 8550 8551 8552 8553 8554

	termination_count = 0;
	query_count = 0;
	for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
		if (ioc->shost_recovery)
			goto out;
8555
		scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
8556 8557
		if (!scmd)
			continue;
8558
		st = scsi_cmd_priv(scmd);
8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570
		sdev = scmd->device;
		sas_device_priv_data = sdev->hostdata;
		if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
			continue;
		 /* skip hidden raid components */
		if (sas_device_priv_data->sas_target->flags &
		    MPT_TARGET_FLAGS_RAID_COMPONENT)
			continue;
		 /* skip volumes */
		if (sas_device_priv_data->sas_target->flags &
		    MPT_TARGET_FLAGS_VOLUME)
			continue;
8571 8572 8573 8574
		 /* skip PCIe devices */
		if (sas_device_priv_data->sas_target->flags &
		    MPT_TARGET_FLAGS_PCIE_DEVICE)
			continue;
8575 8576 8577 8578 8579 8580 8581 8582 8583

		handle = sas_device_priv_data->sas_target->handle;
		lun = sas_device_priv_data->lun;
		query_count++;

		if (ioc->shost_recovery)
			goto out;

		spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
8584
		r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
8585
			MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, st->smid,
8586
			st->msix_io, 30, 0);
8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614
		if (r == FAILED) {
			sdev_printk(KERN_WARNING, sdev,
			    "mpt3sas_scsih_issue_tm: FAILED when sending "
			    "QUERY_TASK: scmd(%p)\n", scmd);
			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
			goto broadcast_aen_retry;
		}
		ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
		    & MPI2_IOCSTATUS_MASK;
		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
			sdev_printk(KERN_WARNING, sdev,
				"query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
				ioc_status, scmd);
			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
			goto broadcast_aen_retry;
		}

		/* see if IO is still owned by IOC and target */
		if (mpi_reply->ResponseCode ==
		     MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
		     mpi_reply->ResponseCode ==
		     MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
			continue;
		}
		task_abort_retries = 0;
 tm_retry:
		if (task_abort_retries++ == 60) {
8615 8616 8617
			dewtprintk(ioc,
				   ioc_info(ioc, "%s: ABORT_TASK: giving up\n",
					    __func__));
8618 8619 8620 8621 8622 8623 8624
			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
			goto broadcast_aen_retry;
		}

		if (ioc->shost_recovery)
			goto out_no_lock;

8625 8626 8627
		r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
			sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
			st->smid, st->msix_io, 30, 0);
8628
		if (r == FAILED || st->cb_idx != 0xFF) {
8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645
			sdev_printk(KERN_WARNING, sdev,
			    "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
			    "scmd(%p)\n", scmd);
			goto tm_retry;
		}

		if (task_abort_retries > 1)
			sdev_printk(KERN_WARNING, sdev,
			    "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
			    " scmd(%p)\n",
			    task_abort_retries - 1, scmd);

		termination_count += le32_to_cpu(mpi_reply->TerminationCount);
		spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
	}

	if (ioc->broadcast_aen_pending) {
8646 8647 8648 8649
		dewtprintk(ioc,
			   ioc_info(ioc,
				    "%s: loop back due to pending AEN\n",
				    __func__));
8650 8651 8652 8653 8654 8655 8656 8657
		 ioc->broadcast_aen_pending = 0;
		 goto broadcast_aen_retry;
	}

 out:
	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
 out_no_lock:

8658 8659 8660
	dewtprintk(ioc,
		   ioc_info(ioc, "%s - exit, query_count = %d termination_count = %d\n",
			    __func__, query_count, termination_count));
8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677

	ioc->broadcast_aen_busy = 0;
	if (!ioc->shost_recovery)
		_scsih_ublock_io_all_device(ioc);
	mutex_unlock(&ioc->tm_cmds.mutex);
}

/**
 * _scsih_sas_discovery_event - handle discovery events
 * @ioc: per adapter object
 * @fw_event: The fw_event_work object
 * Context: user.
 */
static void
_scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
	struct fw_event_work *fw_event)
{
8678 8679
	Mpi2EventDataSasDiscovery_t *event_data =
		(Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
8680 8681

	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
8682 8683 8684
		ioc_info(ioc, "discovery event: (%s)",
			 event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED ?
			 "start" : "stop");
8685
		if (event_data->DiscoveryStatus)
8686 8687 8688
			pr_cont("discovery_status(0x%08x)",
				le32_to_cpu(event_data->DiscoveryStatus));
		pr_cont("\n");
8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701
	}

	if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
	    !ioc->sas_hba.num_phys) {
		if (disable_discovery > 0 && ioc->shost_recovery) {
			/* Wait for the reset to complete */
			while (ioc->shost_recovery)
				ssleep(1);
		}
		_scsih_sas_host_add(ioc);
	}
}

8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717
/**
 * _scsih_sas_device_discovery_error_event - display SAS device discovery error
 *						events
 * @ioc: per adapter object
 * @fw_event: The fw_event_work object
 * Context: user.
 */
static void
_scsih_sas_device_discovery_error_event(struct MPT3SAS_ADAPTER *ioc,
	struct fw_event_work *fw_event)
{
	Mpi25EventDataSasDeviceDiscoveryError_t *event_data =
		(Mpi25EventDataSasDeviceDiscoveryError_t *)fw_event->event_data;

	switch (event_data->ReasonCode) {
	case MPI25_EVENT_SAS_DISC_ERR_SMP_FAILED:
8718 8719 8720 8721
		ioc_warn(ioc, "SMP command sent to the expander (handle:0x%04x, sas_address:0x%016llx, physical_port:0x%02x) has failed\n",
			 le16_to_cpu(event_data->DevHandle),
			 (u64)le64_to_cpu(event_data->SASAddress),
			 event_data->PhysicalPort);
8722 8723
		break;
	case MPI25_EVENT_SAS_DISC_ERR_SMP_TIMEOUT:
8724 8725 8726 8727
		ioc_warn(ioc, "SMP command sent to the expander (handle:0x%04x, sas_address:0x%016llx, physical_port:0x%02x) has timed out\n",
			 le16_to_cpu(event_data->DevHandle),
			 (u64)le64_to_cpu(event_data->SASAddress),
			 event_data->PhysicalPort);
8728 8729 8730 8731 8732 8733
		break;
	default:
		break;
	}
}

8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746
/**
 * _scsih_pcie_enumeration_event - handle enumeration events
 * @ioc: per adapter object
 * @fw_event: The fw_event_work object
 * Context: user.
 */
static void
_scsih_pcie_enumeration_event(struct MPT3SAS_ADAPTER *ioc,
	struct fw_event_work *fw_event)
{
	Mpi26EventDataPCIeEnumeration_t *event_data =
		(Mpi26EventDataPCIeEnumeration_t *)fw_event->event_data;

8747 8748 8749
	if (!(ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK))
		return;

8750 8751 8752 8753
	ioc_info(ioc, "pcie enumeration event: (%s) Flag 0x%02x",
		 (event_data->ReasonCode == MPI26_EVENT_PCIE_ENUM_RC_STARTED) ?
		 "started" : "completed",
		 event_data->Flags);
8754
	if (event_data->EnumerationStatus)
8755 8756 8757
		pr_cont("enumeration_status(0x%08x)",
			le32_to_cpu(event_data->EnumerationStatus));
	pr_cont("\n");
8758 8759
}

8760 8761 8762 8763 8764 8765
/**
 * _scsih_ir_fastpath - turn on fastpath for IR physdisk
 * @ioc: per adapter object
 * @handle: device handle for physical disk
 * @phys_disk_num: physical disk number
 *
8766
 * Return: 0 for success, else failure.
8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778
 */
static int
_scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
{
	Mpi2RaidActionRequest_t *mpi_request;
	Mpi2RaidActionReply_t *mpi_reply;
	u16 smid;
	u8 issue_reset = 0;
	int rc = 0;
	u16 ioc_status;
	u32 log_info;

8779 8780 8781
	if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
		return rc;

8782 8783 8784
	mutex_lock(&ioc->scsih_cmds.mutex);

	if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
8785
		ioc_err(ioc, "%s: scsih_cmd in use\n", __func__);
8786 8787 8788 8789 8790 8791 8792
		rc = -EAGAIN;
		goto out;
	}
	ioc->scsih_cmds.status = MPT3_CMD_PENDING;

	smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
	if (!smid) {
8793
		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806
		ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
		rc = -EAGAIN;
		goto out;
	}

	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
	ioc->scsih_cmds.smid = smid;
	memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));

	mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
	mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
	mpi_request->PhysDiskNum = phys_disk_num;

8807 8808 8809
	dewtprintk(ioc,
		   ioc_info(ioc, "IR RAID_ACTION: turning fast path on for handle(0x%04x), phys_disk_num (0x%02x)\n",
			    handle, phys_disk_num));
8810 8811

	init_completion(&ioc->scsih_cmds.done);
8812
	ioc->put_smid_default(ioc, smid);
8813 8814 8815
	wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);

	if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
8816 8817 8818
		mpt3sas_check_cmd_timeout(ioc,
		    ioc->scsih_cmds.status, mpi_request,
		    sizeof(Mpi2RaidActionRequest_t)/4, issue_reset);
8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832
		rc = -EFAULT;
		goto out;
	}

	if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {

		mpi_reply = ioc->scsih_cmds.reply;
		ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
		if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
			log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
		else
			log_info = 0;
		ioc_status &= MPI2_IOCSTATUS_MASK;
		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
8833 8834 8835
			dewtprintk(ioc,
				   ioc_info(ioc, "IR RAID_ACTION: failed: ioc_status(0x%04x), loginfo(0x%08x)!!!\n",
					    ioc_status, log_info));
8836 8837
			rc = -EFAULT;
		} else
8838 8839
			dewtprintk(ioc,
				   ioc_info(ioc, "IR RAID_ACTION: completed successfully\n"));
8840 8841 8842 8843 8844 8845 8846
	}

 out:
	ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
	mutex_unlock(&ioc->scsih_cmds.mutex);

	if (issue_reset)
8847
		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861
	return rc;
}

/**
 * _scsih_reprobe_lun - reprobing lun
 * @sdev: scsi device struct
 * @no_uld_attach: sdev->no_uld_attach flag setting
 *
 **/
static void
_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
{
	sdev->no_uld_attach = no_uld_attach ? 1 : 0;
	sdev_printk(KERN_INFO, sdev, "%s raid component\n",
8862
	    sdev->no_uld_attach ? "hiding" : "exposing");
8863
	WARN_ON(scsi_device_reprobe(sdev));
8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883
}

/**
 * _scsih_sas_volume_add - add new volume
 * @ioc: per adapter object
 * @element: IR config element data
 * Context: user.
 */
static void
_scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
	Mpi2EventIrConfigElement_t *element)
{
	struct _raid_device *raid_device;
	unsigned long flags;
	u64 wwid;
	u16 handle = le16_to_cpu(element->VolDevHandle);
	int rc;

	mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
	if (!wwid) {
8884 8885
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897
		return;
	}

	spin_lock_irqsave(&ioc->raid_device_lock, flags);
	raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);

	if (raid_device)
		return;

	raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
	if (!raid_device) {
8898 8899
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934
		return;
	}

	raid_device->id = ioc->sas_id++;
	raid_device->channel = RAID_CHANNEL;
	raid_device->handle = handle;
	raid_device->wwid = wwid;
	_scsih_raid_device_add(ioc, raid_device);
	if (!ioc->wait_for_discovery_to_complete) {
		rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
		    raid_device->id, 0);
		if (rc)
			_scsih_raid_device_remove(ioc, raid_device);
	} else {
		spin_lock_irqsave(&ioc->raid_device_lock, flags);
		_scsih_determine_boot_device(ioc, raid_device, 1);
		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
	}
}

/**
 * _scsih_sas_volume_delete - delete volume
 * @ioc: per adapter object
 * @handle: volume device handle
 * Context: user.
 */
static void
_scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
{
	struct _raid_device *raid_device;
	unsigned long flags;
	struct MPT3SAS_TARGET *sas_target_priv_data;
	struct scsi_target *starget = NULL;

	spin_lock_irqsave(&ioc->raid_device_lock, flags);
8935
	raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
8936 8937 8938 8939 8940 8941
	if (raid_device) {
		if (raid_device->starget) {
			starget = raid_device->starget;
			sas_target_priv_data = starget->hostdata;
			sas_target_priv_data->deleted = 1;
		}
8942 8943
		ioc_info(ioc, "removing handle(0x%04x), wwid(0x%016llx)\n",
			 raid_device->handle, (u64)raid_device->wwid);
8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968
		list_del(&raid_device->list);
		kfree(raid_device);
	}
	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
	if (starget)
		scsi_remove_target(&starget->dev);
}

/**
 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
 * @ioc: per adapter object
 * @element: IR config element data
 * Context: user.
 */
static void
_scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
	Mpi2EventIrConfigElement_t *element)
{
	struct _sas_device *sas_device;
	struct scsi_target *starget = NULL;
	struct MPT3SAS_TARGET *sas_target_priv_data;
	unsigned long flags;
	u16 handle = le16_to_cpu(element->PhysDiskDevHandle);

	spin_lock_irqsave(&ioc->sas_device_lock, flags);
8969
	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987
	if (sas_device) {
		sas_device->volume_handle = 0;
		sas_device->volume_wwid = 0;
		clear_bit(handle, ioc->pd_handles);
		if (sas_device->starget && sas_device->starget->hostdata) {
			starget = sas_device->starget;
			sas_target_priv_data = starget->hostdata;
			sas_target_priv_data->flags &=
			    ~MPT_TARGET_FLAGS_RAID_COMPONENT;
		}
	}
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
	if (!sas_device)
		return;

	/* exposing raid component */
	if (starget)
		starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
8988 8989

	sas_device_put(sas_device);
8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015
}

/**
 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
 * @ioc: per adapter object
 * @element: IR config element data
 * Context: user.
 */
static void
_scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
	Mpi2EventIrConfigElement_t *element)
{
	struct _sas_device *sas_device;
	struct scsi_target *starget = NULL;
	struct MPT3SAS_TARGET *sas_target_priv_data;
	unsigned long flags;
	u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
	u16 volume_handle = 0;
	u64 volume_wwid = 0;

	mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
	if (volume_handle)
		mpt3sas_config_get_volume_wwid(ioc, volume_handle,
		    &volume_wwid);

	spin_lock_irqsave(&ioc->sas_device_lock, flags);
9016
	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033
	if (sas_device) {
		set_bit(handle, ioc->pd_handles);
		if (sas_device->starget && sas_device->starget->hostdata) {
			starget = sas_device->starget;
			sas_target_priv_data = starget->hostdata;
			sas_target_priv_data->flags |=
			    MPT_TARGET_FLAGS_RAID_COMPONENT;
			sas_device->volume_handle = volume_handle;
			sas_device->volume_wwid = volume_wwid;
		}
	}
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
	if (!sas_device)
		return;

	/* hiding raid component */
	_scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
9034

9035 9036
	if (starget)
		starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
9037 9038

	sas_device_put(sas_device);
9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075
}

/**
 * _scsih_sas_pd_delete - delete pd component
 * @ioc: per adapter object
 * @element: IR config element data
 * Context: user.
 */
static void
_scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
	Mpi2EventIrConfigElement_t *element)
{
	u16 handle = le16_to_cpu(element->PhysDiskDevHandle);

	_scsih_device_remove_by_handle(ioc, handle);
}

/**
 * _scsih_sas_pd_add - remove pd component
 * @ioc: per adapter object
 * @element: IR config element data
 * Context: user.
 */
static void
_scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
	Mpi2EventIrConfigElement_t *element)
{
	struct _sas_device *sas_device;
	u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
	Mpi2ConfigReply_t mpi_reply;
	Mpi2SasDevicePage0_t sas_device_pg0;
	u32 ioc_status;
	u64 sas_address;
	u16 parent_handle;

	set_bit(handle, ioc->pd_handles);

9076
	sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
9077 9078
	if (sas_device) {
		_scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
9079
		sas_device_put(sas_device);
9080 9081 9082 9083 9084
		return;
	}

	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
9085 9086
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
9087 9088 9089 9090 9091 9092
		return;
	}

	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
	    MPI2_IOCSTATUS_MASK;
	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
9093 9094
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
9095 9096 9097 9098 9099 9100
		return;
	}

	parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
	if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
		mpt3sas_transport_update_links(ioc, sas_address, handle,
9101
		    sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5,
9102 9103
		    mpt3sas_get_port_by_id(ioc,
		    sas_device_pg0.PhysicalPort, 0));
9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125

	_scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
	_scsih_add_device(ioc, handle, 0, 1);
}

/**
 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
 * @ioc: per adapter object
 * @event_data: event data payload
 * Context: user.
 */
static void
_scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
	Mpi2EventDataIrConfigChangeList_t *event_data)
{
	Mpi2EventIrConfigElement_t *element;
	u8 element_type;
	int i;
	char *reason_str = NULL, *element_str = NULL;

	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];

9126 9127 9128 9129
	ioc_info(ioc, "raid config change: (%s), elements(%d)\n",
		 le32_to_cpu(event_data->Flags) & MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG ?
		 "foreign" : "native",
		 event_data->NumElements);
9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199
	for (i = 0; i < event_data->NumElements; i++, element++) {
		switch (element->ReasonCode) {
		case MPI2_EVENT_IR_CHANGE_RC_ADDED:
			reason_str = "add";
			break;
		case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
			reason_str = "remove";
			break;
		case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
			reason_str = "no change";
			break;
		case MPI2_EVENT_IR_CHANGE_RC_HIDE:
			reason_str = "hide";
			break;
		case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
			reason_str = "unhide";
			break;
		case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
			reason_str = "volume_created";
			break;
		case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
			reason_str = "volume_deleted";
			break;
		case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
			reason_str = "pd_created";
			break;
		case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
			reason_str = "pd_deleted";
			break;
		default:
			reason_str = "unknown reason";
			break;
		}
		element_type = le16_to_cpu(element->ElementFlags) &
		    MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
		switch (element_type) {
		case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
			element_str = "volume";
			break;
		case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
			element_str = "phys disk";
			break;
		case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
			element_str = "hot spare";
			break;
		default:
			element_str = "unknown element";
			break;
		}
		pr_info("\t(%s:%s), vol handle(0x%04x), " \
		    "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
		    reason_str, le16_to_cpu(element->VolDevHandle),
		    le16_to_cpu(element->PhysDiskDevHandle),
		    element->PhysDiskNum);
	}
}

/**
 * _scsih_sas_ir_config_change_event - handle ir configuration change events
 * @ioc: per adapter object
 * @fw_event: The fw_event_work object
 * Context: user.
 */
static void
_scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
	struct fw_event_work *fw_event)
{
	Mpi2EventIrConfigElement_t *element;
	int i;
	u8 foreign_config;
9200 9201 9202
	Mpi2EventDataIrConfigChangeList_t *event_data =
		(Mpi2EventDataIrConfigChangeList_t *)
		fw_event->event_data;
9203

9204 9205
	if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
	     (!ioc->hide_ir_msg))
9206 9207 9208 9209 9210 9211
		_scsih_sas_ir_config_change_event_debug(ioc, event_data);

	foreign_config = (le32_to_cpu(event_data->Flags) &
	    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;

	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
9212 9213
	if (ioc->shost_recovery &&
	    ioc->hba_mpi_version_belonged != MPI2_VERSION) {
9214 9215 9216 9217 9218 9219 9220 9221
		for (i = 0; i < event_data->NumElements; i++, element++) {
			if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
				_scsih_ir_fastpath(ioc,
					le16_to_cpu(element->PhysDiskDevHandle),
					element->PhysDiskNum);
		}
		return;
	}
9222

9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237
	for (i = 0; i < event_data->NumElements; i++, element++) {

		switch (element->ReasonCode) {
		case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
		case MPI2_EVENT_IR_CHANGE_RC_ADDED:
			if (!foreign_config)
				_scsih_sas_volume_add(ioc, element);
			break;
		case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
		case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
			if (!foreign_config)
				_scsih_sas_volume_delete(ioc,
				    le16_to_cpu(element->VolDevHandle));
			break;
		case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
9238 9239
			if (!ioc->is_warpdrive)
				_scsih_sas_pd_hide(ioc, element);
9240 9241
			break;
		case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
9242 9243
			if (!ioc->is_warpdrive)
				_scsih_sas_pd_expose(ioc, element);
9244 9245
			break;
		case MPI2_EVENT_IR_CHANGE_RC_HIDE:
9246 9247
			if (!ioc->is_warpdrive)
				_scsih_sas_pd_add(ioc, element);
9248 9249
			break;
		case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
9250 9251
			if (!ioc->is_warpdrive)
				_scsih_sas_pd_delete(ioc, element);
9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272
			break;
		}
	}
}

/**
 * _scsih_sas_ir_volume_event - IR volume event
 * @ioc: per adapter object
 * @fw_event: The fw_event_work object
 * Context: user.
 */
static void
_scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
	struct fw_event_work *fw_event)
{
	u64 wwid;
	unsigned long flags;
	struct _raid_device *raid_device;
	u16 handle;
	u32 state;
	int rc;
9273 9274
	Mpi2EventDataIrVolume_t *event_data =
		(Mpi2EventDataIrVolume_t *) fw_event->event_data;
9275 9276 9277 9278 9279 9280 9281 9282 9283

	if (ioc->shost_recovery)
		return;

	if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
		return;

	handle = le16_to_cpu(event_data->VolDevHandle);
	state = le32_to_cpu(event_data->NewValue);
9284
	if (!ioc->hide_ir_msg)
9285 9286 9287 9288 9289
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
				    __func__, handle,
				    le32_to_cpu(event_data->PreviousValue),
				    state));
9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300
	switch (state) {
	case MPI2_RAID_VOL_STATE_MISSING:
	case MPI2_RAID_VOL_STATE_FAILED:
		_scsih_sas_volume_delete(ioc, handle);
		break;

	case MPI2_RAID_VOL_STATE_ONLINE:
	case MPI2_RAID_VOL_STATE_DEGRADED:
	case MPI2_RAID_VOL_STATE_OPTIMAL:

		spin_lock_irqsave(&ioc->raid_device_lock, flags);
9301
		raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
9302 9303 9304 9305 9306 9307 9308
		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);

		if (raid_device)
			break;

		mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
		if (!wwid) {
9309 9310
			ioc_err(ioc, "failure at %s:%d/%s()!\n",
				__FILE__, __LINE__, __func__);
9311 9312 9313 9314 9315
			break;
		}

		raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
		if (!raid_device) {
9316 9317
			ioc_err(ioc, "failure at %s:%d/%s()!\n",
				__FILE__, __LINE__, __func__);
9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353
			break;
		}

		raid_device->id = ioc->sas_id++;
		raid_device->channel = RAID_CHANNEL;
		raid_device->handle = handle;
		raid_device->wwid = wwid;
		_scsih_raid_device_add(ioc, raid_device);
		rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
		    raid_device->id, 0);
		if (rc)
			_scsih_raid_device_remove(ioc, raid_device);
		break;

	case MPI2_RAID_VOL_STATE_INITIALIZING:
	default:
		break;
	}
}

/**
 * _scsih_sas_ir_physical_disk_event - PD event
 * @ioc: per adapter object
 * @fw_event: The fw_event_work object
 * Context: user.
 */
static void
_scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
	struct fw_event_work *fw_event)
{
	u16 handle, parent_handle;
	u32 state;
	struct _sas_device *sas_device;
	Mpi2ConfigReply_t mpi_reply;
	Mpi2SasDevicePage0_t sas_device_pg0;
	u32 ioc_status;
9354 9355
	Mpi2EventDataIrPhysicalDisk_t *event_data =
		(Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366
	u64 sas_address;

	if (ioc->shost_recovery)
		return;

	if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
		return;

	handle = le16_to_cpu(event_data->PhysDiskDevHandle);
	state = le32_to_cpu(event_data->NewValue);

9367
	if (!ioc->hide_ir_msg)
9368 9369 9370 9371 9372
		dewtprintk(ioc,
			   ioc_info(ioc, "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
				    __func__, handle,
				    le32_to_cpu(event_data->PreviousValue),
				    state));
9373

9374 9375 9376 9377 9378 9379 9380
	switch (state) {
	case MPI2_RAID_PD_STATE_ONLINE:
	case MPI2_RAID_PD_STATE_DEGRADED:
	case MPI2_RAID_PD_STATE_REBUILDING:
	case MPI2_RAID_PD_STATE_OPTIMAL:
	case MPI2_RAID_PD_STATE_HOT_SPARE:

9381 9382
		if (!ioc->is_warpdrive)
			set_bit(handle, ioc->pd_handles);
9383

9384 9385 9386
		sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
		if (sas_device) {
			sas_device_put(sas_device);
9387
			return;
9388
		}
9389 9390 9391 9392

		if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
		    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
		    handle))) {
9393 9394
			ioc_err(ioc, "failure at %s:%d/%s()!\n",
				__FILE__, __LINE__, __func__);
9395 9396 9397 9398 9399 9400
			return;
		}

		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
		    MPI2_IOCSTATUS_MASK;
		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
9401 9402
			ioc_err(ioc, "failure at %s:%d/%s()!\n",
				__FILE__, __LINE__, __func__);
9403 9404 9405 9406 9407 9408
			return;
		}

		parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
		if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
			mpt3sas_transport_update_links(ioc, sas_address, handle,
9409 9410
			    sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5,
			    mpt3sas_get_port_by_id(ioc,
9411
			    sas_device_pg0.PhysicalPort, 0));
9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457

		_scsih_add_device(ioc, handle, 0, 1);

		break;

	case MPI2_RAID_PD_STATE_OFFLINE:
	case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
	case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
	default:
		break;
	}
}

/**
 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
 * @ioc: per adapter object
 * @event_data: event data payload
 * Context: user.
 */
static void
_scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
	Mpi2EventDataIrOperationStatus_t *event_data)
{
	char *reason_str = NULL;

	switch (event_data->RAIDOperation) {
	case MPI2_EVENT_IR_RAIDOP_RESYNC:
		reason_str = "resync";
		break;
	case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
		reason_str = "online capacity expansion";
		break;
	case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
		reason_str = "consistency check";
		break;
	case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
		reason_str = "background init";
		break;
	case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
		reason_str = "make data consistent";
		break;
	}

	if (!reason_str)
		return;

9458 9459 9460 9461
	ioc_info(ioc, "raid operational status: (%s)\thandle(0x%04x), percent complete(%d)\n",
		 reason_str,
		 le16_to_cpu(event_data->VolDevHandle),
		 event_data->PercentComplete);
9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473
}

/**
 * _scsih_sas_ir_operation_status_event - handle RAID operation events
 * @ioc: per adapter object
 * @fw_event: The fw_event_work object
 * Context: user.
 */
static void
_scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
	struct fw_event_work *fw_event)
{
9474 9475 9476
	Mpi2EventDataIrOperationStatus_t *event_data =
		(Mpi2EventDataIrOperationStatus_t *)
		fw_event->event_data;
9477 9478 9479 9480
	static struct _raid_device *raid_device;
	unsigned long flags;
	u16 handle;

9481 9482
	if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
	    (!ioc->hide_ir_msg))
9483 9484 9485 9486 9487 9488 9489 9490
		_scsih_sas_ir_operation_status_event_debug(ioc,
		     event_data);

	/* code added for raid transport support */
	if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {

		spin_lock_irqsave(&ioc->raid_device_lock, flags);
		handle = le16_to_cpu(event_data->VolDevHandle);
9491
		raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521
		if (raid_device)
			raid_device->percent_complete =
			    event_data->PercentComplete;
		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
	}
}

/**
 * _scsih_prep_device_scan - initialize parameters prior to device scan
 * @ioc: per adapter object
 *
 * Set the deleted flag prior to device scan.  If the device is found during
 * the scan, then we clear the deleted flag.
 */
static void
_scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
{
	struct MPT3SAS_DEVICE *sas_device_priv_data;
	struct scsi_device *sdev;

	shost_for_each_device(sdev, ioc->shost) {
		sas_device_priv_data = sdev->hostdata;
		if (sas_device_priv_data && sas_device_priv_data->sas_target)
			sas_device_priv_data->sas_target->deleted = 1;
	}
}

/**
 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
 * @ioc: per adapter object
9522
 * @sas_device_pg0: SAS Device page 0
9523 9524 9525 9526 9527
 *
 * After host reset, find out whether devices are still responding.
 * Used in _scsih_remove_unresponsive_sas_devices.
 */
static void
9528 9529
_scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
Mpi2SasDevicePage0_t *sas_device_pg0)
9530 9531 9532
{
	struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
	struct scsi_target *starget;
9533
	struct _sas_device *sas_device = NULL;
9534
	struct _enclosure_node *enclosure_dev = NULL;
9535
	unsigned long flags;
9536
	struct hba_port *port = mpt3sas_get_port_by_id(
9537
	    ioc, sas_device_pg0->PhysicalPort, 0);
9538

9539 9540 9541 9542 9543
	if (sas_device_pg0->EnclosureHandle) {
		enclosure_dev =
			mpt3sas_scsih_enclosure_find_by_handle(ioc,
				le16_to_cpu(sas_device_pg0->EnclosureHandle));
		if (enclosure_dev == NULL)
9544 9545
			ioc_info(ioc, "Enclosure handle(0x%04x) doesn't match with enclosure device!\n",
				 sas_device_pg0->EnclosureHandle);
9546
	}
9547 9548
	spin_lock_irqsave(&ioc->sas_device_lock, flags);
	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
9549 9550 9551 9552 9553
		if (sas_device->sas_address != le64_to_cpu(
		    sas_device_pg0->SASAddress))
			continue;
		if (sas_device->slot != le16_to_cpu(sas_device_pg0->Slot))
			continue;
9554 9555
		if (sas_device->port != port)
			continue;
9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571
		sas_device->responding = 1;
		starget = sas_device->starget;
		if (starget && starget->hostdata) {
			sas_target_priv_data = starget->hostdata;
			sas_target_priv_data->tm_busy = 0;
			sas_target_priv_data->deleted = 0;
		} else
			sas_target_priv_data = NULL;
		if (starget) {
			starget_printk(KERN_INFO, starget,
			    "handle(0x%04x), sas_addr(0x%016llx)\n",
			    le16_to_cpu(sas_device_pg0->DevHandle),
			    (unsigned long long)
			    sas_device->sas_address);

			if (sas_device->enclosure_handle != 0)
9572
				starget_printk(KERN_INFO, starget,
9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587
				 "enclosure logical id(0x%016llx), slot(%d)\n",
				 (unsigned long long)
				 sas_device->enclosure_logical_id,
				 sas_device->slot);
		}
		if (le16_to_cpu(sas_device_pg0->Flags) &
		      MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
			sas_device->enclosure_level =
			   sas_device_pg0->EnclosureLevel;
			memcpy(&sas_device->connector_name[0],
				&sas_device_pg0->ConnectorName[0], 4);
		} else {
			sas_device->enclosure_level = 0;
			sas_device->connector_name[0] = '\0';
		}
9588

9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599
		sas_device->enclosure_handle =
			le16_to_cpu(sas_device_pg0->EnclosureHandle);
		sas_device->is_chassis_slot_valid = 0;
		if (enclosure_dev) {
			sas_device->enclosure_logical_id = le64_to_cpu(
				enclosure_dev->pg0.EnclosureLogicalID);
			if (le16_to_cpu(enclosure_dev->pg0.Flags) &
			    MPI2_SAS_ENCLS0_FLAGS_CHASSIS_SLOT_VALID) {
				sas_device->is_chassis_slot_valid = 1;
				sas_device->chassis_slot =
					enclosure_dev->pg0.ChassisSlot;
9600
			}
9601
		}
9602

9603 9604
		if (sas_device->handle == le16_to_cpu(
		    sas_device_pg0->DevHandle))
9605
			goto out;
9606 9607 9608 9609 9610 9611 9612 9613
		pr_info("\thandle changed from(0x%04x)!!!\n",
		    sas_device->handle);
		sas_device->handle = le16_to_cpu(
		    sas_device_pg0->DevHandle);
		if (sas_target_priv_data)
			sas_target_priv_data->handle =
			    le16_to_cpu(sas_device_pg0->DevHandle);
		goto out;
9614 9615 9616 9617 9618
	}
 out:
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
}

9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640
/**
 * _scsih_create_enclosure_list_after_reset - Free Existing list,
 *	And create enclosure list by scanning all Enclosure Page(0)s
 * @ioc: per adapter object
 */
static void
_scsih_create_enclosure_list_after_reset(struct MPT3SAS_ADAPTER *ioc)
{
	struct _enclosure_node *enclosure_dev;
	Mpi2ConfigReply_t mpi_reply;
	u16 enclosure_handle;
	int rc;

	/* Free existing enclosure list */
	mpt3sas_free_enclosure_list(ioc);

	/* Re constructing enclosure list after reset*/
	enclosure_handle = 0xFFFF;
	do {
		enclosure_dev =
			kzalloc(sizeof(struct _enclosure_node), GFP_KERNEL);
		if (!enclosure_dev) {
9641
			ioc_err(ioc, "failure at %s:%d/%s()!\n",
9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661
				__FILE__, __LINE__, __func__);
			return;
		}
		rc = mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
				&enclosure_dev->pg0,
				MPI2_SAS_ENCLOS_PGAD_FORM_GET_NEXT_HANDLE,
				enclosure_handle);

		if (rc || (le16_to_cpu(mpi_reply.IOCStatus) &
						MPI2_IOCSTATUS_MASK)) {
			kfree(enclosure_dev);
			return;
		}
		list_add_tail(&enclosure_dev->list,
						&ioc->enclosure_list);
		enclosure_handle =
			le16_to_cpu(enclosure_dev->pg0.EnclosureHandle);
	} while (1);
}

9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677
/**
 * _scsih_search_responding_sas_devices -
 * @ioc: per adapter object
 *
 * After host reset, find out whether devices are still responding.
 * If not remove.
 */
static void
_scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
{
	Mpi2SasDevicePage0_t sas_device_pg0;
	Mpi2ConfigReply_t mpi_reply;
	u16 ioc_status;
	u16 handle;
	u32 device_info;

9678
	ioc_info(ioc, "search for end-devices: start\n");
9679 9680 9681 9682 9683 9684 9685 9686 9687 9688

	if (list_empty(&ioc->sas_device_list))
		goto out;

	handle = 0xFFFF;
	while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
	    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
	    handle))) {
		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
		    MPI2_IOCSTATUS_MASK;
9689
		if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
9690
			break;
9691
		handle = le16_to_cpu(sas_device_pg0.DevHandle);
9692 9693 9694
		device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
		if (!(_scsih_is_end_device(device_info)))
			continue;
9695
		_scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
9696 9697 9698
	}

 out:
9699
	ioc_info(ioc, "search for end-devices: complete\n");
9700 9701
}

9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720
/**
 * _scsih_mark_responding_pcie_device - mark a pcie_device as responding
 * @ioc: per adapter object
 * @pcie_device_pg0: PCIe Device page 0
 *
 * After host reset, find out whether devices are still responding.
 * Used in _scsih_remove_unresponding_devices.
 */
static void
_scsih_mark_responding_pcie_device(struct MPT3SAS_ADAPTER *ioc,
	Mpi26PCIeDevicePage0_t *pcie_device_pg0)
{
	struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
	struct scsi_target *starget;
	struct _pcie_device *pcie_device;
	unsigned long flags;

	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
	list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
9721 9722 9723
		if ((pcie_device->wwid == le64_to_cpu(pcie_device_pg0->WWID))
		    && (pcie_device->slot == le16_to_cpu(
		    pcie_device_pg0->Slot))) {
9724 9725
			pcie_device->access_status =
					pcie_device_pg0->AccessStatus;
9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759
			pcie_device->responding = 1;
			starget = pcie_device->starget;
			if (starget && starget->hostdata) {
				sas_target_priv_data = starget->hostdata;
				sas_target_priv_data->tm_busy = 0;
				sas_target_priv_data->deleted = 0;
			} else
				sas_target_priv_data = NULL;
			if (starget) {
				starget_printk(KERN_INFO, starget,
				    "handle(0x%04x), wwid(0x%016llx) ",
				    pcie_device->handle,
				    (unsigned long long)pcie_device->wwid);
				if (pcie_device->enclosure_handle != 0)
					starget_printk(KERN_INFO, starget,
					    "enclosure logical id(0x%016llx), "
					    "slot(%d)\n",
					    (unsigned long long)
					    pcie_device->enclosure_logical_id,
					    pcie_device->slot);
			}

			if (((le32_to_cpu(pcie_device_pg0->Flags)) &
			    MPI26_PCIEDEV0_FLAGS_ENCL_LEVEL_VALID) &&
			    (ioc->hba_mpi_version_belonged != MPI2_VERSION)) {
				pcie_device->enclosure_level =
				    pcie_device_pg0->EnclosureLevel;
				memcpy(&pcie_device->connector_name[0],
				    &pcie_device_pg0->ConnectorName[0], 4);
			} else {
				pcie_device->enclosure_level = 0;
				pcie_device->connector_name[0] = '\0';
			}

9760 9761
			if (pcie_device->handle == le16_to_cpu(
			    pcie_device_pg0->DevHandle))
9762
				goto out;
9763
			pr_info("\thandle changed from(0x%04x)!!!\n",
9764
			    pcie_device->handle);
9765 9766
			pcie_device->handle = le16_to_cpu(
			    pcie_device_pg0->DevHandle);
9767 9768
			if (sas_target_priv_data)
				sas_target_priv_data->handle =
9769
				    le16_to_cpu(pcie_device_pg0->DevHandle);
9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793
			goto out;
		}
	}

 out:
	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
}

/**
 * _scsih_search_responding_pcie_devices -
 * @ioc: per adapter object
 *
 * After host reset, find out whether devices are still responding.
 * If not remove.
 */
static void
_scsih_search_responding_pcie_devices(struct MPT3SAS_ADAPTER *ioc)
{
	Mpi26PCIeDevicePage0_t pcie_device_pg0;
	Mpi2ConfigReply_t mpi_reply;
	u16 ioc_status;
	u16 handle;
	u32 device_info;

9794
	ioc_info(ioc, "search for end-devices: start\n");
9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805

	if (list_empty(&ioc->pcie_device_list))
		goto out;

	handle = 0xFFFF;
	while (!(mpt3sas_config_get_pcie_device_pg0(ioc, &mpi_reply,
		&pcie_device_pg0, MPI26_PCIE_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
		handle))) {
		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
		    MPI2_IOCSTATUS_MASK;
		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
9806 9807 9808
			ioc_info(ioc, "\tbreak from %s: ioc_status(0x%04x), loginfo(0x%08x)\n",
				 __func__, ioc_status,
				 le32_to_cpu(mpi_reply.IOCLogInfo));
9809 9810 9811 9812
			break;
		}
		handle = le16_to_cpu(pcie_device_pg0.DevHandle);
		device_info = le32_to_cpu(pcie_device_pg0.DeviceInfo);
9813
		if (!(_scsih_is_nvme_pciescsi_device(device_info)))
9814 9815 9816 9817
			continue;
		_scsih_mark_responding_pcie_device(ioc, &pcie_device_pg0);
	}
out:
9818
	ioc_info(ioc, "search for PCIe end-devices: complete\n");
9819 9820
}

9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833
/**
 * _scsih_mark_responding_raid_device - mark a raid_device as responding
 * @ioc: per adapter object
 * @wwid: world wide identifier for raid volume
 * @handle: device handle
 *
 * After host reset, find out whether devices are still responding.
 * Used in _scsih_remove_unresponsive_raid_devices.
 */
static void
_scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
	u16 handle)
{
9834
	struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852
	struct scsi_target *starget;
	struct _raid_device *raid_device;
	unsigned long flags;

	spin_lock_irqsave(&ioc->raid_device_lock, flags);
	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
		if (raid_device->wwid == wwid && raid_device->starget) {
			starget = raid_device->starget;
			if (starget && starget->hostdata) {
				sas_target_priv_data = starget->hostdata;
				sas_target_priv_data->deleted = 0;
			} else
				sas_target_priv_data = NULL;
			raid_device->responding = 1;
			spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
			starget_printk(KERN_INFO, raid_device->starget,
			    "handle(0x%04x), wwid(0x%016llx)\n", handle,
			    (unsigned long long)raid_device->wwid);
9853 9854 9855 9856 9857 9858

			/*
			 * WARPDRIVE: The handles of the PDs might have changed
			 * across the host reset so re-initialize the
			 * required data for Direct IO
			 */
9859
			mpt3sas_init_warpdrive_properties(ioc, raid_device);
9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898
			spin_lock_irqsave(&ioc->raid_device_lock, flags);
			if (raid_device->handle == handle) {
				spin_unlock_irqrestore(&ioc->raid_device_lock,
				    flags);
				return;
			}
			pr_info("\thandle changed from(0x%04x)!!!\n",
			    raid_device->handle);
			raid_device->handle = handle;
			if (sas_target_priv_data)
				sas_target_priv_data->handle = handle;
			spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
			return;
		}
	}
	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
}

/**
 * _scsih_search_responding_raid_devices -
 * @ioc: per adapter object
 *
 * After host reset, find out whether devices are still responding.
 * If not remove.
 */
static void
_scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
{
	Mpi2RaidVolPage1_t volume_pg1;
	Mpi2RaidVolPage0_t volume_pg0;
	Mpi2RaidPhysDiskPage0_t pd_pg0;
	Mpi2ConfigReply_t mpi_reply;
	u16 ioc_status;
	u16 handle;
	u8 phys_disk_num;

	if (!ioc->ir_firmware)
		return;

9899
	ioc_info(ioc, "search for raid volumes: start\n");
9900 9901 9902 9903 9904 9905 9906 9907 9908

	if (list_empty(&ioc->raid_device_list))
		goto out;

	handle = 0xFFFF;
	while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
	    &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
		    MPI2_IOCSTATUS_MASK;
9909
		if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925
			break;
		handle = le16_to_cpu(volume_pg1.DevHandle);

		if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
		    &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
		     sizeof(Mpi2RaidVolPage0_t)))
			continue;

		if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
		    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
		    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
			_scsih_mark_responding_raid_device(ioc,
			    le64_to_cpu(volume_pg1.WWID), handle);
	}

	/* refresh the pd_handles */
9926
	if (!ioc->is_warpdrive) {
9927 9928 9929 9930 9931 9932 9933
		phys_disk_num = 0xFF;
		memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
		while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
		    &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
		    phys_disk_num))) {
			ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
			    MPI2_IOCSTATUS_MASK;
9934
			if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
9935 9936 9937 9938 9939
				break;
			phys_disk_num = pd_pg0.PhysDiskNum;
			handle = le16_to_cpu(pd_pg0.DevHandle);
			set_bit(handle, ioc->pd_handles);
		}
9940
	}
9941
 out:
9942
	ioc_info(ioc, "search for responding raid volumes: complete\n");
9943 9944 9945 9946 9947
}

/**
 * _scsih_mark_responding_expander - mark a expander as responding
 * @ioc: per adapter object
9948
 * @expander_pg0:SAS Expander Config Page0
9949 9950 9951 9952 9953
 *
 * After host reset, find out whether devices are still responding.
 * Used in _scsih_remove_unresponsive_expanders.
 */
static void
9954 9955
_scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc,
	Mpi2ExpanderPage0_t *expander_pg0)
9956
{
9957
	struct _sas_node *sas_expander = NULL;
9958
	unsigned long flags;
9959 9960
	int i;
	struct _enclosure_node *enclosure_dev = NULL;
9961
	u16 handle = le16_to_cpu(expander_pg0->DevHandle);
9962
	u16 enclosure_handle = le16_to_cpu(expander_pg0->EnclosureHandle);
9963
	u64 sas_address = le64_to_cpu(expander_pg0->SASAddress);
9964
	struct hba_port *port = mpt3sas_get_port_by_id(
9965
	    ioc, expander_pg0->PhysicalPort, 0);
9966

9967 9968 9969 9970
	if (enclosure_handle)
		enclosure_dev =
			mpt3sas_scsih_enclosure_find_by_handle(ioc,
							enclosure_handle);
9971 9972 9973 9974 9975

	spin_lock_irqsave(&ioc->sas_node_lock, flags);
	list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
		if (sas_expander->sas_address != sas_address)
			continue;
9976 9977
		if (sas_expander->port != port)
			continue;
9978
		sas_expander->responding = 1;
9979

9980
		if (enclosure_dev) {
9981
			sas_expander->enclosure_logical_id =
9982 9983 9984 9985
			    le64_to_cpu(enclosure_dev->pg0.EnclosureLogicalID);
			sas_expander->enclosure_handle =
			    le16_to_cpu(expander_pg0->EnclosureHandle);
		}
9986

9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017
		if (sas_expander->handle == handle)
			goto out;
		pr_info("\texpander(0x%016llx): handle changed" \
		    " from(0x%04x) to (0x%04x)!!!\n",
		    (unsigned long long)sas_expander->sas_address,
		    sas_expander->handle, handle);
		sas_expander->handle = handle;
		for (i = 0 ; i < sas_expander->num_phys ; i++)
			sas_expander->phy[i].handle = handle;
		goto out;
	}
 out:
	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
}

/**
 * _scsih_search_responding_expanders -
 * @ioc: per adapter object
 *
 * After host reset, find out whether devices are still responding.
 * If not remove.
 */
static void
_scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
{
	Mpi2ExpanderPage0_t expander_pg0;
	Mpi2ConfigReply_t mpi_reply;
	u16 ioc_status;
	u64 sas_address;
	u16 handle;

10018
	ioc_info(ioc, "search for expanders: start\n");
10019 10020 10021 10022 10023 10024 10025 10026 10027 10028

	if (list_empty(&ioc->sas_expander_list))
		goto out;

	handle = 0xFFFF;
	while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
	    MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {

		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
		    MPI2_IOCSTATUS_MASK;
10029
		if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
10030 10031 10032 10033
			break;

		handle = le16_to_cpu(expander_pg0.DevHandle);
		sas_address = le64_to_cpu(expander_pg0.SASAddress);
10034 10035 10036 10037
		pr_info(
		    "\texpander present: handle(0x%04x), sas_addr(0x%016llx), port:%d\n",
		    handle, (unsigned long long)sas_address,
		    expander_pg0.PhysicalPort);
10038
		_scsih_mark_responding_expander(ioc, &expander_pg0);
10039 10040 10041
	}

 out:
10042
	ioc_info(ioc, "search for expanders: complete\n");
10043 10044 10045
}

/**
10046
 * _scsih_remove_unresponding_devices - removing unresponding devices
10047 10048 10049
 * @ioc: per adapter object
 */
static void
10050
_scsih_remove_unresponding_devices(struct MPT3SAS_ADAPTER *ioc)
10051 10052 10053 10054
{
	struct _sas_device *sas_device, *sas_device_next;
	struct _sas_node *sas_expander, *sas_expander_next;
	struct _raid_device *raid_device, *raid_device_next;
10055
	struct _pcie_device *pcie_device, *pcie_device_next;
10056 10057
	struct list_head tmp_list;
	unsigned long flags;
10058
	LIST_HEAD(head);
10059

10060
	ioc_info(ioc, "removing unresponding devices: start\n");
10061 10062

	/* removing unresponding end devices */
10063
	ioc_info(ioc, "removing unresponding devices: end-devices\n");
10064 10065 10066 10067 10068
	/*
	 * Iterate, pulling off devices marked as non-responding. We become the
	 * owner for the reference the list had on any object we prune.
	 */
	spin_lock_irqsave(&ioc->sas_device_lock, flags);
10069 10070 10071
	list_for_each_entry_safe(sas_device, sas_device_next,
	    &ioc->sas_device_list, list) {
		if (!sas_device->responding)
10072
			list_move_tail(&sas_device->list, &head);
10073 10074 10075
		else
			sas_device->responding = 0;
	}
10076 10077 10078 10079 10080 10081 10082 10083 10084 10085
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);

	/*
	 * Now, uninitialize and remove the unresponding devices we pruned.
	 */
	list_for_each_entry_safe(sas_device, sas_device_next, &head, list) {
		_scsih_remove_device(ioc, sas_device);
		list_del_init(&sas_device->list);
		sas_device_put(sas_device);
	}
10086

10087
	ioc_info(ioc, "Removing unresponding devices: pcie end-devices\n");
10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104
	INIT_LIST_HEAD(&head);
	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
	list_for_each_entry_safe(pcie_device, pcie_device_next,
	    &ioc->pcie_device_list, list) {
		if (!pcie_device->responding)
			list_move_tail(&pcie_device->list, &head);
		else
			pcie_device->responding = 0;
	}
	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);

	list_for_each_entry_safe(pcie_device, pcie_device_next, &head, list) {
		_scsih_pcie_device_remove_from_sml(ioc, pcie_device);
		list_del_init(&pcie_device->list);
		pcie_device_put(pcie_device);
	}

10105 10106
	/* removing unresponding volumes */
	if (ioc->ir_firmware) {
10107
		ioc_info(ioc, "removing unresponding devices: volumes\n");
10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118
		list_for_each_entry_safe(raid_device, raid_device_next,
		    &ioc->raid_device_list, list) {
			if (!raid_device->responding)
				_scsih_sas_volume_delete(ioc,
				    raid_device->handle);
			else
				raid_device->responding = 0;
		}
	}

	/* removing unresponding expanders */
10119
	ioc_info(ioc, "removing unresponding devices: expanders\n");
10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134
	spin_lock_irqsave(&ioc->sas_node_lock, flags);
	INIT_LIST_HEAD(&tmp_list);
	list_for_each_entry_safe(sas_expander, sas_expander_next,
	    &ioc->sas_expander_list, list) {
		if (!sas_expander->responding)
			list_move_tail(&sas_expander->list, &tmp_list);
		else
			sas_expander->responding = 0;
	}
	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
	list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
	    list) {
		_scsih_expander_node_remove(ioc, sas_expander);
	}

10135
	ioc_info(ioc, "removing unresponding devices: complete\n");
10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151

	/* unblock devices */
	_scsih_ublock_io_all_device(ioc);
}

static void
_scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
	struct _sas_node *sas_expander, u16 handle)
{
	Mpi2ExpanderPage1_t expander_pg1;
	Mpi2ConfigReply_t mpi_reply;
	int i;

	for (i = 0 ; i < sas_expander->num_phys ; i++) {
		if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
		    &expander_pg1, i, handle))) {
10152 10153
			ioc_err(ioc, "failure at %s:%d/%s()!\n",
				__FILE__, __LINE__, __func__);
10154 10155 10156 10157 10158
			return;
		}

		mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
		    le16_to_cpu(expander_pg1.AttachedDevHandle), i,
10159 10160
		    expander_pg1.NegotiatedLinkRate >> 4,
		    sas_expander->port);
10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172
	}
}

/**
 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
 * @ioc: per adapter object
 */
static void
_scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
{
	Mpi2ExpanderPage0_t expander_pg0;
	Mpi2SasDevicePage0_t sas_device_pg0;
10173
	Mpi26PCIeDevicePage0_t pcie_device_pg0;
10174 10175 10176 10177 10178
	Mpi2RaidVolPage1_t volume_pg1;
	Mpi2RaidVolPage0_t volume_pg0;
	Mpi2RaidPhysDiskPage0_t pd_pg0;
	Mpi2EventIrConfigElement_t element;
	Mpi2ConfigReply_t mpi_reply;
10179
	u8 phys_disk_num, port_id;
10180 10181 10182 10183
	u16 ioc_status;
	u16 handle, parent_handle;
	u64 sas_address;
	struct _sas_device *sas_device;
10184
	struct _pcie_device *pcie_device;
10185 10186 10187 10188 10189
	struct _sas_node *expander_device;
	static struct _raid_device *raid_device;
	u8 retry_count;
	unsigned long flags;

10190
	ioc_info(ioc, "scan devices: start\n");
10191 10192 10193

	_scsih_sas_host_refresh(ioc);

10194
	ioc_info(ioc, "\tscan devices: expanders start\n");
10195 10196 10197 10198 10199 10200 10201 10202

	/* expanders */
	handle = 0xFFFF;
	while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
	    MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
		    MPI2_IOCSTATUS_MASK;
		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
10203 10204
			ioc_info(ioc, "\tbreak from expander scan: ioc_status(0x%04x), loginfo(0x%08x)\n",
				 ioc_status, le32_to_cpu(mpi_reply.IOCLogInfo));
10205 10206 10207 10208
			break;
		}
		handle = le16_to_cpu(expander_pg0.DevHandle);
		spin_lock_irqsave(&ioc->sas_node_lock, flags);
10209
		port_id = expander_pg0.PhysicalPort;
10210
		expander_device = mpt3sas_scsih_expander_find_by_sas_address(
10211
		    ioc, le64_to_cpu(expander_pg0.SASAddress),
10212
		    mpt3sas_get_port_by_id(ioc, port_id, 0));
10213 10214 10215 10216 10217
		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
		if (expander_device)
			_scsih_refresh_expander_links(ioc, expander_device,
			    handle);
		else {
10218 10219 10220
			ioc_info(ioc, "\tBEFORE adding expander: handle (0x%04x), sas_addr(0x%016llx)\n",
				 handle,
				 (u64)le64_to_cpu(expander_pg0.SASAddress));
10221
			_scsih_expander_add(ioc, handle);
10222 10223 10224
			ioc_info(ioc, "\tAFTER adding expander: handle (0x%04x), sas_addr(0x%016llx)\n",
				 handle,
				 (u64)le64_to_cpu(expander_pg0.SASAddress));
10225 10226 10227
		}
	}

10228
	ioc_info(ioc, "\tscan devices: expanders complete\n");
10229 10230 10231 10232

	if (!ioc->ir_firmware)
		goto skip_to_sas;

10233
	ioc_info(ioc, "\tscan devices: phys disk start\n");
10234 10235 10236 10237 10238 10239 10240 10241 10242

	/* phys disk */
	phys_disk_num = 0xFF;
	while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
	    &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
	    phys_disk_num))) {
		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
		    MPI2_IOCSTATUS_MASK;
		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
10243 10244
			ioc_info(ioc, "\tbreak from phys disk scan: ioc_status(0x%04x), loginfo(0x%08x)\n",
				 ioc_status, le32_to_cpu(mpi_reply.IOCLogInfo));
10245 10246 10247 10248
			break;
		}
		phys_disk_num = pd_pg0.PhysDiskNum;
		handle = le16_to_cpu(pd_pg0.DevHandle);
10249 10250 10251
		sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
		if (sas_device) {
			sas_device_put(sas_device);
10252
			continue;
10253
		}
10254 10255 10256 10257 10258 10259 10260
		if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
		    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
		    handle) != 0)
			continue;
		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
		    MPI2_IOCSTATUS_MASK;
		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
10261 10262
			ioc_info(ioc, "\tbreak from phys disk scan ioc_status(0x%04x), loginfo(0x%08x)\n",
				 ioc_status, le32_to_cpu(mpi_reply.IOCLogInfo));
10263 10264 10265 10266 10267
			break;
		}
		parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
		if (!_scsih_get_sas_address(ioc, parent_handle,
		    &sas_address)) {
10268 10269 10270
			ioc_info(ioc, "\tBEFORE adding phys disk: handle (0x%04x), sas_addr(0x%016llx)\n",
				 handle,
				 (u64)le64_to_cpu(sas_device_pg0.SASAddress));
10271
			port_id = sas_device_pg0.PhysicalPort;
10272 10273
			mpt3sas_transport_update_links(ioc, sas_address,
			    handle, sas_device_pg0.PhyNum,
10274
			    MPI2_SAS_NEG_LINK_RATE_1_5,
10275
			    mpt3sas_get_port_by_id(ioc, port_id, 0));
10276 10277 10278 10279 10280 10281 10282 10283 10284 10285
			set_bit(handle, ioc->pd_handles);
			retry_count = 0;
			/* This will retry adding the end device.
			 * _scsih_add_device() will decide on retries and
			 * return "1" when it should be retried
			 */
			while (_scsih_add_device(ioc, handle, retry_count++,
			    1)) {
				ssleep(1);
			}
10286 10287 10288
			ioc_info(ioc, "\tAFTER adding phys disk: handle (0x%04x), sas_addr(0x%016llx)\n",
				 handle,
				 (u64)le64_to_cpu(sas_device_pg0.SASAddress));
10289 10290 10291
		}
	}

10292
	ioc_info(ioc, "\tscan devices: phys disk complete\n");
10293

10294
	ioc_info(ioc, "\tscan devices: volumes start\n");
10295 10296 10297 10298 10299 10300 10301 10302

	/* volumes */
	handle = 0xFFFF;
	while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
	    &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
		    MPI2_IOCSTATUS_MASK;
		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
10303 10304
			ioc_info(ioc, "\tbreak from volume scan: ioc_status(0x%04x), loginfo(0x%08x)\n",
				 ioc_status, le32_to_cpu(mpi_reply.IOCLogInfo));
10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320
			break;
		}
		handle = le16_to_cpu(volume_pg1.DevHandle);
		spin_lock_irqsave(&ioc->raid_device_lock, flags);
		raid_device = _scsih_raid_device_find_by_wwid(ioc,
		    le64_to_cpu(volume_pg1.WWID));
		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
		if (raid_device)
			continue;
		if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
		    &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
		     sizeof(Mpi2RaidVolPage0_t)))
			continue;
		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
		    MPI2_IOCSTATUS_MASK;
		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
10321 10322
			ioc_info(ioc, "\tbreak from volume scan: ioc_status(0x%04x), loginfo(0x%08x)\n",
				 ioc_status, le32_to_cpu(mpi_reply.IOCLogInfo));
10323 10324 10325 10326 10327 10328 10329 10330
			break;
		}
		if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
		    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
		    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
			memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
			element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
			element.VolDevHandle = volume_pg1.DevHandle;
10331 10332
			ioc_info(ioc, "\tBEFORE adding volume: handle (0x%04x)\n",
				 volume_pg1.DevHandle);
10333
			_scsih_sas_volume_add(ioc, &element);
10334 10335
			ioc_info(ioc, "\tAFTER adding volume: handle (0x%04x)\n",
				 volume_pg1.DevHandle);
10336 10337 10338
		}
	}

10339
	ioc_info(ioc, "\tscan devices: volumes complete\n");
10340 10341 10342

 skip_to_sas:

10343
	ioc_info(ioc, "\tscan devices: end devices start\n");
10344 10345 10346 10347 10348 10349 10350 10351 10352

	/* sas devices */
	handle = 0xFFFF;
	while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
	    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
	    handle))) {
		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
		    MPI2_IOCSTATUS_MASK;
		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
10353 10354
			ioc_info(ioc, "\tbreak from end device scan: ioc_status(0x%04x), loginfo(0x%08x)\n",
				 ioc_status, le32_to_cpu(mpi_reply.IOCLogInfo));
10355 10356 10357 10358 10359 10360
			break;
		}
		handle = le16_to_cpu(sas_device_pg0.DevHandle);
		if (!(_scsih_is_end_device(
		    le32_to_cpu(sas_device_pg0.DeviceInfo))))
			continue;
10361
		port_id = sas_device_pg0.PhysicalPort;
10362
		sas_device = mpt3sas_get_sdev_by_addr(ioc,
10363
		    le64_to_cpu(sas_device_pg0.SASAddress),
10364
		    mpt3sas_get_port_by_id(ioc, port_id, 0));
10365 10366
		if (sas_device) {
			sas_device_put(sas_device);
10367
			continue;
10368
		}
10369 10370
		parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
		if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
10371 10372 10373
			ioc_info(ioc, "\tBEFORE adding end device: handle (0x%04x), sas_addr(0x%016llx)\n",
				 handle,
				 (u64)le64_to_cpu(sas_device_pg0.SASAddress));
10374
			mpt3sas_transport_update_links(ioc, sas_address, handle,
10375
			    sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5,
10376
			    mpt3sas_get_port_by_id(ioc, port_id, 0));
10377 10378 10379 10380 10381 10382 10383 10384 10385
			retry_count = 0;
			/* This will retry adding the end device.
			 * _scsih_add_device() will decide on retries and
			 * return "1" when it should be retried
			 */
			while (_scsih_add_device(ioc, handle, retry_count++,
			    0)) {
				ssleep(1);
			}
10386 10387 10388
			ioc_info(ioc, "\tAFTER adding end device: handle (0x%04x), sas_addr(0x%016llx)\n",
				 handle,
				 (u64)le64_to_cpu(sas_device_pg0.SASAddress));
10389 10390
		}
	}
10391 10392
	ioc_info(ioc, "\tscan devices: end devices complete\n");
	ioc_info(ioc, "\tscan devices: pcie end devices start\n");
10393 10394 10395 10396 10397 10398 10399 10400 10401

	/* pcie devices */
	handle = 0xFFFF;
	while (!(mpt3sas_config_get_pcie_device_pg0(ioc, &mpi_reply,
		&pcie_device_pg0, MPI26_PCIE_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
		handle))) {
		ioc_status = le16_to_cpu(mpi_reply.IOCStatus)
				& MPI2_IOCSTATUS_MASK;
		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
10402 10403
			ioc_info(ioc, "\tbreak from pcie end device scan: ioc_status(0x%04x), loginfo(0x%08x)\n",
				 ioc_status, le32_to_cpu(mpi_reply.IOCLogInfo));
10404 10405 10406
			break;
		}
		handle = le16_to_cpu(pcie_device_pg0.DevHandle);
10407
		if (!(_scsih_is_nvme_pciescsi_device(
10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418
			le32_to_cpu(pcie_device_pg0.DeviceInfo))))
			continue;
		pcie_device = mpt3sas_get_pdev_by_wwid(ioc,
				le64_to_cpu(pcie_device_pg0.WWID));
		if (pcie_device) {
			pcie_device_put(pcie_device);
			continue;
		}
		retry_count = 0;
		parent_handle = le16_to_cpu(pcie_device_pg0.ParentDevHandle);
		_scsih_pcie_add_device(ioc, handle);
10419

10420 10421
		ioc_info(ioc, "\tAFTER adding pcie end device: handle (0x%04x), wwid(0x%016llx)\n",
			 handle, (u64)le64_to_cpu(pcie_device_pg0.WWID));
10422
	}
10423 10424
	ioc_info(ioc, "\tpcie devices: pcie end devices complete\n");
	ioc_info(ioc, "scan devices: complete\n");
10425
}
10426

10427 10428 10429 10430 10431
/**
 * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
 * @ioc: per adapter object
 *
 * The handler for doing any required cleanup or initialization.
10432 10433 10434
 */
void mpt3sas_scsih_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
{
10435
	dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_PRE_RESET\n", __func__));
10436 10437 10438
}

/**
10439 10440
 * mpt3sas_scsih_clear_outstanding_scsi_tm_commands - clears outstanding
 *							scsi & tm cmds.
10441
 * @ioc: per adapter object
10442
 *
10443
 * The handler for doing any required cleanup or initialization.
10444 10445
 */
void
10446
mpt3sas_scsih_clear_outstanding_scsi_tm_commands(struct MPT3SAS_ADAPTER *ioc)
10447
{
10448 10449
	dtmprintk(ioc,
	    ioc_info(ioc, "%s: clear outstanding scsi & tm cmds\n", __func__));
10450 10451 10452 10453 10454 10455 10456 10457 10458 10459
	if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
		ioc->scsih_cmds.status |= MPT3_CMD_RESET;
		mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
		complete(&ioc->scsih_cmds.done);
	}
	if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
		ioc->tm_cmds.status |= MPT3_CMD_RESET;
		mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
		complete(&ioc->tm_cmds.done);
	}
10460

10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476
	memset(ioc->pend_os_device_add, 0, ioc->pend_os_device_add_sz);
	memset(ioc->device_remove_in_progress, 0,
	       ioc->device_remove_in_progress_sz);
	_scsih_fw_event_cleanup_queue(ioc);
	_scsih_flush_running_cmds(ioc);
}

/**
 * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
 * @ioc: per adapter object
 *
 * The handler for doing any required cleanup or initialization.
 */
void
mpt3sas_scsih_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
{
10477
	dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_DONE_RESET\n", __func__));
10478 10479
	if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
					   !ioc->sas_hba.num_phys)) {
10480
		_scsih_sas_port_refresh(ioc);
10481
		_scsih_update_vphys_after_reset(ioc);
10482 10483 10484 10485 10486 10487 10488
		_scsih_prep_device_scan(ioc);
		_scsih_create_enclosure_list_after_reset(ioc);
		_scsih_search_responding_sas_devices(ioc);
		_scsih_search_responding_pcie_devices(ioc);
		_scsih_search_responding_raid_devices(ioc);
		_scsih_search_responding_expanders(ioc);
		_scsih_error_recovery_delete_devices(ioc);
10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500
	}
}

/**
 * _mpt3sas_fw_work - delayed task for processing firmware events
 * @ioc: per adapter object
 * @fw_event: The fw_event_work object
 * Context: user.
 */
static void
_mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
{
10501
	ioc->current_event = fw_event;
10502 10503
	_scsih_fw_event_del_from_list(ioc, fw_event);

10504
	/* the queue is being flushed so ignore this event */
10505 10506
	if (ioc->remove_host || ioc->pci_error_recovery) {
		fw_event_work_put(fw_event);
10507
		ioc->current_event = NULL;
10508 10509 10510 10511 10512
		return;
	}

	switch (fw_event->event) {
	case MPT3SAS_PROCESS_TRIGGER_DIAG:
10513 10514 10515
		mpt3sas_process_trigger_data(ioc,
			(struct SL_WH_TRIGGERS_EVENT_DATA_T *)
			fw_event->event_data);
10516 10517
		break;
	case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
10518 10519 10520
		while (scsi_host_in_recovery(ioc->shost) ||
					 ioc->shost_recovery) {
			/*
10521 10522
			 * If we're unloading or cancelling the work, bail.
			 * Otherwise, this can become an infinite loop.
10523
			 */
10524
			if (ioc->remove_host || ioc->fw_events_cleanup)
10525
				goto out;
10526
			ssleep(1);
10527
		}
10528
		_scsih_remove_unresponding_devices(ioc);
10529
		_scsih_del_dirty_vphy(ioc);
10530
		_scsih_del_dirty_port_entries(ioc);
10531
		_scsih_scan_for_devices_after_reset(ioc);
10532
		_scsih_set_nvme_max_shutdown_latency(ioc);
10533 10534 10535
		break;
	case MPT3SAS_PORT_ENABLE_COMPLETE:
		ioc->start_scan = 0;
B
Bart Van Assche 已提交
10536
		if (missing_delay[0] != -1 && missing_delay[1] != -1)
10537 10538
			mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
			    missing_delay[1]);
10539 10540
		dewtprintk(ioc,
			   ioc_info(ioc, "port enable: complete from worker thread\n"));
10541
		break;
10542 10543
	case MPT3SAS_TURN_ON_PFA_LED:
		_scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
10544 10545 10546 10547 10548
		break;
	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
		_scsih_sas_topology_change_event(ioc, fw_event);
		break;
	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
10549 10550 10551 10552
		if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
			_scsih_sas_device_status_change_event_debug(ioc,
			    (Mpi2EventDataSasDeviceStatusChange_t *)
			    fw_event->event_data);
10553 10554 10555 10556
		break;
	case MPI2_EVENT_SAS_DISCOVERY:
		_scsih_sas_discovery_event(ioc, fw_event);
		break;
10557 10558 10559
	case MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
		_scsih_sas_device_discovery_error_event(ioc, fw_event);
		break;
10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578
	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
		_scsih_sas_broadcast_primitive_event(ioc, fw_event);
		break;
	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
		_scsih_sas_enclosure_dev_status_change_event(ioc,
		    fw_event);
		break;
	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
		_scsih_sas_ir_config_change_event(ioc, fw_event);
		break;
	case MPI2_EVENT_IR_VOLUME:
		_scsih_sas_ir_volume_event(ioc, fw_event);
		break;
	case MPI2_EVENT_IR_PHYSICAL_DISK:
		_scsih_sas_ir_physical_disk_event(ioc, fw_event);
		break;
	case MPI2_EVENT_IR_OPERATION_STATUS:
		_scsih_sas_ir_operation_status_event(ioc, fw_event);
		break;
10579 10580 10581 10582 10583 10584 10585 10586
	case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
		_scsih_pcie_device_status_change_event(ioc, fw_event);
		break;
	case MPI2_EVENT_PCIE_ENUMERATION:
		_scsih_pcie_enumeration_event(ioc, fw_event);
		break;
	case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
		_scsih_pcie_topology_change_event(ioc, fw_event);
10587
		ioc->current_event = NULL;
10588 10589
			return;
	break;
10590
	}
10591 10592
out:
	fw_event_work_put(fw_event);
10593
	ioc->current_event = NULL;
10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622
}

/**
 * _firmware_event_work
 * @work: The fw_event_work object
 * Context: user.
 *
 * wrappers for the work thread handling firmware events
 */

static void
_firmware_event_work(struct work_struct *work)
{
	struct fw_event_work *fw_event = container_of(work,
	    struct fw_event_work, work);

	_mpt3sas_fw_work(fw_event->ioc, fw_event);
}

/**
 * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
 * @ioc: per adapter object
 * @msix_index: MSIX table index supplied by the OS
 * @reply: reply message frame(lower 32bit addr)
 * Context: interrupt.
 *
 * This function merely adds a new work task into ioc->firmware_event_thread.
 * The tasks are worked from _firmware_event_work in user context.
 *
10623 10624
 * Return: 1 meaning mf should be freed from _base_interrupt
 *         0 means the mf is freed from this function.
10625 10626 10627 10628 10629 10630 10631 10632 10633
 */
u8
mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
	u32 reply)
{
	struct fw_event_work *fw_event;
	Mpi2EventNotificationReply_t *mpi_reply;
	u16 event;
	u16 sz;
10634
	Mpi26EventDataActiveCableExcept_t *ActiveCableEventData;
10635

10636 10637
	/* events turned off due to host reset */
	if (ioc->pci_error_recovery)
10638 10639 10640 10641 10642
		return 1;

	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);

	if (unlikely(!mpi_reply)) {
10643 10644
		ioc_err(ioc, "mpi_reply not valid at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677
		return 1;
	}

	event = le16_to_cpu(mpi_reply->Event);

	if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
		mpt3sas_trigger_event(ioc, event, 0);

	switch (event) {
	/* handle these */
	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
	{
		Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
		    (Mpi2EventDataSasBroadcastPrimitive_t *)
		    mpi_reply->EventData;

		if (baen_data->Primitive !=
		    MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
			return 1;

		if (ioc->broadcast_aen_busy) {
			ioc->broadcast_aen_pending++;
			return 1;
		} else
			ioc->broadcast_aen_busy = 1;
		break;
	}

	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
		_scsih_check_topo_delete_events(ioc,
		    (Mpi2EventDataSasTopologyChangeList_t *)
		    mpi_reply->EventData);
		break;
10678 10679 10680 10681 10682
	case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
	_scsih_check_pcie_topo_remove_events(ioc,
		    (Mpi26EventDataPCIeTopologyChangeList_t *)
		    mpi_reply->EventData);
		break;
10683 10684 10685 10686 10687 10688 10689 10690 10691 10692
	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
		_scsih_check_ir_config_unhide_events(ioc,
		    (Mpi2EventDataIrConfigChangeList_t *)
		    mpi_reply->EventData);
		break;
	case MPI2_EVENT_IR_VOLUME:
		_scsih_check_volume_delete_events(ioc,
		    (Mpi2EventDataIrVolume_t *)
		    mpi_reply->EventData);
		break;
10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703
	case MPI2_EVENT_LOG_ENTRY_ADDED:
	{
		Mpi2EventDataLogEntryAdded_t *log_entry;
		u32 *log_code;

		if (!ioc->is_warpdrive)
			break;

		log_entry = (Mpi2EventDataLogEntryAdded_t *)
		    mpi_reply->EventData;
		log_code = (u32 *)log_entry->LogData;
10704

10705 10706 10707 10708 10709 10710
		if (le16_to_cpu(log_entry->LogEntryQualifier)
		    != MPT2_WARPDRIVE_LOGENTRY)
			break;

		switch (le32_to_cpu(*log_code)) {
		case MPT2_WARPDRIVE_LC_SSDT:
10711
			ioc_warn(ioc, "WarpDrive Warning: IO Throttling has occurred in the WarpDrive subsystem. Check WarpDrive documentation for additional details.\n");
10712 10713
			break;
		case MPT2_WARPDRIVE_LC_SSDLW:
10714
			ioc_warn(ioc, "WarpDrive Warning: Program/Erase Cycles for the WarpDrive subsystem in degraded range. Check WarpDrive documentation for additional details.\n");
10715 10716
			break;
		case MPT2_WARPDRIVE_LC_SSDLF:
10717
			ioc_err(ioc, "WarpDrive Fatal Error: There are no Program/Erase Cycles for the WarpDrive subsystem. The storage device will be in read-only mode. Check WarpDrive documentation for additional details.\n");
10718 10719
			break;
		case MPT2_WARPDRIVE_LC_BRMF:
10720
			ioc_err(ioc, "WarpDrive Fatal Error: The Backup Rail Monitor has failed on the WarpDrive subsystem. Check WarpDrive documentation for additional details.\n");
10721 10722 10723 10724 10725
			break;
		}

		break;
	}
10726
	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
10727 10728 10729 10730
		_scsih_sas_device_status_change_event(ioc,
		    (Mpi2EventDataSasDeviceStatusChange_t *)
		    mpi_reply->EventData);
		break;
10731 10732
	case MPI2_EVENT_IR_OPERATION_STATUS:
	case MPI2_EVENT_SAS_DISCOVERY:
10733
	case MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
10734 10735
	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
	case MPI2_EVENT_IR_PHYSICAL_DISK:
10736 10737
	case MPI2_EVENT_PCIE_ENUMERATION:
	case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
10738 10739
		break;

10740 10741 10742 10743 10744
	case MPI2_EVENT_TEMP_THRESHOLD:
		_scsih_temp_threshold_events(ioc,
			(Mpi2EventDataTemperature_t *)
			mpi_reply->EventData);
		break;
10745 10746 10747
	case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
		ActiveCableEventData =
		    (Mpi26EventDataActiveCableExcept_t *) mpi_reply->EventData;
10748 10749
		switch (ActiveCableEventData->ReasonCode) {
		case MPI26_EVENT_ACTIVE_CABLE_INSUFFICIENT_POWER:
10750 10751
			ioc_notice(ioc, "Currently an active cable with ReceptacleID %d\n",
				   ActiveCableEventData->ReceptacleID);
10752 10753 10754
			pr_notice("cannot be powered and devices connected\n");
			pr_notice("to this active cable will not be seen\n");
			pr_notice("This active cable requires %d mW of power\n",
10755 10756 10757 10758
			     ActiveCableEventData->ActiveCablePowerRequirement);
			break;

		case MPI26_EVENT_ACTIVE_CABLE_DEGRADED:
10759 10760
			ioc_notice(ioc, "Currently a cable with ReceptacleID %d\n",
				   ActiveCableEventData->ReceptacleID);
10761 10762
			pr_notice(
			    "is not running at optimal speed(12 Gb/s rate)\n");
10763
			break;
10764
		}
10765

10766
		break;
10767

10768 10769 10770 10771 10772
	default: /* ignore the rest */
		return 1;
	}

	sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
10773
	fw_event = alloc_fw_event_work(sz);
10774
	if (!fw_event) {
10775 10776
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
10777 10778 10779 10780 10781 10782 10783 10784 10785
		return 1;
	}

	memcpy(fw_event->event_data, mpi_reply->EventData, sz);
	fw_event->ioc = ioc;
	fw_event->VF_ID = mpi_reply->VF_ID;
	fw_event->VP_ID = mpi_reply->VP_ID;
	fw_event->event = event;
	_scsih_fw_event_add(ioc, fw_event);
10786
	fw_event_work_put(fw_event);
10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802
	return 1;
}

/**
 * _scsih_expander_node_remove - removing expander device from list.
 * @ioc: per adapter object
 * @sas_expander: the sas_device object
 *
 * Removing object and freeing associated memory from the
 * ioc->sas_expander_list.
 */
static void
_scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
	struct _sas_node *sas_expander)
{
	struct _sas_port *mpt3sas_port, *next;
10803
	unsigned long flags;
10804 10805 10806 10807 10808 10809 10810 10811 10812

	/* remove sibling ports attached to this expander */
	list_for_each_entry_safe(mpt3sas_port, next,
	   &sas_expander->sas_port_list, port_list) {
		if (ioc->shost_recovery)
			return;
		if (mpt3sas_port->remote_identify.device_type ==
		    SAS_END_DEVICE)
			mpt3sas_device_remove_by_sas_address(ioc,
10813 10814
			    mpt3sas_port->remote_identify.sas_address,
			    mpt3sas_port->hba_port);
10815 10816 10817 10818 10819
		else if (mpt3sas_port->remote_identify.device_type ==
		    SAS_EDGE_EXPANDER_DEVICE ||
		    mpt3sas_port->remote_identify.device_type ==
		    SAS_FANOUT_EXPANDER_DEVICE)
			mpt3sas_expander_remove(ioc,
10820 10821
			    mpt3sas_port->remote_identify.sas_address,
			    mpt3sas_port->hba_port);
10822 10823 10824
	}

	mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
10825
	    sas_expander->sas_address_parent, sas_expander->port);
10826

10827 10828 10829 10830 10831
	ioc_info(ioc,
	    "expander_remove: handle(0x%04x), sas_addr(0x%016llx), port:%d\n",
	    sas_expander->handle, (unsigned long long)
	    sas_expander->sas_address,
	    sas_expander->port->port_id);
10832

10833 10834 10835 10836
	spin_lock_irqsave(&ioc->sas_node_lock, flags);
	list_del(&sas_expander->list);
	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);

10837 10838 10839 10840
	kfree(sas_expander->phy);
	kfree(sas_expander);
}

10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909
/**
 * _scsih_nvme_shutdown - NVMe shutdown notification
 * @ioc: per adapter object
 *
 * Sending IoUnitControl request with shutdown operation code to alert IOC that
 * the host system is shutting down so that IOC can issue NVMe shutdown to
 * NVMe drives attached to it.
 */
static void
_scsih_nvme_shutdown(struct MPT3SAS_ADAPTER *ioc)
{
	Mpi26IoUnitControlRequest_t *mpi_request;
	Mpi26IoUnitControlReply_t *mpi_reply;
	u16 smid;

	/* are there any NVMe devices ? */
	if (list_empty(&ioc->pcie_device_list))
		return;

	mutex_lock(&ioc->scsih_cmds.mutex);

	if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
		ioc_err(ioc, "%s: scsih_cmd in use\n", __func__);
		goto out;
	}

	ioc->scsih_cmds.status = MPT3_CMD_PENDING;

	smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
	if (!smid) {
		ioc_err(ioc,
		    "%s: failed obtaining a smid\n", __func__);
		ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
		goto out;
	}

	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
	ioc->scsih_cmds.smid = smid;
	memset(mpi_request, 0, sizeof(Mpi26IoUnitControlRequest_t));
	mpi_request->Function = MPI2_FUNCTION_IO_UNIT_CONTROL;
	mpi_request->Operation = MPI26_CTRL_OP_SHUTDOWN;

	init_completion(&ioc->scsih_cmds.done);
	ioc->put_smid_default(ioc, smid);
	/* Wait for max_shutdown_latency seconds */
	ioc_info(ioc,
		"Io Unit Control shutdown (sending), Shutdown latency %d sec\n",
		ioc->max_shutdown_latency);
	wait_for_completion_timeout(&ioc->scsih_cmds.done,
			ioc->max_shutdown_latency*HZ);

	if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
		ioc_err(ioc, "%s: timeout\n", __func__);
		goto out;
	}

	if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
		mpi_reply = ioc->scsih_cmds.reply;
		ioc_info(ioc, "Io Unit Control shutdown (complete):"
			"ioc_status(0x%04x), loginfo(0x%08x)\n",
			le16_to_cpu(mpi_reply->IOCStatus),
			le32_to_cpu(mpi_reply->IOCLogInfo));
	}
 out:
	ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
	mutex_unlock(&ioc->scsih_cmds.mutex);
}


10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934
/**
 * _scsih_ir_shutdown - IR shutdown notification
 * @ioc: per adapter object
 *
 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
 * the host system is shutting down.
 */
static void
_scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
{
	Mpi2RaidActionRequest_t *mpi_request;
	Mpi2RaidActionReply_t *mpi_reply;
	u16 smid;

	/* is IR firmware build loaded ? */
	if (!ioc->ir_firmware)
		return;

	/* are there any volumes ? */
	if (list_empty(&ioc->raid_device_list))
		return;

	mutex_lock(&ioc->scsih_cmds.mutex);

	if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
10935
		ioc_err(ioc, "%s: scsih_cmd in use\n", __func__);
10936 10937 10938 10939 10940 10941
		goto out;
	}
	ioc->scsih_cmds.status = MPT3_CMD_PENDING;

	smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
	if (!smid) {
10942
		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953
		ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
		goto out;
	}

	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
	ioc->scsih_cmds.smid = smid;
	memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));

	mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
	mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;

10954
	if (!ioc->hide_ir_msg)
10955
		ioc_info(ioc, "IR shutdown (sending)\n");
10956
	init_completion(&ioc->scsih_cmds.done);
10957
	ioc->put_smid_default(ioc, smid);
10958 10959 10960
	wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);

	if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
10961
		ioc_err(ioc, "%s: timeout\n", __func__);
10962 10963 10964 10965 10966
		goto out;
	}

	if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
		mpi_reply = ioc->scsih_cmds.reply;
10967
		if (!ioc->hide_ir_msg)
10968 10969 10970
			ioc_info(ioc, "IR shutdown (complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
				 le16_to_cpu(mpi_reply->IOCStatus),
				 le32_to_cpu(mpi_reply->IOCLogInfo));
10971 10972 10973 10974 10975 10976 10977
	}

 out:
	ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
	mutex_unlock(&ioc->scsih_cmds.mutex);
}

10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005
/**
 * _scsih_get_shost_and_ioc - get shost and ioc
 *			and verify whether they are NULL or not
 * @pdev: PCI device struct
 * @shost: address of scsi host pointer
 * @ioc: address of HBA adapter pointer
 *
 * Return zero if *shost and *ioc are not NULL otherwise return error number.
 */
static int
_scsih_get_shost_and_ioc(struct pci_dev *pdev,
	struct Scsi_Host **shost, struct MPT3SAS_ADAPTER **ioc)
{
	*shost = pci_get_drvdata(pdev);
	if (*shost == NULL) {
		dev_err(&pdev->dev, "pdev's driver data is null\n");
		return -ENXIO;
	}

	*ioc = shost_priv(*shost);
	if (*ioc == NULL) {
		dev_err(&pdev->dev, "shost's private data is null\n");
		return -ENXIO;
	}

	return 0;
}

11006
/**
11007
 * scsih_remove - detach and remove add host
11008 11009 11010 11011
 * @pdev: PCI device struct
 *
 * Routine called when unloading the driver.
 */
11012
static void scsih_remove(struct pci_dev *pdev)
11013
{
11014 11015
	struct Scsi_Host *shost;
	struct MPT3SAS_ADAPTER *ioc;
11016 11017 11018
	struct _sas_port *mpt3sas_port, *next_port;
	struct _raid_device *raid_device, *next;
	struct MPT3SAS_TARGET *sas_target_priv_data;
11019
	struct _pcie_device *pcie_device, *pcienext;
11020 11021
	struct workqueue_struct	*wq;
	unsigned long flags;
11022
	Mpi2ConfigReply_t mpi_reply;
11023
	struct hba_port *port, *port_next;
11024

11025 11026 11027
	if (_scsih_get_shost_and_ioc(pdev, &shost, &ioc))
		return;

11028
	ioc->remove_host = 1;
11029

11030 11031
	if (!pci_device_is_present(pdev))
		_scsih_flush_running_cmds(ioc);
11032

11033 11034 11035 11036 11037 11038 11039 11040
	_scsih_fw_event_cleanup_queue(ioc);

	spin_lock_irqsave(&ioc->fw_event_lock, flags);
	wq = ioc->firmware_event_thread;
	ioc->firmware_event_thread = NULL;
	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
	if (wq)
		destroy_workqueue(wq);
11041 11042 11043 11044 11045 11046 11047
	/*
	 * Copy back the unmodified ioc page1. so that on next driver load,
	 * current modified changes on ioc page1 won't take effect.
	 */
	if (ioc->is_aero_ioc)
		mpt3sas_config_set_ioc_pg1(ioc, &mpi_reply,
				&ioc->ioc_pg1_copy);
11048 11049
	/* release all the volumes */
	_scsih_ir_shutdown(ioc);
11050
	mpt3sas_destroy_debugfs(ioc);
11051
	sas_remove_host(shost);
11052 11053 11054 11055 11056 11057 11058 11059
	list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
	    list) {
		if (raid_device->starget) {
			sas_target_priv_data =
			    raid_device->starget->hostdata;
			sas_target_priv_data->deleted = 1;
			scsi_remove_target(&raid_device->starget->dev);
		}
11060 11061
		ioc_info(ioc, "removing handle(0x%04x), wwid(0x%016llx)\n",
			 raid_device->handle, (u64)raid_device->wwid);
11062 11063
		_scsih_raid_device_remove(ioc, raid_device);
	}
11064 11065 11066 11067 11068 11069
	list_for_each_entry_safe(pcie_device, pcienext, &ioc->pcie_device_list,
		list) {
		_scsih_pcie_device_remove_from_sml(ioc, pcie_device);
		list_del_init(&pcie_device->list);
		pcie_device_put(pcie_device);
	}
11070 11071 11072 11073 11074 11075 11076

	/* free ports attached to the sas_host */
	list_for_each_entry_safe(mpt3sas_port, next_port,
	   &ioc->sas_hba.sas_port_list, port_list) {
		if (mpt3sas_port->remote_identify.device_type ==
		    SAS_END_DEVICE)
			mpt3sas_device_remove_by_sas_address(ioc,
11077 11078
			    mpt3sas_port->remote_identify.sas_address,
			    mpt3sas_port->hba_port);
11079 11080 11081 11082 11083
		else if (mpt3sas_port->remote_identify.device_type ==
		    SAS_EDGE_EXPANDER_DEVICE ||
		    mpt3sas_port->remote_identify.device_type ==
		    SAS_FANOUT_EXPANDER_DEVICE)
			mpt3sas_expander_remove(ioc,
11084 11085
			    mpt3sas_port->remote_identify.sas_address,
			    mpt3sas_port->hba_port);
11086 11087
	}

11088 11089 11090 11091 11092 11093
	list_for_each_entry_safe(port, port_next,
	    &ioc->port_table_list, list) {
		list_del(&port->list);
		kfree(port);
	}

11094 11095 11096 11097 11098 11099 11100 11101
	/* free phys attached to the sas_host */
	if (ioc->sas_hba.num_phys) {
		kfree(ioc->sas_hba.phy);
		ioc->sas_hba.phy = NULL;
		ioc->sas_hba.num_phys = 0;
	}

	mpt3sas_base_detach(ioc);
11102
	spin_lock(&gioc_lock);
11103
	list_del(&ioc->list);
11104
	spin_unlock(&gioc_lock);
11105 11106 11107 11108
	scsi_host_put(shost);
}

/**
11109
 * scsih_shutdown - routine call during system shutdown
11110 11111
 * @pdev: PCI device struct
 */
11112
static void
11113
scsih_shutdown(struct pci_dev *pdev)
11114
{
11115 11116
	struct Scsi_Host *shost;
	struct MPT3SAS_ADAPTER *ioc;
11117 11118
	struct workqueue_struct	*wq;
	unsigned long flags;
11119
	Mpi2ConfigReply_t mpi_reply;
11120

11121 11122 11123
	if (_scsih_get_shost_and_ioc(pdev, &shost, &ioc))
		return;

11124
	ioc->remove_host = 1;
11125

11126 11127
	if (!pci_device_is_present(pdev))
		_scsih_flush_running_cmds(ioc);
11128

11129 11130 11131 11132 11133 11134 11135 11136
	_scsih_fw_event_cleanup_queue(ioc);

	spin_lock_irqsave(&ioc->fw_event_lock, flags);
	wq = ioc->firmware_event_thread;
	ioc->firmware_event_thread = NULL;
	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
	if (wq)
		destroy_workqueue(wq);
11137 11138 11139 11140 11141 11142 11143
	/*
	 * Copy back the unmodified ioc page1 so that on next driver load,
	 * current modified changes on ioc page1 won't take effect.
	 */
	if (ioc->is_aero_ioc)
		mpt3sas_config_set_ioc_pg1(ioc, &mpi_reply,
				&ioc->ioc_pg1_copy);
11144 11145

	_scsih_ir_shutdown(ioc);
11146
	_scsih_nvme_shutdown(ioc);
11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161
	mpt3sas_base_detach(ioc);
}


/**
 * _scsih_probe_boot_devices - reports 1st device
 * @ioc: per adapter object
 *
 * If specified in bios page 2, this routine reports the 1st
 * device scsi-ml or sas transport for persistent boot device
 * purposes.  Please refer to function _scsih_determine_boot_device()
 */
static void
_scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
{
11162
	u32 channel;
11163 11164 11165
	void *device;
	struct _sas_device *sas_device;
	struct _raid_device *raid_device;
11166
	struct _pcie_device *pcie_device;
11167 11168 11169 11170 11171
	u16 handle;
	u64 sas_address_parent;
	u64 sas_address;
	unsigned long flags;
	int rc;
11172
	int tid;
11173
	struct hba_port *port;
11174 11175 11176 11177 11178 11179 11180 11181

	 /* no Bios, return immediately */
	if (!ioc->bios_pg3.BiosVersion)
		return;

	device = NULL;
	if (ioc->req_boot_device.device) {
		device =  ioc->req_boot_device.device;
11182
		channel = ioc->req_boot_device.channel;
11183 11184
	} else if (ioc->req_alt_boot_device.device) {
		device =  ioc->req_alt_boot_device.device;
11185
		channel = ioc->req_alt_boot_device.channel;
11186 11187
	} else if (ioc->current_boot_device.device) {
		device =  ioc->current_boot_device.device;
11188
		channel = ioc->current_boot_device.channel;
11189 11190 11191 11192 11193
	}

	if (!device)
		return;

11194
	if (channel == RAID_CHANNEL) {
11195 11196 11197 11198 11199
		raid_device = device;
		rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
		    raid_device->id, 0);
		if (rc)
			_scsih_raid_device_remove(ioc, raid_device);
11200 11201 11202 11203 11204 11205 11206 11207 11208
	} else if (channel == PCIE_CHANNEL) {
		spin_lock_irqsave(&ioc->pcie_device_lock, flags);
		pcie_device = device;
		tid = pcie_device->id;
		list_move_tail(&pcie_device->list, &ioc->pcie_device_list);
		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
		rc = scsi_add_device(ioc->shost, PCIE_CHANNEL, tid, 0);
		if (rc)
			_scsih_pcie_device_remove(ioc, pcie_device);
11209 11210 11211 11212 11213 11214
	} else {
		spin_lock_irqsave(&ioc->sas_device_lock, flags);
		sas_device = device;
		handle = sas_device->handle;
		sas_address_parent = sas_device->sas_address_parent;
		sas_address = sas_device->sas_address;
11215
		port = sas_device->port;
11216 11217 11218
		list_move_tail(&sas_device->list, &ioc->sas_device_list);
		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);

11219 11220
		if (ioc->hide_drives)
			return;
11221 11222 11223 11224

		if (!port)
			return;

11225
		if (!mpt3sas_transport_port_add(ioc, handle,
11226
		    sas_address_parent, port)) {
11227 11228
			_scsih_sas_device_remove(ioc, sas_device);
		} else if (!sas_device->starget) {
11229 11230 11231
			if (!ioc->is_driver_loading) {
				mpt3sas_transport_port_remove(ioc,
				    sas_address,
11232
				    sas_address_parent, port);
11233 11234
				_scsih_sas_device_remove(ioc, sas_device);
			}
11235 11236 11237 11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253 11254 11255 11256 11257 11258 11259 11260 11261
		}
	}
}

/**
 * _scsih_probe_raid - reporting raid volumes to scsi-ml
 * @ioc: per adapter object
 *
 * Called during initial loading of the driver.
 */
static void
_scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
{
	struct _raid_device *raid_device, *raid_next;
	int rc;

	list_for_each_entry_safe(raid_device, raid_next,
	    &ioc->raid_device_list, list) {
		if (raid_device->starget)
			continue;
		rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
		    raid_device->id, 0);
		if (rc)
			_scsih_raid_device_remove(ioc, raid_device);
	}
}

11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288 11289 11290 11291 11292 11293 11294 11295 11296 11297 11298 11299 11300 11301 11302 11303
static struct _sas_device *get_next_sas_device(struct MPT3SAS_ADAPTER *ioc)
{
	struct _sas_device *sas_device = NULL;
	unsigned long flags;

	spin_lock_irqsave(&ioc->sas_device_lock, flags);
	if (!list_empty(&ioc->sas_device_init_list)) {
		sas_device = list_first_entry(&ioc->sas_device_init_list,
				struct _sas_device, list);
		sas_device_get(sas_device);
	}
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);

	return sas_device;
}

static void sas_device_make_active(struct MPT3SAS_ADAPTER *ioc,
		struct _sas_device *sas_device)
{
	unsigned long flags;

	spin_lock_irqsave(&ioc->sas_device_lock, flags);

	/*
	 * Since we dropped the lock during the call to port_add(), we need to
	 * be careful here that somebody else didn't move or delete this item
	 * while we were busy with other things.
	 *
	 * If it was on the list, we need a put() for the reference the list
	 * had. Either way, we need a get() for the destination list.
	 */
	if (!list_empty(&sas_device->list)) {
		list_del_init(&sas_device->list);
		sas_device_put(sas_device);
	}

	sas_device_get(sas_device);
	list_add_tail(&sas_device->list, &ioc->sas_device_list);

	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
}

11304 11305 11306 11307 11308 11309 11310 11311 11312
/**
 * _scsih_probe_sas - reporting sas devices to sas transport
 * @ioc: per adapter object
 *
 * Called during initial loading of the driver.
 */
static void
_scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
{
11313
	struct _sas_device *sas_device;
11314

11315 11316
	if (ioc->hide_drives)
		return;
11317

11318
	while ((sas_device = get_next_sas_device(ioc))) {
11319
		if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
11320
		    sas_device->sas_address_parent, sas_device->port)) {
11321 11322
			_scsih_sas_device_remove(ioc, sas_device);
			sas_device_put(sas_device);
11323 11324 11325 11326 11327 11328 11329 11330
			continue;
		} else if (!sas_device->starget) {
			/*
			 * When asyn scanning is enabled, its not possible to
			 * remove devices while scanning is turned on due to an
			 * oops in scsi_sysfs_add_sdev()->add_device()->
			 * sysfs_addrm_start()
			 */
11331
			if (!ioc->is_driver_loading) {
11332 11333
				mpt3sas_transport_port_remove(ioc,
				    sas_device->sas_address,
11334 11335
				    sas_device->sas_address_parent,
				    sas_device->port);
11336 11337
				_scsih_sas_device_remove(ioc, sas_device);
				sas_device_put(sas_device);
11338 11339
				continue;
			}
11340
		}
11341 11342
		sas_device_make_active(ioc, sas_device);
		sas_device_put(sas_device);
11343 11344 11345
	}
}

11346 11347 11348 11349 11350 11351
/**
 * get_next_pcie_device - Get the next pcie device
 * @ioc: per adapter object
 *
 * Get the next pcie device from pcie_device_init_list list.
 *
11352
 * Return: pcie device structure if pcie_device_init_list list is not empty
11353 11354 11355 11356 11357 11358 11359 11360 11361 11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 11377 11378 11379 11380 11381 11382 11383 11384 11385 11386 11387 11388 11389 11390 11391 11392 11393 11394 11395 11396 11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413
 * otherwise returns NULL
 */
static struct _pcie_device *get_next_pcie_device(struct MPT3SAS_ADAPTER *ioc)
{
	struct _pcie_device *pcie_device = NULL;
	unsigned long flags;

	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
	if (!list_empty(&ioc->pcie_device_init_list)) {
		pcie_device = list_first_entry(&ioc->pcie_device_init_list,
				struct _pcie_device, list);
		pcie_device_get(pcie_device);
	}
	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);

	return pcie_device;
}

/**
 * pcie_device_make_active - Add pcie device to pcie_device_list list
 * @ioc: per adapter object
 * @pcie_device: pcie device object
 *
 * Add the pcie device which has registered with SCSI Transport Later to
 * pcie_device_list list
 */
static void pcie_device_make_active(struct MPT3SAS_ADAPTER *ioc,
		struct _pcie_device *pcie_device)
{
	unsigned long flags;

	spin_lock_irqsave(&ioc->pcie_device_lock, flags);

	if (!list_empty(&pcie_device->list)) {
		list_del_init(&pcie_device->list);
		pcie_device_put(pcie_device);
	}
	pcie_device_get(pcie_device);
	list_add_tail(&pcie_device->list, &ioc->pcie_device_list);

	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
}

/**
 * _scsih_probe_pcie - reporting PCIe devices to scsi-ml
 * @ioc: per adapter object
 *
 * Called during initial loading of the driver.
 */
static void
_scsih_probe_pcie(struct MPT3SAS_ADAPTER *ioc)
{
	struct _pcie_device *pcie_device;
	int rc;

	/* PCIe Device List */
	while ((pcie_device = get_next_pcie_device(ioc))) {
		if (pcie_device->starget) {
			pcie_device_put(pcie_device);
			continue;
		}
11414 11415 11416 11417 11418 11419
		if (pcie_device->access_status ==
		    MPI26_PCIEDEV0_ASTATUS_DEVICE_BLOCKED) {
			pcie_device_make_active(ioc, pcie_device);
			pcie_device_put(pcie_device);
			continue;
		}
11420 11421 11422 11423 11424 11425 11426 11427 11428 11429 11430 11431 11432 11433 11434 11435 11436 11437 11438 11439 11440 11441 11442 11443 11444 11445 11446
		rc = scsi_add_device(ioc->shost, PCIE_CHANNEL,
			pcie_device->id, 0);
		if (rc) {
			_scsih_pcie_device_remove(ioc, pcie_device);
			pcie_device_put(pcie_device);
			continue;
		} else if (!pcie_device->starget) {
			/*
			 * When async scanning is enabled, its not possible to
			 * remove devices while scanning is turned on due to an
			 * oops in scsi_sysfs_add_sdev()->add_device()->
			 * sysfs_addrm_start()
			 */
			if (!ioc->is_driver_loading) {
			/* TODO-- Need to find out whether this condition will
			 * occur or not
			 */
				_scsih_pcie_device_remove(ioc, pcie_device);
				pcie_device_put(pcie_device);
				continue;
			}
		}
		pcie_device_make_active(ioc, pcie_device);
		pcie_device_put(pcie_device);
	}
}

11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459 11460 11461 11462 11463 11464 11465 11466 11467 11468 11469 11470 11471 11472 11473 11474
/**
 * _scsih_probe_devices - probing for devices
 * @ioc: per adapter object
 *
 * Called during initial loading of the driver.
 */
static void
_scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
{
	u16 volume_mapping_flags;

	if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
		return;  /* return when IOC doesn't support initiator mode */

	_scsih_probe_boot_devices(ioc);

	if (ioc->ir_firmware) {
		volume_mapping_flags =
		    le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
		    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
		if (volume_mapping_flags ==
		    MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
			_scsih_probe_raid(ioc);
			_scsih_probe_sas(ioc);
		} else {
			_scsih_probe_sas(ioc);
			_scsih_probe_raid(ioc);
		}
11475
	} else {
11476
		_scsih_probe_sas(ioc);
11477 11478
		_scsih_probe_pcie(ioc);
	}
11479 11480 11481
}

/**
11482
 * scsih_scan_start - scsi lld callback for .scan_start
11483 11484 11485 11486 11487 11488
 * @shost: SCSI host pointer
 *
 * The shost has the ability to discover targets on its own instead
 * of scanning the entire bus.  In our implemention, we will kick off
 * firmware discovery.
 */
11489
static void
11490
scsih_scan_start(struct Scsi_Host *shost)
11491 11492 11493 11494 11495
{
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	int rc;
	if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
		mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
11496 11497
	else if (ioc->manu_pg11.HostTraceBufferMaxSizeKB != 0)
		mpt3sas_enable_diag_buffer(ioc, 1);
11498 11499 11500 11501 11502 11503 11504 11505

	if (disable_discovery > 0)
		return;

	ioc->start_scan = 1;
	rc = mpt3sas_port_enable(ioc);

	if (rc != 0)
11506
		ioc_info(ioc, "port enable: FAILED\n");
11507 11508 11509
}

/**
11510
 * scsih_scan_finished - scsi lld callback for .scan_finished
11511 11512 11513 11514 11515 11516 11517
 * @shost: SCSI host pointer
 * @time: elapsed time of the scan in jiffies
 *
 * This function will be called periodicallyn until it returns 1 with the
 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
 * we wait for firmware discovery to complete, then return 1.
 */
11518
static int
11519
scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
11520 11521 11522 11523 11524 11525 11526 11527 11528 11529
{
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	if (disable_discovery > 0) {
		ioc->is_driver_loading = 0;
		ioc->wait_for_discovery_to_complete = 0;
		return 1;
	}

	if (time >= (300 * HZ)) {
11530
		ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
11531
		ioc_info(ioc, "port enable: FAILED with timeout (timeout=300s)\n");
11532 11533 11534 11535 11536 11537 11538 11539
		ioc->is_driver_loading = 0;
		return 1;
	}

	if (ioc->start_scan)
		return 0;

	if (ioc->start_scan_failed) {
11540 11541
		ioc_info(ioc, "port enable: FAILED with (ioc_status=0x%08x)\n",
			 ioc->start_scan_failed);
11542 11543 11544 11545 11546 11547
		ioc->is_driver_loading = 0;
		ioc->wait_for_discovery_to_complete = 0;
		ioc->remove_host = 1;
		return 1;
	}

11548
	ioc_info(ioc, "port enable: SUCCESS\n");
11549
	ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
11550 11551 11552 11553 11554 11555 11556 11557 11558 11559

	if (ioc->wait_for_discovery_to_complete) {
		ioc->wait_for_discovery_to_complete = 0;
		_scsih_probe_devices(ioc);
	}
	mpt3sas_base_start_watchdog(ioc);
	ioc->is_driver_loading = 0;
	return 1;
}

11560 11561 11562 11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 11573 11574 11575 11576 11577 11578 11579 11580 11581 11582 11583 11584 11585 11586
/* shost template for SAS 2.0 HBA devices */
static struct scsi_host_template mpt2sas_driver_template = {
	.module				= THIS_MODULE,
	.name				= "Fusion MPT SAS Host",
	.proc_name			= MPT2SAS_DRIVER_NAME,
	.queuecommand			= scsih_qcmd,
	.target_alloc			= scsih_target_alloc,
	.slave_alloc			= scsih_slave_alloc,
	.slave_configure		= scsih_slave_configure,
	.target_destroy			= scsih_target_destroy,
	.slave_destroy			= scsih_slave_destroy,
	.scan_finished			= scsih_scan_finished,
	.scan_start			= scsih_scan_start,
	.change_queue_depth		= scsih_change_queue_depth,
	.eh_abort_handler		= scsih_abort,
	.eh_device_reset_handler	= scsih_dev_reset,
	.eh_target_reset_handler	= scsih_target_reset,
	.eh_host_reset_handler		= scsih_host_reset,
	.bios_param			= scsih_bios_param,
	.can_queue			= 1,
	.this_id			= -1,
	.sg_tablesize			= MPT2SAS_SG_DEPTH,
	.max_sectors			= 32767,
	.cmd_per_lun			= 7,
	.shost_attrs			= mpt3sas_host_attrs,
	.sdev_attrs			= mpt3sas_dev_attrs,
	.track_queue_depth		= 1,
11587
	.cmd_size			= sizeof(struct scsiio_tracker),
11588 11589 11590 11591 11592 11593 11594 11595 11596
};

/* raid transport support for SAS 2.0 HBA devices */
static struct raid_function_template mpt2sas_raid_functions = {
	.cookie		= &mpt2sas_driver_template,
	.is_raid	= scsih_is_raid,
	.get_resync	= scsih_get_resync,
	.get_state	= scsih_get_state,
};
11597

11598 11599 11600 11601 11602 11603 11604 11605 11606 11607 11608 11609 11610 11611 11612 11613 11614 11615 11616 11617 11618 11619 11620
/* shost template for SAS 3.0 HBA devices */
static struct scsi_host_template mpt3sas_driver_template = {
	.module				= THIS_MODULE,
	.name				= "Fusion MPT SAS Host",
	.proc_name			= MPT3SAS_DRIVER_NAME,
	.queuecommand			= scsih_qcmd,
	.target_alloc			= scsih_target_alloc,
	.slave_alloc			= scsih_slave_alloc,
	.slave_configure		= scsih_slave_configure,
	.target_destroy			= scsih_target_destroy,
	.slave_destroy			= scsih_slave_destroy,
	.scan_finished			= scsih_scan_finished,
	.scan_start			= scsih_scan_start,
	.change_queue_depth		= scsih_change_queue_depth,
	.eh_abort_handler		= scsih_abort,
	.eh_device_reset_handler	= scsih_dev_reset,
	.eh_target_reset_handler	= scsih_target_reset,
	.eh_host_reset_handler		= scsih_host_reset,
	.bios_param			= scsih_bios_param,
	.can_queue			= 1,
	.this_id			= -1,
	.sg_tablesize			= MPT3SAS_SG_DEPTH,
	.max_sectors			= 32767,
11621
	.max_segment_size		= 0xffffffff,
11622 11623 11624 11625
	.cmd_per_lun			= 7,
	.shost_attrs			= mpt3sas_host_attrs,
	.sdev_attrs			= mpt3sas_dev_attrs,
	.track_queue_depth		= 1,
11626
	.cmd_size			= sizeof(struct scsiio_tracker),
11627 11628 11629 11630 11631 11632 11633 11634 11635 11636 11637 11638 11639 11640 11641 11642
};

/* raid transport support for SAS 3.0 HBA devices */
static struct raid_function_template mpt3sas_raid_functions = {
	.cookie		= &mpt3sas_driver_template,
	.is_raid	= scsih_is_raid,
	.get_resync	= scsih_get_resync,
	.get_state	= scsih_get_state,
};

/**
 * _scsih_determine_hba_mpi_version - determine in which MPI version class
 *					this device belongs to.
 * @pdev: PCI device struct
 *
 * return MPI2_VERSION for SAS 2.0 HBA devices,
11643 11644
 *	MPI25_VERSION for SAS 3.0 HBA devices, and
 *	MPI26 VERSION for Cutlass & Invader SAS 3.0 HBA devices
11645
 */
11646
static u16
11647 11648 11649 11650
_scsih_determine_hba_mpi_version(struct pci_dev *pdev)
{

	switch (pdev->device) {
11651
	case MPI2_MFGPAGE_DEVID_SSS6200:
11652 11653 11654 11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667
	case MPI2_MFGPAGE_DEVID_SAS2004:
	case MPI2_MFGPAGE_DEVID_SAS2008:
	case MPI2_MFGPAGE_DEVID_SAS2108_1:
	case MPI2_MFGPAGE_DEVID_SAS2108_2:
	case MPI2_MFGPAGE_DEVID_SAS2108_3:
	case MPI2_MFGPAGE_DEVID_SAS2116_1:
	case MPI2_MFGPAGE_DEVID_SAS2116_2:
	case MPI2_MFGPAGE_DEVID_SAS2208_1:
	case MPI2_MFGPAGE_DEVID_SAS2208_2:
	case MPI2_MFGPAGE_DEVID_SAS2208_3:
	case MPI2_MFGPAGE_DEVID_SAS2208_4:
	case MPI2_MFGPAGE_DEVID_SAS2208_5:
	case MPI2_MFGPAGE_DEVID_SAS2208_6:
	case MPI2_MFGPAGE_DEVID_SAS2308_1:
	case MPI2_MFGPAGE_DEVID_SAS2308_2:
	case MPI2_MFGPAGE_DEVID_SAS2308_3:
11668
	case MPI2_MFGPAGE_DEVID_SWITCH_MPI_EP:
11669
	case MPI2_MFGPAGE_DEVID_SWITCH_MPI_EP_1:
11670
		return MPI2_VERSION;
11671 11672 11673 11674 11675 11676
	case MPI25_MFGPAGE_DEVID_SAS3004:
	case MPI25_MFGPAGE_DEVID_SAS3008:
	case MPI25_MFGPAGE_DEVID_SAS3108_1:
	case MPI25_MFGPAGE_DEVID_SAS3108_2:
	case MPI25_MFGPAGE_DEVID_SAS3108_5:
	case MPI25_MFGPAGE_DEVID_SAS3108_6:
11677
		return MPI25_VERSION;
11678 11679 11680 11681 11682 11683 11684 11685 11686 11687
	case MPI26_MFGPAGE_DEVID_SAS3216:
	case MPI26_MFGPAGE_DEVID_SAS3224:
	case MPI26_MFGPAGE_DEVID_SAS3316_1:
	case MPI26_MFGPAGE_DEVID_SAS3316_2:
	case MPI26_MFGPAGE_DEVID_SAS3316_3:
	case MPI26_MFGPAGE_DEVID_SAS3316_4:
	case MPI26_MFGPAGE_DEVID_SAS3324_1:
	case MPI26_MFGPAGE_DEVID_SAS3324_2:
	case MPI26_MFGPAGE_DEVID_SAS3324_3:
	case MPI26_MFGPAGE_DEVID_SAS3324_4:
11688 11689 11690 11691 11692 11693
	case MPI26_MFGPAGE_DEVID_SAS3508:
	case MPI26_MFGPAGE_DEVID_SAS3508_1:
	case MPI26_MFGPAGE_DEVID_SAS3408:
	case MPI26_MFGPAGE_DEVID_SAS3516:
	case MPI26_MFGPAGE_DEVID_SAS3516_1:
	case MPI26_MFGPAGE_DEVID_SAS3416:
11694
	case MPI26_MFGPAGE_DEVID_SAS3616:
11695
	case MPI26_ATLAS_PCIe_SWITCH_DEVID:
11696 11697 11698 11699
	case MPI26_MFGPAGE_DEVID_CFG_SEC_3916:
	case MPI26_MFGPAGE_DEVID_HARD_SEC_3916:
	case MPI26_MFGPAGE_DEVID_CFG_SEC_3816:
	case MPI26_MFGPAGE_DEVID_HARD_SEC_3816:
11700 11701 11702 11703
	case MPI26_MFGPAGE_DEVID_INVALID0_3916:
	case MPI26_MFGPAGE_DEVID_INVALID1_3916:
	case MPI26_MFGPAGE_DEVID_INVALID0_3816:
	case MPI26_MFGPAGE_DEVID_INVALID1_3816:
11704
		return MPI26_VERSION;
11705
	}
11706
	return 0;
11707 11708
}

11709
/**
11710
 * _scsih_probe - attach and add scsi host
11711 11712 11713
 * @pdev: PCI device struct
 * @id: pci device id
 *
11714
 * Return: 0 success, anything else error.
11715
 */
11716
static int
11717
_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
11718 11719
{
	struct MPT3SAS_ADAPTER *ioc;
11720
	struct Scsi_Host *shost = NULL;
11721
	int rv;
11722 11723 11724 11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737
	u16 hba_mpi_version;

	/* Determine in which MPI version class this pci device belongs */
	hba_mpi_version = _scsih_determine_hba_mpi_version(pdev);
	if (hba_mpi_version == 0)
		return -ENODEV;

	/* Enumerate only SAS 2.0 HBA's if hbas_to_enumerate is one,
	 * for other generation HBA's return with -ENODEV
	 */
	if ((hbas_to_enumerate == 1) && (hba_mpi_version !=  MPI2_VERSION))
		return -ENODEV;

	/* Enumerate only SAS 3.0 HBA's if hbas_to_enumerate is two,
	 * for other generation HBA's return with -ENODEV
	 */
11738 11739
	if ((hbas_to_enumerate == 2) && (!(hba_mpi_version ==  MPI25_VERSION
		|| hba_mpi_version ==  MPI26_VERSION)))
11740 11741 11742 11743
		return -ENODEV;

	switch (hba_mpi_version) {
	case MPI2_VERSION:
11744 11745
		pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
			PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
11746 11747 11748 11749 11750 11751 11752 11753 11754 11755
		/* Use mpt2sas driver host template for SAS 2.0 HBA's */
		shost = scsi_host_alloc(&mpt2sas_driver_template,
		  sizeof(struct MPT3SAS_ADAPTER));
		if (!shost)
			return -ENODEV;
		ioc = shost_priv(shost);
		memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
		ioc->hba_mpi_version_belonged = hba_mpi_version;
		ioc->id = mpt2_ids++;
		sprintf(ioc->driver_name, "%s", MPT2SAS_DRIVER_NAME);
11756 11757
		switch (pdev->device) {
		case MPI2_MFGPAGE_DEVID_SSS6200:
11758 11759
			ioc->is_warpdrive = 1;
			ioc->hide_ir_msg = 1;
11760
			break;
11761
		case MPI2_MFGPAGE_DEVID_SWITCH_MPI_EP:
11762
		case MPI2_MFGPAGE_DEVID_SWITCH_MPI_EP_1:
11763 11764 11765
			ioc->is_mcpu_endpoint = 1;
			break;
		default:
11766
			ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
11767 11768
			break;
		}
11769 11770
		break;
	case MPI25_VERSION:
11771
	case MPI26_VERSION:
11772 11773 11774 11775 11776 11777 11778 11779 11780 11781
		/* Use mpt3sas driver host template for SAS 3.0 HBA's */
		shost = scsi_host_alloc(&mpt3sas_driver_template,
		  sizeof(struct MPT3SAS_ADAPTER));
		if (!shost)
			return -ENODEV;
		ioc = shost_priv(shost);
		memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
		ioc->hba_mpi_version_belonged = hba_mpi_version;
		ioc->id = mpt3_ids++;
		sprintf(ioc->driver_name, "%s", MPT3SAS_DRIVER_NAME);
11782 11783 11784 11785 11786 11787 11788
		switch (pdev->device) {
		case MPI26_MFGPAGE_DEVID_SAS3508:
		case MPI26_MFGPAGE_DEVID_SAS3508_1:
		case MPI26_MFGPAGE_DEVID_SAS3408:
		case MPI26_MFGPAGE_DEVID_SAS3516:
		case MPI26_MFGPAGE_DEVID_SAS3516_1:
		case MPI26_MFGPAGE_DEVID_SAS3416:
11789
		case MPI26_MFGPAGE_DEVID_SAS3616:
11790
		case MPI26_ATLAS_PCIe_SWITCH_DEVID:
11791 11792
			ioc->is_gen35_ioc = 1;
			break;
11793 11794 11795 11796 11797 11798 11799 11800 11801 11802 11803 11804 11805 11806
		case MPI26_MFGPAGE_DEVID_INVALID0_3816:
		case MPI26_MFGPAGE_DEVID_INVALID0_3916:
			dev_err(&pdev->dev,
			    "HBA with DeviceId 0x%04x, sub VendorId 0x%04x, sub DeviceId 0x%04x is Invalid",
			    pdev->device, pdev->subsystem_vendor,
			    pdev->subsystem_device);
			return 1;
		case MPI26_MFGPAGE_DEVID_INVALID1_3816:
		case MPI26_MFGPAGE_DEVID_INVALID1_3916:
			dev_err(&pdev->dev,
			    "HBA with DeviceId 0x%04x, sub VendorId 0x%04x, sub DeviceId 0x%04x is Tampered",
			    pdev->device, pdev->subsystem_vendor,
			    pdev->subsystem_device);
			return 1;
11807 11808 11809 11810
		case MPI26_MFGPAGE_DEVID_CFG_SEC_3816:
		case MPI26_MFGPAGE_DEVID_CFG_SEC_3916:
			dev_info(&pdev->dev,
			    "HBA is in Configurable Secure mode\n");
11811
			fallthrough;
11812 11813
		case MPI26_MFGPAGE_DEVID_HARD_SEC_3816:
		case MPI26_MFGPAGE_DEVID_HARD_SEC_3916:
11814
			ioc->is_aero_ioc = ioc->is_gen35_ioc = 1;
11815 11816
			break;
		default:
11817
			ioc->is_gen35_ioc = ioc->is_aero_ioc = 0;
11818
		}
11819 11820
		if ((ioc->hba_mpi_version_belonged == MPI25_VERSION &&
			pdev->revision >= SAS3_PCI_DEVICE_C0_REVISION) ||
11821 11822 11823 11824 11825 11826 11827 11828 11829
			(ioc->hba_mpi_version_belonged == MPI26_VERSION)) {
			ioc->combined_reply_queue = 1;
			if (ioc->is_gen35_ioc)
				ioc->combined_reply_index_count =
				 MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT_G35;
			else
				ioc->combined_reply_index_count =
				 MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT_G3;
		}
11830 11831 11832 11833
		break;
	default:
		return -ENODEV;
	}
11834 11835

	INIT_LIST_HEAD(&ioc->list);
11836
	spin_lock(&gioc_lock);
11837
	list_add_tail(&ioc->list, &mpt3sas_ioc_list);
11838
	spin_unlock(&gioc_lock);
11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 11849 11850 11851 11852 11853
	ioc->shost = shost;
	ioc->pdev = pdev;
	ioc->scsi_io_cb_idx = scsi_io_cb_idx;
	ioc->tm_cb_idx = tm_cb_idx;
	ioc->ctl_cb_idx = ctl_cb_idx;
	ioc->base_cb_idx = base_cb_idx;
	ioc->port_enable_cb_idx = port_enable_cb_idx;
	ioc->transport_cb_idx = transport_cb_idx;
	ioc->scsih_cb_idx = scsih_cb_idx;
	ioc->config_cb_idx = config_cb_idx;
	ioc->tm_tr_cb_idx = tm_tr_cb_idx;
	ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
	ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
	ioc->logging_level = logging_level;
	ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
11854 11855
	/* Host waits for minimum of six seconds */
	ioc->max_shutdown_latency = IO_UNIT_CONTROL_SHUTDOWN_TIMEOUT;
11856 11857 11858 11859
	/*
	 * Enable MEMORY MOVE support flag.
	 */
	ioc->drv_support_bitmap |= MPT_DRV_SUPPORT_BITMAP_MEMMOVE;
11860 11861 11862

	ioc->enable_sdev_max_qd = enable_sdev_max_qd;

11863 11864
	/* misc semaphores and spin locks */
	mutex_init(&ioc->reset_in_progress_mutex);
11865 11866
	/* initializing pci_access_mutex lock */
	mutex_init(&ioc->pci_access_mutex);
11867 11868 11869 11870 11871 11872
	spin_lock_init(&ioc->ioc_reset_in_progress_lock);
	spin_lock_init(&ioc->scsi_lookup_lock);
	spin_lock_init(&ioc->sas_device_lock);
	spin_lock_init(&ioc->sas_node_lock);
	spin_lock_init(&ioc->fw_event_lock);
	spin_lock_init(&ioc->raid_device_lock);
11873
	spin_lock_init(&ioc->pcie_device_lock);
11874 11875 11876 11877 11878
	spin_lock_init(&ioc->diag_trigger_lock);

	INIT_LIST_HEAD(&ioc->sas_device_list);
	INIT_LIST_HEAD(&ioc->sas_device_init_list);
	INIT_LIST_HEAD(&ioc->sas_expander_list);
11879
	INIT_LIST_HEAD(&ioc->enclosure_list);
11880 11881
	INIT_LIST_HEAD(&ioc->pcie_device_list);
	INIT_LIST_HEAD(&ioc->pcie_device_init_list);
11882 11883 11884 11885
	INIT_LIST_HEAD(&ioc->fw_event_list);
	INIT_LIST_HEAD(&ioc->raid_device_list);
	INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
	INIT_LIST_HEAD(&ioc->delayed_tr_list);
11886 11887
	INIT_LIST_HEAD(&ioc->delayed_sc_list);
	INIT_LIST_HEAD(&ioc->delayed_event_ack_list);
11888
	INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
11889
	INIT_LIST_HEAD(&ioc->reply_queue_list);
11890
	INIT_LIST_HEAD(&ioc->port_table_list);
11891

11892
	sprintf(ioc->name, "%s_cm%d", ioc->driver_name, ioc->id);
11893

11894 11895 11896 11897 11898 11899
	/* init shost parameters */
	shost->max_cmd_len = 32;
	shost->max_lun = max_lun;
	shost->transportt = mpt3sas_transport_template;
	shost->unique_id = ioc->id;

11900 11901 11902
	if (ioc->is_mcpu_endpoint) {
		/* mCPU MPI support 64K max IO */
		shost->max_sectors = 128;
11903 11904
		ioc_info(ioc, "The max_sectors value is set to %d\n",
			 shost->max_sectors);
11905 11906 11907 11908
	} else {
		if (max_sectors != 0xFFFF) {
			if (max_sectors < 64) {
				shost->max_sectors = 64;
11909 11910
				ioc_warn(ioc, "Invalid value %d passed for max_sectors, range is 64 to 32767. Assigning value of 64.\n",
					 max_sectors);
11911 11912
			} else if (max_sectors > 32767) {
				shost->max_sectors = 32767;
11913 11914
				ioc_warn(ioc, "Invalid value %d passed for max_sectors, range is 64 to 32767.Assigning default value of 32767.\n",
					 max_sectors);
11915 11916
			} else {
				shost->max_sectors = max_sectors & 0xFFFE;
11917 11918
				ioc_info(ioc, "The max_sectors value is set to %d\n",
					 shost->max_sectors);
11919
			}
11920 11921 11922
		}
	}
	/* register EEDP capabilities with SCSI layer */
11923 11924
	if (prot_mask >= 0)
		scsi_host_set_prot(shost, (prot_mask & 0x07));
11925 11926 11927 11928 11929 11930 11931 11932 11933
	else
		scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
				   | SHOST_DIF_TYPE2_PROTECTION
				   | SHOST_DIF_TYPE3_PROTECTION);

	scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);

	/* event thread */
	snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
11934
	    "fw_event_%s%d", ioc->driver_name, ioc->id);
11935
	ioc->firmware_event_thread = alloc_ordered_workqueue(
11936
	    ioc->firmware_event_name, 0);
11937
	if (!ioc->firmware_event_thread) {
11938 11939
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
11940
		rv = -ENODEV;
11941 11942 11943 11944 11945
		goto out_thread_fail;
	}

	ioc->is_driver_loading = 1;
	if ((mpt3sas_base_attach(ioc))) {
11946 11947
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
11948
		rv = -ENODEV;
11949 11950
		goto out_attach_fail;
	}
11951 11952 11953 11954 11955 11956 11957

	if (ioc->is_warpdrive) {
		if (ioc->mfg_pg10_hide_flag ==  MFG_PAGE10_EXPOSE_ALL_DISKS)
			ioc->hide_drives = 0;
		else if (ioc->mfg_pg10_hide_flag ==  MFG_PAGE10_HIDE_ALL_DISKS)
			ioc->hide_drives = 1;
		else {
11958
			if (mpt3sas_get_num_volumes(ioc))
11959 11960 11961 11962 11963 11964 11965
				ioc->hide_drives = 1;
			else
				ioc->hide_drives = 0;
		}
	} else
		ioc->hide_drives = 0;

11966 11967
	rv = scsi_add_host(shost, &pdev->dev);
	if (rv) {
11968 11969
		ioc_err(ioc, "failure at %s:%d/%s()!\n",
			__FILE__, __LINE__, __func__);
11970 11971 11972
		goto out_add_shost_fail;
	}

11973
	scsi_scan_host(shost);
11974
	mpt3sas_setup_debugfs(ioc);
11975
	return 0;
11976 11977
out_add_shost_fail:
	mpt3sas_base_detach(ioc);
11978 11979 11980
 out_attach_fail:
	destroy_workqueue(ioc->firmware_event_thread);
 out_thread_fail:
11981
	spin_lock(&gioc_lock);
11982
	list_del(&ioc->list);
11983
	spin_unlock(&gioc_lock);
11984
	scsi_host_put(shost);
11985
	return rv;
11986 11987 11988 11989
}

#ifdef CONFIG_PM
/**
11990
 * scsih_suspend - power management suspend main entry point
11991 11992 11993
 * @pdev: PCI device struct
 * @state: PM state change to (usually PCI_D3)
 *
11994
 * Return: 0 success, anything else error.
11995
 */
11996
static int
11997
scsih_suspend(struct pci_dev *pdev, pm_message_t state)
11998
{
11999 12000
	struct Scsi_Host *shost;
	struct MPT3SAS_ADAPTER *ioc;
12001
	pci_power_t device_state;
12002 12003 12004 12005 12006
	int rc;

	rc = _scsih_get_shost_and_ioc(pdev, &shost, &ioc);
	if (rc)
		return rc;
12007 12008 12009 12010

	mpt3sas_base_stop_watchdog(ioc);
	flush_scheduled_work();
	scsi_block_requests(shost);
12011
	_scsih_nvme_shutdown(ioc);
12012
	device_state = pci_choose_state(pdev, state);
12013 12014
	ioc_info(ioc, "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
		 pdev, pci_name(pdev), device_state);
12015 12016 12017 12018 12019 12020 12021 12022

	pci_save_state(pdev);
	mpt3sas_base_free_resources(ioc);
	pci_set_power_state(pdev, device_state);
	return 0;
}

/**
12023
 * scsih_resume - power management resume main entry point
12024 12025
 * @pdev: PCI device struct
 *
12026
 * Return: 0 success, anything else error.
12027
 */
12028
static int
12029
scsih_resume(struct pci_dev *pdev)
12030
{
12031 12032
	struct Scsi_Host *shost;
	struct MPT3SAS_ADAPTER *ioc;
12033 12034 12035
	pci_power_t device_state = pdev->current_state;
	int r;

12036 12037 12038 12039
	r = _scsih_get_shost_and_ioc(pdev, &shost, &ioc);
	if (r)
		return r;

12040 12041
	ioc_info(ioc, "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
		 pdev, pci_name(pdev), device_state);
12042 12043 12044 12045 12046 12047 12048 12049

	pci_set_power_state(pdev, PCI_D0);
	pci_enable_wake(pdev, PCI_D0, 0);
	pci_restore_state(pdev);
	ioc->pdev = pdev;
	r = mpt3sas_base_map_resources(ioc);
	if (r)
		return r;
12050
	ioc_info(ioc, "Issuing Hard Reset as part of OS Resume\n");
12051
	mpt3sas_base_hard_reset_handler(ioc, SOFT_RESET);
12052 12053 12054 12055 12056 12057 12058
	scsi_unblock_requests(shost);
	mpt3sas_base_start_watchdog(ioc);
	return 0;
}
#endif /* CONFIG_PM */

/**
12059
 * scsih_pci_error_detected - Called when a PCI error is detected.
12060 12061 12062 12063 12064
 * @pdev: PCI device struct
 * @state: PCI channel state
 *
 * Description: Called when a PCI error is detected.
 *
12065
 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT.
12066
 */
12067
static pci_ers_result_t
12068
scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
12069
{
12070 12071 12072 12073 12074
	struct Scsi_Host *shost;
	struct MPT3SAS_ADAPTER *ioc;

	if (_scsih_get_shost_and_ioc(pdev, &shost, &ioc))
		return PCI_ERS_RESULT_DISCONNECT;
12075

12076
	ioc_info(ioc, "PCI error: detected callback, state(%d)!!\n", state);
12077 12078 12079 12080 12081 12082 12083 12084 12085 12086 12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098

	switch (state) {
	case pci_channel_io_normal:
		return PCI_ERS_RESULT_CAN_RECOVER;
	case pci_channel_io_frozen:
		/* Fatal error, prepare for slot reset */
		ioc->pci_error_recovery = 1;
		scsi_block_requests(ioc->shost);
		mpt3sas_base_stop_watchdog(ioc);
		mpt3sas_base_free_resources(ioc);
		return PCI_ERS_RESULT_NEED_RESET;
	case pci_channel_io_perm_failure:
		/* Permanent error, prepare for device removal */
		ioc->pci_error_recovery = 1;
		mpt3sas_base_stop_watchdog(ioc);
		_scsih_flush_running_cmds(ioc);
		return PCI_ERS_RESULT_DISCONNECT;
	}
	return PCI_ERS_RESULT_NEED_RESET;
}

/**
12099
 * scsih_pci_slot_reset - Called when PCI slot has been reset.
12100 12101 12102 12103 12104 12105
 * @pdev: PCI device struct
 *
 * Description: This routine is called by the pci error recovery
 * code after the PCI slot has been reset, just before we
 * should resume normal operations.
 */
12106
static pci_ers_result_t
12107
scsih_pci_slot_reset(struct pci_dev *pdev)
12108
{
12109 12110
	struct Scsi_Host *shost;
	struct MPT3SAS_ADAPTER *ioc;
12111 12112
	int rc;

12113 12114 12115
	if (_scsih_get_shost_and_ioc(pdev, &shost, &ioc))
		return PCI_ERS_RESULT_DISCONNECT;

12116
	ioc_info(ioc, "PCI error: slot reset callback!!\n");
12117 12118 12119 12120 12121 12122 12123 12124

	ioc->pci_error_recovery = 0;
	ioc->pdev = pdev;
	pci_restore_state(pdev);
	rc = mpt3sas_base_map_resources(ioc);
	if (rc)
		return PCI_ERS_RESULT_DISCONNECT;

12125
	ioc_info(ioc, "Issuing Hard Reset as part of PCI Slot Reset\n");
12126
	rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
12127

12128 12129
	ioc_warn(ioc, "hard reset: %s\n",
		 (rc == 0) ? "success" : "failed");
12130 12131 12132 12133 12134 12135 12136 12137

	if (!rc)
		return PCI_ERS_RESULT_RECOVERED;
	else
		return PCI_ERS_RESULT_DISCONNECT;
}

/**
12138
 * scsih_pci_resume() - resume normal ops after PCI reset
12139 12140 12141 12142 12143 12144
 * @pdev: pointer to PCI device
 *
 * Called when the error recovery driver tells us that its
 * OK to resume normal operation. Use completion to allow
 * halted scsi ops to resume.
 */
12145
static void
12146
scsih_pci_resume(struct pci_dev *pdev)
12147
{
12148 12149 12150 12151 12152
	struct Scsi_Host *shost;
	struct MPT3SAS_ADAPTER *ioc;

	if (_scsih_get_shost_and_ioc(pdev, &shost, &ioc))
		return;
12153

12154
	ioc_info(ioc, "PCI error: resume callback!!\n");
12155 12156 12157 12158 12159 12160

	mpt3sas_base_start_watchdog(ioc);
	scsi_unblock_requests(ioc->shost);
}

/**
12161
 * scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
12162 12163
 * @pdev: pointer to PCI device
 */
12164
static pci_ers_result_t
12165
scsih_pci_mmio_enabled(struct pci_dev *pdev)
12166
{
12167 12168 12169 12170 12171
	struct Scsi_Host *shost;
	struct MPT3SAS_ADAPTER *ioc;

	if (_scsih_get_shost_and_ioc(pdev, &shost, &ioc))
		return PCI_ERS_RESULT_DISCONNECT;
12172

12173
	ioc_info(ioc, "PCI error: mmio enabled callback!!\n");
12174 12175 12176

	/* TODO - dump whatever for debugging purposes */

12177 12178 12179 12180 12181
	/* This called only if scsih_pci_error_detected returns
	 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
	 * works, no need to reset slot.
	 */
	return PCI_ERS_RESULT_RECOVERED;
12182 12183
}

12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208
/**
 * scsih__ncq_prio_supp - Check for NCQ command priority support
 * @sdev: scsi device struct
 *
 * This is called when a user indicates they would like to enable
 * ncq command priorities. This works only on SATA devices.
 */
bool scsih_ncq_prio_supp(struct scsi_device *sdev)
{
	unsigned char *buf;
	bool ncq_prio_supp = false;

	if (!scsi_device_supports_vpd(sdev))
		return ncq_prio_supp;

	buf = kmalloc(SCSI_VPD_PG_LEN, GFP_KERNEL);
	if (!buf)
		return ncq_prio_supp;

	if (!scsi_get_vpd_page(sdev, 0x89, buf, SCSI_VPD_PG_LEN))
		ncq_prio_supp = (buf[213] >> 4) & 1;

	kfree(buf);
	return ncq_prio_supp;
}
12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 12244 12245 12246 12247 12248 12249 12250
/*
 * The pci device ids are defined in mpi/mpi2_cnfg.h.
 */
static const struct pci_device_id mpt3sas_pci_table[] = {
	/* Spitfire ~ 2004 */
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
		PCI_ANY_ID, PCI_ANY_ID },
	/* Falcon ~ 2008 */
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
		PCI_ANY_ID, PCI_ANY_ID },
	/* Liberator ~ 2108 */
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
		PCI_ANY_ID, PCI_ANY_ID },
	/* Meteor ~ 2116 */
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
		PCI_ANY_ID, PCI_ANY_ID },
	/* Thunderbolt ~ 2208 */
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
		PCI_ANY_ID, PCI_ANY_ID },
	/* Mustang ~ 2308 */
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
		PCI_ANY_ID, PCI_ANY_ID },
12251
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SWITCH_MPI_EP,
12252
		PCI_ANY_ID, PCI_ANY_ID },
12253 12254
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SWITCH_MPI_EP_1,
		PCI_ANY_ID, PCI_ANY_ID },
12255 12256 12257 12258 12259 12260 12261 12262 12263 12264 12265 12266 12267 12268 12269 12270 12271
	/* SSS6200 */
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
		PCI_ANY_ID, PCI_ANY_ID },
	/* Fury ~ 3004 and 3008 */
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
		PCI_ANY_ID, PCI_ANY_ID },
	/* Invader ~ 3108 */
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
		PCI_ANY_ID, PCI_ANY_ID },
12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293
	/* Cutlass ~ 3216 and 3224 */
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3216,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3224,
		PCI_ANY_ID, PCI_ANY_ID },
	/* Intruder ~ 3316 and 3324 */
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_1,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_2,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_3,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_4,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_1,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_2,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_3,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_4,
		PCI_ANY_ID, PCI_ANY_ID },
12294 12295 12296 12297 12298 12299 12300 12301 12302 12303 12304 12305 12306
	/* Ventura, Crusader, Harpoon & Tomcat ~ 3516, 3416, 3508 & 3408*/
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3508,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3508_1,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3408,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3516,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3516_1,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3416,
		PCI_ANY_ID, PCI_ANY_ID },
12307 12308 12309
	/* Mercator ~ 3616*/
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3616,
		PCI_ANY_ID, PCI_ANY_ID },
12310 12311 12312 12313 12314 12315 12316 12317 12318

	/* Aero SI 0x00E1 Configurable Secure
	 * 0x00E2 Hard Secure
	 */
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_CFG_SEC_3916,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_HARD_SEC_3916,
		PCI_ANY_ID, PCI_ANY_ID },

12319 12320 12321 12322 12323 12324 12325 12326
	/*
	 *  Aero SI –> 0x00E0 Invalid, 0x00E3 Tampered
	 */
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_INVALID0_3916,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_INVALID1_3916,
		PCI_ANY_ID, PCI_ANY_ID },

12327 12328 12329 12330
	/* Atlas PCIe Switch Management Port */
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_ATLAS_PCIe_SWITCH_DEVID,
		PCI_ANY_ID, PCI_ANY_ID },

12331 12332 12333 12334 12335 12336 12337 12338
	/* Sea SI 0x00E5 Configurable Secure
	 * 0x00E6 Hard Secure
	 */
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_CFG_SEC_3816,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_HARD_SEC_3816,
		PCI_ANY_ID, PCI_ANY_ID },

12339 12340 12341 12342 12343 12344 12345 12346
	/*
	 *  Sea SI –> 0x00E4 Invalid, 0x00E7 Tampered
	 */
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_INVALID0_3816,
		PCI_ANY_ID, PCI_ANY_ID },
	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_INVALID1_3816,
		PCI_ANY_ID, PCI_ANY_ID },

12347 12348 12349 12350 12351 12352 12353 12354 12355 12356 12357 12358 12359 12360 12361 12362 12363 12364 12365 12366 12367 12368 12369 12370
	{0}     /* Terminating entry */
};
MODULE_DEVICE_TABLE(pci, mpt3sas_pci_table);

static struct pci_error_handlers _mpt3sas_err_handler = {
	.error_detected	= scsih_pci_error_detected,
	.mmio_enabled	= scsih_pci_mmio_enabled,
	.slot_reset	= scsih_pci_slot_reset,
	.resume		= scsih_pci_resume,
};

static struct pci_driver mpt3sas_driver = {
	.name		= MPT3SAS_DRIVER_NAME,
	.id_table	= mpt3sas_pci_table,
	.probe		= _scsih_probe,
	.remove		= scsih_remove,
	.shutdown	= scsih_shutdown,
	.err_handler	= &_mpt3sas_err_handler,
#ifdef CONFIG_PM
	.suspend	= scsih_suspend,
	.resume		= scsih_resume,
#endif
};

12371
/**
12372
 * scsih_init - main entry point for this driver.
12373
 *
12374
 * Return: 0 success, anything else error.
12375
 */
12376
static int
12377
scsih_init(void)
12378
{
12379 12380
	mpt2_ids = 0;
	mpt3_ids = 0;
12381 12382 12383 12384 12385 12386

	mpt3sas_base_initialize_callback_handler();

	 /* queuecommand callback hander */
	scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);

12387
	/* task management callback handler */
12388 12389 12390 12391 12392 12393 12394 12395 12396 12397 12398 12399 12400 12401 12402 12403 12404 12405 12406 12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417
	tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);

	/* base internal commands callback handler */
	base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
	port_enable_cb_idx = mpt3sas_base_register_callback_handler(
	    mpt3sas_port_enable_done);

	/* transport internal commands callback handler */
	transport_cb_idx = mpt3sas_base_register_callback_handler(
	    mpt3sas_transport_done);

	/* scsih internal commands callback handler */
	scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);

	/* configuration page API internal commands callback handler */
	config_cb_idx = mpt3sas_base_register_callback_handler(
	    mpt3sas_config_done);

	/* ctl module callback handler */
	ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);

	tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
	    _scsih_tm_tr_complete);

	tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
	    _scsih_tm_volume_tr_complete);

	tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
	    _scsih_sas_control_complete);

12418
	mpt3sas_init_debugfs();
12419
	return 0;
12420 12421 12422
}

/**
12423
 * scsih_exit - exit point for this driver (when it is a module).
12424
 *
12425
 * Return: 0 success, anything else error.
12426
 */
12427
static void
12428
scsih_exit(void)
12429 12430 12431 12432 12433 12434 12435 12436 12437 12438 12439 12440 12441 12442 12443 12444
{

	mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
	mpt3sas_base_release_callback_handler(tm_cb_idx);
	mpt3sas_base_release_callback_handler(base_cb_idx);
	mpt3sas_base_release_callback_handler(port_enable_cb_idx);
	mpt3sas_base_release_callback_handler(transport_cb_idx);
	mpt3sas_base_release_callback_handler(scsih_cb_idx);
	mpt3sas_base_release_callback_handler(config_cb_idx);
	mpt3sas_base_release_callback_handler(ctl_cb_idx);

	mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
	mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
	mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);

/* raid transport support */
12445 12446 12447 12448
	if (hbas_to_enumerate != 1)
		raid_class_release(mpt3sas_raid_template);
	if (hbas_to_enumerate != 2)
		raid_class_release(mpt2sas_raid_template);
12449
	sas_release_transport(mpt3sas_transport_template);
12450
	mpt3sas_exit_debugfs();
12451
}
12452

12453 12454 12455
/**
 * _mpt3sas_init - main entry point for this driver.
 *
12456
 * Return: 0 success, anything else error.
12457 12458 12459 12460 12461 12462 12463 12464 12465 12466 12467 12468 12469 12470 12471 12472 12473 12474 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 12485 12486 12487 12488 12489 12490 12491 12492 12493 12494 12495 12496 12497 12498 12499 12500 12501 12502 12503 12504 12505 12506 12507 12508 12509 12510 12511 12512 12513 12514 12515 12516 12517 12518 12519 12520 12521
 */
static int __init
_mpt3sas_init(void)
{
	int error;

	pr_info("%s version %s loaded\n", MPT3SAS_DRIVER_NAME,
					MPT3SAS_DRIVER_VERSION);

	mpt3sas_transport_template =
	    sas_attach_transport(&mpt3sas_transport_functions);
	if (!mpt3sas_transport_template)
		return -ENODEV;

	/* No need attach mpt3sas raid functions template
	 * if hbas_to_enumarate value is one.
	 */
	if (hbas_to_enumerate != 1) {
		mpt3sas_raid_template =
				raid_class_attach(&mpt3sas_raid_functions);
		if (!mpt3sas_raid_template) {
			sas_release_transport(mpt3sas_transport_template);
			return -ENODEV;
		}
	}

	/* No need to attach mpt2sas raid functions template
	 * if hbas_to_enumarate value is two
	 */
	if (hbas_to_enumerate != 2) {
		mpt2sas_raid_template =
				raid_class_attach(&mpt2sas_raid_functions);
		if (!mpt2sas_raid_template) {
			sas_release_transport(mpt3sas_transport_template);
			return -ENODEV;
		}
	}

	error = scsih_init();
	if (error) {
		scsih_exit();
		return error;
	}

	mpt3sas_ctl_init(hbas_to_enumerate);

	error = pci_register_driver(&mpt3sas_driver);
	if (error)
		scsih_exit();

	return error;
}

/**
 * _mpt3sas_exit - exit point for this driver (when it is a module).
 *
 */
static void __exit
_mpt3sas_exit(void)
{
	pr_info("mpt3sas version %s unloading\n",
				MPT3SAS_DRIVER_VERSION);

	mpt3sas_ctl_exit(hbas_to_enumerate);

12522 12523
	pci_unregister_driver(&mpt3sas_driver);

12524 12525 12526 12527 12528
	scsih_exit();
}

module_init(_mpt3sas_init);
module_exit(_mpt3sas_exit);