linux/drivers/scsi/mpt3sas/mpt3sas_scsih.c
<<
>>
Prefs
   1/*
   2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
   3 *
   4 * This code is based on drivers/scsi/mpt3sas/mpt3sas_scsih.c
   5 * Copyright (C) 2012-2014  LSI Corporation
   6 * Copyright (C) 2013-2014 Avago Technologies
   7 *  (mailto: MPT-FusionLinux.pdl@avagotech.com)
   8 *
   9 * This program is free software; you can redistribute it and/or
  10 * modify it under the terms of the GNU General Public License
  11 * as published by the Free Software Foundation; either version 2
  12 * of the License, or (at your option) any later version.
  13 *
  14 * This program is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17 * GNU General Public License for more details.
  18 *
  19 * NO WARRANTY
  20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  24 * solely responsible for determining the appropriateness of using and
  25 * distributing the Program and assumes all risks associated with its
  26 * exercise of rights under this Agreement, including but not limited to
  27 * the risks and costs of program errors, damage to or loss of data,
  28 * programs or equipment, and unavailability or interruption of operations.
  29
  30 * DISCLAIMER OF LIABILITY
  31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  38
  39 * You should have received a copy of the GNU General Public License
  40 * along with this program; if not, write to the Free Software
  41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
  42 * USA.
  43 */
  44
  45#include <linux/module.h>
  46#include <linux/kernel.h>
  47#include <linux/init.h>
  48#include <linux/errno.h>
  49#include <linux/blkdev.h>
  50#include <linux/sched.h>
  51#include <linux/workqueue.h>
  52#include <linux/delay.h>
  53#include <linux/pci.h>
  54#include <linux/interrupt.h>
  55#include <linux/aer.h>
  56#include <linux/raid_class.h>
  57#include <linux/blk-mq-pci.h>
  58#include <asm/unaligned.h>
  59
  60#include "mpt3sas_base.h"
  61
  62#define RAID_CHANNEL 1
  63
  64#define PCIE_CHANNEL 2
  65
  66/* forward proto's */
  67static void _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
  68        struct _sas_node *sas_expander);
  69static void _firmware_event_work(struct work_struct *work);
  70
  71static void _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
  72        struct _sas_device *sas_device);
  73static int _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
  74        u8 retry_count, u8 is_pd);
  75static int _scsih_pcie_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle);
  76static void _scsih_pcie_device_remove_from_sml(struct MPT3SAS_ADAPTER *ioc,
  77        struct _pcie_device *pcie_device);
  78static void
  79_scsih_pcie_check_device(struct MPT3SAS_ADAPTER *ioc, u16 handle);
  80static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);
  81
  82/* global parameters */
  83LIST_HEAD(mpt3sas_ioc_list);
  84/* global ioc lock for list operations */
  85DEFINE_SPINLOCK(gioc_lock);
  86
  87MODULE_AUTHOR(MPT3SAS_AUTHOR);
  88MODULE_DESCRIPTION(MPT3SAS_DESCRIPTION);
  89MODULE_LICENSE("GPL");
  90MODULE_VERSION(MPT3SAS_DRIVER_VERSION);
  91MODULE_ALIAS("mpt2sas");
  92
  93/* local parameters */
  94static u8 scsi_io_cb_idx = -1;
  95static u8 tm_cb_idx = -1;
  96static u8 ctl_cb_idx = -1;
  97static u8 base_cb_idx = -1;
  98static u8 port_enable_cb_idx = -1;
  99static u8 transport_cb_idx = -1;
 100static u8 scsih_cb_idx = -1;
 101static u8 config_cb_idx = -1;
 102static int mpt2_ids;
 103static int mpt3_ids;
 104
 105static u8 tm_tr_cb_idx = -1 ;
 106static u8 tm_tr_volume_cb_idx = -1 ;
 107static u8 tm_sas_control_cb_idx = -1;
 108
 109/* command line options */
 110static u32 logging_level;
 111MODULE_PARM_DESC(logging_level,
 112        " bits for enabling additional logging info (default=0)");
 113
 114
 115static ushort max_sectors = 0xFFFF;
 116module_param(max_sectors, ushort, 0444);
 117MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767  default=32767");
 118
 119
 120static int missing_delay[2] = {-1, -1};
 121module_param_array(missing_delay, int, NULL, 0444);
 122MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
 123
 124/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
 125#define MPT3SAS_MAX_LUN (16895)
 126static u64 max_lun = MPT3SAS_MAX_LUN;
 127module_param(max_lun, ullong, 0444);
 128MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
 129
 130static ushort hbas_to_enumerate;
 131module_param(hbas_to_enumerate, ushort, 0444);
 132MODULE_PARM_DESC(hbas_to_enumerate,
 133                " 0 - enumerates both SAS 2.0 & SAS 3.0 generation HBAs\n \
 134                  1 - enumerates only SAS 2.0 generation HBAs\n \
 135                  2 - enumerates only SAS 3.0 generation HBAs (default=0)");
 136
 137/* diag_buffer_enable is bitwise
 138 * bit 0 set = TRACE
 139 * bit 1 set = SNAPSHOT
 140 * bit 2 set = EXTENDED
 141 *
 142 * Either bit can be set, or both
 143 */
 144static int diag_buffer_enable = -1;
 145module_param(diag_buffer_enable, int, 0444);
 146MODULE_PARM_DESC(diag_buffer_enable,
 147        " post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
 148static int disable_discovery = -1;
 149module_param(disable_discovery, int, 0444);
 150MODULE_PARM_DESC(disable_discovery, " disable discovery ");
 151
 152
 153/* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
 154static int prot_mask = -1;
 155module_param(prot_mask, int, 0444);
 156MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
 157
 158static bool enable_sdev_max_qd;
 159module_param(enable_sdev_max_qd, bool, 0444);
 160MODULE_PARM_DESC(enable_sdev_max_qd,
 161        "Enable sdev max qd as can_queue, def=disabled(0)");
 162
 163static int multipath_on_hba = -1;
 164module_param(multipath_on_hba, int, 0);
 165MODULE_PARM_DESC(multipath_on_hba,
 166        "Multipath support to add same target device\n\t\t"
 167        "as many times as it is visible to HBA from various paths\n\t\t"
 168        "(by default:\n\t\t"
 169        "\t SAS 2.0 & SAS 3.0 HBA - This will be disabled,\n\t\t"
 170        "\t SAS 3.5 HBA - This will be enabled)");
 171
 172static int host_tagset_enable = 1;
 173module_param(host_tagset_enable, int, 0444);
 174MODULE_PARM_DESC(host_tagset_enable,
 175        "Shared host tagset enable/disable Default: enable(1)");
 176
 177/* raid transport support */
 178static struct raid_template *mpt3sas_raid_template;
 179static struct raid_template *mpt2sas_raid_template;
 180
 181
 182/**
 183 * struct sense_info - common structure for obtaining sense keys
 184 * @skey: sense key
 185 * @asc: additional sense code
 186 * @ascq: additional sense code qualifier
 187 */
 188struct sense_info {
 189        u8 skey;
 190        u8 asc;
 191        u8 ascq;
 192};
 193
 194#define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
 195#define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
 196#define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
 197#define MPT3SAS_ABRT_TASK_SET (0xFFFE)
 198#define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
 199/**
 200 * struct fw_event_work - firmware event struct
 201 * @list: link list framework
 202 * @work: work object (ioc->fault_reset_work_q)
 203 * @ioc: per adapter object
 204 * @device_handle: device handle
 205 * @VF_ID: virtual function id
 206 * @VP_ID: virtual port id
 207 * @ignore: flag meaning this event has been marked to ignore
 208 * @event: firmware event MPI2_EVENT_XXX defined in mpi2_ioc.h
 209 * @refcount: kref for this event
 210 * @event_data: reply event data payload follows
 211 *
 212 * This object stored on ioc->fw_event_list.
 213 */
 214struct fw_event_work {
 215        struct list_head        list;
 216        struct work_struct      work;
 217
 218        struct MPT3SAS_ADAPTER *ioc;
 219        u16                     device_handle;
 220        u8                      VF_ID;
 221        u8                      VP_ID;
 222        u8                      ignore;
 223        u16                     event;
 224        struct kref             refcount;
 225        char                    event_data[] __aligned(4);
 226};
 227
 228static void fw_event_work_free(struct kref *r)
 229{
 230        kfree(container_of(r, struct fw_event_work, refcount));
 231}
 232
 233static void fw_event_work_get(struct fw_event_work *fw_work)
 234{
 235        kref_get(&fw_work->refcount);
 236}
 237
 238static void fw_event_work_put(struct fw_event_work *fw_work)
 239{
 240        kref_put(&fw_work->refcount, fw_event_work_free);
 241}
 242
 243static struct fw_event_work *alloc_fw_event_work(int len)
 244{
 245        struct fw_event_work *fw_event;
 246
 247        fw_event = kzalloc(sizeof(*fw_event) + len, GFP_ATOMIC);
 248        if (!fw_event)
 249                return NULL;
 250
 251        kref_init(&fw_event->refcount);
 252        return fw_event;
 253}
 254
 255/**
 256 * struct _scsi_io_transfer - scsi io transfer
 257 * @handle: sas device handle (assigned by firmware)
 258 * @is_raid: flag set for hidden raid components
 259 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
 260 * @data_length: data transfer length
 261 * @data_dma: dma pointer to data
 262 * @sense: sense data
 263 * @lun: lun number
 264 * @cdb_length: cdb length
 265 * @cdb: cdb contents
 266 * @timeout: timeout for this command
 267 * @VF_ID: virtual function id
 268 * @VP_ID: virtual port id
 269 * @valid_reply: flag set for reply message
 270 * @sense_length: sense length
 271 * @ioc_status: ioc status
 272 * @scsi_state: scsi state
 273 * @scsi_status: scsi staus
 274 * @log_info: log information
 275 * @transfer_length: data length transfer when there is a reply message
 276 *
 277 * Used for sending internal scsi commands to devices within this module.
 278 * Refer to _scsi_send_scsi_io().
 279 */
 280struct _scsi_io_transfer {
 281        u16     handle;
 282        u8      is_raid;
 283        enum dma_data_direction dir;
 284        u32     data_length;
 285        dma_addr_t data_dma;
 286        u8      sense[SCSI_SENSE_BUFFERSIZE];
 287        u32     lun;
 288        u8      cdb_length;
 289        u8      cdb[32];
 290        u8      timeout;
 291        u8      VF_ID;
 292        u8      VP_ID;
 293        u8      valid_reply;
 294  /* the following bits are only valid when 'valid_reply = 1' */
 295        u32     sense_length;
 296        u16     ioc_status;
 297        u8      scsi_state;
 298        u8      scsi_status;
 299        u32     log_info;
 300        u32     transfer_length;
 301};
 302
 303/**
 304 * _scsih_set_debug_level - global setting of ioc->logging_level.
 305 * @val: ?
 306 * @kp: ?
 307 *
 308 * Note: The logging levels are defined in mpt3sas_debug.h.
 309 */
 310static int
 311_scsih_set_debug_level(const char *val, const struct kernel_param *kp)
 312{
 313        int ret = param_set_int(val, kp);
 314        struct MPT3SAS_ADAPTER *ioc;
 315
 316        if (ret)
 317                return ret;
 318
 319        pr_info("setting logging_level(0x%08x)\n", logging_level);
 320        spin_lock(&gioc_lock);
 321        list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
 322                ioc->logging_level = logging_level;
 323        spin_unlock(&gioc_lock);
 324        return 0;
 325}
 326module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
 327        &logging_level, 0644);
 328
 329/**
 330 * _scsih_srch_boot_sas_address - search based on sas_address
 331 * @sas_address: sas address
 332 * @boot_device: boot device object from bios page 2
 333 *
 334 * Return: 1 when there's a match, 0 means no match.
 335 */
 336static inline int
 337_scsih_srch_boot_sas_address(u64 sas_address,
 338        Mpi2BootDeviceSasWwid_t *boot_device)
 339{
 340        return (sas_address == le64_to_cpu(boot_device->SASAddress)) ?  1 : 0;
 341}
 342
 343/**
 344 * _scsih_srch_boot_device_name - search based on device name
 345 * @device_name: device name specified in INDENTIFY fram
 346 * @boot_device: boot device object from bios page 2
 347 *
 348 * Return: 1 when there's a match, 0 means no match.
 349 */
 350static inline int
 351_scsih_srch_boot_device_name(u64 device_name,
 352        Mpi2BootDeviceDeviceName_t *boot_device)
 353{
 354        return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
 355}
 356
 357/**
 358 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
 359 * @enclosure_logical_id: enclosure logical id
 360 * @slot_number: slot number
 361 * @boot_device: boot device object from bios page 2
 362 *
 363 * Return: 1 when there's a match, 0 means no match.
 364 */
 365static inline int
 366_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
 367        Mpi2BootDeviceEnclosureSlot_t *boot_device)
 368{
 369        return (enclosure_logical_id == le64_to_cpu(boot_device->
 370            EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
 371            SlotNumber)) ? 1 : 0;
 372}
 373
 374/**
 375 * mpt3sas_get_port_by_id - get hba port entry corresponding to provided
 376 *                        port number from port list
 377 * @ioc: per adapter object
 378 * @port_id: port number
 379 * @bypass_dirty_port_flag: when set look the matching hba port entry even
 380 *                      if hba port entry is marked as dirty.
 381 *
 382 * Search for hba port entry corresponding to provided port number,
 383 * if available return port object otherwise return NULL.
 384 */
 385struct hba_port *
 386mpt3sas_get_port_by_id(struct MPT3SAS_ADAPTER *ioc,
 387        u8 port_id, u8 bypass_dirty_port_flag)
 388{
 389        struct hba_port *port, *port_next;
 390
 391        /*
 392         * When multipath_on_hba is disabled then
 393         * search the hba_port entry using default
 394         * port id i.e. 255
 395         */
 396        if (!ioc->multipath_on_hba)
 397                port_id = MULTIPATH_DISABLED_PORT_ID;
 398
 399        list_for_each_entry_safe(port, port_next,
 400            &ioc->port_table_list, list) {
 401                if (port->port_id != port_id)
 402                        continue;
 403                if (bypass_dirty_port_flag)
 404                        return port;
 405                if (port->flags & HBA_PORT_FLAG_DIRTY_PORT)
 406                        continue;
 407                return port;
 408        }
 409
 410        /*
 411         * Allocate hba_port object for default port id (i.e. 255)
 412         * when multipath_on_hba is disabled for the HBA.
 413         * And add this object to port_table_list.
 414         */
 415        if (!ioc->multipath_on_hba) {
 416                port = kzalloc(sizeof(struct hba_port), GFP_ATOMIC);
 417                if (!port)
 418                        return NULL;
 419
 420                port->port_id = port_id;
 421                ioc_info(ioc,
 422                   "hba_port entry: %p, port: %d is added to hba_port list\n",
 423                   port, port->port_id);
 424                list_add_tail(&port->list,
 425                    &ioc->port_table_list);
 426                return port;
 427        }
 428        return NULL;
 429}
 430
 431/**
 432 * mpt3sas_get_vphy_by_phy - get virtual_phy object corresponding to phy number
 433 * @ioc: per adapter object
 434 * @port: hba_port object
 435 * @phy: phy number
 436 *
 437 * Return virtual_phy object corresponding to phy number.
 438 */
 439struct virtual_phy *
 440mpt3sas_get_vphy_by_phy(struct MPT3SAS_ADAPTER *ioc,
 441        struct hba_port *port, u32 phy)
 442{
 443        struct virtual_phy *vphy, *vphy_next;
 444
 445        if (!port->vphys_mask)
 446                return NULL;
 447
 448        list_for_each_entry_safe(vphy, vphy_next, &port->vphys_list, list) {
 449                if (vphy->phy_mask & (1 << phy))
 450                        return vphy;
 451        }
 452        return NULL;
 453}
 454
 455/**
 456 * _scsih_is_boot_device - search for matching boot device.
 457 * @sas_address: sas address
 458 * @device_name: device name specified in INDENTIFY fram
 459 * @enclosure_logical_id: enclosure logical id
 460 * @slot: slot number
 461 * @form: specifies boot device form
 462 * @boot_device: boot device object from bios page 2
 463 *
 464 * Return: 1 when there's a match, 0 means no match.
 465 */
 466static int
 467_scsih_is_boot_device(u64 sas_address, u64 device_name,
 468        u64 enclosure_logical_id, u16 slot, u8 form,
 469        Mpi2BiosPage2BootDevice_t *boot_device)
 470{
 471        int rc = 0;
 472
 473        switch (form) {
 474        case MPI2_BIOSPAGE2_FORM_SAS_WWID:
 475                if (!sas_address)
 476                        break;
 477                rc = _scsih_srch_boot_sas_address(
 478                    sas_address, &boot_device->SasWwid);
 479                break;
 480        case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
 481                if (!enclosure_logical_id)
 482                        break;
 483                rc = _scsih_srch_boot_encl_slot(
 484                    enclosure_logical_id,
 485                    slot, &boot_device->EnclosureSlot);
 486                break;
 487        case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
 488                if (!device_name)
 489                        break;
 490                rc = _scsih_srch_boot_device_name(
 491                    device_name, &boot_device->DeviceName);
 492                break;
 493        case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
 494                break;
 495        }
 496
 497        return rc;
 498}
 499
 500/**
 501 * _scsih_get_sas_address - set the sas_address for given device handle
 502 * @ioc: ?
 503 * @handle: device handle
 504 * @sas_address: sas address
 505 *
 506 * Return: 0 success, non-zero when failure
 507 */
 508static int
 509_scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
 510        u64 *sas_address)
 511{
 512        Mpi2SasDevicePage0_t sas_device_pg0;
 513        Mpi2ConfigReply_t mpi_reply;
 514        u32 ioc_status;
 515
 516        *sas_address = 0;
 517
 518        if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
 519            MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
 520                ioc_err(ioc, "failure at %s:%d/%s()!\n",
 521                        __FILE__, __LINE__, __func__);
 522                return -ENXIO;
 523        }
 524
 525        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
 526        if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
 527                /* For HBA, vSES doesn't return HBA SAS address. Instead return
 528                 * vSES's sas address.
 529                 */
 530                if ((handle <= ioc->sas_hba.num_phys) &&
 531                   (!(le32_to_cpu(sas_device_pg0.DeviceInfo) &
 532                   MPI2_SAS_DEVICE_INFO_SEP)))
 533                        *sas_address = ioc->sas_hba.sas_address;
 534                else
 535                        *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
 536                return 0;
 537        }
 538
 539        /* we hit this because the given parent handle doesn't exist */
 540        if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
 541                return -ENXIO;
 542
 543        /* else error case */
 544        ioc_err(ioc, "handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
 545                handle, ioc_status, __FILE__, __LINE__, __func__);
 546        return -EIO;
 547}
 548
 549/**
 550 * _scsih_determine_boot_device - determine boot device.
 551 * @ioc: per adapter object
 552 * @device: sas_device or pcie_device object
 553 * @channel: SAS or PCIe channel
 554 *
 555 * Determines whether this device should be first reported device to
 556 * to scsi-ml or sas transport, this purpose is for persistent boot device.
 557 * There are primary, alternate, and current entries in bios page 2. The order
 558 * priority is primary, alternate, then current.  This routine saves
 559 * the corresponding device object.
 560 * The saved data to be used later in _scsih_probe_boot_devices().
 561 */
 562static void
 563_scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc, void *device,
 564        u32 channel)
 565{
 566        struct _sas_device *sas_device;
 567        struct _pcie_device *pcie_device;
 568        struct _raid_device *raid_device;
 569        u64 sas_address;
 570        u64 device_name;
 571        u64 enclosure_logical_id;
 572        u16 slot;
 573
 574         /* only process this function when driver loads */
 575        if (!ioc->is_driver_loading)
 576                return;
 577
 578         /* no Bios, return immediately */
 579        if (!ioc->bios_pg3.BiosVersion)
 580                return;
 581
 582        if (channel == RAID_CHANNEL) {
 583                raid_device = device;
 584                sas_address = raid_device->wwid;
 585                device_name = 0;
 586                enclosure_logical_id = 0;
 587                slot = 0;
 588        } else if (channel == PCIE_CHANNEL) {
 589                pcie_device = device;
 590                sas_address = pcie_device->wwid;
 591                device_name = 0;
 592                enclosure_logical_id = 0;
 593                slot = 0;
 594        } else {
 595                sas_device = device;
 596                sas_address = sas_device->sas_address;
 597                device_name = sas_device->device_name;
 598                enclosure_logical_id = sas_device->enclosure_logical_id;
 599                slot = sas_device->slot;
 600        }
 601
 602        if (!ioc->req_boot_device.device) {
 603                if (_scsih_is_boot_device(sas_address, device_name,
 604                    enclosure_logical_id, slot,
 605                    (ioc->bios_pg2.ReqBootDeviceForm &
 606                    MPI2_BIOSPAGE2_FORM_MASK),
 607                    &ioc->bios_pg2.RequestedBootDevice)) {
 608                        dinitprintk(ioc,
 609                                    ioc_info(ioc, "%s: req_boot_device(0x%016llx)\n",
 610                                             __func__, (u64)sas_address));
 611                        ioc->req_boot_device.device = device;
 612                        ioc->req_boot_device.channel = channel;
 613                }
 614        }
 615
 616        if (!ioc->req_alt_boot_device.device) {
 617                if (_scsih_is_boot_device(sas_address, device_name,
 618                    enclosure_logical_id, slot,
 619                    (ioc->bios_pg2.ReqAltBootDeviceForm &
 620                    MPI2_BIOSPAGE2_FORM_MASK),
 621                    &ioc->bios_pg2.RequestedAltBootDevice)) {
 622                        dinitprintk(ioc,
 623                                    ioc_info(ioc, "%s: req_alt_boot_device(0x%016llx)\n",
 624                                             __func__, (u64)sas_address));
 625                        ioc->req_alt_boot_device.device = device;
 626                        ioc->req_alt_boot_device.channel = channel;
 627                }
 628        }
 629
 630        if (!ioc->current_boot_device.device) {
 631                if (_scsih_is_boot_device(sas_address, device_name,
 632                    enclosure_logical_id, slot,
 633                    (ioc->bios_pg2.CurrentBootDeviceForm &
 634                    MPI2_BIOSPAGE2_FORM_MASK),
 635                    &ioc->bios_pg2.CurrentBootDevice)) {
 636                        dinitprintk(ioc,
 637                                    ioc_info(ioc, "%s: current_boot_device(0x%016llx)\n",
 638                                             __func__, (u64)sas_address));
 639                        ioc->current_boot_device.device = device;
 640                        ioc->current_boot_device.channel = channel;
 641                }
 642        }
 643}
 644
 645static struct _sas_device *
 646__mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
 647                struct MPT3SAS_TARGET *tgt_priv)
 648{
 649        struct _sas_device *ret;
 650
 651        assert_spin_locked(&ioc->sas_device_lock);
 652
 653        ret = tgt_priv->sas_dev;
 654        if (ret)
 655                sas_device_get(ret);
 656
 657        return ret;
 658}
 659
 660static struct _sas_device *
 661mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
 662                struct MPT3SAS_TARGET *tgt_priv)
 663{
 664        struct _sas_device *ret;
 665        unsigned long flags;
 666
 667        spin_lock_irqsave(&ioc->sas_device_lock, flags);
 668        ret = __mpt3sas_get_sdev_from_target(ioc, tgt_priv);
 669        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 670
 671        return ret;
 672}
 673
 674static struct _pcie_device *
 675__mpt3sas_get_pdev_from_target(struct MPT3SAS_ADAPTER *ioc,
 676        struct MPT3SAS_TARGET *tgt_priv)
 677{
 678        struct _pcie_device *ret;
 679
 680        assert_spin_locked(&ioc->pcie_device_lock);
 681
 682        ret = tgt_priv->pcie_dev;
 683        if (ret)
 684                pcie_device_get(ret);
 685
 686        return ret;
 687}
 688
 689/**
 690 * mpt3sas_get_pdev_from_target - pcie device search
 691 * @ioc: per adapter object
 692 * @tgt_priv: starget private object
 693 *
 694 * Context: This function will acquire ioc->pcie_device_lock and will release
 695 * before returning the pcie_device object.
 696 *
 697 * This searches for pcie_device from target, then return pcie_device object.
 698 */
 699static struct _pcie_device *
 700mpt3sas_get_pdev_from_target(struct MPT3SAS_ADAPTER *ioc,
 701        struct MPT3SAS_TARGET *tgt_priv)
 702{
 703        struct _pcie_device *ret;
 704        unsigned long flags;
 705
 706        spin_lock_irqsave(&ioc->pcie_device_lock, flags);
 707        ret = __mpt3sas_get_pdev_from_target(ioc, tgt_priv);
 708        spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
 709
 710        return ret;
 711}
 712
 713
 714/**
 715 * __mpt3sas_get_sdev_by_rphy - sas device search
 716 * @ioc: per adapter object
 717 * @rphy: sas_rphy pointer
 718 *
 719 * Context: This function will acquire ioc->sas_device_lock and will release
 720 * before returning the sas_device object.
 721 *
 722 * This searches for sas_device from rphy object
 723 * then return sas_device object.
 724 */
 725struct _sas_device *
 726__mpt3sas_get_sdev_by_rphy(struct MPT3SAS_ADAPTER *ioc,
 727        struct sas_rphy *rphy)
 728{
 729        struct _sas_device *sas_device;
 730
 731        assert_spin_locked(&ioc->sas_device_lock);
 732
 733        list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
 734                if (sas_device->rphy != rphy)
 735                        continue;
 736                sas_device_get(sas_device);
 737                return sas_device;
 738        }
 739
 740        sas_device = NULL;
 741        list_for_each_entry(sas_device, &ioc->sas_device_init_list, list) {
 742                if (sas_device->rphy != rphy)
 743                        continue;
 744                sas_device_get(sas_device);
 745                return sas_device;
 746        }
 747
 748        return NULL;
 749}
 750
 751/**
 752 * __mpt3sas_get_sdev_by_addr - get _sas_device object corresponding to provided
 753 *                              sas address from sas_device_list list
 754 * @ioc: per adapter object
 755 * @sas_address: device sas address
 756 * @port: port number
 757 *
 758 * Search for _sas_device object corresponding to provided sas address,
 759 * if available return _sas_device object address otherwise return NULL.
 760 */
 761struct _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        if (!port)
 768                return NULL;
 769
 770        assert_spin_locked(&ioc->sas_device_lock);
 771
 772        list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
 773                if (sas_device->sas_address != sas_address)
 774                        continue;
 775                if (sas_device->port != port)
 776                        continue;
 777                sas_device_get(sas_device);
 778                return sas_device;
 779        }
 780
 781        list_for_each_entry(sas_device, &ioc->sas_device_init_list, list) {
 782                if (sas_device->sas_address != sas_address)
 783                        continue;
 784                if (sas_device->port != port)
 785                        continue;
 786                sas_device_get(sas_device);
 787                return sas_device;
 788        }
 789
 790        return NULL;
 791}
 792
 793/**
 794 * mpt3sas_get_sdev_by_addr - sas device search
 795 * @ioc: per adapter object
 796 * @sas_address: sas address
 797 * @port: hba port entry
 798 * Context: Calling function should acquire ioc->sas_device_lock
 799 *
 800 * This searches for sas_device based on sas_address & port number,
 801 * then return sas_device object.
 802 */
 803struct _sas_device *
 804mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
 805        u64 sas_address, struct hba_port *port)
 806{
 807        struct _sas_device *sas_device;
 808        unsigned long flags;
 809
 810        spin_lock_irqsave(&ioc->sas_device_lock, flags);
 811        sas_device = __mpt3sas_get_sdev_by_addr(ioc,
 812            sas_address, port);
 813        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 814
 815        return sas_device;
 816}
 817
 818static struct _sas_device *
 819__mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
 820{
 821        struct _sas_device *sas_device;
 822
 823        assert_spin_locked(&ioc->sas_device_lock);
 824
 825        list_for_each_entry(sas_device, &ioc->sas_device_list, list)
 826                if (sas_device->handle == handle)
 827                        goto found_device;
 828
 829        list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
 830                if (sas_device->handle == handle)
 831                        goto found_device;
 832
 833        return NULL;
 834
 835found_device:
 836        sas_device_get(sas_device);
 837        return sas_device;
 838}
 839
 840/**
 841 * mpt3sas_get_sdev_by_handle - sas device search
 842 * @ioc: per adapter object
 843 * @handle: sas device handle (assigned by firmware)
 844 * Context: Calling function should acquire ioc->sas_device_lock
 845 *
 846 * This searches for sas_device based on sas_address, then return sas_device
 847 * object.
 848 */
 849struct _sas_device *
 850mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
 851{
 852        struct _sas_device *sas_device;
 853        unsigned long flags;
 854
 855        spin_lock_irqsave(&ioc->sas_device_lock, flags);
 856        sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
 857        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 858
 859        return sas_device;
 860}
 861
 862/**
 863 * _scsih_display_enclosure_chassis_info - display device location info
 864 * @ioc: per adapter object
 865 * @sas_device: per sas device object
 866 * @sdev: scsi device struct
 867 * @starget: scsi target struct
 868 */
 869static void
 870_scsih_display_enclosure_chassis_info(struct MPT3SAS_ADAPTER *ioc,
 871        struct _sas_device *sas_device, struct scsi_device *sdev,
 872        struct scsi_target *starget)
 873{
 874        if (sdev) {
 875                if (sas_device->enclosure_handle != 0)
 876                        sdev_printk(KERN_INFO, sdev,
 877                            "enclosure logical id (0x%016llx), slot(%d) \n",
 878                            (unsigned long long)
 879                            sas_device->enclosure_logical_id,
 880                            sas_device->slot);
 881                if (sas_device->connector_name[0] != '\0')
 882                        sdev_printk(KERN_INFO, sdev,
 883                            "enclosure level(0x%04x), connector name( %s)\n",
 884                            sas_device->enclosure_level,
 885                            sas_device->connector_name);
 886                if (sas_device->is_chassis_slot_valid)
 887                        sdev_printk(KERN_INFO, sdev, "chassis slot(0x%04x)\n",
 888                            sas_device->chassis_slot);
 889        } else if (starget) {
 890                if (sas_device->enclosure_handle != 0)
 891                        starget_printk(KERN_INFO, starget,
 892                            "enclosure logical id(0x%016llx), slot(%d) \n",
 893                            (unsigned long long)
 894                            sas_device->enclosure_logical_id,
 895                            sas_device->slot);
 896                if (sas_device->connector_name[0] != '\0')
 897                        starget_printk(KERN_INFO, starget,
 898                            "enclosure level(0x%04x), connector name( %s)\n",
 899                            sas_device->enclosure_level,
 900                            sas_device->connector_name);
 901                if (sas_device->is_chassis_slot_valid)
 902                        starget_printk(KERN_INFO, starget,
 903                            "chassis slot(0x%04x)\n",
 904                            sas_device->chassis_slot);
 905        } else {
 906                if (sas_device->enclosure_handle != 0)
 907                        ioc_info(ioc, "enclosure logical id(0x%016llx), slot(%d)\n",
 908                                 (u64)sas_device->enclosure_logical_id,
 909                                 sas_device->slot);
 910                if (sas_device->connector_name[0] != '\0')
 911                        ioc_info(ioc, "enclosure level(0x%04x), connector name( %s)\n",
 912                                 sas_device->enclosure_level,
 913                                 sas_device->connector_name);
 914                if (sas_device->is_chassis_slot_valid)
 915                        ioc_info(ioc, "chassis slot(0x%04x)\n",
 916                                 sas_device->chassis_slot);
 917        }
 918}
 919
 920/**
 921 * _scsih_sas_device_remove - remove sas_device from list.
 922 * @ioc: per adapter object
 923 * @sas_device: the sas_device object
 924 * Context: This function will acquire ioc->sas_device_lock.
 925 *
 926 * If sas_device is on the list, remove it and decrement its reference count.
 927 */
 928static void
 929_scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
 930        struct _sas_device *sas_device)
 931{
 932        unsigned long flags;
 933
 934        if (!sas_device)
 935                return;
 936        ioc_info(ioc, "removing handle(0x%04x), sas_addr(0x%016llx)\n",
 937                 sas_device->handle, (u64)sas_device->sas_address);
 938
 939        _scsih_display_enclosure_chassis_info(ioc, sas_device, NULL, NULL);
 940
 941        /*
 942         * The lock serializes access to the list, but we still need to verify
 943         * that nobody removed the entry while we were waiting on the lock.
 944         */
 945        spin_lock_irqsave(&ioc->sas_device_lock, flags);
 946        if (!list_empty(&sas_device->list)) {
 947                list_del_init(&sas_device->list);
 948                sas_device_put(sas_device);
 949        }
 950        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 951}
 952
 953/**
 954 * _scsih_device_remove_by_handle - removing device object by handle
 955 * @ioc: per adapter object
 956 * @handle: device handle
 957 */
 958static void
 959_scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
 960{
 961        struct _sas_device *sas_device;
 962        unsigned long flags;
 963
 964        if (ioc->shost_recovery)
 965                return;
 966
 967        spin_lock_irqsave(&ioc->sas_device_lock, flags);
 968        sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
 969        if (sas_device) {
 970                list_del_init(&sas_device->list);
 971                sas_device_put(sas_device);
 972        }
 973        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 974        if (sas_device) {
 975                _scsih_remove_device(ioc, sas_device);
 976                sas_device_put(sas_device);
 977        }
 978}
 979
 980/**
 981 * mpt3sas_device_remove_by_sas_address - removing device object by
 982 *                                      sas address & port number
 983 * @ioc: per adapter object
 984 * @sas_address: device sas_address
 985 * @port: hba port entry
 986 *
 987 * Return nothing.
 988 */
 989void
 990mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
 991        u64 sas_address, struct hba_port *port)
 992{
 993        struct _sas_device *sas_device;
 994        unsigned long flags;
 995
 996        if (ioc->shost_recovery)
 997                return;
 998
 999        spin_lock_irqsave(&ioc->sas_device_lock, flags);
1000        sas_device = __mpt3sas_get_sdev_by_addr(ioc, sas_address, port);
1001        if (sas_device) {
1002                list_del_init(&sas_device->list);
1003                sas_device_put(sas_device);
1004        }
1005        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1006        if (sas_device) {
1007                _scsih_remove_device(ioc, sas_device);
1008                sas_device_put(sas_device);
1009        }
1010}
1011
1012/**
1013 * _scsih_sas_device_add - insert sas_device to the list.
1014 * @ioc: per adapter object
1015 * @sas_device: the sas_device object
1016 * Context: This function will acquire ioc->sas_device_lock.
1017 *
1018 * Adding new object to the ioc->sas_device_list.
1019 */
1020static void
1021_scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
1022        struct _sas_device *sas_device)
1023{
1024        unsigned long flags;
1025
1026        dewtprintk(ioc,
1027                   ioc_info(ioc, "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
1028                            __func__, sas_device->handle,
1029                            (u64)sas_device->sas_address));
1030
1031        dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
1032            NULL, NULL));
1033
1034        spin_lock_irqsave(&ioc->sas_device_lock, flags);
1035        sas_device_get(sas_device);
1036        list_add_tail(&sas_device->list, &ioc->sas_device_list);
1037        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1038
1039        if (ioc->hide_drives) {
1040                clear_bit(sas_device->handle, ioc->pend_os_device_add);
1041                return;
1042        }
1043
1044        if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
1045             sas_device->sas_address_parent, sas_device->port)) {
1046                _scsih_sas_device_remove(ioc, sas_device);
1047        } else if (!sas_device->starget) {
1048                /*
1049                 * When asyn scanning is enabled, its not possible to remove
1050                 * devices while scanning is turned on due to an oops in
1051                 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
1052                 */
1053                if (!ioc->is_driver_loading) {
1054                        mpt3sas_transport_port_remove(ioc,
1055                            sas_device->sas_address,
1056                            sas_device->sas_address_parent,
1057                            sas_device->port);
1058                        _scsih_sas_device_remove(ioc, sas_device);
1059                }
1060        } else
1061                clear_bit(sas_device->handle, ioc->pend_os_device_add);
1062}
1063
1064/**
1065 * _scsih_sas_device_init_add - insert sas_device to the list.
1066 * @ioc: per adapter object
1067 * @sas_device: the sas_device object
1068 * Context: This function will acquire ioc->sas_device_lock.
1069 *
1070 * Adding new object at driver load time to the ioc->sas_device_init_list.
1071 */
1072static void
1073_scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
1074        struct _sas_device *sas_device)
1075{
1076        unsigned long flags;
1077
1078        dewtprintk(ioc,
1079                   ioc_info(ioc, "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
1080                            __func__, sas_device->handle,
1081                            (u64)sas_device->sas_address));
1082
1083        dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
1084            NULL, NULL));
1085
1086        spin_lock_irqsave(&ioc->sas_device_lock, flags);
1087        sas_device_get(sas_device);
1088        list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
1089        _scsih_determine_boot_device(ioc, sas_device, 0);
1090        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1091}
1092
1093
1094static struct _pcie_device *
1095__mpt3sas_get_pdev_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
1096{
1097        struct _pcie_device *pcie_device;
1098
1099        assert_spin_locked(&ioc->pcie_device_lock);
1100
1101        list_for_each_entry(pcie_device, &ioc->pcie_device_list, list)
1102                if (pcie_device->wwid == wwid)
1103                        goto found_device;
1104
1105        list_for_each_entry(pcie_device, &ioc->pcie_device_init_list, list)
1106                if (pcie_device->wwid == wwid)
1107                        goto found_device;
1108
1109        return NULL;
1110
1111found_device:
1112        pcie_device_get(pcie_device);
1113        return pcie_device;
1114}
1115
1116
1117/**
1118 * mpt3sas_get_pdev_by_wwid - pcie device search
1119 * @ioc: per adapter object
1120 * @wwid: wwid
1121 *
1122 * Context: This function will acquire ioc->pcie_device_lock and will release
1123 * before returning the pcie_device object.
1124 *
1125 * This searches for pcie_device based on wwid, then return pcie_device object.
1126 */
1127static struct _pcie_device *
1128mpt3sas_get_pdev_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
1129{
1130        struct _pcie_device *pcie_device;
1131        unsigned long flags;
1132
1133        spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1134        pcie_device = __mpt3sas_get_pdev_by_wwid(ioc, wwid);
1135        spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1136
1137        return pcie_device;
1138}
1139
1140
1141static struct _pcie_device *
1142__mpt3sas_get_pdev_by_idchannel(struct MPT3SAS_ADAPTER *ioc, int id,
1143        int channel)
1144{
1145        struct _pcie_device *pcie_device;
1146
1147        assert_spin_locked(&ioc->pcie_device_lock);
1148
1149        list_for_each_entry(pcie_device, &ioc->pcie_device_list, list)
1150                if (pcie_device->id == id && pcie_device->channel == channel)
1151                        goto found_device;
1152
1153        list_for_each_entry(pcie_device, &ioc->pcie_device_init_list, list)
1154                if (pcie_device->id == id && pcie_device->channel == channel)
1155                        goto found_device;
1156
1157        return NULL;
1158
1159found_device:
1160        pcie_device_get(pcie_device);
1161        return pcie_device;
1162}
1163
1164static struct _pcie_device *
1165__mpt3sas_get_pdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1166{
1167        struct _pcie_device *pcie_device;
1168
1169        assert_spin_locked(&ioc->pcie_device_lock);
1170
1171        list_for_each_entry(pcie_device, &ioc->pcie_device_list, list)
1172                if (pcie_device->handle == handle)
1173                        goto found_device;
1174
1175        list_for_each_entry(pcie_device, &ioc->pcie_device_init_list, list)
1176                if (pcie_device->handle == handle)
1177                        goto found_device;
1178
1179        return NULL;
1180
1181found_device:
1182        pcie_device_get(pcie_device);
1183        return pcie_device;
1184}
1185
1186
1187/**
1188 * mpt3sas_get_pdev_by_handle - pcie device search
1189 * @ioc: per adapter object
1190 * @handle: Firmware device handle
1191 *
1192 * Context: This function will acquire ioc->pcie_device_lock and will release
1193 * before returning the pcie_device object.
1194 *
1195 * This searches for pcie_device based on handle, then return pcie_device
1196 * object.
1197 */
1198struct _pcie_device *
1199mpt3sas_get_pdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1200{
1201        struct _pcie_device *pcie_device;
1202        unsigned long flags;
1203
1204        spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1205        pcie_device = __mpt3sas_get_pdev_by_handle(ioc, handle);
1206        spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1207
1208        return pcie_device;
1209}
1210
1211/**
1212 * _scsih_set_nvme_max_shutdown_latency - Update max_shutdown_latency.
1213 * @ioc: per adapter object
1214 * Context: This function will acquire ioc->pcie_device_lock
1215 *
1216 * Update ioc->max_shutdown_latency to that NVMe drives RTD3 Entry Latency
1217 * which has reported maximum among all available NVMe drives.
1218 * Minimum max_shutdown_latency will be six seconds.
1219 */
1220static void
1221_scsih_set_nvme_max_shutdown_latency(struct MPT3SAS_ADAPTER *ioc)
1222{
1223        struct _pcie_device *pcie_device;
1224        unsigned long flags;
1225        u16 shutdown_latency = IO_UNIT_CONTROL_SHUTDOWN_TIMEOUT;
1226
1227        spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1228        list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
1229                if (pcie_device->shutdown_latency) {
1230                        if (shutdown_latency < pcie_device->shutdown_latency)
1231                                shutdown_latency =
1232                                        pcie_device->shutdown_latency;
1233                }
1234        }
1235        ioc->max_shutdown_latency = shutdown_latency;
1236        spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1237}
1238
1239/**
1240 * _scsih_pcie_device_remove - remove pcie_device from list.
1241 * @ioc: per adapter object
1242 * @pcie_device: the pcie_device object
1243 * Context: This function will acquire ioc->pcie_device_lock.
1244 *
1245 * If pcie_device is on the list, remove it and decrement its reference count.
1246 */
1247static void
1248_scsih_pcie_device_remove(struct MPT3SAS_ADAPTER *ioc,
1249        struct _pcie_device *pcie_device)
1250{
1251        unsigned long flags;
1252        int was_on_pcie_device_list = 0;
1253        u8 update_latency = 0;
1254
1255        if (!pcie_device)
1256                return;
1257        ioc_info(ioc, "removing handle(0x%04x), wwid(0x%016llx)\n",
1258                 pcie_device->handle, (u64)pcie_device->wwid);
1259        if (pcie_device->enclosure_handle != 0)
1260                ioc_info(ioc, "removing enclosure logical id(0x%016llx), slot(%d)\n",
1261                         (u64)pcie_device->enclosure_logical_id,
1262                         pcie_device->slot);
1263        if (pcie_device->connector_name[0] != '\0')
1264                ioc_info(ioc, "removing enclosure level(0x%04x), connector name( %s)\n",
1265                         pcie_device->enclosure_level,
1266                         pcie_device->connector_name);
1267
1268        spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1269        if (!list_empty(&pcie_device->list)) {
1270                list_del_init(&pcie_device->list);
1271                was_on_pcie_device_list = 1;
1272        }
1273        if (pcie_device->shutdown_latency == ioc->max_shutdown_latency)
1274                update_latency = 1;
1275        spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1276        if (was_on_pcie_device_list) {
1277                kfree(pcie_device->serial_number);
1278                pcie_device_put(pcie_device);
1279        }
1280
1281        /*
1282         * This device's RTD3 Entry Latency matches IOC's
1283         * max_shutdown_latency. Recalculate IOC's max_shutdown_latency
1284         * from the available drives as current drive is getting removed.
1285         */
1286        if (update_latency)
1287                _scsih_set_nvme_max_shutdown_latency(ioc);
1288}
1289
1290
1291/**
1292 * _scsih_pcie_device_remove_by_handle - removing pcie device object by handle
1293 * @ioc: per adapter object
1294 * @handle: device handle
1295 */
1296static void
1297_scsih_pcie_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1298{
1299        struct _pcie_device *pcie_device;
1300        unsigned long flags;
1301        int was_on_pcie_device_list = 0;
1302        u8 update_latency = 0;
1303
1304        if (ioc->shost_recovery)
1305                return;
1306
1307        spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1308        pcie_device = __mpt3sas_get_pdev_by_handle(ioc, handle);
1309        if (pcie_device) {
1310                if (!list_empty(&pcie_device->list)) {
1311                        list_del_init(&pcie_device->list);
1312                        was_on_pcie_device_list = 1;
1313                        pcie_device_put(pcie_device);
1314                }
1315                if (pcie_device->shutdown_latency == ioc->max_shutdown_latency)
1316                        update_latency = 1;
1317        }
1318        spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1319        if (was_on_pcie_device_list) {
1320                _scsih_pcie_device_remove_from_sml(ioc, pcie_device);
1321                pcie_device_put(pcie_device);
1322        }
1323
1324        /*
1325         * This device's RTD3 Entry Latency matches IOC's
1326         * max_shutdown_latency. Recalculate IOC's max_shutdown_latency
1327         * from the available drives as current drive is getting removed.
1328         */
1329        if (update_latency)
1330                _scsih_set_nvme_max_shutdown_latency(ioc);
1331}
1332
1333/**
1334 * _scsih_pcie_device_add - add pcie_device object
1335 * @ioc: per adapter object
1336 * @pcie_device: pcie_device object
1337 *
1338 * This is added to the pcie_device_list link list.
1339 */
1340static void
1341_scsih_pcie_device_add(struct MPT3SAS_ADAPTER *ioc,
1342        struct _pcie_device *pcie_device)
1343{
1344        unsigned long flags;
1345
1346        dewtprintk(ioc,
1347                   ioc_info(ioc, "%s: handle (0x%04x), wwid(0x%016llx)\n",
1348                            __func__,
1349                            pcie_device->handle, (u64)pcie_device->wwid));
1350        if (pcie_device->enclosure_handle != 0)
1351                dewtprintk(ioc,
1352                           ioc_info(ioc, "%s: enclosure logical id(0x%016llx), slot( %d)\n",
1353                                    __func__,
1354                                    (u64)pcie_device->enclosure_logical_id,
1355                                    pcie_device->slot));
1356        if (pcie_device->connector_name[0] != '\0')
1357                dewtprintk(ioc,
1358                           ioc_info(ioc, "%s: enclosure level(0x%04x), connector name( %s)\n",
1359                                    __func__, pcie_device->enclosure_level,
1360                                    pcie_device->connector_name));
1361
1362        spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1363        pcie_device_get(pcie_device);
1364        list_add_tail(&pcie_device->list, &ioc->pcie_device_list);
1365        spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1366
1367        if (pcie_device->access_status ==
1368            MPI26_PCIEDEV0_ASTATUS_DEVICE_BLOCKED) {
1369                clear_bit(pcie_device->handle, ioc->pend_os_device_add);
1370                return;
1371        }
1372        if (scsi_add_device(ioc->shost, PCIE_CHANNEL, pcie_device->id, 0)) {
1373                _scsih_pcie_device_remove(ioc, pcie_device);
1374        } else if (!pcie_device->starget) {
1375                if (!ioc->is_driver_loading) {
1376/*TODO-- Need to find out whether this condition will occur or not*/
1377                        clear_bit(pcie_device->handle, ioc->pend_os_device_add);
1378                }
1379        } else
1380                clear_bit(pcie_device->handle, ioc->pend_os_device_add);
1381}
1382
1383/*
1384 * _scsih_pcie_device_init_add - insert pcie_device to the init list.
1385 * @ioc: per adapter object
1386 * @pcie_device: the pcie_device object
1387 * Context: This function will acquire ioc->pcie_device_lock.
1388 *
1389 * Adding new object at driver load time to the ioc->pcie_device_init_list.
1390 */
1391static void
1392_scsih_pcie_device_init_add(struct MPT3SAS_ADAPTER *ioc,
1393                                struct _pcie_device *pcie_device)
1394{
1395        unsigned long flags;
1396
1397        dewtprintk(ioc,
1398                   ioc_info(ioc, "%s: handle (0x%04x), wwid(0x%016llx)\n",
1399                            __func__,
1400                            pcie_device->handle, (u64)pcie_device->wwid));
1401        if (pcie_device->enclosure_handle != 0)
1402                dewtprintk(ioc,
1403                           ioc_info(ioc, "%s: enclosure logical id(0x%016llx), slot( %d)\n",
1404                                    __func__,
1405                                    (u64)pcie_device->enclosure_logical_id,
1406                                    pcie_device->slot));
1407        if (pcie_device->connector_name[0] != '\0')
1408                dewtprintk(ioc,
1409                           ioc_info(ioc, "%s: enclosure level(0x%04x), connector name( %s)\n",
1410                                    __func__, pcie_device->enclosure_level,
1411                                    pcie_device->connector_name));
1412
1413        spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1414        pcie_device_get(pcie_device);
1415        list_add_tail(&pcie_device->list, &ioc->pcie_device_init_list);
1416        if (pcie_device->access_status !=
1417            MPI26_PCIEDEV0_ASTATUS_DEVICE_BLOCKED)
1418                _scsih_determine_boot_device(ioc, pcie_device, PCIE_CHANNEL);
1419        spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1420}
1421/**
1422 * _scsih_raid_device_find_by_id - raid device search
1423 * @ioc: per adapter object
1424 * @id: sas device target id
1425 * @channel: sas device channel
1426 * Context: Calling function should acquire ioc->raid_device_lock
1427 *
1428 * This searches for raid_device based on target id, then return raid_device
1429 * object.
1430 */
1431static struct _raid_device *
1432_scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
1433{
1434        struct _raid_device *raid_device, *r;
1435
1436        r = NULL;
1437        list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1438                if (raid_device->id == id && raid_device->channel == channel) {
1439                        r = raid_device;
1440                        goto out;
1441                }
1442        }
1443
1444 out:
1445        return r;
1446}
1447
1448/**
1449 * mpt3sas_raid_device_find_by_handle - raid device search
1450 * @ioc: per adapter object
1451 * @handle: sas device handle (assigned by firmware)
1452 * Context: Calling function should acquire ioc->raid_device_lock
1453 *
1454 * This searches for raid_device based on handle, then return raid_device
1455 * object.
1456 */
1457struct _raid_device *
1458mpt3sas_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1459{
1460        struct _raid_device *raid_device, *r;
1461
1462        r = NULL;
1463        list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1464                if (raid_device->handle != handle)
1465                        continue;
1466                r = raid_device;
1467                goto out;
1468        }
1469
1470 out:
1471        return r;
1472}
1473
1474/**
1475 * _scsih_raid_device_find_by_wwid - raid device search
1476 * @ioc: per adapter object
1477 * @wwid: ?
1478 * Context: Calling function should acquire ioc->raid_device_lock
1479 *
1480 * This searches for raid_device based on wwid, then return raid_device
1481 * object.
1482 */
1483static struct _raid_device *
1484_scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
1485{
1486        struct _raid_device *raid_device, *r;
1487
1488        r = NULL;
1489        list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1490                if (raid_device->wwid != wwid)
1491                        continue;
1492                r = raid_device;
1493                goto out;
1494        }
1495
1496 out:
1497        return r;
1498}
1499
1500/**
1501 * _scsih_raid_device_add - add raid_device object
1502 * @ioc: per adapter object
1503 * @raid_device: raid_device object
1504 *
1505 * This is added to the raid_device_list link list.
1506 */
1507static void
1508_scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
1509        struct _raid_device *raid_device)
1510{
1511        unsigned long flags;
1512
1513        dewtprintk(ioc,
1514                   ioc_info(ioc, "%s: handle(0x%04x), wwid(0x%016llx)\n",
1515                            __func__,
1516                            raid_device->handle, (u64)raid_device->wwid));
1517
1518        spin_lock_irqsave(&ioc->raid_device_lock, flags);
1519        list_add_tail(&raid_device->list, &ioc->raid_device_list);
1520        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1521}
1522
1523/**
1524 * _scsih_raid_device_remove - delete raid_device object
1525 * @ioc: per adapter object
1526 * @raid_device: raid_device object
1527 *
1528 */
1529static void
1530_scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
1531        struct _raid_device *raid_device)
1532{
1533        unsigned long flags;
1534
1535        spin_lock_irqsave(&ioc->raid_device_lock, flags);
1536        list_del(&raid_device->list);
1537        kfree(raid_device);
1538        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1539}
1540
1541/**
1542 * mpt3sas_scsih_expander_find_by_handle - expander device search
1543 * @ioc: per adapter object
1544 * @handle: expander handle (assigned by firmware)
1545 * Context: Calling function should acquire ioc->sas_device_lock
1546 *
1547 * This searches for expander device based on handle, then returns the
1548 * sas_node object.
1549 */
1550struct _sas_node *
1551mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1552{
1553        struct _sas_node *sas_expander, *r;
1554
1555        r = NULL;
1556        list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
1557                if (sas_expander->handle != handle)
1558                        continue;
1559                r = sas_expander;
1560                goto out;
1561        }
1562 out:
1563        return r;
1564}
1565
1566/**
1567 * mpt3sas_scsih_enclosure_find_by_handle - exclosure device search
1568 * @ioc: per adapter object
1569 * @handle: enclosure handle (assigned by firmware)
1570 * Context: Calling function should acquire ioc->sas_device_lock
1571 *
1572 * This searches for enclosure device based on handle, then returns the
1573 * enclosure object.
1574 */
1575static struct _enclosure_node *
1576mpt3sas_scsih_enclosure_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1577{
1578        struct _enclosure_node *enclosure_dev, *r;
1579
1580        r = NULL;
1581        list_for_each_entry(enclosure_dev, &ioc->enclosure_list, list) {
1582                if (le16_to_cpu(enclosure_dev->pg0.EnclosureHandle) != handle)
1583                        continue;
1584                r = enclosure_dev;
1585                goto out;
1586        }
1587out:
1588        return r;
1589}
1590/**
1591 * mpt3sas_scsih_expander_find_by_sas_address - expander device search
1592 * @ioc: per adapter object
1593 * @sas_address: sas address
1594 * @port: hba port entry
1595 * Context: Calling function should acquire ioc->sas_node_lock.
1596 *
1597 * This searches for expander device based on sas_address & port number,
1598 * then returns the sas_node object.
1599 */
1600struct _sas_node *
1601mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
1602        u64 sas_address, struct hba_port *port)
1603{
1604        struct _sas_node *sas_expander, *r = NULL;
1605
1606        if (!port)
1607                return r;
1608
1609        list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
1610                if (sas_expander->sas_address != sas_address)
1611                        continue;
1612                if (sas_expander->port != port)
1613                        continue;
1614                r = sas_expander;
1615                goto out;
1616        }
1617 out:
1618        return r;
1619}
1620
1621/**
1622 * _scsih_expander_node_add - insert expander device to the list.
1623 * @ioc: per adapter object
1624 * @sas_expander: the sas_device object
1625 * Context: This function will acquire ioc->sas_node_lock.
1626 *
1627 * Adding new object to the ioc->sas_expander_list.
1628 */
1629static void
1630_scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
1631        struct _sas_node *sas_expander)
1632{
1633        unsigned long flags;
1634
1635        spin_lock_irqsave(&ioc->sas_node_lock, flags);
1636        list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
1637        spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1638}
1639
1640/**
1641 * _scsih_is_end_device - determines if device is an end device
1642 * @device_info: bitfield providing information about the device.
1643 * Context: none
1644 *
1645 * Return: 1 if end device.
1646 */
1647static int
1648_scsih_is_end_device(u32 device_info)
1649{
1650        if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
1651                ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
1652                (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
1653                (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
1654                return 1;
1655        else
1656                return 0;
1657}
1658
1659/**
1660 * _scsih_is_nvme_pciescsi_device - determines if
1661 *                      device is an pcie nvme/scsi device
1662 * @device_info: bitfield providing information about the device.
1663 * Context: none
1664 *
1665 * Returns 1 if device is pcie device type nvme/scsi.
1666 */
1667static int
1668_scsih_is_nvme_pciescsi_device(u32 device_info)
1669{
1670        if (((device_info & MPI26_PCIE_DEVINFO_MASK_DEVICE_TYPE)
1671            == MPI26_PCIE_DEVINFO_NVME) ||
1672            ((device_info & MPI26_PCIE_DEVINFO_MASK_DEVICE_TYPE)
1673            == MPI26_PCIE_DEVINFO_SCSI))
1674                return 1;
1675        else
1676                return 0;
1677}
1678
1679/**
1680 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
1681 * @ioc: per adapter object
1682 * @id: target id
1683 * @channel: channel
1684 * Context: This function will acquire ioc->scsi_lookup_lock.
1685 *
1686 * This will search for a matching channel:id in the scsi_lookup array,
1687 * returning 1 if found.
1688 */
1689static u8
1690_scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
1691        int channel)
1692{
1693        int smid;
1694        struct scsi_cmnd *scmd;
1695
1696        for (smid = 1;
1697             smid <= ioc->shost->can_queue; smid++) {
1698                scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
1699                if (!scmd)
1700                        continue;
1701                if (scmd->device->id == id &&
1702                    scmd->device->channel == channel)
1703                        return 1;
1704        }
1705        return 0;
1706}
1707
1708/**
1709 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
1710 * @ioc: per adapter object
1711 * @id: target id
1712 * @lun: lun number
1713 * @channel: channel
1714 * Context: This function will acquire ioc->scsi_lookup_lock.
1715 *
1716 * This will search for a matching channel:id:lun in the scsi_lookup array,
1717 * returning 1 if found.
1718 */
1719static u8
1720_scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
1721        unsigned int lun, int channel)
1722{
1723        int smid;
1724        struct scsi_cmnd *scmd;
1725
1726        for (smid = 1; smid <= ioc->shost->can_queue; smid++) {
1727
1728                scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
1729                if (!scmd)
1730                        continue;
1731                if (scmd->device->id == id &&
1732                    scmd->device->channel == channel &&
1733                    scmd->device->lun == lun)
1734                        return 1;
1735        }
1736        return 0;
1737}
1738
1739/**
1740 * mpt3sas_scsih_scsi_lookup_get - returns scmd entry
1741 * @ioc: per adapter object
1742 * @smid: system request message index
1743 *
1744 * Return: the smid stored scmd pointer.
1745 * Then will dereference the stored scmd pointer.
1746 */
1747struct scsi_cmnd *
1748mpt3sas_scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1749{
1750        struct scsi_cmnd *scmd = NULL;
1751        struct scsiio_tracker *st;
1752        Mpi25SCSIIORequest_t *mpi_request;
1753        u16 tag = smid - 1;
1754
1755        if (smid > 0  &&
1756            smid <= ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT) {
1757                u32 unique_tag =
1758                    ioc->io_queue_num[tag] << BLK_MQ_UNIQUE_TAG_BITS | tag;
1759
1760                mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1761
1762                /*
1763                 * If SCSI IO request is outstanding at driver level then
1764                 * DevHandle filed must be non-zero. If DevHandle is zero
1765                 * then it means that this smid is free at driver level,
1766                 * so return NULL.
1767                 */
1768                if (!mpi_request->DevHandle)
1769                        return scmd;
1770
1771                scmd = scsi_host_find_tag(ioc->shost, unique_tag);
1772                if (scmd) {
1773                        st = scsi_cmd_priv(scmd);
1774                        if (st->cb_idx == 0xFF || st->smid == 0)
1775                                scmd = NULL;
1776                }
1777        }
1778        return scmd;
1779}
1780
1781/**
1782 * scsih_change_queue_depth - setting device queue depth
1783 * @sdev: scsi device struct
1784 * @qdepth: requested queue depth
1785 *
1786 * Return: queue depth.
1787 */
1788static int
1789scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1790{
1791        struct Scsi_Host *shost = sdev->host;
1792        int max_depth;
1793        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1794        struct MPT3SAS_DEVICE *sas_device_priv_data;
1795        struct MPT3SAS_TARGET *sas_target_priv_data;
1796        struct _sas_device *sas_device;
1797        unsigned long flags;
1798
1799        max_depth = shost->can_queue;
1800
1801        /*
1802         * limit max device queue for SATA to 32 if enable_sdev_max_qd
1803         * is disabled.
1804         */
1805        if (ioc->enable_sdev_max_qd)
1806                goto not_sata;
1807
1808        sas_device_priv_data = sdev->hostdata;
1809        if (!sas_device_priv_data)
1810                goto not_sata;
1811        sas_target_priv_data = sas_device_priv_data->sas_target;
1812        if (!sas_target_priv_data)
1813                goto not_sata;
1814        if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1815                goto not_sata;
1816
1817        spin_lock_irqsave(&ioc->sas_device_lock, flags);
1818        sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1819        if (sas_device) {
1820                if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1821                        max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
1822
1823                sas_device_put(sas_device);
1824        }
1825        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1826
1827 not_sata:
1828
1829        if (!sdev->tagged_supported)
1830                max_depth = 1;
1831        if (qdepth > max_depth)
1832                qdepth = max_depth;
1833        scsi_change_queue_depth(sdev, qdepth);
1834        sdev_printk(KERN_INFO, sdev,
1835            "qdepth(%d), tagged(%d), scsi_level(%d), cmd_que(%d)\n",
1836            sdev->queue_depth, sdev->tagged_supported,
1837            sdev->scsi_level, ((sdev->inquiry[7] & 2) >> 1));
1838        return sdev->queue_depth;
1839}
1840
1841/**
1842 * mpt3sas_scsih_change_queue_depth - setting device queue depth
1843 * @sdev: scsi device struct
1844 * @qdepth: requested queue depth
1845 *
1846 * Returns nothing.
1847 */
1848void
1849mpt3sas_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1850{
1851        struct Scsi_Host *shost = sdev->host;
1852        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1853
1854        if (ioc->enable_sdev_max_qd)
1855                qdepth = shost->can_queue;
1856
1857        scsih_change_queue_depth(sdev, qdepth);
1858}
1859
1860/**
1861 * scsih_target_alloc - target add routine
1862 * @starget: scsi target struct
1863 *
1864 * Return: 0 if ok. Any other return is assumed to be an error and
1865 * the device is ignored.
1866 */
1867static int
1868scsih_target_alloc(struct scsi_target *starget)
1869{
1870        struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1871        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1872        struct MPT3SAS_TARGET *sas_target_priv_data;
1873        struct _sas_device *sas_device;
1874        struct _raid_device *raid_device;
1875        struct _pcie_device *pcie_device;
1876        unsigned long flags;
1877        struct sas_rphy *rphy;
1878
1879        sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1880                                       GFP_KERNEL);
1881        if (!sas_target_priv_data)
1882                return -ENOMEM;
1883
1884        starget->hostdata = sas_target_priv_data;
1885        sas_target_priv_data->starget = starget;
1886        sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
1887
1888        /* RAID volumes */
1889        if (starget->channel == RAID_CHANNEL) {
1890                spin_lock_irqsave(&ioc->raid_device_lock, flags);
1891                raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1892                    starget->channel);
1893                if (raid_device) {
1894                        sas_target_priv_data->handle = raid_device->handle;
1895                        sas_target_priv_data->sas_address = raid_device->wwid;
1896                        sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1897                        if (ioc->is_warpdrive)
1898                                sas_target_priv_data->raid_device = raid_device;
1899                        raid_device->starget = starget;
1900                }
1901                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1902                return 0;
1903        }
1904
1905        /* PCIe devices */
1906        if (starget->channel == PCIE_CHANNEL) {
1907                spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1908                pcie_device = __mpt3sas_get_pdev_by_idchannel(ioc, starget->id,
1909                        starget->channel);
1910                if (pcie_device) {
1911                        sas_target_priv_data->handle = pcie_device->handle;
1912                        sas_target_priv_data->sas_address = pcie_device->wwid;
1913                        sas_target_priv_data->port = NULL;
1914                        sas_target_priv_data->pcie_dev = pcie_device;
1915                        pcie_device->starget = starget;
1916                        pcie_device->id = starget->id;
1917                        pcie_device->channel = starget->channel;
1918                        sas_target_priv_data->flags |=
1919                                MPT_TARGET_FLAGS_PCIE_DEVICE;
1920                        if (pcie_device->fast_path)
1921                                sas_target_priv_data->flags |=
1922                                        MPT_TARGET_FASTPATH_IO;
1923                }
1924                spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1925                return 0;
1926        }
1927
1928        /* sas/sata devices */
1929        spin_lock_irqsave(&ioc->sas_device_lock, flags);
1930        rphy = dev_to_rphy(starget->dev.parent);
1931        sas_device = __mpt3sas_get_sdev_by_rphy(ioc, rphy);
1932
1933        if (sas_device) {
1934                sas_target_priv_data->handle = sas_device->handle;
1935                sas_target_priv_data->sas_address = sas_device->sas_address;
1936                sas_target_priv_data->port = sas_device->port;
1937                sas_target_priv_data->sas_dev = sas_device;
1938                sas_device->starget = starget;
1939                sas_device->id = starget->id;
1940                sas_device->channel = starget->channel;
1941                if (test_bit(sas_device->handle, ioc->pd_handles))
1942                        sas_target_priv_data->flags |=
1943                            MPT_TARGET_FLAGS_RAID_COMPONENT;
1944                if (sas_device->fast_path)
1945                        sas_target_priv_data->flags |=
1946                                        MPT_TARGET_FASTPATH_IO;
1947        }
1948        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1949
1950        return 0;
1951}
1952
1953/**
1954 * scsih_target_destroy - target destroy routine
1955 * @starget: scsi target struct
1956 */
1957static void
1958scsih_target_destroy(struct scsi_target *starget)
1959{
1960        struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1961        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1962        struct MPT3SAS_TARGET *sas_target_priv_data;
1963        struct _sas_device *sas_device;
1964        struct _raid_device *raid_device;
1965        struct _pcie_device *pcie_device;
1966        unsigned long flags;
1967
1968        sas_target_priv_data = starget->hostdata;
1969        if (!sas_target_priv_data)
1970                return;
1971
1972        if (starget->channel == RAID_CHANNEL) {
1973                spin_lock_irqsave(&ioc->raid_device_lock, flags);
1974                raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1975                    starget->channel);
1976                if (raid_device) {
1977                        raid_device->starget = NULL;
1978                        raid_device->sdev = NULL;
1979                }
1980                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1981                goto out;
1982        }
1983
1984        if (starget->channel == PCIE_CHANNEL) {
1985                spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1986                pcie_device = __mpt3sas_get_pdev_from_target(ioc,
1987                                                        sas_target_priv_data);
1988                if (pcie_device && (pcie_device->starget == starget) &&
1989                        (pcie_device->id == starget->id) &&
1990                        (pcie_device->channel == starget->channel))
1991                        pcie_device->starget = NULL;
1992
1993                if (pcie_device) {
1994                        /*
1995                         * Corresponding get() is in _scsih_target_alloc()
1996                         */
1997                        sas_target_priv_data->pcie_dev = NULL;
1998                        pcie_device_put(pcie_device);
1999                        pcie_device_put(pcie_device);
2000                }
2001                spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
2002                goto out;
2003        }
2004
2005        spin_lock_irqsave(&ioc->sas_device_lock, flags);
2006        sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
2007        if (sas_device && (sas_device->starget == starget) &&
2008            (sas_device->id == starget->id) &&
2009            (sas_device->channel == starget->channel))
2010                sas_device->starget = NULL;
2011
2012        if (sas_device) {
2013                /*
2014                 * Corresponding get() is in _scsih_target_alloc()
2015                 */
2016                sas_target_priv_data->sas_dev = NULL;
2017                sas_device_put(sas_device);
2018
2019                sas_device_put(sas_device);
2020        }
2021        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2022
2023 out:
2024        kfree(sas_target_priv_data);
2025        starget->hostdata = NULL;
2026}
2027
2028/**
2029 * scsih_slave_alloc - device add routine
2030 * @sdev: scsi device struct
2031 *
2032 * Return: 0 if ok. Any other return is assumed to be an error and
2033 * the device is ignored.
2034 */
2035static int
2036scsih_slave_alloc(struct scsi_device *sdev)
2037{
2038        struct Scsi_Host *shost;
2039        struct MPT3SAS_ADAPTER *ioc;
2040        struct MPT3SAS_TARGET *sas_target_priv_data;
2041        struct MPT3SAS_DEVICE *sas_device_priv_data;
2042        struct scsi_target *starget;
2043        struct _raid_device *raid_device;
2044        struct _sas_device *sas_device;
2045        struct _pcie_device *pcie_device;
2046        unsigned long flags;
2047
2048        sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
2049                                       GFP_KERNEL);
2050        if (!sas_device_priv_data)
2051                return -ENOMEM;
2052
2053        sas_device_priv_data->lun = sdev->lun;
2054        sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
2055
2056        starget = scsi_target(sdev);
2057        sas_target_priv_data = starget->hostdata;
2058        sas_target_priv_data->num_luns++;
2059        sas_device_priv_data->sas_target = sas_target_priv_data;
2060        sdev->hostdata = sas_device_priv_data;
2061        if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
2062                sdev->no_uld_attach = 1;
2063
2064        shost = dev_to_shost(&starget->dev);
2065        ioc = shost_priv(shost);
2066        if (starget->channel == RAID_CHANNEL) {
2067                spin_lock_irqsave(&ioc->raid_device_lock, flags);
2068                raid_device = _scsih_raid_device_find_by_id(ioc,
2069                    starget->id, starget->channel);
2070                if (raid_device)
2071                        raid_device->sdev = sdev; /* raid is single lun */
2072                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
2073        }
2074        if (starget->channel == PCIE_CHANNEL) {
2075                spin_lock_irqsave(&ioc->pcie_device_lock, flags);
2076                pcie_device = __mpt3sas_get_pdev_by_wwid(ioc,
2077                                sas_target_priv_data->sas_address);
2078                if (pcie_device && (pcie_device->starget == NULL)) {
2079                        sdev_printk(KERN_INFO, sdev,
2080                            "%s : pcie_device->starget set to starget @ %d\n",
2081                            __func__, __LINE__);
2082                        pcie_device->starget = starget;
2083                }
2084
2085                if (pcie_device)
2086                        pcie_device_put(pcie_device);
2087                spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
2088
2089        } else  if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
2090                spin_lock_irqsave(&ioc->sas_device_lock, flags);
2091                sas_device = __mpt3sas_get_sdev_by_addr(ioc,
2092                    sas_target_priv_data->sas_address,
2093                    sas_target_priv_data->port);
2094                if (sas_device && (sas_device->starget == NULL)) {
2095                        sdev_printk(KERN_INFO, sdev,
2096                        "%s : sas_device->starget set to starget @ %d\n",
2097                             __func__, __LINE__);
2098                        sas_device->starget = starget;
2099                }
2100
2101                if (sas_device)
2102                        sas_device_put(sas_device);
2103
2104                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2105        }
2106
2107        return 0;
2108}
2109
2110/**
2111 * scsih_slave_destroy - device destroy routine
2112 * @sdev: scsi device struct
2113 */
2114static void
2115scsih_slave_destroy(struct scsi_device *sdev)
2116{
2117        struct MPT3SAS_TARGET *sas_target_priv_data;
2118        struct scsi_target *starget;
2119        struct Scsi_Host *shost;
2120        struct MPT3SAS_ADAPTER *ioc;
2121        struct _sas_device *sas_device;
2122        struct _pcie_device *pcie_device;
2123        unsigned long flags;
2124
2125        if (!sdev->hostdata)
2126                return;
2127
2128        starget = scsi_target(sdev);
2129        sas_target_priv_data = starget->hostdata;
2130        sas_target_priv_data->num_luns--;
2131
2132        shost = dev_to_shost(&starget->dev);
2133        ioc = shost_priv(shost);
2134
2135        if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_PCIE_DEVICE) {
2136                spin_lock_irqsave(&ioc->pcie_device_lock, flags);
2137                pcie_device = __mpt3sas_get_pdev_from_target(ioc,
2138                                sas_target_priv_data);
2139                if (pcie_device && !sas_target_priv_data->num_luns)
2140                        pcie_device->starget = NULL;
2141
2142                if (pcie_device)
2143                        pcie_device_put(pcie_device);
2144
2145                spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
2146
2147        } else if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
2148                spin_lock_irqsave(&ioc->sas_device_lock, flags);
2149                sas_device = __mpt3sas_get_sdev_from_target(ioc,
2150                                sas_target_priv_data);
2151                if (sas_device && !sas_target_priv_data->num_luns)
2152                        sas_device->starget = NULL;
2153
2154                if (sas_device)
2155                        sas_device_put(sas_device);
2156                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2157        }
2158
2159        kfree(sdev->hostdata);
2160        sdev->hostdata = NULL;
2161}
2162
2163/**
2164 * _scsih_display_sata_capabilities - sata capabilities
2165 * @ioc: per adapter object
2166 * @handle: device handle
2167 * @sdev: scsi device struct
2168 */
2169static void
2170_scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
2171        u16 handle, struct scsi_device *sdev)
2172{
2173        Mpi2ConfigReply_t mpi_reply;
2174        Mpi2SasDevicePage0_t sas_device_pg0;
2175        u32 ioc_status;
2176        u16 flags;
2177        u32 device_info;
2178
2179        if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
2180            MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
2181                ioc_err(ioc, "failure at %s:%d/%s()!\n",
2182                        __FILE__, __LINE__, __func__);
2183                return;
2184        }
2185
2186        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
2187            MPI2_IOCSTATUS_MASK;
2188        if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2189                ioc_err(ioc, "failure at %s:%d/%s()!\n",
2190                        __FILE__, __LINE__, __func__);
2191                return;
2192        }
2193
2194        flags = le16_to_cpu(sas_device_pg0.Flags);
2195        device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
2196
2197        sdev_printk(KERN_INFO, sdev,
2198            "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
2199            "sw_preserve(%s)\n",
2200            (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
2201            (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
2202            (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
2203            "n",
2204            (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
2205            (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
2206            (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
2207}
2208
2209/*
2210 * raid transport support -
2211 * Enabled for SLES11 and newer, in older kernels the driver will panic when
2212 * unloading the driver followed by a load - I believe that the subroutine
2213 * raid_class_release() is not cleaning up properly.
2214 */
2215
2216/**
2217 * scsih_is_raid - return boolean indicating device is raid volume
2218 * @dev: the device struct object
2219 */
2220static int
2221scsih_is_raid(struct device *dev)
2222{
2223        struct scsi_device *sdev = to_scsi_device(dev);
2224        struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
2225
2226        if (ioc->is_warpdrive)
2227                return 0;
2228        return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
2229}
2230
2231static int
2232scsih_is_nvme(struct device *dev)
2233{
2234        struct scsi_device *sdev = to_scsi_device(dev);
2235
2236        return (sdev->channel == PCIE_CHANNEL) ? 1 : 0;
2237}
2238
2239/**
2240 * scsih_get_resync - get raid volume resync percent complete
2241 * @dev: the device struct object
2242 */
2243static void
2244scsih_get_resync(struct device *dev)
2245{
2246        struct scsi_device *sdev = to_scsi_device(dev);
2247        struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
2248        static struct _raid_device *raid_device;
2249        unsigned long flags;
2250        Mpi2RaidVolPage0_t vol_pg0;
2251        Mpi2ConfigReply_t mpi_reply;
2252        u32 volume_status_flags;
2253        u8 percent_complete;
2254        u16 handle;
2255
2256        percent_complete = 0;
2257        handle = 0;
2258        if (ioc->is_warpdrive)
2259                goto out;
2260
2261        spin_lock_irqsave(&ioc->raid_device_lock, flags);
2262        raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
2263            sdev->channel);
2264        if (raid_device) {
2265                handle = raid_device->handle;
2266                percent_complete = raid_device->percent_complete;
2267        }
2268        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
2269
2270        if (!handle)
2271                goto out;
2272
2273        if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
2274             MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
2275             sizeof(Mpi2RaidVolPage0_t))) {
2276                ioc_err(ioc, "failure at %s:%d/%s()!\n",
2277                        __FILE__, __LINE__, __func__);
2278                percent_complete = 0;
2279                goto out;
2280        }
2281
2282        volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
2283        if (!(volume_status_flags &
2284            MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
2285                percent_complete = 0;
2286
2287 out:
2288
2289        switch (ioc->hba_mpi_version_belonged) {
2290        case MPI2_VERSION:
2291                raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
2292                break;
2293        case MPI25_VERSION:
2294        case MPI26_VERSION:
2295                raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
2296                break;
2297        }
2298}
2299
2300/**
2301 * scsih_get_state - get raid volume level
2302 * @dev: the device struct object
2303 */
2304static void
2305scsih_get_state(struct device *dev)
2306{
2307        struct scsi_device *sdev = to_scsi_device(dev);
2308        struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
2309        static struct _raid_device *raid_device;
2310        unsigned long flags;
2311        Mpi2RaidVolPage0_t vol_pg0;
2312        Mpi2ConfigReply_t mpi_reply;
2313        u32 volstate;
2314        enum raid_state state = RAID_STATE_UNKNOWN;
2315        u16 handle = 0;
2316
2317        spin_lock_irqsave(&ioc->raid_device_lock, flags);
2318        raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
2319            sdev->channel);
2320        if (raid_device)
2321                handle = raid_device->handle;
2322        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
2323
2324        if (!raid_device)
2325                goto out;
2326
2327        if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
2328             MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
2329             sizeof(Mpi2RaidVolPage0_t))) {
2330                ioc_err(ioc, "failure at %s:%d/%s()!\n",
2331                        __FILE__, __LINE__, __func__);
2332                goto out;
2333        }
2334
2335        volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
2336        if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
2337                state = RAID_STATE_RESYNCING;
2338                goto out;
2339        }
2340
2341        switch (vol_pg0.VolumeState) {
2342        case MPI2_RAID_VOL_STATE_OPTIMAL:
2343        case MPI2_RAID_VOL_STATE_ONLINE:
2344                state = RAID_STATE_ACTIVE;
2345                break;
2346        case  MPI2_RAID_VOL_STATE_DEGRADED:
2347                state = RAID_STATE_DEGRADED;
2348                break;
2349        case MPI2_RAID_VOL_STATE_FAILED:
2350        case MPI2_RAID_VOL_STATE_MISSING:
2351                state = RAID_STATE_OFFLINE;
2352                break;
2353        }
2354 out:
2355        switch (ioc->hba_mpi_version_belonged) {
2356        case MPI2_VERSION:
2357                raid_set_state(mpt2sas_raid_template, dev, state);
2358                break;
2359        case MPI25_VERSION:
2360        case MPI26_VERSION:
2361                raid_set_state(mpt3sas_raid_template, dev, state);
2362                break;
2363        }
2364}
2365
2366/**
2367 * _scsih_set_level - set raid level
2368 * @ioc: ?
2369 * @sdev: scsi device struct
2370 * @volume_type: volume type
2371 */
2372static void
2373_scsih_set_level(struct MPT3SAS_ADAPTER *ioc,
2374        struct scsi_device *sdev, u8 volume_type)
2375{
2376        enum raid_level level = RAID_LEVEL_UNKNOWN;
2377
2378        switch (volume_type) {
2379        case MPI2_RAID_VOL_TYPE_RAID0:
2380                level = RAID_LEVEL_0;
2381                break;
2382        case MPI2_RAID_VOL_TYPE_RAID10:
2383                level = RAID_LEVEL_10;
2384                break;
2385        case MPI2_RAID_VOL_TYPE_RAID1E:
2386                level = RAID_LEVEL_1E;
2387                break;
2388        case MPI2_RAID_VOL_TYPE_RAID1:
2389                level = RAID_LEVEL_1;
2390                break;
2391        }
2392
2393        switch (ioc->hba_mpi_version_belonged) {
2394        case MPI2_VERSION:
2395                raid_set_level(mpt2sas_raid_template,
2396                        &sdev->sdev_gendev, level);
2397                break;
2398        case MPI25_VERSION:
2399        case MPI26_VERSION:
2400                raid_set_level(mpt3sas_raid_template,
2401                        &sdev->sdev_gendev, level);
2402                break;
2403        }
2404}
2405
2406
2407/**
2408 * _scsih_get_volume_capabilities - volume capabilities
2409 * @ioc: per adapter object
2410 * @raid_device: the raid_device object
2411 *
2412 * Return: 0 for success, else 1
2413 */
2414static int
2415_scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
2416        struct _raid_device *raid_device)
2417{
2418        Mpi2RaidVolPage0_t *vol_pg0;
2419        Mpi2RaidPhysDiskPage0_t pd_pg0;
2420        Mpi2SasDevicePage0_t sas_device_pg0;
2421        Mpi2ConfigReply_t mpi_reply;
2422        u16 sz;
2423        u8 num_pds;
2424
2425        if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
2426            &num_pds)) || !num_pds) {
2427                dfailprintk(ioc,
2428                            ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2429                                     __FILE__, __LINE__, __func__));
2430                return 1;
2431        }
2432
2433        raid_device->num_pds = num_pds;
2434        sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
2435            sizeof(Mpi2RaidVol0PhysDisk_t));
2436        vol_pg0 = kzalloc(sz, GFP_KERNEL);
2437        if (!vol_pg0) {
2438                dfailprintk(ioc,
2439                            ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2440                                     __FILE__, __LINE__, __func__));
2441                return 1;
2442        }
2443
2444        if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
2445             MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
2446                dfailprintk(ioc,
2447                            ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2448                                     __FILE__, __LINE__, __func__));
2449                kfree(vol_pg0);
2450                return 1;
2451        }
2452
2453        raid_device->volume_type = vol_pg0->VolumeType;
2454
2455        /* figure out what the underlying devices are by
2456         * obtaining the device_info bits for the 1st device
2457         */
2458        if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
2459            &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
2460            vol_pg0->PhysDisk[0].PhysDiskNum))) {
2461                if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
2462                    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
2463                    le16_to_cpu(pd_pg0.DevHandle)))) {
2464                        raid_device->device_info =
2465                            le32_to_cpu(sas_device_pg0.DeviceInfo);
2466                }
2467        }
2468
2469        kfree(vol_pg0);
2470        return 0;
2471}
2472
2473/**
2474 * _scsih_enable_tlr - setting TLR flags
2475 * @ioc: per adapter object
2476 * @sdev: scsi device struct
2477 *
2478 * Enabling Transaction Layer Retries for tape devices when
2479 * vpd page 0x90 is present
2480 *
2481 */
2482static void
2483_scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
2484{
2485
2486        /* only for TAPE */
2487        if (sdev->type != TYPE_TAPE)
2488                return;
2489
2490        if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
2491                return;
2492
2493        sas_enable_tlr(sdev);
2494        sdev_printk(KERN_INFO, sdev, "TLR %s\n",
2495            sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
2496        return;
2497
2498}
2499
2500/**
2501 * scsih_slave_configure - device configure routine.
2502 * @sdev: scsi device struct
2503 *
2504 * Return: 0 if ok. Any other return is assumed to be an error and
2505 * the device is ignored.
2506 */
2507static int
2508scsih_slave_configure(struct scsi_device *sdev)
2509{
2510        struct Scsi_Host *shost = sdev->host;
2511        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2512        struct MPT3SAS_DEVICE *sas_device_priv_data;
2513        struct MPT3SAS_TARGET *sas_target_priv_data;
2514        struct _sas_device *sas_device;
2515        struct _pcie_device *pcie_device;
2516        struct _raid_device *raid_device;
2517        unsigned long flags;
2518        int qdepth;
2519        u8 ssp_target = 0;
2520        char *ds = "";
2521        char *r_level = "";
2522        u16 handle, volume_handle = 0;
2523        u64 volume_wwid = 0;
2524
2525        qdepth = 1;
2526        sas_device_priv_data = sdev->hostdata;
2527        sas_device_priv_data->configured_lun = 1;
2528        sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
2529        sas_target_priv_data = sas_device_priv_data->sas_target;
2530        handle = sas_target_priv_data->handle;
2531
2532        /* raid volume handling */
2533        if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
2534
2535                spin_lock_irqsave(&ioc->raid_device_lock, flags);
2536                raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
2537                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
2538                if (!raid_device) {
2539                        dfailprintk(ioc,
2540                                    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2541                                             __FILE__, __LINE__, __func__));
2542                        return 1;
2543                }
2544
2545                if (_scsih_get_volume_capabilities(ioc, raid_device)) {
2546                        dfailprintk(ioc,
2547                                    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2548                                             __FILE__, __LINE__, __func__));
2549                        return 1;
2550                }
2551
2552                /*
2553                 * WARPDRIVE: Initialize the required data for Direct IO
2554                 */
2555                mpt3sas_init_warpdrive_properties(ioc, raid_device);
2556
2557                /* RAID Queue Depth Support
2558                 * IS volume = underlying qdepth of drive type, either
2559                 *    MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
2560                 * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
2561                 */
2562                if (raid_device->device_info &
2563                    MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2564                        qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
2565                        ds = "SSP";
2566                } else {
2567                        qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
2568                        if (raid_device->device_info &
2569                            MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2570                                ds = "SATA";
2571                        else
2572                                ds = "STP";
2573                }
2574
2575                switch (raid_device->volume_type) {
2576                case MPI2_RAID_VOL_TYPE_RAID0:
2577                        r_level = "RAID0";
2578                        break;
2579                case MPI2_RAID_VOL_TYPE_RAID1E:
2580                        qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
2581                        if (ioc->manu_pg10.OEMIdentifier &&
2582                            (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
2583                            MFG10_GF0_R10_DISPLAY) &&
2584                            !(raid_device->num_pds % 2))
2585                                r_level = "RAID10";
2586                        else
2587                                r_level = "RAID1E";
2588                        break;
2589                case MPI2_RAID_VOL_TYPE_RAID1:
2590                        qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
2591                        r_level = "RAID1";
2592                        break;
2593                case MPI2_RAID_VOL_TYPE_RAID10:
2594                        qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
2595                        r_level = "RAID10";
2596                        break;
2597                case MPI2_RAID_VOL_TYPE_UNKNOWN:
2598                default:
2599                        qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
2600                        r_level = "RAIDX";
2601                        break;
2602                }
2603
2604                if (!ioc->hide_ir_msg)
2605                        sdev_printk(KERN_INFO, sdev,
2606                           "%s: handle(0x%04x), wwid(0x%016llx),"
2607                            " pd_count(%d), type(%s)\n",
2608                            r_level, raid_device->handle,
2609                            (unsigned long long)raid_device->wwid,
2610                            raid_device->num_pds, ds);
2611
2612                if (shost->max_sectors > MPT3SAS_RAID_MAX_SECTORS) {
2613                        blk_queue_max_hw_sectors(sdev->request_queue,
2614                                                MPT3SAS_RAID_MAX_SECTORS);
2615                        sdev_printk(KERN_INFO, sdev,
2616                                        "Set queue's max_sector to: %u\n",
2617                                                MPT3SAS_RAID_MAX_SECTORS);
2618                }
2619
2620                mpt3sas_scsih_change_queue_depth(sdev, qdepth);
2621
2622                /* raid transport support */
2623                if (!ioc->is_warpdrive)
2624                        _scsih_set_level(ioc, sdev, raid_device->volume_type);
2625                return 0;
2626        }
2627
2628        /* non-raid handling */
2629        if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
2630                if (mpt3sas_config_get_volume_handle(ioc, handle,
2631                    &volume_handle)) {
2632                        dfailprintk(ioc,
2633                                    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2634                                             __FILE__, __LINE__, __func__));
2635                        return 1;
2636                }
2637                if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
2638                    volume_handle, &volume_wwid)) {
2639                        dfailprintk(ioc,
2640                                    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2641                                             __FILE__, __LINE__, __func__));
2642                        return 1;
2643                }
2644        }
2645
2646        /* PCIe handling */
2647        if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_PCIE_DEVICE) {
2648                spin_lock_irqsave(&ioc->pcie_device_lock, flags);
2649                pcie_device = __mpt3sas_get_pdev_by_wwid(ioc,
2650                                sas_device_priv_data->sas_target->sas_address);
2651                if (!pcie_device) {
2652                        spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
2653                        dfailprintk(ioc,
2654                                    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2655                                             __FILE__, __LINE__, __func__));
2656                        return 1;
2657                }
2658
2659                qdepth = MPT3SAS_NVME_QUEUE_DEPTH;
2660                ds = "NVMe";
2661                sdev_printk(KERN_INFO, sdev,
2662                        "%s: handle(0x%04x), wwid(0x%016llx), port(%d)\n",
2663                        ds, handle, (unsigned long long)pcie_device->wwid,
2664                        pcie_device->port_num);
2665                if (pcie_device->enclosure_handle != 0)
2666                        sdev_printk(KERN_INFO, sdev,
2667                        "%s: enclosure logical id(0x%016llx), slot(%d)\n",
2668                        ds,
2669                        (unsigned long long)pcie_device->enclosure_logical_id,
2670                        pcie_device->slot);
2671                if (pcie_device->connector_name[0] != '\0')
2672                        sdev_printk(KERN_INFO, sdev,
2673                                "%s: enclosure level(0x%04x),"
2674                                "connector name( %s)\n", ds,
2675                                pcie_device->enclosure_level,
2676                                pcie_device->connector_name);
2677
2678                if (pcie_device->nvme_mdts)
2679                        blk_queue_max_hw_sectors(sdev->request_queue,
2680                                        pcie_device->nvme_mdts/512);
2681
2682                pcie_device_put(pcie_device);
2683                spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
2684                mpt3sas_scsih_change_queue_depth(sdev, qdepth);
2685                /* Enable QUEUE_FLAG_NOMERGES flag, so that IOs won't be
2686                 ** merged and can eliminate holes created during merging
2687                 ** operation.
2688                 **/
2689                blk_queue_flag_set(QUEUE_FLAG_NOMERGES,
2690                                sdev->request_queue);
2691                blk_queue_virt_boundary(sdev->request_queue,
2692                                ioc->page_size - 1);
2693                return 0;
2694        }
2695
2696        spin_lock_irqsave(&ioc->sas_device_lock, flags);
2697        sas_device = __mpt3sas_get_sdev_by_addr(ioc,
2698           sas_device_priv_data->sas_target->sas_address,
2699           sas_device_priv_data->sas_target->port);
2700        if (!sas_device) {
2701                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2702                dfailprintk(ioc,
2703                            ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2704                                     __FILE__, __LINE__, __func__));
2705                return 1;
2706        }
2707
2708        sas_device->volume_handle = volume_handle;
2709        sas_device->volume_wwid = volume_wwid;
2710        if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2711                qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
2712                ssp_target = 1;
2713                if (sas_device->device_info &
2714                                MPI2_SAS_DEVICE_INFO_SEP) {
2715                        sdev_printk(KERN_WARNING, sdev,
2716                        "set ignore_delay_remove for handle(0x%04x)\n",
2717                        sas_device_priv_data->sas_target->handle);
2718                        sas_device_priv_data->ignore_delay_remove = 1;
2719                        ds = "SES";
2720                } else
2721                        ds = "SSP";
2722        } else {
2723                qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
2724                if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
2725                        ds = "STP";
2726                else if (sas_device->device_info &
2727                    MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2728                        ds = "SATA";
2729        }
2730
2731        sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
2732            "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
2733            ds, handle, (unsigned long long)sas_device->sas_address,
2734            sas_device->phy, (unsigned long long)sas_device->device_name);
2735
2736        _scsih_display_enclosure_chassis_info(NULL, sas_device, sdev, NULL);
2737
2738        sas_device_put(sas_device);
2739        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2740
2741        if (!ssp_target)
2742                _scsih_display_sata_capabilities(ioc, handle, sdev);
2743
2744
2745        mpt3sas_scsih_change_queue_depth(sdev, qdepth);
2746
2747        if (ssp_target) {
2748                sas_read_port_mode_page(sdev);
2749                _scsih_enable_tlr(ioc, sdev);
2750        }
2751
2752        return 0;
2753}
2754
2755/**
2756 * scsih_bios_param - fetch head, sector, cylinder info for a disk
2757 * @sdev: scsi device struct
2758 * @bdev: pointer to block device context
2759 * @capacity: device size (in 512 byte sectors)
2760 * @params: three element array to place output:
2761 *              params[0] number of heads (max 255)
2762 *              params[1] number of sectors (max 63)
2763 *              params[2] number of cylinders
2764 */
2765static int
2766scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2767        sector_t capacity, int params[])
2768{
2769        int             heads;
2770        int             sectors;
2771        sector_t        cylinders;
2772        ulong           dummy;
2773
2774        heads = 64;
2775        sectors = 32;
2776
2777        dummy = heads * sectors;
2778        cylinders = capacity;
2779        sector_div(cylinders, dummy);
2780
2781        /*
2782         * Handle extended translation size for logical drives
2783         * > 1Gb
2784         */
2785        if ((ulong)capacity >= 0x200000) {
2786                heads = 255;
2787                sectors = 63;
2788                dummy = heads * sectors;
2789                cylinders = capacity;
2790                sector_div(cylinders, dummy);
2791        }
2792
2793        /* return result */
2794        params[0] = heads;
2795        params[1] = sectors;
2796        params[2] = cylinders;
2797
2798        return 0;
2799}
2800
2801/**
2802 * _scsih_response_code - translation of device response code
2803 * @ioc: per adapter object
2804 * @response_code: response code returned by the device
2805 */
2806static void
2807_scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
2808{
2809        char *desc;
2810
2811        switch (response_code) {
2812        case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2813                desc = "task management request completed";
2814                break;
2815        case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2816                desc = "invalid frame";
2817                break;
2818        case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2819                desc = "task management request not supported";
2820                break;
2821        case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2822                desc = "task management request failed";
2823                break;
2824        case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2825                desc = "task management request succeeded";
2826                break;
2827        case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2828                desc = "invalid lun";
2829                break;
2830        case 0xA:
2831                desc = "overlapped tag attempted";
2832                break;
2833        case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2834                desc = "task queued, however not sent to target";
2835                break;
2836        default:
2837                desc = "unknown";
2838                break;
2839        }
2840        ioc_warn(ioc, "response_code(0x%01x): %s\n", response_code, desc);
2841}
2842
2843/**
2844 * _scsih_tm_done - tm completion routine
2845 * @ioc: per adapter object
2846 * @smid: system request message index
2847 * @msix_index: MSIX table index supplied by the OS
2848 * @reply: reply message frame(lower 32bit addr)
2849 * Context: none.
2850 *
2851 * The callback handler when using scsih_issue_tm.
2852 *
2853 * Return: 1 meaning mf should be freed from _base_interrupt
2854 *         0 means the mf is freed from this function.
2855 */
2856static u8
2857_scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
2858{
2859        MPI2DefaultReply_t *mpi_reply;
2860
2861        if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
2862                return 1;
2863        if (ioc->tm_cmds.smid != smid)
2864                return 1;
2865        ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
2866        mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
2867        if (mpi_reply) {
2868                memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2869                ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
2870        }
2871        ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
2872        complete(&ioc->tm_cmds.done);
2873        return 1;
2874}
2875
2876/**
2877 * mpt3sas_scsih_set_tm_flag - set per target tm_busy
2878 * @ioc: per adapter object
2879 * @handle: device handle
2880 *
2881 * During taskmangement request, we need to freeze the device queue.
2882 */
2883void
2884mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2885{
2886        struct MPT3SAS_DEVICE *sas_device_priv_data;
2887        struct scsi_device *sdev;
2888        u8 skip = 0;
2889
2890        shost_for_each_device(sdev, ioc->shost) {
2891                if (skip)
2892                        continue;
2893                sas_device_priv_data = sdev->hostdata;
2894                if (!sas_device_priv_data)
2895                        continue;
2896                if (sas_device_priv_data->sas_target->handle == handle) {
2897                        sas_device_priv_data->sas_target->tm_busy = 1;
2898                        skip = 1;
2899                        ioc->ignore_loginfos = 1;
2900                }
2901        }
2902}
2903
2904/**
2905 * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
2906 * @ioc: per adapter object
2907 * @handle: device handle
2908 *
2909 * During taskmangement request, we need to freeze the device queue.
2910 */
2911void
2912mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2913{
2914        struct MPT3SAS_DEVICE *sas_device_priv_data;
2915        struct scsi_device *sdev;
2916        u8 skip = 0;
2917
2918        shost_for_each_device(sdev, ioc->shost) {
2919                if (skip)
2920                        continue;
2921                sas_device_priv_data = sdev->hostdata;
2922                if (!sas_device_priv_data)
2923                        continue;
2924                if (sas_device_priv_data->sas_target->handle == handle) {
2925                        sas_device_priv_data->sas_target->tm_busy = 0;
2926                        skip = 1;
2927                        ioc->ignore_loginfos = 0;
2928                }
2929        }
2930}
2931
2932/**
2933 * scsih_tm_cmd_map_status - map the target reset & LUN reset TM status
2934 * @ioc: per adapter object
2935 * @channel: the channel assigned by the OS
2936 * @id: the id assigned by the OS
2937 * @lun: lun number
2938 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2939 * @smid_task: smid assigned to the task
2940 *
2941 * Look whether TM has aborted the timed out SCSI command, if
2942 * TM has aborted the IO then return SUCCESS else return FAILED.
2943 */
2944static int
2945scsih_tm_cmd_map_status(struct MPT3SAS_ADAPTER *ioc, uint channel,
2946        uint id, uint lun, u8 type, u16 smid_task)
2947{
2948
2949        if (smid_task <= ioc->shost->can_queue) {
2950                switch (type) {
2951                case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2952                        if (!(_scsih_scsi_lookup_find_by_target(ioc,
2953                            id, channel)))
2954                                return SUCCESS;
2955                        break;
2956                case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
2957                case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2958                        if (!(_scsih_scsi_lookup_find_by_lun(ioc, id,
2959                            lun, channel)))
2960                                return SUCCESS;
2961                        break;
2962                default:
2963                        return SUCCESS;
2964                }
2965        } else if (smid_task == ioc->scsih_cmds.smid) {
2966                if ((ioc->scsih_cmds.status & MPT3_CMD_COMPLETE) ||
2967                    (ioc->scsih_cmds.status & MPT3_CMD_NOT_USED))
2968                        return SUCCESS;
2969        } else if (smid_task == ioc->ctl_cmds.smid) {
2970                if ((ioc->ctl_cmds.status & MPT3_CMD_COMPLETE) ||
2971                    (ioc->ctl_cmds.status & MPT3_CMD_NOT_USED))
2972                        return SUCCESS;
2973        }
2974
2975        return FAILED;
2976}
2977
2978/**
2979 * scsih_tm_post_processing - post processing of target & LUN reset
2980 * @ioc: per adapter object
2981 * @handle: device handle
2982 * @channel: the channel assigned by the OS
2983 * @id: the id assigned by the OS
2984 * @lun: lun number
2985 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2986 * @smid_task: smid assigned to the task
2987 *
2988 * Post processing of target & LUN reset. Due to interrupt latency
2989 * issue it possible that interrupt for aborted IO might not be
2990 * received yet. So before returning failure status, poll the
2991 * reply descriptor pools for the reply of timed out SCSI command.
2992 * Return FAILED status if reply for timed out is not received
2993 * otherwise return SUCCESS.
2994 */
2995static int
2996scsih_tm_post_processing(struct MPT3SAS_ADAPTER *ioc, u16 handle,
2997        uint channel, uint id, uint lun, u8 type, u16 smid_task)
2998{
2999        int rc;
3000
3001        rc = scsih_tm_cmd_map_status(ioc, channel, id, lun, type, smid_task);
3002        if (rc == SUCCESS)
3003                return rc;
3004
3005        ioc_info(ioc,
3006            "Poll ReplyDescriptor queues for completion of"
3007            " smid(%d), task_type(0x%02x), handle(0x%04x)\n",
3008            smid_task, type, handle);
3009
3010        /*
3011         * Due to interrupt latency issues, driver may receive interrupt for
3012         * TM first and then for aborted SCSI IO command. So, poll all the
3013         * ReplyDescriptor pools before returning the FAILED status to SML.
3014         */
3015        mpt3sas_base_mask_interrupts(ioc);
3016        mpt3sas_base_sync_reply_irqs(ioc, 1);
3017        mpt3sas_base_unmask_interrupts(ioc);
3018
3019        return scsih_tm_cmd_map_status(ioc, channel, id, lun, type, smid_task);
3020}
3021
3022/**
3023 * mpt3sas_scsih_issue_tm - main routine for sending tm requests
3024 * @ioc: per adapter struct
3025 * @handle: device handle
3026 * @channel: the channel assigned by the OS
3027 * @id: the id assigned by the OS
3028 * @lun: lun number
3029 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
3030 * @smid_task: smid assigned to the task
3031 * @msix_task: MSIX table index supplied by the OS
3032 * @timeout: timeout in seconds
3033 * @tr_method: Target Reset Method
3034 * Context: user
3035 *
3036 * A generic API for sending task management requests to firmware.
3037 *
3038 * The callback index is set inside `ioc->tm_cb_idx`.
3039 * The caller is responsible to check for outstanding commands.
3040 *
3041 * Return: SUCCESS or FAILED.
3042 */
3043int
3044mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
3045        uint id, u64 lun, u8 type, u16 smid_task, u16 msix_task,
3046        u8 timeout, u8 tr_method)
3047{
3048        Mpi2SCSITaskManagementRequest_t *mpi_request;
3049        Mpi2SCSITaskManagementReply_t *mpi_reply;
3050        Mpi25SCSIIORequest_t *request;
3051        u16 smid = 0;
3052        u32 ioc_state;
3053        int rc;
3054        u8 issue_reset = 0;
3055
3056        lockdep_assert_held(&ioc->tm_cmds.mutex);
3057
3058        if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
3059                ioc_info(ioc, "%s: tm_cmd busy!!!\n", __func__);
3060                return FAILED;
3061        }
3062
3063        if (ioc->shost_recovery || ioc->remove_host ||
3064            ioc->pci_error_recovery) {
3065                ioc_info(ioc, "%s: host reset in progress!\n", __func__);
3066                return FAILED;
3067        }
3068
3069        ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
3070        if (ioc_state & MPI2_DOORBELL_USED) {
3071                dhsprintk(ioc, ioc_info(ioc, "unexpected doorbell active!\n"));
3072                rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
3073                return (!rc) ? SUCCESS : FAILED;
3074        }
3075
3076        if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
3077                mpt3sas_print_fault_code(ioc, ioc_state &
3078                    MPI2_DOORBELL_DATA_MASK);
3079                rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
3080                return (!rc) ? SUCCESS : FAILED;
3081        } else if ((ioc_state & MPI2_IOC_STATE_MASK) ==
3082            MPI2_IOC_STATE_COREDUMP) {
3083                mpt3sas_print_coredump_info(ioc, ioc_state &
3084                    MPI2_DOORBELL_DATA_MASK);
3085                rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
3086                return (!rc) ? SUCCESS : FAILED;
3087        }
3088
3089        smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
3090        if (!smid) {
3091                ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
3092                return FAILED;
3093        }
3094
3095        dtmprintk(ioc,
3096                  ioc_info(ioc, "sending tm: handle(0x%04x), task_type(0x%02x), smid(%d), timeout(%d), tr_method(0x%x)\n",
3097                           handle, type, smid_task, timeout, tr_method));
3098        ioc->tm_cmds.status = MPT3_CMD_PENDING;
3099        mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3100        ioc->tm_cmds.smid = smid;
3101        memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3102        memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
3103        mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3104        mpi_request->DevHandle = cpu_to_le16(handle);
3105        mpi_request->TaskType = type;
3106        if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
3107            type == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
3108                mpi_request->MsgFlags = tr_method;
3109        mpi_request->TaskMID = cpu_to_le16(smid_task);
3110        int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
3111        mpt3sas_scsih_set_tm_flag(ioc, handle);
3112        init_completion(&ioc->tm_cmds.done);
3113        ioc->put_smid_hi_priority(ioc, smid, msix_task);
3114        wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
3115        if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
3116                mpt3sas_check_cmd_timeout(ioc,
3117                    ioc->tm_cmds.status, mpi_request,
3118                    sizeof(Mpi2SCSITaskManagementRequest_t)/4, issue_reset);
3119                if (issue_reset) {
3120                        rc = mpt3sas_base_hard_reset_handler(ioc,
3121                                        FORCE_BIG_HAMMER);
3122                        rc = (!rc) ? SUCCESS : FAILED;
3123                        goto out;
3124                }
3125        }
3126
3127        /* sync IRQs in case those were busy during flush. */
3128        mpt3sas_base_sync_reply_irqs(ioc, 0);
3129
3130        if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
3131                mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
3132                mpi_reply = ioc->tm_cmds.reply;
3133                dtmprintk(ioc,
3134                          ioc_info(ioc, "complete tm: ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
3135                                   le16_to_cpu(mpi_reply->IOCStatus),
3136                                   le32_to_cpu(mpi_reply->IOCLogInfo),
3137                                   le32_to_cpu(mpi_reply->TerminationCount)));
3138                if (ioc->logging_level & MPT_DEBUG_TM) {
3139                        _scsih_response_code(ioc, mpi_reply->ResponseCode);
3140                        if (mpi_reply->IOCStatus)
3141                                _debug_dump_mf(mpi_request,
3142                                    sizeof(Mpi2SCSITaskManagementRequest_t)/4);
3143                }
3144        }
3145
3146        switch (type) {
3147        case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
3148                rc = SUCCESS;
3149                /*
3150                 * If DevHandle filed in smid_task's entry of request pool
3151                 * doesn't match with device handle on which this task abort
3152                 * TM is received then it means that TM has successfully
3153                 * aborted the timed out command. Since smid_task's entry in
3154                 * request pool will be memset to zero once the timed out
3155                 * command is returned to the SML. If the command is not
3156                 * aborted then smid_task\xE2\x80\x99s entry won\xE2\x80\x99t be cleared and it
3157                 * will have same DevHandle value on which this task abort TM
3158                 * is received and driver will return the TM status as FAILED.
3159                 */
3160                request = mpt3sas_base_get_msg_frame(ioc, smid_task);
3161                if (le16_to_cpu(request->DevHandle) != handle)
3162                        break;
3163
3164                ioc_info(ioc, "Task abort tm failed: handle(0x%04x),"
3165                    "timeout(%d) tr_method(0x%x) smid(%d) msix_index(%d)\n",
3166                    handle, timeout, tr_method, smid_task, msix_task);
3167                rc = FAILED;
3168                break;
3169
3170        case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
3171        case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
3172        case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
3173                rc = scsih_tm_post_processing(ioc, handle, channel, id, lun,
3174                    type, smid_task);
3175                break;
3176        case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
3177                rc = SUCCESS;
3178                break;
3179        default:
3180                rc = FAILED;
3181                break;
3182        }
3183
3184out:
3185        mpt3sas_scsih_clear_tm_flag(ioc, handle);
3186        ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
3187        return rc;
3188}
3189
3190int mpt3sas_scsih_issue_locked_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle,
3191                uint channel, uint id, u64 lun, u8 type, u16 smid_task,
3192                u16 msix_task, u8 timeout, u8 tr_method)
3193{
3194        int ret;
3195
3196        mutex_lock(&ioc->tm_cmds.mutex);
3197        ret = mpt3sas_scsih_issue_tm(ioc, handle, channel, id, lun, type,
3198                        smid_task, msix_task, timeout, tr_method);
3199        mutex_unlock(&ioc->tm_cmds.mutex);
3200
3201        return ret;
3202}
3203
3204/**
3205 * _scsih_tm_display_info - displays info about the device
3206 * @ioc: per adapter struct
3207 * @scmd: pointer to scsi command object
3208 *
3209 * Called by task management callback handlers.
3210 */
3211static void
3212_scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
3213{
3214        struct scsi_target *starget = scmd->device->sdev_target;
3215        struct MPT3SAS_TARGET *priv_target = starget->hostdata;
3216        struct _sas_device *sas_device = NULL;
3217        struct _pcie_device *pcie_device = NULL;
3218        unsigned long flags;
3219        char *device_str = NULL;
3220
3221        if (!priv_target)
3222                return;
3223        if (ioc->hide_ir_msg)
3224                device_str = "WarpDrive";
3225        else
3226                device_str = "volume";
3227
3228        scsi_print_command(scmd);
3229        if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
3230                starget_printk(KERN_INFO, starget,
3231                        "%s handle(0x%04x), %s wwid(0x%016llx)\n",
3232                        device_str, priv_target->handle,
3233                    device_str, (unsigned long long)priv_target->sas_address);
3234
3235        } else if (priv_target->flags & MPT_TARGET_FLAGS_PCIE_DEVICE) {
3236                spin_lock_irqsave(&ioc->pcie_device_lock, flags);
3237                pcie_device = __mpt3sas_get_pdev_from_target(ioc, priv_target);
3238                if (pcie_device) {
3239                        starget_printk(KERN_INFO, starget,
3240                                "handle(0x%04x), wwid(0x%016llx), port(%d)\n",
3241                                pcie_device->handle,
3242                                (unsigned long long)pcie_device->wwid,
3243                                pcie_device->port_num);
3244                        if (pcie_device->enclosure_handle != 0)
3245                                starget_printk(KERN_INFO, starget,
3246                                        "enclosure logical id(0x%016llx), slot(%d)\n",
3247                                        (unsigned long long)
3248                                        pcie_device->enclosure_logical_id,
3249                                        pcie_device->slot);
3250                        if (pcie_device->connector_name[0] != '\0')
3251                                starget_printk(KERN_INFO, starget,
3252                                        "enclosure level(0x%04x), connector name( %s)\n",
3253                                        pcie_device->enclosure_level,
3254                                        pcie_device->connector_name);
3255                        pcie_device_put(pcie_device);
3256                }
3257                spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
3258
3259        } else {
3260                spin_lock_irqsave(&ioc->sas_device_lock, flags);
3261                sas_device = __mpt3sas_get_sdev_from_target(ioc, priv_target);
3262                if (sas_device) {
3263                        if (priv_target->flags &
3264                            MPT_TARGET_FLAGS_RAID_COMPONENT) {
3265                                starget_printk(KERN_INFO, starget,
3266                                    "volume handle(0x%04x), "
3267                                    "volume wwid(0x%016llx)\n",
3268                                    sas_device->volume_handle,
3269                                   (unsigned long long)sas_device->volume_wwid);
3270                        }
3271                        starget_printk(KERN_INFO, starget,
3272                            "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
3273                            sas_device->handle,
3274                            (unsigned long long)sas_device->sas_address,
3275                            sas_device->phy);
3276
3277                        _scsih_display_enclosure_chassis_info(NULL, sas_device,
3278                            NULL, starget);
3279
3280                        sas_device_put(sas_device);
3281                }
3282                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3283        }
3284}
3285
3286/**
3287 * scsih_abort - eh threads main abort routine
3288 * @scmd: pointer to scsi command object
3289 *
3290 * Return: SUCCESS if command aborted else FAILED
3291 */
3292static int
3293scsih_abort(struct scsi_cmnd *scmd)
3294{
3295        struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3296        struct MPT3SAS_DEVICE *sas_device_priv_data;
3297        struct scsiio_tracker *st = scsi_cmd_priv(scmd);
3298        u16 handle;
3299        int r;
3300
3301        u8 timeout = 30;
3302        struct _pcie_device *pcie_device = NULL;
3303        sdev_printk(KERN_INFO, scmd->device, "attempting task abort!"
3304            "scmd(0x%p), outstanding for %u ms & timeout %u ms\n",
3305            scmd, jiffies_to_msecs(jiffies - scmd->jiffies_at_alloc),
3306            (scmd->request->timeout / HZ) * 1000);
3307        _scsih_tm_display_info(ioc, scmd);
3308
3309        sas_device_priv_data = scmd->device->hostdata;
3310        if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3311            ioc->remove_host) {
3312                sdev_printk(KERN_INFO, scmd->device,
3313                    "device been deleted! scmd(0x%p)\n", scmd);
3314                scmd->result = DID_NO_CONNECT << 16;
3315                scmd->scsi_done(scmd);
3316                r = SUCCESS;
3317                goto out;
3318        }
3319
3320        /* check for completed command */
3321        if (st == NULL || st->cb_idx == 0xFF) {
3322                sdev_printk(KERN_INFO, scmd->device, "No reference found at "
3323                    "driver, assuming scmd(0x%p) might have completed\n", scmd);
3324                scmd->result = DID_RESET << 16;
3325                r = SUCCESS;
3326                goto out;
3327        }
3328
3329        /* for hidden raid components and volumes this is not supported */
3330        if (sas_device_priv_data->sas_target->flags &
3331            MPT_TARGET_FLAGS_RAID_COMPONENT ||
3332            sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
3333                scmd->result = DID_RESET << 16;
3334                r = FAILED;
3335                goto out;
3336        }
3337
3338        mpt3sas_halt_firmware(ioc);
3339
3340        handle = sas_device_priv_data->sas_target->handle;
3341        pcie_device = mpt3sas_get_pdev_by_handle(ioc, handle);
3342        if (pcie_device && (!ioc->tm_custom_handling) &&
3343            (!(mpt3sas_scsih_is_pcie_scsi_device(pcie_device->device_info))))
3344                timeout = ioc->nvme_abort_timeout;
3345        r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
3346                scmd->device->id, scmd->device->lun,
3347                MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
3348                st->smid, st->msix_io, timeout, 0);
3349        /* Command must be cleared after abort */
3350        if (r == SUCCESS && st->cb_idx != 0xFF)
3351                r = FAILED;
3352 out:
3353        sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(0x%p)\n",
3354            ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3355        if (pcie_device)
3356                pcie_device_put(pcie_device);
3357        return r;
3358}
3359
3360/**
3361 * scsih_dev_reset - eh threads main device reset routine
3362 * @scmd: pointer to scsi command object
3363 *
3364 * Return: SUCCESS if command aborted else FAILED
3365 */
3366static int
3367scsih_dev_reset(struct scsi_cmnd *scmd)
3368{
3369        struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3370        struct MPT3SAS_DEVICE *sas_device_priv_data;
3371        struct _sas_device *sas_device = NULL;
3372        struct _pcie_device *pcie_device = NULL;
3373        u16     handle;
3374        u8      tr_method = 0;
3375        u8      tr_timeout = 30;
3376        int r;
3377
3378        struct scsi_target *starget = scmd->device->sdev_target;
3379        struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
3380
3381        sdev_printk(KERN_INFO, scmd->device,
3382            "attempting device reset! scmd(0x%p)\n", scmd);
3383        _scsih_tm_display_info(ioc, scmd);
3384
3385        sas_device_priv_data = scmd->device->hostdata;
3386        if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3387            ioc->remove_host) {
3388                sdev_printk(KERN_INFO, scmd->device,
3389                    "device been deleted! scmd(0x%p)\n", scmd);
3390                scmd->result = DID_NO_CONNECT << 16;
3391                scmd->scsi_done(scmd);
3392                r = SUCCESS;
3393                goto out;
3394        }
3395
3396        /* for hidden raid components obtain the volume_handle */
3397        handle = 0;
3398        if (sas_device_priv_data->sas_target->flags &
3399            MPT_TARGET_FLAGS_RAID_COMPONENT) {
3400                sas_device = mpt3sas_get_sdev_from_target(ioc,
3401                                target_priv_data);
3402                if (sas_device)
3403                        handle = sas_device->volume_handle;
3404        } else
3405                handle = sas_device_priv_data->sas_target->handle;
3406
3407        if (!handle) {
3408                scmd->result = DID_RESET << 16;
3409                r = FAILED;
3410                goto out;
3411        }
3412
3413        pcie_device = mpt3sas_get_pdev_by_handle(ioc, handle);
3414
3415        if (pcie_device && (!ioc->tm_custom_handling) &&
3416            (!(mpt3sas_scsih_is_pcie_scsi_device(pcie_device->device_info)))) {
3417                tr_timeout = pcie_device->reset_timeout;
3418                tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
3419        } else
3420                tr_method = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
3421
3422        r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
3423                scmd->device->id, scmd->device->lun,
3424                MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 0,
3425                tr_timeout, tr_method);
3426        /* Check for busy commands after reset */
3427        if (r == SUCCESS && scsi_device_busy(scmd->device))
3428                r = FAILED;
3429 out:
3430        sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(0x%p)\n",
3431            ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3432
3433        if (sas_device)
3434                sas_device_put(sas_device);
3435        if (pcie_device)
3436                pcie_device_put(pcie_device);
3437
3438        return r;
3439}
3440
3441/**
3442 * scsih_target_reset - eh threads main target reset routine
3443 * @scmd: pointer to scsi command object
3444 *
3445 * Return: SUCCESS if command aborted else FAILED
3446 */
3447static int
3448scsih_target_reset(struct scsi_cmnd *scmd)
3449{
3450        struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3451        struct MPT3SAS_DEVICE *sas_device_priv_data;
3452        struct _sas_device *sas_device = NULL;
3453        struct _pcie_device *pcie_device = NULL;
3454        u16     handle;
3455        u8      tr_method = 0;
3456        u8      tr_timeout = 30;
3457        int r;
3458        struct scsi_target *starget = scmd->device->sdev_target;
3459        struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
3460
3461        starget_printk(KERN_INFO, starget,
3462            "attempting target reset! scmd(0x%p)\n", scmd);
3463        _scsih_tm_display_info(ioc, scmd);
3464
3465        sas_device_priv_data = scmd->device->hostdata;
3466        if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3467            ioc->remove_host) {
3468                starget_printk(KERN_INFO, starget,
3469                    "target been deleted! scmd(0x%p)\n", scmd);
3470                scmd->result = DID_NO_CONNECT << 16;
3471                scmd->scsi_done(scmd);
3472                r = SUCCESS;
3473                goto out;
3474        }
3475
3476        /* for hidden raid components obtain the volume_handle */
3477        handle = 0;
3478        if (sas_device_priv_data->sas_target->flags &
3479            MPT_TARGET_FLAGS_RAID_COMPONENT) {
3480                sas_device = mpt3sas_get_sdev_from_target(ioc,
3481                                target_priv_data);
3482                if (sas_device)
3483                        handle = sas_device->volume_handle;
3484        } else
3485                handle = sas_device_priv_data->sas_target->handle;
3486
3487        if (!handle) {
3488                scmd->result = DID_RESET << 16;
3489                r = FAILED;
3490                goto out;
3491        }
3492
3493        pcie_device = mpt3sas_get_pdev_by_handle(ioc, handle);
3494
3495        if (pcie_device && (!ioc->tm_custom_handling) &&
3496            (!(mpt3sas_scsih_is_pcie_scsi_device(pcie_device->device_info)))) {
3497                tr_timeout = pcie_device->reset_timeout;
3498                tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
3499        } else
3500                tr_method = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
3501        r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
3502                scmd->device->id, 0,
3503                MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 0,
3504            tr_timeout, tr_method);
3505        /* Check for busy commands after reset */
3506        if (r == SUCCESS && atomic_read(&starget->target_busy))
3507                r = FAILED;
3508 out:
3509        starget_printk(KERN_INFO, starget, "target reset: %s scmd(0x%p)\n",
3510            ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3511
3512        if (sas_device)
3513                sas_device_put(sas_device);
3514        if (pcie_device)
3515                pcie_device_put(pcie_device);
3516        return r;
3517}
3518
3519
3520/**
3521 * scsih_host_reset - eh threads main host reset routine
3522 * @scmd: pointer to scsi command object
3523 *
3524 * Return: SUCCESS if command aborted else FAILED
3525 */
3526static int
3527scsih_host_reset(struct scsi_cmnd *scmd)
3528{
3529        struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3530        int r, retval;
3531
3532        ioc_info(ioc, "attempting host reset! scmd(0x%p)\n", scmd);
3533        scsi_print_command(scmd);
3534
3535        if (ioc->is_driver_loading || ioc->remove_host) {
3536                ioc_info(ioc, "Blocking the host reset\n");
3537                r = FAILED;
3538                goto out;
3539        }
3540
3541        retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
3542        r = (retval < 0) ? FAILED : SUCCESS;
3543out:
3544        ioc_info(ioc, "host reset: %s scmd(0x%p)\n",
3545                 r == SUCCESS ? "SUCCESS" : "FAILED", scmd);
3546
3547        return r;
3548}
3549
3550/**
3551 * _scsih_fw_event_add - insert and queue up fw_event
3552 * @ioc: per adapter object
3553 * @fw_event: object describing the event
3554 * Context: This function will acquire ioc->fw_event_lock.
3555 *
3556 * This adds the firmware event object into link list, then queues it up to
3557 * be processed from user context.
3558 */
3559static void
3560_scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
3561{
3562        unsigned long flags;
3563
3564        if (ioc->firmware_event_thread == NULL)
3565                return;
3566
3567        spin_lock_irqsave(&ioc->fw_event_lock, flags);
3568        fw_event_work_get(fw_event);
3569        INIT_LIST_HEAD(&fw_event->list);
3570        list_add_tail(&fw_event->list, &ioc->fw_event_list);
3571        INIT_WORK(&fw_event->work, _firmware_event_work);
3572        fw_event_work_get(fw_event);
3573        queue_work(ioc->firmware_event_thread, &fw_event->work);
3574        spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3575}
3576
3577/**
3578 * _scsih_fw_event_del_from_list - delete fw_event from the list
3579 * @ioc: per adapter object
3580 * @fw_event: object describing the event
3581 * Context: This function will acquire ioc->fw_event_lock.
3582 *
3583 * If the fw_event is on the fw_event_list, remove it and do a put.
3584 */
3585static void
3586_scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
3587        *fw_event)
3588{
3589        unsigned long flags;
3590
3591        spin_lock_irqsave(&ioc->fw_event_lock, flags);
3592        if (!list_empty(&fw_event->list)) {
3593                list_del_init(&fw_event->list);
3594                fw_event_work_put(fw_event);
3595        }
3596        spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3597}
3598
3599
3600 /**
3601 * mpt3sas_send_trigger_data_event - send event for processing trigger data
3602 * @ioc: per adapter object
3603 * @event_data: trigger event data
3604 */
3605void
3606mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
3607        struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
3608{
3609        struct fw_event_work *fw_event;
3610        u16 sz;
3611
3612        if (ioc->is_driver_loading)
3613                return;
3614        sz = sizeof(*event_data);
3615        fw_event = alloc_fw_event_work(sz);
3616        if (!fw_event)
3617                return;
3618        fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
3619        fw_event->ioc = ioc;
3620        memcpy(fw_event->event_data, event_data, sizeof(*event_data));
3621        _scsih_fw_event_add(ioc, fw_event);
3622        fw_event_work_put(fw_event);
3623}
3624
3625/**
3626 * _scsih_error_recovery_delete_devices - remove devices not responding
3627 * @ioc: per adapter object
3628 */
3629static void
3630_scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
3631{
3632        struct fw_event_work *fw_event;
3633
3634        if (ioc->is_driver_loading)
3635                return;
3636        fw_event = alloc_fw_event_work(0);
3637        if (!fw_event)
3638                return;
3639        fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
3640        fw_event->ioc = ioc;
3641        _scsih_fw_event_add(ioc, fw_event);
3642        fw_event_work_put(fw_event);
3643}
3644
3645/**
3646 * mpt3sas_port_enable_complete - port enable completed (fake event)
3647 * @ioc: per adapter object
3648 */
3649void
3650mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
3651{
3652        struct fw_event_work *fw_event;
3653
3654        fw_event = alloc_fw_event_work(0);
3655        if (!fw_event)
3656                return;
3657        fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
3658        fw_event->ioc = ioc;
3659        _scsih_fw_event_add(ioc, fw_event);
3660        fw_event_work_put(fw_event);
3661}
3662
3663static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc)
3664{
3665        unsigned long flags;
3666        struct fw_event_work *fw_event = NULL;
3667
3668        spin_lock_irqsave(&ioc->fw_event_lock, flags);
3669        if (!list_empty(&ioc->fw_event_list)) {
3670                fw_event = list_first_entry(&ioc->fw_event_list,
3671                                struct fw_event_work, list);
3672                list_del_init(&fw_event->list);
3673        }
3674        spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3675
3676        return fw_event;
3677}
3678
3679/**
3680 * _scsih_fw_event_cleanup_queue - cleanup event queue
3681 * @ioc: per adapter object
3682 *
3683 * Walk the firmware event queue, either killing timers, or waiting
3684 * for outstanding events to complete
3685 *
3686 * Context: task, can sleep
3687 */
3688static void
3689_scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
3690{
3691        struct fw_event_work *fw_event;
3692
3693        if ((list_empty(&ioc->fw_event_list) && !ioc->current_event) ||
3694            !ioc->firmware_event_thread)
3695                return;
3696
3697        ioc->fw_events_cleanup = 1;
3698        while ((fw_event = dequeue_next_fw_event(ioc)) ||
3699             (fw_event = ioc->current_event)) {
3700
3701                /*
3702                 * Don't call cancel_work_sync() for current_event
3703                 * other than MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
3704                 * otherwise we may observe deadlock if current
3705                 * hard reset issued as part of processing the current_event.
3706                 *
3707                 * Orginal logic of cleaning the current_event is added
3708                 * for handling the back to back host reset issued by the user.
3709                 * i.e. during back to back host reset, driver use to process
3710                 * the two instances of MPT3SAS_REMOVE_UNRESPONDING_DEVICES
3711                 * event back to back and this made the drives to unregister
3712                 * the devices from SML.
3713                 */
3714
3715                if (fw_event == ioc->current_event &&
3716                    ioc->current_event->event !=
3717                    MPT3SAS_REMOVE_UNRESPONDING_DEVICES) {
3718                        ioc->current_event = NULL;
3719                        continue;
3720                }
3721
3722                /*
3723                 * Wait on the fw_event to complete. If this returns 1, then
3724                 * the event was never executed, and we need a put for the
3725                 * reference the work had on the fw_event.
3726                 *
3727                 * If it did execute, we wait for it to finish, and the put will
3728                 * happen from _firmware_event_work()
3729                 */
3730                if (cancel_work_sync(&fw_event->work))
3731                        fw_event_work_put(fw_event);
3732
3733                fw_event_work_put(fw_event);
3734        }
3735        ioc->fw_events_cleanup = 0;
3736}
3737
3738/**
3739 * _scsih_internal_device_block - block the sdev device
3740 * @sdev: per device object
3741 * @sas_device_priv_data : per device driver private data
3742 *
3743 * make sure device is blocked without error, if not
3744 * print an error
3745 */
3746static void
3747_scsih_internal_device_block(struct scsi_device *sdev,
3748                        struct MPT3SAS_DEVICE *sas_device_priv_data)
3749{
3750        int r = 0;
3751
3752        sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
3753            sas_device_priv_data->sas_target->handle);
3754        sas_device_priv_data->block = 1;
3755
3756        r = scsi_internal_device_block_nowait(sdev);
3757        if (r == -EINVAL)
3758                sdev_printk(KERN_WARNING, sdev,
3759                    "device_block failed with return(%d) for handle(0x%04x)\n",
3760                    r, sas_device_priv_data->sas_target->handle);
3761}
3762
3763/**
3764 * _scsih_internal_device_unblock - unblock the sdev device
3765 * @sdev: per device object
3766 * @sas_device_priv_data : per device driver private data
3767 * make sure device is unblocked without error, if not retry
3768 * by blocking and then unblocking
3769 */
3770
3771static void
3772_scsih_internal_device_unblock(struct scsi_device *sdev,
3773                        struct MPT3SAS_DEVICE *sas_device_priv_data)
3774{
3775        int r = 0;
3776
3777        sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
3778            "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
3779        sas_device_priv_data->block = 0;
3780        r = scsi_internal_device_unblock_nowait(sdev, SDEV_RUNNING);
3781        if (r == -EINVAL) {
3782                /* The device has been set to SDEV_RUNNING by SD layer during
3783                 * device addition but the request queue is still stopped by
3784                 * our earlier block call. We need to perform a block again
3785                 * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
3786
3787                sdev_printk(KERN_WARNING, sdev,
3788                    "device_unblock failed with return(%d) for handle(0x%04x) "
3789                    "performing a block followed by an unblock\n",
3790                    r, sas_device_priv_data->sas_target->handle);
3791                sas_device_priv_data->block = 1;
3792                r = scsi_internal_device_block_nowait(sdev);
3793                if (r)
3794                        sdev_printk(KERN_WARNING, sdev, "retried device_block "
3795                            "failed with return(%d) for handle(0x%04x)\n",
3796                            r, sas_device_priv_data->sas_target->handle);
3797
3798                sas_device_priv_data->block = 0;
3799                r = scsi_internal_device_unblock_nowait(sdev, SDEV_RUNNING);
3800                if (r)
3801                        sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
3802                            " failed with return(%d) for handle(0x%04x)\n",
3803                            r, sas_device_priv_data->sas_target->handle);
3804        }
3805}
3806
3807/**
3808 * _scsih_ublock_io_all_device - unblock every device
3809 * @ioc: per adapter object
3810 *
3811 * change the device state from block to running
3812 */
3813static void
3814_scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
3815{
3816        struct MPT3SAS_DEVICE *sas_device_priv_data;
3817        struct scsi_device *sdev;
3818
3819        shost_for_each_device(sdev, ioc->shost) {
3820                sas_device_priv_data = sdev->hostdata;
3821                if (!sas_device_priv_data)
3822                        continue;
3823                if (!sas_device_priv_data->block)
3824                        continue;
3825
3826                dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
3827                        "device_running, handle(0x%04x)\n",
3828                    sas_device_priv_data->sas_target->handle));
3829                _scsih_internal_device_unblock(sdev, sas_device_priv_data);
3830        }
3831}
3832
3833
3834/**
3835 * _scsih_ublock_io_device - prepare device to be deleted
3836 * @ioc: per adapter object
3837 * @sas_address: sas address
3838 * @port: hba port entry
3839 *
3840 * unblock then put device in offline state
3841 */
3842static void
3843_scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc,
3844        u64 sas_address, struct hba_port *port)
3845{
3846        struct MPT3SAS_DEVICE *sas_device_priv_data;
3847        struct scsi_device *sdev;
3848
3849        shost_for_each_device(sdev, ioc->shost) {
3850                sas_device_priv_data = sdev->hostdata;
3851                if (!sas_device_priv_data)
3852                        continue;
3853                if (sas_device_priv_data->sas_target->sas_address
3854                    != sas_address)
3855                        continue;
3856                if (sas_device_priv_data->sas_target->port != port)
3857                        continue;
3858                if (sas_device_priv_data->block)
3859                        _scsih_internal_device_unblock(sdev,
3860                                sas_device_priv_data);
3861        }
3862}
3863
3864/**
3865 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
3866 * @ioc: per adapter object
3867 *
3868 * During device pull we need to appropriately set the sdev state.
3869 */
3870static void
3871_scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
3872{
3873        struct MPT3SAS_DEVICE *sas_device_priv_data;
3874        struct scsi_device *sdev;
3875
3876        shost_for_each_device(sdev, ioc->shost) {
3877                sas_device_priv_data = sdev->hostdata;
3878                if (!sas_device_priv_data)
3879                        continue;
3880                if (sas_device_priv_data->block)
3881                        continue;
3882                if (sas_device_priv_data->ignore_delay_remove) {
3883                        sdev_printk(KERN_INFO, sdev,
3884                        "%s skip device_block for SES handle(0x%04x)\n",
3885                        __func__, sas_device_priv_data->sas_target->handle);
3886                        continue;
3887                }
3888                _scsih_internal_device_block(sdev, sas_device_priv_data);
3889        }
3890}
3891
3892/**
3893 * _scsih_block_io_device - set the device state to SDEV_BLOCK
3894 * @ioc: per adapter object
3895 * @handle: device handle
3896 *
3897 * During device pull we need to appropriately set the sdev state.
3898 */
3899static void
3900_scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3901{
3902        struct MPT3SAS_DEVICE *sas_device_priv_data;
3903        struct scsi_device *sdev;
3904        struct _sas_device *sas_device;
3905
3906        sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
3907
3908        shost_for_each_device(sdev, ioc->shost) {
3909                sas_device_priv_data = sdev->hostdata;
3910                if (!sas_device_priv_data)
3911                        continue;
3912                if (sas_device_priv_data->sas_target->handle != handle)
3913                        continue;
3914                if (sas_device_priv_data->block)
3915                        continue;
3916                if (sas_device && sas_device->pend_sas_rphy_add)
3917                        continue;
3918                if (sas_device_priv_data->ignore_delay_remove) {
3919                        sdev_printk(KERN_INFO, sdev,
3920                        "%s skip device_block for SES handle(0x%04x)\n",
3921                        __func__, sas_device_priv_data->sas_target->handle);
3922                        continue;
3923                }
3924                _scsih_internal_device_block(sdev, sas_device_priv_data);
3925        }
3926
3927        if (sas_device)
3928                sas_device_put(sas_device);
3929}
3930
3931/**
3932 * _scsih_block_io_to_children_attached_to_ex
3933 * @ioc: per adapter object
3934 * @sas_expander: the sas_device object
3935 *
3936 * This routine set sdev state to SDEV_BLOCK for all devices
3937 * attached to this expander. This function called when expander is
3938 * pulled.
3939 */
3940static void
3941_scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
3942        struct _sas_node *sas_expander)
3943{
3944        struct _sas_port *mpt3sas_port;
3945        struct _sas_device *sas_device;
3946        struct _sas_node *expander_sibling;
3947        unsigned long flags;
3948
3949        if (!sas_expander)
3950                return;
3951
3952        list_for_each_entry(mpt3sas_port,
3953           &sas_expander->sas_port_list, port_list) {
3954                if (mpt3sas_port->remote_identify.device_type ==
3955                    SAS_END_DEVICE) {
3956                        spin_lock_irqsave(&ioc->sas_device_lock, flags);
3957                        sas_device = __mpt3sas_get_sdev_by_addr(ioc,
3958                            mpt3sas_port->remote_identify.sas_address,
3959                            mpt3sas_port->hba_port);
3960                        if (sas_device) {
3961                                set_bit(sas_device->handle,
3962                                                ioc->blocking_handles);
3963                                sas_device_put(sas_device);
3964                        }
3965                        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3966                }
3967        }
3968
3969        list_for_each_entry(mpt3sas_port,
3970           &sas_expander->sas_port_list, port_list) {
3971
3972                if (mpt3sas_port->remote_identify.device_type ==
3973                    SAS_EDGE_EXPANDER_DEVICE ||
3974                    mpt3sas_port->remote_identify.device_type ==
3975                    SAS_FANOUT_EXPANDER_DEVICE) {
3976                        expander_sibling =
3977                            mpt3sas_scsih_expander_find_by_sas_address(
3978                            ioc, mpt3sas_port->remote_identify.sas_address,
3979                            mpt3sas_port->hba_port);
3980                        _scsih_block_io_to_children_attached_to_ex(ioc,
3981                            expander_sibling);
3982                }
3983        }
3984}
3985
3986/**
3987 * _scsih_block_io_to_children_attached_directly
3988 * @ioc: per adapter object
3989 * @event_data: topology change event data
3990 *
3991 * This routine set sdev state to SDEV_BLOCK for all devices
3992 * direct attached during device pull.
3993 */
3994static void
3995_scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
3996        Mpi2EventDataSasTopologyChangeList_t *event_data)
3997{
3998        int i;
3999        u16 handle;
4000        u16 reason_code;
4001
4002        for (i = 0; i < event_data->NumEntries; i++) {
4003                handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4004                if (!handle)
4005                        continue;
4006                reason_code = event_data->PHY[i].PhyStatus &
4007                    MPI2_EVENT_SAS_TOPO_RC_MASK;
4008                if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
4009                        _scsih_block_io_device(ioc, handle);
4010        }
4011}
4012
4013/**
4014 * _scsih_block_io_to_pcie_children_attached_directly
4015 * @ioc: per adapter object
4016 * @event_data: topology change event data
4017 *
4018 * This routine set sdev state to SDEV_BLOCK for all devices
4019 * direct attached during device pull/reconnect.
4020 */
4021static void
4022_scsih_block_io_to_pcie_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
4023                Mpi26EventDataPCIeTopologyChangeList_t *event_data)
4024{
4025        int i;
4026        u16 handle;
4027        u16 reason_code;
4028
4029        for (i = 0; i < event_data->NumEntries; i++) {
4030                handle =
4031                        le16_to_cpu(event_data->PortEntry[i].AttachedDevHandle);
4032                if (!handle)
4033                        continue;
4034                reason_code = event_data->PortEntry[i].PortStatus;
4035                if (reason_code ==
4036                                MPI26_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING)
4037                        _scsih_block_io_device(ioc, handle);
4038        }
4039}
4040/**
4041 * _scsih_tm_tr_send - send task management request
4042 * @ioc: per adapter object
4043 * @handle: device handle
4044 * Context: interrupt time.
4045 *
4046 * This code is to initiate the device removal handshake protocol
4047 * with controller firmware.  This function will issue target reset
4048 * using high priority request queue.  It will send a sas iounit
4049 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
4050 *
4051 * This is designed to send muliple task management request at the same
4052 * time to the fifo. If the fifo is full, we will append the request,
4053 * and process it in a future completion.
4054 */
4055static void
4056_scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4057{
4058        Mpi2SCSITaskManagementRequest_t *mpi_request;
4059        u16 smid;
4060        struct _sas_device *sas_device = NULL;
4061        struct _pcie_device *pcie_device = NULL;
4062        struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
4063        u64 sas_address = 0;
4064        unsigned long flags;
4065        struct _tr_list *delayed_tr;
4066        u32 ioc_state;
4067        u8 tr_method = 0;
4068        struct hba_port *port = NULL;
4069
4070        if (ioc->pci_error_recovery) {
4071                dewtprintk(ioc,
4072                           ioc_info(ioc, "%s: host in pci error recovery: handle(0x%04x)\n",
4073                                    __func__, handle));
4074                return;
4075        }
4076        ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4077        if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
4078                dewtprintk(ioc,
4079                           ioc_info(ioc, "%s: host is